check.c 117 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 */
13
14
15

#include <config.h>

16
#include <inttypes.h>
17
#include <stdbool.h>
18
#include <stdlib.h>
19

20
#include <isc/aes.h>
21
#include <isc/base64.h>
22
#include <isc/buffer.h>
23
#include <isc/file.h>
24
#include <isc/hex.h>
25
#include <isc/log.h>
26
#include <isc/md.h>
27
#include <isc/mem.h>
28
#include <isc/netaddr.h>
29
#include <isc/parseint.h>
30
#include <isc/platform.h>
31
#include <isc/print.h>
32
#include <isc/region.h>
33
#include <isc/result.h>
34
#include <isc/sockaddr.h>
Evan Hunt's avatar
Evan Hunt committed
35
#include <isc/string.h>
36
#include <isc/symtab.h>
Brian Wellington's avatar
Brian Wellington committed
37
#include <isc/util.h>
38

39
40
#include <pk11/site.h>

41
#include <dns/acl.h>
42
#include <dns/dnstap.h>
43
#include <dns/fixedname.h>
Mark Andrews's avatar
Mark Andrews committed
44
45
#include <dns/rdataclass.h>
#include <dns/rdatatype.h>
46
#include <dns/rrl.h>
47
#include <dns/secalg.h>
48
#include <dns/ssu.h>
49

50
51
#include <dst/dst.h>

52
#include <isccfg/aclconf.h>
53
#include <isccfg/cfg.h>
54
55
#include <isccfg/grammar.h>
#include <isccfg/namedconf.h>
56

57
58
#include <ns/hooks.h>

59
#include <bind9/check.h>
60

Evan Hunt's avatar
Evan Hunt committed
61
62
static unsigned char dlviscorg_ndata[] = "\003dlv\003isc\003org";
static unsigned char dlviscorg_offsets[] = { 0, 4, 8, 12 };
63
static dns_name_t const dlviscorg =
Evan Hunt's avatar
Evan Hunt committed
64
65
	DNS_NAME_INITABSOLUTE(dlviscorg_ndata, dlviscorg_offsets);

66
static isc_result_t
67
fileexist(const cfg_obj_t *obj, isc_symtab_t *symtab, bool writeable,
68
69
	  isc_log_t *logctxlogc);

70
71
72
73
74
75
76
static void
freekey(char *key, unsigned int type, isc_symvalue_t value, void *userarg) {
	UNUSED(type);
	UNUSED(value);
	isc_mem_free(userarg, key);
}

Mark Andrews's avatar
Mark Andrews committed
77
static isc_result_t
78
check_orderent(const cfg_obj_t *ent, isc_log_t *logctx) {
Mark Andrews's avatar
Mark Andrews committed
79
80
81
82
	isc_result_t result = ISC_R_SUCCESS;
	isc_result_t tresult;
	isc_textregion_t r;
	dns_fixedname_t fixed;
83
	const cfg_obj_t *obj;
Mark Andrews's avatar
Mark Andrews committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	dns_rdataclass_t rdclass;
	dns_rdatatype_t rdtype;
	isc_buffer_t b;
	const char *str;

	dns_fixedname_init(&fixed);
	obj = cfg_tuple_get(ent, "class");
	if (cfg_obj_isstring(obj)) {

		DE_CONST(cfg_obj_asstring(obj), r.base);
		r.length = strlen(r.base);
		tresult = dns_rdataclass_fromtext(&rdclass, &r);
		if (tresult != ISC_R_SUCCESS) {
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "rrset-order: invalid class '%s'",
				    r.base);
100
101
			if (result == ISC_R_SUCCESS)
				result = ISC_R_FAILURE;
Mark Andrews's avatar
Mark Andrews committed
102
103
104
105
106
107
108
109
110
111
112
113
		}
	}

	obj = cfg_tuple_get(ent, "type");
	if (cfg_obj_isstring(obj)) {
		DE_CONST(cfg_obj_asstring(obj), r.base);
		r.length = strlen(r.base);
		tresult = dns_rdatatype_fromtext(&rdtype, &r);
		if (tresult != ISC_R_SUCCESS) {
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "rrset-order: invalid type '%s'",
				    r.base);
114
115
			if (result == ISC_R_SUCCESS)
				result = ISC_R_FAILURE;
Mark Andrews's avatar
Mark Andrews committed
116
117
118
119
120
121
		}
	}

	obj = cfg_tuple_get(ent, "name");
	if (cfg_obj_isstring(obj)) {
		str = cfg_obj_asstring(obj);
122
		isc_buffer_constinit(&b, str, strlen(str));
Mark Andrews's avatar
Mark Andrews committed
123
124
		isc_buffer_add(&b, strlen(str));
		tresult = dns_name_fromtext(dns_fixedname_name(&fixed), &b,
125
					    dns_rootname, 0, NULL);
Mark Andrews's avatar
Mark Andrews committed
126
127
128
		if (tresult != ISC_R_SUCCESS) {
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "rrset-order: invalid name '%s'", str);
129
130
			if (result == ISC_R_SUCCESS)
				result = ISC_R_FAILURE;
Mark Andrews's avatar
Mark Andrews committed
131
132
133
134
135
136
137
138
		}
	}

	obj = cfg_tuple_get(ent, "order");
	if (!cfg_obj_isstring(obj) ||
	    strcasecmp("order", cfg_obj_asstring(obj)) != 0) {
		cfg_obj_log(ent, logctx, ISC_LOG_ERROR,
			    "rrset-order: keyword 'order' missing");
139
140
		if (result == ISC_R_SUCCESS)
			result = ISC_R_FAILURE;
Mark Andrews's avatar
Mark Andrews committed
141
142
143
144
145
146
	}

	obj = cfg_tuple_get(ent, "ordering");
	if (!cfg_obj_isstring(obj)) {
	    cfg_obj_log(ent, logctx, ISC_LOG_ERROR,
			"rrset-order: missing ordering");
147
148
		if (result == ISC_R_SUCCESS)
			result = ISC_R_FAILURE;
149
150
151
	} else if (strcasecmp(cfg_obj_asstring(obj), "fixed") == 0) {
#if !DNS_RDATASET_FIXED
		cfg_obj_log(obj, logctx, ISC_LOG_WARNING,
152
153
			    "rrset-order: order 'fixed' was disabled at "
			    "compilation time");
154
155
#endif
	} else if (strcasecmp(cfg_obj_asstring(obj), "random") != 0 &&
156
157
		   strcasecmp(cfg_obj_asstring(obj), "cyclic") != 0 &&
		   strcasecmp(cfg_obj_asstring(obj), "none") != 0) {
Mark Andrews's avatar
Mark Andrews committed
158
159
160
		cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
			    "rrset-order: invalid order '%s'",
			    cfg_obj_asstring(obj));
161
162
		if (result == ISC_R_SUCCESS)
			result = ISC_R_FAILURE;
Mark Andrews's avatar
Mark Andrews committed
163
164
165
166
167
	}
	return (result);
}

static isc_result_t
168
check_order(const cfg_obj_t *options, isc_log_t *logctx) {
Mark Andrews's avatar
Mark Andrews committed
169
170
	isc_result_t result = ISC_R_SUCCESS;
	isc_result_t tresult;
171
172
	const cfg_listelt_t *element;
	const cfg_obj_t *obj = NULL;
Mark Andrews's avatar
Mark Andrews committed
173
174
175
176
177
178
179
180
181

	if (cfg_map_get(options, "rrset-order", &obj) != ISC_R_SUCCESS)
		return (result);

	for (element = cfg_list_first(obj);
	     element != NULL;
	     element = cfg_list_next(element))
	{
		tresult = check_orderent(cfg_listelt_value(element), logctx);
182
		if (result == ISC_R_SUCCESS && tresult != ISC_R_SUCCESS)
Mark Andrews's avatar
Mark Andrews committed
183
184
185
186
187
			result = tresult;
	}
	return (result);
}

