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