Coverage Report

Created: 2026-06-15 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/dns/dlz.c
Line
Count
Source
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0 AND ISC
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
/*
15
 * Copyright (C) 2002 Stichting NLnet, Netherlands, stichting@nlnet.nl.
16
 *
17
 * Permission to use, copy, modify, and distribute this software for any
18
 * purpose with or without fee is hereby granted, provided that the
19
 * above copyright notice and this permission notice appear in all
20
 * copies.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS" AND STICHTING NLNET
23
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
24
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
25
 * STICHTING NLNET BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
26
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
27
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
28
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
29
 * USE OR PERFORMANCE OF THIS SOFTWARE.
30
 *
31
 * The development of Dynamically Loadable Zones (DLZ) for Bind 9 was
32
 * conceived and contributed by Rob Butler.
33
 *
34
 * Permission to use, copy, modify, and distribute this software for any
35
 * purpose with or without fee is hereby granted, provided that the
36
 * above copyright notice and this permission notice appear in all
37
 * copies.
38
 *
39
 * THE SOFTWARE IS PROVIDED "AS IS" AND ROB BUTLER
40
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
42
 * ROB BUTLER BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
43
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
44
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
45
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
46
 * USE OR PERFORMANCE OF THIS SOFTWARE.
47
 */
48
49
/*! \file */
50
51
/***
52
 *** Imports
53
 ***/
54
55
#include <stdbool.h>
56
57
#include <isc/buffer.h>
58
#include <isc/commandline.h>
59
#include <isc/log.h>
60
#include <isc/magic.h>
61
#include <isc/mem.h>
62
#include <isc/netmgr.h>
63
#include <isc/random.h>
64
#include <isc/rwlock.h>
65
#include <isc/string.h>
66
#include <isc/util.h>
67
68
#include <dns/db.h>
69
#include <dns/dlz.h>
70
#include <dns/fixedname.h>
71
#include <dns/master.h>
72
#include <dns/ssu.h>
73
#include <dns/zone.h>
74
#include <dns/zoneproperties.h>
75
76
#include "dlz_p.h"
77
78
/***
79
 *** Supported DLZ DB Implementations Registry
80
 ***/
81
82
static ISC_LIST(dns_dlzimplementation_t) dlz_implementations;
83
static isc_rwlock_t dlz_implock;
84
85
void
86
2
dns__dlz_initialize(void) {
87
2
  isc_rwlock_init(&dlz_implock);
88
2
  ISC_LIST_INIT(dlz_implementations);
89
2
}
90
91
void
92
0
dns__dlz_shutdown(void) {
93
0
  isc_rwlock_destroy(&dlz_implock);
94
0
}
95
96
/*%
97
 * Searches the dlz_implementations list for a driver matching name.
98
 */
99
static dns_dlzimplementation_t *
100
0
dlz_impfind(const char *name) {
101
0
  ISC_LIST_FOREACH(dlz_implementations, imp, link) {
102
0
    if (strcasecmp(name, imp->name) == 0) {
103
0
      return imp;
104
0
    }
105
0
  }
106
0
  return NULL;
107
0
}
108
109
/***
110
 *** Basic DLZ Methods
111
 ***/
