Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1 | /* |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 2 | * files.c -- DHCP server file manipulation * |
| 3 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 4 | */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 6 | #include <sys/socket.h> |
| 7 | #include <arpa/inet.h> |
| 8 | #include <string.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <time.h> |
| 11 | #include <ctype.h> |
Russ Dill | 7becf59 | 2003-12-16 01:42:18 +0000 | [diff] [blame] | 12 | #include <netdb.h> |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 13 | |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame] | 14 | #include <netinet/ether.h> |
| 15 | #include "static_leases.h" |
| 16 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 17 | #include "dhcpd.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 18 | #include "options.h" |
Paul Fox | b6f7164 | 2005-09-22 19:03:24 +0000 | [diff] [blame] | 19 | #include "files.h" |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 20 | #include "common.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 21 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 22 | /* |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 23 | * Domain names may have 254 chars, and string options can be 254 |
| 24 | * chars long. However, 80 bytes will be enough for most, and won't |
| 25 | * hog up memory. If you have a special application, change it |
| 26 | */ |
| 27 | #define READ_CONFIG_BUF_SIZE 80 |
| 28 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 29 | /* on these functions, make sure you datatype matches */ |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 30 | static int read_ip(const char *line, void *arg) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 31 | { |
| 32 | struct in_addr *addr = arg; |
| 33 | struct hostent *host; |
| 34 | int retval = 1; |
| 35 | |
| 36 | if (!inet_aton(line, addr)) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 37 | if ((host = gethostbyname(line))) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 38 | addr->s_addr = *((unsigned long *) host->h_addr_list[0]); |
| 39 | else retval = 0; |
| 40 | } |
| 41 | return retval; |
| 42 | } |
| 43 | |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame] | 44 | static int read_mac(const char *line, void *arg) |
| 45 | { |
| 46 | uint8_t *mac_bytes = arg; |
| 47 | struct ether_addr *temp_ether_addr; |
| 48 | int retval = 1; |
| 49 | |
| 50 | temp_ether_addr = ether_aton(line); |
| 51 | |
| 52 | if(temp_ether_addr == NULL) |
| 53 | retval = 0; |
| 54 | else |
| 55 | memcpy(mac_bytes, temp_ether_addr, 6); |
| 56 | |
| 57 | return retval; |
| 58 | } |
| 59 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 60 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 61 | static int read_str(const char *line, void *arg) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 62 | { |
| 63 | char **dest = arg; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 64 | |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame] | 65 | if (*dest) free(*dest); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 66 | *dest = strdup(line); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 67 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 72 | static int read_u32(const char *line, void *arg) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 73 | { |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 74 | uint32_t *dest = arg; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 75 | char *endptr; |
| 76 | *dest = strtoul(line, &endptr, 0); |
| 77 | return endptr[0] == '\0'; |
| 78 | } |
| 79 | |
| 80 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 81 | static int read_yn(const char *line, void *arg) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 82 | { |
| 83 | char *dest = arg; |
| 84 | int retval = 1; |
| 85 | |
| 86 | if (!strcasecmp("yes", line)) |
| 87 | *dest = 1; |
| 88 | else if (!strcasecmp("no", line)) |
| 89 | *dest = 0; |
| 90 | else retval = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 91 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 92 | return retval; |
| 93 | } |
| 94 | |
| 95 | |
Paul Fox | b6f7164 | 2005-09-22 19:03:24 +0000 | [diff] [blame] | 96 | /* find option 'code' in opt_list */ |
| 97 | struct option_set *find_option(struct option_set *opt_list, char code) |
| 98 | { |
| 99 | while (opt_list && opt_list->data[OPT_CODE] < code) |
| 100 | opt_list = opt_list->next; |
| 101 | |
| 102 | if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list; |
| 103 | else return NULL; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* add an option to the opt_list */ |
"Vladimir N. Oleynik" | 53ad366 | 2005-09-23 10:44:02 +0000 | [diff] [blame] | 108 | static void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length) |
Paul Fox | b6f7164 | 2005-09-22 19:03:24 +0000 | [diff] [blame] | 109 | { |
| 110 | struct option_set *existing, *new, **curr; |
| 111 | |
| 112 | /* add it to an existing option */ |
| 113 | if ((existing = find_option(*opt_list, option->code))) { |
| 114 | DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name); |
| 115 | if (option->flags & OPTION_LIST) { |
| 116 | if (existing->data[OPT_LEN] + length <= 255) { |
| 117 | existing->data = realloc(existing->data, |
| 118 | existing->data[OPT_LEN] + length + 2); |
| 119 | memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length); |
| 120 | existing->data[OPT_LEN] += length; |
| 121 | } /* else, ignore the data, we could put this in a second option in the future */ |
| 122 | } /* else, ignore the new data */ |
| 123 | } else { |
| 124 | DEBUG(LOG_INFO, "Attaching option %s to list", option->name); |
| 125 | |
| 126 | /* make a new option */ |
| 127 | new = xmalloc(sizeof(struct option_set)); |
| 128 | new->data = xmalloc(length + 2); |
| 129 | new->data[OPT_CODE] = option->code; |
| 130 | new->data[OPT_LEN] = length; |
| 131 | memcpy(new->data + 2, buffer, length); |
| 132 | |
| 133 | curr = opt_list; |
| 134 | while (*curr && (*curr)->data[OPT_CODE] < option->code) |
| 135 | curr = &(*curr)->next; |
| 136 | |
| 137 | new->next = *curr; |
| 138 | *curr = new; |
| 139 | } |
| 140 | } |
| 141 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 142 | /* read a dhcp option and add it to opt_list */ |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 143 | static int read_opt(const char *const_line, void *arg) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 144 | { |
| 145 | struct option_set **opt_list = arg; |
| 146 | char *opt, *val, *endptr; |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 147 | struct dhcp_option *option; |
| 148 | int retval = 0, length; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 149 | char buffer[8]; |
| 150 | char *line; |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 151 | uint16_t *result_u16 = (uint16_t *) buffer; |
| 152 | uint32_t *result_u32 = (uint32_t *) buffer; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 153 | |
| 154 | /* Cheat, the only const line we'll actually get is "" */ |
| 155 | line = (char *) const_line; |
| 156 | if (!(opt = strtok(line, " \t="))) return 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 157 | |
Russ Dill | 4a9e34c | 2003-12-15 22:09:36 +0000 | [diff] [blame] | 158 | for (option = dhcp_options; option->code; option++) |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 159 | if (!strcasecmp(option->name, opt)) |
| 160 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 161 | |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 162 | if (!option->code) return 0; |
| 163 | |
| 164 | do { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 165 | if (!(val = strtok(NULL, ", \t"))) break; |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 166 | length = option_lengths[option->flags & TYPE_MASK]; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 167 | retval = 0; |
| 168 | opt = buffer; /* new meaning for variable opt */ |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 169 | switch (option->flags & TYPE_MASK) { |
| 170 | case OPTION_IP: |
| 171 | retval = read_ip(val, buffer); |
| 172 | break; |
| 173 | case OPTION_IP_PAIR: |
| 174 | retval = read_ip(val, buffer); |
| 175 | if (!(val = strtok(NULL, ", \t/-"))) retval = 0; |
| 176 | if (retval) retval = read_ip(val, buffer + 4); |
| 177 | break; |
| 178 | case OPTION_STRING: |
| 179 | length = strlen(val); |
| 180 | if (length > 0) { |
| 181 | if (length > 254) length = 254; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 182 | opt = val; |
| 183 | retval = 1; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 184 | } |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 185 | break; |
| 186 | case OPTION_BOOLEAN: |
| 187 | retval = read_yn(val, buffer); |
| 188 | break; |
| 189 | case OPTION_U8: |
| 190 | buffer[0] = strtoul(val, &endptr, 0); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 191 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 192 | break; |
| 193 | case OPTION_U16: |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 194 | *result_u16 = htons(strtoul(val, &endptr, 0)); |
| 195 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 196 | break; |
| 197 | case OPTION_S16: |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 198 | *result_u16 = htons(strtol(val, &endptr, 0)); |
| 199 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 200 | break; |
| 201 | case OPTION_U32: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 202 | *result_u32 = htonl(strtoul(val, &endptr, 0)); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 203 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 204 | break; |
| 205 | case OPTION_S32: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 206 | *result_u32 = htonl(strtol(val, &endptr, 0)); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 207 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 208 | break; |
| 209 | default: |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 210 | break; |
| 211 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 212 | if (retval) |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 213 | attach_option(opt_list, option, opt, length); |
| 214 | } while (retval && option->flags & OPTION_LIST); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 215 | return retval; |
| 216 | } |
| 217 | |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame] | 218 | static int read_staticlease(const char *const_line, void *arg) |
| 219 | { |
| 220 | |
| 221 | char *line; |
| 222 | char *mac_string; |
| 223 | char *ip_string; |
| 224 | uint8_t *mac_bytes; |
| 225 | uint32_t *ip; |
| 226 | |
| 227 | |
| 228 | /* Allocate memory for addresses */ |
| 229 | mac_bytes = xmalloc(sizeof(unsigned char) * 8); |
| 230 | ip = xmalloc(sizeof(uint32_t)); |
| 231 | |
| 232 | /* Read mac */ |
| 233 | line = (char *) const_line; |
| 234 | mac_string = strtok(line, " \t"); |
| 235 | read_mac(mac_string, mac_bytes); |
| 236 | |
| 237 | /* Read ip */ |
| 238 | ip_string = strtok(NULL, " \t"); |
| 239 | read_ip(ip_string, ip); |
| 240 | |
| 241 | addStaticLease(arg, mac_bytes, ip); |
| 242 | |
| 243 | #ifdef UDHCP_DEBUG |
| 244 | printStaticLeases(arg); |
| 245 | #endif |
| 246 | |
| 247 | return 1; |
| 248 | |
| 249 | } |
| 250 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 251 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 252 | static const struct config_keyword keywords[] = { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 253 | /* keyword handler variable address default */ |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 254 | {"start", read_ip, &(server_config.start), "192.168.0.20"}, |
| 255 | {"end", read_ip, &(server_config.end), "192.168.0.254"}, |
| 256 | {"interface", read_str, &(server_config.interface), "eth0"}, |
| 257 | {"option", read_opt, &(server_config.options), ""}, |
| 258 | {"opt", read_opt, &(server_config.options), ""}, |
| 259 | {"max_leases", read_u32, &(server_config.max_leases), "254"}, |
| 260 | {"remaining", read_yn, &(server_config.remaining), "yes"}, |
| 261 | {"auto_time", read_u32, &(server_config.auto_time), "7200"}, |
| 262 | {"decline_time",read_u32, &(server_config.decline_time),"3600"}, |
| 263 | {"conflict_time",read_u32,&(server_config.conflict_time),"3600"}, |
| 264 | {"offer_time", read_u32, &(server_config.offer_time), "60"}, |
| 265 | {"min_lease", read_u32, &(server_config.min_lease), "60"}, |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 266 | {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE}, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 267 | {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"}, |
| 268 | {"notify_file", read_str, &(server_config.notify_file), ""}, |
| 269 | {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"}, |
| 270 | {"sname", read_str, &(server_config.sname), ""}, |
| 271 | {"boot_file", read_str, &(server_config.boot_file), ""}, |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame] | 272 | {"static_lease",read_staticlease, &(server_config.static_leases), ""}, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 273 | /*ADDME: static lease */ |
| 274 | {"", NULL, NULL, ""} |
| 275 | }; |
| 276 | |
| 277 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 278 | int read_config(const char *file) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 279 | { |
| 280 | FILE *in; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 281 | char buffer[READ_CONFIG_BUF_SIZE], *token, *line; |
| 282 | #ifdef UDHCP_DEBUG |
| 283 | char orig[READ_CONFIG_BUF_SIZE]; |
| 284 | #endif |
| 285 | int i, lm = 0; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 286 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 287 | for (i = 0; keywords[i].keyword[0]; i++) |
| 288 | if (keywords[i].def[0]) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 289 | keywords[i].handler(keywords[i].def, keywords[i].var); |
| 290 | |
| 291 | if (!(in = fopen(file, "r"))) { |
| 292 | LOG(LOG_ERR, "unable to open config file: %s", file); |
| 293 | return 0; |
| 294 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 295 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 296 | while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 297 | lm++; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 298 | if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0'; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 299 | #ifdef UDHCP_DEBUG |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 300 | strcpy(orig, buffer); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 301 | #endif |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 302 | if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0'; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 303 | |
| 304 | if (!(token = strtok(buffer, " \t"))) continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 305 | if (!(line = strtok(NULL, ""))) continue; |
| 306 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 307 | /* eat leading whitespace */ |
| 308 | line = line + strspn(line, " \t="); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 309 | /* eat trailing whitespace */ |
| 310 | for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--); |
| 311 | line[i] = '\0'; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 312 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 313 | for (i = 0; keywords[i].keyword[0]; i++) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 314 | if (!strcasecmp(token, keywords[i].keyword)) |
| 315 | if (!keywords[i].handler(line, keywords[i].var)) { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 316 | LOG(LOG_ERR, "Failure parsing line %d of %s", lm, file); |
| 317 | DEBUG(LOG_ERR, "unable to parse '%s'", orig); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 318 | /* reset back to the default value */ |
| 319 | keywords[i].handler(keywords[i].def, keywords[i].var); |
| 320 | } |
| 321 | } |
| 322 | fclose(in); |
| 323 | return 1; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | void write_leases(void) |
| 328 | { |
| 329 | FILE *fp; |
| 330 | unsigned int i; |
| 331 | char buf[255]; |
| 332 | time_t curr = time(0); |
Russ Dill | 8f43126 | 2003-12-16 01:29:40 +0000 | [diff] [blame] | 333 | unsigned long tmp_time; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 334 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 335 | if (!(fp = fopen(server_config.lease_file, "w"))) { |
| 336 | LOG(LOG_ERR, "Unable to open %s for writing", server_config.lease_file); |
| 337 | return; |
| 338 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 339 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 340 | for (i = 0; i < server_config.max_leases; i++) { |
| 341 | if (leases[i].yiaddr != 0) { |
Russ Dill | 8f43126 | 2003-12-16 01:29:40 +0000 | [diff] [blame] | 342 | |
| 343 | /* screw with the time in the struct, for easier writing */ |
| 344 | tmp_time = leases[i].expires; |
| 345 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 346 | if (server_config.remaining) { |
| 347 | if (lease_expired(&(leases[i]))) |
Russ Dill | 8f43126 | 2003-12-16 01:29:40 +0000 | [diff] [blame] | 348 | leases[i].expires = 0; |
| 349 | else leases[i].expires -= curr; |
| 350 | } /* else stick with the time we got */ |
| 351 | leases[i].expires = htonl(leases[i].expires); |
Russ Dill | a3170d5 | 2003-12-16 01:33:38 +0000 | [diff] [blame] | 352 | fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp); |
Russ Dill | 8f43126 | 2003-12-16 01:29:40 +0000 | [diff] [blame] | 353 | |
| 354 | /* Then restore it when done. */ |
| 355 | leases[i].expires = tmp_time; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | fclose(fp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 359 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 360 | if (server_config.notify_file) { |
| 361 | sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file); |
| 362 | system(buf); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 367 | void read_leases(const char *file) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 368 | { |
| 369 | FILE *fp; |
| 370 | unsigned int i = 0; |
| 371 | struct dhcpOfferedAddr lease; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 372 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 373 | if (!(fp = fopen(file, "r"))) { |
| 374 | LOG(LOG_ERR, "Unable to open %s for reading", file); |
| 375 | return; |
| 376 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 377 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 378 | while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) { |
| 379 | /* ADDME: is it a static lease */ |
| 380 | if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) { |
| 381 | lease.expires = ntohl(lease.expires); |
| 382 | if (!server_config.remaining) lease.expires -= time(0); |
| 383 | if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) { |
| 384 | LOG(LOG_WARNING, "Too many leases while loading %s\n", file); |
| 385 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 386 | } |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 387 | i++; |
| 388 | } |
| 389 | } |
| 390 | DEBUG(LOG_INFO, "Read %d leases", i); |
| 391 | fclose(fp); |
| 392 | } |