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

PowerDNS / pdns / 17235120617

26 Aug 2025 10:17AM UTC coverage: 65.959% (-0.02%) from 65.977%
17235120617

Pull #16016

github

web-flow
Merge d1e0ec6fc into 9eeac00a7
Pull Request #16016: auth: random doc nits

42117 of 92446 branches covered (45.56%)

Branch coverage included in aggregate %.

128034 of 165518 relevant lines covered (77.35%)

5925196.8 hits per line

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

62.41
/modules/bindbackend/bindbackend2.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 <cerrno>
27
#include <string>
28
#include <set>
29
#include <sys/types.h>
30
#include <sys/stat.h>
31
#include <unistd.h>
32
#include <fstream>
33
#include <fcntl.h>
34
#include <sstream>
35
#include <boost/algorithm/string.hpp>
36
#include <system_error>
37
#include <unordered_map>
38
#include <unordered_set>
39

40
#include "pdns/dnsseckeeper.hh"
41
#include "pdns/dnssecinfra.hh"
42
#include "pdns/base32.hh"
43
#include "pdns/namespaces.hh"
44
#include "pdns/dns.hh"
45
#include "pdns/dnsbackend.hh"
46
#include "bindbackend2.hh"
47
#include "pdns/dnspacket.hh"
48
#include "pdns/zoneparser-tng.hh"
49
#include "pdns/bindparserclasses.hh"
50
#include "pdns/logger.hh"
51
#include "pdns/arguments.hh"
52
#include "pdns/qtype.hh"
53
#include "pdns/misc.hh"
54
#include "pdns/dynlistener.hh"
55
#include "pdns/lock.hh"
56
#include "pdns/auth-zonecache.hh"
57
#include "pdns/auth-caches.hh"
58

59
/*
60
   All instances of this backend share one s_state, which is indexed by zone name and zone id.
61
   The s_state is protected by a read/write lock, and the goal it to only interact with it briefly.
62
   When a query comes in, we take a read lock and COPY the best zone to answer from s_state (BB2DomainInfo object)
63
   All answers are served from this copy.
64

65
   To interact with s_state, use safeGetBBDomainInfo (search on name or id), safePutBBDomainInfo (to update)
66
   or safeRemoveBBDomainInfo. These all lock as they should.
67

68
   Several functions need to traverse s_state to get data for the rest of PowerDNS. When doing so,
69
   you need to manually take the lock (read).
70

71
   Parsing zones happens with parseZone(), which fills a BB2DomainInfo object. This can then be stored with safePutBBDomainInfo.
72

73
   Finally, the BB2DomainInfo contains all records as a LookButDontTouch object. This makes sure you only look, but don't touch, since
74
   the records might be in use in other places.
75
*/
76

77
SharedLockGuarded<Bind2Backend::state_t> Bind2Backend::s_state;
78
int Bind2Backend::s_first = 1;
79
bool Bind2Backend::s_ignore_broken_records = false;
80

81
std::mutex Bind2Backend::s_autosecondary_config_lock; // protects writes to config file
82
std::mutex Bind2Backend::s_startup_lock;
83
string Bind2Backend::s_binddirectory;
84

85
BB2DomainInfo::BB2DomainInfo()
86
{
15,496✔
87
  d_loaded = false;
15,496✔
88
  d_lastcheck = 0;
15,496✔
89
  d_checknow = false;
15,496✔
90
  d_status = "Unknown";
15,496✔
91
}
15,496✔
92

93
void BB2DomainInfo::setCheckInterval(time_t seconds)
94
{
2,665✔
95
  d_checkinterval = seconds;
2,665✔
96
}
2,665✔
97

98
bool BB2DomainInfo::current()
99
{
4,127✔
100
  if (d_checknow) {
4,127✔
101
    return false;
6✔
102
  }
6✔
103

104
  if (!d_checkinterval)
4,121!
105
    return true;
4,121✔
106

107
  if (time(nullptr) - d_lastcheck < d_checkinterval)
×
108
    return true;
×
109

110
  if (d_filename.empty())
×
111
    return true;
×
112

113
  return (getCtime() == d_ctime);
×
114
}
×
115

116
time_t BB2DomainInfo::getCtime()
117
{
×
118
  struct stat buf;
×
119

120
  if (d_filename.empty() || stat(d_filename.c_str(), &buf) < 0)
×
121
    return 0;
×
122
  d_lastcheck = time(nullptr);
×
123
  return buf.st_ctime;
×
124
}
×
125

126
void BB2DomainInfo::setCtime()
127
{
2,671✔
128
  struct stat buf;
2,671✔
129
  if (stat(d_filename.c_str(), &buf) < 0)
2,671!
130
    return;
×
131
  d_ctime = buf.st_ctime;
2,671✔
132
}
2,671✔
133

134
// NOLINTNEXTLINE(readability-identifier-length)
135
bool Bind2Backend::safeGetBBDomainInfo(domainid_t id, BB2DomainInfo* bbd)
136
{
11,199✔
137
  auto state = s_state.read_lock();
11,199✔
138
  state_t::const_iterator iter = state->find(id);
11,199✔
139
  if (iter == state->end()) {
11,199!
140
    return false;
×
141
  }
×
142
  *bbd = *iter;
11,199✔
143
  return true;
11,199✔
144
}
11,199✔
145

146
bool Bind2Backend::safeGetBBDomainInfo(const ZoneName& name, BB2DomainInfo* bbd)
147
{
4,300✔
148
  auto state = s_state.read_lock();
4,300✔
149
  const auto& nameindex = boost::multi_index::get<NameTag>(*state);
4,300✔
150
  auto iter = nameindex.find(name);
4,300✔
151
  if (iter == nameindex.end()) {
4,300✔
152
    return false;
3,253✔
153
  }
3,253✔
154
  *bbd = *iter;
1,047✔
155
  return true;
1,047✔
156
}
4,300✔
157

158
bool Bind2Backend::safeRemoveBBDomainInfo(const ZoneName& name)
159
{
×
160
  auto state = s_state.write_lock();
×
161
  using nameindex_t = state_t::index<NameTag>::type;
×
162
  nameindex_t& nameindex = boost::multi_index::get<NameTag>(*state);
×
163

164
  nameindex_t::iterator iter = nameindex.find(name);
×
165
  if (iter == nameindex.end()) {
×
166
    return false;
×
167
  }
×
168
  nameindex.erase(iter);
×
169
  return true;
×
170
}
×
171

172
void Bind2Backend::safePutBBDomainInfo(const BB2DomainInfo& bbd)
173
{
2,815✔
174
  auto state = s_state.write_lock();
2,815✔
175
  replacing_insert(*state, bbd);
2,815✔
176
}
2,815✔
177

178
// NOLINTNEXTLINE(readability-identifier-length)
179
void Bind2Backend::setNotified(domainid_t id, uint32_t serial)
180
{
×
181
  BB2DomainInfo bbd;
×
182
  if (!safeGetBBDomainInfo(id, &bbd))
×
183
    return;
×
184
  bbd.d_lastnotified = serial;
×
185
  safePutBBDomainInfo(bbd);
×
186
}
×
187

188
// NOLINTNEXTLINE(readability-identifier-length)
189
void Bind2Backend::setLastCheck(domainid_t domain_id, time_t lastcheck)
190
{
72✔
191
  BB2DomainInfo bbd;
72✔
192
  if (safeGetBBDomainInfo(domain_id, &bbd)) {
72!
193
    bbd.d_lastcheck = lastcheck;
72✔
194
    safePutBBDomainInfo(bbd);
72✔
195
  }
72✔
196
}
72✔
197

198
void Bind2Backend::setStale(domainid_t domain_id)
199
{
×
200
  Bind2Backend::setLastCheck(domain_id, 0);
×
201
}
×
202

203
void Bind2Backend::setFresh(domainid_t domain_id)
204
{
72✔
205
  Bind2Backend::setLastCheck(domain_id, time(nullptr));
72✔
206
}
72✔
207

208
bool Bind2Backend::startTransaction(const ZoneName& qname, domainid_t domainId)
209
{
221✔
210
  if (domainId == UnknownDomainID) {
221✔
211
    d_transaction_tmpname.clear();
149✔
212
    d_transaction_id = UnknownDomainID;
149✔
213
    // No support for domain contents deletion
214
    return false;
149✔
215
  }
149✔
216
  if (domainId == 0) {
72!
217
    throw DBException("domain_id 0 is invalid for this backend.");
×
218
  }
×
219

220
  d_transaction_id = domainId;
72✔
221
  d_transaction_qname = qname;
72✔
222
  BB2DomainInfo bbd;
72✔
223
  if (safeGetBBDomainInfo(domainId, &bbd)) {
72!
224
    d_transaction_tmpname = bbd.d_filename + "XXXXXX";
72✔
225
    int fd = mkstemp(&d_transaction_tmpname.at(0));
72✔
226
    if (fd == -1) {
72!
227
      throw DBException("Unable to create a unique temporary zonefile '" + d_transaction_tmpname + "': " + stringerror());
×
228
    }
×
229

230
    d_of = std::make_unique<ofstream>(d_transaction_tmpname);
72✔
231
    if (!*d_of) {
72!
232
      unlink(d_transaction_tmpname.c_str());
×
233
      close(fd);
×
234
      fd = -1;
×
235
      d_of.reset();
×
236
      throw DBException("Unable to open temporary zonefile '" + d_transaction_tmpname + "': " + stringerror());
×
237
    }
×
238
    close(fd);
72✔
239
    fd = -1;
72✔
240

241
    *d_of << "; Written by PowerDNS, don't edit!" << endl;
72✔
242
    *d_of << "; Zone '" << bbd.d_name << "' retrieved from primary " << endl
72✔
243
          << "; at " << nowTime() << endl; // insert primary info here again
72✔
244

245
    return true;
72✔
246
  }
72✔
247
  return false;
×
248
}
72✔
249

