]> git.decadent.org.uk Git - odhcp6c.git/blob - src/script.c
script: Launch script with correct action if last script call is terminated
[odhcp6c.git] / src / script.c
1 /**
2  * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License v2 as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14
15 #include <stdio.h>
16 #include <netdb.h>
17 #include <resolv.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <syslog.h>
21 #include <signal.h>
22 #include <unistd.h>
23 #include <inttypes.h>
24 #include <arpa/inet.h>
25 #include <sys/wait.h>
26 #include <netinet/in.h>
27
28 #include "odhcp6c.h"
29
30 static const char hexdigits[] = "0123456789abcdef";
31 static const int8_t hexvals[] = {
32     -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
33     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34     -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
36     -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38     -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40 };
41
42
43 static char action[16] = "";
44 static char *argv[4] = {NULL, NULL, action, NULL};
45 static volatile pid_t running = 0;
46 static time_t started;
47
48
49 static void script_sighandle(int signal)
50 {
51         if (signal == SIGCHLD) {
52                 pid_t child;
53                 while ((child = waitpid(-1, NULL, WNOHANG)) > 0)
54                         if (running == child)
55                                 running = 0;
56         }
57 }
58
59 int script_init(const char *path, const char *ifname)
60 {
61         argv[0] = (char*)path;
62         argv[1] = (char*)ifname;
63         signal(SIGCHLD, script_sighandle);
64         return 0;
65 }
66
67
68 ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src)
69 {
70         size_t c;
71         for (c = 0; c < len && src[0] && src[1]; ++c) {
72                 int8_t x = (int8_t)*src++;
73                 int8_t y = (int8_t)*src++;
74                 if (x < 0 || (x = hexvals[x]) < 0
75                                 || y < 0 || (y = hexvals[y]) < 0)
76                         return -1;
77                 dst[c] = x << 4 | y;
78                 while (((int8_t)*src) < 0 ||
79                                 (*src && hexvals[(uint8_t)*src] < 0))
80                         src++;
81         }
82
83         return c;
84 }
85
86
87 static void script_hexlify(char *dst, const uint8_t *src, size_t len) {
88         for (size_t i = 0; i < len; ++i) {
89                 *dst++ = hexdigits[src[i] >> 4];
90                 *dst++ = hexdigits[src[i] & 0x0f];
91         }
92         *dst = 0;
93 }
94
95
96 static void ipv6_to_env(const char *name,
97                 const struct in6_addr *addr, size_t cnt)
98 {
99         size_t buf_len = strlen(name);
100         char *buf = realloc(NULL, cnt * INET6_ADDRSTRLEN + buf_len + 2);
101         memcpy(buf, name, buf_len);
102         buf[buf_len++] = '=';
103         for (size_t i = 0; i < cnt; ++i) {
104                 inet_ntop(AF_INET6, &addr[i], &buf[buf_len], INET6_ADDRSTRLEN);
105                 buf_len += strlen(&buf[buf_len]);
106                 buf[buf_len++] = ' ';
107         }
108         buf[buf_len - 1] = '\0';
109         putenv(buf);
110 }
111
112
113 static void fqdn_to_env(const char *name, const uint8_t *fqdn, size_t len)
114 {
115         size_t buf_len = strlen(name);
116         size_t buf_size = len + buf_len + 2;
117         const uint8_t *fqdn_end = fqdn + len;
118         char *buf = realloc(NULL, len + buf_len + 2);
119         memcpy(buf, name, buf_len);
120         buf[buf_len++] = '=';
121         int l = 1;
122         while (l > 0 && fqdn < fqdn_end) {
123                 l = dn_expand(fqdn, fqdn_end, fqdn, &buf[buf_len], buf_size - buf_len);
124                 fqdn += l;
125                 buf_len += strlen(&buf[buf_len]);
126                 buf[buf_len++] = ' ';
127         }
128         buf[buf_len - 1] = '\0';
129         putenv(buf);
130 }
131
132 static void bin_to_env(uint8_t *opts, size_t len)
133 {
134         uint8_t *oend = opts + len, *odata;
135         uint16_t otype, olen;
136         dhcpv6_for_each_option(opts, oend, otype, olen, odata) {
137                 char *buf = realloc(NULL, 14 + (olen * 2));
138                 size_t buf_len = 0;
139
140                 snprintf(buf, 14, "OPTION_%hu=", otype);
141                 buf_len += strlen(buf);
142
143                 script_hexlify(&buf[buf_len], odata, olen);
144                 putenv(buf);
145         }
146 }
147
148 enum entry_type {
149         ENTRY_ADDRESS,
150         ENTRY_HOST,
151         ENTRY_ROUTE,
152         ENTRY_PREFIX
153 };
154
155 static void entry_to_env(const char *name, const void *data, size_t len, enum entry_type type)
156 {
157         size_t buf_len = strlen(name);
158         const struct odhcp6c_entry *e = data;
159         char *buf = realloc(NULL, buf_len + 2 + (len / sizeof(*e)) * 144);
160         memcpy(buf, name, buf_len);
161         buf[buf_len++] = '=';
162
163         for (size_t i = 0; i < len / sizeof(*e); ++i) {
164                 inet_ntop(AF_INET6, &e[i].target, &buf[buf_len], INET6_ADDRSTRLEN);
165                 buf_len += strlen(&buf[buf_len]);
166                 if (type != ENTRY_HOST) {
167                         buf_len += snprintf(&buf[buf_len], 6, "/%"PRIu16, e[i].length);
168                         if (type == ENTRY_ROUTE) {
169                                 buf[buf_len++] = ',';
170                                 if (!IN6_IS_ADDR_UNSPECIFIED(&e[i].router)) {
171                                         inet_ntop(AF_INET6, &e[i].router, &buf[buf_len], INET6_ADDRSTRLEN);
172                                         buf_len += strlen(&buf[buf_len]);
173                                 }
174                                 buf_len += snprintf(&buf[buf_len], 24, ",%u", e[i].valid);
175                                 buf_len += snprintf(&buf[buf_len], 12, ",%u", e[i].priority);
176                         } else {
177                                 buf_len += snprintf(&buf[buf_len], 24, ",%u,%u", e[i].preferred, e[i].valid);
178                         }
179
180                         if (type == ENTRY_PREFIX && ntohl(e[i].iaid) != 1)
181                                 buf_len += snprintf(&buf[buf_len], 16, ",class=%08x", ntohl(e[i].iaid));
182
183                         if (type == ENTRY_PREFIX && e[i].priority) {
184                                 // priority and router are abused for prefix exclusion
185                                 buf_len += snprintf(&buf[buf_len], 12, ",excluded=");
186                                 inet_ntop(AF_INET6, &e[i].router, &buf[buf_len], INET6_ADDRSTRLEN);
187                                 buf_len += strlen(&buf[buf_len]);
188                                 buf_len += snprintf(&buf[buf_len], 24, "/%u", e[i].priority);
189                         }
190                 }
191                 buf[buf_len++] = ' ';
192         }
193
194         buf[buf_len - 1] = '\0';
195         putenv(buf);
196 }
197
198
199 static void search_to_env(const char *name, const uint8_t *start, size_t len)
200 {
201         size_t buf_len = strlen(name);
202         char *buf = realloc(NULL, buf_len + 2 + len);
203         char *c = mempcpy(buf, name, buf_len);
204         *c++ = '=';
205
206         for (struct odhcp6c_entry *e = (struct odhcp6c_entry*)start;
207                                 (uint8_t*)e < &start[len] && &e->auxtarget[e->auxlen] <= &start[len];
208                                 e = (struct odhcp6c_entry*)(&e->auxtarget[e->auxlen])) {
209                 c = mempcpy(c, e->auxtarget, e->auxlen);
210                 *c++ = ' ';
211         }
212
213         c[-1] = '\0';
214         putenv(buf);
215 }
216
217
218 static void int_to_env(const char *name, int value)
219 {
220         size_t len = 12 + strlen(name);
221         char *buf = realloc(NULL, len);
222         snprintf(buf, len, "%s=%d", name, value);
223         putenv(buf);
224 }
225
226
227 static void s46_to_env_portparams(const uint8_t *data, size_t len, FILE *fp)
228 {
229         uint8_t *odata;
230         uint16_t otype, olen;
231         dhcpv6_for_each_option(data, &data[len], otype, olen, odata) {
232                 if (otype == DHCPV6_OPT_S46_PORTPARAMS &&
233                                 olen == sizeof(struct dhcpv6_s46_portparams)) {
234                         struct dhcpv6_s46_portparams *params = (void*)odata;
235                         fprintf(fp, "offset=%d,psidlen=%d,psid=%d,",
236                                         params->offset, params->psid_len, ntohs(params->psid));
237                 }
238         }
239 }
240
241
242 static void s46_to_env(enum odhcp6c_state state, const uint8_t *data, size_t len)
243 {
244         const char *name = (state == STATE_S46_MAPE) ? "MAPE" :
245                         (state == STATE_S46_MAPT) ? "MAPT" : "LW4O6";
246
247         if (len == 0)
248                 return;
249
250         char *str;
251         size_t strsize;
252
253         FILE *fp = open_memstream(&str, &strsize);
254         fputs(name, fp);
255         fputc('=', fp);
256
257         const char *type = (state == STATE_S46_MAPE) ? "map-e" :
258                         (state == STATE_S46_MAPT) ? "map-t" : "lw4o6";
259
260         uint8_t *odata;
261         uint16_t otype, olen;
262         dhcpv6_for_each_option(data, &data[len], otype, olen, odata) {
263                 struct dhcpv6_s46_rule *rule = (struct dhcpv6_s46_rule*)odata;
264                 struct dhcpv6_s46_v4v6bind *bind = (struct dhcpv6_s46_v4v6bind*)odata;
265
266                 if (state != STATE_S46_LW && otype == DHCPV6_OPT_S46_RULE &&
267                                 olen >= sizeof(struct dhcpv6_s46_rule)) {
268                         char buf4[INET_ADDRSTRLEN];
269                         char buf6[INET6_ADDRSTRLEN];
270                         struct in6_addr in6 = IN6ADDR_ANY_INIT;
271
272                         size_t prefix6len = rule->prefix6_len;
273                         prefix6len = (prefix6len % 8 == 0) ? prefix6len / 8 : prefix6len / 8 + 1;
274
275                         if (olen < sizeof(struct dhcpv6_s46_rule) + prefix6len)
276                                 continue;
277
278                         memcpy(&in6, rule->ipv6_prefix, prefix6len);
279
280                         inet_ntop(AF_INET, &rule->ipv4_prefix, buf4, sizeof(buf4));
281                         inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6));
282
283                         if (rule->flags & 1)
284                                 fputs("fmr,", fp);
285
286                         fprintf(fp, "type=%s,ealen=%d,prefix4len=%d,prefix6len=%d,ipv4prefix=%s,ipv6prefix=%s,",
287                                         type, rule->ea_len, rule->prefix4_len, rule->prefix6_len, buf4, buf6);
288
289                         s46_to_env_portparams(&rule->ipv6_prefix[prefix6len],
290                                         olen - sizeof(*rule) - prefix6len, fp);
291
292                         dhcpv6_for_each_option(data, &data[len], otype, olen, odata) {
293                                 if (state != STATE_S46_MAPT && otype == DHCPV6_OPT_S46_BR &&
294                                                 olen == sizeof(struct in6_addr)) {
295                                         inet_ntop(AF_INET6, odata, buf6, sizeof(buf6));
296                                         fprintf(fp, "br=%s,", buf6);
297                                 } else if (state == STATE_S46_MAPT && otype == DHCPV6_OPT_S46_DMR &&
298                                                 olen >= sizeof(struct dhcpv6_s46_dmr)) {
299                                         struct dhcpv6_s46_dmr *dmr = (struct dhcpv6_s46_dmr*)odata;
300                                         memset(&in6, 0, sizeof(in6));
301                                         size_t prefix6len = dmr->dmr_prefix6_len;
302                                         prefix6len = (prefix6len % 8 == 0) ? prefix6len / 8 : prefix6len / 8 + 1;
303
304                                         if (olen < sizeof(struct dhcpv6_s46_dmr) + prefix6len)
305                                                 continue;
306
307                                         memcpy(&in6, dmr->dmr_ipv6_prefix, prefix6len);
308                                         inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6));
309                                         fprintf(fp, "dmr=%s/%d,", buf6, dmr->dmr_prefix6_len);
310                                 }
311                         }
312
313                         fputc(' ', fp);
314                 } else if (state == STATE_S46_LW && otype == DHCPV6_OPT_S46_V4V6BIND &&
315                                 olen >= sizeof(struct dhcpv6_s46_v4v6bind)) {
316                         char buf4[INET_ADDRSTRLEN];
317                         char buf6[INET6_ADDRSTRLEN];
318                         struct in6_addr in6 = IN6ADDR_ANY_INIT;
319
320                         size_t prefix6len = bind->bindprefix6_len;
321                         prefix6len = (prefix6len % 8 == 0) ? prefix6len / 8 : prefix6len / 8 + 1;
322
323                         if (olen < sizeof(struct dhcpv6_s46_v4v6bind) + prefix6len)
324                                 continue;
325
326                         memcpy(&in6, bind->bind_ipv6_prefix, prefix6len);
327
328                         inet_ntop(AF_INET, &bind->ipv4_address, buf4, sizeof(buf4));
329                         inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6));
330
331                         fprintf(fp, "type=%s,prefix4len=32,prefix6len=%d,ipv4prefix=%s,ipv6prefix=%s,",
332                                         type, bind->bindprefix6_len, buf4, buf6);
333
334                         s46_to_env_portparams(&bind->bind_ipv6_prefix[prefix6len],
335                                         olen - sizeof(*bind) - prefix6len, fp);
336
337                         dhcpv6_for_each_option(data, &data[len], otype, olen, odata) {
338                                 if (otype == DHCPV6_OPT_S46_BR && olen == sizeof(struct in6_addr)) {
339                                         inet_ntop(AF_INET6, odata, buf6, sizeof(buf6));
340                                         fprintf(fp, "br=%s,", buf6);
341                                 }
342                         }
343
344                         fputc(' ', fp);
345                 }
346         }
347
348         fclose(fp);
349         putenv(str);
350 }
351
352
353 void script_call(const char *status, int delay, bool resume)
354 {
355         time_t now = odhcp6c_get_milli_time() / 1000;
356         bool running_script = false;
357
358         if (running) {
359                 kill(running, SIGTERM);
360                 delay -= now - started;
361                 running_script = true;
362         }
363
364         if (resume || !running_script || !action[0])
365                 strncpy(action, status, sizeof(action) - 1);
366
367         pid_t pid = fork();
368         if (pid > 0) {
369                 running = pid;
370                 started = now;
371
372                 if (!resume)
373                         action[0] = 0;
374         } else if (pid == 0) {
375                 size_t dns_len, search_len, custom_len, sntp_ip_len, ntp_ip_len, ntp_dns_len;
376                 size_t sip_ip_len, sip_fqdn_len, aftr_name_len, cer_len, addr_len;
377                 size_t s46_mapt_len, s46_mape_len, s46_lw_len, passthru_len;
378
379                 signal(SIGTERM, SIG_DFL);
380                 if (delay > 0) {
381                         sleep(delay);
382                         odhcp6c_expire();
383                 }
384
385                 struct in6_addr *addr = odhcp6c_get_state(STATE_SERVER_ADDR, &addr_len);
386                 struct in6_addr *dns = odhcp6c_get_state(STATE_DNS, &dns_len);
387                 uint8_t *search = odhcp6c_get_state(STATE_SEARCH, &search_len);
388                 uint8_t *custom = odhcp6c_get_state(STATE_CUSTOM_OPTS, &custom_len);
389                 struct in6_addr *sntp = odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len);
390                 struct in6_addr *ntp = odhcp6c_get_state(STATE_NTP_IP, &ntp_ip_len);
391                 uint8_t *ntp_dns = odhcp6c_get_state(STATE_NTP_FQDN, &ntp_dns_len);
392                 struct in6_addr *sip = odhcp6c_get_state(STATE_SIP_IP, &sip_ip_len);
393                 uint8_t *sip_fqdn = odhcp6c_get_state(STATE_SIP_FQDN, &sip_fqdn_len);
394                 uint8_t *aftr_name = odhcp6c_get_state(STATE_AFTR_NAME, &aftr_name_len);
395                 struct in6_addr *cer = odhcp6c_get_state(STATE_CER, &cer_len);
396                 uint8_t *s46_mapt = odhcp6c_get_state(STATE_S46_MAPT, &s46_mapt_len);
397                 uint8_t *s46_mape = odhcp6c_get_state(STATE_S46_MAPE, &s46_mape_len);
398                 uint8_t *s46_lw = odhcp6c_get_state(STATE_S46_LW, &s46_lw_len);
399                 uint8_t *passthru = odhcp6c_get_state(STATE_PASSTHRU, &passthru_len);
400
401                 size_t prefix_len, address_len, ra_pref_len,
402                         ra_route_len, ra_dns_len, ra_search_len;
403                 uint8_t *prefix = odhcp6c_get_state(STATE_IA_PD, &prefix_len);
404                 uint8_t *address = odhcp6c_get_state(STATE_IA_NA, &address_len);
405                 uint8_t *ra_pref = odhcp6c_get_state(STATE_RA_PREFIX, &ra_pref_len);
406                 uint8_t *ra_route = odhcp6c_get_state(STATE_RA_ROUTE, &ra_route_len);
407                 uint8_t *ra_dns = odhcp6c_get_state(STATE_RA_DNS, &ra_dns_len);
408                 uint8_t *ra_search = odhcp6c_get_state(STATE_RA_SEARCH, &ra_search_len);
409
410                 ipv6_to_env("SERVER", addr, addr_len / sizeof(*addr));
411                 ipv6_to_env("RDNSS", dns, dns_len / sizeof(*dns));
412                 ipv6_to_env("SNTP_IP", sntp, sntp_ip_len / sizeof(*sntp));
413                 ipv6_to_env("NTP_IP", ntp, ntp_ip_len / sizeof(*ntp));
414                 fqdn_to_env("NTP_FQDN", ntp_dns, ntp_dns_len);
415                 ipv6_to_env("SIP_IP", sip, sip_ip_len / sizeof(*sip));
416                 fqdn_to_env("DOMAINS", search, search_len);
417                 fqdn_to_env("SIP_DOMAIN", sip_fqdn, sip_fqdn_len);
418                 fqdn_to_env("AFTR", aftr_name, aftr_name_len);
419                 ipv6_to_env("CER", cer, cer_len / sizeof(*cer));
420                 s46_to_env(STATE_S46_MAPE, s46_mape, s46_mape_len);
421                 s46_to_env(STATE_S46_MAPT, s46_mapt, s46_mapt_len);
422                 s46_to_env(STATE_S46_LW, s46_lw, s46_lw_len);
423                 bin_to_env(custom, custom_len);
424
425                 if (odhcp6c_is_bound()) {
426                         entry_to_env("PREFIXES", prefix, prefix_len, ENTRY_PREFIX);
427                         entry_to_env("ADDRESSES", address, address_len, ENTRY_ADDRESS);
428                 }
429
430                 entry_to_env("RA_ADDRESSES", ra_pref, ra_pref_len, ENTRY_ADDRESS);
431                 entry_to_env("RA_ROUTES", ra_route, ra_route_len, ENTRY_ROUTE);
432                 entry_to_env("RA_DNS", ra_dns, ra_dns_len, ENTRY_HOST);
433                 search_to_env("RA_DOMAINS", ra_search, ra_search_len);
434
435                 int_to_env("RA_HOPLIMIT", ra_conf_hoplimit(0));
436                 int_to_env("RA_MTU", ra_conf_mtu(0));
437                 int_to_env("RA_REACHABLE", ra_conf_reachable(0));
438                 int_to_env("RA_RETRANSMIT", ra_conf_retransmit(0));
439
440                 char *buf = malloc(10 + passthru_len * 2);
441                 strncpy(buf, "PASSTHRU=", 10);
442                 script_hexlify(&buf[9], passthru, passthru_len);
443                 putenv(buf);
444
445                 execv(argv[0], argv);
446                 _exit(128);
447         }
448 }