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>
40 /* get a rough idea of how long an option will be (rounding up...) */
41 static int max_option_length[] = {
42 [OPTION_IP] = sizeof("255.255.255.255 "),
43 [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2,
45 [OPTION_BOOLEAN] = sizeof("yes "),
46 [OPTION_U8] = sizeof("255 "),
47 [OPTION_U16] = sizeof("65535 "),
48 [OPTION_S16] = sizeof("-32768 "),
49 [OPTION_U32] = sizeof("4294967295 "),
50 [OPTION_S32] = sizeof("-2147483684 "),
54 static int upper_length(int length, struct dhcp_option *option)
56 return max_option_length[option->flags & TYPE_MASK] *
57 (length / option_lengths[option->flags & TYPE_MASK]);
61 static int sprintip(char *dest, char *pre, unsigned char *ip) {
62 return sprintf(dest, "%s%d.%d.%d.%d ", pre, ip[0], ip[1], ip[2], ip[3]);
66 /* Fill dest with the text of option 'option'. */
67 static void fill_options(char *dest, unsigned char *option, struct dhcp_option *type_p)
74 int len = option[OPT_LEN - 2];
76 dest += sprintf(dest, "%s=", type_p->name);
78 type = type_p->flags & TYPE_MASK;
79 optlen = option_lengths[type];
83 dest += sprintip(dest, "", option);
87 case OPTION_IP: /* Works regardless of host byte order. */
88 dest += sprintip(dest, "", option);
91 dest += sprintf(dest, *option ? "yes " : "no ");
94 dest += sprintf(dest, "%u ", *option);
97 memcpy(&val_u16, option, 2);
98 dest += sprintf(dest, "%u ", ntohs(val_u16));
101 memcpy(&val_s16, option, 2);
102 dest += sprintf(dest, "%d ", ntohs(val_s16));
105 memcpy(&val_u32, option, 4);
106 dest += sprintf(dest, "%lu ", (unsigned long) ntohl(val_u32));
109 memcpy(&val_s32, option, 4);
110 dest += sprintf(dest, "%ld ", (long) ntohl(val_s32));
113 memcpy(dest, option, len);
115 return; /* Short circuit this case */
124 static char *find_env(const char *prefix, char *defaultstr)
126 extern char **environ;
128 const int len = strlen(prefix);
130 for (ptr = environ; *ptr != NULL; ptr++) {
131 if (strncmp(prefix, *ptr, len) == 0)
138 /* put all the paramaters into an environment */
139 static char **fill_envp(struct dhcpMessage *packet)
150 for (i = 0; options[i].code; i++)
151 if (get_option(packet, options[i].code))
153 if (packet->siaddr) num_options++;
154 if ((temp = get_option(packet, DHCP_OPTION_OVER)))
156 if (!(over & FILE_FIELD) && packet->file[0]) num_options++;
157 if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++;
160 envp = xmalloc((num_options + 5) * sizeof(char *));
161 envp[0] = xmalloc(sizeof("interface=") + strlen(client_config.interface));
162 sprintf(envp[0], "interface=%s", client_config.interface);
163 envp[1] = find_env("PATH", "PATH=/bin:/usr/bin:/sbin:/usr/sbin");
164 envp[2] = find_env("HOME", "HOME=/");
166 if (packet == NULL) {
171 envp[3] = xmalloc(sizeof("ip=255.255.255.255"));
172 sprintip(envp[3], "ip=", (unsigned char *) &packet->yiaddr);
173 for (i = 0, j = 4; options[i].code; i++) {
174 if ((temp = get_option(packet, options[i].code))) {
175 envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2], &options[i]) + strlen(options[i].name) + 2);
176 fill_options(envp[j], temp, &options[i]);
180 if (packet->siaddr) {
181 envp[j] = xmalloc(sizeof("siaddr=255.255.255.255"));
182 sprintip(envp[j++], "siaddr=", (unsigned char *) &packet->siaddr);
184 if (!(over & FILE_FIELD) && packet->file[0]) {
185 /* watch out for invalid packets */
186 packet->file[sizeof(packet->file) - 1] = '\0';
187 envp[j] = xmalloc(sizeof("boot_file=") + strlen(packet->file));
188 sprintf(envp[j++], "boot_file=%s", packet->file);
190 if (!(over & SNAME_FIELD) && packet->sname[0]) {
191 /* watch out for invalid packets */
192 packet->sname[sizeof(packet->sname) - 1] = '\0';
193 envp[j] = xmalloc(sizeof("sname=") + strlen(packet->sname));
194 sprintf(envp[j++], "sname=%s", packet->sname);
201 /* Call a script with a par file and env vars */
202 void run_script(struct dhcpMessage *packet, const char *name)
207 if (client_config.script == NULL)
213 waitpid(pid, NULL, 0);
215 } else if (pid == 0) {
216 envp = fill_envp(packet);
221 DEBUG(LOG_INFO, "execle'ing %s", client_config.script);
222 execle(client_config.script, client_config.script,
224 LOG(LOG_ERR, "script %s failed: %s",
225 client_config.script, strerror(errno));