250
bool Bind2Backend::commitTransaction()
251
{
221✔
252
  // d_transaction_id is only set to a valid domain id if we are actually
253
  // setting up a replacement zone file with the updated data.
254
  if (d_transaction_id == UnknownDomainID) {
221✔
255
    return false;
149✔
256
  }
149✔
257
  d_of.reset();
72✔
258

259
  BB2DomainInfo bbd;
72✔
260
  if (safeGetBBDomainInfo(d_transaction_id, &bbd)) {
72!
261
    if (rename(d_transaction_tmpname.c_str(), bbd.d_filename.c_str()) < 0)
72!
262
      throw DBException("Unable to commit (rename to: '" + bbd.d_filename + "') AXFRed zone: " + stringerror());
×
263
    queueReloadAndStore(bbd.d_id);
72✔
264
  }
72✔
265

266
  d_transaction_id = UnknownDomainID;
72✔
267

268
  return true;
72✔
269
}
72✔
270

271
bool Bind2Backend::abortTransaction()
272
{
×
273
  // d_transaction_id is only set to a valid domain id if we are actually
274
  // setting up a replacement zone file with the updated data.
275
  if (d_transaction_id != UnknownDomainID) {
×
276
    unlink(d_transaction_tmpname.c_str());
×
277
    d_of.reset();
×
278
    d_transaction_id = UnknownDomainID;
×
279
  }
×
280

281
  return true;
×
282
}
×
283

284
static bool ciEqual(const string& lhs, const string& rhs)
285
{
432✔
286
  if (lhs.size() != rhs.size()) {
432✔
287
    return false;
408✔
288
  }
408✔
289

290
  string::size_type pos = 0;
24✔
291
  const string::size_type epos = lhs.size();
24✔
292
  for (; pos < epos; ++pos) {
384✔
293
    if (dns_tolower(lhs[pos]) != dns_tolower(rhs[pos])) {
368✔
294
      return false;
8✔
295
    }
8✔
296
  }
368✔
297
  return true;
16✔
298
}
24✔
299

300
/** does domain end on suffix? Is smart about "wwwds9a.nl" "ds9a.nl" not matching */
301
static bool endsOn(const string& domain, const string& suffix)
302
{
432✔
303
  if (suffix.empty() || ciEqual(domain, suffix)) {
432!
304
    return true;
16✔
305
  }
16✔
306

307
  if (domain.size() <= suffix.size()) {
416✔
308
    return false;
52✔
309
  }
52✔
310

311
  string::size_type dpos = domain.size() - suffix.size() - 1;
364✔
312
  string::size_type spos = 0;
364✔
313

314
  if (domain[dpos++] != '.') {
364✔
315
    return false;
88✔
316
  }
88✔
317

318
  for (; dpos < domain.size(); ++dpos, ++spos) {
3,928✔
319
    if (dns_tolower(domain[dpos]) != dns_tolower(suffix[spos])) {
3,664✔
320
      return false;
12✔
321
    }
12✔
322
  }
3,664✔
323

324
  return true;
264✔
325
}
276✔
326

327
/** strips a domain suffix from a domain, returns true if it stripped */
328
static bool stripDomainSuffix(string* qname, const ZoneName& zonename)
329
{
432✔
330
  std::string domain = zonename.operator const DNSName&().toString();
432✔
331

332
  if (!endsOn(*qname, domain)) {
432✔
333
    return false;
152✔
334
  }
152✔
335

336
  if (toLower(*qname) == toLower(domain)) {
280✔
337
    *qname = "@";
16✔
338
  }
16✔
339
  else {
264✔
340
    if ((*qname)[qname->size() - domain.size() - 1] != '.') {
264!
341
      return false;
×
342
    }
×
343

344
    qname->resize(qname->size() - domain.size() - 1);
264✔
345
  }
264✔
346
  return true;
280✔
347
}
280✔
348

349
bool Bind2Backend::feedRecord(const DNSResourceRecord& rr, const DNSName& /* ordername */, bool /* ordernameIsNSEC3 */)
350
{
202,785✔
351
  if (d_transaction_id == UnknownDomainID) {
202,785!
352
    throw DBException("Bind2Backend::feedRecord() called outside of transaction");
×
353
  }
×
354

355
  string qname;
202,785✔
356
  if (d_transaction_qname.empty()) {
202,785!
357
    qname = rr.qname.toString();
×
358
  }
×
359
  else if (rr.qname.isPartOf(d_transaction_qname)) {
202,785!
360
    if (rr.qname == d_transaction_qname.operator const DNSName&()) {
202,785✔
361
      qname = "@";
633✔
362
    }
633✔
363
    else {
202,152✔
364
      DNSName relName = rr.qname.makeRelative(d_transaction_qname);
202,152✔
365
      qname = relName.toStringNoDot();
202,152✔
366
    }
202,152✔
367
  }
202,785✔
368
  else {
×
369
    throw DBException("out-of-zone data '" + rr.qname.toLogString() + "' during AXFR of zone '" + d_transaction_qname.toLogString() + "'");
×
370
  }
×
371

372
  shared_ptr<DNSRecordContent> drc(DNSRecordContent::make(rr.qtype.getCode(), QClass::IN, rr.content));
202,785✔
373
  string content = drc->getZoneRepresentation();
202,785✔
374

375
  // SOA needs stripping too! XXX FIXME - also, this should not be here I think
376
  switch (rr.qtype.getCode()) {
202,785✔
377
  case QType::MX:
56✔
378
  case QType::SRV:
76✔
379
  case QType::CNAME:
208✔
380
  case QType::DNAME:
212✔
381
  case QType::NS:
432✔
382
    stripDomainSuffix(&content, d_transaction_qname);
432✔
383
    // fallthrough
384
  default:
202,785✔
385
    if (d_of && *d_of) {
202,785!
386
      *d_of << qname << "\t" << rr.ttl << "\t" << rr.qtype.toString() << "\t" << content << endl;
202,785✔
387
    }
202,785✔
388
  }
202,785✔
389
  return true;
202,785✔
390
}
202,785✔
391

392
void Bind2Backend::getUpdatedPrimaries(vector<DomainInfo>& changedDomains, std::unordered_set<DNSName>& /* catalogs */, CatalogHashMap& /* catalogHashes */)
393
{
×
394
  vector<DomainInfo> consider;
×
395
  {
×
396
    auto state = s_state.read_lock();
×
397

398
    for (const auto& i : *state) {
×
399
      if (i.d_kind != DomainInfo::Primary && this->alsoNotify.empty() && i.d_also_notify.empty())
×
400
        continue;
×
401

402
      DomainInfo di;
×
403
      di.id = i.d_id;
×
404
      di.zone = i.d_name;
×
405
      di.last_check = i.d_lastcheck;
×
406
      di.notified_serial = i.d_lastnotified;
×
407
      di.backend = this;
×
408
      di.kind = DomainInfo::Primary;
×
409
      consider.push_back(std::move(di));
×
410
    }
×
411
  }
×
412

413
  SOAData soadata;
×
414
  for (DomainInfo& di : consider) {
×
415
    soadata.serial = 0;
×
416
    try {
×
417
      this->getSOA(di.zone, di.id, soadata); // we might not *have* a SOA yet, but this might trigger a load of it
×
418
    }
×
419
    catch (...) {
×
420
      continue;
×
421
    }
×
422
    if (di.notified_serial != soadata.serial) {
×
423
      BB2DomainInfo bbd;
×
424
      if (safeGetBBDomainInfo(di.id, &bbd)) {
×
425
        bbd.d_lastnotified = soadata.serial;
×
426
        safePutBBDomainInfo(bbd);
×
427
      }
×
428
      if (di.notified_serial) { // don't do notification storm on startup
×
429
        di.serial = soadata.serial;
×
430
        changedDomains.push_back(std::move(di));
×
431
      }
×
432
    }
×
433
  }
×
434
}
×
435

436
void Bind2Backend::getAllDomains(vector<DomainInfo>* domains, bool getSerial, bool /* include_disabled */)
437
{
98✔
438
  SOAData soadata;
98✔
439

440
  // prevent deadlock by using getSOA() later on
441
  {
98✔
442
    auto state = s_state.read_lock();
98✔
443
    domains->reserve(state->size());
98✔
444

445
    for (const auto& i : *state) {
258✔
446
      DomainInfo di;
185✔
447
      di.id = i.d_id;
185✔
448
      di.zone = i.d_name;
185✔
449
      di.last_check = i.d_lastcheck;
185✔
450
      di.kind = i.d_kind;
185✔
451
      di.primaries = i.d_primaries;
185✔
452
      di.backend = this;
185✔
453
      domains->push_back(std::move(di));
185✔
454
    };
185✔
455
  }
98✔
456

457
  if (getSerial) {
98✔
458
    for (DomainInfo& di : *domains) {
1,338✔
459
      // do not corrupt di if domain supplied by another backend.
460
      if (di.backend != this)
1,338!
461
        continue;
1,338✔
462
      try {
×
463
        this->getSOA(di.zone, di.id, soadata);
×
464
      }
×
465
      catch (...) {
×
466
        continue;
×
467
      }
×
468
      di.serial = soadata.serial;
×
469
    }
×
470
  }
42✔
471
}
98✔
472

