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.
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 "services/view.h"
57 #include "respip/respip.h"
58 #include "sldns/sbuffer.h"
65 #ifdef HAVE_SYS_STAT_H
71 #ifdef WITH_PYTHONMODULE
72 #include "pythonmod/pythonmod.h"
75 #include "edns-subnet/subnet-whitelist.h"
78 /** Give checkconf usage, and exit (1). */
82 printf("Usage: unbound-checkconf [file]\n");
83 printf(" Checks unbound configuration file for errors.\n");
84 printf("file if omitted %s is used.\n", CONFIGFILE);
85 printf("-o option print value of option to stdout.\n");
86 printf("-f output full pathname with chroot applied, eg. with -o pidfile.\n");
87 printf("-h show this usage help.\n");
88 printf("Version %s\n", PACKAGE_VERSION);
89 printf("BSD licensed, see LICENSE in source package for details.\n");
90 printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
95 * Print given option to stdout
97 * @param opt: option name without trailing :.
98 * This is different from config_set_option.
99 * @param final: if final pathname with chroot applied has to be printed.
102 print_option(struct config_file* cfg, const char* opt, int final)
104 if(strcmp(opt, "pidfile") == 0 && final) {
105 char *p = fname_after_chroot(cfg->pidfile, cfg, 1);
106 if(!p) fatal_exit("out of memory");
111 if(!config_get_option(cfg, opt, config_print_func, stdout))
112 fatal_exit("cannot print option '%s'", opt);
115 /** check if module works with config */
117 check_mod(struct config_file* cfg, struct module_func_block* fb)
119 struct module_env env;
120 memset(&env, 0, sizeof(env));
122 env.scratch = regional_create();
123 env.scratch_buffer = sldns_buffer_new(BUFSIZ);
124 if(!env.scratch || !env.scratch_buffer)
125 fatal_exit("out of memory");
126 if(!edns_known_options_init(&env))
127 fatal_exit("out of memory");
128 if(!(*fb->init)(&env, 0)) {
129 fatal_exit("bad config for %s module", fb->name);
131 (*fb->deinit)(&env, 0);
132 sldns_buffer_free(env.scratch_buffer);
133 regional_destroy(env.scratch);
134 edns_known_options_delete(&env);
137 /** check localzones */
139 localzonechecks(struct config_file* cfg)
141 struct local_zones* zs;
142 if(!(zs = local_zones_create()))
143 fatal_exit("out of memory");
144 if(!local_zones_apply_cfg(zs, cfg))
145 fatal_exit("failed local-zone, local-data configuration");
146 local_zones_delete(zs);
149 /** check view and response-ip configuration */
151 view_and_respipchecks(struct config_file* cfg)
153 struct views* views = NULL;
154 struct respip_set* respip = NULL;
156 if(!(views = views_create()))
157 fatal_exit("Could not create views: out of memory");
158 if(!(respip = respip_set_create()))
159 fatal_exit("Could not create respip set: out of memory");
160 if(!views_apply_cfg(views, cfg))
161 fatal_exit("Could not set up views");
162 if(!respip_global_apply_cfg(respip, cfg))
163 fatal_exit("Could not setup respip set");
164 if(!respip_views_apply_cfg(views, cfg, &ignored))
165 fatal_exit("Could not setup per-view respip sets");
167 respip_set_delete(respip);
170 /** emit warnings for IP in hosts */
172 warn_hosts(const char* typ, struct config_stub* list)
174 struct sockaddr_storage a;
176 struct config_stub* s;
177 struct config_strlist* h;
178 for(s=list; s; s=s->next) {
179 for(h=s->hosts; h; h=h->next) {
180 if(extstrtoaddr(h->str, &a, &alen)) {
181 fprintf(stderr, "unbound-checkconf: warning:"
182 " %s %s: \"%s\" is an IP%s address, "
183 "and when looked up as a host name "
184 "during use may not resolve.\n",
185 s->name, typ, h->str,
186 addr_is_ip6(&a, alen)?"6":"4");
192 /** check interface strings */
194 interfacechecks(struct config_file* cfg)
197 struct sockaddr_storage a;
200 for(i=0; i<cfg->num_ifs; i++) {
201 if(!extstrtoaddr(cfg->ifs[i], &a, &alen)) {
202 fatal_exit("cannot parse interface specified as '%s'",
205 for(j=0; j<cfg->num_ifs; j++) {
206 if(i!=j && strcmp(cfg->ifs[i], cfg->ifs[j])==0)
207 fatal_exit("interface: %s present twice, "
208 "cannot bind same ports twice.",
212 for(i=0; i<cfg->num_out_ifs; i++) {
213 if(!ipstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT, &a, &alen) &&
214 !netblockstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT, &a, &alen, &d)) {
215 fatal_exit("cannot parse outgoing-interface "
216 "specified as '%s'", cfg->out_ifs[i]);
218 for(j=0; j<cfg->num_out_ifs; j++) {
219 if(i!=j && strcmp(cfg->out_ifs[i], cfg->out_ifs[j])==0)
220 fatal_exit("outgoing-interface: %s present "
221 "twice, cannot bind same ports twice.",
229 aclchecks(struct config_file* cfg)
232 struct sockaddr_storage a;
234 struct config_str2list* acl;
235 for(acl=cfg->acls; acl; acl = acl->next) {
236 if(!netblockstrtoaddr(acl->str, UNBOUND_DNS_PORT, &a, &alen,
238 fatal_exit("cannot parse access control address %s %s",
239 acl->str, acl->str2);
244 /** true if fname is a file */
246 is_file(const char* fname)
249 if(stat(fname, &buf) < 0) {
251 printf("warning: no search permission for one of the directories in path: %s\n", fname);
257 if(S_ISDIR(buf.st_mode)) {
258 printf("%s is not a file\n", fname);
264 /** true if fname is a directory */
266 is_dir(const char* fname)
269 if(stat(fname, &buf) < 0) {
271 printf("warning: no search permission for one of the directories in path: %s\n", fname);
277 if(!(S_ISDIR(buf.st_mode))) {
278 printf("%s is not a directory\n", fname);
284 /** get base dir of a fname */
289 if(!fname) fatal_exit("out of memory");
290 rev = strrchr(fname, '/');
291 if(!rev) return NULL;
292 if(fname == rev) return NULL;
297 /** check chroot for a file string */
299 check_chroot_string(const char* desc, char** ss,
300 const char* chrootdir, struct config_file* cfg)
304 *ss = fname_after_chroot(str, cfg, 1);
305 if(!*ss) fatal_exit("out of memory");
307 if(chrootdir && chrootdir[0])
308 fatal_exit("%s: \"%s\" does not exist in "
309 "chrootdir %s", desc, str, chrootdir);
311 fatal_exit("%s: \"%s\" does not exist",
314 /* put in a new full path for continued checking */
319 /** check file list, every file must be inside the chroot location */
321 check_chroot_filelist(const char* desc, struct config_strlist* list,
322 const char* chrootdir, struct config_file* cfg)
324 struct config_strlist* p;
325 for(p=list; p; p=p->next) {
326 check_chroot_string(desc, &p->str, chrootdir, cfg);
330 /** check file list, with wildcard processing */
332 check_chroot_filelist_wild(const char* desc, struct config_strlist* list,
333 const char* chrootdir, struct config_file* cfg)
335 struct config_strlist* p;
336 for(p=list; p; p=p->next) {
338 if(strchr(p->str, '*') || strchr(p->str, '[') ||
339 strchr(p->str, '?') || strchr(p->str, '{') ||
340 strchr(p->str, '~')) {
342 /* adjust whole pattern for chroot and check later */
343 p->str = fname_after_chroot(p->str, cfg, 1);
346 #endif /* HAVE_GLOB */
347 check_chroot_string(desc, &p->str, chrootdir, cfg);
352 /** check ECS configuration */
354 ecs_conf_checks(struct config_file* cfg)
356 struct ecs_whitelist* whitelist = NULL;
357 if(!(whitelist = ecs_whitelist_create()))
358 fatal_exit("Could not create ednssubnet whitelist: out of memory");
359 if(!ecs_whitelist_apply_cfg(whitelist, cfg))
360 fatal_exit("Could not setup ednssubnet whitelist");
361 ecs_whitelist_delete(whitelist);
363 #endif /* CLIENT_SUBNET */
365 /** check configuration for errors */
367 morechecks(struct config_file* cfg, const char* fname)
369 warn_hosts("stub-host", cfg->stubs);
370 warn_hosts("forward-host", cfg->forwards);
371 interfacechecks(cfg);
374 if(cfg->verbosity < 0)
375 fatal_exit("verbosity value < 0");
376 if(cfg->num_threads <= 0 || cfg->num_threads > 10000)
377 fatal_exit("num_threads value weird");
378 if(!cfg->do_ip4 && !cfg->do_ip6)
379 fatal_exit("ip4 and ip6 are both disabled, pointless");
380 if(!cfg->do_ip6 && cfg->prefer_ip6)
381 fatal_exit("cannot prefer and disable ip6, pointless");
382 if(!cfg->do_udp && !cfg->do_tcp)
383 fatal_exit("udp and tcp are both disabled, pointless");
384 if(cfg->edns_buffer_size > cfg->msg_buffer_size)
385 fatal_exit("edns-buffer-size larger than msg-buffer-size, "
386 "answers will not fit in processing buffer");
388 w_config_adjust_directory(cfg);
390 if(cfg->chrootdir && cfg->chrootdir[0] &&
391 cfg->chrootdir[strlen(cfg->chrootdir)-1] == '/')
392 fatal_exit("chootdir %s has trailing slash '/' please remove.",
394 if(cfg->chrootdir && cfg->chrootdir[0] &&
395 !is_dir(cfg->chrootdir)) {
396 fatal_exit("bad chroot directory");
398 if(cfg->chrootdir && cfg->chrootdir[0]) {
401 if(fname[0] != '/') {
402 if(getcwd(buf, sizeof(buf)) == NULL)
403 fatal_exit("getcwd: %s", strerror(errno));
404 (void)strlcat(buf, "/", sizeof(buf));
406 (void)strlcat(buf, fname, sizeof(buf));
407 if(strncmp(buf, cfg->chrootdir, strlen(cfg->chrootdir)) != 0)
408 fatal_exit("config file %s is not inside chroot %s",
409 buf, cfg->chrootdir);
411 if(cfg->directory && cfg->directory[0]) {
412 char* ad = fname_after_chroot(cfg->directory, cfg, 0);
413 if(!ad) fatal_exit("out of memory");
414 if(!is_dir(ad)) fatal_exit("bad chdir directory");
417 if( (cfg->chrootdir && cfg->chrootdir[0]) ||
418 (cfg->directory && cfg->directory[0])) {
419 if(cfg->pidfile && cfg->pidfile[0]) {
420 char* ad = (cfg->pidfile[0]=='/')?strdup(cfg->pidfile):
421 fname_after_chroot(cfg->pidfile, cfg, 1);
422 char* bd = basedir(ad);
423 if(bd && !is_dir(bd))
424 fatal_exit("pidfile directory does not exist");
427 if(cfg->logfile && cfg->logfile[0]) {
428 char* ad = fname_after_chroot(cfg->logfile, cfg, 1);
429 char* bd = basedir(ad);
430 if(bd && !is_dir(bd))
431 fatal_exit("logfile directory does not exist");
436 check_chroot_filelist("file with root-hints",
437 cfg->root_hints, cfg->chrootdir, cfg);
438 check_chroot_filelist("trust-anchor-file",
439 cfg->trust_anchor_file_list, cfg->chrootdir, cfg);
440 check_chroot_filelist("auto-trust-anchor-file",
441 cfg->auto_trust_anchor_file_list, cfg->chrootdir, cfg);
442 check_chroot_filelist_wild("trusted-keys-file",
443 cfg->trusted_keys_file_list, cfg->chrootdir, cfg);
444 check_chroot_string("dlv-anchor-file", &cfg->dlv_anchor_file,
445 cfg->chrootdir, cfg);
447 if(cfg->ipsecmod_enabled && strstr(cfg->module_conf, "ipsecmod")) {
448 /* only check hook if enabled */
449 check_chroot_string("ipsecmod-hook", &cfg->ipsecmod_hook,
450 cfg->chrootdir, cfg);
453 /* remove chroot setting so that modules are not stripping pathnames*/
454 free(cfg->chrootdir);
455 cfg->chrootdir = NULL;
457 /* There should be no reason for 'respip' module not to work with
458 * dns64, but it's not explicitly confirmed, so the combination is
459 * excluded below. It's simply unknown yet for the combination of
460 * respip and other modules. */
461 if(strcmp(cfg->module_conf, "iterator") != 0
462 && strcmp(cfg->module_conf, "validator iterator") != 0
463 && strcmp(cfg->module_conf, "dns64 validator iterator") != 0
464 && strcmp(cfg->module_conf, "dns64 iterator") != 0
465 && strcmp(cfg->module_conf, "respip iterator") != 0
466 && strcmp(cfg->module_conf, "respip validator iterator") != 0
467 #ifdef WITH_PYTHONMODULE
468 && strcmp(cfg->module_conf, "python iterator") != 0
469 && strcmp(cfg->module_conf, "python validator iterator") != 0
470 && strcmp(cfg->module_conf, "validator python iterator") != 0
471 && strcmp(cfg->module_conf, "dns64 python iterator") != 0
472 && strcmp(cfg->module_conf, "dns64 python validator iterator") != 0
473 && strcmp(cfg->module_conf, "dns64 validator python iterator") != 0
474 && strcmp(cfg->module_conf, "python dns64 iterator") != 0
475 && strcmp(cfg->module_conf, "python dns64 validator iterator") != 0
478 && strcmp(cfg->module_conf, "validator cachedb iterator") != 0
479 && strcmp(cfg->module_conf, "cachedb iterator") != 0
480 && strcmp(cfg->module_conf, "dns64 validator cachedb iterator") != 0
481 && strcmp(cfg->module_conf, "dns64 cachedb iterator") != 0
483 #if defined(WITH_PYTHONMODULE) && defined(USE_CACHEDB)
484 && strcmp(cfg->module_conf, "python dns64 cachedb iterator") != 0
485 && strcmp(cfg->module_conf, "python dns64 validator cachedb iterator") != 0
486 && strcmp(cfg->module_conf, "dns64 python cachedb iterator") != 0
487 && strcmp(cfg->module_conf, "dns64 python validator cachedb iterator") != 0
488 && strcmp(cfg->module_conf, "python cachedb iterator") != 0
489 && strcmp(cfg->module_conf, "python validator cachedb iterator") != 0
490 && strcmp(cfg->module_conf, "cachedb python iterator") != 0
491 && strcmp(cfg->module_conf, "validator cachedb python iterator") != 0
492 && strcmp(cfg->module_conf, "validator python cachedb iterator") != 0
495 && strcmp(cfg->module_conf, "subnetcache iterator") != 0
496 && strcmp(cfg->module_conf, "subnetcache validator iterator") != 0
497 && strcmp(cfg->module_conf, "dns64 subnetcache iterator") != 0
498 && strcmp(cfg->module_conf, "dns64 subnetcache validator iterator") != 0
500 #if defined(WITH_PYTHONMODULE) && defined(CLIENT_SUBNET)
501 && strcmp(cfg->module_conf, "python subnetcache iterator") != 0
502 && strcmp(cfg->module_conf, "subnetcache python iterator") != 0
503 && strcmp(cfg->module_conf, "subnetcache validator iterator") != 0
504 && strcmp(cfg->module_conf, "python subnetcache validator iterator") != 0
505 && strcmp(cfg->module_conf, "subnetcache python validator iterator") != 0
506 && strcmp(cfg->module_conf, "subnetcache validator python iterator") != 0
509 && strcmp(cfg->module_conf, "ipsecmod iterator") != 0
510 && strcmp(cfg->module_conf, "ipsecmod validator iterator") != 0
512 #if defined(WITH_PYTHONMODULE) && defined(USE_IPSECMOD)
513 && strcmp(cfg->module_conf, "python ipsecmod iterator") != 0
514 && strcmp(cfg->module_conf, "ipsecmod python iterator") != 0
515 && strcmp(cfg->module_conf, "ipsecmod validator iterator") != 0
516 && strcmp(cfg->module_conf, "python ipsecmod validator iterator") != 0
517 && strcmp(cfg->module_conf, "ipsecmod python validator iterator") != 0
518 && strcmp(cfg->module_conf, "ipsecmod validator python iterator") != 0
521 fatal_exit("module conf '%s' is not known to work",
526 if(cfg->username && cfg->username[0]) {
527 if(getpwnam(cfg->username) == NULL)
528 fatal_exit("user '%s' does not exist.", cfg->username);
529 # ifdef HAVE_ENDPWENT
534 if(cfg->remote_control_enable && cfg->remote_control_use_cert) {
535 check_chroot_string("server-key-file", &cfg->server_key_file,
536 cfg->chrootdir, cfg);
537 check_chroot_string("server-cert-file", &cfg->server_cert_file,
538 cfg->chrootdir, cfg);
539 if(!is_file(cfg->control_key_file))
540 fatal_exit("control-key-file: \"%s\" does not exist",
541 cfg->control_key_file);
542 if(!is_file(cfg->control_cert_file))
543 fatal_exit("control-cert-file: \"%s\" does not exist",
544 cfg->control_cert_file);
547 localzonechecks(cfg);
548 view_and_respipchecks(cfg);
550 ecs_conf_checks(cfg);
554 /** check forwards */
556 check_fwd(struct config_file* cfg)
558 struct iter_forwards* fwd = forwards_create();
559 if(!fwd || !forwards_apply_cfg(fwd, cfg)) {
560 fatal_exit("Could not set forward zones");
562 forwards_delete(fwd);
567 check_hints(struct config_file* cfg)
569 struct iter_hints* hints = hints_create();
570 if(!hints || !hints_apply_cfg(hints, cfg)) {
571 fatal_exit("Could not set root or stub hints");
576 /** check config file */
578 checkconf(const char* cfgfile, const char* opt, int final)
581 struct config_file* cfg = config_create();
583 fatal_exit("out of memory");
585 if(!getcwd(oldwd, sizeof(oldwd))) {
586 log_err("cannot getcwd: %s", strerror(errno));
589 if(!config_read(cfg, cfgfile, NULL)) {
590 /* config_read prints messages to stderr */
594 if(oldwd[0] && chdir(oldwd) == -1)
595 log_err("cannot chdir(%s): %s", oldwd, strerror(errno));
597 print_option(cfg, opt, final);
601 morechecks(cfg, cfgfile);
602 check_mod(cfg, iter_get_funcblock());
603 check_mod(cfg, val_get_funcblock());
604 #ifdef WITH_PYTHONMODULE
605 if(strstr(cfg->module_conf, "python"))
606 check_mod(cfg, pythonmod_get_funcblock());
610 printf("unbound-checkconf: no errors in %s\n", cfgfile);
614 /** getopt global, in case header files fail to declare it. */
616 /** getopt global, in case header files fail to declare it. */
619 /** Main routine for checkconf */
620 int main(int argc, char* argv[])
625 const char* opt = NULL;
626 const char* cfgfile = CONFIGFILE;
627 log_ident_set("unbound-checkconf");
628 log_init(NULL, 0, NULL);
631 /* use registry config file in preference to compiletime location */
632 if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile")))
633 cfgfile = CONFIGFILE;
634 #endif /* USE_WINSOCK */
635 /* parse the options */
636 while( (c=getopt(argc, argv, "fho:")) != -1) {
652 if(argc != 0 && argc != 1)
657 checkconf(f, opt, final);