Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * files.c -- DHCP server file manipulation * |
| 3 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 4 | */ |
| 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 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 14 | #include "dhcpd.h" |
| 15 | #include "files.h" |
| 16 | #include "options.h" |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 17 | #include "common.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 18 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 19 | /* |
| 20 | * Domain names may have 254 chars, and string options can be 254 |
| 21 | * chars long. However, 80 bytes will be enough for most, and won't |
| 22 | * hog up memory. If you have a special application, change it |
| 23 | */ |
| 24 | #define READ_CONFIG_BUF_SIZE 80 |
| 25 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 26 | /* on these functions, make sure you datatype matches */ |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 27 | static int read_ip(const char *line, void *arg) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 28 | { |
| 29 | struct in_addr *addr = arg; |
| 30 | struct hostent *host; |
| 31 | int retval = 1; |
| 32 | |
| 33 | if (!inet_aton(line, addr)) { |
| 34 | if ((host = gethostbyname(line))) |
| 35 | addr->s_addr = *((unsigned long *) host->h_addr_list[0]); |
| 36 | else retval = 0; |
| 37 | } |
| 38 | return retval; |
| 39 | } |
| 40 | |
| 41 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 42 | static int read_str(const char *line, void *arg) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 43 | { |
| 44 | char **dest = arg; |
| 45 | |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame] | 46 | if (*dest) free(*dest); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 47 | *dest = strdup(line); |
| 48 | |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 53 | static int read_u32(const char *line, void *arg) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 54 | { |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 55 | uint32_t *dest = arg; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 56 | char *endptr; |
| 57 | *dest = strtoul(line, &endptr, 0); |
| 58 | return endptr[0] == '\0'; |
| 59 | } |
| 60 | |
| 61 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 62 | static int read_yn(const char *line, void *arg) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 63 | { |
| 64 | char *dest = arg; |
| 65 | int retval = 1; |
| 66 | |
| 67 | if (!strcasecmp("yes", line)) |
| 68 | *dest = 1; |
| 69 | else if (!strcasecmp("no", line)) |
| 70 | *dest = 0; |
| 71 | else retval = 0; |
| 72 | |
| 73 | return retval; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /* read a dhcp option and add it to opt_list */ |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 78 | static int read_opt(const char *const_line, void *arg) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 79 | { |
| 80 | struct option_set **opt_list = arg; |
| 81 | char *opt, *val, *endptr; |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 82 | struct dhcp_option *option; |
| 83 | int retval = 0, length; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 84 | char buffer[8]; |
| 85 | char *line; |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 86 | uint16_t *result_u16 = (uint16_t *) buffer; |
| 87 | uint32_t *result_u32 = (uint32_t *) buffer; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 88 | |
| 89 | /* Cheat, the only const line we'll actually get is "" */ |
| 90 | line = (char *) const_line; |
| 91 | if (!(opt = strtok(line, " \t="))) return 0; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 92 | |
Russ Dill | 4a9e34c | 2003-12-15 22:09:36 +0000 | [diff] [blame] | 93 | for (option = dhcp_options; option->code; option++) |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 94 | if (!strcasecmp(option->name, opt)) |
| 95 | break; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 96 | |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 97 | if (!option->code) return 0; |
| 98 | |
| 99 | do { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 100 | if (!(val = strtok(NULL, ", \t"))) break; |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 101 | length = option_lengths[option->flags & TYPE_MASK]; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 102 | retval = 0; |
| 103 | opt = buffer; /* new meaning for variable opt */ |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 104 | switch (option->flags & TYPE_MASK) { |
| 105 | case OPTION_IP: |
| 106 | retval = read_ip(val, buffer); |
| 107 | break; |
| 108 | case OPTION_IP_PAIR: |
| 109 | retval = read_ip(val, buffer); |
| 110 | if (!(val = strtok(NULL, ", \t/-"))) retval = 0; |
| 111 | if (retval) retval = read_ip(val, buffer + 4); |
| 112 | break; |
| 113 | case OPTION_STRING: |
| 114 | length = strlen(val); |
| 115 | if (length > 0) { |
| 116 | if (length > 254) length = 254; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 117 | opt = val; |
| 118 | retval = 1; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 119 | } |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 120 | break; |
| 121 | case OPTION_BOOLEAN: |
| 122 | retval = read_yn(val, buffer); |
| 123 | break; |
| 124 | case OPTION_U8: |
| 125 | buffer[0] = strtoul(val, &endptr, 0); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 126 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 127 | break; |
| 128 | case OPTION_U16: |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 129 | *result_u16 = htons(strtoul(val, &endptr, 0)); |
| 130 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 131 | break; |
| 132 | case OPTION_S16: |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 133 | *result_u16 = htons(strtol(val, &endptr, 0)); |
| 134 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 135 | break; |
| 136 | case OPTION_U32: |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 137 | *result_u32 = htonl(strtoul(val, &endptr, 0)); |
| 138 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 139 | break; |
| 140 | case OPTION_S32: |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 141 | *result_u32 = htonl(strtol(val, &endptr, 0)); |
| 142 | retval = (endptr[0] == '\0'); |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 143 | break; |
| 144 | default: |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 145 | break; |
| 146 | } |
Russ Dill | e304956 | 2003-12-16 22:46:33 +0000 | [diff] [blame] | 147 | if (retval) |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 148 | attach_option(opt_list, option, opt, length); |
| 149 | } while (retval && option->flags & OPTION_LIST); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 150 | return retval; |
| 151 | } |
| 152 | |
| 153 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 154 | static const struct config_keyword keywords[] = { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 155 | /* keyword handler variable address default */ |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 156 | {"start", read_ip, &(server_config.start), "192.168.0.20"}, |
| 157 | {"end", read_ip, &(server_config.end), "192.168.0.254"}, |
| 158 | {"interface", read_str, &(server_config.interface), "eth0"}, |
| 159 | {"option", read_opt, &(server_config.options), ""}, |
| 160 | {"opt", read_opt, &(server_config.options), ""}, |
| 161 | {"max_leases", read_u32, &(server_config.max_leases), "254"}, |
| 162 | {"remaining", read_yn, &(server_config.remaining), "yes"}, |
| 163 | {"auto_time", read_u32, &(server_config.auto_time), "7200"}, |
| 164 | {"decline_time",read_u32, &(server_config.decline_time),"3600"}, |
| 165 | {"conflict_time",read_u32,&(server_config.conflict_time),"3600"}, |
| 166 | {"offer_time", read_u32, &(server_config.offer_time), "60"}, |
| 167 | {"min_lease", read_u32, &(server_config.min_lease), "60"}, |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 168 | {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE}, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 169 | {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"}, |
| 170 | {"notify_file", read_str, &(server_config.notify_file), ""}, |
| 171 | {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"}, |
| 172 | {"sname", read_str, &(server_config.sname), ""}, |
| 173 | {"boot_file", read_str, &(server_config.boot_file), ""}, |
| 174 | /*ADDME: static lease */ |
| 175 | {"", NULL, NULL, ""} |
| 176 | }; |
| 177 | |
| 178 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 179 | int read_config(const char *file) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 180 | { |
| 181 | FILE *in; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 182 | char buffer[READ_CONFIG_BUF_SIZE], *token, *line; |
| 183 | #ifdef UDHCP_DEBUG |
| 184 | char orig[READ_CONFIG_BUF_SIZE]; |
| 185 | #endif |
| 186 | int i, lm = 0; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 187 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 188 | for (i = 0; keywords[i].keyword[0]; i++) |
| 189 | if (keywords[i].def[0]) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 190 | keywords[i].handler(keywords[i].def, keywords[i].var); |
| 191 | |
| 192 | if (!(in = fopen(file, "r"))) { |
| 193 | LOG(LOG_ERR, "unable to open config file: %s", file); |
| 194 | return 0; |
| 195 | } |
| 196 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 197 | while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 198 | lm++; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 199 | if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0'; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 200 | #ifdef UDHCP_DEBUG |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 201 | strcpy(orig, buffer); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 202 | #endif |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 203 | if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0'; |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 204 | |
| 205 | if (!(token = strtok(buffer, " \t"))) continue; |
| 206 | if (!(line = strtok(NULL, ""))) continue; |
| 207 | |
| 208 | /* eat leading whitespace */ |
| 209 | line = line + strspn(line, " \t="); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 210 | /* eat trailing whitespace */ |
| 211 | for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--); |
| 212 | line[i] = '\0'; |
| 213 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 214 | for (i = 0; keywords[i].keyword[0]; i++) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 215 | if (!strcasecmp(token, keywords[i].keyword)) |
| 216 | if (!keywords[i].handler(line, keywords[i].var)) { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 217 | LOG(LOG_ERR, "Failure parsing line %d of %s", lm, file); |
| 218 | DEBUG(LOG_ERR, "unable to parse '%s'", orig); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 219 | /* reset back to the default value */ |
| 220 | keywords[i].handler(keywords[i].def, keywords[i].var); |
| 221 | } |
| 222 | } |
| 223 | fclose(in); |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | void write_leases(void) |
| 229 | { |
| 230 | FILE *fp; |
| 231 | unsigned int i; |
| 232 | char buf[255]; |
| 233 | time_t curr = time(0); |
Russ Dill | 8f43126 | 2003-12-16 01:29:40 +0000 | [diff] [blame] | 234 | unsigned long tmp_time; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 235 | |
| 236 | if (!(fp = fopen(server_config.lease_file, "w"))) { |
| 237 | LOG(LOG_ERR, "Unable to open %s for writing", server_config.lease_file); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | for (i = 0; i < server_config.max_leases; i++) { |
| 242 | if (leases[i].yiaddr != 0) { |
Russ Dill | 8f43126 | 2003-12-16 01:29:40 +0000 | [diff] [blame] | 243 | |
| 244 | /* screw with the time in the struct, for easier writing */ |
| 245 | tmp_time = leases[i].expires; |
| 246 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 247 | if (server_config.remaining) { |
| 248 | if (lease_expired(&(leases[i]))) |
Russ Dill | 8f43126 | 2003-12-16 01:29:40 +0000 | [diff] [blame] | 249 | leases[i].expires = 0; |
| 250 | else leases[i].expires -= curr; |
| 251 | } /* else stick with the time we got */ |
| 252 | leases[i].expires = htonl(leases[i].expires); |
Russ Dill | a3170d5 | 2003-12-16 01:33:38 +0000 | [diff] [blame] | 253 | fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp); |
Russ Dill | 8f43126 | 2003-12-16 01:29:40 +0000 | [diff] [blame] | 254 | |
| 255 | /* Then restore it when done. */ |
| 256 | leases[i].expires = tmp_time; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | fclose(fp); |
| 260 | |
| 261 | if (server_config.notify_file) { |
| 262 | sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file); |
| 263 | system(buf); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 268 | void read_leases(const char *file) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 269 | { |
| 270 | FILE *fp; |
| 271 | unsigned int i = 0; |
| 272 | struct dhcpOfferedAddr lease; |
| 273 | |
| 274 | if (!(fp = fopen(file, "r"))) { |
| 275 | LOG(LOG_ERR, "Unable to open %s for reading", file); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) { |
| 280 | /* ADDME: is it a static lease */ |
| 281 | if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) { |
| 282 | lease.expires = ntohl(lease.expires); |
| 283 | if (!server_config.remaining) lease.expires -= time(0); |
| 284 | if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) { |
| 285 | LOG(LOG_WARNING, "Too many leases while loading %s\n", file); |
| 286 | break; |
| 287 | } |
| 288 | i++; |
| 289 | } |
| 290 | } |
| 291 | DEBUG(LOG_INFO, "Read %d leases", i); |
| 292 | fclose(fp); |
| 293 | } |