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_data_policy[__RPC_D_MAX] = {
67 [RPC_D_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
75 static const struct blobmsg_policy rpc_sshkey_policy[__RPC_K_MAX] = {
76 [RPC_K_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
85 static const struct blobmsg_policy rpc_password_policy[__RPC_P_MAX] = {
86 [RPC_P_USER] = { .name = "user", .type = BLOBMSG_TYPE_STRING },
87 [RPC_P_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
97 static const struct blobmsg_policy rpc_opkg_match_policy[__RPC_OM_MAX] = {
98 [RPC_OM_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
99 [RPC_OM_OFFSET] = { .name = "offset", .type = BLOBMSG_TYPE_INT32 },
100 [RPC_OM_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
108 static const struct blobmsg_policy rpc_opkg_package_policy[__RPC_OP_MAX] = {
109 [RPC_OP_PACKAGE] = { .name = "package", .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_rclocal_get(struct ubus_context *ctx, struct ubus_object *obj,
533 struct ubus_request_data *req, const char *method,
534 struct blob_attr *msg)
537 char data[4096] = { 0 };
539 if (!(f = fopen("/etc/rc.local", "r")))
540 return rpc_errno_status();
542 fread(data, sizeof(data) - 1, 1, f);
545 blob_buf_init(&buf, 0);
546 blobmsg_add_string(&buf, "data", data);
548 ubus_send_reply(ctx, req, buf.head);
553 rpc_luci2_rclocal_set(struct ubus_context *ctx, struct ubus_object *obj,
554 struct ubus_request_data *req, const char *method,
555 struct blob_attr *msg)
558 struct blob_attr *tb[__RPC_D_MAX];
560 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
561 blob_data(msg), blob_len(msg));
563 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 4096)
564 return UBUS_STATUS_INVALID_ARGUMENT;
566 if (!(f = fopen("/etc/rc.local", "w")))
567 return rpc_errno_status();
569 fwrite(blobmsg_data(tb[RPC_D_DATA]),
570 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
577 rpc_luci2_crontab_get(struct ubus_context *ctx, struct ubus_object *obj,
578 struct ubus_request_data *req, const char *method,
579 struct blob_attr *msg)
582 char data[4096] = { 0 };
584 if (!(f = fopen("/etc/crontabs/root", "r")))
585 return rpc_errno_status();
587 fread(data, sizeof(data) - 1, 1, f);
590 blob_buf_init(&buf, 0);
591 blobmsg_add_string(&buf, "data", data);
593 ubus_send_reply(ctx, req, buf.head);
598 rpc_luci2_crontab_set(struct ubus_context *ctx, struct ubus_object *obj,
599 struct ubus_request_data *req, const char *method,
600 struct blob_attr *msg)
604 struct blob_attr *tb[__RPC_D_MAX];
606 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
607 blob_data(msg), blob_len(msg));
609 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 4096)
610 return UBUS_STATUS_INVALID_ARGUMENT;
612 if (stat("/etc/crontabs", &s) && mkdir("/etc/crontabs", 0755))
613 return rpc_errno_status();
615 if (!(f = fopen("/etc/crontabs/root", "w")))
616 return rpc_errno_status();
618 fwrite(blobmsg_data(tb[RPC_D_DATA]),
619 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
626 rpc_luci2_sshkeys_get(struct ubus_context *ctx, struct ubus_object *obj,
627 struct ubus_request_data *req, const char *method,
628 struct blob_attr *msg)
634 if (!(f = fopen("/etc/dropbear/authorized_keys", "r")))
635 return rpc_errno_status();
637 blob_buf_init(&buf, 0);
638 c = blobmsg_open_array(&buf, "keys");
640 while (fgets(line, sizeof(line) - 1, f))
642 for (p = line + strlen(line) - 1; (p > line) && isspace(*p); p--)
645 for (p = line; isspace(*p); p++)
649 blobmsg_add_string(&buf, NULL, p);
652 blobmsg_close_array(&buf, c);
655 ubus_send_reply(ctx, req, buf.head);
660 rpc_luci2_sshkeys_set(struct ubus_context *ctx, struct ubus_object *obj,
661 struct ubus_request_data *req, const char *method,
662 struct blob_attr *msg)
666 struct blob_attr *cur, *tb[__RPC_K_MAX];
668 blobmsg_parse(rpc_sshkey_policy, __RPC_K_MAX, tb,
669 blob_data(msg), blob_len(msg));
672 return UBUS_STATUS_INVALID_ARGUMENT;
674 if (!(f = fopen("/etc/dropbear/authorized_keys", "w")))
675 return rpc_errno_status();
677 blobmsg_for_each_attr(cur, tb[RPC_K_KEYS], rem)
679 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
682 fwrite(blobmsg_data(cur), blobmsg_data_len(cur) - 1, 1, f);
683 fwrite("\n", 1, 1, f);
691 rpc_luci2_password_set(struct ubus_context *ctx, struct ubus_object *obj,
692 struct ubus_request_data *req, const char *method,
693 struct blob_attr *msg)
698 struct blob_attr *tb[__RPC_P_MAX];
700 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
701 blob_data(msg), blob_len(msg));
703 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
704 return UBUS_STATUS_INVALID_ARGUMENT;
706 if (stat("/usr/bin/passwd", &s))
707 return UBUS_STATUS_NOT_FOUND;
709 if (!(s.st_mode & S_IXUSR))
710 return UBUS_STATUS_PERMISSION_DENIED;
713 return rpc_errno_status();
715 switch ((pid = fork()))
720 return rpc_errno_status();
729 if ((fd = open("/dev/null", O_RDWR)) > -1)
738 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
739 blobmsg_data(tb[RPC_P_USER]), NULL))
740 return rpc_errno_status();
745 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
746 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
747 write(fds[1], "\n", 1);
751 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
752 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
753 write(fds[1], "\n", 1);
757 waitpid(pid, NULL, 0);
764 rpc_luci2_led_list(struct ubus_context *ctx, struct ubus_object *obj,
765 struct ubus_request_data *req, const char *method,
766 struct blob_attr *msg)
770 void *list, *led, *trigger;
771 char *p, *active_trigger, line[512];
774 if (!(d = opendir("/sys/class/leds")))
775 return rpc_errno_status();
777 blob_buf_init(&buf, 0);
778 list = blobmsg_open_array(&buf, "leds");
780 while ((e = readdir(d)) != NULL)
782 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/trigger",
785 if (!(f = fopen(line, "r")))
788 led = blobmsg_open_table(&buf, NULL);
790 blobmsg_add_string(&buf, "name", e->d_name);
792 if (fgets(line, sizeof(line) - 1, f))
794 trigger = blobmsg_open_array(&buf, "triggers");
796 for (p = strtok(line, " \n"), active_trigger = NULL;
798 p = strtok(NULL, " \n"))
802 *(p + strlen(p) - 1) = 0;
807 blobmsg_add_string(&buf, NULL, p);
810 blobmsg_close_array(&buf, trigger);
813 blobmsg_add_string(&buf, "active_trigger", active_trigger);
818 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/brightness",
821 if ((f = fopen(line, "r")) != NULL)
823 if (fgets(line, sizeof(line) - 1, f))
824 blobmsg_add_u32(&buf, "brightness", atoi(line));
829 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/max_brightness",
832 if ((f = fopen(line, "r")) != NULL)
834 if (fgets(line, sizeof(line) - 1, f))
835 blobmsg_add_u32(&buf, "max_brightness", atoi(line));
840 blobmsg_close_table(&buf, led);
845 blobmsg_close_array(&buf, list);
846 ubus_send_reply(ctx, req, buf.head);
852 rpc_luci2_usb_list(struct ubus_context *ctx, struct ubus_object *obj,
853 struct ubus_request_data *req, const char *method,
854 struct blob_attr *msg)
864 const char *attributes[] = {
865 "manufacturer", "vendor_name", "s",
866 "product", "product_name", "s",
867 "idVendor", "vendor_id", "x",
868 "idProduct", "product_id", "x",
869 "serial", "serial", "s",
870 "speed", "speed", "d",
873 if (!(d = opendir("/sys/bus/usb/devices")))
874 return rpc_errno_status();
876 blob_buf_init(&buf, 0);
877 list = blobmsg_open_array(&buf, "devices");
879 while ((e = readdir(d)) != NULL)
881 if (e->d_name[0] < '0' || e->d_name[0] > '9')
884 snprintf(line, sizeof(line) - 1,
885 "/sys/bus/usb/devices/%s/%s", e->d_name, attributes[0]);
890 device = blobmsg_open_table(&buf, NULL);
892 blobmsg_add_string(&buf, "name", e->d_name);
894 for (i = 0; i < sizeof(attributes) / sizeof(attributes[0]); i += 3)
896 snprintf(line, sizeof(line) - 1,
897 "/sys/bus/usb/devices/%s/%s", e->d_name, attributes[i]);
899 if (!(f = fopen(line, "r")))
902 if (fgets(line, sizeof(line) - 1, f))
904 switch (*attributes[i+2])
907 blobmsg_add_u32(&buf, attributes[i+1],
908 strtoul(line, NULL, 16));
912 blobmsg_add_u32(&buf, attributes[i+1],
913 strtoul(line, NULL, 10));
917 if ((p = strchr(line, '\n')) != NULL)
918 while (p > line && isspace(*p))
921 blobmsg_add_string(&buf, attributes[i+1], line);
929 blobmsg_close_table(&buf, device);
934 blobmsg_close_array(&buf, list);
935 ubus_send_reply(ctx, req, buf.head);
942 dnsmasq_leasefile(void)
945 struct uci_package *p;
946 struct uci_element *e;
947 struct uci_section *s;
948 struct uci_ptr ptr = {
951 .option = "leasefile"
954 uci_load(cursor, ptr.package, &p);
959 uci_foreach_element(&p->sections, e)
961 s = uci_to_section(e);
963 if (strcmp(s->type, "dnsmasq"))
966 ptr.section = e->name;
967 uci_lookup_ptr(cursor, &ptr, NULL, true);
971 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
972 leases = fopen(ptr.o->v.string, "r");
974 uci_unload(cursor, p);
980 rpc_luci2_network_leases(struct ubus_context *ctx, struct ubus_object *obj,
981 struct ubus_request_data *req, const char *method,
982 struct blob_attr *msg)
987 char *ts, *mac, *addr, *name;
988 time_t now = time(NULL);
990 blob_buf_init(&buf, 0);
991 c = blobmsg_open_array(&buf, "leases");
993 leases = dnsmasq_leasefile();
998 while (fgets(line, sizeof(line) - 1, leases))
1000 ts = strtok(line, " \t");
1001 mac = strtok(NULL, " \t");
1002 addr = strtok(NULL, " \t");
1003 name = strtok(NULL, " \t");
1005 if (!ts || !mac || !addr || !name)
1008 if (strchr(addr, ':'))
1011 d = blobmsg_open_table(&buf, NULL);
1013 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1014 blobmsg_add_string(&buf, "macaddr", mac);
1015 blobmsg_add_string(&buf, "ipaddr", addr);
1017 if (strcmp(name, "*"))
1018 blobmsg_add_string(&buf, "hostname", name);
1020 blobmsg_close_table(&buf, d);
1026 blobmsg_close_array(&buf, c);
1027 ubus_send_reply(ctx, req, buf.head);
1033 rpc_luci2_network_leases6(struct ubus_context *ctx, struct ubus_object *obj,
1034 struct ubus_request_data *req, const char *method,
1035 struct blob_attr *msg)
1040 char *ts, *mac, *addr, *name, *duid;
1041 time_t now = time(NULL);
1043 blob_buf_init(&buf, 0);
1044 c = blobmsg_open_array(&buf, "leases");
1046 leases = fopen("/tmp/hosts/6relayd", "r");
1050 while (fgets(line, sizeof(line) - 1, leases))
1052 if (strncmp(line, "# ", 2))
1055 strtok(line + 2, " \t"); /* iface */
1057 duid = strtok(NULL, " \t");
1059 strtok(NULL, " \t"); /* iaid */
1061 name = strtok(NULL, " \t");
1062 ts = strtok(NULL, " \t");
1064 strtok(NULL, " \t"); /* id */
1065 strtok(NULL, " \t"); /* length */
1067 addr = strtok(NULL, " \t\n");
1072 d = blobmsg_open_table(&buf, NULL);
1074 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1075 blobmsg_add_string(&buf, "duid", duid);
1076 blobmsg_add_string(&buf, "ip6addr", addr);
1078 if (strcmp(name, "-"))
1079 blobmsg_add_string(&buf, "hostname", name);
1081 blobmsg_close_array(&buf, d);
1088 leases = dnsmasq_leasefile();
1093 while (fgets(line, sizeof(line) - 1, leases))
1095 ts = strtok(line, " \t");
1096 mac = strtok(NULL, " \t");
1097 addr = strtok(NULL, " \t");
1098 name = strtok(NULL, " \t");
1099 duid = strtok(NULL, " \t\n");
1101 if (!ts || !mac || !addr || !duid)
1104 if (!strchr(addr, ':'))
1107 d = blobmsg_open_table(&buf, NULL);
1109 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1110 blobmsg_add_string(&buf, "macaddr", mac);
1111 blobmsg_add_string(&buf, "ip6addr", addr);
1113 if (strcmp(name, "*"))
1114 blobmsg_add_string(&buf, "hostname", name);
1116 if (strcmp(duid, "*"))
1117 blobmsg_add_string(&buf, "duid", name);
1119 blobmsg_close_table(&buf, d);
1126 blobmsg_close_array(&buf, c);
1127 ubus_send_reply(ctx, req, buf.head);
1133 rpc_luci2_network_ct_count(struct ubus_context *ctx, struct ubus_object *obj,
1134 struct ubus_request_data *req, const char *method,
1135 struct blob_attr *msg)
1140 blob_buf_init(&buf, 0);
1142 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL)
1144 if (fgets(line, sizeof(line) - 1, f))
1145 blobmsg_add_u32(&buf, "count", atoi(line));
1150 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL)
1152 if (fgets(line, sizeof(line) - 1, f))
1153 blobmsg_add_u32(&buf, "limit", atoi(line));
1158 ubus_send_reply(ctx, req, buf.head);
1164 rpc_luci2_network_ct_table(struct ubus_context *ctx, struct ubus_object *obj,
1165 struct ubus_request_data *req, const char *method,
1166 struct blob_attr *msg)
1174 blob_buf_init(&buf, 0);
1175 c = blobmsg_open_array(&buf, "entries");
1177 if ((f = fopen("/proc/net/nf_conntrack", "r")) != NULL)
1179 while (fgets(line, sizeof(line) - 1, f))
1181 d = blobmsg_open_table(&buf, NULL);
1182 memset(seen, 0, sizeof(seen));
1184 for (i = 0, p = strtok(line, " "); p; i++, p = strtok(NULL, " "))
1187 blobmsg_add_u8(&buf, "ipv6", !strcmp(p, "ipv6"));
1189 blobmsg_add_u32(&buf, "protocol", atoi(p));
1191 blobmsg_add_u32(&buf, "expires", atoi(p));
1197 if (!seen[0] && !strncmp(p, "src=", 4))
1199 blobmsg_add_string(&buf, "src", p + 4);
1202 else if (!seen[1] && !strncmp(p, "dst=", 4))
1204 blobmsg_add_string(&buf, "dest", p + 4);
1207 else if (!seen[2] && !strncmp(p, "sport=", 6))
1209 blobmsg_add_u32(&buf, "sport", atoi(p + 6));
1212 else if (!seen[3] && !strncmp(p, "dport=", 6))
1214 blobmsg_add_u32(&buf, "dport", atoi(p + 6));
1217 else if (!strncmp(p, "packets=", 8))
1219 blobmsg_add_u32(&buf,
1220 seen[4] ? "tx_packets" : "rx_packets",
1224 else if (!strncmp(p, "bytes=", 6))
1226 blobmsg_add_u32(&buf,
1227 seen[5] ? "tx_bytes" : "rx_bytes",
1234 blobmsg_close_table(&buf, d);
1240 blobmsg_close_array(&buf, c);
1241 ubus_send_reply(ctx, req, buf.head);
1247 rpc_luci2_network_arp_table(struct ubus_context *ctx, struct ubus_object *obj,
1248 struct ubus_request_data *req, const char *method,
1249 struct blob_attr *msg)
1253 char *addr, *mac, *dev, line[128];
1255 blob_buf_init(&buf, 0);
1256 c = blobmsg_open_array(&buf, "entries");
1258 if ((f = fopen("/proc/net/arp", "r")) != NULL)
1260 /* skip header line */
1261 fgets(line, sizeof(line) - 1, f);
1263 while (fgets(line, sizeof(line) - 1, f))
1265 addr = strtok(line, " \t");
1267 strtok(NULL, " \t"); /* HW type */
1268 strtok(NULL, " \t"); /* Flags */
1270 mac = strtok(NULL, " \t");
1272 strtok(NULL, " \t"); /* Mask */
1274 dev = strtok(NULL, " \t\n");
1279 d = blobmsg_open_table(&buf, NULL);
1280 blobmsg_add_string(&buf, "ipaddr", addr);
1281 blobmsg_add_string(&buf, "macaddr", mac);
1282 blobmsg_add_string(&buf, "device", dev);
1283 blobmsg_close_table(&buf, d);
1289 blobmsg_close_array(&buf, c);
1290 ubus_send_reply(ctx, req, buf.head);
1296 put_hexaddr(const char *name, const char *s, const char *m)
1300 char as[sizeof("255.255.255.255/32\0")];
1302 a.s_addr = strtoul(s, NULL, 16);
1303 inet_ntop(AF_INET, &a, as, sizeof(as));
1307 for (a.s_addr = ntohl(strtoul(m, NULL, 16)), bits = 0;
1308 a.s_addr & 0x80000000;
1312 sprintf(as + strlen(as), "/%u", bits);
1315 blobmsg_add_string(&buf, name, as);
1319 rpc_luci2_network_routes(struct ubus_context *ctx, struct ubus_object *obj,
1320 struct ubus_request_data *req, const char *method,
1321 struct blob_attr *msg)
1325 char *dst, *dmask, *next, *metric, *device;
1329 if (!(routes = fopen("/proc/net/route", "r")))
1330 return rpc_errno_status();
1332 blob_buf_init(&buf, 0);
1333 c = blobmsg_open_array(&buf, "routes");
1335 /* skip header line */
1336 fgets(line, sizeof(line) - 1, routes);
1338 while (fgets(line, sizeof(line) - 1, routes))
1340 device = strtok(line, "\t ");
1341 dst = strtok(NULL, "\t ");
1342 next = strtok(NULL, "\t ");
1344 strtok(NULL, "\t "); /* flags */
1345 strtok(NULL, "\t "); /* refcount */
1346 strtok(NULL, "\t "); /* usecount */
1348 metric = strtok(NULL, "\t ");
1349 dmask = strtok(NULL, "\t ");
1354 d = blobmsg_open_table(&buf, NULL);
1356 put_hexaddr("target", dst, dmask);
1357 put_hexaddr("nexthop", next, NULL);
1359 n = strtoul(metric, NULL, 10);
1360 blobmsg_add_u32(&buf, "metric", n);
1362 blobmsg_add_string(&buf, "device", device);
1364 blobmsg_close_table(&buf, d);
1367 blobmsg_close_array(&buf, c);
1370 ubus_send_reply(ctx, req, buf.head);
1375 put_hex6addr(const char *name, const char *s, const char *m)
1379 char as[INET6_ADDRSTRLEN + sizeof("/128")];
1382 (((x) <= '9') ? ((x) - '0') : \
1383 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1386 for (i = 0; i < 16; i++, s += 2)
1387 a.s6_addr[i] = (16 * hex(*s)) + hex(*(s+1));
1389 inet_ntop(AF_INET6, &a, as, sizeof(as));
1392 sprintf(as + strlen(as), "/%lu", strtoul(m, NULL, 16));
1394 blobmsg_add_string(&buf, name, as);
1398 rpc_luci2_network_routes6(struct ubus_context *ctx, struct ubus_object *obj,
1399 struct ubus_request_data *req, const char *method,
1400 struct blob_attr *msg)
1404 char *src, *smask, *dst, *dmask, *next, *metric, *flags, *device;
1408 if (!(routes = fopen("/proc/net/ipv6_route", "r")))
1409 return rpc_errno_status();
1411 blob_buf_init(&buf, 0);
1412 c = blobmsg_open_array(&buf, "routes");
1414 while (fgets(line, sizeof(line) - 1, routes))
1416 dst = strtok(line, " ");
1417 dmask = strtok(NULL, " ");
1418 src = strtok(NULL, " ");
1419 smask = strtok(NULL, " ");
1420 next = strtok(NULL, " ");
1421 metric = strtok(NULL, " ");
1423 strtok(NULL, " "); /* refcount */
1424 strtok(NULL, " "); /* usecount */
1426 flags = strtok(NULL, " ");
1427 device = strtok(NULL, " \n");
1432 n = strtoul(flags, NULL, 16);
1437 d = blobmsg_open_table(&buf, NULL);
1439 put_hex6addr("target", dst, dmask);
1440 put_hex6addr("source", src, smask);
1441 put_hex6addr("nexthop", next, NULL);
1443 n = strtoul(metric, NULL, 16);
1444 blobmsg_add_u32(&buf, "metric", n);
1446 blobmsg_add_string(&buf, "device", device);
1448 blobmsg_close_table(&buf, d);
1451 blobmsg_close_array(&buf, c);
1454 ubus_send_reply(ctx, req, buf.head);
1470 opkg_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1472 struct opkg_state *s = priv;
1475 char *nl = strchr(buf, '\n');
1476 char *name = NULL, *vers = NULL, *desc = NULL;
1484 if (s->cur_offset++ < s->req_offset)
1487 if (s->cur_count++ >= s->req_count)
1493 s->array = blobmsg_open_array(blob, "packages");
1496 for (ptr = buf, last = buf, *nl = 0; ptr <= nl; ptr++)
1498 if (!*ptr || (*ptr == ' ' && *(ptr+1) == '-' && *(ptr+2) == ' '))
1510 desc = *ptr ? (ptr + 3) : NULL;
1519 c = blobmsg_open_array(blob, NULL);
1521 blobmsg_add_string(blob, NULL, name);
1522 blobmsg_add_string(blob, NULL, vers);
1525 blobmsg_add_string(blob, NULL, desc);
1527 blobmsg_close_array(blob, c);
1531 return (nl - buf + 1);
1535 opkg_finish_list(struct blob_buf *blob, int status, void *priv)
1537 struct opkg_state *s = priv;
1542 blobmsg_close_array(blob, s->array);
1543 blobmsg_add_u32(blob, "total", s->total);
1547 opkg_exec_list(const char *action, struct blob_attr *msg,
1548 struct ubus_context *ctx, struct ubus_request_data *req)
1550 struct opkg_state *state = NULL;
1551 struct blob_attr *tb[__RPC_OM_MAX];
1552 const char *cmd[5] = { "opkg", action, "-nocase", NULL, NULL };
1554 blobmsg_parse(rpc_opkg_match_policy, __RPC_OM_MAX, tb,
1555 blob_data(msg), blob_len(msg));
1557 state = malloc(sizeof(*state));
1560 return UBUS_STATUS_UNKNOWN_ERROR;
1562 memset(state, 0, sizeof(*state));
1564 if (tb[RPC_OM_PATTERN])
1565 cmd[3] = blobmsg_data(tb[RPC_OM_PATTERN]);
1567 if (tb[RPC_OM_LIMIT])
1568 state->req_count = blobmsg_get_u32(tb[RPC_OM_LIMIT]);
1570 if (tb[RPC_OM_OFFSET])
1571 state->req_offset = blobmsg_get_u32(tb[RPC_OM_OFFSET]);
1573 if (state->req_offset < 0)
1574 state->req_offset = 0;
1576 if (state->req_count <= 0 || state->req_count > 100)
1577 state->req_count = 100;
1579 return rpc_exec(cmd, opkg_parse_list, NULL, opkg_finish_list,
1585 rpc_luci2_opkg_list(struct ubus_context *ctx, struct ubus_object *obj,
1586 struct ubus_request_data *req, const char *method,
1587 struct blob_attr *msg)
1589 return opkg_exec_list("list", msg, ctx, req);
1593 rpc_luci2_opkg_list_installed(struct ubus_context *ctx, struct ubus_object *obj,
1594 struct ubus_request_data *req, const char *method,
1595 struct blob_attr *msg)
1597 return opkg_exec_list("list-installed", msg, ctx, req);
1601 rpc_luci2_opkg_find(struct ubus_context *ctx, struct ubus_object *obj,
1602 struct ubus_request_data *req, const char *method,
1603 struct blob_attr *msg)
1605 return opkg_exec_list("find", msg, ctx, req);
1609 rpc_luci2_opkg_update(struct ubus_context *ctx, struct ubus_object *obj,
1610 struct ubus_request_data *req, const char *method,
1611 struct blob_attr *msg)
1613 const char *cmd[3] = { "opkg", "update", NULL };
1614 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1618 rpc_luci2_opkg_install(struct ubus_context *ctx, struct ubus_object *obj,
1619 struct ubus_request_data *req, const char *method,
1620 struct blob_attr *msg)
1622 struct blob_attr *tb[__RPC_OP_MAX];
1623 const char *cmd[5] = { "opkg", "--force-overwrite",
1624 "install", NULL, NULL };
1626 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1627 blob_data(msg), blob_len(msg));
1629 if (!tb[RPC_OP_PACKAGE])
1630 return UBUS_STATUS_INVALID_ARGUMENT;
1632 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1634 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1638 rpc_luci2_opkg_remove(struct ubus_context *ctx, struct ubus_object *obj,
1639 struct ubus_request_data *req, const char *method,
1640 struct blob_attr *msg)
1642 struct blob_attr *tb[__RPC_OP_MAX];
1643 const char *cmd[5] = { "opkg", "--force-removal-of-dependent-packages",
1644 "remove", NULL, NULL };
1646 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1647 blob_data(msg), blob_len(msg));
1649 if (!tb[RPC_OP_PACKAGE])
1650 return UBUS_STATUS_INVALID_ARGUMENT;
1652 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1654 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1658 rpc_luci2_opkg_config_get(struct ubus_context *ctx, struct ubus_object *obj,
1659 struct ubus_request_data *req, const char *method,
1660 struct blob_attr *msg)
1663 char conf[2048] = { 0 };
1665 if (!(f = fopen("/etc/opkg.conf", "r")))
1666 return rpc_errno_status();
1668 fread(conf, sizeof(conf) - 1, 1, f);
1671 blob_buf_init(&buf, 0);
1672 blobmsg_add_string(&buf, "config", conf);
1674 ubus_send_reply(ctx, req, buf.head);
1679 rpc_luci2_opkg_config_set(struct ubus_context *ctx, struct ubus_object *obj,
1680 struct ubus_request_data *req, const char *method,
1681 struct blob_attr *msg)
1684 struct blob_attr *tb[__RPC_D_MAX];
1686 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
1687 blob_data(msg), blob_len(msg));
1689 if (!tb[RPC_D_DATA])
1690 return UBUS_STATUS_INVALID_ARGUMENT;
1692 if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048)
1693 return UBUS_STATUS_NOT_SUPPORTED;
1695 if (!(f = fopen("/etc/opkg.conf", "w")))
1696 return rpc_errno_status();
1698 fwrite(blobmsg_data(tb[RPC_D_DATA]),
1699 blobmsg_data_len(tb[RPC_D_DATA]), 1, f);
1706 int rpc_luci2_api_init(struct ubus_context *ctx)
1710 static const struct ubus_method luci2_system_methods[] = {
1711 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log),
1712 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg),
1713 UBUS_METHOD_NOARG("diskfree", rpc_luci2_system_diskfree),
1714 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list),
1715 UBUS_METHOD("process_signal", rpc_luci2_process_signal,
1717 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list),
1718 UBUS_METHOD("init_action", rpc_luci2_init_action,
1720 UBUS_METHOD_NOARG("rclocal_get", rpc_luci2_rclocal_get),
1721 UBUS_METHOD("rclocal_set", rpc_luci2_rclocal_set,
1723 UBUS_METHOD_NOARG("crontab_get", rpc_luci2_crontab_get),
1724 UBUS_METHOD("crontab_set", rpc_luci2_crontab_set,
1726 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get),
1727 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set,
1729 UBUS_METHOD("password_set", rpc_luci2_password_set,
1730 rpc_password_policy),
1731 UBUS_METHOD_NOARG("led_list", rpc_luci2_led_list),
1732 UBUS_METHOD_NOARG("usb_list", rpc_luci2_usb_list)
1735 static struct ubus_object_type luci2_system_type =
1736 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods);
1738 static struct ubus_object system_obj = {
1739 .name = "luci2.system",
1740 .type = &luci2_system_type,
1741 .methods = luci2_system_methods,
1742 .n_methods = ARRAY_SIZE(luci2_system_methods),
1746 static const struct ubus_method luci2_network_methods[] = {
1747 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count),
1748 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table),
1749 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table),
1750 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases),
1751 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6),
1752 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes),
1753 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6),
1756 static struct ubus_object_type luci2_network_type =
1757 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods);
1759 static struct ubus_object network_obj = {
1760 .name = "luci2.network",
1761 .type = &luci2_network_type,
1762 .methods = luci2_network_methods,
1763 .n_methods = ARRAY_SIZE(luci2_network_methods),
1767 static const struct ubus_method luci2_opkg_methods[] = {
1768 UBUS_METHOD("list", rpc_luci2_opkg_list,
1769 rpc_opkg_match_policy),
1770 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed,
1771 rpc_opkg_match_policy),
1772 UBUS_METHOD("find", rpc_luci2_opkg_find,
1773 rpc_opkg_match_policy),
1774 UBUS_METHOD("install", rpc_luci2_opkg_install,
1775 rpc_opkg_package_policy),
1776 UBUS_METHOD("remove", rpc_luci2_opkg_remove,
1777 rpc_opkg_package_policy),
1778 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update),
1779 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get),
1780 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set,
1784 static struct ubus_object_type luci2_opkg_type =
1785 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods);
1787 static struct ubus_object opkg_obj = {
1788 .name = "luci2.opkg",
1789 .type = &luci2_opkg_type,
1790 .methods = luci2_opkg_methods,
1791 .n_methods = ARRAY_SIZE(luci2_opkg_methods),
1794 cursor = uci_alloc_context();
1797 return UBUS_STATUS_UNKNOWN_ERROR;
1799 rv |= ubus_add_object(ctx, &system_obj);
1800 rv |= ubus_add_object(ctx, &network_obj);
1801 rv |= ubus_add_object(ctx, &opkg_obj);