db.c 26.3 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
2
 * Copyright (C) 1999-2001, 2003-2005, 2007-2009, 2011-2013, 2015-2017  Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Bob Halley's avatar
Bob Halley committed
7 8
 */

9
/*! \file */
David Lawrence's avatar
David Lawrence committed
10

11 12 13 14
/***
 *** Imports
 ***/

Bob Halley's avatar
Bob Halley committed
15 16
#include <config.h>

17
#include <isc/buffer.h>
18 19 20
#include <isc/mem.h>
#include <isc/once.h>
#include <isc/rwlock.h>
21
#include <isc/string.h>
Bob Halley's avatar
Bob Halley committed
22
#include <isc/util.h>
Bob Halley's avatar
Bob Halley committed
23

24
#include <dns/callbacks.h>
25
#include <dns/clientinfo.h>
26
#include <dns/db.h>
27
#include <dns/dbiterator.h>
28
#include <dns/log.h>
Bob Halley's avatar
Bob Halley committed
29
#include <dns/master.h>
30
#include <dns/rdata.h>
31
#include <dns/rdataset.h>
32
#include <dns/rdatasetiter.h>
33
#include <dns/result.h>
34 35 36 37 38

/***
 *** Private Types
 ***/

39 40 41 42 43 44 45
struct dns_dbimplementation {
	const char *				name;
	dns_dbcreatefunc_t			create;
	isc_mem_t *				mctx;
	void *					driverarg;
	ISC_LINK(dns_dbimplementation_t)	link;
};
46 47 48 49 50 51

/***
 *** Supported DB Implementations Registry
 ***/

/*
52
 * Built in database implementations are registered here.
53
 */
Bob Halley's avatar
Bob Halley committed
54 55

#include "rbtdb.h"
56
#include "rbtdb64.h"
Bob Halley's avatar
Bob Halley committed
57

58 59 60 61 62 63 64 65
static ISC_LIST(dns_dbimplementation_t) implementations;
static isc_rwlock_t implock;
static isc_once_t once = ISC_ONCE_INIT;

static dns_dbimplementation_t rbtimp;
static dns_dbimplementation_t rbt64imp;

static void
66
initialize(void) {
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	RUNTIME_CHECK(isc_rwlock_init(&implock, 0, 0) == ISC_R_SUCCESS);

	rbtimp.name = "rbt";
	rbtimp.create = dns_rbtdb_create;
	rbtimp.mctx = NULL;
	rbtimp.driverarg = NULL;
	ISC_LINK_INIT(&rbtimp, link);

	rbt64imp.name = "rbt64";
	rbt64imp.create = dns_rbtdb64_create;
	rbt64imp.mctx = NULL;
	rbt64imp.driverarg = NULL;
	ISC_LINK_INIT(&rbt64imp, link);

	ISC_LIST_INIT(implementations);
	ISC_LIST_APPEND(implementations, &rbtimp, link);
	ISC_LIST_APPEND(implementations, &rbt64imp, link);
}

static inline dns_dbimplementation_t *
impfind(const char *name) {
	dns_dbimplementation_t *imp;

Automatic Updater's avatar
Automatic Updater committed
90
	for (imp = ISC_LIST_HEAD(implementations);
91 92 93 94 95 96 97
	     imp != NULL;
	     imp = ISC_LIST_NEXT(imp, link))
		if (strcasecmp(name, imp->name) == 0)
			return (imp);
	return (NULL);
}

98 99 100 101 102

/***
 *** Basic DB Methods
 ***/

103
isc_result_t
104
dns_db_create(isc_mem_t *mctx, const char *db_type, const dns_name_t *origin,
105
	      dns_dbtype_t type, dns_rdataclass_t rdclass,
Bob Halley's avatar
Bob Halley committed
106 107
	      unsigned int argc, char *argv[], dns_db_t **dbp)
{
108 109 110
	dns_dbimplementation_t *impinfo;

	RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
111 112 113 114 115 116 117

	/*
	 * Create a new database using implementation 'db_type'.
	 */

	REQUIRE(dbp != NULL && *dbp == NULL);
	REQUIRE(dns_name_isabsolute(origin));
Bob Halley's avatar
Bob Halley committed
118

119 120 121 122 123 124 125 126 127 128 129 130
	RWLOCK(&implock, isc_rwlocktype_read);
	impinfo = impfind(db_type);
	if (impinfo != NULL) {
		isc_result_t result;
		result = ((impinfo->create)(mctx, origin, type,
					    rdclass, argc, argv,
					    impinfo->driverarg, dbp));
		RWUNLOCK(&implock, isc_rwlocktype_read);
		return (result);
	}

	RWUNLOCK(&implock, isc_rwlocktype_read);
Bob Halley's avatar
Bob Halley committed
131

132 133 134 135
	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
		      DNS_LOGMODULE_DB, ISC_LOG_ERROR,
		      "unsupported database type '%s'", db_type);

136
	return (ISC_R_NOTFOUND);
Bob Halley's avatar
Bob Halley committed
137 138 139 140 141
}

