sdb.c 37.9 KB
Newer Older
1
/*
2
 * Copyright (C) 2004-2014  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 2000, 2001, 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
8
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
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
 */

Mark Andrews's avatar
Mark Andrews committed
18
/* $Id$ */
19
20

/*! \file */
21
22
23

#include <config.h>

Brian Wellington's avatar
Brian Wellington committed
24
25
#include <string.h>

26
27
#include <isc/buffer.h>
#include <isc/lex.h>
28
#include <isc/log.h>
29
30
31
32
33
34
35
#include <isc/magic.h>
#include <isc/mem.h>
#include <isc/once.h>
#include <isc/print.h>
#include <isc/region.h>
#include <isc/util.h>

36
#include <dns/callbacks.h>
37
#include <dns/db.h>
38
#include <dns/dbiterator.h>
39
#include <dns/fixedname.h>
40
#include <dns/log.h>
41
42
43
44
45
46
47
48
49
50
51
#include <dns/rdata.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
#include <dns/rdatatype.h>
#include <dns/result.h>
#include <dns/sdb.h>
#include <dns/types.h>

#include "rdatalist_p.h"

52
struct dns_sdbimplementation {
53
	const dns_sdbmethods_t		*methods;
54
	void				*driverdata;
55
	unsigned int			flags;
56
	isc_mem_t			*mctx;
57
	isc_mutex_t			driverlock;
58
59
	dns_dbimplementation_t		*dbimp;
};
60
61
62
63
64

struct dns_sdb {
	/* Unlocked */
	dns_db_t			common;
	char				*zone;
65
	dns_sdbimplementation_t		*implementation;
66
67
68
69
70
71
72
73
74
75
76
77
	void				*dbdata;
	isc_mutex_t			lock;
	/* Locked */
	unsigned int			references;
};

struct dns_sdblookup {
	/* Unlocked */
	unsigned int			magic;
	dns_sdb_t			*sdb;
	ISC_LIST(dns_rdatalist_t)	lists;
	ISC_LIST(isc_buffer_t)		buffers;
78
79
	dns_name_t			*name;
	ISC_LINK(dns_sdblookup_t)	link;
80
	isc_mutex_t			lock;
81
	dns_rdatacallbacks_t		callbacks;
82
83
84
85
	/* Locked */
	unsigned int			references;
};

86
87
88
89
90
91
92
93
94
95
96
typedef struct dns_sdblookup dns_sdbnode_t;

struct dns_sdballnodes {
	dns_dbiterator_t		common;
	ISC_LIST(dns_sdbnode_t)		nodelist;
	dns_sdbnode_t			*current;
	dns_sdbnode_t			*origin;
};

typedef dns_sdballnodes_t sdb_dbiterator_t;

97
98
99
100
101
typedef struct sdb_rdatasetiter {
	dns_rdatasetiter_t		common;
	dns_rdatalist_t			*current;
} sdb_rdatasetiter_t;

102
#define SDB_MAGIC		ISC_MAGIC('S', 'D', 'B', '-')
103

104
/*%
105
106
107
108
109
 * Note that "impmagic" is not the first four bytes of the struct, so
 * ISC_MAGIC_VALID cannot be used.
 */
#define VALID_SDB(sdb)		((sdb) != NULL && \
				 (sdb)->common.impmagic == SDB_MAGIC)
110
111
112
113
114

#define SDBLOOKUP_MAGIC		ISC_MAGIC('S','D','B','L')
#define VALID_SDBLOOKUP(sdbl)	ISC_MAGIC_VALID(sdbl, SDBLOOKUP_MAGIC)
#define VALID_SDBNODE(sdbn)	VALID_SDBLOOKUP(sdbn)

115
/* These values are taken from RFC1537 */
116
117
118
119
120
121
122
123
#define SDB_DEFAULT_REFRESH	(60 * 60 * 8)
#define SDB_DEFAULT_RETRY	(60 * 60 * 2)
#define SDB_DEFAULT_EXPIRE	(60 * 60 * 24 * 7)
#define SDB_DEFAULT_MINIMUM	(60 * 60 * 24)

/* This is a reasonable value */
#define SDB_DEFAULT_TTL		(60 * 60 * 24)

124
125
126
127
#ifdef __COVERITY__
#define MAYBE_LOCK(sdb) LOCK(&sdb->implementation->driverlock)
#define MAYBE_UNLOCK(sdb) UNLOCK(&sdb->implementation->driverlock)
#else
128
129
130
131
#define MAYBE_LOCK(sdb)							\
	do {								\
		unsigned int flags = sdb->implementation->flags;	\
		if ((flags & DNS_SDBFLAG_THREADSAFE) == 0)		\
132
			LOCK(&sdb->implementation->driverlock);		\
133
134
135
136
137
138
	} while (0)

#define MAYBE_UNLOCK(sdb)						\
	do {								\
		unsigned int flags = sdb->implementation->flags;	\
		if ((flags & DNS_SDBFLAG_THREADSAFE) == 0)		\
139
			UNLOCK(&sdb->implementation->driverlock);	\
140
	} while (0)
141
#endif
142

143
144
static int dummy;

145
146
147
148
static isc_result_t dns_sdb_create(isc_mem_t *mctx, dns_name_t *origin,
				   dns_dbtype_t type, dns_rdataclass_t rdclass,
				   unsigned int argc, char *argv[],
				   void *driverarg, dns_db_t **dbp);
149
150
151
152
153
154
155

static isc_result_t findrdataset(dns_db_t *db, dns_dbnode_t *node,
				 dns_dbversion_t *version,
				 dns_rdatatype_t type, dns_rdatatype_t covers,
				 isc_stdtime_t now, dns_rdataset_t *rdataset,
				 dns_rdataset_t *sigrdataset);

156
157
158
159
static isc_result_t createnode(dns_sdb_t *sdb, dns_sdbnode_t **nodep);

static void destroynode(dns_sdbnode_t *node);

160
161
162
163
164
165
166
static void detachnode(dns_db_t *db, dns_dbnode_t **targetp);


static void list_tordataset(dns_rdatalist_t *rdatalist,
			    dns_db_t *db, dns_dbnode_t *node,
			    dns_rdataset_t *rdataset);

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
static void		dbiterator_destroy(dns_dbiterator_t **iteratorp);
static isc_result_t	dbiterator_first(dns_dbiterator_t *iterator);
static isc_result_t	dbiterator_last(dns_dbiterator_t *iterator);
static isc_result_t	dbiterator_seek(dns_dbiterator_t *iterator,
					dns_name_t *name);
