File: bindbackend2.hh

package info (click to toggle)
pdns 5.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,824 kB
  • sloc: cpp: 101,240; sh: 5,616; makefile: 2,318; sql: 860; ansic: 675; python: 635; yacc: 245; perl: 161; lex: 131
file content (325 lines) | stat: -rw-r--r-- 13,392 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * This file is part of PowerDNS or dnsdist.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#pragma once
#include <string>
#include <map>
#include <set>
#include <pthread.h>
#include <time.h>
#include <fstream>
#include <mutex>
#include <boost/utility.hpp>

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "pdns/lock.hh"
#include "pdns/misc.hh"
#include "pdns/dnsbackend.hh"
#include "pdns/namespaces.hh"
#include "pdns/backends/gsql/ssql.hh"

using namespace ::boost::multi_index;

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

struct Bind2DNSRecord
{
  DNSName qname;
  string content;
  string nsec3hash;
  uint32_t ttl;
  uint16_t qtype;
  mutable bool auth;
  bool operator<(const Bind2DNSRecord& rhs) const
  {
    if (int rc = qname.canonCompare_three_way(rhs.qname); rc != 0) {
      return rc < 0;
    }
    if (qtype == QType::SOA && rhs.qtype != QType::SOA)
      return true;
    return std::tie(qtype, content, ttl) < std::tie(rhs.qtype, rhs.content, rhs.ttl);
  }
};

struct Bind2DNSCompare : std::less<Bind2DNSRecord>
{
  using std::less<Bind2DNSRecord>::operator();
  // use operator<
  bool operator()(const DNSName& a, const Bind2DNSRecord& b) const
  {
    return a.canonCompare(b.qname);
  }
  bool operator()(const Bind2DNSRecord& a, const DNSName& b) const
  {
    return a.qname.canonCompare(b);
  }
  bool operator()(const Bind2DNSRecord& a, const Bind2DNSRecord& b) const
  {
    return a.qname.canonCompare(b.qname);
  }
};

struct NSEC3Tag
{
};
struct UnorderedNameTag
{
};

typedef multi_index_container<
  Bind2DNSRecord,
  indexed_by<
    ordered_non_unique<identity<Bind2DNSRecord>, Bind2DNSCompare>,
    hashed_non_unique<tag<UnorderedNameTag>, member<Bind2DNSRecord, DNSName, &Bind2DNSRecord::qname>>,
    ordered_non_unique<tag<NSEC3Tag>, member<Bind2DNSRecord, std::string, &Bind2DNSRecord::nsec3hash>>>>
  recordstorage_t;

template <typename T>
class LookButDontTouch
{
public:
  LookButDontTouch() = default;
  LookButDontTouch(shared_ptr<T>&& records) :
    d_records(std::move(records))
  {
  }

  shared_ptr<const T> get()
  {
    return d_records;
  }

  size_t getEntriesCount() const
  {
    if (!d_records) {
      return 0;
    }
    return d_records->size();
  }

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

/** Class which describes all metadata of a domain for storage by the Bind2Backend, and also contains a pointer to a vector of Bind2DNSRecord's */
class BB2DomainInfo
{
public:
  BB2DomainInfo();
  void setCtime();
  bool current();
  //! configure how often this domain should be checked for changes (on disk)
  void setCheckInterval(time_t seconds);
  time_t getCheckInterval() const
  {
    return d_checkinterval;
  }

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

private:
  time_t getCtime();
  time_t d_checkinterval{0};
};

class SSQLite3;
class NSEC3PARAMRecordContent;

struct NameTag
{
};

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

  static DNSBackend* maker();
  static std::mutex s_startup_lock;

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