void
dns_db_attach(dns_db_t *source, dns_db_t **targetp) {

142 143 144 145
	/*
	 * Attach *targetp to source.
	 */

Bob Halley's avatar
Bob Halley committed
146
	REQUIRE(DNS_DB_VALID(source));
147
	REQUIRE(targetp != NULL && *targetp == NULL);
Bob Halley's avatar
Bob Halley committed
148 149

	(source->methods->attach)(source, targetp);
150 151

	ENSURE(*targetp == source);
Bob Halley's avatar
Bob Halley committed
152 153 154 155 156 157
}

void
dns_db_detach(dns_db_t **dbp) {

	/*
158
	 * Detach *dbp from its database.
Bob Halley's avatar
Bob Halley committed
159 160 161 162 163 164
	 */

	REQUIRE(dbp != NULL);
	REQUIRE(DNS_DB_VALID(*dbp));

	((*dbp)->methods->detach)(dbp);
165 166

	ENSURE(*dbp == NULL);
Bob Halley's avatar
Bob Halley committed
167 168
}

169 170 171 172 173 174 175 176 177
isc_result_t
dns_db_ondestroy(dns_db_t *db, isc_task_t *task, isc_event_t **eventp)
{
	REQUIRE(DNS_DB_VALID(db));

	return (isc_ondestroy_register(&db->ondest, task, eventp));
}


Bob Halley's avatar
Bob Halley committed
178 179
isc_boolean_t
dns_db_iscache(dns_db_t *db) {
180 181 182 183

	/*
	 * Does 'db' have cache semantics?
	 */
184

Bob Halley's avatar
Bob Halley committed
185 186
	REQUIRE(DNS_DB_VALID(db));

187 188 189 190
	if ((db->attributes & DNS_DBATTR_CACHE) != 0)
		return (ISC_TRUE);

	return (ISC_FALSE);
Bob Halley's avatar
Bob Halley committed
191 192 193 194
}

isc_boolean_t
dns_db_iszone(dns_db_t *db) {
195 196 197 198

	/*
	 * Does 'db' have zone semantics?
	 */
199

Bob Halley's avatar
Bob Halley committed
200 201
	REQUIRE(DNS_DB_VALID(db));

202 203 204 205 206 207 208 209 210 211 212 213
	if ((db->attributes & (DNS_DBATTR_CACHE|DNS_DBATTR_STUB)) == 0)
		return (ISC_TRUE);

	return (ISC_FALSE);
}

isc_boolean_t
dns_db_isstub(dns_db_t *db) {

	/*
	 * Does 'db' have stub semantics?
	 */
214

215 216 217
	REQUIRE(DNS_DB_VALID(db));

	if ((db->attributes & DNS_DBATTR_STUB) != 0)
218 219 220
		return (ISC_TRUE);

	return (ISC_FALSE);
Bob Halley's avatar
Bob Halley committed
221 222
}

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
isc_boolean_t
dns_db_isdnssec(dns_db_t *db) {

	/*
	 * Is 'db' secure or partially secure?
	 */

	REQUIRE(DNS_DB_VALID(db));
	REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);

	if (db->methods->isdnssec != NULL)
		return ((db->methods->isdnssec)(db));
	return ((db->methods->issecure)(db));
}

Bob Halley's avatar
Bob Halley committed
238 239 240 241 242 243
isc_boolean_t
dns_db_issecure(dns_db_t *db) {

	/*
	 * Is 'db' secure?
	 */
244

Bob Halley's avatar
Bob Halley committed
245 246 247 248 249 250
	REQUIRE(DNS_DB_VALID(db));
	REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);

	return ((db->methods->issecure)(db));
}

251 252 253 254 255 256 257 258 259 260 261 262
isc_boolean_t
dns_db_ispersistent(dns_db_t *db) {

	/*
	 * Is 'db' persistent?
	 */

	REQUIRE(DNS_DB_VALID(db));

	return ((db->methods->ispersistent)(db));
}

Bob Halley's avatar
Bob Halley committed
263 264
dns_name_t *
dns_db_origin(dns_db_t *db) {
265 266 267 268
	/*
	 * The origin of the database.
	 */

Bob Halley's avatar
Bob Halley committed
269 270
	REQUIRE(DNS_DB_VALID(db));

271
	return (&db->origin);
Bob Halley's avatar
Bob Halley committed
272 273
}

Bob Halley's avatar
Bob Halley committed
274 275 276 277 278 279 280 281 282 283 284
dns_rdataclass_t
dns_db_class(dns_db_t *db) {
	/*
	 * The class of the database.
	 */

	REQUIRE(DNS_DB_VALID(db));

	return (db->rdclass);
}

285
isc_result_t
286
dns_db_beginload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
Bob Halley's avatar
Bob Halley committed
287 288 289 290 291
	/*
	 * Begin loading 'db'.
	 */

	REQUIRE(DNS_DB_VALID(db));
292
	REQUIRE(DNS_CALLBACK_VALID(callbacks));
Bob Halley's avatar
Bob Halley committed
293

294
	return ((db->methods->beginload)(db, callbacks));
Bob Halley's avatar
Bob Halley committed
295 296
}

297
isc_result_t
298
dns_db_endload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
299 300
	dns_dbonupdatelistener_t *listener;

Bob Halley's avatar
Bob Halley committed
301 302 303 304 305
	/*
	 * Finish loading 'db'.
	 */

	REQUIRE(DNS_DB_VALID(db));
