server.c 142 KB
Newer Older
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2004-2006  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2003  Internet Software Consortium.
4
 *
5 6 7
 * Permission to use, copy, modify, and distribute this software for any
 * 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.470 2006/12/21 06:02:29 marka Exp $ */
19 20

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

22 23 24 25
#include <config.h>

#include <stdlib.h>

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

44
#include <isccfg/namedconf.h>
45 46

#include <bind9/check.h>
47

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

79
#include <dst/dst.h>
80
#include <dst/result.h>
81

82
#include <named/client.h>
83
#include <named/config.h>
84
#include <named/control.h>
85
#include <named/interfacemgr.h>
86
#include <named/log.h>
87
#include <named/logconf.h>
88
#include <named/lwresd.h>
89
#include <named/main.h>
90
#include <named/os.h>
Bob Halley's avatar
Bob Halley committed
91
#include <named/server.h>
92 93 94
#include <named/tkeyconf.h>
#include <named/tsigconf.h>
#include <named/zoneconf.h>
95 96 97 98
#ifdef HAVE_LIBSCF
#include <named/ns_smf_globals.h>
#include <stdlib.h>
#endif
99

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

#define CHECKM(op, msg) \
	do { result = (op); 				  	  \
	       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
122 123 124 125 126 127 128 129 130 131 132 133 134
#define CHECKMF(op, msg, file) \
	do { result = (op); 				  	  \
	       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)						  \

135 136 137 138 139 140
#define CHECKFATAL(op, msg) \
	do { result = (op); 				  	  \
	       if (result != ISC_R_SUCCESS)			  \
			fatal(msg, result);			  \
	} while (0)						  \

141 142 143 144 145 146 147
struct ns_dispatch {
	isc_sockaddr_t			addr;
	unsigned int			dispatchgen;
	dns_dispatch_t			*dispatch;
	ISC_LINK(struct ns_dispatch)	link;
};

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
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;
};

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 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
/*
 * 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 */
	{ "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 },
	/* LOCALLY ASSIGNED LOCAL ADDRES S SCOPE */
	{ "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 }
};

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
#ifdef HAVE_LIBXML2

void
server_httpd_create(ns_server_t *server);

static isc_result_t
render_index(const char *url, const char *querystring, void *args,
	     unsigned int *retcode, const char **retmsg, const char **mimetype,
	     isc_buffer_t *b, isc_httpdfree_t **freecb,
	     void **freecb_args);

static isc_result_t
render_xsl(const char *url, const char *querystring, void *args,
	   unsigned int *retcode, const char **retmsg, const char **mimetype,
	   isc_buffer_t *b, isc_httpdfree_t **freecb,
	   void **freecb_args);

void
tree_walk(xmlTextWriterPtr writer, isc_mib_t *mib, isc_mibnode_t *node);

void
server_generatexml(ns_server_t *server, unsigned int *buflen, xmlChar **buf);

#endif /* HAVE_LIBXML2 */

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

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

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

262
static isc_result_t
263 264
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);
265

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

270
static isc_result_t
271 272
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,
273
	       cfg_aclconfctx_t *aclconf);
274

275 276 277
static void
end_reserved_dispatches(ns_server_t *server, isc_boolean_t all);

278
/*%
279 280 281 282 283
 * Configure a single view ACL at '*aclp'.  Get its configuration by
 * calling 'getvcacl' (for per-view configuration) and maybe 'getscacl'
 * (for a global default).
 */
static isc_result_t
284
configure_view_acl(const cfg_obj_t *vconfig, const cfg_obj_t *config,
285
		   const char *aclname, cfg_aclconfctx_t *actx,
286
		   isc_mem_t *mctx, dns_acl_t **aclp)
287 288
{
	isc_result_t result;
289 290
	const cfg_obj_t *maps[3];
	const cfg_obj_t *aclobj = NULL;
291 292
	int i = 0;

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

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

312 313
	result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
				    actx, mctx, aclp);
314 315 316 317

	return (result);
}

