Fix kbuild bugs noticed by Bernhard Fischer <rep.nop@aon.at>
[oweals/busybox.git] / networking / udhcp / files.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * files.c -- DHCP server file manipulation *
4  * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
5  */
6
7 #include <sys/socket.h>
8 #include <arpa/inet.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <time.h>
12 #include <ctype.h>
13 #include <netdb.h>
14
15 #include <netinet/ether.h>
16 #include "static_leases.h"
17
18 #include "dhcpd.h"
19 #include "options.h"
20 #include "files.h"
21 #include "common.h"
22
23 /*
24  * Domain names may have 254 chars, and string options can be 254
25  * chars long. However, 80 bytes will be enough for most, and won't
26  * hog up memory. If you have a special application, change it
27  */
28 #define READ_CONFIG_BUF_SIZE 80
29
30 /* on these functions, make sure you datatype matches */
31 static int read_ip(const char *line, void *arg)
32 {
33         struct in_addr *addr = arg;
34         struct hostent *host;
35         int retval = 1;
36
37         if (!inet_aton(line, addr)) {
38                 host = gethostbyname(line);
39                 if (host)
40                         addr->s_addr = *((unsigned long *) host->h_addr_list[0]);
41                 else retval = 0;
42         }
43         return retval;
44 }
45
46 static int read_mac(const char *line, void *arg)
47 {
48         uint8_t *mac_bytes = arg;
49         struct ether_addr *temp_ether_addr;
50         int retval = 1;
51
52         temp_ether_addr = ether_aton(line);
53
54         if(temp_ether_addr == NULL)
55                 retval = 0;
56         else
57                 memcpy(mac_bytes, temp_ether_addr, 6);
58
59         return retval;
60 }
61
62
63 static int read_str(const char *line, void *arg)
64 {
65         char **dest = arg;
66
67         free(*dest);
68         *dest = strdup(line);
69
70         return 1;
71 }
72
73
74 static int read_u32(const char *line, void *arg)
75 {
76         return safe_strtou32(line, (uint32_t*)arg) == 0;
77 }
78
79
80 static int read_yn(const char *line, void *arg)
81 {
82         char *dest = arg;
83         int retval = 1;
84
85         if (!strcasecmp("yes", line))
86                 *dest = 1;
87         else if (!strcasecmp("no", line))
88                 *dest = 0;
89         else retval = 0;
90
91         return retval;
92 }
93
94
95 /* find option 'code' in opt_list */
96 struct option_set *find_option(struct option_set *opt_list, char code)
97 {
98         while (opt_list && opt_list->data[OPT_CODE] < code)
99                 opt_list = opt_list->next;
100
101         if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
102         else return NULL;
103 }
104
105
106 /* add an option to the opt_list */
107 static void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
108 {
109         struct option_set *existing, *new, **curr;
110
111         /* add it to an existing option */
112         if ((existing = find_option(*opt_list, option->code))) {
113                 DEBUG("Attaching option %s to existing member of list", option->name);
114                 if (option->flags & OPTION_LIST) {
115                         if (existing->data[OPT_LEN] + length <= 255) {
116                                 existing->data = realloc(existing->data,
117                                                 existing->data[OPT_LEN] + length + 2);
118                                 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
119                                 existing->data[OPT_LEN] += length;
120                         } /* else, ignore the data, we could put this in a second option in the future */
121                 } /* else, ignore the new data */
122         } else {
123                 DEBUG("Attaching option %s to list", option->name);
124
125                 /* make a new option */
126                 new = xmalloc(sizeof(struct option_set));
127                 new->data = xmalloc(length + 2);
128                 new->data[OPT_CODE] = option->code;
129                 new->data[OPT_LEN] = length;
130                 memcpy(new->data + 2, buffer, length);
131
132                 curr = opt_list;
133                 while (*curr && (*curr)->data[OPT_CODE] < option->code)
134                         curr = &(*curr)->next;
135
136                 new->next = *curr;
137                 *curr = new;
138         }
139 }
140
141
142 /* read a dhcp option and add it to opt_list */
143 static int read_opt(const char *const_line, void *arg)
144 {
145         struct option_set **opt_list = arg;
146         char *opt, *val, *endptr;
147         struct dhcp_option *option;
148         int retval = 0, length;
149         char buffer[8];
150         char *line;
151         uint16_t *result_u16 = (uint16_t *) buffer;
152         uint32_t *result_u32 = (uint32_t *) buffer;
153
154         /* Cheat, the only const line we'll actually get is "" */
155         line = (char *) const_line;
156         if (!(opt = strtok(line, " \t="))) return 0;
157
158         for (option = dhcp_options; option->code; option++)
159                 if (!strcasecmp(option->name, opt))
160                         break;
161
162         if (!option->code) return 0;
163
164         do {
165                 if (!(val = strtok(NULL, ", \t"))) break;
166                 length = option_lengths[option->flags & TYPE_MASK];
167                 retval = 0;
168                 opt = buffer; /* new meaning for variable opt */
169                 switch (option->flags & TYPE_MASK) {
170                 case OPTION_IP:
171                         retval = read_ip(val, buffer);
172                         break;
173                 case OPTION_IP_PAIR:
174                         retval = read_ip(val, buffer);
175                         if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
176                         if (retval) retval = read_ip(val, buffer + 4);
177                         break;
178                 case OPTION_STRING:
179                         length = strlen(val);
180                         if (length > 0) {
181                                 if (length > 254) length = 254;
182                                 opt = val;
183                                 retval = 1;
184                         }
185                         break;
186                 case OPTION_BOOLEAN:
187                         retval = read_yn(val, buffer);
188                         break;
189                 case OPTION_U8:
190                         buffer[0] = strtoul(val, &endptr, 0);
191                         retval = (endptr[0] == '\0');
192                         break;
193                 case OPTION_U16:
194                         *result_u16 = htons(strtoul(val, &endptr, 0));
195                         retval = (endptr[0] == '\0');
196                         break;
197                 case OPTION_S16:
198                         *result_u16 = htons(strtol(val, &endptr, 0));
199                         retval = (endptr[0] == '\0');
200                         break;
201                 case OPTION_U32:
202                         *result_u32 = htonl(strtoul(val, &endptr, 0));
203                         retval = (endptr[0] == '\0');
204                         break;
205                 case OPTION_S32:
206                         *result_u32 = htonl(strtol(val, &endptr, 0));
207                         retval = (endptr[0] == '\0');
208                         break;
209                 default:
210                         break;
211                 }
212                 if (retval)
213                         attach_option(opt_list, option, opt, length);
214         } while (retval && option->flags & OPTION_LIST);
215         return retval;
216 }
217
218 static int read_staticlease(const char *const_line, void *arg)
219 {
220
221         char *line;
222         char *mac_string;
223         char *ip_string;
224         uint8_t *mac_bytes;
225         uint32_t *ip;
226
227
228         /* Allocate memory for addresses */
229         mac_bytes = xmalloc(sizeof(unsigned char) * 8);
230         ip = xmalloc(sizeof(uint32_t));
231
232         /* Read mac */
233         line = (char *) const_line;
234         mac_string = strtok(line, " \t");
235         read_mac(mac_string, mac_bytes);
236
237         /* Read ip */
238         ip_string = strtok(NULL, " \t");
239         read_ip(ip_string, ip);
240
241         addStaticLease(arg, mac_bytes, ip);
242
243         if (ENABLE_FEATURE_UDHCP_DEBUG) printStaticLeases(arg);
244
245         return 1;
246
247 }
248
249
250 static const struct config_keyword keywords[] = {
251         /* keyword      handler   variable address              default */
252         {"start",       read_ip,  &(server_config.start),       "192.168.0.20"},
253         {"end",         read_ip,  &(server_config.end),         "192.168.0.254"},
254         {"interface",   read_str, &(server_config.interface),   "eth0"},
255         {"option",      read_opt, &(server_config.options),     ""},
256         {"opt",         read_opt, &(server_config.options),     ""},
257         {"max_leases",  read_u32, &(server_config.max_leases),  "254"},
258         {"remaining",   read_yn,  &(server_config.remaining),   "yes"},
259         {"auto_time",   read_u32, &(server_config.auto_time),   "7200"},
260         {"decline_time",read_u32, &(server_config.decline_time),"3600"},
261         {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
262         {"offer_time",  read_u32, &(server_config.offer_time),  "60"},
263         {"min_lease",   read_u32, &(server_config.min_lease),   "60"},
264         {"lease_file",  read_str, &(server_config.lease_file),  LEASES_FILE},
265         {"pidfile",     read_str, &(server_config.pidfile),     "/var/run/udhcpd.pid"},
266         {"notify_file", read_str, &(server_config.notify_file), ""},
267         {"siaddr",      read_ip,  &(server_config.siaddr),      "0.0.0.0"},
268         {"sname",       read_str, &(server_config.sname),       ""},
269         {"boot_file",   read_str, &(server_config.boot_file),   ""},
270         {"static_lease",read_staticlease, &(server_config.static_leases),       ""},
271         /*ADDME: static lease */
272         {"",            NULL,     NULL,                         ""}
273 };
274
275
276 int read_config(const char *file)
277 {
278         FILE *in;
279         char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
280         int i, lm = 0;
281
282         for (i = 0; keywords[i].keyword[0]; i++)
283                 if (keywords[i].def[0])
284                         keywords[i].handler(keywords[i].def, keywords[i].var);
285
286         if (!(in = fopen(file, "r"))) {
287                 bb_error_msg("unable to open config file: %s", file);
288                 return 0;
289         }
290
291         while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
292                 char debug_orig[READ_CONFIG_BUF_SIZE];
293
294                 lm++;
295                 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
296                 if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer);
297                 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
298
299                 if (!(token = strtok(buffer, " \t"))) continue;
300                 if (!(line = strtok(NULL, ""))) continue;
301
302                 /* eat leading whitespace */
303                 line = line + strspn(line, " \t=");
304                 /* eat trailing whitespace */
305                 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
306                 line[i] = '\0';
307
308                 for (i = 0; keywords[i].keyword[0]; i++)
309                         if (!strcasecmp(token, keywords[i].keyword))
310                                 if (!keywords[i].handler(line, keywords[i].var)) {
311                                         bb_error_msg("failure parsing line %d of %s", lm, file);
312                                         if (ENABLE_FEATURE_UDHCP_DEBUG)
313                                                 bb_error_msg("unable to parse '%s'", debug_orig);
314                                         /* reset back to the default value */
315                                         keywords[i].handler(keywords[i].def, keywords[i].var);
316                                 }
317         }
318         fclose(in);
319         return 1;
320 }
321
322
323 void write_leases(void)
324 {
325         FILE *fp;
326         unsigned int i;
327         char buf[255];
328         time_t curr = time(0);
329         unsigned long tmp_time;
330
331         if (!(fp = fopen(server_config.lease_file, "w"))) {
332                 bb_error_msg("unable to open %s for writing", server_config.lease_file);
333                 return;
334         }
335
336         for (i = 0; i < server_config.max_leases; i++) {
337                 if (leases[i].yiaddr != 0) {
338
339                         /* screw with the time in the struct, for easier writing */
340                         tmp_time = leases[i].expires;
341
342                         if (server_config.remaining) {
343                                 if (lease_expired(&(leases[i])))
344                                         leases[i].expires = 0;
345                                 else leases[i].expires -= curr;
346                         } /* else stick with the time we got */
347                         leases[i].expires = htonl(leases[i].expires);
348                         fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp);
349
350                         /* Then restore it when done. */
351                         leases[i].expires = tmp_time;
352                 }
353         }
354         fclose(fp);
355
356         if (server_config.notify_file) {
357                 sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file);
358                 system(buf);
359         }
360 }
361
362
363 void read_leases(const char *file)
364 {
365         FILE *fp;
366         unsigned int i = 0;
367         struct dhcpOfferedAddr lease;
368
369         if (!(fp = fopen(file, "r"))) {
370                 bb_error_msg("unable to open %s for reading", file);
371                 return;
372         }
373
374         while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) {
375                 /* ADDME: is it a static lease */
376                 if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
377                         lease.expires = ntohl(lease.expires);
378                         if (!server_config.remaining) lease.expires -= time(0);
379                         if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
380                                 bb_error_msg("too many leases while loading %s", file);
381                                 break;
382                         }
383                         i++;
384                 }
385         }
386         DEBUG("Read %d leases", i);
387         fclose(fp);
388 }