From: Christian Hofstaedtler Date: Wed, 13 Apr 2016 20:30:31 +0000 (+0200) Subject: API: extract storeChangedPTRs out of patchZone X-Git-Tag: dnsdist-1.0.0~14^2^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=995473c863a491415a8b7120351f70e4bc091808;p=pdns API: extract storeChangedPTRs out of patchZone --- diff --git a/pdns/ws-auth.cc b/pdns/ws-auth.cc index 94d302a24..3b42c4b0e 100644 --- a/pdns/ws-auth.cc +++ b/pdns/ws-auth.cc @@ -48,8 +48,10 @@ using json11::Json; extern StatBag S; +extern PacketCache PC; static void patchZone(HttpRequest* req, HttpResponse* resp); +static void storeChangedPTRs(UeberBackend& B, vector& new_ptrs); static void makePtr(const DNSResourceRecord& rr, DNSResourceRecord* ptr); AuthWebServer::AuthWebServer() @@ -924,6 +926,50 @@ static void makePtr(const DNSResourceRecord& rr, DNSResourceRecord* ptr) { ptr->content = rr.qname.toString(); } +static void storeChangedPTRs(UeberBackend& B, vector& new_ptrs) { + for(const DNSResourceRecord& rr : new_ptrs) { + DNSPacket fakePacket; + SOAData sd; + sd.db = (DNSBackend *)-1; // getAuth() cache bypass + fakePacket.qtype = QType::PTR; + + if (!B.getAuth(&fakePacket, &sd, rr.qname)) + throw ApiException("Could not find domain for PTR '"+rr.qname.toString()+"' requested for '"+rr.content+"' (while saving)"); + + string soa_edit_api_kind; + string soa_edit_kind; + bool soa_changed = false; + DNSResourceRecord soarr; + sd.db->getDomainMetadataOne(sd.qname, "SOA-EDIT-API", soa_edit_api_kind); + sd.db->getDomainMetadataOne(sd.qname, "SOA-EDIT", soa_edit_kind); + if (!soa_edit_api_kind.empty()) { + soarr.qname = sd.qname; + soarr.content = serializeSOAData(sd); + soarr.qtype = "SOA"; + soarr.domain_id = sd.domain_id; + soarr.auth = 1; + soarr.ttl = sd.ttl; + increaseSOARecord(soarr, soa_edit_api_kind, soa_edit_kind); + // fixup dots after serializeSOAData/increaseSOARecord + soarr.content = makeBackendRecordContent(soarr.qtype, soarr.content); + soa_changed = true; + } + + sd.db->startTransaction(sd.qname); + if (!sd.db->replaceRRSet(sd.domain_id, rr.qname, rr.qtype, vector(1, rr))) { + sd.db->abortTransaction(); + throw ApiException("PTR-Hosting backend for "+rr.qname.toString()+"/"+rr.qtype.getName()+" does not support editing records."); + } + + if (soa_changed) { + sd.db->replaceRRSet(sd.domain_id, soarr.qname, soarr.qtype, vector(1, soarr)); + } + + sd.db->commitTransaction(); + PC.purgeExact(rr.qname); + } +} + static void patchZone(HttpRequest* req, HttpResponse* resp) { UeberBackend B; DomainInfo di; @@ -1047,51 +1093,10 @@ static void patchZone(HttpRequest* req, HttpResponse* resp) { } di.backend->commitTransaction(); - extern PacketCache PC; PC.purgeExact(zonename); // now the PTRs - for(const DNSResourceRecord& rr : new_ptrs) { - DNSPacket fakePacket; - SOAData sd; - sd.db = (DNSBackend *)-1; // getAuth() cache bypass - fakePacket.qtype = QType::PTR; - - if (!B.getAuth(&fakePacket, &sd, rr.qname)) - throw ApiException("Could not find domain for PTR '"+rr.qname.toString()+"' requested for '"+rr.content+"' (while saving)"); - - string soa_edit_api_kind; - string soa_edit_kind; - bool soa_changed = false; - DNSResourceRecord soarr; - sd.db->getDomainMetadataOne(sd.qname, "SOA-EDIT-API", soa_edit_api_kind); - sd.db->getDomainMetadataOne(sd.qname, "SOA-EDIT", soa_edit_kind); - if (!soa_edit_api_kind.empty()) { - soarr.qname = sd.qname; - soarr.content = serializeSOAData(sd); - soarr.qtype = "SOA"; - soarr.domain_id = sd.domain_id; - soarr.auth = 1; - soarr.ttl = sd.ttl; - increaseSOARecord(soarr, soa_edit_api_kind, soa_edit_kind); - // fixup dots after serializeSOAData/increaseSOARecord - soarr.content = makeBackendRecordContent(soarr.qtype, soarr.content); - soa_changed = true; - } - - sd.db->startTransaction(sd.qname); - if (!sd.db->replaceRRSet(sd.domain_id, rr.qname, rr.qtype, vector(1, rr))) { - sd.db->abortTransaction(); - throw ApiException("PTR-Hosting backend for "+rr.qname.toString()+"/"+rr.qtype.getName()+" does not support editing records."); - } - - if (soa_changed) { - sd.db->replaceRRSet(sd.domain_id, soarr.qname, soarr.qtype, vector(1, soarr)); - } - - sd.db->commitTransaction(); - PC.purgeExact(rr.qname); - } + storeChangedPTRs(B, new_ptrs); // success fillZone(zonename, resp); @@ -1183,7 +1188,6 @@ void apiServerCacheFlush(HttpRequest* req, HttpResponse* resp) { DNSName canon = apiNameToDNSName(req->getvars["domain"]); - extern PacketCache PC; int count = PC.purgeExact(canon); resp->setBody(Json::object { { "count", count },