2 * luci-rpcd - LuCI UBUS RPC server
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #include <sys/types.h>
29 #include <arpa/inet.h>
35 static struct blob_buf buf;
36 static struct uci_context *cursor;
44 static const struct blobmsg_policy rpc_signal_policy[__RPC_S_MAX] = {
45 [RPC_S_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
46 [RPC_S_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
55 static const struct blobmsg_policy rpc_init_policy[__RPC_I_MAX] = {
56 [RPC_I_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
57 [RPC_I_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
65 static const struct blobmsg_policy rpc_sshkey_policy[__RPC_K_MAX] = {
66 [RPC_K_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
75 static const struct blobmsg_policy rpc_password_policy[__RPC_P_MAX] = {
76 [RPC_P_USER] = { .name = "user", .type = BLOBMSG_TYPE_STRING },
77 [RPC_P_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
87 static const struct blobmsg_policy rpc_opkg_match_policy[__RPC_OM_MAX] = {
88 [RPC_OM_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
89 [RPC_OM_OFFSET] = { .name = "offset", .type = BLOBMSG_TYPE_INT32 },
90 [RPC_OM_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
98 static const struct blobmsg_policy rpc_opkg_package_policy[__RPC_OP_MAX] = {
99 [RPC_OP_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
107 static const struct blobmsg_policy rpc_opkg_config_policy[__RPC_OC_MAX] = {
108 [RPC_OC_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
113 rpc_errno_status(void)
118 return UBUS_STATUS_PERMISSION_DENIED;
121 return UBUS_STATUS_INVALID_ARGUMENT;
124 return UBUS_STATUS_NOT_FOUND;
127 return UBUS_STATUS_INVALID_ARGUMENT;
130 return UBUS_STATUS_UNKNOWN_ERROR;
135 log_read(FILE *log, int logsize)
141 logsize = RPC_LUCI2_DEF_LOGSIZE;
143 len = (logsize > RPC_LUCI2_MAX_LOGSIZE) ? RPC_LUCI2_MAX_LOGSIZE : logsize;
144 logbuf = blobmsg_alloc_string_buffer(&buf, "log", len + 1);
149 while (logsize > RPC_LUCI2_MAX_LOGSIZE)
151 len = logsize % RPC_LUCI2_MAX_LOGSIZE;
154 len = RPC_LUCI2_MAX_LOGSIZE;
156 fread(logbuf, 1, len, log);
160 len = fread(logbuf, 1, logsize, log);
163 blobmsg_add_string_buffer(&buf);
167 rpc_luci2_system_log(struct ubus_context *ctx, struct ubus_object *obj,
168 struct ubus_request_data *req, const char *method,
169 struct blob_attr *msg)
173 const char *logfile = NULL;
175 struct uci_package *p;
176 struct uci_element *e;
177 struct uci_section *s;
178 struct uci_ptr ptr = { .package = "system" };
180 uci_load(cursor, ptr.package, &p);
183 return UBUS_STATUS_NOT_FOUND;
185 uci_foreach_element(&p->sections, e)
187 s = uci_to_section(e);
189 if (strcmp(s->type, "system"))
193 ptr.option = "log_type";
194 ptr.section = e->name;
195 uci_lookup_ptr(cursor, &ptr, NULL, true);
199 if (ptr.o && ptr.o->type == UCI_TYPE_STRING &&
200 !strcmp(ptr.o->v.string, "file"))
203 ptr.option = "log_file";
204 uci_lookup_ptr(cursor, &ptr, NULL, true);
206 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
207 logfile = ptr.o->v.string;
209 logfile = "/var/log/messages";
211 if (stat(logfile, &st) || !(log = fopen(logfile, "r")))
214 logsize = st.st_size;
219 ptr.option = "log_size";
220 uci_lookup_ptr(cursor, &ptr, NULL, true);
222 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
223 logsize = atoi(ptr.o->v.string) * 1024;
225 if (!(log = popen("logread", "r")))
229 blob_buf_init(&buf, 0);
231 log_read(log, logsize);
234 uci_unload(cursor, p);
235 ubus_send_reply(ctx, req, buf.head);
239 uci_unload(cursor, p);
240 return rpc_errno_status();
244 rpc_luci2_system_dmesg(struct ubus_context *ctx, struct ubus_object *obj,
245 struct ubus_request_data *req, const char *method,
246 struct blob_attr *msg)
250 if (!(log = popen("dmesg", "r")))
251 return rpc_errno_status();
253 blob_buf_init(&buf, 0);
255 log_read(log, RPC_LUCI2_MAX_LOGSIZE);
258 ubus_send_reply(ctx, req, buf.head);
263 rpc_luci2_process_list(struct ubus_context *ctx, struct ubus_object *obj,
264 struct ubus_request_data *req, const char *method,
265 struct blob_attr *msg)
270 char *pid, *ppid, *user, *stat, *vsz, *pvsz, *pcpu, *cmd;
272 if (!(top = popen("/bin/busybox top -bn1", "r")))
273 return rpc_errno_status();
275 blob_buf_init(&buf, 0);
276 c = blobmsg_open_array(&buf, "processes");
278 while (fgets(line, sizeof(line) - 1, top))
280 pid = strtok(line, " ");
282 if (*pid < '0' || *pid > '9')
285 ppid = strtok(NULL, " ");
286 user = strtok(NULL, " ");
287 stat = strtok(NULL, " ");
300 vsz = strtok(stat + 4, " ");
301 pvsz = strtok(NULL, " ");
302 pcpu = strtok(NULL, " ");
303 cmd = strtok(NULL, "\n");
308 d = blobmsg_open_table(&buf, NULL);
310 blobmsg_add_u32(&buf, "pid", atoi(pid));
311 blobmsg_add_u32(&buf, "ppid", atoi(ppid));
312 blobmsg_add_string(&buf, "user", user);
313 blobmsg_add_string(&buf, "stat", stat);
314 blobmsg_add_u32(&buf, "vsize", atoi(vsz) * 1024);
315 blobmsg_add_u32(&buf, "vsize_percent", atoi(pvsz));
316 blobmsg_add_u32(&buf, "cpu_percent", atoi(pcpu));
317 blobmsg_add_string(&buf, "command", cmd);
319 blobmsg_close_table(&buf, d);
323 blobmsg_close_array(&buf, c);
325 ubus_send_reply(ctx, req, buf.head);
330 rpc_luci2_process_signal(struct ubus_context *ctx, struct ubus_object *obj,
331 struct ubus_request_data *req, const char *method,
332 struct blob_attr *msg)
335 struct blob_attr *tb[__RPC_S_MAX];
337 blobmsg_parse(rpc_signal_policy, __RPC_S_MAX, tb,
338 blob_data(msg), blob_len(msg));
340 if (!tb[RPC_S_SIGNAL] || !tb[RPC_S_PID])
343 return rpc_errno_status();
346 pid = blobmsg_get_u32(tb[RPC_S_PID]);
347 sig = blobmsg_get_u32(tb[RPC_S_SIGNAL]);
350 return rpc_errno_status();
356 rpc_luci2_init_list(struct ubus_context *ctx, struct ubus_object *obj,
357 struct ubus_request_data *req, const char *method,
358 struct blob_attr *msg)
362 char *p, path[PATH_MAX];
368 if (!(d = opendir("/etc/init.d")))
369 return rpc_errno_status();
371 blob_buf_init(&buf, 0);
372 c = blobmsg_open_array(&buf, "initscripts");
374 while ((e = readdir(d)) != NULL)
376 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s", e->d_name);
378 if (stat(path, &s) || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR))
381 if ((f = fopen(path, "r")) != NULL)
384 p = fgets(path, sizeof(path) - 1, f);
386 if (!p || !strstr(p, "/etc/rc.common"))
389 t = blobmsg_open_table(&buf, NULL);
391 blobmsg_add_string(&buf, "name", e->d_name);
393 while (fgets(path, sizeof(path) - 1, f))
395 p = strtok(path, "= \t");
397 if (!strcmp(p, "START") && !!(p = strtok(NULL, "= \t\n")))
400 blobmsg_add_u32(&buf, "start", n);
402 else if (!strcmp(p, "STOP") && !!(p = strtok(NULL, "= \t\n")))
404 blobmsg_add_u32(&buf, "stop", atoi(p));
411 snprintf(path, sizeof(path) - 1, "/etc/rc.d/S%02d%s",
414 blobmsg_add_u8(&buf, "enabled",
415 (!stat(path, &s) && (s.st_mode & S_IXUSR)));
419 blobmsg_add_u8(&buf, "enabled", 0);
422 blobmsg_close_table(&buf, t);
430 blobmsg_close_array(&buf, c);
432 ubus_send_reply(ctx, req, buf.head);
437 rpc_luci2_init_action(struct ubus_context *ctx, struct ubus_object *obj,
438 struct ubus_request_data *req, const char *method,
439 struct blob_attr *msg)
446 struct blob_attr *tb[__RPC_I_MAX];
448 blobmsg_parse(rpc_init_policy, __RPC_I_MAX, tb,
449 blob_data(msg), blob_len(msg));
451 if (!tb[RPC_I_NAME] || !tb[RPC_I_ACTION])
452 return UBUS_STATUS_INVALID_ARGUMENT;
454 action = blobmsg_data(tb[RPC_I_ACTION]);
456 if (strcmp(action, "start") && strcmp(action, "stop") &&
457 strcmp(action, "reload") && strcmp(action, "restart") &&
458 strcmp(action, "enable") && strcmp(action, "disable"))
459 return UBUS_STATUS_INVALID_ARGUMENT;
461 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s",
462 (char *)blobmsg_data(tb[RPC_I_NAME]));
465 return rpc_errno_status();
467 if (!(s.st_mode & S_IXUSR))
468 return UBUS_STATUS_PERMISSION_DENIED;
470 switch ((pid = fork()))
473 return rpc_errno_status();
478 if ((fd = open("/dev/null", O_RDWR)) > -1)
489 if (execl(path, path, action, NULL))
490 return rpc_errno_status();
498 rpc_luci2_sshkeys_get(struct ubus_context *ctx, struct ubus_object *obj,
499 struct ubus_request_data *req, const char *method,
500 struct blob_attr *msg)
506 if (!(f = fopen("/etc/dropbear/authorized_keys", "r")))
507 return rpc_errno_status();
509 blob_buf_init(&buf, 0);
510 c = blobmsg_open_array(&buf, "keys");
512 while (fgets(line, sizeof(line) - 1, f))
514 for (p = line + strlen(line) - 1; (p > line) && isspace(*p); p--)
517 for (p = line; isspace(*p); p++)
521 blobmsg_add_string(&buf, NULL, p);
524 blobmsg_close_array(&buf, c);
527 ubus_send_reply(ctx, req, buf.head);
532 rpc_luci2_sshkeys_set(struct ubus_context *ctx, struct ubus_object *obj,
533 struct ubus_request_data *req, const char *method,
534 struct blob_attr *msg)
538 struct blob_attr *cur, *tb[__RPC_K_MAX];
540 blobmsg_parse(rpc_sshkey_policy, __RPC_K_MAX, tb,
541 blob_data(msg), blob_len(msg));
544 return UBUS_STATUS_INVALID_ARGUMENT;
546 if (!(f = fopen("/etc/dropbear/authorized_keys", "w")))
547 return rpc_errno_status();
549 blobmsg_for_each_attr(cur, tb[RPC_K_KEYS], rem)
551 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
554 fwrite(blobmsg_data(cur), blobmsg_data_len(cur) - 1, 1, f);
555 fwrite("\n", 1, 1, f);
563 rpc_luci2_password_set(struct ubus_context *ctx, struct ubus_object *obj,
564 struct ubus_request_data *req, const char *method,
565 struct blob_attr *msg)
570 struct blob_attr *tb[__RPC_P_MAX];
572 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
573 blob_data(msg), blob_len(msg));
575 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
576 return UBUS_STATUS_INVALID_ARGUMENT;
578 if (stat("/usr/bin/passwd", &s))
579 return UBUS_STATUS_NOT_FOUND;
581 if (!(s.st_mode & S_IXUSR))
582 return UBUS_STATUS_PERMISSION_DENIED;
585 return rpc_errno_status();
587 switch ((pid = fork()))
592 return rpc_errno_status();
601 if ((fd = open("/dev/null", O_RDWR)) > -1)
610 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
611 blobmsg_data(tb[RPC_P_USER]), NULL))
612 return rpc_errno_status();
617 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
618 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
619 write(fds[1], "\n", 1);
623 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
624 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
625 write(fds[1], "\n", 1);
629 waitpid(pid, NULL, 0);
637 dnsmasq_leasefile(void)
640 struct uci_package *p;
641 struct uci_element *e;
642 struct uci_section *s;
643 struct uci_ptr ptr = {
646 .option = "leasefile"
649 uci_load(cursor, ptr.package, &p);
654 uci_foreach_element(&p->sections, e)
656 s = uci_to_section(e);
658 if (strcmp(s->type, "dnsmasq"))
661 ptr.section = e->name;
662 uci_lookup_ptr(cursor, &ptr, NULL, true);
666 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
667 leases = fopen(ptr.o->v.string, "r");
669 uci_unload(cursor, p);
675 rpc_luci2_network_leases(struct ubus_context *ctx, struct ubus_object *obj,
676 struct ubus_request_data *req, const char *method,
677 struct blob_attr *msg)
682 char *ts, *mac, *addr, *name;
683 time_t now = time(NULL);
685 blob_buf_init(&buf, 0);
686 c = blobmsg_open_array(&buf, "leases");
688 leases = dnsmasq_leasefile();
693 while (fgets(line, sizeof(line) - 1, leases))
695 ts = strtok(line, " \t");
696 mac = strtok(NULL, " \t");
697 addr = strtok(NULL, " \t");
698 name = strtok(NULL, " \t");
700 if (!ts || !mac || !addr || !name)
703 if (strchr(addr, ':'))
706 d = blobmsg_open_table(&buf, NULL);
708 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
709 blobmsg_add_string(&buf, "macaddr", mac);
710 blobmsg_add_string(&buf, "ipaddr", addr);
712 if (strcmp(name, "*"))
713 blobmsg_add_string(&buf, "hostname", name);
715 blobmsg_close_table(&buf, d);
721 blobmsg_close_array(&buf, c);
722 ubus_send_reply(ctx, req, buf.head);
728 rpc_luci2_network_leases6(struct ubus_context *ctx, struct ubus_object *obj,
729 struct ubus_request_data *req, const char *method,
730 struct blob_attr *msg)
735 char *ts, *mac, *addr, *name, *duid;
736 time_t now = time(NULL);
738 blob_buf_init(&buf, 0);
739 c = blobmsg_open_array(&buf, "leases");
741 leases = fopen("/tmp/hosts/6relayd", "r");
745 while (fgets(line, sizeof(line) - 1, leases))
747 if (strncmp(line, "# ", 2))
750 strtok(line + 2, " \t"); /* iface */
752 duid = strtok(NULL, " \t");
754 strtok(NULL, " \t"); /* iaid */
756 name = strtok(NULL, " \t");
757 ts = strtok(NULL, " \t");
759 strtok(NULL, " \t"); /* id */
760 strtok(NULL, " \t"); /* length */
762 addr = strtok(NULL, " \t\n");
767 d = blobmsg_open_table(&buf, NULL);
769 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
770 blobmsg_add_string(&buf, "duid", duid);
771 blobmsg_add_string(&buf, "ip6addr", addr);
773 if (strcmp(name, "-"))
774 blobmsg_add_string(&buf, "hostname", name);
776 blobmsg_close_array(&buf, d);
783 leases = dnsmasq_leasefile();
788 while (fgets(line, sizeof(line) - 1, leases))
790 ts = strtok(line, " \t");
791 mac = strtok(NULL, " \t");
792 addr = strtok(NULL, " \t");
793 name = strtok(NULL, " \t");
794 duid = strtok(NULL, " \t\n");
796 if (!ts || !mac || !addr || !duid)
799 if (!strchr(addr, ':'))
802 d = blobmsg_open_table(&buf, NULL);
804 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
805 blobmsg_add_string(&buf, "macaddr", mac);
806 blobmsg_add_string(&buf, "ip6addr", addr);
808 if (strcmp(name, "*"))
809 blobmsg_add_string(&buf, "hostname", name);
811 if (strcmp(duid, "*"))
812 blobmsg_add_string(&buf, "duid", name);
814 blobmsg_close_table(&buf, d);
821 blobmsg_close_array(&buf, c);
822 ubus_send_reply(ctx, req, buf.head);
828 rpc_luci2_network_ct_count(struct ubus_context *ctx, struct ubus_object *obj,
829 struct ubus_request_data *req, const char *method,
830 struct blob_attr *msg)
835 blob_buf_init(&buf, 0);
837 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL)
839 if (fgets(line, sizeof(line) - 1, f))
840 blobmsg_add_u32(&buf, "count", atoi(line));
845 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL)
847 if (fgets(line, sizeof(line) - 1, f))
848 blobmsg_add_u32(&buf, "limit", atoi(line));
853 ubus_send_reply(ctx, req, buf.head);
859 rpc_luci2_network_ct_table(struct ubus_context *ctx, struct ubus_object *obj,
860 struct ubus_request_data *req, const char *method,
861 struct blob_attr *msg)
869 blob_buf_init(&buf, 0);
870 c = blobmsg_open_array(&buf, "entries");
872 if ((f = fopen("/proc/net/nf_conntrack", "r")) != NULL)
874 while (fgets(line, sizeof(line) - 1, f))
876 d = blobmsg_open_table(&buf, NULL);
877 memset(seen, 0, sizeof(seen));
879 for (i = 0, p = strtok(line, " "); p; i++, p = strtok(NULL, " "))
882 blobmsg_add_u8(&buf, "ipv6", !strcmp(p, "ipv6"));
884 blobmsg_add_u32(&buf, "protocol", atoi(p));
886 blobmsg_add_u32(&buf, "expires", atoi(p));
892 if (!seen[0] && !strncmp(p, "src=", 4))
894 blobmsg_add_string(&buf, "src", p + 4);
897 else if (!seen[1] && !strncmp(p, "dst=", 4))
899 blobmsg_add_string(&buf, "dest", p + 4);
902 else if (!seen[2] && !strncmp(p, "sport=", 6))
904 blobmsg_add_u32(&buf, "sport", atoi(p + 6));
907 else if (!seen[3] && !strncmp(p, "dport=", 6))
909 blobmsg_add_u32(&buf, "dport", atoi(p + 6));
912 else if (!strncmp(p, "packets=", 8))
914 blobmsg_add_u32(&buf,
915 seen[4] ? "tx_packets" : "rx_packets",
919 else if (!strncmp(p, "bytes=", 6))
921 blobmsg_add_u32(&buf,
922 seen[5] ? "tx_bytes" : "rx_bytes",
929 blobmsg_close_table(&buf, d);
935 blobmsg_close_array(&buf, c);
936 ubus_send_reply(ctx, req, buf.head);
942 rpc_luci2_network_arp_table(struct ubus_context *ctx, struct ubus_object *obj,
943 struct ubus_request_data *req, const char *method,
944 struct blob_attr *msg)
948 char *addr, *mac, *dev, line[128];
950 blob_buf_init(&buf, 0);
951 c = blobmsg_open_array(&buf, "entries");
953 if ((f = fopen("/proc/net/arp", "r")) != NULL)
955 /* skip header line */
956 fgets(line, sizeof(line) - 1, f);
958 while (fgets(line, sizeof(line) - 1, f))
960 addr = strtok(line, " \t");
962 strtok(NULL, " \t"); /* HW type */
963 strtok(NULL, " \t"); /* Flags */
965 mac = strtok(NULL, " \t");
967 strtok(NULL, " \t"); /* Mask */
969 dev = strtok(NULL, " \t\n");
974 d = blobmsg_open_table(&buf, NULL);
975 blobmsg_add_string(&buf, "ipaddr", addr);
976 blobmsg_add_string(&buf, "macaddr", mac);
977 blobmsg_add_string(&buf, "device", dev);
978 blobmsg_close_table(&buf, d);
984 blobmsg_close_array(&buf, c);
985 ubus_send_reply(ctx, req, buf.head);
991 put_hexaddr(const char *name, const char *s, const char *m)
995 char as[sizeof("255.255.255.255/32\0")];
997 a.s_addr = strtoul(s, NULL, 16);
998 inet_ntop(AF_INET, &a, as, sizeof(as));
1002 for (a.s_addr = ntohl(strtoul(m, NULL, 16)), bits = 0;
1003 a.s_addr & 0x80000000;
1007 sprintf(as + strlen(as), "/%u", bits);
1010 blobmsg_add_string(&buf, name, as);
1014 rpc_luci2_network_routes(struct ubus_context *ctx, struct ubus_object *obj,
1015 struct ubus_request_data *req, const char *method,
1016 struct blob_attr *msg)
1020 char *dst, *dmask, *next, *metric, *device;
1024 if (!(routes = fopen("/proc/net/route", "r")))
1025 return rpc_errno_status();
1027 blob_buf_init(&buf, 0);
1028 c = blobmsg_open_array(&buf, "routes");
1030 /* skip header line */
1031 fgets(line, sizeof(line) - 1, routes);
1033 while (fgets(line, sizeof(line) - 1, routes))
1035 device = strtok(line, "\t ");
1036 dst = strtok(NULL, "\t ");
1037 next = strtok(NULL, "\t ");
1039 strtok(NULL, "\t "); /* flags */
1040 strtok(NULL, "\t "); /* refcount */
1041 strtok(NULL, "\t "); /* usecount */
1043 metric = strtok(NULL, "\t ");
1044 dmask = strtok(NULL, "\t ");
1049 d = blobmsg_open_table(&buf, NULL);
1051 put_hexaddr("target", dst, dmask);
1052 put_hexaddr("nexthop", next, NULL);
1054 n = strtoul(metric, NULL, 10);
1055 blobmsg_add_u32(&buf, "metric", n);
1057 blobmsg_add_string(&buf, "device", device);
1059 blobmsg_close_table(&buf, d);
1062 blobmsg_close_array(&buf, c);
1065 ubus_send_reply(ctx, req, buf.head);
1070 put_hex6addr(const char *name, const char *s, const char *m)
1074 char as[INET6_ADDRSTRLEN + sizeof("/128")];
1077 (((x) <= '9') ? ((x) - '0') : \
1078 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1081 for (i = 0; i < 16; i++, s += 2)
1082 a.s6_addr[i] = (16 * hex(*s)) + hex(*(s+1));
1084 inet_ntop(AF_INET6, &a, as, sizeof(as));
1087 sprintf(as + strlen(as), "/%lu", strtoul(m, NULL, 16));
1089 blobmsg_add_string(&buf, name, as);
1093 rpc_luci2_network_routes6(struct ubus_context *ctx, struct ubus_object *obj,
1094 struct ubus_request_data *req, const char *method,
1095 struct blob_attr *msg)
1099 char *src, *smask, *dst, *dmask, *next, *metric, *flags, *device;
1103 if (!(routes = fopen("/proc/net/ipv6_route", "r")))
1104 return rpc_errno_status();
1106 blob_buf_init(&buf, 0);
1107 c = blobmsg_open_array(&buf, "routes");
1109 while (fgets(line, sizeof(line) - 1, routes))
1111 dst = strtok(line, " ");
1112 dmask = strtok(NULL, " ");
1113 src = strtok(NULL, " ");
1114 smask = strtok(NULL, " ");
1115 next = strtok(NULL, " ");
1116 metric = strtok(NULL, " ");
1118 strtok(NULL, " "); /* refcount */
1119 strtok(NULL, " "); /* usecount */
1121 flags = strtok(NULL, " ");
1122 device = strtok(NULL, " \n");
1127 n = strtoul(flags, NULL, 16);
1132 d = blobmsg_open_table(&buf, NULL);
1134 put_hex6addr("target", dst, dmask);
1135 put_hex6addr("source", src, smask);
1136 put_hex6addr("nexthop", next, NULL);
1138 n = strtoul(metric, NULL, 16);
1139 blobmsg_add_u32(&buf, "metric", n);
1141 blobmsg_add_string(&buf, "device", device);
1143 blobmsg_close_table(&buf, d);
1146 blobmsg_close_array(&buf, c);
1149 ubus_send_reply(ctx, req, buf.head);
1165 opkg_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1167 struct opkg_state *s = priv;
1170 char *nl = strchr(buf, '\n');
1171 char *name = NULL, *vers = NULL, *desc = NULL;
1179 if (s->cur_offset++ < s->req_offset)
1182 if (s->cur_count++ >= s->req_count)
1188 s->array = blobmsg_open_array(blob, "packages");
1191 for (ptr = buf, last = buf, *nl = 0; ptr <= nl; ptr++)
1193 if (!*ptr || (*ptr == ' ' && *(ptr+1) == '-' && *(ptr+2) == ' '))
1205 desc = *ptr ? (ptr + 3) : NULL;
1214 c = blobmsg_open_array(blob, NULL);
1216 blobmsg_add_string(blob, NULL, name);
1217 blobmsg_add_string(blob, NULL, vers);
1220 blobmsg_add_string(blob, NULL, desc);
1222 blobmsg_close_array(blob, c);
1226 return (nl - buf + 1);
1230 opkg_finish_list(struct blob_buf *blob, int status, void *priv)
1232 struct opkg_state *s = priv;
1237 blobmsg_close_array(blob, s->array);
1238 blobmsg_add_u32(blob, "total", s->total);
1242 opkg_exec_list(const char *action, struct blob_attr *msg,
1243 struct ubus_context *ctx, struct ubus_request_data *req)
1245 struct opkg_state *state = NULL;
1246 struct blob_attr *tb[__RPC_OM_MAX];
1247 const char *cmd[5] = { "opkg", action, "-nocase", NULL, NULL };
1249 blobmsg_parse(rpc_opkg_match_policy, __RPC_OM_MAX, tb,
1250 blob_data(msg), blob_len(msg));
1252 state = malloc(sizeof(*state));
1255 return UBUS_STATUS_UNKNOWN_ERROR;
1257 memset(state, 0, sizeof(*state));
1259 if (tb[RPC_OM_PATTERN])
1260 cmd[3] = blobmsg_data(tb[RPC_OM_PATTERN]);
1262 if (tb[RPC_OM_LIMIT])
1263 state->req_count = blobmsg_get_u32(tb[RPC_OM_LIMIT]);
1265 if (tb[RPC_OM_OFFSET])
1266 state->req_offset = blobmsg_get_u32(tb[RPC_OM_OFFSET]);
1268 if (state->req_offset < 0)
1269 state->req_offset = 0;
1271 if (state->req_count <= 0 || state->req_count > 100)
1272 state->req_count = 100;
1274 return rpc_exec(cmd, opkg_parse_list, NULL, opkg_finish_list,
1280 rpc_luci2_opkg_list(struct ubus_context *ctx, struct ubus_object *obj,
1281 struct ubus_request_data *req, const char *method,
1282 struct blob_attr *msg)
1284 return opkg_exec_list("list", msg, ctx, req);
1288 rpc_luci2_opkg_list_installed(struct ubus_context *ctx, struct ubus_object *obj,
1289 struct ubus_request_data *req, const char *method,
1290 struct blob_attr *msg)
1292 return opkg_exec_list("list-installed", msg, ctx, req);
1296 rpc_luci2_opkg_find(struct ubus_context *ctx, struct ubus_object *obj,
1297 struct ubus_request_data *req, const char *method,
1298 struct blob_attr *msg)
1300 return opkg_exec_list("find", msg, ctx, req);
1304 rpc_luci2_opkg_update(struct ubus_context *ctx, struct ubus_object *obj,
1305 struct ubus_request_data *req, const char *method,
1306 struct blob_attr *msg)
1308 const char *cmd[3] = { "opkg", "update", NULL };
1309 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1313 rpc_luci2_opkg_install(struct ubus_context *ctx, struct ubus_object *obj,
1314 struct ubus_request_data *req, const char *method,
1315 struct blob_attr *msg)
1317 struct blob_attr *tb[__RPC_OP_MAX];
1318 const char *cmd[5] = { "opkg", "--force-overwrite",
1319 "install", NULL, NULL };
1321 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1322 blob_data(msg), blob_len(msg));
1324 if (!tb[RPC_OP_PACKAGE])
1325 return UBUS_STATUS_INVALID_ARGUMENT;
1327 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1329 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1333 rpc_luci2_opkg_remove(struct ubus_context *ctx, struct ubus_object *obj,
1334 struct ubus_request_data *req, const char *method,
1335 struct blob_attr *msg)
1337 struct blob_attr *tb[__RPC_OP_MAX];
1338 const char *cmd[5] = { "opkg", "--force-removal-of-dependent-packages",
1339 "remove", NULL, NULL };
1341 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1342 blob_data(msg), blob_len(msg));
1344 if (!tb[RPC_OP_PACKAGE])
1345 return UBUS_STATUS_INVALID_ARGUMENT;
1347 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1349 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1353 rpc_luci2_opkg_config_get(struct ubus_context *ctx, struct ubus_object *obj,
1354 struct ubus_request_data *req, const char *method,
1355 struct blob_attr *msg)
1358 char conf[2048] = { 0 };
1360 if (!(f = fopen("/etc/opkg.conf", "r")))
1361 return rpc_errno_status();
1363 fread(conf, sizeof(conf) - 1, 1, f);
1366 blob_buf_init(&buf, 0);
1367 blobmsg_add_string(&buf, "config", conf);
1369 ubus_send_reply(ctx, req, buf.head);
1374 rpc_luci2_opkg_config_set(struct ubus_context *ctx, struct ubus_object *obj,
1375 struct ubus_request_data *req, const char *method,
1376 struct blob_attr *msg)
1379 struct blob_attr *tb[__RPC_OC_MAX];
1381 blobmsg_parse(rpc_opkg_package_policy, __RPC_OC_MAX, tb,
1382 blob_data(msg), blob_len(msg));
1384 if (!tb[RPC_OC_CONFIG])
1385 return UBUS_STATUS_INVALID_ARGUMENT;
1387 if (blobmsg_type(tb[RPC_OC_CONFIG]) != BLOBMSG_TYPE_STRING)
1388 return UBUS_STATUS_INVALID_ARGUMENT;
1390 if (blobmsg_data_len(tb[RPC_OC_CONFIG]) >= 2048)
1391 return UBUS_STATUS_NOT_SUPPORTED;
1393 if (!(f = fopen("/etc/opkg.conf", "w")))
1394 return rpc_errno_status();
1396 fwrite(blobmsg_data(tb[RPC_OC_CONFIG]),
1397 blobmsg_data_len(tb[RPC_OC_CONFIG]), 1, f);
1404 int rpc_luci2_api_init(struct ubus_context *ctx)
1408 static const struct ubus_method luci2_system_methods[] = {
1409 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log),
1410 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg),
1411 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list),
1412 UBUS_METHOD("process_signal", rpc_luci2_process_signal,
1414 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list),
1415 UBUS_METHOD("init_action", rpc_luci2_init_action,
1417 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get),
1418 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set,
1420 UBUS_METHOD("password_set", rpc_luci2_password_set,
1421 rpc_password_policy)
1424 static struct ubus_object_type luci2_system_type =
1425 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods);
1427 static struct ubus_object system_obj = {
1428 .name = "luci2.system",
1429 .type = &luci2_system_type,
1430 .methods = luci2_system_methods,
1431 .n_methods = ARRAY_SIZE(luci2_system_methods),
1435 static const struct ubus_method luci2_network_methods[] = {
1436 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count),
1437 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table),
1438 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table),
1439 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases),
1440 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6),
1441 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes),
1442 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6),
1445 static struct ubus_object_type luci2_network_type =
1446 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods);
1448 static struct ubus_object network_obj = {
1449 .name = "luci2.network",
1450 .type = &luci2_network_type,
1451 .methods = luci2_network_methods,
1452 .n_methods = ARRAY_SIZE(luci2_network_methods),
1456 static const struct ubus_method luci2_opkg_methods[] = {
1457 UBUS_METHOD("list", rpc_luci2_opkg_list,
1458 rpc_opkg_match_policy),
1459 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed,
1460 rpc_opkg_match_policy),
1461 UBUS_METHOD("find", rpc_luci2_opkg_find,
1462 rpc_opkg_match_policy),
1463 UBUS_METHOD("install", rpc_luci2_opkg_install,
1464 rpc_opkg_package_policy),
1465 UBUS_METHOD("remove", rpc_luci2_opkg_remove,
1466 rpc_opkg_package_policy),
1467 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update),
1468 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get),
1469 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set,
1470 rpc_opkg_config_policy)
1473 static struct ubus_object_type luci2_opkg_type =
1474 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods);
1476 static struct ubus_object opkg_obj = {
1477 .name = "luci2.opkg",
1478 .type = &luci2_opkg_type,
1479 .methods = luci2_opkg_methods,
1480 .n_methods = ARRAY_SIZE(luci2_opkg_methods),
1483 cursor = uci_alloc_context();
1486 return UBUS_STATUS_UNKNOWN_ERROR;
1488 rv |= ubus_add_object(ctx, &system_obj);
1489 rv |= ubus_add_object(ctx, &network_obj);
1490 rv |= ubus_add_object(ctx, &opkg_obj);