summaryrefslogtreecommitdiffstats
path: root/contrib/dlz/drivers/dlz_bdbhpt_driver.c
blob: fefe99d9dbd5acb41e9d8c4af7775af42218f10d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
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
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
 * Copyright (C) 2002 Stichting NLnet, Netherlands, stichting@nlnet.nl.
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * 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 https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

/*
 * Copyright (C) 1999-2001, 2016  Internet Systems Consortium, Inc. ("ISC")
 *
 * 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/.
 */

#ifdef DLZ_BDB
#include <db.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <isc/mem.h>
#include <isc/print.h>
#include <isc/result.h>
#include <isc/util.h>

#include <dns/log.h>
#include <dns/result.h>
#include <dns/sdlz.h>

#include <dlz/dlz_bdbhpt_driver.h>
#include <named/globals.h>

static dns_sdlzimplementation_t *dlz_bdbhpt = NULL;

/* should the bdb driver use threads. */
#define bdbhpt_threads DB_THREAD

/* bdbhpt database names */
#define dlz_data   "dns_data"
#define dlz_zone   "dns_zone"
#define dlz_xfr	   "dns_xfr"
#define dlz_client "dns_client"

/* This structure contains all the Berkeley DB handles
 * for this instance of the bdbhpt driver.
 */

typedef struct bdbhpt_instance {
	DB_ENV *dbenv;	 /*%< bdbhpt environment */
	DB *data;	 /*%< dns_data database handle */
	DB *zone;	 /*%< zone database handle */
	DB *xfr;	 /*%< zone xfr database handle */
	DB *client;	 /*%< client database handle */
	isc_mem_t *mctx; /*%< memory context */
} bdbhpt_instance_t;

typedef struct bdbhpt_parsed_data {
	char *host;
	char *type;
	int ttl;
	char *data;
} bdbhpt_parsed_data_t;

/* forward reference */

static isc_result_t
bdbhpt_findzone(void *driverarg, void *dbdata, const char *name,
		dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo);

/*%
 * Reverses a string in place.
 */

static char *
bdbhpt_strrev(char *str) {
	char *p1, *p2;

	if (!str || !*str) {
		return (str);
	}
	for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
		*p1 ^= *p2;
		*p2 ^= *p1;
		*p1 ^= *p2;
	}
	return (str);
}

/*%
 * Parses the DBT from the Berkeley DB into a parsed_data record
 * The parsed_data record should be allocated before and passed into the
 * bdbhpt_parse_data function.  The char (type & data) fields should not
 * be "free"d as that memory is part of the DBT data field.  It will be
 * "free"d when the DBT is freed.
 */

