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 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
\n\
	/* view */\n\
132
	allow-new-zones no;\n\
133
	allow-notify {none;};\n\
134
	allow-query-cache { localnets; localhost; };\n\
135
	allow-query-cache-on { any; };\n\
136
	allow-recursion { localnets; localhost; };\n\
137
	allow-recursion-on { any; };\n\
138
	allow-update-forwarding {none;};\n\
139
#	allow-v6-synthesis <obsolete>;\n\
140
	auth-nxdomain false;\n\
141
	check-dup-records warn;\n\
142
	check-mx warn;\n\
143 144 145
	check-names master fail;\n\
	check-names response ignore;\n\
	check-names slave warn;\n\
146
	check-spf warn;\n\
147 148 149
	cleaning-interval 0;  /* now meaningless */\n\
	clients-per-query 10;\n\
	dnssec-accept-expired no;\n\
150
	dnssec-enable yes;\n\
151
	dnssec-validation " VALIDATION_DEFAULT "; \n"
152 153 154 155
#ifdef HAVE_DNSTAP
"	dnstap-identity hostname;\n"
#endif
"\
156
#	fetch-glue <obsolete>;\n\
Evan Hunt's avatar
Evan Hunt committed
157
	fetch-quota-params 100 0.1 0.3 0.7;\n\
158
	fetches-per-server 0;\n\
159 160
	fetches-per-zone 0;\n\
	filter-aaaa-on-v4 no;\n\
161
	filter-aaaa-on-v6 no;\n\
162 163 164 165 166 167
	filter-aaaa { any; };\n"
#ifdef HAVE_GEOIP
"	geoip-use-ecs yes;\n"
#endif
"	glue-cache yes;\n\
	lame-ttl 600;\n"
168
#ifdef HAVE_LMDB
169
"	lmdb-mapsize 32M;\n"
170
#endif
171
"	max-cache-size 90%;\n\
172 173 174 175 176
	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\
177
	max-stale-ttl 604800; /* 1 week */\n\
178 179 180
	message-compression yes;\n\
#	min-roots <obsolete>;\n\
	minimal-any false;\n\
181
	minimal-responses no-auth-recursive;\n\
182 183 184 185 186 187
	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\
Witold Krecicki's avatar
Witold Krecicki committed
188
	qname-minimization relaxed;\n\
189 190 191
	recursion true;\n\
	request-expire true;\n\
	request-ixfr true;\n\
192
	require-server-cookie no;\n\
193 194
	resolver-nonbackoff-tries 3;\n\
	resolver-retry-interval 800; /* in milliseconds */\n\
195
#	rfc2308-type1 <obsolete>;\n\
196
	root-key-sentinel yes;\n\
197 198
	servfail-ttl 1;\n\
#	sortlist <none>\n\
199 200
	stale-answer-enable false;\n\
	stale-answer-ttl 1; /* 1 second */\n\
201 202 203
	synth-from-dnssec yes;\n\
#	topology <none>\n\
	transfer-format many-answers;\n\
204
	v6-bias 50;\n\
205
	zero-no-soa-ttl-cache no;\n\
206 207
\n\
	/* zone */\n\
208
	allow-query {any;};\n\
209
	allow-query-on {any;};\n\
210 211
	allow-transfer {any;};\n\
#	also-notify <none>\n\
212 213 214 215 216 217 218
	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\
219
	dialup no;\n\
220 221 222 223
	dnssec-dnskey-kskonly no;\n\
	dnssec-loadkeys-interval 60;\n\
	dnssec-secure-to-insecure no;\n\
	dnssec-update-mode maintain;\n\
224 225
#	forward <none>\n\
#	forwarders <none>\n\
226 227
	inline-signing no;\n\
	ixfr-from-differences false;\n\
228
#	maintain-ixfr-base <obsolete>;\n\
229
#	max-ixfr-log-size <obsolete>\n\
230
	max-journal-size default;\n\
231
	max-records 0;\n\
232
	max-refresh-time 2419200; /* 4 weeks */\n\
233 234 235 236 237
	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\
238
	min-refresh-time 300;\n\
239
	min-retry-time 500;\n\
240
	multi-master no;\n\
241 242 243 244
	notify yes;\n\
	notify-delay 5;\n\
	notify-to-soa no;\n\
	serial-update-method increment;\n\
245 246
	sig-signing-nodes 100;\n\
	sig-signing-signatures 10;\n\
247
	sig-signing-type 65534;\n\
248
	sig-validity-interval 30; /* days */\n\
Evan Hunt's avatar
Evan Hunt committed
249
	dnskey-sig-validity 0; /* default: sig-validity-interval */\n\
250 251
	transfer-source *;\n\
	transfer-source-v6 *;\n\
252
	try-tcp-refresh yes; /* BIND 8 compat */\n\
253 254 255
	update-check-ksk yes;\n\
	zero-no-soa-ttl yes;\n\
	zone-statistics terse;\n\
256
};\n\
Mark Andrews's avatar
Mark Andrews committed
257 258 259
"

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

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

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

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

	isc_buffer_init(&b, defaultconf, sizeof(defaultconf) - 1);
	isc_buffer_add(&b, sizeof(defaultconf) - 1);
314 315 316
	return (cfg_parse_buffer4(parser, &b, __FILE__, 0,
				  &cfg_type_namedconf,
				  CFG_PCTX_NODEPRECATED, conf));
317 318 319
}

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

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

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

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

		}
	}
}

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

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

	return (i);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

539 540 541 542
	return (ISC_R_SUCCESS);
}

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

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

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

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

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

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

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

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

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

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

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

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

657 658
	result = ISC_R_NOMEMORY;

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

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

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

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

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

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

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

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

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

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

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

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

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