server.c 301 KB
Newer Older
1
/*
2
 * Copyright (C) 2004-2015  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
/*! \file */
David Lawrence's avatar
David Lawrence committed
19

20 21 22
#include <config.h>

#include <stdlib.h>
23
#include <unistd.h>
24
#include <limits.h>
25 26 27
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
28

29
#include <isc/aes.h>
30
#include <isc/app.h>
31
#include <isc/base64.h>
32
#include <isc/dir.h>
33
#include <isc/entropy.h>
34
#include <isc/file.h>
35
#include <isc/hash.h>
36
#include <isc/hex.h>
37
#include <isc/hmacsha.h>
38
#include <isc/httpd.h>
39
#include <isc/lex.h>
40
#include <isc/meminfo.h>
41
#include <isc/parseint.h>
42
#include <isc/portset.h>
43
#include <isc/print.h>
44
#include <isc/random.h>
45
#include <isc/refcount.h>
46
#include <isc/resource.h>
47
#include <isc/sha2.h>
Mark Andrews's avatar
Mark Andrews committed
48
#include <isc/socket.h>
49
#include <isc/stat.h>
50
#include <isc/stats.h>
51
#include <isc/stdio.h>
52
#include <isc/string.h>
53
#include <isc/task.h>
54
#include <isc/timer.h>
Michael Graff's avatar
Michael Graff committed
55
#include <isc/util.h>
56
#include <isc/xml.h>
57

Evan Hunt's avatar
Evan Hunt committed
58
#include <isccfg/grammar.h>
59
#include <isccfg/namedconf.h>
60 61

#include <bind9/check.h>
62

63
#include <dns/acache.h>
64
#include <dns/adb.h>
Evan Hunt's avatar
Evan Hunt committed
65
#include <dns/badcache.h>
66
#include <dns/cache.h>
67
#include <dns/db.h>
Bob Halley's avatar
Bob Halley committed
68
#include <dns/dispatch.h>
69
#include <dns/dlz.h>
70
#include <dns/dns64.h>
71
#include <dns/forward.h>
72
#include <dns/journal.h>
73
#include <dns/keytable.h>
74
#include <dns/keyvalues.h>
75
#include <dns/lib.h>
76
#include <dns/master.h>
77
#include <dns/masterdump.h>
Evan Hunt's avatar
Evan Hunt committed
78
#include <dns/nta.h>
79
#include <dns/order.h>
80
#include <dns/peer.h>
81
#include <dns/portlist.h>
82
#include <dns/private.h>
83
#include <dns/rbt.h>
84
#include <dns/rdataclass.h>
85
#include <dns/rdatalist.h>
86
#include <dns/rdataset.h>
87
#include <dns/rdatastruct.h>
88
#include <dns/resolver.h>
89
#include <dns/rootns.h>
Evan Hunt's avatar
Evan Hunt committed
90
#include <dns/rriterator.h>
91
#include <dns/secalg.h>
92
#include <dns/soa.h>
93
#include <dns/stats.h>
94
#include <dns/tkey.h>
95
#include <dns/tsig.h>
Evan Hunt's avatar
Evan Hunt committed
96
#include <dns/ttl.h>
97
#include <dns/view.h>
98
#include <dns/zone.h>
99
#include <dns/zt.h>
100

101
#include <dst/dst.h>
102
#include <dst/result.h>
103

104
#include <named/client.h>
105
#include <named/config.h>
106
#include <named/control.h>
Evan Hunt's avatar
Evan Hunt committed
107
#include <named/geoip.h>
108
#include <named/interfacemgr.h>
109
#include <named/log.h>
110
#include <named/logconf.h>
111
#include <named/lwresd.h>
112
#include <named/main.h>
113
#include <named/os.h>
Bob Halley's avatar
Bob Halley committed
114
#include <named/server.h>
115
#include <named/statschannel.h>
116 117 118
#include <named/tkeyconf.h>
#include <named/tsigconf.h>
#include <named/zoneconf.h>
119 120 121 122
#ifdef HAVE_LIBSCF
#include <named/ns_smf_globals.h>
#include <stdlib.h>
#endif
Evan Hunt's avatar
Evan Hunt committed
123 124 125
#ifdef HAVE_GEOIP
#include <named/geoip.h>
#endif /* HAVE_GEOIP */
126

127 128 129 130
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif

