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

18
/* $Id: server.c,v 1.493 2007/11/26 02:43:53 marka Exp $ */
19
20

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

22
23
24
25
#include <config.h>

#include <stdlib.h>

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

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

#include <bind9/check.h>
47

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

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

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

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

#define CHECKM(op, msg) \
110
	do { result = (op);					  \
111
112
113
114
115
116
117
118
119
120
121
	       if (result != ISC_R_SUCCESS) {			  \
			isc_log_write(ns_g_lctx,		  \
				      NS_LOGCATEGORY_GENERAL,	  \
				      NS_LOGMODULE_SERVER,	  \
				      ISC_LOG_ERROR,		  \
				      "%s: %s", msg,		  \
				      isc_result_totext(result)); \
			goto cleanup;				  \
		}						  \
	} while (0)						  \

Mark Andrews's avatar
Mark Andrews committed
122
#define CHECKMF(op, msg, file) \
123
	do { result = (op);					  \
Mark Andrews's avatar
Mark Andrews committed
124
125
126
127
128
129
130
131
132
133
134
	       if (result != ISC_R_SUCCESS) {			  \
			isc_log_write(ns_g_lctx,		  \
				      NS_LOGCATEGORY_GENERAL,	  \
				      NS_LOGMODULE_SERVER,	  \
				      ISC_LOG_ERROR,		  \
				      "%s '%s': %s", msg, file,	  \
				      isc_result_totext(result)); \
			goto cleanup;				  \
		}						  \
	} while (0)						  \

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

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

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
struct dumpcontext {
	isc_mem_t			*mctx;
	isc_boolean_t			dumpcache;
	isc_boolean_t			dumpzones;
	FILE				*fp;
	ISC_LIST(struct viewlistentry)	viewlist;
	struct viewlistentry		*view;
	struct zonelistentry		*zone;
	dns_dumpctx_t			*mdctx;
	dns_db_t			*db;
	dns_db_t			*cache;
	isc_task_t			*task;
	dns_dbversion_t			*version;
};

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

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

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * These zones should not leak onto the Internet.
 */
static const struct {
	const char	*zone;
	isc_boolean_t	rfc1918;
} empty_zones[] = {
#ifdef notyet
	/* RFC 1918 */
	{ "10.IN-ADDR.ARPA", ISC_TRUE },
	{ "16.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "17.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "18.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "19.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "20.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "21.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "22.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "23.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "24.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "25.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "26.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "27.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "28.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "29.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "30.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "31.172.IN-ADDR.ARPA", ISC_TRUE },
	{ "168.192.IN-ADDR.ARPA", ISC_TRUE },
#endif

	/* RFC 3330 */
	{ "127.IN-ADDR.ARPA", ISC_FALSE },	/* LOOPBACK */
	{ "254.169.IN-ADDR.ARPA", ISC_FALSE },	/* LINK LOCAL */
	{ "2.0.192.IN-ADDR.ARPA", ISC_FALSE },	/* TEST NET */
	{ "255.255.255.255.IN-ADDR.ARPA", ISC_FALSE },	/* BROADCAST */

	/* Local IPv6 Unicast Addresses */
	{ "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.ARPA", ISC_FALSE },
	{ "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.ARPA", ISC_FALSE },
	/* LOCALLY ASSIGNED LOCAL ADDRES S SCOPE */
	{ "D.F.IP6.ARPA", ISC_FALSE },
	{ "8.E.F.IP6.ARPA", ISC_FALSE },	/* LINK LOCAL */
	{ "9.E.F.IP6.ARPA", ISC_FALSE },	/* LINK LOCAL */
	{ "A.E.F.IP6.ARPA", ISC_FALSE },	/* LINK LOCAL */
	{ "B.E.F.IP6.ARPA", ISC_FALSE },	/* LINK LOCAL */

	{ NULL, ISC_FALSE }
};

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#ifdef HAVE_LIBXML2

void
server_httpd_create(ns_server_t *server);

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

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

void
240
server_generatexml(ns_server_t *server, int *buflen, xmlChar **buf);
241
242
243

#endif /* HAVE_LIBXML2 */

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

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

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

259
static isc_result_t
260
261
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);
262

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

267
static isc_result_t
268
269
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,
270
	       cfg_aclconfctx_t *aclconf);
271

272
273
274
static void
end_reserved_dispatches(ns_server_t *server, isc_boolean_t all);

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

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

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

309
	result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
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
				    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,
		        cfg_aclconfctx_t *actx, isc_mem_t *mctx,
                        dns_acl_t **aclp)
{
	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);

        /*
348
349
350
         * 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
351
352
353
         * into IP tables as is usually done with ACLs.
         */
	result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
354
				    actx, mctx, 3, aclp);
355
356
357
358

	return (result);
}

