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