473
void Bind2Backend::getUnfreshSecondaryInfos(vector<DomainInfo>* unfreshDomains)
474
{
8✔
475
  vector<DomainInfo> domains;
8✔
476
  {
8✔
477
    auto state = s_state.read_lock();
8✔
478
    domains.reserve(state->size());
8✔
479
    for (const auto& i : *state) {
72✔
480
      if (i.d_kind != DomainInfo::Secondary)
72!
481
        continue;
×
482
      DomainInfo sd;
72✔
483
      sd.id = i.d_id;
72✔
484
      sd.zone = i.d_name;
72✔
485
      sd.primaries = i.d_primaries;
72✔
486
      sd.last_check = i.d_lastcheck;
72✔
487
      sd.backend = this;
72✔
488
      sd.kind = DomainInfo::Secondary;
72✔
489
      domains.push_back(std::move(sd));
72✔
490
    }
72✔
491
  }
8✔
492
  unfreshDomains->reserve(domains.size());
8✔
493

494
  for (DomainInfo& sd : domains) {
72✔
495
    SOAData soadata;
72✔
496
    soadata.refresh = 0;
72✔
497
    soadata.serial = 0;
72✔
498
    try {
72✔
499
      getSOA(sd.zone, sd.id, soadata); // we might not *have* a SOA yet
72✔
500
    }
72✔
501
    catch (...) {
72✔
502
    }
72✔
503
    sd.serial = soadata.serial;
72✔
504
    // coverity[store_truncates_time_t]
505
    if (sd.last_check + soadata.refresh < (unsigned int)time(nullptr))
72!
506
      unfreshDomains->push_back(std::move(sd));
72✔
507
  }
72✔
508
}
8✔
509

510
bool Bind2Backend::getDomainInfo(const ZoneName& domain, DomainInfo& info, bool getSerial)
511
{
1,006✔
512
  BB2DomainInfo bbd;
1,006✔
513
  if (!safeGetBBDomainInfo(domain, &bbd))
1,006✔
514
    return false;
561✔
515

516
  info.id = bbd.d_id;
445✔
517
  info.zone = domain;
445✔
518
  info.primaries = bbd.d_primaries;
445✔
519
  info.last_check = bbd.d_lastcheck;
445✔
520
  info.backend = this;
445✔
521
  info.kind = bbd.d_kind;
445✔
522
  info.serial = 0;
445✔
523
  if (getSerial) {
445✔
524
    try {
135✔
525
      SOAData sd;
135✔
526
      sd.serial = 0;
135✔
527

528
      getSOA(bbd.d_name, bbd.d_id, sd); // we might not *have* a SOA yet
135✔
529
      info.serial = sd.serial;
135✔
530
    }
135✔
531
    catch (...) {
135✔
532
    }
72✔
533
  }
135✔
534

535
  return true;
445✔
536
}
445✔
537

538
void Bind2Backend::alsoNotifies(const ZoneName& domain, set<string>* ips)
539
{
4✔
540
  // combine global list with local list
541
  for (const auto& i : this->alsoNotify) {
4!
542
    (*ips).insert(i);
×
543
  }
×
544
  // check metadata too if available
545
  vector<string> meta;
4✔
546
  if (getDomainMetadata(domain, "ALSO-NOTIFY", meta)) {
4!
547
    for (const auto& str : meta) {
×
548
      (*ips).insert(str);
×
549
    }
×
550
  }
×
551
  auto state = s_state.read_lock();
4✔
552
  for (const auto& i : *state) {
4!
553
    if (i.d_name == domain) {
×
554
      for (const auto& it : i.d_also_notify) {
×
555
        (*ips).insert(it);
×
556
      }
×
557
      return;
×
558
    }
×
559
  }
×
560
}
4✔
561

562
// only parses, does NOT add to s_state!
563
void Bind2Backend::parseZoneFile(BB2DomainInfo* bbd)
564
{
2,737✔
565
  NSEC3PARAMRecordContent ns3pr;
2,737✔
566
  bool nsec3zone = false;
2,737✔
567
  if (d_hybrid) {
2,737!
568
    DNSSECKeeper dk;
×
569
    nsec3zone = dk.getNSEC3PARAM(bbd->d_name, &ns3pr);
×
570
  }
×
571
  else
2,737✔
572
    nsec3zone = getNSEC3PARAMuncached(bbd->d_name, &ns3pr);
2,737✔
573

574
  auto records = std::make_shared<recordstorage_t>();
2,737✔
575
  ZoneParserTNG zpt(bbd->d_filename, bbd->d_name, s_binddirectory, d_upgradeContent);
2,737✔
576
  zpt.setMaxGenerateSteps(::arg().asNum("max-generate-steps"));
2,737✔
577
  zpt.setMaxIncludes(::arg().asNum("max-include-depth"));
2,737✔
578
  DNSResourceRecord rr;
2,737✔
579
  string hashed;
2,737✔
580
  while (zpt.get(rr)) {
3,277,123✔
581
    if (rr.qtype.getCode() == QType::NSEC || rr.qtype.getCode() == QType::NSEC3 || rr.qtype.getCode() == QType::NSEC3PARAM)
3,274,386!
582
      continue; // we synthesise NSECs on demand
×
583

584
    insertRecord(records, bbd->d_name, rr.qname, rr.qtype, rr.content, rr.ttl, "");
3,274,386✔
585
  }
3,274,386✔
586
  fixupOrderAndAuth(records, bbd->d_name, nsec3zone, ns3pr);
2,737✔
587
  doEmptyNonTerminals(records, bbd->d_name, nsec3zone, ns3pr);
2,737✔
588
  bbd->setCtime();
2,737✔
589
  bbd->d_loaded = true;
2,737✔
590
  bbd->d_checknow = false;
2,737✔
591
  bbd->d_status = "parsed into memory at " + nowTime();
2,737✔
592
  bbd->d_records = LookButDontTouch<recordstorage_t>(std::move(records));
2,737✔
593
  bbd->d_nsec3zone = nsec3zone;
2,737✔
594
  bbd->d_nsec3param = std::move(ns3pr);
2,737✔
595
}
2,737✔
596

597
/** THIS IS AN INTERNAL FUNCTION! It does moadnsparser prio impedance matching
598
    Much of the complication is due to the efforts to benefit from std::string reference counting copy on write semantics */
599
void Bind2Backend::insertRecord(std::shared_ptr<recordstorage_t>& records, const ZoneName& zoneName, const DNSName& qname, const QType& qtype, const string& content, int ttl, const std::string& hashed, const bool* auth)
600
{
3,279,002✔
601
  Bind2DNSRecord bdr;
3,279,002✔
602
  bdr.qname = qname;
3,279,002✔
603

604
  if (zoneName.empty())
3,279,002!
605
    ;
×
606
  else if (bdr.qname.isPartOf(zoneName))
3,279,002✔
607
    bdr.qname.makeUsRelative(zoneName);
3,278,700✔
608
  else {
302✔
609
    string msg = "Trying to insert non-zone data, name='" + bdr.qname.toLogString() + "', qtype=" + qtype.toString() + ", zone='" + zoneName.toLogString() + "'";
302✔
610
    if (s_ignore_broken_records) {
302!
611
      g_log << Logger::Warning << msg << " ignored" << endl;
302✔
612
      return;
302✔
613
    }
302✔
614
    throw PDNSException(std::move(msg));
×
615
  }
302✔
616

617
  //  bdr.qname.swap(bdr.qname);
618

619
  if (!records->empty() && bdr.qname == boost::prior(records->end())->qname)
3,278,700✔
620
    bdr.qname = boost::prior(records->end())->qname;
8,894✔
621

622
  bdr.qname = bdr.qname;
3,278,700✔
623
  bdr.qtype = qtype.getCode();
3,278,700✔
624
  bdr.content = content;
3,278,700✔
625
  bdr.nsec3hash = hashed;
3,278,700✔
626

627
  if (auth != nullptr) // Set auth on empty non-terminals
3,278,700✔
628
    bdr.auth = *auth;
4,616✔
629
  else
3,274,084✔
630
    bdr.auth = true;
3,274,084✔
631

632
  bdr.ttl = ttl;
3,278,700✔
633
  records->insert(std::move(bdr));
3,278,700✔
634
}
3,278,700✔
635

636
string Bind2Backend::DLReloadNowHandler(const vector<string>& parts, Utility::pid_t /* ppid */)
637
{
×
638
  ostringstream ret;
×
639

640
  for (auto i = parts.begin() + 1; i < parts.end(); ++i) {
×
641
    BB2DomainInfo bbd;
×
642
    ZoneName zone(*i);
×
643
    if (safeGetBBDomainInfo(zone, &bbd)) {
×
644
      Bind2Backend bb2;
×
645
      bb2.queueReloadAndStore(bbd.d_id);
×
646
      if (!safeGetBBDomainInfo(zone, &bbd)) // Read the *new* domain status
×
647
        ret << *i << ": [missing]\n";
×
648
      else
×
649
        ret << *i << ": " << (bbd.d_wasRejectedLastReload ? "[rejected]" : "") << "\t" << bbd.d_status << "\n";
×
650
      purgeAuthCaches(zone.operator const DNSName&().toString() + "$");
×
651
      DNSSECKeeper::clearMetaCache(zone);
×
652
    }
×
653
    else
×
654
      ret << *i << " no such domain\n";
×
655
  }
×
656
  if (ret.str().empty())
×
657
    ret << "no domains reloaded";
×
658
  return ret.str();
×
659
}
×
660

661
string Bind2Backend::DLDomStatusHandler(const vector<string>& parts, Utility::pid_t /* ppid */)
662
{
29✔
663
  ostringstream ret;
29✔
664

665
  if (parts.size() > 1) {
29!
666
    for (auto i = parts.begin() + 1; i < parts.end(); ++i) {
×
667
      BB2DomainInfo bbd;
×
668
      if (safeGetBBDomainInfo(ZoneName(*i), &bbd)) {
×
669
        ret << *i << ": " << (bbd.d_loaded ? "" : "[rejected]") << "\t" << bbd.d_status << "\n";
×
670
      }
×
671
      else {
×
672
        ret << *i << " no such domain\n";
×
673
      }
×
674
    }
×
675
  }
×
676
  else {
29✔
677
    auto state = s_state.read_lock();
29✔
678
    for (const auto& i : *state) {
269✔
679
      ret << i.d_name << ": " << (i.d_loaded ? "" : "[rejected]") << "\t" << i.d_status << "\n";
269✔
680
    }
269✔
681
  }
29✔
682

683
  if (ret.str().empty())
29!
684
    ret << "no domains passed";
×
685

686
  return ret.str();
29✔
687
}
29✔
688

