view.c 61.4 KB
Newer Older
Bob Halley's avatar
add    
Bob Halley committed
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
5
 * 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
6
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7
8
9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
Bob Halley's avatar
add    
Bob Halley committed
10
11
 */

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

14
#include <inttypes.h>
15
#include <limits.h>
16
#include <stdbool.h>
17

18
#ifdef HAVE_LMDB
19
20
#include <lmdb.h>

21
22
#include <dns/lmdb.h>
#endif /* HAVE_LMDB */
23

24
#include <isc/atomic.h>
25
#include <isc/file.h>
26
#include <isc/hash.h>
27
#include <isc/lex.h>
28
#include <isc/print.h>
29
#include <isc/stats.h>
30
#include <isc/string.h> /* Required for HP/UX (and others?) */
31
#include <isc/task.h>
Michael Graff's avatar
Michael Graff committed
32
#include <isc/util.h>
Bob Halley's avatar
add    
Bob Halley committed
33

34
#include <dns/acl.h>
Bob Halley's avatar
add adb    
Bob Halley committed
35
#include <dns/adb.h>
Evan Hunt's avatar
Evan Hunt committed
36
#include <dns/badcache.h>
37
#include <dns/cache.h>
Bob Halley's avatar
Bob Halley committed
38
#include <dns/db.h>
39
#include <dns/dispatch.h>
40
#include <dns/dlz.h>
41
#include <dns/dns64.h>
42
#include <dns/dnssec.h>
43
#include <dns/events.h>
44
#include <dns/forward.h>
45
#include <dns/keytable.h>
46
#include <dns/keyvalues.h>
47
#include <dns/master.h>
48
#include <dns/masterdump.h>
Evan Hunt's avatar
Evan Hunt committed
49
#include <dns/nta.h>
50
#include <dns/order.h>
51
#include <dns/peer.h>
52
#include <dns/rbt.h>
Bob Halley's avatar
Bob Halley committed
53
#include <dns/rdataset.h>
Mark Andrews's avatar
Mark Andrews committed
54
#include <dns/request.h>
55
56
#include <dns/resolver.h>
#include <dns/result.h>
57
#include <dns/rpz.h>
58
#include <dns/rrl.h>
59
#include <dns/stats.h>
60
#include <dns/time.h>
Brian Wellington's avatar
Brian Wellington committed
61
#include <dns/tsig.h>
62
#include <dns/zone.h>
63
#include <dns/zt.h>
Bob Halley's avatar
add    
Bob Halley committed
64

65
66
67
68
69
#define CHECK(op)                            \
	do {                                 \
		result = (op);               \
		if (result != ISC_R_SUCCESS) \
			goto cleanup;        \
70
71
	} while (0)

72
73
74
75
76
77
#define RESSHUTDOWN(v) \
	((atomic_load(&(v)->attributes) & DNS_VIEWATTR_RESSHUTDOWN) != 0)
#define ADBSHUTDOWN(v) \
	((atomic_load(&(v)->attributes) & DNS_VIEWATTR_ADBSHUTDOWN) != 0)
#define REQSHUTDOWN(v) \
	((atomic_load(&(v)->attributes) & DNS_VIEWATTR_REQSHUTDOWN) != 0)
78

79
#define DNS_VIEW_DELONLYHASH   111
Evan Hunt's avatar
Evan Hunt committed
80
#define DNS_VIEW_FAILCACHESIZE 1021
81

82
83
84
85
86
87
static void
resolver_shutdown(isc_task_t *task, isc_event_t *event);
static void
adb_shutdown(isc_task_t *task, isc_event_t *event);
static void
req_shutdown(isc_task_t *task, isc_event_t *event);
88

