config.c 25.4 KB
Newer Older
1
/*
2
 * Copyright (C) 2001-2017  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
/*! \file */
Brian Wellington's avatar
Brian Wellington committed
10 11

#include <config.h>
12 13 14 15 16 17

#include <stdlib.h>

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

25 26
#include <pk11/site.h>

27
#include <isccfg/namedconf.h>
28 29 30 31

#include <dns/fixedname.h>
#include <dns/name.h>
#include <dns/rdataclass.h>
32
#include <dns/rdatatype.h>
Brian Wellington's avatar
Brian Wellington committed
33
#include <dns/tsig.h>
34 35
#include <dns/zone.h>

36 37
#include <dst/dst.h>

38 39 40
#include <named/config.h>
#include <named/globals.h>

Evan Hunt's avatar
Evan Hunt committed
41
#include <bind.keys.h>
42

43
/*% default configuration */
44 45
static char defaultconf[] = "\
options {\n\
46
	automatic-interface-scan yes;\n\
47
	bindkeys-file \"" NAMED_SYSCONFDIR "/bind.keys\";\n\
48
#	blackhole {none;};\n"
49 50 51 52 53
#if defined(HAVE_OPENSSL_AES) || defined(HAVE_OPENSSL_EVP_AES)
"	cookie-algorithm aes;\n"
#else
"	cookie-algorithm sha256;\n"
#endif
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
	prefetch 2 9;\n"
89 90 91
#if defined(ISC_PLATFORM_CRYPTORANDOM)
"	random-device none;\n"
#elif defined(PATH_RANDOMDEV)
92
"	random-device \"" PATH_RANDOMDEV "\";\n"
93
#endif
94
"	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 100
	secroots-file \"named.secroots\";\n\
	send-cookie true;\n\
101
#	serial-queries <obsolete>;\n\
102
	serial-query-rate 20;\n\
103
	server-id none;\n\
104
	session-keyalg hmac-sha256;\n\
105
#	session-keyfile \"" NAMED_LOCALSTATEDIR "/run/named/session.key\";\n\
106 107 108 109 110
	session-keyname local-ddns;\n"
#ifndef WIN32
"	stacksize default;\n"
#endif
"	startup-notify-rate 20;\n\
111
	statistics-file \"named.stats\";\n\
112
#	statistics-interval <obsolete>;\n\
113
	tcp-advertised-timeout 300;\n\
114
	tcp-clients 150;\n\
115 116 117
	tcp-idle-timeout 300;\n\
	tcp-initial-timeout 300;\n\
	tcp-keepalive-timeout 300;\n\
118
	tcp-listen-queue 10;\n\
119 120
#	tkey-dhkey <none>\n\
#	tkey-domain <none>\n\
121
#	tkey-gssapi-credential <none>\n\
122
	transfer-message-size 20480;\n\
123 124
	transfers-in 10;\n\
	transfers-out 10;\n\
125
	transfers-per-ns 2;\n\
126
#	treat-cr-as-space <obsolete>;\n\
127
	trust-anchor-telemetry yes;\n\
128
#	use-id-pool <obsolete>;\n\
129
#	use-ixfr <obsolete>;\n\
130 131 132
\n\
	/* DLV */\n\
	dnssec-lookaside . trust-anchor dlv.isc.org;\n\
133 134
\n\
	/* view */\n\
135
	allow-new-zones no;\n\
136
	allow-notify {none;};\n\
137
	allow-query-cache { localnets; localhost; };\n\
138
	allow-query-cache-on { any; };\n\
139
	allow-recursion { localnets; localhost; };\n\
140
	allow-recursion-on { any; };\n\
141
	allow-update-forwarding {none;};\n\
142
#	allow-v6-synthesis <obsolete>;\n\
143
	auth-nxdomain false;\n\
144
	check-dup-records warn;\n\
145
	check-mx warn;\n\
146 147 148
	check-names master fail;\n\
	check-names response ignore;\n\
	check-names slave warn;\n\
149
	check-spf warn;\n\
150 151 152
	cleaning-interval 0;  /* now meaningless */\n\
	clients-per-query 10;\n\
	dnssec-accept-expired no;\n\
153
	dnssec-enable yes;\n\
154 155 156 157 158
	dnssec-validation yes; \n"
#ifdef HAVE_DNSTAP
"	dnstap-identity hostname;\n"
#endif
"\
159
#	fetch-glue <obsolete>;\n\
Evan Hunt's avatar
Evan Hunt committed
160
	fetch-quota-params 100 0.1 0.3 0.7;\n\
