luci2: change rpc_exec() callsites to accomodate for extra stdin_cb argument, change...
[project/rpcd.git] / luci2.c
1 /*
2 * luci-rpcd - LuCI UBUS RPC server
3 *
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5 *
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.
9 *
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.
17 */
18
19 #include <fcntl.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <sys/wait.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/statvfs.h>
29 #include <dirent.h>
30 #include <arpa/inet.h>
31 #include <signal.h>
32 #include <glob.h>
33
34 #include "luci2.h"
35 #include "exec.h"
36 #include "session.h"
37
38 static struct blob_buf buf;
39 static struct uci_context *cursor;
40
41 enum {
42 RPC_S_PID,
43 RPC_S_SIGNAL,
44 __RPC_S_MAX,
45 };
46
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 },
50 };
51
52 enum {
53 RPC_I_NAME,
54 RPC_I_ACTION,
55 __RPC_I_MAX,
56 };
57
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 },
61 };
62
63 enum {
64 RPC_D_DATA,
65 __RPC_D_MAX
66 };
67
68 static const struct blobmsg_policy rpc_data_policy[__RPC_D_MAX] = {
69 [RPC_D_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
70 };
71
72 enum {
73 RPC_K_KEYS,
74 __RPC_K_MAX
75 };
76
77 static const struct blobmsg_policy rpc_sshkey_policy[__RPC_K_MAX] = {
78 [RPC_K_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
79 };
80
81 enum {
82 RPC_P_USER,
83 RPC_P_PASSWORD,
84 __RPC_P_MAX
85 };
86
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 },
90 };
91
92 enum {
93 RPC_OM_LIMIT,
94 RPC_OM_OFFSET,
95 RPC_OM_PATTERN,
96 __RPC_OM_MAX
97 };
98
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 },
103 };
104
105 enum {
106 RPC_OP_PACKAGE,
107 __RPC_OP_MAX
108 };
109
110 static const struct blobmsg_policy rpc_opkg_package_policy[__RPC_OP_MAX] = {
111 [RPC_OP_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
112 };
113
114 enum {
115 RPC_UPGRADE_KEEP,
116 __RPC_UPGRADE_MAX
117 };
118
119 static const struct blobmsg_policy rpc_upgrade_policy[__RPC_UPGRADE_MAX] = {
120 [RPC_UPGRADE_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
121 };
122
123 enum {
124 RPC_MENU_SESSION,
125 __RPC_MENU_MAX
126 };
127
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 },
131 };
132
133
134 static int
135 rpc_errno_status(void)
136 {
137 switch (errno)
138 {
139 case EACCES:
140 return UBUS_STATUS_PERMISSION_DENIED;
141
142 case ENOTDIR:
143 return UBUS_STATUS_INVALID_ARGUMENT;
144
145 case ENOENT:
146 return UBUS_STATUS_NOT_FOUND;
147
148 case EINVAL:
149 return UBUS_STATUS_INVALID_ARGUMENT;
150
151 default:
152 return UBUS_STATUS_UNKNOWN_ERROR;
153 }
154 }
155
156 static void
157 log_read(FILE *log, int logsize)
158 {
159 int len;
160 char *logbuf;
161
162 if (logsize == 0)
163 logsize = RPC_LUCI2_DEF_LOGSIZE;
164
165 len = (logsize > RPC_LUCI2_MAX_LOGSIZE) ? RPC_LUCI2_MAX_LOGSIZE : logsize;
166 logbuf = blobmsg_alloc_string_buffer(&buf, "log", len + 1);
167
168 if (!logbuf)
169 return;
170
171 while (logsize > RPC_LUCI2_MAX_LOGSIZE)
172 {
173 len = logsize % RPC_LUCI2_MAX_LOGSIZE;
174
175 if (len == 0)
176 len = RPC_LUCI2_MAX_LOGSIZE;
177
178 fread(logbuf, 1, len, log);
179 logsize -= len;
180 }
181
182 len = fread(logbuf, 1, logsize, log);
183 *(logbuf + len) = 0;
184
185 blobmsg_add_string_buffer(&buf);
186 }
187
188 static int
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)
192 {
193 FILE *log;
194 int logsize = 0;
195 const char *logfile = NULL;
196 struct stat st;
197 struct uci_package *p;
198 struct uci_element *e;
199 struct uci_section *s;
200 struct uci_ptr ptr = { .package = "system" };
201
202 uci_load(cursor, ptr.package, &p);
203
204 if (!p)
205 return UBUS_STATUS_NOT_FOUND;
206
207 uci_foreach_element(&p->sections, e)
208 {
209 s = uci_to_section(e);
210
211 if (strcmp(s->type, "system"))
212 continue;
213
214 ptr.o = NULL;
215 ptr.option = "log_type";
216 ptr.section = e->name;
217 uci_lookup_ptr(cursor, &ptr, NULL, true);
218 break;
219 }
220
221 if (ptr.o && ptr.o->type == UCI_TYPE_STRING &&
222 !strcmp(ptr.o->v.string, "file"))
223 {
224 ptr.o = NULL;
225 ptr.option = "log_file";
226 uci_lookup_ptr(cursor, &ptr, NULL, true);
227
228 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
229 logfile = ptr.o->v.string;
230 else
231 logfile = "/var/log/messages";
232
233 if (stat(logfile, &st) || !(log = fopen(logfile, "r")))
234 goto fail;
235
236 logsize = st.st_size;
237 }
238 else
239 {
240 ptr.o = NULL;
241 ptr.option = "log_size";
242 uci_lookup_ptr(cursor, &ptr, NULL, true);
243
244 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
245 logsize = atoi(ptr.o->v.string) * 1024;
246
247 if (!(log = popen("logread", "r")))
248 goto fail;
249 }
250
251 blob_buf_init(&buf, 0);
252
253 log_read(log, logsize);
254 fclose(log);
255
256 uci_unload(cursor, p);
257 ubus_send_reply(ctx, req, buf.head);
258 return 0;
259
260 fail:
261 uci_unload(cursor, p);
262 return rpc_errno_status();
263 }
264
265 static int
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)
269 {
270 FILE *log;
271
272 if (!(log = popen("dmesg", "r")))
273 return rpc_errno_status();
274
275 blob_buf_init(&buf, 0);
276
277 log_read(log, RPC_LUCI2_MAX_LOGSIZE);
278 fclose(log);
279
280 ubus_send_reply(ctx, req, buf.head);
281 return 0;
282 }
283
284 static int
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)
288 {
289 int i;
290 void *c;
291 struct statvfs s;
292 const char *fslist[] = {
293 "/", "root",
294 "/tmp", "tmp",
295 };
296
297 blob_buf_init(&buf, 0);
298
299 for (i = 0; i < sizeof(fslist) / sizeof(fslist[0]); i += 2)
300 {
301 if (statvfs(fslist[i], &s))
302 continue;
303
304 c = blobmsg_open_table(&buf, fslist[i+1]);
305
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);
309
310 blobmsg_close_table(&buf, c);
311 }
312
313 ubus_send_reply(ctx, req, buf.head);
314 return 0;
315 }
316
317 static int
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)
321 {
322 FILE *top;
323 void *c, *d;
324 char line[1024];
325 char *pid, *ppid, *user, *stat, *vsz, *pvsz, *pcpu, *cmd;
326
327 if (!(top = popen("/bin/busybox top -bn1", "r")))
328 return rpc_errno_status();
329
330 blob_buf_init(&buf, 0);
331 c = blobmsg_open_array(&buf, "processes");
332
333 while (fgets(line, sizeof(line) - 1, top))
334 {
335 pid = strtok(line, " ");
336
337 if (*pid < '0' || *pid > '9')
338 continue;
339
340 ppid = strtok(NULL, " ");
341 user = strtok(NULL, " ");
342 stat = strtok(NULL, " ");
343
344 if (!stat)
345 continue;
346
347 if (!*(stat + 1))
348 *(stat + 1) = ' ';
349
350 if (!*(stat + 2))
351 *(stat + 2) = ' ';
352
353 *(stat + 3) = 0;
354
355 vsz = strtok(stat + 4, " ");
356 pvsz = strtok(NULL, " ");
357 pcpu = strtok(NULL, " ");
358 cmd = strtok(NULL, "\n");
359
360 if (!cmd)
361 continue;
362
363 d = blobmsg_open_table(&buf, NULL);
364
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);
373
374 blobmsg_close_table(&buf, d);
375 }
376
377 fclose(top);
378 blobmsg_close_array(&buf, c);
379
380 ubus_send_reply(ctx, req, buf.head);
381 return 0;
382 }
383
384 static int
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)
388 {
389 int pid, sig;
390 struct blob_attr *tb[__RPC_S_MAX];
391
392 blobmsg_parse(rpc_signal_policy, __RPC_S_MAX, tb,
393 blob_data(msg), blob_len(msg));
394
395 if (!tb[RPC_S_SIGNAL] || !tb[RPC_S_PID])
396 {
397 errno = EINVAL;
398 return rpc_errno_status();
399 }
400
401 pid = blobmsg_get_u32(tb[RPC_S_PID]);
402 sig = blobmsg_get_u32(tb[RPC_S_SIGNAL]);
403
404 if (kill(pid, sig))
405 return rpc_errno_status();
406
407 return 0;
408 }
409
410 static int
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)
414 {
415 int n;
416 void *c, *t;
417 char *p, path[PATH_MAX];
418 struct stat s;
419 struct dirent *e;
420 FILE *f;
421 DIR *d;
422
423 if (!(d = opendir("/etc/init.d")))
424 return rpc_errno_status();
425
426 blob_buf_init(&buf, 0);
427 c = blobmsg_open_array(&buf, "initscripts");
428
429 while ((e = readdir(d)) != NULL)
430 {
431 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s", e->d_name);
432
433 if (stat(path, &s) || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR))
434 continue;
435
436 if ((f = fopen(path, "r")) != NULL)
437 {
438 n = -1;
439 p = fgets(path, sizeof(path) - 1, f);
440
441 if (!p || !strstr(p, "/etc/rc.common"))
442 goto skip;
443
444 t = blobmsg_open_table(&buf, NULL);
445
446 blobmsg_add_string(&buf, "name", e->d_name);
447
448 while (fgets(path, sizeof(path) - 1, f))
449 {
450 p = strtok(path, "= \t");
451
452 if (!strcmp(p, "START") && !!(p = strtok(NULL, "= \t\n")))
453 {
454 n = atoi(p);
455 blobmsg_add_u32(&buf, "start", n);
456 }
457 else if (!strcmp(p, "STOP") && !!(p = strtok(NULL, "= \t\n")))
458 {
459 blobmsg_add_u32(&buf, "stop", atoi(p));
460 break;
461 }
462 }
463
464 if (n > -1)
465 {
466 snprintf(path, sizeof(path) - 1, "/etc/rc.d/S%02d%s",
467 n, e->d_name);
468
469 blobmsg_add_u8(&buf, "enabled",
470 (!stat(path, &s) && (s.st_mode & S_IXUSR)));
471 }
472 else
473 {
474 blobmsg_add_u8(&buf, "enabled", 0);
475 }
476
477 blobmsg_close_table(&buf, t);
478
479 skip:
480 fclose(f);
481 }
482 }
483
484 closedir(d);
485 blobmsg_close_array(&buf, c);
486
487 ubus_send_reply(ctx, req, buf.head);
488 return 0;
489 }
490
491 static int
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)
495 {
496 int fd;
497 pid_t pid;
498 struct stat s;
499 char path[PATH_MAX];
500 const char *action;
501 struct blob_attr *tb[__RPC_I_MAX];
502
503 blobmsg_parse(rpc_init_policy, __RPC_I_MAX, tb,
504 blob_data(msg), blob_len(msg));
505
506 if (!tb[RPC_I_NAME] || !tb[RPC_I_ACTION])
507 return UBUS_STATUS_INVALID_ARGUMENT;
508
509 action = blobmsg_data(tb[RPC_I_ACTION]);
510
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;
515
516 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s",
517 (char *)blobmsg_data(tb[RPC_I_NAME]));
518
519 if (stat(path, &s))
520 return rpc_errno_status();
521
522 if (!(s.st_mode & S_IXUSR))
523 return UBUS_STATUS_PERMISSION_DENIED;
524
525 switch ((pid = fork()))
526 {
527 case -1:
528 return rpc_errno_status();
529
530 case 0:
531 uloop_done();
532
533 if ((fd = open("/dev/null", O_RDWR)) > -1)
534 {
535 dup2(fd, 0);
536 dup2(fd, 1);
537 dup2(fd, 2);
538
539 close(fd);
540 }
541
542 chdir("/");
543
544 if (execl(path, path, action, NULL))
545 return rpc_errno_status();
546
547 default:
548 return 0;
549 }
550 }
551
552 static int
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)
556 {
557 FILE *f;
558 char data[4096] = { 0 };
559
560 if (!(f = fopen("/etc/rc.local", "r")))
561 return rpc_errno_status();
562
563 fread(data, sizeof(data) - 1, 1, f);
564 fclose(f);
565
566 blob_buf_init(&buf, 0);
567 blobmsg_add_string(&buf, "data", data);
568
569 ubus_send_reply(ctx, req, buf.head);
570 return 0;
571 }
572
573 static int
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)
577 {
578 FILE *f;
579 struct blob_attr *tb[__RPC_D_MAX];
580
581 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
582 blob_data(msg), blob_len(msg));
583
584 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 4096)
585 return UBUS_STATUS_INVALID_ARGUMENT;
586
587 if (!(f = fopen("/etc/rc.local", "w")))
588 return rpc_errno_status();
589
590 fwrite(blobmsg_data(tb[RPC_D_DATA]),
591 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
592
593 fclose(f);
594 return 0;
595 }
596
597 static int
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)
601 {
602 FILE *f;
603 char data[4096] = { 0 };
604
605 if (!(f = fopen("/etc/crontabs/root", "r")))
606 return rpc_errno_status();
607
608 fread(data, sizeof(data) - 1, 1, f);
609 fclose(f);
610
611 blob_buf_init(&buf, 0);
612 blobmsg_add_string(&buf, "data", data);
613
614 ubus_send_reply(ctx, req, buf.head);
615 return 0;
616 }
617
618 static int
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)
622 {
623 FILE *f;
624 struct stat s;
625 struct blob_attr *tb[__RPC_D_MAX];
626
627 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
628 blob_data(msg), blob_len(msg));
629
630 if (!tb[RPC_D_DATA] || blobmsg_data_len(tb[RPC_D_DATA]) >= 4096)
631 return UBUS_STATUS_INVALID_ARGUMENT;
632
633 if (stat("/etc/crontabs", &s) && mkdir("/etc/crontabs", 0755))
634 return rpc_errno_status();
635
636 if (!(f = fopen("/etc/crontabs/root", "w")))
637 return rpc_errno_status();
638
639 fwrite(blobmsg_data(tb[RPC_D_DATA]),
640 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
641
642 fclose(f);
643 return 0;
644 }
645
646 static int
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)
650 {
651 FILE *f;
652 void *c;
653 char *p, line[4096];
654
655 if (!(f = fopen("/etc/dropbear/authorized_keys", "r")))
656 return rpc_errno_status();
657
658 blob_buf_init(&buf, 0);
659 c = blobmsg_open_array(&buf, "keys");
660
661 while (fgets(line, sizeof(line) - 1, f))
662 {
663 for (p = line + strlen(line) - 1; (p > line) && isspace(*p); p--)
664 *p = 0;
665
666 for (p = line; isspace(*p); p++)
667 *p = 0;
668
669 if (*p)
670 blobmsg_add_string(&buf, NULL, p);
671 }
672
673 blobmsg_close_array(&buf, c);
674 fclose(f);
675
676 ubus_send_reply(ctx, req, buf.head);
677 return 0;
678 }
679
680 static int
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)
684 {
685 FILE *f;
686 int rem;
687 struct blob_attr *cur, *tb[__RPC_K_MAX];
688
689 blobmsg_parse(rpc_sshkey_policy, __RPC_K_MAX, tb,
690 blob_data(msg), blob_len(msg));
691
692 if (!tb[RPC_K_KEYS])
693 return UBUS_STATUS_INVALID_ARGUMENT;
694
695 if (!(f = fopen("/etc/dropbear/authorized_keys", "w")))
696 return rpc_errno_status();
697
698 blobmsg_for_each_attr(cur, tb[RPC_K_KEYS], rem)
699 {
700 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
701 continue;
702
703 fwrite(blobmsg_data(cur), blobmsg_data_len(cur) - 1, 1, f);
704 fwrite("\n", 1, 1, f);
705 }
706
707 fclose(f);
708 return 0;
709 }
710
711 static int
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)
715 {
716 pid_t pid;
717 int fd, fds[2];
718 struct stat s;
719 struct blob_attr *tb[__RPC_P_MAX];
720
721 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
722 blob_data(msg), blob_len(msg));
723
724 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
725 return UBUS_STATUS_INVALID_ARGUMENT;
726
727 if (stat("/usr/bin/passwd", &s))
728 return UBUS_STATUS_NOT_FOUND;
729
730 if (!(s.st_mode & S_IXUSR))
731 return UBUS_STATUS_PERMISSION_DENIED;
732
733 if (pipe(fds))
734 return rpc_errno_status();
735
736 switch ((pid = fork()))
737 {
738 case -1:
739 close(fds[0]);
740 close(fds[1]);
741 return rpc_errno_status();
742
743 case 0:
744 uloop_done();
745
746 dup2(fds[0], 0);
747 close(fds[0]);
748 close(fds[1]);
749
750 if ((fd = open("/dev/null", O_RDWR)) > -1)
751 {
752 dup2(fd, 1);
753 dup2(fd, 2);
754 close(fd);
755 }
756
757 chdir("/");
758
759 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
760 blobmsg_data(tb[RPC_P_USER]), NULL))
761 return rpc_errno_status();
762
763 default:
764 close(fds[0]);
765
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);
769
770 usleep(100 * 1000);
771
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);
775
776 close(fds[1]);
777
778 waitpid(pid, NULL, 0);
779
780 return 0;
781 }
782 }
783
784 static int
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)
788 {
789 DIR *d;
790 FILE *f;
791 void *list, *led, *trigger;
792 char *p, *active_trigger, line[512];
793 struct dirent *e;
794
795 if (!(d = opendir("/sys/class/leds")))
796 return rpc_errno_status();
797
798 blob_buf_init(&buf, 0);
799 list = blobmsg_open_array(&buf, "leds");
800
801 while ((e = readdir(d)) != NULL)
802 {
803 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/trigger",
804 e->d_name);
805
806 if (!(f = fopen(line, "r")))
807 continue;
808
809 led = blobmsg_open_table(&buf, NULL);
810
811 blobmsg_add_string(&buf, "name", e->d_name);
812
813 if (fgets(line, sizeof(line) - 1, f))
814 {
815 trigger = blobmsg_open_array(&buf, "triggers");
816
817 for (p = strtok(line, " \n"), active_trigger = NULL;
818 p != NULL;
819 p = strtok(NULL, " \n"))
820 {
821 if (*p == '[')
822 {
823 *(p + strlen(p) - 1) = 0;
824 *p++ = 0;
825 active_trigger = p;
826 }
827
828 blobmsg_add_string(&buf, NULL, p);
829 }
830
831 blobmsg_close_array(&buf, trigger);
832
833 if (active_trigger)
834 blobmsg_add_string(&buf, "active_trigger", active_trigger);
835 }
836
837 fclose(f);
838
839 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/brightness",
840 e->d_name);
841
842 if ((f = fopen(line, "r")) != NULL)
843 {
844 if (fgets(line, sizeof(line) - 1, f))
845 blobmsg_add_u32(&buf, "brightness", atoi(line));
846
847 fclose(f);
848 }
849
850 snprintf(line, sizeof(line) - 1, "/sys/class/leds/%s/max_brightness",
851 e->d_name);
852
853 if ((f = fopen(line, "r")) != NULL)
854 {
855 if (fgets(line, sizeof(line) - 1, f))
856 blobmsg_add_u32(&buf, "max_brightness", atoi(line));
857
858 fclose(f);
859 }
860
861 blobmsg_close_table(&buf, led);
862 }
863
864 closedir(d);
865
866 blobmsg_close_array(&buf, list);
867 ubus_send_reply(ctx, req, buf.head);
868
869 return 0;
870 }
871
872 static int
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)
876 {
877 DIR *d;
878 FILE *f;
879 int i;
880 void *list, *device;
881 char *p, line[512];
882 struct stat s;
883 struct dirent *e;
884
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",
892 };
893
894 if (!(d = opendir("/sys/bus/usb/devices")))
895 return rpc_errno_status();
896
897 blob_buf_init(&buf, 0);
898 list = blobmsg_open_array(&buf, "devices");
899
900 while ((e = readdir(d)) != NULL)
901 {
902 if (e->d_name[0] < '0' || e->d_name[0] > '9')
903 continue;
904
905 snprintf(line, sizeof(line) - 1,
906 "/sys/bus/usb/devices/%s/%s", e->d_name, attributes[0]);
907
908 if (stat(line, &s))
909 continue;
910
911 device = blobmsg_open_table(&buf, NULL);
912
913 blobmsg_add_string(&buf, "name", e->d_name);
914
915 for (i = 0; i < sizeof(attributes) / sizeof(attributes[0]); i += 3)
916 {
917 snprintf(line, sizeof(line) - 1,
918 "/sys/bus/usb/devices/%s/%s", e->d_name, attributes[i]);
919
920 if (!(f = fopen(line, "r")))
921 continue;
922
923 if (fgets(line, sizeof(line) - 1, f))
924 {
925 switch (*attributes[i+2])
926 {
927 case 'x':
928 blobmsg_add_u32(&buf, attributes[i+1],
929 strtoul(line, NULL, 16));
930 break;
931
932 case 'd':
933 blobmsg_add_u32(&buf, attributes[i+1],
934 strtoul(line, NULL, 10));
935 break;
936
937 default:
938 if ((p = strchr(line, '\n')) != NULL)
939 while (p > line && isspace(*p))
940 *p-- = 0;
941
942 blobmsg_add_string(&buf, attributes[i+1], line);
943 break;
944 }
945 }
946
947 fclose(f);
948 }
949
950 blobmsg_close_table(&buf, device);
951 }
952
953 closedir(d);
954
955 blobmsg_close_array(&buf, list);
956 ubus_send_reply(ctx, req, buf.head);
957
958 return 0;
959 }
960
961 static int
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)
965 {
966 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
967 return rpc_exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
968 }
969
970 static int
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)
974 {
975 return 0;
976 }
977
978 static int
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)
982 {
983 if (unlink("/tmp/firmware.bin"))
984 return rpc_errno_status();
985
986 return 0;
987 }
988
989 static int
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)
993 {
994 const char *cmd[4] = { "sysupgrade", "--restore-backup",
995 "/tmp/backup.tar.gz", NULL };
996
997 return rpc_exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
998 }
999
1000 static int
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)
1004 {
1005 if (unlink("/tmp/backup.tar.gz"))
1006 return rpc_errno_status();
1007
1008 return 0;
1009 }
1010
1011 static int
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)
1015 {
1016 FILE *f;
1017 char conf[2048] = { 0 };
1018
1019 if (!(f = fopen("/etc/sysupgrade.conf", "r")))
1020 return rpc_errno_status();
1021
1022 fread(conf, sizeof(conf) - 1, 1, f);
1023 fclose(f);
1024
1025 blob_buf_init(&buf, 0);
1026 blobmsg_add_string(&buf, "config", conf);
1027
1028 ubus_send_reply(ctx, req, buf.head);
1029 return 0;
1030 }
1031
1032 static int
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)
1036 {
1037 FILE *f;
1038 struct blob_attr *tb[__RPC_D_MAX];
1039
1040 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
1041 blob_data(msg), blob_len(msg));
1042
1043 if (!tb[RPC_D_DATA])
1044 return UBUS_STATUS_INVALID_ARGUMENT;
1045
1046 if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048)
1047 return UBUS_STATUS_NOT_SUPPORTED;
1048
1049 if (!(f = fopen("/etc/sysupgrade.conf", "w")))
1050 return rpc_errno_status();
1051
1052 fwrite(blobmsg_data(tb[RPC_D_DATA]),
1053 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
1054
1055 fclose(f);
1056 return 0;
1057 }
1058
1059 struct backup_state {
1060 bool open;
1061 void *array;
1062 };
1063
1064 static int
1065 backup_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1066 {
1067 struct backup_state *s = priv;
1068 char *nl = strchr(buf, '\n');
1069
1070 if (!nl)
1071 return 0;
1072
1073 if (!s->open)
1074 {
1075 s->open = true;
1076 s->array = blobmsg_open_array(blob, "files");
1077 }
1078
1079 *nl = 0;
1080 blobmsg_add_string(blob, NULL, buf);
1081
1082 return (nl - buf + 1);
1083 }
1084
1085 static int
1086 backup_finish_list(struct blob_buf *blob, int status, void *priv)
1087 {
1088 struct backup_state *s = priv;
1089
1090 if (!s->open)
1091 return UBUS_STATUS_NO_DATA;
1092
1093 blobmsg_close_array(blob, s->array);
1094
1095 return UBUS_STATUS_OK;
1096 }
1097
1098 static int
1099 rpc_luci2_backup_list(struct ubus_context *ctx, struct ubus_object *obj,
1100 struct ubus_request_data *req, const char *method,
1101 struct blob_attr *msg)
1102 {
1103 struct backup_state *state = NULL;
1104 const char *cmd[3] = { "sysupgrade", "--list-backup", NULL };
1105
1106 state = malloc(sizeof(*state));
1107
1108 if (!state)
1109 return UBUS_STATUS_UNKNOWN_ERROR;
1110
1111 memset(state, 0, sizeof(*state));
1112
1113 return rpc_exec(cmd, NULL, backup_parse_list, NULL, backup_finish_list,
1114 state, ctx, req);
1115 }
1116
1117 static int
1118 rpc_luci2_reset_test(struct ubus_context *ctx, struct ubus_object *obj,
1119 struct ubus_request_data *req, const char *method,
1120 struct blob_attr *msg)
1121 {
1122 FILE *mtd;
1123 struct stat s;
1124 char line[64] = { 0 };
1125 bool supported = false;
1126
1127 if (!stat("/sbin/mtd", &s) && (s.st_mode & S_IXUSR))
1128 {
1129 if ((mtd = fopen("/proc/mtd", "r")) != NULL)
1130 {
1131 while (fgets(line, sizeof(line) - 1, mtd))
1132 {
1133 if (strstr(line, "\"rootfs_data\""))
1134 {
1135 supported = true;
1136 break;
1137 }
1138 }
1139
1140 fclose(mtd);
1141 }
1142 }
1143
1144 blob_buf_init(&buf, 0);
1145 blobmsg_add_u8(&buf, "supported", supported);
1146
1147 ubus_send_reply(ctx, req, buf.head);
1148
1149 return 0;
1150 }
1151
1152 static int
1153 rpc_luci2_reset_start(struct ubus_context *ctx, struct ubus_object *obj,
1154 struct ubus_request_data *req, const char *method,
1155 struct blob_attr *msg)
1156 {
1157 switch (fork())
1158 {
1159 case -1:
1160 return rpc_errno_status();
1161
1162 case 0:
1163 uloop_done();
1164
1165 chdir("/");
1166
1167 close(0);
1168 close(1);
1169 close(2);
1170
1171 sleep(1);
1172
1173 execl("/sbin/mtd", "/sbin/mtd", "-r", "erase", "rootfs_data", NULL);
1174
1175 return rpc_errno_status();
1176
1177 default:
1178 return 0;
1179 }
1180 }
1181
1182 static int
1183 rpc_luci2_reboot(struct ubus_context *ctx, struct ubus_object *obj,
1184 struct ubus_request_data *req, const char *method,
1185 struct blob_attr *msg)
1186 {
1187 switch (fork())
1188 {
1189 case -1:
1190 return rpc_errno_status();
1191
1192 case 0:
1193 chdir("/");
1194
1195 close(0);
1196 close(1);
1197 close(2);
1198
1199 sleep(1);
1200
1201 execl("/sbin/reboot", "/sbin/reboot", NULL);
1202
1203 return rpc_errno_status();
1204
1205 default:
1206 return 0;
1207 }
1208 }
1209
1210
1211 static FILE *
1212 dnsmasq_leasefile(void)
1213 {
1214 FILE *leases = NULL;
1215 struct uci_package *p;
1216 struct uci_element *e;
1217 struct uci_section *s;
1218 struct uci_ptr ptr = {
1219 .package = "dhcp",
1220 .section = NULL,
1221 .option = "leasefile"
1222 };
1223
1224 uci_load(cursor, ptr.package, &p);
1225
1226 if (!p)
1227 return NULL;
1228
1229 uci_foreach_element(&p->sections, e)
1230 {
1231 s = uci_to_section(e);
1232
1233 if (strcmp(s->type, "dnsmasq"))
1234 continue;
1235
1236 ptr.section = e->name;
1237 uci_lookup_ptr(cursor, &ptr, NULL, true);
1238 break;
1239 }
1240
1241 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
1242 leases = fopen(ptr.o->v.string, "r");
1243
1244 uci_unload(cursor, p);
1245
1246 return leases;
1247 }
1248
1249 static int
1250 rpc_luci2_network_leases(struct ubus_context *ctx, struct ubus_object *obj,
1251 struct ubus_request_data *req, const char *method,
1252 struct blob_attr *msg)
1253 {
1254 FILE *leases;
1255 void *c, *d;
1256 char line[128];
1257 char *ts, *mac, *addr, *name;
1258 time_t now = time(NULL);
1259
1260 blob_buf_init(&buf, 0);
1261 c = blobmsg_open_array(&buf, "leases");
1262
1263 leases = dnsmasq_leasefile();
1264
1265 if (!leases)
1266 goto out;
1267
1268 while (fgets(line, sizeof(line) - 1, leases))
1269 {
1270 ts = strtok(line, " \t");
1271 mac = strtok(NULL, " \t");
1272 addr = strtok(NULL, " \t");
1273 name = strtok(NULL, " \t");
1274
1275 if (!ts || !mac || !addr || !name)
1276 continue;
1277
1278 if (strchr(addr, ':'))
1279 continue;
1280
1281 d = blobmsg_open_table(&buf, NULL);
1282
1283 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1284 blobmsg_add_string(&buf, "macaddr", mac);
1285 blobmsg_add_string(&buf, "ipaddr", addr);
1286
1287 if (strcmp(name, "*"))
1288 blobmsg_add_string(&buf, "hostname", name);
1289
1290 blobmsg_close_table(&buf, d);
1291 }
1292
1293 fclose(leases);
1294
1295 out:
1296 blobmsg_close_array(&buf, c);
1297 ubus_send_reply(ctx, req, buf.head);
1298
1299 return 0;
1300 }
1301
1302 static int
1303 rpc_luci2_network_leases6(struct ubus_context *ctx, struct ubus_object *obj,
1304 struct ubus_request_data *req, const char *method,
1305 struct blob_attr *msg)
1306 {
1307 FILE *leases;
1308 void *c, *d;
1309 char line[128];
1310 char *ts, *mac, *addr, *name, *duid;
1311 time_t now = time(NULL);
1312
1313 blob_buf_init(&buf, 0);
1314 c = blobmsg_open_array(&buf, "leases");
1315
1316 leases = fopen("/tmp/hosts/6relayd", "r");
1317
1318 if (leases)
1319 {
1320 while (fgets(line, sizeof(line) - 1, leases))
1321 {
1322 if (strncmp(line, "# ", 2))
1323 continue;
1324
1325 strtok(line + 2, " \t"); /* iface */
1326
1327 duid = strtok(NULL, " \t");
1328
1329 strtok(NULL, " \t"); /* iaid */
1330
1331 name = strtok(NULL, " \t");
1332 ts = strtok(NULL, " \t");
1333
1334 strtok(NULL, " \t"); /* id */
1335 strtok(NULL, " \t"); /* length */
1336
1337 addr = strtok(NULL, " \t\n");
1338
1339 if (!addr)
1340 continue;
1341
1342 d = blobmsg_open_table(&buf, NULL);
1343
1344 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1345 blobmsg_add_string(&buf, "duid", duid);
1346 blobmsg_add_string(&buf, "ip6addr", addr);
1347
1348 if (strcmp(name, "-"))
1349 blobmsg_add_string(&buf, "hostname", name);
1350
1351 blobmsg_close_array(&buf, d);
1352 }
1353
1354 fclose(leases);
1355 }
1356 else
1357 {
1358 leases = dnsmasq_leasefile();
1359
1360 if (!leases)
1361 goto out;
1362
1363 while (fgets(line, sizeof(line) - 1, leases))
1364 {
1365 ts = strtok(line, " \t");
1366 mac = strtok(NULL, " \t");
1367 addr = strtok(NULL, " \t");
1368 name = strtok(NULL, " \t");
1369 duid = strtok(NULL, " \t\n");
1370
1371 if (!ts || !mac || !addr || !duid)
1372 continue;
1373
1374 if (!strchr(addr, ':'))
1375 continue;
1376
1377 d = blobmsg_open_table(&buf, NULL);
1378
1379 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1380 blobmsg_add_string(&buf, "macaddr", mac);
1381 blobmsg_add_string(&buf, "ip6addr", addr);
1382
1383 if (strcmp(name, "*"))
1384 blobmsg_add_string(&buf, "hostname", name);
1385
1386 if (strcmp(duid, "*"))
1387 blobmsg_add_string(&buf, "duid", name);
1388
1389 blobmsg_close_table(&buf, d);
1390 }
1391
1392 fclose(leases);
1393 }
1394
1395 out:
1396 blobmsg_close_array(&buf, c);
1397 ubus_send_reply(ctx, req, buf.head);
1398
1399 return 0;
1400 }
1401
1402 static int
1403 rpc_luci2_network_ct_count(struct ubus_context *ctx, struct ubus_object *obj,
1404 struct ubus_request_data *req, const char *method,
1405 struct blob_attr *msg)
1406 {
1407 FILE *f;
1408 char line[128];
1409
1410 blob_buf_init(&buf, 0);
1411
1412 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL)
1413 {
1414 if (fgets(line, sizeof(line) - 1, f))
1415 blobmsg_add_u32(&buf, "count", atoi(line));
1416
1417 fclose(f);
1418 }
1419
1420 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL)
1421 {
1422 if (fgets(line, sizeof(line) - 1, f))
1423 blobmsg_add_u32(&buf, "limit", atoi(line));
1424
1425 fclose(f);
1426 }
1427
1428 ubus_send_reply(ctx, req, buf.head);
1429
1430 return 0;
1431 }
1432
1433 static int
1434 rpc_luci2_network_ct_table(struct ubus_context *ctx, struct ubus_object *obj,
1435 struct ubus_request_data *req, const char *method,
1436 struct blob_attr *msg)
1437 {
1438 FILE *f;
1439 int i;
1440 void *c, *d;
1441 char *p, line[512];
1442 bool seen[6];
1443
1444 blob_buf_init(&buf, 0);
1445 c = blobmsg_open_array(&buf, "entries");
1446
1447 if ((f = fopen("/proc/net/nf_conntrack", "r")) != NULL)
1448 {
1449 while (fgets(line, sizeof(line) - 1, f))
1450 {
1451 d = blobmsg_open_table(&buf, NULL);
1452 memset(seen, 0, sizeof(seen));
1453
1454 for (i = 0, p = strtok(line, " "); p; i++, p = strtok(NULL, " "))
1455 {
1456 if (i == 0)
1457 blobmsg_add_u8(&buf, "ipv6", !strcmp(p, "ipv6"));
1458 else if (i == 3)
1459 blobmsg_add_u32(&buf, "protocol", atoi(p));
1460 else if (i == 4)
1461 blobmsg_add_u32(&buf, "expires", atoi(p));
1462 else if (i >= 5)
1463 {
1464 if (*p == '[')
1465 continue;
1466
1467 if (!seen[0] && !strncmp(p, "src=", 4))
1468 {
1469 blobmsg_add_string(&buf, "src", p + 4);
1470 seen[0] = true;
1471 }
1472 else if (!seen[1] && !strncmp(p, "dst=", 4))
1473 {
1474 blobmsg_add_string(&buf, "dest", p + 4);
1475 seen[1] = true;
1476 }
1477 else if (!seen[2] && !strncmp(p, "sport=", 6))
1478 {
1479 blobmsg_add_u32(&buf, "sport", atoi(p + 6));
1480 seen[2] = true;
1481 }
1482 else if (!seen[3] && !strncmp(p, "dport=", 6))
1483 {
1484 blobmsg_add_u32(&buf, "dport", atoi(p + 6));
1485 seen[3] = true;
1486 }
1487 else if (!strncmp(p, "packets=", 8))
1488 {
1489 blobmsg_add_u32(&buf,
1490 seen[4] ? "tx_packets" : "rx_packets",
1491 atoi(p + 8));
1492 seen[4] = true;
1493 }
1494 else if (!strncmp(p, "bytes=", 6))
1495 {
1496 blobmsg_add_u32(&buf,
1497 seen[5] ? "tx_bytes" : "rx_bytes",
1498 atoi(p + 6));
1499 seen[5] = true;
1500 }
1501 }
1502 }
1503
1504 blobmsg_close_table(&buf, d);
1505 }
1506
1507 fclose(f);
1508 }
1509
1510 blobmsg_close_array(&buf, c);
1511 ubus_send_reply(ctx, req, buf.head);
1512
1513 return 0;
1514 }
1515
1516 static int
1517 rpc_luci2_network_arp_table(struct ubus_context *ctx, struct ubus_object *obj,
1518 struct ubus_request_data *req, const char *method,
1519 struct blob_attr *msg)
1520 {
1521 FILE *f;
1522 void *c, *d;
1523 char *addr, *mac, *dev, line[128];
1524
1525 blob_buf_init(&buf, 0);
1526 c = blobmsg_open_array(&buf, "entries");
1527
1528 if ((f = fopen("/proc/net/arp", "r")) != NULL)
1529 {
1530 /* skip header line */
1531 fgets(line, sizeof(line) - 1, f);
1532
1533 while (fgets(line, sizeof(line) - 1, f))
1534 {
1535 addr = strtok(line, " \t");
1536
1537 strtok(NULL, " \t"); /* HW type */
1538 strtok(NULL, " \t"); /* Flags */
1539
1540 mac = strtok(NULL, " \t");
1541
1542 strtok(NULL, " \t"); /* Mask */
1543
1544 dev = strtok(NULL, " \t\n");
1545
1546 if (!dev)
1547 continue;
1548
1549 d = blobmsg_open_table(&buf, NULL);
1550 blobmsg_add_string(&buf, "ipaddr", addr);
1551 blobmsg_add_string(&buf, "macaddr", mac);
1552 blobmsg_add_string(&buf, "device", dev);
1553 blobmsg_close_table(&buf, d);
1554 }
1555
1556 fclose(f);
1557 }
1558
1559 blobmsg_close_array(&buf, c);
1560 ubus_send_reply(ctx, req, buf.head);
1561
1562 return 0;
1563 }
1564
1565 static void
1566 put_hexaddr(const char *name, const char *s, const char *m)
1567 {
1568 int bits;
1569 struct in_addr a;
1570 char as[sizeof("255.255.255.255/32\0")];
1571
1572 a.s_addr = strtoul(s, NULL, 16);
1573 inet_ntop(AF_INET, &a, as, sizeof(as));
1574
1575 if (m)
1576 {
1577 for (a.s_addr = ntohl(strtoul(m, NULL, 16)), bits = 0;
1578 a.s_addr & 0x80000000;
1579 a.s_addr <<= 1)
1580 bits++;
1581
1582 sprintf(as + strlen(as), "/%u", bits);
1583 }
1584
1585 blobmsg_add_string(&buf, name, as);
1586 }
1587
1588 static int
1589 rpc_luci2_network_routes(struct ubus_context *ctx, struct ubus_object *obj,
1590 struct ubus_request_data *req, const char *method,
1591 struct blob_attr *msg)
1592 {
1593 FILE *routes;
1594 void *c, *d;
1595 char *dst, *dmask, *next, *metric, *device;
1596 char line[256];
1597 unsigned int n;
1598
1599 if (!(routes = fopen("/proc/net/route", "r")))
1600 return rpc_errno_status();
1601
1602 blob_buf_init(&buf, 0);
1603 c = blobmsg_open_array(&buf, "routes");
1604
1605 /* skip header line */
1606 fgets(line, sizeof(line) - 1, routes);
1607
1608 while (fgets(line, sizeof(line) - 1, routes))
1609 {
1610 device = strtok(line, "\t ");
1611 dst = strtok(NULL, "\t ");
1612 next = strtok(NULL, "\t ");
1613
1614 strtok(NULL, "\t "); /* flags */
1615 strtok(NULL, "\t "); /* refcount */
1616 strtok(NULL, "\t "); /* usecount */
1617
1618 metric = strtok(NULL, "\t ");
1619 dmask = strtok(NULL, "\t ");
1620
1621 if (!dmask)
1622 continue;
1623
1624 d = blobmsg_open_table(&buf, NULL);
1625
1626 put_hexaddr("target", dst, dmask);
1627 put_hexaddr("nexthop", next, NULL);
1628
1629 n = strtoul(metric, NULL, 10);
1630 blobmsg_add_u32(&buf, "metric", n);
1631
1632 blobmsg_add_string(&buf, "device", device);
1633
1634 blobmsg_close_table(&buf, d);
1635 }
1636
1637 blobmsg_close_array(&buf, c);
1638 fclose(routes);
1639
1640 ubus_send_reply(ctx, req, buf.head);
1641 return 0;
1642 }
1643
1644 static void
1645 put_hex6addr(const char *name, const char *s, const char *m)
1646 {
1647 int i;
1648 struct in6_addr a;
1649 char as[INET6_ADDRSTRLEN + sizeof("/128")];
1650
1651 #define hex(x) \
1652 (((x) <= '9') ? ((x) - '0') : \
1653 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1654 ((x) - 'a' + 10)))
1655
1656 for (i = 0; i < 16; i++, s += 2)
1657 a.s6_addr[i] = (16 * hex(*s)) + hex(*(s+1));
1658
1659 inet_ntop(AF_INET6, &a, as, sizeof(as));
1660
1661 if (m)
1662 sprintf(as + strlen(as), "/%lu", strtoul(m, NULL, 16));
1663
1664 blobmsg_add_string(&buf, name, as);
1665 }
1666
1667 static int
1668 rpc_luci2_network_routes6(struct ubus_context *ctx, struct ubus_object *obj,
1669 struct ubus_request_data *req, const char *method,
1670 struct blob_attr *msg)
1671 {
1672 FILE *routes;
1673 void *c, *d;
1674 char *src, *smask, *dst, *dmask, *next, *metric, *flags, *device;
1675 char line[256];
1676 unsigned int n;
1677
1678 if (!(routes = fopen("/proc/net/ipv6_route", "r")))
1679 return rpc_errno_status();
1680
1681 blob_buf_init(&buf, 0);
1682 c = blobmsg_open_array(&buf, "routes");
1683
1684 while (fgets(line, sizeof(line) - 1, routes))
1685 {
1686 dst = strtok(line, " ");
1687 dmask = strtok(NULL, " ");
1688 src = strtok(NULL, " ");
1689 smask = strtok(NULL, " ");
1690 next = strtok(NULL, " ");
1691 metric = strtok(NULL, " ");
1692
1693 strtok(NULL, " "); /* refcount */
1694 strtok(NULL, " "); /* usecount */
1695
1696 flags = strtok(NULL, " ");
1697 device = strtok(NULL, " \n");
1698
1699 if (!device)
1700 continue;
1701
1702 n = strtoul(flags, NULL, 16);
1703
1704 if (!(n & 1))
1705 continue;
1706
1707 d = blobmsg_open_table(&buf, NULL);
1708
1709 put_hex6addr("target", dst, dmask);
1710 put_hex6addr("source", src, smask);
1711 put_hex6addr("nexthop", next, NULL);
1712
1713 n = strtoul(metric, NULL, 16);
1714 blobmsg_add_u32(&buf, "metric", n);
1715
1716 blobmsg_add_string(&buf, "device", device);
1717
1718 blobmsg_close_table(&buf, d);
1719 }
1720
1721 blobmsg_close_array(&buf, c);
1722 fclose(routes);
1723
1724 ubus_send_reply(ctx, req, buf.head);
1725 return 0;
1726 }
1727
1728
1729 struct opkg_state {
1730 int cur_offset;
1731 int cur_count;
1732 int req_offset;
1733 int req_count;
1734 int total;
1735 bool open;
1736 void *array;
1737 };
1738
1739 static int
1740 opkg_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1741 {
1742 struct opkg_state *s = priv;
1743
1744 char *ptr, *last;
1745 char *nl = strchr(buf, '\n');
1746 char *name = NULL, *vers = NULL, *desc = NULL;
1747 void *c;
1748
1749 if (!nl)
1750 return 0;
1751
1752 s->total++;
1753
1754 if (s->cur_offset++ < s->req_offset)
1755 goto skip;
1756
1757 if (s->cur_count++ >= s->req_count)
1758 goto skip;
1759
1760 if (!s->open)
1761 {
1762 s->open = true;
1763 s->array = blobmsg_open_array(blob, "packages");
1764 }
1765
1766 for (ptr = buf, last = buf, *nl = 0; ptr <= nl; ptr++)
1767 {
1768 if (!*ptr || (*ptr == ' ' && *(ptr+1) == '-' && *(ptr+2) == ' '))
1769 {
1770 if (!name)
1771 {
1772 name = last;
1773 last = ptr + 3;
1774 *ptr = 0;
1775 ptr += 2;
1776 }
1777 else if (!vers)
1778 {
1779 vers = last;
1780 desc = *ptr ? (ptr + 3) : NULL;
1781 *ptr = 0;
1782 break;
1783 }
1784 }
1785 }
1786
1787 if (name && vers)
1788 {
1789 c = blobmsg_open_array(blob, NULL);
1790
1791 blobmsg_add_string(blob, NULL, name);
1792 blobmsg_add_string(blob, NULL, vers);
1793
1794 if (desc && *desc)
1795 blobmsg_add_string(blob, NULL, desc);
1796
1797 blobmsg_close_array(blob, c);
1798 }
1799
1800 skip:
1801 return (nl - buf + 1);
1802 }
1803
1804 static int
1805 opkg_finish_list(struct blob_buf *blob, int status, void *priv)
1806 {
1807 struct opkg_state *s = priv;
1808
1809 if (!s->open)
1810 return UBUS_STATUS_NO_DATA;
1811
1812 blobmsg_close_array(blob, s->array);
1813 blobmsg_add_u32(blob, "total", s->total);
1814
1815 return UBUS_STATUS_OK;
1816 }
1817
1818 static int
1819 opkg_exec_list(const char *action, struct blob_attr *msg,
1820 struct ubus_context *ctx, struct ubus_request_data *req)
1821 {
1822 struct opkg_state *state = NULL;
1823 struct blob_attr *tb[__RPC_OM_MAX];
1824 const char *cmd[5] = { "opkg", action, "-nocase", NULL, NULL };
1825
1826 blobmsg_parse(rpc_opkg_match_policy, __RPC_OM_MAX, tb,
1827 blob_data(msg), blob_len(msg));
1828
1829 state = malloc(sizeof(*state));
1830
1831 if (!state)
1832 return UBUS_STATUS_UNKNOWN_ERROR;
1833
1834 memset(state, 0, sizeof(*state));
1835
1836 if (tb[RPC_OM_PATTERN])
1837 cmd[3] = blobmsg_data(tb[RPC_OM_PATTERN]);
1838
1839 if (tb[RPC_OM_LIMIT])
1840 state->req_count = blobmsg_get_u32(tb[RPC_OM_LIMIT]);
1841
1842 if (tb[RPC_OM_OFFSET])
1843 state->req_offset = blobmsg_get_u32(tb[RPC_OM_OFFSET]);
1844
1845 if (state->req_offset < 0)
1846 state->req_offset = 0;
1847
1848 if (state->req_count <= 0 || state->req_count > 100)
1849 state->req_count = 100;
1850
1851 return rpc_exec(cmd, NULL, opkg_parse_list, NULL, opkg_finish_list,
1852 state, ctx, req);
1853 }
1854
1855
1856 static int
1857 rpc_luci2_opkg_list(struct ubus_context *ctx, struct ubus_object *obj,
1858 struct ubus_request_data *req, const char *method,
1859 struct blob_attr *msg)
1860 {
1861 return opkg_exec_list("list", msg, ctx, req);
1862 }
1863
1864 static int
1865 rpc_luci2_opkg_list_installed(struct ubus_context *ctx, struct ubus_object *obj,
1866 struct ubus_request_data *req, const char *method,
1867 struct blob_attr *msg)
1868 {
1869 return opkg_exec_list("list-installed", msg, ctx, req);
1870 }
1871
1872 static int
1873 rpc_luci2_opkg_find(struct ubus_context *ctx, struct ubus_object *obj,
1874 struct ubus_request_data *req, const char *method,
1875 struct blob_attr *msg)
1876 {
1877 return opkg_exec_list("find", msg, ctx, req);
1878 }
1879
1880 static int
1881 rpc_luci2_opkg_update(struct ubus_context *ctx, struct ubus_object *obj,
1882 struct ubus_request_data *req, const char *method,
1883 struct blob_attr *msg)
1884 {
1885 const char *cmd[3] = { "opkg", "update", NULL };
1886 return rpc_exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
1887 }
1888
1889 static int
1890 rpc_luci2_opkg_install(struct ubus_context *ctx, struct ubus_object *obj,
1891 struct ubus_request_data *req, const char *method,
1892 struct blob_attr *msg)
1893 {
1894 struct blob_attr *tb[__RPC_OP_MAX];
1895 const char *cmd[5] = { "opkg", "--force-overwrite",
1896 "install", NULL, NULL };
1897
1898 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1899 blob_data(msg), blob_len(msg));
1900
1901 if (!tb[RPC_OP_PACKAGE])
1902 return UBUS_STATUS_INVALID_ARGUMENT;
1903
1904 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1905
1906 return rpc_exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
1907 }
1908
1909 static int
1910 rpc_luci2_opkg_remove(struct ubus_context *ctx, struct ubus_object *obj,
1911 struct ubus_request_data *req, const char *method,
1912 struct blob_attr *msg)
1913 {
1914 struct blob_attr *tb[__RPC_OP_MAX];
1915 const char *cmd[5] = { "opkg", "--force-removal-of-dependent-packages",
1916 "remove", NULL, NULL };
1917
1918 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1919 blob_data(msg), blob_len(msg));
1920
1921 if (!tb[RPC_OP_PACKAGE])
1922 return UBUS_STATUS_INVALID_ARGUMENT;
1923
1924 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1925
1926 return rpc_exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
1927 }
1928
1929 static int
1930 rpc_luci2_opkg_config_get(struct ubus_context *ctx, struct ubus_object *obj,
1931 struct ubus_request_data *req, const char *method,
1932 struct blob_attr *msg)
1933 {
1934 FILE *f;
1935 char conf[2048] = { 0 };
1936
1937 if (!(f = fopen("/etc/opkg.conf", "r")))
1938 return rpc_errno_status();
1939
1940 fread(conf, sizeof(conf) - 1, 1, f);
1941 fclose(f);
1942
1943 blob_buf_init(&buf, 0);
1944 blobmsg_add_string(&buf, "config", conf);
1945
1946 ubus_send_reply(ctx, req, buf.head);
1947 return 0;
1948 }
1949
1950 static int
1951 rpc_luci2_opkg_config_set(struct ubus_context *ctx, struct ubus_object *obj,
1952 struct ubus_request_data *req, const char *method,
1953 struct blob_attr *msg)
1954 {
1955 FILE *f;
1956 struct blob_attr *tb[__RPC_D_MAX];
1957
1958 blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
1959 blob_data(msg), blob_len(msg));
1960
1961 if (!tb[RPC_D_DATA])
1962 return UBUS_STATUS_INVALID_ARGUMENT;
1963
1964 if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048)
1965 return UBUS_STATUS_NOT_SUPPORTED;
1966
1967 if (!(f = fopen("/etc/opkg.conf", "w")))
1968 return rpc_errno_status();
1969
1970 fwrite(blobmsg_data(tb[RPC_D_DATA]),
1971 blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
1972
1973 fclose(f);
1974 return 0;
1975 }
1976
1977
1978 static bool
1979 menu_access(struct blob_attr *sid, struct blob_attr *acls, struct blob_buf *e)
1980 {
1981 int rem;
1982 struct blob_attr *acl;
1983 bool rv = true;
1984 void *c;
1985
1986 c = blobmsg_open_table(e, "write");
1987
1988 blobmsg_for_each_attr(acl, acls, rem)
1989 {
1990 if (!rpc_session_access(blobmsg_data(sid), "luci-ui",
1991 blobmsg_data(acl), "read"))
1992 {
1993 rv = false;
1994 break;
1995 }
1996
1997 blobmsg_add_u8(e, blobmsg_data(acl),
1998 rpc_session_access(blobmsg_data(sid), "luci-ui",
1999 blobmsg_data(acl), "write"));
2000 }
2001
2002 blobmsg_close_table(e, c);
2003
2004 return rv;
2005 }
2006
2007 static int
2008 rpc_luci2_ui_menu(struct ubus_context *ctx, struct ubus_object *obj,
2009 struct ubus_request_data *req, const char *method,
2010 struct blob_attr *msg)
2011 {
2012 int i, rem, rem2;
2013 glob_t gl;
2014 struct blob_buf menu = { 0 };
2015 struct blob_buf item = { 0 };
2016 struct blob_attr *entry, *attr;
2017 struct blob_attr *tb[__RPC_MENU_MAX];
2018 bool access;
2019 void *c, *d;
2020
2021 blobmsg_parse(rpc_menu_policy, __RPC_MENU_MAX, tb,
2022 blob_data(msg), blob_len(msg));
2023
2024 if (!tb[RPC_MENU_SESSION])
2025 return UBUS_STATUS_INVALID_ARGUMENT;
2026
2027
2028 blob_buf_init(&buf, 0);
2029 c = blobmsg_open_table(&buf, "menu");
2030
2031 if (!glob(RPC_LUCI2_MENU_FILES, 0, NULL, &gl))
2032 {
2033 for (i = 0; i < gl.gl_pathc; i++)
2034 {
2035 blob_buf_init(&menu, 0);
2036
2037 if (!blobmsg_add_json_from_file(&menu, gl.gl_pathv[i]))
2038 goto skip;
2039
2040 blob_for_each_attr(entry, menu.head, rem)
2041 {
2042 access = true;
2043
2044 blob_buf_init(&item, 0);
2045 d = blobmsg_open_table(&item, blobmsg_name(entry));
2046
2047 blobmsg_for_each_attr(attr, entry, rem2)
2048 {
2049 if (blob_id(attr) == BLOBMSG_TYPE_ARRAY &&
2050 !strcmp(blobmsg_name(attr), "acls"))
2051 access = menu_access(tb[RPC_MENU_SESSION], attr, &item);
2052 else
2053 blobmsg_add_blob(&item, attr);
2054 }
2055
2056 blobmsg_close_table(&item, d);
2057
2058 if (access)
2059 blob_for_each_attr(attr, item.head, rem2)
2060 blobmsg_add_blob(&buf, attr);
2061
2062 blob_buf_free(&item);
2063 }
2064
2065 skip:
2066 blob_buf_free(&menu);
2067 }
2068
2069 globfree(&gl);
2070 }
2071
2072 blobmsg_close_table(&buf, c);
2073
2074 ubus_send_reply(ctx, req, buf.head);
2075 return 0;
2076 }
2077
2078
2079 int rpc_luci2_api_init(struct ubus_context *ctx)
2080 {
2081 int rv = 0;
2082
2083 static const struct ubus_method luci2_system_methods[] = {
2084 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log),
2085 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg),
2086 UBUS_METHOD_NOARG("diskfree", rpc_luci2_system_diskfree),
2087 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list),
2088 UBUS_METHOD("process_signal", rpc_luci2_process_signal,
2089 rpc_signal_policy),
2090 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list),
2091 UBUS_METHOD("init_action", rpc_luci2_init_action,
2092 rpc_init_policy),
2093 UBUS_METHOD_NOARG("rclocal_get", rpc_luci2_rclocal_get),
2094 UBUS_METHOD("rclocal_set", rpc_luci2_rclocal_set,
2095 rpc_data_policy),
2096 UBUS_METHOD_NOARG("crontab_get", rpc_luci2_crontab_get),
2097 UBUS_METHOD("crontab_set", rpc_luci2_crontab_set,
2098 rpc_data_policy),
2099 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get),
2100 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set,
2101 rpc_sshkey_policy),
2102 UBUS_METHOD("password_set", rpc_luci2_password_set,
2103 rpc_password_policy),
2104 UBUS_METHOD_NOARG("led_list", rpc_luci2_led_list),
2105 UBUS_METHOD_NOARG("usb_list", rpc_luci2_usb_list),
2106 UBUS_METHOD_NOARG("upgrade_test", rpc_luci2_upgrade_test),
2107 UBUS_METHOD("upgrade_start", rpc_luci2_upgrade_start,
2108 rpc_upgrade_policy),
2109 UBUS_METHOD_NOARG("upgrade_clean", rpc_luci2_upgrade_clean),
2110 UBUS_METHOD_NOARG("backup_restore", rpc_luci2_backup_restore),
2111 UBUS_METHOD_NOARG("backup_clean", rpc_luci2_backup_clean),
2112 UBUS_METHOD_NOARG("backup_config_get", rpc_luci2_backup_config_get),
2113 UBUS_METHOD("backup_config_set", rpc_luci2_backup_config_set,
2114 rpc_data_policy),
2115 UBUS_METHOD_NOARG("backup_list", rpc_luci2_backup_list),
2116 UBUS_METHOD_NOARG("reset_test", rpc_luci2_reset_test),
2117 UBUS_METHOD_NOARG("reset_start", rpc_luci2_reset_start),
2118 UBUS_METHOD_NOARG("reboot", rpc_luci2_reboot)
2119 };
2120
2121 static struct ubus_object_type luci2_system_type =
2122 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods);
2123
2124 static struct ubus_object system_obj = {
2125 .name = "luci2.system",
2126 .type = &luci2_system_type,
2127 .methods = luci2_system_methods,
2128 .n_methods = ARRAY_SIZE(luci2_system_methods),
2129 };
2130
2131
2132 static const struct ubus_method luci2_network_methods[] = {
2133 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count),
2134 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table),
2135 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table),
2136 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases),
2137 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6),
2138 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes),
2139 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6),
2140 };
2141
2142 static struct ubus_object_type luci2_network_type =
2143 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods);
2144
2145 static struct ubus_object network_obj = {
2146 .name = "luci2.network",
2147 .type = &luci2_network_type,
2148 .methods = luci2_network_methods,
2149 .n_methods = ARRAY_SIZE(luci2_network_methods),
2150 };
2151
2152
2153 static const struct ubus_method luci2_opkg_methods[] = {
2154 UBUS_METHOD("list", rpc_luci2_opkg_list,
2155 rpc_opkg_match_policy),
2156 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed,
2157 rpc_opkg_match_policy),
2158 UBUS_METHOD("find", rpc_luci2_opkg_find,
2159 rpc_opkg_match_policy),
2160 UBUS_METHOD("install", rpc_luci2_opkg_install,
2161 rpc_opkg_package_policy),
2162 UBUS_METHOD("remove", rpc_luci2_opkg_remove,
2163 rpc_opkg_package_policy),
2164 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update),
2165 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get),
2166 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set,
2167 rpc_data_policy)
2168 };
2169
2170 static struct ubus_object_type luci2_opkg_type =
2171 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods);
2172
2173 static struct ubus_object opkg_obj = {
2174 .name = "luci2.opkg",
2175 .type = &luci2_opkg_type,
2176 .methods = luci2_opkg_methods,
2177 .n_methods = ARRAY_SIZE(luci2_opkg_methods),
2178 };
2179
2180
2181 static const struct ubus_method luci2_ui_methods[] = {
2182 UBUS_METHOD_NOARG("menu", rpc_luci2_ui_menu)
2183 };
2184
2185 static struct ubus_object_type luci2_ui_type =
2186 UBUS_OBJECT_TYPE("luci-rpc-luci2-ui", luci2_ui_methods);
2187
2188 static struct ubus_object ui_obj = {
2189 .name = "luci2.ui",
2190 .type = &luci2_ui_type,
2191 .methods = luci2_ui_methods,
2192 .n_methods = ARRAY_SIZE(luci2_ui_methods),
2193 };
2194
2195 cursor = uci_alloc_context();
2196
2197 if (!cursor)
2198 return UBUS_STATUS_UNKNOWN_ERROR;
2199
2200 rv |= ubus_add_object(ctx, &system_obj);
2201 rv |= ubus_add_object(ctx, &network_obj);
2202 rv |= ubus_add_object(ctx, &opkg_obj);
2203 rv |= ubus_add_object(ctx, &ui_obj);
2204
2205 return rv;
2206 }