]>
Commit | Line | Data |
---|---|---|
12c86877 BH |
1 | /* |
2 | PowerDNS Versatile Database Driven Nameserver | |
66dba9d7 | 3 | Copyright (C) 2002-2010 PowerDNS.COM BV |
12c86877 BH |
4 | |
5 | This program is free software; you can redistribute it and/or modify | |
22dc646a BH |
6 | it under the terms of the GNU General Public License version 2 |
7 | as published by the Free Software Foundation | |
8 | ||
12c86877 BH |
9 | |
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
06bd9ccf | 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
12c86877 | 18 | */ |
12c86877 BH |
19 | #ifndef DNSBACKEND_HH |
20 | #define DNSBACKEND_HH | |
21 | ||
973ad2b5 BH |
22 | class DNSPacket; |
23 | ||
12c86877 BH |
24 | #include "utility.hh" |
25 | #include <string> | |
26 | #include <vector> | |
27 | #include <map> | |
28 | #include <sys/types.h> | |
cc3afe25 | 29 | #include "ahuexception.hh" |
973ad2b5 | 30 | #include <set> |
702b226d | 31 | #include <iostream> |
973ad2b5 BH |
32 | |
33 | #ifndef WIN32 | |
34 | # include <sys/socket.h> | |
35 | # include <dirent.h> | |
36 | #endif // WIN32 | |
12c86877 BH |
37 | |
38 | #include "qtype.hh" | |
39 | #include "dns.hh" | |
e5b11b2f | 40 | #include <vector> |
10f4eea8 | 41 | #include "namespaces.hh" |
12c86877 BH |
42 | |
43 | class DNSBackend; | |
44 | struct DomainInfo | |
45 | { | |
90c9a70e | 46 | DomainInfo() : backend(0) {} |
092f210a | 47 | uint32_t id; |
12c86877 | 48 | string zone; |
e5b11b2f | 49 | vector<string> masters; |
092f210a BH |
50 | uint32_t notified_serial; |
51 | uint32_t serial; | |
12c86877 BH |
52 | time_t last_check; |
53 | enum {Master,Slave,Native} kind; | |
54 | DNSBackend *backend; | |
7f3d870e BH |
55 | |
56 | bool operator<(const DomainInfo& rhs) const | |
57 | { | |
58 | return zone < rhs.zone; | |
59 | } | |
12c86877 BH |
60 | }; |
61 | ||
62 | class DNSPacket; | |
bdc9f8d2 BH |
63 | |
64 | ||
65 | //! This virtual base class defines the interface for backends for the ahudns. | |
66 | /** To create a backend, inherit from this class and implement functions for all virtual methods. | |
67 | Methods should not throw an exception if they are sure they did not find the requested data. However, | |
68 | if an error occurred which prevented them temporarily from performing a lockup, they should throw a DBException, | |
69 | which will cause the nameserver to send out a ServFail or take other evasive action. Probably only locking | |
70 | issues should lead to DBExceptions. | |
71 | ||
72 | More serious errors, which may indicate that the database connection is hosed, or a configuration error occurred, should | |
73 | lead to the throwing of an AhuException. This exception will fall straight through the UeberBackend and the PacketHandler | |
74 | and be caught by the Distributor, which will delete your DNSBackend instance and spawn a new one. | |
75 | */ | |
12c86877 BH |
76 | class DNSBackend |
77 | { | |
78 | public: | |
bdc9f8d2 | 79 | //! lookup() initiates a lookup. A lookup without results should not throw! |
12c86877 | 80 | virtual void lookup(const QType &qtype, const string &qdomain, DNSPacket *pkt_p=0, int zoneId=-1)=0; |
bdc9f8d2 | 81 | virtual bool get(DNSResourceRecord &)=0; //!< retrieves one DNSResource record, returns false if no more were available |
702b226d | 82 | |
5c3bf2db | 83 | |
bdc9f8d2 BH |
84 | //! Initiates a list of the specified domain |
85 | /** Once initiated, DNSResourceRecord objects can be retrieved using get(). Should return false | |
86 | if the backend does not consider itself responsible for the id passed. | |
87 | \param domain_id ID of which a list is requested | |
88 | */ | |
88def049 | 89 | virtual bool list(const string &target, int domain_id)=0; |
12c86877 BH |
90 | |
91 | virtual ~DNSBackend(){}; | |
92 | ||
bdc9f8d2 | 93 | //! fills the soadata struct with the SOA details. Returns false if there is no SOA. |
35933370 | 94 | virtual bool getSOA(const string &name, SOAData &soadata, DNSPacket *p=0); |
12c86877 | 95 | |
d07fc616 PD |
96 | //! Calculates a SOA serial for the zone and stores it in the third argument. |
97 | virtual bool calculateSOASerial(const string& domain, const SOAData& sd, time_t& serial); | |
98 | ||
4aafcb39 | 99 | // the DNSSEC related (getDomainMetadata has broader uses too) |
8ab63616 BH |
100 | virtual bool getDomainMetadata(const string& name, const std::string& kind, std::vector<std::string>& meta) { return false; } |
101 | virtual bool setDomainMetadata(const string& name, const std::string& kind, const std::vector<std::string>& meta) {return false;} | |
4aafcb39 | 102 | |
1325e8a2 PD |
103 | virtual void getAllDomains(vector<DomainInfo> *domains) { } |
104 | ||
4aafcb39 BH |
105 | struct KeyData { |
106 | unsigned int id; | |
107 | unsigned int flags; | |
108 | bool active; | |
109 | std::string content; | |
110 | }; | |
111 | ||
8ab63616 BH |
112 | virtual bool getDomainKeys(const string& name, unsigned int kind, std::vector<KeyData>& keys) { return false;} |
113 | virtual bool removeDomainKey(const string& name, unsigned int id) { return false; } | |
114 | virtual int addDomainKey(const string& name, const KeyData& key){ return -1; } | |
115 | virtual bool activateDomainKey(const string& name, unsigned int id) { return false; } | |
116 | virtual bool deactivateDomainKey(const string& name, unsigned int id) { return false; } | |
117 | ||
78bcb858 BH |
118 | virtual bool getTSIGKey(const string& name, string* algorithm, string* content) { return false; } |
119 | ||
4aafcb39 BH |
120 | virtual bool getBeforeAndAfterNamesAbsolute(uint32_t id, const std::string& qname, std::string& unhashed, std::string& before, std::string& after) |
121 | { | |
122 | std::cerr<<"Default beforeAndAfterAbsolute called!"<<std::endl; | |
123 | abort(); | |
124 | return false; | |
125 | } | |
126 | ||
127 | bool getBeforeAndAfterNames(uint32_t id, const std::string& zonename, const std::string& qname, std::string& before, std::string& after); | |
128 | ||
129 | virtual bool updateDNSSECOrderAndAuth(uint32_t domain_id, const std::string& zonename, const std::string& qname, bool auth) | |
130 | { | |
131 | return false; | |
132 | } | |
133 | ||
134 | virtual bool updateDNSSECOrderAndAuthAbsolute(uint32_t domain_id, const std::string& qname, const std::string& ordername, bool auth) | |
135 | { | |
136 | return false; | |
137 | } | |
138 | ||
b5baefaf PD |
139 | virtual bool updateEmptyNonTerminals(uint32_t domain_id, const std::string& zonename, set<string>& insert, set<string>& erase, bool remove) |
140 | { | |
141 | return false; | |
142 | } | |
143 | ||
144 | virtual bool nullifyDNSSECOrderName(uint32_t domain_id, const std::string& qname) | |
145 | { | |
146 | return false; | |
147 | } | |
148 | ||
27045410 PD |
149 | virtual bool nullifyDNSSECOrderNameAndAuth(uint32_t domain_id, const std::string& qname, const std::string& type) |
150 | { | |
151 | return false; | |
152 | } | |
153 | ||
ece45ffb PD |
154 | virtual bool doesDNSSEC() |
155 | { | |
156 | return false; | |
157 | } | |
158 | ||
4aafcb39 BH |
159 | // end DNSSEC |
160 | ||
bdc9f8d2 | 161 | //! returns true if master ip is master for domain name. |
12c86877 BH |
162 | virtual bool isMaster(const string &name, const string &ip) |
163 | { | |
164 | return false; | |
165 | } | |
166 | ||
bdc9f8d2 | 167 | //! starts the transaction for updating domain qname (FIXME: what is id?) |
12c86877 BH |
168 | virtual bool startTransaction(const string &qname, int id=-1) |
169 | { | |
170 | return false; | |
171 | } | |
172 | ||
bdc9f8d2 | 173 | //! commits the transaction started by startTransaction |
12c86877 BH |
174 | virtual bool commitTransaction() |
175 | { | |
176 | return false; | |
177 | } | |
178 | ||
bdc9f8d2 | 179 | //! aborts the transaction started by strartTransaction, should leave state unaltered |
12c86877 BH |
180 | virtual bool abortTransaction() |
181 | { | |
182 | return false; | |
183 | } | |
184 | ||
185 | virtual void reload() | |
186 | { | |
187 | } | |
188 | ||
973ad2b5 | 189 | virtual void rediscover(string* status=0) |
12c86877 BH |
190 | { |
191 | } | |
192 | ||
bdc9f8d2 | 193 | //! feeds a record to a zone, needs a call to startTransaction first |
12c86877 BH |
194 | virtual bool feedRecord(const DNSResourceRecord &rr) |
195 | { | |
196 | return false; // no problem! | |
197 | } | |
bdc9f8d2 | 198 | //! if this returns true, DomainInfo di contains information about the domain |
12c86877 BH |
199 | virtual bool getDomainInfo(const string &domain, DomainInfo &di) |
200 | { | |
201 | return false; | |
202 | } | |
bdc9f8d2 | 203 | //! slave capable backends should return a list of slaves that should be rechecked for staleness |
12c86877 BH |
204 | virtual void getUnfreshSlaveInfos(vector<DomainInfo>* domains) |
205 | { | |
206 | } | |
207 | ||
bdc9f8d2 | 208 | //! get a list of IP addresses that should also be notified for a domain |
12c86877 BH |
209 | virtual void alsoNotifies(const string &domain, set<string> *ips) |
210 | { | |
211 | } | |
bdc9f8d2 BH |
212 | |
213 | //! get list of domains that have been changed since their last notification to slaves | |
12c86877 BH |
214 | virtual void getUpdatedMasters(vector<DomainInfo>* domains) |
215 | { | |
216 | } | |
bdc9f8d2 BH |
217 | |
218 | //! Called by PowerDNS to inform a backend that a domain has been checked for freshness | |
092f210a | 219 | virtual void setFresh(uint32_t domain_id) |
12c86877 BH |
220 | { |
221 | ||
222 | } | |
bdc9f8d2 | 223 | //! Called by PowerDNS to inform a backend that the changes in the domain have been reported to slaves |
092f210a | 224 | virtual void setNotified(uint32_t id, uint32_t serial) |
12c86877 BH |
225 | { |
226 | } | |
227 | ||
bdc9f8d2 | 228 | //! Can be called to seed the getArg() function with a prefix |
12c86877 | 229 | void setArgPrefix(const string &prefix); |
bdc9f8d2 BH |
230 | |
231 | //! determine if ip is a supermaster or a domain | |
12c86877 BH |
232 | virtual bool superMasterBackend(const string &ip, const string &domain, const vector<DNSResourceRecord>&nsset, string *account, DNSBackend **db) |
233 | { | |
234 | return false; | |
235 | } | |
bdc9f8d2 BH |
236 | |
237 | //! called by PowerDNS to create a slave record for a superMaster | |
12c86877 BH |
238 | virtual bool createSlaveDomain(const string &ip, const string &domain, const string &account) |
239 | { | |
240 | return false; | |
241 | } | |
242 | ||
243 | protected: | |
244 | bool mustDo(const string &key); | |
245 | const string &getArg(const string &key); | |
246 | int getArgAsNum(const string &key); | |
247 | string getRemote(DNSPacket *p); | |
248 | bool getRemote(DNSPacket *p, struct sockaddr *in, Utility::socklen_t *len); | |
249 | ||
250 | private: | |
251 | string d_prefix; | |
252 | }; | |
253 | ||
254 | class BackendFactory | |
255 | { | |
256 | public: | |
257 | BackendFactory(const string &name) : d_name(name) {} | |
258 | virtual ~BackendFactory(){} | |
259 | virtual DNSBackend *make(const string &suffix)=0; | |
2717b8b3 BH |
260 | virtual DNSBackend *makeMetadataOnly(const string &suffix) |
261 | { | |
262 | return this->make(suffix); | |
263 | } | |
12c86877 BH |
264 | virtual void declareArguments(const string &suffix=""){} |
265 | const string &getName() const; | |
266 | ||
267 | protected: | |
268 | void declare(const string &suffix, const string ¶m, const string &explanation, const string &value); | |
269 | ||
270 | private: | |
271 | const string d_name; | |
272 | }; | |
273 | ||
274 | class BackendMakerClass | |
275 | { | |
276 | public: | |
277 | void report(BackendFactory *bf); | |
278 | void launch(const string &instr); | |
e0d84497 | 279 | vector<DNSBackend *>all(bool skipBIND=false); |
12c86877 BH |
280 | void load(const string &module); |
281 | int numLauncheable(); | |
282 | vector<string> getModules(); | |
283 | ||
284 | private: | |
285 | void load_all(); | |
286 | typedef map<string,BackendFactory *>d_repository_t; | |
287 | d_repository_t d_repository; | |
288 | vector<pair<string,string> >d_instances; | |
289 | }; | |
290 | ||
291 | extern BackendMakerClass &BackendMakers(); | |
292 | ||
bdc9f8d2 | 293 | //! Exception that can be thrown by a DNSBackend to indicate a failure |
cc3afe25 BH |
294 | class DBException : public AhuException |
295 | { | |
296 | public: | |
297 | DBException(const string &reason) : AhuException(reason){} | |
298 | }; | |
299 | ||
12c86877 BH |
300 | |
301 | #endif |