]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/ixplore.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
25 #include "arguments.hh"
29 #include "dnsrecords.hh"
32 #include "dnssecinfra.hh"
34 #include "dns_random.hh"
35 #include "gss_context.hh"
36 #include <boost/multi_index_container.hpp>
37 #include "resolver.hh"
40 #include "ixfrutils.hh"
45 static ArgvMap theArg
;
50 cerr
<<"Syntax: ixplore diff ZONE BEFORE_FILE AFTER_FILE"<<endl
;
51 cerr
<<"Syntax: ixplore track IP-ADDRESS PORT ZONE DIRECTORY [TSIGKEY TSIGALGO TSIGSECRET]"<<endl
;
54 int main(int argc
, char** argv
) {
56 for(int n
=1 ; n
< argc
; ++n
) {
57 if ((string
) argv
[n
] == "--help") {
62 if ((string
) argv
[n
] == "--version") {
63 cerr
<<"ixplore "<<VERSION
<<endl
;
70 if(argc
< 5 || (command
=argv
[1], (command
!="diff" && command
!="track"))) {
75 records_t before
, after
;
76 DNSName
zone(argv
[2]);
77 cout
<<"Loading before from "<<argv
[3]<<endl
;
78 loadZoneFromDisk(before
, argv
[3], zone
);
79 cout
<<"Parsed "<<before
.size()<<" records"<<endl
;
80 cout
<<"Zone was complete (SOA at end)"<<endl
;
81 cout
<<"Loading after from "<<argv
[4]<<endl
;
82 loadZoneFromDisk(after
, argv
[4], zone
);
83 cout
<<"Parsed "<<after
.size()<<" records"<<endl
;
84 cout
<<"Zone was complete (SOA at end)"<<endl
;
86 vector
<DNSRecord
> diff
;
88 set_difference(before
.cbegin(), before
.cend(), after
.cbegin(), after
.cend(), back_inserter(diff
), before
.value_comp());
89 for(const auto& d
: diff
) {
90 cout
<<'-'<< (d
.d_name
+zone
) <<" IN "<<DNSRecordContent::NumberToType(d
.d_type
)<<" "<<d
.d_content
->getZoneRepresentation()<<endl
;
93 set_difference(after
.cbegin(), after
.cend(), before
.cbegin(), before
.cend(), back_inserter(diff
), before
.value_comp());
94 for(const auto& d
: diff
) {
95 cout
<<'+'<< (d
.d_name
+zone
) <<" IN "<<DNSRecordContent::NumberToType(d
.d_type
)<<" "<<d
.d_content
->getZoneRepresentation()<<endl
;
100 // must be "track" then
103 in directory/zone-name we leave files with their name the serial number
104 at startup, retrieve current SOA SERIAL for domain from master server
106 compare with what the best is we have in our directory, IXFR from that.
107 Store result in memory, read that best zone in memory, apply deltas, write it out.
109 Next up, loop this every REFRESH seconds */
111 DNSName
zone(argv
[4]);
112 ComboAddress
master(argv
[2], atoi(argv
[3]));
113 string
directory(argv
[5]);
116 uint32_t ourSerial
= getSerialFromDir(directory
);
118 cout
<<"Loading zone, our highest available serial is "<< ourSerial
<<endl
;
122 tt
.name
=DNSName(toLower(argv
[6]));
124 tt
.algo
=DNSName(toLower(argv
[7]));
127 if(B64Decode(argv
[8], tt
.secret
) < 0) {
128 cerr
<<"Could not decode tsig secret!"<<endl
;
135 throw std::runtime_error("There is no local zone available");
136 string fname
=directory
+"/"+std::to_string(ourSerial
);
137 cout
<<"Loading serial number "<<ourSerial
<<" from file "<<fname
<<endl
;
138 loadZoneFromDisk(records
, fname
, zone
);
139 cout
<<"Parsed "<<records
.size()<<" records"<<endl
;
140 cout
<<"Zone was complete (SOA at end)"<<endl
;
142 catch(std::exception
& e
) {
143 cout
<<"Could not load zone from disk: "<<e
.what()<<endl
;
144 cout
<<"Retrieving latest from master "<<master
.toStringWithPort()<<endl
;
145 ComboAddress local
= master
.sin4
.sin_family
== AF_INET
? ComboAddress("0.0.0.0") : ComboAddress("::");
146 AXFRRetriever
axfr(master
, zone
, tt
, &local
);
147 unsigned int nrecords
=0;
149 vector
<DNSRecord
> chunk
;
150 char wheel
[]="|/-\\";
153 while(axfr
.getChunk(nop
, &chunk
)) {
154 for(auto& dr
: chunk
) {
155 if(dr
.d_type
== QType::TSIG
)
157 dr
.d_name
.makeUsRelative(zone
);
162 if(last
!= time(0)) {
163 cout
<< '\r' << wheel
[count
% (sizeof(wheel
)-1)] << ' ' <<nrecords
;
169 cout
<<"\rDone, got "<<nrecords
<<" "<<endl
;
170 cout
<<"Writing to disk.."<<endl
;
171 writeZoneToDisk(records
, zone
, directory
);
176 ourSerial
= getSerialFromRecords(records
, ourSoa
);
178 cout
<<"Checking for update, our serial number is "<<ourSerial
<<".. ";
180 shared_ptr
<SOARecordContent
> sr
;
181 uint32_t serial
= getSerialFromMaster(master
, zone
, sr
, tt
);
182 if(ourSerial
== serial
) {
183 cout
<<"still up to date, their serial is "<<serial
<<", sleeping "<<sr
->d_st
.refresh
<<" seconds"<<endl
;
184 sleep(sr
->d_st
.refresh
);
188 cout
<<"got new serial: "<<serial
<<", initiating IXFR!"<<endl
;
189 auto deltas
= getIXFRDeltas(master
, zone
, ourSoa
, tt
);
190 cout
<<"Got "<<deltas
.size()<<" deltas, applying.."<<endl
;
192 for(const auto& delta
: deltas
) {
194 const auto& remove
= delta
.first
;
195 const auto& add
= delta
.second
;
197 ourSerial
=getSerialFromRecords(records
, ourSoa
);
198 uint32_t newserial
=0;
199 for(const auto& rr
: add
) {
200 if(rr
.d_type
== QType::SOA
) {
201 newserial
=std::dynamic_pointer_cast
<SOARecordContent
>(rr
.d_content
)->d_st
.serial
;
205 cout
<<"This delta ("<<ourSerial
<<" - "<<newserial
<<") has "<<remove
.size()<<" removals, "<<add
.size()<<" additions"<<endl
;
206 ofstream
report(directory
+"/delta."+std::to_string(ourSerial
)+"-"+std::to_string(newserial
));
208 cout
<<"This delta is a whole new zone"<<endl
;
209 report
<<"- everything, whole new zone update follow"<<endl
;
215 for(const auto& rr
: remove
) {
216 report
<<'-'<< (rr
.d_name
+zone
) <<" IN "<<DNSRecordContent::NumberToType(rr
.d_type
)<<" "<<rr
.d_content
->getZoneRepresentation()<<endl
;
217 auto range
= records
.equal_range(tie(rr
.d_name
, rr
.d_type
, rr
.d_class
, rr
.d_content
));
218 if(range
.first
== range
.second
) {
219 cout
<<endl
<<" !! Could not find record "<<rr
.d_name
<<" to remove!!"<<endl
;
223 records
.erase(range
.first
, range
.second
);
226 for(const auto& rr
: add
) {
227 report
<<'+'<< (rr
.d_name
+zone
) <<" IN "<<DNSRecordContent::NumberToType(rr
.d_type
)<<" "<<rr
.d_content
->getZoneRepresentation()<<endl
;
231 cerr
<<"Had error condition, stopping.."<<endl
;
236 cout
<<"Writing zone to disk.. "; cout
.flush();
237 writeZoneToDisk(records
, zone
, directory
);
241 catch(PDNSException
&e2
) {
242 cerr
<<"Fatal: "<<e2
.reason
<<endl
;
244 catch(std::exception
&e
)
246 cerr
<<"Fatal: "<<e
.what()<<endl
;
250 cerr
<<"Any other exception"<<endl
;