a96bdd64886f6a8d3bbae3c6891d9048fbc5823e
[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
33 #include "luci2.h"
34 #include "exec.h"
35
36 static struct blob_buf buf;
37 static struct uci_context *cursor;
38
39 enum {
40 RPC_S_PID,
41 RPC_S_SIGNAL,
42 __RPC_S_MAX,
43 };
44
45 static const struct blobmsg_policy rpc_signal_policy[__RPC_S_MAX] = {
46 [RPC_S_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
47 [RPC_S_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
48 };
49
50 enum {
51 RPC_I_NAME,
52 RPC_I_ACTION,
53 __RPC_I_MAX,
54 };
55
56 static const struct blobmsg_policy rpc_init_policy[__RPC_I_MAX] = {
57 [RPC_I_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
58 [RPC_I_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
59 };
60
61 enum {
62 RPC_RC_DATA,
63 __RPC_RC_MAX
64 };
65
66 static const struct blobmsg_policy rpc_rclocal_policy[__RPC_RC_MAX] = {
67 [RPC_RC_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
68 };
69
70 enum {
71 RPC_K_KEYS,
72 __RPC_K_MAX
73 };
74
75 static const struct blobmsg_policy rpc_sshkey_policy[__RPC_K_MAX] = {
76 [RPC_K_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
77 };
78
79 enum {
80 RPC_P_USER,
81 RPC_P_PASSWORD,
82 __RPC_P_MAX
83 };
84
85 static const struct blobmsg_policy rpc_password_policy[__RPC_P_MAX] = {
86 [RPC_P_USER] = { .name = "user", .type = BLOBMSG_TYPE_STRING },
87 [RPC_P_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
88 };
89
90 enum {
91 RPC_OM_LIMIT,
92 RPC_OM_OFFSET,
93 RPC_OM_PATTERN,
94 __RPC_OM_MAX
95 };
96
97 static const struct blobmsg_policy rpc_opkg_match_policy[__RPC_OM_MAX] = {
98 [RPC_OM_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
99 [RPC_OM_OFFSET] = { .name = "offset", .type = BLOBMSG_TYPE_INT32 },
100 [RPC_OM_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
101 };
102
103 enum {
104 RPC_OP_PACKAGE,
105 __RPC_OP_MAX
106 };
107
108 static const struct blobmsg_policy rpc_opkg_package_policy[__RPC_OP_MAX] = {
109 [RPC_OP_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
110 };
111
112 enum {
113 RPC_OC_CONFIG,
114 __RPC_OC_MAX
115 };
116
117 static const struct blobmsg_policy rpc_opkg_config_policy[__RPC_OC_MAX] = {
118 [RPC_OC_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
119 };
120
121
122 static int
123 rpc_errno_status(void)
124 {
125 switch (errno)
126 {
127 case EACCES:
128 return UBUS_STATUS_PERMISSION_DENIED;
129
130 case ENOTDIR:
131 return UBUS_STATUS_INVALID_ARGUMENT;
132
133 case ENOENT:
134 return UBUS_STATUS_NOT_FOUND;
135
136 case EINVAL:
137 return UBUS_STATUS_INVALID_ARGUMENT;
138
139 default:
140 return UBUS_STATUS_UNKNOWN_ERROR;
141 }
142 }
143
144 static void
145 log_read(FILE *log, int logsize)
146 {
147 int len;
148 char *logbuf;
149
150 if (logsize == 0)
151 logsize = RPC_LUCI2_DEF_LOGSIZE;
152
153 len = (logsize > RPC_LUCI2_MAX_LOGSIZE) ? RPC_LUCI2_MAX_LOGSIZE : logsize;
154 logbuf = blobmsg_alloc_string_buffer(&buf, "log", len + 1);
155
156 if (!logbuf)
157 return;
158
159 while (logsize > RPC_LUCI2_MAX_LOGSIZE)
160 {
161 len = logsize % RPC_LUCI2_MAX_LOGSIZE;
162
163 if (len == 0)
164 len = RPC_LUCI2_MAX_LOGSIZE;
165
166 fread(logbuf, 1, len, log);
167 logsize -= len;
168 }
169
170 len = fread(logbuf, 1, logsize, log);
171 *(logbuf + len) = 0;
172
173 blobmsg_add_string_buffer(&buf);
174 }
175
176 static int
177 rpc_luci2_system_log(struct ubus_context *ctx, struct ubus_object *obj,
178 struct ubus_request_data *req, const char *method,
179 struct blob_attr *msg)
180 {
181 FILE *log;
182 int logsize = 0;
183 const char *logfile = NULL;
184 struct stat st;
185 struct uci_package *p;
186 struct uci_element *e;
187 struct uci_section *s;
188 struct uci_ptr ptr = { .package = "system" };
189
190 uci_load(cursor, ptr.package, &p);
191
192 if (!p)
193 return UBUS_STATUS_NOT_FOUND;
194
195 uci_foreach_element(&p->sections, e)
196 {
197 s = uci_to_section(e);
198
199 if (strcmp(s->type, "system"))
200 continue;
201
202 ptr.o = NULL;
203 ptr.option = "log_type";
204 ptr.section = e->name;
205 uci_lookup_ptr(cursor, &ptr, NULL, true);
206 break;
207 }
208
209 if (ptr.o && ptr.o->type == UCI_TYPE_STRING &&
210 !strcmp(ptr.o->v.string, "file"))
211 {
212 ptr.o = NULL;
213 ptr.option = "log_file";
214 uci_lookup_ptr(cursor, &ptr, NULL, true);
215
216 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
217 logfile = ptr.o->v.string;
218 else
219 logfile = "/var/log/messages";
220
221 if (stat(logfile, &st) || !(log = fopen(logfile, "r")))
222 goto fail;
223
224 logsize = st.st_size;
225 }
226 else
227 {
228 ptr.o = NULL;
229 ptr.option = "log_size";
230 uci_lookup_ptr(cursor, &ptr, NULL, true);
231
232 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
233 logsize = atoi(ptr.o->v.string) * 1024;
234
235 if (!(log = popen("logread", "r")))
236 goto fail;
237 }
238
239 blob_buf_init(&buf, 0);
240
241 log_read(log, logsize);
242 fclose(log);
243
244 uci_unload(cursor, p);
245 ubus_send_reply(ctx, req, buf.head);
246 return 0;
247
248 fail:
249 uci_unload(cursor, p);
250 return rpc_errno_status();
251 }
252
253 static int
254 rpc_luci2_system_dmesg(struct ubus_context *ctx, struct ubus_object *obj,
255 struct ubus_request_data *req, const char *method,
256 struct blob_attr *msg)
257 {
258 FILE *log;
259
260 if (!(log = popen("dmesg", "r")))
261 return rpc_errno_status();
262
263 blob_buf_init(&buf, 0);
264
265 log_read(log, RPC_LUCI2_MAX_LOGSIZE);
266 fclose(log);
267
268 ubus_send_reply(ctx, req, buf.head);
269 return 0;
270 }
271
272 static int
273 rpc_luci2_system_diskfree(struct ubus_context *ctx, struct ubus_object *obj,
274 struct ubus_request_data *req, const char *method,
275 struct blob_attr *msg)
276 {
277 int i;
278 void *c;
279 struct statvfs s;
280 const char *fslist[] = {
281 "/", "root",
282 "/tmp", "tmp",
283 };
284
285 blob_buf_init(&buf, 0);
286
287 for (i = 0; i < sizeof(fslist) / sizeof(fslist[0]); i += 2)
288 {
289 if (statvfs(fslist[i], &s))
290 continue;
291
292 c = blobmsg_open_table(&buf, fslist[i+1]);
293
294 blobmsg_add_u32(&buf, "total", s.f_blocks * s.f_frsize);
295 blobmsg_add_u32(&buf, "free", s.f_bfree * s.f_frsize);
296 blobmsg_add_u32(&buf, "used", (s.f_blocks - s.f_bfree) * s.f_frsize);
297
298 blobmsg_close_table(&buf, c);
299 }
300
301 ubus_send_reply(ctx, req, buf.head);
302 return 0;
303 }
304
305 static int
306 rpc_luci2_process_list(struct ubus_context *ctx, struct ubus_object *obj,
307 struct ubus_request_data *req, const char *method,
308 struct blob_attr *msg)
309 {
310 FILE *top;
311 void *c, *d;
312 char line[1024];
313 char *pid, *ppid, *user, *stat, *vsz, *pvsz, *pcpu, *cmd;
314
315 if (!(top = popen("/bin/busybox top -bn1", "r")))
316 return rpc_errno_status();
317
318 blob_buf_init(&buf, 0);
319 c = blobmsg_open_array(&buf, "processes");
320
321 while (fgets(line, sizeof(line) - 1, top))
322 {
323 pid = strtok(line, " ");
324
325 if (*pid < '0' || *pid > '9')
326 continue;
327
328 ppid = strtok(NULL, " ");
329 user = strtok(NULL, " ");
330 stat = strtok(NULL, " ");
331
332 if (!stat)
333 continue;
334
335 if (!*(stat + 1))
336 *(stat + 1) = ' ';
337
338 if (!*(stat + 2))
339 *(stat + 2) = ' ';
340
341 *(stat + 3) = 0;
342
343 vsz = strtok(stat + 4, " ");
344 pvsz = strtok(NULL, " ");
345 pcpu = strtok(NULL, " ");
346 cmd = strtok(NULL, "\n");
347
348 if (!cmd)
349 continue;
350
351 d = blobmsg_open_table(&buf, NULL);
352
353 blobmsg_add_u32(&buf, "pid", atoi(pid));
354 blobmsg_add_u32(&buf, "ppid", atoi(ppid));
355 blobmsg_add_string(&buf, "user", user);
356 blobmsg_add_string(&buf, "stat", stat);
357 blobmsg_add_u32(&buf, "vsize", atoi(vsz) * 1024);
358 blobmsg_add_u32(&buf, "vsize_percent", atoi(pvsz));
359 blobmsg_add_u32(&buf, "cpu_percent", atoi(pcpu));
360 blobmsg_add_string(&buf, "command", cmd);
361
362 blobmsg_close_table(&buf, d);
363 }
364
365 fclose(top);
366 blobmsg_close_array(&buf, c);
367
368 ubus_send_reply(ctx, req, buf.head);
369 return 0;
370 }
371
372 static int
373 rpc_luci2_process_signal(struct ubus_context *ctx, struct ubus_object *obj,
374 struct ubus_request_data *req, const char *method,
375 struct blob_attr *msg)
376 {
377 int pid, sig;
378 struct blob_attr *tb[__RPC_S_MAX];
379
380 blobmsg_parse(rpc_signal_policy, __RPC_S_MAX, tb,
381 blob_data(msg), blob_len(msg));
382
383 if (!tb[RPC_S_SIGNAL] || !tb[RPC_S_PID])
384 {
385 errno = EINVAL;
386 return rpc_errno_status();
387 }
388
389 pid = blobmsg_get_u32(tb[RPC_S_PID]);
390 sig = blobmsg_get_u32(tb[RPC_S_SIGNAL]);
391
392 if (kill(pid, sig))
393 return rpc_errno_status();
394
395 return 0;
396 }
397
398 static int
399 rpc_luci2_init_list(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 n;
404 void *c, *t;
405 char *p, path[PATH_MAX];
406 struct stat s;
407 struct dirent *e;
408 FILE *f;
409 DIR *d;
410
411 if (!(d = opendir("/etc/init.d")))
412 return rpc_errno_status();
413
414 blob_buf_init(&buf, 0);
415 c = blobmsg_open_array(&buf, "initscripts");
416
417 while ((e = readdir(d)) != NULL)
418 {
419 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s", e->d_name);
420
421 if (stat(path, &s) || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR))
422 continue;
423
424 if ((f = fopen(path, "r")) != NULL)
425 {
426 n = -1;
427 p = fgets(path, sizeof(path) - 1, f);
428
429 if (!p || !strstr(p, "/etc/rc.common"))
430 goto skip;
431
432 t = blobmsg_open_table(&buf, NULL);
433
434 blobmsg_add_string(&buf, "name", e->d_name);
435
436 while (fgets(path, sizeof(path) - 1, f))
437 {
438 p = strtok(path, "= \t");
439
440 if (!strcmp(p, "START") && !!(p = strtok(NULL, "= \t\n")))
441 {
442 n = atoi(p);
443 blobmsg_add_u32(&buf, "start", n);
444 }
445 else if (!strcmp(p, "STOP") && !!(p = strtok(NULL, "= \t\n")))
446 {
447 blobmsg_add_u32(&buf, "stop", atoi(p));
448 break;
449 }
450 }
451
452 if (n > -1)
453 {
454 snprintf(path, sizeof(path) - 1, "/etc/rc.d/S%02d%s",
455 n, e->d_name);
456
457 blobmsg_add_u8(&buf, "enabled",
458 (!stat(path, &s) && (s.st_mode & S_IXUSR)));
459 }
460 else
461 {
462 blobmsg_add_u8(&buf, "enabled", 0);
463 }
464
465 blobmsg_close_table(&buf, t);
466
467 skip:
468 fclose(f);
469 }
470 }
471
472 closedir(d);
473 blobmsg_close_array(&buf, c);
474
475 ubus_send_reply(ctx, req, buf.head);
476 return 0;
477 }
478
479 static int
480 rpc_luci2_init_action(struct ubus_context *ctx, struct ubus_object *obj,
481 struct ubus_request_data *req, const char *method,
482 struct blob_attr *msg)
483 {
484 int fd;
485 pid_t pid;
486 struct stat s;
487 char path[PATH_MAX];
488 const char *action;
489 struct blob_attr *tb[__RPC_I_MAX];
490
491 blobmsg_parse(rpc_init_policy, __RPC_I_MAX, tb,
492 blob_data(msg), blob_len(msg));
493
494 if (!tb[RPC_I_NAME] || !tb[RPC_I_ACTION])
495 return UBUS_STATUS_INVALID_ARGUMENT;
496
497 action = blobmsg_data(tb[RPC_I_ACTION]);
498
499 if (strcmp(action, "start") && strcmp(action, "stop") &&
500 strcmp(action, "reload") && strcmp(action, "restart") &&
501 strcmp(action, "enable") && strcmp(action, "disable"))
502 return UBUS_STATUS_INVALID_ARGUMENT;
503
504 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s",
505 (char *)blobmsg_data(tb[RPC_I_NAME]));
506
507 if (stat(path, &s))
508 return rpc_errno_status();
509
510 if (!(s.st_mode & S_IXUSR))
511 return UBUS_STATUS_PERMISSION_DENIED;
512
513 switch ((pid = fork()))
514 {
515 case -1:
516 return rpc_errno_status();
517
518 case 0:
519 uloop_done();
520
521 if ((fd = open("/dev/null", O_RDWR)) > -1)
522 {
523 dup2(fd, 0);
524 dup2(fd, 1);
525 dup2(fd, 2);
526
527 close(fd);
528 }
529
530 chdir("/");
531
532 if (execl(path, path, action, NULL))
533 return rpc_errno_status();
534
535 default:
536 return 0;
537 }
538 }
539
540 static int
541 rpc_luci2_rclocal_get(struct ubus_context *ctx, struct ubus_object *obj,
542 struct ubus_request_data *req, const char *method,
543 struct blob_attr *msg)
544 {
545 FILE *f;
546 char data[4096] = { 0 };
547
548 if (!(f = fopen("/etc/rc.local", "r")))
549 return rpc_errno_status();
550
551 fread(data, sizeof(data) - 1, 1, f);
552 fclose(f);
553
554 blob_buf_init(&buf, 0);
555 blobmsg_add_string(&buf, "data", data);
556
557 ubus_send_reply(ctx, req, buf.head);
558 return 0;
559 }
560
561 static int
562 rpc_luci2_rclocal_set(struct ubus_context *ctx, struct ubus_object *obj,
563 struct ubus_request_data *req, const char *method,
564 struct blob_attr *msg)
565 {
566 FILE *f;
567 struct blob_attr *tb[__RPC_RC_MAX];
568
569 blobmsg_parse(rpc_rclocal_policy, __RPC_RC_MAX, tb,
570 blob_data(msg), blob_len(msg));
571
572 if (!tb[RPC_RC_DATA] || blobmsg_data_len(tb[RPC_RC_DATA]) >= 4096)
573 return UBUS_STATUS_INVALID_ARGUMENT;
574
575 if (!(f = fopen("/etc/rc.local", "w")))
576 return rpc_errno_status();
577
578 fwrite(blobmsg_data(tb[RPC_RC_DATA]),
579 blobmsg_data_len(tb[RPC_RC_DATA]) - 1, 1, f);
580
581 fclose(f);
582 return 0;
583 }
584
585 static int
586 rpc_luci2_sshkeys_get(struct ubus_context *ctx, struct ubus_object *obj,
587 struct ubus_request_data *req, const char *method,
588 struct blob_attr *msg)
589 {
590 FILE *f;
591 void *c;
592 char *p, line[4096];
593
594 if (!(f = fopen("/etc/dropbear/authorized_keys", "r")))
595 return rpc_errno_status();
596
597 blob_buf_init(&buf, 0);
598 c = blobmsg_open_array(&buf, "keys");
599
600 while (fgets(line, sizeof(line) - 1, f))
601 {
602 for (p = line + strlen(line) - 1; (p > line) && isspace(*p); p--)
603 *p = 0;
604
605 for (p = line; isspace(*p); p++)
606 *p = 0;
607
608 if (*p)
609 blobmsg_add_string(&buf, NULL, p);
610 }
611
612 blobmsg_close_array(&buf, c);
613 fclose(f);
614
615 ubus_send_reply(ctx, req, buf.head);
616 return 0;
617 }
618
619 static int
620 rpc_luci2_sshkeys_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 int rem;
626 struct blob_attr *cur, *tb[__RPC_K_MAX];
627
628 blobmsg_parse(rpc_sshkey_policy, __RPC_K_MAX, tb,
629 blob_data(msg), blob_len(msg));
630
631 if (!tb[RPC_K_KEYS])
632 return UBUS_STATUS_INVALID_ARGUMENT;
633
634 if (!(f = fopen("/etc/dropbear/authorized_keys", "w")))
635 return rpc_errno_status();
636
637 blobmsg_for_each_attr(cur, tb[RPC_K_KEYS], rem)
638 {
639 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
640 continue;
641
642 fwrite(blobmsg_data(cur), blobmsg_data_len(cur) - 1, 1, f);
643 fwrite("\n", 1, 1, f);
644 }
645
646 fclose(f);
647 return 0;
648 }
649
650 static int
651 rpc_luci2_password_set(struct ubus_context *ctx, struct ubus_object *obj,
652 struct ubus_request_data *req, const char *method,
653 struct blob_attr *msg)
654 {
655 pid_t pid;
656 int fd, fds[2];
657 struct stat s;
658 struct blob_attr *tb[__RPC_P_MAX];
659
660 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
661 blob_data(msg), blob_len(msg));
662
663 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
664 return UBUS_STATUS_INVALID_ARGUMENT;
665
666 if (stat("/usr/bin/passwd", &s))
667 return UBUS_STATUS_NOT_FOUND;
668
669 if (!(s.st_mode & S_IXUSR))
670 return UBUS_STATUS_PERMISSION_DENIED;
671
672 if (pipe(fds))
673 return rpc_errno_status();
674
675 switch ((pid = fork()))
676 {
677 case -1:
678 close(fds[0]);
679 close(fds[1]);
680 return rpc_errno_status();
681
682 case 0:
683 uloop_done();
684
685 dup2(fds[0], 0);
686 close(fds[0]);
687 close(fds[1]);
688
689 if ((fd = open("/dev/null", O_RDWR)) > -1)
690 {
691 dup2(fd, 1);
692 dup2(fd, 2);
693 close(fd);
694 }
695
696 chdir("/");
697
698 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
699 blobmsg_data(tb[RPC_P_USER]), NULL))
700 return rpc_errno_status();
701
702 default:
703 close(fds[0]);
704
705 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
706 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
707 write(fds[1], "\n", 1);
708
709 usleep(100 * 1000);
710
711 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
712 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
713 write(fds[1], "\n", 1);
714
715 close(fds[1]);
716
717 waitpid(pid, NULL, 0);
718
719 return 0;
720 }
721 }
722
723
724 static FILE *
725 dnsmasq_leasefile(void)
726 {
727 FILE *leases = NULL;
728 struct uci_package *p;
729 struct uci_element *e;
730 struct uci_section *s;
731 struct uci_ptr ptr = {
732 .package = "dhcp",
733 .section = NULL,
734 .option = "leasefile"
735 };
736
737 uci_load(cursor, ptr.package, &p);
738
739 if (!p)
740 return NULL;
741
742 uci_foreach_element(&p->sections, e)
743 {
744 s = uci_to_section(e);
745
746 if (strcmp(s->type, "dnsmasq"))
747 continue;
748
749 ptr.section = e->name;
750 uci_lookup_ptr(cursor, &ptr, NULL, true);
751 break;
752 }
753
754 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
755 leases = fopen(ptr.o->v.string, "r");
756
757 uci_unload(cursor, p);
758
759 return leases;
760 }
761
762 static int
763 rpc_luci2_network_leases(struct ubus_context *ctx, struct ubus_object *obj,
764 struct ubus_request_data *req, const char *method,
765 struct blob_attr *msg)
766 {
767 FILE *leases;
768 void *c, *d;
769 char line[128];
770 char *ts, *mac, *addr, *name;
771 time_t now = time(NULL);
772
773 blob_buf_init(&buf, 0);
774 c = blobmsg_open_array(&buf, "leases");
775
776 leases = dnsmasq_leasefile();
777
778 if (!leases)
779 goto out;
780
781 while (fgets(line, sizeof(line) - 1, leases))
782 {
783 ts = strtok(line, " \t");
784 mac = strtok(NULL, " \t");
785 addr = strtok(NULL, " \t");
786 name = strtok(NULL, " \t");
787
788 if (!ts || !mac || !addr || !name)
789 continue;
790
791 if (strchr(addr, ':'))
792 continue;
793
794 d = blobmsg_open_table(&buf, NULL);
795
796 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
797 blobmsg_add_string(&buf, "macaddr", mac);
798 blobmsg_add_string(&buf, "ipaddr", addr);
799
800 if (strcmp(name, "*"))
801 blobmsg_add_string(&buf, "hostname", name);
802
803 blobmsg_close_table(&buf, d);
804 }
805
806 fclose(leases);
807
808 out:
809 blobmsg_close_array(&buf, c);
810 ubus_send_reply(ctx, req, buf.head);
811
812 return 0;
813 }
814
815 static int
816 rpc_luci2_network_leases6(struct ubus_context *ctx, struct ubus_object *obj,
817 struct ubus_request_data *req, const char *method,
818 struct blob_attr *msg)
819 {
820 FILE *leases;
821 void *c, *d;
822 char line[128];
823 char *ts, *mac, *addr, *name, *duid;
824 time_t now = time(NULL);
825
826 blob_buf_init(&buf, 0);
827 c = blobmsg_open_array(&buf, "leases");
828
829 leases = fopen("/tmp/hosts/6relayd", "r");
830
831 if (leases)
832 {
833 while (fgets(line, sizeof(line) - 1, leases))
834 {
835 if (strncmp(line, "# ", 2))
836 continue;
837
838 strtok(line + 2, " \t"); /* iface */
839
840 duid = strtok(NULL, " \t");
841
842 strtok(NULL, " \t"); /* iaid */
843
844 name = strtok(NULL, " \t");
845 ts = strtok(NULL, " \t");
846
847 strtok(NULL, " \t"); /* id */
848 strtok(NULL, " \t"); /* length */
849
850 addr = strtok(NULL, " \t\n");
851
852 if (!addr)
853 continue;
854
855 d = blobmsg_open_table(&buf, NULL);
856
857 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
858 blobmsg_add_string(&buf, "duid", duid);
859 blobmsg_add_string(&buf, "ip6addr", addr);
860
861 if (strcmp(name, "-"))
862 blobmsg_add_string(&buf, "hostname", name);
863
864 blobmsg_close_array(&buf, d);
865 }
866
867 fclose(leases);
868 }
869 else
870 {
871 leases = dnsmasq_leasefile();
872
873 if (!leases)
874 goto out;
875
876 while (fgets(line, sizeof(line) - 1, leases))
877 {
878 ts = strtok(line, " \t");
879 mac = strtok(NULL, " \t");
880 addr = strtok(NULL, " \t");
881 name = strtok(NULL, " \t");
882 duid = strtok(NULL, " \t\n");
883
884 if (!ts || !mac || !addr || !duid)
885 continue;
886
887 if (!strchr(addr, ':'))
888 continue;
889
890 d = blobmsg_open_table(&buf, NULL);
891
892 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
893 blobmsg_add_string(&buf, "macaddr", mac);
894 blobmsg_add_string(&buf, "ip6addr", addr);
895
896 if (strcmp(name, "*"))
897 blobmsg_add_string(&buf, "hostname", name);
898
899 if (strcmp(duid, "*"))
900 blobmsg_add_string(&buf, "duid", name);
901
902 blobmsg_close_table(&buf, d);
903 }
904
905 fclose(leases);
906 }
907
908 out:
909 blobmsg_close_array(&buf, c);
910 ubus_send_reply(ctx, req, buf.head);
911
912 return 0;
913 }
914
915 static int
916 rpc_luci2_network_ct_count(struct ubus_context *ctx, struct ubus_object *obj,
917 struct ubus_request_data *req, const char *method,
918 struct blob_attr *msg)
919 {
920 FILE *f;
921 char line[128];
922
923 blob_buf_init(&buf, 0);
924
925 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL)
926 {
927 if (fgets(line, sizeof(line) - 1, f))
928 blobmsg_add_u32(&buf, "count", atoi(line));
929
930 fclose(f);
931 }
932
933 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL)
934 {
935 if (fgets(line, sizeof(line) - 1, f))
936 blobmsg_add_u32(&buf, "limit", atoi(line));
937
938 fclose(f);
939 }
940
941 ubus_send_reply(ctx, req, buf.head);
942
943 return 0;
944 }
945
946 static int
947 rpc_luci2_network_ct_table(struct ubus_context *ctx, struct ubus_object *obj,
948 struct ubus_request_data *req, const char *method,
949 struct blob_attr *msg)
950 {
951 FILE *f;
952 int i;
953 void *c, *d;
954 char *p, line[512];
955 bool seen[6];
956
957 blob_buf_init(&buf, 0);
958 c = blobmsg_open_array(&buf, "entries");
959
960 if ((f = fopen("/proc/net/nf_conntrack", "r")) != NULL)
961 {
962 while (fgets(line, sizeof(line) - 1, f))
963 {
964 d = blobmsg_open_table(&buf, NULL);
965 memset(seen, 0, sizeof(seen));
966
967 for (i = 0, p = strtok(line, " "); p; i++, p = strtok(NULL, " "))
968 {
969 if (i == 0)
970 blobmsg_add_u8(&buf, "ipv6", !strcmp(p, "ipv6"));
971 else if (i == 3)
972 blobmsg_add_u32(&buf, "protocol", atoi(p));
973 else if (i == 4)
974 blobmsg_add_u32(&buf, "expires", atoi(p));
975 else if (i >= 5)
976 {
977 if (*p == '[')
978 continue;
979
980 if (!seen[0] && !strncmp(p, "src=", 4))
981 {
982 blobmsg_add_string(&buf, "src", p + 4);
983 seen[0] = true;
984 }
985 else if (!seen[1] && !strncmp(p, "dst=", 4))
986 {
987 blobmsg_add_string(&buf, "dest", p + 4);
988 seen[1] = true;
989 }
990 else if (!seen[2] && !strncmp(p, "sport=", 6))
991 {
992 blobmsg_add_u32(&buf, "sport", atoi(p + 6));
993 seen[2] = true;
994 }
995 else if (!seen[3] && !strncmp(p, "dport=", 6))
996 {
997 blobmsg_add_u32(&buf, "dport", atoi(p + 6));
998 seen[3] = true;
999 }
1000 else if (!strncmp(p, "packets=", 8))
1001 {
1002 blobmsg_add_u32(&buf,
1003 seen[4] ? "tx_packets" : "rx_packets",
1004 atoi(p + 8));
1005 seen[4] = true;
1006 }
1007 else if (!strncmp(p, "bytes=", 6))
1008 {
1009 blobmsg_add_u32(&buf,
1010 seen[5] ? "tx_bytes" : "rx_bytes",
1011 atoi(p + 6));
1012 seen[5] = true;
1013 }
1014 }
1015 }
1016
1017 blobmsg_close_table(&buf, d);
1018 }
1019
1020 fclose(f);
1021 }
1022
1023 blobmsg_close_array(&buf, c);
1024 ubus_send_reply(ctx, req, buf.head);
1025
1026 return 0;
1027 }
1028
1029 static int
1030 rpc_luci2_network_arp_table(struct ubus_context *ctx, struct ubus_object *obj,
1031 struct ubus_request_data *req, const char *method,
1032 struct blob_attr *msg)
1033 {
1034 FILE *f;
1035 void *c, *d;
1036 char *addr, *mac, *dev, line[128];
1037
1038 blob_buf_init(&buf, 0);
1039 c = blobmsg_open_array(&buf, "entries");
1040
1041 if ((f = fopen("/proc/net/arp", "r")) != NULL)
1042 {
1043 /* skip header line */
1044 fgets(line, sizeof(line) - 1, f);
1045
1046 while (fgets(line, sizeof(line) - 1, f))
1047 {
1048 addr = strtok(line, " \t");
1049
1050 strtok(NULL, " \t"); /* HW type */
1051 strtok(NULL, " \t"); /* Flags */
1052
1053 mac = strtok(NULL, " \t");
1054
1055 strtok(NULL, " \t"); /* Mask */
1056
1057 dev = strtok(NULL, " \t\n");
1058
1059 if (!dev)
1060 continue;
1061
1062 d = blobmsg_open_table(&buf, NULL);
1063 blobmsg_add_string(&buf, "ipaddr", addr);
1064 blobmsg_add_string(&buf, "macaddr", mac);
1065 blobmsg_add_string(&buf, "device", dev);
1066 blobmsg_close_table(&buf, d);
1067 }
1068
1069 fclose(f);
1070 }
1071
1072 blobmsg_close_array(&buf, c);
1073 ubus_send_reply(ctx, req, buf.head);
1074
1075 return 0;
1076 }
1077
1078 static void
1079 put_hexaddr(const char *name, const char *s, const char *m)
1080 {
1081 int bits;
1082 struct in_addr a;
1083 char as[sizeof("255.255.255.255/32\0")];
1084
1085 a.s_addr = strtoul(s, NULL, 16);
1086 inet_ntop(AF_INET, &a, as, sizeof(as));
1087
1088 if (m)
1089 {
1090 for (a.s_addr = ntohl(strtoul(m, NULL, 16)), bits = 0;
1091 a.s_addr & 0x80000000;
1092 a.s_addr <<= 1)
1093 bits++;
1094
1095 sprintf(as + strlen(as), "/%u", bits);
1096 }
1097
1098 blobmsg_add_string(&buf, name, as);
1099 }
1100
1101 static int
1102 rpc_luci2_network_routes(struct ubus_context *ctx, struct ubus_object *obj,
1103 struct ubus_request_data *req, const char *method,
1104 struct blob_attr *msg)
1105 {
1106 FILE *routes;
1107 void *c, *d;
1108 char *dst, *dmask, *next, *metric, *device;
1109 char line[256];
1110 unsigned int n;
1111
1112 if (!(routes = fopen("/proc/net/route", "r")))
1113 return rpc_errno_status();
1114
1115 blob_buf_init(&buf, 0);
1116 c = blobmsg_open_array(&buf, "routes");
1117
1118 /* skip header line */
1119 fgets(line, sizeof(line) - 1, routes);
1120
1121 while (fgets(line, sizeof(line) - 1, routes))
1122 {
1123 device = strtok(line, "\t ");
1124 dst = strtok(NULL, "\t ");
1125 next = strtok(NULL, "\t ");
1126
1127 strtok(NULL, "\t "); /* flags */
1128 strtok(NULL, "\t "); /* refcount */
1129 strtok(NULL, "\t "); /* usecount */
1130
1131 metric = strtok(NULL, "\t ");
1132 dmask = strtok(NULL, "\t ");
1133
1134 if (!dmask)
1135 continue;
1136
1137 d = blobmsg_open_table(&buf, NULL);
1138
1139 put_hexaddr("target", dst, dmask);
1140 put_hexaddr("nexthop", next, NULL);
1141
1142 n = strtoul(metric, NULL, 10);
1143 blobmsg_add_u32(&buf, "metric", n);
1144
1145 blobmsg_add_string(&buf, "device", device);
1146
1147 blobmsg_close_table(&buf, d);
1148 }
1149
1150 blobmsg_close_array(&buf, c);
1151 fclose(routes);
1152
1153 ubus_send_reply(ctx, req, buf.head);
1154 return 0;
1155 }
1156
1157 static void
1158 put_hex6addr(const char *name, const char *s, const char *m)
1159 {
1160 int i;
1161 struct in6_addr a;
1162 char as[INET6_ADDRSTRLEN + sizeof("/128")];
1163
1164 #define hex(x) \
1165 (((x) <= '9') ? ((x) - '0') : \
1166 (((x) <= 'F') ? ((x) - 'A' + 10) : \
1167 ((x) - 'a' + 10)))
1168
1169 for (i = 0; i < 16; i++, s += 2)
1170 a.s6_addr[i] = (16 * hex(*s)) + hex(*(s+1));
1171
1172 inet_ntop(AF_INET6, &a, as, sizeof(as));
1173
1174 if (m)
1175 sprintf(as + strlen(as), "/%lu", strtoul(m, NULL, 16));
1176
1177 blobmsg_add_string(&buf, name, as);
1178 }
1179
1180 static int
1181 rpc_luci2_network_routes6(struct ubus_context *ctx, struct ubus_object *obj,
1182 struct ubus_request_data *req, const char *method,
1183 struct blob_attr *msg)
1184 {
1185 FILE *routes;
1186 void *c, *d;
1187 char *src, *smask, *dst, *dmask, *next, *metric, *flags, *device;
1188 char line[256];
1189 unsigned int n;
1190
1191 if (!(routes = fopen("/proc/net/ipv6_route", "r")))
1192 return rpc_errno_status();
1193
1194 blob_buf_init(&buf, 0);
1195 c = blobmsg_open_array(&buf, "routes");
1196
1197 while (fgets(line, sizeof(line) - 1, routes))
1198 {
1199 dst = strtok(line, " ");
1200 dmask = strtok(NULL, " ");
1201 src = strtok(NULL, " ");
1202 smask = strtok(NULL, " ");
1203 next = strtok(NULL, " ");
1204 metric = strtok(NULL, " ");
1205
1206 strtok(NULL, " "); /* refcount */
1207 strtok(NULL, " "); /* usecount */
1208
1209 flags = strtok(NULL, " ");
1210 device = strtok(NULL, " \n");
1211
1212 if (!device)
1213 continue;
1214
1215 n = strtoul(flags, NULL, 16);
1216
1217 if (!(n & 1))
1218 continue;
1219
1220 d = blobmsg_open_table(&buf, NULL);
1221
1222 put_hex6addr("target", dst, dmask);
1223 put_hex6addr("source", src, smask);
1224 put_hex6addr("nexthop", next, NULL);
1225
1226 n = strtoul(metric, NULL, 16);
1227 blobmsg_add_u32(&buf, "metric", n);
1228
1229 blobmsg_add_string(&buf, "device", device);
1230
1231 blobmsg_close_table(&buf, d);
1232 }
1233
1234 blobmsg_close_array(&buf, c);
1235 fclose(routes);
1236
1237 ubus_send_reply(ctx, req, buf.head);
1238 return 0;
1239 }
1240
1241
1242 struct opkg_state {
1243 int cur_offset;
1244 int cur_count;
1245 int req_offset;
1246 int req_count;
1247 int total;
1248 bool open;
1249 void *array;
1250 };
1251
1252 static int
1253 opkg_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
1254 {
1255 struct opkg_state *s = priv;
1256
1257 char *ptr, *last;
1258 char *nl = strchr(buf, '\n');
1259 char *name = NULL, *vers = NULL, *desc = NULL;
1260 void *c;
1261
1262 if (!nl)
1263 return 0;
1264
1265 s->total++;
1266
1267 if (s->cur_offset++ < s->req_offset)
1268 goto skip;
1269
1270 if (s->cur_count++ >= s->req_count)
1271 goto skip;
1272
1273 if (!s->open)
1274 {
1275 s->open = true;
1276 s->array = blobmsg_open_array(blob, "packages");
1277 }
1278
1279 for (ptr = buf, last = buf, *nl = 0; ptr <= nl; ptr++)
1280 {
1281 if (!*ptr || (*ptr == ' ' && *(ptr+1) == '-' && *(ptr+2) == ' '))
1282 {
1283 if (!name)
1284 {
1285 name = last;
1286 last = ptr + 3;
1287 *ptr = 0;
1288 ptr += 2;
1289 }
1290 else if (!vers)
1291 {
1292 vers = last;
1293 desc = *ptr ? (ptr + 3) : NULL;
1294 *ptr = 0;
1295 break;
1296 }
1297 }
1298 }
1299
1300 if (name && vers)
1301 {
1302 c = blobmsg_open_array(blob, NULL);
1303
1304 blobmsg_add_string(blob, NULL, name);
1305 blobmsg_add_string(blob, NULL, vers);
1306
1307 if (desc && *desc)
1308 blobmsg_add_string(blob, NULL, desc);
1309
1310 blobmsg_close_array(blob, c);
1311 }
1312
1313 skip:
1314 return (nl - buf + 1);
1315 }
1316
1317 static void
1318 opkg_finish_list(struct blob_buf *blob, int status, void *priv)
1319 {
1320 struct opkg_state *s = priv;
1321
1322 if (!s->open)
1323 return;
1324
1325 blobmsg_close_array(blob, s->array);
1326 blobmsg_add_u32(blob, "total", s->total);
1327 }
1328
1329 static int
1330 opkg_exec_list(const char *action, struct blob_attr *msg,
1331 struct ubus_context *ctx, struct ubus_request_data *req)
1332 {
1333 struct opkg_state *state = NULL;
1334 struct blob_attr *tb[__RPC_OM_MAX];
1335 const char *cmd[5] = { "opkg", action, "-nocase", NULL, NULL };
1336
1337 blobmsg_parse(rpc_opkg_match_policy, __RPC_OM_MAX, tb,
1338 blob_data(msg), blob_len(msg));
1339
1340 state = malloc(sizeof(*state));
1341
1342 if (!state)
1343 return UBUS_STATUS_UNKNOWN_ERROR;
1344
1345 memset(state, 0, sizeof(*state));
1346
1347 if (tb[RPC_OM_PATTERN])
1348 cmd[3] = blobmsg_data(tb[RPC_OM_PATTERN]);
1349
1350 if (tb[RPC_OM_LIMIT])
1351 state->req_count = blobmsg_get_u32(tb[RPC_OM_LIMIT]);
1352
1353 if (tb[RPC_OM_OFFSET])
1354 state->req_offset = blobmsg_get_u32(tb[RPC_OM_OFFSET]);
1355
1356 if (state->req_offset < 0)
1357 state->req_offset = 0;
1358
1359 if (state->req_count <= 0 || state->req_count > 100)
1360 state->req_count = 100;
1361
1362 return rpc_exec(cmd, opkg_parse_list, NULL, opkg_finish_list,
1363 state, ctx, req);
1364 }
1365
1366
1367 static int
1368 rpc_luci2_opkg_list(struct ubus_context *ctx, struct ubus_object *obj,
1369 struct ubus_request_data *req, const char *method,
1370 struct blob_attr *msg)
1371 {
1372 return opkg_exec_list("list", msg, ctx, req);
1373 }
1374
1375 static int
1376 rpc_luci2_opkg_list_installed(struct ubus_context *ctx, struct ubus_object *obj,
1377 struct ubus_request_data *req, const char *method,
1378 struct blob_attr *msg)
1379 {
1380 return opkg_exec_list("list-installed", msg, ctx, req);
1381 }
1382
1383 static int
1384 rpc_luci2_opkg_find(struct ubus_context *ctx, struct ubus_object *obj,
1385 struct ubus_request_data *req, const char *method,
1386 struct blob_attr *msg)
1387 {
1388 return opkg_exec_list("find", msg, ctx, req);
1389 }
1390
1391 static int
1392 rpc_luci2_opkg_update(struct ubus_context *ctx, struct ubus_object *obj,
1393 struct ubus_request_data *req, const char *method,
1394 struct blob_attr *msg)
1395 {
1396 const char *cmd[3] = { "opkg", "update", NULL };
1397 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1398 }
1399
1400 static int
1401 rpc_luci2_opkg_install(struct ubus_context *ctx, struct ubus_object *obj,
1402 struct ubus_request_data *req, const char *method,
1403 struct blob_attr *msg)
1404 {
1405 struct blob_attr *tb[__RPC_OP_MAX];
1406 const char *cmd[5] = { "opkg", "--force-overwrite",
1407 "install", NULL, NULL };
1408
1409 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1410 blob_data(msg), blob_len(msg));
1411
1412 if (!tb[RPC_OP_PACKAGE])
1413 return UBUS_STATUS_INVALID_ARGUMENT;
1414
1415 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1416
1417 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1418 }
1419
1420 static int
1421 rpc_luci2_opkg_remove(struct ubus_context *ctx, struct ubus_object *obj,
1422 struct ubus_request_data *req, const char *method,
1423 struct blob_attr *msg)
1424 {
1425 struct blob_attr *tb[__RPC_OP_MAX];
1426 const char *cmd[5] = { "opkg", "--force-removal-of-dependent-packages",
1427 "remove", NULL, NULL };
1428
1429 blobmsg_parse(rpc_opkg_package_policy, __RPC_OP_MAX, tb,
1430 blob_data(msg), blob_len(msg));
1431
1432 if (!tb[RPC_OP_PACKAGE])
1433 return UBUS_STATUS_INVALID_ARGUMENT;
1434
1435 cmd[3] = blobmsg_data(tb[RPC_OP_PACKAGE]);
1436
1437 return rpc_exec(cmd, NULL, NULL, NULL, NULL, ctx, req);
1438 }
1439
1440 static int
1441 rpc_luci2_opkg_config_get(struct ubus_context *ctx, struct ubus_object *obj,
1442 struct ubus_request_data *req, const char *method,
1443 struct blob_attr *msg)
1444 {
1445 FILE *f;
1446 char conf[2048] = { 0 };
1447
1448 if (!(f = fopen("/etc/opkg.conf", "r")))
1449 return rpc_errno_status();
1450
1451 fread(conf, sizeof(conf) - 1, 1, f);
1452 fclose(f);
1453
1454 blob_buf_init(&buf, 0);
1455 blobmsg_add_string(&buf, "config", conf);
1456
1457 ubus_send_reply(ctx, req, buf.head);
1458 return 0;
1459 }
1460
1461 static int
1462 rpc_luci2_opkg_config_set(struct ubus_context *ctx, struct ubus_object *obj,
1463 struct ubus_request_data *req, const char *method,
1464 struct blob_attr *msg)
1465 {
1466 FILE *f;
1467 struct blob_attr *tb[__RPC_OC_MAX];
1468
1469 blobmsg_parse(rpc_opkg_package_policy, __RPC_OC_MAX, tb,
1470 blob_data(msg), blob_len(msg));
1471
1472 if (!tb[RPC_OC_CONFIG])
1473 return UBUS_STATUS_INVALID_ARGUMENT;
1474
1475 if (blobmsg_type(tb[RPC_OC_CONFIG]) != BLOBMSG_TYPE_STRING)
1476 return UBUS_STATUS_INVALID_ARGUMENT;
1477
1478 if (blobmsg_data_len(tb[RPC_OC_CONFIG]) >= 2048)
1479 return UBUS_STATUS_NOT_SUPPORTED;
1480
1481 if (!(f = fopen("/etc/opkg.conf", "w")))
1482 return rpc_errno_status();
1483
1484 fwrite(blobmsg_data(tb[RPC_OC_CONFIG]),
1485 blobmsg_data_len(tb[RPC_OC_CONFIG]), 1, f);
1486
1487 fclose(f);
1488 return 0;
1489 }
1490
1491
1492 int rpc_luci2_api_init(struct ubus_context *ctx)
1493 {
1494 int rv = 0;
1495
1496 static const struct ubus_method luci2_system_methods[] = {
1497 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log),
1498 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg),
1499 UBUS_METHOD_NOARG("diskfree", rpc_luci2_system_diskfree),
1500 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list),
1501 UBUS_METHOD("process_signal", rpc_luci2_process_signal,
1502 rpc_signal_policy),
1503 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list),
1504 UBUS_METHOD("init_action", rpc_luci2_init_action,
1505 rpc_init_policy),
1506 UBUS_METHOD_NOARG("rclocal_get", rpc_luci2_rclocal_get),
1507 UBUS_METHOD("rclocal_set", rpc_luci2_rclocal_set,
1508 rpc_rclocal_policy),
1509 UBUS_METHOD_NOARG("sshkeys_get", rpc_luci2_sshkeys_get),
1510 UBUS_METHOD("sshkeys_set", rpc_luci2_sshkeys_set,
1511 rpc_sshkey_policy),
1512 UBUS_METHOD("password_set", rpc_luci2_password_set,
1513 rpc_password_policy)
1514 };
1515
1516 static struct ubus_object_type luci2_system_type =
1517 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods);
1518
1519 static struct ubus_object system_obj = {
1520 .name = "luci2.system",
1521 .type = &luci2_system_type,
1522 .methods = luci2_system_methods,
1523 .n_methods = ARRAY_SIZE(luci2_system_methods),
1524 };
1525
1526
1527 static const struct ubus_method luci2_network_methods[] = {
1528 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count),
1529 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table),
1530 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table),
1531 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases),
1532 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6),
1533 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes),
1534 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6),
1535 };
1536
1537 static struct ubus_object_type luci2_network_type =
1538 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods);
1539
1540 static struct ubus_object network_obj = {
1541 .name = "luci2.network",
1542 .type = &luci2_network_type,
1543 .methods = luci2_network_methods,
1544 .n_methods = ARRAY_SIZE(luci2_network_methods),
1545 };
1546
1547
1548 static const struct ubus_method luci2_opkg_methods[] = {
1549 UBUS_METHOD("list", rpc_luci2_opkg_list,
1550 rpc_opkg_match_policy),
1551 UBUS_METHOD("list_installed", rpc_luci2_opkg_list_installed,
1552 rpc_opkg_match_policy),
1553 UBUS_METHOD("find", rpc_luci2_opkg_find,
1554 rpc_opkg_match_policy),
1555 UBUS_METHOD("install", rpc_luci2_opkg_install,
1556 rpc_opkg_package_policy),
1557 UBUS_METHOD("remove", rpc_luci2_opkg_remove,
1558 rpc_opkg_package_policy),
1559 UBUS_METHOD_NOARG("update", rpc_luci2_opkg_update),
1560 UBUS_METHOD_NOARG("config_get", rpc_luci2_opkg_config_get),
1561 UBUS_METHOD("config_set", rpc_luci2_opkg_config_set,
1562 rpc_opkg_config_policy)
1563 };
1564
1565 static struct ubus_object_type luci2_opkg_type =
1566 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_opkg_methods);
1567
1568 static struct ubus_object opkg_obj = {
1569 .name = "luci2.opkg",
1570 .type = &luci2_opkg_type,
1571 .methods = luci2_opkg_methods,
1572 .n_methods = ARRAY_SIZE(luci2_opkg_methods),
1573 };
1574
1575 cursor = uci_alloc_context();
1576
1577 if (!cursor)
1578 return UBUS_STATUS_UNKNOWN_ERROR;
1579
1580 rv |= ubus_add_object(ctx, &system_obj);
1581 rv |= ubus_add_object(ctx, &network_obj);
1582 rv |= ubus_add_object(ctx, &opkg_obj);
1583
1584 return rv;
1585 }