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 <sys/socket.h>
15 #include <netinet/ether.h>
16 #include "static_leases.h"
24 * Domain names may have 254 chars, and string options can be 254
25 * chars long. However, 80 bytes will be enough for most, and won't
26 * hog up memory. If you have a special application, change it
28 #define READ_CONFIG_BUF_SIZE 80
30 /* on these functions, make sure you datatype matches */
31 static int read_ip(const char *line, void *arg)
33 struct in_addr *addr = arg;
37 if (!inet_aton(line, addr)) {
38 if ((host = gethostbyname(line)))
39 addr->s_addr = *((unsigned long *) host->h_addr_list[0]);
45 static int read_mac(const char *line, void *arg)
47 uint8_t *mac_bytes = arg;
48 struct ether_addr *temp_ether_addr;
51 temp_ether_addr = ether_aton(line);
53 if(temp_ether_addr == NULL)
56 memcpy(mac_bytes, temp_ether_addr, 6);
62 static int read_str(const char *line, void *arg)
73 static int read_u32(const char *line, void *arg)
77 *dest = strtoul(line, &endptr, 0);
78 return endptr[0] == '\0';
82 static int read_yn(const char *line, void *arg)
87 if (!strcasecmp("yes", line))
89 else if (!strcasecmp("no", line))
97 /* find option 'code' in opt_list */
98 struct option_set *find_option(struct option_set *opt_list, char code)
100 while (opt_list && opt_list->data[OPT_CODE] < code)
101 opt_list = opt_list->next;
103 if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
108 /* add an option to the opt_list */
109 static void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
111 struct option_set *existing, *new, **curr;
113 /* add it to an existing option */
114 if ((existing = find_option(*opt_list, option->code))) {
115 DEBUG("Attaching option %s to existing member of list", option->name);
116 if (option->flags & OPTION_LIST) {
117 if (existing->data[OPT_LEN] + length <= 255) {
118 existing->data = realloc(existing->data,
119 existing->data[OPT_LEN] + length + 2);
120 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
121 existing->data[OPT_LEN] += length;
122 } /* else, ignore the data, we could put this in a second option in the future */
123 } /* else, ignore the new data */
125 DEBUG("Attaching option %s to list", option->name);
127 /* make a new option */
128 new = xmalloc(sizeof(struct option_set));
129 new->data = xmalloc(length + 2);
130 new->data[OPT_CODE] = option->code;
131 new->data[OPT_LEN] = length;
132 memcpy(new->data + 2, buffer, length);
135 while (*curr && (*curr)->data[OPT_CODE] < option->code)
136 curr = &(*curr)->next;
144 /* read a dhcp option and add it to opt_list */
145 static int read_opt(const char *const_line, void *arg)
147 struct option_set **opt_list = arg;
148 char *opt, *val, *endptr;
149 struct dhcp_option *option;
150 int retval = 0, length;
153 uint16_t *result_u16 = (uint16_t *) buffer;
154 uint32_t *result_u32 = (uint32_t *) buffer;
156 /* Cheat, the only const line we'll actually get is "" */
157 line = (char *) const_line;
158 if (!(opt = strtok(line, " \t="))) return 0;
160 for (option = dhcp_options; option->code; option++)
161 if (!strcasecmp(option->name, opt))
164 if (!option->code) return 0;
167 if (!(val = strtok(NULL, ", \t"))) break;
168 length = option_lengths[option->flags & TYPE_MASK];
170 opt = buffer; /* new meaning for variable opt */
171 switch (option->flags & TYPE_MASK) {
173 retval = read_ip(val, buffer);
176 retval = read_ip(val, buffer);
177 if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
178 if (retval) retval = read_ip(val, buffer + 4);
181 length = strlen(val);
183 if (length > 254) length = 254;
189 retval = read_yn(val, buffer);
192 buffer[0] = strtoul(val, &endptr, 0);
193 retval = (endptr[0] == '\0');
196 *result_u16 = htons(strtoul(val, &endptr, 0));
197 retval = (endptr[0] == '\0');
200 *result_u16 = htons(strtol(val, &endptr, 0));
201 retval = (endptr[0] == '\0');
204 *result_u32 = htonl(strtoul(val, &endptr, 0));
205 retval = (endptr[0] == '\0');
208 *result_u32 = htonl(strtol(val, &endptr, 0));
209 retval = (endptr[0] == '\0');
215 attach_option(opt_list, option, opt, length);
216 } while (retval && option->flags & OPTION_LIST);
220 static int read_staticlease(const char *const_line, void *arg)
230 /* Allocate memory for addresses */
231 mac_bytes = xmalloc(sizeof(unsigned char) * 8);
232 ip = xmalloc(sizeof(uint32_t));
235 line = (char *) const_line;
236 mac_string = strtok(line, " \t");
237 read_mac(mac_string, mac_bytes);
240 ip_string = strtok(NULL, " \t");
241 read_ip(ip_string, ip);
243 addStaticLease(arg, mac_bytes, ip);
245 if (ENABLE_FEATURE_UDHCP_DEBUG) printStaticLeases(arg);
252 static const struct config_keyword keywords[] = {
253 /* keyword handler variable address default */
254 {"start", read_ip, &(server_config.start), "192.168.0.20"},
255 {"end", read_ip, &(server_config.end), "192.168.0.254"},
256 {"interface", read_str, &(server_config.interface), "eth0"},
257 {"option", read_opt, &(server_config.options), ""},
258 {"opt", read_opt, &(server_config.options), ""},
259 {"max_leases", read_u32, &(server_config.max_leases), "254"},
260 {"remaining", read_yn, &(server_config.remaining), "yes"},
261 {"auto_time", read_u32, &(server_config.auto_time), "7200"},
262 {"decline_time",read_u32, &(server_config.decline_time),"3600"},
263 {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
264 {"offer_time", read_u32, &(server_config.offer_time), "60"},
265 {"min_lease", read_u32, &(server_config.min_lease), "60"},
266 {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
267 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
268 {"notify_file", read_str, &(server_config.notify_file), ""},
269 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
270 {"sname", read_str, &(server_config.sname), ""},
271 {"boot_file", read_str, &(server_config.boot_file), ""},
272 {"static_lease",read_staticlease, &(server_config.static_leases), ""},
273 /*ADDME: static lease */
278 int read_config(const char *file)
281 char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
284 for (i = 0; keywords[i].keyword[0]; i++)
285 if (keywords[i].def[0])
286 keywords[i].handler(keywords[i].def, keywords[i].var);
288 if (!(in = fopen(file, "r"))) {
289 bb_error_msg("Unable to open config file: %s", file);
293 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
294 char debug_orig[READ_CONFIG_BUF_SIZE];
297 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
298 if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer);
299 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
301 if (!(token = strtok(buffer, " \t"))) continue;
302 if (!(line = strtok(NULL, ""))) continue;
304 /* eat leading whitespace */
305 line = line + strspn(line, " \t=");
306 /* eat trailing whitespace */
307 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
310 for (i = 0; keywords[i].keyword[0]; i++)
311 if (!strcasecmp(token, keywords[i].keyword))
312 if (!keywords[i].handler(line, keywords[i].var)) {
313 bb_error_msg("Failure parsing line %d of %s", lm, file);
314 if (ENABLE_FEATURE_UDHCP_DEBUG)
315 bb_error_msg("unable to parse '%s'", debug_orig);
316 /* reset back to the default value */
317 keywords[i].handler(keywords[i].def, keywords[i].var);
325 void write_leases(void)
330 time_t curr = time(0);
331 unsigned long tmp_time;
333 if (!(fp = fopen(server_config.lease_file, "w"))) {
334 bb_error_msg("Unable to open %s for writing", server_config.lease_file);
338 for (i = 0; i < server_config.max_leases; i++) {
339 if (leases[i].yiaddr != 0) {
341 /* screw with the time in the struct, for easier writing */
342 tmp_time = leases[i].expires;
344 if (server_config.remaining) {
345 if (lease_expired(&(leases[i])))
346 leases[i].expires = 0;
347 else leases[i].expires -= curr;
348 } /* else stick with the time we got */
349 leases[i].expires = htonl(leases[i].expires);
350 fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp);
352 /* Then restore it when done. */
353 leases[i].expires = tmp_time;
358 if (server_config.notify_file) {
359 sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file);
365 void read_leases(const char *file)
369 struct dhcpOfferedAddr lease;
371 if (!(fp = fopen(file, "r"))) {
372 bb_error_msg("Unable to open %s for reading", file);
376 while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) {
377 /* ADDME: is it a static lease */
378 if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
379 lease.expires = ntohl(lease.expires);
380 if (!server_config.remaining) lease.expires -= time(0);
381 if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
382 bb_error_msg("Too many leases while loading %s", file);
388 DEBUG("Read %d leases", i);