server.c 174 KB
Newer Older
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2003  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
6 7
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
16 17
 */

18
/* $Id: server.c,v 1.544 2009/09/01 07:14:25 each Exp $ */
19 20

/*! \file */
David Lawrence's avatar
David Lawrence committed
21

22 23 24
#include <config.h>

#include <stdlib.h>
25
#include <unistd.h>
26

27
#include <isc/app.h>
28
#include <isc/base64.h>
29
#include <isc/dir.h>
30
#include <isc/entropy.h>
31
#include <isc/file.h>
32
#include <isc/hash.h>
33
#include <isc/httpd.h>
34
#include <isc/lex.h>
35
#include <isc/parseint.h>
36
#include <isc/portset.h>
37 38
#include <isc/print.h>
#include <isc/resource.h>
Mark Andrews's avatar
Mark Andrews committed
39
#include <isc/socket.h>
40
#include <isc/stat.h>
41
#include <isc/stats.h>
42
#include <isc/stdio.h>
43
#include <isc/string.h>
44
#include <isc/task.h>
45
#include <isc/timer.h>
Michael Graff's avatar
Michael Graff committed
46
#include <isc/util.h>
47
#include <isc/xml.h>
48

49
#include <isccfg/namedconf.h>
50 51

#include <bind9/check.h>
52

53
#include <dns/acache.h>
54
#include <dns/adb.h>
55
#include <dns/cache.h>
56
#include <dns/db.h>
Bob Halley's avatar
Bob Halley committed
57
#include <dns/dispatch.h>
58 59 60
#ifdef DLZ
#include <dns/dlz.h>
#endif
61
#include <dns/forward.h>
62
#include <dns/journal.h>
63
#include <dns/keytable.h>
64
#include <dns/keyvalues.h>
65
#include <dns/lib.h>
66
#include <dns/master.h>
67
#include <dns/masterdump.h>
68
#include <dns/order.h>
69
#include <dns/peer.h>
70
#include <dns/portlist.h>
71
#include <dns/rbt.h>
72
#include <dns/rdataclass.h>
73
#include <dns/rdataset.h>
74
#include <dns/rdatastruct.h>
75
#include <dns/resolver.h>
76
#include <dns/rootns.h>
77
#include <dns/secalg.h>
78
#include <dns/stats.h>
79
#include <dns/tkey.h>
80
#include <dns/tsig.h>
81
#include <dns/view.h>
82
#include <dns/zone.h>
83
#include <dns/zt.h>
84

85
#include <dst/dst.h>
86
#include <dst/result.h>
87

88
#include <named/client.h>
89
#include <named/config.h>
90
#include <named/control.h>
91
#include <named/interfacemgr.h>
92
#include <named/log.h>
93
#include <named/logconf.h>
94
#include <named/lwresd.h>
95
#include <named/main.h>
96
#include <named/os.h>
Bob Halley's avatar
Bob Halley committed
97
#include <named/server.h>
98
#include <named/statschannel.h>
99 100 101
#include <named/tkeyconf.h>
#include <named/tsigconf.h>
#include <named/zoneconf.h>
102 103 104 105
#ifdef HAVE_LIBSCF
#include <named/ns_smf_globals.h>
#include <stdlib.h>
#endif
106

107
/*%
108 109 110 111
 * Check an operation for failure.  Assumes that the function
 * using it has a 'result' variable and a 'cleanup' label.
 */
#define CHECK(op) \
112 113
	do { result = (op);					 \
	       if (result != ISC_R_SUCCESS) goto cleanup;	 \
114 115 116
	} while (0)

#define CHECKM(op, msg) \
117
	do { result = (op);					  \
118 119 120 121 122 123 124 125 126 127 128
	       if (result != ISC_R_SUCCESS) {			  \
			isc_log_write(ns_g_lctx,		  \
				      NS_LOGCATEGORY_GENERAL,	  \
				      NS_LOGMODULE_SERVER,	  \
				      ISC_LOG_ERROR,		  \
				      "%s: %s", msg,		  \
				      isc_result_totext(result)); \
			goto cleanup;				  \
		}						  \
	} while (0)						  \

Mark Andrews's avatar
Mark Andrews committed
129
#define CHECKMF(op, msg, file) \
130
	do { result = (op);					  \
Mark Andrews's avatar
Mark Andrews committed
131 132 133 134 135 136 137 138 139 140 141
	       if (result != ISC_R_SUCCESS) {			  \
			isc_log_write(ns_g_lctx,		  \
				      NS_LOGCATEGORY_GENERAL,	  \
				      NS_LOGMODULE_SERVER,	  \
				      ISC_LOG_ERROR,		  \
				      "%s '%s': %s", msg, file,	  \
				      isc_result_totext(result)); \
			goto cleanup;				  \
		}						  \
	} while (0)						  \

142
#define CHECKFATAL(op, msg) \
143
	do { result = (op);					  \
144 145 146 147
	       if (result != ISC_R_SUCCESS)			  \
			fatal(msg, result);			  \
	} while (0)						  \

