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

PowerDNS / pdns / 14652478497

pending completion
14652478497

push

github

web-flow
Merge pull request #15439 from nokia/master

dnsdist: Support DSCP marking towards downstream server

56309 of 150030 branches covered (37.53%)

Branch coverage included in aggregate %.

19 of 26 new or added lines in 3 files covered. (73.08%)

9558 existing lines in 155 files now uncovered.

144757 of 188890 relevant lines covered (76.64%)

6616541.25 hits per line

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

16.22
/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
{
722✔
87
  d_loaded = false;
722✔
88
  d_lastcheck = 0;
722✔
89
  d_checknow = false;
722✔
90
  d_status = "Unknown";
722✔
91
}
722✔
92

93
void BB2DomainInfo::setCheckInterval(time_t seconds)
UNCOV
94
{
×
UNCOV
95
  d_checkinterval = seconds;
×
UNCOV
96
}
×
97

98
bool BB2DomainInfo::current()
UNCOV
99
{
×
UNCOV
100
  if (d_checknow) {
×
UNCOV
101
    return false;
×
UNCOV
102
  }
×
103

UNCOV
104
  if (!d_checkinterval)
×
UNCOV
105
    return true;
×
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()
UNCOV
127
{
×
UNCOV
128
  struct stat buf;
×
UNCOV
129
  if (stat(d_filename.c_str(), &buf) < 0)
×
130
    return;
×
UNCOV
131
  d_ctime = buf.st_ctime;
×
UNCOV
132
}
×
133

134
bool Bind2Backend::safeGetBBDomainInfo(int id, BB2DomainInfo* bbd)
UNCOV
135
{
×
UNCOV
136
  auto state = s_state.read_lock();
×
UNCOV
137
  state_t::const_iterator iter = state->find(id);
×
UNCOV
138
  if (iter == state->end()) {
×
139
    return false;
×
140
  }
×
UNCOV
141
  *bbd = *iter;
×
UNCOV
142
  return true;
×
UNCOV
143
}
×
144

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

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

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

171
void Bind2Backend::safePutBBDomainInfo(const BB2DomainInfo& bbd)
UNCOV
172
{
×
UNCOV
173
  auto state = s_state.write_lock();
×
UNCOV
174
  replacing_insert(*state, bbd);
×
UNCOV
175
}
×
176

177
void Bind2Backend::setNotified(uint32_t id, uint32_t serial)
178
{
×
179
  BB2DomainInfo bbd;
×
180
  if (!safeGetBBDomainInfo(id, &bbd))
×
181
    return;
×
182
  bbd.d_lastnotified = serial;
×
183
  safePutBBDomainInfo(bbd);
×
184
}
×
185

186
void Bind2Backend::setLastCheck(uint32_t domain_id, time_t lastcheck)
UNCOV
187
{
×
UNCOV
188
  BB2DomainInfo bbd;
×
UNCOV
189
  if (safeGetBBDomainInfo(domain_id, &bbd)) {
×
UNCOV
190
    bbd.d_lastcheck = lastcheck;
×
UNCOV
191
    safePutBBDomainInfo(bbd);
×
UNCOV
192
  }
×
UNCOV
193
}
×
194

195
void Bind2Backend::setStale(uint32_t domain_id)
196
{
×
197
  setLastCheck(domain_id, 0);
×
198
}
×
199

200
void Bind2Backend::setFresh(uint32_t domain_id)
UNCOV
201
{
×
UNCOV
202
  setLastCheck(domain_id, time(nullptr));
×
UNCOV
203
}
×
204

205
bool Bind2Backend::startTransaction(const ZoneName& qname, int domainId)
UNCOV
206
{
×
UNCOV
207
  if (domainId < 0) {
×
UNCOV
208
    d_transaction_tmpname.clear();
×
UNCOV
209
    d_transaction_id = domainId;
×
UNCOV
210
    return false;
×
UNCOV
211
  }
×
UNCOV
212
  if (domainId == 0) {
×
213
    throw DBException("domain_id 0 is invalid for this backend.");
×
214
  }
×
215

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

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

UNCOV
237
    *d_of << "; Written by PowerDNS, don't edit!" << endl;
×
UNCOV
238
    *d_of << "; Zone '" << bbd.d_name << "' retrieved from primary " << endl
×
UNCOV
239
          << "; at " << nowTime() << endl; // insert primary info here again
×
240

UNCOV
241
    return true;
×
UNCOV
242
  }
×
243
  return false;
×
UNCOV
244
}
×
245

246
bool Bind2Backend::commitTransaction()
UNCOV
247
{
×
UNCOV
248
  if (d_transaction_id < 0)
×
UNCOV
249
    return false;
×
UNCOV
250
  d_of.reset();
×
251

UNCOV
252
  BB2DomainInfo bbd;
×
UNCOV
253
  if (safeGetBBDomainInfo(d_transaction_id, &bbd)) {
×
UNCOV
254
    if (rename(d_transaction_tmpname.c_str(), bbd.d_filename.c_str()) < 0)
×
255
      throw DBException("Unable to commit (rename to: '" + bbd.d_filename + "') AXFRed zone: " + stringerror());
×
UNCOV
256
    queueReloadAndStore(bbd.d_id);
×
UNCOV
257
  }
×
258

UNCOV
259
  d_transaction_id = 0;
×
260

UNCOV
261
  return true;
×
UNCOV
262
}
×
263

264
bool Bind2Backend::abortTransaction()
265
{
×
266
  // -1 = dnssec speciality
267
  // 0  = invalid transact
268
  // >0 = actual transaction
269
  if (d_transaction_id > 0) {
×
270
    unlink(d_transaction_tmpname.c_str());
×
271
    d_of.reset();
×
272
    d_transaction_id = 0;
×
273
  }
×
274

275
  return true;
×
276
}
×
277

278
bool Bind2Backend::feedRecord(const DNSResourceRecord& rr, const DNSName& /* ordername */, bool /* ordernameIsNSEC3 */)
UNCOV
279
{
×
UNCOV
280
  if (d_transaction_id < 1) {
×
281
    throw DBException("Bind2Backend::feedRecord() called outside of transaction");
×
282
  }
×
283

UNCOV
284
  string qname;
×
UNCOV
285
  if (d_transaction_qname.empty()) {
×
286
    qname = rr.qname.toString();
×
287
  }
×
UNCOV
288
  else if (rr.qname.isPartOf(d_transaction_qname)) {
×
UNCOV
289
    if (rr.qname == d_transaction_qname.operator const DNSName&()) {
×
UNCOV
290
      qname = "@";
×
UNCOV
291
    }
×
UNCOV
292
    else {
×
UNCOV
293
      DNSName relName = rr.qname.makeRelative(d_transaction_qname);
×
UNCOV
294
      qname = relName.toStringNoDot();
×
UNCOV
295
    }
×
UNCOV
296
  }
×
297
  else {
×
298
    throw DBException("out-of-zone data '" + rr.qname.toLogString() + "' during AXFR of zone '" + d_transaction_qname.toLogString() + "'");
×
299
  }
×
300

UNCOV
301
  shared_ptr<DNSRecordContent> drc(DNSRecordContent::make(rr.qtype.getCode(), QClass::IN, rr.content));
×
UNCOV
302
  string content = drc->getZoneRepresentation();
×
303

304
  // SOA needs stripping too! XXX FIXME - also, this should not be here I think
UNCOV
305
  switch (rr.qtype.getCode()) {
×
UNCOV
306
  case QType::MX:
×
UNCOV
307
  case QType::SRV:
×
UNCOV
308
  case QType::CNAME:
×
UNCOV
309
  case QType::DNAME:
×
UNCOV
310
  case QType::NS:
×
UNCOV
311
    stripDomainSuffix(&content, d_transaction_qname.toString());
×
312
    // fallthrough
UNCOV
313
  default:
×
UNCOV
314
    if (d_of && *d_of) {
×
UNCOV
315
      *d_of << qname << "\t" << rr.ttl << "\t" << rr.qtype.toString() << "\t" << content << endl;
×
UNCOV
316
    }
×
UNCOV
317
  }
×
UNCOV
318
  return true;
×
UNCOV
319
}
×
320