306 307
	REQUIRE(DNS_CALLBACK_VALID(callbacks));
	REQUIRE(callbacks->add_private != NULL);
Bob Halley's avatar
Bob Halley committed
308

309 310 311 312 313
	for (listener = ISC_LIST_HEAD(db->update_listeners);
	     listener != NULL;
	     listener = ISC_LIST_NEXT(listener, link))
		listener->onupdate(db, listener->onupdate_arg);

314
	return ((db->methods->endload)(db, callbacks));
Bob Halley's avatar
Bob Halley committed
315 316
}

317
isc_result_t
Bob Halley's avatar
Bob Halley committed
318
dns_db_load(dns_db_t *db, const char *filename) {
319
	return (dns_db_load3(db, filename, dns_masterformat_text, 0));
320 321 322 323
}

isc_result_t
dns_db_load2(dns_db_t *db, const char *filename, dns_masterformat_t format) {
324 325 326 327 328
	return (dns_db_load3(db, filename, format, 0));
}

isc_result_t
dns_db_load3(dns_db_t *db, const char *filename, dns_masterformat_t format,
329 330
	     unsigned int options)
{
331
	isc_result_t result, eresult;
Bob Halley's avatar
Bob Halley committed
332 333
	dns_rdatacallbacks_t callbacks;

334 335 336
	/*
	 * Load master file 'filename' into 'db'.
	 */
Bob Halley's avatar
Bob Halley committed
337 338 339

	REQUIRE(DNS_DB_VALID(db));

Bob Halley's avatar
Bob Halley committed
340
	if ((db->attributes & DNS_DBATTR_CACHE) != 0)
341
		options |= DNS_MASTER_AGETTL;
Bob Halley's avatar
Bob Halley committed
342 343

	dns_rdatacallbacks_init(&callbacks);
344
	result = dns_db_beginload(db, &callbacks);
345
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
346
		return (result);
347 348 349
	result = dns_master_loadfile2(filename, &db->origin, &db->origin,
				      db->rdclass, options,
				      &callbacks, db->mctx, format);
350
	eresult = dns_db_endload(db, &callbacks);
Bob Halley's avatar
Bob Halley committed
351 352
	/*
	 * We always call dns_db_endload(), but we only want to return its
353
	 * result if dns_master_loadfile() succeeded.  If dns_master_loadfile()
Bob Halley's avatar
Bob Halley committed
354 355
	 * failed, we want to return the result code it gave us.
	 */
356 357
	if (eresult != ISC_R_SUCCESS &&
	    (result == ISC_R_SUCCESS || result == DNS_R_SEENINCLUDE))
Bob Halley's avatar
Bob Halley committed
358 359 360
		result = eresult;

	return (result);
Bob Halley's avatar
Bob Halley committed
361 362
}

Tinderbox User's avatar
Tinderbox User committed
363
isc_result_t
364 365 366 367 368 369 370
dns_db_serialize(dns_db_t *db, dns_dbversion_t *version, FILE *file) {
	REQUIRE(DNS_DB_VALID(db));
	if (db->methods->serialize == NULL)
		return (ISC_R_NOTIMPLEMENTED);
	return ((db->methods->serialize)(db, version, file));
}

371
isc_result_t
Bob Halley's avatar
Bob Halley committed
372
dns_db_dump(dns_db_t *db, dns_dbversion_t *version, const char *filename) {
373 374 375 376 377 378 379
	return ((db->methods->dump)(db, version, filename,
				    dns_masterformat_text));
}

isc_result_t
dns_db_dump2(dns_db_t *db, dns_dbversion_t *version, const char *filename,
	     dns_masterformat_t masterformat) {
380
	/*
381 382 383
	 * Dump 'db' into master file 'filename' in the 'masterformat' format.
	 * XXXJT: is it okay to modify the interface to the existing "dump"
	 * method?
384 385 386 387
	 */

	REQUIRE(DNS_DB_VALID(db));

388
	return ((db->methods->dump)(db, version, filename, masterformat));
389 390
}

391 392 393
/***
 *** Version Methods
 ***/
Bob Halley's avatar
Bob Halley committed
394 395 396

void
dns_db_currentversion(dns_db_t *db, dns_dbversion_t **versionp) {
397

398 399 400
	/*
	 * Open the current version for reading.
	 */
401

Bob Halley's avatar
Bob Halley committed
402
	REQUIRE(DNS_DB_VALID(db));
403
	REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
404
	REQUIRE(versionp != NULL && *versionp == NULL);
Bob Halley's avatar
Bob Halley committed
405 406 407 408

	(db->methods->currentversion)(db, versionp);
}

409
isc_result_t
Bob Halley's avatar
Bob Halley committed
410
dns_db_newversion(dns_db_t *db, dns_dbversion_t **versionp) {
411

412 413 414 415
	/*
	 * Open a new version for reading and writing.
	 */

Bob Halley's avatar
Bob Halley committed
416
	REQUIRE(DNS_DB_VALID(db));
417
	REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
418
	REQUIRE(versionp != NULL && *versionp == NULL);
Bob Halley's avatar
Bob Halley committed
419 420 421 422

	return ((db->methods->newversion)(db, versionp));
}

