unbound-checkconf.c revision 291767
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 "sldns/sbuffer.h"
57#ifdef HAVE_GETOPT_H
58#include <getopt.h>
59#endif
60#ifdef HAVE_PWD_H
61#include <pwd.h>
62#endif
63#ifdef HAVE_SYS_STAT_H
64#include <sys/stat.h>
65#endif
66#ifdef HAVE_GLOB_H
67#include <glob.h>
68#endif
69#ifdef WITH_PYTHONMODULE
70#include "pythonmod/pythonmod.h"
71#endif
72
73/** Give checkconf usage, and exit (1). */
74static void
75usage()
76{
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);
86	exit(1);
87}
88
89/**
90 * Print given option to stdout
91 * @param cfg: config
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.
95 */
96static void
97print_option(struct config_file* cfg, const char* opt, int final)
98{
99	if(strcmp(opt, "pidfile") == 0 && final) {
100		printf("%s\n", fname_after_chroot(cfg->pidfile, cfg, 1));
101		return;
102	}
103	if(!config_get_option(cfg, opt, config_print_func, stdout))
104		fatal_exit("cannot print option '%s'", opt);
105}
106
107/** check if module works with config */
108static void
109check_mod(struct config_file* cfg, struct module_func_block* fb)
110{
111	struct module_env env;
112	memset(&env, 0, sizeof(env));
113	env.cfg = cfg;
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);
120	}
121	(*fb->deinit)(&env, 0);
122	sldns_buffer_free(env.scratch_buffer);
123	regional_destroy(env.scratch);
124}
125
126/** check localzones */
127static void
128localzonechecks(struct config_file* cfg)
129{
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);
136}
137
138/** emit warnings for IP in hosts */
139static void
140warn_hosts(const char* typ, struct config_stub* list)
141{
142	struct sockaddr_storage a;
143	socklen_t alen;
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");
155			}
156		}
157	}
158}
159
160/** check interface strings */
161static void
162interfacechecks(struct config_file* cfg)
163{
164	struct sockaddr_storage a;
165	socklen_t alen;
166	int i, j;
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'",
170				cfg->ifs[i]);
171		}
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.",
176					cfg->ifs[i]);
177		}
178	}
179	for(i=0; i<cfg->num_out_ifs; i++) {
180		if(!ipstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT,
181			&a, &alen)) {
182			fatal_exit("cannot parse outgoing-interface "
183				"specified as '%s'", cfg->out_ifs[i]);
184		}
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.",
189					cfg->out_ifs[i]);
190		}
191	}
192}
193
194/** check acl ips */
195static void
196aclchecks(struct config_file* cfg)
197{
198	int d;
199	struct sockaddr_storage a;
200	socklen_t alen;
201	struct config_str2list* acl;
202	for(acl=cfg->acls; acl; acl = acl->next) {
203		if(!netblockstrtoaddr(acl->str, UNBOUND_DNS_PORT, &a, &alen,
204			&d)) {
205			fatal_exit("cannot parse access control address %s %s",
206				acl->str, acl->str2);
207		}
208	}
209}
210
211/** true if fname is a file */
212static int
213is_file(const char* fname)
214{
215	struct stat buf;
216	if(stat(fname, &buf) < 0) {
217		if(errno==EACCES) {
218			printf("warning: no search permission for one of the directories in path: %s\n", fname);
219			return 1;
220		}
221		perror(fname);
222		return 0;
223	}
224	if(S_ISDIR(buf.st_mode)) {
225		printf("%s is not a file\n", fname);
226		return 0;
227	}
228	return 1;
229}
230
231/** true if fname is a directory */
232static int
233is_dir(const char* fname)
234{
235	struct stat buf;
236	if(stat(fname, &buf) < 0) {
237		if(errno==EACCES) {
238			printf("warning: no search permission for one of the directories in path: %s\n", fname);
239			return 1;
240		}
241		perror(fname);
242		return 0;
243	}
244	if(!(S_ISDIR(buf.st_mode))) {
245		printf("%s is not a directory\n", fname);
246		return 0;
247	}
248	return 1;
249}
250
251/** get base dir of a fname */
252static char*
253basedir(char* fname)
254{
255	char* rev;
256	if(!fname) fatal_exit("out of memory");
257	rev = strrchr(fname, '/');
258	if(!rev) return NULL;
259	if(fname == rev) return NULL;
260	rev[0] = 0;
261	return fname;
262}
263
264/** check chroot for a file string */
265static void
266check_chroot_string(const char* desc, char** ss,
267	const char* chrootdir, struct config_file* cfg)
268{
269	char* str = *ss;
270	if(str && str[0]) {
271		*ss = fname_after_chroot(str, cfg, 1);
272		if(!*ss) fatal_exit("out of memory");
273		if(!is_file(*ss)) {
274			if(chrootdir && chrootdir[0])
275				fatal_exit("%s: \"%s\" does not exist in "
276					"chrootdir %s", desc, str, chrootdir);
277			else
278				fatal_exit("%s: \"%s\" does not exist",
279					desc, str);
280		}
281		/* put in a new full path for continued checking */
282		free(str);
283	}
284}
285
286/** check file list, every file must be inside the chroot location */
287static void
288check_chroot_filelist(const char* desc, struct config_strlist* list,
289	const char* chrootdir, struct config_file* cfg)
290{
291	struct config_strlist* p;
292	for(p=list; p; p=p->next) {
293		check_chroot_string(desc, &p->str, chrootdir, cfg);
294	}
295}
296
297/** check file list, with wildcard processing */
298static void
299check_chroot_filelist_wild(const char* desc, struct config_strlist* list,
300	const char* chrootdir, struct config_file* cfg)
301{
302	struct config_strlist* p;
303	for(p=list; p; p=p->next) {
304#ifdef HAVE_GLOB
305		if(strchr(p->str, '*') || strchr(p->str, '[') ||
306			strchr(p->str, '?') || strchr(p->str, '{') ||
307			strchr(p->str, '~')) {
308			char* s = p->str;
309			/* adjust whole pattern for chroot and check later */
310			p->str = fname_after_chroot(p->str, cfg, 1);
311			free(s);
312		} else
313#endif /* HAVE_GLOB */
314			check_chroot_string(desc, &p->str, chrootdir, cfg);
315	}
316}
317
318/** check configuration for errors */
319static void
320morechecks(struct config_file* cfg, const char* fname)
321{
322	warn_hosts("stub-host", cfg->stubs);
323	warn_hosts("forward-host", cfg->forwards);
324	interfacechecks(cfg);
325	aclchecks(cfg);
326
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");
338
339	if(cfg->chrootdir && cfg->chrootdir[0] &&
340		cfg->chrootdir[strlen(cfg->chrootdir)-1] == '/')
341		fatal_exit("chootdir %s has trailing slash '/' please remove.",
342			cfg->chrootdir);
343	if(cfg->chrootdir && cfg->chrootdir[0] &&
344		!is_dir(cfg->chrootdir)) {
345		fatal_exit("bad chroot directory");
346	}
347	if(cfg->chrootdir && cfg->chrootdir[0]) {
348		char buf[10240];
349		buf[0] = 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));
354		}
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);
359	}
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");
364		free(ad);
365	}
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");
374			free(ad);
375		}
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");
381			free(ad);
382		}
383	}
384
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;
398
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
412#endif
413		) {
414		fatal_exit("module conf '%s' is not known to work",
415			cfg->module_conf);
416	}
417
418#ifdef HAVE_GETPWNAM
419	if(cfg->username && cfg->username[0]) {
420		if(getpwnam(cfg->username) == NULL)
421			fatal_exit("user '%s' does not exist.", cfg->username);
422		endpwent();
423	}
424#endif
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);
436	}
437
438	localzonechecks(cfg);
439}
440
441/** check forwards */
442static void
443check_fwd(struct config_file* cfg)
444{
445	struct iter_forwards* fwd = forwards_create();
446	if(!fwd || !forwards_apply_cfg(fwd, cfg)) {
447		fatal_exit("Could not set forward zones");
448	}
449	forwards_delete(fwd);
450}
451
452/** check hints */
453static void
454check_hints(struct config_file* cfg)
455{
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");
459	}
460	hints_delete(hints);
461}
462
463/** check config file */
464static void
465checkconf(const char* cfgfile, const char* opt, int final)
466{
467	struct config_file* cfg = config_create();
468	if(!cfg)
469		fatal_exit("out of memory");
470	if(!config_read(cfg, cfgfile, NULL)) {
471		/* config_read prints messages to stderr */
472		config_delete(cfg);
473		exit(1);
474	}
475	if(opt) {
476		print_option(cfg, opt, final);
477		config_delete(cfg);
478		return;
479	}
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());
486#endif
487	check_fwd(cfg);
488	check_hints(cfg);
489	printf("unbound-checkconf: no errors in %s\n", cfgfile);
490	config_delete(cfg);
491}
492
493/** getopt global, in case header files fail to declare it. */
494extern int optind;
495/** getopt global, in case header files fail to declare it. */
496extern char* optarg;
497
498/** Main routine for checkconf */
499int main(int argc, char* argv[])
500{
501	int c;
502	int final = 0;
503	const char* f;
504	const char* opt = NULL;
505	const char* cfgfile = CONFIGFILE;
506	log_ident_set("unbound-checkconf");
507	log_init(NULL, 0, NULL);
508	checklock_start();
509#ifdef USE_WINSOCK
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) {
516		switch(c) {
517		case 'f':
518			final = 1;
519			break;
520		case 'o':
521			opt = optarg;
522			break;
523		case '?':
524		case 'h':
525		default:
526			usage();
527		}
528	}
529	argc -= optind;
530	argv += optind;
531	if(argc != 0 && argc != 1)
532		usage();
533	if(argc == 1)
534		f = argv[0];
535	else	f = cfgfile;
536	checkconf(f, opt, final);
537	checklock_stop();
538	return 0;
539}
540