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
7 #include <netinet/ether.h>
14 /* on these functions, make sure your datatype matches */
15 static int read_ip(const char *line, void *arg)
17 len_and_sockaddr *lsa;
19 lsa = host_and_af2sockaddr(line, 0, AF_INET);
21 *(uint32_t*)arg = lsa->sin.sin_addr.s_addr;
28 static int read_mac(const char *line, void *arg)
30 uint8_t *mac_bytes = arg;
31 struct ether_addr *temp_ether_addr;
33 temp_ether_addr = ether_aton(line);
34 if (temp_ether_addr == NULL)
36 memcpy(mac_bytes, temp_ether_addr, 6);
41 static int read_str(const char *line, void *arg)
46 *dest = xstrdup(line);
51 static int read_u32(const char *line, void *arg)
53 *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
58 static int read_yn(const char *line, void *arg)
62 if (!strcasecmp("yes", line)) {
66 if (!strcasecmp("no", line)) {
74 /* find option 'code' in opt_list */
75 struct option_set *find_option(struct option_set *opt_list, char code)
77 while (opt_list && opt_list->data[OPT_CODE] < code)
78 opt_list = opt_list->next;
80 if (opt_list && opt_list->data[OPT_CODE] == code)
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)
90 struct option_set *existing, *new, **curr;
92 existing = find_option(*opt_list, option->code);
94 DEBUG("Attaching option %s to list", option->name);
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);
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);
110 while (*curr && (*curr)->data[OPT_CODE] < option->code)
111 curr = &(*curr)->next;
115 #if ENABLE_FEATURE_RFC3397
116 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
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);
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)
138 /* add space separator between STRING options in a list */
139 existing->data[existing->data[OPT_LEN] + 2] = ' ';
140 existing->data[OPT_LEN]++;
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)
149 } /* else, ignore the new data */
153 /* read a dhcp option and add it to opt_list */
154 static int read_opt(const char *const_line, void *arg)
156 struct option_set **opt_list = arg;
157 char *opt, *val, *endptr;
158 const struct dhcp_option *option;
159 int retval = 0, length;
162 uint16_t *result_u16 = (uint16_t *) buffer;
163 uint32_t *result_u32 = (uint32_t *) buffer;
165 /* Cheat, the only const line we'll actually get is "" */
166 line = (char *) const_line;
167 opt = strtok(line, " \t=");
170 option = dhcp_options;
174 if (!strcasecmp(option->name, opt))
180 val = strtok(NULL, ", \t");
182 length = option_lengths[option->flags & TYPE_MASK];
184 opt = buffer; /* new meaning for variable opt */
185 switch (option->flags & TYPE_MASK) {
187 retval = read_ip(val, buffer);
190 retval = read_ip(val, buffer);
191 val = strtok(NULL, ", \t/-");
195 retval = read_ip(val, buffer + 4);
198 #if ENABLE_FEATURE_RFC3397
201 length = strlen(val);
203 if (length > 254) length = 254;
209 retval = read_yn(val, buffer);
212 buffer[0] = strtoul(val, &endptr, 0);
213 retval = (endptr[0] == '\0');
215 /* htonX are macros in older libc's, using temp var
216 * in code below for safety */
217 /* TODO: use bb_strtoX? */
219 unsigned long tmp = strtoul(val, &endptr, 0);
220 *result_u16 = htons(tmp);
221 retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
225 long tmp = strtol(val, &endptr, 0);
226 *result_u16 = htons(tmp);
227 retval = (endptr[0] == '\0');
231 unsigned long tmp = strtoul(val, &endptr, 0);
232 *result_u32 = htonl(tmp);
233 retval = (endptr[0] == '\0');
237 long tmp = strtol(val, &endptr, 0);
238 *result_u32 = htonl(tmp);
239 retval = (endptr[0] == '\0');
246 attach_option(opt_list, option, opt, length);
247 } while (retval && option->flags & OPTION_LIST);
251 static int read_staticlease(const char *const_line, void *arg)
259 /* Allocate memory for addresses */
260 mac_bytes = xmalloc(sizeof(unsigned char) * 8);
261 ip = xmalloc(sizeof(uint32_t));
264 line = (char *) const_line;
265 mac_string = strtok(line, " \t");
266 read_mac(mac_string, mac_bytes);
269 ip_string = strtok(NULL, " \t");
270 read_ip(ip_string, ip);
272 addStaticLease(arg, mac_bytes, ip);
274 if (ENABLE_FEATURE_UDHCP_DEBUG) printStaticLeases(arg);
280 struct config_keyword {
282 int (*handler)(const char *line, void *var);
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 */
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
319 #define READ_CONFIG_BUF_SIZE 80
321 int read_config(const char *file)
324 char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
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);
331 in = fopen_or_warn(file, "r");
336 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
337 char debug_orig[READ_CONFIG_BUF_SIZE];
341 p = strchr(buffer, '\n');
343 if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer);
344 p = strchr(buffer, '#');
347 if (!(token = strtok(buffer, " \t"))) continue;
348 if (!(line = strtok(NULL, ""))) continue;
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]))
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);
369 server_config.start_ip = ntohl(server_config.start_ip);
370 server_config.end_ip = ntohl(server_config.end_ip);
376 void write_leases(void)
380 time_t curr = time(0);
381 unsigned long tmp_time;
383 fp = open3_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
388 for (i = 0; i < server_config.max_leases; i++) {
389 if (leases[i].yiaddr != 0) {
391 /* screw with the time in the struct, for easier writing */
392 tmp_time = leases[i].expires;
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]));
403 /* then restore it when done */
404 leases[i].expires = tmp_time;
409 if (server_config.notify_file) {
410 char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
417 void read_leases(const char *file)
421 struct dhcpOfferedAddr lease;
423 fp = open_or_warn(file, O_RDONLY);
428 while (i < server_config.max_leases
429 && full_read(fp, &lease, sizeof(lease)) == sizeof(lease)
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);
444 DEBUG("Read %d leases", i);