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