148 149 150 151 152 153
/*%
 * Maximum ADB size for views that share a cache.  Use this limit to suppress
 * the total of memory footprint, which should be the main reason for sharing
 * a cache.  Only effective when a finite max-cache-size is specified.
 * This is currently defined to be 8MB.
 */
154
#define MAX_ADB_SIZE_FOR_CACHESHARE	8388608
155

156 157 158 159 160 161 162
struct ns_dispatch {
	isc_sockaddr_t			addr;
	unsigned int			dispatchgen;
	dns_dispatch_t			*dispatch;
	ISC_LINK(struct ns_dispatch)	link;
};

163 164 165 166 167 168 169 170
struct ns_cache {
	dns_cache_t			*cache;
	dns_view_t			*primaryview;
	isc_boolean_t			needflush;
	isc_boolean_t			adbsizeadjusted;
	ISC_LINK(ns_cache_t)		link;
};

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
struct dumpcontext {
	isc_mem_t			*mctx;
	isc_boolean_t			dumpcache;
	isc_boolean_t			dumpzones;
	FILE				*fp;
	ISC_LIST(struct viewlistentry)	viewlist;
	struct viewlistentry		*view;
	struct zonelistentry		*zone;
	dns_dumpctx_t			*mdctx;
	dns_db_t			*db;
	dns_db_t			*cache;
	isc_task_t			*task;
	dns_dbversion_t			*version;
};

struct viewlistentry {
	dns_view_t			*view;
	ISC_LINK(struct viewlistentry)	link;
	ISC_LIST(struct zonelistentry)	zonelist;
};

struct zonelistentry {
	dns_zone_t			*zone;
	ISC_LINK(struct zonelistentry)	link;
};

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
/*
 * These zones should not leak onto the Internet.
 */
static const struct {
	const char	*zone;
	isc_boolean_t	rfc1918;
} empty_zones[] = {
#ifdef notyet
	/* RFC 1918 */
	{ "10.IN-ADDR.ARPA", ISC_TRUE },
	{ "16.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "17.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "18.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "19.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "20.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "21.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "22.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "23.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "24.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "25.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "26.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "27.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "28.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "29.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "30.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "31.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "168.192.IN-ADDR.ARPA", ISC_TRUE },
#endif

	/* RFC 3330 */
227
	{ "0.IN-ADDR.ARPA", ISC_FALSE },	/* THIS NETWORK */
228 229 230 231 232 233 234 235
	{ "127.IN-ADDR.ARPA", ISC_FALSE },	/* LOOPBACK */
	{ "254.169.IN-ADDR.ARPA", ISC_FALSE },	/* LINK LOCAL */
	{ "2.0.192.IN-ADDR.ARPA", ISC_FALSE },	/* TEST NET */
	{ "255.255.255.255.IN-ADDR.ARPA", ISC_FALSE },	/* BROADCAST */

	/* Local IPv6 Unicast Addresses */
	{ "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.ARPA", ISC_FALSE },
	{ "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.ARPA", ISC_FALSE },
Francis Dupont's avatar
Francis Dupont committed
236
	/* LOCALLY ASSIGNED LOCAL ADDRESS SCOPE */
237 238 239 240 241 242 243 244 245
	{ "D.F.IP6.ARPA", ISC_FALSE },
	{ "8.E.F.IP6.ARPA", ISC_FALSE },	/* LINK LOCAL */
	{ "9.E.F.IP6.ARPA", ISC_FALSE },	/* LINK LOCAL */
	{ "A.E.F.IP6.ARPA", ISC_FALSE },	/* LINK LOCAL */
	{ "B.E.F.IP6.ARPA", ISC_FALSE },	/* LINK LOCAL */

	{ NULL, ISC_FALSE }
};

David Lawrence's avatar
David Lawrence committed
246 247 248 249 250
static void
fatal(const char *msg, isc_result_t result);

static void
ns_server_reload(isc_task_t *task, isc_event_t *event);
251

252
static isc_result_t
253
ns_listenelt_fromconfig(const cfg_obj_t *listener, const cfg_obj_t *config,
254
			cfg_aclconfctx_t *actx,
255 256
			isc_mem_t *mctx, ns_listenelt_t **target);
static isc_result_t
257
ns_listenlist_fromconfig(const cfg_obj_t *listenlist, const cfg_obj_t *config,
258
			 cfg_aclconfctx_t *actx,
259
			 isc_mem_t *mctx, ns_listenlist_t **target);
260

261
static isc_result_t
262 263
configure_forward(const cfg_obj_t *config, dns_view_t *view, dns_name_t *origin,
		  const cfg_obj_t *forwarders, const cfg_obj_t *forwardtype);
264

265
static isc_result_t
266 267
configure_alternates(const cfg_obj_t *config, dns_view_t *view,
		     const cfg_obj_t *alternates);
268

269
static isc_result_t
270 271
configure_zone(const cfg_obj_t *config, const cfg_obj_t *zconfig,
	       const cfg_obj_t *vconfig, isc_mem_t *mctx, dns_view_t *view,
272
	       cfg_aclconfctx_t *aclconf);
