1 /* vi: set sw=4 ts=4: */
3 * files.c -- DHCP server file manipulation *
4 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
6 * Licensed under GPLv2, see file LICENSE in this tarball for details.
9 #include <netinet/ether.h>
16 static inline uint64_t hton64(uint64_t v)
18 return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32);
23 #define ntoh64(v) hton64(v)
26 /* on these functions, make sure your datatype matches */
27 static int read_ip(const char *line, void *arg)
29 len_and_sockaddr *lsa;
31 lsa = host_and_af2sockaddr(line, 0, AF_INET);
34 *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
39 static int read_mac(const char *line, void *arg)
41 return NULL == ether_aton_r(line, (struct ether_addr *)arg);
45 static int read_str(const char *line, void *arg)
50 *dest = xstrdup(line);
55 static int read_u32(const char *line, void *arg)
57 *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
62 static int read_yn(const char *line, void *arg)
66 if (!strcasecmp("yes", line)) {
70 if (!strcasecmp("no", line)) {
78 /* find option 'code' in opt_list */
79 struct option_set* FAST_FUNC find_option(struct option_set *opt_list, uint8_t code)
81 while (opt_list && opt_list->data[OPT_CODE] < code)
82 opt_list = opt_list->next;
84 if (opt_list && opt_list->data[OPT_CODE] == code)
90 /* add an option to the opt_list */
91 static void attach_option(struct option_set **opt_list,
92 const struct dhcp_option *option, char *buffer, int length)
94 struct option_set *existing, *new, **curr;
96 existing = find_option(*opt_list, option->code);
98 DEBUG("Attaching option %02x to list", option->code);
100 #if ENABLE_FEATURE_UDHCP_RFC3397
101 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
102 /* reuse buffer and length for RFC1035-formatted string */
103 buffer = (char *)dname_enc(NULL, 0, buffer, &length);
106 /* make a new option */
107 new = xmalloc(sizeof(*new));
108 new->data = xmalloc(length + 2);
109 new->data[OPT_CODE] = option->code;
110 new->data[OPT_LEN] = length;
111 memcpy(new->data + 2, buffer, length);
114 while (*curr && (*curr)->data[OPT_CODE] < option->code)
115 curr = &(*curr)->next;
119 #if ENABLE_FEATURE_UDHCP_RFC3397
120 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
126 /* add it to an existing option */
127 DEBUG("Attaching option %02x to existing member of list", option->code);
128 if (option->flags & OPTION_LIST) {
129 #if ENABLE_FEATURE_UDHCP_RFC3397
130 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
131 /* reuse buffer and length for RFC1035-formatted string */
132 buffer = (char *)dname_enc(existing->data + 2,
133 existing->data[OPT_LEN], buffer, &length);
135 if (existing->data[OPT_LEN] + length <= 255) {
136 existing->data = xrealloc(existing->data,
137 existing->data[OPT_LEN] + length + 3);
138 if ((option->flags & TYPE_MASK) == OPTION_STRING) {
139 /* ' ' can bring us to 256 - bad */
140 if (existing->data[OPT_LEN] + length >= 255)
142 /* add space separator between STRING options in a list */
143 existing->data[existing->data[OPT_LEN] + 2] = ' ';
144 existing->data[OPT_LEN]++;
146 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
147 existing->data[OPT_LEN] += length;
148 } /* else, ignore the data, we could put this in a second option in the future */
149 #if ENABLE_FEATURE_UDHCP_RFC3397
150 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
153 } /* else, ignore the new data */
157 /* read a dhcp option and add it to opt_list */
158 static int read_opt(const char *const_line, void *arg)
160 struct option_set **opt_list = arg;
161 char *opt, *val, *endptr;
163 const struct dhcp_option *option;
164 int retval, length, idx;
165 char buffer[8] ALIGNED(4);
166 uint16_t *result_u16 = (uint16_t *) buffer;
167 uint32_t *result_u32 = (uint32_t *) buffer;
169 /* Cheat, the only const line we'll actually get is "" */
170 line = (char *) const_line;
171 opt = strtok(line, " \t=");
175 idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */
178 option = &dhcp_options[idx];
182 val = strtok(NULL, ", \t");
184 length = dhcp_option_lengths[option->flags & TYPE_MASK];
186 opt = buffer; /* new meaning for variable opt */
187 switch (option->flags & TYPE_MASK) {
189 retval = read_ip(val, buffer);
192 retval = read_ip(val, buffer);
193 val = strtok(NULL, ", \t/-");
197 retval = read_ip(val, buffer + 4);
200 #if ENABLE_FEATURE_UDHCP_RFC3397
203 length = strlen(val);
205 if (length > 254) length = 254;
211 retval = read_yn(val, buffer);
214 buffer[0] = strtoul(val, &endptr, 0);
215 retval = (endptr[0] == '\0');
217 /* htonX are macros in older libc's, using temp var
218 * in code below for safety */
219 /* TODO: use bb_strtoX? */
221 unsigned long tmp = strtoul(val, &endptr, 0);
222 *result_u16 = htons(tmp);
223 retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
227 long tmp = strtol(val, &endptr, 0);
228 *result_u16 = htons(tmp);
229 retval = (endptr[0] == '\0');
233 unsigned long tmp = strtoul(val, &endptr, 0);
234 *result_u32 = htonl(tmp);
235 retval = (endptr[0] == '\0');
239 long tmp = strtol(val, &endptr, 0);
240 *result_u32 = htonl(tmp);
241 retval = (endptr[0] == '\0');
248 attach_option(opt_list, option, opt, length);
249 } while (retval && option->flags & OPTION_LIST);
253 static int read_staticlease(const char *const_line, void *arg)
258 struct ether_addr mac_bytes;
262 line = (char *) const_line;
263 mac_string = strtok_r(line, " \t", &line);
264 read_mac(mac_string, &mac_bytes);
267 ip_string = strtok_r(NULL, " \t", &line);
268 read_ip(ip_string, &ip);
270 addStaticLease(arg, (uint8_t*) &mac_bytes, ip);
272 if (ENABLE_UDHCP_DEBUG) printStaticLeases(arg);
278 struct config_keyword {
280 int (*handler)(const char *line, void *var);
285 static const struct config_keyword keywords[] = {
286 /* keyword handler variable address default */
287 {"start", read_ip, &(server_config.start_ip), "192.168.0.20"},
288 {"end", read_ip, &(server_config.end_ip), "192.168.0.254"},
289 {"interface", read_str, &(server_config.interface), "eth0"},
290 /* Avoid "max_leases value not sane" warning by setting default
291 * to default_end_ip - default_start_ip + 1: */
292 {"max_leases", read_u32, &(server_config.max_leases), "235"},
293 // {"remaining", read_yn, &(server_config.remaining), "yes"},
294 {"auto_time", read_u32, &(server_config.auto_time), "7200"},
295 {"decline_time", read_u32, &(server_config.decline_time), "3600"},
296 {"conflict_time",read_u32, &(server_config.conflict_time),"3600"},
297 {"offer_time", read_u32, &(server_config.offer_time), "60"},
298 {"min_lease", read_u32, &(server_config.min_lease), "60"},
299 {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
300 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
301 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
302 /* keywords with no defaults must be last! */
303 {"option", read_opt, &(server_config.options), ""},
304 {"opt", read_opt, &(server_config.options), ""},
305 {"notify_file", read_str, &(server_config.notify_file), ""},
306 {"sname", read_str, &(server_config.sname), ""},
307 {"boot_file", read_str, &(server_config.boot_file), ""},
308 {"static_lease", read_staticlease, &(server_config.static_leases), ""},
310 enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
312 void FAST_FUNC read_config(const char *file)
315 const struct config_keyword *k;
319 for (i = 0; i < KWS_WITH_DEFAULTS; i++)
320 keywords[i].handler(keywords[i].def, keywords[i].var);
322 parser = config_open(file);
323 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
324 for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
325 if (!strcasecmp(token[0], k->keyword)) {
326 if (!k->handler(token[1], k->var)) {
327 bb_error_msg("can't parse line %u in %s",
328 parser->lineno, file);
329 /* reset back to the default value */
330 k->handler(k->def, k->var);
336 config_close(parser);
338 server_config.start_ip = ntohl(server_config.start_ip);
339 server_config.end_ip = ntohl(server_config.end_ip);
343 void FAST_FUNC write_leases(void)
350 fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
354 curr = written_at = time(NULL);
356 written_at = hton64(written_at);
357 full_write(fd, &written_at, sizeof(written_at));
359 for (i = 0; i < server_config.max_leases; i++) {
360 leasetime_t tmp_time;
362 if (leases[i].yiaddr == 0)
365 /* screw with the time in the struct, for easier writing */
366 tmp_time = leases[i].expires;
368 leases[i].expires -= curr;
369 if ((signed_leasetime_t) leases[i].expires < 0)
370 leases[i].expires = 0;
371 leases[i].expires = htonl(leases[i].expires);
373 /* No error check. If the file gets truncated,
374 * we lose some leases on restart. Oh well. */
375 full_write(fd, &leases[i], sizeof(leases[i]));
377 /* then restore it when done */
378 leases[i].expires = tmp_time;
382 if (server_config.notify_file) {
383 // TODO: vfork-based child creation
384 char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
391 void FAST_FUNC read_leases(const char *file)
395 struct dhcpOfferedAddr lease;
396 int64_t written_at, curr;
398 fd = open_or_warn(file, O_RDONLY);
402 if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
404 written_at = ntoh64(written_at);
406 if (curr < written_at)
407 written_at = curr; /* lease file from future! :) */
410 while (i < server_config.max_leases
411 && full_read(fd, &lease, sizeof(lease)) == sizeof(lease)
413 /* ADDME: what if it matches some static lease? */
414 uint32_t y = ntohl(lease.yiaddr);
415 if (y >= server_config.start_ip && y <= server_config.end_ip) {
416 int64_t expires = ntohl(lease.expires) + written_at - curr;
419 /* NB: add_lease takes "relative time", IOW,
420 * lease duration, not lease deadline. */
421 if (!(add_lease(lease.chaddr, lease.yiaddr, expires))) {
422 bb_error_msg("too many leases while loading %s", file);
428 DEBUG("Read %d leases", i);