server.c 410 KB
Newer Older
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
10 11
 */

12
/*! \file */
David Lawrence's avatar
David Lawrence committed
13

14
#include <inttypes.h>
15
#include <stdbool.h>
16
#include <stdlib.h>
17
#include <unistd.h>
18
#include <limits.h>
19 20 21
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
22

23
#include <isc/aes.h>
24
#include <isc/app.h>
25
#include <isc/base64.h>
Evan Hunt's avatar
Evan Hunt committed
26
#include <isc/commandline.h>
27
#include <isc/dir.h>
28
#include <isc/file.h>
29
#include <isc/hash.h>
30
#include <isc/hex.h>
31
#include <isc/hmac.h>
32
#include <isc/httpd.h>
33
#include <isc/lex.h>
34
#include <isc/meminfo.h>
35
#include <isc/nonce.h>
36
#include <isc/parseint.h>
Evan Hunt's avatar
Evan Hunt committed
37
#include <isc/platform.h>
38
#include <isc/portset.h>
39
#include <isc/print.h>
40
#include <isc/refcount.h>
41
#include <isc/resource.h>
42
#include <isc/siphash.h>
Mark Andrews's avatar
Mark Andrews committed
43
#include <isc/socket.h>
44
#include <isc/stat.h>
45
#include <isc/stats.h>
46
#include <isc/stdio.h>
47
#include <isc/string.h>
48
#include <isc/task.h>
49
#include <isc/timer.h>
Michael Graff's avatar
Michael Graff committed
50
#include <isc/util.h>
51

Evan Hunt's avatar
Evan Hunt committed
52
#include <isccfg/grammar.h>
53
#include <isccfg/kaspconf.h>
54
#include <isccfg/namedconf.h>
55 56

#include <bind9/check.h>
57

58
#include <dns/adb.h>
Evan Hunt's avatar
Evan Hunt committed
59
#include <dns/badcache.h>
60
#include <dns/cache.h>
61
#include <dns/catz.h>
62
#include <dns/db.h>
Bob Halley's avatar
Bob Halley committed
63
#include <dns/dispatch.h>
64
#include <dns/dlz.h>
65
#include <dns/dnsrps.h>
66
#include <dns/dns64.h>
Evan Hunt's avatar
Evan Hunt committed
67
#include <dns/dyndb.h>
68
#include <dns/events.h>
69
#include <dns/forward.h>
70
#include <dns/fixedname.h>
71
#include <dns/journal.h>
72
#include <dns/kasp.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 <ns/client.h>
105
#include <ns/hooks.h>
106 107 108
#include <ns/listenlist.h>
#include <ns/interfacemgr.h>

109
#include <named/config.h>
110
#include <named/control.h>
111
#if defined(HAVE_GEOIP2)
Evan Hunt's avatar
Evan Hunt committed
112
#include <named/geoip.h>
113
#endif /* HAVE_GEOIP2 */
114
#include <named/log.h>
115
#include <named/logconf.h>
116
#include <named/main.h>
117
#include <named/os.h>
Bob Halley's avatar
Bob Halley committed
118
#include <named/server.h>
119
#include <named/statschannel.h>
120 121 122
#include <named/tkeyconf.h>
#include <named/tsigconf.h>
#include <named/zoneconf.h>
123
#ifdef HAVE_LIBSCF
124
#include <named/smf_globals.h>
125 126
#include <stdlib.h>
#endif
127 128 129 130 131 132 133 134 135 136 137

#ifdef HAVE_LMDB
#include <lmdb.h>
#define count_newzones count_newzones_db
#define configure_newzones configure_newzones_db
#define dumpzone dumpzone_db
#else  /* HAVE_LMDB */
#define count_newzones count_newzones_file
#define configure_newzones configure_newzones_file
#define dumpzone dumpzone_file
#endif /* HAVE_LMDB */
138

139 140 141 142
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif

