xref: /minix/external/bsd/bind/dist/bin/named/config.c (revision fb9c64b2)
1 /*	$NetBSD: config.c,v 1.11 2015/07/08 17:28:55 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004-2014  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2001-2003  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*! \file */
21 
22 #include <config.h>
23 
24 #include <stdlib.h>
25 
26 #include <isc/buffer.h>
27 #include <isc/log.h>
28 #include <isc/mem.h>
29 #include <isc/parseint.h>
30 #include <isc/region.h>
31 #include <isc/result.h>
32 #include <isc/sockaddr.h>
33 #include <isc/string.h>
34 #include <isc/util.h>
35 
36 #include <isccfg/namedconf.h>
37 
38 #include <dns/fixedname.h>
39 #include <dns/name.h>
40 #include <dns/rdataclass.h>
41 #include <dns/rdatatype.h>
42 #include <dns/tsig.h>
43 #include <dns/zone.h>
44 
45 #include <dst/dst.h>
46 
47 #include <named/config.h>
48 #include <named/globals.h>
49 
50 #include <bind.keys.h>
51 
52 /*% default configuration */
53 static char defaultconf[] = "\
54 options {\n\
55 	automatic-interface-scan yes;\n\
56 #	blackhole {none;};\n"
57 #ifndef WIN32
58 "	coresize default;\n\
59 	datasize default;\n\
60 	files unlimited;\n\
61 	stacksize default;\n"
62 #endif
63 "#	session-keyfile \"" NS_LOCALSTATEDIR "/run/named/session.key\";\n\
64 	session-keyname local-ddns;\n\
65 	session-keyalg hmac-sha256;\n\
66 	deallocate-on-exit true;\n\
67 #	directory <none>\n\
68 	dump-file \"named_dump.db\";\n\
69 	fake-iquery no;\n\
70 	has-old-clients false;\n\
71 	heartbeat-interval 60;\n\
72 	host-statistics no;\n\
73 	interface-interval 60;\n\
74 	listen-on {any;};\n\
75 	listen-on-v6 {any;};\n\
76 	match-mapped-addresses no;\n\
77 	max-rsa-exponent-size 0; /* no limit */\n\
78 	memstatistics-file \"named.memstats\";\n\
79 	multiple-cnames no;\n\
80 #	named-xfer <obsolete>;\n\
81 #	pid-file \"" NS_LOCALSTATEDIR "/run/named/named.pid\"; /* or /lwresd.pid */\n\
82 	bindkeys-file \"" NS_SYSCONFDIR "/bind.keys\";\n\
83 	port 53;\n\
84 	prefetch 2 9;\n\
85 	recursing-file \"named.recursing\";\n\
86 	secroots-file \"named.secroots\";\n\
87 "
88 #ifdef PATH_RANDOMDEV
89 "\
90 	random-device \"" PATH_RANDOMDEV "\";\n\
91 "
92 #endif
93 "\
94 	recursive-clients 1000;\n\
95 	resolver-query-timeout 10;\n\
96 	rrset-order { order random; };\n\
97 	serial-queries 20;\n\
98 	serial-query-rate 20;\n\
99 	server-id none;\n\
100 	statistics-file \"named.stats\";\n\
101 	statistics-interval 60;\n\
102 	tcp-clients 100;\n\
103 	tcp-listen-queue 10;\n\
104 #	tkey-dhkey <none>\n\
105 #	tkey-gssapi-credential <none>\n\
106 #	tkey-domain <none>\n\
107 	transfers-per-ns 2;\n\
108 	transfers-in 10;\n\
109 	transfers-out 10;\n\
110 	treat-cr-as-space true;\n\
111 	use-id-pool true;\n\
112 	use-ixfr true;\n\
113 	edns-udp-size 4096;\n\
114 	max-udp-size 4096;\n\
115 "
116 #ifdef ISC_PLATFORM_USESIT
117 "\
118 	nosit-udp-size 4096;\n\
119 	request-sit true;\n\
120 "
121 #endif
122 "\
123 	request-nsid false;\n\
124 	reserved-sockets 512;\n\
125 \n\
126 	/* DLV */\n\
127 	dnssec-lookaside . trust-anchor dlv.isc.org;\n\
128 \n\
129 	/* view */\n\
130 	allow-notify {none;};\n\
131 	allow-update-forwarding {none;};\n\
132 	allow-query-cache { localnets; localhost; };\n\
133 	allow-query-cache-on { any; };\n\
134 	allow-recursion { localnets; localhost; };\n\
135 	allow-recursion-on { any; };\n\
136 #	allow-v6-synthesis <obsolete>;\n\
137 #	sortlist <none>\n\
138 #	topology <none>\n\
139 	auth-nxdomain false;\n\
140 	minimal-responses false;\n\
141 	recursion true;\n\
142 	provide-ixfr true;\n\
143 	request-ixfr true;\n\
144 	fetch-glue no;\n\
145 	rfc2308-type1 no;\n\
146 	additional-from-auth true;\n\
147 	additional-from-cache true;\n\
148 	query-source address *;\n\
149 	query-source-v6 address *;\n\
150 	notify-source *;\n\
151 	notify-source-v6 *;\n\
152 	cleaning-interval 0;  /* now meaningless */\n\
153 	min-roots 2;\n\
154 	lame-ttl 600;\n\
155 	max-ncache-ttl 10800; /* 3 hours */\n\
156 	max-cache-ttl 604800; /* 1 week */\n\
157 	transfer-format many-answers;\n\
158 	max-cache-size 0;\n\
159 	check-names master fail;\n\
160 	check-names slave warn;\n\
161 	check-names response ignore;\n\
162 	check-dup-records warn;\n\
163 	check-mx warn;\n\
164 	check-spf warn;\n\
165 	acache-enable no;\n\
166 	acache-cleaning-interval 60;\n\
167 	max-acache-size 16M;\n\
168 	dnssec-enable yes;\n\
169 	dnssec-validation yes; \n\
170 	dnssec-accept-expired no;\n\
171 	clients-per-query 10;\n\
172 	max-clients-per-query 100;\n\
173 	max-recursion-depth 7;\n\
174 	max-recursion-queries 75;\n\
175 	zero-no-soa-ttl-cache no;\n\
176 	nsec3-test-zone no;\n\
177 	allow-new-zones no;\n\
178 "
179 #ifdef ALLOW_FILTER_AAAA
180 "	filter-aaaa-on-v4 no;\n\
181 	filter-aaaa-on-v6 no;\n\
182 	filter-aaaa { any; };\n\
183 "
184 #endif
185 
186 "	/* zone */\n\
187 	allow-query {any;};\n\
188 	allow-query-on {any;};\n\
189 	allow-transfer {any;};\n\
190 	notify yes;\n\
191 #	also-notify <none>\n\
192 	notify-delay 5;\n\
193 	notify-to-soa no;\n\
194 	dialup no;\n\
195 #	forward <none>\n\
196 #	forwarders <none>\n\
197 	maintain-ixfr-base no;\n\
198 #	max-ixfr-log-size <obsolete>\n\
199 	transfer-source *;\n\
200 	transfer-source-v6 *;\n\
201 	alt-transfer-source *;\n\
202 	alt-transfer-source-v6 *;\n\
203 	max-transfer-time-in 120;\n\
204 	max-transfer-time-out 120;\n\
205 	max-transfer-idle-in 60;\n\
206 	max-transfer-idle-out 60;\n\
207 	max-retry-time 1209600; /* 2 weeks */\n\
208 	min-retry-time 500;\n\
209 	max-refresh-time 2419200; /* 4 weeks */\n\
210 	min-refresh-time 300;\n\
211 	multi-master no;\n\
212 	dnssec-secure-to-insecure no;\n\
213 	sig-validity-interval 30; /* days */\n\
214 	sig-signing-nodes 100;\n\
215 	sig-signing-signatures 10;\n\
216 	sig-signing-type 65534;\n\
217 	inline-signing no;\n\
218 	zone-statistics terse;\n\
219 	max-journal-size unlimited;\n\
220 	ixfr-from-differences false;\n\
221 	check-wildcard yes;\n\
222 	check-sibling yes;\n\
223 	check-integrity yes;\n\
224 	check-mx-cname warn;\n\
225 	check-srv-cname warn;\n\
226 	zero-no-soa-ttl yes;\n\
227 	update-check-ksk yes;\n\
228 	serial-update-method increment;\n\
229 	dnssec-update-mode maintain;\n\
230 	dnssec-dnskey-kskonly no;\n\
231 	dnssec-loadkeys-interval 60;\n\
232 	try-tcp-refresh yes; /* BIND 8 compat */\n\
233 };\n\
234 "
235 
236 "#\n\
237 #  Zones in the \"_bind\" view are NOT counted in the count of zones.\n\
238 #\n\
239 view \"_bind\" chaos {\n\
240 	recursion no;\n\
241 	notify no;\n\
242 	allow-new-zones no;\n\
243 \n\
244 	# Prevent use of this zone in DNS amplified reflection DoS attacks\n\
245 	rate-limit {\n\
246 		responses-per-second 3;\n\
247 		slip 0;\n\
248 		min-table-size 10;\n\
249 	};\n\
250 \n\
251 	zone \"version.bind\" chaos {\n\
252 		type master;\n\
253 		database \"_builtin version\";\n\
254 	};\n\
255 \n\
256 	zone \"hostname.bind\" chaos {\n\
257 		type master;\n\
258 		database \"_builtin hostname\";\n\
259 	};\n\
260 \n\
261 	zone \"authors.bind\" chaos {\n\
262 		type master;\n\
263 		database \"_builtin authors\";\n\
264 	};\n\
265 \n\
266 	zone \"id.server\" chaos {\n\
267 		type master;\n\
268 		database \"_builtin id\";\n\
269 	};\n\
270 };\n\
271 "
272 "#\n\
273 #  Default trusted key(s) for builtin DLV support\n\
274 #  (used if \"dnssec-lookaside auto;\" is set and\n\
275 #  sysconfdir/bind.keys doesn't exist).\n\
276 #\n\
277 # BEGIN MANAGED KEYS\n"
278 
279 /* Imported from bind.keys.h: */
280 MANAGED_KEYS
281 
282 "# END MANAGED KEYS\n\
283 ";
284 
285 isc_result_t
286 ns_config_parsedefaults(cfg_parser_t *parser, cfg_obj_t **conf) {
287 	isc_buffer_t b;
288 
289 	isc_buffer_init(&b, defaultconf, sizeof(defaultconf) - 1);
290 	isc_buffer_add(&b, sizeof(defaultconf) - 1);
291 	return (cfg_parse_buffer(parser, &b, &cfg_type_namedconf, conf));
292 }
293 
294 isc_result_t
295 ns_config_get(cfg_obj_t const * const *maps, const char *name,
296 	      const cfg_obj_t **obj)
297 {
298 	int i;
299 
300 	for (i = 0;; i++) {
301 		if (maps[i] == NULL)
302 			return (ISC_R_NOTFOUND);
303 		if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
304 			return (ISC_R_SUCCESS);
305 	}
306 }
307 
308 isc_result_t
309 ns_checknames_get(const cfg_obj_t **maps, const char *which,
310 		  const cfg_obj_t **obj)
311 {
312 	const cfg_listelt_t *element;
313 	const cfg_obj_t *checknames;
314 	const cfg_obj_t *type;
315 	const cfg_obj_t *value;
316 	int i;
317 
318 	for (i = 0;; i++) {
319 		if (maps[i] == NULL)
320 			return (ISC_R_NOTFOUND);
321 		checknames = NULL;
322 		if (cfg_map_get(maps[i], "check-names",
323 				&checknames) == ISC_R_SUCCESS) {
324 			/*
325 			 * Zone map entry is not a list.
326 			 */
327 			if (checknames != NULL && !cfg_obj_islist(checknames)) {
328 				*obj = checknames;
329 				return (ISC_R_SUCCESS);
330 			}
331 			for (element = cfg_list_first(checknames);
332 			     element != NULL;
333 			     element = cfg_list_next(element)) {
334 				value = cfg_listelt_value(element);
335 				type = cfg_tuple_get(value, "type");
336 				if (strcasecmp(cfg_obj_asstring(type),
337 					       which) == 0) {
338 					*obj = cfg_tuple_get(value, "mode");
339 					return (ISC_R_SUCCESS);
340 				}
341 			}
342 
343 		}
344 	}
345 }
346 
347 int
348 ns_config_listcount(const cfg_obj_t *list) {
349 	const cfg_listelt_t *e;
350 	int i = 0;
351 
352 	for (e = cfg_list_first(list); e != NULL; e = cfg_list_next(e))
353 		i++;
354 
355 	return (i);
356 }
357 
358 isc_result_t
359 ns_config_getclass(const cfg_obj_t *classobj, dns_rdataclass_t defclass,
360 		   dns_rdataclass_t *classp) {
361 	isc_textregion_t r;
362 	isc_result_t result;
363 
364 	if (!cfg_obj_isstring(classobj)) {
365 		*classp = defclass;
366 		return (ISC_R_SUCCESS);
367 	}
368 	DE_CONST(cfg_obj_asstring(classobj), r.base);
369 	r.length = strlen(r.base);
370 	result = dns_rdataclass_fromtext(classp, &r);
371 	if (result != ISC_R_SUCCESS)
372 		cfg_obj_log(classobj, ns_g_lctx, ISC_LOG_ERROR,
373 			    "unknown class '%s'", r.base);
374 	return (result);
375 }
376 
377 isc_result_t
378 ns_config_gettype(const cfg_obj_t *typeobj, dns_rdatatype_t deftype,
379 		   dns_rdatatype_t *typep) {
380 	isc_textregion_t r;
381 	isc_result_t result;
382 
383 	if (!cfg_obj_isstring(typeobj)) {
384 		*typep = deftype;
385 		return (ISC_R_SUCCESS);
386 	}
387 	DE_CONST(cfg_obj_asstring(typeobj), r.base);
388 	r.length = strlen(r.base);
389 	result = dns_rdatatype_fromtext(typep, &r);
390 	if (result != ISC_R_SUCCESS)
391 		cfg_obj_log(typeobj, ns_g_lctx, ISC_LOG_ERROR,
392 			    "unknown type '%s'", r.base);
393 	return (result);
394 }
395 
396 dns_zonetype_t
397 ns_config_getzonetype(const cfg_obj_t *zonetypeobj) {
398 	dns_zonetype_t ztype = dns_zone_none;
399 	const char *str;
400 
401 	str = cfg_obj_asstring(zonetypeobj);
402 	if (strcasecmp(str, "master") == 0)
403 		ztype = dns_zone_master;
404 	else if (strcasecmp(str, "slave") == 0)
405 		ztype = dns_zone_slave;
406 	else if (strcasecmp(str, "stub") == 0)
407 		ztype = dns_zone_stub;
408 	else if (strcasecmp(str, "static-stub") == 0)
409 		ztype = dns_zone_staticstub;
410 	else if (strcasecmp(str, "redirect") == 0)
411 		ztype = dns_zone_redirect;
412 	else
413 		INSIST(0);
414 	return (ztype);
415 }
416 
417 isc_result_t
418 ns_config_getiplist(const cfg_obj_t *config, const cfg_obj_t *list,
419 		    in_port_t defport, isc_mem_t *mctx,
420 		    isc_sockaddr_t **addrsp, isc_dscp_t **dscpsp,
421 		    isc_uint32_t *countp)
422 {
423 	int count, i = 0;
424 	const cfg_obj_t *addrlist;
425 	const cfg_obj_t *portobj, *dscpobj;
426 	const cfg_listelt_t *element;
427 	isc_sockaddr_t *addrs;
428 	in_port_t port;
429 	isc_dscp_t dscp = -1, *dscps = NULL;
430 	isc_result_t result;
431 
432 	INSIST(addrsp != NULL && *addrsp == NULL);
433 	INSIST(dscpsp == NULL || *dscpsp == NULL);
434 	INSIST(countp != NULL);
435 
436 	addrlist = cfg_tuple_get(list, "addresses");
437 	count = ns_config_listcount(addrlist);
438 
439 	portobj = cfg_tuple_get(list, "port");
440 	if (cfg_obj_isuint32(portobj)) {
441 		isc_uint32_t val = cfg_obj_asuint32(portobj);
442 		if (val > ISC_UINT16_MAX) {
443 			cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
444 				    "port '%u' out of range", val);
445 			return (ISC_R_RANGE);
446 		}
447 		port = (in_port_t) val;
448 	} else if (defport != 0)
449 		port = defport;
450 	else {
451 		result = ns_config_getport(config, &port);
452 		if (result != ISC_R_SUCCESS)
453 			return (result);
454 	}
455 
456 	if (dscpsp != NULL) {
457 		dscps = isc_mem_get(mctx, count * sizeof(isc_dscp_t));
458 		if (dscps == NULL)
459 			return (ISC_R_NOMEMORY);
460 
461 		dscpobj = cfg_tuple_get(list, "dscp");
462 		if (dscpobj != NULL && cfg_obj_isuint32(dscpobj)) {
463 			if (cfg_obj_asuint32(dscpobj) > 63) {
464 				cfg_obj_log(dscpobj, ns_g_lctx, ISC_LOG_ERROR,
465 					    "dscp value '%u' is out of range",
466 					    cfg_obj_asuint32(dscpobj));
467 				return (ISC_R_RANGE);
468 			}
469 			dscp = (isc_dscp_t)cfg_obj_asuint32(dscpobj);
470 		}
471 	}
472 
473 	addrs = isc_mem_get(mctx, count * sizeof(isc_sockaddr_t));
474 	if (addrs == NULL)
475 		return (ISC_R_NOMEMORY);
476 
477 	for (element = cfg_list_first(addrlist);
478 	     element != NULL;
479 	     element = cfg_list_next(element), i++)
480 	{
481 		const cfg_obj_t *addr;
482 		INSIST(i < count);
483 		addr = cfg_listelt_value(element);
484 		addrs[i] = *cfg_obj_assockaddr(addr);
485 		if (dscpsp != NULL) {
486 			isc_dscp_t innerdscp;
487 			innerdscp = cfg_obj_getdscp(addr);
488 			if (innerdscp == -1)
489 				innerdscp = dscp;
490 			dscps[i] = innerdscp;
491 		}
492 		if (isc_sockaddr_getport(&addrs[i]) == 0)
493 			isc_sockaddr_setport(&addrs[i], port);
494 	}
495 	INSIST(i == count);
496 
497 	*addrsp = addrs;
498 	*countp = count;
499 
500 	if (dscpsp != NULL)
501 		*dscpsp = dscps;
502 
503 	return (ISC_R_SUCCESS);
504 }
505 
506 void
507 ns_config_putiplist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
508 		    isc_dscp_t **dscpsp, isc_uint32_t count)
509 {
510 	INSIST(addrsp != NULL && *addrsp != NULL);
511 	INSIST(dscpsp == NULL || *dscpsp != NULL);
512 
513 	isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
514 	*addrsp = NULL;
515 
516 	if (dscpsp != NULL) {
517 		isc_mem_put(mctx, *dscpsp, count * sizeof(isc_dscp_t));
518 		*dscpsp = NULL;
519 	}
520 }
521 
522 static isc_result_t
523 get_masters_def(const cfg_obj_t *cctx, const char *name,
524 		const cfg_obj_t **ret)
525 {
526 	isc_result_t result;
527 	const cfg_obj_t *masters = NULL;
528 	const cfg_listelt_t *elt;
529 
530 	result = cfg_map_get(cctx, "masters", &masters);
531 	if (result != ISC_R_SUCCESS)
532 		return (result);
533 	for (elt = cfg_list_first(masters);
534 	     elt != NULL;
535 	     elt = cfg_list_next(elt)) {
536 		const cfg_obj_t *list;
537 		const char *listname;
538 
539 		list = cfg_listelt_value(elt);
540 		listname = cfg_obj_asstring(cfg_tuple_get(list, "name"));
541 
542 		if (strcasecmp(listname, name) == 0) {
543 			*ret = list;
544 			return (ISC_R_SUCCESS);
545 		}
546 	}
547 	return (ISC_R_NOTFOUND);
548 }
549 
550 isc_result_t
551 ns_config_getipandkeylist(const cfg_obj_t *config, const cfg_obj_t *list,
552 			  isc_mem_t *mctx, isc_sockaddr_t **addrsp,
553 			  isc_dscp_t **dscpsp, dns_name_t ***keysp,
554 			  isc_uint32_t *countp)
555 {
556 	isc_uint32_t addrcount = 0, dscpcount = 0, keycount = 0, i = 0;
557 	isc_uint32_t listcount = 0, l = 0, j;
558 	isc_uint32_t stackcount = 0, pushed = 0;
559 	isc_result_t result;
560 	const cfg_listelt_t *element;
561 	const cfg_obj_t *addrlist;
562 	const cfg_obj_t *portobj;
563 	const cfg_obj_t *dscpobj;
564 	in_port_t port;
565 	isc_dscp_t dscp;
566 	dns_fixedname_t fname;
567 	isc_sockaddr_t *addrs = NULL;
568 	isc_dscp_t *dscps = NULL;
569 	dns_name_t **keys = NULL;
570 	struct { const char *name; } *lists = NULL;
571 	struct {
572 		const cfg_listelt_t *element;
573 		in_port_t port;
574 		isc_dscp_t dscp;
575 	} *stack = NULL;
576 
577 	REQUIRE(addrsp != NULL && *addrsp == NULL);
578 	REQUIRE(dscpsp != NULL && *dscpsp == NULL);
579 	REQUIRE(keysp != NULL && *keysp == NULL);
580 	REQUIRE(countp != NULL);
581 
582 	/*
583 	 * Get system defaults.
584 	 */
585 	result = ns_config_getport(config, &port);
586 	if (result != ISC_R_SUCCESS)
587 		goto cleanup;
588 
589 	result = ns_config_getdscp(config, &dscp);
590 	if (result != ISC_R_SUCCESS)
591 		goto cleanup;
592 
593  newlist:
594 	addrlist = cfg_tuple_get(list, "addresses");
595 	portobj = cfg_tuple_get(list, "port");
596 	dscpobj = cfg_tuple_get(list, "dscp");
597 
598 	if (cfg_obj_isuint32(portobj)) {
599 		isc_uint32_t val = cfg_obj_asuint32(portobj);
600 		if (val > ISC_UINT16_MAX) {
601 			cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
602 				    "port '%u' out of range", val);
603 			result = ISC_R_RANGE;
604 			goto cleanup;
605 		}
606 		port = (in_port_t) val;
607 	}
608 
609 	if (dscpobj != NULL && cfg_obj_isuint32(dscpobj)) {
610 		if (cfg_obj_asuint32(dscpobj) > 63) {
611 			cfg_obj_log(dscpobj, ns_g_lctx, ISC_LOG_ERROR,
612 				    "dscp value '%u' is out of range",
613 				    cfg_obj_asuint32(dscpobj));
614 			return (ISC_R_RANGE);
615 		}
616 		dscp = (isc_dscp_t)cfg_obj_asuint32(dscpobj);
617 	}
618 
619 	result = ISC_R_NOMEMORY;
620 
621 	element = cfg_list_first(addrlist);
622  resume:
623 	for ( ;
624 	     element != NULL;
625 	     element = cfg_list_next(element))
626 	{
627 		const cfg_obj_t *addr;
628 		const cfg_obj_t *key;
629 		const char *keystr;
630 		isc_buffer_t b;
631 
632 		addr = cfg_tuple_get(cfg_listelt_value(element),
633 				     "masterselement");
634 		key = cfg_tuple_get(cfg_listelt_value(element), "key");
635 
636 		if (!cfg_obj_issockaddr(addr)) {
637 			const char *listname = cfg_obj_asstring(addr);
638 			isc_result_t tresult;
639 
640 			/* Grow lists? */
641 			if (listcount == l) {
642 				void * new;
643 				isc_uint32_t newlen = listcount + 16;
644 				size_t newsize, oldsize;
645 
646 				newsize = newlen * sizeof(*lists);
647 				oldsize = listcount * sizeof(*lists);
648 				new = isc_mem_get(mctx, newsize);
649 				if (new == NULL)
650 					goto cleanup;
651 				if (listcount != 0) {
652 					memmove(new, lists, oldsize);
653 					isc_mem_put(mctx, lists, oldsize);
654 				}
655 				lists = new;
656 				listcount = newlen;
657 			}
658 			/* Seen? */
659 			for (j = 0; j < l; j++)
660 				if (strcasecmp(lists[j].name, listname) == 0)
661 					break;
662 			if (j < l)
663 				continue;
664 			tresult = get_masters_def(config, listname, &list);
665 			if (tresult == ISC_R_NOTFOUND) {
666 				cfg_obj_log(addr, ns_g_lctx, ISC_LOG_ERROR,
667 				    "masters \"%s\" not found", listname);
668 
669 				result = tresult;
670 				goto cleanup;
671 			}
672 			if (tresult != ISC_R_SUCCESS)
673 				goto cleanup;
674 			lists[l++].name = listname;
675 			/* Grow stack? */
676 			if (stackcount == pushed) {
677 				void * new;
678 				isc_uint32_t newlen = stackcount + 16;
679 				size_t newsize, oldsize;
680 
681 				newsize = newlen * sizeof(*stack);
682 				oldsize = stackcount * sizeof(*stack);
683 				new = isc_mem_get(mctx, newsize);
684 				if (new == NULL)
685 					goto cleanup;
686 				if (stackcount != 0) {
687 					memmove(new, stack, oldsize);
688 					isc_mem_put(mctx, stack, oldsize);
689 				}
690 				stack = new;
691 				stackcount = newlen;
692 			}
693 			/*
694 			 * We want to resume processing this list on the
695 			 * next element.
696 			 */
697 			stack[pushed].element = cfg_list_next(element);
698 			stack[pushed].port = port;
699 			stack[pushed].dscp = dscp;
700 			pushed++;
701 			goto newlist;
702 		}
703 
704 		if (i == addrcount) {
705 			void * new;
706 			isc_uint32_t newlen = addrcount + 16;
707 			size_t newsize, oldsize;
708 
709 			newsize = newlen * sizeof(isc_sockaddr_t);
710 			oldsize = addrcount * sizeof(isc_sockaddr_t);
711 			new = isc_mem_get(mctx, newsize);
712 			if (new == NULL)
713 				goto cleanup;
714 			if (addrcount != 0) {
715 				memmove(new, addrs, oldsize);
716 				isc_mem_put(mctx, addrs, oldsize);
717 			}
718 			addrs = new;
719 			addrcount = newlen;
720 
721 			newsize = newlen * sizeof(isc_dscp_t);
722 			oldsize = dscpcount * sizeof(isc_dscp_t);
723 			new = isc_mem_get(mctx, newsize);
724 			if (new == NULL)
725 				goto cleanup;
726 			if (dscpcount != 0) {
727 				memmove(new, dscps, oldsize);
728 				isc_mem_put(mctx, dscps, oldsize);
729 			}
730 			dscps = new;
731 			dscpcount = newlen;
732 
733 			newsize = newlen * sizeof(dns_name_t *);
734 			oldsize = keycount * sizeof(dns_name_t *);
735 			new = isc_mem_get(mctx, newsize);
736 			if (new == NULL)
737 				goto cleanup;
738 			if (keycount != 0) {
739 				memmove(new, keys, oldsize);
740 				isc_mem_put(mctx, keys, oldsize);
741 			}
742 			keys = new;
743 			keycount = newlen;
744 		}
745 
746 		addrs[i] = *cfg_obj_assockaddr(addr);
747 		if (isc_sockaddr_getport(&addrs[i]) == 0)
748 			isc_sockaddr_setport(&addrs[i], port);
749 		dscps[i] = cfg_obj_getdscp(addr);
750 		if (dscps[i] == -1)
751 			dscps[i] = dscp;
752 		keys[i] = NULL;
753 		i++;	/* Increment here so that cleanup on error works. */
754 		if (!cfg_obj_isstring(key))
755 			continue;
756 		keys[i - 1] = isc_mem_get(mctx, sizeof(dns_name_t));
757 		if (keys[i - 1] == NULL)
758 			goto cleanup;
759 		dns_name_init(keys[i - 1], NULL);
760 
761 		keystr = cfg_obj_asstring(key);
762 		isc_buffer_constinit(&b, keystr, strlen(keystr));
763 		isc_buffer_add(&b, strlen(keystr));
764 		dns_fixedname_init(&fname);
765 		result = dns_name_fromtext(dns_fixedname_name(&fname), &b,
766 					   dns_rootname, 0, NULL);
767 		if (result != ISC_R_SUCCESS)
768 			goto cleanup;
769 		result = dns_name_dup(dns_fixedname_name(&fname), mctx,
770 				      keys[i - 1]);
771 		if (result != ISC_R_SUCCESS)
772 			goto cleanup;
773 	}
774 	if (pushed != 0) {
775 		pushed--;
776 		element = stack[pushed].element;
777 		port = stack[pushed].port;
778 		dscp = stack[pushed].dscp;
779 		goto resume;
780 	}
781 	if (i < addrcount) {
782 		void * new;
783 		size_t newsize, oldsize;
784 
785 		newsize = i * sizeof(isc_sockaddr_t);
786 		oldsize = addrcount * sizeof(isc_sockaddr_t);
787 		if (i != 0) {
788 			new = isc_mem_get(mctx, newsize);
789 			if (new == NULL)
790 				goto cleanup;
791 			memmove(new, addrs, newsize);
792 		} else
793 			new = NULL;
794 		isc_mem_put(mctx, addrs, oldsize);
795 		addrs = new;
796 		addrcount = i;
797 
798 		newsize = i * sizeof(isc_dscp_t);
799 		oldsize = dscpcount * sizeof(isc_dscp_t);
800 		if (i != 0) {
801 			new = isc_mem_get(mctx, newsize);
802 			if (new == NULL)
803 				goto cleanup;
804 			memmove(new, dscps, newsize);
805 		} else
806 			new = NULL;
807 		isc_mem_put(mctx, dscps, oldsize);
808 		dscps = new;
809 		dscpcount = i;
810 
811 		newsize = i * sizeof(dns_name_t *);
812 		oldsize = keycount * sizeof(dns_name_t *);
813 		if (i != 0) {
814 			new = isc_mem_get(mctx, newsize);
815 			if (new == NULL)
816 				goto cleanup;
817 			memmove(new, keys,  newsize);
818 		} else
819 			new = NULL;
820 		isc_mem_put(mctx, keys, oldsize);
821 		keys = new;
822 		keycount = i;
823 	}
824 
825 	if (lists != NULL)
826 		isc_mem_put(mctx, lists, listcount * sizeof(*lists));
827 	if (stack != NULL)
828 		isc_mem_put(mctx, stack, stackcount * sizeof(*stack));
829 
830 	INSIST(keycount == addrcount);
831 
832 	*addrsp = addrs;
833 	*dscpsp = dscps;
834 	*keysp = keys;
835 	*countp = addrcount;
836 
837 	return (ISC_R_SUCCESS);
838 
839  cleanup:
840 	if (addrs != NULL)
841 		isc_mem_put(mctx, addrs, addrcount * sizeof(isc_sockaddr_t));
842 	if (dscps != NULL)
843 		isc_mem_put(mctx, dscps, dscpcount * sizeof(isc_dscp_t));
844 	if (keys != NULL) {
845 		for (j = 0; j < i; j++) {
846 			if (keys[j] == NULL)
847 				continue;
848 			if (dns_name_dynamic(keys[j]))
849 				dns_name_free(keys[j], mctx);
850 			isc_mem_put(mctx, keys[j], sizeof(dns_name_t));
851 		}
852 		isc_mem_put(mctx, keys, keycount * sizeof(dns_name_t *));
853 	}
854 	if (lists != NULL)
855 		isc_mem_put(mctx, lists, listcount * sizeof(*lists));
856 	if (stack != NULL)
857 		isc_mem_put(mctx, stack, stackcount * sizeof(*stack));
858 	return (result);
859 }
860 
861 void
862 ns_config_putipandkeylist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
863 			  isc_dscp_t **dscpsp, dns_name_t ***keysp,
864 			  isc_uint32_t count)
865 {
866 	unsigned int i;
867 	dns_name_t **keys;
868 
869 	REQUIRE(addrsp != NULL && *addrsp != NULL);
870 	REQUIRE(dscpsp == NULL || *dscpsp != NULL);
871 	REQUIRE(keysp != NULL && *keysp != NULL);
872 
873 	keys = *keysp;
874 
875 	isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
876 	if (dscpsp != NULL)
877 		isc_mem_put(mctx, *dscpsp, count * sizeof(isc_dscp_t));
878 	for (i = 0; i < count; i++) {
879 		if (keys[i] == NULL)
880 			continue;
881 		if (dns_name_dynamic(keys[i]))
882 			dns_name_free(keys[i], mctx);
883 		isc_mem_put(mctx, keys[i], sizeof(dns_name_t));
884 	}
885 	isc_mem_put(mctx, *keysp, count * sizeof(dns_name_t *));
886 	*addrsp = NULL;
887 	if (dscpsp != NULL)
888 		*dscpsp = NULL;
889 	*keysp = NULL;
890 }
891 
892 isc_result_t
893 ns_config_getport(const cfg_obj_t *config, in_port_t *portp) {
894 	const cfg_obj_t *maps[3];
895 	const cfg_obj_t *options = NULL;
896 	const cfg_obj_t *portobj = NULL;
897 	isc_result_t result;
898 	int i;
899 
900 	(void)cfg_map_get(config, "options", &options);
901 	i = 0;
902 	if (options != NULL)
903 		maps[i++] = options;
904 	maps[i++] = ns_g_defaults;
905 	maps[i] = NULL;
906 
907 	result = ns_config_get(maps, "port", &portobj);
908 	INSIST(result == ISC_R_SUCCESS);
909 	if (cfg_obj_asuint32(portobj) >= ISC_UINT16_MAX) {
910 		cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
911 			    "port '%u' out of range",
912 			    cfg_obj_asuint32(portobj));
913 		return (ISC_R_RANGE);
914 	}
915 	*portp = (in_port_t)cfg_obj_asuint32(portobj);
916 	return (ISC_R_SUCCESS);
917 }
918 
919 isc_result_t
920 ns_config_getdscp(const cfg_obj_t *config, isc_dscp_t *dscpp) {
921 	const cfg_obj_t *options = NULL;
922 	const cfg_obj_t *dscpobj = NULL;
923 	isc_result_t result;
924 
925 	(void)cfg_map_get(config, "options", &options);
926 	if (options == NULL)
927 		return (ISC_R_SUCCESS);
928 
929 	result = cfg_map_get(options, "dscp", &dscpobj);
930 	if (result != ISC_R_SUCCESS || dscpobj == NULL) {
931 		*dscpp = -1;
932 		return (ISC_R_SUCCESS);
933 	}
934 	if (cfg_obj_asuint32(dscpobj) >= 64) {
935 		cfg_obj_log(dscpobj, ns_g_lctx, ISC_LOG_ERROR,
936 			    "dscp '%u' out of range",
937 			    cfg_obj_asuint32(dscpobj));
938 		return (ISC_R_RANGE);
939 	}
940 	*dscpp = (isc_dscp_t)cfg_obj_asuint32(dscpobj);
941 	return (ISC_R_SUCCESS);
942 }
943 
944 struct keyalgorithms {
945 	const char *str;
946 	enum { hmacnone, hmacmd5, hmacsha1, hmacsha224,
947 	       hmacsha256, hmacsha384, hmacsha512 } hmac;
948 	unsigned int type;
949 	isc_uint16_t size;
950 } algorithms[] = {
951 	{ "hmac-md5", hmacmd5, DST_ALG_HMACMD5, 128 },
952 	{ "hmac-md5.sig-alg.reg.int", hmacmd5, DST_ALG_HMACMD5, 0 },
953 	{ "hmac-md5.sig-alg.reg.int.", hmacmd5, DST_ALG_HMACMD5, 0 },
954 	{ "hmac-sha1", hmacsha1, DST_ALG_HMACSHA1, 160 },
955 	{ "hmac-sha224", hmacsha224, DST_ALG_HMACSHA224, 224 },
956 	{ "hmac-sha256", hmacsha256, DST_ALG_HMACSHA256, 256 },
957 	{ "hmac-sha384", hmacsha384, DST_ALG_HMACSHA384, 384 },
958 	{ "hmac-sha512", hmacsha512, DST_ALG_HMACSHA512, 512 },
959 	{  NULL, hmacnone, DST_ALG_UNKNOWN, 0 }
960 };
961 
962 isc_result_t
963 ns_config_getkeyalgorithm(const char *str, dns_name_t **name,
964 			  isc_uint16_t *digestbits)
965 {
966 	return (ns_config_getkeyalgorithm2(str, name, NULL, digestbits));
967 }
968 
969 isc_result_t
970 ns_config_getkeyalgorithm2(const char *str, dns_name_t **name,
971 			   unsigned int *typep, isc_uint16_t *digestbits)
972 {
973 	int i;
974 	size_t len = 0;
975 	isc_uint16_t bits;
976 	isc_result_t result;
977 
978 	for (i = 0; algorithms[i].str != NULL; i++) {
979 		len = strlen(algorithms[i].str);
980 		if (strncasecmp(algorithms[i].str, str, len) == 0 &&
981 		    (str[len] == '\0' ||
982 		     (algorithms[i].size != 0 && str[len] == '-')))
983 			break;
984 	}
985 	if (algorithms[i].str == NULL)
986 		return (ISC_R_NOTFOUND);
987 	if (str[len] == '-') {
988 		result = isc_parse_uint16(&bits, str + len + 1, 10);
989 		if (result != ISC_R_SUCCESS)
990 			return (result);
991 		if (bits > algorithms[i].size)
992 			return (ISC_R_RANGE);
993 	} else if (algorithms[i].size == 0)
994 		bits = 128;
995 	else
996 		bits = algorithms[i].size;
997 
998 	if (name != NULL) {
999 		switch (algorithms[i].hmac) {
1000 		case hmacmd5: *name = dns_tsig_hmacmd5_name; break;
1001 		case hmacsha1: *name = dns_tsig_hmacsha1_name; break;
1002 		case hmacsha224: *name = dns_tsig_hmacsha224_name; break;
1003 		case hmacsha256: *name = dns_tsig_hmacsha256_name; break;
1004 		case hmacsha384: *name = dns_tsig_hmacsha384_name; break;
1005 		case hmacsha512: *name = dns_tsig_hmacsha512_name; break;
1006 		default:
1007 			INSIST(0);
1008 		}
1009 	}
1010 	if (typep != NULL)
1011 		*typep = algorithms[i].type;
1012 	if (digestbits != NULL)
1013 		*digestbits = bits;
1014 	return (ISC_R_SUCCESS);
1015 }
1016