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