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" |
| 18 | #include "files.h" |
| 19 | #include "options.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 | |
| 96 | /* read a dhcp option and add it to opt_list */ |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 97 | static int read_opt(const char *const_line, void *arg) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 98 | { |
| 99 | struct option_set **opt_list = arg; |
| 100 | char *opt, *val, *endptr; |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 101 | struct dhcp_option *option; |
| 102 | int retval = 0, length; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 103 | char buffer[8]; |
| 104 | char *line; |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 105 | uint16_t *result_u16 = (uint16_t *) buffer; |
| 106 | uint32_t *result_u32 = (uint32_t *) buffer; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 107 | |
| 108 | /* Cheat, the only const line we'll actually get is "" */ |
| 109 | line = (char *) const_line; |
| 110 | if (!(opt = strtok(line, " \t="))) return 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 111 | |
Russ Dill | 4a9e34c | 2003-12-15 22:09:36 +0000 | [diff] [blame] | 112 | for (option = dhcp_options; option->code; option++) |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 113 | if (!strcasecmp(option->name, opt)) |
| 114 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 115 | |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 116 | if (!option->code) return 0; |
| 117 | |
| 118 | do { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 119 | if (!(val = strtok(NULL, ", \t"))) break; |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 120 | length = option_lengths[option->flags & TYPE_MASK]; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 121 | retval = 0; |
| 122 | opt = buffer; /* new meaning for variable opt */ |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 123 | switch (option->flags & TYPE_MASK) { |
| 124 | case OPTION_IP: |
| 125 | retval = read_ip(val, buffer); |
| 126 | break; |
| 127 | case OPTION_IP_PAIR: |
| 128 | retval = read_ip(val, buffer); |
| 129 | if (!(val = strtok(NULL, ", \t/-"))) retval = 0; |
| 130 | if (retval) retval = read_ip(val, buffer + 4); |
| 131 | break; |
| 132 | case OPTION_STRING: |
| 133 | length = strlen(val); |
| 134 | if (length > 0) { |
| 135 | if (length > 254) length = 254; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 136 | opt = val; |
| 137 | retval = 1; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 138 | } |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 139 | break; |
| 140 | case OPTION_BOOLEAN: |
| 141 | retval = read_yn(val, buffer); |
| 142 | break; |
| 143 | case OPTION_U8: |
| 144 | buffer[0] = strtoul(val, &endptr, 0); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 145 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 146 | break; |
| 147 | case OPTION_U16: |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 148 | *result_u16 = htons(strtoul(val, &endptr, 0)); |
| 149 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 150 | break; |
| 151 | case OPTION_S16: |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 152 | *result_u16 = htons(strtol(val, &endptr, 0)); |
| 153 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 154 | break; |
| 155 | case OPTION_U32: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 156 | *result_u32 = htonl(strtoul(val, &endptr, 0)); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 157 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 158 | break; |
| 159 | case OPTION_S32: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 160 | *result_u32 = htonl(strtol(val, &endptr, 0)); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 161 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 162 | break; |
| 163 | default: |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 164 | break; |
| 165 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 166 | if (retval) |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 167 | attach_option(opt_list, option, opt, length); |
| 168 | } while (retval && option->flags & OPTION_LIST); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 169 | return retval; |
| 170 | } |
| 171 | |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame] | 172 | static int read_staticlease(const char *const_line, void *arg) |
| 173 | { |
| 174 | |
| 175 | char *line; |
| 176 | char *mac_string; |
| 177 | char *ip_string; |
| 178 | uint8_t *mac_bytes; |
| 179 | uint32_t *ip; |
| 180 | |
| 181 | |
| 182 | /* Allocate memory for addresses */ |
| 183 | mac_bytes = xmalloc(sizeof(unsigned char) * 8); |
| 184 | ip = xmalloc(sizeof(uint32_t)); |
| 185 | |
| 186 | /* Read mac */ |
| 187 | line = (char *) const_line; |
| 188 | mac_string = strtok(line, " \t"); |
| 189 | read_mac(mac_string, mac_bytes); |
| 190 | |
| 191 | /* Read ip */ |
| 192 | ip_string = strtok(NULL, " \t"); |
| 193 | read_ip(ip_string, ip); |
| 194 | |
| 195 | addStaticLease(arg, mac_bytes, ip); |
| 196 | |
| 197 | #ifdef UDHCP_DEBUG |
| 198 | printStaticLeases(arg); |
| 199 | #endif |
| 200 | |
| 201 | return 1; |
| 202 | |
| 203 | } |
| 204 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 205 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 206 | static const struct config_keyword keywords[] = { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 207 | /* keyword handler variable address default */ |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 208 | {"start", read_ip, &(server_config.start), "192.168.0.20"}, |
| 209 | {"end", read_ip, &(server_config.end), "192.168.0.254"}, |
| 210 | {"interface", read_str, &(server_config.interface), "eth0"}, |
| 211 | {"option", read_opt, &(server_config.options), ""}, |
| 212 | {"opt", read_opt, &(server_config.options), ""}, |
| 213 | {"max_leases", read_u32, &(server_config.max_leases), "254"}, |
| 214 | {"remaining", read_yn, &(server_config.remaining), "yes"}, |
| 215 | {"auto_time", read_u32, &(server_config.auto_time), "7200"}, |
| 216 | {"decline_time",read_u32, &(server_config.decline_time),"3600"}, |
| 217 | {"conflict_time",read_u32,&(server_config.conflict_time),"3600"}, |
| 218 | {"offer_time", read_u32, &(server_config.offer_time), "60"}, |
| 219 | {"min_lease", read_u32, &(server_config.min_lease), "60"}, |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 220 | {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE}, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 221 | {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"}, |
| 222 | {"notify_file", read_str, &(server_config.notify_file), ""}, |
| 223 | {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"}, |
| 224 | {"sname", read_str, &(server_config.sname), ""}, |
| 225 | {"boot_file", read_str, &(server_config.boot_file), ""}, |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame] | 226 | {"static_lease",read_staticlease, &(server_config.static_leases), ""}, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 227 | /*ADDME: static lease */ |
| 228 | {"", NULL, NULL, ""} |
| 229 | }; |
| 230 | |
| 231 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 232 | int read_config(const char *file) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 233 | { |
| 234 | FILE *in; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 235 | char buffer[READ_CONFIG_BUF_SIZE], *token, *line; |
| 236 | #ifdef UDHCP_DEBUG |
| 237 | char orig[READ_CONFIG_BUF_SIZE]; |
| 238 | #endif |
| 239 | int i, lm = 0; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 240 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 241 | for (i = 0; keywords[i].keyword[0]; i++) |
| 242 | if (keywords[i].def[0]) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 243 | keywords[i].handler(keywords[i].def, keywords[i].var); |
| 244 | |
| 245 | if (!(in = fopen(file, "r"))) { |
| 246 | LOG(LOG_ERR, "unable to open config file: %s", file); |
| 247 | return 0; |
| 248 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 249 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 250 | while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 251 | lm++; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 252 | if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0'; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 253 | #ifdef UDHCP_DEBUG |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 254 | strcpy(orig, buffer); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 255 | #endif |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 256 | if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0'; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 257 | |
| 258 | if (!(token = strtok(buffer, " \t"))) continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 259 | if (!(line = strtok(NULL, ""))) continue; |
| 260 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 261 | /* eat leading whitespace */ |
| 262 | line = line + strspn(line, " \t="); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 263 | /* eat trailing whitespace */ |
| 264 | for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--); |
| 265 | line[i] = '\0'; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 266 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 267 | for (i = 0; keywords[i].keyword[0]; i++) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 268 | if (!strcasecmp(token, keywords[i].keyword)) |
| 269 | if (!keywords[i].handler(line, keywords[i].var)) { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 270 | LOG(LOG_ERR, "Failure parsing line %d of %s", lm, file); |
| 271 | DEBUG(LOG_ERR, "unable to parse '%s'", orig); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 272 | /* reset back to the default value */ |
| 273 | keywords[i].handler(keywords[i].def, keywords[i].var); |
| 274 | } |
| 275 | } |
| 276 | fclose(in); |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | void write_leases(void) |
| 282 | { |
| 283 | FILE *fp; |
| 284 | unsigned int i; |
| 285 | char buf[255]; |
| 286 | time_t curr = time(0); |
Russ Dill | 8f43126 | 2003-12-16 01:29:40 +0000 | [diff] [blame] | 287 | unsigned long tmp_time; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 288 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 289 | if (!(fp = fopen(server_config.lease_file, "w"))) { |
| 290 | LOG(LOG_ERR, "Unable to open %s for writing", server_config.lease_file); |
| 291 | return; |
| 292 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 293 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 294 | for (i = 0; i < server_config.max_leases; i++) { |
| 295 | if (leases[i].yiaddr != 0) { |
Russ Dill | 8f43126 | 2003-12-16 01:29:40 +0000 | [diff] [blame] | 296 | |
| 297 | /* screw with the time in the struct, for easier writing */ |
| 298 | tmp_time = leases[i].expires; |
| 299 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 300 | if (server_config.remaining) { |
| 301 | if (lease_expired(&(leases[i]))) |
Russ Dill | 8f43126 | 2003-12-16 01:29:40 +0000 | [diff] [blame] | 302 | leases[i].expires = 0; |
| 303 | else leases[i].expires -= curr; |
| 304 | } /* else stick with the time we got */ |
| 305 | leases[i].expires = htonl(leases[i].expires); |
Russ Dill | a3170d5 | 2003-12-16 01:33:38 +0000 | [diff] [blame] | 306 | fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp); |
Russ Dill | 8f43126 | 2003-12-16 01:29:40 +0000 | [diff] [blame] | 307 | |
| 308 | /* Then restore it when done. */ |
| 309 | leases[i].expires = tmp_time; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | fclose(fp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 313 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 314 | if (server_config.notify_file) { |
| 315 | sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file); |
| 316 | system(buf); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 321 | void read_leases(const char *file) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 322 | { |
| 323 | FILE *fp; |
| 324 | unsigned int i = 0; |
| 325 | struct dhcpOfferedAddr lease; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 326 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 327 | if (!(fp = fopen(file, "r"))) { |
| 328 | LOG(LOG_ERR, "Unable to open %s for reading", file); |
| 329 | return; |
| 330 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 331 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 332 | while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) { |
| 333 | /* ADDME: is it a static lease */ |
| 334 | if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) { |
| 335 | lease.expires = ntohl(lease.expires); |
| 336 | if (!server_config.remaining) lease.expires -= time(0); |
| 337 | if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) { |
| 338 | LOG(LOG_WARNING, "Too many leases while loading %s\n", file); |
| 339 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 340 | } |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 341 | i++; |
| 342 | } |
| 343 | } |
| 344 | DEBUG(LOG_INFO, "Read %d leases", i); |
| 345 | fclose(fp); |
| 346 | } |