131 132 133 134
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif

135 136 137 138
#ifndef SIZE_AS_PERCENT
#define SIZE_AS_PERCENT ((size_t)-2)
#endif

139 140 141 142 143 144 145 146 147 148
#ifdef TUNE_LARGE
#define RESOLVER_NTASKS 523
#define UDPBUFFERS 32768
#define EXCLBUFFERS 32768
#else
#define RESOLVER_NTASKS 31
#define UDPBUFFERS 1000
#define EXCLBUFFERS 4096
#endif /* TUNE_LARGE */

149
/*%
150 151 152 153
 * Check an operation for failure.  Assumes that the function
 * using it has a 'result' variable and a 'cleanup' label.
 */
#define CHECK(op) \
154 155
	do { result = (op);					 \
	       if (result != ISC_R_SUCCESS) goto cleanup;	 \
156 157
	} while (0)

158 159 160
#define TCHECK(op) \
	do { tresult = (op);					 \
		if (tresult != ISC_R_SUCCESS) {			 \
161
			isc_buffer_clear(*text);		 \
162 163 164 165
			goto cleanup;	 			 \
		}						 \
	} while (0)

166
#define CHECKM(op, msg) \
167
	do { result = (op);					  \
168 169 170 171 172 173 174 175 176 177 178
	       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
179
#define CHECKMF(op, msg, file) \
180
	do { result = (op);					  \
Mark Andrews's avatar
Mark Andrews committed
181 182 183 184 185 186 187 188 189 190 191
	       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)						  \

192
#define CHECKFATAL(op, msg) \
193
	do { result = (op);					  \
194 195 196 197
	       if (result != ISC_R_SUCCESS)			  \
			fatal(msg, result);			  \
	} while (0)						  \

198 199 200 201 202 203
/*%
 * 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.
 */
Mark Andrews's avatar
Mark Andrews committed
204
#define MAX_ADB_SIZE_FOR_CACHESHARE	8388608U
205

206 207 208 209 210 211 212
struct ns_dispatch {
	isc_sockaddr_t			addr;
	unsigned int			dispatchgen;
	dns_dispatch_t			*dispatch;
	ISC_LINK(struct ns_dispatch)	link;
};

213 214 215 216 217
struct ns_cache {
	dns_cache_t			*cache;
	dns_view_t			*primaryview;
	isc_boolean_t			needflush;
	isc_boolean_t			adbsizeadjusted;
218
	dns_rdataclass_t		rdclass;
219 220 221
	ISC_LINK(ns_cache_t)		link;
};

222 223 224 225
struct dumpcontext {
	isc_mem_t			*mctx;
	isc_boolean_t			dumpcache;
	isc_boolean_t			dumpzones;
Evan Hunt's avatar
Evan Hunt committed
226 227 228
	isc_boolean_t			dumpadb;
	isc_boolean_t			dumpbad;
	isc_boolean_t			dumpfail;
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	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;
};

251 252
/*%
 * Configuration context to retain for each view that allows
253
 * new zones to be added at runtime.
254
 */
Evan Hunt's avatar
Evan Hunt committed
255
typedef struct ns_cfgctx {
256
	isc_mem_t *			mctx;
257 258
	cfg_parser_t *			conf_parser;
	cfg_parser_t *			add_parser;
259
	cfg_obj_t *			config;
260
	cfg_obj_t *			vconfig;
261
	cfg_obj_t *			nzconfig;
262
	cfg_aclconfctx_t *		actx;
Evan Hunt's avatar
Evan Hunt committed
263
} ns_cfgctx_t;
264

265 266 267 268 269 270 271 272 273 274
/*%
 * Holds state information for the initial zone loading process.
 * Uses the isc_refcount structure to count the number of views
 * with pending zone loads, dereferencing as each view finishes.
 */
typedef struct {
		ns_server_t *server;
		isc_refcount_t refs;
} ns_zoneload_t;

275 276 277
/*
 * These zones should not leak onto the Internet.
 */
278
const char *empty_zones[] = {
279
	/* RFC 1918 */
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	"10.IN-ADDR.ARPA",
	"16.172.IN-ADDR.ARPA",
	"17.172.IN-ADDR.ARPA",
	"18.172.IN-ADDR.ARPA",
	"19.172.IN-ADDR.ARPA",
	"20.172.IN-ADDR.ARPA",
	"21.172.IN-ADDR.ARPA",
	"22.172.IN-ADDR.ARPA",
	"23.172.IN-ADDR.ARPA",
	"24.172.IN-ADDR.ARPA",
	"25.172.IN-ADDR.ARPA",
	"26.172.IN-ADDR.ARPA",
	"27.172.IN-ADDR.ARPA",
	"28.172.IN-ADDR.ARPA",
	"29.172.IN-ADDR.ARPA",
	"30.172.IN-ADDR.ARPA",
	"31.172.IN-ADDR.ARPA",
	"168.192.IN-ADDR.ARPA",
298

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	/* RFC 6598 */
	"64.100.IN-ADDR.ARPA",
	"65.100.IN-ADDR.ARPA",
	"66.100.IN-ADDR.ARPA",
	"67.100.IN-ADDR.ARPA",
	"68.100.IN-ADDR.ARPA",
	"69.100.IN-ADDR.ARPA",
	"70.100.IN-ADDR.ARPA",
	"71.100.IN-ADDR.ARPA",
	"72.100.IN-ADDR.ARPA",
	"73.100.IN-ADDR.ARPA",
	"74.100.IN-ADDR.ARPA",
	"75.100.IN-ADDR.ARPA",
	"76.100.IN-ADDR.ARPA",
	"77.100.IN-ADDR.ARPA",
	"78.100.IN-ADDR.ARPA",
	"79.100.IN-ADDR.ARPA",
	"80.100.IN-ADDR.ARPA",
	"81.100.IN-ADDR.ARPA",
	"82.100.IN-ADDR.ARPA",
	"83.100.IN-ADDR.ARPA",
	"84.100.IN-ADDR.ARPA",
	"85.100.IN-ADDR.ARPA",
	"86.100.IN-ADDR.ARPA",
	"87.100.IN-ADDR.ARPA",
	"88.100.IN-ADDR.ARPA",
	"89.100.IN-ADDR.ARPA",
	"90.100.IN-ADDR.ARPA",
	"91.100.IN-ADDR.ARPA",
	"92.100.IN-ADDR.ARPA",
	"93.100.IN-ADDR.ARPA",
	"94.100.IN-ADDR.ARPA",
	"95.100.IN-ADDR.ARPA",
	"96.100.IN-ADDR.ARPA",
	"97.100.IN-ADDR.ARPA",
	"98.100.IN-ADDR.ARPA",
	"99.100.IN-ADDR.ARPA",
	"100.100.IN-ADDR.ARPA",
	"101.100.IN-ADDR.ARPA",
	"102.100.IN-ADDR.ARPA",
	"103.100.IN-ADDR.ARPA",
	"104.100.IN-ADDR.ARPA",
	"105.100.IN-ADDR.ARPA",
	"106.100.IN-ADDR.ARPA",
	"107.100.IN-ADDR.ARPA",
	"108.100.IN-ADDR.ARPA",
	"109.100.IN-ADDR.ARPA",
	"110.100.IN-ADDR.ARPA",
	"111.100.IN-ADDR.ARPA",
	"112.100.IN-ADDR.ARPA",
	"113.100.IN-ADDR.ARPA",
	"114.100.IN-ADDR.ARPA",
	"115.100.IN-ADDR.ARPA",
	"116.100.IN-ADDR.ARPA",
	"117.100.IN-ADDR.ARPA",
	"118.100.IN-ADDR.ARPA",
	"119.100.IN-ADDR.ARPA",
	"120.100.IN-ADDR.ARPA",
	"121.100.IN-ADDR.ARPA",
	"122.100.IN-ADDR.ARPA",
	"123.100.IN-ADDR.ARPA",
	"124.100.IN-ADDR.ARPA",
	"125.100.IN-ADDR.ARPA",
	"126.100.IN-ADDR.ARPA",
	"127.100.IN-ADDR.ARPA",

365
	/* RFC 5735 and RFC 5737 */
366 367 368 369 370 371 372
	"0.IN-ADDR.ARPA",	/* THIS NETWORK */
	"127.IN-ADDR.ARPA",	/* LOOPBACK */
	"254.169.IN-ADDR.ARPA",	/* LINK LOCAL */
	"2.0.192.IN-ADDR.ARPA",	/* TEST NET */
	"100.51.198.IN-ADDR.ARPA",	/* TEST NET 2 */
	"113.0.203.IN-ADDR.ARPA",	/* TEST NET 3 */
	"255.255.255.255.IN-ADDR.ARPA",	/* BROADCAST */
373 374

	/* Local IPv6 Unicast Addresses */
375 376
	"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",
	"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",
Francis Dupont's avatar
Francis Dupont committed
377
	/* LOCALLY ASSIGNED LOCAL ADDRESS SCOPE */
378 379 380 381 382
	"D.F.IP6.ARPA",
	"8.E.F.IP6.ARPA",	/* LINK LOCAL */
	"9.E.F.IP6.ARPA",	/* LINK LOCAL */
	"A.E.F.IP6.ARPA",	/* LINK LOCAL */
	"B.E.F.IP6.ARPA",	/* LINK LOCAL */
383

384
	/* Example Prefix, RFC 3849. */
385
	"8.B.D.0.1.0.0.2.IP6.ARPA",
386

387 388 389
	/* RFC 7534 */
	"EMPTY.AS112.ARPA",

390
	NULL
391 392
};

Francis Dupont's avatar
Francis Dupont committed
393 394
ISC_PLATFORM_NORETURN_PRE static void
fatal(const char *msg, isc_result_t result) ISC_PLATFORM_NORETURN_POST;
David Lawrence's avatar
David Lawrence committed
395 396 397

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

399
static isc_result_t
400
ns_listenelt_fromconfig(const cfg_obj_t *listener, const cfg_obj_t *config,
401 402
			cfg_aclconfctx_t *actx, isc_mem_t *mctx,
			isc_uint16_t family, ns_listenelt_t **target);
403
static isc_result_t
404
ns_listenlist_fromconfig(const cfg_obj_t *listenlist, const cfg_obj_t *config,
405 406
			 cfg_aclconfctx_t *actx, isc_mem_t *mctx,
			 isc_uint16_t family, ns_listenlist_t **target);
407

408
static isc_result_t
409 410
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);
411