689
static void printDomainExtendedStatus(ostringstream& ret, const BB2DomainInfo& info)
690
{
×
691
  ret << info.d_name << ": " << std::endl;
×
692
  ret << "\t Status: " << info.d_status << std::endl;
×
693
  ret << "\t Internal ID: " << info.d_id << std::endl;
×
694
  ret << "\t On-disk file: " << info.d_filename << " (" << info.d_ctime << ")" << std::endl;
×
695
  ret << "\t Kind: ";
×
696
  switch (info.d_kind) {
×
697
  case DomainInfo::Primary:
×
698
    ret << "Primary";
×
699
    break;
×
700
  case DomainInfo::Secondary:
×
701
    ret << "Secondary";
×
702
    break;
×
703
  default:
×
704
    ret << "Native";
×
705
  }
×
706
  ret << std::endl;
×
707
  ret << "\t Primaries: " << std::endl;
×
708
  for (const auto& primary : info.d_primaries) {
×
709
    ret << "\t\t - " << primary.toStringWithPort() << std::endl;
×
710
  }
×
711
  ret << "\t Also Notify: " << std::endl;
×
712
  for (const auto& also : info.d_also_notify) {
×
713
    ret << "\t\t - " << also << std::endl;
×
714
  }
×
715
  ret << "\t Number of records: " << info.d_records.getEntriesCount() << std::endl;
×
716
  ret << "\t Loaded: " << info.d_loaded << std::endl;
×
717
  ret << "\t Check now: " << info.d_checknow << std::endl;
×
718
  ret << "\t Check interval: " << info.getCheckInterval() << std::endl;
×
719
  ret << "\t Last check: " << info.d_lastcheck << std::endl;
×
720
  ret << "\t Last notified: " << info.d_lastnotified << std::endl;
×
721
}
×
722

723
string Bind2Backend::DLDomExtendedStatusHandler(const vector<string>& parts, Utility::pid_t /* ppid */)
724
{
×
725
  ostringstream ret;
×
726

727
  if (parts.size() > 1) {
×
728
    for (auto i = parts.begin() + 1; i < parts.end(); ++i) {
×
729
      BB2DomainInfo bbd;
×
730
      if (safeGetBBDomainInfo(ZoneName(*i), &bbd)) {
×
731
        printDomainExtendedStatus(ret, bbd);
×
732
      }
×
733
      else {
×
734
        ret << *i << " no such domain" << std::endl;
×
735
      }
×
736
    }
×
737
  }
×
738
  else {
×
739
    auto rstate = s_state.read_lock();
×
740
    for (const auto& state : *rstate) {
×
741
      printDomainExtendedStatus(ret, state);
×
742
    }
×
743
  }
×
744

745
  if (ret.str().empty()) {
×
746
    ret << "no domains passed" << std::endl;
×
747
  }
×
748

749
  return ret.str();
×
750
}
×
751

752
string Bind2Backend::DLListRejectsHandler(const vector<string>& /* parts */, Utility::pid_t /* ppid */)
753
{
×
754
  ostringstream ret;
×
755
  auto rstate = s_state.read_lock();
×
756
  for (const auto& i : *rstate) {
×
757
    if (!i.d_loaded)
×
758
      ret << i.d_name << "\t" << i.d_status << endl;
×
759
  }
×
760
  return ret.str();
×
761
}
×
762

763
string Bind2Backend::DLAddDomainHandler(const vector<string>& parts, Utility::pid_t /* ppid */)
764
{
12✔
765
  if (parts.size() < 3)
12!
766
    return "ERROR: Domain name and zone filename are required";
×
767

768
  ZoneName domainname(parts[1]);
12✔
769
  const string& filename = parts[2];
12✔
770
  BB2DomainInfo bbd;
12✔
771
  if (safeGetBBDomainInfo(domainname, &bbd))
12✔
772
    return "Already loaded";
6✔
773

774
  if (!boost::starts_with(filename, "/") && ::arg()["chroot"].empty())
6!
775
    return "Unable to load zone " + domainname.toLogString() + " from " + filename + " as the filename is not absolute.";
×
776

777
  struct stat buf;
6✔
778
  if (stat(filename.c_str(), &buf) != 0)
6!
779
    return "Unable to load zone " + domainname.toLogString() + " from " + filename + ": " + strerror(errno);
×
780

781
  Bind2Backend bb2; // createdomainentry needs access to our configuration
6✔
782
  bbd = bb2.createDomainEntry(domainname, filename);
6✔
783
  bbd.d_filename = filename;
6✔
784
  bbd.d_checknow = true;
6✔
785
  bbd.d_loaded = true;
6✔
786
  bbd.d_lastcheck = 0;
6✔
787
  bbd.d_status = "parsing into memory";
6✔
788
  bbd.setCtime();
6✔
789

790
  safePutBBDomainInfo(bbd);
6✔
791

792
  g_zoneCache.add(domainname, bbd.d_id); // make new zone visible
6✔
793

794
  g_log << Logger::Warning << "Zone " << domainname << " loaded" << endl;
6✔
795
  return "Loaded zone " + domainname.toLogString() + " from " + filename;
6✔
796
}
6✔
797

798
Bind2Backend::Bind2Backend(const string& suffix, bool loadZones)
799
{
3,227✔
800
  d_getAllDomainMetadataQuery_stmt = nullptr;
3,227✔
801
  d_getDomainMetadataQuery_stmt = nullptr;
3,227✔
802
  d_deleteDomainMetadataQuery_stmt = nullptr;
3,227✔
803
  d_insertDomainMetadataQuery_stmt = nullptr;
3,227✔
804
  d_getDomainKeysQuery_stmt = nullptr;
3,227✔
805
  d_deleteDomainKeyQuery_stmt = nullptr;
3,227✔
806
  d_insertDomainKeyQuery_stmt = nullptr;
3,227✔
807
  d_GetLastInsertedKeyIdQuery_stmt = nullptr;
3,227✔
808
  d_activateDomainKeyQuery_stmt = nullptr;
3,227✔
809
  d_deactivateDomainKeyQuery_stmt = nullptr;
3,227✔
810
  d_getTSIGKeyQuery_stmt = nullptr;
3,227✔
811
  d_setTSIGKeyQuery_stmt = nullptr;
3,227✔
812
  d_deleteTSIGKeyQuery_stmt = nullptr;
3,227✔
813
  d_getTSIGKeysQuery_stmt = nullptr;
3,227✔
814

815
  setArgPrefix("bind" + suffix);
3,227✔
816
  d_logprefix = "[bind" + suffix + "backend]";
3,227✔
817
  d_hybrid = mustDo("hybrid");
3,227✔
818
  if (d_hybrid && g_zoneCache.isEnabled()) {
3,227!
819
    throw PDNSException("bind-hybrid and the zone cache currently interoperate badly. Please disable the zone cache or stop using bind-hybrid");
×
820
  }
×
821

822
  d_transaction_id = UnknownDomainID;
3,227✔
823
  s_ignore_broken_records = mustDo("ignore-broken-records");
3,227✔
824
  d_upgradeContent = ::arg().mustDo("upgrade-unknown-types");
3,227✔
825

826
  if (!loadZones && d_hybrid)
3,227!
827
    return;
×
828

829
  auto lock = std::scoped_lock(s_startup_lock);
3,227✔
830

831
  setupDNSSEC();
3,227✔
832
  if (s_first == 0) {
3,227✔
833
    return;
2,472✔
834
  }
2,472✔
835

836
  if (loadZones) {
755✔
837
    loadConfig();
311✔
838
    s_first = 0;
311✔
839
  }
311✔
840

841
  DynListener::registerFunc("BIND-RELOAD-NOW", &DLReloadNowHandler, "bindbackend: reload domains", "<domains>");
755✔
842
  DynListener::registerFunc("BIND-DOMAIN-STATUS", &DLDomStatusHandler, "bindbackend: list status of all domains", "[domains]");
755✔
843
  DynListener::registerFunc("BIND-DOMAIN-EXTENDED-STATUS", &DLDomExtendedStatusHandler, "bindbackend: list the extended status of all domains", "[domains]");
755✔
844
  DynListener::registerFunc("BIND-LIST-REJECTS", &DLListRejectsHandler, "bindbackend: list rejected domains");
755✔
845
  DynListener::registerFunc("BIND-ADD-ZONE", &DLAddDomainHandler, "bindbackend: add zone", "<domain> <filename>");
755✔
846
}
755✔
847

848
Bind2Backend::~Bind2Backend()
849
{
3,103✔
850
  freeStatements();
3,103✔
851
} // deallocate statements
3,103✔
852

853
void Bind2Backend::rediscover(string* status)
854
{
×
855
  loadConfig(status);
×
856
}
×
857

858
void Bind2Backend::reload()
859
{
×
860
  auto state = s_state.write_lock();
×
861
  for (const auto& i : *state) {
×
862
    i.d_checknow = true; // being a bit cheeky here, don't index state_t on this (mutable)
×
863
  }
×
864
}
×
865