321
void Bind2Backend::getUpdatedPrimaries(vector<DomainInfo>& changedDomains, std::unordered_set<DNSName>& /* catalogs */, CatalogHashMap& /* catalogHashes */)
322
{
×
323
  vector<DomainInfo> consider;
×
324
  {
×
325
    auto state = s_state.read_lock();
×
326

327
    for (const auto& i : *state) {
×
328
      if (i.d_kind != DomainInfo::Primary && this->alsoNotify.empty() && i.d_also_notify.empty())
×
329
        continue;
×
330

331
      DomainInfo di;
×
332
      di.id = i.d_id;
×
333
      di.zone = i.d_name;
×
334
      di.last_check = i.d_lastcheck;
×
335
      di.notified_serial = i.d_lastnotified;
×
336
      di.backend = this;
×
337
      di.kind = DomainInfo::Primary;
×
338
      consider.push_back(std::move(di));
×
339
    }
×
340
  }
×
341

342
  SOAData soadata;
×
343
  for (DomainInfo& di : consider) {
×
344
    soadata.serial = 0;
×
345
    try {
×
346
      this->getSOA(di.zone, soadata); // we might not *have* a SOA yet, but this might trigger a load of it
×
347
    }
×
348
    catch (...) {
×
349
      continue;
×
350
    }
×
351
    if (di.notified_serial != soadata.serial) {
×
352
      BB2DomainInfo bbd;
×
353
      if (safeGetBBDomainInfo(di.id, &bbd)) {
×
354
        bbd.d_lastnotified = soadata.serial;
×
355
        safePutBBDomainInfo(bbd);
×
356
      }
×
357
      if (di.notified_serial) { // don't do notification storm on startup
×
358
        di.serial = soadata.serial;
×
359
        changedDomains.push_back(std::move(di));
×
360
      }
×
361
    }
×
362
  }
×
363
}
×
364

365
void Bind2Backend::getAllDomains(vector<DomainInfo>* domains, bool getSerial, bool /* include_disabled */)
366
{
83✔
367
  SOAData soadata;
83✔
368

369
  // prevent deadlock by using getSOA() later on
370
  {
83✔
371
    auto state = s_state.read_lock();
83✔
372
    domains->reserve(state->size());
83✔
373

374
    for (const auto& i : *state) {
83!
UNCOV
375
      DomainInfo di;
×
UNCOV
376
      di.id = i.d_id;
×
UNCOV
377
      di.zone = i.d_name;
×
UNCOV
378
      di.last_check = i.d_lastcheck;
×
UNCOV
379
      di.kind = i.d_kind;
×
UNCOV
380
      di.primaries = i.d_primaries;
×
UNCOV
381
      di.backend = this;
×
UNCOV
382
      domains->push_back(std::move(di));
×
UNCOV
383
    };
×
384
  }
83✔
385

386
  if (getSerial) {
83✔
387
    for (DomainInfo& di : *domains) {
1,651✔
388
      // do not corrupt di if domain supplied by another backend.
389
      if (di.backend != this)
1,651!
390
        continue;
1,651✔
391
      try {
×
392
        this->getSOA(di.zone, soadata);
×
393
      }
×
394
      catch (...) {
×
395
        continue;
×
396
      }
×
397
      di.serial = soadata.serial;
×
398
    }
×
399
  }
50✔
400
}
83✔
401

402
void Bind2Backend::getUnfreshSecondaryInfos(vector<DomainInfo>* unfreshDomains)
UNCOV
403
{
×
UNCOV
404
  vector<DomainInfo> domains;
×
UNCOV
405
  {
×
UNCOV
406
    auto state = s_state.read_lock();
×
UNCOV
407
    domains.reserve(state->size());
×
UNCOV
408
    for (const auto& i : *state) {
×
UNCOV
409
      if (i.d_kind != DomainInfo::Secondary)
×
410
        continue;
×
UNCOV
411
      DomainInfo sd;
×
UNCOV
412
      sd.id = i.d_id;
×
UNCOV
413
      sd.zone = i.d_name;
×
UNCOV
414
      sd.primaries = i.d_primaries;
×
UNCOV
415
      sd.last_check = i.d_lastcheck;
×
UNCOV
416
      sd.backend = this;
×
UNCOV
417
      sd.kind = DomainInfo::Secondary;
×
UNCOV
418
      domains.push_back(std::move(sd));
×
UNCOV
419
    }
×
UNCOV
420
  }
×
UNCOV
421
  unfreshDomains->reserve(domains.size());
×
422

UNCOV
423
  for (DomainInfo& sd : domains) {
×
UNCOV
424
    SOAData soadata;
×
UNCOV
425
    soadata.refresh = 0;
×
UNCOV
426
    soadata.serial = 0;
×
UNCOV
427
    try {
×
UNCOV
428
      getSOA(sd.zone, soadata); // we might not *have* a SOA yet
×
UNCOV
429
    }
×
UNCOV
430
    catch (...) {
×
UNCOV
431
    }
×
UNCOV
432
    sd.serial = soadata.serial;
×
433
    // coverity[store_truncates_time_t]
UNCOV
434
    if (sd.last_check + soadata.refresh < (unsigned int)time(nullptr))
×
UNCOV
435
      unfreshDomains->push_back(std::move(sd));
×
UNCOV
436
  }
×
UNCOV
437
}
×
438

439
bool Bind2Backend::getDomainInfo(const ZoneName& domain, DomainInfo& info, bool getSerial)
440
{
697✔
441
  BB2DomainInfo bbd;
697✔
442
  if (!safeGetBBDomainInfo(domain, &bbd))
697!
443
    return false;
697✔
444

UNCOV
445
  info.id = bbd.d_id;
×
UNCOV
446
  info.zone = domain;
×
UNCOV
447
  info.primaries = bbd.d_primaries;
×
UNCOV
448
  info.last_check = bbd.d_lastcheck;
×
UNCOV
449
  info.backend = this;
×
UNCOV
450
  info.kind = bbd.d_kind;
×
UNCOV
451
  info.serial = 0;
×
UNCOV
452
  if (getSerial) {
×
UNCOV
453
    try {
×
UNCOV
454
      SOAData sd;
×
UNCOV
455
      sd.serial = 0;
×
456

UNCOV
457
      getSOA(bbd.d_name, sd); // we might not *have* a SOA yet
×
UNCOV
458
      info.serial = sd.serial;
×
UNCOV
459
    }
×
UNCOV
460
    catch (...) {
×
UNCOV
461
    }
×
UNCOV
462
  }
×
463

UNCOV
464
  return true;
×
UNCOV
465
}
×
466

467
void Bind2Backend::alsoNotifies(const ZoneName& domain, set<string>* ips)
468
{
5✔
469
  // combine global list with local list
470
  for (const auto& i : this->alsoNotify) {
5!
471
    (*ips).insert(i);
×
472
  }
×
473
  // check metadata too if available
474
  vector<string> meta;
5✔
475
  if (getDomainMetadata(domain, "ALSO-NOTIFY", meta)) {
5!
476
    for (const auto& str : meta) {
×
477
      (*ips).insert(str);
×
478
    }
×
479
  }
×
480
  auto state = s_state.read_lock();
5✔
481
  for (const auto& i : *state) {
5!
482
    if (i.d_name == domain) {
×
483
      for (const auto& it : i.d_also_notify) {
×
484
        (*ips).insert(it);
×
485
      }
×
486
      return;
×
487
    }
×
488
  }
×
489
}
5✔
490

491
// only parses, does NOT add to s_state!
492
void Bind2Backend::parseZoneFile(BB2DomainInfo* bbd)
UNCOV
493
{
×
UNCOV
494
  NSEC3PARAMRecordContent ns3pr;
×
UNCOV
495
  bool nsec3zone = false;
×
UNCOV
496
  if (d_hybrid) {
×
497
    DNSSECKeeper dk;
×
498
    nsec3zone = dk.getNSEC3PARAM(bbd->d_name, &ns3pr);
×
499
  }
×
UNCOV
500
  else
×
UNCOV
501
    nsec3zone = getNSEC3PARAMuncached(bbd->d_name, &ns3pr);
×
502

UNCOV
503
  auto records = std::make_shared<recordstorage_t>();
×
UNCOV
504
  ZoneParserTNG zpt(bbd->d_filename, bbd->d_name, s_binddirectory, d_upgradeContent);
×
UNCOV
505
  zpt.setMaxGenerateSteps(::arg().asNum("max-generate-steps"));
×
UNCOV
506
  zpt.setMaxIncludes(::arg().asNum("max-include-depth"));
×
UNCOV
507
  DNSResourceRecord rr;
×
UNCOV
508
  string hashed;
×
UNCOV
509
  while (zpt.get(rr)) {
×
UNCOV
510
    if (rr.qtype.getCode() == QType::NSEC || rr.qtype.getCode() == QType::NSEC3 || rr.qtype.getCode() == QType::NSEC3PARAM)
×
511
      continue; // we synthesise NSECs on demand
×
512

UNCOV
513
    insertRecord(records, bbd->d_name, rr.qname, rr.qtype, rr.content, rr.ttl, "");
×
UNCOV
514
  }
×
UNCOV
515
  fixupOrderAndAuth(records, bbd->d_name, nsec3zone, ns3pr);
×
UNCOV
516
  doEmptyNonTerminals(records, bbd->d_name, nsec3zone, ns3pr);
×
UNCOV
517
  bbd->setCtime();
×
UNCOV
518
  bbd->d_loaded = true;
×
UNCOV
519
  bbd->d_checknow = false;
×
UNCOV
520
  bbd->d_status = "parsed into memory at " + nowTime();
×
UNCOV
521
  bbd->d_records = LookButDontTouch<recordstorage_t>(std::move(records));
×
UNCOV
522
  bbd->d_nsec3zone = nsec3zone;
×
UNCOV
523
  bbd->d_nsec3param = std::move(ns3pr);
×
UNCOV
524
}
×
525

