2 * luci-rpcd - LuCI UBUS RPC server
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #include <sys/types.h>
28 #include <sys/statvfs.h>
30 #include <arpa/inet.h>
36 static struct blob_buf buf;
37 static struct uci_context *cursor;
45 static const struct blobmsg_policy rpc_signal_policy[__RPC_S_MAX] = {
46 [RPC_S_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
47 [RPC_S_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
56 static const struct blobmsg_policy rpc_init_policy[__RPC_I_MAX] = {
57 [RPC_I_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
58 [RPC_I_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
66 static const struct blobmsg_policy rpc_data_policy[__RPC_D_MAX] = {
67 [RPC_D_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
75 static const struct blobmsg_policy rpc_sshkey_policy[__RPC_K_MAX] = {
76 [RPC_K_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
85 static const struct blobmsg_policy rpc_password_policy[__RPC_P_MAX] = {
86 [RPC_P_USER] = { .name = "user", .type = BLOBMSG_TYPE_STRING },
87 [RPC_P_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
97 static const struct blobmsg_policy rpc_opkg_match_policy[__RPC_OM_MAX] = {
98 [RPC_OM_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
99 [RPC_OM_OFFSET] = { .name = "offset", .type = BLOBMSG_TYPE_INT32 },
100 [RPC_OM_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
108 static const struct blobmsg_policy rpc_opkg_package_policy[__RPC_OP_MAX] = {
109 [RPC_OP_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
117 static const struct blobmsg_policy rpc_upgrade_policy[__RPC_UPGRADE_MAX] = {
118 [RPC_UPGRADE_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
123 rpc_errno_status(void)
128 return UBUS_STATUS_PERMISSION_DENIED;
131 return UBUS_STATUS_INVALID_ARGUMENT;
134 return UBUS_STATUS_NOT_FOUND;
137 return UBUS_STATUS_INVALID_ARGUMENT;
140 return UBUS_STATUS_UNKNOWN_ERROR;
145 log_read(FILE *log, int logsize)
151 logsize = RPC_LUCI2_DEF_LOGSIZE;
153 len = (logsize > RPC_LUCI2_MAX_LOGSIZE) ? RPC_LUCI2_MAX_LOGSIZE : logsize;
154 logbuf = blobmsg_alloc_string_buffer(&buf, "log", len + 1);
159 while (logsize > RPC_LUCI2_MAX_LOGSIZE)
161 len = logsize % RPC_LUCI2_MAX_LOGSIZE;
164 len = RPC_LUCI2_MAX_LOGSIZE;
166 fread(logbuf, 1, len, log);
170 len = fread(logbuf, 1, logsize, log);
173 blobmsg_add_string_buffer(&buf);
177 rpc_luci2_system_log(struct ubus_context *ctx, struct ubus_object *obj,
178 struct ubus_request_data *req, const char *method,
179 struct blob_attr *msg)
183 const char *logfile = NULL;
185 struct uci_package *p;
186 struct uci_element *e;
187 struct uci_section *s;
188 struct uci_ptr ptr = { .package = "system" };
190 uci_load(cursor, ptr.package, &p);
193 return UBUS_STATUS_NOT_FOUND;
195 uci_foreach_element(&p->sections, e)
197 s = uci_to_section(e);
199 if (strcmp(s->type, "system"))
203 ptr.option = "log_type";
204 ptr.section = e->name;
205 uci_lookup_ptr(cursor, &ptr, NULL, true);
209 if (ptr.o && ptr.o->type == UCI_TYPE_STRING &&
210 !strcmp(ptr.o->v.string, "file"))
213 ptr.option = "log_file";
214 uci_lookup_ptr(cursor, &ptr, NULL, true);
216 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
217 logfile = ptr.o->v.string;
219 logfile = "/var/log/messages";
221 if (stat(logfile, &st) || !(log = fopen(logfile, "r")))
224 logsize = st.st_size;
229 ptr.option = "log_size";
230 uci_lookup_ptr(cursor, &ptr, NULL, true);
232 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
233 logsize = atoi(ptr.o->v.string) * 1024;
235 if (!(log = popen("logread", "r")))
239 blob_buf_init(&buf, 0);
241 log_read(log, logsize);
244 uci_unload(cursor, p);
245 ubus_send_reply(ctx, req, buf.head);
249 uci_unload(cursor, p);
250 return rpc_errno_status();
254 rpc_luci2_system_dmesg(struct ubus_context *ctx, struct ubus_object *obj,
255 struct ubus_request_data *req, const char *method,
256 struct blob_attr *msg)
260 if (!(log = popen("dmesg", "r")))
261 return rpc_errno_status();
263 blob_buf_init(&buf, 0);
265 log_read(log, RPC_LUCI2_MAX_LOGSIZE);
268 ubus_send_reply(ctx, req, buf.head);
273 rpc_luci2_system_diskfree(struct ubus_context *ctx, struct ubus_object *obj,
274 struct ubus_request_data *req, const char *method,
275 struct blob_attr *msg)
280 const char *fslist[] = {
285 blob_buf_init(&buf, 0);
287 for (i = 0; i < sizeof(fslist) / sizeof(fslist[0]); i += 2)
289 if (statvfs(fslist[i], &s))
292 c = blobmsg_open_table(&buf, fslist[i+1]);
294 blobmsg_add_u32(&buf, "total", s.f_blocks * s.f_frsize);
295 blobmsg_add_u32(&buf, "free", s.f_bfree * s.f_frsize);
296 blobmsg_add_u32(&buf, "used", (s.f_blocks - s.f_bfree) * s.f_frsize);
298 blobmsg_close_table(&buf, c);
301 ubus_send_reply(ctx, req, buf.head);
306 rpc_luci2_process_list(struct ubus_context *ctx, struct ubus_object *obj,
307 struct ubus_request_data *req, const char *method,
308 struct blob_attr *msg)
313 char *pid, *ppid, *user, *stat, *vsz, *pvsz, *pcpu, *cmd;
315 if (!(top = popen("/bin/busybox top -bn1", "r")))
316 return rpc_errno_status();
318 blob_buf_init(&buf, 0);
319 c = blobmsg_open_array(&buf, "processes");
321 while (fgets(line, sizeof(line) - 1, top))
323 pid = strtok(line, " ");
325 if (*pid < '0' || *pid > '9')
328 ppid = strtok(NULL, " ");
329 user = strtok(NULL, " ");
330 stat = strtok(NULL, " ");
343 vsz = strtok(stat + 4, " ");
344 pvsz = strtok(NULL, " ");
345 pcpu = strtok(NULL, " ");
346 cmd = strtok(NULL, "\n");
351 d = blobmsg_open_table(&buf, NULL);
353 blobmsg_add_u32(&buf, "pid", atoi(pid));
354 blobmsg_add_u32(&buf, "ppid", atoi(ppid));
355 blobmsg_add_string(&buf, "user", user);
356 blobmsg_add_string(&buf, "stat", stat);
357 blobmsg_add_u32(&buf, "vsize", atoi(vsz) * 1024);
358 blobmsg_add_u32(&buf, "vsize_percent", atoi(pvsz));
359 blobmsg_add_u32(&buf, "cpu_percent", atoi(pcpu));
360 blobmsg_add_string(&buf, "command", cmd);
362 blobmsg_close_table(&buf, d);
366 blobmsg_close_array(&buf, c);
368 ubus_send_reply(ctx, req, buf.head);
373 rpc_luci2_process_signal(struct ubus_context *ctx, struct ubus_object *obj,
374 struct ubus_request_data *req, const char *method,
375 struct blob_attr *msg)
378 struct blob_attr *tb[__RPC_S_MAX];
380 blobmsg_parse(rpc_signal_policy, __RPC_S_MAX, tb,
381 blob_data(msg), blob_len(msg));
383 if (!tb[RPC_S_SIGNAL] || !tb[RPC_S_PID])
386 return rpc_errno_status();
389 pid = blobmsg_get_u32(tb[RPC_S_PID]);
390 sig = blobmsg_get_u32(tb[RPC_S_SIGNAL]);
393 return rpc_errno_status();
399 rpc_luci2_init_list(struct ubus_context *ctx, struct ubus_object *obj,
400 struct ubus_request_data *req, const char *method,
401 struct blob_attr *msg)
405 char *p, path[PATH_MAX];
411 if (!(d = opendir("/etc/init.d")))
412 return rpc_errno_status();
414 blob_buf_init(&buf, 0);
415 c = blobmsg_open_array(&buf, "initscripts");
417 while ((e = readdir(d)) != NULL)
419 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s", e->d_name);
421 if (stat(path, &s) || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR))
424 if ((f = fopen(path, "r")) != NULL)
427 p = fgets(path, sizeof(path) - 1, f);
429 if (!p || !strstr(p, "/etc/rc.common"))
432 t = blobmsg_open_table(&buf, NULL);
434 blobmsg_add_string(&buf, "name", e->d_name);
436 while (fgets(path, sizeof(path) - 1, f))
438 p = strtok(path, "= \t");
440 if (!strcmp(p, "START") && !!(p = strtok(NULL, "= \t\n")))
443 blobmsg_add_u32(&buf, "start", n);
445 else if (!strcmp(p, "STOP") && !!(p = strtok(NULL, "= \t\n")))
447 blobmsg_add_u32(&buf, "stop", atoi(p));
454 snprintf(path, sizeof(path) - 1, "/etc/rc.d/S%02d%s",
457 blobmsg_add_u8(&buf, "enabled",
458 (!stat(path, &s) && (s.st_mode & S_IXUSR)));
462 blobmsg_add_u8(&buf, "enabled", 0);
465 blobmsg_close_table(&buf, t);
473 blobmsg_close_array(&buf, c);
475 ubus_send_reply(ctx, req, buf.head);
480 rpc_luci2_init_action(struct ubus_context *ctx, struct ubus_object *obj,
481 struct ubus_request_data *req, const char *method,
482 struct blob_attr *msg)
489 struct blob_attr *tb[__RPC_I_MAX];
491 blobmsg_parse(rpc_init_policy, __RPC_I_MAX, tb,
492 blob_data(msg), blob_len(msg));
494 if (!tb[RPC_I_NAME] || !tb[RPC_I_ACTION])
495 return UBUS_STATUS_INVALID_ARGUMENT;
497 action = blobmsg_data(tb[RPC_I_ACTION]);
499 if (strcmp(action, "start") && strcmp(action, "stop") &&
500 strcmp(action, "reload") && strcmp(action, "restart") &&
501 strcmp(action, "enable") && strcmp(action, "disable"))
502 return UBUS_STATUS_INVALID_ARGUMENT;
504 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s",
505 (char *)blobmsg_data(tb[RPC_I_NAME]));
508 return rpc_errno_status();
510 if (!(s.st_mode & S_IXUSR))
511 return UBUS_STATUS_PERMISSION_DENIED;
513 switch ((pid = fork()))
516 return rpc_errno_status();
521 if ((fd = open("/dev/null", O_RDWR)) > -1)
532 if (execl(path, path, action, NULL))
533 return rpc_errno_status();
541 rpc_luci2_rclocal_get(struct ubus_context *ctx, struct ubus_object *obj,
542 struct ubus_request_data *req, const char *method,
543 struct blob_attr *msg)
546 char data[4096] = { 0 };
548 if (!(f = fopen("/etc/rc.local", "r")))
549 return rpc_errno_status();
551 fread(data, sizeof(data) - 1, 1, f);
554 blob_buf_init(&buf, 0);
555 blobmsg_add_string(&buf, "data", data);
557 ubus_send_reply(ctx, req, buf.head);
562 rpc_luci2_rclocal_set(struct ubus_context *ctx, struct ubus_object *obj,
563 struct ubus_request_data *req, const char *method,
564 struct blob_attr *msg)
567 struct blob_attr *tb[__RPC_D_MAX];
569 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
570 blob_data(msg), blob_len(msg));
572 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 4096)
573 return UBUS_STATUS_INVALID_ARGUMENT;
575 if (!(f = fopen("/etc/rc.local", "w")))
576 return rpc_errno_status();
578 fwrite(blobmsg_data(tb[RPC_D_DATA]),
579 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
586 rpc_luci2_crontab_get(struct ubus_context *ctx, struct ubus_object *obj,
587 struct ubus_request_data *req, const char *method,
588 struct blob_attr *msg)
591 char data[4096] = { 0 };
593 if (!(f = fopen("/etc/crontabs/root", "r")))
594 return rpc_errno_status();
596 fread(data, sizeof(data) - 1, 1, f);
599 blob_buf_init(&buf, 0);
600 blobmsg_add_string(&buf, "data", data);
602 ubus_send_reply(ctx, req, buf.head);
607 rpc_luci2_crontab_set(struct ubus_context *ctx, struct ubus_object *obj,
608 struct ubus_request_data *req, const char *method,
609 struct blob_attr *msg)
613 struct blob_attr *tb[__RPC_D_MAX];
615 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
616 blob_data(msg), blob_len(msg));
618 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 4096)
619 return UBUS_STATUS_INVALID_ARGUMENT;
621 if (stat("/etc/crontabs", &s) && mkdir("/etc/crontabs", 0755))
622 return rpc_errno_status();
624 if (!(f = fopen("/etc/crontabs/root", "w")))
625 return rpc_errno_status();
627 fwrite(blobmsg_data(tb[RPC_D_DATA]),
628 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
635 rpc_luci2_sshkeys_get(struct ubus_context *ctx, struct ubus_object *obj,
636 struct ubus_request_data *req, const char *method,
637 struct blob_attr *msg)
643 if (!(f = fopen("/etc/dropbear/authorized_keys", "r")))
644 return rpc_errno_status();
646 blob_buf_init(&buf, 0);
647 c = blobmsg_open_array(&buf, "keys");
649 while (fgets(line, sizeof(line) - 1, f))
651 for (p = line + strlen(line) - 1; (p > line) && isspace(*p); p--)
654 for (p = line; isspace(*p); p++)
658 blobmsg_add_string(&buf, NULL, p);
661 blobmsg_close_array(&buf, c);
664 ubus_send_reply(ctx, req, buf.head);
669 rpc_luci2_sshkeys_set(struct ubus_context *ctx, struct ubus_object *obj,
670 struct ubus_request_data *req, const char *method,
671 struct blob_attr *msg)
675 struct blob_attr *cur, *tb[__RPC_K_MAX];
677 blobmsg_parse(rpc_sshkey_policy, __RPC_K_MAX, tb,
678 blob_data(msg), blob_len(msg));
681 return UBUS_STATUS_INVALID_ARGUMENT;
683 if (!(f = fopen("/etc/dropbear/authorized_keys", "w")))
684 return rpc_errno_status();
686 blobmsg_for_each_attr(cur, tb[RPC_K_KEYS], rem)
688 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
691 fwrite(blobmsg_data(cur), blobmsg_data_len(cur) - 1, 1, f);
692 fwrite("\n", 1, 1, f);
700 rpc_luci2_password_set(struct ubus_context *ctx, struct ubus_object *obj,
701 struct ubus_request_data *req, const char *method,
702 struct blob_attr *msg)
707 struct blob_attr *tb[__RPC_P_MAX];
709 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
710 blob_data(msg), blob_len(msg));
712 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
713 return UBUS_STATUS_INVALID_ARGUMENT;
715 if (stat("/usr/bin/passwd", &s))
716 return UBUS_STATUS_NOT_FOUND;
718 if (!(s.st_mode & S_IXUSR))
719 return UBUS_STATUS_PERMISSION_DENIED;
722 return rpc_errno_status();
724 switch ((pid = fork()))
729 return rpc_errno_status();
738 if ((fd = open("/dev/null", O_RDWR)) > -1)
747 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
748 blobmsg_data(tb[RPC_P_USER]), NULL))
749 return rpc_errno_status();
754 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
755 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
756 write(fds[1], "\n", 1);
760 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
761 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
762 write(fds[1], "\n", 1);
766 waitpid(pid, NULL, 0);
773 rpc_luci2_led_list(struct ubus_context *ctx, struct ubus_object *obj,
774 struct ubus_request_data *req, const char *method,
775 struct blob_attr *msg)
779 void *list, *led, *trigger;
780 char *p, *active_trigger, line[512];
783 if (!(d = opendir("/sys/class/leds")))
784 return rpc_errno_status();
786 blob_buf_init(&buf, 0);
787 list = blobmsg_open_array(&buf, "leds");
789 while ((e = readdir(d)) != NULL)
791 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/trigger",
794 if (!(f = fopen(line, "r")))
797 led = blobmsg_open_table(&buf, NULL);
799 blobmsg_add_string(&buf, "name", e->d_name);
801 if (fgets(line, sizeof(line) - 1, f))
803 trigger = blobmsg_open_array(&buf, "triggers");
805 for (p = strtok(line, " \n"), active_trigger = NULL;
807 p = strtok(NULL, " \n"))
811 *(p + strlen(p) - 1) = 0;
816 blobmsg_add_string(&buf, NULL, p);
819 blobmsg_close_array(&buf, trigger);
822 blobmsg_add_string(&buf, "active_trigger", active_trigger);
827 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/brightness",
830 if ((f = fopen(line, "r")) != NULL)
832 if (fgets(line, sizeof(line) - 1, f))
833 blobmsg_add_u32(&buf, "brightness", atoi(line));
838 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/max_brightness",
841 if ((f = fopen(line, "r")) != NULL)
843 if (fgets(line, sizeof(line) - 1, f))
844 blobmsg_add_u32(&buf, "max_brightness", atoi(line));
849 blobmsg_close_table(&buf, led);
854 blobmsg_close_array(&buf, list);
855 ubus_send_reply(ctx, req, buf.head);
861 rpc_luci2_usb_list(struct ubus_context *ctx, struct ubus_object *obj,
862 struct ubus_request_data *req, const char *method,
863 struct blob_attr *msg)
873 const char *attributes[] = {
874 "manufacturer", "vendor_name", "s",
875 "product", "product_name", "s",
876 "idVendor", "vendor_id", "x",
877 "idProduct", "product_id", "x",
878 "serial", "serial", "s",
879 "speed", "speed", "d",
882 if (!(d = opendir("/sys/bus/usb/devices")))
883 return rpc_errno_status();
885 blob_buf_init(&buf, 0);
886 list = blobmsg_open_array(&buf, "devices");
888 while ((e = readdir(d)) != NULL)
890 if (e->d_name[0] < '0' || e->d_name[0] > '9')
893 snprintf(line, sizeof(line) - 1,
894 "/sys/bus/usb/devices/%s/%s", e->d_name, attributes[0]);
899 device = blobmsg_open_table(&buf, NULL);
901 blobmsg_add_string(&buf, "name", e->d_name);
903 for (i = 0; i < sizeof(attributes) / sizeof(attributes[0]); i += 3)
905 snprintf(line, sizeof(line) - 1,
906 "/sys/bus/usb/devices/%s/%s", e->d_name, attributes[i]);
908 if (!(f = fopen(line, "r")))
911 if (fgets(line, sizeof(line) - 1, f))
913 switch (*attributes[i+2])
916 blobmsg_add_u32(&buf, attributes[i+1],
917 strtoul(line, NULL, 16));
921 blobmsg_add_u32(&buf, attributes[i+1],
922 strtoul(line, NULL, 10));
926 if ((p = strchr(line, '\n')) != NULL)
927 while (p > line && isspace(*p))
930 blobmsg_add_string(&buf, attributes[i+1], line);
938 blobmsg_close_table(&buf, device);
943 blobmsg_close_array(&buf, list);
944 ubus_send_reply(ctx, req, buf.head);
950 rpc_luci2_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
951 struct ubus_request_data *req, const char *method,
952 struct blob_attr *msg)
954 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
955 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
959 rpc_luci2_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
960 struct ubus_request_data *req, const char *method,
961 struct blob_attr *msg)
967 rpc_luci2_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
968 struct ubus_request_data *req, const char *method,
969 struct blob_attr *msg)
971 if (unlink("/tmp/firmware.bin"))
972 return rpc_errno_status();
978 rpc_luci2_backup_restore(struct ubus_context *ctx, struct ubus_object *obj,
979 struct ubus_request_data *req, const char *method,
980 struct blob_attr *msg)
982 const char *cmd[4] = { "sysupgrade", "--restore-backup",
983 "/tmp/backup.tar.gz", NULL };
985 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
989 rpc_luci2_backup_clean(struct ubus_context *ctx, struct ubus_object *obj,
990 struct ubus_request_data *req, const char *method,
991 struct blob_attr *msg)
993 if (unlink("/tmp/backup.tar.gz"))
994 return rpc_errno_status();
1001 dnsmasq_leasefile(void)
1003 FILE *leases = NULL;
1004 struct uci_package *p;
1005 struct uci_element *e;
1006 struct uci_section *s;
1007 struct uci_ptr ptr = {
1010 .option = "leasefile"
1013 uci_load(cursor, ptr.package, &p);
1018 uci_foreach_element(&p->sections, e)
1020 s = uci_to_section(e);
1022 if (strcmp(s->type, "dnsmasq"))
1025 ptr.section = e->name;
1026 uci_lookup_ptr(cursor, &ptr, NULL, true);
1030 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
1031 leases = fopen(ptr.o->v.string, "r");
1033 uci_unload(cursor, p);
1039 rpc_luci2_network_leases(struct ubus_context *ctx, struct ubus_object *obj,
1040 struct ubus_request_data *req, const char *method,
1041 struct blob_attr *msg)
1046 char *ts, *mac, *addr, *name;
1047 time_t now = time(NULL);
1049 blob_buf_init(&buf, 0);
1050 c = blobmsg_open_array(&buf, "leases");
1052 leases = dnsmasq_leasefile();
1057 while (fgets(line, sizeof(line) - 1, leases))
1059 ts = strtok(line, " \t");
1060 mac = strtok(NULL, " \t");
1061 addr = strtok(NULL, " \t");
1062 name = strtok(NULL, " \t");
1064 if (!ts || !mac || !addr || !name)
1067 if (strchr(addr, ':'))
1070 d = blobmsg_open_table(&buf, NULL);
1072 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1073 blobmsg_add_string(&buf, "macaddr", mac);
1074 blobmsg_add_string(&buf, "ipaddr", addr);
1076 if (strcmp(name, "*"))
1077 blobmsg_add_string(&buf, "hostname", name);
1079 blobmsg_close_table(&buf, d);
1085 blobmsg_close_array(&buf, c);
1086 ubus_send_reply(ctx, req, buf.head);
1092 rpc_luci2_network_leases6(struct ubus_context *ctx, struct ubus_object *obj,
1093 struct ubus_request_data *req, const char *method,
1094 struct blob_attr *msg)
1099 char *ts, *mac, *addr, *name, *duid;
1100 time_t now = time(NULL);
1102 blob_buf_init(&buf, 0);
1103 c = blobmsg_open_array(&buf, "leases");
1105 leases = fopen("/tmp/hosts/6relayd", "r");
1109 while (fgets(line, sizeof(line) - 1, leases))
1111 if (strncmp(line, "# ", 2))
1114 strtok(line + 2, " \t"); /* iface */
1116 duid = strtok(NULL, " \t");
1118 strtok(NULL, " \t"); /* iaid */
1120 name = strtok(NULL, " \t");
1121 ts = strtok(NULL, " \t");
1123 strtok(NULL, " \t"); /* id */
1124 strtok(NULL, " \t"); /* length */
1126 addr = strtok(NULL, " \t\n");
1131 d = blobmsg_open_table(&buf, NULL);
1133 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1134 blobmsg_add_string(&buf, "duid", duid);
1135 blobmsg_add_string(&buf, "ip6addr", addr);
1137 if (strcmp(name, "-"))
1138 blobmsg_add_string(&buf, "hostname", name);
1140 blobmsg_close_array(&buf, d);
1147 leases = dnsmasq_leasefile();
1152 while (fgets(line, sizeof(line) - 1, leases))
1154 ts = strtok(line, " \t");
1155 mac = strtok(NULL, " \t");
1156 addr = strtok(NULL, " \t");
1157 name = strtok(NULL, " \t");
1158 duid = strtok(NULL, " \t\n");
1160 if (!ts || !mac || !addr || !duid)
1163 if (!strchr(addr, ':'))
1166 d = blobmsg_open_table(&buf, NULL);
1168 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1169 blobmsg_add_string(&buf, "macaddr", mac);
1170 blobmsg_add_string(&buf, "ip6addr", addr);
1172 if (strcmp(name, "*"))
1173 blobmsg_add_string(&buf, "hostname", name);
1175 if (strcmp(duid, "*"))
1176 blobmsg_add_string(&buf, "duid", name);
1178 blobmsg_close_table(&buf, d);
1185 blobmsg_close_array(&buf, c);
1186 ubus_send_reply(ctx, req, buf.head);
1192 rpc_luci2_network_ct_count(struct ubus_context *ctx, struct ubus_object *obj,
1193 struct ubus_request_data *req, const char *method,
1194 struct blob_attr *msg)
1199 blob_buf_init(&buf, 0);
1201 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL)
1203 if (fgets(line, sizeof(line) - 1, f))
1204 blobmsg_add_u32(&buf, "count", atoi(line));
1209 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL)
1211 if (fgets(line, sizeof(line) - 1, f))
1212 blobmsg_add_u32(&buf, "limit", atoi(line));
1217 ubus_send_reply(ctx, req, buf.head);
1223 rpc_luci2_network_ct_table(struct ubus_context *ctx, struct ubus_object *obj,
1224 struct ubus_request_data *req, const char *method,
1225 struct blob_attr *msg)
1233 blob_buf_init(&buf, 0);
1234 c = blobmsg_open_array(&buf, "entries");
1236 if ((f = fopen("/proc/net/nf_conntrack", "r")) != NULL)
1238 while (fgets(line, sizeof(line) - 1, f))
1240 d = blobmsg_open_table(&buf, NULL);
1241 memset(seen, 0, sizeof(seen));
1243 for (i = 0, p = strtok(line, " "); p; i++, p = strtok(NULL, " "))
1246 blobmsg_add_u8(&buf, "ipv6", !strcmp(p, "ipv6"));
1248 blobmsg_add_u32(&buf, "protocol", atoi(p));
1250 blobmsg_add_u32(&buf, "expires", atoi(p));
1256 if (!seen[0] && !strncmp(p, "src=", 4))
1258 blobmsg_add_string(&buf, "src", p + 4);
1261 else if (!seen[1] && !strncmp(p, "dst=", 4))
1263 blobmsg_add_string(&buf, "dest", p + 4);
1266 else if (!seen[2] && !strncmp(p, "sport=", 6))
1268 blobmsg_add_u32(&buf, "sport", atoi(p + 6));
1271 else if (!seen[3] && !strncmp(p, "dport=", 6))
1273 blobmsg_add_u32(&buf, "dport", atoi(p + 6));
1276 else if (!strncmp(p, "packets=", 8))
1278 blobmsg_add_u32(&buf,
1279 seen[4] ? "tx_packets" : "rx_packets",
1283 else if (!strncmp(p, "bytes=", 6))
1285 blobmsg_add_u32(&buf,
1286 seen[5] ? "tx_bytes" : "rx_bytes",
1293 blobmsg_close_table(&buf, d);
1299 blobmsg_close_array(&buf, c);
1300 ubus_send_reply(ctx, req, buf.head);
1306 rpc_luci2_network_arp_table(struct ubus_context *ctx, struct ubus_object *obj,
1307 struct ubus_request_data *req, const char *method,
1308 struct blob_attr *msg)
1312 char *addr, *mac, *dev, line[128];
1314 blob_buf_init(&buf, 0);
1315 c = blobmsg_open_array(&buf, "entries");
1317 if ((f = fopen("/proc/net/arp", "r")) != NULL)
1319 /* skip header line */
1320 fgets(line, sizeof(line) - 1, f);
1322 while (fgets(line, sizeof(line) - 1, f))
1324 addr = strtok(line, " \t");
1326 strtok(NULL, " \t"); /* HW type */
1327 strtok(NULL, " \t"); /* Flags */
1329 mac = strtok(NULL, " \t");
1331 strtok(NULL, " \t"); /* Mask */
1333 dev = strtok(NULL, " \t\n");
1338 d = blobmsg_open_table(&buf, NULL);
1339 blobmsg_add_string(&buf, "ipaddr", addr);
1340 blobmsg_add_string(&buf, "macaddr", mac);
1341 blobmsg_add_string(&buf, "device", dev);
1342 blobmsg_close_table(&buf, d);
1348 blobmsg_close_array(&buf, c);
1349 ubus_send_reply(ctx, req, buf.head);
1355 put_hexaddr(const char *name, const char *s, const char *m)
1359 char as[sizeof("255.255.255.255/32\0")];
1361 a.s_addr = strtoul(s, NULL, 16);
1362 inet_ntop(AF_INET, &a, as, sizeof(as));
1366 for (a.s_addr = ntohl(strtoul(m, NULL, 16)), bits = 0;
1367 a.s_addr & 0x80000000;
1371 sprintf(as + strlen(as), "/%u", bits);
1374 blobmsg_add_string(&buf, name, as);
1378 rpc_luci2_network_routes(struct ubus_context *ctx, struct ubus_object *obj,
1379 struct ubus_request_data *req, const char *method,
1380 struct blob_attr *msg)
1384 char *dst, *dmask, *next, *metric, *device;
1388 if (!(routes = fopen("/proc/net/route", "r")))
1389 return rpc_errno_status();
1391 blob_buf_init(&buf, 0);
1392 c = blobmsg_open_array(&buf, "routes");
1394 /* skip header line */
1395 fgets(line, sizeof(line) - 1, routes);
1397 while (fgets(line, sizeof(line) - 1, routes))
1399 device = strtok(line, "\t ");
1400 dst = strtok(NULL, "\t ");
1401 next = strtok(NULL, "\t ");
1403 strtok(NULL, "\t "); /* flags */
1404 strtok(NULL, "\t "); /* refcount */
1405 strtok(NULL, "\t "); /* usecount */
1407 metric = strtok(NULL, "\t ");
1408 dmask = strtok(NULL, "\t ");
1413 d = blobmsg_open_table(&buf, NULL);
1415 put_hexaddr("target", dst, dmask);
1416 put_hexaddr("nexthop", next, NULL);
1418 n = strtoul(metric, NULL, 10);
1419 blobmsg_add_u32(&buf, "metric", n);
1421 blobmsg_add_string(&buf, "device", device);
1423 blobmsg_close_table(&buf, d);
1426 blobmsg_close_array(&buf, c);
1429 ubus_send_reply(ctx, req, buf.head);
1434 put_hex6addr(const char *name, const char *s, const char *m)
1438 char as[INET6_ADDRSTRLEN + sizeof("/128")];
1441 (((x) <= '9') ? ((x) - '0') : \
1442 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1445 for (i = 0; i < 16; i++, s += 2)
1446 a.s6_addr[i] = (16 * hex(*s)) + hex(*(s+1));
1448 inet_ntop(AF_INET6, &a, as, sizeof(as));
1451 sprintf(as + strlen(as), "/%lu", strtoul(m, NULL, 16));
1453 blobmsg_add_string(&buf, name, as);
1457 rpc_luci2_network_routes6(struct ubus_context *ctx, struct ubus_object *obj,
1458 struct ubus_request_data *req, const char *method,
1459 struct blob_attr *msg)
1463 char *src, *smask, *dst, *dmask, *next, *metric, *flags, *device;
1467 if (!(routes = fopen("/proc/net/ipv6_route", "r")))
1468 return rpc_errno_status();
1470 blob_buf_init(&buf, 0);
1471 c = blobmsg_open_array(&buf, "routes");
1473 while (fgets(line, sizeof(line) - 1, routes))
1475 dst = strtok(line, " ");
1476 dmask = strtok(NULL, " ");
1477 src = strtok(NULL, " ");
1478 smask = strtok(NULL, " ");
1479 next = strtok(NULL, " ");
1480 metric = strtok(NULL, " ");
1482 strtok(NULL, " "); /* refcount */
1483 strtok(NULL, " "); /* usecount */
1485 flags = strtok(NULL, " ");
1486 device = strtok(NULL, " \n");
1491 n = strtoul(flags, NULL, 16);
1496 d = blobmsg_open_table(&buf, NULL);
1498 put_hex6addr("target", dst, dmask);
1499 put_hex6addr("source", src, smask);
1500 put_hex6addr("nexthop", next, NULL);
1502 n = strtoul(metric, NULL, 16);
1503 blobmsg_add_u32(&buf, "metric", n);
1505 blobmsg_add_string(&buf, "device", device);
1507 blobmsg_close_table(&buf, d);
1510 blobmsg_close_array(&buf, c);
1513 ubus_send_reply(ctx, req, buf.head);
1529 opkg_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1531 struct opkg_state *s = priv;
1534 char *nl = strchr(buf, '\n');
1535 char *name = NULL, *vers = NULL, *desc = NULL;
1543 if (s->cur_offset++ < s->req_offset)
1546 if (s->cur_count++ >= s->req_count)
1552 s->array = blobmsg_open_array(blob, "packages");
1555 for (ptr = buf, last = buf, *nl = 0; ptr <= nl; ptr++)
1557 if (!*ptr || (*ptr == ' ' && *(ptr+1) == '-' && *(ptr+2) == ' '))
1569 desc = *ptr ? (ptr + 3) : NULL;
1578 c = blobmsg_open_array(blob, NULL);
1580 blobmsg_add_string(blob, NULL, name);
1581 blobmsg_add_string(blob, NULL, vers);
1584 blobmsg_add_string(blob, NULL, desc);
1586 blobmsg_close_array(blob, c);
1590 return (nl - buf + 1);
1594 opkg_finish_list(struct blob_buf *blob, int status, void *priv)
1596 struct opkg_state *s = priv;
1601 blobmsg_close_array(blob, s->array);
1602 blobmsg_add_u32(blob, "total", s->total);
1606 opkg_exec_list(const char *action, struct blob_attr *msg,
1607 struct ubus_context *ctx, struct ubus_request_data *req)
1609 struct opkg_state *state = NULL;
1610 struct blob_attr *tb[__RPC_OM_MAX];
1611 const char *cmd[5] = { "opkg", action, "-nocase", NULL, NULL };
1613 blobmsg_parse(rpc_opkg_match_policy, __RPC_OM_MAX, tb,
1614 blob_data(msg), blob_len(msg));
1616 state = malloc(sizeof(*state));
1619 return UBUS_STATUS_UNKNOWN_ERROR;
1621 memset(state, 0, sizeof(*state));
1623 if (tb[RPC_OM_PATTERN])
1624 cmd[3] = blobmsg_data(tb[RPC_OM_PATTERN]);
1626 if (tb[RPC_OM_LIMIT])
1627 state->req_count = blobmsg_get_u32(tb[RPC_OM_LIMIT]);
1629 if (tb[RPC_OM_OFFSET])
1630 state->req_offset = blobmsg_get_u32(tb[RPC_OM_OFFSET]);
1632 if (state->req_offset < 0)
1633 state->req_offset = 0;
1635 if (state->req_count <= 0 || state->req_count > 100)
1636 state->req_count = 100;
1638 return rpc_exec(cmd, opkg_parse_list, NULL, opkg_finish_list,
1644 rpc_luci2_opkg_list(struct ubus_context *ctx, struct ubus_object *obj,
1645 struct ubus_request_data *req, const char *method,
1646 struct blob_attr *msg)
1648 return opkg_exec_list("list", msg, ctx, req);
1652 rpc_luci2_opkg_list_installed(struct ubus_context *ctx, struct ubus_object *obj,
1653 struct ubus_request_data *req, const char *method,
1654 struct blob_attr *msg)
1656 return opkg_exec_list("list-installed", msg, ctx, req);
1660 rpc_luci2_opkg_find(struct ubus_context *ctx, struct ubus_object *obj,
1661 struct ubus_request_data *req, const char *method,
1662 struct blob_attr *msg)
1664 return opkg_exec_list("find", msg, ctx, req);
1668 rpc_luci2_opkg_update(struct ubus_context *ctx, struct ubus_object *obj,
1669 struct ubus_request_data *req, const char *method,
1670 struct blob_attr *msg)
1672 const char *cmd[3] = { "opkg", "update", NULL };
1673 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1677 rpc_luci2_opkg_install(struct ubus_context *ctx, struct ubus_object *obj,
1678 struct ubus_request_data *req, const char *method,
1679 struct blob_attr *msg)
1681 struct blob_attr *tb[__RPC_OP_MAX];
1682 const char *cmd[5] = { "opkg", "--force-overwrite",
1683 "install", NULL, NULL };
1685 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1686 blob_data(msg), blob_len(msg));
1688 if (!tb[RPC_OP_PACKAGE])
1689 return UBUS_STATUS_INVALID_ARGUMENT;
1691 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1693 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1697 rpc_luci2_opkg_remove(struct ubus_context *ctx, struct ubus_object *obj,
1698 struct ubus_request_data *req, const char *method,
1699 struct blob_attr *msg)
1701 struct blob_attr *tb[__RPC_OP_MAX];
1702 const char *cmd[5] = { "opkg", "--force-removal-of-dependent-packages",
1703 "remove", NULL, NULL };
1705 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1706 blob_data(msg), blob_len(msg));
1708 if (!tb[RPC_OP_PACKAGE])
1709 return UBUS_STATUS_INVALID_ARGUMENT;
1711 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1713 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1717 rpc_luci2_opkg_config_get(struct ubus_context *ctx, struct ubus_object *obj,
1718 struct ubus_request_data *req, const char *method,
1719 struct blob_attr *msg)
1722 char conf[2048] = { 0 };
1724 if (!(f = fopen("/etc/opkg.conf", "r")))
1725 return rpc_errno_status();
1727 fread(conf, sizeof(conf) - 1, 1, f);
1730 blob_buf_init(&buf, 0);
1731 blobmsg_add_string(&buf, "config", conf);
1733 ubus_send_reply(ctx, req, buf.head);
1738 rpc_luci2_opkg_config_set(struct ubus_context *ctx, struct ubus_object *obj,
1739 struct ubus_request_data *req, const char *method,
1740 struct blob_attr *msg)
1743 struct blob_attr *tb[__RPC_D_MAX];
1745 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
1746 blob_data(msg), blob_len(msg));
1748 if (!tb[RPC_D_DATA])
1749 return UBUS_STATUS_INVALID_ARGUMENT;
1751 if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048)
1752 return UBUS_STATUS_NOT_SUPPORTED;
1754 if (!(f = fopen("/etc/opkg.conf", "w")))
1755 return rpc_errno_status();
1757 fwrite(blobmsg_data(tb[RPC_D_DATA]),
1758 blobmsg_data_len(tb[RPC_D_DATA]), 1, f);
1765 int rpc_luci2_api_init(struct ubus_context *ctx)
1769 static const struct ubus_method luci2_system_methods[] = {
1770 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log),
1771 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg),
1772 UBUS_METHOD_NOARG("diskfree", rpc_luci2_system_diskfree),
1773 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list),
1774 UBUS_METHOD("process_signal", rpc_luci2_process_signal,
1776 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list),
1777 UBUS_METHOD("init_action", rpc_luci2_init_action,
1779 UBUS_METHOD_NOARG("rclocal_get", rpc_luci2_rclocal_get),
1780 UBUS_METHOD("rclocal_set", rpc_luci2_rclocal_set,
1782 UBUS_METHOD_NOARG("crontab_get", rpc_luci2_crontab_get),
1783 UBUS_METHOD("crontab_set", rpc_luci2_crontab_set,
1785 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get),
1786 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set,
1788 UBUS_METHOD("password_set", rpc_luci2_password_set,
1789 rpc_password_policy),
1790 UBUS_METHOD_NOARG("led_list", rpc_luci2_led_list),
1791 UBUS_METHOD_NOARG("usb_list", rpc_luci2_usb_list),
1792 UBUS_METHOD_NOARG("upgrade_test", rpc_luci2_upgrade_test),
1793 UBUS_METHOD("upgrade_start", rpc_luci2_upgrade_start,
1794 rpc_upgrade_policy),
1795 UBUS_METHOD_NOARG("upgrade_clean", rpc_luci2_upgrade_clean),
1796 UBUS_METHOD_NOARG("backup_restore", rpc_luci2_backup_restore),
1797 UBUS_METHOD_NOARG("backup_clean", rpc_luci2_backup_clean)
1800 static struct ubus_object_type luci2_system_type =
1801 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods);
1803 static struct ubus_object system_obj = {
1804 .name = "luci2.system",
1805 .type = &luci2_system_type,
1806 .methods = luci2_system_methods,
1807 .n_methods = ARRAY_SIZE(luci2_system_methods),
1811 static const struct ubus_method luci2_network_methods[] = {
1812 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count),
1813 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table),
1814 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table),
1815 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases),
1816 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6),
1817 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes),
1818 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6),
1821 static struct ubus_object_type luci2_network_type =
1822 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods);
1824 static struct ubus_object network_obj = {
1825 .name = "luci2.network",
1826 .type = &luci2_network_type,
1827 .methods = luci2_network_methods,
1828 .n_methods = ARRAY_SIZE(luci2_network_methods),
1832 static const struct ubus_method luci2_opkg_methods[] = {
1833 UBUS_METHOD("list", rpc_luci2_opkg_list,
1834 rpc_opkg_match_policy),
1835 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed,
1836 rpc_opkg_match_policy),
1837 UBUS_METHOD("find", rpc_luci2_opkg_find,
1838 rpc_opkg_match_policy),
1839 UBUS_METHOD("install", rpc_luci2_opkg_install,
1840 rpc_opkg_package_policy),
1841 UBUS_METHOD("remove", rpc_luci2_opkg_remove,
1842 rpc_opkg_package_policy),
1843 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update),
1844 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get),
1845 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set,
1849 static struct ubus_object_type luci2_opkg_type =
1850 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods);
1852 static struct ubus_object opkg_obj = {
1853 .name = "luci2.opkg",
1854 .type = &luci2_opkg_type,
1855 .methods = luci2_opkg_methods,
1856 .n_methods = ARRAY_SIZE(luci2_opkg_methods),
1859 cursor = uci_alloc_context();
1862 return UBUS_STATUS_UNKNOWN_ERROR;
1864 rv |= ubus_add_object(ctx, &system_obj);
1865 rv |= ubus_add_object(ctx, &network_obj);
1866 rv |= ubus_add_object(ctx, &opkg_obj);