2 * files.c -- DHCP server file manipulation *
3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
6 #include <sys/socket.h>
18 /* on these functions, make sure you datatype matches */
19 static int read_ip(const char *line, void *arg)
21 struct in_addr *addr = arg;
25 if (!inet_aton(line, addr)) {
26 if ((host = gethostbyname(line)))
27 addr->s_addr = *((unsigned long *) host->h_addr_list[0]);
34 static int read_str(const char *line, void *arg)
38 if (*dest) free(*dest);
45 static int read_u32(const char *line, void *arg)
47 u_int32_t *dest = arg;
49 *dest = strtoul(line, &endptr, 0);
50 return endptr[0] == '\0';
54 static int read_yn(const char *line, void *arg)
59 if (!strcasecmp("yes", line))
61 else if (!strcasecmp("no", line))
68 #define READ_CONFIG_BUF_SIZE 512 /* domainname may have 254 chars */
70 /* read a dhcp option and add it to opt_list */
71 static int read_opt(const char *const_line, void *arg)
73 char line[READ_CONFIG_BUF_SIZE];
74 struct option_set **opt_list = arg;
75 char *opt, *val, *endptr;
76 struct dhcp_option *option;
77 int retval = 0, length;
78 char buffer[256]; /* max opt length */
83 if ((opt = strtok(strcpy(line, const_line), " \t="))) {
85 for (option = dhcp_options; option->code; option++)
86 if (!strcasecmp(option->name, opt))
89 if (option->code) do {
90 val = strtok(NULL, ", \t");
93 length = option_lengths[option->flags & TYPE_MASK];
95 switch (option->flags & TYPE_MASK) {
97 retval = read_ip(val, buffer);
100 retval = read_ip(val, buffer);
101 if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
102 if (retval) retval = read_ip(val, buffer + 4);
105 length = strlen(val);
107 if (length > 254) length = 254;
108 endptr = buffer + length;
114 retval = read_yn(val, buffer);
117 buffer[0] = strtoul(val, &endptr, 0);
121 result_u16 = htons(strtoul(val, &endptr, 0));
122 valptr = &result_u16;
125 result_u16 = htons(strtol(val, &endptr, 0));
126 valptr = &result_u16;
129 result_u32 = htonl(strtoul(val, &endptr, 0));
130 valptr = &result_u32;
133 result_u32 = htonl(strtol(val, &endptr, 0));
134 valptr = &result_u32;
141 memcpy(buffer, valptr, length);
142 retval = (endptr[0] == '\0');
145 attach_option(opt_list, option, buffer, length);
148 } while (option->flags & OPTION_LIST);
154 static const struct config_keyword keywords[] = {
155 /* keyword handler variable address default */
156 {"start", read_ip, &(server_config.start), "192.168.0.20"},
157 {"end", read_ip, &(server_config.end), "192.168.0.254"},
158 {"interface", read_str, &(server_config.interface), "eth0"},
159 {"option", read_opt, &(server_config.options), ""},
160 {"opt", read_opt, &(server_config.options), ""},
161 {"max_leases", read_u32, &(server_config.max_leases), "254"},
162 {"remaining", read_yn, &(server_config.remaining), "yes"},
163 {"auto_time", read_u32, &(server_config.auto_time), "7200"},
164 {"decline_time",read_u32, &(server_config.decline_time),"3600"},
165 {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
166 {"offer_time", read_u32, &(server_config.offer_time), "60"},
167 {"min_lease", read_u32, &(server_config.min_lease), "60"},
168 {"lease_file", read_str, &(server_config.lease_file), leases_file},
169 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
170 {"notify_file", read_str, &(server_config.notify_file), ""},
171 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
172 {"sname", read_str, &(server_config.sname), ""},
173 {"boot_file", read_str, &(server_config.boot_file), ""},
174 /*ADDME: static lease */
179 int read_config(const char *file)
182 char buffer[READ_CONFIG_BUF_SIZE], orig[READ_CONFIG_BUF_SIZE];
186 for (i = 0; keywords[i].keyword[0]; i++)
187 if (keywords[i].def[0])
188 keywords[i].handler(keywords[i].def, keywords[i].var);
190 if (!(in = fopen(file, "r"))) {
191 LOG(LOG_ERR, "unable to open config file: %s", file);
195 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
196 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
197 strcpy(orig, buffer);
198 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
199 token = strtok(buffer, " \t");
202 line = strtok(NULL, "");
205 while(*line == '=' || isspace(*line))
207 /* eat trailing whitespace */
208 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
213 for (i = 0; keywords[i].keyword[0]; i++)
214 if (!strcasecmp(token, keywords[i].keyword))
215 if (!keywords[i].handler(line, keywords[i].var)) {
216 LOG(LOG_ERR, "unable to parse '%s'", orig);
217 /* reset back to the default value */
218 keywords[i].handler(keywords[i].def, keywords[i].var);
226 void write_leases(void)
231 time_t curr = time(0);
232 unsigned long tmp_time;
234 if (!(fp = fopen(server_config.lease_file, "w"))) {
235 LOG(LOG_ERR, "Unable to open %s for writing", server_config.lease_file);
239 for (i = 0; i < server_config.max_leases; i++) {
240 if (leases[i].yiaddr != 0) {
242 /* screw with the time in the struct, for easier writing */
243 tmp_time = leases[i].expires;
245 if (server_config.remaining) {
246 if (lease_expired(&(leases[i])))
247 leases[i].expires = 0;
248 else leases[i].expires -= curr;
249 } /* else stick with the time we got */
250 leases[i].expires = htonl(leases[i].expires);
251 fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp);
253 /* Then restore it when done. */
254 leases[i].expires = tmp_time;
259 if (server_config.notify_file) {
260 sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file);
266 void read_leases(const char *file)
270 struct dhcpOfferedAddr lease;
272 if (!(fp = fopen(file, "r"))) {
273 LOG(LOG_ERR, "Unable to open %s for reading", file);
277 while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) {
278 /* ADDME: is it a static lease */
279 if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
280 lease.expires = ntohl(lease.expires);
281 if (!server_config.remaining) lease.expires -= time(0);
282 if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
283 LOG(LOG_WARNING, "Too many leases while loading %s\n", file);
289 DEBUG(LOG_INFO, "Read %d leases", i);