526
/** THIS IS AN INTERNAL FUNCTION! It does moadnsparser prio impedance matching
527
    Much of the complication is due to the efforts to benefit from std::string reference counting copy on write semantics */
528
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)
UNCOV
529
{
×
UNCOV
530
  Bind2DNSRecord bdr;
×
UNCOV
531
  bdr.qname = qname;
×
532

UNCOV
533
  if (zoneName.empty())
×
534
    ;
×
UNCOV
535
  else if (bdr.qname.isPartOf(zoneName))
×
UNCOV
536
    bdr.qname.makeUsRelative(zoneName);
×
UNCOV
537
  else {
×
UNCOV
538
    string msg = "Trying to insert non-zone data, name='" + bdr.qname.toLogString() + "', qtype=" + qtype.toString() + ", zone='" + zoneName.toLogString() + "'";
×
UNCOV
539
    if (s_ignore_broken_records) {
×
UNCOV
540
      g_log << Logger::Warning << msg << " ignored" << endl;
×
UNCOV
541
      return;
×
UNCOV
542
    }
×
543
    else
×
544
      throw PDNSException(msg);
×
UNCOV
545
  }
×
546

547
  //  bdr.qname.swap(bdr.qname);
548

UNCOV
549
  if (!records->empty() && bdr.qname == boost::prior(records->end())->qname)
×
UNCOV
550
    bdr.qname = boost::prior(records->end())->qname;
×
551

UNCOV
552
  bdr.qname = bdr.qname;
×
UNCOV
553
  bdr.qtype = qtype.getCode();
×
UNCOV
554
  bdr.content = content;
×
UNCOV
555
  bdr.nsec3hash = hashed;
×
556

UNCOV
557
  if (auth != nullptr) // Set auth on empty non-terminals
×
UNCOV
558
    bdr.auth = *auth;
×
UNCOV
559
  else
×
UNCOV
560
    bdr.auth = true;
×
561

UNCOV
562
  bdr.ttl = ttl;
×
UNCOV
563
  records->insert(std::move(bdr));
×
UNCOV
564
}
×
565

566
string Bind2Backend::DLReloadNowHandler(const vector<string>& parts, Utility::pid_t /* ppid */)
567
{
×
568
  ostringstream ret;
×
569

570
  for (auto i = parts.begin() + 1; i < parts.end(); ++i) {
×
571
    BB2DomainInfo bbd;
×
572
    ZoneName zone(*i);
×
573
    if (safeGetBBDomainInfo(zone, &bbd)) {
×
574
      Bind2Backend bb2;
×
575
      bb2.queueReloadAndStore(bbd.d_id);
×
576
      if (!safeGetBBDomainInfo(zone, &bbd)) // Read the *new* domain status
×
577
        ret << *i << ": [missing]\n";
×
578
      else
×
579
        ret << *i << ": " << (bbd.d_wasRejectedLastReload ? "[rejected]" : "") << "\t" << bbd.d_status << "\n";
×
580
      purgeAuthCaches(zone.toString() + "$");
×
581
      DNSSECKeeper::clearMetaCache(zone);
×
582
    }
×
583
    else
×
584
      ret << *i << " no such domain\n";
×
585
  }
×
586
  if (ret.str().empty())
×
587
    ret << "no domains reloaded";
×
588
  return ret.str();
×
589
}
×
590

591
string Bind2Backend::DLDomStatusHandler(const vector<string>& parts, Utility::pid_t /* ppid */)
UNCOV
592
{
×
UNCOV
593
  ostringstream ret;
×
594

UNCOV
595
  if (parts.size() > 1) {
×
596
    for (auto i = parts.begin() + 1; i < parts.end(); ++i) {
×
597
      BB2DomainInfo bbd;
×
598
      if (safeGetBBDomainInfo(ZoneName(*i), &bbd)) {
×
599
        ret << *i << ": " << (bbd.d_loaded ? "" : "[rejected]") << "\t" << bbd.d_status << "\n";
×
600
      }
×
601
      else {
×
602
        ret << *i << " no such domain\n";
×
603
      }
×
604
    }
×
605
  }
×
UNCOV
606
  else {
×
UNCOV
607
    auto state = s_state.read_lock();
×
UNCOV
608
    for (const auto& i : *state) {
×
UNCOV
609
      ret << i.d_name << ": " << (i.d_loaded ? "" : "[rejected]") << "\t" << i.d_status << "\n";
×
UNCOV
610
    }
×
UNCOV
611
  }
×
612

UNCOV
613
  if (ret.str().empty())
×
614
    ret << "no domains passed";
×
615

UNCOV
616
  return ret.str();
×
UNCOV
617
}
×
618

619
static void printDomainExtendedStatus(ostringstream& ret, const BB2DomainInfo& info)
620
{
×
621
  ret << info.d_name << ": " << std::endl;
×
622
  ret << "\t Status: " << info.d_status << std::endl;
×
623
  ret << "\t Internal ID: " << info.d_id << std::endl;
×
624
  ret << "\t On-disk file: " << info.d_filename << " (" << info.d_ctime << ")" << std::endl;
×
625
  ret << "\t Kind: ";
×
626
  switch (info.d_kind) {
×
627
  case DomainInfo::Primary:
×
628
    ret << "Primary";
×
629
    break;
×
630
  case DomainInfo::Secondary:
×
631
    ret << "Secondary";
×
632
    break;
×
633
  default:
×
634
    ret << "Native";
×
635
  }
×
636
  ret << std::endl;
×
637
  ret << "\t Primaries: " << std::endl;
×
638
  for (const auto& primary : info.d_primaries) {
×
639
    ret << "\t\t - " << primary.toStringWithPort() << std::endl;
×
640
  }
×
641
  ret << "\t Also Notify: " << std::endl;
×
642
  for (const auto& also : info.d_also_notify) {
×
643
    ret << "\t\t - " << also << std::endl;
×
644
  }
×
645
  ret << "\t Number of records: " << info.d_records.getEntriesCount() << std::endl;
×
646
  ret << "\t Loaded: " << info.d_loaded << std::endl;
×
647
  ret << "\t Check now: " << info.d_checknow << std::endl;
×
648
  ret << "\t Check interval: " << info.getCheckInterval() << std::endl;
×
649
  ret << "\t Last check: " << info.d_lastcheck << std::endl;
×
650
  ret << "\t Last notified: " << info.d_lastnotified << std::endl;
×
651
}
×
652

653
string Bind2Backend::DLDomExtendedStatusHandler(const vector<string>& parts, Utility::pid_t /* ppid */)
654
{
×
655
  ostringstream ret;
×
656

657
  if (parts.size() > 1) {
×
658
    for (auto i = parts.begin() + 1; i < parts.end(); ++i) {
×
659
      BB2DomainInfo bbd;
×
660
      if (safeGetBBDomainInfo(ZoneName(*i), &bbd)) {
×
661
        printDomainExtendedStatus(ret, bbd);
×
662
      }
×
663
      else {
×
664
        ret << *i << " no such domain" << std::endl;
×
665
      }
×
666
    }
×
667
  }
×
668
  else {
×
669
    auto rstate = s_state.read_lock();
×
670
    for (const auto& state : *rstate) {
×
671
      printDomainExtendedStatus(ret, state);
×
672
    }
×
673
  }
×
674

675
  if (ret.str().empty()) {
×
676
    ret << "no domains passed" << std::endl;
×
677
  }
×
678

679
  return ret.str();
×
680
}
×
681

682
string Bind2Backend::DLListRejectsHandler(const vector<string>& /* parts */, Utility::pid_t /* ppid */)
683
{
×
684
  ostringstream ret;
×
685
  auto rstate = s_state.read_lock();
×
686
  for (const auto& i : *rstate) {
×
687
    if (!i.d_loaded)
×
688
      ret << i.d_name << "\t" << i.d_status << endl;
×
689
  }
×
690
  return ret.str();
×
691
}
×
692

