xref: /openbsd-src/usr.sbin/nsd/nsd-checkconf.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
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 
24 #define ZONE_GET_ACL(NAME, VAR, PATTERN) 		\
25 	if (strcasecmp(#NAME, (VAR)) == 0) { 	\
26 		quote_acl(PATTERN->NAME); 	\
27 		return; 			\
28 	}
29 
30 #define ZONE_GET_OUTGOING(NAME, VAR, PATTERN)			\
31 	if (strcasecmp(#NAME, (VAR)) == 0) {		\
32 		acl_options_t* acl; 			\
33 		for(acl=PATTERN->NAME; acl; acl=acl->next)	\
34 			quote(acl->ip_address_spec);	\
35 		return; 				\
36 	}
37 
38 #define ZONE_GET_STR(NAME, VAR, PATTERN) 		\
39 	if (strcasecmp(#NAME, (VAR)) == 0) { 	\
40 		quote(PATTERN->NAME); 		\
41 		return; 			\
42 	}
43 
44 #define ZONE_GET_BIN(NAME, VAR, PATTERN) 			\
45 	if (strcasecmp(#NAME, (VAR)) == 0) { 		\
46 		printf("%s\n", (PATTERN->NAME)?"yes":"no"); 	\
47 		return;					\
48 	}
49 
50 #define ZONE_GET_RRL(NAME, VAR, PATTERN) 			\
51 	if (strcasecmp(#NAME, (VAR)) == 0) { 		\
52 		zone_print_rrl_whitelist("", PATTERN->NAME);	\
53 		return;					\
54 	}
55 
56 #define SERV_GET_BIN(NAME, VAR) 			\
57 	if (strcasecmp(#NAME, (VAR)) == 0) { 		\
58 		printf("%s\n", opt->NAME?"yes":"no"); 	\
59 		return;					\
60 	}
61 
62 #define SERV_GET_STR(NAME, VAR) 		\
63 	if (strcasecmp(#NAME, (VAR)) == 0) { 	\
64 		quote(opt->NAME); 		\
65 		return; 			\
66 	}
67 
68 #define SERV_GET_INT(NAME, VAR) 		\
69 	if (strcasecmp(#NAME, (VAR)) == 0) { 	\
70 		printf("%d\n", (int) opt->NAME); 	\
71 		return; 			\
72 	}
73 
74 #define SERV_GET_IP(NAME, MEMBER, VAR) 				\
75 	if (strcasecmp(#NAME, (VAR)) == 0) { 		\
76 		for(ip = opt->MEMBER; ip; ip=ip->next)	\
77 		{						\
78 			quote(ip->address);			\
79 		}						\
80 		return;						\
81 	}
82 
83 #ifdef RATELIMIT
84 static void zone_print_rrl_whitelist(const char* s, uint16_t w)
85 {
86 	int i;
87 	if(w==rrl_type_all) {
88 		printf("%sall\n", s);
89 		return;
90 	}
91 	for(i=0x01; i <= 0x80; i<<=1) {
92 		if( (w&i) )
93 			printf("%s%s\n", s, rrltype2str(i));
94 	}
95 }
96 #endif /* RATELIMIT */
97 
98 static char buf[BUFSIZ];
99 
100 static char *
101 underscore(const char *s) {
102 	const char *j = s;
103 	size_t i = 0;
104 
105 	while(j && *j) {
106 		if (*j == '-') {
107 			buf[i++] = '_';
108 		} else {
109 			buf[i++] = *j;
110 		}
111 		j++;
112 		if (i >= BUFSIZ) {
113 			return NULL;
114 		}
115 	}
116 	buf[i] = '\0';
117 	return buf;
118 }
119 
120 static void
121 usage(void)
122 {
123 	fprintf(stderr, "usage: nsd-checkconf [-v|-h] [-o option] [-z zonename]\n");
124 	fprintf(stderr, "                     [-s keyname] <configfilename>\n");
125 	fprintf(stderr, "       Checks NSD configuration file for errors.\n");
126 	fprintf(stderr, "       Version %s. Report bugs to <%s>.\n\n",
127 		PACKAGE_VERSION, PACKAGE_BUGREPORT);
128 	fprintf(stderr, "Use with a configfile as argument to check syntax.\n");
129 	fprintf(stderr, "Use with -o, -z or -s options to query the configuration.\n\n");
130 	fprintf(stderr, "-v		Verbose, echo settings that take effect to std output.\n");
131 	fprintf(stderr, "-h		Print this help information.\n");
132 	fprintf(stderr, "-o option	Print value of the option specified to stdout.\n");
133 	fprintf(stderr, "-p pattern	Print option value for the pattern given.\n");
134 	fprintf(stderr, "-z zonename	Print option value for the zone given.\n");
135 	fprintf(stderr, "-a keyname	Print algorithm name for the TSIG key.\n");
136 	fprintf(stderr, "-s keyname	Print base64 secret blob for the TSIG key.\n");
137 	exit(1);
138 }
139 
140 static void
141 print_string_var(const char* varname, const char* value)
142 {
143 	if (!value) {
144 		printf("\t#%s\n", varname);
145 	} else {
146 		printf("\t%s \"%s\"\n", varname, value);
147 	}
148 }
149 
150 static void
151 quote(const char *v)
152 {
153 	if(v==NULL)
154 		printf("\n");
155 	else
156 		printf("%s\n", v);
157 }
158 
159 static void
160 quote_acl(acl_options_t* acl)
161 {
162 	while(acl)
163 	{
164 		printf("%s %s\n", acl->ip_address_spec,
165 			acl->nokey?"NOKEY":(acl->blocked?"BLOCKED":
166 			(acl->key_name?acl->key_name:"(null)")));
167 		acl=acl->next;
168 	}
169 }
170 
171 static void
172 print_acl(const char* varname, acl_options_t* acl)
173 {
174 	while(acl)
175 	{
176 		printf("\t%s ", varname);
177 		if(acl->use_axfr_only)
178 			printf("AXFR ");
179 		if(acl->allow_udp)
180 			printf("UDP ");
181 		printf("%s %s\n", acl->ip_address_spec,
182 			acl->nokey?"NOKEY":(acl->blocked?"BLOCKED":
183 			(acl->key_name?acl->key_name:"(null)")));
184 		if(verbosity>1) {
185 			printf("\t# %s", acl->is_ipv6?"ip6":"ip4");
186 			if(acl->port == 0) printf(" noport");
187 			else printf(" port=%d", acl->port);
188 			if(acl->rangetype == acl_range_single) printf(" single");
189 			if(acl->rangetype == acl_range_mask)   printf(" masked");
190 			if(acl->rangetype == acl_range_subnet) printf(" subnet");
191 			if(acl->rangetype == acl_range_minmax) printf(" minmax");
192 			if(acl->is_ipv6) {
193 #ifdef INET6
194 				char dest[128];
195 				inet_ntop(AF_INET6, &acl->addr.addr6, dest, sizeof(dest));
196 				printf(" addr=%s", dest);
197 				if(acl->rangetype != acl_range_single) {
198 					inet_ntop(AF_INET6, &acl->range_mask.addr6, dest, sizeof(dest));
199 					printf(" rangemask=%s", dest);
200 				}
201 #else
202 				printf(" ip6addr-noip6defined");
203 #endif
204 			} else {
205 				char dest[128];
206 				inet_ntop(AF_INET, &acl->addr.addr, dest, sizeof(dest));
207 				printf(" addr=%s", dest);
208 				if(acl->rangetype != acl_range_single) {
209 					inet_ntop(AF_INET, &acl->range_mask.addr, dest, sizeof(dest));
210 					printf(" rangemask=%s", dest);
211 				}
212 			}
213 			printf("\n");
214 		}
215 		acl=acl->next;
216 	}
217 }
218 
219 static void
220 print_acl_ips(const char* varname, acl_options_t* acl)
221 {
222 	while(acl)
223 	{
224 		printf("\t%s %s\n", varname, acl->ip_address_spec);
225 		acl=acl->next;
226 	}
227 }
228 
229 void
230 config_print_zone(nsd_options_t* opt, const char* k, int s, const char *o,
231 	const char *z, const char* pat)
232 {
233 	ip_address_option_t* ip;
234 
235 	if (k) {
236 		/* find key */
237 		key_options_t* key = key_options_find(opt, k);
238 		if(key) {
239 			if (s) {
240 				quote(key->secret);
241 			} else {
242 				quote(key->algorithm);
243 			}
244 			return;
245 		}
246 		printf("Could not find key %s\n", k);
247 		return;
248 	}
249 
250 	if (!o) {
251 		return;
252 	}
253 
254 	if (z) {
255 		zone_options_t* zone;
256 		const dname_type *dname = dname_parse(opt->region, z);
257 		if(!dname) {
258 			printf("Could not parse zone name %s\n", z);
259 			exit(1);
260 		}
261 		zone = zone_options_find(opt, dname);
262 		if(!zone) {
263 			printf("Zone does not exist: %s\n", z);
264 			exit(1);
265 		}
266 		ZONE_GET_STR(name, o, zone);
267 		if(strcasecmp("pattern", o)==0) {
268 			quote(zone->pattern->pname);
269 			return;
270 		}
271 		ZONE_GET_BIN(part_of_config, o, zone);
272 		ZONE_GET_STR(zonefile, o, zone->pattern);
273 		ZONE_GET_ACL(request_xfr, o, zone->pattern);
274 		ZONE_GET_ACL(provide_xfr, o, zone->pattern);
275 		ZONE_GET_ACL(allow_notify, o, zone->pattern);
276 		ZONE_GET_ACL(notify, o, zone->pattern);
277 		ZONE_GET_BIN(notify_retry, o, zone->pattern);
278 		ZONE_GET_OUTGOING(outgoing_interface, o, zone->pattern);
279 		ZONE_GET_BIN(allow_axfr_fallback, o, zone->pattern);
280 #ifdef RATELIMIT
281 		ZONE_GET_RRL(rrl_whitelist, o, zone->pattern);
282 #endif
283 		printf("Zone option not handled: %s %s\n", z, o);
284 		exit(1);
285 	} else if(pat) {
286 		pattern_options_t* p = pattern_options_find(opt, pat);
287 		if(!p) {
288 			printf("Pattern does not exist: %s\n", pat);
289 			exit(1);
290 		}
291 		if(strcasecmp("name", o)==0) {
292 			quote(p->pname);
293 			return;
294 		}
295 		ZONE_GET_STR(zonefile, o, p);
296 		ZONE_GET_ACL(request_xfr, o, p);
297 		ZONE_GET_ACL(provide_xfr, o, p);
298 		ZONE_GET_ACL(allow_notify, o, p);
299 		ZONE_GET_ACL(notify, o, p);
300 		ZONE_GET_BIN(notify_retry, o, p);
301 		ZONE_GET_OUTGOING(outgoing_interface, o, p);
302 		ZONE_GET_BIN(allow_axfr_fallback, o, p);
303 #ifdef RATELIMIT
304 		ZONE_GET_RRL(rrl_whitelist, o, p);
305 #endif
306 		printf("Pattern option not handled: %s %s\n", pat, o);
307 		exit(1);
308 	} else {
309 		/* look in the server section */
310 		SERV_GET_IP(ip_address, ip_addresses, o);
311 		/* bin */
312 		SERV_GET_BIN(ip_transparent, o);
313 		SERV_GET_BIN(debug_mode, o);
314 		SERV_GET_BIN(do_ip4, o);
315 		SERV_GET_BIN(do_ip6, o);
316 		SERV_GET_BIN(hide_version, o);
317 		SERV_GET_BIN(zonefiles_check, o);
318 		/* str */
319 		SERV_GET_STR(database, o);
320 		SERV_GET_STR(identity, o);
321 		SERV_GET_STR(nsid, o);
322 		SERV_GET_STR(logfile, o);
323 		SERV_GET_STR(pidfile, o);
324 		SERV_GET_STR(chroot, o);
325 		SERV_GET_STR(username, o);
326 		SERV_GET_STR(zonesdir, o);
327 		SERV_GET_STR(xfrdfile, o);
328 		SERV_GET_STR(xfrdir, o);
329 		SERV_GET_STR(zonelistfile, o);
330 		SERV_GET_STR(port, o);
331 		/* int */
332 		SERV_GET_INT(server_count, o);
333 		SERV_GET_INT(tcp_count, o);
334 		SERV_GET_INT(tcp_query_count, o);
335 		SERV_GET_INT(tcp_timeout, o);
336 		SERV_GET_INT(ipv4_edns_size, o);
337 		SERV_GET_INT(ipv6_edns_size, o);
338 		SERV_GET_INT(statistics, o);
339 		SERV_GET_INT(xfrd_reload_timeout, o);
340 		SERV_GET_INT(verbosity, o);
341 #ifdef RATELIMIT
342 		SERV_GET_INT(rrl_size, o);
343 		SERV_GET_INT(rrl_ratelimit, o);
344 		SERV_GET_INT(rrl_slip, o);
345 		SERV_GET_INT(rrl_ipv4_prefix_length, o);
346 		SERV_GET_INT(rrl_ipv6_prefix_length, o);
347 		SERV_GET_INT(rrl_whitelist_ratelimit, o);
348 #endif
349 		/* remote control */
350 		SERV_GET_BIN(control_enable, o);
351 		SERV_GET_IP(control_interface, control_interface, o);
352 		SERV_GET_INT(control_port, o);
353 		SERV_GET_STR(server_key_file, o);
354 		SERV_GET_STR(server_cert_file, o);
355 		SERV_GET_STR(control_key_file, o);
356 		SERV_GET_STR(control_cert_file, o);
357 
358 		if(strcasecmp(o, "zones") == 0) {
359 			zone_options_t* zone;
360 			RBTREE_FOR(zone, zone_options_t*, opt->zone_options)
361 				quote(zone->name);
362 			return;
363 		}
364 		if(strcasecmp(o, "patterns") == 0) {
365 			pattern_options_t* p;
366 			RBTREE_FOR(p, pattern_options_t*, opt->patterns)
367 				quote(p->pname);
368 			return;
369 		}
370 		printf("Server option not handled: %s\n", o);
371 		exit(1);
372 	}
373 }
374 
375 /* print zone content items */
376 static void print_zone_content_elems(pattern_options_t* pat)
377 {
378 	if(pat->zonefile)
379 		print_string_var("zonefile:", pat->zonefile);
380 #ifdef RATELIMIT
381 	zone_print_rrl_whitelist("\trrl-whitelist: ", pat->rrl_whitelist);
382 #endif
383 	print_acl("allow-notify:", pat->allow_notify);
384 	print_acl("request-xfr:", pat->request_xfr);
385 	if(!pat->notify_retry_is_default)
386 		printf("\tnotify-retry: %d\n", pat->notify_retry);
387 	print_acl("notify:", pat->notify);
388 	print_acl("provide-xfr:", pat->provide_xfr);
389 	print_acl_ips("outgoing-interface:", pat->outgoing_interface);
390 	if(!pat->allow_axfr_fallback_is_default)
391 		printf("\tallow-axfr-fallback: %s\n",
392 			pat->allow_axfr_fallback?"yes":"no");
393 }
394 
395 void
396 config_test_print_server(nsd_options_t* opt)
397 {
398 	ip_address_option_t* ip;
399 	key_options_t* key;
400 	zone_options_t* zone;
401 	pattern_options_t* pat;
402 
403 	printf("# Config settings.\n");
404 	printf("server:\n");
405 	printf("\tdebug-mode: %s\n", opt->debug_mode?"yes":"no");
406 	printf("\tip-transparent: %s\n", opt->ip_transparent?"yes":"no");
407 	printf("\tdo-ip4: %s\n", opt->do_ip4?"yes":"no");
408 	printf("\tdo-ip6: %s\n", opt->do_ip6?"yes":"no");
409 	printf("\thide-version: %s\n", opt->hide_version?"yes":"no");
410 	print_string_var("database:", opt->database);
411 	print_string_var("identity:", opt->identity);
412 	print_string_var("nsid:", opt->nsid);
413 	print_string_var("logfile:", opt->logfile);
414 	printf("\tserver_count: %d\n", opt->server_count);
415 	printf("\ttcp_count: %d\n", opt->tcp_count);
416 	printf("\ttcp_query_count: %d\n", opt->tcp_query_count);
417 	printf("\ttcp_timeout: %d\n", opt->tcp_timeout);
418 	printf("\tipv4-edns-size: %d\n", (int) opt->ipv4_edns_size);
419 	printf("\tipv6-edns-size: %d\n", (int) opt->ipv6_edns_size);
420 	print_string_var("pidfile:", opt->pidfile);
421 	print_string_var("port:", opt->port);
422 	printf("\tstatistics: %d\n", opt->statistics);
423 	print_string_var("chroot:", opt->chroot);
424 	print_string_var("username:", opt->username);
425 	print_string_var("zonesdir:", opt->zonesdir);
426 	print_string_var("xfrdfile:", opt->xfrdfile);
427 	print_string_var("zonelistfile:", opt->zonelistfile);
428 	print_string_var("xfrdir:", opt->xfrdir);
429 	printf("\txfrd_reload_timeout: %d\n", opt->xfrd_reload_timeout);
430 	printf("\tverbosity: %d\n", opt->verbosity);
431 	for(ip = opt->ip_addresses; ip; ip=ip->next)
432 	{
433 		print_string_var("ip-address:", ip->address);
434 	}
435 #ifdef RATELIMIT
436 	printf("\trrl-size: %d\n", (int)opt->rrl_size);
437 	printf("\trrl-ratelimit: %d\n", (int)opt->rrl_ratelimit);
438 	printf("\trrl-slip: %d\n", (int)opt->rrl_slip);
439 	printf("\trrl-ipv4-prefix-length: %d\n", (int)opt->rrl_ipv4_prefix_length);
440 	printf("\trrl-ipv6-prefix-length: %d\n", (int)opt->rrl_ipv6_prefix_length);
441 	printf("\trrl-whitelist-ratelimit: %d\n", (int)opt->rrl_whitelist_ratelimit);
442 #endif
443 	printf("\tzonefiles-check: %s\n", opt->zonefiles_check?"yes":"no");
444 
445 	printf("\nremote-control:\n");
446 	printf("\tcontrol-enable: %s\n", opt->control_enable?"yes":"no");
447 	for(ip = opt->control_interface; ip; ip=ip->next)
448 		print_string_var("control-interface:", ip->address);
449 	printf("\tcontrol-port: %d\n", opt->control_port);
450 	print_string_var("server-key-file:", opt->server_key_file);
451 	print_string_var("server-cert-file:", opt->server_cert_file);
452 	print_string_var("control-key-file:", opt->control_key_file);
453 	print_string_var("control-cert-file:", opt->control_cert_file);
454 
455 	RBTREE_FOR(key, key_options_t*, opt->keys)
456 	{
457 		printf("\nkey:\n");
458 		print_string_var("name:", key->name);
459 		print_string_var("algorithm:", key->algorithm);
460 		print_string_var("secret:", key->secret);
461 	}
462 	RBTREE_FOR(pat, pattern_options_t*, opt->patterns)
463 	{
464 		if(pat->implicit) continue;
465 		printf("\npattern:\n");
466 		print_string_var("name:", pat->pname);
467 		print_zone_content_elems(pat);
468 	}
469 	RBTREE_FOR(zone, zone_options_t*, opt->zone_options)
470 	{
471 		if(!zone->part_of_config)
472 			continue;
473 		printf("\nzone:\n");
474 		print_string_var("name:", zone->name);
475 		print_zone_content_elems(zone->pattern);
476 	}
477 
478 }
479 
480 static void
481 append_trailing_slash(const char** dirname, region_type* region)
482 {
483 	int l = strlen(*dirname);
484 	if (l>0 && (*dirname)[l-1] != '/') {
485 		char *dirname_slash = region_alloc(region, l+2);
486 		memcpy(dirname_slash, *dirname, l+1);
487 		strlcat(dirname_slash, "/", l+2);
488 		*dirname = dirname_slash;
489 	}
490 }
491 
492 static int
493 file_inside_chroot(const char* fname, const char* chr)
494 {
495 	/* true if filename starts with chroot or is not absolute */
496 	return ((fname && fname[0] && strncmp(fname, chr, strlen(chr)) == 0) ||
497 		(fname && fname[0] != '/'));
498 }
499 
500 static int
501 additional_checks(nsd_options_t* opt, const char* filename)
502 {
503 	ip_address_option_t* ip = opt->ip_addresses;
504 	zone_options_t* zone;
505 	int num = 0;
506 	int errors = 0;
507 	while(ip) {
508 		num++;
509 		ip = ip->next;
510 	}
511 	if(num > MAX_INTERFACES) {
512 		fprintf(stderr, "%s: too many interfaces (ip-address:) specified.\n", filename);
513 		errors ++;
514 	}
515 
516 	RBTREE_FOR(zone, zone_options_t*, opt->zone_options)
517 	{
518 		const dname_type* dname = dname_parse(opt->region, zone->name); /* memory leak. */
519 		if(!dname) {
520 			fprintf(stderr, "%s: cannot parse zone name syntax for zone %s.\n", filename, zone->name);
521 			errors ++;
522 		}
523 		if(zone->pattern->allow_notify && !zone->pattern->request_xfr) {
524 			fprintf(stderr, "%s: zone %s has allow-notify but no request-xfr"
525 				" items. Where can it get a zone transfer when a notify "
526 				"is received?\n", filename, zone->name);
527 			errors ++;
528 		}
529 	}
530 
531 #ifndef BIND8_STATS
532 	if(opt->statistics > 0)
533 	{
534 		fprintf(stderr, "%s: 'statistics: %d' but BIND 8 statistics feature not enabled.\n",
535 			filename, opt->statistics);
536 		errors ++;
537 	}
538 #endif
539 #ifndef HAVE_CHROOT
540 	if(opt->chroot != 0)
541 	{
542 		fprintf(stderr, "%s: chroot %s given. chroot not supported on this platform.\n",
543 			filename, opt->chroot);
544 		errors ++;
545 	}
546 #endif
547 	if (opt->identity && strlen(opt->identity) > UCHAR_MAX) {
548                 fprintf(stderr, "%s: server identity too long (%u characters)\n",
549                       filename, (unsigned) strlen(opt->identity));
550 		errors ++;
551         }
552 
553 	/* not done here: parsing of ip-address. parsing of username. */
554 
555         if (opt->chroot && opt->chroot[0]) {
556 		/* append trailing slash for strncmp checking */
557 		append_trailing_slash(&opt->chroot, opt->region);
558 		append_trailing_slash(&opt->xfrdir, opt->region);
559 		append_trailing_slash(&opt->zonesdir, opt->region);
560 
561 		/* zonesdir must be absolute and within chroot,
562 		 * all other pathnames may be relative to zonesdir */
563 		if (strncmp(opt->zonesdir, opt->chroot, strlen(opt->chroot)) != 0) {
564 			fprintf(stderr, "%s: zonesdir %s is not relative to chroot %s.\n",
565 				filename, opt->zonesdir, opt->chroot);
566 			errors ++;
567                 }
568 		if (!file_inside_chroot(opt->pidfile, opt->chroot)) {
569 			fprintf(stderr, "%s: pidfile %s is not relative to chroot %s.\n",
570 				filename, opt->pidfile, opt->chroot);
571 			errors ++;
572                 }
573 		if (!file_inside_chroot(opt->database, opt->chroot)) {
574 			fprintf(stderr, "%s: database %s is not relative to chroot %s.\n",
575 				filename, opt->database, opt->chroot);
576 			errors ++;
577                 }
578 		if (!file_inside_chroot(opt->xfrdfile, opt->chroot)) {
579 			fprintf(stderr, "%s: xfrdfile %s is not relative to chroot %s.\n",
580 				filename, opt->xfrdfile, opt->chroot);
581 			errors ++;
582                 }
583 		if (!file_inside_chroot(opt->zonelistfile, opt->chroot)) {
584 			fprintf(stderr, "%s: zonelistfile %s is not relative to chroot %s.\n",
585 				filename, opt->zonelistfile, opt->chroot);
586 			errors ++;
587                 }
588 		if (!file_inside_chroot(opt->xfrdir, opt->chroot)) {
589 			fprintf(stderr, "%s: xfrdir %s is not relative to chroot %s.\n",
590 				filename, opt->xfrdir, opt->chroot);
591 			errors ++;
592                 }
593 	}
594 
595 	if (atoi(opt->port) <= 0) {
596 		fprintf(stderr, "%s: port number '%s' is not a positive number.\n",
597 			filename, opt->port);
598 		errors ++;
599 	}
600 	if(errors != 0) {
601 		fprintf(stderr, "%s: %d semantic errors in %d zones, %d keys.\n",
602 			filename, errors, (int)nsd_options_num_zones(opt),
603 			(int)opt->keys->count);
604 	}
605 
606 	return (errors == 0);
607 }
608 
609 int
610 main(int argc, char* argv[])
611 {
612 	int c;
613 	int verbose = 0;
614 	int key_sec = 0;
615 	const char * conf_opt = NULL; /* what option do you want? Can be NULL -> print all */
616 	const char * conf_zone = NULL; /* what zone are we talking about */
617 	const char * conf_key = NULL; /* what key is needed */
618 	const char * conf_pat = NULL; /* what pattern is talked about */
619 	const char* configfile;
620 	nsd_options_t *options;
621 
622 	log_init("nsd-checkconf");
623 
624 	/* Parse the command line... */
625 	while ((c = getopt(argc, argv, "vo:a:p:s:z:")) != -1) {
626 		switch (c) {
627 		case 'v':
628 			verbose = 1;
629 			verbosity++;
630 			break;
631 		case 'o':
632 			conf_opt = optarg;
633 			break;
634 		case 'p':
635 			conf_pat = optarg;
636 			break;
637 		case 'a':
638 			if (conf_key) {
639 				fprintf(stderr, "Error: cannot combine -a with -s or other -a.\n");
640 				exit(1);
641 			}
642 			conf_key = optarg;
643 			break;
644 		case 's':
645 			if (conf_key) {
646 				fprintf(stderr, "Error: cannot combine -s with -a or other -s.\n");
647 				exit(1);
648 			}
649 			conf_key = optarg;
650 			key_sec = 1;
651 			break;
652 		case 'z':
653 			conf_zone = optarg;
654 			break;
655 		default:
656 			usage();
657 		};
658 	}
659         argc -= optind;
660         argv += optind;
661         if (argc == 0 || argc>=2) {
662 		usage();
663 	}
664 	configfile = argv[0];
665 
666 	/* read config file */
667 	options = nsd_options_create(region_create(xalloc, free));
668 	tsig_init(options->region);
669 	if (!parse_options_file(options, configfile, NULL, NULL) ||
670 	   !additional_checks(options, configfile)) {
671 		exit(2);
672 	}
673 	if (conf_opt || conf_key) {
674 		config_print_zone(options, conf_key, key_sec,
675 			underscore(conf_opt), conf_zone, conf_pat);
676 	} else {
677 		if (verbose) {
678 			printf("# Read file %s: %d patterns, %d fixed-zones, "
679 				"%d keys.\n",
680 				configfile,
681 				(int)options->patterns->count,
682 				(int)nsd_options_num_zones(options),
683 				(int)options->keys->count);
684 			config_test_print_server(options);
685 		}
686 	}
687 	return 0;
688 }
689