359
static isc_result_t
360
configure_view_dnsseckey(const cfg_obj_t *vconfig, const cfg_obj_t *key,
361
362
363
			 dns_keytable_t *keytable, isc_mem_t *mctx)
{
	dns_rdataclass_t viewclass;
364
	dns_rdata_dnskey_t keystruct;
365
	isc_uint32_t flags, proto, alg;
366
	const char *keystr, *keynamestr;
367
368
369
370
371
372
373
374
375
376
377
	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;

378
379
380
381
382
383
384
	flags = cfg_obj_asuint32(cfg_tuple_get(key, "flags"));
	proto = cfg_obj_asuint32(cfg_tuple_get(key, "protocol"));
	alg = cfg_obj_asuint32(cfg_tuple_get(key, "algorithm"));
	keyname = dns_fixedname_name(&fkeyname);
	keynamestr = cfg_obj_asstring(cfg_tuple_get(key, "name"));

	if (vconfig == NULL)
385
		viewclass = dns_rdataclass_in;
386
	else {
387
		const cfg_obj_t *classobj = cfg_tuple_get(vconfig, "class");
388
389
		CHECK(ns_config_getclass(classobj, dns_rdataclass_in,
					 &viewclass));
390
	}
391
	keystruct.common.rdclass = viewclass;
392
	keystruct.common.rdtype = dns_rdatatype_dnskey;
393
	/*
394
	 * The key data in keystruct is not dynamically allocated.
395
396
397
398
399
	 */
	keystruct.mctx = NULL;

	ISC_LINK_INIT(&keystruct.common, link);

400
	if (flags > 0xffff)
401
		CHECKM(ISC_R_RANGE, "key flags");
402
	if (proto > 0xff)
403
		CHECKM(ISC_R_RANGE, "key protocol");
404
	if (alg > 0xff)
405
		CHECKM(ISC_R_RANGE, "key algorithm");
406
407
408
	keystruct.flags = (isc_uint16_t)flags;
	keystruct.protocol = (isc_uint8_t)proto;
	keystruct.algorithm = (isc_uint8_t)alg;
409
410
411
412

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

413
	keystr = cfg_obj_asstring(cfg_tuple_get(key, "key"));
414
	CHECK(isc_base64_decodestring(keystr, &keydatabuf));
415
416
417
418
	isc_buffer_usedregion(&keydatabuf, &r);
	keystruct.datalen = r.length;
	keystruct.data = r.base;

419
420
421
422
423
424
425
	if ((keystruct.algorithm == DST_ALG_RSASHA1 ||
	     keystruct.algorithm == DST_ALG_RSAMD5) &&
	    r.length > 1 && r.base[0] == 1 && r.base[1] == 3)
		cfg_obj_log(key, ns_g_lctx, ISC_LOG_WARNING,
			    "trusted key '%s' has a weak exponent",
			    keynamestr);

426
427
428
429
430
	CHECK(dns_rdata_fromstruct(NULL,
				   keystruct.common.rdclass,
				   keystruct.common.rdtype,
				   &keystruct, &rrdatabuf));
	dns_fixedname_init(&fkeyname);
431
432
	isc_buffer_init(&namebuf, keynamestr, strlen(keynamestr));
	isc_buffer_add(&namebuf, strlen(keynamestr));
433
434
435
436
437
438
439
440
441
442
443
	CHECK(dns_name_fromtext(keyname, &namebuf,
				dns_rootname, ISC_FALSE,
				NULL));
	CHECK(dst_key_fromdns(keyname, viewclass, &rrdatabuf,
			      mctx, &dstkey));

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

 cleanup:
444
445
446
447
448
449
450
451
452
453
454
	if (result == DST_R_NOCRYPTO) {
		cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
			    "ignoring trusted key for '%s': no crypto support",
			    keynamestr);
		result = ISC_R_SUCCESS;
	} else {
		cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
			    "configuring trusted key for '%s': %s",
			    keynamestr, isc_result_totext(result));
		result = ISC_R_FAILURE;
	}
455
456
457
458
459
460
461

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

	return (result);
}

462
/*%
463
464
 * Configure DNSSEC keys for a view.  Currently used only for
 * the security roots.
465
 *
466
 * The per-view configuration values and the server-global defaults are read
467
 * from 'vconfig' and 'config'.	 The variable to be configured is '*target'.
468
469
 */
static isc_result_t
470
configure_view_dnsseckeys(const cfg_obj_t *vconfig, const cfg_obj_t *config,
471
			  isc_mem_t *mctx, dns_keytable_t **target)
