luci2: change luci2.ui.menu call to return acl levels for each menu item
[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, 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, 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 void
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;
1092
1093         blobmsg_close_array(blob, s->array);
1094 }
1095
1096 static int
1097 rpc_luci2_backup_list(struct ubus_context *ctx, struct ubus_object *obj,
1098                       struct ubus_request_data *req, const char *method,
1099                       struct blob_attr *msg)
1100 {
1101         struct backup_state *state = NULL;
1102         const char *cmd[3] = { "sysupgrade", "--list-backup", NULL };
1103
1104         state = malloc(sizeof(*state));
1105
1106         if (!state)
1107                 return UBUS_STATUS_UNKNOWN_ERROR;
1108
1109         memset(state, 0, sizeof(*state));
1110
1111         return rpc_exec(cmd, backup_parse_list, NULL, backup_finish_list,
1112                         state, ctx, req);
1113 }
1114
1115 static int
1116 rpc_luci2_reset_test(struct ubus_context *ctx, struct ubus_object *obj,
1117                      struct ubus_request_data *req, const char *method,
1118                      struct blob_attr *msg)
1119 {
1120         FILE *mtd;
1121         struct stat s;
1122         char line[64] = { 0 };
1123         bool supported = false;
1124
1125         if (!stat("/sbin/mtd", &s) && (s.st_mode & S_IXUSR))
1126         {
1127                 if ((mtd = fopen("/proc/mtd", "r")) != NULL)
1128                 {
1129                         while (fgets(line, sizeof(line) - 1, mtd))
1130                         {
1131                                 if (strstr(line, "\"rootfs_data\""))
1132                                 {
1133                                         supported = true;
1134                                         break;
1135                                 }
1136                         }
1137
1138                         fclose(mtd);
1139                 }
1140         }
1141
1142         blob_buf_init(&buf, 0);
1143         blobmsg_add_u8(&buf, "supported", supported);
1144
1145         ubus_send_reply(ctx, req, buf.head);
1146
1147         return 0;
1148 }
1149
1150 static int
1151 rpc_luci2_reset_start(struct ubus_context *ctx, struct ubus_object *obj,
1152                       struct ubus_request_data *req, const char *method,
1153                       struct blob_attr *msg)
1154 {
1155         switch (fork())
1156         {
1157         case -1:
1158                 return rpc_errno_status();
1159
1160         case 0:
1161                 uloop_done();
1162
1163                 chdir("/");
1164
1165                 close(0);
1166                 close(1);
1167                 close(2);
1168
1169                 sleep(1);
1170
1171                 execl("/sbin/mtd", "/sbin/mtd", "-r", "erase", "rootfs_data", NULL);
1172
1173                 return rpc_errno_status();
1174
1175         default:
1176                 return 0;
1177         }
1178 }
1179
1180 static int
1181 rpc_luci2_reboot(struct ubus_context *ctx, struct ubus_object *obj,
1182                  struct ubus_request_data *req, const char *method,
1183                  struct blob_attr *msg)
1184 {
1185         switch (fork())
1186         {
1187         case -1:
1188                 return rpc_errno_status();
1189
1190         case 0:
1191                 chdir("/");
1192
1193                 close(0);
1194                 close(1);
1195                 close(2);
1196
1197                 sleep(1);
1198
1199                 execl("/sbin/reboot", "/sbin/reboot", NULL);
1200
1201                 return rpc_errno_status();
1202
1203         default:
1204                 return 0;
1205         }
1206 }
1207
1208
1209 static FILE *
1210 dnsmasq_leasefile(void)
1211 {
1212         FILE *leases = NULL;
1213         struct uci_package *p;
1214         struct uci_element *e;
1215         struct uci_section *s;
1216         struct uci_ptr ptr = {
1217                 .package = "dhcp",
1218                 .section = NULL,
1219                 .option  = "leasefile"
1220         };
1221
1222         uci_load(cursor, ptr.package, &p);
1223
1224         if (!p)
1225                 return NULL;
1226
1227         uci_foreach_element(&p->sections, e)
1228         {
1229                 s = uci_to_section(e);
1230
1231                 if (strcmp(s->type, "dnsmasq"))
1232                         continue;
1233
1234                 ptr.section = e->name;
1235                 uci_lookup_ptr(cursor, &ptr, NULL, true);
1236                 break;
1237         }
1238
1239         if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
1240                 leases = fopen(ptr.o->v.string, "r");
1241
1242         uci_unload(cursor, p);
1243
1244         return leases;
1245 }
1246
1247 static int
1248 rpc_luci2_network_leases(struct ubus_context *ctx, struct ubus_object *obj,
1249                          struct ubus_request_data *req, const char *method,
1250                          struct blob_attr *msg)
1251 {
1252         FILE *leases;
1253         void *c, *d;
1254         char line[128];
1255         char *ts, *mac, *addr, *name;
1256         time_t now = time(NULL);
1257
1258         blob_buf_init(&buf, 0);
1259         c = blobmsg_open_array(&buf, "leases");
1260
1261         leases = dnsmasq_leasefile();
1262
1263         if (!leases)
1264                 goto out;
1265
1266         while (fgets(line, sizeof(line) - 1, leases))
1267         {
1268                 ts   = strtok(line, " \t");
1269                 mac  = strtok(NULL, " \t");
1270                 addr = strtok(NULL, " \t");
1271                 name = strtok(NULL, " \t");
1272
1273                 if (!ts || !mac || !addr || !name)
1274                         continue;
1275
1276                 if (strchr(addr, ':'))
1277                         continue;
1278
1279                 d = blobmsg_open_table(&buf, NULL);
1280
1281                 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1282                 blobmsg_add_string(&buf, "macaddr", mac);
1283                 blobmsg_add_string(&buf, "ipaddr", addr);
1284
1285                 if (strcmp(name, "*"))
1286                         blobmsg_add_string(&buf, "hostname", name);
1287
1288                 blobmsg_close_table(&buf, d);
1289         }
1290
1291         fclose(leases);
1292
1293 out:
1294         blobmsg_close_array(&buf, c);
1295         ubus_send_reply(ctx, req, buf.head);
1296
1297         return 0;
1298 }
1299
1300 static int
1301 rpc_luci2_network_leases6(struct ubus_context *ctx, struct ubus_object *obj,
1302                           struct ubus_request_data *req, const char *method,
1303                           struct blob_attr *msg)
1304 {
1305         FILE *leases;
1306         void *c, *d;
1307         char line[128];
1308         char *ts, *mac, *addr, *name, *duid;
1309         time_t now = time(NULL);
1310
1311         blob_buf_init(&buf, 0);
1312         c = blobmsg_open_array(&buf, "leases");
1313
1314         leases = fopen("/tmp/hosts/6relayd", "r");
1315
1316         if (leases)
1317         {
1318                 while (fgets(line, sizeof(line) - 1, leases))
1319                 {
1320                         if (strncmp(line, "# ", 2))
1321                                 continue;
1322
1323                         strtok(line + 2, " \t"); /* iface */
1324
1325                         duid = strtok(NULL, " \t");
1326
1327                         strtok(NULL, " \t"); /* iaid */
1328
1329                         name = strtok(NULL, " \t");
1330                         ts   = strtok(NULL, " \t");
1331
1332                         strtok(NULL, " \t"); /* id */
1333                         strtok(NULL, " \t"); /* length */
1334
1335                         addr = strtok(NULL, " \t\n");
1336
1337                         if (!addr)
1338                                 continue;
1339
1340                         d = blobmsg_open_table(&buf, NULL);
1341
1342                         blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1343                         blobmsg_add_string(&buf, "duid", duid);
1344                         blobmsg_add_string(&buf, "ip6addr", addr);
1345
1346                         if (strcmp(name, "-"))
1347                                 blobmsg_add_string(&buf, "hostname", name);
1348
1349                         blobmsg_close_array(&buf, d);
1350                 }
1351
1352                 fclose(leases);
1353         }
1354         else
1355         {
1356                 leases = dnsmasq_leasefile();
1357
1358                 if (!leases)
1359                         goto out;
1360
1361                 while (fgets(line, sizeof(line) - 1, leases))
1362                 {
1363                         ts   = strtok(line, " \t");
1364                         mac  = strtok(NULL, " \t");
1365                         addr = strtok(NULL, " \t");
1366                         name = strtok(NULL, " \t");
1367                         duid = strtok(NULL, " \t\n");
1368
1369                         if (!ts || !mac || !addr || !duid)
1370                                 continue;
1371
1372                         if (!strchr(addr, ':'))
1373                                 continue;
1374
1375                         d = blobmsg_open_table(&buf, NULL);
1376
1377                         blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
1378                         blobmsg_add_string(&buf, "macaddr", mac);
1379                         blobmsg_add_string(&buf, "ip6addr", addr);
1380
1381                         if (strcmp(name, "*"))
1382                                 blobmsg_add_string(&buf, "hostname", name);
1383
1384                         if (strcmp(duid, "*"))
1385                                 blobmsg_add_string(&buf, "duid", name);
1386
1387                         blobmsg_close_table(&buf, d);
1388                 }
1389
1390                 fclose(leases);
1391         }
1392
1393 out:
1394         blobmsg_close_array(&buf, c);
1395         ubus_send_reply(ctx, req, buf.head);
1396
1397         return 0;
1398 }
1399
1400 static int
1401 rpc_luci2_network_ct_count(struct ubus_context *ctx, struct ubus_object *obj,
1402                            struct ubus_request_data *req, const char *method,
1403                            struct blob_attr *msg)
1404 {
1405         FILE *f;
1406         char line[128];
1407
1408         blob_buf_init(&buf, 0);
1409
1410         if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL)
1411         {
1412                 if (fgets(line, sizeof(line) - 1, f))
1413                         blobmsg_add_u32(&buf, "count", atoi(line));
1414
1415                 fclose(f);
1416         }
1417
1418         if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL)
1419         {
1420                 if (fgets(line, sizeof(line) - 1, f))
1421                         blobmsg_add_u32(&buf, "limit", atoi(line));
1422
1423                 fclose(f);
1424         }
1425
1426         ubus_send_reply(ctx, req, buf.head);
1427
1428         return 0;
1429 }
1430
1431 static int
1432 rpc_luci2_network_ct_table(struct ubus_context *ctx, struct ubus_object *obj,
1433                            struct ubus_request_data *req, const char *method,
1434                            struct blob_attr *msg)
1435 {
1436         FILE *f;
1437         int i;
1438         void *c, *d;
1439         char *p, line[512];
1440         bool seen[6];
1441
1442         blob_buf_init(&buf, 0);
1443         c = blobmsg_open_array(&buf, "entries");
1444
1445         if ((f = fopen("/proc/net/nf_conntrack", "r")) != NULL)
1446         {
1447                 while (fgets(line, sizeof(line) - 1, f))
1448                 {
1449                         d = blobmsg_open_table(&buf, NULL);
1450                         memset(seen, 0, sizeof(seen));
1451
1452                         for (i = 0, p = strtok(line, " "); p; i++, p = strtok(NULL, " "))
1453                         {
1454                                 if (i == 0)
1455                                         blobmsg_add_u8(&buf, "ipv6", !strcmp(p, "ipv6"));
1456                                 else if (i == 3)
1457                                         blobmsg_add_u32(&buf, "protocol", atoi(p));
1458                                 else if (i == 4)
1459                                         blobmsg_add_u32(&buf, "expires", atoi(p));
1460                                 else if (i >= 5)
1461                                 {
1462                                         if (*p == '[')
1463                                                 continue;
1464
1465                                         if (!seen[0] && !strncmp(p, "src=", 4))
1466                                         {
1467                                                 blobmsg_add_string(&buf, "src", p + 4);
1468                                                 seen[0] = true;
1469                                         }
1470                                         else if (!seen[1] && !strncmp(p, "dst=", 4))
1471                                         {
1472                                                 blobmsg_add_string(&buf, "dest", p + 4);
1473                                                 seen[1] = true;
1474                                         }
1475                                         else if (!seen[2] && !strncmp(p, "sport=", 6))
1476                                         {
1477                                                 blobmsg_add_u32(&buf, "sport", atoi(p + 6));
1478                                                 seen[2] = true;
1479                                         }
1480                                         else if (!seen[3] && !strncmp(p, "dport=", 6))
1481                                         {
1482                                                 blobmsg_add_u32(&buf, "dport", atoi(p + 6));
1483                                                 seen[3] = true;
1484                                         }
1485                                         else if (!strncmp(p, "packets=", 8))
1486                                         {
1487                                                 blobmsg_add_u32(&buf,
1488                                                                 seen[4] ? "tx_packets" : "rx_packets",
1489                                                                 atoi(p + 8));
1490                                                 seen[4] = true;
1491                                         }
1492                                         else if (!strncmp(p, "bytes=", 6))
1493                                         {
1494                                                 blobmsg_add_u32(&buf,
1495                                                                                 seen[5] ? "tx_bytes" : "rx_bytes",
1496                                                                 atoi(p + 6));
1497                                                 seen[5] = true;
1498                                         }
1499                                 }
1500                         }
1501
1502                         blobmsg_close_table(&buf, d);
1503                 }
1504
1505                 fclose(f);
1506         }
1507
1508         blobmsg_close_array(&buf, c);
1509         ubus_send_reply(ctx, req, buf.head);
1510
1511         return 0;
1512 }
1513
1514 static int
1515 rpc_luci2_network_arp_table(struct ubus_context *ctx, struct ubus_object *obj,
1516                             struct ubus_request_data *req, const char *method,
1517                             struct blob_attr *msg)
1518 {
1519         FILE *f;
1520         void *c, *d;
1521         char *addr, *mac, *dev, line[128];
1522
1523         blob_buf_init(&buf, 0);
1524         c = blobmsg_open_array(&buf, "entries");
1525
1526         if ((f = fopen("/proc/net/arp", "r")) != NULL)
1527         {
1528                 /* skip header line */
1529                 fgets(line, sizeof(line) - 1, f);
1530
1531                 while (fgets(line, sizeof(line) - 1, f))
1532                 {
1533                         addr = strtok(line, " \t");
1534
1535                         strtok(NULL, " \t"); /* HW type */
1536                         strtok(NULL, " \t"); /* Flags */
1537
1538                         mac = strtok(NULL, " \t");
1539
1540                         strtok(NULL, " \t"); /* Mask */
1541
1542                         dev = strtok(NULL, " \t\n");
1543
1544                         if (!dev)
1545                                 continue;
1546
1547                         d = blobmsg_open_table(&buf, NULL);
1548                         blobmsg_add_string(&buf, "ipaddr", addr);
1549                         blobmsg_add_string(&buf, "macaddr", mac);
1550                         blobmsg_add_string(&buf, "device", dev);
1551                         blobmsg_close_table(&buf, d);
1552                 }
1553
1554                 fclose(f);
1555         }
1556
1557         blobmsg_close_array(&buf, c);
1558         ubus_send_reply(ctx, req, buf.head);
1559
1560         return 0;
1561 }
1562
1563 static void
1564 put_hexaddr(const char *name, const char *s, const char *m)
1565 {
1566         int bits;
1567         struct in_addr a;
1568         char as[sizeof("255.255.255.255/32\0")];
1569
1570         a.s_addr = strtoul(s, NULL, 16);
1571         inet_ntop(AF_INET, &a, as, sizeof(as));
1572
1573         if (m)
1574         {
1575                 for (a.s_addr = ntohl(strtoul(m, NULL, 16)), bits = 0;
1576                      a.s_addr & 0x80000000;
1577                      a.s_addr <<= 1)
1578                         bits++;
1579
1580                 sprintf(as + strlen(as), "/%u", bits);
1581         }
1582
1583         blobmsg_add_string(&buf, name, as);
1584 }
1585
1586 static int
1587 rpc_luci2_network_routes(struct ubus_context *ctx, struct ubus_object *obj,
1588                          struct ubus_request_data *req, const char *method,
1589                          struct blob_attr *msg)
1590 {
1591         FILE *routes;
1592         void *c, *d;
1593         char *dst, *dmask, *next, *metric, *device;
1594         char line[256];
1595         unsigned int n;
1596
1597         if (!(routes = fopen("/proc/net/route", "r")))
1598                 return rpc_errno_status();
1599
1600         blob_buf_init(&buf, 0);
1601         c = blobmsg_open_array(&buf, "routes");
1602
1603         /* skip header line */
1604         fgets(line, sizeof(line) - 1, routes);
1605
1606         while (fgets(line, sizeof(line) - 1, routes))
1607         {
1608                 device = strtok(line, "\t ");
1609                 dst    = strtok(NULL, "\t ");
1610                 next   = strtok(NULL, "\t ");
1611
1612                 strtok(NULL, "\t "); /* flags */
1613                 strtok(NULL, "\t "); /* refcount */
1614                 strtok(NULL, "\t "); /* usecount */
1615
1616                 metric = strtok(NULL, "\t ");
1617                 dmask  = strtok(NULL, "\t ");
1618
1619                 if (!dmask)
1620                         continue;
1621
1622                 d = blobmsg_open_table(&buf, NULL);
1623
1624                 put_hexaddr("target", dst, dmask);
1625                 put_hexaddr("nexthop", next, NULL);
1626
1627                 n = strtoul(metric, NULL, 10);
1628                 blobmsg_add_u32(&buf, "metric", n);
1629
1630                 blobmsg_add_string(&buf, "device", device);
1631
1632                 blobmsg_close_table(&buf, d);
1633         }
1634
1635         blobmsg_close_array(&buf, c);
1636         fclose(routes);
1637
1638         ubus_send_reply(ctx, req, buf.head);
1639         return 0;
1640 }
1641
1642 static void
1643 put_hex6addr(const char *name, const char *s, const char *m)
1644 {
1645         int i;
1646         struct in6_addr a;
1647         char as[INET6_ADDRSTRLEN + sizeof("/128")];
1648
1649 #define hex(x) \
1650         (((x) <= '9') ? ((x) - '0') : \
1651                 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1652                         ((x) - 'a' + 10)))
1653
1654         for (i = 0; i < 16; i++, s += 2)
1655                 a.s6_addr[i] = (16 * hex(*s)) + hex(*(s+1));
1656
1657         inet_ntop(AF_INET6, &a, as, sizeof(as));
1658
1659         if (m)
1660                 sprintf(as + strlen(as), "/%lu", strtoul(m, NULL, 16));
1661
1662         blobmsg_add_string(&buf, name, as);
1663 }
1664
1665 static int
1666 rpc_luci2_network_routes6(struct ubus_context *ctx, struct ubus_object *obj,
1667                           struct ubus_request_data *req, const char *method,
1668                           struct blob_attr *msg)
1669 {
1670         FILE *routes;
1671         void *c, *d;
1672         char *src, *smask, *dst, *dmask, *next, *metric, *flags, *device;
1673         char line[256];
1674         unsigned int n;
1675
1676         if (!(routes = fopen("/proc/net/ipv6_route", "r")))
1677                 return rpc_errno_status();
1678
1679         blob_buf_init(&buf, 0);
1680         c = blobmsg_open_array(&buf, "routes");
1681
1682         while (fgets(line, sizeof(line) - 1, routes))
1683         {
1684                 dst    = strtok(line, " ");
1685                 dmask  = strtok(NULL, " ");
1686                 src    = strtok(NULL, " ");
1687                 smask  = strtok(NULL, " ");
1688                 next   = strtok(NULL, " ");
1689                 metric = strtok(NULL, " ");
1690
1691                 strtok(NULL, " "); /* refcount */
1692                 strtok(NULL, " "); /* usecount */
1693
1694                 flags  = strtok(NULL, " ");
1695                 device = strtok(NULL, " \n");
1696
1697                 if (!device)
1698                         continue;
1699
1700                 n = strtoul(flags, NULL, 16);
1701
1702                 if (!(n & 1))
1703                         continue;
1704
1705                 d = blobmsg_open_table(&buf, NULL);
1706
1707                 put_hex6addr("target", dst, dmask);
1708                 put_hex6addr("source", src, smask);
1709                 put_hex6addr("nexthop", next, NULL);
1710
1711                 n = strtoul(metric, NULL, 16);
1712                 blobmsg_add_u32(&buf, "metric", n);
1713
1714                 blobmsg_add_string(&buf, "device", device);
1715
1716                 blobmsg_close_table(&buf, d);
1717         }
1718
1719         blobmsg_close_array(&buf, c);
1720         fclose(routes);
1721
1722         ubus_send_reply(ctx, req, buf.head);
1723         return 0;
1724 }
1725
1726
1727 struct opkg_state {
1728         int cur_offset;
1729         int cur_count;
1730         int req_offset;
1731         int req_count;
1732         int total;
1733         bool open;
1734         void *array;
1735 };
1736
1737 static int
1738 opkg_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1739 {
1740         struct opkg_state *s = priv;
1741
1742         char *ptr, *last;
1743         char *nl = strchr(buf, '\n');
1744         char *name = NULL, *vers = NULL, *desc = NULL;
1745         void *c;
1746
1747         if (!nl)
1748                 return 0;
1749
1750         s->total++;
1751
1752         if (s->cur_offset++ < s->req_offset)
1753                 goto skip;
1754
1755         if (s->cur_count++ >= s->req_count)
1756                 goto skip;
1757
1758         if (!s->open)
1759         {
1760                 s->open  = true;
1761                 s->array = blobmsg_open_array(blob, "packages");
1762         }
1763
1764         for (ptr = buf, last = buf, *nl = 0; ptr <= nl; ptr++)
1765         {
1766                 if (!*ptr || (*ptr == ' ' && *(ptr+1) == '-' && *(ptr+2) == ' '))
1767                 {
1768                         if (!name)
1769                         {
1770                                 name = last;
1771                                 last = ptr + 3;
1772                                 *ptr = 0;
1773                                 ptr += 2;
1774                         }
1775                         else if (!vers)
1776                         {
1777                                 vers = last;
1778                                 desc = *ptr ? (ptr + 3) : NULL;
1779                                 *ptr = 0;
1780                                 break;
1781                         }
1782                 }
1783         }
1784
1785         if (name && vers)
1786         {
1787                 c = blobmsg_open_array(blob, NULL);
1788
1789                 blobmsg_add_string(blob, NULL, name);
1790                 blobmsg_add_string(blob, NULL, vers);
1791
1792                 if (desc && *desc)
1793                         blobmsg_add_string(blob, NULL, desc);
1794
1795                 blobmsg_close_array(blob, c);
1796         }
1797
1798 skip:
1799         return (nl - buf + 1);
1800 }
1801
1802 static void
1803 opkg_finish_list(struct blob_buf *blob, int status, void *priv)
1804 {
1805         struct opkg_state *s = priv;
1806
1807         if (!s->open)
1808                 return;
1809
1810         blobmsg_close_array(blob, s->array);
1811         blobmsg_add_u32(blob, "total", s->total);
1812 }
1813
1814 static int
1815 opkg_exec_list(const char *action, struct blob_attr *msg,
1816                struct ubus_context *ctx, struct ubus_request_data *req)
1817 {
1818         struct opkg_state *state = NULL;
1819         struct blob_attr *tb[__RPC_OM_MAX];
1820         const char *cmd[5] = { "opkg", action, "-nocase", NULL, NULL };
1821
1822         blobmsg_parse(rpc_opkg_match_policy, __RPC_OM_MAX, tb,
1823                       blob_data(msg), blob_len(msg));
1824
1825         state = malloc(sizeof(*state));
1826
1827         if (!state)
1828                 return UBUS_STATUS_UNKNOWN_ERROR;
1829
1830         memset(state, 0, sizeof(*state));
1831
1832         if (tb[RPC_OM_PATTERN])
1833                 cmd[3] = blobmsg_data(tb[RPC_OM_PATTERN]);
1834
1835         if (tb[RPC_OM_LIMIT])
1836                 state->req_count = blobmsg_get_u32(tb[RPC_OM_LIMIT]);
1837
1838         if (tb[RPC_OM_OFFSET])
1839                 state->req_offset = blobmsg_get_u32(tb[RPC_OM_OFFSET]);
1840
1841         if (state->req_offset < 0)
1842                 state->req_offset = 0;
1843
1844         if (state->req_count <= 0 || state->req_count > 100)
1845                 state->req_count = 100;
1846
1847         return rpc_exec(cmd, opkg_parse_list, NULL, opkg_finish_list,
1848                         state, ctx, req);
1849 }
1850
1851
1852 static int
1853 rpc_luci2_opkg_list(struct ubus_context *ctx, struct ubus_object *obj,
1854                     struct ubus_request_data *req, const char *method,
1855                     struct blob_attr *msg)
1856 {
1857         return opkg_exec_list("list", msg, ctx, req);
1858 }
1859
1860 static int
1861 rpc_luci2_opkg_list_installed(struct ubus_context *ctx, struct ubus_object *obj,
1862                               struct ubus_request_data *req, const char *method,
1863                               struct blob_attr *msg)
1864 {
1865         return opkg_exec_list("list-installed", msg, ctx, req);
1866 }
1867
1868 static int
1869 rpc_luci2_opkg_find(struct ubus_context *ctx, struct ubus_object *obj,
1870                     struct ubus_request_data *req, const char *method,
1871                     struct blob_attr *msg)
1872 {
1873         return opkg_exec_list("find", msg, ctx, req);
1874 }
1875
1876 static int
1877 rpc_luci2_opkg_update(struct ubus_context *ctx, struct ubus_object *obj,
1878                       struct ubus_request_data *req, const char *method,
1879                       struct blob_attr *msg)
1880 {
1881         const char *cmd[3] = { "opkg", "update", NULL };
1882         return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1883 }
1884
1885 static int
1886 rpc_luci2_opkg_install(struct ubus_context *ctx, struct ubus_object *obj,
1887                        struct ubus_request_data *req, const char *method,
1888                        struct blob_attr *msg)
1889 {
1890         struct blob_attr *tb[__RPC_OP_MAX];
1891         const char *cmd[5] = { "opkg", "--force-overwrite",
1892                                "install", NULL, NULL };
1893
1894         blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1895                       blob_data(msg), blob_len(msg));
1896
1897         if (!tb[RPC_OP_PACKAGE])
1898                 return UBUS_STATUS_INVALID_ARGUMENT;
1899
1900         cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1901
1902         return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1903 }
1904
1905 static int
1906 rpc_luci2_opkg_remove(struct ubus_context *ctx, struct ubus_object *obj,
1907                       struct ubus_request_data *req, const char *method,
1908                       struct blob_attr *msg)
1909 {
1910         struct blob_attr *tb[__RPC_OP_MAX];
1911         const char *cmd[5] = { "opkg", "--force-removal-of-dependent-packages",
1912                                "remove", NULL, NULL };
1913
1914         blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1915                       blob_data(msg), blob_len(msg));
1916
1917         if (!tb[RPC_OP_PACKAGE])
1918                 return UBUS_STATUS_INVALID_ARGUMENT;
1919
1920         cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1921
1922         return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1923 }
1924
1925 static int
1926 rpc_luci2_opkg_config_get(struct ubus_context *ctx, struct ubus_object *obj,
1927                           struct ubus_request_data *req, const char *method,
1928                           struct blob_attr *msg)
1929 {
1930         FILE *f;
1931         char conf[2048] = { 0 };
1932
1933         if (!(f = fopen("/etc/opkg.conf", "r")))
1934                 return rpc_errno_status();
1935
1936         fread(conf, sizeof(conf) - 1, 1, f);
1937         fclose(f);
1938
1939         blob_buf_init(&buf, 0);
1940         blobmsg_add_string(&buf, "config", conf);
1941
1942         ubus_send_reply(ctx, req, buf.head);
1943         return 0;
1944 }
1945
1946 static int
1947 rpc_luci2_opkg_config_set(struct ubus_context *ctx, struct ubus_object *obj,
1948                           struct ubus_request_data *req, const char *method,
1949                           struct blob_attr *msg)
1950 {
1951         FILE *f;
1952         struct blob_attr *tb[__RPC_D_MAX];
1953
1954         blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
1955                       blob_data(msg), blob_len(msg));
1956
1957         if (!tb[RPC_D_DATA])
1958                 return UBUS_STATUS_INVALID_ARGUMENT;
1959
1960         if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048)
1961                 return UBUS_STATUS_NOT_SUPPORTED;
1962
1963         if (!(f = fopen("/etc/opkg.conf", "w")))
1964                 return rpc_errno_status();
1965
1966         fwrite(blobmsg_data(tb[RPC_D_DATA]),
1967                blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
1968
1969         fclose(f);
1970         return 0;
1971 }
1972
1973
1974 static bool
1975 menu_access(struct blob_attr *sid, struct blob_attr *acls, struct blob_buf *e)
1976 {
1977         int rem;
1978         struct blob_attr *acl;
1979         bool rv = true;
1980         void *c;
1981
1982         c = blobmsg_open_table(e, "write");
1983
1984         blobmsg_for_each_attr(acl, acls, rem)
1985         {
1986                 if (!rpc_session_access(blobmsg_data(sid), "luci-ui",
1987                                         blobmsg_data(acl), "read"))
1988                 {
1989                         rv = false;
1990                         break;
1991                 }
1992
1993                 blobmsg_add_u8(e, blobmsg_data(acl),
1994                                rpc_session_access(blobmsg_data(sid), "luci-ui",
1995                                                   blobmsg_data(acl), "write"));
1996         }
1997
1998         blobmsg_close_table(e, c);
1999
2000         return rv;
2001 }
2002
2003 static int
2004 rpc_luci2_ui_menu(struct ubus_context *ctx, struct ubus_object *obj,
2005                   struct ubus_request_data *req, const char *method,
2006                   struct blob_attr *msg)
2007 {
2008         int i, rem, rem2;
2009         glob_t gl;
2010         struct blob_buf menu = { 0 };
2011         struct blob_buf item = { 0 };
2012         struct blob_attr *entry, *attr;
2013         struct blob_attr *tb[__RPC_MENU_MAX];
2014         bool access;
2015         void *c, *d;
2016
2017         blobmsg_parse(rpc_menu_policy, __RPC_MENU_MAX, tb,
2018                       blob_data(msg), blob_len(msg));
2019
2020         if (!tb[RPC_MENU_SESSION])
2021                 return UBUS_STATUS_INVALID_ARGUMENT;
2022
2023
2024         blob_buf_init(&buf, 0);
2025         c = blobmsg_open_table(&buf, "menu");
2026
2027         if (!glob(RPC_LUCI2_MENU_FILES, 0, NULL, &gl))
2028         {
2029                 for (i = 0; i < gl.gl_pathc; i++)
2030                 {
2031                         blob_buf_init(&menu, 0);
2032
2033                         if (!blobmsg_add_json_from_file(&menu, gl.gl_pathv[i]))
2034                                 goto skip;
2035
2036                         blob_for_each_attr(entry, menu.head, rem)
2037                         {
2038                                 access = true;
2039
2040                                 blob_buf_init(&item, 0);
2041                                 d = blobmsg_open_table(&item, blobmsg_name(entry));
2042
2043                                 blobmsg_for_each_attr(attr, entry, rem2)
2044                                 {
2045                                         if (blob_id(attr) == BLOBMSG_TYPE_ARRAY &&
2046                                             !strcmp(blobmsg_name(attr), "acls"))
2047                                                 access = menu_access(tb[RPC_MENU_SESSION], attr, &item);
2048                                         else
2049                                                 blobmsg_add_blob(&item, attr);
2050                                 }
2051
2052                                 blobmsg_close_table(&item, d);
2053
2054                                 if (access)
2055                                         blob_for_each_attr(attr, item.head, rem2)
2056                                                 blobmsg_add_blob(&buf, attr);
2057
2058                                 blob_buf_free(&item);
2059                         }
2060
2061 skip:
2062                         blob_buf_free(&menu);
2063                 }
2064
2065                 globfree(&gl);
2066         }
2067
2068         blobmsg_close_table(&buf, c);
2069
2070         ubus_send_reply(ctx, req, buf.head);
2071         return 0;
2072 }
2073
2074
2075 int rpc_luci2_api_init(struct ubus_context *ctx)
2076 {
2077         int rv = 0;
2078
2079         static const struct ubus_method luci2_system_methods[] = {
2080                 UBUS_METHOD_NOARG("syslog",       rpc_luci2_system_log),
2081                 UBUS_METHOD_NOARG("dmesg",        rpc_luci2_system_dmesg),
2082                 UBUS_METHOD_NOARG("diskfree",     rpc_luci2_system_diskfree),
2083                 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list),
2084                 UBUS_METHOD("process_signal",     rpc_luci2_process_signal,
2085                                                   rpc_signal_policy),
2086                 UBUS_METHOD_NOARG("init_list",    rpc_luci2_init_list),
2087                 UBUS_METHOD("init_action",        rpc_luci2_init_action,
2088                                                   rpc_init_policy),
2089                 UBUS_METHOD_NOARG("rclocal_get",  rpc_luci2_rclocal_get),
2090                 UBUS_METHOD("rclocal_set",        rpc_luci2_rclocal_set,
2091                                                   rpc_data_policy),
2092                 UBUS_METHOD_NOARG("crontab_get",  rpc_luci2_crontab_get),
2093                 UBUS_METHOD("crontab_set",        rpc_luci2_crontab_set,
2094                                                   rpc_data_policy),
2095                 UBUS_METHOD_NOARG("sshkeys_get",  rpc_luci2_sshkeys_get),
2096                 UBUS_METHOD("sshkeys_set",        rpc_luci2_sshkeys_set,
2097                                                   rpc_sshkey_policy),
2098                 UBUS_METHOD("password_set",       rpc_luci2_password_set,
2099                                                   rpc_password_policy),
2100                 UBUS_METHOD_NOARG("led_list",     rpc_luci2_led_list),
2101                 UBUS_METHOD_NOARG("usb_list",     rpc_luci2_usb_list),
2102                 UBUS_METHOD_NOARG("upgrade_test", rpc_luci2_upgrade_test),
2103                 UBUS_METHOD("upgrade_start",      rpc_luci2_upgrade_start,
2104                                                   rpc_upgrade_policy),
2105                 UBUS_METHOD_NOARG("upgrade_clean", rpc_luci2_upgrade_clean),
2106                 UBUS_METHOD_NOARG("backup_restore", rpc_luci2_backup_restore),
2107                 UBUS_METHOD_NOARG("backup_clean", rpc_luci2_backup_clean),
2108                 UBUS_METHOD_NOARG("backup_config_get", rpc_luci2_backup_config_get),
2109                 UBUS_METHOD("backup_config_set",  rpc_luci2_backup_config_set,
2110                                                   rpc_data_policy),
2111                 UBUS_METHOD_NOARG("backup_list",  rpc_luci2_backup_list),
2112                 UBUS_METHOD_NOARG("reset_test",   rpc_luci2_reset_test),
2113                 UBUS_METHOD_NOARG("reset_start",  rpc_luci2_reset_start),
2114                 UBUS_METHOD_NOARG("reboot",       rpc_luci2_reboot)
2115         };
2116
2117         static struct ubus_object_type luci2_system_type =
2118                 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods);
2119
2120         static struct ubus_object system_obj = {
2121                 .name = "luci2.system",
2122                 .type = &luci2_system_type,
2123                 .methods = luci2_system_methods,
2124                 .n_methods = ARRAY_SIZE(luci2_system_methods),
2125         };
2126
2127
2128         static const struct ubus_method luci2_network_methods[] = {
2129                 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count),
2130                 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table),
2131                 UBUS_METHOD_NOARG("arp_table",       rpc_luci2_network_arp_table),
2132                 UBUS_METHOD_NOARG("dhcp_leases",     rpc_luci2_network_leases),
2133                 UBUS_METHOD_NOARG("dhcp6_leases",    rpc_luci2_network_leases6),
2134                 UBUS_METHOD_NOARG("routes",          rpc_luci2_network_routes),
2135                 UBUS_METHOD_NOARG("routes6",         rpc_luci2_network_routes6),
2136         };
2137
2138         static struct ubus_object_type luci2_network_type =
2139                 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods);
2140
2141         static struct ubus_object network_obj = {
2142                 .name = "luci2.network",
2143                 .type = &luci2_network_type,
2144                 .methods = luci2_network_methods,
2145                 .n_methods = ARRAY_SIZE(luci2_network_methods),
2146         };
2147
2148
2149         static const struct ubus_method luci2_opkg_methods[] = {
2150                 UBUS_METHOD("list",                  rpc_luci2_opkg_list,
2151                                                      rpc_opkg_match_policy),
2152                 UBUS_METHOD("list_installed",        rpc_luci2_opkg_list_installed,
2153                                                      rpc_opkg_match_policy),
2154                 UBUS_METHOD("find",                  rpc_luci2_opkg_find,
2155                                                      rpc_opkg_match_policy),
2156                 UBUS_METHOD("install",               rpc_luci2_opkg_install,
2157                                                      rpc_opkg_package_policy),
2158                 UBUS_METHOD("remove",                rpc_luci2_opkg_remove,
2159                                                      rpc_opkg_package_policy),
2160                 UBUS_METHOD_NOARG("update",          rpc_luci2_opkg_update),
2161                 UBUS_METHOD_NOARG("config_get",      rpc_luci2_opkg_config_get),
2162                 UBUS_METHOD("config_set",            rpc_luci2_opkg_config_set,
2163                                                      rpc_data_policy)
2164         };
2165
2166         static struct ubus_object_type luci2_opkg_type =
2167                 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods);
2168
2169         static struct ubus_object opkg_obj = {
2170                 .name = "luci2.opkg",
2171                 .type = &luci2_opkg_type,
2172                 .methods = luci2_opkg_methods,
2173                 .n_methods = ARRAY_SIZE(luci2_opkg_methods),
2174         };
2175
2176
2177         static const struct ubus_method luci2_ui_methods[] = {
2178                 UBUS_METHOD_NOARG("menu",            rpc_luci2_ui_menu)
2179         };
2180
2181         static struct ubus_object_type luci2_ui_type =
2182                 UBUS_OBJECT_TYPE("luci-rpc-luci2-ui", luci2_ui_methods);
2183
2184         static struct ubus_object ui_obj = {
2185                 .name = "luci2.ui",
2186                 .type = &luci2_ui_type,
2187                 .methods = luci2_ui_methods,
2188                 .n_methods = ARRAY_SIZE(luci2_ui_methods),
2189         };
2190
2191         cursor = uci_alloc_context();
2192
2193         if (!cursor)
2194                 return UBUS_STATUS_UNKNOWN_ERROR;
2195
2196         rv |= ubus_add_object(ctx, &system_obj);
2197         rv |= ubus_add_object(ctx, &network_obj);
2198         rv |= ubus_add_object(ctx, &opkg_obj);
2199         rv |= ubus_add_object(ctx, &ui_obj);
2200
2201         return rv;
2202 }