2 * luci-rpcd - LuCI UBUS RPC server
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #include <sys/types.h>
28 #include <sys/statvfs.h>
30 #include <arpa/inet.h>
38 static struct blob_buf buf
;
39 static struct uci_context
*cursor
;
47 static const struct blobmsg_policy rpc_signal_policy
[__RPC_S_MAX
] = {
48 [RPC_S_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
49 [RPC_S_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
58 static const struct blobmsg_policy rpc_init_policy
[__RPC_I_MAX
] = {
59 [RPC_I_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
60 [RPC_I_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_STRING
},
68 static const struct blobmsg_policy rpc_data_policy
[__RPC_D_MAX
] = {
69 [RPC_D_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_STRING
},
77 static const struct blobmsg_policy rpc_sshkey_policy
[__RPC_K_MAX
] = {
78 [RPC_K_KEYS
] = { .name
= "keys", .type
= BLOBMSG_TYPE_ARRAY
},
87 static const struct blobmsg_policy rpc_password_policy
[__RPC_P_MAX
] = {
88 [RPC_P_USER
] = { .name
= "user", .type
= BLOBMSG_TYPE_STRING
},
89 [RPC_P_PASSWORD
] = { .name
= "password", .type
= BLOBMSG_TYPE_STRING
},
99 static const struct blobmsg_policy rpc_opkg_match_policy
[__RPC_OM_MAX
] = {
100 [RPC_OM_LIMIT
] = { .name
= "limit", .type
= BLOBMSG_TYPE_INT32
},
101 [RPC_OM_OFFSET
] = { .name
= "offset", .type
= BLOBMSG_TYPE_INT32
},
102 [RPC_OM_PATTERN
] = { .name
= "pattern", .type
= BLOBMSG_TYPE_STRING
},
110 static const struct blobmsg_policy rpc_opkg_package_policy
[__RPC_OP_MAX
] = {
111 [RPC_OP_PACKAGE
] = { .name
= "package", .type
= BLOBMSG_TYPE_STRING
},
119 static const struct blobmsg_policy rpc_upgrade_policy
[__RPC_UPGRADE_MAX
] = {
120 [RPC_UPGRADE_KEEP
] = { .name
= "keep", .type
= BLOBMSG_TYPE_BOOL
},
128 static const struct blobmsg_policy rpc_menu_policy
[__RPC_MENU_MAX
] = {
129 [RPC_MENU_SESSION
] = { .name
= "ubus_rpc_session",
130 .type
= BLOBMSG_TYPE_STRING
},
135 rpc_errno_status(void)
140 return UBUS_STATUS_PERMISSION_DENIED
;
143 return UBUS_STATUS_INVALID_ARGUMENT
;
146 return UBUS_STATUS_NOT_FOUND
;
149 return UBUS_STATUS_INVALID_ARGUMENT
;
152 return UBUS_STATUS_UNKNOWN_ERROR
;
157 log_read(FILE *log
, int logsize
)
163 logsize
= RPC_LUCI2_DEF_LOGSIZE
;
165 len
= (logsize
> RPC_LUCI2_MAX_LOGSIZE
) ? RPC_LUCI2_MAX_LOGSIZE
: logsize
;
166 logbuf
= blobmsg_alloc_string_buffer(&buf
, "log", len
+ 1);
171 while (logsize
> RPC_LUCI2_MAX_LOGSIZE
)
173 len
= logsize
% RPC_LUCI2_MAX_LOGSIZE
;
176 len
= RPC_LUCI2_MAX_LOGSIZE
;
178 fread(logbuf
, 1, len
, log
);
182 len
= fread(logbuf
, 1, logsize
, log
);
185 blobmsg_add_string_buffer(&buf
);
189 rpc_luci2_system_log(struct ubus_context
*ctx
, struct ubus_object
*obj
,
190 struct ubus_request_data
*req
, const char *method
,
191 struct blob_attr
*msg
)
195 const char *logfile
= NULL
;
197 struct uci_package
*p
;
198 struct uci_element
*e
;
199 struct uci_section
*s
;
200 struct uci_ptr ptr
= { .package
= "system" };
202 uci_load(cursor
, ptr
.package
, &p
);
205 return UBUS_STATUS_NOT_FOUND
;
207 uci_foreach_element(&p
->sections
, e
)
209 s
= uci_to_section(e
);
211 if (strcmp(s
->type
, "system"))
215 ptr
.option
= "log_type";
216 ptr
.section
= e
->name
;
217 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
221 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
&&
222 !strcmp(ptr
.o
->v
.string
, "file"))
225 ptr
.option
= "log_file";
226 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
228 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
)
229 logfile
= ptr
.o
->v
.string
;
231 logfile
= "/var/log/messages";
233 if (stat(logfile
, &st
) || !(log
= fopen(logfile
, "r")))
236 logsize
= st
.st_size
;
241 ptr
.option
= "log_size";
242 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
244 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
)
245 logsize
= atoi(ptr
.o
->v
.string
) * 1024;
247 if (!(log
= popen("logread", "r")))
251 blob_buf_init(&buf
, 0);
253 log_read(log
, logsize
);
256 uci_unload(cursor
, p
);
257 ubus_send_reply(ctx
, req
, buf
.head
);
261 uci_unload(cursor
, p
);
262 return rpc_errno_status();
266 rpc_luci2_system_dmesg(struct ubus_context
*ctx
, struct ubus_object
*obj
,
267 struct ubus_request_data
*req
, const char *method
,
268 struct blob_attr
*msg
)
272 if (!(log
= popen("dmesg", "r")))
273 return rpc_errno_status();
275 blob_buf_init(&buf
, 0);
277 log_read(log
, RPC_LUCI2_MAX_LOGSIZE
);
280 ubus_send_reply(ctx
, req
, buf
.head
);
285 rpc_luci2_system_diskfree(struct ubus_context
*ctx
, struct ubus_object
*obj
,
286 struct ubus_request_data
*req
, const char *method
,
287 struct blob_attr
*msg
)
292 const char *fslist
[] = {
297 blob_buf_init(&buf
, 0);
299 for (i
= 0; i
< sizeof(fslist
) / sizeof(fslist
[0]); i
+= 2)
301 if (statvfs(fslist
[i
], &s
))
304 c
= blobmsg_open_table(&buf
, fslist
[i
+1]);
306 blobmsg_add_u32(&buf
, "total", s
.f_blocks
* s
.f_frsize
);
307 blobmsg_add_u32(&buf
, "free", s
.f_bfree
* s
.f_frsize
);
308 blobmsg_add_u32(&buf
, "used", (s
.f_blocks
- s
.f_bfree
) * s
.f_frsize
);
310 blobmsg_close_table(&buf
, c
);
313 ubus_send_reply(ctx
, req
, buf
.head
);
318 rpc_luci2_process_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
319 struct ubus_request_data
*req
, const char *method
,
320 struct blob_attr
*msg
)
325 char *pid
, *ppid
, *user
, *stat
, *vsz
, *pvsz
, *pcpu
, *cmd
;
327 if (!(top
= popen("/bin/busybox top -bn1", "r")))
328 return rpc_errno_status();
330 blob_buf_init(&buf
, 0);
331 c
= blobmsg_open_array(&buf
, "processes");
333 while (fgets(line
, sizeof(line
) - 1, top
))
335 pid
= strtok(line
, " ");
337 if (*pid
< '0' || *pid
> '9')
340 ppid
= strtok(NULL
, " ");
341 user
= strtok(NULL
, " ");
342 stat
= strtok(NULL
, " ");
355 vsz
= strtok(stat
+ 4, " ");
356 pvsz
= strtok(NULL
, " ");
357 pcpu
= strtok(NULL
, " ");
358 cmd
= strtok(NULL
, "\n");
363 d
= blobmsg_open_table(&buf
, NULL
);
365 blobmsg_add_u32(&buf
, "pid", atoi(pid
));
366 blobmsg_add_u32(&buf
, "ppid", atoi(ppid
));
367 blobmsg_add_string(&buf
, "user", user
);
368 blobmsg_add_string(&buf
, "stat", stat
);
369 blobmsg_add_u32(&buf
, "vsize", atoi(vsz
) * 1024);
370 blobmsg_add_u32(&buf
, "vsize_percent", atoi(pvsz
));
371 blobmsg_add_u32(&buf
, "cpu_percent", atoi(pcpu
));
372 blobmsg_add_string(&buf
, "command", cmd
);
374 blobmsg_close_table(&buf
, d
);
378 blobmsg_close_array(&buf
, c
);
380 ubus_send_reply(ctx
, req
, buf
.head
);
385 rpc_luci2_process_signal(struct ubus_context
*ctx
, struct ubus_object
*obj
,
386 struct ubus_request_data
*req
, const char *method
,
387 struct blob_attr
*msg
)
390 struct blob_attr
*tb
[__RPC_S_MAX
];
392 blobmsg_parse(rpc_signal_policy
, __RPC_S_MAX
, tb
,
393 blob_data(msg
), blob_len(msg
));
395 if (!tb
[RPC_S_SIGNAL
] || !tb
[RPC_S_PID
])
398 return rpc_errno_status();
401 pid
= blobmsg_get_u32(tb
[RPC_S_PID
]);
402 sig
= blobmsg_get_u32(tb
[RPC_S_SIGNAL
]);
405 return rpc_errno_status();
411 rpc_luci2_init_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
412 struct ubus_request_data
*req
, const char *method
,
413 struct blob_attr
*msg
)
417 char *p
, path
[PATH_MAX
];
423 if (!(d
= opendir("/etc/init.d")))
424 return rpc_errno_status();
426 blob_buf_init(&buf
, 0);
427 c
= blobmsg_open_array(&buf
, "initscripts");
429 while ((e
= readdir(d
)) != NULL
)
431 snprintf(path
, sizeof(path
) - 1, "/etc/init.d/%s", e
->d_name
);
433 if (stat(path
, &s
) || !S_ISREG(s
.st_mode
) || !(s
.st_mode
& S_IXUSR
))
436 if ((f
= fopen(path
, "r")) != NULL
)
439 p
= fgets(path
, sizeof(path
) - 1, f
);
441 if (!p
|| !strstr(p
, "/etc/rc.common"))
444 t
= blobmsg_open_table(&buf
, NULL
);
446 blobmsg_add_string(&buf
, "name", e
->d_name
);
448 while (fgets(path
, sizeof(path
) - 1, f
))
450 p
= strtok(path
, "= \t");
452 if (!strcmp(p
, "START") && !!(p
= strtok(NULL
, "= \t\n")))
455 blobmsg_add_u32(&buf
, "start", n
);
457 else if (!strcmp(p
, "STOP") && !!(p
= strtok(NULL
, "= \t\n")))
459 blobmsg_add_u32(&buf
, "stop", atoi(p
));
466 snprintf(path
, sizeof(path
) - 1, "/etc/rc.d/S%02d%s",
469 blobmsg_add_u8(&buf
, "enabled",
470 (!stat(path
, &s
) && (s
.st_mode
& S_IXUSR
)));
474 blobmsg_add_u8(&buf
, "enabled", 0);
477 blobmsg_close_table(&buf
, t
);
485 blobmsg_close_array(&buf
, c
);
487 ubus_send_reply(ctx
, req
, buf
.head
);
492 rpc_luci2_init_action(struct ubus_context
*ctx
, struct ubus_object
*obj
,
493 struct ubus_request_data
*req
, const char *method
,
494 struct blob_attr
*msg
)
501 struct blob_attr
*tb
[__RPC_I_MAX
];
503 blobmsg_parse(rpc_init_policy
, __RPC_I_MAX
, tb
,
504 blob_data(msg
), blob_len(msg
));
506 if (!tb
[RPC_I_NAME
] || !tb
[RPC_I_ACTION
])
507 return UBUS_STATUS_INVALID_ARGUMENT
;
509 action
= blobmsg_data(tb
[RPC_I_ACTION
]);
511 if (strcmp(action
, "start") && strcmp(action
, "stop") &&
512 strcmp(action
, "reload") && strcmp(action
, "restart") &&
513 strcmp(action
, "enable") && strcmp(action
, "disable"))
514 return UBUS_STATUS_INVALID_ARGUMENT
;
516 snprintf(path
, sizeof(path
) - 1, "/etc/init.d/%s",
517 (char *)blobmsg_data(tb
[RPC_I_NAME
]));
520 return rpc_errno_status();
522 if (!(s
.st_mode
& S_IXUSR
))
523 return UBUS_STATUS_PERMISSION_DENIED
;
525 switch ((pid
= fork()))
528 return rpc_errno_status();
533 if ((fd
= open("/dev/null", O_RDWR
)) > -1)
544 if (execl(path
, path
, action
, NULL
))
545 return rpc_errno_status();
553 rpc_luci2_rclocal_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
554 struct ubus_request_data
*req
, const char *method
,
555 struct blob_attr
*msg
)
558 char data
[4096] = { 0 };
560 if (!(f
= fopen("/etc/rc.local", "r")))
561 return rpc_errno_status();
563 fread(data
, sizeof(data
) - 1, 1, f
);
566 blob_buf_init(&buf
, 0);
567 blobmsg_add_string(&buf
, "data", data
);
569 ubus_send_reply(ctx
, req
, buf
.head
);
574 rpc_luci2_rclocal_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
575 struct ubus_request_data
*req
, const char *method
,
576 struct blob_attr
*msg
)
579 struct blob_attr
*tb
[__RPC_D_MAX
];
581 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
582 blob_data(msg
), blob_len(msg
));
584 if (!tb
[RPC_D_DATA
] || blobmsg_data_len(tb
[RPC_D_DATA
]) >= 4096)
585 return UBUS_STATUS_INVALID_ARGUMENT
;
587 if (!(f
= fopen("/etc/rc.local", "w")))
588 return rpc_errno_status();
590 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
591 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
598 rpc_luci2_crontab_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
599 struct ubus_request_data
*req
, const char *method
,
600 struct blob_attr
*msg
)
603 char data
[4096] = { 0 };
605 if (!(f
= fopen("/etc/crontabs/root", "r")))
606 return rpc_errno_status();
608 fread(data
, sizeof(data
) - 1, 1, f
);
611 blob_buf_init(&buf
, 0);
612 blobmsg_add_string(&buf
, "data", data
);
614 ubus_send_reply(ctx
, req
, buf
.head
);
619 rpc_luci2_crontab_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
620 struct ubus_request_data
*req
, const char *method
,
621 struct blob_attr
*msg
)
625 struct blob_attr
*tb
[__RPC_D_MAX
];
627 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
628 blob_data(msg
), blob_len(msg
));
630 if (!tb
[RPC_D_DATA
] || blobmsg_data_len(tb
[RPC_D_DATA
]) >= 4096)
631 return UBUS_STATUS_INVALID_ARGUMENT
;
633 if (stat("/etc/crontabs", &s
) && mkdir("/etc/crontabs", 0755))
634 return rpc_errno_status();
636 if (!(f
= fopen("/etc/crontabs/root", "w")))
637 return rpc_errno_status();
639 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
640 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
647 rpc_luci2_sshkeys_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
648 struct ubus_request_data
*req
, const char *method
,
649 struct blob_attr
*msg
)
655 if (!(f
= fopen("/etc/dropbear/authorized_keys", "r")))
656 return rpc_errno_status();
658 blob_buf_init(&buf
, 0);
659 c
= blobmsg_open_array(&buf
, "keys");
661 while (fgets(line
, sizeof(line
) - 1, f
))
663 for (p
= line
+ strlen(line
) - 1; (p
> line
) && isspace(*p
); p
--)
666 for (p
= line
; isspace(*p
); p
++)
670 blobmsg_add_string(&buf
, NULL
, p
);
673 blobmsg_close_array(&buf
, c
);
676 ubus_send_reply(ctx
, req
, buf
.head
);
681 rpc_luci2_sshkeys_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
682 struct ubus_request_data
*req
, const char *method
,
683 struct blob_attr
*msg
)
687 struct blob_attr
*cur
, *tb
[__RPC_K_MAX
];
689 blobmsg_parse(rpc_sshkey_policy
, __RPC_K_MAX
, tb
,
690 blob_data(msg
), blob_len(msg
));
693 return UBUS_STATUS_INVALID_ARGUMENT
;
695 if (!(f
= fopen("/etc/dropbear/authorized_keys", "w")))
696 return rpc_errno_status();
698 blobmsg_for_each_attr(cur
, tb
[RPC_K_KEYS
], rem
)
700 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
703 fwrite(blobmsg_data(cur
), blobmsg_data_len(cur
) - 1, 1, f
);
704 fwrite("\n", 1, 1, f
);
712 rpc_luci2_password_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
713 struct ubus_request_data
*req
, const char *method
,
714 struct blob_attr
*msg
)
719 struct blob_attr
*tb
[__RPC_P_MAX
];
721 blobmsg_parse(rpc_password_policy
, __RPC_P_MAX
, tb
,
722 blob_data(msg
), blob_len(msg
));
724 if (!tb
[RPC_P_USER
] || !tb
[RPC_P_PASSWORD
])
725 return UBUS_STATUS_INVALID_ARGUMENT
;
727 if (stat("/usr/bin/passwd", &s
))
728 return UBUS_STATUS_NOT_FOUND
;
730 if (!(s
.st_mode
& S_IXUSR
))
731 return UBUS_STATUS_PERMISSION_DENIED
;
734 return rpc_errno_status();
736 switch ((pid
= fork()))
741 return rpc_errno_status();
750 if ((fd
= open("/dev/null", O_RDWR
)) > -1)
759 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
760 blobmsg_data(tb
[RPC_P_USER
]), NULL
))
761 return rpc_errno_status();
766 write(fds
[1], blobmsg_data(tb
[RPC_P_PASSWORD
]),
767 blobmsg_data_len(tb
[RPC_P_PASSWORD
]) - 1);
768 write(fds
[1], "\n", 1);
772 write(fds
[1], blobmsg_data(tb
[RPC_P_PASSWORD
]),
773 blobmsg_data_len(tb
[RPC_P_PASSWORD
]) - 1);
774 write(fds
[1], "\n", 1);
778 waitpid(pid
, NULL
, 0);
785 rpc_luci2_led_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
786 struct ubus_request_data
*req
, const char *method
,
787 struct blob_attr
*msg
)
791 void *list
, *led
, *trigger
;
792 char *p
, *active_trigger
, line
[512];
795 if (!(d
= opendir("/sys/class/leds")))
796 return rpc_errno_status();
798 blob_buf_init(&buf
, 0);
799 list
= blobmsg_open_array(&buf
, "leds");
801 while ((e
= readdir(d
)) != NULL
)
803 snprintf(line
, sizeof(line
) - 1, "/sys/class/leds/%s/trigger",
806 if (!(f
= fopen(line
, "r")))
809 led
= blobmsg_open_table(&buf
, NULL
);
811 blobmsg_add_string(&buf
, "name", e
->d_name
);
813 if (fgets(line
, sizeof(line
) - 1, f
))
815 trigger
= blobmsg_open_array(&buf
, "triggers");
817 for (p
= strtok(line
, " \n"), active_trigger
= NULL
;
819 p
= strtok(NULL
, " \n"))
823 *(p
+ strlen(p
) - 1) = 0;
828 blobmsg_add_string(&buf
, NULL
, p
);
831 blobmsg_close_array(&buf
, trigger
);
834 blobmsg_add_string(&buf
, "active_trigger", active_trigger
);
839 snprintf(line
, sizeof(line
) - 1, "/sys/class/leds/%s/brightness",
842 if ((f
= fopen(line
, "r")) != NULL
)
844 if (fgets(line
, sizeof(line
) - 1, f
))
845 blobmsg_add_u32(&buf
, "brightness", atoi(line
));
850 snprintf(line
, sizeof(line
) - 1, "/sys/class/leds/%s/max_brightness",
853 if ((f
= fopen(line
, "r")) != NULL
)
855 if (fgets(line
, sizeof(line
) - 1, f
))
856 blobmsg_add_u32(&buf
, "max_brightness", atoi(line
));
861 blobmsg_close_table(&buf
, led
);
866 blobmsg_close_array(&buf
, list
);
867 ubus_send_reply(ctx
, req
, buf
.head
);
873 rpc_luci2_usb_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
874 struct ubus_request_data
*req
, const char *method
,
875 struct blob_attr
*msg
)
885 const char *attributes
[] = {
886 "manufacturer", "vendor_name", "s",
887 "product", "product_name", "s",
888 "idVendor", "vendor_id", "x",
889 "idProduct", "product_id", "x",
890 "serial", "serial", "s",
891 "speed", "speed", "d",
894 if (!(d
= opendir("/sys/bus/usb/devices")))
895 return rpc_errno_status();
897 blob_buf_init(&buf
, 0);
898 list
= blobmsg_open_array(&buf
, "devices");
900 while ((e
= readdir(d
)) != NULL
)
902 if (e
->d_name
[0] < '0' || e
->d_name
[0] > '9')
905 snprintf(line
, sizeof(line
) - 1,
906 "/sys/bus/usb/devices/%s/%s", e
->d_name
, attributes
[0]);
911 device
= blobmsg_open_table(&buf
, NULL
);
913 blobmsg_add_string(&buf
, "name", e
->d_name
);
915 for (i
= 0; i
< sizeof(attributes
) / sizeof(attributes
[0]); i
+= 3)
917 snprintf(line
, sizeof(line
) - 1,
918 "/sys/bus/usb/devices/%s/%s", e
->d_name
, attributes
[i
]);
920 if (!(f
= fopen(line
, "r")))
923 if (fgets(line
, sizeof(line
) - 1, f
))
925 switch (*attributes
[i
+2])
928 blobmsg_add_u32(&buf
, attributes
[i
+1],
929 strtoul(line
, NULL
, 16));
933 blobmsg_add_u32(&buf
, attributes
[i
+1],
934 strtoul(line
, NULL
, 10));
938 if ((p
= strchr(line
, '\n')) != NULL
)
939 while (p
> line
&& isspace(*p
))
942 blobmsg_add_string(&buf
, attributes
[i
+1], line
);
950 blobmsg_close_table(&buf
, device
);
955 blobmsg_close_array(&buf
, list
);
956 ubus_send_reply(ctx
, req
, buf
.head
);
962 rpc_luci2_upgrade_test(struct ubus_context
*ctx
, struct ubus_object
*obj
,
963 struct ubus_request_data
*req
, const char *method
,
964 struct blob_attr
*msg
)
966 const char *cmd
[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL
};
967 return rpc_exec(cmd
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
971 rpc_luci2_upgrade_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
972 struct ubus_request_data
*req
, const char *method
,
973 struct blob_attr
*msg
)
979 rpc_luci2_upgrade_clean(struct ubus_context
*ctx
, struct ubus_object
*obj
,
980 struct ubus_request_data
*req
, const char *method
,
981 struct blob_attr
*msg
)
983 if (unlink("/tmp/firmware.bin"))
984 return rpc_errno_status();
990 rpc_luci2_backup_restore(struct ubus_context
*ctx
, struct ubus_object
*obj
,
991 struct ubus_request_data
*req
, const char *method
,
992 struct blob_attr
*msg
)
994 const char *cmd
[4] = { "sysupgrade", "--restore-backup",
995 "/tmp/backup.tar.gz", NULL
};
997 return rpc_exec(cmd
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1001 rpc_luci2_backup_clean(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1002 struct ubus_request_data
*req
, const char *method
,
1003 struct blob_attr
*msg
)
1005 if (unlink("/tmp/backup.tar.gz"))
1006 return rpc_errno_status();
1012 rpc_luci2_backup_config_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1013 struct ubus_request_data
*req
, const char *method
,
1014 struct blob_attr
*msg
)
1017 char conf
[2048] = { 0 };
1019 if (!(f
= fopen("/etc/sysupgrade.conf", "r")))
1020 return rpc_errno_status();
1022 fread(conf
, sizeof(conf
) - 1, 1, f
);
1025 blob_buf_init(&buf
, 0);
1026 blobmsg_add_string(&buf
, "config", conf
);
1028 ubus_send_reply(ctx
, req
, buf
.head
);
1033 rpc_luci2_backup_config_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1034 struct ubus_request_data
*req
, const char *method
,
1035 struct blob_attr
*msg
)
1038 struct blob_attr
*tb
[__RPC_D_MAX
];
1040 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
1041 blob_data(msg
), blob_len(msg
));
1043 if (!tb
[RPC_D_DATA
])
1044 return UBUS_STATUS_INVALID_ARGUMENT
;
1046 if (blobmsg_data_len(tb
[RPC_D_DATA
]) >= 2048)
1047 return UBUS_STATUS_NOT_SUPPORTED
;
1049 if (!(f
= fopen("/etc/sysupgrade.conf", "w")))
1050 return rpc_errno_status();
1052 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
1053 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
1059 struct backup_state
{
1065 backup_parse_list(struct blob_buf
*blob
, char *buf
, int len
, void *priv
)
1067 struct backup_state
*s
= priv
;
1068 char *nl
= strchr(buf
, '\n');
1076 s
->array
= blobmsg_open_array(blob
, "files");
1080 blobmsg_add_string(blob
, NULL
, buf
);
1082 return (nl
- buf
+ 1);
1086 backup_finish_list(struct blob_buf
*blob
, int status
, void *priv
)
1088 struct backup_state
*s
= priv
;
1093 blobmsg_close_array(blob
, s
->array
);
1097 rpc_luci2_backup_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1098 struct ubus_request_data
*req
, const char *method
,
1099 struct blob_attr
*msg
)
1101 struct backup_state
*state
= NULL
;
1102 const char *cmd
[3] = { "sysupgrade", "--list-backup", NULL
};
1104 state
= malloc(sizeof(*state
));
1107 return UBUS_STATUS_UNKNOWN_ERROR
;
1109 memset(state
, 0, sizeof(*state
));
1111 return rpc_exec(cmd
, backup_parse_list
, NULL
, backup_finish_list
,
1116 rpc_luci2_reset_test(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1117 struct ubus_request_data
*req
, const char *method
,
1118 struct blob_attr
*msg
)
1122 char line
[64] = { 0 };
1123 bool supported
= false;
1125 if (!stat("/sbin/mtd", &s
) && (s
.st_mode
& S_IXUSR
))
1127 if ((mtd
= fopen("/proc/mtd", "r")) != NULL
)
1129 while (fgets(line
, sizeof(line
) - 1, mtd
))
1131 if (strstr(line
, "\"rootfs_data\""))
1142 blob_buf_init(&buf
, 0);
1143 blobmsg_add_u8(&buf
, "supported", supported
);
1145 ubus_send_reply(ctx
, req
, buf
.head
);
1151 rpc_luci2_reset_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1152 struct ubus_request_data
*req
, const char *method
,
1153 struct blob_attr
*msg
)
1158 return rpc_errno_status();
1171 execl("/sbin/mtd", "/sbin/mtd", "-r", "erase", "rootfs_data", NULL
);
1173 return rpc_errno_status();
1181 rpc_luci2_reboot(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1182 struct ubus_request_data
*req
, const char *method
,
1183 struct blob_attr
*msg
)
1188 return rpc_errno_status();
1199 execl("/sbin/reboot", "/sbin/reboot", NULL
);
1201 return rpc_errno_status();
1210 dnsmasq_leasefile(void)
1212 FILE *leases
= NULL
;
1213 struct uci_package
*p
;
1214 struct uci_element
*e
;
1215 struct uci_section
*s
;
1216 struct uci_ptr ptr
= {
1219 .option
= "leasefile"
1222 uci_load(cursor
, ptr
.package
, &p
);
1227 uci_foreach_element(&p
->sections
, e
)
1229 s
= uci_to_section(e
);
1231 if (strcmp(s
->type
, "dnsmasq"))
1234 ptr
.section
= e
->name
;
1235 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
1239 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
)
1240 leases
= fopen(ptr
.o
->v
.string
, "r");
1242 uci_unload(cursor
, p
);
1248 rpc_luci2_network_leases(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1249 struct ubus_request_data
*req
, const char *method
,
1250 struct blob_attr
*msg
)
1255 char *ts
, *mac
, *addr
, *name
;
1256 time_t now
= time(NULL
);
1258 blob_buf_init(&buf
, 0);
1259 c
= blobmsg_open_array(&buf
, "leases");
1261 leases
= dnsmasq_leasefile();
1266 while (fgets(line
, sizeof(line
) - 1, leases
))
1268 ts
= strtok(line
, " \t");
1269 mac
= strtok(NULL
, " \t");
1270 addr
= strtok(NULL
, " \t");
1271 name
= strtok(NULL
, " \t");
1273 if (!ts
|| !mac
|| !addr
|| !name
)
1276 if (strchr(addr
, ':'))
1279 d
= blobmsg_open_table(&buf
, NULL
);
1281 blobmsg_add_u32(&buf
, "expires", atoi(ts
) - now
);
1282 blobmsg_add_string(&buf
, "macaddr", mac
);
1283 blobmsg_add_string(&buf
, "ipaddr", addr
);
1285 if (strcmp(name
, "*"))
1286 blobmsg_add_string(&buf
, "hostname", name
);
1288 blobmsg_close_table(&buf
, d
);
1294 blobmsg_close_array(&buf
, c
);
1295 ubus_send_reply(ctx
, req
, buf
.head
);
1301 rpc_luci2_network_leases6(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1302 struct ubus_request_data
*req
, const char *method
,
1303 struct blob_attr
*msg
)
1308 char *ts
, *mac
, *addr
, *name
, *duid
;
1309 time_t now
= time(NULL
);
1311 blob_buf_init(&buf
, 0);
1312 c
= blobmsg_open_array(&buf
, "leases");
1314 leases
= fopen("/tmp/hosts/6relayd", "r");
1318 while (fgets(line
, sizeof(line
) - 1, leases
))
1320 if (strncmp(line
, "# ", 2))
1323 strtok(line
+ 2, " \t"); /* iface */
1325 duid
= strtok(NULL
, " \t");
1327 strtok(NULL
, " \t"); /* iaid */
1329 name
= strtok(NULL
, " \t");
1330 ts
= strtok(NULL
, " \t");
1332 strtok(NULL
, " \t"); /* id */
1333 strtok(NULL
, " \t"); /* length */
1335 addr
= strtok(NULL
, " \t\n");
1340 d
= blobmsg_open_table(&buf
, NULL
);
1342 blobmsg_add_u32(&buf
, "expires", atoi(ts
) - now
);
1343 blobmsg_add_string(&buf
, "duid", duid
);
1344 blobmsg_add_string(&buf
, "ip6addr", addr
);
1346 if (strcmp(name
, "-"))
1347 blobmsg_add_string(&buf
, "hostname", name
);
1349 blobmsg_close_array(&buf
, d
);
1356 leases
= dnsmasq_leasefile();
1361 while (fgets(line
, sizeof(line
) - 1, leases
))
1363 ts
= strtok(line
, " \t");
1364 mac
= strtok(NULL
, " \t");
1365 addr
= strtok(NULL
, " \t");
1366 name
= strtok(NULL
, " \t");
1367 duid
= strtok(NULL
, " \t\n");
1369 if (!ts
|| !mac
|| !addr
|| !duid
)
1372 if (!strchr(addr
, ':'))
1375 d
= blobmsg_open_table(&buf
, NULL
);
1377 blobmsg_add_u32(&buf
, "expires", atoi(ts
) - now
);
1378 blobmsg_add_string(&buf
, "macaddr", mac
);
1379 blobmsg_add_string(&buf
, "ip6addr", addr
);
1381 if (strcmp(name
, "*"))
1382 blobmsg_add_string(&buf
, "hostname", name
);
1384 if (strcmp(duid
, "*"))
1385 blobmsg_add_string(&buf
, "duid", name
);
1387 blobmsg_close_table(&buf
, d
);
1394 blobmsg_close_array(&buf
, c
);
1395 ubus_send_reply(ctx
, req
, buf
.head
);
1401 rpc_luci2_network_ct_count(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1402 struct ubus_request_data
*req
, const char *method
,
1403 struct blob_attr
*msg
)
1408 blob_buf_init(&buf
, 0);
1410 if ((f
= fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL
)
1412 if (fgets(line
, sizeof(line
) - 1, f
))
1413 blobmsg_add_u32(&buf
, "count", atoi(line
));
1418 if ((f
= fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL
)
1420 if (fgets(line
, sizeof(line
) - 1, f
))
1421 blobmsg_add_u32(&buf
, "limit", atoi(line
));
1426 ubus_send_reply(ctx
, req
, buf
.head
);
1432 rpc_luci2_network_ct_table(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1433 struct ubus_request_data
*req
, const char *method
,
1434 struct blob_attr
*msg
)
1442 blob_buf_init(&buf
, 0);
1443 c
= blobmsg_open_array(&buf
, "entries");
1445 if ((f
= fopen("/proc/net/nf_conntrack", "r")) != NULL
)
1447 while (fgets(line
, sizeof(line
) - 1, f
))
1449 d
= blobmsg_open_table(&buf
, NULL
);
1450 memset(seen
, 0, sizeof(seen
));
1452 for (i
= 0, p
= strtok(line
, " "); p
; i
++, p
= strtok(NULL
, " "))
1455 blobmsg_add_u8(&buf
, "ipv6", !strcmp(p
, "ipv6"));
1457 blobmsg_add_u32(&buf
, "protocol", atoi(p
));
1459 blobmsg_add_u32(&buf
, "expires", atoi(p
));
1465 if (!seen
[0] && !strncmp(p
, "src=", 4))
1467 blobmsg_add_string(&buf
, "src", p
+ 4);
1470 else if (!seen
[1] && !strncmp(p
, "dst=", 4))
1472 blobmsg_add_string(&buf
, "dest", p
+ 4);
1475 else if (!seen
[2] && !strncmp(p
, "sport=", 6))
1477 blobmsg_add_u32(&buf
, "sport", atoi(p
+ 6));
1480 else if (!seen
[3] && !strncmp(p
, "dport=", 6))
1482 blobmsg_add_u32(&buf
, "dport", atoi(p
+ 6));
1485 else if (!strncmp(p
, "packets=", 8))
1487 blobmsg_add_u32(&buf
,
1488 seen
[4] ? "tx_packets" : "rx_packets",
1492 else if (!strncmp(p
, "bytes=", 6))
1494 blobmsg_add_u32(&buf
,
1495 seen
[5] ? "tx_bytes" : "rx_bytes",
1502 blobmsg_close_table(&buf
, d
);
1508 blobmsg_close_array(&buf
, c
);
1509 ubus_send_reply(ctx
, req
, buf
.head
);
1515 rpc_luci2_network_arp_table(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1516 struct ubus_request_data
*req
, const char *method
,
1517 struct blob_attr
*msg
)
1521 char *addr
, *mac
, *dev
, line
[128];
1523 blob_buf_init(&buf
, 0);
1524 c
= blobmsg_open_array(&buf
, "entries");
1526 if ((f
= fopen("/proc/net/arp", "r")) != NULL
)
1528 /* skip header line */
1529 fgets(line
, sizeof(line
) - 1, f
);
1531 while (fgets(line
, sizeof(line
) - 1, f
))
1533 addr
= strtok(line
, " \t");
1535 strtok(NULL
, " \t"); /* HW type */
1536 strtok(NULL
, " \t"); /* Flags */
1538 mac
= strtok(NULL
, " \t");
1540 strtok(NULL
, " \t"); /* Mask */
1542 dev
= strtok(NULL
, " \t\n");
1547 d
= blobmsg_open_table(&buf
, NULL
);
1548 blobmsg_add_string(&buf
, "ipaddr", addr
);
1549 blobmsg_add_string(&buf
, "macaddr", mac
);
1550 blobmsg_add_string(&buf
, "device", dev
);
1551 blobmsg_close_table(&buf
, d
);
1557 blobmsg_close_array(&buf
, c
);
1558 ubus_send_reply(ctx
, req
, buf
.head
);
1564 put_hexaddr(const char *name
, const char *s
, const char *m
)
1568 char as
[sizeof("255.255.255.255/32\0")];
1570 a
.s_addr
= strtoul(s
, NULL
, 16);
1571 inet_ntop(AF_INET
, &a
, as
, sizeof(as
));
1575 for (a
.s_addr
= ntohl(strtoul(m
, NULL
, 16)), bits
= 0;
1576 a
.s_addr
& 0x80000000;
1580 sprintf(as
+ strlen(as
), "/%u", bits
);
1583 blobmsg_add_string(&buf
, name
, as
);
1587 rpc_luci2_network_routes(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1588 struct ubus_request_data
*req
, const char *method
,
1589 struct blob_attr
*msg
)
1593 char *dst
, *dmask
, *next
, *metric
, *device
;
1597 if (!(routes
= fopen("/proc/net/route", "r")))
1598 return rpc_errno_status();
1600 blob_buf_init(&buf
, 0);
1601 c
= blobmsg_open_array(&buf
, "routes");
1603 /* skip header line */
1604 fgets(line
, sizeof(line
) - 1, routes
);
1606 while (fgets(line
, sizeof(line
) - 1, routes
))
1608 device
= strtok(line
, "\t ");
1609 dst
= strtok(NULL
, "\t ");
1610 next
= strtok(NULL
, "\t ");
1612 strtok(NULL
, "\t "); /* flags */
1613 strtok(NULL
, "\t "); /* refcount */
1614 strtok(NULL
, "\t "); /* usecount */
1616 metric
= strtok(NULL
, "\t ");
1617 dmask
= strtok(NULL
, "\t ");
1622 d
= blobmsg_open_table(&buf
, NULL
);
1624 put_hexaddr("target", dst
, dmask
);
1625 put_hexaddr("nexthop", next
, NULL
);
1627 n
= strtoul(metric
, NULL
, 10);
1628 blobmsg_add_u32(&buf
, "metric", n
);
1630 blobmsg_add_string(&buf
, "device", device
);
1632 blobmsg_close_table(&buf
, d
);
1635 blobmsg_close_array(&buf
, c
);
1638 ubus_send_reply(ctx
, req
, buf
.head
);
1643 put_hex6addr(const char *name
, const char *s
, const char *m
)
1647 char as
[INET6_ADDRSTRLEN
+ sizeof("/128")];
1650 (((x) <= '9') ? ((x) - '0') : \
1651 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1654 for (i
= 0; i
< 16; i
++, s
+= 2)
1655 a
.s6_addr
[i
] = (16 * hex(*s
)) + hex(*(s
+1));
1657 inet_ntop(AF_INET6
, &a
, as
, sizeof(as
));
1660 sprintf(as
+ strlen(as
), "/%lu", strtoul(m
, NULL
, 16));
1662 blobmsg_add_string(&buf
, name
, as
);
1666 rpc_luci2_network_routes6(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1667 struct ubus_request_data
*req
, const char *method
,
1668 struct blob_attr
*msg
)
1672 char *src
, *smask
, *dst
, *dmask
, *next
, *metric
, *flags
, *device
;
1676 if (!(routes
= fopen("/proc/net/ipv6_route", "r")))
1677 return rpc_errno_status();
1679 blob_buf_init(&buf
, 0);
1680 c
= blobmsg_open_array(&buf
, "routes");
1682 while (fgets(line
, sizeof(line
) - 1, routes
))
1684 dst
= strtok(line
, " ");
1685 dmask
= strtok(NULL
, " ");
1686 src
= strtok(NULL
, " ");
1687 smask
= strtok(NULL
, " ");
1688 next
= strtok(NULL
, " ");
1689 metric
= strtok(NULL
, " ");
1691 strtok(NULL
, " "); /* refcount */
1692 strtok(NULL
, " "); /* usecount */
1694 flags
= strtok(NULL
, " ");
1695 device
= strtok(NULL
, " \n");
1700 n
= strtoul(flags
, NULL
, 16);
1705 d
= blobmsg_open_table(&buf
, NULL
);
1707 put_hex6addr("target", dst
, dmask
);
1708 put_hex6addr("source", src
, smask
);
1709 put_hex6addr("nexthop", next
, NULL
);
1711 n
= strtoul(metric
, NULL
, 16);
1712 blobmsg_add_u32(&buf
, "metric", n
);
1714 blobmsg_add_string(&buf
, "device", device
);
1716 blobmsg_close_table(&buf
, d
);
1719 blobmsg_close_array(&buf
, c
);
1722 ubus_send_reply(ctx
, req
, buf
.head
);
1738 opkg_parse_list(struct blob_buf
*blob
, char *buf
, int len
, void *priv
)
1740 struct opkg_state
*s
= priv
;
1743 char *nl
= strchr(buf
, '\n');
1744 char *name
= NULL
, *vers
= NULL
, *desc
= NULL
;
1752 if (s
->cur_offset
++ < s
->req_offset
)
1755 if (s
->cur_count
++ >= s
->req_count
)
1761 s
->array
= blobmsg_open_array(blob
, "packages");
1764 for (ptr
= buf
, last
= buf
, *nl
= 0; ptr
<= nl
; ptr
++)
1766 if (!*ptr
|| (*ptr
== ' ' && *(ptr
+1) == '-' && *(ptr
+2) == ' '))
1778 desc
= *ptr
? (ptr
+ 3) : NULL
;
1787 c
= blobmsg_open_array(blob
, NULL
);
1789 blobmsg_add_string(blob
, NULL
, name
);
1790 blobmsg_add_string(blob
, NULL
, vers
);
1793 blobmsg_add_string(blob
, NULL
, desc
);
1795 blobmsg_close_array(blob
, c
);
1799 return (nl
- buf
+ 1);
1803 opkg_finish_list(struct blob_buf
*blob
, int status
, void *priv
)
1805 struct opkg_state
*s
= priv
;
1810 blobmsg_close_array(blob
, s
->array
);
1811 blobmsg_add_u32(blob
, "total", s
->total
);
1815 opkg_exec_list(const char *action
, struct blob_attr
*msg
,
1816 struct ubus_context
*ctx
, struct ubus_request_data
*req
)
1818 struct opkg_state
*state
= NULL
;
1819 struct blob_attr
*tb
[__RPC_OM_MAX
];
1820 const char *cmd
[5] = { "opkg", action
, "-nocase", NULL
, NULL
};
1822 blobmsg_parse(rpc_opkg_match_policy
, __RPC_OM_MAX
, tb
,
1823 blob_data(msg
), blob_len(msg
));
1825 state
= malloc(sizeof(*state
));
1828 return UBUS_STATUS_UNKNOWN_ERROR
;
1830 memset(state
, 0, sizeof(*state
));
1832 if (tb
[RPC_OM_PATTERN
])
1833 cmd
[3] = blobmsg_data(tb
[RPC_OM_PATTERN
]);
1835 if (tb
[RPC_OM_LIMIT
])
1836 state
->req_count
= blobmsg_get_u32(tb
[RPC_OM_LIMIT
]);
1838 if (tb
[RPC_OM_OFFSET
])
1839 state
->req_offset
= blobmsg_get_u32(tb
[RPC_OM_OFFSET
]);
1841 if (state
->req_offset
< 0)
1842 state
->req_offset
= 0;
1844 if (state
->req_count
<= 0 || state
->req_count
> 100)
1845 state
->req_count
= 100;
1847 return rpc_exec(cmd
, opkg_parse_list
, NULL
, opkg_finish_list
,
1853 rpc_luci2_opkg_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1854 struct ubus_request_data
*req
, const char *method
,
1855 struct blob_attr
*msg
)
1857 return opkg_exec_list("list", msg
, ctx
, req
);
1861 rpc_luci2_opkg_list_installed(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1862 struct ubus_request_data
*req
, const char *method
,
1863 struct blob_attr
*msg
)
1865 return opkg_exec_list("list-installed", msg
, ctx
, req
);
1869 rpc_luci2_opkg_find(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1870 struct ubus_request_data
*req
, const char *method
,
1871 struct blob_attr
*msg
)
1873 return opkg_exec_list("find", msg
, ctx
, req
);
1877 rpc_luci2_opkg_update(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1878 struct ubus_request_data
*req
, const char *method
,
1879 struct blob_attr
*msg
)
1881 const char *cmd
[3] = { "opkg", "update", NULL
};
1882 return rpc_exec(cmd
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1886 rpc_luci2_opkg_install(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1887 struct ubus_request_data
*req
, const char *method
,
1888 struct blob_attr
*msg
)
1890 struct blob_attr
*tb
[__RPC_OP_MAX
];
1891 const char *cmd
[5] = { "opkg", "--force-overwrite",
1892 "install", NULL
, NULL
};
1894 blobmsg_parse(rpc_opkg_package_policy
, __RPC_OP_MAX
, tb
,
1895 blob_data(msg
), blob_len(msg
));
1897 if (!tb
[RPC_OP_PACKAGE
])
1898 return UBUS_STATUS_INVALID_ARGUMENT
;
1900 cmd
[3] = blobmsg_data(tb
[RPC_OP_PACKAGE
]);
1902 return rpc_exec(cmd
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1906 rpc_luci2_opkg_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1907 struct ubus_request_data
*req
, const char *method
,
1908 struct blob_attr
*msg
)
1910 struct blob_attr
*tb
[__RPC_OP_MAX
];
1911 const char *cmd
[5] = { "opkg", "--force-removal-of-dependent-packages",
1912 "remove", NULL
, NULL
};
1914 blobmsg_parse(rpc_opkg_package_policy
, __RPC_OP_MAX
, tb
,
1915 blob_data(msg
), blob_len(msg
));
1917 if (!tb
[RPC_OP_PACKAGE
])
1918 return UBUS_STATUS_INVALID_ARGUMENT
;
1920 cmd
[3] = blobmsg_data(tb
[RPC_OP_PACKAGE
]);
1922 return rpc_exec(cmd
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1926 rpc_luci2_opkg_config_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1927 struct ubus_request_data
*req
, const char *method
,
1928 struct blob_attr
*msg
)
1931 char conf
[2048] = { 0 };
1933 if (!(f
= fopen("/etc/opkg.conf", "r")))
1934 return rpc_errno_status();
1936 fread(conf
, sizeof(conf
) - 1, 1, f
);
1939 blob_buf_init(&buf
, 0);
1940 blobmsg_add_string(&buf
, "config", conf
);
1942 ubus_send_reply(ctx
, req
, buf
.head
);
1947 rpc_luci2_opkg_config_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1948 struct ubus_request_data
*req
, const char *method
,
1949 struct blob_attr
*msg
)
1952 struct blob_attr
*tb
[__RPC_D_MAX
];
1954 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
1955 blob_data(msg
), blob_len(msg
));
1957 if (!tb
[RPC_D_DATA
])
1958 return UBUS_STATUS_INVALID_ARGUMENT
;
1960 if (blobmsg_data_len(tb
[RPC_D_DATA
]) >= 2048)
1961 return UBUS_STATUS_NOT_SUPPORTED
;
1963 if (!(f
= fopen("/etc/opkg.conf", "w")))
1964 return rpc_errno_status();
1966 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
1967 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
1975 menu_access(struct blob_attr
*sid
, struct blob_attr
*acls
, struct blob_buf
*e
)
1978 struct blob_attr
*acl
;
1982 c
= blobmsg_open_table(e
, "write");
1984 blobmsg_for_each_attr(acl
, acls
, rem
)
1986 if (!rpc_session_access(blobmsg_data(sid
), "luci-ui",
1987 blobmsg_data(acl
), "read"))
1993 blobmsg_add_u8(e
, blobmsg_data(acl
),
1994 rpc_session_access(blobmsg_data(sid
), "luci-ui",
1995 blobmsg_data(acl
), "write"));
1998 blobmsg_close_table(e
, c
);
2004 rpc_luci2_ui_menu(struct ubus_context
*ctx
, struct ubus_object
*obj
,
2005 struct ubus_request_data
*req
, const char *method
,
2006 struct blob_attr
*msg
)
2010 struct blob_buf menu
= { 0 };
2011 struct blob_buf item
= { 0 };
2012 struct blob_attr
*entry
, *attr
;
2013 struct blob_attr
*tb
[__RPC_MENU_MAX
];
2017 blobmsg_parse(rpc_menu_policy
, __RPC_MENU_MAX
, tb
,
2018 blob_data(msg
), blob_len(msg
));
2020 if (!tb
[RPC_MENU_SESSION
])
2021 return UBUS_STATUS_INVALID_ARGUMENT
;
2024 blob_buf_init(&buf
, 0);
2025 c
= blobmsg_open_table(&buf
, "menu");
2027 if (!glob(RPC_LUCI2_MENU_FILES
, 0, NULL
, &gl
))
2029 for (i
= 0; i
< gl
.gl_pathc
; i
++)
2031 blob_buf_init(&menu
, 0);
2033 if (!blobmsg_add_json_from_file(&menu
, gl
.gl_pathv
[i
]))
2036 blob_for_each_attr(entry
, menu
.head
, rem
)
2040 blob_buf_init(&item
, 0);
2041 d
= blobmsg_open_table(&item
, blobmsg_name(entry
));
2043 blobmsg_for_each_attr(attr
, entry
, rem2
)
2045 if (blob_id(attr
) == BLOBMSG_TYPE_ARRAY
&&
2046 !strcmp(blobmsg_name(attr
), "acls"))
2047 access
= menu_access(tb
[RPC_MENU_SESSION
], attr
, &item
);
2049 blobmsg_add_blob(&item
, attr
);
2052 blobmsg_close_table(&item
, d
);
2055 blob_for_each_attr(attr
, item
.head
, rem2
)
2056 blobmsg_add_blob(&buf
, attr
);
2058 blob_buf_free(&item
);
2062 blob_buf_free(&menu
);
2068 blobmsg_close_table(&buf
, c
);
2070 ubus_send_reply(ctx
, req
, buf
.head
);
2075 int rpc_luci2_api_init(struct ubus_context
*ctx
)
2079 static const struct ubus_method luci2_system_methods
[] = {
2080 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log
),
2081 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg
),
2082 UBUS_METHOD_NOARG("diskfree", rpc_luci2_system_diskfree
),
2083 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list
),
2084 UBUS_METHOD("process_signal", rpc_luci2_process_signal
,
2086 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list
),
2087 UBUS_METHOD("init_action", rpc_luci2_init_action
,
2089 UBUS_METHOD_NOARG("rclocal_get", rpc_luci2_rclocal_get
),
2090 UBUS_METHOD("rclocal_set", rpc_luci2_rclocal_set
,
2092 UBUS_METHOD_NOARG("crontab_get", rpc_luci2_crontab_get
),
2093 UBUS_METHOD("crontab_set", rpc_luci2_crontab_set
,
2095 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get
),
2096 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set
,
2098 UBUS_METHOD("password_set", rpc_luci2_password_set
,
2099 rpc_password_policy
),
2100 UBUS_METHOD_NOARG("led_list", rpc_luci2_led_list
),
2101 UBUS_METHOD_NOARG("usb_list", rpc_luci2_usb_list
),
2102 UBUS_METHOD_NOARG("upgrade_test", rpc_luci2_upgrade_test
),
2103 UBUS_METHOD("upgrade_start", rpc_luci2_upgrade_start
,
2104 rpc_upgrade_policy
),
2105 UBUS_METHOD_NOARG("upgrade_clean", rpc_luci2_upgrade_clean
),
2106 UBUS_METHOD_NOARG("backup_restore", rpc_luci2_backup_restore
),
2107 UBUS_METHOD_NOARG("backup_clean", rpc_luci2_backup_clean
),
2108 UBUS_METHOD_NOARG("backup_config_get", rpc_luci2_backup_config_get
),
2109 UBUS_METHOD("backup_config_set", rpc_luci2_backup_config_set
,
2111 UBUS_METHOD_NOARG("backup_list", rpc_luci2_backup_list
),
2112 UBUS_METHOD_NOARG("reset_test", rpc_luci2_reset_test
),
2113 UBUS_METHOD_NOARG("reset_start", rpc_luci2_reset_start
),
2114 UBUS_METHOD_NOARG("reboot", rpc_luci2_reboot
)
2117 static struct ubus_object_type luci2_system_type
=
2118 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods
);
2120 static struct ubus_object system_obj
= {
2121 .name
= "luci2.system",
2122 .type
= &luci2_system_type
,
2123 .methods
= luci2_system_methods
,
2124 .n_methods
= ARRAY_SIZE(luci2_system_methods
),
2128 static const struct ubus_method luci2_network_methods
[] = {
2129 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count
),
2130 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table
),
2131 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table
),
2132 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases
),
2133 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6
),
2134 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes
),
2135 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6
),
2138 static struct ubus_object_type luci2_network_type
=
2139 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods
);
2141 static struct ubus_object network_obj
= {
2142 .name
= "luci2.network",
2143 .type
= &luci2_network_type
,
2144 .methods
= luci2_network_methods
,
2145 .n_methods
= ARRAY_SIZE(luci2_network_methods
),
2149 static const struct ubus_method luci2_opkg_methods
[] = {
2150 UBUS_METHOD("list", rpc_luci2_opkg_list
,
2151 rpc_opkg_match_policy
),
2152 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed
,
2153 rpc_opkg_match_policy
),
2154 UBUS_METHOD("find", rpc_luci2_opkg_find
,
2155 rpc_opkg_match_policy
),
2156 UBUS_METHOD("install", rpc_luci2_opkg_install
,
2157 rpc_opkg_package_policy
),
2158 UBUS_METHOD("remove", rpc_luci2_opkg_remove
,
2159 rpc_opkg_package_policy
),
2160 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update
),
2161 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get
),
2162 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set
,
2166 static struct ubus_object_type luci2_opkg_type
=
2167 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods
);
2169 static struct ubus_object opkg_obj
= {
2170 .name
= "luci2.opkg",
2171 .type
= &luci2_opkg_type
,
2172 .methods
= luci2_opkg_methods
,
2173 .n_methods
= ARRAY_SIZE(luci2_opkg_methods
),
2177 static const struct ubus_method luci2_ui_methods
[] = {
2178 UBUS_METHOD_NOARG("menu", rpc_luci2_ui_menu
)
2181 static struct ubus_object_type luci2_ui_type
=
2182 UBUS_OBJECT_TYPE("luci-rpc-luci2-ui", luci2_ui_methods
);
2184 static struct ubus_object ui_obj
= {
2186 .type
= &luci2_ui_type
,
2187 .methods
= luci2_ui_methods
,
2188 .n_methods
= ARRAY_SIZE(luci2_ui_methods
),
2191 cursor
= uci_alloc_context();
2194 return UBUS_STATUS_UNKNOWN_ERROR
;
2196 rv
|= ubus_add_object(ctx
, &system_obj
);
2197 rv
|= ubus_add_object(ctx
, &network_obj
);
2198 rv
|= ubus_add_object(ctx
, &opkg_obj
);
2199 rv
|= ubus_add_object(ctx
, &ui_obj
);