hush/networking/udhcp/files.c

217 lines
6.2 KiB
C
Raw Normal View History

/* vi: set sw=4 ts=4: */
2006-05-08 03:20:50 +00:00
/*
* DHCP server config and lease file manipulation
*
2006-05-08 03:20:50 +00:00
* Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
2008-12-07 00:52:58 +00:00
*
* Licensed under GPLv2, see file LICENSE in this source tree.
2006-05-08 03:20:50 +00:00
*/
#include <netinet/ether.h>
#include "common.h"
2006-05-08 03:20:50 +00:00
#include "dhcpd.h"
/* on these functions, make sure your datatype matches */
static int FAST_FUNC read_str(const char *line, void *arg)
2006-05-08 03:20:50 +00:00
{
char **dest = arg;
free(*dest);
*dest = xstrdup(line);
2006-05-08 03:20:50 +00:00
return 1;
}
static int FAST_FUNC read_u32(const char *line, void *arg)
2006-05-08 03:20:50 +00:00
{
*(uint32_t*)arg = bb_strtou32(line, NULL, 10);
return errno == 0;
2006-05-08 03:20:50 +00:00
}
static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
2006-05-08 03:20:50 +00:00
{
char *line;
char *mac_string;
char *ip_string;
struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */
uint32_t nip;
2006-05-08 03:20:50 +00:00
/* Read mac */
line = (char *) const_line;
mac_string = strtok_r(line, " \t", &line);
if (!mac_string || !ether_aton_r(mac_string, &mac_bytes))
return 0;
2006-05-08 03:20:50 +00:00
/* Read ip */
ip_string = strtok_r(NULL, " \t", &line);
if (!ip_string || !udhcp_str2nip(ip_string, &nip))
return 0;
2006-05-08 03:20:50 +00:00
add_static_lease(arg, (uint8_t*) &mac_bytes, nip);
2006-05-08 03:20:50 +00:00
log_static_leases(arg);
2006-05-08 03:20:50 +00:00
return 1;
}
struct config_keyword {
const char *keyword;
int (*handler)(const char *line, void *var) FAST_FUNC;
void *var;
const char *def;
};
2006-05-08 03:20:50 +00:00
static const struct config_keyword keywords[] = {
/* keyword handler variable address default */
{"start" , udhcp_str2nip , &server_config.start_ip , "192.168.0.20"},
{"end" , udhcp_str2nip , &server_config.end_ip , "192.168.0.254"},
{"interface" , read_str , &server_config.interface , "eth0"},
/* Avoid "max_leases value not sane" warning by setting default
* to default_end_ip - default_start_ip + 1: */
{"max_leases" , read_u32 , &server_config.max_leases , "235"},
{"auto_time" , read_u32 , &server_config.auto_time , "7200"},
{"decline_time" , read_u32 , &server_config.decline_time , "3600"},
{"conflict_time", read_u32 , &server_config.conflict_time, "3600"},
{"offer_time" , read_u32 , &server_config.offer_time , "60"},
{"min_lease" , read_u32 , &server_config.min_lease_sec, "60"},
{"lease_file" , read_str , &server_config.lease_file , LEASES_FILE},
{"pidfile" , read_str , &server_config.pidfile , "/var/run/udhcpd.pid"},
{"siaddr" , udhcp_str2nip , &server_config.siaddr_nip , "0.0.0.0"},
/* keywords with no defaults must be last! */
{"option" , udhcp_str2optset, &server_config.options , ""},
{"opt" , udhcp_str2optset, &server_config.options , ""},
{"notify_file" , read_str , &server_config.notify_file , ""},
{"sname" , read_str , &server_config.sname , ""},
{"boot_file" , read_str , &server_config.boot_file , ""},
{"static_lease" , read_staticlease, &server_config.static_leases, ""},
2006-05-08 03:20:50 +00:00
};
enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
2006-05-08 03:20:50 +00:00
void FAST_FUNC read_config(const char *file)
2006-05-08 03:20:50 +00:00
{
parser_t *parser;
const struct config_keyword *k;
unsigned i;
char *token[2];
2006-05-08 03:20:50 +00:00
for (i = 0; i < KWS_WITH_DEFAULTS; i++)
keywords[i].handler(keywords[i].def, keywords[i].var);
2006-05-08 03:20:50 +00:00
parser = config_open(file);
while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
if (strcasecmp(token[0], k->keyword) == 0) {
if (!k->handler(token[1], k->var)) {
bb_error_msg("can't parse line %u in %s",
parser->lineno, file);
2006-05-08 03:20:50 +00:00
/* reset back to the default value */
k->handler(k->def, k->var);
2006-05-08 03:20:50 +00:00
}
break;
}
}
2006-05-08 03:20:50 +00:00
}
config_close(parser);
server_config.start_ip = ntohl(server_config.start_ip);
server_config.end_ip = ntohl(server_config.end_ip);
2006-05-08 03:20:50 +00:00
}
void FAST_FUNC write_leases(void)
2006-05-08 03:20:50 +00:00
{
int fd;
unsigned i;
leasetime_t curr;
int64_t written_at;
2006-05-08 03:20:50 +00:00
fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
if (fd < 0)
2006-05-08 03:20:50 +00:00
return;
curr = written_at = time(NULL);
written_at = SWAP_BE64(written_at);
full_write(fd, &written_at, sizeof(written_at));
2006-05-08 03:20:50 +00:00
for (i = 0; i < server_config.max_leases; i++) {
leasetime_t tmp_time;
if (g_leases[i].lease_nip == 0)
continue;
/* Screw with the time in the struct, for easier writing */
tmp_time = g_leases[i].expires;
g_leases[i].expires -= curr;
if ((signed_leasetime_t) g_leases[i].expires < 0)
g_leases[i].expires = 0;
g_leases[i].expires = htonl(g_leases[i].expires);
/* No error check. If the file gets truncated,
* we lose some leases on restart. Oh well. */
full_write(fd, &g_leases[i], sizeof(g_leases[i]));
/* Then restore it when done */
g_leases[i].expires = tmp_time;
2006-05-08 03:20:50 +00:00
}
close(fd);
2006-05-08 03:20:50 +00:00
if (server_config.notify_file) {
char *argv[3];
argv[0] = server_config.notify_file;
argv[1] = server_config.lease_file;
argv[2] = NULL;
spawn_and_wait(argv);
2006-05-08 03:20:50 +00:00
}
}
void FAST_FUNC read_leases(const char *file)
2006-05-08 03:20:50 +00:00
{
struct dyn_lease lease;
int64_t written_at, time_passed;
int fd;
#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
unsigned i = 0;
#endif
2006-05-08 03:20:50 +00:00
fd = open_or_warn(file, O_RDONLY);
if (fd < 0)
2006-05-08 03:20:50 +00:00
return;
if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
goto ret;
written_at = SWAP_BE64(written_at);
time_passed = time(NULL) - written_at;
/* Strange written_at, or lease file from old version of udhcpd
* which had no "written_at" field? */
if ((uint64_t)time_passed > 12 * 60 * 60)
goto ret;
while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
//FIXME: what if it matches some static lease?
uint32_t y = ntohl(lease.lease_nip);
if (y >= server_config.start_ip && y <= server_config.end_ip) {
signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed;
if (expires <= 0)
continue;
/* NB: add_lease takes "relative time", IOW,
* lease duration, not lease deadline. */
if (add_lease(lease.lease_mac, lease.lease_nip,
expires,
lease.hostname, sizeof(lease.hostname)
) == 0
) {
bb_error_msg("too many leases while loading %s", file);
2006-05-08 03:20:50 +00:00
break;
}
#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
i++;
#endif
2006-05-08 03:20:50 +00:00
}
}
log1("Read %d leases", i);
ret:
close(fd);
2006-05-08 03:20:50 +00:00
}