xref: /freebsd/contrib/unbound/smallapp/unbound-checkconf.c (revision 53120fbb68952b7d620c2c0e1cf05c5017fc1b27)
1 /*
2  * smallapp/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 <ctype.h>
47 #include "util/log.h"
48 #include "util/config_file.h"
49 #include "util/module.h"
50 #include "util/net_help.h"
51 #include "util/regional.h"
52 #include "iterator/iterator.h"
53 #include "iterator/iter_fwd.h"
54 #include "iterator/iter_hints.h"
55 #include "validator/validator.h"
56 #include "services/localzone.h"
57 #include "services/listen_dnsport.h"
58 #include "services/view.h"
59 #include "services/authzone.h"
60 #include "respip/respip.h"
61 #include "sldns/sbuffer.h"
62 #include "sldns/str2wire.h"
63 #ifdef HAVE_GETOPT_H
64 #include <getopt.h>
65 #endif
66 #ifdef HAVE_PWD_H
67 #include <pwd.h>
68 #endif
69 #ifdef HAVE_SYS_STAT_H
70 #include <sys/stat.h>
71 #endif
72 #ifdef HAVE_GLOB_H
73 #include <glob.h>
74 #endif
75 #ifdef WITH_PYTHONMODULE
76 #include "pythonmod/pythonmod.h"
77 #endif
78 #ifdef CLIENT_SUBNET
79 #include "edns-subnet/subnet-whitelist.h"
80 #endif
81 
82 /** Give checkconf usage, and exit (1). */
83 static void
84 usage(void)
85 {
86 	printf("Usage:	local-unbound-checkconf [file]\n");
87 	printf("	Checks unbound configuration file for errors.\n");
88 	printf("file	if omitted %s is used.\n", CONFIGFILE);
89 	printf("-o option	print value of option to stdout.\n");
90 	printf("-f 		output full pathname with chroot applied, eg. with -o pidfile.\n");
91 	printf("-q 		quiet (suppress output on success).\n");
92 	printf("-h		show this usage help.\n");
93 	printf("Version %s\n", PACKAGE_VERSION);
94 	printf("BSD licensed, see LICENSE in source package for details.\n");
95 	printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
96 	exit(1);
97 }
98 
99 /**
100  * Print given option to stdout
101  * @param cfg: config
102  * @param opt: option name without trailing :.
103  *	This is different from config_set_option.
104  * @param final: if final pathname with chroot applied has to be printed.
105  */
106 static void
107 print_option(struct config_file* cfg, const char* opt, int final)
108 {
109 	if(strcmp(opt, "pidfile") == 0 && final) {
110 		char *p = fname_after_chroot(cfg->pidfile, cfg, 1);
111 		if(!p) fatal_exit("out of memory");
112 		printf("%s\n", p);
113 		free(p);
114 		return;
115 	}
116 	if(strcmp(opt, "auto-trust-anchor-file") == 0 && final) {
117 		struct config_strlist* s = cfg->auto_trust_anchor_file_list;
118 		for(; s; s=s->next) {
119 			char *p = fname_after_chroot(s->str, cfg, 1);
120 			if(!p) fatal_exit("out of memory");
121 			printf("%s\n", p);
122 			free(p);
123 		}
124 		return;
125 	}
126 	if(!config_get_option(cfg, opt, config_print_func, stdout))
127 		fatal_exit("cannot print option '%s'", opt);
128 }
129 
130 /** check if module works with config */
131 static void
132 check_mod(struct config_file* cfg, struct module_func_block* fb)
133 {
134 	struct module_env env;
135 	memset(&env, 0, sizeof(env));
136 	env.cfg = cfg;
137 	env.scratch = regional_create();
138 	env.scratch_buffer = sldns_buffer_new(BUFSIZ);
139 	if(!env.scratch || !env.scratch_buffer)
140 		fatal_exit("out of memory");
141 	if(!edns_known_options_init(&env))
142 		fatal_exit("out of memory");
143 	if(!(*fb->init)(&env, 0)) {
144 		fatal_exit("bad config for %s module", fb->name);
145 	}
146 	(*fb->deinit)(&env, 0);
147 	sldns_buffer_free(env.scratch_buffer);
148 	regional_destroy(env.scratch);
149 	edns_known_options_delete(&env);
150 }
151 
152 /** true if addr is a localhost address, 127.0.0.1 or ::1 (with maybe "@port"
153  * after it) */
154 static int
155 str_addr_is_localhost(const char* a)
156 {
157 	if(strncmp(a, "127.", 4) == 0) return 1;
158 	if(strncmp(a, "::1", 3) == 0) return 1;
159 	return 0;
160 }
161 
162 /** check do-not-query-localhost */
163 static void
164 donotquerylocalhostcheck(struct config_file* cfg)
165 {
166 	if(cfg->donotquery_localhost) {
167 		struct config_stub* p;
168 		struct config_strlist* s;
169 		for(p=cfg->forwards; p; p=p->next) {
170 			for(s=p->addrs; s; s=s->next) {
171 				if(str_addr_is_localhost(s->str)) {
172 					fprintf(stderr, "unbound-checkconf: warning: forward-addr: '%s' is specified for forward-zone: '%s', but do-not-query-localhost: yes means that the address will not be used for lookups.\n",
173 						s->str, p->name);
174 				}
175 			}
176 		}
177 		for(p=cfg->stubs; p; p=p->next) {
178 			for(s=p->addrs; s; s=s->next) {
179 				if(str_addr_is_localhost(s->str)) {
180 					fprintf(stderr, "unbound-checkconf: warning: stub-addr: '%s' is specified for stub-zone: '%s', but do-not-query-localhost: yes means that the address will not be used for lookups.\n",
181 						s->str, p->name);
182 				}
183 			}
184 		}
185 	}
186 }
187 
188 /** check localzones */
189 static void
190 localzonechecks(struct config_file* cfg)
191 {
192 	struct local_zones* zs;
193 	if(!(zs = local_zones_create()))
194 		fatal_exit("out of memory");
195 	if(!local_zones_apply_cfg(zs, cfg))
196 		fatal_exit("failed local-zone, local-data configuration");
197 	local_zones_delete(zs);
198 }
199 
200 /** checks for acl and views */
201 static void
202 acl_view_tag_checks(struct config_file* cfg, struct views* views)
203 {
204 	int d;
205 	struct sockaddr_storage a;
206 	socklen_t alen;
207 	struct config_str2list* acl;
208 	struct config_str3list* s3;
209 	struct config_strbytelist* sb;
210 
211 	/* acl_view */
212 	for(acl=cfg->acl_view; acl; acl = acl->next) {
213 		struct view* v;
214 		if(!netblockstrtoaddr(acl->str, UNBOUND_DNS_PORT, &a, &alen,
215 			&d)) {
216 			fatal_exit("cannot parse access-control-view "
217 				"address %s %s", acl->str, acl->str2);
218 		}
219 		v = views_find_view(views, acl->str2, 0);
220 		if(!v) {
221 			fatal_exit("cannot find view for "
222 				"access-control-view: %s %s",
223 				acl->str, acl->str2);
224 		}
225 		lock_rw_unlock(&v->lock);
226 	}
227 
228 	/* acl_tags */
229 	for(sb=cfg->acl_tags; sb; sb = sb->next) {
230 		if(!netblockstrtoaddr(sb->str, UNBOUND_DNS_PORT, &a, &alen,
231 			&d)) {
232 			fatal_exit("cannot parse access-control-tags "
233 				"address %s", sb->str);
234 		}
235 	}
236 
237 	/* acl_tag_actions */
238 	for(s3=cfg->acl_tag_actions; s3; s3 = s3->next) {
239 		enum localzone_type t;
240 		if(!netblockstrtoaddr(s3->str, UNBOUND_DNS_PORT, &a, &alen,
241 			&d)) {
242 			fatal_exit("cannot parse access-control-tag-actions "
243 				"address %s %s %s",
244 				s3->str, s3->str2, s3->str3);
245 		}
246 		if(find_tag_id(cfg, s3->str2) == -1) {
247 			fatal_exit("cannot parse tag %s (define-tag it), "
248 				"for access-control-tag-actions: %s %s %s",
249 				s3->str2, s3->str, s3->str2, s3->str3);
250 		}
251 		if(!local_zone_str2type(s3->str3, &t)) {
252 			fatal_exit("cannot parse access control action type %s"
253 				" for access-control-tag-actions: %s %s %s",
254 				s3->str3, s3->str, s3->str2, s3->str3);
255 		}
256 	}
257 
258 	/* acl_tag_datas */
259 	for(s3=cfg->acl_tag_datas; s3; s3 = s3->next) {
260 		char buf[65536];
261 		uint8_t rr[LDNS_RR_BUF_SIZE];
262 		size_t len = sizeof(rr);
263 		int res;
264 		if(!netblockstrtoaddr(s3->str, UNBOUND_DNS_PORT, &a, &alen,
265 			&d)) {
266 			fatal_exit("cannot parse access-control-tag-datas address %s %s '%s'",
267 				s3->str, s3->str2, s3->str3);
268 		}
269 		if(find_tag_id(cfg, s3->str2) == -1) {
270 			fatal_exit("cannot parse tag %s (define-tag it), "
271 				"for access-control-tag-datas: %s %s '%s'",
272 				s3->str2, s3->str, s3->str2, s3->str3);
273 		}
274 		/* '.' is sufficient for validation, and it makes the call to
275 		 * sldns_wirerr_get_type() simpler below. */
276 		snprintf(buf, sizeof(buf), "%s %s", ".", s3->str3);
277 		res = sldns_str2wire_rr_buf(buf, rr, &len, NULL, 3600, NULL,
278 			0, NULL, 0);
279 		if(res != 0) {
280 			fatal_exit("cannot parse rr data [char %d] parse error %s, for access-control-tag-datas: %s %s '%s'",
281 				(int)LDNS_WIREPARSE_OFFSET(res)-2,
282 				sldns_get_errorstr_parse(res),
283 				s3->str, s3->str2, s3->str3);
284 		}
285 	}
286 }
287 
288 /** check view and response-ip configuration */
289 static void
290 view_and_respipchecks(struct config_file* cfg)
291 {
292 	struct views* views = NULL;
293 	struct respip_set* respip = NULL;
294 	int ignored = 0;
295 	if(!(views = views_create()))
296 		fatal_exit("Could not create views: out of memory");
297 	if(!(respip = respip_set_create()))
298 		fatal_exit("Could not create respip set: out of memory");
299 	if(!views_apply_cfg(views, cfg))
300 		fatal_exit("Could not set up views");
301 	if(!respip_global_apply_cfg(respip, cfg))
302 		fatal_exit("Could not setup respip set");
303 	if(!respip_views_apply_cfg(views, cfg, &ignored))
304 		fatal_exit("Could not setup per-view respip sets");
305 	acl_view_tag_checks(cfg, views);
306 	views_delete(views);
307 	respip_set_delete(respip);
308 }
309 
310 /** emit warnings for IP in hosts */
311 static void
312 warn_hosts(const char* typ, struct config_stub* list)
313 {
314 	struct sockaddr_storage a;
315 	socklen_t alen;
316 	struct config_stub* s;
317 	struct config_strlist* h;
318 	for(s=list; s; s=s->next) {
319 		for(h=s->hosts; h; h=h->next) {
320 			if(extstrtoaddr(h->str, &a, &alen, UNBOUND_DNS_PORT)) {
321 				fprintf(stderr, "unbound-checkconf: warning:"
322 				  " %s %s: \"%s\" is an IP%s address, "
323 				  "and when looked up as a host name "
324 				  "during use may not resolve.\n",
325 				  s->name, typ, h->str,
326 				  addr_is_ip6(&a, alen)?"6":"4");
327 			}
328 		}
329 	}
330 }
331 
332 /** check interface strings */
333 static void
334 interfacechecks(struct config_file* cfg)
335 {
336 	int d;
337 	struct sockaddr_storage a;
338 	socklen_t alen;
339 	int i, j, i2, j2;
340 	char*** resif = NULL;
341 	int* num_resif = NULL;
342 	char portbuf[32];
343 	snprintf(portbuf, sizeof(portbuf), "%d", cfg->port);
344 
345 	if(cfg->num_ifs != 0) {
346 		resif = (char***)calloc(cfg->num_ifs, sizeof(char**));
347 		if(!resif) fatal_exit("malloc failure");
348 		num_resif = (int*)calloc(cfg->num_ifs, sizeof(int));
349 		if(!num_resif) fatal_exit("malloc failure");
350 	}
351 	for(i=0; i<cfg->num_ifs; i++) {
352 		/* search for duplicates in IP or ifname arguments */
353 		for(i2=0; i2<i; i2++) {
354 			if(strcmp(cfg->ifs[i], cfg->ifs[i2]) == 0) {
355 				fatal_exit("interface: %s present twice, "
356 					"cannot bind same ports twice.",
357 					cfg->ifs[i]);
358 			}
359 		}
360 		if(!resolve_interface_names(&cfg->ifs[i], 1, NULL, &resif[i],
361 			&num_resif[i])) {
362 			fatal_exit("could not resolve interface names, for %s",
363 				cfg->ifs[i]);
364 		}
365 		/* check for port combinations that are not supported */
366 		if(if_is_pp2(resif[i][0], portbuf, cfg->proxy_protocol_port)) {
367 			if(if_is_dnscrypt(resif[i][0], portbuf,
368 				cfg->dnscrypt_port)) {
369 				fatal_exit("PROXYv2 and DNSCrypt combination not "
370 					"supported!");
371 			} else if(if_is_https(resif[i][0], portbuf,
372 				cfg->https_port)) {
373 				fatal_exit("PROXYv2 and DoH combination not "
374 					"supported!");
375 			}
376 		}
377 		/* search for duplicates in the returned addresses */
378 		for(j=0; j<num_resif[i]; j++) {
379 			if(!extstrtoaddr(resif[i][j], &a, &alen, cfg->port)) {
380 				if(strcmp(cfg->ifs[i], resif[i][j]) != 0)
381 					fatal_exit("cannot parse interface address '%s' from the interface specified as '%s'",
382 						resif[i][j], cfg->ifs[i]);
383 				else
384 					fatal_exit("cannot parse interface specified as '%s'",
385 						cfg->ifs[i]);
386 			}
387 			for(i2=0; i2<i; i2++) {
388 				for(j2=0; j2<num_resif[i2]; j2++) {
389 					if(strcmp(resif[i][j], resif[i2][j2])
390 						== 0) {
391 						char info1[1024], info2[1024];
392 						if(strcmp(cfg->ifs[i], resif[i][j]) != 0)
393 							snprintf(info1, sizeof(info1), "address %s from interface: %s", resif[i][j], cfg->ifs[i]);
394 						else	snprintf(info1, sizeof(info1), "interface: %s", cfg->ifs[i]);
395 						if(strcmp(cfg->ifs[i2], resif[i2][j2]) != 0)
396 							snprintf(info2, sizeof(info2), "address %s from interface: %s", resif[i2][j2], cfg->ifs[i2]);
397 						else	snprintf(info2, sizeof(info2), "interface: %s", cfg->ifs[i2]);
398 						fatal_exit("%s present twice, cannot bind the same ports twice. The first entry is %s and the second is %s", resif[i][j], info2, info1);
399 					}
400 				}
401 			}
402 		}
403 	}
404 
405 	for(i=0; i<cfg->num_ifs; i++) {
406 		config_del_strarray(resif[i], num_resif[i]);
407 	}
408 	free(resif);
409 	free(num_resif);
410 
411 	for(i=0; i<cfg->num_out_ifs; i++) {
412 		if(!ipstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT, &a, &alen) &&
413 		   !netblockstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT, &a, &alen, &d)) {
414 			fatal_exit("cannot parse outgoing-interface "
415 				"specified as '%s'", cfg->out_ifs[i]);
416 		}
417 		for(j=0; j<cfg->num_out_ifs; j++) {
418 			if(i!=j && strcmp(cfg->out_ifs[i], cfg->out_ifs[j])==0)
419 				fatal_exit("outgoing-interface: %s present "
420 					"twice, cannot bind same ports twice.",
421 					cfg->out_ifs[i]);
422 		}
423 	}
424 }
425 
426 /** check interface-automatic-ports */
427 static void
428 ifautomaticportschecks(char* ifautomaticports)
429 {
430 	char* now = ifautomaticports;
431 	while(now && *now) {
432 		char* after;
433 		int extraport;
434 		while(isspace((unsigned char)*now))
435 			now++;
436 		if(!*now)
437 			break;
438 		after = now;
439 		extraport = (int)strtol(now, &after, 10);
440 		if(extraport < 0 || extraport > 65535)
441 			fatal_exit("interface-automatic-ports: port out of range at position %d in '%s'", (int)(now-ifautomaticports)+1, ifautomaticports);
442 		if(extraport == 0 && now == after)
443 			fatal_exit("interface-automatic-ports: parse error at position %d in '%s'", (int)(now-ifautomaticports)+1, ifautomaticports);
444 		now = after;
445 	}
446 }
447 
448 /** check acl ips */
449 static void
450 aclchecks(struct config_file* cfg)
451 {
452 	int d;
453 	struct sockaddr_storage a;
454 	socklen_t alen;
455 	struct config_str2list* acl;
456 	for(acl=cfg->acls; acl; acl = acl->next) {
457 		if(!netblockstrtoaddr(acl->str, UNBOUND_DNS_PORT, &a, &alen,
458 			&d)) {
459 			fatal_exit("cannot parse access control address %s %s",
460 				acl->str, acl->str2);
461 		}
462 	}
463 }
464 
465 /** check tcp connection limit ips */
466 static void
467 tcpconnlimitchecks(struct config_file* cfg)
468 {
469 	int d;
470 	struct sockaddr_storage a;
471 	socklen_t alen;
472 	struct config_str2list* tcl;
473 	for(tcl=cfg->tcp_connection_limits; tcl; tcl = tcl->next) {
474 		if(!netblockstrtoaddr(tcl->str, UNBOUND_DNS_PORT, &a, &alen,
475 			&d)) {
476 			fatal_exit("cannot parse tcp connection limit address %s %s",
477 				tcl->str, tcl->str2);
478 		}
479 	}
480 }
481 
482 /** true if fname is a file */
483 static int
484 is_file(const char* fname)
485 {
486 	struct stat buf;
487 	if(stat(fname, &buf) < 0) {
488 		if(errno==EACCES) {
489 			printf("warning: no search permission for one of the directories in path: %s\n", fname);
490 			return 1;
491 		}
492 		perror(fname);
493 		return 0;
494 	}
495 	if(S_ISDIR(buf.st_mode)) {
496 		printf("%s is not a file\n", fname);
497 		return 0;
498 	}
499 	return 1;
500 }
501 
502 /** true if fname is a directory */
503 static int
504 is_dir(const char* fname)
505 {
506 	struct stat buf;
507 	if(stat(fname, &buf) < 0) {
508 		if(errno==EACCES) {
509 			printf("warning: no search permission for one of the directories in path: %s\n", fname);
510 			return 1;
511 		}
512 		perror(fname);
513 		return 0;
514 	}
515 	if(!(S_ISDIR(buf.st_mode))) {
516 		printf("%s is not a directory\n", fname);
517 		return 0;
518 	}
519 	return 1;
520 }
521 
522 /** get base dir of a fname */
523 static char*
524 basedir(char* fname)
525 {
526 	char* rev;
527 	if(!fname) fatal_exit("out of memory");
528 	rev = strrchr(fname, '/');
529 	if(!rev) return NULL;
530 	if(fname == rev) return NULL;
531 	rev[0] = 0;
532 	return fname;
533 }
534 
535 /** check chroot for a file string */
536 static void
537 check_chroot_string(const char* desc, char** ss,
538 	const char* chrootdir, struct config_file* cfg)
539 {
540 	char* str = *ss;
541 	if(str && str[0]) {
542 		*ss = fname_after_chroot(str, cfg, 1);
543 		if(!*ss) fatal_exit("out of memory");
544 		if(!is_file(*ss)) {
545 			if(chrootdir && chrootdir[0])
546 				fatal_exit("%s: \"%s\" does not exist in "
547 					"chrootdir %s", desc, str, chrootdir);
548 			else
549 				fatal_exit("%s: \"%s\" does not exist",
550 					desc, str);
551 		}
552 		/* put in a new full path for continued checking */
553 		free(str);
554 	}
555 }
556 
557 /** check file list, every file must be inside the chroot location */
558 static void
559 check_chroot_filelist(const char* desc, struct config_strlist* list,
560 	const char* chrootdir, struct config_file* cfg)
561 {
562 	struct config_strlist* p;
563 	for(p=list; p; p=p->next) {
564 		check_chroot_string(desc, &p->str, chrootdir, cfg);
565 	}
566 }
567 
568 /** check file list, with wildcard processing */
569 static void
570 check_chroot_filelist_wild(const char* desc, struct config_strlist* list,
571 	const char* chrootdir, struct config_file* cfg)
572 {
573 	struct config_strlist* p;
574 	for(p=list; p; p=p->next) {
575 #ifdef HAVE_GLOB
576 		if(strchr(p->str, '*') || strchr(p->str, '[') ||
577 			strchr(p->str, '?') || strchr(p->str, '{') ||
578 			strchr(p->str, '~')) {
579 			char* s = p->str;
580 			/* adjust whole pattern for chroot and check later */
581 			p->str = fname_after_chroot(p->str, cfg, 1);
582 			free(s);
583 		} else
584 #endif /* HAVE_GLOB */
585 			check_chroot_string(desc, &p->str, chrootdir, cfg);
586 	}
587 }
588 
589 #ifdef CLIENT_SUBNET
590 /** check ECS configuration */
591 static void
592 ecs_conf_checks(struct config_file* cfg)
593 {
594 	struct ecs_whitelist* whitelist = NULL;
595 	if(!(whitelist = ecs_whitelist_create()))
596 		fatal_exit("Could not create ednssubnet whitelist: out of memory");
597         if(!ecs_whitelist_apply_cfg(whitelist, cfg))
598 		fatal_exit("Could not setup ednssubnet whitelist");
599 	ecs_whitelist_delete(whitelist);
600 }
601 #endif /* CLIENT_SUBNET */
602 
603 /** check that the modules exist, are compiled in */
604 static void
605 check_modules_exist(const char* module_conf)
606 {
607 	const char** names = module_list_avail();
608 	const char* s = module_conf;
609 	while(*s) {
610 		int i = 0;
611 		int is_ok = 0;
612 		while(*s && isspace((unsigned char)*s))
613 			s++;
614 		if(!*s) break;
615 		while(names[i]) {
616 			if(strncmp(names[i], s, strlen(names[i])) == 0) {
617 				is_ok = 1;
618 				break;
619 			}
620 			i++;
621 		}
622 		if(is_ok == 0) {
623 			char n[64];
624 			size_t j;
625 			n[0]=0;
626 			n[sizeof(n)-1]=0;
627 			for(j=0; j<sizeof(n)-1; j++) {
628 				if(!s[j] || isspace((unsigned char)s[j])) {
629 					n[j] = 0;
630 					break;
631 				}
632 				n[j] = s[j];
633 			}
634 			fatal_exit("module_conf lists module '%s' but that "
635 				"module is not available.", n);
636 		}
637 		s += strlen(names[i]);
638 	}
639 }
640 
641 /** check configuration for errors */
642 static void
643 morechecks(struct config_file* cfg)
644 {
645 	warn_hosts("stub-host", cfg->stubs);
646 	warn_hosts("forward-host", cfg->forwards);
647 	interfacechecks(cfg);
648 	ifautomaticportschecks(cfg->if_automatic_ports);
649 	aclchecks(cfg);
650 	tcpconnlimitchecks(cfg);
651 
652 	if(cfg->verbosity < 0)
653 		fatal_exit("verbosity value < 0");
654 	if(cfg->num_threads <= 0 || cfg->num_threads > 10000)
655 		fatal_exit("num_threads value weird");
656 	if(!cfg->do_ip4 && !cfg->do_ip6)
657 		fatal_exit("ip4 and ip6 are both disabled, pointless");
658 	if(!cfg->do_ip4 && cfg->prefer_ip4)
659 		fatal_exit("cannot prefer and disable ip4, pointless");
660 	if(!cfg->do_ip6 && cfg->prefer_ip6)
661 		fatal_exit("cannot prefer and disable ip6, pointless");
662 	if(!cfg->do_udp && !cfg->do_tcp)
663 		fatal_exit("udp and tcp are both disabled, pointless");
664 	if(cfg->edns_buffer_size > cfg->msg_buffer_size)
665 		fatal_exit("edns-buffer-size larger than msg-buffer-size, "
666 			"answers will not fit in processing buffer");
667 #ifdef UB_ON_WINDOWS
668 	w_config_adjust_directory(cfg);
669 #endif
670 	if(cfg->chrootdir && cfg->chrootdir[0] &&
671 		cfg->chrootdir[strlen(cfg->chrootdir)-1] == '/')
672 		fatal_exit("chootdir %s has trailing slash '/' please remove.",
673 			cfg->chrootdir);
674 	if(cfg->chrootdir && cfg->chrootdir[0] &&
675 		!is_dir(cfg->chrootdir)) {
676 		fatal_exit("bad chroot directory");
677 	}
678 	if(cfg->directory && cfg->directory[0]) {
679 		char* ad = fname_after_chroot(cfg->directory, cfg, 0);
680 		if(!ad) fatal_exit("out of memory");
681 		if(!is_dir(ad)) fatal_exit("bad chdir directory");
682 		free(ad);
683 	}
684 	if( (cfg->chrootdir && cfg->chrootdir[0]) ||
685 	    (cfg->directory && cfg->directory[0])) {
686 		if(cfg->pidfile && cfg->pidfile[0]) {
687 			char* ad = (cfg->pidfile[0]=='/')?strdup(cfg->pidfile):
688 				fname_after_chroot(cfg->pidfile, cfg, 1);
689 			char* bd = basedir(ad);
690 			if(bd && !is_dir(bd))
691 				fatal_exit("pidfile directory does not exist");
692 			free(ad);
693 		}
694 		if(cfg->logfile && cfg->logfile[0]) {
695 			char* ad = fname_after_chroot(cfg->logfile, cfg, 1);
696 			char* bd = basedir(ad);
697 			if(bd && !is_dir(bd))
698 				fatal_exit("logfile directory does not exist");
699 			free(ad);
700 		}
701 	}
702 
703 	check_chroot_filelist("file with root-hints",
704 		cfg->root_hints, cfg->chrootdir, cfg);
705 	check_chroot_filelist("trust-anchor-file",
706 		cfg->trust_anchor_file_list, cfg->chrootdir, cfg);
707 	check_chroot_filelist("auto-trust-anchor-file",
708 		cfg->auto_trust_anchor_file_list, cfg->chrootdir, cfg);
709 	check_chroot_filelist_wild("trusted-keys-file",
710 		cfg->trusted_keys_file_list, cfg->chrootdir, cfg);
711 	if(cfg->disable_edns_do && strstr(cfg->module_conf, "validator")
712 		&& (cfg->trust_anchor_file_list
713 		|| cfg->trust_anchor_list
714 		|| cfg->auto_trust_anchor_file_list
715 		|| cfg->trusted_keys_file_list)) {
716 		char* key = NULL;
717 		if(cfg->auto_trust_anchor_file_list)
718 			key = cfg->auto_trust_anchor_file_list->str;
719 		if(!key && cfg->trust_anchor_file_list)
720 			key = cfg->trust_anchor_file_list->str;
721 		if(!key && cfg->trust_anchor_list)
722 			key = cfg->trust_anchor_list->str;
723 		if(!key && cfg->trusted_keys_file_list)
724 			key = cfg->trusted_keys_file_list->str;
725 		if(!key) key = "";
726 		fatal_exit("disable-edns-do does not allow DNSSEC to work, but the validator module uses a trust anchor %s, turn off disable-edns-do or disable validation", key);
727 	}
728 #ifdef USE_IPSECMOD
729 	if(cfg->ipsecmod_enabled && strstr(cfg->module_conf, "ipsecmod")) {
730 		/* only check hook if enabled */
731 		check_chroot_string("ipsecmod-hook", &cfg->ipsecmod_hook,
732 			cfg->chrootdir, cfg);
733 	}
734 #endif
735 	/* remove chroot setting so that modules are not stripping pathnames */
736 	free(cfg->chrootdir);
737 	cfg->chrootdir = NULL;
738 
739 	/* check that the modules listed in module_conf exist */
740 	check_modules_exist(cfg->module_conf);
741 
742 	/* Respip is known to *not* work with dns64. */
743 	if(strcmp(cfg->module_conf, "iterator") != 0
744 		&& strcmp(cfg->module_conf, "validator iterator") != 0
745 		&& strcmp(cfg->module_conf, "dns64 validator iterator") != 0
746 		&& strcmp(cfg->module_conf, "dns64 iterator") != 0
747 		&& strcmp(cfg->module_conf, "respip iterator") != 0
748 		&& strcmp(cfg->module_conf, "respip validator iterator") != 0
749 		&& strcmp(cfg->module_conf, "respip dns64 validator iterator") != 0
750 		&& strcmp(cfg->module_conf, "respip dns64 iterator") != 0
751 #ifdef WITH_PYTHONMODULE
752 		&& strcmp(cfg->module_conf, "python iterator") != 0
753 		&& strcmp(cfg->module_conf, "python respip iterator") != 0
754 		&& strcmp(cfg->module_conf, "python validator iterator") != 0
755 		&& strcmp(cfg->module_conf, "python respip validator iterator") != 0
756 		&& strcmp(cfg->module_conf, "validator python iterator") != 0
757 		&& strcmp(cfg->module_conf, "dns64 python iterator") != 0
758 		&& strcmp(cfg->module_conf, "dns64 python validator iterator") != 0
759 		&& strcmp(cfg->module_conf, "dns64 validator python iterator") != 0
760 		&& strcmp(cfg->module_conf, "python dns64 iterator") != 0
761 		&& strcmp(cfg->module_conf, "python dns64 validator iterator") != 0
762 #endif
763 #ifdef WITH_DYNLIBMODULE
764 		&& strcmp(cfg->module_conf, "dynlib iterator") != 0
765 		&& strcmp(cfg->module_conf, "dynlib dynlib iterator") != 0
766 		&& strcmp(cfg->module_conf, "dynlib dynlib dynlib iterator") != 0
767 		&& strcmp(cfg->module_conf, "python dynlib iterator") != 0
768 		&& strcmp(cfg->module_conf, "python dynlib dynlib iterator") != 0
769 		&& strcmp(cfg->module_conf, "python dynlib dynlib dynlib iterator") != 0
770 		&& strcmp(cfg->module_conf, "dynlib respip iterator") != 0
771 		&& strcmp(cfg->module_conf, "dynlib validator iterator") != 0
772 		&& strcmp(cfg->module_conf, "dynlib dynlib validator iterator") != 0
773 		&& strcmp(cfg->module_conf, "dynlib dynlib dynlib validator iterator") != 0
774 		&& strcmp(cfg->module_conf, "python dynlib validator iterator") != 0
775 		&& strcmp(cfg->module_conf, "python dynlib dynlib validator iterator") != 0
776 		&& strcmp(cfg->module_conf, "python dynlib dynlib dynlib validator iterator") != 0
777 		&& strcmp(cfg->module_conf, "dynlib respip validator iterator") != 0
778 		&& strcmp(cfg->module_conf, "validator dynlib iterator") != 0
779 		&& strcmp(cfg->module_conf, "dns64 dynlib iterator") != 0
780 		&& strcmp(cfg->module_conf, "dns64 dynlib validator iterator") != 0
781 		&& strcmp(cfg->module_conf, "dns64 validator dynlib iterator") != 0
782 		&& strcmp(cfg->module_conf, "dynlib dns64 iterator") != 0
783 		&& strcmp(cfg->module_conf, "dynlib dns64 validator iterator") != 0
784 		&& strcmp(cfg->module_conf, "dynlib dns64 cachedb iterator") != 0
785 		&& strcmp(cfg->module_conf, "dynlib dns64 validator cachedb iterator") != 0
786 		&& strcmp(cfg->module_conf, "dns64 dynlib cachedb iterator") != 0
787 		&& strcmp(cfg->module_conf, "dns64 dynlib validator cachedb iterator") != 0
788 		&& strcmp(cfg->module_conf, "dynlib cachedb iterator") != 0
789 		&& strcmp(cfg->module_conf, "dynlib respip cachedb iterator") != 0
790 		&& strcmp(cfg->module_conf, "dynlib validator cachedb iterator") != 0
791 		&& strcmp(cfg->module_conf, "dynlib respip validator cachedb iterator") != 0
792 		&& strcmp(cfg->module_conf, "cachedb dynlib iterator") != 0
793 		&& strcmp(cfg->module_conf, "respip cachedb dynlib iterator") != 0
794 		&& strcmp(cfg->module_conf, "validator cachedb dynlib iterator") != 0
795 		&& strcmp(cfg->module_conf, "respip validator cachedb dynlib iterator") != 0
796 		&& strcmp(cfg->module_conf, "validator dynlib cachedb iterator") != 0
797 		&& strcmp(cfg->module_conf, "respip validator dynlib cachedb iterator") != 0
798 		&& strcmp(cfg->module_conf, "dynlib subnetcache iterator") != 0
799 		&& strcmp(cfg->module_conf, "dynlib respip subnetcache iterator") != 0
800 		&& strcmp(cfg->module_conf, "subnetcache dynlib iterator") != 0
801 		&& strcmp(cfg->module_conf, "respip subnetcache dynlib iterator") != 0
802 		&& strcmp(cfg->module_conf, "dynlib subnetcache validator iterator") != 0
803 		&& strcmp(cfg->module_conf, "dynlib respip subnetcache validator iterator") != 0
804 		&& strcmp(cfg->module_conf, "subnetcache dynlib validator iterator") != 0
805 		&& strcmp(cfg->module_conf, "respip subnetcache dynlib validator iterator") != 0
806 		&& strcmp(cfg->module_conf, "subnetcache validator dynlib iterator") != 0
807 		&& strcmp(cfg->module_conf, "respip subnetcache validator dynlib iterator") != 0
808 		&& strcmp(cfg->module_conf, "dynlib ipsecmod iterator") != 0
809 		&& strcmp(cfg->module_conf, "dynlib ipsecmod respip iterator") != 0
810 		&& strcmp(cfg->module_conf, "ipsecmod dynlib iterator") != 0
811 		&& strcmp(cfg->module_conf, "ipsecmod dynlib respip iterator") != 0
812 		&& strcmp(cfg->module_conf, "ipsecmod validator iterator") != 0
813 		&& strcmp(cfg->module_conf, "ipsecmod respip validator iterator") != 0
814 		&& strcmp(cfg->module_conf, "dynlib ipsecmod validator iterator") != 0
815 		&& strcmp(cfg->module_conf, "dynlib ipsecmod respip validator iterator") != 0
816 		&& strcmp(cfg->module_conf, "ipsecmod dynlib validator iterator") != 0
817 		&& strcmp(cfg->module_conf, "ipsecmod dynlib respip validator iterator") != 0
818 		&& strcmp(cfg->module_conf, "ipsecmod validator dynlib iterator") != 0
819 		&& strcmp(cfg->module_conf, "ipsecmod respip validator dynlib iterator") != 0
820 #endif
821 #ifdef USE_CACHEDB
822 		&& strcmp(cfg->module_conf, "validator cachedb iterator") != 0
823 		&& strcmp(cfg->module_conf, "respip validator cachedb iterator") != 0
824 		&& strcmp(cfg->module_conf, "cachedb iterator") != 0
825 		&& strcmp(cfg->module_conf, "respip cachedb iterator") != 0
826 		&& strcmp(cfg->module_conf, "dns64 validator cachedb iterator") != 0
827 		&& strcmp(cfg->module_conf, "dns64 cachedb iterator") != 0
828 #endif
829 #if defined(WITH_PYTHONMODULE) && defined(USE_CACHEDB)
830 		&& strcmp(cfg->module_conf, "python dns64 cachedb iterator") != 0
831 		&& strcmp(cfg->module_conf, "python dns64 validator cachedb iterator") != 0
832 		&& strcmp(cfg->module_conf, "dns64 python cachedb iterator") != 0
833 		&& strcmp(cfg->module_conf, "dns64 python validator cachedb iterator") != 0
834 		&& strcmp(cfg->module_conf, "python cachedb iterator") != 0
835 		&& strcmp(cfg->module_conf, "python respip cachedb iterator") != 0
836 		&& strcmp(cfg->module_conf, "python validator cachedb iterator") != 0
837 		&& strcmp(cfg->module_conf, "python respip validator cachedb iterator") != 0
838 		&& strcmp(cfg->module_conf, "cachedb python iterator") != 0
839 		&& strcmp(cfg->module_conf, "respip cachedb python iterator") != 0
840 		&& strcmp(cfg->module_conf, "validator cachedb python iterator") != 0
841 		&& strcmp(cfg->module_conf, "respip validator cachedb python iterator") != 0
842 		&& strcmp(cfg->module_conf, "validator python cachedb iterator") != 0
843 		&& strcmp(cfg->module_conf, "respip validator python cachedb iterator") != 0
844 #endif
845 #if defined(CLIENT_SUBNET) && defined(USE_CACHEDB)
846 		&& strcmp(cfg->module_conf, "respip subnetcache validator cachedb iterator") != 0
847 		&& strcmp(cfg->module_conf, "subnetcache validator cachedb iterator") != 0
848 #endif
849 #ifdef CLIENT_SUBNET
850 		&& strcmp(cfg->module_conf, "subnetcache iterator") != 0
851 		&& strcmp(cfg->module_conf, "respip subnetcache iterator") != 0
852 		&& strcmp(cfg->module_conf, "subnetcache validator iterator") != 0
853 		&& strcmp(cfg->module_conf, "respip subnetcache validator iterator") != 0
854 		&& strcmp(cfg->module_conf, "dns64 subnetcache iterator") != 0
855 		&& strcmp(cfg->module_conf, "dns64 subnetcache validator iterator") != 0
856 		&& strcmp(cfg->module_conf, "dns64 subnetcache respip iterator") != 0
857 		&& strcmp(cfg->module_conf, "dns64 subnetcache respip validator iterator") != 0
858 #endif
859 #if defined(WITH_PYTHONMODULE) && defined(CLIENT_SUBNET)
860 		&& strcmp(cfg->module_conf, "python subnetcache iterator") != 0
861 		&& strcmp(cfg->module_conf, "python respip subnetcache iterator") != 0
862 		&& strcmp(cfg->module_conf, "subnetcache python iterator") != 0
863 		&& strcmp(cfg->module_conf, "respip subnetcache python iterator") != 0
864 		&& strcmp(cfg->module_conf, "python subnetcache validator iterator") != 0
865 		&& strcmp(cfg->module_conf, "python respip subnetcache validator iterator") != 0
866 		&& strcmp(cfg->module_conf, "subnetcache python validator iterator") != 0
867 		&& strcmp(cfg->module_conf, "respip subnetcache python validator iterator") != 0
868 		&& strcmp(cfg->module_conf, "subnetcache validator python iterator") != 0
869 		&& strcmp(cfg->module_conf, "respip subnetcache validator python iterator") != 0
870 #endif
871 #ifdef USE_IPSECMOD
872 		&& strcmp(cfg->module_conf, "ipsecmod iterator") != 0
873 		&& strcmp(cfg->module_conf, "ipsecmod respip iterator") != 0
874 		&& strcmp(cfg->module_conf, "ipsecmod validator iterator") != 0
875 		&& strcmp(cfg->module_conf, "ipsecmod respip validator iterator") != 0
876 #endif
877 #if defined(WITH_PYTHONMODULE) && defined(USE_IPSECMOD)
878 		&& strcmp(cfg->module_conf, "python ipsecmod iterator") != 0
879 		&& strcmp(cfg->module_conf, "python ipsecmod respip iterator") != 0
880 		&& strcmp(cfg->module_conf, "ipsecmod python iterator") != 0
881 		&& strcmp(cfg->module_conf, "ipsecmod python respip iterator") != 0
882 		&& strcmp(cfg->module_conf, "ipsecmod validator iterator") != 0
883 		&& strcmp(cfg->module_conf, "ipsecmod respip validator iterator") != 0
884 		&& strcmp(cfg->module_conf, "python ipsecmod validator iterator") != 0
885 		&& strcmp(cfg->module_conf, "python ipsecmod respip validator iterator") != 0
886 		&& strcmp(cfg->module_conf, "ipsecmod python validator iterator") != 0
887 		&& strcmp(cfg->module_conf, "ipsecmod python respip validator iterator") != 0
888 		&& strcmp(cfg->module_conf, "ipsecmod validator python iterator") != 0
889 		&& strcmp(cfg->module_conf, "ipsecmod respip validator python iterator") != 0
890 #endif
891 #ifdef USE_IPSET
892 		&& strcmp(cfg->module_conf, "validator ipset iterator") != 0
893 		&& strcmp(cfg->module_conf, "validator ipset respip iterator") != 0
894 		&& strcmp(cfg->module_conf, "ipset iterator") != 0
895 		&& strcmp(cfg->module_conf, "ipset respip iterator") != 0
896 #endif
897 		) {
898 		fatal_exit("module conf '%s' is not known to work",
899 			cfg->module_conf);
900 	}
901 
902 #ifdef HAVE_GETPWNAM
903 	if(cfg->username && cfg->username[0]) {
904 		if(getpwnam(cfg->username) == NULL)
905 			fatal_exit("user '%s' does not exist.", cfg->username);
906 #  ifdef HAVE_ENDPWENT
907 		endpwent();
908 #  endif
909 	}
910 #endif
911 	if(cfg->remote_control_enable && options_remote_is_address(cfg)
912 		&& cfg->control_use_cert) {
913 		check_chroot_string("server-key-file", &cfg->server_key_file,
914 			cfg->chrootdir, cfg);
915 		check_chroot_string("server-cert-file", &cfg->server_cert_file,
916 			cfg->chrootdir, cfg);
917 		if(!is_file(cfg->control_key_file))
918 			fatal_exit("control-key-file: \"%s\" does not exist",
919 				cfg->control_key_file);
920 		if(!is_file(cfg->control_cert_file))
921 			fatal_exit("control-cert-file: \"%s\" does not exist",
922 				cfg->control_cert_file);
923 	}
924 
925 	donotquerylocalhostcheck(cfg);
926 	localzonechecks(cfg);
927 	view_and_respipchecks(cfg);
928 #ifdef CLIENT_SUBNET
929 	ecs_conf_checks(cfg);
930 #endif
931 }
932 
933 /** check forwards */
934 static void
935 check_fwd(struct config_file* cfg)
936 {
937 	struct iter_forwards* fwd = forwards_create();
938 	if(!fwd || !forwards_apply_cfg(fwd, cfg)) {
939 		fatal_exit("Could not set forward zones");
940 	}
941 	forwards_delete(fwd);
942 }
943 
944 /** check hints */
945 static void
946 check_hints(struct config_file* cfg)
947 {
948 	struct iter_hints* hints = hints_create();
949 	if(!hints || !hints_apply_cfg(hints, cfg)) {
950 		fatal_exit("Could not set root or stub hints");
951 	}
952 	hints_delete(hints);
953 }
954 
955 /** check auth zones */
956 static void
957 check_auth(struct config_file* cfg)
958 {
959 	int is_rpz = 0;
960 	struct auth_zones* az = auth_zones_create();
961 	if(!az || !auth_zones_apply_cfg(az, cfg, 0, &is_rpz, NULL, NULL)) {
962 		fatal_exit("Could not setup authority zones");
963 	}
964 	auth_zones_delete(az);
965 }
966 
967 /** check config file */
968 static void
969 checkconf(const char* cfgfile, const char* opt, int final, int quiet)
970 {
971 	char oldwd[4096];
972 	struct config_file* cfg = config_create();
973 	if(!cfg)
974 		fatal_exit("out of memory");
975 	oldwd[0] = 0;
976 	if(!getcwd(oldwd, sizeof(oldwd))) {
977 		log_err("cannot getcwd: %s", strerror(errno));
978 		oldwd[0] = 0;
979 	}
980 	if(!config_read(cfg, cfgfile, NULL)) {
981 		/* config_read prints messages to stderr */
982 		config_delete(cfg);
983 		exit(1);
984 	}
985 	if(oldwd[0] && chdir(oldwd) == -1)
986 		log_err("cannot chdir(%s): %s", oldwd, strerror(errno));
987 	if(opt) {
988 		print_option(cfg, opt, final);
989 		config_delete(cfg);
990 		return;
991 	}
992 	morechecks(cfg);
993 	check_mod(cfg, iter_get_funcblock());
994 	check_mod(cfg, val_get_funcblock());
995 #ifdef WITH_PYTHONMODULE
996 	if(strstr(cfg->module_conf, "python"))
997 		check_mod(cfg, pythonmod_get_funcblock());
998 #endif
999 	check_fwd(cfg);
1000 	check_hints(cfg);
1001 	check_auth(cfg);
1002 	if(!quiet) { printf("unbound-checkconf: no errors in %s\n", cfgfile); }
1003 	config_delete(cfg);
1004 }
1005 
1006 /** getopt global, in case header files fail to declare it. */
1007 extern int optind;
1008 /** getopt global, in case header files fail to declare it. */
1009 extern char* optarg;
1010 
1011 /** Main routine for checkconf */
1012 int main(int argc, char* argv[])
1013 {
1014 	int c;
1015 	int final = 0;
1016 	int quiet = 0;
1017 	const char* f;
1018 	const char* opt = NULL;
1019 	const char* cfgfile = CONFIGFILE;
1020 	checklock_start();
1021 	log_ident_set("unbound-checkconf");
1022 	log_init(NULL, 0, NULL);
1023 #ifdef USE_WINSOCK
1024 	/* use registry config file in preference to compiletime location */
1025 	if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile")))
1026 		cfgfile = CONFIGFILE;
1027 #endif /* USE_WINSOCK */
1028 	/* parse the options */
1029 	while( (c=getopt(argc, argv, "fhqo:")) != -1) {
1030 		switch(c) {
1031 		case 'f':
1032 			final = 1;
1033 			break;
1034 		case 'o':
1035 			opt = optarg;
1036 			break;
1037 		case 'q':
1038 			quiet = 1;
1039 			break;
1040 		case '?':
1041 		case 'h':
1042 		default:
1043 			usage();
1044 		}
1045 	}
1046 	argc -= optind;
1047 	argv += optind;
1048 	if(argc != 0 && argc != 1)
1049 		usage();
1050 	if(argc == 1)
1051 		f = argv[0];
1052 	else	f = cfgfile;
1053 	checkconf(f, opt, final, quiet);
1054 	checklock_stop();
1055 	return 0;
1056 }
1057