2 * files.c -- DHCP server file manipulation *
3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
6 #include <sys/socket.h>
14 #include <netinet/ether.h>
15 #include "static_leases.h"
23 * Domain names may have 254 chars, and string options can be 254
24 * chars long. However, 80 bytes will be enough for most, and won't
25 * hog up memory. If you have a special application, change it
27 #define READ_CONFIG_BUF_SIZE 80
29 /* on these functions, make sure you datatype matches */
30 static int read_ip(const char *line, void *arg)
32 struct in_addr *addr = arg;
36 if (!inet_aton(line, addr)) {
37 if ((host = gethostbyname(line)))
38 addr->s_addr = *((unsigned long *) host->h_addr_list[0]);
44 static int read_mac(const char *line, void *arg)
46 uint8_t *mac_bytes = arg;
47 struct ether_addr *temp_ether_addr;
50 temp_ether_addr = ether_aton(line);
52 if(temp_ether_addr == NULL)
55 memcpy(mac_bytes, temp_ether_addr, 6);
61 static int read_str(const char *line, void *arg)
72 static int read_u32(const char *line, void *arg)
76 *dest = strtoul(line, &endptr, 0);
77 return endptr[0] == '\0';
81 static int read_yn(const char *line, void *arg)
86 if (!strcasecmp("yes", line))
88 else if (!strcasecmp("no", line))
96 /* find option 'code' in opt_list */
97 struct option_set *find_option(struct option_set *opt_list, char code)
99 while (opt_list && opt_list->data[OPT_CODE] < code)
100 opt_list = opt_list->next;
102 if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
107 /* add an option to the opt_list */
108 static void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
110 struct option_set *existing, *new, **curr;
112 /* add it to an existing option */
113 if ((existing = find_option(*opt_list, option->code))) {
114 DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name);
115 if (option->flags & OPTION_LIST) {
116 if (existing->data[OPT_LEN] + length <= 255) {
117 existing->data = realloc(existing->data,
118 existing->data[OPT_LEN] + length + 2);
119 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
120 existing->data[OPT_LEN] += length;
121 } /* else, ignore the data, we could put this in a second option in the future */
122 } /* else, ignore the new data */
124 DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
126 /* make a new option */
127 new = xmalloc(sizeof(struct option_set));
128 new->data = xmalloc(length + 2);
129 new->data[OPT_CODE] = option->code;
130 new->data[OPT_LEN] = length;
131 memcpy(new->data + 2, buffer, length);
134 while (*curr && (*curr)->data[OPT_CODE] < option->code)
135 curr = &(*curr)->next;
143 /* read a dhcp option and add it to opt_list */
144 static int read_opt(const char *const_line, void *arg)
146 struct option_set **opt_list = arg;
147 char *opt, *val, *endptr;
148 struct dhcp_option *option;
149 int retval = 0, length;
152 uint16_t *result_u16 = (uint16_t *) buffer;
153 uint32_t *result_u32 = (uint32_t *) buffer;
155 /* Cheat, the only const line we'll actually get is "" */
156 line = (char *) const_line;
157 if (!(opt = strtok(line, " \t="))) return 0;
159 for (option = dhcp_options; option->code; option++)
160 if (!strcasecmp(option->name, opt))
163 if (!option->code) return 0;
166 if (!(val = strtok(NULL, ", \t"))) break;
167 length = option_lengths[option->flags & TYPE_MASK];
169 opt = buffer; /* new meaning for variable opt */
170 switch (option->flags & TYPE_MASK) {
172 retval = read_ip(val, buffer);
175 retval = read_ip(val, buffer);
176 if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
177 if (retval) retval = read_ip(val, buffer + 4);
180 length = strlen(val);
182 if (length > 254) length = 254;
188 retval = read_yn(val, buffer);
191 buffer[0] = strtoul(val, &endptr, 0);
192 retval = (endptr[0] == '\0');
195 *result_u16 = htons(strtoul(val, &endptr, 0));
196 retval = (endptr[0] == '\0');
199 *result_u16 = htons(strtol(val, &endptr, 0));
200 retval = (endptr[0] == '\0');
203 *result_u32 = htonl(strtoul(val, &endptr, 0));
204 retval = (endptr[0] == '\0');
207 *result_u32 = htonl(strtol(val, &endptr, 0));
208 retval = (endptr[0] == '\0');
214 attach_option(opt_list, option, opt, length);
215 } while (retval && option->flags & OPTION_LIST);
219 static int read_staticlease(const char *const_line, void *arg)
229 /* Allocate memory for addresses */
230 mac_bytes = xmalloc(sizeof(unsigned char) * 8);
231 ip = xmalloc(sizeof(uint32_t));
234 line = (char *) const_line;
235 mac_string = strtok(line, " \t");
236 read_mac(mac_string, mac_bytes);
239 ip_string = strtok(NULL, " \t");
240 read_ip(ip_string, ip);
242 addStaticLease(arg, mac_bytes, ip);
245 printStaticLeases(arg);
253 static const struct config_keyword keywords[] = {
254 /* keyword handler variable address default */
255 {"start", read_ip, &(server_config.start), "192.168.0.20"},
256 {"end", read_ip, &(server_config.end), "192.168.0.254"},
257 {"interface", read_str, &(server_config.interface), "eth0"},
258 {"option", read_opt, &(server_config.options), ""},
259 {"opt", read_opt, &(server_config.options), ""},
260 {"max_leases", read_u32, &(server_config.max_leases), "254"},
261 {"remaining", read_yn, &(server_config.remaining), "yes"},
262 {"auto_time", read_u32, &(server_config.auto_time), "7200"},
263 {"decline_time",read_u32, &(server_config.decline_time),"3600"},
264 {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
265 {"offer_time", read_u32, &(server_config.offer_time), "60"},
266 {"min_lease", read_u32, &(server_config.min_lease), "60"},
267 {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
268 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
269 {"notify_file", read_str, &(server_config.notify_file), ""},
270 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
271 {"sname", read_str, &(server_config.sname), ""},
272 {"boot_file", read_str, &(server_config.boot_file), ""},
273 {"static_lease",read_staticlease, &(server_config.static_leases), ""},
274 /*ADDME: static lease */
279 int read_config(const char *file)
282 char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
284 char orig[READ_CONFIG_BUF_SIZE];
288 for (i = 0; keywords[i].keyword[0]; i++)
289 if (keywords[i].def[0])
290 keywords[i].handler(keywords[i].def, keywords[i].var);
292 if (!(in = fopen(file, "r"))) {
293 LOG(LOG_ERR, "unable to open config file: %s", file);
297 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
299 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
301 strcpy(orig, buffer);
303 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
305 if (!(token = strtok(buffer, " \t"))) continue;
306 if (!(line = strtok(NULL, ""))) continue;
308 /* eat leading whitespace */
309 line = line + strspn(line, " \t=");
310 /* eat trailing whitespace */
311 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
314 for (i = 0; keywords[i].keyword[0]; i++)
315 if (!strcasecmp(token, keywords[i].keyword))
316 if (!keywords[i].handler(line, keywords[i].var)) {
317 LOG(LOG_ERR, "Failure parsing line %d of %s", lm, file);
318 DEBUG(LOG_ERR, "unable to parse '%s'", orig);
319 /* reset back to the default value */
320 keywords[i].handler(keywords[i].def, keywords[i].var);
328 void write_leases(void)
333 time_t curr = time(0);
334 unsigned long tmp_time;
336 if (!(fp = fopen(server_config.lease_file, "w"))) {
337 LOG(LOG_ERR, "Unable to open %s for writing", server_config.lease_file);
341 for (i = 0; i < server_config.max_leases; i++) {
342 if (leases[i].yiaddr != 0) {
344 /* screw with the time in the struct, for easier writing */
345 tmp_time = leases[i].expires;
347 if (server_config.remaining) {
348 if (lease_expired(&(leases[i])))
349 leases[i].expires = 0;
350 else leases[i].expires -= curr;
351 } /* else stick with the time we got */
352 leases[i].expires = htonl(leases[i].expires);
353 fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp);
355 /* Then restore it when done. */
356 leases[i].expires = tmp_time;
361 if (server_config.notify_file) {
362 sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file);
368 void read_leases(const char *file)
372 struct dhcpOfferedAddr lease;
374 if (!(fp = fopen(file, "r"))) {
375 LOG(LOG_ERR, "Unable to open %s for reading", file);
379 while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) {
380 /* ADDME: is it a static lease */
381 if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
382 lease.expires = ntohl(lease.expires);
383 if (!server_config.remaining) lease.expires -= time(0);
384 if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
385 LOG(LOG_WARNING, "Too many leases while loading %s\n", file);
391 DEBUG(LOG_INFO, "Read %d leases", i);