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.
19 #define _GNU_SOURCE /* crypt() */
29 #include <sys/types.h>
30 #include <sys/statvfs.h>
32 #include <arpa/inet.h>
35 #include <libubox/blobmsg_json.h>
36 #include <libubox/avl-cmp.h>
40 #include <rpcd/plugin.h>
42 /* limit of log size buffer */
43 #define RPC_LUCI2_MAX_LOGSIZE (128 * 1024)
44 #define RPC_LUCI2_DEF_LOGSIZE (16 * 1024)
46 /* location of menu definitions */
47 #define RPC_LUCI2_MENU_FILES "/usr/share/rpcd/menu.d/*.json" /* */
50 static const struct rpc_daemon_ops *ops;
52 static struct blob_buf buf;
53 static struct uci_context *cursor;
61 static const struct blobmsg_policy rpc_signal_policy[__RPC_S_MAX] = {
62 [RPC_S_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
63 [RPC_S_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
72 static const struct blobmsg_policy rpc_init_policy[__RPC_I_MAX] = {
73 [RPC_I_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
74 [RPC_I_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
82 static const struct blobmsg_policy rpc_data_policy[__RPC_D_MAX] = {
83 [RPC_D_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
91 static const struct blobmsg_policy rpc_sshkey_policy[__RPC_K_MAX] = {
92 [RPC_K_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
101 static const struct blobmsg_policy rpc_password_policy[__RPC_P_MAX] = {
102 [RPC_P_USER] = { .name = "user", .type = BLOBMSG_TYPE_STRING },
103 [RPC_P_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
113 static const struct blobmsg_policy rpc_opkg_match_policy[__RPC_OM_MAX] = {
114 [RPC_OM_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
115 [RPC_OM_OFFSET] = { .name = "offset", .type = BLOBMSG_TYPE_INT32 },
116 [RPC_OM_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
124 static const struct blobmsg_policy rpc_opkg_package_policy[__RPC_OP_MAX] = {
125 [RPC_OP_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
133 static const struct blobmsg_policy rpc_upgrade_policy[__RPC_UPGRADE_MAX] = {
134 [RPC_UPGRADE_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
142 static const struct blobmsg_policy rpc_menu_policy[__RPC_MENU_MAX] = {
143 [RPC_MENU_SESSION] = { .name = "ubus_rpc_session",
144 .type = BLOBMSG_TYPE_STRING },
152 static const struct blobmsg_policy rpc_switch_policy[__RPC_SWITCH_MAX] = {
153 [RPC_SWITCH_NAME] = { .name = "switch", .type = BLOBMSG_TYPE_STRING },
158 rpc_errno_status(void)
163 return UBUS_STATUS_PERMISSION_DENIED;
166 return UBUS_STATUS_INVALID_ARGUMENT;
169 return UBUS_STATUS_NOT_FOUND;
172 return UBUS_STATUS_INVALID_ARGUMENT;
175 return UBUS_STATUS_UNKNOWN_ERROR;
180 log_read(FILE *log, int logsize)
186 logsize = RPC_LUCI2_DEF_LOGSIZE;
188 len = (logsize > RPC_LUCI2_MAX_LOGSIZE) ? RPC_LUCI2_MAX_LOGSIZE : logsize;
189 logbuf = blobmsg_alloc_string_buffer(&buf, "log", len + 1);
194 while (logsize > RPC_LUCI2_MAX_LOGSIZE)
196 len = logsize % RPC_LUCI2_MAX_LOGSIZE;
199 len = RPC_LUCI2_MAX_LOGSIZE;
201 fread(logbuf, 1, len, log);
205 len = fread(logbuf, 1, logsize, log);
208 blobmsg_add_string_buffer(&buf);
212 rpc_luci2_system_log(struct ubus_context *ctx, struct ubus_object *obj,
213 struct ubus_request_data *req, const char *method,
214 struct blob_attr *msg)
218 const char *logfile = NULL;
220 struct uci_package *p;
221 struct uci_element *e;
222 struct uci_section *s;
223 struct uci_ptr ptr = { .package = "system" };
225 uci_load(cursor, ptr.package, &p);
228 return UBUS_STATUS_NOT_FOUND;
230 uci_foreach_element(&p->sections, e)
232 s = uci_to_section(e);
234 if (strcmp(s->type, "system"))
238 ptr.option = "log_type";
239 ptr.section = e->name;
240 uci_lookup_ptr(cursor, &ptr, NULL, true);
244 if (ptr.o && ptr.o->type == UCI_TYPE_STRING &&
245 !strcmp(ptr.o->v.string, "file"))
248 ptr.option = "log_file";
249 uci_lookup_ptr(cursor, &ptr, NULL, true);
251 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
252 logfile = ptr.o->v.string;
254 logfile = "/var/log/messages";
256 if (stat(logfile, &st) || !(log = fopen(logfile, "r")))
259 logsize = st.st_size;
264 ptr.option = "log_size";
265 uci_lookup_ptr(cursor, &ptr, NULL, true);
267 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
268 logsize = atoi(ptr.o->v.string) * 1024;
270 if (!(log = popen("logread", "r")))
274 blob_buf_init(&buf, 0);
276 log_read(log, logsize);
279 uci_unload(cursor, p);
280 ubus_send_reply(ctx, req, buf.head);
284 uci_unload(cursor, p);
285 return rpc_errno_status();
289 rpc_luci2_system_dmesg(struct ubus_context *ctx, struct ubus_object *obj,
290 struct ubus_request_data *req, const char *method,
291 struct blob_attr *msg)
295 if (!(log = popen("dmesg", "r")))
296 return rpc_errno_status();
298 blob_buf_init(&buf, 0);
300 log_read(log, RPC_LUCI2_MAX_LOGSIZE);
303 ubus_send_reply(ctx, req, buf.head);
308 rpc_luci2_system_diskfree(struct ubus_context *ctx, struct ubus_object *obj,
309 struct ubus_request_data *req, const char *method,
310 struct blob_attr *msg)
315 const char *fslist[] = {
320 blob_buf_init(&buf, 0);
322 for (i = 0; i < sizeof(fslist) / sizeof(fslist[0]); i += 2)
324 if (statvfs(fslist[i], &s))
327 c = blobmsg_open_table(&buf, fslist[i+1]);
329 blobmsg_add_u32(&buf, "total", s.f_blocks * s.f_frsize);
330 blobmsg_add_u32(&buf, "free", s.f_bfree * s.f_frsize);
331 blobmsg_add_u32(&buf, "used", (s.f_blocks - s.f_bfree) * s.f_frsize);
333 blobmsg_close_table(&buf, c);
336 ubus_send_reply(ctx, req, buf.head);
341 rpc_luci2_process_list(struct ubus_context *ctx, struct ubus_object *obj,
342 struct ubus_request_data *req, const char *method,
343 struct blob_attr *msg)
348 char *pid, *ppid, *user, *stat, *vsz, *pvsz, *pcpu, *cmd;
350 if (!(top = popen("/bin/busybox top -bn1", "r")))
351 return rpc_errno_status();
353 blob_buf_init(&buf, 0);
354 c = blobmsg_open_array(&buf, "processes");
356 while (fgets(line, sizeof(line) - 1, top))
358 pid = strtok(line, " ");
360 if (*pid < '0' || *pid > '9')
363 ppid = strtok(NULL, " ");
364 user = strtok(NULL, " ");
365 stat = strtok(NULL, " ");
378 vsz = strtok(stat + 4, " ");
379 pvsz = strtok(NULL, " ");
380 pcpu = strtok(NULL, " ");
381 cmd = strtok(NULL, "\n");
386 d = blobmsg_open_table(&buf, NULL);
388 blobmsg_add_u32(&buf, "pid", atoi(pid));
389 blobmsg_add_u32(&buf, "ppid", atoi(ppid));
390 blobmsg_add_string(&buf, "user", user);
391 blobmsg_add_string(&buf, "stat", stat);
392 blobmsg_add_u32(&buf, "vsize", atoi(vsz) * 1024);
393 blobmsg_add_u32(&buf, "vsize_percent", atoi(pvsz));
394 blobmsg_add_u32(&buf, "cpu_percent", atoi(pcpu));
395 blobmsg_add_string(&buf, "command", cmd);
397 blobmsg_close_table(&buf, d);
401 blobmsg_close_array(&buf, c);
403 ubus_send_reply(ctx, req, buf.head);
408 rpc_luci2_process_signal(struct ubus_context *ctx, struct ubus_object *obj,
409 struct ubus_request_data *req, const char *method,
410 struct blob_attr *msg)
413 struct blob_attr *tb[__RPC_S_MAX];
415 blobmsg_parse(rpc_signal_policy, __RPC_S_MAX, tb,
416 blob_data(msg), blob_len(msg));
418 if (!tb[RPC_S_SIGNAL] || !tb[RPC_S_PID])
421 return rpc_errno_status();
424 pid = blobmsg_get_u32(tb[RPC_S_PID]);
425 sig = blobmsg_get_u32(tb[RPC_S_SIGNAL]);
428 return rpc_errno_status();
434 rpc_luci2_init_list(struct ubus_context *ctx, struct ubus_object *obj,
435 struct ubus_request_data *req, const char *method,
436 struct blob_attr *msg)
440 char *p, path[PATH_MAX];
446 if (!(d = opendir("/etc/init.d")))
447 return rpc_errno_status();
449 blob_buf_init(&buf, 0);
450 c = blobmsg_open_array(&buf, "initscripts");
452 while ((e = readdir(d)) != NULL)
454 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s", e->d_name);
456 if (stat(path, &s) || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR))
459 if ((f = fopen(path, "r")) != NULL)
462 p = fgets(path, sizeof(path) - 1, f);
464 if (!p || !strstr(p, "/etc/rc.common"))
467 t = blobmsg_open_table(&buf, NULL);
469 blobmsg_add_string(&buf, "name", e->d_name);
471 while (fgets(path, sizeof(path) - 1, f))
473 p = strtok(path, "= \t");
475 if (!strcmp(p, "START") && !!(p = strtok(NULL, "= \t\n")))
478 blobmsg_add_u32(&buf, "start", n);
480 else if (!strcmp(p, "STOP") && !!(p = strtok(NULL, "= \t\n")))
482 blobmsg_add_u32(&buf, "stop", atoi(p));
489 snprintf(path, sizeof(path) - 1, "/etc/rc.d/S%02d%s",
492 blobmsg_add_u8(&buf, "enabled",
493 (!stat(path, &s) && (s.st_mode & S_IXUSR)));
497 blobmsg_add_u8(&buf, "enabled", 0);
500 blobmsg_close_table(&buf, t);
508 blobmsg_close_array(&buf, c);
510 ubus_send_reply(ctx, req, buf.head);
515 rpc_luci2_init_action(struct ubus_context *ctx, struct ubus_object *obj,
516 struct ubus_request_data *req, const char *method,
517 struct blob_attr *msg)
524 struct blob_attr *tb[__RPC_I_MAX];
526 blobmsg_parse(rpc_init_policy, __RPC_I_MAX, tb,
527 blob_data(msg), blob_len(msg));
529 if (!tb[RPC_I_NAME] || !tb[RPC_I_ACTION])
530 return UBUS_STATUS_INVALID_ARGUMENT;
532 action = blobmsg_data(tb[RPC_I_ACTION]);
534 if (strcmp(action, "start") && strcmp(action, "stop") &&
535 strcmp(action, "reload") && strcmp(action, "restart") &&
536 strcmp(action, "enable") && strcmp(action, "disable"))
537 return UBUS_STATUS_INVALID_ARGUMENT;
539 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s",
540 (char *)blobmsg_data(tb[RPC_I_NAME]));
543 return rpc_errno_status();
545 if (!(s.st_mode & S_IXUSR))
546 return UBUS_STATUS_PERMISSION_DENIED;
548 switch ((pid = fork()))
551 return rpc_errno_status();
556 if ((fd = open("/dev/null", O_RDWR)) > -1)
567 if (execl(path, path, action, NULL))
568 return rpc_errno_status();
576 rpc_luci2_rclocal_get(struct ubus_context *ctx, struct ubus_object *obj,
577 struct ubus_request_data *req, const char *method,
578 struct blob_attr *msg)
581 char data[4096] = { 0 };
583 if (!(f = fopen("/etc/rc.local", "r")))
584 return rpc_errno_status();
586 fread(data, sizeof(data) - 1, 1, f);
589 blob_buf_init(&buf, 0);
590 blobmsg_add_string(&buf, "data", data);
592 ubus_send_reply(ctx, req, buf.head);
597 rpc_luci2_rclocal_set(struct ubus_context *ctx, struct ubus_object *obj,
598 struct ubus_request_data *req, const char *method,
599 struct blob_attr *msg)
602 struct blob_attr *tb[__RPC_D_MAX];
604 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
605 blob_data(msg), blob_len(msg));
607 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 4096)
608 return UBUS_STATUS_INVALID_ARGUMENT;
610 if (!(f = fopen("/etc/rc.local", "w")))
611 return rpc_errno_status();
613 fwrite(blobmsg_data(tb[RPC_D_DATA]),
614 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
621 rpc_luci2_crontab_get(struct ubus_context *ctx, struct ubus_object *obj,
622 struct ubus_request_data *req, const char *method,
623 struct blob_attr *msg)
626 char data[4096] = { 0 };
628 if (!(f = fopen("/etc/crontabs/root", "r")))
629 return rpc_errno_status();
631 fread(data, sizeof(data) - 1, 1, f);
634 blob_buf_init(&buf, 0);
635 blobmsg_add_string(&buf, "data", data);
637 ubus_send_reply(ctx, req, buf.head);
642 rpc_luci2_crontab_set(struct ubus_context *ctx, struct ubus_object *obj,
643 struct ubus_request_data *req, const char *method,
644 struct blob_attr *msg)
648 struct blob_attr *tb[__RPC_D_MAX];
650 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
651 blob_data(msg), blob_len(msg));
653 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 4096)
654 return UBUS_STATUS_INVALID_ARGUMENT;
656 if (stat("/etc/crontabs", &s) && mkdir("/etc/crontabs", 0755))
657 return rpc_errno_status();
659 if (!(f = fopen("/etc/crontabs/root", "w")))
660 return rpc_errno_status();
662 fwrite(blobmsg_data(tb[RPC_D_DATA]),
663 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
670 rpc_luci2_sshkeys_get(struct ubus_context *ctx, struct ubus_object *obj,
671 struct ubus_request_data *req, const char *method,
672 struct blob_attr *msg)
678 if (!(f = fopen("/etc/dropbear/authorized_keys", "r")))
679 return rpc_errno_status();
681 blob_buf_init(&buf, 0);
682 c = blobmsg_open_array(&buf, "keys");
684 while (fgets(line, sizeof(line) - 1, f))
686 for (p = line + strlen(line) - 1; (p > line) && isspace(*p); p--)
689 for (p = line; isspace(*p); p++)
693 blobmsg_add_string(&buf, NULL, p);
696 blobmsg_close_array(&buf, c);
699 ubus_send_reply(ctx, req, buf.head);
704 rpc_luci2_sshkeys_set(struct ubus_context *ctx, struct ubus_object *obj,
705 struct ubus_request_data *req, const char *method,
706 struct blob_attr *msg)
710 struct blob_attr *cur, *tb[__RPC_K_MAX];
712 blobmsg_parse(rpc_sshkey_policy, __RPC_K_MAX, tb,
713 blob_data(msg), blob_len(msg));
716 return UBUS_STATUS_INVALID_ARGUMENT;
718 if (!(f = fopen("/etc/dropbear/authorized_keys", "w")))
719 return rpc_errno_status();
721 blobmsg_for_each_attr(cur, tb[RPC_K_KEYS], rem)
723 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
726 fwrite(blobmsg_data(cur), blobmsg_data_len(cur) - 1, 1, f);
727 fwrite("\n", 1, 1, f);
735 rpc_luci2_password_set(struct ubus_context *ctx, struct ubus_object *obj,
736 struct ubus_request_data *req, const char *method,
737 struct blob_attr *msg)
742 struct blob_attr *tb[__RPC_P_MAX];
744 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
745 blob_data(msg), blob_len(msg));
747 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
748 return UBUS_STATUS_INVALID_ARGUMENT;
750 if (stat("/usr/bin/passwd", &s))
751 return UBUS_STATUS_NOT_FOUND;
753 if (!(s.st_mode & S_IXUSR))
754 return UBUS_STATUS_PERMISSION_DENIED;
757 return rpc_errno_status();
759 switch ((pid = fork()))
764 return rpc_errno_status();
773 if ((fd = open("/dev/null", O_RDWR)) > -1)
782 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
783 blobmsg_data(tb[RPC_P_USER]), NULL))
784 return rpc_errno_status();
789 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
790 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
791 write(fds[1], "\n", 1);
795 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
796 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
797 write(fds[1], "\n", 1);
801 waitpid(pid, NULL, 0);
808 rpc_luci2_led_list(struct ubus_context *ctx, struct ubus_object *obj,
809 struct ubus_request_data *req, const char *method,
810 struct blob_attr *msg)
814 void *list, *led, *trigger;
815 char *p, *active_trigger, line[512];
818 if (!(d = opendir("/sys/class/leds")))
819 return rpc_errno_status();
821 blob_buf_init(&buf, 0);
822 list = blobmsg_open_array(&buf, "leds");
824 while ((e = readdir(d)) != NULL)
826 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/trigger",
829 if (!(f = fopen(line, "r")))
832 led = blobmsg_open_table(&buf, NULL);
834 blobmsg_add_string(&buf, "name", e->d_name);
836 if (fgets(line, sizeof(line) - 1, f))
838 trigger = blobmsg_open_array(&buf, "triggers");
840 for (p = strtok(line, " \n"), active_trigger = NULL;
842 p = strtok(NULL, " \n"))
846 *(p + strlen(p) - 1) = 0;
851 blobmsg_add_string(&buf, NULL, p);
854 blobmsg_close_array(&buf, trigger);
857 blobmsg_add_string(&buf, "active_trigger", active_trigger);
862 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/brightness",
865 if ((f = fopen(line, "r")) != NULL)
867 if (fgets(line, sizeof(line) - 1, f))
868 blobmsg_add_u32(&buf, "brightness", atoi(line));
873 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/max_brightness",
876 if ((f = fopen(line, "r")) != NULL)
878 if (fgets(line, sizeof(line) - 1, f))
879 blobmsg_add_u32(&buf, "max_brightness", atoi(line));
884 blobmsg_close_table(&buf, led);
889 blobmsg_close_array(&buf, list);
890 ubus_send_reply(ctx, req, buf.head);
896 rpc_luci2_usb_list(struct ubus_context *ctx, struct ubus_object *obj,
897 struct ubus_request_data *req, const char *method,
898 struct blob_attr *msg)
908 const char *attributes[] = {
909 "manufacturer", "vendor_name", "s",
910 "product", "product_name", "s",
911 "idVendor", "vendor_id", "x",
912 "idProduct", "product_id", "x",
913 "serial", "serial", "s",
914 "speed", "speed", "d",
917 if (!(d = opendir("/sys/bus/usb/devices")))
918 return rpc_errno_status();
920 blob_buf_init(&buf, 0);
921 list = blobmsg_open_array(&buf, "devices");
923 while ((e = readdir(d)) != NULL)
925 if (e->d_name[0] < '0' || e->d_name[0] > '9')
928 snprintf(line, sizeof(line) - 1,
929 "/sys/bus/usb/devices/%s/%s", e->d_name, attributes[0]);
934 device = blobmsg_open_table(&buf, NULL);
936 blobmsg_add_string(&buf, "name", e->d_name);
938 for (i = 0; i < sizeof(attributes) / sizeof(attributes[0]); i += 3)
940 snprintf(line, sizeof(line) - 1,
941 "/sys/bus/usb/devices/%s/%s", e->d_name, attributes[i]);
943 if (!(f = fopen(line, "r")))
946 if (fgets(line, sizeof(line) - 1, f))
948 switch (*attributes[i+2])
951 blobmsg_add_u32(&buf, attributes[i+1],
952 strtoul(line, NULL, 16));
956 blobmsg_add_u32(&buf, attributes[i+1],
957 strtoul(line, NULL, 10));
961 if ((p = strchr(line, '\n')) != NULL)
962 while (p > line && isspace(*p))
965 blobmsg_add_string(&buf, attributes[i+1], line);
973 blobmsg_close_table(&buf, device);
978 blobmsg_close_array(&buf, list);
979 ubus_send_reply(ctx, req, buf.head);
985 rpc_luci2_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
986 struct ubus_request_data *req, const char *method,
987 struct blob_attr *msg)
989 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
990 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
994 rpc_luci2_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
995 struct ubus_request_data *req, const char *method,
996 struct blob_attr *msg)
1002 rpc_luci2_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
1003 struct ubus_request_data *req, const char *method,
1004 struct blob_attr *msg)
1006 if (unlink("/tmp/firmware.bin"))
1007 return rpc_errno_status();
1013 rpc_luci2_backup_restore(struct ubus_context *ctx, struct ubus_object *obj,
1014 struct ubus_request_data *req, const char *method,
1015 struct blob_attr *msg)
1017 const char *cmd[4] = { "sysupgrade", "--restore-backup",
1018 "/tmp/backup.tar.gz", NULL };
1020 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
1024 rpc_luci2_backup_clean(struct ubus_context *ctx, struct ubus_object *obj,
1025 struct ubus_request_data *req, const char *method,
1026 struct blob_attr *msg)
1028 if (unlink("/tmp/backup.tar.gz"))
1029 return rpc_errno_status();
1035 rpc_luci2_backup_config_get(struct ubus_context *ctx, struct ubus_object *obj,
1036 struct ubus_request_data *req, const char *method,
1037 struct blob_attr *msg)
1040 char conf[2048] = { 0 };
1042 if (!(f = fopen("/etc/sysupgrade.conf", "r")))
1043 return rpc_errno_status();
1045 fread(conf, sizeof(conf) - 1, 1, f);
1048 blob_buf_init(&buf, 0);
1049 blobmsg_add_string(&buf, "config", conf);
1051 ubus_send_reply(ctx, req, buf.head);
1056 rpc_luci2_backup_config_set(struct ubus_context *ctx, struct ubus_object *obj,
1057 struct ubus_request_data *req, const char *method,
1058 struct blob_attr *msg)
1061 struct blob_attr *tb[__RPC_D_MAX];
1063 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
1064 blob_data(msg), blob_len(msg));
1066 if (!tb[RPC_D_DATA])
1067 return UBUS_STATUS_INVALID_ARGUMENT;
1069 if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048)
1070 return UBUS_STATUS_NOT_SUPPORTED;
1072 if (!(f = fopen("/etc/sysupgrade.conf", "w")))
1073 return rpc_errno_status();
1075 fwrite(blobmsg_data(tb[RPC_D_DATA]),
1076 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
1082 struct backup_state {
1088 backup_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1090 struct backup_state *s = priv;
1091 char *nl = strchr(buf, '\n');
1099 s->array = blobmsg_open_array(blob, "files");
1103 blobmsg_add_string(blob, NULL, buf);
1105 return (nl - buf + 1);
1109 backup_finish_list(struct blob_buf *blob, int status, void *priv)
1111 struct backup_state *s = priv;
1114 return UBUS_STATUS_NO_DATA;
1116 blobmsg_close_array(blob, s->array);
1118 return UBUS_STATUS_OK;
1122 rpc_luci2_backup_list(struct ubus_context *ctx, struct ubus_object *obj,
1123 struct ubus_request_data *req, const char *method,
1124 struct blob_attr *msg)
1126 struct backup_state *state = NULL;
1127 const char *cmd[3] = { "sysupgrade", "--list-backup", NULL };
1129 state = malloc(sizeof(*state));
1132 return UBUS_STATUS_UNKNOWN_ERROR;
1134 memset(state, 0, sizeof(*state));
1136 return ops->exec(cmd, NULL, backup_parse_list, NULL, backup_finish_list,
1141 rpc_luci2_reset_test(struct ubus_context *ctx, struct ubus_object *obj,
1142 struct ubus_request_data *req, const char *method,
1143 struct blob_attr *msg)
1147 char line[64] = { 0 };
1148 bool supported = false;
1150 if (!stat("/sbin/mtd", &s) && (s.st_mode & S_IXUSR))
1152 if ((mtd = fopen("/proc/mtd", "r")) != NULL)
1154 while (fgets(line, sizeof(line) - 1, mtd))
1156 if (strstr(line, "\"rootfs_data\""))
1167 blob_buf_init(&buf, 0);
1168 blobmsg_add_u8(&buf, "supported", supported);
1170 ubus_send_reply(ctx, req, buf.head);
1176 rpc_luci2_reset_start(struct ubus_context *ctx, struct ubus_object *obj,
1177 struct ubus_request_data *req, const char *method,
1178 struct blob_attr *msg)
1183 return rpc_errno_status();
1196 execl("/sbin/mtd", "/sbin/mtd", "-r", "erase", "rootfs_data", NULL);
1198 return rpc_errno_status();
1206 rpc_luci2_reboot(struct ubus_context *ctx, struct ubus_object *obj,
1207 struct ubus_request_data *req, const char *method,
1208 struct blob_attr *msg)
1213 return rpc_errno_status();
1224 execl("/sbin/reboot", "/sbin/reboot", NULL);
1226 return rpc_errno_status();
1235 dnsmasq_leasefile(void)
1237 FILE *leases = NULL;
1238 struct uci_package *p;
1239 struct uci_element *e;
1240 struct uci_section *s;
1241 struct uci_ptr ptr = {
1244 .option = "leasefile"
1247 uci_load(cursor, ptr.package, &p);
1252 uci_foreach_element(&p->sections, e)
1254 s = uci_to_section(e);
1256 if (strcmp(s->type, "dnsmasq"))
1259 ptr.section = e->name;
1260 uci_lookup_ptr(cursor, &ptr, NULL, true);
1264 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
1265 leases = fopen(ptr.o->v.string, "r");
1267 uci_unload(cursor, p);
1273 rpc_luci2_network_leases(struct ubus_context *ctx, struct ubus_object *obj,
1274 struct ubus_request_data *req, const char *method,
1275 struct blob_attr *msg)
1280 char *ts, *mac, *addr, *name;
1281 time_t now = time(NULL);
1283 blob_buf_init(&buf, 0);
1284 c = blobmsg_open_array(&buf, "leases");
1286 leases = dnsmasq_leasefile();
1291 while (fgets(line, sizeof(line) - 1, leases))
1293 ts = strtok(line, " \t");
1294 mac = strtok(NULL, " \t");
1295 addr = strtok(NULL, " \t");
1296 name = strtok(NULL, " \t");
1298 if (!ts || !mac || !addr || !name)
1301 if (strchr(addr, ':'))
1304 d = blobmsg_open_table(&buf, NULL);
1306 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1307 blobmsg_add_string(&buf, "macaddr", mac);
1308 blobmsg_add_string(&buf, "ipaddr", addr);
1310 if (strcmp(name, "*"))
1311 blobmsg_add_string(&buf, "hostname", name);
1313 blobmsg_close_table(&buf, d);
1319 blobmsg_close_array(&buf, c);
1320 ubus_send_reply(ctx, req, buf.head);
1326 rpc_luci2_network_leases6(struct ubus_context *ctx, struct ubus_object *obj,
1327 struct ubus_request_data *req, const char *method,
1328 struct blob_attr *msg)
1333 char *ts, *mac, *addr, *name, *duid;
1334 time_t now = time(NULL);
1336 blob_buf_init(&buf, 0);
1337 c = blobmsg_open_array(&buf, "leases");
1339 leases = fopen("/tmp/hosts/6relayd", "r");
1343 while (fgets(line, sizeof(line) - 1, leases))
1345 if (strncmp(line, "# ", 2))
1348 strtok(line + 2, " \t"); /* iface */
1350 duid = strtok(NULL, " \t");
1352 strtok(NULL, " \t"); /* iaid */
1354 name = strtok(NULL, " \t");
1355 ts = strtok(NULL, " \t");
1357 strtok(NULL, " \t"); /* id */
1358 strtok(NULL, " \t"); /* length */
1360 addr = strtok(NULL, " \t\n");
1365 d = blobmsg_open_table(&buf, NULL);
1367 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1368 blobmsg_add_string(&buf, "duid", duid);
1369 blobmsg_add_string(&buf, "ip6addr", addr);
1371 if (strcmp(name, "-"))
1372 blobmsg_add_string(&buf, "hostname", name);
1374 blobmsg_close_array(&buf, d);
1381 leases = dnsmasq_leasefile();
1386 while (fgets(line, sizeof(line) - 1, leases))
1388 ts = strtok(line, " \t");
1389 mac = strtok(NULL, " \t");
1390 addr = strtok(NULL, " \t");
1391 name = strtok(NULL, " \t");
1392 duid = strtok(NULL, " \t\n");
1394 if (!ts || !mac || !addr || !duid)
1397 if (!strchr(addr, ':'))
1400 d = blobmsg_open_table(&buf, NULL);
1402 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1403 blobmsg_add_string(&buf, "macaddr", mac);
1404 blobmsg_add_string(&buf, "ip6addr", addr);
1406 if (strcmp(name, "*"))
1407 blobmsg_add_string(&buf, "hostname", name);
1409 if (strcmp(duid, "*"))
1410 blobmsg_add_string(&buf, "duid", name);
1412 blobmsg_close_table(&buf, d);
1419 blobmsg_close_array(&buf, c);
1420 ubus_send_reply(ctx, req, buf.head);
1426 rpc_luci2_network_ct_count(struct ubus_context *ctx, struct ubus_object *obj,
1427 struct ubus_request_data *req, const char *method,
1428 struct blob_attr *msg)
1433 blob_buf_init(&buf, 0);
1435 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL)
1437 if (fgets(line, sizeof(line) - 1, f))
1438 blobmsg_add_u32(&buf, "count", atoi(line));
1443 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL)
1445 if (fgets(line, sizeof(line) - 1, f))
1446 blobmsg_add_u32(&buf, "limit", atoi(line));
1451 ubus_send_reply(ctx, req, buf.head);
1457 rpc_luci2_network_ct_table(struct ubus_context *ctx, struct ubus_object *obj,
1458 struct ubus_request_data *req, const char *method,
1459 struct blob_attr *msg)
1467 blob_buf_init(&buf, 0);
1468 c = blobmsg_open_array(&buf, "entries");
1470 if ((f = fopen("/proc/net/nf_conntrack", "r")) != NULL)
1472 while (fgets(line, sizeof(line) - 1, f))
1474 d = blobmsg_open_table(&buf, NULL);
1475 memset(seen, 0, sizeof(seen));
1477 for (i = 0, p = strtok(line, " "); p; i++, p = strtok(NULL, " "))
1480 blobmsg_add_u8(&buf, "ipv6", !strcmp(p, "ipv6"));
1482 blobmsg_add_u32(&buf, "protocol", atoi(p));
1484 blobmsg_add_u32(&buf, "expires", atoi(p));
1490 if (!seen[0] && !strncmp(p, "src=", 4))
1492 blobmsg_add_string(&buf, "src", p + 4);
1495 else if (!seen[1] && !strncmp(p, "dst=", 4))
1497 blobmsg_add_string(&buf, "dest", p + 4);
1500 else if (!seen[2] && !strncmp(p, "sport=", 6))
1502 blobmsg_add_u32(&buf, "sport", atoi(p + 6));
1505 else if (!seen[3] && !strncmp(p, "dport=", 6))
1507 blobmsg_add_u32(&buf, "dport", atoi(p + 6));
1510 else if (!strncmp(p, "packets=", 8))
1512 blobmsg_add_u32(&buf,
1513 seen[4] ? "tx_packets" : "rx_packets",
1517 else if (!strncmp(p, "bytes=", 6))
1519 blobmsg_add_u32(&buf,
1520 seen[5] ? "tx_bytes" : "rx_bytes",
1527 blobmsg_close_table(&buf, d);
1533 blobmsg_close_array(&buf, c);
1534 ubus_send_reply(ctx, req, buf.head);
1540 rpc_luci2_network_arp_table(struct ubus_context *ctx, struct ubus_object *obj,
1541 struct ubus_request_data *req, const char *method,
1542 struct blob_attr *msg)
1546 char *addr, *mac, *dev, line[128];
1548 blob_buf_init(&buf, 0);
1549 c = blobmsg_open_array(&buf, "entries");
1551 if ((f = fopen("/proc/net/arp", "r")) != NULL)
1553 /* skip header line */
1554 fgets(line, sizeof(line) - 1, f);
1556 while (fgets(line, sizeof(line) - 1, f))
1558 addr = strtok(line, " \t");
1560 strtok(NULL, " \t"); /* HW type */
1561 strtok(NULL, " \t"); /* Flags */
1563 mac = strtok(NULL, " \t");
1565 strtok(NULL, " \t"); /* Mask */
1567 dev = strtok(NULL, " \t\n");
1572 d = blobmsg_open_table(&buf, NULL);
1573 blobmsg_add_string(&buf, "ipaddr", addr);
1574 blobmsg_add_string(&buf, "macaddr", mac);
1575 blobmsg_add_string(&buf, "device", dev);
1576 blobmsg_close_table(&buf, d);
1582 blobmsg_close_array(&buf, c);
1583 ubus_send_reply(ctx, req, buf.head);
1589 put_hexaddr(const char *name, const char *s, const char *m)
1593 char as[sizeof("255.255.255.255/32\0")];
1595 a.s_addr = strtoul(s, NULL, 16);
1596 inet_ntop(AF_INET, &a, as, sizeof(as));
1600 for (a.s_addr = ntohl(strtoul(m, NULL, 16)), bits = 0;
1601 a.s_addr & 0x80000000;
1605 sprintf(as + strlen(as), "/%u", bits);
1608 blobmsg_add_string(&buf, name, as);
1612 rpc_luci2_network_routes(struct ubus_context *ctx, struct ubus_object *obj,
1613 struct ubus_request_data *req, const char *method,
1614 struct blob_attr *msg)
1618 char *dst, *dmask, *next, *metric, *device;
1622 if (!(routes = fopen("/proc/net/route", "r")))
1623 return rpc_errno_status();
1625 blob_buf_init(&buf, 0);
1626 c = blobmsg_open_array(&buf, "routes");
1628 /* skip header line */
1629 fgets(line, sizeof(line) - 1, routes);
1631 while (fgets(line, sizeof(line) - 1, routes))
1633 device = strtok(line, "\t ");
1634 dst = strtok(NULL, "\t ");
1635 next = strtok(NULL, "\t ");
1637 strtok(NULL, "\t "); /* flags */
1638 strtok(NULL, "\t "); /* refcount */
1639 strtok(NULL, "\t "); /* usecount */
1641 metric = strtok(NULL, "\t ");
1642 dmask = strtok(NULL, "\t ");
1647 d = blobmsg_open_table(&buf, NULL);
1649 put_hexaddr("target", dst, dmask);
1650 put_hexaddr("nexthop", next, NULL);
1652 n = strtoul(metric, NULL, 10);
1653 blobmsg_add_u32(&buf, "metric", n);
1655 blobmsg_add_string(&buf, "device", device);
1657 blobmsg_close_table(&buf, d);
1660 blobmsg_close_array(&buf, c);
1663 ubus_send_reply(ctx, req, buf.head);
1668 put_hex6addr(const char *name, const char *s, const char *m)
1672 char as[INET6_ADDRSTRLEN + sizeof("/128")];
1675 (((x) <= '9') ? ((x) - '0') : \
1676 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1679 for (i = 0; i < 16; i++, s += 2)
1680 a.s6_addr[i] = (16 * hex(*s)) + hex(*(s+1));
1682 inet_ntop(AF_INET6, &a, as, sizeof(as));
1685 sprintf(as + strlen(as), "/%lu", strtoul(m, NULL, 16));
1687 blobmsg_add_string(&buf, name, as);
1691 rpc_luci2_network_routes6(struct ubus_context *ctx, struct ubus_object *obj,
1692 struct ubus_request_data *req, const char *method,
1693 struct blob_attr *msg)
1697 char *src, *smask, *dst, *dmask, *next, *metric, *flags, *device;
1701 if (!(routes = fopen("/proc/net/ipv6_route", "r")))
1702 return rpc_errno_status();
1704 blob_buf_init(&buf, 0);
1705 c = blobmsg_open_array(&buf, "routes");
1707 while (fgets(line, sizeof(line) - 1, routes))
1709 dst = strtok(line, " ");
1710 dmask = strtok(NULL, " ");
1711 src = strtok(NULL, " ");
1712 smask = strtok(NULL, " ");
1713 next = strtok(NULL, " ");
1714 metric = strtok(NULL, " ");
1716 strtok(NULL, " "); /* refcount */
1717 strtok(NULL, " "); /* usecount */
1719 flags = strtok(NULL, " ");
1720 device = strtok(NULL, " \n");
1725 n = strtoul(flags, NULL, 16);
1730 d = blobmsg_open_table(&buf, NULL);
1732 put_hex6addr("target", dst, dmask);
1733 put_hex6addr("source", src, smask);
1734 put_hex6addr("nexthop", next, NULL);
1736 n = strtoul(metric, NULL, 16);
1737 blobmsg_add_u32(&buf, "metric", n);
1739 blobmsg_add_string(&buf, "device", device);
1741 blobmsg_close_table(&buf, d);
1744 blobmsg_close_array(&buf, c);
1747 ubus_send_reply(ctx, req, buf.head);
1752 struct swconfig_state {
1761 swconfig_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1764 char *nl = strchr(buf, '\n');
1765 struct swconfig_state *s = priv;
1773 s->array = blobmsg_open_array(blob, "switches");
1777 p = strtok(NULL, " \n");
1780 blobmsg_add_string(blob, NULL, p);
1782 return (nl - buf + 1);
1786 swconfig_finish_list(struct blob_buf *blob, int status, void *priv)
1788 struct swconfig_state *s = priv;
1791 return UBUS_STATUS_NO_DATA;
1793 blobmsg_close_array(blob, s->array);
1795 return UBUS_STATUS_OK;
1799 rpc_luci2_network_sw_list(struct ubus_context *ctx, struct ubus_object *obj,
1800 struct ubus_request_data *req, const char *method,
1801 struct blob_attr *msg)
1803 struct swconfig_state *state = NULL;
1804 const char *cmd[3] = { "swconfig", "list", NULL };
1806 state = malloc(sizeof(*state));
1809 return UBUS_STATUS_UNKNOWN_ERROR;
1811 memset(state, 0, sizeof(*state));
1813 return ops->exec(cmd, NULL, swconfig_parse_list, NULL, swconfig_finish_list,
1819 swconfig_parse_help(struct blob_buf *blob, char *buf, int len, void *priv)
1823 char *nl = strchr(buf, '\n');
1824 struct swconfig_state *s = priv;
1832 s->array = blobmsg_open_table(blob, "info");
1839 p = strtok(NULL, "-\n");
1844 blobmsg_close_array(blob, s->array2);
1846 s->array2 = blobmsg_open_array(blob, p);
1853 c = blobmsg_open_table(blob, NULL);
1856 p = strtok(NULL, ")");
1859 blobmsg_add_string(blob, "type", p);
1861 p = strtok(NULL, ":( ");
1864 blobmsg_add_string(blob, "name", p);
1866 p = strtok(NULL, "\n");
1870 blobmsg_add_string(blob, "description", p + 1);
1872 blobmsg_close_table(blob, c);
1877 p = strtok(NULL, ")");
1880 blobmsg_add_string(blob, "model", p);
1883 p = strtok(NULL, "(");
1886 blobmsg_add_u32(blob, "num_ports", atoi(p));
1889 p = strtok(NULL, ")");
1892 blobmsg_add_u32(blob, "cpu_port", atoi(p));
1895 p = strtok(NULL, "\n");
1898 blobmsg_add_u32(blob, "num_vlans", atoi(p));
1903 return (nl - buf + 1);
1907 swconfig_finish_help(struct blob_buf *blob, int status, void *priv)
1909 struct swconfig_state *s = priv;
1912 return UBUS_STATUS_NO_DATA;
1915 blobmsg_close_array(blob, s->array2);
1917 blobmsg_close_table(blob, s->array);
1919 return UBUS_STATUS_OK;
1923 rpc_luci2_network_sw_info(struct ubus_context *ctx, struct ubus_object *obj,
1924 struct ubus_request_data *req, const char *method,
1925 struct blob_attr *msg)
1927 struct swconfig_state *state = NULL;
1928 struct blob_attr *tb[__RPC_SWITCH_MAX];
1929 const char *cmd[5] = { "swconfig", "dev", NULL, "help", NULL };
1931 blobmsg_parse(rpc_switch_policy, __RPC_SWITCH_MAX, tb,
1932 blob_data(msg), blob_len(msg));
1934 if (!tb[RPC_SWITCH_NAME])
1935 return UBUS_STATUS_INVALID_ARGUMENT;
1937 state = malloc(sizeof(*state));
1940 return UBUS_STATUS_UNKNOWN_ERROR;
1942 memset(state, 0, sizeof(*state));
1944 cmd[2] = blobmsg_get_string(tb[RPC_SWITCH_NAME]);
1946 return ops->exec(cmd, NULL, swconfig_parse_help, NULL, swconfig_finish_help,
1952 swconfig_parse_link(struct blob_buf *blob, char *val)
1958 bool rxflow = false;
1959 bool txflow = false;
1960 bool duplex = false;
1964 for (p = strtok(val, " "); p; p = strtok(NULL, " "))
1966 if (!strncmp(p, "speed:", 6))
1967 speed = atoi(p + 6);
1968 else if (!strcmp(p, "link:up"))
1970 else if (!strcmp(p, "txflow"))
1972 else if (!strcmp(p, "rxflow"))
1974 else if (!strcmp(p, "full-duplex"))
1976 else if (!strcmp(p, "auto"))
1980 blobmsg_add_u8(blob, "link", up);
1981 blobmsg_add_u8(blob, "rx_flow_control", rxflow);
1982 blobmsg_add_u8(blob, "tx_flow_control", txflow);
1983 blobmsg_add_u8(blob, "full_duplex", duplex);
1984 blobmsg_add_u8(blob, "auto_negotiation", aneg);
1985 blobmsg_add_u32(blob, "speed", speed);
1989 swconfig_parse_stat(struct blob_buf *blob, char *buf, int len, void *priv)
1992 char *nl = strchr(buf, '\n');
1993 struct swconfig_state *s = priv;
2004 s->array = blobmsg_open_array(blob, "ports");
2007 p = strtok(buf, " :\t");
2011 if (!strcmp(p, "Port"))
2014 blobmsg_close_table(blob, s->array2);
2016 s->array2 = blobmsg_open_table(blob, NULL);
2021 v = strtok(NULL, "\n");
2025 if (!strcmp(p, "link"))
2026 swconfig_parse_link(blob, v);
2031 return (nl - buf + 1);
2035 swconfig_finish_stat(struct blob_buf *blob, int status, void *priv)
2037 struct swconfig_state *s = priv;
2040 return UBUS_STATUS_NO_DATA;
2043 blobmsg_close_table(blob, s->array2);
2045 blobmsg_close_array(blob, s->array);
2047 return UBUS_STATUS_OK;
2051 rpc_luci2_network_sw_status(struct ubus_context *ctx, struct ubus_object *obj,
2052 struct ubus_request_data *req, const char *method,
2053 struct blob_attr *msg)
2055 struct swconfig_state *state = NULL;
2056 struct blob_attr *tb[__RPC_SWITCH_MAX];
2057 const char *cmd[5] = { "swconfig", "dev", NULL, "show", NULL };
2059 blobmsg_parse(rpc_switch_policy, __RPC_SWITCH_MAX, tb,
2060 blob_data(msg), blob_len(msg));
2062 if (!tb[RPC_SWITCH_NAME])
2063 return UBUS_STATUS_INVALID_ARGUMENT;
2065 state = malloc(sizeof(*state));
2068 return UBUS_STATUS_UNKNOWN_ERROR;
2070 memset(state, 0, sizeof(*state));
2072 cmd[2] = blobmsg_get_string(tb[RPC_SWITCH_NAME]);
2074 return ops->exec(cmd, NULL, swconfig_parse_stat, NULL, swconfig_finish_stat,
2081 NETWORK_CMD_TRACEROUTE,
2082 NETWORK_CMD_TRACEROUTE6,
2083 NETWORK_CMD_NSLOOKUP
2087 network_cmd(struct ubus_context *ctx, struct ubus_request_data *req,
2088 struct blob_attr *msg, int which)
2091 struct blob_attr *tb[__RPC_D_MAX];
2093 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
2094 blob_data(msg), blob_len(msg));
2096 if (!tb[RPC_D_DATA])
2097 return UBUS_STATUS_INVALID_ARGUMENT;
2099 arg = blobmsg_get_string(tb[RPC_D_DATA]);
2101 const char *cmds[][8] = {
2102 [NETWORK_CMD_PING] = {
2103 "ping", "-c", "5", "-W", "1", arg
2105 [NETWORK_CMD_PING6] = {
2106 "ping6", "-c", "5", "-W", "1", arg
2108 [NETWORK_CMD_TRACEROUTE] = {
2109 "traceroute", "-q", "1", "-w", "1", "-n", arg
2111 [NETWORK_CMD_TRACEROUTE6] = {
2112 "traceroute6", "-q", "1", "-w", "2", "-n", arg
2114 [NETWORK_CMD_NSLOOKUP] = {
2119 return ops->exec(cmds[which], NULL, NULL, NULL, NULL, NULL, ctx, req);
2123 rpc_luci2_network_ping(struct ubus_context *ctx, struct ubus_object *obj,
2124 struct ubus_request_data *req, const char *method,
2125 struct blob_attr *msg)
2127 return network_cmd(ctx, req, msg, NETWORK_CMD_PING);
2131 rpc_luci2_network_ping6(struct ubus_context *ctx, struct ubus_object *obj,
2132 struct ubus_request_data *req, const char *method,
2133 struct blob_attr *msg)
2135 return network_cmd(ctx, req, msg, NETWORK_CMD_PING6);
2139 rpc_luci2_network_traceroute(struct ubus_context *ctx, struct ubus_object *obj,
2140 struct ubus_request_data *req, const char *method,
2141 struct blob_attr *msg)
2143 return network_cmd(ctx, req, msg, NETWORK_CMD_TRACEROUTE);
2147 rpc_luci2_network_traceroute6(struct ubus_context *ctx, struct ubus_object *obj,
2148 struct ubus_request_data *req, const char *method,
2149 struct blob_attr *msg)
2151 return network_cmd(ctx, req, msg, NETWORK_CMD_TRACEROUTE6);
2155 rpc_luci2_network_nslookup(struct ubus_context *ctx, struct ubus_object *obj,
2156 struct ubus_request_data *req, const char *method,
2157 struct blob_attr *msg)
2159 return network_cmd(ctx, req, msg, NETWORK_CMD_NSLOOKUP);
2164 network_ifupdown(struct ubus_context *ctx, struct ubus_request_data *req,
2165 struct blob_attr *msg, bool up)
2167 const char *cmd[3] = { NULL };
2168 struct blob_attr *tb[__RPC_D_MAX];
2170 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
2171 blob_data(msg), blob_len(msg));
2173 if (!tb[RPC_D_DATA])
2174 return UBUS_STATUS_INVALID_ARGUMENT;
2176 cmd[0] = up ? "/sbin/ifup" : "/sbin/ifdown";
2177 cmd[1] = blobmsg_get_string(tb[RPC_D_DATA]);
2179 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
2183 rpc_luci2_network_ifup(struct ubus_context *ctx, struct ubus_object *obj,
2184 struct ubus_request_data *req, const char *method,
2185 struct blob_attr *msg)
2187 return network_ifupdown(ctx, req, msg, true);
2191 rpc_luci2_network_ifdown(struct ubus_context *ctx, struct ubus_object *obj,
2192 struct ubus_request_data *req, const char *method,
2193 struct blob_attr *msg)
2195 return network_ifupdown(ctx, req, msg, false);
2199 rpc_luci2_network_dev_list(struct ubus_context *ctx, struct ubus_object *obj,
2200 struct ubus_request_data *req, const char *method,
2201 struct blob_attr *msg)
2203 char path[PATH_MAX];
2207 bool wireless, bridge, tuntap;
2212 if (!(d = opendir("/sys/class/net")))
2213 return rpc_errno_status();
2215 blob_buf_init(&buf, 0);
2216 c = blobmsg_open_array(&buf, "devices");
2218 while ((e = readdir(d)) != NULL)
2220 snprintf(path, sizeof(path) - 1, "/sys/class/net/%s/type", e->d_name);
2222 if (stat(path, &s) || !S_ISREG(s.st_mode) || !(f = fopen(path, "r")))
2226 memset(path, 0, sizeof(path));
2228 if (fread(path, 1, sizeof(path) - 1, f) > 0)
2233 snprintf(path, sizeof(path) - 1, "/sys/class/net/%s/flags", e->d_name);
2235 if (stat(path, &s) || !S_ISREG(s.st_mode) || !(f = fopen(path, "r")))
2239 memset(path, 0, sizeof(path));
2241 if (fread(path, 1, sizeof(path) - 1, f) > 0)
2242 flags = strtoul(path, NULL, 16);
2246 snprintf(path, sizeof(path) - 1,
2247 "/sys/class/net/%s/wireless", e->d_name);
2249 wireless = (!stat(path, &s) && S_ISDIR(s.st_mode));
2251 snprintf(path, sizeof(path) - 1,
2252 "/sys/class/net/%s/phy80211", e->d_name);
2254 wireless = (wireless || (!stat(path, &s) && S_ISLNK(s.st_mode)));
2256 snprintf(path, sizeof(path) - 1,
2257 "/sys/class/net/%s/bridge", e->d_name);
2259 bridge = (!stat(path, &s) && S_ISDIR(s.st_mode));
2261 snprintf(path, sizeof(path) - 1,
2262 "/sys/class/net/%s/tun_flags", e->d_name);
2264 tuntap = (!stat(path, &s) && S_ISREG(s.st_mode));
2266 t = blobmsg_open_table(&buf, NULL);
2268 blobmsg_add_string(&buf, "device", e->d_name);
2269 blobmsg_add_u32(&buf, "type", type);
2270 blobmsg_add_u8(&buf, "is_up", flags & 1);
2271 blobmsg_add_u8(&buf, "is_bridge", bridge);
2272 blobmsg_add_u8(&buf, "is_tuntap", tuntap);
2273 blobmsg_add_u8(&buf, "is_wireless", wireless);
2275 blobmsg_close_table(&buf, t);
2278 blobmsg_close_array(&buf, c);
2282 ubus_send_reply(ctx, req, buf.head);
2298 opkg_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
2300 struct opkg_state *s = priv;
2303 char *nl = strchr(buf, '\n');
2304 char *name = NULL, *vers = NULL, *desc = NULL;
2312 if (s->cur_offset++ < s->req_offset)
2315 if (s->cur_count++ >= s->req_count)
2321 s->array = blobmsg_open_array(blob, "packages");
2324 for (ptr = buf, last = buf, *nl = 0; ptr <= nl; ptr++)
2326 if (!*ptr || (*ptr == ' ' && *(ptr+1) == '-' && *(ptr+2) == ' '))
2338 desc = *ptr ? (ptr + 3) : NULL;
2347 c = blobmsg_open_array(blob, NULL);
2349 blobmsg_add_string(blob, NULL, name);
2350 blobmsg_add_string(blob, NULL, vers);
2353 blobmsg_add_string(blob, NULL, desc);
2355 blobmsg_close_array(blob, c);
2359 return (nl - buf + 1);
2363 opkg_finish_list(struct blob_buf *blob, int status, void *priv)
2365 struct opkg_state *s = priv;
2368 return UBUS_STATUS_NO_DATA;
2370 blobmsg_close_array(blob, s->array);
2371 blobmsg_add_u32(blob, "total", s->total);
2373 return UBUS_STATUS_OK;
2377 opkg_exec_list(const char *action, struct blob_attr *msg,
2378 struct ubus_context *ctx, struct ubus_request_data *req)
2380 struct opkg_state *state = NULL;
2381 struct blob_attr *tb[__RPC_OM_MAX];
2382 const char *cmd[5] = { "opkg", action, "-nocase", NULL, NULL };
2384 blobmsg_parse(rpc_opkg_match_policy, __RPC_OM_MAX, tb,
2385 blob_data(msg), blob_len(msg));
2387 state = malloc(sizeof(*state));
2390 return UBUS_STATUS_UNKNOWN_ERROR;
2392 memset(state, 0, sizeof(*state));
2394 if (tb[RPC_OM_PATTERN])
2395 cmd[3] = blobmsg_data(tb[RPC_OM_PATTERN]);
2397 if (tb[RPC_OM_LIMIT])
2398 state->req_count = blobmsg_get_u32(tb[RPC_OM_LIMIT]);
2400 if (tb[RPC_OM_OFFSET])
2401 state->req_offset = blobmsg_get_u32(tb[RPC_OM_OFFSET]);
2403 if (state->req_offset < 0)
2404 state->req_offset = 0;
2406 if (state->req_count <= 0 || state->req_count > 100)
2407 state->req_count = 100;
2409 return ops->exec(cmd, NULL, opkg_parse_list, NULL, opkg_finish_list,
2415 rpc_luci2_opkg_list(struct ubus_context *ctx, struct ubus_object *obj,
2416 struct ubus_request_data *req, const char *method,
2417 struct blob_attr *msg)
2419 return opkg_exec_list("list", msg, ctx, req);
2423 rpc_luci2_opkg_list_installed(struct ubus_context *ctx, struct ubus_object *obj,
2424 struct ubus_request_data *req, const char *method,
2425 struct blob_attr *msg)
2427 return opkg_exec_list("list-installed", msg, ctx, req);
2431 rpc_luci2_opkg_find(struct ubus_context *ctx, struct ubus_object *obj,
2432 struct ubus_request_data *req, const char *method,
2433 struct blob_attr *msg)
2435 return opkg_exec_list("find", msg, ctx, req);
2439 rpc_luci2_opkg_update(struct ubus_context *ctx, struct ubus_object *obj,
2440 struct ubus_request_data *req, const char *method,
2441 struct blob_attr *msg)
2443 const char *cmd[3] = { "opkg", "update", NULL };
2444 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
2448 rpc_luci2_opkg_install(struct ubus_context *ctx, struct ubus_object *obj,
2449 struct ubus_request_data *req, const char *method,
2450 struct blob_attr *msg)
2452 struct blob_attr *tb[__RPC_OP_MAX];
2453 const char *cmd[5] = { "opkg", "--force-overwrite",
2454 "install", NULL, NULL };
2456 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
2457 blob_data(msg), blob_len(msg));
2459 if (!tb[RPC_OP_PACKAGE])
2460 return UBUS_STATUS_INVALID_ARGUMENT;
2462 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
2464 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
2468 rpc_luci2_opkg_remove(struct ubus_context *ctx, struct ubus_object *obj,
2469 struct ubus_request_data *req, const char *method,
2470 struct blob_attr *msg)
2472 struct blob_attr *tb[__RPC_OP_MAX];
2473 const char *cmd[5] = { "opkg", "--force-removal-of-dependent-packages",
2474 "remove", NULL, NULL };
2476 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
2477 blob_data(msg), blob_len(msg));
2479 if (!tb[RPC_OP_PACKAGE])
2480 return UBUS_STATUS_INVALID_ARGUMENT;
2482 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
2484 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
2488 rpc_luci2_opkg_config_get(struct ubus_context *ctx, struct ubus_object *obj,
2489 struct ubus_request_data *req, const char *method,
2490 struct blob_attr *msg)
2493 char conf[2048] = { 0 };
2495 if (!(f = fopen("/etc/opkg.conf", "r")))
2496 return rpc_errno_status();
2498 fread(conf, sizeof(conf) - 1, 1, f);
2501 blob_buf_init(&buf, 0);
2502 blobmsg_add_string(&buf, "config", conf);
2504 ubus_send_reply(ctx, req, buf.head);
2509 rpc_luci2_opkg_config_set(struct ubus_context *ctx, struct ubus_object *obj,
2510 struct ubus_request_data *req, const char *method,
2511 struct blob_attr *msg)
2514 struct blob_attr *tb[__RPC_D_MAX];
2516 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
2517 blob_data(msg), blob_len(msg));
2519 if (!tb[RPC_D_DATA])
2520 return UBUS_STATUS_INVALID_ARGUMENT;
2522 if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048)
2523 return UBUS_STATUS_NOT_SUPPORTED;
2525 if (!(f = fopen("/etc/opkg.conf", "w")))
2526 return rpc_errno_status();
2528 fwrite(blobmsg_data(tb[RPC_D_DATA]),
2529 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
2537 menu_access(struct blob_attr *sid, struct blob_attr *acls, struct blob_buf *e)
2540 struct blob_attr *acl;
2544 c = blobmsg_open_table(e, "write");
2546 blobmsg_for_each_attr(acl, acls, rem)
2548 if (!ops->session_access(blobmsg_data(sid), "access-group",
2549 blobmsg_data(acl), "read"))
2555 blobmsg_add_u8(e, blobmsg_data(acl),
2556 ops->session_access(blobmsg_data(sid), "access-group",
2557 blobmsg_data(acl), "write"));
2560 blobmsg_close_table(e, c);
2566 menu_files(struct blob_attr *files)
2571 struct blob_attr *file;
2573 blobmsg_for_each_attr(file, files, rem)
2577 if (blobmsg_type(file) != BLOBMSG_TYPE_STRING)
2580 if (stat(blobmsg_get_string(file), &s) || !S_ISREG(s.st_mode))
2590 rpc_luci2_ui_menu(struct ubus_context *ctx, struct ubus_object *obj,
2591 struct ubus_request_data *req, const char *method,
2592 struct blob_attr *msg)
2596 struct blob_buf menu = { 0 };
2597 struct blob_buf item = { 0 };
2598 struct blob_attr *entry, *attr;
2599 struct blob_attr *tb[__RPC_MENU_MAX];
2603 blobmsg_parse(rpc_menu_policy, __RPC_MENU_MAX, tb,
2604 blob_data(msg), blob_len(msg));
2606 if (!tb[RPC_MENU_SESSION])
2607 return UBUS_STATUS_INVALID_ARGUMENT;
2610 blob_buf_init(&buf, 0);
2611 c = blobmsg_open_table(&buf, "menu");
2613 if (!glob(RPC_LUCI2_MENU_FILES, 0, NULL, &gl))
2615 for (i = 0; i < gl.gl_pathc; i++)
2617 blob_buf_init(&menu, 0);
2619 if (!blobmsg_add_json_from_file(&menu, gl.gl_pathv[i]))
2622 blob_for_each_attr(entry, menu.head, rem)
2624 access = files = true;
2626 blob_buf_init(&item, 0);
2627 d = blobmsg_open_table(&item, blobmsg_name(entry));
2629 blobmsg_for_each_attr(attr, entry, rem2)
2631 if (blob_id(attr) == BLOBMSG_TYPE_ARRAY &&
2632 !strcmp(blobmsg_name(attr), "acls"))
2633 access = menu_access(tb[RPC_MENU_SESSION], attr, &item);
2634 else if (blob_id(attr) == BLOBMSG_TYPE_ARRAY &&
2635 !strcmp(blobmsg_name(attr), "files"))
2636 files = menu_files(attr);
2638 blobmsg_add_blob(&item, attr);
2641 blobmsg_close_table(&item, d);
2643 if (access && files)
2644 blob_for_each_attr(attr, item.head, rem2)
2645 blobmsg_add_blob(&buf, attr);
2647 blob_buf_free(&item);
2651 blob_buf_free(&menu);
2657 blobmsg_close_table(&buf, c);
2659 ubus_send_reply(ctx, req, buf.head);
2665 parse_acl_file(struct blob_buf *acls, const char *path)
2667 struct blob_buf acl = { 0 };
2668 struct blob_attr *cur;
2672 blob_buf_init(&acl, 0);
2674 if (blobmsg_add_json_from_file(&acl, path))
2676 c = blobmsg_open_table(acls, NULL);
2678 blob_for_each_attr(cur, acl.head, rem)
2679 blobmsg_add_blob(acls, cur);
2681 blobmsg_close_table(acls, c);
2684 blob_buf_free(&acl);
2688 rpc_luci2_ui_acls(struct ubus_context *ctx, struct ubus_object *obj,
2689 struct ubus_request_data *req, const char *method,
2690 struct blob_attr *msg)
2696 if (glob(RPC_SESSION_ACL_DIR "/*.json", 0, NULL, &gl))
2697 return rpc_errno_status();
2699 blob_buf_init(&buf, 0);
2700 c = blobmsg_open_array(&buf, "acls");
2702 for (i = 0; i < gl.gl_pathc; i++)
2703 parse_acl_file(&buf, gl.gl_pathv[i]);
2706 blobmsg_close_array(&buf, c);
2708 ubus_send_reply(ctx, req, buf.head);
2713 rpc_luci2_ui_crypt(struct ubus_context *ctx, struct ubus_object *obj,
2714 struct ubus_request_data *req, const char *method,
2715 struct blob_attr *msg)
2718 struct blob_attr *tb[__RPC_D_MAX];
2720 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
2721 blob_data(msg), blob_len(msg));
2723 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 128)
2724 return UBUS_STATUS_INVALID_ARGUMENT;
2726 hash = crypt(blobmsg_get_string(tb[RPC_D_DATA]), "$1$");
2728 blob_buf_init(&buf, 0);
2729 blobmsg_add_string(&buf, "crypt", hash);
2731 ubus_send_reply(ctx, req, buf.head);
2737 rpc_luci2_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
2741 static const struct ubus_method luci2_system_methods[] = {
2742 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log),
2743 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg),
2744 UBUS_METHOD_NOARG("diskfree", rpc_luci2_system_diskfree),
2745 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list),
2746 UBUS_METHOD("process_signal", rpc_luci2_process_signal,
2748 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list),
2749 UBUS_METHOD("init_action", rpc_luci2_init_action,
2751 UBUS_METHOD_NOARG("rclocal_get", rpc_luci2_rclocal_get),
2752 UBUS_METHOD("rclocal_set", rpc_luci2_rclocal_set,
2754 UBUS_METHOD_NOARG("crontab_get", rpc_luci2_crontab_get),
2755 UBUS_METHOD("crontab_set", rpc_luci2_crontab_set,
2757 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get),
2758 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set,
2760 UBUS_METHOD("password_set", rpc_luci2_password_set,
2761 rpc_password_policy),
2762 UBUS_METHOD_NOARG("led_list", rpc_luci2_led_list),
2763 UBUS_METHOD_NOARG("usb_list", rpc_luci2_usb_list),
2764 UBUS_METHOD_NOARG("upgrade_test", rpc_luci2_upgrade_test),
2765 UBUS_METHOD("upgrade_start", rpc_luci2_upgrade_start,
2766 rpc_upgrade_policy),
2767 UBUS_METHOD_NOARG("upgrade_clean", rpc_luci2_upgrade_clean),
2768 UBUS_METHOD_NOARG("backup_restore", rpc_luci2_backup_restore),
2769 UBUS_METHOD_NOARG("backup_clean", rpc_luci2_backup_clean),
2770 UBUS_METHOD_NOARG("backup_config_get", rpc_luci2_backup_config_get),
2771 UBUS_METHOD("backup_config_set", rpc_luci2_backup_config_set,
2773 UBUS_METHOD_NOARG("backup_list", rpc_luci2_backup_list),
2774 UBUS_METHOD_NOARG("reset_test", rpc_luci2_reset_test),
2775 UBUS_METHOD_NOARG("reset_start", rpc_luci2_reset_start),
2776 UBUS_METHOD_NOARG("reboot", rpc_luci2_reboot)
2779 static struct ubus_object_type luci2_system_type =
2780 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods);
2782 static struct ubus_object system_obj = {
2783 .name = "luci2.system",
2784 .type = &luci2_system_type,
2785 .methods = luci2_system_methods,
2786 .n_methods = ARRAY_SIZE(luci2_system_methods),
2790 static const struct ubus_method luci2_network_methods[] = {
2791 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count),
2792 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table),
2793 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table),
2794 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases),
2795 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6),
2796 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes),
2797 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6),
2798 UBUS_METHOD_NOARG("switch_list", rpc_luci2_network_sw_list),
2799 UBUS_METHOD("switch_info", rpc_luci2_network_sw_info,
2801 UBUS_METHOD("switch_status", rpc_luci2_network_sw_status,
2803 UBUS_METHOD("ping", rpc_luci2_network_ping,
2805 UBUS_METHOD("ping6", rpc_luci2_network_ping6,
2807 UBUS_METHOD("traceroute", rpc_luci2_network_traceroute,
2809 UBUS_METHOD("traceroute6", rpc_luci2_network_traceroute6,
2811 UBUS_METHOD("nslookup", rpc_luci2_network_nslookup,
2813 UBUS_METHOD("ifup", rpc_luci2_network_ifup,
2815 UBUS_METHOD("ifdown", rpc_luci2_network_ifdown,
2817 UBUS_METHOD_NOARG("device_list", rpc_luci2_network_dev_list)
2820 static struct ubus_object_type luci2_network_type =
2821 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods);
2823 static struct ubus_object network_obj = {
2824 .name = "luci2.network",
2825 .type = &luci2_network_type,
2826 .methods = luci2_network_methods,
2827 .n_methods = ARRAY_SIZE(luci2_network_methods),
2831 static const struct ubus_method luci2_opkg_methods[] = {
2832 UBUS_METHOD("list", rpc_luci2_opkg_list,
2833 rpc_opkg_match_policy),
2834 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed,
2835 rpc_opkg_match_policy),
2836 UBUS_METHOD("find", rpc_luci2_opkg_find,
2837 rpc_opkg_match_policy),
2838 UBUS_METHOD("install", rpc_luci2_opkg_install,
2839 rpc_opkg_package_policy),
2840 UBUS_METHOD("remove", rpc_luci2_opkg_remove,
2841 rpc_opkg_package_policy),
2842 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update),
2843 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get),
2844 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set,
2848 static struct ubus_object_type luci2_opkg_type =
2849 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods);
2851 static struct ubus_object opkg_obj = {
2852 .name = "luci2.opkg",
2853 .type = &luci2_opkg_type,
2854 .methods = luci2_opkg_methods,
2855 .n_methods = ARRAY_SIZE(luci2_opkg_methods),
2859 static const struct ubus_method luci2_ui_methods[] = {
2860 UBUS_METHOD_NOARG("menu", rpc_luci2_ui_menu),
2861 UBUS_METHOD_NOARG("acls", rpc_luci2_ui_acls),
2862 UBUS_METHOD("crypt", rpc_luci2_ui_crypt,
2866 static struct ubus_object_type luci2_ui_type =
2867 UBUS_OBJECT_TYPE("luci-rpc-luci2-ui", luci2_ui_methods);
2869 static struct ubus_object ui_obj = {
2871 .type = &luci2_ui_type,
2872 .methods = luci2_ui_methods,
2873 .n_methods = ARRAY_SIZE(luci2_ui_methods),
2876 cursor = uci_alloc_context();
2879 return UBUS_STATUS_UNKNOWN_ERROR;
2883 rv |= ubus_add_object(ctx, &system_obj);
2884 rv |= ubus_add_object(ctx, &network_obj);
2885 rv |= ubus_add_object(ctx, &opkg_obj);
2886 rv |= ubus_add_object(ctx, &ui_obj);
2891 const struct rpc_plugin rpc_plugin = {
2892 .init = rpc_luci2_api_init