Coverage Report

Created: 2025-11-24 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/dns/zt.c
Line
Count
Source
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
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
/*! \file */
15
16
#include <inttypes.h>
17
#include <stdbool.h>
18
19
#include <isc/atomic.h>
20
#include <isc/file.h>
21
#include <isc/log.h>
22
#include <isc/magic.h>
23
#include <isc/mem.h>
24
#include <isc/result.h>
25
#include <isc/string.h>
26
#include <isc/tid.h>
27
#include <isc/util.h>
28
29
#include <dns/name.h>
30
#include <dns/qp.h>
31
#include <dns/rdataclass.h>
32
#include <dns/view.h>
33
#include <dns/zone.h>
34
#include <dns/zt.h>
35
36
0
#define ZTMAGIC      ISC_MAGIC('Z', 'T', 'b', 'l')
37
#define VALID_ZT(zt) ISC_MAGIC_VALID(zt, ZTMAGIC)
38
39
struct dns_zt {
40
  unsigned int magic;
41
  isc_mem_t *mctx;
42
  dns_qpmulti_t *multi;
43
44
  atomic_bool flush;
45
  isc_refcount_t references;
46
  isc_refcount_t loads_pending;
47
};
48
49
struct zt_load_params {
50
  dns_zt_t *zt;
51
  dns_zt_callback_t *loaddone;
52
  void *loaddone_arg;
53
  bool newonly;
54
};
55
56
struct zt_freeze_params {
57
  dns_view_t *view;
58
  bool freeze;
59
};
60
61
static void
62
ztqpattach(void *uctx ISC_ATTR_UNUSED, void *pval,
63
0
     uint32_t ival ISC_ATTR_UNUSED) {
64
0
  dns_zone_t *zone = pval;
65
0
  dns_zone_ref(zone);
66
0
}
67
68
static void
69
ztqpdetach(void *uctx ISC_ATTR_UNUSED, void *pval,
70
0
     uint32_t ival ISC_ATTR_UNUSED) {
71
0
  dns_zone_t *zone = pval;
72
0
  dns_zone_detach(&zone);
73
0
}
74
75
static size_t
76
ztqpmakekey(dns_qpkey_t key, void *uctx ISC_ATTR_UNUSED, void *pval,
77
0
      uint32_t ival ISC_ATTR_UNUSED) {
78
0
  dns_zone_t *zone = pval;
79
0
  dns_name_t *name = dns_zone_getorigin(zone);
80
0
  return dns_qpkey_fromname(key, name, DNS_DBNAMESPACE_NORMAL);
81
0
}
82
83
static void
84
0
ztqptriename(void *uctx, char *buf, size_t size) {
85
0
  dns_view_t *view = uctx;
86
0
  snprintf(buf, size, "view %s zone table", view->name);
87
0
}
88
89
static dns_qpmethods_t ztqpmethods = {
90
  ztqpattach,
91
  ztqpdetach,
92
  ztqpmakekey,
93
  ztqptriename,
94
};
95
96
void
97
0
dns_zt_create(isc_mem_t *mctx, dns_view_t *view, dns_zt_t **ztp) {
98
0
  dns_qpmulti_t *multi = NULL;
99
0
  dns_zt_t *zt = NULL;
100
101
0
  REQUIRE(ztp != NULL && *ztp == NULL);
102
0
  REQUIRE(view != NULL);
103
104
0
  dns_qpmulti_create(mctx, &ztqpmethods, view, &multi);
105
106
0
  zt = isc_mem_get(mctx, sizeof(*zt));
107
0
  *zt = (dns_zt_t){
108
0
    .magic = ZTMAGIC,
109
0
    .multi = multi,
110
0
    .references = 1,
111
0
  };
112
113
0
  isc_mem_attach(mctx, &zt->mctx);
114
115
0
  *ztp = zt;
116
0
}
117
118
/*
119
 * XXXFANF it isn't clear whether this function will be useful. There
120
 * is only one zone table per view, so it is probably enough to let
121
 * the qp-trie auto-GC do its thing. However it might be problematic
122
 * if a very large zone is replaced, and its database memory is
123
 * retained for a long time.
124
 */