Bob Halley's avatar
Bob Halley committed
423 424 425 426 427 428 429 430 431 432 433
void
dns_db_attachversion(dns_db_t *db, dns_dbversion_t *source,
		     dns_dbversion_t **targetp)
{
	/*
	 * Attach '*targetp' to 'source'.
	 */

	REQUIRE(DNS_DB_VALID(db));
	REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
	REQUIRE(source != NULL);
434
	REQUIRE(targetp != NULL && *targetp == NULL);
Bob Halley's avatar
Bob Halley committed
435 436 437 438

	(db->methods->attachversion)(db, source, targetp);

	ENSURE(*targetp != NULL);
439
}
Bob Halley's avatar
Bob Halley committed
440

Bob Halley's avatar
Bob Halley committed
441
void
442 443 444
dns_db_closeversion(dns_db_t *db, dns_dbversion_t **versionp,
		    isc_boolean_t commit)
{
445
	dns_dbonupdatelistener_t *listener;
446

447 448 449 450
	/*
	 * Close version '*versionp'.
	 */

Bob Halley's avatar
Bob Halley committed
451
	REQUIRE(DNS_DB_VALID(db));
452
	REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
453 454 455
	REQUIRE(versionp != NULL && *versionp != NULL);

	(db->methods->closeversion)(db, versionp, commit);
Bob Halley's avatar
Bob Halley committed
456

457 458 459 460 461 462 463
	if (commit == ISC_TRUE) {
		for (listener = ISC_LIST_HEAD(db->update_listeners);
		     listener != NULL;
		     listener = ISC_LIST_NEXT(listener, link))
			listener->onupdate(db, listener->onupdate_arg);
	}

464
	ENSURE(*versionp == NULL);
Bob Halley's avatar
Bob Halley committed
465 466
}

467 468 469
/***
 *** Node Methods
 ***/
Bob Halley's avatar
Bob Halley committed
470

471
isc_result_t
472
dns_db_findnode(dns_db_t *db, const dns_name_t *name,
Bob Halley's avatar
Bob Halley committed
473 474
		isc_boolean_t create, dns_dbnode_t **nodep)
{
475 476 477 478 479

	/*
	 * Find the node with name 'name'.
	 */

Bob Halley's avatar
Bob Halley committed
480
	REQUIRE(DNS_DB_VALID(db));
481
	REQUIRE(nodep != NULL && *nodep == NULL);
Bob Halley's avatar
Bob Halley committed
482

483 484 485 486 487 488 489 490
	if (db->methods->findnode != NULL)
		return ((db->methods->findnode)(db, name, create, nodep));
	else
		return ((db->methods->findnodeext)(db, name, create,
						   NULL, NULL, nodep));
}

isc_result_t
491
dns_db_findnodeext(dns_db_t *db, const dns_name_t *name,
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
		   isc_boolean_t create, dns_clientinfomethods_t *methods,
		   dns_clientinfo_t *clientinfo, dns_dbnode_t **nodep)
{
	/*
	 * Find the node with name 'name', passing 'arg' to the database
	 * implementation.
	 */

	REQUIRE(DNS_DB_VALID(db));
	REQUIRE(nodep != NULL && *nodep == NULL);

	if (db->methods->findnodeext != NULL)
		return ((db->methods->findnodeext)(db, name, create,
						   methods, clientinfo, nodep));
	else
		return ((db->methods->findnode)(db, name, create, nodep));
Bob Halley's avatar
Bob Halley committed
508 509
}

510
isc_result_t
511
dns_db_findnsec3node(dns_db_t *db, const dns_name_t *name,
512 513 514 515 516 517 518 519 520 521 522 523 524
		     isc_boolean_t create, dns_dbnode_t **nodep)
{

	/*
	 * Find the node with name 'name'.
	 */

	REQUIRE(DNS_DB_VALID(db));
	REQUIRE(nodep != NULL && *nodep == NULL);

	return ((db->methods->findnsec3node)(db, name, create, nodep));
}

