2 * checkconf/unbound-checkconf.c - config file checker for unbound.conf file.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
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
42 * Exit status 1 means an error.
48 #include "util/config_file.h"
49 #include "util/module.h"
50 #include "util/net_help.h"
51 #include "util/regional.h"
52 #include "iterator/iterator.h"
53 #include "iterator/iter_fwd.h"
54 #include "iterator/iter_hints.h"
55 #include "validator/validator.h"
56 #include "services/localzone.h"
57 #include "services/view.h"
58 #include "services/authzone.h"
59 #include "respip/respip.h"
60 #include "sldns/sbuffer.h"
67 #ifdef HAVE_SYS_STAT_H
73 #ifdef WITH_PYTHONMODULE
74 #include "pythonmod/pythonmod.h"
77 #include "edns-subnet/subnet-whitelist.h"
80 /** Give checkconf usage, and exit (1). */
84 printf("Usage: local-unbound-checkconf [file]\n");
85 printf(" Checks unbound configuration file for errors.\n");
86 printf("file if omitted %s is used.\n", CONFIGFILE);
87 printf("-o option print value of option to stdout.\n");
88 printf("-f output full pathname with chroot applied, eg. with -o pidfile.\n");
89 printf("-h show this usage help.\n");
90 printf("Version %s\n", PACKAGE_VERSION);
91 printf("BSD licensed, see LICENSE in source package for details.\n");
92 printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
97 * Print given option to stdout
99 * @param opt: option name without trailing :.
100 * This is different from config_set_option.
101 * @param final: if final pathname with chroot applied has to be printed.
104 print_option(struct config_file* cfg, const char* opt, int final)
106 if(strcmp(opt, "pidfile") == 0 && final) {
107 char *p = fname_after_chroot(cfg->pidfile, cfg, 1);
108 if(!p) fatal_exit("out of memory");
113 if(strcmp(opt, "auto-trust-anchor-file") == 0 && final) {
114 struct config_strlist* s = cfg->auto_trust_anchor_file_list;
115 for(; s; s=s->next) {
116 char *p = fname_after_chroot(s->str, cfg, 1);
117 if(!p) fatal_exit("out of memory");
123 if(!config_get_option(cfg, opt, config_print_func, stdout))
124 fatal_exit("cannot print option '%s'", opt);
127 /** check if module works with config */
129 check_mod(struct config_file* cfg, struct module_func_block* fb)
131 struct module_env env;
132 memset(&env, 0, sizeof(env));
134 env.scratch = regional_create();
135 env.scratch_buffer = sldns_buffer_new(BUFSIZ);
136 if(!env.scratch || !env.scratch_buffer)
137 fatal_exit("out of memory");
138 if(!edns_known_options_init(&env))
139 fatal_exit("out of memory");
140 if(!(*fb->init)(&env, 0)) {
141 fatal_exit("bad config for %s module", fb->name);
143 (*fb->deinit)(&env, 0);
144 sldns_buffer_free(env.scratch_buffer);
145 regional_destroy(env.scratch);
146 edns_known_options_delete(&env);
149 /** check localzones */
151 localzonechecks(struct config_file* cfg)
153 struct local_zones* zs;
154 if(!(zs = local_zones_create()))
155 fatal_exit("out of memory");
156 if(!local_zones_apply_cfg(zs, cfg))
157 fatal_exit("failed local-zone, local-data configuration");
158 local_zones_delete(zs);
161 /** check view and response-ip configuration */
163 view_and_respipchecks(struct config_file* cfg)
165 struct views* views = NULL;
166 struct respip_set* respip = NULL;
168 if(!(views = views_create()))
169 fatal_exit("Could not create views: out of memory");
170 if(!(respip = respip_set_create()))
171 fatal_exit("Could not create respip set: out of memory");
172 if(!views_apply_cfg(views, cfg))
173 fatal_exit("Could not set up views");
174 if(!respip_global_apply_cfg(respip, cfg))
175 fatal_exit("Could not setup respip set");
176 if(!respip_views_apply_cfg(views, cfg, &ignored))
177 fatal_exit("Could not setup per-view respip sets");
179 respip_set_delete(respip);
182 /** emit warnings for IP in hosts */
184 warn_hosts(const char* typ, struct config_stub* list)
186 struct sockaddr_storage a;
188 struct config_stub* s;
189 struct config_strlist* h;
190 for(s=list; s; s=s->next) {
191 for(h=s->hosts; h; h=h->next) {
192 if(extstrtoaddr(h->str, &a, &alen)) {
193 fprintf(stderr, "unbound-checkconf: warning:"
194 " %s %s: \"%s\" is an IP%s address, "
195 "and when looked up as a host name "
196 "during use may not resolve.\n",
197 s->name, typ, h->str,
198 addr_is_ip6(&a, alen)?"6":"4");
204 /** check interface strings */
206 interfacechecks(struct config_file* cfg)
209 struct sockaddr_storage a;
212 for(i=0; i<cfg->num_ifs; i++) {
213 if(!extstrtoaddr(cfg->ifs[i], &a, &alen)) {
214 fatal_exit("cannot parse interface specified as '%s'",
217 for(j=0; j<cfg->num_ifs; j++) {
218 if(i!=j && strcmp(cfg->ifs[i], cfg->ifs[j])==0)
219 fatal_exit("interface: %s present twice, "
220 "cannot bind same ports twice.",
224 for(i=0; i<cfg->num_out_ifs; i++) {
225 if(!ipstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT, &a, &alen) &&
226 !netblockstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT, &a, &alen, &d)) {
227 fatal_exit("cannot parse outgoing-interface "
228 "specified as '%s'", cfg->out_ifs[i]);
230 for(j=0; j<cfg->num_out_ifs; j++) {
231 if(i!=j && strcmp(cfg->out_ifs[i], cfg->out_ifs[j])==0)
232 fatal_exit("outgoing-interface: %s present "
233 "twice, cannot bind same ports twice.",
241 aclchecks(struct config_file* cfg)
244 struct sockaddr_storage a;
246 struct config_str2list* acl;
247 for(acl=cfg->acls; acl; acl = acl->next) {
248 if(!netblockstrtoaddr(acl->str, UNBOUND_DNS_PORT, &a, &alen,
250 fatal_exit("cannot parse access control address %s %s",
251 acl->str, acl->str2);
256 /** check tcp connection limit ips */
258 tcpconnlimitchecks(struct config_file* cfg)
261 struct sockaddr_storage a;
263 struct config_str2list* tcl;
264 for(tcl=cfg->tcp_connection_limits; tcl; tcl = tcl->next) {
265 if(!netblockstrtoaddr(tcl->str, UNBOUND_DNS_PORT, &a, &alen,
267 fatal_exit("cannot parse tcp connection limit address %s %s",
268 tcl->str, tcl->str2);
273 /** true if fname is a file */
275 is_file(const char* fname)
278 if(stat(fname, &buf) < 0) {
280 printf("warning: no search permission for one of the directories in path: %s\n", fname);
286 if(S_ISDIR(buf.st_mode)) {
287 printf("%s is not a file\n", fname);
293 /** true if fname is a directory */
295 is_dir(const char* fname)
298 if(stat(fname, &buf) < 0) {
300 printf("warning: no search permission for one of the directories in path: %s\n", fname);
306 if(!(S_ISDIR(buf.st_mode))) {
307 printf("%s is not a directory\n", fname);
313 /** get base dir of a fname */
318 if(!fname) fatal_exit("out of memory");
319 rev = strrchr(fname, '/');
320 if(!rev) return NULL;
321 if(fname == rev) return NULL;
326 /** check chroot for a file string */
328 check_chroot_string(const char* desc, char** ss,
329 const char* chrootdir, struct config_file* cfg)
333 *ss = fname_after_chroot(str, cfg, 1);
334 if(!*ss) fatal_exit("out of memory");
336 if(chrootdir && chrootdir[0])
337 fatal_exit("%s: \"%s\" does not exist in "
338 "chrootdir %s", desc, str, chrootdir);
340 fatal_exit("%s: \"%s\" does not exist",
343 /* put in a new full path for continued checking */
348 /** check file list, every file must be inside the chroot location */
350 check_chroot_filelist(const char* desc, struct config_strlist* list,
351 const char* chrootdir, struct config_file* cfg)
353 struct config_strlist* p;
354 for(p=list; p; p=p->next) {
355 check_chroot_string(desc, &p->str, chrootdir, cfg);
359 /** check file list, with wildcard processing */
361 check_chroot_filelist_wild(const char* desc, struct config_strlist* list,
362 const char* chrootdir, struct config_file* cfg)
364 struct config_strlist* p;
365 for(p=list; p; p=p->next) {
367 if(strchr(p->str, '*') || strchr(p->str, '[') ||
368 strchr(p->str, '?') || strchr(p->str, '{') ||
369 strchr(p->str, '~')) {
371 /* adjust whole pattern for chroot and check later */
372 p->str = fname_after_chroot(p->str, cfg, 1);
375 #endif /* HAVE_GLOB */
376 check_chroot_string(desc, &p->str, chrootdir, cfg);
381 /** check ECS configuration */
383 ecs_conf_checks(struct config_file* cfg)
385 struct ecs_whitelist* whitelist = NULL;
386 if(!(whitelist = ecs_whitelist_create()))
387 fatal_exit("Could not create ednssubnet whitelist: out of memory");
388 if(!ecs_whitelist_apply_cfg(whitelist, cfg))
389 fatal_exit("Could not setup ednssubnet whitelist");
390 ecs_whitelist_delete(whitelist);
392 #endif /* CLIENT_SUBNET */
394 /** check that the modules exist, are compiled in */
396 check_modules_exist(const char* module_conf)
398 const char** names = module_list_avail();
399 const char* s = module_conf;
403 while(*s && isspace((unsigned char)*s))
407 if(strncmp(names[i], s, strlen(names[i])) == 0) {
418 for(j=0; j<sizeof(n)-1; j++) {
419 if(!s[j] || isspace((unsigned char)s[j])) {
425 fatal_exit("module_conf lists module '%s' but that "
426 "module is not available.", n);
428 s += strlen(names[i]);
432 /** check configuration for errors */
434 morechecks(struct config_file* cfg, const char* fname)
436 warn_hosts("stub-host", cfg->stubs);
437 warn_hosts("forward-host", cfg->forwards);
438 interfacechecks(cfg);
440 tcpconnlimitchecks(cfg);
442 if(cfg->verbosity < 0)
443 fatal_exit("verbosity value < 0");
444 if(cfg->num_threads <= 0 || cfg->num_threads > 10000)
445 fatal_exit("num_threads value weird");
446 if(!cfg->do_ip4 && !cfg->do_ip6)
447 fatal_exit("ip4 and ip6 are both disabled, pointless");
448 if(!cfg->do_ip6 && cfg->prefer_ip6)
449 fatal_exit("cannot prefer and disable ip6, pointless");
450 if(!cfg->do_udp && !cfg->do_tcp)
451 fatal_exit("udp and tcp are both disabled, pointless");
452 if(cfg->edns_buffer_size > cfg->msg_buffer_size)
453 fatal_exit("edns-buffer-size larger than msg-buffer-size, "
454 "answers will not fit in processing buffer");
456 w_config_adjust_directory(cfg);
458 if(cfg->chrootdir && cfg->chrootdir[0] &&
459 cfg->chrootdir[strlen(cfg->chrootdir)-1] == '/')
460 fatal_exit("chootdir %s has trailing slash '/' please remove.",
462 if(cfg->chrootdir && cfg->chrootdir[0] &&
463 !is_dir(cfg->chrootdir)) {
464 fatal_exit("bad chroot directory");
466 if(cfg->chrootdir && cfg->chrootdir[0]) {
469 if(fname[0] != '/') {
470 if(getcwd(buf, sizeof(buf)) == NULL)
471 fatal_exit("getcwd: %s", strerror(errno));
472 (void)strlcat(buf, "/", sizeof(buf));
474 (void)strlcat(buf, fname, sizeof(buf));
475 if(strncmp(buf, cfg->chrootdir, strlen(cfg->chrootdir)) != 0)
476 fatal_exit("config file %s is not inside chroot %s",
477 buf, cfg->chrootdir);
479 if(cfg->directory && cfg->directory[0]) {
480 char* ad = fname_after_chroot(cfg->directory, cfg, 0);
481 if(!ad) fatal_exit("out of memory");
482 if(!is_dir(ad)) fatal_exit("bad chdir directory");
485 if( (cfg->chrootdir && cfg->chrootdir[0]) ||
486 (cfg->directory && cfg->directory[0])) {
487 if(cfg->pidfile && cfg->pidfile[0]) {
488 char* ad = (cfg->pidfile[0]=='/')?strdup(cfg->pidfile):
489 fname_after_chroot(cfg->pidfile, cfg, 1);
490 char* bd = basedir(ad);
491 if(bd && !is_dir(bd))
492 fatal_exit("pidfile directory does not exist");
495 if(cfg->logfile && cfg->logfile[0]) {
496 char* ad = fname_after_chroot(cfg->logfile, cfg, 1);
497 char* bd = basedir(ad);
498 if(bd && !is_dir(bd))
499 fatal_exit("logfile directory does not exist");
504 check_chroot_filelist("file with root-hints",
505 cfg->root_hints, cfg->chrootdir, cfg);
506 check_chroot_filelist("trust-anchor-file",
507 cfg->trust_anchor_file_list, cfg->chrootdir, cfg);
508 check_chroot_filelist("auto-trust-anchor-file",
509 cfg->auto_trust_anchor_file_list, cfg->chrootdir, cfg);
510 check_chroot_filelist_wild("trusted-keys-file",
511 cfg->trusted_keys_file_list, cfg->chrootdir, cfg);
512 check_chroot_string("dlv-anchor-file", &cfg->dlv_anchor_file,
513 cfg->chrootdir, cfg);
515 if(cfg->ipsecmod_enabled && strstr(cfg->module_conf, "ipsecmod")) {
516 /* only check hook if enabled */
517 check_chroot_string("ipsecmod-hook", &cfg->ipsecmod_hook,
518 cfg->chrootdir, cfg);
521 /* remove chroot setting so that modules are not stripping pathnames*/
522 free(cfg->chrootdir);
523 cfg->chrootdir = NULL;
525 /* check that the modules listed in module_conf exist */
526 check_modules_exist(cfg->module_conf);
528 /* There should be no reason for 'respip' module not to work with
529 * dns64, but it's not explicitly confirmed, so the combination is
530 * excluded below. It's simply unknown yet for the combination of
531 * respip and other modules. */
532 if(strcmp(cfg->module_conf, "iterator") != 0
533 && strcmp(cfg->module_conf, "validator iterator") != 0
534 && strcmp(cfg->module_conf, "dns64 validator iterator") != 0
535 && strcmp(cfg->module_conf, "dns64 iterator") != 0
536 && strcmp(cfg->module_conf, "respip iterator") != 0
537 && strcmp(cfg->module_conf, "respip validator iterator") != 0
538 #ifdef WITH_PYTHONMODULE
539 && strcmp(cfg->module_conf, "python iterator") != 0
540 && strcmp(cfg->module_conf, "python validator iterator") != 0
541 && strcmp(cfg->module_conf, "validator python iterator") != 0
542 && strcmp(cfg->module_conf, "dns64 python iterator") != 0
543 && strcmp(cfg->module_conf, "dns64 python validator iterator") != 0
544 && strcmp(cfg->module_conf, "dns64 validator python iterator") != 0
545 && strcmp(cfg->module_conf, "python dns64 iterator") != 0
546 && strcmp(cfg->module_conf, "python dns64 validator iterator") != 0
549 && strcmp(cfg->module_conf, "validator cachedb iterator") != 0
550 && strcmp(cfg->module_conf, "cachedb iterator") != 0
551 && strcmp(cfg->module_conf, "dns64 validator cachedb iterator") != 0
552 && strcmp(cfg->module_conf, "dns64 cachedb iterator") != 0
554 #if defined(WITH_PYTHONMODULE) && defined(USE_CACHEDB)
555 && strcmp(cfg->module_conf, "python dns64 cachedb iterator") != 0
556 && strcmp(cfg->module_conf, "python dns64 validator cachedb iterator") != 0
557 && strcmp(cfg->module_conf, "dns64 python cachedb iterator") != 0
558 && strcmp(cfg->module_conf, "dns64 python validator cachedb iterator") != 0
559 && strcmp(cfg->module_conf, "python cachedb iterator") != 0
560 && strcmp(cfg->module_conf, "python validator cachedb iterator") != 0
561 && strcmp(cfg->module_conf, "cachedb python iterator") != 0
562 && strcmp(cfg->module_conf, "validator cachedb python iterator") != 0
563 && strcmp(cfg->module_conf, "validator python cachedb iterator") != 0
566 && strcmp(cfg->module_conf, "subnetcache iterator") != 0
567 && strcmp(cfg->module_conf, "subnetcache validator iterator") != 0
568 && strcmp(cfg->module_conf, "dns64 subnetcache iterator") != 0
569 && strcmp(cfg->module_conf, "dns64 subnetcache validator iterator") != 0
571 #if defined(WITH_PYTHONMODULE) && defined(CLIENT_SUBNET)
572 && strcmp(cfg->module_conf, "python subnetcache iterator") != 0
573 && strcmp(cfg->module_conf, "subnetcache python iterator") != 0
574 && strcmp(cfg->module_conf, "python subnetcache validator iterator") != 0
575 && strcmp(cfg->module_conf, "subnetcache python validator iterator") != 0
576 && strcmp(cfg->module_conf, "subnetcache validator python iterator") != 0
579 && strcmp(cfg->module_conf, "ipsecmod iterator") != 0
580 && strcmp(cfg->module_conf, "ipsecmod validator iterator") != 0
582 #if defined(WITH_PYTHONMODULE) && defined(USE_IPSECMOD)
583 && strcmp(cfg->module_conf, "python ipsecmod iterator") != 0
584 && strcmp(cfg->module_conf, "ipsecmod python iterator") != 0
585 && strcmp(cfg->module_conf, "ipsecmod validator iterator") != 0
586 && strcmp(cfg->module_conf, "python ipsecmod validator iterator") != 0
587 && strcmp(cfg->module_conf, "ipsecmod python validator iterator") != 0
588 && strcmp(cfg->module_conf, "ipsecmod validator python iterator") != 0
591 fatal_exit("module conf '%s' is not known to work",
596 if(cfg->username && cfg->username[0]) {
597 if(getpwnam(cfg->username) == NULL)
598 fatal_exit("user '%s' does not exist.", cfg->username);
599 # ifdef HAVE_ENDPWENT
604 if(cfg->remote_control_enable && options_remote_is_address(cfg)
605 && cfg->control_use_cert) {
606 check_chroot_string("server-key-file", &cfg->server_key_file,
607 cfg->chrootdir, cfg);
608 check_chroot_string("server-cert-file", &cfg->server_cert_file,
609 cfg->chrootdir, cfg);
610 if(!is_file(cfg->control_key_file))
611 fatal_exit("control-key-file: \"%s\" does not exist",
612 cfg->control_key_file);
613 if(!is_file(cfg->control_cert_file))
614 fatal_exit("control-cert-file: \"%s\" does not exist",
615 cfg->control_cert_file);
618 localzonechecks(cfg);
619 view_and_respipchecks(cfg);
621 ecs_conf_checks(cfg);
625 /** check forwards */
627 check_fwd(struct config_file* cfg)
629 struct iter_forwards* fwd = forwards_create();
630 if(!fwd || !forwards_apply_cfg(fwd, cfg)) {
631 fatal_exit("Could not set forward zones");
633 forwards_delete(fwd);
638 check_hints(struct config_file* cfg)
640 struct iter_hints* hints = hints_create();
641 if(!hints || !hints_apply_cfg(hints, cfg)) {
642 fatal_exit("Could not set root or stub hints");
647 /** check auth zones */
649 check_auth(struct config_file* cfg)
651 struct auth_zones* az = auth_zones_create();
652 if(!az || !auth_zones_apply_cfg(az, cfg, 0)) {
653 fatal_exit("Could not setup authority zones");
655 auth_zones_delete(az);
658 /** check config file */
660 checkconf(const char* cfgfile, const char* opt, int final)
663 struct config_file* cfg = config_create();
665 fatal_exit("out of memory");
667 if(!getcwd(oldwd, sizeof(oldwd))) {
668 log_err("cannot getcwd: %s", strerror(errno));
671 if(!config_read(cfg, cfgfile, NULL)) {
672 /* config_read prints messages to stderr */
676 if(oldwd[0] && chdir(oldwd) == -1)
677 log_err("cannot chdir(%s): %s", oldwd, strerror(errno));
679 print_option(cfg, opt, final);
683 morechecks(cfg, cfgfile);
684 check_mod(cfg, iter_get_funcblock());
685 check_mod(cfg, val_get_funcblock());
686 #ifdef WITH_PYTHONMODULE
687 if(strstr(cfg->module_conf, "python"))
688 check_mod(cfg, pythonmod_get_funcblock());
693 printf("unbound-checkconf: no errors in %s\n", cfgfile);
697 /** getopt global, in case header files fail to declare it. */
699 /** getopt global, in case header files fail to declare it. */
702 /** Main routine for checkconf */
703 int main(int argc, char* argv[])
708 const char* opt = NULL;
709 const char* cfgfile = CONFIGFILE;
710 log_ident_set("unbound-checkconf");
711 log_init(NULL, 0, NULL);
714 /* use registry config file in preference to compiletime location */
715 if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile")))
716 cfgfile = CONFIGFILE;
717 #endif /* USE_WINSOCK */
718 /* parse the options */
719 while( (c=getopt(argc, argv, "fho:")) != -1) {
735 if(argc != 0 && argc != 1)
740 checkconf(f, opt, final);