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

PowerDNS / pdns / 18370591226

09 Oct 2025 08:40AM UTC coverage: 64.094% (-0.04%) from 64.136%
18370591226

Pull #16224

github

web-flow
Merge b58891300 into 152db0df0
Pull Request #16224: dnsdist: Fix a typo in the XSK documentation

42757 of 101504 branches covered (42.12%)

Branch coverage included in aggregate %.

129859 of 167814 relevant lines covered (77.38%)

5755713.48 hits per line

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

61.8
/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,521✔
87
  d_loaded = false;
15,521✔
88
  d_lastcheck = 0;
15,521✔
89
  d_checknow = false;
15,521✔
90
  d_status = "Unknown";
15,521✔
91
}
15,521✔
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,126✔
100
  if (d_checknow) {
4,126✔
101
    return false;
6✔
102
  }
6✔
103

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

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

110
  d_lastcheck = time(nullptr);
×
111
  // Our data is still current if all the files it has been obtained from have
112
  // their modification times unchanged since the last parse.
113
  return std::all_of(d_fileinfo.cbegin(), d_fileinfo.cend(),
×
114
                     [](const auto& fileinfo) { return getCtime(fileinfo.first) == fileinfo.second; });
×
115
}
×
116

117
time_t BB2DomainInfo::getCtime(const std::string& filename)
118
{
×
119
  struct stat buf;
×
120

121
  if (filename.empty() || stat(filename.c_str(), &buf) < 0) {
×
122
    return 0;
×
123
  }
×
124
  return buf.st_ctime;
×
125
}
×
126

127
void BB2DomainInfo::updateCtime()
128
{
×
129
  struct stat buf;
×
130

131
  for (auto& fileinfo : d_fileinfo) {
×
132
    if (stat(fileinfo.first.c_str(), &buf) < 0) {
×
133
      buf.st_ctime = 0;
×
134
    }
×
135
    fileinfo.second = buf.st_ctime;
×
136
  }
×
137
}
×
138

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

151
bool Bind2Backend::safeGetBBDomainInfo(const ZoneName& name, BB2DomainInfo* bbd)
152
{
4,315✔
153
  auto state = s_state.read_lock();
4,315✔
154
  const auto& nameindex = boost::multi_index::get<NameTag>(*state);
4,315✔
155
  auto iter = nameindex.find(name);
4,315✔
156
  if (iter == nameindex.end()) {
4,315✔
157
    return false;
3,268✔
158
  }
3,268✔
159
  *bbd = *iter;
1,047✔
160
  return true;
1,047✔
161
}
4,315✔
162

163
bool Bind2Backend::safeRemoveBBDomainInfo(const ZoneName& name)
164
{
×
165
  auto state = s_state.write_lock();
×
166
  using nameindex_t = state_t::index<NameTag>::type;
×
167
  nameindex_t& nameindex = boost::multi_index::get<NameTag>(*state);
×
168

169
  nameindex_t::iterator iter = nameindex.find(name);
×
170
  if (iter == nameindex.end()) {
×
171
    return false;
×
172
  }
×
173
  nameindex.erase(iter);
×
174
  return true;
×
175
}
×
176

177
void Bind2Backend::safePutBBDomainInfo(const BB2DomainInfo& bbd)
178
{
2,815✔
179
  auto state = s_state.write_lock();
2,815✔
180
  replacing_insert(*state, bbd);
2,815✔
181
}
2,815✔
182

183
// NOLINTNEXTLINE(readability-identifier-length)
184
void Bind2Backend::setNotified(domainid_t id, uint32_t serial)
185
{
×
186
  BB2DomainInfo bbd;
×
187
  if (!safeGetBBDomainInfo(id, &bbd))
×
188
    return;
×
189
  bbd.d_lastnotified = serial;
×
190
  safePutBBDomainInfo(bbd);
×
191
}
×
192

193
// NOLINTNEXTLINE(readability-identifier-length)
194
void Bind2Backend::setLastCheck(domainid_t domain_id, time_t lastcheck)
195
{
72✔
196
  BB2DomainInfo bbd;
72✔
197
  if (safeGetBBDomainInfo(domain_id, &bbd)) {
72!
198
    bbd.d_lastcheck = lastcheck;
72✔
199
    safePutBBDomainInfo(bbd);
72✔
200
  }
72✔
201
}
72✔
202

203
void Bind2Backend::setStale(domainid_t domain_id)
204
{
×
205
  Bind2Backend::setLastCheck(domain_id, 0);
×
206
}
×
207

208
void Bind2Backend::setFresh(domainid_t domain_id)
209
{
72✔
210
  Bind2Backend::setLastCheck(domain_id, time(nullptr));
72✔
211
}
72✔
212

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

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

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

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

250
    return true;
72✔
251
  }
72✔
252
  return false;
×
253
}
72✔
254

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

264
  BB2DomainInfo bbd;
72✔
265
  if (safeGetBBDomainInfo(d_transaction_id, &bbd)) {
72!
266
    if (rename(d_transaction_tmpname.c_str(), bbd.main_filename().c_str()) < 0) {
72!
267
      throw DBException("Unable to commit (rename to: '" + bbd.main_filename() + "') AXFRed zone: " + stringerror());
×
268
    }
×
269
    queueReloadAndStore(bbd.d_id);
72✔
270
  }
72✔
271

272
  d_transaction_id = UnknownDomainID;
72✔
273

274
  return true;
72✔
275
}
72✔
276

277
bool Bind2Backend::abortTransaction()
278
{
×
279
  // d_transaction_id is only set to a valid domain id if we are actually
280
  // setting up a replacement zone file with the updated data.
281
  if (d_transaction_id != UnknownDomainID) {
×
282
    unlink(d_transaction_tmpname.c_str());
×
283
    d_of.reset();
×
284
    d_transaction_id = UnknownDomainID;
×
285
  }
×
286

287
  return true;
×
288
}
×
289

290
static bool ciEqual(const string& lhs, const string& rhs)
291
{
432✔
292
  if (lhs.size() != rhs.size()) {
432✔
293
    return false;
408✔
294
  }
408✔
295

296
  string::size_type pos = 0;
24✔
297
  const string::size_type epos = lhs.size();
24✔
298
  for (; pos < epos; ++pos) {
384✔
299
    if (dns_tolower(lhs[pos]) != dns_tolower(rhs[pos])) {
368✔
300
      return false;
8✔
301
    }
8✔
302
  }
368✔
303
  return true;
16✔
304
}
24✔
305

