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