472
473
{
	isc_result_t result;
474
475
476
477
478
	const cfg_obj_t *keys = NULL;
	const cfg_obj_t *voptions = NULL;
	const cfg_listelt_t *element, *element2;
	const cfg_obj_t *keylist;
	const cfg_obj_t *key;
479
	dns_keytable_t *keytable = NULL;
480

481
482
	CHECK(dns_keytable_create(mctx, &keytable));

483
484
	if (vconfig != NULL)
		voptions = cfg_tuple_get(vconfig, "options");
485

486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
	keys = NULL;
	if (voptions != NULL)
		(void)cfg_map_get(voptions, "trusted-keys", &keys);
	if (keys == NULL)
		(void)cfg_map_get(config, "trusted-keys", &keys);

	for (element = cfg_list_first(keys);
	     element != NULL;
	     element = cfg_list_next(element))
	{
		keylist = cfg_listelt_value(element);
		for (element2 = cfg_list_first(keylist);
		     element2 != NULL;
		     element2 = cfg_list_next(element2))
		{
			key = cfg_listelt_value(element2);
			CHECK(configure_view_dnsseckey(vconfig, key,
503
						       keytable, mctx));
504
		}
505
	}
506

507
508
509
510
	dns_keytable_detach(target);
	*target = keytable; /* Transfer ownership. */
	keytable = NULL;
	result = ISC_R_SUCCESS;
511
	
512
513
514
 cleanup:
	return (result);
}
515

516
static isc_result_t
517
mustbesecure(const cfg_obj_t *mbs, dns_resolver_t *resolver)
518
{
519
520
	const cfg_listelt_t *element;
	const cfg_obj_t *obj;
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
	const char *str;
	dns_fixedname_t fixed;
	dns_name_t *name;
	isc_boolean_t value;
	isc_result_t result;
	isc_buffer_t b;
	
	dns_fixedname_init(&fixed);
	name = dns_fixedname_name(&fixed);
	for (element = cfg_list_first(mbs);
	     element != NULL;
	     element = cfg_list_next(element))
	{
		obj = cfg_listelt_value(element);
		str = cfg_obj_asstring(cfg_tuple_get(obj, "name"));
		isc_buffer_init(&b, str, strlen(str));
		isc_buffer_add(&b, strlen(str));
		CHECK(dns_name_fromtext(name, &b, dns_rootname,
539
					ISC_FALSE, NULL));
540
541
542
543
544
545
546
547
548
		value = cfg_obj_asboolean(cfg_tuple_get(obj, "value"));
		CHECK(dns_resolver_setmustbesecure(resolver, name, value));
	}

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

550
/*%
551
552
553
 * Get a dispatch appropriate for the resolver of a given view.
 */
static isc_result_t
554
get_view_querysource_dispatch(const cfg_obj_t **maps,
555
556
557
558
559
560
			      int af, dns_dispatch_t **dispatchp)
{
	isc_result_t result;
	dns_dispatch_t *disp;
	isc_sockaddr_t sa;
	unsigned int attrs, attrmask;
561
	const cfg_obj_t *obj = NULL;
562
563
564
565
566
567
568
569

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

	switch (af) {
	case AF_INET:
570
571
		result = ns_config_get(maps, "query-source", &obj);
		INSIST(result == ISC_R_SUCCESS);
572
573
		break;
	case AF_INET6:
574
575
		result = ns_config_get(maps, "query-source-v6", &obj);
		INSIST(result == ISC_R_SUCCESS);
576
577
578
579
		break;
	default:
		INSIST(0);
	}
580

581
	sa = *(cfg_obj_assockaddr(obj));
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
	INSIST(isc_sockaddr_pf(&sa) == af);

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

	/*
	 * Try to find a dispatcher that we can share.
	 */
	attrs = 0;
	attrs |= DNS_DISPATCHATTR_UDP;
	switch (af) {
	case AF_INET:
		attrs |= DNS_DISPATCHATTR_IPV4;
		break;
	case AF_INET6:
		attrs |= DNS_DISPATCHATTR_IPV6;
		break;
	}
	attrmask = 0;
	attrmask |= DNS_DISPATCHATTR_UDP;
	attrmask |= DNS_DISPATCHATTR_TCP;
	attrmask |= DNS_DISPATCHATTR_IPV4;
	attrmask |= DNS_DISPATCHATTR_IPV6;

	disp = NULL;
	result = dns_dispatch_getudp(ns_g_dispatchmgr, ns_g_socketmgr,
				     ns_g_taskmgr, &sa, 4096,
				     1000, 32768, 16411, 16433,
				     attrs, attrmask, &disp);
624
	if (result != ISC_R_SUCCESS) {
625
626
627
628
629
630
631
632
633
634
635
636
637
638
		isc_sockaddr_t any;
		char buf[ISC_SOCKADDR_FORMATSIZE];

		switch (af) {
		case AF_INET:
			isc_sockaddr_any(&any);
			break;
		case AF_INET6:
			isc_sockaddr_any6(&any);
			break;
		}
		if (isc_sockaddr_equal(&sa, &any))
			return (ISC_R_SUCCESS);
		isc_sockaddr_format(&sa, buf, sizeof(buf));
639
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
640
			      NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
641
642
			      "could not get query source dispatcher (%s)",
			      buf);
643
		return (result);
644
	}
645
646
647
648
649
650

	*dispatchp = disp;

	return (ISC_R_SUCCESS);
}