143 144 145 146
#ifndef SIZE_AS_PERCENT
#define SIZE_AS_PERCENT ((size_t)-2)
#endif

147 148 149 150 151 152 153 154 155 156
#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 */

157 158
#define MAX_TCP_TIMEOUT 65535

159
/*%
160 161 162 163
 * Check an operation for failure.  Assumes that the function
 * using it has a 'result' variable and a 'cleanup' label.
 */
#define CHECK(op) \
164 165
	do { result = (op);					  \
	       if (result != ISC_R_SUCCESS) goto cleanup;	  \
166 167
	} while (0)

168
#define TCHECK(op) \
169 170 171 172 173
	do { tresult = (op);					  \
		if (tresult != ISC_R_SUCCESS) {			  \
			isc_buffer_clear(*text);		  \
			goto cleanup;	 			  \
		}						  \
174 175
	} while (0)

176
#define CHECKM(op, msg) \
177
	do { result = (op);					  \
178
	       if (result != ISC_R_SUCCESS) {			  \
179
			isc_log_write(named_g_lctx,		  \
180
				      NAMED_LOGCATEGORY_GENERAL,  \
181
				      NAMED_LOGMODULE_SERVER,	  \
182 183 184 185 186 187 188
				      ISC_LOG_ERROR,		  \
				      "%s: %s", msg,		  \
				      isc_result_totext(result)); \
			goto cleanup;				  \
		}						  \
	} while (0)						  \

Mark Andrews's avatar
Mark Andrews committed
189
#define CHECKMF(op, msg, file) \
190
	do { result = (op);					  \
Mark Andrews's avatar
Mark Andrews committed
191
	       if (result != ISC_R_SUCCESS) {			  \
192
			isc_log_write(named_g_lctx,		  \
193
				      NAMED_LOGCATEGORY_GENERAL,  \
194
				      NAMED_LOGMODULE_SERVER,	  \
Mark Andrews's avatar
Mark Andrews committed
195 196 197 198 199 200 201
				      ISC_LOG_ERROR,		  \
				      "%s '%s': %s", msg, file,	  \
				      isc_result_totext(result)); \
			goto cleanup;				  \
		}						  \
	} while (0)						  \

202
#define CHECKFATAL(op, msg) \
203
	do { result = (op);					  \
204 205
		if (result != ISC_R_SUCCESS)			  \
			fatal(server, msg, result);		  \
206 207
	} while (0)						  \

208 209 210 211 212 213
/*%
 * 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
214
#define MAX_ADB_SIZE_FOR_CACHESHARE	8388608U
215

216
struct named_dispatch {
217 218 219
	isc_sockaddr_t			addr;
	unsigned int			dispatchgen;
	dns_dispatch_t			*dispatch;
220
	ISC_LINK(struct named_dispatch)	link;
221 222
};

223
struct named_cache {
224 225
	dns_cache_t			*cache;
	dns_view_t			*primaryview;
226 227
	bool			needflush;
	bool			adbsizeadjusted;
228
	dns_rdataclass_t		rdclass;
229
	ISC_LINK(named_cache_t)		link;
230 231
};

232 233
struct dumpcontext {
	isc_mem_t			*mctx;
234 235 236 237 238
	bool			dumpcache;
	bool			dumpzones;
	bool			dumpadb;
	bool			dumpbad;
	bool			dumpfail;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	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;
};

261 262
/*%
 * Configuration context to retain for each view that allows
263
 * new zones to be added at runtime.
264
 */
Evan Hunt's avatar
Evan Hunt committed
265
typedef struct ns_cfgctx {
266
	isc_mem_t *			mctx;
267 268
	cfg_parser_t *			conf_parser;
	cfg_parser_t *			add_parser;
269
	cfg_obj_t *			config;
270
	cfg_obj_t *			vconfig;
271
	cfg_obj_t *			nzf_config;
272
	cfg_aclconfctx_t *		actx;
Evan Hunt's avatar
Evan Hunt committed
273
} ns_cfgctx_t;
274

