config.c 25.5 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/namedconf.h>
31 32 33 34

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

39 40
#include <dst/dst.h>

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

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

46
/*% default configuration */
47 48
static char defaultconf[] = "\
options {\n\
49
	automatic-interface-scan yes;\n\
50
	bindkeys-file \"" NAMED_SYSCONFDIR "/bind.keys\";\n\
51
#	blackhole {none;};\n"
52 53 54 55 56
#if defined(HAVE_OPENSSL_AES) || defined(HAVE_OPENSSL_EVP_AES)
"	cookie-algorithm aes;\n"
#else
"	cookie-algorithm sha256;\n"
#endif
57 58
#ifndef WIN32
"	coresize default;\n\
59
	datasize default;\n"
60
#endif
61
"\
62
#	deallocate-on-exit <obsolete>;\n\
63 64
#	directory <none>\n\
	dump-file \"named_dump.db\";\n\
65 66 67 68 69 70
	edns-udp-size 4096;\n\
#	fake-iquery <obsolete>;\n"
#ifndef WIN32
"	files unlimited;\n"
#endif
"\
71
#	has-old-clients <obsolete>;\n\
72
	heartbeat-interval 60;\n\
73
#	host-statistics <obsolete>;\n\
74
	interface-interval 60;\n\
75
#	keep-response-order {none;};\n\
76
	listen-on {any;};\n\
77
	listen-on-v6 {any;};\n\
78
#	lock-file \"" NAMED_LOCALSTATEDIR "/run/named/named.lock\";\n\
79
	match-mapped-addresses no;\n\
80
	max-rsa-exponent-size 0; /* no limit */\n\
81
	max-udp-size 4096;\n\
82
	memstatistics-file \"named.memstats\";\n\
83
#	multiple-cnames <obsolete>;\n\
84
#	named-xfer <obsolete>;\n\
85 86
	nocookie-udp-size 4096;\n\
	notify-rate 20;\n\
Evan Hunt's avatar
Evan Hunt committed
87 88
	nta-lifetime 3600;\n\
	nta-recheck 300;\n\
89
#	pid-file \"" NAMED_LOCALSTATEDIR "/run/named/named.pid\"; \n\
90
	port 53;\n\
91
	prefetch 2 9;\n"
92 93 94
#if defined(ISC_PLATFORM_CRYPTORANDOM)
"	random-device none;\n"
#elif defined(PATH_RANDOMDEV)
95
"	random-device \"" PATH_RANDOMDEV "\";\n"
96
#endif
97
"	recursing-file \"named.recursing\";\n\
98
	recursive-clients 1000;\n\
99 100
	request-nsid false;\n\
	reserved-sockets 512;\n\
101
	resolver-query-timeout 10;\n\
102 103
	secroots-file \"named.secroots\";\n\
	send-cookie true;\n\
104
#	serial-queries <obsolete>;\n\
105
	serial-query-rate 20;\n\
106
	server-id none;\n\
107
	session-keyalg hmac-sha256;\n\
108
#	session-keyfile \"" NAMED_LOCALSTATEDIR "/run/named/session.key\";\n\
109 110 111 112 113
	session-keyname local-ddns;\n"
#ifndef WIN32
"	stacksize default;\n"
#endif
"	startup-notify-rate 20;\n\
114
	statistics-file \"named.stats\";\n\
115
#	statistics-interval <obsolete>;\n\
116
	tcp-advertised-timeout 300;\n\
117
	tcp-clients 150;\n\
118 119 120
	tcp-idle-timeout 300;\n\
	tcp-initial-timeout 300;\n\
	tcp-keepalive-timeout 300;\n\
121
	tcp-listen-queue 10;\n\
122 123
#	tkey-dhkey <none>\n\
#	tkey-domain <none>\n\
124
#	tkey-gssapi-credential <none>\n\
125
	transfer-message-size 20480;\n\
126 127
	transfers-in 10;\n\
	transfers-out 10;\n\
128
	transfers-per-ns 2;\n\
129
#	treat-cr-as-space <obsolete>;\n\
130
	trust-anchor-telemetry yes;\n\
131
#	use-id-pool <obsolete>;\n\
132
#	use-ixfr <obsolete>;\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 163
	fetches-per-zone 0;\n\
	filter-aaaa-on-v4 no;\n\
164
	filter-aaaa-on-v6 no;\n\
165 166 167 168 169 170
	filter-aaaa { any; };\n"
#ifdef HAVE_GEOIP
"	geoip-use-ecs yes;\n"
#endif
"	glue-cache yes;\n\
	lame-ttl 600;\n"
171
#ifdef HAVE_LMDB
172
"	lmdb-mapsize 32M;\n"
173
#endif
174
"	max-cache-size 90%;\n\
175 176 177 178 179
	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\
180
	max-stale-ttl 604800; /* 1 week */\n\
181 182 183
	message-compression yes;\n\
#	min-roots <obsolete>;\n\
	minimal-any false;\n\
184
	minimal-responses no-auth-recursive;\n\
185 186 187 188 189 190 191 192 193
	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\
194
	require-server-cookie no;\n\
195 196
	resolver-nonbackoff-tries 3;\n\
	resolver-retry-interval 800; /* in milliseconds */\n\
197 198 199
#	rfc2308-type1 <obsolete>;\n\
	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 250 251
	sig-validity-interval 30; /* days */\n\
	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
	return (cfg_parse_buffer3(parser, &b, __FILE__, 0,
315
				  &cfg_type_namedconf, conf));
316 317 318
}

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

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

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

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

		}
	}
}

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

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

	return (i);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

538 539 540 541
	return (ISC_R_SUCCESS);
}

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

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

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

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

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

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

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

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

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

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

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

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

656 657
	result = ISC_R_NOMEMORY;

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

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

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

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

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

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

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

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

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

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

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

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

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