651
static isc_result_t
652
configure_order(dns_order_t *order, const cfg_obj_t *ent) {
653
654
	dns_rdataclass_t rdclass;
	dns_rdatatype_t rdtype;
655
	const cfg_obj_t *obj;
656
657
658
659
660
	dns_fixedname_t fixed;
	unsigned int mode = 0;
	const char *str;
	isc_buffer_t b;
	isc_result_t result;
661
	isc_boolean_t addroot;
662
663
664
665
666
667
668
669
670
671
672
673

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

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

	obj = cfg_tuple_get(ent, "name");
674
	if (cfg_obj_isstring(obj))
675
676
677
		str = cfg_obj_asstring(obj);
	else
		str = "*";
678
	addroot = ISC_TF(strcmp(str, "*") == 0);
679
680
681
682
	isc_buffer_init(&b, str, strlen(str));
	isc_buffer_add(&b, strlen(str));
	dns_fixedname_init(&fixed);
	result = dns_name_fromtext(dns_fixedname_name(&fixed), &b,
683
				   dns_rootname, ISC_FALSE, NULL);
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
	if (result != ISC_R_SUCCESS)
		return (result);

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

699
700
701
	/*
	 * "*" should match everything including the root (BIND 8 compat).
	 * As dns_name_matcheswildcard(".", "*.") returns FALSE add a
Mark Andrews's avatar
Mark Andrews committed
702
	 * explicit entry for "." when the name is "*".
703
704
705
706
707
708
709
710
	 */
	if (addroot) {
		result = dns_order_add(order, dns_rootname,
				       rdtype, rdclass, mode);
		if (result != ISC_R_SUCCESS)
			return (result);
	}

711
712
713
714
	return (dns_order_add(order, dns_fixedname_name(&fixed),
			      rdtype, rdclass, mode));
}