static isc_result_t
bdbhpt_parse_data(char *in, bdbhpt_parsed_data_t *pd) {
	char *endp, *ttlStr;
	char *tmp = in;
	char *lastchar = (char *)&tmp[strlen(tmp)];

	/*%
	 * String should be formatted as:
	 *   replication_id
	 *   (a space)
	 *   host_name
	 *   (a space)
	 *   ttl
	 *   (a space)
	 *   type
	 *   (a space)
	 *   remaining data
	 *
	 * examples:
	 *
	 * 9191 host 10 A 127.0.0.1
	 * server1_212 host 10 A 127.0.0.2
	 * {xxxx-xxxx-xxxx-xxxx-xxxx} host 10 MX 20 mail.example.com
	 */

	/*
	 * we don't need the replication id, so don't
	 * bother saving a pointer to it.
	 */

	/* find space after replication id */
	tmp = strchr(tmp, ' ');
	/* verify we found a space */
	if (tmp == NULL) {
		return (ISC_R_FAILURE);
	}
	/* make sure it is safe to increment pointer */
	if (++tmp > lastchar) {
		return (ISC_R_FAILURE);
	}

	/* save pointer to host */
	pd->host = tmp;

	/* find space after host and change it to a '\0' */
	tmp = strchr(tmp, ' ');
	/* verify we found a space */
	if (tmp == NULL) {
		return (ISC_R_FAILURE);
	}
	/* change the space to a null (string terminator) */
	tmp[0] = '\0';
	/* make sure it is safe to increment pointer */
	if (++tmp > lastchar) {
		return (ISC_R_FAILURE);
	}

	/* save pointer to ttl string */
	ttlStr = tmp;

	/* find space after ttl and change it to a '\0' */
	tmp = strchr(tmp, ' ');
	/* verify we found a space */
	if (tmp == NULL) {
		return (ISC_R_FAILURE);
	}
	/* change the space to a null (string terminator) */
	tmp[0] = '\0';
	/* make sure it is safe to increment pointer */
	if (++tmp > lastchar) {
		return (ISC_R_FAILURE);
	}

	/* save pointer to dns type */
	pd->type = tmp;

	/* find space after type and change it to a '\0' */
	tmp = strchr(tmp, ' ');
	/* verify we found a space */
	if (tmp == NULL) {
		return (ISC_R_FAILURE);
	}
	/* change the space to a null (string terminator) */
	tmp[0] = '\0';
	/* make sure it is safe to increment pointer */
	if (++tmp > lastchar) {
		return (ISC_R_FAILURE);
	}

	/* save pointer to remainder of DNS data */
	pd->data = tmp;

	/* convert ttl string to integer */
	pd->ttl = strtol(ttlStr, &endp, 10);
	if (*endp != '\0' || pd->ttl < 0) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "bdbhpt driver ttl must be a positive number");
		return (ISC_R_FAILURE);
	}

	/* if we get this far everything should have worked. */
	return (ISC_R_SUCCESS);
}

/*
 * DLZ methods
 */

static isc_result_t
bdbhpt_allowzonexfr(void *driverarg, void *dbdata, const char *name,
		    const char *client) {
	isc_result_t result;
	bdbhpt_instance_t *db = (bdbhpt_instance_t *)dbdata;
	DBT key, data;

	/* check to see if we are authoritative for the zone first. */
	result = bdbhpt_findzone(driverarg, dbdata, name, NULL, NULL);
	if (result != ISC_R_SUCCESS) {
		return (ISC_R_NOTFOUND);
	}

	memset(&key, 0, sizeof(DBT));
	key.flags = DB_DBT_MALLOC;
	key.data = strdup(name);
	if (key.data == NULL) {
		result = ISC_R_NOMEMORY;
		goto xfr_cleanup;
	}
	key.size = strlen(key.data);

	memset(&data, 0, sizeof(DBT));
	data.flags = DB_DBT_MALLOC;
	data.data = strdup(client);
	if (data.data == NULL) {
		result = ISC_R_NOMEMORY;
		goto xfr_cleanup;
	}
	data.size = strlen(data.data);

	switch (db->client->get(db->client, NULL, &key, &data, DB_GET_BOTH)) {
	case DB_NOTFOUND:
		result = ISC_R_NOTFOUND;
		break;
	case 0:
		result = ISC_R_SUCCESS;
		break;
	default:
		result = ISC_R_FAILURE;
	}

xfr_cleanup:

	/* free any memory duplicate string in the key field */
	if (key.data != NULL) {
		free(key.data);
	}

	/* free any memory allocated to the data field. */
	if (data.data != NULL) {
		free(data.data);
	}

	return (result);
}

/*%
 * BDB does not allow a secondary index on a database that allows
 * duplicates.  We have a few options:
 *
 * 1) kill speed by having lookup method use a secondary db which
 * is associated to the primary DB with the DNS data.  Then have
 * another secondary db for zone transfer which also points to
 * the dns_data primary.  NO - The  point of this driver is
 * lookup performance.
 *
 * 2) Blow up database size by storing DNS data twice.  Once for
 * the lookup (dns_data) database, and a second time for the zone
 * transfer (dns_xfr) database. NO - That would probably require
 * a larger cache to provide good performance.  Also, that would
 * make the DB larger on disk potentially slowing it as well.
 *
 * 3) Loop through the dns_xfr database with a cursor to get
 * all the different hosts in a zone.  Then use the zone & host
 * together to lookup the data in the dns_data database. YES -
 * This may slow down zone xfr's a little, but that's ok they
 * don't happen as often and don't need to be as fast. We can
 * also use this table when deleting a zone (The BDB driver
 * is read only - the delete would be used during replication
 * updates by a separate process).
 */