306
/** does domain end on suffix? Is smart about "wwwds9a.nl" "ds9a.nl" not matching */
307
static bool endsOn(const string& domain, const string& suffix)
308
{
432✔
309
  if (suffix.empty() || ciEqual(domain, suffix)) {
432!
310
    return true;
16✔
311
  }
16✔
312

313
  if (domain.size() <= suffix.size()) {
416✔
314
    return false;
52✔
315
  }
52✔
316

317
  string::size_type dpos = domain.size() - suffix.size() - 1;
364✔
318
  string::size_type spos = 0;
364✔
319

320
  if (domain[dpos++] != '.') {
364✔
321
    return false;
88✔
322
  }
88✔
323

324
  for (; dpos < domain.size(); ++dpos, ++spos) {
3,928✔
325
    if (dns_tolower(domain[dpos]) != dns_tolower(suffix[spos])) {
3,664✔
326
      return false;
12✔
327
    }
12✔
328
  }
3,664✔
329

330
  return true;
264✔
331
}
276✔
332

333
/** strips a domain suffix from a domain, returns true if it stripped */
334
static bool stripDomainSuffix(string* qname, const ZoneName& zonename)
335
{
432✔
336
  std::string domain = zonename.operator const DNSName&().toString();
432✔
337

338
  if (!endsOn(*qname, domain)) {
432✔
339
    return false;
152✔
340
  }
152✔
341

342
  if (toLower(*qname) == toLower(domain)) {
280✔
343
    *qname = "@";
16✔
344
  }
16✔
345
  else {
264✔
346
    if ((*qname)[qname->size() - domain.size() - 1] != '.') {
264!
347
      return false;
×
348
    }
×
349

350
    qname->resize(qname->size() - domain.size() - 1);
264✔
351
  }
264✔
352
  return true;
280✔
353
}
280✔
354

355
bool Bind2Backend::feedRecord(const DNSResourceRecord& rr, const DNSName& /* ordername */, bool /* ordernameIsNSEC3 */)
356
{
202,785✔
357
  if (d_transaction_id == UnknownDomainID) {
202,785!
358
    throw DBException("Bind2Backend::feedRecord() called outside of transaction");
×
359
  }
×
360

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

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

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

398
void Bind2Backend::getUpdatedPrimaries(vector<DomainInfo>& changedDomains, std::unordered_set<DNSName>& /* catalogs */, CatalogHashMap& /* catalogHashes */)
399
{
×
400
  vector<DomainInfo> consider;
×
401
  {
×
402
    auto state = s_state.read_lock();
×
403

404
    for (const auto& i : *state) {
×
405
      if (i.d_kind != DomainInfo::Primary && this->alsoNotify.empty() && i.d_also_notify.empty())
×
406
        continue;
×
407

408
      DomainInfo di;
×
409
      di.id = i.d_id;
×
410
      di.zone = i.d_name;
×
411
      di.last_check = i.d_lastcheck;
×
412
      di.notified_serial = i.d_lastnotified;
×
413
      di.backend = this;
×
414
      di.kind = DomainInfo::Primary;
×
415
      consider.push_back(std::move(di));
×
416
    }
×
417
  }
×
418

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

442
void Bind2Backend::getAllDomains(vector<DomainInfo>* domains, bool getSerial, bool /* include_disabled */)
443
{
98✔
444
  SOAData soadata;
98✔
445

446
  // prevent deadlock by using getSOA() later on
447
  {
98✔
448
    auto state = s_state.read_lock();
98✔
449
    domains->reserve(state->size());
98✔
450

451
    for (const auto& i : *state) {
258✔
452
      DomainInfo di;
185✔
453
      di.id = i.d_id;
185✔
454
      di.zone = i.d_name;
185✔
455
      di.last_check = i.d_lastcheck;
185✔
456
      di.kind = i.d_kind;
185✔
457
      di.primaries = i.d_primaries;
185✔
458
      di.backend = this;
185✔
459
      domains->push_back(std::move(di));
185✔
460
    };
185✔
461
  }
98✔
462

463
  if (getSerial) {
98✔
464
    for (DomainInfo& di : *domains) {
1,382✔
465
      // do not corrupt di if domain supplied by another backend.
466
      if (di.backend != this)
1,382!
467
        continue;
1,382✔
468
      try {
×
469
        this->getSOA(di.zone, di.id, soadata);
×
470
      }
×
471
      catch (...) {
×
472
        continue;
×
473
      }
×
474
      di.serial = soadata.serial;
×
475
    }
×
476
  }
42✔
477
}
98✔
478

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

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

516
bool Bind2Backend::getDomainInfo(const ZoneName& domain, DomainInfo& info, bool getSerial)
517
{
1,021✔
518
  BB2DomainInfo bbd;
1,021✔
519
  if (!safeGetBBDomainInfo(domain, &bbd))
1,021✔
520
    return false;
576✔
521

522
  info.id = bbd.d_id;
445✔
523
  info.zone = domain;
445✔
524
  info.primaries = bbd.d_primaries;
445✔
525
  info.last_check = bbd.d_lastcheck;
445✔
526
  info.backend = this;
445✔
527
  info.kind = bbd.d_kind;
445✔
528
  info.serial = 0;
445✔
529
  if (getSerial) {
445✔
530
    try {
135✔
531
      SOAData sd;
135✔
532
      sd.serial = 0;
135✔
533

534
      getSOA(bbd.d_name, bbd.d_id, sd); // we might not *have* a SOA yet
135✔
535
      info.serial = sd.serial;
135✔
536
    }
135✔
537
    catch (...) {
135✔
538
    }
72✔
539
  }
135✔
540

541
  return true;
445✔
542
}
445✔
543

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

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

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

590
    insertRecord(records, bbd->d_name, rr.qname, rr.qtype, rr.content, rr.ttl, "");
3,274,386✔
591
  }
3,274,386✔
592
  fixupOrderAndAuth(records, bbd->d_name, nsec3zone, ns3pr);
2,737✔
593
  doEmptyNonTerminals(records, bbd->d_name, nsec3zone, ns3pr);
2,737✔
594
  bbd->d_fileinfo = zpt.getFileset();
2,737✔
595
  bbd->d_loaded = true;
2,737✔
596
  bbd->d_checknow = false;
2,737✔
597
  bbd->d_status = "parsed into memory at " + nowTime();
2,737✔
598
  bbd->d_records = LookButDontTouch<recordstorage_t>(std::move(records));
2,737✔
599
  bbd->d_nsec3zone = nsec3zone;
2,737✔
600
  bbd->d_nsec3param = std::move(ns3pr);
2,737✔
601
}
2,737✔
602