715
static isc_result_t
716
configure_peer(const cfg_obj_t *cpeer, isc_mem_t *mctx, dns_peer_t **peerp) {
717
718
	isc_netaddr_t na;
	dns_peer_t *peer;
719
	const cfg_obj_t *obj;
720
	const char *str;
721
	isc_result_t result;
722
	unsigned int prefixlen;
723

724
	cfg_obj_asnetprefix(cfg_map_getname(cpeer), &na, &prefixlen);
725
726

	peer = NULL;
727
	result = dns_peer_newprefix(mctx, &na, prefixlen, &peer);
728
729
730
731
732
733
	if (result != ISC_R_SUCCESS)
		return (result);

	obj = NULL;
	(void)cfg_map_get(cpeer, "bogus", &obj);
	if (obj != NULL)
734
		CHECK(dns_peer_setbogus(peer, cfg_obj_asboolean(obj)));
735
736
737
738

	obj = NULL;
	(void)cfg_map_get(cpeer, "provide-ixfr", &obj);
	if (obj != NULL)
739
		CHECK(dns_peer_setprovideixfr(peer, cfg_obj_asboolean(obj)));
740
741
742
743

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

746
747
748
	obj = NULL;
	(void)cfg_map_get(cpeer, "edns", &obj);
	if (obj != NULL)
749
		CHECK(dns_peer_setsupportedns(peer, cfg_obj_asboolean(obj)));
750

751
752
753
754
755
756
757
758
759
760
761
	obj = NULL;
	(void)cfg_map_get(cpeer, "edns-udp-size", &obj);
	if (obj != NULL) {
		isc_uint32_t udpsize = cfg_obj_asuint32(obj);
		if (udpsize < 512)
			udpsize = 512;
		if (udpsize > 4096)
			udpsize = 4096;
		CHECK(dns_peer_setudpsize(peer, (isc_uint16_t)udpsize));
	}

762
763
764
765
766
767
768
769
770
771
772
	obj = NULL;
	(void)cfg_map_get(cpeer, "max-udp-size", &obj);
	if (obj != NULL) {
		isc_uint32_t udpsize = cfg_obj_asuint32(obj);
		if (udpsize < 512)
			udpsize = 512;
		if (udpsize > 4096)
			udpsize = 4096;
		CHECK(dns_peer_setmaxudp(peer, (isc_uint16_t)udpsize));
	}

773
774
775
	obj = NULL;
	(void)cfg_map_get(cpeer, "transfers", &obj);
	if (obj != NULL)
776
		CHECK(dns_peer_settransfers(peer, cfg_obj_asuint32(obj)));
777
778
779
780
781
782

	obj = NULL;
	(void)cfg_map_get(cpeer, "transfer-format", &obj);
	if (obj != NULL) {
		str = cfg_obj_asstring(obj);
		if (strcasecmp(str, "many-answers") == 0)
783
784
			CHECK(dns_peer_settransferformat(peer,
							 dns_many_answers));
785
		else if (strcasecmp(str, "one-answer") == 0)
786
787
			CHECK(dns_peer_settransferformat(peer,
							 dns_one_answer));
788
789
790
791
792
793
794
795
796
797
798
		else
			INSIST(0);
	}

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

	obj = NULL;
801
	if (na.family == AF_INET)
802
803
804
805
806
807
808
809
		(void)cfg_map_get(cpeer, "transfer-source", &obj);
	else
		(void)cfg_map_get(cpeer, "transfer-source-v6", &obj);
	if (obj != NULL) {
		result = dns_peer_settransfersource(peer,
						    cfg_obj_assockaddr(obj));
		if (result != ISC_R_SUCCESS)
			goto cleanup;
810
		ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
811
	}
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838

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

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

839
840
841
842
843
844
845
846
	*peerp = peer;
	return (ISC_R_SUCCESS);

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

847
static isc_result_t
848
disable_algorithms(const cfg_obj_t *disabled, dns_resolver_t *resolver) {
849
	isc_result_t result;
850
851
	const cfg_obj_t *algorithms;
	const cfg_listelt_t *element;
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
	const char *str;
	dns_fixedname_t fixed;
	dns_name_t *name;
	isc_buffer_t b;

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

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

872
		DE_CONST(cfg_obj_asstring(cfg_listelt_value(element)), r.base);
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
		r.length = strlen(r.base);

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

893
static isc_boolean_t
894
895
on_disable_list(const cfg_obj_t *disablelist, dns_name_t *zonename) {
	const cfg_listelt_t *element;
896
897
898
	dns_fixedname_t fixed;
	dns_name_t *name;
	isc_result_t result;
899
	const cfg_obj_t *value;
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
	const char *str;
	isc_buffer_t b;

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

923
924
925
926
927
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
static void
check_dbtype(dns_zone_t **zonep, unsigned int dbtypec, const char **dbargv,
	     isc_mem_t *mctx)
{
	char **argv = NULL;
	unsigned int i;
	isc_result_t result;

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

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

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


955
/*
956
957
 * Configure 'view' according to 'vconfig', taking defaults from 'config'
 * where values are missing in 'vconfig'.
958
 *
959
960
 * When configuring the default view, 'vconfig' will be NULL and the
 * global defaults in 'config' used exclusively.
961
 */
962
static isc_result_t
963
964
965
configure_view(dns_view_t *view, const cfg_obj_t *config,
	       const cfg_obj_t *vconfig, isc_mem_t *mctx,
	       cfg_aclconfctx_t *actx, isc_boolean_t need_hints)
966
{
967
968
969
970
971
972
973
974
	const cfg_obj_t *maps[4];
	const cfg_obj_t *cfgmaps[3];
	const cfg_obj_t *options = NULL;
	const cfg_obj_t *voptions = NULL;
	const cfg_obj_t *forwardtype;
	const cfg_obj_t *forwarders;
	const cfg_obj_t *alternates;
	const cfg_obj_t *zonelist;
975
#ifdef DLZ
976
977
978
	const cfg_obj_t *dlz;
	unsigned int dlzargc;
	char **dlzargv;
979
#endif
980
981
982
	const cfg_obj_t *disabled;
	const cfg_obj_t *obj;
	const cfg_listelt_t *element;
983
	in_port_t port;
984
	dns_cache_t *cache = NULL;
985
	isc_result_t result;
986
	isc_uint32_t max_adb_size;
987
	isc_uint32_t max_cache_size;
988
	isc_uint32_t max_acache_size;
Mark Andrews's avatar
Mark Andrews committed
989
	isc_uint32_t lame_ttl;
Brian Wellington's avatar
Brian Wellington committed
990
	dns_tsig_keyring_t *ring;
991
	dns_view_t *pview = NULL;	/* Production view */
992
	isc_mem_t *cmctx;
993
994
	dns_dispatch_t *dispatch4 = NULL;
	dns_dispatch_t *dispatch6 = NULL;
995
	isc_boolean_t reused_cache = ISC_FALSE;
996
	int i;
997
	const char *str;
998
	dns_order_t *order = NULL;
999
	isc_uint32_t udpsize;
1000
	unsigned int resopts = 0;
1001
	dns_zone_t *zone = NULL;
1002
	isc_uint32_t max_clients_per_query;
1003
1004
1005
1006
1007
	const char *sep = ": view ";
	const char *viewname = view->name;
	const char *forview = " for view ";
	isc_boolean_t rfc1918;
	isc_boolean_t empty_zones_enable;
1008
	const cfg_obj_t *disablelist = NULL;
1009
	isc_uint32_t nqports, qports_updateinterval;
1010

1011
	REQUIRE(DNS_VIEW_VALID(view));
1012

1013
	cmctx = NULL;
1014

1015
	if (config != NULL)
1016
		(void)cfg_map_get(config, "options", &options);
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034

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

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

1035
1036
1037
1038
1039
1040
	if (!strcmp(viewname, "_default")) {
		sep = "";
		viewname = "";
		forview = "";
	}

1041
1042
1043
	/*
	 * Set the view's port number for outgoing queries.
	 */
1044
	CHECKM(ns_config_getport(config, &port), "port");
1045
	dns_view_setdstport(view, port);
1046

1047
1048
	/*
	 * Create additional cache for this view and zones under the view
1049
1050
	 * if explicitly enabled.
	 * XXX950 default to on.
1051
1052
	 */
	obj = NULL;
1053
1054
	(void)ns_config_get(maps, "acache-enable", &obj);
	if (obj != NULL && cfg_obj_asboolean(obj)) {
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
		cmctx = NULL;
		CHECK(isc_mem_create(0, 0, &cmctx));
		CHECK(dns_acache_create(&view->acache, cmctx, ns_g_taskmgr,
					ns_g_timermgr));
		isc_mem_detach(&cmctx);
	}
	if (view->acache != NULL) {
		obj = NULL;
		result = ns_config_get(maps, "acache-cleaning-interval", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_acache_setcleaninginterval(view->acache,
					       cfg_obj_asuint32(obj) * 60);

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

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

1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
	/*
	 * Configure the zones.
	 */
	zonelist = NULL;
	if (voptions != NULL)
		(void)cfg_map_get(voptions, "zone", &zonelist);
	else
		(void)cfg_map_get(config, "zone", &zonelist);
	for (element = cfg_list_first(zonelist);
	     element != NULL;
	     element = cfg_list_next(element))
	{
1105
		const cfg_obj_t *zconfig = cfg_listelt_value(element);
1106
1107
1108
1109
		CHECK(configure_zone(config, zconfig, vconfig, mctx, view,
				     actx));
	}

1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
#ifdef DLZ
	/*
	 * Create Dynamically Loadable Zone driver.
	 */
	dlz = NULL;
	if (voptions != NULL)
		(void)cfg_map_get(voptions, "dlz", &dlz);
	else
		(void)cfg_map_get(config, "dlz", &dlz);

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

			obj = cfg_tuple_get(dlz, "name");
			result = dns_dlzcreate(mctx, cfg_obj_asstring(obj),
					       dlzargv[0], dlzargc, dlzargv,
					       &view->dlzdatabase);
			isc_mem_free(mctx, s);
			isc_mem_put(mctx, dlzargv, dlzargc * sizeof(*dlzargv));
1143
			if (result != ISC_R_SUCCESS)
1144
1145
1146
1147
1148
				goto cleanup;
		}
	}
#endif

1149
	/*
1150
1151
1152
	 * Configure the view's cache.  Try to reuse an existing
	 * cache if possible, otherwise create a new cache.
	 * Note that the ADB is not preserved in either case.
1153
1154
	 *
	 * XXX Determining when it is safe to reuse a cache is
1155
1156
1157
1158
1159
1160
	 * tricky.  When the view's configuration changes, the cached
	 * data may become invalid because it reflects our old
	 * view of the world.  As more view attributes become
	 * configurable, we will have to add code here to check
	 * whether they have changed in ways that could
	 * invalidate the cache.
1161
	 */
1162
1163
1164
1165
1166
1167
1168
1169
	result = dns_viewlist_find(&ns_g_server->viewlist,
				   view->name, view->rdclass,
				   &pview);
	if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
		goto cleanup;
	if (pview != NULL) {
		INSIST(pview->cache != NULL);
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1170
1171
			      NS_LOGMODULE_SERVER, ISC_LOG_DEBUG(3),
			      "reusing existing cache");
1172
		reused_cache = ISC_TRUE;
1173
1174
1175
		dns_cache_attach(pview->cache, &cache);
		dns_view_detach(&pview);
	} else {
1176
1177
		CHECK(isc_mem_create(0, 0, &cmctx));
		CHECK(dns_cache_create(cmctx, ns_g_taskmgr, ns_g_timermgr,
1178
1179
				       view->rdclass, "rbt", 0, NULL, &cache));
	}
1180
	dns_view_setcache(view, cache);
1181

1182
1183
1184
1185
1186
1187
	/*
	 * cache-file cannot be inherited if views are present, but this
	 * should be caught by the configuration checking stage.
	 */
	obj = NULL;
	result = ns_config_get(maps, "cache-file", &obj);
1188
	if (result == ISC_R_SUCCESS && strcmp(view->name, "_bind") != 0) {
1189
		CHECK(dns_cache_setfilename(cache, cfg_obj_asstring(obj)));
1190
1191
1192
1193
		if (!reused_cache)
			CHECK(dns_cache_load(cache));
	}

1194
1195
1196
	obj = NULL;
	result = ns_config_get(maps, "cleaning-interval", &obj);
	INSIST(result == ISC_R_SUCCESS);
1197
	dns_cache_setcleaninginterval(cache, cfg_obj_asuint32(obj) * 60);
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218

	obj = NULL;
	result = ns_config_get(maps, "max-cache-size", &obj);
	INSIST(result == ISC_R_SUCCESS);
	if (cfg_obj_isstring(obj)) {
		str = cfg_obj_asstring(obj);
		INSIST(strcasecmp(str, "unlimited") == 0);
		max_cache_size = ISC_UINT32_MAX;
	} else {
		isc_resourcevalue_t value;
		value = cfg_obj_asuint64(obj);
		if (value > ISC_UINT32_MAX) {
			cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
				    "'max-cache-size "
				    "%" ISC_PRINT_QUADFORMAT "d' is too large",
				    value);
			result = ISC_R_RANGE;
			goto cleanup;
		}
		max_cache_size = (isc_uint32_t)value;
	}
1219
1220
	dns_cache_setcachesize(cache, max_cache_size);

1221
	dns_cache_detach(&cache);
1222

1223
1224
1225
1226
	/*
	 * Check-names.
	 */
	obj = NULL;
1227
	result = ns_checknames_get(maps, "response", &obj);
1228
1229
	INSIST(result == ISC_R_SUCCESS);

1230
1231
	str = cfg_obj_asstring(obj);
	if (strcasecmp(str, "fail") == 0) {
1232
		resopts |= DNS_RESOLVER_CHECKNAMES |
1233
1234
			DNS_RESOLVER_CHECKNAMESFAIL;
		view->checknames = ISC_TRUE;
1235
	} else if (strcasecmp(str, "warn") == 0) {
1236
		resopts |= DNS_RESOLVER_CHECKNAMES;
1237
		view->checknames = ISC_FALSE;
1238
	} else if (strcasecmp(str, "ignore") == 0) {
1239
1240
		view->checknames = ISC_FALSE;
	} else
1241
		INSIST(0);
1242

1243
1244
1245
	/*
	 * Resolver.
	 *
Bob Halley's avatar
Bob Halley committed
1246
	 * XXXRTH  Hardwired number of tasks.
1247
	 */
1248
1249
	CHECK(get_view_querysource_dispatch(maps, AF_INET, &dispatch4));
	CHECK(get_view_querysource_dispatch(maps, AF_INET6, &dispatch6));
1250
1251
	if (dispatch4 == NULL && dispatch6 == NULL) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
Mark Andrews's avatar
Mark Andrews committed
1252
1253
				 "unable to obtain neither an IPv4 nor"
				 " an IPv6 dispatch");
1254
1255
1256
		result = ISC_R_UNEXPECTED;
		goto cleanup;
	}
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300

	obj = NULL;
	(void)ns_config_get(maps, "use-queryport-pool", &obj);
	if (obj == NULL || cfg_obj_asboolean(obj)) {
		isc_sockaddr_t sa;
		isc_boolean_t logit4 = ISC_FALSE, logit6 = ISC_FALSE;

		resopts |= (DNS_RESOLVER_USEDISPATCHPOOL4 |
			    DNS_RESOLVER_USEDISPATCHPOOL6);

		/* Check consistency with query-source(-v6) */
		if (dispatch4 == NULL)
			resopts &= ~DNS_RESOLVER_USEDISPATCHPOOL4;
		else {
			result = dns_dispatch_getlocaladdress(dispatch4, &sa);
			INSIST(result == ISC_R_SUCCESS);
			if (isc_sockaddr_getport(&sa) != 0) {
				logit4 = ISC_TRUE;
				resopts &= ~DNS_RESOLVER_USEDISPATCHPOOL4;
			}
		}

		if (dispatch6 == NULL)
			resopts &= ~DNS_RESOLVER_USEDISPATCHPOOL6;
		else {
			result = dns_dispatch_getlocaladdress(dispatch6, &sa);
			INSIST(result == ISC_R_SUCCESS);
			if (isc_sockaddr_getport(&sa) != 0) {
				logit6 = ISC_TRUE;
				resopts &= ~DNS_RESOLVER_USEDISPATCHPOOL6;
			}
		}
		if (logit4 && obj != NULL)
			cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
				    "specific query-source port "
				    "cannot coexist with queryport-pool. "
				    "(Pool disabled)");
		if (logit6 && obj != NULL)
			cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
				    "specific query-source-v6 port "
				    "cannot coexist with queryport-pool. "
				    "(Pool disabled)");
	}