318
static isc_result_t
319
configure_view_dnsseckey(const cfg_obj_t *vconfig, const cfg_obj_t *key,
320 321 322
			 dns_keytable_t *keytable, isc_mem_t *mctx)
{
	dns_rdataclass_t viewclass;
323
	dns_rdata_dnskey_t keystruct;
324
	isc_uint32_t flags, proto, alg;
325
	const char *keystr, *keynamestr;
326 327 328 329 330 331 332 333 334 335 336
	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;

337 338 339 340 341 342 343
	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"));

	if (vconfig == NULL)
344
		viewclass = dns_rdataclass_in;
345
	else {
346
		const cfg_obj_t *classobj = cfg_tuple_get(vconfig, "class");
347 348
		CHECK(ns_config_getclass(classobj, dns_rdataclass_in,
					 &viewclass));
349
	}
350
	keystruct.common.rdclass = viewclass;
351
	keystruct.common.rdtype = dns_rdatatype_dnskey;
352
	/*
353
	 * The key data in keystruct is not dynamically allocated.
354 355 356 357 358
	 */
	keystruct.mctx = NULL;

	ISC_LINK_INIT(&keystruct.common, link);

359
	if (flags > 0xffff)
360
		CHECKM(ISC_R_RANGE, "key flags");
361
	if (proto > 0xff)
362
		CHECKM(ISC_R_RANGE, "key protocol");
363
	if (alg > 0xff)
364
		CHECKM(ISC_R_RANGE, "key algorithm");
365 366 367
	keystruct.flags = (isc_uint16_t)flags;
	keystruct.protocol = (isc_uint8_t)proto;
	keystruct.algorithm = (isc_uint8_t)alg;
368 369 370 371

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

372
	keystr = cfg_obj_asstring(cfg_tuple_get(key, "key"));
373
	CHECK(isc_base64_decodestring(keystr, &keydatabuf));
374 375 376 377
	isc_buffer_usedregion(&keydatabuf, &r);
	keystruct.datalen = r.length;
	keystruct.data = r.base;

378 379 380 381 382 383 384
	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,
			    "trusted key '%s' has a weak exponent",
			    keynamestr);

385 386 387 388 389
	CHECK(dns_rdata_fromstruct(NULL,
				   keystruct.common.rdclass,
				   keystruct.common.rdtype,
				   &keystruct, &rrdatabuf));
	dns_fixedname_init(&fkeyname);
390 391
	isc_buffer_init(&namebuf, keynamestr, strlen(keynamestr));
	isc_buffer_add(&namebuf, strlen(keynamestr));
392 393 394 395 396 397 398 399 400 401 402
	CHECK(dns_name_fromtext(keyname, &namebuf,
				dns_rootname, ISC_FALSE,
				NULL));
	CHECK(dst_key_fromdns(keyname, viewclass, &rrdatabuf,
			      mctx, &dstkey));

	CHECK(dns_keytable_add(keytable, &dstkey));
	INSIST(dstkey == NULL);
	return (ISC_R_SUCCESS);

 cleanup:
403 404 405 406 407 408 409 410 411 412 413
	if (result == DST_R_NOCRYPTO) {
		cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
			    "ignoring trusted key for '%s': no crypto support",
			    keynamestr);
		result = ISC_R_SUCCESS;
	} else {
		cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
			    "configuring trusted key for '%s': %s",
			    keynamestr, isc_result_totext(result));
		result = ISC_R_FAILURE;
	}
414 415 416 417 418 419 420

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

	return (result);
}

421
/*%
422 423
 * Configure DNSSEC keys for a view.  Currently used only for
 * the security roots.
424
 *
425 426
 * The per-view configuration values and the server-global defaults are read
 * from 'vconfig' and 'config'.  The variable to be configured is '*target'.
427 428
 */
static isc_result_t
429
configure_view_dnsseckeys(const cfg_obj_t *vconfig, const cfg_obj_t *config,
430
			  isc_mem_t *mctx, dns_keytable_t **target)