273

274 275 276
static isc_result_t
add_keydata_zone(dns_view_t *view, isc_mem_t *mctx);

277 278 279
static void
end_reserved_dispatches(ns_server_t *server, isc_boolean_t all);

280
/*%
Tatuya JINMEI 神明達哉's avatar
Tatuya JINMEI 神明達哉 committed
281 282
 * Configure a single view ACL at '*aclp'.  Get its configuration from
 * 'vconfig' (for per-view configuration) and maybe from 'config'
283 284
 */
static isc_result_t
285
configure_view_acl(const cfg_obj_t *vconfig, const cfg_obj_t *config,
286 287
		   const char *aclname, const char *acltuplename,
		   cfg_aclconfctx_t *actx, isc_mem_t *mctx, dns_acl_t **aclp)
288 289
{
	isc_result_t result;
290 291
	const cfg_obj_t *maps[3];
	const cfg_obj_t *aclobj = NULL;
292 293
	int i = 0;

294 295
	if (*aclp != NULL)
		dns_acl_detach(aclp);
296 297 298
	if (vconfig != NULL)
		maps[i++] = cfg_tuple_get(vconfig, "options");
	if (config != NULL) {
299
		const cfg_obj_t *options = NULL;
300
		(void)cfg_map_get(config, "options", &options);
301 302 303 304
		if (options != NULL)
			maps[i++] = options;
	}
	maps[i] = NULL;
305

306
	(void)ns_config_get(maps, aclname, &aclobj);
307
	if (aclobj == NULL)
308
		/*
309
		 * No value available.	*aclp == NULL.
310
		 */
311 312
		return (ISC_R_SUCCESS);

313 314 315 316 317 318 319 320 321
	if (acltuplename != NULL) {
		/*
		 * If the ACL is given in an optional tuple, retrieve it.
		 * The parser should have ensured that a valid object be
		 * returned.
		 */
		aclobj = cfg_tuple_get(aclobj, acltuplename);
	}

322
	result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
323 324 325 326 327 328 329 330 331 332 333 334
				    actx, mctx, 0, aclp);

	return (result);
}

/*%
 * Configure a sortlist at '*aclp'.  Essentially the same as
 * configure_view_acl() except it calls cfg_acl_fromconfig with a
 * nest_level value of 2.
 */
static isc_result_t
configure_view_sortlist(const cfg_obj_t *vconfig, const cfg_obj_t *config,
Automatic Updater's avatar
Automatic Updater committed
335 336
			cfg_aclconfctx_t *actx, isc_mem_t *mctx,
			dns_acl_t **aclp)
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
{
	isc_result_t result;
	const cfg_obj_t *maps[3];
	const cfg_obj_t *aclobj = NULL;
	int i = 0;

	if (*aclp != NULL)
		dns_acl_detach(aclp);
	if (vconfig != NULL)
		maps[i++] = cfg_tuple_get(vconfig, "options");
	if (config != NULL) {
		const cfg_obj_t *options = NULL;
		(void)cfg_map_get(config, "options", &options);
		if (options != NULL)
			maps[i++] = options;
	}
	maps[i] = NULL;

	(void)ns_config_get(maps, "sortlist", &aclobj);
	if (aclobj == NULL)
		return (ISC_R_SUCCESS);

Automatic Updater's avatar
Automatic Updater committed
359 360 361 362 363 364
	/*
	 * Use a nest level of 3 for the "top level" of the sortlist;
	 * this means each entry in the top three levels will be stored
	 * as lists of separate, nested ACLs, rather than merged together
	 * into IP tables as is usually done with ACLs.
	 */
365
	result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
366
				    actx, mctx, 3, aclp);
