blob: ff63111c9b1e929fbd20880f0d1aaedfd130b483 [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/*
Denys Vlasenko385b4562010-03-26 10:09:34 +01003 * DHCP server config and lease file manipulation
4 *
Mike Frysinger7031f622006-05-08 03:20:50 +00005 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00006 *
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Mike Frysinger7031f622006-05-08 03:20:50 +00008 */
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"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000013
Denis Vlasenko73121c32009-02-01 00:40:45 +000014#if BB_LITTLE_ENDIAN
15static 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
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000024/* on these functions, make sure your datatype matches */
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020025static int FAST_FUNC read_str(const char *line, void *arg)
Mike Frysinger7031f622006-05-08 03:20:50 +000026{
27 char **dest = arg;
28
29 free(*dest);
Denis Vlasenko4ebaf102007-01-19 21:33:19 +000030 *dest = xstrdup(line);
Mike Frysinger7031f622006-05-08 03:20:50 +000031 return 1;
32}
33
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020034static int FAST_FUNC read_u32(const char *line, void *arg)
Mike Frysinger7031f622006-05-08 03:20:50 +000035{
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000036 *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
Denis Vlasenkod686a042006-11-27 14:43:21 +000037 return errno == 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000038}
39
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020040static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
Mike Frysinger7031f622006-05-08 03:20:50 +000041{
Mike Frysinger7031f622006-05-08 03:20:50 +000042 char *line;
43 char *mac_string;
44 char *ip_string;
Denys Vlasenko37a658c2010-03-23 15:43:08 +010045 struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */
46 uint32_t nip;
Mike Frysinger7031f622006-05-08 03:20:50 +000047
48 /* Read mac */
49 line = (char *) const_line;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000050 mac_string = strtok_r(line, " \t", &line);
Denys Vlasenko37a658c2010-03-23 15:43:08 +010051 if (!mac_string || !ether_aton_r(mac_string, &mac_bytes))
52 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000053
54 /* Read ip */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000055 ip_string = strtok_r(NULL, " \t", &line);
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +010056 if (!ip_string || !udhcp_str2nip(ip_string, &nip))
Denys Vlasenko37a658c2010-03-23 15:43:08 +010057 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000058
Denys Vlasenko37a658c2010-03-23 15:43:08 +010059 add_static_lease(arg, (uint8_t*) &mac_bytes, nip);
Mike Frysinger7031f622006-05-08 03:20:50 +000060
Denys Vlasenko799d00e2009-06-17 13:44:04 +020061 log_static_leases(arg);
Mike Frysinger7031f622006-05-08 03:20:50 +000062
63 return 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000064}
65
66
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000067struct config_keyword {
68 const char *keyword;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020069 int (*handler)(const char *line, void *var) FAST_FUNC;
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000070 void *var;
71 const char *def;
Mike Frysinger7031f622006-05-08 03:20:50 +000072};
73
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000074static const struct config_keyword keywords[] = {
Denys Vlasenko385b4562010-03-26 10:09:34 +010075 /* 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"},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000079 /* Avoid "max_leases value not sane" warning by setting default
80 * to default_end_ip - default_start_ip + 1: */
Denys Vlasenko385b4562010-03-26 10:09:34 +010081 {"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"},
Denis Vlasenko027271e2008-03-20 12:47:01 +000090 /* keywords with no defaults must be last! */
Denys Vlasenko385b4562010-03-26 10:09:34 +010091 {"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, ""},
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000097};
Denis Vlasenko027271e2008-03-20 12:47:01 +000098enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000099
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000100void FAST_FUNC read_config(const char *file)
Mike Frysinger7031f622006-05-08 03:20:50 +0000101{
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000102 parser_t *parser;
103 const struct config_keyword *k;
104 unsigned i;
105 char *token[2];
Mike Frysinger7031f622006-05-08 03:20:50 +0000106
Denis Vlasenko027271e2008-03-20 12:47:01 +0000107 for (i = 0; i < KWS_WITH_DEFAULTS; i++)
108 keywords[i].handler(keywords[i].def, keywords[i].var);
Mike Frysinger7031f622006-05-08 03:20:50 +0000109
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000110 parser = config_open(file);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000111 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000112 for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
Denys Vlasenko37a658c2010-03-23 15:43:08 +0100113 if (strcasecmp(token[0], k->keyword) == 0) {
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000114 if (!k->handler(token[1], k->var)) {
115 bb_error_msg("can't parse line %u in %s",
116 parser->lineno, file);
Mike Frysinger7031f622006-05-08 03:20:50 +0000117 /* reset back to the default value */
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000118 k->handler(k->def, k->var);
Mike Frysinger7031f622006-05-08 03:20:50 +0000119 }
Denis Vlasenko027271e2008-03-20 12:47:01 +0000120 break;
121 }
122 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000123 }
Denis Vlasenkoeb751292008-07-20 17:58:12 +0000124 config_close(parser);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000125
126 server_config.start_ip = ntohl(server_config.start_ip);
127 server_config.end_ip = ntohl(server_config.end_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000128}
129
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000130void FAST_FUNC write_leases(void)
Mike Frysinger7031f622006-05-08 03:20:50 +0000131{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000132 int fd;
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000133 unsigned i;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000134 leasetime_t curr;
Denis Vlasenko73121c32009-02-01 00:40:45 +0000135 int64_t written_at;
Mike Frysinger7031f622006-05-08 03:20:50 +0000136
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000137 fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
138 if (fd < 0)
Mike Frysinger7031f622006-05-08 03:20:50 +0000139 return;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000140
Denis Vlasenko73121c32009-02-01 00:40:45 +0000141 curr = written_at = time(NULL);
142
143 written_at = hton64(written_at);
144 full_write(fd, &written_at, sizeof(written_at));
Mike Frysinger7031f622006-05-08 03:20:50 +0000145
146 for (i = 0; i < server_config.max_leases; i++) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000147 leasetime_t tmp_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000148
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200149 if (g_leases[i].lease_nip == 0)
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000150 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000151
Denis Vlasenko04158e02009-02-02 10:48:06 +0000152 /* Screw with the time in the struct, for easier writing */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200153 tmp_time = g_leases[i].expires;
Mike Frysinger7031f622006-05-08 03:20:50 +0000154
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200155 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);
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000159
160 /* No error check. If the file gets truncated,
161 * we lose some leases on restart. Oh well. */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200162 full_write(fd, &g_leases[i], sizeof(g_leases[i]));
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000163
Denis Vlasenko04158e02009-02-02 10:48:06 +0000164 /* Then restore it when done */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200165 g_leases[i].expires = tmp_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000166 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000167 close(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000168
169 if (server_config.notify_file) {
Denys Vlasenko7724c762010-03-26 09:32:09 +0100170 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);
Mike Frysinger7031f622006-05-08 03:20:50 +0000175 }
176}
177
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000178void FAST_FUNC read_leases(const char *file)
Mike Frysinger7031f622006-05-08 03:20:50 +0000179{
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200180 struct dyn_lease lease;
Denis Vlasenko04158e02009-02-02 10:48:06 +0000181 int64_t written_at, time_passed;
182 int fd;
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200183#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
184 unsigned i = 0;
185#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000186
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000187 fd = open_or_warn(file, O_RDONLY);
188 if (fd < 0)
Mike Frysinger7031f622006-05-08 03:20:50 +0000189 return;
Mike Frysinger7031f622006-05-08 03:20:50 +0000190
Denis Vlasenko73121c32009-02-01 00:40:45 +0000191 if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
192 goto ret;
193 written_at = ntoh64(written_at);
Denis Vlasenko73121c32009-02-01 00:40:45 +0000194
Denis Vlasenko04158e02009-02-02 10:48:06 +0000195 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
Denis Vlasenko04158e02009-02-02 10:48:06 +0000201 while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200202//FIXME: what if it matches some static lease?
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200203 uint32_t y = ntohl(lease.lease_nip);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000204 if (y >= server_config.start_ip && y <= server_config.end_ip) {
Denis Vlasenko04158e02009-02-02 10:48:06 +0000205 signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed;
Denis Vlasenko73121c32009-02-01 00:40:45 +0000206 if (expires <= 0)
207 continue;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000208 /* NB: add_lease takes "relative time", IOW,
209 * lease duration, not lease deadline. */
Denys Vlasenko95cc8142009-07-07 14:59:30 +0200210 if (add_lease(lease.lease_mac, lease.lease_nip,
211 expires,
212 lease.hostname, sizeof(lease.hostname)
213 ) == 0
214 ) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000215 bb_error_msg("too many leases while loading %s", file);
Mike Frysinger7031f622006-05-08 03:20:50 +0000216 break;
217 }
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200218#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
219 i++;
220#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000221 }
222 }
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200223 log1("Read %d leases", i);
Denis Vlasenko73121c32009-02-01 00:40:45 +0000224 ret:
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000225 close(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000226}