693
string Bind2Backend::DLAddDomainHandler(const vector<string>& parts, Utility::pid_t /* ppid */)
UNCOV
694
{
×
UNCOV
695
  if (parts.size() < 3)
×
696
    return "ERROR: Domain name and zone filename are required";
×
697

UNCOV
698
  ZoneName domainname(parts[1]);
×
UNCOV
699
  const string& filename = parts[2];
×
UNCOV
700
  BB2DomainInfo bbd;
×
UNCOV
701
  if (safeGetBBDomainInfo(domainname, &bbd))
×
UNCOV
702
    return "Already loaded";
×
703

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

UNCOV
707
  struct stat buf;
×
UNCOV
708
  if (stat(filename.c_str(), &buf) != 0)
×
709
    return "Unable to load zone " + domainname.toLogString() + " from " + filename + ": " + strerror(errno);
×
710

UNCOV
711
  Bind2Backend bb2; // createdomainentry needs access to our configuration
×
UNCOV
712
  bbd = bb2.createDomainEntry(domainname, filename);
×
UNCOV
713
  bbd.d_filename = filename;
×
UNCOV
714
  bbd.d_checknow = true;
×
UNCOV
715
  bbd.d_loaded = true;
×
UNCOV
716
  bbd.d_lastcheck = 0;
×
UNCOV
717
  bbd.d_status = "parsing into memory";
×
UNCOV
718
  bbd.setCtime();
×
719

UNCOV
720
  safePutBBDomainInfo(bbd);
×
721

UNCOV
722
  g_zoneCache.add(domainname, bbd.d_id); // make new zone visible
×
723

UNCOV
724
  g_log << Logger::Warning << "Zone " << domainname << " loaded" << endl;
×
UNCOV
725
  return "Loaded zone " + domainname.toLogString() + " from " + filename;
×
UNCOV
726
}
×
727

728
Bind2Backend::Bind2Backend(const string& suffix, bool loadZones)
729
{
2,215✔
730
  d_getAllDomainMetadataQuery_stmt = nullptr;
2,215✔
731
  d_getDomainMetadataQuery_stmt = nullptr;
2,215✔
732
  d_deleteDomainMetadataQuery_stmt = nullptr;
2,215✔
733
  d_insertDomainMetadataQuery_stmt = nullptr;
2,215✔
734
  d_getDomainKeysQuery_stmt = nullptr;
2,215✔
735
  d_deleteDomainKeyQuery_stmt = nullptr;
2,215✔
736
  d_insertDomainKeyQuery_stmt = nullptr;
2,215✔
737
  d_GetLastInsertedKeyIdQuery_stmt = nullptr;
2,215✔
738
  d_activateDomainKeyQuery_stmt = nullptr;
2,215✔
739
  d_deactivateDomainKeyQuery_stmt = nullptr;
2,215✔
740
  d_getTSIGKeyQuery_stmt = nullptr;
2,215✔
741
  d_setTSIGKeyQuery_stmt = nullptr;
2,215✔
742
  d_deleteTSIGKeyQuery_stmt = nullptr;
2,215✔
743
  d_getTSIGKeysQuery_stmt = nullptr;
2,215✔
744

745
  setArgPrefix("bind" + suffix);
2,215✔
746
  d_logprefix = "[bind" + suffix + "backend]";
2,215✔
747
  d_hybrid = mustDo("hybrid");
2,215✔
748
  if (d_hybrid && g_zoneCache.isEnabled()) {
2,215!
749
    throw PDNSException("bind-hybrid and the zone cache currently interoperate badly. Please disable the zone cache or stop using bind-hybrid");
×
750
  }
×
751

752
  d_transaction_id = 0;
2,215✔
753
  s_ignore_broken_records = mustDo("ignore-broken-records");
2,215✔
754
  d_upgradeContent = ::arg().mustDo("upgrade-unknown-types");
2,215✔
755

756
  if (!loadZones && d_hybrid)
2,215!
757
    return;
×
758

759
  std::lock_guard<std::mutex> l(s_startup_lock);
2,215✔
760

761
  setupDNSSEC();
2,215✔
762
  if (s_first == 0) {
2,215✔
763
    return;
1,765✔
764
  }
1,765✔
765

766
  if (loadZones) {
450✔
767
    loadConfig();
164✔
768
    s_first = 0;
164✔
769
  }
164✔
770

771
  DynListener::registerFunc("BIND-RELOAD-NOW", &DLReloadNowHandler, "bindbackend: reload domains", "<domains>");
450✔
772
  DynListener::registerFunc("BIND-DOMAIN-STATUS", &DLDomStatusHandler, "bindbackend: list status of all domains", "[domains]");
450✔
773
  DynListener::registerFunc("BIND-DOMAIN-EXTENDED-STATUS", &DLDomExtendedStatusHandler, "bindbackend: list the extended status of all domains", "[domains]");
450✔
774
  DynListener::registerFunc("BIND-LIST-REJECTS", &DLListRejectsHandler, "bindbackend: list rejected domains");
450✔
775
  DynListener::registerFunc("BIND-ADD-ZONE", &DLAddDomainHandler, "bindbackend: add zone", "<domain> <filename>");
450✔
776
}
450✔
777

778
Bind2Backend::~Bind2Backend()
779
{
2,195✔
780
  freeStatements();
2,195✔
781
} // deallocate statements
2,195✔
782

783
void Bind2Backend::rediscover(string* status)
784
{
×
785
  loadConfig(status);
×
786
}
×
787

788
void Bind2Backend::reload()
789
{
×
790
  auto state = s_state.write_lock();
×
791
  for (const auto& i : *state) {
×
792
    i.d_checknow = true; // being a bit cheeky here, don't index state_t on this (mutable)
×
793
  }
×
794
}
×
795

796
void Bind2Backend::fixupOrderAndAuth(std::shared_ptr<recordstorage_t>& records, const ZoneName& zoneName, bool nsec3zone, const NSEC3PARAMRecordContent& ns3pr)
UNCOV
797
{
×
UNCOV
798
  bool skip;
×
UNCOV
799
  DNSName shorter;
×
UNCOV
800
  set<DNSName> nssets, dssets;
×
801

UNCOV
802
  for (const auto& bdr : *records) {
×
UNCOV
803
    if (!bdr.qname.isRoot() && bdr.qtype == QType::NS)
×
UNCOV
804
      nssets.insert(bdr.qname);
×
UNCOV
805
    else if (bdr.qtype == QType::DS)
×
UNCOV
806
      dssets.insert(bdr.qname);
×
UNCOV
807
  }
×
808

UNCOV
809
  for (auto iter = records->begin(); iter != records->end(); iter++) {
×
UNCOV
810
    skip = false;
×
UNCOV
811
    shorter = iter->qname;
×
812

UNCOV
813
    if (!iter->qname.isRoot() && shorter.chopOff() && !iter->qname.isRoot()) {
×
UNCOV
814
      do {
×
UNCOV
815
        if (nssets.count(shorter) != 0u) {
×
UNCOV
816
          skip = true;
×
UNCOV
817
          break;
×
UNCOV
818
        }
×
UNCOV
819
      } while (shorter.chopOff() && !iter->qname.isRoot());
×
UNCOV
820
    }
×
821

UNCOV
822
    iter->auth = (!skip && (iter->qtype == QType::DS || iter->qtype == QType::RRSIG || (nssets.count(iter->qname) == 0u)));
×
823

UNCOV
824
    if (!skip && nsec3zone && iter->qtype != QType::RRSIG && (iter->auth || (iter->qtype == QType::NS && (ns3pr.d_flags == 0u)) || (dssets.count(iter->qname) != 0u))) {
×
UNCOV
825
      Bind2DNSRecord bdr = *iter;
×
UNCOV
826
      bdr.nsec3hash = toBase32Hex(hashQNameWithSalt(ns3pr, bdr.qname + zoneName.operator const DNSName&()));
×
UNCOV
827
      records->replace(iter, bdr);
×
UNCOV
828
    }
×
829

830
    // cerr<<iter->qname<<"\t"<<QType(iter->qtype).toString()<<"\t"<<iter->nsec3hash<<"\t"<<iter->auth<<endl;
UNCOV
831
  }
×
UNCOV
832
}
×
833

834
void Bind2Backend::doEmptyNonTerminals(std::shared_ptr<recordstorage_t>& records, const ZoneName& zoneName, bool nsec3zone, const NSEC3PARAMRecordContent& ns3pr)
UNCOV
835
{
×
UNCOV
836
  bool auth = false;
×
UNCOV
837
  DNSName shorter;
×
UNCOV
838
  std::unordered_set<DNSName> qnames;
×
UNCOV
839
  std::unordered_map<DNSName, bool> nonterm;
×
840

UNCOV
841
  uint32_t maxent = ::arg().asNum("max-ent-entries");
×
842

UNCOV
843
  for (const auto& bdr : *records)
×
UNCOV
844
    qnames.insert(bdr.qname);
×
845

UNCOV
846
  for (const auto& bdr : *records) {
×
847

UNCOV
848
    if (!bdr.auth && bdr.qtype == QType::NS)
×
UNCOV
849
      auth = (!nsec3zone || (ns3pr.d_flags == 0u));
×
UNCOV
850
    else
×
UNCOV
851
      auth = bdr.auth;
×
852

UNCOV
853
    shorter = bdr.qname;
×
UNCOV
854
    while (shorter.chopOff()) {
×
UNCOV
855
      if (qnames.count(shorter) == 0u) {
×
UNCOV
856
        if (!(maxent)) {
×
857
          g_log << Logger::Error << "Zone '" << zoneName << "' has too many empty non terminals." << endl;
×
858
          return;
×
859
        }
×
860

UNCOV
861
        if (nonterm.count(shorter) == 0u) {
×
UNCOV
862
          nonterm.emplace(shorter, auth);
×
UNCOV
863
          --maxent;
×
UNCOV
864
        }
×
UNCOV
865
        else if (auth)
×
UNCOV
866
          nonterm[shorter] = true;
×
UNCOV
867
      }
×
UNCOV
868
    }
×
UNCOV
869
  }
×
870

UNCOV
871
  DNSResourceRecord rr;
×
UNCOV
872
  rr.qtype = "#0";
×
UNCOV
873
  rr.content = "";
×
UNCOV
874
  rr.ttl = 0;
×
UNCOV
875
  for (auto& nt : nonterm) {
×
UNCOV
876
    string hashed;
×
UNCOV
877
    rr.qname = nt.first + zoneName.operator const DNSName&();
×
UNCOV
878
    if (nsec3zone && nt.second)
×
UNCOV
879
      hashed = toBase32Hex(hashQNameWithSalt(ns3pr, rr.qname));
×
UNCOV
880
    insertRecord(records, zoneName, rr.qname, rr.qtype, rr.content, rr.ttl, hashed, &nt.second);
×
881

882
    // cerr<<rr.qname<<"\t"<<rr.qtype.toString()<<"\t"<<hashed<<"\t"<<nt.second<<endl;
UNCOV
883
  }
×
UNCOV
884
}
×
885

