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

PowerDNS / pdns / 19151133429

06 Nov 2025 02:49PM UTC coverage: 60.704% (-12.3%) from 73.0%
19151133429

push

github

web-flow
Merge pull request #16446 from jsoref/contributing-ai-policy

docs: Mention AI Policy in contributing pull requests

68493 of 178556 branches covered (38.36%)

Branch coverage included in aggregate %.

152002 of 184673 relevant lines covered (82.31%)

7226535.04 hits per line

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

0.0
/pdns/rfc2136handler.cc
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
 */
22

23
#ifdef HAVE_CONFIG_H
24
#include "config.h"
25
#endif
26
#include "dnswriter.hh"
27
#include "packethandler.hh"
28
#include "qtype.hh"
29
#include "dnspacket.hh"
30
#include "auth-caches.hh"
×
31
#include "statbag.hh"
×
32
#include "dnsseckeeper.hh"
×
33
#include "base64.hh"
34
#include "base32.hh"
35

×
36
#include "misc.hh"
×
37
#include "arguments.hh"
38
#include "resolver.hh"
×
39
#include "dns_random.hh"
×
40
#include "backends/gsql/ssql.hh"
×
41
#include "communicator.hh"
×
42
#include "query-local-address.hh"
×
43
#include "gss_context.hh"
×
44
#include "auth-main.hh"
×
45

×
46
std::mutex PacketHandler::s_rfc2136lock;
×
47

48
// Context data for RFC2136 operation
49
struct updateContext {
×
50
  const DomainInfo *di{nullptr};
×
51
  bool isPresigned{false};
×
52

×
53
  // The following may be modified
×
54
  bool narrow{false};
×
55
  bool haveNSEC3{false};
56
  NSEC3PARAMRecordContent ns3pr{};
57
  bool updatedSerial{false};
×
58
};
×
59

×
60
static void increaseSerial(const string& msgPrefix, const string& soaEditSetting, const updateContext& ctx);
×
61

×
62
// Implement section 3.2.1 and 3.2.2 of RFC2136
×
63
// NOLINTNEXTLINE(readability-identifier-length)
64
static int checkUpdatePrerequisites(const DNSRecord* rr, DomainInfo* di)
×
65
{
×
66
  if (rr->d_ttl != 0) {
67
    return RCode::FormErr;
68
  }
69

×
70
  // 3.2.1 and 3.2.2 check content length.
71
  if ((rr->d_class == QClass::NONE || rr->d_class == QClass::ANY) && rr->d_clen != 0) {
×
72
    return RCode::FormErr;
×
73
  }
×
74

75
  bool foundRecord = false;
×
76
  DNSResourceRecord rec;
77
  di->backend->lookup(QType(QType::ANY), rr->d_name, di->id);
×
78
  while (di->backend->get(rec)) {
×
79
    if (rec.qtype.getCode() == QType::ENT) {
×
80
      continue;
81
    }
×
82
    if ((rr->d_type != QType::ANY && rec.qtype == rr->d_type) || rr->d_type == QType::ANY) {
×
83
      foundRecord = true;
×
84
      di->backend->lookupEnd();
85
      break;
×
86
    }
×
87
  }
×
88

89
  // Section 3.2.1
×
90
  if (rr->d_class == QClass::ANY && !foundRecord) {
×
91
    if (rr->d_type == QType::ANY) {
×
92
      return RCode::NXDomain;
93
    }
×
94
    if (rr->d_type != QType::ANY) {
×
95
      return RCode::NXRRSet;
×
96
    }
97
  }
×
98

×
99
  // Section 3.2.2
100
  if (rr->d_class == QClass::NONE && foundRecord) {
101
    if (rr->d_type == QType::ANY) {
102
      return RCode::YXDomain;
103
    }
104
    if (rr->d_type != QType::ANY) {
105
      return RCode::YXRRSet;
106
    }
×
107
  }
×
108

×
109
  return RCode::NoError;
×
110
}
111

×
112
// Method implements section 3.4.1 of RFC2136
×
113
// NOLINTNEXTLINE(readability-identifier-length)
×
114
static int checkUpdatePrescan(const DNSRecord* rr)
×
115
{
116
  // The RFC stats that d_class != ZCLASS, but we only support the IN class.
×
117
  if (rr->d_class != QClass::IN && rr->d_class != QClass::NONE && rr->d_class != QClass::ANY) {
×
118
    return RCode::FormErr;
×
119
  }
×
120

121
  auto qtype = QType(rr->d_type);
122

123
  if (!qtype.isSupportedType()) {
×
124
    return RCode::FormErr;
×
125
  }
×
126

127
  if ((rr->d_class == QClass::NONE || rr->d_class == QClass::ANY) && rr->d_ttl != 0) {
128
    return RCode::FormErr;
×
129
  }
×
130

131
  if (rr->d_class == QClass::ANY && rr->d_clen != 0) {
×
132
    return RCode::FormErr;
×
133
  }
134

135
  if (qtype.isMetadataType()) {
×
136
    return RCode::FormErr;
×
137
  }
×
138

139
  if (rr->d_class != QClass::ANY && qtype.getCode() == QType::ANY) {
140
    return RCode::FormErr;
×
141
  }
×
142

×
143
  return RCode::NoError;
×
144
}
×
145

×
146
// Implements section 3.4.2 of RFC2136
147
// Due to large complexity, this is stuck in multiple routines.
148

149
static bool mayPerformUpdate(const string& msgPrefix, const DNSRecord* rr, const updateContext& ctx) // NOLINT(readability-identifier-length)
150
{
×
151
  auto rrType = QType(rr->d_type);
×
152

×
153
  if (rrType == QType::NSEC || rrType == QType::NSEC3) {
×
154
    g_log << Logger::Warning << msgPrefix << "Trying to add/update/delete " << rr->d_name << "|" << rrType.toString() << ". These are generated records, ignoring!" << endl;
×
155
    return false;
156
  }
×
157

×
158
  if (!ctx.isPresigned && rrType == QType::RRSIG) {
×
159
    g_log << Logger::Warning << msgPrefix << "Trying to add/update/delete " << rr->d_name << "|" << rrType.toString() << " in non-presigned zone, ignoring!" << endl;
×
160
    return false;
×
161
  }
×
162

×
163
  if ((rrType == QType::NSEC3PARAM || rrType == QType::DNSKEY) && rr->d_name != ctx.di->zone.operator const DNSName&()) {
×
164
    g_log << Logger::Warning << msgPrefix << "Trying to add/update/delete " << rr->d_name << "|" << rrType.toString() << ", " << rrType.toString() << " must be at zone apex, ignoring!" << endl;
×
165
    return false;
×
166
  }
×
167

×
168
  return true;
×
169
}
×
170