Bob Halley's avatar
add    
Bob Halley committed
89
isc_result_t
90
dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, const char *name,
91
92
		dns_view_t **viewp) {
	dns_view_t *view;
Bob Halley's avatar
add    
Bob Halley committed
93
	isc_result_t result;
94
	char buffer[1024];
Bob Halley's avatar
add    
Bob Halley committed
95
96
97
98
99
100
101
102

	/*
	 * Create a view.
	 */

	REQUIRE(name != NULL);
	REQUIRE(viewp != NULL && *viewp == NULL);

Andreas Gustafsson's avatar
Andreas Gustafsson committed
103
	view = isc_mem_get(mctx, sizeof(*view));
104

105
	view->nta_file = NULL;
106
107
	view->mctx = NULL;
	isc_mem_attach(mctx, &view->mctx);
Bob Halley's avatar
add    
Bob Halley committed
108
	view->name = isc_mem_strdup(mctx, name);
109

110
111
	result = isc_file_sanitize(NULL, view->name, "nta", buffer,
				   sizeof(buffer));
112
	if (result != ISC_R_SUCCESS) {
113
		goto cleanup_name;
114
	}
115
116
	view->nta_file = isc_mem_strdup(mctx, buffer);

Ondřej Surý's avatar
Ondřej Surý committed
117
	isc_mutex_init(&view->lock);
118

119
	view->zonetable = NULL;
Evan Hunt's avatar
Evan Hunt committed
120
121
122
123
124
125
126
	result = dns_zt_create(mctx, rdclass, &view->zonetable);
	if (result != ISC_R_SUCCESS) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "dns_zt_create() failed: %s",
				 isc_result_totext(result));
		result = ISC_R_UNEXPECTED;
		goto cleanup_mutex;
Bob Halley's avatar
add    
Bob Halley committed
127
	}
Evan Hunt's avatar
Evan Hunt committed
128

129
	view->secroots_priv = NULL;
Evan Hunt's avatar
Evan Hunt committed
130
	view->ntatable_priv = NULL;
131
132
133
134
135
136
137
	view->fwdtable = NULL;
	result = dns_fwdtable_create(mctx, &view->fwdtable);
	if (result != ISC_R_SUCCESS) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "dns_fwdtable_create() failed: %s",
				 isc_result_totext(result));
		result = ISC_R_UNEXPECTED;
138
		goto cleanup_zt;
139
	}
140

141
	view->cache = NULL;
Bob Halley's avatar
Bob Halley committed
142
	view->cachedb = NULL;
Evan Hunt's avatar
Evan Hunt committed
143
144
	ISC_LIST_INIT(view->dlz_searched);
	ISC_LIST_INIT(view->dlz_unsearched);
Bob Halley's avatar
Bob Halley committed
145
	view->hints = NULL;
Bob Halley's avatar
add    
Bob Halley committed
146
	view->resolver = NULL;
Bob Halley's avatar
add adb    
Bob Halley committed
147
	view->adb = NULL;
Mark Andrews's avatar
Mark Andrews committed
148
	view->requestmgr = NULL;
Bob Halley's avatar
add    
Bob Halley committed
149
	view->rdclass = rdclass;
150
	view->frozen = false;
151
	view->task = NULL;
152
	isc_refcount_init(&view->references, 1);
153
	isc_refcount_init(&view->weakrefs, 1);
154
155
156
	atomic_init(&view->attributes,
		    (DNS_VIEWATTR_RESSHUTDOWN | DNS_VIEWATTR_ADBSHUTDOWN |
		     DNS_VIEWATTR_REQSHUTDOWN));
Brian Wellington's avatar
Brian Wellington committed
157
158
	view->statickeys = NULL;
	view->dynamickeys = NULL;
159
	view->matchclients = NULL;
160
	view->matchdestinations = NULL;
161
	view->matchrecursiveonly = false;
162
	result = dns_tsigkeyring_create(view->mctx, &view->dynamickeys);
163
	if (result != ISC_R_SUCCESS) {
164
		goto cleanup_weakrefs;
165
	}
166
	view->peers = NULL;
167
	view->order = NULL;
168
	view->delonly = NULL;
169
	view->rootdelonly = false;
Mark Andrews's avatar
Mark Andrews committed
170
	view->rootexclude = NULL;
171
	view->adbstats = NULL;
172
173
	view->resstats = NULL;
	view->resquerystats = NULL;
174
	view->cacheshared = false;
175
176
	ISC_LIST_INIT(view->dns64);
	view->dns64cnt = 0;
177

178
179
	/*
	 * Initialize configuration data with default values.
180
	 */
