Coverage Report

Created: 2026-01-17 06:14

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
2
#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
2
     uint32_t ival ISC_ATTR_UNUSED) {
64
2
  dns_zone_t *zone = pval;
65
2
  dns_zone_ref(zone);
66
2
}
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
367
      uint32_t ival ISC_ATTR_UNUSED) {
78
367
  dns_zone_t *zone = pval;
79
367
  dns_name_t *name = dns_zone_getorigin(zone);
80
367
  return dns_qpkey_fromname(key, name, DNS_DBNAMESPACE_NORMAL);
81
367
}
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
2
dns_zt_create(isc_mem_t *mctx, dns_view_t *view, dns_zt_t **ztp) {
98
2
  dns_qpmulti_t *multi = NULL;
99
2
  dns_zt_t *zt = NULL;
100
101
2
  REQUIRE(ztp != NULL && *ztp == NULL);
102
2
  REQUIRE(view != NULL);
103
104
2
  dns_qpmulti_create(mctx, &ztqpmethods, view, &multi);
105
106
2
  zt = isc_mem_get(mctx, sizeof(*zt));
107
2
  *zt = (dns_zt_t){
108
2
    .magic = ZTMAGIC,
109
2
    .multi = multi,
110
2
    .references = 1,
111
2
  };
112
113
2
  isc_mem_attach(mctx, &zt->mctx);
114
115
2
  *ztp = zt;
116
2
}
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
2
dns_zt_mount(dns_zt_t *zt, dns_zone_t *zone) {
138
2
  isc_result_t result;
139
2
  dns_qp_t *qp = NULL;
140
141
2
  REQUIRE(VALID_ZT(zt));
142
143
2
  dns_qpmulti_write(zt->multi, &qp);
144
2
  result = dns_qp_insert(qp, zone, 0);
145
2
  dns_qp_compact(qp, DNS_QPGC_MAYBE);
146
2
  dns_qpmulti_commit(zt->multi, &qp);
147
148
2
  return result;
149
2
}
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
365
      dns_zone_t **zonep) {
170
365
  isc_result_t result;
171
365
  dns_qpread_t qpr;
172
365
  void *pval = NULL;
173
365
  dns_ztfind_t exactmask = DNS_ZTFIND_NOEXACT | DNS_ZTFIND_EXACT;
174
365
  dns_ztfind_t exactopts = options & exactmask;
175
365
  dns_qpchain_t chain;
176
177
365
  REQUIRE(VALID_ZT(zt));
178
365
  REQUIRE(exactopts != exactmask);
179
180
365
  dns_qpmulti_query(zt->multi, &qpr);
181
182
365
  if (exactopts == DNS_ZTFIND_EXACT) {
183
0
    result = dns_qp_getname(&qpr, name, DNS_DBNAMESPACE_NORMAL,
184
0
          &pval, NULL);
185
365
  } else {
186
365
    result = dns_qp_lookup(&qpr, name, DNS_DBNAMESPACE_NORMAL, NULL,
187
365
               &chain, &pval, NULL);
188
365
    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, &pval, NULL);
194
0
        result = DNS_R_PARTIALMATCH;
195
0
      } else {
196
0
        result = ISC_R_NOTFOUND;
197
0
      }
198
0
    }
199
365
  }
200
365
  dns_qpread_destroy(zt->multi, &qpr);
201
202
365
  if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
203
206
    dns_zone_t *zone = pval;
204
    /*
205
     * If DNS_ZTFIND_MIRROR is set and the zone which was
206
     * determined to be the deepest match for the supplied name is
207
     * a mirror zone which is expired or not yet loaded, treat it
208
     * as non-existent.  This will trigger a fallback to recursion
209
     * instead of returning a SERVFAIL.
210
     *
211
     * Note that currently only the deepest match in the zone table
212
     * is checked.  Consider a server configured with two mirror
213
     * zones: "bar" and its child, "foo.bar".  If zone data is
214
     * available for "bar" but not for "foo.bar", a query with
215
     * QNAME equal to or below "foo.bar" will cause ISC_R_NOTFOUND
216
     * to be returned, not DNS_R_PARTIALMATCH, despite zone data
217
     * being available for "bar".  This is considered to be an edge
218
     * case, handling which more appropriately is possible, but
219
     * arguably not worth the added complexity.
220
     */
