blob: 794e3ca8d3d484ebe272d893086054864bf42a25 [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...) */
Denis Vlasenko3f112312007-08-19 21:21:16 +000017static const uint8_t max_option_length[] = {
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
33static inline int upper_length(int length, int opt_index)
34{
35 return max_option_length[opt_index] *
Denis Vlasenkob539c842007-11-29 08:17:45 +000036 (length / dhcp_option_lengths[opt_index]);
Mike Frysinger7031f622006-05-08 03:20:50 +000037}
38
39
Denys Vlasenko56f2d062009-06-16 10:25:35 +020040/* note: ip is a pointer to an IP in network order, possibly misaliged */
41static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
Mike Frysinger7031f622006-05-08 03:20:50 +000042{
43 return sprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]);
44}
45
46
47/* really simple implementation, just count the bits */
Denis Vlasenko35ff7462007-11-28 19:23:12 +000048static int mton(uint32_t mask)
Mike Frysinger7031f622006-05-08 03:20:50 +000049{
Denis Vlasenko35ff7462007-11-28 19:23:12 +000050 int i = 0;
51 mask = ntohl(mask); /* 111110000-like bit pattern */
52 while (mask) {
53 i++;
54 mask <<= 1;
55 }
56 return i;
Mike Frysinger7031f622006-05-08 03:20:50 +000057}
58
59
Denis Vlasenko50664732007-02-27 21:15:08 +000060/* Allocate and fill with the text of option 'option'. */
Denis Vlasenkob539c842007-11-29 08:17:45 +000061static char *alloc_fill_opts(uint8_t *option, const struct dhcp_option *type_p, const char *opt_name)
Mike Frysinger7031f622006-05-08 03:20:50 +000062{
Denis Vlasenko50664732007-02-27 21:15:08 +000063 int len, type, optlen;
Mike Frysinger7031f622006-05-08 03:20:50 +000064 uint16_t val_u16;
65 int16_t val_s16;
66 uint32_t val_u32;
67 int32_t val_s32;
Denis Vlasenko50664732007-02-27 21:15:08 +000068 char *dest, *ret;
Mike Frysinger7031f622006-05-08 03:20:50 +000069
Denis Vlasenko50664732007-02-27 21:15:08 +000070 len = option[OPT_LEN - 2];
Mike Frysinger7031f622006-05-08 03:20:50 +000071 type = type_p->flags & TYPE_MASK;
Denis Vlasenkob539c842007-11-29 08:17:45 +000072 optlen = dhcp_option_lengths[type];
Denis Vlasenko50664732007-02-27 21:15:08 +000073
Denis Vlasenkob539c842007-11-29 08:17:45 +000074 dest = ret = xmalloc(upper_length(len, type) + strlen(opt_name) + 2);
75 dest += sprintf(ret, "%s=", opt_name);
Denis Vlasenko50664732007-02-27 21:15:08 +000076
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000077 for (;;) {
Mike Frysinger7031f622006-05-08 03:20:50 +000078 switch (type) {
79 case OPTION_IP_PAIR:
Denys Vlasenko56f2d062009-06-16 10:25:35 +020080 dest += sprint_nip(dest, "", option);
Denis Vlasenkofbd29182007-04-07 01:05:47 +000081 *dest++ = '/';
Mike Frysinger7031f622006-05-08 03:20:50 +000082 option += 4;
83 optlen = 4;
84 case OPTION_IP: /* Works regardless of host byte order. */
Denys Vlasenko56f2d062009-06-16 10:25:35 +020085 dest += sprint_nip(dest, "", option);
Mike Frysinger7031f622006-05-08 03:20:50 +000086 break;
87 case OPTION_BOOLEAN:
88 dest += sprintf(dest, *option ? "yes" : "no");
89 break;
90 case OPTION_U8:
91 dest += sprintf(dest, "%u", *option);
92 break;
93 case OPTION_U16:
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000094 move_from_unaligned16(val_u16, option);
Mike Frysinger7031f622006-05-08 03:20:50 +000095 dest += sprintf(dest, "%u", ntohs(val_u16));
96 break;
97 case OPTION_S16:
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000098 move_from_unaligned16(val_s16, option);
Mike Frysinger7031f622006-05-08 03:20:50 +000099 dest += sprintf(dest, "%d", ntohs(val_s16));
100 break;
101 case OPTION_U32:
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000102 move_from_unaligned32(val_u32, option);
Mike Frysinger7031f622006-05-08 03:20:50 +0000103 dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32));
104 break;
105 case OPTION_S32:
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000106 move_from_unaligned32(val_s32, option);
Mike Frysinger7031f622006-05-08 03:20:50 +0000107 dest += sprintf(dest, "%ld", (long) ntohl(val_s32));
108 break;
109 case OPTION_STRING:
110 memcpy(dest, option, len);
111 dest[len] = '\0';
Denis Vlasenko50664732007-02-27 21:15:08 +0000112 return ret; /* Short circuit this case */
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000113#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000114 case OPTION_STR1035:
115 /* unpack option into dest; use ret for prefix (i.e., "optname=") */
116 dest = dname_dec(option, len, ret);
117 free(ret);
118 return dest;
119#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;
Mike Frysinger7031f622006-05-08 03:20:50 +0000125 dest += sprintf(dest, " ");
126 }
Denis Vlasenko50664732007-02-27 21:15:08 +0000127 return ret;
Mike Frysinger7031f622006-05-08 03:20:50 +0000128}
129
130
131/* put all the parameters into an environment */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200132static char **fill_envp(struct dhcp_packet *packet)
Mike Frysinger7031f622006-05-08 03:20:50 +0000133{
134 int num_options = 0;
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000135 int i;
136 char **envp, **curr;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000137 const char *opt_name;
Mike Frysinger7031f622006-05-08 03:20:50 +0000138 uint8_t *temp;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000139 uint8_t over = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000140
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000141 if (packet) {
142 for (i = 0; dhcp_options[i].code; i++) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000143 if (get_option(packet, dhcp_options[i].code)) {
144 num_options++;
145 if (dhcp_options[i].code == DHCP_SUBNET)
146 num_options++; /* for mton */
147 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000148 }
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200149 if (packet->siaddr_nip)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000150 num_options++;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000151 temp = get_option(packet, DHCP_OPTION_OVERLOAD);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000152 if (temp)
Mike Frysinger7031f622006-05-08 03:20:50 +0000153 over = *temp;
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000154 if (!(over & FILE_FIELD) && packet->file[0])
155 num_options++;
156 if (!(over & SNAME_FIELD) && packet->sname[0])
157 num_options++;
Mike Frysinger7031f622006-05-08 03:20:50 +0000158 }
159
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000160 curr = envp = xzalloc(sizeof(char *) * (num_options + 3));
161 *curr = xasprintf("interface=%s", client_config.interface);
162 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000163
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000164 if (packet == NULL)
165 return envp;
Mike Frysinger7031f622006-05-08 03:20:50 +0000166
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000167 *curr = xmalloc(sizeof("ip=255.255.255.255"));
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200168 sprint_nip(*curr, "ip=", (uint8_t *) &packet->yiaddr);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000169 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000170
Denis Vlasenkob539c842007-11-29 08:17:45 +0000171 opt_name = dhcp_option_strings;
172 i = 0;
173 while (*opt_name) {
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000174 temp = get_option(packet, dhcp_options[i].code);
Denis Vlasenkoc2f5b022006-11-28 00:21:46 +0000175 if (!temp)
Denis Vlasenkob539c842007-11-29 08:17:45 +0000176 goto next;
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000177 *curr = alloc_fill_opts(temp, &dhcp_options[i], opt_name);
178 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000179
180 /* Fill in a subnet bits option for things like /24 */
181 if (dhcp_options[i].code == DHCP_SUBNET) {
Denis Vlasenko35ff7462007-11-28 19:23:12 +0000182 uint32_t subnet;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000183 move_from_unaligned32(subnet, temp);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000184 *curr = xasprintf("mask=%d", mton(subnet));
185 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000186 }
Denis Vlasenkob539c842007-11-29 08:17:45 +0000187 next:
188 opt_name += strlen(opt_name) + 1;
189 i++;
Mike Frysinger7031f622006-05-08 03:20:50 +0000190 }
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200191 if (packet->siaddr_nip) {
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000192 *curr = xmalloc(sizeof("siaddr=255.255.255.255"));
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200193 sprint_nip(*curr, "siaddr=", (uint8_t *) &packet->siaddr_nip);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000194 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000195 }
196 if (!(over & FILE_FIELD) && packet->file[0]) {
197 /* watch out for invalid packets */
198 packet->file[sizeof(packet->file) - 1] = '\0';
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000199 *curr = xasprintf("boot_file=%s", packet->file);
200 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000201 }
202 if (!(over & SNAME_FIELD) && packet->sname[0]) {
203 /* watch out for invalid packets */
204 packet->sname[sizeof(packet->sname) - 1] = '\0';
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000205 *curr = xasprintf("sname=%s", packet->sname);
206 putenv(*curr++);
Mike Frysinger7031f622006-05-08 03:20:50 +0000207 }
208 return envp;
209}
210
211
212/* Call a script with a par file and env vars */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200213void FAST_FUNC udhcp_run_script(struct dhcp_packet *packet, const char *name)
Mike Frysinger7031f622006-05-08 03:20:50 +0000214{
Mike Frysinger7031f622006-05-08 03:20:50 +0000215 char **envp, **curr;
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000216 char *argv[3];
Mike Frysinger7031f622006-05-08 03:20:50 +0000217
218 if (client_config.script == NULL)
219 return;
220
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200221 log1("Executing %s", client_config.script);
Mike Frysinger7031f622006-05-08 03:20:50 +0000222
223 envp = fill_envp(packet);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000224
Mike Frysinger7031f622006-05-08 03:20:50 +0000225 /* call 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++) {
232 bb_unsetenv(*curr);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000233 free(*curr);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000234 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000235 free(envp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000236}