Standardize on the vi editing directives being on the first line.
[oweals/busybox.git] / networking / udhcp / files.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * files.c -- DHCP server file manipulation *
4  * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
5  */
6
7 #include <sys/socket.h>
8 #include <arpa/inet.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <time.h>
12 #include <ctype.h>
13 #include <netdb.h>
14
15 #include <netinet/ether.h>
16 #include "static_leases.h"
17
18 #include "dhcpd.h"
19 #include "options.h"
20 #include "files.h"
21 #include "common.h"
22
23 /*
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
27  */
28 #define READ_CONFIG_BUF_SIZE 80
29
30 /* on these functions, make sure you datatype matches */
31 static int read_ip(const char *line, void *arg)
32 {
33         struct in_addr *addr = arg;
34         struct hostent *host;
35         int retval = 1;
36
37         if (!inet_aton(line, addr)) {
38                 if ((host = gethostbyname(line)))
39                         addr->s_addr = *((unsigned long *) host->h_addr_list[0]);
40                 else retval = 0;
41         }
42         return retval;
43 }
44
45 static int read_mac(const char *line, void *arg)
46 {
47         uint8_t *mac_bytes = arg;
48         struct ether_addr *temp_ether_addr;
49         int retval = 1;
50
51         temp_ether_addr = ether_aton(line);
52
53         if(temp_ether_addr == NULL)
54                 retval = 0;
55         else
56                 memcpy(mac_bytes, temp_ether_addr, 6);
57
58         return retval;
59 }
60
61
62 static int read_str(const char *line, void *arg)
63 {
64         char **dest = arg;
65
66         free(*dest);
67         *dest = strdup(line);
68
69         return 1;
70 }
71
72
73 static int read_u32(const char *line, void *arg)
74 {
75         uint32_t *dest = arg;
76         char *endptr;
77         *dest = strtoul(line, &endptr, 0);
78         return endptr[0] == '\0';
79 }
80
81
82 static int read_yn(const char *line, void *arg)
83 {
84         char *dest = arg;
85         int retval = 1;
86
87         if (!strcasecmp("yes", line))
88                 *dest = 1;
89         else if (!strcasecmp("no", line))
90                 *dest = 0;
91         else retval = 0;
92
93         return retval;
94 }
95
96
97 /* find option 'code' in opt_list */
98 struct option_set *find_option(struct option_set *opt_list, char code)
99 {
100         while (opt_list && opt_list->data[OPT_CODE] < code)
101                 opt_list = opt_list->next;
102
103         if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
104         else return NULL;
105 }
106
107
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)
110 {
111         struct option_set *existing, *new, **curr;
112
113         /* add it to an existing option */
114         if ((existing = find_option(*opt_list, option->code))) {
115                 DEBUG(LOG_INFO, "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 */
124         } else {
125                 DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
126
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);
133
134                 curr = opt_list;
135                 while (*curr && (*curr)->data[OPT_CODE] < option->code)
136                         curr = &(*curr)->next;
137
138                 new->next = *curr;
139                 *curr = new;
140         }
141 }
142
143
144 /* read a dhcp option and add it to opt_list */
145 static int read_opt(const char *const_line, void *arg)
146 {
147         struct option_set **opt_list = arg;
148         char *opt, *val, *endptr;
149         struct dhcp_option *option;
150         int retval = 0, length;
151         char buffer[8];
152         char *line;
153         uint16_t *result_u16 = (uint16_t *) buffer;
154         uint32_t *result_u32 = (uint32_t *) buffer;
155
156         /* Cheat, the only const line we'll actually get is "" */
157         line = (char *) const_line;
158         if (!(opt = strtok(line, " \t="))) return 0;
159
160         for (option = dhcp_options; option->code; option++)
161                 if (!strcasecmp(option->name, opt))
162                         break;
163
164         if (!option->code) return 0;
165
166         do {
167                 if (!(val = strtok(NULL, ", \t"))) break;
168                 length = option_lengths[option->flags & TYPE_MASK];
169                 retval = 0;
170                 opt = buffer; /* new meaning for variable opt */
171                 switch (option->flags & TYPE_MASK) {
172                 case OPTION_IP:
173                         retval = read_ip(val, buffer);
174                         break;
175                 case OPTION_IP_PAIR:
176                         retval = read_ip(val, buffer);
177                         if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
178                         if (retval) retval = read_ip(val, buffer + 4);
179                         break;
180                 case OPTION_STRING:
181                         length = strlen(val);
182                         if (length > 0) {
183                                 if (length > 254) length = 254;
184                                 opt = val;
185                                 retval = 1;
186                         }
187                         break;
188                 case OPTION_BOOLEAN:
189                         retval = read_yn(val, buffer);
190                         break;
191                 case OPTION_U8:
192                         buffer[0] = strtoul(val, &endptr, 0);
193                         retval = (endptr[0] == '\0');
194                         break;
195                 case OPTION_U16:
196                         *result_u16 = htons(strtoul(val, &endptr, 0));
197                         retval = (endptr[0] == '\0');
198                         break;
199                 case OPTION_S16:
200                         *result_u16 = htons(strtol(val, &endptr, 0));
201                         retval = (endptr[0] == '\0');
202                         break;
203                 case OPTION_U32:
204                         *result_u32 = htonl(strtoul(val, &endptr, 0));
205                         retval = (endptr[0] == '\0');
206                         break;
207                 case OPTION_S32:
208                         *result_u32 = htonl(strtol(val, &endptr, 0));
209                         retval = (endptr[0] == '\0');
210                         break;
211                 default:
212                         break;
213                 }
214                 if (retval)
215                         attach_option(opt_list, option, opt, length);
216         } while (retval && option->flags & OPTION_LIST);
217         return retval;
218 }
219
220 static int read_staticlease(const char *const_line, void *arg)
221 {
222
223         char *line;
224         char *mac_string;
225         char *ip_string;
226         uint8_t *mac_bytes;
227         uint32_t *ip;
228
229
230         /* Allocate memory for addresses */
231         mac_bytes = xmalloc(sizeof(unsigned char) * 8);
232         ip = xmalloc(sizeof(uint32_t));
233
234         /* Read mac */
235         line = (char *) const_line;
236         mac_string = strtok(line, " \t");
237         read_mac(mac_string, mac_bytes);
238
239         /* Read ip */
240         ip_string = strtok(NULL, " \t");
241         read_ip(ip_string, ip);
242
243         addStaticLease(arg, mac_bytes, ip);
244
245         if (ENABLE_FEATURE_UDHCP_DEBUG) printStaticLeases(arg);
246
247         return 1;
248
249 }
250
251
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 */
274         {"",            NULL,     NULL,                         ""}
275 };
276
277
278 int read_config(const char *file)
279 {
280         FILE *in;
281         char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
282         int i, lm = 0;
283
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);
287
288         if (!(in = fopen(file, "r"))) {
289                 LOG(LOG_ERR, "unable to open config file: %s", file);
290                 return 0;
291         }
292
293         while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
294                 char debug_orig[READ_CONFIG_BUF_SIZE];
295
296                 lm++;
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';
300
301                 if (!(token = strtok(buffer, " \t"))) continue;
302                 if (!(line = strtok(NULL, ""))) continue;
303
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--);
308                 line[i] = '\0';
309
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                                         LOG(LOG_ERR, "Failure parsing line %d of %s", lm, file);
314                                         DEBUG(LOG_ERR, "unable to parse '%s'", debug_orig);
315                                         /* reset back to the default value */
316                                         keywords[i].handler(keywords[i].def, keywords[i].var);
317                                 }
318         }
319         fclose(in);
320         return 1;
321 }
322
323
324 void write_leases(void)
325 {
326         FILE *fp;
327         unsigned int i;
328         char buf[255];
329         time_t curr = time(0);
330         unsigned long tmp_time;
331
332         if (!(fp = fopen(server_config.lease_file, "w"))) {
333                 LOG(LOG_ERR, "Unable to open %s for writing", server_config.lease_file);
334                 return;
335         }
336
337         for (i = 0; i < server_config.max_leases; i++) {
338                 if (leases[i].yiaddr != 0) {
339
340                         /* screw with the time in the struct, for easier writing */
341                         tmp_time = leases[i].expires;
342
343                         if (server_config.remaining) {
344                                 if (lease_expired(&(leases[i])))
345                                         leases[i].expires = 0;
346                                 else leases[i].expires -= curr;
347                         } /* else stick with the time we got */
348                         leases[i].expires = htonl(leases[i].expires);
349                         fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp);
350
351                         /* Then restore it when done. */
352                         leases[i].expires = tmp_time;
353                 }
354         }
355         fclose(fp);
356
357         if (server_config.notify_file) {
358                 sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file);
359                 system(buf);
360         }
361 }
362
363
364 void read_leases(const char *file)
365 {
366         FILE *fp;
367         unsigned int i = 0;
368         struct dhcpOfferedAddr lease;
369
370         if (!(fp = fopen(file, "r"))) {
371                 LOG(LOG_ERR, "Unable to open %s for reading", file);
372                 return;
373         }
374
375         while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) {
376                 /* ADDME: is it a static lease */
377                 if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
378                         lease.expires = ntohl(lease.expires);
379                         if (!server_config.remaining) lease.expires -= time(0);
380                         if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
381                                 LOG(LOG_WARNING, "Too many leases while loading %s\n", file);
382                                 break;
383                         }
384                         i++;
385                 }
386         }
387         DEBUG(LOG_INFO, "Read %d leases", i);
388         fclose(fp);
389 }