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.
30 #include <sys/types.h>
31 #include "pdnsexception.hh"
34 #include <sys/socket.h>
40 #include "namespaces.hh"
43 #include "dnsrecords.hh"
49 DomainInfo() : last_check(0), backend(NULL), id(0), notified_serial(0), serial(0), kind(DomainInfo::Native) {}
54 vector<ComboAddress> masters;
58 uint32_t notified_serial;
61 enum DomainKind : uint8_t { Master, Slave, Native } kind;
63 bool operator<(const DomainInfo& rhs) const
65 return zone < rhs.zone;
68 const char *getKindString() const
70 return DomainInfo::getKindString(kind);
73 static const char *getKindString(enum DomainKind kind)
75 const char *kinds[]={"Master", "Slave", "Native"};
79 static DomainKind stringToKind(const string& kind)
81 if(pdns_iequals(kind,"SLAVE"))
82 return DomainInfo::Slave;
83 else if(pdns_iequals(kind,"MASTER"))
84 return DomainInfo::Master;
86 return DomainInfo::Native;
89 bool isMaster(const ComboAddress& ip) const
91 for( const auto& master: masters) {
92 if(ComboAddress::addressOnlyEqual()(ip, master))
108 //! This virtual base class defines the interface for backends for PowerDNS.
109 /** To create a backend, inherit from this class and implement functions for all virtual methods.
110 Methods should not throw an exception if they are sure they did not find the requested data. However,
111 if an error occurred which prevented them temporarily from performing a lockup, they should throw a DBException,
112 which will cause the nameserver to send out a ServFail or take other evasive action. Probably only locking
113 issues should lead to DBExceptions.
115 More serious errors, which may indicate that the database connection is hosed, or a configuration error occurred, should
116 lead to the throwing of an PDNSException. This exception will fall straight through the UeberBackend and the PacketHandler
117 and be caught by the Distributor, which will delete your DNSBackend instance and spawn a new one.
122 //! lookup() initiates a lookup. A lookup without results should not throw!
123 virtual void lookup(const QType &qtype, const DNSName &qdomain, int zoneId=-1, DNSPacket *pkt_p=nullptr)=0;
124 virtual bool get(DNSResourceRecord &)=0; //!< retrieves one DNSResource record, returns false if no more were available
125 virtual bool get(DNSZoneRecord &r);
127 //! Initiates a list of the specified domain
128 /** Once initiated, DNSResourceRecord objects can be retrieved using get(). Should return false
129 if the backend does not consider itself responsible for the id passed.
130 \param domain_id ID of which a list is requested
132 virtual bool list(const DNSName &target, int domain_id, bool include_disabled=false)=0;
134 virtual ~DNSBackend(){};
136 //! fills the soadata struct with the SOA details. Returns false if there is no SOA.
137 virtual bool getSOA(const DNSName &name, SOAData &soadata);
139 virtual bool replaceRRSet(uint32_t domain_id, const DNSName& qname, const QType& qt, const vector<DNSResourceRecord>& rrset)
144 virtual bool listSubZone(const DNSName &zone, int domain_id)
149 // the DNSSEC related (getDomainMetadata has broader uses too)
150 bool isDnssecDomainMetadata (const string& name) {
151 return (name == "PRESIGNED" || name == "NSEC3PARAM" || name == "NSEC3NARROW");
153 virtual bool getAllDomainMetadata(const DNSName& name, std::map<std::string, std::vector<std::string> >& meta) { return false; };
154 virtual bool getDomainMetadata(const DNSName& name, const std::string& kind, std::vector<std::string>& meta) { return false; }
155 virtual bool getDomainMetadataOne(const DNSName& name, const std::string& kind, std::string& value)
157 std::vector<std::string> meta;
158 if (getDomainMetadata(name, kind, meta)) {
160 value = *meta.begin();
167 virtual bool setDomainMetadata(const DNSName& name, const std::string& kind, const std::vector<std::string>& meta) {return false;}
168 virtual bool setDomainMetadataOne(const DNSName& name, const std::string& kind, const std::string& value)
170 const std::vector<std::string> meta(1, value);
171 return setDomainMetadata(name, kind, meta);
175 virtual void getAllDomains(vector<DomainInfo> *domains, bool include_disabled=false) { }
177 /** Determines if we are authoritative for a zone, and at what level */
178 virtual bool getAuth(const DNSName &target, SOAData *sd);
188 virtual bool getDomainKeys(const DNSName& name, std::vector<KeyData>& keys) { return false;}
189 virtual bool removeDomainKey(const DNSName& name, unsigned int id) { return false; }
190 virtual bool addDomainKey(const DNSName& name, const KeyData& key, int64_t& id){ return false; }
191 virtual bool activateDomainKey(const DNSName& name, unsigned int id) { return false; }
192 virtual bool deactivateDomainKey(const DNSName& name, unsigned int id) { return false; }
193 virtual bool publishDomainKey(const DNSName& name, unsigned int id) { return false; }
194 virtual bool unpublishDomainKey(const DNSName& name, unsigned int id) { return false; }
196 virtual bool getTSIGKey(const DNSName& name, DNSName* algorithm, string* content) { return false; }
197 virtual bool setTSIGKey(const DNSName& name, const DNSName& algorithm, const string& content) { return false; }
198 virtual bool deleteTSIGKey(const DNSName& name) { return false; }
199 virtual bool getTSIGKeys(std::vector< struct TSIGKey > &keys) { return false; }
201 virtual bool getBeforeAndAfterNamesAbsolute(uint32_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after)
203 std::cerr<<"Default beforeAndAfterAbsolute called!"<<std::endl;
208 virtual bool getBeforeAndAfterNames(uint32_t id, const DNSName& zonename, const DNSName& qname, DNSName& before, DNSName& after);
210 virtual bool updateDNSSECOrderNameAndAuth(uint32_t domain_id, const DNSName& qname, const DNSName& ordername, bool auth, const uint16_t qtype=QType::ANY)
215 virtual bool updateEmptyNonTerminals(uint32_t domain_id, set<DNSName>& insert, set<DNSName>& erase, bool remove)
220 virtual bool doesDNSSEC()
228 virtual bool listComments(uint32_t domain_id)
230 return false; // unsupported by this backend
233 virtual bool getComment(Comment& comment)
238 virtual void feedComment(const Comment& comment)
242 virtual bool replaceComments(const uint32_t domain_id, const DNSName& qname, const QType& qt, const vector<Comment>& comments)
247 //! returns true if master ip is master for domain name.
248 //! starts the transaction for updating domain qname (FIXME: what is id?)
249 virtual bool startTransaction(const DNSName &qname, int id=-1)
254 //! commits the transaction started by startTransaction
255 virtual bool commitTransaction()
260 //! aborts the transaction started by strartTransaction, should leave state unaltered
261 virtual bool abortTransaction()
266 virtual void reload()
270 virtual void rediscover(string* status=0)
274 //! feeds a record to a zone, needs a call to startTransaction first
275 virtual bool feedRecord(const DNSResourceRecord &rr, const DNSName &ordername, bool ordernameIsNSEC3=false)
277 return false; // no problem!
279 virtual bool feedEnts(int domain_id, map<DNSName,bool> &nonterm)
283 virtual bool feedEnts3(int domain_id, const DNSName &domain, map<DNSName,bool> &nonterm, const NSEC3PARAMRecordContent& ns3prc, bool narrow)
288 //! if this returns true, DomainInfo di contains information about the domain
289 virtual bool getDomainInfo(const DNSName &domain, DomainInfo &di, bool getSerial=true)
293 //! slave capable backends should return a list of slaves that should be rechecked for staleness
294 virtual void getUnfreshSlaveInfos(vector<DomainInfo>* domains)
298 //! get a list of IP addresses that should also be notified for a domain
299 virtual void alsoNotifies(const DNSName &domain, set<string> *ips)
303 //! get list of domains that have been changed since their last notification to slaves
304 virtual void getUpdatedMasters(vector<DomainInfo>* domains)
308 //! Called by PowerDNS to inform a backend that a domain has been checked for freshness
309 virtual void setFresh(uint32_t domain_id)
313 //! Called by PowerDNS to inform a backend that the changes in the domain have been reported to slaves
314 virtual void setNotified(uint32_t id, uint32_t serial)
318 //! Called when the Master of a domain should be changed
319 virtual bool setMaster(const DNSName &domain, const string &ip)
324 //! Called when the Kind of a domain should be changed (master -> native and similar)
325 virtual bool setKind(const DNSName &domain, const DomainInfo::DomainKind kind)
330 //! Called when the Account of a domain should be changed
331 virtual bool setAccount(const DNSName &domain, const string &account)
336 //! Can be called to seed the getArg() function with a prefix
337 void setArgPrefix(const string &prefix);
339 //! determine if ip is a supermaster or a domain
340 virtual bool superMasterBackend(const string &ip, const DNSName &domain, const vector<DNSResourceRecord>&nsset, string *nameserver, string *account, DNSBackend **db)
345 //! called by PowerDNS to create a new domain
346 virtual bool createDomain(const DNSName &domain)
351 //! called by PowerDNS to create a slave record for a superMaster
352 virtual bool createSlaveDomain(const string &ip, const DNSName &domain, const string &nameserver, const string &account)
357 //! called to delete a domain, incl. all metadata, zone contents, etc.
358 virtual bool deleteDomain(const DNSName &domain)
363 virtual string directBackendCmd(const string &query)
365 return "directBackendCmd not supported for this backend\n";
368 //! Search for records, returns true if search was done successfully.
369 virtual bool searchRecords(const string &pattern, int maxResults, vector<DNSResourceRecord>& result)
374 //! Search for comments, returns true if search was done successfully.
375 virtual bool searchComments(const string &pattern, int maxResults, vector<Comment>& result)
380 const string& getPrefix() { return d_prefix; };
382 bool mustDo(const string &key);
383 const string &getArg(const string &key);
384 int getArgAsNum(const string &key);
393 BackendFactory(const string &name) : d_name(name) {}
394 virtual ~BackendFactory(){}
395 virtual DNSBackend *make(const string &suffix)=0;
396 virtual DNSBackend *makeMetadataOnly(const string &suffix)
398 return this->make(suffix);
400 virtual void declareArguments(const string &suffix=""){}
401 const string &getName() const;
404 void declare(const string &suffix, const string ¶m, const string &explanation, const string &value);
410 class BackendMakerClass
413 void report(BackendFactory *bf);
414 void launch(const string &instr);
415 vector<DNSBackend *>all(bool skipBIND=false);
416 void load(const string &module);
417 int numLauncheable();
418 vector<string> getModules();
422 typedef map<string,BackendFactory *>d_repository_t;
423 d_repository_t d_repository;
424 vector<pair<string,string> >d_instances;
427 extern BackendMakerClass &BackendMakers();
429 //! Exception that can be thrown by a DNSBackend to indicate a failure
430 class DBException : public PDNSException
433 DBException(const string &reason_) : PDNSException(reason_){}
436 /** helper function for both DNSPacket and addSOARecord() - converts a line into a struct, for easier parsing */
437 void fillSOAData(const string &content, SOAData &data);
438 // same but more karmic
439 void fillSOAData(const DNSZoneRecord& in, SOAData& data);
441 std::shared_ptr<DNSRecordContent> makeSOAContent(const SOAData& sd);