525
isc_result_t
526
dns_db_find(dns_db_t *db, const dns_name_t *name, dns_dbversion_t *version,
527
	    dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
Bob Halley's avatar
Bob Halley committed
528
	    dns_dbnode_t **nodep, dns_name_t *foundname,
Bob Halley's avatar
Bob Halley committed
529
	    dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
Bob Halley's avatar
lint  
Bob Halley committed
530
{
531 532 533 534 535 536
	/*
	 * Find the best match for 'name' and 'type' in version 'version'
	 * of 'db'.
	 */

	REQUIRE(DNS_DB_VALID(db));
537
	REQUIRE(type != dns_rdatatype_rrsig);
538
	REQUIRE(nodep == NULL || (nodep != NULL && *nodep == NULL));
539
	REQUIRE(dns_name_hasbuffer(foundname));
540
	REQUIRE(rdataset == NULL ||
541 542
		(DNS_RDATASET_VALID(rdataset) &&
		 ! dns_rdataset_isassociated(rdataset)));
Bob Halley's avatar
Bob Halley committed
543 544
	REQUIRE(sigrdataset == NULL ||
		(DNS_RDATASET_VALID(sigrdataset) &&
545
		 ! dns_rdataset_isassociated(sigrdataset)));
546

547 548 549 550 551 552 553 554 555 556 557 558
	if (db->methods->find != NULL)
		return ((db->methods->find)(db, name, version, type,
					    options, now, nodep, foundname,
					    rdataset, sigrdataset));
	else
		return ((db->methods->findext)(db, name, version, type,
					       options, now, nodep, foundname,
					       NULL, NULL,
					       rdataset, sigrdataset));
}

isc_result_t
559
dns_db_findext(dns_db_t *db, const dns_name_t *name, dns_dbversion_t *version,
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
	       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)
{

	/*
	 * Find the best match for 'name' and 'type' in version 'version'
	 * of 'db', passing in 'arg'.
	 */

	REQUIRE(DNS_DB_VALID(db));
	REQUIRE(type != dns_rdatatype_rrsig);
	REQUIRE(nodep == NULL || (nodep != NULL && *nodep == NULL));
	REQUIRE(dns_name_hasbuffer(foundname));
	REQUIRE(rdataset == NULL ||
		(DNS_RDATASET_VALID(rdataset) &&
		 ! dns_rdataset_isassociated(rdataset)));
	REQUIRE(sigrdataset == NULL ||
		(DNS_RDATASET_VALID(sigrdataset) &&
		 ! dns_rdataset_isassociated(sigrdataset)));

	if (db->methods->findext != NULL)
		return ((db->methods->findext)(db, name, version, type,
					       options, now, nodep, foundname,
					       methods, clientinfo,
					       rdataset, sigrdataset));
	else
		return ((db->methods->find)(db, name, version, type,
					    options, now, nodep, foundname,
					    rdataset, sigrdataset));
Bob Halley's avatar
Bob Halley committed
591 592
}

593
isc_result_t
594
dns_db_findzonecut(dns_db_t *db, const dns_name_t *name,
Bob Halley's avatar
Bob Halley committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608
		   unsigned int options, isc_stdtime_t now,
		   dns_dbnode_t **nodep, dns_name_t *foundname,
		   dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
{
	/*
	 * Find the deepest known zonecut which encloses 'name' in 'db'.
	 */

	REQUIRE(DNS_DB_VALID(db));
	REQUIRE((db->attributes & DNS_DBATTR_CACHE) != 0);
	REQUIRE(nodep == NULL || (nodep != NULL && *nodep == NULL));
	REQUIRE(dns_name_hasbuffer(foundname));
	REQUIRE(sigrdataset == NULL ||
		(DNS_RDATASET_VALID(sigrdataset) &&
609
		 ! dns_rdataset_isassociated(sigrdataset)));
Bob Halley's avatar
Bob Halley committed
610 611 612 613 614

	return ((db->methods->findzonecut)(db, name, options, now, nodep,
					   foundname, rdataset, sigrdataset));
}

Bob Halley's avatar
Bob Halley committed
615 616 617
void
dns_db_attachnode(dns_db_t *db, dns_dbnode_t *source, dns_dbnode_t **targetp) {

618 619 620 621
	/*
	 * Attach *targetp to source.
	 */

Bob Halley's avatar
Bob Halley committed
622
	REQUIRE(DNS_DB_VALID(db));
623 624
	REQUIRE(source != NULL);
	REQUIRE(targetp != NULL && *targetp == NULL);
Bob Halley's avatar
Bob Halley committed
625 626 627 628 629 630 631

	(db->methods->attachnode)(db, source, targetp);
}

void
dns_db_detachnode(dns_db_t *db, dns_dbnode_t **nodep) {

632 633 634 635
	/*
	 * Detach *nodep from its node.
	 */

Bob Halley's avatar
Bob Halley committed
636
	REQUIRE(DNS_DB_VALID(db));
637
	REQUIRE(nodep != NULL && *nodep != NULL);
Bob Halley's avatar
Bob Halley committed
638 639

	(db->methods->detachnode)(db, nodep);
640 641

	ENSURE(*nodep == NULL);
Bob Halley's avatar
Bob Halley committed
642 643
}

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
void
dns_db_transfernode(dns_db_t *db, dns_dbnode_t **sourcep,
		    dns_dbnode_t **targetp)
{
	REQUIRE(DNS_DB_VALID(db));
	REQUIRE(targetp != NULL && *targetp == NULL);
	/*
	 * This doesn't check the implementation magic.  If we find that
	 * we need such checks in future then this will be done in the
	 * method.
	 */
	REQUIRE(sourcep != NULL && *sourcep != NULL);

	UNUSED(db);

	if (db->methods->transfernode == NULL) {
Automatic Updater's avatar
Automatic Updater committed
660 661
		*targetp = *sourcep;
		*sourcep = NULL;
662
	} else
Automatic Updater's avatar
Automatic Updater committed
663
		(db->methods->transfernode)(db, sourcep, targetp);
664 665 666 667

	ENSURE(*sourcep == NULL);
}

668
isc_result_t
Bob Halley's avatar
Bob Halley committed
669 670 671 672 673 674 675 676 677 678 679 680 681
dns_db_expirenode(dns_db_t *db, dns_dbnode_t *node, isc_stdtime_t now) {

	/*
	 * Mark as stale all records at 'node' which expire at or before 'now'.
	 */

	REQUIRE(DNS_DB_VALID(db));
	REQUIRE((db->attributes & DNS_DBATTR_CACHE) != 0);
	REQUIRE(node != NULL);

	return ((db->methods->expirenode)(db, node, now));
}

Bob Halley's avatar
Bob Halley committed
682 683 684 685 686 687 688 689 690 691 692 693 694
void
dns_db_printnode(dns_db_t *db, dns_dbnode_t *node, FILE *out) {
	/*
	 * Print a textual representation of the contents of the node to
	 * 'out'.
	 */

	REQUIRE(DNS_DB_VALID(db));
	REQUIRE(node != NULL);

	(db->methods->printnode)(db, node, out);
}

Bob Halley's avatar
Bob Halley committed
695 696 697 698
/***
 *** DB Iterator Creation
 ***/

699
isc_result_t
700
dns_db_createiterator(dns_db_t *db, unsigned int flags,
Bob Halley's avatar
Bob Halley committed
701 702 703 704 705 706 707 708 709
		      dns_dbiterator_t **iteratorp)
{
	/*
	 * Create an iterator for version 'version' of 'db'.
	 */

	REQUIRE(DNS_DB_VALID(db));
	REQUIRE(iteratorp != NULL && *iteratorp == NULL);

710
	return (db->methods->createiterator(db, flags, iteratorp));
Bob Halley's avatar
Bob Halley committed
711 712
}

713 714 715 716
/***
 *** Rdataset Methods
 ***/

717
isc_result_t
Bob Halley's avatar
Bob Halley committed
718
dns_db_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
Bob Halley's avatar
Bob Halley committed
719
		    dns_rdatatype_t type, dns_rdatatype_t covers,
Bob Halley's avatar
Bob Halley committed
720 721
		    isc_stdtime_t now, dns_rdataset_t *rdataset,
		    dns_rdataset_t *sigrdataset)
Bob Halley's avatar
Bob Halley committed
722 723
{
	REQUIRE(DNS_DB_VALID(db));
724 725
	REQUIRE(node != NULL);
	REQUIRE(DNS_RDATASET_VALID(rdataset));
726
	REQUIRE(! dns_rdataset_isassociated(rdataset));
727
	REQUIRE(covers == 0 || type == dns_rdatatype_rrsig);
Bob Halley's avatar
Bob Halley committed
728
	REQUIRE(type != dns_rdatatype_any);
Bob Halley's avatar
Bob Halley committed
729 730
	REQUIRE(sigrdataset == NULL ||
		(DNS_RDATASET_VALID(sigrdataset) &&
731
		 ! dns_rdataset_isassociated(sigrdataset)));
Bob Halley's avatar
Bob Halley committed
732

733 734 735
	return ((db->methods->findrdataset)(db, node, version, type,
					    covers, now, rdataset,
					    sigrdataset));
Bob Halley's avatar
Bob Halley committed
736 737
}

738
isc_result_t
Bob Halley's avatar
Bob Halley committed
739
dns_db_allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
740
		    isc_stdtime_t now, dns_rdatasetiter_t **iteratorp)
