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

PowerDNS / pdns / 20618548088

31 Dec 2025 12:00PM UTC coverage: 72.648% (-0.7%) from 73.336%
20618548088

Pull #16693

github

web-flow
Merge 3f7d9a75b into 65de281db
Pull Request #16693: auth: plumbing for structured logging

39009 of 65430 branches covered (59.62%)

Branch coverage included in aggregate %.

807 of 2400 new or added lines in 58 files covered. (33.63%)

200 existing lines in 39 files now uncovered.

129187 of 166092 relevant lines covered (77.78%)

5266744.49 hits per line

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

67.19
/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
  DomainInfo di{};
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
  // Logging-related fields
60
  std::string msgPrefix;
61
  std::shared_ptr<Logr::Logger> slog;
62
};
63

64
static void increaseSerial(const string& soaEditSetting, const updateContext& ctx);
65

66
static DNSName computeOrdername(const updateContext& ctx, const DNSName& qname)
67
{
5,888✔
68
  DNSName ordername;
5,888✔
69
  if (ctx.haveNSEC3) {
5,888✔
70
    if (!ctx.narrow) {
3,310✔
71
      ordername = DNSName(toBase32Hex(hashQNameWithSalt(ctx.ns3pr, qname)));
2,226✔
72
    }
2,226✔
73
  }
3,310✔
74
  else { // NSEC
2,578✔
75
    ordername = qname.makeRelative(ctx.di.zone);
2,578✔
76
  }
2,578✔
77
  return ordername;
5,888✔
78
}
5,888✔
79

80
// Implement section 3.2.1 and 3.2.2 of RFC2136
81
// NOLINTNEXTLINE(readability-identifier-length)
82
static int checkUpdatePrerequisites(const DNSRecord* rr, DomainInfo* di)
83
{
418✔
84
  if (rr->d_ttl != 0) {
418!
85
    return RCode::FormErr;
×
86
  }
×
87

88
  // 3.2.1 and 3.2.2 check content length.
89
  if ((rr->d_class == QClass::NONE || rr->d_class == QClass::ANY) && rr->d_clen != 0) {
418!
90
    return RCode::FormErr;
×
91
  }
×
92

93
  bool foundRecord = false;
418✔
94
  DNSResourceRecord rec;
418✔
95
  di->backend->lookup(QType(QType::ANY), rr->d_name, di->id);
418✔
96
  while (di->backend->get(rec)) {
440✔
97
    if (rec.qtype.getCode() == QType::ENT) {
330✔
98
      continue;
22✔
99
    }
22✔
100
    if ((rr->d_type != QType::ANY && rec.qtype == rr->d_type) || rr->d_type == QType::ANY) {
308!
101
      foundRecord = true;
308✔
102
      di->backend->lookupEnd();
308✔
103
      break;
308✔
104
    }
308✔
105
  }
308✔
106

107
  // Section 3.2.1
108
  if (rr->d_class == QClass::ANY && !foundRecord) {
418✔
109
    if (rr->d_type == QType::ANY) {
66✔
110
      return RCode::NXDomain;
44✔
111
    }
44✔
112
    if (rr->d_type != QType::ANY) {
22!
113
      return RCode::NXRRSet;
22✔
114
    }
22✔
115
  }
22✔
116

117
  // Section 3.2.2
118
  if (rr->d_class == QClass::NONE && foundRecord) {
352✔
119
    if (rr->d_type == QType::ANY) {
44✔
120
      return RCode::YXDomain;
22✔
121
    }
22✔
122
    if (rr->d_type != QType::ANY) {
22!
123
      return RCode::YXRRSet;
22✔
124
    }
22✔
125
  }
22✔
126

127
  return RCode::NoError;
308✔
128
}
352✔
129

130
// Method implements section 3.4.1 of RFC2136
131
// NOLINTNEXTLINE(readability-identifier-length)
132
static int checkUpdatePrescan(const DNSRecord* rr)
133
{
5,006✔
134
  // The RFC stats that d_class != ZCLASS, but we only support the IN class.
135
  if (rr->d_class != QClass::IN && rr->d_class != QClass::NONE && rr->d_class != QClass::ANY) {
5,006!
136
    return RCode::FormErr;
×
137
  }
×
138

139
  auto qtype = QType(rr->d_type);
5,006✔
140

141
  if (!qtype.isSupportedType()) {
5,006!
142
    return RCode::FormErr;
×
143
  }
×
144

145
  if ((rr->d_class == QClass::NONE || rr->d_class == QClass::ANY) && rr->d_ttl != 0) {
5,006!
146
    return RCode::FormErr;
×
147
  }
×
148

149
  if (rr->d_class == QClass::ANY && rr->d_clen != 0) {
5,006!
150
    return RCode::FormErr;
×
151
  }
×
152

153
  if (qtype.isMetadataType()) {
5,006!
154
    return RCode::FormErr;
×
155
  }
×
156

157
  if (rr->d_class != QClass::ANY && qtype.getCode() == QType::ANY) {
5,006✔
158
    return RCode::FormErr;
22✔
159
  }
22✔
160

161
  return RCode::NoError;
4,984✔
162
}
5,006✔
163

164
// Implements section 3.4.2 of RFC2136
165
// Due to large complexity, this is stuck in multiple routines.
166