603
/** THIS IS AN INTERNAL FUNCTION! It does moadnsparser prio impedance matching
604
    Much of the complication is due to the efforts to benefit from std::string reference counting copy on write semantics */
605
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)
606
{
3,279,002✔
607
  Bind2DNSRecord bdr;
3,279,002✔
608
  bdr.qname = qname;
3,279,002✔
609

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

623
  //  bdr.qname.swap(bdr.qname);
624

625
  if (!records->empty() && bdr.qname == boost::prior(records->end())->qname)
3,278,700✔
626
    bdr.qname = boost::prior(records->end())->qname;
8,889✔
627

628
  bdr.qname = bdr.qname;
3,278,700✔
629
  bdr.qtype = qtype.getCode();
3,278,700✔
630
  bdr.content = content;
3,278,700✔
631
  bdr.nsec3hash = hashed;
3,278,700✔
632

633
  if (auth != nullptr) // Set auth on empty non-terminals
3,278,700✔
634
    bdr.auth = *auth;
4,616✔
635
  else
3,274,084✔
636
    bdr.auth = true;
3,274,084✔
637

638
  bdr.ttl = ttl;
3,278,700✔
639
  records->insert(std::move(bdr));
3,278,700✔
640
}
3,278,700✔
641

642
string Bind2Backend::DLReloadNowHandler(const vector<string>& parts, Utility::pid_t /* ppid */)
643
{
×
644
  ostringstream ret;
×
645

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

667
string Bind2Backend::DLDomStatusHandler(const vector<string>& parts, Utility::pid_t /* ppid */)
668
{
29✔
669
  ostringstream ret;
29✔
670

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

689
  if (ret.str().empty())
29!
690
    ret << "no domains passed";
×
691

692
  return ret.str();
29✔
693
}
29✔
694

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

729
string Bind2Backend::DLDomExtendedStatusHandler(const vector<string>& parts, Utility::pid_t /* ppid */)
730
{
×
731
  ostringstream ret;
×
732

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

751
  if (ret.str().empty()) {
×
752
    ret << "no domains passed" << std::endl;
×
753
  }
×
754

755
  return ret.str();
×
756
}
×
757

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

769
string Bind2Backend::DLAddDomainHandler(const vector<string>& parts, Utility::pid_t /* ppid */)
770
{
12✔
771
  if (parts.size() < 3)
12!
772
    return "ERROR: Domain name and zone filename are required";
×
773

774
  ZoneName domainname(parts[1]);
12✔
775
  const string& filename = parts[2];
12✔
776
  BB2DomainInfo bbd;
12✔
777
  if (safeGetBBDomainInfo(domainname, &bbd))
12✔
778
    return "Already loaded";
6✔
779

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

783
  struct stat buf;
6✔
784
  if (stat(filename.c_str(), &buf) != 0)
6!
785
    return "Unable to load zone " + domainname.toLogString() + " from " + filename + ": " + strerror(errno);
×
786

787
  Bind2Backend bb2; // createdomainentry needs access to our configuration
6✔
788
  bbd = bb2.createDomainEntry(domainname);
6✔
789
  bbd.d_fileinfo.emplace_back(std::make_pair(filename, buf.st_ctime));
6✔
790
  bbd.d_checknow = true;
6✔
791
  bbd.d_loaded = true;
6✔
792
  bbd.d_lastcheck = 0;
6✔
793
  bbd.d_status = "parsing into memory";
6✔
794

795
  safePutBBDomainInfo(bbd);
6✔
796

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

799
  g_log << Logger::Warning << "Zone " << domainname << " loaded" << endl;
6✔
800
  return "Loaded zone " + domainname.toLogString() + " from " + filename;
6✔
801
}
6✔
802

803
Bind2Backend::Bind2Backend(const string& suffix, bool loadZones)
804
{
3,272✔
805
  d_getAllDomainMetadataQuery_stmt = nullptr;
3,272✔
806
  d_getDomainMetadataQuery_stmt = nullptr;
3,272✔
807
  d_deleteDomainMetadataQuery_stmt = nullptr;
3,272✔
808
  d_insertDomainMetadataQuery_stmt = nullptr;
3,272✔
809
  d_getDomainKeysQuery_stmt = nullptr;
3,272✔
810
  d_deleteDomainKeyQuery_stmt = nullptr;
3,272✔
811
  d_insertDomainKeyQuery_stmt = nullptr;
3,272✔
812
  d_GetLastInsertedKeyIdQuery_stmt = nullptr;
3,272✔
813
  d_activateDomainKeyQuery_stmt = nullptr;
3,272✔
814
  d_deactivateDomainKeyQuery_stmt = nullptr;
3,272✔
815
  d_getTSIGKeyQuery_stmt = nullptr;
3,272✔
816
  d_setTSIGKeyQuery_stmt = nullptr;
3,272✔
817
  d_deleteTSIGKeyQuery_stmt = nullptr;
3,272✔
818
  d_getTSIGKeysQuery_stmt = nullptr;
3,272✔
819

820
  setArgPrefix("bind" + suffix);
3,272✔
821
  d_logprefix = "[bind" + suffix + "backend]";
3,272✔
822
  d_hybrid = mustDo("hybrid");
3,272✔
823
  if (d_hybrid && g_zoneCache.isEnabled()) {
3,272!
824
    throw PDNSException("bind-hybrid and the zone cache currently interoperate badly. Please disable the zone cache or stop using bind-hybrid");
×
825
  }
×
826

827
  d_transaction_id = UnknownDomainID;
3,272✔
828
  s_ignore_broken_records = mustDo("ignore-broken-records");
3,272✔
829
  d_upgradeContent = ::arg().mustDo("upgrade-unknown-types");
3,272✔
830

831
  if (!loadZones && d_hybrid)
3,272!
832
    return;
×
833

834
  auto lock = std::scoped_lock(s_startup_lock);
3,272✔
835

836
  setupDNSSEC();
3,272✔
837
  if (s_first == 0) {
3,272✔
838
    return;
2,517✔
839
  }
2,517✔
840

841
  if (loadZones) {
755✔
842
    loadConfig();
311✔
843
    s_first = 0;
311✔
844
  }
311✔
845

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

853
Bind2Backend::~Bind2Backend()
854
{
3,148✔
855
  freeStatements();
3,148✔
856
} // deallocate statements
3,148✔
857

858
void Bind2Backend::rediscover(string* status)
859
{
×
860
  loadConfig(status);
×
861
}
×
862

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

871
void Bind2Backend::fixupOrderAndAuth(std::shared_ptr<recordstorage_t>& records, const ZoneName& zoneName, bool nsec3zone, const NSEC3PARAMRecordContent& ns3pr)
872
{
2,665✔
873
  bool skip;
2,665✔
874
  DNSName shorter;
2,665✔
875
  set<DNSName> nssets, dssets;
2,665✔
876

877
  for (const auto& bdr : *records) {
3,274,084✔
878
    if (!bdr.qname.isRoot() && bdr.qtype == QType::NS)
3,274,084✔
879
      nssets.insert(bdr.qname);
3,010✔
880
    else if (bdr.qtype == QType::DS)
3,271,074✔
881
      dssets.insert(bdr.qname);
499✔
882
  }
3,274,084✔
883

884
  for (auto iter = records->begin(); iter != records->end(); iter++) {
3,276,749✔
885
    skip = false;
3,274,084✔
886
    shorter = iter->qname;
3,274,084✔
887

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

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

899
    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✔
900
      Bind2DNSRecord bdr = *iter;
1,439,445✔
901
      bdr.nsec3hash = toBase32Hex(hashQNameWithSalt(ns3pr, bdr.qname + zoneName.operator const DNSName&()));
1,439,445✔
902
      records->replace(iter, bdr);
1,439,445✔
903
    }
1,439,445✔
904

905
    // cerr<<iter->qname<<"\t"<<QType(iter->qtype).toString()<<"\t"<<iter->nsec3hash<<"\t"<<iter->auth<<endl;
906
  }
3,274,084✔
907
}
2,665✔
908

909
void Bind2Backend::doEmptyNonTerminals(std::shared_ptr<recordstorage_t>& records, const ZoneName& zoneName, bool nsec3zone, const NSEC3PARAMRecordContent& ns3pr)
910
{
2,665✔
911
  bool auth = false;
2,665✔
912
  DNSName shorter;
2,665✔
913
  std::unordered_set<DNSName> qnames;
2,665✔
914
  std::unordered_map<DNSName, bool> nonterm;
2,665✔
915

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

918
  for (const auto& bdr : *records)
2,665✔
919
    qnames.insert(bdr.qname);
3,274,084✔
920

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

923
    if (!bdr.auth && bdr.qtype == QType::NS)
3,274,084✔
924
      auth = (!nsec3zone || (ns3pr.d_flags == 0u));
3,010✔
925
    else
3,271,074✔
926
      auth = bdr.auth;
3,271,074✔
927

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

936
        if (nonterm.count(shorter) == 0u) {
9,422✔
937
          nonterm.emplace(shorter, auth);
4,616✔
938
          --maxent;
4,616✔
939
        }
4,616✔
940
        else if (auth)
4,806✔
941
          nonterm[shorter] = true;
4,710✔
942
      }
9,422✔
943
    }
