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