"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; |
| 22 | *(uint32_t*)arg = lsa->sin.sin_addr.s_addr; |
| 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 */ |
| 74 | struct option_set *find_option(struct option_set *opt_list, char code) |
| 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 | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 93 | DEBUG("Attaching option %s to list", option->name); |
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 */ |
| 122 | DEBUG("Attaching option %s to existing member of list", option->name); |
| 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; |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 157 | const struct dhcp_option *option; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 158 | int retval = 0, length; |
| 159 | char buffer[8]; |
| 160 | char *line; |
| 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="); |
| 167 | if (!opt) return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 168 | |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 169 | option = dhcp_options; |
| 170 | while (1) { |
| 171 | if (!option->code) |
| 172 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 173 | if (!strcasecmp(option->name, opt)) |
| 174 | break; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 175 | option++; |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 176 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 177 | |
| 178 | do { |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 179 | val = strtok(NULL, ", \t"); |
| 180 | if (!val) break; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 181 | length = option_lengths[option->flags & TYPE_MASK]; |
| 182 | retval = 0; |
| 183 | opt = buffer; /* new meaning for variable opt */ |
| 184 | switch (option->flags & TYPE_MASK) { |
| 185 | case OPTION_IP: |
| 186 | retval = read_ip(val, buffer); |
| 187 | break; |
| 188 | case OPTION_IP_PAIR: |
| 189 | retval = read_ip(val, buffer); |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 190 | val = strtok(NULL, ", \t/-"); |
| 191 | if (!val) |
| 192 | retval = 0; |
| 193 | if (retval) |
| 194 | retval = read_ip(val, buffer + 4); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 195 | break; |
| 196 | case OPTION_STRING: |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 197 | #if ENABLE_FEATURE_RFC3397 |
| 198 | case OPTION_STR1035: |
| 199 | #endif |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 200 | length = strlen(val); |
| 201 | if (length > 0) { |
| 202 | if (length > 254) length = 254; |
| 203 | opt = val; |
| 204 | retval = 1; |
| 205 | } |
| 206 | break; |
| 207 | case OPTION_BOOLEAN: |
| 208 | retval = read_yn(val, buffer); |
| 209 | break; |
| 210 | case OPTION_U8: |
| 211 | buffer[0] = strtoul(val, &endptr, 0); |
| 212 | retval = (endptr[0] == '\0'); |
| 213 | break; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 214 | /* htonX are macros in older libc's, using temp var |
| 215 | * in code below for safety */ |
| 216 | /* TODO: use bb_strtoX? */ |
| 217 | case OPTION_U16: { |
| 218 | unsigned long tmp = strtoul(val, &endptr, 0); |
| 219 | *result_u16 = htons(tmp); |
| 220 | retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/); |
| 221 | break; |
| 222 | } |
| 223 | case OPTION_S16: { |
| 224 | long tmp = strtol(val, &endptr, 0); |
| 225 | *result_u16 = htons(tmp); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 226 | retval = (endptr[0] == '\0'); |
| 227 | break; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 228 | } |
| 229 | case OPTION_U32: { |
| 230 | unsigned long tmp = strtoul(val, &endptr, 0); |
| 231 | *result_u32 = htonl(tmp); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 232 | retval = (endptr[0] == '\0'); |
| 233 | break; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 234 | } |
| 235 | case OPTION_S32: { |
| 236 | long tmp = strtol(val, &endptr, 0); |
| 237 | *result_u32 = htonl(tmp); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 238 | retval = (endptr[0] == '\0'); |
| 239 | break; |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 240 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 241 | default: |
| 242 | break; |
| 243 | } |
| 244 | if (retval) |
| 245 | attach_option(opt_list, option, opt, length); |
| 246 | } while (retval && option->flags & OPTION_LIST); |
| 247 | return retval; |
| 248 | } |
| 249 | |
| 250 | static int read_staticlease(const char *const_line, void *arg) |
| 251 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 252 | char *line; |
| 253 | char *mac_string; |
| 254 | char *ip_string; |
| 255 | uint8_t *mac_bytes; |
| 256 | uint32_t *ip; |
| 257 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 258 | /* Allocate memory for addresses */ |
| 259 | mac_bytes = xmalloc(sizeof(unsigned char) * 8); |
| 260 | ip = xmalloc(sizeof(uint32_t)); |
| 261 | |
| 262 | /* Read mac */ |
| 263 | line = (char *) const_line; |
| 264 | mac_string = strtok(line, " \t"); |
| 265 | read_mac(mac_string, mac_bytes); |
| 266 | |
| 267 | /* Read ip */ |
| 268 | ip_string = strtok(NULL, " \t"); |
| 269 | read_ip(ip_string, ip); |
| 270 | |
| 271 | addStaticLease(arg, mac_bytes, ip); |
| 272 | |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 273 | if (ENABLE_FEATURE_UDHCP_DEBUG) printStaticLeases(arg); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 274 | |
| 275 | return 1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 279 | struct config_keyword { |
| 280 | const char *keyword; |
| 281 | int (*handler)(const char *line, void *var); |
| 282 | void *var; |
| 283 | const char *def; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 284 | }; |
| 285 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 286 | static const struct config_keyword keywords[] = { |
| 287 | /* keyword handler variable address default */ |
| 288 | {"start", read_ip, &(server_config.start_ip), "192.168.0.20"}, |
| 289 | {"end", read_ip, &(server_config.end_ip), "192.168.0.254"}, |
| 290 | {"interface", read_str, &(server_config.interface), "eth0"}, |
| 291 | {"option", read_opt, &(server_config.options), ""}, |
| 292 | {"opt", read_opt, &(server_config.options), ""}, |
| 293 | /* Avoid "max_leases value not sane" warning by setting default |
| 294 | * to default_end_ip - default_start_ip + 1: */ |
| 295 | {"max_leases", read_u32, &(server_config.max_leases), "235"}, |
| 296 | {"remaining", read_yn, &(server_config.remaining), "yes"}, |
| 297 | {"auto_time", read_u32, &(server_config.auto_time), "7200"}, |
| 298 | {"decline_time", read_u32, &(server_config.decline_time), "3600"}, |
| 299 | {"conflict_time",read_u32, &(server_config.conflict_time),"3600"}, |
| 300 | {"offer_time", read_u32, &(server_config.offer_time), "60"}, |
| 301 | {"min_lease", read_u32, &(server_config.min_lease), "60"}, |
| 302 | {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE}, |
| 303 | {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"}, |
| 304 | {"notify_file", read_str, &(server_config.notify_file), ""}, |
| 305 | {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"}, |
| 306 | {"sname", read_str, &(server_config.sname), ""}, |
| 307 | {"boot_file", read_str, &(server_config.boot_file), ""}, |
| 308 | {"static_lease", read_staticlease, &(server_config.static_leases), ""}, |
| 309 | /* ADDME: static lease */ |
| 310 | }; |
| 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 | |
| 320 | int read_config(const char *file) |
| 321 | { |
| 322 | FILE *in; |
| 323 | char buffer[READ_CONFIG_BUF_SIZE], *token, *line; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 324 | int i, lm = 0; |
| 325 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 326 | for (i = 0; i < ARRAY_SIZE(keywords); i++) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 327 | if (keywords[i].def[0]) |
| 328 | keywords[i].handler(keywords[i].def, keywords[i].var); |
| 329 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 330 | in = fopen_or_warn(file, "r"); |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 331 | if (!in) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) { |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 336 | char debug_orig[READ_CONFIG_BUF_SIZE]; |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 337 | char *p; |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 338 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 339 | lm++; |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 340 | p = strchr(buffer, '\n'); |
| 341 | if (p) *p = '\0'; |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 342 | if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer); |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 343 | p = strchr(buffer, '#'); |
| 344 | if (p) *p = '\0'; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 345 | |
| 346 | if (!(token = strtok(buffer, " \t"))) continue; |
| 347 | if (!(line = strtok(NULL, ""))) continue; |
| 348 | |
| 349 | /* eat leading whitespace */ |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 350 | line = skip_whitespace(line); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 351 | /* eat trailing whitespace */ |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 352 | i = strlen(line) - 1; |
| 353 | while (i >= 0 && isspace(line[i])) |
| 354 | line[i--] = '\0'; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 355 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 356 | for (i = 0; i < ARRAY_SIZE(keywords); i++) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 357 | if (!strcasecmp(token, keywords[i].keyword)) |
| 358 | if (!keywords[i].handler(line, keywords[i].var)) { |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 359 | bb_error_msg("cannot parse line %d of %s", lm, file); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 360 | if (ENABLE_FEATURE_UDHCP_DEBUG) |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 361 | bb_error_msg("cannot parse '%s'", debug_orig); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 362 | /* reset back to the default value */ |
| 363 | keywords[i].handler(keywords[i].def, keywords[i].var); |
| 364 | } |
| 365 | } |
| 366 | fclose(in); |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 367 | |
| 368 | server_config.start_ip = ntohl(server_config.start_ip); |
| 369 | server_config.end_ip = ntohl(server_config.end_ip); |
| 370 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | |
| 375 | void write_leases(void) |
| 376 | { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 377 | int fp; |
| 378 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 379 | time_t curr = time(0); |
| 380 | unsigned long tmp_time; |
| 381 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 382 | fp = open3_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 383 | if (fp < 0) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 384 | return; |
| 385 | } |
| 386 | |
| 387 | for (i = 0; i < server_config.max_leases; i++) { |
| 388 | if (leases[i].yiaddr != 0) { |
| 389 | |
| 390 | /* screw with the time in the struct, for easier writing */ |
| 391 | tmp_time = leases[i].expires; |
| 392 | |
| 393 | if (server_config.remaining) { |
| 394 | if (lease_expired(&(leases[i]))) |
| 395 | leases[i].expires = 0; |
| 396 | else leases[i].expires -= curr; |
| 397 | } /* else stick with the time we got */ |
| 398 | leases[i].expires = htonl(leases[i].expires); |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 399 | // FIXME: error check?? |
| 400 | full_write(fp, &leases[i], sizeof(leases[i])); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 401 | |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 402 | /* then restore it when done */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 403 | leases[i].expires = tmp_time; |
| 404 | } |
| 405 | } |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 406 | close(fp); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 407 | |
| 408 | if (server_config.notify_file) { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 409 | char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file); |
| 410 | system(cmd); |
| 411 | free(cmd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
| 415 | |
| 416 | void read_leases(const char *file) |
| 417 | { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 418 | int fp; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 419 | unsigned int i = 0; |
| 420 | struct dhcpOfferedAddr lease; |
| 421 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 422 | fp = open_or_warn(file, O_RDONLY); |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 423 | if (fp < 0) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 427 | while (i < server_config.max_leases |
| 428 | && full_read(fp, &lease, sizeof(lease)) == sizeof(lease) |
| 429 | ) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 430 | /* ADDME: is it a static lease */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 431 | uint32_t y = ntohl(lease.yiaddr); |
| 432 | if (y >= server_config.start_ip && y <= server_config.end_ip) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 433 | lease.expires = ntohl(lease.expires); |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 434 | if (!server_config.remaining) |
| 435 | lease.expires -= time(0); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 436 | if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 437 | bb_error_msg("too many leases while loading %s", file); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 438 | break; |
| 439 | } |
| 440 | i++; |
| 441 | } |
| 442 | } |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 443 | DEBUG("Read %d leases", i); |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 444 | close(fp); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 445 | } |