161
	fetches-per-server 0;\n\
162
	fetches-per-zone 0;\n"
163 164 165
#ifdef ALLOW_FILTER_AAAA
"	filter-aaaa-on-v4 no;\n\
	filter-aaaa-on-v6 no;\n\
166
	filter-aaaa { any; };\n"
167
#endif
168 169 170 171 172
#ifdef HAVE_GEOIP
"	geoip-use-ecs yes;\n"
#endif
"	glue-cache yes;\n\
	lame-ttl 600;\n"
173
#ifdef HAVE_LMDB
174
"	lmdb-mapsize 32M;\n"
175
#endif
176
"	max-cache-size 90%;\n\
177 178 179 180 181
	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\
182
	max-stale-ttl 604800; /* 1 week */\n\
183 184 185
	message-compression yes;\n\
#	min-roots <obsolete>;\n\
	minimal-any false;\n\
186
	minimal-responses no-auth-recursive;\n\
187 188 189 190 191 192 193 194 195
	notify-source *;\n\
	notify-source-v6 *;\n\
	nsec3-test-zone no;\n\
	provide-ixfr true;\n\
	query-source address *;\n\
	query-source-v6 address *;\n\
	recursion true;\n\
	request-expire true;\n\
	request-ixfr true;\n\
196
	require-server-cookie no;\n\
197 198
	resolver-nonbackoff-tries 3;\n\
	resolver-retry-interval 800; /* in milliseconds */\n\
199 200 201
#	rfc2308-type1 <obsolete>;\n\
	servfail-ttl 1;\n\
#	sortlist <none>\n\
202 203
	stale-answer-enable false;\n\
	stale-answer-ttl 1; /* 1 second */\n\
204 205 206
	synth-from-dnssec yes;\n\
#	topology <none>\n\
	transfer-format many-answers;\n\
207
	v6-bias 50;\n\
208
	zero-no-soa-ttl-cache no;\n\
209 210
\n\
	/* zone */\n\
211
	allow-query {any;};\n\
212
	allow-query-on {any;};\n\
213 214
	allow-transfer {any;};\n\
#	also-notify <none>\n\
215 216 217 218 219 220 221
	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\
222
	dialup no;\n\
223 224 225 226
	dnssec-dnskey-kskonly no;\n\
	dnssec-loadkeys-interval 60;\n\
	dnssec-secure-to-insecure no;\n\
	dnssec-update-mode maintain;\n\
227 228
#	forward <none>\n\
#	forwarders <none>\n\
229 230
	inline-signing no;\n\
	ixfr-from-differences false;\n\
231
#	maintain-ixfr-base <obsolete>;\n\
232
#	max-ixfr-log-size <obsolete>\n\
233
	max-journal-size default;\n\
234
	max-records 0;\n\
235
	max-refresh-time 2419200; /* 4 weeks */\n\
236 237 238 239 240
	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\
241
	min-refresh-time 300;\n\
242
	min-retry-time 500;\n\
243
	multi-master no;\n\
244 245 246 247
	notify yes;\n\
	notify-delay 5;\n\
	notify-to-soa no;\n\
	serial-update-method increment;\n\
248 249
	sig-signing-nodes 100;\n\
	sig-signing-signatures 10;\n\
250
	sig-signing-type 65534;\n\
251 252 253
	sig-validity-interval 30; /* days */\n\
	transfer-source *;\n\
	transfer-source-v6 *;\n\
254
	try-tcp-refresh yes; /* BIND 8 compat */\n\
255 256 257
	update-check-ksk yes;\n\
	zero-no-soa-ttl yes;\n\
	zone-statistics terse;\n\
258
};\n\
Mark Andrews's avatar
Mark Andrews committed
259 260 261
"

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

/* Imported from bind.keys.h: */
305
MANAGED_KEYS
306

307
"# END MANAGED KEYS\n\
308
";
309 310

isc_result_t
311
named_config_parsedefaults(cfg_parser_t *parser, cfg_obj_t **conf) {
312 313 314 315
	isc_buffer_t b;

	isc_buffer_init(&b, defaultconf, sizeof(defaultconf) - 1);
	isc_buffer_add(&b, sizeof(defaultconf) - 1);
316
	return (cfg_parse_buffer3(parser, &b, __FILE__, 0,
317
				  &cfg_type_namedconf, conf));
318 319 320
}

isc_result_t
321 322
named_config_get(cfg_obj_t const * const *maps, const char *name,
		 const cfg_obj_t **obj)