static isc_result_t
bdbhpt_allnodes(const char *zone, void *driverarg, void *dbdata,
		dns_sdlzallnodes_t *allnodes) {
	isc_result_t result = ISC_R_NOTFOUND;
	bdbhpt_instance_t *db = (bdbhpt_instance_t *)dbdata;
	DBC *xfr_cursor = NULL;
	DBC *dns_cursor = NULL;
	DBT xfr_key, xfr_data, dns_key, dns_data;
	int xfr_flags;
	int dns_flags;
	int bdbhptres;
	bdbhpt_parsed_data_t pd;
	char *tmp = NULL, *tmp_zone, *tmp_zone_host = NULL;

	UNUSED(driverarg);

	memset(&xfr_key, 0, sizeof(DBT));
	memset(&xfr_data, 0, sizeof(DBT));
	memset(&dns_key, 0, sizeof(DBT));
	memset(&dns_data, 0, sizeof(DBT));

	xfr_key.data = tmp_zone = strdup(zone);
	if (xfr_key.data == NULL) {
		return (ISC_R_NOMEMORY);
	}

	xfr_key.size = strlen(xfr_key.data);

	/* get a cursor to loop through dns_xfr table */
	if (db->xfr->cursor(db->xfr, NULL, &xfr_cursor, 0) != 0) {
		result = ISC_R_FAILURE;
		goto allnodes_cleanup;
	}

	/* get a cursor to loop through dns_data table */
	if (db->data->cursor(db->data, NULL, &dns_cursor, 0) != 0) {
		result = ISC_R_FAILURE;
		goto allnodes_cleanup;
	}

	xfr_flags = DB_SET;

	/* loop through xfr table for specified zone. */
	while ((bdbhptres = xfr_cursor->c_get(xfr_cursor, &xfr_key, &xfr_data,
					      xfr_flags)) == 0)
	{
		xfr_flags = DB_NEXT_DUP;

		/* +1 to allow for space between zone and host names */
		dns_key.size = xfr_data.size + xfr_key.size + 1;

		/* +1 to allow for null term at end of string. */
		dns_key.data = tmp_zone_host = malloc(dns_key.size + 1);
		if (dns_key.data == NULL) {
			goto allnodes_cleanup;
		}

		/*
		 * construct search key for dns_data.
		 * zone_name(a space)host_name
		 */
		strcpy(dns_key.data, zone);
		strcat(dns_key.data, " ");
		strncat(dns_key.data, xfr_data.data, xfr_data.size);

		dns_flags = DB_SET;

		while ((bdbhptres = dns_cursor->c_get(dns_cursor, &dns_key,
						      &dns_data, dns_flags)) ==
		       0)
		{
			dns_flags = DB_NEXT_DUP;

			/* +1 to allow for null term at end of string. */
			tmp = realloc(tmp, dns_data.size + 1);
			if (tmp == NULL) {
				goto allnodes_cleanup;
			}

			/* copy data to tmp string, and append null term. */
			strncpy(tmp, dns_data.data, dns_data.size);
			tmp[dns_data.size] = '\0';

			/* split string into dns data parts. */
			if (bdbhpt_parse_data(tmp, &pd) != ISC_R_SUCCESS) {
				goto allnodes_cleanup;
			}

			result = dns_sdlz_putnamedrr(allnodes, pd.host, pd.type,
						     pd.ttl, pd.data);
			if (result != ISC_R_SUCCESS) {
				goto allnodes_cleanup;
			}
		} /* end inner while loop */

		/* clean up memory */
		if (tmp_zone_host != NULL) {
			free(tmp_zone_host);
			tmp_zone_host = NULL;
		}
	} /* end outer while loop */

allnodes_cleanup:

	/* free any memory */
	if (tmp != NULL) {
		free(tmp);
	}

	if (tmp_zone_host != NULL) {
		free(tmp_zone_host);
	}

	if (tmp_zone != NULL) {
		free(tmp_zone);
	}

	/* get rid of cursors */
	if (xfr_cursor != NULL) {
		xfr_cursor->c_close(xfr_cursor);
	}

	if (dns_cursor != NULL) {
		dns_cursor->c_close(dns_cursor);
	}

	return (result);
}

