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 ) { 416 fatal_exit("module conf '%s' is not known to work", 417 cfg->module_conf); 418 } 419 420 #ifdef HAVE_GETPWNAM 421 if(cfg->username && cfg->username[0]) { 422 if(getpwnam(cfg->username) == NULL) 423 fatal_exit("user '%s' does not exist.", cfg->username); 424 endpwent(); 425 } 426 #endif 427 if(cfg->remote_control_enable && cfg->remote_control_use_cert) { 428 check_chroot_string("server-key-file", &cfg->server_key_file, 429 cfg->chrootdir, cfg); 430 check_chroot_string("server-cert-file", &cfg->server_cert_file, 431 cfg->chrootdir, cfg); 432 if(!is_file(cfg->control_key_file)) 433 fatal_exit("control-key-file: \"%s\" does not exist", 434 cfg->control_key_file); 435 if(!is_file(cfg->control_cert_file)) 436 fatal_exit("control-cert-file: \"%s\" does not exist", 437 cfg->control_cert_file); 438 } 439 440 localzonechecks(cfg); 441 } 442 443 /** check forwards */ 444 static void 445 check_fwd(struct config_file* cfg) 446 { 447 struct iter_forwards* fwd = forwards_create(); 448 if(!fwd || !forwards_apply_cfg(fwd, cfg)) { 449 fatal_exit("Could not set forward zones"); 450 } 451 forwards_delete(fwd); 452 } 453 454 /** check hints */ 455 static void 456 check_hints(struct config_file* cfg) 457 { 458 struct iter_hints* hints = hints_create(); 459 if(!hints || !hints_apply_cfg(hints, cfg)) { 460 fatal_exit("Could not set root or stub hints"); 461 } 462 hints_delete(hints); 463 } 464 465 /** check config file */ 466 static void 467 checkconf(const char* cfgfile, const char* opt, int final) 468 { 469 struct config_file* cfg = config_create(); 470 if(!cfg) 471 fatal_exit("out of memory"); 472 if(!config_read(cfg, cfgfile, NULL)) { 473 /* config_read prints messages to stderr */ 474 config_delete(cfg); 475 exit(1); 476 } 477 if(opt) { 478 print_option(cfg, opt, final); 479 config_delete(cfg); 480 return; 481 } 482 morechecks(cfg, cfgfile); 483 check_mod(cfg, iter_get_funcblock()); 484 check_mod(cfg, val_get_funcblock()); 485 #ifdef WITH_PYTHONMODULE 486 if(strstr(cfg->module_conf, "python")) 487 check_mod(cfg, pythonmod_get_funcblock()); 488 #endif 489 check_fwd(cfg); 490 check_hints(cfg); 491 printf("unbound-checkconf: no errors in %s\n", cfgfile); 492 config_delete(cfg); 493 } 494 495 /** getopt global, in case header files fail to declare it. */ 496 extern int optind; 497 /** getopt global, in case header files fail to declare it. */ 498 extern char* optarg; 499 500 /** Main routine for checkconf */ 501 int main(int argc, char* argv[]) 502 { 503 int c; 504 int final = 0; 505 const char* f; 506 const char* opt = NULL; 507 const char* cfgfile = CONFIGFILE; 508 log_ident_set("unbound-checkconf"); 509 log_init(NULL, 0, NULL); 510 checklock_start(); 511 #ifdef USE_WINSOCK 512 /* use registry config file in preference to compiletime location */ 513 if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile"))) 514 cfgfile = CONFIGFILE; 515 #endif /* USE_WINSOCK */ 516 /* parse the options */ 517 while( (c=getopt(argc, argv, "fho:")) != -1) { 518 switch(c) { 519 case 'f': 520 final = 1; 521 break; 522 case 'o': 523 opt = optarg; 524 break; 525 case '?': 526 case 'h': 527 default: 528 usage(); 529 } 530 } 531 argc -= optind; 532 argv += optind; 533 if(argc != 0 && argc != 1) 534 usage(); 535 if(argc == 1) 536 f = argv[0]; 537 else f = cfgfile; 538 checkconf(f, opt, final); 539 checklock_stop(); 540 return 0; 541 } 542