866
void Bind2Backend::fixupOrderAndAuth(std::shared_ptr<recordstorage_t>& records, const ZoneName& zoneName, bool nsec3zone, const NSEC3PARAMRecordContent& ns3pr)
867
{
2,665✔
868
  bool skip;
2,665✔
869
  DNSName shorter;
2,665✔
870
  set<DNSName> nssets, dssets;
2,665✔
871

872
  for (const auto& bdr : *records) {
3,274,084✔
873
    if (!bdr.qname.isRoot() && bdr.qtype == QType::NS)
3,274,084✔
874
      nssets.insert(bdr.qname);
3,010✔
875
    else if (bdr.qtype == QType::DS)
3,271,074✔
876
      dssets.insert(bdr.qname);
499✔
877
  }
3,274,084✔
878

879
  for (auto iter = records->begin(); iter != records->end(); iter++) {
3,276,749✔
880
    skip = false;
3,274,084✔
881
    shorter = iter->qname;
3,274,084✔
882

883
    if (!iter->qname.isRoot() && shorter.chopOff() && !iter->qname.isRoot()) {
3,274,084!
884
      do {
3,272,541✔
885
        if (nssets.count(shorter) != 0u) {
3,272,541✔
886
          skip = true;
1,359✔
887
          break;
1,359✔
888
        }
1,359✔
889
      } while (shorter.chopOff() && !iter->qname.isRoot());
3,272,541!
890
    }
3,262,953✔
891

892
    iter->auth = (!skip && (iter->qtype == QType::DS || iter->qtype == QType::RRSIG || (nssets.count(iter->qname) == 0u)));
3,274,084✔
893

894
    if (!skip && nsec3zone && iter->qtype != QType::RRSIG && (iter->auth || (iter->qtype == QType::NS && (ns3pr.d_flags == 0u)) || (dssets.count(iter->qname) != 0u))) {
3,274,084✔
895
      Bind2DNSRecord bdr = *iter;
1,439,445✔
896
      bdr.nsec3hash = toBase32Hex(hashQNameWithSalt(ns3pr, bdr.qname + zoneName.operator const DNSName&()));
1,439,445✔
897
      records->replace(iter, bdr);
1,439,445✔
898
    }
1,439,445✔
899

900
    // cerr<<iter->qname<<"\t"<<QType(iter->qtype).toString()<<"\t"<<iter->nsec3hash<<"\t"<<iter->auth<<endl;
901
  }
3,274,084✔
902
}
2,665✔
903

904
void Bind2Backend::doEmptyNonTerminals(std::shared_ptr<recordstorage_t>& records, const ZoneName& zoneName, bool nsec3zone, const NSEC3PARAMRecordContent& ns3pr)
905
{
2,665✔
906
  bool auth = false;
2,665✔
907
  DNSName shorter;
2,665✔
908
  std::unordered_set<DNSName> qnames;
2,665✔
909
  std::unordered_map<DNSName, bool> nonterm;
2,665✔
910

911
  uint32_t maxent = ::arg().asNum("max-ent-entries");
2,665✔
912

913
  for (const auto& bdr : *records)
2,665✔
914
    qnames.insert(bdr.qname);
3,274,084✔
915

916
  for (const auto& bdr : *records) {
3,274,084✔
917

918
    if (!bdr.auth && bdr.qtype == QType::NS)
3,274,084✔
919
      auth = (!nsec3zone || (ns3pr.d_flags == 0u));
3,010✔
920
    else
3,271,074✔
921
      auth = bdr.auth;
3,271,074✔
922

923
    shorter = bdr.qname;
3,274,084✔
924
    while (shorter.chopOff()) {
6,547,984✔
925
      if (qnames.count(shorter) == 0u) {
3,273,900✔
926
        if (!(maxent)) {
9,422!
927
          g_log << Logger::Error << "Zone '" << zoneName << "' has too many empty non terminals." << endl;
×
928
          return;
×
929
        }
×
930

931
        if (nonterm.count(shorter) == 0u) {
9,422✔
932
          nonterm.emplace(shorter, auth);
4,616✔
933
          --maxent;
4,616✔
934
        }
4,616✔
935
        else if (auth)
4,806✔
936
          nonterm[shorter] = true;
4,710✔
937
      }
9,422✔
938
    }
3,273,900✔
939
  }
3,274,084✔
940

941
  DNSResourceRecord rr;
2,665✔
942
  rr.qtype = "#0";
2,665✔
943
  rr.content = "";
2,665✔
944
  rr.ttl = 0;
2,665✔
945
  for (auto& nt : nonterm) {
4,617✔
946
    string hashed;
4,616✔
947
    rr.qname = nt.first + zoneName.operator const DNSName&();
4,616✔
948
    if (nsec3zone && nt.second)
4,616✔
949
      hashed = toBase32Hex(hashQNameWithSalt(ns3pr, rr.qname));
1,782✔
950
    insertRecord(records, zoneName, rr.qname, rr.qtype, rr.content, rr.ttl, hashed, &nt.second);
4,616✔
951

952
    // cerr<<rr.qname<<"\t"<<rr.qtype.toString()<<"\t"<<hashed<<"\t"<<nt.second<<endl;
953
  }
4,616✔
954
}
2,665✔
955

956
void Bind2Backend::loadConfig(string* status) // NOLINT(readability-function-cognitive-complexity) 13379 https://github.com/PowerDNS/pdns/issues/13379 Habbie: zone2sql.cc, bindbackend2.cc: reduce complexity
957
{
311✔
958
  static domainid_t domain_id = 1;
311✔
959

960
  if (!getArg("config").empty()) {
311!
961
    BindParser BP;
311✔
962
    try {
311✔
963
      BP.parse(getArg("config"));
311✔
964
    }
311✔
965
    catch (PDNSException& ae) {
311✔
966
      g_log << Logger::Error << "Error parsing bind configuration: " << ae.reason << endl;
×
967
      throw;
×
968
    }
×
969

970
    vector<BindDomainInfo> domains = BP.getDomains();
311✔
971
    this->alsoNotify = BP.getAlsoNotify();
311✔
972

973
    s_binddirectory = BP.getDirectory();
311✔
974
    //    ZP.setDirectory(d_binddirectory);
975

976
    g_log << Logger::Warning << d_logprefix << " Parsing " << domains.size() << " domain(s), will report when done" << endl;
311✔
977

978
    set<ZoneName> oldnames;
311✔
979
    set<ZoneName> newnames;
311✔
980
    {
311✔
981
      auto state = s_state.read_lock();
311✔
982
      for (const BB2DomainInfo& bbd : *state) {
311!
983
        oldnames.insert(bbd.d_name);
×
984
      }
×
985
    }
311✔
986
    int rejected = 0;
311✔
987
    int newdomains = 0;
311✔
988

989
    struct stat st;
311✔
990

991
    for (auto& domain : domains) {
2,791✔
992
      if (stat(domain.filename.c_str(), &st) == 0) {
2,659✔
993
        domain.d_dev = st.st_dev;
2,587✔
994
        domain.d_ino = st.st_ino;
2,587✔
995
      }
2,587✔
996
    }
2,659✔
997

998
    sort(domains.begin(), domains.end()); // put stuff in inode order
311✔
999
    for (const auto& domain : domains) {
2,791✔
1000
      if (!(domain.hadFileDirective)) {
2,659!
1001
        g_log << Logger::Warning << d_logprefix << " Zone '" << domain.name << "' has no 'file' directive set in " << getArg("config") << endl;
×
1002
        rejected++;
×
1003
        continue;
×
1004
      }
×
1005

1006
      if (domain.type.empty()) {
2,659!
1007
        g_log << Logger::Notice << d_logprefix << " Zone '" << domain.name << "' has no type specified, assuming 'native'" << endl;
×
1008
      }
×
1009
      if (domain.type != "primary" && domain.type != "secondary" && domain.type != "native" && !domain.type.empty() && domain.type != "master" && domain.type != "slave") {
2,659!
1010
        g_log << Logger::Warning << d_logprefix << " Warning! Skipping zone '" << domain.name << "' because type '" << domain.type << "' is invalid" << endl;
×
1011
        rejected++;
×
1012
        continue;
×
1013
      }
×
1014

1015
      BB2DomainInfo bbd;
2,659✔
1016
      bool isNew = false;
2,659✔
1017

1018
      if (!safeGetBBDomainInfo(domain.name, &bbd)) {
2,659!
1019
        isNew = true;
2,659✔
1020
        bbd.d_id = domain_id++;
2,659✔
1021
        bbd.setCheckInterval(getArgAsNum("check-interval"));
2,659✔
1022
        bbd.d_lastnotified = 0;
2,659✔
1023
        bbd.d_loaded = false;
2,659✔
1024
      }
2,659✔
1025

1026
      // overwrite what we knew about the domain
1027
      bbd.d_name = domain.name;
2,659✔
1028
      bool filenameChanged = (bbd.d_filename != domain.filename);
2,659✔
1029
      bool addressesChanged = (bbd.d_primaries != domain.primaries || bbd.d_also_notify != domain.alsoNotify);
2,659!
1030
      bbd.d_filename = domain.filename;
2,659✔
1031
      bbd.d_primaries = domain.primaries;
2,659✔
1032
      bbd.d_also_notify = domain.alsoNotify;
2,659✔
1033

1034
      DomainInfo::DomainKind kind = DomainInfo::Native;
2,659✔
1035
      if (domain.type == "primary" || domain.type == "master") {
2,659!
1036
        kind = DomainInfo::Primary;
2,587✔
1037
      }
2,587✔
1038
      if (domain.type == "secondary" || domain.type == "slave") {
2,659!
1039
        kind = DomainInfo::Secondary;
72✔
1040
      }
72✔
1041

1042
      bool kindChanged = (bbd.d_kind != kind);
2,659✔
1043
      bbd.d_kind = kind;
2,659✔
1044

1045
      newnames.insert(bbd.d_name);
2,659✔
1046
      if (filenameChanged || !bbd.d_loaded || !bbd.current()) {
2,659!
1047
        g_log << Logger::Info << d_logprefix << " parsing '" << domain.name << "' from file '" << domain.filename << "'" << endl;
2,659✔
1048

1049
        try {
2,659✔
1050
          parseZoneFile(&bbd);
2,659✔
1051
        }
2,659✔
1052
        catch (PDNSException& ae) {
2,659✔
1053
          ostringstream msg;
×
1054
          msg << " error at " + nowTime() + " parsing '" << domain.name << "' from file '" << domain.filename << "': " << ae.reason;
×
1055

1056
          if (status != nullptr)
×
1057
            *status += msg.str();
×
1058
          bbd.d_status = msg.str();
×
1059

1060
          g_log << Logger::Warning << d_logprefix << msg.str() << endl;
×
1061
          rejected++;
×
1062
        }
×
1063
        catch (std::system_error& ae) {
2,659✔
1064
          ostringstream msg;
72✔
1065
          if (ae.code().value() == ENOENT && isNew && domain.type == "slave")
72!
1066
            msg << " error at " + nowTime() << " no file found for new secondary domain '" << domain.name << "'. Has not been AXFR'd yet";
×
1067
          else
72✔
1068
            msg << " error at " + nowTime() + " parsing '" << domain.name << "' from file '" << domain.filename << "': " << ae.what();
72✔
1069

1070
          if (status != nullptr)
72!
1071
            *status += msg.str();
×
1072
          bbd.d_status = msg.str();
72✔
1073
          g_log << Logger::Warning << d_logprefix << msg.str() << endl;
72✔
1074
          rejected++;
72✔
1075
        }
72✔
1076
        catch (std::exception& ae) {
2,659✔
1077
          ostringstream msg;
×
1078
          msg << " error at " + nowTime() + " parsing '" << domain.name << "' from file '" << domain.filename << "': " << ae.what();
×
1079

1080
          if (status != nullptr)
×
1081
            *status += msg.str();
×
1082
          bbd.d_status = msg.str();
×
1083

1084
          g_log << Logger::Warning << d_logprefix << msg.str() << endl;
×
1085
          rejected++;
×
1086
        }
×
1087
        safePutBBDomainInfo(bbd);
2,659✔
1088
      }
2,659✔
1089
      else if (addressesChanged || kindChanged) {
×
1090
        safePutBBDomainInfo(bbd);
×
1091
      }
×
1092
    }
2,659✔
1093
    vector<ZoneName> diff;
311✔
1094

1095
    set_difference(oldnames.begin(), oldnames.end(), newnames.begin(), newnames.end(), back_inserter(diff));
311✔
1096
    unsigned int remdomains = diff.size();
311✔
1097

1098
    for (const ZoneName& name : diff) {
311!
1099
      safeRemoveBBDomainInfo(name);
×
1100
    }
×
1101

1102
    // count number of entirely new domains
1103
    diff.clear();
311✔
1104
    set_difference(newnames.begin(), newnames.end(), oldnames.begin(), oldnames.end(), back_inserter(diff));
311✔
1105
    newdomains = diff.size();
311✔
1106

1107
    ostringstream msg;
311✔
1108
    msg << " Done parsing domains, " << rejected << " rejected, " << newdomains << " new, " << remdomains << " removed";
311✔
1109
    if (status != nullptr)
311!
1110
      *status = msg.str();
×
1111

1112
    g_log << Logger::Error << d_logprefix << msg.str() << endl;
311✔
1113
  }
311✔
1114
}
311✔
1115