367 368 369 370

	return (result);
}

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
static isc_result_t
configure_view_nametable(const cfg_obj_t *vconfig, const cfg_obj_t *config,
			 const char *confname, const char *conftuplename,
			 isc_mem_t *mctx, dns_rbt_t **rbtp)
{
	isc_result_t result;
	const cfg_obj_t *maps[3];
	const cfg_obj_t *obj = NULL;
	const cfg_listelt_t *element;
	int i = 0;
	dns_fixedname_t fixed;
	dns_name_t *name;
	isc_buffer_t b;
	const char *str;
	const cfg_obj_t *nameobj;

	if (*rbtp != NULL)
		dns_rbt_destroy(rbtp);
	if (vconfig != NULL)
		maps[i++] = cfg_tuple_get(vconfig, "options");
	if (config != NULL) {
		const cfg_obj_t *options = NULL;
		(void)cfg_map_get(config, "options", &options);
		if (options != NULL)
			maps[i++] = options;
	}
	maps[i] = NULL;

	(void)ns_config_get(maps, confname, &obj);
	if (obj == NULL)
		/*
		 * No value available.	*rbtp == NULL.
		 */
		return (ISC_R_SUCCESS);

	if (conftuplename != NULL) {
		obj = cfg_tuple_get(obj, conftuplename);
		if (cfg_obj_isvoid(obj))
			return (ISC_R_SUCCESS);
	}

	result = dns_rbt_create(mctx, NULL, NULL, rbtp);
	if (result != ISC_R_SUCCESS)
		return (result);

	dns_fixedname_init(&fixed);
	name = dns_fixedname_name(&fixed);
	for (element = cfg_list_first(obj);
	     element != NULL;
	     element = cfg_list_next(element)) {
		nameobj = cfg_listelt_value(element);
		str = cfg_obj_asstring(nameobj);
		isc_buffer_init(&b, str, strlen(str));
		isc_buffer_add(&b, strlen(str));
425
		CHECK(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
426 427 428 429 430 431 432
		/*
		 * We don't need the node data, but need to set dummy data to
		 * avoid a partial match with an empty node.  For example, if
		 * we have foo.example.com and bar.example.com, we'd get a match
		 * for baz.example.com, which is not the expected result.
		 * We simply use (void *)1 as the dummy data.
		 */
433 434 435 436 437 438 439
		result = dns_rbt_addname(*rbtp, name, (void *)1);
		if (result != ISC_R_SUCCESS) {
			cfg_obj_log(nameobj, ns_g_lctx, ISC_LOG_ERROR,
				    "failed to add %s for %s: %s",
				    str, confname, isc_result_totext(result));
			goto cleanup;
		}
Automatic Updater's avatar
Automatic Updater committed
440

441 442 443 444 445 446 447
	}

	return (result);

  cleanup:
	dns_rbt_destroy(rbtp);
	return (result);
Automatic Updater's avatar
Automatic Updater committed
448

449 450
}

451
static isc_result_t
452 453
dstkey_fromconfig(const cfg_obj_t *vconfig, const cfg_obj_t *key,
		  isc_boolean_t managed, dst_key_t **target, isc_mem_t *mctx)
454 455
{
	dns_rdataclass_t viewclass;
456
	dns_rdata_dnskey_t keystruct;
457
	isc_uint32_t flags, proto, alg;
458
	const char *keystr, *keynamestr;
459 460 461 462 463 464 465 466 467 468 469
	unsigned char keydata[4096];
	isc_buffer_t keydatabuf;
	unsigned char rrdata[4096];
	isc_buffer_t rrdatabuf;
	isc_region_t r;
	dns_fixedname_t fkeyname;
	dns_name_t *keyname;
	isc_buffer_t namebuf;
	isc_result_t result;
	dst_key_t *dstkey = NULL;

470 471
	INSIST(target != NULL && *target == NULL);

472 473 474 475 476 477
	flags = cfg_obj_asuint32(cfg_tuple_get(key, "flags"));
	proto = cfg_obj_asuint32(cfg_tuple_get(key, "protocol"));
	alg = cfg_obj_asuint32(cfg_tuple_get(key, "algorithm"));
	keyname = dns_fixedname_name(&fkeyname);
	keynamestr = cfg_obj_asstring(cfg_tuple_get(key, "name"));

478 479 480 481 482 483 484 485 486 487 488 489 490 491
        if (managed) {
                const char *initmethod;
                initmethod = cfg_obj_asstring(cfg_tuple_get(key, "init"));

                if (strcmp(initmethod, "initial-key") != 0) {
                        cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
                                    "managed key '%s': "
                                    "invalid initialization method '%s'",
                                    keynamestr, initmethod);
                        result = ISC_R_FAILURE;
                        goto cleanup;
                }
        }

492
	if (vconfig == NULL)
493
		viewclass = dns_rdataclass_in;
494
	else {
495
		const cfg_obj_t *classobj = cfg_tuple_get(vconfig, "class");
496 497
		CHECK(ns_config_getclass(classobj, dns_rdataclass_in,
					 &viewclass));
498
	}
499
	keystruct.common.rdclass = viewclass;
500
	keystruct.common.rdtype = dns_rdatatype_dnskey;
501
	/*
502
	 * The key data in keystruct is not dynamically allocated.
503 504 505 506 507
	 */
	keystruct.mctx = NULL;

	ISC_LINK_INIT(&keystruct.common, link);

508
	if (flags > 0xffff)
509
		CHECKM(ISC_R_RANGE, "key flags");
510
	if (proto > 0xff)
511
		CHECKM(ISC_R_RANGE, "key protocol");
512
	if (alg > 0xff)
513
		CHECKM(ISC_R_RANGE, "key algorithm");
514 515 516
	keystruct.flags = (isc_uint16_t)flags;
	keystruct.protocol = (isc_uint8_t)proto;
	keystruct.algorithm = (isc_uint8_t)alg;
517 518 519 520

	isc_buffer_init(&keydatabuf, keydata, sizeof(keydata));
	isc_buffer_init(&rrdatabuf, rrdata, sizeof(rrdata));

521
	keystr = cfg_obj_asstring(cfg_tuple_get(key, "key"));
522
	CHECK(isc_base64_decodestring(keystr, &keydatabuf));
523 524 525 526
	isc_buffer_usedregion(&keydatabuf, &r);
	keystruct.datalen = r.length;
	keystruct.data = r.base;

527 528 529 530
	if ((keystruct.algorithm == DST_ALG_RSASHA1 ||
	     keystruct.algorithm == DST_ALG_RSAMD5) &&
	    r.length > 1 && r.base[0] == 1 && r.base[1] == 3)
		cfg_obj_log(key, ns_g_lctx, ISC_LOG_WARNING,
531 532
			    "%s key '%s' has a weak exponent",
			    managed ? "managed" : "trusted",
533 534
			    keynamestr);

535 536 537 538 539
	CHECK(dns_rdata_fromstruct(NULL,
				   keystruct.common.rdclass,
				   keystruct.common.rdtype,
				   &keystruct, &rrdatabuf));
	dns_fixedname_init(&fkeyname);
540 541
	isc_buffer_init(&namebuf, keynamestr, strlen(keynamestr));
	isc_buffer_add(&namebuf, strlen(keynamestr));
542
	CHECK(dns_name_fromtext(keyname, &namebuf, dns_rootname, 0, NULL));
543 544 545
	CHECK(dst_key_fromdns(keyname, viewclass, &rrdatabuf,
			      mctx, &dstkey));

546
	*target = dstkey;
547 548 549
	return (ISC_R_SUCCESS);

 cleanup:
550 551
	if (result == DST_R_NOCRYPTO) {
		cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
552 553
			    "ignoring %s key for '%s': no crypto support",
			    managed ? "managed" : "trusted",
554 555 556 557
			    keynamestr);
		result = ISC_R_SUCCESS;
	} else {
		cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
558 559
			    "configuring %s key for '%s': %s",
			    managed ? "managed" : "trusted",
560 561 562
			    keynamestr, isc_result_totext(result));
		result = ISC_R_FAILURE;
	}
