]>
Commit | Line | Data |
---|---|---|
3696224d | 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 | |
38fdb531 | 25 | |
3696224d | 26 | #include "utility.hh" |
d7652f3a BH |
27 | #include "dnssecinfra.hh" |
28 | #include "dnsseckeeper.hh" | |
29 | #include "base32.hh" | |
3696224d BH |
30 | #include <errno.h> |
31 | #include "communicator.hh" | |
32 | #include <set> | |
33 | #include <boost/utility.hpp> | |
34 | #include "dnsbackend.hh" | |
35 | #include "ueberbackend.hh" | |
36 | #include "packethandler.hh" | |
37 | #include "resolver.hh" | |
38 | #include "logger.hh" | |
39 | #include "dns.hh" | |
40 | #include "arguments.hh" | |
38fdb531 | 41 | #include "auth-caches.hh" |
fa8fd4d2 | 42 | |
29b92d6f | 43 | #include "base64.hh" |
3696224d | 44 | #include "inflighter.cc" |
3696224d | 45 | #include "namespaces.hh" |
7108e055 | 46 | #include "common_startup.hh" |
3e7dcee6 | 47 | |
cd189f24 | 48 | #include "ixfr.hh" |
e23622a7 | 49 | using boost::scoped_ptr; |
3696224d | 50 | |
fab71044 | 51 | |
d622042f | 52 | void CommunicatorClass::addSuckRequest(const DNSName &domain, const ComboAddress& master) |
3696224d BH |
53 | { |
54 | Lock l(&d_lock); | |
3696224d BH |
55 | SuckRequest sr; |
56 | sr.domain = domain; | |
57 | sr.master = master; | |
dbcb3066 | 58 | pair<UniQueue::iterator, bool> res; |
a71bee29 PD |
59 | |
60 | res=d_suckdomains.push_back(sr); | |
dbcb3066 | 61 | if(res.second) { |
7f3d870e | 62 | d_suck_sem.post(); |
dbcb3066 | 63 | } |
3e7dcee6 | 64 | |
3696224d BH |
65 | } |
66 | ||
d3ee36f2 | 67 | struct ZoneStatus |
68 | { | |
69 | bool isDnssecZone{false}; | |
70 | bool isPresigned{false}; | |
71 | bool isNSEC3 {false}; | |
72 | bool optOutFlag {false}; | |
73 | NSEC3PARAMRecordContent ns3pr; | |
74 | ||
75 | bool isNarrow{false}; | |
76 | unsigned int soa_serial{0}; | |
77 | set<DNSName> nsset, qnames, secured; | |
78 | uint32_t domain_id; | |
3e7dcee6 | 79 | int numDeltas{0}; |
d3ee36f2 | 80 | }; |
81 | ||
82 | ||
18b433ac | 83 | void CommunicatorClass::ixfrSuck(const DNSName &domain, const TSIGTriplet& tt, const ComboAddress& laddr, const ComboAddress& remote, scoped_ptr<AuthLua4>& pdl, |
3e7dcee6 | 84 | ZoneStatus& zs, vector<DNSRecord>* axfr) |
3696224d | 85 | { |
cd189f24 | 86 | UeberBackend B; // fresh UeberBackend |
87 | ||
88 | DomainInfo di; | |
89 | di.backend=0; | |
90 | // bool transaction=false; | |
91 | try { | |
92 | DNSSECKeeper dk (&B); // reuse our UeberBackend copy for DNSSECKeeper | |
93 | ||
3729e7f8 | 94 | bool wrongDomainKind = false; |
1fbf6fea | 95 | // this checks three error conditions, and sets wrongDomainKind if we hit the third & had an error |
3729e7f8 | 96 | if(!B.getDomainInfo(domain, di) || !di.backend || (wrongDomainKind = true, di.kind != DomainInfo::Slave)) { // di.backend and B are mostly identical |
97 | if(wrongDomainKind) | |
98 | g_log<<Logger::Error<<"Can't determine backend for domain '"<<domain<<"', not configured as slave"<<endl; | |
99 | else | |
100 | g_log<<Logger::Error<<"Can't determine backend for domain '"<<domain<<"'"<<endl; | |
cd189f24 | 101 | return; |
102 | } | |
cd189f24 | 103 | |
104 | soatimes st; | |
105 | memset(&st, 0, sizeof(st)); | |
d3ee36f2 | 106 | st.serial=di.serial; |
cd189f24 | 107 | |
aaf7799a CH |
108 | DNSRecord drsoa; |
109 | drsoa.d_content = std::make_shared<SOARecordContent>(g_rootdnsname, g_rootdnsname, st); | |
110 | auto deltas = getIXFRDeltas(remote, domain, drsoa, tt, laddr.sin4.sin_family ? &laddr : 0, ((size_t) ::arg().asNum("xfr-max-received-mbytes")) * 1024 * 1024); | |
3e7dcee6 | 111 | zs.numDeltas=deltas.size(); |
112 | // cout<<"Got "<<deltas.size()<<" deltas from serial "<<di.serial<<", applying.."<<endl; | |
cd189f24 | 113 | |
114 | for(const auto& d : deltas) { | |
3e7dcee6 | 115 | const auto& remove = d.first; |
116 | const auto& add = d.second; | |
117 | // cout<<"Delta sizes: "<<remove.size()<<", "<<add.size()<<endl; | |
cd189f24 | 118 | |
3e7dcee6 | 119 | if(remove.empty()) { // we got passed an AXFR! |
120 | *axfr = add; | |
121 | return; | |
122 | } | |
123 | ||
124 | ||
cd189f24 | 125 | // our hammer is 'replaceRRSet(domain_id, qname, qt, vector<DNSResourceRecord>& rrset) |
3e7dcee6 | 126 | // which thinks in terms of RRSETs |
127 | // however, IXFR does not, and removes and adds *records* (bummer) | |
128 | // this means that we must group updates by {qname,qtype}, retrieve the RRSET, apply | |
129 | // the add/remove updates, and replaceRRSet the whole thing. | |
cd189f24 | 130 | |
3e7dcee6 | 131 | |
132 | map<pair<DNSName,uint16_t>, pair<vector<DNSRecord>, vector<DNSRecord> > > grouped; | |
133 | ||
134 | for(const auto& x: remove) | |
135 | grouped[{x.d_name, x.d_type}].first.push_back(x); | |
136 | for(const auto& x: add) | |
137 | grouped[{x.d_name, x.d_type}].second.push_back(x); | |
cd189f24 | 138 | |
139 | di.backend->startTransaction(domain, -1); | |
3e7dcee6 | 140 | for(const auto g : grouped) { |
3e7dcee6 | 141 | vector<DNSRecord> rrset; |
abcd36a1 CH |
142 | { |
143 | DNSZoneRecord zrr; | |
acb61e0a | 144 | B.lookup(QType(g.first.second), g.first.first+domain, di.id); |
abcd36a1 | 145 | while(B.get(zrr)) { |
668624c8 | 146 | zrr.dr.d_name.makeUsRelative(domain); |
abcd36a1 CH |
147 | rrset.push_back(zrr.dr); |
148 | } | |
3e7dcee6 | 149 | } |
150 | // O(N^2)! | |
151 | rrset.erase(remove_if(rrset.begin(), rrset.end(), | |
152 | [&g](const DNSRecord& dr) { | |
153 | return count(g.second.first.cbegin(), | |
154 | g.second.first.cend(), dr); | |
155 | }), rrset.end()); | |
156 | // the DNSRecord== operator compares on name, type, class and lowercase content representation | |
157 | ||
158 | for(const auto& x : g.second.second) { | |
159 | rrset.push_back(x); | |
160 | } | |
cd189f24 | 161 | |
3e7dcee6 | 162 | vector<DNSResourceRecord> replacement; |
4950b140 CH |
163 | for(const auto& dr : rrset) { |
164 | auto rr = DNSResourceRecord::fromWire(dr); | |
165 | rr.qname += domain; | |
166 | rr.domain_id = di.id; | |
167 | if(dr.d_type == QType::SOA) { | |
3e7dcee6 | 168 | // cout<<"New SOA: "<<x.d_content->getZoneRepresentation()<<endl; |
4950b140 | 169 | auto sr = getRR<SOARecordContent>(dr); |
3e7dcee6 | 170 | zs.soa_serial=sr->d_st.serial; |
171 | } | |
172 | ||
4950b140 | 173 | replacement.push_back(rr); |
3e7dcee6 | 174 | } |
d3ee36f2 | 175 | |
3e7dcee6 | 176 | di.backend->replaceRRSet(di.id, g.first.first+domain, QType(g.first.second), replacement); |
177 | } | |
cd189f24 | 178 | di.backend->commitTransaction(); |
179 | } | |
cd189f24 | 180 | } |
181 | catch(std::exception& p) { | |
e6a9dde5 | 182 | g_log<<Logger::Error<<"Got exception during IXFR: "<<p.what()<<endl; |
3e7dcee6 | 183 | throw; |
cd189f24 | 184 | } |
185 | catch(PDNSException& p) { | |
e6a9dde5 | 186 | g_log<<Logger::Error<<"Got exception during IXFR: "<<p.reason<<endl; |
3e7dcee6 | 187 | throw; |
188 | } | |
189 | } | |
cd189f24 | 190 | |
3e7dcee6 | 191 | |
192 | static bool processRecordForZS(const DNSName& domain, bool& firstNSEC3, DNSResourceRecord& rr, ZoneStatus& zs) | |
193 | { | |
194 | switch(rr.qtype.getCode()) { | |
195 | case QType::NSEC3PARAM: | |
196 | zs.ns3pr = NSEC3PARAMRecordContent(rr.content); | |
197 | zs.isDnssecZone = zs.isNSEC3 = true; | |
198 | zs.isNarrow = false; | |
e4fb8488 | 199 | return false; |
3e7dcee6 | 200 | case QType::NSEC3: { |
201 | NSEC3RecordContent ns3rc(rr.content); | |
202 | if (firstNSEC3) { | |
203 | zs.isDnssecZone = zs.isPresigned = true; | |
204 | firstNSEC3 = false; | |
205 | } else if (zs.optOutFlag != (ns3rc.d_flags & 1)) | |
206 | throw PDNSException("Zones with a mixture of Opt-Out NSEC3 RRs and non-Opt-Out NSEC3 RRs are not supported."); | |
207 | zs.optOutFlag = ns3rc.d_flags & 1; | |
27d4a65b | 208 | if (ns3rc.isSet(QType::NS) && !(rr.qname==domain)) { |
92c6c467 | 209 | DNSName hashPart = rr.qname.makeRelative(domain); |
3e7dcee6 | 210 | zs.secured.insert(hashPart); |
211 | } | |
e4fb8488 | 212 | return false; |
3e7dcee6 | 213 | } |
214 | ||
215 | case QType::NSEC: | |
216 | zs.isDnssecZone = zs.isPresigned = true; | |
e4fb8488 | 217 | return false; |
3e7dcee6 | 218 | |
219 | case QType::NS: | |
220 | if(rr.qname!=domain) | |
221 | zs.nsset.insert(rr.qname); | |
bd9602fa | 222 | break; |
3e7dcee6 | 223 | } |
224 | ||
225 | zs.qnames.insert(rr.qname); | |
226 | ||
227 | rr.domain_id=zs.domain_id; | |
228 | return true; | |
cd189f24 | 229 | } |
230 | ||
d3ee36f2 | 231 | /* So this code does a number of things. |
232 | 1) It will AXFR a domain from a master | |
233 | The code can retrieve the current serial number in the database itself. | |
234 | It may attempt an IXFR | |
235 | 2) It will filter the zone through a lua *filter* script | |
236 | 3) The code walks through the zone records do determine DNSSEC status (secured, nsec/nsec3, optout) | |
237 | 4) It inserts the zone into the database | |
238 | With the right 'ordername' fields | |
239 | 5) It updates the Empty Non Terminals | |
240 | */ | |
cd189f24 | 241 | |
18b433ac | 242 | static vector<DNSResourceRecord> doAxfr(const ComboAddress& raddr, const DNSName& domain, const TSIGTriplet& tt, const ComboAddress& laddr, scoped_ptr<AuthLua4>& pdl, ZoneStatus& zs) |
d3ee36f2 | 243 | { |
e3619f57 | 244 | uint16_t axfr_timeout=::arg().asNum("axfr-fetch-timeout"); |
d3ee36f2 | 245 | vector<DNSResourceRecord> rrs; |
e3619f57 | 246 | AXFRRetriever retriever(raddr, domain, tt, (laddr.sin4.sin_family == 0) ? NULL : &laddr, ((size_t) ::arg().asNum("xfr-max-received-mbytes")) * 1024 * 1024, axfr_timeout); |
d3ee36f2 | 247 | Resolver::res_t recs; |
248 | bool first=true; | |
249 | bool firstNSEC3{true}; | |
250 | bool soa_received {false}; | |
e3619f57 | 251 | while(retriever.getChunk(recs, nullptr, axfr_timeout)) { |
d3ee36f2 | 252 | if(first) { |
e6a9dde5 | 253 | g_log<<Logger::Error<<"AXFR started for '"<<domain<<"'"<<endl; |
d3ee36f2 | 254 | first=false; |
255 | } | |
256 | ||
257 | for(Resolver::res_t::iterator i=recs.begin();i!=recs.end();++i) { | |
92c6c467 | 258 | i->qname.makeUsLowerCase(); |
d3ee36f2 | 259 | if(i->qtype.getCode() == QType::OPT || i->qtype.getCode() == QType::TSIG) // ignore EDNS0 & TSIG |
260 | continue; | |
261 | ||
262 | if(!i->qname.isPartOf(domain)) { | |
e6a9dde5 | 263 | g_log<<Logger::Error<<"Remote "<<raddr.toStringWithPort()<<" tried to sneak in out-of-zone data '"<<i->qname<<"'|"<<i->qtype.getName()<<" during AXFR of zone '"<<domain<<"', ignoring"<<endl; |
d3ee36f2 | 264 | continue; |
265 | } | |
266 | ||
267 | vector<DNSResourceRecord> out; | |
268 | if(!pdl || !pdl->axfrfilter(raddr, domain, *i, out)) { | |
3e7dcee6 | 269 | out.push_back(*i); // if axfrfilter didn't do anything, we put our record in 'out' ourselves |
d3ee36f2 | 270 | } |
271 | ||
272 | for(DNSResourceRecord& rr : out) { | |
50b867eb | 273 | if(!rr.qname.isPartOf(domain)) { |
e6a9dde5 | 274 | g_log<<Logger::Error<<"Lua axfrfilter() filter tried to sneak in out-of-zone data '"<<i->qname<<"'|"<<i->qtype.getName()<<" during AXFR of zone '"<<domain<<"', ignoring"<<endl; |
50b867eb KM |
275 | continue; |
276 | } | |
3ce97f4a | 277 | if(!processRecordForZS(domain, firstNSEC3, rr, zs)) |
278 | continue; | |
3e7dcee6 | 279 | if(rr.qtype.getCode() == QType::SOA) { |
d3ee36f2 | 280 | if(soa_received) |
281 | continue; //skip the last SOA | |
282 | SOAData sd; | |
283 | fillSOAData(rr.content,sd); | |
284 | zs.soa_serial = sd.serial; | |
285 | soa_received = true; | |
d3ee36f2 | 286 | } |
287 | ||
d3ee36f2 | 288 | rrs.push_back(rr); |
3e7dcee6 | 289 | |
d3ee36f2 | 290 | } |
291 | } | |
292 | } | |
293 | return rrs; | |
294 | } | |
295 | ||
3e7dcee6 | 296 | |
d622042f | 297 | void CommunicatorClass::suck(const DNSName &domain, const ComboAddress& remote) |
cd189f24 | 298 | { |
3e7dcee6 | 299 | { |
300 | Lock l(&d_lock); | |
301 | if(d_inprogress.count(domain)) { | |
302 | return; | |
303 | } | |
304 | d_inprogress.insert(domain); | |
305 | } | |
306 | RemoveSentinel rs(domain, this); // this removes us from d_inprogress when we go out of scope | |
307 | ||
9b0f144f | 308 | g_log<<Logger::Error<<"Initiating transfer of '"<<domain<<"' from remote '"<<remote<<"'"<<endl; |
295c4a00 | 309 | UeberBackend B; // fresh UeberBackend |
3696224d BH |
310 | |
311 | DomainInfo di; | |
312 | di.backend=0; | |
376ec278 | 313 | bool transaction=false; |
3696224d | 314 | try { |
295c4a00 | 315 | DNSSECKeeper dk (&B); // reuse our UeberBackend copy for DNSSECKeeper |
3729e7f8 | 316 | bool wrongDomainKind = false; |
1fbf6fea | 317 | // this checks three error conditions & sets wrongDomainKind if we hit the third |
3729e7f8 | 318 | if(!B.getDomainInfo(domain, di) || !di.backend || (wrongDomainKind = true, di.kind != DomainInfo::Slave)) { // di.backend and B are mostly identical |
319 | if(wrongDomainKind) | |
320 | g_log<<Logger::Error<<"Can't determine backend for domain '"<<domain<<"', not configured as slave"<<endl; | |
321 | else | |
322 | g_log<<Logger::Error<<"Can't determine backend for domain '"<<domain<<"'"<<endl; | |
3696224d BH |
323 | return; |
324 | } | |
d3ee36f2 | 325 | ZoneStatus zs; |
326 | zs.domain_id=di.id; | |
53568203 | 327 | |
98c9ec39 | 328 | TSIGTriplet tt; |
329 | if(dk.getTSIGForAccess(domain, remote, &tt.name)) { | |
29b92d6f | 330 | string tsigsecret64; |
98c9ec39 | 331 | if(B.getTSIGKey(tt.name, &tt.algo, &tsigsecret64)) { |
332 | if(B64Decode(tsigsecret64, tt.secret)) { | |
e6a9dde5 | 333 | g_log<<Logger::Error<<"Unable to Base-64 decode TSIG key '"<<tt.name<<"' for domain '"<<domain<<"' not found"<<endl; |
98c9ec39 | 334 | return; |
335 | } | |
53568203 | 336 | } else { |
e6a9dde5 | 337 | g_log<<Logger::Error<<"TSIG key '"<<tt.name<<"' for domain '"<<domain<<"' not found"<<endl; |
53568203 | 338 | return; |
a1467662 | 339 | } |
29b92d6f | 340 | } |
53568203 KM |
341 | |
342 | ||
18b433ac | 343 | scoped_ptr<AuthLua4> pdl; |
e23622a7 | 344 | vector<string> scripts; |
86177332 | 345 | string script=::arg()["lua-axfr-script"]; |
295c4a00 | 346 | if(B.getDomainMetadata(domain, "LUA-AXFR-SCRIPT", scripts) && !scripts.empty()) { |
86177332 KM |
347 | if (pdns_iequals(scripts[0], "NONE")) { |
348 | script.clear(); | |
349 | } else { | |
350 | script=scripts[0]; | |
351 | } | |
352 | } | |
353 | if(!script.empty()){ | |
b2aefbf9 | 354 | try { |
7c99293d AT |
355 | pdl.reset(new AuthLua4()); |
356 | pdl->loadFile(script); | |
e6a9dde5 | 357 | g_log<<Logger::Info<<"Loaded Lua script '"<<script<<"' to edit the incoming AXFR of '"<<domain<<"'"<<endl; |
b2aefbf9 BH |
358 | } |
359 | catch(std::exception& e) { | |
e6a9dde5 | 360 | g_log<<Logger::Error<<"Failed to load Lua editing script '"<<script<<"' for incoming AXFR of '"<<domain<<"': "<<e.what()<<endl; |
b2aefbf9 BH |
361 | return; |
362 | } | |
e23622a7 | 363 | } |
53568203 | 364 | |
fc396d56 BH |
365 | vector<string> localaddr; |
366 | ComboAddress laddr; | |
d622042f | 367 | |
295c4a00 | 368 | if(B.getDomainMetadata(domain, "AXFR-SOURCE", localaddr) && !localaddr.empty()) { |
fc396d56 BH |
369 | try { |
370 | laddr = ComboAddress(localaddr[0]); | |
e6a9dde5 | 371 | g_log<<Logger::Info<<"AXFR source for domain '"<<domain<<"' set to "<<localaddr[0]<<endl; |
fc396d56 BH |
372 | } |
373 | catch(std::exception& e) { | |
e6a9dde5 | 374 | g_log<<Logger::Error<<"Failed to load AXFR source '"<<localaddr[0]<<"' for incoming AXFR of '"<<domain<<"': "<<e.what()<<endl; |
fc396d56 BH |
375 | return; |
376 | } | |
9d36589a | 377 | } else { |
d622042f | 378 | if(remote.sin4.sin_family == AF_INET && !::arg()["query-local-address"].empty()) { |
9d36589a | 379 | laddr = ComboAddress(::arg()["query-local-address"]); |
d622042f | 380 | } else if(remote.sin4.sin_family == AF_INET6 && !::arg()["query-local-address6"].empty()) { |
9d36589a PL |
381 | laddr = ComboAddress(::arg()["query-local-address6"]); |
382 | } else { | |
d622042f | 383 | bool isv6 = remote.sin4.sin_family == AF_INET6; |
e6a9dde5 | 384 | g_log<<Logger::Error<<"Unable to AXFR, destination address is IPv" << (isv6 ? "6" : "4") << ", but query-local-address"<< (isv6 ? "6" : "") << " is unset!"<<endl; |
9d36589a PL |
385 | return; |
386 | } | |
8c949c52 | 387 | } |
fc396d56 | 388 | |
53568203 KM |
389 | bool hadDnssecZone = false; |
390 | bool hadPresigned = false; | |
391 | bool hadNSEC3 = false; | |
d3ee36f2 | 392 | NSEC3PARAMRecordContent hadNs3pr; |
393 | bool hadNarrow=false; | |
394 | ||
edb693f0 | 395 | |
3e7dcee6 | 396 | vector<DNSResourceRecord> rrs; |
53568203 KM |
397 | if(dk.isSecuredZone(domain)) { |
398 | hadDnssecZone=true; | |
399 | hadPresigned=dk.isPresigned(domain); | |
d3ee36f2 | 400 | if (dk.getNSEC3PARAM(domain, &zs.ns3pr, &zs.isNarrow)) { |
53568203 | 401 | hadNSEC3 = true; |
d3ee36f2 | 402 | hadNs3pr = zs.ns3pr; |
403 | hadNarrow = zs.isNarrow; | |
53568203 KM |
404 | } |
405 | } | |
d3ee36f2 | 406 | else if(di.serial) { |
407 | vector<string> meta; | |
408 | B.getDomainMetadata(domain, "IXFR", meta); | |
3e7dcee6 | 409 | if(!meta.empty() && meta[0]=="1") { |
410 | vector<DNSRecord> axfr; | |
9b0f144f | 411 | g_log<<Logger::Warning<<"Starting IXFR of '"<<domain<<"' from remote "<<remote<<endl; |
d622042f | 412 | ixfrSuck(domain, tt, laddr, remote, pdl, zs, &axfr); |
3e7dcee6 | 413 | if(!axfr.empty()) { |
9b0f144f | 414 | g_log<<Logger::Warning<<"IXFR of '"<<domain<<"' from remote '"<<remote<<"' turned into an AXFR"<<endl; |
3e7dcee6 | 415 | bool firstNSEC3=true; |
416 | rrs.reserve(axfr.size()); | |
417 | for(const auto& dr : axfr) { | |
4950b140 | 418 | auto rr = DNSResourceRecord::fromWire(dr); |
92c6c467 | 419 | (rr.qname += domain).makeUsLowerCase(); |
3e7dcee6 | 420 | rr.domain_id = zs.domain_id; |
3ce97f4a | 421 | if(!processRecordForZS(domain, firstNSEC3, rr, zs)) |
422 | continue; | |
3e7dcee6 | 423 | if(dr.d_type == QType::SOA) { |
424 | auto sd = getRR<SOARecordContent>(dr); | |
425 | zs.soa_serial = sd->d_st.serial; | |
426 | } | |
427 | rrs.push_back(rr); | |
428 | } | |
429 | } | |
430 | else { | |
e6a9dde5 | 431 | g_log<<Logger::Warning<<"Done with IXFR of '"<<domain<<"' from remote '"<<remote<<"', got "<<zs.numDeltas<<" delta"<<addS(zs.numDeltas)<<", serial now "<<zs.soa_serial<<endl; |
1d0995bc | 432 | purgeAuthCaches(domain.toString()+"$"); |
3e7dcee6 | 433 | return; |
434 | } | |
435 | } | |
3696224d | 436 | } |
68fa524b | 437 | |
3e7dcee6 | 438 | if(rrs.empty()) { |
9b0f144f | 439 | g_log<<Logger::Warning<<"Starting AXFR of '"<<domain<<"' from remote "<<remote<<endl; |
d622042f | 440 | rrs = doAxfr(remote, domain, tt, laddr, pdl, zs); |
9b0f144f | 441 | g_log<<Logger::Warning<<"AXFR of '"<<domain<<"' from remote "<<remote<<" done"<<endl; |
3e7dcee6 | 442 | } |
d3ee36f2 | 443 | |
444 | if(zs.isNSEC3) { | |
445 | zs.ns3pr.d_flags = zs.optOutFlag ? 1 : 0; | |
53568203 | 446 | } |
ff473027 | 447 | |
d3ee36f2 | 448 | if(!zs.isPresigned) { |
53568203 KM |
449 | DNSSECKeeper::keyset_t keys = dk.getKeys(domain); |
450 | if(!keys.empty()) { | |
d3ee36f2 | 451 | zs.isDnssecZone = true; |
452 | zs.isNSEC3 = hadNSEC3; | |
453 | zs.ns3pr = hadNs3pr; | |
454 | zs.optOutFlag = (hadNs3pr.d_flags & 1); | |
455 | zs.isNarrow = hadNarrow; | |
53568203 KM |
456 | } |
457 | } | |
458 | ||
d3ee36f2 | 459 | if(zs.isDnssecZone) { |
460 | if(!zs.isNSEC3) | |
e6a9dde5 | 461 | g_log<<Logger::Info<<"Adding NSEC ordering information"<<endl; |
d3ee36f2 | 462 | else if(!zs.isNarrow) |
e6a9dde5 | 463 | g_log<<Logger::Info<<"Adding NSEC3 hashed ordering information for '"<<domain<<"'"<<endl; |
f9cf6d92 | 464 | else |
e6a9dde5 | 465 | g_log<<Logger::Info<<"Erasing NSEC3 ordering since we are narrow, only setting 'auth' fields"<<endl; |
f9cf6d92 KM |
466 | } |
467 | ||
b5baefaf | 468 | |
d3ee36f2 | 469 | transaction=di.backend->startTransaction(domain, zs.domain_id); |
e6a9dde5 | 470 | g_log<<Logger::Error<<"Backend transaction started for '"<<domain<<"' storage"<<endl; |
f9cf6d92 | 471 | |
7f2aa497 | 472 | // update the presigned flag and NSEC3PARAM |
d3ee36f2 | 473 | if (zs.isDnssecZone) { |
53568203 | 474 | // update presigned if there was a change |
d3ee36f2 | 475 | if (zs.isPresigned && !hadPresigned) { |
7f2aa497 KM |
476 | // zone is now presigned |
477 | dk.setPresigned(domain); | |
d3ee36f2 | 478 | } else if (hadPresigned && !zs.isPresigned) { |
53568203 KM |
479 | // zone is no longer presigned |
480 | dk.unsetPresigned(domain); | |
7f2aa497 | 481 | } |
53568203 | 482 | // update NSEC3PARAM |
d3ee36f2 | 483 | if (zs.isNSEC3) { |
53568203 | 484 | // zone is NSEC3, only update if there was a change |
d3ee36f2 | 485 | if (!hadNSEC3 || (hadNarrow != zs.isNarrow) || |
486 | (zs.ns3pr.d_algorithm != hadNs3pr.d_algorithm) || | |
487 | (zs.ns3pr.d_flags != hadNs3pr.d_flags) || | |
488 | (zs.ns3pr.d_iterations != hadNs3pr.d_iterations) || | |
489 | (zs.ns3pr.d_salt != hadNs3pr.d_salt)) { | |
490 | dk.setNSEC3PARAM(domain, zs.ns3pr, zs.isNarrow); | |
7f2aa497 | 491 | } |
53568203 KM |
492 | } else if (hadNSEC3 ) { |
493 | // zone is no longer NSEC3 | |
494 | dk.unsetNSEC3PARAM(domain); | |
495 | } | |
496 | } else if (hadDnssecZone) { | |
497 | // zone is no longer signed | |
498 | if (hadPresigned) { | |
499 | // remove presigned | |
500 | dk.unsetPresigned(domain); | |
501 | } | |
502 | if (hadNSEC3) { | |
503 | // unset NSEC3PARAM | |
504 | dk.unsetNSEC3PARAM(domain); | |
7f2aa497 | 505 | } |
7f2aa497 KM |
506 | } |
507 | ||
f9cf6d92 | 508 | bool doent=true; |
b5baefaf | 509 | uint32_t maxent = ::arg().asNum("max-ent-entries"); |
c9b43446 | 510 | DNSName shorter, ordername; |
7abbc40f PD |
511 | set<DNSName> rrterm; |
512 | map<DNSName,bool> nonterm; | |
b5baefaf | 513 | |
53568203 | 514 | |
ef7cd021 | 515 | for(DNSResourceRecord& rr : rrs) { |
d3ee36f2 | 516 | if(!zs.isPresigned) { |
53568203 KM |
517 | if (rr.qtype.getCode() == QType::RRSIG) |
518 | continue; | |
d3ee36f2 | 519 | if(zs.isDnssecZone && rr.qtype.getCode() == QType::DNSKEY && !::arg().mustDo("direct-dnskey")) |
53568203 KM |
520 | continue; |
521 | } | |
522 | ||
f9cf6d92 KM |
523 | // Figure out auth and ents |
524 | rr.auth=true; | |
525 | shorter=rr.qname; | |
526 | rrterm.clear(); | |
527 | do { | |
528 | if(doent) { | |
d3ee36f2 | 529 | if (!zs.qnames.count(shorter)) |
f9cf6d92 | 530 | rrterm.insert(shorter); |
d7652f3a | 531 | } |
d3ee36f2 | 532 | if(zs.nsset.count(shorter) && rr.qtype.getCode() != QType::DS) |
f9cf6d92 | 533 | rr.auth=false; |
e5c7239c | 534 | |
e325f20c | 535 | if (shorter==domain) // stop at apex |
f9cf6d92 | 536 | break; |
7abbc40f | 537 | }while(shorter.chopOff()); |
b8adb30d | 538 | |
e5c7239c KM |
539 | // Insert ents |
540 | if(doent && !rrterm.empty()) { | |
541 | bool auth; | |
542 | if (!rr.auth && rr.qtype.getCode() == QType::NS) { | |
d3ee36f2 | 543 | if (zs.isNSEC3) |
c9b43446 KM |
544 | ordername=DNSName(toBase32Hex(hashQNameWithSalt(zs.ns3pr, rr.qname))); |
545 | auth=(!zs.isNSEC3 || !zs.optOutFlag || zs.secured.count(ordername)); | |
e5c7239c KM |
546 | } else |
547 | auth=rr.auth; | |
548 | ||
7abbc40f | 549 | for(const auto &nt: rrterm){ |
e5c7239c | 550 | if (!nonterm.count(nt)) |
7abbc40f | 551 | nonterm.insert(pair<DNSName, bool>(nt, auth)); |
e5c7239c KM |
552 | else if (auth) |
553 | nonterm[nt]=true; | |
554 | } | |
555 | ||
f9cf6d92 | 556 | if(nonterm.size() > maxent) { |
e6a9dde5 | 557 | g_log<<Logger::Error<<"AXFR zone "<<domain<<" has too many empty non terminals."<<endl; |
f9cf6d92 KM |
558 | nonterm.clear(); |
559 | doent=false; | |
27045410 | 560 | } |
d7652f3a | 561 | } |
f9cf6d92 KM |
562 | |
563 | // RRSIG is always auth, even inside a delegation | |
564 | if (rr.qtype.getCode() == QType::RRSIG) | |
565 | rr.auth=true; | |
566 | ||
567 | // Add ordername and insert record | |
d3ee36f2 | 568 | if (zs.isDnssecZone && rr.qtype.getCode() != QType::RRSIG) { |
569 | if (zs.isNSEC3) { | |
f9cf6d92 | 570 | // NSEC3 |
c9b43446 KM |
571 | ordername=DNSName(toBase32Hex(hashQNameWithSalt(zs.ns3pr, rr.qname))); |
572 | if(!zs.isNarrow && (rr.auth || (rr.qtype.getCode() == QType::NS && (!zs.optOutFlag || zs.secured.count(ordername))))) { | |
3bb3f561 | 573 | di.backend->feedRecord(rr, ordername, true); |
f9cf6d92 | 574 | } else |
c9b43446 | 575 | di.backend->feedRecord(rr, DNSName()); |
f9cf6d92 KM |
576 | } else { |
577 | // NSEC | |
578 | if (rr.auth || rr.qtype.getCode() == QType::NS) { | |
c9b43446 KM |
579 | ordername=rr.qname.makeRelative(domain); |
580 | di.backend->feedRecord(rr, ordername); | |
f9cf6d92 | 581 | } else |
c9b43446 | 582 | di.backend->feedRecord(rr, DNSName()); |
f9cf6d92 KM |
583 | } |
584 | } else | |
c9b43446 | 585 | di.backend->feedRecord(rr, DNSName()); |
d7652f3a | 586 | } |
b5baefaf | 587 | |
f9cf6d92 KM |
588 | // Insert empty non-terminals |
589 | if(doent && !nonterm.empty()) { | |
d3ee36f2 | 590 | if (zs.isNSEC3) { |
591 | di.backend->feedEnts3(zs.domain_id, domain, nonterm, zs.ns3pr, zs.isNarrow); | |
f9cf6d92 | 592 | } else |
d3ee36f2 | 593 | di.backend->feedEnts(zs.domain_id, nonterm); |
b5baefaf PD |
594 | } |
595 | ||
14b7e03b | 596 | di.backend->commitTransaction(); |
376ec278 | 597 | transaction = false; |
d3ee36f2 | 598 | di.backend->setFresh(zs.domain_id); |
38fdb531 | 599 | purgeAuthCaches(domain.toString()+"$"); |
14b7e03b | 600 | |
a1282cdd | 601 | |
e6a9dde5 | 602 | g_log<<Logger::Error<<"AXFR done for '"<<domain<<"', zone committed with serial number "<<zs.soa_serial<<endl; |
7b4e8eed MH |
603 | |
604 | bool renotify = false; | |
8de9c054 | 605 | if(::arg().mustDo("slave-renotify")) |
7b4e8eed MH |
606 | renotify = true; |
607 | vector<string> meta; | |
608 | if (B.getDomainMetadata(domain, "SLAVE-RENOTIFY", meta) && meta.size() > 0) { | |
609 | if (meta[0] == "1") { | |
610 | renotify = true; | |
611 | } else { | |
612 | renotify = false; | |
613 | } | |
614 | } | |
615 | if(renotify) | |
8de9c054 | 616 | notifyDomain(domain); |
3696224d BH |
617 | } |
618 | catch(DBException &re) { | |
e6a9dde5 | 619 | g_log<<Logger::Error<<"Unable to feed record during incoming AXFR of '" << domain<<"': "<<re.reason<<endl; |
376ec278 | 620 | if(di.backend && transaction) { |
e6a9dde5 | 621 | g_log<<Logger::Error<<"Aborting possible open transaction for domain '"<<domain<<"' AXFR"<<endl; |
3696224d BH |
622 | di.backend->abortTransaction(); |
623 | } | |
624 | } | |
16ce7f18 JS |
625 | catch(const MOADNSException &mde) { |
626 | g_log<<Logger::Error<<"Unable to parse record during incoming AXFR of '"<<domain<<"' (MOADNSException): "<<mde.what()<<endl; | |
376ec278 | 627 | if(di.backend && transaction) { |
e6a9dde5 | 628 | g_log<<Logger::Error<<"Aborting possible open transaction for domain '"<<domain<<"' AXFR"<<endl; |
2714db17 BH |
629 | di.backend->abortTransaction(); |
630 | } | |
631 | } | |
632 | catch(std::exception &re) { | |
e6a9dde5 | 633 | g_log<<Logger::Error<<"Unable to parse record during incoming AXFR of '"<<domain<<"' (std::exception): "<<re.what()<<endl; |
376ec278 | 634 | if(di.backend && transaction) { |
e6a9dde5 | 635 | g_log<<Logger::Error<<"Aborting possible open transaction for domain '"<<domain<<"' AXFR"<<endl; |
75ccb5b9 BH |
636 | di.backend->abortTransaction(); |
637 | } | |
638 | } | |
3696224d | 639 | catch(ResolverException &re) { |
dd73b264 KD |
640 | { |
641 | Lock l(&d_lock); | |
642 | // The AXFR probably failed due to a problem on the master server. If SOA-checks against this master | |
643 | // still succeed, we would constantly try to AXFR the zone. To avoid this, we add the zone to the list of | |
644 | // failed slave-checks. This will suspend slave-checks (and subsequent AXFR) for this zone for some time. | |
645 | uint64_t newCount = 1; | |
646 | time_t now = time(0); | |
647 | const auto failedEntry = d_failedSlaveRefresh.find(domain); | |
648 | if (failedEntry != d_failedSlaveRefresh.end()) | |
649 | newCount = d_failedSlaveRefresh[domain].first + 1; | |
650 | time_t nextCheck = now + std::min(newCount * d_tickinterval, (uint64_t)::arg().asNum("soa-retry-default")); | |
651 | d_failedSlaveRefresh[domain] = {newCount, nextCheck}; | |
652 | g_log<<Logger::Error<<"Unable to AXFR zone '"<<domain<<"' from remote '"<<remote<<"' (resolver): "<<re.reason<<" (This was the "<<(newCount == 1 ? "first" : std::to_string(newCount) + "th")<<" time. Excluding zone from slave-checks until "<<nextCheck<<")"<<endl; | |
653 | } | |
376ec278 | 654 | if(di.backend && transaction) { |
e6a9dde5 | 655 | g_log<<Logger::Error<<"Aborting possible open transaction for domain '"<<domain<<"' AXFR"<<endl; |
7f450125 PD |
656 | di.backend->abortTransaction(); |
657 | } | |
658 | } | |
3f81d239 | 659 | catch(PDNSException &ae) { |
e6a9dde5 | 660 | g_log<<Logger::Error<<"Unable to AXFR zone '"<<domain<<"' from remote '"<<remote<<"' (PDNSException): "<<ae.reason<<endl; |
376ec278 | 661 | if(di.backend && transaction) { |
e6a9dde5 | 662 | g_log<<Logger::Error<<"Aborting possible open transaction for domain '"<<domain<<"' AXFR"<<endl; |
3696224d BH |
663 | di.backend->abortTransaction(); |
664 | } | |
665 | } | |
666 | } | |
7597b3cf | 667 | namespace { |
7597b3cf BH |
668 | struct DomainNotificationInfo |
669 | { | |
670 | DomainInfo di; | |
671 | bool dnssecOk; | |
95302209 | 672 | ComboAddress localaddr; |
561434a6 PD |
673 | DNSName tsigkeyname, tsigalgname; |
674 | string tsigsecret; | |
7597b3cf BH |
675 | }; |
676 | } | |
677 | ||
3696224d BH |
678 | |
679 | struct SlaveSenderReceiver | |
680 | { | |
40c9a111 | 681 | typedef std::tuple<DNSName, ComboAddress, uint16_t> Identifier; |
53568203 | 682 | |
54b2edb4 BH |
683 | struct Answer { |
684 | uint32_t theirSerial; | |
685 | uint32_t theirInception; | |
686 | uint32_t theirExpire; | |
687 | }; | |
53568203 | 688 | |
54b2edb4 | 689 | map<uint32_t, Answer> d_freshness; |
53568203 | 690 | |
3696224d BH |
691 | SlaveSenderReceiver() |
692 | { | |
3696224d | 693 | } |
53568203 | 694 | |
c0a5fc34 | 695 | void deliverTimeout(const Identifier& i) |
0c01dd7c BH |
696 | { |
697 | } | |
53568203 | 698 | |
7597b3cf | 699 | Identifier send(DomainNotificationInfo& dni) |
3696224d | 700 | { |
7597b3cf | 701 | random_shuffle(dni.di.masters.begin(), dni.di.masters.end()); |
0c01dd7c | 702 | try { |
40c9a111 | 703 | return std::make_tuple(dni.di.zone, |
d622042f | 704 | *dni.di.masters.begin(), |
705 | d_resolver.sendResolve(*dni.di.masters.begin(), | |
40c9a111 RG |
706 | dni.localaddr, |
707 | dni.di.zone, | |
708 | QType::SOA, | |
709 | nullptr, | |
710 | dni.dnssecOk, dni.tsigkeyname, dni.tsigalgname, dni.tsigsecret) | |
711 | ); | |
0c01dd7c | 712 | } |
3f81d239 | 713 | catch(PDNSException& e) { |
86f1af1c | 714 | throw runtime_error("While attempting to query freshness of '"+dni.di.zone.toLogString()+"': "+e.reason); |
0c01dd7c | 715 | } |
3696224d | 716 | } |
68dae32c | 717 | |
3696224d BH |
718 | bool receive(Identifier& id, Answer& a) |
719 | { | |
40c9a111 | 720 | if(d_resolver.tryGetSOASerial(&(std::get<0>(id)), &(std::get<1>(id)), &a.theirSerial, &a.theirInception, &a.theirExpire, &(std::get<2>(id)))) { |
3696224d BH |
721 | return 1; |
722 | } | |
723 | return 0; | |
724 | } | |
68dae32c | 725 | |
7597b3cf | 726 | void deliverAnswer(DomainNotificationInfo& dni, const Answer& a, unsigned int usec) |
3696224d | 727 | { |
7597b3cf | 728 | d_freshness[dni.di.id]=a; |
3696224d | 729 | } |
68dae32c | 730 | |
3696224d | 731 | Resolver d_resolver; |
3696224d BH |
732 | }; |
733 | ||
7f3d870e BH |
734 | void CommunicatorClass::addSlaveCheckRequest(const DomainInfo& di, const ComboAddress& remote) |
735 | { | |
736 | Lock l(&d_lock); | |
90c9a70e BH |
737 | DomainInfo ours = di; |
738 | ours.backend = 0; | |
d381d4a4 OM |
739 | |
740 | // When adding a check, if the remote addr from which notification was | |
741 | // received is a master, clear all other masters so we can be sure the | |
742 | // query goes to that one. | |
a9abb26d KM |
743 | for (const auto& master : di.masters) { |
744 | if (ComboAddress::addressOnlyEqual()(remote, master)) { | |
d381d4a4 | 745 | ours.masters.clear(); |
a9abb26d | 746 | ours.masters.push_back(master); |
d381d4a4 OM |
747 | break; |
748 | } | |
749 | } | |
750 | d_tocheck.erase(di); | |
90c9a70e | 751 | d_tocheck.insert(ours); |
7f3d870e BH |
752 | d_any_sem.post(); // kick the loop! |
753 | } | |
754 | ||
7108e055 PD |
755 | void CommunicatorClass::addTrySuperMasterRequest(DNSPacket *p) |
756 | { | |
757 | Lock l(&d_lock); | |
758 | DNSPacket ours = *p; | |
02980dc2 | 759 | if(d_potentialsupermasters.insert(ours).second) |
760 | d_any_sem.post(); // kick the loop! | |
7108e055 PD |
761 | } |
762 | ||
3696224d BH |
763 | void CommunicatorClass::slaveRefresh(PacketHandler *P) |
764 | { | |
56a4f068 AT |
765 | // not unless we are slave |
766 | if (!::arg().mustDo("slave")) return; | |
767 | ||
3971cf53 | 768 | UeberBackend *B=P->getBackend(); |
16e654fa | 769 | vector<DomainInfo> rdomains; |
02980dc2 | 770 | vector<DomainNotificationInfo> sdomains; |
771 | set<DNSPacket, cmp> trysuperdomains; | |
7f3d870e BH |
772 | { |
773 | Lock l(&d_lock); | |
7d4ac70c PD |
774 | set<DomainInfo> requeue; |
775 | for(const auto& di: d_tocheck) { | |
776 | if(d_inprogress.count(di.zone)) { | |
777 | g_log<<Logger::Debug<<"Got NOTIFY for "<<di.zone<<" while AXFR in progress, requeueing SOA check"<<endl; | |
778 | requeue.insert(di); | |
779 | } | |
780 | else { | |
f1a7ff7a KD |
781 | // We received a NOTIFY for a zone. This means at least one of the zone's master server is working. |
782 | // Therefore we delete the zone from the list of failed slave-checks to allow immediate checking. | |
783 | const auto wasFailedDomain = d_failedSlaveRefresh.find(di.zone); | |
784 | if (wasFailedDomain != d_failedSlaveRefresh.end()) { | |
785 | g_log<<Logger::Debug<<"Got NOTIFY for "<<di.zone<<", removing zone from list of failed slave-checks and going to check SOA serial"<<endl; | |
786 | d_failedSlaveRefresh.erase(di.zone); | |
787 | } else { | |
788 | g_log<<Logger::Debug<<"Got NOTIFY for "<<di.zone<<", going to check SOA serial"<<endl; | |
789 | } | |
7d4ac70c PD |
790 | rdomains.push_back(di); |
791 | } | |
792 | } | |
793 | d_tocheck.swap(requeue); | |
02980dc2 | 794 | |
795 | trysuperdomains = d_potentialsupermasters; | |
7108e055 | 796 | d_potentialsupermasters.clear(); |
7f3d870e | 797 | } |
68dae32c | 798 | |
02980dc2 | 799 | for(const DNSPacket& dp : trysuperdomains) { |
7731e32c AT |
800 | // get the TSIG key name |
801 | TSIGRecordContent trc; | |
802 | DNSName tsigkeyname; | |
ea3816cf | 803 | dp.getTSIGDetails(&trc, &tsigkeyname); |
e8090386 | 804 | P->trySuperMasterSynchronous(&dp, tsigkeyname); // FIXME could use some error loging |
7108e055 | 805 | } |
3e7dcee6 | 806 | if(rdomains.empty()) { // if we have priority domains, check them first |
7f3d870e | 807 | B->getUnfreshSlaveInfos(&rdomains); |
3e7dcee6 | 808 | } |
936eb34a | 809 | DNSSECKeeper dk(B); // NOW HEAR THIS! This DK uses our B backend, so no interleaved access! |
dbcb3066 BH |
810 | { |
811 | Lock l(&d_lock); | |
dbcb3066 | 812 | domains_by_name_t& nameindex=boost::multi_index::get<IDTag>(d_suckdomains); |
b239dfba | 813 | time_t now = time(0); |
dbcb3066 | 814 | |
ef7cd021 | 815 | for(DomainInfo& di : rdomains) { |
b239dfba | 816 | const auto failed = d_failedSlaveRefresh.find(di.zone); |
f1a7ff7a | 817 | if (failed != d_failedSlaveRefresh.end() && now < failed->second.second ) { |
b239dfba | 818 | // If the domain has failed before and the time before the next check has not expired, skip this domain |
f1a7ff7a | 819 | g_log<<Logger::Debug<<"Zone '"<<di.zone<<"' is on the list of failed SOA checks. Skipping SOA checks until "<< failed->second.second<<endl; |
b239dfba | 820 | continue; |
f1a7ff7a | 821 | } |
95302209 | 822 | std::vector<std::string> localaddr; |
dbcb3066 BH |
823 | SuckRequest sr; |
824 | sr.domain=di.zone; | |
825 | if(di.masters.empty()) // slave domains w/o masters are ignored | |
826 | continue; | |
827 | // remove unfresh domains already queued for AXFR, no sense polling them again | |
828 | sr.master=*di.masters.begin(); | |
3e7dcee6 | 829 | if(nameindex.count(sr)) { // this does NOT however protect us against AXFRs already in progress! |
dbcb3066 | 830 | continue; |
a71bee29 | 831 | } |
3e7dcee6 | 832 | if(d_inprogress.count(sr.domain)) // this does |
833 | continue; | |
834 | ||
7597b3cf BH |
835 | DomainNotificationInfo dni; |
836 | dni.di=di; | |
7fa35c07 | 837 | dni.dnssecOk = dk.doesDNSSEC(); |
68dae32c | 838 | |
7597b3cf BH |
839 | if(dk.getTSIGForAccess(di.zone, sr.master, &dni.tsigkeyname)) { |
840 | string secret64; | |
40920298 | 841 | if(!B->getTSIGKey(dni.tsigkeyname, &dni.tsigalgname, &secret64)) { |
e6a9dde5 | 842 | g_log<<Logger::Error<<"TSIG key '"<<dni.tsigkeyname<<"' for domain '"<<di.zone<<"' not found, can not AXFR."<<endl; |
40920298 PL |
843 | continue; |
844 | } | |
845 | if (B64Decode(secret64, dni.tsigsecret) == -1) { | |
e6a9dde5 | 846 | g_log<<Logger::Error<<"Unable to Base-64 decode TSIG key '"<<dni.tsigkeyname<<"' for domain '"<<di.zone<<"', can not AXFR."<<endl; |
40920298 PL |
847 | continue; |
848 | } | |
7597b3cf | 849 | } |
95302209 AT |
850 | |
851 | localaddr.clear(); | |
852 | // check for AXFR-SOURCE | |
853 | if(B->getDomainMetadata(di.zone, "AXFR-SOURCE", localaddr) && !localaddr.empty()) { | |
854 | try { | |
855 | dni.localaddr = ComboAddress(localaddr[0]); | |
e6a9dde5 | 856 | g_log<<Logger::Info<<"Freshness check source (AXFR-SOURCE) for domain '"<<di.zone<<"' set to "<<localaddr[0]<<endl; |
95302209 AT |
857 | } |
858 | catch(std::exception& e) { | |
e6a9dde5 | 859 | g_log<<Logger::Error<<"Failed to load freshness check source '"<<localaddr[0]<<"' for '"<<di.zone<<"': "<<e.what()<<endl; |
95302209 AT |
860 | return; |
861 | } | |
862 | } else { | |
863 | dni.localaddr.sin4.sin_family = 0; | |
864 | } | |
865 | ||
7597b3cf | 866 | sdomains.push_back(dni); |
dbcb3066 | 867 | } |
dbcb3066 | 868 | } |
3696224d BH |
869 | if(sdomains.empty()) |
870 | { | |
dbcb3066 BH |
871 | if(d_slaveschanged) { |
872 | Lock l(&d_lock); | |
e6a9dde5 | 873 | g_log<<Logger::Warning<<"No new unfresh slave domains, "<<d_suckdomains.size()<<" queued for AXFR already, "<<d_inprogress.size()<<" in progress"<<endl; |
dbcb3066 BH |
874 | } |
875 | d_slaveschanged = !rdomains.empty(); | |
3696224d BH |
876 | return; |
877 | } | |
dbcb3066 BH |
878 | else { |
879 | Lock l(&d_lock); | |
e6a9dde5 | 880 | g_log<<Logger::Warning<<sdomains.size()<<" slave domain"<<(sdomains.size()>1 ? "s" : "")<<" need"<< |
3696224d | 881 | (sdomains.size()>1 ? "" : "s")<< |
dbcb3066 BH |
882 | " checking, "<<d_suckdomains.size()<<" queued for AXFR"<<endl; |
883 | } | |
68dae32c | 884 | |
3696224d | 885 | SlaveSenderReceiver ssr; |
68dae32c | 886 | |
7597b3cf | 887 | Inflighter<vector<DomainNotificationInfo>, SlaveSenderReceiver> ifl(sdomains, ssr); |
68dae32c | 888 | |
3696224d BH |
889 | ifl.d_maxInFlight = 200; |
890 | ||
891 | for(;;) { | |
892 | try { | |
893 | ifl.run(); | |
894 | break; | |
895 | } | |
dbcb3066 | 896 | catch(std::exception& e) { |
e6a9dde5 | 897 | g_log<<Logger::Error<<"While checking domain freshness: " << e.what()<<endl; |
3696224d | 898 | } |
68dae32c | 899 | catch(PDNSException &re) { |
e6a9dde5 | 900 | g_log<<Logger::Error<<"While checking domain freshness: " << re.reason<<endl; |
3696224d BH |
901 | } |
902 | } | |
e6a9dde5 | 903 | g_log<<Logger::Warning<<"Received serial number updates for "<<ssr.d_freshness.size()<<" zone"<<addS(ssr.d_freshness.size())<<", had "<<ifl.getTimeouts()<<" timeout"<<addS(ifl.getTimeouts())<<endl; |
16e654fa | 904 | |
7597b3cf | 905 | typedef DomainNotificationInfo val_t; |
b239dfba | 906 | time_t now = time(0); |
ef7cd021 | 907 | for(val_t& val : sdomains) { |
7597b3cf | 908 | DomainInfo& di(val.di); |
d381d4a4 | 909 | DomainInfo tempdi; |
f9fa0e2d | 910 | // might've come from the packethandler |
d381d4a4 OM |
911 | // Please do not overwrite received DI just to make sure it exists in backend. |
912 | if(!di.backend) { | |
913 | if (!B->getDomainInfo(di.zone, tempdi)) { | |
e6a9dde5 | 914 | g_log<<Logger::Warning<<"Ignore domain "<< di.zone<<" since it has been removed from our backend"<<endl; |
232f0877 | 915 | continue; |
d381d4a4 OM |
916 | } |
917 | // Backend for di still doesn't exist and this might cause us to | |
918 | // SEGFAULT on the setFresh command later on | |
919 | di.backend = tempdi.backend; | |
f9fa0e2d | 920 | } |
68dae32c | 921 | |
b239dfba PL |
922 | if(!ssr.d_freshness.count(di.id)) { // If we don't have an answer for the domain |
923 | uint64_t newCount = 1; | |
f1a7ff7a | 924 | Lock l(&d_lock); |
b239dfba PL |
925 | const auto failedEntry = d_failedSlaveRefresh.find(di.zone); |
926 | if (failedEntry != d_failedSlaveRefresh.end()) | |
927 | newCount = d_failedSlaveRefresh[di.zone].first + 1; | |
928 | time_t nextCheck = now + std::min(newCount * d_tickinterval, (uint64_t)::arg().asNum("soa-retry-default")); | |
929 | d_failedSlaveRefresh[di.zone] = {newCount, nextCheck}; | |
f1a7ff7a KD |
930 | if (newCount == 1) { |
931 | g_log<<Logger::Warning<<"Unable to retrieve SOA for "<<di.zone<< | |
932 | ", this was the first time. NOTE: For every subsequent failed SOA check the domain will be suspended from freshness checks for 'num-errors x "<< | |
933 | d_tickinterval<<" seconds', with a maximum of "<<(uint64_t)::arg().asNum("soa-retry-default")<<" seconds. Skipping SOA checks until "<<nextCheck<<endl; | |
934 | } else if (newCount % 10 == 0) { | |
935 | g_log<<Logger::Warning<<"Unable to retrieve SOA for "<<di.zone<<", this was the "<<std::to_string(newCount)<<"th time. Skipping SOA checks until "<<nextCheck<<endl; | |
936 | } | |
3696224d | 937 | continue; |
b239dfba PL |
938 | } |
939 | ||
f1a7ff7a KD |
940 | { |
941 | Lock l(&d_lock); | |
942 | const auto wasFailedDomain = d_failedSlaveRefresh.find(di.zone); | |
943 | if (wasFailedDomain != d_failedSlaveRefresh.end()) | |
944 | d_failedSlaveRefresh.erase(di.zone); | |
945 | } | |
b239dfba | 946 | |
b76c5f3a KM |
947 | bool hasSOA = false; |
948 | SOAData sd; | |
949 | try{ | |
950 | hasSOA = B->getSOA(di.zone, sd); | |
951 | } | |
952 | catch(...) {} | |
953 | ||
954 | uint32_t theirserial = ssr.d_freshness[di.id].theirSerial, ourserial = sd.serial; | |
68dae32c | 955 | |
0f76617d | 956 | if(rfc1982LessThan(theirserial, ourserial) && ourserial != 0 && !::arg().mustDo("axfr-lower-serial")) { |
e6a9dde5 | 957 | g_log<<Logger::Error<<"Domain '"<<di.zone<<"' more recent than master, our serial " << ourserial << " > their serial "<< theirserial << endl; |
3696224d BH |
958 | di.backend->setFresh(di.id); |
959 | } | |
b76c5f3a | 960 | else if(hasSOA && theirserial == ourserial) { |
7fa35c07 KM |
961 | uint32_t maxExpire=0, maxInception=0; |
962 | if(dk.isPresigned(di.zone)) { | |
acb61e0a | 963 | B->lookup(QType(QType::RRSIG), di.zone, -1); // can't use DK before we are done with this lookup! |
abcd36a1 | 964 | DNSZoneRecord zr; |
abcd36a1 CH |
965 | while(B->get(zr)) { |
966 | auto rrsig = getRR<RRSIGRecordContent>(zr.dr); | |
967 | if(rrsig->d_type == QType::SOA) { | |
968 | maxInception = std::max(maxInception, rrsig->d_siginception); | |
969 | maxExpire = std::max(maxExpire, rrsig->d_sigexpire); | |
54b2edb4 BH |
970 | } |
971 | } | |
7fa35c07 KM |
972 | } |
973 | if(! maxInception && ! ssr.d_freshness[di.id].theirInception) { | |
b3d982f3 | 974 | g_log<<Logger::Info<<"Domain '"<< di.zone<<"' is fresh (no DNSSEC), serial is "<<ourserial<<endl; |
7fa35c07 KM |
975 | di.backend->setFresh(di.id); |
976 | } | |
977 | else if(maxInception == ssr.d_freshness[di.id].theirInception && maxExpire == ssr.d_freshness[di.id].theirExpire) { | |
b3d982f3 | 978 | g_log<<Logger::Info<<"Domain '"<< di.zone<<"' is fresh and SOA RRSIGs match, serial is "<<ourserial<<endl; |
7fa35c07 KM |
979 | di.backend->setFresh(di.id); |
980 | } | |
981 | else if(maxExpire >= now && ! ssr.d_freshness[di.id].theirInception ) { | |
b3d982f3 | 982 | g_log<<Logger::Info<<"Domain '"<< di.zone<<"' is fresh, master is no longer signed but (some) signatures are still vallid, serial is "<<ourserial<<endl; |
7fa35c07 KM |
983 | di.backend->setFresh(di.id); |
984 | } | |
985 | else if(maxInception && ! ssr.d_freshness[di.id].theirInception ) { | |
b3d982f3 | 986 | g_log<<Logger::Warning<<"Domain '"<< di.zone<<"' is stale, master is no longer signed and all signatures have expired, serial is "<<ourserial<<endl; |
7fa35c07 KM |
987 | addSuckRequest(di.zone, *di.masters.begin()); |
988 | } | |
989 | else if(dk.doesDNSSEC() && ! maxInception && ssr.d_freshness[di.id].theirInception) { | |
b3d982f3 | 990 | g_log<<Logger::Warning<<"Domain '"<< di.zone<<"' is stale, master has signed, serial is "<<ourserial<<endl; |
7fa35c07 KM |
991 | addSuckRequest(di.zone, *di.masters.begin()); |
992 | } | |
993 | else { | |
b3d982f3 | 994 | g_log<<Logger::Warning<<"Domain '"<< di.zone<<"' is fresh, but RRSIGs differ, so DNSSEC is stale, serial is "<<ourserial<<endl; |
7fa35c07 | 995 | addSuckRequest(di.zone, *di.masters.begin()); |
54b2edb4 | 996 | } |
3696224d BH |
997 | } |
998 | else { | |
b76c5f3a KM |
999 | if(hasSOA) { |
1000 | g_log<<Logger::Warning<<"Domain '"<< di.zone<<"' is stale, master serial "<<theirserial<<", our serial "<< ourserial <<endl; | |
1001 | } | |
1002 | else { | |
1003 | g_log<<Logger::Warning<<"Domain '"<< di.zone<<"' is empty, master serial "<<theirserial<<endl; | |
1004 | } | |
d3ee36f2 | 1005 | addSuckRequest(di.zone, *di.masters.begin()); |
3696224d BH |
1006 | } |
1007 | } | |
68dae32c | 1008 | } |