3,273,900✔
944
  }
3,274,084✔
945

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

957
    // cerr<<rr.qname<<"\t"<<rr.qtype.toString()<<"\t"<<hashed<<"\t"<<nt.second<<endl;
958
  }
4,616✔
959
}
2,665✔
960

961
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
962
{
311✔
963
  static domainid_t domain_id = 1;
311✔
964

965
  if (!getArg("config").empty()) {
311!
966
    BindParser BP;
311✔
967
    try {
311✔
968
      BP.parse(getArg("config"));
311✔
969
    }
311✔
970
    catch (PDNSException& ae) {
311✔
971
      g_log << Logger::Error << "Error parsing bind configuration: " << ae.reason << endl;
×
972
      throw;
×
973
    }
×
974

975
    vector<BindDomainInfo> domains = BP.getDomains();
311✔
976
    this->alsoNotify = BP.getAlsoNotify();
311✔
977

978
    s_binddirectory = BP.getDirectory();
311✔
979
    //    ZP.setDirectory(d_binddirectory);
980

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

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

994
    struct stat st;
311✔
995

996
    for (auto& domain : domains) {
2,791✔
997
      if (stat(domain.filename.c_str(), &st) == 0) {
2,659✔
998
        domain.d_dev = st.st_dev;
2,587✔
999
        domain.d_ino = st.st_ino;
2,587✔
1000
      }
2,587✔
1001
    }
2,659✔
1002

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

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

1020
      BB2DomainInfo bbd;
2,659✔
1021
      bool isNew = false;
2,659✔
1022

1023
      if (!safeGetBBDomainInfo(domain.name, &bbd)) {
2,659!
1024
        isNew = true;
2,659✔
1025
        bbd.d_id = domain_id++;
2,659✔
1026
        bbd.setCheckInterval(getArgAsNum("check-interval"));
2,659✔
1027
        bbd.d_lastnotified = 0;
2,659✔
1028
        bbd.d_loaded = false;
2,659✔
1029
      }
2,659✔
1030

1031
      // overwrite what we knew about the domain
1032
      bbd.d_name = domain.name;
2,659✔
1033
      bool filenameChanged = bbd.d_fileinfo.empty() || (bbd.main_filename() != domain.filename);
2,659!
1034
      bool addressesChanged = (bbd.d_primaries != domain.primaries || bbd.d_also_notify != domain.alsoNotify);
2,659!
1035
      // Preserve existing fileinfo in case we won't reread anything.
1036
      if (filenameChanged) {
2,659!
1037
        bbd.d_fileinfo.clear();
2,659✔
1038
        bbd.d_fileinfo.emplace_back(std::make_pair(domain.filename, 0));
2,659✔
1039
      }
2,659✔
1040
      bbd.d_primaries = domain.primaries;
2,659✔
1041
      bbd.d_also_notify = domain.alsoNotify;
2,659✔
1042

1043
      DomainInfo::DomainKind kind = DomainInfo::Native;
2,659✔
1044
      if (domain.type == "primary" || domain.type == "master") {
2,659!
1045
        kind = DomainInfo::Primary;
2,587✔
1046
      }
2,587✔
1047
      if (domain.type == "secondary" || domain.type == "slave") {
2,659!
1048
        kind = DomainInfo::Secondary;
72✔
1049
      }
72✔
1050

1051
      bool kindChanged = (bbd.d_kind != kind);
2,659✔
1052
      bbd.d_kind = kind;
2,659✔
1053

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

1058
        try {
2,659✔
1059
          parseZoneFile(&bbd);
2,659✔
1060
        }
2,659✔
1061
        catch (PDNSException& ae) {
2,659✔
1062
          ostringstream msg;
×
1063
          msg << " error at " + nowTime() + " parsing '" << domain.name << "' from file '" << domain.filename << "': " << ae.reason;
×
1064

1065
          if (status != nullptr)
×
1066
            *status += msg.str();
×
1067
          bbd.d_status = msg.str();
×
1068

1069
          g_log << Logger::Warning << d_logprefix << msg.str() << endl;
×
1070
          rejected++;
×
1071
        }
×
1072
        catch (std::system_error& ae) {
2,659✔
1073
          ostringstream msg;
72✔
1074
          if (ae.code().value() == ENOENT && isNew && domain.type == "slave")
72!
1075
            msg << " error at " + nowTime() << " no file found for new secondary domain '" << domain.name << "'. Has not been AXFR'd yet";
×
1076
          else
72✔
1077
            msg << " error at " + nowTime() + " parsing '" << domain.name << "' from file '" << domain.filename << "': " << ae.what();
72✔
1078

1079
          if (status != nullptr)
72!
1080
            *status += msg.str();
×
1081
          bbd.d_status = msg.str();
72✔
1082
          g_log << Logger::Warning << d_logprefix << msg.str() << endl;
72✔
1083
          rejected++;
72✔
1084
        }
72✔
1085
        catch (std::exception& ae) {
2,659✔
1086
          ostringstream msg;
×
1087
          msg << " error at " + nowTime() + " parsing '" << domain.name << "' from file '" << domain.filename << "': " << ae.what();
×
1088

1089
          if (status != nullptr)
×
1090
            *status += msg.str();
×
1091
          bbd.d_status = msg.str();
×
1092

1093
          g_log << Logger::Warning << d_logprefix << msg.str() << endl;
×
1094
          rejected++;
×
1095
        }
×
1096
        safePutBBDomainInfo(bbd);
2,659✔
1097
      }
2,659✔
1098
      else if (addressesChanged || kindChanged) {
×
1099
        safePutBBDomainInfo(bbd);
×
1100
      }
×
1101
    }