×
171
// 3.4.2.2 QClass::IN means insert or update
×
172
// Caller has checked that we are allowed to insert the record and has handled
173
// the NSEC3PARAM case already.
174
// ctx is not const, may update updateSerial
×
175
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
×
176
static uint performInsert(const string& msgPrefix, const DNSRecord* rr, updateContext& ctx, vector<DNSResourceRecord>& rrset, set<DNSName>& insnonterm, set<DNSName>& delnonterm) // NOLINT(readability-identifier-length)
×
177
{
×
178
  uint changedRecords = 0;
×
179
  DNSResourceRecord rec;
×
180
  auto rrType = QType(rr->d_type);
×
181

×
182
  bool foundRecord = false;
×
183
  ctx.di->backend->lookup(rrType, rr->d_name, ctx.di->id);
×
184
  while (ctx.di->backend->get(rec)) {
×
185
    rrset.push_back(rec);
×
186
    foundRecord = true;
×
187
  }
×
188

×
189
  if (foundRecord) {
×
190
    switch (rrType) {
191
    case QType::SOA: {
192
      // SOA updates require the serial to be higher than the current
193
      SOAData sdOld;
×
194
      SOAData sdUpdate;
×
195
      DNSResourceRecord* oldRec = &rrset.front();
×
196
      fillSOAData(oldRec->content, sdOld);
×
197
      oldRec->setContent(rr->getContent()->getZoneRepresentation());
×
198
      fillSOAData(oldRec->content, sdUpdate);
×
199
      if (rfc1982LessThan(sdOld.serial, sdUpdate.serial)) {
×
200
        ctx.di->backend->replaceRRSet(ctx.di->id, oldRec->qname, oldRec->qtype, rrset);
×
201
        ctx.updatedSerial = true;
×
202
        changedRecords++;
×
203
        g_log << Logger::Notice << msgPrefix << "Replacing SOA record " << rr->d_name << "|" << rrType.toString() << endl;
×
204
      }
×
205
      else {
×
206
        g_log << Logger::Notice << msgPrefix << "Provided serial (" << sdUpdate.serial << ") is older than the current serial (" << sdOld.serial << "), ignoring SOA update." << endl;
×
207
      }
×
208
    } break;
×
209
    case QType::CNAME: {
×
210
      // It's not possible to have multiple CNAME's with the same NAME. So we always update.
×
211
      int changedCNames = 0;
×
212
      for (auto& i : rrset) { // NOLINT(readability-identifier-length)
×
213
        if (i.ttl != rr->d_ttl || i.content != rr->getContent()->getZoneRepresentation()) {
×
214
          i.ttl = rr->d_ttl;
×
215
          i.setContent(rr->getContent()->getZoneRepresentation());
×
216
          changedCNames++;
×
217
        }
×
218
      }
×
219
      if (changedCNames > 0) {
×
220
        ctx.di->backend->replaceRRSet(ctx.di->id, rr->d_name, rrType, rrset);
×
221
        g_log << Logger::Notice << msgPrefix << "Replacing CNAME record " << rr->d_name << "|" << rrType.toString() << endl;
×
222
        changedRecords += changedCNames;
×
223
      }
×
224
      else {
225
        g_log << Logger::Notice << msgPrefix << "Replace for CNAME record " << rr->d_name << "|" << rrType.toString() << " requested, but no changes made." << endl;
226
      }
227
    } break;
×
228
    default: {
×
229
      // In any other case, we must check if the TYPE and RDATA match to provide an update (which effectively means a update of TTL)
230
      int updateTTL = 0;
×
231
      foundRecord = false;
×
232
      bool lowerCase = false;
×
233
      switch (rrType.getCode()) {
×
234
      case QType::MX:
235
      case QType::PTR:
×
236
      case QType::SRV:
×
237
        lowerCase = true;
×
238
        break;
×
239
      }
×
240
      string content = rr->getContent()->getZoneRepresentation();
×
241
      if (lowerCase) {
×
242
        content = toLower(content);
×
243
      }
×
244
      for (auto& i : rrset) { // NOLINT(readability-identifier-length)
245
        if (rrType != i.qtype.getCode()) {
×
246
          continue;
×
247
        }
×
248
        if (!foundRecord) {
×
249
          string icontent = i.getZoneRepresentation();
×
250
          if (lowerCase) {
×
251
            icontent = toLower(icontent);
×
252
          }
×
253
          if (icontent == content) {
254
            foundRecord = true;
×
255
          }
256
        }
257
        if (i.ttl != rr->d_ttl) {
×
258
          i.ttl = rr->d_ttl;
259
          updateTTL++;
260
        }
×
261
      }
×
262
      if (updateTTL > 0) {
×
263
        ctx.di->backend->replaceRRSet(ctx.di->id, rr->d_name, rrType, rrset);
×
264
        g_log << Logger::Notice << msgPrefix << "Updating TTLs for " << rr->d_name << "|" << rrType.toString() << endl;
×
265
        changedRecords += updateTTL;
×
266
      }
267
      else if (foundRecord) {
268
        g_log << Logger::Notice << msgPrefix << "Replace for recordset " << rr->d_name << "|" << rrType.toString() << " requested, but no changes made." << endl;
269
      }
270
    } break;
271
    }
272

×
273
    // ReplaceRRSet dumps our ordername and auth flag, so we need to correct it if we have changed records.
×
274
    // We can take the auth flag from the first RR in the set, as the name is different, so should the auth be.
275
    if (changedRecords > 0) {
×
276
      bool auth = rrset.front().auth;
×
277

278
      if (ctx.haveNSEC3) {
×
279
        DNSName ordername;
×
280
        if (!ctx.narrow) {
×
281
          ordername = DNSName(toBase32Hex(hashQNameWithSalt(ctx.ns3pr, rr->d_name)));
×
282
        }
×
283

×
284
        if (ctx.narrow) {
×
285
          ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, DNSName(), auth, QType::ANY, false);
×
286
        }
×
287
        else {
×
288
          ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, ordername, auth, QType::ANY, true);
289
        }
×
290
        if (!auth || rrType == QType::DS) {
×
291
          ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, DNSName(), false, QType::NS, !ctx.narrow);
×
292
          ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, DNSName(), false, QType::A, !ctx.narrow);
×
293
          ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, DNSName(), false, QType::AAAA, !ctx.narrow);
×
294
        }
×
295
      }
296
      else { // NSEC
×
297
        ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, rr->d_name.makeRelative(ctx.di->zone), auth, QType::ANY, false);
×
298
        if (!auth || rrType == QType::DS) {
×
299
          ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, DNSName(), false, QType::A, false);
×
300
          ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, DNSName(), false, QType::AAAA, false);
×
301
        }
302
      }
×
303
    }
×
304
  } // if (foundRecord)
×
305

×
306
  // If we haven't found a record that matches, we must add it.
307
  if (!foundRecord) {
×
308
    g_log << Logger::Notice << msgPrefix << "Adding record " << rr->d_name << "|" << rrType.toString() << endl;
×
309
    delnonterm.insert(rr->d_name); // always remove any ENT's in the place where we're going to add a record.
310
    auto newRec = DNSResourceRecord::fromWire(*rr);
×
311
    newRec.domain_id = ctx.di->id;
×
312
    newRec.auth = (rr->d_name == ctx.di->zone.operator const DNSName&() || rrType.getCode() != QType::NS);
×
313
    ctx.di->backend->feedRecord(newRec, DNSName());
×
314
    changedRecords++;
315

×
316
    // because we added a record, we need to fix DNSSEC data.
×
317
    DNSName shorter(rr->d_name);
×
318
    bool auth = newRec.auth;
319
    bool fixDS = (rrType == QType::DS);
×
320

×
321
    if (ctx.di->zone.operator const DNSName&() != shorter) { // Everything at APEX is auth=1 && no ENT's
×
322
      do {
×
323
        if (ctx.di->zone.operator const DNSName&() == shorter) {
×
324
          break;
×
325
        }
×
326

×
327
        bool foundShorter = false;
×
328
        ctx.di->backend->lookup(QType(QType::ANY), shorter, ctx.di->id);
×
329
        while (ctx.di->backend->get(rec)) {
×
330
          if (rec.qname == rr->d_name && rec.qtype == QType::DS) {
×
331
            fixDS = true;
332
          }
333
          if (shorter != rr->d_name) {
×
334
            foundShorter = true;
×
335
          }
×
336
          if (rec.qtype == QType::NS) { // are we inserting below a delegate?
×
337
            auth = false;
×
338
          }
339
        }
×
340

×
341
        if (!foundShorter && auth && shorter != rr->d_name) { // haven't found any record at current level, insert ENT.
342
          insnonterm.insert(shorter);
×
343
        }
×
344
        if (foundShorter) {
×
345
          break; // if we find a shorter record, we can stop searching
×
346
        }
×
347
      } while (shorter.chopOff());
×
348
    }
×
349

×
350
    if (ctx.haveNSEC3) {
×
351
      DNSName ordername;
×
352
      if (!ctx.narrow) {
×
353
        ordername = DNSName(toBase32Hex(hashQNameWithSalt(ctx.ns3pr, rr->d_name)));
354
      }
×
355

×
356
      if (ctx.narrow) {
357
        ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, DNSName(), auth, QType::ANY, false);
×
358
      }
×
359
      else {
×
360
        ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, ordername, auth, QType::ANY, true);
×
361
      }
