config.c 26 KB
Newer Older
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
10 11
 */

12
/*! \file */
Brian Wellington's avatar
Brian Wellington committed
13

14
#include <inttypes.h>
15 16 17 18 19
#include <stdlib.h>

#include <isc/buffer.h>
#include <isc/log.h>
#include <isc/mem.h>
20
#include <isc/parseint.h>
21 22 23
#include <isc/region.h>
#include <isc/result.h>
#include <isc/sockaddr.h>
Evan Hunt's avatar
Evan Hunt committed
24
#include <isc/string.h>
25 26
#include <isc/util.h>

27 28
#include <pk11/site.h>

29
#include <isccfg/grammar.h>
30
#include <isccfg/namedconf.h>
31 32 33 34

#include <dns/fixedname.h>
#include <dns/name.h>
#include <dns/rdataclass.h>
35
#include <dns/rdatatype.h>
Brian Wellington's avatar
Brian Wellington committed
36
#include <dns/tsig.h>
37 38
#include <dns/zone.h>

39 40
#include <dst/dst.h>

41 42 43
#include <named/config.h>
#include <named/globals.h>

Evan Hunt's avatar
Evan Hunt committed
44
#include <bind.keys.h>
45

46
/*% default configuration */
47 48
static char defaultconf[] = "\
options {\n\
49
	answer-cookie true;\n\
50
	automatic-interface-scan yes;\n\
51
	bindkeys-file \"" NAMED_SYSCONFDIR "/bind.keys\";\n\
52
#	blackhole {none;};\n"
53
"	cookie-algorithm aes;\n"
54 55
#ifndef WIN32
"	coresize default;\n\
56
	datasize default;\n"
57
#endif
58
"\
59
#	deallocate-on-exit <obsolete>;\n\
60 61
#	directory <none>\n\
	dump-file \"named_dump.db\";\n\
62 63 64 65 66 67
	edns-udp-size 4096;\n\
#	fake-iquery <obsolete>;\n"
#ifndef WIN32
"	files unlimited;\n"
#endif
"\
68
#	has-old-clients <obsolete>;\n\
69
	heartbeat-interval 60;\n\
70
#	host-statistics <obsolete>;\n\
71
	interface-interval 60;\n\
72
#	keep-response-order {none;};\n\
73
	listen-on {any;};\n\
74
	listen-on-v6 {any;};\n\
75
#	lock-file \"" NAMED_LOCALSTATEDIR "/run/named/named.lock\";\n\
76
	match-mapped-addresses no;\n\
77
	max-rsa-exponent-size 0; /* no limit */\n\
78
	max-udp-size 4096;\n\
79
	memstatistics-file \"named.memstats\";\n\
80
#	multiple-cnames <obsolete>;\n\
81
#	named-xfer <obsolete>;\n\
82 83
	nocookie-udp-size 4096;\n\
	notify-rate 20;\n\
Evan Hunt's avatar
Evan Hunt committed
84 85
	nta-lifetime 3600;\n\
	nta-recheck 300;\n\
86
#	pid-file \"" NAMED_LOCALSTATEDIR "/run/named/named.pid\"; \n\
87
	port 53;\n\
88 89
	prefetch 2 9;\n\
	recursing-file \"named.recursing\";\n\
90
	recursive-clients 1000;\n\
91 92
	request-nsid false;\n\
	reserved-sockets 512;\n\
93
	resolver-query-timeout 10;\n\
94
	rrset-order { order random; };\n\
95 96
	secroots-file \"named.secroots\";\n\
	send-cookie true;\n\
97
#	serial-queries <obsolete>;\n\
98
	serial-query-rate 20;\n\
99
	server-id none;\n\
100
	session-keyalg hmac-sha256;\n\
101
#	session-keyfile \"" NAMED_LOCALSTATEDIR "/run/named/session.key\";\n\
102 103 104 105 106
	session-keyname local-ddns;\n"
#ifndef WIN32
"	stacksize default;\n"
#endif
"	startup-notify-rate 20;\n\
107
	statistics-file \"named.stats\";\n\
108
#	statistics-interval <obsolete>;\n\
109
	tcp-advertised-timeout 300;\n\
110
	tcp-clients 150;\n\
111 112 113
	tcp-idle-timeout 300;\n\
	tcp-initial-timeout 300;\n\
	tcp-keepalive-timeout 300;\n\
