luci2: make parsing START and STOP from init scripts more robust
[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 <sys/stat.h>
25 #include <sys/types.h>
26 #include <dirent.h>
27 #include <arpa/inet.h>
28 #include <signal.h>
29
30 #include "luci2.h"
31
32 static struct blob_buf buf;
33 static struct uci_context *cursor;
34
35 enum {
36 RPC_S_PID,
37 RPC_S_SIGNAL,
38 __RPC_S_MAX,
39 };
40
41 static const struct blobmsg_policy rpc_signal_policy[__RPC_S_MAX] = {
42 [RPC_S_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
43 [RPC_S_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
44 };
45
46 enum {
47 RPC_I_NAME,
48 RPC_I_ACTION,
49 __RPC_I_MAX,
50 };
51
52 static const struct blobmsg_policy rpc_init_policy[__RPC_I_MAX] = {
53 [RPC_I_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
54 [RPC_I_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
55 };
56
57
58 static int
59 rpc_errno_status(void)
60 {
61 switch (errno)
62 {
63 case EACCES:
64 return UBUS_STATUS_PERMISSION_DENIED;
65
66 case ENOTDIR:
67 return UBUS_STATUS_INVALID_ARGUMENT;
68
69 case ENOENT:
70 return UBUS_STATUS_NOT_FOUND;
71
72 case EINVAL:
73 return UBUS_STATUS_INVALID_ARGUMENT;
74
75 default:
76 return UBUS_STATUS_UNKNOWN_ERROR;
77 }
78 }
79
80 static void
81 log_read(FILE *log, int logsize)
82 {
83 int len;
84 char *logbuf;
85
86 if (logsize == 0)
87 logsize = RPC_LUCI2_DEF_LOGSIZE;
88
89 len = (logsize > RPC_LUCI2_MAX_LOGSIZE) ? RPC_LUCI2_MAX_LOGSIZE : logsize;
90 logbuf = blobmsg_alloc_string_buffer(&buf, "log", len + 1);
91
92 if (!logbuf)
93 return;
94
95 while (logsize > RPC_LUCI2_MAX_LOGSIZE)
96 {
97 len = logsize % RPC_LUCI2_MAX_LOGSIZE;
98
99 if (len == 0)
100 len = RPC_LUCI2_MAX_LOGSIZE;
101
102 fread(logbuf, 1, len, log);
103 logsize -= len;
104 }
105
106 len = fread(logbuf, 1, logsize, log);
107 *(logbuf + len) = 0;
108
109 blobmsg_add_string_buffer(&buf);
110 }
111
112 static int
113 rpc_luci2_system_log(struct ubus_context *ctx, struct ubus_object *obj,
114 struct ubus_request_data *req, const char *method,
115 struct blob_attr *msg)
116 {
117 FILE *log;
118 int logsize = 0;
119 const char *logfile = NULL;
120 struct stat st;
121 struct uci_package *p;
122 struct uci_element *e;
123 struct uci_section *s;
124 struct uci_ptr ptr = { .package = "system" };
125
126 uci_load(cursor, ptr.package, &p);
127
128 if (!p)
129 return UBUS_STATUS_NOT_FOUND;
130
131 uci_foreach_element(&p->sections, e)
132 {
133 s = uci_to_section(e);
134
135 if (strcmp(s->type, "system"))
136 continue;
137
138 ptr.o = NULL;
139 ptr.option = "log_type";
140 ptr.section = e->name;
141 uci_lookup_ptr(cursor, &ptr, NULL, true);
142 break;
143 }
144
145 if (ptr.o && ptr.o->type == UCI_TYPE_STRING &&
146 !strcmp(ptr.o->v.string, "file"))
147 {
148 ptr.o = NULL;
149 ptr.option = "log_file";
150 uci_lookup_ptr(cursor, &ptr, NULL, true);
151
152 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
153 logfile = ptr.o->v.string;
154 else
155 logfile = "/var/log/messages";
156
157 if (stat(logfile, &st) || !(log = fopen(logfile, "r")))
158 goto fail;
159
160 logsize = st.st_size;
161 }
162 else
163 {
164 ptr.o = NULL;
165 ptr.option = "log_size";
166 uci_lookup_ptr(cursor, &ptr, NULL, true);
167
168 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
169 logsize = atoi(ptr.o->v.string) * 1024;
170
171 if (!(log = popen("logread", "r")))
172 goto fail;
173 }
174
175 blob_buf_init(&buf, 0);
176
177 log_read(log, logsize);
178 fclose(log);
179
180 uci_unload(cursor, p);
181 ubus_send_reply(ctx, req, buf.head);
182 return 0;
183
184 fail:
185 uci_unload(cursor, p);
186 return rpc_errno_status();
187 }
188
189 static int
190 rpc_luci2_system_dmesg(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
196 if (!(log = popen("dmesg", "r")))
197 return rpc_errno_status();
198
199 blob_buf_init(&buf, 0);
200
201 log_read(log, RPC_LUCI2_MAX_LOGSIZE);
202 fclose(log);
203
204 ubus_send_reply(ctx, req, buf.head);
205 return 0;
206 }
207
208 static int
209 rpc_luci2_process_list(struct ubus_context *ctx, struct ubus_object *obj,
210 struct ubus_request_data *req, const char *method,
211 struct blob_attr *msg)
212 {
213 FILE *top;
214 void *c, *d;
215 char line[1024];
216 char *pid, *ppid, *user, *stat, *vsz, *pvsz, *pcpu, *cmd;
217
218 if (!(top = popen("/bin/busybox top -bn1", "r")))
219 return rpc_errno_status();
220
221 blob_buf_init(&buf, 0);
222 c = blobmsg_open_array(&buf, "processes");
223
224 while (fgets(line, sizeof(line) - 1, top))
225 {
226 pid = strtok(line, " ");
227
228 if (*pid < '0' || *pid > '9')
229 continue;
230
231 ppid = strtok(NULL, " ");
232 user = strtok(NULL, " ");
233 stat = strtok(NULL, " ");
234
235 if (!stat)
236 continue;
237
238 if (!*(stat + 1))
239 *(stat + 1) = ' ';
240
241 if (!*(stat + 2))
242 *(stat + 2) = ' ';
243
244 *(stat + 3) = 0;
245
246 vsz = strtok(stat + 4, " ");
247 pvsz = strtok(NULL, " ");
248 pcpu = strtok(NULL, " ");
249 cmd = strtok(NULL, "\n");
250
251 if (!cmd)
252 continue;
253
254 d = blobmsg_open_table(&buf, NULL);
255
256 blobmsg_add_u32(&buf, "pid", atoi(pid));
257 blobmsg_add_u32(&buf, "ppid", atoi(ppid));
258 blobmsg_add_string(&buf, "user", user);
259 blobmsg_add_string(&buf, "stat", stat);
260 blobmsg_add_u32(&buf, "vsize", atoi(vsz) * 1024);
261 blobmsg_add_u32(&buf, "vsize_percent", atoi(pvsz));
262 blobmsg_add_u32(&buf, "cpu_percent", atoi(pcpu));
263 blobmsg_add_string(&buf, "command", cmd);
264
265 blobmsg_close_table(&buf, d);
266 }
267
268 fclose(top);
269 blobmsg_close_array(&buf, c);
270
271 ubus_send_reply(ctx, req, buf.head);
272 return 0;
273 }
274
275 static int
276 rpc_luci2_process_signal(struct ubus_context *ctx, struct ubus_object *obj,
277 struct ubus_request_data *req, const char *method,
278 struct blob_attr *msg)
279 {
280 int pid, sig;
281 struct blob_attr *tb[__RPC_S_MAX];
282
283 blobmsg_parse(rpc_signal_policy, __RPC_S_MAX, tb,
284 blob_data(msg), blob_len(msg));
285
286 if (!tb[RPC_S_SIGNAL] || !tb[RPC_S_PID])
287 {
288 errno = EINVAL;
289 return rpc_errno_status();
290 }
291
292 pid = blobmsg_get_u32(tb[RPC_S_PID]);
293 sig = blobmsg_get_u32(tb[RPC_S_SIGNAL]);
294
295 if (kill(pid, sig))
296 return rpc_errno_status();
297
298 return 0;
299 }
300
301 static int
302 rpc_luci2_init_list(struct ubus_context *ctx, struct ubus_object *obj,
303 struct ubus_request_data *req, const char *method,
304 struct blob_attr *msg)
305 {
306 int n;
307 void *c, *t;
308 char *p, path[PATH_MAX];
309 struct stat s;
310 struct dirent *e;
311 FILE *f;
312 DIR *d;
313
314 if (!(d = opendir("/etc/init.d")))
315 return rpc_errno_status();
316
317 blob_buf_init(&buf, 0);
318 c = blobmsg_open_array(&buf, "initscripts");
319
320 while ((e = readdir(d)) != NULL)
321 {
322 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s", e->d_name);
323
324 if (stat(path, &s) || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR))
325 continue;
326
327 if ((f = fopen(path, "r")) != NULL)
328 {
329 n = -1;
330 p = fgets(path, sizeof(path) - 1, f);
331
332 if (!p || !strstr(p, "/etc/rc.common"))
333 goto skip;
334
335 t = blobmsg_open_table(&buf, NULL);
336
337 blobmsg_add_string(&buf, "name", e->d_name);
338
339 while (fgets(path, sizeof(path) - 1, f))
340 {
341 p = strtok(path, "= \t");
342
343 if (!strcmp(p, "START") && !!(p = strtok(NULL, "= \t\n")))
344 {
345 n = atoi(p);
346 blobmsg_add_u32(&buf, "start", n);
347 }
348 else if (!strcmp(p, "STOP") && !!(p = strtok(NULL, "= \t\n")))
349 {
350 blobmsg_add_u32(&buf, "stop", atoi(p));
351 break;
352 }
353 }
354
355 if (n > -1)
356 {
357 snprintf(path, sizeof(path) - 1, "/etc/rc.d/S%02d%s",
358 n, e->d_name);
359
360 blobmsg_add_u8(&buf, "enabled",
361 (!stat(path, &s) && (s.st_mode & S_IXUSR)));
362 }
363 else
364 {
365 blobmsg_add_u8(&buf, "enabled", 0);
366 }
367
368 blobmsg_close_table(&buf, t);
369
370 skip:
371 fclose(f);
372 }
373 }
374
375 closedir(d);
376 blobmsg_close_array(&buf, c);
377
378 ubus_send_reply(ctx, req, buf.head);
379 return 0;
380 }
381
382 static int
383 rpc_luci2_init_action(struct ubus_context *ctx, struct ubus_object *obj,
384 struct ubus_request_data *req, const char *method,
385 struct blob_attr *msg)
386 {
387 int fd;
388 pid_t pid;
389 struct stat s;
390 char path[PATH_MAX];
391 const char *action;
392 struct blob_attr *tb[__RPC_I_MAX];
393
394 blobmsg_parse(rpc_init_policy, __RPC_I_MAX, tb,
395 blob_data(msg), blob_len(msg));
396
397 if (!tb[RPC_I_NAME] || !tb[RPC_I_ACTION])
398 return UBUS_STATUS_INVALID_ARGUMENT;
399
400 action = blobmsg_data(tb[RPC_I_ACTION]);
401
402 if (strcmp(action, "start") && strcmp(action, "stop") &&
403 strcmp(action, "reload") && strcmp(action, "restart") &&
404 strcmp(action, "enable") && strcmp(action, "disable"))
405 return UBUS_STATUS_INVALID_ARGUMENT;
406
407 snprintf(path, sizeof(path) - 1, "/etc/init.d/%s",
408 (char *)blobmsg_data(tb[RPC_I_NAME]));
409
410 if (stat(path, &s))
411 return rpc_errno_status();
412
413 if (!(s.st_mode & S_IXUSR))
414 return UBUS_STATUS_PERMISSION_DENIED;
415
416 switch ((pid = fork()))
417 {
418 case -1:
419 return rpc_errno_status();
420
421 case 0:
422 uloop_done();
423
424 if ((fd = open("/dev/null", O_RDWR)) > -1)
425 {
426 dup2(fd, 0);
427 dup2(fd, 1);
428 dup2(fd, 2);
429
430 close(fd);
431 }
432
433 chdir("/");
434
435 if (execl(path, path, action, NULL))
436 return rpc_errno_status();
437
438 default:
439 return 0;
440 }
441 }
442
443
444 static FILE *
445 dnsmasq_leasefile(void)
446 {
447 FILE *leases = NULL;
448 struct uci_package *p;
449 struct uci_element *e;
450 struct uci_section *s;
451 struct uci_ptr ptr = {
452 .package = "dhcp",
453 .section = NULL,
454 .option = "leasefile"
455 };
456
457 uci_load(cursor, ptr.package, &p);
458
459 if (!p)
460 return NULL;
461
462 uci_foreach_element(&p->sections, e)
463 {
464 s = uci_to_section(e);
465
466 if (strcmp(s->type, "dnsmasq"))
467 continue;
468
469 ptr.section = e->name;
470 uci_lookup_ptr(cursor, &ptr, NULL, true);
471 break;
472 }
473
474 if (ptr.o && ptr.o->type == UCI_TYPE_STRING)
475 leases = fopen(ptr.o->v.string, "r");
476
477 uci_unload(cursor, p);
478
479 return leases;
480 }
481
482 static int
483 rpc_luci2_network_leases(struct ubus_context *ctx, struct ubus_object *obj,
484 struct ubus_request_data *req, const char *method,
485 struct blob_attr *msg)
486 {
487 FILE *leases;
488 void *c, *d;
489 char line[128];
490 char *ts, *mac, *addr, *name;
491 time_t now = time(NULL);
492
493 blob_buf_init(&buf, 0);
494 c = blobmsg_open_array(&buf, "leases");
495
496 leases = dnsmasq_leasefile();
497
498 if (!leases)
499 goto out;
500
501 while (fgets(line, sizeof(line) - 1, leases))
502 {
503 ts = strtok(line, " \t");
504 mac = strtok(NULL, " \t");
505 addr = strtok(NULL, " \t");
506 name = strtok(NULL, " \t");
507
508 if (!ts || !mac || !addr || !name)
509 continue;
510
511 if (strchr(addr, ':'))
512 continue;
513
514 d = blobmsg_open_table(&buf, NULL);
515
516 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
517 blobmsg_add_string(&buf, "macaddr", mac);
518 blobmsg_add_string(&buf, "ipaddr", addr);
519
520 if (strcmp(name, "*"))
521 blobmsg_add_string(&buf, "hostname", name);
522
523 blobmsg_close_table(&buf, d);
524 }
525
526 fclose(leases);
527
528 out:
529 blobmsg_close_array(&buf, c);
530 ubus_send_reply(ctx, req, buf.head);
531
532 return 0;
533 }
534
535 static int
536 rpc_luci2_network_leases6(struct ubus_context *ctx, struct ubus_object *obj,
537 struct ubus_request_data *req, const char *method,
538 struct blob_attr *msg)
539 {
540 FILE *leases;
541 void *c, *d;
542 char line[128];
543 char *ts, *mac, *addr, *name, *duid;
544 time_t now = time(NULL);
545
546 blob_buf_init(&buf, 0);
547 c = blobmsg_open_array(&buf, "leases");
548
549 leases = fopen("/tmp/hosts/6relayd", "r");
550
551 if (leases)
552 {
553 while (fgets(line, sizeof(line) - 1, leases))
554 {
555 if (strncmp(line, "# ", 2))
556 continue;
557
558 strtok(line + 2, " \t"); /* iface */
559
560 duid = strtok(NULL, " \t");
561
562 strtok(NULL, " \t"); /* iaid */
563
564 name = strtok(NULL, " \t");
565 ts = strtok(NULL, " \t");
566
567 strtok(NULL, " \t"); /* id */
568 strtok(NULL, " \t"); /* length */
569
570 addr = strtok(NULL, " \t\n");
571
572 if (!addr)
573 continue;
574
575 d = blobmsg_open_table(&buf, NULL);
576
577 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
578 blobmsg_add_string(&buf, "duid", duid);
579 blobmsg_add_string(&buf, "ip6addr", addr);
580
581 if (strcmp(name, "-"))
582 blobmsg_add_string(&buf, "hostname", name);
583
584 blobmsg_close_array(&buf, d);
585 }
586
587 fclose(leases);
588 }
589 else
590 {
591 leases = dnsmasq_leasefile();
592
593 if (!leases)
594 goto out;
595
596 while (fgets(line, sizeof(line) - 1, leases))
597 {
598 ts = strtok(line, " \t");
599 mac = strtok(NULL, " \t");
600 addr = strtok(NULL, " \t");
601 name = strtok(NULL, " \t");
602 duid = strtok(NULL, " \t\n");
603
604 if (!ts || !mac || !addr || !duid)
605 continue;
606
607 if (!strchr(addr, ':'))
608 continue;
609
610 d = blobmsg_open_table(&buf, NULL);
611
612 blobmsg_add_u32(&buf, "expires", atoi(ts) - now);
613 blobmsg_add_string(&buf, "macaddr", mac);
614 blobmsg_add_string(&buf, "ip6addr", addr);
615
616 if (strcmp(name, "*"))
617 blobmsg_add_string(&buf, "hostname", name);
618
619 if (strcmp(duid, "*"))
620 blobmsg_add_string(&buf, "duid", name);
621
622 blobmsg_close_table(&buf, d);
623 }
624
625 fclose(leases);
626 }
627
628 out:
629 blobmsg_close_array(&buf, c);
630 ubus_send_reply(ctx, req, buf.head);
631
632 return 0;
633 }
634
635 static int
636 rpc_luci2_network_ct_count(struct ubus_context *ctx, struct ubus_object *obj,
637 struct ubus_request_data *req, const char *method,
638 struct blob_attr *msg)
639 {
640 FILE *f;
641 char line[128];
642
643 blob_buf_init(&buf, 0);
644
645 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_count", "r")) != NULL)
646 {
647 if (fgets(line, sizeof(line) - 1, f))
648 blobmsg_add_u32(&buf, "count", atoi(line));
649
650 fclose(f);
651 }
652
653 if ((f = fopen("/proc/sys/net/netfilter/nf_conntrack_max", "r")) != NULL)
654 {
655 if (fgets(line, sizeof(line) - 1, f))
656 blobmsg_add_u32(&buf, "limit", atoi(line));
657
658 fclose(f);
659 }
660
661 ubus_send_reply(ctx, req, buf.head);
662
663 return 0;
664 }
665
666 static int
667 rpc_luci2_network_ct_table(struct ubus_context *ctx, struct ubus_object *obj,
668 struct ubus_request_data *req, const char *method,
669 struct blob_attr *msg)
670 {
671 FILE *f;
672 int i;
673 void *c, *d;
674 char *p, line[512];
675 bool seen[6];
676
677 blob_buf_init(&buf, 0);
678 c = blobmsg_open_array(&buf, "entries");
679
680 if ((f = fopen("/proc/net/nf_conntrack", "r")) != NULL)
681 {
682 while (fgets(line, sizeof(line) - 1, f))
683 {
684 d = blobmsg_open_table(&buf, NULL);
685 memset(seen, 0, sizeof(seen));
686
687 for (i = 0, p = strtok(line, " "); p; i++, p = strtok(NULL, " "))
688 {
689 if (i == 0)
690 blobmsg_add_u8(&buf, "ipv6", !strcmp(p, "ipv6"));
691 else if (i == 3)
692 blobmsg_add_u32(&buf, "protocol", atoi(p));
693 else if (i == 4)
694 blobmsg_add_u32(&buf, "expires", atoi(p));
695 else if (i >= 5)
696 {
697 if (*p == '[')
698 continue;
699
700 if (!seen[0] && !strncmp(p, "src=", 4))
701 {
702 blobmsg_add_string(&buf, "src", p + 4);
703 seen[0] = true;
704 }
705 else if (!seen[1] && !strncmp(p, "dst=", 4))
706 {
707 blobmsg_add_string(&buf, "dest", p + 4);
708 seen[1] = true;
709 }
710 else if (!seen[2] && !strncmp(p, "sport=", 6))
711 {
712 blobmsg_add_u32(&buf, "sport", atoi(p + 6));
713 seen[2] = true;
714 }
715 else if (!seen[3] && !strncmp(p, "dport=", 6))
716 {
717 blobmsg_add_u32(&buf, "dport", atoi(p + 6));
718 seen[3] = true;
719 }
720 else if (!strncmp(p, "packets=", 8))
721 {
722 blobmsg_add_u32(&buf,
723 seen[4] ? "tx_packets" : "rx_packets",
724 atoi(p + 8));
725 seen[4] = true;
726 }
727 else if (!strncmp(p, "bytes=", 6))
728 {
729 blobmsg_add_u32(&buf,
730 seen[5] ? "tx_bytes" : "rx_bytes",
731 atoi(p + 6));
732 seen[5] = true;
733 }
734 }
735 }
736
737 blobmsg_close_table(&buf, d);
738 }
739
740 fclose(f);
741 }
742
743 blobmsg_close_array(&buf, c);
744 ubus_send_reply(ctx, req, buf.head);
745
746 return 0;
747 }
748
749 static int
750 rpc_luci2_network_arp_table(struct ubus_context *ctx, struct ubus_object *obj,
751 struct ubus_request_data *req, const char *method,
752 struct blob_attr *msg)
753 {
754 FILE *f;
755 void *c, *d;
756 char *addr, *mac, *dev, line[128];
757
758 blob_buf_init(&buf, 0);
759 c = blobmsg_open_array(&buf, "entries");
760
761 if ((f = fopen("/proc/net/arp", "r")) != NULL)
762 {
763 /* skip header line */
764 fgets(line, sizeof(line) - 1, f);
765
766 while (fgets(line, sizeof(line) - 1, f))
767 {
768 addr = strtok(line, " \t");
769
770 strtok(NULL, " \t"); /* HW type */
771 strtok(NULL, " \t"); /* Flags */
772
773 mac = strtok(NULL, " \t");
774
775 strtok(NULL, " \t"); /* Mask */
776
777 dev = strtok(NULL, " \t\n");
778
779 if (!dev)
780 continue;
781
782 d = blobmsg_open_table(&buf, NULL);
783 blobmsg_add_string(&buf, "ipaddr", addr);
784 blobmsg_add_string(&buf, "macaddr", mac);
785 blobmsg_add_string(&buf, "device", dev);
786 blobmsg_close_table(&buf, d);
787 }
788
789 fclose(f);
790 }
791
792 blobmsg_close_array(&buf, c);
793 ubus_send_reply(ctx, req, buf.head);
794
795 return 0;
796 }
797
798 static void
799 put_hexaddr(const char *name, const char *s, const char *m)
800 {
801 int bits;
802 struct in_addr a;
803 char as[sizeof("255.255.255.255/32\0")];
804
805 a.s_addr = strtoul(s, NULL, 16);
806 inet_ntop(AF_INET, &a, as, sizeof(as));
807
808 if (m)
809 {
810 for (a.s_addr = ntohl(strtoul(m, NULL, 16)), bits = 0;
811 a.s_addr & 0x80000000;
812 a.s_addr <<= 1)
813 bits++;
814
815 sprintf(as + strlen(as), "/%u", bits);
816 }
817
818 blobmsg_add_string(&buf, name, as);
819 }
820
821 static int
822 rpc_luci2_network_routes(struct ubus_context *ctx, struct ubus_object *obj,
823 struct ubus_request_data *req, const char *method,
824 struct blob_attr *msg)
825 {
826 FILE *routes;
827 void *c, *d;
828 char *dst, *dmask, *next, *metric, *device;
829 char line[256];
830 unsigned int n;
831
832 if (!(routes = fopen("/proc/net/route", "r")))
833 return rpc_errno_status();
834
835 blob_buf_init(&buf, 0);
836 c = blobmsg_open_array(&buf, "routes");
837
838 /* skip header line */
839 fgets(line, sizeof(line) - 1, routes);
840
841 while (fgets(line, sizeof(line) - 1, routes))
842 {
843 device = strtok(line, "\t ");
844 dst = strtok(NULL, "\t ");
845 next = strtok(NULL, "\t ");
846
847 strtok(NULL, "\t "); /* flags */
848 strtok(NULL, "\t "); /* refcount */
849 strtok(NULL, "\t "); /* usecount */
850
851 metric = strtok(NULL, "\t ");
852 dmask = strtok(NULL, "\t ");
853
854 if (!dmask)
855 continue;
856
857 d = blobmsg_open_table(&buf, NULL);
858
859 put_hexaddr("target", dst, dmask);
860 put_hexaddr("nexthop", next, NULL);
861
862 n = strtoul(metric, NULL, 10);
863 blobmsg_add_u32(&buf, "metric", n);
864
865 blobmsg_add_string(&buf, "device", device);
866
867 blobmsg_close_table(&buf, d);
868 }
869
870 blobmsg_close_array(&buf, c);
871 fclose(routes);
872
873 ubus_send_reply(ctx, req, buf.head);
874 return 0;
875 }
876
877 static void
878 put_hex6addr(const char *name, const char *s, const char *m)
879 {
880 int i;
881 struct in6_addr a;
882 char as[INET6_ADDRSTRLEN + sizeof("/128")];
883
884 #define hex(x) \
885 (((x) <= '9') ? ((x) - '0') : \
886 (((x) <= 'F') ? ((x) - 'A' + 10) : \
887 ((x) - 'a' + 10)))
888
889 for (i = 0; i < 16; i++, s += 2)
890 a.s6_addr[i] = (16 * hex(*s)) + hex(*(s+1));
891
892 inet_ntop(AF_INET6, &a, as, sizeof(as));
893
894 if (m)
895 sprintf(as + strlen(as), "/%lu", strtoul(m, NULL, 16));
896
897 blobmsg_add_string(&buf, name, as);
898 }
899
900 static int
901 rpc_luci2_network_routes6(struct ubus_context *ctx, struct ubus_object *obj,
902 struct ubus_request_data *req, const char *method,
903 struct blob_attr *msg)
904 {
905 FILE *routes;
906 void *c, *d;
907 char *src, *smask, *dst, *dmask, *next, *metric, *flags, *device;
908 char line[256];
909 unsigned int n;
910
911 if (!(routes = fopen("/proc/net/ipv6_route", "r")))
912 return rpc_errno_status();
913
914 blob_buf_init(&buf, 0);
915 c = blobmsg_open_array(&buf, "routes");
916
917 while (fgets(line, sizeof(line) - 1, routes))
918 {
919 dst = strtok(line, " ");
920 dmask = strtok(NULL, " ");
921 src = strtok(NULL, " ");
922 smask = strtok(NULL, " ");
923 next = strtok(NULL, " ");
924 metric = strtok(NULL, " ");
925
926 strtok(NULL, " "); /* refcount */
927 strtok(NULL, " "); /* usecount */
928
929 flags = strtok(NULL, " ");
930 device = strtok(NULL, " \n");
931
932 if (!device)
933 continue;
934
935 n = strtoul(flags, NULL, 16);
936
937 if (!(n & 1))
938 continue;
939
940 d = blobmsg_open_table(&buf, NULL);
941
942 put_hex6addr("target", dst, dmask);
943 put_hex6addr("source", src, smask);
944 put_hex6addr("nexthop", next, NULL);
945
946 n = strtoul(metric, NULL, 16);
947 blobmsg_add_u32(&buf, "metric", n);
948
949 blobmsg_add_string(&buf, "device", device);
950
951 blobmsg_close_table(&buf, d);
952 }
953
954 blobmsg_close_array(&buf, c);
955 fclose(routes);
956
957 ubus_send_reply(ctx, req, buf.head);
958 return 0;
959 }
960
961
962 int rpc_luci2_api_init(struct ubus_context *ctx)
963 {
964 int rv = 0;
965
966 static const struct ubus_method luci2_system_methods[] = {
967 UBUS_METHOD_NOARG("syslog", rpc_luci2_system_log),
968 UBUS_METHOD_NOARG("dmesg", rpc_luci2_system_dmesg),
969 UBUS_METHOD_NOARG("process_list", rpc_luci2_process_list),
970 UBUS_METHOD("process_signal", rpc_luci2_process_signal,
971 rpc_signal_policy),
972 UBUS_METHOD_NOARG("init_list", rpc_luci2_init_list),
973 UBUS_METHOD("init_action", rpc_luci2_init_action,
974 rpc_init_policy)
975 };
976
977 static struct ubus_object_type luci2_system_type =
978 UBUS_OBJECT_TYPE("luci-rpc-luci2-system", luci2_system_methods);
979
980 static struct ubus_object system_obj = {
981 .name = "luci2.system",
982 .type = &luci2_system_type,
983 .methods = luci2_system_methods,
984 .n_methods = ARRAY_SIZE(luci2_system_methods),
985 };
986
987
988 static const struct ubus_method luci2_network_methods[] = {
989 UBUS_METHOD_NOARG("conntrack_count", rpc_luci2_network_ct_count),
990 UBUS_METHOD_NOARG("conntrack_table", rpc_luci2_network_ct_table),
991 UBUS_METHOD_NOARG("arp_table", rpc_luci2_network_arp_table),
992 UBUS_METHOD_NOARG("dhcp_leases", rpc_luci2_network_leases),
993 UBUS_METHOD_NOARG("dhcp6_leases", rpc_luci2_network_leases6),
994 UBUS_METHOD_NOARG("routes", rpc_luci2_network_routes),
995 UBUS_METHOD_NOARG("routes6", rpc_luci2_network_routes6),
996 };
997
998 static struct ubus_object_type luci2_network_type =
999 UBUS_OBJECT_TYPE("luci-rpc-luci2-network", luci2_network_methods);
1000
1001 static struct ubus_object network_obj = {
1002 .name = "luci2.network",
1003 .type = &luci2_network_type,
1004 .methods = luci2_network_methods,
1005 .n_methods = ARRAY_SIZE(luci2_network_methods),
1006 };
1007
1008 cursor = uci_alloc_context();
1009
1010 if (!cursor)
1011 return UBUS_STATUS_UNKNOWN_ERROR;
1012
1013 rv |= ubus_add_object(ctx, &system_obj);
1014 rv |= ubus_add_object(ctx, &network_obj);
1015
1016 return rv;
1017 }