blob: 065ea0cc7f0c401a4a0a20eea5aaa4151dfc9bda [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/* script.c
3 *
4 * Functions to call the DHCP client notification scripts
5 *
6 * Russ Dill <Russ.Dill@asu.edu> July 2001
7 *
Rob Landley3f785612006-05-28 01:06:36 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Mike Frysinger7031f622006-05-08 03:20:50 +00009 */
10
Mike Frysinger7031f622006-05-08 03:20:50 +000011#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000012#include "dhcpc.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000013#include "options.h"
14
Mike Frysinger7031f622006-05-08 03:20:50 +000015
16/* get a rough idea of how long an option will be (rounding up...) */
Denys Vlasenko7895b912009-07-03 16:59:59 +020017static const uint8_t len_of_option_as_string[] = {
Mike Frysinger7031f622006-05-08 03:20:50 +000018 [OPTION_IP] = sizeof("255.255.255.255 "),
19 [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2,
Denys Vlasenko97ef6542009-07-03 18:14:03 +020020 [OPTION_STATIC_ROUTES]= sizeof("255.255.255.255/32 255.255.255.255 "),
Mike Frysinger7031f622006-05-08 03:20:50 +000021 [OPTION_STRING] = 1,
Denis Vlasenko35a064b2008-11-06 00:49:59 +000022#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +000023 [OPTION_STR1035] = 1,
24#endif
Mike Frysinger7031f622006-05-08 03:20:50 +000025 [OPTION_BOOLEAN] = sizeof("yes "),
26 [OPTION_U8] = sizeof("255 "),
27 [OPTION_U16] = sizeof("65535 "),
28 [OPTION_S16] = sizeof("-32768 "),
29 [OPTION_U32] = sizeof("4294967295 "),
30 [OPTION_S32] = sizeof("-2147483684 "),
31};
32
33
Denys Vlasenko56f2d062009-06-16 10:25:35 +020034/* note: ip is a pointer to an IP in network order, possibly misaliged */
35static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
Mike Frysinger7031f622006-05-08 03:20:50 +000036{
Denys Vlasenko7895b912009-07-03 16:59:59 +020037 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
Mike Frysinger7031f622006-05-08 03:20:50 +000038}
39
40
41/* really simple implementation, just count the bits */
Denis Vlasenko35ff7462007-11-28 19:23:12 +000042static int mton(uint32_t mask)
Mike Frysinger7031f622006-05-08 03:20:50 +000043{
Denis Vlasenko35ff7462007-11-28 19:23:12 +000044 int i = 0;
45 mask = ntohl(mask); /* 111110000-like bit pattern */
46 while (mask) {
47 i++;
48 mask <<= 1;
49 }
50 return i;
Mike Frysinger7031f622006-05-08 03:20:50 +000051}
52
53
Denys Vlasenko7895b912009-07-03 16:59:59 +020054/* Create "opt_name=opt_value" string */
Denys Vlasenko97ef6542009-07-03 18:14:03 +020055static NOINLINE char *xmalloc_optname_optval(uint8_t *option, const struct dhcp_option *type_p, const char *opt_name)
Mike Frysinger7031f622006-05-08 03:20:50 +000056{
Denys Vlasenko7895b912009-07-03 16:59:59 +020057 unsigned upper_length;
Denis Vlasenko50664732007-02-27 21:15:08 +000058 int len, type, optlen;
Mike Frysinger7031f622006-05-08 03:20:50 +000059 uint16_t val_u16;
60 int16_t val_s16;
61 uint32_t val_u32;
62 int32_t val_s32;
Denis Vlasenko50664732007-02-27 21:15:08 +000063 char *dest, *ret;
Mike Frysinger7031f622006-05-08 03:20:50 +000064
Denys Vlasenko7895b912009-07-03 16:59:59 +020065 /* option points to OPT_DATA, need to go back and get OPT_LEN */
66 len = option[OPT_LEN - OPT_DATA];
Mike Frysinger7031f622006-05-08 03:20:50 +000067 type = type_p->flags & TYPE_MASK;
Denis Vlasenkob539c842007-11-29 08:17:45 +000068 optlen = dhcp_option_lengths[type];
Denys Vlasenko7895b912009-07-03 16:59:59 +020069 upper_length = len_of_option_as_string[type] * (len / optlen);
Denis Vlasenko50664732007-02-27 21:15:08 +000070
Denys Vlasenko7895b912009-07-03 16:59:59 +020071 dest = ret = xmalloc(upper_length + strlen(opt_name) + 2);
Denis Vlasenkob539c842007-11-29 08:17:45 +000072 dest += sprintf(ret, "%s=", opt_name);
Denis Vlasenko50664732007-02-27 21:15:08 +000073
Denys Vlasenko7895b912009-07-03 16:59:59 +020074 while (len >= optlen) {
Mike Frysinger7031f622006-05-08 03:20:50 +000075 switch (type) {
76 case OPTION_IP_PAIR:
Denys Vlasenko56f2d062009-06-16 10:25:35 +020077 dest += sprint_nip(dest, "", option);
Denis Vlasenkofbd29182007-04-07 01:05:47 +000078 *dest++ = '/';
Mike Frysinger7031f622006-05-08 03:20:50 +000079 option += 4;
80 optlen = 4;
81 case OPTION_IP: /* Works regardless of host byte order. */
Denys Vlasenko56f2d062009-06-16 10:25:35 +020082 dest += sprint_nip(dest, "", option);
Mike Frysinger7031f622006-05-08 03:20:50 +000083 break;
84 case OPTION_BOOLEAN:
85 dest += sprintf(dest, *option ? "yes" : "no");
86 break;
87 case OPTION_U8:
88 dest += sprintf(dest, "%u", *option);
89 break;
90 case OPTION_U16:
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000091 move_from_unaligned16(val_u16, option);
Mike Frysinger7031f622006-05-08 03:20:50 +000092 dest += sprintf(dest, "%u", ntohs(val_u16));
93 break;
94 case OPTION_S16:
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000095 move_from_unaligned16(val_s16, option);
Mike Frysinger7031f622006-05-08 03:20:50 +000096 dest += sprintf(dest, "%d", ntohs(val_s16));
97 break;
98 case OPTION_U32:
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000099 move_from_unaligned32(val_u32, option);
Mike Frysinger7031f622006-05-08 03:20:50 +0000100 dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32));
101 break;
102 case OPTION_S32:
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000103 move_from_unaligned32(val_s32, option);
Mike Frysinger7031f622006-05-08 03:20:50 +0000104 dest += sprintf(dest, "%ld", (long) ntohl(val_s32));
105 break;
106 case OPTION_STRING:
107 memcpy(dest, option, len);
108 dest[len] = '\0';
Denis Vlasenko50664732007-02-27 21:15:08 +0000109 return ret; /* Short circuit this case */
Denys Vlasenko97ef6542009-07-03 18:14:03 +0200110 case OPTION_STATIC_ROUTES: {
111 /* Option binary format:
112 * mask [one byte, 0..32]
113 * ip [big endian, 0..4 bytes depending on mask]
114 * router [big endian, 4 bytes]
115 * may be repeated
116 *
117 * We convert it to a string "IP/MASK ROUTER IP2/MASK2 ROUTER2"
118 */
119 const char *pfx = "";
120
121 while (len >= 1 + 4) { /* mask + 0-byte ip + router */
122 uint32_t nip;
123 uint8_t *p;
124 unsigned mask;
125 int bytes;
126
127 mask = *option++;
128 if (mask > 32)
129 break;
130 len--;
131
132 nip = 0;
133 p = (void*) &nip;
134 bytes = (mask + 7) / 8; /* 0 -> 0, 1..8 -> 1, 9..16 -> 2 etc */
135 while (--bytes >= 0) {
136 *p++ = *option++;
137 len--;
138 }
139 if (len < 4)
140 break;
141
142 /* print ip/mask */
143 dest += sprint_nip(dest, pfx, (void*) &nip);
144 pfx = " ";
145 dest += sprintf(dest, "/%u ", mask);
146 /* print router */
147 dest += sprint_nip(dest, "", option);
148 option += 4;
149 len -= 4;
150 }
151
152 return ret;
153 }
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000154#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000155 case OPTION_STR1035:
156 /* unpack option into dest; use ret for prefix (i.e., "optname=") */
157 dest = dname_dec(option, len, ret);
Denys Vlasenko7895b912009-07-03 16:59:59 +0200158 if (dest) {
159 free(ret);
160 return dest;
161 }
162 /* error. return "optname=" string */
163 return ret;
Denis Vlasenko50664732007-02-27 21:15:08 +0000164#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000165 }
166 option += optlen;
167 len -= optlen;
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000168 if (len <= 0)
169 break;
Denys Vlasenko7895b912009-07-03 16:59:59 +0200170 *dest++ = ' ';
171 *dest = '\0';
Mike Frysinger7031f622006-05-08 03:20:50 +0000172 }
Denis Vlasenko50664732007-02-27 21:15:08 +0000173 return ret;
Mike Frysinger7031f622006-05-08 03:20:50 +0000174}
175
176
177/* put all the parameters into an environment */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200178static char **fill_envp(struct dhcp_packet *packet)
Mike Frysinger7031f622006-05-08 03:20:50 +0000179{
180 int num_options = 0;
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000181 int i;
182 char **envp, **curr;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000183 const char *opt_name;
Mike Frysinger7031f622006-05-08 03:20:50 +0000184 uint8_t *temp;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000185 uint8_t over = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000186
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000187 if (packet) {
188 for (i = 0; dhcp_options[i].code; i++) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000189 if (get_option(packet, dhcp_options[i].code)) {
190 num_options++;
191 if (dhcp_options[i].code == DHCP_SUBNET)
192 num_options++; /* for mton */
193 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000194 }
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200195 if (packet->siaddr_nip)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000196 num_options++;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000197 temp = get_option(packet, DHCP_OPTION_OVERLOAD);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000198 if (temp)
Mike Frysinger7031f622006-05-08 03:20:50 +0000199 over = *temp;
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000200 if (!(over & FILE_FIELD) && packet->file[0])
201 num_options++;
202 if (!(over & SNAME_FIELD) && packet->sname[0])
203 num_options++;
Mike Frysinger7031f622006-05-08 03:20:50 +0000204 }
205
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000206 curr = envp = xzalloc(sizeof(char *) * (num_options + 3));
207 *curr = xasprintf("interface=%s", client_config.interface);
208 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000209
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000210 if (packet == NULL)
211 return envp;
Mike Frysinger7031f622006-05-08 03:20:50 +0000212
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000213 *curr = xmalloc(sizeof("ip=255.255.255.255"));
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200214 sprint_nip(*curr, "ip=", (uint8_t *) &packet->yiaddr);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000215 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000216
Denis Vlasenkob539c842007-11-29 08:17:45 +0000217 opt_name = dhcp_option_strings;
218 i = 0;
219 while (*opt_name) {
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000220 temp = get_option(packet, dhcp_options[i].code);
Denis Vlasenkoc2f5b022006-11-28 00:21:46 +0000221 if (!temp)
Denis Vlasenkob539c842007-11-29 08:17:45 +0000222 goto next;
Denys Vlasenko7895b912009-07-03 16:59:59 +0200223 *curr = xmalloc_optname_optval(temp, &dhcp_options[i], opt_name);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000224 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000225
226 /* Fill in a subnet bits option for things like /24 */
227 if (dhcp_options[i].code == DHCP_SUBNET) {
Denis Vlasenko35ff7462007-11-28 19:23:12 +0000228 uint32_t subnet;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000229 move_from_unaligned32(subnet, temp);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000230 *curr = xasprintf("mask=%d", mton(subnet));
231 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000232 }
Denis Vlasenkob539c842007-11-29 08:17:45 +0000233 next:
234 opt_name += strlen(opt_name) + 1;
235 i++;
Mike Frysinger7031f622006-05-08 03:20:50 +0000236 }
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200237 if (packet->siaddr_nip) {
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000238 *curr = xmalloc(sizeof("siaddr=255.255.255.255"));
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200239 sprint_nip(*curr, "siaddr=", (uint8_t *) &packet->siaddr_nip);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000240 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000241 }
242 if (!(over & FILE_FIELD) && packet->file[0]) {
243 /* watch out for invalid packets */
244 packet->file[sizeof(packet->file) - 1] = '\0';
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000245 *curr = xasprintf("boot_file=%s", packet->file);
246 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000247 }
248 if (!(over & SNAME_FIELD) && packet->sname[0]) {
249 /* watch out for invalid packets */
250 packet->sname[sizeof(packet->sname) - 1] = '\0';
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000251 *curr = xasprintf("sname=%s", packet->sname);
252 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000253 }
254 return envp;
255}
256
257
258/* Call a script with a par file and env vars */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200259void FAST_FUNC udhcp_run_script(struct dhcp_packet *packet, const char *name)
Mike Frysinger7031f622006-05-08 03:20:50 +0000260{
Mike Frysinger7031f622006-05-08 03:20:50 +0000261 char **envp, **curr;
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000262 char *argv[3];
Mike Frysinger7031f622006-05-08 03:20:50 +0000263
264 if (client_config.script == NULL)
265 return;
266
Mike Frysinger7031f622006-05-08 03:20:50 +0000267 envp = fill_envp(packet);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000268
Mike Frysinger7031f622006-05-08 03:20:50 +0000269 /* call script */
Denys Vlasenko26c64ba2009-11-02 18:53:57 +0100270 log1("Executing %s %s", client_config.script, name);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000271 argv[0] = (char*) client_config.script;
272 argv[1] = (char*) name;
273 argv[2] = NULL;
274 wait4pid(spawn(argv));
275
276 for (curr = envp; *curr; curr++) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200277 log2(" %s", *curr);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000278 bb_unsetenv(*curr);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000279 free(*curr);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000280 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000281 free(envp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000282}