blob: f24f01cb66254a3e40a9223333506b846de2588e [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001/*
2 * files.c -- DHCP server file manipulation *
3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
4 */
5
Russ Dill61fb4892002-10-14 21:41:28 +00006#include <sys/socket.h>
7#include <arpa/inet.h>
8#include <string.h>
9#include <stdlib.h>
10#include <time.h>
11#include <ctype.h>
Russ Dill7becf592003-12-16 01:42:18 +000012#include <netdb.h>
Russ Dill61fb4892002-10-14 21:41:28 +000013
Russ Dill61fb4892002-10-14 21:41:28 +000014#include "dhcpd.h"
15#include "files.h"
16#include "options.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000017#include "common.h"
Russ Dill61fb4892002-10-14 21:41:28 +000018
19/* on these functions, make sure you datatype matches */
Glenn L McGrath24833432003-06-10 17:22:49 +000020static int read_ip(const char *line, void *arg)
Russ Dill61fb4892002-10-14 21:41:28 +000021{
22 struct in_addr *addr = arg;
23 struct hostent *host;
24 int retval = 1;
25
26 if (!inet_aton(line, addr)) {
27 if ((host = gethostbyname(line)))
28 addr->s_addr = *((unsigned long *) host->h_addr_list[0]);
29 else retval = 0;
30 }
31 return retval;
32}
33
34
Glenn L McGrath24833432003-06-10 17:22:49 +000035static int read_str(const char *line, void *arg)
Russ Dill61fb4892002-10-14 21:41:28 +000036{
37 char **dest = arg;
38
Russ Dill1eb7a172002-12-11 21:12:45 +000039 if (*dest) free(*dest);
Russ Dill61fb4892002-10-14 21:41:28 +000040 *dest = strdup(line);
41
42 return 1;
43}
44
45
Glenn L McGrath24833432003-06-10 17:22:49 +000046static int read_u32(const char *line, void *arg)
Russ Dill61fb4892002-10-14 21:41:28 +000047{
48 u_int32_t *dest = arg;
49 char *endptr;
50 *dest = strtoul(line, &endptr, 0);
51 return endptr[0] == '\0';
52}
53
54
Glenn L McGrath24833432003-06-10 17:22:49 +000055static int read_yn(const char *line, void *arg)
Russ Dill61fb4892002-10-14 21:41:28 +000056{
57 char *dest = arg;
58 int retval = 1;
59
60 if (!strcasecmp("yes", line))
61 *dest = 1;
62 else if (!strcasecmp("no", line))
63 *dest = 0;
64 else retval = 0;
65
66 return retval;
67}
68
Glenn L McGrath24833432003-06-10 17:22:49 +000069#define READ_CONFIG_BUF_SIZE 512 /* domainname may have 254 chars */
Russ Dill61fb4892002-10-14 21:41:28 +000070
71/* read a dhcp option and add it to opt_list */
Glenn L McGrath24833432003-06-10 17:22:49 +000072static int read_opt(const char *const_line, void *arg)
Russ Dill61fb4892002-10-14 21:41:28 +000073{
Russ Dille3049562003-12-16 22:46:33 +000074 char line[READ_CONFIG_BUF_SIZE];
Russ Dill61fb4892002-10-14 21:41:28 +000075 struct option_set **opt_list = arg;
76 char *opt, *val, *endptr;
Russ Dille3049562003-12-16 22:46:33 +000077 struct dhcp_option *option;
78 int retval = 0, length;
79 char buffer[256]; /* max opt length */
Russ Dill61fb4892002-10-14 21:41:28 +000080 u_int16_t result_u16;
81 u_int32_t result_u32;
Russ Dille3049562003-12-16 22:46:33 +000082 void *valptr;
Russ Dill61fb4892002-10-14 21:41:28 +000083
Russ Dille3049562003-12-16 22:46:33 +000084 if (!(opt = strtok(strcpy(line, const_line), " \t="))) return 0;
Russ Dill61fb4892002-10-14 21:41:28 +000085
Russ Dill4a9e34c2003-12-15 22:09:36 +000086 for (option = dhcp_options; option->code; option++)
Glenn L McGrath24833432003-06-10 17:22:49 +000087 if (!strcasecmp(option->name, opt))
88 break;
Russ Dill61fb4892002-10-14 21:41:28 +000089
Russ Dille3049562003-12-16 22:46:33 +000090 if (!option->code) return 0;
91
92 do {
Russ Dill61fb4892002-10-14 21:41:28 +000093 val = strtok(NULL, ", \t");
Glenn L McGrath24833432003-06-10 17:22:49 +000094 if(!val)
95 break;
Russ Dille3049562003-12-16 22:46:33 +000096 length = option_lengths[option->flags & TYPE_MASK];
Glenn L McGrath24833432003-06-10 17:22:49 +000097 valptr = NULL;
Russ Dille3049562003-12-16 22:46:33 +000098 switch (option->flags & TYPE_MASK) {
99 case OPTION_IP:
100 retval = read_ip(val, buffer);
101 break;
102 case OPTION_IP_PAIR:
103 retval = read_ip(val, buffer);
104 if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
105 if (retval) retval = read_ip(val, buffer + 4);
106 break;
107 case OPTION_STRING:
108 length = strlen(val);
109 if (length > 0) {
110 if (length > 254) length = 254;
Glenn L McGrath24833432003-06-10 17:22:49 +0000111 endptr = buffer + length;
112 endptr[0] = 0;
113 valptr = val;
Russ Dill61fb4892002-10-14 21:41:28 +0000114 }
Russ Dille3049562003-12-16 22:46:33 +0000115 break;
116 case OPTION_BOOLEAN:
117 retval = read_yn(val, buffer);
118 break;
119 case OPTION_U8:
120 buffer[0] = strtoul(val, &endptr, 0);
121 valptr = buffer;
122 break;
123 case OPTION_U16:
124 result_u16 = htons(strtoul(val, &endptr, 0));
125 valptr = &result_u16;
126 break;
127 case OPTION_S16:
128 result_u16 = htons(strtol(val, &endptr, 0));
129 valptr = &result_u16;
130 break;
131 case OPTION_U32:
132 result_u32 = htonl(strtoul(val, &endptr, 0));
133 valptr = &result_u32;
134 break;
135 case OPTION_S32:
136 result_u32 = htonl(strtol(val, &endptr, 0));
137 valptr = &result_u32;
138 break;
139 default:
140 retval = 0;
141 break;
142 }
143 if (valptr) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000144 memcpy(buffer, valptr, length);
145 retval = (endptr[0] == '\0');
146 }
Russ Dille3049562003-12-16 22:46:33 +0000147 if (retval)
148 attach_option(opt_list, option, buffer, length);
Glenn L McGrath24833432003-06-10 17:22:49 +0000149 else
150 break;
151 } while (option->flags & OPTION_LIST);
Russ Dill61fb4892002-10-14 21:41:28 +0000152 return retval;
153}
154
155
Glenn L McGrath24833432003-06-10 17:22:49 +0000156static const struct config_keyword keywords[] = {
157 /* keyword handler variable address default */
Russ Dill61fb4892002-10-14 21:41:28 +0000158 {"start", read_ip, &(server_config.start), "192.168.0.20"},
159 {"end", read_ip, &(server_config.end), "192.168.0.254"},
160 {"interface", read_str, &(server_config.interface), "eth0"},
161 {"option", read_opt, &(server_config.options), ""},
162 {"opt", read_opt, &(server_config.options), ""},
163 {"max_leases", read_u32, &(server_config.max_leases), "254"},
164 {"remaining", read_yn, &(server_config.remaining), "yes"},
165 {"auto_time", read_u32, &(server_config.auto_time), "7200"},
166 {"decline_time",read_u32, &(server_config.decline_time),"3600"},
167 {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
168 {"offer_time", read_u32, &(server_config.offer_time), "60"},
169 {"min_lease", read_u32, &(server_config.min_lease), "60"},
Russ Dill8b384512003-12-16 02:30:53 +0000170 {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
Russ Dill61fb4892002-10-14 21:41:28 +0000171 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
172 {"notify_file", read_str, &(server_config.notify_file), ""},
173 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
174 {"sname", read_str, &(server_config.sname), ""},
175 {"boot_file", read_str, &(server_config.boot_file), ""},
176 /*ADDME: static lease */
177 {"", NULL, NULL, ""}
178};
179
180
Glenn L McGrath24833432003-06-10 17:22:49 +0000181int read_config(const char *file)
Russ Dill61fb4892002-10-14 21:41:28 +0000182{
183 FILE *in;
Glenn L McGrath24833432003-06-10 17:22:49 +0000184 char buffer[READ_CONFIG_BUF_SIZE], orig[READ_CONFIG_BUF_SIZE];
185 char *token, *line;
Russ Dill61fb4892002-10-14 21:41:28 +0000186 int i;
187
Glenn L McGrath24833432003-06-10 17:22:49 +0000188 for (i = 0; keywords[i].keyword[0]; i++)
189 if (keywords[i].def[0])
Russ Dill61fb4892002-10-14 21:41:28 +0000190 keywords[i].handler(keywords[i].def, keywords[i].var);
191
192 if (!(in = fopen(file, "r"))) {
193 LOG(LOG_ERR, "unable to open config file: %s", file);
194 return 0;
195 }
196
Glenn L McGrath24833432003-06-10 17:22:49 +0000197 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
Russ Dill61fb4892002-10-14 21:41:28 +0000198 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
Glenn L McGrath24833432003-06-10 17:22:49 +0000199 strcpy(orig, buffer);
Russ Dill61fb4892002-10-14 21:41:28 +0000200 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
Glenn L McGrath24833432003-06-10 17:22:49 +0000201 token = strtok(buffer, " \t");
202 if(!token)
203 continue;
204 line = strtok(NULL, "");
205 if(!line)
206 continue;
207 while(*line == '=' || isspace(*line))
Russ Dill61fb4892002-10-14 21:41:28 +0000208 line++;
Russ Dill61fb4892002-10-14 21:41:28 +0000209 /* eat trailing whitespace */
210 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
211 line[i] = '\0';
Glenn L McGrath24833432003-06-10 17:22:49 +0000212 if (*line == '\0')
213 continue;
Russ Dill61fb4892002-10-14 21:41:28 +0000214
Glenn L McGrath24833432003-06-10 17:22:49 +0000215 for (i = 0; keywords[i].keyword[0]; i++)
Russ Dill61fb4892002-10-14 21:41:28 +0000216 if (!strcasecmp(token, keywords[i].keyword))
217 if (!keywords[i].handler(line, keywords[i].var)) {
218 LOG(LOG_ERR, "unable to parse '%s'", orig);
219 /* reset back to the default value */
220 keywords[i].handler(keywords[i].def, keywords[i].var);
221 }
222 }
223 fclose(in);
224 return 1;
225}
226
227
228void write_leases(void)
229{
230 FILE *fp;
231 unsigned int i;
232 char buf[255];
233 time_t curr = time(0);
Russ Dill8f431262003-12-16 01:29:40 +0000234 unsigned long tmp_time;
Russ Dill61fb4892002-10-14 21:41:28 +0000235
236 if (!(fp = fopen(server_config.lease_file, "w"))) {
237 LOG(LOG_ERR, "Unable to open %s for writing", server_config.lease_file);
238 return;
239 }
240
241 for (i = 0; i < server_config.max_leases; i++) {
242 if (leases[i].yiaddr != 0) {
Russ Dill8f431262003-12-16 01:29:40 +0000243
244 /* screw with the time in the struct, for easier writing */
245 tmp_time = leases[i].expires;
246
Russ Dill61fb4892002-10-14 21:41:28 +0000247 if (server_config.remaining) {
248 if (lease_expired(&(leases[i])))
Russ Dill8f431262003-12-16 01:29:40 +0000249 leases[i].expires = 0;
250 else leases[i].expires -= curr;
251 } /* else stick with the time we got */
252 leases[i].expires = htonl(leases[i].expires);
Russ Dilla3170d52003-12-16 01:33:38 +0000253 fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp);
Russ Dill8f431262003-12-16 01:29:40 +0000254
255 /* Then restore it when done. */
256 leases[i].expires = tmp_time;
Russ Dill61fb4892002-10-14 21:41:28 +0000257 }
258 }
259 fclose(fp);
260
261 if (server_config.notify_file) {
262 sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file);
263 system(buf);
264 }
265}
266
267
Glenn L McGrath24833432003-06-10 17:22:49 +0000268void read_leases(const char *file)
Russ Dill61fb4892002-10-14 21:41:28 +0000269{
270 FILE *fp;
271 unsigned int i = 0;
272 struct dhcpOfferedAddr lease;
273
274 if (!(fp = fopen(file, "r"))) {
275 LOG(LOG_ERR, "Unable to open %s for reading", file);
276 return;
277 }
278
279 while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) {
280 /* ADDME: is it a static lease */
281 if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
282 lease.expires = ntohl(lease.expires);
283 if (!server_config.remaining) lease.expires -= time(0);
284 if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
285 LOG(LOG_WARNING, "Too many leases while loading %s\n", file);
286 break;
287 }
288 i++;
289 }
290 }
291 DEBUG(LOG_INFO, "Read %d leases", i);
292 fclose(fp);
293}