2,659✔
1102
    vector<ZoneName> diff;
311✔
1103

1104
    set_difference(oldnames.begin(), oldnames.end(), newnames.begin(), newnames.end(), back_inserter(diff));
311✔
1105
    unsigned int remdomains = diff.size();
311✔
1106

1107
    for (const ZoneName& name : diff) {
311!
1108
      safeRemoveBBDomainInfo(name);
×
1109
    }
×
1110

1111
    // count number of entirely new domains
1112
    diff.clear();
311✔
1113
    set_difference(newnames.begin(), newnames.end(), oldnames.begin(), oldnames.end(), back_inserter(diff));
311✔
1114
    newdomains = diff.size();
311✔
1115

1116
    ostringstream msg;
311✔
1117
    msg << " Done parsing domains, " << rejected << " rejected, " << newdomains << " new, " << remdomains << " removed";
311✔
1118
    if (status != nullptr)
311!
1119
      *status = msg.str();
×
1120

1121
    g_log << Logger::Error << d_logprefix << msg.str() << endl;
311✔
1122
  }
311✔
1123
}
311✔
1124

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

1162
bool Bind2Backend::findBeforeAndAfterUnhashed(std::shared_ptr<const recordstorage_t>& records, const DNSName& qname, DNSName& /* unhashed */, DNSName& before, DNSName& after)
1163
{
2,718✔
1164
  // for(const auto& record: *records)
1165
  //   cerr<<record.qname<<"\t"<<makeHexDump(record.qname.toDNSString())<<endl;
1166

1167
  recordstorage_t::const_iterator iterBefore, iterAfter;
2,718✔
1168

1169
  iterBefore = iterAfter = records->upper_bound(qname.makeLowerCase());
2,718✔
1170

1171
  if (iterBefore != records->begin())
2,718!
1172
    --iterBefore;
2,718✔
1173
  while ((!iterBefore->auth && iterBefore->qtype != QType::NS) || !iterBefore->qtype)
3,247✔
1174
    --iterBefore;
529✔
1175
  before = iterBefore->qname;
2,718✔
1176

1177
  if (iterAfter == records->end()) {
2,718✔
1178
    iterAfter = records->begin();
376✔
1179
  }
376✔
1180
  else {
2,342✔
1181
    while ((!iterAfter->auth && iterAfter->qtype != QType::NS) || !iterAfter->qtype) {
3,506✔
1182
      ++iterAfter;
1,164✔
1183
      if (iterAfter == records->end()) {
1,164!
1184
        iterAfter = records->begin();
×
1185
        break;
×
1186
      }
×
1187
    }
1,164✔
1188
  }
2,342✔
1189
  after = iterAfter->qname;
2,718✔
1190

1191
  return true;
2,718✔
1192
}
2,718✔
1193

1194
// NOLINTNEXTLINE(readability-identifier-length)
1195
bool Bind2Backend::getBeforeAndAfterNamesAbsolute(domainid_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after)
1196
{
7,048✔
1197
  BB2DomainInfo bbd;
7,048✔
1198
  if (!safeGetBBDomainInfo(id, &bbd))
7,048!
1199
    return false;
×
1200

1201
  shared_ptr<const recordstorage_t> records = bbd.d_records.get();
7,048✔
1202
  if (!bbd.d_nsec3zone) {
7,048✔
1203
    return findBeforeAndAfterUnhashed(records, qname, unhashed, before, after);
2,718✔
1204
  }
2,718✔
1205
  else {
4,330✔
1206
    const auto& hashindex = boost::multi_index::get<NSEC3Tag>(*records);
4,330✔
1207

1208
    // for(auto iter = first; iter != hashindex.end(); iter++)
1209
    //  cerr<<iter->nsec3hash<<endl;
1210

1211
    auto first = hashindex.upper_bound("");
4,330✔
1212
    auto iter = hashindex.upper_bound(qname.toStringNoDot());
4,330✔
1213

1214
    if (iter == hashindex.end()) {
4,330✔
1215
      --iter;
360✔
1216
      before = DNSName(iter->nsec3hash);
360✔
1217
      after = DNSName(first->nsec3hash);
360✔
1218
    }
360✔
1219
    else {
3,970✔
1220
      after = DNSName(iter->nsec3hash);
3,970✔
1221
      if (iter != first)
3,970✔
1222
        --iter;
3,879✔
1223
      else
91✔
1224
        iter = --hashindex.end();
91✔
1225
      before = DNSName(iter->nsec3hash);
3,970✔
1226
    }
3,970✔
1227
    unhashed = iter->qname + bbd.d_name.operator const DNSName&();
4,330✔
1228

1229
    return true;
4,330✔
1230
  }
4,330✔
1231
}
7,048✔
1232

