]>
Commit | Line | Data |
---|---|---|
f1b59a55 RA |
1 | #include "packethandler.hh" |
2 | #include "qtype.hh" | |
3 | #include "dnspacket.hh" | |
4 | #include "packetcache.hh" | |
5 | #include "dnsseckeeper.hh" | |
6 | #include "base64.hh" | |
7 | #include "base32.hh" | |
8 | #include "misc.hh" | |
9 | #include "arguments.hh" | |
b3148887 RA |
10 | #include "resolver.hh" |
11 | #include "dns_random.hh" | |
7facf345 | 12 | #include "backends/gsql/ssql.hh" |
f1b59a55 RA |
13 | |
14 | extern PacketCache PC; | |
276a5ce7 | 15 | extern StatBag S; |
f1b59a55 | 16 | |
ee9ef8f2 RA |
17 | pthread_mutex_t PacketHandler::s_rfc2136lock=PTHREAD_MUTEX_INITIALIZER; |
18 | ||
f1b59a55 RA |
19 | // Implement section 3.2.1 and 3.2.2 of RFC2136 |
20 | int PacketHandler::checkUpdatePrerequisites(const DNSRecord *rr, DomainInfo *di) { | |
21 | if (rr->d_ttl != 0) | |
22 | return RCode::FormErr; | |
23 | ||
24 | // 3.2.1 and 3.2.2 check content length. | |
25 | if ( (rr->d_class == QClass::NONE || rr->d_class == QClass::ANY) && rr->d_clen != 0) | |
26 | return RCode::FormErr; | |
27 | ||
6a323f63 | 28 | string rrLabel = stripDot(rr->d_label); |
f1b59a55 RA |
29 | |
30 | bool foundRecord=false; | |
31 | DNSResourceRecord rec; | |
6a323f63 | 32 | di->backend->lookup(QType(QType::ANY), rrLabel); |
f1b59a55 RA |
33 | while(di->backend->get(rec)) { |
34 | if (!rec.qtype.getCode()) | |
35 | continue; | |
36 | if ((rr->d_type != QType::ANY && rec.qtype == rr->d_type) || rr->d_type == QType::ANY) | |
37 | foundRecord=true; | |
38 | } | |
39 | ||
40 | // Section 3.2.1 | |
41 | if (rr->d_class == QClass::ANY && !foundRecord) { | |
42 | if (rr->d_type == QType::ANY) | |
43 | return RCode::NXDomain; | |
44 | if (rr->d_type != QType::ANY) | |
45 | return RCode::NXRRSet; | |
46 | } | |
47 | ||
48 | // Section 3.2.2 | |
49 | if (rr->d_class == QClass::NONE && foundRecord) { | |
50 | if (rr->d_type == QType::ANY) | |
51 | return RCode::YXDomain; | |
52 | if (rr->d_type != QType::ANY) | |
53 | return RCode::YXRRSet; | |
54 | } | |
55 | ||
56 | return RCode::NoError; | |
57 | } | |
58 | ||
59 | ||
60 | // Method implements section 3.4.1 of RFC2136 | |
61 | int PacketHandler::checkUpdatePrescan(const DNSRecord *rr) { | |
62 | // The RFC stats that d_class != ZCLASS, but we only support the IN class. | |
63 | if (rr->d_class != QClass::IN && rr->d_class != QClass::NONE && rr->d_class != QClass::ANY) | |
64 | return RCode::FormErr; | |
65 | ||
66 | QType qtype = QType(rr->d_type); | |
67 | ||
68 | if (! qtype.isSupportedType()) | |
69 | return RCode::FormErr; | |
70 | ||
71 | if ((rr->d_class == QClass::NONE || rr->d_class == QClass::ANY) && rr->d_ttl != 0) | |
72 | return RCode::FormErr; | |
73 | ||
74 | if (rr->d_class == QClass::ANY && rr->d_clen != 0) | |
75 | return RCode::FormErr; | |
76 | ||
77 | if (qtype.isMetadataType()) | |
78 | return RCode::FormErr; | |
79 | ||
80 | if (rr->d_class != QClass::ANY && qtype.getCode() == QType::ANY) | |
81 | return RCode::FormErr; | |
82 | ||
83 | return RCode::NoError; | |
84 | } | |
85 | ||
6a323f63 | 86 | |
f1b59a55 | 87 | // Implements section 3.4.2 of RFC2136 |
dff32e09 RA |
88 | uint16_t PacketHandler::performUpdate(const string &msgPrefix, const DNSRecord *rr, DomainInfo *di, bool isPresigned, bool* narrow, bool* haveNSEC3, NSEC3PARAMRecordContent *ns3pr, bool *updatedSerial) { |
89 | string rrLabel = stripDot(rr->d_label); | |
90 | rrLabel = toLower(rrLabel); | |
91 | QType rrType = QType(rr->d_type); | |
92 | ||
93 | ||
94 | if (rrType == QType::NSEC || rrType == QType::NSEC3) { | |
95 | L<<Logger::Warning<<msgPrefix<<"Trying to add/update/delete "<<rrLabel<<"|"<<rrType.getName()<<". These are generated records, ignoring!"<<endl; | |
96 | return 0; | |
97 | } | |
98 | ||
99 | if (!isPresigned && (rrType == QType::RRSIG || rrType == QType::DNSKEY) ) { | |
100 | L<<Logger::Warning<<msgPrefix<<"Trying to add/update/delete "<<rrLabel<<"|"<<rrType.getName()<<" in non-presigned zone, ignoring!"<<endl; | |
101 | return 0; | |
102 | } | |
103 | ||
104 | if (rrType == QType::NSEC3PARAM && rrLabel != di->zone) { | |
105 | L<<Logger::Warning<<msgPrefix<<"Trying to add/update/delete "<<rrLabel<<"|NSEC3PARAM, NSEC3PARAM must be at zone apex, ignoring!"<<endl; | |
106 | return 0; | |
107 | } | |
108 | ||
109 | ||
b0704e0d | 110 | uint16_t changedRecords = 0; |
6a323f63 RA |
111 | DNSResourceRecord rec; |
112 | vector<DNSResourceRecord> rrset, recordsToDelete; | |
113 | set<string> delnonterm, insnonterm; // used to (at the end) fix ENT records. | |
114 | ||
f1b59a55 | 115 | |
6a323f63 RA |
116 | if (rr->d_class == QClass::IN) { // 3.4.2.2 QClass::IN means insert or update |
117 | DLOG(L<<msgPrefix<<"Add/Update record (QClass == IN) "<<rrLabel<<"|"<<rrType.getName()<<endl); | |
f1b59a55 | 118 | |
dff32e09 | 119 | if (rrType == QType::NSEC3PARAM) { |
91ecc3f6 | 120 | L<<Logger::Notice<<msgPrefix<<"Setting NSEC3PARAM for zone, resetting ordernames and auth flags."<<endl; |
dff32e09 RA |
121 | NSEC3PARAMRecordContent nsec3param(rr->d_content->getZoneRepresentation(), di->zone); |
122 | d_dk.setNSEC3PARAM(di->zone, nsec3param, (*narrow)); | |
123 | *haveNSEC3 = d_dk.getNSEC3PARAM(di->zone, ns3pr, narrow); | |
124 | di->backend->list(di->zone, di->id); | |
125 | vector<DNSResourceRecord> rrs; | |
126 | while (di->backend->get(rec)) { | |
127 | rrs.push_back(rec); | |
128 | } | |
129 | for (vector<DNSResourceRecord>::const_iterator i = rrs.begin(); i != rrs.end(); i++) { | |
130 | if (*narrow) { | |
131 | di->backend->nullifyDNSSECOrderNameAndUpdateAuth(di->id, i->qname, i->auth); | |
132 | } else { | |
133 | string hashed=toLower(toBase32Hex(hashQNameWithSalt(ns3pr->d_iterations, ns3pr->d_salt, i->qname))); | |
134 | di->backend->updateDNSSECOrderAndAuthAbsolute(di->id, i->qname, hashed, i->auth); | |
135 | } | |
136 | } | |
137 | return 1; | |
138 | } | |
139 | ||
6a323f63 RA |
140 | bool foundRecord = false; |
141 | di->backend->lookup(rrType, rrLabel); | |
f1b59a55 | 142 | while (di->backend->get(rec)) { |
dff32e09 RA |
143 | rrset.push_back(rec); |
144 | foundRecord = true; | |
6a323f63 | 145 | } |
6a323f63 RA |
146 | |
147 | if (foundRecord) { | |
dff32e09 | 148 | if (rrType == QType::SOA) { // SOA updates require the serial to be higher than the current |
f1b59a55 | 149 | SOAData sdOld, sdUpdate; |
6a323f63 RA |
150 | DNSResourceRecord *oldRec = &rrset.front(); |
151 | fillSOAData(oldRec->content, sdOld); | |
152 | oldRec->setContent(rr->d_content->getZoneRepresentation()); | |
153 | fillSOAData(oldRec->content, sdUpdate); | |
f1b59a55 | 154 | if (rfc1982LessThan(sdOld.serial, sdUpdate.serial)) { |
b0704e0d | 155 | changedRecords++; |
6a323f63 | 156 | di->backend->replaceRRSet(di->id, oldRec->qname, oldRec->qtype, rrset); |
f1b59a55 | 157 | *updatedSerial = true; |
7facf345 | 158 | L<<Logger::Notice<<msgPrefix<<"Replacing record "<<rrLabel<<"|"<<rrType.getName()<<endl; |
f1b59a55 RA |
159 | } |
160 | else | |
161 | L<<Logger::Notice<<msgPrefix<<"Provided serial ("<<sdUpdate.serial<<") is older than the current serial ("<<sdOld.serial<<"), ignoring SOA update."<<endl; | |
6a323f63 RA |
162 | |
163 | // It's not possible to have multiple CNAME's with the same NAME. So we always update. | |
164 | } else if (rrType == QType::CNAME) { | |
165 | for (vector<DNSResourceRecord>::iterator i = rrset.begin(); i != rrset.end(); i++) { | |
166 | i->ttl = rr->d_ttl; | |
167 | i->setContent(rr->d_content->getZoneRepresentation()); | |
b0704e0d | 168 | changedRecords++; |
f1b59a55 | 169 | } |
6a323f63 | 170 | di->backend->replaceRRSet(di->id, rrLabel, rrType, rrset); |
7facf345 | 171 | L<<Logger::Notice<<msgPrefix<<"Replacing record "<<rrLabel<<"|"<<rrType.getName()<<endl; |
6a323f63 RA |
172 | |
173 | // In any other case, we must check if the TYPE and RDATA match to provide an update (which effectily means a update of TTL) | |
174 | } else { | |
175 | foundRecord = false; | |
176 | for (vector<DNSResourceRecord>::iterator i = rrset.begin(); i != rrset.end(); i++) { | |
177 | string content = rr->d_content->getZoneRepresentation(); | |
178 | if (rrType == i->qtype.getCode() && i->getZoneRepresentation() == content) { | |
179 | foundRecord = true; | |
180 | i->ttl = rr->d_ttl; | |
b0704e0d | 181 | changedRecords++; |
6a323f63 RA |
182 | } |
183 | } | |
7facf345 | 184 | if (foundRecord) { |
6a323f63 | 185 | di->backend->replaceRRSet(di->id, rrLabel, rrType, rrset); |
7facf345 RA |
186 | L<<Logger::Notice<<msgPrefix<<"Replacing record "<<rrLabel<<"|"<<rrType.getName()<<endl; |
187 | } | |
f1b59a55 | 188 | } |
6e42cac2 RA |
189 | |
190 | // ReplaceRRSet dumps our ordername and auth flag, so we need to correct it. | |
191 | // We can take the auth flag from the first RR in the set, as the name is different, so should the auth be. | |
192 | bool auth = rrset.front().auth; | |
dff32e09 | 193 | if(*haveNSEC3) { |
6e42cac2 | 194 | string hashed; |
dff32e09 | 195 | if(! *narrow) |
6e42cac2 RA |
196 | hashed=toLower(toBase32Hex(hashQNameWithSalt(ns3pr->d_iterations, ns3pr->d_salt, rrLabel))); |
197 | ||
198 | di->backend->updateDNSSECOrderAndAuthAbsolute(di->id, rrLabel, hashed, auth); | |
199 | if(!auth || rrType == QType::DS) { | |
200 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, rrLabel, "NS"); | |
201 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, rrLabel, "A"); | |
202 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, rrLabel, "AAAA"); | |
203 | } | |
204 | ||
205 | } else { // NSEC | |
206 | di->backend->updateDNSSECOrderAndAuth(di->id, di->zone, rrLabel, auth); | |
207 | if(!auth || rrType == QType::DS) { | |
208 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, rrLabel, "A"); | |
209 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, rrLabel, "AAAA"); | |
210 | } | |
211 | } | |
212 | ||
213 | } // if (foundRecord) | |
f1b59a55 | 214 | |
6a323f63 | 215 | // If we haven't found a record that matches, we must add it. |
f1b59a55 | 216 | if (! foundRecord) { |
6a323f63 RA |
217 | L<<Logger::Notice<<msgPrefix<<"Adding record "<<rrLabel<<"|"<<rrType.getName()<<endl; |
218 | delnonterm.insert(rrLabel); // always remove any ENT's in the place where we're going to add a record. | |
f1b59a55 RA |
219 | DNSResourceRecord newRec(*rr); |
220 | newRec.domain_id = di->id; | |
f48c7915 | 221 | newRec.auth = (rrLabel == di->zone || rrType.getCode() != QType::NS); |
f1b59a55 | 222 | di->backend->feedRecord(newRec); |
b0704e0d | 223 | changedRecords++; |
6a323f63 RA |
224 | |
225 | ||
226 | // because we added a record, we need to fix DNSSEC data. | |
227 | string shorter(rrLabel); | |
163cb726 | 228 | bool auth=newRec.auth; |
f1b59a55 | 229 | |
6a323f63 | 230 | if (shorter != di->zone && rrType != QType::DS) { |
a95f897d | 231 | while(chopOff(shorter)) { |
f1b59a55 RA |
232 | if (shorter == di->zone) |
233 | break; | |
f1b59a55 RA |
234 | bool foundShorter = false; |
235 | di->backend->lookup(QType(QType::ANY), shorter); | |
236 | while (di->backend->get(rec)) { | |
a95f897d | 237 | foundShorter = true; |
163cb726 | 238 | if (rec.qtype == QType::NS) // are we inserting below a delegate? |
f1b59a55 RA |
239 | auth=false; |
240 | } | |
163cb726 | 241 | if (!foundShorter) |
f1b59a55 | 242 | insnonterm.insert(shorter); |
a95f897d RA |
243 | else |
244 | break; // if we find a shorter record, we can stop searching | |
245 | } | |
f1b59a55 RA |
246 | } |
247 | ||
dff32e09 | 248 | if(*haveNSEC3) |
f1b59a55 RA |
249 | { |
250 | string hashed; | |
dff32e09 | 251 | if(! *narrow) |
6a323f63 | 252 | hashed=toLower(toBase32Hex(hashQNameWithSalt(ns3pr->d_iterations, ns3pr->d_salt, rrLabel))); |
f1b59a55 | 253 | |
6a323f63 RA |
254 | di->backend->updateDNSSECOrderAndAuthAbsolute(di->id, rrLabel, hashed, auth); |
255 | if(!auth || rrType == QType::DS) | |
f1b59a55 | 256 | { |
6a323f63 RA |
257 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, rrLabel, "NS"); |
258 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, rrLabel, "A"); | |
259 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, rrLabel, "AAAA"); | |
f1b59a55 RA |
260 | } |
261 | } | |
262 | else // NSEC | |
263 | { | |
6a323f63 RA |
264 | di->backend->updateDNSSECOrderAndAuth(di->id, di->zone, rrLabel, auth); |
265 | if(!auth || rrType == QType::DS) | |
f1b59a55 | 266 | { |
6a323f63 RA |
267 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, rrLabel, "A"); |
268 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, rrLabel, "AAAA"); | |
f1b59a55 RA |
269 | } |
270 | } | |
6a323f63 RA |
271 | |
272 | ||
f1b59a55 | 273 | // If we insert an NS, all the records below it become non auth - so, we're inserting a delegate. |
6a323f63 RA |
274 | // Auth can only be false when the rrLabel is not the zone |
275 | if (auth == false && rrType == QType::NS) { | |
276 | DLOG(L<<msgPrefix<<"Going to fix auth flags below "<<rrLabel<<endl); | |
163cb726 | 277 | insnonterm.clear(); // clean ENT's again, as it's a delegate and auth=false; |
f1b59a55 | 278 | vector<string> qnames; |
6a323f63 | 279 | di->backend->listSubZone(rrLabel, di->id); |
f1b59a55 RA |
280 | while(di->backend->get(rec)) { |
281 | if (rec.qtype.getCode() && rec.qtype.getCode() != QType::DS) // Skip ENT and DS records. | |
282 | qnames.push_back(rec.qname); | |
283 | } | |
284 | for(vector<string>::const_iterator qname=qnames.begin(); qname != qnames.end(); ++qname) { | |
dff32e09 | 285 | if(*haveNSEC3) { |
f1b59a55 | 286 | string hashed; |
dff32e09 | 287 | if(! *narrow) |
f1b59a55 RA |
288 | hashed=toLower(toBase32Hex(hashQNameWithSalt(ns3pr->d_iterations, ns3pr->d_salt, *qname))); |
289 | ||
290 | di->backend->updateDNSSECOrderAndAuthAbsolute(di->id, *qname, hashed, auth); | |
291 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, *qname, "NS"); | |
292 | } | |
293 | else // NSEC | |
294 | di->backend->updateDNSSECOrderAndAuth(di->id, di->zone, *qname, auth); | |
295 | ||
296 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, *qname, "AAAA"); | |
297 | di->backend->nullifyDNSSECOrderNameAndAuth(di->id, *qname, "A"); | |
298 | } | |
299 | } | |
f1b59a55 RA |
300 | } |
301 | } // rr->d_class == QClass::IN | |
302 | ||
6a323f63 RA |
303 | |
304 | // Delete records - section 3.4.2.3 and 3.4.2.4 with the exception of the 'always leave 1 NS rule' as that's handled by | |
305 | // the code that calls this performUpdate(). | |
306 | if ((rr->d_class == QClass::ANY || rr->d_class == QClass::NONE) && rrType != QType::SOA) { // never delete a SOA. | |
fdf983eb | 307 | DLOG(L<<msgPrefix<<"Deleting records: "<<rrLabel<<"; QClasse:"<<rr->d_class<<"; rrType: "<<rrType.getName()<<endl); |
dff32e09 RA |
308 | |
309 | if (rrType == QType::NSEC3PARAM) { | |
310 | L<<Logger::Notice<<msgPrefix<<"Removing NSEC3PARAM from zone, resetting ordernames."<<endl; | |
311 | if (rr->d_class == QClass::ANY) | |
312 | d_dk.unsetNSEC3PARAM(rrLabel); | |
313 | else if (rr->d_class == QClass::NONE) { | |
314 | NSEC3PARAMRecordContent nsec3rr(rr->d_content->getZoneRepresentation(), di->zone); | |
0bb68041 | 315 | if (ns3pr->getZoneRepresentation() == nsec3rr.getZoneRepresentation()) |
dff32e09 RA |
316 | d_dk.unsetNSEC3PARAM(rrLabel); |
317 | else | |
318 | return 0; | |
319 | } else | |
320 | return 0; | |
321 | ||
0bb68041 | 322 | *haveNSEC3 = d_dk.getNSEC3PARAM(di->zone, ns3pr, narrow); // still update, as other records in this update packet need to use it as well. |
dff32e09 RA |
323 | di->backend->list(di->zone, di->id); |
324 | vector<DNSResourceRecord> rrs; | |
325 | while (di->backend->get(rec)) { | |
326 | rrs.push_back(rec); | |
327 | } | |
328 | for (vector<DNSResourceRecord>::const_iterator i = rrs.begin(); i != rrs.end(); i++) { | |
329 | di->backend->updateDNSSECOrderAndAuth(di->id, di->zone, i->qname, i->auth); | |
330 | } | |
331 | return 1; | |
332 | } | |
333 | ||
334 | ||
6a323f63 RA |
335 | di->backend->lookup(rrType, rrLabel); |
336 | while(di->backend->get(rec)) { | |
337 | if (rr->d_class == QClass::ANY) { // 3.4.2.3 | |
338 | if (rec.qname == di->zone && (rec.qtype == QType::NS || rec.qtype == QType::SOA)) // Never delete all SOA and NS's | |
339 | rrset.push_back(rec); | |
340 | else | |
f1b59a55 RA |
341 | recordsToDelete.push_back(rec); |
342 | } | |
6a323f63 RA |
343 | if (rr->d_class == QClass::NONE) { // 3.4.2.4 |
344 | if (rrType == rec.qtype && rec.getZoneRepresentation() == rr->d_content->getZoneRepresentation()) | |
345 | recordsToDelete.push_back(rec); | |
346 | else | |
347 | rrset.push_back(rec); | |
348 | } | |
f1b59a55 | 349 | |
f1b59a55 | 350 | } |
6a323f63 | 351 | di->backend->replaceRRSet(di->id, rrLabel, rrType, rrset); |
7facf345 | 352 | L<<Logger::Notice<<msgPrefix<<"Deleting record "<<rrLabel<<"|"<<rrType.getName()<<endl; |
f1b59a55 | 353 | |
f1b59a55 | 354 | |
6a323f63 RA |
355 | if (recordsToDelete.size()) { |
356 | // If we remove an NS which is not at apex of the zone, we need to make everthing below it auth=true as those now are not delegated anymore. | |
357 | if (rrType == QType::NS && rrLabel != di->zone) { | |
a95f897d | 358 | vector<string> belowOldDelegate, nsRecs, updateAuthFlag; |
6a323f63 | 359 | di->backend->listSubZone(rrLabel, di->id); |
f1b59a55 | 360 | while (di->backend->get(rec)) { |
7a0a9f8a | 361 | if (rec.qtype.getCode()) // skip ENT records, they are always auth=false |
a95f897d RA |
362 | belowOldDelegate.push_back(rec.qname); |
363 | if (rec.qtype.getCode() == QType::NS && rec.qname != rrLabel) | |
364 | nsRecs.push_back(rec.qname); | |
f1b59a55 | 365 | } |
a95f897d RA |
366 | |
367 | for(vector<string>::const_iterator belowOldDel=belowOldDelegate.begin(); belowOldDel!= belowOldDelegate.end(); belowOldDel++) | |
368 | { | |
369 | bool isBelowDelegate = false; | |
370 | for(vector<string>::const_iterator ns=nsRecs.begin(); ns!= nsRecs.end(); belowOldDel++) { | |
371 | if (endsOn(*ns, *belowOldDel)) { | |
372 | isBelowDelegate=true; | |
373 | break; | |
374 | } | |
375 | } | |
376 | if (!isBelowDelegate) | |
377 | updateAuthFlag.push_back(*belowOldDel); | |
378 | } | |
379 | ||
380 | for (vector<string>::const_iterator changeRec=updateAuthFlag.begin(); changeRec!=updateAuthFlag.end(); ++changeRec) { | |
dff32e09 | 381 | if(*haveNSEC3) { |
f1b59a55 | 382 | string hashed; |
dff32e09 | 383 | if(! *narrow) |
f1b59a55 RA |
384 | hashed=toLower(toBase32Hex(hashQNameWithSalt(ns3pr->d_iterations, ns3pr->d_salt, *changeRec))); |
385 | ||
386 | di->backend->updateDNSSECOrderAndAuthAbsolute(di->id, *changeRec, hashed, true); | |
387 | } | |
388 | else // NSEC | |
389 | di->backend->updateDNSSECOrderAndAuth(di->id, di->zone, *changeRec, true); | |
390 | } | |
391 | } | |
f1b59a55 | 392 | |
6a323f63 RA |
393 | // Fix ENT records. |
394 | // We must check if we have a record below the current level and if we removed the 'last' record | |
395 | // on that level. If so, we must insert an ENT record. | |
fdf983eb | 396 | // We take extra care here to not 'include' the record that we just deleted. Some backends will still return it as they only reload on a commit. |
9353d244 | 397 | bool foundDeeper = false, foundOtherWithSameName = false; |
6a323f63 RA |
398 | di->backend->listSubZone(rrLabel, di->id); |
399 | while (di->backend->get(rec)) { | |
400 | if (rec.qname == rrLabel && !count(recordsToDelete.begin(), recordsToDelete.end(), rec)) | |
9353d244 | 401 | foundOtherWithSameName = true; |
7a0a9f8a | 402 | if (rec.qname != rrLabel && rec.qtype.getCode() != QType::NS) //Skip NS records, as this would be a delegate that we can ignore as this does not require us to create a ENT |
6a323f63 RA |
403 | foundDeeper = true; |
404 | } | |
f1b59a55 | 405 | |
9353d244 | 406 | if (foundDeeper && !foundOtherWithSameName) { |
6a323f63 | 407 | insnonterm.insert(rrLabel); |
9353d244 | 408 | } else if (!foundOtherWithSameName) { |
6a323f63 RA |
409 | // If we didn't have to insert an ENT, we might have deleted a record at very deep level |
410 | // and we must then clean up the ENT's above the deleted record. | |
411 | string shorter(rrLabel); | |
9353d244 RA |
412 | while (shorter != di->zone) { |
413 | chopOff(shorter); | |
414 | bool foundRealRR = false; | |
a95f897d | 415 | bool foundEnt = false; |
9353d244 RA |
416 | |
417 | // The reason for a listSubZone here is because might go up the tree and find the ENT of another branch | |
6a323f63 | 418 | // consider these non ENT-records: |
9353d244 RA |
419 | // b.c.d.e.test.com |
420 | // b.d.e.test.com | |
421 | // if we delete b.c.d.e.test.com, we go up to d.e.test.com and then find b.d.e.test.com because that's below d.e.test.com. | |
6a323f63 RA |
422 | // At that point we can stop deleting ENT's because the tree is in tact again. |
423 | di->backend->listSubZone(shorter, di->id); | |
a95f897d | 424 | |
9353d244 RA |
425 | while (di->backend->get(rec)) { |
426 | if (rec.qtype.getCode()) | |
427 | foundRealRR = true; | |
a95f897d RA |
428 | else |
429 | foundEnt = true; | |
9353d244 RA |
430 | } |
431 | if (!foundRealRR) | |
a95f897d RA |
432 | if (foundEnt) // only delete the ENT if we actually found one. |
433 | delnonterm.insert(shorter); | |
9353d244 RA |
434 | else |
435 | break; | |
436 | } | |
6a323f63 | 437 | } |
f1b59a55 | 438 | } |
6a323f63 | 439 | } |
f1b59a55 | 440 | |
6a323f63 RA |
441 | |
442 | //Insert and delete ENT's | |
443 | if (insnonterm.size() > 0 || delnonterm.size() > 0) { | |
444 | DLOG(L<<msgPrefix<<"Updating ENT records - "<<insnonterm.size()<<"|"<<delnonterm.size()<<endl); | |
445 | di->backend->updateEmptyNonTerminals(di->id, di->zone, insnonterm, delnonterm, false); | |
446 | for (set<string>::const_iterator i=insnonterm.begin(); i!=insnonterm.end(); i++) { | |
447 | string hashed; | |
dff32e09 | 448 | if(*haveNSEC3) |
6a323f63 | 449 | { |
f1b59a55 | 450 | string hashed; |
dff32e09 | 451 | if(! *narrow) |
6a323f63 RA |
452 | hashed=toLower(toBase32Hex(hashQNameWithSalt(ns3pr->d_iterations, ns3pr->d_salt, *i))); |
453 | di->backend->updateDNSSECOrderAndAuthAbsolute(di->id, *i, hashed, false); | |
f1b59a55 RA |
454 | } |
455 | } | |
456 | } | |
457 | ||
b0704e0d | 458 | return recordsToDelete.size() + changedRecords; |
f1b59a55 RA |
459 | } |
460 | ||
b3148887 | 461 | int PacketHandler::forwardPacket(const string &msgPrefix, DNSPacket *p, DomainInfo *di) { |
e174b255 RA |
462 | vector<string> forward; |
463 | B.getDomainMetadata(p->qdomain, "FORWARD-2136", forward); | |
464 | ||
465 | if (forward.size() == 0 && ! ::arg().mustDo("forward-2136")) { | |
d8293c79 RA |
466 | L<<Logger::Notice<<msgPrefix<<"Not configured to forward to master, returning Refused."<<endl; |
467 | return RCode::Refused; | |
e174b255 RA |
468 | } |
469 | ||
b3148887 | 470 | for(vector<string>::const_iterator master=di->masters.begin(); master != di->masters.end(); master++) { |
dff32e09 | 471 | L<<Logger::Notice<<msgPrefix<<"Forwarding packet to master "<<*master<<endl; |
b3148887 RA |
472 | ComboAddress remote; |
473 | try { | |
cc456024 | 474 | remote = ComboAddress(*master, 53); |
b3148887 RA |
475 | } |
476 | catch (...) { | |
477 | L<<Logger::Error<<msgPrefix<<"Failed to parse "<<*master<<" as valid remote."<<endl; | |
478 | continue; | |
479 | } | |
480 | ||
481 | ComboAddress local; | |
482 | if(remote.sin4.sin_family == AF_INET) | |
cc456024 | 483 | local = ComboAddress(::arg()["query-local-address"]); |
b3148887 | 484 | else if(!::arg()["query-local-address6"].empty()) |
cc456024 | 485 | local = ComboAddress(::arg()["query-local-address6"]); |
b3148887 | 486 | else |
cc456024 | 487 | local = ComboAddress("::"); |
b3148887 RA |
488 | int sock = makeQuerySocket(local, false); // create TCP socket. RFC2136 section 6.2 seems to be ok with this. |
489 | ||
490 | if( connect(sock, (struct sockaddr*)&remote, remote.getSocklen()) < 0 ) { | |
491 | L<<Logger::Error<<msgPrefix<<"Failed to connect to "<<remote.toStringWithPort()<<": "<<stringerror()<<endl; | |
492 | Utility::closesocket(sock); | |
493 | continue; | |
494 | } | |
495 | ||
496 | DNSPacket forwardPacket(*p); | |
497 | forwardPacket.setID(dns_random(0xffff)); | |
498 | forwardPacket.setRemote(&remote); | |
499 | uint16_t len=htons(forwardPacket.getString().length()); | |
500 | string buffer((const char*)&len, 2); | |
501 | buffer.append(forwardPacket.getString()); | |
502 | if(write(sock, buffer.c_str(), buffer.length()) < 0) { | |
503 | L<<Logger::Error<<msgPrefix<<"Unable to forward update message to "<<remote.toStringWithPort()<<", error:"<<stringerror()<<endl; | |
504 | continue; | |
505 | } | |
506 | ||
507 | int res = waitForData(sock, 10, 0); | |
508 | if (!res) { | |
cc456024 | 509 | L<<Logger::Error<<msgPrefix<<"Timeout waiting for reply from master at "<<remote.toStringWithPort()<<endl; |
b3148887 RA |
510 | Utility::closesocket(sock); |
511 | continue; | |
512 | } | |
513 | if (res < 0) { | |
514 | L<<Logger::Error<<msgPrefix<<"Error waiting for answer from master at "<<remote.toStringWithPort()<<", error:"<<stringerror()<<endl; | |
515 | Utility::closesocket(sock); | |
516 | continue; | |
517 | } | |
518 | ||
519 | char lenBuf[2]; | |
520 | int recvRes; | |
521 | recvRes = recv(sock, &lenBuf, sizeof(lenBuf), 0); | |
522 | if (recvRes < 0) { | |
523 | L<<Logger::Error<<msgPrefix<<"Could not receive data (length) from master at "<<remote.toStringWithPort()<<", error:"<<stringerror()<<endl; | |
524 | Utility::closesocket(sock); | |
525 | continue; | |
526 | } | |
527 | int packetLen = lenBuf[0]*256+lenBuf[1]; | |
528 | ||
529 | ||
530 | char buf[packetLen]; | |
531 | recvRes = recv(sock, &buf, packetLen, 0); | |
532 | if (recvRes < 0) { | |
533 | L<<Logger::Error<<msgPrefix<<"Could not receive data (dnspacket) from master at "<<remote.toStringWithPort()<<", error:"<<stringerror()<<endl; | |
534 | Utility::closesocket(sock); | |
535 | continue; | |
536 | } | |
537 | Utility::closesocket(sock); | |
538 | ||
539 | try { | |
b3148887 RA |
540 | MOADNSParser mdp(buf, recvRes); |
541 | L<<Logger::Info<<msgPrefix<<"Forward update message to "<<remote.toStringWithPort()<<", result was RCode "<<mdp.d_header.rcode<<endl; | |
542 | return mdp.d_header.rcode; | |
543 | } | |
544 | catch (...) { | |
545 | L<<Logger::Error<<msgPrefix<<"Failed to parse response packet from master at "<<remote.toStringWithPort()<<endl; | |
546 | continue; | |
547 | } | |
548 | } | |
549 | L<<Logger::Error<<msgPrefix<<"Failed to forward packet to master(s). Returning ServFail."<<endl; | |
550 | return RCode::ServFail; | |
551 | ||
552 | } | |
553 | ||
f1b59a55 | 554 | int PacketHandler::processUpdate(DNSPacket *p) { |
629e6103 | 555 | if (! ::arg().mustDo("experimental-rfc2136")) |
f1b59a55 RA |
556 | return RCode::Refused; |
557 | ||
7facf345 | 558 | string msgPrefix="UPDATE (" + itoa(p->d.id) + ") from " + p->getRemote() + " for " + p->qdomain + ": "; |
f1b59a55 RA |
559 | L<<Logger::Info<<msgPrefix<<"Processing started."<<endl; |
560 | ||
561 | // Check permissions - IP based | |
562 | vector<string> allowedRanges; | |
563 | B.getDomainMetadata(p->qdomain, "ALLOW-2136-FROM", allowedRanges); | |
564 | if (! ::arg()["allow-2136-from"].empty()) | |
565 | stringtok(allowedRanges, ::arg()["allow-2136-from"], ", \t" ); | |
566 | ||
567 | NetmaskGroup ng; | |
568 | for(vector<string>::const_iterator i=allowedRanges.begin(); i != allowedRanges.end(); i++) | |
569 | ng.addMask(*i); | |
570 | ||
571 | if ( ! ng.match(&p->d_remote)) { | |
572 | L<<Logger::Error<<msgPrefix<<"Remote not listed in allow-2136-from or domainmetadata. Sending REFUSED"<<endl; | |
573 | return RCode::Refused; | |
574 | } | |
575 | ||
576 | ||
577 | // Check permissions - TSIG based. | |
578 | vector<string> tsigKeys; | |
579 | B.getDomainMetadata(p->qdomain, "TSIG-ALLOW-2136", tsigKeys); | |
580 | if (tsigKeys.size() > 0) { | |
581 | bool validKey = false; | |
582 | ||
583 | TSIGRecordContent trc; | |
584 | string inputkey, message; | |
585 | if (! p->getTSIGDetails(&trc, &inputkey, &message)) { | |
586 | L<<Logger::Error<<msgPrefix<<"TSIG key required, but packet does not contain key. Sending REFUSED"<<endl; | |
587 | return RCode::Refused; | |
588 | } | |
589 | ||
590 | for(vector<string>::const_iterator key=tsigKeys.begin(); key != tsigKeys.end(); key++) { | |
591 | if (inputkey == *key) // because checkForCorrectTSIG has already been performed earlier on, if the names of the ky match with the domain given. THis is valid. | |
592 | validKey=true; | |
593 | } | |
594 | ||
595 | if (!validKey) { | |
596 | L<<Logger::Error<<msgPrefix<<"TSIG key ("<<inputkey<<") required, but no matching key found in domainmetadata, tried "<<tsigKeys.size()<<". Sending REFUSED"<<endl; | |
597 | return RCode::Refused; | |
598 | } | |
599 | } | |
600 | ||
601 | if (tsigKeys.size() == 0 && p->d_havetsig) | |
602 | L<<Logger::Warning<<msgPrefix<<"TSIG is provided, but domain is not secured with TSIG. Processing continues"<<endl; | |
603 | ||
604 | // RFC2136 uses the same DNS Header and Message as defined in RFC1035. | |
605 | // This means we can use the MOADNSParser to parse the incoming packet. The result is that we have some different | |
606 | // variable names during the use of our MOADNSParser. | |
607 | MOADNSParser mdp(p->getString()); | |
608 | if (mdp.d_header.qdcount != 1) { | |
609 | L<<Logger::Warning<<msgPrefix<<"Zone Count is not 1, sending FormErr"<<endl; | |
610 | return RCode::FormErr; | |
611 | } | |
612 | ||
613 | if (p->qtype.getCode() != QType::SOA) { // RFC2136 2.3 - ZTYPE must be SOA | |
614 | L<<Logger::Warning<<msgPrefix<<"Query ZTYPE is not SOA, sending FormErr"<<endl; | |
615 | return RCode::FormErr; | |
616 | } | |
617 | ||
618 | if (p->qclass != QClass::IN) { | |
619 | L<<Logger::Warning<<msgPrefix<<"Class is not IN, sending NotAuth"<<endl; | |
620 | return RCode::NotAuth; | |
621 | } | |
622 | ||
623 | DomainInfo di; | |
624 | di.backend=0; | |
625 | if(!B.getDomainInfo(p->qdomain, di) || !di.backend) { | |
626 | L<<Logger::Error<<msgPrefix<<"Can't determine backend for domain '"<<p->qdomain<<"' (or backend does not support RFC2136 operation)"<<endl; | |
627 | return RCode::NotAuth; | |
628 | } | |
629 | ||
b3148887 RA |
630 | if (di.kind == DomainInfo::Slave) |
631 | return forwardPacket(msgPrefix, p, &di); | |
f1b59a55 RA |
632 | |
633 | // Check if all the records provided are within the zone | |
634 | for(MOADNSParser::answers_t::const_iterator i=mdp.d_answers.begin(); i != mdp.d_answers.end(); ++i) { | |
635 | const DNSRecord *rr = &i->first; | |
636 | // Skip this check for other field types (like the TSIG - which is in the additional section) | |
dff32e09 | 637 | // For a TSIG, the label is the dnskey, so it does not pass the endOn validation. |
f1b59a55 RA |
638 | if (! (rr->d_place == DNSRecord::Answer || rr->d_place == DNSRecord::Nameserver)) |
639 | continue; | |
640 | ||
641 | string label = stripDot(rr->d_label); | |
642 | ||
643 | if (!endsOn(label, di.zone)) { | |
644 | L<<Logger::Error<<msgPrefix<<"Received update/record out of zone, sending NotZone."<<endl; | |
645 | return RCode::NotZone; | |
646 | } | |
647 | } | |
648 | ||
dff32e09 RA |
649 | |
650 | Lock l(&s_rfc2136lock); //TODO: i think this lock can be per zone, not for everything | |
f1b59a55 | 651 | L<<Logger::Info<<msgPrefix<<"starting transaction."<<endl; |
dff32e09 | 652 | if (!di.backend->startTransaction(p->qdomain, -1)) { // Not giving the domain_id means that we do not delete the existing records. |
f1b59a55 RA |
653 | L<<Logger::Error<<msgPrefix<<"Backend for domain "<<p->qdomain<<" does not support transaction. Can't do Update packet."<<endl; |
654 | return RCode::NotImp; | |
655 | } | |
656 | ||
657 | // 3.2.1 and 3.2.2 - Prerequisite check | |
658 | for(MOADNSParser::answers_t::const_iterator i=mdp.d_answers.begin(); i != mdp.d_answers.end(); ++i) { | |
659 | const DNSRecord *rr = &i->first; | |
660 | if (rr->d_place == DNSRecord::Answer) { | |
661 | int res = checkUpdatePrerequisites(rr, &di); | |
662 | if (res>0) { | |
663 | L<<Logger::Error<<msgPrefix<<"Failed PreRequisites check, returning "<<res<<endl; | |
664 | di.backend->abortTransaction(); | |
665 | return res; | |
666 | } | |
667 | } | |
668 | } | |
669 | ||
670 | // 3.2.3 - Prerequisite check - this is outside of updatePrequisitesCheck because we check an RRSet and not the RR. | |
671 | typedef pair<string, QType> rrSetKey_t; | |
672 | typedef vector<DNSResourceRecord> rrVector_t; | |
673 | typedef std::map<rrSetKey_t, rrVector_t> RRsetMap_t; | |
674 | RRsetMap_t preReqRRsets; | |
675 | for(MOADNSParser::answers_t::const_iterator i=mdp.d_answers.begin(); i != mdp.d_answers.end(); ++i) { | |
676 | const DNSRecord *rr = &i->first; | |
677 | if (rr->d_place == DNSRecord::Answer) { | |
678 | // Last line of 3.2.3 | |
679 | if (rr->d_class != QClass::IN && rr->d_class != QClass::NONE && rr->d_class != QClass::ANY) | |
680 | return RCode::FormErr; | |
681 | ||
682 | if (rr->d_class == QClass::IN) { | |
683 | rrSetKey_t key = make_pair(stripDot(rr->d_label), rr->d_type); | |
684 | rrVector_t *vec = &preReqRRsets[key]; | |
685 | vec->push_back(DNSResourceRecord(*rr)); | |
686 | } | |
687 | } | |
688 | } | |
689 | ||
690 | if (preReqRRsets.size() > 0) { | |
691 | RRsetMap_t zoneRRsets; | |
692 | for (RRsetMap_t::iterator preRRSet = preReqRRsets.begin(); preRRSet != preReqRRsets.end(); ++preRRSet) { | |
693 | rrSetKey_t rrSet=preRRSet->first; | |
694 | rrVector_t *vec = &preRRSet->second; | |
695 | ||
696 | DNSResourceRecord rec; | |
697 | di.backend->lookup(QType(QType::ANY), rrSet.first); | |
698 | uint16_t foundRR=0, matchRR=0; | |
699 | while (di.backend->get(rec)) { | |
700 | if (rec.qtype == rrSet.second) { | |
701 | foundRR++; | |
702 | for(rrVector_t::iterator rrItem=vec->begin(); rrItem != vec->end(); ++rrItem) { | |
703 | rrItem->ttl = rec.ttl; // The compare one line below also compares TTL, so we make them equal because TTL is not user within prerequisite checks. | |
704 | if (*rrItem == rec) | |
705 | matchRR++; | |
706 | } | |
707 | } | |
708 | } | |
709 | if (matchRR != foundRR || foundRR != vec->size()) { | |
710 | L<<Logger::Error<<msgPrefix<<"Failed PreRequisites check, returning NXRRSet"<<endl; | |
711 | di.backend->abortTransaction(); | |
712 | return RCode::NXRRSet; | |
713 | } | |
714 | } | |
715 | } | |
716 | ||
717 | ||
718 | ||
0bb68041 | 719 | // 3.4 - Prescan & Add/Update/Delete records - is all done within a try block. |
f1b59a55 | 720 | try { |
0bb68041 | 721 | uint16_t changedRecords = 0; |
f1b59a55 RA |
722 | // 3.4.1 - Prescan section |
723 | for(MOADNSParser::answers_t::const_iterator i=mdp.d_answers.begin(); i != mdp.d_answers.end(); ++i) { | |
724 | const DNSRecord *rr = &i->first; | |
725 | if (rr->d_place == DNSRecord::Nameserver) { | |
726 | int res = checkUpdatePrescan(rr); | |
727 | if (res>0) { | |
728 | L<<Logger::Error<<msgPrefix<<"Failed prescan check, returning "<<res<<endl; | |
729 | di.backend->abortTransaction(); | |
730 | return res; | |
731 | } | |
732 | } | |
733 | } | |
734 | ||
735 | bool updatedSerial=false; | |
736 | NSEC3PARAMRecordContent ns3pr; | |
dff32e09 | 737 | bool narrow=false; |
f1b59a55 | 738 | bool haveNSEC3 = d_dk.getNSEC3PARAM(di.zone, &ns3pr, &narrow); |
dff32e09 | 739 | bool isPresigned = d_dk.isPresigned(di.zone); |
f1b59a55 | 740 | |
f1b59a55 RA |
741 | // 3.4.2 - Perform the updates. |
742 | // There's a special condition where deleting the last NS record at zone apex is never deleted (3.4.2.4) | |
743 | // This means we must do it outside the normal performUpdate() because that focusses only on a seperate RR. | |
744 | vector<const DNSRecord *> nsRRtoDelete; | |
745 | for(MOADNSParser::answers_t::const_iterator i=mdp.d_answers.begin(); i != mdp.d_answers.end(); ++i) { | |
746 | const DNSRecord *rr = &i->first; | |
747 | if (rr->d_place == DNSRecord::Nameserver) { | |
748 | if (rr->d_class == QClass::NONE && rr->d_type == QType::NS && stripDot(rr->d_label) == di.zone) | |
749 | nsRRtoDelete.push_back(rr); | |
750 | else | |
dff32e09 | 751 | changedRecords += performUpdate(msgPrefix, rr, &di, isPresigned, &narrow, &haveNSEC3, &ns3pr, &updatedSerial); |
f1b59a55 RA |
752 | } |
753 | } | |
754 | if (nsRRtoDelete.size()) { | |
755 | vector<DNSResourceRecord> nsRRInZone; | |
756 | DNSResourceRecord rec; | |
757 | di.backend->lookup(QType(QType::NS), di.zone); | |
758 | while (di.backend->get(rec)) { | |
759 | nsRRInZone.push_back(rec); | |
760 | } | |
761 | if (nsRRInZone.size() > nsRRtoDelete.size()) { // only delete if the NS's we delete are less then what we have in the zone (3.4.2.4) | |
762 | for (vector<DNSResourceRecord>::iterator inZone=nsRRInZone.begin(); inZone != nsRRInZone.end(); inZone++) { | |
763 | for (vector<const DNSRecord *>::iterator rr=nsRRtoDelete.begin(); rr != nsRRtoDelete.end(); rr++) { | |
764 | if (inZone->getZoneRepresentation() == (*rr)->d_content->getZoneRepresentation()) | |
dff32e09 | 765 | changedRecords += performUpdate(msgPrefix, *rr, &di, isPresigned, &narrow, &haveNSEC3, &ns3pr, &updatedSerial); |
f1b59a55 RA |
766 | } |
767 | } | |
768 | } | |
769 | } | |
770 | ||
f1b59a55 | 771 | // Section 3.6 - Update the SOA serial - outside of performUpdate because we do a SOA update for the complete update message |
890c7124 | 772 | if (changedRecords > 0 && !updatedSerial) { |
166ac305 | 773 | increaseSerial(msgPrefix, &di, haveNSEC3, narrow, &ns3pr); |
890c7124 RA |
774 | changedRecords++; |
775 | } | |
776 | ||
0bb68041 RA |
777 | if (!di.backend->commitTransaction()) { |
778 | L<<Logger::Error<<msgPrefix<<"Failed to commit updates!"<<endl; | |
779 | return RCode::ServFail; | |
780 | } | |
781 | ||
276a5ce7 | 782 | S.deposit("rfc2136-changes", changedRecords); |
7facf345 | 783 | |
0bb68041 RA |
784 | // Purge the records! |
785 | string zone(di.zone); | |
786 | zone.append("$"); | |
787 | PC.purge(zone); | |
788 | ||
789 | L<<Logger::Info<<msgPrefix<<"Update completed, "<<changedRecords<<" changed records commited."<<endl; | |
790 | return RCode::NoError; //rfc 2136 3.4.2.5 | |
791 | } | |
792 | catch (SSqlException &e) { | |
793 | L<<Logger::Error<<msgPrefix<<"Caught SSqlException: "<<e.txtReason()<<"; Sending ServFail!"<<endl; | |
794 | di.backend->abortTransaction(); | |
795 | return RCode::ServFail; | |
f1b59a55 | 796 | } |
ee9ef8f2 RA |
797 | catch (DBException &e) { |
798 | L<<Logger::Error<<msgPrefix<<"Caught DBException: "<<e.reason<<"; Sending ServFail!"<<endl; | |
799 | di.backend->abortTransaction(); | |
800 | return RCode::ServFail; | |
801 | } | |
f1b59a55 RA |
802 | catch (AhuException &e) { |
803 | L<<Logger::Error<<msgPrefix<<"Caught AhuException: "<<e.reason<<"; Sending ServFail!"<<endl; | |
804 | di.backend->abortTransaction(); | |
805 | return RCode::ServFail; | |
806 | } | |
807 | catch (...) { | |
808 | L<<Logger::Error<<msgPrefix<<"Caught unknown exception when performing update. Sending ServFail!"<<endl; | |
809 | di.backend->abortTransaction(); | |
810 | return RCode::ServFail; | |
811 | } | |
f1b59a55 RA |
812 | } |
813 | ||
166ac305 | 814 | void PacketHandler::increaseSerial(const string &msgPrefix, const DomainInfo *di, bool haveNSEC3, bool narrow, const NSEC3PARAMRecordContent *ns3pr) { |
f1b59a55 | 815 | DNSResourceRecord rec, newRec; |
166ac305 | 816 | di->backend->lookup(QType(QType::SOA), di->zone); |
f1b59a55 | 817 | bool foundSOA=false; |
166ac305 | 818 | while (di->backend->get(rec)) { |
f1b59a55 RA |
819 | newRec = rec; |
820 | foundSOA=true; | |
821 | } | |
822 | if (!foundSOA) { | |
823 | throw AhuException("SOA-Serial update failed because there was no SOA. Wowie."); | |
824 | } | |
825 | SOAData soa2Update; | |
826 | fillSOAData(rec.content, soa2Update); | |
890c7124 | 827 | int oldSerial = soa2Update.serial; |
f1b59a55 RA |
828 | |
829 | vector<string> soaEdit2136Setting; | |
166ac305 | 830 | B.getDomainMetadata(di->zone, "SOA-EDIT-2136", soaEdit2136Setting); |
f1b59a55 RA |
831 | string soaEdit2136 = "DEFAULT"; |
832 | string soaEdit; | |
833 | if (!soaEdit2136Setting.empty()) { | |
834 | soaEdit2136 = soaEdit2136Setting[0]; | |
835 | if (pdns_iequals(soaEdit2136, "SOA-EDIT") || pdns_iequals(soaEdit2136,"SOA-EDIT-INCREASE") ){ | |
836 | vector<string> soaEditSetting; | |
166ac305 | 837 | B.getDomainMetadata(di->zone, "SOA-EDIT", soaEditSetting); |
f1b59a55 RA |
838 | if (soaEditSetting.empty()) { |
839 | L<<Logger::Error<<msgPrefix<<"Using "<<soaEdit2136<<" for SOA-EDIT-2136 increase on RFC2136, but SOA-EDIT is not set for domain. Using DEFAULT for SOA-EDIT-2136"<<endl; | |
840 | soaEdit2136 = "DEFAULT"; | |
841 | } else | |
842 | soaEdit = soaEditSetting[0]; | |
843 | } | |
844 | } | |
845 | ||
846 | ||
847 | if (pdns_iequals(soaEdit2136, "INCREASE")) | |
848 | soa2Update.serial++; | |
849 | else if (pdns_iequals(soaEdit2136, "SOA-EDIT-INCREASE")) { | |
850 | uint32_t newSer = calculateEditSOA(soa2Update, soaEdit); | |
851 | if (newSer <= soa2Update.serial) | |
852 | soa2Update.serial++; | |
853 | else | |
854 | soa2Update.serial = newSer; | |
855 | } else if (pdns_iequals(soaEdit2136, "SOA-EDIT")) | |
856 | soa2Update.serial = calculateEditSOA(soa2Update, soaEdit); | |
857 | else if (pdns_iequals(soaEdit2136, "EPOCH")) | |
858 | soa2Update.serial = time(0); | |
859 | else { | |
860 | time_t now = time(0); | |
861 | struct tm tm; | |
862 | localtime_r(&now, &tm); | |
863 | boost::format fmt("%04d%02d%02d%02d"); | |
864 | string newserdate=(fmt % (tm.tm_year+1900) % (tm.tm_mon +1 )% tm.tm_mday % 1).str(); | |
865 | uint32_t newser = atol(newserdate.c_str()); | |
866 | if (newser <= soa2Update.serial) | |
867 | soa2Update.serial++; | |
868 | else | |
869 | soa2Update.serial = newser; | |
870 | } | |
871 | ||
872 | ||
873 | newRec.content = serializeSOAData(soa2Update); | |
3e0ab216 RA |
874 | vector<DNSResourceRecord> rrset; |
875 | rrset.push_back(newRec); | |
166ac305 | 876 | di->backend->replaceRRSet(di->id, newRec.qname, newRec.qtype, rrset); |
dff32e09 | 877 | L<<Logger::Notice<<msgPrefix<<"Increasing SOA serial ("<<oldSerial<<" -> "<<soa2Update.serial<<")"<<endl; |
166ac305 RA |
878 | |
879 | //Correct ordername + auth flag | |
c1c3e5a4 RA |
880 | if (haveNSEC3 && narrow) |
881 | di->backend->nullifyDNSSECOrderNameAndUpdateAuth(di->id, newRec.qname, true); | |
882 | else if (haveNSEC3) { | |
166ac305 | 883 | string hashed; |
c1c3e5a4 RA |
884 | if (!narrow) |
885 | hashed = toLower(toBase32Hex(hashQNameWithSalt(ns3pr->d_iterations, ns3pr->d_salt, newRec.qname))); | |
886 | ||
166ac305 RA |
887 | di->backend->updateDNSSECOrderAndAuthAbsolute(di->id, newRec.qname, hashed, true); |
888 | } | |
889 | else // NSEC | |
890 | di->backend->updateDNSSECOrderAndAuth(di->id, di->zone, newRec.qname, true); | |
166ac305 | 891 | } |