make udhcp work under uclinux, to an extent
[oweals/busybox.git] / networking / udhcp / script.c
1 /* 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>
31
32 #include "options.h"
33 #include "dhcpd.h"
34 #include "dhcpc.h"
35 #include "options.h"
36 #include "common.h"
37
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,
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
52 static inline int upper_length(int length, int opt_index)
53 {
54         return max_option_length[opt_index] *
55                 (length / option_lengths[opt_index]);
56 }
57
58
59 static int sprintip(char *dest, unsigned char *ip)
60 {
61         return sprintf(dest, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
62 }
63
64 static 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]);
67 }
68
69
70 /* really simple implementation, just count the bits */
71 static int mton(struct in_addr *mask)
72 {
73         int i;
74         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;
79 }
80
81
82 /* Fill dest with the text of option 'option'. */
83 static 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:
99                         dest += sprintip(dest, option);
100                         *(dest++) = '/';
101                         option += 4;
102                         optlen = 4;
103                 case OPTION_IP: /* Works regardless of host byte order. */
104                         dest += sprintip(dest, option);
105                         break;
106                 case OPTION_BOOLEAN:
107                         dest += sprintf(dest, *option ? "yes" : "no");
108                         break;
109                 case OPTION_U8:
110                         dest += sprintf(dest, "%u", *option);
111                         break;
112                 case OPTION_U16:
113                         memcpy(&val_u16, option, 2);
114                         dest += sprintf(dest, "%u", ntohs(val_u16));
115                         break;
116                 case OPTION_S16:
117                         memcpy(&val_s16, option, 2);
118                         dest += sprintf(dest, "%d", ntohs(val_s16));
119                         break;
120                 case OPTION_U32:
121                         memcpy(&val_u32, option, 4);
122                         dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32));
123                         break;
124                 case OPTION_S32:
125                         memcpy(&val_s32, option, 4);
126                         dest += sprintf(dest, "%ld", (long) ntohl(val_s32));
127                         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;
136                 dest += sprintf(dest, " ");
137         }
138 }
139
140
141 static char *find_env(const char *prefix, char *defaultstr)
142 {
143         char *ptr;
144
145         ptr = getenv(prefix);
146         return ptr ? ptr : defaultstr;
147 }
148
149
150 /* put all the paramaters into an environment */
151 static char **fill_envp(struct dhcpMessage *packet)
152 {
153         int num_options = 0;
154         int i, j;
155         char **envp;
156         unsigned char *temp;
157         struct in_addr subnet;
158         char over = 0;
159
160         if (packet == NULL)
161                 num_options = 0;
162         else {
163                 for (i = 0; dhcp_options[i].code; i++)
164                         if (get_option(packet, dhcp_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 *));
174         j = 0;
175         asprintf(&envp[j++], "interface=%s", client_config.interface);
176         envp[j++] = find_env("PATH", "PATH=/bin:/usr/bin:/sbin:/usr/sbin");
177         envp[j++] = find_env("HOME", "HOME=/");
178
179         if (packet == NULL) {
180                 envp[j++] = NULL;
181                 return envp;
182         }
183
184         asprintip(&envp[j++], "ip=", (unsigned char *) &packet->yiaddr);
185
186
187         for (i = 0; dhcp_options[i].code; i++) {
188                 if (!(temp = get_option(packet, dhcp_options[i].code)))
189                         continue;
190                 envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2],
191                         dhcp_options[i].flags & TYPE_MASK) + strlen(dhcp_options[i].name) + 2);
192                 fill_options(envp[j++], temp, &dhcp_options[i]);
193
194                 /* Fill in a subnet bits option for things like /24 */
195                 if (dhcp_options[i].code == DHCP_SUBNET) {
196                         memcpy(&subnet, temp, 4);
197                         asprintf(&envp[j++], "mask=%d", mton(&subnet));
198                 }
199         }
200         if (packet->siaddr) {
201                 asprintip(&envp[j++], "siaddr=", (unsigned char *) &packet->siaddr);
202         }
203         if (!(over & FILE_FIELD) && packet->file[0]) {
204                 /* watch out for invalid packets */
205                 packet->file[sizeof(packet->file) - 1] = '\0';
206                 asprintf(&envp[j++], "boot_file=%s", packet->file);
207         }
208         if (!(over & SNAME_FIELD) && packet->sname[0]) {
209                 /* watch out for invalid packets */
210                 packet->sname[sizeof(packet->sname) - 1] = '\0';
211                 asprintf(&envp[j++], "sname=%s", packet->sname);
212         }       
213         envp[j] = NULL;
214         return envp;
215 }
216
217
218 /* Call a script with a par file and env vars */
219 void run_script(struct dhcpMessage *packet, const char *name)
220 {
221         int pid;
222         char **envp;
223
224         if (client_config.script == NULL)
225                 return;
226
227         /* call script */
228         pid = vfork();
229         if (pid) {
230                 waitpid(pid, NULL, 0);
231                 return;
232         } else if (pid == 0) {
233                 envp = fill_envp(packet);
234                 
235                 /* close fd's? */
236                 
237                 /* exec script */
238 #ifndef __uClinux__
239                 DEBUG(LOG_INFO, "execle'ing %s", client_config.script);
240 #endif /* __uClinux__ */
241                 execle(client_config.script, client_config.script,
242                        name, NULL, envp);
243                 LOG(LOG_ERR, "script %s failed: %m", client_config.script);
244                 exit(1);
245         }                       
246 }