167
static bool mayPerformUpdate(const DNSRecord* rr, const updateContext& ctx) // NOLINT(readability-identifier-length)
168
{
4,852✔
169
  auto rrType = QType(rr->d_type);
4,852✔
170

171
  if (rrType == QType::NSEC || rrType == QType::NSEC3) {
4,852!
NEW
172
    SLOG(g_log << Logger::Warning << ctx.msgPrefix << "Trying to add/update/delete " << rr->d_name << "|" << rrType.toString() << ". These are generated records, ignoring!" << endl,
×
NEW
173
         ctx.slog->info(Logr::Warning, "Update: trying to add/update/delete generated record, ignoring", "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType)));
×
174
    return false;
×
175
  }
×
176

177
  if (!ctx.isPresigned && rrType == QType::RRSIG) {
4,852!
NEW
178
    SLOG(g_log << Logger::Warning << ctx.msgPrefix << "Trying to add/update/delete " << rr->d_name << "|" << rrType.toString() << " in non-presigned zone, ignoring!" << endl,
×
NEW
179
         ctx.slog->info(Logr::Warning, "Update: trying to add/update/delete record in non-presigned zone, ignoring", "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType)));
×
180
    return false;
×
181
  }
×
182

183
  if ((rrType == QType::NSEC3PARAM || rrType == QType::DNSKEY) && rr->d_name != ctx.di.zone.operator const DNSName&()) {
4,852!
NEW
184
    SLOG(g_log << Logger::Warning << ctx.msgPrefix << "Trying to add/update/delete " << rr->d_name << "|" << rrType.toString() << ", " << rrType.toString() << " must be at zone apex, ignoring!" << endl,
×
NEW
185
         ctx.slog->info(Logr::Warning, "Update: trying to add/update/delete record which must be at zone apex, ignoring", "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType)));
×
186
    return false;
×
187
  }
×
188

189
  return true;
4,852✔
190
}
4,852✔
191

192
// 3.4.2.2 QClass::IN means insert or update
193
// Caller has checked that we are allowed to insert the record and has handled
194
// the NSEC3PARAM case already.
195
// ctx is not const, may update updateSerial
196
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
197
static uint performInsert(const DNSRecord* rr, updateContext& ctx, set<DNSName>& insnonterm, set<DNSName>& delnonterm) // NOLINT(readability-identifier-length)
198
{
3,600✔
199
  vector<DNSResourceRecord> rrset;
3,600✔
200
  uint changedRecords = 0;
3,600✔
201
  DNSResourceRecord rec;
3,600✔
202
  auto rrType = QType(rr->d_type);
3,600✔
203

204
  ctx.di.backend->lookup(rrType, rr->d_name, ctx.di.id);
3,600✔
205
  while (ctx.di.backend->get(rec)) {
110,828✔
206
    rrset.push_back(rec);
107,228✔
207
  }
107,228✔
208
  bool foundRecord = !rrset.empty();
3,600✔
209

210
  if (foundRecord) {
3,600✔
211
    switch (rrType) {
2,552✔
212
    case QType::SOA: {
88✔
213
      // SOA updates require the serial to be higher than the current
214
      SOAData sdOld;
88✔
215
      SOAData sdUpdate;
88✔
216
      DNSResourceRecord* oldRec = &rrset.front();
88✔
217
      fillSOAData(oldRec->content, sdOld);
88✔
218
      oldRec->setContent(rr->getContent()->getZoneRepresentation());
88✔
219
      fillSOAData(oldRec->content, sdUpdate);
88✔
220
      if (rfc1982LessThan(sdOld.serial, sdUpdate.serial)) {
88✔
221
        ctx.di.backend->replaceRRSet(ctx.di.id, oldRec->qname, oldRec->qtype, rrset);
44✔
222
        ctx.updatedSerial = true;
44✔
223
        changedRecords++;
44✔
224
        SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Replacing SOA record " << rr->d_name << "|" << rrType.toString() << endl,
44!
225
             ctx.slog->info(Logr::Notice, "Update: replacing SOA record", "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType)));
44✔
226
      }
44✔
227
      else {
44✔
228
        SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Provided serial (" << sdUpdate.serial << ") is older than the current serial (" << sdOld.serial << "), ignoring SOA update." << endl,
44!
229
             ctx.slog->info(Logr::Notice, "Update: provided serial is older than the current serial, ignoring SOA update", "received serial", Logging::Loggable(sdUpdate.serial), "current serial", Logging::Loggable(sdOld.serial)));
44✔
230
      }
44✔
231
    } break;
88✔
232
    case QType::CNAME: {
44✔
233
      // It's not possible to have multiple CNAME's with the same NAME. So we always update.
234
      int changedCNames = 0;
44✔
235
      for (auto& i : rrset) { // NOLINT(readability-identifier-length)
44✔
236
        if (i.ttl != rr->d_ttl || i.content != rr->getContent()->getZoneRepresentation()) {
44!
237
          i.ttl = rr->d_ttl;
44✔
238
          i.setContent(rr->getContent()->getZoneRepresentation());
44✔
239
          changedCNames++;
44✔
240
        }
44✔
241
      }
44✔
242
      if (changedCNames > 0) {
44!
243
        ctx.di.backend->replaceRRSet(ctx.di.id, rr->d_name, rrType, rrset);
44✔
244
        SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Replacing CNAME record " << rr->d_name << "|" << rrType.toString() << endl,
44!
245
             ctx.slog->info(Logr::Notice, "Update: replacing CNAME record", "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType)));
44✔
246
        changedRecords += changedCNames;
44✔
247
      }
44✔
248
      else {
×
NEW
249
        SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Replace for CNAME record " << rr->d_name << "|" << rrType.toString() << " requested, but no changes made." << endl,
×
NEW
250
             ctx.slog->info(Logr::Notice, "Update: replace for CNAME record requested, but no changes made", "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType)));
×
UNCOV
251
      }
×
252
    } break;
44✔
253
    default: {
2,420✔
254
      // In any other case, we must check if the TYPE and RDATA match to provide an update (which effectively means an update of TTL)
255
      int updateTTL = 0;
2,420✔
256
      foundRecord = false;
2,420✔
257
      bool lowerCase = false;
2,420✔
258
      switch (rrType.getCode()) {
2,420✔
259
      case QType::MX:
88✔
260
      case QType::PTR:
110✔
261
      case QType::SRV:
154✔
262
        lowerCase = true;
154✔
263
        break;
154✔
264
      }
2,420✔
265
      string content = rr->getContent()->getZoneRepresentation();
2,420✔
266
      if (lowerCase) {
2,420✔
267
        content = toLower(content);
154✔
268
      }
154✔
269
      for (auto& i : rrset) { // NOLINT(readability-identifier-length)
107,096✔
270
        if (rrType != i.qtype.getCode()) {
107,096!
271
          continue;
×
272
        }
×
273
        if (!foundRecord) {
107,096✔
274
          string icontent = i.getZoneRepresentation();
107,030✔
275
          if (lowerCase) {
107,030✔
276
            icontent = toLower(icontent);
198✔
277
          }
198✔
278
          if (icontent == content) {
107,030✔
279
            foundRecord = true;
132✔
280
          }
132✔
281
        }
107,030✔
282
        if (i.ttl != rr->d_ttl) {
107,096✔
283
          i.ttl = rr->d_ttl;
264✔
284
          updateTTL++;
264✔
285
        }
264✔
286
      }
107,096✔
287
      if (updateTTL > 0) {
2,420✔
288
        ctx.di.backend->replaceRRSet(ctx.di.id, rr->d_name, rrType, rrset);
154✔
289
        SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Updating TTLs for " << rr->d_name << "|" << rrType.toString() << endl,
154!
290
             ctx.slog->info(Logr::Notice, "Update: updating record TTLs", "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType)));
154✔
291
        changedRecords += updateTTL;
154✔
292
      }
154✔
293
      else if (foundRecord) {
2,266✔
294
        SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Replace for recordset " << rr->d_name << "|" << rrType.toString() << " requested, but no changes made." << endl,
22!
295
             ctx.slog->info(Logr::Notice, "Update: replace for recordset requested, but no changes made", "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType)));
22✔
296
      }
22✔
297
    } break;
2,420✔
298
    }
2,552✔
299

300
    // ReplaceRRSet dumps our ordername and auth flag, so we need to correct it if we have changed records.
301
    // We can take the auth flag from the first RR in the set, as the name is different, so should the auth be.
302
    if (changedRecords > 0) {
2,552✔
303
      bool auth = rrset.front().auth;
242✔
304
      DNSName ordername = computeOrdername(ctx, rr->d_name);
242✔
305
      ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, rr->d_name, ordername, auth, QType::ANY, ctx.haveNSEC3 && !ctx.narrow);
242✔
306
      if (!auth || rrType == QType::DS) {
242!
307
        if (ctx.haveNSEC3) {
×
308
          ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, rr->d_name, DNSName(), false, QType::NS, !ctx.narrow);
×
309
        }
×
310
        ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, rr->d_name, DNSName(), false, QType::A, ctx.haveNSEC3 && !ctx.narrow);
×
311
        ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, rr->d_name, DNSName(), false, QType::AAAA, ctx.haveNSEC3 && !ctx.narrow);
×
312
      }
×
313
    }
242✔
314
    rrset.clear(); // no longer needed
2,552✔
315
  } // if (foundRecord) - note foundRecord may have be set to false at this point
2,552✔
316

317
  // If we haven't found a record that matches, we must add it.
318
  if (!foundRecord) {
3,600✔
319
    SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Adding record " << rr->d_name << "|" << rrType.toString() << endl,
3,336!
320
         ctx.slog->info(Logr::Notice, "Update: adding record", "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType)));
3,336✔
321
    delnonterm.insert(rr->d_name); // always remove any ENT's in the place where we're going to add a record.
3,336✔
322
    auto newRec = DNSResourceRecord::fromWire(*rr);
3,336✔
323
    newRec.domain_id = ctx.di.id;
3,336✔
324
    newRec.auth = (rr->d_name == ctx.di.zone.operator const DNSName&() || rrType.getCode() != QType::NS);
3,336✔
325
    ctx.di.backend->feedRecord(newRec, DNSName());
3,336✔
326
    changedRecords++;
3,336✔
327

328
    // because we added a record, we need to fix DNSSEC data.
329
    DNSName shorter(rr->d_name);
3,336✔
330
    bool auth = newRec.auth;
3,336✔
331
    bool fixDS = (rrType == QType::DS);
3,336✔
332

333
    if (ctx.di.zone.operator const DNSName&() != shorter) { // Everything at APEX is auth=1 && no ENT's
3,336✔
334
      do {
6,672✔
335
        if (ctx.di.zone.operator const DNSName&() == shorter) {
6,672✔
336
          break;
2,882✔
337
        }
2,882✔
338

339
        bool foundShorter = false;
3,790✔
340
        ctx.di.backend->lookup(QType(QType::ANY), shorter, ctx.di.id);
3,790✔
341
        while (ctx.di.backend->get(rec)) {
114,280✔
342
          if (rec.qname == rr->d_name && rec.qtype == QType::DS) {
110,490✔
343
            fixDS = true;
66✔
344
          }
66✔
345
          if (shorter != rr->d_name) {
110,490✔
346
            foundShorter = true;
366✔
347
          }
366✔
348
          if (rec.qtype == QType::NS) { // are we inserting below a delegate?
110,490✔
349
            auth = false;
550✔
350
          }
550✔
351
        }
110,490✔
352

353
        if (!foundShorter && auth && shorter != rr->d_name) { // haven't found any record at current level, insert ENT.
3,790✔
354
          insnonterm.insert(shorter);
220✔
355
        }
220✔
356
        if (foundShorter) {
3,790✔
357
          break; // if we find a shorter record, we can stop searching
278✔
358
        }
278✔
359
      } while (shorter.chopOff());
3,790!
360
    }
3,160✔
361

362
    {
×
363
      DNSName ordername = computeOrdername(ctx, rr->d_name);
3,336✔
364
      ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, rr->d_name, ordername, auth, QType::ANY, ctx.haveNSEC3 && !ctx.narrow);
3,336✔
365
      if (fixDS) {
3,336✔
366
        ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, rr->d_name, ordername, true, QType::DS, ctx.haveNSEC3 && !ctx.narrow);
66✔
367
      }
66✔
368
      if (!auth) {
3,336✔
369
        if (ctx.haveNSEC3 && ctx.ns3pr.d_flags != 0) {
418✔
370
          ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, rr->d_name, DNSName(), false, QType::NS, !ctx.narrow);
152✔
371
        }
152✔
372
        ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, rr->d_name, DNSName(), false, QType::A, ctx.haveNSEC3 && !ctx.narrow);
418✔
373
        ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, rr->d_name, DNSName(), false, QType::AAAA, ctx.haveNSEC3 && !ctx.narrow);
418✔
374
      }
418✔
375
    }
3,336✔
376

377
    // If we insert an NS, all the records below it become non auth - so, we're inserting a delegate.
378
    // Auth can only be false when the rr->d_name is not the zone
379
    if (!auth && rrType == QType::NS) {
3,336✔
380
      DLOG(SLOG(g_log << ctx.msgPrefix << "Going to fix auth flags below " << rr->d_name << endl,
242✔
381
                ctx.slog->info(Logr::Debug, "Update: going to fix auth flags below", "name", Logging::Loggable(rr->d_name))));
242✔
382
      insnonterm.clear(); // No ENT's are needed below delegates (auth=0)
242✔
383
      vector<DNSName> qnames;
242✔
384
      ctx.di.backend->listSubZone(ZoneName(rr->d_name), ctx.di.id);
242✔
385
      while (ctx.di.backend->get(rec)) {
726✔
386
        if (rec.qtype.getCode() != QType::ENT && rec.qtype.getCode() != QType::DS && rr->d_name != rec.qname) { // Skip ENT, DS and our already corrected record.
484✔
387
          qnames.push_back(rec.qname);
132✔
388
        }
132✔
389
      }
484✔
390
      for (const auto& qname : qnames) {
242✔
391
        DNSName ordername = computeOrdername(ctx, qname);
132✔
392
        if (ctx.haveNSEC3) {
132✔
393
          ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, qname, ordername, auth, QType::ANY, !ctx.narrow);
72✔
394
          if (ctx.ns3pr.d_flags != 0) {
72✔
395
            ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, qname, DNSName(), false, QType::NS, !ctx.narrow);
48✔
396
          }
48✔
397
        }
72✔
398
        else { // NSEC
60✔
399
          ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, qname, ordername, false, QType::NS, false);
60✔
400
        }
60✔
401

402
        ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, qname, DNSName(), false, QType::A, ctx.haveNSEC3 && !ctx.narrow);
132✔
403
        ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, qname, DNSName(), false, QType::AAAA, ctx.haveNSEC3 && !ctx.narrow);
132✔
404
      }
