X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fluci2%2Fui.git;a=blobdiff_plain;f=luci2%2Fsrc%2Frpcd%2Fluci2.c;h=f76f49eb0d5f9b0d54a9ba74719035a10513a44c;hp=23bd629eb9f71963a9b1be366be47d5775f08f1e;hb=e7d59e4da70c0971e120bd640dfc82bc0fed938d;hpb=229985257a52765c2994439a2f3e664df9f85909 diff --git a/luci2/src/rpcd/luci2.c b/luci2/src/rpcd/luci2.c index 23bd629..f76f49e 100644 --- a/luci2/src/rpcd/luci2.c +++ b/luci2/src/rpcd/luci2.c @@ -16,6 +16,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#define _GNU_SOURCE /* crypt() */ + #include #include #include @@ -31,6 +33,7 @@ #include #include #include +#include #include #include @@ -141,6 +144,15 @@ static const struct blobmsg_policy rpc_menu_policy[__RPC_MENU_MAX] = { .type = BLOBMSG_TYPE_STRING }, }; +enum { + RPC_SWITCH_NAME, + __RPC_SWITCH_MAX +}; + +static const struct blobmsg_policy rpc_switch_policy[__RPC_SWITCH_MAX] = { + [RPC_SWITCH_NAME] = { .name = "switch", .type = BLOBMSG_TYPE_STRING }, +}; + static int rpc_errno_status(void) @@ -1737,6 +1749,417 @@ rpc_luci2_network_routes6(struct ubus_context *ctx, struct ubus_object *obj, } +struct swconfig_state { + bool open; + void *array; + bool open2; + void *array2; + int port; +}; + +static int +swconfig_parse_list(struct blob_buf *blob, char *buf, int len, void *priv) +{ + char *p; + char *nl = strchr(buf, '\n'); + struct swconfig_state *s = priv; + + if (!nl) + return 0; + + if (!s->open) + { + s->open = true; + s->array = blobmsg_open_array(blob, "switches"); + } + + strtok(buf, "-"); + p = strtok(NULL, " \n"); + + if (p) + blobmsg_add_string(blob, NULL, p); + + return (nl - buf + 1); +} + +static int +swconfig_finish_list(struct blob_buf *blob, int status, void *priv) +{ + struct swconfig_state *s = priv; + + if (!s->open) + return UBUS_STATUS_NO_DATA; + + blobmsg_close_array(blob, s->array); + + return UBUS_STATUS_OK; +} + +static int +rpc_luci2_network_sw_list(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct swconfig_state *state = NULL; + const char *cmd[3] = { "swconfig", "list", NULL }; + + state = malloc(sizeof(*state)); + + if (!state) + return UBUS_STATUS_UNKNOWN_ERROR; + + memset(state, 0, sizeof(*state)); + + return ops->exec(cmd, NULL, swconfig_parse_list, NULL, swconfig_finish_list, + state, ctx, req); +} + + +static int +swconfig_parse_help(struct blob_buf *blob, char *buf, int len, void *priv) +{ + void *c; + char *p; + char *nl = strchr(buf, '\n'); + struct swconfig_state *s = priv; + + if (!nl) + return 0; + + if (!s->open) + { + s->open = true; + s->array = blobmsg_open_table(blob, "info"); + } + + switch (*buf) + { + case ' ': + strtok(buf, "-"); + p = strtok(NULL, "-\n"); + + if (p) + { + if (s->open2) + blobmsg_close_array(blob, s->array2); + + s->array2 = blobmsg_open_array(blob, p); + s->open2 = true; + } + + break; + + case '\t': + c = blobmsg_open_table(blob, NULL); + + strtok(buf, "("); + p = strtok(NULL, ")"); + + if (p) + blobmsg_add_string(blob, "type", p); + + p = strtok(NULL, ":( "); + + if (p) + blobmsg_add_string(blob, "name", p); + + p = strtok(NULL, "\n"); + *(nl - 1) = 0; + + if (p) + blobmsg_add_string(blob, "description", p + 1); + + blobmsg_close_table(blob, c); + break; + + default: + strtok(buf, "("); + p = strtok(NULL, ")"); + + if (p) + blobmsg_add_string(blob, "model", p); + + strtok(NULL, ":"); + p = strtok(NULL, "("); + + if (p) + blobmsg_add_u32(blob, "num_ports", atoi(p)); + + strtok(NULL, "@"); + p = strtok(NULL, ")"); + + if (p) + blobmsg_add_u32(blob, "cpu_port", atoi(p)); + + strtok(NULL, ":"); + p = strtok(NULL, "\n"); + + if (p) + blobmsg_add_u32(blob, "num_vlans", atoi(p)); + + break; + } + + return (nl - buf + 1); +} + +static int +swconfig_finish_help(struct blob_buf *blob, int status, void *priv) +{ + struct swconfig_state *s = priv; + + if (!s->open) + return UBUS_STATUS_NO_DATA; + + if (s->open2) + blobmsg_close_array(blob, s->array2); + + blobmsg_close_table(blob, s->array); + + return UBUS_STATUS_OK; +} + +static int +rpc_luci2_network_sw_info(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct swconfig_state *state = NULL; + struct blob_attr *tb[__RPC_SWITCH_MAX]; + const char *cmd[5] = { "swconfig", "dev", NULL, "help", NULL }; + + blobmsg_parse(rpc_switch_policy, __RPC_SWITCH_MAX, tb, + blob_data(msg), blob_len(msg)); + + if (!tb[RPC_SWITCH_NAME]) + return UBUS_STATUS_INVALID_ARGUMENT; + + state = malloc(sizeof(*state)); + + if (!state) + return UBUS_STATUS_UNKNOWN_ERROR; + + memset(state, 0, sizeof(*state)); + + cmd[2] = blobmsg_get_string(tb[RPC_SWITCH_NAME]); + + return ops->exec(cmd, NULL, swconfig_parse_help, NULL, swconfig_finish_help, + state, ctx, req); +} + + +static void +swconfig_parse_link(struct blob_buf *blob, char *val) +{ + char *p; + + int speed = 0; + + bool rxflow = false; + bool txflow = false; + bool duplex = false; + bool aneg = false; + bool up = false; + + for (p = strtok(val, " "); p; p = strtok(NULL, " ")) + { + if (!strncmp(p, "speed:", 6)) + speed = atoi(p + 6); + else if (!strcmp(p, "link:up")) + up = true; + else if (!strcmp(p, "txflow")) + txflow = true; + else if (!strcmp(p, "rxflow")) + rxflow = true; + else if (!strcmp(p, "full-duplex")) + duplex = true; + else if (!strcmp(p, "auto")) + aneg = true; + } + + blobmsg_add_u8(blob, "link", up); + blobmsg_add_u8(blob, "rx_flow_control", rxflow); + blobmsg_add_u8(blob, "tx_flow_control", txflow); + blobmsg_add_u8(blob, "full_duplex", duplex); + blobmsg_add_u8(blob, "auto_negotiation", aneg); + blobmsg_add_u32(blob, "speed", speed); +} + +static int +swconfig_parse_stat(struct blob_buf *blob, char *buf, int len, void *priv) +{ + char *p, *v; + char *nl = strchr(buf, '\n'); + struct swconfig_state *s = priv; + + if (!nl) + return 0; + + if (nl == buf) + return 1; + + if (!s->open) + { + s->open = true; + s->array = blobmsg_open_array(blob, "ports"); + } + + p = strtok(buf, " :\t"); + + if (p) + { + if (!strcmp(p, "Port")) + { + if (s->open2) + blobmsg_close_table(blob, s->array2); + + s->array2 = blobmsg_open_table(blob, NULL); + s->open2 = true; + } + else if (s->open2) + { + v = strtok(NULL, "\n"); + + if (v) + { + if (!strcmp(p, "link")) + swconfig_parse_link(blob, v); + } + } + } + + return (nl - buf + 1); +} + +static int +swconfig_finish_stat(struct blob_buf *blob, int status, void *priv) +{ + struct swconfig_state *s = priv; + + if (!s->open) + return UBUS_STATUS_NO_DATA; + + if (s->open2) + blobmsg_close_table(blob, s->array2); + + blobmsg_close_array(blob, s->array); + + return UBUS_STATUS_OK; +} + +static int +rpc_luci2_network_sw_status(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct swconfig_state *state = NULL; + struct blob_attr *tb[__RPC_SWITCH_MAX]; + const char *cmd[5] = { "swconfig", "dev", NULL, "show", NULL }; + + blobmsg_parse(rpc_switch_policy, __RPC_SWITCH_MAX, tb, + blob_data(msg), blob_len(msg)); + + if (!tb[RPC_SWITCH_NAME]) + return UBUS_STATUS_INVALID_ARGUMENT; + + state = malloc(sizeof(*state)); + + if (!state) + return UBUS_STATUS_UNKNOWN_ERROR; + + memset(state, 0, sizeof(*state)); + + cmd[2] = blobmsg_get_string(tb[RPC_SWITCH_NAME]); + + return ops->exec(cmd, NULL, swconfig_parse_stat, NULL, swconfig_finish_stat, + state, ctx, req); +} + +enum { + NETWORK_CMD_PING, + NETWORK_CMD_PING6, + NETWORK_CMD_TRACEROUTE, + NETWORK_CMD_TRACEROUTE6, + NETWORK_CMD_NSLOOKUP +}; + +static int +network_cmd(struct ubus_context *ctx, struct ubus_request_data *req, + struct blob_attr *msg, int which) +{ + char *arg; + struct blob_attr *tb[__RPC_D_MAX]; + + blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb, + blob_data(msg), blob_len(msg)); + + if (!tb[RPC_D_DATA]) + return UBUS_STATUS_INVALID_ARGUMENT; + + arg = blobmsg_get_string(tb[RPC_D_DATA]); + + const char *cmds[][8] = { + [NETWORK_CMD_PING] = { + "ping", "-c", "5", "-W", "1", arg + }, + [NETWORK_CMD_PING6] = { + "ping6", "-c", "5", "-W", "1", arg + }, + [NETWORK_CMD_TRACEROUTE] = { + "traceroute", "-q", "1", "-w", "1", "-n", arg + }, + [NETWORK_CMD_TRACEROUTE6] = { + "traceroute6", "-q", "1", "-w", "2", "-n", arg + }, + [NETWORK_CMD_NSLOOKUP] = { + "nslookup", arg + } + }; + + return ops->exec(cmds[which], NULL, NULL, NULL, NULL, NULL, ctx, req); +} + +static int +rpc_luci2_network_ping(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + return network_cmd(ctx, req, msg, NETWORK_CMD_PING); +} + +static int +rpc_luci2_network_ping6(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + return network_cmd(ctx, req, msg, NETWORK_CMD_PING6); +} + +static int +rpc_luci2_network_traceroute(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + return network_cmd(ctx, req, msg, NETWORK_CMD_TRACEROUTE); +} + +static int +rpc_luci2_network_traceroute6(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + return network_cmd(ctx, req, msg, NETWORK_CMD_TRACEROUTE6); +} + +static int +rpc_luci2_network_nslookup(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + return network_cmd(ctx, req, msg, NETWORK_CMD_NSLOOKUP); +} + + struct opkg_state { int cur_offset; int cur_count; @@ -2087,6 +2510,78 @@ skip: } +static void +parse_acl_file(struct blob_buf *acls, const char *path) +{ + struct blob_buf acl = { 0 }; + struct blob_attr *cur; + void *c; + int rem; + + blob_buf_init(&acl, 0); + + if (blobmsg_add_json_from_file(&acl, path)) + { + c = blobmsg_open_table(acls, NULL); + + blob_for_each_attr(cur, acl.head, rem) + blobmsg_add_blob(acls, cur); + + blobmsg_close_table(acls, c); + } + + blob_buf_free(&acl); +} + +static int +rpc_luci2_ui_acls(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + int i; + void *c; + glob_t gl; + + if (glob(RPC_SESSION_ACL_DIR "/*.json", 0, NULL, &gl)) + return rpc_errno_status(); + + blob_buf_init(&buf, 0); + c = blobmsg_open_array(&buf, "acls"); + + for (i = 0; i < gl.gl_pathc; i++) + parse_acl_file(&buf, gl.gl_pathv[i]); + + globfree(&gl); + blobmsg_close_array(&buf, c); + + ubus_send_reply(ctx, req, buf.head); + return 0; +} + +static int +rpc_luci2_ui_crypt(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + char *hash; + struct blob_attr *tb[__RPC_D_MAX]; + + blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb, + blob_data(msg), blob_len(msg)); + + if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 128) + return UBUS_STATUS_INVALID_ARGUMENT; + + hash = crypt(blobmsg_get_string(tb[RPC_D_DATA]), "$1$"); + + blob_buf_init(&buf, 0); + blobmsg_add_string(&buf, "crypt", hash); + + ubus_send_reply(ctx, req, buf.head); + return 0; +} + + static int rpc_luci2_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx) { @@ -2149,6 +2644,21 @@ rpc_luci2_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx) UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6), UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes), UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6), + UBUS_METHOD_NOARG("switch_list", rpc_luci2_network_sw_list), + UBUS_METHOD("switch_info", rpc_luci2_network_sw_info, + rpc_switch_policy), + UBUS_METHOD("switch_status", rpc_luci2_network_sw_status, + rpc_switch_policy), + UBUS_METHOD("ping", rpc_luci2_network_ping, + rpc_data_policy), + UBUS_METHOD("ping6", rpc_luci2_network_ping6, + rpc_data_policy), + UBUS_METHOD("traceroute", rpc_luci2_network_traceroute, + rpc_data_policy), + UBUS_METHOD("traceroute6", rpc_luci2_network_traceroute6, + rpc_data_policy), + UBUS_METHOD("nslookup", rpc_luci2_network_nslookup, + rpc_data_policy) }; static struct ubus_object_type luci2_network_type = @@ -2191,7 +2701,10 @@ rpc_luci2_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx) static const struct ubus_method luci2_ui_methods[] = { - UBUS_METHOD_NOARG("menu", rpc_luci2_ui_menu) + UBUS_METHOD_NOARG("menu", rpc_luci2_ui_menu), + UBUS_METHOD_NOARG("acls", rpc_luci2_ui_acls), + UBUS_METHOD("crypt", rpc_luci2_ui_crypt, + rpc_data_policy) }; static struct ubus_object_type luci2_ui_type =