"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 | /* |
| 3 | * files.c -- DHCP server file manipulation * |
| 4 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 5 | * |
| 6 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 7 | */ |
| 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" |
| 13 | #include "options.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 14 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 16 | /* on these functions, make sure your datatype matches */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 17 | static int read_ip(const char *line, void *arg) |
| 18 | { |
Denis Vlasenko | e991303 | 2007-02-04 02:41:57 +0000 | [diff] [blame] | 19 | len_and_sockaddr *lsa; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | e991303 | 2007-02-04 02:41:57 +0000 | [diff] [blame] | 21 | lsa = host_and_af2sockaddr(line, 0, AF_INET); |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 22 | if (!lsa) |
| 23 | return 0; |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 24 | *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 25 | free(lsa); |
| 26 | return 1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | static int read_mac(const char *line, void *arg) |
| 30 | { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 31 | return NULL == ether_aton_r(line, (struct ether_addr *)arg); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | |
| 35 | static int read_str(const char *line, void *arg) |
| 36 | { |
| 37 | char **dest = arg; |
| 38 | |
| 39 | free(*dest); |
Denis Vlasenko | 4ebaf10 | 2007-01-19 21:33:19 +0000 | [diff] [blame] | 40 | *dest = xstrdup(line); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | static int read_u32(const char *line, void *arg) |
| 46 | { |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 47 | *(uint32_t*)arg = bb_strtou32(line, NULL, 10); |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 48 | return errno == 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | |
| 52 | static int read_yn(const char *line, void *arg) |
| 53 | { |
| 54 | char *dest = arg; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 56 | if (!strcasecmp("yes", line)) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 57 | *dest = 1; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 58 | return 1; |
| 59 | } |
| 60 | if (!strcasecmp("no", line)) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 61 | *dest = 0; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 62 | return 1; |
| 63 | } |
| 64 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
| 68 | /* find option 'code' in opt_list */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 69 | struct option_set* FAST_FUNC find_option(struct option_set *opt_list, uint8_t code) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 70 | { |
| 71 | while (opt_list && opt_list->data[OPT_CODE] < code) |
| 72 | opt_list = opt_list->next; |
| 73 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 74 | if (opt_list && opt_list->data[OPT_CODE] == code) |
| 75 | return opt_list; |
| 76 | return NULL; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
| 80 | /* add an option to the opt_list */ |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 81 | static void attach_option(struct option_set **opt_list, |
| 82 | const struct dhcp_option *option, char *buffer, int length) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 83 | { |
| 84 | struct option_set *existing, *new, **curr; |
| 85 | |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 86 | existing = find_option(*opt_list, option->code); |
Denis Vlasenko | c4d606e | 2007-02-02 01:16:08 +0000 | [diff] [blame] | 87 | if (!existing) { |
Denis Vlasenko | 35ff746 | 2007-11-28 19:23:12 +0000 | [diff] [blame] | 88 | DEBUG("Attaching option %02x to list", option->code); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 89 | |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 90 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 91 | if ((option->flags & TYPE_MASK) == OPTION_STR1035) |
| 92 | /* reuse buffer and length for RFC1035-formatted string */ |
Denis Vlasenko | c4f12f5 | 2008-05-12 14:35:56 +0000 | [diff] [blame] | 93 | buffer = (char *)dname_enc(NULL, 0, buffer, &length); |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 94 | #endif |
| 95 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 96 | /* make a new option */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 97 | new = xmalloc(sizeof(*new)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 98 | new->data = xmalloc(length + 2); |
| 99 | new->data[OPT_CODE] = option->code; |
| 100 | new->data[OPT_LEN] = length; |
| 101 | memcpy(new->data + 2, buffer, length); |
| 102 | |
| 103 | curr = opt_list; |
| 104 | while (*curr && (*curr)->data[OPT_CODE] < option->code) |
| 105 | curr = &(*curr)->next; |
| 106 | |
| 107 | new->next = *curr; |
| 108 | *curr = new; |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 109 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 110 | if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL) |
| 111 | free(buffer); |
| 112 | #endif |
Denis Vlasenko | c4d606e | 2007-02-02 01:16:08 +0000 | [diff] [blame] | 113 | return; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 114 | } |
Denis Vlasenko | c4d606e | 2007-02-02 01:16:08 +0000 | [diff] [blame] | 115 | |
| 116 | /* add it to an existing option */ |
Denis Vlasenko | 35ff746 | 2007-11-28 19:23:12 +0000 | [diff] [blame] | 117 | DEBUG("Attaching option %02x to existing member of list", option->code); |
Denis Vlasenko | c4d606e | 2007-02-02 01:16:08 +0000 | [diff] [blame] | 118 | if (option->flags & OPTION_LIST) { |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 119 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 120 | if ((option->flags & TYPE_MASK) == OPTION_STR1035) |
| 121 | /* reuse buffer and length for RFC1035-formatted string */ |
Denis Vlasenko | c4f12f5 | 2008-05-12 14:35:56 +0000 | [diff] [blame] | 122 | buffer = (char *)dname_enc(existing->data + 2, |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 123 | existing->data[OPT_LEN], buffer, &length); |
| 124 | #endif |
Denis Vlasenko | c4d606e | 2007-02-02 01:16:08 +0000 | [diff] [blame] | 125 | if (existing->data[OPT_LEN] + length <= 255) { |
| 126 | existing->data = xrealloc(existing->data, |
| 127 | existing->data[OPT_LEN] + length + 3); |
| 128 | if ((option->flags & TYPE_MASK) == OPTION_STRING) { |
Denis Vlasenko | e129f43 | 2007-02-02 01:57:24 +0000 | [diff] [blame] | 129 | /* ' ' can bring us to 256 - bad */ |
Denis Vlasenko | c4d606e | 2007-02-02 01:16:08 +0000 | [diff] [blame] | 130 | if (existing->data[OPT_LEN] + length >= 255) |
| 131 | return; |
| 132 | /* add space separator between STRING options in a list */ |
| 133 | existing->data[existing->data[OPT_LEN] + 2] = ' '; |
| 134 | existing->data[OPT_LEN]++; |
| 135 | } |
| 136 | memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length); |
| 137 | existing->data[OPT_LEN] += length; |
| 138 | } /* else, ignore the data, we could put this in a second option in the future */ |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 139 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 140 | if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL) |
| 141 | free(buffer); |
| 142 | #endif |
Denis Vlasenko | c4d606e | 2007-02-02 01:16:08 +0000 | [diff] [blame] | 143 | } /* else, ignore the new data */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
| 147 | /* read a dhcp option and add it to opt_list */ |
| 148 | static int read_opt(const char *const_line, void *arg) |
| 149 | { |
| 150 | struct option_set **opt_list = arg; |
| 151 | char *opt, *val, *endptr; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 152 | char *line; |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 153 | const struct dhcp_option *option; |
| 154 | int retval, length, idx; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 155 | char buffer[8] ALIGNED(4); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 156 | uint16_t *result_u16 = (uint16_t *) buffer; |
| 157 | uint32_t *result_u32 = (uint32_t *) buffer; |
| 158 | |
| 159 | /* Cheat, the only const line we'll actually get is "" */ |
| 160 | line = (char *) const_line; |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 161 | opt = strtok(line, " \t="); |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 162 | if (!opt) |
| 163 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 164 | |
Denis Vlasenko | 027271e | 2008-03-20 12:47:01 +0000 | [diff] [blame] | 165 | idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 166 | if (idx < 0) |
| 167 | return 0; |
| 168 | option = &dhcp_options[idx]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 169 | |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 170 | retval = 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 171 | do { |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 172 | val = strtok(NULL, ", \t"); |
| 173 | if (!val) break; |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 174 | length = dhcp_option_lengths[option->flags & TYPE_MASK]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 175 | retval = 0; |
| 176 | opt = buffer; /* new meaning for variable opt */ |
| 177 | switch (option->flags & TYPE_MASK) { |
| 178 | case OPTION_IP: |
| 179 | retval = read_ip(val, buffer); |
| 180 | break; |
| 181 | case OPTION_IP_PAIR: |
| 182 | retval = read_ip(val, buffer); |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 183 | val = strtok(NULL, ", \t/-"); |
| 184 | if (!val) |
| 185 | retval = 0; |
| 186 | if (retval) |
| 187 | retval = read_ip(val, buffer + 4); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 188 | break; |
| 189 | case OPTION_STRING: |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 190 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 191 | case OPTION_STR1035: |
| 192 | #endif |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 193 | length = strlen(val); |
| 194 | if (length > 0) { |
| 195 | if (length > 254) length = 254; |
| 196 | opt = val; |
| 197 | retval = 1; |
| 198 | } |
| 199 | break; |
| 200 | case OPTION_BOOLEAN: |
| 201 | retval = read_yn(val, buffer); |
| 202 | break; |
| 203 | case OPTION_U8: |
| 204 | buffer[0] = strtoul(val, &endptr, 0); |
| 205 | retval = (endptr[0] == '\0'); |
| 206 | break; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 207 | /* htonX are macros in older libc's, using temp var |
| 208 | * in code below for safety */ |
| 209 | /* TODO: use bb_strtoX? */ |
| 210 | case OPTION_U16: { |
| 211 | unsigned long tmp = strtoul(val, &endptr, 0); |
| 212 | *result_u16 = htons(tmp); |
| 213 | retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/); |
| 214 | break; |
| 215 | } |
| 216 | case OPTION_S16: { |
| 217 | long tmp = strtol(val, &endptr, 0); |
| 218 | *result_u16 = htons(tmp); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 219 | retval = (endptr[0] == '\0'); |
| 220 | break; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 221 | } |
| 222 | case OPTION_U32: { |
| 223 | unsigned long tmp = strtoul(val, &endptr, 0); |
| 224 | *result_u32 = htonl(tmp); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 225 | retval = (endptr[0] == '\0'); |
| 226 | break; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 227 | } |
| 228 | case OPTION_S32: { |
| 229 | long tmp = strtol(val, &endptr, 0); |
| 230 | *result_u32 = htonl(tmp); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 231 | retval = (endptr[0] == '\0'); |
| 232 | break; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 233 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 234 | default: |
| 235 | break; |
| 236 | } |
| 237 | if (retval) |
| 238 | attach_option(opt_list, option, opt, length); |
| 239 | } while (retval && option->flags & OPTION_LIST); |
| 240 | return retval; |
| 241 | } |
| 242 | |
| 243 | static int read_staticlease(const char *const_line, void *arg) |
| 244 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 245 | char *line; |
| 246 | char *mac_string; |
| 247 | char *ip_string; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 248 | struct ether_addr mac_bytes; |
| 249 | uint32_t ip; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 250 | |
| 251 | /* Read mac */ |
| 252 | line = (char *) const_line; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 253 | mac_string = strtok_r(line, " \t", &line); |
| 254 | read_mac(mac_string, &mac_bytes); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 255 | |
| 256 | /* Read ip */ |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 257 | ip_string = strtok_r(NULL, " \t", &line); |
| 258 | read_ip(ip_string, &ip); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 259 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 260 | addStaticLease(arg, (uint8_t*) &mac_bytes, ip); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 261 | |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 262 | if (ENABLE_UDHCP_DEBUG) printStaticLeases(arg); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 263 | |
| 264 | return 1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 268 | struct config_keyword { |
| 269 | const char *keyword; |
| 270 | int (*handler)(const char *line, void *var); |
| 271 | void *var; |
| 272 | const char *def; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 275 | static const struct config_keyword keywords[] = { |
| 276 | /* keyword handler variable address default */ |
| 277 | {"start", read_ip, &(server_config.start_ip), "192.168.0.20"}, |
| 278 | {"end", read_ip, &(server_config.end_ip), "192.168.0.254"}, |
| 279 | {"interface", read_str, &(server_config.interface), "eth0"}, |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 280 | /* Avoid "max_leases value not sane" warning by setting default |
| 281 | * to default_end_ip - default_start_ip + 1: */ |
| 282 | {"max_leases", read_u32, &(server_config.max_leases), "235"}, |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 283 | // {"remaining", read_yn, &(server_config.remaining), "yes"}, |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 284 | {"auto_time", read_u32, &(server_config.auto_time), "7200"}, |
| 285 | {"decline_time", read_u32, &(server_config.decline_time), "3600"}, |
| 286 | {"conflict_time",read_u32, &(server_config.conflict_time),"3600"}, |
| 287 | {"offer_time", read_u32, &(server_config.offer_time), "60"}, |
| 288 | {"min_lease", read_u32, &(server_config.min_lease), "60"}, |
| 289 | {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE}, |
| 290 | {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"}, |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 291 | {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"}, |
Denis Vlasenko | 027271e | 2008-03-20 12:47:01 +0000 | [diff] [blame] | 292 | /* keywords with no defaults must be last! */ |
| 293 | {"option", read_opt, &(server_config.options), ""}, |
| 294 | {"opt", read_opt, &(server_config.options), ""}, |
| 295 | {"notify_file", read_str, &(server_config.notify_file), ""}, |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 296 | {"sname", read_str, &(server_config.sname), ""}, |
| 297 | {"boot_file", read_str, &(server_config.boot_file), ""}, |
| 298 | {"static_lease", read_staticlease, &(server_config.static_leases), ""}, |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 299 | }; |
Denis Vlasenko | 027271e | 2008-03-20 12:47:01 +0000 | [diff] [blame] | 300 | enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 }; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 301 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 302 | void FAST_FUNC read_config(const char *file) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 303 | { |
Denis Vlasenko | eb75129 | 2008-07-20 17:58:12 +0000 | [diff] [blame] | 304 | parser_t *parser; |
| 305 | const struct config_keyword *k; |
| 306 | unsigned i; |
| 307 | char *token[2]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 308 | |
Denis Vlasenko | 027271e | 2008-03-20 12:47:01 +0000 | [diff] [blame] | 309 | for (i = 0; i < KWS_WITH_DEFAULTS; i++) |
| 310 | keywords[i].handler(keywords[i].def, keywords[i].var); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 311 | |
Denis Vlasenko | eb75129 | 2008-07-20 17:58:12 +0000 | [diff] [blame] | 312 | parser = config_open(file); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 313 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) { |
Denis Vlasenko | eb75129 | 2008-07-20 17:58:12 +0000 | [diff] [blame] | 314 | for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) { |
| 315 | if (!strcasecmp(token[0], k->keyword)) { |
| 316 | if (!k->handler(token[1], k->var)) { |
| 317 | bb_error_msg("can't parse line %u in %s", |
| 318 | parser->lineno, file); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 319 | /* reset back to the default value */ |
Denis Vlasenko | eb75129 | 2008-07-20 17:58:12 +0000 | [diff] [blame] | 320 | k->handler(k->def, k->var); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 321 | } |
Denis Vlasenko | 027271e | 2008-03-20 12:47:01 +0000 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 325 | } |
Denis Vlasenko | eb75129 | 2008-07-20 17:58:12 +0000 | [diff] [blame] | 326 | config_close(parser); |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 327 | |
| 328 | server_config.start_ip = ntohl(server_config.start_ip); |
| 329 | server_config.end_ip = ntohl(server_config.end_ip); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 333 | void FAST_FUNC write_leases(void) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 334 | { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 335 | int fd; |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 336 | unsigned i; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 337 | leasetime_t curr; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 338 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 339 | fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC); |
| 340 | if (fd < 0) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 341 | return; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 342 | |
| 343 | curr = time(NULL); |
| 344 | //TODO: write out current time? Readers need to adjust .expires field |
| 345 | // to account for time between file was written and when it was read back. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 346 | |
| 347 | for (i = 0; i < server_config.max_leases; i++) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 348 | leasetime_t tmp_time; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 349 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 350 | if (leases[i].yiaddr == 0) |
| 351 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 352 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 353 | /* screw with the time in the struct, for easier writing */ |
| 354 | tmp_time = leases[i].expires; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 355 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 356 | //if (server_config.remaining) { |
| 357 | leases[i].expires -= curr; |
| 358 | if ((signed_leasetime_t) leases[i].expires < 0) |
| 359 | leases[i].expires = 0; |
| 360 | //} /* else stick with the time we got */ |
| 361 | leases[i].expires = htonl(leases[i].expires); |
| 362 | |
| 363 | /* No error check. If the file gets truncated, |
| 364 | * we lose some leases on restart. Oh well. */ |
| 365 | full_write(fd, &leases[i], sizeof(leases[i])); |
| 366 | |
| 367 | /* then restore it when done */ |
| 368 | leases[i].expires = tmp_time; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 369 | } |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 370 | close(fd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 371 | |
| 372 | if (server_config.notify_file) { |
Denis Vlasenko | 2e4c3c4 | 2008-04-02 13:04:19 +0000 | [diff] [blame] | 373 | // TODO: vfork-based child creation |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 374 | char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file); |
| 375 | system(cmd); |
| 376 | free(cmd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 381 | void FAST_FUNC read_leases(const char *file) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 382 | { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 383 | int fd; |
Denis Vlasenko | 2e4c3c4 | 2008-04-02 13:04:19 +0000 | [diff] [blame] | 384 | unsigned i; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 385 | // leasetime_t curr; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 386 | struct dhcpOfferedAddr lease; |
| 387 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 388 | fd = open_or_warn(file, O_RDONLY); |
| 389 | if (fd < 0) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 390 | return; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 391 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 392 | // curr = time(NULL); |
Denis Vlasenko | 2e4c3c4 | 2008-04-02 13:04:19 +0000 | [diff] [blame] | 393 | i = 0; |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 394 | while (i < server_config.max_leases |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 395 | && full_read(fd, &lease, sizeof(lease)) == sizeof(lease) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 396 | ) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 397 | /* ADDME: what if it matches some static lease? */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 398 | uint32_t y = ntohl(lease.yiaddr); |
| 399 | if (y >= server_config.start_ip && y <= server_config.end_ip) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 400 | leasetime_t expires = ntohl(lease.expires); |
| 401 | // if (!server_config.remaining) |
| 402 | // expires -= curr; |
| 403 | /* NB: add_lease takes "relative time", IOW, |
| 404 | * lease duration, not lease deadline. */ |
| 405 | if (!(add_lease(lease.chaddr, lease.yiaddr, expires))) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 406 | bb_error_msg("too many leases while loading %s", file); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 407 | break; |
| 408 | } |
| 409 | i++; |
| 410 | } |
| 411 | } |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 412 | DEBUG("Read %d leases", i); |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 413 | close(fd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 414 | } |