114
	tcp-listen-queue 10;\n\
115 116
#	tkey-dhkey <none>\n\
#	tkey-domain <none>\n\
117
#	tkey-gssapi-credential <none>\n\
118
	transfer-message-size 20480;\n\
119 120
	transfers-in 10;\n\
	transfers-out 10;\n\
121
	transfers-per-ns 2;\n\
122
#	treat-cr-as-space <obsolete>;\n\
123
	trust-anchor-telemetry yes;\n\
124
#	use-id-pool <obsolete>;\n\
125
#	use-ixfr <obsolete>;\n\
126 127
\n\
	/* view */\n\
128
	allow-new-zones no;\n\
129
	allow-notify {none;};\n\
130
	allow-query-cache { localnets; localhost; };\n\
131
	allow-query-cache-on { any; };\n\
132
	allow-recursion { localnets; localhost; };\n\
133
	allow-recursion-on { any; };\n\
134
	allow-update-forwarding {none;};\n\
135
#	allow-v6-synthesis <obsolete>;\n\
136
	auth-nxdomain false;\n\
137
	check-dup-records warn;\n\
138
	check-mx warn;\n\
139 140 141
	check-names master fail;\n\
	check-names response ignore;\n\
	check-names slave warn;\n\
142
	check-spf warn;\n\
143 144
	clients-per-query 10;\n\
	dnssec-accept-expired no;\n\
145
	dnssec-validation " VALIDATION_DEFAULT "; \n"
146 147 148 149
#ifdef HAVE_DNSTAP
"	dnstap-identity hostname;\n"
#endif
"\
150
#	fetch-glue <obsolete>;\n\
Evan Hunt's avatar
Evan Hunt committed
151
	fetch-quota-params 100 0.1 0.3 0.7;\n\
152
	fetches-per-server 0;\n\
153
	fetches-per-zone 0;\n"
154 155 156 157 158
#ifdef HAVE_GEOIP
"	geoip-use-ecs yes;\n"
#endif
"	glue-cache yes;\n\
	lame-ttl 600;\n"
159
#ifdef HAVE_LMDB
160
"	lmdb-mapsize 32M;\n"
161
#endif
162
"	max-cache-size 90%;\n\
163 164 165 166 167
	max-cache-ttl 604800; /* 1 week */\n\
	max-clients-per-query 100;\n\
	max-ncache-ttl 10800; /* 3 hours */\n\
	max-recursion-depth 7;\n\
	max-recursion-queries 75;\n\
168
	max-stale-ttl 604800; /* 1 week */\n\
169
	message-compression yes;\n\
170 171
	min-ncache-ttl 0; /* 0 hours */\n\
	min-cache-ttl 0; /* 0 seconds */\n\
172 173
#	min-roots <obsolete>;\n\
	minimal-any false;\n\
174
	minimal-responses no-auth-recursive;\n\
175 176 177 178
	notify-source *;\n\
	notify-source-v6 *;\n\
	nsec3-test-zone no;\n\
	provide-ixfr true;\n\
179
	qname-minimization relaxed;\n\
180 181 182 183 184
	query-source address *;\n\
	query-source-v6 address *;\n\
	recursion true;\n\
	request-expire true;\n\
	request-ixfr true;\n\
185
	require-server-cookie no;\n\
186 187
	resolver-nonbackoff-tries 3;\n\
	resolver-retry-interval 800; /* in milliseconds */\n\
188
#	rfc2308-type1 <obsolete>;\n\
189
	root-key-sentinel yes;\n\
190 191
	servfail-ttl 1;\n\
#	sortlist <none>\n\
192 193
	stale-answer-enable false;\n\
	stale-answer-ttl 1; /* 1 second */\n\
194 195 196
	synth-from-dnssec yes;\n\
#	topology <none>\n\
	transfer-format many-answers;\n\
197
	v6-bias 50;\n\
198
	zero-no-soa-ttl-cache no;\n\
199 200
\n\
	/* zone */\n\
201
	allow-query {any;};\n\
202
	allow-query-on {any;};\n\
203 204
	allow-transfer {any;};\n\