132✔
405
    }
242✔
406
  }
3,336✔
407

408
  return changedRecords;
×
409
}
3,600✔
410

411
// 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
412
// the code that calls this performUpdate().
413
// Caller has checked that we are allowed to delete the record and has handled
414
// the NSEC3PARAM case already.
415
static uint performDelete(const DNSRecord* rr, const updateContext& ctx, set<DNSName>& insnonterm, set<DNSName>& delnonterm) // NOLINT(readability-identifier-length)
416
{
1,158✔
417
  vector<DNSResourceRecord> recordsToKeep;
1,158✔
418
  vector<DNSResourceRecord> recordsToDelete;
1,158✔
419
  DNSResourceRecord rec;
1,158✔
420
  auto rrType = QType(rr->d_type);
1,158✔
421

422
  ctx.di.backend->lookup(rrType, rr->d_name, ctx.di.id);
1,158✔
423
  while (ctx.di.backend->get(rec)) {
4,736✔
424
    if (rr->d_class == QClass::ANY) { // 3.4.2.3
3,578✔
425
      if (rec.qname == ctx.di.zone.operator const DNSName&() && (rec.qtype == QType::NS || rec.qtype == QType::SOA)) { // Never delete all SOA and NS's
2,992!
426
        recordsToKeep.push_back(rec);
44✔
427
      }
44✔
428
      else {
2,948✔
429
        recordsToDelete.push_back(rec);
2,948✔
430
      }
2,948✔
431
    }
2,992✔
432
    if (rr->d_class == QClass::NONE) { // 3.4.2.4
3,578✔
433
      auto repr = rec.getZoneRepresentation();
586✔
434
      if (rec.qtype == QType::TXT) {
586✔
435
        DLOG(SLOG(g_log << ctx.msgPrefix << "Adjusting TXT content from [" << repr << "]" << endl,
66✔
436
                  ctx.slog->info(Logr::Debug, "Update: adjusting TXT content", "content", Logging::Loggable(repr))));
66✔
437
        auto drc = DNSRecordContent::make(rec.qtype.getCode(), QClass::IN, repr);
66✔
438
        auto ser = drc->serialize(rec.qname, true, true);
66✔
439
        auto rc = DNSRecordContent::deserialize(rec.qname, rec.qtype.getCode(), ser); // NOLINT(readability-identifier-length)
66✔
440
        repr = rc->getZoneRepresentation(true);
66✔
441
        DLOG(SLOG(g_log << ctx.msgPrefix << "Adjusted TXT content to [" << repr << "]" << endl,
66✔
442
                  ctx.slog->info(Logr::Debug, "Update: adjusted TXT to", "content", Logging::Loggable(repr))));
66✔
443
      }
66✔
444
      DLOG(SLOG(g_log << ctx.msgPrefix << "Matching RR in RRset - (adjusted) representation from request=[" << repr << "], rr->getContent()->getZoneRepresentation()=[" << rr->getContent()->getZoneRepresentation() << "]" << endl,
586✔
445
                ctx.slog->info(Logr::Debug, "Update: matching RR in RRset, adjusting representation", "requested content", Logging::Loggable(repr), "adjusted content", Logging::Loggable(rr->getContent()->getZoneRepresentation()))));
586✔
446
      if (rrType == rec.qtype && repr == rr->getContent()->getZoneRepresentation()) {
586!
447
        recordsToDelete.push_back(rec);
410✔
448
      }
410✔
449
      else {
176✔
450
        recordsToKeep.push_back(rec);
176✔
451
      }
176✔
452
    }
586✔
453
  }
3,578✔
454

455
  if (recordsToDelete.empty()) {
1,158✔
456
    SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Deletion for record " << rr->d_name << "|" << rrType.toString() << " requested, but not found." << endl,
44!
457
         ctx.slog->info(Logr::Notice, "Update: record deletion requested, but record not found", "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType)));
44✔
458
    return 0;
44✔
459
  }
44✔
460

461
  ctx.di.backend->replaceRRSet(ctx.di.id, rr->d_name, rrType, recordsToKeep);
1,114✔
462
  recordsToKeep.clear(); // no longer needed
1,114✔
463
  SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Deleting record " << rr->d_name << "|" << rrType.toString() << endl,
1,114!
464
       ctx.slog->info(Logr::Notice, "Update: deleting record", "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType)));
1,114✔
465

466
  // If we've removed a delegate, we need to reset ordername/auth for some records.
467
  if (rrType == QType::NS && rr->d_name != ctx.di.zone.operator const DNSName&()) {
1,114✔
468
    vector<DNSName> belowOldDelegate;
154✔
469
    vector<DNSName> nsRecs;
154✔
470
    vector<DNSName> updateAuthFlag;
154✔
471
    ctx.di.backend->listSubZone(ZoneName(rr->d_name), ctx.di.id);
154✔
472
    while (ctx.di.backend->get(rec)) {
396✔
473
      if (rec.qtype.getCode() != QType::ENT) { // skip ENT records, they are always auth=false
242!
474
        belowOldDelegate.push_back(rec.qname);
242✔
475
      }
242✔
476
      if (rec.qtype.getCode() == QType::NS && rec.qname != rr->d_name) {
242!
477
        nsRecs.push_back(rec.qname);
22✔
478
      }
22✔
479
    }
242✔
480

481
    for (auto& belowOldDel : belowOldDelegate) {
242✔
482
      bool isBelowDelegate = false;
242✔
483
      for (const auto& ns : nsRecs) { // NOLINT(readability-identifier-length)
242✔
484
        if (ns.isPartOf(belowOldDel)) {
66✔
485
          isBelowDelegate = true;
22✔
486
          break;
22✔
487
        }
22✔
488
      }
66✔
489
      if (!isBelowDelegate) {
242✔
490
        updateAuthFlag.push_back(belowOldDel);
220✔
491
      }
220✔
492
    }
242✔
493

494
    for (const auto& changeRec : updateAuthFlag) {
220✔
495
      DNSName ordername = computeOrdername(ctx, changeRec);
220✔
496
      ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, changeRec, ordername, true, QType::ANY, ctx.haveNSEC3 && !ctx.narrow);
220✔
497
    }
220✔
498
  }
154✔
499

500
  // Fix ENT records.
501
  // We must check if we have a record below the current level and if we removed the 'last' record
502
  // on that level. If so, we must insert an ENT record.
503
  // 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.
504
  bool foundDeeper = false;
1,114✔
505
  bool foundOtherWithSameName = false;
1,114✔
506
  ctx.di.backend->listSubZone(ZoneName(rr->d_name), ctx.di.id);
1,114✔
507
  while (ctx.di.backend->get(rec)) {
6,328✔
508
    if (rec.qname == rr->d_name && count(recordsToDelete.begin(), recordsToDelete.end(), rec) == 0) {
5,214!
509
      foundOtherWithSameName = true;
946✔
510
    }
946✔
511
    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
5,214✔
512
      foundDeeper = true;
3,872✔
513
    }
3,872✔
514
  }
5,214✔
515

516
  if (foundDeeper && !foundOtherWithSameName) {
1,114✔
517
    insnonterm.insert(rr->d_name);
132✔
518
  }
132✔
519
  else if (!foundOtherWithSameName) {
982✔
520
    // If we didn't have to insert an ENT, we might have deleted a record at very deep level
521
    // and we must then clean up the ENT's above the deleted record.
522
    DNSName shorter(rr->d_name);
718✔
523
    while (shorter != ctx.di.zone.operator const DNSName&()) {
1,114!
524
      shorter.chopOff();
1,114✔
525
      bool foundRealRR = false;
1,114✔
526
      bool foundEnt = false;
1,114✔
527

528
      // The reason for a listSubZone here is because might go up the tree and find the ENT of another branch
529
      // consider these non ENT-records:
530
      // b.c.d.e.test.com
531
      // b.d.e.test.com
532
      // 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.
533
      // At that point we can stop deleting ENT's because the tree is in tact again.
534
      ctx.di.backend->listSubZone(ZoneName(shorter), ctx.di.id);
1,114✔
535

536
      while (ctx.di.backend->get(rec)) {
15,170✔
537
        if (rec.qtype.getCode() != QType::ENT) {
14,056✔
538
          foundRealRR = true;
12,150✔
539
        }
12,150✔
540
        else {
1,906✔
541
          foundEnt = true;
1,906✔
542
        }
1,906✔
543
      }
14,056✔
544
      if (!foundRealRR) {
1,114✔
545
        if (foundEnt) { // only delete the ENT if we actually found one.
396✔
546
          delnonterm.insert(shorter);
330✔
547
        }
330✔
548
      }
396✔
549
      else {
718✔
550
        break;
718✔
551
      }
718✔
552
    }
1,114✔
553
  }
718✔
554

555
  return recordsToDelete.size();
1,114✔
556
}
1,158✔
557

