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

PowerDNS / pdns / 16673722362

01 Aug 2025 11:19AM UTC coverage: 65.826% (+12.0%) from 53.823%
16673722362

Pull #15955

github

web-flow
Merge 245ce2bbf into 631f2ad0f
Pull Request #15955: rec: test rpzPrimary instead of rpzMaster

42043 of 92440 branches covered (45.48%)

Branch coverage included in aggregate %.

127950 of 165806 relevant lines covered (77.17%)

6122257.21 hits per line

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

35.29
/modules/bindbackend/bindbackend2.hh
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
#pragma once
23
#include <string>
24
#include <map>
25
#include <set>
26
#include <pthread.h>
27
#include <time.h>
28
#include <fstream>
29
#include <mutex>
30
#include <boost/utility.hpp>
31

32
#include <boost/multi_index_container.hpp>
33
#include <boost/multi_index/hashed_index.hpp>
34
#include <boost/multi_index/ordered_index.hpp>
35
#include <boost/multi_index/identity.hpp>
36
#include <boost/multi_index/member.hpp>
37
#include <sys/types.h>
38
#include <sys/stat.h>
39
#include <unistd.h>
40
#include "pdns/lock.hh"
41
#include "pdns/misc.hh"
42
#include "pdns/dnsbackend.hh"
43
#include "pdns/namespaces.hh"
44
#include "pdns/backends/gsql/ssql.hh"
45

46
using namespace ::boost::multi_index;
47

48
/**
49
  This struct is used within the Bind2Backend to store DNS information. It is
50
  almost identical to a DNSResourceRecord, but then a bit smaller and with
51
  different sorting rules, which make sure that the SOA record comes up front.
52
*/
53

54
struct Bind2DNSRecord
55
{
56
  DNSName qname;
57
  string content;
58
  string nsec3hash;
59
  uint32_t ttl;
60
  uint16_t qtype;
61
  mutable bool auth;
62
  bool operator<(const Bind2DNSRecord& rhs) const
63
  {
×
64
    if (int rc = qname.canonCompare_three_way(rhs.qname); rc != 0) {
×
65
      return rc < 0;
×
66
    }
×
67
    if (qtype == QType::SOA && rhs.qtype != QType::SOA)
×
68
      return true;
×
69
    return std::tie(qtype, content, ttl) < std::tie(rhs.qtype, rhs.content, rhs.ttl);
×
70
  }
×
71
};
72

73
struct Bind2DNSCompare : std::less<Bind2DNSRecord>
74
{
75
  using std::less<Bind2DNSRecord>::operator();
76
  // use operator<
77
  bool operator()(const DNSName& a, const Bind2DNSRecord& b) const
78
  {
31,386✔
79
    return a.canonCompare(b.qname);
31,386✔
80
  }
31,386✔
81
  bool operator()(const Bind2DNSRecord& a, const DNSName& b) const
82
  {
×
83
    return a.qname.canonCompare(b);
×
84
  }
×
85
  bool operator()(const Bind2DNSRecord& a, const Bind2DNSRecord& b) const
86
  {
66,481,612✔
87
    return a.qname.canonCompare(b.qname);
66,481,612✔
88
  }
66,481,612✔
89
};
90

91
struct NSEC3Tag
92
{
93
};
94
struct UnorderedNameTag
95
{
96
};
97

98
typedef multi_index_container<
99
  Bind2DNSRecord,
100
  indexed_by<
101
    ordered_non_unique<identity<Bind2DNSRecord>, Bind2DNSCompare>,
102
    hashed_non_unique<tag<UnorderedNameTag>, member<Bind2DNSRecord, DNSName, &Bind2DNSRecord::qname>>,
103
    ordered_non_unique<tag<NSEC3Tag>, member<Bind2DNSRecord, std::string, &Bind2DNSRecord::nsec3hash>>>>
104
  recordstorage_t;
105

