Coverage Report

Created: 2026-06-07 06:20

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/zoneproperties.h>
35
#include <dns/zt.h>
36
37
0
#define ZTMAGIC      ISC_MAGIC('Z', 'T', 'b', 'l')
38
#define VALID_ZT(zt) ISC_MAGIC_VALID(zt, ZTMAGIC)
39
40
struct dns_zt {
41
  unsigned int magic;
42
  isc_mem_t *mctx;
43
  dns_qpmulti_t *multi;
44
45
  atomic_bool flush;
46
  isc_refcount_t references;
47
  isc_refcount_t loads_pending;
48
};
49
50
struct zt_load_params {
51
  dns_zt_t *zt;
52
  dns_zt_callback_t *loaddone;
53
  void *loaddone_arg;
54
  bool newonly;
55
};
56
57
struct zt_freeze_params {
58
  dns_view_t *view;
59
  bool freeze;
60
};
61
62
static void
63
ztqpattach(void *uctx ISC_ATTR_UNUSED, void *pval,
64
0
     uint32_t ival ISC_ATTR_UNUSED) {
65
0
  dns_zone_t *zone = pval;
66
0
  dns_zone_ref(zone);
67
0
}
68
69
static void
70
ztqpdetach(void *uctx ISC_ATTR_UNUSED, void *pval,
71
0
     uint32_t ival ISC_ATTR_UNUSED) {
72
0
  dns_zone_t *zone = pval;
73
0
  dns_zone_detach(&zone);
74
0
}
75
76
static size_t
77
ztqpmakekey(dns_qpkey_t key, void *uctx ISC_ATTR_UNUSED, void *pval,
78
0
      uint32_t ival ISC_ATTR_UNUSED) {
79
0
  dns_zone_t *zone = pval;
80
0
  dns_name_t *name = dns_zone_getorigin(zone);
81
0
  return dns_qpkey_fromname(key, name, DNS_DBNAMESPACE_NORMAL);
82
0
}
83
84
static void
85
0
ztqptriename(void *uctx, char *buf, size_t size) {
86
0
  dns_view_t *view = uctx;
87
0
  snprintf(buf, size, "view %s zone table", view->name);
88
0
}
89
90
static dns_qpmethods_t ztqpmethods = {
91
  ztqpattach,
92
  ztqpdetach,
93
  ztqpmakekey,
94
  ztqptriename,
95
};
96
97
void
98
0
dns_zt_create(isc_mem_t *mctx, dns_view_t *view, dns_zt_t **ztp) {
99
0
  dns_qpmulti_t *multi = NULL;
100
0
  dns_zt_t *zt = NULL;
101
102
0
  REQUIRE(ztp != NULL && *ztp == NULL);
103
0
  REQUIRE(view != NULL);
104
105
0
  dns_qpmulti_create(mctx, &ztqpmethods, view, &multi);
106
107
0
  zt = isc_mem_get(mctx, sizeof(*zt));
108
0
  *zt = (dns_zt_t){
109
0
    .magic = ZTMAGIC,
110
0
    .multi = multi,
111
0
    .references = 1,
112
0
  };
113
114
0
  isc_mem_attach(mctx, &zt->mctx);
115
116
0
  *ztp = zt;
117
0
}
118
119
/*
120
 * XXXFANF it isn't clear whether this function will be useful. There
121
 * is only one zone table per view, so it is probably enough to let
122
 * the qp-trie auto-GC do its thing. However it might be problematic
123
 * if a very large zone is replaced, and its database memory is
124
 * retained for a long time.
125
 */
