• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 15844689462

24 Jun 2025 07:56AM UTC coverage: 65.605% (+0.1%) from 65.499%
15844689462

Pull #15668

github

web-flow
Merge a481b0cef into ab85807a1
Pull Request #15668: rec: generate trace data in OpenTelemetry format

41505 of 91826 branches covered (45.2%)

Branch coverage included in aggregate %.

741 of 1012 new or added lines in 14 files covered. (73.22%)

2527 existing lines in 39 files now uncovered.

126717 of 164591 relevant lines covered (76.99%)

5751917.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.27
/pdns/rfc2136handler.cc
1
#include "dnswriter.hh"
2
#ifdef HAVE_CONFIG_H
3
#include "config.h"
4
#endif
5
#include "packethandler.hh"
6
#include "qtype.hh"
7
#include "dnspacket.hh"
8
#include "auth-caches.hh"
9
#include "statbag.hh"
10
#include "dnsseckeeper.hh"
11
#include "base64.hh"
12
#include "base32.hh"
13

14
#include "misc.hh"
15
#include "arguments.hh"
16
#include "resolver.hh"
17
#include "dns_random.hh"
18
#include "backends/gsql/ssql.hh"
19
#include "communicator.hh"
20
#include "query-local-address.hh"
21
#include "gss_context.hh"
22
#include "auth-main.hh"
23

24
extern StatBag S;
25
extern CommunicatorClass Communicator;
26

27
std::mutex PacketHandler::s_rfc2136lock;
28

29
// Implement section 3.2.1 and 3.2.2 of RFC2136
30
int PacketHandler::checkUpdatePrerequisites(const DNSRecord *rr, DomainInfo *di) {
228✔
31
  if (rr->d_ttl != 0)
228!
32
    return RCode::FormErr;
×
33

34
  // 3.2.1 and 3.2.2 check content length.
35
  if ( (rr->d_class == QClass::NONE || rr->d_class == QClass::ANY) && rr->d_clen != 0)
228!
36
    return RCode::FormErr;
×
37

38
  bool foundRecord=false;
228✔
39
  DNSResourceRecord rec;
228✔
40
  di->backend->lookup(QType(QType::ANY), rr->d_name, di->id);
228✔
41
  while(di->backend->get(rec)) {
624✔
42
    if (!rec.qtype.getCode())
396✔
43
      continue;
12✔
44
    if ((rr->d_type != QType::ANY && rec.qtype == rr->d_type) || rr->d_type == QType::ANY)
384!
45
      foundRecord=true;
384✔
46
  }
384✔
47

48
  // Section 3.2.1
49
  if (rr->d_class == QClass::ANY && !foundRecord) {
228✔
50
    if (rr->d_type == QType::ANY)
36✔
51
      return RCode::NXDomain;
24✔
52
    if (rr->d_type != QType::ANY)
12!
53
      return RCode::NXRRSet;
12✔
54
  }
12✔
55

56
  // Section 3.2.2
57
  if (rr->d_class == QClass::NONE && foundRecord) {
192✔
58
    if (rr->d_type == QType::ANY)
24✔
59
      return RCode::YXDomain;
12✔
60
    if (rr->d_type != QType::ANY)
12!
61
      return RCode::YXRRSet;
12✔
62
  }
12✔
63

64
  return RCode::NoError;
168✔
65
}
192✔
66

67

68
// Method implements section 3.4.1 of RFC2136
69
int PacketHandler::checkUpdatePrescan(const DNSRecord *rr) {
2,732✔
70
  // The RFC stats that d_class != ZCLASS, but we only support the IN class.
71
  if (rr->d_class != QClass::IN && rr->d_class != QClass::NONE && rr->d_class != QClass::ANY) {
2,732!
72
    return RCode::FormErr;
×
73
  }
×
74

75
  QType qtype = QType(rr->d_type);
2,732✔
76

77
  if (!qtype.isSupportedType()) {
2,732!
78
    return RCode::FormErr;
×
79
  }
×
80

81
  if ((rr->d_class == QClass::NONE || rr->d_class == QClass::ANY) && rr->d_ttl != 0) {
2,732!
82
    return RCode::FormErr;
×
83
  }
×
84

85
  if (rr->d_class == QClass::ANY && rr->d_clen != 0) {
2,732!
86
    return RCode::FormErr;
×
87
  }
×
88

89
  if (qtype.isMetadataType()) {
2,732!
90
    return RCode::FormErr;
×
91
  }
×
92

93
  if (rr->d_class != QClass::ANY && qtype.getCode() == QType::ANY) {
2,732✔
94
    return RCode::FormErr;
12✔
95
  }
12✔
96

97
  return RCode::NoError;
2,720✔
98
}
2,732✔
99

100

