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.
19 #define _GNU_SOURCE /* crypt() */
29 #include <sys/types.h>
30 #include <sys/statvfs.h>
32 #include <arpa/inet.h>
35 #include <libubox/blobmsg_json.h>
36 #include <libubox/avl-cmp.h>
40 #include <rpcd/plugin.h>
42 /* limit of log size buffer */
43 #define RPC_LUCI2_MAX_LOGSIZE (128 * 1024)
44 #define RPC_LUCI2_DEF_LOGSIZE (16 * 1024)
46 /* location of menu definitions */
47 #define RPC_LUCI2_MENU_FILES "/usr/share/rpcd/menu.d/*.json" /* */
50 static const struct rpc_daemon_ops
*ops
;
52 static struct blob_buf buf
;
53 static struct uci_context
*cursor
;
61 static const struct blobmsg_policy rpc_signal_policy
[__RPC_S_MAX
] = {
62 [RPC_S_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
63 [RPC_S_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
72 static const struct blobmsg_policy rpc_init_policy
[__RPC_I_MAX
] = {
73 [RPC_I_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
74 [RPC_I_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_STRING
},
82 static const struct blobmsg_policy rpc_data_policy
[__RPC_D_MAX
] = {
83 [RPC_D_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_STRING
},
91 static const struct blobmsg_policy rpc_sshkey_policy
[__RPC_K_MAX
] = {
92 [RPC_K_KEYS
] = { .name
= "keys", .type
= BLOBMSG_TYPE_ARRAY
},
101 static const struct blobmsg_policy rpc_password_policy
[__RPC_P_MAX
] = {
102 [RPC_P_USER
] = { .name
= "user", .type
= BLOBMSG_TYPE_STRING
},
103 [RPC_P_PASSWORD
] = { .name
= "password", .type
= BLOBMSG_TYPE_STRING
},
113 static const struct blobmsg_policy rpc_opkg_match_policy
[__RPC_OM_MAX
] = {
114 [RPC_OM_LIMIT
] = { .name
= "limit", .type
= BLOBMSG_TYPE_INT32
},
115 [RPC_OM_OFFSET
] = { .name
= "offset", .type
= BLOBMSG_TYPE_INT32
},
116 [RPC_OM_PATTERN
] = { .name
= "pattern", .type
= BLOBMSG_TYPE_STRING
},
124 static const struct blobmsg_policy rpc_opkg_package_policy
[__RPC_OP_MAX
] = {
125 [RPC_OP_PACKAGE
] = { .name
= "package", .type
= BLOBMSG_TYPE_STRING
},
133 static const struct blobmsg_policy rpc_upgrade_policy
[__RPC_UPGRADE_MAX
] = {
134 [RPC_UPGRADE_KEEP
] = { .name
= "keep", .type
= BLOBMSG_TYPE_BOOL
},
142 static const struct blobmsg_policy rpc_menu_policy
[__RPC_MENU_MAX
] = {
143 [RPC_MENU_SESSION
] = { .name
= "ubus_rpc_session",
144 .type
= BLOBMSG_TYPE_STRING
},
149 rpc_errno_status(void)
154 return UBUS_STATUS_PERMISSION_DENIED
;
157 return UBUS_STATUS_INVALID_ARGUMENT
;
160 return UBUS_STATUS_NOT_FOUND
;
163 return UBUS_STATUS_INVALID_ARGUMENT
;
166 return UBUS_STATUS_UNKNOWN_ERROR
;
171 log_read(FILE *log
, int logsize
)
177 logsize
= RPC_LUCI2_DEF_LOGSIZE
;
179 len
= (logsize
> RPC_LUCI2_MAX_LOGSIZE
) ? RPC_LUCI2_MAX_LOGSIZE
: logsize
;
180 logbuf
= blobmsg_alloc_string_buffer(&buf
, "log", len
+ 1);
185 while (logsize
> RPC_LUCI2_MAX_LOGSIZE
)
187 len
= logsize
% RPC_LUCI2_MAX_LOGSIZE
;
190 len
= RPC_LUCI2_MAX_LOGSIZE
;
192 fread(logbuf
, 1, len
, log
);
196 len
= fread(logbuf
, 1, logsize
, log
);
199 blobmsg_add_string_buffer(&buf
);
203 rpc_luci2_system_log(struct ubus_context
*ctx
, struct ubus_object
*obj
,
204 struct ubus_request_data
*req
, const char *method
,
205 struct blob_attr
*msg
)
209 const char *logfile
= NULL
;
211 struct uci_package
*p
;
212 struct uci_element
*e
;
213 struct uci_section
*s
;
214 struct uci_ptr ptr
= { .package
= "system" };
216 uci_load(cursor
, ptr
.package
, &p
);
219 return UBUS_STATUS_NOT_FOUND
;
221 uci_foreach_element(&p
->sections
, e
)
223 s
= uci_to_section(e
);
225 if (strcmp(s
->type
, "system"))
229 ptr
.option
= "log_type";
230 ptr
.section
= e
->name
;
231 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
235 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
&&
236 !strcmp(ptr
.o
->v
.string
, "file"))
239 ptr
.option
= "log_file";
240 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
242 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
)
243 logfile
= ptr
.o
->v
.string
;
245 logfile
= "/var/log/messages";
247 if (stat(logfile
, &st
) || !(log
= fopen(logfile
, "r")))
250 logsize
= st
.st_size
;
255 ptr
.option
= "log_size";
256 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
258 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
)
259 logsize
= atoi(ptr
.o
->v
.string
) * 1024;
261 if (!(log
= popen("logread", "r")))
265 blob_buf_init(&buf
, 0);
267 log_read(log
, logsize
);
270 uci_unload(cursor
, p
);
271 ubus_send_reply(ctx
, req
, buf
.head
);
275 uci_unload(cursor
, p
);
276 return rpc_errno_status();
280 rpc_luci2_system_dmesg(struct ubus_context
*ctx
, struct ubus_object
*obj
,
281 struct ubus_request_data
*req
, const char *method
,
282 struct blob_attr
*msg
)
286 if (!(log
= popen("dmesg", "r")))
287 return rpc_errno_status();
289 blob_buf_init(&buf
, 0);
291 log_read(log
, RPC_LUCI2_MAX_LOGSIZE
);
294 ubus_send_reply(ctx
, req
, buf
.head
);
299 rpc_luci2_system_diskfree(struct ubus_context
*ctx
, struct ubus_object
*obj
,
300 struct ubus_request_data
*req
, const char *method
,
301 struct blob_attr
*msg
)
306 const char *fslist
[] = {
311 blob_buf_init(&buf
, 0);
313 for (i
= 0; i
< sizeof(fslist
) / sizeof(fslist
[0]); i
+= 2)
315 if (statvfs(fslist
[i
], &s
))
318 c
= blobmsg_open_table(&buf
, fslist
[i
+1]);
320 blobmsg_add_u32(&buf
, "total", s
.f_blocks
* s
.f_frsize
);
321 blobmsg_add_u32(&buf
, "free", s
.f_bfree
* s
.f_frsize
);
322 blobmsg_add_u32(&buf
, "used", (s
.f_blocks
- s
.f_bfree
) * s
.f_frsize
);
324 blobmsg_close_table(&buf
, c
);
327 ubus_send_reply(ctx
, req
, buf
.head
);
332 rpc_luci2_process_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
333 struct ubus_request_data
*req
, const char *method
,
334 struct blob_attr
*msg
)
339 char *pid
, *ppid
, *user
, *stat
, *vsz
, *pvsz
, *pcpu
, *cmd
;
341 if (!(top
= popen("/bin/busybox top -bn1", "r")))
342 return rpc_errno_status();
344 blob_buf_init(&buf
, 0);
345 c
= blobmsg_open_array(&buf
, "processes");
347 while (fgets(line
, sizeof(line
) - 1, top
))
349 pid
= strtok(line
, " ");
351 if (*pid
< '0' || *pid
> '9')
354 ppid
= strtok(NULL
, " ");
355 user
= strtok(NULL
, " ");
356 stat
= strtok(NULL
, " ");
369 vsz
= strtok(stat
+ 4, " ");
370 pvsz
= strtok(NULL
, " ");
371 pcpu
= strtok(NULL
, " ");
372 cmd
= strtok(NULL
, "\n");
377 d
= blobmsg_open_table(&buf
, NULL
);
379 blobmsg_add_u32(&buf
, "pid", atoi(pid
));
380 blobmsg_add_u32(&buf
, "ppid", atoi(ppid
));
381 blobmsg_add_string(&buf
, "user", user
);
382 blobmsg_add_string(&buf
, "stat", stat
);
383 blobmsg_add_u32(&buf
, "vsize", atoi(vsz
) * 1024);
384 blobmsg_add_u32(&buf
, "vsize_percent", atoi(pvsz
));
385 blobmsg_add_u32(&buf
, "cpu_percent", atoi(pcpu
));
386 blobmsg_add_string(&buf
, "command", cmd
);
388 blobmsg_close_table(&buf
, d
);
392 blobmsg_close_array(&buf
, c
);
394 ubus_send_reply(ctx
, req
, buf
.head
);
399 rpc_luci2_process_signal(struct ubus_context
*ctx
, struct ubus_object
*obj
,
400 struct ubus_request_data
*req
, const char *method
,
401 struct blob_attr
*msg
)
404 struct blob_attr
*tb
[__RPC_S_MAX
];
406 blobmsg_parse(rpc_signal_policy
, __RPC_S_MAX
, tb
,
407 blob_data(msg
), blob_len(msg
));
409 if (!tb
[RPC_S_SIGNAL
] || !tb
[RPC_S_PID
])
412 return rpc_errno_status();
415 pid
= blobmsg_get_u32(tb
[RPC_S_PID
]);
416 sig
= blobmsg_get_u32(tb
[RPC_S_SIGNAL
]);
419 return rpc_errno_status();
425 rpc_luci2_init_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
426 struct ubus_request_data
*req
, const char *method
,
427 struct blob_attr
*msg
)
431 char *p
, path
[PATH_MAX
];
437 if (!(d
= opendir("/etc/init.d")))
438 return rpc_errno_status();
440 blob_buf_init(&buf
, 0);
441 c
= blobmsg_open_array(&buf
, "initscripts");
443 while ((e
= readdir(d
)) != NULL
)
445 snprintf(path
, sizeof(path
) - 1, "/etc/init.d/%s", e
->d_name
);
447 if (stat(path
, &s
) || !S_ISREG(s
.st_mode
) || !(s
.st_mode
& S_IXUSR
))
450 if ((f
= fopen(path
, "r")) != NULL
)
453 p
= fgets(path
, sizeof(path
) - 1, f
);
455 if (!p
|| !strstr(p
, "/etc/rc.common"))
458 t
= blobmsg_open_table(&buf
, NULL
);
460 blobmsg_add_string(&buf
, "name", e
->d_name
);
462 while (fgets(path
, sizeof(path
) - 1, f
))
464 p
= strtok(path
, "= \t");
466 if (!strcmp(p
, "START") && !!(p
= strtok(NULL
, "= \t\n")))
469 blobmsg_add_u32(&buf
, "start", n
);
471 else if (!strcmp(p
, "STOP") && !!(p
= strtok(NULL
, "= \t\n")))
473 blobmsg_add_u32(&buf
, "stop", atoi(p
));
480 snprintf(path
, sizeof(path
) - 1, "/etc/rc.d/S%02d%s",
483 blobmsg_add_u8(&buf
, "enabled",
484 (!stat(path
, &s
) && (s
.st_mode
& S_IXUSR
)));
488 blobmsg_add_u8(&buf
, "enabled", 0);
491 blobmsg_close_table(&buf
, t
);
499 blobmsg_close_array(&buf
, c
);
501 ubus_send_reply(ctx
, req
, buf
.head
);
506 rpc_luci2_init_action(struct ubus_context
*ctx
, struct ubus_object
*obj
,
507 struct ubus_request_data
*req
, const char *method
,
508 struct blob_attr
*msg
)
515 struct blob_attr
*tb
[__RPC_I_MAX
];
517 blobmsg_parse(rpc_init_policy
, __RPC_I_MAX
, tb
,
518 blob_data(msg
), blob_len(msg
));
520 if (!tb
[RPC_I_NAME
] || !tb
[RPC_I_ACTION
])
521 return UBUS_STATUS_INVALID_ARGUMENT
;
523 action
= blobmsg_data(tb
[RPC_I_ACTION
]);
525 if (strcmp(action
, "start") && strcmp(action
, "stop") &&
526 strcmp(action
, "reload") && strcmp(action
, "restart") &&
527 strcmp(action
, "enable") && strcmp(action
, "disable"))
528 return UBUS_STATUS_INVALID_ARGUMENT
;
530 snprintf(path
, sizeof(path
) - 1, "/etc/init.d/%s",
531 (char *)blobmsg_data(tb
[RPC_I_NAME
]));
534 return rpc_errno_status();
536 if (!(s
.st_mode
& S_IXUSR
))
537 return UBUS_STATUS_PERMISSION_DENIED
;
539 switch ((pid
= fork()))
542 return rpc_errno_status();
547 if ((fd
= open("/dev/null", O_RDWR
)) > -1)
558 if (execl(path
, path
, action
, NULL
))
559 return rpc_errno_status();
567 rpc_luci2_rclocal_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
568 struct ubus_request_data
*req
, const char *method
,
569 struct blob_attr
*msg
)
572 char data
[4096] = { 0 };
574 if (!(f
= fopen("/etc/rc.local", "r")))
575 return rpc_errno_status();
577 fread(data
, sizeof(data
) - 1, 1, f
);
580 blob_buf_init(&buf
, 0);
581 blobmsg_add_string(&buf
, "data", data
);
583 ubus_send_reply(ctx
, req
, buf
.head
);
588 rpc_luci2_rclocal_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
589 struct ubus_request_data
*req
, const char *method
,
590 struct blob_attr
*msg
)
593 struct blob_attr
*tb
[__RPC_D_MAX
];
595 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
596 blob_data(msg
), blob_len(msg
));
598 if (!tb
[RPC_D_DATA
] || blobmsg_data_len(tb
[RPC_D_DATA
]) >= 4096)
599 return UBUS_STATUS_INVALID_ARGUMENT
;
601 if (!(f
= fopen("/etc/rc.local", "w")))
602 return rpc_errno_status();
604 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
605 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
612 rpc_luci2_crontab_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
613 struct ubus_request_data
*req
, const char *method
,
614 struct blob_attr
*msg
)
617 char data
[4096] = { 0 };
619 if (!(f
= fopen("/etc/crontabs/root", "r")))
620 return rpc_errno_status();
622 fread(data
, sizeof(data
) - 1, 1, f
);
625 blob_buf_init(&buf
, 0);
626 blobmsg_add_string(&buf
, "data", data
);
628 ubus_send_reply(ctx
, req
, buf
.head
);
633 rpc_luci2_crontab_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
634 struct ubus_request_data
*req
, const char *method
,
635 struct blob_attr
*msg
)
639 struct blob_attr
*tb
[__RPC_D_MAX
];
641 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
642 blob_data(msg
), blob_len(msg
));
644 if (!tb
[RPC_D_DATA
] || blobmsg_data_len(tb
[RPC_D_DATA
]) >= 4096)
645 return UBUS_STATUS_INVALID_ARGUMENT
;
647 if (stat("/etc/crontabs", &s
) && mkdir("/etc/crontabs", 0755))
648 return rpc_errno_status();
650 if (!(f
= fopen("/etc/crontabs/root", "w")))
651 return rpc_errno_status();
653 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
654 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
661 rpc_luci2_sshkeys_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
662 struct ubus_request_data
*req
, const char *method
,
663 struct blob_attr
*msg
)
669 if (!(f
= fopen("/etc/dropbear/authorized_keys", "r")))
670 return rpc_errno_status();
672 blob_buf_init(&buf
, 0);
673 c
= blobmsg_open_array(&buf
, "keys");
675 while (fgets(line
, sizeof(line
) - 1, f
))
677 for (p
= line
+ strlen(line
) - 1; (p
> line
) && isspace(*p
); p
--)
680 for (p
= line
; isspace(*p
); p
++)
684 blobmsg_add_string(&buf
, NULL
, p
);
687 blobmsg_close_array(&buf
, c
);
690 ubus_send_reply(ctx
, req
, buf
.head
);
695 rpc_luci2_sshkeys_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
696 struct ubus_request_data
*req
, const char *method
,
697 struct blob_attr
*msg
)
701 struct blob_attr
*cur
, *tb
[__RPC_K_MAX
];
703 blobmsg_parse(rpc_sshkey_policy
, __RPC_K_MAX
, tb
,
704 blob_data(msg
), blob_len(msg
));
707 return UBUS_STATUS_INVALID_ARGUMENT
;
709 if (!(f
= fopen("/etc/dropbear/authorized_keys", "w")))
710 return rpc_errno_status();
712 blobmsg_for_each_attr(cur
, tb
[RPC_K_KEYS
], rem
)
714 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
717 fwrite(blobmsg_data(cur
), blobmsg_data_len(cur
) - 1, 1, f
);
718 fwrite("\n", 1, 1, f
);
726 rpc_luci2_password_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
727 struct ubus_request_data
*req
, const char *method
,
728 struct blob_attr
*msg
)
733 struct blob_attr
*tb
[__RPC_P_MAX
];
735 blobmsg_parse(rpc_password_policy
, __RPC_P_MAX
, tb
,
736 blob_data(msg
), blob_len(msg
));
738 if (!tb
[RPC_P_USER
] || !tb
[RPC_P_PASSWORD
])
739 return UBUS_STATUS_INVALID_ARGUMENT
;
741 if (stat("/usr/bin/passwd", &s
))
742 return UBUS_STATUS_NOT_FOUND
;
744 if (!(s
.st_mode
& S_IXUSR
))
745 return UBUS_STATUS_PERMISSION_DENIED
;
748 return rpc_errno_status();
750 switch ((pid
= fork()))
755 return rpc_errno_status();
764 if ((fd
= open("/dev/null", O_RDWR
)) > -1)
773 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
774 blobmsg_data(tb
[RPC_P_USER
]), NULL
))
775 return rpc_errno_status();
780 write(fds
[1], blobmsg_data(tb
[RPC_P_PASSWORD
]),
781 blobmsg_data_len(tb
[RPC_P_PASSWORD
]) - 1);
782 write(fds
[1], "\n", 1);
786 write(fds
[1], blobmsg_data(tb
[RPC_P_PASSWORD
]),
787 blobmsg_data_len(tb
[RPC_P_PASSWORD
]) - 1);
788 write(fds
[1], "\n", 1);
792 waitpid(pid
, NULL
, 0);
799 rpc_luci2_led_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
800 struct ubus_request_data
*req
, const char *method
,
801 struct blob_attr
*msg
)
805 void *list
, *led
, *trigger
;
806 char *p
, *active_trigger
, line
[512];
809 if (!(d
= opendir("/sys/class/leds")))
810 return rpc_errno_status();
812 blob_buf_init(&buf
, 0);
813 list
= blobmsg_open_array(&buf
, "leds");
815 while ((e
= readdir(d
)) != NULL
)
817 snprintf(line
, sizeof(line
) - 1, "/sys/class/leds/%s/trigger",
820 if (!(f
= fopen(line
, "r")))
823 led
= blobmsg_open_table(&buf
, NULL
);
825 blobmsg_add_string(&buf
, "name", e
->d_name
);
827 if (fgets(line
, sizeof(line
) - 1, f
))
829 trigger
= blobmsg_open_array(&buf
, "triggers");
831 for (p
= strtok(line
, " \n"), active_trigger
= NULL
;
833 p
= strtok(NULL
, " \n"))
837 *(p
+ strlen(p
) - 1) = 0;
842 blobmsg_add_string(&buf
, NULL
, p
);
845 blobmsg_close_array(&buf
, trigger
);
848 blobmsg_add_string(&buf
, "active_trigger", active_trigger
);
853 snprintf(line
, sizeof(line
) - 1, "/sys/class/leds/%s/brightness",
856 if ((f
= fopen(line
, "r")) != NULL
)
858 if (fgets(line
, sizeof(line
) - 1, f
))
859 blobmsg_add_u32(&buf
, "brightness", atoi(line
));
864 snprintf(line
, sizeof(line
) - 1, "/sys/class/leds/%s/max_brightness",
867 if ((f
= fopen(line
, "r")) != NULL
)
869 if (fgets(line
, sizeof(line
) - 1, f
))
870 blobmsg_add_u32(&buf
, "max_brightness", atoi(line
));
875 blobmsg_close_table(&buf
, led
);
880 blobmsg_close_array(&buf
, list
);
881 ubus_send_reply(ctx
, req
, buf
.head
);
887 rpc_luci2_usb_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
888 struct ubus_request_data
*req
, const char *method
,
889 struct blob_attr
*msg
)
899 const char *attributes
[] = {
900 "manufacturer", "vendor_name", "s",
901 "product", "product_name", "s",
902 "idVendor", "vendor_id", "x",
903 "idProduct", "product_id", "x",
904 "serial", "serial", "s",
905 "speed", "speed", "d",
908 if (!(d
= opendir("/sys/bus/usb/devices")))
909 return rpc_errno_status();
911 blob_buf_init(&buf
, 0);
912 list
= blobmsg_open_array(&buf
, "devices");
914 while ((e
= readdir(d
)) != NULL
)
916 if (e
->d_name
[0] < '0' || e
->d_name
[0] > '9')
919 snprintf(line
, sizeof(line
) - 1,
920 "/sys/bus/usb/devices/%s/%s", e
->d_name
, attributes
[0]);
925 device
= blobmsg_open_table(&buf
, NULL
);
927 blobmsg_add_string(&buf
, "name", e
->d_name
);
929 for (i
= 0; i
< sizeof(attributes
) / sizeof(attributes
[0]); i
+= 3)
931 snprintf(line
, sizeof(line
) - 1,
932 "/sys/bus/usb/devices/%s/%s", e
->d_name
, attributes
[i
]);
934 if (!(f
= fopen(line
, "r")))
937 if (fgets(line
, sizeof(line
) - 1, f
))
939 switch (*attributes
[i
+2])
942 blobmsg_add_u32(&buf
, attributes
[i
+1],
943 strtoul(line
, NULL
, 16));
947 blobmsg_add_u32(&buf
, attributes
[i
+1],
948 strtoul(line
, NULL
, 10));
952 if ((p
= strchr(line
, '\n')) != NULL
)
953 while (p
> line
&& isspace(*p
))
956 blobmsg_add_string(&buf
, attributes
[i
+1], line
);
964 blobmsg_close_table(&buf
, device
);
969 blobmsg_close_array(&buf
, list
);
970 ubus_send_reply(ctx
, req
, buf
.head
);
976 rpc_luci2_upgrade_test(struct ubus_context
*ctx
, struct ubus_object
*obj
,
977 struct ubus_request_data
*req
, const char *method
,
978 struct blob_attr
*msg
)
980 const char *cmd
[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL
};
981 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
985 rpc_luci2_upgrade_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
986 struct ubus_request_data
*req
, const char *method
,
987 struct blob_attr
*msg
)
993 rpc_luci2_upgrade_clean(struct ubus_context
*ctx
, struct ubus_object
*obj
,
994 struct ubus_request_data
*req
, const char *method
,
995 struct blob_attr
*msg
)
997 if (unlink("/tmp/firmware.bin"))
998 return rpc_errno_status();
1004 rpc_luci2_backup_restore(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1005 struct ubus_request_data
*req
, const char *method
,
1006 struct blob_attr
*msg
)
1008 const char *cmd
[4] = { "sysupgrade", "--restore-backup",
1009 "/tmp/backup.tar.gz", NULL
};
1011 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1015 rpc_luci2_backup_clean(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1016 struct ubus_request_data
*req
, const char *method
,
1017 struct blob_attr
*msg
)
1019 if (unlink("/tmp/backup.tar.gz"))
1020 return rpc_errno_status();
1026 rpc_luci2_backup_config_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1027 struct ubus_request_data
*req
, const char *method
,
1028 struct blob_attr
*msg
)
1031 char conf
[2048] = { 0 };
1033 if (!(f
= fopen("/etc/sysupgrade.conf", "r")))
1034 return rpc_errno_status();
1036 fread(conf
, sizeof(conf
) - 1, 1, f
);
1039 blob_buf_init(&buf
, 0);
1040 blobmsg_add_string(&buf
, "config", conf
);
1042 ubus_send_reply(ctx
, req
, buf
.head
);
1047 rpc_luci2_backup_config_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1048 struct ubus_request_data
*req
, const char *method
,
1049 struct blob_attr
*msg
)
1052 struct blob_attr
*tb
[__RPC_D_MAX
];
1054 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
1055 blob_data(msg
), blob_len(msg
));
1057 if (!tb
[RPC_D_DATA
])
1058 return UBUS_STATUS_INVALID_ARGUMENT
;
1060 if (blobmsg_data_len(tb
[RPC_D_DATA
]) >= 2048)
1061 return UBUS_STATUS_NOT_SUPPORTED
;
1063 if (!(f
= fopen("/etc/sysupgrade.conf", "w")))
1064 return rpc_errno_status();
1066 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
1067 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
1073 struct backup_state
{
1079 backup_parse_list(struct blob_buf
*blob
, char *buf
, int len
, void *priv
)
1081 struct backup_state
*s
= priv
;
1082 char *nl
= strchr(buf
, '\n');
1090 s
->array
= blobmsg_open_array(blob
, "files");
1094 blobmsg_add_string(blob
, NULL
, buf
);
1096 return (nl
- buf
+ 1);
1100 backup_finish_list(struct blob_buf
*blob
, int status
, void *priv
)
1102 struct backup_state
*s
= priv
;
1105 return UBUS_STATUS_NO_DATA
;
1107 blobmsg_close_array(blob
, s
->array
);
1109 return UBUS_STATUS_OK
;
1113 rpc_luci2_backup_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1114 struct ubus_request_data
*req
, const char *method
,
1115 struct blob_attr
*msg
)
1117 struct backup_state
*state
= NULL
;
1118 const char *cmd
[3] = { "sysupgrade", "--list-backup", NULL
};
1120 state
= malloc(sizeof(*state
));
1123 return UBUS_STATUS_UNKNOWN_ERROR
;
1125 memset(state
, 0, sizeof(*state
));
1127 return ops
->exec(cmd
, NULL
, backup_parse_list
, NULL
, backup_finish_list
,
1132 rpc_luci2_reset_test(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1133 struct ubus_request_data
*req
, const char *method
,
1134 struct blob_attr
*msg
)
1138 char line
[64] = { 0 };
1139 bool supported
= false;
1141 if (!stat("/sbin/mtd", &s
) && (s
.st_mode
& S_IXUSR
))
1143 if ((mtd
= fopen("/proc/mtd", "r")) != NULL
)
1145 while (fgets(line
, sizeof(line
) - 1, mtd
))
1147 if (strstr(line
, "\"rootfs_data\""))
1158 blob_buf_init(&buf
, 0);
1159 blobmsg_add_u8(&buf
, "supported", supported
);
1161 ubus_send_reply(ctx
, req
, buf
.head
);
1167 rpc_luci2_reset_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1168 struct ubus_request_data
*req
, const char *method
,
1169 struct blob_attr
*msg
)
1174 return rpc_errno_status();
1187 execl("/sbin/mtd", "/sbin/mtd", "-r", "erase", "rootfs_data", NULL
);
1189 return rpc_errno_status();
1197 rpc_luci2_reboot(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1198 struct ubus_request_data
*req
, const char *method
,
1199 struct blob_attr
*msg
)
1204 return rpc_errno_status();
1215 execl("/sbin/reboot", "/sbin/reboot", NULL
);
1217 return rpc_errno_status();
1226 dnsmasq_leasefile(void)
1228 FILE *leases
= NULL
;
1229 struct uci_package
*p
;
1230 struct uci_element
*e
;
1231 struct uci_section
*s
;
1232 struct uci_ptr ptr
= {
1235 .option
= "leasefile"
1238 uci_load(cursor
, ptr
.package
, &p
);
1243 uci_foreach_element(&p
->sections
, e
)
1245 s
= uci_to_section(e
);
1247 if (strcmp(s
->type
, "dnsmasq"))
1250 ptr
.section
= e
->name
;
1251 uci_lookup_ptr(cursor
, &ptr
, NULL
, true);
1255 if (ptr
.o
&& ptr
.o
->type
== UCI_TYPE_STRING
)
1256 leases
= fopen(ptr
.o
->v
.string
, "r");
1258 uci_unload(cursor
, p
);
1264 rpc_luci2_network_leases(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1265 struct ubus_request_data
*req
, const char *method
,
1266 struct blob_attr
*msg
)
1271 char *ts
, *mac
, *addr
, *name
;
1272 time_t now
= time(NULL
);
1274 blob_buf_init(&buf
, 0);
1275 c
= blobmsg_open_array(&buf
, "leases");
1277 leases
= dnsmasq_leasefile();
1282 while (fgets(line
, sizeof(line
) - 1, leases
))
1284 ts
= strtok(line
, " \t");
1285 mac
= strtok(NULL
, " \t");
1286 addr
= strtok(NULL
, " \t");
1287 name
= strtok(NULL
, " \t");
1289 if (!ts
|| !mac
|| !addr
|| !name
)
1292 if (strchr(addr
, ':'))
1295 d
= blobmsg_open_table(&buf
, NULL
);
1297 blobmsg_add_u32(&buf
, "expires", atoi(ts
) - now
);
1298 blobmsg_add_string(&buf
, "macaddr", mac
);
1299 blobmsg_add_string(&buf
, "ipaddr", addr
);
1301 if (strcmp(name
, "*"))
1302 blobmsg_add_string(&buf
, "hostname", name
);
1304 blobmsg_close_table(&buf
, d
);
1310 blobmsg_close_array(&buf
, c
);
1311 ubus_send_reply(ctx
, req
, buf
.head
);
1317 rpc_luci2_network_leases6(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1318 struct ubus_request_data
*req
, const char *method
,
1319 struct blob_attr
*msg
)
1324 char *ts
, *mac
, *addr
, *name
, *duid
;
1325 time_t now
= time(NULL
);
1327 blob_buf_init(&buf
, 0);
1328 c
= blobmsg_open_array(&buf
, "leases");
1330 leases
= fopen("/tmp/hosts/6relayd", "r");
1334 while (fgets(line
, sizeof(line
) - 1, leases
))
1336 if (strncmp(line
, "# ", 2))
1339 strtok(line
+ 2, " \t"); /* iface */
1341 duid
= strtok(NULL
, " \t");
1343 strtok(NULL
, " \t"); /* iaid */
1345 name
= strtok(NULL
, " \t");
1346 ts
= strtok(NULL
, " \t");
1348 strtok(NULL
, " \t"); /* id */
1349 strtok(NULL
, " \t"); /* length */
1351 addr
= strtok(NULL
, " \t\n");
1356 d
= blobmsg_open_table(&buf
, NULL
);
1358 blobmsg_add_u32(&buf
, "expires", atoi(ts
) - now
);
1359 blobmsg_add_string(&buf
, "duid", duid
);
1360 blobmsg_add_string(&buf
, "ip6addr", addr
);
1362 if (strcmp(name
, "-"))
1363 blobmsg_add_string(&buf
, "hostname", name
);
1365 blobmsg_close_array(&buf
, d
);
1372 leases
= dnsmasq_leasefile();
1377 while (fgets(line
, sizeof(line
) - 1, leases
))
1379 ts
= strtok(line
, " \t");
1380 mac
= strtok(NULL
, " \t");
1381 addr
= strtok(NULL
, " \t");
1382 name
= strtok(NULL
, " \t");
1383 duid
= strtok(NULL
, " \t\n");
1385 if (!ts
|| !mac
|| !addr
|| !duid
)
1388 if (!strchr(addr
, ':'))
1391 d
= blobmsg_open_table(&buf
, NULL
);
1393 blobmsg_add_u32(&buf
, "expires", atoi(ts
) - now
);
1394 blobmsg_add_string(&buf
, "macaddr", mac
);
1395 blobmsg_add_string(&buf
, "ip6addr", addr
);
1397 if (strcmp(name
, "*"))
1398 blobmsg_add_string(&buf
, "hostname", name
);
1400 if (strcmp(duid
, "*"))
1401 blobmsg_add_string(&buf
, "duid", name
);
1403 blobmsg_close_table(&buf
, d
);
1410 blobmsg_close_array(&buf
, c
);
1411 ubus_send_reply(ctx
, req
, buf
.head
);
1417 rpc_luci2_network_ct_count(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1418 struct ubus_request_data
*req
, const char *method
,
1419 struct blob_attr
*msg
)
1424 blob_buf_init(&buf
, 0);
1426 if ((f
= fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL
)
1428 if (fgets(line
, sizeof(line
) - 1, f
))
1429 blobmsg_add_u32(&buf
, "count", atoi(line
));
1434 if ((f
= fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL
)
1436 if (fgets(line
, sizeof(line
) - 1, f
))
1437 blobmsg_add_u32(&buf
, "limit", atoi(line
));
1442 ubus_send_reply(ctx
, req
, buf
.head
);
1448 rpc_luci2_network_ct_table(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1449 struct ubus_request_data
*req
, const char *method
,
1450 struct blob_attr
*msg
)
1458 blob_buf_init(&buf
, 0);
1459 c
= blobmsg_open_array(&buf
, "entries");
1461 if ((f
= fopen("/proc/net/nf_conntrack", "r")) != NULL
)
1463 while (fgets(line
, sizeof(line
) - 1, f
))
1465 d
= blobmsg_open_table(&buf
, NULL
);
1466 memset(seen
, 0, sizeof(seen
));
1468 for (i
= 0, p
= strtok(line
, " "); p
; i
++, p
= strtok(NULL
, " "))
1471 blobmsg_add_u8(&buf
, "ipv6", !strcmp(p
, "ipv6"));
1473 blobmsg_add_u32(&buf
, "protocol", atoi(p
));
1475 blobmsg_add_u32(&buf
, "expires", atoi(p
));
1481 if (!seen
[0] && !strncmp(p
, "src=", 4))
1483 blobmsg_add_string(&buf
, "src", p
+ 4);
1486 else if (!seen
[1] && !strncmp(p
, "dst=", 4))
1488 blobmsg_add_string(&buf
, "dest", p
+ 4);
1491 else if (!seen
[2] && !strncmp(p
, "sport=", 6))
1493 blobmsg_add_u32(&buf
, "sport", atoi(p
+ 6));
1496 else if (!seen
[3] && !strncmp(p
, "dport=", 6))
1498 blobmsg_add_u32(&buf
, "dport", atoi(p
+ 6));
1501 else if (!strncmp(p
, "packets=", 8))
1503 blobmsg_add_u32(&buf
,
1504 seen
[4] ? "tx_packets" : "rx_packets",
1508 else if (!strncmp(p
, "bytes=", 6))
1510 blobmsg_add_u32(&buf
,
1511 seen
[5] ? "tx_bytes" : "rx_bytes",
1518 blobmsg_close_table(&buf
, d
);
1524 blobmsg_close_array(&buf
, c
);
1525 ubus_send_reply(ctx
, req
, buf
.head
);
1531 rpc_luci2_network_arp_table(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1532 struct ubus_request_data
*req
, const char *method
,
1533 struct blob_attr
*msg
)
1537 char *addr
, *mac
, *dev
, line
[128];
1539 blob_buf_init(&buf
, 0);
1540 c
= blobmsg_open_array(&buf
, "entries");
1542 if ((f
= fopen("/proc/net/arp", "r")) != NULL
)
1544 /* skip header line */
1545 fgets(line
, sizeof(line
) - 1, f
);
1547 while (fgets(line
, sizeof(line
) - 1, f
))
1549 addr
= strtok(line
, " \t");
1551 strtok(NULL
, " \t"); /* HW type */
1552 strtok(NULL
, " \t"); /* Flags */
1554 mac
= strtok(NULL
, " \t");
1556 strtok(NULL
, " \t"); /* Mask */
1558 dev
= strtok(NULL
, " \t\n");
1563 d
= blobmsg_open_table(&buf
, NULL
);
1564 blobmsg_add_string(&buf
, "ipaddr", addr
);
1565 blobmsg_add_string(&buf
, "macaddr", mac
);
1566 blobmsg_add_string(&buf
, "device", dev
);
1567 blobmsg_close_table(&buf
, d
);
1573 blobmsg_close_array(&buf
, c
);
1574 ubus_send_reply(ctx
, req
, buf
.head
);
1580 put_hexaddr(const char *name
, const char *s
, const char *m
)
1584 char as
[sizeof("255.255.255.255/32\0")];
1586 a
.s_addr
= strtoul(s
, NULL
, 16);
1587 inet_ntop(AF_INET
, &a
, as
, sizeof(as
));
1591 for (a
.s_addr
= ntohl(strtoul(m
, NULL
, 16)), bits
= 0;
1592 a
.s_addr
& 0x80000000;
1596 sprintf(as
+ strlen(as
), "/%u", bits
);
1599 blobmsg_add_string(&buf
, name
, as
);
1603 rpc_luci2_network_routes(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1604 struct ubus_request_data
*req
, const char *method
,
1605 struct blob_attr
*msg
)
1609 char *dst
, *dmask
, *next
, *metric
, *device
;
1613 if (!(routes
= fopen("/proc/net/route", "r")))
1614 return rpc_errno_status();
1616 blob_buf_init(&buf
, 0);
1617 c
= blobmsg_open_array(&buf
, "routes");
1619 /* skip header line */
1620 fgets(line
, sizeof(line
) - 1, routes
);
1622 while (fgets(line
, sizeof(line
) - 1, routes
))
1624 device
= strtok(line
, "\t ");
1625 dst
= strtok(NULL
, "\t ");
1626 next
= strtok(NULL
, "\t ");
1628 strtok(NULL
, "\t "); /* flags */
1629 strtok(NULL
, "\t "); /* refcount */
1630 strtok(NULL
, "\t "); /* usecount */
1632 metric
= strtok(NULL
, "\t ");
1633 dmask
= strtok(NULL
, "\t ");
1638 d
= blobmsg_open_table(&buf
, NULL
);
1640 put_hexaddr("target", dst
, dmask
);
1641 put_hexaddr("nexthop", next
, NULL
);
1643 n
= strtoul(metric
, NULL
, 10);
1644 blobmsg_add_u32(&buf
, "metric", n
);
1646 blobmsg_add_string(&buf
, "device", device
);
1648 blobmsg_close_table(&buf
, d
);
1651 blobmsg_close_array(&buf
, c
);
1654 ubus_send_reply(ctx
, req
, buf
.head
);
1659 put_hex6addr(const char *name
, const char *s
, const char *m
)
1663 char as
[INET6_ADDRSTRLEN
+ sizeof("/128")];
1666 (((x) <= '9') ? ((x) - '0') : \
1667 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1670 for (i
= 0; i
< 16; i
++, s
+= 2)
1671 a
.s6_addr
[i
] = (16 * hex(*s
)) + hex(*(s
+1));
1673 inet_ntop(AF_INET6
, &a
, as
, sizeof(as
));
1676 sprintf(as
+ strlen(as
), "/%lu", strtoul(m
, NULL
, 16));
1678 blobmsg_add_string(&buf
, name
, as
);
1682 rpc_luci2_network_routes6(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1683 struct ubus_request_data
*req
, const char *method
,
1684 struct blob_attr
*msg
)
1688 char *src
, *smask
, *dst
, *dmask
, *next
, *metric
, *flags
, *device
;
1692 if (!(routes
= fopen("/proc/net/ipv6_route", "r")))
1693 return rpc_errno_status();
1695 blob_buf_init(&buf
, 0);
1696 c
= blobmsg_open_array(&buf
, "routes");
1698 while (fgets(line
, sizeof(line
) - 1, routes
))
1700 dst
= strtok(line
, " ");
1701 dmask
= strtok(NULL
, " ");
1702 src
= strtok(NULL
, " ");
1703 smask
= strtok(NULL
, " ");
1704 next
= strtok(NULL
, " ");
1705 metric
= strtok(NULL
, " ");
1707 strtok(NULL
, " "); /* refcount */
1708 strtok(NULL
, " "); /* usecount */
1710 flags
= strtok(NULL
, " ");
1711 device
= strtok(NULL
, " \n");
1716 n
= strtoul(flags
, NULL
, 16);
1721 d
= blobmsg_open_table(&buf
, NULL
);
1723 put_hex6addr("target", dst
, dmask
);
1724 put_hex6addr("source", src
, smask
);
1725 put_hex6addr("nexthop", next
, NULL
);
1727 n
= strtoul(metric
, NULL
, 16);
1728 blobmsg_add_u32(&buf
, "metric", n
);
1730 blobmsg_add_string(&buf
, "device", device
);
1732 blobmsg_close_table(&buf
, d
);
1735 blobmsg_close_array(&buf
, c
);
1738 ubus_send_reply(ctx
, req
, buf
.head
);
1754 opkg_parse_list(struct blob_buf
*blob
, char *buf
, int len
, void *priv
)
1756 struct opkg_state
*s
= priv
;
1759 char *nl
= strchr(buf
, '\n');
1760 char *name
= NULL
, *vers
= NULL
, *desc
= NULL
;
1768 if (s
->cur_offset
++ < s
->req_offset
)
1771 if (s
->cur_count
++ >= s
->req_count
)
1777 s
->array
= blobmsg_open_array(blob
, "packages");
1780 for (ptr
= buf
, last
= buf
, *nl
= 0; ptr
<= nl
; ptr
++)
1782 if (!*ptr
|| (*ptr
== ' ' && *(ptr
+1) == '-' && *(ptr
+2) == ' '))
1794 desc
= *ptr
? (ptr
+ 3) : NULL
;
1803 c
= blobmsg_open_array(blob
, NULL
);
1805 blobmsg_add_string(blob
, NULL
, name
);
1806 blobmsg_add_string(blob
, NULL
, vers
);
1809 blobmsg_add_string(blob
, NULL
, desc
);
1811 blobmsg_close_array(blob
, c
);
1815 return (nl
- buf
+ 1);
1819 opkg_finish_list(struct blob_buf
*blob
, int status
, void *priv
)
1821 struct opkg_state
*s
= priv
;
1824 return UBUS_STATUS_NO_DATA
;
1826 blobmsg_close_array(blob
, s
->array
);
1827 blobmsg_add_u32(blob
, "total", s
->total
);
1829 return UBUS_STATUS_OK
;
1833 opkg_exec_list(const char *action
, struct blob_attr
*msg
,
1834 struct ubus_context
*ctx
, struct ubus_request_data
*req
)
1836 struct opkg_state
*state
= NULL
;
1837 struct blob_attr
*tb
[__RPC_OM_MAX
];
1838 const char *cmd
[5] = { "opkg", action
, "-nocase", NULL
, NULL
};
1840 blobmsg_parse(rpc_opkg_match_policy
, __RPC_OM_MAX
, tb
,
1841 blob_data(msg
), blob_len(msg
));
1843 state
= malloc(sizeof(*state
));
1846 return UBUS_STATUS_UNKNOWN_ERROR
;
1848 memset(state
, 0, sizeof(*state
));
1850 if (tb
[RPC_OM_PATTERN
])
1851 cmd
[3] = blobmsg_data(tb
[RPC_OM_PATTERN
]);
1853 if (tb
[RPC_OM_LIMIT
])
1854 state
->req_count
= blobmsg_get_u32(tb
[RPC_OM_LIMIT
]);
1856 if (tb
[RPC_OM_OFFSET
])
1857 state
->req_offset
= blobmsg_get_u32(tb
[RPC_OM_OFFSET
]);
1859 if (state
->req_offset
< 0)
1860 state
->req_offset
= 0;
1862 if (state
->req_count
<= 0 || state
->req_count
> 100)
1863 state
->req_count
= 100;
1865 return ops
->exec(cmd
, NULL
, opkg_parse_list
, NULL
, opkg_finish_list
,
1871 rpc_luci2_opkg_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1872 struct ubus_request_data
*req
, const char *method
,
1873 struct blob_attr
*msg
)
1875 return opkg_exec_list("list", msg
, ctx
, req
);
1879 rpc_luci2_opkg_list_installed(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1880 struct ubus_request_data
*req
, const char *method
,
1881 struct blob_attr
*msg
)
1883 return opkg_exec_list("list-installed", msg
, ctx
, req
);
1887 rpc_luci2_opkg_find(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1888 struct ubus_request_data
*req
, const char *method
,
1889 struct blob_attr
*msg
)
1891 return opkg_exec_list("find", msg
, ctx
, req
);
1895 rpc_luci2_opkg_update(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1896 struct ubus_request_data
*req
, const char *method
,
1897 struct blob_attr
*msg
)
1899 const char *cmd
[3] = { "opkg", "update", NULL
};
1900 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1904 rpc_luci2_opkg_install(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1905 struct ubus_request_data
*req
, const char *method
,
1906 struct blob_attr
*msg
)
1908 struct blob_attr
*tb
[__RPC_OP_MAX
];
1909 const char *cmd
[5] = { "opkg", "--force-overwrite",
1910 "install", NULL
, NULL
};
1912 blobmsg_parse(rpc_opkg_package_policy
, __RPC_OP_MAX
, tb
,
1913 blob_data(msg
), blob_len(msg
));
1915 if (!tb
[RPC_OP_PACKAGE
])
1916 return UBUS_STATUS_INVALID_ARGUMENT
;
1918 cmd
[3] = blobmsg_data(tb
[RPC_OP_PACKAGE
]);
1920 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1924 rpc_luci2_opkg_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1925 struct ubus_request_data
*req
, const char *method
,
1926 struct blob_attr
*msg
)
1928 struct blob_attr
*tb
[__RPC_OP_MAX
];
1929 const char *cmd
[5] = { "opkg", "--force-removal-of-dependent-packages",
1930 "remove", NULL
, NULL
};
1932 blobmsg_parse(rpc_opkg_package_policy
, __RPC_OP_MAX
, tb
,
1933 blob_data(msg
), blob_len(msg
));
1935 if (!tb
[RPC_OP_PACKAGE
])
1936 return UBUS_STATUS_INVALID_ARGUMENT
;
1938 cmd
[3] = blobmsg_data(tb
[RPC_OP_PACKAGE
]);
1940 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
1944 rpc_luci2_opkg_config_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1945 struct ubus_request_data
*req
, const char *method
,
1946 struct blob_attr
*msg
)
1949 char conf
[2048] = { 0 };
1951 if (!(f
= fopen("/etc/opkg.conf", "r")))
1952 return rpc_errno_status();
1954 fread(conf
, sizeof(conf
) - 1, 1, f
);
1957 blob_buf_init(&buf
, 0);
1958 blobmsg_add_string(&buf
, "config", conf
);
1960 ubus_send_reply(ctx
, req
, buf
.head
);
1965 rpc_luci2_opkg_config_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1966 struct ubus_request_data
*req
, const char *method
,
1967 struct blob_attr
*msg
)
1970 struct blob_attr
*tb
[__RPC_D_MAX
];
1972 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
1973 blob_data(msg
), blob_len(msg
));
1975 if (!tb
[RPC_D_DATA
])
1976 return UBUS_STATUS_INVALID_ARGUMENT
;
1978 if (blobmsg_data_len(tb
[RPC_D_DATA
]) >= 2048)
1979 return UBUS_STATUS_NOT_SUPPORTED
;
1981 if (!(f
= fopen("/etc/opkg.conf", "w")))
1982 return rpc_errno_status();
1984 fwrite(blobmsg_data(tb
[RPC_D_DATA
]),
1985 blobmsg_data_len(tb
[RPC_D_DATA
]) - 1, 1, f
);
1993 menu_access(struct blob_attr
*sid
, struct blob_attr
*acls
, struct blob_buf
*e
)
1996 struct blob_attr
*acl
;
2000 c
= blobmsg_open_table(e
, "write");
2002 blobmsg_for_each_attr(acl
, acls
, rem
)
2004 if (!ops
->session_access(blobmsg_data(sid
), "access-group",
2005 blobmsg_data(acl
), "read"))
2011 blobmsg_add_u8(e
, blobmsg_data(acl
),
2012 ops
->session_access(blobmsg_data(sid
), "access-group",
2013 blobmsg_data(acl
), "write"));
2016 blobmsg_close_table(e
, c
);
2022 rpc_luci2_ui_menu(struct ubus_context
*ctx
, struct ubus_object
*obj
,
2023 struct ubus_request_data
*req
, const char *method
,
2024 struct blob_attr
*msg
)
2028 struct blob_buf menu
= { 0 };
2029 struct blob_buf item
= { 0 };
2030 struct blob_attr
*entry
, *attr
;
2031 struct blob_attr
*tb
[__RPC_MENU_MAX
];
2035 blobmsg_parse(rpc_menu_policy
, __RPC_MENU_MAX
, tb
,
2036 blob_data(msg
), blob_len(msg
));
2038 if (!tb
[RPC_MENU_SESSION
])
2039 return UBUS_STATUS_INVALID_ARGUMENT
;
2042 blob_buf_init(&buf
, 0);
2043 c
= blobmsg_open_table(&buf
, "menu");
2045 if (!glob(RPC_LUCI2_MENU_FILES
, 0, NULL
, &gl
))
2047 for (i
= 0; i
< gl
.gl_pathc
; i
++)
2049 blob_buf_init(&menu
, 0);
2051 if (!blobmsg_add_json_from_file(&menu
, gl
.gl_pathv
[i
]))
2054 blob_for_each_attr(entry
, menu
.head
, rem
)
2058 blob_buf_init(&item
, 0);
2059 d
= blobmsg_open_table(&item
, blobmsg_name(entry
));
2061 blobmsg_for_each_attr(attr
, entry
, rem2
)
2063 if (blob_id(attr
) == BLOBMSG_TYPE_ARRAY
&&
2064 !strcmp(blobmsg_name(attr
), "acls"))
2065 access
= menu_access(tb
[RPC_MENU_SESSION
], attr
, &item
);
2067 blobmsg_add_blob(&item
, attr
);
2070 blobmsg_close_table(&item
, d
);
2073 blob_for_each_attr(attr
, item
.head
, rem2
)
2074 blobmsg_add_blob(&buf
, attr
);
2076 blob_buf_free(&item
);
2080 blob_buf_free(&menu
);
2086 blobmsg_close_table(&buf
, c
);
2088 ubus_send_reply(ctx
, req
, buf
.head
);
2094 parse_acl_file(struct blob_buf
*acls
, const char *path
)
2096 struct blob_buf acl
= { 0 };
2097 struct blob_attr
*cur
;
2101 blob_buf_init(&acl
, 0);
2103 if (blobmsg_add_json_from_file(&acl
, path
))
2105 c
= blobmsg_open_table(acls
, NULL
);
2107 blob_for_each_attr(cur
, acl
.head
, rem
)
2108 blobmsg_add_blob(acls
, cur
);
2110 blobmsg_close_table(acls
, c
);
2113 blob_buf_free(&acl
);
2117 rpc_luci2_ui_acls(struct ubus_context
*ctx
, struct ubus_object
*obj
,
2118 struct ubus_request_data
*req
, const char *method
,
2119 struct blob_attr
*msg
)
2125 if (glob(RPC_SESSION_ACL_DIR
"/*.json", 0, NULL
, &gl
))
2126 return rpc_errno_status();
2128 blob_buf_init(&buf
, 0);
2129 c
= blobmsg_open_array(&buf
, "acls");
2131 for (i
= 0; i
< gl
.gl_pathc
; i
++)
2132 parse_acl_file(&buf
, gl
.gl_pathv
[i
]);
2135 blobmsg_close_array(&buf
, c
);
2137 ubus_send_reply(ctx
, req
, buf
.head
);
2142 rpc_luci2_ui_crypt(struct ubus_context
*ctx
, struct ubus_object
*obj
,
2143 struct ubus_request_data
*req
, const char *method
,
2144 struct blob_attr
*msg
)
2147 struct blob_attr
*tb
[__RPC_D_MAX
];
2149 blobmsg_parse(rpc_data_policy
, __RPC_D_MAX
, tb
,
2150 blob_data(msg
), blob_len(msg
));
2152 if (!tb
[RPC_D_DATA
] || blobmsg_data_len(tb
[RPC_D_DATA
]) >= 128)
2153 return UBUS_STATUS_INVALID_ARGUMENT
;
2155 hash
= crypt(blobmsg_get_string(tb
[RPC_D_DATA
]), "$1$");
2157 blob_buf_init(&buf
, 0);
2158 blobmsg_add_string(&buf
, "crypt", hash
);
2160 ubus_send_reply(ctx
, req
, buf
.head
);
2166 rpc_luci2_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
2170 static const struct ubus_method luci2_system_methods
[] = {
2171 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log
),
2172 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg
),
2173 UBUS_METHOD_NOARG("diskfree", rpc_luci2_system_diskfree
),
2174 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list
),
2175 UBUS_METHOD("process_signal", rpc_luci2_process_signal
,
2177 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list
),
2178 UBUS_METHOD("init_action", rpc_luci2_init_action
,
2180 UBUS_METHOD_NOARG("rclocal_get", rpc_luci2_rclocal_get
),
2181 UBUS_METHOD("rclocal_set", rpc_luci2_rclocal_set
,
2183 UBUS_METHOD_NOARG("crontab_get", rpc_luci2_crontab_get
),
2184 UBUS_METHOD("crontab_set", rpc_luci2_crontab_set
,
2186 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get
),
2187 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set
,
2189 UBUS_METHOD("password_set", rpc_luci2_password_set
,
2190 rpc_password_policy
),
2191 UBUS_METHOD_NOARG("led_list", rpc_luci2_led_list
),
2192 UBUS_METHOD_NOARG("usb_list", rpc_luci2_usb_list
),
2193 UBUS_METHOD_NOARG("upgrade_test", rpc_luci2_upgrade_test
),
2194 UBUS_METHOD("upgrade_start", rpc_luci2_upgrade_start
,
2195 rpc_upgrade_policy
),
2196 UBUS_METHOD_NOARG("upgrade_clean", rpc_luci2_upgrade_clean
),
2197 UBUS_METHOD_NOARG("backup_restore", rpc_luci2_backup_restore
),
2198 UBUS_METHOD_NOARG("backup_clean", rpc_luci2_backup_clean
),
2199 UBUS_METHOD_NOARG("backup_config_get", rpc_luci2_backup_config_get
),
2200 UBUS_METHOD("backup_config_set", rpc_luci2_backup_config_set
,
2202 UBUS_METHOD_NOARG("backup_list", rpc_luci2_backup_list
),
2203 UBUS_METHOD_NOARG("reset_test", rpc_luci2_reset_test
),
2204 UBUS_METHOD_NOARG("reset_start", rpc_luci2_reset_start
),
2205 UBUS_METHOD_NOARG("reboot", rpc_luci2_reboot
)
2208 static struct ubus_object_type luci2_system_type
=
2209 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods
);
2211 static struct ubus_object system_obj
= {
2212 .name
= "luci2.system",
2213 .type
= &luci2_system_type
,
2214 .methods
= luci2_system_methods
,
2215 .n_methods
= ARRAY_SIZE(luci2_system_methods
),
2219 static const struct ubus_method luci2_network_methods
[] = {
2220 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count
),
2221 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table
),
2222 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table
),
2223 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases
),
2224 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6
),
2225 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes
),
2226 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6
),
2229 static struct ubus_object_type luci2_network_type
=
2230 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods
);
2232 static struct ubus_object network_obj
= {
2233 .name
= "luci2.network",
2234 .type
= &luci2_network_type
,
2235 .methods
= luci2_network_methods
,
2236 .n_methods
= ARRAY_SIZE(luci2_network_methods
),
2240 static const struct ubus_method luci2_opkg_methods
[] = {
2241 UBUS_METHOD("list", rpc_luci2_opkg_list
,
2242 rpc_opkg_match_policy
),
2243 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed
,
2244 rpc_opkg_match_policy
),
2245 UBUS_METHOD("find", rpc_luci2_opkg_find
,
2246 rpc_opkg_match_policy
),
2247 UBUS_METHOD("install", rpc_luci2_opkg_install
,
2248 rpc_opkg_package_policy
),
2249 UBUS_METHOD("remove", rpc_luci2_opkg_remove
,
2250 rpc_opkg_package_policy
),
2251 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update
),
2252 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get
),
2253 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set
,
2257 static struct ubus_object_type luci2_opkg_type
=
2258 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods
);
2260 static struct ubus_object opkg_obj
= {
2261 .name
= "luci2.opkg",
2262 .type
= &luci2_opkg_type
,
2263 .methods
= luci2_opkg_methods
,
2264 .n_methods
= ARRAY_SIZE(luci2_opkg_methods
),
2268 static const struct ubus_method luci2_ui_methods
[] = {
2269 UBUS_METHOD_NOARG("menu", rpc_luci2_ui_menu
),
2270 UBUS_METHOD_NOARG("acls", rpc_luci2_ui_acls
),
2271 UBUS_METHOD("crypt", rpc_luci2_ui_crypt
,
2275 static struct ubus_object_type luci2_ui_type
=
2276 UBUS_OBJECT_TYPE("luci-rpc-luci2-ui", luci2_ui_methods
);
2278 static struct ubus_object ui_obj
= {
2280 .type
= &luci2_ui_type
,
2281 .methods
= luci2_ui_methods
,
2282 .n_methods
= ARRAY_SIZE(luci2_ui_methods
),
2285 cursor
= uci_alloc_context();
2288 return UBUS_STATUS_UNKNOWN_ERROR
;
2292 rv
|= ubus_add_object(ctx
, &system_obj
);
2293 rv
|= ubus_add_object(ctx
, &network_obj
);
2294 rv
|= ubus_add_object(ctx
, &opkg_obj
);
2295 rv
|= ubus_add_object(ctx
, &ui_obj
);
2300 const struct rpc_plugin rpc_plugin
= {
2301 .init
= rpc_luci2_api_init