Bob Halley's avatar
Bob Halley committed
741 742 743 744 745 746 747 748 749
{
	/*
	 * Make '*iteratorp' an rdataset iteratator for all rdatasets at
	 * 'node' in version 'version' of 'db'.
	 */

	REQUIRE(DNS_DB_VALID(db));
	REQUIRE(iteratorp != NULL && *iteratorp == NULL);

750 751
	return ((db->methods->allrdatasets)(db, node, version, now,
					    iteratorp));
Bob Halley's avatar
Bob Halley committed
752 753
}

754
isc_result_t
Bob Halley's avatar
Bob Halley committed
755
dns_db_addrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
756
		   isc_stdtime_t now, dns_rdataset_t *rdataset,
Bob Halley's avatar
Bob Halley committed
757
		   unsigned int options, dns_rdataset_t *addedrdataset)
Bob Halley's avatar
Bob Halley committed
758
{
759 760 761 762
	/*
	 * Add 'rdataset' to 'node' in version 'version' of 'db'.
	 */

Bob Halley's avatar
Bob Halley committed
763
	REQUIRE(DNS_DB_VALID(db));
764
	REQUIRE(node != NULL);
765
	REQUIRE(((db->attributes & DNS_DBATTR_CACHE) == 0 && version != NULL)||
Bob Halley's avatar
Bob Halley committed
766
		((db->attributes & DNS_DBATTR_CACHE) != 0 &&
Bob Halley's avatar
Bob Halley committed
767
		 version == NULL && (options & DNS_DBADD_MERGE) == 0));
768 769
	REQUIRE((options & DNS_DBADD_EXACT) == 0 ||
		(options & DNS_DBADD_MERGE) != 0);
770
	REQUIRE(DNS_RDATASET_VALID(rdataset));
771
	REQUIRE(dns_rdataset_isassociated(rdataset));
772
	REQUIRE(rdataset->rdclass == db->rdclass);
773 774
	REQUIRE(addedrdataset == NULL ||
		(DNS_RDATASET_VALID(addedrdataset) &&
775
		 ! dns_rdataset_isassociated(addedrdataset)));
Bob Halley's avatar
Bob Halley committed
776

777
	return ((db->methods->addrdataset)(db, node, version, now, rdataset,
Bob Halley's avatar
Bob Halley committed
778
					   options, addedrdataset));
Bob Halley's avatar
Bob Halley committed
779 780
}

