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>
37 static const struct rpc_daemon_ops
*ops
;
39 static struct blob_buf buf
;
40 static struct uci_context
*cursor
;
48 static const struct blobmsg_policy rpc_signal_policy
[__RPC_S_MAX
] = {
49 [RPC_S_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
50 [RPC_S_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
59 static const struct blobmsg_policy rpc_init_policy
[__RPC_I_MAX
] = {
60 [RPC_I_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
61 [RPC_I_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_STRING
},
69 static const struct blobmsg_policy rpc_data_policy
[__RPC_D_MAX
] = {
70 [RPC_D_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_STRING
},
78 static const struct blobmsg_policy rpc_sshkey_policy
[__RPC_K_MAX
] = {
79 [RPC_K_KEYS
] = { .name
= "keys", .type
= BLOBMSG_TYPE_ARRAY
},
88 static const struct blobmsg_policy rpc_password_policy
[__RPC_P_MAX
] = {
89 [RPC_P_USER
] = { .name
= "user", .type
= BLOBMSG_TYPE_STRING
},
90 [RPC_P_PASSWORD
] = { .name
= "password", .type
= BLOBMSG_TYPE_STRING
},
100 static const struct blobmsg_policy rpc_opkg_match_policy
[__RPC_OM_MAX
] = {
101 [RPC_OM_LIMIT
] = { .name
= "limit", .type
= BLOBMSG_TYPE_INT32
},
102 [RPC_OM_OFFSET
] = { .name
= "offset", .type
= BLOBMSG_TYPE_INT32
},
103 [RPC_OM_PATTERN
] = { .name
= "pattern", .type
= BLOBMSG_TYPE_STRING
},
111 static const struct blobmsg_policy rpc_opkg_package_policy
[__RPC_OP_MAX
] = {
112 [RPC_OP_PACKAGE
] = { .name
= "package", .type
= BLOBMSG_TYPE_STRING
},
120 static const struct blobmsg_policy rpc_upgrade_policy
[__RPC_UPGRADE_MAX
] = {
121 [RPC_UPGRADE_KEEP
] = { .name
= "keep", .type
= BLOBMSG_TYPE_BOOL
},
129 static const struct blobmsg_policy rpc_menu_policy
[__RPC_MENU_MAX
] = {
130 [RPC_MENU_SESSION
] = { .name
= "ubus_rpc_session",
131 .type
= BLOBMSG_TYPE_STRING
},
136 rpc_errno_status(void)
141 return UBUS_STATUS_PERMISSION_DENIED
;
144 return UBUS_STATUS_INVALID_ARGUMENT
;
147 return UBUS_STATUS_NOT_FOUND
;
150 return UBUS_STATUS_INVALID_ARGUMENT
;
153 return UBUS_STATUS_UNKNOWN_ERROR
;
158 log_read(FILE *log
, int logsize
)
164 logsize
= RPC_LUCI2_DEF_LOGSIZE
;
166 len
= (logsize
> RPC_LUCI2_MAX_LOGSIZE
) ? RPC_LUCI2_MAX_LOGSIZE
: logsize
;
167 logbuf
= blobmsg_alloc_string_buffer(&buf
, "log", len
+ 1);
172 while (logsize
> RPC_LUCI2_MAX_LOGSIZE
)
174 len
= logsize
% RPC_LUCI2_MAX_LOGSIZE
;
177 len
= RPC_LUCI2_MAX_LOGSIZE
;
179 fread(logbuf
, 1, len
, log
);
183 len
= fread(logbuf
, 1, logsize
, log
);
186 blobmsg_add_string_buffer(&buf
);
190 rpc_luci2_system_log(struct ubus_context
*ctx
, struct ubus_object
*obj
,
191 struct ubus_request_data
*req
, const char *method
,
192 struct blob_attr
*msg
)
196 const char *logfile
= NULL
;
198 struct uci_package
*p
;
199 struct uci_element
*e
;
200 struct uci_section
*s
;
201 struct uci_ptr ptr
= { .package
= "system" };
203 uci_load(cursor
, ptr
.package
, &p
);
206 return UBUS_STATUS_NOT_FOUND
;
208 uci_foreach_element(&p
->sections
, e
)
210 s
= uci_to_section(e
);
212 if (strcmp(s
->type
, "system"))
216 ptr
.option
= "log_type";
217 ptr
.section
= e
->name
;
218 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
222 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
&&
223 !strcmp(ptr
.o
->v
.string
, "file"))
226 ptr
.option
= "log_file";
227 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
229 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
)
230 logfile
= ptr
.o
->v
.string
;
232 logfile
= "/var/log/messages";
234 if (stat(logfile
, &st
) || !(log
= fopen(logfile
, "r")))
237 logsize
= st
.st_size
;
242 ptr
.option
= "log_size";
243 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
245 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
)
246 logsize
= atoi(ptr
.o
->v
.string
) * 1024;
248 if (!(log
= popen("logread", "r")))
252 blob_buf_init(&buf
, 0);
254 log_read(log
, logsize
);
257 uci_unload(cursor
, p
);
258 ubus_send_reply(ctx
, req
, buf
.head
);
262 uci_unload(cursor
, p
);
263 return rpc_errno_status();
267 rpc_luci2_system_dmesg(struct ubus_context
*ctx
, struct ubus_object
*obj
,
268 struct ubus_request_data
*req
, const char *method
,
269 struct blob_attr
*msg
)
273 if (!(log
= popen("dmesg", "r")))
274 return rpc_errno_status();
276 blob_buf_init(&buf
, 0);
278 log_read(log
, RPC_LUCI2_MAX_LOGSIZE
);
281 ubus_send_reply(ctx
, req
, buf
.head
);
286 rpc_luci2_system_diskfree(struct ubus_context
*ctx
, struct ubus_object
*obj
,
287 struct ubus_request_data
*req
, const char *method
,
288 struct blob_attr
*msg
)
293 const char *fslist
[] = {
298 blob_buf_init(&buf
, 0);
300 for (i
= 0; i
< sizeof(fslist
) / sizeof(fslist
[0]); i
+= 2)
302 if (statvfs(fslist
[i
], &s
))
305 c
= blobmsg_open_table(&buf
, fslist
[i
+1]);
307 blobmsg_add_u32(&buf
, "total", s
.f_blocks
* s
.f_frsize
);
308 blobmsg_add_u32(&buf
, "free", s
.f_bfree
* s
.f_frsize
);
309 blobmsg_add_u32(&buf
, "used", (s
.f_blocks
- s
.f_bfree
) * s
.f_frsize
);
311 blobmsg_close_table(&buf
, c
);
314 ubus_send_reply(ctx
, req
, buf
.head
);
319 rpc_luci2_process_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
320 struct ubus_request_data
*req
, const char *method
,
321 struct blob_attr
*msg
)
326 char *pid
, *ppid
, *user
, *stat
, *vsz
, *pvsz
, *pcpu
, *cmd
;
328 if (!(top
= popen("/bin/busybox top -bn1", "r")))
329 return rpc_errno_status();
331 blob_buf_init(&buf
, 0);
332 c
= blobmsg_open_array(&buf
, "processes");
334 while (fgets(line
, sizeof(line
) - 1, top
))
336 pid
= strtok(line
, " ");
338 if (*pid
< '0' || *pid
> '9')
341 ppid
= strtok(NULL
, " ");
342 user
= strtok(NULL
, " ");
343 stat
= strtok(NULL
, " ");
356 vsz
= strtok(stat
+ 4, " ");
357 pvsz
= strtok(NULL
, " ");
358 pcpu
= strtok(NULL
, " ");
359 cmd
= strtok(NULL
, "\n");
364 d
= blobmsg_open_table(&buf
, NULL
);
366 blobmsg_add_u32(&buf
, "pid", atoi(pid
));
367 blobmsg_add_u32(&buf
, "ppid", atoi(ppid
));
368 blobmsg_add_string(&buf
, "user", user
);
369 blobmsg_add_string(&buf
, "stat", stat
);
370 blobmsg_add_u32(&buf
, "vsize", atoi(vsz
) * 1024);
371 blobmsg_add_u32(&buf
, "vsize_percent", atoi(pvsz
));
372 blobmsg_add_u32(&buf
, "cpu_percent", atoi(pcpu
));
373 blobmsg_add_string(&buf
, "command", cmd
);
375 blobmsg_close_table(&buf
, d
);
379 blobmsg_close_array(&buf
, c
);
381 ubus_send_reply(ctx
, req
, buf
.head
);
386 rpc_luci2_process_signal(struct ubus_context
*ctx
, struct ubus_object
*obj
,
387 struct ubus_request_data
*req
, const char *method
,
388 struct blob_attr
*msg
)
391 struct blob_attr
*tb
[__RPC_S_MAX
];
393 blobmsg_parse(rpc_signal_policy
, __RPC_S_MAX
, tb
,
394 blob_data(msg
), blob_len(msg
));
396 if (!tb
[RPC_S_SIGNAL
] || !tb
[RPC_S_PID
])
399 return rpc_errno_status();
402 pid
= blobmsg_get_u32(tb
[RPC_S_PID
]);
403 sig
= blobmsg_get_u32(tb
[RPC_S_SIGNAL
]);
406 return rpc_errno_status();
412 rpc_luci2_init_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
413 struct ubus_request_data
*req
, const char *method
,
414 struct blob_attr
*msg
)
418 char *p
, path
[PATH_MAX
];
424 if (!(d
= opendir("/etc/init.d")))
425 return rpc_errno_status();
427 blob_buf_init(&buf
, 0);
428 c
= blobmsg_open_array(&buf
, "initscripts");
430 while ((e
= readdir(d
)) != NULL
)
432 snprintf(path
, sizeof(path
) - 1, "/etc/init.d/%s", e
->d_name
);
434 if (stat(path
, &s
) || !S_ISREG(s
.st_mode
) || !(s
.st_mode
& S_IXUSR
))
437 if ((f
= fopen(path
, "r")) != NULL
)
440 p
= fgets(path
, sizeof(path
) - 1, f
);
442 if (!p
|| !strstr(p
, "/etc/rc.common"))
445 t
= blobmsg_open_table(&buf
, NULL
);
447 blobmsg_add_string(&buf
, "name", e
->d_name
);
449 while (fgets(path
, sizeof(path
) - 1, f
))
451 p
= strtok(path
, "= \t");
453 if (!strcmp(p
, "START") && !!(p
= strtok(NULL
, "= \t\n")))
456 blobmsg_add_u32(&buf
, "start", n
);
458 else if (!strcmp(p
, "STOP") && !!(p
= strtok(NULL
, "= \t\n")))
460 blobmsg_add_u32(&buf
, "stop", atoi(p
));
467 snprintf(path
, sizeof(path
) - 1, "/etc/rc.d/S%02d%s",
470 blobmsg_add_u8(&buf
, "enabled",
471 (!stat(path
, &s
) && (s
.st_mode
& S_IXUSR
)));
475 blobmsg_add_u8(&buf
, "enabled", 0);
478 blobmsg_close_table(&buf
, t
);
486 blobmsg_close_array(&buf
, c
);
488 ubus_send_reply(ctx
, req
, buf
.head
);
493 rpc_luci2_init_action(struct ubus_context
*ctx
, struct ubus_object
*obj
,
494 struct ubus_request_data
*req
, const char *method
,
495 struct blob_attr
*msg
)
502 struct blob_attr
*tb
[__RPC_I_MAX
];
504 blobmsg_parse(rpc_init_policy
, __RPC_I_MAX
, tb
,
505 blob_data(msg
), blob_len(msg
));
507 if (!tb
[RPC_I_NAME
] || !tb
[RPC_I_ACTION
])
508 return UBUS_STATUS_INVALID_ARGUMENT
;
510 action
= blobmsg_data(tb
[RPC_I_ACTION
]);
512 if (strcmp(action
, "start") && strcmp(action
, "stop") &&
513 strcmp(action
, "reload") && strcmp(action
, "restart") &&
514 strcmp(action
, "enable") && strcmp(action
, "disable"))
515 return UBUS_STATUS_INVALID_ARGUMENT
;
517 snprintf(path
, sizeof(path
) - 1, "/etc/init.d/%s",
518 (char *)blobmsg_data(tb
[RPC_I_NAME
]));
521 return rpc_errno_status();
523 if (!(s
.st_mode
& S_IXUSR
))
524 return UBUS_STATUS_PERMISSION_DENIED
;
526 switch ((pid
= fork()))
529 return rpc_errno_status();
534 if ((fd
= open("/dev/null", O_RDWR
)) > -1)
545 if (execl(path
, path
, action
, NULL
))
546 return rpc_errno_status();
554 rpc_luci2_rclocal_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
555 struct ubus_request_data
*req
, const char *method
,
556 struct blob_attr
*msg
)
559 char data
[4096] = { 0 };
561 if (!(f
= fopen("/etc/rc.local", "r")))
562 return rpc_errno_status();
564 fread(data
, sizeof(data
) - 1, 1, f
);
567 blob_buf_init(&buf
, 0);
568 blobmsg_add_string(&buf
, "data", data
);
570 ubus_send_reply(ctx
, req
, buf
.head
);
575 rpc_luci2_rclocal_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
576 struct ubus_request_data
*req
, const char *method
,
577 struct blob_attr
*msg
)
580 struct blob_attr
*tb
[__RPC_D_MAX
];
582 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
583 blob_data(msg
), blob_len(msg
));
585 if (!tb
[RPC_D_DATA
] || blobmsg_data_len(tb
[RPC_D_DATA
]) >= 4096)
586 return UBUS_STATUS_INVALID_ARGUMENT
;
588 if (!(f
= fopen("/etc/rc.local", "w")))
589 return rpc_errno_status();
591 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
592 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
599 rpc_luci2_crontab_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
600 struct ubus_request_data
*req
, const char *method
,
601 struct blob_attr
*msg
)
604 char data
[4096] = { 0 };
606 if (!(f
= fopen("/etc/crontabs/root", "r")))
607 return rpc_errno_status();
609 fread(data
, sizeof(data
) - 1, 1, f
);
612 blob_buf_init(&buf
, 0);
613 blobmsg_add_string(&buf
, "data", data
);
615 ubus_send_reply(ctx
, req
, buf
.head
);
620 rpc_luci2_crontab_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
621 struct ubus_request_data
*req
, const char *method
,
622 struct blob_attr
*msg
)
626 struct blob_attr
*tb
[__RPC_D_MAX
];
628 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
629 blob_data(msg
), blob_len(msg
));
631 if (!tb
[RPC_D_DATA
] || blobmsg_data_len(tb
[RPC_D_DATA
]) >= 4096)
632 return UBUS_STATUS_INVALID_ARGUMENT
;
634 if (stat("/etc/crontabs", &s
) && mkdir("/etc/crontabs", 0755))
635 return rpc_errno_status();
637 if (!(f
= fopen("/etc/crontabs/root", "w")))
638 return rpc_errno_status();
640 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
641 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
648 rpc_luci2_sshkeys_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
649 struct ubus_request_data
*req
, const char *method
,
650 struct blob_attr
*msg
)
656 if (!(f
= fopen("/etc/dropbear/authorized_keys", "r")))
657 return rpc_errno_status();
659 blob_buf_init(&buf
, 0);
660 c
= blobmsg_open_array(&buf
, "keys");
662 while (fgets(line
, sizeof(line
) - 1, f
))
664 for (p
= line
+ strlen(line
) - 1; (p
> line
) && isspace(*p
); p
--)
667 for (p
= line
; isspace(*p
); p
++)
671 blobmsg_add_string(&buf
, NULL
, p
);
674 blobmsg_close_array(&buf
, c
);
677 ubus_send_reply(ctx
, req
, buf
.head
);
682 rpc_luci2_sshkeys_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
683 struct ubus_request_data
*req
, const char *method
,
684 struct blob_attr
*msg
)
688 struct blob_attr
*cur
, *tb
[__RPC_K_MAX
];
690 blobmsg_parse(rpc_sshkey_policy
, __RPC_K_MAX
, tb
,
691 blob_data(msg
), blob_len(msg
));
694 return UBUS_STATUS_INVALID_ARGUMENT
;
696 if (!(f
= fopen("/etc/dropbear/authorized_keys", "w")))
697 return rpc_errno_status();
699 blobmsg_for_each_attr(cur
, tb
[RPC_K_KEYS
], rem
)
701 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
704 fwrite(blobmsg_data(cur
), blobmsg_data_len(cur
) - 1, 1, f
);
705 fwrite("\n", 1, 1, f
);
713 rpc_luci2_password_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
714 struct ubus_request_data
*req
, const char *method
,
715 struct blob_attr
*msg
)
720 struct blob_attr
*tb
[__RPC_P_MAX
];
722 blobmsg_parse(rpc_password_policy
, __RPC_P_MAX
, tb
,
723 blob_data(msg
), blob_len(msg
));
725 if (!tb
[RPC_P_USER
] || !tb
[RPC_P_PASSWORD
])
726 return UBUS_STATUS_INVALID_ARGUMENT
;
728 if (stat("/usr/bin/passwd", &s
))
729 return UBUS_STATUS_NOT_FOUND
;
731 if (!(s
.st_mode
& S_IXUSR
))
732 return UBUS_STATUS_PERMISSION_DENIED
;
735 return rpc_errno_status();
737 switch ((pid
= fork()))
742 return rpc_errno_status();
751 if ((fd
= open("/dev/null", O_RDWR
)) > -1)
760 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
761 blobmsg_data(tb
[RPC_P_USER
]), NULL
))
762 return rpc_errno_status();
767 write(fds
[1], blobmsg_data(tb
[RPC_P_PASSWORD
]),
768 blobmsg_data_len(tb
[RPC_P_PASSWORD
]) - 1);
769 write(fds
[1], "\n", 1);
773 write(fds
[1], blobmsg_data(tb
[RPC_P_PASSWORD
]),
774 blobmsg_data_len(tb
[RPC_P_PASSWORD
]) - 1);
775 write(fds
[1], "\n", 1);
779 waitpid(pid
, NULL
, 0);
786 rpc_luci2_led_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
787 struct ubus_request_data
*req
, const char *method
,
788 struct blob_attr
*msg
)
792 void *list
, *led
, *trigger
;
793 char *p
, *active_trigger
, line
[512];
796 if (!(d
= opendir("/sys/class/leds")))
797 return rpc_errno_status();
799 blob_buf_init(&buf
, 0);
800 list
= blobmsg_open_array(&buf
, "leds");
802 while ((e
= readdir(d
)) != NULL
)
804 snprintf(line
, sizeof(line
) - 1, "/sys/class/leds/%s/trigger",
807 if (!(f
= fopen(line
, "r")))
810 led
= blobmsg_open_table(&buf
, NULL
);
812 blobmsg_add_string(&buf
, "name", e
->d_name
);
814 if (fgets(line
, sizeof(line
) - 1, f
))
816 trigger
= blobmsg_open_array(&buf
, "triggers");
818 for (p
= strtok(line
, " \n"), active_trigger
= NULL
;
820 p
= strtok(NULL
, " \n"))
824 *(p
+ strlen(p
) - 1) = 0;
829 blobmsg_add_string(&buf
, NULL
, p
);
832 blobmsg_close_array(&buf
, trigger
);
835 blobmsg_add_string(&buf
, "active_trigger", active_trigger
);
840 snprintf(line
, sizeof(line
) - 1, "/sys/class/leds/%s/brightness",
843 if ((f
= fopen(line
, "r")) != NULL
)
845 if (fgets(line
, sizeof(line
) - 1, f
))
846 blobmsg_add_u32(&buf
, "brightness", atoi(line
));
851 snprintf(line
, sizeof(line
) - 1, "/sys/class/leds/%s/max_brightness",
854 if ((f
= fopen(line
, "r")) != NULL
)
856 if (fgets(line
, sizeof(line
) - 1, f
))
857 blobmsg_add_u32(&buf
, "max_brightness", atoi(line
));
862 blobmsg_close_table(&buf
, led
);
867 blobmsg_close_array(&buf
, list
);
868 ubus_send_reply(ctx
, req
, buf
.head
);
874 rpc_luci2_usb_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
875 struct ubus_request_data
*req
, const char *method
,
876 struct blob_attr
*msg
)
886 const char *attributes
[] = {
887 "manufacturer", "vendor_name", "s",
888 "product", "product_name", "s",
889 "idVendor", "vendor_id", "x",
890 "idProduct", "product_id", "x",
891 "serial", "serial", "s",
892 "speed", "speed", "d",
895 if (!(d
= opendir("/sys/bus/usb/devices")))
896 return rpc_errno_status();
898 blob_buf_init(&buf
, 0);
899 list
= blobmsg_open_array(&buf
, "devices");
901 while ((e
= readdir(d
)) != NULL
)
903 if (e
->d_name
[0] < '0' || e
->d_name
[0] > '9')
906 snprintf(line
, sizeof(line
) - 1,
907 "/sys/bus/usb/devices/%s/%s", e
->d_name
, attributes
[0]);
912 device
= blobmsg_open_table(&buf
, NULL
);
914 blobmsg_add_string(&buf
, "name", e
->d_name
);
916 for (i
= 0; i
< sizeof(attributes
) / sizeof(attributes
[0]); i
+= 3)
918 snprintf(line
, sizeof(line
) - 1,
919 "/sys/bus/usb/devices/%s/%s", e
->d_name
, attributes
[i
]);
921 if (!(f
= fopen(line
, "r")))
924 if (fgets(line
, sizeof(line
) - 1, f
))
926 switch (*attributes
[i
+2])
929 blobmsg_add_u32(&buf
, attributes
[i
+1],
930 strtoul(line
, NULL
, 16));
934 blobmsg_add_u32(&buf
, attributes
[i
+1],
935 strtoul(line
, NULL
, 10));
939 if ((p
= strchr(line
, '\n')) != NULL
)
940 while (p
> line
&& isspace(*p
))
943 blobmsg_add_string(&buf
, attributes
[i
+1], line
);
951 blobmsg_close_table(&buf
, device
);
956 blobmsg_close_array(&buf
, list
);
957 ubus_send_reply(ctx
, req
, buf
.head
);
963 rpc_luci2_upgrade_test(struct ubus_context
*ctx
, struct ubus_object
*obj
,
964 struct ubus_request_data
*req
, const char *method
,
965 struct blob_attr
*msg
)
967 const char *cmd
[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL
};
968 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
972 rpc_luci2_upgrade_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
973 struct ubus_request_data
*req
, const char *method
,
974 struct blob_attr
*msg
)
980 rpc_luci2_upgrade_clean(struct ubus_context
*ctx
, struct ubus_object
*obj
,
981 struct ubus_request_data
*req
, const char *method
,
982 struct blob_attr
*msg
)
984 if (unlink("/tmp/firmware.bin"))
985 return rpc_errno_status();
991 rpc_luci2_backup_restore(struct ubus_context
*ctx
, struct ubus_object
*obj
,
992 struct ubus_request_data
*req
, const char *method
,
993 struct blob_attr
*msg
)
995 const char *cmd
[4] = { "sysupgrade", "--restore-backup",
996 "/tmp/backup.tar.gz", NULL
};
998 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1002 rpc_luci2_backup_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/backup.tar.gz"))
1007 return rpc_errno_status();
1013 rpc_luci2_backup_config_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1014 struct ubus_request_data
*req
, const char *method
,
1015 struct blob_attr
*msg
)
1018 char conf
[2048] = { 0 };
1020 if (!(f
= fopen("/etc/sysupgrade.conf", "r")))
1021 return rpc_errno_status();
1023 fread(conf
, sizeof(conf
) - 1, 1, f
);
1026 blob_buf_init(&buf
, 0);
1027 blobmsg_add_string(&buf
, "config", conf
);
1029 ubus_send_reply(ctx
, req
, buf
.head
);
1034 rpc_luci2_backup_config_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1035 struct ubus_request_data
*req
, const char *method
,
1036 struct blob_attr
*msg
)
1039 struct blob_attr
*tb
[__RPC_D_MAX
];
1041 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
1042 blob_data(msg
), blob_len(msg
));
1044 if (!tb
[RPC_D_DATA
])
1045 return UBUS_STATUS_INVALID_ARGUMENT
;
1047 if (blobmsg_data_len(tb
[RPC_D_DATA
]) >= 2048)
1048 return UBUS_STATUS_NOT_SUPPORTED
;
1050 if (!(f
= fopen("/etc/sysupgrade.conf", "w")))
1051 return rpc_errno_status();
1053 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
1054 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
1060 struct backup_state
{
1066 backup_parse_list(struct blob_buf
*blob
, char *buf
, int len
, void *priv
)
1068 struct backup_state
*s
= priv
;
1069 char *nl
= strchr(buf
, '\n');
1077 s
->array
= blobmsg_open_array(blob
, "files");
1081 blobmsg_add_string(blob
, NULL
, buf
);
1083 return (nl
- buf
+ 1);
1087 backup_finish_list(struct blob_buf
*blob
, int status
, void *priv
)
1089 struct backup_state
*s
= priv
;
1092 return UBUS_STATUS_NO_DATA
;
1094 blobmsg_close_array(blob
, s
->array
);
1096 return UBUS_STATUS_OK
;
1100 rpc_luci2_backup_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1101 struct ubus_request_data
*req
, const char *method
,
1102 struct blob_attr
*msg
)
1104 struct backup_state
*state
= NULL
;
1105 const char *cmd
[3] = { "sysupgrade", "--list-backup", NULL
};
1107 state
= malloc(sizeof(*state
));
1110 return UBUS_STATUS_UNKNOWN_ERROR
;
1112 memset(state
, 0, sizeof(*state
));
1114 return ops
->exec(cmd
, NULL
, backup_parse_list
, NULL
, backup_finish_list
,
1119 rpc_luci2_reset_test(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1120 struct ubus_request_data
*req
, const char *method
,
1121 struct blob_attr
*msg
)
1125 char line
[64] = { 0 };
1126 bool supported
= false;
1128 if (!stat("/sbin/mtd", &s
) && (s
.st_mode
& S_IXUSR
))
1130 if ((mtd
= fopen("/proc/mtd", "r")) != NULL
)
1132 while (fgets(line
, sizeof(line
) - 1, mtd
))
1134 if (strstr(line
, "\"rootfs_data\""))
1145 blob_buf_init(&buf
, 0);
1146 blobmsg_add_u8(&buf
, "supported", supported
);
1148 ubus_send_reply(ctx
, req
, buf
.head
);
1154 rpc_luci2_reset_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1155 struct ubus_request_data
*req
, const char *method
,
1156 struct blob_attr
*msg
)
1161 return rpc_errno_status();
1174 execl("/sbin/mtd", "/sbin/mtd", "-r", "erase", "rootfs_data", NULL
);
1176 return rpc_errno_status();
1184 rpc_luci2_reboot(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1185 struct ubus_request_data
*req
, const char *method
,
1186 struct blob_attr
*msg
)
1191 return rpc_errno_status();
1202 execl("/sbin/reboot", "/sbin/reboot", NULL
);
1204 return rpc_errno_status();
1213 dnsmasq_leasefile(void)
1215 FILE *leases
= NULL
;
1216 struct uci_package
*p
;
1217 struct uci_element
*e
;
1218 struct uci_section
*s
;
1219 struct uci_ptr ptr
= {
1222 .option
= "leasefile"
1225 uci_load(cursor
, ptr
.package
, &p
);
1230 uci_foreach_element(&p
->sections
, e
)
1232 s
= uci_to_section(e
);
1234 if (strcmp(s
->type
, "dnsmasq"))
1237 ptr
.section
= e
->name
;
1238 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
1242 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
)
1243 leases
= fopen(ptr
.o
->v
.string
, "r");
1245 uci_unload(cursor
, p
);
1251 rpc_luci2_network_leases(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1252 struct ubus_request_data
*req
, const char *method
,
1253 struct blob_attr
*msg
)
1258 char *ts
, *mac
, *addr
, *name
;
1259 time_t now
= time(NULL
);
1261 blob_buf_init(&buf
, 0);
1262 c
= blobmsg_open_array(&buf
, "leases");
1264 leases
= dnsmasq_leasefile();
1269 while (fgets(line
, sizeof(line
) - 1, leases
))
1271 ts
= strtok(line
, " \t");
1272 mac
= strtok(NULL
, " \t");
1273 addr
= strtok(NULL
, " \t");
1274 name
= strtok(NULL
, " \t");
1276 if (!ts
|| !mac
|| !addr
|| !name
)
1279 if (strchr(addr
, ':'))
1282 d
= blobmsg_open_table(&buf
, NULL
);
1284 blobmsg_add_u32(&buf
, "expires", atoi(ts
) - now
);
1285 blobmsg_add_string(&buf
, "macaddr", mac
);
1286 blobmsg_add_string(&buf
, "ipaddr", addr
);
1288 if (strcmp(name
, "*"))
1289 blobmsg_add_string(&buf
, "hostname", name
);
1291 blobmsg_close_table(&buf
, d
);
1297 blobmsg_close_array(&buf
, c
);
1298 ubus_send_reply(ctx
, req
, buf
.head
);
1304 rpc_luci2_network_leases6(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1305 struct ubus_request_data
*req
, const char *method
,
1306 struct blob_attr
*msg
)
1311 char *ts
, *mac
, *addr
, *name
, *duid
;
1312 time_t now
= time(NULL
);
1314 blob_buf_init(&buf
, 0);
1315 c
= blobmsg_open_array(&buf
, "leases");
1317 leases
= fopen("/tmp/hosts/6relayd", "r");
1321 while (fgets(line
, sizeof(line
) - 1, leases
))
1323 if (strncmp(line
, "# ", 2))
1326 strtok(line
+ 2, " \t"); /* iface */
1328 duid
= strtok(NULL
, " \t");
1330 strtok(NULL
, " \t"); /* iaid */
1332 name
= strtok(NULL
, " \t");
1333 ts
= strtok(NULL
, " \t");
1335 strtok(NULL
, " \t"); /* id */
1336 strtok(NULL
, " \t"); /* length */
1338 addr
= strtok(NULL
, " \t\n");
1343 d
= blobmsg_open_table(&buf
, NULL
);
1345 blobmsg_add_u32(&buf
, "expires", atoi(ts
) - now
);
1346 blobmsg_add_string(&buf
, "duid", duid
);
1347 blobmsg_add_string(&buf
, "ip6addr", addr
);
1349 if (strcmp(name
, "-"))
1350 blobmsg_add_string(&buf
, "hostname", name
);
1352 blobmsg_close_array(&buf
, d
);
1359 leases
= dnsmasq_leasefile();
1364 while (fgets(line
, sizeof(line
) - 1, leases
))
1366 ts
= strtok(line
, " \t");
1367 mac
= strtok(NULL
, " \t");
1368 addr
= strtok(NULL
, " \t");
1369 name
= strtok(NULL
, " \t");
1370 duid
= strtok(NULL
, " \t\n");
1372 if (!ts
|| !mac
|| !addr
|| !duid
)
1375 if (!strchr(addr
, ':'))
1378 d
= blobmsg_open_table(&buf
, NULL
);
1380 blobmsg_add_u32(&buf
, "expires", atoi(ts
) - now
);
1381 blobmsg_add_string(&buf
, "macaddr", mac
);
1382 blobmsg_add_string(&buf
, "ip6addr", addr
);
1384 if (strcmp(name
, "*"))
1385 blobmsg_add_string(&buf
, "hostname", name
);
1387 if (strcmp(duid
, "*"))
1388 blobmsg_add_string(&buf
, "duid", name
);
1390 blobmsg_close_table(&buf
, d
);
1397 blobmsg_close_array(&buf
, c
);
1398 ubus_send_reply(ctx
, req
, buf
.head
);
1404 rpc_luci2_network_ct_count(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1405 struct ubus_request_data
*req
, const char *method
,
1406 struct blob_attr
*msg
)
1411 blob_buf_init(&buf
, 0);
1413 if ((f
= fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL
)
1415 if (fgets(line
, sizeof(line
) - 1, f
))
1416 blobmsg_add_u32(&buf
, "count", atoi(line
));
1421 if ((f
= fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL
)
1423 if (fgets(line
, sizeof(line
) - 1, f
))
1424 blobmsg_add_u32(&buf
, "limit", atoi(line
));
1429 ubus_send_reply(ctx
, req
, buf
.head
);
1435 rpc_luci2_network_ct_table(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1436 struct ubus_request_data
*req
, const char *method
,
1437 struct blob_attr
*msg
)
1445 blob_buf_init(&buf
, 0);
1446 c
= blobmsg_open_array(&buf
, "entries");
1448 if ((f
= fopen("/proc/net/nf_conntrack", "r")) != NULL
)
1450 while (fgets(line
, sizeof(line
) - 1, f
))
1452 d
= blobmsg_open_table(&buf
, NULL
);
1453 memset(seen
, 0, sizeof(seen
));
1455 for (i
= 0, p
= strtok(line
, " "); p
; i
++, p
= strtok(NULL
, " "))
1458 blobmsg_add_u8(&buf
, "ipv6", !strcmp(p
, "ipv6"));
1460 blobmsg_add_u32(&buf
, "protocol", atoi(p
));
1462 blobmsg_add_u32(&buf
, "expires", atoi(p
));
1468 if (!seen
[0] && !strncmp(p
, "src=", 4))
1470 blobmsg_add_string(&buf
, "src", p
+ 4);
1473 else if (!seen
[1] && !strncmp(p
, "dst=", 4))
1475 blobmsg_add_string(&buf
, "dest", p
+ 4);
1478 else if (!seen
[2] && !strncmp(p
, "sport=", 6))
1480 blobmsg_add_u32(&buf
, "sport", atoi(p
+ 6));
1483 else if (!seen
[3] && !strncmp(p
, "dport=", 6))
1485 blobmsg_add_u32(&buf
, "dport", atoi(p
+ 6));
1488 else if (!strncmp(p
, "packets=", 8))
1490 blobmsg_add_u32(&buf
,
1491 seen
[4] ? "tx_packets" : "rx_packets",
1495 else if (!strncmp(p
, "bytes=", 6))
1497 blobmsg_add_u32(&buf
,
1498 seen
[5] ? "tx_bytes" : "rx_bytes",
1505 blobmsg_close_table(&buf
, d
);
1511 blobmsg_close_array(&buf
, c
);
1512 ubus_send_reply(ctx
, req
, buf
.head
);
1518 rpc_luci2_network_arp_table(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1519 struct ubus_request_data
*req
, const char *method
,
1520 struct blob_attr
*msg
)
1524 char *addr
, *mac
, *dev
, line
[128];
1526 blob_buf_init(&buf
, 0);
1527 c
= blobmsg_open_array(&buf
, "entries");
1529 if ((f
= fopen("/proc/net/arp", "r")) != NULL
)
1531 /* skip header line */
1532 fgets(line
, sizeof(line
) - 1, f
);
1534 while (fgets(line
, sizeof(line
) - 1, f
))
1536 addr
= strtok(line
, " \t");
1538 strtok(NULL
, " \t"); /* HW type */
1539 strtok(NULL
, " \t"); /* Flags */
1541 mac
= strtok(NULL
, " \t");
1543 strtok(NULL
, " \t"); /* Mask */
1545 dev
= strtok(NULL
, " \t\n");
1550 d
= blobmsg_open_table(&buf
, NULL
);
1551 blobmsg_add_string(&buf
, "ipaddr", addr
);
1552 blobmsg_add_string(&buf
, "macaddr", mac
);
1553 blobmsg_add_string(&buf
, "device", dev
);
1554 blobmsg_close_table(&buf
, d
);
1560 blobmsg_close_array(&buf
, c
);
1561 ubus_send_reply(ctx
, req
, buf
.head
);
1567 put_hexaddr(const char *name
, const char *s
, const char *m
)
1571 char as
[sizeof("255.255.255.255/32\0")];
1573 a
.s_addr
= strtoul(s
, NULL
, 16);
1574 inet_ntop(AF_INET
, &a
, as
, sizeof(as
));
1578 for (a
.s_addr
= ntohl(strtoul(m
, NULL
, 16)), bits
= 0;
1579 a
.s_addr
& 0x80000000;
1583 sprintf(as
+ strlen(as
), "/%u", bits
);
1586 blobmsg_add_string(&buf
, name
, as
);
1590 rpc_luci2_network_routes(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1591 struct ubus_request_data
*req
, const char *method
,
1592 struct blob_attr
*msg
)
1596 char *dst
, *dmask
, *next
, *metric
, *device
;
1600 if (!(routes
= fopen("/proc/net/route", "r")))
1601 return rpc_errno_status();
1603 blob_buf_init(&buf
, 0);
1604 c
= blobmsg_open_array(&buf
, "routes");
1606 /* skip header line */
1607 fgets(line
, sizeof(line
) - 1, routes
);
1609 while (fgets(line
, sizeof(line
) - 1, routes
))
1611 device
= strtok(line
, "\t ");
1612 dst
= strtok(NULL
, "\t ");
1613 next
= strtok(NULL
, "\t ");
1615 strtok(NULL
, "\t "); /* flags */
1616 strtok(NULL
, "\t "); /* refcount */
1617 strtok(NULL
, "\t "); /* usecount */
1619 metric
= strtok(NULL
, "\t ");
1620 dmask
= strtok(NULL
, "\t ");
1625 d
= blobmsg_open_table(&buf
, NULL
);
1627 put_hexaddr("target", dst
, dmask
);
1628 put_hexaddr("nexthop", next
, NULL
);
1630 n
= strtoul(metric
, NULL
, 10);
1631 blobmsg_add_u32(&buf
, "metric", n
);
1633 blobmsg_add_string(&buf
, "device", device
);
1635 blobmsg_close_table(&buf
, d
);
1638 blobmsg_close_array(&buf
, c
);
1641 ubus_send_reply(ctx
, req
, buf
.head
);
1646 put_hex6addr(const char *name
, const char *s
, const char *m
)
1650 char as
[INET6_ADDRSTRLEN
+ sizeof("/128")];
1653 (((x) <= '9') ? ((x) - '0') : \
1654 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1657 for (i
= 0; i
< 16; i
++, s
+= 2)
1658 a
.s6_addr
[i
] = (16 * hex(*s
)) + hex(*(s
+1));
1660 inet_ntop(AF_INET6
, &a
, as
, sizeof(as
));
1663 sprintf(as
+ strlen(as
), "/%lu", strtoul(m
, NULL
, 16));
1665 blobmsg_add_string(&buf
, name
, as
);
1669 rpc_luci2_network_routes6(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1670 struct ubus_request_data
*req
, const char *method
,
1671 struct blob_attr
*msg
)
1675 char *src
, *smask
, *dst
, *dmask
, *next
, *metric
, *flags
, *device
;
1679 if (!(routes
= fopen("/proc/net/ipv6_route", "r")))
1680 return rpc_errno_status();
1682 blob_buf_init(&buf
, 0);
1683 c
= blobmsg_open_array(&buf
, "routes");
1685 while (fgets(line
, sizeof(line
) - 1, routes
))
1687 dst
= strtok(line
, " ");
1688 dmask
= strtok(NULL
, " ");
1689 src
= strtok(NULL
, " ");
1690 smask
= strtok(NULL
, " ");
1691 next
= strtok(NULL
, " ");
1692 metric
= strtok(NULL
, " ");
1694 strtok(NULL
, " "); /* refcount */
1695 strtok(NULL
, " "); /* usecount */
1697 flags
= strtok(NULL
, " ");
1698 device
= strtok(NULL
, " \n");
1703 n
= strtoul(flags
, NULL
, 16);
1708 d
= blobmsg_open_table(&buf
, NULL
);
1710 put_hex6addr("target", dst
, dmask
);
1711 put_hex6addr("source", src
, smask
);
1712 put_hex6addr("nexthop", next
, NULL
);
1714 n
= strtoul(metric
, NULL
, 16);
1715 blobmsg_add_u32(&buf
, "metric", n
);
1717 blobmsg_add_string(&buf
, "device", device
);
1719 blobmsg_close_table(&buf
, d
);
1722 blobmsg_close_array(&buf
, c
);
1725 ubus_send_reply(ctx
, req
, buf
.head
);
1741 opkg_parse_list(struct blob_buf
*blob
, char *buf
, int len
, void *priv
)
1743 struct opkg_state
*s
= priv
;
1746 char *nl
= strchr(buf
, '\n');
1747 char *name
= NULL
, *vers
= NULL
, *desc
= NULL
;
1755 if (s
->cur_offset
++ < s
->req_offset
)
1758 if (s
->cur_count
++ >= s
->req_count
)
1764 s
->array
= blobmsg_open_array(blob
, "packages");
1767 for (ptr
= buf
, last
= buf
, *nl
= 0; ptr
<= nl
; ptr
++)
1769 if (!*ptr
|| (*ptr
== ' ' && *(ptr
+1) == '-' && *(ptr
+2) == ' '))
1781 desc
= *ptr
? (ptr
+ 3) : NULL
;
1790 c
= blobmsg_open_array(blob
, NULL
);
1792 blobmsg_add_string(blob
, NULL
, name
);
1793 blobmsg_add_string(blob
, NULL
, vers
);
1796 blobmsg_add_string(blob
, NULL
, desc
);
1798 blobmsg_close_array(blob
, c
);
1802 return (nl
- buf
+ 1);
1806 opkg_finish_list(struct blob_buf
*blob
, int status
, void *priv
)
1808 struct opkg_state
*s
= priv
;
1811 return UBUS_STATUS_NO_DATA
;
1813 blobmsg_close_array(blob
, s
->array
);
1814 blobmsg_add_u32(blob
, "total", s
->total
);
1816 return UBUS_STATUS_OK
;
1820 opkg_exec_list(const char *action
, struct blob_attr
*msg
,
1821 struct ubus_context
*ctx
, struct ubus_request_data
*req
)
1823 struct opkg_state
*state
= NULL
;
1824 struct blob_attr
*tb
[__RPC_OM_MAX
];
1825 const char *cmd
[5] = { "opkg", action
, "-nocase", NULL
, NULL
};
1827 blobmsg_parse(rpc_opkg_match_policy
, __RPC_OM_MAX
, tb
,
1828 blob_data(msg
), blob_len(msg
));
1830 state
= malloc(sizeof(*state
));
1833 return UBUS_STATUS_UNKNOWN_ERROR
;
1835 memset(state
, 0, sizeof(*state
));
1837 if (tb
[RPC_OM_PATTERN
])
1838 cmd
[3] = blobmsg_data(tb
[RPC_OM_PATTERN
]);
1840 if (tb
[RPC_OM_LIMIT
])
1841 state
->req_count
= blobmsg_get_u32(tb
[RPC_OM_LIMIT
]);
1843 if (tb
[RPC_OM_OFFSET
])
1844 state
->req_offset
= blobmsg_get_u32(tb
[RPC_OM_OFFSET
]);
1846 if (state
->req_offset
< 0)
1847 state
->req_offset
= 0;
1849 if (state
->req_count
<= 0 || state
->req_count
> 100)
1850 state
->req_count
= 100;
1852 return ops
->exec(cmd
, NULL
, opkg_parse_list
, NULL
, opkg_finish_list
,
1858 rpc_luci2_opkg_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1859 struct ubus_request_data
*req
, const char *method
,
1860 struct blob_attr
*msg
)
1862 return opkg_exec_list("list", msg
, ctx
, req
);
1866 rpc_luci2_opkg_list_installed(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1867 struct ubus_request_data
*req
, const char *method
,
1868 struct blob_attr
*msg
)
1870 return opkg_exec_list("list-installed", msg
, ctx
, req
);
1874 rpc_luci2_opkg_find(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1875 struct ubus_request_data
*req
, const char *method
,
1876 struct blob_attr
*msg
)
1878 return opkg_exec_list("find", msg
, ctx
, req
);
1882 rpc_luci2_opkg_update(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1883 struct ubus_request_data
*req
, const char *method
,
1884 struct blob_attr
*msg
)
1886 const char *cmd
[3] = { "opkg", "update", NULL
};
1887 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1891 rpc_luci2_opkg_install(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1892 struct ubus_request_data
*req
, const char *method
,
1893 struct blob_attr
*msg
)
1895 struct blob_attr
*tb
[__RPC_OP_MAX
];
1896 const char *cmd
[5] = { "opkg", "--force-overwrite",
1897 "install", NULL
, NULL
};
1899 blobmsg_parse(rpc_opkg_package_policy
, __RPC_OP_MAX
, tb
,
1900 blob_data(msg
), blob_len(msg
));
1902 if (!tb
[RPC_OP_PACKAGE
])
1903 return UBUS_STATUS_INVALID_ARGUMENT
;
1905 cmd
[3] = blobmsg_data(tb
[RPC_OP_PACKAGE
]);
1907 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1911 rpc_luci2_opkg_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1912 struct ubus_request_data
*req
, const char *method
,
1913 struct blob_attr
*msg
)
1915 struct blob_attr
*tb
[__RPC_OP_MAX
];
1916 const char *cmd
[5] = { "opkg", "--force-removal-of-dependent-packages",
1917 "remove", NULL
, NULL
};
1919 blobmsg_parse(rpc_opkg_package_policy
, __RPC_OP_MAX
, tb
,
1920 blob_data(msg
), blob_len(msg
));
1922 if (!tb
[RPC_OP_PACKAGE
])
1923 return UBUS_STATUS_INVALID_ARGUMENT
;
1925 cmd
[3] = blobmsg_data(tb
[RPC_OP_PACKAGE
]);
1927 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1931 rpc_luci2_opkg_config_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1932 struct ubus_request_data
*req
, const char *method
,
1933 struct blob_attr
*msg
)
1936 char conf
[2048] = { 0 };
1938 if (!(f
= fopen("/etc/opkg.conf", "r")))
1939 return rpc_errno_status();
1941 fread(conf
, sizeof(conf
) - 1, 1, f
);
1944 blob_buf_init(&buf
, 0);
1945 blobmsg_add_string(&buf
, "config", conf
);
1947 ubus_send_reply(ctx
, req
, buf
.head
);
1952 rpc_luci2_opkg_config_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1953 struct ubus_request_data
*req
, const char *method
,
1954 struct blob_attr
*msg
)
1957 struct blob_attr
*tb
[__RPC_D_MAX
];
1959 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
1960 blob_data(msg
), blob_len(msg
));
1962 if (!tb
[RPC_D_DATA
])
1963 return UBUS_STATUS_INVALID_ARGUMENT
;
1965 if (blobmsg_data_len(tb
[RPC_D_DATA
]) >= 2048)
1966 return UBUS_STATUS_NOT_SUPPORTED
;
1968 if (!(f
= fopen("/etc/opkg.conf", "w")))
1969 return rpc_errno_status();
1971 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
1972 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
1980 menu_access(struct blob_attr
*sid
, struct blob_attr
*acls
, struct blob_buf
*e
)
1983 struct blob_attr
*acl
;
1987 c
= blobmsg_open_table(e
, "write");
1989 blobmsg_for_each_attr(acl
, acls
, rem
)
1991 if (!ops
->access(blobmsg_data(sid
), "luci-ui",
1992 blobmsg_data(acl
), "read"))
1998 blobmsg_add_u8(e
, blobmsg_data(acl
),
1999 ops
->access(blobmsg_data(sid
), "luci-ui",
2000 blobmsg_data(acl
), "write"));
2003 blobmsg_close_table(e
, c
);
2009 rpc_luci2_ui_menu(struct ubus_context
*ctx
, struct ubus_object
*obj
,
2010 struct ubus_request_data
*req
, const char *method
,
2011 struct blob_attr
*msg
)
2015 struct blob_buf menu
= { 0 };
2016 struct blob_buf item
= { 0 };
2017 struct blob_attr
*entry
, *attr
;
2018 struct blob_attr
*tb
[__RPC_MENU_MAX
];
2022 blobmsg_parse(rpc_menu_policy
, __RPC_MENU_MAX
, tb
,
2023 blob_data(msg
), blob_len(msg
));
2025 if (!tb
[RPC_MENU_SESSION
])
2026 return UBUS_STATUS_INVALID_ARGUMENT
;
2029 blob_buf_init(&buf
, 0);
2030 c
= blobmsg_open_table(&buf
, "menu");
2032 if (!glob(RPC_LUCI2_MENU_FILES
, 0, NULL
, &gl
))
2034 for (i
= 0; i
< gl
.gl_pathc
; i
++)
2036 blob_buf_init(&menu
, 0);
2038 if (!blobmsg_add_json_from_file(&menu
, gl
.gl_pathv
[i
]))
2041 blob_for_each_attr(entry
, menu
.head
, rem
)
2045 blob_buf_init(&item
, 0);
2046 d
= blobmsg_open_table(&item
, blobmsg_name(entry
));
2048 blobmsg_for_each_attr(attr
, entry
, rem2
)
2050 if (blob_id(attr
) == BLOBMSG_TYPE_ARRAY
&&
2051 !strcmp(blobmsg_name(attr
), "acls"))
2052 access
= menu_access(tb
[RPC_MENU_SESSION
], attr
, &item
);
2054 blobmsg_add_blob(&item
, attr
);
2057 blobmsg_close_table(&item
, d
);
2060 blob_for_each_attr(attr
, item
.head
, rem2
)
2061 blobmsg_add_blob(&buf
, attr
);
2063 blob_buf_free(&item
);
2067 blob_buf_free(&menu
);
2073 blobmsg_close_table(&buf
, c
);
2075 ubus_send_reply(ctx
, req
, buf
.head
);
2081 rpc_luci2_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
2085 static const struct ubus_method luci2_system_methods
[] = {
2086 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log
),
2087 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg
),
2088 UBUS_METHOD_NOARG("diskfree", rpc_luci2_system_diskfree
),
2089 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list
),
2090 UBUS_METHOD("process_signal", rpc_luci2_process_signal
,
2092 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list
),
2093 UBUS_METHOD("init_action", rpc_luci2_init_action
,
2095 UBUS_METHOD_NOARG("rclocal_get", rpc_luci2_rclocal_get
),
2096 UBUS_METHOD("rclocal_set", rpc_luci2_rclocal_set
,
2098 UBUS_METHOD_NOARG("crontab_get", rpc_luci2_crontab_get
),
2099 UBUS_METHOD("crontab_set", rpc_luci2_crontab_set
,
2101 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get
),
2102 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set
,
2104 UBUS_METHOD("password_set", rpc_luci2_password_set
,
2105 rpc_password_policy
),
2106 UBUS_METHOD_NOARG("led_list", rpc_luci2_led_list
),
2107 UBUS_METHOD_NOARG("usb_list", rpc_luci2_usb_list
),
2108 UBUS_METHOD_NOARG("upgrade_test", rpc_luci2_upgrade_test
),
2109 UBUS_METHOD("upgrade_start", rpc_luci2_upgrade_start
,
2110 rpc_upgrade_policy
),
2111 UBUS_METHOD_NOARG("upgrade_clean", rpc_luci2_upgrade_clean
),
2112 UBUS_METHOD_NOARG("backup_restore", rpc_luci2_backup_restore
),
2113 UBUS_METHOD_NOARG("backup_clean", rpc_luci2_backup_clean
),
2114 UBUS_METHOD_NOARG("backup_config_get", rpc_luci2_backup_config_get
),
2115 UBUS_METHOD("backup_config_set", rpc_luci2_backup_config_set
,
2117 UBUS_METHOD_NOARG("backup_list", rpc_luci2_backup_list
),
2118 UBUS_METHOD_NOARG("reset_test", rpc_luci2_reset_test
),
2119 UBUS_METHOD_NOARG("reset_start", rpc_luci2_reset_start
),
2120 UBUS_METHOD_NOARG("reboot", rpc_luci2_reboot
)
2123 static struct ubus_object_type luci2_system_type
=
2124 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods
);
2126 static struct ubus_object system_obj
= {
2127 .name
= "luci2.system",
2128 .type
= &luci2_system_type
,
2129 .methods
= luci2_system_methods
,
2130 .n_methods
= ARRAY_SIZE(luci2_system_methods
),
2134 static const struct ubus_method luci2_network_methods
[] = {
2135 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count
),
2136 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table
),
2137 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table
),
2138 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases
),
2139 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6
),
2140 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes
),
2141 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6
),
2144 static struct ubus_object_type luci2_network_type
=
2145 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods
);
2147 static struct ubus_object network_obj
= {
2148 .name
= "luci2.network",
2149 .type
= &luci2_network_type
,
2150 .methods
= luci2_network_methods
,
2151 .n_methods
= ARRAY_SIZE(luci2_network_methods
),
2155 static const struct ubus_method luci2_opkg_methods
[] = {
2156 UBUS_METHOD("list", rpc_luci2_opkg_list
,
2157 rpc_opkg_match_policy
),
2158 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed
,
2159 rpc_opkg_match_policy
),
2160 UBUS_METHOD("find", rpc_luci2_opkg_find
,
2161 rpc_opkg_match_policy
),
2162 UBUS_METHOD("install", rpc_luci2_opkg_install
,
2163 rpc_opkg_package_policy
),
2164 UBUS_METHOD("remove", rpc_luci2_opkg_remove
,
2165 rpc_opkg_package_policy
),
2166 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update
),
2167 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get
),
2168 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set
,
2172 static struct ubus_object_type luci2_opkg_type
=
2173 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods
);
2175 static struct ubus_object opkg_obj
= {
2176 .name
= "luci2.opkg",
2177 .type
= &luci2_opkg_type
,
2178 .methods
= luci2_opkg_methods
,
2179 .n_methods
= ARRAY_SIZE(luci2_opkg_methods
),
2183 static const struct ubus_method luci2_ui_methods
[] = {
2184 UBUS_METHOD_NOARG("menu", rpc_luci2_ui_menu
)
2187 static struct ubus_object_type luci2_ui_type
=
2188 UBUS_OBJECT_TYPE("luci-rpc-luci2-ui", luci2_ui_methods
);
2190 static struct ubus_object ui_obj
= {
2192 .type
= &luci2_ui_type
,
2193 .methods
= luci2_ui_methods
,
2194 .n_methods
= ARRAY_SIZE(luci2_ui_methods
),
2197 cursor
= uci_alloc_context();
2200 return UBUS_STATUS_UNKNOWN_ERROR
;
2204 rv
|= ubus_add_object(ctx
, &system_obj
);
2205 rv
|= ubus_add_object(ctx
, &network_obj
);
2206 rv
|= ubus_add_object(ctx
, &opkg_obj
);
2207 rv
|= ubus_add_object(ctx
, &ui_obj
);
2212 const struct rpc_plugin rpc_plugin
= {
2213 .init
= rpc_luci2_api_init