275 276 277 278 279 280
/*%
 * A function to write out added-zone configuration to the new_zone_file
 * specified in 'view'. Maybe called by delete_zoneconf().
 */
typedef isc_result_t (*nzfwriter_t)(const cfg_obj_t *config, dns_view_t *view);

281 282 283 284 285 286
/*%
 * 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 {
287
		named_server_t *server;
288
		bool reconfig;
289 290 291
		isc_refcount_t refs;
} ns_zoneload_t;

292
typedef struct {
293
	named_server_t *server;
294 295 296 297 298 299 300 301
} catz_cb_data_t;

typedef struct catz_chgzone_event {
	ISC_EVENT_COMMON(struct catz_chgzone_event);
	dns_catz_entry_t *entry;
	dns_catz_zone_t *origin;
	dns_view_t *view;
	catz_cb_data_t *cbd;
302
	bool mod;
303 304
} catz_chgzone_event_t;

Mark Andrews's avatar
Mark Andrews committed
305 306 307 308 309 310 311
typedef struct {
	unsigned int magic;
#define DZARG_MAGIC		ISC_MAGIC('D', 'z', 'a', 'r')
	isc_buffer_t **text;
	isc_result_t result;
} ns_dzarg_t;

312 313 314
/*
 * These zones should not leak onto the Internet.
 */
315
const char *empty_zones[] = {
316
	/* RFC 1918 */
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	"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",
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 365 366 367 368 369 370 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
	/* 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",

402
	/* RFC 5735 and RFC 5737 */
403 404 405 406 407 408 409
	"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 */
410 411

	/* Local IPv6 Unicast Addresses */
412 413
	"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
414
	/* LOCALLY ASSIGNED LOCAL ADDRESS SCOPE */
415 416 417 418 419
	"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 */
420

421
	/* Example Prefix, RFC 3849. */
422
	"8.B.D.0.1.0.0.2.IP6.ARPA",
423

424 425 426
	/* RFC 7534 */
	"EMPTY.AS112.ARPA",

427 428 429
	/* RFC 8375 */
	"HOME.ARPA",

430
	NULL
431 432
};

Francis Dupont's avatar
Francis Dupont committed
433
ISC_PLATFORM_NORETURN_PRE static void
434 435
fatal(named_server_t *server,const char *msg, isc_result_t result)
ISC_PLATFORM_NORETURN_POST;
David Lawrence's avatar
David Lawrence committed
436 437

static void
438
named_server_reload(isc_task_t *task, isc_event_t *event);
439

440
static isc_result_t
441
ns_listenelt_fromconfig(const cfg_obj_t *listener, const cfg_obj_t *config,
442
			cfg_aclconfctx_t *actx, isc_mem_t *mctx,
443
			uint16_t family, ns_listenelt_t **target);
444
static isc_result_t
445
ns_listenlist_fromconfig(const cfg_obj_t *listenlist, const cfg_obj_t *config,
446
			 cfg_aclconfctx_t *actx, isc_mem_t *mctx,
447
			 uint16_t family, ns_listenlist_t **target);
448

449
static isc_result_t
450
configure_forward(const cfg_obj_t *config, dns_view_t *view,
451 452
		  const dns_name_t *origin, const cfg_obj_t *forwarders,
		  const cfg_obj_t *forwardtype);
453

454
static isc_result_t
455 456
configure_alternates(const cfg_obj_t *config, dns_view_t *view,
		     const cfg_obj_t *alternates);
457

458
static isc_result_t
459 460
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,
461 462
	       dns_viewlist_t *viewlist, dns_kasplist_t* kasplist,
	       cfg_aclconfctx_t *aclconf, bool added, bool old_rpz_ok,
463
	       bool modify);
464

465 466 467 468
static isc_result_t
configure_newzones(dns_view_t *view, cfg_obj_t *config, cfg_obj_t *vconfig,
		   isc_mem_t *mctx, cfg_aclconfctx_t *actx);

