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