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 <config.h>
15 16 17 18 19 20

#include <stdlib.h>

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

28 29
#include <pk11/site.h>

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

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

40 41
#include <dst/dst.h>

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

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

47
/*% default configuration */
48 49
static char defaultconf[] = "\
options {\n\
50
#	answer-cookie <obsolete>;\n\
51
	automatic-interface-scan yes;\n\
52
	bindkeys-file \"" NAMED_SYSCONFDIR "/bind.keys\";\n\
53
#	blackhole {none;};\n"
54 55 56 57 58
#if defined(HAVE_OPENSSL_AES) || defined(HAVE_OPENSSL_EVP_AES)
"	cookie-algorithm aes;\n"
#else
"	cookie-algorithm sha256;\n"
#endif
59 60
#ifndef WIN32
"	coresize default;\n\
61
	datasize default;\n"
62
#endif
63
"\
64
#	deallocate-on-exit <obsolete>;\n\
65 66
#	directory <none>\n\
	dump-file \"named_dump.db\";\n\
67 68 69 70 71 72
	edns-udp-size 4096;\n\
#	fake-iquery <obsolete>;\n"
#ifndef WIN32
"	files unlimited;\n"
#endif
"\
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 150
	cleaning-interval 0;  /* now meaningless */\n\
	clients-per-query 10;\n\
	dnssec-accept-expired no;\n\
151
	dnssec-enable yes;\n\
152
	dnssec-validation " VALIDATION_DEFAULT "; \n"
153 154 155 156
#ifdef HAVE_DNSTAP
"	dnstap-identity hostname;\n"
#endif
"\
157
#	fetch-glue <obsolete>;\n\
Evan Hunt's avatar
Evan Hunt committed
158
	fetch-quota-params 100 0.1 0.3 0.7;\n\
159
	fetches-per-server 0;\n\
160 161
	fetches-per-zone 0;\n\
	filter-aaaa-on-v4 no;\n\
162
	filter-aaaa-on-v6 no;\n\
163 164 165 166 167 168
	filter-aaaa { any; };\n"
#ifdef HAVE_GEOIP
"	geoip-use-ecs yes;\n"
#endif
"	glue-cache yes;\n\
	lame-ttl 600;\n"
169
#ifdef HAVE_LMDB
170
"	lmdb-mapsize 32M;\n"
171
#endif
172
"	max-cache-size 90%;\n\
173 174 175 176 177
	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\
178
	max-stale-ttl 604800; /* 1 week */\n\
179 180 181
	message-compression yes;\n\
#	min-roots <obsolete>;\n\
	minimal-any false;\n\
182
	minimal-responses no-auth-recursive;\n\
183 184 185 186
	notify-source *;\n\
	notify-source-v6 *;\n\
	nsec3-test-zone no;\n\
	provide-ixfr true;\n\
187
	qname-minimization relaxed;\n\
188 189 190 191 192
	query-source address *;\n\
	query-source-v6 address *;\n\
	recursion true;\n\
	request-expire true;\n\
	request-ixfr true;\n\
193
	require-server-cookie no;\n\
194 195
	resolver-nonbackoff-tries 3;\n\
	resolver-retry-interval 800; /* in milliseconds */\n\
196
#	rfc2308-type1 <obsolete>;\n\
197
	root-key-sentinel yes;\n\
198 199
	servfail-ttl 1;\n\
#	sortlist <none>\n\
200 201
	stale-answer-enable false;\n\
	stale-answer-ttl 1; /* 1 second */\n\
202 203 204
	synth-from-dnssec yes;\n\
#	topology <none>\n\
	transfer-format many-answers;\n\
205
	v6-bias 50;\n\
206
	zero-no-soa-ttl-cache no;\n\
207 208
\n\
	/* zone */\n\
209
	allow-query {any;};\n\
210
	allow-query-on {any;};\n\
211 212
	allow-transfer {any;};\n\
#	also-notify <none>\n\
213 214 215 216 217 218 219
	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\
220
	dialup no;\n\
221 222 223 224
	dnssec-dnskey-kskonly no;\n\
	dnssec-loadkeys-interval 60;\n\
	dnssec-secure-to-insecure no;\n\
	dnssec-update-mode maintain;\n\
225 226
#	forward <none>\n\
#	forwarders <none>\n\
227 228
	inline-signing no;\n\
	ixfr-from-differences false;\n\