106
template <typename T>
107
class LookButDontTouch
108
{
109
public:
110
  LookButDontTouch() = default;
15,574✔
111
  LookButDontTouch(shared_ptr<T>&& records) :
112
    d_records(std::move(records))
113
  {
2,671✔
114
  }
2,671✔
115

116
  shared_ptr<const T> get()
117
  {
11,279✔
118
    return d_records;
11,279✔
119
  }
11,279✔
120

121
  size_t getEntriesCount() const
122
  {
×
123
    if (!d_records) {
×
124
      return 0;
×
125
    }
×
126
    return d_records->size();
×
127
  }
×
128

129
private:
130
  /* we can increase the number of references to that object,
131
     but never update the object itself */
132
  shared_ptr<const T> d_records;
133
};
134

135
/** Class which describes all metadata of a domain for storage by the Bind2Backend, and also contains a pointer to a vector of Bind2DNSRecord's */
136
class BB2DomainInfo
137
{
138
public:
139
  BB2DomainInfo();
140
  void setCtime();
141
  bool current();
142
  //! configure how often this domain should be checked for changes (on disk)
143
  void setCheckInterval(time_t seconds);
144
  time_t getCheckInterval() const
145
  {
×
146
    return d_checkinterval;
×
147
  }
×
148

149
  ZoneName d_name; //!< actual name of the domain
150
  DomainInfo::DomainKind d_kind{DomainInfo::Native}; //!< the kind of domain
151
  string d_filename; //!< full absolute filename of the zone on disk
152
  string d_status; //!< message describing status of a domain, for human consumption
153
  vector<ComboAddress> d_primaries; //!< IP address of the primary of this domain
154
  set<string> d_also_notify; //!< IP list of hosts to also notify
155
  LookButDontTouch<recordstorage_t> d_records; //!< the actual records belonging to this domain
156
  time_t d_ctime{0}; //!< last known ctime of the file on disk
157
  time_t d_lastcheck{0}; //!< last time domain was checked for freshness
158
  uint32_t d_lastnotified{0}; //!< Last serial number we notified our secondaries of
159
  domainid_t d_id{0}; //!< internal id of the domain
160
  mutable bool d_checknow; //!< if this domain has been flagged for a check
161
  bool d_loaded{false}; //!< if a domain is loaded
162
  bool d_wasRejectedLastReload{false}; //!< if the domain was rejected during Bind2Backend::queueReloadAndStore
163
  bool d_nsec3zone{false};
164
  NSEC3PARAMRecordContent d_nsec3param;
165

166
private:
167
  time_t getCtime();
168
  time_t d_checkinterval{0};
169
};
170

171
class SSQLite3;
172
class NSEC3PARAMRecordContent;
173

174
struct NameTag
175
{
176
};
177