431 432
{
	isc_result_t result;
433 434 435 436 437
	const cfg_obj_t *keys = NULL;
	const cfg_obj_t *voptions = NULL;
	const cfg_listelt_t *element, *element2;
	const cfg_obj_t *keylist;
	const cfg_obj_t *key;
438
	dns_keytable_t *keytable = NULL;
439

440 441
	CHECK(dns_keytable_create(mctx, &keytable));

442 443
	if (vconfig != NULL)
		voptions = cfg_tuple_get(vconfig, "options");
444

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	keys = NULL;
	if (voptions != NULL)
		(void)cfg_map_get(voptions, "trusted-keys", &keys);
	if (keys == NULL)
		(void)cfg_map_get(config, "trusted-keys", &keys);

	for (element = cfg_list_first(keys);
	     element != NULL;
	     element = cfg_list_next(element))
	{
		keylist = cfg_listelt_value(element);
		for (element2 = cfg_list_first(keylist);
		     element2 != NULL;
		     element2 = cfg_list_next(element2))
		{
			key = cfg_listelt_value(element2);
			CHECK(configure_view_dnsseckey(vconfig, key,
462
						       keytable, mctx));
463
		}
464
	}
465

466 467 468 469
	dns_keytable_detach(target);
	*target = keytable; /* Transfer ownership. */
	keytable = NULL;
	result = ISC_R_SUCCESS;
470
	
471 472 473
 cleanup:
	return (result);
}
474

475
static isc_result_t
476
mustbesecure(const cfg_obj_t *mbs, dns_resolver_t *resolver)
477
{
478 479
	const cfg_listelt_t *element;
	const cfg_obj_t *obj;
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
	const char *str;
	dns_fixedname_t fixed;
	dns_name_t *name;
	isc_boolean_t value;
	isc_result_t result;
	isc_buffer_t b;
	
	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));
		CHECK(dns_name_fromtext(name, &b, dns_rootname,
498
					ISC_FALSE, NULL));
499 500 501 502 503 504 505 506 507
		value = cfg_obj_asboolean(cfg_tuple_get(obj, "value"));
		CHECK(dns_resolver_setmustbesecure(resolver, name, value));
	}

	result = ISC_R_SUCCESS;
	
 cleanup:
	return (result);
}
508

509
/*%
510 511 512
 * Get a dispatch appropriate for the resolver of a given view.
 */
static isc_result_t
513
get_view_querysource_dispatch(const cfg_obj_t **maps,
514 515 516 517 518 519
			      int af, dns_dispatch_t **dispatchp)
{
	isc_result_t result;
	dns_dispatch_t *disp;
	isc_sockaddr_t sa;
	unsigned int attrs, attrmask;
520
	const cfg_obj_t *obj = NULL;
521 522 523 524 525 526 527 528

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

	switch (af) {
	case AF_INET:
529 530
		result = ns_config_get(maps, "query-source", &obj);
		INSIST(result == ISC_R_SUCCESS);
531 532
		break;
	case AF_INET6:
533 534
		result = ns_config_get(maps, "query-source-v6", &obj);
		INSIST(result == ISC_R_SUCCESS);
535 536 537 538
		break;
	default:
		INSIST(0);
	}
539

540
	sa = *(cfg_obj_assockaddr(obj));
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
	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;
	}
	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,
				     1000, 32768, 16411, 16433,
				     attrs, attrmask, &disp);
583
	if (result != ISC_R_SUCCESS) {
584 585 586 587 588 589 590 591 592 593 594 595 596 597
		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));
598
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
599
			      NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
600 601
			      "could not get query source dispatcher (%s)",
			      buf);
602
		return (result);
603
	}
604 605 606 607 608 609

	*dispatchp = disp;

	return (ISC_R_SUCCESS);
}

610
static isc_result_t
611
configure_order(dns_order_t *order, const cfg_obj_t *ent) {
612 613
	dns_rdataclass_t rdclass;
	dns_rdatatype_t rdtype;
614
	const cfg_obj_t *obj;
615 616 617 618 619
	dns_fixedname_t fixed;
	unsigned int mode = 0;
	const char *str;
	isc_buffer_t b;
	isc_result_t result;
620
	isc_boolean_t addroot;
621 622 623 624 625 626 627 628 629 630 631 632

	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");
633
	if (cfg_obj_isstring(obj))
634 635 636
		str = cfg_obj_asstring(obj);
	else
		str = "*";
637
	addroot = ISC_TF(strcmp(str, "*") == 0);
638 639 640 641
	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,
642
				   dns_rootname, ISC_FALSE, NULL);
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	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);