412
static isc_result_t
413 414
configure_alternates(const cfg_obj_t *config, dns_view_t *view,
		     const cfg_obj_t *alternates);
415

416
static isc_result_t
417 418
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,
Evan Hunt's avatar
Evan Hunt committed
419
	       dns_viewlist_t *viewlist, cfg_aclconfctx_t *aclconf,
Evan Hunt's avatar
Evan Hunt committed
420 421
	       isc_boolean_t added, isc_boolean_t old_rpz_ok,
	       isc_boolean_t modify);
422

423
static isc_result_t
424
add_keydata_zone(dns_view_t *view, const char *directory, isc_mem_t *mctx);
425

426 427 428
static void
end_reserved_dispatches(ns_server_t *server, isc_boolean_t all);

429
static void
430
newzone_cfgctx_destroy(void **cfgp);
431

Mark Andrews's avatar
Mark Andrews committed
432
static inline isc_result_t
433
putstr(isc_buffer_t **b, const char *str);
434

435
static isc_result_t
436 437 438 439 440
putmem(isc_buffer_t **b, const char *str, size_t len);

static isc_result_t
putuint8(isc_buffer_t **b, isc_uint8_t val);

Mark Andrews's avatar
Mark Andrews committed
441
static inline isc_result_t
442
putnull(isc_buffer_t **b);
443