558
static void updateENT(const updateContext& ctx, set<DNSName>& insnonterm, set<DNSName>& delnonterm)
559
{
4,758✔
560
  if (insnonterm.empty() && delnonterm.empty()) {
4,758✔
561
    return;
1,114✔
562
  }
1,114✔
563

564
  DLOG(SLOG(g_log << ctx.msgPrefix << "Updating ENT records - " << insnonterm.size() << "|" << delnonterm.size() << endl,
3,644✔
565
            ctx.slog->info(Logr::Debug, "Update: updating ENT records", "added ENT records", Logging::Loggable(insnonterm.size()), "deleted ENT records", Logging::Loggable(delnonterm.size()))));
3,644✔
566
  ctx.di.backend->updateEmptyNonTerminals(ctx.di.id, insnonterm, delnonterm, false);
3,644✔
567
  if (ctx.haveNSEC3) {
3,644✔
568
    for (const auto& insert : insnonterm) {
1,994✔
569
      DNSName ordername = computeOrdername(ctx, insert);
192✔
570
      ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, insert, ordername, true, QType::ANY, !ctx.narrow);
192✔
571
    }
192✔
572
  }
1,994✔
573
}
3,644✔
574

575
static uint performUpdate(DNSSECKeeper& dsk, const DNSRecord* rr, updateContext& ctx) // NOLINT(readability-identifier-length)
576
{
4,852✔
577
  if (!mayPerformUpdate(rr, ctx)) {
4,852!
578
    return 0;
×
579
  }
×
580

581
  auto rrType = QType(rr->d_type);
4,852✔
582

583
  // Decide which action to take.
584
  // 3.4.2.2 QClass::IN means insert or update
585
  bool insertAction = rr->d_class == QClass::IN;
4,852✔
586
  bool deleteAction = (rr->d_class == QClass::ANY || rr->d_class == QClass::NONE) && rrType != QType::SOA; // never delete a SOA.
4,852✔
587

588
  if (!insertAction && !deleteAction) {
4,852✔
589
    return 0; // nothing to do!
22✔
590
  }
22✔
591

592
  // Special processing for NSEC3PARAM
593
  if (rrType == QType::NSEC3PARAM) {
4,830✔
594
    if (insertAction) {
72✔
595
      SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Adding/updating NSEC3PARAM for zone, resetting ordernames." << endl,
44!
596
           ctx.slog->info(Logr::Notice, "Update: adding/updating NSEC3PARAM for zone, resetting ordernames."));
44✔
597

598
      ctx.ns3pr = NSEC3PARAMRecordContent(rr->getContent()->getZoneRepresentation(), ctx.di.zone);
44✔
599
      // adding a NSEC3 will cause narrow mode to be dropped, as you cannot specify that in a NSEC3PARAM record
600
      ctx.narrow = false;
44✔
601
      dsk.setNSEC3PARAM(ctx.di.zone, ctx.ns3pr, ctx.narrow);
44✔
602
      ctx.haveNSEC3 = true;
44✔
603
    }
44✔
604
    else {
28✔
605
      SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Deleting NSEC3PARAM from zone, resetting ordernames." << endl,
28!
606
           ctx.slog->info(Logr::Notice, "Update: deleting NSEC3PARAM from zone, resetting ordernames."));
28✔
607
      // Be sure to use a ZoneName with a variant matching the domain we are
608
      // working on, for the sake of unsetNSEC3PARAM.
609
      ZoneName zonename(rr->d_name, ctx.di.zone.getVariant());
28✔
610
      if (rr->d_class == QClass::ANY) {
28!
611
        dsk.unsetNSEC3PARAM(zonename);
28✔
612
      }
28✔
613
      else { // rr->d_class == QClass::NONE then
×
614
        NSEC3PARAMRecordContent nsec3rr(rr->getContent()->getZoneRepresentation(), ctx.di.zone);
×
615
        if (ctx.haveNSEC3 && ctx.ns3pr.getZoneRepresentation() == nsec3rr.getZoneRepresentation()) {
×
616
          dsk.unsetNSEC3PARAM(zonename);
×
617
        }
×
618
        else {
×
619
          return 0;
×
620
        }
×
621
      }
×
622

623
      // Update NSEC3 variables, other RR's in this update package might need them as well.
624
      ctx.narrow = false;
28✔
625
      ctx.haveNSEC3 = false;
28✔
626
    }
28✔
627

628
    string error;
72✔
629
    string info;
72✔
630
    if (!dsk.rectifyZone(ctx.di.zone, error, info, false)) {
72!
631
      throw PDNSException("Failed to rectify '" + ctx.di.zone.toLogString() + "': " + error);
×
632
    }
×
633
    return 1;
72✔
634
  }
72✔
635

636
  uint changedRecords = 0;
4,758✔
637
  // used to (at the end) fix ENT records.
638
  set<DNSName> delnonterm;
4,758✔
639
  set<DNSName> insnonterm;
4,758✔
640

