when compiled standalone, udhcp needs these headers
[oweals/busybox.git] / networking / udhcp / files.c
1 /* 
2  * files.c -- DHCP server file manipulation *
3  * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
4  */
5  
6 #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>
12 #include <netdb.h>
13
14 #include "debug.h"
15 #include "dhcpd.h"
16 #include "files.h"
17 #include "options.h"
18 #include "common.h"
19
20 /* on these functions, make sure you datatype matches */
21 static int read_ip(const char *line, void *arg)
22 {
23         struct in_addr *addr = arg;
24         struct hostent *host;
25         int retval = 1;
26
27         if (!inet_aton(line, addr)) {
28                 if ((host = gethostbyname(line))) 
29                         addr->s_addr = *((unsigned long *) host->h_addr_list[0]);
30                 else retval = 0;
31         }
32         return retval;
33 }
34
35
36 static int read_str(const char *line, void *arg)
37 {
38         char **dest = arg;
39         
40         if (*dest) free(*dest);
41         *dest = strdup(line);
42         
43         return 1;
44 }
45
46
47 static int read_u32(const char *line, void *arg)
48 {
49         u_int32_t *dest = arg;
50         char *endptr;
51         *dest = strtoul(line, &endptr, 0);
52         return endptr[0] == '\0';
53 }
54
55
56 static int read_yn(const char *line, void *arg)
57 {
58         char *dest = arg;
59         int retval = 1;
60
61         if (!strcasecmp("yes", line))
62                 *dest = 1;
63         else if (!strcasecmp("no", line))
64                 *dest = 0;
65         else retval = 0;
66         
67         return retval;
68 }
69
70 #define READ_CONFIG_BUF_SIZE 512        /* domainname may have 254 chars */
71
72 /* read a dhcp option and add it to opt_list */
73 static int read_opt(const char *const_line, void *arg)
74 {
75     char line[READ_CONFIG_BUF_SIZE];
76         struct option_set **opt_list = arg;
77         char *opt, *val, *endptr;
78     struct dhcp_option *option;
79     int retval = 0, length;
80     char buffer[256];                       /* max opt length */
81         u_int16_t result_u16;
82         u_int32_t result_u32;
83     void *valptr;
84         
85     if ((opt = strtok(strcpy(line, const_line), " \t="))) {
86                 
87         for (option = dhcp_options; option->code; option++)
88                 if (!strcasecmp(option->name, opt))
89                         break;
90         
91         if (option->code) do {
92                 val = strtok(NULL, ", \t");
93                 if(!val)
94                         break;
95                         length = option_lengths[option->flags & TYPE_MASK];
96                 valptr = NULL;
97                         switch (option->flags & TYPE_MASK) {
98                         case OPTION_IP:
99                                 retval = read_ip(val, buffer);
100                                 break;
101                         case OPTION_IP_PAIR:
102                                 retval = read_ip(val, buffer);
103                                 if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
104                                 if (retval) retval = read_ip(val, buffer + 4);
105                                 break;
106                         case OPTION_STRING:
107                                 length = strlen(val);
108                                 if (length > 0) {
109                                         if (length > 254) length = 254;
110                                 endptr = buffer + length;
111                                 endptr[0] = 0;
112                                 valptr = val;
113                                 }
114                                 break;
115                         case OPTION_BOOLEAN:
116                                 retval = read_yn(val, buffer);
117                                 break;
118                         case OPTION_U8:
119                                 buffer[0] = strtoul(val, &endptr, 0);
120                         valptr = buffer;
121                                 break;
122                         case OPTION_U16:
123                                 result_u16 = htons(strtoul(val, &endptr, 0));
124                         valptr = &result_u16;
125                                 break;
126                         case OPTION_S16:
127                                 result_u16 = htons(strtol(val, &endptr, 0));
128                         valptr = &result_u16;
129                                 break;
130                         case OPTION_U32:
131                                 result_u32 = htonl(strtoul(val, &endptr, 0));
132                         valptr = &result_u32;
133                                 break;
134                         case OPTION_S32:
135                                 result_u32 = htonl(strtol(val, &endptr, 0));    
136                         valptr = &result_u32;
137                                 break;
138                         default:
139                         retval = 0;
140                                 break;
141                         }
142                 if(valptr) {
143                         memcpy(buffer, valptr, length);
144                         retval = (endptr[0] == '\0');
145                 }
146                         if (retval) 
147                                 attach_option(opt_list, option, buffer, length);
148                 else
149                         break;
150         } while (option->flags & OPTION_LIST);
151     }
152         return retval;
153 }
154
155
156 static const struct config_keyword keywords[] = {
157         /* keyword      handler   variable address              default     */
158         {"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"},
170         {"lease_file",  read_str, &(server_config.lease_file),  leases_file},
171         {"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
181 int read_config(const char *file)
182 {
183         FILE *in;
184         char buffer[READ_CONFIG_BUF_SIZE], orig[READ_CONFIG_BUF_SIZE];
185         char *token, *line;
186         int i;
187
188         for (i = 0; keywords[i].keyword[0]; i++)
189                 if (keywords[i].def[0])
190                         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         
197         while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
198                 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
199                 strcpy(orig, buffer);
200                 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
201                 token = strtok(buffer, " \t");
202                 if(!token)
203                         continue;
204                 line = strtok(NULL, "");
205                 if(!line)
206                         continue;
207                 while(*line == '=' || isspace(*line))
208                 line++;
209                 /* eat trailing whitespace */
210                 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
211                 line[i] = '\0';
212                 if (*line == '\0')
213                         continue;
214                 
215                 for (i = 0; keywords[i].keyword[0]; i++)
216                         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
228 void write_leases(void)
229 {
230         FILE *fp;
231         unsigned int i;
232         char buf[255];
233         time_t curr = time(0);
234         unsigned long tmp_time;
235         
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) {
243
244                         /* screw with the time in the struct, for easier writing */
245                         tmp_time = leases[i].expires;
246
247                         if (server_config.remaining) {
248                                 if (lease_expired(&(leases[i])))
249                                         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);
253                         fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp);
254
255                         /* Then restore it when done. */
256                         leases[i].expires = tmp_time;
257                 }
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
268 void read_leases(const char *file)
269 {
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 }