2 |
/* |
/* |
3 |
* files.c -- DHCP server file manipulation * |
* files.c -- DHCP server file manipulation * |
4 |
* Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
* 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> |
#include <netinet/ether.h> |
12 |
#include "dhcpd.h" |
#include "dhcpd.h" |
13 |
#include "options.h" |
#include "options.h" |
14 |
|
|
15 |
|
#if BB_LITTLE_ENDIAN |
16 |
|
static 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 |
|
|
25 |
|
|
26 |
/* on these functions, make sure your datatype matches */ |
/* on these functions, make sure your datatype matches */ |
27 |
static int read_ip(const char *line, void *arg) |
static int FAST_FUNC read_nip(const char *line, void *arg) |
28 |
{ |
{ |
29 |
len_and_sockaddr *lsa; |
len_and_sockaddr *lsa; |
30 |
|
|
36 |
return 1; |
return 1; |
37 |
} |
} |
38 |
|
|
|
static int read_mac(const char *line, void *arg) |
|
|
{ |
|
|
struct ether_addr *temp_ether_addr; |
|
39 |
|
|
40 |
temp_ether_addr = ether_aton_r(line, (struct ether_addr *)arg); |
static int FAST_FUNC read_mac(const char *line, void *arg) |
41 |
if (temp_ether_addr == NULL) |
{ |
42 |
return 0; |
return NULL == ether_aton_r(line, (struct ether_addr *)arg); |
|
return 1; |
|
43 |
} |
} |
44 |
|
|
45 |
|
|
46 |
static int read_str(const char *line, void *arg) |
static int FAST_FUNC read_str(const char *line, void *arg) |
47 |
{ |
{ |
48 |
char **dest = arg; |
char **dest = arg; |
49 |
|
|
53 |
} |
} |
54 |
|
|
55 |
|
|
56 |
static int read_u32(const char *line, void *arg) |
static int FAST_FUNC read_u32(const char *line, void *arg) |
57 |
{ |
{ |
58 |
*(uint32_t*)arg = bb_strtou32(line, NULL, 10); |
*(uint32_t*)arg = bb_strtou32(line, NULL, 10); |
59 |
return errno == 0; |
return errno == 0; |
60 |
} |
} |
61 |
|
|
62 |
|
|
63 |
static int read_yn(const char *line, void *arg) |
static int FAST_FUNC read_yn(const char *line, void *arg) |
64 |
{ |
{ |
65 |
char *dest = arg; |
char *dest = arg; |
66 |
|
|
96 |
|
|
97 |
existing = find_option(*opt_list, option->code); |
existing = find_option(*opt_list, option->code); |
98 |
if (!existing) { |
if (!existing) { |
99 |
DEBUG("Attaching option %02x to list", option->code); |
log2("Attaching option %02x to list", option->code); |
100 |
|
|
101 |
#if ENABLE_FEATURE_UDHCP_RFC3397 |
#if ENABLE_FEATURE_UDHCP_RFC3397 |
102 |
if ((option->flags & TYPE_MASK) == OPTION_STR1035) |
if ((option->flags & TYPE_MASK) == OPTION_STR1035) |
125 |
} |
} |
126 |
|
|
127 |
/* add it to an existing option */ |
/* add it to an existing option */ |
128 |
DEBUG("Attaching option %02x to existing member of list", option->code); |
log1("Attaching option %02x to existing member of list", option->code); |
129 |
if (option->flags & OPTION_LIST) { |
if (option->flags & OPTION_LIST) { |
130 |
#if ENABLE_FEATURE_UDHCP_RFC3397 |
#if ENABLE_FEATURE_UDHCP_RFC3397 |
131 |
if ((option->flags & TYPE_MASK) == OPTION_STR1035) |
if ((option->flags & TYPE_MASK) == OPTION_STR1035) |
156 |
|
|
157 |
|
|
158 |
/* read a dhcp option and add it to opt_list */ |
/* read a dhcp option and add it to opt_list */ |
159 |
static int read_opt(const char *const_line, void *arg) |
static int FAST_FUNC read_opt(const char *const_line, void *arg) |
160 |
{ |
{ |
161 |
struct option_set **opt_list = arg; |
struct option_set **opt_list = arg; |
162 |
char *opt, *val, *endptr; |
char *opt, *val, *endptr; |
187 |
opt = buffer; /* new meaning for variable opt */ |
opt = buffer; /* new meaning for variable opt */ |
188 |
switch (option->flags & TYPE_MASK) { |
switch (option->flags & TYPE_MASK) { |
189 |
case OPTION_IP: |
case OPTION_IP: |
190 |
retval = read_ip(val, buffer); |
retval = read_nip(val, buffer); |
191 |
break; |
break; |
192 |
case OPTION_IP_PAIR: |
case OPTION_IP_PAIR: |
193 |
retval = read_ip(val, buffer); |
retval = read_nip(val, buffer); |
194 |
val = strtok(NULL, ", \t/-"); |
val = strtok(NULL, ", \t/-"); |
195 |
if (!val) |
if (!val) |
196 |
retval = 0; |
retval = 0; |
197 |
if (retval) |
if (retval) |
198 |
retval = read_ip(val, buffer + 4); |
retval = read_nip(val, buffer + 4); |
199 |
break; |
break; |
200 |
case OPTION_STRING: |
case OPTION_STRING: |
201 |
#if ENABLE_FEATURE_UDHCP_RFC3397 |
#if ENABLE_FEATURE_UDHCP_RFC3397 |
251 |
return retval; |
return retval; |
252 |
} |
} |
253 |
|
|
254 |
static int read_staticlease(const char *const_line, void *arg) |
static int FAST_FUNC read_staticlease(const char *const_line, void *arg) |
255 |
{ |
{ |
256 |
char *line; |
char *line; |
257 |
char *mac_string; |
char *mac_string; |
258 |
char *ip_string; |
char *ip_string; |
259 |
uint8_t *mac_bytes; |
struct ether_addr mac_bytes; |
260 |
uint32_t *ip; |
uint32_t ip; |
|
|
|
|
/* Allocate memory for addresses */ |
|
|
mac_bytes = xmalloc(sizeof(unsigned char) * 8); |
|
|
ip = xmalloc(sizeof(uint32_t)); |
|
261 |
|
|
262 |
/* Read mac */ |
/* Read mac */ |
263 |
line = (char *) const_line; |
line = (char *) const_line; |
264 |
mac_string = strtok(line, " \t"); |
mac_string = strtok_r(line, " \t", &line); |
265 |
read_mac(mac_string, mac_bytes); |
read_mac(mac_string, &mac_bytes); |
266 |
|
|
267 |
/* Read ip */ |
/* Read ip */ |
268 |
ip_string = strtok(NULL, " \t"); |
ip_string = strtok_r(NULL, " \t", &line); |
269 |
read_ip(ip_string, ip); |
read_nip(ip_string, &ip); |
270 |
|
|
271 |
addStaticLease(arg, mac_bytes, ip); |
add_static_lease(arg, (uint8_t*) &mac_bytes, ip); |
272 |
|
|
273 |
if (ENABLE_UDHCP_DEBUG) printStaticLeases(arg); |
log_static_leases(arg); |
274 |
|
|
275 |
return 1; |
return 1; |
276 |
} |
} |
278 |
|
|
279 |
struct config_keyword { |
struct config_keyword { |
280 |
const char *keyword; |
const char *keyword; |
281 |
int (*handler)(const char *line, void *var); |
int (*handler)(const char *line, void *var) FAST_FUNC; |
282 |
void *var; |
void *var; |
283 |
const char *def; |
const char *def; |
284 |
}; |
}; |
285 |
|
|
286 |
static const struct config_keyword keywords[] = { |
static const struct config_keyword keywords[] = { |
287 |
/* keyword handler variable address default */ |
/* keyword handler variable address default */ |
288 |
{"start", read_ip, &(server_config.start_ip), "192.168.0.20"}, |
{"start", read_nip, &(server_config.start_ip), "192.168.0.20"}, |
289 |
{"end", read_ip, &(server_config.end_ip), "192.168.0.254"}, |
{"end", read_nip, &(server_config.end_ip), "192.168.0.254"}, |
290 |
{"interface", read_str, &(server_config.interface), "eth0"}, |
{"interface", read_str, &(server_config.interface), "eth0"}, |
291 |
/* Avoid "max_leases value not sane" warning by setting default |
/* Avoid "max_leases value not sane" warning by setting default |
292 |
* to default_end_ip - default_start_ip + 1: */ |
* to default_end_ip - default_start_ip + 1: */ |
293 |
{"max_leases", read_u32, &(server_config.max_leases), "235"}, |
{"max_leases", read_u32, &(server_config.max_leases), "235"}, |
|
{"remaining", read_yn, &(server_config.remaining), "yes"}, |
|
294 |
{"auto_time", read_u32, &(server_config.auto_time), "7200"}, |
{"auto_time", read_u32, &(server_config.auto_time), "7200"}, |
295 |
{"decline_time", read_u32, &(server_config.decline_time), "3600"}, |
{"decline_time", read_u32, &(server_config.decline_time), "3600"}, |
296 |
{"conflict_time",read_u32, &(server_config.conflict_time),"3600"}, |
{"conflict_time",read_u32, &(server_config.conflict_time),"3600"}, |
297 |
{"offer_time", read_u32, &(server_config.offer_time), "60"}, |
{"offer_time", read_u32, &(server_config.offer_time), "60"}, |
298 |
{"min_lease", read_u32, &(server_config.min_lease), "60"}, |
{"min_lease", read_u32, &(server_config.min_lease_sec),"60"}, |
299 |
{"lease_file", read_str, &(server_config.lease_file), LEASES_FILE}, |
{"lease_file", read_str, &(server_config.lease_file), LEASES_FILE}, |
300 |
{"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"}, |
{"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"}, |
301 |
{"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"}, |
{"siaddr", read_nip, &(server_config.siaddr_nip), "0.0.0.0"}, |
302 |
/* keywords with no defaults must be last! */ |
/* keywords with no defaults must be last! */ |
303 |
{"option", read_opt, &(server_config.options), ""}, |
{"option", read_opt, &(server_config.options), ""}, |
304 |
{"opt", read_opt, &(server_config.options), ""}, |
{"opt", read_opt, &(server_config.options), ""}, |
306 |
{"sname", read_str, &(server_config.sname), ""}, |
{"sname", read_str, &(server_config.sname), ""}, |
307 |
{"boot_file", read_str, &(server_config.boot_file), ""}, |
{"boot_file", read_str, &(server_config.boot_file), ""}, |
308 |
{"static_lease", read_staticlease, &(server_config.static_leases), ""}, |
{"static_lease", read_staticlease, &(server_config.static_leases), ""}, |
|
/* ADDME: static lease */ |
|
309 |
}; |
}; |
310 |
enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 }; |
enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 }; |
311 |
|
|
342 |
|
|
343 |
void FAST_FUNC write_leases(void) |
void FAST_FUNC write_leases(void) |
344 |
{ |
{ |
345 |
int fp; |
int fd; |
346 |
unsigned i; |
unsigned i; |
347 |
time_t curr = time(0); |
leasetime_t curr; |
348 |
unsigned long tmp_time; |
int64_t written_at; |
349 |
|
|
350 |
fp = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC); |
fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC); |
351 |
if (fp < 0) { |
if (fd < 0) |
352 |
return; |
return; |
353 |
} |
|
354 |
|
curr = written_at = time(NULL); |
355 |
|
|
356 |
|
written_at = hton64(written_at); |
357 |
|
full_write(fd, &written_at, sizeof(written_at)); |
358 |
|
|
359 |
for (i = 0; i < server_config.max_leases; i++) { |
for (i = 0; i < server_config.max_leases; i++) { |
360 |
if (leases[i].yiaddr != 0) { |
leasetime_t tmp_time; |
361 |
|
|
362 |
/* screw with the time in the struct, for easier writing */ |
if (g_leases[i].lease_nip == 0) |
363 |
tmp_time = leases[i].expires; |
continue; |
364 |
|
|
365 |
if (server_config.remaining) { |
/* Screw with the time in the struct, for easier writing */ |
366 |
if (lease_expired(&(leases[i]))) |
tmp_time = g_leases[i].expires; |
|
leases[i].expires = 0; |
|
|
else leases[i].expires -= curr; |
|
|
} /* else stick with the time we got */ |
|
|
leases[i].expires = htonl(leases[i].expires); |
|
|
// FIXME: error check?? |
|
|
full_write(fp, &leases[i], sizeof(leases[i])); |
|
367 |
|
|
368 |
/* then restore it when done */ |
g_leases[i].expires -= curr; |
369 |
leases[i].expires = tmp_time; |
if ((signed_leasetime_t) g_leases[i].expires < 0) |
370 |
} |
g_leases[i].expires = 0; |
371 |
|
g_leases[i].expires = htonl(g_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, &g_leases[i], sizeof(g_leases[i])); |
376 |
|
|
377 |
|
/* Then restore it when done */ |
378 |
|
g_leases[i].expires = tmp_time; |
379 |
} |
} |
380 |
close(fp); |
close(fd); |
381 |
|
|
382 |
if (server_config.notify_file) { |
if (server_config.notify_file) { |
383 |
// TODO: vfork-based child creation |
// TODO: vfork-based child creation |
390 |
|
|
391 |
void FAST_FUNC read_leases(const char *file) |
void FAST_FUNC read_leases(const char *file) |
392 |
{ |
{ |
393 |
int fp; |
struct dyn_lease lease; |
394 |
unsigned i; |
int64_t written_at, time_passed; |
395 |
struct dhcpOfferedAddr lease; |
int fd; |
396 |
|
#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
397 |
|
unsigned i = 0; |
398 |
|
#endif |
399 |
|
|
400 |
fp = open_or_warn(file, O_RDONLY); |
fd = open_or_warn(file, O_RDONLY); |
401 |
if (fp < 0) { |
if (fd < 0) |
402 |
return; |
return; |
|
} |
|
403 |
|
|
404 |
i = 0; |
if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at)) |
405 |
while (i < server_config.max_leases |
goto ret; |
406 |
&& full_read(fp, &lease, sizeof(lease)) == sizeof(lease) |
written_at = ntoh64(written_at); |
407 |
) { |
|
408 |
/* ADDME: is it a static lease */ |
time_passed = time(NULL) - written_at; |
409 |
uint32_t y = ntohl(lease.yiaddr); |
/* 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 |
|
|
414 |
|
while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { |
415 |
|
//FIXME: what if it matches some static lease? |
416 |
|
uint32_t y = ntohl(lease.lease_nip); |
417 |
if (y >= server_config.start_ip && y <= server_config.end_ip) { |
if (y >= server_config.start_ip && y <= server_config.end_ip) { |
418 |
lease.expires = ntohl(lease.expires); |
signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed; |
419 |
if (!server_config.remaining) |
if (expires <= 0) |
420 |
lease.expires -= time(NULL); |
continue; |
421 |
if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) { |
/* NB: add_lease takes "relative time", IOW, |
422 |
|
* lease duration, not lease deadline. */ |
423 |
|
if (add_lease(lease.lease_mac, lease.lease_nip, |
424 |
|
expires, |
425 |
|
lease.hostname, sizeof(lease.hostname) |
426 |
|
) == 0 |
427 |
|
) { |
428 |
bb_error_msg("too many leases while loading %s", file); |
bb_error_msg("too many leases while loading %s", file); |
429 |
break; |
break; |
430 |
} |
} |
431 |
|
#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
432 |
i++; |
i++; |
433 |
|
#endif |
434 |
} |
} |
435 |
} |
} |
436 |
DEBUG("Read %d leases", i); |
log1("Read %d leases", i); |
437 |
close(fp); |
ret: |
438 |
|
close(fd); |
439 |
} |
} |