112
113
isc_result_t
114
dns_dlzallowzonexfr(dns_view_t *view, const dns_name_t *name,
115
0
        const isc_sockaddr_t *clientaddr, dns_db_t **dbp) {
116
0
  isc_result_t result = ISC_R_NOTFOUND;
117
0
  dns_dlzallowzonexfr_t allowzonexfr;
118
119
  /*
120
   * Performs checks to make sure data is as we expect it to be.
121
   */
122
0
  REQUIRE(name != NULL);
123
0
  REQUIRE(dbp != NULL && *dbp == NULL);
124
125
  /*
126
   * Find a driver in which the zone exists and transfer is supported
127
   */
128
0
  ISC_LIST_FOREACH(view->dlz_searched, dlzdb, link) {
129
0
    REQUIRE(DNS_DLZ_VALID(dlzdb));
130
131
0
    allowzonexfr = dlzdb->implementation->methods->allowzonexfr;
132
0
    result = (*allowzonexfr)(dlzdb->implementation->driverarg,
133
0
           dlzdb->dbdata, dlzdb->mctx,
134
0
           view->rdclass, name, clientaddr, dbp);
135
136
    /*
137
     * In these cases, we found the right database. Non-success
138
     * result codes indicate the zone might not transfer.
139
     */
140
0
    switch (result) {
141
0
    case ISC_R_SUCCESS:
142
0
    case ISC_R_NOPERM:
143
0
    case ISC_R_DEFAULT:
144
0
      return result;
145
0
    default:
146
0
      break;
147
0
    }
148
0
  }
149
150
0
  if (result == ISC_R_NOTIMPLEMENTED) {
151
0
    result = ISC_R_NOTFOUND;
152
0
  }
153
154
0
  return result;
155
0
}
156
157
isc_result_t
158
dns_dlzcreate(isc_mem_t *mctx, const char *dlzname, const char *drivername,
159
0
        unsigned int argc, char *argv[], dns_dlzdb_t **dbp) {
160
0
  dns_dlzimplementation_t *impinfo;
161
0
  isc_result_t result;
162
0
  dns_dlzdb_t *db = NULL;
163
164
  /*
165
   * Performs checks to make sure data is as we expect it to be.
166
   */
167
0
  REQUIRE(dbp != NULL && *dbp == NULL);
168
0
  REQUIRE(dlzname != NULL);
169
0
  REQUIRE(drivername != NULL);
170
0
  REQUIRE(mctx != NULL);
171
172
  /* write log message */
173
0
  isc_log_write(DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_INFO,
174
0
          "Loading '%s' using driver %s", dlzname, drivername);
175
176
  /* lock the dlz_implementations list so we can search it. */
177
0
  RWLOCK(&dlz_implock, isc_rwlocktype_read);
178
179
  /* search for the driver implementation  */
180
0
  impinfo = dlz_impfind(drivername);
181
0
  if (impinfo == NULL) {
182
0
    RWUNLOCK(&dlz_implock, isc_rwlocktype_read);
183
184
0
    isc_log_write(DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
185
0
            ISC_LOG_ERROR,
186
0
            "unsupported DLZ database driver '%s'."
187
0
            "  %s not loaded.",
188
0
            drivername, dlzname);
189
190
0
    return ISC_R_NOTFOUND;
191
0
  }
192
193
  /* Allocate memory to hold the DLZ database driver */
194
0
  db = isc_mem_get(mctx, sizeof(*db));
195
0
  *db = (dns_dlzdb_t){
196
0
    .implementation = impinfo,
197
0
  };
198
199
0
  ISC_LINK_INIT(db, link);
200
0
  if (dlzname != NULL) {
201
0
    db->dlzname = isc_mem_strdup(mctx, dlzname);
202
0
  }
203
204
  /* Create a new database using implementation 'drivername'. */
205
0
  result = ((impinfo->methods->create)(mctx, dlzname, argc, argv,
206
0
               impinfo->driverarg, &db->dbdata));
207
0
  RWUNLOCK(&dlz_implock, isc_rwlocktype_read);
208
0
  CHECK(result);
209
210
  /* Mark the DLZ driver as valid */
211
0
  db->magic = DNS_DLZ_MAGIC;
212
0
  isc_mem_attach(mctx, &db->mctx);
213
0
  isc_log_write(DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
214
0
          ISC_LOG_DEBUG(2), "DLZ driver loaded successfully.");
215
0
  *dbp = db;
216
0
  return ISC_R_SUCCESS;
217
0
cleanup:
218
0
  isc_log_write(DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
219
0
          ISC_LOG_ERROR, "DLZ driver failed to load.");
220
221
  /* impinfo->methods->create failed. */
222
0
  isc_mem_free(mctx, db->dlzname);
223
0
  isc_mem_put(mctx, db, sizeof(*db));
224
0
  return result;
225
0
}
226
227
void
228
0
dns_dlzdestroy(dns_dlzdb_t **dbp) {
229
0
  dns_dlzdestroy_t destroy;
230
0
  dns_dlzdb_t *db;
231
232
  /* Write debugging message to log */
233
0
  isc_log_write(DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
234
0
          ISC_LOG_DEBUG(2), "Unloading DLZ driver.");
235
236
  /*
237
   * Perform checks to make sure data is as we expect it to be.
238
   */
239
0
  REQUIRE(dbp != NULL && DNS_DLZ_VALID(*dbp));
240
241
0
  db = *dbp;
242
0
  *dbp = NULL;
243
244
0
  if (db->ssutable != NULL) {
245
0
    dns_ssutable_detach(&db->ssutable);
246
0
  }
247
248
  /* call the drivers destroy method */
249
0
  if (db->dlzname != NULL) {
250
0
    isc_mem_free(db->mctx, db->dlzname);
251
0
  }
252
0
  destroy = db->implementation->methods->destroy;
253
0
  (*destroy)(db->implementation->driverarg, db->dbdata);
254
  /* return memory and detach */
255
0
  isc_mem_putanddetach(&db->mctx, db, sizeof(*db));
256
0
}
257
258
/*%
259
 * Registers a DLZ driver.  This basically just adds the dlz
260
 * driver to the list of available drivers in the dlz_implementations list.
261
 */