362

×
363
      if (fixDS) {
364
        ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, ordername, true, QType::DS, !ctx.narrow);
×
365
      }
×
366

×
367
      if (!auth) {
×
368
        if (ctx.ns3pr.d_flags != 0) {
369
          ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, DNSName(), false, QType::NS, !ctx.narrow);
370
        }
371
        ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, DNSName(), false, QType::A, !ctx.narrow);
372
        ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, DNSName(), false, QType::AAAA, !ctx.narrow);
×
373
      }
×
374
    }
×
375
    else { // NSEC
×
376
      DNSName ordername = rr->d_name.makeRelative(ctx.di->zone);
×
377
      ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, ordername, auth, QType::ANY, false);
×
378
      if (fixDS) {
×
379
        ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, ordername, true, QType::DS, false);
×
380
      }
381
      if (!auth) {
×
382
        ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, DNSName(), false, QType::A, false);
383
        ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr->d_name, DNSName(), false, QType::AAAA, false);
×
384
      }
×
385
    }
386

×
387
    // If we insert an NS, all the records below it become non auth - so, we're inserting a delegate.
388
    // Auth can only be false when the rr->d_name is not the zone
389
    if (!auth && rrType == QType::NS) {
390
      DLOG(g_log << msgPrefix << "Going to fix auth flags below " << rr->d_name << endl);
×
391
      insnonterm.clear(); // No ENT's are needed below delegates (auth=0)
×
392
      vector<DNSName> qnames;
×
393
      ctx.di->backend->listSubZone(ZoneName(rr->d_name), ctx.di->id);
×
394
      while (ctx.di->backend->get(rec)) {
×
395
        if (rec.qtype.getCode() != QType::ENT && rec.qtype.getCode() != QType::DS && rr->d_name != rec.qname) { // Skip ENT, DS and our already corrected record.
×
396
          qnames.push_back(rec.qname);
×
397
        }
×
398
      }
×
399
      for (const auto& qname : qnames) {
400
        if (ctx.haveNSEC3) {
401
          DNSName ordername;
×
402
          if (!ctx.narrow) {
×
403
            ordername = DNSName(toBase32Hex(hashQNameWithSalt(ctx.ns3pr, qname)));
×
404
          }
×
405

×
406
          if (ctx.narrow) {
407
            ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, qname, DNSName(), auth, QType::ANY, false);
×
408
          }
×
409
          else {
×
410
            ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, qname, ordername, auth, QType::ANY, true);
×
411
          }
×
412

×
413
          if (ctx.ns3pr.d_flags != 0) {
×
414
            ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, qname, DNSName(), false, QType::NS, !ctx.narrow);
415
          }
416
        }
×
417
        else { // NSEC
×
418
          DNSName ordername = DNSName(qname).makeRelative(ctx.di->zone);
×
419
          ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, qname, ordername, false, QType::NS, false);
×
420
        }
×
421

×
422
        ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, qname, DNSName(), false, QType::A, ctx.haveNSEC3 && !ctx.narrow);
×
423
        ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, qname, DNSName(), false, QType::AAAA, ctx.haveNSEC3 && !ctx.narrow);
×
424
      }
×
425
    }
×
426
  }
427

×
428
  return changedRecords;
×
429
}
×
430

×
431
// 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
432
// the code that calls this performUpdate().
433
// Caller has checked that we are allowed to delete the record and has handled
434
// the NSEC3PARAM case already.
×
435
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
×
436
static uint performDelete(const string& msgPrefix, const DNSRecord* rr, const updateContext& ctx, vector<DNSResourceRecord>& rrset, set<DNSName>& insnonterm, set<DNSName>& delnonterm) // NOLINT(readability-identifier-length)
×
437
{
×
438
  vector<DNSResourceRecord> recordsToDelete;
×
439
  DNSResourceRecord rec;
×
440
  auto rrType = QType(rr->d_type);
×
441

×
442
  ctx.di->backend->lookup(rrType, rr->d_name, ctx.di->id);
443
  while (ctx.di->backend->get(rec)) {
×
444
    if (rr->d_class == QClass::ANY) { // 3.4.2.3
×
445
      if (rec.qname == ctx.di->zone.operator const DNSName&() && (rec.qtype == QType::NS || rec.qtype == QType::SOA)) { // Never delete all SOA and NS's
×
446
        rrset.push_back(rec);
×
447
      }
×
448
      else {
×
449
        recordsToDelete.push_back(rec);
450
      }
×
451
    }
×
452
    if (rr->d_class == QClass::NONE) { // 3.4.2.4
×
453
      auto repr = rec.getZoneRepresentation();
×
454
      if (rec.qtype == QType::TXT) {
×
455
        DLOG(g_log << msgPrefix << "Adjusting TXT content from [" << repr << "]" << endl);
×
456
        auto drc = DNSRecordContent::make(rec.qtype.getCode(), QClass::IN, repr);
×
457
        auto ser = drc->serialize(rec.qname, true, true);
×
458
        auto rc = DNSRecordContent::deserialize(rec.qname, rec.qtype.getCode(), ser); // NOLINT(readability-identifier-length)
×
459
        repr = rc->getZoneRepresentation(true);
460
        DLOG(g_log << msgPrefix << "Adjusted TXT content to [" << repr << "]" << endl);
×
461
      }
×
462
      DLOG(g_log << msgPrefix << "Matching RR in RRset - (adjusted) representation from request=[" << repr << "], rr->getContent()->getZoneRepresentation()=[" << rr->getContent()->getZoneRepresentation() << "]" << endl);
463
      if (rrType == rec.qtype && repr == rr->getContent()->getZoneRepresentation()) {
×
464
        recordsToDelete.push_back(rec);
×
465
      }
×
466
      else {
×
467
        rrset.push_back(rec);
×
468
      }
×
469
    }
×
470
  }
×
471

472
  if (recordsToDelete.empty()) {
473
    g_log << Logger::Notice << msgPrefix << "Deletion for record " << rr->d_name << "|" << rrType.toString() << " requested, but not found." << endl;
×
474
    return 0;
475
  }
×
476

×
477
  ctx.di->backend->replaceRRSet(ctx.di->id, rr->d_name, rrType, rrset);
×
478
  g_log << Logger::Notice << msgPrefix << "Deleting record " << rr->d_name << "|" << rrType.toString() << endl;
×
479

×
480
  // If we've removed a delegate, we need to reset ordername/auth for some records.
×
481
  if (rrType == QType::NS && rr->d_name != ctx.di->zone.operator const DNSName&()) {
×
482
    vector<DNSName> belowOldDelegate;
×
483
    vector<DNSName> nsRecs;
×
484
    vector<DNSName> updateAuthFlag;
485
    ctx.di->backend->listSubZone(ZoneName(rr->d_name), ctx.di->id);
×
486
    while (ctx.di->backend->get(rec)) {
487
      if (rec.qtype.getCode() != QType::ENT) { // skip ENT records, they are always auth=false
×
488
        belowOldDelegate.push_back(rec.qname);
489
      }
490
      if (rec.qtype.getCode() == QType::NS && rec.qname != rr->d_name) {
491
        nsRecs.push_back(rec.qname);
×
492
      }
×
493
    }
×
494

×
495
    for (auto& belowOldDel : belowOldDelegate) {
496
      bool isBelowDelegate = false;
×
497
      for (const auto& ns : nsRecs) { // NOLINT(readability-identifier-length)
498
        if (ns.isPartOf(belowOldDel)) {
499
          isBelowDelegate = true;
500
          break;
×
501
        }
502
      }
×
503
      if (!isBelowDelegate) {
504
        updateAuthFlag.push_back(belowOldDel);
×
505
      }
×
506
    }
×
507

×
508
    for (const auto& changeRec : updateAuthFlag) {
×
509
      DNSName ordername;
×
510
      if (ctx.haveNSEC3) {
×
511
        if (!ctx.narrow) {
×
512
          ordername = DNSName(toBase32Hex(hashQNameWithSalt(ctx.ns3pr, changeRec)));
513
        }
514
      }
515
      else { // NSEC
516
        ordername = changeRec.makeRelative(ctx.di->zone);
517
      }
×
518
      ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, changeRec, ordername, true, QType::ANY, ctx.haveNSEC3 && !ctx.narrow);
519
    }
×
520
  }