static isc_result_t	dbiterator_prev(dns_dbiterator_t *iterator);
static isc_result_t	dbiterator_next(dns_dbiterator_t *iterator);
static isc_result_t	dbiterator_current(dns_dbiterator_t *iterator,
					   dns_dbnode_t **nodep,
					   dns_name_t *name);
static isc_result_t	dbiterator_pause(dns_dbiterator_t *iterator);
static isc_result_t	dbiterator_origin(dns_dbiterator_t *iterator,
					  dns_name_t *name);

static dns_dbiteratormethods_t dbiterator_methods = {
	dbiterator_destroy,
	dbiterator_first,
	dbiterator_last,
	dbiterator_seek,
	dbiterator_prev,
	dbiterator_next,
	dbiterator_current,
	dbiterator_pause,
	dbiterator_origin
};
192

193
194
195
196
197
static void		rdatasetiter_destroy(dns_rdatasetiter_t **iteratorp);
static isc_result_t	rdatasetiter_first(dns_rdatasetiter_t *iterator);
static isc_result_t	rdatasetiter_next(dns_rdatasetiter_t *iterator);
static void		rdatasetiter_current(dns_rdatasetiter_t *iterator,
					     dns_rdataset_t *rdataset);
198
199
200
201
202
203
204
205
206
207
208
209

static dns_rdatasetitermethods_t rdatasetiter_methods = {
	rdatasetiter_destroy,
	rdatasetiter_first,
	rdatasetiter_next,
	rdatasetiter_current
};

/*
 * Functions used by implementors of simple databases
 */
isc_result_t
210
dns_sdb_register(const char *drivername, const dns_sdbmethods_t *methods,
211
212
		 void *driverdata, unsigned int flags, isc_mem_t *mctx,
		 dns_sdbimplementation_t **sdbimp)
213
{
214
215
	dns_sdbimplementation_t *imp;
	isc_result_t result;
216
217

	REQUIRE(drivername != NULL);
218
	REQUIRE(methods != NULL);
219
	REQUIRE(methods->lookup != NULL || methods->lookup2 != NULL);
220
221
	REQUIRE(mctx != NULL);
	REQUIRE(sdbimp != NULL && *sdbimp == NULL);
222
223
	REQUIRE((flags & ~(DNS_SDBFLAG_RELATIVEOWNER |
			   DNS_SDBFLAG_RELATIVERDATA |
224
225
			   DNS_SDBFLAG_THREADSAFE|
			   DNS_SDBFLAG_DNS64)) == 0);
226

227
228
229
230
231
232
233
234
	imp = isc_mem_get(mctx, sizeof(dns_sdbimplementation_t));
	if (imp == NULL)
		return (ISC_R_NOMEMORY);
	imp->methods = methods;
	imp->driverdata = driverdata;
	imp->flags = flags;
	imp->mctx = NULL;
	isc_mem_attach(mctx, &imp->mctx);
235
	result = isc_mutex_init(&imp->driverlock);
236
	if (result != ISC_R_SUCCESS)
237
238
		goto cleanup_mctx;

239
240
241
	imp->dbimp = NULL;
	result = dns_db_register(drivername, dns_sdb_create, imp, mctx,
				 &imp->dbimp);
242
243
	if (result != ISC_R_SUCCESS)
		goto cleanup_mutex;
244
	*sdbimp = imp;
245
246

	return (ISC_R_SUCCESS);
247
248
249
250
251
252

 cleanup_mutex:
	DESTROYLOCK(&imp->driverlock);
 cleanup_mctx:
	isc_mem_put(mctx, imp, sizeof(dns_sdbimplementation_t));
	return (result);
253
254
255
}

void
256
257
258
dns_sdb_unregister(dns_sdbimplementation_t **sdbimp) {
	dns_sdbimplementation_t *imp;
	isc_mem_t *mctx;
259

260
261
262
263
	REQUIRE(sdbimp != NULL && *sdbimp != NULL);

	imp = *sdbimp;
	dns_db_unregister(&imp->dbimp);
264
	DESTROYLOCK(&imp->driverlock);
265
266
267
268
269
270

	mctx = imp->mctx;
	isc_mem_put(mctx, imp, sizeof(dns_sdbimplementation_t));
	isc_mem_detach(&mctx);

	*sdbimp = NULL;
271
272
}

273
static inline unsigned int
274
initial_size(unsigned int len) {
Brian Wellington's avatar
Brian Wellington committed
275
	unsigned int size;
276
277

	for (size = 1024; size < (64 * 1024); size *= 2)
278
279
		if (len < size)
			return (size);
280
	return (65535);
281
282
}

283
isc_result_t
284
285
286
dns_sdb_putrdata(dns_sdblookup_t *lookup, dns_rdatatype_t typeval,
		 dns_ttl_t ttl, const unsigned char *rdatap,
		 unsigned int rdlen)
287
288
289
{
	dns_rdatalist_t *rdatalist;
	dns_rdata_t *rdata;
290
	isc_buffer_t *rdatabuf = NULL;
291
292
	isc_result_t result;
	isc_mem_t *mctx;
293
	isc_region_t region;
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

	mctx = lookup->sdb->common.mctx;

	rdatalist = ISC_LIST_HEAD(lookup->lists);
	while (rdatalist != NULL) {
		if (rdatalist->type == typeval)
			break;
		rdatalist = ISC_LIST_NEXT(rdatalist, link);
	}

	if (rdatalist == NULL) {
		rdatalist = isc_mem_get(mctx, sizeof(dns_rdatalist_t));
		if (rdatalist == NULL)
			return (ISC_R_NOMEMORY);
		rdatalist->rdclass = lookup->sdb->common.rdclass;
		rdatalist->type = typeval;
		rdatalist->covers = 0;
		rdatalist->ttl = ttl;
		ISC_LIST_INIT(rdatalist->rdata);
		ISC_LINK_INIT(rdatalist, link);
		ISC_LIST_APPEND(lookup->lists, rdatalist, link);
Automatic Updater's avatar
Automatic Updater committed
315
	} else
316
317
318
319
320
321
		if (rdatalist->ttl != ttl)
			return (DNS_R_BADTTL);

	rdata = isc_mem_get(mctx, sizeof(dns_rdata_t));
	if (rdata == NULL)
		return (ISC_R_NOMEMORY);
322
323
324
325
326
327
328
329

	result = isc_buffer_allocate(mctx, &rdatabuf, rdlen);
	if (result != ISC_R_SUCCESS)
		goto failure;
	DE_CONST(rdatap, region.base);
	region.length = rdlen;
	isc_buffer_copyregion(rdatabuf, &region);
	isc_buffer_usedregion(rdatabuf, &region);
330
	dns_rdata_init(rdata);
331
332
333
334
335
336
337
338
339
340
341
	dns_rdata_fromregion(rdata, rdatalist->rdclass, rdatalist->type,
			     &region);
	ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
	ISC_LIST_APPEND(lookup->buffers, rdatabuf, link);
	rdata = NULL;

 failure:
	if (rdata != NULL)
		isc_mem_put(mctx, rdata, sizeof(dns_rdata_t));
	return (result);
}
Automatic Updater's avatar
Automatic Updater committed
342

