blob: 7fc7348890c2c99d5c8495eebd27e892dee25322 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/*
3 * files.c -- DHCP server file manipulation *
4 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
5 */
6
Mike Frysinger7031f622006-05-08 03:20:50 +00007#include <netinet/ether.h>
Mike Frysinger7031f622006-05-08 03:20:50 +00008
Denis Vlasenko5a3395b2006-11-18 19:51:32 +00009#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000010#include "dhcpd.h"
11#include "options.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000012
Mike Frysinger7031f622006-05-08 03:20:50 +000013
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000014/* on these functions, make sure your datatype matches */
Mike Frysinger7031f622006-05-08 03:20:50 +000015static int read_ip(const char *line, void *arg)
16{
Denis Vlasenkoe9913032007-02-04 02:41:57 +000017 len_and_sockaddr *lsa;
Mike Frysinger7031f622006-05-08 03:20:50 +000018
Denis Vlasenkoe9913032007-02-04 02:41:57 +000019 lsa = host_and_af2sockaddr(line, 0, AF_INET);
20 if (lsa) {
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000021 *(uint32_t*)arg = lsa->sin.sin_addr.s_addr;
Denis Vlasenkoe9913032007-02-04 02:41:57 +000022 free(lsa);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000023 return 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000024 }
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000025 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000026}
27
28static int read_mac(const char *line, void *arg)
29{
30 uint8_t *mac_bytes = arg;
31 struct ether_addr *temp_ether_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +000032
33 temp_ether_addr = ether_aton(line);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000034 if (temp_ether_addr == NULL)
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000035 return 0;
36 memcpy(mac_bytes, temp_ether_addr, 6);
37 return 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000038}
39
40
41static int read_str(const char *line, void *arg)
42{
43 char **dest = arg;
44
45 free(*dest);
Denis Vlasenko4ebaf102007-01-19 21:33:19 +000046 *dest = xstrdup(line);
Mike Frysinger7031f622006-05-08 03:20:50 +000047 return 1;
48}
49
50
51static int read_u32(const char *line, void *arg)
52{
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000053 *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
Denis Vlasenkod686a042006-11-27 14:43:21 +000054 return errno == 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000055}
56
57
58static int read_yn(const char *line, void *arg)
59{
60 char *dest = arg;
Mike Frysinger7031f622006-05-08 03:20:50 +000061
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000062 if (!strcasecmp("yes", line)) {
Mike Frysinger7031f622006-05-08 03:20:50 +000063 *dest = 1;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000064 return 1;
65 }
66 if (!strcasecmp("no", line)) {
Mike Frysinger7031f622006-05-08 03:20:50 +000067 *dest = 0;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000068 return 1;
69 }
70 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000071}
72
73
74/* find option 'code' in opt_list */
75struct 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
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000080 if (opt_list && opt_list->data[OPT_CODE] == code)
81 return opt_list;
82 return NULL;
Mike Frysinger7031f622006-05-08 03:20:50 +000083}
84
85
86/* add an option to the opt_list */
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000087static void attach_option(struct option_set **opt_list,
88 const struct dhcp_option *option, char *buffer, int length)
Mike Frysinger7031f622006-05-08 03:20:50 +000089{
90 struct option_set *existing, *new, **curr;
91
Denis Vlasenkod686a042006-11-27 14:43:21 +000092 existing = find_option(*opt_list, option->code);
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +000093 if (!existing) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000094 DEBUG("Attaching option %s to list", option->name);
Mike Frysinger7031f622006-05-08 03:20:50 +000095
Denis Vlasenko50664732007-02-27 21:15:08 +000096#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
Mike Frysinger7031f622006-05-08 03:20:50 +0000102 /* make a new option */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000103 new = xmalloc(sizeof(*new));
Mike Frysinger7031f622006-05-08 03:20:50 +0000104 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;
Denis Vlasenko50664732007-02-27 21:15:08 +0000115#if ENABLE_FEATURE_RFC3397
116 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
117 free(buffer);
118#endif
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000119 return;
Mike Frysinger7031f622006-05-08 03:20:50 +0000120 }
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000121
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) {
Denis Vlasenko50664732007-02-27 21:15:08 +0000125#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
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000131 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) {
Denis Vlasenkoe129f432007-02-02 01:57:24 +0000135 /* ' ' can bring us to 256 - bad */
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000136 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 */
Denis Vlasenko50664732007-02-27 21:15:08 +0000145#if ENABLE_FEATURE_RFC3397
146 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
147 free(buffer);
148#endif
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000149 } /* else, ignore the new data */
Mike Frysinger7031f622006-05-08 03:20:50 +0000150}
151
152
153/* read a dhcp option and add it to opt_list */
154static int read_opt(const char *const_line, void *arg)
155{
156 struct option_set **opt_list = arg;
157 char *opt, *val, *endptr;
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000158 const struct dhcp_option *option;
Mike Frysinger7031f622006-05-08 03:20:50 +0000159 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;
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000167 opt = strtok(line, " \t=");
168 if (!opt) return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000169
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000170 option = dhcp_options;
171 while (1) {
172 if (!option->code)
173 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000174 if (!strcasecmp(option->name, opt))
175 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000176 option++;
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000177 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000178
179 do {
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000180 val = strtok(NULL, ", \t");
181 if (!val) break;
Mike Frysinger7031f622006-05-08 03:20:50 +0000182 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);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000191 val = strtok(NULL, ", \t/-");
192 if (!val)
193 retval = 0;
194 if (retval)
195 retval = read_ip(val, buffer + 4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000196 break;
197 case OPTION_STRING:
Denis Vlasenko50664732007-02-27 21:15:08 +0000198#if ENABLE_FEATURE_RFC3397
199 case OPTION_STR1035:
200#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000201 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;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000215 /* 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);
Mike Frysinger7031f622006-05-08 03:20:50 +0000227 retval = (endptr[0] == '\0');
228 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000229 }
230 case OPTION_U32: {
231 unsigned long tmp = strtoul(val, &endptr, 0);
232 *result_u32 = htonl(tmp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000233 retval = (endptr[0] == '\0');
234 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000235 }
236 case OPTION_S32: {
237 long tmp = strtol(val, &endptr, 0);
238 *result_u32 = htonl(tmp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000239 retval = (endptr[0] == '\0');
240 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000241 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000242 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
251static int read_staticlease(const char *const_line, void *arg)
252{
Mike Frysinger7031f622006-05-08 03:20:50 +0000253 char *line;
254 char *mac_string;
255 char *ip_string;
256 uint8_t *mac_bytes;
257 uint32_t *ip;
258
Mike Frysinger7031f622006-05-08 03:20:50 +0000259 /* 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
Rob Landley3f785612006-05-28 01:06:36 +0000274 if (ENABLE_FEATURE_UDHCP_DEBUG) printStaticLeases(arg);
Mike Frysinger7031f622006-05-08 03:20:50 +0000275
276 return 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000277}
278
279
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000280struct config_keyword {
281 const char *keyword;
282 int (*handler)(const char *line, void *var);
283 void *var;
284 const char *def;
Mike Frysinger7031f622006-05-08 03:20:50 +0000285};
286
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000287static 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
Mike Frysinger7031f622006-05-08 03:20:50 +0000320
321int read_config(const char *file)
322{
323 FILE *in;
324 char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
Mike Frysinger7031f622006-05-08 03:20:50 +0000325 int i, lm = 0;
326
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000327 for (i = 0; i < ARRAY_SIZE(keywords); i++)
Mike Frysinger7031f622006-05-08 03:20:50 +0000328 if (keywords[i].def[0])
329 keywords[i].handler(keywords[i].def, keywords[i].var);
330
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000331 in = fopen_or_warn(file, "r");
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000332 if (!in) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000333 return 0;
334 }
335
336 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
Rob Landley3f785612006-05-28 01:06:36 +0000337 char debug_orig[READ_CONFIG_BUF_SIZE];
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000338 char *p;
Rob Landley3f785612006-05-28 01:06:36 +0000339
Mike Frysinger7031f622006-05-08 03:20:50 +0000340 lm++;
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000341 p = strchr(buffer, '\n');
342 if (p) *p = '\0';
Rob Landley3f785612006-05-28 01:06:36 +0000343 if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000344 p = strchr(buffer, '#');
345 if (p) *p = '\0';
Mike Frysinger7031f622006-05-08 03:20:50 +0000346
347 if (!(token = strtok(buffer, " \t"))) continue;
348 if (!(line = strtok(NULL, ""))) continue;
349
350 /* eat leading whitespace */
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000351 line = skip_whitespace(line);
Mike Frysinger7031f622006-05-08 03:20:50 +0000352 /* eat trailing whitespace */
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000353 i = strlen(line) - 1;
354 while (i >= 0 && isspace(line[i]))
355 line[i--] = '\0';
Mike Frysinger7031f622006-05-08 03:20:50 +0000356
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000357 for (i = 0; i < ARRAY_SIZE(keywords); i++)
Mike Frysinger7031f622006-05-08 03:20:50 +0000358 if (!strcasecmp(token, keywords[i].keyword))
359 if (!keywords[i].handler(line, keywords[i].var)) {
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000360 bb_error_msg("cannot parse line %d of %s", lm, file);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000361 if (ENABLE_FEATURE_UDHCP_DEBUG)
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000362 bb_error_msg("cannot parse '%s'", debug_orig);
Mike Frysinger7031f622006-05-08 03:20:50 +0000363 /* reset back to the default value */
364 keywords[i].handler(keywords[i].def, keywords[i].var);
365 }
366 }
367 fclose(in);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000368
369 server_config.start_ip = ntohl(server_config.start_ip);
370 server_config.end_ip = ntohl(server_config.end_ip);
371
Mike Frysinger7031f622006-05-08 03:20:50 +0000372 return 1;
373}
374
375
376void write_leases(void)
377{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000378 int fp;
379 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +0000380 time_t curr = time(0);
381 unsigned long tmp_time;
382
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000383 fp = open3_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000384 if (fp < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000385 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);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000400 // FIXME: error check??
401 full_write(fp, &leases[i], sizeof(leases[i]));
Mike Frysinger7031f622006-05-08 03:20:50 +0000402
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000403 /* then restore it when done */
Mike Frysinger7031f622006-05-08 03:20:50 +0000404 leases[i].expires = tmp_time;
405 }
406 }
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000407 close(fp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000408
409 if (server_config.notify_file) {
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000410 char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
411 system(cmd);
412 free(cmd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000413 }
414}
415
416
417void read_leases(const char *file)
418{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000419 int fp;
Mike Frysinger7031f622006-05-08 03:20:50 +0000420 unsigned int i = 0;
421 struct dhcpOfferedAddr lease;
422
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000423 fp = open_or_warn(file, O_RDONLY);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000424 if (fp < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000425 return;
426 }
427
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000428 while (i < server_config.max_leases
429 && full_read(fp, &lease, sizeof(lease)) == sizeof(lease)
430 ) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000431 /* ADDME: is it a static lease */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000432 uint32_t y = ntohl(lease.yiaddr);
433 if (y >= server_config.start_ip && y <= server_config.end_ip) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000434 lease.expires = ntohl(lease.expires);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000435 if (!server_config.remaining)
436 lease.expires -= time(0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000437 if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000438 bb_error_msg("too many leases while loading %s", file);
Mike Frysinger7031f622006-05-08 03:20:50 +0000439 break;
440 }
441 i++;
442 }
443 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000444 DEBUG("Read %d leases", i);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000445 close(fp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000446}