229
#	maintain-ixfr-base <obsolete>;\n\
230
#	max-ixfr-log-size <obsolete>\n\
231
	max-journal-size default;\n\
232
	max-records 0;\n\
233
	max-refresh-time 2419200; /* 4 weeks */\n\
234 235 236 237 238
	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\
239
	min-refresh-time 300;\n\
240
	min-retry-time 500;\n\
241
	multi-master no;\n\
242 243 244 245
	notify yes;\n\
	notify-delay 5;\n\
	notify-to-soa no;\n\
	serial-update-method increment;\n\
246 247
	sig-signing-nodes 100;\n\
	sig-signing-signatures 10;\n\
248
	sig-signing-type 65534;\n\
249
	sig-validity-interval 30; /* days */\n\
Evan Hunt's avatar
Evan Hunt committed
250
	dnskey-sig-validity 0; /* default: sig-validity-interval */\n\
251 252
	transfer-source *;\n\
	transfer-source-v6 *;\n\
253
	try-tcp-refresh yes; /* BIND 8 compat */\n\
254 255 256
	update-check-ksk yes;\n\
	zero-no-soa-ttl yes;\n\
	zone-statistics terse;\n\
257
};\n\
Mark Andrews's avatar
Mark Andrews committed
258 259 260
"

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

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

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

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

	isc_buffer_init(&b, defaultconf, sizeof(defaultconf) - 1);
	isc_buffer_add(&b, sizeof(defaultconf) - 1);
315 316 317
	return (cfg_parse_buffer4(parser, &b, __FILE__, 0,
				  &cfg_type_namedconf,
				  CFG_PCTX_NODEPRECATED, 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 431 432
	if (strcasecmp(str, "primary") == 0 ||
	    strcasecmp(str, "master") == 0)
	{
433
		ztype = dns_zone_master;
434 435 436
	} else if (strcasecmp(str, "secondary") == 0 ||
		   strcasecmp(str, "slave") == 0)
	{
437
		ztype = dns_zone_slave;
438
	} else if (strcasecmp(str, "stub") == 0) {
439
		ztype = dns_zone_stub;
440
	} else if (strcasecmp(str, "static-stub") == 0) {
441
		ztype = dns_zone_staticstub;
442
	} else if (strcasecmp(str, "redirect") == 0) {
443
		ztype = dns_zone_redirect;
444
	} else {
445
		INSIST(0);
446
	}
447 448 449 450
	return (ztype);
}

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

	INSIST(addrsp != NULL && *addrsp == NULL);
Evan Hunt's avatar
Evan Hunt committed
466
	INSIST(dscpsp == NULL || *dscpsp == NULL);
467
	INSIST(countp != NULL);
468 469

	addrlist = cfg_tuple_get(list, "addresses");
470
	count = named_config_listcount(addrlist);
471 472 473 474 475

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

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

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

507
	addrs = isc_mem_get(mctx, count * sizeof(isc_sockaddr_t));
508 509 510
	if (addrs == NULL) {
		if (dscps != NULL)
			isc_mem_put(mctx, dscps, count * sizeof(isc_dscp_t));
511
		return (ISC_R_NOMEMORY);
512
	}
513 514 515 516 517

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

540 541 542 543
	return (ISC_R_SUCCESS);
}

void
544 545
named_config_putiplist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
		       isc_dscp_t **dscpsp, isc_uint32_t count)
546 547
{
	INSIST(addrsp != NULL && *addrsp != NULL);
Evan Hunt's avatar
Evan Hunt committed
548
	INSIST(dscpsp == NULL || *dscpsp != NULL);
549 550 551

	isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
	*addrsp = NULL;
Evan Hunt's avatar
Evan Hunt committed
552 553 554 555 556

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

559
static isc_result_t
560
get_masters_def(const cfg_obj_t *cctx, const char *name,
Automatic Updater's avatar
Automatic Updater committed
561
		const cfg_obj_t **ret)
562
{
563
	isc_result_t result;
564 565
	const cfg_obj_t *masters = NULL;
	const cfg_listelt_t *elt;
566 567 568 569 570 571 572

	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)) {
573
		const cfg_obj_t *list;
574 575 576 577 578 579 580 581 582 583 584 585 586
		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);
}

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

612
	REQUIRE(ipkl != NULL);
613
	REQUIRE(ipkl->count == 0);
614 615 616
	REQUIRE(ipkl->addrs == NULL);
	REQUIRE(ipkl->keys == NULL);
	REQUIRE(ipkl->dscps == NULL);