181
182
183
184
185
186
187
188
	view->recursion = true;
	view->qminimization = false;
	view->qmin_strict = false;
	view->auth_nxdomain = false; /* Was true in BIND 8 */
	view->enablevalidation = true;
	view->acceptexpired = false;
	view->use_glue_cache = false;
	view->minimal_any = false;
189
	view->minimalresponses = dns_minimal_no;
190
	view->transfer_format = dns_one_answer;
191
192
	view->cacheacl = NULL;
	view->cacheonacl = NULL;
193
	view->checknames = false;
194
	view->queryacl = NULL;
195
	view->queryonacl = NULL;
196
	view->recursionacl = NULL;
197
	view->recursiononacl = NULL;
198
	view->sortlist = NULL;
199
200
201
202
	view->transferacl = NULL;
	view->notifyacl = NULL;
	view->updateacl = NULL;
	view->upfwdacl = NULL;
203
	view->denyansweracl = NULL;
Evan Hunt's avatar
Evan Hunt committed
204
	view->nocasecompress = NULL;
205
	view->msgcompression = true;
206
207
208
	view->answeracl_exclude = NULL;
	view->denyanswernames = NULL;
	view->answernames_exclude = NULL;
Evan Hunt's avatar
Evan Hunt committed
209
	view->rrl = NULL;
210
	view->provideixfr = true;
211
212
	view->maxcachettl = 7 * 24 * 3600;
	view->maxncachettl = 3 * 3600;
213
214
	view->mincachettl = 0;
	view->minncachettl = 0;
Evan Hunt's avatar
Evan Hunt committed
215
216
	view->nta_lifetime = 0;
	view->nta_recheck = 0;
Evan Hunt's avatar
Evan Hunt committed
217
218
	view->prefetch_eligible = 0;
	view->prefetch_trigger = 0;
219
	view->dstport = 53;
220
	view->preferred_glue = 0;
221
	view->flush = false;
222
	view->maxudp = 0;
223
224
	view->staleanswerttl = 1;
	view->staleanswersok = dns_stale_answer_conf;
225
	view->staleanswersenable = false;
226
	view->nocookieudp = 0;
227
228
	view->padding = 0;
	view->pad_acl = NULL;
229
	view->maxbits = 0;
230
	view->rpzs = NULL;
231
	view->catzs = NULL;
232
	view->managed_keys = NULL;
233
	view->redirect = NULL;
234
235
	view->redirectzone = NULL;
	dns_fixedname_init(&view->redirectfixed);
236
237
238
239
240
241
	view->requestnsid = false;
	view->sendcookie = true;
	view->requireservercookie = false;
	view->synthfromdnssec = true;
	view->trust_anchor_telemetry = true;
	view->root_key_sentinel = true;
242
	view->new_zone_dir = NULL;
243
	view->new_zone_file = NULL;
244
245
	view->new_zone_db = NULL;
	view->new_zone_dbenv = NULL;
246
	view->new_zone_mapsize = 0ULL;
247
248
	view->new_zone_config = NULL;
	view->cfg_destroy = NULL;
Evan Hunt's avatar
Evan Hunt committed
249
250
	view->fail_ttl = 0;
	view->failcache = NULL;
251
	result = dns_badcache_init(view->mctx, DNS_VIEW_FAILCACHESIZE,
252
				   &view->failcache);
253
254
255
	if (result != ISC_R_SUCCESS) {
		goto cleanup_dynkeys;
	}
256
	view->v6bias = 0;
Evan Hunt's avatar
Evan Hunt committed
257
258
	view->dtenv = NULL;
	view->dttypes = 0;
259

260
261
	view->plugins = NULL;
	view->plugins_free = NULL;
262
263
264
	view->hooktable = NULL;
	view->hooktable_free = NULL;

Ondřej Surý's avatar
Ondřej Surý committed
265
	isc_mutex_init(&view->new_zone_lock);
266

Evan Hunt's avatar
Evan Hunt committed
267
268
269
	result = dns_order_create(view->mctx, &view->order);
	if (result != ISC_R_SUCCESS) {
		goto cleanup_new_zone_lock;
270
	}