1233
void Bind2Backend::lookup(const QType& qtype, const DNSName& qname, domainid_t zoneId, DNSPacket* /* pkt_p */)
1234
{
4,150✔
1235
  d_handle.reset();
4,150✔
1236

1237
  static bool mustlog = ::arg().mustDo("query-logging");
4,150✔
1238

1239
  bool found = false;
4,150✔
1240
  ZoneName domain;
4,150✔
1241
  BB2DomainInfo bbd;
4,150✔
1242

1243
  if (mustlog)
4,150!
1244
    g_log << Logger::Warning << "Lookup for '" << qtype.toString() << "' of '" << qname << "' within zoneID " << zoneId << endl;
×
1245

1246
  if (zoneId != UnknownDomainID) {
4,150✔
1247
    if ((found = (safeGetBBDomainInfo(zoneId, &bbd) && qname.isPartOf(bbd.d_name)))) {
3,536!
1248
      domain = std::move(bbd.d_name);
3,536✔
1249
    }
3,536✔
1250
  }
3,536✔
1251
  else {
614✔
1252
    domain = ZoneName(qname);
614✔
1253
    do {
617✔
1254
      found = safeGetBBDomainInfo(domain, &bbd);
617✔
1255
    } while (!found && qtype != QType::SOA && domain.chopOff());
617✔
1256
  }
614✔
1257

1258
  if (!found) {
4,150✔
1259
    if (mustlog)
24!
1260
      g_log << Logger::Warning << "Found no authoritative zone for '" << qname << "' and/or id " << zoneId << endl;
×
1261
    d_handle.d_list = false;
24✔
1262
    return;
24✔
1263
  }
24✔
1264

1265
  if (mustlog)
4,126!
1266
    g_log << Logger::Warning << "Found a zone '" << domain << "' (with id " << bbd.d_id << ") that might contain data " << endl;
×
1267

1268
  d_handle.id = bbd.d_id;
4,126✔
1269
  d_handle.qname = qname.makeRelative(domain); // strip domain name
4,126✔
1270
  d_handle.qtype = qtype;
4,126✔
1271
  d_handle.domain = std::move(domain);
4,126✔
1272

1273
  if (!bbd.current()) {
4,126✔
1274
    g_log << Logger::Warning << "Zone '" << d_handle.domain << "' (" << bbd.main_filename() << ") needs reloading" << endl;
6✔
1275
    queueReloadAndStore(bbd.d_id);
6✔
1276
    if (!safeGetBBDomainInfo(d_handle.domain, &bbd))
6!
1277
      throw DBException("Zone '" + bbd.d_name.toLogString() + "' (" + bbd.main_filename() + ") gone after reload"); // if we don't throw here, we crash for some reason
×
1278
  }
6✔
1279

1280
  if (!bbd.d_loaded) {
4,126✔
1281
    d_handle.reset();
216✔
1282
    throw DBException("Zone for '" + d_handle.domain.toLogString() + "' in '" + bbd.main_filename() + "' not loaded (file missing, corrupt or primary dead)"); // fsck
216✔
1283
  }
216✔
1284

1285
  d_handle.d_records = bbd.d_records.get();
3,910✔
1286

1287
  if (d_handle.d_records->empty())
3,910!
1288
    DLOG(g_log << "Query with no results" << endl);
×
1289

1290
  d_handle.mustlog = mustlog;
3,910✔
1291

1292
  const auto& hashedidx = boost::multi_index::get<UnorderedNameTag>(*d_handle.d_records);
3,910✔
1293
  auto range = hashedidx.equal_range(d_handle.qname);
3,910✔
1294

1295
  d_handle.d_list = false;
3,910✔
1296
  d_handle.d_iter = range.first;
3,910✔
1297
  d_handle.d_end_iter = range.second;
3,910✔
1298
}
3,910✔
1299

1300
Bind2Backend::handle::handle()
1301
{
3,272✔
1302
  mustlog = false;
3,272✔
1303
}
3,272✔
1304

1305
bool Bind2Backend::get(DNSResourceRecord& r)
1306
{
197,928✔
1307
  if (!d_handle.d_records) {
197,928✔
1308
    if (d_handle.mustlog)
24!
1309
      g_log << Logger::Warning << "There were no answers" << endl;
×
1310
    return false;
24✔
1311
  }
24✔
1312

1313
  if (!d_handle.get(r)) {
197,904✔
1314
    if (d_handle.mustlog)
4,215!
1315
      g_log << Logger::Warning << "End of answers" << endl;
×
1316

1317
    d_handle.reset();
4,215✔
1318

1319
    return false;
4,215✔
1320
  }
4,215✔
1321
  if (d_handle.mustlog)
193,689!
1322
    g_log << Logger::Warning << "Returning: '" << r.qtype.toString() << "' of '" << r.qname << "', content: '" << r.content << "'" << endl;
×
1323
  return true;
193,689✔
1324
}
197,904✔
1325

1326
void Bind2Backend::lookupEnd()
1327
{
26✔
1328
  d_handle.reset();
26✔
1329
}
26✔
1330

1331
bool Bind2Backend::handle::get(DNSResourceRecord& r)
1332
{
197,904✔
1333
  if (d_list)
197,904✔
1334
    return get_list(r);
188,900✔
1335
  else
9,004✔
1336
    return get_normal(r);
9,004✔
1337
}
197,904✔
1338

1339
void Bind2Backend::handle::reset()
1340
{
8,938✔
1341
  d_records.reset();
8,938✔
1342
  qname.clear();
8,938✔
1343
  mustlog = false;
8,938✔
1344
}
8,938✔
1345