221
206
    if ((options & DNS_ZTFIND_MIRROR) != 0 &&
222
206
        dns_zone_gettype(zone) == dns_zone_mirror &&
223
0
        !dns_zone_isloaded(zone))
224
0
    {
225
0
      result = ISC_R_NOTFOUND;
226
206
    } else {
227
206
      dns_zone_attach(zone, zonep);
228
206
    }
229
206
  }
230
231
365
  return result;
232
365
}
233
234
void
235
0
dns_zt_attach(dns_zt_t *zt, dns_zt_t **ztp) {
236
0
  REQUIRE(VALID_ZT(zt));
237
0
  REQUIRE(ztp != NULL && *ztp == NULL);
238
239
0
  isc_refcount_increment(&zt->references);
240
241
0
  *ztp = zt;
242
0
}
243
244
static isc_result_t
245
0
flush(dns_zone_t *zone, void *uap) {
246
0
  UNUSED(uap);
247
0
  return dns_zone_flush(zone);
248
0
}
249
250
static void
251
0
zt_destroy(dns_zt_t *zt) {
252
0
  isc_refcount_destroy(&zt->references);
253
0
  isc_refcount_destroy(&zt->loads_pending);
254
255
0
  if (atomic_load_acquire(&zt->flush)) {
256
0
    (void)dns_zt_apply(zt, false, NULL, flush, NULL);
257
0
  }
258
259
0
  dns_qpmulti_destroy(&zt->multi);
260
0
  zt->magic = 0;
261
0
  isc_mem_putanddetach(&zt->mctx, zt, sizeof(*zt));
262
0
}
263
264
void
265
0
dns_zt_detach(dns_zt_t **ztp) {
266
0
  dns_zt_t *zt;
267
268
0
  REQUIRE(ztp != NULL && VALID_ZT(*ztp));
269
270
0
  zt = *ztp;
271
0
  *ztp = NULL;
272
273
0
  if (isc_refcount_decrement(&zt->references) == 1) {
274
0
    zt_destroy(zt);
275
0
  }
276
0
}
277
278
void
279
0
dns_zt_flush(dns_zt_t *zt) {
280
0
  REQUIRE(VALID_ZT(zt));
281
0
  atomic_store_release(&zt->flush, true);
282
0
}
283
284
static isc_result_t
285
0
load(dns_zone_t *zone, void *uap) {
286
0
  isc_result_t result;
287
0
  result = dns_zone_load(zone, uap != NULL);
288
0
  if (result == DNS_R_CONTINUE || result == ISC_R_LOADING ||
289
0
      result == DNS_R_UPTODATE || result == DNS_R_DYNAMIC)
290
0
  {
291
0
    result = ISC_R_SUCCESS;
292
0
  }
293
0
  return result;
294
0
}
295
296
isc_result_t
297
0
dns_zt_load(dns_zt_t *zt, bool stop, bool newonly) {
298
0
  REQUIRE(VALID_ZT(zt));
299
0
  return dns_zt_apply(zt, stop, NULL, load, newonly ? &newonly : NULL);
300
0
}
301
302
static void
303
0
loaded_all(struct zt_load_params *params) {
304
0
  if (params->loaddone != NULL) {
305
0
    params->loaddone(params->loaddone_arg);
306
0
  }
307
0
  isc_mem_put(params->zt->mctx, params, sizeof(*params));
308
0
}
309
310
/*
311
 * Decrement the loads_pending counter; when counter reaches
312
 * zero, call the loaddone callback that was initially set by
313
 * dns_zt_asyncload().
314
 */
315
static isc_result_t
316
0
loaded_one(void *uap) {
317
0
  struct zt_load_params *params = uap;
318
0
  dns_zt_t *zt = params->zt;
319
320
0
  REQUIRE(VALID_ZT(zt));
321
322
0
  if (isc_refcount_decrement(&zt->loads_pending) == 1) {
323
0
    loaded_all(params);
324
0
  }
325
326
0
  if (isc_refcount_decrement(&zt->references) == 1) {
327
0
    zt_destroy(zt);
328
0
  }
329
330
0
  return ISC_R_SUCCESS;
331
0
}
332
333
/*
334
 * Initiates asynchronous loading of zone 'zone'.  'callback' is a
335
 * pointer to a function which will be used to inform the caller when
336
 * the zone loading is complete.
337
 */