262
isc_result_t
263
dns_dlzregister(const char *drivername, const dns_dlzmethods_t *methods,
264
    void *driverarg, isc_mem_t *mctx,
265
0
    dns_dlzimplementation_t **dlzimp) {
266
0
  dns_dlzimplementation_t *dlz_imp;
267
268
  /* Write debugging message to log */
269
0
  isc_log_write(DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
270
0
          ISC_LOG_DEBUG(2), "Registering DLZ driver '%s'",
271
0
          drivername);
272
273
  /*
274
   * Performs checks to make sure data is as we expect it to be.
275
   */
276
0
  REQUIRE(drivername != NULL);
277
0
  REQUIRE(methods != NULL);
278
0
  REQUIRE(methods->create != NULL);
279
0
  REQUIRE(methods->destroy != NULL);
280
0
  REQUIRE(methods->findzone != NULL);
281
0
  REQUIRE(mctx != NULL);
282
0
  REQUIRE(dlzimp != NULL && *dlzimp == NULL);
283
284
  /* lock the dlz_implementations list so we can modify it. */
285
0
  RWLOCK(&dlz_implock, isc_rwlocktype_write);
286
287
  /*
288
   * check that another already registered driver isn't using
289
   * the same name
290
   */
291
0
  dlz_imp = dlz_impfind(drivername);
292
0
  if (dlz_imp != NULL) {
293
0
    isc_log_write(DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
294
0
            ISC_LOG_DEBUG(2),
295
0
            "DLZ Driver '%s' already registered", drivername);
296
0
    RWUNLOCK(&dlz_implock, isc_rwlocktype_write);
297
0
    return ISC_R_EXISTS;
298
0
  }
299
300
  /*
301
   * Allocate memory for a dlz_implementation object.  Error if
302
   * we cannot.
303
   */
304
0
  dlz_imp = isc_mem_get(mctx, sizeof(*dlz_imp));
305
0
  *dlz_imp = (dns_dlzimplementation_t){
306
0
    .name = drivername,
307
0
    .methods = methods,
308
0
    .driverarg = driverarg,
309
0
  };
310
311
  /* attach the new dlz_implementation object to a memory context */
312
0
  isc_mem_attach(mctx, &dlz_imp->mctx);
313
314
  /*
315
   * prepare the dlz_implementation object to be put in a list,
316
   * and append it to the list
317
   */
318
0
  ISC_LINK_INIT(dlz_imp, link);
319
0
  ISC_LIST_APPEND(dlz_implementations, dlz_imp, link);
320
321
  /* Unlock the dlz_implementations list.  */
322
0
  RWUNLOCK(&dlz_implock, isc_rwlocktype_write);
323
324
  /* Pass back the dlz_implementation that we created. */
325
0
  *dlzimp = dlz_imp;
326
327
0
  return ISC_R_SUCCESS;
328
0
}
329
330
/*%
331
 * Tokenize the string "s" into whitespace-separated words,
332
 * return the number of words in '*argcp' and an array
333
 * of pointers to the words in '*argvp'.  The caller
334
 * must free the array using isc_mem_put().  The string
335
 * is modified in-place.
336
 */