563 564 565 566 567 568 569

	if (dstkey != NULL)
		dst_key_free(&dstkey);

	return (result);
}

570 571 572
static isc_result_t
load_view_keys(const cfg_obj_t *keys, const cfg_obj_t *vconfig,
	       dns_view_t *view, isc_boolean_t managed, isc_mem_t *mctx)
573 574
{
	const cfg_listelt_t *elt, *elt2;
575 576 577
	const cfg_obj_t *key, *keylist;
	dst_key_t *dstkey = NULL;
	isc_result_t result = ISC_R_SUCCESS;
578 579 580 581 582 583 584 585 586 587

	for (elt = cfg_list_first(keys);
	     elt != NULL;
	     elt = cfg_list_next(elt)) {
		keylist = cfg_listelt_value(elt);

		for (elt2 = cfg_list_first(keylist);
		     elt2 != NULL;
		     elt2 = cfg_list_next(elt2)) {
			key = cfg_listelt_value(elt2);
588 589 590 591
			CHECK(dstkey_fromconfig(vconfig, key, managed,
						&dstkey, mctx));
			CHECK(dns_keytable_add(view->secroots, managed,
					       &dstkey));
592 593 594 595
		}
	}

 cleanup:
596
	return (result);
597 598
}

599
/*%
600
 * Configure DNSSEC keys for a view.
601
 *
602
 * The per-view configuration values and the server-global defaults are read
603
 * from 'vconfig' and 'config'.
604 605
 */
static isc_result_t
606 607 608
configure_view_dnsseckeys(dns_view_t *view, const cfg_obj_t *vconfig,
			  const cfg_obj_t *config, const cfg_obj_t *bindkeys,
			  isc_boolean_t auto_dlv, isc_mem_t *mctx)
609
{
610
	isc_result_t result = ISC_R_SUCCESS;
611 612
	const cfg_obj_t *view_keys = NULL;
	const cfg_obj_t *global_keys = NULL;
613
	const cfg_obj_t *global_managed_keys = NULL;
614
	const cfg_obj_t *builtin_keys = NULL;
615
	const cfg_obj_t *builtin_managed_keys = NULL;
616
	const cfg_obj_t *maps[4];
617
	const cfg_obj_t *voptions = NULL;
618 619
	const cfg_obj_t *options = NULL;
	int i = 0;
620

621 622 623 624 625
	/* We don't need trust anchors for the _bind view */
	if (strcmp(view->name, "_bind") == 0) {
		view->secroots = NULL;
		return (ISC_R_SUCCESS);
	}
626

627
	if (vconfig != NULL) {
628
		voptions = cfg_tuple_get(vconfig, "options");
629
		if (voptions != NULL) {
630 631
			(void) cfg_map_get(voptions, "trusted-keys",
					   &view_keys);
632 633 634
			maps[i++] = voptions;
		}
	}
635

636 637
	if (config != NULL) {
		(void)cfg_map_get(config, "trusted-keys", &global_keys);
638
		(void)cfg_map_get(config, "managed-keys", &global_managed_keys);
639 640 641
		(void)cfg_map_get(config, "options", &options);
		if (options != NULL) {
			maps[i++] = options;
642
		}
643
	}
644

645 646 647
	maps[i++] = ns_g_defaults;
	maps[i] = NULL;

648 649 650 651 652 653 654 655 656 657 658 659 660
	if (view->secroots != NULL)
		dns_keytable_detach(&view->secroots);
	result = dns_keytable_create(mctx, &view->secroots);
	if (result != ISC_R_SUCCESS) {
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
			      NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
			      "couldn't create keytable");
		return (ISC_R_UNEXPECTED);
	}

	if (global_managed_keys != NULL)
		ns_g_server->managedkeys = ISC_TRUE;