Evan Hunt's avatar
Evan Hunt committed
444
static isc_result_t
Evan Hunt's avatar
Evan Hunt committed
445 446
add_comment(FILE *fp, const char *viewname);

447
/*%
Tatuya JINMEI 神明達哉's avatar
Tatuya JINMEI 神明達哉 committed
448 449
 * Configure a single view ACL at '*aclp'.  Get its configuration from
 * 'vconfig' (for per-view configuration) and maybe from 'config'
450 451
 */
static isc_result_t
452
configure_view_acl(const cfg_obj_t *vconfig, const cfg_obj_t *config,
453 454
		   const char *aclname, const char *acltuplename,
		   cfg_aclconfctx_t *actx, isc_mem_t *mctx, dns_acl_t **aclp)
455 456
{
	isc_result_t result;
457 458
	const cfg_obj_t *maps[3];
	const cfg_obj_t *aclobj = NULL;
459 460
	int i = 0;

461 462
	if (*aclp != NULL)
		dns_acl_detach(aclp);
463 464 465
	if (vconfig != NULL)
		maps[i++] = cfg_tuple_get(vconfig, "options");
	if (config != NULL) {
466
		const cfg_obj_t *options = NULL;
467
		(void)cfg_map_get(config, "options", &options);
468 469 470 471
		if (options != NULL)
			maps[i++] = options;
	}
	maps[i] = NULL;
472

473
	(void)ns_config_get(maps, aclname, &aclobj);
474
	if (aclobj == NULL)
475
		/*
476
		 * No value available.	*aclp == NULL.
477
		 */
478 479
		return (ISC_R_SUCCESS);

480 481 482 483 484 485 486 487 488
	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);
	}

