blob: bddf3e141ff4d001c2a5931ccb7d464810a9283e [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/*
3 * files.c -- DHCP server file manipulation *
4 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00005 *
6 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Mike Frysinger7031f622006-05-08 03:20:50 +00007 */
8
Mike Frysinger7031f622006-05-08 03:20:50 +00009#include <netinet/ether.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000010
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000011#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000012#include "dhcpd.h"
13#include "options.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000014
Denis Vlasenko73121c32009-02-01 00:40:45 +000015#if BB_LITTLE_ENDIAN
16static inline uint64_t hton64(uint64_t v)
17{
18 return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32);
19}
20#else
21#define hton64(v) (v)
22#endif
23#define ntoh64(v) hton64(v)
24
Mike Frysinger7031f622006-05-08 03:20:50 +000025
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000026/* on these functions, make sure your datatype matches */
Denys Vlasenko56f2d062009-06-16 10:25:35 +020027static int FAST_FUNC read_nip(const char *line, void *arg)
Mike Frysinger7031f622006-05-08 03:20:50 +000028{
Denis Vlasenkoe9913032007-02-04 02:41:57 +000029 len_and_sockaddr *lsa;
Mike Frysinger7031f622006-05-08 03:20:50 +000030
Denis Vlasenkoe9913032007-02-04 02:41:57 +000031 lsa = host_and_af2sockaddr(line, 0, AF_INET);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000032 if (!lsa)
33 return 0;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +000034 *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000035 free(lsa);
36 return 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000037}
38
Denis Vlasenko04158e02009-02-02 10:48:06 +000039
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020040static int FAST_FUNC read_mac(const char *line, void *arg)
Mike Frysinger7031f622006-05-08 03:20:50 +000041{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000042 return NULL == ether_aton_r(line, (struct ether_addr *)arg);
Mike Frysinger7031f622006-05-08 03:20:50 +000043}
44
45
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020046static int FAST_FUNC read_str(const char *line, void *arg)
Mike Frysinger7031f622006-05-08 03:20:50 +000047{
48 char **dest = arg;
49
50 free(*dest);
Denis Vlasenko4ebaf102007-01-19 21:33:19 +000051 *dest = xstrdup(line);
Mike Frysinger7031f622006-05-08 03:20:50 +000052 return 1;
53}
54
55
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020056static int FAST_FUNC read_u32(const char *line, void *arg)
Mike Frysinger7031f622006-05-08 03:20:50 +000057{
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000058 *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
Denis Vlasenkod686a042006-11-27 14:43:21 +000059 return errno == 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000060}
61
62
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020063static int FAST_FUNC read_yn(const char *line, void *arg)
Mike Frysinger7031f622006-05-08 03:20:50 +000064{
65 char *dest = arg;
Mike Frysinger7031f622006-05-08 03:20:50 +000066
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000067 if (!strcasecmp("yes", line)) {
Mike Frysinger7031f622006-05-08 03:20:50 +000068 *dest = 1;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000069 return 1;
70 }
71 if (!strcasecmp("no", line)) {
Mike Frysinger7031f622006-05-08 03:20:50 +000072 *dest = 0;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000073 return 1;
74 }
75 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000076}
77
78
79/* find option 'code' in opt_list */
Denis Vlasenkof1980f62008-09-26 09:34:59 +000080struct option_set* FAST_FUNC find_option(struct option_set *opt_list, uint8_t code)
Mike Frysinger7031f622006-05-08 03:20:50 +000081{
82 while (opt_list && opt_list->data[OPT_CODE] < code)
83 opt_list = opt_list->next;
84
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000085 if (opt_list && opt_list->data[OPT_CODE] == code)
86 return opt_list;
87 return NULL;
Mike Frysinger7031f622006-05-08 03:20:50 +000088}
89
90
91/* add an option to the opt_list */
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000092static void attach_option(struct option_set **opt_list,
93 const struct dhcp_option *option, char *buffer, int length)
Mike Frysinger7031f622006-05-08 03:20:50 +000094{
95 struct option_set *existing, *new, **curr;
96
Denis Vlasenkod686a042006-11-27 14:43:21 +000097 existing = find_option(*opt_list, option->code);
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +000098 if (!existing) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020099 log2("Attaching option %02x to list", option->code);
Mike Frysinger7031f622006-05-08 03:20:50 +0000100
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000101#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000102 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
103 /* reuse buffer and length for RFC1035-formatted string */
Denis Vlasenkoc4f12f52008-05-12 14:35:56 +0000104 buffer = (char *)dname_enc(NULL, 0, buffer, &length);
Denis Vlasenko50664732007-02-27 21:15:08 +0000105#endif
106
Mike Frysinger7031f622006-05-08 03:20:50 +0000107 /* make a new option */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000108 new = xmalloc(sizeof(*new));
Mike Frysinger7031f622006-05-08 03:20:50 +0000109 new->data = xmalloc(length + 2);
110 new->data[OPT_CODE] = option->code;
111 new->data[OPT_LEN] = length;
112 memcpy(new->data + 2, buffer, length);
113
114 curr = opt_list;
115 while (*curr && (*curr)->data[OPT_CODE] < option->code)
116 curr = &(*curr)->next;
117
118 new->next = *curr;
119 *curr = new;
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000120#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000121 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
122 free(buffer);
123#endif
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000124 return;
Mike Frysinger7031f622006-05-08 03:20:50 +0000125 }
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000126
127 /* add it to an existing option */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200128 log1("Attaching option %02x to existing member of list", option->code);
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000129 if (option->flags & OPTION_LIST) {
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000130#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000131 if ((option->flags & TYPE_MASK) == OPTION_STR1035)
132 /* reuse buffer and length for RFC1035-formatted string */
Denis Vlasenkoc4f12f52008-05-12 14:35:56 +0000133 buffer = (char *)dname_enc(existing->data + 2,
Denis Vlasenko50664732007-02-27 21:15:08 +0000134 existing->data[OPT_LEN], buffer, &length);
135#endif
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000136 if (existing->data[OPT_LEN] + length <= 255) {
137 existing->data = xrealloc(existing->data,
138 existing->data[OPT_LEN] + length + 3);
139 if ((option->flags & TYPE_MASK) == OPTION_STRING) {
Denis Vlasenkoe129f432007-02-02 01:57:24 +0000140 /* ' ' can bring us to 256 - bad */
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000141 if (existing->data[OPT_LEN] + length >= 255)
142 return;
143 /* add space separator between STRING options in a list */
144 existing->data[existing->data[OPT_LEN] + 2] = ' ';
145 existing->data[OPT_LEN]++;
146 }
147 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
148 existing->data[OPT_LEN] += length;
149 } /* else, ignore the data, we could put this in a second option in the future */
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000150#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000151 if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
152 free(buffer);
153#endif
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +0000154 } /* else, ignore the new data */
Mike Frysinger7031f622006-05-08 03:20:50 +0000155}
156
157
158/* read a dhcp option and add it to opt_list */
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200159static int FAST_FUNC read_opt(const char *const_line, void *arg)
Mike Frysinger7031f622006-05-08 03:20:50 +0000160{
161 struct option_set **opt_list = arg;
162 char *opt, *val, *endptr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000163 char *line;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000164 const struct dhcp_option *option;
165 int retval, length, idx;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000166 char buffer[8] ALIGNED(4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000167 uint16_t *result_u16 = (uint16_t *) buffer;
168 uint32_t *result_u32 = (uint32_t *) buffer;
169
170 /* Cheat, the only const line we'll actually get is "" */
171 line = (char *) const_line;
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000172 opt = strtok(line, " \t=");
Denis Vlasenkob539c842007-11-29 08:17:45 +0000173 if (!opt)
174 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000175
Denis Vlasenko027271e2008-03-20 12:47:01 +0000176 idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000177 if (idx < 0)
178 return 0;
179 option = &dhcp_options[idx];
Mike Frysinger7031f622006-05-08 03:20:50 +0000180
Denis Vlasenkob539c842007-11-29 08:17:45 +0000181 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000182 do {
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000183 val = strtok(NULL, ", \t");
184 if (!val) break;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000185 length = dhcp_option_lengths[option->flags & TYPE_MASK];
Mike Frysinger7031f622006-05-08 03:20:50 +0000186 retval = 0;
187 opt = buffer; /* new meaning for variable opt */
188 switch (option->flags & TYPE_MASK) {
189 case OPTION_IP:
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200190 retval = read_nip(val, buffer);
Mike Frysinger7031f622006-05-08 03:20:50 +0000191 break;
192 case OPTION_IP_PAIR:
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200193 retval = read_nip(val, buffer);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000194 val = strtok(NULL, ", \t/-");
195 if (!val)
196 retval = 0;
197 if (retval)
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200198 retval = read_nip(val, buffer + 4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000199 break;
200 case OPTION_STRING:
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000201#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000202 case OPTION_STR1035:
203#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000204 length = strlen(val);
205 if (length > 0) {
206 if (length > 254) length = 254;
207 opt = val;
208 retval = 1;
209 }
210 break;
211 case OPTION_BOOLEAN:
212 retval = read_yn(val, buffer);
213 break;
214 case OPTION_U8:
215 buffer[0] = strtoul(val, &endptr, 0);
216 retval = (endptr[0] == '\0');
217 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000218 /* htonX are macros in older libc's, using temp var
219 * in code below for safety */
220 /* TODO: use bb_strtoX? */
221 case OPTION_U16: {
222 unsigned long tmp = strtoul(val, &endptr, 0);
223 *result_u16 = htons(tmp);
224 retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
225 break;
226 }
227 case OPTION_S16: {
228 long tmp = strtol(val, &endptr, 0);
229 *result_u16 = htons(tmp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000230 retval = (endptr[0] == '\0');
231 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000232 }
233 case OPTION_U32: {
234 unsigned long tmp = strtoul(val, &endptr, 0);
235 *result_u32 = htonl(tmp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000236 retval = (endptr[0] == '\0');
237 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000238 }
239 case OPTION_S32: {
240 long tmp = strtol(val, &endptr, 0);
241 *result_u32 = htonl(tmp);
Mike Frysinger7031f622006-05-08 03:20:50 +0000242 retval = (endptr[0] == '\0');
243 break;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000244 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000245 default:
246 break;
247 }
248 if (retval)
249 attach_option(opt_list, option, opt, length);
250 } while (retval && option->flags & OPTION_LIST);
251 return retval;
252}
253
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200254static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
Mike Frysinger7031f622006-05-08 03:20:50 +0000255{
Mike Frysinger7031f622006-05-08 03:20:50 +0000256 char *line;
257 char *mac_string;
258 char *ip_string;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000259 struct ether_addr mac_bytes;
260 uint32_t ip;
Mike Frysinger7031f622006-05-08 03:20:50 +0000261
262 /* Read mac */
263 line = (char *) const_line;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000264 mac_string = strtok_r(line, " \t", &line);
265 read_mac(mac_string, &mac_bytes);
Mike Frysinger7031f622006-05-08 03:20:50 +0000266
267 /* Read ip */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000268 ip_string = strtok_r(NULL, " \t", &line);
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200269 read_nip(ip_string, &ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000270
Denys Vlasenkocab3a012009-06-16 12:03:12 +0200271 add_static_lease(arg, (uint8_t*) &mac_bytes, ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000272
Denys Vlasenkocab3a012009-06-16 12:03:12 +0200273 if (ENABLE_UDHCP_DEBUG) print_static_leases(arg);
Mike Frysinger7031f622006-05-08 03:20:50 +0000274
275 return 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000276}
277
278
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000279struct config_keyword {
280 const char *keyword;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200281 int (*handler)(const char *line, void *var) FAST_FUNC;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000282 void *var;
283 const char *def;
Mike Frysinger7031f622006-05-08 03:20:50 +0000284};
285
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000286static const struct config_keyword keywords[] = {
287 /* keyword handler variable address default */
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200288 {"start", read_nip, &(server_config.start_ip), "192.168.0.20"},
289 {"end", read_nip, &(server_config.end_ip), "192.168.0.254"},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000290 {"interface", read_str, &(server_config.interface), "eth0"},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000291 /* Avoid "max_leases value not sane" warning by setting default
292 * to default_end_ip - default_start_ip + 1: */
293 {"max_leases", read_u32, &(server_config.max_leases), "235"},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000294 {"auto_time", read_u32, &(server_config.auto_time), "7200"},
295 {"decline_time", read_u32, &(server_config.decline_time), "3600"},
296 {"conflict_time",read_u32, &(server_config.conflict_time),"3600"},
297 {"offer_time", read_u32, &(server_config.offer_time), "60"},
298 {"min_lease", read_u32, &(server_config.min_lease), "60"},
299 {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
300 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200301 {"siaddr", read_nip, &(server_config.siaddr_nip), "0.0.0.0"},
Denis Vlasenko027271e2008-03-20 12:47:01 +0000302 /* keywords with no defaults must be last! */
303 {"option", read_opt, &(server_config.options), ""},
304 {"opt", read_opt, &(server_config.options), ""},
305 {"notify_file", read_str, &(server_config.notify_file), ""},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000306 {"sname", read_str, &(server_config.sname), ""},
307 {"boot_file", read_str, &(server_config.boot_file), ""},
308 {"static_lease", read_staticlease, &(server_config.static_leases), ""},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000309};
Denis Vlasenko027271e2008-03-20 12:47:01 +0000310enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000311
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000312void FAST_FUNC read_config(const char *file)
Mike Frysinger7031f622006-05-08 03:20:50 +0000313{
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000314 parser_t *parser;
315 const struct config_keyword *k;
316 unsigned i;
317 char *token[2];
Mike Frysinger7031f622006-05-08 03:20:50 +0000318
Denis Vlasenko027271e2008-03-20 12:47:01 +0000319 for (i = 0; i < KWS_WITH_DEFAULTS; i++)
320 keywords[i].handler(keywords[i].def, keywords[i].var);
Mike Frysinger7031f622006-05-08 03:20:50 +0000321
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000322 parser = config_open(file);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000323 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000324 for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
325 if (!strcasecmp(token[0], k->keyword)) {
326 if (!k->handler(token[1], k->var)) {
327 bb_error_msg("can't parse line %u in %s",
328 parser->lineno, file);
Mike Frysinger7031f622006-05-08 03:20:50 +0000329 /* reset back to the default value */
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000330 k->handler(k->def, k->var);
Mike Frysinger7031f622006-05-08 03:20:50 +0000331 }
Denis Vlasenko027271e2008-03-20 12:47:01 +0000332 break;
333 }
334 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000335 }
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000336 config_close(parser);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000337
338 server_config.start_ip = ntohl(server_config.start_ip);
339 server_config.end_ip = ntohl(server_config.end_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000340}
341
342
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000343void FAST_FUNC write_leases(void)
Mike Frysinger7031f622006-05-08 03:20:50 +0000344{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000345 int fd;
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000346 unsigned i;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000347 leasetime_t curr;
Denis Vlasenko73121c32009-02-01 00:40:45 +0000348 int64_t written_at;
Mike Frysinger7031f622006-05-08 03:20:50 +0000349
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000350 fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
351 if (fd < 0)
Mike Frysinger7031f622006-05-08 03:20:50 +0000352 return;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000353
Denis Vlasenko73121c32009-02-01 00:40:45 +0000354 curr = written_at = time(NULL);
355
356 written_at = hton64(written_at);
357 full_write(fd, &written_at, sizeof(written_at));
Mike Frysinger7031f622006-05-08 03:20:50 +0000358
359 for (i = 0; i < server_config.max_leases; i++) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000360 leasetime_t tmp_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000361
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200362 if (leases[i].lease_nip == 0)
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000363 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000364
Denis Vlasenko04158e02009-02-02 10:48:06 +0000365 /* Screw with the time in the struct, for easier writing */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000366 tmp_time = leases[i].expires;
Mike Frysinger7031f622006-05-08 03:20:50 +0000367
Denis Vlasenko73121c32009-02-01 00:40:45 +0000368 leases[i].expires -= curr;
369 if ((signed_leasetime_t) leases[i].expires < 0)
370 leases[i].expires = 0;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000371 leases[i].expires = htonl(leases[i].expires);
372
373 /* No error check. If the file gets truncated,
374 * we lose some leases on restart. Oh well. */
375 full_write(fd, &leases[i], sizeof(leases[i]));
376
Denis Vlasenko04158e02009-02-02 10:48:06 +0000377 /* Then restore it when done */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000378 leases[i].expires = tmp_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000379 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000380 close(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000381
382 if (server_config.notify_file) {
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000383// TODO: vfork-based child creation
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000384 char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
385 system(cmd);
386 free(cmd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000387 }
388}
389
390
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000391void FAST_FUNC read_leases(const char *file)
Mike Frysinger7031f622006-05-08 03:20:50 +0000392{
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200393 struct dyn_lease lease;
Denis Vlasenko04158e02009-02-02 10:48:06 +0000394 int64_t written_at, time_passed;
395 int fd;
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200396#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
397 unsigned i = 0;
398#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000399
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000400 fd = open_or_warn(file, O_RDONLY);
401 if (fd < 0)
Mike Frysinger7031f622006-05-08 03:20:50 +0000402 return;
Mike Frysinger7031f622006-05-08 03:20:50 +0000403
Denis Vlasenko73121c32009-02-01 00:40:45 +0000404 if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
405 goto ret;
406 written_at = ntoh64(written_at);
Denis Vlasenko73121c32009-02-01 00:40:45 +0000407
Denis Vlasenko04158e02009-02-02 10:48:06 +0000408 time_passed = time(NULL) - written_at;
409 /* Strange written_at, or lease file from old version of udhcpd
410 * which had no "written_at" field? */
411 if ((uint64_t)time_passed > 12 * 60 * 60)
412 goto ret;
413
Denis Vlasenko04158e02009-02-02 10:48:06 +0000414 while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200415//FIXME: what if it matches some static lease?
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200416 uint32_t y = ntohl(lease.lease_nip);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000417 if (y >= server_config.start_ip && y <= server_config.end_ip) {
Denis Vlasenko04158e02009-02-02 10:48:06 +0000418 signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed;
Denis Vlasenko73121c32009-02-01 00:40:45 +0000419 if (expires <= 0)
420 continue;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000421 /* NB: add_lease takes "relative time", IOW,
422 * lease duration, not lease deadline. */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200423 if (!(add_lease(lease.lease_mac, lease.lease_nip, expires, lease.hostname))) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000424 bb_error_msg("too many leases while loading %s", file);
Mike Frysinger7031f622006-05-08 03:20:50 +0000425 break;
426 }
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200427#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
428 i++;
429#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000430 }
431 }
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200432 log1("Read %d leases", i);
Denis Vlasenko73121c32009-02-01 00:40:45 +0000433 ret:
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000434 close(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000435}