658 659 660
	/*
	 * "*" should match everything including the root (BIND 8 compat).
	 * As dns_name_matcheswildcard(".", "*.") returns FALSE add a
Mark Andrews's avatar
Mark Andrews committed
661
	 * explicit entry for "." when the name is "*".
662 663 664 665 666 667 668 669
	 */
	if (addroot) {
		result = dns_order_add(order, dns_rootname,
				       rdtype, rdclass, mode);
		if (result != ISC_R_SUCCESS)
			return (result);
	}

670 671 672 673
	return (dns_order_add(order, dns_fixedname_name(&fixed),
			      rdtype, rdclass, mode));
}

674
static isc_result_t
675
configure_peer(const cfg_obj_t *cpeer, isc_mem_t *mctx, dns_peer_t **peerp) {
676 677
	isc_netaddr_t na;
	dns_peer_t *peer;
678
	const cfg_obj_t *obj;
679
	const char *str;
680
	isc_result_t result;
681
	unsigned int prefixlen;
682

683
	cfg_obj_asnetprefix(cfg_map_getname(cpeer), &na, &prefixlen);
684 685 686 687 688 689 690 691 692

	peer = NULL;
	result = dns_peer_new(mctx, &na, &peer);
	if (result != ISC_R_SUCCESS)
		return (result);

	obj = NULL;
	(void)cfg_map_get(cpeer, "bogus", &obj);
	if (obj != NULL)
693
		CHECK(dns_peer_setbogus(peer, cfg_obj_asboolean(obj)));
694 695 696 697

	obj = NULL;
	(void)cfg_map_get(cpeer, "provide-ixfr", &obj);
	if (obj != NULL)
698
		CHECK(dns_peer_setprovideixfr(peer, cfg_obj_asboolean(obj)));
699 700 701 702

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

705 706 707
	obj = NULL;
	(void)cfg_map_get(cpeer, "edns", &obj);
	if (obj != NULL)
708
		CHECK(dns_peer_setsupportedns(peer, cfg_obj_asboolean(obj)));
709

710 711 712 713 714 715 716 717 718 719 720
	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));
	}

721 722 723 724 725 726 727 728 729 730 731
	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));
	}

732 733 734
	obj = NULL;
	(void)cfg_map_get(cpeer, "transfers", &obj);
	if (obj != NULL)
735
		CHECK(dns_peer_settransfers(peer, cfg_obj_asuint32(obj)));
736 737 738 739 740 741

	obj = NULL;
	(void)cfg_map_get(cpeer, "transfer-format", &obj);
	if (obj != NULL) {
		str = cfg_obj_asstring(obj);
		if (strcasecmp(str, "many-answers") == 0)
742 743
			CHECK(dns_peer_settransferformat(peer,
							 dns_many_answers));
744
		else if (strcasecmp(str, "one-answer") == 0)
745 746
			CHECK(dns_peer_settransferformat(peer,
							 dns_one_answer));
747 748 749 750 751 752 753 754 755 756 757
		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;
	}
758 759

	obj = NULL;
760
	if (na.family == AF_INET)
761 762 763 764 765 766 767 768
		(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;
769
		ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
770
	}
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

	obj = NULL;
	if (na.family == AF_INET)
		(void)cfg_map_get(cpeer, "notify-source", &obj);
	else
		(void)cfg_map_get(cpeer, "notify-source-v6", &obj);
	if (obj != NULL) {
		result = dns_peer_setnotifysource(peer,
						  cfg_obj_assockaddr(obj));
		if (result != ISC_R_SUCCESS)
			goto cleanup;
		ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
	}

	obj = NULL;
	if (na.family == AF_INET)
		(void)cfg_map_get(cpeer, "query-source", &obj);
	else
		(void)cfg_map_get(cpeer, "query-source-v6", &obj);
	if (obj != NULL) {
		result = dns_peer_setquerysource(peer,
						 cfg_obj_assockaddr(obj));
		if (result != ISC_R_SUCCESS)
			goto cleanup;
		ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
	}

798 799 800 801 802 803 804 805
	*peerp = peer;
	return (ISC_R_SUCCESS);

 cleanup:
	dns_peer_detach(&peer);
	return (result);
}