641
  if (insertAction) {
4,758✔
642
    DLOG(SLOG(g_log << ctx.msgPrefix << "Add/Update record (QClass == IN) " << rr->d_name << "|" << rrType.toString() << endl,
3,600✔
643
              ctx.slog->info(Logr::Debug, "Update: add/update record", "QClass", Logging::Loggable("IN"), "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType))));
3,600✔
644
    changedRecords = performInsert(rr, ctx, insnonterm, delnonterm);
3,600✔
645
  }
3,600✔
646
  else {
1,158✔
647
    DLOG(SLOG(g_log << ctx.msgPrefix << "Deleting records: " << rr->d_name << "; QClass:" << rr->d_class << "; rrType: " << rrType.toString() << endl,
1,158✔
648
              ctx.slog->info(Logr::Debug, "Update: deleting record", "QClass", Logging::Loggable(rr->d_class), "name", Logging::Loggable(rr->d_name), "type", Logging::Loggable(rrType))));
1,158✔
649
    changedRecords = performDelete(rr, ctx, insnonterm, delnonterm);
1,158✔
650
  }
1,158✔
651

652
  //Insert and delete ENT's
653
  updateENT(ctx, insnonterm, delnonterm);
4,758✔
654

655
  return changedRecords;
4,758✔
656
}
4,830✔
657

658
static int forwardPacket(UeberBackend& B, const updateContext& ctx, const DNSPacket& p) // NOLINT(readability-identifier-length)
659
{
×
660
  vector<string> forward;
×
661
  B.getDomainMetadata(p.qdomainzone, "FORWARD-DNSUPDATE", forward);
×
662

663
  if (forward.empty() && !::arg().mustDo("forward-dnsupdate")) {
×
NEW
664
    SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Not configured to forward to primary, returning Refused." << endl,
×
NEW
665
         ctx.slog->info(Logr::Notice, "Update: not configured to forward to primary, returning Refused"));
×
666
    return RCode::Refused;
×
667
  }
×
668

669
  for (const auto& remote : ctx.di.primaries) {
×
NEW
670
    SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Forwarding packet to primary " << remote << endl,
×
NEW
671
         ctx.slog->info(Logr::Notice, "Update: forwarding packet to primary", "primary", Logging::Loggable(remote)));
×
672

673
    if (!pdns::isQueryLocalAddressFamilyEnabled(remote.sin4.sin_family)) {
×
674
      continue;
×
675
    }
×
676
    auto local = pdns::getQueryLocalAddress(remote.sin4.sin_family, 0);
×
677
    int sock = makeQuerySocket(local, false); // create TCP socket. RFC2136 section 6.2 seems to be ok with this.
×
678
    if (sock < 0) {
×
NEW
679
      SLOG(g_log << Logger::Error << ctx.msgPrefix << "Error creating socket: " << stringerror() << endl,
×
NEW
680
           ctx.slog->error(Logr::Error, errno, "Update: error creating socket"));
×
681
      continue;
×
682
    }
×
683

684
    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
×
NEW
685
      SLOG(g_log << Logger::Error << ctx.msgPrefix << "Failed to connect to " << remote.toStringWithPort() << ": " << stringerror() << endl,
×
NEW
686
           ctx.slog->error(Logr::Error, errno, "Update: failed to connect", "remote", Logging::Loggable(remote.toStringWithPort())));
×
687
      try {
×
688
        closesocket(sock);
×
689
      }
×
690
      catch (const PDNSException& e) {
×
NEW
691
        SLOG(g_log << Logger::Error << "Error closing primary forwarding socket after connect() failed: " << e.reason << endl,
×
NEW
692
             ctx.slog->error(Logr::Error, errno, "Update: error closing primary forwarding socket after connect() failed"));
×
693
      }
×
694
      continue;
×
695
    }
×
696

697
    DNSPacket l_forwardPacket(p);
×
698
    l_forwardPacket.setID(dns_random_uint16());
×
699
    l_forwardPacket.setRemote(&remote);
×
700
    uint16_t len = htons(l_forwardPacket.getString().length());
×
701
    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
×
702
    buffer.append(l_forwardPacket.getString());
×
703
    if (write(sock, buffer.c_str(), buffer.length()) < 0) {
×
NEW
704
      SLOG(g_log << Logger::Error << ctx.msgPrefix << "Unable to forward update message to " << remote.toStringWithPort() << ", error:" << stringerror() << endl,
×
NEW
705
           ctx.slog->error(Logr::Error, errno, "Update: unable to forward update message", "remote", Logging::Loggable(remote.toStringWithPort())));
×
706
      try {
×
707
        closesocket(sock);
×
708
      }
×
709
      catch (const PDNSException& e) {
×
NEW
710
        SLOG(g_log << Logger::Error << "Error closing primary forwarding socket after write() failed: " << e.reason << endl,
×
NEW
711
             ctx.slog->error(Logr::Error, e.reason, "Update: error closing primary forwarding socket after write() failed"));
×
712
      }
×
713
      continue;
×
714
    }
×
715

716
    int res = waitForData(sock, 10, 0);
×
717
    if (res == 0) {
×
NEW
718
      SLOG(g_log << Logger::Error << ctx.msgPrefix << "Timeout waiting for reply from primary at " << remote.toStringWithPort() << endl,
×
NEW
719
           ctx.slog->info(Logr::Error, "Update: timeout waiting for reply from primary", "remote", Logging::Loggable(remote.toStringWithPort())));
×
720
      try {
×
721
        closesocket(sock);
×
722
      }
×
723
      catch (const PDNSException& e) {
×
NEW
724
        SLOG(g_log << Logger::Error << "Error closing primary forwarding socket after a timeout occurred: " << e.reason << endl,
×
NEW
725
             ctx.slog->error(Logr::Error, e.reason, "Update: error closing primary forwarding socket after a timeout occurred"));
×
726
      }
×
727
      continue;
×
728
    }
×
729
    if (res < 0) {
×
NEW
730
      SLOG(g_log << Logger::Error << ctx.msgPrefix << "Error waiting for answer from primary at " << remote.toStringWithPort() << ", error:" << stringerror() << endl,
×
NEW
731
           ctx.slog->error(Logr::Error, errno, "Update: error waiting for answer from primary", "remote", Logging::Loggable(remote.toStringWithPort())));
×
732
      try {
×
733
        closesocket(sock);
×
734
      }
×
735
      catch (const PDNSException& e) {
×
NEW
736
        SLOG(g_log << Logger::Error << "Error closing primary forwarding socket after an error occurred: " << e.reason << endl,
×
NEW
737
             ctx.slog->error(Logr::Error, e.reason, "Update: error closing primary forwarding socket after an error occurred"));
×
738
      }
×
739
      continue;
×
740
    }
×
741

742
    std::array<unsigned char, 2> lenBuf{};
×
743
    ssize_t recvRes = recv(sock, lenBuf.data(), lenBuf.size(), 0);
×
744
    if (recvRes < 0 || static_cast<size_t>(recvRes) < lenBuf.size()) {
×
NEW
745
      SLOG(g_log << Logger::Error << ctx.msgPrefix << "Could not receive data (length) from primary at " << remote.toStringWithPort() << ", error:" << stringerror() << endl,
×
NEW
746
           ctx.slog->error(Logr::Error, errno, "Update: could not receive data (length) from primary", "remote", Logging::Loggable(remote.toStringWithPort())));
×
747
      try {
×
748
        closesocket(sock);
×
749
      }
×
750
      catch (const PDNSException& e) {
×
NEW
751
        SLOG(g_log << Logger::Error << "Error closing primary forwarding socket after recv() failed: " << e.reason << endl,
×
NEW
752
             ctx.slog->error(Logr::Error, e.reason, "Update: error closing primary forwarding socket after recv() failed"));
×
753
      }
×
754
      continue;
×
755
    }
×
756
    size_t packetLen = lenBuf[0] * 256 + lenBuf[1];
×
757

758
    buffer.resize(packetLen);
×
759
    recvRes = recv(sock, &buffer.at(0), packetLen, 0);
×
760
    if (recvRes < 0) {
×
NEW
761
      SLOG(g_log << Logger::Error << ctx.msgPrefix << "Could not receive data (dnspacket) from primary at " << remote.toStringWithPort() << ", error:" << stringerror() << endl,
×
NEW
762
           ctx.slog->error(Logr::Error, errno, "Update: could not receive data (dnspacket) from primary", "remote", Logging::Loggable(remote.toStringWithPort())));
×
763
      try {
×
764
        closesocket(sock);
×
765
      }
×
766
      catch (const PDNSException& e) {
×
NEW
767
        SLOG(g_log << Logger::Error << "Error closing primary forwarding socket after recv() failed: " << e.reason << endl,
×
NEW
768
             ctx.slog->error(Logr::Error, e.reason, "Update: error closing primary forwarding socket after recv() failed"));
×
769
      }
×
770
      continue;
×
771
    }
×
772
    try {
×
773
      closesocket(sock);
×
774
    }
×
775
    catch (const PDNSException& e) {
×
NEW
776
      SLOG(g_log << Logger::Error << "Error closing primary forwarding socket: " << e.reason << endl,
×
NEW
777
           ctx.slog->error(Logr::Error, e.reason, "Update: error closing primary forwarding socket"));
×
UNCOV
778
    }
×
779

780
    try {
×
781
      MOADNSParser mdp(false, buffer.data(), static_cast<unsigned int>(recvRes));
×
NEW
782
      SLOG(g_log << Logger::Info << ctx.msgPrefix << "Forward update message to " << remote.toStringWithPort() << ", result was RCode " << mdp.d_header.rcode << endl,
×
NEW
783
           ctx.slog->info(Logr::Info, "Update: forwarded update message to primary and received result", "remote", Logging::Loggable(remote.toStringWithPort()), "result", Logging::Loggable(RCode::to_s(mdp.d_header.rcode))));
×
784
      return mdp.d_header.rcode;
×
785
    }
×
786
    catch (...) {
×
NEW
787
      SLOG(g_log << Logger::Error << ctx.msgPrefix << "Failed to parse response packet from primary at " << remote.toStringWithPort() << endl,
×
NEW
788
           ctx.slog->info(Logr::Error, "Update: failed to parse response packet from primary", "remote", Logging::Loggable(remote.toStringWithPort())));
×
789
      continue;
×
790
    }
×
791
  }
×
NEW
792
  SLOG(g_log << Logger::Error << ctx.msgPrefix << "Failed to forward packet to primary(s). Returning ServFail." << endl,
×
NEW
793
       ctx.slog->info(Logr::Error, "Update: failed to forward packet to primary, returning ServFail,"));
×
794
  return RCode::ServFail;
×
795
}
×
796

797
static bool isUpdateAllowed(UeberBackend& UBackend, const updateContext& ctx, DNSPacket& packet)
798
{
2,294✔
799
  // Check permissions - IP based
800
  vector<string> allowedRanges;
2,294✔
801

802
  UBackend.getDomainMetadata(packet.qdomainzone, "ALLOW-DNSUPDATE-FROM", allowedRanges);
2,294✔
803
  if (!::arg()["allow-dnsupdate-from"].empty()) {
2,294!
804
    stringtok(allowedRanges, ::arg()["allow-dnsupdate-from"], ", \t");
2,294✔
805
  }
2,294✔
806

807
  NetmaskGroup nmg;
2,294✔
808
  for (const auto& range : allowedRanges) {
4,588✔
809
    nmg.addMask(range);
4,588✔
810
  }
4,588✔
811

812
  if (!nmg.match(packet.getInnerRemote())) {
2,294!
NEW
813
    SLOG(g_log << Logger::Error << ctx.msgPrefix << "Remote not listed in allow-dnsupdate-from or domainmetadata. Sending REFUSED" << endl,
×
NEW
814
         ctx.slog->info(Logr::Error, "Update: remote not listed in allow-dnsupdates-from or domain metadata, sending Refused"));
×
815
    return false;
×
816
  }
×
817

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

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

853
    if (!validKey) {
×
NEW
854
      SLOG(g_log << Logger::Error << ctx.msgPrefix << "TSIG key (" << inputkey << ") required, but no matching key found in domainmetadata, tried " << tsigKeys.size() << ". Sending REFUSED" << endl,
×
NEW
855
           ctx.slog->info(Logr::Error, "Update: TSIG key required, but no matching key found in domain metadata, sending Refused", "key", Logging::Loggable(inputkey), "keys checked", Logging::Loggable(tsigKeys.size())));
×
856
      return false;
×
857
    }
×
858
  }
×
859
  else if (::arg().mustDo("dnsupdate-require-tsig")) {
2,294!
NEW
860
    SLOG(g_log << Logger::Error << ctx.msgPrefix << "TSIG key required, but domain is not secured with TSIG. Sending REFUSED" << endl,
×
NEW
861
         ctx.slog->info(Logr::Error, "Update: TSIG key required, but domain is not secured with TSIG, sending Refused"));
×
862
    return false;
×
863
  }
×
864

865
  if (tsigKeys.empty() && packet.d_havetsig) {
2,294!
NEW
866
    SLOG(g_log << Logger::Warning << ctx.msgPrefix << "TSIG is provided, but domain is not secured with TSIG. Processing continues" << endl,
×
NEW
867
         ctx.slog->info(Logr::Error, "Update: TSIG key provided, but domain is not secured with TSIG, processing anyway"));
×
UNCOV
868
  }
×
869