469
static isc_result_t
470
add_keydata_zone(dns_view_t *view, const char *directory, isc_mem_t *mctx);
471

472
static void
473
end_reserved_dispatches(named_server_t *server, bool all);
474

475
static void
476
newzone_cfgctx_destroy(void **cfgp);
477

Mark Andrews's avatar
Mark Andrews committed
478
static inline isc_result_t
479
putstr(isc_buffer_t **b, const char *str);
480

481
static isc_result_t
482 483 484
putmem(isc_buffer_t **b, const char *str, size_t len);

static isc_result_t
485
putuint8(isc_buffer_t **b, uint8_t val);
486

Mark Andrews's avatar
Mark Andrews committed
487
static inline isc_result_t
488
putnull(isc_buffer_t **b);
489

490 491 492 493 494 495 496
static int
count_zones(const cfg_obj_t *conf);

#ifdef HAVE_LMDB
static isc_result_t
migrate_nzf(dns_view_t *view);

497 498 499
static isc_result_t
nzd_writable(dns_view_t *view);

500 501 502
static isc_result_t
nzd_open(dns_view_t *view, unsigned int flags, MDB_txn **txnp, MDB_dbi *dbi);

503 504 505 506 507 508
static isc_result_t
nzd_env_reopen(dns_view_t *view);

static void
nzd_env_close(dns_view_t *view);

509
static isc_result_t
510
nzd_close(MDB_txn **txnp, bool commit);
511 512 513 514

static isc_result_t
nzd_count(dns_view_t *view, int *countp);
#else
Evan Hunt's avatar
Evan Hunt committed
515
static isc_result_t
516 517
nzf_append(dns_view_t *view, const cfg_obj_t *zconfig);
#endif
Evan Hunt's avatar
Evan Hunt committed
518

519
/*%
Tatuya JINMEI 神明達哉's avatar
Tatuya JINMEI 神明達哉 committed
520 521
 * Configure a single view ACL at '*aclp'.  Get its configuration from
 * 'vconfig' (for per-view configuration) and maybe from 'config'
522 523
 */
static isc_result_t
524
configure_view_acl(const cfg_obj_t *vconfig, const cfg_obj_t *config,
525 526 527
		   const cfg_obj_t *gconfig, const char *aclname,
		   const char *acltuplename, cfg_aclconfctx_t *actx,
		   isc_mem_t *mctx, dns_acl_t **aclp)
528 529
{
	isc_result_t result;
530
	const cfg_obj_t *maps[4];
531
	const cfg_obj_t *aclobj = NULL;
532 533
	int i = 0;

534
	if (*aclp != NULL) {
535
		dns_acl_detach(aclp);
536 537
	}
	if (vconfig != NULL) {
538
		maps[i++] = cfg_tuple_get(vconfig, "options");
539
	}
540
	if (config != NULL) {
541
		const cfg_obj_t *options = NULL;
542
		(void)cfg_map_get(config, "options", &options);
543
		if (options != NULL) {
544
			maps[i++] = options;
545 546 547 548 549 550 551 552
		}
	}
	if (gconfig != NULL) {
		const cfg_obj_t *options = NULL;
		(void)cfg_map_get(gconfig, "options", &options);
		if (options != NULL) {
			maps[i++] = options;
		}
553 554
	}
	maps[i] = NULL;
555

556
	(void)named_config_get(maps, aclname, &aclobj);
557
	if (aclobj == NULL) {
558
		/*
559
		 * No value available.	*aclp == NULL.
560
		 */
561
		return (ISC_R_SUCCESS);
562
	}
563

564 565 566 567 568 569 570 571 572
	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);
	}

