3 * Functions to call the DHCP client notification scripts
5 * Russ Dill <Russ.Dill@asu.edu> July 2001
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <sys/types.h>
25 /* get a rough idea of how long an option will be (rounding up...) */
26 static const int max_option_length[] = {
27 [OPTION_IP] = sizeof("255.255.255.255 "),
28 [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2,
30 [OPTION_BOOLEAN] = sizeof("yes "),
31 [OPTION_U8] = sizeof("255 "),
32 [OPTION_U16] = sizeof("65535 "),
33 [OPTION_S16] = sizeof("-32768 "),
34 [OPTION_U32] = sizeof("4294967295 "),
35 [OPTION_S32] = sizeof("-2147483684 "),
39 static inline int upper_length(int length, int opt_index)
41 return max_option_length[opt_index] *
42 (length / option_lengths[opt_index]);
46 static int sprintip(char *dest, char *pre, uint8_t *ip)
48 return sprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]);
52 /* really simple implementation, just count the bits */
53 static int mton(struct in_addr *mask)
56 unsigned long bits = ntohl(mask->s_addr);
57 /* too bad one can't check the carry bit, etc in c bit
59 for (i = 0; i < 32 && !((bits >> i) & 1); i++);
64 /* Fill dest with the text of option 'option'. */
65 static void fill_options(char *dest, uint8_t *option, struct dhcp_option *type_p)
72 int len = option[OPT_LEN - 2];
74 dest += sprintf(dest, "%s=", type_p->name);
76 type = type_p->flags & TYPE_MASK;
77 optlen = option_lengths[type];
81 dest += sprintip(dest, "", option);
85 case OPTION_IP: /* Works regardless of host byte order. */
86 dest += sprintip(dest, "", option);
89 dest += sprintf(dest, *option ? "yes" : "no");
92 dest += sprintf(dest, "%u", *option);
95 memcpy(&val_u16, option, 2);
96 dest += sprintf(dest, "%u", ntohs(val_u16));
99 memcpy(&val_s16, option, 2);
100 dest += sprintf(dest, "%d", ntohs(val_s16));
103 memcpy(&val_u32, option, 4);
104 dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32));
107 memcpy(&val_s32, option, 4);
108 dest += sprintf(dest, "%ld", (long) ntohl(val_s32));
111 memcpy(dest, option, len);
113 return; /* Short circuit this case */
118 dest += sprintf(dest, " ");
123 /* put all the parameters into an environment */
124 static char **fill_envp(struct dhcpMessage *packet)
130 struct in_addr subnet;
136 for (i = 0; dhcp_options[i].code; i++)
137 if (get_option(packet, dhcp_options[i].code)) {
139 if (dhcp_options[i].code == DHCP_SUBNET)
140 num_options++; /* for mton */
142 if (packet->siaddr) num_options++;
143 if ((temp = get_option(packet, DHCP_OPTION_OVER)))
145 if (!(over & FILE_FIELD) && packet->file[0]) num_options++;
146 if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++;
149 envp = xzalloc(sizeof(char *) * (num_options + 5));
151 envp[j++] = bb_xasprintf("interface=%s", client_config.interface);
152 envp[j++] = bb_xasprintf("PATH=%s",
153 getenv("PATH") ? : "/bin:/usr/bin:/sbin:/usr/sbin");
154 envp[j++] = bb_xasprintf("HOME=%s", getenv("HOME") ? : "/");
156 if (packet == NULL) return envp;
158 envp[j] = xmalloc(sizeof("ip=255.255.255.255"));
159 sprintip(envp[j++], "ip=", (uint8_t *) &packet->yiaddr);
162 for (i = 0; dhcp_options[i].code; i++) {
163 if (!(temp = get_option(packet, dhcp_options[i].code)))
165 envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2],
166 dhcp_options[i].flags & TYPE_MASK) + strlen(dhcp_options[i].name) + 2);
167 fill_options(envp[j++], temp, &dhcp_options[i]);
169 /* Fill in a subnet bits option for things like /24 */
170 if (dhcp_options[i].code == DHCP_SUBNET) {
171 memcpy(&subnet, temp, 4);
172 envp[j++] = bb_xasprintf("mask=%d", mton(&subnet));
175 if (packet->siaddr) {
176 envp[j] = xmalloc(sizeof("siaddr=255.255.255.255"));
177 sprintip(envp[j++], "siaddr=", (uint8_t *) &packet->siaddr);
179 if (!(over & FILE_FIELD) && packet->file[0]) {
180 /* watch out for invalid packets */
181 packet->file[sizeof(packet->file) - 1] = '\0';
182 envp[j++] = bb_xasprintf("boot_file=%s", packet->file);
184 if (!(over & SNAME_FIELD) && packet->sname[0]) {
185 /* watch out for invalid packets */
186 packet->sname[sizeof(packet->sname) - 1] = '\0';
187 envp[j++] = bb_xasprintf("sname=%s", packet->sname);
193 /* Call a script with a par file and env vars */
194 void udhcp_run_script(struct dhcpMessage *packet, const char *name)
199 if (client_config.script == NULL)
202 DEBUG(LOG_INFO, "vforking and execle'ing %s", client_config.script);
204 envp = fill_envp(packet);
208 waitpid(pid, NULL, 0);
209 for (curr = envp; *curr; curr++) free(*curr);
212 } else if (pid == 0) {
216 execle(client_config.script, client_config.script,
218 LOG(LOG_ERR, "script %s failed: %m", client_config.script);