343
344
345
346
347
348
isc_result_t
dns_sdb_putrr(dns_sdblookup_t *lookup, const char *type, dns_ttl_t ttl,
	      const char *data)
{
	unsigned int datalen;
	dns_rdatatype_t typeval;
Mark Andrews's avatar
Mark Andrews committed
349
	isc_textregion_t r;
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
	isc_lex_t *lex = NULL;
	isc_result_t result;
	unsigned char *p = NULL;
	unsigned int size = 0; /* Init to suppress compiler warning */
	isc_mem_t *mctx;
	dns_sdbimplementation_t *imp;
	dns_name_t *origin;
	isc_buffer_t b;
	isc_buffer_t rb;

	REQUIRE(VALID_SDBLOOKUP(lookup));
	REQUIRE(type != NULL);
	REQUIRE(data != NULL);

	mctx = lookup->sdb->common.mctx;

Mark Andrews's avatar
Mark Andrews committed
366
	DE_CONST(type, r.base);
367
	r.length = strlen(type);
Mark Andrews's avatar
Mark Andrews committed
368
	result = dns_rdatatype_fromtext(&typeval, &r);
369
370
	if (result != ISC_R_SUCCESS)
		return (result);
371

372
373
374
375
376
377
	imp = lookup->sdb->implementation;
	if ((imp->flags & DNS_SDBFLAG_RELATIVERDATA) != 0)
		origin = &lookup->sdb->common.origin;
	else
		origin = dns_rootname;

378
379
380
381
	result = isc_lex_create(mctx, 64, &lex);
	if (result != ISC_R_SUCCESS)
		goto failure;

382
383
	datalen = strlen(data);
	size = initial_size(datalen);
384
	do {
385
		isc_buffer_constinit(&b, data, datalen);
386
		isc_buffer_add(&b, datalen);
387
388
389
390
		result = isc_lex_openbuffer(lex, &b);
		if (result != ISC_R_SUCCESS)
			goto failure;

391
392
		if (size >= 65535)
			size = 65535;
393
394
395
		p = isc_mem_get(mctx, size);
		if (p == NULL) {
			result = ISC_R_NOMEMORY;
396
			goto failure;
397
398
399
400
401
		}
		isc_buffer_init(&rb, p, size);
		result = dns_rdata_fromtext(NULL,
					    lookup->sdb->common.rdclass,
					    typeval, lex,
402
					    origin, 0,
403
					    mctx, &rb,
404
					    &lookup->callbacks);
405
406
407
		if (result != ISC_R_NOSPACE)
			break;

408
409
410
411
412
		/*
		 * Is the RR too big?
		 */
		if (size >= 65535)
			break;
413
414
		isc_mem_put(mctx, p, size);
		p = NULL;
415
416
417
418
419
420
		size *= 2;
	} while (result == ISC_R_NOSPACE);

	if (result != ISC_R_SUCCESS)
		goto failure;

421
422
423
	result = dns_sdb_putrdata(lookup, typeval, ttl,
				  isc_buffer_base(&rb),
				  isc_buffer_usedlength(&rb));
424
 failure:
425
426
	if (p != NULL)
		isc_mem_put(mctx, p, size);
427
428
429
430
431
432
	if (lex != NULL)
		isc_lex_destroy(&lex);

	return (result);
}

433
434
static isc_result_t
getnode(dns_sdballnodes_t *allnodes, const char *name, dns_sdbnode_t **nodep) {
435
436
437
	dns_name_t *newname, *origin;
	dns_fixedname_t fnewname;
	dns_sdb_t *sdb = (dns_sdb_t *)allnodes->common.db;
438
	dns_sdbimplementation_t *imp = sdb->implementation;
439
440
441
442
443
444
445
446
447
448
449
450
	dns_sdbnode_t *sdbnode;
	isc_mem_t *mctx = sdb->common.mctx;
	isc_buffer_t b;
	isc_result_t result;

	dns_fixedname_init(&fnewname);
	newname = dns_fixedname_name(&fnewname);

	if ((imp->flags & DNS_SDBFLAG_RELATIVERDATA) != 0)
		origin = &sdb->common.origin;
	else
		origin = dns_rootname;
451
	isc_buffer_constinit(&b, name, strlen(name));
452
453
	isc_buffer_add(&b, strlen(name));

454
	result = dns_name_fromtext(newname, &b, origin, 0, NULL);
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
	if (result != ISC_R_SUCCESS)
		return (result);

	if (allnodes->common.relative_names) {
		/* All names are relative to the root */
		unsigned int nlabels = dns_name_countlabels(newname);
		dns_name_getlabelsequence(newname, 0, nlabels - 1, newname);
	}

	sdbnode = ISC_LIST_HEAD(allnodes->nodelist);
	if (sdbnode == NULL || !dns_name_equal(sdbnode->name, newname)) {
		sdbnode = NULL;
		result = createnode(sdb, &sdbnode);
		if (result != ISC_R_SUCCESS)
			return (result);
		sdbnode->name = isc_mem_get(mctx, sizeof(dns_name_t));
		if (sdbnode->name == NULL) {
			destroynode(sdbnode);
			return (ISC_R_NOMEMORY);
		}
		dns_name_init(sdbnode->name, NULL);
		result = dns_name_dup(newname, mctx, sdbnode->name);
		if (result != ISC_R_SUCCESS) {
			isc_mem_put(mctx, sdbnode->name, sizeof(dns_name_t));
			destroynode(sdbnode);
			return (result);
		}
		ISC_LIST_PREPEND(allnodes->nodelist, sdbnode, link);
		if (allnodes->origin == NULL &&
		    dns_name_equal(newname, &sdb->common.origin))
			allnodes->origin = sdbnode;
	}
487
488
489
490
491
492
493
494
495
496
497
498
499
	*nodep = sdbnode;
	return (ISC_R_SUCCESS);
}