×
521

522
  // Fix ENT records.
523
  // We must check if we have a record below the current level and if we removed the 'last' record
524
  // on that level. If so, we must insert an ENT record.
525
  // 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.
×
526
  bool foundDeeper = false;
×
527
  bool foundOtherWithSameName = false;
×
528
  ctx.di->backend->listSubZone(ZoneName(rr->d_name), ctx.di->id);
×
529
  while (ctx.di->backend->get(rec)) {
×
530
    if (rec.qname == rr->d_name && count(recordsToDelete.begin(), recordsToDelete.end(), rec) == 0) {
×
531
      foundOtherWithSameName = true;
×
532
    }
×
533
    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
×
534
      foundDeeper = true;
×
535
    }
×
536
  }
537

538
  if (foundDeeper && !foundOtherWithSameName) {
539
    insnonterm.insert(rr->d_name);
540
  }
×
541
  else if (!foundOtherWithSameName) {
×
542
    // If we didn't have to insert an ENT, we might have deleted a record at very deep level
543
    // and we must then clean up the ENT's above the deleted record.
×
544
    DNSName shorter(rr->d_name);
×
545
    while (shorter != ctx.di->zone.operator const DNSName&()) {
×
546
      shorter.chopOff();
547
      bool foundRealRR = false;
×
548
      bool foundEnt = false;
×
549

×
550
      // The reason for a listSubZone here is because might go up the tree and find the ENT of another branch
×
551
      // consider these non ENT-records:
552
      // b.c.d.e.test.com
×
553
      // b.d.e.test.com
×
554
      // 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.
555
      // At that point we can stop deleting ENT's because the tree is in tact again.
×
556
      ctx.di->backend->listSubZone(ZoneName(shorter), ctx.di->id);
×
557

×
558
      while (ctx.di->backend->get(rec)) {
559
        if (rec.qtype.getCode() != QType::ENT) {
×
560
          foundRealRR = true;
×
561
        }
×
562
        else {
563
          foundEnt = true;
×
564
        }
565
      }
×
566
      if (!foundRealRR) {
×
567
        if (foundEnt) { // only delete the ENT if we actually found one.
568
          delnonterm.insert(shorter);
×
569
        }
×
570
      }
571
      else {
×
572
        break;
×
573
      }
×
574
    }
×
575
  }
576

×
577
  return recordsToDelete.size();
×
578
}
×
579

×
580
static void updateENT([[maybe_unused]] const string& msgPrefix, const updateContext& ctx, set<DNSName>& insnonterm, set<DNSName>& delnonterm)
581
{
×
582
  if (insnonterm.empty() && delnonterm.empty()) {
×
583
    return;
×
584
  }
×
585

×
586
  DLOG(g_log << msgPrefix << "Updating ENT records - " << insnonterm.size() << "|" << delnonterm.size() << endl);
×
587
  ctx.di->backend->updateEmptyNonTerminals(ctx.di->id, insnonterm, delnonterm, false);
×
588
  for (const auto& insert : insnonterm) {
×
589
    string hashed;
×
590
    if (ctx.haveNSEC3) {
×
591
      DNSName ordername;
592
      if (!ctx.narrow) {
593
        ordername = DNSName(toBase32Hex(hashQNameWithSalt(ctx.ns3pr, insert)));
×
594
      }
×
595
      ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, insert, ordername, true, QType::ANY, !ctx.narrow);
×
596
    }
×
597
  }
×
598
}
×
599

×
600
static uint performUpdate(DNSSECKeeper& dsk, const string& msgPrefix, const DNSRecord* rr, updateContext& ctx) // NOLINT(readability-identifier-length)
×
601
{
×
602
  if (!mayPerformUpdate(msgPrefix, rr, ctx)) {
×
603
    return 0;
×
604
  }
×
605

×
606
  auto rrType = QType(rr->d_type);
×
607

×
608
  // Decide which action to take.
609
  // 3.4.2.2 QClass::IN means insert or update
×
610
  bool insertAction = rr->d_class == QClass::IN;
×
611
  bool deleteAction = (rr->d_class == QClass::ANY || rr->d_class == QClass::NONE) && rrType != QType::SOA; // never delete a SOA.
×
612

×
613
  if (!insertAction && !deleteAction) {
×
614
    return 0; // nothing to do!
615
  }
×
616

×
617
  // Special processing for NSEC3PARAM
×
618
  if (rrType == QType::NSEC3PARAM) {
×
619
    if (insertAction) {
×
620
      g_log << Logger::Notice << msgPrefix << "Adding/updating NSEC3PARAM for zone, resetting ordernames." << endl;
×
621

×
622
      ctx.ns3pr = NSEC3PARAMRecordContent(rr->getContent()->getZoneRepresentation(), ctx.di->zone);
×
623
      // adding a NSEC3 will cause narrow mode to be dropped, as you cannot specify that in a NSEC3PARAM record
×
624
      ctx.narrow = false;
×
625
      dsk.setNSEC3PARAM(ctx.di->zone, ctx.ns3pr, ctx.narrow);
×
626
      ctx.haveNSEC3 = true;
×
627
    }
×
628
    else {
×
629
      g_log << Logger::Notice << msgPrefix << "Deleting NSEC3PARAM from zone, resetting ordernames." << endl;
630
      // Be sure to use a ZoneName with a variant matching the domain we are
631
      // working on, for the sake of unsetNSEC3PARAM.
×
632
      ZoneName zonename(rr->d_name, ctx.di->zone.getVariant());
×
633
      if (rr->d_class == QClass::ANY) {
×
634
        dsk.unsetNSEC3PARAM(zonename);
×
635
      }
×
636
      else { // rr->d_class == QClass::NONE then
×
637
        NSEC3PARAMRecordContent nsec3rr(rr->getContent()->getZoneRepresentation(), ctx.di->zone);
×
638
        if (ctx.haveNSEC3 && ctx.ns3pr.getZoneRepresentation() == nsec3rr.getZoneRepresentation()) {
×
639
          dsk.unsetNSEC3PARAM(zonename);
×
640
        }
×
641
        else {
×
642
          return 0;
×
643
        }
×
644
      }
×
645

646
      // Update NSEC3 variables, other RR's in this update package might need them as well.
×
647
      ctx.narrow = false;
×
648
      ctx.haveNSEC3 = false;
×
649
    }
×
650

×
651
    string error;
×
652
    string info;
×
653
    if (!dsk.rectifyZone(ctx.di->zone, error, info, false)) {
×
654
      throw PDNSException("Failed to rectify '" + ctx.di->zone.toLogString() + "': " + error);
×
655
    }
×
656
    return 1;
×
657
  }
×
658

×
659
  uint changedRecords = 0;
×
660
  vector<DNSResourceRecord> rrset;
×
661
  // used to (at the end) fix ENT records.
662
  set<DNSName> delnonterm;
×
663
  set<DNSName> insnonterm;
664

665
  if (insertAction) {
×
666
    DLOG(g_log << msgPrefix << "Add/Update record (QClass == IN) " << rr->d_name << "|" << rrType.toString() << endl);
×
667
    changedRecords = performInsert(msgPrefix, rr, ctx, rrset, insnonterm, delnonterm);
668
  }
×
669
  else {
×
670
    DLOG(g_log << msgPrefix << "Deleting records: " << rr->d_name << "; QClass:" << rr->d_class << "; rrType: " << rrType.toString() << endl);
671
    changedRecords = performDelete(msgPrefix, rr, ctx, rrset, insnonterm, delnonterm);
672
  }
×
673

674
  //Insert and delete ENT's
675
  updateENT(msgPrefix, ctx, insnonterm, delnonterm);
×
676

×
677
  return changedRecords;
×
678
}
×
679