/*%
 * Performs bdbhpt cleanup.
 * Used by bdbhpt_create if there is an error starting up.
 * Used by bdbhpt_destroy when the driver is shutting down.
 */

static void
bdbhpt_cleanup(bdbhpt_instance_t *db) {
	isc_mem_t *mctx;

	/* close databases */
	if (db->data != NULL) {
		db->data->close(db->data, 0);
	}
	if (db->xfr != NULL) {
		db->xfr->close(db->xfr, 0);
	}
	if (db->zone != NULL) {
		db->zone->close(db->zone, 0);
	}
	if (db->client != NULL) {
		db->client->close(db->client, 0);
	}

	/* close environment */
	if (db->dbenv != NULL) {
		db->dbenv->close(db->dbenv, 0);
	}

	/* cleanup memory */
	if (db->mctx != NULL) {
		/* save mctx for later */
		mctx = db->mctx;
		/* return, and detach the memory */
		isc_mem_putanddetach(&mctx, db, sizeof(bdbhpt_instance_t));
	}
}

static isc_result_t
bdbhpt_findzone(void *driverarg, void *dbdata, const char *name,
		dns_clientinfomethods_t *methods,
		dns_clientinfo_t *clientinfo) {
	isc_result_t result;
	bdbhpt_instance_t *db = (bdbhpt_instance_t *)dbdata;
	DBT key, data;

	UNUSED(driverarg);
	UNUSED(methods);
	UNUSED(clientinfo);

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	data.flags = DB_DBT_MALLOC;

	key.data = strdup(name);

	if (key.data == NULL) {
		return (ISC_R_NOMEMORY);
	}

	/*
	 * reverse string to take advantage of BDB locality of reference
	 * if we need further lookups because the zone doesn't match the
	 * first time.
	 */
	key.data = bdbhpt_strrev(key.data);
	key.size = strlen(key.data);

	switch (db->zone->get(db->zone, NULL, &key, &data, 0)) {
	case DB_NOTFOUND:
		result = ISC_R_NOTFOUND;
		break;
	case 0:
		result = ISC_R_SUCCESS;
		break;
	default:
		result = ISC_R_FAILURE;
	}

	/* free any memory duplicate string in the key field */
	if (key.data != NULL) {
		free(key.data);
	}

	/* free any memory allocated to the data field. */
	if (data.data != NULL) {
		free(data.data);
	}

	return (result);
}

static isc_result_t
bdbhpt_lookup(const char *zone, const char *name, void *driverarg, void *dbdata,
	      dns_sdlzlookup_t *lookup, dns_clientinfomethods_t *methods,
	      dns_clientinfo_t *clientinfo) {
	isc_result_t result = ISC_R_NOTFOUND;
	bdbhpt_instance_t *db = (bdbhpt_instance_t *)dbdata;
	DBC *data_cursor = NULL;
	DBT key, data;
	int bdbhptres;
	int flags;

	bdbhpt_parsed_data_t pd;
	char *tmp = NULL;
	char *keyStr = NULL;

	UNUSED(driverarg);
	UNUSED(methods);
	UNUSED(clientinfo);

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	key.size = strlen(zone) + strlen(name) + 1;

	/* allocate mem for key */
	key.data = keyStr = malloc((key.size + 1) * sizeof(char));

	if (keyStr == NULL) {
		return (ISC_R_NOMEMORY);
	}

	strcpy(keyStr, zone);
	strcat(keyStr, " ");
	strcat(keyStr, name);

	/* get a cursor to loop through data */
	if (db->data->cursor(db->data, NULL, &data_cursor, 0) != 0) {
		result = ISC_R_FAILURE;
		goto lookup_cleanup;
	}

	result = ISC_R_NOTFOUND;

	flags = DB_SET;
	while ((bdbhptres = data_cursor->c_get(data_cursor, &key, &data,
					       flags)) == 0)
	{
		flags = DB_NEXT_DUP;
		tmp = realloc(tmp, data.size + 1);
		if (tmp == NULL) {
			goto lookup_cleanup;
		}

		strncpy(tmp, data.data, data.size);
		tmp[data.size] = '\0';

		if (bdbhpt_parse_data(tmp, &pd) != ISC_R_SUCCESS) {
			goto lookup_cleanup;
		}

		result = dns_sdlz_putrr(lookup, pd.type, pd.ttl, pd.data);

		if (result != ISC_R_SUCCESS) {
			goto lookup_cleanup;
		}
	} /* end while loop */

lookup_cleanup:

	/* get rid of cursor */
	if (data_cursor != NULL) {
		data_cursor->c_close(data_cursor);
	}

	free(keyStr);
	if (tmp != NULL) {
		free(tmp);
	}

	return (result);
}