101
// Implements section 3.4.2 of RFC2136
102
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
103
uint PacketHandler::performUpdate(const string &msgPrefix, const DNSRecord *rr, DomainInfo *di, bool isPresigned, bool* narrow, bool* haveNSEC3, NSEC3PARAMRecordContent *ns3pr, bool *updatedSerial) {
2,648✔
104

105
  QType rrType = QType(rr->d_type);
2,648✔
106

107
  if (rrType == QType::NSEC || rrType == QType::NSEC3) {
2,648!
108
    g_log<<Logger::Warning<<msgPrefix<<"Trying to add/update/delete "<<rr->d_name<<"|"<<rrType.toString()<<". These are generated records, ignoring!"<<endl;
×
109
    return 0;
×
110
  }
×
111

112
  if (!isPresigned && rrType == QType::RRSIG) {
2,648!
113
    g_log<<Logger::Warning<<msgPrefix<<"Trying to add/update/delete "<<rr->d_name<<"|"<<rrType.toString()<<" in non-presigned zone, ignoring!"<<endl;
×
114
    return 0;
×
115
  }
×
116

117
  if ((rrType == QType::NSEC3PARAM || rrType == QType::DNSKEY) && rr->d_name != di->zone.operator const DNSName&()) {
2,648!
118
    g_log<<Logger::Warning<<msgPrefix<<"Trying to add/update/delete "<<rr->d_name<<"|"<<rrType.toString()<<", "<<rrType.toString()<<" must be at zone apex, ignoring!"<<endl;
×
119
    return 0;
×
120
  }
×
121

122

123
  uint changedRecords = 0;
2,648✔
124
  DNSResourceRecord rec;
2,648✔
125
  vector<DNSResourceRecord> rrset, recordsToDelete;
2,648✔
126
  set<DNSName> delnonterm, insnonterm; // used to (at the end) fix ENT records.
2,648✔
127

128

129
  if (rr->d_class == QClass::IN) { // 3.4.2.2 QClass::IN means insert or update
2,648✔
130
    DLOG(g_log<<msgPrefix<<"Add/Update record (QClass == IN) "<<rr->d_name<<"|"<<rrType.toString()<<endl);
1,988✔
131

132
    if (rrType == QType::NSEC3PARAM) {
1,988✔
133
      g_log<<Logger::Notice<<msgPrefix<<"Adding/updating NSEC3PARAM for zone, resetting ordernames."<<endl;
24✔
134

135
      *ns3pr = NSEC3PARAMRecordContent(rr->getContent()->getZoneRepresentation(), di->zone);
24✔
136
      *narrow = false; // adding a NSEC3 will cause narrow mode to be dropped, as you cannot specify that in a NSEC3PARAM record
24✔
137
      d_dk.setNSEC3PARAM(di->zone, *ns3pr, (*narrow));
24✔
138
      *haveNSEC3 = true;
24✔
139

140
      string error;
24✔
141
      string info;
24✔
142
      if (!d_dk.rectifyZone(di->zone, error, info, false)) {
24!
143
        throw PDNSException("Failed to rectify '" + di->zone.toLogString() + "': " + error);
×
144
      }
×
145
      return 1;
24✔
146
    }
24✔
147

148

149

150
    bool foundRecord = false;
1,964✔
151
    di->backend->lookup(rrType, rr->d_name, di->id);
1,964✔
152
    while (di->backend->get(rec)) {
60,452✔
153
      rrset.push_back(rec);
58,488✔
154
      foundRecord = true;
58,488✔
155
    }
58,488✔
156

157
    if (foundRecord) {
1,964✔
158

159
      if (rrType == QType::SOA) { // SOA updates require the serial to be higher than the current
1,392✔
160
        SOAData sdOld, sdUpdate;
48✔
161
        DNSResourceRecord *oldRec = &rrset.front();
48✔
162
        fillSOAData(oldRec->content, sdOld);
48✔
163
        oldRec->setContent(rr->getContent()->getZoneRepresentation());
48✔
164
        fillSOAData(oldRec->content, sdUpdate);
48✔
165
        if (rfc1982LessThan(sdOld.serial, sdUpdate.serial)) {
48✔
166
          di->backend->replaceRRSet(di->id, oldRec->qname, oldRec->qtype, rrset);
24✔
167
          *updatedSerial = true;
24✔
168
          changedRecords++;
24✔
169
          g_log<<Logger::Notice<<msgPrefix<<"Replacing SOA record "<<rr->d_name<<"|"<<rrType.toString()<<endl;
24✔
170
        } else {
24✔
171
          g_log<<Logger::Notice<<msgPrefix<<"Provided serial ("<<sdUpdate.serial<<") is older than the current serial ("<<sdOld.serial<<"), ignoring SOA update."<<endl;
24✔
172
        }
24✔
173

174
      // It's not possible to have multiple CNAME's with the same NAME. So we always update.
175
      } else if (rrType == QType::CNAME) {
1,344✔
176
        int changedCNames = 0;
24✔
177
        for (auto& i : rrset) {
24✔
178
          if (i.ttl != rr->d_ttl || i.content != rr->getContent()->getZoneRepresentation()) {
24!
179
            i.ttl = rr->d_ttl;
24✔
180
            i.setContent(rr->getContent()->getZoneRepresentation());
24✔
181
            changedCNames++;
24✔
182
          }
24✔
183
        }
24✔
184
        if (changedCNames > 0) {
24!
185
          di->backend->replaceRRSet(di->id, rr->d_name, rrType, rrset);
24✔
186
          g_log<<Logger::Notice<<msgPrefix<<"Replacing CNAME record "<<rr->d_name<<"|"<<rrType.toString()<<endl;
24✔
187
          changedRecords += changedCNames;
24✔
188
        } else {
24✔
189
          g_log<<Logger::Notice<<msgPrefix<<"Replace for CNAME record "<<rr->d_name<<"|"<<rrType.toString()<<" requested, but no changes made."<<endl;
×
190
        }
×
191

192
      // In any other case, we must check if the TYPE and RDATA match to provide an update (which effectively means a update of TTL)
193
      } else {
1,320✔
194
        int updateTTL=0;
1,320✔
195
        foundRecord = false;
1,320✔
196
        bool lowerCase = false;
1,320✔
197
        if (rrType.getCode() == QType::PTR ||
1,320✔
198
            rrType.getCode() == QType::MX ||
1,320✔
199
            rrType.getCode() == QType::SRV) {
1,320✔
200
          lowerCase = true;
84✔
201
        }
84✔
202
        string content = rr->getContent()->getZoneRepresentation();
1,320✔
203
        if (lowerCase) content = toLower(content);
1,320✔
204
        for (auto& i : rrset) {
58,416✔
205
          string icontent = i.getZoneRepresentation();
58,416✔
206
          if (lowerCase) icontent = toLower(icontent);
58,416✔
207
          if (rrType == i.qtype.getCode()) {
58,416!
208
            if (icontent == content) {
58,416✔
209
              foundRecord=true;
72✔
210
            }
72✔
211
            if (i.ttl != rr->d_ttl)  {
58,416✔
212
              i.ttl = rr->d_ttl;
144✔
213
              updateTTL++;
144✔
214
            }
144✔
215
          }
58,416✔
216
        }
58,416✔
217
        if (updateTTL > 0) {
1,320✔
218
          di->backend->replaceRRSet(di->id, rr->d_name, rrType, rrset);
84✔
219
          g_log<<Logger::Notice<<msgPrefix<<"Updating TTLs for "<<rr->d_name<<"|"<<rrType.toString()<<endl;
84✔
220
          changedRecords += updateTTL;
84✔
221
        } else {
1,236✔
222
          g_log<<Logger::Notice<<msgPrefix<<"Replace for recordset "<<rr->d_name<<"|"<<rrType.toString()<<" requested, but no changes made."<<endl;
1,236✔
223
        }
1,236✔
224
      }
1,320✔
225

226
      // ReplaceRRSet dumps our ordername and auth flag, so we need to correct it if we have changed records.
227
      // We can take the auth flag from the first RR in the set, as the name is different, so should the auth be.
228
      if (changedRecords > 0) {
1,392✔
229
        bool auth = rrset.front().auth;
132✔
230

231
        if(*haveNSEC3) {
132✔
232
          DNSName ordername;
66✔
233
          if(! *narrow)
66✔
234
            ordername=DNSName(toBase32Hex(hashQNameWithSalt(*ns3pr, rr->d_name)));
44✔
235

236
          if (*narrow)
66✔
237
            di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, DNSName(), auth);
22✔
238
          else
44✔
239
            di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, ordername, auth);
44✔
240
          if(!auth || rrType == QType::DS) {
66!
241
            di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, DNSName(), false, QType::NS);
×
242
            di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, DNSName(), false, QType::A);
×
243
            di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, DNSName(), false, QType::AAAA);
×
244
          }
×
245

246
        } else { // NSEC
66✔
247
          di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, rr->d_name.makeRelative(di->zone), auth);
66✔
248
          if(!auth || rrType == QType::DS) {
66!
249
            di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, DNSName(), false, QType::A);
×
250
            di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, DNSName(), false, QType::AAAA);
×
251
          }
×
252
        }
66✔
253
      }
132✔
254

255
    } // if (foundRecord)
1,392✔
256

257
    // If we haven't found a record that matches, we must add it.
258
    if (! foundRecord) {
1,964✔
259
      g_log<<Logger::Notice<<msgPrefix<<"Adding record "<<rr->d_name<<"|"<<rrType.toString()<<endl;
1,820✔
260
      delnonterm.insert(rr->d_name); // always remove any ENT's in the place where we're going to add a record.
1,820✔
261
      auto newRec = DNSResourceRecord::fromWire(*rr);
1,820✔
262
      newRec.domain_id = di->id;
1,820✔
263
      newRec.auth = (rr->d_name == di->zone.operator const DNSName&() || rrType.getCode() != QType::NS);
1,820✔
264
      di->backend->feedRecord(newRec, DNSName());
1,820✔
265
      changedRecords++;
1,820✔
266

267

268
      // because we added a record, we need to fix DNSSEC data.
269
      ZoneName shorter(rr->d_name);
1,820✔
270
      bool auth=newRec.auth;
1,820✔
271
      bool fixDS = (rrType == QType::DS);
1,820✔
272

273
      if (di->zone != shorter) { // Everything at APEX is auth=1 && no ENT's
1,820✔
274
        do {
3,640✔
275

276
          if (di->zone == shorter)
3,640✔
277
            break;
1,572✔
278

279
          bool foundShorter = false;
2,068✔
280
          di->backend->lookup(QType(QType::ANY), shorter.operator const DNSName&(), di->id);
2,068✔
281
          while (di->backend->get(rec)) {
62,336✔
282
            if (rec.qname == rr->d_name && rec.qtype == QType::DS)
60,268✔
283
              fixDS = true;
36✔
284
            if (shorter.operator const DNSName&() != rr->d_name) {
60,268✔
285
              foundShorter = true;
200✔
286
            }
200✔
287
            if (rec.qtype == QType::NS) // are we inserting below a delegate?
60,268✔
288
              auth=false;
300✔
289
          }
60,268✔
290

291
          if (!foundShorter && auth && shorter.operator const DNSName&() != rr->d_name) { // haven't found any record at current level, insert ENT.
2,068✔
292
            insnonterm.insert(shorter.operator const DNSName&());
120✔
293
          }
120✔
294
          if (foundShorter)
2,068✔
295
            break; // if we find a shorter record, we can stop searching
152✔
296
        } while(shorter.chopOff());
2,068!
297
      }
1,724✔
298

299
      if(*haveNSEC3)
1,820✔
300
      {
914✔
301
        DNSName ordername;
914✔
302
        if(! *narrow)
914✔
303
          ordername=DNSName(toBase32Hex(hashQNameWithSalt(*ns3pr, rr->d_name)));
612✔
304

305
        if (*narrow)
914✔
306
          di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, DNSName(), auth);
302✔
307
        else
612✔
308
          di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, ordername, auth);