661 662 663
	if (auto_dlv) {
		isc_log_write(ns_g_lctx, DNS_LOGCATEGORY_SECURITY,
			      NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
664 665
			      "using built-in trusted-keys for view %s",
			      view->name);
666 667

		/*
668
		 * If bind.keys exists, it overrides the managed-keys
669 670
		 * clause hard-coded in ns_g_config.
		 */
671
		if (bindkeys != NULL) {
672 673
			(void)cfg_map_get(bindkeys, "trusted-keys",
					  &builtin_keys);
674 675 676
			(void)cfg_map_get(bindkeys, "managed-keys",
					  &builtin_managed_keys);
		} else {
677 678
			(void)cfg_map_get(ns_g_config, "trusted-keys",
					  &builtin_keys);
679 680 681 682 683 684 685
			(void)cfg_map_get(ns_g_config, "managed-keys",
					  &builtin_managed_keys);
		}

		if (builtin_managed_keys != NULL)
			ns_g_server->managedkeys = ISC_TRUE;
		CHECK(load_view_keys(builtin_keys, vconfig, view,
Automatic Updater's avatar
Automatic Updater committed
686
				     ISC_FALSE, mctx));
687

688 689
		if (strcmp(view->name, "_meta") == 0)
			CHECK(load_view_keys(builtin_managed_keys, vconfig,
Automatic Updater's avatar
Automatic Updater committed
690
					     view, ISC_TRUE, mctx));
691 692
	}

693 694
	CHECK(load_view_keys(view_keys, vconfig, view, ISC_FALSE, mctx));
	CHECK(load_view_keys(global_keys, vconfig, view, ISC_FALSE, mctx));
695

696 697 698
	if (strcmp(view->name, "_meta") == 0)
		CHECK(load_view_keys(global_managed_keys, vconfig, view,
			       ISC_TRUE, mctx));
Automatic Updater's avatar
Automatic Updater committed
699

700 701
  cleanup:
	return (result);
702
}
703