271

272
	result = dns_peerlist_new(view->mctx, &view->peers);
273
	if (result != ISC_R_SUCCESS) {
274
		goto cleanup_order;
275
	}
276

277
	result = dns_aclenv_init(view->mctx, &view->aclenv);
278
	if (result != ISC_R_SUCCESS) {
279
		goto cleanup_peerlist;
280
	}
281

Michael Graff's avatar
Michael Graff committed
282
	ISC_LINK_INIT(view, link);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
283
	ISC_EVENT_INIT(&view->resevent, sizeof(view->resevent), 0, NULL,
284
285
		       DNS_EVENT_VIEWRESSHUTDOWN, resolver_shutdown, view, NULL,
		       NULL, NULL);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
286
	ISC_EVENT_INIT(&view->adbevent, sizeof(view->adbevent), 0, NULL,
287
288
		       DNS_EVENT_VIEWADBSHUTDOWN, adb_shutdown, view, NULL,
		       NULL, NULL);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
289
	ISC_EVENT_INIT(&view->reqevent, sizeof(view->reqevent), 0, NULL,
290
291
		       DNS_EVENT_VIEWREQSHUTDOWN, req_shutdown, view, NULL,
		       NULL, NULL);
292
	view->viewlist = NULL;
Bob Halley's avatar
add    
Bob Halley committed
293
	view->magic = DNS_VIEW_MAGIC;
294

Bob Halley's avatar
add    
Bob Halley committed
295
296
297
298
	*viewp = view;

	return (ISC_R_SUCCESS);

299
cleanup_peerlist:
300
	if (view->peers != NULL) {
301
		dns_peerlist_detach(&view->peers);
302
	}
303

304
cleanup_order:
305
	if (view->order != NULL) {
306
		dns_order_detach(&view->order);
307
	}
308

309
cleanup_new_zone_lock:
310
	isc_mutex_destroy(&view->new_zone_lock);
311

312
313
	dns_badcache_destroy(&view->failcache);

314
cleanup_dynkeys:
315
	if (view->dynamickeys != NULL) {
316
		dns_tsigkeyring_detach(&view->dynamickeys);
317
	}
318

319
cleanup_weakrefs:
320
	isc_refcount_decrementz(&view->weakrefs);
321
322
	isc_refcount_destroy(&view->weakrefs);

323
	isc_refcount_decrementz(&view->references);
324
325
	isc_refcount_destroy(&view->references);

326
	if (view->fwdtable != NULL) {
327
		dns_fwdtable_destroy(&view->fwdtable);
328
	}
329

330
cleanup_zt:
331
	if (view->zonetable != NULL) {
332
		dns_zt_detach(&view->zonetable);
333
	}
Bob Halley's avatar
Bob Halley committed
334

335
cleanup_mutex:
336
	isc_mutex_destroy(&view->lock);
Bob Halley's avatar
add    
Bob Halley committed
337

338
	if (view->nta_file != NULL) {
339
		isc_mem_free(mctx, view->nta_file);
340
	}
341

342
cleanup_name:
Bob Halley's avatar
add    
Bob Halley committed
343
	isc_mem_free(mctx, view->name);
344
	isc_mem_putanddetach(&view->mctx, view, sizeof(*view));
Bob Halley's avatar
add    
Bob Halley committed
345
346
347
348
349

	return (result);
}