188
static isc_result_t
189
190
191
192
193
check_dual_stack(const cfg_obj_t *options, isc_log_t *logctx) {
	const cfg_listelt_t *element;
	const cfg_obj_t *alternates = NULL;
	const cfg_obj_t *value;
	const cfg_obj_t *obj;
194
	const char *str;
195
196
197
198
199
200
201
202
203
204
205
206
207
	dns_fixedname_t fixed;
	dns_name_t *name;
	isc_buffer_t buffer;
	isc_result_t result = ISC_R_SUCCESS;
	isc_result_t tresult;

	(void)cfg_map_get(options, "dual-stack-servers", &alternates);

	if (alternates == NULL)
		return (ISC_R_SUCCESS);

	obj = cfg_tuple_get(alternates, "port");
	if (cfg_obj_isuint32(obj)) {
208
209
		uint32_t val = cfg_obj_asuint32(obj);
		if (val > UINT16_MAX) {
210
211
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "port '%u' out of range", val);
212
213
			if (result == ISC_R_SUCCESS)
				result = ISC_R_RANGE;
214
215
216
217
218
219
220
221
222
223
224
		}
	}
	obj = cfg_tuple_get(alternates, "addresses");
	for (element = cfg_list_first(obj);
	     element != NULL;
	     element = cfg_list_next(element)) {
		value = cfg_listelt_value(element);
		if (cfg_obj_issockaddr(value))
			continue;
		obj = cfg_tuple_get(value, "name");
		str = cfg_obj_asstring(obj);
225
		isc_buffer_constinit(&buffer, str, strlen(str));
226
		isc_buffer_add(&buffer, strlen(str));
227
		name = dns_fixedname_initname(&fixed);
228
		tresult = dns_name_fromtext(name, &buffer, dns_rootname,
229
					    0, NULL);
230
231
232
		if (tresult != ISC_R_SUCCESS) {
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "bad name '%s'", str);
233
234
			if (result == ISC_R_SUCCESS)
				result = tresult;
235
236
237
		}
		obj = cfg_tuple_get(value, "port");
		if (cfg_obj_isuint32(obj)) {
238
239
			uint32_t val = cfg_obj_asuint32(obj);
			if (val > UINT16_MAX) {
240
241
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "port '%u' out of range", val);
242
243
				if (result == ISC_R_SUCCESS)
					result = ISC_R_RANGE;
244
245
246
247
248
249
			}
		}
	}
	return (result);
}

250
static isc_result_t
251
252
253
check_forward(const cfg_obj_t *options,  const cfg_obj_t *global,
	      isc_log_t *logctx)
{
254
255
	const cfg_obj_t *forward = NULL;
	const cfg_obj_t *forwarders = NULL;
256
257
258
259

	(void)cfg_map_get(options, "forward", &forward);
	(void)cfg_map_get(options, "forwarders", &forwarders);

260
261
262
263
264
265
266
267
268
	if (forwarders != NULL && global != NULL) {
		const char *file = cfg_obj_file(global);
		unsigned int line = cfg_obj_line(global);
		cfg_obj_log(forwarders, logctx, ISC_LOG_ERROR,
			    "forwarders declared in root zone and "
			    "in general configuration: %s:%u",
			    file, line);
		return (ISC_R_FAILURE);
	}
269
270
271
272
273
274
275
276
	if (forward != NULL && forwarders == NULL) {
		cfg_obj_log(forward, logctx, ISC_LOG_ERROR,
			    "no matching 'forwarders' statement");
		return (ISC_R_FAILURE);
	}
	return (ISC_R_SUCCESS);
}

277
static isc_result_t
278
disabled_algorithms(const cfg_obj_t *disabled, isc_log_t *logctx) {
279
280
	isc_result_t result = ISC_R_SUCCESS;
	isc_result_t tresult;
281
	const cfg_listelt_t *element;
282
283
284
285
	const char *str;
	isc_buffer_t b;
	dns_fixedname_t fixed;
	dns_name_t *name;
286
	const cfg_obj_t *obj;
287

288
	name = dns_fixedname_initname(&fixed);
289
290
	obj = cfg_tuple_get(disabled, "name");
	str = cfg_obj_asstring(obj);
291
	isc_buffer_constinit(&b, str, strlen(str));
292
	isc_buffer_add(&b, strlen(str));
293
	tresult = dns_name_fromtext(name, &b, dns_rootname, 0, NULL);
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
	if (tresult != ISC_R_SUCCESS) {
		cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
			    "bad domain name '%s'", str);
		result = tresult;
	}

	obj = cfg_tuple_get(disabled, "algorithms");

	for (element = cfg_list_first(obj);
	     element != NULL;
	     element = cfg_list_next(element))
	{
		isc_textregion_t r;
		dns_secalg_t alg;

309
		DE_CONST(cfg_obj_asstring(cfg_listelt_value(element)), r.base);
310
311
312
		r.length = strlen(r.base);

		tresult = dns_secalg_fromtext(&alg, &r);
Tinderbox User's avatar
Tinderbox User committed
313
		if (tresult != ISC_R_SUCCESS) {
314
			cfg_obj_log(cfg_listelt_value(element), logctx,
315
316
				    ISC_LOG_ERROR, "invalid algorithm '%s'",
				    r.base);
317
			result = tresult;
Tinderbox User's avatar
Tinderbox User committed
318
		}
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
	}
	return (result);
}

static isc_result_t
disabled_ds_digests(const cfg_obj_t *disabled, isc_log_t *logctx) {
	isc_result_t result = ISC_R_SUCCESS;
	isc_result_t tresult;
	const cfg_listelt_t *element;
	const char *str;
	isc_buffer_t b;
	dns_fixedname_t fixed;
	dns_name_t *name;
	const cfg_obj_t *obj;

334
	name = dns_fixedname_initname(&fixed);
335
336
	obj = cfg_tuple_get(disabled, "name");
	str = cfg_obj_asstring(obj);
337
	isc_buffer_constinit(&b, str, strlen(str));
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
	isc_buffer_add(&b, strlen(str));
	tresult = dns_name_fromtext(name, &b, dns_rootname, 0, NULL);
	if (tresult != ISC_R_SUCCESS) {
		cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
			    "bad domain name '%s'", str);
		result = tresult;
	}

	obj = cfg_tuple_get(disabled, "digests");

	for (element = cfg_list_first(obj);
	     element != NULL;
	     element = cfg_list_next(element))
	{
		isc_textregion_t r;
		dns_dsdigest_t digest;

		DE_CONST(cfg_obj_asstring(cfg_listelt_value(element)), r.base);
		r.length = strlen(r.base);

		/* works with a numeric argument too */
		tresult = dns_dsdigest_fromtext(&digest, &r);
Tinderbox User's avatar
Tinderbox User committed
360
361
		if (tresult != ISC_R_SUCCESS) {
			cfg_obj_log(cfg_listelt_value(element), logctx,
362
				    ISC_LOG_ERROR, "invalid digest type '%s'",
Tinderbox User's avatar
Tinderbox User committed
363
364
365
				    r.base);
			result = tresult;
		}
366
367
368
369
	}
	return (result);
}

370
static isc_result_t
371
372
373
nameexist(const cfg_obj_t *obj, const char *name, int value,
	  isc_symtab_t *symtab, const char *fmt, isc_log_t *logctx,
	  isc_mem_t *mctx)