178
class Bind2Backend : public DNSBackend
179
{
180
public:
181
  Bind2Backend(const string& suffix = "", bool loadZones = true);
182
  ~Bind2Backend() override;
183
  unsigned int getCapabilities() override;
184
  void getUnfreshSecondaryInfos(vector<DomainInfo>* unfreshDomains) override;
185
  void getUpdatedPrimaries(vector<DomainInfo>& changedDomains, std::unordered_set<DNSName>& catalogs, CatalogHashMap& catalogHashes) override;
186
  bool getDomainInfo(const ZoneName& domain, DomainInfo& info, bool getSerial = true) override;
187
  time_t getCtime(const string& fname);
188
  // DNSSEC
189
  bool getBeforeAndAfterNamesAbsolute(domainid_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after) override;
190
  void lookup(const QType& qtype, const DNSName& qname, domainid_t zoneId, DNSPacket* p = nullptr) override;
191
  bool list(const ZoneName& target, domainid_t domainId, bool include_disabled = false) override;
192
  bool get(DNSResourceRecord&) override;
193
  void getAllDomains(vector<DomainInfo>* domains, bool getSerial, bool include_disabled = false) override;
194

195
  static DNSBackend* maker();
196
  static std::mutex s_startup_lock;
197

198
  void setStale(domainid_t domain_id) override;
199
  void setFresh(domainid_t domain_id) override;
200
  void setNotified(domainid_t id, uint32_t serial) override;
201
  bool startTransaction(const ZoneName& qname, domainid_t domainId) override;
202
  bool feedRecord(const DNSResourceRecord& rr, const DNSName& ordername, bool ordernameIsNSEC3 = false) override;
203
  bool commitTransaction() override;
204
  bool abortTransaction() override;
205
  void alsoNotifies(const ZoneName& domain, set<string>* ips) override;
206
  bool searchRecords(const string& pattern, size_t maxResults, vector<DNSResourceRecord>& result) override;
207

208
  // the DNSSEC related (getDomainMetadata has broader uses too)
209
  bool getAllDomainMetadata(const ZoneName& name, std::map<std::string, std::vector<std::string>>& meta) override;
210
  bool getDomainMetadata(const ZoneName& name, const std::string& kind, std::vector<std::string>& meta) override;
211
  bool setDomainMetadata(const ZoneName& name, const std::string& kind, const std::vector<std::string>& meta) override;
212
  bool getDomainKeys(const ZoneName& name, std::vector<KeyData>& keys) override;
213
  bool removeDomainKey(const ZoneName& name, unsigned int keyId) override;
214
  bool addDomainKey(const ZoneName& name, const KeyData& key, int64_t& keyId) override;
215
  bool activateDomainKey(const ZoneName& name, unsigned int keyId) override;
216
  bool deactivateDomainKey(const ZoneName& name, unsigned int keyId) override;
217
  bool publishDomainKey(const ZoneName& name, unsigned int keyId) override;
218
  bool unpublishDomainKey(const ZoneName& name, unsigned int keyId) override;
219
  bool getTSIGKey(const DNSName& name, DNSName& algorithm, string& content) override;
220
  bool setTSIGKey(const DNSName& name, const DNSName& algorithm, const string& content) override;
221
  bool deleteTSIGKey(const DNSName& name) override;
222
  bool getTSIGKeys(std::vector<struct TSIGKey>& keys) override;
223
  // end of DNSSEC
224

225
  typedef multi_index_container<BB2DomainInfo,
226
                                indexed_by<ordered_unique<member<BB2DomainInfo, domainid_t, &BB2DomainInfo::d_id>>,
227
                                           ordered_unique<tag<NameTag>, member<BB2DomainInfo, ZoneName, &BB2DomainInfo::d_name>>>>
228
    state_t;
229
  static SharedLockGuarded<state_t> s_state;
230

231
  void parseZoneFile(BB2DomainInfo* bbd);
232
  void rediscover(string* status = nullptr) override;
233

234
  // for autoprimary support
235
  bool autoPrimariesList(std::vector<AutoPrimary>& primaries) override;
236
  bool autoPrimaryBackend(const string& ipAddress, const ZoneName& domain, const vector<DNSResourceRecord>& nsset, string* nameserver, string* account, DNSBackend** backend) override;
237
  static std::mutex s_autosecondary_config_lock;
238
  bool createSecondaryDomain(const string& ipAddress, const ZoneName& domain, const string& nameserver, const string& account) override;
239

240
private:
241
  void setupDNSSEC();
242
  void setupStatements();
243
  void freeStatements();
244
  static bool safeGetBBDomainInfo(domainid_t id, BB2DomainInfo* bbd);
245
  static void safePutBBDomainInfo(const BB2DomainInfo& bbd);
246
  static bool safeGetBBDomainInfo(const ZoneName& name, BB2DomainInfo* bbd);
247
  static bool safeRemoveBBDomainInfo(const ZoneName& name);
248
  shared_ptr<SSQLite3> d_dnssecdb;
249
  bool getNSEC3PARAM(const ZoneName& name, NSEC3PARAMRecordContent* ns3p);
250
  static void setLastCheck(domainid_t domain_id, time_t lastcheck);
251
  bool getNSEC3PARAMuncached(const ZoneName& name, NSEC3PARAMRecordContent* ns3p);
252
  class handle
253
  {
254
  public:
255
    bool get(DNSResourceRecord&);
256
    void reset();
257

258
    handle();
259

260
    handle(const handle&) = delete;
261
    handle& operator=(const handle&) = delete; // don't go copying this
262

263
    shared_ptr<const recordstorage_t> d_records;
264
    recordstorage_t::index<UnorderedNameTag>::type::const_iterator d_iter, d_end_iter;
265

266
    recordstorage_t::const_iterator d_qname_iter, d_qname_end;
267

268
    DNSName qname;
269
    ZoneName domain;
270

271
    domainid_t id{UnknownDomainID};
272
    QType qtype;
273
    bool d_list{false};
274
    bool mustlog{false};
275

276
  private:
277
    bool get_normal(DNSResourceRecord&);
278
    bool get_list(DNSResourceRecord&);
279
  };
280

281
  unique_ptr<SSqlStatement> d_getAllDomainMetadataQuery_stmt;
282
  unique_ptr<SSqlStatement> d_getDomainMetadataQuery_stmt;
283
  unique_ptr<SSqlStatement> d_deleteDomainMetadataQuery_stmt;
284
  unique_ptr<SSqlStatement> d_insertDomainMetadataQuery_stmt;
285
  unique_ptr<SSqlStatement> d_getDomainKeysQuery_stmt;
286
  unique_ptr<SSqlStatement> d_deleteDomainKeyQuery_stmt;
287
  unique_ptr<SSqlStatement> d_insertDomainKeyQuery_stmt;
288
  unique_ptr<SSqlStatement> d_GetLastInsertedKeyIdQuery_stmt;
289
  unique_ptr<SSqlStatement> d_activateDomainKeyQuery_stmt;
290
  unique_ptr<SSqlStatement> d_deactivateDomainKeyQuery_stmt;
291
  unique_ptr<SSqlStatement> d_publishDomainKeyQuery_stmt;
292
  unique_ptr<SSqlStatement> d_unpublishDomainKeyQuery_stmt;
293
  unique_ptr<SSqlStatement> d_getTSIGKeyQuery_stmt;
294
  unique_ptr<SSqlStatement> d_setTSIGKeyQuery_stmt;
295
  unique_ptr<SSqlStatement> d_deleteTSIGKeyQuery_stmt;
296
  unique_ptr<SSqlStatement> d_getTSIGKeysQuery_stmt;
297

298
  ZoneName d_transaction_qname;
299
  string d_transaction_tmpname;
300
  string d_logprefix;
301
  set<string> alsoNotify; //!< this is used to store the also-notify list of interested peers.
302
  std::unique_ptr<ofstream> d_of;
303
  handle d_handle;
304
  static string s_binddirectory; //!< this is used to store the 'directory' setting of the bind configuration
305
  static int s_first; //!< this is raised on construction to prevent multiple instances of us being generated
306
  domainid_t d_transaction_id;
307
  static bool s_ignore_broken_records;
308
  bool d_hybrid;
309
  bool d_upgradeContent;
310

311
  BB2DomainInfo createDomainEntry(const ZoneName& domain, const string& filename); //!< does not insert in s_state
312

313
  void queueReloadAndStore(domainid_t id);
314
  static bool findBeforeAndAfterUnhashed(std::shared_ptr<const recordstorage_t>& records, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after);
315
  static void 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 = string(), const bool* auth = nullptr);
316
  void reload() override;
317
  static string DLDomStatusHandler(const vector<string>& parts, Utility::pid_t ppid);
318
  static string DLDomExtendedStatusHandler(const vector<string>& parts, Utility::pid_t ppid);
319
  static string DLListRejectsHandler(const vector<string>& parts, Utility::pid_t ppid);
320
  static string DLReloadNowHandler(const vector<string>& parts, Utility::pid_t ppid);
321
  static string DLAddDomainHandler(const vector<string>& parts, Utility::pid_t ppid);
322
  static void fixupOrderAndAuth(std::shared_ptr<recordstorage_t>& records, const ZoneName& zoneName, bool nsec3zone, const NSEC3PARAMRecordContent& ns3pr);
323
  static void doEmptyNonTerminals(std::shared_ptr<recordstorage_t>& records, const ZoneName& zoneName, bool nsec3zone, const NSEC3PARAMRecordContent& ns3pr);
324
  void loadConfig(string* status = nullptr);
325
};
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