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