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);
22 *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
27 static int read_mac(const char *line, void *arg)
29 struct ether_addr *temp_ether_addr;
31 temp_ether_addr = ether_aton_r(line, (struct ether_addr *)arg);
32 if (temp_ether_addr == NULL)
38 static int read_str(const char *line, void *arg)
43 *dest = xstrdup(line);
48 static int read_u32(const char *line, void *arg)
50 *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
55 static int read_yn(const char *line, void *arg)
59 if (!strcasecmp("yes", line)) {
63 if (!strcasecmp("no", line)) {
71 /* find option 'code' in opt_list */
72 struct option_set *find_option(struct option_set *opt_list, uint8_t code)
74 while (opt_list && opt_list->data[OPT_CODE] < code)
75 opt_list = opt_list->next;
77 if (opt_list && opt_list->data[OPT_CODE] == code)
83 /* add an option to the opt_list */
84 static void attach_option(struct option_set **opt_list,
85 const struct dhcp_option *option, char *buffer, int length)
87 struct option_set *existing, *new, **curr;
89 existing = find_option(*opt_list, option->code);
91 DEBUG("Attaching option %02x to list", option->code);
93 #if ENABLE_FEATURE_RFC3397
94 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
95 /* reuse buffer and length for RFC1035-formatted string */
96 buffer = (char *)dname_enc(NULL, 0, buffer, &length);
99 /* make a new option */
100 new = xmalloc(sizeof(*new));
101 new->data = xmalloc(length + 2);
102 new->data[OPT_CODE] = option->code;
103 new->data[OPT_LEN] = length;
104 memcpy(new->data + 2, buffer, length);
107 while (*curr && (*curr)->data[OPT_CODE] < option->code)
108 curr = &(*curr)->next;
112 #if ENABLE_FEATURE_RFC3397
113 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
119 /* add it to an existing option */
120 DEBUG("Attaching option %02x to existing member of list", option->code);
121 if (option->flags & OPTION_LIST) {
122 #if ENABLE_FEATURE_RFC3397
123 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
124 /* reuse buffer and length for RFC1035-formatted string */
125 buffer = (char *)dname_enc(existing->data + 2,
126 existing->data[OPT_LEN], buffer, &length);
128 if (existing->data[OPT_LEN] + length <= 255) {
129 existing->data = xrealloc(existing->data,
130 existing->data[OPT_LEN] + length + 3);
131 if ((option->flags & TYPE_MASK) == OPTION_STRING) {
132 /* ' ' can bring us to 256 - bad */
133 if (existing->data[OPT_LEN] + length >= 255)
135 /* add space separator between STRING options in a list */
136 existing->data[existing->data[OPT_LEN] + 2] = ' ';
137 existing->data[OPT_LEN]++;
139 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
140 existing->data[OPT_LEN] += length;
141 } /* else, ignore the data, we could put this in a second option in the future */
142 #if ENABLE_FEATURE_RFC3397
143 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
146 } /* else, ignore the new data */
150 /* read a dhcp option and add it to opt_list */
151 static int read_opt(const char *const_line, void *arg)
153 struct option_set **opt_list = arg;
154 char *opt, *val, *endptr;
156 const struct dhcp_option *option;
157 int retval, length, idx;
158 char buffer[8] ALIGNED(4);
159 uint16_t *result_u16 = (uint16_t *) buffer;
160 uint32_t *result_u32 = (uint32_t *) buffer;
162 /* Cheat, the only const line we'll actually get is "" */
163 line = (char *) const_line;
164 opt = strtok(line, " \t=");
168 idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */
171 option = &dhcp_options[idx];
175 val = strtok(NULL, ", \t");
177 length = dhcp_option_lengths[option->flags & TYPE_MASK];
179 opt = buffer; /* new meaning for variable opt */
180 switch (option->flags & TYPE_MASK) {
182 retval = read_ip(val, buffer);
185 retval = read_ip(val, buffer);
186 val = strtok(NULL, ", \t/-");
190 retval = read_ip(val, buffer + 4);
193 #if ENABLE_FEATURE_RFC3397
196 length = strlen(val);
198 if (length > 254) length = 254;
204 retval = read_yn(val, buffer);
207 buffer[0] = strtoul(val, &endptr, 0);
208 retval = (endptr[0] == '\0');
210 /* htonX are macros in older libc's, using temp var
211 * in code below for safety */
212 /* TODO: use bb_strtoX? */
214 unsigned long tmp = strtoul(val, &endptr, 0);
215 *result_u16 = htons(tmp);
216 retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
220 long tmp = strtol(val, &endptr, 0);
221 *result_u16 = htons(tmp);
222 retval = (endptr[0] == '\0');
226 unsigned long tmp = strtoul(val, &endptr, 0);
227 *result_u32 = htonl(tmp);
228 retval = (endptr[0] == '\0');
232 long tmp = strtol(val, &endptr, 0);
233 *result_u32 = htonl(tmp);
234 retval = (endptr[0] == '\0');
241 attach_option(opt_list, option, opt, length);
242 } while (retval && option->flags & OPTION_LIST);
246 static int read_staticlease(const char *const_line, void *arg)
254 /* Allocate memory for addresses */
255 mac_bytes = xmalloc(sizeof(unsigned char) * 8);
256 ip = xmalloc(sizeof(uint32_t));
259 line = (char *) const_line;
260 mac_string = strtok(line, " \t");
261 read_mac(mac_string, mac_bytes);
264 ip_string = strtok(NULL, " \t");
265 read_ip(ip_string, ip);
267 addStaticLease(arg, mac_bytes, ip);
269 if (ENABLE_FEATURE_UDHCP_DEBUG) printStaticLeases(arg);
275 struct config_keyword {
277 int (*handler)(const char *line, void *var);
282 static const struct config_keyword keywords[] = {
283 /* keyword handler variable address default */
284 {"start", read_ip, &(server_config.start_ip), "192.168.0.20"},
285 {"end", read_ip, &(server_config.end_ip), "192.168.0.254"},
286 {"interface", read_str, &(server_config.interface), "eth0"},
287 /* Avoid "max_leases value not sane" warning by setting default
288 * to default_end_ip - default_start_ip + 1: */
289 {"max_leases", read_u32, &(server_config.max_leases), "235"},
290 {"remaining", read_yn, &(server_config.remaining), "yes"},
291 {"auto_time", read_u32, &(server_config.auto_time), "7200"},
292 {"decline_time", read_u32, &(server_config.decline_time), "3600"},
293 {"conflict_time",read_u32, &(server_config.conflict_time),"3600"},
294 {"offer_time", read_u32, &(server_config.offer_time), "60"},
295 {"min_lease", read_u32, &(server_config.min_lease), "60"},
296 {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
297 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
298 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
299 /* keywords with no defaults must be last! */
300 {"option", read_opt, &(server_config.options), ""},
301 {"opt", read_opt, &(server_config.options), ""},
302 {"notify_file", read_str, &(server_config.notify_file), ""},
303 {"sname", read_str, &(server_config.sname), ""},
304 {"boot_file", read_str, &(server_config.boot_file), ""},
305 {"static_lease", read_staticlease, &(server_config.static_leases), ""},
306 /* ADDME: static lease */
308 enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
312 * Domain names may have 254 chars, and string options can be 254
313 * chars long. However, 80 bytes will be enough for most, and won't
314 * hog up memory. If you have a special application, change it
316 #define READ_CONFIG_BUF_SIZE 80
318 void read_config(const char *file)
321 char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
324 for (i = 0; i < KWS_WITH_DEFAULTS; i++)
325 keywords[i].handler(keywords[i].def, keywords[i].var);
327 in = fopen_or_warn(file, "r");
332 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
334 /* *strchrnul(buffer, '\n') = '\0'; - trim() will do it */
335 *strchrnul(buffer, '#') = '\0';
337 token = strtok(buffer, " \t");
338 if (!token) continue;
339 line = strtok(NULL, "");
342 trim(line); /* remove leading/trailing whitespace */
344 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
345 if (!strcasecmp(token, keywords[i].keyword)) {
346 if (!keywords[i].handler(line, keywords[i].var)) {
347 bb_error_msg("can't parse line %u in %s at '%s'",
349 /* reset back to the default value */
350 keywords[i].handler(keywords[i].def, keywords[i].var);
358 server_config.start_ip = ntohl(server_config.start_ip);
359 server_config.end_ip = ntohl(server_config.end_ip);
363 void write_leases(void)
367 time_t curr = time(0);
368 unsigned long tmp_time;
370 fp = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
375 for (i = 0; i < server_config.max_leases; i++) {
376 if (leases[i].yiaddr != 0) {
378 /* screw with the time in the struct, for easier writing */
379 tmp_time = leases[i].expires;
381 if (server_config.remaining) {
382 if (lease_expired(&(leases[i])))
383 leases[i].expires = 0;
384 else leases[i].expires -= curr;
385 } /* else stick with the time we got */
386 leases[i].expires = htonl(leases[i].expires);
387 // FIXME: error check??
388 full_write(fp, &leases[i], sizeof(leases[i]));
390 /* then restore it when done */
391 leases[i].expires = tmp_time;
396 if (server_config.notify_file) {
397 // TODO: vfork-based child creation
398 char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
405 void read_leases(const char *file)
409 struct dhcpOfferedAddr lease;
411 fp = open_or_warn(file, O_RDONLY);
417 while (i < server_config.max_leases
418 && full_read(fp, &lease, sizeof(lease)) == sizeof(lease)
420 /* ADDME: is it a static lease */
421 uint32_t y = ntohl(lease.yiaddr);
422 if (y >= server_config.start_ip && y <= server_config.end_ip) {
423 lease.expires = ntohl(lease.expires);
424 if (!server_config.remaining)
425 lease.expires -= time(NULL);
426 if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
427 bb_error_msg("too many leases while loading %s", file);
433 DEBUG("Read %d leases", i);