]> git.decadent.org.uk Git - odhcp6c.git/blob - src/script.c
Export AFTR-IP as well and update README
[odhcp6c.git] / src / script.c
1 /**
2  * Copyright (C) 2012-2013 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 <arpa/inet.h>
24 #include <netinet/in.h>
25
26 #include "odhcp6c.h"
27
28 static const char hexdigits[] = "0123456789abcdef";
29 static const int8_t hexvals[] = {
30     -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
31     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32     -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
34     -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -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 };
39
40
41
42 static char *argv[4] = {NULL, NULL, NULL, NULL};
43 static volatile char *delayed_call = NULL;
44
45
46 int script_init(const char *path, const char *ifname)
47 {
48         argv[0] = (char*)path;
49         argv[1] = (char*)ifname;
50         return 0;
51 }
52
53
54 ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src)
55 {
56         size_t c;
57         for (c = 0; c < len && src[0] && src[1]; ++c) {
58                 int8_t x = (int8_t)*src++;
59                 int8_t y = (int8_t)*src++;
60                 if (x < 0 || (x = hexvals[x]) < 0
61                                 || y < 0 || (y = hexvals[y]) < 0)
62                         return -1;
63                 dst[c] = x << 4 | y;
64                 while (((int8_t)*src) < 0 ||
65                                 (*src && hexvals[(uint8_t)*src] < 0))
66                         src++;
67         }
68
69         return c;
70 }
71
72
73 static void script_hexlify(char *dst, const uint8_t *src, size_t len) {
74         for (size_t i = 0; i < len; ++i) {
75                 *dst++ = hexdigits[src[i] >> 4];
76                 *dst++ = hexdigits[src[i] & 0x0f];
77         }
78         *dst = 0;
79 }
80
81
82 static void ipv6_to_env(const char *name,
83                 const struct in6_addr *addr, size_t cnt)
84 {
85         size_t buf_len = strlen(name);
86         char *buf = realloc(NULL, cnt * INET6_ADDRSTRLEN + buf_len + 2);
87         memcpy(buf, name, buf_len);
88         buf[buf_len++] = '=';
89         for (size_t i = 0; i < cnt; ++i) {
90                 inet_ntop(AF_INET6, &addr[i], &buf[buf_len], INET6_ADDRSTRLEN);
91                 buf_len += strlen(&buf[buf_len]);
92                 buf[buf_len++] = ' ';
93         }
94         buf[buf_len - 1] = '\0';
95         putenv(buf);
96 }
97
98
99 static void fqdn_to_env(const char *name, const uint8_t *fqdn, size_t len)
100 {
101         size_t buf_len = strlen(name);
102         size_t buf_size = len + buf_len + 2;
103         const uint8_t *fqdn_end = fqdn + len;
104         char *buf = realloc(NULL, len + buf_len + 2);
105         memcpy(buf, name, buf_len);
106         buf[buf_len++] = '=';
107         int l = 1;
108         while (l > 0 && fqdn < fqdn_end) {
109                 l = dn_expand(fqdn, fqdn_end, fqdn, &buf[buf_len], buf_size - buf_len);
110                 fqdn += l;
111                 buf_len += strlen(&buf[buf_len]);
112                 buf[buf_len++] = ' ';
113         }
114         buf[buf_len - 1] = '\0';
115         putenv(buf);
116 }
117
118
119 static void fqdn_to_ip_env(const char *name, const uint8_t *fqdn, size_t len)
120 {
121         size_t buf_len = strlen(name);
122         char *buf = realloc(NULL, INET6_ADDRSTRLEN + buf_len + 3);
123         memcpy(buf, name, buf_len);
124         buf[buf_len++] = '=';
125
126         char namebuf[256];
127         if (dn_expand(fqdn, fqdn + len, fqdn, namebuf, sizeof(namebuf)) <= 0)
128                 return;
129
130         struct addrinfo hints = {.ai_family = AF_INET6}, *r;
131         if (getaddrinfo(namebuf, NULL, &hints, &r))
132                 return;
133
134         struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)r->ai_addr;
135         inet_ntop(AF_INET6, &sin6->sin6_addr, &buf[buf_len], INET6_ADDRSTRLEN);
136
137         freeaddrinfo(r);
138         putenv(buf);
139 }
140
141
142 static void bin_to_env(uint8_t *opts, size_t len)
143 {
144         uint8_t *oend = opts + len, *odata;
145         uint16_t otype, olen;
146         dhcpv6_for_each_option(opts, oend, otype, olen, odata) {
147                 char *buf = realloc(NULL, 14 + (olen * 2));
148                 size_t buf_len = 0;
149
150                 snprintf(buf, 14, "OPTION_%hu=", otype);
151                 buf_len += strlen(buf);
152
153                 script_hexlify(&buf[buf_len], odata, olen);
154                 putenv(buf);
155         }
156 }
157
158 enum entry_type {
159         ENTRY_ADDRESS,
160         ENTRY_HOST,
161         ENTRY_ROUTE,
162         ENTRY_PREFIX
163 };
164
165 static void entry_to_env(const char *name, const void *data, size_t len, enum entry_type type)
166 {
167         size_t buf_len = strlen(name);
168         const struct odhcp6c_entry *e = data;
169         char *buf = realloc(NULL, buf_len + 2 + (len / sizeof(*e)) * 144);
170         memcpy(buf, name, buf_len);
171         buf[buf_len++] = '=';
172
173         for (size_t i = 0; i < len / sizeof(*e); ++i) {
174                 inet_ntop(AF_INET6, &e[i].target, &buf[buf_len], INET6_ADDRSTRLEN);
175                 buf_len += strlen(&buf[buf_len]);
176                 if (type != ENTRY_HOST) {
177                         buf_len += snprintf(&buf[buf_len], 6, "/%hhu", e[i].length);
178                         if (type == ENTRY_ROUTE) {
179                                 buf[buf_len++] = ',';
180                                 if (!IN6_IS_ADDR_UNSPECIFIED(&e[i].router)) {
181                                         inet_ntop(AF_INET6, &e[i].router, &buf[buf_len], INET6_ADDRSTRLEN);
182                                         buf_len += strlen(&buf[buf_len]);
183                                 }
184                                 buf_len += snprintf(&buf[buf_len], 24, ",%u", e[i].valid);
185                                 buf_len += snprintf(&buf[buf_len], 12, ",%u", e[i].priority);
186                         } else {
187                                 buf_len += snprintf(&buf[buf_len], 24, ",%u,%u", e[i].preferred, e[i].valid);
188                         }
189
190                         if (type == ENTRY_PREFIX && e[i].priority) {
191                                 // priority and router are abused for prefix exclusion
192                                 buf_len += snprintf(&buf[buf_len], 12, ",excluded=");
193                                 inet_ntop(AF_INET6, &e[i].router, &buf[buf_len], INET6_ADDRSTRLEN);
194                                 buf_len += strlen(&buf[buf_len]);
195                                 buf_len += snprintf(&buf[buf_len], 24, "/%u", e[i].priority);
196                         }
197                 }
198                 buf[buf_len++] = ' ';
199         }
200
201         buf[buf_len - 1] = '\0';
202         putenv(buf);
203 }
204
205
206 static void script_call_delayed(int signal __attribute__((unused)))
207 {
208         if (delayed_call)
209                 script_call((char*)delayed_call);
210 }
211
212
213 void script_delay_call(const char *status, int timeout)
214 {
215         if (!delayed_call) {
216                 delayed_call = strdup(status);
217                 signal(SIGALRM, script_call_delayed);
218                 alarm(timeout);
219         }
220 }
221
222
223 void script_call(const char *status)
224 {
225         size_t dns_len, search_len, custom_len, sntp_ip_len, sntp_dns_len;
226         size_t sip_ip_len, sip_fqdn_len, aftr_name_len;
227
228         odhcp6c_expire();
229         if (delayed_call)
230                 alarm(0);
231
232         struct in6_addr *dns = odhcp6c_get_state(STATE_DNS, &dns_len);
233         uint8_t *search = odhcp6c_get_state(STATE_SEARCH, &search_len);
234         uint8_t *custom = odhcp6c_get_state(STATE_CUSTOM_OPTS, &custom_len);
235         struct in6_addr *sntp = odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len);
236         uint8_t *sntp_dns = odhcp6c_get_state(STATE_SNTP_FQDN, &sntp_dns_len);
237         struct in6_addr *sip = odhcp6c_get_state(STATE_SIP_IP, &sip_ip_len);
238         uint8_t *sip_fqdn = odhcp6c_get_state(STATE_SIP_FQDN, &sip_fqdn_len);
239         uint8_t *aftr_name = odhcp6c_get_state(STATE_AFTR_NAME, &aftr_name_len);
240
241         size_t prefix_len, address_len, ra_pref_len, ra_route_len, ra_dns_len;
242         uint8_t *prefix = odhcp6c_get_state(STATE_IA_PD, &prefix_len);
243         uint8_t *address = odhcp6c_get_state(STATE_IA_NA, &address_len);
244         uint8_t *ra_pref = odhcp6c_get_state(STATE_RA_PREFIX, &ra_pref_len);
245         uint8_t *ra_route = odhcp6c_get_state(STATE_RA_ROUTE, &ra_route_len);
246         uint8_t *ra_dns = odhcp6c_get_state(STATE_RA_DNS, &ra_dns_len);
247
248         // Don't set environment before forking, because env is leaky.
249         if (fork() == 0) {
250                 ipv6_to_env("RDNSS", dns, dns_len / sizeof(*dns));
251                 ipv6_to_env("SNTP_IP", sntp, sntp_ip_len / sizeof(*sntp));
252                 ipv6_to_env("SIP_IP", sip, sip_ip_len / sizeof(*sip));
253                 fqdn_to_env("DOMAINS", search, search_len);
254                 fqdn_to_env("SNTP_FQDN", sntp_dns, sntp_dns_len);
255                 fqdn_to_env("SIP_DOMAIN", sip_fqdn, sip_fqdn_len);
256                 fqdn_to_env("AFTR", aftr_name, aftr_name_len);
257                 fqdn_to_ip_env("AFTR_IP", aftr_name, aftr_name_len);
258                 bin_to_env(custom, custom_len);
259                 entry_to_env("PREFIXES", prefix, prefix_len, ENTRY_PREFIX);
260                 entry_to_env("ADDRESSES", address, address_len, ENTRY_ADDRESS);
261                 entry_to_env("RA_ADDRESSES", ra_pref, ra_pref_len, ENTRY_ADDRESS);
262                 entry_to_env("RA_ROUTES", ra_route, ra_route_len, ENTRY_ROUTE);
263                 entry_to_env("RA_DNS", ra_dns, ra_dns_len, ENTRY_HOST);
264
265                 argv[2] = (char*)status;
266                 execv(argv[0], argv);
267                 _exit(128);
268         }
269
270         // Delete lost prefixes and user opts
271         odhcp6c_clear_state(STATE_CUSTOM_OPTS);
272 }