"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 3 | * DHCP server config and lease file manipulation |
| 4 | * |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 5 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 8 | */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 9 | #include <netinet/ether.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 10 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 11 | #include "common.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 12 | #include "dhcpd.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 14 | /* on these functions, make sure your datatype matches */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 15 | static int FAST_FUNC read_str(const char *line, void *arg) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 16 | { |
| 17 | char **dest = arg; |
| 18 | |
| 19 | free(*dest); |
Denis Vlasenko | 4ebaf10 | 2007-01-19 21:33:19 +0000 | [diff] [blame] | 20 | *dest = xstrdup(line); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 21 | return 1; |
| 22 | } |
| 23 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 24 | static int FAST_FUNC read_u32(const char *line, void *arg) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 25 | { |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 26 | *(uint32_t*)arg = bb_strtou32(line, NULL, 10); |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 27 | return errno == 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 30 | static int FAST_FUNC read_staticlease(const char *const_line, void *arg) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 31 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 32 | char *line; |
| 33 | char *mac_string; |
| 34 | char *ip_string; |
Denys Vlasenko | 37a658c | 2010-03-23 15:43:08 +0100 | [diff] [blame] | 35 | struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */ |
| 36 | uint32_t nip; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 37 | |
| 38 | /* Read mac */ |
| 39 | line = (char *) const_line; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 40 | mac_string = strtok_r(line, " \t", &line); |
Denys Vlasenko | 37a658c | 2010-03-23 15:43:08 +0100 | [diff] [blame] | 41 | if (!mac_string || !ether_aton_r(mac_string, &mac_bytes)) |
| 42 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 43 | |
| 44 | /* Read ip */ |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 45 | ip_string = strtok_r(NULL, " \t", &line); |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 46 | if (!ip_string || !udhcp_str2nip(ip_string, &nip)) |
Denys Vlasenko | 37a658c | 2010-03-23 15:43:08 +0100 | [diff] [blame] | 47 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 48 | |
Denys Vlasenko | 37a658c | 2010-03-23 15:43:08 +0100 | [diff] [blame] | 49 | add_static_lease(arg, (uint8_t*) &mac_bytes, nip); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 50 | |
Denys Vlasenko | 799d00e | 2009-06-17 13:44:04 +0200 | [diff] [blame] | 51 | log_static_leases(arg); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 52 | |
| 53 | return 1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 57 | struct config_keyword { |
| 58 | const char *keyword; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 59 | int (*handler)(const char *line, void *var) FAST_FUNC; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 60 | void *var; |
| 61 | const char *def; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 64 | static const struct config_keyword keywords[] = { |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 65 | /* keyword handler variable address default */ |
| 66 | {"start" , udhcp_str2nip , &server_config.start_ip , "192.168.0.20"}, |
| 67 | {"end" , udhcp_str2nip , &server_config.end_ip , "192.168.0.254"}, |
| 68 | {"interface" , read_str , &server_config.interface , "eth0"}, |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 69 | /* Avoid "max_leases value not sane" warning by setting default |
| 70 | * to default_end_ip - default_start_ip + 1: */ |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 71 | {"max_leases" , read_u32 , &server_config.max_leases , "235"}, |
| 72 | {"auto_time" , read_u32 , &server_config.auto_time , "7200"}, |
| 73 | {"decline_time" , read_u32 , &server_config.decline_time , "3600"}, |
| 74 | {"conflict_time", read_u32 , &server_config.conflict_time, "3600"}, |
| 75 | {"offer_time" , read_u32 , &server_config.offer_time , "60"}, |
| 76 | {"min_lease" , read_u32 , &server_config.min_lease_sec, "60"}, |
| 77 | {"lease_file" , read_str , &server_config.lease_file , LEASES_FILE}, |
| 78 | {"pidfile" , read_str , &server_config.pidfile , "/var/run/udhcpd.pid"}, |
| 79 | {"siaddr" , udhcp_str2nip , &server_config.siaddr_nip , "0.0.0.0"}, |
Denis Vlasenko | 027271e | 2008-03-20 12:47:01 +0000 | [diff] [blame] | 80 | /* keywords with no defaults must be last! */ |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 81 | {"option" , udhcp_str2optset, &server_config.options , ""}, |
| 82 | {"opt" , udhcp_str2optset, &server_config.options , ""}, |
Denys Vlasenko | 7945808 | 2011-05-03 16:46:47 +0200 | [diff] [blame] | 83 | {"notify_file" , read_str , &server_config.notify_file , NULL}, |
| 84 | {"sname" , read_str , &server_config.sname , NULL}, |
| 85 | {"boot_file" , read_str , &server_config.boot_file , NULL}, |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 86 | {"static_lease" , read_staticlease, &server_config.static_leases, ""}, |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 87 | }; |
Denis Vlasenko | 027271e | 2008-03-20 12:47:01 +0000 | [diff] [blame] | 88 | enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 }; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 89 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 90 | void FAST_FUNC read_config(const char *file) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 91 | { |
Denis Vlasenko | eb75129 | 2008-07-20 17:58:12 +0000 | [diff] [blame] | 92 | parser_t *parser; |
| 93 | const struct config_keyword *k; |
| 94 | unsigned i; |
| 95 | char *token[2]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 96 | |
Denis Vlasenko | 027271e | 2008-03-20 12:47:01 +0000 | [diff] [blame] | 97 | for (i = 0; i < KWS_WITH_DEFAULTS; i++) |
| 98 | keywords[i].handler(keywords[i].def, keywords[i].var); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 99 | |
Denis Vlasenko | eb75129 | 2008-07-20 17:58:12 +0000 | [diff] [blame] | 100 | parser = config_open(file); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 101 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) { |
Denis Vlasenko | eb75129 | 2008-07-20 17:58:12 +0000 | [diff] [blame] | 102 | for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) { |
Denys Vlasenko | 37a658c | 2010-03-23 15:43:08 +0100 | [diff] [blame] | 103 | if (strcasecmp(token[0], k->keyword) == 0) { |
Denis Vlasenko | eb75129 | 2008-07-20 17:58:12 +0000 | [diff] [blame] | 104 | if (!k->handler(token[1], k->var)) { |
| 105 | bb_error_msg("can't parse line %u in %s", |
| 106 | parser->lineno, file); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 107 | /* reset back to the default value */ |
Denis Vlasenko | eb75129 | 2008-07-20 17:58:12 +0000 | [diff] [blame] | 108 | k->handler(k->def, k->var); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 109 | } |
Denis Vlasenko | 027271e | 2008-03-20 12:47:01 +0000 | [diff] [blame] | 110 | break; |
| 111 | } |
| 112 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 113 | } |
Denis Vlasenko | eb75129 | 2008-07-20 17:58:12 +0000 | [diff] [blame] | 114 | config_close(parser); |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 115 | |
| 116 | server_config.start_ip = ntohl(server_config.start_ip); |
| 117 | server_config.end_ip = ntohl(server_config.end_ip); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 120 | void FAST_FUNC write_leases(void) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 121 | { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 122 | int fd; |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 123 | unsigned i; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 124 | leasetime_t curr; |
Denis Vlasenko | 73121c3 | 2009-02-01 00:40:45 +0000 | [diff] [blame] | 125 | int64_t written_at; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 126 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 127 | fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC); |
| 128 | if (fd < 0) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 129 | return; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 130 | |
Denis Vlasenko | 73121c3 | 2009-02-01 00:40:45 +0000 | [diff] [blame] | 131 | curr = written_at = time(NULL); |
| 132 | |
Denys Vlasenko | 9ff50b8 | 2010-10-18 11:40:26 +0200 | [diff] [blame] | 133 | written_at = SWAP_BE64(written_at); |
Denis Vlasenko | 73121c3 | 2009-02-01 00:40:45 +0000 | [diff] [blame] | 134 | full_write(fd, &written_at, sizeof(written_at)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 135 | |
| 136 | for (i = 0; i < server_config.max_leases; i++) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 137 | leasetime_t tmp_time; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 138 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 139 | if (g_leases[i].lease_nip == 0) |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 140 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 141 | |
Denis Vlasenko | 04158e0 | 2009-02-02 10:48:06 +0000 | [diff] [blame] | 142 | /* Screw with the time in the struct, for easier writing */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 143 | tmp_time = g_leases[i].expires; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 144 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 145 | g_leases[i].expires -= curr; |
| 146 | if ((signed_leasetime_t) g_leases[i].expires < 0) |
| 147 | g_leases[i].expires = 0; |
| 148 | g_leases[i].expires = htonl(g_leases[i].expires); |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 149 | |
| 150 | /* No error check. If the file gets truncated, |
| 151 | * we lose some leases on restart. Oh well. */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 152 | full_write(fd, &g_leases[i], sizeof(g_leases[i])); |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 153 | |
Denis Vlasenko | 04158e0 | 2009-02-02 10:48:06 +0000 | [diff] [blame] | 154 | /* Then restore it when done */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 155 | g_leases[i].expires = tmp_time; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 156 | } |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 157 | close(fd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 158 | |
| 159 | if (server_config.notify_file) { |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 160 | char *argv[3]; |
| 161 | argv[0] = server_config.notify_file; |
| 162 | argv[1] = server_config.lease_file; |
| 163 | argv[2] = NULL; |
| 164 | spawn_and_wait(argv); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 168 | void FAST_FUNC read_leases(const char *file) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 169 | { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 170 | struct dyn_lease lease; |
Denis Vlasenko | 04158e0 | 2009-02-02 10:48:06 +0000 | [diff] [blame] | 171 | int64_t written_at, time_passed; |
| 172 | int fd; |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 173 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 174 | unsigned i = 0; |
| 175 | #endif |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 176 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 177 | fd = open_or_warn(file, O_RDONLY); |
| 178 | if (fd < 0) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 179 | return; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 180 | |
Denis Vlasenko | 73121c3 | 2009-02-01 00:40:45 +0000 | [diff] [blame] | 181 | if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at)) |
| 182 | goto ret; |
Denys Vlasenko | 9ff50b8 | 2010-10-18 11:40:26 +0200 | [diff] [blame] | 183 | written_at = SWAP_BE64(written_at); |
Denis Vlasenko | 73121c3 | 2009-02-01 00:40:45 +0000 | [diff] [blame] | 184 | |
Denis Vlasenko | 04158e0 | 2009-02-02 10:48:06 +0000 | [diff] [blame] | 185 | time_passed = time(NULL) - written_at; |
| 186 | /* Strange written_at, or lease file from old version of udhcpd |
| 187 | * which had no "written_at" field? */ |
| 188 | if ((uint64_t)time_passed > 12 * 60 * 60) |
| 189 | goto ret; |
| 190 | |
Denis Vlasenko | 04158e0 | 2009-02-02 10:48:06 +0000 | [diff] [blame] | 191 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 192 | //FIXME: what if it matches some static lease? |
Denys Vlasenko | 1d924f5 | 2009-06-16 10:23:01 +0200 | [diff] [blame] | 193 | uint32_t y = ntohl(lease.lease_nip); |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 194 | if (y >= server_config.start_ip && y <= server_config.end_ip) { |
Denis Vlasenko | 04158e0 | 2009-02-02 10:48:06 +0000 | [diff] [blame] | 195 | signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed; |
Denis Vlasenko | 73121c3 | 2009-02-01 00:40:45 +0000 | [diff] [blame] | 196 | if (expires <= 0) |
| 197 | continue; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 198 | /* NB: add_lease takes "relative time", IOW, |
| 199 | * lease duration, not lease deadline. */ |
Denys Vlasenko | 95cc814 | 2009-07-07 14:59:30 +0200 | [diff] [blame] | 200 | if (add_lease(lease.lease_mac, lease.lease_nip, |
| 201 | expires, |
| 202 | lease.hostname, sizeof(lease.hostname) |
| 203 | ) == 0 |
| 204 | ) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 205 | bb_error_msg("too many leases while loading %s", file); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 206 | break; |
| 207 | } |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 208 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 209 | i++; |
| 210 | #endif |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 213 | log1("Read %d leases", i); |
Denis Vlasenko | 73121c3 | 2009-02-01 00:40:45 +0000 | [diff] [blame] | 214 | ret: |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 215 | close(fd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 216 | } |