489
	result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
490 491 492 493 494 495 496 497 498 499 500 501
				    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
502 503
			cfg_aclconfctx_t *actx, isc_mem_t *mctx,
			dns_acl_t **aclp)
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
{
	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
526 527 528 529 530 531
	/*
	 * 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.
	 */
532
	result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
533
				    actx, mctx, 3, aclp);
534 535 536 537

	return (result);
}

538 539 540 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 583 584 585 586 587 588 589
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);
590
		isc_buffer_constinit(&b, str, strlen(str));
591
		isc_buffer_add(&b, strlen(str));
592
		CHECK(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
593 594 595 596 597 598 599
		/*
		 * 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.
		 */
600 601 602 603 604 605 606
		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
607

608 609 610 611 612 613 614
	}

	return (result);

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

616 617
}

618
static isc_result_t
619 620
dstkey_fromconfig(const cfg_obj_t *vconfig, const cfg_obj_t *key,
		  isc_boolean_t managed, dst_key_t **target, isc_mem_t *mctx)
621 622
{
	dns_rdataclass_t viewclass;
623
	dns_rdata_dnskey_t keystruct;
624
	isc_uint32_t flags, proto, alg;
625
	const char *keystr, *keynamestr;
626 627 628 629 630 631 632 633 634 635 636
	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;

637 638
	INSIST(target != NULL && *target == NULL);

639 640 641 642 643 644
	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"));

Automatic Updater's avatar
Automatic Updater committed
645 646 647 648
	if (managed) {
		const char *initmethod;
		initmethod = cfg_obj_asstring(cfg_tuple_get(key, "init"));

649
		if (strcasecmp(initmethod, "initial-key") != 0) {
Automatic Updater's avatar
Automatic Updater committed
650 651 652 653 654 655 656 657
			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;
		}
	}
658

659
	if (vconfig == NULL)
660
		viewclass = dns_rdataclass_in;
661
	else {
662
		const cfg_obj_t *classobj = cfg_tuple_get(vconfig, "class");
663 664
		CHECK(ns_config_getclass(classobj, dns_rdataclass_in,
					 &viewclass));
665
	}
666
	keystruct.common.rdclass = viewclass;
667
	keystruct.common.rdtype = dns_rdatatype_dnskey;
668
	/*
669
	 * The key data in keystruct is not dynamically allocated.
670 671 672 673 674
	 */
	keystruct.mctx = NULL;

	ISC_LINK_INIT(&keystruct.common, link);

675
	if (flags > 0xffff)
676
		CHECKM(ISC_R_RANGE, "key flags");
677
	if (proto > 0xff)
678
		CHECKM(ISC_R_RANGE, "key protocol");
679
	if (alg > 0xff)
680
		CHECKM(ISC_R_RANGE, "key algorithm");
681 682 683
	keystruct.flags = (isc_uint16_t)flags;
	keystruct.protocol = (isc_uint8_t)proto;
	keystruct.algorithm = (isc_uint8_t)alg;
684 685 686 687

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

688
	keystr = cfg_obj_asstring(cfg_tuple_get(key, "key"));
689
	CHECK(isc_base64_decodestring(keystr, &keydatabuf));
690 691 692 693
	isc_buffer_usedregion(&keydatabuf, &r);
	keystruct.datalen = r.length;
	keystruct.data = r.base;

694 695 696 697
	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,
698 699
			    "%s key '%s' has a weak exponent",
			    managed ? "managed" : "trusted",
700 701
			    keynamestr);

702 703 704 705 706
	CHECK(dns_rdata_fromstruct(NULL,
				   keystruct.common.rdclass,
				   keystruct.common.rdtype,
				   &keystruct, &rrdatabuf));
	dns_fixedname_init(&fkeyname);