612✔
309

310
        if (fixDS)
914✔
311
          di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, ordername, true, QType::DS);
18✔
312

313
        if(!auth)
914✔
314
        {
114✔
315
          if (ns3pr->d_flags)
114✔
316
            di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, DNSName(), false, QType::NS);
76✔
317
          di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, DNSName(), false, QType::A);
114✔
318
          di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, DNSName(), false, QType::AAAA);
114✔
319
        }
114✔
320
      }
914✔
321
      else // NSEC
906✔
322
      {
906✔
323
        DNSName ordername=rr->d_name.makeRelative(di->zone);
906✔
324
        di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, ordername, auth);
906✔
325
        if (fixDS) {
906✔
326
          di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, ordername, true, QType::DS);
18✔
327
        }
18✔
328
        if(!auth) {
906✔
329
          di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, DNSName(), false, QType::A);
114✔
330
          di->backend->updateDNSSECOrderNameAndAuth(di->id, rr->d_name, DNSName(), false, QType::AAAA);
114✔
331
        }
114✔
332
      }
906✔
333

334

335
      // If we insert an NS, all the records below it become non auth - so, we're inserting a delegate.
336
      // Auth can only be false when the rr->d_name is not the zone
337
      if (auth == false && rrType == QType::NS) {
1,820✔
338
        DLOG(g_log<<msgPrefix<<"Going to fix auth flags below "<<rr->d_name<<endl);
132✔
339
        insnonterm.clear(); // No ENT's are needed below delegates (auth=0)
132✔
340
        vector<DNSName> qnames;
132✔
341
        di->backend->listSubZone(ZoneName(rr->d_name), di->id);
132✔
342
        while(di->backend->get(rec)) {
396✔
343
          if (rec.qtype.getCode() && rec.qtype.getCode() != QType::DS && rr->d_name != rec.qname) // Skip ENT, DS and our already corrected record.
264✔
344
            qnames.push_back(rec.qname);
72✔
345
        }
264✔
346
        for(const auto & qname : qnames) {
132✔
347
          if(*haveNSEC3)  {
72✔
348
            DNSName ordername;
36✔
349
            if(! *narrow)
36✔
350
              ordername=DNSName(toBase32Hex(hashQNameWithSalt(*ns3pr, qname)));
24✔
351

352
            if (*narrow)
36✔
353
              di->backend->updateDNSSECOrderNameAndAuth(di->id, qname, DNSName(), auth);
12✔
354
            else
24✔
355
              di->backend->updateDNSSECOrderNameAndAuth(di->id, qname, ordername, auth);
24✔
356

357
            if (ns3pr->d_flags)
36✔
358
              di->backend->updateDNSSECOrderNameAndAuth(di->id, qname, DNSName(), false, QType::NS);
24✔
359
          }
36✔
360
          else { // NSEC
36✔
361
            DNSName ordername=DNSName(qname).makeRelative(di->zone);
36✔
362
            di->backend->updateDNSSECOrderNameAndAuth(di->id, qname, ordername, false, QType::NS);
36✔
363
          }
36✔
364

365
          di->backend->updateDNSSECOrderNameAndAuth(di->id, qname, DNSName(), false, QType::A);
72✔
366
          di->backend->updateDNSSECOrderNameAndAuth(di->id, qname, DNSName(), false, QType::AAAA);
72✔
367
        }
72✔
368
      }
132✔
369
    }
1,820✔
370
  } // rr->d_class == QClass::IN
1,964✔
371

372

373
  // Delete records - section 3.4.2.3 and 3.4.2.4 with the exception of the 'always leave 1 NS rule' as that's handled by
374
  // the code that calls this performUpdate().
