udhcp: pass pointer to whole packet to "add option" functions
[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  * Licensed under GPLv2, see file LICENSE in this tarball for details.
7  */
8
9 #include <netinet/ether.h>
10
11 #include "common.h"
12 #include "dhcpd.h"
13
14 #if BB_LITTLE_ENDIAN
15 static inline uint64_t hton64(uint64_t v)
16 {
17         return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32);
18 }
19 #else
20 #define hton64(v) (v)
21 #endif
22 #define ntoh64(v) hton64(v)
23
24 /* on these functions, make sure your datatype matches */
25 static int FAST_FUNC read_str(const char *line, void *arg)
26 {
27         char **dest = arg;
28
29         free(*dest);
30         *dest = xstrdup(line);
31         return 1;
32 }
33
34 static int FAST_FUNC read_u32(const char *line, void *arg)
35 {
36         *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
37         return errno == 0;
38 }
39
40 static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
41 {
42         char *line;
43         char *mac_string;
44         char *ip_string;
45         struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */
46         uint32_t nip;
47
48         /* Read mac */
49         line = (char *) const_line;
50         mac_string = strtok_r(line, " \t", &line);
51         if (!mac_string || !ether_aton_r(mac_string, &mac_bytes))
52                 return 0;
53
54         /* Read ip */
55         ip_string = strtok_r(NULL, " \t", &line);
56         if (!ip_string || !udhcp_str2nip(ip_string, &nip))
57                 return 0;
58
59         add_static_lease(arg, (uint8_t*) &mac_bytes, nip);
60
61         log_static_leases(arg);
62
63         return 1;
64 }
65
66
67 struct config_keyword {
68         const char *keyword;
69         int (*handler)(const char *line, void *var) FAST_FUNC;
70         void *var;
71         const char *def;
72 };
73
74 static const struct config_keyword keywords[] = {
75         /* keyword       handler   variable address               default */
76         {"start",        udhcp_str2nip, &(server_config.start_ip),     "192.168.0.20"},
77         {"end",          udhcp_str2nip, &(server_config.end_ip),       "192.168.0.254"},
78         {"interface",    read_str, &(server_config.interface),    "eth0"},
79         /* Avoid "max_leases value not sane" warning by setting default
80          * to default_end_ip - default_start_ip + 1: */
81         {"max_leases",   read_u32, &(server_config.max_leases),   "235"},
82         {"auto_time",    read_u32, &(server_config.auto_time),    "7200"},
83         {"decline_time", read_u32, &(server_config.decline_time), "3600"},
84         {"conflict_time",read_u32, &(server_config.conflict_time),"3600"},
85         {"offer_time",   read_u32, &(server_config.offer_time),   "60"},
86         {"min_lease",    read_u32, &(server_config.min_lease_sec),"60"},
87         {"lease_file",   read_str, &(server_config.lease_file),   LEASES_FILE},
88         {"pidfile",      read_str, &(server_config.pidfile),      "/var/run/udhcpd.pid"},
89         {"siaddr",       udhcp_str2nip, &(server_config.siaddr_nip),   "0.0.0.0"},
90         /* keywords with no defaults must be last! */
91         {"option",       udhcp_str2optset, &(server_config.options), ""},
92         {"opt",          udhcp_str2optset, &(server_config.options), ""},
93         {"notify_file",  read_str, &(server_config.notify_file),  ""},
94         {"sname",        read_str, &(server_config.sname),        ""},
95         {"boot_file",    read_str, &(server_config.boot_file),    ""},
96         {"static_lease", read_staticlease, &(server_config.static_leases), ""},
97 };
98 enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
99
100 void FAST_FUNC read_config(const char *file)
101 {
102         parser_t *parser;
103         const struct config_keyword *k;
104         unsigned i;
105         char *token[2];
106
107         for (i = 0; i < KWS_WITH_DEFAULTS; i++)
108                 keywords[i].handler(keywords[i].def, keywords[i].var);
109
110         parser = config_open(file);
111         while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
112                 for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
113                         if (strcasecmp(token[0], k->keyword) == 0) {
114                                 if (!k->handler(token[1], k->var)) {
115                                         bb_error_msg("can't parse line %u in %s",
116                                                         parser->lineno, file);
117                                         /* reset back to the default value */
118                                         k->handler(k->def, k->var);
119                                 }
120                                 break;
121                         }
122                 }
123         }
124         config_close(parser);
125
126         server_config.start_ip = ntohl(server_config.start_ip);
127         server_config.end_ip = ntohl(server_config.end_ip);
128 }
129
130 void FAST_FUNC write_leases(void)
131 {
132         int fd;
133         unsigned i;
134         leasetime_t curr;
135         int64_t written_at;
136
137         fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
138         if (fd < 0)
139                 return;
140
141         curr = written_at = time(NULL);
142
143         written_at = hton64(written_at);
144         full_write(fd, &written_at, sizeof(written_at));
145
146         for (i = 0; i < server_config.max_leases; i++) {
147                 leasetime_t tmp_time;
148
149                 if (g_leases[i].lease_nip == 0)
150                         continue;
151
152                 /* Screw with the time in the struct, for easier writing */
153                 tmp_time = g_leases[i].expires;
154
155                 g_leases[i].expires -= curr;
156                 if ((signed_leasetime_t) g_leases[i].expires < 0)
157                         g_leases[i].expires = 0;
158                 g_leases[i].expires = htonl(g_leases[i].expires);
159
160                 /* No error check. If the file gets truncated,
161                  * we lose some leases on restart. Oh well. */
162                 full_write(fd, &g_leases[i], sizeof(g_leases[i]));
163
164                 /* Then restore it when done */
165                 g_leases[i].expires = tmp_time;
166         }
167         close(fd);
168
169         if (server_config.notify_file) {
170                 char *argv[3];
171                 argv[0] = server_config.notify_file;
172                 argv[1] = server_config.lease_file;
173                 argv[2] = NULL;
174                 spawn_and_wait(argv);
175         }
176 }
177
178 void FAST_FUNC read_leases(const char *file)
179 {
180         struct dyn_lease lease;
181         int64_t written_at, time_passed;
182         int fd;
183 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
184         unsigned i = 0;
185 #endif
186
187         fd = open_or_warn(file, O_RDONLY);
188         if (fd < 0)
189                 return;
190
191         if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
192                 goto ret;
193         written_at = ntoh64(written_at);
194
195         time_passed = time(NULL) - written_at;
196         /* Strange written_at, or lease file from old version of udhcpd
197          * which had no "written_at" field? */
198         if ((uint64_t)time_passed > 12 * 60 * 60)
199                 goto ret;
200
201         while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
202 //FIXME: what if it matches some static lease?
203                 uint32_t y = ntohl(lease.lease_nip);
204                 if (y >= server_config.start_ip && y <= server_config.end_ip) {
205                         signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed;
206                         if (expires <= 0)
207                                 continue;
208                         /* NB: add_lease takes "relative time", IOW,
209                          * lease duration, not lease deadline. */
210                         if (add_lease(lease.lease_mac, lease.lease_nip,
211                                         expires,
212                                         lease.hostname, sizeof(lease.hostname)
213                                 ) == 0
214                         ) {
215                                 bb_error_msg("too many leases while loading %s", file);
216                                 break;
217                         }
218 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
219                         i++;
220 #endif
221                 }
222         }
223         log1("Read %d leases", i);
224  ret:
225         close(fd);
226 }