#	also-notify <none>\n\
205 206 207 208 209 210 211
	alt-transfer-source *;\n\
	alt-transfer-source-v6 *;\n\
	check-integrity yes;\n\
	check-mx-cname warn;\n\
	check-sibling yes;\n\
	check-srv-cname warn;\n\
	check-wildcard yes;\n\
212
	dialup no;\n\
213 214 215 216
	dnssec-dnskey-kskonly no;\n\
	dnssec-loadkeys-interval 60;\n\
	dnssec-secure-to-insecure no;\n\
	dnssec-update-mode maintain;\n\
217 218
#	forward <none>\n\
#	forwarders <none>\n\
219 220
	inline-signing no;\n\
	ixfr-from-differences false;\n\
221
#	maintain-ixfr-base <obsolete>;\n\
222
#	max-ixfr-log-size <obsolete>\n\
223
	max-journal-size default;\n\
224
	max-records 0;\n\
225
	max-refresh-time 2419200; /* 4 weeks */\n\
226 227 228 229 230
	max-retry-time 1209600; /* 2 weeks */\n\
	max-transfer-idle-in 60;\n\
	max-transfer-idle-out 60;\n\
	max-transfer-time-in 120;\n\
	max-transfer-time-out 120;\n\
231
	min-refresh-time 300;\n\
232
	min-retry-time 500;\n\
233
	multi-master no;\n\
234 235 236 237
	notify yes;\n\
	notify-delay 5;\n\
	notify-to-soa no;\n\
	serial-update-method increment;\n\
238 239
	sig-signing-nodes 100;\n\
	sig-signing-signatures 10;\n\
240
	sig-signing-type 65534;\n\
241
	sig-validity-interval 30; /* days */\n\
Evan Hunt's avatar
Evan Hunt committed
242
	dnskey-sig-validity 0; /* default: sig-validity-interval */\n\
243 244
	transfer-source *;\n\
	transfer-source-v6 *;\n\
245
	try-tcp-refresh yes; /* BIND 8 compat */\n\
246 247 248
	update-check-ksk yes;\n\
	zero-no-soa-ttl yes;\n\
	zone-statistics terse;\n\
249
};\n\
Mark Andrews's avatar
Mark Andrews committed
250 251 252
"

"#\n\
Mark Andrews's avatar
Mark Andrews committed
253
#  Zones in the \"_bind\" view are NOT counted in the count of zones.\n\
254
#\n\
255 256
view \"_bind\" chaos {\n\
	recursion no;\n\
257
	notify no;\n\
258
	allow-new-zones no;\n\
Evan Hunt's avatar
Evan Hunt committed
259 260 261 262 263 264 265
\n\
	# Prevent use of this zone in DNS amplified reflection DoS attacks\n\
	rate-limit {\n\
		responses-per-second 3;\n\
		slip 0;\n\
		min-table-size 10;\n\
	};\n\
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
\n\
	zone \"version.bind\" chaos {\n\
		type master;\n\
		database \"_builtin version\";\n\
	};\n\
\n\
	zone \"hostname.bind\" chaos {\n\
		type master;\n\
		database \"_builtin hostname\";\n\
	};\n\
\n\
	zone \"authors.bind\" chaos {\n\
		type master;\n\
		database \"_builtin authors\";\n\
	};\n\
281
\n\
282 283 284 285
	zone \"id.server\" chaos {\n\
		type master;\n\
		database \"_builtin id\";\n\
	};\n\
286
};\n\
287 288
"
"#\n\
Evan Hunt's avatar
Evan Hunt committed
289 290
#  Default trusted key(s), used if \n\
# \"dnssec-validation auto;\" is set and\n\
291 292
#  sysconfdir/bind.keys doesn't exist).\n\
#\n\
293
# BEGIN MANAGED KEYS\n"
294 295

/* Imported from bind.keys.h: */
296
MANAGED_KEYS
297

298
"# END MANAGED KEYS\n\
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
\n\
masters " DEFAULT_IANA_ROOT_ZONE_MASTERS " {\n\
	2001:500:84::b;		# b.root-servers.net\n\
	2001:500:2f::f;		# f.root-servers.net\n\
	2001:7fd::1;		# k.root-servers.net\n\
	2620:0:2830:202::132;	# xfr.cjr.dns.icann.org\n\
	2620:0:2d0:202::132;	# xfr.lax.dns.icann.org\n\
	192.228.79.201;		# b.root-servers.net\n\
	192.33.4.12;		# c.root-servers.net\n\
	192.5.5.241;		# f.root-servers.net\n\
	192.112.36.4;		# g.root-servers.net\n\
	193.0.14.129;		# k.root-servers.net\n\
	192.0.47.132;		# xfr.cjr.dns.icann.org\n\
	192.0.32.132;		# xfr.lax.dns.icann.org\n\
};\n\
314
";
315 316

