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 "ldns/sbuffer.h"
63 #ifdef HAVE_SYS_STAT_H
69 #ifdef WITH_PYTHONMODULE
70 #include "pythonmod/pythonmod.h"
73 /** Give checkconf usage, and exit (1). */
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);
90 * Print given option to stdout
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.
97 print_option(struct config_file* cfg, const char* opt, int final)
99 if(strcmp(opt, "pidfile") == 0 && final) {
100 printf("%s\n", fname_after_chroot(cfg->pidfile, cfg, 1));
103 if(!config_get_option(cfg, opt, config_print_func, stdout))
104 fatal_exit("cannot print option '%s'", opt);
107 /** check if module works with config */
109 check_mod(struct config_file* cfg, struct module_func_block* fb)
111 struct module_env env;
112 memset(&env, 0, sizeof(env));
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);
121 (*fb->deinit)(&env, 0);
122 sldns_buffer_free(env.scratch_buffer);
123 regional_destroy(env.scratch);
126 /** check localzones */
128 localzonechecks(struct config_file* cfg)
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);
138 /** emit warnings for IP in hosts */
140 warn_hosts(const char* typ, struct config_stub* list)
142 struct sockaddr_storage a;
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");
160 /** check interface strings */
162 interfacechecks(struct config_file* cfg)
164 struct sockaddr_storage a;
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'",
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.",
179 for(i=0; i<cfg->num_out_ifs; i++) {
180 if(!ipstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT,
182 fatal_exit("cannot parse outgoing-interface "
183 "specified as '%s'", cfg->out_ifs[i]);
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.",
196 aclchecks(struct config_file* cfg)
199 struct sockaddr_storage a;
201 struct config_str2list* acl;
202 for(acl=cfg->acls; acl; acl = acl->next) {
203 if(!netblockstrtoaddr(acl->str, UNBOUND_DNS_PORT, &a, &alen,
205 fatal_exit("cannot parse access control address %s %s",
206 acl->str, acl->str2);
211 /** true if fname is a file */
213 is_file(const char* fname)
216 if(stat(fname, &buf) < 0) {
218 printf("warning: no search permission for one of the directories in path: %s\n", fname);
224 if(S_ISDIR(buf.st_mode)) {
225 printf("%s is not a file\n", fname);
231 /** true if fname is a directory */
233 is_dir(const char* fname)
236 if(stat(fname, &buf) < 0) {
238 printf("warning: no search permission for one of the directories in path: %s\n", fname);
244 if(!(S_ISDIR(buf.st_mode))) {
245 printf("%s is not a directory\n", fname);
251 /** get base dir of a fname */
256 if(!fname) fatal_exit("out of memory");
257 rev = strrchr(fname, '/');
258 if(!rev) return NULL;
259 if(fname == rev) return NULL;
264 /** check chroot for a file string */
266 check_chroot_string(const char* desc, char** ss,
267 const char* chrootdir, struct config_file* cfg)
271 *ss = fname_after_chroot(str, cfg, 1);
272 if(!*ss) fatal_exit("out of memory");
274 if(chrootdir && chrootdir[0])
275 fatal_exit("%s: \"%s\" does not exist in "
276 "chrootdir %s", desc, str, chrootdir);
278 fatal_exit("%s: \"%s\" does not exist",
281 /* put in a new full path for continued checking */
286 /** check file list, every file must be inside the chroot location */
288 check_chroot_filelist(const char* desc, struct config_strlist* list,
289 const char* chrootdir, struct config_file* cfg)
291 struct config_strlist* p;
292 for(p=list; p; p=p->next) {
293 check_chroot_string(desc, &p->str, chrootdir, cfg);
297 /** check file list, with wildcard processing */
299 check_chroot_filelist_wild(const char* desc, struct config_strlist* list,
300 const char* chrootdir, struct config_file* cfg)
302 struct config_strlist* p;
303 for(p=list; p; p=p->next) {
305 if(strchr(p->str, '*') || strchr(p->str, '[') ||
306 strchr(p->str, '?') || strchr(p->str, '{') ||
307 strchr(p->str, '~')) {
309 /* adjust whole pattern for chroot and check later */
310 p->str = fname_after_chroot(p->str, cfg, 1);
313 #endif /* HAVE_GLOB */
314 check_chroot_string(desc, &p->str, chrootdir, cfg);
318 /** check configuration for errors */
320 morechecks(struct config_file* cfg, const char* fname)
322 warn_hosts("stub-host", cfg->stubs);
323 warn_hosts("forward-host", cfg->forwards);
324 interfacechecks(cfg);
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");
339 if(cfg->chrootdir && cfg->chrootdir[0] &&
340 cfg->chrootdir[strlen(cfg->chrootdir)-1] == '/')
341 fatal_exit("chootdir %s has trailing slash '/' please remove.",
343 if(cfg->chrootdir && cfg->chrootdir[0] &&
344 !is_dir(cfg->chrootdir)) {
345 fatal_exit("bad chroot directory");
347 if(cfg->chrootdir && cfg->chrootdir[0]) {
350 if(fname[0] != '/') {
351 if(getcwd(buf, sizeof(buf)) == NULL)
352 fatal_exit("getcwd: %s", strerror(errno));
353 (void)strlcat(buf, "/", sizeof(buf));
355 (void)strlcat(buf, fname, sizeof(buf));
356 if(strncmp(buf, cfg->chrootdir, strlen(cfg->chrootdir)) != 0)
357 fatal_exit("config file %s is not inside chroot %s",
358 buf, cfg->chrootdir);
360 if(cfg->directory && cfg->directory[0]) {
361 char* ad = fname_after_chroot(cfg->directory, cfg, 0);
362 if(!ad) fatal_exit("out of memory");
363 if(!is_dir(ad)) fatal_exit("bad chdir directory");
366 if( (cfg->chrootdir && cfg->chrootdir[0]) ||
367 (cfg->directory && cfg->directory[0])) {
368 if(cfg->pidfile && cfg->pidfile[0]) {
369 char* ad = (cfg->pidfile[0]=='/')?strdup(cfg->pidfile):
370 fname_after_chroot(cfg->pidfile, cfg, 1);
371 char* bd = basedir(ad);
372 if(bd && !is_dir(bd))
373 fatal_exit("pidfile directory does not exist");
376 if(cfg->logfile && cfg->logfile[0]) {
377 char* ad = fname_after_chroot(cfg->logfile, cfg, 1);
378 char* bd = basedir(ad);
379 if(bd && !is_dir(bd))
380 fatal_exit("logfile directory does not exist");
385 check_chroot_filelist("file with root-hints",
386 cfg->root_hints, cfg->chrootdir, cfg);
387 check_chroot_filelist("trust-anchor-file",
388 cfg->trust_anchor_file_list, cfg->chrootdir, cfg);
389 check_chroot_filelist("auto-trust-anchor-file",
390 cfg->auto_trust_anchor_file_list, cfg->chrootdir, cfg);
391 check_chroot_filelist_wild("trusted-keys-file",
392 cfg->trusted_keys_file_list, cfg->chrootdir, cfg);
393 check_chroot_string("dlv-anchor-file", &cfg->dlv_anchor_file,
394 cfg->chrootdir, cfg);
395 /* remove chroot setting so that modules are not stripping pathnames*/
396 free(cfg->chrootdir);
397 cfg->chrootdir = NULL;
399 if(strcmp(cfg->module_conf, "iterator") != 0
400 && strcmp(cfg->module_conf, "validator iterator") != 0
401 && strcmp(cfg->module_conf, "dns64 validator iterator") != 0
402 && strcmp(cfg->module_conf, "dns64 iterator") != 0
403 #ifdef WITH_PYTHONMODULE
404 && strcmp(cfg->module_conf, "python iterator") != 0
405 && strcmp(cfg->module_conf, "python validator iterator") != 0
406 && strcmp(cfg->module_conf, "validator python iterator") != 0
407 && strcmp(cfg->module_conf, "dns64 python iterator") != 0
408 && strcmp(cfg->module_conf, "dns64 python validator iterator") != 0
409 && strcmp(cfg->module_conf, "dns64 validator python iterator") != 0
410 && strcmp(cfg->module_conf, "python dns64 iterator") != 0
411 && strcmp(cfg->module_conf, "python dns64 validator iterator") != 0
414 fatal_exit("module conf '%s' is not known to work",
419 if(cfg->username && cfg->username[0]) {
420 if(getpwnam(cfg->username) == NULL)
421 fatal_exit("user '%s' does not exist.", cfg->username);
425 if(cfg->remote_control_enable && cfg->remote_control_use_cert) {
426 check_chroot_string("server-key-file", &cfg->server_key_file,
427 cfg->chrootdir, cfg);
428 check_chroot_string("server-cert-file", &cfg->server_cert_file,
429 cfg->chrootdir, cfg);
430 if(!is_file(cfg->control_key_file))
431 fatal_exit("control-key-file: \"%s\" does not exist",
432 cfg->control_key_file);
433 if(!is_file(cfg->control_cert_file))
434 fatal_exit("control-cert-file: \"%s\" does not exist",
435 cfg->control_cert_file);
438 localzonechecks(cfg);
441 /** check forwards */
443 check_fwd(struct config_file* cfg)
445 struct iter_forwards* fwd = forwards_create();
446 if(!fwd || !forwards_apply_cfg(fwd, cfg)) {
447 fatal_exit("Could not set forward zones");
449 forwards_delete(fwd);
454 check_hints(struct config_file* cfg)
456 struct iter_hints* hints = hints_create();
457 if(!hints || !hints_apply_cfg(hints, cfg)) {
458 fatal_exit("Could not set root or stub hints");
463 /** check config file */
465 checkconf(const char* cfgfile, const char* opt, int final)
467 struct config_file* cfg = config_create();
469 fatal_exit("out of memory");
470 if(!config_read(cfg, cfgfile, NULL)) {
471 /* config_read prints messages to stderr */
476 print_option(cfg, opt, final);
480 morechecks(cfg, cfgfile);
481 check_mod(cfg, iter_get_funcblock());
482 check_mod(cfg, val_get_funcblock());
483 #ifdef WITH_PYTHONMODULE
484 if(strstr(cfg->module_conf, "python"))
485 check_mod(cfg, pythonmod_get_funcblock());
489 printf("unbound-checkconf: no errors in %s\n", cfgfile);
493 /** getopt global, in case header files fail to declare it. */
495 /** getopt global, in case header files fail to declare it. */
498 /** Main routine for checkconf */
499 int main(int argc, char* argv[])
504 const char* opt = NULL;
505 const char* cfgfile = CONFIGFILE;
506 log_ident_set("unbound-checkconf");
507 log_init(NULL, 0, NULL);
510 /* use registry config file in preference to compiletime location */
511 if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile")))
512 cfgfile = CONFIGFILE;
513 #endif /* USE_WINSOCK */
514 /* parse the options */
515 while( (c=getopt(argc, argv, "fho:")) != -1) {
531 if(argc != 0 && argc != 1)
536 checkconf(f, opt, final);