xref: /netbsd-src/external/bsd/unbound/dist/smallapp/unbound-checkconf.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
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
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.
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 "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"
59 #ifdef HAVE_GETOPT_H
60 #include <getopt.h>
61 #endif
62 #ifdef HAVE_PWD_H
63 #include <pwd.h>
64 #endif
65 #ifdef HAVE_SYS_STAT_H
66 #include <sys/stat.h>
67 #endif
68 #ifdef HAVE_GLOB_H
69 #include <glob.h>
70 #endif
71 #ifdef WITH_PYTHONMODULE
72 #include "pythonmod/pythonmod.h"
73 #endif
74 #ifdef CLIENT_SUBNET
75 #include "edns-subnet/subnet-whitelist.h"
76 #endif
77 
78 /** Give checkconf usage, and exit (1). */
79 static void
80 usage(void)
81 {
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);
91 	exit(1);
92 }
93 
94 /**
95  * Print given option to stdout
96  * @param cfg: config
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.
100  */
101 static void
102 print_option(struct config_file* cfg, const char* opt, int final)
103 {
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");
107 		printf("%s\n", p);
108 		free(p);
109 		return;
110 	}
111 	if(!config_get_option(cfg, opt, config_print_func, stdout))
112 		fatal_exit("cannot print option '%s'", opt);
113 }
114 
115 /** check if module works with config */
116 static void
117 check_mod(struct config_file* cfg, struct module_func_block* fb)
118 {
119 	struct module_env env;
120 	memset(&env, 0, sizeof(env));
121 	env.cfg = cfg;
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);
130 	}
131 	(*fb->deinit)(&env, 0);
132 	sldns_buffer_free(env.scratch_buffer);
133 	regional_destroy(env.scratch);
134 	edns_known_options_delete(&env);
135 }
136 
137 /** check localzones */
138 static void
139 localzonechecks(struct config_file* cfg)
140 {
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);
147 }
148 
149 /** check view and response-ip configuration */
150 static void
151 view_and_respipchecks(struct config_file* cfg)
152 {
153 	struct views* views = NULL;
154 	struct respip_set* respip = NULL;
155 	int ignored = 0;
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");
166 	views_delete(views);
167 	respip_set_delete(respip);
168 }
169 
170 /** emit warnings for IP in hosts */
171 static void
172 warn_hosts(const char* typ, struct config_stub* list)
173 {
174 	struct sockaddr_storage a;
175 	socklen_t alen;
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");
187 			}
188 		}
189 	}
190 }
191 
192 /** check interface strings */
193 static void
194 interfacechecks(struct config_file* cfg)
195 {
196 	int d;
197 	struct sockaddr_storage a;
198 	socklen_t alen;
199 	int i, j;
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'",
203 				cfg->ifs[i]);
204 		}
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.",
209 					cfg->ifs[i]);
210 		}
211 	}
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]);
217 		}
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.",
222 					cfg->out_ifs[i]);
223 		}
224 	}
225 }
226 
227 /** check acl ips */
228 static void
229 aclchecks(struct config_file* cfg)
230 {
231 	int d;
232 	struct sockaddr_storage a;
233 	socklen_t alen;
234 	struct config_str2list* acl;
235 	for(acl=cfg->acls; acl; acl = acl->next) {
236 		if(!netblockstrtoaddr(acl->str, UNBOUND_DNS_PORT, &a, &alen,
237 			&d)) {
238 			fatal_exit("cannot parse access control address %s %s",
239 				acl->str, acl->str2);
240 		}
241 	}
242 }
243 
244 /** true if fname is a file */
245 static int
246 is_file(const char* fname)
247 {
248 	struct stat buf;
249 	if(stat(fname, &buf) < 0) {
250 		if(errno==EACCES) {
251 			printf("warning: no search permission for one of the directories in path: %s\n", fname);
252 			return 1;
253 		}
254 		perror(fname);
255 		return 0;
256 	}
257 	if(S_ISDIR(buf.st_mode)) {
258 		printf("%s is not a file\n", fname);
259 		return 0;
260 	}
261 	return 1;
262 }
263 
264 /** true if fname is a directory */
265 static int
266 is_dir(const char* fname)
267 {
268 	struct stat buf;
269 	if(stat(fname, &buf) < 0) {
270 		if(errno==EACCES) {
271 			printf("warning: no search permission for one of the directories in path: %s\n", fname);
272 			return 1;
273 		}
274 		perror(fname);
275 		return 0;
276 	}
277 	if(!(S_ISDIR(buf.st_mode))) {
278 		printf("%s is not a directory\n", fname);
279 		return 0;
280 	}
281 	return 1;
282 }
283 
284 /** get base dir of a fname */
285 static char*
286 basedir(char* fname)
287 {
288 	char* rev;
289 	if(!fname) fatal_exit("out of memory");
290 	rev = strrchr(fname, '/');
291 	if(!rev) return NULL;
292 	if(fname == rev) return NULL;
293 	rev[0] = 0;
294 	return fname;
295 }
296 
297 /** check chroot for a file string */
298 static void
299 check_chroot_string(const char* desc, char** ss,
300 	const char* chrootdir, struct config_file* cfg)
301 {
302 	char* str = *ss;
303 	if(str && str[0]) {
304 		*ss = fname_after_chroot(str, cfg, 1);
305 		if(!*ss) fatal_exit("out of memory");
306 		if(!is_file(*ss)) {
307 			if(chrootdir && chrootdir[0])
308 				fatal_exit("%s: \"%s\" does not exist in "
309 					"chrootdir %s", desc, str, chrootdir);
310 			else
311 				fatal_exit("%s: \"%s\" does not exist",
312 					desc, str);
313 		}
314 		/* put in a new full path for continued checking */
315 		free(str);
316 	}
317 }
318 
319 /** check file list, every file must be inside the chroot location */
320 static void
321 check_chroot_filelist(const char* desc, struct config_strlist* list,
322 	const char* chrootdir, struct config_file* cfg)
323 {
324 	struct config_strlist* p;
325 	for(p=list; p; p=p->next) {
326 		check_chroot_string(desc, &p->str, chrootdir, cfg);
327 	}
328 }
329 
330 /** check file list, with wildcard processing */
331 static void
332 check_chroot_filelist_wild(const char* desc, struct config_strlist* list,
333 	const char* chrootdir, struct config_file* cfg)
334 {
335 	struct config_strlist* p;
336 	for(p=list; p; p=p->next) {
337 #ifdef HAVE_GLOB
338 		if(strchr(p->str, '*') || strchr(p->str, '[') ||
339 			strchr(p->str, '?') || strchr(p->str, '{') ||
340 			strchr(p->str, '~')) {
341 			char* s = p->str;
342 			/* adjust whole pattern for chroot and check later */
343 			p->str = fname_after_chroot(p->str, cfg, 1);
344 			free(s);
345 		} else
346 #endif /* HAVE_GLOB */
347 			check_chroot_string(desc, &p->str, chrootdir, cfg);
348 	}
349 }
350 
351 #ifdef CLIENT_SUBNET
352 /** check ECS configuration */
353 static void
354 ecs_conf_checks(struct config_file* cfg)
355 {
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);
362 }
363 #endif /* CLIENT_SUBNET */
364 
365 /** check configuration for errors */
366 static void
367 morechecks(struct config_file* cfg, const char* fname)
368 {
369 	warn_hosts("stub-host", cfg->stubs);
370 	warn_hosts("forward-host", cfg->forwards);
371 	interfacechecks(cfg);
372 	aclchecks(cfg);
373 
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");
387 #ifdef UB_ON_WINDOWS
388 	w_config_adjust_directory(cfg);
389 #endif
390 	if(cfg->chrootdir && cfg->chrootdir[0] &&
391 		cfg->chrootdir[strlen(cfg->chrootdir)-1] == '/')
392 		fatal_exit("chootdir %s has trailing slash '/' please remove.",
393 			cfg->chrootdir);
394 	if(cfg->chrootdir && cfg->chrootdir[0] &&
395 		!is_dir(cfg->chrootdir)) {
396 		fatal_exit("bad chroot directory");
397 	}
398 	if(cfg->chrootdir && cfg->chrootdir[0]) {
399 		char buf[10240];
400 		buf[0] = 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));
405 		}
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);
410 	}
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");
415 		free(ad);
416 	}
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");
425 			free(ad);
426 		}
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");
432 			free(ad);
433 		}
434 	}
435 
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);
446 #ifdef USE_IPSECMOD
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);
451 	}
452 #endif
453 	/* remove chroot setting so that modules are not stripping pathnames*/
454 	free(cfg->chrootdir);
455 	cfg->chrootdir = NULL;
456 
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
476 #endif
477 #ifdef USE_CACHEDB
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
482 #endif
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
493 #endif
494 #ifdef CLIENT_SUBNET
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
499 #endif
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
507 #endif
508 #ifdef USE_IPSECMOD
509 		&& strcmp(cfg->module_conf, "ipsecmod iterator") != 0
510 		&& strcmp(cfg->module_conf, "ipsecmod validator iterator") != 0
511 #endif
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
519 #endif
520 		) {
521 		fatal_exit("module conf '%s' is not known to work",
522 			cfg->module_conf);
523 	}
524 
525 #ifdef HAVE_GETPWNAM
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
530 		endpwent();
531 #  endif
532 	}
533 #endif
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);
545 	}
546 
547 	localzonechecks(cfg);
548 	view_and_respipchecks(cfg);
549 #ifdef CLIENT_SUBNET
550 	ecs_conf_checks(cfg);
551 #endif
552 }
553 
554 /** check forwards */
555 static void
556 check_fwd(struct config_file* cfg)
557 {
558 	struct iter_forwards* fwd = forwards_create();
559 	if(!fwd || !forwards_apply_cfg(fwd, cfg)) {
560 		fatal_exit("Could not set forward zones");
561 	}
562 	forwards_delete(fwd);
563 }
564 
565 /** check hints */
566 static void
567 check_hints(struct config_file* cfg)
568 {
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");
572 	}
573 	hints_delete(hints);
574 }
575 
576 /** check config file */
577 static void
578 checkconf(const char* cfgfile, const char* opt, int final)
579 {
580 	char oldwd[4096];
581 	struct config_file* cfg = config_create();
582 	if(!cfg)
583 		fatal_exit("out of memory");
584 	oldwd[0] = 0;
585 	if(!getcwd(oldwd, sizeof(oldwd))) {
586 		log_err("cannot getcwd: %s", strerror(errno));
587 		oldwd[0] = 0;
588 	}
589 	if(!config_read(cfg, cfgfile, NULL)) {
590 		/* config_read prints messages to stderr */
591 		config_delete(cfg);
592 		exit(1);
593 	}
594 	if(oldwd[0] && chdir(oldwd) == -1)
595 		log_err("cannot chdir(%s): %s", oldwd, strerror(errno));
596 	if(opt) {
597 		print_option(cfg, opt, final);
598 		config_delete(cfg);
599 		return;
600 	}
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());
607 #endif
608 	check_fwd(cfg);
609 	check_hints(cfg);
610 	printf("unbound-checkconf: no errors in %s\n", cfgfile);
611 	config_delete(cfg);
612 }
613 
614 /** getopt global, in case header files fail to declare it. */
615 extern int optind;
616 /** getopt global, in case header files fail to declare it. */
617 extern char* optarg;
618 
619 /** Main routine for checkconf */
620 int main(int argc, char* argv[])
621 {
622 	int c;
623 	int final = 0;
624 	const char* f;
625 	const char* opt = NULL;
626 	const char* cfgfile = CONFIGFILE;
627 	log_ident_set("unbound-checkconf");
628 	log_init(NULL, 0, NULL);
629 	checklock_start();
630 #ifdef USE_WINSOCK
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) {
637 		switch(c) {
638 		case 'f':
639 			final = 1;
640 			break;
641 		case 'o':
642 			opt = optarg;
643 			break;
644 		case '?':
645 		case 'h':
646 		default:
647 			usage();
648 		}
649 	}
650 	argc -= optind;
651 	argv += optind;
652 	if(argc != 0 && argc != 1)
653 		usage();
654 	if(argc == 1)
655 		f = argv[0];
656 	else	f = cfgfile;
657 	checkconf(f, opt, final);
658 	checklock_stop();
659 	return 0;
660 }
661