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