707
	isc_buffer_constinit(&namebuf, keynamestr, strlen(keynamestr));
708
	isc_buffer_add(&namebuf, strlen(keynamestr));
709
	CHECK(dns_name_fromtext(keyname, &namebuf, dns_rootname, 0, NULL));
710 711 712
	CHECK(dst_key_fromdns(keyname, viewclass, &rrdatabuf,
			      mctx, &dstkey));

713
	*target = dstkey;
714 715 716
	return (ISC_R_SUCCESS);

 cleanup:
717 718
	if (result == DST_R_NOCRYPTO) {
		cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
719 720
			    "ignoring %s key for '%s': no crypto support",
			    managed ? "managed" : "trusted",
721
			    keynamestr);
722 723 724 725 726
	} else if (result == DST_R_UNSUPPORTEDALG) {
		cfg_obj_log(key, ns_g_lctx, ISC_LOG_WARNING,
			    "skipping %s key for '%s': %s",
			    managed ? "managed" : "trusted",
			    keynamestr, isc_result_totext(result));
727 728
	} else {
		cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
729 730
			    "configuring %s key for '%s': %s",
			    managed ? "managed" : "trusted",
731 732 733
			    keynamestr, isc_result_totext(result));
		result = ISC_R_FAILURE;
	}
734 735 736 737 738 739 740

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

	return (result);
}

741 742
static isc_result_t
load_view_keys(const cfg_obj_t *keys, const cfg_obj_t *vconfig,
743 744
	       dns_view_t *view, isc_boolean_t managed,
	       dns_name_t *keyname, isc_mem_t *mctx)
745 746
{
	const cfg_listelt_t *elt, *elt2;
747 748
	const cfg_obj_t *key, *keylist;
	dst_key_t *dstkey = NULL;
749 750 751 752
	isc_result_t result;
	dns_keytable_t *secroots = NULL;

	CHECK(dns_view_getsecroots(view, &secroots));
753 754 755 756 757 758 759 760 761 762

	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);
763 764 765 766 767 768 769 770
			result = dstkey_fromconfig(vconfig, key, managed,
						   &dstkey, mctx);
			if (result ==  DST_R_UNSUPPORTEDALG) {
				result = ISC_R_SUCCESS;
				continue;
			}
			if (result != ISC_R_SUCCESS)
				goto cleanup;
771

772 773 774 775
			/*
			 * If keyname was specified, we only add that key.
			 */
			if (keyname != NULL &&
Automatic Updater's avatar
Automatic Updater committed
776
			    !dns_name_equal(keyname, dst_key_name(dstkey)))
777 778 779 780 781
			{
				dst_key_free(&dstkey);
				continue;
			}

782
			CHECK(dns_keytable_add(secroots, managed, &dstkey));
783 784 785 786
		}
	}

 cleanup:
787 788
	if (dstkey != NULL)
		dst_key_free(&dstkey);
789 790
	if (secroots != NULL)
		dns_keytable_detach(&secroots);
791 792
	if (result == DST_R_NOCRYPTO)
		result = ISC_R_SUCCESS;
793
	return (result);
794 795
}

796
/*%
797
 * Configure DNSSEC keys for a view.
798
 *
799
 * The per-view configuration values and the server-global defaults are read
800
 * from 'vconfig' and 'config'.
801 802
 */
static isc_result_t
803 804
configure_view_dnsseckeys(dns_view_t *view, const cfg_obj_t *vconfig,
			  const cfg_obj_t *config, const cfg_obj_t *bindkeys,
805 806
			  isc_boolean_t auto_dlv, isc_boolean_t auto_root,
			  isc_mem_t *mctx)