573
	result = cfg_acl_fromconfig(aclobj, config, named_g_lctx,
574 575 576 577 578 579 580 581 582 583 584 585
				    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
586 587
			cfg_aclconfctx_t *actx, isc_mem_t *mctx,
			dns_acl_t **aclp)
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
{
	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;

606
	(void)named_config_get(maps, "sortlist", &aclobj);
607 608 609
	if (aclobj == NULL)
		return (ISC_R_SUCCESS);

Automatic Updater's avatar
Automatic Updater committed
610 611 612 613 614 615
	/*
	 * 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.
	 */
616
	result = cfg_acl_fromconfig(aclobj, config, named_g_lctx,
617
				    actx, mctx, 3, aclp);
618 619 620 621

	return (result);
}

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
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;

650
	(void)named_config_get(maps, confname, &obj);
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
	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);

667
	name = dns_fixedname_initname(&fixed);
668 669 670 671 672
	for (element = cfg_list_first(obj);
	     element != NULL;
	     element = cfg_list_next(element)) {
		nameobj = cfg_listelt_value(element);
		str = cfg_obj_asstring(nameobj);
673
		isc_buffer_constinit(&b, str, strlen(str));
674
		isc_buffer_add(&b, strlen(str));
675
		CHECK(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
676 677 678 679 680 681 682
		/*
		 * 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.
		 */
683 684
		result = dns_rbt_addname(*rbtp, name, (void *)1);
		if (result != ISC_R_SUCCESS) {
685
			cfg_obj_log(nameobj, named_g_lctx, ISC_LOG_ERROR,
686 687 688 689
				    "failed to add %s for %s: %s",
				    str, confname, isc_result_totext(result));
			goto cleanup;
		}
Automatic Updater's avatar
Automatic Updater committed
690

691 692 693 694 695 696 697
	}

	return (result);

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

699 700
}

701
static isc_result_t
702 703
ta_fromconfig(const cfg_obj_t *key, bool *initialp, dst_key_t **keyp,
	      dns_rdata_ds_t **dsp, const char **namestrp, isc_mem_t *mctx)
