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