xref: /openbsd/usr.sbin/nsd/nsd-checkconf.c (revision bc6311d7)
1 /*
2  * checkconf - Read and repeat configuration file to output.
3  *
4  * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
5  *
6  * See LICENSE for the license.
7  *
8  */
9 #include "config.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <limits.h>
15 #include "tsig.h"
16 #include "options.h"
17 #include "util.h"
18 #include "dname.h"
19 #include "rrl.h"
20 
21 extern char *optarg;
22 extern int optind;
23 static void usage(void) ATTR_NORETURN;
24 
25 #define ZONE_GET_ACL(NAME, VAR, PATTERN) 		\
26 	if (strcasecmp(#NAME, (VAR)) == 0) { 	\
27 		quote_acl(PATTERN->NAME); 	\
28 		return; 			\
29 	}
30 
31 #define ZONE_GET_OUTGOING(NAME, VAR, PATTERN)			\
32 	if (strcasecmp(#NAME, (VAR)) == 0) {		\
33 		acl_options_type* acl; 			\
34 		for(acl=PATTERN->NAME; acl; acl=acl->next)	\
35 			quote(acl->ip_address_spec);	\
36 		return; 				\
37 	}
38 
39 #define ZONE_GET_STR(NAME, VAR, PATTERN) 		\
40 	if (strcasecmp(#NAME, (VAR)) == 0) { 	\
41 		quote(PATTERN->NAME); 		\
42 		return; 			\
43 	}
44 
45 #define ZONE_GET_PATH(FINAL, NAME, VAR, PATTERN) 	\
46 	if (strcasecmp(#NAME, (VAR)) == 0) { 		\
47 		quotepath(opt, FINAL, PATTERN->NAME); 	\
48 		return; 				\
49 	}
50 
51 #define ZONE_GET_BIN(NAME, VAR, PATTERN) 			\
52 	if (strcasecmp(#NAME, (VAR)) == 0) { 		\
53 		printf("%s\n", (PATTERN->NAME)?"yes":"no"); 	\
54 		return;					\
55 	}
56 
57 #define ZONE_GET_RRL(NAME, VAR, PATTERN) 			\
58 	if (strcasecmp(#NAME, (VAR)) == 0) { 		\
59 		zone_print_rrl_whitelist("", PATTERN->NAME);	\
60 		return;					\
61 	}
62 
63 #define ZONE_GET_INT(NAME, VAR, PATTERN) 		\
64 	if (strcasecmp(#NAME, (VAR)) == 0) { 	\
65 		printf("%d\n", (int) PATTERN->NAME); 	\
66 		return; 			\
67 	}
68 
69 #define SERV_GET_BIN(NAME, VAR) 			\
70 	if (strcasecmp(#NAME, (VAR)) == 0) { 		\
71 		printf("%s\n", opt->NAME?"yes":"no"); 	\
72 		return;					\
73 	}
74 
75 #define SERV_GET_STR(NAME, VAR) 		\
76 	if (strcasecmp(#NAME, (VAR)) == 0) { 	\
77 		quote(opt->NAME); 		\
78 		return; 			\
79 	}
80 
81 #define SERV_GET_PATH(FINAL, NAME, VAR) 	\
82 	if (strcasecmp(#NAME, (VAR)) == 0) { 	\
83 		quotepath(opt, FINAL, opt->NAME); 	\
84 		return; 			\
85 	}
86 
87 #define SERV_GET_INT(NAME, VAR) 		\
88 	if (strcasecmp(#NAME, (VAR)) == 0) { 	\
89 		printf("%d\n", (int) opt->NAME); 	\
90 		return; 			\
91 	}
92 
93 #define SERV_GET_IP(NAME, MEMBER, VAR) 				\
94 	if (strcasecmp(#NAME, (VAR)) == 0) { 		\
95 		for(ip = opt->MEMBER; ip; ip=ip->next)	\
96 		{						\
97 			quote(ip->address);			\
98 		}						\
99 		return;						\
100 	}
101 
102 #ifdef RATELIMIT
103 static void zone_print_rrl_whitelist(const char* s, uint16_t w)
104 {
105 	int i;
106 	if(w==rrl_type_all) {
107 		printf("%sall\n", s);
108 		return;
109 	}
110 	for(i=0x01; i <= 0x80; i<<=1) {
111 		if( (w&i) )
112 			printf("%s%s\n", s, rrltype2str(i));
113 	}
114 }
115 #endif /* RATELIMIT */
116 
117 static char buf[BUFSIZ];
118 
119 static char *
120 underscore(const char *s) {
121 	const char *j = s;
122 	size_t i = 0;
123 
124 	while(j && *j) {
125 		if (*j == '-') {
126 			buf[i++] = '_';
127 		} else {
128 			buf[i++] = *j;
129 		}
130 		j++;
131 		if (i >= BUFSIZ) {
132 			return NULL;
133 		}
134 	}
135 	buf[i] = '\0';
136 	return buf;
137 }
138 
139 static void
140 usage(void)
141 {
142 	fprintf(stderr, "usage: nsd-checkconf [-v|-h] [-o option] [-z zonename]\n");
143 	fprintf(stderr, "                     [-s keyname] [-t tlsauthname] <configfilename>\n");
144 	fprintf(stderr, "       Checks NSD configuration file for errors.\n");
145 	fprintf(stderr, "       Version %s. Report bugs to <%s>.\n\n",
146 		PACKAGE_VERSION, PACKAGE_BUGREPORT);
147 	fprintf(stderr, "Use with a configfile as argument to check syntax.\n");
148 	fprintf(stderr, "Use with -o, -z, -t or -s options to query the configuration.\n\n");
149 	fprintf(stderr, "-v			Verbose, echo settings that take effect to std output.\n");
150 	fprintf(stderr, "-h			Print this help information.\n");
151 	fprintf(stderr, "-f			Use with -o to print final pathnames, ie. with chroot.\n");
152 	fprintf(stderr, "-o option		Print value of the option specified to stdout.\n");
153 	fprintf(stderr, "-p pattern		Print option value for the pattern given.\n");
154 	fprintf(stderr, "-z zonename		Print option value for the zone given.\n");
155 	fprintf(stderr, "-a keyname		Print algorithm name for the TSIG key.\n");
156 	fprintf(stderr, "-s keyname		Print base64 secret blob for the TSIG key.\n");
157 	fprintf(stderr, "-t tls-auth-name	Print auth domain name for the tls-auth clause.\n");
158 	exit(1);
159 }
160 
161 static void
162 print_string_var(const char* varname, const char* value)
163 {
164 	if (!value) {
165 		printf("\t#%s\n", varname);
166 	} else {
167 		printf("\t%s \"%s\"\n", varname, value);
168 	}
169 }
170 
171 static void
172 quote(const char *v)
173 {
174 	if(v==NULL)
175 		printf("\n");
176 	else
177 		printf("%s\n", v);
178 }
179 
180 static void
181 quotepath(nsd_options_type* opt, int final, const char *f)
182 {
183 	const char* chr = opt->chroot;
184 #ifdef CHROOTDIR
185 	if(chr == 0) chr = CHROOTDIR;
186 #endif
187 	if(f == 0 || f[0] == '/' || !final || !chr || chr[0]==0) {
188 		quote(f);
189 		return;
190 	}
191 	/* chroot has had trailing slash applied in check part of checkconf */
192 	printf("%s%s\n", chr, f);
193 }
194 
195 static void
196 quote_acl(acl_options_type* acl)
197 {
198 	while(acl)
199 	{
200 		if (acl->tls_auth_name)
201 			printf("%s %s %s\n", acl->ip_address_spec,
202 				acl->nokey?"NOKEY":(acl->blocked?"BLOCKED":
203 				(acl->key_name?acl->key_name:"(null)")),
204 				acl->tls_auth_name?acl->tls_auth_name:"");
205 		else
206 			printf("%s %s\n", acl->ip_address_spec,
207 				acl->nokey?"NOKEY":(acl->blocked?"BLOCKED":
208 				(acl->key_name?acl->key_name:"(null)")));
209 		acl=acl->next;
210 	}
211 }
212 
213 static void
214 print_acl(const char* varname, acl_options_type* acl)
215 {
216 	while(acl)
217 	{
218 		printf("\t%s ", varname);
219 		if(acl->use_axfr_only)
220 			printf("AXFR ");
221 		if(acl->allow_udp)
222 			printf("UDP ");
223 		if (acl->tls_auth_name)
224 			printf("%s %s %s\n", acl->ip_address_spec,
225 				acl->nokey?"NOKEY":(acl->blocked?"BLOCKED":
226 				(acl->key_name?acl->key_name:"(null)")),
227 				acl->tls_auth_name?acl->tls_auth_name:"");
228 		else
229 			printf("%s %s\n", acl->ip_address_spec,
230 				acl->nokey?"NOKEY":(acl->blocked?"BLOCKED":
231 				(acl->key_name?acl->key_name:"(null)")));
232 		if(verbosity>1) {
233 			printf("\t# %s", acl->is_ipv6?"ip6":"ip4");
234 			if(acl->port == 0) printf(" noport");
235 			else printf(" port=%d", acl->port);
236 			if(acl->rangetype == acl_range_single) printf(" single");
237 			if(acl->rangetype == acl_range_mask)   printf(" masked");
238 			if(acl->rangetype == acl_range_subnet) printf(" subnet");
239 			if(acl->rangetype == acl_range_minmax) printf(" minmax");
240 			if(acl->is_ipv6) {
241 #ifdef INET6
242 				char dest[128];
243 				inet_ntop(AF_INET6, &acl->addr.addr6, dest, sizeof(dest));
244 				printf(" addr=%s", dest);
245 				if(acl->rangetype != acl_range_single) {
246 					inet_ntop(AF_INET6, &acl->range_mask.addr6, dest, sizeof(dest));
247 					printf(" rangemask=%s", dest);
248 				}
249 #else
250 				printf(" ip6addr-noip6defined");
251 #endif
252 			} else {
253 				char dest[128];
254 				inet_ntop(AF_INET, &acl->addr.addr, dest, sizeof(dest));
255 				printf(" addr=%s", dest);
256 				if(acl->rangetype != acl_range_single) {
257 					inet_ntop(AF_INET, &acl->range_mask.addr, dest, sizeof(dest));
258 					printf(" rangemask=%s", dest);
259 				}
260 			}
261 			printf("\n");
262 		}
263 		acl=acl->next;
264 	}
265 }
266 
267 static void
268 print_acl_ips(const char* varname, acl_options_type* acl)
269 {
270 	while(acl)
271 	{
272 		printf("\t%s %s\n", varname, acl->ip_address_spec);
273 		acl=acl->next;
274 	}
275 }
276 
277 void
278 config_print_zone(nsd_options_type* opt, const char* k, int s, const char *o,
279 	const char *z, const char* pat, const char* tls, int final)
280 {
281 	ip_address_option_type* ip;
282 
283 	if (k) {
284 		/* find key */
285 		key_options_type* key = key_options_find(opt, k);
286 		if(key) {
287 			if (s) {
288 				quote(key->secret);
289 			} else {
290 				quote(key->algorithm);
291 			}
292 			return;
293 		}
294 		printf("Could not find key %s\n", k);
295 		return;
296 	}
297 
298 	if (tls) {
299 		/* find tlsauth */
300 		tls_auth_options_type* tlsauth = tls_auth_options_find(opt, tls);
301 		if(tlsauth) {
302 			quote(tlsauth->auth_domain_name);
303 			return;
304 		}
305 		printf("Could not find tls-auth %s\n", tls);
306 		return;
307 	}
308 
309 	if (!o) {
310 		return;
311 	}
312 
313 	if (z) {
314 		zone_options_type* zone;
315 		const dname_type *dname = dname_parse(opt->region, z);
316 		if(!dname) {
317 			printf("Could not parse zone name %s\n", z);
318 			exit(1);
319 		}
320 		zone = zone_options_find(opt, dname);
321 		if(!zone) {
322 			printf("Zone does not exist: %s\n", z);
323 			exit(1);
324 		}
325 		ZONE_GET_STR(name, o, zone);
326 		if(strcasecmp("pattern", o)==0) {
327 			quote(zone->pattern->pname);
328 			return;
329 		}
330 		ZONE_GET_BIN(part_of_config, o, zone);
331 		ZONE_GET_PATH(final, zonefile, o, zone->pattern);
332 		ZONE_GET_ACL(allow_query, o, zone->pattern);
333 		ZONE_GET_ACL(request_xfr, o, zone->pattern);
334 		ZONE_GET_ACL(provide_xfr, o, zone->pattern);
335 		ZONE_GET_ACL(allow_notify, o, zone->pattern);
336 		ZONE_GET_ACL(notify, o, zone->pattern);
337 		ZONE_GET_BIN(notify_retry, o, zone->pattern);
338 		ZONE_GET_STR(zonestats, o, zone->pattern);
339 		ZONE_GET_OUTGOING(outgoing_interface, o, zone->pattern);
340 		ZONE_GET_BIN(allow_axfr_fallback, o, zone->pattern);
341 		ZONE_GET_INT(max_refresh_time, o, zone->pattern);
342 		ZONE_GET_INT(min_refresh_time, o, zone->pattern);
343 		ZONE_GET_INT(max_retry_time, o, zone->pattern);
344 		ZONE_GET_INT(min_retry_time, o, zone->pattern);
345 		ZONE_GET_INT(min_expire_time, o, zone->pattern);
346 		ZONE_GET_INT(size_limit_xfr, o, zone->pattern);
347 #ifdef RATELIMIT
348 		ZONE_GET_RRL(rrl_whitelist, o, zone->pattern);
349 #endif
350 		ZONE_GET_BIN(multi_master_check, o, zone->pattern);
351 		printf("Zone option not handled: %s %s\n", z, o);
352 		exit(1);
353 	} else if(pat) {
354 		pattern_options_type* p = pattern_options_find(opt, pat);
355 		if(!p) {
356 			printf("Pattern does not exist: %s\n", pat);
357 			exit(1);
358 		}
359 		if(strcasecmp("name", o)==0) {
360 			quote(p->pname);
361 			return;
362 		}
363 		ZONE_GET_STR(zonefile, o, p);
364 		ZONE_GET_PATH(final, zonefile, o, p);
365 		ZONE_GET_ACL(allow_query, o, p);
366 		ZONE_GET_ACL(request_xfr, o, p);
367 		ZONE_GET_ACL(provide_xfr, o, p);
368 		ZONE_GET_ACL(allow_notify, o, p);
369 		ZONE_GET_ACL(notify, o, p);
370 		ZONE_GET_BIN(notify_retry, o, p);
371 		ZONE_GET_STR(zonestats, o, p);
372 		ZONE_GET_OUTGOING(outgoing_interface, o, p);
373 		ZONE_GET_BIN(allow_axfr_fallback, o, p);
374 		ZONE_GET_INT(max_refresh_time, o, p);
375 		ZONE_GET_INT(min_refresh_time, o, p);
376 		ZONE_GET_INT(max_retry_time, o, p);
377 		ZONE_GET_INT(min_retry_time, o, p);
378 		ZONE_GET_INT(min_expire_time, o, p);
379 		ZONE_GET_INT(size_limit_xfr, o, p);
380 #ifdef RATELIMIT
381 		ZONE_GET_RRL(rrl_whitelist, o, p);
382 #endif
383 		ZONE_GET_BIN(multi_master_check, o, p);
384 		printf("Pattern option not handled: %s %s\n", pat, o);
385 		exit(1);
386 	} else {
387 		/* look in the server section */
388 		SERV_GET_IP(ip_address, ip_addresses, o);
389 		/* bin */
390 		SERV_GET_BIN(ip_transparent, o);
391 		SERV_GET_BIN(ip_freebind, o);
392 		SERV_GET_BIN(debug_mode, o);
393 		SERV_GET_BIN(do_ip4, o);
394 		SERV_GET_BIN(do_ip6, o);
395 		SERV_GET_BIN(reuseport, o);
396 		SERV_GET_BIN(hide_version, o);
397 		SERV_GET_BIN(hide_identity, o);
398 		SERV_GET_BIN(drop_updates, o);
399 		SERV_GET_BIN(zonefiles_check, o);
400 		SERV_GET_BIN(log_time_ascii, o);
401 		SERV_GET_BIN(round_robin, o);
402 		SERV_GET_BIN(minimal_responses, o);
403 		SERV_GET_BIN(confine_to_zone, o);
404 		SERV_GET_BIN(refuse_any, o);
405 		SERV_GET_BIN(tcp_reject_overflow, o);
406 		SERV_GET_BIN(log_only_syslog, o);
407 		/* str */
408 		SERV_GET_PATH(final, database, o);
409 		SERV_GET_STR(identity, o);
410 		SERV_GET_STR(version, o);
411 		SERV_GET_STR(nsid, o);
412 		SERV_GET_PATH(final, logfile, o);
413 		SERV_GET_PATH(final, pidfile, o);
414 		SERV_GET_STR(chroot, o);
415 		SERV_GET_STR(username, o);
416 		SERV_GET_PATH(final, zonesdir, o);
417 		SERV_GET_PATH(final, xfrdfile, o);
418 		SERV_GET_PATH(final, xfrdir, o);
419 		SERV_GET_PATH(final, zonelistfile, o);
420 		SERV_GET_STR(port, o);
421 		SERV_GET_STR(tls_service_key, o);
422 		SERV_GET_STR(tls_service_ocsp, o);
423 		SERV_GET_STR(tls_service_pem, o);
424 		SERV_GET_STR(tls_port, o);
425 		SERV_GET_STR(tls_cert_bundle, o);
426 		SERV_GET_STR(cookie_secret, o);
427 		SERV_GET_STR(cookie_secret_file, o);
428 		SERV_GET_BIN(answer_cookie, o);
429 		/* int */
430 		SERV_GET_INT(server_count, o);
431 		SERV_GET_INT(tcp_count, o);
432 		SERV_GET_INT(tcp_query_count, o);
433 		SERV_GET_INT(tcp_timeout, o);
434 		SERV_GET_INT(tcp_mss, o);
435 		SERV_GET_INT(outgoing_tcp_mss, o);
436 		SERV_GET_INT(xfrd_tcp_max, o);
437 		SERV_GET_INT(xfrd_tcp_pipeline, o);
438 		SERV_GET_INT(ipv4_edns_size, o);
439 		SERV_GET_INT(ipv6_edns_size, o);
440 		SERV_GET_INT(statistics, o);
441 		SERV_GET_INT(xfrd_reload_timeout, o);
442 		SERV_GET_INT(verbosity, o);
443 		SERV_GET_INT(send_buffer_size, o);
444 		SERV_GET_INT(receive_buffer_size, o);
445 #ifdef RATELIMIT
446 		SERV_GET_INT(rrl_size, o);
447 		SERV_GET_INT(rrl_ratelimit, o);
448 		SERV_GET_INT(rrl_slip, o);
449 		SERV_GET_INT(rrl_ipv4_prefix_length, o);
450 		SERV_GET_INT(rrl_ipv6_prefix_length, o);
451 		SERV_GET_INT(rrl_whitelist_ratelimit, o);
452 #endif
453 #ifdef USE_DNSTAP
454 		SERV_GET_BIN(dnstap_enable, o);
455 		SERV_GET_STR(dnstap_socket_path, o);
456 		SERV_GET_BIN(dnstap_send_identity, o);
457 		SERV_GET_BIN(dnstap_send_version, o);
458 		SERV_GET_STR(dnstap_identity, o);
459 		SERV_GET_STR(dnstap_version, o);
460 		SERV_GET_BIN(dnstap_log_auth_query_messages, o);
461 		SERV_GET_BIN(dnstap_log_auth_response_messages, o);
462 #endif
463 		SERV_GET_INT(zonefiles_write, o);
464 		/* remote control */
465 		SERV_GET_BIN(control_enable, o);
466 		SERV_GET_IP(control_interface, control_interface, o);
467 		SERV_GET_INT(control_port, o);
468 		SERV_GET_STR(server_key_file, o);
469 		SERV_GET_STR(server_cert_file, o);
470 		SERV_GET_STR(control_key_file, o);
471 		SERV_GET_STR(control_cert_file, o);
472 
473 		if(strcasecmp(o, "zones") == 0) {
474 			zone_options_type* zone;
475 			RBTREE_FOR(zone, zone_options_type*, opt->zone_options)
476 				quote(zone->name);
477 			return;
478 		}
479 		if(strcasecmp(o, "patterns") == 0) {
480 			pattern_options_type* p;
481 			RBTREE_FOR(p, pattern_options_type*, opt->patterns)
482 				quote(p->pname);
483 			return;
484 		}
485 		printf("Server option not handled: %s\n", o);
486 		exit(1);
487 	}
488 }
489 
490 /* print zone content items */
491 static void print_zone_content_elems(pattern_options_type* pat)
492 {
493 	if(pat->zonefile)
494 		print_string_var("zonefile:", pat->zonefile);
495 #ifdef RATELIMIT
496 	zone_print_rrl_whitelist("\trrl-whitelist: ", pat->rrl_whitelist);
497 #endif
498 	print_acl("allow_query:", pat->allow_query);
499 	print_acl("allow-notify:", pat->allow_notify);
500 	print_acl("request-xfr:", pat->request_xfr);
501 	if(pat->multi_master_check)
502 		printf("\tmulti-master-check: %s\n", pat->multi_master_check?"yes":"no");
503 	if(!pat->notify_retry_is_default)
504 		printf("\tnotify-retry: %d\n", pat->notify_retry);
505 	print_acl("notify:", pat->notify);
506 	print_acl("provide-xfr:", pat->provide_xfr);
507 	if(pat->zonestats)
508 		print_string_var("zonestats:", pat->zonestats);
509 	print_acl_ips("outgoing-interface:", pat->outgoing_interface);
510 	if(!pat->allow_axfr_fallback_is_default)
511 		printf("\tallow-axfr-fallback: %s\n",
512 			pat->allow_axfr_fallback?"yes":"no");
513 	if(!pat->max_refresh_time_is_default)
514 		printf("\tmax-refresh-time: %d\n", pat->max_refresh_time);
515 	if(!pat->min_refresh_time_is_default)
516 		printf("\tmin-refresh-time: %d\n", pat->min_refresh_time);
517 	if(!pat->max_retry_time_is_default)
518 		printf("\tmax-retry-time: %d\n", pat->max_retry_time);
519 	if(!pat->min_retry_time_is_default)
520 		printf("\tmin-retry-time: %d\n", pat->min_retry_time);
521 	if(pat->min_expire_time_expr == REFRESHPLUSRETRYPLUS1)
522 		printf("\tmin-expire-time: " REFRESHPLUSRETRYPLUS1_STR "\n");
523 	else if(pat->min_expire_time_expr == EXPIRE_TIME_HAS_VALUE)
524 		printf("\tmin-expire-time: %d\n", pat->min_expire_time);
525 	if(pat->size_limit_xfr != 0)
526 		printf("\tsize-limit-xfr: %llu\n",
527 			(long long unsigned)pat->size_limit_xfr);
528 }
529 
530 void
531 config_test_print_server(nsd_options_type* opt)
532 {
533 	ip_address_option_type* ip;
534 	key_options_type* key;
535 	tls_auth_options_type* tlsauth;
536 	zone_options_type* zone;
537 	pattern_options_type* pat;
538 
539 	printf("# Config settings.\n");
540 	printf("server:\n");
541 	printf("\tdebug-mode: %s\n", opt->debug_mode?"yes":"no");
542 	printf("\tip-transparent: %s\n", opt->ip_transparent?"yes":"no");
543 	printf("\tip-freebind: %s\n", opt->ip_freebind?"yes":"no");
544 	printf("\treuseport: %s\n", opt->reuseport?"yes":"no");
545 	printf("\tdo-ip4: %s\n", opt->do_ip4?"yes":"no");
546 	printf("\tdo-ip6: %s\n", opt->do_ip6?"yes":"no");
547 	printf("\tsend-buffer-size: %d\n", opt->send_buffer_size);
548 	printf("\treceive-buffer-size: %d\n", opt->receive_buffer_size);
549 	printf("\thide-version: %s\n", opt->hide_version?"yes":"no");
550 	printf("\thide-identity: %s\n", opt->hide_identity?"yes":"no");
551 	printf("\tdrop-updates: %s\n", opt->drop_updates?"yes":"no");
552 	printf("\ttcp-reject-overflow: %s\n",
553 		opt->tcp_reject_overflow ? "yes" : "no");
554 	print_string_var("database:", opt->database);
555 	print_string_var("identity:", opt->identity);
556 	print_string_var("version:", opt->version);
557 	print_string_var("nsid:", opt->nsid);
558 	print_string_var("logfile:", opt->logfile);
559 	printf("\tlog-only-syslog: %s\n", opt->log_only_syslog?"yes":"no");
560 	printf("\tserver-count: %d\n", opt->server_count);
561 	if(opt->cpu_affinity) {
562 		cpu_option_type *n;
563 		printf("\tcpu-affinity:");
564 		for(n = opt->cpu_affinity; n; n = n->next) {
565 			printf(" %d", n->cpu);
566 		}
567 		printf("\n");
568 	}
569 	if(opt->cpu_affinity && opt->service_cpu_affinity) {
570 		cpu_map_option_type *n;
571 		for(n = opt->service_cpu_affinity; n; n = n->next) {
572 			if(n->service > 0) {
573 				printf("\tserver-%d-cpu-affinity: %d\n",
574 				       n->service, n->cpu);
575 			} else if(n->service == -1) {
576 				printf("\txfrd-cpu-affinity: %d\n",
577 				       n->cpu);
578 			}
579 		}
580 	}
581 	printf("\ttcp-count: %d\n", opt->tcp_count);
582 	printf("\ttcp-query-count: %d\n", opt->tcp_query_count);
583 	printf("\ttcp-timeout: %d\n", opt->tcp_timeout);
584 	printf("\ttcp-mss: %d\n", opt->tcp_mss);
585 	printf("\toutgoing-tcp-mss: %d\n", opt->outgoing_tcp_mss);
586 	printf("\txfrd-tcp-max: %d\n", opt->xfrd_tcp_max);
587 	printf("\txfrd-tcp-pipeline: %d\n", opt->xfrd_tcp_pipeline);
588 	printf("\tipv4-edns-size: %d\n", (int) opt->ipv4_edns_size);
589 	printf("\tipv6-edns-size: %d\n", (int) opt->ipv6_edns_size);
590 	print_string_var("pidfile:", opt->pidfile);
591 	print_string_var("port:", opt->port);
592 	printf("\tstatistics: %d\n", opt->statistics);
593 	print_string_var("chroot:", opt->chroot);
594 	print_string_var("username:", opt->username);
595 	print_string_var("zonesdir:", opt->zonesdir);
596 	print_string_var("xfrdfile:", opt->xfrdfile);
597 	print_string_var("zonelistfile:", opt->zonelistfile);
598 	print_string_var("xfrdir:", opt->xfrdir);
599 	printf("\txfrd-reload-timeout: %d\n", opt->xfrd_reload_timeout);
600 	printf("\tlog-time-ascii: %s\n", opt->log_time_ascii?"yes":"no");
601 	printf("\tround-robin: %s\n", opt->round_robin?"yes":"no");
602 	printf("\tminimal-responses: %s\n", opt->minimal_responses?"yes":"no");
603 	printf("\tconfine-to-zone: %s\n",
604 		opt->confine_to_zone ? "yes" : "no");
605 	printf("\trefuse-any: %s\n", opt->refuse_any?"yes":"no");
606 	printf("\tverbosity: %d\n", opt->verbosity);
607 	for(ip = opt->ip_addresses; ip; ip=ip->next)
608 	{
609 		printf("\tip-address: %s", ip->address);
610 		if(ip->servers) {
611 			const char *sep;
612 			struct range_option *n;
613 			printf(" servers=\"");
614 			for(n=ip->servers, sep=""; n; n = n->next, sep=" ") {
615 				if(n->first == n->last) {
616 					printf("%s%d", sep, n->first);
617 				} else {
618 					printf("%s%d-%d", sep, n->first, n->last);
619 				}
620 			}
621 			printf("\"");
622 		}
623 		if(ip->fib != -1) {
624 			printf(" setfib=%d", ip->fib);
625 		}
626 		printf("\n");
627 	}
628 #ifdef RATELIMIT
629 	printf("\trrl-size: %d\n", (int)opt->rrl_size);
630 	printf("\trrl-ratelimit: %d\n", (int)opt->rrl_ratelimit);
631 	printf("\trrl-slip: %d\n", (int)opt->rrl_slip);
632 	printf("\trrl-ipv4-prefix-length: %d\n", (int)opt->rrl_ipv4_prefix_length);
633 	printf("\trrl-ipv6-prefix-length: %d\n", (int)opt->rrl_ipv6_prefix_length);
634 	printf("\trrl-whitelist-ratelimit: %d\n", (int)opt->rrl_whitelist_ratelimit);
635 #endif
636 	printf("\tzonefiles-check: %s\n", opt->zonefiles_check?"yes":"no");
637 	printf("\tzonefiles-write: %d\n", opt->zonefiles_write);
638 	print_string_var("tls-service-key:", opt->tls_service_key);
639 	print_string_var("tls-service-pem:", opt->tls_service_pem);
640 	print_string_var("tls-service-ocsp:", opt->tls_service_ocsp);
641 	print_string_var("tls-port:", opt->tls_port);
642 	print_string_var("tls-cert-bundle:", opt->tls_cert_bundle);
643 	printf("\tanswer-cookie: %s\n", opt->answer_cookie?"yes":"no");
644 	if (opt->cookie_secret)
645 		print_string_var("cookie-secret:", opt->cookie_secret);
646 	if (opt->cookie_secret_file)
647 		print_string_var("cookie-secret-file:", opt->cookie_secret_file);
648 
649 #ifdef USE_DNSTAP
650 	printf("\ndnstap:\n");
651 	printf("\tdnstap-enable: %s\n", opt->dnstap_enable?"yes":"no");
652 	print_string_var("dnstap-socket-path:", opt->dnstap_socket_path);
653 	printf("\tdnstap-send-identity: %s\n", opt->dnstap_send_identity?"yes":"no");
654 	printf("\tdnstap-send-version: %s\n", opt->dnstap_send_version?"yes":"no");
655 	print_string_var("dnstap-identity:", opt->dnstap_identity);
656 	print_string_var("dnstap-version:", opt->dnstap_version);
657 	printf("\tdnstap-log-auth-query-messages: %s\n", opt->dnstap_log_auth_query_messages?"yes":"no");
658 	printf("\tdnstap-log-auth-response-messages: %s\n", opt->dnstap_log_auth_response_messages?"yes":"no");
659 #endif
660 
661 	printf("\nremote-control:\n");
662 	printf("\tcontrol-enable: %s\n", opt->control_enable?"yes":"no");
663 	for(ip = opt->control_interface; ip; ip=ip->next)
664 		print_string_var("control-interface:", ip->address);
665 	printf("\tcontrol-port: %d\n", opt->control_port);
666 	print_string_var("server-key-file:", opt->server_key_file);
667 	print_string_var("server-cert-file:", opt->server_cert_file);
668 	print_string_var("control-key-file:", opt->control_key_file);
669 	print_string_var("control-cert-file:", opt->control_cert_file);
670 
671 	RBTREE_FOR(key, key_options_type*, opt->keys)
672 	{
673 		printf("\nkey:\n");
674 		print_string_var("name:", key->name);
675 		print_string_var("algorithm:", key->algorithm);
676 		print_string_var("secret:", key->secret);
677 	}
678 	RBTREE_FOR(tlsauth, tls_auth_options_type*, opt->tls_auths)
679 	{
680 		printf("\ntls-auth:\n");
681 		print_string_var("name:", tlsauth->name);
682 		print_string_var("auth-domain-name:", tlsauth->auth_domain_name);
683 	}
684 	RBTREE_FOR(pat, pattern_options_type*, opt->patterns)
685 	{
686 		if(pat->implicit) continue;
687 		printf("\npattern:\n");
688 		print_string_var("name:", pat->pname);
689 		print_zone_content_elems(pat);
690 	}
691 	RBTREE_FOR(zone, zone_options_type*, opt->zone_options)
692 	{
693 		if(!zone->part_of_config)
694 			continue;
695 		printf("\nzone:\n");
696 		print_string_var("name:", zone->name);
697 		print_zone_content_elems(zone->pattern);
698 	}
699 }
700 
701 static int
702 additional_checks(nsd_options_type* opt, const char* filename)
703 {
704 	zone_options_type* zone;
705 	int errors = 0;
706 
707 	RBTREE_FOR(zone, zone_options_type*, opt->zone_options)
708 	{
709 		const dname_type* dname = dname_parse(opt->region, zone->name); /* memory leak. */
710 		if(!dname) {
711 			fprintf(stderr, "%s: cannot parse zone name syntax for zone %s.\n", filename, zone->name);
712 			errors ++;
713 			continue;
714 		}
715 #ifndef ROOT_SERVER
716 		/* Is it a root zone? Are we a root server then? Idiot proof. */
717 		if(dname->label_count == 1) {
718 			fprintf(stderr, "%s: not configured as a root server.\n", filename);
719 			errors ++;
720 		}
721 #endif
722 		if(zone->pattern->allow_notify && !zone->pattern->request_xfr) {
723 			fprintf(stderr, "%s: zone %s has allow-notify but no request-xfr"
724 				" items. Where can it get a zone transfer when a notify "
725 				"is received?\n", filename, zone->name);
726 			errors ++;
727 		}
728 		if(!zone_is_slave(zone) && (!zone->pattern->zonefile ||
729 			zone->pattern->zonefile[0] == 0)) {
730 			fprintf(stderr, "%s: zone %s is a master zone but has "
731 				"no zonefile. Where can the data come from?\n",
732 				filename, zone->name);
733 			errors ++;
734 		}
735 	}
736 
737 #ifndef BIND8_STATS
738 	if(opt->statistics > 0)
739 	{
740 		fprintf(stderr, "%s: 'statistics: %d' but BIND 8 statistics feature not enabled.\n",
741 			filename, opt->statistics);
742 		errors ++;
743 	}
744 #endif
745 #ifndef HAVE_CHROOT
746 	if(opt->chroot != 0)
747 	{
748 		fprintf(stderr, "%s: chroot %s given. chroot not supported on this platform.\n",
749 			filename, opt->chroot);
750 		errors ++;
751 	}
752 #endif
753 	if (opt->identity && strlen(opt->identity) > UCHAR_MAX) {
754                 fprintf(stderr, "%s: server identity too long (%u characters)\n",
755                       filename, (unsigned) strlen(opt->identity));
756 		errors ++;
757         }
758 	if (opt->version && strlen(opt->version) > UCHAR_MAX) {
759                 fprintf(stderr, "%s: server version too long (%u characters)\n",
760                       filename, (unsigned) strlen(opt->version));
761 		errors ++;
762         }
763 
764 	/* not done here: parsing of ip-address. parsing of username. */
765 
766         if (opt->chroot && opt->chroot[0]) {
767 		/* append trailing slash for strncmp checking */
768 		append_trailing_slash(&opt->chroot, opt->region);
769 		append_trailing_slash(&opt->xfrdir, opt->region);
770 		append_trailing_slash(&opt->zonesdir, opt->region);
771 
772 		/* zonesdir must be absolute and within chroot,
773 		 * all other pathnames may be relative to zonesdir */
774 		if (strncmp(opt->zonesdir, opt->chroot, strlen(opt->chroot)) != 0) {
775 			fprintf(stderr, "%s: zonesdir %s has to be an absolute path that starts with the chroot path %s\n",
776 				filename, opt->zonesdir, opt->chroot);
777 			errors ++;
778                 }
779 		if (!file_inside_chroot(opt->pidfile, opt->chroot)) {
780 			fprintf(stderr, "%s: pidfile %s is not relative to chroot %s.\n",
781 				filename, opt->pidfile, opt->chroot);
782 			errors ++;
783                 }
784 		if (!file_inside_chroot(opt->database, opt->chroot)) {
785 			fprintf(stderr, "%s: database %s is not relative to chroot %s.\n",
786 				filename, opt->database, opt->chroot);
787 			errors ++;
788                 }
789 		if (!file_inside_chroot(opt->xfrdfile, opt->chroot)) {
790 			fprintf(stderr, "%s: xfrdfile %s is not relative to chroot %s.\n",
791 				filename, opt->xfrdfile, opt->chroot);
792 			errors ++;
793                 }
794 		if (!file_inside_chroot(opt->zonelistfile, opt->chroot)) {
795 			fprintf(stderr, "%s: zonelistfile %s is not relative to chroot %s.\n",
796 				filename, opt->zonelistfile, opt->chroot);
797 			errors ++;
798                 }
799 		if (!file_inside_chroot(opt->xfrdir, opt->chroot)) {
800 			fprintf(stderr, "%s: xfrdir %s is not relative to chroot %s.\n",
801 				filename, opt->xfrdir, opt->chroot);
802 			errors ++;
803                 }
804 	}
805 
806 	if (atoi(opt->port) <= 0) {
807 		fprintf(stderr, "%s: port number '%s' is not a positive number.\n",
808 			filename, opt->port);
809 		errors ++;
810 	}
811 	if(errors != 0) {
812 		fprintf(stderr, "%s: %d semantic errors in %d zones, %d keys, %d tls-auth.\n",
813 			filename, errors, (int)nsd_options_num_zones(opt),
814 			(int)opt->keys->count,
815 			(int)opt->tls_auths->count);
816 	}
817 
818 	return (errors == 0);
819 }
820 
821 int
822 main(int argc, char* argv[])
823 {
824 	int c;
825 	int verbose = 0;
826 	int key_sec = 0;
827 	int final = 0;
828 	const char * conf_opt = NULL; /* what option do you want? Can be NULL -> print all */
829 	const char * conf_zone = NULL; /* what zone are we talking about */
830 	const char * conf_key = NULL; /* what key is needed */
831 	const char * conf_tlsauth = NULL; /* what tls-auth is needed */
832 	const char * conf_pat = NULL; /* what pattern is talked about */
833 	const char* configfile;
834 	nsd_options_type *options;
835 
836 	log_init("nsd-checkconf");
837 
838 	/* Parse the command line... */
839 	while ((c = getopt(argc, argv, "vfho:a:p:s:z:t:")) != -1) {
840 		switch (c) {
841 		case 'v':
842 			verbose = 1;
843 			verbosity++;
844 			break;
845 		case 'o':
846 			conf_opt = optarg;
847 			break;
848 		case 'f':
849 			final = 1;
850 			break;
851 		case 'p':
852 			conf_pat = optarg;
853 			break;
854 		case 'a':
855 			if (conf_key) {
856 				fprintf(stderr, "Error: cannot combine -a with -s or other -a.\n");
857 				exit(1);
858 			}
859 			conf_key = optarg;
860 			break;
861 		case 's':
862 			if (conf_key) {
863 				fprintf(stderr, "Error: cannot combine -s with -a or other -s.\n");
864 				exit(1);
865 			}
866 			conf_key = optarg;
867 			key_sec = 1;
868 			break;
869 		case 't':
870 			conf_tlsauth = optarg;
871 			break;
872 		case 'z':
873 			conf_zone = optarg;
874 			break;
875 		case 'h':
876 		default:
877 			usage();
878 		};
879 	}
880 	argc -= optind;
881 	argv += optind;
882 	if (argc == 0 || argc>=2) {
883 		usage();
884 	}
885 	configfile = argv[0];
886 
887 	/* read config file */
888 	options = nsd_options_create(region_create(xalloc, free));
889 	tsig_init(options->region);
890 	if (!parse_options_file(options, configfile, NULL, NULL) ||
891 	   !additional_checks(options, configfile)) {
892 		exit(2);
893 	}
894 	if (conf_opt || conf_key || conf_tlsauth) {
895 		config_print_zone(options, conf_key, key_sec,
896 			underscore(conf_opt), conf_zone, conf_pat,  conf_tlsauth, final);
897 	} else {
898 		if (verbose) {
899 			printf("# Read file %s: %d patterns, %d fixed-zones, "
900 				"%d keys, %d tls-auth.\n",
901 				configfile,
902 				(int)options->patterns->count,
903 				(int)nsd_options_num_zones(options),
904 				(int)options->keys->count,
905 				(int)options->tls_auths->count);
906 			config_test_print_server(options);
907 		}
908 	}
909 	return 0;
910 }
911