886
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
887
{
164✔
888
  static int domain_id = 1;
164✔
889

890
  if (!getArg("config").empty()) {
164!
891
    BindParser BP;
164✔
892
    try {
164✔
893
      BP.parse(getArg("config"));
164✔
894
    }
164✔
895
    catch (PDNSException& ae) {
164✔
896
      g_log << Logger::Error << "Error parsing bind configuration: " << ae.reason << endl;
×
897
      throw;
×
898
    }
×
899

900
    vector<BindDomainInfo> domains = BP.getDomains();
164✔
901
    this->alsoNotify = BP.getAlsoNotify();
164✔
902

903
    s_binddirectory = BP.getDirectory();
164✔
904
    //    ZP.setDirectory(d_binddirectory);
905

906
    g_log << Logger::Warning << d_logprefix << " Parsing " << domains.size() << " domain(s), will report when done" << endl;
164✔
907

908
    set<ZoneName> oldnames;
164✔
909
    set<ZoneName> newnames;
164✔
910
    {
164✔
911
      auto state = s_state.read_lock();
164!
912
      for (const BB2DomainInfo& bbd : *state) {
33!
913
        oldnames.insert(bbd.d_name);
×
914
      }
131✔
915
    }
164✔
916
    int rejected = 0;
164✔
917
    int newdomains = 0;
33✔
918

131✔
919
    struct stat st;
33✔
920

131!
921
    for (auto& domain : domains) {
33!
UNCOV
922
      if (stat(domain.filename.c_str(), &st) == 0) {
×
UNCOV
923
        domain.d_dev = st.st_dev;
×
UNCOV
924
        domain.d_ino = st.st_ino;
×
UNCOV
925
      }
×
926
    }
927

131✔
928
    sort(domains.begin(), domains.end()); // put stuff in inode order
164!
929
    for (const auto& domain : domains) {
33!
UNCOV
930
      if (!(domain.hadFileDirective)) {
×
931
        g_log << Logger::Warning << d_logprefix << " Zone '" << domain.name << "' has no 'file' directive set in " << getArg("config") << endl;
×
932
        rejected++;
×
933
        continue;
×
934
      }
935

×
UNCOV
936
      if (domain.type.empty()) {
×
937
        g_log << Logger::Notice << d_logprefix << " Zone '" << domain.name << "' has no type specified, assuming 'native'" << endl;
×
938
      }
×
UNCOV
939
      if (domain.type != "primary" && domain.type != "secondary" && domain.type != "native" && !domain.type.empty() && domain.type != "master" && domain.type != "slave") {
×
940
        g_log << Logger::Warning << d_logprefix << " Warning! Skipping zone '" << domain.name << "' because type '" << domain.type << "' is invalid" << endl;
×
941
        rejected++;
×
942
        continue;
×
943
      }
944

UNCOV
945
      BB2DomainInfo bbd;
×
946
      bool isNew = false;
947

×
UNCOV
948
      if (!safeGetBBDomainInfo(domain.name, &bbd)) {
×
UNCOV
949
        isNew = true;
×
UNCOV
950
        bbd.d_id = domain_id++;
×
UNCOV
951
        bbd.setCheckInterval(getArgAsNum("check-interval"));
×
UNCOV
952
        bbd.d_lastnotified = 0;
×
UNCOV
953
        bbd.d_loaded = false;
×
954
      }
955

956
      // overwrite what we knew about the domain
UNCOV
957
      bbd.d_name = domain.name;
×
UNCOV
958
      bool filenameChanged = (bbd.d_filename != domain.filename);
×
UNCOV
959
      bool addressesChanged = (bbd.d_primaries != domain.primaries || bbd.d_also_notify != domain.alsoNotify);
×
UNCOV
960
      bbd.d_filename = domain.filename;
×
UNCOV
961
      bbd.d_primaries = domain.primaries;
×
962
      bbd.d_also_notify = domain.alsoNotify;
963

UNCOV
964
      DomainInfo::DomainKind kind = DomainInfo::Native;
×
UNCOV
965
      if (domain.type == "primary" || domain.type == "master") {
×
UNCOV
966
        kind = DomainInfo::Primary;
×
UNCOV
967
      }
×
UNCOV
968
      if (domain.type == "secondary" || domain.type == "slave") {
×
UNCOV
969
        kind = DomainInfo::Secondary;
×
970
      }
971

UNCOV
972
      bool kindChanged = (bbd.d_kind != kind);
×
973
      bbd.d_kind = kind;
974

UNCOV
975
      newnames.insert(bbd.d_name);
×
UNCOV
976
      if (filenameChanged || !bbd.d_loaded || !bbd.current()) {
×
977
        g_log << Logger::Info << d_logprefix << " parsing '" << domain.name << "' from file '" << domain.filename << "'" << endl;
978

UNCOV
979
        try {
×
UNCOV
980
          parseZoneFile(&bbd);
×
UNCOV
981
        }
×
UNCOV
982
        catch (PDNSException& ae) {
×
983
          ostringstream msg;
×
984
          msg << " error at " + nowTime() + " parsing '" << domain.name << "' from file '" << domain.filename << "': " << ae.reason;
985

×
986
          if (status != nullptr)
×
987
            *status += msg.str();
×
988
          bbd.d_status = msg.str();
989

990
          g_log << Logger::Warning << d_logprefix << msg.str() << endl;
×
991
          rejected++;
×
992
        }
×
UNCOV
993
        catch (std::system_error& ae) {
×
UNCOV
994
          ostringstream msg;
×
UNCOV
995
          if (ae.code().value() == ENOENT && isNew && domain.type == "slave")
×
996
            msg << " error at " + nowTime() << " no file found for new secondary domain '" << domain.name << "'. Has not been AXFR'd yet";
×
UNCOV
997
          else
×
998
            msg << " error at " + nowTime() + " parsing '" << domain.name << "' from file '" << domain.filename << "': " << ae.what();
999

×
UNCOV
1000
          if (status != nullptr)
×
1001
            *status += msg.str();
×
UNCOV
1002
          bbd.d_status = msg.str();
×
UNCOV
1003
          g_log << Logger::Warning << d_logprefix << msg.str() << endl;
×
UNCOV
1004
          rejected++;
×
UNCOV
1005
        }
×
UNCOV
1006
        catch (std::exception& ae) {
×
1007
          ostringstream msg;
×
1008
          msg << " error at " + nowTime() + " parsing '" << domain.name << "' from file '" << domain.filename << "': " << ae.what();
1009

×
1010
          if (status != nullptr)
×
1011
            *status += msg.str();
×
1012
          bbd.d_status = msg.str();
1013

1014
          g_log << Logger::Warning << d_logprefix << msg.str() << endl;
×
1015
          rejected++;
×
1016
        }
×
UNCOV
1017
        safePutBBDomainInfo(bbd);
×
UNCOV
1018
      }
×
1019
      else if (addressesChanged || kindChanged) {
×
1020
        safePutBBDomainInfo(bbd);
×
1021
      }
×
1022
    }
131✔
1023
    vector<ZoneName> diff;
33✔
1024

131✔
1025
    set_difference(oldnames.begin(), oldnames.end(), newnames.begin(), newnames.end(), back_inserter(diff));
164✔
1026
    unsigned int remdomains = diff.size();
33✔
1027

131!
1028
    for (const ZoneName& name : diff) {
33!
1029
      safeRemoveBBDomainInfo(name);
×
1030
    }
1031

1032
    // count number of entirely new domains
131✔
1033
    diff.clear();
164✔
1034
    set_difference(newnames.begin(), newnames.end(), oldnames.begin(), oldnames.end(), back_inserter(diff));
164✔
1035
    newdomains = diff.size();
33✔
1036

131✔
1037
    ostringstream msg;
164✔
1038
    msg << " Done parsing domains, " << rejected << " rejected, " << newdomains << " new, " << remdomains << " removed";
164!
1039
    if (status != nullptr)
33!
1040
      *status = msg.str();
1041

131✔
1042
    g_log << Logger::Error << d_logprefix << msg.str() << endl;
164✔
1043
  }
164✔
1044
}
33✔
1045