806
static isc_result_t
807
disable_algorithms(const cfg_obj_t *disabled, dns_resolver_t *resolver) {
808
	isc_result_t result;
809 810
	const cfg_obj_t *algorithms;
	const cfg_listelt_t *element;
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
	const char *str;
	dns_fixedname_t fixed;
	dns_name_t *name;
	isc_buffer_t b;

	dns_fixedname_init(&fixed);
	name = dns_fixedname_name(&fixed);
	str = cfg_obj_asstring(cfg_tuple_get(disabled, "name"));
	isc_buffer_init(&b, str, strlen(str));
	isc_buffer_add(&b, strlen(str));
	CHECK(dns_name_fromtext(name, &b, dns_rootname, ISC_FALSE, NULL));

	algorithms = cfg_tuple_get(disabled, "algorithms");
	for (element = cfg_list_first(algorithms);
	     element != NULL;
	     element = cfg_list_next(element))
	{
		isc_textregion_t r;
		dns_secalg_t alg;

831
		DE_CONST(cfg_obj_asstring(cfg_listelt_value(element)), r.base);
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
		r.length = strlen(r.base);

		result = dns_secalg_fromtext(&alg, &r);
		if (result != ISC_R_SUCCESS) {
			isc_uint8_t ui;
			result = isc_parse_uint8(&ui, r.base, 10);
			alg = ui;
		}
		if (result != ISC_R_SUCCESS) {
			cfg_obj_log(cfg_listelt_value(element),
				    ns_g_lctx, ISC_LOG_ERROR,
				    "invalid algorithm");
			CHECK(result);
		}
		CHECK(dns_resolver_disable_algorithm(resolver, name, alg));
	}
 cleanup:
	return (result);
}

852
static isc_boolean_t
853 854
on_disable_list(const cfg_obj_t *disablelist, dns_name_t *zonename) {
	const cfg_listelt_t *element;
855 856 857
	dns_fixedname_t fixed;
	dns_name_t *name;
	isc_result_t result;
858
	const cfg_obj_t *value;
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
	const char *str;
	isc_buffer_t b;

	dns_fixedname_init(&fixed);
	name = dns_fixedname_name(&fixed);
	
	for (element = cfg_list_first(disablelist);
	     element != NULL;
	     element = cfg_list_next(element))
	{
		value = cfg_listelt_value(element);
		str = cfg_obj_asstring(value);
		isc_buffer_init(&b, str, strlen(str));
		isc_buffer_add(&b, strlen(str));
		result = dns_name_fromtext(name, &b, dns_rootname,
					   ISC_TRUE, NULL);
		RUNTIME_CHECK(result == ISC_R_SUCCESS);
		if (dns_name_equal(name, zonename))
			return (ISC_TRUE);
	}
	return (ISC_FALSE);
}

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
static void
check_dbtype(dns_zone_t **zonep, unsigned int dbtypec, const char **dbargv,
	     isc_mem_t *mctx)
{
	char **argv = NULL;
	unsigned int i;
	isc_result_t result;

	result = dns_zone_getdbtype(*zonep, &argv, mctx);
	if (result != ISC_R_SUCCESS) {
		dns_zone_detach(zonep);
		return;
	}

	/*
	 * Check that all the arguments match.
	 */
	for (i = 0; i < dbtypec; i++)
		if (argv[i] == NULL || strcmp(argv[i], dbargv[i]) != 0) {
			dns_zone_detach(zonep);
			break;
		}

	/*
	 * Check that there are not extra arguments.
	 */
	if (i == dbtypec && argv[i] != NULL)
		dns_zone_detach(zonep);
	isc_mem_free(mctx, argv);
}


914
/*
915 916
 * Configure 'view' according to 'vconfig', taking defaults from 'config'
 * where values are missing in 'vconfig'.
917
 *
918 919
 * When configuring the default view, 'vconfig' will be NULL and the
 * global defaults in 'config' used exclusively.
920
 */
921
static isc_result_t
922 923 924
configure_view(dns_view_t *view, const cfg_obj_t *config,
	       const cfg_obj_t *vconfig, isc_mem_t *mctx,
	       cfg_aclconfctx_t *actx, isc_boolean_t need_hints)