static inline void
350
destroy(dns_view_t *view) {
351
	dns_dns64_t *dns64;
Evan Hunt's avatar
Evan Hunt committed
352
	dns_dlzdb_t *dlzdb;
353

Bob Halley's avatar
add    
Bob Halley committed
354
	REQUIRE(!ISC_LINK_LINKED(view, link));
355
356
	REQUIRE(RESSHUTDOWN(view));
	REQUIRE(ADBSHUTDOWN(view));
Mark Andrews's avatar
Mark Andrews committed
357
	REQUIRE(REQSHUTDOWN(view));
Bob Halley's avatar
add    
Bob Halley committed
358

359
360
361
	isc_refcount_destroy(&view->references);
	isc_refcount_destroy(&view->weakrefs);

362
	if (view->order != NULL) {
363
		dns_order_detach(&view->order);
364
365
	}
	if (view->peers != NULL) {
366
		dns_peerlist_detach(&view->peers);
367
	}
368
369
370

	if (view->dynamickeys != NULL) {
		isc_result_t result;
371
		char template[PATH_MAX];
372
		char keyfile[PATH_MAX];
373
		FILE *fp = NULL;
374
375
376
377

		result = isc_file_mktemplate(NULL, template, sizeof(template));
		if (result == ISC_R_SUCCESS) {
			(void)isc_file_openuniqueprivate(template, &fp);
378
		}
379
		if (fp == NULL) {
380
			dns_tsigkeyring_detach(&view->dynamickeys);
381
		} else {
382
383
			result = dns_tsigkeyring_dumpanddetach(
				&view->dynamickeys, fp);
384
			if (result == ISC_R_SUCCESS) {
385
				if (fclose(fp) == 0) {
386
387
388
					result = isc_file_sanitize(
						NULL, view->name, "tsigkeys",
						keyfile, sizeof(keyfile));
389
					if (result == ISC_R_SUCCESS) {
390
391
						result = isc_file_rename(
							template, keyfile);
392
					}
393
				}
394
				if (result != ISC_R_SUCCESS) {
395
					(void)remove(template);
396
				}
397
398
399
400
401
402
			} else {
				(void)fclose(fp);
				(void)remove(template);
			}
		}
	}
403
	if (view->statickeys != NULL) {
404
		dns_tsigkeyring_detach(&view->statickeys);
405
406
	}
	if (view->adb != NULL) {
Bob Halley's avatar
add adb    
Bob Halley committed
407
		dns_adb_detach(&view->adb);
408
409
	}
	if (view->resolver != NULL) {
Bob Halley's avatar
add    
Bob Halley committed
410
		dns_resolver_detach(&view->resolver);
411
	}
Evan Hunt's avatar
Evan Hunt committed
412
	dns_rrl_view_destroy(view);
413
	if (view->rpzs != NULL) {
414
		dns_rpz_detach_rpzs(&view->rpzs);
415
416
	}
	if (view->catzs != NULL) {
417
		dns_catz_catzs_detach(&view->catzs);
418
	}
419
	for (dlzdb = ISC_LIST_HEAD(view->dlz_searched); dlzdb != NULL;
420
421
	     dlzdb = ISC_LIST_HEAD(view->dlz_searched))
	{
Evan Hunt's avatar
Evan Hunt committed
422
423
424
		ISC_LIST_UNLINK(view->dlz_searched, dlzdb, link);
		dns_dlzdestroy(&dlzdb);
	}
425
	for (dlzdb = ISC_LIST_HEAD(view->dlz_unsearched); dlzdb != NULL;
426
427
	     dlzdb = ISC_LIST_HEAD(view->dlz_unsearched))
	{
Evan Hunt's avatar
Evan Hunt committed
428
429
430
		ISC_LIST_UNLINK(view->dlz_unsearched, dlzdb, link);
		dns_dlzdestroy(&dlzdb);
	}
431
	if (view->requestmgr != NULL) {
Mark Andrews's avatar
Mark Andrews committed
432
		dns_requestmgr_detach(&view->requestmgr);
433
434
	}
	if (view->task != NULL) {
435
		isc_task_detach(&view->task);
436
437
	}
	if (view->hints != NULL) {
Bob Halley's avatar
Bob Halley committed
438
		dns_db_detach(&view->hints);
439
440
	}
	if (view->cachedb != NULL) {
Bob Halley's avatar
Bob Halley committed
441
		dns_db_detach(&view->cachedb);
442
443
	}
	if (view->cache != NULL) {
444
		dns_cache_detach(&view->cache);
445
446
	}
	if (view->nocasecompress != NULL) {
Evan Hunt's avatar
Evan Hunt committed
447
		dns_acl_detach(&view->nocasecompress);
448
449
	}
	if (view->matchclients != NULL) {
450
		dns_acl_detach(&view->matchclients);
451
452
	}
	if (view->matchdestinations != NULL) {
453
		dns_acl_detach(&view->matchdestinations);
454
455
	}
	if (view->cacheacl != NULL) {
456
		dns_acl_detach(&view->cacheacl);
457
458
	}
	if (view->cacheonacl != NULL) {
459
		dns_acl_detach(&view->cacheonacl);
460
461
	}
	if (view->queryacl != NULL) {
462
		dns_acl_detach(&view->queryacl);
463
464
	}
	if (view->queryonacl != NULL) {
465
		dns_acl_detach(&view->queryonacl);
466
467
	}
	if (view->recursionacl != NULL) {
468
		dns_acl_detach(&view->recursionacl);
469
470
	}
	if (view->recursiononacl != NULL) {
471
		dns_acl_detach(&view->recursiononacl);
472
473
	}
	if (view->sortlist != NULL) {
474
		dns_acl_detach(&view->sortlist);
475
476
	}
	if (view->transferacl != NULL) {
477
		dns_acl_detach(&view->transferacl);
478
479
	}
	if (view->notifyacl != NULL) {
480
		dns_acl_detach(&view->notifyacl);
481
482
	}
	if (view->updateacl != NULL) {
483
		dns_acl_detach(&view->updateacl);
484
485
	}
	if (view->upfwdacl != NULL) {
486
		dns_acl_detach(&view->upfwdacl);
487
488
	}
	if (view->denyansweracl != NULL) {
489
		dns_acl_detach(&view->denyansweracl);
490
491
	}
	if (view->pad_acl != NULL) {
492
		dns_acl_detach(&view->pad_acl);
493
494
	}
	if (view->answeracl_exclude != NULL) {
495
		dns_rbt_destroy(&view->answeracl_exclude);
496
497
	}
	if (view->denyanswernames != NULL) {
498
		dns_rbt_destroy(&view->denyanswernames);
499
500
	}
	if (view->answernames_exclude != NULL) {
501
		dns_rbt_destroy(&view->answernames_exclude);
502
	}
503
504
	if (view->delonly != NULL) {
		dns_name_t *name;
505
		int i;
506
507
508
509
510
511
512
513
514
515

		for (i = 0; i < DNS_VIEW_DELONLYHASH; i++) {
			name = ISC_LIST_HEAD(view->delonly[i]);
			while (name != NULL) {
				ISC_LIST_UNLINK(view->delonly[i], name, link);
				dns_name_free(name, view->mctx);
				isc_mem_put(view->mctx, name, sizeof(*name));
				name = ISC_LIST_HEAD(view->delonly[i]);
			}
		}
516
517
		isc_mem_put(view->mctx, view->delonly,
			    sizeof(dns_namelist_t) * DNS_VIEW_DELONLYHASH);
518
519
		view->delonly = NULL;
	}
Mark Andrews's avatar
Mark Andrews committed
520
521
	if (view->rootexclude != NULL) {
		dns_name_t *name;
522
		int i;
Mark Andrews's avatar
Mark Andrews committed
523
524
525
526

		for (i = 0; i < DNS_VIEW_DELONLYHASH; i++) {
			name = ISC_LIST_HEAD(view->rootexclude[i]);
			while (name != NULL) {
527
528
				ISC_LIST_UNLINK(view->rootexclude[i], name,
						link);
Mark Andrews's avatar
Mark Andrews committed
529
530
531
532
533
534
535
536
537
				dns_name_free(name, view->mctx);
				isc_mem_put(view->mctx, name, sizeof(*name));
				name = ISC_LIST_HEAD(view->rootexclude[i]);
			}
		}
		isc_mem_put(view->mctx, view->rootexclude,
			    sizeof(dns_namelist_t) * DNS_VIEW_DELONLYHASH);
		view->rootexclude = NULL;
	}
538
	if (view->adbstats != NULL) {
539
		isc_stats_detach(&view->adbstats);
540
541
	}
	if (view->resstats != NULL) {
542
		isc_stats_detach(&view->resstats);
543
544
	}
	if (view->resquerystats != NULL) {
545
		dns_stats_detach(&view->resquerystats);
546
547
	}
	if (view->secroots_priv != NULL) {
548
		dns_keytable_detach(&view->secroots_priv);
549
550
	}
	if (view->ntatable_priv != NULL) {
Evan Hunt's avatar
Evan Hunt committed
551
		dns_ntatable_detach(&view->ntatable_priv);
552
	}
553
	for (dns64 = ISC_LIST_HEAD(view->dns64); dns64 != NULL;
554
555
	     dns64 = ISC_LIST_HEAD(view->dns64))
	{
556
557
558
		dns_dns64_unlink(&view->dns64, dns64);
		dns_dns64_destroy(&dns64);
	}
559
	if (view->managed_keys != NULL) {
560
		dns_zone_detach(&view->managed_keys);
561
562
	}
	if (view->redirect != NULL) {
563
		dns_zone_detach(&view->redirect);
564
	}
Evan Hunt's avatar
Evan Hunt committed
565
#ifdef HAVE_DNSTAP
566
	if (view->dtenv != NULL) {
Evan Hunt's avatar
Evan Hunt committed
567
		dns_dt_detach(&view->dtenv);
568
	}
Evan Hunt's avatar
Evan Hunt committed
569
#endif /* HAVE_DNSTAP */
570
	dns_view_setnewzones(view, false, NULL, NULL, 0ULL);
571
572
573
574
	if (view->new_zone_file != NULL) {
		isc_mem_free(view->mctx, view->new_zone_file);
		view->new_zone_file = NULL;
	}
575
576
577
578
	if (view->new_zone_dir != NULL) {
		isc_mem_free(view->mctx, view->new_zone_dir);
		view->new_zone_dir = NULL;
	}
579
#ifdef HAVE_LMDB
580
	if (view->new_zone_dbenv != NULL) {
581
		mdb_env_close((MDB_env *)view->new_zone_dbenv);
582
583
		view->new_zone_dbenv = NULL;
	}
584
585
586
587
588
	if (view->new_zone_db != NULL) {
		isc_mem_free(view->mctx, view->new_zone_db);
		view->new_zone_db = NULL;
	}
#endif /* HAVE_LMDB */
589
	dns_fwdtable_destroy(&view->fwdtable);
590
	dns_aclenv_destroy(&view->aclenv);
591
	if (view->failcache != NULL) {
592
		dns_badcache_destroy(&view->failcache);
593
	}
594
595
	isc_mutex_destroy(&view->new_zone_lock);
	isc_mutex_destroy(&view->lock);
596
597
	isc_refcount_destroy(&view->references);
	isc_refcount_destroy(&view->weakrefs);
598
	isc_mem_free(view->mctx, view->nta_file);
Bob Halley's avatar
add    
Bob Halley committed
599
	isc_mem_free(view->mctx, view->name);
600
601
602
	if (view->hooktable != NULL && view->hooktable_free != NULL) {
		view->hooktable_free(view->mctx, &view->hooktable);
	}
603
604
	if (view->plugins != NULL && view->plugins_free != NULL) {
		view->plugins_free(view->mctx, &view->plugins);
605
	}
606
	isc_mem_putanddetach(&view->mctx, view, sizeof(*view));
Bob Halley's avatar
add    
Bob Halley committed
607
608
}