isc_result_t
317
named_config_parsedefaults(cfg_parser_t *parser, cfg_obj_t **conf) {
318 319 320 321
	isc_buffer_t b;

	isc_buffer_init(&b, defaultconf, sizeof(defaultconf) - 1);
	isc_buffer_add(&b, sizeof(defaultconf) - 1);
322 323 324
	return (cfg_parse_buffer(parser, &b, __FILE__, 0,
				 &cfg_type_namedconf,
				 CFG_PCTX_NODEPRECATED, conf));
325 326 327
}

isc_result_t
328 329
named_config_get(cfg_obj_t const * const *maps, const char *name,
		 const cfg_obj_t **obj)
Evan Hunt's avatar
Evan Hunt committed
330
{
331 332
	int i;

333
	for (i = 0;; i++) {
334 335 336 337 338 339 340
		if (maps[i] == NULL)
			return (ISC_R_NOTFOUND);
		if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
			return (ISC_R_SUCCESS);
	}
}

341
isc_result_t
342 343
named_checknames_get(const cfg_obj_t **maps, const char *which,
		     const cfg_obj_t **obj)
344 345 346 347 348
{
	const cfg_listelt_t *element;
	const cfg_obj_t *checknames;
	const cfg_obj_t *type;
	const cfg_obj_t *value;
349 350 351 352 353 354
	int i;

	for (i = 0;; i++) {
		if (maps[i] == NULL)
			return (ISC_R_NOTFOUND);
		checknames = NULL;
355
		if (cfg_map_get(maps[i], "check-names",
Automatic Updater's avatar
Automatic Updater committed
356
				&checknames) == ISC_R_SUCCESS) {
357 358 359 360 361 362 363 364 365 366 367 368
			/*
			 * Zone map entry is not a list.
			 */
			if (checknames != NULL && !cfg_obj_islist(checknames)) {
				*obj = checknames;
				return (ISC_R_SUCCESS);
			}
			for (element = cfg_list_first(checknames);
			     element != NULL;
			     element = cfg_list_next(element)) {
				value = cfg_listelt_value(element);
				type = cfg_tuple_get(value, "type");
369 370
				if (strcasecmp(cfg_obj_asstring(type),
					       which) == 0) {
371 372 373 374 375 376 377 378 379
					*obj = cfg_tuple_get(value, "mode");
					return (ISC_R_SUCCESS);
				}
			}

		}
	}
}

380
int
381
named_config_listcount(const cfg_obj_t *list) {
382
	const cfg_listelt_t *e;
383 384 385 386 387 388 389 390 391
	int i = 0;

	for (e = cfg_list_first(list); e != NULL; e = cfg_list_next(e))
		i++;

	return (i);
}

isc_result_t
392 393 394
named_config_getclass(const cfg_obj_t *classobj, dns_rdataclass_t defclass,
		      dns_rdataclass_t *classp)
{
395
	isc_textregion_t r;
396
	isc_result_t result;
397 398

	if (!cfg_obj_isstring(classobj)) {
399
		*classp = defclass;
400 401
		return (ISC_R_SUCCESS);
	}
402 403
	DE_CONST(cfg_obj_asstring(classobj), r.base);
	r.length = strlen(r.base);
404 405
	result = dns_rdataclass_fromtext(classp, &r);
	if (result != ISC_R_SUCCESS)
406
		cfg_obj_log(classobj, named_g_lctx, ISC_LOG_ERROR,
407
			    "unknown class '%s'", r.base);
408
	return (result);
409 410
}

