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

PowerDNS / pdns / 14531677934

18 Apr 2025 07:37AM UTC coverage: 63.505% (+0.02%) from 63.488%
14531677934

Pull #15441

github

web-flow
Merge 5969ff017 into 5ea1a1229
Pull Request #15441: [evil] ZoneName, step 2

41772 of 100536 branches covered (41.55%)

Branch coverage included in aggregate %.

361 of 414 new or added lines in 38 files covered. (87.2%)

29 existing lines in 9 files now uncovered.

129015 of 168398 relevant lines covered (76.61%)

3908639.88 hits per line

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

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

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

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

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

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

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

335

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

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

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

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

374

375
  // 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
376
  // the code that calls this performUpdate().
377
  if ((rr->d_class == QClass::ANY || rr->d_class == QClass::NONE) && rrType != QType::SOA) { // never delete a SOA.
2,624✔
378
    DLOG(g_log<<msgPrefix<<"Deleting records: "<<rr->d_name<<"; QClass:"<<rr->d_class<<"; rrType: "<<rrType.toString()<<endl);
648✔
379

380
    if (rrType == QType::NSEC3PARAM) {
648✔
381
      g_log<<Logger::Notice<<msgPrefix<<"Deleting NSEC3PARAM from zone, resetting ordernames."<<endl;
16✔
382
      if (rr->d_class == QClass::ANY)
16!
383
        d_dk.unsetNSEC3PARAM(ZoneName(rr->d_name));
16✔
384
      else if (rr->d_class == QClass::NONE) {
×
385
        NSEC3PARAMRecordContent nsec3rr(rr->getContent()->getZoneRepresentation(), di->zone);
×
386
        if (*haveNSEC3 && ns3pr->getZoneRepresentation() == nsec3rr.getZoneRepresentation())
×
NEW
387
          d_dk.unsetNSEC3PARAM(ZoneName(rr->d_name));
×
388
        else
×
389
          return 0;
×
390
      } else
×
391
        return 0;
×
392

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

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

405

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

438

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

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

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

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

479
      // Fix ENT records.
480
      // We must check if we have a record below the current level and if we removed the 'last' record
481
      // on that level. If so, we must insert an ENT record.
482
      // 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.
483
      bool foundDeeper = false, foundOtherWithSameName = false;
608✔
484
      // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
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;
×
NEW
553
  B.getDomainMetadata(ZoneName(p.qdomain), "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
  ZoneName zonename(packet.qdomain);
1,252✔
677
  string msgPrefix="UPDATE (" + std::to_string(packet.d.id) + ") from " + packet.getRemoteString() + " for " + zonename.toLogString() + ": ";
1,252✔
678
  g_log<<Logger::Info<<msgPrefix<<"Processing started."<<endl;
1,252✔
679

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

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

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

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

699

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

706
      TSIGRecordContent trc;
×
707
      DNSName inputkey;
×
708
      string message;
×
709
      if (! packet.getTSIGDetails(&trc,  &inputkey)) {
×
710
        g_log<<Logger::Error<<msgPrefix<<"TSIG key required, but packet does not contain key. Sending REFUSED"<<endl;
×
711
        return RCode::Refused;
×
712
      }
×
713
#ifdef ENABLE_GSS_TSIG
×
714
      if (g_doGssTSIG && packet.d_tsig_algo == TSIG_GSS) {
×
715
        GssName inputname(packet.d_peer_principal); // match against principal since GSS requires that
×
716
        for(const auto& key: tsigKeys) {
×
717
          if (inputname.match(key)) {
×
718
            validKey = true;
×
719
            break;
×
720
          }
×
721
        }
×
722
      }
×
723
      else
×
724
#endif
×
725
        {
×
726
        for(const auto& key: tsigKeys) {
×
727
          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.
×
728
            validKey=true;
×
729
            break;
×
730
          }
×
731
        }
×
732
      }
×
733

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

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

747
  }
1,252✔
748

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

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

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

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

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

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

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

793

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

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

814
  // 3.2.3 - Prerequisite check - this is outside of updatePrerequisitesCheck because we check an RRSet and not the RR.
815
  typedef pair<DNSName, QType> rrSetKey_t;
1,156✔
816
  typedef vector<DNSResourceRecord> rrVector_t;
1,156✔
817
  typedef std::map<rrSetKey_t, rrVector_t> RRsetMap_t;
1,156✔
818
  RRsetMap_t preReqRRsets;
1,156✔
819
  for(const auto& i: mdp.d_answers) {
2,900✔
820
    const DNSRecord* dnsRecord = &i;
2,900✔
821
    if (dnsRecord->d_place == DNSResourceRecord::ANSWER) {
2,900✔
822
      // Last line of 3.2.3
823
      if (dnsRecord->d_class != QClass::IN && dnsRecord->d_class != QClass::NONE && dnsRecord->d_class != QClass::ANY) {
168!
824
        return RCode::FormErr;
×
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!
NEW
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
          }
×
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!
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!
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
      // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
975
      di.backend->lookup(QType(QType::NS), di.zone.operator const DNSName&(), di.id);
24✔
976
      while (di.backend->get(rec)) {
72✔
977
        nsRRInZone.push_back(rec);
48✔
978
      }
48✔
979
      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✔
980
        for (auto& inZone: nsRRInZone) {
24✔
981
          for (auto& rr: nsRRtoDelete) {
24✔
982
            if (inZone.getZoneRepresentation() == (rr)->getContent()->getZoneRepresentation())
24✔
983
              changedRecords += performUpdate(msgPrefix, rr, &di, isPresigned, &narrow, &haveNSEC3, &ns3pr, &updatedSerial);
12✔
984
          }
24✔
985
        }
24✔
986
      }
12✔
987
    }
24✔
988

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

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

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

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

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

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

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

1059
  uint32_t oldSerial = sd.serial;
964✔
1060

1061
  vector<string> soaEdit2136Setting;
964✔
1062
  B.getDomainMetadata(di->zone, "SOA-EDIT-DNSUPDATE", soaEdit2136Setting);
964✔
1063
  string soaEdit2136 = "DEFAULT";
964✔
1064
  string soaEdit;
964✔
1065
  if (!soaEdit2136Setting.empty()) {
964!
1066
    soaEdit2136 = soaEdit2136Setting[0];
×
1067
    if (pdns_iequals(soaEdit2136, "SOA-EDIT") || pdns_iequals(soaEdit2136,"SOA-EDIT-INCREASE") ){
×
1068
      if (soaEditSetting.empty()) {
×
1069
        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;
×
1070
        soaEdit2136 = "DEFAULT";
×
1071
      } else
×
1072
        soaEdit = soaEditSetting;
×
1073
    }
×
1074
  }
×
1075

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

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

1087
      di->backend->updateDNSSECOrderNameAndAuth(di->id, rr.qname, ordername, true);
486✔
1088
    } else { // NSEC
486✔
1089
      DNSName ordername = rr.qname.makeRelative(di->zone);
478✔
1090
      di->backend->updateDNSSECOrderNameAndAuth(di->id, rr.qname, ordername, true);
478✔
1091
    }
478✔
1092
  }
964✔
1093
}
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