  // the DNSSEC related (getDomainMetadata has broader uses too)
  bool getAllDomainMetadata(const ZoneName& name, std::map<std::string, std::vector<std::string>>& meta) override;
  bool getDomainMetadata(const ZoneName& name, const std::string& kind, std::vector<std::string>& meta) override;
  bool setDomainMetadata(const ZoneName& name, const std::string& kind, const std::vector<std::string>& meta) override;
  bool getDomainKeys(const ZoneName& name, std::vector<KeyData>& keys) override;
  bool removeDomainKey(const ZoneName& name, unsigned int keyId) override;
  bool addDomainKey(const ZoneName& name, const KeyData& key, int64_t& keyId) override;
  bool activateDomainKey(const ZoneName& name, unsigned int keyId) override;
  bool deactivateDomainKey(const ZoneName& name, unsigned int keyId) override;
  bool publishDomainKey(const ZoneName& name, unsigned int keyId) override;
  bool unpublishDomainKey(const ZoneName& name, unsigned int keyId) override;
  bool getTSIGKey(const DNSName& name, DNSName& algorithm, string& content) override;
  bool setTSIGKey(const DNSName& name, const DNSName& algorithm, const string& content) override;
  bool deleteTSIGKey(const DNSName& name) override;
  bool getTSIGKeys(std::vector<struct TSIGKey>& keys) override;
  // end of DNSSEC

  typedef multi_index_container<BB2DomainInfo,
                                indexed_by<ordered_unique<member<BB2DomainInfo, domainid_t, &BB2DomainInfo::d_id>>,
                                           ordered_unique<tag<NameTag>, member<BB2DomainInfo, ZoneName, &BB2DomainInfo::d_name>>>>
    state_t;
  static SharedLockGuarded<state_t> s_state;

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

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

private:
  void setupDNSSEC();
  void setupStatements();
  void freeStatements();
  static bool safeGetBBDomainInfo(domainid_t id, BB2DomainInfo* bbd);
  static void safePutBBDomainInfo(const BB2DomainInfo& bbd);
  static bool safeGetBBDomainInfo(const ZoneName& name, BB2DomainInfo* bbd);
  static bool safeRemoveBBDomainInfo(const ZoneName& name);
  shared_ptr<SSQLite3> d_dnssecdb;
  bool getNSEC3PARAM(const ZoneName& name, NSEC3PARAMRecordContent* ns3p);
  static void setLastCheck(domainid_t domain_id, time_t lastcheck);
  bool getNSEC3PARAMuncached(const ZoneName& name, NSEC3PARAMRecordContent* ns3p);
  class handle
  {
  public:
    bool get(DNSResourceRecord&);
    void reset();

    handle();

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

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

    recordstorage_t::const_iterator d_qname_iter, d_qname_end;

    DNSName qname;
    ZoneName domain;

    domainid_t id{UnknownDomainID};
    QType qtype;
    bool d_list{false};
    bool mustlog{false};

  private:
    bool get_normal(DNSResourceRecord&);
    bool get_list(DNSResourceRecord&);
  };

  unique_ptr<SSqlStatement> d_getAllDomainMetadataQuery_stmt;
  unique_ptr<SSqlStatement> d_getDomainMetadataQuery_stmt;
  unique_ptr<SSqlStatement> d_deleteDomainMetadataQuery_stmt;
  unique_ptr<SSqlStatement> d_insertDomainMetadataQuery_stmt;
  unique_ptr<SSqlStatement> d_getDomainKeysQuery_stmt;
  unique_ptr<SSqlStatement> d_deleteDomainKeyQuery_stmt;
  unique_ptr<SSqlStatement> d_insertDomainKeyQuery_stmt;
  unique_ptr<SSqlStatement> d_GetLastInsertedKeyIdQuery_stmt;
  unique_ptr<SSqlStatement> d_activateDomainKeyQuery_stmt;
  unique_ptr<SSqlStatement> d_deactivateDomainKeyQuery_stmt;
  unique_ptr<SSqlStatement> d_publishDomainKeyQuery_stmt;
  unique_ptr<SSqlStatement> d_unpublishDomainKeyQuery_stmt;
  unique_ptr<SSqlStatement> d_getTSIGKeyQuery_stmt;
  unique_ptr<SSqlStatement> d_setTSIGKeyQuery_stmt;
  unique_ptr<SSqlStatement> d_deleteTSIGKeyQuery_stmt;
  unique_ptr<SSqlStatement> d_getTSIGKeysQuery_stmt;

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

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

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