411
isc_result_t
412 413 414
named_config_gettype(const cfg_obj_t *typeobj, dns_rdatatype_t deftype,
		     dns_rdatatype_t *typep)
{
415 416 417 418 419 420 421
	isc_textregion_t r;
	isc_result_t result;

	if (!cfg_obj_isstring(typeobj)) {
		*typep = deftype;
		return (ISC_R_SUCCESS);
	}
422 423
	DE_CONST(cfg_obj_asstring(typeobj), r.base);
	r.length = strlen(r.base);
424 425
	result = dns_rdatatype_fromtext(typep, &r);
	if (result != ISC_R_SUCCESS)
426
		cfg_obj_log(typeobj, named_g_lctx, ISC_LOG_ERROR,
427
			    "unknown type '%s'", r.base);
428 429 430
	return (result);
}

431
dns_zonetype_t
432
named_config_getzonetype(const cfg_obj_t *zonetypeobj) {
433
	dns_zonetype_t ztype = dns_zone_none;
434
	const char *str;
435 436

	str = cfg_obj_asstring(zonetypeobj);
437 438 439
	if (strcasecmp(str, "primary") == 0 ||
	    strcasecmp(str, "master") == 0)
	{
440
		ztype = dns_zone_master;
441 442 443
	} else if (strcasecmp(str, "secondary") == 0 ||
		   strcasecmp(str, "slave") == 0)
	{
444
		ztype = dns_zone_slave;
445 446
	} else if (strcasecmp(str, "mirror") == 0) {
		ztype = dns_zone_mirror;
447
	} else if (strcasecmp(str, "stub") == 0) {
448
		ztype = dns_zone_stub;
449
	} else if (strcasecmp(str, "static-stub") == 0) {
450
		ztype = dns_zone_staticstub;
451
	} else if (strcasecmp(str, "redirect") == 0) {
452
		ztype = dns_zone_redirect;
453
	} else {
454
		INSIST(0);
455
		ISC_UNREACHABLE();
456
	}
457 458 459 460
	return (ztype);
}

isc_result_t
461
named_config_getiplist(const cfg_obj_t *config, const cfg_obj_t *list,
462
		    in_port_t defport, isc_mem_t *mctx,
Evan Hunt's avatar
Evan Hunt committed
463
		    isc_sockaddr_t **addrsp, isc_dscp_t **dscpsp,
464
		    uint32_t *countp)
465 466
{
	int count, i = 0;
467
	const cfg_obj_t *addrlist;
Evan Hunt's avatar
Evan Hunt committed
468
	const cfg_obj_t *portobj, *dscpobj;
469
	const cfg_listelt_t *element;
470 471
	isc_sockaddr_t *addrs;
	in_port_t port;
Mark Andrews's avatar
Mark Andrews committed
472
	isc_dscp_t dscp = -1, *dscps = NULL;
473 474 475
	isc_result_t result;

	INSIST(addrsp != NULL && *addrsp == NULL);
Evan Hunt's avatar
Evan Hunt committed
476
	INSIST(dscpsp == NULL || *dscpsp == NULL);
477
	INSIST(countp != NULL);
478 479

	addrlist = cfg_tuple_get(list, "addresses");
480
	count = named_config_listcount(addrlist);
481 482 483

	portobj = cfg_tuple_get(list, "port");
	if (cfg_obj_isuint32(portobj)) {
484 485
		uint32_t val = cfg_obj_asuint32(portobj);
		if (val > UINT16_MAX) {
486
			cfg_obj_log(portobj, named_g_lctx, ISC_LOG_ERROR,
487 488 489 490 491 492 493
				    "port '%u' out of range", val);
			return (ISC_R_RANGE);
		}
		port = (in_port_t) val;
	} else if (defport != 0)
		port = defport;
	else {
494
		result = named_config_getport(config, &port);
495 496 497 498
		if (result != ISC_R_SUCCESS)
			return (result);
	}

Evan Hunt's avatar
Evan Hunt committed
499 500 501 502
	if (dscpsp != NULL) {
		dscpobj = cfg_tuple_get(list, "dscp");
		if (dscpobj != NULL && cfg_obj_isuint32(dscpobj)) {
			if (cfg_obj_asuint32(dscpobj) > 63) {
503 504
				cfg_obj_log(dscpobj, named_g_lctx,
					    ISC_LOG_ERROR,
Evan Hunt's avatar
Evan Hunt committed
505 506 507 508 509 510
					    "dscp value '%u' is out of range",
					    cfg_obj_asuint32(dscpobj));
				return (ISC_R_RANGE);
			}
			dscp = (isc_dscp_t)cfg_obj_asuint32(dscpobj);
		}
511 512 513 514

		dscps = isc_mem_get(mctx, count * sizeof(isc_dscp_t));
		if (dscps == NULL)
			return (ISC_R_NOMEMORY);
Evan Hunt's avatar
Evan Hunt committed
515 516
	}

517
	addrs = isc_mem_get(mctx, count * sizeof(isc_sockaddr_t));
518 519 520
	if (addrs == NULL) {
		if (dscps != NULL)
			isc_mem_put(mctx, dscps, count * sizeof(isc_dscp_t));
521
		return (ISC_R_NOMEMORY);
522
	}
523 524 525 526 527

	for (element = cfg_list_first(addrlist);
	     element != NULL;
	     element = cfg_list_next(element), i++)
	{
Evan Hunt's avatar
Evan Hunt committed
528
		const cfg_obj_t *addr;
529
		INSIST(i < count);
Evan Hunt's avatar
Evan Hunt committed
530 531 532 533 534 535 536 537 538
		addr = cfg_listelt_value(element);
		addrs[i] = *cfg_obj_assockaddr(addr);
		if (dscpsp != NULL) {
			isc_dscp_t innerdscp;
			innerdscp = cfg_obj_getdscp(addr);
			if (innerdscp == -1)
				innerdscp = dscp;
			dscps[i] = innerdscp;
		}
539 540 541 542 543 544 545 546
		if (isc_sockaddr_getport(&addrs[i]) == 0)
			isc_sockaddr_setport(&addrs[i], port);
	}
	INSIST(i == count);

	*addrsp = addrs;
	*countp = count;

Evan Hunt's avatar
Evan Hunt committed
547 548 549
	if (dscpsp != NULL)
		*dscpsp = dscps;

550 551 552 553
	return (ISC_R_SUCCESS);
}

