Implement optional syslog logging using ordinary
[oweals/busybox.git] / networking / udhcp / script.c
1 /* vi: set sw=4 ts=4: */
2 /* script.c
3  *
4  * Functions to call the DHCP client notification scripts
5  *
6  * Russ Dill <Russ.Dill@asu.edu> July 2001
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 #include <string.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20
21 #include "common.h"
22 #include "options.h"
23 #include "dhcpd.h"
24 #include "dhcpc.h"
25
26 /* get a rough idea of how long an option will be (rounding up...) */
27 static const int max_option_length[] = {
28         [OPTION_IP] =           sizeof("255.255.255.255 "),
29         [OPTION_IP_PAIR] =      sizeof("255.255.255.255 ") * 2,
30         [OPTION_STRING] =       1,
31         [OPTION_BOOLEAN] =      sizeof("yes "),
32         [OPTION_U8] =           sizeof("255 "),
33         [OPTION_U16] =          sizeof("65535 "),
34         [OPTION_S16] =          sizeof("-32768 "),
35         [OPTION_U32] =          sizeof("4294967295 "),
36         [OPTION_S32] =          sizeof("-2147483684 "),
37 };
38
39
40 static inline int upper_length(int length, int opt_index)
41 {
42         return max_option_length[opt_index] *
43                 (length / option_lengths[opt_index]);
44 }
45
46
47 static int sprintip(char *dest, char *pre, uint8_t *ip)
48 {
49         return sprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]);
50 }
51
52
53 /* really simple implementation, just count the bits */
54 static int mton(struct in_addr *mask)
55 {
56         int i;
57         unsigned long bits = ntohl(mask->s_addr);
58         /* too bad one can't check the carry bit, etc in c bit
59          * shifting */
60         for (i = 0; i < 32 && !((bits >> i) & 1); i++);
61         return 32 - i;
62 }
63
64
65 /* Fill dest with the text of option 'option'. */
66 static void fill_options(char *dest, uint8_t *option, struct dhcp_option *type_p)
67 {
68         int type, optlen;
69         uint16_t val_u16;
70         int16_t val_s16;
71         uint32_t val_u32;
72         int32_t val_s32;
73         int len = option[OPT_LEN - 2];
74
75         dest += sprintf(dest, "%s=", type_p->name);
76
77         type = type_p->flags & TYPE_MASK;
78         optlen = option_lengths[type];
79         for(;;) {
80                 switch (type) {
81                 case OPTION_IP_PAIR:
82                         dest += sprintip(dest, "", option);
83                         *(dest++) = '/';
84                         option += 4;
85                         optlen = 4;
86                 case OPTION_IP: /* Works regardless of host byte order. */
87                         dest += sprintip(dest, "", option);
88                         break;
89                 case OPTION_BOOLEAN:
90                         dest += sprintf(dest, *option ? "yes" : "no");
91                         break;
92                 case OPTION_U8:
93                         dest += sprintf(dest, "%u", *option);
94                         break;
95                 case OPTION_U16:
96                         memcpy(&val_u16, option, 2);
97                         dest += sprintf(dest, "%u", ntohs(val_u16));
98                         break;
99                 case OPTION_S16:
100                         memcpy(&val_s16, option, 2);
101                         dest += sprintf(dest, "%d", ntohs(val_s16));
102                         break;
103                 case OPTION_U32:
104                         memcpy(&val_u32, option, 4);
105                         dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32));
106                         break;
107                 case OPTION_S32:
108                         memcpy(&val_s32, option, 4);
109                         dest += sprintf(dest, "%ld", (long) ntohl(val_s32));
110                         break;
111                 case OPTION_STRING:
112                         memcpy(dest, option, len);
113                         dest[len] = '\0';
114                         return;  /* Short circuit this case */
115                 }
116                 option += optlen;
117                 len -= optlen;
118                 if (len <= 0) break;
119                 dest += sprintf(dest, " ");
120         }
121 }
122
123
124 /* put all the parameters into an environment */
125 static char **fill_envp(struct dhcpMessage *packet)
126 {
127         int num_options = 0;
128         int i, j;
129         char **envp;
130         uint8_t *temp;
131         struct in_addr subnet;
132         char over = 0;
133
134         if (packet == NULL)
135                 num_options = 0;
136         else {
137                 for (i = 0; dhcp_options[i].code; i++)
138                         if (get_option(packet, dhcp_options[i].code)) {
139                                 num_options++;
140                                 if (dhcp_options[i].code == DHCP_SUBNET)
141                                         num_options++; /* for mton */
142                         }
143                 if (packet->siaddr) num_options++;
144                 if ((temp = get_option(packet, DHCP_OPTION_OVER)))
145                         over = *temp;
146                 if (!(over & FILE_FIELD) && packet->file[0]) num_options++;
147                 if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++;
148         }
149
150         envp = xzalloc(sizeof(char *) * (num_options + 5));
151         j = 0;
152         envp[j++] = xasprintf("interface=%s", client_config.interface);
153         envp[j++] = xasprintf("PATH=%s",
154                 getenv("PATH") ? : "/bin:/usr/bin:/sbin:/usr/sbin");
155         envp[j++] = xasprintf("HOME=%s", getenv("HOME") ? : "/");
156
157         if (packet == NULL) return envp;
158
159         envp[j] = xmalloc(sizeof("ip=255.255.255.255"));
160         sprintip(envp[j++], "ip=", (uint8_t *) &packet->yiaddr);
161
162
163         for (i = 0; dhcp_options[i].code; i++) {
164                 if (!(temp = get_option(packet, dhcp_options[i].code)))
165                         continue;
166                 envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2],
167                         dhcp_options[i].flags & TYPE_MASK) + strlen(dhcp_options[i].name) + 2);
168                 fill_options(envp[j++], temp, &dhcp_options[i]);
169
170                 /* Fill in a subnet bits option for things like /24 */
171                 if (dhcp_options[i].code == DHCP_SUBNET) {
172                         memcpy(&subnet, temp, 4);
173                         envp[j++] = xasprintf("mask=%d", mton(&subnet));
174                 }
175         }
176         if (packet->siaddr) {
177                 envp[j] = xmalloc(sizeof("siaddr=255.255.255.255"));
178                 sprintip(envp[j++], "siaddr=", (uint8_t *) &packet->siaddr);
179         }
180         if (!(over & FILE_FIELD) && packet->file[0]) {
181                 /* watch out for invalid packets */
182                 packet->file[sizeof(packet->file) - 1] = '\0';
183                 envp[j++] = xasprintf("boot_file=%s", packet->file);
184         }
185         if (!(over & SNAME_FIELD) && packet->sname[0]) {
186                 /* watch out for invalid packets */
187                 packet->sname[sizeof(packet->sname) - 1] = '\0';
188                 envp[j++] = xasprintf("sname=%s", packet->sname);
189         }
190         return envp;
191 }
192
193
194 /* Call a script with a par file and env vars */
195 void udhcp_run_script(struct dhcpMessage *packet, const char *name)
196 {
197         int pid;
198         char **envp, **curr;
199
200         if (client_config.script == NULL)
201                 return;
202
203         DEBUG("vfork'ing and execle'ing %s", client_config.script);
204
205         envp = fill_envp(packet);
206         /* call script */
207         pid = vfork();
208         if (pid) {
209                 waitpid(pid, NULL, 0);
210                 for (curr = envp; *curr; curr++) free(*curr);
211                 free(envp);
212                 return;
213         } else if (pid == 0) {
214                 /* close fd's? */
215
216                 /* exec script */
217                 execle(client_config.script, client_config.script,
218                        name, NULL, envp);
219                 bb_perror_msg("script %s failed", client_config.script);
220                 exit(1);
221         }
222 }