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
32 #include <boost/utility.hpp>
34 #include <boost/tuple/tuple.hpp>
35 #include <boost/tuple/tuple_comparison.hpp>
36 #include <boost/multi_index_container.hpp>
37 #include <boost/multi_index/hashed_index.hpp>
38 #include <boost/multi_index/ordered_index.hpp>
39 #include <boost/multi_index/identity.hpp>
40 #include <boost/multi_index/member.hpp>
41 #include <sys/types.h>
44 #include "pdns/lock.hh"
45 #include "pdns/misc.hh"
46 #include "pdns/dnsbackend.hh"
47 #include "pdns/namespaces.hh"
48 #include "pdns/backends/gsql/ssql.hh"
50 using namespace ::boost::multi_index;
53 This struct is used within the Bind2Backend to store DNS information. It is
54 almost identical to a DNSResourceRecord, but then a bit smaller and with
55 different sorting rules, which make sure that the SOA record comes up front.
66 bool operator<(const Bind2DNSRecord& rhs) const
68 if(qname.canonCompare(rhs.qname))
70 if(rhs.qname.canonCompare(qname))
72 if(qtype==QType::SOA && rhs.qtype!=QType::SOA)
74 return tie(qtype,content, ttl) < tie(rhs.qtype, rhs.content, rhs.ttl);
78 struct Bind2DNSCompare : std::less<Bind2DNSRecord>
80 using std::less<Bind2DNSRecord>::operator();
82 bool operator() (const DNSName& a, const Bind2DNSRecord& b) const
83 {return a.canonCompare(b.qname);}
84 bool operator() (const Bind2DNSRecord& a, const DNSName& b) const
85 {return a.qname.canonCompare(b);}
86 bool operator() (const Bind2DNSRecord& a, const Bind2DNSRecord& b) const
87 {return a.qname.canonCompare(b.qname);}
91 struct UnorderedNameTag{};
93 typedef multi_index_container<
96 ordered_non_unique<identity<Bind2DNSRecord>, Bind2DNSCompare >,
97 hashed_non_unique<tag<UnorderedNameTag>, member<Bind2DNSRecord, DNSName, &Bind2DNSRecord::qname> >,
98 ordered_non_unique<tag<NSEC3Tag>, member<Bind2DNSRecord, std::string, &Bind2DNSRecord::nsec3hash> >
102 template <typename T>
103 class LookButDontTouch // : public boost::noncopyable
108 pthread_mutex_init(&d_lock, 0);
109 pthread_mutex_init(&d_swaplock, 0);
111 LookButDontTouch(shared_ptr<T> records) : d_records(records)
113 pthread_mutex_init(&d_lock, 0);
114 pthread_mutex_init(&d_swaplock, 0);
117 shared_ptr<const T> get()
119 shared_ptr<const T> ret;
127 shared_ptr<T> getWRITABLE()
138 void swap(shared_ptr<T> records)
141 Lock l2(&d_swaplock);
142 d_records.swap(records);
144 pthread_mutex_t d_lock;
145 pthread_mutex_t d_swaplock;
147 shared_ptr<T> d_records;
151 /** Class which describes all metadata of a domain for storage by the Bind2Backend, and also contains a pointer to a vector of Bind2DNSRecord's */
158 //! configure how often this domain should be checked for changes (on disk)
159 void setCheckInterval(time_t seconds);
161 DNSName d_name; //!< actual name of the domain
162 string d_filename; //!< full absolute filename of the zone on disk
163 string d_status; //!< message describing status of a domain, for human consumption
164 vector<string> d_masters; //!< IP address of the master of this domain
165 set<string> d_also_notify; //!< IP list of hosts to also notify
166 LookButDontTouch<recordstorage_t> d_records; //!< the actual records belonging to this domain
167 time_t d_ctime; //!< last known ctime of the file on disk
168 time_t d_lastcheck; //!< last time domain was checked for freshness
169 uint32_t d_lastnotified; //!< Last serial number we notified our slaves of
170 unsigned int d_id; //!< internal id of the domain
171 mutable bool d_checknow; //!< if this domain has been flagged for a check
172 bool d_loaded; //!< if a domain is loaded
173 bool d_wasRejectedLastReload{false}; //!< if the domain was rejected during Bind2Backend::queueReloadAndStore
177 time_t d_checkinterval;
181 class NSEC3PARAMRecordContent;
186 class Bind2Backend : public DNSBackend
189 Bind2Backend(const string &suffix="", bool loadZones=true);
191 void getUnfreshSlaveInfos(vector<DomainInfo> *unfreshDomains);
192 void getUpdatedMasters(vector<DomainInfo> *changedDomains);
193 bool getDomainInfo(const DNSName &domain, DomainInfo &di);
194 time_t getCtime(const string &fname);
196 virtual bool getBeforeAndAfterNamesAbsolute(uint32_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after);
197 void lookup(const QType &, const DNSName &qdomain, DNSPacket *p=0, int zoneId=-1);
198 bool list(const DNSName &target, int id, bool include_disabled=false);
199 bool get(DNSResourceRecord &);
200 // bool get(DNSZoneRecord &) override;
201 void getAllDomains(vector<DomainInfo> *domains, bool include_disabled=false);
203 static DNSBackend *maker();
204 static pthread_mutex_t s_startup_lock;
206 void setFresh(uint32_t domain_id);
207 void setNotified(uint32_t id, uint32_t serial);
208 bool startTransaction(const DNSName &qname, int id);
209 bool feedRecord(const DNSResourceRecord &rr, string *ordername=0);
210 bool commitTransaction();
211 bool abortTransaction();
212 void alsoNotifies(const DNSName &domain, set<string> *ips);
213 bool searchRecords(const string &pattern, int maxResults, vector<DNSResourceRecord>& result);
215 // the DNSSEC related (getDomainMetadata has broader uses too)
216 virtual bool getAllDomainMetadata(const DNSName& name, std::map<std::string, std::vector<std::string> >& meta);
217 virtual bool getDomainMetadata(const DNSName& name, const std::string& kind, std::vector<std::string>& meta);
218 virtual bool setDomainMetadata(const DNSName& name, const std::string& kind, const std::vector<std::string>& meta);
219 virtual bool getDomainKeys(const DNSName& name, unsigned int kind, std::vector<KeyData>& keys);
220 virtual bool removeDomainKey(const DNSName& name, unsigned int id);
221 virtual bool addDomainKey(const DNSName& name, const KeyData& key, int64_t& id);
222 virtual bool activateDomainKey(const DNSName& name, unsigned int id);
223 virtual bool deactivateDomainKey(const DNSName& name, unsigned int id);
224 virtual bool getTSIGKey(const DNSName& name, DNSName* algorithm, string* content);
225 virtual bool setTSIGKey(const DNSName& name, const DNSName& algorithm, const string& content);
226 virtual bool deleteTSIGKey(const DNSName& name);
227 virtual bool getTSIGKeys(std::vector< struct TSIGKey > &keys);
228 virtual bool doesDNSSEC();
231 typedef multi_index_container < BB2DomainInfo ,
232 indexed_by < ordered_unique<member<BB2DomainInfo, unsigned int, &BB2DomainInfo::d_id> >,
233 ordered_unique<tag<NameTag>, member<BB2DomainInfo, DNSName, &BB2DomainInfo::d_name> >
235 static state_t s_state;
236 static pthread_rwlock_t s_state_lock;
238 void parseZoneFile(BB2DomainInfo *bbd);
239 void insertRecord(BB2DomainInfo& bbd, const DNSName &qname, const QType &qtype, const string &content, int ttl, const std::string& hashed=string(), bool *auth=0);
240 void rediscover(string *status=0);
242 bool isMaster(const DNSName &name, const string &ip);
244 // for supermaster support
245 bool superMasterBackend(const string &ip, const DNSName &domain, const vector<DNSResourceRecord>&nsset, string *nameserver, string *account, DNSBackend **db);
246 static pthread_mutex_t s_supermaster_config_lock;
247 bool createSlaveDomain(const string &ip, const DNSName &domain, const string &nameserver, const string &account);
251 void setupStatements();
252 void freeStatements();
253 void release(SSqlStatement**);
254 static bool safeGetBBDomainInfo(int id, BB2DomainInfo* bbd);
255 static void safePutBBDomainInfo(const BB2DomainInfo& bbd);
256 static bool safeGetBBDomainInfo(const DNSName& name, BB2DomainInfo* bbd);
257 static bool safeRemoveBBDomainInfo(const DNSName& name);
258 bool GetBBDomainInfo(int id, BB2DomainInfo** bbd);
259 shared_ptr<SSQLite3> d_dnssecdb;
260 bool getNSEC3PARAM(const DNSName& name, NSEC3PARAMRecordContent* ns3p);
264 bool get(DNSResourceRecord &);
269 shared_ptr<const recordstorage_t > d_records;
270 recordstorage_t::index<UnorderedNameTag>::type::const_iterator d_iter, d_end_iter;
272 recordstorage_t::const_iterator d_qname_iter, d_qname_end;
283 bool get_normal(DNSResourceRecord &);
284 bool get_list(DNSResourceRecord &);
286 void operator=(const handle& ); // don't go copying this
287 handle(const handle &);
290 SSqlStatement* d_getAllDomainMetadataQuery_stmt;
291 SSqlStatement* d_getDomainMetadataQuery_stmt;
292 SSqlStatement* d_deleteDomainMetadataQuery_stmt;
293 SSqlStatement* d_insertDomainMetadataQuery_stmt;
294 SSqlStatement* d_getDomainKeysQuery_stmt;
295 SSqlStatement* d_deleteDomainKeyQuery_stmt;
296 SSqlStatement* d_insertDomainKeyQuery_stmt;
297 SSqlStatement* d_GetLastInsertedKeyIdQuery_stmt;
298 SSqlStatement* d_activateDomainKeyQuery_stmt;
299 SSqlStatement* d_deactivateDomainKeyQuery_stmt;
300 SSqlStatement* d_getTSIGKeyQuery_stmt;
301 SSqlStatement* d_setTSIGKeyQuery_stmt;
302 SSqlStatement* d_deleteTSIGKeyQuery_stmt;
303 SSqlStatement* d_getTSIGKeysQuery_stmt;
305 string d_transaction_tmpname;
307 set<string> alsoNotify; //!< this is used to store the also-notify list of interested peers.
310 static string s_binddirectory; //!< this is used to store the 'directory' setting of the bind configuration
311 static int s_first; //!< this is raised on construction to prevent multiple instances of us being generated
312 int d_transaction_id;
313 static bool s_ignore_broken_records;
316 BB2DomainInfo createDomainEntry(const DNSName& domain, const string &filename); //!< does not insert in s_state
318 void queueReloadAndStore(unsigned int id);
319 bool findBeforeAndAfterUnhashed(BB2DomainInfo& bbd, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after);
321 static string DLDomStatusHandler(const vector<string>&parts, Utility::pid_t ppid);
322 static string DLListRejectsHandler(const vector<string>&parts, Utility::pid_t ppid);
323 static string DLReloadNowHandler(const vector<string>&parts, Utility::pid_t ppid);
324 static string DLAddDomainHandler(const vector<string>&parts, Utility::pid_t ppid);
325 static void fixupOrderAndAuth(BB2DomainInfo& bbd, bool nsec3zone, NSEC3PARAMRecordContent ns3pr);
326 void doEmptyNonTerminals(BB2DomainInfo& bbd, bool nsec3zone, NSEC3PARAMRecordContent ns3pr);
327 void loadConfig(string *status=0);
328 static void nukeZoneRecords(BB2DomainInfo *bbd);
332 #endif /* PDNS_BINDBACKEND_HH */