680
int PacketHandler::forwardPacket(const string& msgPrefix, const DNSPacket& p, const DomainInfo& di) // NOLINT(readability-identifier-length)
×
681
{
×
682
  vector<string> forward;
×
683
  B.getDomainMetadata(p.qdomainzone, "FORWARD-DNSUPDATE", forward);
684

685
  if (forward.empty() && !::arg().mustDo("forward-dnsupdate")) {
×
686
    g_log << Logger::Notice << msgPrefix << "Not configured to forward to primary, returning Refused." << endl;
687
    return RCode::Refused;
688
  }
×
689

690
  for (const auto& remote : di.primaries) {
691
    g_log << Logger::Notice << msgPrefix << "Forwarding packet to primary " << remote << endl;
692

×
693
    if (!pdns::isQueryLocalAddressFamilyEnabled(remote.sin4.sin_family)) {
×
694
      continue;
×
695
    }
696
    auto local = pdns::getQueryLocalAddress(remote.sin4.sin_family, 0);
697
    int sock = makeQuerySocket(local, false); // create TCP socket. RFC2136 section 6.2 seems to be ok with this.
×
698
    if (sock < 0) {
×
699
      g_log << Logger::Error << msgPrefix << "Error creating socket: " << stringerror() << endl;
×
700
      continue;
×
701
    }
×
702

×
703
    if (connect(sock, (const struct sockaddr*)&remote, remote.getSocklen()) < 0) { // NOLINT(cppcoreguidelines-pro-type-cstyle-cast): less ugly than a reinterpret_cast + const_cast combination which would require a linter annotation anyway
×
704
      g_log << Logger::Error << msgPrefix << "Failed to connect to " << remote.toStringWithPort() << ": " << stringerror() << endl;
×
705
      try {
×
706
        closesocket(sock);
707
      }
×
708
      catch (const PDNSException& e) {
×
709
        g_log << Logger::Error << "Error closing primary forwarding socket after connect() failed: " << e.reason << endl;
×
710
      }
×
711
      continue;
×
712
    }
713

×
714
    DNSPacket l_forwardPacket(p);
×
715
    l_forwardPacket.setID(dns_random_uint16());
×
716
    l_forwardPacket.setRemote(&remote);
×
717
    uint16_t len = htons(l_forwardPacket.getString().length());
×
718
    string buffer((const char*)&len, 2); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast): less ugly than a reinterpret_cast which would require a linter annotation anyway
×
719
    buffer.append(l_forwardPacket.getString());
×
720
    if (write(sock, buffer.c_str(), buffer.length()) < 0) {
×
721
      g_log << Logger::Error << msgPrefix << "Unable to forward update message to " << remote.toStringWithPort() << ", error:" << stringerror() << endl;
×
722
      try {
×
723
        closesocket(sock);
×
724
      }
×
725
      catch (const PDNSException& e) {
×
726
        g_log << Logger::Error << "Error closing primary forwarding socket after write() failed: " << e.reason << endl;
×
727
      }
×
728
      continue;
×
729
    }
×
730

731
    int res = waitForData(sock, 10, 0);
×
732
    if (res == 0) {
×
733
      g_log << Logger::Error << msgPrefix << "Timeout waiting for reply from primary at " << remote.toStringWithPort() << endl;
×
734
      try {
×
735
        closesocket(sock);
736
      }
737
      catch (const PDNSException& e) {
738
        g_log << Logger::Error << "Error closing primary forwarding socket after a timeout occurred: " << e.reason << endl;
739
      }
×
740
      continue;
×
741
    }
×
742
    if (res < 0) {
×
743
      g_log << Logger::Error << msgPrefix << "Error waiting for answer from primary at " << remote.toStringWithPort() << ", error:" << stringerror() << endl;
×
744
      try {
745
        closesocket(sock);
×
746
      }
×
747
      catch (const PDNSException& e) {
×
748
        g_log << Logger::Error << "Error closing primary forwarding socket after an error occurred: " << e.reason << endl;
×
749
      }
750
      continue;
×
751
    }
×
752

×
753
    std::array<unsigned char, 2> lenBuf{};
754
    ssize_t recvRes = recv(sock, lenBuf.data(), lenBuf.size(), 0);
755
    if (recvRes < 0 || static_cast<size_t>(recvRes) < lenBuf.size()) {
756
      g_log << Logger::Error << msgPrefix << "Could not receive data (length) from primary at " << remote.toStringWithPort() << ", error:" << stringerror() << endl;
757
      try {
×
758
        closesocket(sock);
759
      }
×
760
      catch (const PDNSException& e) {
×
761
        g_log << Logger::Error << "Error closing primary forwarding socket after recv() failed: " << e.reason << endl;
762
      }
×
763
      continue;
×
764
    }
765
    size_t packetLen = lenBuf[0] * 256 + lenBuf[1];
×
766

×
767
    buffer.resize(packetLen);
×
768
    recvRes = recv(sock, &buffer.at(0), packetLen, 0);
769
    if (recvRes < 0) {
770
      g_log << Logger::Error << msgPrefix << "Could not receive data (dnspacket) from primary at " << remote.toStringWithPort() << ", error:" << stringerror() << endl;
×
771
      try {
×
772
        closesocket(sock);
773
      }
×
774
      catch (const PDNSException& e) {
775
        g_log << Logger::Error << "Error closing primary forwarding socket after recv() failed: " << e.reason << endl;
×
776
      }
×
777
      continue;
×
778
    }
779
    try {
780
      closesocket(sock);
×
781
    }
782
    catch (const PDNSException& e) {
×
783
      g_log << Logger::Error << "Error closing primary forwarding socket: " << e.reason << endl;
×
784
    }
785

786
    try {
×
787
      MOADNSParser mdp(false, buffer.data(), static_cast<unsigned int>(recvRes));
788
      g_log << Logger::Info << msgPrefix << "Forward update message to " << remote.toStringWithPort() << ", result was RCode " << mdp.d_header.rcode << endl;
×
789
      return mdp.d_header.rcode;
790
    }
×
791
    catch (...) {
×
792
      g_log << Logger::Error << msgPrefix << "Failed to parse response packet from primary at " << remote.toStringWithPort() << endl;
×
793
      continue;
794
    }
×
795
  }
×
796
  g_log << Logger::Error << msgPrefix << "Failed to forward packet to primary(s). Returning ServFail." << endl;
×
797
  return RCode::ServFail;
×
798
}
×
799