1046
void Bind2Backend::queueReloadAndStore(unsigned int id)
UNCOV
1047
{
×
UNCOV
1048
  BB2DomainInfo bbold;
×
UNCOV
1049
  try {
×
UNCOV
1050
    if (!safeGetBBDomainInfo(id, &bbold))
×
1051
      return;
×
UNCOV
1052
    bbold.d_checknow = false;
×
1053
    BB2DomainInfo bbnew(bbold);
1054
    /* make sure that nothing will be able to alter the existing records,
1055
       we will load them from the zone file instead */
UNCOV
1056
    bbnew.d_records = LookButDontTouch<recordstorage_t>();
×
UNCOV
1057
    parseZoneFile(&bbnew);
×
UNCOV
1058
    bbnew.d_wasRejectedLastReload = false;
×
UNCOV
1059
    safePutBBDomainInfo(bbnew);
×
UNCOV
1060
    g_log << Logger::Warning << "Zone '" << bbnew.d_name << "' (" << bbnew.d_filename << ") reloaded" << endl;
×
UNCOV
1061
  }
×
UNCOV
1062
  catch (PDNSException& ae) {
×
1063
    ostringstream msg;
×
1064
    msg << " error at " + nowTime() + " parsing '" << bbold.d_name << "' from file '" << bbold.d_filename << "': " << ae.reason;
×
1065
    g_log << Logger::Warning << "Error parsing '" << bbold.d_name << "' from file '" << bbold.d_filename << "': " << ae.reason << endl;
×
1066
    bbold.d_status = msg.str();
×
1067
    bbold.d_lastcheck = time(nullptr);
×
1068
    bbold.d_wasRejectedLastReload = true;
×
1069
    safePutBBDomainInfo(bbold);
×
1070
  }
×
UNCOV
1071
  catch (std::exception& ae) {
×
1072
    ostringstream msg;
×
1073
    msg << " error at " + nowTime() + " parsing '" << bbold.d_name << "' from file '" << bbold.d_filename << "': " << ae.what();
×
1074
    g_log << Logger::Warning << "Error parsing '" << bbold.d_name << "' from file '" << bbold.d_filename << "': " << ae.what() << endl;
×
1075
    bbold.d_status = msg.str();
×
1076
    bbold.d_lastcheck = time(nullptr);
×
1077
    bbold.d_wasRejectedLastReload = true;
×
1078
    safePutBBDomainInfo(bbold);
×
1079
  }
×
1080
}
1081

1082
bool Bind2Backend::findBeforeAndAfterUnhashed(std::shared_ptr<const recordstorage_t>& records, const DNSName& qname, DNSName& /* unhashed */, DNSName& before, DNSName& after)
1083
{
1084
  // for(const auto& record: *records)
1085
  //   cerr<<record.qname<<"\t"<<makeHexDump(record.qname.toDNSString())<<endl;
1086

1087
  recordstorage_t::const_iterator iterBefore, iterAfter;
1088

1089
  iterBefore = iterAfter = records->upper_bound(qname.makeLowerCase());
1090

×
UNCOV
1091
  if (iterBefore != records->begin())
×
UNCOV
1092
    --iterBefore;
×
UNCOV
1093
  while ((!iterBefore->auth && iterBefore->qtype != QType::NS) || !iterBefore->qtype)
×
UNCOV
1094
    --iterBefore;
×
1095
  before = iterBefore->qname;
1096

×
UNCOV
1097
  if (iterAfter == records->end()) {
×
UNCOV
1098
    iterAfter = records->begin();
×
UNCOV
1099
  }
×
UNCOV
1100
  else {
×
UNCOV
1101
    while ((!iterAfter->auth && iterAfter->qtype != QType::NS) || !iterAfter->qtype) {
×
UNCOV
1102
      ++iterAfter;
×
UNCOV
1103
      if (iterAfter == records->end()) {
×
1104
        iterAfter = records->begin();
×
1105
        break;
×
1106
      }
×
UNCOV
1107
    }
×
UNCOV
1108
  }
×
1109
  after = iterAfter->qname;
1110

UNCOV
1111
  return true;
×
1112
}
1113

1114
bool Bind2Backend::getBeforeAndAfterNamesAbsolute(uint32_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after)
UNCOV
1115
{
×
UNCOV
1116
  BB2DomainInfo bbd;
×
UNCOV
1117
  if (!safeGetBBDomainInfo(id, &bbd))
×
1118
    return false;
1119

UNCOV
1120
  shared_ptr<const recordstorage_t> records = bbd.d_records.get();
×
UNCOV
1121
  if (!bbd.d_nsec3zone) {
×
UNCOV
1122
    return findBeforeAndAfterUnhashed(records, qname, unhashed, before, after);
×
UNCOV
1123
  }
×
UNCOV
1124
  else {
×
1125
    const auto& hashindex = boost::multi_index::get<NSEC3Tag>(*records);
1126

1127
    // for(auto iter = first; iter != hashindex.end(); iter++)
1128
    //  cerr<<iter->nsec3hash<<endl;
1129

UNCOV
1130
    auto first = hashindex.upper_bound("");
×
1131
    auto iter = hashindex.upper_bound(qname.toStringNoDot());
1132

×
UNCOV
1133
    if (iter == hashindex.end()) {
×
UNCOV
1134
      --iter;
×
UNCOV
1135
      before = DNSName(iter->nsec3hash);
×
UNCOV
1136
      after = DNSName(first->nsec3hash);
×
UNCOV
1137
    }
×
UNCOV
1138
    else {
×
UNCOV
1139
      after = DNSName(iter->nsec3hash);
×
UNCOV
1140
      if (iter != first)
×
UNCOV
1141
        --iter;
×
UNCOV
1142
      else
×
UNCOV
1143
        iter = --hashindex.end();
×
UNCOV
1144
      before = DNSName(iter->nsec3hash);
×
UNCOV
1145
    }
×
1146
    unhashed = iter->qname + bbd.d_name.operator const DNSName&();
1147

UNCOV
1148
    return true;
×
UNCOV
1149
  }
×
1150
}
1151

1152
void Bind2Backend::lookup(const QType& qtype, const DNSName& qname, int zoneId, DNSPacket* /* pkt_p */)
20✔
1153
{
25✔
1154
  d_handle.reset();
5✔
1155

20✔
1156
  static bool mustlog = ::arg().mustDo("query-logging");
5✔
1157

20✔
1158
  bool found = false;
25✔
1159
  ZoneName domain;
25✔
1160
  BB2DomainInfo bbd;
5✔
1161

20!
1162
  if (mustlog)
5!
1163
    g_log << Logger::Warning << "Lookup for '" << qtype.toString() << "' of '" << qname << "' within zoneID " << zoneId << endl;
1164

20!
1165
  if (zoneId >= 0) {
5!
UNCOV
1166
    if ((found = (safeGetBBDomainInfo(zoneId, &bbd) && qname.isPartOf(bbd.d_name)))) {
×
UNCOV
1167
      domain = std::move(bbd.d_name);
×
UNCOV
1168
    }
×
1169
  }
20✔
1170
  else {
25✔
1171
    domain = ZoneName(qname);
25✔
1172
    do {
25✔
1173
      found = safeGetBBDomainInfo(domain, &bbd);
25!
1174
    } while (!found && qtype != QType::SOA && domain.chopOff());
25!
1175
  }
5✔
1176

20!
1177
  if (!found) {
25✔
1178
    if (mustlog)
5!
1179
      g_log << Logger::Warning << "Found no authoritative zone for '" << qname << "' and/or id " << zoneId << endl;
20✔
1180
    d_handle.d_list = false;
25✔
1181
    return;
25✔
1182
  }
5✔
1183

×
UNCOV
1184
  if (mustlog)
×
1185
    g_log << Logger::Warning << "Found a zone '" << domain << "' (with id " << bbd.d_id << ") that might contain data " << endl;
1186

UNCOV
1187
  d_handle.id = bbd.d_id;
×
UNCOV
1188
  d_handle.qname = qname.makeRelative(domain); // strip domain name
×
UNCOV
1189
  d_handle.qtype = qtype;
×
1190
  d_handle.domain = std::move(domain);
1191

×
UNCOV
1192
  if (!bbd.current()) {
×
UNCOV
1193
    g_log << Logger::Warning << "Zone '" << d_handle.domain << "' (" << bbd.d_filename << ") needs reloading" << endl;
×
UNCOV
1194
    queueReloadAndStore(bbd.d_id);
×
UNCOV
1195
    if (!safeGetBBDomainInfo(d_handle.domain, &bbd))
×
1196
      throw DBException("Zone '" + bbd.d_name.toLogString() + "' (" + bbd.d_filename + ") gone after reload"); // if we don't throw here, we crash for some reason
×
1197
  }
1198

×
UNCOV
1199
  if (!bbd.d_loaded) {
×
UNCOV
1200
    d_handle.reset();
×
UNCOV
1201
    throw DBException("Zone for '" + d_handle.domain.toLogString() + "' in '" + bbd.d_filename + "' not loaded (file missing, corrupt or primary dead)"); // fsck
×
1202
  }
1203

1204
  d_handle.d_records = bbd.d_records.get();
1205

×
UNCOV
1206
  if (d_handle.d_records->empty())
×
1207
    DLOG(g_log << "Query with no results" << endl);
1208

1209
  d_handle.mustlog = mustlog;
1210

UNCOV
1211
  const auto& hashedidx = boost::multi_index::get<UnorderedNameTag>(*d_handle.d_records);
×
1212
  auto range = hashedidx.equal_range(d_handle.qname);
1213

UNCOV
1214
  d_handle.d_list = false;
×
UNCOV
1215
  d_handle.d_iter = range.first;
×
UNCOV
1216
  d_handle.d_end_iter = range.second;
×
1217
}
1218

