Coverage Report

Created: 2025-05-21 07:03

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