374
375
376
377
378
379
380
381
382
383
{
	char *key;
	const char *file;
	unsigned int line;
	isc_result_t result;
	isc_symvalue_t symvalue;

	key = isc_mem_strdup(mctx, name);
	if (key == NULL)
		return (ISC_R_NOMEMORY);
384
	symvalue.as_cpointer = obj;
385
386
387
388
389
	result = isc_symtab_define(symtab, key, value, symvalue,
				   isc_symexists_reject);
	if (result == ISC_R_EXISTS) {
		RUNTIME_CHECK(isc_symtab_lookup(symtab, key, value,
						&symvalue) == ISC_R_SUCCESS);
390
391
		file = cfg_obj_file(symvalue.as_cpointer);
		line = cfg_obj_line(symvalue.as_cpointer);
392
393
394
395
396
397
398
399
400
401
402
403

		if (file == NULL)
			file = "<unknown file>";
		cfg_obj_log(obj, logctx, ISC_LOG_ERROR, fmt, key, file, line);
		isc_mem_free(mctx, key);
		result = ISC_R_EXISTS;
	} else if (result != ISC_R_SUCCESS) {
		isc_mem_free(mctx, key);
	}
	return (result);
}

404
static isc_result_t
405
mustbesecure(const cfg_obj_t *secure, isc_symtab_t *symtab, isc_log_t *logctx,
406
407
	     isc_mem_t *mctx)
{
408
	const cfg_obj_t *obj;
409
410
411
412
413
414
415
	char namebuf[DNS_NAME_FORMATSIZE];
	const char *str;
	dns_fixedname_t fixed;
	dns_name_t *name;
	isc_buffer_t b;
	isc_result_t result = ISC_R_SUCCESS;

416
	name = dns_fixedname_initname(&fixed);
417
418
	obj = cfg_tuple_get(secure, "name");
	str = cfg_obj_asstring(obj);
419
	isc_buffer_constinit(&b, str, strlen(str));
420
	isc_buffer_add(&b, strlen(str));
421
	result = dns_name_fromtext(name, &b, dns_rootname, 0, NULL);
422
	if (result != ISC_R_SUCCESS) {
423
424
425
426
		cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
			    "bad domain name '%s'", str);
	} else {
		dns_name_format(name, namebuf, sizeof(namebuf));
427
428
429
430
		result = nameexist(secure, namebuf, 1, symtab,
				   "dnssec-must-be-secure '%s': already "
				   "exists previous definition: %s:%u",
				   logctx, mctx);
431
432
433
434
	}
	return (result);
}

435
static isc_result_t
436
437
438
checkacl(const char *aclname, cfg_aclconfctx_t *actx, const cfg_obj_t *zconfig,
	 const cfg_obj_t *voptions, const cfg_obj_t *config,
	 isc_log_t *logctx, isc_mem_t *mctx)
439
440
{
	isc_result_t result;
441
442
	const cfg_obj_t *aclobj = NULL;
	const cfg_obj_t *options;
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
	dns_acl_t *acl = NULL;

	if (zconfig != NULL) {
		options = cfg_tuple_get(zconfig, "options");
		cfg_map_get(options, aclname, &aclobj);
	}
	if (voptions != NULL && aclobj == NULL)
		cfg_map_get(voptions, aclname, &aclobj);
	if (config != NULL && aclobj == NULL) {
		options = NULL;
		cfg_map_get(config, "options", &options);
		if (options != NULL)
			cfg_map_get(options, aclname, &aclobj);
	}
	if (aclobj == NULL)
		return (ISC_R_SUCCESS);
459
	result = cfg_acl_fromconfig(aclobj, config, logctx,
Automatic Updater's avatar
Automatic Updater committed
460
				    actx, mctx, 0, &acl);
461
462
463
464
465
466
	if (acl != NULL)
		dns_acl_detach(&acl);
	return (result);
}

static isc_result_t
467
468
check_viewacls(cfg_aclconfctx_t *actx, const cfg_obj_t *voptions,
	       const cfg_obj_t *config, isc_log_t *logctx, isc_mem_t *mctx)
469
470
471
{
	isc_result_t result = ISC_R_SUCCESS, tresult;
	int i = 0;
Automatic Updater's avatar
Automatic Updater committed
472

473
	static const char *acls[] = { "allow-query", "allow-query-on",
Automatic Updater's avatar
Automatic Updater committed
474
		"allow-query-cache", "allow-query-cache-on",
475
		"blackhole", "keep-response-order", "match-clients",
476
		"match-destinations", "sortlist", NULL };
477
478

	while (acls[i] != NULL) {
479
		tresult = checkacl(acls[i++], actx, NULL, voptions, config,
480
481
				   logctx, mctx);
		if (tresult != ISC_R_SUCCESS)
Automatic Updater's avatar
Automatic Updater committed
482
			result = tresult;
483
484
485
486
	}
	return (result);
}

487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
static isc_result_t
check_non_viewacls(const cfg_obj_t *voptions, const cfg_obj_t *config,
		   isc_log_t *logctx)
{
	const cfg_obj_t *aclobj = NULL;
	const cfg_obj_t *options;
	const char *where = NULL;
	int i;

	static const char *acls[] = {
		"allow-update", "allow-update-forwarding", NULL
	};

	for (i = 0; acls[i] != NULL; i++) {
		if (voptions != NULL && aclobj == NULL) {
			cfg_map_get(voptions, acls[i], &aclobj);
			where = "view";
		}
		if (config != NULL && aclobj == NULL) {
			options = NULL;
			cfg_map_get(config, "options", &options);
			if (options != NULL) {
				cfg_map_get(options, acls[i], &aclobj);
				where = "options";
			}
		}
		if (aclobj != NULL) {
			cfg_obj_log(aclobj, logctx, ISC_LOG_ERROR,
				    "'%s' can only be set per-zone, "
				    "not in '%s'", acls[i], where);
			return (ISC_R_FAILURE);
		}
	}

	return (ISC_R_SUCCESS);
}

524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
static const unsigned char zeros[16];

static isc_result_t
check_dns64(cfg_aclconfctx_t *actx, const cfg_obj_t *voptions,
	    const cfg_obj_t *config, isc_log_t *logctx, isc_mem_t *mctx)
{
	isc_result_t result = ISC_R_SUCCESS;
	const cfg_obj_t *dns64 = NULL;
	const cfg_obj_t *options;
	const cfg_listelt_t *element;
	const cfg_obj_t *map, *obj;
	isc_netaddr_t na, sa;
	unsigned int prefixlen;
	int nbytes;
	int i;

540
	static const char *acls[] = { "clients", "exclude", "mapped", NULL};
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

	if (voptions != NULL)
		cfg_map_get(voptions, "dns64", &dns64);
	if (config != NULL && dns64 == NULL) {
		options = NULL;
		cfg_map_get(config, "options", &options);
		if (options != NULL)
			cfg_map_get(options, "dns64", &dns64);
	}
	if (dns64 == NULL)
		return (ISC_R_SUCCESS);

	for (element = cfg_list_first(dns64);
	     element != NULL;
	     element = cfg_list_next(element))
	{
		map = cfg_listelt_value(element);
		obj = cfg_map_getname(map);

		cfg_obj_asnetprefix(obj, &na, &prefixlen);
		if (na.family != AF_INET6) {
			cfg_obj_log(map, logctx, ISC_LOG_ERROR,
				    "dns64 requires a IPv6 prefix");
			result = ISC_R_FAILURE;
			continue;
		}
Automatic Updater's avatar
Automatic Updater committed
567

568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
		if (prefixlen != 32 && prefixlen != 40 && prefixlen != 48 &&
		    prefixlen != 56 && prefixlen != 64 && prefixlen != 96) {
			cfg_obj_log(map, logctx, ISC_LOG_ERROR,
				    "bad prefix length %u [32/40/48/56/64/96]",
				    prefixlen);
			result = ISC_R_FAILURE;
			continue;
		}

		for (i = 0; acls[i] != NULL; i++) {
			obj = NULL;
			(void)cfg_map_get(map, acls[i], &obj);
			if (obj != NULL) {
				dns_acl_t *acl = NULL;
				isc_result_t tresult;

				tresult = cfg_acl_fromconfig(obj, config,
							     logctx, actx,
							     mctx, 0, &acl);
				if (acl != NULL)
					dns_acl_detach(&acl);
				if (tresult != ISC_R_SUCCESS)
					result = tresult;
			}
		}

		obj = NULL;
		(void)cfg_map_get(map, "suffix", &obj);
		if (obj != NULL) {
			isc_netaddr_fromsockaddr(&sa, cfg_obj_assockaddr(obj));
			if (sa.family != AF_INET6) {
				cfg_obj_log(map, logctx, ISC_LOG_ERROR,
					    "dns64 requires a IPv6 suffix");
				result = ISC_R_FAILURE;
				continue;
			}
			nbytes = prefixlen / 8 + 4;
Mark Andrews's avatar
Mark Andrews committed
605
			if (prefixlen <= 64)
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
				nbytes++;
			if (memcmp(sa.type.in6.s6_addr, zeros, nbytes) != 0) {
				char netaddrbuf[ISC_NETADDR_FORMATSIZE];
				isc_netaddr_format(&sa, netaddrbuf,
						   sizeof(netaddrbuf));
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "bad suffix '%s' leading "
					    "%u octets not zeros",
					    netaddrbuf, nbytes);
				result = ISC_R_FAILURE;
			}
		}
	}

	return (result);
}

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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#define CHECK_RRL(cond, pat, val1, val2)				\
	do {								\
		if (!(cond)) {						\
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,		\
				    pat, val1, val2);			\
			if (result == ISC_R_SUCCESS)			\
				result = ISC_R_RANGE;			\
		    }							\
	} while (0)