1219
Bind2Backend::handle::handle()
1,764✔
1220
{
2,215✔
1221
  mustlog = false;
2,215✔
1222
}
451✔
1223

1224
bool Bind2Backend::get(DNSResourceRecord& r)
20✔
1225
{
25!
1226
  if (!d_handle.d_records) {
25✔
1227
    if (d_handle.mustlog)
5!
1228
      g_log << Logger::Warning << "There were no answers" << endl;
20✔
1229
    return false;
25✔
1230
  }
5✔
1231

×
UNCOV
1232
  if (!d_handle.get(r)) {
×
UNCOV
1233
    if (d_handle.mustlog)
×
1234
      g_log << Logger::Warning << "End of answers" << endl;
1235

1236
    d_handle.reset();
1237

UNCOV
1238
    return false;
×
UNCOV
1239
  }
×
UNCOV
1240
  if (d_handle.mustlog)
×
1241
    g_log << Logger::Warning << "Returning: '" << r.qtype.toString() << "' of '" << r.qname << "', content: '" << r.content << "'" << endl;
×
UNCOV
1242
  return true;
×
1243
}
1244

1245
bool Bind2Backend::handle::get(DNSResourceRecord& r)
UNCOV
1246
{
×
UNCOV
1247
  if (d_list)
×
UNCOV
1248
    return get_list(r);
×
UNCOV
1249
  else
×
UNCOV
1250
    return get_normal(r);
×
1251
}
1252

1253
void Bind2Backend::handle::reset()
20✔
1254
{
25✔
1255
  d_records.reset();
25✔
1256
  qname.clear();
25✔
1257
  mustlog = false;
25✔
1258
}
5✔
1259

1260
//#define DLOG(x) x
1261
bool Bind2Backend::handle::get_normal(DNSResourceRecord& r)
UNCOV
1262
{
×
1263
  DLOG(g_log << "Bind2Backend get() was called for " << qtype.toString() << " record for '" << qname << "' - " << d_records->size() << " available in total!" << endl);
1264

×
UNCOV
1265
  if (d_iter == d_end_iter) {
×
UNCOV
1266
    return false;
×
1267
  }
1268

×
UNCOV
1269
  while (d_iter != d_end_iter && !(qtype.getCode() == QType::ANY || (d_iter)->qtype == qtype.getCode())) {
×
UNCOV
1270
    DLOG(g_log << Logger::Warning << "Skipped " << qname << "/" << QType(d_iter->qtype).toString() << ": '" << d_iter->content << "'" << endl);
×
UNCOV
1271
    d_iter++;
×
UNCOV
1272
  }
×
UNCOV
1273
  if (d_iter == d_end_iter) {
×
1274
    return false;
×
1275
  }
×
1276
  DLOG(g_log << "Bind2Backend get() returning a rr with a " << QType(d_iter->qtype).getCode() << endl);
1277

×
UNCOV
1278
  const DNSName& domainName(domain);
×
UNCOV
1279
  r.qname = qname.empty() ? domainName : (qname + domainName);
×
1280
  r.domain_id = id;
UNCOV
1281
  r.content = (d_iter)->content;
×
1282
  //  r.domain_id=(d_iter)->domain_id;
1283
  r.qtype = (d_iter)->qtype;
1284
  r.ttl = (d_iter)->ttl;
1285

1286
  //if(!d_iter->auth && r.qtype.getCode() != QType::A && r.qtype.getCode()!=QType::AAAA && r.qtype.getCode() != QType::NS)
1287
  //  cerr<<"Warning! Unauth response for qtype "<< r.qtype.toString() << " for '"<<r.qname<<"'"<<endl;
UNCOV
1288
  r.auth = d_iter->auth;
×
1289

UNCOV
1290
  d_iter++;
×
1291

1292
  return true;
1293
}
1294

1295
bool Bind2Backend::list(const ZoneName& /* target */, int domainId, bool /* include_disabled */)
1296
{
UNCOV
1297
  BB2DomainInfo bbd;
×
1298

1299
  if (!safeGetBBDomainInfo(domainId, &bbd)) {
×
1300
    return false;
×
1301
  }
×
1302

UNCOV
1303
  d_handle.reset();
×
UNCOV
1304
  DLOG(g_log << "Bind2Backend constructing handle for list of " << domainId << endl);
×
1305

1306
  if (!bbd.d_loaded) {
×
1307
    throw PDNSException("zone was not loaded, perhaps because of: " + bbd.d_status);
×
1308
  }
×
1309

1310
  d_handle.d_records = bbd.d_records.get(); // give it a copy, which will stay around
UNCOV
1311
  d_handle.d_qname_iter = d_handle.d_records->begin();
×
UNCOV
1312
  d_handle.d_qname_end = d_handle.d_records->end(); // iter now points to a vector of pointers to vector<BBResourceRecords>
×
1313

UNCOV
1314
  d_handle.id = domainId;
×
UNCOV
1315
  d_handle.domain = bbd.d_name;
×
1316
  d_handle.d_list = true;
1317
  return true;
UNCOV
1318
}
×
1319

×
1320
bool Bind2Backend::handle::get_list(DNSResourceRecord& r)
×
UNCOV
1321
{
×
UNCOV
1322
  if (d_qname_iter != d_qname_end) {
×
UNCOV
1323
    const DNSName& domainName(domain);
×
UNCOV
1324
    r.qname = d_qname_iter->qname.empty() ? domainName : (d_qname_iter->qname + domainName);
×
UNCOV
1325
    r.domain_id = id;
×
UNCOV
1326
    r.content = (d_qname_iter)->content;
×
UNCOV
1327
    r.qtype = (d_qname_iter)->qtype;
×
UNCOV
1328
    r.ttl = (d_qname_iter)->ttl;
×
UNCOV
1329
    r.auth = d_qname_iter->auth;
×
UNCOV
1330
    d_qname_iter++;
×
1331
    return true;
1332
  }
UNCOV
1333
  return false;
×
UNCOV
1334
}
×
1335

1336
bool Bind2Backend::autoPrimariesList(std::vector<AutoPrimary>& primaries)
1337
{
×
1338
  if (getArg("autoprimary-config").empty())
×
1339
    return false;
×
1340

1341
  ifstream c_if(getArg("autoprimaries"), std::ios::in);
×
1342
  if (!c_if) {
×
1343
    g_log << Logger::Error << "Unable to open autoprimaries file for read: " << stringerror() << endl;
×
1344
    return false;
×
1345
  }
×
1346

1347
  string line, sip, saccount;
×
1348
  while (getline(c_if, line)) {
×
1349
    std::istringstream ii(line);
×
1350
    ii >> sip;
×
1351
    if (!sip.empty()) {
×
1352
      ii >> saccount;
1353
      primaries.emplace_back(sip, "", saccount);
×
1354
    }
×
1355
  }
×
1356

1357
  c_if.close();
1358
  return true;
×
1359
}
1360