337
isc_result_t
338
0
dns_dlzstrtoargv(isc_mem_t *mctx, char *s, unsigned int *argcp, char ***argvp) {
339
0
  return isc_commandline_strtoargv(mctx, s, argcp, argvp, 0);
340
0
}
341
342
/*%
343
 * Unregisters a DLZ driver.  This basically just removes the dlz
344
 * driver from the list of available drivers in the dlz_implementations list.
345
 */
346
void
347
0
dns_dlzunregister(dns_dlzimplementation_t **dlzimp) {
348
0
  dns_dlzimplementation_t *dlz_imp;
349
350
  /* Write debugging message to log */
351
0
  isc_log_write(DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
352
0
          ISC_LOG_DEBUG(2), "Unregistering DLZ driver.");
353
354
  /*
355
   * Performs checks to make sure data is as we expect it to be.
356
   */
357
0
  REQUIRE(dlzimp != NULL && *dlzimp != NULL);
358
359
0
  dlz_imp = *dlzimp;
360
361
  /* lock the dlz_implementations list so we can modify it. */
362
0
  RWLOCK(&dlz_implock, isc_rwlocktype_write);
363
364
  /* remove the dlz_implementation object from the list */
365
0
  ISC_LIST_UNLINK(dlz_implementations, dlz_imp, link);
366
367
  /*
368
   * Return the memory back to the available memory pool and
369
   * remove it from the memory context.
370
   */
371
0
  isc_mem_putanddetach(&dlz_imp->mctx, dlz_imp, sizeof(*dlz_imp));
372
373
  /* Unlock the dlz_implementations list. */
374
0
  RWUNLOCK(&dlz_implock, isc_rwlocktype_write);
375
0
}
376
377
/*
378
 * Create a writeable DLZ zone. This can be called by DLZ drivers
379
 * during configure() to create a zone that can be updated. The zone
380
 * type is set to dns_zone_dlz, which is equivalent to a primary zone
381
 *
382
 * This function uses a callback setup in dns_dlzconfigure() to call
383
 * into the server zone code to setup the remaining pieces of server
384
 * specific functionality on the zone
385
 */