#define CHECK_RRL_RATE(rate, def, max_rate, name)			\
	do {								\
		obj = NULL;						\
		mresult = cfg_map_get(map, name, &obj);			\
		if (mresult == ISC_R_SUCCESS) {				\
			rate = cfg_obj_asuint32(obj);			\
			CHECK_RRL(rate <= max_rate, name" %d > %d",	\
				  rate, max_rate);			\
		}							\
	} while (0)

static isc_result_t
check_ratelimit(cfg_aclconfctx_t *actx, const cfg_obj_t *voptions,
		const cfg_obj_t *config, isc_log_t *logctx, isc_mem_t *mctx)
{
	isc_result_t result = ISC_R_SUCCESS;
	isc_result_t mresult;
	const cfg_obj_t *map = NULL;
	const cfg_obj_t *options;
	const cfg_obj_t *obj;
	int min_entries, i;
	int all_per_second;
	int errors_per_second;
	int nodata_per_second;
	int nxdomains_per_second;
	int referrals_per_second;
	int responses_per_second;
	int slip;

	if (voptions != NULL)
		cfg_map_get(voptions, "rate-limit", &map);
	if (config != NULL && map == NULL) {
		options = NULL;
		cfg_map_get(config, "options", &options);
		if (options != NULL)
			cfg_map_get(options, "rate-limit", &map);
	}
	if (map == NULL)
		return (ISC_R_SUCCESS);

	min_entries = 500;
	obj = NULL;
	mresult = cfg_map_get(map, "min-table-size", &obj);
	if (mresult == ISC_R_SUCCESS) {
		min_entries = cfg_obj_asuint32(obj);
		if (min_entries < 1)
			min_entries = 1;
	}

	obj = NULL;
	mresult = cfg_map_get(map, "max-table-size", &obj);
	if (mresult == ISC_R_SUCCESS) {
		i = cfg_obj_asuint32(obj);
		CHECK_RRL(i >= min_entries,
			  "max-table-size %d < min-table-size %d",
			  i, min_entries);
	}

	CHECK_RRL_RATE(responses_per_second, 0, DNS_RRL_MAX_RATE,
		       "responses-per-second");

	CHECK_RRL_RATE(referrals_per_second, responses_per_second,
		       DNS_RRL_MAX_RATE, "referrals-per-second");
	CHECK_RRL_RATE(nodata_per_second, responses_per_second,
		       DNS_RRL_MAX_RATE, "nodata-per-second");
	CHECK_RRL_RATE(nxdomains_per_second, responses_per_second,
		       DNS_RRL_MAX_RATE, "nxdomains-per-second");
	CHECK_RRL_RATE(errors_per_second, responses_per_second,
		       DNS_RRL_MAX_RATE, "errors-per-second");

	CHECK_RRL_RATE(all_per_second, 0, DNS_RRL_MAX_RATE, "all-per-second");

	CHECK_RRL_RATE(slip, 2, DNS_RRL_MAX_SLIP, "slip");

	obj = NULL;
	mresult = cfg_map_get(map, "window", &obj);
	if (mresult == ISC_R_SUCCESS) {
		i = cfg_obj_asuint32(obj);
		CHECK_RRL(i >= 1 && i <= DNS_RRL_MAX_WINDOW,
			  "window %d < 1 or > %d", i, DNS_RRL_MAX_WINDOW);
	}

	obj = NULL;
	mresult = cfg_map_get(map, "qps-scale", &obj);
	if (mresult == ISC_R_SUCCESS) {
		i = cfg_obj_asuint32(obj);
		CHECK_RRL(i >= 1, "invalid 'qps-scale %d'%s", i, "");
	}

	obj = NULL;
	mresult = cfg_map_get(map, "ipv4-prefix-length", &obj);
	if (mresult == ISC_R_SUCCESS) {
		i = cfg_obj_asuint32(obj);
		CHECK_RRL(i >= 8 && i <= 32,
			  "invalid 'ipv4-prefix-length %d'%s", i, "");
	}

	obj = NULL;
	mresult = cfg_map_get(map, "ipv6-prefix-length", &obj);
	if (mresult == ISC_R_SUCCESS) {
		i = cfg_obj_asuint32(obj);
		CHECK_RRL(i >= 16 && i <= DNS_RRL_MAX_PREFIX,
			  "ipv6-prefix-length %d < 16 or > %d",
			  i, DNS_RRL_MAX_PREFIX);
	}

	obj = NULL;
	(void)cfg_map_get(map, "exempt-clients", &obj);
	if (obj != NULL) {
		dns_acl_t *acl = NULL;
		isc_result_t tresult;

		tresult = cfg_acl_fromconfig(obj, config, logctx, actx,
					     mctx, 0, &acl);
		if (acl != NULL)
			dns_acl_detach(&acl);
		if (result == ISC_R_SUCCESS)
			result = tresult;
	}

	return (result);
}
755

756
757
758
759
760
761
762
763
764
765
766
767
/*
 * Check allow-recursion and allow-recursion-on acls, and also log a
 * warning if they're inconsistent with the "recursion" option.
 */
