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 "sldns/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)
165 struct sockaddr_storage a;
168 for(i=0; i<cfg->num_ifs; i++) {
169 if(!extstrtoaddr(cfg->ifs[i], &a, &alen)) {
170 fatal_exit("cannot parse interface specified as '%s'",
173 for(j=0; j<cfg->num_ifs; j++) {
174 if(i!=j && strcmp(cfg->ifs[i], cfg->ifs[j])==0)
175 fatal_exit("interface: %s present twice, "
176 "cannot bind same ports twice.",
180 for(i=0; i<cfg->num_out_ifs; i++) {
181 if(!ipstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT, &a, &alen) &&
182 !netblockstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT, &a, &alen, &d)) {
183 fatal_exit("cannot parse outgoing-interface "
184 "specified as '%s'", cfg->out_ifs[i]);
186 for(j=0; j<cfg->num_out_ifs; j++) {
187 if(i!=j && strcmp(cfg->out_ifs[i], cfg->out_ifs[j])==0)
188 fatal_exit("outgoing-interface: %s present "
189 "twice, cannot bind same ports twice.",
197 aclchecks(struct config_file* cfg)
200 struct sockaddr_storage a;
202 struct config_str2list* acl;
203 for(acl=cfg->acls; acl; acl = acl->next) {
204 if(!netblockstrtoaddr(acl->str, UNBOUND_DNS_PORT, &a, &alen,
206 fatal_exit("cannot parse access control address %s %s",
207 acl->str, acl->str2);
212 /** true if fname is a file */
214 is_file(const char* fname)
217 if(stat(fname, &buf) < 0) {
219 printf("warning: no search permission for one of the directories in path: %s\n", fname);
225 if(S_ISDIR(buf.st_mode)) {
226 printf("%s is not a file\n", fname);
232 /** true if fname is a directory */
234 is_dir(const char* fname)
237 if(stat(fname, &buf) < 0) {
239 printf("warning: no search permission for one of the directories in path: %s\n", fname);
245 if(!(S_ISDIR(buf.st_mode))) {
246 printf("%s is not a directory\n", fname);
252 /** get base dir of a fname */
257 if(!fname) fatal_exit("out of memory");
258 rev = strrchr(fname, '/');
259 if(!rev) return NULL;
260 if(fname == rev) return NULL;
265 /** check chroot for a file string */
267 check_chroot_string(const char* desc, char** ss,
268 const char* chrootdir, struct config_file* cfg)
272 *ss = fname_after_chroot(str, cfg, 1);
273 if(!*ss) fatal_exit("out of memory");
275 if(chrootdir && chrootdir[0])
276 fatal_exit("%s: \"%s\" does not exist in "
277 "chrootdir %s", desc, str, chrootdir);
279 fatal_exit("%s: \"%s\" does not exist",
282 /* put in a new full path for continued checking */
287 /** check file list, every file must be inside the chroot location */
289 check_chroot_filelist(const char* desc, struct config_strlist* list,
290 const char* chrootdir, struct config_file* cfg)
292 struct config_strlist* p;
293 for(p=list; p; p=p->next) {
294 check_chroot_string(desc, &p->str, chrootdir, cfg);
298 /** check file list, with wildcard processing */
300 check_chroot_filelist_wild(const char* desc, struct config_strlist* list,
301 const char* chrootdir, struct config_file* cfg)
303 struct config_strlist* p;
304 for(p=list; p; p=p->next) {
306 if(strchr(p->str, '*') || strchr(p->str, '[') ||
307 strchr(p->str, '?') || strchr(p->str, '{') ||
308 strchr(p->str, '~')) {
310 /* adjust whole pattern for chroot and check later */
311 p->str = fname_after_chroot(p->str, cfg, 1);
314 #endif /* HAVE_GLOB */
315 check_chroot_string(desc, &p->str, chrootdir, cfg);
319 /** check configuration for errors */
321 morechecks(struct config_file* cfg, const char* fname)
323 warn_hosts("stub-host", cfg->stubs);
324 warn_hosts("forward-host", cfg->forwards);
325 interfacechecks(cfg);
328 if(cfg->verbosity < 0)
329 fatal_exit("verbosity value < 0");
330 if(cfg->num_threads <= 0 || cfg->num_threads > 10000)
331 fatal_exit("num_threads value weird");
332 if(!cfg->do_ip4 && !cfg->do_ip6)
333 fatal_exit("ip4 and ip6 are both disabled, pointless");
334 if(!cfg->do_ip6 && cfg->prefer_ip6)
335 fatal_exit("cannot prefer and disable ip6, pointless");
336 if(!cfg->do_udp && !cfg->do_tcp)
337 fatal_exit("udp and tcp are both disabled, pointless");
338 if(cfg->edns_buffer_size > cfg->msg_buffer_size)
339 fatal_exit("edns-buffer-size larger than msg-buffer-size, "
340 "answers will not fit in processing buffer");
342 w_config_adjust_directory(cfg);
344 if(cfg->chrootdir && cfg->chrootdir[0] &&
345 cfg->chrootdir[strlen(cfg->chrootdir)-1] == '/')
346 fatal_exit("chootdir %s has trailing slash '/' please remove.",
348 if(cfg->chrootdir && cfg->chrootdir[0] &&
349 !is_dir(cfg->chrootdir)) {
350 fatal_exit("bad chroot directory");
352 if(cfg->chrootdir && cfg->chrootdir[0]) {
355 if(fname[0] != '/') {
356 if(getcwd(buf, sizeof(buf)) == NULL)
357 fatal_exit("getcwd: %s", strerror(errno));
358 (void)strlcat(buf, "/", sizeof(buf));
360 (void)strlcat(buf, fname, sizeof(buf));
361 if(strncmp(buf, cfg->chrootdir, strlen(cfg->chrootdir)) != 0)
362 fatal_exit("config file %s is not inside chroot %s",
363 buf, cfg->chrootdir);
365 if(cfg->directory && cfg->directory[0]) {
366 char* ad = fname_after_chroot(cfg->directory, cfg, 0);
367 if(!ad) fatal_exit("out of memory");
368 if(!is_dir(ad)) fatal_exit("bad chdir directory");
371 if( (cfg->chrootdir && cfg->chrootdir[0]) ||
372 (cfg->directory && cfg->directory[0])) {
373 if(cfg->pidfile && cfg->pidfile[0]) {
374 char* ad = (cfg->pidfile[0]=='/')?strdup(cfg->pidfile):
375 fname_after_chroot(cfg->pidfile, cfg, 1);
376 char* bd = basedir(ad);
377 if(bd && !is_dir(bd))
378 fatal_exit("pidfile directory does not exist");
381 if(cfg->logfile && cfg->logfile[0]) {
382 char* ad = fname_after_chroot(cfg->logfile, cfg, 1);
383 char* bd = basedir(ad);
384 if(bd && !is_dir(bd))
385 fatal_exit("logfile directory does not exist");
390 check_chroot_filelist("file with root-hints",
391 cfg->root_hints, cfg->chrootdir, cfg);
392 check_chroot_filelist("trust-anchor-file",
393 cfg->trust_anchor_file_list, cfg->chrootdir, cfg);
394 check_chroot_filelist("auto-trust-anchor-file",
395 cfg->auto_trust_anchor_file_list, cfg->chrootdir, cfg);
396 check_chroot_filelist_wild("trusted-keys-file",
397 cfg->trusted_keys_file_list, cfg->chrootdir, cfg);
398 check_chroot_string("dlv-anchor-file", &cfg->dlv_anchor_file,
399 cfg->chrootdir, cfg);
400 /* remove chroot setting so that modules are not stripping pathnames*/
401 free(cfg->chrootdir);
402 cfg->chrootdir = NULL;
404 if(strcmp(cfg->module_conf, "iterator") != 0
405 && strcmp(cfg->module_conf, "validator iterator") != 0
406 && strcmp(cfg->module_conf, "dns64 validator iterator") != 0
407 && strcmp(cfg->module_conf, "dns64 iterator") != 0
408 #ifdef WITH_PYTHONMODULE
409 && strcmp(cfg->module_conf, "python iterator") != 0
410 && strcmp(cfg->module_conf, "python validator iterator") != 0
411 && strcmp(cfg->module_conf, "validator python iterator") != 0
412 && strcmp(cfg->module_conf, "dns64 python iterator") != 0
413 && strcmp(cfg->module_conf, "dns64 python validator iterator") != 0
414 && strcmp(cfg->module_conf, "dns64 validator python iterator") != 0
415 && strcmp(cfg->module_conf, "python dns64 iterator") != 0
416 && strcmp(cfg->module_conf, "python dns64 validator iterator") != 0
419 && strcmp(cfg->module_conf, "validator cachedb iterator") != 0
420 && strcmp(cfg->module_conf, "cachedb iterator") != 0
421 && strcmp(cfg->module_conf, "dns64 validator cachedb iterator") != 0
422 && strcmp(cfg->module_conf, "dns64 cachedb iterator") != 0
423 && strcmp(cfg->module_conf, "python dns64 cachedb iterator") != 0
424 && strcmp(cfg->module_conf, "python dns64 validator cachedb iterator") != 0
425 && strcmp(cfg->module_conf, "dns64 python cachedb iterator") != 0
426 && strcmp(cfg->module_conf, "dns64 python validator cachedb iterator") != 0
427 && strcmp(cfg->module_conf, "python cachedb iterator") != 0
428 && strcmp(cfg->module_conf, "python validator cachedb iterator") != 0
429 && strcmp(cfg->module_conf, "cachedb python iterator") != 0
430 && strcmp(cfg->module_conf, "validator cachedb python iterator") != 0
431 && strcmp(cfg->module_conf, "validator python cachedb iterator") != 0
434 fatal_exit("module conf '%s' is not known to work",
439 if(cfg->username && cfg->username[0]) {
440 if(getpwnam(cfg->username) == NULL)
441 fatal_exit("user '%s' does not exist.", cfg->username);
442 # ifdef HAVE_ENDPWENT
447 if(cfg->remote_control_enable && cfg->remote_control_use_cert) {
448 check_chroot_string("server-key-file", &cfg->server_key_file,
449 cfg->chrootdir, cfg);
450 check_chroot_string("server-cert-file", &cfg->server_cert_file,
451 cfg->chrootdir, cfg);
452 if(!is_file(cfg->control_key_file))
453 fatal_exit("control-key-file: \"%s\" does not exist",
454 cfg->control_key_file);
455 if(!is_file(cfg->control_cert_file))
456 fatal_exit("control-cert-file: \"%s\" does not exist",
457 cfg->control_cert_file);
460 localzonechecks(cfg);
463 /** check forwards */
465 check_fwd(struct config_file* cfg)
467 struct iter_forwards* fwd = forwards_create();
468 if(!fwd || !forwards_apply_cfg(fwd, cfg)) {
469 fatal_exit("Could not set forward zones");
471 forwards_delete(fwd);
476 check_hints(struct config_file* cfg)
478 struct iter_hints* hints = hints_create();
479 if(!hints || !hints_apply_cfg(hints, cfg)) {
480 fatal_exit("Could not set root or stub hints");
485 /** check config file */
487 checkconf(const char* cfgfile, const char* opt, int final)
489 char oldwd[PATH_MAX];
490 struct config_file* cfg = config_create();
492 fatal_exit("out of memory");
494 if(!getcwd(oldwd, sizeof(oldwd))) {
495 log_err("cannot getcwd: %s", strerror(errno));
498 if(!config_read(cfg, cfgfile, NULL)) {
499 /* config_read prints messages to stderr */
503 if(oldwd[0] && chdir(oldwd) == -1)
504 log_err("cannot chdir(%s): %s", oldwd, strerror(errno));
506 print_option(cfg, opt, final);
510 morechecks(cfg, cfgfile);
511 check_mod(cfg, iter_get_funcblock());
512 check_mod(cfg, val_get_funcblock());
513 #ifdef WITH_PYTHONMODULE
514 if(strstr(cfg->module_conf, "python"))
515 check_mod(cfg, pythonmod_get_funcblock());
519 printf("unbound-checkconf: no errors in %s\n", cfgfile);
523 /** getopt global, in case header files fail to declare it. */
525 /** getopt global, in case header files fail to declare it. */
528 /** Main routine for checkconf */
529 int main(int argc, char* argv[])
534 const char* opt = NULL;
535 const char* cfgfile = CONFIGFILE;
536 log_ident_set("unbound-checkconf");
537 log_init(NULL, 0, NULL);
540 /* use registry config file in preference to compiletime location */
541 if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile")))
542 cfgfile = CONFIGFILE;
543 #endif /* USE_WINSOCK */
544 /* parse the options */
545 while( (c=getopt(argc, argv, "fho:")) != -1) {
561 if(argc != 0 && argc != 1)
566 checkconf(f, opt, final);