isc_result_t
dns_sdb_putnamedrr(dns_sdballnodes_t *allnodes, const char *name,
		   const char *type, dns_ttl_t ttl, const char *data)
{
	isc_result_t result;
	dns_sdbnode_t *sdbnode = NULL;
	result = getnode(allnodes, name, &sdbnode);
	if (result != ISC_R_SUCCESS)
		return (result);
500
	return (dns_sdb_putrr(sdbnode, type, ttl, data));
501
}
502

503
504
505
506
507
508
509
510
511
512
513
isc_result_t
dns_sdb_putnamedrdata(dns_sdballnodes_t *allnodes, const char *name,
		      dns_rdatatype_t type, dns_ttl_t ttl,
		      const void *rdata, unsigned int rdlen)
{
	isc_result_t result;
	dns_sdbnode_t *sdbnode = NULL;
	result = getnode(allnodes, name, &sdbnode);
	if (result != ISC_R_SUCCESS)
		return (result);
	return (dns_sdb_putrdata(sdbnode, type, ttl, rdata, rdlen));
514
515
}

516
517
518
519
520
521
522
523
524
525
isc_result_t
dns_sdb_putsoa(dns_sdblookup_t *lookup, const char *mname, const char *rname,
	       isc_uint32_t serial)
{
	char str[2 * DNS_NAME_MAXTEXT + 5 * (sizeof("2147483647")) + 7];
	int n;

	REQUIRE(mname != NULL);
	REQUIRE(rname != NULL);

Andreas Gustafsson's avatar
Andreas Gustafsson committed
526
	n = snprintf(str, sizeof(str), "%s %s %u %u %u %u %u",
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
		     mname, rname, serial,
		     SDB_DEFAULT_REFRESH, SDB_DEFAULT_RETRY,
		     SDB_DEFAULT_EXPIRE, SDB_DEFAULT_MINIMUM);
	if (n >= (int)sizeof(str) || n < 0)
		return (ISC_R_NOSPACE);
	return (dns_sdb_putrr(lookup, "SOA", SDB_DEFAULT_TTL, str));
}

/*
 * DB routines
 */

static void
attach(dns_db_t *source, dns_db_t **targetp) {
	dns_sdb_t *sdb = (dns_sdb_t *) source;

	REQUIRE(VALID_SDB(sdb));

	LOCK(&sdb->lock);
	REQUIRE(sdb->references > 0);
	sdb->references++;
	UNLOCK(&sdb->lock);

	*targetp = source;
}

static void
destroy(dns_sdb_t *sdb) {
	isc_mem_t *mctx;
556
	dns_sdbimplementation_t *imp = sdb->implementation;
557
558
559

	mctx = sdb->common.mctx;

560
561
	if (imp->methods->destroy != NULL) {
		MAYBE_LOCK(sdb);
562
563
		imp->methods->destroy(sdb->zone, imp->driverdata,
				      &sdb->dbdata);
564
565
		MAYBE_UNLOCK(sdb);
	}
566
567

	isc_mem_free(mctx, sdb->zone);
568
	DESTROYLOCK(&sdb->lock);
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

	sdb->common.magic = 0;
	sdb->common.impmagic = 0;

	dns_name_free(&sdb->common.origin, mctx);

	isc_mem_put(mctx, sdb, sizeof(dns_sdb_t));
	isc_mem_detach(&mctx);
}

static void
detach(dns_db_t **dbp) {
	dns_sdb_t *sdb = (dns_sdb_t *)(*dbp);
	isc_boolean_t need_destroy = ISC_FALSE;

	REQUIRE(VALID_SDB(sdb));
	LOCK(&sdb->lock);
	REQUIRE(sdb->references > 0);
	sdb->references--;
	if (sdb->references == 0)
		need_destroy = ISC_TRUE;
	UNLOCK(&sdb->lock);

	if (need_destroy)
		destroy(sdb);

	*dbp = NULL;
}

static isc_result_t
599
beginload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
600
	UNUSED(db);
601
	UNUSED(callbacks);
602
603
604
605
	return (ISC_R_NOTIMPLEMENTED);
}

static isc_result_t
606
endload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
607
	UNUSED(db);
608
	UNUSED(callbacks);
609
610
611
612
	return (ISC_R_NOTIMPLEMENTED);
}

static isc_result_t
613
614
dump(dns_db_t *db, dns_dbversion_t *version, const char *filename,
     dns_masterformat_t masterformat) {
615
616
617
	UNUSED(db);
	UNUSED(version);
	UNUSED(filename);
618
	UNUSED(masterformat);
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
	return (ISC_R_NOTIMPLEMENTED);
}

static void
currentversion(dns_db_t *db, dns_dbversion_t **versionp) {
	REQUIRE(versionp != NULL && *versionp == NULL);

	UNUSED(db);

	*versionp = (void *) &dummy;
	return;
}

static isc_result_t
newversion(dns_db_t *db, dns_dbversion_t **versionp) {
	UNUSED(db);
	UNUSED(versionp);

	return (ISC_R_NOTIMPLEMENTED);
}

static void
Automatic Updater's avatar
Automatic Updater committed
641
attachversion(dns_db_t *db, dns_dbversion_t *source,
642
643
644
	      dns_dbversion_t **targetp)
{
	REQUIRE(source != NULL && source == (void *) &dummy);
645
	REQUIRE(targetp != NULL && *targetp == NULL);
646
647

	UNUSED(db);
648
	*targetp = source;
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
	return;
}

static void
closeversion(dns_db_t *db, dns_dbversion_t **versionp, isc_boolean_t commit) {
	REQUIRE(versionp != NULL && *versionp == (void *) &dummy);
	REQUIRE(commit == ISC_FALSE);

	UNUSED(db);
	UNUSED(commit);

	*versionp = NULL;
}