static isc_result_t
check_recursionacls(cfg_aclconfctx_t *actx, const cfg_obj_t *voptions,
		    const char *viewname, const cfg_obj_t *config,
		    isc_log_t *logctx, isc_mem_t *mctx)
{
	const cfg_obj_t *options, *aclobj, *obj = NULL;
	dns_acl_t *acl = NULL;
	isc_result_t result = ISC_R_SUCCESS, tresult;
768
	bool recursion;
769
770
771
772
	const char *forview = " for view ";
	int i = 0;

	static const char *acls[] = { "allow-recursion", "allow-recursion-on",
Automatic Updater's avatar
Automatic Updater committed
773
774
				      NULL };

775
776
777
778
779
780
781
782
783
	if (voptions != NULL)
		cfg_map_get(voptions, "recursion", &obj);
	if (obj == NULL && config != NULL) {
		options = NULL;
		cfg_map_get(config, "options", &options);
		if (options != NULL)
			cfg_map_get(options, "recursion", &obj);
	}
	if (obj == NULL)
784
		recursion = true;
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
	else
		recursion = cfg_obj_asboolean(obj);

	if (viewname == NULL) {
		viewname = "";
		forview = "";
	}

	for (i = 0; acls[i] != NULL; i++) {
		aclobj = options = NULL;
		acl = NULL;

		if (voptions != NULL)
			cfg_map_get(voptions, acls[i], &aclobj);
		if (config != NULL && aclobj == NULL) {
			options = NULL;
			cfg_map_get(config, "options", &options);
			if (options != NULL)
				cfg_map_get(options, acls[i], &aclobj);
		}
Automatic Updater's avatar
Automatic Updater committed
805
		if (aclobj == NULL)
806
807
808
			continue;

		tresult = cfg_acl_fromconfig(aclobj, config, logctx,
809
					    actx, mctx, 0, &acl);
810
811

		if (tresult != ISC_R_SUCCESS)
Automatic Updater's avatar
Automatic Updater committed
812
			result = tresult;
813
814
815
816

		if (acl == NULL)
			continue;

817
		if (recursion == false && !dns_acl_isnone(acl)) {
818
819
820
821
822
823
824
825
			cfg_obj_log(aclobj, logctx, ISC_LOG_WARNING,
				    "both \"recursion no;\" and "
				    "\"%s\" active%s%s",
				    acls[i], forview, viewname);
		}

		if (acl != NULL)
			dns_acl_detach(&acl);
Automatic Updater's avatar
Automatic Updater committed
826
	}
827
828
829
830

	return (result);
}

831
832
833
typedef struct {
	const char *name;
	unsigned int scale;
834
	unsigned int max;
835
836
} intervaltable;

837
#ifdef HAVE_DNSTAP
838
839
840
841
842
typedef struct {
	const char *name;
	unsigned int min;
	unsigned int max;
} fstrmtable;
843
#endif
844

845
846
847
848
849
850
851
typedef enum {
	optlevel_config,
	optlevel_options,
	optlevel_view,
	optlevel_zone
} optlevel_t;

Evan Hunt's avatar
Evan Hunt committed
852
853
854
855
856
857
858
859
860
861
862
static isc_result_t
check_dscp(const cfg_obj_t *options, isc_log_t *logctx) {
       isc_result_t result = ISC_R_SUCCESS;
       const cfg_obj_t *obj = NULL;

       /*
	* Check that DSCP setting is within range
	*/
       obj = NULL;
       (void)cfg_map_get(options, "dscp", &obj);
       if (obj != NULL) {
863
	       uint32_t dscp = cfg_obj_asuint32(obj);
Evan Hunt's avatar
Evan Hunt committed
864
865
866
867
868
869
870
871
872
873
	       if (dscp >= 64) {
		       cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				   "'dscp' out of range (0-63)");
		       result = ISC_R_FAILURE;
	       }
       }

       return (result);
}

874
875
876
877
878
879
880
881
static isc_result_t
check_name(const char *str) {
	dns_fixedname_t fixed;

	dns_fixedname_init(&fixed);
	return (dns_name_fromstring(dns_fixedname_name(&fixed), str, 0, NULL));
}

882
static isc_result_t
883
884
885
check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
	      optlevel_t optlevel)
{
886
	isc_result_t result = ISC_R_SUCCESS;
887
	isc_result_t tresult;
888
	unsigned int i;
889
	const cfg_obj_t *obj = NULL;
890
	const cfg_obj_t *resignobj = NULL;
891
	const cfg_listelt_t *element;
892
	isc_symtab_t *symtab = NULL;
893
894
895
896
	dns_fixedname_t fixed;
	const char *str;
	dns_name_t *name;
	isc_buffer_t b;
897
	uint32_t lifetime = 3600;
898
	const char *ccalg = "aes";
899

900
901
902
903
	/*
	 * { "name", scale, value }
	 * (scale * value) <= UINT32_MAX
	 */
904
	static intervaltable intervals[] = {
905
906
907
908
909
910
911
912
913
914
915
916
917
918
		{ "cleaning-interval", 60, 28 * 24 * 60 },	/* 28 days */
		{ "heartbeat-interval", 60, 28 * 24 * 60 },	/* 28 days */
		{ "interface-interval", 60, 28 * 24 * 60 },	/* 28 days */
		{ "max-transfer-idle-in", 60, 28 * 24 * 60 },	/* 28 days */
		{ "max-transfer-idle-out", 60, 28 * 24 * 60 },	/* 28 days */
		{ "max-transfer-time-in", 60, 28 * 24 * 60 },	/* 28 days */
		{ "max-transfer-time-out", 60, 28 * 24 * 60 },	/* 28 days */
		{ "statistics-interval", 60, 28 * 24 * 60 },	/* 28 days */

		/* minimum and maximum cache and negative cache TTLs */
		{ "min-cache-ttl", 1, MAX_MIN_CACHE_TTL },	/* 90 secs */
		{ "max-cache-ttl", 1, UINT32_MAX },		/* no limit */
		{ "min-ncache-ttl", 1, MAX_MIN_NCACHE_TTL},	/* 90 secs */
		{ "max-ncache-ttl", 1, MAX_MAX_NCACHE_TTL },	/*  7 days */
919
920
	};

921
922
923
924
925
926
	static const char *server_contact[] = {
		"empty-server", "empty-contact",
		"dns64-server", "dns64-contact",
		NULL
	};

927
#ifdef HAVE_DNSTAP
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
	static fstrmtable fstrm[] = {
		{
			"fstrm-set-buffer-hint",
			FSTRM_IOTHR_BUFFER_HINT_MIN,
			FSTRM_IOTHR_BUFFER_HINT_MAX
		},
		{
			"fstrm-set-flush-timeout",
			FSTRM_IOTHR_FLUSH_TIMEOUT_MIN,
			FSTRM_IOTHR_FLUSH_TIMEOUT_MAX
		},
		{
			"fstrm-set-input-queue-size",
			FSTRM_IOTHR_INPUT_QUEUE_SIZE_MIN,
			FSTRM_IOTHR_INPUT_QUEUE_SIZE_MAX
		},
		{
			"fstrm-set-output-notify-threshold",
			FSTRM_IOTHR_QUEUE_NOTIFY_THRESHOLD_MIN,
			0
		},
		{
			"fstrm-set-output-queue-size",
			FSTRM_IOTHR_OUTPUT_QUEUE_SIZE_MIN,
			FSTRM_IOTHR_OUTPUT_QUEUE_SIZE_MAX
		},
		{
			"fstrm-set-reopen-interval",
			FSTRM_IOTHR_REOPEN_INTERVAL_MIN,
			FSTRM_IOTHR_REOPEN_INTERVAL_MAX
		}
	};
#endif

962
963
964
965
966
	/*
	 * Check that fields specified in units of time other than seconds
	 * have reasonable values.
	 */
	for (i = 0; i < sizeof(intervals) / sizeof(intervals[0]); i++) {
967
		uint32_t val;
968
		obj = NULL;
969
970
971
972
		(void)cfg_map_get(options, intervals[i].name, &obj);
		if (obj == NULL)
			continue;
		val = cfg_obj_asuint32(obj);
973
974
975
976
977
978
		if (val > intervals[i].max) {
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "%s '%u' is out of range (0..%u)",
				    intervals[i].name, val,
				    intervals[i].max);
			result = ISC_R_RANGE;
979
		} else if (val > (UINT32_MAX / intervals[i].scale)) {
980
981
982
983
984
985
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "%s '%d' is out of range",
				    intervals[i].name, val);
			result = ISC_R_RANGE;
		}
	}
986

987
988
989
	obj = NULL;
	cfg_map_get(options, "max-rsa-exponent-size", &obj);
	if (obj != NULL) {
990
		uint32_t val;
991
992
993
994
995
996
997
998
999
1000

		val = cfg_obj_asuint32(obj);
		if (val != 0 && (val < 35 || val > 4096)) {
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "max-rsa-exponent-size '%u' is out of "
				    "range (35..4096)", val);
			result = ISC_R_RANGE;
		}
	}