1116
// NOLINTNEXTLINE(readability-identifier-length)
1117
void Bind2Backend::queueReloadAndStore(domainid_t id)
1118
{
78✔
1119
  BB2DomainInfo bbold;
78✔
1120
  try {
78✔
1121
    if (!safeGetBBDomainInfo(id, &bbold))
78!
1122
      return;
×
1123
    bbold.d_checknow = false;
78✔
1124
    BB2DomainInfo bbnew(bbold);
78✔
1125
    /* make sure that nothing will be able to alter the existing records,
1126
       we will load them from the zone file instead */
1127
    bbnew.d_records = LookButDontTouch<recordstorage_t>();
78✔
1128
    parseZoneFile(&bbnew);
78✔
1129
    bbnew.d_wasRejectedLastReload = false;
78✔
1130
    safePutBBDomainInfo(bbnew);
78✔
1131
    g_log << Logger::Warning << "Zone '" << bbnew.d_name << "' (" << bbnew.d_filename << ") reloaded" << endl;
78✔
1132
  }
78✔
1133
  catch (PDNSException& ae) {
78✔
1134
    ostringstream msg;
×
1135
    msg << " error at " + nowTime() + " parsing '" << bbold.d_name << "' from file '" << bbold.d_filename << "': " << ae.reason;
×
1136
    g_log << Logger::Warning << "Error parsing '" << bbold.d_name << "' from file '" << bbold.d_filename << "': " << ae.reason << endl;
×
1137
    bbold.d_status = msg.str();
×
1138
    bbold.d_lastcheck = time(nullptr);
×
1139
    bbold.d_wasRejectedLastReload = true;
×
1140
    safePutBBDomainInfo(bbold);
×
1141
  }
×
1142
  catch (std::exception& ae) {
78✔
1143
    ostringstream msg;
×
1144
    msg << " error at " + nowTime() + " parsing '" << bbold.d_name << "' from file '" << bbold.d_filename << "': " << ae.what();
×
1145
    g_log << Logger::Warning << "Error parsing '" << bbold.d_name << "' from file '" << bbold.d_filename << "': " << ae.what() << endl;
×
1146
    bbold.d_status = msg.str();
×
1147
    bbold.d_lastcheck = time(nullptr);
×
1148
    bbold.d_wasRejectedLastReload = true;
×
1149
    safePutBBDomainInfo(bbold);
×
1150
  }
×
1151
}
78✔
1152

1153
bool Bind2Backend::findBeforeAndAfterUnhashed(std::shared_ptr<const recordstorage_t>& records, const DNSName& qname, DNSName& /* unhashed */, DNSName& before, DNSName& after)
1154
{
2,710✔
1155
  // for(const auto& record: *records)
1156
  //   cerr<<record.qname<<"\t"<<makeHexDump(record.qname.toDNSString())<<endl;
1157

1158
  recordstorage_t::const_iterator iterBefore, iterAfter;
2,710✔
1159

1160
  iterBefore = iterAfter = records->upper_bound(qname.makeLowerCase());
2,710✔
1161

1162
  if (iterBefore != records->begin())
2,710!
1163
    --iterBefore;
2,710✔
1164
  while ((!iterBefore->auth && iterBefore->qtype != QType::NS) || !iterBefore->qtype)
3,239✔
1165
    --iterBefore;
529✔
1166
  before = iterBefore->qname;
2,710✔
1167

1168
  if (iterAfter == records->end()) {
2,710✔
1169
    iterAfter = records->begin();
376✔
1170
  }
376✔
1171
  else {
2,334✔
1172
    while ((!iterAfter->auth && iterAfter->qtype != QType::NS) || !iterAfter->qtype) {
3,498✔
1173
      ++iterAfter;
1,164✔
1174
      if (iterAfter == records->end()) {
1,164!
1175
        iterAfter = records->begin();
×
1176
        break;
×
1177
      }
×
1178
    }
1,164✔
1179
  }
2,334✔
1180
  after = iterAfter->qname;
2,710✔
1181

1182
  return true;
2,710✔
1183
}
2,710✔
1184

1185
// NOLINTNEXTLINE(readability-identifier-length)
1186
bool Bind2Backend::getBeforeAndAfterNamesAbsolute(domainid_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after)
1187
{
7,037✔
1188
  BB2DomainInfo bbd;
7,037✔
1189
  if (!safeGetBBDomainInfo(id, &bbd))
7,037!
1190
    return false;
×
1191

1192
  shared_ptr<const recordstorage_t> records = bbd.d_records.get();
7,037✔
1193
  if (!bbd.d_nsec3zone) {
7,037✔
1194
    return findBeforeAndAfterUnhashed(records, qname, unhashed, before, after);
2,710✔
1195
  }
2,710✔
1196
  else {
4,327✔
1197
    const auto& hashindex = boost::multi_index::get<NSEC3Tag>(*records);
4,327✔
1198

1199
    // for(auto iter = first; iter != hashindex.end(); iter++)
1200
    //  cerr<<iter->nsec3hash<<endl;
1201

1202
    auto first = hashindex.upper_bound("");
4,327✔
1203
    auto iter = hashindex.upper_bound(qname.toStringNoDot());
4,327✔
1204

1205
    if (iter == hashindex.end()) {
4,327✔
1206
      --iter;
345✔
1207
      before = DNSName(iter->nsec3hash);
345✔
1208
      after = DNSName(first->nsec3hash);
345✔
1209
    }
345✔
1210
    else {
3,982✔
1211
      after = DNSName(iter->nsec3hash);
3,982✔
1212
      if (iter != first)
3,982✔
1213
        --iter;
3,879✔
1214
      else
103✔
1215
        iter = --hashindex.end();
103✔
1216
      before = DNSName(iter->nsec3hash);
3,982✔
1217
    }
3,982✔
1218
    unhashed = iter->qname + bbd.d_name.operator const DNSName&();
4,327✔
1219

1220
    return true;
4,327✔
1221
  }
4,327✔
1222
}
7,037✔
1223