125
void
126
0
dns_zt_compact(dns_zt_t *zt) {
127
0
  dns_qp_t *qp = NULL;
128
129
0
  REQUIRE(VALID_ZT(zt));
130
131
0
  dns_qpmulti_write(zt->multi, &qp);
132
0
  dns_qp_compact(qp, DNS_QPGC_ALL);
133
0
  dns_qpmulti_commit(zt->multi, &qp);
134
0
}
135
136
isc_result_t
137
0
dns_zt_mount(dns_zt_t *zt, dns_zone_t *zone) {
138
0
  isc_result_t result;
139
0
  dns_qp_t *qp = NULL;
140
141
0
  REQUIRE(VALID_ZT(zt));
142
143
0
  dns_qpmulti_write(zt->multi, &qp);
144
0
  result = dns_qp_insert(qp, zone, 0);
145
0
  dns_qp_compact(qp, DNS_QPGC_MAYBE);
146
0
  dns_qpmulti_commit(zt->multi, &qp);
147
148
0
  return result;
149
0
}
150
151
isc_result_t
152
0
dns_zt_unmount(dns_zt_t *zt, dns_zone_t *zone) {
153
0
  isc_result_t result;
154
0
  dns_qp_t *qp = NULL;
155
156
0
  REQUIRE(VALID_ZT(zt));
157
158
0
  dns_qpmulti_write(zt->multi, &qp);
159
0
  result = dns_qp_deletename(qp, dns_zone_getorigin(zone),
160
0
           DNS_DBNAMESPACE_NORMAL, NULL, NULL);
161
0
  dns_qp_compact(qp, DNS_QPGC_MAYBE);
162
0
  dns_qpmulti_commit(zt->multi, &qp);
163
164
0
  return result;
165
0
}
166
167
isc_result_t
168
dns_zt_find(dns_zt_t *zt, const dns_name_t *name, dns_ztfind_t options,
169
0
      dns_zone_t **zonep) {
170
0
  isc_result_t result;
171
0
  dns_qpread_t qpr;
172
0
  void *pval = NULL;
173
0
  dns_ztfind_t exactmask = DNS_ZTFIND_NOEXACT | DNS_ZTFIND_EXACT;
174
0
  dns_ztfind_t exactopts = options & exactmask;
175
0
  dns_qpchain_t chain;
176
177
0
  REQUIRE(VALID_ZT(zt));
178
0
  REQUIRE(exactopts != exactmask);
179
180
0
  dns_qpmulti_query(zt->multi, &qpr);
181
182
0
  if (exactopts == DNS_ZTFIND_EXACT) {
183
0
    result = dns_qp_getname(&qpr, name, DNS_DBNAMESPACE_NORMAL,
184
0
          &pval, NULL);
185
0
  } else {
186
0
    result = dns_qp_lookup(&qpr, name, DNS_DBNAMESPACE_NORMAL, NULL,
187
0
               NULL, &chain, &pval, NULL);
188
0
    if (exactopts == DNS_ZTFIND_NOEXACT && result == ISC_R_SUCCESS)
189
0
    {
190
      /* get pval from the previous chain link */
191
0
      int len = dns_qpchain_length(&chain);
192
0
      if (len >= 2) {
193
0
        dns_qpchain_node(&chain, len - 2, NULL, &pval,
194
0
             NULL);
195
0
        result = DNS_R_PARTIALMATCH;
196
0
      } else {
197
0
        result = ISC_R_NOTFOUND;
198
0
      }
199
0
    }
200
0
  }
201
0
  dns_qpread_destroy(zt->multi, &qpr);
202
203
0
  if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
204
0
    dns_zone_t *zone = pval;
205
    /*
206
     * If DNS_ZTFIND_MIRROR is set and the zone which was
207
     * determined to be the deepest match for the supplied name is
208
     * a mirror zone which is expired or not yet loaded, treat it
209
     * as non-existent.  This will trigger a fallback to recursion
210
     * instead of returning a SERVFAIL.
211
     *
212
     * Note that currently only the deepest match in the zone table
213
     * is checked.  Consider a server configured with two mirror
214
     * zones: "bar" and its child, "foo.bar".  If zone data is
215
     * available for "bar" but not for "foo.bar", a query with
216
     * QNAME equal to or below "foo.bar" will cause ISC_R_NOTFOUND
217
     * to be returned, not DNS_R_PARTIALMATCH, despite zone data
218
     * being available for "bar".  This is considered to be an edge
219
     * case, handling which more appropriately is possible, but
220
     * arguably not worth the added complexity.
221
     */
222
0
    if ((options & DNS_ZTFIND_MIRROR) != 0 &&
223
0
        dns_zone_gettype(zone) == dns_zone_mirror &&
224
0
        !dns_zone_isloaded(zone))
225
0
    {
226
0
      result = ISC_R_NOTFOUND;
227
0
    } else {
228
0
      dns_zone_attach(zone, zonep);
229
0
    }
230
0
  }