704
static isc_result_t
705
mustbesecure(const cfg_obj_t *mbs, dns_resolver_t *resolver)
706
{
707 708
	const cfg_listelt_t *element;
	const cfg_obj_t *obj;
709 710 711 712 713 714
	const char *str;
	dns_fixedname_t fixed;
	dns_name_t *name;
	isc_boolean_t value;
	isc_result_t result;
	isc_buffer_t b;
Automatic Updater's avatar
Automatic Updater committed
715

716 717 718 719 720 721 722 723 724 725
	dns_fixedname_init(&fixed);
	name = dns_fixedname_name(&fixed);
	for (element = cfg_list_first(mbs);
	     element != NULL;
	     element = cfg_list_next(element))
	{
		obj = cfg_listelt_value(element);
		str = cfg_obj_asstring(cfg_tuple_get(obj, "name"));
		isc_buffer_init(&b, str, strlen(str));
		isc_buffer_add(&b, strlen(str));
726
		CHECK(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
727 728 729 730 731
		value = cfg_obj_asboolean(cfg_tuple_get(obj, "value"));
		CHECK(dns_resolver_setmustbesecure(resolver, name, value));
	}

	result = ISC_R_SUCCESS;
Automatic Updater's avatar
Automatic Updater committed
732

733 734 735
 cleanup:
	return (result);
}
736

737
/*%
738 739 740
 * Get a dispatch appropriate for the resolver of a given view.
 */
static isc_result_t
741
get_view_querysource_dispatch(const cfg_obj_t **maps,
742 743
			      int af, dns_dispatch_t **dispatchp,
			      isc_boolean_t is_firstview)
744 745 746 747 748
{
	isc_result_t result;
	dns_dispatch_t *disp;
	isc_sockaddr_t sa;
	unsigned int attrs, attrmask;
749
	const cfg_obj_t *obj = NULL;
750
	unsigned int maxdispatchbuffers;
751 752 753 754 755 756 757 758

	/*
	 * Make compiler happy.
	 */
	result = ISC_R_FAILURE;

	switch (af) {
	case AF_INET:
759 760
		result = ns_config_get(maps, "query-source", &obj);
		INSIST(result == ISC_R_SUCCESS);
761 762
		break;
	case AF_INET6:
763 764
		result = ns_config_get(maps, "query-source-v6", &obj);
		INSIST(result == ISC_R_SUCCESS);
765 766 767 768
		break;
	default:
		INSIST(0);
	}
769

770
	sa = *(cfg_obj_assockaddr(obj));
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
	INSIST(isc_sockaddr_pf(&sa) == af);

	/*
	 * If we don't support this address family, we're done!
	 */
	switch (af) {
	case AF_INET:
		result = isc_net_probeipv4();
		break;
	case AF_INET6:
		result = isc_net_probeipv6();
		break;
	default:
		INSIST(0);
	}
	if (result != ISC_R_SUCCESS)
		return (ISC_R_SUCCESS);

	/*
	 * Try to find a dispatcher that we can share.
	 */
	attrs = 0;
	attrs |= DNS_DISPATCHATTR_UDP;
	switch (af) {
	case AF_INET:
		attrs |= DNS_DISPATCHATTR_IPV4;
		break;
	case AF_INET6:
		attrs |= DNS_DISPATCHATTR_IPV6;
		break;
	}
802 803 804 805 806 807 808 809 810 811 812 813 814
	if (isc_sockaddr_getport(&sa) == 0) {
		attrs |= DNS_DISPATCHATTR_EXCLUSIVE;
		maxdispatchbuffers = 4096;
	} else {
		INSIST(obj != NULL);
		if (is_firstview) {
			cfg_obj_log(obj, ns_g_lctx, ISC_LOG_INFO,
				    "using specific query-source port "
				    "suppresses port randomization and can be "
				    "insecure.");
		}
		maxdispatchbuffers = 1000;
	}
815

816 817 818 819 820 821 822 823 824
	attrmask = 0;
	attrmask |= DNS_DISPATCHATTR_UDP;
	attrmask |= DNS_DISPATCHATTR_TCP;
	attrmask |= DNS_DISPATCHATTR_IPV4;
	attrmask |= DNS_DISPATCHATTR_IPV6;

	disp = NULL;
	result = dns_dispatch_getudp(ns_g_dispatchmgr, ns_g_socketmgr,
				     ns_g_taskmgr, &sa, 4096,
825
				     maxdispatchbuffers, 32768, 16411, 16433,
826
				     attrs, attrmask, &disp);
827
	if (result != ISC_R_SUCCESS) {
828 829 830 831 832 833 834 835 836 837 838 839 840 841
		isc_sockaddr_t any;
		char buf[ISC_SOCKADDR_FORMATSIZE];

		switch (af) {
		case AF_INET:
			isc_sockaddr_any(&any);
			break;
		case AF_INET6:
			isc_sockaddr_any6(&any);
			break;
		}
		if (isc_sockaddr_equal(&sa, &any))
			return (ISC_R_SUCCESS);
		isc_sockaddr_format(&sa, buf, sizeof(buf));
842
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
843
			      NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
844 845
			      "could not get query source dispatcher (%s)",
			      buf);
846
		return (result);
847
	}
848 849 850 851 852 853

	*dispatchp = disp;

	return (ISC_R_SUCCESS);
}

854
static isc_result_t
855
configure_order(dns_order_t *order, const cfg_obj_t *ent) {
856 857
	dns_rdataclass_t rdclass;
	dns_rdatatype_t rdtype;
858
	const cfg_obj_t *obj;
859 860 861 862 863
	dns_fixedname_t fixed;
	unsigned int mode = 0;
	const char *str;
	isc_buffer_t b;
	isc_result_t result;
864
	isc_boolean_t addroot;
865 866 867 868 869 870 871 872 873 874 875 876

	result = ns_config_getclass(cfg_tuple_get(ent, "class"),
				    dns_rdataclass_any, &rdclass);
	if (result != ISC_R_SUCCESS)
		return (result);

	result = ns_config_gettype(cfg_tuple_get(ent, "type"),
				   dns_rdatatype_any, &rdtype);
	if (result != ISC_R_SUCCESS)
		return (result);

	obj = cfg_tuple_get(ent, "name");
877
	if (cfg_obj_isstring(obj))
878 879 880
		str = cfg_obj_asstring(obj);
	else
		str = "*";
881
	addroot = ISC_TF(strcmp(str, "*") == 0);
882 883 884 885
	isc_buffer_init(&b, str, strlen(str));
	isc_buffer_add(&b, strlen(str));
	dns_fixedname_init(&fixed);
	result = dns_name_fromtext(dns_fixedname_name(&fixed), &b,
886
				   dns_rootname, 0, NULL);
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
	if (result != ISC_R_SUCCESS)
		return (result);

	obj = cfg_tuple_get(ent, "ordering");
	INSIST(cfg_obj_isstring(obj));
	str = cfg_obj_asstring(obj);
	if (!strcasecmp(str, "fixed"))
		mode = DNS_RDATASETATTR_FIXEDORDER;
	else if (!strcasecmp(str, "random"))
		mode = DNS_RDATASETATTR_RANDOMIZE;
	else if (!strcasecmp(str, "cyclic"))
		mode = 0;
	else
		INSIST(0);

902 903 904
	/*
	 * "*" should match everything including the root (BIND 8 compat).
	 * As dns_name_matcheswildcard(".", "*.") returns FALSE add a
Mark Andrews's avatar
Mark Andrews committed
905
	 * explicit entry for "." when the name is "*".
906 907 908 909 910 911 912 913
	 */
	if (addroot) {
		result = dns_order_add(order, dns_rootname,
				       rdtype, rdclass, mode);
		if (result != ISC_R_SUCCESS)
			return (result);
	}

914 915 916 917
	return (dns_order_add(order, dns_fixedname_name(&fixed),
			      rdtype, rdclass, mode));
}

