udhcp: code shrink
[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  * Licensed under GPLv2, see file LICENSE in this tarball for details.
7  */
8
9 #include <netinet/ether.h>
10
11 #include "common.h"
12 #include "dhcpd.h"
13
14 #if BB_LITTLE_ENDIAN
15 static inline uint64_t hton64(uint64_t v)
16 {
17         return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32);
18 }
19 #else
20 #define hton64(v) (v)
21 #endif
22 #define ntoh64(v) hton64(v)
23
24 /* on these functions, make sure your datatype matches */
25 static int FAST_FUNC read_nip(const char *line, void *arg)
26 {
27         len_and_sockaddr *lsa;
28
29         lsa = host_and_af2sockaddr(line, 0, AF_INET);
30         if (!lsa)
31                 return 0;
32         *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
33         free(lsa);
34         return 1;
35 }
36
37 static int FAST_FUNC read_mac(const char *line, void *arg)
38 {
39         return NULL == ether_aton_r(line, (struct ether_addr *)arg);
40 }
41
42 static int FAST_FUNC read_str(const char *line, void *arg)
43 {
44         char **dest = arg;
45
46         free(*dest);
47         *dest = xstrdup(line);
48         return 1;
49 }
50
51 static int FAST_FUNC read_u32(const char *line, void *arg)
52 {
53         *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
54         return errno == 0;
55 }
56
57 static int read_yn(const char *line, void *arg)
58 {
59         char *dest = arg;
60
61         if (strcasecmp("yes", line) == 0) {
62                 *dest = 1;
63                 return 1;
64         }
65         if (strcasecmp("no", line) == 0) {
66                 *dest = 0;
67                 return 1;
68         }
69         return 0;
70 }
71
72 /* find option 'code' in opt_list */
73 struct option_set* FAST_FUNC find_option(struct option_set *opt_list, uint8_t code)
74 {
75         while (opt_list && opt_list->data[OPT_CODE] < code)
76                 opt_list = opt_list->next;
77
78         if (opt_list && opt_list->data[OPT_CODE] == code)
79                 return opt_list;
80         return NULL;
81 }
82
83 /* add an option to the opt_list */
84 static NOINLINE void attach_option(
85                 struct option_set **opt_list,
86                 const struct dhcp_option *option,
87                 char *buffer,
88                 int length)
89 {
90         struct option_set *existing, *new, **curr;
91 #if ENABLE_FEATURE_UDHCP_RFC3397
92         char *allocated = NULL;
93 #endif
94
95         existing = find_option(*opt_list, option->code);
96         if (!existing) {
97                 log2("Attaching option %02x to list", option->code);
98 #if ENABLE_FEATURE_UDHCP_RFC3397
99                 if ((option->flags & OPTION_TYPE_MASK) == OPTION_STR1035) {
100                         /* reuse buffer and length for RFC1035-formatted string */
101                         allocated = buffer = (char *)dname_enc(NULL, 0, buffer, &length);
102                 }
103 #endif
104                 /* make a new option */
105                 new = xmalloc(sizeof(*new));
106                 new->data = xmalloc(length + OPT_DATA);
107                 new->data[OPT_CODE] = option->code;
108                 new->data[OPT_LEN] = length;
109                 memcpy(new->data + OPT_DATA, buffer, length);
110
111                 curr = opt_list;
112                 while (*curr && (*curr)->data[OPT_CODE] < option->code)
113                         curr = &(*curr)->next;
114
115                 new->next = *curr;
116                 *curr = new;
117                 goto ret;
118         }
119
120         if (option->flags & OPTION_LIST) {
121                 unsigned old_len;
122
123                 /* add it to an existing option */
124                 log1("Attaching option %02x to existing member of list", option->code);
125                 old_len = existing->data[OPT_LEN];
126 #if ENABLE_FEATURE_UDHCP_RFC3397
127                 if ((option->flags & OPTION_TYPE_MASK) == OPTION_STR1035) {
128                         /* reuse buffer and length for RFC1035-formatted string */
129                         allocated = buffer = (char *)dname_enc(existing->data + OPT_DATA, old_len, buffer, &length);
130                 }
131 #endif
132                 if (old_len + length < 255) {
133                         /* actually 255 is ok too, but adding a space can overlow it */
134
135                         existing->data = xrealloc(existing->data, OPT_DATA + 1 + old_len + length);
136                         if ((option->flags & OPTION_TYPE_MASK) == OPTION_STRING) {
137                                 /* add space separator between STRING options in a list */
138                                 existing->data[OPT_DATA + old_len] = ' ';
139                                 old_len++;
140                         }
141                         memcpy(existing->data + OPT_DATA + old_len, buffer, length);
142                         existing->data[OPT_LEN] = old_len + length;
143                 } /* else, ignore the data, we could put this in a second option in the future */
144         } /* else, ignore the new data */
145
146  ret: ;
147 #if ENABLE_FEATURE_UDHCP_RFC3397
148         free(allocated);
149 #endif
150 }
151
152 /* read a dhcp option and add it to opt_list */
153 static int FAST_FUNC read_opt(const char *const_line, void *arg)
154 {
155         struct option_set **opt_list = arg;
156         char *opt, *val, *endptr;
157         char *line;
158         const struct dhcp_option *option;
159         int retval, length, idx;
160         char buffer[8] ALIGNED(4);
161         uint16_t *result_u16 = (uint16_t *) buffer;
162         uint32_t *result_u32 = (uint32_t *) buffer;
163
164         /* Cheat, the only *const* line possible is "" */
165         line = (char *) const_line;
166         opt = strtok(line, " \t=");
167         if (!opt)
168                 return 0;
169
170         idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */
171         if (idx < 0)
172                 return 0;
173         option = &dhcp_options[idx];
174
175         retval = 0;
176         do {
177                 val = strtok(NULL, ", \t");
178                 if (!val)
179                         break;
180                 length = dhcp_option_lengths[option->flags & OPTION_TYPE_MASK];
181                 retval = 0;
182                 opt = buffer; /* new meaning for variable opt */
183                 switch (option->flags & OPTION_TYPE_MASK) {
184                 case OPTION_IP:
185                         retval = read_nip(val, buffer);
186                         break;
187                 case OPTION_IP_PAIR:
188                         retval = read_nip(val, buffer);
189                         val = strtok(NULL, ", \t/-");
190                         if (!val)
191                                 retval = 0;
192                         if (retval)
193                                 retval = read_nip(val, buffer + 4);
194                         break;
195                 case OPTION_STRING:
196 #if ENABLE_FEATURE_UDHCP_RFC3397
197                 case OPTION_STR1035:
198 #endif
199                         length = strnlen(val, 254);
200                         if (length > 0) {
201                                 opt = val;
202                                 retval = 1;
203                         }
204                         break;
205                 case OPTION_BOOLEAN:
206                         retval = read_yn(val, buffer);
207                         break;
208                 case OPTION_U8:
209                         buffer[0] = strtoul(val, &endptr, 0);
210                         retval = (endptr[0] == '\0');
211                         break;
212                 /* htonX are macros in older libc's, using temp var
213                  * in code below for safety */
214                 /* TODO: use bb_strtoX? */
215                 case OPTION_U16: {
216                         unsigned long tmp = strtoul(val, &endptr, 0);
217                         *result_u16 = htons(tmp);
218                         retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
219                         break;
220                 }
221                 case OPTION_S16: {
222                         long tmp = strtol(val, &endptr, 0);
223                         *result_u16 = htons(tmp);
224                         retval = (endptr[0] == '\0');
225                         break;
226                 }
227                 case OPTION_U32: {
228                         unsigned long tmp = strtoul(val, &endptr, 0);
229                         *result_u32 = htonl(tmp);
230                         retval = (endptr[0] == '\0');
231                         break;
232                 }
233                 case OPTION_S32: {
234                         long tmp = strtol(val, &endptr, 0);
235                         *result_u32 = htonl(tmp);
236                         retval = (endptr[0] == '\0');
237                         break;
238                 }
239                 default:
240                         break;
241                 }
242                 if (retval)
243                         attach_option(opt_list, option, opt, length);
244         } while (retval && option->flags & OPTION_LIST);
245         return retval;
246 }
247
248 static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
249 {
250         char *line;
251         char *mac_string;
252         char *ip_string;
253         struct ether_addr mac_bytes;
254         uint32_t ip;
255
256         /* Read mac */
257         line = (char *) const_line;
258         mac_string = strtok_r(line, " \t", &line);
259         read_mac(mac_string, &mac_bytes);
260
261         /* Read ip */
262         ip_string = strtok_r(NULL, " \t", &line);
263         read_nip(ip_string, &ip);
264
265         add_static_lease(arg, (uint8_t*) &mac_bytes, ip);
266
267         log_static_leases(arg);
268
269         return 1;
270 }
271
272
273 struct config_keyword {
274         const char *keyword;
275         int (*handler)(const char *line, void *var) FAST_FUNC;
276         void *var;
277         const char *def;
278 };
279
280 static const struct config_keyword keywords[] = {
281         /* keyword       handler   variable address               default */
282         {"start",        read_nip, &(server_config.start_ip),     "192.168.0.20"},
283         {"end",          read_nip, &(server_config.end_ip),       "192.168.0.254"},
284         {"interface",    read_str, &(server_config.interface),    "eth0"},
285         /* Avoid "max_leases value not sane" warning by setting default
286          * to default_end_ip - default_start_ip + 1: */
287         {"max_leases",   read_u32, &(server_config.max_leases),   "235"},
288         {"auto_time",    read_u32, &(server_config.auto_time),    "7200"},
289         {"decline_time", read_u32, &(server_config.decline_time), "3600"},
290         {"conflict_time",read_u32, &(server_config.conflict_time),"3600"},
291         {"offer_time",   read_u32, &(server_config.offer_time),   "60"},
292         {"min_lease",    read_u32, &(server_config.min_lease_sec),"60"},
293         {"lease_file",   read_str, &(server_config.lease_file),   LEASES_FILE},
294         {"pidfile",      read_str, &(server_config.pidfile),      "/var/run/udhcpd.pid"},
295         {"siaddr",       read_nip, &(server_config.siaddr_nip),   "0.0.0.0"},
296         /* keywords with no defaults must be last! */
297         {"option",       read_opt, &(server_config.options),      ""},
298         {"opt",          read_opt, &(server_config.options),      ""},
299         {"notify_file",  read_str, &(server_config.notify_file),  ""},
300         {"sname",        read_str, &(server_config.sname),        ""},
301         {"boot_file",    read_str, &(server_config.boot_file),    ""},
302         {"static_lease", read_staticlease, &(server_config.static_leases), ""},
303 };
304 enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
305
306 void FAST_FUNC read_config(const char *file)
307 {
308         parser_t *parser;
309         const struct config_keyword *k;
310         unsigned i;
311         char *token[2];
312
313         for (i = 0; i < KWS_WITH_DEFAULTS; i++)
314                 keywords[i].handler(keywords[i].def, keywords[i].var);
315
316         parser = config_open(file);
317         while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
318                 for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
319                         if (!strcasecmp(token[0], k->keyword)) {
320                                 if (!k->handler(token[1], k->var)) {
321                                         bb_error_msg("can't parse line %u in %s",
322                                                         parser->lineno, file);
323                                         /* reset back to the default value */
324                                         k->handler(k->def, k->var);
325                                 }
326                                 break;
327                         }
328                 }
329         }
330         config_close(parser);
331
332         server_config.start_ip = ntohl(server_config.start_ip);
333         server_config.end_ip = ntohl(server_config.end_ip);
334 }
335
336 void FAST_FUNC write_leases(void)
337 {
338         int fd;
339         unsigned i;
340         leasetime_t curr;
341         int64_t written_at;
342
343         fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
344         if (fd < 0)
345                 return;
346
347         curr = written_at = time(NULL);
348
349         written_at = hton64(written_at);
350         full_write(fd, &written_at, sizeof(written_at));
351
352         for (i = 0; i < server_config.max_leases; i++) {
353                 leasetime_t tmp_time;
354
355                 if (g_leases[i].lease_nip == 0)
356                         continue;
357
358                 /* Screw with the time in the struct, for easier writing */
359                 tmp_time = g_leases[i].expires;
360
361                 g_leases[i].expires -= curr;
362                 if ((signed_leasetime_t) g_leases[i].expires < 0)
363                         g_leases[i].expires = 0;
364                 g_leases[i].expires = htonl(g_leases[i].expires);
365
366                 /* No error check. If the file gets truncated,
367                  * we lose some leases on restart. Oh well. */
368                 full_write(fd, &g_leases[i], sizeof(g_leases[i]));
369
370                 /* Then restore it when done */
371                 g_leases[i].expires = tmp_time;
372         }
373         close(fd);
374
375         if (server_config.notify_file) {
376 // TODO: vfork-based child creation
377                 char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
378                 system(cmd);
379                 free(cmd);
380         }
381 }
382
383 void FAST_FUNC read_leases(const char *file)
384 {
385         struct dyn_lease lease;
386         int64_t written_at, time_passed;
387         int fd;
388 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
389         unsigned i = 0;
390 #endif
391
392         fd = open_or_warn(file, O_RDONLY);
393         if (fd < 0)
394                 return;
395
396         if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
397                 goto ret;
398         written_at = ntoh64(written_at);
399
400         time_passed = time(NULL) - written_at;
401         /* Strange written_at, or lease file from old version of udhcpd
402          * which had no "written_at" field? */
403         if ((uint64_t)time_passed > 12 * 60 * 60)
404                 goto ret;
405
406         while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
407 //FIXME: what if it matches some static lease?
408                 uint32_t y = ntohl(lease.lease_nip);
409                 if (y >= server_config.start_ip && y <= server_config.end_ip) {
410                         signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed;
411                         if (expires <= 0)
412                                 continue;
413                         /* NB: add_lease takes "relative time", IOW,
414                          * lease duration, not lease deadline. */
415                         if (add_lease(lease.lease_mac, lease.lease_nip,
416                                         expires,
417                                         lease.hostname, sizeof(lease.hostname)
418                                 ) == 0
419                         ) {
420                                 bb_error_msg("too many leases while loading %s", file);
421                                 break;
422                         }
423 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
424                         i++;
425 #endif
426                 }
427         }
428         log1("Read %d leases", i);
429  ret:
430         close(fd);
431 }