2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #include <sys/types.h>
28 #include <sys/statvfs.h>
30 #include <arpa/inet.h>
33 #include <libubox/blobmsg_json.h>
39 /* limit of log size buffer */
40 #define RPC_LUCI2_MAX_LOGSIZE (128 * 1024)
41 #define RPC_LUCI2_DEF_LOGSIZE (16 * 1024)
43 /* location of menu definitions */
44 #define RPC_LUCI2_MENU_FILES "/usr/share/luci2/menu.d/*.json" /* */
47 static const struct rpc_daemon_ops
*ops
;
49 static struct blob_buf buf
;
50 static struct uci_context
*cursor
;
58 static const struct blobmsg_policy rpc_signal_policy
[__RPC_S_MAX
] = {
59 [RPC_S_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
60 [RPC_S_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
69 static const struct blobmsg_policy rpc_init_policy
[__RPC_I_MAX
] = {
70 [RPC_I_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
71 [RPC_I_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_STRING
},
79 static const struct blobmsg_policy rpc_data_policy
[__RPC_D_MAX
] = {
80 [RPC_D_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_STRING
},
88 static const struct blobmsg_policy rpc_sshkey_policy
[__RPC_K_MAX
] = {
89 [RPC_K_KEYS
] = { .name
= "keys", .type
= BLOBMSG_TYPE_ARRAY
},
98 static const struct blobmsg_policy rpc_password_policy
[__RPC_P_MAX
] = {
99 [RPC_P_USER
] = { .name
= "user", .type
= BLOBMSG_TYPE_STRING
},
100 [RPC_P_PASSWORD
] = { .name
= "password", .type
= BLOBMSG_TYPE_STRING
},
110 static const struct blobmsg_policy rpc_opkg_match_policy
[__RPC_OM_MAX
] = {
111 [RPC_OM_LIMIT
] = { .name
= "limit", .type
= BLOBMSG_TYPE_INT32
},
112 [RPC_OM_OFFSET
] = { .name
= "offset", .type
= BLOBMSG_TYPE_INT32
},
113 [RPC_OM_PATTERN
] = { .name
= "pattern", .type
= BLOBMSG_TYPE_STRING
},
121 static const struct blobmsg_policy rpc_opkg_package_policy
[__RPC_OP_MAX
] = {
122 [RPC_OP_PACKAGE
] = { .name
= "package", .type
= BLOBMSG_TYPE_STRING
},
130 static const struct blobmsg_policy rpc_upgrade_policy
[__RPC_UPGRADE_MAX
] = {
131 [RPC_UPGRADE_KEEP
] = { .name
= "keep", .type
= BLOBMSG_TYPE_BOOL
},
139 static const struct blobmsg_policy rpc_menu_policy
[__RPC_MENU_MAX
] = {
140 [RPC_MENU_SESSION
] = { .name
= "ubus_rpc_session",
141 .type
= BLOBMSG_TYPE_STRING
},
146 rpc_errno_status(void)
151 return UBUS_STATUS_PERMISSION_DENIED
;
154 return UBUS_STATUS_INVALID_ARGUMENT
;
157 return UBUS_STATUS_NOT_FOUND
;
160 return UBUS_STATUS_INVALID_ARGUMENT
;
163 return UBUS_STATUS_UNKNOWN_ERROR
;
168 log_read(FILE *log
, int logsize
)
174 logsize
= RPC_LUCI2_DEF_LOGSIZE
;
176 len
= (logsize
> RPC_LUCI2_MAX_LOGSIZE
) ? RPC_LUCI2_MAX_LOGSIZE
: logsize
;
177 logbuf
= blobmsg_alloc_string_buffer(&buf
, "log", len
+ 1);
182 while (logsize
> RPC_LUCI2_MAX_LOGSIZE
)
184 len
= logsize
% RPC_LUCI2_MAX_LOGSIZE
;
187 len
= RPC_LUCI2_MAX_LOGSIZE
;
189 fread(logbuf
, 1, len
, log
);
193 len
= fread(logbuf
, 1, logsize
, log
);
196 blobmsg_add_string_buffer(&buf
);
200 rpc_luci2_system_log(struct ubus_context
*ctx
, struct ubus_object
*obj
,
201 struct ubus_request_data
*req
, const char *method
,
202 struct blob_attr
*msg
)
206 const char *logfile
= NULL
;
208 struct uci_package
*p
;
209 struct uci_element
*e
;
210 struct uci_section
*s
;
211 struct uci_ptr ptr
= { .package
= "system" };
213 uci_load(cursor
, ptr
.package
, &p
);
216 return UBUS_STATUS_NOT_FOUND
;
218 uci_foreach_element(&p
->sections
, e
)
220 s
= uci_to_section(e
);
222 if (strcmp(s
->type
, "system"))
226 ptr
.option
= "log_type";
227 ptr
.section
= e
->name
;
228 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
232 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
&&
233 !strcmp(ptr
.o
->v
.string
, "file"))
236 ptr
.option
= "log_file";
237 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
239 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
)
240 logfile
= ptr
.o
->v
.string
;
242 logfile
= "/var/log/messages";
244 if (stat(logfile
, &st
) || !(log
= fopen(logfile
, "r")))
247 logsize
= st
.st_size
;
252 ptr
.option
= "log_size";
253 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
255 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
)
256 logsize
= atoi(ptr
.o
->v
.string
) * 1024;
258 if (!(log
= popen("logread", "r")))
262 blob_buf_init(&buf
, 0);
264 log_read(log
, logsize
);
267 uci_unload(cursor
, p
);
268 ubus_send_reply(ctx
, req
, buf
.head
);
272 uci_unload(cursor
, p
);
273 return rpc_errno_status();
277 rpc_luci2_system_dmesg(struct ubus_context
*ctx
, struct ubus_object
*obj
,
278 struct ubus_request_data
*req
, const char *method
,
279 struct blob_attr
*msg
)
283 if (!(log
= popen("dmesg", "r")))
284 return rpc_errno_status();
286 blob_buf_init(&buf
, 0);
288 log_read(log
, RPC_LUCI2_MAX_LOGSIZE
);
291 ubus_send_reply(ctx
, req
, buf
.head
);
296 rpc_luci2_system_diskfree(struct ubus_context
*ctx
, struct ubus_object
*obj
,
297 struct ubus_request_data
*req
, const char *method
,
298 struct blob_attr
*msg
)
303 const char *fslist
[] = {
308 blob_buf_init(&buf
, 0);
310 for (i
= 0; i
< sizeof(fslist
) / sizeof(fslist
[0]); i
+= 2)
312 if (statvfs(fslist
[i
], &s
))
315 c
= blobmsg_open_table(&buf
, fslist
[i
+1]);
317 blobmsg_add_u32(&buf
, "total", s
.f_blocks
* s
.f_frsize
);
318 blobmsg_add_u32(&buf
, "free", s
.f_bfree
* s
.f_frsize
);
319 blobmsg_add_u32(&buf
, "used", (s
.f_blocks
- s
.f_bfree
) * s
.f_frsize
);
321 blobmsg_close_table(&buf
, c
);
324 ubus_send_reply(ctx
, req
, buf
.head
);
329 rpc_luci2_process_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
330 struct ubus_request_data
*req
, const char *method
,
331 struct blob_attr
*msg
)
336 char *pid
, *ppid
, *user
, *stat
, *vsz
, *pvsz
, *pcpu
, *cmd
;
338 if (!(top
= popen("/bin/busybox top -bn1", "r")))
339 return rpc_errno_status();
341 blob_buf_init(&buf
, 0);
342 c
= blobmsg_open_array(&buf
, "processes");
344 while (fgets(line
, sizeof(line
) - 1, top
))
346 pid
= strtok(line
, " ");
348 if (*pid
< '0' || *pid
> '9')
351 ppid
= strtok(NULL
, " ");
352 user
= strtok(NULL
, " ");
353 stat
= strtok(NULL
, " ");
366 vsz
= strtok(stat
+ 4, " ");
367 pvsz
= strtok(NULL
, " ");
368 pcpu
= strtok(NULL
, " ");
369 cmd
= strtok(NULL
, "\n");
374 d
= blobmsg_open_table(&buf
, NULL
);
376 blobmsg_add_u32(&buf
, "pid", atoi(pid
));
377 blobmsg_add_u32(&buf
, "ppid", atoi(ppid
));
378 blobmsg_add_string(&buf
, "user", user
);
379 blobmsg_add_string(&buf
, "stat", stat
);
380 blobmsg_add_u32(&buf
, "vsize", atoi(vsz
) * 1024);
381 blobmsg_add_u32(&buf
, "vsize_percent", atoi(pvsz
));
382 blobmsg_add_u32(&buf
, "cpu_percent", atoi(pcpu
));
383 blobmsg_add_string(&buf
, "command", cmd
);
385 blobmsg_close_table(&buf
, d
);
389 blobmsg_close_array(&buf
, c
);
391 ubus_send_reply(ctx
, req
, buf
.head
);
396 rpc_luci2_process_signal(struct ubus_context
*ctx
, struct ubus_object
*obj
,
397 struct ubus_request_data
*req
, const char *method
,
398 struct blob_attr
*msg
)
401 struct blob_attr
*tb
[__RPC_S_MAX
];
403 blobmsg_parse(rpc_signal_policy
, __RPC_S_MAX
, tb
,
404 blob_data(msg
), blob_len(msg
));
406 if (!tb
[RPC_S_SIGNAL
] || !tb
[RPC_S_PID
])
409 return rpc_errno_status();
412 pid
= blobmsg_get_u32(tb
[RPC_S_PID
]);
413 sig
= blobmsg_get_u32(tb
[RPC_S_SIGNAL
]);
416 return rpc_errno_status();
422 rpc_luci2_init_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
423 struct ubus_request_data
*req
, const char *method
,
424 struct blob_attr
*msg
)
428 char *p
, path
[PATH_MAX
];
434 if (!(d
= opendir("/etc/init.d")))
435 return rpc_errno_status();
437 blob_buf_init(&buf
, 0);
438 c
= blobmsg_open_array(&buf
, "initscripts");
440 while ((e
= readdir(d
)) != NULL
)
442 snprintf(path
, sizeof(path
) - 1, "/etc/init.d/%s", e
->d_name
);
444 if (stat(path
, &s
) || !S_ISREG(s
.st_mode
) || !(s
.st_mode
& S_IXUSR
))
447 if ((f
= fopen(path
, "r")) != NULL
)
450 p
= fgets(path
, sizeof(path
) - 1, f
);
452 if (!p
|| !strstr(p
, "/etc/rc.common"))
455 t
= blobmsg_open_table(&buf
, NULL
);
457 blobmsg_add_string(&buf
, "name", e
->d_name
);
459 while (fgets(path
, sizeof(path
) - 1, f
))
461 p
= strtok(path
, "= \t");
463 if (!strcmp(p
, "START") && !!(p
= strtok(NULL
, "= \t\n")))
466 blobmsg_add_u32(&buf
, "start", n
);
468 else if (!strcmp(p
, "STOP") && !!(p
= strtok(NULL
, "= \t\n")))
470 blobmsg_add_u32(&buf
, "stop", atoi(p
));
477 snprintf(path
, sizeof(path
) - 1, "/etc/rc.d/S%02d%s",
480 blobmsg_add_u8(&buf
, "enabled",
481 (!stat(path
, &s
) && (s
.st_mode
& S_IXUSR
)));
485 blobmsg_add_u8(&buf
, "enabled", 0);
488 blobmsg_close_table(&buf
, t
);
496 blobmsg_close_array(&buf
, c
);
498 ubus_send_reply(ctx
, req
, buf
.head
);
503 rpc_luci2_init_action(struct ubus_context
*ctx
, struct ubus_object
*obj
,
504 struct ubus_request_data
*req
, const char *method
,
505 struct blob_attr
*msg
)
512 struct blob_attr
*tb
[__RPC_I_MAX
];
514 blobmsg_parse(rpc_init_policy
, __RPC_I_MAX
, tb
,
515 blob_data(msg
), blob_len(msg
));
517 if (!tb
[RPC_I_NAME
] || !tb
[RPC_I_ACTION
])
518 return UBUS_STATUS_INVALID_ARGUMENT
;
520 action
= blobmsg_data(tb
[RPC_I_ACTION
]);
522 if (strcmp(action
, "start") && strcmp(action
, "stop") &&
523 strcmp(action
, "reload") && strcmp(action
, "restart") &&
524 strcmp(action
, "enable") && strcmp(action
, "disable"))
525 return UBUS_STATUS_INVALID_ARGUMENT
;
527 snprintf(path
, sizeof(path
) - 1, "/etc/init.d/%s",
528 (char *)blobmsg_data(tb
[RPC_I_NAME
]));
531 return rpc_errno_status();
533 if (!(s
.st_mode
& S_IXUSR
))
534 return UBUS_STATUS_PERMISSION_DENIED
;
536 switch ((pid
= fork()))
539 return rpc_errno_status();
544 if ((fd
= open("/dev/null", O_RDWR
)) > -1)
555 if (execl(path
, path
, action
, NULL
))
556 return rpc_errno_status();
564 rpc_luci2_rclocal_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
565 struct ubus_request_data
*req
, const char *method
,
566 struct blob_attr
*msg
)
569 char data
[4096] = { 0 };
571 if (!(f
= fopen("/etc/rc.local", "r")))
572 return rpc_errno_status();
574 fread(data
, sizeof(data
) - 1, 1, f
);
577 blob_buf_init(&buf
, 0);
578 blobmsg_add_string(&buf
, "data", data
);
580 ubus_send_reply(ctx
, req
, buf
.head
);
585 rpc_luci2_rclocal_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
586 struct ubus_request_data
*req
, const char *method
,
587 struct blob_attr
*msg
)
590 struct blob_attr
*tb
[__RPC_D_MAX
];
592 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
593 blob_data(msg
), blob_len(msg
));
595 if (!tb
[RPC_D_DATA
] || blobmsg_data_len(tb
[RPC_D_DATA
]) >= 4096)
596 return UBUS_STATUS_INVALID_ARGUMENT
;
598 if (!(f
= fopen("/etc/rc.local", "w")))
599 return rpc_errno_status();
601 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
602 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
609 rpc_luci2_crontab_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
610 struct ubus_request_data
*req
, const char *method
,
611 struct blob_attr
*msg
)
614 char data
[4096] = { 0 };
616 if (!(f
= fopen("/etc/crontabs/root", "r")))
617 return rpc_errno_status();
619 fread(data
, sizeof(data
) - 1, 1, f
);
622 blob_buf_init(&buf
, 0);
623 blobmsg_add_string(&buf
, "data", data
);
625 ubus_send_reply(ctx
, req
, buf
.head
);
630 rpc_luci2_crontab_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
631 struct ubus_request_data
*req
, const char *method
,
632 struct blob_attr
*msg
)
636 struct blob_attr
*tb
[__RPC_D_MAX
];
638 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
639 blob_data(msg
), blob_len(msg
));
641 if (!tb
[RPC_D_DATA
] || blobmsg_data_len(tb
[RPC_D_DATA
]) >= 4096)
642 return UBUS_STATUS_INVALID_ARGUMENT
;
644 if (stat("/etc/crontabs", &s
) && mkdir("/etc/crontabs", 0755))
645 return rpc_errno_status();
647 if (!(f
= fopen("/etc/crontabs/root", "w")))
648 return rpc_errno_status();
650 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
651 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
658 rpc_luci2_sshkeys_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
659 struct ubus_request_data
*req
, const char *method
,
660 struct blob_attr
*msg
)
666 if (!(f
= fopen("/etc/dropbear/authorized_keys", "r")))
667 return rpc_errno_status();
669 blob_buf_init(&buf
, 0);
670 c
= blobmsg_open_array(&buf
, "keys");
672 while (fgets(line
, sizeof(line
) - 1, f
))
674 for (p
= line
+ strlen(line
) - 1; (p
> line
) && isspace(*p
); p
--)
677 for (p
= line
; isspace(*p
); p
++)
681 blobmsg_add_string(&buf
, NULL
, p
);
684 blobmsg_close_array(&buf
, c
);
687 ubus_send_reply(ctx
, req
, buf
.head
);
692 rpc_luci2_sshkeys_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
693 struct ubus_request_data
*req
, const char *method
,
694 struct blob_attr
*msg
)
698 struct blob_attr
*cur
, *tb
[__RPC_K_MAX
];
700 blobmsg_parse(rpc_sshkey_policy
, __RPC_K_MAX
, tb
,
701 blob_data(msg
), blob_len(msg
));
704 return UBUS_STATUS_INVALID_ARGUMENT
;
706 if (!(f
= fopen("/etc/dropbear/authorized_keys", "w")))
707 return rpc_errno_status();
709 blobmsg_for_each_attr(cur
, tb
[RPC_K_KEYS
], rem
)
711 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
714 fwrite(blobmsg_data(cur
), blobmsg_data_len(cur
) - 1, 1, f
);
715 fwrite("\n", 1, 1, f
);
723 rpc_luci2_password_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
724 struct ubus_request_data
*req
, const char *method
,
725 struct blob_attr
*msg
)
730 struct blob_attr
*tb
[__RPC_P_MAX
];
732 blobmsg_parse(rpc_password_policy
, __RPC_P_MAX
, tb
,
733 blob_data(msg
), blob_len(msg
));
735 if (!tb
[RPC_P_USER
] || !tb
[RPC_P_PASSWORD
])
736 return UBUS_STATUS_INVALID_ARGUMENT
;
738 if (stat("/usr/bin/passwd", &s
))
739 return UBUS_STATUS_NOT_FOUND
;
741 if (!(s
.st_mode
& S_IXUSR
))
742 return UBUS_STATUS_PERMISSION_DENIED
;
745 return rpc_errno_status();
747 switch ((pid
= fork()))
752 return rpc_errno_status();
761 if ((fd
= open("/dev/null", O_RDWR
)) > -1)
770 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
771 blobmsg_data(tb
[RPC_P_USER
]), NULL
))
772 return rpc_errno_status();
777 write(fds
[1], blobmsg_data(tb
[RPC_P_PASSWORD
]),
778 blobmsg_data_len(tb
[RPC_P_PASSWORD
]) - 1);
779 write(fds
[1], "\n", 1);
783 write(fds
[1], blobmsg_data(tb
[RPC_P_PASSWORD
]),
784 blobmsg_data_len(tb
[RPC_P_PASSWORD
]) - 1);
785 write(fds
[1], "\n", 1);
789 waitpid(pid
, NULL
, 0);
796 rpc_luci2_led_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
797 struct ubus_request_data
*req
, const char *method
,
798 struct blob_attr
*msg
)
802 void *list
, *led
, *trigger
;
803 char *p
, *active_trigger
, line
[512];
806 if (!(d
= opendir("/sys/class/leds")))
807 return rpc_errno_status();
809 blob_buf_init(&buf
, 0);
810 list
= blobmsg_open_array(&buf
, "leds");
812 while ((e
= readdir(d
)) != NULL
)
814 snprintf(line
, sizeof(line
) - 1, "/sys/class/leds/%s/trigger",
817 if (!(f
= fopen(line
, "r")))
820 led
= blobmsg_open_table(&buf
, NULL
);
822 blobmsg_add_string(&buf
, "name", e
->d_name
);
824 if (fgets(line
, sizeof(line
) - 1, f
))
826 trigger
= blobmsg_open_array(&buf
, "triggers");
828 for (p
= strtok(line
, " \n"), active_trigger
= NULL
;
830 p
= strtok(NULL
, " \n"))
834 *(p
+ strlen(p
) - 1) = 0;
839 blobmsg_add_string(&buf
, NULL
, p
);
842 blobmsg_close_array(&buf
, trigger
);
845 blobmsg_add_string(&buf
, "active_trigger", active_trigger
);
850 snprintf(line
, sizeof(line
) - 1, "/sys/class/leds/%s/brightness",
853 if ((f
= fopen(line
, "r")) != NULL
)
855 if (fgets(line
, sizeof(line
) - 1, f
))
856 blobmsg_add_u32(&buf
, "brightness", atoi(line
));
861 snprintf(line
, sizeof(line
) - 1, "/sys/class/leds/%s/max_brightness",
864 if ((f
= fopen(line
, "r")) != NULL
)
866 if (fgets(line
, sizeof(line
) - 1, f
))
867 blobmsg_add_u32(&buf
, "max_brightness", atoi(line
));
872 blobmsg_close_table(&buf
, led
);
877 blobmsg_close_array(&buf
, list
);
878 ubus_send_reply(ctx
, req
, buf
.head
);
884 rpc_luci2_usb_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
885 struct ubus_request_data
*req
, const char *method
,
886 struct blob_attr
*msg
)
896 const char *attributes
[] = {
897 "manufacturer", "vendor_name", "s",
898 "product", "product_name", "s",
899 "idVendor", "vendor_id", "x",
900 "idProduct", "product_id", "x",
901 "serial", "serial", "s",
902 "speed", "speed", "d",
905 if (!(d
= opendir("/sys/bus/usb/devices")))
906 return rpc_errno_status();
908 blob_buf_init(&buf
, 0);
909 list
= blobmsg_open_array(&buf
, "devices");
911 while ((e
= readdir(d
)) != NULL
)
913 if (e
->d_name
[0] < '0' || e
->d_name
[0] > '9')
916 snprintf(line
, sizeof(line
) - 1,
917 "/sys/bus/usb/devices/%s/%s", e
->d_name
, attributes
[0]);
922 device
= blobmsg_open_table(&buf
, NULL
);
924 blobmsg_add_string(&buf
, "name", e
->d_name
);
926 for (i
= 0; i
< sizeof(attributes
) / sizeof(attributes
[0]); i
+= 3)
928 snprintf(line
, sizeof(line
) - 1,
929 "/sys/bus/usb/devices/%s/%s", e
->d_name
, attributes
[i
]);
931 if (!(f
= fopen(line
, "r")))
934 if (fgets(line
, sizeof(line
) - 1, f
))
936 switch (*attributes
[i
+2])
939 blobmsg_add_u32(&buf
, attributes
[i
+1],
940 strtoul(line
, NULL
, 16));
944 blobmsg_add_u32(&buf
, attributes
[i
+1],
945 strtoul(line
, NULL
, 10));
949 if ((p
= strchr(line
, '\n')) != NULL
)
950 while (p
> line
&& isspace(*p
))
953 blobmsg_add_string(&buf
, attributes
[i
+1], line
);
961 blobmsg_close_table(&buf
, device
);
966 blobmsg_close_array(&buf
, list
);
967 ubus_send_reply(ctx
, req
, buf
.head
);
973 rpc_luci2_upgrade_test(struct ubus_context
*ctx
, struct ubus_object
*obj
,
974 struct ubus_request_data
*req
, const char *method
,
975 struct blob_attr
*msg
)
977 const char *cmd
[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL
};
978 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
982 rpc_luci2_upgrade_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
983 struct ubus_request_data
*req
, const char *method
,
984 struct blob_attr
*msg
)
990 rpc_luci2_upgrade_clean(struct ubus_context
*ctx
, struct ubus_object
*obj
,
991 struct ubus_request_data
*req
, const char *method
,
992 struct blob_attr
*msg
)
994 if (unlink("/tmp/firmware.bin"))
995 return rpc_errno_status();
1001 rpc_luci2_backup_restore(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1002 struct ubus_request_data
*req
, const char *method
,
1003 struct blob_attr
*msg
)
1005 const char *cmd
[4] = { "sysupgrade", "--restore-backup",
1006 "/tmp/backup.tar.gz", NULL
};
1008 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1012 rpc_luci2_backup_clean(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1013 struct ubus_request_data
*req
, const char *method
,
1014 struct blob_attr
*msg
)
1016 if (unlink("/tmp/backup.tar.gz"))
1017 return rpc_errno_status();
1023 rpc_luci2_backup_config_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1024 struct ubus_request_data
*req
, const char *method
,
1025 struct blob_attr
*msg
)
1028 char conf
[2048] = { 0 };
1030 if (!(f
= fopen("/etc/sysupgrade.conf", "r")))
1031 return rpc_errno_status();
1033 fread(conf
, sizeof(conf
) - 1, 1, f
);
1036 blob_buf_init(&buf
, 0);
1037 blobmsg_add_string(&buf
, "config", conf
);
1039 ubus_send_reply(ctx
, req
, buf
.head
);
1044 rpc_luci2_backup_config_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1045 struct ubus_request_data
*req
, const char *method
,
1046 struct blob_attr
*msg
)
1049 struct blob_attr
*tb
[__RPC_D_MAX
];
1051 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
1052 blob_data(msg
), blob_len(msg
));
1054 if (!tb
[RPC_D_DATA
])
1055 return UBUS_STATUS_INVALID_ARGUMENT
;
1057 if (blobmsg_data_len(tb
[RPC_D_DATA
]) >= 2048)
1058 return UBUS_STATUS_NOT_SUPPORTED
;
1060 if (!(f
= fopen("/etc/sysupgrade.conf", "w")))
1061 return rpc_errno_status();
1063 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
1064 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
1070 struct backup_state
{
1076 backup_parse_list(struct blob_buf
*blob
, char *buf
, int len
, void *priv
)
1078 struct backup_state
*s
= priv
;
1079 char *nl
= strchr(buf
, '\n');
1087 s
->array
= blobmsg_open_array(blob
, "files");
1091 blobmsg_add_string(blob
, NULL
, buf
);
1093 return (nl
- buf
+ 1);
1097 backup_finish_list(struct blob_buf
*blob
, int status
, void *priv
)
1099 struct backup_state
*s
= priv
;
1102 return UBUS_STATUS_NO_DATA
;
1104 blobmsg_close_array(blob
, s
->array
);
1106 return UBUS_STATUS_OK
;
1110 rpc_luci2_backup_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1111 struct ubus_request_data
*req
, const char *method
,
1112 struct blob_attr
*msg
)
1114 struct backup_state
*state
= NULL
;
1115 const char *cmd
[3] = { "sysupgrade", "--list-backup", NULL
};
1117 state
= malloc(sizeof(*state
));
1120 return UBUS_STATUS_UNKNOWN_ERROR
;
1122 memset(state
, 0, sizeof(*state
));
1124 return ops
->exec(cmd
, NULL
, backup_parse_list
, NULL
, backup_finish_list
,
1129 rpc_luci2_reset_test(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1130 struct ubus_request_data
*req
, const char *method
,
1131 struct blob_attr
*msg
)
1135 char line
[64] = { 0 };
1136 bool supported
= false;
1138 if (!stat("/sbin/mtd", &s
) && (s
.st_mode
& S_IXUSR
))
1140 if ((mtd
= fopen("/proc/mtd", "r")) != NULL
)
1142 while (fgets(line
, sizeof(line
) - 1, mtd
))
1144 if (strstr(line
, "\"rootfs_data\""))
1155 blob_buf_init(&buf
, 0);
1156 blobmsg_add_u8(&buf
, "supported", supported
);
1158 ubus_send_reply(ctx
, req
, buf
.head
);
1164 rpc_luci2_reset_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1165 struct ubus_request_data
*req
, const char *method
,
1166 struct blob_attr
*msg
)
1171 return rpc_errno_status();
1184 execl("/sbin/mtd", "/sbin/mtd", "-r", "erase", "rootfs_data", NULL
);
1186 return rpc_errno_status();
1194 rpc_luci2_reboot(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1195 struct ubus_request_data
*req
, const char *method
,
1196 struct blob_attr
*msg
)
1201 return rpc_errno_status();
1212 execl("/sbin/reboot", "/sbin/reboot", NULL
);
1214 return rpc_errno_status();
1223 dnsmasq_leasefile(void)
1225 FILE *leases
= NULL
;
1226 struct uci_package
*p
;
1227 struct uci_element
*e
;
1228 struct uci_section
*s
;
1229 struct uci_ptr ptr
= {
1232 .option
= "leasefile"
1235 uci_load(cursor
, ptr
.package
, &p
);
1240 uci_foreach_element(&p
->sections
, e
)
1242 s
= uci_to_section(e
);
1244 if (strcmp(s
->type
, "dnsmasq"))
1247 ptr
.section
= e
->name
;
1248 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
1252 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
)
1253 leases
= fopen(ptr
.o
->v
.string
, "r");
1255 uci_unload(cursor
, p
);
1261 rpc_luci2_network_leases(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1262 struct ubus_request_data
*req
, const char *method
,
1263 struct blob_attr
*msg
)
1268 char *ts
, *mac
, *addr
, *name
;
1269 time_t now
= time(NULL
);
1271 blob_buf_init(&buf
, 0);
1272 c
= blobmsg_open_array(&buf
, "leases");
1274 leases
= dnsmasq_leasefile();
1279 while (fgets(line
, sizeof(line
) - 1, leases
))
1281 ts
= strtok(line
, " \t");
1282 mac
= strtok(NULL
, " \t");
1283 addr
= strtok(NULL
, " \t");
1284 name
= strtok(NULL
, " \t");
1286 if (!ts
|| !mac
|| !addr
|| !name
)
1289 if (strchr(addr
, ':'))
1292 d
= blobmsg_open_table(&buf
, NULL
);
1294 blobmsg_add_u32(&buf
, "expires", atoi(ts
) - now
);
1295 blobmsg_add_string(&buf
, "macaddr", mac
);
1296 blobmsg_add_string(&buf
, "ipaddr", addr
);
1298 if (strcmp(name
, "*"))
1299 blobmsg_add_string(&buf
, "hostname", name
);
1301 blobmsg_close_table(&buf
, d
);
1307 blobmsg_close_array(&buf
, c
);
1308 ubus_send_reply(ctx
, req
, buf
.head
);
1314 rpc_luci2_network_leases6(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1315 struct ubus_request_data
*req
, const char *method
,
1316 struct blob_attr
*msg
)
1321 char *ts
, *mac
, *addr
, *name
, *duid
;
1322 time_t now
= time(NULL
);
1324 blob_buf_init(&buf
, 0);
1325 c
= blobmsg_open_array(&buf
, "leases");
1327 leases
= fopen("/tmp/hosts/6relayd", "r");
1331 while (fgets(line
, sizeof(line
) - 1, leases
))
1333 if (strncmp(line
, "# ", 2))
1336 strtok(line
+ 2, " \t"); /* iface */
1338 duid
= strtok(NULL
, " \t");
1340 strtok(NULL
, " \t"); /* iaid */
1342 name
= strtok(NULL
, " \t");
1343 ts
= strtok(NULL
, " \t");
1345 strtok(NULL
, " \t"); /* id */
1346 strtok(NULL
, " \t"); /* length */
1348 addr
= strtok(NULL
, " \t\n");
1353 d
= blobmsg_open_table(&buf
, NULL
);
1355 blobmsg_add_u32(&buf
, "expires", atoi(ts
) - now
);
1356 blobmsg_add_string(&buf
, "duid", duid
);
1357 blobmsg_add_string(&buf
, "ip6addr", addr
);
1359 if (strcmp(name
, "-"))
1360 blobmsg_add_string(&buf
, "hostname", name
);
1362 blobmsg_close_array(&buf
, d
);
1369 leases
= dnsmasq_leasefile();
1374 while (fgets(line
, sizeof(line
) - 1, leases
))
1376 ts
= strtok(line
, " \t");
1377 mac
= strtok(NULL
, " \t");
1378 addr
= strtok(NULL
, " \t");
1379 name
= strtok(NULL
, " \t");
1380 duid
= strtok(NULL
, " \t\n");
1382 if (!ts
|| !mac
|| !addr
|| !duid
)
1385 if (!strchr(addr
, ':'))
1388 d
= blobmsg_open_table(&buf
, NULL
);
1390 blobmsg_add_u32(&buf
, "expires", atoi(ts
) - now
);
1391 blobmsg_add_string(&buf
, "macaddr", mac
);
1392 blobmsg_add_string(&buf
, "ip6addr", addr
);
1394 if (strcmp(name
, "*"))
1395 blobmsg_add_string(&buf
, "hostname", name
);
1397 if (strcmp(duid
, "*"))
1398 blobmsg_add_string(&buf
, "duid", name
);
1400 blobmsg_close_table(&buf
, d
);
1407 blobmsg_close_array(&buf
, c
);
1408 ubus_send_reply(ctx
, req
, buf
.head
);
1414 rpc_luci2_network_ct_count(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1415 struct ubus_request_data
*req
, const char *method
,
1416 struct blob_attr
*msg
)
1421 blob_buf_init(&buf
, 0);
1423 if ((f
= fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL
)
1425 if (fgets(line
, sizeof(line
) - 1, f
))
1426 blobmsg_add_u32(&buf
, "count", atoi(line
));
1431 if ((f
= fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL
)
1433 if (fgets(line
, sizeof(line
) - 1, f
))
1434 blobmsg_add_u32(&buf
, "limit", atoi(line
));
1439 ubus_send_reply(ctx
, req
, buf
.head
);
1445 rpc_luci2_network_ct_table(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1446 struct ubus_request_data
*req
, const char *method
,
1447 struct blob_attr
*msg
)
1455 blob_buf_init(&buf
, 0);
1456 c
= blobmsg_open_array(&buf
, "entries");
1458 if ((f
= fopen("/proc/net/nf_conntrack", "r")) != NULL
)
1460 while (fgets(line
, sizeof(line
) - 1, f
))
1462 d
= blobmsg_open_table(&buf
, NULL
);
1463 memset(seen
, 0, sizeof(seen
));
1465 for (i
= 0, p
= strtok(line
, " "); p
; i
++, p
= strtok(NULL
, " "))
1468 blobmsg_add_u8(&buf
, "ipv6", !strcmp(p
, "ipv6"));
1470 blobmsg_add_u32(&buf
, "protocol", atoi(p
));
1472 blobmsg_add_u32(&buf
, "expires", atoi(p
));
1478 if (!seen
[0] && !strncmp(p
, "src=", 4))
1480 blobmsg_add_string(&buf
, "src", p
+ 4);
1483 else if (!seen
[1] && !strncmp(p
, "dst=", 4))
1485 blobmsg_add_string(&buf
, "dest", p
+ 4);
1488 else if (!seen
[2] && !strncmp(p
, "sport=", 6))
1490 blobmsg_add_u32(&buf
, "sport", atoi(p
+ 6));
1493 else if (!seen
[3] && !strncmp(p
, "dport=", 6))
1495 blobmsg_add_u32(&buf
, "dport", atoi(p
+ 6));
1498 else if (!strncmp(p
, "packets=", 8))
1500 blobmsg_add_u32(&buf
,
1501 seen
[4] ? "tx_packets" : "rx_packets",
1505 else if (!strncmp(p
, "bytes=", 6))
1507 blobmsg_add_u32(&buf
,
1508 seen
[5] ? "tx_bytes" : "rx_bytes",
1515 blobmsg_close_table(&buf
, d
);
1521 blobmsg_close_array(&buf
, c
);
1522 ubus_send_reply(ctx
, req
, buf
.head
);
1528 rpc_luci2_network_arp_table(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1529 struct ubus_request_data
*req
, const char *method
,
1530 struct blob_attr
*msg
)
1534 char *addr
, *mac
, *dev
, line
[128];
1536 blob_buf_init(&buf
, 0);
1537 c
= blobmsg_open_array(&buf
, "entries");
1539 if ((f
= fopen("/proc/net/arp", "r")) != NULL
)
1541 /* skip header line */
1542 fgets(line
, sizeof(line
) - 1, f
);
1544 while (fgets(line
, sizeof(line
) - 1, f
))
1546 addr
= strtok(line
, " \t");
1548 strtok(NULL
, " \t"); /* HW type */
1549 strtok(NULL
, " \t"); /* Flags */
1551 mac
= strtok(NULL
, " \t");
1553 strtok(NULL
, " \t"); /* Mask */
1555 dev
= strtok(NULL
, " \t\n");
1560 d
= blobmsg_open_table(&buf
, NULL
);
1561 blobmsg_add_string(&buf
, "ipaddr", addr
);
1562 blobmsg_add_string(&buf
, "macaddr", mac
);
1563 blobmsg_add_string(&buf
, "device", dev
);
1564 blobmsg_close_table(&buf
, d
);
1570 blobmsg_close_array(&buf
, c
);
1571 ubus_send_reply(ctx
, req
, buf
.head
);
1577 put_hexaddr(const char *name
, const char *s
, const char *m
)
1581 char as
[sizeof("255.255.255.255/32\0")];
1583 a
.s_addr
= strtoul(s
, NULL
, 16);
1584 inet_ntop(AF_INET
, &a
, as
, sizeof(as
));
1588 for (a
.s_addr
= ntohl(strtoul(m
, NULL
, 16)), bits
= 0;
1589 a
.s_addr
& 0x80000000;
1593 sprintf(as
+ strlen(as
), "/%u", bits
);
1596 blobmsg_add_string(&buf
, name
, as
);
1600 rpc_luci2_network_routes(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1601 struct ubus_request_data
*req
, const char *method
,
1602 struct blob_attr
*msg
)
1606 char *dst
, *dmask
, *next
, *metric
, *device
;
1610 if (!(routes
= fopen("/proc/net/route", "r")))
1611 return rpc_errno_status();
1613 blob_buf_init(&buf
, 0);
1614 c
= blobmsg_open_array(&buf
, "routes");
1616 /* skip header line */
1617 fgets(line
, sizeof(line
) - 1, routes
);
1619 while (fgets(line
, sizeof(line
) - 1, routes
))
1621 device
= strtok(line
, "\t ");
1622 dst
= strtok(NULL
, "\t ");
1623 next
= strtok(NULL
, "\t ");
1625 strtok(NULL
, "\t "); /* flags */
1626 strtok(NULL
, "\t "); /* refcount */
1627 strtok(NULL
, "\t "); /* usecount */
1629 metric
= strtok(NULL
, "\t ");
1630 dmask
= strtok(NULL
, "\t ");
1635 d
= blobmsg_open_table(&buf
, NULL
);
1637 put_hexaddr("target", dst
, dmask
);
1638 put_hexaddr("nexthop", next
, NULL
);
1640 n
= strtoul(metric
, NULL
, 10);
1641 blobmsg_add_u32(&buf
, "metric", n
);
1643 blobmsg_add_string(&buf
, "device", device
);
1645 blobmsg_close_table(&buf
, d
);
1648 blobmsg_close_array(&buf
, c
);
1651 ubus_send_reply(ctx
, req
, buf
.head
);
1656 put_hex6addr(const char *name
, const char *s
, const char *m
)
1660 char as
[INET6_ADDRSTRLEN
+ sizeof("/128")];
1663 (((x) <= '9') ? ((x) - '0') : \
1664 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1667 for (i
= 0; i
< 16; i
++, s
+= 2)
1668 a
.s6_addr
[i
] = (16 * hex(*s
)) + hex(*(s
+1));
1670 inet_ntop(AF_INET6
, &a
, as
, sizeof(as
));
1673 sprintf(as
+ strlen(as
), "/%lu", strtoul(m
, NULL
, 16));
1675 blobmsg_add_string(&buf
, name
, as
);
1679 rpc_luci2_network_routes6(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1680 struct ubus_request_data
*req
, const char *method
,
1681 struct blob_attr
*msg
)
1685 char *src
, *smask
, *dst
, *dmask
, *next
, *metric
, *flags
, *device
;
1689 if (!(routes
= fopen("/proc/net/ipv6_route", "r")))
1690 return rpc_errno_status();
1692 blob_buf_init(&buf
, 0);
1693 c
= blobmsg_open_array(&buf
, "routes");
1695 while (fgets(line
, sizeof(line
) - 1, routes
))
1697 dst
= strtok(line
, " ");
1698 dmask
= strtok(NULL
, " ");
1699 src
= strtok(NULL
, " ");
1700 smask
= strtok(NULL
, " ");
1701 next
= strtok(NULL
, " ");
1702 metric
= strtok(NULL
, " ");
1704 strtok(NULL
, " "); /* refcount */
1705 strtok(NULL
, " "); /* usecount */
1707 flags
= strtok(NULL
, " ");
1708 device
= strtok(NULL
, " \n");
1713 n
= strtoul(flags
, NULL
, 16);
1718 d
= blobmsg_open_table(&buf
, NULL
);
1720 put_hex6addr("target", dst
, dmask
);
1721 put_hex6addr("source", src
, smask
);
1722 put_hex6addr("nexthop", next
, NULL
);
1724 n
= strtoul(metric
, NULL
, 16);
1725 blobmsg_add_u32(&buf
, "metric", n
);
1727 blobmsg_add_string(&buf
, "device", device
);
1729 blobmsg_close_table(&buf
, d
);
1732 blobmsg_close_array(&buf
, c
);
1735 ubus_send_reply(ctx
, req
, buf
.head
);
1751 opkg_parse_list(struct blob_buf
*blob
, char *buf
, int len
, void *priv
)
1753 struct opkg_state
*s
= priv
;
1756 char *nl
= strchr(buf
, '\n');
1757 char *name
= NULL
, *vers
= NULL
, *desc
= NULL
;
1765 if (s
->cur_offset
++ < s
->req_offset
)
1768 if (s
->cur_count
++ >= s
->req_count
)
1774 s
->array
= blobmsg_open_array(blob
, "packages");
1777 for (ptr
= buf
, last
= buf
, *nl
= 0; ptr
<= nl
; ptr
++)
1779 if (!*ptr
|| (*ptr
== ' ' && *(ptr
+1) == '-' && *(ptr
+2) == ' '))
1791 desc
= *ptr
? (ptr
+ 3) : NULL
;
1800 c
= blobmsg_open_array(blob
, NULL
);
1802 blobmsg_add_string(blob
, NULL
, name
);
1803 blobmsg_add_string(blob
, NULL
, vers
);
1806 blobmsg_add_string(blob
, NULL
, desc
);
1808 blobmsg_close_array(blob
, c
);
1812 return (nl
- buf
+ 1);
1816 opkg_finish_list(struct blob_buf
*blob
, int status
, void *priv
)
1818 struct opkg_state
*s
= priv
;
1821 return UBUS_STATUS_NO_DATA
;
1823 blobmsg_close_array(blob
, s
->array
);
1824 blobmsg_add_u32(blob
, "total", s
->total
);
1826 return UBUS_STATUS_OK
;
1830 opkg_exec_list(const char *action
, struct blob_attr
*msg
,
1831 struct ubus_context
*ctx
, struct ubus_request_data
*req
)
1833 struct opkg_state
*state
= NULL
;
1834 struct blob_attr
*tb
[__RPC_OM_MAX
];
1835 const char *cmd
[5] = { "opkg", action
, "-nocase", NULL
, NULL
};
1837 blobmsg_parse(rpc_opkg_match_policy
, __RPC_OM_MAX
, tb
,
1838 blob_data(msg
), blob_len(msg
));
1840 state
= malloc(sizeof(*state
));
1843 return UBUS_STATUS_UNKNOWN_ERROR
;
1845 memset(state
, 0, sizeof(*state
));
1847 if (tb
[RPC_OM_PATTERN
])
1848 cmd
[3] = blobmsg_data(tb
[RPC_OM_PATTERN
]);
1850 if (tb
[RPC_OM_LIMIT
])
1851 state
->req_count
= blobmsg_get_u32(tb
[RPC_OM_LIMIT
]);
1853 if (tb
[RPC_OM_OFFSET
])
1854 state
->req_offset
= blobmsg_get_u32(tb
[RPC_OM_OFFSET
]);
1856 if (state
->req_offset
< 0)
1857 state
->req_offset
= 0;
1859 if (state
->req_count
<= 0 || state
->req_count
> 100)
1860 state
->req_count
= 100;
1862 return ops
->exec(cmd
, NULL
, opkg_parse_list
, NULL
, opkg_finish_list
,
1868 rpc_luci2_opkg_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1869 struct ubus_request_data
*req
, const char *method
,
1870 struct blob_attr
*msg
)
1872 return opkg_exec_list("list", msg
, ctx
, req
);
1876 rpc_luci2_opkg_list_installed(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1877 struct ubus_request_data
*req
, const char *method
,
1878 struct blob_attr
*msg
)
1880 return opkg_exec_list("list-installed", msg
, ctx
, req
);
1884 rpc_luci2_opkg_find(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1885 struct ubus_request_data
*req
, const char *method
,
1886 struct blob_attr
*msg
)
1888 return opkg_exec_list("find", msg
, ctx
, req
);
1892 rpc_luci2_opkg_update(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1893 struct ubus_request_data
*req
, const char *method
,
1894 struct blob_attr
*msg
)
1896 const char *cmd
[3] = { "opkg", "update", NULL
};
1897 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1901 rpc_luci2_opkg_install(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1902 struct ubus_request_data
*req
, const char *method
,
1903 struct blob_attr
*msg
)
1905 struct blob_attr
*tb
[__RPC_OP_MAX
];
1906 const char *cmd
[5] = { "opkg", "--force-overwrite",
1907 "install", NULL
, NULL
};
1909 blobmsg_parse(rpc_opkg_package_policy
, __RPC_OP_MAX
, tb
,
1910 blob_data(msg
), blob_len(msg
));
1912 if (!tb
[RPC_OP_PACKAGE
])
1913 return UBUS_STATUS_INVALID_ARGUMENT
;
1915 cmd
[3] = blobmsg_data(tb
[RPC_OP_PACKAGE
]);
1917 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1921 rpc_luci2_opkg_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1922 struct ubus_request_data
*req
, const char *method
,
1923 struct blob_attr
*msg
)
1925 struct blob_attr
*tb
[__RPC_OP_MAX
];
1926 const char *cmd
[5] = { "opkg", "--force-removal-of-dependent-packages",
1927 "remove", NULL
, NULL
};
1929 blobmsg_parse(rpc_opkg_package_policy
, __RPC_OP_MAX
, tb
,
1930 blob_data(msg
), blob_len(msg
));
1932 if (!tb
[RPC_OP_PACKAGE
])
1933 return UBUS_STATUS_INVALID_ARGUMENT
;
1935 cmd
[3] = blobmsg_data(tb
[RPC_OP_PACKAGE
]);
1937 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1941 rpc_luci2_opkg_config_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1942 struct ubus_request_data
*req
, const char *method
,
1943 struct blob_attr
*msg
)
1946 char conf
[2048] = { 0 };
1948 if (!(f
= fopen("/etc/opkg.conf", "r")))
1949 return rpc_errno_status();
1951 fread(conf
, sizeof(conf
) - 1, 1, f
);
1954 blob_buf_init(&buf
, 0);
1955 blobmsg_add_string(&buf
, "config", conf
);
1957 ubus_send_reply(ctx
, req
, buf
.head
);
1962 rpc_luci2_opkg_config_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1963 struct ubus_request_data
*req
, const char *method
,
1964 struct blob_attr
*msg
)
1967 struct blob_attr
*tb
[__RPC_D_MAX
];
1969 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
1970 blob_data(msg
), blob_len(msg
));
1972 if (!tb
[RPC_D_DATA
])
1973 return UBUS_STATUS_INVALID_ARGUMENT
;
1975 if (blobmsg_data_len(tb
[RPC_D_DATA
]) >= 2048)
1976 return UBUS_STATUS_NOT_SUPPORTED
;
1978 if (!(f
= fopen("/etc/opkg.conf", "w")))
1979 return rpc_errno_status();
1981 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
1982 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
1990 menu_access(struct blob_attr
*sid
, struct blob_attr
*acls
, struct blob_buf
*e
)
1993 struct blob_attr
*acl
;
1997 c
= blobmsg_open_table(e
, "write");
1999 blobmsg_for_each_attr(acl
, acls
, rem
)
2001 if (!ops
->access(blobmsg_data(sid
), "luci-ui",
2002 blobmsg_data(acl
), "read"))
2008 blobmsg_add_u8(e
, blobmsg_data(acl
),
2009 ops
->access(blobmsg_data(sid
), "luci-ui",
2010 blobmsg_data(acl
), "write"));
2013 blobmsg_close_table(e
, c
);
2019 rpc_luci2_ui_menu(struct ubus_context
*ctx
, struct ubus_object
*obj
,
2020 struct ubus_request_data
*req
, const char *method
,
2021 struct blob_attr
*msg
)
2025 struct blob_buf menu
= { 0 };
2026 struct blob_buf item
= { 0 };
2027 struct blob_attr
*entry
, *attr
;
2028 struct blob_attr
*tb
[__RPC_MENU_MAX
];
2032 blobmsg_parse(rpc_menu_policy
, __RPC_MENU_MAX
, tb
,
2033 blob_data(msg
), blob_len(msg
));
2035 if (!tb
[RPC_MENU_SESSION
])
2036 return UBUS_STATUS_INVALID_ARGUMENT
;
2039 blob_buf_init(&buf
, 0);
2040 c
= blobmsg_open_table(&buf
, "menu");
2042 if (!glob(RPC_LUCI2_MENU_FILES
, 0, NULL
, &gl
))
2044 for (i
= 0; i
< gl
.gl_pathc
; i
++)
2046 blob_buf_init(&menu
, 0);
2048 if (!blobmsg_add_json_from_file(&menu
, gl
.gl_pathv
[i
]))
2051 blob_for_each_attr(entry
, menu
.head
, rem
)
2055 blob_buf_init(&item
, 0);
2056 d
= blobmsg_open_table(&item
, blobmsg_name(entry
));
2058 blobmsg_for_each_attr(attr
, entry
, rem2
)
2060 if (blob_id(attr
) == BLOBMSG_TYPE_ARRAY
&&
2061 !strcmp(blobmsg_name(attr
), "acls"))
2062 access
= menu_access(tb
[RPC_MENU_SESSION
], attr
, &item
);
2064 blobmsg_add_blob(&item
, attr
);
2067 blobmsg_close_table(&item
, d
);
2070 blob_for_each_attr(attr
, item
.head
, rem2
)
2071 blobmsg_add_blob(&buf
, attr
);
2073 blob_buf_free(&item
);
2077 blob_buf_free(&menu
);
2083 blobmsg_close_table(&buf
, c
);
2085 ubus_send_reply(ctx
, req
, buf
.head
);
2091 rpc_luci2_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
2095 static const struct ubus_method luci2_system_methods
[] = {
2096 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log
),
2097 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg
),
2098 UBUS_METHOD_NOARG("diskfree", rpc_luci2_system_diskfree
),
2099 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list
),
2100 UBUS_METHOD("process_signal", rpc_luci2_process_signal
,
2102 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list
),
2103 UBUS_METHOD("init_action", rpc_luci2_init_action
,
2105 UBUS_METHOD_NOARG("rclocal_get", rpc_luci2_rclocal_get
),
2106 UBUS_METHOD("rclocal_set", rpc_luci2_rclocal_set
,
2108 UBUS_METHOD_NOARG("crontab_get", rpc_luci2_crontab_get
),
2109 UBUS_METHOD("crontab_set", rpc_luci2_crontab_set
,
2111 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get
),
2112 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set
,
2114 UBUS_METHOD("password_set", rpc_luci2_password_set
,
2115 rpc_password_policy
),
2116 UBUS_METHOD_NOARG("led_list", rpc_luci2_led_list
),
2117 UBUS_METHOD_NOARG("usb_list", rpc_luci2_usb_list
),
2118 UBUS_METHOD_NOARG("upgrade_test", rpc_luci2_upgrade_test
),
2119 UBUS_METHOD("upgrade_start", rpc_luci2_upgrade_start
,
2120 rpc_upgrade_policy
),
2121 UBUS_METHOD_NOARG("upgrade_clean", rpc_luci2_upgrade_clean
),
2122 UBUS_METHOD_NOARG("backup_restore", rpc_luci2_backup_restore
),
2123 UBUS_METHOD_NOARG("backup_clean", rpc_luci2_backup_clean
),
2124 UBUS_METHOD_NOARG("backup_config_get", rpc_luci2_backup_config_get
),
2125 UBUS_METHOD("backup_config_set", rpc_luci2_backup_config_set
,
2127 UBUS_METHOD_NOARG("backup_list", rpc_luci2_backup_list
),
2128 UBUS_METHOD_NOARG("reset_test", rpc_luci2_reset_test
),
2129 UBUS_METHOD_NOARG("reset_start", rpc_luci2_reset_start
),
2130 UBUS_METHOD_NOARG("reboot", rpc_luci2_reboot
)
2133 static struct ubus_object_type luci2_system_type
=
2134 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods
);
2136 static struct ubus_object system_obj
= {
2137 .name
= "luci2.system",
2138 .type
= &luci2_system_type
,
2139 .methods
= luci2_system_methods
,
2140 .n_methods
= ARRAY_SIZE(luci2_system_methods
),
2144 static const struct ubus_method luci2_network_methods
[] = {
2145 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count
),
2146 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table
),
2147 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table
),
2148 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases
),
2149 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6
),
2150 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes
),
2151 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6
),
2154 static struct ubus_object_type luci2_network_type
=
2155 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods
);
2157 static struct ubus_object network_obj
= {
2158 .name
= "luci2.network",
2159 .type
= &luci2_network_type
,
2160 .methods
= luci2_network_methods
,
2161 .n_methods
= ARRAY_SIZE(luci2_network_methods
),
2165 static const struct ubus_method luci2_opkg_methods
[] = {
2166 UBUS_METHOD("list", rpc_luci2_opkg_list
,
2167 rpc_opkg_match_policy
),
2168 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed
,
2169 rpc_opkg_match_policy
),
2170 UBUS_METHOD("find", rpc_luci2_opkg_find
,
2171 rpc_opkg_match_policy
),
2172 UBUS_METHOD("install", rpc_luci2_opkg_install
,
2173 rpc_opkg_package_policy
),
2174 UBUS_METHOD("remove", rpc_luci2_opkg_remove
,
2175 rpc_opkg_package_policy
),
2176 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update
),
2177 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get
),
2178 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set
,
2182 static struct ubus_object_type luci2_opkg_type
=
2183 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods
);
2185 static struct ubus_object opkg_obj
= {
2186 .name
= "luci2.opkg",
2187 .type
= &luci2_opkg_type
,
2188 .methods
= luci2_opkg_methods
,
2189 .n_methods
= ARRAY_SIZE(luci2_opkg_methods
),
2193 static const struct ubus_method luci2_ui_methods
[] = {
2194 UBUS_METHOD_NOARG("menu", rpc_luci2_ui_menu
)
2197 static struct ubus_object_type luci2_ui_type
=
2198 UBUS_OBJECT_TYPE("luci-rpc-luci2-ui", luci2_ui_methods
);
2200 static struct ubus_object ui_obj
= {
2202 .type
= &luci2_ui_type
,
2203 .methods
= luci2_ui_methods
,
2204 .n_methods
= ARRAY_SIZE(luci2_ui_methods
),
2207 cursor
= uci_alloc_context();
2210 return UBUS_STATUS_UNKNOWN_ERROR
;
2214 rv
|= ubus_add_object(ctx
, &system_obj
);
2215 rv
|= ubus_add_object(ctx
, &network_obj
);
2216 rv
|= ubus_add_object(ctx
, &opkg_obj
);
2217 rv
|= ubus_add_object(ctx
, &ui_obj
);
2222 const struct rpc_plugin rpc_plugin
= {
2223 .init
= rpc_luci2_api_init