proto-shell: add dns search domains
[project/netifd.git] / proto-shell.c
1 #define _GNU_SOURCE
2
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <glob.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <signal.h>
10
11 #include <arpa/inet.h>
12 #include <netinet/in.h>
13
14 #include <libubox/blobmsg_json.h>
15
16 #include "netifd.h"
17 #include "interface.h"
18 #include "interface-ip.h"
19 #include "proto.h"
20
21 static int proto_fd;
22
23 struct proto_shell_handler {
24         struct list_head list;
25         struct proto_handler proto;
26         struct config_param_list config;
27         char *config_buf;
28         char script_name[];
29 };
30
31 struct proto_shell_state {
32         struct interface_proto_state proto;
33         struct proto_shell_handler *handler;
34         struct blob_attr *config;
35
36         struct device_user l3_dev;
37
38         struct uloop_timeout setup_timeout;
39         struct uloop_process setup_task;
40         struct uloop_process teardown_task;
41         bool teardown_pending;
42         bool teardown_wait_task;
43
44         struct uloop_process proto_task;
45 };
46
47 static void
48 kill_process(struct uloop_process *proc)
49 {
50         if (!proc->pending)
51                 return;
52
53         kill(proc->pid, SIGTERM);
54         uloop_process_delete(proc);
55 }
56
57 static int
58 start_process(const char **argv, struct uloop_process *proc)
59 {
60         int pid;
61
62         kill_process(proc);
63
64         if ((pid = fork()) < 0)
65                 return -1;
66
67         if (!pid) {
68                 fchdir(proto_fd);
69                 execvp(argv[0], (char **) argv);
70                 exit(127);
71         }
72
73         if (pid < 0)
74                 return -1;
75
76         proc->pid = pid;
77         uloop_process_add(proc);
78
79         return 0;
80 }
81
82 static int
83 proto_shell_handler(struct interface_proto_state *proto,
84                     enum interface_proto_cmd cmd, bool force)
85 {
86         struct proto_shell_state *state;
87         struct proto_shell_handler *handler;
88         struct uloop_process *proc;
89         const char *argv[6];
90         const char *action;
91         char *config;
92         int ret, i = 0;
93
94         state = container_of(proto, struct proto_shell_state, proto);
95         handler = state->handler;
96
97         if (cmd == PROTO_CMD_SETUP) {
98                 action = "setup";
99                 proc = &state->setup_task;
100         } else {
101                 action = "teardown";
102                 proc = &state->teardown_task;
103                 if (state->setup_task.pending && !state->teardown_wait_task) {
104                         uloop_timeout_set(&state->setup_timeout, 1000);
105                         kill(state->setup_task.pid, SIGTERM);
106                         state->teardown_pending = true;
107                         return 0;
108                 }
109         }
110
111         config = blobmsg_format_json(state->config, true);
112         if (!config)
113                 return -1;
114
115         argv[i++] = handler->script_name;
116         argv[i++] = handler->proto.name;
117         argv[i++] = action;
118         argv[i++] = proto->iface->name;
119         argv[i++] = config;
120         if (proto->iface->main_dev.dev)
121                 argv[i++] = proto->iface->main_dev.dev->ifname;
122         argv[i] = NULL;
123
124         ret = start_process(argv, proc);
125         free(config);
126
127         return ret;
128 }
129
130 static void
131 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
132 {
133         struct proto_shell_state *state;
134
135         state = container_of(timeout, struct proto_shell_state, setup_timeout);
136         kill(state->setup_task.pid, SIGKILL);
137 }
138
139 static void
140 proto_shell_setup_cb(struct uloop_process *p, int ret)
141 {
142         struct proto_shell_state *state;
143
144         state = container_of(p, struct proto_shell_state, setup_task);
145         uloop_timeout_cancel(&state->setup_timeout);
146         if (state->teardown_pending) {
147                 state->teardown_pending = false;
148                 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
149         }
150 }
151
152 static void
153 proto_shell_teardown_cb(struct uloop_process *p, int ret)
154 {
155         struct proto_shell_state *state;
156
157         state = container_of(p, struct proto_shell_state, teardown_task);
158
159         if (state->teardown_wait_task)
160                 return;
161
162         kill_process(&state->proto_task);
163
164         if (state->l3_dev.dev)
165                 device_remove_user(&state->l3_dev);
166
167         state->proto.proto_event(&state->proto, IFPEV_DOWN);
168 }
169
170 static void
171 proto_shell_task_cb(struct uloop_process *p, int ret)
172 {
173         struct proto_shell_state *state;
174         bool teardown_wait_task;
175
176         state = container_of(p, struct proto_shell_state, proto_task);
177
178         teardown_wait_task = state->teardown_wait_task;
179         state->teardown_wait_task = false;
180         if (state->teardown_pending || state->teardown_task.pending)
181                 return;
182
183         if (teardown_wait_task) {
184                 proto_shell_teardown_cb(&state->teardown_task, 0);
185                 return;
186         }
187
188         state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
189         proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
190 }
191
192 static void
193 proto_shell_free(struct interface_proto_state *proto)
194 {
195         struct proto_shell_state *state;
196
197         state = container_of(proto, struct proto_shell_state, proto);
198         free(state->config);
199         free(state);
200 }
201
202 static void
203 proto_shell_parse_addr_list(struct interface *iface, struct blob_attr *attr,
204                             bool v6, bool external)
205 {
206         struct device_addr *addr;
207         struct blob_attr *cur;
208         int rem;
209
210         blobmsg_for_each_attr(cur, attr, rem) {
211                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
212                         DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur));
213                         continue;
214                 }
215
216                 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
217                 if (!addr) {
218                         DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
219                         continue;
220                 }
221
222                 if (external)
223                         addr->flags |= DEVADDR_EXTERNAL;
224
225                 vlist_add(&iface->proto_addr, &addr->node);
226         }
227 }
228
229 enum {
230         ROUTE_TARGET,
231         ROUTE_MASK,
232         ROUTE_GATEWAY,
233         ROUTE_DEVICE,
234         __ROUTE_LAST
235 };
236
237 static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
238         [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
239         [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
240         [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
241         [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
242 };
243
244 static void
245 parse_route(struct interface *iface, struct blob_attr *attr, bool v6)
246 {
247         struct blob_attr *tb[__ROUTE_LAST], *cur;
248         struct device_route *route;
249         int af = v6 ? AF_INET6 : AF_INET;
250
251         blobmsg_parse(route_attr, __ROUTE_LAST, tb, blobmsg_data(attr), blobmsg_data_len(attr));
252
253         if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
254                 return;
255
256         route = calloc(1, sizeof(*route));
257         if (!route)
258                 return;
259
260         route->mask = v6 ? 128 : 32;
261         if ((cur = tb[ROUTE_MASK]) != NULL) {
262                 route->mask = blobmsg_get_u32(cur);
263                 if (route->mask > v6 ? 128 : 32)
264                         goto error;
265         }
266
267         if ((cur = tb[ROUTE_TARGET]) != NULL) {
268                 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
269                         DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
270                         goto error;
271                 }
272         }
273
274         if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
275                 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
276                         DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
277                         goto error;
278                 }
279         }
280
281         if ((cur = tb[ROUTE_DEVICE]) != NULL)
282                 route->device = device_get(blobmsg_data(cur), true);
283
284         vlist_add(&iface->proto_route, &route->node);
285         return;
286
287 error:
288         free(route);
289 }
290
291 static void
292 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
293                              bool v6)
294 {
295         struct blob_attr *cur;
296         int rem;
297
298         blobmsg_for_each_attr(cur, attr, rem) {
299                 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
300                         DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
301                         continue;
302                 }
303
304                 parse_route(iface, cur, v6);
305         }
306 }
307
308
309 enum {
310         NOTIFY_ACTION,
311         NOTIFY_COMMAND,
312         NOTIFY_SIGNAL,
313         NOTIFY_LINK_UP,
314         NOTIFY_IFNAME,
315         NOTIFY_ADDR_EXT,
316         NOTIFY_IPADDR,
317         NOTIFY_IP6ADDR,
318         NOTIFY_ROUTES,
319         NOTIFY_ROUTES6,
320         NOTIFY_DNS,
321         NOTIFY_DNS_SEARCH,
322         __NOTIFY_LAST
323 };
324
325 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
326         [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
327         [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
328         [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
329         [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
330         [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
331         [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
332         [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
333         [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
334         [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
335         [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
336         [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
337         [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
338 };
339
340 static int
341 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
342 {
343         struct blob_attr *cur;
344         bool addr_ext = false;
345         bool up;
346
347         if (!tb[NOTIFY_LINK_UP])
348                 return UBUS_STATUS_INVALID_ARGUMENT;
349
350         up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
351         if (!up) {
352                 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
353                 return 0;
354         }
355
356         if (!tb[NOTIFY_IFNAME]) {
357                 if (!state->proto.iface->main_dev.dev)
358                         return UBUS_STATUS_INVALID_ARGUMENT;
359         } else if (!state->l3_dev.dev) {
360                 device_add_user(&state->l3_dev,
361                         device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
362                 device_claim(&state->l3_dev);
363                 state->proto.iface->l3_dev = &state->l3_dev;
364         }
365
366         interface_ip_update_start(state->proto.iface);
367
368         if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
369                 addr_ext = blobmsg_get_bool(cur);
370
371         if ((cur = tb[NOTIFY_IPADDR]) != NULL)
372                 proto_shell_parse_addr_list(state->proto.iface, cur, false, addr_ext);
373
374         if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
375                 proto_shell_parse_addr_list(state->proto.iface, cur, true, addr_ext);
376
377         if ((cur = tb[NOTIFY_ROUTES]) != NULL)
378                 proto_shell_parse_route_list(state->proto.iface, cur, false);
379
380         if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
381                 proto_shell_parse_route_list(state->proto.iface, cur, true);
382
383         if ((cur = tb[NOTIFY_DNS]) != NULL)
384                 interface_add_dns_server_list(state->proto.iface, cur);
385
386         if ((cur = tb[NOTIFY_DNS_SEARCH]) != NULL)
387                 interface_add_dns_search_list(state->proto.iface, cur);
388
389         interface_ip_update_complete(state->proto.iface);
390
391         state->proto.proto_event(&state->proto, IFPEV_UP);
392
393         return 0;
394 }
395
396 static int
397 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
398 {
399         struct blob_attr *cur;
400         char *argv[64];
401         int argc = 0;
402         int rem;
403
404         if (!tb[NOTIFY_COMMAND])
405                 goto error;
406
407         blobmsg_for_each_attr(cur, tb[NOTIFY_COMMAND], rem) {
408                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
409                         goto error;
410
411                 if (!blobmsg_check_attr(cur, NULL))
412                         goto error;
413
414                 argv[argc++] = blobmsg_data(cur);
415                 if (argc == ARRAY_SIZE(argv) - 1)
416                         goto error;
417         }
418         argv[argc] = NULL;
419         start_process((const char **) argv, &state->proto_task);
420
421         return 0;
422
423 error:
424         return UBUS_STATUS_INVALID_ARGUMENT;
425 }
426
427 static int
428 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
429 {
430         unsigned int signal = ~0;
431
432         if (tb[NOTIFY_SIGNAL])
433                 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
434
435         if (signal > 31)
436                 signal = SIGTERM;
437
438         if (state->proto_task.pending) {
439                 kill(state->proto_task.pid, signal);
440                 state->teardown_wait_task = true;
441         }
442
443         return 0;
444 }
445
446 static int
447 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
448 {
449         struct proto_shell_state *state;
450         struct blob_attr *tb[__NOTIFY_LAST];
451
452         state = container_of(proto, struct proto_shell_state, proto);
453
454         blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
455         if (!tb[NOTIFY_ACTION])
456                 return UBUS_STATUS_INVALID_ARGUMENT;
457
458         switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
459         case 0:
460                 return proto_shell_update_link(state, tb);
461         case 1:
462                 return proto_shell_run_command(state, tb);
463         case 2:
464                 return proto_shell_kill_command(state, tb);
465         default:
466                 return UBUS_STATUS_INVALID_ARGUMENT;
467         }
468 }
469
470 struct interface_proto_state *
471 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
472                    struct blob_attr *attr)
473 {
474         struct proto_shell_state *state;
475
476         state = calloc(1, sizeof(*state));
477         state->config = malloc(blob_pad_len(attr));
478         if (!state->config)
479                 goto error;
480
481         memcpy(state->config, attr, blob_pad_len(attr));
482         state->proto.free = proto_shell_free;
483         state->proto.notify = proto_shell_notify;
484         state->proto.cb = proto_shell_handler;
485         state->setup_timeout.cb = proto_shell_setup_timeout_cb;
486         state->setup_task.cb = proto_shell_setup_cb;
487         state->teardown_task.cb = proto_shell_teardown_cb;
488         state->proto_task.cb = proto_shell_task_cb;
489         state->handler = container_of(h, struct proto_shell_handler, proto);
490
491         return &state->proto;
492
493 error:
494         free(state);
495         return NULL;
496 }
497
498 static json_object *
499 check_type(json_object *obj, json_type type)
500 {
501         if (!obj)
502                 return NULL;
503
504         if (json_object_get_type(obj) != type)
505                 return NULL;
506
507         return obj;
508 }
509
510 static inline json_object *
511 get_field(json_object *obj, const char *name, json_type type)
512 {
513         return check_type(json_object_object_get(obj, name), type);
514 }
515
516 static char *
517 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
518 {
519         struct blobmsg_policy *attrs;
520         char *str_buf, *str_cur;
521         int str_len = 0;
522         int i;
523
524         config->n_params = json_object_array_length(obj);
525         attrs = calloc(1, sizeof(*attrs) * config->n_params);
526         if (!attrs)
527                 return NULL;
528
529         config->params = attrs;
530         for (i = 0; i < config->n_params; i++) {
531                 json_object *cur, *name, *type;
532
533                 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
534                 if (!cur)
535                         goto error;
536
537                 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
538                 if (!name)
539                         goto error;
540
541                 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
542                 if (!type)
543                         goto error;
544
545                 attrs[i].name = json_object_get_string(name);
546                 attrs[i].type = json_object_get_int(type);
547                 if (attrs[i].type > BLOBMSG_TYPE_LAST)
548                         goto error;
549
550                 str_len += strlen(attrs[i].name) + 1;
551         }
552
553         str_buf = malloc(str_len);
554         if (!str_buf)
555                 goto error;
556
557         str_cur = str_buf;
558         for (i = 0; i < config->n_params; i++) {
559                 const char *name = attrs[i].name;
560
561                 attrs[i].name = str_cur;
562                 str_cur += sprintf(str_cur, "%s", name) + 1;
563         }
564
565         return str_buf;
566
567 error:
568         free(attrs);
569         config->n_params = 0;
570         return NULL;
571 }
572
573 static void
574 proto_shell_add_handler(const char *script, json_object *obj)
575 {
576         struct proto_shell_handler *handler;
577         struct proto_handler *proto;
578         json_object *config, *tmp;
579         const char *name;
580         char *str;
581
582         if (!check_type(obj, json_type_object))
583                 return;
584
585         tmp = get_field(obj, "name", json_type_string);
586         if (!tmp)
587                 return;
588
589         name = json_object_get_string(tmp);
590
591         handler = calloc(1, sizeof(*handler) +
592                          strlen(script) + 1 +
593                          strlen(name) + 1);
594         if (!handler)
595                 return;
596
597         strcpy(handler->script_name, script);
598
599         str = handler->script_name + strlen(handler->script_name) + 1;
600         strcpy(str, name);
601
602         proto = &handler->proto;
603         proto->name = str;
604         proto->config_params = &handler->config;
605         proto->attach = proto_shell_attach;
606
607         tmp = get_field(obj, "no-device", json_type_boolean);
608         if (tmp && json_object_get_boolean(tmp))
609                 handler->proto.flags |= PROTO_FLAG_NODEV;
610
611         config = get_field(obj, "config", json_type_array);
612         if (config)
613                 handler->config_buf = proto_shell_parse_config(&handler->config, config);
614
615         DPRINTF("Add handler for script %s: %s\n", script, proto->name);
616         add_proto_handler(proto);
617 }
618
619 static void proto_shell_add_script(const char *name)
620 {
621         struct json_tokener *tok = NULL;
622         json_object *obj;
623         static char buf[512];
624         char *start, *end, *cmd;
625         FILE *f;
626         int buflen, len;
627
628 #define DUMP_SUFFIX     " '' dump"
629
630         cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
631         sprintf(cmd, "%s" DUMP_SUFFIX, name);
632
633         f = popen(cmd, "r");
634         if (!f)
635                 return;
636
637         do {
638                 buflen = fread(buf, 1, sizeof(buf) - 1, f);
639                 if (buflen <= 0)
640                         continue;
641
642                 start = buf;
643                 len = buflen;
644                 do {
645                         end = memchr(start, '\n', len);
646                         if (end)
647                                 len = end - start;
648
649                         if (!tok)
650                                 tok = json_tokener_new();
651
652                         obj = json_tokener_parse_ex(tok, start, len);
653                         if (!is_error(obj)) {
654                                 proto_shell_add_handler(name, obj);
655                                 json_object_put(obj);
656                                 json_tokener_free(tok);
657                                 tok = NULL;
658                         }
659
660                         if (end) {
661                                 start = end + 1;
662                                 len = buflen - (start - buf);
663                         }
664                 } while (len > 0);
665         } while (!feof(f) && !ferror(f));
666
667         if (tok)
668                 json_tokener_free(tok);
669
670         pclose(f);
671 }
672
673 void __init proto_shell_init(void)
674 {
675         glob_t g;
676         int main_fd;
677         int i;
678
679         main_fd = open(".", O_RDONLY | O_DIRECTORY);
680         if (main_fd < 0)
681                 return;
682
683         if (chdir(main_path)) {
684                 perror("chdir(main path)");
685                 goto close_cur;
686         }
687
688         if (chdir("./proto"))
689                 goto close_cur;
690
691         proto_fd = open(".", O_RDONLY | O_DIRECTORY);
692         if (proto_fd < 0)
693                 goto close_cur;
694
695         glob("./*.sh", 0, NULL, &g);
696         for (i = 0; i < g.gl_pathc; i++)
697                 proto_shell_add_script(g.gl_pathv[i]);
698
699 close_cur:
700         fchdir(main_fd);
701         close(main_fd);
702 }