Coverage Report

Created: 2026-07-16 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/dns/zonefetch.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 <isc/async.h>
17
#include <isc/loop.h>
18
19
#include <dns/resolver.h>
20
#include <dns/view.h>
21
#include <dns/zone.h>
22
#include <dns/zonefetch.h>
23
#include <dns/zoneproperties.h>
24
25
#include "zone_p.h"
26
27
void
28
0
dns_zonefetch_run(void *arg) {
29
0
  dns_zonefetch_t *fetch = (dns_zonefetch_t *)arg;
30
0
  dns_zone_t *zone;
31
0
  dns_view_t *view;
32
0
  isc_loop_t *loop;
33
0
  isc_result_t result;
34
0
  dns_resolver_t *resolver = NULL;
35
36
0
  zone = fetch->zone;
37
0
  if (dns__zone_exiting(zone)) {
38
0
    result = ISC_R_SHUTTINGDOWN;
39
0
    goto cancel;
40
0
  }
41
0
  view = dns_zone_getview(zone);
42
0
  loop = dns_zone_getloop(zone);
43
44
0
  INSIST(view != NULL);
45
0
  INSIST(loop != NULL);
46
47
0
  result = fetch->fetchmethods.start_fetch(fetch);
48
0
  if (result != ISC_R_SUCCESS) {
49
0
    goto cancel;
50
0
  }
51
52
0
  result = dns_view_getresolver(view, &resolver);
53
0
  if (result != ISC_R_SUCCESS) {
54
0
    goto cancel;
55
0
  }
56
57
0
  if (isc_log_wouldlog(ISC_LOG_DEBUG(3))) {
58
0
    char namebuf[DNS_NAME_FORMATSIZE];
59
0
    char typebuf[DNS_RDATATYPE_FORMATSIZE];
60
0
    dns_name_format(fetch->qname, namebuf, sizeof(namebuf));
61
0
    dns_rdatatype_format(fetch->qtype, typebuf, sizeof(typebuf));
62
0
    dns_zone_logc(zone, DNS_LOGCATEGORY_DNSSEC, ISC_LOG_DEBUG(3),
63
0
            "Do fetch for %s/%s request", namebuf, typebuf);
64
0
  }
65
66
0
  result = dns_resolver_createfetch(
67
0
    resolver, fetch->qname, fetch->qtype, NULL, NULL, NULL, NULL, 0,
68
0
    fetch->options, 0, NULL, NULL, NULL, loop, dns_zonefetch_done,
69
0
    fetch, NULL, &fetch->rrset, &fetch->sigset, &fetch->fetch);
70
71
0
  dns_resolver_detach(&resolver);
72
73
0
cancel:
74
0
  if (result == ISC_R_SUCCESS) {
75
0
    return;
76
0
  } else if (result != ISC_R_SHUTTINGDOWN) {
77
0
    char namebuf[DNS_NAME_FORMATSIZE];
78
79
0
    if (DNS_NAME_VALID(fetch->qname)) {
80
0
      char typebuf[DNS_RDATATYPE_FORMATSIZE];
81
0
      dns_name_format(fetch->qname, namebuf, sizeof(namebuf));
82
0
      dns_rdatatype_format(fetch->qtype, typebuf,
83
0
               sizeof(typebuf));
84
0
      dns_zone_log(zone, ISC_LOG_WARNING,
85
0
             "Failed fetch for %s/%s request", namebuf,
86
0
             typebuf);
87
0
    } else {
88
0
      dns_zone_nameonly(zone, namebuf, sizeof(namebuf));
89
0
      dns_zone_log(zone, ISC_LOG_WARNING,
90
0
             "Failed fetch for zone %s", namebuf);
91
0
    }
92
0
  }
93
94
  /*
95
   * Fetch failed, cancel.
96
   */
97
0
  dns__zone_lock(zone);
98
99
0
  dns_name_t *zname = dns_fixedname_name(&fetch->name);
100
0
  isc_mem_t *mctx = dns_zone_getmctx(zone);
101
0
  bool free_needed;
102
103
0
  isc_refcount_decrement(dns__zone_irefs(zone));
104
0
  dns_name_free(zname, mctx);
105
106
0
  fetch->fetchmethods.cancel_fetch(fetch);
107
108
0
  isc_mem_putanddetach(&fetch->mctx, fetch, sizeof(*fetch));
109
0
  free_needed = dns__zone_free_check(zone);
110
111
0
  dns__zone_unlock(zone);
112
113
0
  if (free_needed) {
114
0
    dns__zone_free(zone);
115
0
  }
116
0
}
117
118
void
119
0
dns_zonefetch_done(void *arg) {
120
0
  dns_fetchresponse_t *resp = (dns_fetchresponse_t *)arg;
121
0
  isc_result_t result = ISC_R_NOMORE;
122
0
  isc_result_t eresult;
123
0
  dns_zonefetch_t *fetch = NULL;
124
0
  dns_zone_t *zone = NULL;
125
0
  dns_view_t *view = NULL;
126
0
  isc_mem_t *mctx = NULL;
127
0
  dns_name_t *zname = NULL;
128
0
  dns_rdataset_t *rrset = NULL;
129
0
  dns_rdataset_t *sigset = NULL;
130
131
0
  INSIST(resp != NULL);
132
133
0
  fetch = resp->arg;
134
135
0
  INSIST(fetch != NULL);
136
137
0
  mctx = fetch->mctx;
138
0
  zone = fetch->zone;
139
0
  zname = dns_fixedname_name(&fetch->name);
140
0
  rrset = &fetch->rrset;
141
0
  sigset = &fetch->sigset;
142
0
  view = dns_zone_getview(zone);
143
0
  eresult = resp->result;
144
145
  /* Free resources which are not of interest */
146
0
  if (resp->node != NULL) {
147
0
    dns_db_detachnode(&resp->node);
148
0
  }
149
0
  if (resp->cache != NULL) {
150
0
    dns_db_detach(&resp->cache);
151
0
  }
152
0
  dns_resolver_destroyfetch(&fetch->fetch);
153
154
0
  dns__zone_lock(zone);
155
0
  if (dns__zone_exiting(zone) || view == NULL) {
156
0
    goto cleanup;
157
0
  }
158
159
0
  result = fetch->fetchmethods.done_fetch(fetch, eresult);
160
161
0
cleanup:
162
0
  isc_refcount_decrement(dns__zone_irefs(zone));
163
164
0
  dns_rdataset_cleanup(rrset);
165
0
  dns_rdataset_cleanup(sigset);
166
167
0
  fetch->fetchmethods.cleanup_fetch(fetch);
168
169
0
  dns_resolver_freefresp(&resp);
170
171
0
  if (result == DNS_R_CONTINUE) {
172
0
    dns__zone_unlock(zone);
173
0
    fetch->fetchmethods.continue_fetch(fetch);
174
0
  } else {
175
0
    bool free_needed = false;
176
0
    dns_name_free(zname, mctx);
177
0
    isc_mem_putanddetach(&fetch->mctx, fetch,
178
0
             sizeof(dns_zonefetch_t));
179
0
    free_needed = dns__zone_free_check(zone);
180
181
0
    dns__zone_unlock(zone);
182
183
0
    if (free_needed) {
184
0
      dns__zone_free(zone);
185
0
    }
186
0
  }
187
0
}
188
189
static void
190
0
zonefetch_schedule(dns_zonefetch_t *fetch, dns_name_t *name) {
191
0
  dns_zone_t *zone = fetch->zone;
192
193
0
  isc_refcount_increment0(dns__zone_irefs(zone));
194
195
0
  if (name != NULL) {
196
0
    dns_name_t *fname = dns_fixedname_initname(&fetch->name);
197
0
    dns_name_dup(name, fetch->mctx, fname);
198
0
  }
199
200
0
  dns_rdataset_init(&fetch->rrset);
201
0
  dns_rdataset_init(&fetch->sigset);
202
203
0
  isc_async_run(dns_zone_getloop(zone), dns_zonefetch_run, fetch);
204
0
}
205
206
void
207
0
dns_zonefetch_schedule(dns_zonefetch_t *fetch, dns_name_t *name) {
208
0
  REQUIRE(fetch != NULL);
209
0
  REQUIRE(name != NULL);
210
211
0
  zonefetch_schedule(fetch, name);
212
0
}
213
214
void
215
0
dns_zonefetch_reschedule(dns_zonefetch_t *fetch) {
216
0
  REQUIRE(fetch != NULL);
217
218
0
  zonefetch_schedule(fetch, NULL);
219
0
}
220
221
isc_result_t
222
dns_zonefetch_verify(dns_zonefetch_t *fetch, isc_result_t eresult,
223
0
         dns_trust_t trust) {
224
0
  char namebuf[DNS_NAME_FORMATSIZE];
225
0
  char typebuf[DNS_RDATATYPE_FORMATSIZE];
226
0
  dns_rdataset_t *rrset = NULL;
227
0
  dns_rdataset_t *sigset = NULL;
228
229
0
  REQUIRE(fetch != NULL);
230
231
0
  rrset = &fetch->rrset;
232
0
  sigset = &fetch->sigset;
233
0
  dns_name_format(fetch->qname, namebuf, sizeof(namebuf));
234
0
  dns_rdatatype_format(fetch->qtype, typebuf, sizeof(typebuf));
235
236
0
  if (eresult != ISC_R_SUCCESS) {
237
0
    dns_zone_logc(fetch->zone, DNS_LOGCATEGORY_DNSSEC,
238
0
            ISC_LOG_WARNING, "Unable to fetch %s/%s: %s",
239
0
            namebuf, typebuf, isc_result_totext(eresult));
240
0
    return eresult;
241
0
  }
242
243
  /* No records found */
244
0
  if (!dns_rdataset_isassociated(rrset)) {
245
0
    dns_zone_logc(fetch->zone, DNS_LOGCATEGORY_DNSSEC,
246
0
            ISC_LOG_WARNING, "No %s records found for '%s'",
247
0
            typebuf, namebuf);
248
0
    return ISC_R_NOTFOUND;
249
0
  }
250
251
  /* No RRSIGs found */
252
0
  if (!dns_rdataset_isassociated(sigset)) {
253
0
    dns_zone_logc(fetch->zone, DNS_LOGCATEGORY_DNSSEC,
254
0
            ISC_LOG_WARNING, "No %s RRSIGs found for '%s'",
255
0
            typebuf, namebuf);
256
0
    return DNS_R_NOVALIDSIG;
257
0
  }
258
259
  /* Check trust level */
260
0
  if (rrset->trust < trust) {
261
0
    dns_zone_logc(fetch->zone, DNS_LOGCATEGORY_DNSSEC,
262
0
            ISC_LOG_WARNING,
263
0
            "Invalid %s RRset for '%s' trust level %u",
264
0
            typebuf, namebuf, rrset->trust);
265
0
    return DNS_R_NOVALIDSIG;
266
0
  }
267
268
0
  return ISC_R_SUCCESS;
269
0
}