375
  if ((rr->d_class == QClass::ANY || rr->d_class == QClass::NONE) && rrType != QType::SOA) { // never delete a SOA.
2,624✔
376
    DLOG(g_log<<msgPrefix<<"Deleting records: "<<rr->d_name<<"; QClass:"<<rr->d_class<<"; rrType: "<<rrType.toString()<<endl);
648✔
377

378
    if (rrType == QType::NSEC3PARAM) {
648✔
379
      g_log<<Logger::Notice<<msgPrefix<<"Deleting NSEC3PARAM from zone, resetting ordernames."<<endl;
16✔
380
      // Be sure to use a ZoneName with a variant matching the domain we are
381
      // working on, for the sake of unsetNSEC3PARAM.
382
      ZoneName zonename(rr->d_name, di->zone.getVariant());
16✔
383
      if (rr->d_class == QClass::ANY) {
16!
384
        d_dk.unsetNSEC3PARAM(zonename);
16✔
385
      }
16✔
386
      else if (rr->d_class == QClass::NONE) {
×
387
        NSEC3PARAMRecordContent nsec3rr(rr->getContent()->getZoneRepresentation(), di->zone);
×
388
        if (*haveNSEC3 && ns3pr->getZoneRepresentation() == nsec3rr.getZoneRepresentation())
×
389
          d_dk.unsetNSEC3PARAM(zonename);
×
390
        else
×
391
          return 0;
×
392
      } else
×
393
        return 0;
×
394

395
      // Update NSEC3 variables, other RR's in this update package might need them as well.
396
      *haveNSEC3 = false;
16✔
397
      *narrow = false;
16✔
398

399
      string error;
16✔
400
      string info;
16✔
401
      if (!d_dk.rectifyZone(di->zone, error, info, false)) {
16!
402
        throw PDNSException("Failed to rectify '" + di->zone.toLogString() + "': " + error);
×
403
      }
×
404
      return 1;
16✔
405
    } // end of NSEC3PARAM delete block
16✔
406

407

408
    di->backend->lookup(rrType, rr->d_name, di->id);
632✔
409
    while(di->backend->get(rec)) {
2,584✔
410
      if (rr->d_class == QClass::ANY) { // 3.4.2.3
1,952✔
411
        if (rec.qname == di->zone.operator const DNSName&() && (rec.qtype == QType::NS || rec.qtype == QType::SOA)) { // Never delete all SOA and NS's
1,632!
412
          rrset.push_back(rec);
24✔
413
        }
24✔
414
        else
1,608✔
415
          recordsToDelete.push_back(rec);
1,608✔
416
      }
1,632✔
417
      if (rr->d_class == QClass::NONE) { // 3.4.2.4
1,952✔
418
        auto repr = rec.getZoneRepresentation();
320✔
419
        if (rec.qtype == QType::TXT) {
320✔
420
          DLOG(g_log<<msgPrefix<<"Adjusting TXT content from ["<<repr<<"]"<<endl);
36✔
421
          auto drc = DNSRecordContent::make(rec.qtype.getCode(), QClass::IN, repr);
36✔
422
          auto ser = drc->serialize(rec.qname, true, true);
36✔
423
          auto rc = DNSRecordContent::deserialize(rec.qname, rec.qtype.getCode(), ser);
36✔
424
          repr = rc->getZoneRepresentation(true);
36✔
425
          DLOG(g_log<<msgPrefix<<"Adjusted TXT content to ["<<repr<<"]"<<endl);
36✔
426
        }
36✔
427
        DLOG(g_log<<msgPrefix<<"Matching RR in RRset - (adjusted) representation from request=["<<repr<<"], rr->getContent()->getZoneRepresentation()=["<<rr->getContent()->getZoneRepresentation()<<"]"<<endl);
320✔
428
        if (rrType == rec.qtype && repr == rr->getContent()->getZoneRepresentation())
320!
429
          recordsToDelete.push_back(rec);
224✔
430
        else
96✔
431
          rrset.push_back(rec);
96✔
432
      }
320✔
433
    }
1,952✔
434
  
435
    if (recordsToDelete.size()) {
632✔
436
      di->backend->replaceRRSet(di->id, rr->d_name, rrType, rrset);
608✔
437
      g_log<<Logger::Notice<<msgPrefix<<"Deleting record "<<rr->d_name<<"|"<<rrType.toString()<<endl;
608✔
438
      changedRecords += recordsToDelete.size();
608✔
439

440

441
      // If we've removed a delegate, we need to reset ordername/auth for some records.
442
      if (rrType == QType::NS && rr->d_name != di->zone.operator const DNSName&()) { 
608✔
443
        vector<DNSName> belowOldDelegate, nsRecs, updateAuthFlag;
84✔
444
        di->backend->listSubZone(ZoneName(rr->d_name), di->id);
84✔
445
        while (di->backend->get(rec)) {
216✔
446
          if (rec.qtype.getCode()) // skip ENT records, they are always auth=false
132!
447
            belowOldDelegate.push_back(rec.qname);
132✔
448
          if (rec.qtype.getCode() == QType::NS && rec.qname != rr->d_name)
132!
449
            nsRecs.push_back(rec.qname);
12✔
450
        }
132✔
451

452
        for(auto &belowOldDel: belowOldDelegate)
84✔
453
        {
132✔
454
          bool isBelowDelegate = false;
132✔
455
          for(const auto & ns: nsRecs) {
132✔
456
            if (ns.isPartOf(belowOldDel)) {
36✔
457
              isBelowDelegate=true;
12✔
458
              break;
12✔
459
            }
12✔
460
          }
36✔
461
          if (!isBelowDelegate)
132✔
462
            updateAuthFlag.push_back(belowOldDel);
120✔
463
        }
132✔
464

465
        for (const auto &changeRec:updateAuthFlag) {
120✔
466
          if(*haveNSEC3)  {
120✔
467
            DNSName ordername;
60✔
468
            if(! *narrow)
60✔
469
              ordername=DNSName(toBase32Hex(hashQNameWithSalt(*ns3pr, changeRec)));
40✔
470

471
            di->backend->updateDNSSECOrderNameAndAuth(di->id, changeRec, ordername, true);
60✔
472
          }
60✔
473
          else { // NSEC
60✔
474
            DNSName ordername=changeRec.makeRelative(di->zone);
60✔
475
            di->backend->updateDNSSECOrderNameAndAuth(di->id, changeRec, ordername, true);
60✔
476
          }
60✔
477
        }
120✔
478
      }
84✔
479

480
      // Fix ENT records.
481
      // We must check if we have a record below the current level and if we removed the 'last' record
482
      // on that level. If so, we must insert an ENT record.
483
      // We take extra care here to not 'include' the record that we just deleted. Some backends will still return it as they only reload on a commit.
484
      bool foundDeeper = false, foundOtherWithSameName = false;
608✔
485
      di->backend->listSubZone(ZoneName(rr->d_name), di->id);
608✔
486
      while (di->backend->get(rec)) {
3,452✔
487
        if (rec.qname == rr->d_name && !count(recordsToDelete.begin(), recordsToDelete.end(), rec))
2,844!
488
          foundOtherWithSameName = true;
516✔
489
        if (rec.qname != rr->d_name && rec.qtype.getCode() != QType::NS) //Skip NS records, as this would be a delegate that we can ignore as this does not require us to create a ENT
2,844✔
490
          foundDeeper = true;
2,112✔
491
      }
2,844✔
492

493
      if (foundDeeper && !foundOtherWithSameName) {
608✔
494
        insnonterm.insert(rr->d_name);
72✔
495
      } else if (!foundOtherWithSameName) {
536✔
496
        // If we didn't have to insert an ENT, we might have deleted a record at very deep level
497
        // and we must then clean up the ENT's above the deleted record.
498
        ZoneName shorter(rr->d_name);
392✔
499
        while (shorter != di->zone) {
608!
500
          shorter.chopOff();
608✔
501
          bool foundRealRR = false;
608✔
502
          bool foundEnt = false;
608✔
503

504
          // The reason for a listSubZone here is because might go up the tree and find the ENT of another branch
505
          // consider these non ENT-records:
506
          // b.c.d.e.test.com
507
          // b.d.e.test.com
508
          // if we delete b.c.d.e.test.com, we go up to d.e.test.com and then find b.d.e.test.com because that's below d.e.test.com.
509
          // At that point we can stop deleting ENT's because the tree is in tact again.
510
          di->backend->listSubZone(shorter, di->id);
608✔
511

512
          while (di->backend->get(rec)) {
8,276✔
513
            if (rec.qtype.getCode())
7,668✔
514
              foundRealRR = true;
6,628✔
515
            else
1,040✔
516
              foundEnt = true;
1,040✔
517
          }
7,668✔
518
          if (!foundRealRR) {
608✔
519
            if (foundEnt) // only delete the ENT if we actually found one.
216✔
520
              delnonterm.insert(shorter.operator const DNSName&());
180✔
521
          } else
216✔
522
            break;
392✔
523
        }
608✔
524
      }
392✔
525
    } else { // if (recordsToDelete.size())
608✔
526
      g_log<<Logger::Notice<<msgPrefix<<"Deletion for record "<<rr->d_name<<"|"<<rrType.toString()<<" requested, but not found."<<endl;
24✔
527
    }
24✔
528
  } // (End of delete block d_class == ANY || d_class == NONE
632✔
529
  
530

531

532
  //Insert and delete ENT's
533
  if (insnonterm.size() > 0 || delnonterm.size() > 0) {
2,608✔
534
    DLOG(g_log<<msgPrefix<<"Updating ENT records - "<<insnonterm.size()<<"|"<<delnonterm.size()<<endl);
1,988✔
535
    di->backend->updateEmptyNonTerminals(di->id, insnonterm, delnonterm, false);
1,988✔
536
    for (const auto &i: insnonterm) {
1,988✔
537
      string hashed;
192✔
538
      if(*haveNSEC3)
192✔
539
      {
96✔
540
        DNSName ordername;
96✔
541
        if(! *narrow)
96✔
542
          ordername=DNSName(toBase32Hex(hashQNameWithSalt(*ns3pr, i)));
64✔
543
        di->backend->updateDNSSECOrderNameAndAuth(di->id, i, ordername, true);
96✔
544
      }
96✔
545
    }
192✔
546
  }
1,988✔
547

548
  return changedRecords;
2,608✔
549
}
2,624✔
550

