| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#include <config.h> |
| 41 |
|
| 42 |
#include <arpa/inet.h> |
| 43 |
#include <ctype.h> |
| 44 |
#include <errno.h> |
| 45 |
#include <fcntl.h> |
| 46 |
#include <limits.h> |
| 47 |
#include <netinet/in.h> |
| 48 |
#include <stddef.h> |
| 49 |
#include <stdint.h> |
| 50 |
#include <stdio.h> |
| 51 |
#include <stdlib.h> |
| 52 |
#include <sys/stat.h> |
| 53 |
|
| 54 |
#include "error.h" |
| 55 |
#include "xalloc.h" |
| 56 |
|
| 57 |
#include "dhcpd-pools.h" |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
enum dhcpd_magic_numbers { |
| 64 |
MAXLEN = 1024 |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
enum isc_conf_parser { |
| 72 |
ITS_NOTHING_INTERESTING, |
| 73 |
ITS_A_RANGE_FIRST_IP, |
| 74 |
ITS_A_RANGE_SECOND_IP, |
| 75 |
ITS_A_SHAREDNET, |
| 76 |
ITS_AN_INCLUDE, |
| 77 |
ITS_A_SUBNET, |
| 78 |
ITS_A_NETMASK |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
int parse_leases(struct conf_t *state) |
| 84 |
{ |
| 85 |
FILE *dhcpd_leases; |
| 86 |
char *line, *ipstring, macstring[20], *stop; |
| 87 |
union ipaddr_t addr; |
| 88 |
struct leases_t *lease; |
| 89 |
|
| 90 |
dhcpd_leases = fopen(state->dhcpdlease_file, "r"); |
| 91 |
if (dhcpd_leases == NULL) |
| 92 |
error(EXIT_FAILURE, errno, "parse_leases: %s", state->dhcpdlease_file); |
| 93 |
#ifdef HAVE_POSIX_FADVISE |
| 94 |
# ifdef POSIX_FADV_SEQUENTIAL |
| 95 |
if (posix_fadvise(fileno(dhcpd_leases), 0, 0, POSIX_FADV_SEQUENTIAL) != 0) |
| 96 |
error(EXIT_FAILURE, errno, "parse_leases: fadvise %s", state->dhcpdlease_file); |
| 97 |
# endif |
| 98 |
#endif |
| 99 |
line = xmalloc(sizeof(char) * MAXLEN); |
| 100 |
line[0] = '\0'; |
| 101 |
ipstring = xmalloc(sizeof(char) * MAXLEN); |
| 102 |
ipstring[0] = '\0'; |
| 103 |
while (!feof(dhcpd_leases)) { |
| 104 |
if (!fgets(line, MAXLEN, dhcpd_leases) && ferror(dhcpd_leases)) |
| 105 |
error(EXIT_FAILURE, errno, "parse_leases: %s", state->dhcpdlease_file); |
| 106 |
switch (xstrstr(state, line)) { |
| 107 |
|
| 108 |
case PREFIX_LEASE: |
| 109 |
stop = |
| 110 |
memccpy(ipstring, |
| 111 |
line + (state->ip_version == |
| 112 |
IPv4 ? 6 : 9), ' ', strlen(line)); |
| 113 |
if (stop != NULL) { |
| 114 |
--stop; |
| 115 |
*stop = '\0'; |
| 116 |
} |
| 117 |
parse_ipaddr(state, ipstring, &addr); |
| 118 |
break; |
| 119 |
case PREFIX_BINDING_STATE_FREE: |
| 120 |
case PREFIX_BINDING_STATE_ABANDONED: |
| 121 |
case PREFIX_BINDING_STATE_EXPIRED: |
| 122 |
case PREFIX_BINDING_STATE_RELEASED: |
| 123 |
if ((lease = find_lease(state, &addr)) != NULL) |
| 124 |
delete_lease(state, lease); |
| 125 |
add_lease(state, &addr, FREE); |
| 126 |
break; |
| 127 |
case PREFIX_BINDING_STATE_ACTIVE: |
| 128 |
|
| 129 |
if ((lease = find_lease(state, &addr)) != NULL) |
| 130 |
delete_lease(state, lease); |
| 131 |
add_lease(state, &addr, ACTIVE); |
| 132 |
break; |
| 133 |
case PREFIX_BINDING_STATE_BACKUP: |
| 134 |
|
| 135 |
if ((lease = find_lease(state, &addr)) != NULL) |
| 136 |
delete_lease(state, lease); |
| 137 |
add_lease(state, &addr, BACKUP); |
| 138 |
state->backups_found = 1; |
| 139 |
break; |
| 140 |
case PREFIX_HARDWARE_ETHERNET: |
| 141 |
if (state->print_mac_addreses == 0) |
| 142 |
break; |
| 143 |
memcpy(macstring, line + 20, 17); |
| 144 |
macstring[17] = '\0'; |
| 145 |
if ((lease = find_lease(state, &addr)) != NULL) |
| 146 |
lease->ethernet = xstrdup(macstring); |
| 147 |
break; |
| 148 |
default: |
| 149 |
; |
| 150 |
} |
| 151 |
} |
| 152 |
#undef HAS_PREFIX |
| 153 |
free(line); |
| 154 |
free(ipstring); |
| 155 |
fclose(dhcpd_leases); |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
static int is_interesting_config_clause(struct conf_t *state, char const *restrict s) |
| 163 |
{ |
| 164 |
if (strstr(s, "range")) |
| 165 |
return ITS_A_RANGE_FIRST_IP; |
| 166 |
if (strstr(s, "shared-network")) |
| 167 |
return ITS_A_SHAREDNET; |
| 168 |
if (state->all_as_shared) { |
| 169 |
if (strstr(s, "subnet")) |
| 170 |
return ITS_A_SUBNET; |
| 171 |
if (strstr(s, "netmask")) |
| 172 |
return ITS_A_NETMASK; |
| 173 |
} |
| 174 |
if (strstr(s, "include")) |
| 175 |
return ITS_AN_INCLUDE; |
| 176 |
return ITS_NOTHING_INTERESTING; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
static void reorder_last_first(struct range_t *range_p) |
| 182 |
{ |
| 183 |
if (ipcomp(&range_p->first_ip, &range_p->last_ip) > 0) { |
| 184 |
union ipaddr_t tmp; |
| 185 |
|
| 186 |
tmp = range_p->first_ip; |
| 187 |
range_p->first_ip = range_p->last_ip; |
| 188 |
range_p->last_ip = tmp; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
void parse_config(struct conf_t *state, const int is_include, const char *restrict config_file, |
| 197 |
struct shared_network_t *restrict shared_p) |
| 198 |
{ |
| 199 |
FILE *dhcpd_config; |
| 200 |
int newclause = 1, comment = 0, one_ip_range = 0; |
| 201 |
int quote = 0, braces = 0, argument = ITS_NOTHING_INTERESTING; |
| 202 |
size_t i = 0; |
| 203 |
char *word; |
| 204 |
int braces_shared = 1000; |
| 205 |
union ipaddr_t addr; |
| 206 |
struct range_t *range_p = NULL; |
| 207 |
|
| 208 |
word = xmalloc(sizeof(char) * MAXLEN); |
| 209 |
if (is_include) |
| 210 |
|
| 211 |
shared_p->name = state->shared_net_root->name; |
| 212 |
|
| 213 |
dhcpd_config = fopen(config_file, "r"); |
| 214 |
if (dhcpd_config == NULL) |
| 215 |
error(EXIT_FAILURE, errno, "parse_config: %s", config_file); |
| 216 |
#ifdef HAVE_POSIX_FADVISE |
| 217 |
# ifdef POSIX_FADV_SEQUENTIAL |
| 218 |
if (posix_fadvise(fileno(dhcpd_config), 0, 0, POSIX_FADV_SEQUENTIAL) != 0) |
| 219 |
error(EXIT_FAILURE, errno, "parse_config: fadvise %s", config_file); |
| 220 |
# endif |
| 221 |
#endif |
| 222 |
|
| 223 |
while (unlikely(!feof(dhcpd_config))) { |
| 224 |
int c; |
| 225 |
|
| 226 |
c = fgetc(dhcpd_config); |
| 227 |
if (CHAR_MAX < c) |
| 228 |
continue; |
| 229 |
|
| 230 |
switch (c) { |
| 231 |
|
| 232 |
case '#': |
| 233 |
if (quote == 0) |
| 234 |
comment = 1; |
| 235 |
continue; |
| 236 |
case '"': |
| 237 |
if (comment == 0) { |
| 238 |
quote++; |
| 239 |
|
| 240 |
quote = quote % 2; |
| 241 |
} |
| 242 |
continue; |
| 243 |
case '\n': |
| 244 |
|
| 245 |
|
| 246 |
if (quote == 0) |
| 247 |
comment = 0; |
| 248 |
break; |
| 249 |
case ';': |
| 250 |
|
| 251 |
if (0 < quote) |
| 252 |
break; |
| 253 |
if (comment == 0 |
| 254 |
&& argument != ITS_A_RANGE_FIRST_IP |
| 255 |
&& argument != ITS_A_RANGE_SECOND_IP && argument != ITS_AN_INCLUDE) { |
| 256 |
newclause = 1; |
| 257 |
i = 0; |
| 258 |
} else if (argument == ITS_A_RANGE_FIRST_IP && one_ip_range == 1) { |
| 259 |
argument = ITS_A_RANGE_SECOND_IP; |
| 260 |
c = ' '; |
| 261 |
} else if (argument == ITS_A_RANGE_SECOND_IP && 0 < i) { |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
c = ' '; |
| 269 |
break; |
| 270 |
} else if (argument == ITS_A_RANGE_SECOND_IP && i == 0) { |
| 271 |
if (!range_p) { |
| 272 |
long int pos; |
| 273 |
pos = ftell(dhcpd_config); |
| 274 |
error(EXIT_FAILURE, 0, "parse_config: parsing failed at position: %ld", pos); |
| 275 |
} |
| 276 |
range_p->last_ip = range_p->first_ip; |
| 277 |
goto newrange; |
| 278 |
} |
| 279 |
continue; |
| 280 |
case '{': |
| 281 |
if (0 < quote) |
| 282 |
break; |
| 283 |
if (comment == 0) |
| 284 |
braces++; |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
if (i == 0) { |
| 289 |
newclause = 1; |
| 290 |
continue; |
| 291 |
} |
| 292 |
break; |
| 293 |
case '}': |
| 294 |
if (0 < quote) |
| 295 |
break; |
| 296 |
if (comment == 0) { |
| 297 |
braces--; |
| 298 |
|
| 299 |
if (braces_shared == braces) { |
| 300 |
|
| 301 |
|
| 302 |
braces_shared = 1000; |
| 303 |
shared_p = state->shared_net_root; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
newclause = 1; |
| 308 |
} |
| 309 |
continue; |
| 310 |
default: |
| 311 |
break; |
| 312 |
} |
| 313 |
|
| 314 |
if (comment == 1 || (newclause == 0 && argument == ITS_NOTHING_INTERESTING)) |
| 315 |
continue; |
| 316 |
|
| 317 |
if ((newclause == 1 || argument != ITS_NOTHING_INTERESTING) |
| 318 |
&& isspace(c) && i == 0 && one_ip_range == 0) |
| 319 |
continue; |
| 320 |
|
| 321 |
if ((newclause == 1 || argument != ITS_NOTHING_INTERESTING) |
| 322 |
&& (!isspace(c) || 0 < quote)) { |
| 323 |
word[i] = (char) c; |
| 324 |
i++; |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
if (MAXLEN == i) { |
| 329 |
newclause = 0; |
| 330 |
i = 0; |
| 331 |
continue; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
else if (newclause == 1) { |
| 336 |
|
| 337 |
word[i] = '\0'; |
| 338 |
if (word[i - 1] != '{') |
| 339 |
newclause = 0; |
| 340 |
i = 0; |
| 341 |
argument = is_interesting_config_clause(state, word); |
| 342 |
if (argument == ITS_A_RANGE_FIRST_IP) |
| 343 |
one_ip_range = 1; |
| 344 |
} |
| 345 |
|
| 346 |
else if (argument != ITS_NOTHING_INTERESTING) { |
| 347 |
word[i] = '\0'; |
| 348 |
newclause = 0; |
| 349 |
i = 0; |
| 350 |
|
| 351 |
switch (argument) { |
| 352 |
case ITS_A_RANGE_SECOND_IP: |
| 353 |
|
| 354 |
range_p = state->ranges + state->num_ranges; |
| 355 |
argument = ITS_NOTHING_INTERESTING; |
| 356 |
if (strchr(word, '/')) { |
| 357 |
parse_cidr(state, range_p, word); |
| 358 |
one_ip_range = 0; |
| 359 |
} else { |
| 360 |
|
| 361 |
parse_ipaddr(state, word, &addr); |
| 362 |
if (one_ip_range == 1) { |
| 363 |
one_ip_range = 0; |
| 364 |
copy_ipaddr(&range_p->first_ip, &addr); |
| 365 |
} |
| 366 |
copy_ipaddr(&range_p->last_ip, &addr); |
| 367 |
reorder_last_first(range_p); |
| 368 |
} |
| 369 |
newrange: |
| 370 |
range_p->count = 0; |
| 371 |
range_p->touched = 0; |
| 372 |
range_p->backups = 0; |
| 373 |
range_p->shared_net = shared_p; |
| 374 |
state->num_ranges++; |
| 375 |
if (state->ranges_size <= state->num_ranges) { |
| 376 |
state->ranges_size *= 2; |
| 377 |
state->ranges = xrealloc(state->ranges, sizeof(struct range_t) * state->ranges_size); |
| 378 |
range_p = state->ranges + state->num_ranges; |
| 379 |
} |
| 380 |
newclause = 1; |
| 381 |
break; |
| 382 |
case ITS_A_RANGE_FIRST_IP: |
| 383 |
|
| 384 |
range_p = state->ranges + state->num_ranges; |
| 385 |
if (!(parse_ipaddr(state, word, &addr))) |
| 386 |
|
| 387 |
break; |
| 388 |
copy_ipaddr(&range_p->first_ip, &addr); |
| 389 |
one_ip_range = 0; |
| 390 |
argument = ITS_A_RANGE_SECOND_IP; |
| 391 |
break; |
| 392 |
case ITS_A_SHAREDNET: |
| 393 |
case ITS_A_SUBNET: |
| 394 |
|
| 395 |
if (argument == ITS_A_SUBNET && shared_p != state->shared_net_root) { |
| 396 |
argument = ITS_NOTHING_INTERESTING; |
| 397 |
break; |
| 398 |
} |
| 399 |
state->shared_net_head->next = xcalloc(sizeof(struct shared_network_t), 1); |
| 400 |
state->shared_net_head = state->shared_net_head->next; |
| 401 |
shared_p = state->shared_net_head; |
| 402 |
shared_p->name = xstrdup(word); |
| 403 |
shared_p->netmask = (argument == ITS_A_SUBNET ? -1 : 0); |
| 404 |
|
| 405 |
if (argument == ITS_A_SUBNET) |
| 406 |
newclause = 1; |
| 407 |
argument = ITS_NOTHING_INTERESTING; |
| 408 |
braces_shared = braces; |
| 409 |
break; |
| 410 |
case ITS_A_NETMASK: |
| 411 |
|
| 412 |
if (shared_p->netmask) { |
| 413 |
if (!(parse_ipaddr(state, word, &addr))) |
| 414 |
break; |
| 415 |
shared_p->netmask = 32; |
| 416 |
while ((addr.v4 & 0x01) == 0) { |
| 417 |
addr.v4 >>= 1; |
| 418 |
shared_p->netmask--; |
| 419 |
} |
| 420 |
snprintf(word, MAXLEN-1, "%s/%d", shared_p->name, shared_p->netmask); |
| 421 |
if (shared_p->name) |
| 422 |
free(shared_p->name); |
| 423 |
shared_p->name = xstrdup(word); |
| 424 |
} |
| 425 |
argument = ITS_NOTHING_INTERESTING; |
| 426 |
braces_shared = braces; |
| 427 |
break; |
| 428 |
case ITS_AN_INCLUDE: |
| 429 |
|
| 430 |
argument = ITS_NOTHING_INTERESTING; |
| 431 |
parse_config(state, 0, word, shared_p); |
| 432 |
newclause = 1; |
| 433 |
break; |
| 434 |
case ITS_NOTHING_INTERESTING: |
| 435 |
|
| 436 |
argument = ITS_NOTHING_INTERESTING; |
| 437 |
break; |
| 438 |
default: |
| 439 |
puts("impossible occurred, report a bug"); |
| 440 |
abort(); |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
free(word); |
| 445 |
fclose(dhcpd_config); |
| 446 |
return; |
| 447 |
} |