blob: 41b680d6b44465379fd0c280ae1d03014d602b98 [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001/* script.c
2 *
3 * Functions to call the DHCP client notification scripts
4 *
5 * Russ Dill <Russ.Dill@asu.edu> July 2001
6 *
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.
11 *
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.
16 *
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.
20 */
21
22#include <string.h>
23#include <unistd.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <sys/socket.h>
27#include <netinet/in.h>
28#include <arpa/inet.h>
29#include <sys/types.h>
30#include <sys/wait.h>
Russ Dill61fb4892002-10-14 21:41:28 +000031
32#include "options.h"
33#include "dhcpd.h"
34#include "dhcpc.h"
Russ Dill61fb4892002-10-14 21:41:28 +000035#include "options.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000036#include "common.h"
Russ Dill61fb4892002-10-14 21:41:28 +000037
38/* get a rough idea of how long an option will be (rounding up...) */
Glenn L McGrath24833432003-06-10 17:22:49 +000039static const int max_option_length[] = {
Russ Dill61fb4892002-10-14 21:41:28 +000040 [OPTION_IP] = sizeof("255.255.255.255 "),
41 [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2,
42 [OPTION_STRING] = 1,
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 "),
49};
50
51
Glenn L McGrath24833432003-06-10 17:22:49 +000052static inline int upper_length(int length, int opt_index)
Russ Dill61fb4892002-10-14 21:41:28 +000053{
Glenn L McGrath24833432003-06-10 17:22:49 +000054 return max_option_length[opt_index] *
55 (length / option_lengths[opt_index]);
Russ Dill61fb4892002-10-14 21:41:28 +000056}
57
58
Glenn L McGrath24833432003-06-10 17:22:49 +000059static int sprintip(char *dest, unsigned char *ip)
Glenn L McGrath6b5bd0e2002-12-08 22:17:54 +000060{
Glenn L McGrath24833432003-06-10 17:22:49 +000061 return sprintf(dest, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
62}
63
64static void asprintip(char **dest, char *pre, unsigned char *ip)
65{
66 asprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]);
Glenn L McGrath6b5bd0e2002-12-08 22:17:54 +000067}
68
Russ Dill1eb7a172002-12-11 21:12:45 +000069
70/* really simple implementation, just count the bits */
71static int mton(struct in_addr *mask)
72{
73 int i;
Russ Dill858fad72003-02-12 22:20:19 +000074 unsigned long bits = ntohl(mask->s_addr);
75 /* too bad one can't check the carry bit, etc in c bit
76 * shifting */
77 for (i = 0; i < 32 && !((bits >> i) & 1); i++);
78 return 32 - i;
Glenn L McGrath6b5bd0e2002-12-08 22:17:54 +000079}
Russ Dill1eb7a172002-12-11 21:12:45 +000080
Russ Dill61fb4892002-10-14 21:41:28 +000081
82/* Fill dest with the text of option 'option'. */
83static void fill_options(char *dest, unsigned char *option, struct dhcp_option *type_p)
84{
85 int type, optlen;
86 u_int16_t val_u16;
87 int16_t val_s16;
88 u_int32_t val_u32;
89 int32_t val_s32;
90 int len = option[OPT_LEN - 2];
91
92 dest += sprintf(dest, "%s=", type_p->name);
93
94 type = type_p->flags & TYPE_MASK;
95 optlen = option_lengths[type];
96 for(;;) {
97 switch (type) {
98 case OPTION_IP_PAIR:
Glenn L McGrath24833432003-06-10 17:22:49 +000099 dest += sprintip(dest, option);
Russ Dill61fb4892002-10-14 21:41:28 +0000100 *(dest++) = '/';
101 option += 4;
102 optlen = 4;
103 case OPTION_IP: /* Works regardless of host byte order. */
Glenn L McGrath24833432003-06-10 17:22:49 +0000104 dest += sprintip(dest, option);
Russ Dill61fb4892002-10-14 21:41:28 +0000105 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000106 case OPTION_BOOLEAN:
Russ Dill1eb7a172002-12-11 21:12:45 +0000107 dest += sprintf(dest, *option ? "yes" : "no");
Russ Dill61fb4892002-10-14 21:41:28 +0000108 break;
109 case OPTION_U8:
Russ Dill1eb7a172002-12-11 21:12:45 +0000110 dest += sprintf(dest, "%u", *option);
Russ Dill61fb4892002-10-14 21:41:28 +0000111 break;
112 case OPTION_U16:
113 memcpy(&val_u16, option, 2);
Russ Dill1eb7a172002-12-11 21:12:45 +0000114 dest += sprintf(dest, "%u", ntohs(val_u16));
Russ Dill61fb4892002-10-14 21:41:28 +0000115 break;
116 case OPTION_S16:
117 memcpy(&val_s16, option, 2);
Russ Dill1eb7a172002-12-11 21:12:45 +0000118 dest += sprintf(dest, "%d", ntohs(val_s16));
Russ Dill61fb4892002-10-14 21:41:28 +0000119 break;
120 case OPTION_U32:
121 memcpy(&val_u32, option, 4);
Russ Dill1eb7a172002-12-11 21:12:45 +0000122 dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32));
Russ Dill61fb4892002-10-14 21:41:28 +0000123 break;
124 case OPTION_S32:
125 memcpy(&val_s32, option, 4);
Russ Dill1eb7a172002-12-11 21:12:45 +0000126 dest += sprintf(dest, "%ld", (long) ntohl(val_s32));
Russ Dill61fb4892002-10-14 21:41:28 +0000127 break;
128 case OPTION_STRING:
129 memcpy(dest, option, len);
130 dest[len] = '\0';
131 return; /* Short circuit this case */
132 }
133 option += optlen;
134 len -= optlen;
135 if (len <= 0) break;
Russ Dill1eb7a172002-12-11 21:12:45 +0000136 dest += sprintf(dest, " ");
Russ Dill61fb4892002-10-14 21:41:28 +0000137 }
138}
139
140
141static char *find_env(const char *prefix, char *defaultstr)
142{
Glenn L McGrath24833432003-06-10 17:22:49 +0000143 char *ptr;
Russ Dill61fb4892002-10-14 21:41:28 +0000144
Glenn L McGrath24833432003-06-10 17:22:49 +0000145 ptr = getenv(prefix);
146 return ptr ? ptr : defaultstr;
Russ Dill61fb4892002-10-14 21:41:28 +0000147}
148
149
150/* put all the paramaters into an environment */
151static char **fill_envp(struct dhcpMessage *packet)
152{
153 int num_options = 0;
154 int i, j;
155 char **envp;
156 unsigned char *temp;
Russ Dill1eb7a172002-12-11 21:12:45 +0000157 struct in_addr subnet;
Russ Dill61fb4892002-10-14 21:41:28 +0000158 char over = 0;
159
160 if (packet == NULL)
161 num_options = 0;
162 else {
163 for (i = 0; options[i].code; i++)
164 if (get_option(packet, options[i].code))
165 num_options++;
166 if (packet->siaddr) num_options++;
167 if ((temp = get_option(packet, DHCP_OPTION_OVER)))
168 over = *temp;
169 if (!(over & FILE_FIELD) && packet->file[0]) num_options++;
170 if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++;
171 }
172
173 envp = xmalloc((num_options + 5) * sizeof(char *));
Russ Dill1eb7a172002-12-11 21:12:45 +0000174 j = 0;
Glenn L McGrath24833432003-06-10 17:22:49 +0000175 asprintf(&envp[j++], "interface=%s", client_config.interface);
Russ Dill1eb7a172002-12-11 21:12:45 +0000176 envp[j++] = find_env("PATH", "PATH=/bin:/usr/bin:/sbin:/usr/sbin");
177 envp[j++] = find_env("HOME", "HOME=/");
Russ Dill61fb4892002-10-14 21:41:28 +0000178
179 if (packet == NULL) {
Russ Dill1eb7a172002-12-11 21:12:45 +0000180 envp[j++] = NULL;
Russ Dill61fb4892002-10-14 21:41:28 +0000181 return envp;
182 }
183
Glenn L McGrath24833432003-06-10 17:22:49 +0000184 asprintip(&envp[j++], "ip=", (unsigned char *) &packet->yiaddr);
Russ Dill1eb7a172002-12-11 21:12:45 +0000185
186
187 for (i = 0; options[i].code; i++) {
188 if (!(temp = get_option(packet, options[i].code)))
189 continue;
Glenn L McGrath24833432003-06-10 17:22:49 +0000190 envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2], options[i].flags & TYPE_MASK) + strlen(options[i].name) + 2);
Russ Dill1eb7a172002-12-11 21:12:45 +0000191 fill_options(envp[j++], temp, &options[i]);
192
193 /* Fill in a subnet bits option for things like /24 */
194 if (options[i].code == DHCP_SUBNET) {
Russ Dill1eb7a172002-12-11 21:12:45 +0000195 memcpy(&subnet, temp, 4);
Glenn L McGrath24833432003-06-10 17:22:49 +0000196 asprintf(&envp[j++], "mask=%d", mton(&subnet));
Russ Dill61fb4892002-10-14 21:41:28 +0000197 }
198 }
199 if (packet->siaddr) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000200 asprintip(&envp[j++], "siaddr=", (unsigned char *) &packet->siaddr);
Russ Dill61fb4892002-10-14 21:41:28 +0000201 }
202 if (!(over & FILE_FIELD) && packet->file[0]) {
203 /* watch out for invalid packets */
204 packet->file[sizeof(packet->file) - 1] = '\0';
Glenn L McGrath24833432003-06-10 17:22:49 +0000205 asprintf(&envp[j++], "boot_file=%s", packet->file);
Russ Dill61fb4892002-10-14 21:41:28 +0000206 }
207 if (!(over & SNAME_FIELD) && packet->sname[0]) {
208 /* watch out for invalid packets */
209 packet->sname[sizeof(packet->sname) - 1] = '\0';
Glenn L McGrath24833432003-06-10 17:22:49 +0000210 asprintf(&envp[j++], "sname=%s", packet->sname);
Russ Dill61fb4892002-10-14 21:41:28 +0000211 }
212 envp[j] = NULL;
213 return envp;
214}
215
216
217/* Call a script with a par file and env vars */
218void run_script(struct dhcpMessage *packet, const char *name)
219{
220 int pid;
221 char **envp;
222
223 if (client_config.script == NULL)
224 return;
225
226 /* call script */
227 pid = fork();
228 if (pid) {
229 waitpid(pid, NULL, 0);
230 return;
231 } else if (pid == 0) {
232 envp = fill_envp(packet);
233
234 /* close fd's? */
235
236 /* exec script */
237 DEBUG(LOG_INFO, "execle'ing %s", client_config.script);
238 execle(client_config.script, client_config.script,
239 name, NULL, envp);
Glenn L McGrath24833432003-06-10 17:22:49 +0000240 LOG(LOG_ERR, "script %s failed: %m", client_config.script);
Russ Dill61fb4892002-10-14 21:41:28 +0000241 exit(1);
242 }
243}