1301
1302
	CHECK(dns_view_createresolver(view, ns_g_taskmgr, 31,
				      ns_g_socketmgr, ns_g_timermgr,
1303
				      resopts, ns_g_dispatchmgr,
1304
				      dispatch4, dispatch6));
1305

1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
	/*
	 * Query-port pool parameters.
	 */
	obj = NULL;
	nqports = 8;
	result = ns_config_get(maps, "queryport-pool-ports", &obj);
	if (result == ISC_R_SUCCESS) {
		if ((resopts & (DNS_RESOLVER_USEDISPATCHPOOL4 |
				DNS_RESOLVER_USEDISPATCHPOOL6)) == 0) {
			cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
				    "queryport-pool-ports is effective only "
				    "with 'use-queryport-pool yes' (ignored)");
		} else
			nqports = cfg_obj_asuint32(obj);
	}

	obj = NULL;
	qports_updateinterval = 15;
	result = ns_config_get(maps, "queryport-pool-updateinterval", &obj);
	if (result == ISC_R_SUCCESS) {
		if ((resopts & (DNS_RESOLVER_USEDISPATCHPOOL4 |
				DNS_RESOLVER_USEDISPATCHPOOL6)) == 0) {
			cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
				    "queryport-pool-updateinterval is "
				    "effective only with 'use-queryport-pool "
				    "yes' (ignored)");
		} else
			qports_updateinterval = cfg_obj_asuint32(obj);
	}

	if ((resopts & (DNS_RESOLVER_USEDISPATCHPOOL4 |
			DNS_RESOLVER_USEDISPATCHPOOL6)) != 0) {
		CHECK(dns_resolver_createdispatchpool(view->resolver,
						      nqports,
						      qports_updateinterval
						      * 60));
	}