1001
1002
1003
	obj = NULL;
	cfg_map_get(options, "sig-validity-interval", &obj);
	if (obj != NULL) {
1004
		uint32_t validity, resign = 0;
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036

		validity = cfg_obj_asuint32(cfg_tuple_get(obj, "validity"));
		resignobj = cfg_tuple_get(obj, "re-sign");
		if (!cfg_obj_isvoid(resignobj))
			resign = cfg_obj_asuint32(resignobj);

		if (validity > 3660 || validity == 0) { /* 10 years */
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "%s '%u' is out of range (1..3660)",
				    "sig-validity-interval", validity);
			result = ISC_R_RANGE;
		}

		if (!cfg_obj_isvoid(resignobj)) {
			if (resign > 3660 || resign == 0) { /* 10 years */
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "%s '%u' is out of range (1..3660)",
					    "sig-validity-interval (re-sign)",
					    validity);
				result = ISC_R_RANGE;
			} else if ((validity > 7 && validity < resign) ||
				   (validity <= 7 && validity * 24 < resign)) {
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "validity interval (%u days) "
					    "less than re-signing interval "
					    "(%u %s)", validity, resign,
					    (validity > 7) ? "days" : "hours");
				result = ISC_R_RANGE;
			}
		}
	}

Evan Hunt's avatar
Evan Hunt committed
1037
1038
1039
	obj = NULL;
	cfg_map_get(options, "dnskey-sig-validity", &obj);
	if (obj != NULL) {
1040
		uint32_t keyvalidity;
Evan Hunt's avatar
Evan Hunt committed
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052

		keyvalidity = cfg_obj_asuint32(obj);
		if (keyvalidity > 3660 || keyvalidity == 0) { /* 10 years */
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "%s '%u' is out of range (1..3660)",
				    "dnskey-sig-validity",
				    keyvalidity);
			result = ISC_R_RANGE;
		}

	}

1053
1054
1055
	obj = NULL;
	(void)cfg_map_get(options, "preferred-glue", &obj);
	if (obj != NULL) {
Automatic Updater's avatar
Automatic Updater committed
1056
1057
		str = cfg_obj_asstring(obj);
		if (strcasecmp(str, "a") != 0 &&
1058
1059
1060
1061
1062
1063
		    strcasecmp(str, "aaaa") != 0 &&
		    strcasecmp(str, "none") != 0)
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "preferred-glue unexpected value '%s'",
				    str);
	}
1064

1065
1066
1067
1068
1069
1070
1071
	obj = NULL;
	(void)cfg_map_get(options, "root-delegation-only", &obj);
	if (obj != NULL) {
		if (!cfg_obj_isvoid(obj)) {
			for (element = cfg_list_first(obj);
			     element != NULL;
			     element = cfg_list_next(element)) {
1072
1073
				const cfg_obj_t *exclude;

1074
1075
				exclude = cfg_listelt_value(element);
				str = cfg_obj_asstring(exclude);
1076
				tresult = check_name(str);
1077
1078
1079
1080
1081
1082
1083
1084
1085
				if (tresult != ISC_R_SUCCESS) {
					cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
						    "bad domain name '%s'",
						    str);
					result = tresult;
				}
			}
		}
	}
Automatic Updater's avatar
Automatic Updater committed
1086

1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
	/*
	 * Set supported DNSSEC algorithms.
	 */
	obj = NULL;
	(void)cfg_map_get(options, "disable-algorithms", &obj);
	if (obj != NULL) {
		for (element = cfg_list_first(obj);
		     element != NULL;
		     element = cfg_list_next(element))
		{
			obj = cfg_listelt_value(element);
			tresult = disabled_algorithms(obj, logctx);
			if (tresult != ISC_R_SUCCESS)
				result = tresult;
		}
	}

1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
	/*
	 * Set supported DS/DLV digest types.
	 */
	obj = NULL;
	(void)cfg_map_get(options, "disable-ds-digests", &obj);
	if (obj != NULL) {
		for (element = cfg_list_first(obj);
		     element != NULL;
		     element = cfg_list_next(element))
		{
			obj = cfg_listelt_value(element);
			tresult = disabled_ds_digests(obj, logctx);
			if (tresult != ISC_R_SUCCESS)
				result = tresult;
		}
	}

1121
	name = dns_fixedname_initname(&fixed);
1122

1123
1124
1125
1126
1127
1128
	/*
	 * Check the DLV zone name.
	 */
	obj = NULL;
	(void)cfg_map_get(options, "dnssec-lookaside", &obj);
	if (obj != NULL) {
1129
		tresult = isc_symtab_create(mctx, 100, freekey, mctx,
1130
					    false, &symtab);
1131
		if (tresult != ISC_R_SUCCESS)
1132
			result = tresult;
1133
1134
1135
1136
1137
		for (element = cfg_list_first(obj);
		     element != NULL;
		     element = cfg_list_next(element))
		{
			const char *dlv;
1138
			const cfg_obj_t *dlvobj, *anchor;
1139
1140
1141

			obj = cfg_listelt_value(element);

1142
			anchor = cfg_tuple_get(obj, "trust-anchor");
1143
1144
			dlvobj = cfg_tuple_get(obj, "domain");
			dlv = cfg_obj_asstring(dlvobj);
1145
1146

			/*
1147
1148
			 * If domain is "auto" or "no" and trust anchor
			 * is missing, skip remaining tests
Automatic Updater's avatar
Automatic Updater committed
1149
			 */
1150
			if (cfg_obj_isvoid(anchor)) {
Evan Hunt's avatar
Evan Hunt committed
1151
				if (!strcasecmp(dlv, "no")) {
1152
					continue;
Evan Hunt's avatar
Evan Hunt committed
1153
1154
				}
				if (!strcasecmp(dlv, "auto")) {
1155
					cfg_obj_log(obj, logctx, ISC_LOG_WARNING,
Evan Hunt's avatar
Evan Hunt committed
1156
1157
1158
1159
						    "dnssec-lookaside 'auto' "
						    "is no longer supported");
					continue;
				}
1160
			}
1161

1162
			tresult = dns_name_fromstring(name, dlv, 0, NULL);
1163
1164
1165
1166
			if (tresult != ISC_R_SUCCESS) {
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "bad domain name '%s'", dlv);
				result = tresult;
1167
				continue;
1168
1169
1170
1171
			}
			if (symtab != NULL) {
				tresult = nameexist(obj, dlv, 1, symtab,
						    "dnssec-lookaside '%s': "
Evan Hunt's avatar
Evan Hunt committed
1172
						    "already exists; previous "
1173
1174
1175
1176
1177
1178
						    "definition: %s:%u",
						    logctx, mctx);
				if (tresult != ISC_R_SUCCESS &&
				    result == ISC_R_SUCCESS)
					result = tresult;
			}
1179

1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
			/*
			 * XXXMPA to be removed when multiple lookaside
			 * namespaces are supported.
			 */
			if (!dns_name_equal(dns_rootname, name)) {
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "dnssec-lookaside '%s': "
					    "non-root not yet supported", dlv);
				if (result == ISC_R_SUCCESS)
					result = ISC_R_FAILURE;
			}
1191

Evan Hunt's avatar
Evan Hunt committed
1192
			if (cfg_obj_isvoid(anchor)) {
1193
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
Evan Hunt's avatar
Evan Hunt committed
1194
1195
1196
					    "dnssec-lookaside requires "
					    "either or 'no' or a "
					    "domain and trust anchor");
1197
				if (result == ISC_R_SUCCESS)
1198
					result = ISC_R_FAILURE;
