2 * rpcd - 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>
33 #include <libubox/blobmsg_json.h>
34 #include <libubox/avl-cmp.h>
38 #include <rpcd/plugin.h>
40 /* limit of log size buffer */
41 #define RPC_LUCI2_MAX_LOGSIZE (128 * 1024)
42 #define RPC_LUCI2_DEF_LOGSIZE (16 * 1024)
44 /* location of menu definitions */
45 #define RPC_LUCI2_MENU_FILES "/usr/share/rpcd/menu.d/*.json" /* */
48 static const struct rpc_daemon_ops *ops;
50 static struct blob_buf buf;
51 static struct uci_context *cursor;
59 static const struct blobmsg_policy rpc_signal_policy[__RPC_S_MAX] = {
60 [RPC_S_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
61 [RPC_S_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
70 static const struct blobmsg_policy rpc_init_policy[__RPC_I_MAX] = {
71 [RPC_I_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
72 [RPC_I_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
80 static const struct blobmsg_policy rpc_data_policy[__RPC_D_MAX] = {
81 [RPC_D_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
89 static const struct blobmsg_policy rpc_sshkey_policy[__RPC_K_MAX] = {
90 [RPC_K_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
99 static const struct blobmsg_policy rpc_password_policy[__RPC_P_MAX] = {
100 [RPC_P_USER] = { .name = "user", .type = BLOBMSG_TYPE_STRING },
101 [RPC_P_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
111 static const struct blobmsg_policy rpc_opkg_match_policy[__RPC_OM_MAX] = {
112 [RPC_OM_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
113 [RPC_OM_OFFSET] = { .name = "offset", .type = BLOBMSG_TYPE_INT32 },
114 [RPC_OM_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
122 static const struct blobmsg_policy rpc_opkg_package_policy[__RPC_OP_MAX] = {
123 [RPC_OP_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
131 static const struct blobmsg_policy rpc_upgrade_policy[__RPC_UPGRADE_MAX] = {
132 [RPC_UPGRADE_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
140 static const struct blobmsg_policy rpc_menu_policy[__RPC_MENU_MAX] = {
141 [RPC_MENU_SESSION] = { .name = "ubus_rpc_session",
142 .type = BLOBMSG_TYPE_STRING },
147 rpc_errno_status(void)
152 return UBUS_STATUS_PERMISSION_DENIED;
155 return UBUS_STATUS_INVALID_ARGUMENT;
158 return UBUS_STATUS_NOT_FOUND;
161 return UBUS_STATUS_INVALID_ARGUMENT;
164 return UBUS_STATUS_UNKNOWN_ERROR;
169 log_read(FILE *log, int logsize)
175 logsize = RPC_LUCI2_DEF_LOGSIZE;
177 len = (logsize > RPC_LUCI2_MAX_LOGSIZE) ? RPC_LUCI2_MAX_LOGSIZE : logsize;
178 logbuf = blobmsg_alloc_string_buffer(&buf, "log", len + 1);
183 while (logsize > RPC_LUCI2_MAX_LOGSIZE)
185 len = logsize % RPC_LUCI2_MAX_LOGSIZE;
188 len = RPC_LUCI2_MAX_LOGSIZE;
190 fread(logbuf, 1, len, log);
194 len = fread(logbuf, 1, logsize, log);
197 blobmsg_add_string_buffer(&buf);
201 rpc_luci2_system_log(struct ubus_context *ctx, struct ubus_object *obj,
202 struct ubus_request_data *req, const char *method,
203 struct blob_attr *msg)
207 const char *logfile = NULL;
209 struct uci_package *p;
210 struct uci_element *e;
211 struct uci_section *s;
212 struct uci_ptr ptr = { .package = "system" };
214 uci_load(cursor, ptr.package, &p);
217 return UBUS_STATUS_NOT_FOUND;
219 uci_foreach_element(&p->sections, e)
221 s = uci_to_section(e);
223 if (strcmp(s->type, "system"))
227 ptr.option = "log_type";
228 ptr.section = e->name;
229 uci_lookup_ptr(cursor, &ptr, NULL, true);
233 if (ptr.o && ptr.o->type == UCI_TYPE_STRING &&
234 !strcmp(ptr.o->v.string, "file"))
237 ptr.option = "log_file";
238 uci_lookup_ptr(cursor, &ptr, NULL, true);
240 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
241 logfile = ptr.o->v.string;
243 logfile = "/var/log/messages";
245 if (stat(logfile, &st) || !(log = fopen(logfile, "r")))
248 logsize = st.st_size;
253 ptr.option = "log_size";
254 uci_lookup_ptr(cursor, &ptr, NULL, true);
256 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
257 logsize = atoi(ptr.o->v.string) * 1024;
259 if (!(log = popen("logread", "r")))
263 blob_buf_init(&buf, 0);
265 log_read(log, logsize);
268 uci_unload(cursor, p);
269 ubus_send_reply(ctx, req, buf.head);
273 uci_unload(cursor, p);
274 return rpc_errno_status();
278 rpc_luci2_system_dmesg(struct ubus_context *ctx, struct ubus_object *obj,
279 struct ubus_request_data *req, const char *method,
280 struct blob_attr *msg)
284 if (!(log = popen("dmesg", "r")))
285 return rpc_errno_status();
287 blob_buf_init(&buf, 0);
289 log_read(log, RPC_LUCI2_MAX_LOGSIZE);
292 ubus_send_reply(ctx, req, buf.head);
297 rpc_luci2_system_diskfree(struct ubus_context *ctx, struct ubus_object *obj,
298 struct ubus_request_data *req, const char *method,
299 struct blob_attr *msg)
304 const char *fslist[] = {
309 blob_buf_init(&buf, 0);
311 for (i = 0; i < sizeof(fslist) / sizeof(fslist[0]); i += 2)
313 if (statvfs(fslist[i], &s))
316 c = blobmsg_open_table(&buf, fslist[i+1]);
318 blobmsg_add_u32(&buf, "total", s.f_blocks * s.f_frsize);
319 blobmsg_add_u32(&buf, "free", s.f_bfree * s.f_frsize);
320 blobmsg_add_u32(&buf, "used", (s.f_blocks - s.f_bfree) * s.f_frsize);
322 blobmsg_close_table(&buf, c);
325 ubus_send_reply(ctx, req, buf.head);
330 rpc_luci2_process_list(struct ubus_context *ctx, struct ubus_object *obj,
331 struct ubus_request_data *req, const char *method,
332 struct blob_attr *msg)
337 char *pid, *ppid, *user, *stat, *vsz, *pvsz, *pcpu, *cmd;
339 if (!(top = popen("/bin/busybox top -bn1", "r")))
340 return rpc_errno_status();
342 blob_buf_init(&buf, 0);
343 c = blobmsg_open_array(&buf, "processes");
345 while (fgets(line, sizeof(line) - 1, top))
347 pid = strtok(line, " ");
349 if (*pid < '0' || *pid > '9')
352 ppid = strtok(NULL, " ");
353 user = strtok(NULL, " ");
354 stat = strtok(NULL, " ");
367 vsz = strtok(stat + 4, " ");
368 pvsz = strtok(NULL, " ");
369 pcpu = strtok(NULL, " ");
370 cmd = strtok(NULL, "\n");
375 d = blobmsg_open_table(&buf, NULL);
377 blobmsg_add_u32(&buf, "pid", atoi(pid));
378 blobmsg_add_u32(&buf, "ppid", atoi(ppid));
379 blobmsg_add_string(&buf, "user", user);
380 blobmsg_add_string(&buf, "stat", stat);
381 blobmsg_add_u32(&buf, "vsize", atoi(vsz) * 1024);
382 blobmsg_add_u32(&buf, "vsize_percent", atoi(pvsz));
383 blobmsg_add_u32(&buf, "cpu_percent", atoi(pcpu));
384 blobmsg_add_string(&buf, "command", cmd);
386 blobmsg_close_table(&buf, d);
390 blobmsg_close_array(&buf, c);
392 ubus_send_reply(ctx, req, buf.head);
397 rpc_luci2_process_signal(struct ubus_context *ctx, struct ubus_object *obj,
398 struct ubus_request_data *req, const char *method,
399 struct blob_attr *msg)
402 struct blob_attr *tb[__RPC_S_MAX];
404 blobmsg_parse(rpc_signal_policy, __RPC_S_MAX, tb,
405 blob_data(msg), blob_len(msg));
407 if (!tb[RPC_S_SIGNAL] || !tb[RPC_S_PID])
410 return rpc_errno_status();
413 pid = blobmsg_get_u32(tb[RPC_S_PID]);
414 sig = blobmsg_get_u32(tb[RPC_S_SIGNAL]);
417 return rpc_errno_status();
423 rpc_luci2_init_list(struct ubus_context *ctx, struct ubus_object *obj,
424 struct ubus_request_data *req, const char *method,
425 struct blob_attr *msg)
429 char *p, path[PATH_MAX];
435 if (!(d = opendir("/etc/init.d")))
436 return rpc_errno_status();
438 blob_buf_init(&buf, 0);
439 c = blobmsg_open_array(&buf, "initscripts");
441 while ((e = readdir(d)) != NULL)
443 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s", e->d_name);
445 if (stat(path, &s) || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR))
448 if ((f = fopen(path, "r")) != NULL)
451 p = fgets(path, sizeof(path) - 1, f);
453 if (!p || !strstr(p, "/etc/rc.common"))
456 t = blobmsg_open_table(&buf, NULL);
458 blobmsg_add_string(&buf, "name", e->d_name);
460 while (fgets(path, sizeof(path) - 1, f))
462 p = strtok(path, "= \t");
464 if (!strcmp(p, "START") && !!(p = strtok(NULL, "= \t\n")))
467 blobmsg_add_u32(&buf, "start", n);
469 else if (!strcmp(p, "STOP") && !!(p = strtok(NULL, "= \t\n")))
471 blobmsg_add_u32(&buf, "stop", atoi(p));
478 snprintf(path, sizeof(path) - 1, "/etc/rc.d/S%02d%s",
481 blobmsg_add_u8(&buf, "enabled",
482 (!stat(path, &s) && (s.st_mode & S_IXUSR)));
486 blobmsg_add_u8(&buf, "enabled", 0);
489 blobmsg_close_table(&buf, t);
497 blobmsg_close_array(&buf, c);
499 ubus_send_reply(ctx, req, buf.head);
504 rpc_luci2_init_action(struct ubus_context *ctx, struct ubus_object *obj,
505 struct ubus_request_data *req, const char *method,
506 struct blob_attr *msg)
513 struct blob_attr *tb[__RPC_I_MAX];
515 blobmsg_parse(rpc_init_policy, __RPC_I_MAX, tb,
516 blob_data(msg), blob_len(msg));
518 if (!tb[RPC_I_NAME] || !tb[RPC_I_ACTION])
519 return UBUS_STATUS_INVALID_ARGUMENT;
521 action = blobmsg_data(tb[RPC_I_ACTION]);
523 if (strcmp(action, "start") && strcmp(action, "stop") &&
524 strcmp(action, "reload") && strcmp(action, "restart") &&
525 strcmp(action, "enable") && strcmp(action, "disable"))
526 return UBUS_STATUS_INVALID_ARGUMENT;
528 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s",
529 (char *)blobmsg_data(tb[RPC_I_NAME]));
532 return rpc_errno_status();
534 if (!(s.st_mode & S_IXUSR))
535 return UBUS_STATUS_PERMISSION_DENIED;
537 switch ((pid = fork()))
540 return rpc_errno_status();
545 if ((fd = open("/dev/null", O_RDWR)) > -1)
556 if (execl(path, path, action, NULL))
557 return rpc_errno_status();
565 rpc_luci2_rclocal_get(struct ubus_context *ctx, struct ubus_object *obj,
566 struct ubus_request_data *req, const char *method,
567 struct blob_attr *msg)
570 char data[4096] = { 0 };
572 if (!(f = fopen("/etc/rc.local", "r")))
573 return rpc_errno_status();
575 fread(data, sizeof(data) - 1, 1, f);
578 blob_buf_init(&buf, 0);
579 blobmsg_add_string(&buf, "data", data);
581 ubus_send_reply(ctx, req, buf.head);
586 rpc_luci2_rclocal_set(struct ubus_context *ctx, struct ubus_object *obj,
587 struct ubus_request_data *req, const char *method,
588 struct blob_attr *msg)
591 struct blob_attr *tb[__RPC_D_MAX];
593 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
594 blob_data(msg), blob_len(msg));
596 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 4096)
597 return UBUS_STATUS_INVALID_ARGUMENT;
599 if (!(f = fopen("/etc/rc.local", "w")))
600 return rpc_errno_status();
602 fwrite(blobmsg_data(tb[RPC_D_DATA]),
603 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
610 rpc_luci2_crontab_get(struct ubus_context *ctx, struct ubus_object *obj,
611 struct ubus_request_data *req, const char *method,
612 struct blob_attr *msg)
615 char data[4096] = { 0 };
617 if (!(f = fopen("/etc/crontabs/root", "r")))
618 return rpc_errno_status();
620 fread(data, sizeof(data) - 1, 1, f);
623 blob_buf_init(&buf, 0);
624 blobmsg_add_string(&buf, "data", data);
626 ubus_send_reply(ctx, req, buf.head);
631 rpc_luci2_crontab_set(struct ubus_context *ctx, struct ubus_object *obj,
632 struct ubus_request_data *req, const char *method,
633 struct blob_attr *msg)
637 struct blob_attr *tb[__RPC_D_MAX];
639 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
640 blob_data(msg), blob_len(msg));
642 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 4096)
643 return UBUS_STATUS_INVALID_ARGUMENT;
645 if (stat("/etc/crontabs", &s) && mkdir("/etc/crontabs", 0755))
646 return rpc_errno_status();
648 if (!(f = fopen("/etc/crontabs/root", "w")))
649 return rpc_errno_status();
651 fwrite(blobmsg_data(tb[RPC_D_DATA]),
652 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
659 rpc_luci2_sshkeys_get(struct ubus_context *ctx, struct ubus_object *obj,
660 struct ubus_request_data *req, const char *method,
661 struct blob_attr *msg)
667 if (!(f = fopen("/etc/dropbear/authorized_keys", "r")))
668 return rpc_errno_status();
670 blob_buf_init(&buf, 0);
671 c = blobmsg_open_array(&buf, "keys");
673 while (fgets(line, sizeof(line) - 1, f))
675 for (p = line + strlen(line) - 1; (p > line) && isspace(*p); p--)
678 for (p = line; isspace(*p); p++)
682 blobmsg_add_string(&buf, NULL, p);
685 blobmsg_close_array(&buf, c);
688 ubus_send_reply(ctx, req, buf.head);
693 rpc_luci2_sshkeys_set(struct ubus_context *ctx, struct ubus_object *obj,
694 struct ubus_request_data *req, const char *method,
695 struct blob_attr *msg)
699 struct blob_attr *cur, *tb[__RPC_K_MAX];
701 blobmsg_parse(rpc_sshkey_policy, __RPC_K_MAX, tb,
702 blob_data(msg), blob_len(msg));
705 return UBUS_STATUS_INVALID_ARGUMENT;
707 if (!(f = fopen("/etc/dropbear/authorized_keys", "w")))
708 return rpc_errno_status();
710 blobmsg_for_each_attr(cur, tb[RPC_K_KEYS], rem)
712 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
715 fwrite(blobmsg_data(cur), blobmsg_data_len(cur) - 1, 1, f);
716 fwrite("\n", 1, 1, f);
724 rpc_luci2_password_set(struct ubus_context *ctx, struct ubus_object *obj,
725 struct ubus_request_data *req, const char *method,
726 struct blob_attr *msg)
731 struct blob_attr *tb[__RPC_P_MAX];
733 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
734 blob_data(msg), blob_len(msg));
736 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
737 return UBUS_STATUS_INVALID_ARGUMENT;
739 if (stat("/usr/bin/passwd", &s))
740 return UBUS_STATUS_NOT_FOUND;
742 if (!(s.st_mode & S_IXUSR))
743 return UBUS_STATUS_PERMISSION_DENIED;
746 return rpc_errno_status();
748 switch ((pid = fork()))
753 return rpc_errno_status();
762 if ((fd = open("/dev/null", O_RDWR)) > -1)
771 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
772 blobmsg_data(tb[RPC_P_USER]), NULL))
773 return rpc_errno_status();
778 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
779 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
780 write(fds[1], "\n", 1);
784 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
785 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
786 write(fds[1], "\n", 1);
790 waitpid(pid, NULL, 0);
797 rpc_luci2_led_list(struct ubus_context *ctx, struct ubus_object *obj,
798 struct ubus_request_data *req, const char *method,
799 struct blob_attr *msg)
803 void *list, *led, *trigger;
804 char *p, *active_trigger, line[512];
807 if (!(d = opendir("/sys/class/leds")))
808 return rpc_errno_status();
810 blob_buf_init(&buf, 0);
811 list = blobmsg_open_array(&buf, "leds");
813 while ((e = readdir(d)) != NULL)
815 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/trigger",
818 if (!(f = fopen(line, "r")))
821 led = blobmsg_open_table(&buf, NULL);
823 blobmsg_add_string(&buf, "name", e->d_name);
825 if (fgets(line, sizeof(line) - 1, f))
827 trigger = blobmsg_open_array(&buf, "triggers");
829 for (p = strtok(line, " \n"), active_trigger = NULL;
831 p = strtok(NULL, " \n"))
835 *(p + strlen(p) - 1) = 0;
840 blobmsg_add_string(&buf, NULL, p);
843 blobmsg_close_array(&buf, trigger);
846 blobmsg_add_string(&buf, "active_trigger", active_trigger);
851 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/brightness",
854 if ((f = fopen(line, "r")) != NULL)
856 if (fgets(line, sizeof(line) - 1, f))
857 blobmsg_add_u32(&buf, "brightness", atoi(line));
862 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/max_brightness",
865 if ((f = fopen(line, "r")) != NULL)
867 if (fgets(line, sizeof(line) - 1, f))
868 blobmsg_add_u32(&buf, "max_brightness", atoi(line));
873 blobmsg_close_table(&buf, led);
878 blobmsg_close_array(&buf, list);
879 ubus_send_reply(ctx, req, buf.head);
885 rpc_luci2_usb_list(struct ubus_context *ctx, struct ubus_object *obj,
886 struct ubus_request_data *req, const char *method,
887 struct blob_attr *msg)
897 const char *attributes[] = {
898 "manufacturer", "vendor_name", "s",
899 "product", "product_name", "s",
900 "idVendor", "vendor_id", "x",
901 "idProduct", "product_id", "x",
902 "serial", "serial", "s",
903 "speed", "speed", "d",
906 if (!(d = opendir("/sys/bus/usb/devices")))
907 return rpc_errno_status();
909 blob_buf_init(&buf, 0);
910 list = blobmsg_open_array(&buf, "devices");
912 while ((e = readdir(d)) != NULL)
914 if (e->d_name[0] < '0' || e->d_name[0] > '9')
917 snprintf(line, sizeof(line) - 1,
918 "/sys/bus/usb/devices/%s/%s", e->d_name, attributes[0]);
923 device = blobmsg_open_table(&buf, NULL);
925 blobmsg_add_string(&buf, "name", e->d_name);
927 for (i = 0; i < sizeof(attributes) / sizeof(attributes[0]); i += 3)
929 snprintf(line, sizeof(line) - 1,
930 "/sys/bus/usb/devices/%s/%s", e->d_name, attributes[i]);
932 if (!(f = fopen(line, "r")))
935 if (fgets(line, sizeof(line) - 1, f))
937 switch (*attributes[i+2])
940 blobmsg_add_u32(&buf, attributes[i+1],
941 strtoul(line, NULL, 16));
945 blobmsg_add_u32(&buf, attributes[i+1],
946 strtoul(line, NULL, 10));
950 if ((p = strchr(line, '\n')) != NULL)
951 while (p > line && isspace(*p))
954 blobmsg_add_string(&buf, attributes[i+1], line);
962 blobmsg_close_table(&buf, device);
967 blobmsg_close_array(&buf, list);
968 ubus_send_reply(ctx, req, buf.head);
974 rpc_luci2_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
975 struct ubus_request_data *req, const char *method,
976 struct blob_attr *msg)
978 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
979 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
983 rpc_luci2_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
984 struct ubus_request_data *req, const char *method,
985 struct blob_attr *msg)
991 rpc_luci2_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
992 struct ubus_request_data *req, const char *method,
993 struct blob_attr *msg)
995 if (unlink("/tmp/firmware.bin"))
996 return rpc_errno_status();
1002 rpc_luci2_backup_restore(struct ubus_context *ctx, struct ubus_object *obj,
1003 struct ubus_request_data *req, const char *method,
1004 struct blob_attr *msg)
1006 const char *cmd[4] = { "sysupgrade", "--restore-backup",
1007 "/tmp/backup.tar.gz", NULL };
1009 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
1013 rpc_luci2_backup_clean(struct ubus_context *ctx, struct ubus_object *obj,
1014 struct ubus_request_data *req, const char *method,
1015 struct blob_attr *msg)
1017 if (unlink("/tmp/backup.tar.gz"))
1018 return rpc_errno_status();
1024 rpc_luci2_backup_config_get(struct ubus_context *ctx, struct ubus_object *obj,
1025 struct ubus_request_data *req, const char *method,
1026 struct blob_attr *msg)
1029 char conf[2048] = { 0 };
1031 if (!(f = fopen("/etc/sysupgrade.conf", "r")))
1032 return rpc_errno_status();
1034 fread(conf, sizeof(conf) - 1, 1, f);
1037 blob_buf_init(&buf, 0);
1038 blobmsg_add_string(&buf, "config", conf);
1040 ubus_send_reply(ctx, req, buf.head);
1045 rpc_luci2_backup_config_set(struct ubus_context *ctx, struct ubus_object *obj,
1046 struct ubus_request_data *req, const char *method,
1047 struct blob_attr *msg)
1050 struct blob_attr *tb[__RPC_D_MAX];
1052 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
1053 blob_data(msg), blob_len(msg));
1055 if (!tb[RPC_D_DATA])
1056 return UBUS_STATUS_INVALID_ARGUMENT;
1058 if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048)
1059 return UBUS_STATUS_NOT_SUPPORTED;
1061 if (!(f = fopen("/etc/sysupgrade.conf", "w")))
1062 return rpc_errno_status();
1064 fwrite(blobmsg_data(tb[RPC_D_DATA]),
1065 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
1071 struct backup_state {
1077 backup_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1079 struct backup_state *s = priv;
1080 char *nl = strchr(buf, '\n');
1088 s->array = blobmsg_open_array(blob, "files");
1092 blobmsg_add_string(blob, NULL, buf);
1094 return (nl - buf + 1);
1098 backup_finish_list(struct blob_buf *blob, int status, void *priv)
1100 struct backup_state *s = priv;
1103 return UBUS_STATUS_NO_DATA;
1105 blobmsg_close_array(blob, s->array);
1107 return UBUS_STATUS_OK;
1111 rpc_luci2_backup_list(struct ubus_context *ctx, struct ubus_object *obj,
1112 struct ubus_request_data *req, const char *method,
1113 struct blob_attr *msg)
1115 struct backup_state *state = NULL;
1116 const char *cmd[3] = { "sysupgrade", "--list-backup", NULL };
1118 state = malloc(sizeof(*state));
1121 return UBUS_STATUS_UNKNOWN_ERROR;
1123 memset(state, 0, sizeof(*state));
1125 return ops->exec(cmd, NULL, backup_parse_list, NULL, backup_finish_list,
1130 rpc_luci2_reset_test(struct ubus_context *ctx, struct ubus_object *obj,
1131 struct ubus_request_data *req, const char *method,
1132 struct blob_attr *msg)
1136 char line[64] = { 0 };
1137 bool supported = false;
1139 if (!stat("/sbin/mtd", &s) && (s.st_mode & S_IXUSR))
1141 if ((mtd = fopen("/proc/mtd", "r")) != NULL)
1143 while (fgets(line, sizeof(line) - 1, mtd))
1145 if (strstr(line, "\"rootfs_data\""))
1156 blob_buf_init(&buf, 0);
1157 blobmsg_add_u8(&buf, "supported", supported);
1159 ubus_send_reply(ctx, req, buf.head);
1165 rpc_luci2_reset_start(struct ubus_context *ctx, struct ubus_object *obj,
1166 struct ubus_request_data *req, const char *method,
1167 struct blob_attr *msg)
1172 return rpc_errno_status();
1185 execl("/sbin/mtd", "/sbin/mtd", "-r", "erase", "rootfs_data", NULL);
1187 return rpc_errno_status();
1195 rpc_luci2_reboot(struct ubus_context *ctx, struct ubus_object *obj,
1196 struct ubus_request_data *req, const char *method,
1197 struct blob_attr *msg)
1202 return rpc_errno_status();
1213 execl("/sbin/reboot", "/sbin/reboot", NULL);
1215 return rpc_errno_status();
1224 dnsmasq_leasefile(void)
1226 FILE *leases = NULL;
1227 struct uci_package *p;
1228 struct uci_element *e;
1229 struct uci_section *s;
1230 struct uci_ptr ptr = {
1233 .option = "leasefile"
1236 uci_load(cursor, ptr.package, &p);
1241 uci_foreach_element(&p->sections, e)
1243 s = uci_to_section(e);
1245 if (strcmp(s->type, "dnsmasq"))
1248 ptr.section = e->name;
1249 uci_lookup_ptr(cursor, &ptr, NULL, true);
1253 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
1254 leases = fopen(ptr.o->v.string, "r");
1256 uci_unload(cursor, p);
1262 rpc_luci2_network_leases(struct ubus_context *ctx, struct ubus_object *obj,
1263 struct ubus_request_data *req, const char *method,
1264 struct blob_attr *msg)
1269 char *ts, *mac, *addr, *name;
1270 time_t now = time(NULL);
1272 blob_buf_init(&buf, 0);
1273 c = blobmsg_open_array(&buf, "leases");
1275 leases = dnsmasq_leasefile();
1280 while (fgets(line, sizeof(line) - 1, leases))
1282 ts = strtok(line, " \t");
1283 mac = strtok(NULL, " \t");
1284 addr = strtok(NULL, " \t");
1285 name = strtok(NULL, " \t");
1287 if (!ts || !mac || !addr || !name)
1290 if (strchr(addr, ':'))
1293 d = blobmsg_open_table(&buf, NULL);
1295 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1296 blobmsg_add_string(&buf, "macaddr", mac);
1297 blobmsg_add_string(&buf, "ipaddr", addr);
1299 if (strcmp(name, "*"))
1300 blobmsg_add_string(&buf, "hostname", name);
1302 blobmsg_close_table(&buf, d);
1308 blobmsg_close_array(&buf, c);
1309 ubus_send_reply(ctx, req, buf.head);
1315 rpc_luci2_network_leases6(struct ubus_context *ctx, struct ubus_object *obj,
1316 struct ubus_request_data *req, const char *method,
1317 struct blob_attr *msg)
1322 char *ts, *mac, *addr, *name, *duid;
1323 time_t now = time(NULL);
1325 blob_buf_init(&buf, 0);
1326 c = blobmsg_open_array(&buf, "leases");
1328 leases = fopen("/tmp/hosts/6relayd", "r");
1332 while (fgets(line, sizeof(line) - 1, leases))
1334 if (strncmp(line, "# ", 2))
1337 strtok(line + 2, " \t"); /* iface */
1339 duid = strtok(NULL, " \t");
1341 strtok(NULL, " \t"); /* iaid */
1343 name = strtok(NULL, " \t");
1344 ts = strtok(NULL, " \t");
1346 strtok(NULL, " \t"); /* id */
1347 strtok(NULL, " \t"); /* length */
1349 addr = strtok(NULL, " \t\n");
1354 d = blobmsg_open_table(&buf, NULL);
1356 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1357 blobmsg_add_string(&buf, "duid", duid);
1358 blobmsg_add_string(&buf, "ip6addr", addr);
1360 if (strcmp(name, "-"))
1361 blobmsg_add_string(&buf, "hostname", name);
1363 blobmsg_close_array(&buf, d);
1370 leases = dnsmasq_leasefile();
1375 while (fgets(line, sizeof(line) - 1, leases))
1377 ts = strtok(line, " \t");
1378 mac = strtok(NULL, " \t");
1379 addr = strtok(NULL, " \t");
1380 name = strtok(NULL, " \t");
1381 duid = strtok(NULL, " \t\n");
1383 if (!ts || !mac || !addr || !duid)
1386 if (!strchr(addr, ':'))
1389 d = blobmsg_open_table(&buf, NULL);
1391 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1392 blobmsg_add_string(&buf, "macaddr", mac);
1393 blobmsg_add_string(&buf, "ip6addr", addr);
1395 if (strcmp(name, "*"))
1396 blobmsg_add_string(&buf, "hostname", name);
1398 if (strcmp(duid, "*"))
1399 blobmsg_add_string(&buf, "duid", name);
1401 blobmsg_close_table(&buf, d);
1408 blobmsg_close_array(&buf, c);
1409 ubus_send_reply(ctx, req, buf.head);
1415 rpc_luci2_network_ct_count(struct ubus_context *ctx, struct ubus_object *obj,
1416 struct ubus_request_data *req, const char *method,
1417 struct blob_attr *msg)
1422 blob_buf_init(&buf, 0);
1424 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL)
1426 if (fgets(line, sizeof(line) - 1, f))
1427 blobmsg_add_u32(&buf, "count", atoi(line));
1432 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL)
1434 if (fgets(line, sizeof(line) - 1, f))
1435 blobmsg_add_u32(&buf, "limit", atoi(line));
1440 ubus_send_reply(ctx, req, buf.head);
1446 rpc_luci2_network_ct_table(struct ubus_context *ctx, struct ubus_object *obj,
1447 struct ubus_request_data *req, const char *method,
1448 struct blob_attr *msg)
1456 blob_buf_init(&buf, 0);
1457 c = blobmsg_open_array(&buf, "entries");
1459 if ((f = fopen("/proc/net/nf_conntrack", "r")) != NULL)
1461 while (fgets(line, sizeof(line) - 1, f))
1463 d = blobmsg_open_table(&buf, NULL);
1464 memset(seen, 0, sizeof(seen));
1466 for (i = 0, p = strtok(line, " "); p; i++, p = strtok(NULL, " "))
1469 blobmsg_add_u8(&buf, "ipv6", !strcmp(p, "ipv6"));
1471 blobmsg_add_u32(&buf, "protocol", atoi(p));
1473 blobmsg_add_u32(&buf, "expires", atoi(p));
1479 if (!seen[0] && !strncmp(p, "src=", 4))
1481 blobmsg_add_string(&buf, "src", p + 4);
1484 else if (!seen[1] && !strncmp(p, "dst=", 4))
1486 blobmsg_add_string(&buf, "dest", p + 4);
1489 else if (!seen[2] && !strncmp(p, "sport=", 6))
1491 blobmsg_add_u32(&buf, "sport", atoi(p + 6));
1494 else if (!seen[3] && !strncmp(p, "dport=", 6))
1496 blobmsg_add_u32(&buf, "dport", atoi(p + 6));
1499 else if (!strncmp(p, "packets=", 8))
1501 blobmsg_add_u32(&buf,
1502 seen[4] ? "tx_packets" : "rx_packets",
1506 else if (!strncmp(p, "bytes=", 6))
1508 blobmsg_add_u32(&buf,
1509 seen[5] ? "tx_bytes" : "rx_bytes",
1516 blobmsg_close_table(&buf, d);
1522 blobmsg_close_array(&buf, c);
1523 ubus_send_reply(ctx, req, buf.head);
1529 rpc_luci2_network_arp_table(struct ubus_context *ctx, struct ubus_object *obj,
1530 struct ubus_request_data *req, const char *method,
1531 struct blob_attr *msg)
1535 char *addr, *mac, *dev, line[128];
1537 blob_buf_init(&buf, 0);
1538 c = blobmsg_open_array(&buf, "entries");
1540 if ((f = fopen("/proc/net/arp", "r")) != NULL)
1542 /* skip header line */
1543 fgets(line, sizeof(line) - 1, f);
1545 while (fgets(line, sizeof(line) - 1, f))
1547 addr = strtok(line, " \t");
1549 strtok(NULL, " \t"); /* HW type */
1550 strtok(NULL, " \t"); /* Flags */
1552 mac = strtok(NULL, " \t");
1554 strtok(NULL, " \t"); /* Mask */
1556 dev = strtok(NULL, " \t\n");
1561 d = blobmsg_open_table(&buf, NULL);
1562 blobmsg_add_string(&buf, "ipaddr", addr);
1563 blobmsg_add_string(&buf, "macaddr", mac);
1564 blobmsg_add_string(&buf, "device", dev);
1565 blobmsg_close_table(&buf, d);
1571 blobmsg_close_array(&buf, c);
1572 ubus_send_reply(ctx, req, buf.head);
1578 put_hexaddr(const char *name, const char *s, const char *m)
1582 char as[sizeof("255.255.255.255/32\0")];
1584 a.s_addr = strtoul(s, NULL, 16);
1585 inet_ntop(AF_INET, &a, as, sizeof(as));
1589 for (a.s_addr = ntohl(strtoul(m, NULL, 16)), bits = 0;
1590 a.s_addr & 0x80000000;
1594 sprintf(as + strlen(as), "/%u", bits);
1597 blobmsg_add_string(&buf, name, as);
1601 rpc_luci2_network_routes(struct ubus_context *ctx, struct ubus_object *obj,
1602 struct ubus_request_data *req, const char *method,
1603 struct blob_attr *msg)
1607 char *dst, *dmask, *next, *metric, *device;
1611 if (!(routes = fopen("/proc/net/route", "r")))
1612 return rpc_errno_status();
1614 blob_buf_init(&buf, 0);
1615 c = blobmsg_open_array(&buf, "routes");
1617 /* skip header line */
1618 fgets(line, sizeof(line) - 1, routes);
1620 while (fgets(line, sizeof(line) - 1, routes))
1622 device = strtok(line, "\t ");
1623 dst = strtok(NULL, "\t ");
1624 next = strtok(NULL, "\t ");
1626 strtok(NULL, "\t "); /* flags */
1627 strtok(NULL, "\t "); /* refcount */
1628 strtok(NULL, "\t "); /* usecount */
1630 metric = strtok(NULL, "\t ");
1631 dmask = strtok(NULL, "\t ");
1636 d = blobmsg_open_table(&buf, NULL);
1638 put_hexaddr("target", dst, dmask);
1639 put_hexaddr("nexthop", next, NULL);
1641 n = strtoul(metric, NULL, 10);
1642 blobmsg_add_u32(&buf, "metric", n);
1644 blobmsg_add_string(&buf, "device", device);
1646 blobmsg_close_table(&buf, d);
1649 blobmsg_close_array(&buf, c);
1652 ubus_send_reply(ctx, req, buf.head);
1657 put_hex6addr(const char *name, const char *s, const char *m)
1661 char as[INET6_ADDRSTRLEN + sizeof("/128")];
1664 (((x) <= '9') ? ((x) - '0') : \
1665 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1668 for (i = 0; i < 16; i++, s += 2)
1669 a.s6_addr[i] = (16 * hex(*s)) + hex(*(s+1));
1671 inet_ntop(AF_INET6, &a, as, sizeof(as));
1674 sprintf(as + strlen(as), "/%lu", strtoul(m, NULL, 16));
1676 blobmsg_add_string(&buf, name, as);
1680 rpc_luci2_network_routes6(struct ubus_context *ctx, struct ubus_object *obj,
1681 struct ubus_request_data *req, const char *method,
1682 struct blob_attr *msg)
1686 char *src, *smask, *dst, *dmask, *next, *metric, *flags, *device;
1690 if (!(routes = fopen("/proc/net/ipv6_route", "r")))
1691 return rpc_errno_status();
1693 blob_buf_init(&buf, 0);
1694 c = blobmsg_open_array(&buf, "routes");
1696 while (fgets(line, sizeof(line) - 1, routes))
1698 dst = strtok(line, " ");
1699 dmask = strtok(NULL, " ");
1700 src = strtok(NULL, " ");
1701 smask = strtok(NULL, " ");
1702 next = strtok(NULL, " ");
1703 metric = strtok(NULL, " ");
1705 strtok(NULL, " "); /* refcount */
1706 strtok(NULL, " "); /* usecount */
1708 flags = strtok(NULL, " ");
1709 device = strtok(NULL, " \n");
1714 n = strtoul(flags, NULL, 16);
1719 d = blobmsg_open_table(&buf, NULL);
1721 put_hex6addr("target", dst, dmask);
1722 put_hex6addr("source", src, smask);
1723 put_hex6addr("nexthop", next, NULL);
1725 n = strtoul(metric, NULL, 16);
1726 blobmsg_add_u32(&buf, "metric", n);
1728 blobmsg_add_string(&buf, "device", device);
1730 blobmsg_close_table(&buf, d);
1733 blobmsg_close_array(&buf, c);
1736 ubus_send_reply(ctx, req, buf.head);
1752 opkg_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1754 struct opkg_state *s = priv;
1757 char *nl = strchr(buf, '\n');
1758 char *name = NULL, *vers = NULL, *desc = NULL;
1766 if (s->cur_offset++ < s->req_offset)
1769 if (s->cur_count++ >= s->req_count)
1775 s->array = blobmsg_open_array(blob, "packages");
1778 for (ptr = buf, last = buf, *nl = 0; ptr <= nl; ptr++)
1780 if (!*ptr || (*ptr == ' ' && *(ptr+1) == '-' && *(ptr+2) == ' '))
1792 desc = *ptr ? (ptr + 3) : NULL;
1801 c = blobmsg_open_array(blob, NULL);
1803 blobmsg_add_string(blob, NULL, name);
1804 blobmsg_add_string(blob, NULL, vers);
1807 blobmsg_add_string(blob, NULL, desc);
1809 blobmsg_close_array(blob, c);
1813 return (nl - buf + 1);
1817 opkg_finish_list(struct blob_buf *blob, int status, void *priv)
1819 struct opkg_state *s = priv;
1822 return UBUS_STATUS_NO_DATA;
1824 blobmsg_close_array(blob, s->array);
1825 blobmsg_add_u32(blob, "total", s->total);
1827 return UBUS_STATUS_OK;
1831 opkg_exec_list(const char *action, struct blob_attr *msg,
1832 struct ubus_context *ctx, struct ubus_request_data *req)
1834 struct opkg_state *state = NULL;
1835 struct blob_attr *tb[__RPC_OM_MAX];
1836 const char *cmd[5] = { "opkg", action, "-nocase", NULL, NULL };
1838 blobmsg_parse(rpc_opkg_match_policy, __RPC_OM_MAX, tb,
1839 blob_data(msg), blob_len(msg));
1841 state = malloc(sizeof(*state));
1844 return UBUS_STATUS_UNKNOWN_ERROR;
1846 memset(state, 0, sizeof(*state));
1848 if (tb[RPC_OM_PATTERN])
1849 cmd[3] = blobmsg_data(tb[RPC_OM_PATTERN]);
1851 if (tb[RPC_OM_LIMIT])
1852 state->req_count = blobmsg_get_u32(tb[RPC_OM_LIMIT]);
1854 if (tb[RPC_OM_OFFSET])
1855 state->req_offset = blobmsg_get_u32(tb[RPC_OM_OFFSET]);
1857 if (state->req_offset < 0)
1858 state->req_offset = 0;
1860 if (state->req_count <= 0 || state->req_count > 100)
1861 state->req_count = 100;
1863 return ops->exec(cmd, NULL, opkg_parse_list, NULL, opkg_finish_list,
1869 rpc_luci2_opkg_list(struct ubus_context *ctx, struct ubus_object *obj,
1870 struct ubus_request_data *req, const char *method,
1871 struct blob_attr *msg)
1873 return opkg_exec_list("list", msg, ctx, req);
1877 rpc_luci2_opkg_list_installed(struct ubus_context *ctx, struct ubus_object *obj,
1878 struct ubus_request_data *req, const char *method,
1879 struct blob_attr *msg)
1881 return opkg_exec_list("list-installed", msg, ctx, req);
1885 rpc_luci2_opkg_find(struct ubus_context *ctx, struct ubus_object *obj,
1886 struct ubus_request_data *req, const char *method,
1887 struct blob_attr *msg)
1889 return opkg_exec_list("find", msg, ctx, req);
1893 rpc_luci2_opkg_update(struct ubus_context *ctx, struct ubus_object *obj,
1894 struct ubus_request_data *req, const char *method,
1895 struct blob_attr *msg)
1897 const char *cmd[3] = { "opkg", "update", NULL };
1898 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
1902 rpc_luci2_opkg_install(struct ubus_context *ctx, struct ubus_object *obj,
1903 struct ubus_request_data *req, const char *method,
1904 struct blob_attr *msg)
1906 struct blob_attr *tb[__RPC_OP_MAX];
1907 const char *cmd[5] = { "opkg", "--force-overwrite",
1908 "install", NULL, NULL };
1910 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1911 blob_data(msg), blob_len(msg));
1913 if (!tb[RPC_OP_PACKAGE])
1914 return UBUS_STATUS_INVALID_ARGUMENT;
1916 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1918 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
1922 rpc_luci2_opkg_remove(struct ubus_context *ctx, struct ubus_object *obj,
1923 struct ubus_request_data *req, const char *method,
1924 struct blob_attr *msg)
1926 struct blob_attr *tb[__RPC_OP_MAX];
1927 const char *cmd[5] = { "opkg", "--force-removal-of-dependent-packages",
1928 "remove", NULL, NULL };
1930 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1931 blob_data(msg), blob_len(msg));
1933 if (!tb[RPC_OP_PACKAGE])
1934 return UBUS_STATUS_INVALID_ARGUMENT;
1936 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1938 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
1942 rpc_luci2_opkg_config_get(struct ubus_context *ctx, struct ubus_object *obj,
1943 struct ubus_request_data *req, const char *method,
1944 struct blob_attr *msg)
1947 char conf[2048] = { 0 };
1949 if (!(f = fopen("/etc/opkg.conf", "r")))
1950 return rpc_errno_status();
1952 fread(conf, sizeof(conf) - 1, 1, f);
1955 blob_buf_init(&buf, 0);
1956 blobmsg_add_string(&buf, "config", conf);
1958 ubus_send_reply(ctx, req, buf.head);
1963 rpc_luci2_opkg_config_set(struct ubus_context *ctx, struct ubus_object *obj,
1964 struct ubus_request_data *req, const char *method,
1965 struct blob_attr *msg)
1968 struct blob_attr *tb[__RPC_D_MAX];
1970 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
1971 blob_data(msg), blob_len(msg));
1973 if (!tb[RPC_D_DATA])
1974 return UBUS_STATUS_INVALID_ARGUMENT;
1976 if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048)
1977 return UBUS_STATUS_NOT_SUPPORTED;
1979 if (!(f = fopen("/etc/opkg.conf", "w")))
1980 return rpc_errno_status();
1982 fwrite(blobmsg_data(tb[RPC_D_DATA]),
1983 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
1991 menu_access(struct blob_attr *sid, struct blob_attr *acls, struct blob_buf *e)
1994 struct blob_attr *acl;
1998 c = blobmsg_open_table(e, "write");
2000 blobmsg_for_each_attr(acl, acls, rem)
2002 if (!ops->session_access(blobmsg_data(sid), "access-group",
2003 blobmsg_data(acl), "read"))
2009 blobmsg_add_u8(e, blobmsg_data(acl),
2010 ops->session_access(blobmsg_data(sid), "access-group",
2011 blobmsg_data(acl), "write"));
2014 blobmsg_close_table(e, c);
2020 rpc_luci2_ui_menu(struct ubus_context *ctx, struct ubus_object *obj,
2021 struct ubus_request_data *req, const char *method,
2022 struct blob_attr *msg)
2026 struct blob_buf menu = { 0 };
2027 struct blob_buf item = { 0 };
2028 struct blob_attr *entry, *attr;
2029 struct blob_attr *tb[__RPC_MENU_MAX];
2033 blobmsg_parse(rpc_menu_policy, __RPC_MENU_MAX, tb,
2034 blob_data(msg), blob_len(msg));
2036 if (!tb[RPC_MENU_SESSION])
2037 return UBUS_STATUS_INVALID_ARGUMENT;
2040 blob_buf_init(&buf, 0);
2041 c = blobmsg_open_table(&buf, "menu");
2043 if (!glob(RPC_LUCI2_MENU_FILES, 0, NULL, &gl))
2045 for (i = 0; i < gl.gl_pathc; i++)
2047 blob_buf_init(&menu, 0);
2049 if (!blobmsg_add_json_from_file(&menu, gl.gl_pathv[i]))
2052 blob_for_each_attr(entry, menu.head, rem)
2056 blob_buf_init(&item, 0);
2057 d = blobmsg_open_table(&item, blobmsg_name(entry));
2059 blobmsg_for_each_attr(attr, entry, rem2)
2061 if (blob_id(attr) == BLOBMSG_TYPE_ARRAY &&
2062 !strcmp(blobmsg_name(attr), "acls"))
2063 access = menu_access(tb[RPC_MENU_SESSION], attr, &item);
2065 blobmsg_add_blob(&item, attr);
2068 blobmsg_close_table(&item, d);
2071 blob_for_each_attr(attr, item.head, rem2)
2072 blobmsg_add_blob(&buf, attr);
2074 blob_buf_free(&item);
2078 blob_buf_free(&menu);
2084 blobmsg_close_table(&buf, c);
2086 ubus_send_reply(ctx, req, buf.head);
2092 parse_acl_file(struct blob_buf *acls, const char *path)
2094 struct blob_buf acl = { 0 };
2095 struct blob_attr *cur;
2099 blob_buf_init(&acl, 0);
2101 if (blobmsg_add_json_from_file(&acl, path))
2103 c = blobmsg_open_table(acls, NULL);
2105 blob_for_each_attr(cur, acl.head, rem)
2106 blobmsg_add_blob(acls, cur);
2108 blobmsg_close_table(acls, c);
2111 blob_buf_free(&acl);
2115 rpc_luci2_ui_acls(struct ubus_context *ctx, struct ubus_object *obj,
2116 struct ubus_request_data *req, const char *method,
2117 struct blob_attr *msg)
2123 if (glob(RPC_SESSION_ACL_DIR "/*.json", 0, NULL, &gl))
2124 return rpc_errno_status();
2126 blob_buf_init(&buf, 0);
2127 c = blobmsg_open_array(&buf, "acls");
2129 for (i = 0; i < gl.gl_pathc; i++)
2130 parse_acl_file(&buf, gl.gl_pathv[i]);
2133 blobmsg_close_array(&buf, c);
2135 ubus_send_reply(ctx, req, buf.head);
2141 rpc_luci2_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
2145 static const struct ubus_method luci2_system_methods[] = {
2146 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log),
2147 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg),
2148 UBUS_METHOD_NOARG("diskfree", rpc_luci2_system_diskfree),
2149 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list),
2150 UBUS_METHOD("process_signal", rpc_luci2_process_signal,
2152 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list),
2153 UBUS_METHOD("init_action", rpc_luci2_init_action,
2155 UBUS_METHOD_NOARG("rclocal_get", rpc_luci2_rclocal_get),
2156 UBUS_METHOD("rclocal_set", rpc_luci2_rclocal_set,
2158 UBUS_METHOD_NOARG("crontab_get", rpc_luci2_crontab_get),
2159 UBUS_METHOD("crontab_set", rpc_luci2_crontab_set,
2161 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get),
2162 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set,
2164 UBUS_METHOD("password_set", rpc_luci2_password_set,
2165 rpc_password_policy),
2166 UBUS_METHOD_NOARG("led_list", rpc_luci2_led_list),
2167 UBUS_METHOD_NOARG("usb_list", rpc_luci2_usb_list),
2168 UBUS_METHOD_NOARG("upgrade_test", rpc_luci2_upgrade_test),
2169 UBUS_METHOD("upgrade_start", rpc_luci2_upgrade_start,
2170 rpc_upgrade_policy),
2171 UBUS_METHOD_NOARG("upgrade_clean", rpc_luci2_upgrade_clean),
2172 UBUS_METHOD_NOARG("backup_restore", rpc_luci2_backup_restore),
2173 UBUS_METHOD_NOARG("backup_clean", rpc_luci2_backup_clean),
2174 UBUS_METHOD_NOARG("backup_config_get", rpc_luci2_backup_config_get),
2175 UBUS_METHOD("backup_config_set", rpc_luci2_backup_config_set,
2177 UBUS_METHOD_NOARG("backup_list", rpc_luci2_backup_list),
2178 UBUS_METHOD_NOARG("reset_test", rpc_luci2_reset_test),
2179 UBUS_METHOD_NOARG("reset_start", rpc_luci2_reset_start),
2180 UBUS_METHOD_NOARG("reboot", rpc_luci2_reboot)
2183 static struct ubus_object_type luci2_system_type =
2184 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods);
2186 static struct ubus_object system_obj = {
2187 .name = "luci2.system",
2188 .type = &luci2_system_type,
2189 .methods = luci2_system_methods,
2190 .n_methods = ARRAY_SIZE(luci2_system_methods),
2194 static const struct ubus_method luci2_network_methods[] = {
2195 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count),
2196 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table),
2197 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table),
2198 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases),
2199 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6),
2200 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes),
2201 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6),
2204 static struct ubus_object_type luci2_network_type =
2205 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods);
2207 static struct ubus_object network_obj = {
2208 .name = "luci2.network",
2209 .type = &luci2_network_type,
2210 .methods = luci2_network_methods,
2211 .n_methods = ARRAY_SIZE(luci2_network_methods),
2215 static const struct ubus_method luci2_opkg_methods[] = {
2216 UBUS_METHOD("list", rpc_luci2_opkg_list,
2217 rpc_opkg_match_policy),
2218 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed,
2219 rpc_opkg_match_policy),
2220 UBUS_METHOD("find", rpc_luci2_opkg_find,
2221 rpc_opkg_match_policy),
2222 UBUS_METHOD("install", rpc_luci2_opkg_install,
2223 rpc_opkg_package_policy),
2224 UBUS_METHOD("remove", rpc_luci2_opkg_remove,
2225 rpc_opkg_package_policy),
2226 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update),
2227 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get),
2228 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set,
2232 static struct ubus_object_type luci2_opkg_type =
2233 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods);
2235 static struct ubus_object opkg_obj = {
2236 .name = "luci2.opkg",
2237 .type = &luci2_opkg_type,
2238 .methods = luci2_opkg_methods,
2239 .n_methods = ARRAY_SIZE(luci2_opkg_methods),
2243 static const struct ubus_method luci2_ui_methods[] = {
2244 UBUS_METHOD_NOARG("menu", rpc_luci2_ui_menu),
2245 UBUS_METHOD_NOARG("acls", rpc_luci2_ui_acls)
2248 static struct ubus_object_type luci2_ui_type =
2249 UBUS_OBJECT_TYPE("luci-rpc-luci2-ui", luci2_ui_methods);
2251 static struct ubus_object ui_obj = {
2253 .type = &luci2_ui_type,
2254 .methods = luci2_ui_methods,
2255 .n_methods = ARRAY_SIZE(luci2_ui_methods),
2258 cursor = uci_alloc_context();
2261 return UBUS_STATUS_UNKNOWN_ERROR;
2265 rv |= ubus_add_object(ctx, &system_obj);
2266 rv |= ubus_add_object(ctx, &network_obj);
2267 rv |= ubus_add_object(ctx, &opkg_obj);
2268 rv |= ubus_add_object(ctx, &ui_obj);
2273 const struct rpc_plugin rpc_plugin = {
2274 .init = rpc_luci2_api_init