781
isc_result_t
Bob Halley's avatar
Bob Halley committed
782 783
dns_db_subtractrdataset(dns_db_t *db, dns_dbnode_t *node,
			dns_dbversion_t *version, dns_rdataset_t *rdataset,
784
			unsigned int options, dns_rdataset_t *newrdataset)
Bob Halley's avatar
Bob Halley committed
785 786 787 788 789 790 791 792
{
	/*
	 * Remove any rdata in 'rdataset' from 'node' in version 'version' of
	 * 'db'.
	 */

	REQUIRE(DNS_DB_VALID(db));
	REQUIRE(node != NULL);
Bob Halley's avatar
lint  
Bob Halley committed
793
	REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0 && version != NULL);
Bob Halley's avatar
Bob Halley committed
794
	REQUIRE(DNS_RDATASET_VALID(rdataset));
795
	REQUIRE(dns_rdataset_isassociated(rdataset));
Bob Halley's avatar
Bob Halley committed
796 797 798
	REQUIRE(rdataset->rdclass == db->rdclass);
	REQUIRE(newrdataset == NULL ||
		(DNS_RDATASET_VALID(newrdataset) &&
799
		 ! dns_rdataset_isassociated(newrdataset)));
Bob Halley's avatar
Bob Halley committed
800 801

	return ((db->methods->subtractrdataset)(db, node, version, rdataset,
802
						options, newrdataset));
Bob Halley's avatar
Bob Halley committed
803 804
}

805
isc_result_t
Bob Halley's avatar
Bob Halley committed
806
dns_db_deleterdataset(dns_db_t *db, dns_dbnode_t *node,
807 808
		      dns_dbversion_t *version, dns_rdatatype_t type,
		      dns_rdatatype_t covers)
Bob Halley's avatar
Bob Halley committed
809
{
810 811 812 813 814
	/*
	 * Make it so that no rdataset of type 'type' exists at 'node' in
	 * version version 'version' of 'db'.
	 */

Bob Halley's avatar
Bob Halley committed
815
	REQUIRE(DNS_DB_VALID(db));
816
	REQUIRE(node != NULL);
817 818
	REQUIRE(((db->attributes & DNS_DBATTR_CACHE) == 0 && version != NULL)||
		((db->attributes & DNS_DBATTR_CACHE) != 0 && version == NULL));
Bob Halley's avatar
Bob Halley committed
819

820 821
	return ((db->methods->deleterdataset)(db, node, version,
					      type, covers));
Bob Halley's avatar
Bob Halley committed
822
}
823

Automatic Updater's avatar
Automatic Updater committed
824
void
825 826 827 828 829 830 831
dns_db_overmem(dns_db_t *db, isc_boolean_t overmem) {

	REQUIRE(DNS_DB_VALID(db));

	(db->methods->overmem)(db, overmem);
}

832 833 834 835 836 837
isc_result_t
dns_db_getsoaserial(dns_db_t *db, dns_dbversion_t *ver, isc_uint32_t *serialp)
{
	isc_result_t result;
	dns_dbnode_t *node = NULL;
	dns_rdataset_t rdataset;
838
	dns_rdata_t rdata = DNS_RDATA_INIT;
839 840
	isc_buffer_t buffer;

841
	REQUIRE(dns_db_iszone(db) || dns_db_isstub(db));
842

843 844 845 846 847 848 849
	result = dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node);
	if (result != ISC_R_SUCCESS)
		return (result);

	dns_rdataset_init(&rdataset);
	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_soa, 0,
				     (isc_stdtime_t)0, &rdataset, NULL);
Automatic Updater's avatar
Automatic Updater committed
850
	if (result != ISC_R_SUCCESS)
851
		goto freenode;
852

853
	result = dns_rdataset_first(&rdataset);
Automatic Updater's avatar
Automatic Updater committed
854
	if (result != ISC_R_SUCCESS)
855 856
		goto freerdataset;
	dns_rdataset_current(&rdataset, &rdata);
857
	result = dns_rdataset_next(&rdataset);
858
	INSIST(result == ISC_R_NOMORE);
859 860 861 862 863 864 865 866 867 868 869

	INSIST(rdata.length > 20);
	isc_buffer_init(&buffer, rdata.data, rdata.length);
	isc_buffer_add(&buffer, rdata.length);
	isc_buffer_forward(&buffer, rdata.length - 20);
	*serialp = isc_buffer_getuint32(&buffer);

	result = ISC_R_SUCCESS;

 freerdataset:
	dns_rdataset_disassociate(&rdataset);
870

871 872 873 874
 freenode:
	dns_db_detachnode(db, &node);
	return (result);
}
875 876 877 878 879 880 881

unsigned int
dns_db_nodecount(dns_db_t *db) {
	REQUIRE(DNS_DB_VALID(db));

	return ((db->methods->nodecount)(db));
}
882

883
size_t
884 885 886 887
dns_db_hashsize(dns_db_t *db) {
	REQUIRE(DNS_DB_VALID(db));

	if (db->methods->hashsize == NULL)
888
		return (0);
889 890 891 892

	return ((db->methods->hashsize)(db));
}

893 894 895 896 897 898 899
void
dns_db_settask(dns_db_t *db, isc_task_t *task) {
	REQUIRE(DNS_DB_VALID(db));

	(db->methods->settask)(db, task);
}

900 901 902 903 904 905 906 907 908
isc_result_t
dns_db_register(const char *name, dns_dbcreatefunc_t create, void *driverarg,
		isc_mem_t *mctx, dns_dbimplementation_t **dbimp)
{
	dns_dbimplementation_t *imp;

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

909 910
	RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);

911 912 913 914 915 916
	RWLOCK(&implock, isc_rwlocktype_write);
	imp = impfind(name);
	if (imp != NULL) {
		RWUNLOCK(&implock, isc_rwlocktype_write);
		return (ISC_R_EXISTS);
	}
Automatic Updater's avatar
Automatic Updater committed
917

918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
	imp = isc_mem_get(mctx, sizeof(dns_dbimplementation_t));
	if (imp == NULL) {
		RWUNLOCK(&implock, isc_rwlocktype_write);
		return (ISC_R_NOMEMORY);
	}
	imp->name = name;
	imp->create = create;
	imp->mctx = NULL;
	imp->driverarg = driverarg;
	isc_mem_attach(mctx, &imp->mctx);
	ISC_LINK_INIT(imp, link);
	ISC_LIST_APPEND(implementations, imp, link);
	RWUNLOCK(&implock, isc_rwlocktype_write);

	*dbimp = imp;

	return (ISC_R_SUCCESS);
}

void
dns_db_unregister(dns_dbimplementation_t **dbimp) {
	dns_dbimplementation_t *imp;
	isc_mem_t *mctx;

	REQUIRE(dbimp != NULL && *dbimp != NULL);

944 945
	RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);

946
	imp = *dbimp;
947
	*dbimp = NULL;
948 949 950 951 952 953
	RWLOCK(&implock, isc_rwlocktype_write);
	ISC_LIST_UNLINK(implementations, imp, link);
	mctx = imp->mctx;
	isc_mem_put(mctx, imp, sizeof(dns_dbimplementation_t));
	isc_mem_detach(&mctx);
	RWUNLOCK(&implock, isc_rwlocktype_write);
954
	ENSURE(*dbimp == NULL);
955
}
956 957

isc_result_t
958
dns_db_getoriginnode(dns_db_t *db, dns_dbnode_t **nodep) {
959 960 961 962
	REQUIRE(DNS_DB_VALID(db));
	REQUIRE(dns_db_iszone(db) == ISC_TRUE);
	REQUIRE(nodep != NULL && *nodep == NULL);

963 964
	if (db->methods->getoriginnode != NULL)
		return ((db->methods->getoriginnode)(db, nodep));
965 966 967

	return (ISC_R_NOTFOUND);
}
968

969 970 971 972 973 974 975 976 977 978
dns_stats_t *
dns_db_getrrsetstats(dns_db_t *db) {
	REQUIRE(DNS_DB_VALID(db));

	if (db->methods->getrrsetstats != NULL)
		return ((db->methods->getrrsetstats)(db));

	return (NULL);
}

979 980 981 982 983 984 985 986 987 988
isc_result_t
dns_db_setcachestats(dns_db_t *db, isc_stats_t *stats) {
	REQUIRE(DNS_DB_VALID(db));

	if (db->methods->setcachestats != NULL)
		return ((db->methods->setcachestats)(db, stats));

	return (ISC_R_NOTIMPLEMENTED);
}

989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
isc_result_t
dns_db_getnsec3parameters(dns_db_t *db, dns_dbversion_t *version,
			  dns_hash_t *hash, isc_uint8_t *flags,
			  isc_uint16_t *iterations,
			  unsigned char *salt, size_t *salt_length)
{
	REQUIRE(DNS_DB_VALID(db));
	REQUIRE(dns_db_iszone(db) == ISC_TRUE);

	if (db->methods->getnsec3parameters != NULL)
		return ((db->methods->getnsec3parameters)(db, version, hash,
							  flags, iterations,
							  salt, salt_length));

	return (ISC_R_NOTFOUND);
}

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
isc_result_t
dns_db_getsize(dns_db_t *db, dns_dbversion_t *version, isc_uint64_t *records,
	       isc_uint64_t *bytes)
{
	REQUIRE(DNS_DB_VALID(db));
	REQUIRE(dns_db_iszone(db) == ISC_TRUE);

	if (db->methods->getsize != NULL)
		return ((db->methods->getsize)(db, version, records, bytes));

	return (ISC_R_NOTFOUND);
}

1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
isc_result_t
dns_db_setsigningtime(dns_db_t *db, dns_rdataset_t *rdataset,
		      isc_stdtime_t resign)
{
	if (db->methods->setsigningtime != NULL)
		return ((db->methods->setsigningtime)(db, rdataset, resign));
	return (ISC_R_NOTIMPLEMENTED);
}

isc_result_t