]>
Commit | Line | Data |
---|---|---|
12c86877 | 1 | /* |
6edbf68a PL |
2 | * This file is part of PowerDNS or dnsdist. |
3 | * Copyright -- PowerDNS.COM B.V. and its contributors | |
4 | * | |
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. | |
8 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
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. | |
21 | */ | |
870a0fe4 AT |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
cb433f9c BH |
25 | #include <boost/archive/binary_iarchive.hpp> |
26 | #include <boost/archive/binary_oarchive.hpp> | |
27 | ||
bf269e28 | 28 | #include "auth-querycache.hh" |
12c86877 | 29 | #include "utility.hh" |
c6566265 | 30 | |
c6566265 | 31 | |
96349507 | 32 | #include <dlfcn.h> |
12c86877 BH |
33 | #include <string> |
34 | #include <map> | |
35 | #include <sys/types.h> | |
cb433f9c | 36 | #include <sstream> |
12c86877 BH |
37 | #include <errno.h> |
38 | #include <iostream> | |
39 | #include <sstream> | |
40 | #include <functional> | |
fa8fd4d2 | 41 | |
12c86877 BH |
42 | #include "dns.hh" |
43 | #include "arguments.hh" | |
44 | #include "dnsbackend.hh" | |
45 | #include "ueberbackend.hh" | |
46 | #include "dnspacket.hh" | |
47 | #include "logger.hh" | |
48 | #include "statbag.hh" | |
12c86877 BH |
49 | |
50 | extern StatBag S; | |
51 | ||
52 | vector<UeberBackend *>UeberBackend::instances; | |
53 | pthread_mutex_t UeberBackend::instances_lock=PTHREAD_MUTEX_INITIALIZER; | |
54 | ||
12c86877 BH |
55 | // initially we are blocked |
56 | bool UeberBackend::d_go=false; | |
57 | pthread_mutex_t UeberBackend::d_mut = PTHREAD_MUTEX_INITIALIZER; | |
58 | pthread_cond_t UeberBackend::d_cond = PTHREAD_COND_INITIALIZER; | |
59 | ||
12c86877 BH |
60 | //! Loads a module and reports it to all UeberBackend threads |
61 | bool UeberBackend::loadmodule(const string &name) | |
62 | { | |
74caf870 KM |
63 | L<<Logger::Warning <<"Loading '"<<name<<"'" << endl; |
64 | ||
12c86877 | 65 | void *dlib=dlopen(name.c_str(), RTLD_NOW); |
74caf870 | 66 | |
12c86877 | 67 | if(dlib == NULL) { |
74caf870 | 68 | L<<Logger::Error <<"Unable to load module '"<<name<<"': "<<dlerror() << endl; |
12c86877 BH |
69 | return false; |
70 | } | |
74caf870 | 71 | |
12c86877 | 72 | return true; |
12c86877 BH |
73 | } |
74 | ||
75 | void UeberBackend::go(void) | |
76 | { | |
77 | pthread_mutex_lock(&d_mut); | |
78 | d_go=true; | |
79 | pthread_cond_broadcast(&d_cond); | |
80 | pthread_mutex_unlock(&d_mut); | |
81 | } | |
82 | ||
675fa24c | 83 | bool UeberBackend::getDomainInfo(const DNSName &domain, DomainInfo &di) |
12c86877 BH |
84 | { |
85 | for(vector<DNSBackend *>::const_iterator i=backends.begin();i!=backends.end();++i) | |
86 | if((*i)->getDomainInfo(domain, di)) | |
87 | return true; | |
88 | return false; | |
89 | } | |
90 | ||
675fa24c | 91 | bool UeberBackend::createDomain(const DNSName &domain) |
487cf033 | 92 | { |
ef7cd021 | 93 | for(DNSBackend* mydb : backends) { |
487cf033 | 94 | if(mydb->createDomain(domain)) { |
487cf033 CH |
95 | return true; |
96 | } | |
97 | } | |
98 | return false; | |
99 | } | |
100 | ||
7fa35c07 KM |
101 | bool UeberBackend::doesDNSSEC() |
102 | { | |
103 | for(auto* db : backends) { | |
104 | if(db->doesDNSSEC()) | |
105 | return true; | |
106 | } | |
107 | return false; | |
108 | } | |
109 | ||
82cc0761 | 110 | bool UeberBackend::addDomainKey(const DNSName& name, const DNSBackend::KeyData& key, int64_t& id) |
c0273500 | 111 | { |
82cc0761 | 112 | id = -1; |
ef7cd021 | 113 | for(DNSBackend* db : backends) { |
82cc0761 BZ |
114 | if(db->addDomainKey(name, key, id)) |
115 | return true; | |
c0273500 | 116 | } |
82cc0761 | 117 | return false; |
c0273500 | 118 | } |
9c1c5d49 | 119 | bool UeberBackend::getDomainKeys(const DNSName& name, std::vector<DNSBackend::KeyData>& keys) |
c0273500 | 120 | { |
ef7cd021 | 121 | for(DNSBackend* db : backends) { |
9c1c5d49 | 122 | if(db->getDomainKeys(name, keys)) |
c0273500 BH |
123 | return true; |
124 | } | |
125 | return false; | |
126 | } | |
127 | ||
675fa24c | 128 | bool UeberBackend::getAllDomainMetadata(const DNSName& name, std::map<std::string, std::vector<std::string> >& meta) |
ac993e0a | 129 | { |
ef7cd021 | 130 | for(DNSBackend* db : backends) { |
ac993e0a AT |
131 | if(db->getAllDomainMetadata(name, meta)) |
132 | return true; | |
133 | } | |
134 | return false; | |
135 | } | |
136 | ||
675fa24c | 137 | bool UeberBackend::getDomainMetadata(const DNSName& name, const std::string& kind, std::vector<std::string>& meta) |
c0273500 | 138 | { |
ef7cd021 | 139 | for(DNSBackend* db : backends) { |
c0273500 BH |
140 | if(db->getDomainMetadata(name, kind, meta)) |
141 | return true; | |
142 | } | |
143 | return false; | |
144 | } | |
145 | ||
675fa24c | 146 | bool UeberBackend::setDomainMetadata(const DNSName& name, const std::string& kind, const std::vector<std::string>& meta) |
c0273500 | 147 | { |
ef7cd021 | 148 | for(DNSBackend* db : backends) { |
c0273500 BH |
149 | if(db->setDomainMetadata(name, kind, meta)) |
150 | return true; | |
151 | } | |
152 | return false; | |
153 | } | |
154 | ||
675fa24c | 155 | bool UeberBackend::activateDomainKey(const DNSName& name, unsigned int id) |
4496f66f | 156 | { |
ef7cd021 | 157 | for(DNSBackend* db : backends) { |
4496f66f BH |
158 | if(db->activateDomainKey(name, id)) |
159 | return true; | |
160 | } | |
161 | return false; | |
162 | } | |
163 | ||
675fa24c | 164 | bool UeberBackend::deactivateDomainKey(const DNSName& name, unsigned int id) |
4496f66f | 165 | { |
ef7cd021 | 166 | for(DNSBackend* db : backends) { |
4496f66f BH |
167 | if(db->deactivateDomainKey(name, id)) |
168 | return true; | |
169 | } | |
170 | return false; | |
171 | } | |
172 | ||
675fa24c | 173 | bool UeberBackend::removeDomainKey(const DNSName& name, unsigned int id) |
4496f66f | 174 | { |
ef7cd021 | 175 | for(DNSBackend* db : backends) { |
4496f66f BH |
176 | if(db->removeDomainKey(name, id)) |
177 | return true; | |
178 | } | |
179 | return false; | |
180 | } | |
181 | ||
c0273500 | 182 | |
675fa24c | 183 | bool UeberBackend::getTSIGKey(const DNSName& name, DNSName* algorithm, string* content) |
78bcb858 | 184 | { |
ef7cd021 | 185 | for(DNSBackend* db : backends) { |
78bcb858 BH |
186 | if(db->getTSIGKey(name, algorithm, content)) |
187 | return true; | |
188 | } | |
189 | return false; | |
190 | } | |
191 | ||
192 | ||
675fa24c | 193 | bool UeberBackend::setTSIGKey(const DNSName& name, const DNSName& algorithm, const string& content) |
6f872b78 | 194 | { |
ef7cd021 | 195 | for(DNSBackend* db : backends) { |
6f872b78 AT |
196 | if(db->setTSIGKey(name, algorithm, content)) |
197 | return true; | |
198 | } | |
199 | return false; | |
200 | } | |
201 | ||
675fa24c | 202 | bool UeberBackend::deleteTSIGKey(const DNSName& name) |
6f872b78 | 203 | { |
ef7cd021 | 204 | for(DNSBackend* db : backends) { |
6f872b78 AT |
205 | if(db->deleteTSIGKey(name)) |
206 | return true; | |
207 | } | |
208 | return false; | |
209 | } | |
210 | ||
211 | bool UeberBackend::getTSIGKeys(std::vector< struct TSIGKey > &keys) | |
212 | { | |
ef7cd021 | 213 | for(DNSBackend* db : backends) { |
6f872b78 AT |
214 | db->getTSIGKeys(keys); |
215 | } | |
216 | return true; | |
217 | } | |
218 | ||
12c86877 BH |
219 | void UeberBackend::reload() |
220 | { | |
221 | for ( vector< DNSBackend * >::iterator i = backends.begin(); i != backends.end(); ++i ) | |
222 | { | |
223 | ( *i )->reload(); | |
224 | } | |
225 | } | |
226 | ||
973ad2b5 | 227 | void UeberBackend::rediscover(string *status) |
12c86877 | 228 | { |
20ca8e7d | 229 | |
12c86877 BH |
230 | for ( vector< DNSBackend * >::iterator i = backends.begin(); i != backends.end(); ++i ) |
231 | { | |
973ad2b5 BH |
232 | string tmpstr; |
233 | ( *i )->rediscover(&tmpstr); | |
6242d8a4 KM |
234 | if(status) |
235 | *status+=tmpstr + (i!=backends.begin() ? "\n" : ""); | |
12c86877 BH |
236 | } |
237 | } | |
238 | ||
239 | ||
240 | void UeberBackend::getUnfreshSlaveInfos(vector<DomainInfo>* domains) | |
241 | { | |
242 | for ( vector< DNSBackend * >::iterator i = backends.begin(); i != backends.end(); ++i ) | |
243 | { | |
244 | ( *i )->getUnfreshSlaveInfos( domains ); | |
245 | } | |
246 | } | |
247 | ||
248 | ||
249 | ||
250 | void UeberBackend::getUpdatedMasters(vector<DomainInfo>* domains) | |
251 | { | |
252 | for ( vector< DNSBackend * >::iterator i = backends.begin(); i != backends.end(); ++i ) | |
253 | { | |
254 | ( *i )->getUpdatedMasters( domains ); | |
255 | } | |
256 | } | |
257 | ||
cec52de6 | 258 | bool UeberBackend::getAuth(const DNSName &target, const QType& qtype, SOAData* sd, bool cachedOk) |
c14bc34a | 259 | { |
5b9ac871 KM |
260 | bool found = false; |
261 | int cstat; | |
9105d7b3 | 262 | DNSName choppedOff(target); |
5b9ac871 KM |
263 | vector<pair<size_t, SOAData> > bestmatch (backends.size(), make_pair(target.wirelength()+1, SOAData())); |
264 | do { | |
265 | ||
266 | // Check cache | |
cec52de6 | 267 | if(cachedOk && (d_cache_ttl || d_negcache_ttl)) { |
5b9ac871 | 268 | d_question.qtype = QType::SOA; |
9105d7b3 | 269 | d_question.qname = choppedOff; |
5b9ac871 KM |
270 | d_question.zoneId = -1; |
271 | ||
272 | cstat = cacheHas(d_question,d_answers); | |
273 | ||
274 | if(cstat == 1 && !d_answers.empty() && d_cache_ttl) { | |
c348fec2 | 275 | DLOG(L<<Logger::Error<<"has pos cache entry: "<<choppedOff<<endl); |
90ba52e0 | 276 | fillSOAData(d_answers[0], *sd); |
277 | ||
5b9ac871 | 278 | sd->db = 0; |
9105d7b3 | 279 | sd->qname = choppedOff; |
5b9ac871 KM |
280 | goto found; |
281 | } else if(cstat == 0 && d_negcache_ttl) { | |
c348fec2 | 282 | DLOG(L<<Logger::Error<<"has neg cache entry: "<<choppedOff<<endl); |
5b9ac871 | 283 | continue; |
c14bc34a | 284 | } |
c14bc34a | 285 | } |
f70be3c3 | 286 | |
5b9ac871 | 287 | // Check backends |
9105d7b3 RG |
288 | // A backend can respond to our SOA request with the 'best' |
289 | // match it has. For example, when asked the SOA for a.b.c.powerdns.com. | |
290 | // it might respond with the SOA for powerdns.com. | |
291 | // We then store that, keep querying the other backends in case | |
292 | // one of them has a more specific SOA but don't bother | |
293 | // asking this specific backend again for b.c.powerdns.com. or c.powerdns.com. | |
5b9ac871 KM |
294 | { |
295 | vector<DNSBackend *>::const_iterator i = backends.begin(); | |
296 | vector<pair<size_t, SOAData> >::iterator j = bestmatch.begin(); | |
297 | for(; i != backends.end() && j != bestmatch.end(); ++i, ++j) { | |
298 | ||
9105d7b3 | 299 | DLOG(L<<Logger::Error<<"backend: "<<i-backends.begin()<<", qname: "<<choppedOff<<endl); |
5b9ac871 | 300 | |
9105d7b3 | 301 | if(j->first < choppedOff.wirelength()) { |
c348fec2 | 302 | DLOG(L<<Logger::Error<<"skip this backend, we already know the 'higher' match: "<<j->second.qname<<endl); |
5b9ac871 | 303 | continue; |
9105d7b3 | 304 | } else if(j->first == choppedOff.wirelength()) { |
c348fec2 | 305 | DLOG(L<<Logger::Error<<"use 'higher' match: "<<j->second.qname<<endl); |
5b9ac871 KM |
306 | *sd = j->second; |
307 | break; | |
308 | } else { | |
9105d7b3 | 309 | DLOG(L<<Logger::Error<<"lookup: "<<choppedOff<<endl); |
cec52de6 | 310 | if((*i)->getAuth(choppedOff, sd)) { |
5b9ac871 KM |
311 | DLOG(L<<Logger::Error<<"got: "<<sd->qname<<endl); |
312 | j->first = sd->qname.wirelength(); | |
bee25428 | 313 | j->second = *sd; |
9105d7b3 | 314 | if(sd->qname == choppedOff) { |
5b9ac871 KM |
315 | break; |
316 | } | |
317 | } else { | |
c348fec2 | 318 | DLOG(L<<Logger::Error<<"no match for: "<<choppedOff<<endl); |
5b9ac871 KM |
319 | } |
320 | } | |
f70be3c3 | 321 | } |
c14bc34a | 322 | |
5b9ac871 KM |
323 | // Add to cache |
324 | if(i == backends.end()) { | |
325 | if(d_negcache_ttl) { | |
c348fec2 | 326 | DLOG(L<<Logger::Error<<"add neg cache entry:"<<choppedOff<<endl); |
9105d7b3 | 327 | d_question.qname=choppedOff; |
5b9ac871 KM |
328 | addNegCache(d_question); |
329 | } | |
330 | continue; | |
331 | } else if(d_cache_ttl) { | |
c348fec2 | 332 | DLOG(L<<Logger::Error<<"add pos cache entry: "<<sd->qname<<endl); |
c14bc34a MZ |
333 | d_question.qtype = QType::SOA; |
334 | d_question.qname = sd->qname; | |
335 | d_question.zoneId = -1; | |
336 | ||
90ba52e0 | 337 | DNSZoneRecord rr; |
338 | rr.dr.d_name = sd->qname; | |
339 | rr.dr.d_type = QType::SOA; | |
340 | ||
341 | rr.dr.d_content = makeSOAContent(*sd); | |
342 | rr.dr.d_ttl = sd->ttl; | |
c14bc34a | 343 | rr.domain_id = sd->domain_id; |
90ba52e0 | 344 | |
345 | addCache(d_question, {rr}); | |
5b9ac871 | 346 | } |
c14bc34a MZ |
347 | } |
348 | ||
5b9ac871 | 349 | found: |
cec52de6 | 350 | if(found == (qtype == QType::DS)){ |
c348fec2 | 351 | DLOG(L<<Logger::Error<<"found: "<<sd->qname<<endl); |
5b9ac871 KM |
352 | return true; |
353 | } else { | |
c348fec2 | 354 | DLOG(L<<Logger::Error<<"chasing next: "<<sd->qname<<endl); |
5b9ac871 KM |
355 | found = true; |
356 | } | |
357 | ||
9105d7b3 | 358 | } while(choppedOff.chopOff()); |
5b9ac871 | 359 | return found; |
c14bc34a MZ |
360 | } |
361 | ||
94bfa5b6 | 362 | bool UeberBackend::getSOA(const DNSName &domain, SOAData &sd) |
12c86877 BH |
363 | { |
364 | d_question.qtype=QType::SOA; | |
365 | d_question.qname=domain; | |
366 | d_question.zoneId=-1; | |
367 | ||
79ba7763 CH |
368 | int cstat=cacheHas(d_question,d_answers); |
369 | if(cstat==0) { // negative | |
370 | return false; | |
12c86877 | 371 | } |
79ba7763 | 372 | else if(cstat==1 && !d_answers.empty()) { |
90ba52e0 | 373 | fillSOAData(d_answers[0],sd); |
79ba7763 | 374 | sd.domain_id=d_answers[0].domain_id; |
90ba52e0 | 375 | sd.ttl=d_answers[0].dr.d_ttl; |
79ba7763 CH |
376 | sd.db=0; |
377 | return true; | |
378 | } | |
379 | ||
380 | // not found in neg. or pos. cache, look it up | |
94bfa5b6 | 381 | return getSOAUncached(domain, sd); |
79ba7763 CH |
382 | } |
383 | ||
94bfa5b6 | 384 | bool UeberBackend::getSOAUncached(const DNSName &domain, SOAData &sd) |
79ba7763 CH |
385 | { |
386 | d_question.qtype=QType::SOA; | |
387 | d_question.qname=domain; | |
388 | d_question.zoneId=-1; | |
389 | ||
12c86877 | 390 | for(vector<DNSBackend *>::const_iterator i=backends.begin();i!=backends.end();++i) |
94bfa5b6 | 391 | if((*i)->getSOA(domain, sd)) { |
f1d64762 | 392 | if( d_cache_ttl ) { |
90ba52e0 | 393 | DNSZoneRecord rr; |
394 | rr.dr.d_name = sd.qname; | |
395 | rr.dr.d_type = QType::SOA; | |
396 | ||
397 | rr.dr.d_content = makeSOAContent(sd); | |
398 | rr.dr.d_ttl = sd.ttl; | |
399 | rr.domain_id = sd.domain_id; | |
400 | ||
401 | addCache(d_question, {rr}); | |
402 | ||
f1d64762 | 403 | } |
12c86877 BH |
404 | return true; |
405 | } | |
406 | ||
b87f301b KM |
407 | if(d_negcache_ttl) |
408 | addNegCache(d_question); | |
12c86877 BH |
409 | return false; |
410 | } | |
411 | ||
675fa24c | 412 | bool UeberBackend::superMasterBackend(const string &ip, const DNSName &domain, const vector<DNSResourceRecord>&nsset, string *nameserver, string *account, DNSBackend **db) |
12c86877 BH |
413 | { |
414 | for(vector<DNSBackend *>::const_iterator i=backends.begin();i!=backends.end();++i) | |
719f9024 | 415 | if((*i)->superMasterBackend(ip, domain, nsset, nameserver, account, db)) |
12c86877 BH |
416 | return true; |
417 | return false; | |
12c86877 BH |
418 | } |
419 | ||
12c86877 BH |
420 | UeberBackend::UeberBackend(const string &pname) |
421 | { | |
12c86877 BH |
422 | pthread_mutex_lock(&instances_lock); |
423 | instances.push_back(this); // report to the static list of ourself | |
424 | pthread_mutex_unlock(&instances_lock); | |
425 | ||
51d01c80 AT |
426 | d_negcached=0; |
427 | d_ancount=0; | |
ce4a60ce | 428 | d_domain_id=-1; |
51d01c80 | 429 | d_cached=0; |
f1d64762 MZ |
430 | d_cache_ttl = ::arg().asNum("query-cache-ttl"); |
431 | d_negcache_ttl = ::arg().asNum("negquery-cache-ttl"); | |
432 | ||
ce4a60ce CH |
433 | d_tid=pthread_self(); |
434 | d_stale=false; | |
12c86877 | 435 | |
e0d84497 | 436 | backends=BackendMakers().all(pname=="key-only"); |
12c86877 BH |
437 | } |
438 | ||
12c86877 BH |
439 | void del(DNSBackend* d) |
440 | { | |
441 | delete d; | |
442 | } | |
443 | ||
444 | void UeberBackend::cleanup() | |
445 | { | |
446 | pthread_mutex_lock(&instances_lock); | |
447 | ||
448 | remove(instances.begin(),instances.end(),this); | |
449 | instances.resize(instances.size()-1); | |
450 | ||
451 | pthread_mutex_unlock(&instances_lock); | |
452 | ||
6242d8a4 | 453 | for_each(backends.begin(),backends.end(),del); |
12c86877 BH |
454 | } |
455 | ||
cb433f9c | 456 | // returns -1 for miss, 0 for negative match, 1 for hit |
90ba52e0 | 457 | int UeberBackend::cacheHas(const Question &q, vector<DNSZoneRecord> &rrs) |
12c86877 | 458 | { |
bf269e28 | 459 | extern AuthQueryCache QC; |
12c86877 | 460 | |
f1d64762 | 461 | if(!d_cache_ttl && ! d_negcache_ttl) { |
12c86877 BH |
462 | return -1; |
463 | } | |
464 | ||
a3b6f8d0 | 465 | rrs.clear(); |
2f24bcd2 | 466 | // L<<Logger::Warning<<"looking up: '"<<q.qname+"'|N|"+q.qtype.getName()+"|"+itoa(q.zoneId)<<endl; |
cf71f03f | 467 | |
bf269e28 | 468 | bool ret=QC.getEntry(q.qname, q.qtype, rrs, q.zoneId); // think about lowercasing here |
12c86877 | 469 | if(!ret) { |
12c86877 BH |
470 | return -1; |
471 | } | |
a3b6f8d0 | 472 | if(rrs.empty()) // negatively cached |
12c86877 | 473 | return 0; |
cb433f9c | 474 | |
12c86877 BH |
475 | return 1; |
476 | } | |
477 | ||
478 | void UeberBackend::addNegCache(const Question &q) | |
479 | { | |
bf269e28 | 480 | extern AuthQueryCache QC; |
f1d64762 | 481 | if(!d_negcache_ttl) |
12c86877 | 482 | return; |
ee5e1751 | 483 | // we should also not be storing negative answers if a pipebackend does scopeMask, but we can't pass a negative scopeMask in an empty set! |
bf269e28 | 484 | QC.insert(q.qname, q.qtype, vector<DNSZoneRecord>(), d_negcache_ttl, q.zoneId); |
12c86877 BH |
485 | } |
486 | ||
90ba52e0 | 487 | void UeberBackend::addCache(const Question &q, const vector<DNSZoneRecord> &rrs) |
12c86877 | 488 | { |
bf269e28 | 489 | extern AuthQueryCache QC; |
46c6efbe | 490 | |
f1d64762 | 491 | if(!d_cache_ttl) |
12c86877 | 492 | return; |
f1d64762 MZ |
493 | |
494 | unsigned int store_ttl = d_cache_ttl; | |
90ba52e0 | 495 | for(const auto& rr : rrs) { |
496 | if (rr.dr.d_ttl < d_cache_ttl) | |
497 | store_ttl = rr.dr.d_ttl; | |
429ae4cb | 498 | if (rr.scopeMask) |
499 | return; | |
500 | } | |
501 | ||
bf269e28 | 502 | QC.insert(q.qname, q.qtype, rrs, store_ttl, q.zoneId); |
12c86877 BH |
503 | } |
504 | ||
675fa24c | 505 | void UeberBackend::alsoNotifies(const DNSName &domain, set<string> *ips) |
27d94a79 BH |
506 | { |
507 | for ( vector< DNSBackend * >::iterator i = backends.begin(); i != backends.end(); ++i ) | |
508 | (*i)->alsoNotifies(domain,ips); | |
509 | } | |
12c86877 BH |
510 | |
511 | UeberBackend::~UeberBackend() | |
512 | { | |
513 | DLOG(L<<Logger::Error<<"UeberBackend destructor called, removing ourselves from instances, and deleting our backends"<<endl); | |
514 | cleanup(); | |
515 | } | |
516 | ||
517 | // this handle is more magic than most | |
675fa24c | 518 | void UeberBackend::lookup(const QType &qtype,const DNSName &qname, DNSPacket *pkt_p, int zoneId) |
12c86877 | 519 | { |
ce4a60ce | 520 | if(d_stale) { |
12c86877 | 521 | L<<Logger::Error<<"Stale ueberbackend received question, signalling that we want to be recycled"<<endl; |
3f81d239 | 522 | throw PDNSException("We are stale, please recycle"); |
12c86877 BH |
523 | } |
524 | ||
f43c4448 | 525 | DLOG(L<<"UeberBackend received question for "<<qtype.getName()<<" of "<<qname<<endl); |
12c86877 BH |
526 | if(!d_go) { |
527 | pthread_mutex_lock(&d_mut); | |
528 | while (d_go==false) { | |
529 | L<<Logger::Error<<"UeberBackend is blocked, waiting for 'go'"<<endl; | |
530 | pthread_cond_wait(&d_cond, &d_mut); | |
531 | L<<Logger::Error<<"Broadcast received, unblocked"<<endl; | |
532 | } | |
533 | pthread_mutex_unlock(&d_mut); | |
534 | } | |
535 | ||
ce4a60ce | 536 | d_domain_id=zoneId; |
c27c8d96 | 537 | |
12c86877 BH |
538 | d_handle.i=0; |
539 | d_handle.qtype=qtype; | |
540 | d_handle.qname=qname; | |
541 | d_handle.pkt_p=pkt_p; | |
542 | d_ancount=0; | |
543 | ||
544 | if(!backends.size()) { | |
b34510a3 | 545 | L<<Logger::Error<<"No database backends available - unable to answer questions."<<endl; |
ce4a60ce | 546 | d_stale=true; // please recycle us! |
3f81d239 | 547 | throw PDNSException("We are stale, please recycle"); |
12c86877 BH |
548 | } |
549 | else { | |
6242d8a4 KM |
550 | d_question.qtype=qtype; |
551 | d_question.qname=qname; | |
552 | d_question.zoneId=zoneId; | |
553 | int cstat=cacheHas(d_question, d_answers); | |
554 | if(cstat<0) { // nothing | |
aa7b2405 | 555 | // cout<<"UeberBackend::lookup("<<qname<<"|"<<DNSRecordContent::NumberToType(qtype.getCode())<<"): uncached"<<endl; |
6242d8a4 KM |
556 | d_negcached=d_cached=false; |
557 | d_answers.clear(); | |
558 | (d_handle.d_hinterBackend=backends[d_handle.i++])->lookup(qtype, qname,pkt_p,zoneId); | |
559 | } | |
560 | else if(cstat==0) { | |
aa7b2405 | 561 | // cout<<"UeberBackend::lookup("<<qname<<"|"<<DNSRecordContent::NumberToType(qtype.getCode())<<"): NEGcached"<<endl; |
6242d8a4 KM |
562 | d_negcached=true; |
563 | d_cached=false; | |
564 | d_answers.clear(); | |
565 | } | |
566 | else { | |
aa7b2405 | 567 | // cout<<"UeberBackend::lookup("<<qname<<"|"<<DNSRecordContent::NumberToType(qtype.getCode())<<"): CACHED"<<endl; |
6242d8a4 KM |
568 | d_negcached=false; |
569 | d_cached=true; | |
570 | d_cachehandleiter = d_answers.begin(); | |
571 | } | |
12c86877 BH |
572 | } |
573 | ||
574 | d_handle.parent=this; | |
12c86877 BH |
575 | } |
576 | ||
cea26350 | 577 | void UeberBackend::getAllDomains(vector<DomainInfo> *domains, bool include_disabled) { |
1325e8a2 PD |
578 | for (vector<DNSBackend*>::iterator i = backends.begin(); i != backends.end(); ++i ) |
579 | { | |
cea26350 | 580 | (*i)->getAllDomains(domains, include_disabled); |
1325e8a2 PD |
581 | } |
582 | } | |
583 | ||
90ba52e0 | 584 | bool UeberBackend::get(DNSZoneRecord &rr) |
12c86877 | 585 | { |
aa7b2405 | 586 | // cout<<"UeberBackend::get(DNSZoneRecord) called"<<endl; |
12c86877 BH |
587 | if(d_negcached) { |
588 | return false; | |
589 | } | |
590 | ||
591 | if(d_cached) { | |
cb433f9c BH |
592 | if(d_cachehandleiter != d_answers.end()) { |
593 | rr=*d_cachehandleiter++;; | |
594 | return true; | |
595 | } | |
596 | return false; | |
12c86877 | 597 | } |
12c86877 | 598 | if(!d_handle.get(rr)) { |
aa7b2405 | 599 | // cout<<"end of ueberbackend get, seeing if we should cache"<<endl; |
600 | if(!d_ancount && d_handle.qname.countLabels()) {// don't cache axfr | |
16925355 | 601 | // cout<<"adding negcache"<<endl; |
12c86877 | 602 | addNegCache(d_question); |
aa7b2405 | 603 | } |
604 | else { | |
605 | // cout<<"adding query cache"<<endl; | |
606 | addCache(d_question, d_answers); | |
607 | } | |
cb433f9c | 608 | d_answers.clear(); |
12c86877 BH |
609 | return false; |
610 | } | |
cb433f9c BH |
611 | d_ancount++; |
612 | d_answers.push_back(rr); | |
12c86877 BH |
613 | return true; |
614 | } | |
615 | ||
9f8e226e AT |
616 | bool UeberBackend::searchRecords(const string& pattern, int maxResults, vector<DNSResourceRecord>& result) |
617 | { | |
618 | bool rc = false; | |
619 | for ( vector< DNSBackend * >::iterator i = backends.begin(); result.size() < static_cast<vector<DNSResourceRecord>::size_type>(maxResults) && i != backends.end(); ++i ) | |
620 | if ((*i)->searchRecords(pattern, maxResults - result.size(), result)) rc = true; | |
621 | return rc; | |
622 | } | |
623 | ||
624 | bool UeberBackend::searchComments(const string& pattern, int maxResults, vector<Comment>& result) | |
625 | { | |
626 | bool rc = false; | |
627 | for ( vector< DNSBackend * >::iterator i = backends.begin(); result.size() < static_cast<vector<Comment>::size_type>(maxResults) && i != backends.end(); ++i ) | |
628 | if ((*i)->searchComments(pattern, maxResults - result.size(), result)) rc = true; | |
629 | return rc; | |
630 | } | |
12c86877 | 631 | |
16f7d28d | 632 | AtomicCounter UeberBackend::handle::instances(0); |
12c86877 BH |
633 | |
634 | UeberBackend::handle::handle() | |
635 | { | |
636 | // L<<Logger::Warning<<"Handle instances: "<<instances<<endl; | |
16f7d28d | 637 | ++instances; |
457d7c72 AT |
638 | parent=NULL; |
639 | d_hinterBackend=NULL; | |
640 | pkt_p=NULL; | |
641 | i=0; | |
12c86877 BH |
642 | } |
643 | ||
644 | UeberBackend::handle::~handle() | |
645 | { | |
16f7d28d | 646 | --instances; |
12c86877 BH |
647 | } |
648 | ||
90ba52e0 | 649 | bool UeberBackend::handle::get(DNSZoneRecord &r) |
12c86877 BH |
650 | { |
651 | DLOG(L << "Ueber get() was called for a "<<qtype.getName()<<" record" << endl); | |
652 | bool isMore=false; | |
653 | while(d_hinterBackend && !(isMore=d_hinterBackend->get(r))) { // this backend out of answers | |
654 | if(i<parent->backends.size()) { | |
655 | DLOG(L<<"Backend #"<<i<<" of "<<parent->backends.size() | |
4957a608 | 656 | <<" out of answers, taking next"<<endl); |
12c86877 BH |
657 | |
658 | d_hinterBackend=parent->backends[i++]; | |
ce4a60ce | 659 | d_hinterBackend->lookup(qtype,qname,pkt_p,parent->d_domain_id); |
12c86877 BH |
660 | } |
661 | else | |
662 | break; | |
663 | ||
664 | DLOG(L<<"Now asking backend #"<<i<<endl); | |
665 | } | |
666 | ||
667 | if(!isMore && i==parent->backends.size()) { | |
668 | DLOG(L<<"UeberBackend reached end of backends"<<endl); | |
669 | return false; | |
670 | } | |
671 | ||
672 | DLOG(L<<"Found an answering backend - will not try another one"<<endl); | |
673 | i=parent->backends.size(); // don't go on to the next backend | |
674 | return true; | |
675 | } |