1344
	/*
Mark Andrews's avatar
Mark Andrews committed
1345
	 * Set the ADB cache size to 1/8th of the max-cache-size.
1346
	 */
Mark Andrews's avatar
Mark Andrews committed
1347
1348
	max_adb_size = 0;
	if (max_cache_size != 0) {
Andreas Gustafsson's avatar
spacing    
Andreas Gustafsson committed
1349
		max_adb_size = max_cache_size / 8;
Mark Andrews's avatar
Mark Andrews committed
1350
1351
		if (max_adb_size == 0)
			max_adb_size = 1;	/* Force minimum. */
1352
1353
1354
	}
	dns_adb_setadbsize(view->adb, max_adb_size);

Mark Andrews's avatar
Mark Andrews committed
1355
1356
1357
	/*
	 * Set resolver's lame-ttl.
	 */
1358
1359
1360
1361
	obj = NULL;
	result = ns_config_get(maps, "lame-ttl", &obj);
	INSIST(result == ISC_R_SUCCESS);
	lame_ttl = cfg_obj_asuint32(obj);
1362
1363
	if (lame_ttl > 1800)
		lame_ttl = 1800;
Mark Andrews's avatar
Mark Andrews committed
1364
	dns_resolver_setlamettl(view->resolver, lame_ttl);