807
{
808
	isc_result_t result = ISC_R_SUCCESS;
809 810
	const cfg_obj_t *view_keys = NULL;
	const cfg_obj_t *global_keys = NULL;
811
	const cfg_obj_t *view_managed_keys = NULL;
812
	const cfg_obj_t *global_managed_keys = NULL;
813
	const cfg_obj_t *maps[4];
814
	const cfg_obj_t *voptions = NULL;
815
	const cfg_obj_t *options = NULL;
816 817
	const cfg_obj_t *obj = NULL;
	const char *directory;
818
	int i = 0;
819

820
	/* We don't need trust anchors for the _bind view */
821 822
	if (strcmp(view->name, "_bind") == 0 &&
	    view->rdclass == dns_rdataclass_chaos) {
823 824
		return (ISC_R_SUCCESS);
	}
825

826
	if (vconfig != NULL) {
827
		voptions = cfg_tuple_get(vconfig, "options");
828
		if (voptions != NULL) {
829 830
			(void) cfg_map_get(voptions, "trusted-keys",
					   &view_keys);
831 832
			(void) cfg_map_get(voptions, "managed-keys",
					   &view_managed_keys);
833 834 835
			maps[i++] = voptions;
		}
	}
836

837 838
	if (config != NULL) {
		(void)cfg_map_get(config, "trusted-keys", &global_keys);
839
		(void)cfg_map_get(config, "managed-keys", &global_managed_keys);
840 841 842
		(void)cfg_map_get(config, "options", &options);
		if (options != NULL) {
			maps[i++] = options;
843
		}
844
	}
845

846 847 848
	maps[i++] = ns_g_defaults;
	maps[i] = NULL;

849
	result = dns_view_initsecroots(view, mctx);
850 851 852 853 854 855 856
	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);
	}

Evan Hunt's avatar
Evan Hunt committed
857
	result = dns_view_initntatable(view, ns_g_taskmgr, ns_g_timermgr);
Evan Hunt's avatar
Evan Hunt committed
858 859 860 861 862 863 864
	if (result != ISC_R_SUCCESS) {
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
			      NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
			      "couldn't create NTA table");
		return (ISC_R_UNEXPECTED);
	}

865
	if (auto_dlv && view->rdclass == dns_rdataclass_in) {
866 867 868
		const cfg_obj_t *builtin_keys = NULL;
		const cfg_obj_t *builtin_managed_keys = NULL;

869
		isc_log_write(ns_g_lctx, DNS_LOGCATEGORY_SECURITY,
870
			      NS_LOGMODULE_SERVER, ISC_LOG_INFO,
871
			      "using built-in DLV key for view %s",
872
			      view->name);
873 874

		/*
875
		 * If bind.keys exists, it overrides the managed-keys
876 877
		 * clause hard-coded in ns_g_config.
		 */
878
		if (bindkeys != NULL) {
879 880
			(void)cfg_map_get(bindkeys, "trusted-keys",
					  &builtin_keys);
881 882 883
			(void)cfg_map_get(bindkeys, "managed-keys",
					  &builtin_managed_keys);
		} else {
884 885
			(void)cfg_map_get(ns_g_config, "trusted-keys",
					  &builtin_keys);
886 887 888 889
			(void)cfg_map_get(ns_g_config, "managed-keys",
					  &builtin_managed_keys);
		}

890 891
		if (builtin_keys != NULL)
			CHECK(load_view_keys(builtin_keys, vconfig, view,
892
					     ISC_FALSE, view->dlv, mctx));
893 894
		if (builtin_managed_keys != NULL)
			CHECK(load_view_keys(builtin_managed_keys, vconfig,
895
					     view, ISC_TRUE, view->dlv, mctx));
896 897
	}

898 899 900 901 902
	if (auto_root && view->rdclass == dns_rdataclass_in) {
		const cfg_obj_t *builtin_keys = NULL;
		const cfg_obj_t *builtin_managed_keys = NULL;

		isc_log_write(ns_g_lctx, DNS_LOGCATEGORY_SECURITY,
903
			      NS_LOGMODULE_SERVER, ISC_LOG_INFO,
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
			      "using built-in root key for view %s",
			      view->name);

		/*
		 * If bind.keys exists, it overrides the managed-keys
		 * clause hard-coded in ns_g_config.
		 */
		if (bindkeys != NULL) {
			(void)cfg_map_get(bindkeys, "trusted-keys",
					  &builtin_keys);
			(void)cfg_map_get(bindkeys, "managed-keys",
					  &builtin_managed_keys);
		} else {
			(void)cfg_map_get(ns_g_config, "trusted-keys",
					  &builtin_keys);
			(void)cfg_map_get(ns_g_config, "managed-keys",
					  &builtin_managed_keys);
		}

		if (builtin_keys != NULL)
			CHECK(load_view_keys(builtin_keys, vconfig, view,
					     ISC_FALSE, dns_rootname, mctx));
		if (builtin_managed_keys != NULL)
			CHECK(load_view_keys(builtin_managed_keys, vconfig,
					     view, ISC_TRUE, dns_rootname,
					     mctx));
	}

	CHECK(load_view_keys(view_keys, vconfig, view, ISC_FALSE,
			     NULL, mctx));
	CHECK(load_view_keys(view_managed_keys, vconfig, view, ISC_TRUE,
			     NULL, mctx));

