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 <config.h>
15

16
#include <inttypes.h>
17 18 19 20 21
#include <stdlib.h>

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

29 30
#include <pk11/site.h>

31
#include <isccfg/grammar.h>
32
#include <isccfg/namedconf.h>
33 34 35 36

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

41 42
#include <dst/dst.h>

43 44 45
#include <named/config.h>
#include <named/globals.h>

Evan Hunt's avatar
Evan Hunt committed
46
#include <bind.keys.h>
47

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

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

/* Imported from bind.keys.h: */
301
MANAGED_KEYS
302

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

isc_result_t
322
named_config_parsedefaults(cfg_parser_t *parser, cfg_obj_t **conf) {
323 324 325 326
	isc_buffer_t b;

	isc_buffer_init(&b, defaultconf, sizeof(defaultconf) - 1);
	isc_buffer_add(&b, sizeof(defaultconf) - 1);
327 328 329
	return (cfg_parse_buffer4(parser, &b, __FILE__, 0,
				  &cfg_type_namedconf,
				  CFG_PCTX_NODEPRECATED, conf));
330 331 332
}

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

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

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

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

		}
	}
}

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

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

	return (i);
}

isc_result_t
397 398 399
named_config_getclass(const cfg_obj_t *classobj, dns_rdataclass_t defclass,
		      dns_rdataclass_t *classp)
{
400
	isc_textregion_t r;
401
	isc_result_t result;
402 403

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

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

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

436
dns_zonetype_t
437
named_config_getzonetype(const cfg_obj_t *zonetypeobj) {
438
	dns_zonetype_t ztype = dns_zone_none;
439
	const char *str;
440 441

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

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

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

	addrlist = cfg_tuple_get(list, "addresses");
484
	count = named_config_listcount(addrlist);
485 486 487

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

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

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

521
	addrs = isc_mem_get(mctx, count * sizeof(isc_sockaddr_t));
522 523 524
	if (addrs == NULL) {
		if (dscps != NULL)
			isc_mem_put(mctx, dscps, count * sizeof(isc_dscp_t));
525
		return (ISC_R_NOMEMORY);
526
	}
527 528 529 530 531

	for (element = cfg_list_first(addrlist);
	     element != NULL;
	     element = cfg_list_next(element), i++)
	{
Evan Hunt's avatar
Evan Hunt committed
532
		const cfg_obj_t *addr;
533
		INSIST(i < count);
Evan Hunt's avatar
Evan Hunt committed
534 535 536 537 538 539 540 541 542
		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;
		}
543 544 545 546 547 548 549 550
		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
551 552 553
	if (dscpsp != NULL)
		*dscpsp = dscps;

554 555 556 557
	return (ISC_R_SUCCESS);
}

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

	isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
	*addrsp = NULL;
Evan Hunt's avatar
Evan Hunt committed
566 567 568 569 570

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

573 574 575
isc_result_t
named_config_getmastersdef(const cfg_obj_t *cctx, const char *name,
			   const cfg_obj_t **ret)
576
{
577
	isc_result_t result;
578 579
	const cfg_obj_t *masters = NULL;
	const cfg_listelt_t *elt;
580 581 582 583 584 585 586

	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)) {
587
		const cfg_obj_t *list;
588 589 590 591 592 593 594 595 596 597 598 599 600
		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);
}

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

626
	REQUIRE(ipkl != NULL);
627
	REQUIRE(ipkl->count == 0);
628 629 630
	REQUIRE(ipkl->addrs == NULL);
	REQUIRE(ipkl->keys == NULL);
	REQUIRE(ipkl->dscps == NULL);
631 632
	REQUIRE(ipkl->labels == NULL);
	REQUIRE(ipkl->allocated == 0);
633

634 635 636
	/*
	 * Get system defaults.
	 */
637
	result = named_config_getport(config, &port);
638 639 640
	if (result != ISC_R_SUCCESS)
		goto cleanup;

641
	result = named_config_getdscp(config, &dscp);
642 643 644
	if (result != ISC_R_SUCCESS)
		goto cleanup;

645
 newlist:
646 647
	addrlist = cfg_tuple_get(list, "addresses");
	portobj = cfg_tuple_get(list, "port");
Evan Hunt's avatar
Evan Hunt committed
648 649
	dscpobj = cfg_tuple_get(list, "dscp");

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

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

672 673
	result = ISC_R_NOMEMORY;

674 675 676
	element = cfg_list_first(addrlist);
 resume:
	for ( ;
677
	     element != NULL;
678
	     element = cfg_list_next(element))
679
	{
680 681
		const cfg_obj_t *addr;
		const cfg_obj_t *key;
682
		const char *keystr;
683 684
		isc_buffer_t b;

685 686
		addr = cfg_tuple_get(cfg_listelt_value(element),
				     "masterselement");
687 688
		key = cfg_tuple_get(cfg_listelt_value(element), "key");

689
		if (!cfg_obj_issockaddr(addr)) {
690
			const char *listname = cfg_obj_asstring(addr);
691 692 693 694
			isc_result_t tresult;

			/* Grow lists? */
			if (listcount == l) {
695
				void * tmp;
696
				uint32_t newlen = listcount + 16;
697 698 699 700
				size_t newsize, oldsize;

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

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

				newsize = newlen * sizeof(*stack);
				oldsize = stackcount * sizeof(*stack);
737 738
				tmp = isc_mem_get(mctx, newsize);
				if (tmp == NULL)
739 740
					goto cleanup;
				if (stackcount != 0) {
741
					memmove(tmp, stack, oldsize);
742 743
					isc_mem_put(mctx, stack, oldsize);
				}
744
				stack = tmp;
745 746 747 748 749 750 751 752
				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
753
			stack[pushed].dscp = dscp;
754 755 756 757 758
			pushed++;
			goto newlist;
		}

		if (i == addrcount) {
759
			void * tmp;
760
			uint32_t newlen = addrcount + 16;
761 762 763 764
			size_t newsize, oldsize;

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

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

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

800 801 802
		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
803 804 805
		dscps[i] = cfg_obj_getdscp(addr);
		if (dscps[i] == -1)
			dscps[i] = dscp;
806
		keys[i] = NULL;
807 808
		i++;	/* Increment here so that cleanup on error works. */
		if (!cfg_obj_isstring(key))
809
			continue;
810 811
		keys[i - 1] = isc_mem_get(mctx, sizeof(dns_name_t));
		if (keys[i - 1] == NULL)
812
			goto cleanup;
813
		dns_name_init(keys[i - 1