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>
28 #include <sys/statvfs.h>
30 #include <arpa/inet.h>
36 static struct blob_buf buf;
37 static struct uci_context *cursor;
45 static const struct blobmsg_policy rpc_signal_policy[__RPC_S_MAX] = {
46 [RPC_S_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
47 [RPC_S_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
56 static const struct blobmsg_policy rpc_init_policy[__RPC_I_MAX] = {
57 [RPC_I_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
58 [RPC_I_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
66 static const struct blobmsg_policy rpc_sshkey_policy[__RPC_K_MAX] = {
67 [RPC_K_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
76 static const struct blobmsg_policy rpc_password_policy[__RPC_P_MAX] = {
77 [RPC_P_USER] = { .name = "user", .type = BLOBMSG_TYPE_STRING },
78 [RPC_P_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
88 static const struct blobmsg_policy rpc_opkg_match_policy[__RPC_OM_MAX] = {
89 [RPC_OM_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
90 [RPC_OM_OFFSET] = { .name = "offset", .type = BLOBMSG_TYPE_INT32 },
91 [RPC_OM_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
99 static const struct blobmsg_policy rpc_opkg_package_policy[__RPC_OP_MAX] = {
100 [RPC_OP_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
108 static const struct blobmsg_policy rpc_opkg_config_policy[__RPC_OC_MAX] = {
109 [RPC_OC_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
114 rpc_errno_status(void)
119 return UBUS_STATUS_PERMISSION_DENIED;
122 return UBUS_STATUS_INVALID_ARGUMENT;
125 return UBUS_STATUS_NOT_FOUND;
128 return UBUS_STATUS_INVALID_ARGUMENT;
131 return UBUS_STATUS_UNKNOWN_ERROR;
136 log_read(FILE *log, int logsize)
142 logsize = RPC_LUCI2_DEF_LOGSIZE;
144 len = (logsize > RPC_LUCI2_MAX_LOGSIZE) ? RPC_LUCI2_MAX_LOGSIZE : logsize;
145 logbuf = blobmsg_alloc_string_buffer(&buf, "log", len + 1);
150 while (logsize > RPC_LUCI2_MAX_LOGSIZE)
152 len = logsize % RPC_LUCI2_MAX_LOGSIZE;
155 len = RPC_LUCI2_MAX_LOGSIZE;
157 fread(logbuf, 1, len, log);
161 len = fread(logbuf, 1, logsize, log);
164 blobmsg_add_string_buffer(&buf);
168 rpc_luci2_system_log(struct ubus_context *ctx, struct ubus_object *obj,
169 struct ubus_request_data *req, const char *method,
170 struct blob_attr *msg)
174 const char *logfile = NULL;
176 struct uci_package *p;
177 struct uci_element *e;
178 struct uci_section *s;
179 struct uci_ptr ptr = { .package = "system" };
181 uci_load(cursor, ptr.package, &p);
184 return UBUS_STATUS_NOT_FOUND;
186 uci_foreach_element(&p->sections, e)
188 s = uci_to_section(e);
190 if (strcmp(s->type, "system"))
194 ptr.option = "log_type";
195 ptr.section = e->name;
196 uci_lookup_ptr(cursor, &ptr, NULL, true);
200 if (ptr.o && ptr.o->type == UCI_TYPE_STRING &&
201 !strcmp(ptr.o->v.string, "file"))
204 ptr.option = "log_file";
205 uci_lookup_ptr(cursor, &ptr, NULL, true);
207 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
208 logfile = ptr.o->v.string;
210 logfile = "/var/log/messages";
212 if (stat(logfile, &st) || !(log = fopen(logfile, "r")))
215 logsize = st.st_size;
220 ptr.option = "log_size";
221 uci_lookup_ptr(cursor, &ptr, NULL, true);
223 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
224 logsize = atoi(ptr.o->v.string) * 1024;
226 if (!(log = popen("logread", "r")))
230 blob_buf_init(&buf, 0);
232 log_read(log, logsize);
235 uci_unload(cursor, p);
236 ubus_send_reply(ctx, req, buf.head);
240 uci_unload(cursor, p);
241 return rpc_errno_status();
245 rpc_luci2_system_dmesg(struct ubus_context *ctx, struct ubus_object *obj,
246 struct ubus_request_data *req, const char *method,
247 struct blob_attr *msg)
251 if (!(log = popen("dmesg", "r")))
252 return rpc_errno_status();
254 blob_buf_init(&buf, 0);
256 log_read(log, RPC_LUCI2_MAX_LOGSIZE);
259 ubus_send_reply(ctx, req, buf.head);
264 rpc_luci2_system_diskfree(struct ubus_context *ctx, struct ubus_object *obj,
265 struct ubus_request_data *req, const char *method,
266 struct blob_attr *msg)
271 const char *fslist[] = {
276 blob_buf_init(&buf, 0);
278 for (i = 0; i < sizeof(fslist) / sizeof(fslist[0]); i += 2)
280 if (statvfs(fslist[i], &s))
283 c = blobmsg_open_table(&buf, fslist[i+1]);
285 blobmsg_add_u32(&buf, "total", s.f_blocks * s.f_frsize);
286 blobmsg_add_u32(&buf, "free", s.f_bfree * s.f_frsize);
287 blobmsg_add_u32(&buf, "used", (s.f_blocks - s.f_bfree) * s.f_frsize);
289 blobmsg_close_table(&buf, c);
292 ubus_send_reply(ctx, req, buf.head);
297 rpc_luci2_process_list(struct ubus_context *ctx, struct ubus_object *obj,
298 struct ubus_request_data *req, const char *method,
299 struct blob_attr *msg)
304 char *pid, *ppid, *user, *stat, *vsz, *pvsz, *pcpu, *cmd;
306 if (!(top = popen("/bin/busybox top -bn1", "r")))
307 return rpc_errno_status();
309 blob_buf_init(&buf, 0);
310 c = blobmsg_open_array(&buf, "processes");
312 while (fgets(line, sizeof(line) - 1, top))
314 pid = strtok(line, " ");
316 if (*pid < '0' || *pid > '9')
319 ppid = strtok(NULL, " ");
320 user = strtok(NULL, " ");
321 stat = strtok(NULL, " ");
334 vsz = strtok(stat + 4, " ");
335 pvsz = strtok(NULL, " ");
336 pcpu = strtok(NULL, " ");
337 cmd = strtok(NULL, "\n");
342 d = blobmsg_open_table(&buf, NULL);
344 blobmsg_add_u32(&buf, "pid", atoi(pid));
345 blobmsg_add_u32(&buf, "ppid", atoi(ppid));
346 blobmsg_add_string(&buf, "user", user);
347 blobmsg_add_string(&buf, "stat", stat);
348 blobmsg_add_u32(&buf, "vsize", atoi(vsz) * 1024);
349 blobmsg_add_u32(&buf, "vsize_percent", atoi(pvsz));
350 blobmsg_add_u32(&buf, "cpu_percent", atoi(pcpu));
351 blobmsg_add_string(&buf, "command", cmd);
353 blobmsg_close_table(&buf, d);
357 blobmsg_close_array(&buf, c);
359 ubus_send_reply(ctx, req, buf.head);
364 rpc_luci2_process_signal(struct ubus_context *ctx, struct ubus_object *obj,
365 struct ubus_request_data *req, const char *method,
366 struct blob_attr *msg)
369 struct blob_attr *tb[__RPC_S_MAX];
371 blobmsg_parse(rpc_signal_policy, __RPC_S_MAX, tb,
372 blob_data(msg), blob_len(msg));
374 if (!tb[RPC_S_SIGNAL] || !tb[RPC_S_PID])
377 return rpc_errno_status();
380 pid = blobmsg_get_u32(tb[RPC_S_PID]);
381 sig = blobmsg_get_u32(tb[RPC_S_SIGNAL]);
384 return rpc_errno_status();
390 rpc_luci2_init_list(struct ubus_context *ctx, struct ubus_object *obj,
391 struct ubus_request_data *req, const char *method,
392 struct blob_attr *msg)
396 char *p, path[PATH_MAX];
402 if (!(d = opendir("/etc/init.d")))
403 return rpc_errno_status();
405 blob_buf_init(&buf, 0);
406 c = blobmsg_open_array(&buf, "initscripts");
408 while ((e = readdir(d)) != NULL)
410 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s", e->d_name);
412 if (stat(path, &s) || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR))
415 if ((f = fopen(path, "r")) != NULL)
418 p = fgets(path, sizeof(path) - 1, f);
420 if (!p || !strstr(p, "/etc/rc.common"))
423 t = blobmsg_open_table(&buf, NULL);
425 blobmsg_add_string(&buf, "name", e->d_name);
427 while (fgets(path, sizeof(path) - 1, f))
429 p = strtok(path, "= \t");
431 if (!strcmp(p, "START") && !!(p = strtok(NULL, "= \t\n")))
434 blobmsg_add_u32(&buf, "start", n);
436 else if (!strcmp(p, "STOP") && !!(p = strtok(NULL, "= \t\n")))
438 blobmsg_add_u32(&buf, "stop", atoi(p));
445 snprintf(path, sizeof(path) - 1, "/etc/rc.d/S%02d%s",
448 blobmsg_add_u8(&buf, "enabled",
449 (!stat(path, &s) && (s.st_mode & S_IXUSR)));
453 blobmsg_add_u8(&buf, "enabled", 0);
456 blobmsg_close_table(&buf, t);
464 blobmsg_close_array(&buf, c);
466 ubus_send_reply(ctx, req, buf.head);
471 rpc_luci2_init_action(struct ubus_context *ctx, struct ubus_object *obj,
472 struct ubus_request_data *req, const char *method,
473 struct blob_attr *msg)
480 struct blob_attr *tb[__RPC_I_MAX];
482 blobmsg_parse(rpc_init_policy, __RPC_I_MAX, tb,
483 blob_data(msg), blob_len(msg));
485 if (!tb[RPC_I_NAME] || !tb[RPC_I_ACTION])
486 return UBUS_STATUS_INVALID_ARGUMENT;
488 action = blobmsg_data(tb[RPC_I_ACTION]);
490 if (strcmp(action, "start") && strcmp(action, "stop") &&
491 strcmp(action, "reload") && strcmp(action, "restart") &&
492 strcmp(action, "enable") && strcmp(action, "disable"))
493 return UBUS_STATUS_INVALID_ARGUMENT;
495 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s",
496 (char *)blobmsg_data(tb[RPC_I_NAME]));
499 return rpc_errno_status();
501 if (!(s.st_mode & S_IXUSR))
502 return UBUS_STATUS_PERMISSION_DENIED;
504 switch ((pid = fork()))
507 return rpc_errno_status();
512 if ((fd = open("/dev/null", O_RDWR)) > -1)
523 if (execl(path, path, action, NULL))
524 return rpc_errno_status();
532 rpc_luci2_sshkeys_get(struct ubus_context *ctx, struct ubus_object *obj,
533 struct ubus_request_data *req, const char *method,
534 struct blob_attr *msg)
540 if (!(f = fopen("/etc/dropbear/authorized_keys", "r")))
541 return rpc_errno_status();
543 blob_buf_init(&buf, 0);
544 c = blobmsg_open_array(&buf, "keys");
546 while (fgets(line, sizeof(line) - 1, f))
548 for (p = line + strlen(line) - 1; (p > line) && isspace(*p); p--)
551 for (p = line; isspace(*p); p++)
555 blobmsg_add_string(&buf, NULL, p);
558 blobmsg_close_array(&buf, c);
561 ubus_send_reply(ctx, req, buf.head);
566 rpc_luci2_sshkeys_set(struct ubus_context *ctx, struct ubus_object *obj,
567 struct ubus_request_data *req, const char *method,
568 struct blob_attr *msg)
572 struct blob_attr *cur, *tb[__RPC_K_MAX];
574 blobmsg_parse(rpc_sshkey_policy, __RPC_K_MAX, tb,
575 blob_data(msg), blob_len(msg));
578 return UBUS_STATUS_INVALID_ARGUMENT;
580 if (!(f = fopen("/etc/dropbear/authorized_keys", "w")))
581 return rpc_errno_status();
583 blobmsg_for_each_attr(cur, tb[RPC_K_KEYS], rem)
585 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
588 fwrite(blobmsg_data(cur), blobmsg_data_len(cur) - 1, 1, f);
589 fwrite("\n", 1, 1, f);
597 rpc_luci2_password_set(struct ubus_context *ctx, struct ubus_object *obj,
598 struct ubus_request_data *req, const char *method,
599 struct blob_attr *msg)
604 struct blob_attr *tb[__RPC_P_MAX];
606 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
607 blob_data(msg), blob_len(msg));
609 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
610 return UBUS_STATUS_INVALID_ARGUMENT;
612 if (stat("/usr/bin/passwd", &s))
613 return UBUS_STATUS_NOT_FOUND;
615 if (!(s.st_mode & S_IXUSR))
616 return UBUS_STATUS_PERMISSION_DENIED;
619 return rpc_errno_status();
621 switch ((pid = fork()))
626 return rpc_errno_status();
635 if ((fd = open("/dev/null", O_RDWR)) > -1)
644 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
645 blobmsg_data(tb[RPC_P_USER]), NULL))
646 return rpc_errno_status();
651 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
652 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
653 write(fds[1], "\n", 1);
657 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
658 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
659 write(fds[1], "\n", 1);
663 waitpid(pid, NULL, 0);
671 dnsmasq_leasefile(void)
674 struct uci_package *p;
675 struct uci_element *e;
676 struct uci_section *s;
677 struct uci_ptr ptr = {
680 .option = "leasefile"
683 uci_load(cursor, ptr.package, &p);
688 uci_foreach_element(&p->sections, e)
690 s = uci_to_section(e);
692 if (strcmp(s->type, "dnsmasq"))
695 ptr.section = e->name;
696 uci_lookup_ptr(cursor, &ptr, NULL, true);
700 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
701 leases = fopen(ptr.o->v.string, "r");
703 uci_unload(cursor, p);
709 rpc_luci2_network_leases(struct ubus_context *ctx, struct ubus_object *obj,
710 struct ubus_request_data *req, const char *method,
711 struct blob_attr *msg)
716 char *ts, *mac, *addr, *name;
717 time_t now = time(NULL);
719 blob_buf_init(&buf, 0);
720 c = blobmsg_open_array(&buf, "leases");
722 leases = dnsmasq_leasefile();
727 while (fgets(line, sizeof(line) - 1, leases))
729 ts = strtok(line, " \t");
730 mac = strtok(NULL, " \t");
731 addr = strtok(NULL, " \t");
732 name = strtok(NULL, " \t");
734 if (!ts || !mac || !addr || !name)
737 if (strchr(addr, ':'))
740 d = blobmsg_open_table(&buf, NULL);
742 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
743 blobmsg_add_string(&buf, "macaddr", mac);
744 blobmsg_add_string(&buf, "ipaddr", addr);
746 if (strcmp(name, "*"))
747 blobmsg_add_string(&buf, "hostname", name);
749 blobmsg_close_table(&buf, d);
755 blobmsg_close_array(&buf, c);
756 ubus_send_reply(ctx, req, buf.head);
762 rpc_luci2_network_leases6(struct ubus_context *ctx, struct ubus_object *obj,
763 struct ubus_request_data *req, const char *method,
764 struct blob_attr *msg)
769 char *ts, *mac, *addr, *name, *duid;
770 time_t now = time(NULL);
772 blob_buf_init(&buf, 0);
773 c = blobmsg_open_array(&buf, "leases");
775 leases = fopen("/tmp/hosts/6relayd", "r");
779 while (fgets(line, sizeof(line) - 1, leases))
781 if (strncmp(line, "# ", 2))
784 strtok(line + 2, " \t"); /* iface */
786 duid = strtok(NULL, " \t");
788 strtok(NULL, " \t"); /* iaid */
790 name = strtok(NULL, " \t");
791 ts = strtok(NULL, " \t");
793 strtok(NULL, " \t"); /* id */
794 strtok(NULL, " \t"); /* length */
796 addr = strtok(NULL, " \t\n");
801 d = blobmsg_open_table(&buf, NULL);
803 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
804 blobmsg_add_string(&buf, "duid", duid);
805 blobmsg_add_string(&buf, "ip6addr", addr);
807 if (strcmp(name, "-"))
808 blobmsg_add_string(&buf, "hostname", name);
810 blobmsg_close_array(&buf, d);
817 leases = dnsmasq_leasefile();
822 while (fgets(line, sizeof(line) - 1, leases))
824 ts = strtok(line, " \t");
825 mac = strtok(NULL, " \t");
826 addr = strtok(NULL, " \t");
827 name = strtok(NULL, " \t");
828 duid = strtok(NULL, " \t\n");
830 if (!ts || !mac || !addr || !duid)
833 if (!strchr(addr, ':'))
836 d = blobmsg_open_table(&buf, NULL);
838 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
839 blobmsg_add_string(&buf, "macaddr", mac);
840 blobmsg_add_string(&buf, "ip6addr", addr);
842 if (strcmp(name, "*"))
843 blobmsg_add_string(&buf, "hostname", name);
845 if (strcmp(duid, "*"))
846 blobmsg_add_string(&buf, "duid", name);
848 blobmsg_close_table(&buf, d);
855 blobmsg_close_array(&buf, c);
856 ubus_send_reply(ctx, req, buf.head);
862 rpc_luci2_network_ct_count(struct ubus_context *ctx, struct ubus_object *obj,
863 struct ubus_request_data *req, const char *method,
864 struct blob_attr *msg)
869 blob_buf_init(&buf, 0);
871 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL)
873 if (fgets(line, sizeof(line) - 1, f))
874 blobmsg_add_u32(&buf, "count", atoi(line));
879 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL)
881 if (fgets(line, sizeof(line) - 1, f))
882 blobmsg_add_u32(&buf, "limit", atoi(line));
887 ubus_send_reply(ctx, req, buf.head);
893 rpc_luci2_network_ct_table(struct ubus_context *ctx, struct ubus_object *obj,
894 struct ubus_request_data *req, const char *method,
895 struct blob_attr *msg)
903 blob_buf_init(&buf, 0);
904 c = blobmsg_open_array(&buf, "entries");
906 if ((f = fopen("/proc/net/nf_conntrack", "r")) != NULL)
908 while (fgets(line, sizeof(line) - 1, f))
910 d = blobmsg_open_table(&buf, NULL);
911 memset(seen, 0, sizeof(seen));
913 for (i = 0, p = strtok(line, " "); p; i++, p = strtok(NULL, " "))
916 blobmsg_add_u8(&buf, "ipv6", !strcmp(p, "ipv6"));
918 blobmsg_add_u32(&buf, "protocol", atoi(p));
920 blobmsg_add_u32(&buf, "expires", atoi(p));
926 if (!seen[0] && !strncmp(p, "src=", 4))
928 blobmsg_add_string(&buf, "src", p + 4);
931 else if (!seen[1] && !strncmp(p, "dst=", 4))
933 blobmsg_add_string(&buf, "dest", p + 4);
936 else if (!seen[2] && !strncmp(p, "sport=", 6))
938 blobmsg_add_u32(&buf, "sport", atoi(p + 6));
941 else if (!seen[3] && !strncmp(p, "dport=", 6))
943 blobmsg_add_u32(&buf, "dport", atoi(p + 6));
946 else if (!strncmp(p, "packets=", 8))
948 blobmsg_add_u32(&buf,
949 seen[4] ? "tx_packets" : "rx_packets",
953 else if (!strncmp(p, "bytes=", 6))
955 blobmsg_add_u32(&buf,
956 seen[5] ? "tx_bytes" : "rx_bytes",
963 blobmsg_close_table(&buf, d);
969 blobmsg_close_array(&buf, c);
970 ubus_send_reply(ctx, req, buf.head);
976 rpc_luci2_network_arp_table(struct ubus_context *ctx, struct ubus_object *obj,
977 struct ubus_request_data *req, const char *method,
978 struct blob_attr *msg)
982 char *addr, *mac, *dev, line[128];
984 blob_buf_init(&buf, 0);
985 c = blobmsg_open_array(&buf, "entries");
987 if ((f = fopen("/proc/net/arp", "r")) != NULL)
989 /* skip header line */
990 fgets(line, sizeof(line) - 1, f);
992 while (fgets(line, sizeof(line) - 1, f))
994 addr = strtok(line, " \t");
996 strtok(NULL, " \t"); /* HW type */
997 strtok(NULL, " \t"); /* Flags */
999 mac = strtok(NULL, " \t");
1001 strtok(NULL, " \t"); /* Mask */
1003 dev = strtok(NULL, " \t\n");
1008 d = blobmsg_open_table(&buf, NULL);
1009 blobmsg_add_string(&buf, "ipaddr", addr);
1010 blobmsg_add_string(&buf, "macaddr", mac);
1011 blobmsg_add_string(&buf, "device", dev);
1012 blobmsg_close_table(&buf, d);
1018 blobmsg_close_array(&buf, c);
1019 ubus_send_reply(ctx, req, buf.head);
1025 put_hexaddr(const char *name, const char *s, const char *m)
1029 char as[sizeof("255.255.255.255/32\0")];
1031 a.s_addr = strtoul(s, NULL, 16);
1032 inet_ntop(AF_INET, &a, as, sizeof(as));
1036 for (a.s_addr = ntohl(strtoul(m, NULL, 16)), bits = 0;
1037 a.s_addr & 0x80000000;
1041 sprintf(as + strlen(as), "/%u", bits);
1044 blobmsg_add_string(&buf, name, as);
1048 rpc_luci2_network_routes(struct ubus_context *ctx, struct ubus_object *obj,
1049 struct ubus_request_data *req, const char *method,
1050 struct blob_attr *msg)
1054 char *dst, *dmask, *next, *metric, *device;
1058 if (!(routes = fopen("/proc/net/route", "r")))
1059 return rpc_errno_status();
1061 blob_buf_init(&buf, 0);
1062 c = blobmsg_open_array(&buf, "routes");
1064 /* skip header line */
1065 fgets(line, sizeof(line) - 1, routes);
1067 while (fgets(line, sizeof(line) - 1, routes))
1069 device = strtok(line, "\t ");
1070 dst = strtok(NULL, "\t ");
1071 next = strtok(NULL, "\t ");
1073 strtok(NULL, "\t "); /* flags */
1074 strtok(NULL, "\t "); /* refcount */
1075 strtok(NULL, "\t "); /* usecount */
1077 metric = strtok(NULL, "\t ");
1078 dmask = strtok(NULL, "\t ");
1083 d = blobmsg_open_table(&buf, NULL);
1085 put_hexaddr("target", dst, dmask);
1086 put_hexaddr("nexthop", next, NULL);
1088 n = strtoul(metric, NULL, 10);
1089 blobmsg_add_u32(&buf, "metric", n);
1091 blobmsg_add_string(&buf, "device", device);
1093 blobmsg_close_table(&buf, d);
1096 blobmsg_close_array(&buf, c);
1099 ubus_send_reply(ctx, req, buf.head);
1104 put_hex6addr(const char *name, const char *s, const char *m)
1108 char as[INET6_ADDRSTRLEN + sizeof("/128")];
1111 (((x) <= '9') ? ((x) - '0') : \
1112 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1115 for (i = 0; i < 16; i++, s += 2)
1116 a.s6_addr[i] = (16 * hex(*s)) + hex(*(s+1));
1118 inet_ntop(AF_INET6, &a, as, sizeof(as));
1121 sprintf(as + strlen(as), "/%lu", strtoul(m, NULL, 16));
1123 blobmsg_add_string(&buf, name, as);
1127 rpc_luci2_network_routes6(struct ubus_context *ctx, struct ubus_object *obj,
1128 struct ubus_request_data *req, const char *method,
1129 struct blob_attr *msg)
1133 char *src, *smask, *dst, *dmask, *next, *metric, *flags, *device;
1137 if (!(routes = fopen("/proc/net/ipv6_route", "r")))
1138 return rpc_errno_status();
1140 blob_buf_init(&buf, 0);
1141 c = blobmsg_open_array(&buf, "routes");
1143 while (fgets(line, sizeof(line) - 1, routes))
1145 dst = strtok(line, " ");
1146 dmask = strtok(NULL, " ");
1147 src = strtok(NULL, " ");
1148 smask = strtok(NULL, " ");
1149 next = strtok(NULL, " ");
1150 metric = strtok(NULL, " ");
1152 strtok(NULL, " "); /* refcount */
1153 strtok(NULL, " "); /* usecount */
1155 flags = strtok(NULL, " ");
1156 device = strtok(NULL, " \n");
1161 n = strtoul(flags, NULL, 16);
1166 d = blobmsg_open_table(&buf, NULL);
1168 put_hex6addr("target", dst, dmask);
1169 put_hex6addr("source", src, smask);
1170 put_hex6addr("nexthop", next, NULL);
1172 n = strtoul(metric, NULL, 16);
1173 blobmsg_add_u32(&buf, "metric", n);
1175 blobmsg_add_string(&buf, "device", device);
1177 blobmsg_close_table(&buf, d);
1180 blobmsg_close_array(&buf, c);
1183 ubus_send_reply(ctx, req, buf.head);
1199 opkg_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1201 struct opkg_state *s = priv;
1204 char *nl = strchr(buf, '\n');
1205 char *name = NULL, *vers = NULL, *desc = NULL;
1213 if (s->cur_offset++ < s->req_offset)
1216 if (s->cur_count++ >= s->req_count)
1222 s->array = blobmsg_open_array(blob, "packages");
1225 for (ptr = buf, last = buf, *nl = 0; ptr <= nl; ptr++)
1227 if (!*ptr || (*ptr == ' ' && *(ptr+1) == '-' && *(ptr+2) == ' '))
1239 desc = *ptr ? (ptr + 3) : NULL;
1248 c = blobmsg_open_array(blob, NULL);
1250 blobmsg_add_string(blob, NULL, name);
1251 blobmsg_add_string(blob, NULL, vers);
1254 blobmsg_add_string(blob, NULL, desc);
1256 blobmsg_close_array(blob, c);
1260 return (nl - buf + 1);
1264 opkg_finish_list(struct blob_buf *blob, int status, void *priv)
1266 struct opkg_state *s = priv;
1271 blobmsg_close_array(blob, s->array);
1272 blobmsg_add_u32(blob, "total", s->total);
1276 opkg_exec_list(const char *action, struct blob_attr *msg,
1277 struct ubus_context *ctx, struct ubus_request_data *req)
1279 struct opkg_state *state = NULL;
1280 struct blob_attr *tb[__RPC_OM_MAX];
1281 const char *cmd[5] = { "opkg", action, "-nocase", NULL, NULL };
1283 blobmsg_parse(rpc_opkg_match_policy, __RPC_OM_MAX, tb,
1284 blob_data(msg), blob_len(msg));
1286 state = malloc(sizeof(*state));
1289 return UBUS_STATUS_UNKNOWN_ERROR;
1291 memset(state, 0, sizeof(*state));
1293 if (tb[RPC_OM_PATTERN])
1294 cmd[3] = blobmsg_data(tb[RPC_OM_PATTERN]);
1296 if (tb[RPC_OM_LIMIT])
1297 state->req_count = blobmsg_get_u32(tb[RPC_OM_LIMIT]);
1299 if (tb[RPC_OM_OFFSET])
1300 state->req_offset = blobmsg_get_u32(tb[RPC_OM_OFFSET]);
1302 if (state->req_offset < 0)
1303 state->req_offset = 0;
1305 if (state->req_count <= 0 || state->req_count > 100)
1306 state->req_count = 100;
1308 return rpc_exec(cmd, opkg_parse_list, NULL, opkg_finish_list,
1314 rpc_luci2_opkg_list(struct ubus_context *ctx, struct ubus_object *obj,
1315 struct ubus_request_data *req, const char *method,
1316 struct blob_attr *msg)
1318 return opkg_exec_list("list", msg, ctx, req);
1322 rpc_luci2_opkg_list_installed(struct ubus_context *ctx, struct ubus_object *obj,
1323 struct ubus_request_data *req, const char *method,
1324 struct blob_attr *msg)
1326 return opkg_exec_list("list-installed", msg, ctx, req);
1330 rpc_luci2_opkg_find(struct ubus_context *ctx, struct ubus_object *obj,
1331 struct ubus_request_data *req, const char *method,
1332 struct blob_attr *msg)
1334 return opkg_exec_list("find", msg, ctx, req);
1338 rpc_luci2_opkg_update(struct ubus_context *ctx, struct ubus_object *obj,
1339 struct ubus_request_data *req, const char *method,
1340 struct blob_attr *msg)
1342 const char *cmd[3] = { "opkg", "update", NULL };
1343 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1347 rpc_luci2_opkg_install(struct ubus_context *ctx, struct ubus_object *obj,
1348 struct ubus_request_data *req, const char *method,
1349 struct blob_attr *msg)
1351 struct blob_attr *tb[__RPC_OP_MAX];
1352 const char *cmd[5] = { "opkg", "--force-overwrite",
1353 "install", NULL, NULL };
1355 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1356 blob_data(msg), blob_len(msg));
1358 if (!tb[RPC_OP_PACKAGE])
1359 return UBUS_STATUS_INVALID_ARGUMENT;
1361 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1363 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1367 rpc_luci2_opkg_remove(struct ubus_context *ctx, struct ubus_object *obj,
1368 struct ubus_request_data *req, const char *method,
1369 struct blob_attr *msg)
1371 struct blob_attr *tb[__RPC_OP_MAX];
1372 const char *cmd[5] = { "opkg", "--force-removal-of-dependent-packages",
1373 "remove", NULL, NULL };
1375 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1376 blob_data(msg), blob_len(msg));
1378 if (!tb[RPC_OP_PACKAGE])
1379 return UBUS_STATUS_INVALID_ARGUMENT;
1381 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1383 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1387 rpc_luci2_opkg_config_get(struct ubus_context *ctx, struct ubus_object *obj,
1388 struct ubus_request_data *req, const char *method,
1389 struct blob_attr *msg)
1392 char conf[2048] = { 0 };
1394 if (!(f = fopen("/etc/opkg.conf", "r")))
1395 return rpc_errno_status();
1397 fread(conf, sizeof(conf) - 1, 1, f);
1400 blob_buf_init(&buf, 0);
1401 blobmsg_add_string(&buf, "config", conf);
1403 ubus_send_reply(ctx, req, buf.head);
1408 rpc_luci2_opkg_config_set(struct ubus_context *ctx, struct ubus_object *obj,
1409 struct ubus_request_data *req, const char *method,
1410 struct blob_attr *msg)
1413 struct blob_attr *tb[__RPC_OC_MAX];
1415 blobmsg_parse(rpc_opkg_package_policy, __RPC_OC_MAX, tb,
1416 blob_data(msg), blob_len(msg));
1418 if (!tb[RPC_OC_CONFIG])
1419 return UBUS_STATUS_INVALID_ARGUMENT;
1421 if (blobmsg_type(tb[RPC_OC_CONFIG]) != BLOBMSG_TYPE_STRING)
1422 return UBUS_STATUS_INVALID_ARGUMENT;
1424 if (blobmsg_data_len(tb[RPC_OC_CONFIG]) >= 2048)
1425 return UBUS_STATUS_NOT_SUPPORTED;
1427 if (!(f = fopen("/etc/opkg.conf", "w")))
1428 return rpc_errno_status();
1430 fwrite(blobmsg_data(tb[RPC_OC_CONFIG]),
1431 blobmsg_data_len(tb[RPC_OC_CONFIG]), 1, f);
1438 int rpc_luci2_api_init(struct ubus_context *ctx)
1442 static const struct ubus_method luci2_system_methods[] = {
1443 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log),
1444 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg),
1445 UBUS_METHOD_NOARG("diskfree", rpc_luci2_system_diskfree),
1446 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list),
1447 UBUS_METHOD("process_signal", rpc_luci2_process_signal,
1449 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list),
1450 UBUS_METHOD("init_action", rpc_luci2_init_action,
1452 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get),
1453 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set,
1455 UBUS_METHOD("password_set", rpc_luci2_password_set,
1456 rpc_password_policy)
1459 static struct ubus_object_type luci2_system_type =
1460 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods);
1462 static struct ubus_object system_obj = {
1463 .name = "luci2.system",
1464 .type = &luci2_system_type,
1465 .methods = luci2_system_methods,
1466 .n_methods = ARRAY_SIZE(luci2_system_methods),
1470 static const struct ubus_method luci2_network_methods[] = {
1471 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count),
1472 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table),
1473 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table),
1474 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases),
1475 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6),
1476 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes),
1477 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6),
1480 static struct ubus_object_type luci2_network_type =
1481 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods);
1483 static struct ubus_object network_obj = {
1484 .name = "luci2.network",
1485 .type = &luci2_network_type,
1486 .methods = luci2_network_methods,
1487 .n_methods = ARRAY_SIZE(luci2_network_methods),
1491 static const struct ubus_method luci2_opkg_methods[] = {
1492 UBUS_METHOD("list", rpc_luci2_opkg_list,
1493 rpc_opkg_match_policy),
1494 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed,
1495 rpc_opkg_match_policy),
1496 UBUS_METHOD("find", rpc_luci2_opkg_find,
1497 rpc_opkg_match_policy),
1498 UBUS_METHOD("install", rpc_luci2_opkg_install,
1499 rpc_opkg_package_policy),
1500 UBUS_METHOD("remove", rpc_luci2_opkg_remove,
1501 rpc_opkg_package_policy),
1502 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update),
1503 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get),
1504 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set,
1505 rpc_opkg_config_policy)
1508 static struct ubus_object_type luci2_opkg_type =
1509 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods);
1511 static struct ubus_object opkg_obj = {
1512 .name = "luci2.opkg",
1513 .type = &luci2_opkg_type,
1514 .methods = luci2_opkg_methods,
1515 .n_methods = ARRAY_SIZE(luci2_opkg_methods),
1518 cursor = uci_alloc_context();
1521 return UBUS_STATUS_UNKNOWN_ERROR;
1523 rv |= ubus_add_object(ctx, &system_obj);
1524 rv |= ubus_add_object(ctx, &network_obj);
1525 rv |= ubus_add_object(ctx, &opkg_obj);