870
  return true;
2,294✔
871
}
2,294✔
872

873
static uint8_t updatePrereqCheck323(MOADNSParser& mdp, const updateContext& ctx)
874
{
2,118✔
875
  using rrSetKey_t = pair<DNSName, QType>;
2,118✔
876
  using rrVector_t = vector<DNSResourceRecord>;
2,118✔
877
  using RRsetMap_t = std::map<rrSetKey_t, rrVector_t>;
2,118✔
878
  RRsetMap_t preReqRRsets;
2,118✔
879

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

888
      if (dnsRecord->d_class == QClass::IN) {
308✔
889
        rrSetKey_t key = {dnsRecord->d_name, QType(dnsRecord->d_type)};
198✔
890
        rrVector_t* vec = &preReqRRsets[key];
198✔
891
        vec->push_back(DNSResourceRecord::fromWire(*dnsRecord));
198✔
892
      }
198✔
893
    }
308✔
894
  }
5,314✔
895

896
  if (!preReqRRsets.empty()) {
2,118✔
897
    RRsetMap_t zoneRRsets;
66✔
898
    for (auto& preReqRRset : preReqRRsets) {
66✔
899
      rrSetKey_t rrSet = preReqRRset.first;
66✔
900
      rrVector_t* vec = &preReqRRset.second;
66✔
901

902
      DNSResourceRecord rec;
66✔
903
      ctx.di.backend->lookup(QType(QType::ANY), rrSet.first, ctx.di.id);
66✔
904
      size_t foundRR{0};
66✔
905
      size_t matchRR{0};
66✔
906
      while (ctx.di.backend->get(rec)) {
264✔
907
        if (rec.qtype == rrSet.second) {
198!
908
          foundRR++;
198✔
909
          for (auto& rrItem : *vec) {
594✔
910
            rrItem.ttl = rec.ttl; // The comparison on the next line also compares TTL, so we make them equal because TTL is not used within prerequisite checks.
594✔
911
            if (rrItem == rec) {
594✔
912
              matchRR++;
198✔
913
            }
198✔
914
          }
594✔
915
        }
198✔
916
      }
198✔
917
      if (matchRR != foundRR || foundRR != vec->size()) {
66!
918
        SLOG(g_log << Logger::Error << ctx.msgPrefix << "Failed PreRequisites check (RRs differ), returning NXRRSet" << endl,
44!
919
             ctx.slog->info(Logr::Error, "Update: failed PreRequisites check (RRs differ), returning NXRRSet"));
44✔
920
        return RCode::NXRRSet;
44✔
921
      }
44✔
922
    }
66✔
923
  }
66✔
924
  return RCode::NoError;
2,074✔
925
}
2,118✔
926

927
static uint8_t updateRecords(MOADNSParser& mdp, DNSSECKeeper& dsk, uint& changedRecords, const std::unique_ptr<AuthLua4>& update_policy_lua, DNSPacket& packet, updateContext& ctx)
928
{
2,030✔
929
  vector<const DNSRecord*> cnamesToAdd;
2,030✔
930
  vector<const DNSRecord*> nonCnamesToAdd;
2,030✔
931
  vector<const DNSRecord*> nsRRtoDelete;
2,030✔
932

933
  bool anyRecordProcessed{false};
2,030✔
934
  bool anyRecordAcceptedByLua{false};
2,030✔
935
  for (const auto& answer : mdp.d_answers) {
5,116✔
936
    const DNSRecord* dnsRecord = &answer;
5,116✔
937
    if (dnsRecord->d_place == DNSResourceRecord::AUTHORITY) {
5,116✔
938
      anyRecordProcessed = true;
4,940✔
939
      /* see if it's permitted by policy */
940
      if (update_policy_lua != nullptr) {
4,940!
941
        if (!update_policy_lua->updatePolicy(dnsRecord->d_name, QType(dnsRecord->d_type), ctx.di.zone.operator const DNSName&(), packet)) {
×
NEW
942
          SLOG(g_log << Logger::Warning << ctx.msgPrefix << "Refusing update for " << dnsRecord->d_name << "/" << QType(dnsRecord->d_type).toString() << ": Not permitted by policy" << endl,
×
NEW
943
               ctx.slog->info(Logr::Warning, "Update: refusing record update, not permitted by policy", "name", Logging::Loggable(dnsRecord->d_name), "type", Logging::Loggable(dnsRecord->d_type)));
×
944
          continue;
×
945
        }
×
NEW
946
        SLOG(g_log << Logger::Debug << ctx.msgPrefix << "Accepting update for " << dnsRecord->d_name << "/" << QType(dnsRecord->d_type).toString() << ": Permitted by policy" << endl,
×
NEW
947
             ctx.slog->info(Logr::Debug, "Update: accepting record update, permitted by policy", "name", Logging::Loggable(dnsRecord->d_name), "type", Logging::Loggable(dnsRecord->d_type)));
×
948
        anyRecordAcceptedByLua = true;
×
949
      }
×
950

951
      if (dnsRecord->d_class == QClass::NONE && dnsRecord->d_type == QType::NS && dnsRecord->d_name == ctx.di.zone.operator const DNSName&()) {
4,940!
952
        nsRRtoDelete.push_back(dnsRecord);
66✔
953
      }
66✔
954
      else if (dnsRecord->d_class == QClass::IN && dnsRecord->d_ttl > 0) {
4,874!
955
        if (dnsRecord->d_type == QType::CNAME) {
3,688✔
956
          cnamesToAdd.push_back(dnsRecord);
154✔
957
        }
154✔
958
        else {
3,534✔
959
          nonCnamesToAdd.push_back(dnsRecord);
3,534✔
960
        }
3,534✔
961
      }
3,688✔
962
      else {
1,186✔
963
        changedRecords += performUpdate(dsk, dnsRecord, ctx);
1,186✔
964
      }
1,186✔
965
    }
4,940✔
966
  }
5,116✔
967

968
  if (update_policy_lua != nullptr) {
2,030!
969
    // If the Lua update policy script has been invoked, and has rejected
970
    // everything, better return Refused.
971
    if (anyRecordProcessed && !anyRecordAcceptedByLua) {
×
972
      return RCode::Refused;
×
973
    }
×
974
  }
×
975

976
  for (const auto& resrec : cnamesToAdd) {
2,030✔
977
    DNSResourceRecord rec;
154✔
978
    ctx.di.backend->lookup(QType(QType::ANY), resrec->d_name, ctx.di.id);
154✔
979
    while (ctx.di.backend->get(rec)) {
220✔
980
      if (rec.qtype != QType::CNAME && rec.qtype != QType::ENT && rec.qtype != QType::RRSIG) {
88!
981
        // leave database handle in a consistent state
982
        ctx.di.backend->lookupEnd();
22✔
983
        SLOG(g_log << Logger::Warning << ctx.msgPrefix << "Refusing update for " << resrec->d_name << "/" << QType(resrec->d_type).toString() << ": Data other than CNAME exists for the same name" << endl,
22!
984
             ctx.slog->info(Logr::Warning, "Update: refusing record update, data other than CNAME exists for the same name", "name", Logging::Loggable(resrec->d_name), "type", Logging::Loggable(resrec->d_type)));
22✔
985
        return RCode::Refused;
22✔
986
      }
22✔
987
    }
88✔
988
    changedRecords += performUpdate(dsk, resrec, ctx);
132✔
989
  }
132✔
990
  for (const auto& resrec : nonCnamesToAdd) {
3,534✔
991
    DNSResourceRecord rec;
3,534✔
992
    ctx.di.backend->lookup(QType(QType::CNAME), resrec->d_name, ctx.di.id);
3,534✔
993
    while (ctx.di.backend->get(rec)) {
3,534✔
994
      if (rec.qtype == QType::CNAME && resrec->d_type != QType::RRSIG) {
22!
995
        // leave database handle in a consistent state
996
        ctx.di.backend->lookupEnd();
22✔
997
        SLOG(g_log << Logger::Warning << ctx.msgPrefix << "Refusing update for " << resrec->d_name << "/" << QType(resrec->d_type).toString() << ": CNAME exists for the same name" << endl,
22!
998
             ctx.slog->info(Logr::Warning, "Update: refusing record update, CNAME exists for the same name", "name", Logging::Loggable(resrec->d_name), "type", Logging::Loggable(resrec->d_type)));
22✔
999
        return RCode::Refused;
22✔
1000
      }
22✔
1001
    }
22✔
1002
    changedRecords += performUpdate(dsk, resrec, ctx);
3,512✔
1003
  }
3,512✔
1004

1005
  if (!nsRRtoDelete.empty()) {
1,986✔
1006
    vector<DNSResourceRecord> nsRRInZone;
44✔
1007
    DNSResourceRecord rec;
44✔
1008
    ctx.di.backend->lookup(QType(QType::NS), ctx.di.zone.operator const DNSName&(), ctx.di.id);
44✔
1009
    while (ctx.di.backend->get(rec)) {
132✔
1010
      nsRRInZone.push_back(rec);
88✔
1011
    }
88✔
1012
    if (nsRRInZone.size() > nsRRtoDelete.size()) { // only delete if the NS's we delete are less than what we have in the zone (3.4.2.4)
44✔
1013
      for (auto& inZone : nsRRInZone) {
44✔
1014
        for (auto& resrec : nsRRtoDelete) {
44✔
1015
          if (inZone.getZoneRepresentation() == resrec->getContent()->getZoneRepresentation()) {
44✔
1016
            changedRecords += performUpdate(dsk, resrec, ctx);
22✔
1017
          }
22✔
1018
        }
44✔
1019
      }
44✔
1020
    }
22✔
1021
  }
44✔
1022

1023
  return RCode::NoError;
1,986✔
1024
}
2,008✔
1025

