2c2c5422c3e44dd8c071db8f4f2bc678ef8aef7c
[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 <netinet/ether.h>
8
9 #include "common.h"
10 #include "dhcpd.h"
11 #include "options.h"
12
13
14 /*
15  * Domain names may have 254 chars, and string options can be 254
16  * chars long. However, 80 bytes will be enough for most, and won't
17  * hog up memory. If you have a special application, change it
18  */
19 #define READ_CONFIG_BUF_SIZE 80
20
21 /* on these functions, make sure you datatype matches */
22 static int read_ip(const char *line, void *arg)
23 {
24         struct in_addr *addr = arg;
25         struct hostent *host;
26         int retval = 1;
27
28         if (!inet_aton(line, addr)) {
29                 host = gethostbyname(line);
30                 if (host)
31                         addr->s_addr = *((unsigned long *) host->h_addr_list[0]);
32                 else retval = 0;
33         }
34         return retval;
35 }
36
37 static int read_mac(const char *line, void *arg)
38 {
39         uint8_t *mac_bytes = arg;
40         struct ether_addr *temp_ether_addr;
41         int retval = 1;
42
43         temp_ether_addr = ether_aton(line);
44
45         if (temp_ether_addr == NULL)
46                 retval = 0;
47         else
48                 memcpy(mac_bytes, temp_ether_addr, 6);
49
50         return retval;
51 }
52
53
54 static int read_str(const char *line, void *arg)
55 {
56         char **dest = arg;
57
58         free(*dest);
59         *dest = strdup(line);
60
61         return 1;
62 }
63
64
65 static int read_u32(const char *line, void *arg)
66 {
67         return safe_strtou32(line, (uint32_t*)arg) == 0;
68 }
69
70
71 static int read_yn(const char *line, void *arg)
72 {
73         char *dest = arg;
74         int retval = 1;
75
76         if (!strcasecmp("yes", line))
77                 *dest = 1;
78         else if (!strcasecmp("no", line))
79                 *dest = 0;
80         else retval = 0;
81
82         return retval;
83 }
84
85
86 /* find option 'code' in opt_list */
87 struct option_set *find_option(struct option_set *opt_list, char code)
88 {
89         while (opt_list && opt_list->data[OPT_CODE] < code)
90                 opt_list = opt_list->next;
91
92         if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
93         else return NULL;
94 }
95
96
97 /* add an option to the opt_list */
98 static void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
99 {
100         struct option_set *existing, *new, **curr;
101
102         /* add it to an existing option */
103         if ((existing = find_option(*opt_list, option->code))) {
104                 DEBUG("Attaching option %s to existing member of list", option->name);
105                 if (option->flags & OPTION_LIST) {
106                         if (existing->data[OPT_LEN] + length <= 255) {
107                                 existing->data = realloc(existing->data,
108                                                 existing->data[OPT_LEN] + length + 2);
109                                 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
110                                 existing->data[OPT_LEN] += length;
111                         } /* else, ignore the data, we could put this in a second option in the future */
112                 } /* else, ignore the new data */
113         } else {
114                 DEBUG("Attaching option %s to list", option->name);
115
116                 /* make a new option */
117                 new = xmalloc(sizeof(struct option_set));
118                 new->data = xmalloc(length + 2);
119                 new->data[OPT_CODE] = option->code;
120                 new->data[OPT_LEN] = length;
121                 memcpy(new->data + 2, buffer, length);
122
123                 curr = opt_list;
124                 while (*curr && (*curr)->data[OPT_CODE] < option->code)
125                         curr = &(*curr)->next;
126
127                 new->next = *curr;
128                 *curr = new;
129         }
130 }
131
132
133 /* read a dhcp option and add it to opt_list */
134 static int read_opt(const char *const_line, void *arg)
135 {
136         struct option_set **opt_list = arg;
137         char *opt, *val, *endptr;
138         struct dhcp_option *option;
139         int retval = 0, length;
140         char buffer[8];
141         char *line;
142         uint16_t *result_u16 = (uint16_t *) buffer;
143         uint32_t *result_u32 = (uint32_t *) buffer;
144
145         /* Cheat, the only const line we'll actually get is "" */
146         line = (char *) const_line;
147         if (!(opt = strtok(line, " \t="))) return 0;
148
149         for (option = dhcp_options; option->code; option++)
150                 if (!strcasecmp(option->name, opt))
151                         break;
152
153         if (!option->code) return 0;
154
155         do {
156                 if (!(val = strtok(NULL, ", \t"))) break;
157                 length = option_lengths[option->flags & TYPE_MASK];
158                 retval = 0;
159                 opt = buffer; /* new meaning for variable opt */
160                 switch (option->flags & TYPE_MASK) {
161                 case OPTION_IP:
162                         retval = read_ip(val, buffer);
163                         break;
164                 case OPTION_IP_PAIR:
165                         retval = read_ip(val, buffer);
166                         if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
167                         if (retval) retval = read_ip(val, buffer + 4);
168                         break;
169                 case OPTION_STRING:
170                         length = strlen(val);
171                         if (length > 0) {
172                                 if (length > 254) length = 254;
173                                 opt = val;
174                                 retval = 1;
175                         }
176                         break;
177                 case OPTION_BOOLEAN:
178                         retval = read_yn(val, buffer);
179                         break;
180                 case OPTION_U8:
181                         buffer[0] = strtoul(val, &endptr, 0);
182                         retval = (endptr[0] == '\0');
183                         break;
184                 case OPTION_U16:
185                         *result_u16 = htons(strtoul(val, &endptr, 0));
186                         retval = (endptr[0] == '\0');
187                         break;
188                 case OPTION_S16:
189                         *result_u16 = htons(strtol(val, &endptr, 0));
190                         retval = (endptr[0] == '\0');
191                         break;
192                 case OPTION_U32:
193                         *result_u32 = htonl(strtoul(val, &endptr, 0));
194                         retval = (endptr[0] == '\0');
195                         break;
196                 case OPTION_S32:
197                         *result_u32 = htonl(strtol(val, &endptr, 0));
198                         retval = (endptr[0] == '\0');
199                         break;
200                 default:
201                         break;
202                 }
203                 if (retval)
204                         attach_option(opt_list, option, opt, length);
205         } while (retval && option->flags & OPTION_LIST);
206         return retval;
207 }
208
209 static int read_staticlease(const char *const_line, void *arg)
210 {
211         char *line;
212         char *mac_string;
213         char *ip_string;
214         uint8_t *mac_bytes;
215         uint32_t *ip;
216
217
218         /* Allocate memory for addresses */
219         mac_bytes = xmalloc(sizeof(unsigned char) * 8);
220         ip = xmalloc(sizeof(uint32_t));
221
222         /* Read mac */
223         line = (char *) const_line;
224         mac_string = strtok(line, " \t");
225         read_mac(mac_string, mac_bytes);
226
227         /* Read ip */
228         ip_string = strtok(NULL, " \t");
229         read_ip(ip_string, ip);
230
231         addStaticLease(arg, mac_bytes, ip);
232
233         if (ENABLE_FEATURE_UDHCP_DEBUG) printStaticLeases(arg);
234
235         return 1;
236 }
237
238
239 static const struct config_keyword keywords[] = {
240         /* keyword      handler   variable address              default */
241         {"start",       read_ip,  &(server_config.start),       "192.168.0.20"},
242         {"end",         read_ip,  &(server_config.end),         "192.168.0.254"},
243         {"interface",   read_str, &(server_config.interface),   "eth0"},
244         {"option",      read_opt, &(server_config.options),     ""},
245         {"opt",         read_opt, &(server_config.options),     ""},
246         {"max_leases",  read_u32, &(server_config.max_leases),  "254"},
247         {"remaining",   read_yn,  &(server_config.remaining),   "yes"},
248         {"auto_time",   read_u32, &(server_config.auto_time),   "7200"},
249         {"decline_time",read_u32, &(server_config.decline_time),"3600"},
250         {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
251         {"offer_time",  read_u32, &(server_config.offer_time),  "60"},
252         {"min_lease",   read_u32, &(server_config.min_lease),   "60"},
253         {"lease_file",  read_str, &(server_config.lease_file),  LEASES_FILE},
254         {"pidfile",     read_str, &(server_config.pidfile),     "/var/run/udhcpd.pid"},
255         {"notify_file", read_str, &(server_config.notify_file), ""},
256         {"siaddr",      read_ip,  &(server_config.siaddr),      "0.0.0.0"},
257         {"sname",       read_str, &(server_config.sname),       ""},
258         {"boot_file",   read_str, &(server_config.boot_file),   ""},
259         {"static_lease",read_staticlease, &(server_config.static_leases),       ""},
260         /*ADDME: static lease */
261         {"",            NULL,     NULL,                         ""}
262 };
263
264
265 int read_config(const char *file)
266 {
267         FILE *in;
268         char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
269         int i, lm = 0;
270
271         for (i = 0; keywords[i].keyword[0]; i++)
272                 if (keywords[i].def[0])
273                         keywords[i].handler(keywords[i].def, keywords[i].var);
274
275         in = fopen(file, "r");
276         if (!in) {
277                 bb_error_msg("cannot open config file: %s", file);
278                 return 0;
279         }
280
281         while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
282                 char debug_orig[READ_CONFIG_BUF_SIZE];
283                 char *p;
284
285                 lm++;
286                 p = strchr(buffer, '\n');
287                 if (p) *p = '\0';
288                 if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer);
289                 p = strchr(buffer, '#');
290                 if (p) *p = '\0';
291
292                 if (!(token = strtok(buffer, " \t"))) continue;
293                 if (!(line = strtok(NULL, ""))) continue;
294
295                 /* eat leading whitespace */
296                 line = skip_whitespace(line);
297                 /* eat trailing whitespace */
298                 i = strlen(line) - 1;
299                 while (i >= 0 && isspace(line[i]))
300                         line[i--] = '\0';
301
302                 for (i = 0; keywords[i].keyword[0]; i++)
303                         if (!strcasecmp(token, keywords[i].keyword))
304                                 if (!keywords[i].handler(line, keywords[i].var)) {
305                                         bb_error_msg("cannot parse line %d of %s", lm, file);
306                                         if (ENABLE_FEATURE_UDHCP_DEBUG)
307                                                 bb_error_msg("cannot parse '%s'", debug_orig);
308                                         /* reset back to the default value */
309                                         keywords[i].handler(keywords[i].def, keywords[i].var);
310                                 }
311         }
312         fclose(in);
313         return 1;
314 }
315
316
317 void write_leases(void)
318 {
319         int fp;
320         unsigned i;
321         time_t curr = time(0);
322         unsigned long tmp_time;
323
324         fp = open(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
325         if (fp < 0) {
326                 bb_error_msg("cannot open %s for writing", server_config.lease_file);
327                 return;
328         }
329
330         for (i = 0; i < server_config.max_leases; i++) {
331                 if (leases[i].yiaddr != 0) {
332
333                         /* screw with the time in the struct, for easier writing */
334                         tmp_time = leases[i].expires;
335
336                         if (server_config.remaining) {
337                                 if (lease_expired(&(leases[i])))
338                                         leases[i].expires = 0;
339                                 else leases[i].expires -= curr;
340                         } /* else stick with the time we got */
341                         leases[i].expires = htonl(leases[i].expires);
342                         // FIXME: error check??
343                         full_write(fp, &leases[i], sizeof(leases[i]));
344
345                         /* then restore it when done */
346                         leases[i].expires = tmp_time;
347                 }
348         }
349         close(fp);
350
351         if (server_config.notify_file) {
352                 char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
353                 system(cmd);
354                 free(cmd);
355         }
356 }
357
358
359 void read_leases(const char *file)
360 {
361         int fp;
362         unsigned int i = 0;
363         struct dhcpOfferedAddr lease;
364
365         fp = open(file, O_RDONLY);
366         if (fp < 0) {
367                 bb_error_msg("cannot open %s for reading", file);
368                 return;
369         }
370
371         while (i < server_config.max_leases
372          && full_read(fp, &lease, sizeof(lease)) == sizeof(lease)
373         ) {
374                 /* ADDME: is it a static lease */
375                 if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
376                         lease.expires = ntohl(lease.expires);
377                         if (!server_config.remaining) lease.expires -= time(0);
378                         if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
379                                 bb_error_msg("too many leases while loading %s", file);
380                                 break;
381                         }
382                         i++;
383                 }
384         }
385         DEBUG("Read %d leases", i);
386         close(fp);
387 }