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