1 /* 2 * checkconf - Read and repeat configuration file to output. 3 * 4 * Copyright (c) 2001-2006, NLnet Labs. All rights reserved. 5 * 6 * See LICENSE for the license. 7 * 8 */ 9 #include "config.h" 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <unistd.h> 13 #include <string.h> 14 #include <limits.h> 15 #include "tsig.h" 16 #include "options.h" 17 #include "util.h" 18 #include "dname.h" 19 #include "rrl.h" 20 21 extern char *optarg; 22 extern int optind; 23 24 #define ZONE_GET_ACL(NAME, VAR, PATTERN) \ 25 if (strcasecmp(#NAME, (VAR)) == 0) { \ 26 quote_acl(PATTERN->NAME); \ 27 return; \ 28 } 29 30 #define ZONE_GET_OUTGOING(NAME, VAR, PATTERN) \ 31 if (strcasecmp(#NAME, (VAR)) == 0) { \ 32 acl_options_t* acl; \ 33 for(acl=PATTERN->NAME; acl; acl=acl->next) \ 34 quote(acl->ip_address_spec); \ 35 return; \ 36 } 37 38 #define ZONE_GET_STR(NAME, VAR, PATTERN) \ 39 if (strcasecmp(#NAME, (VAR)) == 0) { \ 40 quote(PATTERN->NAME); \ 41 return; \ 42 } 43 44 #define ZONE_GET_PATH(FINAL, NAME, VAR, PATTERN) \ 45 if (strcasecmp(#NAME, (VAR)) == 0) { \ 46 quotepath(opt, FINAL, PATTERN->NAME); \ 47 return; \ 48 } 49 50 #define ZONE_GET_BIN(NAME, VAR, PATTERN) \ 51 if (strcasecmp(#NAME, (VAR)) == 0) { \ 52 printf("%s\n", (PATTERN->NAME)?"yes":"no"); \ 53 return; \ 54 } 55 56 #define ZONE_GET_RRL(NAME, VAR, PATTERN) \ 57 if (strcasecmp(#NAME, (VAR)) == 0) { \ 58 zone_print_rrl_whitelist("", PATTERN->NAME); \ 59 return; \ 60 } 61 62 #define SERV_GET_BIN(NAME, VAR) \ 63 if (strcasecmp(#NAME, (VAR)) == 0) { \ 64 printf("%s\n", opt->NAME?"yes":"no"); \ 65 return; \ 66 } 67 68 #define SERV_GET_STR(NAME, VAR) \ 69 if (strcasecmp(#NAME, (VAR)) == 0) { \ 70 quote(opt->NAME); \ 71 return; \ 72 } 73 74 #define SERV_GET_PATH(FINAL, NAME, VAR) \ 75 if (strcasecmp(#NAME, (VAR)) == 0) { \ 76 quotepath(opt, FINAL, opt->NAME); \ 77 return; \ 78 } 79 80 #define SERV_GET_INT(NAME, VAR) \ 81 if (strcasecmp(#NAME, (VAR)) == 0) { \ 82 printf("%d\n", (int) opt->NAME); \ 83 return; \ 84 } 85 86 #define SERV_GET_IP(NAME, MEMBER, VAR) \ 87 if (strcasecmp(#NAME, (VAR)) == 0) { \ 88 for(ip = opt->MEMBER; ip; ip=ip->next) \ 89 { \ 90 quote(ip->address); \ 91 } \ 92 return; \ 93 } 94 95 #ifdef RATELIMIT 96 static void zone_print_rrl_whitelist(const char* s, uint16_t w) 97 { 98 int i; 99 if(w==rrl_type_all) { 100 printf("%sall\n", s); 101 return; 102 } 103 for(i=0x01; i <= 0x80; i<<=1) { 104 if( (w&i) ) 105 printf("%s%s\n", s, rrltype2str(i)); 106 } 107 } 108 #endif /* RATELIMIT */ 109 110 static char buf[BUFSIZ]; 111 112 static char * 113 underscore(const char *s) { 114 const char *j = s; 115 size_t i = 0; 116 117 while(j && *j) { 118 if (*j == '-') { 119 buf[i++] = '_'; 120 } else { 121 buf[i++] = *j; 122 } 123 j++; 124 if (i >= BUFSIZ) { 125 return NULL; 126 } 127 } 128 buf[i] = '\0'; 129 return buf; 130 } 131 132 static void 133 usage(void) 134 { 135 fprintf(stderr, "usage: nsd-checkconf [-v|-h] [-o option] [-z zonename]\n"); 136 fprintf(stderr, " [-s keyname] <configfilename>\n"); 137 fprintf(stderr, " Checks NSD configuration file for errors.\n"); 138 fprintf(stderr, " Version %s. Report bugs to <%s>.\n\n", 139 PACKAGE_VERSION, PACKAGE_BUGREPORT); 140 fprintf(stderr, "Use with a configfile as argument to check syntax.\n"); 141 fprintf(stderr, "Use with -o, -z or -s options to query the configuration.\n\n"); 142 fprintf(stderr, "-v Verbose, echo settings that take effect to std output.\n"); 143 fprintf(stderr, "-h Print this help information.\n"); 144 fprintf(stderr, "-f Use with -o to print final pathnames, ie. with chroot.\n"); 145 fprintf(stderr, "-o option Print value of the option specified to stdout.\n"); 146 fprintf(stderr, "-p pattern Print option value for the pattern given.\n"); 147 fprintf(stderr, "-z zonename Print option value for the zone given.\n"); 148 fprintf(stderr, "-a keyname Print algorithm name for the TSIG key.\n"); 149 fprintf(stderr, "-s keyname Print base64 secret blob for the TSIG key.\n"); 150 exit(1); 151 } 152 153 static void 154 print_string_var(const char* varname, const char* value) 155 { 156 if (!value) { 157 printf("\t#%s\n", varname); 158 } else { 159 printf("\t%s \"%s\"\n", varname, value); 160 } 161 } 162 163 static void 164 quote(const char *v) 165 { 166 if(v==NULL) 167 printf("\n"); 168 else 169 printf("%s\n", v); 170 } 171 172 static void 173 quotepath(nsd_options_t* opt, int final, const char *f) 174 { 175 const char* chr = opt->chroot; 176 #ifdef CHROOTDIR 177 if(chr == 0) chr = CHROOTDIR; 178 #endif 179 if(f == 0 || f[0] == '/' || !final || !chr || chr[0]==0) { 180 quote(f); 181 return; 182 } 183 /* chroot has had trailing slash applied in check part of checkconf */ 184 printf("%s%s\n", chr, f); 185 } 186 187 static void 188 quote_acl(acl_options_t* acl) 189 { 190 while(acl) 191 { 192 printf("%s %s\n", acl->ip_address_spec, 193 acl->nokey?"NOKEY":(acl->blocked?"BLOCKED": 194 (acl->key_name?acl->key_name:"(null)"))); 195 acl=acl->next; 196 } 197 } 198 199 static void 200 print_acl(const char* varname, acl_options_t* acl) 201 { 202 while(acl) 203 { 204 printf("\t%s ", varname); 205 if(acl->use_axfr_only) 206 printf("AXFR "); 207 if(acl->allow_udp) 208 printf("UDP "); 209 printf("%s %s\n", acl->ip_address_spec, 210 acl->nokey?"NOKEY":(acl->blocked?"BLOCKED": 211 (acl->key_name?acl->key_name:"(null)"))); 212 if(verbosity>1) { 213 printf("\t# %s", acl->is_ipv6?"ip6":"ip4"); 214 if(acl->port == 0) printf(" noport"); 215 else printf(" port=%d", acl->port); 216 if(acl->rangetype == acl_range_single) printf(" single"); 217 if(acl->rangetype == acl_range_mask) printf(" masked"); 218 if(acl->rangetype == acl_range_subnet) printf(" subnet"); 219 if(acl->rangetype == acl_range_minmax) printf(" minmax"); 220 if(acl->is_ipv6) { 221 #ifdef INET6 222 char dest[128]; 223 inet_ntop(AF_INET6, &acl->addr.addr6, dest, sizeof(dest)); 224 printf(" addr=%s", dest); 225 if(acl->rangetype != acl_range_single) { 226 inet_ntop(AF_INET6, &acl->range_mask.addr6, dest, sizeof(dest)); 227 printf(" rangemask=%s", dest); 228 } 229 #else 230 printf(" ip6addr-noip6defined"); 231 #endif 232 } else { 233 char dest[128]; 234 inet_ntop(AF_INET, &acl->addr.addr, dest, sizeof(dest)); 235 printf(" addr=%s", dest); 236 if(acl->rangetype != acl_range_single) { 237 inet_ntop(AF_INET, &acl->range_mask.addr, dest, sizeof(dest)); 238 printf(" rangemask=%s", dest); 239 } 240 } 241 printf("\n"); 242 } 243 acl=acl->next; 244 } 245 } 246 247 static void 248 print_acl_ips(const char* varname, acl_options_t* acl) 249 { 250 while(acl) 251 { 252 printf("\t%s %s\n", varname, acl->ip_address_spec); 253 acl=acl->next; 254 } 255 } 256 257 void 258 config_print_zone(nsd_options_t* opt, const char* k, int s, const char *o, 259 const char *z, const char* pat, int final) 260 { 261 ip_address_option_t* ip; 262 263 if (k) { 264 /* find key */ 265 key_options_t* key = key_options_find(opt, k); 266 if(key) { 267 if (s) { 268 quote(key->secret); 269 } else { 270 quote(key->algorithm); 271 } 272 return; 273 } 274 printf("Could not find key %s\n", k); 275 return; 276 } 277 278 if (!o) { 279 return; 280 } 281 282 if (z) { 283 zone_options_t* zone; 284 const dname_type *dname = dname_parse(opt->region, z); 285 if(!dname) { 286 printf("Could not parse zone name %s\n", z); 287 exit(1); 288 } 289 zone = zone_options_find(opt, dname); 290 if(!zone) { 291 printf("Zone does not exist: %s\n", z); 292 exit(1); 293 } 294 ZONE_GET_STR(name, o, zone); 295 if(strcasecmp("pattern", o)==0) { 296 quote(zone->pattern->pname); 297 return; 298 } 299 ZONE_GET_BIN(part_of_config, o, zone); 300 ZONE_GET_PATH(final, zonefile, o, zone->pattern); 301 ZONE_GET_ACL(request_xfr, o, zone->pattern); 302 ZONE_GET_ACL(provide_xfr, o, zone->pattern); 303 ZONE_GET_ACL(allow_notify, o, zone->pattern); 304 ZONE_GET_ACL(notify, o, zone->pattern); 305 ZONE_GET_BIN(notify_retry, o, zone->pattern); 306 ZONE_GET_STR(zonestats, o, zone->pattern); 307 ZONE_GET_OUTGOING(outgoing_interface, o, zone->pattern); 308 ZONE_GET_BIN(allow_axfr_fallback, o, zone->pattern); 309 #ifdef RATELIMIT 310 ZONE_GET_RRL(rrl_whitelist, o, zone->pattern); 311 #endif 312 printf("Zone option not handled: %s %s\n", z, o); 313 exit(1); 314 } else if(pat) { 315 pattern_options_t* p = pattern_options_find(opt, pat); 316 if(!p) { 317 printf("Pattern does not exist: %s\n", pat); 318 exit(1); 319 } 320 if(strcasecmp("name", o)==0) { 321 quote(p->pname); 322 return; 323 } 324 ZONE_GET_STR(zonefile, o, p); 325 ZONE_GET_PATH(final, zonefile, o, p); 326 ZONE_GET_ACL(request_xfr, o, p); 327 ZONE_GET_ACL(provide_xfr, o, p); 328 ZONE_GET_ACL(allow_notify, o, p); 329 ZONE_GET_ACL(notify, o, p); 330 ZONE_GET_BIN(notify_retry, o, p); 331 ZONE_GET_STR(zonestats, o, p); 332 ZONE_GET_OUTGOING(outgoing_interface, o, p); 333 ZONE_GET_BIN(allow_axfr_fallback, o, p); 334 #ifdef RATELIMIT 335 ZONE_GET_RRL(rrl_whitelist, o, p); 336 #endif 337 printf("Pattern option not handled: %s %s\n", pat, o); 338 exit(1); 339 } else { 340 /* look in the server section */ 341 SERV_GET_IP(ip_address, ip_addresses, o); 342 /* bin */ 343 SERV_GET_BIN(ip_transparent, o); 344 SERV_GET_BIN(debug_mode, o); 345 SERV_GET_BIN(do_ip4, o); 346 SERV_GET_BIN(do_ip6, o); 347 SERV_GET_BIN(reuseport, o); 348 SERV_GET_BIN(hide_version, o); 349 SERV_GET_BIN(zonefiles_check, o); 350 SERV_GET_BIN(log_time_ascii, o); 351 SERV_GET_BIN(round_robin, o); 352 /* str */ 353 SERV_GET_PATH(final, database, o); 354 SERV_GET_STR(identity, o); 355 SERV_GET_STR(version, o); 356 SERV_GET_STR(nsid, o); 357 SERV_GET_PATH(final, logfile, o); 358 SERV_GET_PATH(final, pidfile, o); 359 SERV_GET_STR(chroot, o); 360 SERV_GET_STR(username, o); 361 SERV_GET_PATH(final, zonesdir, o); 362 SERV_GET_PATH(final, xfrdfile, o); 363 SERV_GET_PATH(final, xfrdir, o); 364 SERV_GET_PATH(final, zonelistfile, o); 365 SERV_GET_STR(port, o); 366 /* int */ 367 SERV_GET_INT(server_count, o); 368 SERV_GET_INT(tcp_count, o); 369 SERV_GET_INT(tcp_query_count, o); 370 SERV_GET_INT(tcp_timeout, o); 371 SERV_GET_INT(ipv4_edns_size, o); 372 SERV_GET_INT(ipv6_edns_size, o); 373 SERV_GET_INT(statistics, o); 374 SERV_GET_INT(xfrd_reload_timeout, o); 375 SERV_GET_INT(verbosity, o); 376 #ifdef RATELIMIT 377 SERV_GET_INT(rrl_size, o); 378 SERV_GET_INT(rrl_ratelimit, o); 379 SERV_GET_INT(rrl_slip, o); 380 SERV_GET_INT(rrl_ipv4_prefix_length, o); 381 SERV_GET_INT(rrl_ipv6_prefix_length, o); 382 SERV_GET_INT(rrl_whitelist_ratelimit, o); 383 #endif 384 SERV_GET_INT(zonefiles_write, o); 385 /* remote control */ 386 SERV_GET_BIN(control_enable, o); 387 SERV_GET_IP(control_interface, control_interface, o); 388 SERV_GET_INT(control_port, o); 389 SERV_GET_STR(server_key_file, o); 390 SERV_GET_STR(server_cert_file, o); 391 SERV_GET_STR(control_key_file, o); 392 SERV_GET_STR(control_cert_file, o); 393 394 if(strcasecmp(o, "zones") == 0) { 395 zone_options_t* zone; 396 RBTREE_FOR(zone, zone_options_t*, opt->zone_options) 397 quote(zone->name); 398 return; 399 } 400 if(strcasecmp(o, "patterns") == 0) { 401 pattern_options_t* p; 402 RBTREE_FOR(p, pattern_options_t*, opt->patterns) 403 quote(p->pname); 404 return; 405 } 406 printf("Server option not handled: %s\n", o); 407 exit(1); 408 } 409 } 410 411 /* print zone content items */ 412 static void print_zone_content_elems(pattern_options_t* pat) 413 { 414 if(pat->zonefile) 415 print_string_var("zonefile:", pat->zonefile); 416 #ifdef RATELIMIT 417 zone_print_rrl_whitelist("\trrl-whitelist: ", pat->rrl_whitelist); 418 #endif 419 print_acl("allow-notify:", pat->allow_notify); 420 print_acl("request-xfr:", pat->request_xfr); 421 if(!pat->notify_retry_is_default) 422 printf("\tnotify-retry: %d\n", pat->notify_retry); 423 print_acl("notify:", pat->notify); 424 print_acl("provide-xfr:", pat->provide_xfr); 425 if(pat->zonestats) 426 print_string_var("zonestats:", pat->zonestats); 427 print_acl_ips("outgoing-interface:", pat->outgoing_interface); 428 if(!pat->allow_axfr_fallback_is_default) 429 printf("\tallow-axfr-fallback: %s\n", 430 pat->allow_axfr_fallback?"yes":"no"); 431 } 432 433 void 434 config_test_print_server(nsd_options_t* opt) 435 { 436 ip_address_option_t* ip; 437 key_options_t* key; 438 zone_options_t* zone; 439 pattern_options_t* pat; 440 441 printf("# Config settings.\n"); 442 printf("server:\n"); 443 printf("\tdebug-mode: %s\n", opt->debug_mode?"yes":"no"); 444 printf("\tip-transparent: %s\n", opt->ip_transparent?"yes":"no"); 445 printf("\treuseport: %s\n", opt->reuseport?"yes":"no"); 446 printf("\tdo-ip4: %s\n", opt->do_ip4?"yes":"no"); 447 printf("\tdo-ip6: %s\n", opt->do_ip6?"yes":"no"); 448 printf("\thide-version: %s\n", opt->hide_version?"yes":"no"); 449 print_string_var("database:", opt->database); 450 print_string_var("identity:", opt->identity); 451 print_string_var("version:", opt->version); 452 print_string_var("nsid:", opt->nsid); 453 print_string_var("logfile:", opt->logfile); 454 printf("\tserver_count: %d\n", opt->server_count); 455 printf("\ttcp_count: %d\n", opt->tcp_count); 456 printf("\ttcp_query_count: %d\n", opt->tcp_query_count); 457 printf("\ttcp_timeout: %d\n", opt->tcp_timeout); 458 printf("\tipv4-edns-size: %d\n", (int) opt->ipv4_edns_size); 459 printf("\tipv6-edns-size: %d\n", (int) opt->ipv6_edns_size); 460 print_string_var("pidfile:", opt->pidfile); 461 print_string_var("port:", opt->port); 462 printf("\tstatistics: %d\n", opt->statistics); 463 print_string_var("chroot:", opt->chroot); 464 print_string_var("username:", opt->username); 465 print_string_var("zonesdir:", opt->zonesdir); 466 print_string_var("xfrdfile:", opt->xfrdfile); 467 print_string_var("zonelistfile:", opt->zonelistfile); 468 print_string_var("xfrdir:", opt->xfrdir); 469 printf("\txfrd_reload_timeout: %d\n", opt->xfrd_reload_timeout); 470 printf("\tlog-time-ascii: %s\n", opt->log_time_ascii?"yes":"no"); 471 printf("\tround-robin: %s\n", opt->round_robin?"yes":"no"); 472 printf("\tverbosity: %d\n", opt->verbosity); 473 for(ip = opt->ip_addresses; ip; ip=ip->next) 474 { 475 print_string_var("ip-address:", ip->address); 476 } 477 #ifdef RATELIMIT 478 printf("\trrl-size: %d\n", (int)opt->rrl_size); 479 printf("\trrl-ratelimit: %d\n", (int)opt->rrl_ratelimit); 480 printf("\trrl-slip: %d\n", (int)opt->rrl_slip); 481 printf("\trrl-ipv4-prefix-length: %d\n", (int)opt->rrl_ipv4_prefix_length); 482 printf("\trrl-ipv6-prefix-length: %d\n", (int)opt->rrl_ipv6_prefix_length); 483 printf("\trrl-whitelist-ratelimit: %d\n", (int)opt->rrl_whitelist_ratelimit); 484 #endif 485 printf("\tzonefiles-check: %s\n", opt->zonefiles_check?"yes":"no"); 486 printf("\tzonefiles-write: %d\n", opt->zonefiles_write); 487 488 printf("\nremote-control:\n"); 489 printf("\tcontrol-enable: %s\n", opt->control_enable?"yes":"no"); 490 for(ip = opt->control_interface; ip; ip=ip->next) 491 print_string_var("control-interface:", ip->address); 492 printf("\tcontrol-port: %d\n", opt->control_port); 493 print_string_var("server-key-file:", opt->server_key_file); 494 print_string_var("server-cert-file:", opt->server_cert_file); 495 print_string_var("control-key-file:", opt->control_key_file); 496 print_string_var("control-cert-file:", opt->control_cert_file); 497 498 RBTREE_FOR(key, key_options_t*, opt->keys) 499 { 500 printf("\nkey:\n"); 501 print_string_var("name:", key->name); 502 print_string_var("algorithm:", key->algorithm); 503 print_string_var("secret:", key->secret); 504 } 505 RBTREE_FOR(pat, pattern_options_t*, opt->patterns) 506 { 507 if(pat->implicit) continue; 508 printf("\npattern:\n"); 509 print_string_var("name:", pat->pname); 510 print_zone_content_elems(pat); 511 } 512 RBTREE_FOR(zone, zone_options_t*, opt->zone_options) 513 { 514 if(!zone->part_of_config) 515 continue; 516 printf("\nzone:\n"); 517 print_string_var("name:", zone->name); 518 print_zone_content_elems(zone->pattern); 519 } 520 521 } 522 523 static void 524 append_trailing_slash(const char** dirname, region_type* region) 525 { 526 int l = strlen(*dirname); 527 if (l>0 && (*dirname)[l-1] != '/' && l < 0xffffff) { 528 char *dirname_slash = region_alloc(region, l+2); 529 memcpy(dirname_slash, *dirname, l+1); 530 strlcat(dirname_slash, "/", l+2); 531 *dirname = dirname_slash; 532 } 533 } 534 535 static int 536 file_inside_chroot(const char* fname, const char* chr) 537 { 538 /* true if filename starts with chroot or is not absolute */ 539 return ((fname && fname[0] && strncmp(fname, chr, strlen(chr)) == 0) || 540 (fname && fname[0] != '/')); 541 } 542 543 static int 544 additional_checks(nsd_options_t* opt, const char* filename) 545 { 546 zone_options_t* zone; 547 int errors = 0; 548 549 RBTREE_FOR(zone, zone_options_t*, opt->zone_options) 550 { 551 const dname_type* dname = dname_parse(opt->region, zone->name); /* memory leak. */ 552 if(!dname) { 553 fprintf(stderr, "%s: cannot parse zone name syntax for zone %s.\n", filename, zone->name); 554 errors ++; 555 } 556 if(zone->pattern->allow_notify && !zone->pattern->request_xfr) { 557 fprintf(stderr, "%s: zone %s has allow-notify but no request-xfr" 558 " items. Where can it get a zone transfer when a notify " 559 "is received?\n", filename, zone->name); 560 errors ++; 561 } 562 if(!zone_is_slave(zone) && (!zone->pattern->zonefile || 563 zone->pattern->zonefile[0] == 0)) { 564 fprintf(stderr, "%s: zone %s is a master zone but has " 565 "no zonefile. Where can the data come from?\n", 566 filename, zone->name); 567 errors ++; 568 } 569 } 570 571 #ifndef BIND8_STATS 572 if(opt->statistics > 0) 573 { 574 fprintf(stderr, "%s: 'statistics: %d' but BIND 8 statistics feature not enabled.\n", 575 filename, opt->statistics); 576 errors ++; 577 } 578 #endif 579 #ifndef HAVE_CHROOT 580 if(opt->chroot != 0) 581 { 582 fprintf(stderr, "%s: chroot %s given. chroot not supported on this platform.\n", 583 filename, opt->chroot); 584 errors ++; 585 } 586 #endif 587 if (opt->identity && strlen(opt->identity) > UCHAR_MAX) { 588 fprintf(stderr, "%s: server identity too long (%u characters)\n", 589 filename, (unsigned) strlen(opt->identity)); 590 errors ++; 591 } 592 if (opt->version && strlen(opt->version) > UCHAR_MAX) { 593 fprintf(stderr, "%s: server version too long (%u characters)\n", 594 filename, (unsigned) strlen(opt->version)); 595 errors ++; 596 } 597 598 /* not done here: parsing of ip-address. parsing of username. */ 599 600 if (opt->chroot && opt->chroot[0]) { 601 /* append trailing slash for strncmp checking */ 602 append_trailing_slash(&opt->chroot, opt->region); 603 append_trailing_slash(&opt->xfrdir, opt->region); 604 append_trailing_slash(&opt->zonesdir, opt->region); 605 606 /* zonesdir must be absolute and within chroot, 607 * all other pathnames may be relative to zonesdir */ 608 if (strncmp(opt->zonesdir, opt->chroot, strlen(opt->chroot)) != 0) { 609 fprintf(stderr, "%s: zonesdir %s has to be an absolute path that starts with the chroot path %s\n", 610 filename, opt->zonesdir, opt->chroot); 611 errors ++; 612 } 613 if (!file_inside_chroot(opt->pidfile, opt->chroot)) { 614 fprintf(stderr, "%s: pidfile %s is not relative to chroot %s.\n", 615 filename, opt->pidfile, opt->chroot); 616 errors ++; 617 } 618 if (!file_inside_chroot(opt->database, opt->chroot)) { 619 fprintf(stderr, "%s: database %s is not relative to chroot %s.\n", 620 filename, opt->database, opt->chroot); 621 errors ++; 622 } 623 if (!file_inside_chroot(opt->xfrdfile, opt->chroot)) { 624 fprintf(stderr, "%s: xfrdfile %s is not relative to chroot %s.\n", 625 filename, opt->xfrdfile, opt->chroot); 626 errors ++; 627 } 628 if (!file_inside_chroot(opt->zonelistfile, opt->chroot)) { 629 fprintf(stderr, "%s: zonelistfile %s is not relative to chroot %s.\n", 630 filename, opt->zonelistfile, opt->chroot); 631 errors ++; 632 } 633 if (!file_inside_chroot(opt->xfrdir, opt->chroot)) { 634 fprintf(stderr, "%s: xfrdir %s is not relative to chroot %s.\n", 635 filename, opt->xfrdir, opt->chroot); 636 errors ++; 637 } 638 } 639 640 if (atoi(opt->port) <= 0) { 641 fprintf(stderr, "%s: port number '%s' is not a positive number.\n", 642 filename, opt->port); 643 errors ++; 644 } 645 if(errors != 0) { 646 fprintf(stderr, "%s: %d semantic errors in %d zones, %d keys.\n", 647 filename, errors, (int)nsd_options_num_zones(opt), 648 (int)opt->keys->count); 649 } 650 651 return (errors == 0); 652 } 653 654 int 655 main(int argc, char* argv[]) 656 { 657 int c; 658 int verbose = 0; 659 int key_sec = 0; 660 int final = 0; 661 const char * conf_opt = NULL; /* what option do you want? Can be NULL -> print all */ 662 const char * conf_zone = NULL; /* what zone are we talking about */ 663 const char * conf_key = NULL; /* what key is needed */ 664 const char * conf_pat = NULL; /* what pattern is talked about */ 665 const char* configfile; 666 nsd_options_t *options; 667 668 log_init("nsd-checkconf"); 669 670 /* Parse the command line... */ 671 while ((c = getopt(argc, argv, "vfo:a:p:s:z:")) != -1) { 672 switch (c) { 673 case 'v': 674 verbose = 1; 675 verbosity++; 676 break; 677 case 'o': 678 conf_opt = optarg; 679 break; 680 case 'f': 681 final = 1; 682 break; 683 case 'p': 684 conf_pat = optarg; 685 break; 686 case 'a': 687 if (conf_key) { 688 fprintf(stderr, "Error: cannot combine -a with -s or other -a.\n"); 689 exit(1); 690 } 691 conf_key = optarg; 692 break; 693 case 's': 694 if (conf_key) { 695 fprintf(stderr, "Error: cannot combine -s with -a or other -s.\n"); 696 exit(1); 697 } 698 conf_key = optarg; 699 key_sec = 1; 700 break; 701 case 'z': 702 conf_zone = optarg; 703 break; 704 default: 705 usage(); 706 }; 707 } 708 argc -= optind; 709 argv += optind; 710 if (argc == 0 || argc>=2) { 711 usage(); 712 } 713 configfile = argv[0]; 714 715 /* read config file */ 716 options = nsd_options_create(region_create(xalloc, free)); 717 tsig_init(options->region); 718 if (!parse_options_file(options, configfile, NULL, NULL) || 719 !additional_checks(options, configfile)) { 720 exit(2); 721 } 722 if (conf_opt || conf_key) { 723 config_print_zone(options, conf_key, key_sec, 724 underscore(conf_opt), conf_zone, conf_pat, final); 725 } else { 726 if (verbose) { 727 printf("# Read file %s: %d patterns, %d fixed-zones, " 728 "%d keys.\n", 729 configfile, 730 (int)options->patterns->count, 731 (int)nsd_options_num_zones(options), 732 (int)options->keys->count); 733 config_test_print_server(options); 734 } 735 } 736 return 0; 737 } 738