static isc_result_t
createnode(dns_sdb_t *sdb, dns_sdbnode_t **nodep) {
	dns_sdbnode_t *node;
	isc_result_t result;

	node = isc_mem_get(sdb->common.mctx, sizeof(dns_sdbnode_t));
	if (node == NULL)
		return (ISC_R_NOMEMORY);

	node->sdb = NULL;
	attach((dns_db_t *)sdb, (dns_db_t **)&node->sdb);
	ISC_LIST_INIT(node->lists);
	ISC_LIST_INIT(node->buffers);
676
677
	ISC_LINK_INIT(node, link);
	node->name = NULL;
678
679
680
	result = isc_mutex_init(&node->lock);
	if (result != ISC_R_SUCCESS) {
		isc_mem_put(sdb->common.mctx, node, sizeof(dns_sdbnode_t));
681
		return (result);
682
	}
683
	dns_rdatacallbacks_init(&node->callbacks);
684
685
686
687
688
689
690
691
692
693
694
695
696
	node->references = 1;
	node->magic = SDBLOOKUP_MAGIC;

	*nodep = node;
	return (ISC_R_SUCCESS);
}

static void
destroynode(dns_sdbnode_t *node) {
	dns_rdatalist_t *list;
	dns_rdata_t *rdata;
	isc_buffer_t *b;
	dns_sdb_t *sdb;
697
698
699
700
	isc_mem_t *mctx;

	sdb = node->sdb;
	mctx = sdb->common.mctx;
701
702
703
704
705
706

	while (!ISC_LIST_EMPTY(node->lists)) {
		list = ISC_LIST_HEAD(node->lists);
		while (!ISC_LIST_EMPTY(list->rdata)) {
			rdata = ISC_LIST_HEAD(list->rdata);
			ISC_LIST_UNLINK(list->rdata, rdata, link);
707
			isc_mem_put(mctx, rdata, sizeof(dns_rdata_t));
708
709
		}
		ISC_LIST_UNLINK(node->lists, list, link);
710
		isc_mem_put(mctx, list, sizeof(dns_rdatalist_t));
711
712
713
714
715
716
717
718
	}

	while (!ISC_LIST_EMPTY(node->buffers)) {
		b = ISC_LIST_HEAD(node->buffers);
		ISC_LIST_UNLINK(node->buffers, b, link);
		isc_buffer_free(&b);
	}

719
720
721
722
	if (node->name != NULL) {
		dns_name_free(node->name, mctx);
		isc_mem_put(mctx, node->name, sizeof(dns_name_t));
	}
723
	DESTROYLOCK(&node->lock);
724
	node->magic = 0;
725
	isc_mem_put(mctx, node, sizeof(dns_sdbnode_t));
Mark Andrews's avatar
Mark Andrews committed
726
	detach((dns_db_t **) (void *)&sdb);
727
728
729
}

static isc_result_t
730
731
732
findnodeext(dns_db_t *db, dns_name_t *name, isc_boolean_t create,
	    dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo,
	    dns_dbnode_t **nodep)
733
734
735
736
737
738
739
{
	dns_sdb_t *sdb = (dns_sdb_t *)db;
	dns_sdbnode_t *node = NULL;
	isc_result_t result;
	isc_buffer_t b;
	char namestr[DNS_NAME_MAXTEXT + 1];
	isc_boolean_t isorigin;
740
	dns_sdbimplementation_t *imp;
741
742
	dns_name_t relname;
	unsigned int labels;
743
744
745
746
747
748

	REQUIRE(VALID_SDB(sdb));
	REQUIRE(create == ISC_FALSE);
	REQUIRE(nodep != NULL && *nodep == NULL);

	UNUSED(name);
749
	UNUSED(create);
750

751
752
	imp = sdb->implementation;

753
	isorigin = dns_name_equal(name, &sdb->common.origin);
754

755
756
757
758
759
760
761
762
	if (imp->methods->lookup2 != NULL) {
		if ((imp->flags & DNS_SDBFLAG_RELATIVEOWNER) != 0) {
			labels = dns_name_countlabels(name) -
				 dns_name_countlabels(&db->origin);
			dns_name_init(&relname, NULL);
			dns_name_getlabelsequence(name, 0, labels, &relname);
			name = &relname;
		}
763
	} else {
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
		isc_buffer_init(&b, namestr, sizeof(namestr));
		if ((imp->flags & DNS_SDBFLAG_RELATIVEOWNER) != 0) {

			labels = dns_name_countlabels(name) -
				 dns_name_countlabels(&db->origin);
			dns_name_init(&relname, NULL);
			dns_name_getlabelsequence(name, 0, labels, &relname);
			result = dns_name_totext(&relname, ISC_TRUE, &b);
			if (result != ISC_R_SUCCESS)
				return (result);
		} else {
			result = dns_name_totext(name, ISC_TRUE, &b);
			if (result != ISC_R_SUCCESS)
				return (result);
		}
		isc_buffer_putuint8(&b, 0);
780
	}
781
782
783
784
785

	result = createnode(sdb, &node);
	if (result != ISC_R_SUCCESS)
		return (result);

786
	MAYBE_LOCK(sdb);
787
788
789
790
	if (imp->methods->lookup2 != NULL)
		result = imp->methods->lookup2(&sdb->common.origin, name,
					       sdb->dbdata, node, methods,
					       clientinfo);
Tinderbox User's avatar
Tinderbox User committed
791
	else
792
793
		result = imp->methods->lookup(sdb->zone, namestr, sdb->dbdata,
					      node, methods, clientinfo);
794
	MAYBE_UNLOCK(sdb);
795
796
797
798
	if (result != ISC_R_SUCCESS &&
	    !(result == ISC_R_NOTFOUND &&
	      isorigin && imp->methods->authority != NULL))
	{
799
800
801
802
		destroynode(node);
		return (result);
	}

803
	if (isorigin && imp->methods->authority != NULL) {
804
		MAYBE_LOCK(sdb);
805
		result = imp->methods->authority(sdb->zone, sdb->dbdata, node);
806
		MAYBE_UNLOCK(sdb);
807
808
809
810
811
		if (result != ISC_R_SUCCESS) {
			destroynode(node);
			return (result);
		}
	}
Automatic Updater's avatar
Automatic Updater committed
812

813
814
815
816
817
	*nodep = node;
	return (ISC_R_SUCCESS);
}

