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

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

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

302
"# END MANAGED KEYS\n\
303
";
304 305

isc_result_t
306
named_config_parsedefaults(cfg_parser_t *parser, cfg_obj_t **conf) {
307 308 309 310
	isc_buffer_t b;

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

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

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

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

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

		}
	}
}

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

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

	return (i);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

536 537 538 539
	return (ISC_R_SUCCESS);
}

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

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

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

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

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

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

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

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

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

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

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

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

654 655
	result = ISC_R_NOMEMORY;

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

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

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

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

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

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

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

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

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

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

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

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

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