800
static bool isUpdateAllowed(UeberBackend& UBackend, const std::string& msgPrefix, DNSPacket& packet)
801
{
×
802
  // Check permissions - IP based
×
803
  vector<string> allowedRanges;
×
804

×
805
  UBackend.getDomainMetadata(packet.qdomainzone, "ALLOW-DNSUPDATE-FROM", allowedRanges);
×
806
  if (!::arg()["allow-dnsupdate-from"].empty()) {
×
807
    stringtok(allowedRanges, ::arg()["allow-dnsupdate-from"], ", \t");
×
808
  }
809

×
810
  NetmaskGroup nmg;
×
811
  for (const auto& range : allowedRanges) {
×
812
    nmg.addMask(range);
×
813
  }
×
814

×
815
  if (!nmg.match(packet.getInnerRemote())) {
×
816
    g_log << Logger::Error << msgPrefix << "Remote not listed in allow-dnsupdate-from or domainmetadata. Sending REFUSED" << endl;
×
817
    return false;
×
818
  }
×
819

820
  // Check permissions - TSIG based.
×
821
  vector<string> tsigKeys;
822
  UBackend.getDomainMetadata(packet.qdomainzone, "TSIG-ALLOW-DNSUPDATE", tsigKeys);
×
823
  if (!tsigKeys.empty()) {
×
824
    bool validKey = false;
×
825

826
    TSIGRecordContent trc;
×
827
    DNSName inputkey;
×
828
    string message;
×
829
    if (!packet.getTSIGDetails(&trc, &inputkey)) {
×
830
      g_log << Logger::Error << msgPrefix << "TSIG key required, but packet does not contain key. Sending REFUSED" << endl;
×
831
      return false;
×
832
    }
×
833
#ifdef ENABLE_GSS_TSIG
×
834
    if (g_doGssTSIG && packet.d_tsig_algo == TSIG_GSS) {
×
835
      GssName inputname(packet.d_peer_principal); // match against principal since GSS requires that
×
836
      for (const auto& key : tsigKeys) {
×
837
        if (inputname.match(key)) {
×
838
          validKey = true;
×
839
          break;
×
840
        }
×
841
      }
×
842
    }
843
    else
×
844
#endif
×
845
    {
×
846
      for (const auto& key : tsigKeys) {
847
        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.
848
          validKey = true;
849
          break;
850
        }
×
851
      }
×
852
    }
×
853

×
854
    if (!validKey) {
×
855
      g_log << Logger::Error << msgPrefix << "TSIG key (" << inputkey << ") required, but no matching key found in domainmetadata, tried " << tsigKeys.size() << ". Sending REFUSED" << endl;
×
856
      return false;
×
857
    }
×
858
  }
×
859
  else if (::arg().mustDo("dnsupdate-require-tsig")) {
×
860
    g_log << Logger::Error << msgPrefix << "TSIG key required, but domain is not secured with TSIG. Sending REFUSED" << endl;
×
861
    return false;
×
862
  }
×
863

×
864
  if (tsigKeys.empty() && packet.d_havetsig) {
865
    g_log << Logger::Warning << msgPrefix << "TSIG is provided, but domain is not secured with TSIG. Processing continues" << endl;
×
866
  }
×
867

×
868
  return true;
×
869
}
870

871
static uint8_t updatePrereqCheck323(MOADNSParser& mdp, DomainInfo& info, const std::string& msgPrefix)
872
{
873
  using rrSetKey_t = pair<DNSName, QType>;
874
  using rrVector_t = vector<DNSResourceRecord>;
875
  using RRsetMap_t = std::map<rrSetKey_t, rrVector_t>;
876
  RRsetMap_t preReqRRsets;
×
877

878
  for (const auto& rec : mdp.d_answers) {
×
879
    const DNSRecord* dnsRecord = &rec;
×
880
    if (dnsRecord->d_place == DNSResourceRecord::ANSWER) {
×
881
      // Last line of 3.2.3
×
882
      if (dnsRecord->d_class != QClass::IN && dnsRecord->d_class != QClass::NONE && dnsRecord->d_class != QClass::ANY) {
883
        return RCode::FormErr;
×
884
      }
×
885

×
886
      if (dnsRecord->d_class == QClass::IN) {
×
887
        rrSetKey_t key = {dnsRecord->d_name, QType(dnsRecord->d_type)};
888
        rrVector_t* vec = &preReqRRsets[key];
×
889
        vec->push_back(DNSResourceRecord::fromWire(*dnsRecord));
×
890
      }
×
891
    }
×
892
  }
×
893

×
894
  if (!preReqRRsets.empty()) {
×
895
    RRsetMap_t zoneRRsets;
896
    for (auto& preReqRRset : preReqRRsets) {
897
      rrSetKey_t rrSet = preReqRRset.first;
898
      rrVector_t* vec = &preReqRRset.second;
899

×
900
      DNSResourceRecord rec;
901
      info.backend->lookup(QType(QType::ANY), rrSet.first, info.id);
×
902
      size_t foundRR{0};
903
      size_t matchRR{0};
×
904
      while (info.backend->get(rec)) {
×
905
        if (rec.qtype == rrSet.second) {
906
          foundRR++;
×
907
          for (auto& rrItem : *vec) {
×
908
            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.
×
909
            if (rrItem == rec) {
×
910
              matchRR++;
×
911
            }
912
          }
×
913
        }
×
914
      }
×
915
      if (matchRR != foundRR || foundRR != vec->size()) {
×
916
        g_log << Logger::Error << msgPrefix << "Failed PreRequisites check (RRs differ), returning NXRRSet" << endl;
×
917
        return RCode::NXRRSet;
×
918
      }
×
919
    }
×
920
  }
921
  return RCode::NoError;
×
922
}
×
923

×
924
static uint8_t updateRecords(MOADNSParser& mdp, DNSSECKeeper& dsk, uint& changedRecords, const std::unique_ptr<AuthLua4>& update_policy_lua, DNSPacket& packet, updateContext& ctx, const std::string& msgPrefix)
×
925
{
×
926
  vector<const DNSRecord*> cnamesToAdd;
×
927
  vector<const DNSRecord*> nonCnamesToAdd;
×
928
  vector<const DNSRecord*> nsRRtoDelete;
×
929

×
930
  bool anyRecordProcessed{false};
931
  bool anyRecordAcceptedByLua{false};
×
932
  for (const auto& answer : mdp.d_answers) {
×
933
    const DNSRecord* dnsRecord = &answer;
×
934
    if (dnsRecord->d_place == DNSResourceRecord::AUTHORITY) {
935
      anyRecordProcessed = true;
×
936
      /* see if it's permitted by policy */
×
937
      if (update_policy_lua != nullptr) {
×
938
        if (!update_policy_lua->updatePolicy(dnsRecord->d_name, QType(dnsRecord->d_type), ctx.di->zone.operator const DNSName&(), packet)) {
×
939
          g_log << Logger::Warning << msgPrefix << "Refusing update for " << dnsRecord->d_name << "/" << QType(dnsRecord->d_type).toString() << ": Not permitted by policy" << endl;
×
940
          continue;
×
941
        }
×
942
        g_log << Logger::Debug << msgPrefix << "Accepting update for " << dnsRecord->d_name << "/" << QType(dnsRecord->d_type).toString() << ": Permitted by policy" << endl;
×
943
        anyRecordAcceptedByLua = true;
×
944
      }
×
945

946
      if (dnsRecord->d_class == QClass::NONE && dnsRecord->d_type == QType::NS && dnsRecord->d_name == ctx.di->zone.operator const DNSName&()) {
×
947
        nsRRtoDelete.push_back(dnsRecord);
×
948
      }
×
949
      else if (dnsRecord->d_class == QClass::IN && dnsRecord->d_ttl > 0) {
×
950
        if (dnsRecord->d_type == QType::CNAME) {
×
951
          cnamesToAdd.push_back(dnsRecord);
×
952
        }
×
953
        else {
×
954
          nonCnamesToAdd.push_back(dnsRecord);
955
        }
×
956
      }
×
957
      else {
×
958
        changedRecords += performUpdate(dsk, msgPrefix, dnsRecord, ctx);
×
959
      }
×
960
    }
×
961
  }
×
962

×
963
  if (update_policy_lua != nullptr) {
×
964
    // If the Lua update policy script has been invoked, and has rejected
×
965
    // everything, better return Refused.
×
966
    if (anyRecordProcessed && !anyRecordAcceptedByLua) {
×
967
      return RCode::Refused;
×
968
    }
×
969
  }
970

×
971
  for (const auto& resrec : cnamesToAdd) {
972
    DNSResourceRecord rec;
973
    ctx.di->backend->lookup(QType(QType::ANY), resrec->d_name, ctx.di->id);
×
974
    while (ctx.di->backend->get(rec)) {
×
975
      if (rec.qtype != QType::CNAME && rec.qtype != QType::ENT && rec.qtype != QType::RRSIG) {
×
976
        // leave database handle in a consistent state
×
977
        ctx.di->backend->lookupEnd();
978
        g_log << Logger::Warning << msgPrefix << "Refusing update for " << resrec->d_name << "/" << QType(resrec->d_type).toString() << ": Data other than CNAME exists for the same name" << endl;
×
979
        return RCode::Refused;
×
980
      }
×
981
    }
×
982
    changedRecords += performUpdate(dsk, msgPrefix, resrec, ctx);
×
983
  }
×
984
  for (const auto& resrec : nonCnamesToAdd) {
×
985
    DNSResourceRecord rec;
986
    ctx.di->backend->lookup(QType(QType::CNAME), resrec->d_name, ctx.di->id);
×
987
    while (ctx.di->backend->get(rec)) {
×
988
      if (rec.qtype == QType::CNAME && resrec->d_type != QType::RRSIG) {
×
989
        // leave database handle in a consistent state
×
990
        ctx.di->backend->lookupEnd();
×
991
        g_log << Logger::Warning << msgPrefix << "Refusing update for " << resrec->d_name << "/" << QType(resrec->d_type).toString() << ": CNAME exists for the same name" << endl;
992
        return RCode::Refused;
993
      }
×
994
    }
×
995
    changedRecords += performUpdate(dsk, msgPrefix, resrec, ctx);
996
  }
×
997

×
998
  if (!nsRRtoDelete.empty()) {
×
999
    vector<DNSResourceRecord> nsRRInZone;
×
1000
    DNSResourceRecord rec;
1001
    ctx.di->backend->lookup(QType(QType::NS), ctx.di->zone.operator const DNSName&(), ctx.di->id);
1002
    while (ctx.di->backend->get(rec)) {
×
1003
      nsRRInZone.push_back(rec);
×
1004
    }
×
1005
    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)
×
1006
      for (auto& inZone : nsRRInZone) {
×
1007
        for (auto& resrec : nsRRtoDelete) {
1008
          if (inZone.getZoneRepresentation() == resrec->getContent()->getZoneRepresentation()) {
×
1009
            changedRecords += performUpdate(dsk, msgPrefix, resrec, ctx);
1010
          }
×
1011
        }
1012
      }
×
1013
    }