×
1361
bool Bind2Backend::autoPrimaryBackend(const string& ipAddress, const ZoneName& /* domain */, const vector<DNSResourceRecord>& /* nsset */, string* /* nameserver */, string* account, DNSBackend** backend)
1362
{
1363
  // Check whether we have a configfile available.
1364
  if (getArg("autoprimary-config").empty())
×
1365
    return false;
×
1366

1367
  ifstream c_if(getArg("autoprimaries").c_str(), std::ios::in); // this was nocreate?
×
1368
  if (!c_if) {
×
1369
    g_log << Logger::Error << "Unable to open autoprimaries file for read: " << stringerror() << endl;
1370
    return false;
1371
  }
×
1372

×
1373
  // Format:
1374
  // <ip> <accountname>
1375
  string line, sip, saccount;
×
1376
  while (getline(c_if, line)) {
×
1377
    std::istringstream ii(line);
×
1378
    ii >> sip;
×
1379
    if (sip == ipAddress) {
×
1380
      ii >> saccount;
×
1381
      break;
1382
    }
×
1383
  }
×
1384
  c_if.close();
1385

1386
  if (sip != ipAddress) { // ip not found in authorization list - reject
×
1387
    return false;
×
1388
  }
×
1389

1390
  // ip authorized as autoprimary - accept
1391
  *backend = this;
×
1392
  if (saccount.length() > 0)
×
1393
    *account = saccount.c_str();
1394

1395
  return true;
×
1396
}
×
1397

1398
BB2DomainInfo Bind2Backend::createDomainEntry(const ZoneName& domain, const string& filename)
×
1399
{
1400
  int newid = 1;
UNCOV
1401
  { // Find a free zone id nr.
×
UNCOV
1402
    auto state = s_state.read_lock();
×
UNCOV
1403
    if (!state->empty()) {
×
1404
      // older (1.53) versions of boost have an expression for s_state.rbegin()
1405
      // that is ambiguous in C++17. So construct it explicitly
UNCOV
1406
      newid = boost::make_reverse_iterator(state->end())->d_id + 1;
×
UNCOV
1407
    }
×
UNCOV
1408
  }
×
1409

UNCOV
1410
  BB2DomainInfo bbd;
×
UNCOV
1411
  bbd.d_kind = DomainInfo::Native;
×
1412
  bbd.d_id = newid;
UNCOV
1413
  bbd.d_records = std::make_shared<recordstorage_t>();
×
UNCOV
1414
  bbd.d_name = domain;
×
1415
  bbd.setCheckInterval(getArgAsNum("check-interval"));
1416
  bbd.d_filename = filename;
1417

UNCOV
1418
  return bbd;
×
1419
}
1420

1421
bool Bind2Backend::createSecondaryDomain(const string& ipAddress, const ZoneName& domain, const string& /* nameserver */, const string& account)
1422
{
×
1423
  string filename = getArg("autoprimary-destdir") + '/' + domain.toStringNoDot();
1424

1425
  g_log << Logger::Warning << d_logprefix
×
1426
        << " Writing bind config zone statement for superslave zone '" << domain
1427
        << "' from autoprimary " << ipAddress << endl;
×
1428

×
1429
  {
×
1430
    std::lock_guard<std::mutex> l2(s_autosecondary_config_lock);
×
1431

1432
    ofstream c_of(getArg("autoprimary-config").c_str(), std::ios::app);
1433
    if (!c_of) {
×
1434
      g_log << Logger::Error << "Unable to open autoprimary configfile for append: " << stringerror() << endl;
×
1435
      throw DBException("Unable to open autoprimary configfile for append: " + stringerror());
×
1436
    }
×
1437

1438
    c_of << endl;
×
1439
    c_of << "# AutoSecondary zone '" << domain.toString() << "' (added: " << nowTime() << ") (account: " << account << ')' << endl;
×
1440
    c_of << "zone \"" << domain.toStringNoDot() << "\" {" << endl;
×
1441
    c_of << "\ttype secondary;" << endl;
×
1442
    c_of << "\tfile \"" << filename << "\";" << endl;
1443
    c_of << "\tprimaries { " << ipAddress << "; };" << endl;
×
1444
    c_of << "};" << endl;
×
1445
    c_of.close();
×
1446
  }
×
1447

1448
  BB2DomainInfo bbd = createDomainEntry(domain, filename);
1449
  bbd.d_kind = DomainInfo::Secondary;
×
1450
  bbd.d_primaries.emplace_back(ComboAddress(ipAddress, 53));
×
1451
  bbd.setCtime();
1452
  safePutBBDomainInfo(bbd);
1453

18✔
1454
  return true;
18✔
1455
}
18✔
1456

18!
1457
bool Bind2Backend::searchRecords(const string& pattern, size_t maxResults, vector<DNSResourceRecord>& result)
1458
{
6✔
1459
  SimpleMatch sm(pattern, true);
24✔
1460
  static bool mustlog = ::arg().mustDo("query-logging");
24✔
1461
  if (mustlog)
6!
1462
    g_log << Logger::Warning << "Search for pattern '" << pattern << "'" << endl;
18!
1463

1464
  {
6!
1465
    auto state = s_state.read_lock();
6✔
1466

1467
    for (const auto& i : *state) {
6!
1468
      BB2DomainInfo h;
×
1469
      if (!safeGetBBDomainInfo(i.d_id, &h)) {
×
1470
        continue;
×
1471
      }
1472

1473
      if (!h.d_loaded) {
×
1474
        continue;
×
1475
      }
×
1476

×
1477
      shared_ptr<const recordstorage_t> rhandle = h.d_records.get();
×
1478

1479
      for (recordstorage_t::const_iterator ri = rhandle->begin(); result.size() < maxResults && ri != rhandle->end(); ri++) {
×
1480
        const DNSName& domainName(i.d_name);
×
1481
        DNSName name = ri->qname.empty() ? domainName : (ri->qname + domainName);
×
1482
        if (sm.match(name) || sm.match(ri->content)) {
×
1483
          DNSResourceRecord r;
×
1484
          r.qname = std::move(name);
×
1485
          r.domain_id = i.d_id;
×
1486
          r.content = ri->content;
×
1487
          r.qtype = ri->qtype;
×
1488
          r.ttl = ri->ttl;
18✔
1489
          r.auth = ri->auth;
1490
          result.push_back(std::move(r));
18✔
1491
        }
18✔
1492
      }
1493
    }
1494
  }
6✔
1495

1496
  return true;
6✔
1497
}
275✔
1498

1499
class Bind2Factory : public BackendFactory
1500
{
235✔
1501
public:
235✔
1502
  Bind2Factory() :
235✔
1503
    BackendFactory("bind") {}
299✔
1504

235✔
1505
  void declareArguments(const string& suffix = "") override
235✔
1506
  {
294✔
1507
    declare(suffix, "ignore-broken-records", "Ignore records that are out-of-bound for the zone.", "no");
294✔
1508
    declare(suffix, "config", "Location of named.conf", "");
294✔
1509
    declare(suffix, "check-interval", "Interval for zonefile changes", "0");
294✔
1510
    declare(suffix, "autoprimary-config", "Location of (part of) named.conf where pdns can write zone-statements to", "");
294✔
1511
    declare(suffix, "autoprimaries", "List of IP-addresses of autoprimaries", "");
59✔
1512
    declare(suffix, "autoprimary-destdir", "Destination directory for newly added secondary zones", ::arg()["config-dir"]);
59✔
1513
    declare(suffix, "dnssec-db", "Filename to store & access our DNSSEC metadatabase, empty for none", "");
1,592✔
1514
    declare(suffix, "dnssec-db-journal-mode", "SQLite3 journal mode", "WAL");
1,592✔
1515
    declare(suffix, "hybrid", "Store DNSSEC metadata in other backend", "no");
1,592✔
1516
  }
1,592✔
1517

1518
  DNSBackend* make(const string& suffix = "") override
1519
  {
627✔
1520
    assertEmptySuffix(suffix);
627✔
1521
    return new Bind2Backend(suffix);
627✔
1522
  }
627✔
1523

1524
  DNSBackend* makeMetadataOnly(const string& suffix = "") override
1525
  {
55✔
1526
    assertEmptySuffix(suffix);
1,819✔
1527
    return new Bind2Backend(suffix, false);
1,819!
1528
  }
55✔
1529

1,764✔
1530
private:
1531
  void assertEmptySuffix(const string& suffix)
1532
  {
451✔
1533
    if (!suffix.empty())
451!
1534
      throw PDNSException("launch= suffixes are not supported on the bindbackend");
1535
  }
451✔
1536
};
1537

269✔
1538
//! Magic class that is activated when the dynamic library is loaded
269✔
1539
class Bind2Loader
269✔
1540
{
269✔
1541
public:
269✔
1542
  Bind2Loader()
269✔
1543
  {
333✔
1544
    BackendMakers().report(std::make_unique<Bind2Factory>());
333✔
1545
    g_log << Logger::Info << "[bind2backend] This is the bind backend version " << VERSION
333✔
1546
#ifndef REPRODUCIBLE
333✔
1547
          << " (" __DATE__ " " __TIME__ ")"
333✔
1548
#endif
64✔
1549
#ifdef HAVE_SQLITE3
64✔
1550
          << " (with bind-dnssec-db support)"
64✔
1551
#endif
64✔
1552
          << " reporting" << endl;
64✔
1553
  }
64✔
1554
};
1555
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

© 2026 Coveralls, Inc