Evan Hunt's avatar
Evan Hunt committed
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
				continue;
			}

			dlv = cfg_obj_asstring(anchor);
			tresult = dns_name_fromstring(name, dlv, 0, NULL);
			if (tresult != ISC_R_SUCCESS) {
				cfg_obj_log(anchor, logctx, ISC_LOG_ERROR,
					    "bad domain name '%s'", dlv);
				if (result == ISC_R_SUCCESS)
					result = tresult;
				continue;
			}
			if (dns_name_equal(&dlviscorg, name)) {
1212
				cfg_obj_log(anchor, logctx, ISC_LOG_WARNING,
Evan Hunt's avatar
Evan Hunt committed
1213
1214
					    "dlv.isc.org has been shut down");
				continue;
1215
			}
1216
		}
1217

1218
1219
		if (symtab != NULL)
			isc_symtab_destroy(&symtab);
1220
	}
1221

1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
	/*
	 * Check auto-dnssec at the view/options level
	 */
	obj = NULL;
	(void)cfg_map_get(options, "auto-dnssec", &obj);
	if (obj != NULL) {
		const char *arg = cfg_obj_asstring(obj);
		if (optlevel != optlevel_zone && strcasecmp(arg, "off") != 0) {
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "auto-dnssec may only be activated at the "
				    "zone level");
1233
1234
			if (result == ISC_R_SUCCESS)
				result = ISC_R_FAILURE;
1235
1236
1237
		}
	}

1238
1239
1240
1241
1242
1243
1244
	/*
	 * Check dnssec-must-be-secure.
	 */
	obj = NULL;
	(void)cfg_map_get(options, "dnssec-must-be-secure", &obj);
	if (obj != NULL) {
		tresult = isc_symtab_create(mctx, 100, freekey, mctx,
1245
					    false, &symtab);
1246
1247
1248
1249
1250
1251
1252
1253
		if (tresult != ISC_R_SUCCESS)
			result = tresult;
		for (element = cfg_list_first(obj);
		     element != NULL;
		     element = cfg_list_next(element))
		{
			obj = cfg_listelt_value(element);
			tresult = mustbesecure(obj, symtab, logctx, mctx);
1254
			if (result == ISC_R_SUCCESS && tresult != ISC_R_SUCCESS)
1255
1256
1257
1258
1259
1260
				result = tresult;
		}
		if (symtab != NULL)
			isc_symtab_destroy(&symtab);
	}

1261
	/*
1262
	 * Check server/contacts for syntactic validity.
1263
	 */
1264
1265
1266
1267
1268
	for (i= 0; server_contact[i] != NULL; i++) {
		obj = NULL;
		(void)cfg_map_get(options, server_contact[i], &obj);
		if (obj != NULL) {
			str = cfg_obj_asstring(obj);
1269
			if (check_name(str) != ISC_R_SUCCESS) {
1270
1271
1272
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "%s: invalid name '%s'",
					    server_contact[i], str);
1273
1274
				if (result == ISC_R_SUCCESS)
					result = ISC_R_FAILURE;
1275
			}
1276
1277
1278
		}
	}

1279
1280
1281
	/*
	 * Check empty zone configuration.
	 */
1282
1283
1284
1285
1286
1287
1288
1289
	obj = NULL;
	(void)cfg_map_get(options, "disable-empty-zone", &obj);
	for (element = cfg_list_first(obj);
	     element != NULL;
	     element = cfg_list_next(element))
	{
		obj = cfg_listelt_value(element);
		str = cfg_obj_asstring(obj);
1290
		if (check_name(str) != ISC_R_SUCCESS) {
1291
1292
1293
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "disable-empty-zone: invalid name '%s'",
				    str);
1294
1295
			if (result == ISC_R_SUCCESS)
				result = ISC_R_FAILURE;
1296
1297
1298
		}
	}

1299
1300
1301
1302
1303
1304
1305
	/*
	 * Check that server-id is not too long.
	 * 1024 bytes should be big enough.
	 */
	obj = NULL;
	(void)cfg_map_get(options, "server-id", &obj);
	if (obj != NULL && cfg_obj_isstring(obj) &&
Mark Andrews's avatar
Mark Andrews committed
1306
	    strlen(cfg_obj_asstring(obj)) > 1024U) {
1307
1308
		cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
			    "'server-id' too big (>1024 bytes)");
1309
1310
		if (result == ISC_R_SUCCESS)
			result = ISC_R_FAILURE;
1311
1312
	}

Evan Hunt's avatar
Evan Hunt committed
1313
	tresult = check_dscp(options, logctx);
1314
	if (result == ISC_R_SUCCESS && tresult != ISC_R_SUCCESS)
Evan Hunt's avatar
Evan Hunt committed
1315
1316
		result = tresult;

Evan Hunt's avatar
Evan Hunt committed
1317
1318
1319
1320
	obj = NULL;
	(void)cfg_map_get(options, "nta-lifetime", &obj);
	if (obj != NULL) {
		lifetime = cfg_obj_asuint32(obj);
1321
		if (lifetime > 604800) {	/* 7 days */
Evan Hunt's avatar
Evan Hunt committed
1322
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
1323
				    "'nta-lifetime' cannot exceed one week");
1324
1325
			if (result == ISC_R_SUCCESS)
				result = ISC_R_RANGE;
Evan Hunt's avatar
Evan Hunt committed
1326
1327
1328
		} else if (lifetime == 0) {
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "'nta-lifetime' may not be zero");
1329
1330
			if (result == ISC_R_SUCCESS)
				result = ISC_R_RANGE;
Evan Hunt's avatar
Evan Hunt committed
1331
1332
1333
1334
1335
1336
		}
	}

	obj = NULL;
	(void)cfg_map_get(options, "nta-recheck", &obj);
	if (obj != NULL) {
1337
		uint32_t recheck = cfg_obj_asuint32(obj);
1338
		if (recheck > 604800) {		/* 7 days */
Evan Hunt's avatar
Evan Hunt committed
1339
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
1340
				    "'nta-recheck' cannot exceed one week");
1341
1342
			if (result == ISC_R_SUCCESS)
				result = ISC_R_RANGE;
Evan Hunt's avatar
Evan Hunt committed
1343
1344
1345
1346
1347
1348
1349
1350
1351
		}

		if (recheck > lifetime)
			cfg_obj_log(obj, logctx, ISC_LOG_WARNING,
				    "'nta-recheck' (%d seconds) is "
				    "greater than 'nta-lifetime' "
				    "(%d seconds)", recheck, lifetime);
	}

Tinderbox User's avatar
Tinderbox User committed
1352
	obj = NULL;
1353
	(void) cfg_map_get(options, "cookie-algorithm", &obj);
Ondřej Surý's avatar
Ondřej Surý committed
1354
	if (obj != NULL) {
1355
		ccalg = cfg_obj_asstring(obj);
1356
	}
1357
1358
1359

	obj = NULL;
	(void) cfg_map_get(options, "cookie-secret", &obj);
Tinderbox User's avatar
Tinderbox User committed
1360
	if (obj != NULL) {
1361
1362
		unsigned char secret[32];

1363
1364
1365
1366
		for (element = cfg_list_first(obj);
		     element != NULL;
		     element = cfg_list_next(element)) {
			unsigned int usedlength;
1367

1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
			obj = cfg_listelt_value(element);
			str = cfg_obj_asstring(obj);

			memset(secret, 0, sizeof(secret));
			isc_buffer_init(&b, secret, sizeof(secret));
			tresult = isc_hex_decodestring(str, &b);
			if (tresult == ISC_R_NOSPACE) {
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "cookie-secret: too long");
			} else if (tresult != ISC_R_SUCCESS) {
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "cookie-secret: invalid hex "
					    "string");
			}
			if (tresult != ISC_R_SUCCESS) {
				if (result == ISC_R_SUCCESS)
					result = tresult;
				continue;
			}

			usedlength = isc_buffer_usedlength(&b);
			if (strcasecmp(ccalg, "aes") == 0 &&
			    usedlength != ISC_AES128_KEYLENGTH) {
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "AES cookie-secret must be "
					    "128 bits");
				if (result == ISC_R_SUCCESS)
					result = ISC_R_RANGE;
			}
			if (strcasecmp(ccalg, "sha1") == 0 &&
			    usedlength != ISC_SHA1_DIGESTLENGTH) {
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "SHA1 cookie-secret must be "
					    "160 bits");
				if (result == ISC_R_SUCCESS)
					result = ISC_R_RANGE;
			}
			if (strcasecmp(ccalg, "sha256") == 0 &&
			    usedlength != ISC_SHA256_DIGESTLENGTH) {
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "SHA256 cookie-secret must be "
					    "256 bits");
				if (result == ISC_R_SUCCESS)
					result = ISC_R_RANGE;
			}