1014
  }
1015

×
1016
  return RCode::NoError;
×
1017
}
×
1018

×
1019
int PacketHandler::processUpdate(DNSPacket& packet)
×
1020
{
×
1021
  if (!::arg().mustDo("dnsupdate")) {
×
1022
    return RCode::Refused;
1023
  }
×
1024

×
1025
  string msgPrefix = "UPDATE (" + std::to_string(packet.d.id) + ") from " + packet.getRemoteString() + " for " + packet.qdomainzone.toLogString() + ": ";
1026
  g_log << Logger::Info << msgPrefix << "Processing started." << endl;
×
1027

×
1028
  // if there is policy, we delegate all checks to it
×
1029
  if (this->d_update_policy_lua == nullptr) {
×
1030
    if (!isUpdateAllowed(B, msgPrefix, packet)) {
×
1031
      return RCode::Refused;
×
1032
    }
×
1033
  }
×
1034

×
1035
  // RFC2136 uses the same DNS Header and Message as defined in RFC1035.
1036
  // This means we can use the MOADNSParser to parse the incoming packet. The result is that we have some different
×
1037
  // variable names during the use of our MOADNSParser.
×
1038
  MOADNSParser mdp(false, packet.getString());
×
1039
  if (mdp.d_header.qdcount != 1) {
×
1040
    g_log << Logger::Warning << msgPrefix << "Zone Count is not 1, sending FormErr" << endl;
×
1041
    return RCode::FormErr;
1042
  }
×
1043

1044
  if (packet.qtype.getCode() != QType::SOA) { // RFC2136 2.3 - ZTYPE must be SOA
×
1045
    g_log << Logger::Warning << msgPrefix << "Query ZTYPE is not SOA, sending FormErr" << endl;
×
1046
    return RCode::FormErr;
×
1047
  }
×
1048

×
1049
  if (packet.qclass != QClass::IN) {
×
1050
    g_log << Logger::Warning << msgPrefix << "Class is not IN, sending NotAuth" << endl;
×
1051
    return RCode::NotAuth;
×
1052
  }
×
1053

×
1054
  DomainInfo di; // NOLINT(readability-identifier-length)
×
1055
  di.backend = nullptr;
×
1056
  if (!B.getDomainInfo(packet.qdomainzone, di) || (di.backend == nullptr)) {
×
1057
    g_log << Logger::Error << msgPrefix << "Can't determine backend for domain '" << packet.qdomainzone << "' (or backend does not support DNS update operation)" << endl;
1058
    return RCode::NotAuth;
1059
  }
×
1060

×
1061
  if (di.kind == DomainInfo::Secondary) {
×
1062
    return forwardPacket(msgPrefix, packet, di);
×
1063
  }
1064

1065
  // Check if all the records provided are within the zone
×
1066
  for (const auto& answer : mdp.d_answers) {
×
1067
    const DNSRecord* dnsRecord = &answer;
×
1068
    // Skip this check for other field types (like the TSIG -  which is in the additional section)
×
1069
    // For a TSIG, the label is the dnskey, so it does not pass the endOn validation.
1070
    if (dnsRecord->d_place != DNSResourceRecord::ANSWER && dnsRecord->d_place != DNSResourceRecord::AUTHORITY) {
×
1071
      continue;
×
1072
    }
×
1073

×
1074
    if (!dnsRecord->d_name.isPartOf(di.zone)) {
×
1075
      g_log << Logger::Error << msgPrefix << "Received update/record out of zone, sending NotZone." << endl;
×
1076
      return RCode::NotZone;
×
1077
    }
1078
  }
1079

1080
  auto lock = std::scoped_lock(s_rfc2136lock); //TODO: i think this lock can be per zone, not for everything
1081
  g_log << Logger::Info << msgPrefix << "starting transaction." << endl;
1082
  if (!di.backend->startTransaction(packet.qdomainzone, UnknownDomainID)) { // Not giving the domain_id means that we do not delete the existing records.
×
1083
    g_log << Logger::Error << msgPrefix << "Backend for domain " << packet.qdomainzone << " does not support transaction. Can't do Update packet." << endl;
1084
    return RCode::NotImp;
1085
  }
1086

1087
  // 3.2.1 and 3.2.2 - Prerequisite check
1088
  for (const auto& answer : mdp.d_answers) {
×
1089
    const DNSRecord* dnsRecord = &answer;
×
1090
    if (dnsRecord->d_place == DNSResourceRecord::ANSWER) {
1091
      int res = checkUpdatePrerequisites(dnsRecord, &di);
1092
      if (res > 0) {
1093
        g_log << Logger::Error << msgPrefix << "Failed PreRequisites check for " << dnsRecord->d_name << ", returning " << RCode::to_s(res) << endl;
1094
        di.backend->abortTransaction();
1095
        return res;
×
1096
      }
1097
    }
1098
  }
1099

1100
  // 3.2.3 - Prerequisite check - this is outside of updatePrerequisitesCheck because we check an RRSet and not the RR.
1101
  if (auto rcode = updatePrereqCheck323(mdp, di, msgPrefix); rcode != RCode::NoError) {
1102
    di.backend->abortTransaction();
1103
    return rcode;
1104
  }
1105

1106
  // 3.4 - Prescan & Add/Update/Delete records - is all done within a try block.
1107
  try {
1108
    // 3.4.1 - Prescan section
1109
    for (const auto& answer : mdp.d_answers) {
1110
      const DNSRecord* dnsRecord = &answer;
1111
      if (dnsRecord->d_place == DNSResourceRecord::AUTHORITY) {
1112
        int res = checkUpdatePrescan(dnsRecord);
1113
        if (res > 0) {
1114
          g_log << Logger::Error << msgPrefix << "Failed prescan check, returning " << res << endl;
1115
          di.backend->abortTransaction();
1116
          return res;
1117
        }
1118
      }
1119
    }
1120

1121
    updateContext ctx{};
1122
    ctx.di = &di;
1123
    ctx.isPresigned = d_dk.isPresigned(di.zone);
1124
    ctx.narrow = false;
1125
    ctx.haveNSEC3 = d_dk.getNSEC3PARAM(di.zone, &ctx.ns3pr, &ctx.narrow);
1126
    ctx.updatedSerial = false;
1127

1128
    string soaEditSetting;
1129
    d_dk.getSoaEdit(di.zone, soaEditSetting);
1130

1131
    // 3.4.2 - Perform the updates.
1132
    // There's a special condition where deleting the last NS record at zone apex is never deleted (3.4.2.4)
1133
    // This means we must do it outside the normal performUpdate() because that focusses only on a separate RR.
1134

1135
    // Another special case is the addition of both a CNAME and a non-CNAME for the same name (#6270)
1136
    set<DNSName> cn; // NOLINT(readability-identifier-length)
1137
    set<DNSName> nocn;
1138
    for (const auto& rr : mdp.d_answers) { // NOLINT(readability-identifier-length)
1139
      if (rr.d_place == DNSResourceRecord::AUTHORITY && rr.d_class == QClass::IN && rr.d_ttl > 0) {
1140
        // Addition
1141
        if (rr.d_type == QType::CNAME) {
1142
          cn.insert(rr.d_name);
1143
        }
1144
        else if (rr.d_type != QType::RRSIG) {
1145
          nocn.insert(rr.d_name);
1146
        }
1147
      }
1148
    }
1149
    for (auto const& n : cn) { // NOLINT(readability-identifier-length)
1150
      if (nocn.count(n) > 0) {
1151
        g_log << Logger::Error << msgPrefix << "Refusing update, found CNAME and non-CNAME addition" << endl;
1152
        di.backend->abortTransaction();
1153
        return RCode::FormErr;
1154
      }
1155
    }
1156

1157
    uint changedRecords = 0;
1158
    if (auto rcode = updateRecords(mdp, d_dk, changedRecords, d_update_policy_lua, packet, ctx, msgPrefix); rcode != RCode::NoError) {
1159
      di.backend->abortTransaction();
1160
      return rcode;
1161
    }
1162

1163
    // Section 3.6 - Update the SOA serial - outside of performUpdate because we do a SOA update for the complete update message
1164
    if (changedRecords != 0 && !ctx.updatedSerial) {
1165
      increaseSerial(msgPrefix, soaEditSetting, ctx);
1166
      changedRecords++;
1167
    }
1168

1169
    if (changedRecords != 0) {
1170
      if (!di.backend->commitTransaction()) {
1171
        g_log << Logger::Error << msgPrefix << "Failed to commit updates!" << endl;
1172
        return RCode::ServFail;
1173
      }
1174

1175
      S.deposit("dnsupdate-changes", static_cast<int>(changedRecords));
1176

1177
      DNSSECKeeper::clearMetaCache(di.zone);
1178
      // Purge the records!
1179
      purgeAuthCaches(di.zone.operator const DNSName&().toString() + "$");
1180

1181
      // Notify secondaries
1182
      if (di.kind == DomainInfo::Primary) {
1183
        vector<string> notify;
1184
        B.getDomainMetadata(packet.qdomainzone, "NOTIFY-DNSUPDATE", notify);
1185
        if (!notify.empty() && notify.front() == "1") {
1186
          Communicator.notifyDomain(di.zone, &B);
1187
        }
1188
      }
1189

1190
      g_log << Logger::Info << msgPrefix << "Update completed, " << changedRecords << " changed records committed." << endl;
1191
    }
1192
    else {
1193
      //No change, no commit, we perform abort() because some backends might like this more.
1194
      g_log << Logger::Info << msgPrefix << "Update completed, 0 changes, rolling back." << endl;
1195
      di.backend->abortTransaction();
1196
    }
1197
    return RCode::NoError; //rfc 2136 3.4.2.5
1198
  }
1199
  catch (SSqlException& e) {
1200
    g_log << Logger::Error << msgPrefix << "Caught SSqlException: " << e.txtReason() << "; Sending ServFail!" << endl;
1201
    di.backend->abortTransaction();
1202
    return RCode::ServFail;
1203
  }
1204
  catch (DBException& e) {
1205
    g_log << Logger::Error << msgPrefix << "Caught DBException: " << e.reason << "; Sending ServFail!" << endl;
1206
    di.backend->abortTransaction();
1207
    return RCode::ServFail;
1208
  }
1209
  catch (PDNSException& e) {
1210
    g_log << Logger::Error << msgPrefix << "Caught PDNSException: " << e.reason << "; Sending ServFail!" << endl;
1211
    di.backend->abortTransaction();
1212
    return RCode::ServFail;
1213
  }
1214
  catch (std::exception& e) {
1215
    g_log << Logger::Error << msgPrefix << "Caught std:exception: " << e.what() << "; Sending ServFail!" << endl;
1216
    di.backend->abortTransaction();
1217
    return RCode::ServFail;
1218
  }
1219
  catch (...) {
1220
    g_log << Logger::Error << msgPrefix << "Caught unknown exception when performing update. Sending ServFail!" << endl;
1221
    di.backend->abortTransaction();
1222
    return RCode::ServFail;
1223
  }
1224
}
1225