338
static isc_result_t
339
0
asyncload(dns_zone_t *zone, void *uap) {
340
0
  struct zt_load_params *params = uap;
341
0
  struct dns_zt *zt = params->zt;
342
0
  isc_result_t result;
343
344
0
  REQUIRE(VALID_ZT(zt));
345
0
  REQUIRE(zone != NULL);
346
347
0
  isc_refcount_increment(&zt->references);
348
0
  isc_refcount_increment(&zt->loads_pending);
349
350
0
  result = dns_zone_asyncload(zone, params->newonly, loaded_one, params);
351
0
  if (result != ISC_R_SUCCESS) {
352
    /*
353
     * Caller is holding a reference to zt->loads_pending
354
     * and zt->references so these can't decrement to zero.
355
     */
356
0
    isc_refcount_decrement1(&zt->references);
357
0
    isc_refcount_decrement1(&zt->loads_pending);
358
0
  }
359
0
  return ISC_R_SUCCESS;
360
0
}
361
362
isc_result_t
363
dns_zt_asyncload(dns_zt_t *zt, bool newonly, dns_zt_callback_t *loaddone,
364
0
     void *arg) {
365
0
  isc_result_t result;
366
0
  uint_fast32_t loads_pending;
367
0
  struct zt_load_params *params = NULL;
368
369
0
  REQUIRE(VALID_ZT(zt));
370
371
  /*
372
   * Obtain a reference to zt->loads_pending so that asyncload can
373
   * safely decrement both zt->references and zt->loads_pending
374
   * without going to zero.
375
   */
376
0
  loads_pending = isc_refcount_increment0(&zt->loads_pending);
377
0
  INSIST(loads_pending == 0);
378
379
0
  params = isc_mem_get(zt->mctx, sizeof(*params));
380
0
  *params = (struct zt_load_params){
381
0
    .zt = zt,
382
0
    .newonly = newonly,
383
0
    .loaddone = loaddone,
384
0
    .loaddone_arg = arg,
385
0
  };
386
387
0
  result = dns_zt_apply(zt, false, NULL, asyncload, params);
388
389
  /*
390
   * Have all the loads completed?
391
   */
392
0
  if (isc_refcount_decrement(&zt->loads_pending) == 1) {
393
0
    loaded_all(params);
394
0
  }
395
396
0
  return result;
397
0
}
398
399
static isc_result_t
400
0
freezezones(dns_zone_t *zone, void *uap) {
401
0
  struct zt_freeze_params *params = uap;
402
0
  bool frozen;
403
0
  isc_result_t result = ISC_R_SUCCESS;
404
0
  char classstr[DNS_RDATACLASS_FORMATSIZE];
405
0
  char zonename[DNS_NAME_FORMATSIZE];
406
0
  dns_zone_t *raw = NULL;
407
0
  dns_view_t *view;
408
0
  const char *vname;
409
0
  const char *sep;
410
0
  int level;
411
412
0
  dns_zone_getraw(zone, &raw);
413
0
  if (raw != NULL) {
414
0
    zone = raw;
415
0
  }
416
0
  if (params->view != dns_zone_getview(zone)) {
417
0
    if (raw != NULL) {
418
0
      dns_zone_detach(&raw);
419
0
    }
420
0
    return ISC_R_SUCCESS;
421
0
  }
422
0
  if (dns_zone_gettype(zone) != dns_zone_primary) {
423
0
    if (raw != NULL) {
424
0
      dns_zone_detach(&raw);
425
0
    }
426
0
    return ISC_R_SUCCESS;
427
0
  }
428
0
  if (!dns_zone_isdynamic(zone, true)) {
429
0
    if (raw != NULL) {
430
0
      dns_zone_detach(&raw);
431
0
    }
432
0
    return ISC_R_SUCCESS;
433
0
  }
434
435
0
  frozen = dns_zone_getupdatedisabled(zone);
436
0
  if (params->freeze) {
437
0
    if (frozen) {
438
0
      result = DNS_R_FROZEN;
439
0
    }
440
0
    if (result == ISC_R_SUCCESS) {
441
0
      result = dns_zone_flush(zone);
442
0
    }
443
0
    if (result == ISC_R_SUCCESS) {
444
0
      dns_zone_setupdatedisabled(zone, params->freeze);
445
0
    }
446
0
  } else {
447
0
    if (frozen) {
448
0
      result = dns_zone_loadandthaw(zone);
449
0
      if (result == DNS_R_CONTINUE ||
450
0
          result == ISC_R_LOADING || result == DNS_R_UPTODATE)
451
0
      {
452
0
        result = ISC_R_SUCCESS;
453
0
      }
454
0
    }
455
0
  }
456
0
  view = dns_zone_getview(zone);
457
0
  if (strcmp(view->name, "_bind") == 0 ||
458
0
      strcmp(view->name, "_default") == 0)
459
0
  {
460
0
    vname = "";
461
0
    sep = "";
462
0
  } else {
463
0
    vname = view->name;
464
0
    sep = " ";
465
0
  }
466
0
  dns_rdataclass_format(dns_zone_getclass(zone), classstr,
467
0
            sizeof(classstr));
468
0
  dns_name_format(dns_zone_getorigin(zone), zonename, sizeof(zonename));
469
0
  level = (result != ISC_R_SUCCESS) ? ISC_LOG_ERROR : ISC_LOG_DEBUG(1);
470
0
  isc_log_write(DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_ZONE, level,
471
0
          "%s zone '%s/%s'%s%s: %s",
472
0
          params->freeze ? "freezing" : "thawing", zonename,
473
0
          classstr, sep, vname, isc_result_totext(result));
474
0
  if (raw != NULL) {
475
0
    dns_zone_detach(&raw);
476
0
  }
477
0
  return result;
478
0
}
479
480
isc_result_t
481
0
dns_zt_freezezones(dns_zt_t *zt, dns_view_t *view, bool freeze) {
482
0
  isc_result_t result, tresult;
483
0
  struct zt_freeze_params params = { view, freeze };
484
485
0
  REQUIRE(VALID_ZT(zt));
486
487
0
  result = dns_zt_apply(zt, false, &tresult, freezezones, &params);
488
0
  if (tresult == ISC_R_NOTFOUND) {
489
0
    tresult = ISC_R_SUCCESS;
490
0
  }
491
0
  return (result == ISC_R_SUCCESS) ? tresult : result;
492
0
}
493
494
typedef void
495
setview_cb(dns_zone_t *zone);
496
497
static isc_result_t
498
0
setview(dns_zone_t *zone, void *arg) {
499
0
  setview_cb *cb = arg;
500
0
  cb(zone);
501
0
  return ISC_R_SUCCESS;
502
0
}
503
504
void
505
0
dns_zt_setviewcommit(dns_zt_t *zt) {
506
0
  dns_zt_apply(zt, false, NULL, setview, dns_zone_setviewcommit);
507
0
}
508
509
void
510
0
dns_zt_setviewrevert(dns_zt_t *zt) {
511
0
  dns_zt_apply(zt, false, NULL, setview, dns_zone_setviewrevert);
512
0
}
513
514
isc_result_t
515
dns_zt_apply(dns_zt_t *zt, bool stop, isc_result_t *sub,
516
0
       isc_result_t (*action)(dns_zone_t *, void *), void *uap) {
517
0
  isc_result_t result = ISC_R_SUCCESS;
518
0
  isc_result_t tresult = ISC_R_SUCCESS;
519
0
  dns_qpiter_t qpi;
520
0
  dns_qpread_t qpr;
521
0
  void *zone = NULL;
522
523
0
  REQUIRE(VALID_ZT(zt));
524
0
  REQUIRE(action != NULL);
525
526
0
  dns_qpmulti_query(zt->multi, &qpr);
527
0
  dns_qpiter_init(&qpr, &qpi);
528
529
0
  while (dns_qpiter_next(&qpi, &zone, NULL) == ISC_R_SUCCESS) {
530
0
    result = action(zone, uap);
531
0
    if (tresult == ISC_R_SUCCESS) {
532
0
      tresult = result;
533
0
    }
534
0
    if (result != ISC_R_SUCCESS && stop) {
535
0
      break;
536
0
    }
537
0
  }
538
0
  dns_qpread_destroy(zt->multi, &qpr);
539
540
0
  SET_IF_NOT_NULL(sub, tresult);
541
542
0
  return result;
543
0
}