1026
int PacketHandler::processUpdate(DNSPacket& packet)
1027
{
2,294✔
1028
  if (!::arg().mustDo("dnsupdate")) {
2,294!
1029
    return RCode::Refused;
×
1030
  }
×
1031

1032
  updateContext ctx{};
2,294✔
1033
  if (g_slogStructured) {
2,294!
NEW
1034
    ctx.slog = d_slog->withValues("remote", Logging::Loggable(packet.getRemoteString()), "target", Logging::Loggable(packet.qdomainzone), "packet id", Logging::Loggable(packet.d.id));
×
NEW
1035
  }
×
1036
  else {
2,294✔
1037
    ctx.msgPrefix = "UPDATE (" + std::to_string(packet.d.id) + ") from " + packet.getRemoteString() + " for " + packet.qdomainzone.toLogString() + ": ";
2,294✔
1038
  }
2,294✔
1039

1040
  SLOG(g_log << Logger::Info << ctx.msgPrefix << "Processing started." << endl,
2,294!
1041
       ctx.slog->info(Logr::Info, "Update: processing started"));
2,294✔
1042

1043
  // if there is policy, we delegate all checks to it
1044
  if (this->d_update_policy_lua == nullptr) {
2,294!
1045
    if (!isUpdateAllowed(B, ctx, packet)) {
2,294!
1046
      return RCode::Refused;
×
1047
    }
×
1048
  }
2,294✔
1049

1050
  // RFC2136 uses the same DNS Header and Message as defined in RFC1035.
1051
  // This means we can use the MOADNSParser to parse the incoming packet. The result is that we have some different
1052
  // variable names during the use of our MOADNSParser.
1053
  MOADNSParser mdp(false, packet.getString());
2,294✔
1054
  if (mdp.d_header.qdcount != 1) {
2,294!
NEW
1055
    SLOG(g_log << Logger::Warning << ctx.msgPrefix << "Zone Count is not 1, sending FormErr" << endl,
×
NEW
1056
         ctx.slog->info(Logr::Warning, "Update: zone count is not 1. sending FormErr"));
×
1057
    return RCode::FormErr;
×
1058
  }
×
1059

1060
  if (packet.qtype.getCode() != QType::SOA) { // RFC2136 2.3 - ZTYPE must be SOA
2,294!
NEW
1061
    SLOG(g_log << Logger::Warning << ctx.msgPrefix << "Query ZTYPE is not SOA, sending FormErr" << endl,
×
NEW
1062
         ctx.slog->info(Logr::Warning, "Update: query ZTYPE is not SOA, sending FormErr"));
×
1063
    return RCode::FormErr;
×
1064
  }
×
1065

1066
  if (packet.qclass != QClass::IN) {
2,294!
NEW
1067
    SLOG(g_log << Logger::Warning << ctx.msgPrefix << "Class is not IN, sending NotAuth" << endl,
×
NEW
1068
         ctx.slog->info(Logr::Warning, "Update: class is not IN, sending NotAuth"));
×
1069
    return RCode::NotAuth;
×
1070
  }
×
1071

1072
  ctx.di.backend = nullptr;
2,294✔
1073
  if (!B.getDomainInfo(packet.qdomainzone, ctx.di) || (ctx.di.backend == nullptr)) {
2,294!
1074
    SLOG(g_log << Logger::Error << ctx.msgPrefix << "Can't determine backend for domain (or backend does not support DNS update operation)" << endl,
22!
1075
         ctx.slog->info(Logr::Error, "Update: can't determine backend for domain, or backend does not support DNS update operation"));
22✔
1076
    return RCode::NotAuth;
22✔
1077
  }
22✔
1078
  // ctx.di field valid from now on
1079

1080
  if (ctx.di.kind == DomainInfo::Secondary) {
2,272!
1081
    return forwardPacket(B, ctx, packet);
×
1082
  }
×
1083

1084
  // Check if all the records provided are within the zone
1085
  for (const auto& answer : mdp.d_answers) {
5,556✔
1086
    const DNSRecord* dnsRecord = &answer;
5,556✔
1087
    // Skip this check for other field types (like the TSIG -  which is in the additional section)
1088
    // For a TSIG, the label is the dnskey, so it does not pass the endOn validation.
1089
    if (dnsRecord->d_place != DNSResourceRecord::ANSWER && dnsRecord->d_place != DNSResourceRecord::AUTHORITY) {
5,556!
1090
      continue;
×
1091
    }
×
1092

1093
    if (!dnsRecord->d_name.isPartOf(ctx.di.zone)) {
5,556✔
1094
      SLOG(g_log << Logger::Error << ctx.msgPrefix << "Received update/record out of zone, sending NotZone." << endl,
44!
1095
           ctx.slog->info(Logr::Error, "Update: received update/record out of zone, sending NotZone"));
44✔
1096
      return RCode::NotZone;
44✔
1097
    }
44✔
1098
  }
5,556✔
1099

1100
  auto lock = std::scoped_lock(s_rfc2136lock); //TODO: i think this lock can be per zone, not for everything
2,228✔
1101
  SLOG(g_log << Logger::Info << ctx.msgPrefix << "starting transaction." << endl,
2,228!
1102
       ctx.slog->info(Logr::Info, "Update: starting transaction"));
2,228✔
1103
  if (!ctx.di.backend->startTransaction(packet.qdomainzone, UnknownDomainID)) { // Not giving the domain_id means that we do not delete the existing records.
2,228!
NEW
1104
    SLOG(g_log << Logger::Error << ctx.msgPrefix << "Backend does not support transaction. Can't do Update packet." << endl,
×
NEW
1105
         ctx.slog->info(Logr::Error, "Update: backend does not support transaction. Can't process Update packet."));
×
1106
    return RCode::NotImp;
×
1107
  }
×
1108

1109
  // 3.2.1 and 3.2.2 - Prerequisite check
1110
  for (const auto& answer : mdp.d_answers) {
5,424✔
1111
    const DNSRecord* dnsRecord = &answer;
5,424✔
1112
    if (dnsRecord->d_place == DNSResourceRecord::ANSWER) {
5,424✔
1113
      int res = checkUpdatePrerequisites(dnsRecord, &ctx.di);
418✔
1114
      if (res > 0) {
418✔
1115
        SLOG(g_log << Logger::Error << ctx.msgPrefix << "Failed PreRequisites check for " << dnsRecord->d_name << ", returning " << RCode::to_s(res) << endl,
110!
1116
             ctx.slog->info(Logr::Error, "Update: failed PreRequisites check for record", "record", Logging::Loggable(dnsRecord->d_name), "returned value", Logging::Loggable(RCode::to_s(res))));
110✔
1117
        ctx.di.backend->abortTransaction();
110✔
1118
        return res;
110✔
1119
      }
110✔
1120
    }
418✔
1121
  }
5,424✔
1122

1123
  // 3.2.3 - Prerequisite check - this is outside of updatePrerequisitesCheck because we check an RRSet and not the RR.
1124
  if (auto rcode = updatePrereqCheck323(mdp, ctx); rcode != RCode::NoError) {
2,118✔
1125
    ctx.di.backend->abortTransaction();
44✔
1126
    return rcode;
44✔
1127
  }
44✔
1128

1129
  // 3.4 - Prescan & Add/Update/Delete records - is all done within a try block.
1130
  try {
2,074✔
1131
    // 3.4.1 - Prescan section
1132
    for (const auto& answer : mdp.d_answers) {
5,182✔
1133
      const DNSRecord* dnsRecord = &answer;
5,182✔
1134
      if (dnsRecord->d_place == DNSResourceRecord::AUTHORITY) {
5,182✔
1135
        int res = checkUpdatePrescan(dnsRecord);
5,006✔
1136
        if (res > 0) {
5,006✔
1137
          SLOG(g_log << Logger::Error << ctx.msgPrefix << "Failed prescan check, returning " << RCode::to_s(res) << endl,
22!
1138
               ctx.slog->info(Logr::Error, "Update: failed prescan check", "returned value", Logging::Loggable(RCode::to_s(res))));
22✔
1139
          ctx.di.backend->abortTransaction();
22✔
1140
          return res;
22✔
1141
        }
22✔
1142
      }
5,006✔
1143
    }
5,182✔
1144

1145
    ctx.isPresigned = d_dk.isPresigned(ctx.di.zone);
2,052✔
1146
    ctx.narrow = false;
2,052✔
1147
    ctx.haveNSEC3 = d_dk.getNSEC3PARAM(ctx.di.zone, &ctx.ns3pr, &ctx.narrow);
2,052✔
1148
    ctx.updatedSerial = false;
2,052✔
1149
    // all ctx fields valid from now on
1150

1151
    string soaEditSetting;
2,052✔
1152
    d_dk.getSoaEdit(ctx.di.zone, soaEditSetting);
2,052✔
1153

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

1158
    // Another special case is the addition of both a CNAME and a non-CNAME for the same name (#6270)
1159
    // TODO: convert to use Check::checkRRSet() for consistency
1160
    set<DNSName> cn; // NOLINT(readability-identifier-length)
2,052✔
1161
    set<DNSName> nocn;
2,052✔
1162
    for (const auto& rr : mdp.d_answers) { // NOLINT(readability-identifier-length)
5,160✔
1163
      if (rr.d_place == DNSResourceRecord::AUTHORITY && rr.d_class == QClass::IN && rr.d_ttl > 0) {
5,160!
1164
        // Addition
1165
        if (rr.d_type == QType::CNAME) {
3,732✔
1166
          cn.insert(rr.d_name);
176✔
1167
        }
176✔
1168
        else if (rr.d_type != QType::RRSIG) {
3,556!
1169
          nocn.insert(rr.d_name);
3,556✔
1170
        }
3,556✔
1171
      }
3,732✔
1172
    }
5,160✔
1173
    for (auto const& n : cn) { // NOLINT(readability-identifier-length)
2,052✔
1174
      if (nocn.count(n) > 0) {
176✔
1175
        SLOG(g_log << Logger::Error << ctx.msgPrefix << "Refusing update, found CNAME and non-CNAME addition" << endl,
22!
1176
             ctx.slog->info(Logr::Error, "Update: found CNAME and non-CNAME addition, refusing update"));
22✔
1177
        ctx.di.backend->abortTransaction();
22✔
1178
        return RCode::FormErr;
22✔
1179
      }
22✔
1180
    }
176✔
1181

1182
    uint changedRecords = 0;
2,030✔
1183
    if (auto rcode = updateRecords(mdp, d_dk, changedRecords, d_update_policy_lua, packet, ctx); rcode != RCode::NoError) {
2,030✔
1184
      ctx.di.backend->abortTransaction();
44✔
1185
      return rcode;
44✔
1186
    }
44✔
1187

1188
    // Section 3.6 - Update the SOA serial - outside of performUpdate because we do a SOA update for the complete update message
1189
    if (changedRecords != 0 && !ctx.updatedSerial) {
1,986✔
1190
      increaseSerial(soaEditSetting, ctx);
1,766✔
1191
      changedRecords++;
1,766✔
1192
    }
1,766✔
1193

1194
    if (changedRecords != 0) {
1,986✔
1195
      if (!ctx.di.backend->commitTransaction()) {
1,810!
NEW
1196
        SLOG(g_log << Logger::Error << ctx.msgPrefix << "Failed to commit updates!" << endl,
×
NEW
1197
             ctx.slog->info(Logr::Error, "Update: failed to commit updates!"));
×
1198
        return RCode::ServFail;
×
1199
      }
×
1200

1201
      S.deposit("dnsupdate-changes", static_cast<int>(changedRecords));
1,810✔
1202

1203
      DNSSECKeeper::clearMetaCache(ctx.di.zone);
1,810✔
1204
      // Purge the records!
1205
      purgeAuthCaches(ctx.di.zone.operator const DNSName&().toString() + "$");
1,810✔
1206

1207
      // Notify secondaries
1208
      if (ctx.di.kind == DomainInfo::Primary) {
1,810!
1209
        vector<string> notify;
1,810✔
1210
        B.getDomainMetadata(packet.qdomainzone, "NOTIFY-DNSUPDATE", notify);
1,810✔
1211
        if (!notify.empty() && notify.front() == "1") {
1,810!
1212
          Communicator.notifyDomain(ctx.di.zone, &B);
×
1213
        }
×
1214
      }
1,810✔
1215

1216
      SLOG(g_log << Logger::Info << ctx.msgPrefix << "Update completed, " << changedRecords << " changed records committed." << endl,
1,810!
1217
           ctx.slog->info(Logr::Info, "Update: completed", "number of changed records", Logging::Loggable(changedRecords)));
1,810✔
1218
    }
1,810✔
1219
    else {
176✔
1220
      //No change, no commit, we perform abort() because some backends might like this more.
1221
      SLOG (g_log << Logger::Info << ctx.msgPrefix << "Update completed, 0 changes, rolling back." << endl,
176!
1222
            ctx.slog->info(Logr::Info, "Update: completed, no changes, rolling back"));
176✔
1223
      ctx.di.backend->abortTransaction();
176✔
1224
    }
176✔
1225
    return RCode::NoError; //rfc 2136 3.4.2.5
1,986✔
1226
  }
1,986✔
1227
  catch (SSqlException& e) {
2,074✔
NEW
1228
    SLOG(g_log << Logger::Error << ctx.msgPrefix << "Caught SSqlException: " << e.txtReason() << "; Sending ServFail!" << endl,
×
NEW
1229
         ctx.slog->error(Logr::Error, e.txtReason(), "Update: caught SSqlException, sending ServFail"));
×
1230
    ctx.di.backend->abortTransaction();
×
1231
    return RCode::ServFail;
×
1232
  }
×
1233
  catch (DBException& e) {
2,074✔
NEW
1234
    SLOG(g_log << Logger::Error << ctx.msgPrefix << "Caught DBException: " << e.reason << "; Sending ServFail!" << endl,
×
NEW
1235
         ctx.slog->error(Logr::Error, e.reason, "Update: caught DBException, sending ServFail"));
×
1236
    ctx.di.backend->abortTransaction();
×
1237
    return RCode::ServFail;
×
1238
  }
×
1239
  catch (PDNSException& e) {
2,074✔
NEW
1240
    SLOG(g_log << Logger::Error << ctx.msgPrefix << "Caught PDNSException: " << e.reason << "; Sending ServFail!" << endl,
×
NEW
1241
         ctx.slog->error(Logr::Error, e.reason, "Update: caught PDNSException, sending ServFail"));
×
1242
    ctx.di.backend->abortTransaction();
×
1243
    return RCode::ServFail;
×
1244
  }
×
1245
  catch (std::exception& e) {
2,074✔
NEW
1246
    SLOG(g_log << Logger::Error << ctx.msgPrefix << "Caught std:exception: " << e.what() << "; Sending ServFail!" << endl,
×
NEW
1247
         ctx.slog->error(Logr::Error, e.what(), "Update: caught std::exception, sending ServFail"));
×
1248
    ctx.di.backend->abortTransaction();
×
1249
    return RCode::ServFail;
×
1250
  }
×
1251
  catch (...) {
2,074✔
NEW
1252
    SLOG(g_log << Logger::Error << ctx.msgPrefix << "Caught unknown exception when performing update. Sending ServFail!" << endl,
×
NEW
1253
         ctx.slog->info(Logr::Error, "Update: caught unknown exception, sending ServFail"));
×
1254
    ctx.di.backend->abortTransaction();
×
1255
    return RCode::ServFail;
×
1256
  }
×
1257
}
2,074✔
1258