/*% Initializes, sets flags and then opens Berkeley databases. */

static isc_result_t
bdbhpt_opendb(DB_ENV *db_env, DBTYPE db_type, DB **db, const char *db_name,
	      char *db_file, int flags) {
	int result;

	/* Initialize the database. */
	if ((result = db_create(db, db_env, 0)) != 0) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "bdbhpt could not initialize %s database. "
			      "bdbhpt error: %s",
			      db_name, db_strerror(result));
		return (ISC_R_FAILURE);
	}

	/* set database flags. */
	if ((result = (*db)->set_flags(*db, flags)) != 0) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "bdbhpt could not set flags for %s database. "
			      "bdbhpt error: %s",
			      db_name, db_strerror(result));
		return (ISC_R_FAILURE);
	}

	/* open the database. */
	if ((result = (*db)->open(*db, NULL, db_file, db_name, db_type,
				  DB_RDONLY | bdbhpt_threads, 0)) != 0)
	{
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "bdbhpt could not open %s database in %s. "
			      "bdbhpt error: %s",
			      db_name, db_file, db_strerror(result));
		return (ISC_R_FAILURE);
	}

	return (ISC_R_SUCCESS);
}

static isc_result_t
bdbhpt_create(const char *dlzname, unsigned int argc, char *argv[],
	      void *driverarg, void **dbdata) {
	isc_result_t result;
	int bdbhptres;
	int bdbFlags = 0;
	bdbhpt_instance_t *db = NULL;

	UNUSED(dlzname);
	UNUSED(driverarg);

	/* verify we have 4 arg's passed to the driver */
	if (argc != 4) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "bdbhpt driver requires at least "
			      "3 command line args.");
		return (ISC_R_FAILURE);
	}

	switch ((char)*argv[1]) {
	/*
	 * Transactional mode.  Highest safety - lowest speed.
	 */
	case 'T':
	case 't':
		bdbFlags = DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG |
			   DB_INIT_TXN;
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1),
			      "bdbhpt driver using transactional mode.");
		break;
	/*
	 * Concurrent mode.  Lower safety (no rollback) -
	 * higher speed.
	 */
	case 'C':
	case 'c':
		bdbFlags = DB_INIT_CDB | DB_INIT_MPOOL;
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1),
			      "bdbhpt driver using concurrent mode.");
		break;
	/*
	 * Private mode. No inter-process communication & no locking.
	 * Lowest safety - highest speed.
	 */
	case 'P':
	case 'p':
		bdbFlags = DB_PRIVATE | DB_INIT_MPOOL;
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1),
			      "bdbhpt driver using private mode.");
		break;
	default:
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "bdbhpt driver requires the operating mode "
			      "be set to P or C or T.  You specified '%s'",
			      argv[1]);
		return (ISC_R_FAILURE);
	}

	/* allocate and zero memory for driver structure */
	db = isc_mem_get(named_g_mctx, sizeof(bdbhpt_instance_t));
	memset(db, 0, sizeof(bdbhpt_instance_t));

	/* attach to the memory context */
	isc_mem_attach(named_g_mctx, &db->mctx);

	/*
	 * create bdbhpt environment
	 * Basically bdbhpt allocates and assigns memory to db->dbenv
	 */
	bdbhptres = db_env_create(&db->dbenv, 0);
	if (bdbhptres != 0) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "bdbhpt environment could not be created. "
			      "bdbhpt error: %s",
			      db_strerror(bdbhptres));
		result = ISC_R_FAILURE;
		goto init_cleanup;
	}

	/* open bdbhpt environment */
	bdbhptres = db->dbenv->open(db->dbenv, argv[2],
				    bdbFlags | bdbhpt_threads | DB_CREATE, 0);
	if (bdbhptres != 0) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "bdbhpt environment at '%s' could not be opened."
			      " bdbhpt error: %s",
			      argv[2], db_strerror(bdbhptres));
		result = ISC_R_FAILURE;
		goto init_cleanup;
	}

	/* open dlz_data database. */
	result = bdbhpt_opendb(db->dbenv, DB_UNKNOWN, &db->data, dlz_data,
			       argv[3], DB_DUP | DB_DUPSORT);
	if (result != ISC_R_SUCCESS) {
		goto init_cleanup;
	}

	/* open dlz_xfr database. */
	result = bdbhpt_opendb(db->dbenv, DB_UNKNOWN, &db->xfr, dlz_xfr,
			       argv[3], DB_DUP | DB_DUPSORT);
	if (result != ISC_R_SUCCESS) {
		goto init_cleanup;
	}

	/* open dlz_zone database. */
	result = bdbhpt_opendb(db->dbenv, DB_UNKNOWN, &db->zone, dlz_zone,
			       argv[3], 0);
	if (result != ISC_R_SUCCESS) {
		goto init_cleanup;
	}

	/* open dlz_client database. */
	result = bdbhpt_opendb(db->dbenv, DB_UNKNOWN, &db->client, dlz_client,
			       argv[3], DB_DUP | DB_DUPSORT);
	if (result != ISC_R_SUCCESS) {
		goto init_cleanup;
	}

	*dbdata = db;

	return (ISC_R_SUCCESS);

