3 * Functions to call the DHCP client notification scripts
5 * Russ Dill <Russ.Dill@asu.edu> July 2001
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <sys/types.h>
38 /* get a rough idea of how long an option will be (rounding up...) */
39 static const int max_option_length[] = {
40 [OPTION_IP] = sizeof("255.255.255.255 "),
41 [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2,
43 [OPTION_BOOLEAN] = sizeof("yes "),
44 [OPTION_U8] = sizeof("255 "),
45 [OPTION_U16] = sizeof("65535 "),
46 [OPTION_S16] = sizeof("-32768 "),
47 [OPTION_U32] = sizeof("4294967295 "),
48 [OPTION_S32] = sizeof("-2147483684 "),
52 static inline int upper_length(int length, int opt_index)
54 return max_option_length[opt_index] *
55 (length / option_lengths[opt_index]);
59 static int sprintip(char *dest, char *pre, uint8_t *ip)
61 return sprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]);
65 /* really simple implementation, just count the bits */
66 static int mton(struct in_addr *mask)
69 unsigned long bits = ntohl(mask->s_addr);
70 /* too bad one can't check the carry bit, etc in c bit
72 for (i = 0; i < 32 && !((bits >> i) & 1); i++);
77 /* Fill dest with the text of option 'option'. */
78 static void fill_options(char *dest, uint8_t *option, struct dhcp_option *type_p)
85 int len = option[OPT_LEN - 2];
87 dest += sprintf(dest, "%s=", type_p->name);
89 type = type_p->flags & TYPE_MASK;
90 optlen = option_lengths[type];
94 dest += sprintip(dest, "", option);
98 case OPTION_IP: /* Works regardless of host byte order. */
99 dest += sprintip(dest, "", option);
102 dest += sprintf(dest, *option ? "yes" : "no");
105 dest += sprintf(dest, "%u", *option);
108 memcpy(&val_u16, option, 2);
109 dest += sprintf(dest, "%u", ntohs(val_u16));
112 memcpy(&val_s16, option, 2);
113 dest += sprintf(dest, "%d", ntohs(val_s16));
116 memcpy(&val_u32, option, 4);
117 dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32));
120 memcpy(&val_s32, option, 4);
121 dest += sprintf(dest, "%ld", (long) ntohl(val_s32));
124 memcpy(dest, option, len);
126 return; /* Short circuit this case */
131 dest += sprintf(dest, " ");
136 /* put all the parameters into an environment */
137 static char **fill_envp(struct dhcpMessage *packet)
143 struct in_addr subnet;
149 for (i = 0; dhcp_options[i].code; i++)
150 if (get_option(packet, dhcp_options[i].code)) {
152 if (dhcp_options[i].code == DHCP_SUBNET)
153 num_options++; /* for mton */
155 if (packet->siaddr) num_options++;
156 if ((temp = get_option(packet, DHCP_OPTION_OVER)))
158 if (!(over & FILE_FIELD) && packet->file[0]) num_options++;
159 if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++;
162 envp = xcalloc(sizeof(char *), num_options + 5);
164 asprintf(&envp[j++], "interface=%s", client_config.interface);
165 asprintf(&envp[j++], "%s=%s", "PATH",
166 getenv("PATH") ? : "/bin:/usr/bin:/sbin:/usr/sbin");
167 asprintf(&envp[j++], "%s=%s", "HOME", getenv("HOME") ? : "/");
169 if (packet == NULL) return envp;
171 envp[j] = xmalloc(sizeof("ip=255.255.255.255"));
172 sprintip(envp[j++], "ip=", (uint8_t *) &packet->yiaddr);
175 for (i = 0; dhcp_options[i].code; i++) {
176 if (!(temp = get_option(packet, dhcp_options[i].code)))
178 envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2],
179 dhcp_options[i].flags & TYPE_MASK) + strlen(dhcp_options[i].name) + 2);
180 fill_options(envp[j++], temp, &dhcp_options[i]);
182 /* Fill in a subnet bits option for things like /24 */
183 if (dhcp_options[i].code == DHCP_SUBNET) {
184 memcpy(&subnet, temp, 4);
185 asprintf(&envp[j++], "mask=%d", mton(&subnet));
188 if (packet->siaddr) {
189 envp[j] = xmalloc(sizeof("siaddr=255.255.255.255"));
190 sprintip(envp[j++], "siaddr=", (uint8_t *) &packet->siaddr);
192 if (!(over & FILE_FIELD) && packet->file[0]) {
193 /* watch out for invalid packets */
194 packet->file[sizeof(packet->file) - 1] = '\0';
195 asprintf(&envp[j++], "boot_file=%s", packet->file);
197 if (!(over & SNAME_FIELD) && packet->sname[0]) {
198 /* watch out for invalid packets */
199 packet->sname[sizeof(packet->sname) - 1] = '\0';
200 asprintf(&envp[j++], "sname=%s", packet->sname);
206 /* Call a script with a par file and env vars */
207 void run_script(struct dhcpMessage *packet, const char *name)
212 if (client_config.script == NULL)
215 DEBUG(LOG_INFO, "vforking and execle'ing %s", client_config.script);
217 envp = fill_envp(packet);
221 waitpid(pid, NULL, 0);
222 for (curr = envp; *curr; curr++) free(*curr);
225 } else if (pid == 0) {
229 execle(client_config.script, client_config.script,
231 LOG(LOG_ERR, "script %s failed: %m", client_config.script);