void
554
named_config_putiplist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
555
		       isc_dscp_t **dscpsp, uint32_t count)
556 557
{
	INSIST(addrsp != NULL && *addrsp != NULL);
Evan Hunt's avatar
Evan Hunt committed
558
	INSIST(dscpsp == NULL || *dscpsp != NULL);
559 560 561

	isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
	*addrsp = NULL;
Evan Hunt's avatar
Evan Hunt committed
562 563 564 565 566

	if (dscpsp != NULL) {
		isc_mem_put(mctx, *dscpsp, count * sizeof(isc_dscp_t));
		*dscpsp = NULL;
	}
567 568
}

569 570 571
isc_result_t
named_config_getmastersdef(const cfg_obj_t *cctx, const char *name,
			   const cfg_obj_t **ret)
572
{
573
	isc_result_t result;
574 575
	const cfg_obj_t *masters = NULL;
	const cfg_listelt_t *elt;
576 577 578 579 580 581 582

	result = cfg_map_get(cctx, "masters", &masters);
	if (result != ISC_R_SUCCESS)
		return (result);
	for (elt = cfg_list_first(masters);
	     elt != NULL;
	     elt = cfg_list_next(elt)) {
583
		const cfg_obj_t *list;
584 585 586 587 588 589 590 591 592 593 594 595 596
		const char *listname;

		list = cfg_listelt_value(elt);
		listname = cfg_obj_asstring(cfg_tuple_get(list, "name"));

		if (strcasecmp(listname, name) == 0) {
			*ret = list;
			return (ISC_R_SUCCESS);
		}
	}
	return (ISC_R_NOTFOUND);
}

597
isc_result_t
598
named_config_getipandkeylist(const cfg_obj_t *config, const cfg_obj_t *list,
599
			  isc_mem_t *mctx, dns_ipkeylist_t *ipkl)