925
{
926 927 928 929 930 931 932 933
	const cfg_obj_t *maps[4];
	const cfg_obj_t *cfgmaps[3];
	const cfg_obj_t *options = NULL;
	const cfg_obj_t *voptions = NULL;
	const cfg_obj_t *forwardtype;
	const cfg_obj_t *forwarders;
	const cfg_obj_t *alternates;
	const cfg_obj_t *zonelist;
934
#ifdef DLZ
935 936 937
	const cfg_obj_t *dlz;
	unsigned int dlzargc;
	char **dlzargv;
938
#endif
939 940 941
	const cfg_obj_t *disabled;
	const cfg_obj_t *obj;
	const cfg_listelt_t *element;
942
	in_port_t port;
943
	dns_cache_t *cache = NULL;
944
	isc_result_t result;
945
	isc_uint32_t max_adb_size;
946
	isc_uint32_t max_cache_size;
947
	isc_uint32_t max_acache_size;
Mark Andrews's avatar
Mark Andrews committed
948
	isc_uint32_t lame_ttl;
Brian Wellington's avatar
Brian Wellington committed
949
	dns_tsig_keyring_t *ring;
950
	dns_view_t *pview = NULL;	/* Production view */
951
	isc_mem_t *cmctx;
952 953
	dns_dispatch_t *dispatch4 = NULL;
	dns_dispatch_t *dispatch6 = NULL;
954
	isc_boolean_t reused_cache = ISC_FALSE;
955
	int i;
956
	const char *str;
957
	dns_order_t *order = NULL;
958
	isc_uint32_t udpsize;
959
	unsigned int check = 0;
960
	dns_zone_t *zone = NULL;
961
	isc_uint32_t max_clients_per_query;
962 963 964 965 966
	const char *sep = ": view ";
	const char *viewname = view->name;
	const char *forview = " for view ";
	isc_boolean_t rfc1918;
	isc_boolean_t empty_zones_enable;
967
	const cfg_obj_t *disablelist = NULL;
968

969
	REQUIRE(DNS_VIEW_VALID(view));
970

971
	cmctx = NULL;
972

973
	if (config != NULL)
974
		(void)cfg_map_get(config, "options", &options);
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992

	i = 0;
	if (vconfig != NULL) {
		voptions = cfg_tuple_get(vconfig, "options");
		maps[i++] = voptions;
	}
	if (options != NULL)
		maps[i++] = options;
	maps[i++] = ns_g_defaults;
	maps[i] = NULL;

	i = 0;
	if (voptions != NULL)
		cfgmaps[i++] = voptions;
	if (config != NULL)
		cfgmaps[i++] = config;
	cfgmaps[i] = NULL;

993 994 995 996 997 998
	if (!strcmp(viewname, "_default")) {
		sep = "";
		viewname = "";
		forview = "";
	}

999 1000 1001
	/*
	 * Set the view's port number for outgoing queries.
	 */
1002
	CHECKM(ns_config_getport(config, &port), "port");
1003
	dns_view_setdstport(view, port);
1004

1005 1006
	/*
	 * Create additional cache for this view and zones under the view
1007 1008
	 * if explicitly enabled.
	 * XXX950 default to on.
1009 1010
	 */
	obj = NULL;
1011 1012
	(void)ns_config_get(maps, "acache-enable", &obj);
	if (obj != NULL && cfg_obj_asboolean(obj)) {
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
		cmctx = NULL;
		CHECK(isc_mem_create(0, 0, &cmctx));
		CHECK(dns_acache_create(&view->acache, cmctx, ns_g_taskmgr,
					ns_g_timermgr));
		isc_mem_detach(&cmctx);
	}
	if (view->acache != NULL) {
		obj = NULL;
		result = ns_config_get(maps, "acache-cleaning-interval", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_acache_setcleaninginterval(view->acache,
					       cfg_obj_asuint32(obj) * 60);

		obj = NULL;
		result = ns_config_get(maps, "max-acache-size", &obj);
		INSIST(result == ISC_R_SUCCESS);
		if (cfg_obj_isstring(obj)) {
			str = cfg_obj_asstring(obj);
			INSIST(strcasecmp(str, "unlimited") == 0);
			max_acache_size = ISC_UINT32_MAX;
		} else {
			isc_resourcevalue_t value;

			value = cfg_obj_asuint64(obj);
			if (value > ISC_UINT32_MAX) {
				cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
					    "'max-acache-size "
					    "%" ISC_PRINT_QUADFORMAT
					    "d' is too large",
					    value);
				result = ISC_R_RANGE;
				goto cleanup;
			}
			max_acache_size = (isc_uint32_t)value;
		}
		dns_acache_setcachesize(view->acache, max_acache_size);
	}

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	/*
	 * Configure the zones.
	 */
	zonelist = NULL;
	if (voptions != NULL)
		(void)cfg_map_get(voptions, "zone", &zonelist);
	else
		(void)cfg_map_get(config, "zone", &zonelist);
	for (element = cfg_list_first(zonelist);
	     element != NULL;
	     element = cfg_list_next(element))
	{
1063
		const cfg_obj_t *zconfig = cfg_listelt_value(element);
1064 1065 1066 1067
		CHECK(configure_zone(config, zconfig, vconfig, mctx, view,
				     actx));
	}

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
#ifdef DLZ
	/*
	 * Create Dynamically Loadable Zone driver.
	 */
	dlz = NULL;
	if (voptions != NULL)
		(void)cfg_map_get(voptions, "dlz", &dlz);
	else
		(void)cfg_map_get(config, "dlz", &dlz);

	obj = NULL;
	if (dlz != NULL) {
		(void)cfg_map_get(cfg_tuple_get(dlz, "options"),
				  "database", &obj);
		if (obj != NULL) {
			char *s = isc_mem_strdup(mctx, cfg_obj_asstring(obj));
			if (s == NULL) {
				result = ISC_R_NOMEMORY;
				goto cleanup;
			}
			
			result = dns_dlzstrtoargv(mctx, s, &dlzargc, &dlzargv);
			if (result != ISC_R_SUCCESS) {
				isc_mem_free(mctx, s);
				goto cleanup;
			}

			obj = cfg_tuple_get(dlz, "name");
			result = dns_dlzcreate(mctx, cfg_obj_asstring(obj),
					       dlzargv[0], dlzargc, dlzargv,
					       &view->dlzdatabase);
			isc_mem_free(mctx, s);
			isc_mem_put(mctx, dlzargv, dlzargc * sizeof(*dlzargv));
1101
			if (result != ISC_R_SUCCESS)
1102 1103 1104 1105 1106
				goto cleanup;
		}
	}
#endif

1107
	/*
1108 1109 1110
	 * Configure the view's cache.  Try to reuse an existing
	 * cache if possible, otherwise create a new cache.
	 * Note that the ADB is not preserved in either case.
1111 1112
	 *
	 * XXX Determining when it is safe to reuse a cache is
1113 1114 1115 1116 1117 1118
	 * tricky.  When the view's configuration changes, the cached
	 * data may become invalid because it reflects our old
	 * view of the world.  As more view attributes become
	 * configurable, we will have to add code here to check
	 * whether they have changed in ways that could
	 * invalidate the cache.
1119
	 */
1120 1121 1122 1123 1124 1125 1126 1127
	result = dns_viewlist_find(&ns_g_server->viewlist,
				   view->name, view->rdclass,
				   &pview);
	if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
		goto cleanup;
	if (pview != NULL) {
		INSIST(pview->cache != NULL);
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1128 1129
			      NS_LOGMODULE_SERVER, ISC_LOG_DEBUG(3),
			      "reusing existing cache");
1130
		reused_cache = ISC_TRUE;
1131 1132 1133
		dns_cache_attach(pview->cache, &cache);
		dns_view_detach(&pview);
	} else {
1134 1135
		CHECK(isc_mem_create(0, 0, &cmctx));
		CHECK(dns_cache_create(cmctx, ns_g_taskmgr, ns_g_timermgr,
1136 1137
				       view->rdclass, "rbt", 0, NULL, &cache));
	}
1138
	dns_view_setcache(view, cache);
1139

1140 1141 1142 1143 1144 1145
	/*
	 * cache-file cannot be inherited if views are present, but this
	 * should be caught by the configuration checking stage.
	 */
	obj = NULL;
	result = ns_config_get(maps, "cache-file", &obj);
1146
	if (result == ISC_R_SUCCESS && strcmp(view->name, "_bind") != 0) {
1147
		CHECK(dns_cache_setfilename(cache, cfg_obj_asstring(obj)));
1148 1149 1150 1151
		if (!reused_cache)
			CHECK(dns_cache_load(cache));
	}

Brian Wellington's avatar