231
232
0
  return result;
233
0
}
234
235
void
236
0
dns_zt_attach(dns_zt_t *zt, dns_zt_t **ztp) {
237
0
  REQUIRE(VALID_ZT(zt));
238
0
  REQUIRE(ztp != NULL && *ztp == NULL);
239
240
0
  isc_refcount_increment(&zt->references);
241
242
0
  *ztp = zt;
243
0
}
244
245
static isc_result_t
246
0
flush(dns_zone_t *zone, void *uap) {
247
0
  UNUSED(uap);
248
0
  return dns_zone_flush(zone);
249
0
}
250
251
static void
252
0
zt_destroy(dns_zt_t *zt) {
253
0
  isc_refcount_destroy(&zt->references);
254
0
  isc_refcount_destroy(&zt->loads_pending);
255
256
0
  if (atomic_load_acquire(&zt->flush)) {
257
0
    (void)dns_zt_apply(zt, false, NULL, flush, NULL);
258
0
  }
259
260
0
  dns_qpmulti_destroy(&zt->multi);
261
0
  zt->magic = 0;
262
0
  isc_mem_putanddetach(&zt->mctx, zt, sizeof(*zt));
263
0
}
264
265
void
266
0
dns_zt_detach(dns_zt_t **ztp) {
267
0
  dns_zt_t *zt;
268
269
0
  REQUIRE(ztp != NULL && VALID_ZT(*ztp));
270
271
0
  zt = *ztp;
272
0
  *ztp = NULL;
273
274
0
  if (isc_refcount_decrement(&zt->references) == 1) {
275
0
    zt_destroy(zt);
276
0
  }
277
0
}
278
279
void
280
0
dns_zt_flush(dns_zt_t *zt) {
281
0
  REQUIRE(VALID_ZT(zt));
282
0
  atomic_store_release(&zt->flush, true);
283
0
}
284
285
static isc_result_t
286
0
load(dns_zone_t *zone, void *uap) {
287
0
  isc_result_t result;
288
0
  result = dns_zone_load(zone, uap != NULL);
289
0
  if (result == DNS_R_CONTINUE || result == ISC_R_LOADING ||
290
0
      result == DNS_R_UPTODATE || result == DNS_R_DYNAMIC)
291
0
  {
292
0
    result = ISC_R_SUCCESS;
293
0
  }
294
0
  return result;
295
0
}
296
297
isc_result_t
298
0
dns_zt_load(dns_zt_t *zt, bool stop, bool newonly) {
299
0
  REQUIRE(VALID_ZT(zt));
300
0
  return dns_zt_apply(zt, stop, NULL, load, newonly ? &newonly : NULL);
301
0
}
302
303
static void
304
0
loaded_all(struct zt_load_params *params) {
305
0
  if (params->loaddone != NULL) {
306
0
    params->loaddone(params->loaddone_arg);
307
0
  }
308
0
  isc_mem_put(params->zt->mctx, params, sizeof(*params));
309
0
}
310
311
/*
312
 * Decrement the loads_pending counter; when counter reaches
313
 * zero, call the loaddone callback that was initially set by
314
 * dns_zt_asyncload().
315
 */
316
static isc_result_t
317
0
loaded_one(void *uap) {
318
0
  struct zt_load_params *params = uap;
319
0
  dns_zt_t *zt = params->zt;
320
321
0
  REQUIRE(VALID_ZT(zt));
322
323
0
  if (isc_refcount_decrement(&zt->loads_pending) == 1) {
324
0
    loaded_all(params);
325
0
  }
326
327
0
  if (isc_refcount_decrement(&zt->references) == 1) {
328
0
    zt_destroy(zt);
329
0
  }
330
331
0
  return ISC_R_SUCCESS;
332
0
}
333
334
/*
335
 * Initiates asynchronous loading of zone 'zone'.  'callback' is a
336
 * pointer to a function which will be used to inform the caller when
337
 * the zone loading is complete.
338
 */