init_cleanup:

	bdbhpt_cleanup(db);
	return (result);
}

static void
bdbhpt_destroy(void *driverarg, void *dbdata) {
	UNUSED(driverarg);

	bdbhpt_cleanup((bdbhpt_instance_t *)dbdata);
}

/*
 * bdbhpt_authority not needed as authority data is returned by lookup
 */
static dns_sdlzmethods_t dlz_bdbhpt_methods = {
	bdbhpt_create,
	bdbhpt_destroy,
	bdbhpt_findzone,
	bdbhpt_lookup,
	NULL,
	bdbhpt_allnodes,
	bdbhpt_allowzonexfr,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
};

/*%
 * Wrapper around dns_sdlzregister().
 */
isc_result_t
dlz_bdbhpt_init(void) {
	isc_result_t result;

	/*
	 * Write debugging message to log
	 */
	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
		      ISC_LOG_DEBUG(2), "Registering DLZ bdbhpt driver.");

	result = dns_sdlzregister("bdbhpt", &dlz_bdbhpt_methods, NULL,
				  DNS_SDLZFLAG_RELATIVEOWNER |
					  DNS_SDLZFLAG_RELATIVERDATA |
					  DNS_SDLZFLAG_THREADSAFE,
				  named_g_mctx, &dlz_bdbhpt);
	if (result != ISC_R_SUCCESS) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "dns_sdlzregister() failed: %s",
				 isc_result_totext(result));
		result = ISC_R_UNEXPECTED;
	}

	return (result);
}

/*%
 * Wrapper around dns_sdlzunregister().
 */
void
dlz_bdbhpt_clear(void) {
	/*
	 * Write debugging message to log
	 */
	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
		      ISC_LOG_DEBUG(2), "Unregistering DLZ bdbhpt driver.");

	if (dlz_bdbhpt != NULL) {
		dns_sdlzunregister(&dlz_bdbhpt);
	}
}

#endif /* ifdef DLZ_BDB */