2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
23 #ifndef PDNS_BINDBACKEND_HH
24 #define PDNS_BINDBACKEND_HH
33 #include <boost/utility.hpp>
35 #include <boost/tuple/tuple.hpp>
36 #include <boost/tuple/tuple_comparison.hpp>
37 #include <boost/multi_index_container.hpp>
38 #include <boost/multi_index/hashed_index.hpp>
39 #include <boost/multi_index/ordered_index.hpp>
40 #include <boost/multi_index/identity.hpp>
41 #include <boost/multi_index/member.hpp>
42 #include <sys/types.h>
45 #include "pdns/lock.hh"
46 #include "pdns/misc.hh"
47 #include "pdns/dnsbackend.hh"
48 #include "pdns/namespaces.hh"
49 #include "pdns/backends/gsql/ssql.hh"
51 using namespace ::boost::multi_index;
54 This struct is used within the Bind2Backend to store DNS information. It is
55 almost identical to a DNSResourceRecord, but then a bit smaller and with
56 different sorting rules, which make sure that the SOA record comes up front.
67 bool operator<(const Bind2DNSRecord& rhs) const
69 if(qname.canonCompare(rhs.qname))
71 if(rhs.qname.canonCompare(qname))
73 if(qtype==QType::SOA && rhs.qtype!=QType::SOA)
75 return tie(qtype,content, ttl) < tie(rhs.qtype, rhs.content, rhs.ttl);
79 struct Bind2DNSCompare : std::less<Bind2DNSRecord>
81 using std::less<Bind2DNSRecord>::operator();
83 bool operator() (const DNSName& a, const Bind2DNSRecord& b) const
84 {return a.canonCompare(b.qname);}
85 bool operator() (const Bind2DNSRecord& a, const DNSName& b) const
86 {return a.qname.canonCompare(b);}
87 bool operator() (const Bind2DNSRecord& a, const Bind2DNSRecord& b) const
88 {return a.qname.canonCompare(b.qname);}
92 struct UnorderedNameTag{};
94 typedef multi_index_container<
97 ordered_non_unique<identity<Bind2DNSRecord>, Bind2DNSCompare >,
98 hashed_non_unique<tag<UnorderedNameTag>, member<Bind2DNSRecord, DNSName, &Bind2DNSRecord::qname> >,
99 ordered_non_unique<tag<NSEC3Tag>, member<Bind2DNSRecord, std::string, &Bind2DNSRecord::nsec3hash> >
103 template <typename T>
104 class LookButDontTouch // : public boost::noncopyable
110 LookButDontTouch(shared_ptr<T> records) : d_records(records)
114 shared_ptr<const T> get()
116 shared_ptr<const T> ret;
118 std::lock_guard<std::mutex> lock(s_lock);
125 static std::mutex s_lock;
126 shared_ptr<T> d_records;
130 /** 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 //! configure how often this domain should be checked for changes (on disk)
138 void setCheckInterval(time_t seconds);
140 DNSName d_name; //!< actual name of the domain
141 DomainInfo::DomainKind d_kind; //!< the kind of domain
142 string d_filename; //!< full absolute filename of the zone on disk
143 string d_status; //!< message describing status of a domain, for human consumption
144 vector<ComboAddress> d_masters; //!< IP address of the master of this domain
145 set<string> d_also_notify; //!< IP list of hosts to also notify
146 LookButDontTouch<recordstorage_t> d_records; //!< the actual records belonging to this domain
147 time_t d_ctime{0}; //!< last known ctime of the file on disk
148 time_t d_lastcheck{0}; //!< last time domain was checked for freshness
149 uint32_t d_lastnotified{0}; //!< Last serial number we notified our slaves of
150 unsigned int d_id; //!< internal id of the domain
151 mutable bool d_checknow; //!< if this domain has been flagged for a check
152 bool d_loaded; //!< if a domain is loaded
153 bool d_wasRejectedLastReload{false}; //!< if the domain was rejected during Bind2Backend::queueReloadAndStore
157 time_t d_checkinterval;
161 class NSEC3PARAMRecordContent;
166 class Bind2Backend : public DNSBackend
169 Bind2Backend(const string &suffix="", bool loadZones=true);
171 void getUnfreshSlaveInfos(vector<DomainInfo> *unfreshDomains) override;
172 void getUpdatedMasters(vector<DomainInfo> *changedDomains) override;
173 bool getDomainInfo(const DNSName &domain, DomainInfo &di, bool getSerial=true ) override;
174 time_t getCtime(const string &fname);
176 bool getBeforeAndAfterNamesAbsolute(uint32_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after) override;
177 void lookup(const QType &, const DNSName &qdomain, int zoneId, DNSPacket *p=nullptr) override;
178 bool list(const DNSName &target, int id, bool include_disabled=false) override;
179 bool get(DNSResourceRecord &) override;
180 void getAllDomains(vector<DomainInfo> *domains, bool include_disabled=false) override;
182 static DNSBackend *maker();
183 static pthread_mutex_t s_startup_lock;
185 void setFresh(uint32_t domain_id) override;
186 void setNotified(uint32_t id, uint32_t serial) override;
187 bool startTransaction(const DNSName &qname, int id) override;
188 bool feedRecord(const DNSResourceRecord &rr, const DNSName &ordername, bool ordernameIsNSEC3=false) override;
189 bool commitTransaction() override;
190 bool abortTransaction() override;
191 void alsoNotifies(const DNSName &domain, set<string> *ips) override;
192 bool searchRecords(const string &pattern, int maxResults, vector<DNSResourceRecord>& result) override;
194 // the DNSSEC related (getDomainMetadata has broader uses too)
195 bool getAllDomainMetadata(const DNSName& name, std::map<std::string, std::vector<std::string> >& meta) override;
196 bool getDomainMetadata(const DNSName& name, const std::string& kind, std::vector<std::string>& meta) override;
197 bool setDomainMetadata(const DNSName& name, const std::string& kind, const std::vector<std::string>& meta) override;
198 bool getDomainKeys(const DNSName& name, std::vector<KeyData>& keys) override;
199 bool removeDomainKey(const DNSName& name, unsigned int id) override;
200 bool addDomainKey(const DNSName& name, const KeyData& key, int64_t& id) override;
201 bool activateDomainKey(const DNSName& name, unsigned int id) override;
202 bool deactivateDomainKey(const DNSName& name, unsigned int id) override;
203 bool getTSIGKey(const DNSName& name, DNSName* algorithm, string* content) override;
204 bool setTSIGKey(const DNSName& name, const DNSName& algorithm, const string& content) override;
205 bool deleteTSIGKey(const DNSName& name) override;
206 bool getTSIGKeys(std::vector< struct TSIGKey > &keys) override;
207 bool doesDNSSEC() override;
210 typedef multi_index_container < BB2DomainInfo ,
211 indexed_by < ordered_unique<member<BB2DomainInfo, unsigned int, &BB2DomainInfo::d_id> >,
212 ordered_unique<tag<NameTag>, member<BB2DomainInfo, DNSName, &BB2DomainInfo::d_name> >
214 static state_t s_state;
215 static pthread_rwlock_t s_state_lock;
217 void parseZoneFile(BB2DomainInfo *bbd);
218 void rediscover(string *status=nullptr) override;
221 // for supermaster support
222 bool superMasterBackend(const string &ip, const DNSName &domain, const vector<DNSResourceRecord>&nsset, string *nameserver, string *account, DNSBackend **db) override;
223 static pthread_mutex_t s_supermaster_config_lock;
224 bool createSlaveDomain(const string &ip, const DNSName &domain, const string &nameserver, const string &account) override;
228 void setupStatements();
229 void freeStatements();
230 static bool safeGetBBDomainInfo(int id, BB2DomainInfo* bbd);
231 static void safePutBBDomainInfo(const BB2DomainInfo& bbd);
232 static bool safeGetBBDomainInfo(const DNSName& name, BB2DomainInfo* bbd);
233 static bool safeRemoveBBDomainInfo(const DNSName& name);
234 shared_ptr<SSQLite3> d_dnssecdb;
235 bool getNSEC3PARAM(const DNSName& name, NSEC3PARAMRecordContent* ns3p);
239 bool get(DNSResourceRecord &);
244 shared_ptr<const recordstorage_t > d_records;
245 recordstorage_t::index<UnorderedNameTag>::type::const_iterator d_iter, d_end_iter;
247 recordstorage_t::const_iterator d_qname_iter, d_qname_end;
258 bool get_normal(DNSResourceRecord &);
259 bool get_list(DNSResourceRecord &);
261 void operator=(const handle& ); // don't go copying this
262 handle(const handle &);
265 unique_ptr<SSqlStatement> d_getAllDomainMetadataQuery_stmt;
266 unique_ptr<SSqlStatement> d_getDomainMetadataQuery_stmt;
267 unique_ptr<SSqlStatement> d_deleteDomainMetadataQuery_stmt;
268 unique_ptr<SSqlStatement> d_insertDomainMetadataQuery_stmt;
269 unique_ptr<SSqlStatement> d_getDomainKeysQuery_stmt;
270 unique_ptr<SSqlStatement> d_deleteDomainKeyQuery_stmt;
271 unique_ptr<SSqlStatement> d_insertDomainKeyQuery_stmt;
272 unique_ptr<SSqlStatement> d_GetLastInsertedKeyIdQuery_stmt;
273 unique_ptr<SSqlStatement> d_activateDomainKeyQuery_stmt;
274 unique_ptr<SSqlStatement> d_deactivateDomainKeyQuery_stmt;
275 unique_ptr<SSqlStatement> d_getTSIGKeyQuery_stmt;
276 unique_ptr<SSqlStatement> d_setTSIGKeyQuery_stmt;
277 unique_ptr<SSqlStatement> d_deleteTSIGKeyQuery_stmt;
278 unique_ptr<SSqlStatement> d_getTSIGKeysQuery_stmt;
280 string d_transaction_tmpname;
282 set<string> alsoNotify; //!< this is used to store the also-notify list of interested peers.
283 std::unique_ptr<ofstream> d_of;
285 static string s_binddirectory; //!< this is used to store the 'directory' setting of the bind configuration
286 static int s_first; //!< this is raised on construction to prevent multiple instances of us being generated
287 int d_transaction_id;
288 static bool s_ignore_broken_records;
291 BB2DomainInfo createDomainEntry(const DNSName& domain, const string &filename); //!< does not insert in s_state
293 void queueReloadAndStore(unsigned int id);
294 static bool findBeforeAndAfterUnhashed(std::shared_ptr<const recordstorage_t>& records, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after);
295 static void insertRecord(std::shared_ptr<recordstorage_t>& records, const DNSName& zoneName, const DNSName &qname, const QType &qtype, const string &content, int ttl, const std::string& hashed=string(), bool *auth=nullptr);
296 void reload() override;
297 static string DLDomStatusHandler(const vector<string>&parts, Utility::pid_t ppid);
298 static string DLListRejectsHandler(const vector<string>&parts, Utility::pid_t ppid);
299 static string DLReloadNowHandler(const vector<string>&parts, Utility::pid_t ppid);
300 static string DLAddDomainHandler(const vector<string>&parts, Utility::pid_t ppid);
301 static void fixupOrderAndAuth(std::shared_ptr<recordstorage_t>& records, const DNSName& zoneName, bool nsec3zone, NSEC3PARAMRecordContent ns3pr);
302 static void doEmptyNonTerminals(std::shared_ptr<recordstorage_t>& records, const DNSName& zoneName, bool nsec3zone, NSEC3PARAMRecordContent ns3pr);
303 void loadConfig(string *status=nullptr);
306 #endif /* PDNS_BINDBACKEND_HH */