339
static isc_result_t
340
0
asyncload(dns_zone_t *zone, void *uap) {
341
0
  struct zt_load_params *params = uap;
342
0
  struct dns_zt *zt = params->zt;
343
0
  isc_result_t result;
344
345
0
  REQUIRE(VALID_ZT(zt));
346
0
  REQUIRE(zone != NULL);
347
348
0
  isc_refcount_increment(&zt->references);
349
0
  isc_refcount_increment(&zt->loads_pending);
350
351
0
  result = dns_zone_asyncload(zone, params->newonly, loaded_one, params);
352
0
  if (result != ISC_R_SUCCESS) {
353
    /*
354
     * Caller is holding a reference to zt->loads_pending
355
     * and zt->references so these can't decrement to zero.
356
     */
357
0
    isc_refcount_decrement1(&zt->references);
358
0
    isc_refcount_decrement1(&zt->loads_pending);
359
0
  }
360
0
  return ISC_R_SUCCESS;
361
0
}
362
363
isc_result_t
364
dns_zt_asyncload(dns_zt_t *zt, bool newonly, dns_zt_callback_t *loaddone,
365
0
     void *arg) {
366
0
  isc_result_t result;
367
0
  uint_fast32_t loads_pending;
368
0
  struct zt_load_params *params = NULL;
369
370
0
  REQUIRE(VALID_ZT(zt));
371
372
  /*
373
   * Obtain a reference to zt->loads_pending so that asyncload can
374
   * safely decrement both zt->references and zt->loads_pending
375
   * without going to zero.
376
   */
377
0
  loads_pending = isc_refcount_increment0(&zt->loads_pending);
378
0
  INSIST(loads_pending == 0);
379
380
0
  params = isc_mem_get(zt->mctx, sizeof(*params));
381
0
  *params = (struct zt_load_params){
382
0
    .zt = zt,
383
0
    .newonly = newonly,
384
0
    .loaddone = loaddone,
385
0
    .loaddone_arg = arg,
386
0
  };
387
388
0
  result = dns_zt_apply(zt, false, NULL, asyncload, params);
389
390
  /*
391
   * Have all the loads completed?
392
   */
393
0
  if (isc_refcount_decrement(&zt->loads_pending) == 1) {
394
0
    loaded_all(params);
395
0
  }
396
397
0
  return result;
398
0
}
399
400
static isc_result_t
401
0
freezezones(dns_zone_t *zone, void *uap) {
402
0
  struct zt_freeze_params *params = uap;
403
0
  bool frozen;
404
0
  isc_result_t result = ISC_R_SUCCESS;
405
0
  char classstr[DNS_RDATACLASS_FORMATSIZE];
406
0
  char zonename[DNS_NAME_FORMATSIZE];
407
0
  dns_zone_t *raw = NULL;
408
0
  dns_view_t *view;
409
0
  const char *vname;
410
0
  const char *sep;
411
0
  int level;
412
413
0
  dns_zone_getraw(zone, &raw);
414
0
  if (raw != NULL) {
415
0
    zone = raw;
416
0
  }
417
0
  if (params->view != dns_zone_getview(zone)) {
418
0
    if (raw != NULL) {
419
0
      dns_zone_detach(&raw);
420
0
    }
421
0
    return ISC_R_SUCCESS;
422
0
  }
423
0
  if (dns_zone_gettype(zone) != dns_zone_primary) {
424
0
    if (raw != NULL) {
425
0
      dns_zone_detach(&raw);
426
0
    }
427
0
    return ISC_R_SUCCESS;
428
0
  }
429
0
  if (!dns_zone_isdynamic(zone, true)) {
430
0
    if (raw != NULL) {
431
0
      dns_zone_detach(&raw);
432
0
    }
433
0
    return ISC_R_SUCCESS;
434
0
  }
435
436
0
  frozen = dns_zone_getupdatedisabled(zone);
437
0
  if (params->freeze) {
438
0
    if (frozen) {
439
0
      result = DNS_R_FROZEN;
440
0
    }
441
0
    if (result == ISC_R_SUCCESS) {
442
0
      result = dns_zone_flush(zone);
443
0
    }
444
0
    if (result == ISC_R_SUCCESS) {
445
0
      dns_zone_setupdatedisabled(zone, params->freeze);
446
0
    }
447
0
  } else {
448
0
    if (frozen) {
449
0
      result = dns_zone_loadandthaw(zone);
450
0
      if (result == DNS_R_CONTINUE ||
451
0
          result == ISC_R_LOADING || result == DNS_R_UPTODATE)
452
0
      {
453
0
        result = ISC_R_SUCCESS;
454
0
      }
455
0
    }
456
0
  }
457
0
  view = dns_zone_getview(zone);
458
0
  if (strcmp(view->name, "_bind") == 0 ||
459
0
      strcmp(view->name, "_default") == 0)
460
0
  {
461
0
    vname = "";
462
0
    sep = "";
463
0
  } else {
464
0
    vname = view->name;
465
0
    sep = " ";
466
0
  }
467
0
  dns_rdataclass_format(dns_zone_getclass(zone), classstr,
468
0
            sizeof(classstr));
469
0
  dns_name_format(dns_zone_getorigin(zone), zonename, sizeof(zonename));
470
0
  level = (result != ISC_R_SUCCESS) ? ISC_LOG_ERROR : ISC_LOG_DEBUG(1);
471
0
  isc_log_write(DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_ZONE, level,
472
0
          "%s zone '%s/%s'%s%s: %s",
473
0
          params->freeze ? "freezing" : "thawing", zonename,
474
0
          classstr, sep, vname, isc_result_totext(result));
475
0
  if (raw != NULL) {
476
0
    dns_zone_detach(&raw);
477
0
  }
478
0
  return result;
479
0
}
480
481
isc_result_t
482
0
dns_zt_freezezones(dns_zt_t *zt, dns_view_t *view, bool freeze) {
483
0
  isc_result_t result, tresult;
484
0
  struct zt_freeze_params params = { view, freeze };
485
486
0
  REQUIRE(VALID_ZT(zt));
487
488
0
  result = dns_zt_apply(zt, false, &tresult, freezezones, &params);
489
0
  if (tresult == ISC_R_NOTFOUND) {
490
0
    tresult = ISC_R_SUCCESS;
491
0
  }
492
0
  return (result == ISC_R_SUCCESS) ? tresult : result;
493
0
}
494
495
typedef void
496
setview_cb(dns_zone_t *zone);
497
498
static isc_result_t
499
0
setview(dns_zone_t *zone, void *arg) {
500
0
  setview_cb *cb = arg;
501
0
  cb(zone);
502
0
  return ISC_R_SUCCESS;
503
0
}
504
505
void
506
0
dns_zt_setviewcommit(dns_zt_t *zt) {
507
0
  dns_zt_apply(zt, false, NULL, setview, dns_zone_setviewcommit);
508
0
}
509
510
void
511
0
dns_zt_setviewrevert(dns_zt_t *zt) {
512
0
  dns_zt_apply(zt, false, NULL, setview, dns_zone_setviewrevert);
513
0
}
514
515
isc_result_t
516
dns_zt_apply(dns_zt_t *zt, bool stop, isc_result_t *sub,
517
0
       isc_result_t (*action)(dns_zone_t *, void *), void *uap) {
518
0
  isc_result_t result = ISC_R_SUCCESS;
519
0
  isc_result_t tresult = ISC_R_SUCCESS;
520
0
  dns_qpiter_t qpi;
521
0
  dns_qpread_t qpr;
522
0
  void *zone = NULL;
523
524
0
  REQUIRE(VALID_ZT(zt));
525
0
  REQUIRE(action != NULL);
526
527
0
  dns_qpmulti_query(zt->multi, &qpr);
528
0
  dns_qpiter_init(&qpr, &qpi);
529
530
0
  while (dns_qpiter_next(&qpi, NULL, &zone, NULL) == ISC_R_SUCCESS) {
531
0
    result = action(zone, uap);
532
0
    if (tresult == ISC_R_SUCCESS) {
533
0
      tresult = result;
534
0
    }
535
0
    if (result != ISC_R_SUCCESS && stop) {
536
0
      break;
537
0
    }
538
0
  }
539
0
  dns_qpread_destroy(zt->multi, &qpr);
540
541
0
  SET_IF_NOT_NULL(sub, tresult);
542
543
0
  return result;
544
0
}