918
static isc_result_t
919
configure_peer(const cfg_obj_t *cpeer, isc_mem_t *mctx, dns_peer_t **peerp) {
920 921
	isc_netaddr_t na;
	dns_peer_t *peer;
922
	const cfg_obj_t *obj;
923
	const char *str;
924
	isc_result_t result;
925
	unsigned int prefixlen;
926

927
	cfg_obj_asnetprefix(cfg_map_getname(cpeer), &na, &prefixlen);
928 929

	peer = NULL;
930
	result = dns_peer_newprefix(mctx, &na, prefixlen, &peer);
931 932 933 934 935 936
	if (result != ISC_R_SUCCESS)
		return (result);

	obj = NULL;
	(void)cfg_map_get(cpeer, "bogus", &obj);
	if (obj != NULL)
937
		CHECK(dns_peer_setbogus(peer, cfg_obj_asboolean(obj)));
938 939 940 941

	obj = NULL;
	(void)cfg_map_get(cpeer, "provide-ixfr", &obj);
	if (obj != NULL)
942
		CHECK(dns_peer_setprovideixfr(peer, cfg_obj_asboolean(obj)));
943 944 945 946

	obj = NULL;
	(void)cfg_map_get(cpeer, "request-ixfr", &obj);
	if (obj != NULL)
947
		CHECK(dns_peer_setrequestixfr(peer, cfg_obj_asboolean(obj)));
948

949 950 951 952 953
	obj = NULL;
	(void)cfg_map_get(cpeer, "request-nsid", &obj);
	if (obj != NULL)
		CHECK(dns_peer_setrequestnsid(peer, cfg_obj_asboolean(obj)));

954 955 956
	obj = NULL;
	(void)cfg_map_get(cpeer, "edns", &obj);
	if (obj != NULL)
957
		CHECK(dns_peer_setsupportedns(peer, cfg_obj_asboolean(obj)));
958

959 960 961 962 963 964 965 966 967 968 969
	obj = NULL;
	(void)cfg_map_get(cpeer, "edns-udp-size", &obj);
	if (obj != NULL) {
		isc_uint32_t udpsize = cfg_obj_asuint32(obj);
		if (udpsize < 512)
			udpsize = 512;
		if (udpsize > 4096)
			udpsize = 4096;
		CHECK(dns_peer_setudpsize(peer, (isc_uint16_t)udpsize));
	}

970 971 972 973 974 975 976 977 978 979 980
	obj = NULL;
	(void)cfg_map_get(cpeer, "max-udp-size", &obj);
	if (obj != NULL) {
		isc_uint32_t udpsize = cfg_obj_asuint32(obj);
		if (udpsize < 512)
			udpsize = 512;
		if (udpsize > 4096)
			udpsize = 4096;
		CHECK(dns_peer_setmaxudp(peer, (isc_uint16_t)udpsize));
	}

981 982 983
	obj = NULL;
	(void)cfg_map_get(cpeer, "transfers", &obj);
	if (obj != NULL)
984
		CHECK(dns_peer_settransfers(peer, cfg_obj_asuint32(obj)));
985 986 987 988 989 990

	obj = NULL;
	(void)cfg_map_get(cpeer, "transfer-format", &obj);
	if (obj != NULL) {
		str = cfg_obj_asstring(obj);
		if (strcasecmp(str, "many-answers") == 0)
991 992
			CHECK(dns_peer_settransferformat(peer,
							 dns_many_answers));
993
		else if (strcasecmp(str, "one-answer") == 0)
994 995
			CHECK(dns_peer_settransferformat(peer,
							 dns_one_answer));
996 997 998 999 1000 1001 1002 1003 1004 1005 1006
		else
			INSIST(0);
	}

	obj = NULL;
	(void)cfg_map_get(cpeer, "keys", &obj);
	if (obj != NULL) {
		result = dns_peer_setkeybycharp(peer, cfg_obj_asstring(obj));
		if (result != ISC_R_SUCCESS)
			goto cleanup;
	}
1007 1008

	obj = NULL;
1009
	if (na.family == AF_INET)
1010 1011 1012 1013 1014 1015 1016 1017
		(void)cfg_map_get(cpeer, "transfer-source", &obj);
	else
		(void)cfg_map_get(cpeer, "transfer-source-v6", &obj);
	if (obj != NULL) {
		result = dns_peer_settransfersource(peer,
						    cfg_obj_assockaddr(obj));
		if (result != ISC_R_SUCCESS)
			goto cleanup;
1018
		ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
1019