Evan Hunt's avatar
Evan Hunt committed
323
{
324 325
	int i;

326
	for (i = 0;; i++) {
327 328 329 330 331 332 333
		if (maps[i] == NULL)
			return (ISC_R_NOTFOUND);
		if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
			return (ISC_R_SUCCESS);
	}
}

334
isc_result_t
335 336
named_checknames_get(const cfg_obj_t **maps, const char *which,
		     const cfg_obj_t **obj)
337 338 339 340 341
{
	const cfg_listelt_t *element;
	const cfg_obj_t *checknames;
	const cfg_obj_t *type;
	const cfg_obj_t *value;
342 343 344 345 346 347
	int i;

	for (i = 0;; i++) {
		if (maps[i] == NULL)
			return (ISC_R_NOTFOUND);
		checknames = NULL;
348
		if (cfg_map_get(maps[i], "check-names",
Automatic Updater's avatar
Automatic Updater committed
349
				&checknames) == ISC_R_SUCCESS) {
350 351 352 353 354 355 356 357 358 359 360 361
			/*
			 * 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");
362 363
				if (strcasecmp(cfg_obj_asstring(type),
					       which) == 0) {
364 365 366 367 368 369 370 371 372
					*obj = cfg_tuple_get(value, "mode");
					return (ISC_R_SUCCESS);
				}
			}

		}
	}
}

373
int
374
named_config_listcount(const cfg_obj_t *list) {
375
	const cfg_listelt_t *e;
376 377 378 379 380 381 382 383 384
	int i = 0;

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

	return (i);
}

isc_result_t
385 386 387
named_config_getclass(const cfg_obj_t *classobj, dns_rdataclass_t defclass,
		      dns_rdataclass_t *classp)
{
388
	isc_textregion_t r;
389
	isc_result_t result;
390 391

	if (!cfg_obj_isstring(classobj)) {
392
		*classp = defclass;
393 394
		return (ISC_R_SUCCESS);
	}
395 396
	DE_CONST(cfg_obj_asstring(classobj), r.base);
	r.length = strlen(r.base);
397 398
	result = dns_rdataclass_fromtext(classp, &r);
	if (result != ISC_R_SUCCESS)
399
		cfg_obj_log(classobj, named_g_lctx, ISC_LOG_ERROR,
400
			    "unknown class '%s'", r.base);
401
	return (result);
402 403
}

404
isc_result_t
405 406 407
named_config_gettype(const cfg_obj_t *typeobj, dns_rdatatype_t deftype,
		     dns_rdatatype_t *typep)
{
408 409 410 411 412 413 414
	isc_textregion_t r;
	isc_result_t result;

	if (!cfg_obj_isstring(typeobj)) {
		*typep = deftype;
		return (ISC_R_SUCCESS);
	}
415 416
	DE_CONST(cfg_obj_asstring(typeobj), r.base);
	r.length = strlen(r.base);
417 418
	result = dns_rdatatype_fromtext(typep, &r);
	if (result != ISC_R_SUCCESS)
419
		cfg_obj_log(typeobj, named_g_lctx, ISC_LOG_ERROR,
420
			    "unknown type '%s'", r.base);
421 422 423
	return (result);
}

424
dns_zonetype_t
425
named_config_getzonetype(const cfg_obj_t *zonetypeobj) {
426
	dns_zonetype_t ztype = dns_zone_none;
427
	const char *str;
428 429

	str = cfg_obj_asstring(zonetypeobj);
430
	if (strcasecmp(str, "master") == 0)
431
		ztype = dns_zone_master;
432
	else if (strcasecmp(str, "slave") == 0)
433
		ztype = dns_zone_slave;
434
	else if (strcasecmp(str, "stub") == 0)
435
		ztype = dns_zone_stub;
436 437
	else if (strcasecmp(str, "static-stub") == 0)
		ztype = dns_zone_staticstub;
438 439
	else if (strcasecmp(str, "redirect") == 0)
		ztype = dns_zone_redirect;
440 441 442 443 444 445
	else
		INSIST(0);
	return (ztype);
}

isc_result_t
446
named_config_getiplist(const cfg_obj_t *config, const cfg_obj_t *list,
447
		    in_port_t defport, isc_mem_t *mctx,
Evan Hunt's avatar
Evan Hunt committed
448 449
		    isc_sockaddr_t **addrsp, isc_dscp_t **dscpsp,
		    isc_uint32_t *countp)
450 451
{
	int count, i = 0;
452
	const cfg_obj_t *addrlist;
Evan Hunt's avatar
Evan Hunt committed
453
	const cfg_obj_t *portobj, *dscpobj;
454
	const cfg_listelt_t *element;
455 456
	isc_sockaddr_t *addrs;
	in_port_t port;
Mark Andrews's avatar
Mark Andrews committed
457
	isc_dscp_t dscp = -1, *dscps = NULL;
458 459 460
	isc_result_t result;

	INSIST(addrsp != NULL && *addrsp == NULL);
Evan Hunt's avatar
Evan Hunt committed
461
	INSIST(dscpsp == NULL || *dscpsp == NULL);
462
	INSIST(countp != NULL);
463 464

	addrlist = cfg_tuple_get(list, "addresses");
465
	count = named_config_listcount(addrlist);
466 467 468 469 470

	portobj = cfg_tuple_get(list, "port");
	if (cfg_obj_isuint32(portobj)) {
		isc_uint32_t val = cfg_obj_asuint32(portobj);
		if (val > ISC_UINT16_MAX) {
471
			cfg_obj_log(portobj, named_g_lctx, ISC_LOG_ERROR,
472 473 474 475 476 477 478
				    "port '%u' out of range", val);
			return (ISC_R_RANGE);
		}
		port = (in_port_t) val;
	} else if (defport != 0)
		port = defport;
	else {
479
		result = named_config_getport(config, &port);
480 481 482 483
		if (result != ISC_R_SUCCESS)
			return (result);
	}

Evan Hunt's avatar
Evan Hunt committed
484 485 486 487
	if (dscpsp != NULL) {
		dscpobj = cfg_tuple_get(list, "dscp");
		if (dscpobj != NULL && cfg_obj_isuint32(dscpobj)) {
			if (cfg_obj_asuint32(dscpobj) > 63) {
488 489
				cfg_obj_log(dscpobj, named_g_lctx,
					    ISC_LOG_ERROR,
Evan Hunt's avatar
Evan Hunt committed
490 491 492 493 494 495
					    "dscp value '%u' is out of range",
					    cfg_obj_asuint32(dscpobj));
				return (ISC_R_RANGE);
			}
			dscp = (isc_dscp_t)cfg_obj_asuint32(dscpobj);
		}
496 497 498 499

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

502
	addrs = isc_mem_get(mctx, count * sizeof(isc_sockaddr_t));
503 504 505
	if (addrs == NULL) {
		if (dscps != NULL)
			isc_mem_put(mctx, dscps, count * sizeof(isc_dscp_t));
506
		return (ISC_R_NOMEMORY);
507
	}
508 509 510 511 512

	for (element = cfg_list_first(addrlist);
	     element != NULL;
	     element = cfg_list_next(element), i++)
	{
Evan Hunt's avatar
Evan Hunt committed
513
		const cfg_obj_t *addr;
514
		INSIST(i < count);
Evan Hunt's avatar
Evan Hunt committed
515 516 517 518 519 520 521 522 523
		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;
		}
524 525 526 527 528 529 530 531
		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
532 533 534
	if (dscpsp != NULL)
		*dscpsp = dscps;

535 536 537 538
	return (ISC_R_SUCCESS);
}

void
539 540
named_config_putiplist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
		       isc_dscp_t **dscpsp, isc_uint32_t count)
541 542
{
	INSIST(addrsp != NULL && *addrsp != NULL);
Evan Hunt's avatar
Evan Hunt committed
543
	INSIST(dscpsp == NULL || *dscpsp != NULL);
544 545 546

	isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
	*addrsp = NULL;
Evan Hunt's avatar
Evan Hunt committed
547 548 549 550 551

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

554
static isc_result_t
555
get_masters_def(const cfg_obj_t *cctx, const char *name,
Automatic Updater's avatar
Automatic Updater committed
556
		const cfg_obj_t **ret)
557
{
558
	isc_result_t result;
559 560
	const cfg_obj_t *masters = NULL;
	const cfg_listelt_t *elt;
561 562 563 564 565 566 567

	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)) {
568
		const cfg_obj_t *list;
569 570 571 572 573 574 575 576 577 578 579 580 581
		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);
}

582
isc_result_t
583
named_config_getipandkeylist(const cfg_obj_t *config, const cfg_obj_t *list,
584
			  isc_mem_t *mctx, dns_ipkeylist_t *ipkl)
585
{
Evan Hunt's avatar
Evan Hunt committed
586
	isc_uint32_t addrcount = 0, dscpcount = 0, keycount = 0, i = 0;
587 588
	isc_uint32_t listcount = 0, l = 0, j;
	isc_uint32_t stackcount = 0, pushed = 0;
589
	isc_result_t result;
590 591 592
	const cfg_listelt_t *element;
	const cfg_obj_t *addrlist;
	const cfg_obj_t *portobj;
Evan Hunt's avatar
Evan Hunt committed
593
	const cfg_obj_t *dscpobj;
594
	in_port_t port;
595
	isc_dscp_t dscp = -1;
596 597
	dns_fixedname_t fname;
	isc_sockaddr_t *addrs = NULL;
Evan Hunt's avatar
Evan Hunt committed
598
	isc_dscp_t *dscps = NULL;
599
	dns_name_t **keys = NULL;
Mark Andrews's avatar
Mark Andrews committed
600
	struct { const char *name; } *lists = NULL;
601
	struct {
602
		const cfg_listelt_t *element;
603
		in_port_t port;
Evan Hunt's avatar
Evan Hunt committed
604
		isc_dscp_t dscp;
605
	} *stack = NULL;
606

607
	REQUIRE(ipkl != NULL);
608
	REQUIRE(ipkl->count == 0);
609 610 611
	REQUIRE(ipkl->addrs == NULL);
	REQUIRE(ipkl->keys == NULL);
	REQUIRE(ipkl->dscps == NULL);
612 613
	REQUIRE(ipkl->labels == NULL);
	REQUIRE(ipkl->allocated == 0);
614

615 616 617
	/*
	 * Get system defaults.
	 */
618
	result = named_config_getport(config, &port);
619 620 621
	if (result != ISC_R_SUCCESS)
		goto cleanup;

622
	result = named_config_getdscp(config, &dscp);
623 624 625
	if (result != ISC_R_SUCCESS)
		goto cleanup;

626
 newlist:
627 628
	addrlist = cfg_tuple_get(list, "addresses");
	portobj = cfg_tuple_get(list, "port");
Evan Hunt's avatar
Evan Hunt committed
629 630
	dscpobj = cfg_tuple_get(list, "dscp");

631 632 633
	if (cfg_obj_isuint32(portobj)) {
		isc_uint32_t val = cfg_obj_asuint32(portobj);
		if (val > ISC_UINT16_MAX) {
634
			cfg_obj_log(portobj, named_g_lctx, ISC_LOG_ERROR,
635
				    "port '%u' out of range", val);
636 637
			result = ISC_R_RANGE;
			goto cleanup;
638 639 640 641
		}
		port = (in_port_t) val;
	}

Evan Hunt's avatar
Evan Hunt committed
642 643
	if (dscpobj != NULL && cfg_obj_isuint32(dscpobj)) {
		if (cfg_obj_asuint32(dscpobj) > 63) {
644
			cfg_obj_log(dscpobj, named_g_lctx, ISC_LOG_ERROR,
Evan Hunt's avatar
Evan Hunt committed
645 646
				    "dscp value '%u' is out of range",
				    cfg_obj_asuint32(dscpobj));
647 648
			result = ISC_R_RANGE;
			goto cleanup;
Evan Hunt's avatar
Evan Hunt committed
649 650 651 652
		}
		dscp = (isc_dscp_t)cfg_obj_asuint32(dscpobj);
	}

653 654
	result = ISC_R_NOMEMORY;

655 656 657
	element = cfg_list_first(addrlist);
 resume:
	for ( ;
658
	     element != NULL;
659
	     element = cfg_list_next(element))
660
	{
661 662
		const cfg_obj_t *addr;
		const cfg_obj_t *key;
663
		const char *keystr;
664 665
		isc_buffer_t b;

666 667
		addr = cfg_tuple_get(cfg_listelt_value(element),
				     "masterselement");
668 669
		key = cfg_tuple_get(cfg_listelt_value(element), "key");

670
		if (!cfg_obj_issockaddr(addr)) {
671
			const char *listname = cfg_obj_asstring(addr);
672 673 674 675
			isc_result_t tresult;

			/* Grow lists? */
			if (listcount == l) {
676
				void * tmp;
677 678 679 680 681
				isc_uint32_t newlen = listcount + 16;
				size_t newsize, oldsize;

				newsize = newlen * sizeof(*lists);
				oldsize = listcount * sizeof(*lists);
682 683
				tmp = isc_mem_get(mctx, newsize);
				if (tmp == NULL)
684 685
					goto cleanup;
				if (listcount != 0) {
686
					memmove(tmp, lists, oldsize);
687 688
					isc_mem_put(mctx, lists, oldsize);
				}
689
				lists = tmp;
690 691 692 693
				listcount = newlen;
			}
			/* Seen? */
			for (j = 0; j < l; j++)
Mark Andrews's avatar
Mark Andrews committed
694
				if (strcasecmp(lists[j].name, listname) == 0)
695 696 697 698 699
					break;
			if (j < l)
				continue;
			tresult = get_masters_def(config, listname, &list);
			if (tresult == ISC_R_NOTFOUND) {
700
				cfg_obj_log(addr, named_g_lctx, ISC_LOG_ERROR,
Automatic Updater's avatar
Automatic Updater committed
701
				    "masters \"%s\" not found", listname);
702 703 704 705 706 707

				result = tresult;
				goto cleanup;
			}
			if (tresult != ISC_R_SUCCESS)
				goto cleanup;
Mark Andrews's avatar
Mark Andrews committed
708
			lists[l++].name = listname;
709 710
			/* Grow stack? */
			if (stackcount == pushed) {
711
				void * tmp;
712 713 714 715 716
				isc_uint32_t newlen = stackcount + 16;
				size_t newsize, oldsize;

				newsize = newlen * sizeof(*stack);
				oldsize = stackcount * sizeof(*stack);
717 718
				tmp = isc_mem_get(mctx, newsize);
				if (tmp == NULL)
719 720
					goto cleanup;
				if (stackcount != 0) {
721
					memmove(tmp, stack, oldsize);
722 723
					isc_mem_put(mctx, stack, oldsize);
				}
724
				stack = tmp;
725 726 727 728 729 730 731 732
				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
733
			stack[pushed].dscp = dscp;
734 735 736 737 738
			pushed++;
			goto newlist;
		}

		if (i == addrcount) {
739
			void * tmp;
740 741 742 743 744
			isc_uint32_t newlen = addrcount + 16;
			size_t newsize, oldsize;

			newsize = newlen * sizeof(isc_sockaddr_t);
			oldsize = addrcount * sizeof(isc_sockaddr_t);
745 746
			tmp = isc_mem_get(mctx, newsize);
			if (tmp == NULL)
747 748
				goto cleanup;
			if (addrcount != 0) {
749
				memmove(tmp, addrs, oldsize);
750 751
				isc_mem_put(mctx, addrs, oldsize);
			}
752
			addrs = tmp;
753 754
			addrcount = newlen;

Evan Hunt's avatar
Evan Hunt committed
755 756
			newsize = newlen * sizeof(isc_dscp_t);
			oldsize = dscpcount * sizeof(isc_dscp_t);
757 758
			tmp = isc_mem_get(mctx, newsize);
			if (tmp == NULL)
Evan Hunt's avatar
Evan Hunt committed
759 760
				goto cleanup;
			if (dscpcount != 0) {
761
				memmove(tmp, dscps, oldsize);
Evan Hunt's avatar
Evan Hunt committed
762 763
				isc_mem_put(mctx, dscps, oldsize);
			}
764
			dscps = tmp;
Evan Hunt's avatar
Evan Hunt committed
765 766
			dscpcount = newlen;

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

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

795
		keystr = cfg_obj_asstring(key);
796
		isc_buffer_constinit(&b, keystr, strlen(keystr));
797 798 799
		isc_buffer_add(&b, strlen(keystr));
		dns_fixedname_init(&fname);
		result = dns_name_fromtext(dns_fixedname_name(&fname), &b,
800
					   dns_rootname, 0, NULL);
801 802 803
		if (result != ISC_R_SUCCESS)
			goto cleanup;
		result = dns_name_dup(dns_fixedname_name(&fname), mctx,
804
				      keys[i - 1]);
805 806
		if (result != ISC_R_SUCCESS)
			goto cleanup;
807 808 809 810 811
	}
	if (pushed != 0) {
		pushed--;
		element = stack[pushed].element;
		port = stack[pushed].port;
Evan Hunt's avatar
Evan Hunt committed
812
		dscp = stack[pushed].dscp;
813 814 815
		goto resume;
	}
	if (i < addrcount) {
816
		void * tmp;
817 818 819 820 821
		size_t newsize, oldsize;

		newsize = i * sizeof(isc_sockaddr_t);
		oldsize = addrcount * sizeof(isc_sockaddr_t);
		if (i != 0) {
822 823
			tmp = isc_mem_get(mctx, newsize);
			if (tmp == NULL)
824
				goto cleanup;