551
int PacketHandler::forwardPacket(const string &msgPrefix, const DNSPacket& p, const DomainInfo& di) {
×
552
  vector<string> forward;
×
553
  B.getDomainMetadata(p.qdomainzone, "FORWARD-DNSUPDATE", forward);
×
554

555
  if (forward.size() == 0 && ! ::arg().mustDo("forward-dnsupdate")) {
×
556
    g_log << Logger::Notice << msgPrefix << "Not configured to forward to primary, returning Refused." << endl;
×
557
    return RCode::Refused;
×
558
  }
×
559

560
  for (const auto& remote : di.primaries) {
×
561
    g_log << Logger::Notice << msgPrefix << "Forwarding packet to primary " << remote << endl;
×
562

563
    if (!pdns::isQueryLocalAddressFamilyEnabled(remote.sin4.sin_family)) {
×
564
      continue;
×
565
    }
×
566
    auto local = pdns::getQueryLocalAddress(remote.sin4.sin_family, 0);
×
567
    int sock = makeQuerySocket(local, false); // create TCP socket. RFC2136 section 6.2 seems to be ok with this.
×
568
    if(sock < 0) {
×
569
      g_log<<Logger::Error<<msgPrefix<<"Error creating socket: "<<stringerror()<<endl;
×
570
      continue;
×
571
    }
×
572

573
    if( connect(sock, (struct sockaddr*)&remote, remote.getSocklen()) < 0 ) {
×
574
      g_log<<Logger::Error<<msgPrefix<<"Failed to connect to "<<remote.toStringWithPort()<<": "<<stringerror()<<endl;
×
575
      try {
×
576
        closesocket(sock);
×
577
      }
×
578
      catch(const PDNSException& e) {
×
579
        g_log << Logger::Error << "Error closing primary forwarding socket after connect() failed: " << e.reason << endl;
×
580
      }
×
581
      continue;
×
582
    }
×
583

584
    DNSPacket l_forwardPacket(p);
×
585
    l_forwardPacket.setID(dns_random_uint16());
×
586
    l_forwardPacket.setRemote(&remote);
×
587
    uint16_t len=htons(l_forwardPacket.getString().length());
×
588
    string buffer((const char*)&len, 2);
×
589
    buffer.append(l_forwardPacket.getString());
×
590
    if(write(sock, buffer.c_str(), buffer.length()) < 0) {
×
591
      g_log<<Logger::Error<<msgPrefix<<"Unable to forward update message to "<<remote.toStringWithPort()<<", error:"<<stringerror()<<endl;
×
592
      try {
×
593
        closesocket(sock);
×
594
      }
×
595
      catch(const PDNSException& e) {
×
596
        g_log << Logger::Error << "Error closing primary forwarding socket after write() failed: " << e.reason << endl;
×
597
      }
×
598
      continue;
×
599
    }
×
600

601
    int res = waitForData(sock, 10, 0);
×
602
    if (!res) {
×
603
      g_log << Logger::Error << msgPrefix << "Timeout waiting for reply from primary at " << remote.toStringWithPort() << endl;
×
604
      try {
×
605
        closesocket(sock);
×
606
      }
×
607
      catch(const PDNSException& e) {
×
608
        g_log << Logger::Error << "Error closing primary forwarding socket after a timeout occurred: " << e.reason << endl;
×
609
      }
×
610
      continue;
×
611
    }
×
612
    if (res < 0) {
×
613
      g_log << Logger::Error << msgPrefix << "Error waiting for answer from primary at " << remote.toStringWithPort() << ", error:" << stringerror() << endl;
×
614
      try {
×
615
        closesocket(sock);
×
616
      }
×
617
      catch(const PDNSException& e) {
×
618
        g_log << Logger::Error << "Error closing primary forwarding socket after an error occurred: " << e.reason << endl;
×
619
      }
×
620
      continue;
×
621
    }
×
622

623
    unsigned char lenBuf[2];
×
624
    ssize_t recvRes;
×
625
    recvRes = recv(sock, &lenBuf, sizeof(lenBuf), 0);
×
626
    if (recvRes < 0 || static_cast<size_t>(recvRes) < sizeof(lenBuf)) {
×
627
      g_log << Logger::Error << msgPrefix << "Could not receive data (length) from primary at " << remote.toStringWithPort() << ", error:" << stringerror() << endl;
×
628
      try {
×
629
        closesocket(sock);
×
630
      }
×
631
      catch(const PDNSException& e) {
×
632
        g_log << Logger::Error << "Error closing primary forwarding socket after recv() failed: " << e.reason << endl;
×
633
      }
×
634
      continue;
×
635
    }
×
636
    size_t packetLen = lenBuf[0]*256+lenBuf[1];
×
637

638
    buffer.resize(packetLen);
×
639
    recvRes = recv(sock, &buffer.at(0), packetLen, 0);
×
640
    if (recvRes < 0) {
×
641
      g_log << Logger::Error << msgPrefix << "Could not receive data (dnspacket) from primary at " << remote.toStringWithPort() << ", error:" << stringerror() << endl;
×
642
      try {
×
643
        closesocket(sock);
×
644
      }
×
645
      catch(const PDNSException& e) {
×
646
        g_log << Logger::Error << "Error closing primary forwarding socket after recv() failed: " << e.reason << endl;
×
647
      }
×
648
      continue;
×
649
    }
×
650
    try {
×
651
      closesocket(sock);
×
652
    }
×
653
    catch(const PDNSException& e) {
×
654
      g_log << Logger::Error << "Error closing primary forwarding socket: " << e.reason << endl;
×
655
    }
×
656

657
    try {
×
658
      MOADNSParser mdp(false, buffer.data(), static_cast<unsigned int>(recvRes));
×
659
      g_log<<Logger::Info<<msgPrefix<<"Forward update message to "<<remote.toStringWithPort()<<", result was RCode "<<mdp.d_header.rcode<<endl;
×
660
      return mdp.d_header.rcode;
×
661
    }
×
662
    catch (...) {
×
663
      g_log << Logger::Error << msgPrefix << "Failed to parse response packet from primary at " << remote.toStringWithPort() << endl;
×
664
      continue;
×
665
    }
×
666
  }
×
667
  g_log << Logger::Error << msgPrefix << "Failed to forward packet to primary(s). Returning ServFail." << endl;
×
668
  return RCode::ServFail;
×
669

670
}
×
671