1413
		}
Tinderbox User's avatar
Tinderbox User committed
1414
	}
1415

1416
#ifdef HAVE_DNSTAP
1417
	for (i = 0; i < sizeof(fstrm) / sizeof(fstrm[0]); i++) {
1418
		uint32_t value;
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436

		obj = NULL;
		(void) cfg_map_get(options, fstrm[i].name, &obj);
		if (obj == NULL)
			continue;

		value = cfg_obj_asuint32(obj);
		if (value < fstrm[i].min ||
		    (fstrm[i].max != 0U && value > fstrm[i].max)) {
			if (fstrm[i].max != 0U)
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "%s '%u' out of range (%u..%u)",
					    fstrm[i].name, value,
					    fstrm[i].min, fstrm[i].max);
			else
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "%s out of range (%u < %u)",
					    fstrm[i].name, value, fstrm[i].min);
1437
1438
			if (result == ISC_R_SUCCESS)
				result = ISC_R_RANGE;
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
		}

		if (strcmp(fstrm[i].name, "fstrm-set-input-queue-size") == 0) {
			int bits = 0;
			do {
				bits += value & 0x1;
				value >>= 1;
			} while (value != 0U);
			if (bits != 1) {
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "%s '%u' not a power-of-2",
					    fstrm[i].name,
					    cfg_obj_asuint32(obj));
1452
1453
				if (result == ISC_R_SUCCESS)
					result = ISC_R_RANGE;
1454
1455
1456
			}
		}
	}
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468

	/* Check that dnstap-ouput values are consistent */
	obj = NULL;
	(void) cfg_map_get(options, "dnstap-output", &obj);
	if (obj != NULL) {
		const cfg_obj_t *obj2;
		dns_dtmode_t dmode;

		obj2 = cfg_tuple_get(obj, "mode");
		if (obj2 == NULL) {
			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
				    "dnstap-output mode not found");
1469
1470
1471
1472
1473
1474
1475
			if (result == ISC_R_SUCCESS)
				result = ISC_R_FAILURE;
		} else {
			if (strcasecmp(cfg_obj_asstring(obj2), "file") == 0)
				dmode = dns_dtmode_file;
			else
				dmode = dns_dtmode_unix;
1476

1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
			obj2 = cfg_tuple_get(obj, "size");
			if (obj2 != NULL && !cfg_obj_isvoid(obj2) &&
			    dmode == dns_dtmode_unix)
			{
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "dnstap-output size "
					    "cannot be set with mode unix");
				if (result == ISC_R_SUCCESS)
					result = ISC_R_FAILURE;
			}
1487

1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
			obj2 = cfg_tuple_get(obj, "versions");
			if (obj2 != NULL && !cfg_obj_isvoid(obj2) &&
			    dmode == dns_dtmode_unix)
			{
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "dnstap-output versions "
					    "cannot be set with mode unix");
				if (result == ISC_R_SUCCESS)
					result = ISC_R_FAILURE;
			}
1498

1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
			obj2 = cfg_tuple_get(obj, "suffix");
			if (obj2 != NULL && !cfg_obj_isvoid(obj2) &&
			    dmode == dns_dtmode_unix)
			{
				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
					    "dnstap-output suffix "
					    "cannot be set with mode unix");
				if (result == ISC_R_SUCCESS)
					result = ISC_R_FAILURE;
			}
1509
		}
1510
	}
1511
1512
#endif

1513
1514
1515
	obj = NULL;
	(void)cfg_map_get(options, "lmdb-mapsize", &obj);
	if (obj != NULL) {
1516
		uint64_t mapsize = cfg_obj_asuint64(obj);
1517
1518
1519
1520
1521

		if (mapsize < (1ULL << 20)) { /* 1 megabyte */
			cfg_obj_log(obj, logctx,
				    ISC_LOG_ERROR,
				    "'lmdb-mapsize "
1522
				    "%" PRId64 "' "
1523
1524
				    "is too small",
				    mapsize);
1525
1526
			if (result == ISC_R_SUCCESS)
				result = ISC_R_RANGE;
1527
1528
1529
1530
		} else if (mapsize > (1ULL << 40)) { /* 1 terabyte */
			cfg_obj_log(obj, logctx,
				    ISC_LOG_ERROR,
				    "'lmdb-mapsize "
1531
				    "%" PRId64 "' "
1532
1533
				    "is too large",
				    mapsize);
1534
1535
			if (result == ISC_R_SUCCESS)
				result = ISC_R_RANGE;
1536
1537
1538
		}
	}

1539
1540
1541
1542
1543
1544
1545
1546
1547
	obj = NULL;
	(void)cfg_map_get(options, "resolver-nonbackoff-tries", &obj);
	if (obj != NULL && cfg_obj_asuint32(obj) == 0U) {
		cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
			    "'resolver-nonbackoff-tries' must be >= 1");
		if (result == ISC_R_SUCCESS)
			result = ISC_R_RANGE;
	}

1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
	obj = NULL;
	(void)cfg_map_get(options, "geoip-use-ecs", &obj);
	if (obj != NULL && cfg_obj_asboolean(obj)) {
		cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
			    "'geoip-use-ecs yes': "
			    "ECS can no longer be used in geoip ACLs");
		if (result == ISC_R_SUCCESS) {
			result = ISC_R_FAILURE;
		}
	}

1559
1560
1561
	return (result);
}

1562
static isc_result_t
1563
get_masters_def(const cfg_obj_t *cctx, const char *name, const cfg_obj_t **ret) {
1564
	isc_result_t result;
1565
1566
	const cfg_obj_t *masters = NULL;
	const cfg_listelt_t *elt;
1567
1568
1569
1570
1571
1572
1573

	result = cfg_map_get(cctx, "masters", &masters);
	if (result != ISC_R_SUCCESS)
		return (result);
	for (elt = cfg_list_first(masters);
	     elt != NULL;
	     elt = cfg_list_next(elt)) {
1574
		const cfg_obj_t *list;
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
		const char *listname;

		list = cfg_listelt_value(elt);
		listname = cfg_obj_asstring(cfg_tuple_get(list, "name"));

		if (strcasecmp(listname, name) == 0) {
			*ret = list;
			return (ISC_R_SUCCESS);
		}
	}
	return (ISC_R_NOTFOUND);
}

static isc_result_t
1589
validate_masters(const cfg_obj_t *obj, const cfg_obj_t *config,
1590
		 uint32_t *countp, isc_log_t *logctx, isc_mem_t *mctx)
1591
1592
1593
{
	isc_result_t result = ISC_R_SUCCESS;
	isc_result_t tresult;
1594
	uint32_t count = 0;
1595
1596
	isc_symtab_t *symtab = NULL;
	isc_symvalue_t symvalue;
1597
1598
	const cfg_listelt_t *element;
	const cfg_listelt_t **stack = NULL;
1599
	uint32_t stackcount = 0, pushed = 0;
1600
	const cfg_obj_t *list;
1601
1602

	REQUIRE(countp != NULL);
1603
	result = isc_symtab_create(mctx, 100, NULL, NULL, false, &symtab);
1604
1605
	if (result != ISC_R_SUCCESS) {
		*countp = count;