1 /* 2 * checkconf/unbound-checkconf.c - config file checker for unbound.conf file. 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * Neither the name of the NLNET LABS nor the names of its contributors may 20 * be used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /** 37 * \file 38 * 39 * The config checker checks for syntax and other errors in the unbound.conf 40 * file, and can be used to check for errors before the server is started 41 * or sigHUPped. 42 * Exit status 1 means an error. 43 */ 44 45 #include "config.h" 46 #include "util/log.h" 47 #include "util/config_file.h" 48 #include "util/module.h" 49 #include "util/net_help.h" 50 #include "util/regional.h" 51 #include "iterator/iterator.h" 52 #include "iterator/iter_fwd.h" 53 #include "iterator/iter_hints.h" 54 #include "validator/validator.h" 55 #include "services/localzone.h" 56 #include "sldns/sbuffer.h" 57 #ifdef HAVE_GETOPT_H 58 #include <getopt.h> 59 #endif 60 #ifdef HAVE_PWD_H 61 #include <pwd.h> 62 #endif 63 #ifdef HAVE_SYS_STAT_H 64 #include <sys/stat.h> 65 #endif 66 #ifdef HAVE_GLOB_H 67 #include <glob.h> 68 #endif 69 #ifdef WITH_PYTHONMODULE 70 #include "pythonmod/pythonmod.h" 71 #endif 72 73 /** Give checkconf usage, and exit (1). */ 74 static void 75 usage() 76 { 77 printf("Usage: unbound-checkconf [file]\n"); 78 printf(" Checks unbound configuration file for errors.\n"); 79 printf("file if omitted %s is used.\n", CONFIGFILE); 80 printf("-o option print value of option to stdout.\n"); 81 printf("-f output full pathname with chroot applied, eg. with -o pidfile.\n"); 82 printf("-h show this usage help.\n"); 83 printf("Version %s\n", PACKAGE_VERSION); 84 printf("BSD licensed, see LICENSE in source package for details.\n"); 85 printf("Report bugs to %s\n", PACKAGE_BUGREPORT); 86 exit(1); 87 } 88 89 /** 90 * Print given option to stdout 91 * @param cfg: config 92 * @param opt: option name without trailing :. 93 * This is different from config_set_option. 94 * @param final: if final pathname with chroot applied has to be printed. 95 */ 96 static void 97 print_option(struct config_file* cfg, const char* opt, int final) 98 { 99 if(strcmp(opt, "pidfile") == 0 && final) { 100 printf("%s\n", fname_after_chroot(cfg->pidfile, cfg, 1)); 101 return; 102 } 103 if(!config_get_option(cfg, opt, config_print_func, stdout)) 104 fatal_exit("cannot print option '%s'", opt); 105 } 106 107 /** check if module works with config */ 108 static void 109 check_mod(struct config_file* cfg, struct module_func_block* fb) 110 { 111 struct module_env env; 112 memset(&env, 0, sizeof(env)); 113 env.cfg = cfg; 114 env.scratch = regional_create(); 115 env.scratch_buffer = sldns_buffer_new(BUFSIZ); 116 if(!env.scratch || !env.scratch_buffer) 117 fatal_exit("out of memory"); 118 if(!(*fb->init)(&env, 0)) { 119 fatal_exit("bad config for %s module", fb->name); 120 } 121 (*fb->deinit)(&env, 0); 122 sldns_buffer_free(env.scratch_buffer); 123 regional_destroy(env.scratch); 124 } 125 126 /** check localzones */ 127 static void 128 localzonechecks(struct config_file* cfg) 129 { 130 struct local_zones* zs; 131 if(!(zs = local_zones_create())) 132 fatal_exit("out of memory"); 133 if(!local_zones_apply_cfg(zs, cfg)) 134 fatal_exit("failed local-zone, local-data configuration"); 135 local_zones_delete(zs); 136 } 137 138 /** emit warnings for IP in hosts */ 139 static void 140 warn_hosts(const char* typ, struct config_stub* list) 141 { 142 struct sockaddr_storage a; 143 socklen_t alen; 144 struct config_stub* s; 145 struct config_strlist* h; 146 for(s=list; s; s=s->next) { 147 for(h=s->hosts; h; h=h->next) { 148 if(extstrtoaddr(h->str, &a, &alen)) { 149 fprintf(stderr, "unbound-checkconf: warning:" 150 " %s %s: \"%s\" is an IP%s address, " 151 "and when looked up as a host name " 152 "during use may not resolve.\n", 153 s->name, typ, h->str, 154 addr_is_ip6(&a, alen)?"6":"4"); 155 } 156 } 157 } 158 } 159 160 /** check interface strings */ 161 static void 162 interfacechecks(struct config_file* cfg) 163 { 164 struct sockaddr_storage a; 165 socklen_t alen; 166 int i, j; 167 for(i=0; i<cfg->num_ifs; i++) { 168 if(!extstrtoaddr(cfg->ifs[i], &a, &alen)) { 169 fatal_exit("cannot parse interface specified as '%s'", 170 cfg->ifs[i]); 171 } 172 for(j=0; j<cfg->num_ifs; j++) { 173 if(i!=j && strcmp(cfg->ifs[i], cfg->ifs[j])==0) 174 fatal_exit("interface: %s present twice, " 175 "cannot bind same ports twice.", 176 cfg->ifs[i]); 177 } 178 } 179 for(i=0; i<cfg->num_out_ifs; i++) { 180 if(!ipstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT, 181 &a, &alen)) { 182 fatal_exit("cannot parse outgoing-interface " 183 "specified as '%s'", cfg->out_ifs[i]); 184 } 185 for(j=0; j<cfg->num_out_ifs; j++) { 186 if(i!=j && strcmp(cfg->out_ifs[i], cfg->out_ifs[j])==0) 187 fatal_exit("outgoing-interface: %s present " 188 "twice, cannot bind same ports twice.", 189 cfg->out_ifs[i]); 190 } 191 } 192 } 193 194 /** check acl ips */ 195 static void 196 aclchecks(struct config_file* cfg) 197 { 198 int d; 199 struct sockaddr_storage a; 200 socklen_t alen; 201 struct config_str2list* acl; 202 for(acl=cfg->acls; acl; acl = acl->next) { 203 if(!netblockstrtoaddr(acl->str, UNBOUND_DNS_PORT, &a, &alen, 204 &d)) { 205 fatal_exit("cannot parse access control address %s %s", 206 acl->str, acl->str2); 207 } 208 } 209 } 210 211 /** true if fname is a file */ 212 static int 213 is_file(const char* fname) 214 { 215 struct stat buf; 216 if(stat(fname, &buf) < 0) { 217 if(errno==EACCES) { 218 printf("warning: no search permission for one of the directories in path: %s\n", fname); 219 return 1; 220 } 221 perror(fname); 222 return 0; 223 } 224 if(S_ISDIR(buf.st_mode)) { 225 printf("%s is not a file\n", fname); 226 return 0; 227 } 228 return 1; 229 } 230 231 /** true if fname is a directory */ 232 static int 233 is_dir(const char* fname) 234 { 235 struct stat buf; 236 if(stat(fname, &buf) < 0) { 237 if(errno==EACCES) { 238 printf("warning: no search permission for one of the directories in path: %s\n", fname); 239 return 1; 240 } 241 perror(fname); 242 return 0; 243 } 244 if(!(S_ISDIR(buf.st_mode))) { 245 printf("%s is not a directory\n", fname); 246 return 0; 247 } 248 return 1; 249 } 250 251 /** get base dir of a fname */ 252 static char* 253 basedir(char* fname) 254 { 255 char* rev; 256 if(!fname) fatal_exit("out of memory"); 257 rev = strrchr(fname, '/'); 258 if(!rev) return NULL; 259 if(fname == rev) return NULL; 260 rev[0] = 0; 261 return fname; 262 } 263 264 /** check chroot for a file string */ 265 static void 266 check_chroot_string(const char* desc, char** ss, 267 const char* chrootdir, struct config_file* cfg) 268 { 269 char* str = *ss; 270 if(str && str[0]) { 271 *ss = fname_after_chroot(str, cfg, 1); 272 if(!*ss) fatal_exit("out of memory"); 273 if(!is_file(*ss)) { 274 if(chrootdir && chrootdir[0]) 275 fatal_exit("%s: \"%s\" does not exist in " 276 "chrootdir %s", desc, str, chrootdir); 277 else 278 fatal_exit("%s: \"%s\" does not exist", 279 desc, str); 280 } 281 /* put in a new full path for continued checking */ 282 free(str); 283 } 284 } 285 286 /** check file list, every file must be inside the chroot location */ 287 static void 288 check_chroot_filelist(const char* desc, struct config_strlist* list, 289 const char* chrootdir, struct config_file* cfg) 290 { 291 struct config_strlist* p; 292 for(p=list; p; p=p->next) { 293 check_chroot_string(desc, &p->str, chrootdir, cfg); 294 } 295 } 296 297 /** check file list, with wildcard processing */ 298 static void 299 check_chroot_filelist_wild(const char* desc, struct config_strlist* list, 300 const char* chrootdir, struct config_file* cfg) 301 { 302 struct config_strlist* p; 303 for(p=list; p; p=p->next) { 304 #ifdef HAVE_GLOB 305 if(strchr(p->str, '*') || strchr(p->str, '[') || 306 strchr(p->str, '?') || strchr(p->str, '{') || 307 strchr(p->str, '~')) { 308 char* s = p->str; 309 /* adjust whole pattern for chroot and check later */ 310 p->str = fname_after_chroot(p->str, cfg, 1); 311 free(s); 312 } else 313 #endif /* HAVE_GLOB */ 314 check_chroot_string(desc, &p->str, chrootdir, cfg); 315 } 316 } 317 318 /** check configuration for errors */ 319 static void 320 morechecks(struct config_file* cfg, const char* fname) 321 { 322 warn_hosts("stub-host", cfg->stubs); 323 warn_hosts("forward-host", cfg->forwards); 324 interfacechecks(cfg); 325 aclchecks(cfg); 326 327 if(cfg->verbosity < 0) 328 fatal_exit("verbosity value < 0"); 329 if(cfg->num_threads <= 0 || cfg->num_threads > 10000) 330 fatal_exit("num_threads value weird"); 331 if(!cfg->do_ip4 && !cfg->do_ip6) 332 fatal_exit("ip4 and ip6 are both disabled, pointless"); 333 if(!cfg->do_udp && !cfg->do_tcp) 334 fatal_exit("udp and tcp are both disabled, pointless"); 335 if(cfg->edns_buffer_size > cfg->msg_buffer_size) 336 fatal_exit("edns-buffer-size larger than msg-buffer-size, " 337 "answers will not fit in processing buffer"); 338 #ifdef UB_ON_WINDOWS 339 w_config_adjust_directory(cfg); 340 #endif 341 if(cfg->chrootdir && cfg->chrootdir[0] && 342 cfg->chrootdir[strlen(cfg->chrootdir)-1] == '/') 343 fatal_exit("chootdir %s has trailing slash '/' please remove.", 344 cfg->chrootdir); 345 if(cfg->chrootdir && cfg->chrootdir[0] && 346 !is_dir(cfg->chrootdir)) { 347 fatal_exit("bad chroot directory"); 348 } 349 if(cfg->chrootdir && cfg->chrootdir[0]) { 350 char buf[10240]; 351 buf[0] = 0; 352 if(fname[0] != '/') { 353 if(getcwd(buf, sizeof(buf)) == NULL) 354 fatal_exit("getcwd: %s", strerror(errno)); 355 (void)strlcat(buf, "/", sizeof(buf)); 356 } 357 (void)strlcat(buf, fname, sizeof(buf)); 358 if(strncmp(buf, cfg->chrootdir, strlen(cfg->chrootdir)) != 0) 359 fatal_exit("config file %s is not inside chroot %s", 360 buf, cfg->chrootdir); 361 } 362 if(cfg->directory && cfg->directory[0]) { 363 char* ad = fname_after_chroot(cfg->directory, cfg, 0); 364 if(!ad) fatal_exit("out of memory"); 365 if(!is_dir(ad)) fatal_exit("bad chdir directory"); 366 free(ad); 367 } 368 if( (cfg->chrootdir && cfg->chrootdir[0]) || 369 (cfg->directory && cfg->directory[0])) { 370 if(cfg->pidfile && cfg->pidfile[0]) { 371 char* ad = (cfg->pidfile[0]=='/')?strdup(cfg->pidfile): 372 fname_after_chroot(cfg->pidfile, cfg, 1); 373 char* bd = basedir(ad); 374 if(bd && !is_dir(bd)) 375 fatal_exit("pidfile directory does not exist"); 376 free(ad); 377 } 378 if(cfg->logfile && cfg->logfile[0]) { 379 char* ad = fname_after_chroot(cfg->logfile, cfg, 1); 380 char* bd = basedir(ad); 381 if(bd && !is_dir(bd)) 382 fatal_exit("logfile directory does not exist"); 383 free(ad); 384 } 385 } 386 387 check_chroot_filelist("file with root-hints", 388 cfg->root_hints, cfg->chrootdir, cfg); 389 check_chroot_filelist("trust-anchor-file", 390 cfg->trust_anchor_file_list, cfg->chrootdir, cfg); 391 check_chroot_filelist("auto-trust-anchor-file", 392 cfg->auto_trust_anchor_file_list, cfg->chrootdir, cfg); 393 check_chroot_filelist_wild("trusted-keys-file", 394 cfg->trusted_keys_file_list, cfg->chrootdir, cfg); 395 check_chroot_string("dlv-anchor-file", &cfg->dlv_anchor_file, 396 cfg->chrootdir, cfg); 397 /* remove chroot setting so that modules are not stripping pathnames*/ 398 free(cfg->chrootdir); 399 cfg->chrootdir = NULL; 400 401 if(strcmp(cfg->module_conf, "iterator") != 0 402 && strcmp(cfg->module_conf, "validator iterator") != 0 403 && strcmp(cfg->module_conf, "dns64 validator iterator") != 0 404 && strcmp(cfg->module_conf, "dns64 iterator") != 0 405 #ifdef WITH_PYTHONMODULE 406 && strcmp(cfg->module_conf, "python iterator") != 0 407 && strcmp(cfg->module_conf, "python validator iterator") != 0 408 && strcmp(cfg->module_conf, "validator python iterator") != 0 409 && strcmp(cfg->module_conf, "dns64 python iterator") != 0 410 && strcmp(cfg->module_conf, "dns64 python validator iterator") != 0 411 && strcmp(cfg->module_conf, "dns64 validator python iterator") != 0 412 && strcmp(cfg->module_conf, "python dns64 iterator") != 0 413 && strcmp(cfg->module_conf, "python dns64 validator iterator") != 0 414 #endif 415 #ifdef USE_CACHEDB 416 && strcmp(cfg->module_conf, "validator cachedb iterator") != 0 417 && strcmp(cfg->module_conf, "cachedb iterator") != 0 418 && strcmp(cfg->module_conf, "dns64 validator cachedb iterator") != 0 419 && strcmp(cfg->module_conf, "dns64 cachedb iterator") != 0 420 && strcmp(cfg->module_conf, "python dns64 cachedb iterator") != 0 421 && strcmp(cfg->module_conf, "python dns64 validator cachedb iterator") != 0 422 && strcmp(cfg->module_conf, "dns64 python cachedb iterator") != 0 423 && strcmp(cfg->module_conf, "dns64 python validator cachedb iterator") != 0 424 && strcmp(cfg->module_conf, "python cachedb iterator") != 0 425 && strcmp(cfg->module_conf, "python validator cachedb iterator") != 0 426 && strcmp(cfg->module_conf, "cachedb python iterator") != 0 427 && strcmp(cfg->module_conf, "validator cachedb python iterator") != 0 428 && strcmp(cfg->module_conf, "validator python cachedb iterator") != 0 429 #endif 430 ) { 431 fatal_exit("module conf '%s' is not known to work", 432 cfg->module_conf); 433 } 434 435 #ifdef HAVE_GETPWNAM 436 if(cfg->username && cfg->username[0]) { 437 if(getpwnam(cfg->username) == NULL) 438 fatal_exit("user '%s' does not exist.", cfg->username); 439 endpwent(); 440 } 441 #endif 442 if(cfg->remote_control_enable && cfg->remote_control_use_cert) { 443 check_chroot_string("server-key-file", &cfg->server_key_file, 444 cfg->chrootdir, cfg); 445 check_chroot_string("server-cert-file", &cfg->server_cert_file, 446 cfg->chrootdir, cfg); 447 if(!is_file(cfg->control_key_file)) 448 fatal_exit("control-key-file: \"%s\" does not exist", 449 cfg->control_key_file); 450 if(!is_file(cfg->control_cert_file)) 451 fatal_exit("control-cert-file: \"%s\" does not exist", 452 cfg->control_cert_file); 453 } 454 455 localzonechecks(cfg); 456 } 457 458 /** check forwards */ 459 static void 460 check_fwd(struct config_file* cfg) 461 { 462 struct iter_forwards* fwd = forwards_create(); 463 if(!fwd || !forwards_apply_cfg(fwd, cfg)) { 464 fatal_exit("Could not set forward zones"); 465 } 466 forwards_delete(fwd); 467 } 468 469 /** check hints */ 470 static void 471 check_hints(struct config_file* cfg) 472 { 473 struct iter_hints* hints = hints_create(); 474 if(!hints || !hints_apply_cfg(hints, cfg)) { 475 fatal_exit("Could not set root or stub hints"); 476 } 477 hints_delete(hints); 478 } 479 480 /** check config file */ 481 static void 482 checkconf(const char* cfgfile, const char* opt, int final) 483 { 484 struct config_file* cfg = config_create(); 485 if(!cfg) 486 fatal_exit("out of memory"); 487 if(!config_read(cfg, cfgfile, NULL)) { 488 /* config_read prints messages to stderr */ 489 config_delete(cfg); 490 exit(1); 491 } 492 if(opt) { 493 print_option(cfg, opt, final); 494 config_delete(cfg); 495 return; 496 } 497 morechecks(cfg, cfgfile); 498 check_mod(cfg, iter_get_funcblock()); 499 check_mod(cfg, val_get_funcblock()); 500 #ifdef WITH_PYTHONMODULE 501 if(strstr(cfg->module_conf, "python")) 502 check_mod(cfg, pythonmod_get_funcblock()); 503 #endif 504 check_fwd(cfg); 505 check_hints(cfg); 506 printf("unbound-checkconf: no errors in %s\n", cfgfile); 507 config_delete(cfg); 508 } 509 510 /** getopt global, in case header files fail to declare it. */ 511 extern int optind; 512 /** getopt global, in case header files fail to declare it. */ 513 extern char* optarg; 514 515 /** Main routine for checkconf */ 516 int main(int argc, char* argv[]) 517 { 518 int c; 519 int final = 0; 520 const char* f; 521 const char* opt = NULL; 522 const char* cfgfile = CONFIGFILE; 523 log_ident_set("unbound-checkconf"); 524 log_init(NULL, 0, NULL); 525 checklock_start(); 526 #ifdef USE_WINSOCK 527 /* use registry config file in preference to compiletime location */ 528 if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile"))) 529 cfgfile = CONFIGFILE; 530 #endif /* USE_WINSOCK */ 531 /* parse the options */ 532 while( (c=getopt(argc, argv, "fho:")) != -1) { 533 switch(c) { 534 case 'f': 535 final = 1; 536 break; 537 case 'o': 538 opt = optarg; 539 break; 540 case '?': 541 case 'h': 542 default: 543 usage(); 544 } 545 } 546 argc -= optind; 547 argv += optind; 548 if(argc != 0 && argc != 1) 549 usage(); 550 if(argc == 1) 551 f = argv[0]; 552 else f = cfgfile; 553 checkconf(f, opt, final); 554 checklock_stop(); 555 return 0; 556 } 557