672
int PacketHandler::processUpdate(DNSPacket& packet) { // NOLINT(readability-function-cognitive-complexity)
1,252✔
673
  if (! ::arg().mustDo("dnsupdate"))
1,252!
674
    return RCode::Refused;
×
675

676
  string msgPrefix="UPDATE (" + std::to_string(packet.d.id) + ") from " + packet.getRemoteString() + " for " + packet.qdomainzone.toLogString() + ": ";
1,252✔
677
  g_log<<Logger::Info<<msgPrefix<<"Processing started."<<endl;
1,252✔
678

679
  // if there is policy, we delegate all checks to it
680
  if (this->d_update_policy_lua == nullptr) {
1,252!
681

682
    // Check permissions - IP based
683
    vector<string> allowedRanges;
1,252✔
684
    B.getDomainMetadata(packet.qdomainzone, "ALLOW-DNSUPDATE-FROM", allowedRanges);
1,252✔
685
    if (! ::arg()["allow-dnsupdate-from"].empty())
1,252!
686
      stringtok(allowedRanges, ::arg()["allow-dnsupdate-from"], ", \t" );
1,252✔
687

688
    NetmaskGroup ng;
1,252✔
689
    for(const auto& i: allowedRanges) {
2,504✔
690
      ng.addMask(i);
2,504✔
691
    }
2,504✔
692

693
    if ( ! ng.match(packet.getInnerRemote())) {
1,252!
694
      g_log<<Logger::Error<<msgPrefix<<"Remote not listed in allow-dnsupdate-from or domainmetadata. Sending REFUSED"<<endl;
×
695
      return RCode::Refused;
×
696
    }
×
697

698

699
    // Check permissions - TSIG based.
700
    vector<string> tsigKeys;
1,252✔
701
    B.getDomainMetadata(packet.qdomainzone, "TSIG-ALLOW-DNSUPDATE", tsigKeys);
1,252✔
702
    if (tsigKeys.size() > 0) {
1,252!
703
      bool validKey = false;
×
704

705
      TSIGRecordContent trc;
×
706
      DNSName inputkey;
×
707
      string message;
×
708
      if (! packet.getTSIGDetails(&trc,  &inputkey)) {
×
709
        g_log<<Logger::Error<<msgPrefix<<"TSIG key required, but packet does not contain key. Sending REFUSED"<<endl;
×
710
        return RCode::Refused;
×
711
      }
×
712
#ifdef ENABLE_GSS_TSIG
×
713
      if (g_doGssTSIG && packet.d_tsig_algo == TSIG_GSS) {
×
714
        GssName inputname(packet.d_peer_principal); // match against principal since GSS requires that
×
715
        for(const auto& key: tsigKeys) {
×
716
          if (inputname.match(key)) {
×
717
            validKey = true;
×
718
            break;
×
719
          }
×
720
        }
×
721
      }
×
722
      else
×
723
#endif
×
724
        {
×
725
        for(const auto& key: tsigKeys) {
×
726
          if (inputkey == DNSName(key)) { // because checkForCorrectTSIG has already been performed earlier on, if the name of the key matches with the domain given it is valid.
×
727
            validKey=true;
×
728
            break;
×
729
          }
×
730
        }
×
731
      }
×
732

733
      if (!validKey) {
×
734
        g_log<<Logger::Error<<msgPrefix<<"TSIG key ("<<inputkey<<") required, but no matching key found in domainmetadata, tried "<<tsigKeys.size()<<". Sending REFUSED"<<endl;
×
735
        return RCode::Refused;
×
736
      }
×
737
    } else if(::arg().mustDo("dnsupdate-require-tsig")) {
1,252!
738
      g_log<<Logger::Error<<msgPrefix<<"TSIG key required, but domain is not secured with TSIG. Sending REFUSED"<<endl;
×
739
      return RCode::Refused;
×
740
    }
×
741

742
    if (tsigKeys.empty() && packet.d_havetsig) {
1,252!
743
      g_log<<Logger::Warning<<msgPrefix<<"TSIG is provided, but domain is not secured with TSIG. Processing continues"<<endl;
×
744
    }
×
745

746
  }
1,252✔
747

748
  // RFC2136 uses the same DNS Header and Message as defined in RFC1035.
749
  // This means we can use the MOADNSParser to parse the incoming packet. The result is that we have some different
750
  // variable names during the use of our MOADNSParser.
751
  MOADNSParser mdp(false, packet.getString());
1,252✔
752
  if (mdp.d_header.qdcount != 1) {
1,252!
753
    g_log<<Logger::Warning<<msgPrefix<<"Zone Count is not 1, sending FormErr"<<endl;
×
754
    return RCode::FormErr;
×
755
  }
×
756

757
  if (packet.qtype.getCode() != QType::SOA) { // RFC2136 2.3 - ZTYPE must be SOA
1,252!
758
    g_log<<Logger::Warning<<msgPrefix<<"Query ZTYPE is not SOA, sending FormErr"<<endl;
×
759
    return RCode::FormErr;
×
760
  }
×
761

762
  if (packet.qclass != QClass::IN) {
1,252!
763
    g_log<<Logger::Warning<<msgPrefix<<"Class is not IN, sending NotAuth"<<endl;
×
764
    return RCode::NotAuth;
×
765
  }
×
766

767
  DomainInfo di;
1,252✔
768
  di.backend=nullptr;
1,252✔
769
  if(!B.getDomainInfo(packet.qdomainzone, di) || (di.backend == nullptr)) {
1,252!
770
    g_log<<Logger::Error<<msgPrefix<<"Can't determine backend for domain '"<<packet.qdomainzone<<"' (or backend does not support DNS update operation)"<<endl;
12✔
771
    return RCode::NotAuth;
12✔
772
  }
12✔
773

774
  if (di.kind == DomainInfo::Secondary)
1,240!
775
    return forwardPacket(msgPrefix, packet, di);
×
776

777
  // Check if all the records provided are within the zone
778
  for(const auto & answer : mdp.d_answers) {
3,032✔
779
    const DNSRecord *dnsRecord = &answer;
3,032✔
780
    // Skip this check for other field types (like the TSIG -  which is in the additional section)
781
    // For a TSIG, the label is the dnskey, so it does not pass the endOn validation.
782
    if (dnsRecord->d_place != DNSResourceRecord::ANSWER && dnsRecord->d_place != DNSResourceRecord::AUTHORITY) {
3,032!
783
      continue;
×
784
    }
×
785

786
    if (!dnsRecord->d_name.isPartOf(di.zone)) {
3,032✔
787
      g_log<<Logger::Error<<msgPrefix<<"Received update/record out of zone, sending NotZone."<<endl;
24✔
788
      return RCode::NotZone;
24✔
789
    }
24✔
790
  }
3,032✔
791

792

793
  std::lock_guard<std::mutex> l(s_rfc2136lock); //TODO: i think this lock can be per zone, not for everything
1,216✔
794
  g_log<<Logger::Info<<msgPrefix<<"starting transaction."<<endl;
1,216✔
795
  if (!di.backend->startTransaction(packet.qdomainzone, UnknownDomainID)) { // Not giving the domain_id means that we do not delete the existing records.
1,216!
796
    g_log<<Logger::Error<<msgPrefix<<"Backend for domain "<<packet.qdomainzone<<" does not support transaction. Can't do Update packet."<<endl;
×
797
    return RCode::NotImp;
×
798
  }
×
799

800
  // 3.2.1 and 3.2.2 - Prerequisite check
801
  for(const auto & answer : mdp.d_answers) {
2,960✔
802
    const DNSRecord *dnsRecord = &answer;
2,960✔
803
    if (dnsRecord->d_place == DNSResourceRecord::ANSWER) {
2,960✔
804
      int res = checkUpdatePrerequisites(dnsRecord, &di);
228✔
805
      if (res>0) {
228✔
806
        g_log<<Logger::Error<<msgPrefix<<"Failed PreRequisites check for "<<dnsRecord->d_name<<", returning "<<RCode::to_s(res)<<endl;
60✔
807
        di.backend->abortTransaction();
60✔
808
        return res;
60✔
809
      }
60✔
810
    }
228✔
811
  }
2,960✔
812

813
  // 3.2.3 - Prerequisite check - this is outside of updatePrerequisitesCheck because we check an RRSet and not the RR.
814
  typedef pair<DNSName, QType> rrSetKey_t;
1,156✔
815
  typedef vector<DNSResourceRecord> rrVector_t;
1,156✔
816
  typedef std::map<rrSetKey_t, rrVector_t> RRsetMap_t;
1,156✔
817
  RRsetMap_t preReqRRsets;
1,156✔
818
  for(const auto& i: mdp.d_answers) {
2,900✔
819
    const DNSRecord* dnsRecord = &i;
2,900✔
820
    if (dnsRecord->d_place == DNSResourceRecord::ANSWER) {
2,900✔
821
      // Last line of 3.2.3
822
      if (dnsRecord->d_class != QClass::IN && dnsRecord->d_class != QClass::NONE && dnsRecord->d_class != QClass::ANY) {
168!
823
        di.backend->abortTransaction();
×
824
        return RCode::FormErr;
×
UNCOV
825
      }
×
826

827
      if (dnsRecord->d_class == QClass::IN) {
168✔
828
        rrSetKey_t key = {dnsRecord->d_name, QType(dnsRecord->d_type)};
108✔
829
        rrVector_t *vec = &preReqRRsets[key];
108✔
830
        vec->push_back(DNSResourceRecord::fromWire(*dnsRecord));
108✔
831
      }
108✔
832
    }
168✔
833
  }
2,900✔
834

835
  if (preReqRRsets.size() > 0) {
1,156✔
836
    RRsetMap_t zoneRRsets;
36✔
837
    for (auto & preReqRRset : preReqRRsets) {
36✔
838
      rrSetKey_t rrSet=preReqRRset.first;
36✔
839
      rrVector_t *vec = &preReqRRset.second;
36✔
840

841
      DNSResourceRecord rec;
36✔
842
      di.backend->lookup(QType(QType::ANY), rrSet.first, di.id);
36✔
843
      uint16_t foundRR=0, matchRR=0;
36✔
844
      while (di.backend->get(rec)) {
144✔
845
        if (rec.qtype == rrSet.second) {
108!
846
          foundRR++;
108✔
847
          for(auto & rrItem : *vec) {
324✔
848
            rrItem.ttl = rec.ttl; // The compare one line below also compares TTL, so we make them equal because TTL is not user within prerequisite checks.
324✔
849
            if (rrItem == rec)
324✔
850
              matchRR++;
108✔
851
          }
324✔
852
        }
108✔
853
      }
108✔
854
      if (matchRR != foundRR || foundRR != vec->size()) {
36!
855
        g_log<<Logger::Error<<msgPrefix<<"Failed PreRequisites check (RRs differ), returning NXRRSet"<<endl;
24✔
856
        di.backend->abortTransaction();
24✔
857
        return RCode::NXRRSet;
24✔
858
      }
24✔
859
    }
36✔
860
  }
36✔
861

862

863

864
  // 3.4 - Prescan & Add/Update/Delete records - is all done within a try block.
865
  try {
1,132✔
866
    uint changedRecords = 0;
1,132✔
867
    // 3.4.1 - Prescan section
868
    for(const auto & answer : mdp.d_answers) {
2,828✔
869
      const DNSRecord *dnsRecord = &answer;
2,828✔
870
      if (dnsRecord->d_place == DNSResourceRecord::AUTHORITY) {
2,828✔
871
        int res = checkUpdatePrescan(dnsRecord);
2,732✔
872
        if (res>0) {
2,732✔
873
          g_log<<Logger::Error<<msgPrefix<<"Failed prescan check, returning "<<res<<endl;
12✔
874
          di.backend->abortTransaction();
12✔
875
          return res;
12✔
876
        }
12✔
877
      }
2,732✔
878
    }
2,828✔
879

880
    bool updatedSerial=false;
1,120✔
881
    NSEC3PARAMRecordContent ns3pr;
1,120✔
882
    bool narrow=false;
1,120✔
883
    bool haveNSEC3 = d_dk.getNSEC3PARAM(di.zone, &ns3pr, &narrow);
1,120✔
884
    bool isPresigned = d_dk.isPresigned(di.zone);
1,120✔
885
    string soaEditSetting;
1,120✔
886
    d_dk.getSoaEdit(di.zone, soaEditSetting);
1,120✔
887

888
    // 3.4.2 - Perform the updates.
889
    // There's a special condition where deleting the last NS record at zone apex is never deleted (3.4.2.4)
890
    // This means we must do it outside the normal performUpdate() because that focusses only on a separate RR.
891
    vector<const DNSRecord *> nsRRtoDelete;
1,120✔
892

893
    // Another special case is the addition of both a CNAME and a non-CNAME for the same name (#6270)
894
    set<DNSName> cn, nocn;
1,120✔
895
    for (const auto &rr : mdp.d_answers) {
2,816✔
896
      if (rr.d_place == DNSResourceRecord::AUTHORITY && rr.d_class == QClass::IN && rr.d_ttl > 0) {
2,816!
897
        // Addition
898
        if (rr.d_type == QType::CNAME) {
2,036✔
899
          cn.insert(rr.d_name);
96✔
900
        } else if (rr.d_type != QType::RRSIG) {
1,940!
901
          nocn.insert(rr.d_name);
1,940✔
902
        }
1,940✔
903
      }
2,036✔
904
    }
2,816✔
905
    for (auto const &n : cn) {
1,120✔
906
      if (nocn.count(n) > 0) {
96✔
907
        g_log<<Logger::Error<<msgPrefix<<"Refusing update, found CNAME and non-CNAME addition"<<endl;
12✔
908
        di.backend->abortTransaction();
12✔
909
        return RCode::FormErr;
12✔
910
      }
12✔
911
    }
96✔
912

913
    vector<const DNSRecord *> cnamesToAdd, nonCnamesToAdd;
1,108✔
914
    for(const auto & answer : mdp.d_answers) {
2,792✔
915
      const DNSRecord *dnsRecord = &answer;
2,792✔
916
      if (dnsRecord->d_place == DNSResourceRecord::AUTHORITY) {
2,792✔
917
        /* see if it's permitted by policy */
918
        if (this->d_update_policy_lua != nullptr) {
2,696!
919
          if (!this->d_update_policy_lua->updatePolicy(dnsRecord->d_name, QType(dnsRecord->d_type), di.zone.operator const DNSName&(), packet)) {
×
920
            g_log<<Logger::Warning<<msgPrefix<<"Refusing update for " << dnsRecord->d_name << "/" << QType(dnsRecord->d_type).toString() << ": Not permitted by policy"<<endl;
×
921
            continue;
×
922
          } else {
×
923
            g_log<<Logger::Debug<<msgPrefix<<"Accepting update for " << dnsRecord->d_name << "/" << QType(dnsRecord->d_type).toString() << ": Permitted by policy"<<endl;
×
924
          }
×
UNCOV
925
        }
×
926

927
        if (dnsRecord->d_class == QClass::NONE  && dnsRecord->d_type == QType::NS && dnsRecord->d_name == di.zone.operator const DNSName&()) {
2,696!
928
          nsRRtoDelete.push_back(dnsRecord);
36✔
929
        }
36✔
930
        else if (dnsRecord->d_class == QClass::IN &&  dnsRecord->d_ttl > 0) {
2,660!
931
          if (dnsRecord->d_type == QType::CNAME) {
2,012✔
932
            cnamesToAdd.push_back(dnsRecord);
84✔
933
          } else {
1,928✔
934
            nonCnamesToAdd.push_back(dnsRecord);
1,928✔
935
          }
1,928✔
936
        }
2,012✔
937
        else
648✔
938
          changedRecords += performUpdate(msgPrefix, dnsRecord, &di, isPresigned, &narrow, &haveNSEC3, &ns3pr, &updatedSerial);
648✔
939
      }
2,696✔
940
    }
2,792✔
941
    for (const auto &rr : cnamesToAdd) {
1,108✔
942
      DNSResourceRecord rec;
84✔
943
      di.backend->lookup(QType(QType::ANY), rr->d_name, di.id);
84✔
944
      while (di.backend->get(rec)) {
120✔
945
        if (rec.qtype != QType::CNAME && rec.qtype != QType::ENT && rec.qtype != QType::RRSIG) {
48!
946
          // leave database handle in a consistent state
947
          while (di.backend->get(rec))
12!
UNCOV
948
            ;
×
949
          g_log<<Logger::Warning<<msgPrefix<<"Refusing update for " << rr->d_name << "/" << QType(rr->d_type).toString() << ": Data other than CNAME exists for the same name"<<endl;
12✔
950
          di.backend->abortTransaction();
12✔
951
          return RCode::Refused;
12✔
952
        }
12✔
953
      }
48✔
954
      changedRecords += performUpdate(msgPrefix, rr, &di, isPresigned, &narrow, &haveNSEC3, &ns3pr, &updatedSerial);
72✔
955
    }
72✔
956
    for (const auto &rr : nonCnamesToAdd) {
1,928✔
957
      DNSResourceRecord rec;
1,928✔
958
      di.backend->lookup(QType(QType::CNAME), rr->d_name, di.id);
1,928✔
959
      while (di.backend->get(rec)) {
1,928✔
960
        if (rec.qtype == QType::CNAME && rr->d_type != QType::RRSIG) {
12!
961
          // leave database handle in a consistent state
962
          while (di.backend->get(rec))
12!
UNCOV
963
            ;
×
964
          g_log<<Logger::Warning<<msgPrefix<<"Refusing update for " << rr->d_name << "/" << QType(rr->d_type).toString() << ": CNAME exists for the same name"<<endl;
12✔
965
          di.backend->abortTransaction();
12✔
966
          return RCode::Refused;
12✔
967
        }
12✔
968
      }
12✔
969
      changedRecords += performUpdate(msgPrefix, rr, &di, isPresigned, &narrow, &haveNSEC3, &ns3pr, &updatedSerial);
1,916✔
970
    }
1,916✔
971
    if (nsRRtoDelete.size()) {
1,084✔
972
      vector<DNSResourceRecord> nsRRInZone;
24✔
973
      DNSResourceRecord rec;
24✔
974
      di.backend->lookup(QType(QType::NS), di.zone.operator const DNSName&(), di.id);
24✔
975
      while (di.backend->get(rec)) {
72✔
976
        nsRRInZone.push_back(rec);
48✔
977
      }
48✔
978
      if (nsRRInZone.size() > nsRRtoDelete.size()) { // only delete if the NS's we delete are less then what we have in the zone (3.4.2.4)
24✔
979
        for (auto& inZone: nsRRInZone) {
24✔
980
          for (auto& rr: nsRRtoDelete) {
24✔
981
            if (inZone.getZoneRepresentation() == (rr)->getContent()->getZoneRepresentation())
24✔
982
              changedRecords += performUpdate(msgPrefix, rr, &di, isPresigned, &narrow, &haveNSEC3, &ns3pr, &updatedSerial);
12✔
983
          }
24✔
984
        }
24✔
985
      }
12✔
986
    }
24✔
987

988
    // Section 3.6 - Update the SOA serial - outside of performUpdate because we do a SOA update for the complete update message
989
    if (changedRecords > 0 && !updatedSerial) {
1,084✔
990
      increaseSerial(msgPrefix, &di, soaEditSetting, haveNSEC3, narrow, &ns3pr);
964✔
991
      changedRecords++;
964✔
992
    }
964✔
993

994
    if (changedRecords > 0) {
1,084✔
995
      if (!di.backend->commitTransaction()) {
988!
996
       g_log<<Logger::Error<<msgPrefix<<"Failed to commit updates!"<<endl;
×
997
        return RCode::ServFail;
×
UNCOV
998
      }
×
999

1000
      S.deposit("dnsupdate-changes", changedRecords);
988✔
1001

1002
      d_dk.clearMetaCache(di.zone);
988✔
1003
      // Purge the records!
1004
      string zone(di.zone.toString());
988✔
1005
      zone.append("$");
988✔
1006
      purgeAuthCaches(zone);
988✔
1007

1008
      // Notify secondaries
1009
      if (di.kind == DomainInfo::Primary) {
988!
1010
        vector<string> notify;
988✔
1011
        B.getDomainMetadata(packet.qdomainzone, "NOTIFY-DNSUPDATE", notify);
988✔
1012
        if (!notify.empty() && notify.front() == "1") {
988!
1013
          Communicator.notifyDomain(di.zone, &B);
×
UNCOV
1014
        }
×
1015
      }
988✔
1016

1017
      g_log<<Logger::Info<<msgPrefix<<"Update completed, "<<changedRecords<<" changed records committed."<<endl;
988✔
1018
    } else {
988✔
1019
      //No change, no commit, we perform abort() because some backends might like this more.
1020
      g_log<<Logger::Info<<msgPrefix<<"Update completed, 0 changes, rolling back."<<endl;
96✔
1021
      di.backend->abortTransaction();
96✔
1022
    }
96✔
1023
    return RCode::NoError; //rfc 2136 3.4.2.5
1,084✔
1024
  }
1,084✔
1025
  catch (SSqlException &e) {
1,132✔
1026
    g_log<<Logger::Error<<msgPrefix<<"Caught SSqlException: "<<e.txtReason()<<"; Sending ServFail!"<<endl;
×
1027
    di.backend->abortTransaction();
×
1028
    return RCode::ServFail;
×
UNCOV
1029
  }
×
1030
  catch (DBException &e) {
1,132✔
1031
    g_log<<Logger::Error<<msgPrefix<<"Caught DBException: "<<e.reason<<"; Sending ServFail!"<<endl;
×
1032
    di.backend->abortTransaction();
×
1033
    return RCode::ServFail;
×
UNCOV
1034
  }
×
1035
  catch (PDNSException &e) {
1,132✔
1036
    g_log<<Logger::Error<<msgPrefix<<"Caught PDNSException: "<<e.reason<<"; Sending ServFail!"<<endl;
×
1037
    di.backend->abortTransaction();
×
1038
    return RCode::ServFail;
×
UNCOV
1039
  }
×
1040
  catch(std::exception &e) {
1,132✔
1041
    g_log<<Logger::Error<<msgPrefix<<"Caught std:exception: "<<e.what()<<"; Sending ServFail!"<<endl;
×
1042
    di.backend->abortTransaction();
×
1043
    return RCode::ServFail;
×
UNCOV
1044
  }
×
1045
  catch (...) {
1,132✔
1046
    g_log<<Logger::Error<<msgPrefix<<"Caught unknown exception when performing update. Sending ServFail!"<<endl;
×
1047
    di.backend->abortTransaction();
×
1048
    return RCode::ServFail;
×
UNCOV
1049
  }
×
1050
}
1,132✔
1051