static isc_result_t
818
819
820
821
822
findext(dns_db_t *db, dns_name_t *name, dns_dbversion_t *version,
	dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
	dns_dbnode_t **nodep, dns_name_t *foundname,
	dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo,
	dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
823
824
825
826
827
828
829
830
831
{
	dns_sdb_t *sdb = (dns_sdb_t *)db;
	dns_dbnode_t *node = NULL;
	dns_fixedname_t fname;
	dns_rdataset_t xrdataset;
	dns_name_t *xname;
	unsigned int nlabels, olabels;
	isc_result_t result;
	unsigned int i;
832
	unsigned int flags;
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848

	REQUIRE(VALID_SDB(sdb));
	REQUIRE(nodep == NULL || *nodep == NULL);
	REQUIRE(version == NULL || version == (void *) &dummy);

	UNUSED(options);

	if (!dns_name_issubdomain(name, &db->origin))
		return (DNS_R_NXDOMAIN);

	olabels = dns_name_countlabels(&db->origin);
	nlabels = dns_name_countlabels(name);

	dns_fixedname_init(&fname);
	xname = dns_fixedname_name(&fname);

849
850
	if (rdataset == NULL) {
		dns_rdataset_init(&xrdataset);
851
		rdataset = &xrdataset;
852
	}
853
854

	result = DNS_R_NXDOMAIN;
855
856
857
	flags = sdb->implementation->flags;
	i = (flags & DNS_SDBFLAG_DNS64) != 0 ? nlabels : olabels;
	for (; i <= nlabels; i++) {
858
859
860
861
		/*
		 * Look up the next label.
		 */
		dns_name_getlabelsequence(name, nlabels - i, i, xname);
862
863
		result = findnodeext(db, xname, ISC_FALSE, methods,
				     clientinfo, &node);
864
865
866
867
868
869
		if (result == ISC_R_NOTFOUND) {
			/*
			 * No data at zone apex?
			 */
			if (i == olabels)
				return (DNS_R_BADDB);
870
871
872
			result = DNS_R_NXDOMAIN;
			continue;
		}
873
874
		if (result != ISC_R_SUCCESS)
			return (result);
875

876
877
878
879
880
881
882
883
884
885
886
887
		/*
		 * DNS64 zone's don't have DNAME or NS records.
		 */
		if ((flags & DNS_SDBFLAG_DNS64) != 0)
			goto skip;

		/*
		 * DNS64 zone's don't have DNAME or NS records.
		 */
		if ((flags & DNS_SDBFLAG_DNS64) != 0)
			goto skip;

888
889
890
891
892
893
894
895
896
		/*
		 * Look for a DNAME at the current label, unless this is
		 * the qname.
		 */
		if (i < nlabels) {
			result = findrdataset(db, node, version,
					      dns_rdatatype_dname,
					      0, now, rdataset, sigrdataset);
			if (result == ISC_R_SUCCESS) {
897
				result = DNS_R_DNAME;
898
899
900
901
902
903
				break;
			}
		}

		/*
		 * Look for an NS at the current label, unless this is the
904
		 * origin or glue is ok.
905
		 */
906
		if (i != olabels && (options & DNS_DBFIND_GLUEOK) == 0) {
907
908
909
910
			result = findrdataset(db, node, version,
					      dns_rdatatype_ns,
					      0, now, rdataset, sigrdataset);
			if (result == ISC_R_SUCCESS) {
911
912
913
914
				if (i == nlabels && type == dns_rdatatype_any)
				{
					result = DNS_R_ZONECUT;
					dns_rdataset_disassociate(rdataset);
915
916
					if (sigrdataset != NULL &&
					    dns_rdataset_isassociated
Automatic Updater's avatar
Automatic Updater committed
917
							(sigrdataset)) {
918
919
						dns_rdataset_disassociate
								(sigrdataset);
920
					}
921
				} else
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
					result = DNS_R_DELEGATION;
				break;
			}
		}

		/*
		 * If the current name is not the qname, add another label
		 * and try again.
		 */
		if (i < nlabels) {
			destroynode(node);
			node = NULL;
			continue;
		}

937
 skip:
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
		/*
		 * If we're looking for ANY, we're done.
		 */
		if (type == dns_rdatatype_any) {
			result = ISC_R_SUCCESS;
			break;
		}

		/*
		 * Look for the qtype.
		 */
		result = findrdataset(db, node, version, type,
				      0, now, rdataset, sigrdataset);
		if (result == ISC_R_SUCCESS)
			break;

		/*
		 * Look for a CNAME
		 */
		if (type != dns_rdatatype_cname) {
			result = findrdataset(db, node, version,
					      dns_rdatatype_cname,
					      0, now, rdataset, sigrdataset);
			if (result == ISC_R_SUCCESS) {
				result = DNS_R_CNAME;
				break;
			}
		}

		result = DNS_R_NXRRSET;
		break;
	}

	if (rdataset == &xrdataset && dns_rdataset_isassociated(rdataset))
		dns_rdataset_disassociate(rdataset);

	if (foundname != NULL) {
		isc_result_t xresult;

977
		xresult = dns_name_copy(xname, foundname, NULL);
978
		if (xresult != ISC_R_SUCCESS) {
979
980
			if (node != NULL)
				destroynode(node);
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
			if (dns_rdataset_isassociated(rdataset))
				dns_rdataset_disassociate(rdataset);
			return (DNS_R_BADDB);
		}
	}

	if (nodep != NULL)
		*nodep = node;
	else if (node != NULL)
		detachnode(db, &node);

	return (result);
}

static isc_result_t
findzonecut(dns_db_t *db, dns_name_t *name, unsigned int options,
	    isc_stdtime_t now, dns_dbnode_t **nodep, dns_name_t *foundname,
	    dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
{
	UNUSED(db);
	UNUSED(name);
	UNUSED(options);
	UNUSED(now);
	UNUSED(nodep);
	UNUSED(foundname);
	UNUSED(rdataset);
	UNUSED(sigrdataset);

	return (ISC_R_NOTIMPLEMENTED);
}

static void
attachnode(dns_db_t *db, dns_dbnode_t *source, dns_dbnode_t **targetp) {
	dns_sdb_t *sdb = (dns_sdb_t *)db;
	dns_sdbnode_t *node = (dns_sdbnode_t *)source;

	REQUIRE(VALID_SDB(sdb));
1018
1019
1020

	UNUSED(sdb);

1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
	LOCK(&node->lock);
	INSIST(node->references > 0);
	node->references++;
	INSIST(node->references != 0);		/* Catch overflow. */
	UNLOCK(&node->lock);

	*targetp = source;
}

static void
detachnode(dns_db_t *db, dns_dbnode_t **targetp) {
	dns_sdb_t *sdb = (dns_sdb_t *)db;
	dns_sdbnode_t *node;
	isc_boolean_t need_destroy = ISC_FALSE;

	REQUIRE(VALID_SDB(sdb));
	REQUIRE(targetp != NULL && *targetp != NULL);

1039
1040
	UNUSED(sdb);

1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
	node = (dns_sdbnode_t *)(*targetp);

	LOCK(&node->lock);
	INSIST(node->references > 0);
	node->references--;
	if (node->references == 0)
		need_destroy = ISC_TRUE;
	UNLOCK(&node->lock);

	if (need_destroy)
		destroynode(node);

	*targetp = NULL;
}

static isc_result_t
expirenode(dns_db_t *db, dns_dbnode_t *node, isc_stdtime_t now) {
	UNUSED(db);
	UNUSED(node);
	UNUSED(now);
	INSIST(0);
	return (ISC_R_UNEXPECTED);
}

static void
printnode(dns_db_t *db, dns_dbnode_t *node, FILE *out) {
	UNUSED(db);
	UNUSED(node);
	UNUSED(out);
	return;
}

static isc_result_t
1074
createiterator(dns_db_t *db, unsigned int options, dns_dbiterator_t **iteratorp)
1075
{
1076
1077
	dns_sdb_t *sdb = (dns_sdb_t *)db;
	sdb_dbiterator_t *sdbiter;
1078
	dns_sdbimplementation_t *imp = sdb->implementation;
1079
1080
1081
1082
1083
1084
1085
	isc_result_t result;

	REQUIRE(VALID_SDB(sdb));

	if (imp->methods->allnodes == NULL)
		return (ISC_R_NOTIMPLEMENTED);

1086
1087
	if ((options & DNS_DB_NSEC3ONLY) != 0 ||
	    (options & DNS_DB_NONSEC3) != 0)
Automatic Updater's avatar
Automatic Updater committed
1088
		return (ISC_R_NOTIMPLEMENTED);
1089

1090
1091
1092
1093
1094
1095
1096
	sdbiter = isc_mem_get(sdb->common.mctx, sizeof(sdb_dbiterator_t));
	if (sdbiter == NULL)
		return (ISC_R_NOMEMORY);

	sdbiter->common.methods = &dbiterator_methods;
	sdbiter->common.db = NULL;
	dns_db_attach(db, &sdbiter->common.db);
1097
	sdbiter->common.relative_names = ISC_TF(options & DNS_DB_RELATIVENAMES);
1098
1099
1100
1101
1102
	sdbiter->common.magic = DNS_DBITERATOR_MAGIC;
	ISC_LIST_INIT(sdbiter->nodelist);
	sdbiter->current = NULL;
	sdbiter->origin = NULL;

1103
	MAYBE_LOCK(sdb);
1104
	result = imp->methods->allnodes(sdb->zone, sdb->dbdata, sdbiter);
1105
	MAYBE_UNLOCK(sdb);
1106
	if (result != ISC_R_SUCCESS) {
Mark Andrews's avatar
Mark Andrews committed
1107
		dbiterator_destroy((dns_dbiterator_t **) (void *)&sdbiter);
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
		return (result);
	}

	if (sdbiter->origin != NULL) {
		ISC_LIST_UNLINK(sdbiter->nodelist, sdbiter->origin, link);
		ISC_LIST_PREPEND(sdbiter->nodelist, sdbiter->origin, link);
	}

	*iteratorp = (dns_dbiterator_t *)sdbiter;

	return (ISC_R_SUCCESS);
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
}

static isc_result_t
findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
	     dns_rdatatype_t type, dns_rdatatype_t covers,
	     isc_stdtime_t now, dns_rdataset_t *rdataset,
	     dns_rdataset_t *sigrdataset)
{
	dns_rdatalist_t *list;
	dns_sdbnode_t *sdbnode = (dns_sdbnode_t *)node;

	REQUIRE(VALID_SDBNODE(node));

	UNUSED(db);
	UNUSED(version);
	UNUSED(covers);
	UNUSED(now);
	UNUSED(sigrdataset);

1138
	if (type == dns_rdatatype_rrsig)
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
		return (ISC_R_NOTIMPLEMENTED);

	list = ISC_LIST_HEAD(sdbnode->lists);
	while (list != NULL) {
		if (list->type == type)
			break;
		list = ISC_LIST_NEXT(list, link);
	}
	if (list == NULL)
		return (ISC_R_NOTFOUND);

	list_tordataset(list, db, node, rdataset);

	return (ISC_R_SUCCESS);
}

static isc_result_t
allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
	     isc_stdtime_t now, dns_rdatasetiter_t **iteratorp)
{
	sdb_rdatasetiter_t *iterator;

	REQUIRE(version == NULL || version == &dummy);
Automatic Updater's avatar
Automatic Updater committed
1162

1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
	UNUSED(version);
	UNUSED(now);

	iterator = isc_mem_get(db->mctx, sizeof(sdb_rdatasetiter_t));
	if (iterator == NULL)
		return (ISC_R_NOMEMORY);

	iterator->common.magic = DNS_RDATASETITER_MAGIC;
	iterator->common.methods = &rdatasetiter_methods;
	iterator->common.db = db;
	iterator->common.node = NULL;
	attachnode(db, node, &iterator->common.node);
	iterator->common.version = version;
	iterator->common.now = now;

	*iteratorp = (dns_rdatasetiter_t *)iterator;

	return (ISC_R_SUCCESS);
}