704
{
705
	dns_rdata_dnskey_t keystruct;
706
	dns_rdata_ds_t *ds = NULL;
707
	uint32_t n1, n2, n3;
708
	const char *datastr = NULL, *namestr = NULL;
709 710
	unsigned char data[4096];
	isc_buffer_t databuf;
711 712 713
	unsigned char rrdata[4096];
	isc_buffer_t rrdatabuf;
	isc_region_t r;
714 715
	dns_fixedname_t fname;
	dns_name_t *name = NULL;
716 717 718
	isc_buffer_t namebuf;
	isc_result_t result;
	dst_key_t *dstkey = NULL;
719 720 721 722 723 724 725 726
	const char *atstr = NULL;
	enum {
		INIT_DNSKEY,
		STATIC_DNSKEY,
		INIT_DS,
		STATIC_DS,
		TRUSTED
	} anchortype;
727

728 729 730
	REQUIRE(keyp != NULL && *keyp == NULL);
	REQUIRE(dsp != NULL && *dsp == NULL);
	REQUIRE(namestrp != NULL && *namestrp == NULL);
731 732 733 734 735 736 737 738 739

	/* if DNSKEY, flags; if DS, key tag */
	n1 = cfg_obj_asuint32(cfg_tuple_get(key, "n1"));

	/* if DNSKEY, protocol; if DS, algorithm */
	n2 = cfg_obj_asuint32(cfg_tuple_get(key, "n2"));

	/* if DNSKEY, algorithm; if DS, digest type */
	n3 = cfg_obj_asuint32(cfg_tuple_get(key, "n3"));
740

741 742
	namestr = cfg_obj_asstring(cfg_tuple_get(key, "name"));
	*namestrp = namestr;
743

744 745 746 747
	name = dns_fixedname_initname(&fname);
	isc_buffer_constinit(&namebuf, namestr, strlen(namestr));
	isc_buffer_add(&namebuf, strlen(namestr));
	CHECK(dns_name_fromtext(name, &namebuf, dns_rootname, 0, NULL));
748

Evan Hunt's avatar
Evan Hunt committed
749
	if (*initialp) {
750
		atstr = cfg_obj_asstring(cfg_tuple_get(key, "anchortype"));
Automatic Updater's avatar
Automatic Updater committed
751

752 753 754 755
		if (strcasecmp(atstr, "static-key") == 0) {
			*initialp = false;
			anchortype = STATIC_DNSKEY;
		} else if (strcasecmp(atstr, "static-ds") == 0) {
Evan Hunt's avatar
Evan Hunt committed
756
			*initialp = false;
757 758 759 760 761 762
			anchortype = STATIC_DS;
		} else if (strcasecmp(atstr, "initial-key") == 0) {
			anchortype = INIT_DNSKEY;
		} else if (strcasecmp(atstr, "initial-ds") == 0) {
			anchortype = INIT_DS;
		} else {
763
			cfg_obj_log(key, named_g_lctx, ISC_LOG_ERROR,
Evan Hunt's avatar
Evan Hunt committed
764
				    "key '%s': "
Automatic Updater's avatar
Automatic Updater committed
765
				    "invalid initialization method '%s'",
766
				    namestr, atstr);
Automatic Updater's avatar
Automatic Updater committed
767 768 769
			result = ISC_R_FAILURE;
			goto cleanup;
		}
770 771
	} else {
		anchortype = TRUSTED;
Automatic Updater's avatar
Automatic Updater committed
772
	}
773

774 775 776
	isc_buffer_init(&databuf, data, sizeof(data));
	isc_buffer_init(&rrdatabuf, rrdata, sizeof(rrdata));

777 778 779 780 781 782 783 784 785 786
	switch(anchortype) {
	case INIT_DNSKEY:
	case STATIC_DNSKEY:
	case TRUSTED:
		/*
		 * This function should never be reached for view
		 * class other than IN
		 */
		keystruct.common.rdclass = dns_rdataclass_in;
		keystruct.common.rdtype = dns_rdatatype_dnskey;
Evan Hunt's avatar
Evan Hunt committed
787

788 789 790 791
		/*
		 * The key data in keystruct is not dynamically allocated.
		 */
		keystruct.mctx = NULL;
792

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
		ISC_LINK_INIT(&keystruct.common, link);

		if (n1 > 0xffff) {
			CHECKM(ISC_R_RANGE, "key flags");
		}
		if (n1 & DNS_KEYFLAG_REVOKE) {
			CHECKM(DST_R_BADKEYTYPE, "key flags revoke bit set");
		}
		if (n2 > 0xff) {
			CHECKM(ISC_R_RANGE, "key protocol");
		}
		if (n3> 0xff) {
			CHECKM(ISC_R_RANGE, "key algorithm");
		}

		keystruct.flags = (uint16_t)n1;
		keystruct.protocol = (uint8_t)n2;
		keystruct.algorithm = (uint8_t)n3;

		datastr = cfg_obj_asstring(cfg_tuple_get(key, "data"));
		CHECK(isc_base64_decodestring(datastr, &databuf));
		isc_buffer_usedregion(&databuf, &r);
		keystruct.datalen = r.length;
		keystruct.data = r.base;

		CHECK(dns_rdata_fromstruct(NULL, keystruct.common.rdclass,
					   keystruct.common.rdtype,
					   &keystruct, &rrdatabuf));
821
		CHECK(dst_key_fromdns(name, dns_rdataclass_in,
822 823
				      &rrdatabuf, mctx, &dstkey));

824
		*keyp = dstkey;
825 826 827 828
		break;

	case INIT_DS:
	case STATIC_DS:
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
		ds = isc_mem_get(mctx, sizeof(*ds));
		ds->common.rdclass = dns_rdataclass_in;
		ds->common.rdtype = dns_rdatatype_ds;
		ds->mctx = NULL;

		ISC_LINK_INIT(&ds->common, link);

		if (n1 > 0xffff) {
			CHECKM(ISC_R_RANGE, "key tag");
		}
		if (n2 > 0xff) {
			CHECKM(ISC_R_RANGE, "key algorithm");
		}
		if (n3 > 0xff) {
			CHECKM(ISC_R_RANGE, "digest type");
		}

		ds->key_tag = (uint16_t)n1;
		ds->algorithm = (uint8_t)n2;
		ds->digest_type = (uint8_t)n3;

		datastr = cfg_obj_asstring(cfg_tuple_get(key, "data"));
		CHECK(isc_hex_decodestring(datastr, &databuf));
		isc_buffer_usedregion(&databuf, &r);

		switch (ds->digest_type) {
		case DNS_DSDIGEST_SHA1:
			if (r.length != ISC_SHA1_DIGESTLENGTH) {
				CHECK(ISC_R_UNEXPECTEDEND);
			}
			break;
		case DNS_DSDIGEST_SHA256:
			if (r.length != ISC_SHA256_DIGESTLENGTH) {
				CHECK(ISC_R_UNEXPECTEDEND);
			}
			break;
		case DNS_DSDIGEST_SHA384:
			if (r.length != ISC_SHA384_DIGESTLENGTH) {
				CHECK(ISC_R_UNEXPECTEDEND);
			}
			break;
		}

		ds->mctx = mctx;
		ds->length = r.length;
		ds->digest = isc_mem_allocate(mctx, r.length);
		memmove(ds->digest, r.base, r.length);

		*dsp = ds;
		ds = NULL;
		break;
880 881 882 883 884

	default:
		INSIST(0);
		ISC_UNREACHABLE();
	}
885 886 887 888

	return (ISC_R_SUCCESS);

 cleanup:
889 890 891 892
	if (dstkey != NULL) {
		dst_key_free(&dstkey);
	}

893 894 895 896 897
	if (ds != NULL) {
		dns_rdata_freestruct(ds);
		isc_mem_put(mctx, ds, sizeof(*ds));
	}

898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
	return (result);
}

