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 /* on these functions, make sure your datatype matches */
17 static int read_ip(const char *line, void *arg)
19 len_and_sockaddr *lsa;
21 lsa = host_and_af2sockaddr(line, 0, AF_INET);
24 *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
29 static int read_mac(const char *line, void *arg)
31 struct ether_addr *temp_ether_addr;
33 temp_ether_addr = ether_aton_r(line, (struct ether_addr *)arg);
34 if (temp_ether_addr == NULL)
40 static int read_str(const char *line, void *arg)
45 *dest = xstrdup(line);
50 static int read_u32(const char *line, void *arg)
52 *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
57 static int read_yn(const char *line, void *arg)
61 if (!strcasecmp("yes", line)) {
65 if (!strcasecmp("no", line)) {
73 /* find option 'code' in opt_list */
74 struct option_set* FAST_FUNC find_option(struct option_set *opt_list, uint8_t code)
76 while (opt_list && opt_list->data[OPT_CODE] < code)
77 opt_list = opt_list->next;
79 if (opt_list && opt_list->data[OPT_CODE] == code)
85 /* add an option to the opt_list */
86 static void attach_option(struct option_set **opt_list,
87 const struct dhcp_option *option, char *buffer, int length)
89 struct option_set *existing, *new, **curr;
91 existing = find_option(*opt_list, option->code);
93 DEBUG("Attaching option %02x to list", option->code);
95 #if ENABLE_FEATURE_UDHCP_RFC3397
96 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
97 /* reuse buffer and length for RFC1035-formatted string */
98 buffer = (char *)dname_enc(NULL, 0, buffer, &length);
101 /* make a new option */
102 new = xmalloc(sizeof(*new));
103 new->data = xmalloc(length + 2);
104 new->data[OPT_CODE] = option->code;
105 new->data[OPT_LEN] = length;
106 memcpy(new->data + 2, buffer, length);
109 while (*curr && (*curr)->data[OPT_CODE] < option->code)
110 curr = &(*curr)->next;
114 #if ENABLE_FEATURE_UDHCP_RFC3397
115 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
121 /* add it to an existing option */
122 DEBUG("Attaching option %02x to existing member of list", option->code);
123 if (option->flags & OPTION_LIST) {
124 #if ENABLE_FEATURE_UDHCP_RFC3397
125 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
126 /* reuse buffer and length for RFC1035-formatted string */
127 buffer = (char *)dname_enc(existing->data + 2,
128 existing->data[OPT_LEN], buffer, &length);
130 if (existing->data[OPT_LEN] + length <= 255) {
131 existing->data = xrealloc(existing->data,
132 existing->data[OPT_LEN] + length + 3);
133 if ((option->flags & TYPE_MASK) == OPTION_STRING) {
134 /* ' ' can bring us to 256 - bad */
135 if (existing->data[OPT_LEN] + length >= 255)
137 /* add space separator between STRING options in a list */
138 existing->data[existing->data[OPT_LEN] + 2] = ' ';
139 existing->data[OPT_LEN]++;
141 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
142 existing->data[OPT_LEN] += length;
143 } /* else, ignore the data, we could put this in a second option in the future */
144 #if ENABLE_FEATURE_UDHCP_RFC3397
145 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
148 } /* else, ignore the new data */
152 /* read a dhcp option and add it to opt_list */
153 static int read_opt(const char *const_line, void *arg)
155 struct option_set **opt_list = arg;
156 char *opt, *val, *endptr;
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;
164 /* Cheat, the only const line we'll actually get is "" */
165 line = (char *) const_line;
166 opt = strtok(line, " \t=");
170 idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */
173 option = &dhcp_options[idx];
177 val = strtok(NULL, ", \t");
179 length = dhcp_option_lengths[option->flags & TYPE_MASK];
181 opt = buffer; /* new meaning for variable opt */
182 switch (option->flags & TYPE_MASK) {
184 retval = read_ip(val, buffer);
187 retval = read_ip(val, buffer);
188 val = strtok(NULL, ", \t/-");
192 retval = read_ip(val, buffer + 4);
195 #if ENABLE_FEATURE_UDHCP_RFC3397
198 length = strlen(val);
200 if (length > 254) length = 254;
206 retval = read_yn(val, buffer);
209 buffer[0] = strtoul(val, &endptr, 0);
210 retval = (endptr[0] == '\0');
212 /* htonX are macros in older libc's, using temp var
213 * in code below for safety */
214 /* TODO: use bb_strtoX? */
216 unsigned long tmp = strtoul(val, &endptr, 0);
217 *result_u16 = htons(tmp);
218 retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
222 long tmp = strtol(val, &endptr, 0);
223 *result_u16 = htons(tmp);
224 retval = (endptr[0] == '\0');
228 unsigned long tmp = strtoul(val, &endptr, 0);
229 *result_u32 = htonl(tmp);
230 retval = (endptr[0] == '\0');
234 long tmp = strtol(val, &endptr, 0);
235 *result_u32 = htonl(tmp);
236 retval = (endptr[0] == '\0');
243 attach_option(opt_list, option, opt, length);
244 } while (retval && option->flags & OPTION_LIST);
248 static int read_staticlease(const char *const_line, void *arg)
256 /* Allocate memory for addresses */
257 mac_bytes = xmalloc(sizeof(unsigned char) * 8);
258 ip = xmalloc(sizeof(uint32_t));
261 line = (char *) const_line;
262 mac_string = strtok(line, " \t");
263 read_mac(mac_string, mac_bytes);
266 ip_string = strtok(NULL, " \t");
267 read_ip(ip_string, ip);
269 addStaticLease(arg, mac_bytes, ip);
271 if (ENABLE_UDHCP_DEBUG) printStaticLeases(arg);
277 struct config_keyword {
279 int (*handler)(const char *line, void *var);
284 static const struct config_keyword keywords[] = {
285 /* keyword handler variable address default */
286 {"start", read_ip, &(server_config.start_ip), "192.168.0.20"},
287 {"end", read_ip, &(server_config.end_ip), "192.168.0.254"},
288 {"interface", read_str, &(server_config.interface), "eth0"},
289 /* Avoid "max_leases value not sane" warning by setting default
290 * to default_end_ip - default_start_ip + 1: */
291 {"max_leases", read_u32, &(server_config.max_leases), "235"},
292 {"remaining", read_yn, &(server_config.remaining), "yes"},
293 {"auto_time", read_u32, &(server_config.auto_time), "7200"},
294 {"decline_time", read_u32, &(server_config.decline_time), "3600"},
295 {"conflict_time",read_u32, &(server_config.conflict_time),"3600"},
296 {"offer_time", read_u32, &(server_config.offer_time), "60"},
297 {"min_lease", read_u32, &(server_config.min_lease), "60"},
298 {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
299 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
300 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
301 /* keywords with no defaults must be last! */
302 {"option", read_opt, &(server_config.options), ""},
303 {"opt", read_opt, &(server_config.options), ""},
304 {"notify_file", read_str, &(server_config.notify_file), ""},
305 {"sname", read_str, &(server_config.sname), ""},
306 {"boot_file", read_str, &(server_config.boot_file), ""},
307 {"static_lease", read_staticlease, &(server_config.static_leases), ""},
308 /* ADDME: static lease */
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)
347 time_t curr = time(0);
348 unsigned long tmp_time;
350 fp = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
355 for (i = 0; i < server_config.max_leases; i++) {
356 if (leases[i].yiaddr != 0) {
358 /* screw with the time in the struct, for easier writing */
359 tmp_time = leases[i].expires;
361 if (server_config.remaining) {
362 if (lease_expired(&(leases[i])))
363 leases[i].expires = 0;
364 else leases[i].expires -= curr;
365 } /* else stick with the time we got */
366 leases[i].expires = htonl(leases[i].expires);
367 // FIXME: error check??
368 full_write(fp, &leases[i], sizeof(leases[i]));
370 /* then restore it when done */
371 leases[i].expires = tmp_time;
376 if (server_config.notify_file) {
377 // TODO: vfork-based child creation
378 char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
385 void FAST_FUNC read_leases(const char *file)
389 struct dhcpOfferedAddr lease;
391 fp = open_or_warn(file, O_RDONLY);
397 while (i < server_config.max_leases
398 && full_read(fp, &lease, sizeof(lease)) == sizeof(lease)
400 /* ADDME: is it a static lease */
401 uint32_t y = ntohl(lease.yiaddr);
402 if (y >= server_config.start_ip && y <= server_config.end_ip) {
403 lease.expires = ntohl(lease.expires);
404 if (!server_config.remaining)
405 lease.expires -= time(NULL);
406 if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
407 bb_error_msg("too many leases while loading %s", file);
413 DEBUG("Read %d leases", i);