617 618
	REQUIRE(ipkl->labels == NULL);
	REQUIRE(ipkl->allocated == 0);
619

620 621 622
	/*
	 * Get system defaults.
	 */
623
	result = named_config_getport(config, &port);
624 625 626
	if (result != ISC_R_SUCCESS)
		goto cleanup;

627
	result = named_config_getdscp(config, &dscp);
628 629 630
	if (result != ISC_R_SUCCESS)
		goto cleanup;

631
 newlist:
632 633
	addrlist = cfg_tuple_get(list, "addresses");
	portobj = cfg_tuple_get(list, "port");
Evan Hunt's avatar
Evan Hunt committed
634 635
	dscpobj = cfg_tuple_get(list, "dscp");

636 637 638
	if (cfg_obj_isuint32(portobj)) {
		isc_uint32_t val = cfg_obj_asuint32(portobj);
		if (val > ISC_UINT16_MAX) {
639
			cfg_obj_log(portobj, named_g_lctx, ISC_LOG_ERROR,
640
				    "port '%u' out of range", val);
641 642
			result = ISC_R_RANGE;
			goto cleanup;
643 644 645 646
		}
		port = (in_port_t) val;
	}

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

658 659
	result = ISC_R_NOMEMORY;

660 661 662
	element = cfg_list_first(addrlist);
 resume:
	for ( ;
663
	     element != NULL;
664
	     element = cfg_list_next(element))
665
	{
666 667
		const cfg_obj_t *addr;
		const cfg_obj_t *key;
668
		const char *keystr;
669 670
		isc_buffer_t b;

671 672
		addr = cfg_tuple_get(cfg_listelt_value(element),
				     "masterselement");
673 674
		key = cfg_tuple_get(cfg_listelt_value(element), "key");

675
		if (!cfg_obj_issockaddr(addr)) {
676
			const char *listname = cfg_obj_asstring(addr);
677 678 679 680
			isc_result_t tresult;

			/* Grow lists? */
			if (listcount == l) {
681
				void * tmp;
682 683 684 685 686
				isc_uint32_t newlen = listcount + 16;
				size_t newsize, oldsize;

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

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

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

		if (i == addrcount) {
744
			void * tmp;
745 746 747 748 749
			isc_uint32_t newlen = addrcount + 16;
			size_t newsize, oldsize;

			newsize = newlen * sizeof(isc_sockaddr_t);
			oldsize = addrcount * sizeof(isc_sockaddr_t);
750 751
			tmp = isc_mem_get(mctx, newsize);
			if (tmp == NULL)
752 753
				goto cleanup;
			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 763
			tmp = isc_mem_get(mctx, newsize);
			if (tmp == NULL)
Evan Hunt's avatar
Evan Hunt committed
764 765
				goto cleanup;
			if (dscpcount != 0) {
766
				memmove(tmp, dscps, oldsize);
Evan Hunt's avatar
Evan Hunt committed
767 768
				isc_mem_put(mctx, dscps, oldsize);
			}
769
			dscps = tmp;
Evan Hunt's avatar
Evan Hunt committed
770 771
			dscpcount = newlen;

772 773
			newsize = newlen * sizeof(dns_name_t *);
			oldsize = keycount * sizeof(dns_name_t *);
774 775
			tmp = isc_mem_get(mctx, newsize);
			if (tmp == NULL)
776 777
				goto cleanup;
			if (keycount != 0) {
778
				memmove(tmp, keys, oldsize);
779
				isc_mem_put(mctx, keys, oldsize);
780
			}
781
			keys = tmp;
782 783 784
			keycount = newlen;
		}

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

800
		keystr = cfg_obj_asstring(key);
801
		isc_buffer_constinit(&b, keystr, strlen(keystr));
802 803 804
		isc_buffer_add(&b, strlen(keystr));
		dns_fixedname_init(&fname);
		result = dns_name_fromtext(dns_fixedname_name(&fname), &b,
805
					   dns_rootname, 0, NULL);
806 807 808
		if (result != ISC_R_SUCCESS)
			goto cleanup;
		result = dns_name_dup(dns_fixedname_name(&fname), mctx,
809
				      keys[i - 1]);
810 811
		if (result != ISC_R_SUCCESS)
			goto cleanup;
812 813 814 815 816
	}
	if (pushed != 0) {
		pushed