609
void
610
dns_view_attach(dns_view_t *source, dns_view_t **targetp) {
611
612
613
	REQUIRE(DNS_VIEW_VALID(source));
	REQUIRE(targetp != NULL && *targetp == NULL);

614
	isc_refcount_increment(&source->references);
615
616
617
618

	*targetp = source;
}

619
static void
620
view_flushanddetach(dns_view_t **viewp, bool flush) {
621
622
623
	REQUIRE(viewp != NULL && DNS_VIEW_VALID(*viewp));
	dns_view_t *view = *viewp;
	*viewp = NULL;
Bob Halley's avatar
add    
Bob Halley committed
624

625
626
627
	if (flush) {
		view->flush = flush;
	}
Bob Halley's avatar
add    
Bob Halley committed
628

629
	if (isc_refcount_decrement(&view->references) == 1) {
Evan Hunt's avatar
Evan Hunt committed
630
631
		dns_zone_t *mkzone = NULL, *rdzone = NULL;

632
633
		isc_refcount_destroy(&view->references);
		if (!RESSHUTDOWN(view)) {
634
			dns_resolver_shutdown(view->resolver);
635
636
		}
		if (!ADBSHUTDOWN(view)) {
637
			dns_adb_shutdown(view->adb);
638
639
		}
		if (!REQSHUTDOWN(view)) {
Mark Andrews's avatar
Mark Andrews committed
640
			dns_requestmgr_shutdown(view->requestmgr);
641
		}
Mark Andrews's avatar
Mark Andrews committed
642
		LOCK(&view->lock);
643
		if (view->zonetable != NULL) {
644
			if (view->flush) {
645
				dns_zt_flushanddetach(&view->zonetable);
646
			} else {
647
				dns_zt_detach(&view->zonetable);
648
			}
649
		}
650
		if (view->managed_keys != NULL) {
Evan Hunt's avatar
Evan Hunt committed
651
652
			mkzone = view->managed_keys;
			view->managed_keys = NULL;
653
			if (view->flush) {
Evan Hunt's avatar
Evan Hunt committed
654
				dns_zone_flush(mkzone);
655
			}
656
		}
657
		if (view->redirect != NULL) {
Evan Hunt's avatar
Evan Hunt committed
658
659
			rdzone = view->redirect;
			view->redirect = NULL;
660
			if (view->flush) {
Evan Hunt's avatar
Evan Hunt committed
661
				dns_zone_flush(rdzone);
662
			}
663
		}
664
665
666
		if (view->catzs != NULL) {
			dns_catz_catzs_detach(&view->catzs);
		}
667
668
669
		if (view->ntatable_priv != NULL) {
			dns_ntatable_shutdown(view->ntatable_priv);
		}
670
		UNLOCK(&view->lock);
Evan Hunt's avatar
Evan Hunt committed
671
672

		/* Need to detach zones outside view lock */
673
		if (mkzone != NULL) {
Evan Hunt's avatar
Evan Hunt committed
674
			dns_zone_detach(&mkzone);
675
		}
Evan Hunt's avatar
Evan Hunt committed
676

677
		if (rdzone != NULL) {
Evan Hunt's avatar
Evan Hunt committed
678
			dns_zone_detach(&rdzone);
679
		}
Bob Halley's avatar
add    
Bob Halley committed
680

681
		dns_view_weakdetach(&view);
682
	}
683
684
}

