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