1226
static void increaseSerial(const string& msgPrefix, const string& soaEditSetting, const updateContext& ctx)
1227
{
1228
  SOAData sd; // NOLINT(readability-identifier-length)
1229
  if (!ctx.di->backend->getSOA(ctx.di->zone, ctx.di->id, sd)) {
1230
    throw PDNSException("SOA-Serial update failed because there was no SOA. Wowie.");
1231
  }
1232

1233
  uint32_t oldSerial = sd.serial;
1234

1235
  vector<string> soaEdit2136Setting;
1236
  ctx.di->backend->getDomainMetadata(ctx.di->zone, "SOA-EDIT-DNSUPDATE", soaEdit2136Setting);
1237
  string soaEdit2136 = "DEFAULT";
1238
  string soaEdit;
1239
  if (!soaEdit2136Setting.empty()) {
1240
    soaEdit2136 = soaEdit2136Setting[0];
1241
    if (pdns_iequals(soaEdit2136, "SOA-EDIT") || pdns_iequals(soaEdit2136, "SOA-EDIT-INCREASE")) {
1242
      if (soaEditSetting.empty()) {
1243
        g_log << Logger::Error << msgPrefix << "Using " << soaEdit2136 << " for SOA-EDIT-DNSUPDATE increase on DNS update, but SOA-EDIT is not set for domain \"" << ctx.di->zone << "\". Using DEFAULT for SOA-EDIT-DNSUPDATE" << endl;
1244
        soaEdit2136 = "DEFAULT";
1245
      }
1246
      else {
1247
        soaEdit = soaEditSetting;
1248
      }
1249
    }
1250
  }
1251

1252
  DNSResourceRecord rr; // NOLINT(readability-identifier-length)
1253
  if (makeIncreasedSOARecord(sd, soaEdit2136, soaEdit, rr)) {
1254
    ctx.di->backend->replaceRRSet(ctx.di->id, rr.qname, rr.qtype, vector<DNSResourceRecord>(1, rr));
1255
    g_log << Logger::Notice << msgPrefix << "Increasing SOA serial (" << oldSerial << " -> " << sd.serial << ")" << endl;
1256

1257
    //Correct ordername + auth flag
1258
    DNSName ordername;
1259
    if (ctx.haveNSEC3) {
1260
      if (!ctx.narrow) {
1261
        ordername = DNSName(toBase32Hex(hashQNameWithSalt(ctx.ns3pr, rr.qname)));
1262
      }
1263
    }
1264
    else { // NSEC
1265
      ordername = rr.qname.makeRelative(ctx.di->zone);
1266
    }
1267
    ctx.di->backend->updateDNSSECOrderNameAndAuth(ctx.di->id, rr.qname, ordername, true, QType::ANY, ctx.haveNSEC3 && !ctx.narrow);
1268
  }
1269
}
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