1224
void Bind2Backend::lookup(const QType& qtype, const DNSName& qname, domainid_t zoneId, DNSPacket* /* pkt_p */)
1225
{
4,151✔
1226
  d_handle.reset();
4,151✔
1227

1228
  static bool mustlog = ::arg().mustDo("query-logging");
4,151✔
1229

1230
  bool found = false;
4,151✔
1231
  ZoneName domain;
4,151✔
1232
  BB2DomainInfo bbd;
4,151✔
1233

1234
  if (mustlog)
4,151!
1235
    g_log << Logger::Warning << "Lookup for '" << qtype.toString() << "' of '" << qname << "' within zoneID " << zoneId << endl;
×
1236

1237
  if (zoneId != UnknownDomainID) {
4,151✔
1238
    if ((found = (safeGetBBDomainInfo(zoneId, &bbd) && qname.isPartOf(bbd.d_name)))) {
3,537!
1239
      domain = std::move(bbd.d_name);
3,537✔
1240
    }
3,537✔
1241
  }
3,537✔
1242
  else {
614✔
1243
    domain = ZoneName(qname);
614✔
1244
    do {
617✔
1245
      found = safeGetBBDomainInfo(domain, &bbd);
617✔
1246
    } while (!found && qtype != QType::SOA && domain.chopOff());
617✔
1247
  }
614✔
1248

1249
  if (!found) {
4,151✔
1250
    if (mustlog)
24!
1251
      g_log << Logger::Warning << "Found no authoritative zone for '" << qname << "' and/or id " << zoneId << endl;
×
1252
    d_handle.d_list = false;
24✔
1253
    return;
24✔
1254
  }
24✔
1255

1256
  if (mustlog)
4,127!
1257
    g_log << Logger::Warning << "Found a zone '" << domain << "' (with id " << bbd.d_id << ") that might contain data " << endl;
×
1258

1259
  d_handle.id = bbd.d_id;
4,127✔
1260
  d_handle.qname = qname.makeRelative(domain); // strip domain name
4,127✔
1261
  d_handle.qtype = qtype;
4,127✔
1262
  d_handle.domain = std::move(domain);
4,127✔
1263

1264
  if (!bbd.current()) {
4,127✔
1265
    g_log << Logger::Warning << "Zone '" << d_handle.domain << "' (" << bbd.d_filename << ") needs reloading" << endl;
6✔
1266
    queueReloadAndStore(bbd.d_id);
6✔
1267
    if (!safeGetBBDomainInfo(d_handle.domain, &bbd))
6!
1268
      throw DBException("Zone '" + bbd.d_name.toLogString() + "' (" + bbd.d_filename + ") gone after reload"); // if we don't throw here, we crash for some reason
×
1269
  }
6✔
1270

1271
  if (!bbd.d_loaded) {
4,127✔
1272
    d_handle.reset();
216✔
1273
    throw DBException("Zone for '" + d_handle.domain.toLogString() + "' in '" + bbd.d_filename + "' not loaded (file missing, corrupt or primary dead)"); // fsck
216✔
1274
  }
216✔
1275

1276
  d_handle.d_records = bbd.d_records.get();
3,911✔
1277

1278
  if (d_handle.d_records->empty())
3,911!
1279
    DLOG(g_log << "Query with no results" << endl);
×
1280

1281
  d_handle.mustlog = mustlog;
3,911✔
1282

1283
  const auto& hashedidx = boost::multi_index::get<UnorderedNameTag>(*d_handle.d_records);
3,911✔
1284
  auto range = hashedidx.equal_range(d_handle.qname);
3,911✔
1285

1286
  d_handle.d_list = false;
3,911✔
1287
  d_handle.d_iter = range.first;
3,911✔
1288
  d_handle.d_end_iter = range.second;
3,911✔
1289
}
3,911✔
1290

1291
Bind2Backend::handle::handle()
1292
{
3,227✔
1293
  mustlog = false;
3,227✔
1294
}
3,227✔
1295

1296
bool Bind2Backend::get(DNSResourceRecord& r)
1297
{
197,927✔
1298
  if (!d_handle.d_records) {
197,927✔
1299
    if (d_handle.mustlog)
24!
1300
      g_log << Logger::Warning << "There were no answers" << endl;
×
1301
    return false;
24✔
1302
  }
24✔
1303

1304
  if (!d_handle.get(r)) {
197,903✔
1305
    if (d_handle.mustlog)
4,216!
1306
      g_log << Logger::Warning << "End of answers" << endl;
×
1307

1308
    d_handle.reset();
4,216✔
1309

1310
    return false;
4,216✔
1311
  }
4,216✔
1312
  if (d_handle.mustlog)
193,687!
1313
    g_log << Logger::Warning << "Returning: '" << r.qtype.toString() << "' of '" << r.qname << "', content: '" << r.content << "'" << endl;
×
1314
  return true;
193,687✔
1315
}
197,903✔
1316

1317
void Bind2Backend::lookupEnd()
1318
{
26✔
1319
  d_handle.reset();
26✔
1320
}
26✔
1321

1322
bool Bind2Backend::handle::get(DNSResourceRecord& r)
1323
{
197,903✔
1324
  if (d_list)
197,903✔
1325
    return get_list(r);
188,900✔
1326
  else
9,003✔
1327
    return get_normal(r);
9,003✔
1328
}
197,903✔
1329

1330
void Bind2Backend::handle::reset()
1331
{
8,940✔
1332
  d_records.reset();
8,940✔
1333
  qname.clear();
8,940✔
1334
  mustlog = false;
8,940✔
1335
}
8,940✔
1336

1337
//#define DLOG(x) x
1338
bool Bind2Backend::handle::get_normal(DNSResourceRecord& r)
1339
{
9,003✔
1340
  DLOG(g_log << "Bind2Backend get() was called for " << qtype.toString() << " record for '" << qname << "' - " << d_records->size() << " available in total!" << endl);
9,003✔
1341

1342
  if (d_iter == d_end_iter) {
9,003✔
1343
    return false;
3,885✔
1344
  }
3,885✔
1345

1346
  while (d_iter != d_end_iter && !(qtype.getCode() == QType::ANY || (d_iter)->qtype == qtype.getCode())) {
7,851!
1347
    DLOG(g_log << Logger::Warning << "Skipped " << qname << "/" << QType(d_iter->qtype).toString() << ": '" << d_iter->content << "'" << endl);
2,733✔
1348
    d_iter++;
2,733✔
1349
  }
2,733✔
1350
  if (d_iter == d_end_iter) {
5,118!
1351
    return false;
×
1352
  }
×
1353
  DLOG(g_log << "Bind2Backend get() returning a rr with a " << QType(d_iter->qtype).getCode() << endl);
5,118✔
1354

1355
  const DNSName& domainName(domain);
5,118✔
1356
  r.qname = qname.empty() ? domainName : (qname + domainName);
5,118!
1357
  r.domain_id = id;
5,118✔
1358
  r.content = (d_iter)->content;
5,118✔
1359
  //  r.domain_id=(d_iter)->domain_id;
1360
  r.qtype = (d_iter)->qtype;
5,118✔
1361
  r.ttl = (d_iter)->ttl;
5,118✔
1362

1363
  //if(!d_iter->auth && r.qtype.getCode() != QType::A && r.qtype.getCode()!=QType::AAAA && r.qtype.getCode() != QType::NS)
1364
  //  cerr<<"Warning! Unauth response for qtype "<< r.qtype.toString() << " for '"<<r.qname<<"'"<<endl;
1365
  r.auth = d_iter->auth;
5,118✔
1366

1367
  d_iter++;
5,118✔
1368

1369
  return true;
5,118✔
1370
}
5,118✔
1371

1372
bool Bind2Backend::list(const ZoneName& /* target */, domainid_t domainId, bool /* include_disabled */)
1373
{
331✔
1374
  BB2DomainInfo bbd;
331✔
1375

1376
  if (!safeGetBBDomainInfo(domainId, &bbd)) {
331!
1377
    return false;
×
1378
  }
×
1379

1380
  d_handle.reset();
331✔
1381
  DLOG(g_log << "Bind2Backend constructing handle for list of " << domainId << endl);
331✔
1382

1383
  if (!bbd.d_loaded) {
331!
1384
    throw PDNSException("zone was not loaded, perhaps because of: " + bbd.d_status);
×
1385
  }
×
1386

1387
  d_handle.d_records = bbd.d_records.get(); // give it a copy, which will stay around
331✔
1388
  d_handle.d_qname_iter = d_handle.d_records->begin();
331✔
1389
  d_handle.d_qname_end = d_handle.d_records->end(); // iter now points to a vector of pointers to vector<BBResourceRecords>
331✔
1390

1391
  d_handle.id = domainId;
331✔
1392
  d_handle.domain = bbd.d_name;
331✔
1393
  d_handle.d_list = true;
331✔
1394
  return true;
331✔
1395
}
331✔
1396

1397
bool Bind2Backend::handle::get_list(DNSResourceRecord& r)
1398
{
188,900✔
1399
  if (d_qname_iter != d_qname_end) {
188,900✔
1400
    const DNSName& domainName(domain);
188,569✔
1401
    r.qname = d_qname_iter->qname.empty() ? domainName : (d_qname_iter->qname + domainName);
188,569!
1402
    r.domain_id = id;
188,569✔
1403
    r.content = (d_qname_iter)->content;
188,569✔
1404
    r.qtype = (d_qname_iter)->qtype;
188,569✔
1405
    r.ttl = (d_qname_iter)->ttl;
188,569✔
1406
    r.auth = d_qname_iter->auth;
188,569✔
1407
    d_qname_iter++;
188,569✔
1408
    return true;
188,569✔
1409
  }
188,569✔
1410
  return false;
331✔
1411
}
188,900✔
1412

