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