386
isc_result_t
387
dns_dlz_writeablezone(dns_view_t *view, dns_dlzdb_t *dlzdb,
388
0
          const char *zone_name) {
389
0
  dns_zone_t *zone = NULL;
390
0
  dns_zone_t *dupzone = NULL;
391
0
  isc_result_t result;
392
0
  isc_buffer_t buffer;
393
0
  dns_fixedname_t fixorigin;
394
0
  dns_name_t *origin;
395
396
0
  REQUIRE(DNS_DLZ_VALID(dlzdb));
397
398
0
  REQUIRE(dlzdb->configure_callback != NULL);
399
400
0
  isc_buffer_constinit(&buffer, zone_name, strlen(zone_name));
401
0
  isc_buffer_add(&buffer, strlen(zone_name));
402
0
  dns_fixedname_init(&fixorigin);
403
0
  CHECK(dns_name_fromtext(dns_fixedname_name(&fixorigin), &buffer,
404
0
        dns_rootname, 0));
405
0
  origin = dns_fixedname_name(&fixorigin);
406
407
0
  if (!dlzdb->search) {
408
0
    isc_log_write(DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
409
0
            ISC_LOG_WARNING,
410
0
            "DLZ %s has 'search no;', but attempted to "
411
0
            "register writeable zone %s.",
412
0
            dlzdb->dlzname, zone_name);
413
0
    result = ISC_R_SUCCESS;
414
0
    goto cleanup;
415
0
  }
416
417
  /* See if the zone already exists */
418
0
  result = dns_view_findzone(view, origin, DNS_ZTFIND_EXACT, &dupzone);
419
0
  if (result == ISC_R_SUCCESS) {
420
0
    dns_zone_detach(&dupzone);
421
0
    CLEANUP(ISC_R_EXISTS);
422
0
  }
423
0
  INSIST(dupzone == NULL);
424
425
  /* Create it */
426
0
  dns_zone_create(&zone, view->mctx, 0);
427
0
  dns_zone_setorigin(zone, origin);
428
0
  dns_zone_setview(zone, view);
429
430
0
  dns_zone_setadded(zone, true);
431
432
0
  if (dlzdb->ssutable == NULL) {
433
0
    dns_ssutable_createdlz(dlzdb->mctx, &dlzdb->ssutable, dlzdb);
434
0
  }
435
0
  dns_zone_setssutable(zone, dlzdb->ssutable);
436
437
0
  CHECK(dlzdb->configure_callback(view, dlzdb, zone));
438
439
0
  result = dns_view_addzone(view, zone);
440
441
0
cleanup:
442
0
  if (zone != NULL) {
443
0
    dns_zone_detach(&zone);
444
0
  }
445
446
0
  return result;
447
0
}
448
449
/*%
450
 * Configure a DLZ driver. This is optional, and if supplied gives
451
 * the backend an opportunity to configure parameters related to DLZ.
452
 */
453
isc_result_t
454
dns_dlzconfigure(dns_view_t *view, dns_dlzdb_t *dlzdb,
455
0
     dlzconfigure_callback_t callback) {
456
0
  dns_dlzimplementation_t *impl;
457
0
  isc_result_t result;
458
459
0
  REQUIRE(DNS_DLZ_VALID(dlzdb));
460
0
  REQUIRE(dlzdb->implementation != NULL);
461
462
0
  impl = dlzdb->implementation;
463
464
0
  if (impl->methods->configure == NULL) {
465
0
    return ISC_R_SUCCESS;
466
0
  }
467
468
0
  dlzdb->configure_callback = callback;
469
470
0
  result = impl->methods->configure(impl->driverarg, dlzdb->dbdata, view,
471
0
            dlzdb);
472
0
  return result;
473
0
}
474
475
bool
476
dns_dlz_ssumatch(dns_dlzdb_t *dlzdatabase, const dns_name_t *signer,
477
     const dns_name_t *name, const isc_netaddr_t *tcpaddr,
478
0
     dns_rdatatype_t type, const dst_key_t *key) {
479
0
  dns_dlzimplementation_t *impl;
480
0
  bool r;
481
482
0
  REQUIRE(dlzdatabase != NULL);
483
0
  REQUIRE(dlzdatabase->implementation != NULL);
484
0
  REQUIRE(dlzdatabase->implementation->methods != NULL);
485
0
  impl = dlzdatabase->implementation;
486
487
0
  if (impl->methods->ssumatch == NULL) {
488
0
    isc_log_write(DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
489
0
            ISC_LOG_INFO,
490
0
            "No ssumatch method for DLZ database");
491
0
    return false;
492
0
  }
493
494
0
  r = impl->methods->ssumatch(signer, name, tcpaddr, type, key,
495
0
            impl->driverarg, dlzdatabase->dbdata);
496
0
  return r;
497
0
}