1346
//#define DLOG(x) x
1347
bool Bind2Backend::handle::get_normal(DNSResourceRecord& r)
1348
{
9,004✔
1349
  DLOG(g_log << "Bind2Backend get() was called for " << qtype.toString() << " record for '" << qname << "' - " << d_records->size() << " available in total!" << endl);
9,004✔
1350

1351
  if (d_iter == d_end_iter) {
9,004✔
1352
    return false;
3,884✔
1353
  }
3,884✔
1354

1355
  while (d_iter != d_end_iter && !(qtype.getCode() == QType::ANY || (d_iter)->qtype == qtype.getCode())) {
7,853!
1356
    DLOG(g_log << Logger::Warning << "Skipped " << qname << "/" << QType(d_iter->qtype).toString() << ": '" << d_iter->content << "'" << endl);
2,733✔
1357
    d_iter++;
2,733✔
1358
  }
2,733✔
1359
  if (d_iter == d_end_iter) {
5,120!
1360
    return false;
×
1361
  }
×
1362
  DLOG(g_log << "Bind2Backend get() returning a rr with a " << QType(d_iter->qtype).getCode() << endl);
5,120✔
1363

1364
  const DNSName& domainName(domain);
5,120✔
1365
  r.qname = qname.empty() ? domainName : (qname + domainName);
5,120!
1366
  r.domain_id = id;
5,120✔
1367
  r.content = (d_iter)->content;
5,120✔
1368
  //  r.domain_id=(d_iter)->domain_id;
1369
  r.qtype = (d_iter)->qtype;
5,120✔
1370
  r.ttl = (d_iter)->ttl;
5,120✔
1371

1372
  //if(!d_iter->auth && r.qtype.getCode() != QType::A && r.qtype.getCode()!=QType::AAAA && r.qtype.getCode() != QType::NS)
1373
  //  cerr<<"Warning! Unauth response for qtype "<< r.qtype.toString() << " for '"<<r.qname<<"'"<<endl;
1374
  r.auth = d_iter->auth;
5,120✔
1375

1376
  d_iter++;
5,120✔
1377

1378
  return true;
5,120✔
1379
}
5,120✔
1380

1381
bool Bind2Backend::list(const ZoneName& /* target */, domainid_t domainId, bool /* include_disabled */)
1382
{
331✔
1383
  BB2DomainInfo bbd;
331✔
1384

1385
  if (!safeGetBBDomainInfo(domainId, &bbd)) {
331!
1386
    return false;
×
1387
  }
×
1388

1389
  d_handle.reset();
331✔
1390
  DLOG(g_log << "Bind2Backend constructing handle for list of " << domainId << endl);
331✔
1391

1392
  if (!bbd.d_loaded) {
331!
1393
    throw PDNSException("zone was not loaded, perhaps because of: " + bbd.d_status);
×
1394
  }
×
1395

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

1400
  d_handle.id = domainId;
331✔
1401
  d_handle.domain = bbd.d_name;
331✔
1402
  d_handle.d_list = true;
331✔
1403
  return true;
331✔
1404
}
331✔
1405

1406
bool Bind2Backend::handle::get_list(DNSResourceRecord& r)
1407
{
188,900✔
1408
  if (d_qname_iter != d_qname_end) {
188,900✔
1409
    const DNSName& domainName(domain);
188,569✔
1410
    r.qname = d_qname_iter->qname.empty() ? domainName : (d_qname_iter->qname + domainName);
188,569!
1411
    r.domain_id = id;
188,569✔
1412
    r.content = (d_qname_iter)->content;
188,569✔
1413
    r.qtype = (d_qname_iter)->qtype;
188,569✔
1414
    r.ttl = (d_qname_iter)->ttl;
188,569✔
1415
    r.auth = d_qname_iter->auth;
188,569✔
1416
    d_qname_iter++;
188,569✔
1417
    return true;
188,569✔
1418
  }
188,569✔
1419
  return false;
331✔
1420
}
188,900✔
1421

1422
bool Bind2Backend::autoPrimariesList(std::vector<AutoPrimary>& primaries)
1423
{
×
1424
  if (getArg("autoprimary-config").empty())
×
1425
    return false;
×
1426

1427
  ifstream c_if(getArg("autoprimaries"), std::ios::in);
×
1428
  if (!c_if) {
×
1429
    g_log << Logger::Error << "Unable to open autoprimaries file for read: " << stringerror() << endl;
×
1430
    return false;
×
1431
  }
×
1432

1433
  string line, sip, saccount;
×
1434
  while (getline(c_if, line)) {
×
1435
    std::istringstream ii(line);
×
1436
    ii >> sip;
×
1437
    if (!sip.empty()) {
×
1438
      ii >> saccount;
×
1439
      primaries.emplace_back(sip, "", saccount);
×
1440
    }
×
1441
  }
×
1442

1443
  c_if.close();
×
1444
  return true;
×
1445
}
×
1446

1447
bool Bind2Backend::autoPrimaryBackend(const string& ipAddress, const ZoneName& /* domain */, const vector<DNSResourceRecord>& /* nsset */, string* /* nameserver */, string* account, DNSBackend** backend)
1448
{
×
1449
  // Check whether we have a configfile available.
1450
  if (getArg("autoprimary-config").empty())
×
1451
    return false;
×
1452

1453
  ifstream c_if(getArg("autoprimaries").c_str(), std::ios::in); // this was nocreate?
×
1454
  if (!c_if) {
×
1455
    g_log << Logger::Error << "Unable to open autoprimaries file for read: " << stringerror() << endl;
×
1456
    return false;
×
1457
  }
×
1458

1459
  // Format:
1460
  // <ip> <accountname>
1461
  string line, sip, saccount;
×
1462
  while (getline(c_if, line)) {
×
1463
    std::istringstream ii(line);
×
1464
    ii >> sip;
×
1465
    if (sip == ipAddress) {
×
1466
      ii >> saccount;
×
1467
      break;
×
1468
    }
×
1469
  }
×
1470
  c_if.close();
×
1471

1472
  if (sip != ipAddress) { // ip not found in authorization list - reject
×
1473
    return false;
×
1474
  }
×
1475

1476
  // ip authorized as autoprimary - accept
1477
  *backend = this;
×
1478
  if (saccount.length() > 0)
×
1479
    *account = saccount.c_str();
×
1480

1481
  return true;
×
1482
}
×
1483

