2 * luci-rpcd - LuCI UBUS RPC server
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #include <sys/types.h>
28 #include <sys/statvfs.h>
30 #include <arpa/inet.h>
38 static struct blob_buf buf;
39 static struct uci_context *cursor;
47 static const struct blobmsg_policy rpc_signal_policy[__RPC_S_MAX] = {
48 [RPC_S_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
49 [RPC_S_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
58 static const struct blobmsg_policy rpc_init_policy[__RPC_I_MAX] = {
59 [RPC_I_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
60 [RPC_I_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
68 static const struct blobmsg_policy rpc_data_policy[__RPC_D_MAX] = {
69 [RPC_D_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
77 static const struct blobmsg_policy rpc_sshkey_policy[__RPC_K_MAX] = {
78 [RPC_K_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
87 static const struct blobmsg_policy rpc_password_policy[__RPC_P_MAX] = {
88 [RPC_P_USER] = { .name = "user", .type = BLOBMSG_TYPE_STRING },
89 [RPC_P_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
99 static const struct blobmsg_policy rpc_opkg_match_policy[__RPC_OM_MAX] = {
100 [RPC_OM_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
101 [RPC_OM_OFFSET] = { .name = "offset", .type = BLOBMSG_TYPE_INT32 },
102 [RPC_OM_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
110 static const struct blobmsg_policy rpc_opkg_package_policy[__RPC_OP_MAX] = {
111 [RPC_OP_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
119 static const struct blobmsg_policy rpc_upgrade_policy[__RPC_UPGRADE_MAX] = {
120 [RPC_UPGRADE_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
128 static const struct blobmsg_policy rpc_menu_policy[__RPC_MENU_MAX] = {
129 [RPC_MENU_SESSION] = { .name = "ubus_rpc_session",
130 .type = BLOBMSG_TYPE_STRING },
135 rpc_errno_status(void)
140 return UBUS_STATUS_PERMISSION_DENIED;
143 return UBUS_STATUS_INVALID_ARGUMENT;
146 return UBUS_STATUS_NOT_FOUND;
149 return UBUS_STATUS_INVALID_ARGUMENT;
152 return UBUS_STATUS_UNKNOWN_ERROR;
157 log_read(FILE *log, int logsize)
163 logsize = RPC_LUCI2_DEF_LOGSIZE;
165 len = (logsize > RPC_LUCI2_MAX_LOGSIZE) ? RPC_LUCI2_MAX_LOGSIZE : logsize;
166 logbuf = blobmsg_alloc_string_buffer(&buf, "log", len + 1);
171 while (logsize > RPC_LUCI2_MAX_LOGSIZE)
173 len = logsize % RPC_LUCI2_MAX_LOGSIZE;
176 len = RPC_LUCI2_MAX_LOGSIZE;
178 fread(logbuf, 1, len, log);
182 len = fread(logbuf, 1, logsize, log);
185 blobmsg_add_string_buffer(&buf);
189 rpc_luci2_system_log(struct ubus_context *ctx, struct ubus_object *obj,
190 struct ubus_request_data *req, const char *method,
191 struct blob_attr *msg)
195 const char *logfile = NULL;
197 struct uci_package *p;
198 struct uci_element *e;
199 struct uci_section *s;
200 struct uci_ptr ptr = { .package = "system" };
202 uci_load(cursor, ptr.package, &p);
205 return UBUS_STATUS_NOT_FOUND;
207 uci_foreach_element(&p->sections, e)
209 s = uci_to_section(e);
211 if (strcmp(s->type, "system"))
215 ptr.option = "log_type";
216 ptr.section = e->name;
217 uci_lookup_ptr(cursor, &ptr, NULL, true);
221 if (ptr.o && ptr.o->type == UCI_TYPE_STRING &&
222 !strcmp(ptr.o->v.string, "file"))
225 ptr.option = "log_file";
226 uci_lookup_ptr(cursor, &ptr, NULL, true);
228 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
229 logfile = ptr.o->v.string;
231 logfile = "/var/log/messages";
233 if (stat(logfile, &st) || !(log = fopen(logfile, "r")))
236 logsize = st.st_size;
241 ptr.option = "log_size";
242 uci_lookup_ptr(cursor, &ptr, NULL, true);
244 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
245 logsize = atoi(ptr.o->v.string) * 1024;
247 if (!(log = popen("logread", "r")))
251 blob_buf_init(&buf, 0);
253 log_read(log, logsize);
256 uci_unload(cursor, p);
257 ubus_send_reply(ctx, req, buf.head);
261 uci_unload(cursor, p);
262 return rpc_errno_status();
266 rpc_luci2_system_dmesg(struct ubus_context *ctx, struct ubus_object *obj,
267 struct ubus_request_data *req, const char *method,
268 struct blob_attr *msg)
272 if (!(log = popen("dmesg", "r")))
273 return rpc_errno_status();
275 blob_buf_init(&buf, 0);
277 log_read(log, RPC_LUCI2_MAX_LOGSIZE);
280 ubus_send_reply(ctx, req, buf.head);
285 rpc_luci2_system_diskfree(struct ubus_context *ctx, struct ubus_object *obj,
286 struct ubus_request_data *req, const char *method,
287 struct blob_attr *msg)
292 const char *fslist[] = {
297 blob_buf_init(&buf, 0);
299 for (i = 0; i < sizeof(fslist) / sizeof(fslist[0]); i += 2)
301 if (statvfs(fslist[i], &s))
304 c = blobmsg_open_table(&buf, fslist[i+1]);
306 blobmsg_add_u32(&buf, "total", s.f_blocks * s.f_frsize);
307 blobmsg_add_u32(&buf, "free", s.f_bfree * s.f_frsize);
308 blobmsg_add_u32(&buf, "used", (s.f_blocks - s.f_bfree) * s.f_frsize);
310 blobmsg_close_table(&buf, c);
313 ubus_send_reply(ctx, req, buf.head);
318 rpc_luci2_process_list(struct ubus_context *ctx, struct ubus_object *obj,
319 struct ubus_request_data *req, const char *method,
320 struct blob_attr *msg)
325 char *pid, *ppid, *user, *stat, *vsz, *pvsz, *pcpu, *cmd;
327 if (!(top = popen("/bin/busybox top -bn1", "r")))
328 return rpc_errno_status();
330 blob_buf_init(&buf, 0);
331 c = blobmsg_open_array(&buf, "processes");
333 while (fgets(line, sizeof(line) - 1, top))
335 pid = strtok(line, " ");
337 if (*pid < '0' || *pid > '9')
340 ppid = strtok(NULL, " ");
341 user = strtok(NULL, " ");
342 stat = strtok(NULL, " ");
355 vsz = strtok(stat + 4, " ");
356 pvsz = strtok(NULL, " ");
357 pcpu = strtok(NULL, " ");
358 cmd = strtok(NULL, "\n");
363 d = blobmsg_open_table(&buf, NULL);
365 blobmsg_add_u32(&buf, "pid", atoi(pid));
366 blobmsg_add_u32(&buf, "ppid", atoi(ppid));
367 blobmsg_add_string(&buf, "user", user);
368 blobmsg_add_string(&buf, "stat", stat);
369 blobmsg_add_u32(&buf, "vsize", atoi(vsz) * 1024);
370 blobmsg_add_u32(&buf, "vsize_percent", atoi(pvsz));
371 blobmsg_add_u32(&buf, "cpu_percent", atoi(pcpu));
372 blobmsg_add_string(&buf, "command", cmd);
374 blobmsg_close_table(&buf, d);
378 blobmsg_close_array(&buf, c);
380 ubus_send_reply(ctx, req, buf.head);
385 rpc_luci2_process_signal(struct ubus_context *ctx, struct ubus_object *obj,
386 struct ubus_request_data *req, const char *method,
387 struct blob_attr *msg)
390 struct blob_attr *tb[__RPC_S_MAX];
392 blobmsg_parse(rpc_signal_policy, __RPC_S_MAX, tb,
393 blob_data(msg), blob_len(msg));
395 if (!tb[RPC_S_SIGNAL] || !tb[RPC_S_PID])
398 return rpc_errno_status();
401 pid = blobmsg_get_u32(tb[RPC_S_PID]);
402 sig = blobmsg_get_u32(tb[RPC_S_SIGNAL]);
405 return rpc_errno_status();
411 rpc_luci2_init_list(struct ubus_context *ctx, struct ubus_object *obj,
412 struct ubus_request_data *req, const char *method,
413 struct blob_attr *msg)
417 char *p, path[PATH_MAX];
423 if (!(d = opendir("/etc/init.d")))
424 return rpc_errno_status();
426 blob_buf_init(&buf, 0);
427 c = blobmsg_open_array(&buf, "initscripts");
429 while ((e = readdir(d)) != NULL)
431 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s", e->d_name);
433 if (stat(path, &s) || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR))
436 if ((f = fopen(path, "r")) != NULL)
439 p = fgets(path, sizeof(path) - 1, f);
441 if (!p || !strstr(p, "/etc/rc.common"))
444 t = blobmsg_open_table(&buf, NULL);
446 blobmsg_add_string(&buf, "name", e->d_name);
448 while (fgets(path, sizeof(path) - 1, f))
450 p = strtok(path, "= \t");
452 if (!strcmp(p, "START") && !!(p = strtok(NULL, "= \t\n")))
455 blobmsg_add_u32(&buf, "start", n);
457 else if (!strcmp(p, "STOP") && !!(p = strtok(NULL, "= \t\n")))
459 blobmsg_add_u32(&buf, "stop", atoi(p));
466 snprintf(path, sizeof(path) - 1, "/etc/rc.d/S%02d%s",
469 blobmsg_add_u8(&buf, "enabled",
470 (!stat(path, &s) && (s.st_mode & S_IXUSR)));
474 blobmsg_add_u8(&buf, "enabled", 0);
477 blobmsg_close_table(&buf, t);
485 blobmsg_close_array(&buf, c);
487 ubus_send_reply(ctx, req, buf.head);
492 rpc_luci2_init_action(struct ubus_context *ctx, struct ubus_object *obj,
493 struct ubus_request_data *req, const char *method,
494 struct blob_attr *msg)
501 struct blob_attr *tb[__RPC_I_MAX];
503 blobmsg_parse(rpc_init_policy, __RPC_I_MAX, tb,
504 blob_data(msg), blob_len(msg));
506 if (!tb[RPC_I_NAME] || !tb[RPC_I_ACTION])
507 return UBUS_STATUS_INVALID_ARGUMENT;
509 action = blobmsg_data(tb[RPC_I_ACTION]);
511 if (strcmp(action, "start") && strcmp(action, "stop") &&
512 strcmp(action, "reload") && strcmp(action, "restart") &&
513 strcmp(action, "enable") && strcmp(action, "disable"))
514 return UBUS_STATUS_INVALID_ARGUMENT;
516 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s",
517 (char *)blobmsg_data(tb[RPC_I_NAME]));
520 return rpc_errno_status();
522 if (!(s.st_mode & S_IXUSR))
523 return UBUS_STATUS_PERMISSION_DENIED;
525 switch ((pid = fork()))
528 return rpc_errno_status();
533 if ((fd = open("/dev/null", O_RDWR)) > -1)
544 if (execl(path, path, action, NULL))
545 return rpc_errno_status();
553 rpc_luci2_rclocal_get(struct ubus_context *ctx, struct ubus_object *obj,
554 struct ubus_request_data *req, const char *method,
555 struct blob_attr *msg)
558 char data[4096] = { 0 };
560 if (!(f = fopen("/etc/rc.local", "r")))
561 return rpc_errno_status();
563 fread(data, sizeof(data) - 1, 1, f);
566 blob_buf_init(&buf, 0);
567 blobmsg_add_string(&buf, "data", data);
569 ubus_send_reply(ctx, req, buf.head);
574 rpc_luci2_rclocal_set(struct ubus_context *ctx, struct ubus_object *obj,
575 struct ubus_request_data *req, const char *method,
576 struct blob_attr *msg)
579 struct blob_attr *tb[__RPC_D_MAX];
581 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
582 blob_data(msg), blob_len(msg));
584 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 4096)
585 return UBUS_STATUS_INVALID_ARGUMENT;
587 if (!(f = fopen("/etc/rc.local", "w")))
588 return rpc_errno_status();
590 fwrite(blobmsg_data(tb[RPC_D_DATA]),
591 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
598 rpc_luci2_crontab_get(struct ubus_context *ctx, struct ubus_object *obj,
599 struct ubus_request_data *req, const char *method,
600 struct blob_attr *msg)
603 char data[4096] = { 0 };
605 if (!(f = fopen("/etc/crontabs/root", "r")))
606 return rpc_errno_status();
608 fread(data, sizeof(data) - 1, 1, f);
611 blob_buf_init(&buf, 0);
612 blobmsg_add_string(&buf, "data", data);
614 ubus_send_reply(ctx, req, buf.head);
619 rpc_luci2_crontab_set(struct ubus_context *ctx, struct ubus_object *obj,
620 struct ubus_request_data *req, const char *method,
621 struct blob_attr *msg)
625 struct blob_attr *tb[__RPC_D_MAX];
627 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
628 blob_data(msg), blob_len(msg));
630 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 4096)
631 return UBUS_STATUS_INVALID_ARGUMENT;
633 if (stat("/etc/crontabs", &s) && mkdir("/etc/crontabs", 0755))
634 return rpc_errno_status();
636 if (!(f = fopen("/etc/crontabs/root", "w")))
637 return rpc_errno_status();
639 fwrite(blobmsg_data(tb[RPC_D_DATA]),
640 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
647 rpc_luci2_sshkeys_get(struct ubus_context *ctx, struct ubus_object *obj,
648 struct ubus_request_data *req, const char *method,
649 struct blob_attr *msg)
655 if (!(f = fopen("/etc/dropbear/authorized_keys", "r")))
656 return rpc_errno_status();
658 blob_buf_init(&buf, 0);
659 c = blobmsg_open_array(&buf, "keys");
661 while (fgets(line, sizeof(line) - 1, f))
663 for (p = line + strlen(line) - 1; (p > line) && isspace(*p); p--)
666 for (p = line; isspace(*p); p++)
670 blobmsg_add_string(&buf, NULL, p);
673 blobmsg_close_array(&buf, c);
676 ubus_send_reply(ctx, req, buf.head);
681 rpc_luci2_sshkeys_set(struct ubus_context *ctx, struct ubus_object *obj,
682 struct ubus_request_data *req, const char *method,
683 struct blob_attr *msg)
687 struct blob_attr *cur, *tb[__RPC_K_MAX];
689 blobmsg_parse(rpc_sshkey_policy, __RPC_K_MAX, tb,
690 blob_data(msg), blob_len(msg));
693 return UBUS_STATUS_INVALID_ARGUMENT;
695 if (!(f = fopen("/etc/dropbear/authorized_keys", "w")))
696 return rpc_errno_status();
698 blobmsg_for_each_attr(cur, tb[RPC_K_KEYS], rem)
700 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
703 fwrite(blobmsg_data(cur), blobmsg_data_len(cur) - 1, 1, f);
704 fwrite("\n", 1, 1, f);
712 rpc_luci2_password_set(struct ubus_context *ctx, struct ubus_object *obj,
713 struct ubus_request_data *req, const char *method,
714 struct blob_attr *msg)
719 struct blob_attr *tb[__RPC_P_MAX];
721 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
722 blob_data(msg), blob_len(msg));
724 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
725 return UBUS_STATUS_INVALID_ARGUMENT;
727 if (stat("/usr/bin/passwd", &s))
728 return UBUS_STATUS_NOT_FOUND;
730 if (!(s.st_mode & S_IXUSR))
731 return UBUS_STATUS_PERMISSION_DENIED;
734 return rpc_errno_status();
736 switch ((pid = fork()))
741 return rpc_errno_status();
750 if ((fd = open("/dev/null", O_RDWR)) > -1)
759 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
760 blobmsg_data(tb[RPC_P_USER]), NULL))
761 return rpc_errno_status();
766 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
767 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
768 write(fds[1], "\n", 1);
772 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
773 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
774 write(fds[1], "\n", 1);
778 waitpid(pid, NULL, 0);
785 rpc_luci2_led_list(struct ubus_context *ctx, struct ubus_object *obj,
786 struct ubus_request_data *req, const char *method,
787 struct blob_attr *msg)
791 void *list, *led, *trigger;
792 char *p, *active_trigger, line[512];
795 if (!(d = opendir("/sys/class/leds")))
796 return rpc_errno_status();
798 blob_buf_init(&buf, 0);
799 list = blobmsg_open_array(&buf, "leds");
801 while ((e = readdir(d)) != NULL)
803 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/trigger",
806 if (!(f = fopen(line, "r")))
809 led = blobmsg_open_table(&buf, NULL);
811 blobmsg_add_string(&buf, "name", e->d_name);
813 if (fgets(line, sizeof(line) - 1, f))
815 trigger = blobmsg_open_array(&buf, "triggers");
817 for (p = strtok(line, " \n"), active_trigger = NULL;
819 p = strtok(NULL, " \n"))
823 *(p + strlen(p) - 1) = 0;
828 blobmsg_add_string(&buf, NULL, p);
831 blobmsg_close_array(&buf, trigger);
834 blobmsg_add_string(&buf, "active_trigger", active_trigger);
839 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/brightness",
842 if ((f = fopen(line, "r")) != NULL)
844 if (fgets(line, sizeof(line) - 1, f))
845 blobmsg_add_u32(&buf, "brightness", atoi(line));
850 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/max_brightness",
853 if ((f = fopen(line, "r")) != NULL)
855 if (fgets(line, sizeof(line) - 1, f))
856 blobmsg_add_u32(&buf, "max_brightness", atoi(line));
861 blobmsg_close_table(&buf, led);
866 blobmsg_close_array(&buf, list);
867 ubus_send_reply(ctx, req, buf.head);
873 rpc_luci2_usb_list(struct ubus_context *ctx, struct ubus_object *obj,
874 struct ubus_request_data *req, const char *method,
875 struct blob_attr *msg)
885 const char *attributes[] = {
886 "manufacturer", "vendor_name", "s",
887 "product", "product_name", "s",
888 "idVendor", "vendor_id", "x",
889 "idProduct", "product_id", "x",
890 "serial", "serial", "s",
891 "speed", "speed", "d",
894 if (!(d = opendir("/sys/bus/usb/devices")))
895 return rpc_errno_status();
897 blob_buf_init(&buf, 0);
898 list = blobmsg_open_array(&buf, "devices");
900 while ((e = readdir(d)) != NULL)
902 if (e->d_name[0] < '0' || e->d_name[0] > '9')
905 snprintf(line, sizeof(line) - 1,
906 "/sys/bus/usb/devices/%s/%s", e->d_name, attributes[0]);
911 device = blobmsg_open_table(&buf, NULL);
913 blobmsg_add_string(&buf, "name", e->d_name);
915 for (i = 0; i < sizeof(attributes) / sizeof(attributes[0]); i += 3)
917 snprintf(line, sizeof(line) - 1,
918 "/sys/bus/usb/devices/%s/%s", e->d_name, attributes[i]);
920 if (!(f = fopen(line, "r")))
923 if (fgets(line, sizeof(line) - 1, f))
925 switch (*attributes[i+2])
928 blobmsg_add_u32(&buf, attributes[i+1],
929 strtoul(line, NULL, 16));
933 blobmsg_add_u32(&buf, attributes[i+1],
934 strtoul(line, NULL, 10));
938 if ((p = strchr(line, '\n')) != NULL)
939 while (p > line && isspace(*p))
942 blobmsg_add_string(&buf, attributes[i+1], line);
950 blobmsg_close_table(&buf, device);
955 blobmsg_close_array(&buf, list);
956 ubus_send_reply(ctx, req, buf.head);
962 rpc_luci2_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
963 struct ubus_request_data *req, const char *method,
964 struct blob_attr *msg)
966 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
967 return rpc_exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
971 rpc_luci2_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
972 struct ubus_request_data *req, const char *method,
973 struct blob_attr *msg)
979 rpc_luci2_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
980 struct ubus_request_data *req, const char *method,
981 struct blob_attr *msg)
983 if (unlink("/tmp/firmware.bin"))
984 return rpc_errno_status();
990 rpc_luci2_backup_restore(struct ubus_context *ctx, struct ubus_object *obj,
991 struct ubus_request_data *req, const char *method,
992 struct blob_attr *msg)
994 const char *cmd[4] = { "sysupgrade", "--restore-backup",
995 "/tmp/backup.tar.gz", NULL };
997 return rpc_exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
1001 rpc_luci2_backup_clean(struct ubus_context *ctx, struct ubus_object *obj,
1002 struct ubus_request_data *req, const char *method,
1003 struct blob_attr *msg)
1005 if (unlink("/tmp/backup.tar.gz"))
1006 return rpc_errno_status();
1012 rpc_luci2_backup_config_get(struct ubus_context *ctx, struct ubus_object *obj,
1013 struct ubus_request_data *req, const char *method,
1014 struct blob_attr *msg)
1017 char conf[2048] = { 0 };
1019 if (!(f = fopen("/etc/sysupgrade.conf", "r")))
1020 return rpc_errno_status();
1022 fread(conf, sizeof(conf) - 1, 1, f);
1025 blob_buf_init(&buf, 0);
1026 blobmsg_add_string(&buf, "config", conf);
1028 ubus_send_reply(ctx, req, buf.head);
1033 rpc_luci2_backup_config_set(struct ubus_context *ctx, struct ubus_object *obj,
1034 struct ubus_request_data *req, const char *method,
1035 struct blob_attr *msg)
1038 struct blob_attr *tb[__RPC_D_MAX];
1040 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
1041 blob_data(msg), blob_len(msg));
1043 if (!tb[RPC_D_DATA])
1044 return UBUS_STATUS_INVALID_ARGUMENT;
1046 if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048)
1047 return UBUS_STATUS_NOT_SUPPORTED;
1049 if (!(f = fopen("/etc/sysupgrade.conf", "w")))
1050 return rpc_errno_status();
1052 fwrite(blobmsg_data(tb[RPC_D_DATA]),
1053 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
1059 struct backup_state {
1065 backup_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1067 struct backup_state *s = priv;
1068 char *nl = strchr(buf, '\n');
1076 s->array = blobmsg_open_array(blob, "files");
1080 blobmsg_add_string(blob, NULL, buf);
1082 return (nl - buf + 1);
1086 backup_finish_list(struct blob_buf *blob, int status, void *priv)
1088 struct backup_state *s = priv;
1091 return UBUS_STATUS_NO_DATA;
1093 blobmsg_close_array(blob, s->array);
1095 return UBUS_STATUS_OK;
1099 rpc_luci2_backup_list(struct ubus_context *ctx, struct ubus_object *obj,
1100 struct ubus_request_data *req, const char *method,
1101 struct blob_attr *msg)
1103 struct backup_state *state = NULL;
1104 const char *cmd[3] = { "sysupgrade", "--list-backup", NULL };
1106 state = malloc(sizeof(*state));
1109 return UBUS_STATUS_UNKNOWN_ERROR;
1111 memset(state, 0, sizeof(*state));
1113 return rpc_exec(cmd, NULL, backup_parse_list, NULL, backup_finish_list,
1118 rpc_luci2_reset_test(struct ubus_context *ctx, struct ubus_object *obj,
1119 struct ubus_request_data *req, const char *method,
1120 struct blob_attr *msg)
1124 char line[64] = { 0 };
1125 bool supported = false;
1127 if (!stat("/sbin/mtd", &s) && (s.st_mode & S_IXUSR))
1129 if ((mtd = fopen("/proc/mtd", "r")) != NULL)
1131 while (fgets(line, sizeof(line) - 1, mtd))
1133 if (strstr(line, "\"rootfs_data\""))
1144 blob_buf_init(&buf, 0);
1145 blobmsg_add_u8(&buf, "supported", supported);
1147 ubus_send_reply(ctx, req, buf.head);
1153 rpc_luci2_reset_start(struct ubus_context *ctx, struct ubus_object *obj,
1154 struct ubus_request_data *req, const char *method,
1155 struct blob_attr *msg)
1160 return rpc_errno_status();
1173 execl("/sbin/mtd", "/sbin/mtd", "-r", "erase", "rootfs_data", NULL);
1175 return rpc_errno_status();
1183 rpc_luci2_reboot(struct ubus_context *ctx, struct ubus_object *obj,
1184 struct ubus_request_data *req, const char *method,
1185 struct blob_attr *msg)
1190 return rpc_errno_status();
1201 execl("/sbin/reboot", "/sbin/reboot", NULL);
1203 return rpc_errno_status();
1212 dnsmasq_leasefile(void)
1214 FILE *leases = NULL;
1215 struct uci_package *p;
1216 struct uci_element *e;
1217 struct uci_section *s;
1218 struct uci_ptr ptr = {
1221 .option = "leasefile"
1224 uci_load(cursor, ptr.package, &p);
1229 uci_foreach_element(&p->sections, e)
1231 s = uci_to_section(e);
1233 if (strcmp(s->type, "dnsmasq"))
1236 ptr.section = e->name;
1237 uci_lookup_ptr(cursor, &ptr, NULL, true);
1241 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
1242 leases = fopen(ptr.o->v.string, "r");
1244 uci_unload(cursor, p);
1250 rpc_luci2_network_leases(struct ubus_context *ctx, struct ubus_object *obj,
1251 struct ubus_request_data *req, const char *method,
1252 struct blob_attr *msg)
1257 char *ts, *mac, *addr, *name;
1258 time_t now = time(NULL);
1260 blob_buf_init(&buf, 0);
1261 c = blobmsg_open_array(&buf, "leases");
1263 leases = dnsmasq_leasefile();
1268 while (fgets(line, sizeof(line) - 1, leases))
1270 ts = strtok(line, " \t");
1271 mac = strtok(NULL, " \t");
1272 addr = strtok(NULL, " \t");
1273 name = strtok(NULL, " \t");
1275 if (!ts || !mac || !addr || !name)
1278 if (strchr(addr, ':'))
1281 d = blobmsg_open_table(&buf, NULL);
1283 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1284 blobmsg_add_string(&buf, "macaddr", mac);
1285 blobmsg_add_string(&buf, "ipaddr", addr);
1287 if (strcmp(name, "*"))
1288 blobmsg_add_string(&buf, "hostname", name);
1290 blobmsg_close_table(&buf, d);
1296 blobmsg_close_array(&buf, c);
1297 ubus_send_reply(ctx, req, buf.head);
1303 rpc_luci2_network_leases6(struct ubus_context *ctx, struct ubus_object *obj,
1304 struct ubus_request_data *req, const char *method,
1305 struct blob_attr *msg)
1310 char *ts, *mac, *addr, *name, *duid;
1311 time_t now = time(NULL);
1313 blob_buf_init(&buf, 0);
1314 c = blobmsg_open_array(&buf, "leases");
1316 leases = fopen("/tmp/hosts/6relayd", "r");
1320 while (fgets(line, sizeof(line) - 1, leases))
1322 if (strncmp(line, "# ", 2))
1325 strtok(line + 2, " \t"); /* iface */
1327 duid = strtok(NULL, " \t");
1329 strtok(NULL, " \t"); /* iaid */
1331 name = strtok(NULL, " \t");
1332 ts = strtok(NULL, " \t");
1334 strtok(NULL, " \t"); /* id */
1335 strtok(NULL, " \t"); /* length */
1337 addr = strtok(NULL, " \t\n");
1342 d = blobmsg_open_table(&buf, NULL);
1344 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1345 blobmsg_add_string(&buf, "duid", duid);
1346 blobmsg_add_string(&buf, "ip6addr", addr);
1348 if (strcmp(name, "-"))
1349 blobmsg_add_string(&buf, "hostname", name);
1351 blobmsg_close_array(&buf, d);
1358 leases = dnsmasq_leasefile();
1363 while (fgets(line, sizeof(line) - 1, leases))
1365 ts = strtok(line, " \t");
1366 mac = strtok(NULL, " \t");
1367 addr = strtok(NULL, " \t");
1368 name = strtok(NULL, " \t");
1369 duid = strtok(NULL, " \t\n");
1371 if (!ts || !mac || !addr || !duid)
1374 if (!strchr(addr, ':'))
1377 d = blobmsg_open_table(&buf, NULL);
1379 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1380 blobmsg_add_string(&buf, "macaddr", mac);
1381 blobmsg_add_string(&buf, "ip6addr", addr);
1383 if (strcmp(name, "*"))
1384 blobmsg_add_string(&buf, "hostname", name);
1386 if (strcmp(duid, "*"))
1387 blobmsg_add_string(&buf, "duid", name);
1389 blobmsg_close_table(&buf, d);
1396 blobmsg_close_array(&buf, c);
1397 ubus_send_reply(ctx, req, buf.head);
1403 rpc_luci2_network_ct_count(struct ubus_context *ctx, struct ubus_object *obj,
1404 struct ubus_request_data *req, const char *method,
1405 struct blob_attr *msg)
1410 blob_buf_init(&buf, 0);
1412 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL)
1414 if (fgets(line, sizeof(line) - 1, f))
1415 blobmsg_add_u32(&buf, "count", atoi(line));
1420 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL)
1422 if (fgets(line, sizeof(line) - 1, f))
1423 blobmsg_add_u32(&buf, "limit", atoi(line));
1428 ubus_send_reply(ctx, req, buf.head);
1434 rpc_luci2_network_ct_table(struct ubus_context *ctx, struct ubus_object *obj,
1435 struct ubus_request_data *req, const char *method,
1436 struct blob_attr *msg)
1444 blob_buf_init(&buf, 0);
1445 c = blobmsg_open_array(&buf, "entries");
1447 if ((f = fopen("/proc/net/nf_conntrack", "r")) != NULL)
1449 while (fgets(line, sizeof(line) - 1, f))
1451 d = blobmsg_open_table(&buf, NULL);
1452 memset(seen, 0, sizeof(seen));
1454 for (i = 0, p = strtok(line, " "); p; i++, p = strtok(NULL, " "))
1457 blobmsg_add_u8(&buf, "ipv6", !strcmp(p, "ipv6"));
1459 blobmsg_add_u32(&buf, "protocol", atoi(p));
1461 blobmsg_add_u32(&buf, "expires", atoi(p));
1467 if (!seen[0] && !strncmp(p, "src=", 4))
1469 blobmsg_add_string(&buf, "src", p + 4);
1472 else if (!seen[1] && !strncmp(p, "dst=", 4))
1474 blobmsg_add_string(&buf, "dest", p + 4);
1477 else if (!seen[2] && !strncmp(p, "sport=", 6))
1479 blobmsg_add_u32(&buf, "sport", atoi(p + 6));
1482 else if (!seen[3] && !strncmp(p, "dport=", 6))
1484 blobmsg_add_u32(&buf, "dport", atoi(p + 6));
1487 else if (!strncmp(p, "packets=", 8))
1489 blobmsg_add_u32(&buf,
1490 seen[4] ? "tx_packets" : "rx_packets",
1494 else if (!strncmp(p, "bytes=", 6))
1496 blobmsg_add_u32(&buf,
1497 seen[5] ? "tx_bytes" : "rx_bytes",
1504 blobmsg_close_table(&buf, d);
1510 blobmsg_close_array(&buf, c);
1511 ubus_send_reply(ctx, req, buf.head);
1517 rpc_luci2_network_arp_table(struct ubus_context *ctx, struct ubus_object *obj,
1518 struct ubus_request_data *req, const char *method,
1519 struct blob_attr *msg)
1523 char *addr, *mac, *dev, line[128];
1525 blob_buf_init(&buf, 0);
1526 c = blobmsg_open_array(&buf, "entries");
1528 if ((f = fopen("/proc/net/arp", "r")) != NULL)
1530 /* skip header line */
1531 fgets(line, sizeof(line) - 1, f);
1533 while (fgets(line, sizeof(line) - 1, f))
1535 addr = strtok(line, " \t");
1537 strtok(NULL, " \t"); /* HW type */
1538 strtok(NULL, " \t"); /* Flags */
1540 mac = strtok(NULL, " \t");
1542 strtok(NULL, " \t"); /* Mask */
1544 dev = strtok(NULL, " \t\n");
1549 d = blobmsg_open_table(&buf, NULL);
1550 blobmsg_add_string(&buf, "ipaddr", addr);
1551 blobmsg_add_string(&buf, "macaddr", mac);
1552 blobmsg_add_string(&buf, "device", dev);
1553 blobmsg_close_table(&buf, d);
1559 blobmsg_close_array(&buf, c);
1560 ubus_send_reply(ctx, req, buf.head);
1566 put_hexaddr(const char *name, const char *s, const char *m)
1570 char as[sizeof("255.255.255.255/32\0")];
1572 a.s_addr = strtoul(s, NULL, 16);
1573 inet_ntop(AF_INET, &a, as, sizeof(as));
1577 for (a.s_addr = ntohl(strtoul(m, NULL, 16)), bits = 0;
1578 a.s_addr & 0x80000000;
1582 sprintf(as + strlen(as), "/%u", bits);
1585 blobmsg_add_string(&buf, name, as);
1589 rpc_luci2_network_routes(struct ubus_context *ctx, struct ubus_object *obj,
1590 struct ubus_request_data *req, const char *method,
1591 struct blob_attr *msg)
1595 char *dst, *dmask, *next, *metric, *device;
1599 if (!(routes = fopen("/proc/net/route", "r")))
1600 return rpc_errno_status();
1602 blob_buf_init(&buf, 0);
1603 c = blobmsg_open_array(&buf, "routes");
1605 /* skip header line */
1606 fgets(line, sizeof(line) - 1, routes);
1608 while (fgets(line, sizeof(line) - 1, routes))
1610 device = strtok(line, "\t ");
1611 dst = strtok(NULL, "\t ");
1612 next = strtok(NULL, "\t ");
1614 strtok(NULL, "\t "); /* flags */
1615 strtok(NULL, "\t "); /* refcount */
1616 strtok(NULL, "\t "); /* usecount */
1618 metric = strtok(NULL, "\t ");
1619 dmask = strtok(NULL, "\t ");
1624 d = blobmsg_open_table(&buf, NULL);
1626 put_hexaddr("target", dst, dmask);
1627 put_hexaddr("nexthop", next, NULL);
1629 n = strtoul(metric, NULL, 10);
1630 blobmsg_add_u32(&buf, "metric", n);
1632 blobmsg_add_string(&buf, "device", device);
1634 blobmsg_close_table(&buf, d);
1637 blobmsg_close_array(&buf, c);
1640 ubus_send_reply(ctx, req, buf.head);
1645 put_hex6addr(const char *name, const char *s, const char *m)
1649 char as[INET6_ADDRSTRLEN + sizeof("/128")];
1652 (((x) <= '9') ? ((x) - '0') : \
1653 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1656 for (i = 0; i < 16; i++, s += 2)
1657 a.s6_addr[i] = (16 * hex(*s)) + hex(*(s+1));
1659 inet_ntop(AF_INET6, &a, as, sizeof(as));
1662 sprintf(as + strlen(as), "/%lu", strtoul(m, NULL, 16));
1664 blobmsg_add_string(&buf, name, as);
1668 rpc_luci2_network_routes6(struct ubus_context *ctx, struct ubus_object *obj,
1669 struct ubus_request_data *req, const char *method,
1670 struct blob_attr *msg)
1674 char *src, *smask, *dst, *dmask, *next, *metric, *flags, *device;
1678 if (!(routes = fopen("/proc/net/ipv6_route", "r")))
1679 return rpc_errno_status();
1681 blob_buf_init(&buf, 0);
1682 c = blobmsg_open_array(&buf, "routes");
1684 while (fgets(line, sizeof(line) - 1, routes))
1686 dst = strtok(line, " ");
1687 dmask = strtok(NULL, " ");
1688 src = strtok(NULL, " ");
1689 smask = strtok(NULL, " ");
1690 next = strtok(NULL, " ");
1691 metric = strtok(NULL, " ");
1693 strtok(NULL, " "); /* refcount */
1694 strtok(NULL, " "); /* usecount */
1696 flags = strtok(NULL, " ");
1697 device = strtok(NULL, " \n");
1702 n = strtoul(flags, NULL, 16);
1707 d = blobmsg_open_table(&buf, NULL);
1709 put_hex6addr("target", dst, dmask);
1710 put_hex6addr("source", src, smask);
1711 put_hex6addr("nexthop", next, NULL);
1713 n = strtoul(metric, NULL, 16);
1714 blobmsg_add_u32(&buf, "metric", n);
1716 blobmsg_add_string(&buf, "device", device);
1718 blobmsg_close_table(&buf, d);
1721 blobmsg_close_array(&buf, c);
1724 ubus_send_reply(ctx, req, buf.head);
1740 opkg_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1742 struct opkg_state *s = priv;
1745 char *nl = strchr(buf, '\n');
1746 char *name = NULL, *vers = NULL, *desc = NULL;
1754 if (s->cur_offset++ < s->req_offset)
1757 if (s->cur_count++ >= s->req_count)
1763 s->array = blobmsg_open_array(blob, "packages");
1766 for (ptr = buf, last = buf, *nl = 0; ptr <= nl; ptr++)
1768 if (!*ptr || (*ptr == ' ' && *(ptr+1) == '-' && *(ptr+2) == ' '))
1780 desc = *ptr ? (ptr + 3) : NULL;
1789 c = blobmsg_open_array(blob, NULL);
1791 blobmsg_add_string(blob, NULL, name);
1792 blobmsg_add_string(blob, NULL, vers);
1795 blobmsg_add_string(blob, NULL, desc);
1797 blobmsg_close_array(blob, c);
1801 return (nl - buf + 1);
1805 opkg_finish_list(struct blob_buf *blob, int status, void *priv)
1807 struct opkg_state *s = priv;
1810 return UBUS_STATUS_NO_DATA;
1812 blobmsg_close_array(blob, s->array);
1813 blobmsg_add_u32(blob, "total", s->total);
1815 return UBUS_STATUS_OK;
1819 opkg_exec_list(const char *action, struct blob_attr *msg,
1820 struct ubus_context *ctx, struct ubus_request_data *req)
1822 struct opkg_state *state = NULL;
1823 struct blob_attr *tb[__RPC_OM_MAX];
1824 const char *cmd[5] = { "opkg", action, "-nocase", NULL, NULL };
1826 blobmsg_parse(rpc_opkg_match_policy, __RPC_OM_MAX, tb,
1827 blob_data(msg), blob_len(msg));
1829 state = malloc(sizeof(*state));
1832 return UBUS_STATUS_UNKNOWN_ERROR;
1834 memset(state, 0, sizeof(*state));
1836 if (tb[RPC_OM_PATTERN])
1837 cmd[3] = blobmsg_data(tb[RPC_OM_PATTERN]);
1839 if (tb[RPC_OM_LIMIT])
1840 state->req_count = blobmsg_get_u32(tb[RPC_OM_LIMIT]);
1842 if (tb[RPC_OM_OFFSET])
1843 state->req_offset = blobmsg_get_u32(tb[RPC_OM_OFFSET]);
1845 if (state->req_offset < 0)
1846 state->req_offset = 0;
1848 if (state->req_count <= 0 || state->req_count > 100)
1849 state->req_count = 100;
1851 return rpc_exec(cmd, NULL, opkg_parse_list, NULL, opkg_finish_list,
1857 rpc_luci2_opkg_list(struct ubus_context *ctx, struct ubus_object *obj,
1858 struct ubus_request_data *req, const char *method,
1859 struct blob_attr *msg)
1861 return opkg_exec_list("list", msg, ctx, req);
1865 rpc_luci2_opkg_list_installed(struct ubus_context *ctx, struct ubus_object *obj,
1866 struct ubus_request_data *req, const char *method,
1867 struct blob_attr *msg)
1869 return opkg_exec_list("list-installed", msg, ctx, req);
1873 rpc_luci2_opkg_find(struct ubus_context *ctx, struct ubus_object *obj,
1874 struct ubus_request_data *req, const char *method,
1875 struct blob_attr *msg)
1877 return opkg_exec_list("find", msg, ctx, req);
1881 rpc_luci2_opkg_update(struct ubus_context *ctx, struct ubus_object *obj,
1882 struct ubus_request_data *req, const char *method,
1883 struct blob_attr *msg)
1885 const char *cmd[3] = { "opkg", "update", NULL };
1886 return rpc_exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
1890 rpc_luci2_opkg_install(struct ubus_context *ctx, struct ubus_object *obj,
1891 struct ubus_request_data *req, const char *method,
1892 struct blob_attr *msg)
1894 struct blob_attr *tb[__RPC_OP_MAX];
1895 const char *cmd[5] = { "opkg", "--force-overwrite",
1896 "install", NULL, NULL };
1898 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1899 blob_data(msg), blob_len(msg));
1901 if (!tb[RPC_OP_PACKAGE])
1902 return UBUS_STATUS_INVALID_ARGUMENT;
1904 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1906 return rpc_exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
1910 rpc_luci2_opkg_remove(struct ubus_context *ctx, struct ubus_object *obj,
1911 struct ubus_request_data *req, const char *method,
1912 struct blob_attr *msg)
1914 struct blob_attr *tb[__RPC_OP_MAX];
1915 const char *cmd[5] = { "opkg", "--force-removal-of-dependent-packages",
1916 "remove", NULL, NULL };
1918 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1919 blob_data(msg), blob_len(msg));
1921 if (!tb[RPC_OP_PACKAGE])
1922 return UBUS_STATUS_INVALID_ARGUMENT;
1924 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1926 return rpc_exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
1930 rpc_luci2_opkg_config_get(struct ubus_context *ctx, struct ubus_object *obj,
1931 struct ubus_request_data *req, const char *method,
1932 struct blob_attr *msg)
1935 char conf[2048] = { 0 };
1937 if (!(f = fopen("/etc/opkg.conf", "r")))
1938 return rpc_errno_status();
1940 fread(conf, sizeof(conf) - 1, 1, f);
1943 blob_buf_init(&buf, 0);
1944 blobmsg_add_string(&buf, "config", conf);
1946 ubus_send_reply(ctx, req, buf.head);
1951 rpc_luci2_opkg_config_set(struct ubus_context *ctx, struct ubus_object *obj,
1952 struct ubus_request_data *req, const char *method,
1953 struct blob_attr *msg)
1956 struct blob_attr *tb[__RPC_D_MAX];
1958 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
1959 blob_data(msg), blob_len(msg));
1961 if (!tb[RPC_D_DATA])
1962 return UBUS_STATUS_INVALID_ARGUMENT;
1964 if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048)
1965 return UBUS_STATUS_NOT_SUPPORTED;
1967 if (!(f = fopen("/etc/opkg.conf", "w")))
1968 return rpc_errno_status();
1970 fwrite(blobmsg_data(tb[RPC_D_DATA]),
1971 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
1979 menu_access(struct blob_attr *sid, struct blob_attr *acls, struct blob_buf *e)
1982 struct blob_attr *acl;
1986 c = blobmsg_open_table(e, "write");
1988 blobmsg_for_each_attr(acl, acls, rem)
1990 if (!rpc_session_access(blobmsg_data(sid), "luci-ui",
1991 blobmsg_data(acl), "read"))
1997 blobmsg_add_u8(e, blobmsg_data(acl),
1998 rpc_session_access(blobmsg_data(sid), "luci-ui",
1999 blobmsg_data(acl), "write"));
2002 blobmsg_close_table(e, c);
2008 rpc_luci2_ui_menu(struct ubus_context *ctx, struct ubus_object *obj,
2009 struct ubus_request_data *req, const char *method,
2010 struct blob_attr *msg)
2014 struct blob_buf menu = { 0 };
2015 struct blob_buf item = { 0 };
2016 struct blob_attr *entry, *attr;
2017 struct blob_attr *tb[__RPC_MENU_MAX];
2021 blobmsg_parse(rpc_menu_policy, __RPC_MENU_MAX, tb,
2022 blob_data(msg), blob_len(msg));
2024 if (!tb[RPC_MENU_SESSION])
2025 return UBUS_STATUS_INVALID_ARGUMENT;
2028 blob_buf_init(&buf, 0);
2029 c = blobmsg_open_table(&buf, "menu");
2031 if (!glob(RPC_LUCI2_MENU_FILES, 0, NULL, &gl))
2033 for (i = 0; i < gl.gl_pathc; i++)
2035 blob_buf_init(&menu, 0);
2037 if (!blobmsg_add_json_from_file(&menu, gl.gl_pathv[i]))
2040 blob_for_each_attr(entry, menu.head, rem)
2044 blob_buf_init(&item, 0);
2045 d = blobmsg_open_table(&item, blobmsg_name(entry));
2047 blobmsg_for_each_attr(attr, entry, rem2)
2049 if (blob_id(attr) == BLOBMSG_TYPE_ARRAY &&
2050 !strcmp(blobmsg_name(attr), "acls"))
2051 access = menu_access(tb[RPC_MENU_SESSION], attr, &item);
2053 blobmsg_add_blob(&item, attr);
2056 blobmsg_close_table(&item, d);
2059 blob_for_each_attr(attr, item.head, rem2)
2060 blobmsg_add_blob(&buf, attr);
2062 blob_buf_free(&item);
2066 blob_buf_free(&menu);
2072 blobmsg_close_table(&buf, c);
2074 ubus_send_reply(ctx, req, buf.head);
2079 int rpc_luci2_api_init(struct ubus_context *ctx)
2083 static const struct ubus_method luci2_system_methods[] = {
2084 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log),
2085 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg),
2086 UBUS_METHOD_NOARG("diskfree", rpc_luci2_system_diskfree),
2087 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list),
2088 UBUS_METHOD("process_signal", rpc_luci2_process_signal,
2090 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list),
2091 UBUS_METHOD("init_action", rpc_luci2_init_action,
2093 UBUS_METHOD_NOARG("rclocal_get", rpc_luci2_rclocal_get),
2094 UBUS_METHOD("rclocal_set", rpc_luci2_rclocal_set,
2096 UBUS_METHOD_NOARG("crontab_get", rpc_luci2_crontab_get),
2097 UBUS_METHOD("crontab_set", rpc_luci2_crontab_set,
2099 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get),
2100 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set,
2102 UBUS_METHOD("password_set", rpc_luci2_password_set,
2103 rpc_password_policy),
2104 UBUS_METHOD_NOARG("led_list", rpc_luci2_led_list),
2105 UBUS_METHOD_NOARG("usb_list", rpc_luci2_usb_list),
2106 UBUS_METHOD_NOARG("upgrade_test", rpc_luci2_upgrade_test),
2107 UBUS_METHOD("upgrade_start", rpc_luci2_upgrade_start,
2108 rpc_upgrade_policy),
2109 UBUS_METHOD_NOARG("upgrade_clean", rpc_luci2_upgrade_clean),
2110 UBUS_METHOD_NOARG("backup_restore", rpc_luci2_backup_restore),
2111 UBUS_METHOD_NOARG("backup_clean", rpc_luci2_backup_clean),
2112 UBUS_METHOD_NOARG("backup_config_get", rpc_luci2_backup_config_get),
2113 UBUS_METHOD("backup_config_set", rpc_luci2_backup_config_set,
2115 UBUS_METHOD_NOARG("backup_list", rpc_luci2_backup_list),
2116 UBUS_METHOD_NOARG("reset_test", rpc_luci2_reset_test),
2117 UBUS_METHOD_NOARG("reset_start", rpc_luci2_reset_start),
2118 UBUS_METHOD_NOARG("reboot", rpc_luci2_reboot)
2121 static struct ubus_object_type luci2_system_type =
2122 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods);
2124 static struct ubus_object system_obj = {
2125 .name = "luci2.system",
2126 .type = &luci2_system_type,
2127 .methods = luci2_system_methods,
2128 .n_methods = ARRAY_SIZE(luci2_system_methods),
2132 static const struct ubus_method luci2_network_methods[] = {
2133 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count),
2134 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table),
2135 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table),
2136 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases),
2137 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6),
2138 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes),
2139 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6),
2142 static struct ubus_object_type luci2_network_type =
2143 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods);
2145 static struct ubus_object network_obj = {
2146 .name = "luci2.network",
2147 .type = &luci2_network_type,
2148 .methods = luci2_network_methods,
2149 .n_methods = ARRAY_SIZE(luci2_network_methods),
2153 static const struct ubus_method luci2_opkg_methods[] = {
2154 UBUS_METHOD("list", rpc_luci2_opkg_list,
2155 rpc_opkg_match_policy),
2156 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed,
2157 rpc_opkg_match_policy),
2158 UBUS_METHOD("find", rpc_luci2_opkg_find,
2159 rpc_opkg_match_policy),
2160 UBUS_METHOD("install", rpc_luci2_opkg_install,
2161 rpc_opkg_package_policy),
2162 UBUS_METHOD("remove", rpc_luci2_opkg_remove,
2163 rpc_opkg_package_policy),
2164 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update),
2165 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get),
2166 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set,
2170 static struct ubus_object_type luci2_opkg_type =
2171 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods);
2173 static struct ubus_object opkg_obj = {
2174 .name = "luci2.opkg",
2175 .type = &luci2_opkg_type,
2176 .methods = luci2_opkg_methods,
2177 .n_methods = ARRAY_SIZE(luci2_opkg_methods),
2181 static const struct ubus_method luci2_ui_methods[] = {
2182 UBUS_METHOD_NOARG("menu", rpc_luci2_ui_menu)
2185 static struct ubus_object_type luci2_ui_type =
2186 UBUS_OBJECT_TYPE("luci-rpc-luci2-ui", luci2_ui_methods);
2188 static struct ubus_object ui_obj = {
2190 .type = &luci2_ui_type,
2191 .methods = luci2_ui_methods,
2192 .n_methods = ARRAY_SIZE(luci2_ui_methods),
2195 cursor = uci_alloc_context();
2198 return UBUS_STATUS_UNKNOWN_ERROR;
2200 rv |= ubus_add_object(ctx, &system_obj);
2201 rv |= ubus_add_object(ctx, &network_obj);
2202 rv |= ubus_add_object(ctx, &opkg_obj);
2203 rv |= ubus_add_object(ctx, &ui_obj);