static isc_result_t
addrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
	    isc_stdtime_t now, dns_rdataset_t *rdataset, unsigned int options,
	    dns_rdataset_t *addedrdataset)
{
	UNUSED(db);
	UNUSED(node);
	UNUSED(version);
	UNUSED(now);
	UNUSED(rdataset);
	UNUSED(options);
	UNUSED(addedrdataset);

	return (ISC_R_NOTIMPLEMENTED);
}

static isc_result_t
subtractrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
1201
		 dns_rdataset_t *rdataset, unsigned int options,
1202
		 dns_rdataset_t *newrdataset)
1203
1204
1205
1206
1207
{
	UNUSED(db);
	UNUSED(node);
	UNUSED(version);
	UNUSED(rdataset);
1208
	UNUSED(options);
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
	UNUSED(newrdataset);

	return (ISC_R_NOTIMPLEMENTED);
}

static isc_result_t
deleterdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
	       dns_rdatatype_t type, dns_rdatatype_t covers)
{
	UNUSED(db);
	UNUSED(node);
	UNUSED(version);
	UNUSED(type);
	UNUSED(covers);

	return (ISC_R_NOTIMPLEMENTED);
}

static isc_boolean_t
issecure(dns_db_t *db) {
	UNUSED(db);

	return (ISC_FALSE);
}

static unsigned int
nodecount(dns_db_t *db) {
	UNUSED(db);

	return (0);
}

static isc_boolean_t
ispersistent(dns_db_t *db) {
	UNUSED(db);
	return (ISC_TRUE);
}