1052
void PacketHandler::increaseSerial(const string &msgPrefix, const DomainInfo *di, const string& soaEditSetting,  bool haveNSEC3, bool narrow, const NSEC3PARAMRecordContent *ns3pr) {
964✔
1053
  SOAData sd;
964✔
1054
  if (!di->backend->getSOA(di->zone, di->id, sd)) {
964!
1055
    throw PDNSException("SOA-Serial update failed because there was no SOA. Wowie.");
×
UNCOV
1056
  }
×
1057

1058
  uint32_t oldSerial = sd.serial;
964✔
1059

1060
  vector<string> soaEdit2136Setting;
964✔
1061
  B.getDomainMetadata(di->zone, "SOA-EDIT-DNSUPDATE", soaEdit2136Setting);
964✔
1062
  string soaEdit2136 = "DEFAULT";
964✔
1063
  string soaEdit;
964✔
1064
  if (!soaEdit2136Setting.empty()) {
964!
1065
    soaEdit2136 = soaEdit2136Setting[0];
×
1066
    if (pdns_iequals(soaEdit2136, "SOA-EDIT") || pdns_iequals(soaEdit2136,"SOA-EDIT-INCREASE") ){
×
1067
      if (soaEditSetting.empty()) {
×
1068
        g_log<<Logger::Error<<msgPrefix<<"Using "<<soaEdit2136<<" for SOA-EDIT-DNSUPDATE increase on DNS update, but SOA-EDIT is not set for domain \""<< di->zone <<"\". Using DEFAULT for SOA-EDIT-DNSUPDATE"<<endl;
×
1069
        soaEdit2136 = "DEFAULT";
×
1070
      } else
×
1071
        soaEdit = soaEditSetting;
×
1072
    }
×
UNCOV
1073
  }
×
1074

1075
  DNSResourceRecord rr;
964✔
1076
  if (makeIncreasedSOARecord(sd, soaEdit2136, soaEdit, rr)) {
964!
1077
    di->backend->replaceRRSet(di->id, rr.qname, rr.qtype, vector<DNSResourceRecord>(1, rr));
964✔
1078
    g_log << Logger::Notice << msgPrefix << "Increasing SOA serial (" << oldSerial << " -> " << sd.serial << ")" << endl;
964✔
1079

1080
    //Correct ordername + auth flag
1081
    if (haveNSEC3) {
964✔
1082
      DNSName ordername;
486✔
1083
      if (!narrow)
486✔
1084
        ordername = DNSName(toBase32Hex(hashQNameWithSalt(*ns3pr, rr.qname)));
332✔
1085

1086
      di->backend->updateDNSSECOrderNameAndAuth(di->id, rr.qname, ordername, true);
486✔
1087
    } else { // NSEC
486✔
1088
      DNSName ordername = rr.qname.makeRelative(di->zone);
478✔
1089
      di->backend->updateDNSSECOrderNameAndAuth(di->id, rr.qname, ordername, true);
478✔
1090
    }
478✔
1091
  }
964✔
1092
}
964✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc