sdlz.c revision 1.15 1 /* $NetBSD: sdlz.c,v 1.15 2026/01/29 18:37:50 christos Exp $ */
2
3 /*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0 AND ISC
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16 /*
17 * Copyright (C) 2002 Stichting NLnet, Netherlands, stichting (at) nlnet.nl.
18 *
19 * Permission to use, copy, modify, and distribute this software for any
20 * purpose with or without fee is hereby granted, provided that the
21 * above copyright notice and this permission notice appear in all
22 * copies.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS" AND STICHTING NLNET
25 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
27 * STICHTING NLNET BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
28 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
29 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
30 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
31 * USE OR PERFORMANCE OF THIS SOFTWARE.
32 *
33 * The development of Dynamically Loadable Zones (DLZ) for Bind 9 was
34 * conceived and contributed by Rob Butler.
35 *
36 * Permission to use, copy, modify, and distribute this software for any
37 * purpose with or without fee is hereby granted, provided that the
38 * above copyright notice and this permission notice appear in all
39 * copies.
40 *
41 * THE SOFTWARE IS PROVIDED "AS IS" AND ROB BUTLER
42 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
44 * ROB BUTLER BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
45 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
46 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
47 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
48 * USE OR PERFORMANCE OF THIS SOFTWARE.
49 */
50
51 /*! \file */
52
53 #include <inttypes.h>
54 #include <stdbool.h>
55 #include <string.h>
56
57 #include <isc/ascii.h>
58 #include <isc/buffer.h>
59 #include <isc/lex.h>
60 #include <isc/log.h>
61 #include <isc/magic.h>
62 #include <isc/mem.h>
63 #include <isc/once.h>
64 #include <isc/region.h>
65 #include <isc/result.h>
66 #include <isc/rwlock.h>
67 #include <isc/string.h>
68 #include <isc/util.h>
69
70 #include <dns/callbacks.h>
71 #include <dns/db.h>
72 #include <dns/dbiterator.h>
73 #include <dns/dlz.h>
74 #include <dns/fixedname.h>
75 #include <dns/log.h>
76 #include <dns/master.h>
77 #include <dns/rdata.h>
78 #include <dns/rdatalist.h>
79 #include <dns/rdataset.h>
80 #include <dns/rdatasetiter.h>
81 #include <dns/rdatatype.h>
82 #include <dns/sdlz.h>
83 #include <dns/types.h>
84
85 /*
86 * Private Types
87 */
88
89 struct dns_sdlzimplementation {
90 const dns_sdlzmethods_t *methods;
91 isc_mem_t *mctx;
92 void *driverarg;
93 unsigned int flags;
94 isc_mutex_t driverlock;
95 dns_dlzimplementation_t *dlz_imp;
96 };
97
98 struct dns_sdlz_db {
99 /* Unlocked */
100 dns_db_t common;
101 void *dbdata;
102 dns_sdlzimplementation_t *dlzimp;
103
104 /* Locked */
105 dns_dbversion_t *future_version;
106 int dummy_version;
107 };
108
109 struct dns_sdlzlookup {
110 /* Unlocked */
111 unsigned int magic;
112 dns_sdlz_db_t *sdlz;
113 ISC_LIST(dns_rdatalist_t) lists;
114 ISC_LIST(isc_buffer_t) buffers;
115 dns_name_t *name;
116 ISC_LINK(dns_sdlzlookup_t) link;
117 dns_rdatacallbacks_t callbacks;
118
119 /* Atomic */
120 isc_refcount_t references;
121 };
122
123 typedef struct dns_sdlzlookup dns_sdlznode_t;
124
125 struct dns_sdlzallnodes {
126 dns_dbiterator_t common;
127 ISC_LIST(dns_sdlznode_t) nodelist;
128 dns_sdlznode_t *current;
129 dns_sdlznode_t *origin;
130 };
131
132 typedef dns_sdlzallnodes_t sdlz_dbiterator_t;
133
134 typedef struct sdlz_rdatasetiter {
135 dns_rdatasetiter_t common;
136 dns_rdatalist_t *current;
137 } sdlz_rdatasetiter_t;
138
139 #define SDLZDB_MAGIC ISC_MAGIC('D', 'L', 'Z', 'S')
140
141 /*
142 * Note that "impmagic" is not the first four bytes of the struct, so
143 * ISC_MAGIC_VALID cannot be used.
144 */
145
146 #define VALID_SDLZDB(sdlzdb) \
147 ((sdlzdb) != NULL && (sdlzdb)->common.impmagic == SDLZDB_MAGIC)
148
149 #define SDLZLOOKUP_MAGIC ISC_MAGIC('D', 'L', 'Z', 'L')
150 #define VALID_SDLZLOOKUP(sdlzl) ISC_MAGIC_VALID(sdlzl, SDLZLOOKUP_MAGIC)
151 #define VALID_SDLZNODE(sdlzn) VALID_SDLZLOOKUP(sdlzn)
152
153 /* These values are taken from RFC 1537 */
154 #define SDLZ_DEFAULT_REFRESH 28800U /* 8 hours */
155 #define SDLZ_DEFAULT_RETRY 7200U /* 2 hours */
156 #define SDLZ_DEFAULT_EXPIRE 604800U /* 7 days */
157 #define SDLZ_DEFAULT_MINIMUM 86400U /* 1 day */
158
159 /* This is a reasonable value */
160 #define SDLZ_DEFAULT_TTL (60 * 60 * 24)
161
162 #ifdef __COVERITY__
163 #define MAYBE_LOCK(imp) LOCK(&imp->driverlock)
164 #define MAYBE_UNLOCK(imp) UNLOCK(&imp->driverlock)
165 #else /* ifdef __COVERITY__ */
166 #define MAYBE_LOCK(imp) \
167 do { \
168 unsigned int flags = imp->flags; \
169 if ((flags & DNS_SDLZFLAG_THREADSAFE) == 0) \
170 LOCK(&imp->driverlock); \
171 } while (0)
172
173 #define MAYBE_UNLOCK(imp) \
174 do { \
175 unsigned int flags = imp->flags; \
176 if ((flags & DNS_SDLZFLAG_THREADSAFE) == 0) \
177 UNLOCK(&imp->driverlock); \
178 } while (0)
179 #endif /* ifdef __COVERITY__ */
180
181 /*
182 * Forward references.
183 */
184 static isc_result_t
185 getnodedata(dns_db_t *db, const dns_name_t *name, bool create,
186 unsigned int options, dns_clientinfomethods_t *methods,
187 dns_clientinfo_t *clientinfo, dns_dbnode_t **nodep);
188
189 static void
190 list_tordataset(dns_rdatalist_t *rdatalist, dns_db_t *db, dns_dbnode_t *node,
191 dns_rdataset_t *rdataset);
192
193 static void
194 detachnode(dns_db_t *db, dns_dbnode_t **targetp DNS__DB_FLARG);
195
196 static void
197 dbiterator_destroy(dns_dbiterator_t **iteratorp DNS__DB_FLARG);
198 static isc_result_t
199 dbiterator_first(dns_dbiterator_t *iterator DNS__DB_FLARG);
200 static isc_result_t
201 dbiterator_last(dns_dbiterator_t *iterator DNS__DB_FLARG);
202 static isc_result_t
203 dbiterator_seek(dns_dbiterator_t *iterator,
204 const dns_name_t *name DNS__DB_FLARG);
205 static isc_result_t
206 dbiterator_seek3(dns_dbiterator_t *iterator,
207 const dns_name_t *name DNS__DB_FLARG);
208 static isc_result_t
209 dbiterator_prev(dns_dbiterator_t *iterator DNS__DB_FLARG);
210 static isc_result_t
211 dbiterator_next(dns_dbiterator_t *iterator DNS__DB_FLARG);
212 static isc_result_t
213 dbiterator_current(dns_dbiterator_t *iterator, dns_dbnode_t **nodep,
214 dns_name_t *name DNS__DB_FLARG);
215 static isc_result_t
216 dbiterator_pause(dns_dbiterator_t *iterator);
217 static isc_result_t
218 dbiterator_origin(dns_dbiterator_t *iterator, dns_name_t *name);
219
220 static dns_dbiteratormethods_t dbiterator_methods = {
221 dbiterator_destroy, dbiterator_first, dbiterator_last,
222 dbiterator_seek, dbiterator_seek3, dbiterator_prev,
223 dbiterator_next, dbiterator_current, dbiterator_pause,
224 dbiterator_origin
225 };
226
227 /*
228 * Utility functions
229 */
230
231 /*
232 * Log a message at the given level
233 */
234 static void
235 sdlz_log(int level, const char *fmt, ...) {
236 va_list ap;
237 va_start(ap, fmt);
238 isc_log_vwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
239 ISC_LOG_DEBUG(level), fmt, ap);
240 va_end(ap);
241 }
242
243 static unsigned int
244 initial_size(const char *data) {
245 unsigned int len = (strlen(data) / 64) + 1;
246 return len * 64 + 64;
247 }
248
249 /*
250 * Rdataset Iterator Methods. These methods were "borrowed" from the SDB
251 * driver interface. See the SDB driver interface documentation for more info.
252 */
253
254 static void
255 rdatasetiter_destroy(dns_rdatasetiter_t **iteratorp DNS__DB_FLARG) {
256 sdlz_rdatasetiter_t *sdlziterator = (sdlz_rdatasetiter_t *)(*iteratorp);
257
258 detachnode(sdlziterator->common.db,
259 &sdlziterator->common.node DNS__DB_FLARG_PASS);
260 isc_mem_put(sdlziterator->common.db->mctx, sdlziterator,
261 sizeof(sdlz_rdatasetiter_t));
262 *iteratorp = NULL;
263 }
264
265 static isc_result_t
266 rdatasetiter_first(dns_rdatasetiter_t *iterator DNS__DB_FLARG) {
267 sdlz_rdatasetiter_t *sdlziterator = (sdlz_rdatasetiter_t *)iterator;
268 dns_sdlznode_t *sdlznode = (dns_sdlznode_t *)iterator->node;
269
270 if (ISC_LIST_EMPTY(sdlznode->lists)) {
271 return ISC_R_NOMORE;
272 }
273 sdlziterator->current = ISC_LIST_HEAD(sdlznode->lists);
274 return ISC_R_SUCCESS;
275 }
276
277 static isc_result_t
278 rdatasetiter_next(dns_rdatasetiter_t *iterator DNS__DB_FLARG) {
279 sdlz_rdatasetiter_t *sdlziterator = (sdlz_rdatasetiter_t *)iterator;
280
281 sdlziterator->current = ISC_LIST_NEXT(sdlziterator->current, link);
282 if (sdlziterator->current == NULL) {
283 return ISC_R_NOMORE;
284 } else {
285 return ISC_R_SUCCESS;
286 }
287 }
288
289 static void
290 rdatasetiter_current(dns_rdatasetiter_t *iterator,
291 dns_rdataset_t *rdataset DNS__DB_FLARG) {
292 sdlz_rdatasetiter_t *sdlziterator = (sdlz_rdatasetiter_t *)iterator;
293
294 list_tordataset(sdlziterator->current, iterator->db, iterator->node,
295 rdataset);
296 }
297
298 static dns_rdatasetitermethods_t rdatasetiter_methods = {
299 rdatasetiter_destroy, rdatasetiter_first, rdatasetiter_next,
300 rdatasetiter_current
301 };
302
303 /*
304 * DB routines. These methods were "borrowed" from the SDB driver interface.
305 * See the SDB driver interface documentation for more info.
306 */
307
308 static void
309 destroy(dns_db_t *db) {
310 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
311
312 sdlz->common.magic = 0;
313 sdlz->common.impmagic = 0;
314
315 dns_name_free(&sdlz->common.origin, sdlz->common.mctx);
316
317 isc_refcount_destroy(&sdlz->common.references);
318 isc_mem_putanddetach(&sdlz->common.mctx, sdlz, sizeof(dns_sdlz_db_t));
319 }
320
321 static void
322 currentversion(dns_db_t *db, dns_dbversion_t **versionp) {
323 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
324 REQUIRE(VALID_SDLZDB(sdlz));
325 REQUIRE(versionp != NULL && *versionp == NULL);
326
327 *versionp = (void *)&sdlz->dummy_version;
328 return;
329 }
330
331 static isc_result_t
332 newversion(dns_db_t *db, dns_dbversion_t **versionp) {
333 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
334 char origin[DNS_NAME_MAXTEXT + 1];
335 isc_result_t result;
336
337 REQUIRE(VALID_SDLZDB(sdlz));
338
339 if (sdlz->dlzimp->methods->newversion == NULL) {
340 return ISC_R_NOTIMPLEMENTED;
341 }
342
343 dns_name_format(&sdlz->common.origin, origin, sizeof(origin));
344
345 result = sdlz->dlzimp->methods->newversion(
346 origin, sdlz->dlzimp->driverarg, sdlz->dbdata, versionp);
347 if (result != ISC_R_SUCCESS) {
348 sdlz_log(ISC_LOG_ERROR,
349 "sdlz newversion on origin %s failed : %s", origin,
350 isc_result_totext(result));
351 return result;
352 }
353
354 sdlz->future_version = *versionp;
355 return ISC_R_SUCCESS;
356 }
357
358 static void
359 attachversion(dns_db_t *db, dns_dbversion_t *source,
360 dns_dbversion_t **targetp) {
361 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
362
363 REQUIRE(VALID_SDLZDB(sdlz));
364 REQUIRE(source != NULL && source == (void *)&sdlz->dummy_version);
365
366 *targetp = source;
367 }
368
369 static void
370 closeversion(dns_db_t *db, dns_dbversion_t **versionp,
371 bool commit DNS__DB_FLARG) {
372 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
373 char origin[DNS_NAME_MAXTEXT + 1];
374
375 REQUIRE(VALID_SDLZDB(sdlz));
376 REQUIRE(versionp != NULL);
377
378 if (*versionp == (void *)&sdlz->dummy_version) {
379 *versionp = NULL;
380 return;
381 }
382
383 REQUIRE(*versionp == sdlz->future_version);
384 REQUIRE(sdlz->dlzimp->methods->closeversion != NULL);
385
386 dns_name_format(&sdlz->common.origin, origin, sizeof(origin));
387
388 sdlz->dlzimp->methods->closeversion(origin, commit,
389 sdlz->dlzimp->driverarg,
390 sdlz->dbdata, versionp);
391 if (*versionp != NULL) {
392 sdlz_log(ISC_LOG_ERROR, "sdlz closeversion on origin %s failed",
393 origin);
394 }
395
396 sdlz->future_version = NULL;
397 }
398
399 static isc_result_t
400 createnode(dns_sdlz_db_t *sdlz, dns_sdlznode_t **nodep) {
401 dns_sdlznode_t *node;
402
403 node = isc_mem_get(sdlz->common.mctx, sizeof(dns_sdlznode_t));
404
405 node->sdlz = NULL;
406 dns_db_attach((dns_db_t *)sdlz, (dns_db_t **)&node->sdlz);
407 ISC_LIST_INIT(node->lists);
408 ISC_LIST_INIT(node->buffers);
409 ISC_LINK_INIT(node, link);
410 node->name = NULL;
411 dns_rdatacallbacks_init(&node->callbacks);
412
413 isc_refcount_init(&node->references, 1);
414 node->magic = SDLZLOOKUP_MAGIC;
415
416 *nodep = node;
417 return ISC_R_SUCCESS;
418 }
419
420 static void
421 destroynode(dns_sdlznode_t *node) {
422 dns_rdatalist_t *list;
423 dns_rdata_t *rdata;
424 isc_buffer_t *b;
425 dns_sdlz_db_t *sdlz;
426 dns_db_t *db;
427 isc_mem_t *mctx;
428
429 isc_refcount_destroy(&node->references);
430
431 sdlz = node->sdlz;
432 mctx = sdlz->common.mctx;
433
434 while (!ISC_LIST_EMPTY(node->lists)) {
435 list = ISC_LIST_HEAD(node->lists);
436 while (!ISC_LIST_EMPTY(list->rdata)) {
437 rdata = ISC_LIST_HEAD(list->rdata);
438 ISC_LIST_UNLINK(list->rdata, rdata, link);
439 isc_mem_put(mctx, rdata, sizeof(dns_rdata_t));
440 }
441 ISC_LIST_UNLINK(node->lists, list, link);
442 isc_mem_put(mctx, list, sizeof(dns_rdatalist_t));
443 }
444
445 while (!ISC_LIST_EMPTY(node->buffers)) {
446 b = ISC_LIST_HEAD(node->buffers);
447 ISC_LIST_UNLINK(node->buffers, b, link);
448 isc_buffer_free(&b);
449 }
450
451 if (node->name != NULL) {
452 dns_name_free(node->name, mctx);
453 isc_mem_put(mctx, node->name, sizeof(dns_name_t));
454 }
455
456 node->magic = 0;
457 isc_mem_put(mctx, node, sizeof(dns_sdlznode_t));
458 db = &sdlz->common;
459 dns_db_detach(&db);
460 }
461
462 static isc_result_t
463 getnodedata(dns_db_t *db, const dns_name_t *name, bool create,
464 unsigned int options, dns_clientinfomethods_t *methods,
465 dns_clientinfo_t *clientinfo, dns_dbnode_t **nodep) {
466 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
467 dns_sdlznode_t *node = NULL;
468 isc_result_t result;
469 isc_buffer_t b;
470 char namestr[DNS_NAME_MAXTEXT + 1];
471 isc_buffer_t b2;
472 char zonestr[DNS_NAME_MAXTEXT + 1];
473 bool isorigin;
474 dns_sdlzauthorityfunc_t authority;
475
476 REQUIRE(VALID_SDLZDB(sdlz));
477 REQUIRE(nodep != NULL && *nodep == NULL);
478
479 if (sdlz->dlzimp->methods->newversion == NULL) {
480 REQUIRE(!create);
481 }
482
483 isc_buffer_init(&b, namestr, sizeof(namestr));
484 if ((sdlz->dlzimp->flags & DNS_SDLZFLAG_RELATIVEOWNER) != 0) {
485 dns_name_t relname;
486 unsigned int labels;
487
488 labels = dns_name_countlabels(name) -
489 dns_name_countlabels(&sdlz->common.origin);
490 dns_name_init(&relname, NULL);
491 dns_name_getlabelsequence(name, 0, labels, &relname);
492 result = dns_name_totext(&relname, DNS_NAME_OMITFINALDOT, &b);
493 if (result != ISC_R_SUCCESS) {
494 return result;
495 }
496 } else {
497 result = dns_name_totext(name, DNS_NAME_OMITFINALDOT, &b);
498 if (result != ISC_R_SUCCESS) {
499 return result;
500 }
501 }
502 isc_buffer_putuint8(&b, 0);
503
504 isc_buffer_init(&b2, zonestr, sizeof(zonestr));
505 result = dns_name_totext(&sdlz->common.origin, DNS_NAME_OMITFINALDOT,
506 &b2);
507 if (result != ISC_R_SUCCESS) {
508 return result;
509 }
510 isc_buffer_putuint8(&b2, 0);
511
512 result = createnode(sdlz, &node);
513 if (result != ISC_R_SUCCESS) {
514 return result;
515 }
516
517 isorigin = dns_name_equal(name, &sdlz->common.origin);
518
519 /* make sure strings are always lowercase */
520 isc_ascii_strtolower(zonestr);
521 isc_ascii_strtolower(namestr);
522
523 MAYBE_LOCK(sdlz->dlzimp);
524
525 /* try to lookup the host (namestr) */
526 result = sdlz->dlzimp->methods->lookup(
527 zonestr, namestr, sdlz->dlzimp->driverarg, sdlz->dbdata, node,
528 methods, clientinfo);
529
530 /*
531 * If the name was not found and DNS_DBFIND_NOWILD is not
532 * set, then we try to find a wildcard entry.
533 *
534 * If DNS_DBFIND_NOZONECUT is set and there are multiple
535 * levels between the host and the zone origin, we also look
536 * for wildcards at each level.
537 */
538 if (result == ISC_R_NOTFOUND && !create &&
539 (options & DNS_DBFIND_NOWILD) == 0)
540 {
541 unsigned int i, dlabels, nlabels;
542
543 nlabels = dns_name_countlabels(name);
544 dlabels = nlabels - dns_name_countlabels(&sdlz->common.origin);
545 for (i = 0; i < dlabels; i++) {
546 char wildstr[DNS_NAME_MAXTEXT + 1];
547 dns_fixedname_t fixed;
548 const dns_name_t *wild;
549
550 dns_fixedname_init(&fixed);
551 if (i == dlabels - 1) {
552 wild = dns_wildcardname;
553 } else {
554 dns_name_t *fname;
555 fname = dns_fixedname_name(&fixed);
556 dns_name_getlabelsequence(
557 name, i + 1, dlabels - i - 1, fname);
558 result = dns_name_concatenate(
559 dns_wildcardname, fname, fname, NULL);
560 if (result != ISC_R_SUCCESS) {
561 MAYBE_UNLOCK(sdlz->dlzimp);
562 return result;
563 }
564 wild = fname;
565 }
566
567 isc_buffer_init(&b, wildstr, sizeof(wildstr));
568 result = dns_name_totext(wild, DNS_NAME_OMITFINALDOT,
569 &b);
570 if (result != ISC_R_SUCCESS) {
571 MAYBE_UNLOCK(sdlz->dlzimp);
572 return result;
573 }
574 isc_buffer_putuint8(&b, 0);
575
576 result = sdlz->dlzimp->methods->lookup(
577 zonestr, wildstr, sdlz->dlzimp->driverarg,
578 sdlz->dbdata, node, methods, clientinfo);
579 if (result == ISC_R_SUCCESS) {
580 break;
581 }
582 }
583 }
584
585 MAYBE_UNLOCK(sdlz->dlzimp);
586
587 if (result == ISC_R_NOTFOUND && (isorigin || create)) {
588 result = ISC_R_SUCCESS;
589 }
590
591 if (result != ISC_R_SUCCESS) {
592 isc_refcount_decrementz(&node->references);
593 destroynode(node);
594 return result;
595 }
596
597 if (isorigin && sdlz->dlzimp->methods->authority != NULL) {
598 MAYBE_LOCK(sdlz->dlzimp);
599 authority = sdlz->dlzimp->methods->authority;
600 result = (*authority)(zonestr, sdlz->dlzimp->driverarg,
601 sdlz->dbdata, node);
602 MAYBE_UNLOCK(sdlz->dlzimp);
603 if (result != ISC_R_SUCCESS && result != ISC_R_NOTIMPLEMENTED) {
604 isc_refcount_decrementz(&node->references);
605 destroynode(node);
606 return result;
607 }
608 }
609
610 if (node->name == NULL) {
611 node->name = isc_mem_get(sdlz->common.mctx, sizeof(dns_name_t));
612 dns_name_init(node->name, NULL);
613 dns_name_dup(name, sdlz->common.mctx, node->name);
614 }
615
616 *nodep = node;
617 return ISC_R_SUCCESS;
618 }
619
620 static isc_result_t
621 findnodeext(dns_db_t *db, const dns_name_t *name, bool create,
622 dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo,
623 dns_dbnode_t **nodep DNS__DB_FLARG) {
624 return getnodedata(db, name, create, 0, methods, clientinfo, nodep);
625 }
626
627 static isc_result_t
628 findnode(dns_db_t *db, const dns_name_t *name, bool create,
629 dns_dbnode_t **nodep DNS__DB_FLARG) {
630 return getnodedata(db, name, create, 0, NULL, NULL, nodep);
631 }
632
633 static void
634 attachnode(dns_db_t *db, dns_dbnode_t *source,
635 dns_dbnode_t **targetp DNS__DB_FLARG) {
636 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
637 dns_sdlznode_t *node = (dns_sdlznode_t *)source;
638 uint_fast32_t refs;
639
640 REQUIRE(VALID_SDLZDB(sdlz));
641
642 UNUSED(sdlz);
643
644 refs = isc_refcount_increment(&node->references);
645 #if DNS_DB_NODETRACE
646 fprintf(stderr, "incr:node:%s:%s:%u:%p->references = %" PRIuFAST32 "\n",
647 func, file, line, node, refs + 1);
648 #else
649 UNUSED(refs);
650 #endif
651
652 *targetp = source;
653 }
654
655 static void
656 detachnode(dns_db_t *db, dns_dbnode_t **targetp DNS__DB_FLARG) {
657 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
658 dns_sdlznode_t *node;
659 uint_fast32_t refs;
660
661 REQUIRE(VALID_SDLZDB(sdlz));
662 REQUIRE(targetp != NULL && *targetp != NULL);
663
664 UNUSED(sdlz);
665
666 node = (dns_sdlznode_t *)(*targetp);
667 *targetp = NULL;
668
669 refs = isc_refcount_decrement(&node->references);
670 #if DNS_DB_NODETRACE
671 fprintf(stderr, "decr:node:%s:%s:%u:%p->references = %" PRIuFAST32 "\n",
672 func, file, line, node, refs - 1);
673 #else
674 UNUSED(refs);
675 #endif
676
677 if (refs == 1) {
678 destroynode(node);
679 }
680 }
681
682 static isc_result_t
683 createiterator(dns_db_t *db, unsigned int options,
684 dns_dbiterator_t **iteratorp) {
685 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
686 sdlz_dbiterator_t *sdlziter;
687 isc_result_t result;
688 isc_buffer_t b;
689 char zonestr[DNS_NAME_MAXTEXT + 1];
690
691 REQUIRE(VALID_SDLZDB(sdlz));
692
693 if (sdlz->dlzimp->methods->allnodes == NULL) {
694 return ISC_R_NOTIMPLEMENTED;
695 }
696
697 if ((options & DNS_DB_NSEC3ONLY) != 0 ||
698 (options & DNS_DB_NONSEC3) != 0)
699 {
700 return ISC_R_NOTIMPLEMENTED;
701 }
702
703 isc_buffer_init(&b, zonestr, sizeof(zonestr));
704 result = dns_name_totext(&sdlz->common.origin, DNS_NAME_OMITFINALDOT,
705 &b);
706 if (result != ISC_R_SUCCESS) {
707 return result;
708 }
709 isc_buffer_putuint8(&b, 0);
710
711 sdlziter = isc_mem_get(sdlz->common.mctx, sizeof(sdlz_dbiterator_t));
712
713 sdlziter->common.methods = &dbiterator_methods;
714 sdlziter->common.db = NULL;
715 dns_db_attach(db, &sdlziter->common.db);
716 sdlziter->common.relative_names = ((options & DNS_DB_RELATIVENAMES) !=
717 0);
718 sdlziter->common.magic = DNS_DBITERATOR_MAGIC;
719 ISC_LIST_INIT(sdlziter->nodelist);
720 sdlziter->current = NULL;
721 sdlziter->origin = NULL;
722
723 /* make sure strings are always lowercase */
724 isc_ascii_strtolower(zonestr);
725
726 MAYBE_LOCK(sdlz->dlzimp);
727 result = sdlz->dlzimp->methods->allnodes(
728 zonestr, sdlz->dlzimp->driverarg, sdlz->dbdata, sdlziter);
729 MAYBE_UNLOCK(sdlz->dlzimp);
730 if (result != ISC_R_SUCCESS) {
731 dns_dbiterator_t *iter = &sdlziter->common;
732 dbiterator_destroy(&iter DNS__DB_FILELINE);
733 return result;
734 }
735
736 if (sdlziter->origin != NULL) {
737 ISC_LIST_UNLINK(sdlziter->nodelist, sdlziter->origin, link);
738 ISC_LIST_PREPEND(sdlziter->nodelist, sdlziter->origin, link);
739 }
740
741 *iteratorp = (dns_dbiterator_t *)sdlziter;
742
743 return ISC_R_SUCCESS;
744 }
745
746 static isc_result_t
747 findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
748 dns_rdatatype_t type, dns_rdatatype_t covers, isc_stdtime_t now,
749 dns_rdataset_t *rdataset,
750 dns_rdataset_t *sigrdataset DNS__DB_FLARG) {
751 REQUIRE(VALID_SDLZNODE(node));
752 dns_rdatalist_t *list;
753 dns_sdlznode_t *sdlznode = (dns_sdlznode_t *)node;
754
755 UNUSED(db);
756 UNUSED(version);
757 UNUSED(covers);
758 UNUSED(now);
759 UNUSED(sigrdataset);
760
761 if (type == dns_rdatatype_sig || type == dns_rdatatype_rrsig) {
762 return ISC_R_NOTIMPLEMENTED;
763 }
764
765 list = ISC_LIST_HEAD(sdlznode->lists);
766 while (list != NULL) {
767 if (list->type == type) {
768 break;
769 }
770 list = ISC_LIST_NEXT(list, link);
771 }
772 if (list == NULL) {
773 return ISC_R_NOTFOUND;
774 }
775
776 list_tordataset(list, db, node, rdataset);
777
778 return ISC_R_SUCCESS;
779 }
780
781 static isc_result_t
782 findext(dns_db_t *db, const dns_name_t *name, dns_dbversion_t *version,
783 dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
784 dns_dbnode_t **nodep, dns_name_t *foundname,
785 dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo,
786 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset DNS__DB_FLARG) {
787 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
788 dns_dbnode_t *node = NULL;
789 dns_fixedname_t fname;
790 dns_rdataset_t xrdataset;
791 dns_name_t *xname;
792 unsigned int nlabels, olabels;
793 isc_result_t result;
794 unsigned int i;
795
796 REQUIRE(VALID_SDLZDB(sdlz));
797 REQUIRE(nodep == NULL || *nodep == NULL);
798 REQUIRE(version == NULL || version == (void *)&sdlz->dummy_version ||
799 version == sdlz->future_version);
800
801 UNUSED(sdlz);
802
803 if (!dns_name_issubdomain(name, &db->origin)) {
804 return DNS_R_NXDOMAIN;
805 }
806
807 olabels = dns_name_countlabels(&db->origin);
808 nlabels = dns_name_countlabels(name);
809
810 xname = dns_fixedname_initname(&fname);
811
812 if (rdataset == NULL) {
813 dns_rdataset_init(&xrdataset);
814 rdataset = &xrdataset;
815 }
816
817 result = DNS_R_NXDOMAIN;
818
819 /*
820 * If we're not walking down searching for zone
821 * cuts, we can cut straight to the chase
822 */
823 if ((options & DNS_DBFIND_NOZONECUT) != 0) {
824 i = nlabels;
825 goto search;
826 }
827
828 for (i = olabels; i <= nlabels; i++) {
829 search:
830 /*
831 * Look up the next label.
832 */
833 dns_name_getlabelsequence(name, nlabels - i, i, xname);
834 result = getnodedata(db, xname, false, options, methods,
835 clientinfo, &node);
836 if (result == ISC_R_NOTFOUND) {
837 result = DNS_R_NXDOMAIN;
838 continue;
839 } else if (result != ISC_R_SUCCESS) {
840 break;
841 }
842
843 /*
844 * Look for a DNAME at the current label, unless this is
845 * the qname.
846 */
847 if (i < nlabels) {
848 result = findrdataset(
849 db, node, version, dns_rdatatype_dname, 0, now,
850 rdataset, sigrdataset DNS__DB_FLARG_PASS);
851 if (result == ISC_R_SUCCESS) {
852 result = DNS_R_DNAME;
853 break;
854 }
855 }
856
857 /*
858 * Look for an NS at the current label, unless this is the
859 * origin, glue is ok, or there are known to be no zone cuts.
860 */
861 if (i != olabels && (options & DNS_DBFIND_GLUEOK) == 0 &&
862 (options & DNS_DBFIND_NOZONECUT) == 0)
863 {
864 result = findrdataset(
865 db, node, version, dns_rdatatype_ns, 0, now,
866 rdataset, sigrdataset DNS__DB_FLARG_PASS);
867
868 if (result == ISC_R_SUCCESS && i == nlabels &&
869 type == dns_rdatatype_any)
870 {
871 result = DNS_R_ZONECUT;
872 dns_rdataset_disassociate(rdataset);
873 if (sigrdataset != NULL &&
874 dns_rdataset_isassociated(sigrdataset))
875 {
876 dns_rdataset_disassociate(sigrdataset);
877 }
878 break;
879 } else if (result == ISC_R_SUCCESS) {
880 result = DNS_R_DELEGATION;
881 break;
882 }
883 }
884
885 /*
886 * If the current name is not the qname, add another label
887 * and try again.
888 */
889 if (i < nlabels) {
890 detachnode(db, &node DNS__DB_FLARG_PASS);
891 node = NULL;
892 continue;
893 }
894
895 /*
896 * If we're looking for ANY, we're done.
897 */
898 if (type == dns_rdatatype_any) {
899 result = ISC_R_SUCCESS;
900 break;
901 }
902
903 /*
904 * Look for the qtype.
905 */
906 result = findrdataset(db, node, version, type, 0, now, rdataset,
907 sigrdataset DNS__DB_FLARG_PASS);
908 if (result == ISC_R_SUCCESS) {
909 break;
910 }
911
912 /*
913 * Look for a CNAME
914 */
915 if (type != dns_rdatatype_cname) {
916 result = findrdataset(
917 db, node, version, dns_rdatatype_cname, 0, now,
918 rdataset, sigrdataset DNS__DB_FLARG_PASS);
919 if (result == ISC_R_SUCCESS) {
920 result = DNS_R_CNAME;
921 break;
922 }
923 }
924
925 result = DNS_R_NXRRSET;
926 break;
927 }
928
929 if (rdataset == &xrdataset && dns_rdataset_isassociated(rdataset)) {
930 dns_rdataset_disassociate(rdataset);
931 }
932
933 if (foundname != NULL) {
934 dns_name_copy(xname, foundname);
935 }
936
937 if (nodep != NULL) {
938 *nodep = node;
939 } else if (node != NULL) {
940 detachnode(db, &node DNS__DB_FLARG_PASS);
941 }
942
943 return result;
944 }
945
946 static isc_result_t
947 find(dns_db_t *db, const dns_name_t *name, dns_dbversion_t *version,
948 dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
949 dns_dbnode_t **nodep, dns_name_t *foundname, dns_rdataset_t *rdataset,
950 dns_rdataset_t *sigrdataset DNS__DB_FLARG) {
951 return findext(db, name, version, type, options, now, nodep, foundname,
952 NULL, NULL, rdataset, sigrdataset DNS__DB_FLARG_PASS);
953 }
954
955 static isc_result_t
956 allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
957 unsigned int options, isc_stdtime_t now,
958 dns_rdatasetiter_t **iteratorp DNS__DB_FLARG) {
959 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
960 sdlz_rdatasetiter_t *iterator;
961
962 REQUIRE(VALID_SDLZDB(sdlz));
963
964 REQUIRE(version == NULL || version == (void *)&sdlz->dummy_version ||
965 version == sdlz->future_version);
966
967 UNUSED(version);
968 UNUSED(now);
969
970 iterator = isc_mem_get(db->mctx, sizeof(sdlz_rdatasetiter_t));
971
972 iterator->common.magic = DNS_RDATASETITER_MAGIC;
973 iterator->common.methods = &rdatasetiter_methods;
974 iterator->common.db = db;
975 iterator->common.node = NULL;
976 attachnode(db, node, &iterator->common.node DNS__DB_FLARG_PASS);
977 iterator->common.version = version;
978 iterator->common.options = options;
979 iterator->common.now = now;
980
981 *iteratorp = (dns_rdatasetiter_t *)iterator;
982
983 return ISC_R_SUCCESS;
984 }
985
986 static isc_result_t
987 modrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
988 dns_rdataset_t *rdataset, unsigned int options,
989 dns_sdlzmodrdataset_t mod_function) {
990 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
991 dns_master_style_t *style = NULL;
992 isc_result_t result;
993 isc_buffer_t *buffer = NULL;
994 isc_mem_t *mctx;
995 dns_sdlznode_t *sdlznode;
996 char *rdatastr = NULL;
997 char name[DNS_NAME_MAXTEXT + 1];
998
999 REQUIRE(VALID_SDLZDB(sdlz));
1000
1001 if (mod_function == NULL) {
1002 return ISC_R_NOTIMPLEMENTED;
1003 }
1004
1005 sdlznode = (dns_sdlznode_t *)node;
1006
1007 UNUSED(options);
1008
1009 dns_name_format(sdlznode->name, name, sizeof(name));
1010
1011 mctx = sdlz->common.mctx;
1012
1013 isc_buffer_allocate(mctx, &buffer, 1024);
1014
1015 result = dns_master_stylecreate(&style, 0, 0, 0, 0, 0, 0, 1, 0xffffffff,
1016 mctx);
1017 if (result != ISC_R_SUCCESS) {
1018 goto cleanup;
1019 }
1020
1021 result = dns_master_rdatasettotext(sdlznode->name, rdataset, style,
1022 NULL, buffer);
1023 if (result != ISC_R_SUCCESS) {
1024 goto cleanup;
1025 }
1026
1027 if (isc_buffer_usedlength(buffer) < 1) {
1028 result = ISC_R_BADADDRESSFORM;
1029 goto cleanup;
1030 }
1031
1032 rdatastr = isc_buffer_base(buffer);
1033 if (rdatastr == NULL) {
1034 result = ISC_R_NOMEMORY;
1035 goto cleanup;
1036 }
1037 rdatastr[isc_buffer_usedlength(buffer) - 1] = 0;
1038
1039 MAYBE_LOCK(sdlz->dlzimp);
1040 result = mod_function(name, rdatastr, sdlz->dlzimp->driverarg,
1041 sdlz->dbdata, version);
1042 MAYBE_UNLOCK(sdlz->dlzimp);
1043
1044 cleanup:
1045 isc_buffer_free(&buffer);
1046 if (style != NULL) {
1047 dns_master_styledestroy(&style, mctx);
1048 }
1049
1050 return result;
1051 }
1052
1053 static isc_result_t
1054 addrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
1055 isc_stdtime_t now, dns_rdataset_t *rdataset, unsigned int options,
1056 dns_rdataset_t *addedrdataset DNS__DB_FLARG) {
1057 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
1058 isc_result_t result;
1059
1060 UNUSED(now);
1061 UNUSED(addedrdataset);
1062 REQUIRE(VALID_SDLZDB(sdlz));
1063
1064 if (sdlz->dlzimp->methods->addrdataset == NULL) {
1065 return ISC_R_NOTIMPLEMENTED;
1066 }
1067
1068 result = modrdataset(db, node, version, rdataset, options,
1069 sdlz->dlzimp->methods->addrdataset);
1070 return result;
1071 }
1072
1073 static isc_result_t
1074 subtractrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
1075 dns_rdataset_t *rdataset, unsigned int options,
1076 dns_rdataset_t *newrdataset DNS__DB_FLARG) {
1077 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
1078 isc_result_t result;
1079
1080 UNUSED(newrdataset);
1081 REQUIRE(VALID_SDLZDB(sdlz));
1082
1083 if (sdlz->dlzimp->methods->subtractrdataset == NULL) {
1084 return ISC_R_NOTIMPLEMENTED;
1085 }
1086
1087 result = modrdataset(db, node, version, rdataset, options,
1088 sdlz->dlzimp->methods->subtractrdataset);
1089 return result;
1090 }
1091
1092 static isc_result_t
1093 deleterdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
1094 dns_rdatatype_t type, dns_rdatatype_t covers DNS__DB_FLARG) {
1095 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
1096 char name[DNS_NAME_MAXTEXT + 1];
1097 char b_type[DNS_RDATATYPE_FORMATSIZE];
1098 dns_sdlznode_t *sdlznode;
1099 isc_result_t result;
1100
1101 UNUSED(covers);
1102
1103 REQUIRE(VALID_SDLZDB(sdlz));
1104
1105 if (sdlz->dlzimp->methods->delrdataset == NULL) {
1106 return ISC_R_NOTIMPLEMENTED;
1107 }
1108
1109 sdlznode = (dns_sdlznode_t *)node;
1110 dns_name_format(sdlznode->name, name, sizeof(name));
1111 dns_rdatatype_format(type, b_type, sizeof(b_type));
1112
1113 MAYBE_LOCK(sdlz->dlzimp);
1114 result = sdlz->dlzimp->methods->delrdataset(
1115 name, b_type, sdlz->dlzimp->driverarg, sdlz->dbdata, version);
1116 MAYBE_UNLOCK(sdlz->dlzimp);
1117
1118 return result;
1119 }
1120
1121 static bool
1122 issecure(dns_db_t *db) {
1123 UNUSED(db);
1124
1125 return false;
1126 }
1127
1128 static unsigned int
1129 nodecount(dns_db_t *db, dns_dbtree_t tree) {
1130 UNUSED(db);
1131 UNUSED(tree);
1132
1133 return 0;
1134 }
1135
1136 static void
1137 setloop(dns_db_t *db, isc_loop_t *loop) {
1138 UNUSED(db);
1139 UNUSED(loop);
1140 }
1141
1142 /*
1143 * getoriginnode() is used by the update code to find the
1144 * dns_rdatatype_dnskey record for a zone
1145 */
1146 static isc_result_t
1147 getoriginnode(dns_db_t *db, dns_dbnode_t **nodep DNS__DB_FLARG) {
1148 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)db;
1149 isc_result_t result;
1150
1151 REQUIRE(VALID_SDLZDB(sdlz));
1152 if (sdlz->dlzimp->methods->newversion == NULL) {
1153 return ISC_R_NOTIMPLEMENTED;
1154 }
1155
1156 result = getnodedata(db, &sdlz->common.origin, false, 0, NULL, NULL,
1157 nodep);
1158 if (result != ISC_R_SUCCESS) {
1159 sdlz_log(ISC_LOG_ERROR, "sdlz getoriginnode failed: %s",
1160 isc_result_totext(result));
1161 }
1162 return result;
1163 }
1164
1165 static dns_dbmethods_t sdlzdb_methods = {
1166 .destroy = destroy,
1167 .currentversion = currentversion,
1168 .newversion = newversion,
1169 .attachversion = attachversion,
1170 .closeversion = closeversion,
1171 .findnode = findnode,
1172 .find = find,
1173 .attachnode = attachnode,
1174 .detachnode = detachnode,
1175 .createiterator = createiterator,
1176 .findrdataset = findrdataset,
1177 .allrdatasets = allrdatasets,
1178 .addrdataset = addrdataset,
1179 .subtractrdataset = subtractrdataset,
1180 .deleterdataset = deleterdataset,
1181 .issecure = issecure,
1182 .nodecount = nodecount,
1183 .setloop = setloop,
1184 .getoriginnode = getoriginnode,
1185 .findnodeext = findnodeext,
1186 .findext = findext,
1187 };
1188
1189 /*
1190 * Database Iterator Methods. These methods were "borrowed" from the SDB
1191 * driver interface. See the SDB driver interface documentation for more info.
1192 */
1193
1194 static void
1195 dbiterator_destroy(dns_dbiterator_t **iteratorp DNS__DB_FLARG) {
1196 sdlz_dbiterator_t *sdlziter = (sdlz_dbiterator_t *)(*iteratorp);
1197 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)sdlziter->common.db;
1198
1199 while (!ISC_LIST_EMPTY(sdlziter->nodelist)) {
1200 dns_sdlznode_t *node;
1201 node = ISC_LIST_HEAD(sdlziter->nodelist);
1202 ISC_LIST_UNLINK(sdlziter->nodelist, node, link);
1203 isc_refcount_decrementz(&node->references);
1204 destroynode(node);
1205 }
1206
1207 dns_db_detach(&sdlziter->common.db);
1208 isc_mem_put(sdlz->common.mctx, sdlziter, sizeof(sdlz_dbiterator_t));
1209
1210 *iteratorp = NULL;
1211 }
1212
1213 static isc_result_t
1214 dbiterator_first(dns_dbiterator_t *iterator DNS__DB_FLARG) {
1215 sdlz_dbiterator_t *sdlziter = (sdlz_dbiterator_t *)iterator;
1216
1217 sdlziter->current = ISC_LIST_HEAD(sdlziter->nodelist);
1218 if (sdlziter->current == NULL) {
1219 return ISC_R_NOMORE;
1220 } else {
1221 return ISC_R_SUCCESS;
1222 }
1223 }
1224
1225 static isc_result_t
1226 dbiterator_last(dns_dbiterator_t *iterator DNS__DB_FLARG) {
1227 sdlz_dbiterator_t *sdlziter = (sdlz_dbiterator_t *)iterator;
1228
1229 sdlziter->current = ISC_LIST_TAIL(sdlziter->nodelist);
1230 if (sdlziter->current == NULL) {
1231 return ISC_R_NOMORE;
1232 } else {
1233 return ISC_R_SUCCESS;
1234 }
1235 }
1236
1237 static isc_result_t
1238 dbiterator_seek(dns_dbiterator_t *iterator,
1239 const dns_name_t *name DNS__DB_FLARG) {
1240 sdlz_dbiterator_t *sdlziter = (sdlz_dbiterator_t *)iterator;
1241
1242 sdlziter->current = ISC_LIST_HEAD(sdlziter->nodelist);
1243 while (sdlziter->current != NULL) {
1244 if (dns_name_equal(sdlziter->current->name, name)) {
1245 return ISC_R_SUCCESS;
1246 }
1247 sdlziter->current = ISC_LIST_NEXT(sdlziter->current, link);
1248 }
1249 return ISC_R_NOTFOUND;
1250 }
1251
1252 static isc_result_t
1253 dbiterator_seek3(dns_dbiterator_t *iterator ISC_ATTR_UNUSED,
1254 const dns_name_t *name ISC_ATTR_UNUSED DNS__DB_FLARG) {
1255 return ISC_R_NOTIMPLEMENTED;
1256 }
1257
1258 static isc_result_t
1259 dbiterator_prev(dns_dbiterator_t *iterator DNS__DB_FLARG) {
1260 sdlz_dbiterator_t *sdlziter = (sdlz_dbiterator_t *)iterator;
1261
1262 sdlziter->current = ISC_LIST_PREV(sdlziter->current, link);
1263 if (sdlziter->current == NULL) {
1264 return ISC_R_NOMORE;
1265 } else {
1266 return ISC_R_SUCCESS;
1267 }
1268 }
1269
1270 static isc_result_t
1271 dbiterator_next(dns_dbiterator_t *iterator DNS__DB_FLARG) {
1272 sdlz_dbiterator_t *sdlziter = (sdlz_dbiterator_t *)iterator;
1273
1274 sdlziter->current = ISC_LIST_NEXT(sdlziter->current, link);
1275 if (sdlziter->current == NULL) {
1276 return ISC_R_NOMORE;
1277 } else {
1278 return ISC_R_SUCCESS;
1279 }
1280 }
1281
1282 static isc_result_t
1283 dbiterator_current(dns_dbiterator_t *iterator, dns_dbnode_t **nodep,
1284 dns_name_t *name DNS__DB_FLARG) {
1285 sdlz_dbiterator_t *sdlziter = (sdlz_dbiterator_t *)iterator;
1286
1287 attachnode(iterator->db, sdlziter->current, nodep DNS__DB_FLARG_PASS);
1288 if (name != NULL) {
1289 dns_name_copy(sdlziter->current->name, name);
1290 return ISC_R_SUCCESS;
1291 }
1292 return ISC_R_SUCCESS;
1293 }
1294
1295 static isc_result_t
1296 dbiterator_pause(dns_dbiterator_t *iterator) {
1297 UNUSED(iterator);
1298 return ISC_R_SUCCESS;
1299 }
1300
1301 static isc_result_t
1302 dbiterator_origin(dns_dbiterator_t *iterator, dns_name_t *name) {
1303 UNUSED(iterator);
1304 dns_name_copy(dns_rootname, name);
1305 return ISC_R_SUCCESS;
1306 }
1307
1308 /*
1309 * Rdataset Methods. These methods were "borrowed" from the SDB driver
1310 * interface. See the SDB driver interface documentation for more info.
1311 */
1312
1313 static void
1314 disassociate(dns_rdataset_t *rdataset DNS__DB_FLARG) {
1315 dns_dbnode_t *node = rdataset->rdlist.node;
1316 dns_sdlznode_t *sdlznode = (dns_sdlznode_t *)node;
1317 dns_db_t *db = (dns_db_t *)sdlznode->sdlz;
1318
1319 detachnode(db, &node DNS__DB_FLARG_PASS);
1320 dns_rdatalist_disassociate(rdataset DNS__DB_FLARG_PASS);
1321 }
1322
1323 static void
1324 rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target DNS__DB_FLARG) {
1325 dns_dbnode_t *node = source->rdlist.node;
1326 dns_sdlznode_t *sdlznode = (dns_sdlznode_t *)node;
1327 dns_db_t *db = (dns_db_t *)sdlznode->sdlz;
1328
1329 dns_rdatalist_clone(source, target DNS__DB_FLARG_PASS);
1330 attachnode(db, node, &target->rdlist.node DNS__DB_FLARG_PASS);
1331 }
1332
1333 static dns_rdatasetmethods_t rdataset_methods = {
1334 .disassociate = disassociate,
1335 .first = dns_rdatalist_first,
1336 .next = dns_rdatalist_next,
1337 .current = dns_rdatalist_current,
1338 .clone = rdataset_clone,
1339 .count = dns_rdatalist_count,
1340 .addnoqname = dns_rdatalist_addnoqname,
1341 .getnoqname = dns_rdatalist_getnoqname,
1342 };
1343
1344 static void
1345 list_tordataset(dns_rdatalist_t *rdatalist, dns_db_t *db, dns_dbnode_t *node,
1346 dns_rdataset_t *rdataset) {
1347 /*
1348 * The sdlz rdataset is an rdatalist, but additionally holds
1349 * a database node reference.
1350 */
1351
1352 dns_rdatalist_tordataset(rdatalist, rdataset);
1353 rdataset->methods = &rdataset_methods;
1354 dns_db_attachnode(db, node, &rdataset->rdlist.node);
1355 }
1356
1357 /*
1358 * SDLZ core methods. This is the core of the new DLZ functionality.
1359 */
1360
1361 /*%
1362 * Build a 'bind' database driver structure to be returned by
1363 * either the find zone or the allow zone transfer method.
1364 * This method is only available in this source file, it is
1365 * not made available anywhere else.
1366 */
1367
1368 static isc_result_t
1369 dns_sdlzcreateDBP(isc_mem_t *mctx, void *driverarg, void *dbdata,
1370 const dns_name_t *name, dns_rdataclass_t rdclass,
1371 dns_db_t **dbp) {
1372 dns_sdlz_db_t *sdlzdb;
1373 dns_sdlzimplementation_t *imp;
1374
1375 /* check that things are as we expect */
1376 REQUIRE(dbp != NULL && *dbp == NULL);
1377 REQUIRE(name != NULL);
1378
1379 imp = (dns_sdlzimplementation_t *)driverarg;
1380
1381 /* allocate and zero memory for driver structure */
1382 sdlzdb = isc_mem_get(mctx, sizeof(*sdlzdb));
1383
1384 *sdlzdb = (dns_sdlz_db_t) {
1385 .dlzimp = imp,
1386 .common = { .methods = &sdlzdb_methods,
1387 .rdclass = rdclass, },
1388 .dbdata = dbdata,
1389 };
1390
1391 /* initialize and set origin */
1392 dns_name_init(&sdlzdb->common.origin, NULL);
1393 dns_name_dupwithoffsets(name, mctx, &sdlzdb->common.origin);
1394
1395 isc_refcount_init(&sdlzdb->common.references, 1);
1396
1397 /* attach to the memory context */
1398 isc_mem_attach(mctx, &sdlzdb->common.mctx);
1399
1400 /* mark structure as valid */
1401 sdlzdb->common.magic = DNS_DB_MAGIC;
1402 sdlzdb->common.impmagic = SDLZDB_MAGIC;
1403 *dbp = (dns_db_t *)sdlzdb;
1404
1405 return ISC_R_SUCCESS;
1406 }
1407
1408 static isc_result_t
1409 dns_sdlzallowzonexfr(void *driverarg, void *dbdata, isc_mem_t *mctx,
1410 dns_rdataclass_t rdclass, const dns_name_t *name,
1411 const isc_sockaddr_t *clientaddr, dns_db_t **dbp) {
1412 isc_buffer_t b;
1413 isc_buffer_t b2;
1414 char namestr[DNS_NAME_MAXTEXT + 1];
1415 char clientstr[(sizeof "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255."
1416 "255") +
1417 1];
1418 isc_netaddr_t netaddr;
1419 isc_result_t result;
1420 dns_sdlzimplementation_t *imp;
1421
1422 /*
1423 * Perform checks to make sure data is as we expect it to be.
1424 */
1425 REQUIRE(driverarg != NULL);
1426 REQUIRE(name != NULL);
1427 REQUIRE(clientaddr != NULL);
1428 REQUIRE(dbp != NULL && *dbp == NULL);
1429
1430 imp = (dns_sdlzimplementation_t *)driverarg;
1431
1432 /* Convert DNS name to ascii text */
1433 isc_buffer_init(&b, namestr, sizeof(namestr));
1434 result = dns_name_totext(name, DNS_NAME_OMITFINALDOT, &b);
1435 if (result != ISC_R_SUCCESS) {
1436 return result;
1437 }
1438 isc_buffer_putuint8(&b, 0);
1439
1440 /* convert client address to ascii text */
1441 isc_buffer_init(&b2, clientstr, sizeof(clientstr));
1442 isc_netaddr_fromsockaddr(&netaddr, clientaddr);
1443 result = isc_netaddr_totext(&netaddr, &b2);
1444 if (result != ISC_R_SUCCESS) {
1445 return result;
1446 }
1447 isc_buffer_putuint8(&b2, 0);
1448
1449 /* make sure strings are always lowercase */
1450 isc_ascii_strtolower(namestr);
1451 isc_ascii_strtolower(clientstr);
1452
1453 /* Call SDLZ driver's find zone method */
1454 if (imp->methods->allowzonexfr != NULL) {
1455 isc_result_t rresult = ISC_R_SUCCESS;
1456
1457 MAYBE_LOCK(imp);
1458 result = imp->methods->allowzonexfr(imp->driverarg, dbdata,
1459 namestr, clientstr);
1460 MAYBE_UNLOCK(imp);
1461 /*
1462 * if zone is supported and transfers are (or might be)
1463 * allowed, build a 'bind' database driver
1464 */
1465 if (result == ISC_R_SUCCESS || result == ISC_R_DEFAULT) {
1466 rresult = dns_sdlzcreateDBP(mctx, driverarg, dbdata,
1467 name, rdclass, dbp);
1468 }
1469 if (rresult != ISC_R_SUCCESS) {
1470 result = rresult;
1471 }
1472 return result;
1473 }
1474
1475 return ISC_R_NOTIMPLEMENTED;
1476 }
1477
1478 static isc_result_t
1479 dns_sdlzcreate(isc_mem_t *mctx, const char *dlzname, unsigned int argc,
1480 char *argv[], void *driverarg, void **dbdata) {
1481 dns_sdlzimplementation_t *imp;
1482 isc_result_t result = ISC_R_NOTFOUND;
1483
1484 /* Write debugging message to log */
1485 sdlz_log(ISC_LOG_DEBUG(2), "Loading SDLZ driver.");
1486
1487 /*
1488 * Performs checks to make sure data is as we expect it to be.
1489 */
1490 REQUIRE(driverarg != NULL);
1491 REQUIRE(dlzname != NULL);
1492 REQUIRE(dbdata != NULL);
1493 UNUSED(mctx);
1494
1495 imp = driverarg;
1496
1497 /* If the create method exists, call it. */
1498 if (imp->methods->create != NULL) {
1499 MAYBE_LOCK(imp);
1500 result = imp->methods->create(dlzname, argc, argv,
1501 imp->driverarg, dbdata);
1502 MAYBE_UNLOCK(imp);
1503 }
1504
1505 /* Write debugging message to log */
1506 if (result == ISC_R_SUCCESS) {
1507 sdlz_log(ISC_LOG_DEBUG(2), "SDLZ driver loaded successfully.");
1508 } else {
1509 sdlz_log(ISC_LOG_ERROR, "SDLZ driver failed to load.");
1510 }
1511
1512 return result;
1513 }
1514
1515 static void
1516 dns_sdlzdestroy(void *driverdata, void **dbdata) {
1517 dns_sdlzimplementation_t *imp;
1518
1519 /* Write debugging message to log */
1520 sdlz_log(ISC_LOG_DEBUG(2), "Unloading SDLZ driver.");
1521
1522 imp = driverdata;
1523
1524 /* If the destroy method exists, call it. */
1525 if (imp->methods->destroy != NULL) {
1526 MAYBE_LOCK(imp);
1527 imp->methods->destroy(imp->driverarg, dbdata);
1528 MAYBE_UNLOCK(imp);
1529 }
1530 }
1531
1532 static isc_result_t
1533 dns_sdlzfindzone(void *driverarg, void *dbdata, isc_mem_t *mctx,
1534 dns_rdataclass_t rdclass, const dns_name_t *name,
1535 dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo,
1536 dns_db_t **dbp) {
1537 isc_buffer_t b;
1538 char namestr[DNS_NAME_MAXTEXT + 1];
1539 isc_result_t result;
1540 dns_sdlzimplementation_t *imp;
1541
1542 /*
1543 * Perform checks to make sure data is as we expect it to be.
1544 */
1545 REQUIRE(driverarg != NULL);
1546 REQUIRE(name != NULL);
1547 REQUIRE(dbp != NULL && *dbp == NULL);
1548
1549 imp = (dns_sdlzimplementation_t *)driverarg;
1550
1551 /* Convert DNS name to ascii text */
1552 isc_buffer_init(&b, namestr, sizeof(namestr));
1553 result = dns_name_totext(name, DNS_NAME_OMITFINALDOT, &b);
1554 if (result != ISC_R_SUCCESS) {
1555 return result;
1556 }
1557 isc_buffer_putuint8(&b, 0);
1558
1559 /* make sure strings are always lowercase */
1560 isc_ascii_strtolower(namestr);
1561
1562 /* Call SDLZ driver's find zone method */
1563 MAYBE_LOCK(imp);
1564 result = imp->methods->findzone(imp->driverarg, dbdata, namestr,
1565 methods, clientinfo);
1566 MAYBE_UNLOCK(imp);
1567
1568 /*
1569 * if zone is supported build a 'bind' database driver
1570 * structure to return
1571 */
1572 if (result == ISC_R_SUCCESS) {
1573 result = dns_sdlzcreateDBP(mctx, driverarg, dbdata, name,
1574 rdclass, dbp);
1575 }
1576
1577 return result;
1578 }
1579
1580 static isc_result_t
1581 dns_sdlzconfigure(void *driverarg, void *dbdata, dns_view_t *view,
1582 dns_dlzdb_t *dlzdb) {
1583 isc_result_t result;
1584 dns_sdlzimplementation_t *imp;
1585
1586 REQUIRE(driverarg != NULL);
1587
1588 imp = (dns_sdlzimplementation_t *)driverarg;
1589
1590 /* Call SDLZ driver's configure method */
1591 if (imp->methods->configure != NULL) {
1592 MAYBE_LOCK(imp);
1593 result = imp->methods->configure(view, dlzdb, imp->driverarg,
1594 dbdata);
1595 MAYBE_UNLOCK(imp);
1596 } else {
1597 result = ISC_R_SUCCESS;
1598 }
1599
1600 return result;
1601 }
1602
1603 static bool
1604 dns_sdlzssumatch(const dns_name_t *signer, const dns_name_t *name,
1605 const isc_netaddr_t *tcpaddr, dns_rdatatype_t type,
1606 const dst_key_t *key, void *driverarg, void *dbdata) {
1607 dns_sdlzimplementation_t *imp;
1608 char b_signer[DNS_NAME_FORMATSIZE];
1609 char b_name[DNS_NAME_FORMATSIZE];
1610 char b_addr[ISC_NETADDR_FORMATSIZE];
1611 char b_type[DNS_RDATATYPE_FORMATSIZE];
1612 char b_key[DST_KEY_FORMATSIZE];
1613 isc_buffer_t *tkey_token = NULL;
1614 isc_region_t token_region = { NULL, 0 };
1615 uint32_t token_len = 0;
1616 bool ret;
1617
1618 REQUIRE(driverarg != NULL);
1619
1620 imp = (dns_sdlzimplementation_t *)driverarg;
1621 if (imp->methods->ssumatch == NULL) {
1622 return false;
1623 }
1624
1625 /*
1626 * Format the request elements. sdlz operates on strings, not
1627 * structures
1628 */
1629 if (signer != NULL) {
1630 dns_name_format(signer, b_signer, sizeof(b_signer));
1631 } else {
1632 b_signer[0] = 0;
1633 }
1634
1635 dns_name_format(name, b_name, sizeof(b_name));
1636
1637 if (tcpaddr != NULL) {
1638 isc_netaddr_format(tcpaddr, b_addr, sizeof(b_addr));
1639 } else {
1640 b_addr[0] = 0;
1641 }
1642
1643 dns_rdatatype_format(type, b_type, sizeof(b_type));
1644
1645 if (key != NULL) {
1646 dst_key_format(key, b_key, sizeof(b_key));
1647 tkey_token = dst_key_tkeytoken(key);
1648 } else {
1649 b_key[0] = 0;
1650 }
1651
1652 if (tkey_token != NULL) {
1653 isc_buffer_region(tkey_token, &token_region);
1654 token_len = token_region.length;
1655 }
1656
1657 MAYBE_LOCK(imp);
1658 ret = imp->methods->ssumatch(b_signer, b_name, b_addr, b_type, b_key,
1659 token_len,
1660 token_len != 0 ? token_region.base : NULL,
1661 imp->driverarg, dbdata);
1662 MAYBE_UNLOCK(imp);
1663 return ret;
1664 }
1665
1666 static dns_dlzmethods_t sdlzmethods = { dns_sdlzcreate, dns_sdlzdestroy,
1667 dns_sdlzfindzone, dns_sdlzallowzonexfr,
1668 dns_sdlzconfigure, dns_sdlzssumatch };
1669
1670 /*
1671 * Public functions.
1672 */
1673
1674 isc_result_t
1675 dns_sdlz_putrr(dns_sdlzlookup_t *lookup, const char *type, dns_ttl_t ttl,
1676 const char *data) {
1677 dns_rdatalist_t *rdatalist;
1678 dns_rdata_t *rdata;
1679 dns_rdatatype_t typeval;
1680 isc_consttextregion_t r;
1681 isc_buffer_t b;
1682 isc_buffer_t *rdatabuf = NULL;
1683 isc_lex_t *lex;
1684 isc_result_t result;
1685 unsigned int size;
1686 isc_mem_t *mctx;
1687 const dns_name_t *origin;
1688
1689 REQUIRE(VALID_SDLZLOOKUP(lookup));
1690 REQUIRE(type != NULL);
1691 REQUIRE(data != NULL);
1692
1693 mctx = lookup->sdlz->common.mctx;
1694
1695 r.base = type;
1696 r.length = strlen(type);
1697 result = dns_rdatatype_fromtext(&typeval, (void *)&r);
1698 if (result != ISC_R_SUCCESS) {
1699 return result;
1700 }
1701
1702 rdatalist = ISC_LIST_HEAD(lookup->lists);
1703 while (rdatalist != NULL) {
1704 if (rdatalist->type == typeval) {
1705 break;
1706 }
1707 rdatalist = ISC_LIST_NEXT(rdatalist, link);
1708 }
1709
1710 if (rdatalist == NULL) {
1711 rdatalist = isc_mem_get(mctx, sizeof(dns_rdatalist_t));
1712 dns_rdatalist_init(rdatalist);
1713 rdatalist->rdclass = lookup->sdlz->common.rdclass;
1714 rdatalist->type = typeval;
1715 rdatalist->ttl = ttl;
1716 ISC_LIST_APPEND(lookup->lists, rdatalist, link);
1717 } else if (rdatalist->ttl > ttl) {
1718 /*
1719 * BIND9 doesn't enforce all RRs in an RRset
1720 * having the same TTL, as per RFC 2136,
1721 * section 7.12. If a DLZ backend has
1722 * different TTLs, then the best
1723 * we can do is return the lowest.
1724 */
1725 rdatalist->ttl = ttl;
1726 }
1727
1728 rdata = isc_mem_get(mctx, sizeof(dns_rdata_t));
1729 dns_rdata_init(rdata);
1730
1731 if ((lookup->sdlz->dlzimp->flags & DNS_SDLZFLAG_RELATIVERDATA) != 0) {
1732 origin = &lookup->sdlz->common.origin;
1733 } else {
1734 origin = dns_rootname;
1735 }
1736
1737 lex = NULL;
1738 isc_lex_create(mctx, 64, &lex);
1739
1740 size = initial_size(data);
1741 do {
1742 isc_buffer_constinit(&b, data, strlen(data));
1743 isc_buffer_add(&b, strlen(data));
1744
1745 result = isc_lex_openbuffer(lex, &b);
1746 if (result != ISC_R_SUCCESS) {
1747 goto failure;
1748 }
1749
1750 rdatabuf = NULL;
1751 isc_buffer_allocate(mctx, &rdatabuf, size);
1752
1753 result = dns_rdata_fromtext(rdata, rdatalist->rdclass,
1754 rdatalist->type, lex, origin, false,
1755 mctx, rdatabuf, &lookup->callbacks);
1756 if (result != ISC_R_SUCCESS) {
1757 isc_buffer_free(&rdatabuf);
1758 }
1759 if (size >= 65535) {
1760 break;
1761 }
1762 size *= 2;
1763 if (size >= 65535) {
1764 size = 65535;
1765 }
1766 } while (result == ISC_R_NOSPACE);
1767
1768 if (result != ISC_R_SUCCESS) {
1769 result = DNS_R_SERVFAIL;
1770 goto failure;
1771 }
1772
1773 ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
1774 ISC_LIST_APPEND(lookup->buffers, rdatabuf, link);
1775
1776 if (lex != NULL) {
1777 isc_lex_destroy(&lex);
1778 }
1779
1780 return ISC_R_SUCCESS;
1781
1782 failure:
1783 if (rdatabuf != NULL) {
1784 isc_buffer_free(&rdatabuf);
1785 }
1786 if (lex != NULL) {
1787 isc_lex_destroy(&lex);
1788 }
1789 isc_mem_put(mctx, rdata, sizeof(dns_rdata_t));
1790
1791 return result;
1792 }
1793
1794 isc_result_t
1795 dns_sdlz_putnamedrr(dns_sdlzallnodes_t *allnodes, const char *name,
1796 const char *type, dns_ttl_t ttl, const char *data) {
1797 dns_name_t *newname;
1798 const dns_name_t *origin;
1799 dns_fixedname_t fnewname;
1800 dns_sdlz_db_t *sdlz = (dns_sdlz_db_t *)allnodes->common.db;
1801 dns_sdlznode_t *sdlznode;
1802 isc_mem_t *mctx = sdlz->common.mctx;
1803 isc_buffer_t b;
1804 isc_result_t result;
1805
1806 newname = dns_fixedname_initname(&fnewname);
1807
1808 if ((sdlz->dlzimp->flags & DNS_SDLZFLAG_RELATIVERDATA) != 0) {
1809 origin = &sdlz->common.origin;
1810 } else {
1811 origin = dns_rootname;
1812 }
1813 isc_buffer_constinit(&b, name, strlen(name));
1814 isc_buffer_add(&b, strlen(name));
1815
1816 result = dns_name_fromtext(newname, &b, origin, 0, NULL);
1817 if (result != ISC_R_SUCCESS) {
1818 return result;
1819 }
1820
1821 if (allnodes->common.relative_names) {
1822 /* All names are relative to the root */
1823 unsigned int nlabels = dns_name_countlabels(newname);
1824 dns_name_getlabelsequence(newname, 0, nlabels - 1, newname);
1825 }
1826
1827 sdlznode = ISC_LIST_HEAD(allnodes->nodelist);
1828 if (sdlznode == NULL || !dns_name_equal(sdlznode->name, newname)) {
1829 sdlznode = NULL;
1830 result = createnode(sdlz, &sdlznode);
1831 if (result != ISC_R_SUCCESS) {
1832 return result;
1833 }
1834 sdlznode->name = isc_mem_get(mctx, sizeof(dns_name_t));
1835 dns_name_init(sdlznode->name, NULL);
1836 dns_name_dup(newname, mctx, sdlznode->name);
1837 ISC_LIST_PREPEND(allnodes->nodelist, sdlznode, link);
1838 if (allnodes->origin == NULL &&
1839 dns_name_equal(newname, &sdlz->common.origin))
1840 {
1841 allnodes->origin = sdlznode;
1842 }
1843 }
1844 return dns_sdlz_putrr(sdlznode, type, ttl, data);
1845 }
1846
1847 isc_result_t
1848 dns_sdlz_putsoa(dns_sdlzlookup_t *lookup, const char *mname, const char *rname,
1849 uint32_t serial) {
1850 char str[2 * DNS_NAME_MAXTEXT + 5 * (sizeof("2147483647")) + 7];
1851 int n;
1852
1853 REQUIRE(mname != NULL);
1854 REQUIRE(rname != NULL);
1855
1856 n = snprintf(str, sizeof str, "%s %s %u %u %u %u %u", mname, rname,
1857 serial, SDLZ_DEFAULT_REFRESH, SDLZ_DEFAULT_RETRY,
1858 SDLZ_DEFAULT_EXPIRE, SDLZ_DEFAULT_MINIMUM);
1859 if (n >= (int)sizeof(str) || n < 0) {
1860 return ISC_R_NOSPACE;
1861 }
1862 return dns_sdlz_putrr(lookup, "SOA", SDLZ_DEFAULT_TTL, str);
1863 }
1864
1865 isc_result_t
1866 dns_sdlzregister(const char *drivername, const dns_sdlzmethods_t *methods,
1867 void *driverarg, unsigned int flags, isc_mem_t *mctx,
1868 dns_sdlzimplementation_t **sdlzimp) {
1869 dns_sdlzimplementation_t *imp;
1870 isc_result_t result;
1871
1872 /*
1873 * Performs checks to make sure data is as we expect it to be.
1874 */
1875 REQUIRE(drivername != NULL);
1876 REQUIRE(methods != NULL);
1877 REQUIRE(methods->findzone != NULL);
1878 REQUIRE(methods->lookup != NULL);
1879 REQUIRE(mctx != NULL);
1880 REQUIRE(sdlzimp != NULL && *sdlzimp == NULL);
1881 REQUIRE((flags &
1882 ~(DNS_SDLZFLAG_RELATIVEOWNER | DNS_SDLZFLAG_RELATIVERDATA |
1883 DNS_SDLZFLAG_THREADSAFE)) == 0);
1884
1885 /* Write debugging message to log */
1886 sdlz_log(ISC_LOG_DEBUG(2), "Registering SDLZ driver '%s'", drivername);
1887
1888 /*
1889 * Allocate memory for a sdlz_implementation object. Error if
1890 * we cannot.
1891 */
1892 imp = isc_mem_get(mctx, sizeof(*imp));
1893
1894 /* Store the data passed into this method */
1895 *imp = (dns_sdlzimplementation_t){
1896 .methods = methods,
1897 .driverarg = driverarg,
1898 .flags = flags,
1899 };
1900
1901 /* attach the new sdlz_implementation object to a memory context */
1902 isc_mem_attach(mctx, &imp->mctx);
1903
1904 /*
1905 * initialize the driver lock, error if we cannot
1906 * (used if a driver does not support multiple threads)
1907 */
1908 isc_mutex_init(&imp->driverlock);
1909
1910 /*
1911 * register the DLZ driver. Pass in our "extra" sdlz information as
1912 * a driverarg. (that's why we stored the passed in driver arg in our
1913 * sdlz_implementation structure) Also, store the dlz_implementation
1914 * structure in our sdlz_implementation.
1915 */
1916 result = dns_dlzregister(drivername, &sdlzmethods, imp, mctx,
1917 &imp->dlz_imp);
1918
1919 /* if registration fails, cleanup and get outta here. */
1920 if (result != ISC_R_SUCCESS) {
1921 goto cleanup_mutex;
1922 }
1923
1924 *sdlzimp = imp;
1925
1926 return ISC_R_SUCCESS;
1927
1928 cleanup_mutex:
1929 /* destroy the driver lock, we don't need it anymore */
1930 isc_mutex_destroy(&imp->driverlock);
1931
1932 /*
1933 * return the memory back to the available memory pool and
1934 * remove it from the memory context.
1935 */
1936 isc_mem_putanddetach(&imp->mctx, imp, sizeof(*imp));
1937 return result;
1938 }
1939
1940 void
1941 dns_sdlzunregister(dns_sdlzimplementation_t **sdlzimp) {
1942 dns_sdlzimplementation_t *imp;
1943
1944 /* Write debugging message to log */
1945 sdlz_log(ISC_LOG_DEBUG(2), "Unregistering SDLZ driver.");
1946
1947 /*
1948 * Performs checks to make sure data is as we expect it to be.
1949 */
1950 REQUIRE(sdlzimp != NULL && *sdlzimp != NULL);
1951
1952 imp = *sdlzimp;
1953 *sdlzimp = NULL;
1954
1955 /* Unregister the DLZ driver implementation */
1956 dns_dlzunregister(&imp->dlz_imp);
1957
1958 /* destroy the driver lock, we don't need it anymore */
1959 isc_mutex_destroy(&imp->driverlock);
1960
1961 /*
1962 * return the memory back to the available memory pool and
1963 * remove it from the memory context.
1964 */
1965 isc_mem_putanddetach(&imp->mctx, imp, sizeof(dns_sdlzimplementation_t));
1966 }
1967
1968 isc_result_t
1969 dns_sdlz_setdb(dns_dlzdb_t *dlzdatabase, dns_rdataclass_t rdclass,
1970 const dns_name_t *name, dns_db_t **dbp) {
1971 isc_result_t result;
1972
1973 result = dns_sdlzcreateDBP(dlzdatabase->mctx,
1974 dlzdatabase->implementation->driverarg,
1975 dlzdatabase->dbdata, name, rdclass, dbp);
1976 return result;
1977 }
1978