1484
BB2DomainInfo Bind2Backend::createDomainEntry(const ZoneName& domain)
1485
{
6✔
1486
  domainid_t newid = 1;
6✔
1487
  { // Find a free zone id nr.
6✔
1488
    auto state = s_state.read_lock();
6✔
1489
    if (!state->empty()) {
6!
1490
      newid = state->rbegin()->d_id + 1;
6✔
1491
    }
6✔
1492
  }
6✔
1493

1494
  BB2DomainInfo bbd;
6✔
1495
  bbd.d_kind = DomainInfo::Native;
6✔
1496
  bbd.d_id = newid;
6✔
1497
  bbd.d_records = std::make_shared<recordstorage_t>();
6✔
1498
  bbd.d_name = domain;
6✔
1499
  bbd.setCheckInterval(getArgAsNum("check-interval"));
6✔
1500

1501
  return bbd;
6✔
1502
}
6✔
1503

1504
bool Bind2Backend::createSecondaryDomain(const string& ipAddress, const ZoneName& domain, const string& /* nameserver */, const string& account)
1505
{
×
1506
  string filename = getArg("autoprimary-destdir") + '/' + domain.toStringNoDot();
×
1507

1508
  g_log << Logger::Warning << d_logprefix
×
1509
        << " Writing bind config zone statement for superslave zone '" << domain
×
1510
        << "' from autoprimary " << ipAddress << endl;
×
1511

1512
  {
×
1513
    auto lock = std::scoped_lock(s_autosecondary_config_lock);
×
1514

1515
    ofstream c_of(getArg("autoprimary-config").c_str(), std::ios::app);
×
1516
    if (!c_of) {
×
1517
      g_log << Logger::Error << "Unable to open autoprimary configfile for append: " << stringerror() << endl;
×
1518
      throw DBException("Unable to open autoprimary configfile for append: " + stringerror());
×
1519
    }
×
1520

1521
    c_of << endl;
×
1522
    c_of << "# AutoSecondary zone '" << domain.toString() << "' (added: " << nowTime() << ") (account: " << account << ')' << endl;
×
1523
    c_of << "zone \"" << domain.toStringNoDot() << "\" {" << endl;
×
1524
    c_of << "\ttype secondary;" << endl;
×
1525
    c_of << "\tfile \"" << filename << "\";" << endl;
×
1526
    c_of << "\tprimaries { " << ipAddress << "; };" << endl;
×
1527
    c_of << "};" << endl;
×
1528
    c_of.close();
×
1529
  }
×
1530

1531
  BB2DomainInfo bbd = createDomainEntry(domain);
×
1532
  bbd.d_kind = DomainInfo::Secondary;
×
1533
  bbd.d_primaries.emplace_back(ComboAddress(ipAddress, 53));
×
1534
  bbd.d_fileinfo.emplace_back(std::make_pair(filename, 0));
×
1535
  bbd.updateCtime();
×
1536
  safePutBBDomainInfo(bbd);
×
1537

1538
  return true;
×
1539
}
×
1540

1541
bool Bind2Backend::searchRecords(const string& pattern, size_t maxResults, vector<DNSResourceRecord>& result)
1542
{
23✔
1543
  SimpleMatch sm(pattern, true);
23✔
1544
  static bool mustlog = ::arg().mustDo("query-logging");
23✔
1545
  if (mustlog)
23!
1546
    g_log << Logger::Warning << "Search for pattern '" << pattern << "'" << endl;
×
1547

1548
  {
23✔
1549
    auto state = s_state.read_lock();
23✔
1550

1551
    for (const auto& i : *state) {
23!
1552
      BB2DomainInfo h;
×
1553
      if (!safeGetBBDomainInfo(i.d_id, &h)) {
×
1554
        continue;
×
1555
      }
×
1556

1557
      if (!h.d_loaded) {
×
1558
        continue;
×
1559
      }
×
1560

1561
      shared_ptr<const recordstorage_t> rhandle = h.d_records.get();
×
1562

1563
      for (recordstorage_t::const_iterator ri = rhandle->begin(); result.size() < maxResults && ri != rhandle->end(); ri++) {
×
1564
        const DNSName& domainName(i.d_name);
×
1565
        DNSName name = ri->qname.empty() ? domainName : (ri->qname + domainName);
×
1566
        if (sm.match(name) || sm.match(ri->content)) {
×
1567
          DNSResourceRecord r;
×
1568
          r.qname = std::move(name);
×
1569
          r.domain_id = i.d_id;
×
1570
          r.content = ri->content;
×
1571
          r.qtype = ri->qtype;
×
1572
          r.ttl = ri->ttl;
×
1573
          r.auth = ri->auth;
×
1574
          result.push_back(std::move(r));
×
1575
        }
×
1576
      }
×
1577
    }
×
1578
  }
23✔
1579

1580
  return true;
23✔
1581
}
23✔
1582

1583
class Bind2Factory : public BackendFactory
1584
{
1585
public:
1586
  Bind2Factory() :
1587
    BackendFactory("bind") {}
5,869✔
1588

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

1602
  DNSBackend* make(const string& suffix = "") override
1603
  {
2,365✔
1604
    assertEmptySuffix(suffix);
2,365✔
1605
    return new Bind2Backend(suffix);
2,365✔
1606
  }
2,365✔
1607

1608
  DNSBackend* makeMetadataOnly(const string& suffix = "") override
1609
  {
901✔
1610
    assertEmptySuffix(suffix);
901✔
1611
    return new Bind2Backend(suffix, false);
901✔
1612
  }
901✔
1613

1614
private:
1615
  void assertEmptySuffix(const string& suffix)
1616
  {
3,266✔
1617
    if (!suffix.empty())
3,266!
1618
      throw PDNSException("launch= suffixes are not supported on the bindbackend");
×
1619
  }
3,266✔
1620
};
1621

1622
//! Magic class that is activated when the dynamic library is loaded
1623
class Bind2Loader
1624
{
1625
public:
1626
  Bind2Loader()
1627
  {
5,869✔
1628
    BackendMakers().report(std::make_unique<Bind2Factory>());
5,869✔
1629
    g_log << Logger::Info << "[bind2backend] This is the bind backend version " << VERSION
5,869✔
1630
#ifndef REPRODUCIBLE
5,869✔
1631
          << " (" __DATE__ " " __TIME__ ")"
5,869✔
1632
#endif
5,869✔
1633
#ifdef HAVE_SQLITE3
5,869✔
1634
          << " (with bind-dnssec-db support)"
5,869✔
1635
#endif
5,869✔
1636
          << " reporting" << endl;
5,869✔
1637
  }
5,869✔
1638
};
1639
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