600
{
601 602 603
	uint32_t addrcount = 0, dscpcount = 0, keycount = 0, i = 0;
	uint32_t listcount = 0, l = 0, j;
	uint32_t stackcount = 0, pushed = 0;
604
	isc_result_t result;
605 606 607
	const cfg_listelt_t *element;
	const cfg_obj_t *addrlist;
	const cfg_obj_t *portobj;
Evan Hunt's avatar
Evan Hunt committed
608
	const cfg_obj_t *dscpobj;
609
	in_port_t port;
610
	isc_dscp_t dscp = -1;
611 612
	dns_fixedname_t fname;
	isc_sockaddr_t *addrs = NULL;
Evan Hunt's avatar
Evan Hunt committed
613
	isc_dscp_t *dscps = NULL;
614
	dns_name_t **keys = NULL;
Mark Andrews's avatar
Mark Andrews committed
615
	struct { const char *name; } *lists = NULL;
616
	struct {
617
		const cfg_listelt_t *element;
618
		in_port_t port;
Evan Hunt's avatar
Evan Hunt committed
619
		isc_dscp_t dscp;
620
	} *stack = NULL;
621

622
	REQUIRE(ipkl != NULL);
623
	REQUIRE(ipkl->count == 0);
624 625 626
	REQUIRE(ipkl->addrs == NULL);
	REQUIRE(ipkl->keys == NULL);
	REQUIRE(ipkl->dscps == NULL);
627 628
	REQUIRE(ipkl->labels == NULL);
	REQUIRE(ipkl->allocated == 0);
629

630 631 632
	/*
	 * Get system defaults.
	 */
633
	result = named_config_getport(config, &port);
634 635 636
	if (result != ISC_R_SUCCESS)
		goto cleanup;

637
	result = named_config_getdscp(config, &dscp);
638 639 640
	if (result != ISC_R_SUCCESS)
		goto cleanup;

641
 newlist:
642 643
	addrlist = cfg_tuple_get(list, "addresses");
	portobj = cfg_tuple_get(list, "port");
Evan Hunt's avatar
Evan Hunt committed
644 645
	dscpobj = cfg_tuple_get(list, "dscp");

646
	if (cfg_obj_isuint32(portobj)) {
647 648
		uint32_t val = cfg_obj_asuint32(portobj);
		if (val > UINT16_MAX) {
649
			cfg_obj_log(portobj, named_g_lctx, ISC_LOG_ERROR,
650
				    "port '%u' out of range", val);
651 652
			result = ISC_R_RANGE;
			goto cleanup;
653 654 655 656
		}
		port = (in_port_t) val;
	}

Evan Hunt's avatar
Evan Hunt committed
657 658
	if (dscpobj != NULL && cfg_obj_isuint32(dscpobj)) {
		if (cfg_obj_asuint32(dscpobj) > 63) {
659
			cfg_obj_log(dscpobj, named_g_lctx, ISC_LOG_ERROR,
Evan Hunt's avatar
Evan Hunt committed
660 661
				    "dscp value '%u' is out of range",
				    cfg_obj_asuint32(dscpobj));
662 663
			result = ISC_R_RANGE;
			goto cleanup;
Evan Hunt's avatar
Evan Hunt committed
664 665 666 667
		}
		dscp = (isc_dscp_t)cfg_obj_asuint32(dscpobj);
	}

668 669
	result = ISC_R_NOMEMORY;

670 671 672
	element = cfg_list_first(addrlist);
 resume:
	for ( ;
673
	     element != NULL;
674
	     element = cfg_list_next(element))
675
	{
676 677
		const cfg_obj_t *addr;
		const cfg_obj_t *key;
678
		const char *keystr;
679 680
		isc_buffer_t b;

681 682
		addr = cfg_tuple_get(cfg_listelt_value(element),
				     "masterselement");
683 684
		key = cfg_tuple_get(cfg_listelt_value(element), "key");

685
		if (!cfg_obj_issockaddr(addr)) {
686
			const char *listname = cfg_obj_asstring(addr);
687 688 689 690
			isc_result_t tresult;

			/* Grow lists? */
			if (listcount == l) {
691
				void * tmp;
692
				uint32_t newlen = listcount + 16;
693 694 695 696
				size_t newsize, oldsize;

				newsize = newlen * sizeof(*lists);
				oldsize = listcount * sizeof(*lists);
697 698
				tmp = isc_mem_get(mctx, newsize);
				if (tmp == NULL)
699 700
					goto cleanup;
				if (listcount != 0) {
701
					memmove(tmp, lists, oldsize);
702 703
					isc_mem_put(mctx, lists, oldsize);
				}
704
				lists = tmp;
705 706 707 708
				listcount = newlen;
			}
			/* Seen? */
			for (j = 0; j < l; j++)
Mark Andrews's avatar
Mark Andrews committed
709
				if (strcasecmp(lists[j].name, listname) == 0)
710 711 712
					break;
			if (j < l)
				continue;
713 714
			tresult = named_config_getmastersdef(config, listname,
							     &list);
715
			if (tresult == ISC_R_NOTFOUND) {
716
				cfg_obj_log(addr, named_g_lctx, ISC_LOG_ERROR,
Automatic Updater's avatar
Automatic Updater committed
717
				    "masters \"%s\" not found", listname);
718 719 720 721 722 723

				result = tresult;
				goto cleanup;
			}
			if (tresult != ISC_R_SUCCESS)
				goto cleanup;
Mark Andrews's avatar
Mark Andrews committed
724
			lists[l++].name = listname;
725 726
			/* Grow stack? */
			if (stackcount == pushed) {
727
				void * tmp;
728
				uint32_t newlen = stackcount + 16;
729 730 731 732
				size_t newsize, oldsize;

				newsize = newlen * sizeof(*stack);
				oldsize = stackcount * sizeof(*stack);
733 734
				tmp = isc_mem_get(mctx, newsize);
				if (tmp == NULL)
735 736
					goto cleanup;
				if (stackcount != 0) {
737
					memmove(tmp, stack, oldsize);
738 739
					isc_mem_put(mctx, stack, oldsize);
				}
740
				stack = tmp;
741 742 743 744 745 746 747 748
				stackcount = newlen;
			}
			/*
			 * We want to resume processing this list on the
			 * next element.
			 */
			stack[pushed].element = cfg_list_next(element);
			stack[pushed].port = port;
Evan Hunt's avatar
Evan Hunt committed
749
			stack[pushed].dscp = dscp;
750 751 752 753 754
			pushed++;
			goto newlist;
		}

		if (i == addrcount) {
755
			void * tmp;
756
			uint32_t newlen = addrcount + 16;
757 758 759 760
			size_t newsize, oldsize;

			newsize = newlen * sizeof(isc_sockaddr_t);
			oldsize = addrcount * sizeof(isc_sockaddr_t);
761 762
			tmp = isc_mem_get(mctx, newsize);
			if (tmp == NULL)
763 764
				goto cleanup;
			if (addrcount != 0) {
765
				memmove(tmp, addrs, oldsize);
766 767
				isc_mem_put(mctx, addrs, oldsize);
			}
768
			addrs = tmp;
769 770
			addrcount = newlen;

Evan Hunt's avatar
Evan Hunt committed
771 772
			newsize = newlen * sizeof(isc_dscp_t);
			oldsize = dscpcount * sizeof(isc_dscp_t);
773 774
			tmp = isc_mem_get(mctx, newsize);
			if (tmp == NULL)
Evan Hunt's avatar
Evan Hunt committed
775 776
				goto cleanup;
			if (dscpcount != 0) {
777
				memmove(tmp, dscps, oldsize);
Evan Hunt's avatar
Evan Hunt committed
778 779
				isc_mem_put(mctx, dscps, oldsize);
			}
780
			dscps = tmp;
Evan Hunt's avatar
Evan Hunt committed
781 782
			dscpcount = newlen;

783 784
			newsize = newlen * sizeof(dns_name_t *);
			oldsize = keycount * sizeof(dns_name_t *);
785 786
			tmp = isc_mem_get(mctx, newsize);
			if (tmp == NULL)
787 788
				goto cleanup;
			if (keycount != 0) {
789
				memmove(tmp, keys, oldsize);
790
				isc_mem_put(mctx, keys, oldsize);
791
			}
792
			keys = tmp;
793 794 795
			keycount = newlen;
		}

796 797 798
		addrs[i] = *cfg_obj_assockaddr(addr);
		if (isc_sockaddr_getport(&addrs[i]) == 0)
			isc_sockaddr_setport(&addrs[i], port);
Evan Hunt's avatar
Evan Hunt committed
799 800 801
		dscps[i] = cfg_obj_getdscp(addr);
		if (dscps[i] == -1)
			dscps[i] = dscp;
802
		keys[i] = NULL;
803 804
		i++;	/* Increment here so that cleanup on error works. */
		if (!cfg_obj_isstring(key))
805
			continue;
806 807
		keys[i - 1] = isc_mem_get(mctx, sizeof(dns_name_t));
		if (keys[i - 1] == NULL)
808
			goto cleanup;
809
		dns_name_init(keys[i - 1], NULL);
Automatic Updater's avatar
Automatic Updater committed