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

PowerDNS / pdns / 20618548088

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

Pull #16693

github

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

39009 of 65430 branches covered (59.62%)

Branch coverage included in aggregate %.

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

200 existing lines in 39 files now uncovered.

129187 of 166092 relevant lines covered (77.78%)

5266744.49 hits per line

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

37.84
/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/logr.hh"
42
#include "pdns/misc.hh"
43
#include "pdns/dnsbackend.hh"
44
#include "pdns/namespaces.hh"
45
#include "pdns/backends/gsql/ssql.hh"
46

47
using namespace ::boost::multi_index;
48

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

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

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

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

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

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

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

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

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

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

150
  ZoneName d_name; //!< actual name of the domain
151
  DomainInfo::DomainKind d_kind{DomainInfo::Native}; //!< the kind of domain
152
  std::vector<std::pair<std::string, time_t>> d_fileinfo; //!< list of full absolute filename of the zone on disk, and any included file, with their last verified ctime
153
  string d_status; //!< message describing status of a domain, for human consumption
154
  vector<ComboAddress> d_primaries; //!< IP address of the primary of this domain
155
  set<string> d_also_notify; //!< IP list of hosts to also notify
156
  LookButDontTouch<recordstorage_t> d_records; //!< the actual records belonging to this domain
157
  time_t d_lastcheck{0}; //!< last time files were 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
  // Sugar for the main filename. Only use if d_fileinfo is NOT empty!
167
  const std::string& main_filename() const { return d_fileinfo.front().first; }
3,181✔
168

169
private:
170
  static time_t getCtime(const std::string&);
171
  time_t d_checkinterval{0};
172
};
173

174
class SSQLite3;
175
class NSEC3PARAMRecordContent;
176

177
struct NameTag
178
{
179
};
180

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

198
  static std::mutex s_startup_lock;
199

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

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

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

233
  void parseZoneFile(BB2DomainInfo* bbd);
234
  void rediscover(string* status = nullptr) override;
235

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

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

260
    handle() = default;
3,370✔
261
    handle(const handle&) = delete;
262
    handle& operator=(const handle&) = delete; // don't go copying this
263

NEW
264
    void setSLog(std::shared_ptr<Logr::Logger> log) { d_slog = log; }
×
265

266
    shared_ptr<const recordstorage_t> d_records;
267
    recordstorage_t::index<UnorderedNameTag>::type::const_iterator d_iter, d_end_iter;
268

269
    recordstorage_t::const_iterator d_qname_iter, d_qname_end;
270

271
    DNSName qname;
272
    ZoneName domain;
273

274
    domainid_t id{UnknownDomainID};
275
    QType qtype;
276
    bool d_list{false};
277
    bool mustlog{false};
278
    std::shared_ptr<Logr::Logger> d_slog;
279

280
  private:
281
    bool get_normal(DNSResourceRecord&);
282
    bool get_list(DNSResourceRecord&);
283
  };
284

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

302
  string d_logprefix;
303

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

316
  BB2DomainInfo createDomainEntry(const ZoneName& domain); //!< does not insert in s_state
317

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