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