1259
static void increaseSerial(const string& soaEditSetting, const updateContext& ctx)
1260
{
1,766✔
1261
  SOAData sd; // NOLINT(readability-identifier-length)
1,766✔
1262
  if (!ctx.di.backend->getSOA(ctx.di.zone, ctx.di.id, sd)) {
1,766!
1263
    throw PDNSException("SOA-Serial update failed because there was no SOA. Wowie.");
×
1264
  }
×
1265

1266
  uint32_t oldSerial = sd.serial;
1,766✔
1267

1268
  vector<string> soaEdit2136Setting;
1,766✔
1269
  ctx.di.backend->getDomainMetadata(ctx.di.zone, "SOA-EDIT-DNSUPDATE", soaEdit2136Setting);
1,766✔
1270
  string soaEdit2136 = "DEFAULT";
1,766✔
1271
  string soaEdit;
1,766✔
1272
  if (!soaEdit2136Setting.empty()) {
1,766!
1273
    soaEdit2136 = soaEdit2136Setting[0];
×
1274
    if (pdns_iequals(soaEdit2136, "SOA-EDIT") || pdns_iequals(soaEdit2136, "SOA-EDIT-INCREASE")) {
×
1275
      if (soaEditSetting.empty()) {
×
NEW
1276
        SLOG(g_log << Logger::Error << ctx.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,
×
NEW
1277
             ctx.slog->info(Logr::Error, "Update: SOA-EDIT-DNSUPDATE set, but SOA-EDIT not set, using DEFAULT for SOA-EDIT-DNSUPDATE", "SOA-EDIT-DNSUPDATE", Logging::Loggable(soaEdit2136)));
×
1278
        soaEdit2136 = "DEFAULT";
×
1279
      }
×
1280
      else {
×
1281
        soaEdit = soaEditSetting;
×
1282
      }
×
1283
    }
×
1284
  }
×
1285

1286
  DNSResourceRecord rr; // NOLINT(readability-identifier-length)
1,766✔
1287
  if (makeIncreasedSOARecord(sd, soaEdit2136, soaEdit, rr)) {
1,766!
1288
    ctx.di.backend->replaceRRSet(ctx.di.id, rr.qname, rr.qtype, vector<DNSResourceRecord>(1, rr));
1,766✔
1289
    SLOG(g_log << Logger::Notice << ctx.msgPrefix << "Increasing SOA serial (" << oldSerial << " -> " << sd.serial << ")" << endl,
1,766!
1290
         ctx.slog->info(Logr::Notice, "Update: increasing SOA serial", "old serial", Logging::Loggable(oldSerial), "new serial", Logging::Loggable(sd.serial)));
1,766✔
1291

1292
    //Correct ordername + auth flag
1293
    DNSName ordername = computeOrdername(ctx, rr.qname);
1,766✔
1294
    ctx.di.backend->updateDNSSECOrderNameAndAuth(ctx.di.id, rr.qname, ordername, true, QType::ANY, ctx.haveNSEC3 && !ctx.narrow);
1,766✔
1295
  }
1,766✔
1296
}
1,766✔
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