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)
65 if (*dest) free(*dest);
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 /* read a dhcp option and add it to opt_list */
97 static int read_opt(const char *const_line, void *arg)
99 struct option_set **opt_list = arg;
100 char *opt, *val, *endptr;
101 struct dhcp_option *option;
102 int retval = 0, length;
105 uint16_t *result_u16 = (uint16_t *) buffer;
106 uint32_t *result_u32 = (uint32_t *) buffer;
108 /* Cheat, the only const line we'll actually get is "" */
109 line = (char *) const_line;
110 if (!(opt = strtok(line, " \t="))) return 0;
112 for (option = dhcp_options; option->code; option++)
113 if (!strcasecmp(option->name, opt))
116 if (!option->code) return 0;
119 if (!(val = strtok(NULL, ", \t"))) break;
120 length = option_lengths[option->flags & TYPE_MASK];
122 opt = buffer; /* new meaning for variable opt */
123 switch (option->flags & TYPE_MASK) {
125 retval = read_ip(val, buffer);
128 retval = read_ip(val, buffer);
129 if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
130 if (retval) retval = read_ip(val, buffer + 4);
133 length = strlen(val);
135 if (length > 254) length = 254;
141 retval = read_yn(val, buffer);
144 buffer[0] = strtoul(val, &endptr, 0);
145 retval = (endptr[0] == '\0');
148 *result_u16 = htons(strtoul(val, &endptr, 0));
149 retval = (endptr[0] == '\0');
152 *result_u16 = htons(strtol(val, &endptr, 0));
153 retval = (endptr[0] == '\0');
156 *result_u32 = htonl(strtoul(val, &endptr, 0));
157 retval = (endptr[0] == '\0');
160 *result_u32 = htonl(strtol(val, &endptr, 0));
161 retval = (endptr[0] == '\0');
167 attach_option(opt_list, option, opt, length);
168 } while (retval && option->flags & OPTION_LIST);
172 static int read_staticlease(const char *const_line, void *arg)
182 /* Allocate memory for addresses */
183 mac_bytes = xmalloc(sizeof(unsigned char) * 8);
184 ip = xmalloc(sizeof(uint32_t));
187 line = (char *) const_line;
188 mac_string = strtok(line, " \t");
189 read_mac(mac_string, mac_bytes);
192 ip_string = strtok(NULL, " \t");
193 read_ip(ip_string, ip);
195 addStaticLease(arg, mac_bytes, ip);
198 printStaticLeases(arg);
206 static const struct config_keyword keywords[] = {
207 /* keyword handler variable address default */
208 {"start", read_ip, &(server_config.start), "192.168.0.20"},
209 {"end", read_ip, &(server_config.end), "192.168.0.254"},
210 {"interface", read_str, &(server_config.interface), "eth0"},
211 {"option", read_opt, &(server_config.options), ""},
212 {"opt", read_opt, &(server_config.options), ""},
213 {"max_leases", read_u32, &(server_config.max_leases), "254"},
214 {"remaining", read_yn, &(server_config.remaining), "yes"},
215 {"auto_time", read_u32, &(server_config.auto_time), "7200"},
216 {"decline_time",read_u32, &(server_config.decline_time),"3600"},
217 {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
218 {"offer_time", read_u32, &(server_config.offer_time), "60"},
219 {"min_lease", read_u32, &(server_config.min_lease), "60"},
220 {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
221 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
222 {"notify_file", read_str, &(server_config.notify_file), ""},
223 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
224 {"sname", read_str, &(server_config.sname), ""},
225 {"boot_file", read_str, &(server_config.boot_file), ""},
226 {"static_lease",read_staticlease, &(server_config.static_leases), ""},
227 /*ADDME: static lease */
232 int read_config(const char *file)
235 char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
237 char orig[READ_CONFIG_BUF_SIZE];
241 for (i = 0; keywords[i].keyword[0]; i++)
242 if (keywords[i].def[0])
243 keywords[i].handler(keywords[i].def, keywords[i].var);
245 if (!(in = fopen(file, "r"))) {
246 LOG(LOG_ERR, "unable to open config file: %s", file);
250 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
252 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
254 strcpy(orig, buffer);
256 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
258 if (!(token = strtok(buffer, " \t"))) continue;
259 if (!(line = strtok(NULL, ""))) continue;
261 /* eat leading whitespace */
262 line = line + strspn(line, " \t=");
263 /* eat trailing whitespace */
264 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
267 for (i = 0; keywords[i].keyword[0]; i++)
268 if (!strcasecmp(token, keywords[i].keyword))
269 if (!keywords[i].handler(line, keywords[i].var)) {
270 LOG(LOG_ERR, "Failure parsing line %d of %s", lm, file);
271 DEBUG(LOG_ERR, "unable to parse '%s'", orig);
272 /* reset back to the default value */
273 keywords[i].handler(keywords[i].def, keywords[i].var);
281 void write_leases(void)
286 time_t curr = time(0);
287 unsigned long tmp_time;
289 if (!(fp = fopen(server_config.lease_file, "w"))) {
290 LOG(LOG_ERR, "Unable to open %s for writing", server_config.lease_file);
294 for (i = 0; i < server_config.max_leases; i++) {
295 if (leases[i].yiaddr != 0) {
297 /* screw with the time in the struct, for easier writing */
298 tmp_time = leases[i].expires;
300 if (server_config.remaining) {
301 if (lease_expired(&(leases[i])))
302 leases[i].expires = 0;
303 else leases[i].expires -= curr;
304 } /* else stick with the time we got */
305 leases[i].expires = htonl(leases[i].expires);
306 fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp);
308 /* Then restore it when done. */
309 leases[i].expires = tmp_time;
314 if (server_config.notify_file) {
315 sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file);
321 void read_leases(const char *file)
325 struct dhcpOfferedAddr lease;
327 if (!(fp = fopen(file, "r"))) {
328 LOG(LOG_ERR, "Unable to open %s for reading", file);
332 while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) {
333 /* ADDME: is it a static lease */
334 if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
335 lease.expires = ntohl(lease.expires);
336 if (!server_config.remaining) lease.expires -= time(0);
337 if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
338 LOG(LOG_WARNING, "Too many leases while loading %s\n", file);
344 DEBUG(LOG_INFO, "Read %d leases", i);