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

"#\n\
Mark Andrews's avatar
Mark Andrews committed
255
#  Zones in the \"_bind\" view are NOT counted in the count of zones.\n\
256
#\n\
257 258
view \"_bind\" chaos {\n\
	recursion no;\n\
259
	notify no;\n\
260
	allow-new-zones no;\n\
Evan Hunt's avatar
Evan Hunt committed
261 262 263 264 265 266 267
\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\
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
\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\
283
\n\
284 285 286 287
	zone \"id.server\" chaos {\n\
		type master;\n\
		database \"_builtin id\";\n\
	};\n\
288
};\n\
289 290
"
"#\n\
Evan Hunt's avatar
Evan Hunt committed
291 292
#  Default trusted key(s), used if \n\
# \"dnssec-validation auto;\" is set and\n\
293 294
#  sysconfdir/bind.keys doesn't exist).\n\
#\n\
295
# BEGIN DNSSEC KEYS\n"
296 297

/* Imported from bind.keys.h: */
298
DNSSEC_KEYS
299

300
"# END MANAGED KEYS\n\
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
\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\
316
";
317 318

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

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

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

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

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

	for (i = 0;; i++) {
		if (maps[i] == NULL)
			return (ISC_R_NOTFOUND);
		checknames = NULL;
357
		if (cfg_map_get(maps[i], "check-names",
Automatic Updater's avatar
Automatic Updater committed
358
				&checknames) == ISC_R_SUCCESS) {
359 360 361 362 363 364 365 366 367 368 369 370
			/*
			 * 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");
371 372
				if (strcasecmp(cfg_obj_asstring(type),
					       which) == 0) {
373 374 375 376 377 378 379 380 381
					*obj = cfg_tuple_get(value, "mode");
					return (ISC_R_SUCCESS);
				}
			}

		}
	}
}

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

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

	return (i);
}

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

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

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

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

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

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

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

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

	addrlist = cfg_tuple_get(list, "addresses");
482
	count = named_config_listcount(addrlist);
483 484 485

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

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

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

517 518 519 520 521 522
	addrs = isc_mem_get(mctx, count * sizeof(isc_sockaddr_t));

	for (element = cfg_list_first(addrlist);
	     element != NULL;
	     element = cfg_list_next(element), i++)
	{
Evan Hunt's avatar
Evan Hunt committed
523
		const cfg_obj_t *addr;
524
		INSIST(i < count);
Evan Hunt's avatar
Evan Hunt committed
525 526 527 528 529 530 531 532 533
		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;
		}
534 535 536 537 538 539 540 541
		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
542 543 544
	if (dscpsp != NULL)
		*dscpsp = dscps;

545 546 547 548
	return (ISC_R_SUCCESS);
}

void
549
named_config_putiplist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
550
		       isc_dscp_t **dscpsp, uint32_t count)
551 552
{
	INSIST(addrsp != NULL && *addrsp != NULL);
Evan Hunt's avatar
Evan Hunt committed
553
	INSIST(dscpsp == NULL || *dscpsp != NULL);
554 555 556

	isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
	*addrsp = NULL;
Evan Hunt's avatar
Evan Hunt committed
557 558 559 560 561

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

564 565 566
isc_result_t
named_config_getmastersdef(const cfg_obj_t *cctx, const char *name,
			   const cfg_obj_t **ret)
567
{
568
	isc_result_t result;
569 570
	const cfg_obj_t *masters = NULL;
	const cfg_listelt_t *elt;
571 572 573 574 575 576 577

	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)) {
578
		const cfg_obj_t *list;
579 580 581 582 583 584 585 586 587 588 589 590 591
		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);
}

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

617
	REQUIRE(ipkl != NULL);
618
	REQUIRE(ipkl->count == 0);
619 620 621
	REQUIRE(ipkl->addrs == NULL);
	REQUIRE(ipkl->keys == NULL);
	REQUIRE(ipkl->dscps == NULL);
622 623
	REQUIRE(ipkl->labels == NULL);
	REQUIRE(ipkl->allocated == 0);
624

625 626 627
	/*
	 * Get system defaults.
	 */
628
	result = named_config_getport(config, &port);
629 630 631
	if (result != ISC_R_SUCCESS)
		goto cleanup;

632
	result = named_config_getdscp(config, &dscp);
633 634 635
	if (result != ISC_R_SUCCESS)
		goto cleanup;

636
 newlist:
637 638
	addrlist = cfg_tuple_get(list, "addresses");
	portobj = cfg_tuple_get(list, "port");
Evan Hunt's avatar
Evan Hunt committed
639 640
	dscpobj = cfg_tuple_get(list, "dscp");

641
	if (cfg_obj_isuint32(portobj)) {
642 643
		uint32_t val = cfg_obj_asuint32(portobj);
		if (val > UINT16_MAX) {
644
			cfg_obj_log(portobj, named_g_lctx, ISC_LOG_ERROR,
645
				    "port '%u' out of range", val);
646 647
			result = ISC_R_RANGE;
			goto cleanup;
648 649 650 651
		}
		port = (in_port_t) val;
	}

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

663 664
	result = ISC_R_NOMEMORY;

665 666 667
	element = cfg_list_first(addrlist);
 resume:
	for ( ;
668
	     element != NULL;
669
	     element = cfg_list_next(element))
670
	{
671 672
		const cfg_obj_t *addr;
		const cfg_obj_t *key;
673
		const char *keystr;
674 675
		isc_buffer_t b;

676 677
		addr = cfg_tuple_get(cfg_listelt_value(element),
				     "masterselement");
678 679
		key = cfg_tuple_get(cfg_listelt_value(element), "key");

680
		if (!cfg_obj_issockaddr(addr)) {
681
			const char *listname = cfg_obj_asstring(addr);
682 683 684 685
			isc_result_t tresult;

			/* Grow lists? */
			if (listcount == l) {
686
				void * tmp;
687
				uint32_t newlen = listcount + 16;
688 689 690 691
				size_t newsize, oldsize;

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

				result = tresult;
				goto cleanup;
			}
			if (tresult != ISC_R_SUCCESS)
				goto cleanup;
Mark Andrews's avatar
Mark Andrews committed
717
			lists[l++].name = listname;
718 719
			/* Grow stack? */
			if (stackcount == pushed) {
720
				void * tmp;
721
				uint32_t newlen = stackcount + 16;
722 723 724 725
				size_t newsize, oldsize;

				newsize = newlen * sizeof(*stack);
				oldsize = stackcount * sizeof(*stack);
726
				tmp = isc_mem_get(mctx, newsize);
727
				if (stackcount != 0) {
728
					memmove(tmp, stack, oldsize);
729 730
					isc_mem_put(mctx, stack, oldsize);
				}
731
				stack = tmp;
732 733 734 735 736 737 738 739
				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
740
			stack[pushed].dscp = dscp;
741 742 743 744 745
			pushed++;
			goto newlist;
		}

		if (i == addrcount) {
746
			void * tmp;
747
			uint32_t newlen = addrcount + 16;
748 749 750 751
			size_t newsize, oldsize;

			newsize = newlen * sizeof(isc_sockaddr_t);
			oldsize = addrcount * sizeof(isc_sockaddr_t);
752
			tmp = isc_mem_get(mctx, newsize);
753
			if (addrcount != 0) {
754
				memmove(tmp, addrs, oldsize);
755 756
				isc_mem_put(mctx, addrs, oldsize);
			}
757
			addrs = tmp;
758 759
			addrcount = newlen;

Evan Hunt's avatar
Evan Hunt committed
760 761
			newsize = newlen * sizeof(isc_dscp_t);
			oldsize = dscpcount * sizeof(isc_dscp_t);
762
			tmp = isc_mem_get(mctx, newsize);
Evan Hunt's avatar
Evan Hunt committed
763
			if (dscpcount != 0) {
764
				memmove(tmp, dscps, oldsize);
Evan Hunt's avatar
Evan Hunt committed
765 766
				isc_mem_put(mctx, dscps, oldsize);
			}
767
			dscps = tmp;
Evan Hunt's avatar
Evan Hunt committed
768 769
			dscpcount = newlen;

770 771
			newsize = newlen * sizeof(dns_name_t *);
			oldsize = keycount * sizeof(dns_name_t *);
772
			tmp = isc_mem_get(mctx, newsize);
773
			if (keycount != 0) {
774
				memmove(tmp, keys, oldsize);
775
				isc_mem_put(mctx, keys, oldsize);
776
			}
777
			keys = tmp;
778 779 780
			keycount = newlen;
		}

781 782 783
		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
784 785 786
		dscps[i] = cfg_obj_getdscp(addr);
		if (dscps[i] == -1)
			dscps[i] = dscp;
787
		keys[i] = NULL;
788 789
		i++;	/* Increment here so that cleanup on error works. */
		if (!cfg_obj_isstring(key))
790
			continue;
791 792
		keys[i - 1] = isc_mem_get(mctx, sizeof(dns_name_t));
		dns_name_init(keys[i - 1], NULL);
Automatic Updater's avatar
Automatic Updater committed
793

794
		keystr = cfg_obj_asstring(key);
795
		isc_buffer_constinit(&b, keystr, strlen(keystr));
796 797 798
		isc_buffer_add(&b, strlen(keystr));
		dns_fixedname_init(&fname);
		result = dns_name_fromtext(dns_fixedname_name(&fname), &b,
799