685
void
686
dns_view_flushanddetach(dns_view_t **viewp) {
687
	view_flushanddetach(viewp, true);
688
689
690
}

void
691
dns_view_detach(dns_view_t **viewp) {
692
	view_flushanddetach(viewp, false);
693
694
}

Mark Andrews's avatar
Mark Andrews committed
695
static isc_result_t
696
dialup(dns_zone_t *zone, void *dummy) {
697
	UNUSED(dummy);
Mark Andrews's avatar
Mark Andrews committed
698
699
700
701
702
	dns_zone_dialup(zone);
	return (ISC_R_SUCCESS);
}

void
703
dns_view_dialup(dns_view_t *view) {
Mark Andrews's avatar
Mark Andrews committed
704
	REQUIRE(DNS_VIEW_VALID(view));
705
706
	REQUIRE(view->zonetable != NULL);

707
	(void)dns_zt_apply(view->zonetable, false, NULL, dialup, NULL);
Mark Andrews's avatar
Mark Andrews committed
708
709
}

710
void
711
dns_view_weakattach(dns_view_t *source, dns_view_t **targetp) {
712
713
714
	REQUIRE(DNS_VIEW_VALID(source));
	REQUIRE(targetp != NULL && *targetp == NULL);

715
	isc_refcount_increment(&source->weakrefs);
716
717
718
719
720

	*targetp = source;
}

void
721
dns_view_weakdetach(dns_view_t **viewp) {
722
723
724
725
726
	dns_view_t *view;

	REQUIRE(viewp != NULL);
	view = *viewp;
	*viewp = NULL;
727
	REQUIRE(DNS_VIEW_VALID(view));
728

729
	if (isc_refcount_decrement(&view->weakrefs) == 1) {