blob: 94dabf4d17058a6637d473041a773b5ffd5badf1 [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,
20 [OPTION_STRING] = 1,
Denis Vlasenko35a064b2008-11-06 00:49:59 +000021#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +000022 [OPTION_STR1035] = 1,
23#endif
Mike Frysinger7031f622006-05-08 03:20:50 +000024 [OPTION_BOOLEAN] = sizeof("yes "),
25 [OPTION_U8] = sizeof("255 "),
26 [OPTION_U16] = sizeof("65535 "),
27 [OPTION_S16] = sizeof("-32768 "),
28 [OPTION_U32] = sizeof("4294967295 "),
29 [OPTION_S32] = sizeof("-2147483684 "),
30};
31
32
Denys Vlasenko56f2d062009-06-16 10:25:35 +020033/* note: ip is a pointer to an IP in network order, possibly misaliged */
34static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
Mike Frysinger7031f622006-05-08 03:20:50 +000035{
Denys Vlasenko7895b912009-07-03 16:59:59 +020036 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
Mike Frysinger7031f622006-05-08 03:20:50 +000037}
38
39
40/* really simple implementation, just count the bits */
Denis Vlasenko35ff7462007-11-28 19:23:12 +000041static int mton(uint32_t mask)
Mike Frysinger7031f622006-05-08 03:20:50 +000042{
Denis Vlasenko35ff7462007-11-28 19:23:12 +000043 int i = 0;
44 mask = ntohl(mask); /* 111110000-like bit pattern */
45 while (mask) {
46 i++;
47 mask <<= 1;
48 }
49 return i;
Mike Frysinger7031f622006-05-08 03:20:50 +000050}
51
52
Denys Vlasenko7895b912009-07-03 16:59:59 +020053/* Create "opt_name=opt_value" string */
54static char *xmalloc_optname_optval(uint8_t *option, const struct dhcp_option *type_p, const char *opt_name)
Mike Frysinger7031f622006-05-08 03:20:50 +000055{
Denys Vlasenko7895b912009-07-03 16:59:59 +020056 unsigned upper_length;
Denis Vlasenko50664732007-02-27 21:15:08 +000057 int len, type, optlen;
Mike Frysinger7031f622006-05-08 03:20:50 +000058 uint16_t val_u16;
59 int16_t val_s16;
60 uint32_t val_u32;
61 int32_t val_s32;
Denis Vlasenko50664732007-02-27 21:15:08 +000062 char *dest, *ret;
Mike Frysinger7031f622006-05-08 03:20:50 +000063
Denys Vlasenko7895b912009-07-03 16:59:59 +020064 /* option points to OPT_DATA, need to go back and get OPT_LEN */
65 len = option[OPT_LEN - OPT_DATA];
Mike Frysinger7031f622006-05-08 03:20:50 +000066 type = type_p->flags & TYPE_MASK;
Denis Vlasenkob539c842007-11-29 08:17:45 +000067 optlen = dhcp_option_lengths[type];
Denys Vlasenko7895b912009-07-03 16:59:59 +020068 upper_length = len_of_option_as_string[type] * (len / optlen);
Denis Vlasenko50664732007-02-27 21:15:08 +000069
Denys Vlasenko7895b912009-07-03 16:59:59 +020070 dest = ret = xmalloc(upper_length + strlen(opt_name) + 2);
Denis Vlasenkob539c842007-11-29 08:17:45 +000071 dest += sprintf(ret, "%s=", opt_name);
Denis Vlasenko50664732007-02-27 21:15:08 +000072
Denys Vlasenko7895b912009-07-03 16:59:59 +020073 while (len >= optlen) {
Mike Frysinger7031f622006-05-08 03:20:50 +000074 switch (type) {
75 case OPTION_IP_PAIR:
Denys Vlasenko56f2d062009-06-16 10:25:35 +020076 dest += sprint_nip(dest, "", option);
Denis Vlasenkofbd29182007-04-07 01:05:47 +000077 *dest++ = '/';
Mike Frysinger7031f622006-05-08 03:20:50 +000078 option += 4;
79 optlen = 4;
80 case OPTION_IP: /* Works regardless of host byte order. */
Denys Vlasenko56f2d062009-06-16 10:25:35 +020081 dest += sprint_nip(dest, "", option);
Mike Frysinger7031f622006-05-08 03:20:50 +000082 break;
83 case OPTION_BOOLEAN:
84 dest += sprintf(dest, *option ? "yes" : "no");
85 break;
86 case OPTION_U8:
87 dest += sprintf(dest, "%u", *option);
88 break;
89 case OPTION_U16:
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000090 move_from_unaligned16(val_u16, option);
Mike Frysinger7031f622006-05-08 03:20:50 +000091 dest += sprintf(dest, "%u", ntohs(val_u16));
92 break;
93 case OPTION_S16:
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000094 move_from_unaligned16(val_s16, option);
Mike Frysinger7031f622006-05-08 03:20:50 +000095 dest += sprintf(dest, "%d", ntohs(val_s16));
96 break;
97 case OPTION_U32:
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000098 move_from_unaligned32(val_u32, option);
Mike Frysinger7031f622006-05-08 03:20:50 +000099 dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32));
100 break;
101 case OPTION_S32:
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000102 move_from_unaligned32(val_s32, option);
Mike Frysinger7031f622006-05-08 03:20:50 +0000103 dest += sprintf(dest, "%ld", (long) ntohl(val_s32));
104 break;
105 case OPTION_STRING:
106 memcpy(dest, option, len);
107 dest[len] = '\0';
Denis Vlasenko50664732007-02-27 21:15:08 +0000108 return ret; /* Short circuit this case */
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000109#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000110 case OPTION_STR1035:
111 /* unpack option into dest; use ret for prefix (i.e., "optname=") */
112 dest = dname_dec(option, len, ret);
Denys Vlasenko7895b912009-07-03 16:59:59 +0200113 if (dest) {
114 free(ret);
115 return dest;
116 }
117 /* error. return "optname=" string */
118 return ret;
Denis Vlasenko50664732007-02-27 21:15:08 +0000119#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000120 }
121 option += optlen;
122 len -= optlen;
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000123 if (len <= 0)
124 break;
Denys Vlasenko7895b912009-07-03 16:59:59 +0200125 *dest++ = ' ';
126 *dest = '\0';
Mike Frysinger7031f622006-05-08 03:20:50 +0000127 }
Denis Vlasenko50664732007-02-27 21:15:08 +0000128 return ret;
Mike Frysinger7031f622006-05-08 03:20:50 +0000129}
130
131
132/* put all the parameters into an environment */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200133static char **fill_envp(struct dhcp_packet *packet)
Mike Frysinger7031f622006-05-08 03:20:50 +0000134{
135 int num_options = 0;
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000136 int i;
137 char **envp, **curr;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000138 const char *opt_name;
Mike Frysinger7031f622006-05-08 03:20:50 +0000139 uint8_t *temp;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000140 uint8_t over = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000141
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000142 if (packet) {
143 for (i = 0; dhcp_options[i].code; i++) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000144 if (get_option(packet, dhcp_options[i].code)) {
145 num_options++;
146 if (dhcp_options[i].code == DHCP_SUBNET)
147 num_options++; /* for mton */
148 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000149 }
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200150 if (packet->siaddr_nip)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000151 num_options++;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000152 temp = get_option(packet, DHCP_OPTION_OVERLOAD);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000153 if (temp)
Mike Frysinger7031f622006-05-08 03:20:50 +0000154 over = *temp;
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000155 if (!(over & FILE_FIELD) && packet->file[0])
156 num_options++;
157 if (!(over & SNAME_FIELD) && packet->sname[0])
158 num_options++;
Mike Frysinger7031f622006-05-08 03:20:50 +0000159 }
160
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000161 curr = envp = xzalloc(sizeof(char *) * (num_options + 3));
162 *curr = xasprintf("interface=%s", client_config.interface);
163 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000164
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000165 if (packet == NULL)
166 return envp;
Mike Frysinger7031f622006-05-08 03:20:50 +0000167
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000168 *curr = xmalloc(sizeof("ip=255.255.255.255"));
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200169 sprint_nip(*curr, "ip=", (uint8_t *) &packet->yiaddr);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000170 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000171
Denis Vlasenkob539c842007-11-29 08:17:45 +0000172 opt_name = dhcp_option_strings;
173 i = 0;
174 while (*opt_name) {
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000175 temp = get_option(packet, dhcp_options[i].code);
Denis Vlasenkoc2f5b022006-11-28 00:21:46 +0000176 if (!temp)
Denis Vlasenkob539c842007-11-29 08:17:45 +0000177 goto next;
Denys Vlasenko7895b912009-07-03 16:59:59 +0200178 *curr = xmalloc_optname_optval(temp, &dhcp_options[i], opt_name);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000179 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000180
181 /* Fill in a subnet bits option for things like /24 */
182 if (dhcp_options[i].code == DHCP_SUBNET) {
Denis Vlasenko35ff7462007-11-28 19:23:12 +0000183 uint32_t subnet;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000184 move_from_unaligned32(subnet, temp);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000185 *curr = xasprintf("mask=%d", mton(subnet));
186 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000187 }
Denis Vlasenkob539c842007-11-29 08:17:45 +0000188 next:
189 opt_name += strlen(opt_name) + 1;
190 i++;
Mike Frysinger7031f622006-05-08 03:20:50 +0000191 }
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200192 if (packet->siaddr_nip) {
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000193 *curr = xmalloc(sizeof("siaddr=255.255.255.255"));
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200194 sprint_nip(*curr, "siaddr=", (uint8_t *) &packet->siaddr_nip);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000195 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000196 }
197 if (!(over & FILE_FIELD) && packet->file[0]) {
198 /* watch out for invalid packets */
199 packet->file[sizeof(packet->file) - 1] = '\0';
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000200 *curr = xasprintf("boot_file=%s", packet->file);
201 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000202 }
203 if (!(over & SNAME_FIELD) && packet->sname[0]) {
204 /* watch out for invalid packets */
205 packet->sname[sizeof(packet->sname) - 1] = '\0';
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000206 *curr = xasprintf("sname=%s", packet->sname);
207 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000208 }
209 return envp;
210}
211
212
213/* Call a script with a par file and env vars */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200214void FAST_FUNC udhcp_run_script(struct dhcp_packet *packet, const char *name)
Mike Frysinger7031f622006-05-08 03:20:50 +0000215{
Mike Frysinger7031f622006-05-08 03:20:50 +0000216 char **envp, **curr;
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000217 char *argv[3];
Mike Frysinger7031f622006-05-08 03:20:50 +0000218
219 if (client_config.script == NULL)
220 return;
221
Mike Frysinger7031f622006-05-08 03:20:50 +0000222 envp = fill_envp(packet);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000223
Mike Frysinger7031f622006-05-08 03:20:50 +0000224 /* call script */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200225 log1("Executing %s", client_config.script);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000226 argv[0] = (char*) client_config.script;
227 argv[1] = (char*) name;
228 argv[2] = NULL;
229 wait4pid(spawn(argv));
230
231 for (curr = envp; *curr; curr++) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200232 log2(" %s", *curr);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000233 bb_unsetenv(*curr);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000234 free(*curr);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000235 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000236 free(envp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000237}