/*%
 * Parse 'key' in the context of view configuration 'vconfig'.  If successful,
 * add the key to 'secroots' if both of the following conditions are true:
 *
 *   - 'keyname_match' is NULL or it matches the owner name of 'key',
 *   - support for the algorithm used by 'key' is not disabled by 'resolver'
 *     for the owner name of 'key'.
 *
 * 'managed' is true for managed keys and false for trusted keys.  'mctx' is
 * the memory context to use for allocating memory.
 */
static isc_result_t
Evan Hunt's avatar
Evan Hunt committed
913 914 915
process_key(const cfg_obj_t *key, dns_keytable_t *secroots,
	    const dns_name_t *keyname_match, dns_resolver_t *resolver,
	    bool managed, isc_mem_t *mctx)
916
{
917 918 919
	dns_fixedname_t fkeyname;
	dns_name_t *keyname = NULL;
	const char *namestr = NULL;
920
	dst_key_t *dstkey = NULL;
921
	dns_rdata_ds_t *ds = NULL;
922 923
	unsigned int keyalg;
	isc_result_t result;
Evan Hunt's avatar
Evan Hunt committed
924
	bool initializing = managed;
925

926 927
	result = ta_fromconfig(key, &initializing, &dstkey, &ds,
			       &namestr, mctx);
928 929 930 931

	switch (result) {
	case ISC_R_SUCCESS:
		/*
932 933 934 935 936
		 * Trust anchor was parsed correctly. If dstkey is
		 * not NULL, then it was a key anchor, its algorithm
		 * is supported by the crypto library, and it is not
		 * revoked. If dstkey is NULL, then it was a DS
		 * trust anchor instead.
937
		 */
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
		if (dstkey != NULL) {
			keyname = dst_key_name(dstkey);
			keyalg = dst_key_alg(dstkey);
		} else {
			isc_buffer_t b;

			INSIST(ds != NULL);

			isc_buffer_constinit(&b, namestr, strlen(namestr));
			isc_buffer_add(&b, strlen(namestr));
			keyname = dns_fixedname_initname(&fkeyname);
			result = dns_name_fromtext(keyname, &b,
						   dns_rootname, 0, NULL);
			if (result != ISC_R_SUCCESS) {
				return (result);
			}
			keyalg = ds->algorithm;
		}
956 957 958 959 960 961 962 963 964
		break;
	case DST_R_UNSUPPORTEDALG:
	case DST_R_BADKEYTYPE:
		/*
		 * Key was parsed correctly, but it cannot be used; this is not
		 * a fatal error - log a warning about this key being ignored,
		 * but do not prevent any further ones from being processed.
		 */
		cfg_obj_log(key, named_g_lctx, ISC_LOG_WARNING,
Evan Hunt's avatar
Evan Hunt committed
965 966
			    "ignoring %s for '%s': %s",
			    initializing ? "initial-key" : "static-key",
967
			    namestr, isc_result_totext(result));
968 969 970 971 972
		return (ISC_R_SUCCESS);
	case DST_R_NOCRYPTO:
		/*
		 * Crypto support is not available.
		 */
973
		cfg_obj_log(key, named_g_lctx, ISC_LOG_ERROR,
Evan Hunt's avatar
Evan Hunt committed
974 975
			    "ignoring %s for '%s': no crypto support",
			    initializing ? "initial-key" : "static-key",
976
			    namestr);
977 978 979 980 981 982 983
		return (result);
	default:
		/*
		 * Something unexpected happened; we have no choice but to
		 * indicate an error so that the configuration loading process
		 * is interrupted.
		 */
984
		cfg_obj_log(key, named_g_lctx, ISC_LOG_ERROR,
Evan Hunt's avatar
Evan Hunt committed
985 986
			    "configuring %s for '%s': %s",
			    initializing ? "initial-key" : "static-key",
987
			    namestr, isc_result_totext(result));
988
		return (ISC_R_FAILURE);
989
	}
990

991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	/*
	 * If the caller requested to only load keys for a specific name and
	 * the owner name of this key does not match the requested name, do not
	 * load it.
	 */
	if (keyname_match != NULL && !dns_name_equal(keyname_match, keyname)) {
		goto done;
	}

	/*
	 * Ensure that 'resolver' allows using the algorithm of this key for
	 * its owner name.  If it does not, do not load the key and log a
	 * warning, but do not prevent further keys from being processed.
	 */
	if (!dns_resolver_algorithm_supported(resolver, keyname, keyalg)) {
		cfg_obj_log(key, named_g_lctx, ISC_LOG_WARNING,
Evan Hunt's avatar
Evan Hunt committed
1007 1008
			    "ignoring %s for '%s': algorithm is disabled",
			    initializing ? "initial-key" : "static-key",
1009
			    namestr);
1010 1011 1012 1013
		goto done;
	}

	/*
Evan Hunt's avatar
Evan Hunt committed
1014
	 * Add the key to 'secroots'.  Keys from a "dnssec-keys" or
1015
	 * "managed-keys" statement may be either static or initializing
Evan Hunt's avatar
Evan Hunt committed
1016 1017 1018
	 * keys. If it's not initializing, we don't want to treat it as
	 * managed, so we use 'initializing' twice here, for both the
	 * 'managed' and 'initializing' arguments to dns_keytable_add().
1019
	 */
Evan Hunt's avatar
Evan Hunt committed
1020
	result = dns_keytable_add(secroots, initializing,
1021 1022 1023
				  initializing, keyname,
				  dstkey != NULL ? &dstkey : NULL,
				  ds);
1024 1025 1026 1027 1028 1029 1030 1031

 done:
	/*
	 * Ensure 'dstkey' does not leak.  Note that if dns_keytable_add()
	 * succeeds, ownership of the key structure is transferred to the key
	 * table, i.e. 'dstkey' is set to NULL.
	 */
	if (dstkey != NULL) {
1032
		dst_key_free(&dstkey);
1033
	}
1034

1035
	/*
1036
	 * Free 'ds'.