1413
bool Bind2Backend::autoPrimariesList(std::vector<AutoPrimary>& primaries)
1414
{
×
1415
  if (getArg("autoprimary-config").empty())
×
1416
    return false;
×
1417

1418
  ifstream c_if(getArg("autoprimaries"), std::ios::in);
×
1419
  if (!c_if) {
×
1420
    g_log << Logger::Error << "Unable to open autoprimaries file for read: " << stringerror() << endl;
×
1421
    return false;
×
1422
  }
×
1423

1424
  string line, sip, saccount;
×
1425
  while (getline(c_if, line)) {
×
1426
    std::istringstream ii(line);
×
1427
    ii >> sip;
×
1428
    if (!sip.empty()) {
×
1429
      ii >> saccount;
×
1430
      primaries.emplace_back(sip, "", saccount);
×
1431
    }
×
1432
  }
×
1433

1434
  c_if.close();
×
1435
  return true;
×
1436
}
×
1437

1438
bool Bind2Backend::autoPrimaryBackend(const string& ipAddress, const ZoneName& /* domain */, const vector<DNSResourceRecord>& /* nsset */, string* /* nameserver */, string* account, DNSBackend** backend)
1439
{
×
1440
  // Check whether we have a configfile available.
1441
  if (getArg("autoprimary-config").empty())
×
1442
    return false;
×
1443

1444
  ifstream c_if(getArg("autoprimaries").c_str(), std::ios::in); // this was nocreate?
×
1445
  if (!c_if) {
×
1446
    g_log << Logger::Error << "Unable to open autoprimaries file for read: " << stringerror() << endl;
×
1447
    return false;
×
1448
  }
×
1449

1450
  // Format:
1451
  // <ip> <accountname>
1452
  string line, sip, saccount;
×
1453
  while (getline(c_if, line)) {
×
1454
    std::istringstream ii(line);
×
1455
    ii >> sip;
×
1456
    if (sip == ipAddress) {
×
1457
      ii >> saccount;
×
1458
      break;
×
1459
    }
×
1460
  }
×
1461
  c_if.close();
×
1462

1463
  if (sip != ipAddress) { // ip not found in authorization list - reject
×
1464
    return false;
×
1465
  }
×
1466

1467
  // ip authorized as autoprimary - accept
1468
  *backend = this;
×
1469
  if (saccount.length() > 0)
×
1470
    *account = saccount.c_str();
×
1471

1472
  return true;
×
1473
}
×
1474

1475
BB2DomainInfo Bind2Backend::createDomainEntry(const ZoneName& domain, const string& filename)
1476
{
6✔
1477
  domainid_t newid = 1;
6✔
1478
  { // Find a free zone id nr.
6✔
1479
    auto state = s_state.read_lock();
6✔
1480
    if (!state->empty()) {
6!
1481
      newid = state->rbegin()->d_id + 1;
6✔
1482
    }
6✔
1483
  }
6✔
1484

1485
  BB2DomainInfo bbd;
6✔
1486
  bbd.d_kind = DomainInfo::Native;
6✔
1487
  bbd.d_id = newid;
6✔
1488
  bbd.d_records = std::make_shared<recordstorage_t>();
6✔
1489
  bbd.d_name = domain;
6✔
1490
  bbd.setCheckInterval(getArgAsNum("check-interval"));
6✔
1491
  bbd.d_filename = filename;
6✔
1492

1493
  return bbd;
6✔
1494
}
6✔
1495

1496
bool Bind2Backend::createSecondaryDomain(const string& ipAddress, const ZoneName& domain, const string& /* nameserver */, const string& account)
1497
{
×
1498
  string filename = getArg("autoprimary-destdir") + '/' + domain.toStringNoDot();
×
1499

1500
  g_log << Logger::Warning << d_logprefix
×
1501
        << " Writing bind config zone statement for superslave zone '" << domain
×
1502
        << "' from autoprimary " << ipAddress << endl;
×
1503

1504
  {
×
1505
    auto lock = std::scoped_lock(s_autosecondary_config_lock);
×
1506

1507
    ofstream c_of(getArg("autoprimary-config").c_str(), std::ios::app);
×
1508
    if (!c_of) {
×
1509
      g_log << Logger::Error << "Unable to open autoprimary configfile for append: " << stringerror() << endl;
×
1510
      throw DBException("Unable to open autoprimary configfile for append: " + stringerror());
×
1511
    }
×
1512

1513
    c_of << endl;
×
1514
    c_of << "# AutoSecondary zone '" << domain.toString() << "' (added: " << nowTime() << ") (account: " << account << ')' << endl;
×
1515
    c_of << "zone \"" << domain.toStringNoDot() << "\" {" << endl;
×
1516
    c_of << "\ttype secondary;" << endl;
×
1517
    c_of << "\tfile \"" << filename << "\";" << endl;
×
1518
    c_of << "\tprimaries { " << ipAddress << "; };" << endl;
×
1519
    c_of << "};" << endl;
×
1520
    c_of.close();
×
1521
  }
×
1522

1523
  BB2DomainInfo bbd = createDomainEntry(domain, filename);
×
1524
  bbd.d_kind = DomainInfo::Secondary;
×
1525
  bbd.d_primaries.emplace_back(ComboAddress(ipAddress, 53));
×
1526
  bbd.setCtime();
×
1527
  safePutBBDomainInfo(bbd);
×
1528

1529
  return true;
×
1530
}
×
1531

1532
bool Bind2Backend::searchRecords(const string& pattern, size_t maxResults, vector<DNSResourceRecord>& result)
1533
{
23✔
1534
  SimpleMatch sm(pattern, true);
23✔
1535
  static bool mustlog = ::arg().mustDo("query-logging");
23✔
1536
  if (mustlog)
23!
1537
    g_log << Logger::Warning << "Search for pattern '" << pattern << "'" << endl;
×
1538

1539
  {
23✔
1540
    auto state = s_state.read_lock();
23✔
1541

1542
    for (const auto& i : *state) {
23!
1543
      BB2DomainInfo h;
×
1544
      if (!safeGetBBDomainInfo(i.d_id, &h)) {
×
1545
        continue;
×
1546
      }
×
1547

1548
      if (!h.d_loaded) {
×
1549
        continue;
×
1550
      }
×
1551

1552
      shared_ptr<const recordstorage_t> rhandle = h.d_records.get();
×
1553

1554
      for (recordstorage_t::const_iterator ri = rhandle->begin(); result.size() < maxResults && ri != rhandle->end(); ri++) {
×
1555
        const DNSName& domainName(i.d_name);
×
1556
        DNSName name = ri->qname.empty() ? domainName : (ri->qname + domainName);
×
1557
        if (sm.match(name) || sm.match(ri->content)) {
×
1558
          DNSResourceRecord r;
×
1559
          r.qname = std::move(name);
×
1560
          r.domain_id = i.d_id;
×
1561
          r.content = ri->content;
×
1562
          r.qtype = ri->qtype;
×
1563
          r.ttl = ri->ttl;
×
1564
          r.auth = ri->auth;
×
1565
          result.push_back(std::move(r));
×
1566
        }
×
1567
      }
×
1568
    }
×
1569
  }
23✔
1570

1571
  return true;
23✔
1572
}
23✔
1573

1574
class Bind2Factory : public BackendFactory
1575
{
1576
public:
1577
  Bind2Factory() :
1578
    BackendFactory("bind") {}
5,861✔
1579

1580
  void declareArguments(const string& suffix = "") override
1581
  {
559✔
1582
    declare(suffix, "ignore-broken-records", "Ignore records that are out-of-bound for the zone.", "no");
559✔
1583
    declare(suffix, "config", "Location of named.conf", "");
559✔
1584
    declare(suffix, "check-interval", "Interval for zonefile changes", "0");
559✔
1585
    declare(suffix, "autoprimary-config", "Location of (part of) named.conf where pdns can write zone-statements to", "");
559✔
1586
    declare(suffix, "autoprimaries", "List of IP-addresses of autoprimaries", "");
559✔
1587
    declare(suffix, "autoprimary-destdir", "Destination directory for newly added secondary zones", ::arg()["config-dir"]);
559✔
1588
    declare(suffix, "dnssec-db", "Filename to store & access our DNSSEC metadatabase, empty for none", "");
559✔
1589
    declare(suffix, "dnssec-db-journal-mode", "SQLite3 journal mode", "WAL");
559✔
1590
    declare(suffix, "hybrid", "Store DNSSEC metadata in other backend", "no");
559✔
1591
  }
559✔
1592

1593
  DNSBackend* make(const string& suffix = "") override
1594
  {
2,320✔
1595
    assertEmptySuffix(suffix);
2,320✔
1596
    return new Bind2Backend(suffix);
2,320✔
1597
  }
2,320✔
1598

1599
  DNSBackend* makeMetadataOnly(const string& suffix = "") override
1600
  {
901✔
1601
    assertEmptySuffix(suffix);
901✔
1602
    return new Bind2Backend(suffix, false);
901✔
1603
  }
901✔
1604

1605
private:
1606
  void assertEmptySuffix(const string& suffix)
1607
  {
3,221✔
1608
    if (!suffix.empty())
3,221!
1609
      throw PDNSException("launch= suffixes are not supported on the bindbackend");
×
1610
  }
3,221✔
1611
};
1612

1613
//! Magic class that is activated when the dynamic library is loaded
1614
class Bind2Loader
1615
{
1616
public:
1617
  Bind2Loader()
1618
  {
5,861✔
1619
    BackendMakers().report(std::make_unique<Bind2Factory>());
5,861✔
1620
    g_log << Logger::Info << "[bind2backend] This is the bind backend version " << VERSION
5,861✔
1621
#ifndef REPRODUCIBLE
5,861✔
1622
          << " (" __DATE__ " " __TIME__ ")"
5,861✔
1623
#endif
5,861✔
1624
#ifdef HAVE_SQLITE3
5,861✔
1625
          << " (with bind-dnssec-db support)"
5,861✔
1626
#endif
5,861✔
1627
          << " reporting" << endl;
5,861✔
1628
  }
5,861✔
1629
};
1630
static Bind2Loader bind2loader;
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

© 2025 Coveralls, Inc