126
void
127
0
dns_zt_compact(dns_zt_t *zt) {
128
0
  dns_qp_t *qp = NULL;
129
130
0
  REQUIRE(VALID_ZT(zt));
131
132
0
  dns_qpmulti_write(zt->multi, &qp);
133
0
  dns_qp_compact(qp, DNS_QPGC_ALL);
134
0
  dns_qpmulti_commit(zt->multi, &qp);
135
0
}
136
137
isc_result_t
138
0
dns_zt_mount(dns_zt_t *zt, dns_zone_t *zone) {
139
0
  isc_result_t result;
140
0
  dns_qp_t *qp = NULL;
141
142
0
  REQUIRE(VALID_ZT(zt));
143
144
0
  dns_qpmulti_write(zt->multi, &qp);
145
0
  result = dns_qp_insert(qp, zone, 0);
146
0
  dns_qp_compact(qp, DNS_QPGC_MAYBE);
147
0
  dns_qpmulti_commit(zt->multi, &qp);
148
149
0
  return result;
150
0
}
151
152
isc_result_t
153
0
dns_zt_unmount(dns_zt_t *zt, dns_zone_t *zone) {
154
0
  isc_result_t result;
155
0
  dns_qp_t *qp = NULL;
156
157
0
  REQUIRE(VALID_ZT(zt));
158
159
0
  dns_qpmulti_write(zt->multi, &qp);
160
0
  result = dns_qp_deletename(qp, dns_zone_getorigin(zone),
161
0
           DNS_DBNAMESPACE_NORMAL, NULL, NULL);
162
0
  dns_qp_compact(qp, DNS_QPGC_MAYBE);
163
0
  dns_qpmulti_commit(zt->multi, &qp);
164
165
0
  return result;
166
0
}
167
168
isc_result_t
169
dns_zt_find(dns_zt_t *zt, const dns_name_t *name, dns_ztfind_t options,
170
0
      dns_zone_t **zonep) {
171
0
  isc_result_t result;
172
0
  dns_qpread_t qpr;
173
0
  void *pval = NULL;
174
0
  dns_ztfind_t exactmask = DNS_ZTFIND_NOEXACT | DNS_ZTFIND_EXACT;
175
0
  dns_ztfind_t exactopts = options & exactmask;
176
0
  dns_qpchain_t chain;
177
178
0
  REQUIRE(VALID_ZT(zt));
179
0
  REQUIRE(exactopts != exactmask);
180
181
0
  dns_qpmulti_query(zt->multi, &qpr);
182
183
0
  if (exactopts == DNS_ZTFIND_EXACT) {
184
0
    result = dns_qp_getname(&qpr, name, DNS_DBNAMESPACE_NORMAL,
185
0
          &pval, NULL);
186
0
  } else {
187
0
    result = dns_qp_lookup(&qpr, name, DNS_DBNAMESPACE_NORMAL, NULL,
188
0
               &chain, &pval, NULL);
189
0
    if (exactopts == DNS_ZTFIND_NOEXACT && result == ISC_R_SUCCESS)
190
0
    {
191
      /* get pval from the previous chain link */
192
0
      int len = dns_qpchain_length(&chain);
193
0
      if (len >= 2) {
194
0
        dns_qpchain_node(&chain, len - 2, &pval, 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 void
286
0
loaded_all(struct zt_load_params *params) {
287
0
  if (params->loaddone != NULL) {
288
0
    params->loaddone(params->loaddone_arg);
289
0
  }
290
0
  isc_mem_put(params->zt->mctx, params, sizeof(*params));
291
0
}
292
293
/*
294
 * Decrement the loads_pending counter; when counter reaches
295
 * zero, call the loaddone callback that was initially set by
296
 * dns_zt_asyncload().
297
 */
298
static isc_result_t
299
0
loaded_one(void *uap) {
300
0
  struct zt_load_params *params = uap;
301
0
  dns_zt_t *zt = params->zt;
302
303
0
  REQUIRE(VALID_ZT(zt));
304
305
0
  if (isc_refcount_decrement(&zt->loads_pending) == 1) {
306
0
    loaded_all(params);
307
0
  }
308
309
0
  if (isc_refcount_decrement(&zt->references) == 1) {
310
0
    zt_destroy(zt);
311
0
  }
312
313
0
  return ISC_R_SUCCESS;
314
0
}
315
316
/*
317
 * Initiates asynchronous loading of zone 'zone'.  'callback' is a
318
 * pointer to a function which will be used to inform the caller when
319
 * the zone loading is complete.
320
 */
321
static isc_result_t
322
0
asyncload(dns_zone_t *zone, void *uap) {
323
0
  struct zt_load_params *params = uap;
324
0
  struct dns_zt *zt = params->zt;
325
0
  isc_result_t result;
326
327
0
  REQUIRE(VALID_ZT(zt));
328
0
  REQUIRE(zone != NULL);
329
330
0
  isc_refcount_increment(&zt->references);
331
0
  isc_refcount_increment(&zt->loads_pending);
332
333
0
  result = dns_zone_asyncload(zone, params->newonly, loaded_one, params);
334
0
  if (result != ISC_R_SUCCESS) {
335
    /*
336
     * Caller is holding a reference to zt->loads_pending
337
     * and zt->references so these can't decrement to zero.
338
     */
339
0
    isc_refcount_decrement1(&zt->references);
340
0
    isc_refcount_decrement1(&zt->loads_pending);
341
0
  }
342
0
  return ISC_R_SUCCESS;
343
0
}
344
345
isc_result_t
346
dns_zt_asyncload(dns_zt_t *zt, bool newonly, dns_zt_callback_t *loaddone,
347
0
     void *arg) {
348
0
  isc_result_t result;
349
0
  uint_fast32_t loads_pending;
350
0
  struct zt_load_params *params = NULL;
351
352
0
  REQUIRE(VALID_ZT(zt));
353
354
  /*
355
   * Obtain a reference to zt->loads_pending so that asyncload can
356
   * safely decrement both zt->references and zt->loads_pending
357
   * without going to zero.
358
   */
359
0
  loads_pending = isc_refcount_increment0(&zt->loads_pending);
360
0
  INSIST(loads_pending == 0);
361
362
0
  params = isc_mem_get(zt->mctx, sizeof(*params));
363
0
  *params = (struct zt_load_params){
364
0
    .zt = zt,
365
0
    .newonly = newonly,
366
0
    .loaddone = loaddone,
367
0
    .loaddone_arg = arg,
368
0
  };
369
370
0
  result = dns_zt_apply(zt, false, NULL, asyncload, params);
371
372
  /*
373
   * Have all the loads completed?
374
   */
375
0
  if (isc_refcount_decrement(&zt->loads_pending) == 1) {
376
0
    loaded_all(params);
377
0
  }
378
379
0
  return result;
380
0
}
381
382
static isc_result_t
383
0
freezezones(dns_zone_t *zone, void *uap) {
384
0
  struct zt_freeze_params *params = uap;
385
0
  bool frozen;
386
0
  isc_result_t result = ISC_R_SUCCESS;
387
0
  char classstr[DNS_RDATACLASS_FORMATSIZE];
388
0
  char zonename[DNS_NAME_FORMATSIZE];
389
0
  dns_zone_t *raw = NULL;
390
0
  dns_view_t *view;
391
0
  const char *vname;
392
0
  const char *sep;
393
0
  int level;
394
395
0
  dns_zone_getraw(zone, &raw);
396
0
  if (raw != NULL) {
397
0
    zone = raw;
398
0
  }
399
0
  if (params->view != dns_zone_getview(zone)) {
400
0
    if (raw != NULL) {
401
0
      dns_zone_detach(&raw);
402
0
    }
403
0
    return ISC_R_SUCCESS;
404
0
  }
405
0
  if (dns_zone_gettype(zone) != dns_zone_primary) {
406
0
    if (raw != NULL) {
407
0
      dns_zone_detach(&raw);
408
0
    }
409
0
    return ISC_R_SUCCESS;
410
0
  }
411
0
  if (!dns_zone_isdynamic(zone, true)) {
412
0
    if (raw != NULL) {
413
0
      dns_zone_detach(&raw);
414
0
    }
415
0
    return ISC_R_SUCCESS;
416
0
  }
417
418
0
  frozen = dns_zone_getupdatedisabled(zone);
419
0
  if (params->freeze) {
420
0
    if (frozen) {
421
0
      result = DNS_R_FROZEN;
422
0
    }
423
0
    if (result == ISC_R_SUCCESS) {
424
0
      result = dns_zone_flush(zone);
425
0
    }
426
0
    if (result == ISC_R_SUCCESS) {
427
0
      dns_zone_setupdatedisabled(zone, params->freeze);
428
0
    }
429
0
  } else {
430
0
    if (frozen) {
431
0
      result = dns_zone_loadandthaw(zone);
432
0
      if (result == DNS_R_CONTINUE ||
433
0
          result == ISC_R_LOADING || result == DNS_R_UPTODATE)
434
0
      {
435
0
        result = ISC_R_SUCCESS;
436
0
      }
437
0
    }
438
0
  }
439
0
  view = dns_zone_getview(zone);
440
0
  if (strcmp(view->name, "_bind") == 0 ||
441
0
      strcmp(view->name, "_default") == 0)
442
0
  {
443
0
    vname = "";
444
0
    sep = "";
445
0
  } else {
446
0
    vname = view->name;
447
0
    sep = " ";
448
0
  }
449
0
  dns_rdataclass_format(dns_zone_getclass(zone), classstr,
450
0
            sizeof(classstr));
451
0
  dns_name_format(dns_zone_getorigin(zone), zonename, sizeof(zonename));
452
0
  level = (result != ISC_R_SUCCESS) ? ISC_LOG_ERROR : ISC_LOG_DEBUG(1);
453
0
  isc_log_write(DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_ZONE, level,
454
0
          "%s zone '%s/%s'%s%s: %s",
455
0
          params->freeze ? "freezing" : "thawing", zonename,
456
0
          classstr, sep, vname, isc_result_totext(result));
457
0
  if (raw != NULL) {
458
0
    dns_zone_detach(&raw);
459
0
  }
460
0
  return result;
461
0
}
462
463
isc_result_t
464
0
dns_zt_freezezones(dns_zt_t *zt, dns_view_t *view, bool freeze) {
465
0
  isc_result_t result, tresult;
466
0
  struct zt_freeze_params params = { view, freeze };
467
468
0
  REQUIRE(VALID_ZT(zt));
469
470
0
  result = dns_zt_apply(zt, false, &tresult, freezezones, &params);
471
0
  if (tresult == ISC_R_NOTFOUND) {
472
0
    tresult = ISC_R_SUCCESS;
473
0
  }
474
0
  return (result == ISC_R_SUCCESS) ? tresult : result;
475
0
}
476
477
typedef void
478
setview_cb(dns_zone_t *zone);
479
480
static isc_result_t
481
0
setview(dns_zone_t *zone, void *arg) {
482
0
  setview_cb *cb = arg;
483
0
  cb(zone);
484
0
  return ISC_R_SUCCESS;
485
0
}
486
487
void
488
0
dns_zt_setviewcommit(dns_zt_t *zt) {
489
0
  dns_zt_apply(zt, false, NULL, setview, dns_zone_setviewcommit);
490
0
}
491
492
void
493
0
dns_zt_setviewrevert(dns_zt_t *zt) {
494
0
  dns_zt_apply(zt, false, NULL, setview, dns_zone_setviewrevert);
495
0
}
496
497
isc_result_t
498
dns_zt_apply(dns_zt_t *zt, bool stop, isc_result_t *sub,
499
0
       isc_result_t (*action)(dns_zone_t *, void *), void *uap) {
500
0
  isc_result_t result = ISC_R_SUCCESS;
501
0
  isc_result_t tresult = ISC_R_SUCCESS;
502
0
  dns_qpiter_t qpi;
503
0
  dns_qpread_t qpr;
504
0
  void *zone = NULL;
505
506
0
  REQUIRE(VALID_ZT(zt));
507
0
  REQUIRE(action != NULL);
508
509
0
  dns_qpmulti_query(zt->multi, &qpr);
510
0
  dns_qpiter_init(&qpr, &qpi);
511
512
0
  while (dns_qpiter_next(&qpi, &zone, NULL) == ISC_R_SUCCESS) {
513
0
    result = action(zone, uap);
514
0
    if (tresult == ISC_R_SUCCESS) {
515
0
      tresult = result;
516
0
    }
517
0
    if (result != ISC_R_SUCCESS && stop) {
518
0
      break;
519
0
    }
520
0
  }
521
0
  dns_qpread_destroy(zt->multi, &qpr);
522
523
0
  SET_IF_NOT_NULL(sub, tresult);
524
525
0
  return result;
526
0
}