937 938
	if (view->rdclass == dns_rdataclass_in) {
		CHECK(load_view_keys(global_keys, vconfig, view, ISC_FALSE,
939
				     NULL, mctx));
940
		CHECK(load_view_keys(global_managed_keys, vconfig, view,
941
				     ISC_TRUE, NULL, mctx));
942 943 944 945 946 947 948
	}

	/*
	 * Add key zone for managed-keys.
	 */
	obj = NULL;
	(void)ns_config_get(maps, "managed-keys-directory", &obj);
949 950 951 952 953 954 955 956 957 958 959
	directory = (obj != NULL ? cfg_obj_asstring(obj) : NULL);
	if (directory != NULL)
		result = isc_file_isdirectory(directory);
	if (result != ISC_R_SUCCESS) {
		isc_log_write(ns_g_lctx, DNS_LOGCATEGORY_SECURITY,
			      NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
			      "invalid managed-keys-directory %s: %s",
			      directory, isc_result_totext(result));
		goto cleanup;

	}
960
	CHECK(add_keydata_zone(view, directory, ns_g_mctx));
Automatic Updater's avatar
Automatic Updater committed
961

962 963
  cleanup:
	return (result);
964
}
965

966
static isc_result_t
967
mustbesecure(const cfg_obj_t *mbs, dns_resolver_t *resolver) {
968 969
	const cfg_listelt_t *element;
	const cfg_obj_t *obj;
970 971 972 973 974 975
	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
976

977 978 979 980 981 982 983 984
	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"));
985
		isc_buffer_constinit(&b, str, strlen(str));
986
		isc_buffer_add(&b, strlen(str));
987
		CHECK(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
988 989 990 991 992
		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
993

994 995 996
 cleanup:
	return (result);
}
997

998
/*%
999 1000 1001
 * Get a dispatch appropriate for the resolver of a given view.
 */
static isc_result_t
Evan Hunt's avatar
Evan Hunt committed
1002 1003
get_view_querysource_dispatch(const cfg_obj_t **maps, int af,
			      dns_dispatch_t **dispatchp, isc_dscp_t *dscpp,
1004
			      isc_boolean_t is_firstview)
1005
{
1006
	isc_result_t result = ISC_R_FAILURE;
1007 1008 1009
	dns_dispatch_t *disp;
	isc_sockaddr_t sa;
	unsigned int attrs, attrmask;
1010
	const cfg_obj_t *obj = NULL;
1011
	unsigned int maxdispatchbuffers = UDPBUFFERS;
Evan Hunt's avatar
Evan Hunt committed
1012
	isc_dscp_t dscp = -1;
1013 1014 1015

	switch (af) {
	case AF_INET:
1016 1017
		result = ns_config_get(maps, "query-source", &obj);
		INSIST(result == ISC_R_SUCCESS);
1018 1019
		break;
	case AF_INET6:
1020 1021
		result = ns_config_get(maps, "query-source-v6", &obj);
		INSIST(result == ISC_R_SUCCESS);
1022 1023 1024 1025
		break;
	default:
		INSIST(0);
	}
1026

1027
	sa = *(cfg_obj_assockaddr(obj));
1028 1029
	INSIST(isc_sockaddr_pf(&sa) == af);

Evan Hunt's avatar
Evan Hunt committed
1030 1031 1032 1033
	dscp = cfg_obj_getdscp(obj);
	if (dscp != -1 && dscpp != NULL)
		*dscpp = dscp;

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	/*
	 * 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;
	}
1063 1064
	if (isc_sockaddr_getport(&sa) == 0) {
		attrs |= DNS_DISPATCHATTR_EXCLUSIVE;
1065
		maxdispatchbuffers = EXCLBUFFERS;
1066 1067 1068 1069 1070 1071 1072 1073 1074
	} 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.");
		}
	}
1075

Michael Graff's avatar