7fc7348890c2c99d5c8495eebd27e892dee25322
[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 <netinet/ether.h>
8
9 #include "common.h"
10 #include "dhcpd.h"
11 #include "options.h"
12
13
14 /* on these functions, make sure your datatype matches */
15 static int read_ip(const char *line, void *arg)
16 {
17         len_and_sockaddr *lsa;
18
19         lsa = host_and_af2sockaddr(line, 0, AF_INET);
20         if (lsa) {
21                 *(uint32_t*)arg = lsa->sin.sin_addr.s_addr;
22                 free(lsa);
23                 return 1;
24         }
25         return 0;
26 }
27
28 static int read_mac(const char *line, void *arg)
29 {
30         uint8_t *mac_bytes = arg;
31         struct ether_addr *temp_ether_addr;
32
33         temp_ether_addr = ether_aton(line);
34         if (temp_ether_addr == NULL)
35                 return 0;
36         memcpy(mac_bytes, temp_ether_addr, 6);
37         return 1;
38 }
39
40
41 static int read_str(const char *line, void *arg)
42 {
43         char **dest = arg;
44
45         free(*dest);
46         *dest = xstrdup(line);
47         return 1;
48 }
49
50
51 static int read_u32(const char *line, void *arg)
52 {
53         *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
54         return errno == 0;
55 }
56
57
58 static int read_yn(const char *line, void *arg)
59 {
60         char *dest = arg;
61
62         if (!strcasecmp("yes", line)) {
63                 *dest = 1;
64                 return 1;
65         }
66         if (!strcasecmp("no", line)) {
67                 *dest = 0;
68                 return 1;
69         }
70         return 0;
71 }
72
73
74 /* find option 'code' in opt_list */
75 struct option_set *find_option(struct option_set *opt_list, char code)
76 {
77         while (opt_list && opt_list->data[OPT_CODE] < code)
78                 opt_list = opt_list->next;
79
80         if (opt_list && opt_list->data[OPT_CODE] == code)
81                 return opt_list;
82         return NULL;
83 }
84
85
86 /* add an option to the opt_list */
87 static void attach_option(struct option_set **opt_list,
88                 const struct dhcp_option *option, char *buffer, int length)
89 {
90         struct option_set *existing, *new, **curr;
91
92         existing = find_option(*opt_list, option->code);
93         if (!existing) {
94                 DEBUG("Attaching option %s to list", option->name);
95
96 #if ENABLE_FEATURE_RFC3397
97                 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
98                         /* reuse buffer and length for RFC1035-formatted string */
99                         buffer = dname_enc(NULL, 0, buffer, &length);
100 #endif
101
102                 /* make a new option */
103                 new = xmalloc(sizeof(*new));
104                 new->data = xmalloc(length + 2);
105                 new->data[OPT_CODE] = option->code;
106                 new->data[OPT_LEN] = length;
107                 memcpy(new->data + 2, buffer, length);
108
109                 curr = opt_list;
110                 while (*curr && (*curr)->data[OPT_CODE] < option->code)
111                         curr = &(*curr)->next;
112
113                 new->next = *curr;
114                 *curr = new;
115 #if ENABLE_FEATURE_RFC3397
116                 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
117                         free(buffer);
118 #endif
119                 return;
120         }
121
122         /* add it to an existing option */
123         DEBUG("Attaching option %s to existing member of list", option->name);
124         if (option->flags & OPTION_LIST) {
125 #if ENABLE_FEATURE_RFC3397
126                 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
127                         /* reuse buffer and length for RFC1035-formatted string */
128                         buffer = dname_enc(existing->data + 2,
129                                         existing->data[OPT_LEN], buffer, &length);
130 #endif
131                 if (existing->data[OPT_LEN] + length <= 255) {
132                         existing->data = xrealloc(existing->data,
133                                         existing->data[OPT_LEN] + length + 3);
134                         if ((option->flags & TYPE_MASK) == OPTION_STRING) {
135                                 /* ' ' can bring us to 256 - bad */
136                                 if (existing->data[OPT_LEN] + length >= 255)
137                                         return;
138                                 /* add space separator between STRING options in a list */
139                                 existing->data[existing->data[OPT_LEN] + 2] = ' ';
140                                 existing->data[OPT_LEN]++;
141                         }
142                         memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
143                         existing->data[OPT_LEN] += length;
144                 } /* else, ignore the data, we could put this in a second option in the future */
145 #if ENABLE_FEATURE_RFC3397
146                 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
147                         free(buffer);
148 #endif
149         } /* else, ignore the new data */
150 }
151
152
153 /* read a dhcp option and add it to opt_list */
154 static int read_opt(const char *const_line, void *arg)
155 {
156         struct option_set **opt_list = arg;
157         char *opt, *val, *endptr;
158         const struct dhcp_option *option;
159         int retval = 0, length;
160         char buffer[8];
161         char *line;
162         uint16_t *result_u16 = (uint16_t *) buffer;
163         uint32_t *result_u32 = (uint32_t *) buffer;
164
165         /* Cheat, the only const line we'll actually get is "" */
166         line = (char *) const_line;
167         opt = strtok(line, " \t=");
168         if (!opt) return 0;
169
170         option = dhcp_options;
171         while (1) {
172                 if (!option->code)
173                         return 0;
174                 if (!strcasecmp(option->name, opt))
175                         break;
176                 option++;
177         }
178
179         do {
180                 val = strtok(NULL, ", \t");
181                 if (!val) break;
182                 length = option_lengths[option->flags & TYPE_MASK];
183                 retval = 0;
184                 opt = buffer; /* new meaning for variable opt */
185                 switch (option->flags & TYPE_MASK) {
186                 case OPTION_IP:
187                         retval = read_ip(val, buffer);
188                         break;
189                 case OPTION_IP_PAIR:
190                         retval = read_ip(val, buffer);
191                         val = strtok(NULL, ", \t/-");
192                         if (!val)
193                                 retval = 0;
194                         if (retval)
195                                 retval = read_ip(val, buffer + 4);
196                         break;
197                 case OPTION_STRING:
198 #if ENABLE_FEATURE_RFC3397
199                 case OPTION_STR1035:
200 #endif
201                         length = strlen(val);
202                         if (length > 0) {
203                                 if (length > 254) length = 254;
204                                 opt = val;
205                                 retval = 1;
206                         }
207                         break;
208                 case OPTION_BOOLEAN:
209                         retval = read_yn(val, buffer);
210                         break;
211                 case OPTION_U8:
212                         buffer[0] = strtoul(val, &endptr, 0);
213                         retval = (endptr[0] == '\0');
214                         break;
215                 /* htonX are macros in older libc's, using temp var
216                  * in code below for safety */
217                 /* TODO: use bb_strtoX? */
218                 case OPTION_U16: {
219                         unsigned long tmp = strtoul(val, &endptr, 0);
220                         *result_u16 = htons(tmp);
221                         retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
222                         break;
223                 }
224                 case OPTION_S16: {
225                         long tmp = strtol(val, &endptr, 0);
226                         *result_u16 = htons(tmp);
227                         retval = (endptr[0] == '\0');
228                         break;
229                 }
230                 case OPTION_U32: {
231                         unsigned long tmp = strtoul(val, &endptr, 0);
232                         *result_u32 = htonl(tmp);
233                         retval = (endptr[0] == '\0');
234                         break;
235                 }
236                 case OPTION_S32: {
237                         long tmp = strtol(val, &endptr, 0);
238                         *result_u32 = htonl(tmp);
239                         retval = (endptr[0] == '\0');
240                         break;
241                 }
242                 default:
243                         break;
244                 }
245                 if (retval)
246                         attach_option(opt_list, option, opt, length);
247         } while (retval && option->flags & OPTION_LIST);
248         return retval;
249 }
250
251 static int read_staticlease(const char *const_line, void *arg)
252 {
253         char *line;
254         char *mac_string;
255         char *ip_string;
256         uint8_t *mac_bytes;
257         uint32_t *ip;
258
259         /* Allocate memory for addresses */
260         mac_bytes = xmalloc(sizeof(unsigned char) * 8);
261         ip = xmalloc(sizeof(uint32_t));
262
263         /* Read mac */
264         line = (char *) const_line;
265         mac_string = strtok(line, " \t");
266         read_mac(mac_string, mac_bytes);
267
268         /* Read ip */
269         ip_string = strtok(NULL, " \t");
270         read_ip(ip_string, ip);
271
272         addStaticLease(arg, mac_bytes, ip);
273
274         if (ENABLE_FEATURE_UDHCP_DEBUG) printStaticLeases(arg);
275
276         return 1;
277 }
278
279
280 struct config_keyword {
281         const char *keyword;
282         int (*handler)(const char *line, void *var);
283         void *var;
284         const char *def;
285 };
286
287 static const struct config_keyword keywords[] = {
288         /* keyword       handler   variable address               default */
289         {"start",        read_ip,  &(server_config.start_ip),     "192.168.0.20"},
290         {"end",          read_ip,  &(server_config.end_ip),       "192.168.0.254"},
291         {"interface",    read_str, &(server_config.interface),    "eth0"},
292         {"option",       read_opt, &(server_config.options),      ""},
293         {"opt",          read_opt, &(server_config.options),      ""},
294         /* Avoid "max_leases value not sane" warning by setting default
295          * to default_end_ip - default_start_ip + 1: */
296         {"max_leases",   read_u32, &(server_config.max_leases),   "235"},
297         {"remaining",    read_yn,  &(server_config.remaining),    "yes"},
298         {"auto_time",    read_u32, &(server_config.auto_time),    "7200"},
299         {"decline_time", read_u32, &(server_config.decline_time), "3600"},
300         {"conflict_time",read_u32, &(server_config.conflict_time),"3600"},
301         {"offer_time",   read_u32, &(server_config.offer_time),   "60"},
302         {"min_lease",    read_u32, &(server_config.min_lease),    "60"},
303         {"lease_file",   read_str, &(server_config.lease_file),   LEASES_FILE},
304         {"pidfile",      read_str, &(server_config.pidfile),      "/var/run/udhcpd.pid"},
305         {"notify_file",  read_str, &(server_config.notify_file),  ""},
306         {"siaddr",       read_ip,  &(server_config.siaddr),       "0.0.0.0"},
307         {"sname",        read_str, &(server_config.sname),        ""},
308         {"boot_file",    read_str, &(server_config.boot_file),    ""},
309         {"static_lease", read_staticlease, &(server_config.static_leases), ""},
310         /* ADDME: static lease */
311 };
312
313
314 /*
315  * Domain names may have 254 chars, and string options can be 254
316  * chars long. However, 80 bytes will be enough for most, and won't
317  * hog up memory. If you have a special application, change it
318  */
319 #define READ_CONFIG_BUF_SIZE 80
320
321 int read_config(const char *file)
322 {
323         FILE *in;
324         char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
325         int i, lm = 0;
326
327         for (i = 0; i < ARRAY_SIZE(keywords); i++)
328                 if (keywords[i].def[0])
329                         keywords[i].handler(keywords[i].def, keywords[i].var);
330
331         in = fopen_or_warn(file, "r");
332         if (!in) {
333                 return 0;
334         }
335
336         while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
337                 char debug_orig[READ_CONFIG_BUF_SIZE];
338                 char *p;
339
340                 lm++;
341                 p = strchr(buffer, '\n');
342                 if (p) *p = '\0';
343                 if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer);
344                 p = strchr(buffer, '#');
345                 if (p) *p = '\0';
346
347                 if (!(token = strtok(buffer, " \t"))) continue;
348                 if (!(line = strtok(NULL, ""))) continue;
349
350                 /* eat leading whitespace */
351                 line = skip_whitespace(line);
352                 /* eat trailing whitespace */
353                 i = strlen(line) - 1;
354                 while (i >= 0 && isspace(line[i]))
355                         line[i--] = '\0';
356
357                 for (i = 0; i < ARRAY_SIZE(keywords); i++)
358                         if (!strcasecmp(token, keywords[i].keyword))
359                                 if (!keywords[i].handler(line, keywords[i].var)) {
360                                         bb_error_msg("cannot parse line %d of %s", lm, file);
361                                         if (ENABLE_FEATURE_UDHCP_DEBUG)
362                                                 bb_error_msg("cannot parse '%s'", debug_orig);
363                                         /* reset back to the default value */
364                                         keywords[i].handler(keywords[i].def, keywords[i].var);
365                                 }
366         }
367         fclose(in);
368
369         server_config.start_ip = ntohl(server_config.start_ip);
370         server_config.end_ip = ntohl(server_config.end_ip);
371
372         return 1;
373 }
374
375
376 void write_leases(void)
377 {
378         int fp;
379         unsigned i;
380         time_t curr = time(0);
381         unsigned long tmp_time;
382
383         fp = open3_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
384         if (fp < 0) {
385                 return;
386         }
387
388         for (i = 0; i < server_config.max_leases; i++) {
389                 if (leases[i].yiaddr != 0) {
390
391                         /* screw with the time in the struct, for easier writing */
392                         tmp_time = leases[i].expires;
393
394                         if (server_config.remaining) {
395                                 if (lease_expired(&(leases[i])))
396                                         leases[i].expires = 0;
397                                 else leases[i].expires -= curr;
398                         } /* else stick with the time we got */
399                         leases[i].expires = htonl(leases[i].expires);
400                         // FIXME: error check??
401                         full_write(fp, &leases[i], sizeof(leases[i]));
402
403                         /* then restore it when done */
404                         leases[i].expires = tmp_time;
405                 }
406         }
407         close(fp);
408
409         if (server_config.notify_file) {
410                 char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
411                 system(cmd);
412                 free(cmd);
413         }
414 }
415
416
417 void read_leases(const char *file)
418 {
419         int fp;
420         unsigned int i = 0;
421         struct dhcpOfferedAddr lease;
422
423         fp = open_or_warn(file, O_RDONLY);
424         if (fp < 0) {
425                 return;
426         }
427
428         while (i < server_config.max_leases
429          && full_read(fp, &lease, sizeof(lease)) == sizeof(lease)
430         ) {
431                 /* ADDME: is it a static lease */
432                 uint32_t y = ntohl(lease.yiaddr);
433                 if (y >= server_config.start_ip && y <= server_config.end_ip) {
434                         lease.expires = ntohl(lease.expires);
435                         if (!server_config.remaining)
436                                 lease.expires -= time(0);
437                         if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
438                                 bb_error_msg("too many leases while loading %s", file);
439                                 break;
440                         }
441                         i++;
442                 }
443         }
444         DEBUG("Read %d leases", i);
445         close(fp);
446 }