1365
1366
1367
1368
1369

	obj = NULL;
	result = ns_config_get(maps, "zero-no-soa-ttl-cache", &obj);
	INSIST(result == ISC_R_SUCCESS);
	dns_resolver_setzeronosoattl(view->resolver, cfg_obj_asboolean(obj));
1370

1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
	/*
	 * Set the resolver's EDNS UDP size.
	 */
	obj = NULL;
	result = ns_config_get(maps, "edns-udp-size", &obj);
	INSIST(result == ISC_R_SUCCESS);
	udpsize = cfg_obj_asuint32(obj);
	if (udpsize < 512)
		udpsize = 512;
	if (udpsize > 4096)
		udpsize = 4096;
1382
	dns_resolver_setudpsize(view->resolver, (isc_uint16_t)udpsize);
1383
	
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
	/*
	 * Set the maximum UDP response size.
	 */
	obj = NULL;
	result = ns_config_get(maps, "max-udp-size", &obj);
	INSIST(result == ISC_R_SUCCESS);
	udpsize = cfg_obj_asuint32(obj);
	if (udpsize < 512)
		udpsize = 512;
	if (udpsize > 4096)
		udpsize = 4096;
	view->maxudp = udpsize;

1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
	/*
	 * Set supported DNSSEC algorithms.
	 */
	dns_resolver_reset_algorithms(view->resolver);
	disabled = NULL;
	(void)ns_config_get(maps, "disable-algorithms", &disabled);
	if (disabled != NULL) {
		for (element = cfg_list_first(disabled);
		     element != NULL;
		     element = cfg_list_next(element))
			CHECK(disable_algorithms(cfg_listelt_value(element),
						 view->resolver));
	}
1410

1411
	/*
1412
1413
	 * A global or view "forwarders" option, if present,
	 * creates an entry for "." in the forwarding table.
1414
	 */
1415
	forwardtype = NULL;
1416
	forwarders = NULL;
1417
	(void)ns_config_get(maps, "forward", &forwardtype);
1418
	(void)ns_config_get(maps, "forwarders", &forwarders);
1419
	if (forwarders != NULL)
1420
		CHECK(configure_forward(config, view, dns_rootname,
1421
					forwarders, forwardtype));
1422

1423
1424
1425
1426
1427
1428
1429
1430
	/*
	 * Dual Stack Servers.
	 */
	alternates = NULL;
	(void)ns_config_get(maps, "dual-stack-servers", &alternates);
	if (alternates != NULL)
		CHECK(configure_alternates(config, view, alternates));

1431
	/*
1432
	 * We have default hints for class IN if we need them.
1433
	 */
1434
	if (view->rdclass == dns_rdataclass_in && view->hints == NULL)
1435
		dns_view_sethints(view, ns_g_server->in_roothints);
1436

1437
1438
	/*
	 * If we still have no hints, this is a non-IN view with no
1439
	 * "hints zone" configured.  Issue a warning, except if this