Mark Andrews's avatar
Mark Andrews committed
1247
1248
1249
1250
1251
1252
static void
overmem(dns_db_t *db, isc_boolean_t overmem) {
	UNUSED(db);
	UNUSED(overmem);
}

1253
1254
1255
1256
1257
1258
static void
settask(dns_db_t *db, isc_task_t *task) {
	UNUSED(db);
	UNUSED(task);
}

1259
1260
1261
1262
1263
1264

static dns_dbmethods_t sdb_methods = {
	attach,
	detach,
	beginload,
	endload,
1265
	NULL,
1266
1267
1268
1269
1270
	dump,
	currentversion,
	newversion,
	attachversion,
	closeversion,
1271
1272
	NULL,
	NULL,
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
	findzonecut,
	attachnode,
	detachnode,
	expirenode,
	printnode,
	createiterator,
	findrdataset,
	allrdatasets,
	addrdataset,
	subtractrdataset,
	deleterdataset,
	issecure,
	nodecount,
Mark Andrews's avatar
Mark Andrews committed
1286
	ispersistent,
1287
	overmem,
1288
	settask,
1289
1290
1291
1292
1293
1294
1295
1296
1297
	NULL,			/* getoriginnode */
	NULL,			/* transfernode */
	NULL,			/* getnsec3parameters */
	NULL,			/* findnsec3node */
	NULL,			/* setsigningtime */
	NULL,			/* getsigningtime */
	NULL,			/* resigned */
	NULL,			/* isdnssec */
	NULL,			/* getrrsetstats */
1298
1299
	NULL,			/* rpz_attach */
	NULL,			/* rpz_ready */
1300
	findnodeext,
1301
1302
1303
	findext,
	NULL,			/* setcachestats */
	NULL			/* hashsize */
1304
1305
};

Brian Wellington's avatar
Brian Wellington committed
1306
static isc_result_t
1307
1308
dns_sdb_create(isc_mem_t *mctx, dns_name_t *origin, dns_dbtype_t type,
	       dns_rdataclass_t rdclass, unsigned int argc, char *argv[],
1309
	       void *driverarg, dns_db_t **dbp)
1310
1311
1312
1313
1314
{
	dns_sdb_t *sdb;
	isc_result_t result;
	char zonestr[DNS_NAME_MAXTEXT + 1];
	isc_buffer_t b;
1315
	dns_sdbimplementation_t *imp;
1316

1317
	REQUIRE(driverarg != NULL);
1318

1319
	imp = driverarg;
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338

	if (type != dns_dbtype_zone)
		return (ISC_R_NOTIMPLEMENTED);

	sdb = isc_mem_get(mctx, sizeof(dns_sdb_t));
	if (sdb == NULL)
		return (ISC_R_NOMEMORY);
	memset(sdb, 0, sizeof(dns_sdb_t));

	dns_name_init(&sdb->common.origin, NULL);
	sdb->common.attributes = 0;
	sdb->common.methods = &sdb_methods;
	sdb->common.rdclass = rdclass;
	sdb->common.mctx = NULL;
	sdb->implementation = imp;

	isc_mem_attach(mctx, &sdb->common.mctx);

	result = isc_mutex_init(&sdb->lock);
1339
	if (result != ISC_R_SUCCESS)
1340
		goto cleanup_mctx;
1341

1342
1343
	result = dns_name_dupwithoffsets(origin, mctx, &sdb->common.origin);
	if (result != ISC_R_SUCCESS)
1344
		goto cleanup_lock;
1345

1346
1347
	isc_buffer_init(&b, zonestr, sizeof(zonestr));
	result = dns_name_totext(origin, ISC_TRUE, &b);
1348
1349
	if (result != ISC_R_SUCCESS)
		goto cleanup_origin;
1350
	isc_buffer_putuint8(&b, 0);
1351

1352
1353
	sdb->zone = isc_mem_strdup(mctx, zonestr);
	if (sdb->zone == NULL) {
1354
1355
		result = ISC_R_NOMEMORY;
		goto cleanup_origin;
1356
	}
1357

1358
	sdb->dbdata = NULL;
1359
	if (imp->methods->create != NULL) {
1360
		MAYBE_LOCK(sdb);
1361
		result = imp->methods->create(sdb->zone, argc, argv,
1362
					      imp->driverdata, &sdb->dbdata);
1363
		MAYBE_UNLOCK(sdb);
1364
1365
		if (result != ISC_R_SUCCESS)
			goto cleanup_zonestr;
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
	}

	sdb->references = 1;

	sdb->common.magic = DNS_DB_MAGIC;
	sdb->common.impmagic = SDB_MAGIC;

	*dbp = (dns_db_t *)sdb;

	return (ISC_R_SUCCESS);
1376
1377
1378
1379
1380
1381

 cleanup_zonestr:
	isc_mem_free(mctx, sdb->zone);
 cleanup_origin:
	dns_name_free(&sdb->common.origin, mctx);
 cleanup_lock:
Francis Dupont's avatar
Francis Dupont committed
1382
	(void)isc_mutex_destroy(&sdb->lock);
1383
1384
1385
1386
1387
 cleanup_mctx:
	isc_mem_put(mctx, sdb, sizeof(dns_sdb_t));
	isc_mem_detach(&mctx);

	return (result);
1388
1389
1390
1391
1392
1393
1394
1395
1396
}


/*
 * Rdataset Methods
 */

static void
disassociate(dns_rdataset_t *rdataset) {
Brian Wellington's avatar
Brian Wellington committed
1397
	dns_dbnode_t *node = rdataset->private5;
1398
1399
1400
1401
1402
1403
1404
	dns_sdbnode_t *sdbnode = (dns_sdbnode_t *) node;
	dns_db_t *db = (dns_db_t *) sdbnode->sdb;

	detachnode(db, &node);
	isc__rdatalist_disassociate(rdataset);
}

1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
static void
rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
	dns_dbnode_t *node = source->private5;
	dns_sdbnode_t *sdbnode = (dns_sdbnode_t *) node;
	dns_db_t *db = (dns_db_t *) sdbnode->sdb;
	dns_dbnode_t *tempdb = NULL;

	isc__rdatalist_clone(source, target);
	attachnode(db, node, &tempdb);
	source->private5 = tempdb;
}

1417
1418
1419
1420
1421
static dns_rdatasetmethods_t methods = {
	disassociate,
	isc__rdatalist_first,
	isc__rdatalist_next,