5 #include "dnsseckeeper.hh"
6 #include "dnssecinfra.hh"
11 #include <boost/program_options.hpp>
12 #include <boost/assign/std/vector.hpp>
13 #include <boost/assign/list_of.hpp>
14 #include "tsigutils.hh"
15 #include "dnsbackend.hh"
16 #include "ueberbackend.hh"
17 #include "arguments.hh"
18 #include "auth-packetcache.hh"
19 #include "auth-querycache.hh"
20 #include "zoneparser-tng.hh"
21 #include "signingpipe.hh"
22 #include "dns_random.hh"
24 #include <termios.h> //termios, TCSANOW, ECHO, ICANON
25 #include "opensslsigners.hh"
30 #include "ssqlite3.hh"
31 #include "bind-dnssec.schema.sqlite3.sql.h"
38 namespace po
= boost::program_options
;
39 po::variables_map g_vm
;
41 string s_programname
="pdns";
53 string
humanTime(time_t t
)
58 strftime(ret
, sizeof(ret
)-1, "%c", &tm
); // %h:%M %Y-%m-%d
62 void loadMainConfig(const std::string
& configdir
)
64 ::arg().set("config-dir","Location of configuration directory (pdns.conf)")=configdir
;
65 ::arg().set("default-ttl","Seconds a result is valid if not set otherwise")="3600";
66 ::arg().set("launch","Which backends to launch");
67 ::arg().set("dnssec","if we should do dnssec")="true";
68 ::arg().set("config-name","Name of this virtual configuration - will rename the binary image")=g_vm
["config-name"].as
<string
>();
69 ::arg().setCmd("help","Provide a helpful message");
70 ::arg().set("load-modules","Load this module - supply absolute or relative path")="";
71 //::arg().laxParse(argc,argv);
73 if(::arg().mustDo("help")) {
74 cout
<<"syntax:"<<endl
<<endl
;
75 cout
<<::arg().helpstring(::arg()["help"])<<endl
;
79 if(::arg()["config-name"]!="")
80 s_programname
+="-"+::arg()["config-name"];
82 string configname
=::arg()["config-dir"]+"/"+s_programname
+".conf";
83 cleanSlashes(configname
);
85 ::arg().set("resolver","Use this resolver for ALIAS and the internal stub resolver")="no";
86 ::arg().set("default-ksk-algorithm","Default KSK algorithm")="ecdsa256";
87 ::arg().set("default-ksk-size","Default KSK size (0 means default)")="0";
88 ::arg().set("default-zsk-algorithm","Default ZSK algorithm")="";
89 ::arg().set("default-zsk-size","Default ZSK size (0 means default)")="0";
90 ::arg().set("default-soa-edit","Default SOA-EDIT value")="";
91 ::arg().set("default-soa-edit-signed","Default SOA-EDIT value for signed zones")="";
92 ::arg().set("max-ent-entries", "Maximum number of empty non-terminals in a zone")="100000";
93 ::arg().set("module-dir","Default directory for modules")=PKGLIBDIR
;
94 ::arg().set("entropy-source", "If set, read entropy from this file")="/dev/urandom";
95 ::arg().setSwitch("query-logging","Hint backends that queries should be logged")="no";
96 ::arg().set("loglevel","Amount of logging. Higher is more.")="3";
97 ::arg().setSwitch("direct-dnskey","Fetch DNSKEY, CDS and CDNSKEY RRs from backend during DNSKEY or CDS/CDNSKEY synthesis")="no";
98 ::arg().set("max-nsec3-iterations","Limit the number of NSEC3 hash iterations")="500"; // RFC5155 10.3
99 ::arg().set("max-signature-cache-entries", "Maximum number of signatures cache entries")="";
100 ::arg().set("rng", "Specify random number generator to use. Valid values are auto,sodium,openssl,getrandom,arc4random,urandom.")="auto";
101 ::arg().laxFile(configname
.c_str());
103 if(!::arg()["load-modules"].empty()) {
104 vector
<string
> modules
;
106 stringtok(modules
,::arg()["load-modules"], ", ");
107 if (!UeberBackend::loadModules(modules
, ::arg()["module-dir"])) {
112 g_log
.toConsole(Logger::Error
); // so we print any errors
113 BackendMakers().launch(::arg()["launch"]); // vrooooom!
114 if(::arg().asNum("loglevel") >= 3) // so you can't kill our errors
115 g_log
.toConsole((Logger::Urgency
)::arg().asNum("loglevel"));
117 //cerr<<"Backend: "<<::arg()["launch"]<<", '" << ::arg()["gmysql-dbname"] <<"'" <<endl;
119 S
.declare("qsize-q","Number of questions waiting for database attention");
121 ::arg().set("max-cache-entries", "Maximum number of cache entries")="1000000";
122 ::arg().set("cache-ttl","Seconds to store packets in the PacketCache")="20";
123 ::arg().set("negquery-cache-ttl","Seconds to store negative query results in the QueryCache")="60";
124 ::arg().set("query-cache-ttl","Seconds to store query results in the QueryCache")="20";
125 ::arg().set("default-soa-name","name to insert in the SOA record if none set in the backend")="a.misconfigured.powerdns.server";
126 ::arg().set("default-soa-mail","mail address to insert in the SOA record if none set in the backend")="";
127 ::arg().set("soa-refresh-default","Default SOA refresh")="10800";
128 ::arg().set("soa-retry-default","Default SOA retry")="3600";
129 ::arg().set("soa-expire-default","Default SOA expire")="604800";
130 ::arg().set("soa-minimum-ttl","Default SOA minimum ttl")="3600";
131 ::arg().set("chroot","Switch to this chroot jail")="";
132 ::arg().set("dnssec-key-cache-ttl","Seconds to cache DNSSEC keys from the database")="30";
133 ::arg().set("domain-metadata-cache-ttl","Seconds to cache domain metadata from the database")="60";
135 // Keep this line below all ::arg().set() statements
136 if (! ::arg().laxFile(configname
.c_str()))
137 cerr
<<"Warning: unable to read configuration file '"<<configname
<<"': "<<strerror(errno
)<<endl
;
139 #ifdef HAVE_LIBSODIUM
140 if (sodium_init() == -1) {
141 cerr
<<"Unable to initialize sodium crypto library"<<endl
;
146 /* init rng before chroot */
149 if (!::arg()["chroot"].empty()) {
150 if (chroot(::arg()["chroot"].c_str())<0 || chdir("/") < 0) {
151 cerr
<<"Unable to chroot to '"+::arg()["chroot"]+"': "<<strerror (errno
)<<endl
;
159 bool rectifyZone(DNSSECKeeper
& dk
, const DNSName
& zone
, bool quiet
= false)
163 bool ret
= dk
.rectifyZone(zone
, error
, output
, true);
164 if (!quiet
|| !ret
) {
165 // When quiet, only print output if there was an error
166 if (!output
.empty()) {
169 if (!ret
&& !error
.empty()) {
176 void dbBench(const std::string
& fname
)
178 ::arg().set("query-cache-ttl")="0";
179 ::arg().set("negquery-cache-ttl")="0";
180 UeberBackend
B("default");
182 vector
<string
> domains
;
184 ifstream
ifs(fname
.c_str());
186 cerr
<<"Could not open '"<<fname
<<"' for reading domain names to query"<<endl
;
189 while(getline(ifs
,line
)) {
191 domains
.push_back(line
);
195 domains
.push_back("powerdns.com");
201 unsigned int hits
=0, misses
=0;
202 for(; n
< 10000; ++n
) {
203 DNSName
domain(domains
[dns_random(domains
.size())]);
204 B
.lookup(QType(QType::NS
), domain
);
208 B
.lookup(QType(QType::A
), DNSName(std::to_string(random()))+domain
);
214 cout
<<0.001*dt
.udiff()/n
<<" millisecond/lookup"<<endl
;
215 cout
<<"Retrieved "<<hits
<<" records, did "<<misses
<<" queries which should have no match"<<endl
;
216 cout
<<"Packet cache reports: "<<S
.read("query-cache-hit")<<" hits (should be 0) and "<<S
.read("query-cache-miss") <<" misses"<<endl
;
219 bool rectifyAllZones(DNSSECKeeper
&dk
, bool quiet
= false)
221 UeberBackend
B("default");
222 vector
<DomainInfo
> domainInfo
;
225 B
.getAllDomains(&domainInfo
);
226 for(DomainInfo di
: domainInfo
) {
228 cerr
<<"Rectifying "<<di
.zone
<<": ";
230 if (!rectifyZone(dk
, di
.zone
, quiet
)) {
235 cout
<<"Rectified "<<domainInfo
.size()<<" zones."<<endl
;
240 int checkZone(DNSSECKeeper
&dk
, UeberBackend
&B
, const DNSName
& zone
, const vector
<DNSResourceRecord
>* suppliedrecords
=0)
243 if(!B
.getSOAUncached(zone
, sd
)) {
244 cout
<<"[Error] No SOA record present, or active, in zone '"<<zone
<<"'"<<endl
;
245 cout
<<"Checked 0 records of '"<<zone
<<"', 1 errors, 0 warnings."<<endl
;
249 NSEC3PARAMRecordContent ns3pr
;
251 bool haveNSEC3
= dk
.getNSEC3PARAM(zone
, &ns3pr
, &narrow
);
252 bool isOptOut
=(haveNSEC3
&& ns3pr
.d_flags
);
254 bool isSecure
=dk
.isSecuredZone(zone
);
255 bool presigned
=dk
.isPresigned(zone
);
256 vector
<string
> checkKeyErrors
;
257 bool validKeys
=dk
.checkKeys(zone
, &checkKeyErrors
);
259 uint64_t numerrors
=0, numwarnings
=0;
262 if(isSecure
&& zone
.wirelength() > 222) {
264 cout
<<"[Error] zone '" << zone
<< "' has NSEC3 semantics but is too long to have the hash prepended. Zone name is " << zone
.wirelength() << " bytes long, whereas the maximum is 222 bytes." << endl
;
267 vector
<DNSBackend::KeyData
> dbkeyset
;
268 B
.getDomainKeys(zone
, dbkeyset
);
270 for(DNSBackend::KeyData
& kd
: dbkeyset
) {
271 DNSKEYRecordContent dkrc
;
272 shared_ptr
<DNSCryptoKeyEngine
>(DNSCryptoKeyEngine::makeFromISCString(dkrc
, kd
.content
));
274 if(dkrc
.d_algorithm
== DNSSECKeeper::RSASHA1
) {
275 cout
<<"[Warning] zone '"<<zone
<<"' has NSEC3 semantics, but the "<< (kd
.active
? "" : "in" ) <<"active key with id "<<kd
.id
<<" has 'Algorithm: 5'. This should be corrected to 'Algorithm: 7' in the database (or NSEC3 should be disabled)."<<endl
;
283 cout
<<"[Error] zone '" << zone
<< "' has at least one invalid DNS Private Key." << endl
;
284 for (const auto &msg
: checkKeyErrors
) {
285 cout
<<"\t"<<msg
<<endl
;
289 // Check for delegation in parent zone
290 DNSName
parent(zone
);
291 while(parent
.chopOff()) {
293 if(B
.getSOAUncached(parent
, sd_p
)) {
296 B
.lookup(QType(QType::ANY
), zone
, NULL
, sd_p
.domain_id
);
298 ns
|= (zr
.dr
.d_type
== QType::NS
);
300 cout
<<"[Error] No delegation for zone '"<<zone
<<"' in parent '"<<parent
<<"'"<<endl
;
308 bool hasNsAtApex
= false;
309 set
<DNSName
> tlsas
, cnames
, noncnames
, glue
, checkglue
;
310 set
<pair
<DNSName
, QType
> > checkOcclusion
;
311 set
<string
> recordcontents
;
312 map
<string
, unsigned int> ttl
;
314 ostringstream content
;
315 pair
<map
<string
, unsigned int>::iterator
,bool> ret
;
317 vector
<DNSResourceRecord
> records
;
318 if(!suppliedrecords
) {
319 DNSResourceRecord drr
;
320 sd
.db
->list(zone
, sd
.domain_id
, g_verbose
);
321 while(sd
.db
->get(drr
)) {
322 records
.push_back(drr
);
326 records
=*suppliedrecords
;
328 for(auto &rr
: records
) { // we modify this
329 if(rr
.qtype
.getCode() == QType::TLSA
)
330 tlsas
.insert(rr
.qname
);
331 if(rr
.qtype
.getCode() == QType::SOA
) {
333 stringtok(parts
, rr
.content
);
337 for(int pleft
=parts
.size(); pleft
< 7; ++pleft
) {
343 if(rr
.qtype
.getCode() == QType::TXT
&& !rr
.content
.empty() && rr
.content
[0]!='"')
344 rr
.content
= "\""+rr
.content
+"\"";
347 shared_ptr
<DNSRecordContent
> drc(DNSRecordContent::mastermake(rr
.qtype
.getCode(), 1, rr
.content
));
348 string tmp
=drc
->serialize(rr
.qname
);
349 tmp
= drc
->getZoneRepresentation(true);
350 if (rr
.qtype
.getCode() != QType::AAAA
) {
351 if (!pdns_iequals(tmp
, rr
.content
)) {
352 if(rr
.qtype
.getCode() == QType::SOA
) {
353 tmp
= drc
->getZoneRepresentation(false);
355 if(!pdns_iequals(tmp
, rr
.content
)) {
356 cout
<<"[Warning] Parsed and original record content are not equal: "<<rr
.qname
<<" IN " <<rr
.qtype
.getName()<< " '" << rr
.content
<<"' (Content parsed as '"<<tmp
<<"')"<<endl
;
361 struct in6_addr tmpbuf
;
362 if (inet_pton(AF_INET6
, rr
.content
.c_str(), &tmpbuf
) != 1 || rr
.content
.find('.') != string::npos
) {
363 cout
<<"[Warning] Following record is not a valid IPv6 address: "<<rr
.qname
<<" IN " <<rr
.qtype
.getName()<< " '" << rr
.content
<<"'"<<endl
;
368 catch(std::exception
& e
)
370 cout
<<"[Error] Following record had a problem: \""<<rr
.qname
<<" IN "<<rr
.qtype
.getName()<<" "<<rr
.content
<<"\""<<endl
;
371 cout
<<"[Error] Error was: "<<e
.what()<<endl
;
376 if(!rr
.qname
.isPartOf(zone
)) {
377 cout
<<"[Error] Record '"<<rr
.qname
<<" IN "<<rr
.qtype
.getName()<<" "<<rr
.content
<<"' in zone '"<<zone
<<"' is out-of-zone."<<endl
;
383 content
<<rr
.qname
<<" "<<rr
.qtype
.getName()<<" "<<rr
.content
;
384 if (recordcontents
.count(toLower(content
.str()))) {
385 cout
<<"[Error] Duplicate record found in rrset: '"<<rr
.qname
<<" IN "<<rr
.qtype
.getName()<<" "<<rr
.content
<<"'"<<endl
;
389 recordcontents
.insert(toLower(content
.str()));
392 content
<<rr
.qname
<<" "<<rr
.qtype
.getName();
393 if (rr
.qtype
.getCode() == QType::RRSIG
) {
394 RRSIGRecordContent
rrc(rr
.content
);
395 content
<<" ("<<DNSRecordContent::NumberToType(rrc
.d_type
)<<")";
397 ret
= ttl
.insert(pair
<string
, unsigned int>(toLower(content
.str()), rr
.ttl
));
398 if (ret
.second
== false && ret
.first
->second
!= rr
.ttl
) {
399 cout
<<"[Error] TTL mismatch in rrset: '"<<rr
.qname
<<" IN " <<rr
.qtype
.getName()<<" "<<rr
.content
<<"' ("<<ret
.first
->second
<<" != "<<rr
.ttl
<<")"<<endl
;
404 if (isSecure
&& isOptOut
&& (rr
.qname
.countLabels() && rr
.qname
.getRawLabels()[0] == "*")) {
405 cout
<<"[Warning] wildcard record '"<<rr
.qname
<<" IN " <<rr
.qtype
.getName()<<" "<<rr
.content
<<"' is insecure"<<endl
;
406 cout
<<"[Info] Wildcard records in opt-out zones are insecure. Disable the opt-out flag for this zone to avoid this warning. Command: pdnsutil set-nsec3 "<<zone
<<endl
;
411 if (rr
.qtype
.getCode() == QType::NS
) {
413 } else if (rr
.qtype
.getCode() == QType::DS
) {
414 cout
<<"[Warning] DS at apex in zone '"<<zone
<<"', should not be here."<<endl
;
418 if (rr
.qtype
.getCode() == QType::SOA
) {
419 cout
<<"[Error] SOA record not at apex '"<<rr
.qname
<<" IN "<<rr
.qtype
.getName()<<" "<<rr
.content
<<"' in zone '"<<zone
<<"'"<<endl
;
422 } else if (rr
.qtype
.getCode() == QType::DNSKEY
) {
423 cout
<<"[Warning] DNSKEY record not at apex '"<<rr
.qname
<<" IN "<<rr
.qtype
.getName()<<" "<<rr
.content
<<"' in zone '"<<zone
<<"', should not be here."<<endl
;
425 } else if (rr
.qtype
.getCode() == QType::NS
) {
426 if (DNSName(rr
.content
).isPartOf(rr
.qname
)) {
427 checkglue
.insert(DNSName(toLower(rr
.content
)));
429 checkOcclusion
.insert({rr
.qname
, rr
.qtype
});
430 } else if (rr
.qtype
.getCode() == QType::A
|| rr
.qtype
.getCode() == QType::AAAA
) {
431 glue
.insert(rr
.qname
);
432 } else if (rr
.qtype
== QType::DNAME
) {
433 checkOcclusion
.insert({rr
.qname
, rr
.qtype
});
436 if((rr
.qtype
.getCode() == QType::A
|| rr
.qtype
.getCode() == QType::AAAA
) && !rr
.qname
.isWildcard() && !rr
.qname
.isHostname())
437 cout
<<"[Info] "<<rr
.qname
.toString()<<" record for '"<<rr
.qtype
.getName()<<"' is not a valid hostname."<<endl
;
439 // Check if the DNSNames that should be hostnames, are hostnames
441 checkHostnameCorrectness(rr
);
442 } catch (const std::exception
& e
) {
443 cout
<< "[Warning] " << rr
.qtype
.getName() << " record in zone '" << zone
<< ": " << e
.what() << endl
;
447 if (rr
.qtype
.getCode() == QType::CNAME
) {
448 if (!cnames
.count(rr
.qname
))
449 cnames
.insert(rr
.qname
);
451 cout
<<"[Error] Duplicate CNAME found at '"<<rr
.qname
<<"'"<<endl
;
456 if (rr
.qtype
.getCode() == QType::RRSIG
) {
458 cout
<<"[Error] RRSIG found at '"<<rr
.qname
<<"' in non-presigned zone. These do not belong in the database."<<endl
;
463 noncnames
.insert(rr
.qname
);
466 if(rr
.qtype
.getCode() == QType::NSEC
|| rr
.qtype
.getCode() == QType::NSEC3
)
468 cout
<<"[Error] NSEC or NSEC3 found at '"<<rr
.qname
<<"'. These do not belong in the database."<<endl
;
473 if(!presigned
&& rr
.qtype
.getCode() == QType::DNSKEY
)
475 if(::arg().mustDo("direct-dnskey"))
477 if(rr
.ttl
!= sd
.default_ttl
)
479 cout
<<"[Warning] DNSKEY TTL of "<<rr
.ttl
<<" at '"<<rr
.qname
<<"' differs from SOA minimum of "<<sd
.default_ttl
<<endl
;
485 cout
<<"[Warning] DNSKEY at '"<<rr
.qname
<<"' in non-presigned zone will mostly be ignored and can cause problems."<<endl
;
491 for(auto &i
: cnames
) {
492 if (noncnames
.find(i
) != noncnames
.end()) {
493 cout
<<"[Error] CNAME "<<i
<<" found, but other records with same label exist."<<endl
;
498 for(const auto &i
: tlsas
) {
499 DNSName name
= DNSName(i
);
500 name
.trimToLabels(name
.countLabels()-2);
501 if (cnames
.find(name
) == cnames
.end() && noncnames
.find(name
) == noncnames
.end()) {
502 // No specific record for the name in the TLSA record exists, this
503 // is already worth emitting a warning. Let's see if a wildcard exist.
505 DNSName
wcname(name
);
507 wcname
.prependRawLabel("*");
508 if (cnames
.find(wcname
) != cnames
.end() || noncnames
.find(wcname
) != noncnames
.end()) {
509 cout
<<"A wildcard record exist for '"<<wcname
<<"' and a TLSA record for '"<<i
<<"'.";
511 cout
<<"No record for '"<<name
<<"' exists, but a TLSA record for '"<<i
<<"' does.";
514 cout
<<" A query for '"<<name
<<"' will yield an empty response. This is most likely a mistake, please create records for '"<<name
<<"'."<<endl
;
519 cout
<<"[Error] No NS record at zone apex in zone '"<<zone
<<"'"<<endl
;
523 for(const auto &qname
: checkglue
) {
524 if (!glue
.count(qname
)) {
525 cout
<<"[Warning] Missing glue for '"<<qname
<<"' in zone '"<<zone
<<"'"<<endl
;
530 for( const auto &qname
: checkOcclusion
) {
531 for( const auto &rr
: records
) {
532 if( qname
.first
== rr
.qname
&& ((( rr
.qtype
== QType::NS
|| rr
.qtype
== QType::DS
) && qname
.second
== QType::NS
) || ( rr
.qtype
== QType::DNAME
&& qname
.second
== QType::DNAME
) ) ) {
535 if( rr
.qname
.isPartOf( qname
.first
) ) {
536 if( qname
.second
== QType::DNAME
|| ( rr
.qtype
!= QType::ENT
&& rr
.qtype
.getCode() != QType::A
&& rr
.qtype
.getCode() != QType::AAAA
) ) {
537 cout
<< "[Warning] '" << rr
.qname
<< "|" << rr
.qtype
.getName() << "' in zone '" << zone
<< "' is occluded by a ";
538 if( qname
.second
== QType::NS
) {
539 cout
<< "delegation";
543 cout
<< " at '" << qname
.first
<< "'" << endl
;
550 bool ok
, ds_ns
, done
;
551 for( const auto &rr
: records
) {
552 ok
= ( rr
.auth
== 1 );
554 done
= (suppliedrecords
|| !sd
.db
->doesDNSSEC());
555 for( const auto &qname
: checkOcclusion
) {
556 if( qname
.second
== QType::NS
) {
557 if( qname
.first
== rr
.qname
) {
564 if( rr
.qname
.isPartOf( qname
.first
) && ( qname
.first
!= rr
.qname
|| rr
.qtype
!= QType::DS
) ) {
567 if( rr
.qtype
== QType::ENT
&& qname
.first
.isPartOf( rr
.qname
) ) {
570 } else if( rr
.qname
.isPartOf( qname
.first
) && ( ( qname
.first
!= rr
.qname
|| rr
.qtype
!= QType::DS
) || rr
.qtype
== QType::NS
) ) {
576 if( ! ds_ns
&& rr
.qtype
.getCode() == QType::DS
&& rr
.qname
!= zone
) {
577 cout
<< "[Warning] DS record without a delegation '" << rr
.qname
<<"'." << endl
;
580 if( ! ok
&& ! suppliedrecords
) {
581 cout
<< "[Error] Following record is auth=" << rr
.auth
<< ", run pdnsutil rectify-zone?: " << rr
.qname
<< " IN " << rr
.qtype
.getName() << " " << rr
.content
<< endl
;
586 cout
<<"Checked "<<records
.size()<<" records of '"<<zone
<<"', "<<numerrors
<<" errors, "<<numwarnings
<<" warnings."<<endl
;
592 int checkAllZones(DNSSECKeeper
&dk
, bool exitOnError
)
594 UeberBackend
B("default");
595 vector
<DomainInfo
> domainInfo
;
596 multi_index_container
<
599 ordered_non_unique
< member
<DomainInfo
,DNSName
,&DomainInfo::zone
>, CanonDNSNameCompare
>,
600 ordered_non_unique
< member
<DomainInfo
,uint32_t,&DomainInfo::id
> >
603 auto& seenNames
= seenInfos
.get
<0>();
604 auto& seenIds
= seenInfos
.get
<1>();
606 B
.getAllDomains(&domainInfo
, true);
608 for(auto di
: domainInfo
) {
609 if (checkZone(dk
, B
, di
.zone
) > 0) {
613 auto seenName
= seenNames
.find(di
.zone
);
614 if (seenName
!= seenNames
.end()) {
615 cout
<<"[Error] Another SOA for zone '"<<di
.zone
<<"' (serial "<<di
.serial
<<") has already been seen (serial "<<seenName
->serial
<<")."<<endl
;
619 auto seenId
= seenIds
.find(di
.id
);
620 if (seenId
!= seenIds
.end()) {
621 cout
<<"[Error] Domain ID "<<di
.id
<<" of '"<<di
.zone
<<"' in backend "<<di
.backend
->getPrefix()<<" has already been used by zone '"<<seenId
->zone
<<"' in backend "<<seenId
->backend
->getPrefix()<<"."<<endl
;
625 seenInfos
.insert(di
);
627 if(errors
&& exitOnError
)
630 cout
<<"Checked "<<domainInfo
.size()<<" zones, "<<errors
<<" had errors."<<endl
;
636 int increaseSerial(const DNSName
& zone
, DNSSECKeeper
&dk
)
638 UeberBackend
B("default");
640 if(!B
.getSOAUncached(zone
, sd
)) {
641 cerr
<<"No SOA for zone '"<<zone
<<"'"<<endl
;
645 if (dk
.isPresigned(zone
)) {
646 cerr
<<"Serial increase of presigned zone '"<<zone
<<"' is not allowed."<<endl
;
651 dk
.getSoaEdit(zone
, soaEditKind
);
653 DNSResourceRecord rr
;
654 makeIncreasedSOARecord(sd
, "SOA-EDIT-INCREASE", soaEditKind
, rr
);
656 sd
.db
->startTransaction(zone
, -1);
658 if (!sd
.db
->replaceRRSet(sd
.domain_id
, zone
, rr
.qtype
, vector
<DNSResourceRecord
>(1, rr
))) {
659 sd
.db
->abortTransaction();
660 cerr
<<"Backend did not replace SOA record. Backend might not support this operation."<<endl
;
664 if (sd
.db
->doesDNSSEC()) {
665 NSEC3PARAMRecordContent ns3pr
;
667 bool haveNSEC3
=dk
.getNSEC3PARAM(zone
, &ns3pr
, &narrow
);
672 ordername
=DNSName(toBase32Hex(hashQNameWithSalt(ns3pr
, zone
))) + zone
;
676 cerr
<<"'"<<rr
.qname
<<"' -> '"<< ordername
<<"'"<<endl
;
677 sd
.db
->updateDNSSECOrderNameAndAuth(sd
.domain_id
, rr
.qname
, ordername
, true);
680 sd
.db
->commitTransaction();
682 cout
<<"SOA serial for zone "<<zone
<<" set to "<<sd
.serial
<<endl
;
686 int deleteZone(const DNSName
&zone
) {
689 if (! B
.getDomainInfo(zone
, di
)) {
690 cerr
<<"Domain '"<<zone
<<"' not found!"<<endl
;
694 if(di
.backend
->deleteDomain(zone
))
697 cerr
<<"Failed to delete domain '"<<zone
<<"'"<<endl
;;
701 void listKey(DomainInfo
const &di
, DNSSECKeeper
& dk
, bool printHeader
= true) {
703 cout
<<"Zone Type Size Algorithm ID Location Keytag"<<endl
;
704 cout
<<"----------------------------------------------------------------------------------"<<endl
;
706 unsigned int spacelen
= 0;
707 for (auto const &key
: dk
.getKeys(di
.zone
)) {
709 if (di
.zone
.toStringNoDot().length() > 29)
710 cout
<<endl
<<string(30, ' ');
712 cout
<<string(30 - di
.zone
.toStringNoDot().length(), ' ');
714 cout
<<DNSSECKeeper::keyTypeToString(key
.second
.keyType
)<<" ";
716 spacelen
= (std::to_string(key
.first
.getKey()->getBits()).length() >= 8) ? 1 : 8 - std::to_string(key
.first
.getKey()->getBits()).length();
717 if (key
.first
.getKey()->getBits() < 1) {
718 cout
<<"invalid "<<endl
;
721 cout
<<key
.first
.getKey()->getBits()<<string(spacelen
, ' ');
724 string algname
= DNSSECKeeper::algorithm2name(key
.first
.d_algorithm
);
725 spacelen
= (algname
.length() >= 13) ? 1 : 13 - algname
.length();
726 cout
<<algname
<<string(spacelen
, ' ');
728 spacelen
= (std::to_string(key
.second
.id
).length() > 5) ? 1 : 5 - std::to_string(key
.second
.id
).length();
729 cout
<<key
.second
.id
<<string(spacelen
, ' ');
732 auto stormap
= key
.first
.getKey()->convertToISCVector();
733 string engine
, slot
, label
= "";
734 for (auto const &elem
: stormap
) {
735 //cout<<elem.first<<" "<<elem.second<<endl;
736 if (elem
.first
== "Engine")
737 engine
= elem
.second
;
738 if (elem
.first
== "Slot")
740 if (elem
.first
== "Label")
743 if (engine
.empty() || slot
.empty()){
746 spacelen
= (engine
.length()+slot
.length()+label
.length()+2 >= 12) ? 1 : 12 - engine
.length()-slot
.length()-label
.length()-2;
747 cout
<<engine
<<","<<slot
<<","<<label
<<string(spacelen
, ' ');
752 cout
<<key
.first
.getDNSKEY().getTag()<<endl
;
756 int listKeys(const string
&zname
, DNSSECKeeper
& dk
){
757 UeberBackend
B("default");
759 if (zname
!= "all") {
761 if(!B
.getDomainInfo(DNSName(zname
), di
)) {
762 cerr
<< "Zone "<<zname
<<" not found."<<endl
;
767 vector
<DomainInfo
> domainInfo
;
768 B
.getAllDomains(&domainInfo
);
769 bool printHeader
= true;
770 for (auto const di
: domainInfo
) {
771 listKey(di
, dk
, printHeader
);
778 int listZone(const DNSName
&zone
) {
782 if (! B
.getDomainInfo(zone
, di
)) {
783 cerr
<<"Domain '"<<zone
<<"' not found!"<<endl
;
786 di
.backend
->list(zone
, di
.id
);
787 DNSResourceRecord rr
;
788 cout
<<"$ORIGIN ."<<endl
;
789 cout
.sync_with_stdio(false);
791 while(di
.backend
->get(rr
)) {
792 if(rr
.qtype
.getCode()) {
793 if ( (rr
.qtype
.getCode() == QType::NS
|| rr
.qtype
.getCode() == QType::SRV
|| rr
.qtype
.getCode() == QType::MX
|| rr
.qtype
.getCode() == QType::CNAME
) && !rr
.content
.empty() && rr
.content
[rr
.content
.size()-1] != '.')
794 rr
.content
.append(1, '.');
796 cout
<<rr
.qname
<<"\t"<<rr
.ttl
<<"\tIN\t"<<rr
.qtype
.getName()<<"\t"<<rr
.content
<<"\n";
803 // lovingly copied from http://stackoverflow.com/questions/1798511/how-to-avoid-press-enter-with-any-getchar
806 static struct termios oldt
, newt
;
808 /*tcgetattr gets the parameters of the current terminal
809 STDIN_FILENO will tell tcgetattr that it should write the settings
811 tcgetattr( STDIN_FILENO
, &oldt
);
812 /*now the settings will be copied*/
815 /*ICANON normally takes care that one line at a time will be processed
816 that means it will return if it sees a "\n" or an EOF or an EOL*/
817 newt
.c_lflag
&= ~(ICANON
);
819 /*Those new settings will be set to STDIN
820 TCSANOW tells tcsetattr to change attributes immediately. */
821 tcsetattr( STDIN_FILENO
, TCSANOW
, &newt
);
825 /*restore the old settings*/
826 tcsetattr( STDIN_FILENO
, TCSANOW
, &oldt
);
831 int clearZone(DNSSECKeeper
& dk
, const DNSName
&zone
) {
835 if (! B
.getDomainInfo(zone
, di
)) {
836 cerr
<<"Domain '"<<zone
<<"' not found!"<<endl
;
839 if(!di
.backend
->startTransaction(zone
, di
.id
)) {
840 cerr
<<"Unable to start transaction for load of zone '"<<zone
<<"'"<<endl
;
843 di
.backend
->commitTransaction();
847 int editZone(DNSSECKeeper
& dk
, const DNSName
&zone
) {
851 if (! B
.getDomainInfo(zone
, di
)) {
852 cerr
<<"Domain '"<<zone
<<"' not found!"<<endl
;
855 vector
<DNSRecord
> pre
, post
;
856 char tmpnam
[]="/tmp/pdnsutil-XXXXXX";
857 int tmpfd
=mkstemp(tmpnam
);
859 unixDie("Making temporary filename in "+string(tmpnam
));
861 ~deleteme() { unlink(d_name
.c_str()); }
862 deleteme(string name
) : d_name(name
) {}
866 vector
<DNSResourceRecord
> checkrr
;
868 string editor
="editor";
869 if(auto e
=getenv("EDITOR")) // <3
873 di
.backend
->list(zone
, di
.id
);
874 pre
.clear(); post
.clear();
876 if(tmpfd
< 0 && (tmpfd
=open(tmpnam
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0600)) < 0)
877 unixDie("Error reopening temporary file "+string(tmpnam
));
878 string
header("; Warning - every name in this file is ABSOLUTE!\n$ORIGIN .\n");
879 if(write(tmpfd
, header
.c_str(), header
.length()) < 0)
880 unixDie("Writing zone to temporary file");
881 DNSResourceRecord rr
;
882 while(di
.backend
->get(rr
)) {
883 if(!rr
.qtype
.getCode())
888 sort(pre
.begin(), pre
.end(), DNSRecord::prettyCompare
);
889 for(const auto& dr
: pre
) {
891 os
<<dr
.d_name
<<"\t"<<dr
.d_ttl
<<"\tIN\t"<<DNSRecordContent::NumberToType(dr
.d_type
)<<"\t"<<dr
.d_content
->getZoneRepresentation(true)<<endl
;
892 if(write(tmpfd
, os
.str().c_str(), os
.str().length()) < 0)
893 unixDie("Writing zone to temporary file");
901 cmdline
+="+"+std::to_string(gotoline
)+" ";
903 int err
=system(cmdline
.c_str());
905 unixDie("Editing file with: '"+cmdline
+"', perhaps set EDITOR variable");
908 ZoneParserTNG
zpt(tmpnam
, g_rootdnsname
);
909 DNSResourceRecord zrr
;
910 map
<pair
<DNSName
,uint16_t>, vector
<DNSRecord
> > grouped
;
912 while(zpt
.get(zrr
)) {
915 grouped
[{dr
.d_name
,dr
.d_type
}].push_back(dr
);
918 catch(std::exception
& e
) {
919 cerr
<<"Problem: "<<e
.what()<<" "<<zpt
.getLineOfFile()<<endl
;
920 auto fnum
= zpt
.getLineNumAndFile();
921 gotoline
= fnum
.second
;
925 sort(post
.begin(), post
.end(), DNSRecord::prettyCompare
);
928 for(const DNSRecord
& rr
: post
) {
929 DNSResourceRecord drr
= DNSResourceRecord::fromWire(rr
);
930 drr
.domain_id
= di
.id
;
931 checkrr
.push_back(drr
);
933 if(checkZone(dk
, B
, zone
, &checkrr
)) {
935 cerr
<<"\x1b[31;1mThere was a problem with your zone\x1b[0m\nOptions are: (e)dit your changes, (r)etry with original zone, (a)pply change anyhow, (q)uit: "<<endl
;
951 vector
<DNSRecord
> diff
;
953 map
<pair
<DNSName
,uint16_t>, string
> changed
;
954 set_difference(pre
.cbegin(), pre
.cend(), post
.cbegin(), post
.cend(), back_inserter(diff
), DNSRecord::prettyCompare
);
955 for(const auto& d
: diff
) {
957 str
<<"\033[0;31m-"<< d
.d_name
<<" "<<d
.d_ttl
<<" IN "<<DNSRecordContent::NumberToType(d
.d_type
)<<" "<<d
.d_content
->getZoneRepresentation(true)<<"\033[0m"<<endl
;
958 changed
[{d
.d_name
,d
.d_type
}] += str
.str();
962 set_difference(post
.cbegin(), post
.cend(), pre
.cbegin(), pre
.cend(), back_inserter(diff
), DNSRecord::prettyCompare
);
963 for(const auto& d
: diff
) {
966 str
<<"\033[0;32m+"<< d
.d_name
<<" "<<d
.d_ttl
<<" IN "<<DNSRecordContent::NumberToType(d
.d_type
)<<" "<<d
.d_content
->getZoneRepresentation(true)<<"\033[0m"<<endl
;
967 changed
[{d
.d_name
,d
.d_type
}]+=str
.str();
969 if (changed
.size() > 0)
970 cout
<<"Detected the following changes:"<<endl
;
971 for(const auto& c
: changed
) {
975 if(changed
.empty()) {
976 cout
<<endl
<<"No changes to apply."<<endl
;
977 return(EXIT_SUCCESS
);
979 cout
<<endl
<<"(a)pply these changes, (e)dit again, (r)etry with original zone, (q)uit: ";
984 return(EXIT_SUCCESS
);
989 else if(changed
.empty() || c
!='a')
992 for(const auto& change
: changed
) {
993 vector
<DNSResourceRecord
> vrr
;
994 for(const DNSRecord
& rr
: grouped
[change
.first
]) {
995 DNSResourceRecord crr
= DNSResourceRecord::fromWire(rr
);
996 crr
.domain_id
= di
.id
;
999 di
.backend
->replaceRRSet(di
.id
, change
.first
.first
, QType(change
.first
.second
), vrr
);
1001 rectifyZone(dk
, zone
);
1002 return EXIT_SUCCESS
;
1006 int loadZone(DNSName zone
, const string
& fname
) {
1010 if (B
.getDomainInfo(zone
, di
)) {
1011 cerr
<<"Domain '"<<zone
<<"' exists already, replacing contents"<<endl
;
1014 cerr
<<"Creating '"<<zone
<<"'"<<endl
;
1015 B
.createDomain(zone
);
1017 if(!B
.getDomainInfo(zone
, di
)) {
1018 cerr
<<"Domain '"<<zone
<<"' was not created - perhaps backend ("<<::arg()["launch"]<<") does not support storing new zones."<<endl
;
1019 return EXIT_FAILURE
;
1022 DNSBackend
* db
= di
.backend
;
1023 ZoneParserTNG
zpt(fname
, zone
);
1025 DNSResourceRecord rr
;
1026 if(!db
->startTransaction(zone
, di
.id
)) {
1027 cerr
<<"Unable to start transaction for load of zone '"<<zone
<<"'"<<endl
;
1028 return EXIT_FAILURE
;
1031 bool haveSOA
= false;
1032 while(zpt
.get(rr
)) {
1033 if(!rr
.qname
.isPartOf(zone
) && rr
.qname
!=zone
) {
1034 cerr
<<"File contains record named '"<<rr
.qname
<<"' which is not part of zone '"<<zone
<<"'"<<endl
;
1035 return EXIT_FAILURE
;
1037 if (rr
.qtype
== QType::SOA
) {
1043 db
->feedRecord(rr
, DNSName());
1045 db
->commitTransaction();
1046 return EXIT_SUCCESS
;
1049 int createZone(const DNSName
&zone
, const DNSName
& nsname
) {
1052 if (B
.getDomainInfo(zone
, di
)) {
1053 cerr
<<"Domain '"<<zone
<<"' exists already"<<endl
;
1054 return EXIT_FAILURE
;
1056 cerr
<<"Creating empty zone '"<<zone
<<"'"<<endl
;
1057 B
.createDomain(zone
);
1058 if(!B
.getDomainInfo(zone
, di
)) {
1059 cerr
<<"Domain '"<<zone
<<"' was not created!"<<endl
;
1060 return EXIT_FAILURE
;
1063 DNSResourceRecord rr
;
1066 rr
.ttl
= ::arg().asNum("default-ttl");
1069 string soa
= (boost::format("%s %s 1")
1070 % (nsname
.empty() ? ::arg()["default-soa-name"] : nsname
.toString())
1071 % (::arg().isEmpty("default-soa-mail") ? (DNSName("hostmaster.") + zone
).toString() : ::arg()["default-soa-mail"])
1074 fillSOAData(soa
, sd
); // fills out default values for us
1075 rr
.content
= makeSOAContent(sd
)->getZoneRepresentation(true);
1076 rr
.domain_id
= di
.id
;
1077 di
.backend
->startTransaction(zone
, di
.id
);
1078 di
.backend
->feedRecord(rr
, DNSName());
1079 if(!nsname
.empty()) {
1080 cout
<<"Also adding one NS record"<<endl
;
1082 rr
.content
=nsname
.toStringNoDot();
1083 di
.backend
->feedRecord(rr
, DNSName());
1086 di
.backend
->commitTransaction();
1088 return EXIT_SUCCESS
;
1091 int createSlaveZone(const vector
<string
>& cmds
) {
1094 DNSName
zone(cmds
[1]);
1095 if (B
.getDomainInfo(zone
, di
)) {
1096 cerr
<<"Domain '"<<zone
<<"' exists already"<<endl
;
1097 return EXIT_FAILURE
;
1099 vector
<string
> masters
;
1100 for (unsigned i
=2; i
< cmds
.size(); i
++) {
1101 ComboAddress
master(cmds
[i
], 53);
1102 masters
.push_back(master
.toStringWithPort());
1104 cerr
<<"Creating slave zone '"<<zone
<<"', with master(s) '"<<boost::join(masters
, ",")<<"'"<<endl
;
1105 B
.createDomain(zone
);
1106 if(!B
.getDomainInfo(zone
, di
)) {
1107 cerr
<<"Domain '"<<zone
<<"' was not created!"<<endl
;
1108 return EXIT_FAILURE
;
1110 di
.backend
->setKind(zone
, DomainInfo::Slave
);
1111 di
.backend
->setMaster(zone
, boost::join(masters
, ","));
1112 return EXIT_SUCCESS
;
1115 int changeSlaveZoneMaster(const vector
<string
>& cmds
) {
1118 DNSName
zone(cmds
[1]);
1119 if (!B
.getDomainInfo(zone
, di
)) {
1120 cerr
<<"Domain '"<<zone
<<"' doesn't exist"<<endl
;
1121 return EXIT_FAILURE
;
1123 vector
<string
> masters
;
1124 for (unsigned i
=2; i
< cmds
.size(); i
++) {
1125 ComboAddress
master(cmds
[i
], 53);
1126 masters
.push_back(master
.toStringWithPort());
1128 cerr
<<"Updating slave zone '"<<zone
<<"', master(s) to '"<<boost::join(masters
, ",")<<"'"<<endl
;
1130 di
.backend
->setMaster(zone
, boost::join(masters
, ","));
1131 return EXIT_SUCCESS
;
1133 catch (PDNSException
& e
) {
1134 cerr
<<"Setting master for zone '"<<zone
<<"' failed: "<<e
.reason
<<endl
;
1135 return EXIT_FAILURE
;
1139 // add-record ZONE name type [ttl] "content" ["content"]
1140 int addOrReplaceRecord(bool addOrReplace
, const vector
<string
>& cmds
) {
1141 DNSResourceRecord rr
;
1142 vector
<DNSResourceRecord
> newrrs
;
1143 DNSName
zone(cmds
[1]);
1148 name
=DNSName(cmds
[2])+zone
;
1150 rr
.qtype
= DNSRecordContent::TypeToNumber(cmds
[3]);
1151 rr
.ttl
= ::arg().asNum("default-ttl");
1156 if(!B
.getDomainInfo(zone
, di
)) {
1157 cerr
<<"Domain '"<<zone
<<"' does not exist"<<endl
;
1158 return EXIT_FAILURE
;
1161 rr
.domain_id
= di
.id
;
1163 DNSResourceRecord oldrr
;
1164 if(addOrReplace
) { // the 'add' case
1165 di
.backend
->lookup(rr
.qtype
, rr
.qname
, 0, di
.id
);
1167 while(di
.backend
->get(oldrr
))
1168 newrrs
.push_back(oldrr
);
1171 unsigned int contentStart
= 4;
1172 if(cmds
.size() > 5) {
1173 rr
.ttl
=atoi(cmds
[4].c_str());
1174 if(std::to_string(rr
.ttl
)==cmds
[4]) {
1178 rr
.ttl
= ::arg().asNum("default-ttl");
1182 di
.backend
->lookup(QType(QType::ANY
), rr
.qname
, 0, di
.id
);
1184 if(rr
.qtype
.getCode() == QType::CNAME
) { // this will save us SO many questions
1186 while(di
.backend
->get(oldrr
)) {
1187 if(addOrReplace
|| oldrr
.qtype
.getCode() != QType::CNAME
) // the replace case is ok if we replace one CNAME by the other
1191 cerr
<<"Attempting to add CNAME to "<<rr
.qname
<<" which already had existing records"<<endl
;
1192 return EXIT_FAILURE
;
1196 while(di
.backend
->get(oldrr
)) {
1197 if(oldrr
.qtype
.getCode() == QType::CNAME
)
1201 cerr
<<"Attempting to add record to "<<rr
.qname
<<" which already had a CNAME record"<<endl
;
1202 return EXIT_FAILURE
;
1207 cout
<<"Current records for "<<rr
.qname
<<" IN "<<rr
.qtype
.getName()<<" will be replaced"<<endl
;
1209 for(auto i
= contentStart
; i
< cmds
.size() ; ++i
) {
1210 rr
.content
= DNSRecordContent::mastermake(rr
.qtype
.getCode(), QClass::IN
, cmds
[i
])->getZoneRepresentation(true);
1212 newrrs
.push_back(rr
);
1216 di
.backend
->replaceRRSet(di
.id
, name
, rr
.qtype
, newrrs
);
1217 // need to be explicit to bypass the ueberbackend cache!
1218 di
.backend
->lookup(rr
.qtype
, name
, 0, di
.id
);
1219 cout
<<"New rrset:"<<endl
;
1220 while(di
.backend
->get(rr
)) {
1221 cout
<<rr
.qname
.toString()<<" "<<rr
.ttl
<<" IN "<<rr
.qtype
.getName()<<" "<<rr
.content
<<endl
;
1223 return EXIT_SUCCESS
;
1226 // delete-rrset zone name type
1227 int deleteRRSet(const std::string
& zone_
, const std::string
& name_
, const std::string
& type_
)
1231 DNSName
zone(zone_
);
1232 if(!B
.getDomainInfo(zone
, di
)) {
1233 cerr
<<"Domain '"<<zone
<<"' does not exist"<<endl
;
1234 return EXIT_FAILURE
;
1241 name
=DNSName(name_
)+zone
;
1243 QType
qt(QType::chartocode(type_
.c_str()));
1244 di
.backend
->replaceRRSet(di
.id
, name
, qt
, vector
<DNSResourceRecord
>());
1245 return EXIT_SUCCESS
;
1248 int listAllZones(const string
&type
="") {
1250 int kindFilter
= -1;
1252 if (toUpper(type
) == "MASTER")
1254 else if (toUpper(type
) == "SLAVE")
1256 else if (toUpper(type
) == "NATIVE")
1259 cerr
<<"Syntax: pdnsutil list-all-zones [master|slave|native]"<<endl
;
1264 UeberBackend
B("default");
1266 vector
<DomainInfo
> domains
;
1267 B
.getAllDomains(&domains
, true);
1270 for (const auto& di
: domains
) {
1271 if (di
.kind
== kindFilter
|| kindFilter
== -1) {
1272 cout
<<di
.zone
<<endl
;
1278 if (kindFilter
!= -1)
1279 cout
<<type
<<" zonecount: "<<count
<<endl
;
1281 cout
<<"All zonecount: "<<count
<<endl
;
1287 bool testAlgorithm(int algo
)
1289 return DNSCryptoKeyEngine::testOne(algo
);
1292 bool testAlgorithms()
1294 return DNSCryptoKeyEngine::testAll();
1297 void testSpeed(DNSSECKeeper
& dk
, const DNSName
& zone
, const string
& remote
, int cores
)
1299 DNSResourceRecord rr
;
1300 rr
.qname
=DNSName("blah")+zone
;
1304 rr
.qclass
= QClass::IN
;
1306 UeberBackend
db("key-only");
1308 if ( ! db
.backends
.size() )
1310 throw runtime_error("No backends available for DNSSEC key storage");
1313 ChunkedSigningPipe
csp(DNSName(zone
), 1, cores
);
1315 vector
<DNSZoneRecord
> signatures
;
1317 unsigned char* octets
= (unsigned char*)&rnd
;
1321 for(unsigned int n
=0; n
< 100000; ++n
) {
1322 rnd
= dns_random(UINT32_MAX
);
1323 snprintf(tmp
, sizeof(tmp
), "%d.%d.%d.%d",
1324 octets
[0], octets
[1], octets
[2], octets
[3]);
1327 snprintf(tmp
, sizeof(tmp
), "r-%u", rnd
);
1328 rr
.qname
=DNSName(tmp
)+zone
;
1330 dzr
.dr
=DNSRecord(rr
);
1332 while(signatures
= csp
.getChunk(), !signatures
.empty())
1335 cerr
<<"Flushing the pipe, "<<csp
.d_signed
<<" signed, "<<csp
.d_queued
<<" queued, "<<csp
.d_outstanding
<<" outstanding"<< endl
;
1336 cerr
<<"Net speed: "<<csp
.d_signed
/ (dt
.udiffNoReset()/1000000.0) << " sigs/s"<<endl
;
1337 while(signatures
= csp
.getChunk(true), !signatures
.empty())
1339 cerr
<<"Done, "<<csp
.d_signed
<<" signed, "<<csp
.d_queued
<<" queued, "<<csp
.d_outstanding
<<" outstanding"<< endl
;
1340 cerr
<<"Net speed: "<<csp
.d_signed
/ (dt
.udiff()/1000000.0) << " sigs/s"<<endl
;
1343 void verifyCrypto(const string
& zone
)
1345 ZoneParserTNG
zpt(zone
);
1346 DNSResourceRecord rr
;
1347 DNSKEYRecordContent drc
;
1348 RRSIGRecordContent rrc
;
1349 DSRecordContent dsrc
;
1350 vector
<shared_ptr
<DNSRecordContent
> > toSign
;
1351 DNSName qname
, apex
;
1352 dsrc
.d_digesttype
=0;
1353 while(zpt
.get(rr
)) {
1354 if(rr
.qtype
.getCode() == QType::DNSKEY
) {
1355 cerr
<<"got DNSKEY!"<<endl
;
1357 drc
= *std::dynamic_pointer_cast
<DNSKEYRecordContent
>(DNSRecordContent::mastermake(QType::DNSKEY
, 1, rr
.content
));
1359 else if(rr
.qtype
.getCode() == QType::RRSIG
) {
1360 cerr
<<"got RRSIG"<<endl
;
1361 rrc
= *std::dynamic_pointer_cast
<RRSIGRecordContent
>(DNSRecordContent::mastermake(QType::RRSIG
, 1, rr
.content
));
1363 else if(rr
.qtype
.getCode() == QType::DS
) {
1364 cerr
<<"got DS"<<endl
;
1365 dsrc
= *std::dynamic_pointer_cast
<DSRecordContent
>(DNSRecordContent::mastermake(QType::DS
, 1, rr
.content
));
1369 toSign
.push_back(DNSRecordContent::mastermake(rr
.qtype
.getCode(), 1, rr
.content
));
1373 string msg
= getMessageForRRSET(qname
, rrc
, toSign
);
1374 cerr
<<"Verify: "<<DNSCryptoKeyEngine::makeFromPublicKeyString(drc
.d_algorithm
, drc
.d_key
)->verify(msg
, rrc
.d_signature
)<<endl
;
1375 if(dsrc
.d_digesttype
) {
1376 cerr
<<"Calculated DS: "<<apex
.toString()<<" IN DS "<<makeDSFromDNSKey(apex
, drc
, dsrc
.d_digesttype
).getZoneRepresentation()<<endl
;
1377 cerr
<<"Original DS: "<<apex
.toString()<<" IN DS "<<dsrc
.getZoneRepresentation()<<endl
;
1380 std::shared_ptr
<DNSCryptoKeyEngine
> key
=DNSCryptoKeyEngine::makeFromISCString(drc
, "Private-key-format: v1.2\n"
1381 "Algorithm: 12 (ECC-GOST)\n"
1382 "GostAsn1: MEUCAQAwHAYGKoUDAgITMBIGByqFAwICIwEGByqFAwICHgEEIgQg/9MiXtXKg9FDXDN/R9CmVhJDyuzRAIgh4tPwCu4NHIs=\n");
1383 string resign
=key
->sign(hash
);
1384 cerr
<<Base64Encode(resign
)<<endl
;
1385 cerr
<<"Verify: "<<DNSCryptoKeyEngine::makeFromPublicKeyString(drc
.d_algorithm
, drc
.d_key
)->verify(hash
, resign
)<<endl
;
1389 bool disableDNSSECOnZone(DNSSECKeeper
& dk
, const DNSName
& zone
)
1391 UeberBackend
B("default");
1394 if (!B
.getDomainInfo(zone
, di
)){
1395 cerr
<< "No such zone in the database" << endl
;
1400 bool ret
= dk
.unSecureZone(zone
, error
, info
);
1402 cerr
<< error
<< endl
;
1407 int setZoneAccount(const DNSName
& zone
, const string
&account
)
1409 UeberBackend
B("default");
1412 if (!B
.getDomainInfo(zone
, di
)){
1413 cerr
<< "No such zone "<<zone
<<" in the database" << endl
;
1414 return EXIT_FAILURE
;
1416 if(!di
.backend
->setAccount(zone
, account
)) {
1417 cerr
<<"Could not find backend willing to accept new zone configuration"<<endl
;
1418 return EXIT_FAILURE
;
1420 return EXIT_SUCCESS
;
1423 int setZoneKind(const DNSName
& zone
, const DomainInfo::DomainKind kind
)
1425 UeberBackend
B("default");
1428 if (!B
.getDomainInfo(zone
, di
)){
1429 cerr
<< "No such zone "<<zone
<<" in the database" << endl
;
1430 return EXIT_FAILURE
;
1432 if(!di
.backend
->setKind(zone
, kind
)) {
1433 cerr
<<"Could not find backend willing to accept new zone configuration"<<endl
;
1434 return EXIT_FAILURE
;
1436 return EXIT_SUCCESS
;
1439 bool showZone(DNSSECKeeper
& dk
, const DNSName
& zone
, bool exportDS
= false)
1441 UeberBackend
B("default");
1444 if (!B
.getDomainInfo(zone
, di
)){
1445 cerr
<< "No such zone in the database" << endl
;
1449 if (!di
.account
.empty()) {
1450 cout
<<"This zone is owned by "<<di
.account
<<endl
;
1453 cout
<<"This is a "<<DomainInfo::getKindString(di
.kind
)<<" zone"<<endl
;
1454 if(di
.kind
== DomainInfo::Master
) {
1455 cout
<<"Last SOA serial number we notified: "<<di
.notified_serial
<<" ";
1457 if(B
.getSOAUncached(zone
, sd
)) {
1458 if(sd
.serial
== di
.notified_serial
)
1462 cout
<<sd
.serial
<<" (serial in the database)"<<endl
;
1465 else if(di
.kind
== DomainInfo::Slave
) {
1466 cout
<<"Master"<<addS(di
.masters
)<<": ";
1467 for(const auto& m
: di
.masters
)
1468 cout
<<m
.toStringWithPort()<<" ";
1471 localtime_r(&di
.last_check
, &tm
);
1474 strftime(buf
, sizeof(buf
)-1, "%a %F %H:%M:%S", &tm
);
1476 strncpy(buf
, "Never", sizeof(buf
)-1);
1477 buf
[sizeof(buf
)-1] = '\0';
1478 cout
<<"Last time we got update from master: "<<buf
<<endl
;
1480 if(B
.getSOAUncached(zone
, sd
)) {
1481 cout
<<"SOA serial in database: "<<sd
.serial
<<endl
;
1482 cout
<<"Refresh interval: "<<sd
.refresh
<<" seconds"<<endl
;
1485 cout
<<"No SOA serial found in database"<<endl
;
1489 if(!dk
.isSecuredZone(zone
)) {
1490 auto &outstream
= (exportDS
? cerr
: cout
);
1491 outstream
<< "Zone is not actively secured" << endl
;
1493 // it does not make sense to proceed here, and it might be useful
1494 // for scripts to know that something is odd here
1499 NSEC3PARAMRecordContent ns3pr
;
1501 bool haveNSEC3
=dk
.getNSEC3PARAM(zone
, &ns3pr
, &narrow
);
1503 DNSSECKeeper::keyset_t keyset
=dk
.getKeys(zone
);
1506 std::vector
<std::string
> meta
;
1508 if (B
.getDomainMetadata(zone
, "TSIG-ALLOW-AXFR", meta
) && meta
.size() > 0) {
1509 cout
<< "Zone has following allowed TSIG key(s): " << boost::join(meta
, ",") << endl
;
1513 if (B
.getDomainMetadata(zone
, "AXFR-MASTER-TSIG", meta
) && meta
.size() > 0) {
1514 cout
<< "Zone uses following TSIG key(s): " << boost::join(meta
, ",") << endl
;
1517 std::map
<std::string
, std::vector
<std::string
> > metamap
;
1518 if(B
.getAllDomainMetadata(zone
, metamap
)) {
1519 cout
<<"Metadata items: ";
1524 for(const auto& m
: metamap
) {
1525 for(const auto i
: m
.second
)
1526 cout
<< '\t' << m
.first
<<'\t' << i
<<endl
;
1532 if (dk
.isPresigned(zone
)) {
1534 cout
<<"Zone is presigned"<<endl
;
1538 vector
<DNSKEYRecordContent
> keys
;
1541 B
.lookup(QType(QType::DNSKEY
), zone
);
1543 if (zr
.dr
.d_type
!= QType::DNSKEY
) continue;
1544 keys
.push_back(*getRR
<DNSKEYRecordContent
>(zr
.dr
));
1548 cerr
<< "No keys for zone '"<<zone
<<"'."<<endl
;
1554 cout
<<"Zone has NSEC semantics"<<endl
;
1556 cout
<<"Zone has " << (narrow
? "NARROW " : "") <<"hashed NSEC3 semantics, configuration: "<<ns3pr
.getZoneRepresentation()<<endl
;
1557 cout
<< "keys: "<<endl
;
1560 sort(keys
.begin(),keys
.end());
1561 reverse(keys
.begin(),keys
.end());
1563 for(const auto& key
: keys
) {
1564 string algname
= DNSSECKeeper::algorithm2name(key
.d_algorithm
);
1568 std::shared_ptr
<DNSCryptoKeyEngine
> engine(DNSCryptoKeyEngine::makeFromPublicKeyString(key
.d_algorithm
, key
.d_key
)); // throws on unknown algo or bad key
1569 bits
=engine
->getBits();
1571 catch(std::exception
& e
) {
1572 cerr
<<"Could not process key to extract metadata: "<<e
.what()<<endl
;
1575 cout
<< (key
.d_flags
== 257 ? "KSK" : "ZSK") << ", tag = " << key
.getTag() << ", algo = "<<(int)key
.d_algorithm
<< ", bits = " << bits
<< endl
;
1576 cout
<< "DNSKEY = " <<zone
.toString()<<" IN DNSKEY "<< key
.getZoneRepresentation() << "; ( " + algname
+ " ) " <<endl
;
1579 if (shown
) continue;
1582 const std::string
prefix(exportDS
? "" : "DS = ");
1583 cout
<<prefix
<<zone
.toString()<<" IN DS "<<makeDSFromDNSKey(zone
, key
, DNSSECKeeper::SHA1
).getZoneRepresentation() << " ; ( SHA1 digest )" << endl
;
1584 cout
<<prefix
<<zone
.toString()<<" IN DS "<<makeDSFromDNSKey(zone
, key
, DNSSECKeeper::SHA256
).getZoneRepresentation() << " ; ( SHA256 digest )" << endl
;
1586 string output
=makeDSFromDNSKey(zone
, key
, DNSSECKeeper::GOST
).getZoneRepresentation();
1587 cout
<<prefix
<<zone
.toString()<<" IN DS "<<output
<< " ; ( GOST R 34.11-94 digest )" << endl
;
1592 string output
=makeDSFromDNSKey(zone
, key
, DNSSECKeeper::SHA384
).getZoneRepresentation();
1593 cout
<<prefix
<<zone
.toString()<<" IN DS "<<output
<< " ; ( SHA-384 digest )" << endl
;
1599 else if(keyset
.empty()) {
1600 cerr
<< "No keys for zone '"<<zone
<<"'."<<endl
;
1605 cout
<<"Zone has NSEC semantics"<<endl
;
1607 cout
<<"Zone has " << (narrow
? "NARROW " : "") <<"hashed NSEC3 semantics, configuration: "<<ns3pr
.getZoneRepresentation()<<endl
;
1608 cout
<< "keys: "<<endl
;
1611 for(DNSSECKeeper::keyset_t::value_type value
: keyset
) {
1612 string algname
= DNSSECKeeper::algorithm2name(value
.first
.d_algorithm
);
1614 cout
<<"ID = "<<value
.second
.id
<<" ("<<DNSSECKeeper::keyTypeToString(value
.second
.keyType
)<<")";
1616 if (value
.first
.getKey()->getBits() < 1) {
1617 cerr
<<" <key missing or defunct>" <<endl
;
1621 cout
<<", flags = "<<std::to_string(value
.first
.d_flags
);
1622 cout
<<", tag = "<<value
.first
.getDNSKEY().getTag();
1623 cout
<<", algo = "<<(int)value
.first
.d_algorithm
<<", bits = "<<value
.first
.getKey()->getBits()<<"\t"<<((int)value
.second
.active
== 1 ? " A" : "Ina")<<"ctive ( " + algname
+ " ) "<<endl
;
1627 if (value
.second
.keyType
== DNSSECKeeper::KSK
|| value
.second
.keyType
== DNSSECKeeper::CSK
|| ::arg().mustDo("direct-dnskey")) {
1628 cout
<<DNSSECKeeper::keyTypeToString(value
.second
.keyType
)<<" DNSKEY = "<<zone
.toString()<<" IN DNSKEY "<< value
.first
.getDNSKEY().getZoneRepresentation() << " ; ( " + algname
+ " )" << endl
;
1631 if (value
.second
.keyType
== DNSSECKeeper::KSK
|| value
.second
.keyType
== DNSSECKeeper::CSK
) {
1632 const auto &key
= value
.first
.getDNSKEY();
1633 const std::string
prefix(exportDS
? "" : "DS = ");
1634 cout
<<prefix
<<zone
.toString()<<" IN DS "<<makeDSFromDNSKey(zone
, key
, DNSSECKeeper::SHA1
).getZoneRepresentation() << " ; ( SHA1 digest )" << endl
;
1635 cout
<<prefix
<<zone
.toString()<<" IN DS "<<makeDSFromDNSKey(zone
, key
, DNSSECKeeper::SHA256
).getZoneRepresentation() << " ; ( SHA256 digest )" << endl
;
1637 string output
=makeDSFromDNSKey(zone
, key
, DNSSECKeeper::GOST
).getZoneRepresentation();
1638 cout
<<prefix
<<zone
.toString()<<" IN DS "<<output
<< " ; ( GOST R 34.11-94 digest )" << endl
;
1643 string output
=makeDSFromDNSKey(zone
, key
, DNSSECKeeper::SHA384
).getZoneRepresentation();
1644 cout
<<prefix
<<zone
.toString()<<" IN DS "<<output
<< " ; ( SHA-384 digest )" << endl
;
1654 bool secureZone(DNSSECKeeper
& dk
, const DNSName
& zone
)
1659 // temp var for addKey
1662 string k_algo
= ::arg()["default-ksk-algorithm"];
1663 k_size
= ::arg().asNum("default-ksk-size");
1664 string z_algo
= ::arg()["default-zsk-algorithm"];
1665 z_size
= ::arg().asNum("default-zsk-size");
1668 throw runtime_error("KSK key size must be equal to or greater than 0");
1671 if (k_algo
== "" && z_algo
== "") {
1672 throw runtime_error("Zero algorithms given for KSK+ZSK in total");
1676 throw runtime_error("ZSK key size must be equal to or greater than 0");
1679 if(dk
.isSecuredZone(zone
)) {
1680 cerr
<< "Zone '"<<zone
<<"' already secure, remove keys with pdnsutil remove-zone-key if needed"<<endl
;
1685 UeberBackend
B("default");
1686 if(!B
.getDomainInfo(zone
, di
) || !di
.backend
) { // di.backend and B are mostly identical
1687 cerr
<<"Can't find a zone called '"<<zone
<<"'"<<endl
;
1691 if(di
.kind
== DomainInfo::Slave
)
1693 cerr
<<"Warning! This is a slave domain! If this was a mistake, please run"<<endl
;
1694 cerr
<<"pdnsutil disable-dnssec "<<zone
<<" right now!"<<endl
;
1697 if (k_algo
!= "") { // Add a KSK
1699 cout
<< "Securing zone with key size " << k_size
<< endl
;
1701 cout
<< "Securing zone with default key size" << endl
;
1703 cout
<< "Adding "<<(z_algo
== "" ? "CSK (257)" : "KSK")<<" with algorithm " << k_algo
<< endl
;
1705 int k_real_algo
= DNSSECKeeper::shorthand2algorithm(k_algo
);
1707 if (!dk
.addKey(zone
, true, k_real_algo
, id
, k_size
, true)) {
1708 cerr
<<"No backend was able to secure '"<<zone
<<"', most likely because no DNSSEC"<<endl
;
1709 cerr
<<"capable backends are loaded, or because the backends have DNSSEC disabled."<<endl
;
1710 cerr
<<"For the Generic SQL backends, set the 'gsqlite3-dnssec', 'gmysql-dnssec' or"<<endl
;
1711 cerr
<<"'gpgsql-dnssec' flag. Also make sure the schema has been updated for DNSSEC!"<<endl
;
1717 cout
<< "Adding "<<(k_algo
== "" ? "CSK (256)" : "ZSK")<<" with algorithm " << z_algo
<< endl
;
1719 int z_real_algo
= DNSSECKeeper::shorthand2algorithm(z_algo
);
1721 if (!dk
.addKey(zone
, false, z_real_algo
, id
, z_size
, true)) {
1722 cerr
<<"No backend was able to secure '"<<zone
<<"', most likely because no DNSSEC"<<endl
;
1723 cerr
<<"capable backends are loaded, or because the backends have DNSSEC disabled."<<endl
;
1724 cerr
<<"For the Generic SQL backends, set the 'gsqlite3-dnssec', 'gmysql-dnssec' or"<<endl
;
1725 cerr
<<"'gpgsql-dnssec' flag. Also make sure the schema has been updated for DNSSEC!"<<endl
;
1730 if(!dk
.isSecuredZone(zone
)) {
1731 cerr
<<"Failed to secure zone. Is your backend dnssec enabled? (set "<<endl
;
1732 cerr
<<"gsqlite3-dnssec, or gmysql-dnssec etc). Check this first."<<endl
;
1733 cerr
<<"If you run with the BIND backend, make sure you have configured"<<endl
;
1734 cerr
<<"it to use DNSSEC with 'bind-dnssec-db=/path/fname' and"<<endl
;
1735 cerr
<<"'pdnsutil create-bind-db /path/fname'!"<<endl
;
1739 // rectifyZone(dk, zone);
1740 // showZone(dk, zone);
1741 cout
<<"Zone "<<zone
<<" secured"<<endl
;
1745 void testSchema(DNSSECKeeper
& dk
, const DNSName
& zone
)
1747 cout
<<"Note: test-schema will try to create the zone, but it will not remove it."<<endl
;
1748 cout
<<"Please clean up after this."<<endl
;
1750 cout
<<"Constructing UeberBackend"<<endl
;
1751 UeberBackend
B("default");
1752 cout
<<"Picking first backend - if this is not what you want, edit launch line!"<<endl
;
1753 DNSBackend
*db
= B
.backends
[0];
1754 cout
<<"Creating slave domain "<<zone
<<endl
;
1755 db
->createSlaveDomain("127.0.0.1", zone
, "", "_testschema");
1756 cout
<<"Slave domain created"<<endl
;
1759 if(!B
.getDomainInfo(zone
, di
) || !di
.backend
) { // di.backend and B are mostly identical
1760 cout
<<"Can't find domain we just created, aborting"<<endl
;
1764 DNSResourceRecord rr
, rrget
;
1765 cout
<<"Starting transaction to feed records"<<endl
;
1766 db
->startTransaction(zone
, di
.id
);
1768 rr
.qtype
=QType::SOA
;
1773 rr
.content
="ns1.example.com. ahu.example.com. 2012081039 7200 3600 1209600 3600";
1774 cout
<<"Feeding SOA"<<endl
;
1775 db
->feedRecord(rr
, DNSName());
1776 rr
.qtype
=QType::TXT
;
1778 rr
.content
="\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"";
1779 cout
<<"Feeding overlong TXT"<<endl
;
1780 db
->feedRecord(rr
, DNSName());
1781 cout
<<"Committing"<<endl
;
1782 db
->commitTransaction();
1783 cout
<<"Querying TXT"<<endl
;
1784 db
->lookup(QType(QType::TXT
), zone
, NULL
, di
.id
);
1787 DNSResourceRecord rrthrowaway
;
1788 if(db
->get(rrthrowaway
)) // should not touch rr but don't assume anything
1790 cout
<<"Expected one record, got multiple, aborting"<<endl
;
1793 int size
=rrget
.content
.size();
1796 cout
<<"Expected 302 bytes, got "<<size
<<", aborting"<<endl
;
1800 cout
<<"[+] content field is over 255 bytes"<<endl
;
1802 cout
<<"Dropping all records, inserting SOA+2xA"<<endl
;
1803 db
->startTransaction(zone
, di
.id
);
1805 rr
.qtype
=QType::SOA
;
1810 rr
.content
="ns1.example.com. ahu.example.com. 2012081039 7200 3600 1209600 3600";
1811 cout
<<"Feeding SOA"<<endl
;
1812 db
->feedRecord(rr
, DNSName());
1815 rr
.qname
=DNSName("_underscore")+zone
;
1816 rr
.content
="127.0.0.1";
1817 db
->feedRecord(rr
, DNSName());
1819 rr
.qname
=DNSName("bla")+zone
;
1820 cout
<<"Committing"<<endl
;
1821 db
->commitTransaction();
1823 cout
<<"Securing zone"<<endl
;
1824 secureZone(dk
, zone
);
1825 cout
<<"Rectifying zone"<<endl
;
1826 rectifyZone(dk
, zone
);
1827 cout
<<"Checking underscore ordering"<<endl
;
1828 DNSName before
, after
;
1829 db
->getBeforeAndAfterNames(di
.id
, zone
, DNSName("z")+zone
, before
, after
);
1830 cout
<<"got '"<<before
.toString()<<"' < 'z."<<zone
.toString()<<"' < '"<<after
.toString()<<"'"<<endl
;
1831 if(before
!= DNSName("_underscore")+zone
)
1833 cout
<<"before is wrong, got '"<<before
.toString()<<"', expected '_underscore."<<zone
.toString()<<"', aborting"<<endl
;
1838 cout
<<"after is wrong, got '"<<after
.toString()<<"', expected '"<<zone
.toString()<<"', aborting"<<endl
;
1841 cout
<<"[+] ordername sorting is correct for names starting with _"<<endl
;
1843 cout
<<"End of tests, please remove "<<zone
<<" from domains+records"<<endl
;
1846 int addOrSetMeta(const DNSName
& zone
, const string
& kind
, const vector
<string
>& values
, bool clobber
) {
1847 UeberBackend
B("default");
1850 if (!B
.getDomainInfo(zone
, di
)) {
1851 cerr
<< "Invalid zone '" << zone
<< "'" << endl
;
1855 vector
<string
> all_metadata
;
1858 B
.getDomainMetadata(zone
, kind
, all_metadata
);
1861 all_metadata
.insert(all_metadata
.end(), values
.begin(), values
.end());
1863 if (!B
.setDomainMetadata(zone
, kind
, all_metadata
)) {
1864 cerr
<< "Unable to set meta for '" << zone
<< "'" << endl
;
1868 cout
<< "Set '" << zone
<< "' meta " << kind
<< " = " << boost::join(all_metadata
, ", ") << endl
;
1872 int main(int argc
, char** argv
)
1875 po::options_description
desc("Allowed options");
1877 ("help,h", "produce help message")
1878 ("version", "show version")
1879 ("verbose,v", "be verbose")
1880 ("force", "force an action")
1881 ("config-name", po::value
<string
>()->default_value(""), "virtual configuration name")
1882 ("config-dir", po::value
<string
>()->default_value(SYSCONFDIR
), "location of pdns.conf")
1883 ("commands", po::value
<vector
<string
> >());
1885 po::positional_options_description p
;
1886 p
.add("commands", -1);
1887 po::store(po::command_line_parser(argc
, argv
).options(desc
).positional(p
).run(), g_vm
);
1890 vector
<string
> cmds
;
1892 if(g_vm
.count("commands"))
1893 cmds
= g_vm
["commands"].as
<vector
<string
> >();
1895 g_verbose
= g_vm
.count("verbose");
1897 if (g_vm
.count("version")) {
1898 cout
<<"pdnsutil "<<VERSION
<<endl
;
1902 if(cmds
.empty() || g_vm
.count("help") || cmds
[0] == "help") {
1903 cout
<<"Usage: \npdnsutil [options] <command> [params ..]\n"<<endl
;
1904 cout
<<"Commands:"<<endl
;
1905 cout
<<"activate-tsig-key ZONE NAME {master|slave}"<<endl
;
1906 cout
<<" Enable TSIG authenticated AXFR using the key NAME for ZONE"<<endl
;
1907 cout
<<"activate-zone-key ZONE KEY-ID Activate the key with key id KEY-ID in ZONE"<<endl
;
1908 cout
<<"add-record ZONE NAME TYPE [ttl] content"<<endl
;
1909 cout
<<" [content..] Add one or more records to ZONE"<<endl
;
1910 cout
<<"add-zone-key ZONE {zsk|ksk} [BITS] [active|inactive]"<<endl
;
1911 cout
<<" [rsasha1|rsasha256|rsasha512|ecdsa256|ecdsa384";
1912 #if defined(HAVE_LIBSODIUM) || defined(HAVE_LIBDECAF)
1915 #ifdef HAVE_LIBDECAF
1919 cout
<<" Add a ZSK or KSK to zone and specify algo&bits"<<endl
;
1920 cout
<<"backend-cmd BACKEND CMD [CMD..] Perform one or more backend commands"<<endl
;
1921 cout
<<"b2b-migrate OLD NEW Move all data from one backend to another"<<endl
;
1922 cout
<<"bench-db [filename] Bench database backend with queries, one domain per line"<<endl
;
1923 cout
<<"check-zone ZONE Check a zone for correctness"<<endl
;
1924 cout
<<"check-all-zones [exit-on-error] Check all zones for correctness. Set exit-on-error to exit immediately"<<endl
;
1925 cout
<<" after finding an error in a zone."<<endl
;
1926 cout
<<"clear-zone ZONE Clear all records of a zone, but keep everything else"<<endl
;
1927 cout
<<"create-bind-db FNAME Create DNSSEC db for BIND backend (bind-dnssec-db)"<<endl
;
1928 cout
<<"create-slave-zone ZONE master-ip [master-ip..]"<<endl
;
1929 cout
<<" Create slave zone ZONE with master IP address master-ip"<<endl
;
1930 cout
<<"change-slave-zone-master ZONE master-ip [master-ip..]"<<endl
;
1931 cout
<<" Change slave zone ZONE master IP address to master-ip"<<endl
;
1932 cout
<<"create-zone ZONE [nsname] Create empty zone ZONE"<<endl
;
1933 cout
<<"deactivate-tsig-key ZONE NAME {master|slave}"<<endl
;
1934 cout
<<" Disable TSIG authenticated AXFR using the key NAME for ZONE"<<endl
;
1935 cout
<<"deactivate-zone-key ZONE KEY-ID Deactivate the key with key id KEY-ID in ZONE"<<endl
;
1936 cout
<<"delete-rrset ZONE NAME TYPE Delete named RRSET from zone"<<endl
;
1937 cout
<<"delete-tsig-key NAME Delete TSIG key (warning! will not unmap key!)"<<endl
;
1938 cout
<<"delete-zone ZONE Delete the zone"<<endl
;
1939 cout
<<"disable-dnssec ZONE Deactivate all keys and unset PRESIGNED in ZONE"<<endl
;
1940 cout
<<"edit-zone ZONE Edit zone contents using $EDITOR"<<endl
;
1941 cout
<<"export-zone-dnskey ZONE KEY-ID Export to stdout the public DNSKEY described"<<endl
;
1942 cout
<<"export-zone-ds ZONE Export to stdout all KSK DS records for ZONE"<<endl
;
1943 cout
<<"export-zone-key ZONE KEY-ID Export to stdout the private key described"<<endl
;
1944 cout
<<"generate-tsig-key NAME ALGORITHM Generate new TSIG key"<<endl
;
1945 cout
<<"generate-zone-key {zsk|ksk} [ALGORITHM] [BITS]"<<endl
;
1946 cout
<<" Generate a ZSK or KSK to stdout with specified ALGORITHM and BITS"<<endl
;
1947 cout
<<"get-meta ZONE [KIND ...] Get zone metadata. If no KIND given, lists all known"<<endl
;
1948 cout
<<"hash-zone-record ZONE RNAME Calculate the NSEC3 hash for RNAME in ZONE"<<endl
;
1950 cout
<<"hsm assign ZONE ALGORITHM {ksk|zsk} MODULE SLOT PIN LABEL"<<endl
<<
1951 " Assign a hardware signing module to a ZONE"<<endl
;
1952 cout
<<"hsm create-key ZONE KEY-ID [BITS] Create a key using hardware signing module for ZONE (use assign first)"<<endl
;
1953 cout
<<" BITS defaults to 2048"<<endl
;
1955 cout
<<"increase-serial ZONE Increases the SOA-serial by 1. Uses SOA-EDIT"<<endl
;
1956 cout
<<"import-tsig-key NAME ALGORITHM KEY Import TSIG key"<<endl
;
1957 cout
<<"import-zone-key ZONE FILE Import from a file a private key, ZSK or KSK"<<endl
;
1958 cout
<<" [active|inactive] [ksk|zsk] Defaults to KSK and active"<<endl
;
1959 cout
<<"load-zone ZONE FILE Load ZONE from FILE, possibly creating zone or atomically"<<endl
;
1960 cout
<<" replacing contents"<<endl
;
1961 cout
<<"list-algorithms [with-backend] List all DNSSEC algorithms supported, optionally also listing the crypto library used"<<endl
;
1962 cout
<<"list-keys [ZONE] List DNSSEC keys for ZONE. When ZONE is unset or \"all\", display all keys for all zones"<<endl
;
1963 cout
<<"list-zone ZONE List zone contents"<<endl
;
1964 cout
<<"list-all-zones [master|slave|native]"<<endl
;
1965 cout
<<" List all zone names"<<endl
;;
1966 cout
<<"list-tsig-keys List all TSIG keys"<<endl
;
1967 cout
<<"rectify-zone ZONE [ZONE ..] Fix up DNSSEC fields (order, auth)"<<endl
;
1968 cout
<<"rectify-all-zones [quiet] Rectify all zones. Optionally quiet output with errors only"<<endl
;
1969 cout
<<"remove-zone-key ZONE KEY-ID Remove key with KEY-ID from ZONE"<<endl
;
1970 cout
<<"replace-rrset ZONE NAME TYPE [ttl] Replace named RRSET from zone"<<endl
;
1971 cout
<<" content [content..]"<<endl
;
1972 cout
<<"secure-all-zones [increase-serial] Secure all zones without keys"<<endl
;
1973 cout
<<"secure-zone ZONE [ZONE ..] Add DNSSEC to zone ZONE"<<endl
;
1974 cout
<<"set-kind ZONE KIND Change the kind of ZONE to KIND (master, slave native)"<<endl
;
1975 cout
<<"set-account ZONE ACCOUNT Change the account (owner) of ZONE to ACCOUNT"<<endl
;
1976 cout
<<"set-nsec3 ZONE ['PARAMS' [narrow]] Enable NSEC3 with PARAMS. Optionally narrow"<<endl
;
1977 cout
<<"set-presigned ZONE Use presigned RRSIGs from storage"<<endl
;
1978 cout
<<"set-publish-cdnskey ZONE Enable sending CDNSKEY responses for ZONE"<<endl
;
1979 cout
<<"set-publish-cds ZONE [DIGESTALGOS] Enable sending CDS responses for ZONE, using DIGESTALGOS as signature algorithms"<<endl
;
1980 cout
<<" DIGESTALGOS should be a comma separated list of numbers, is is '1,2' by default"<<endl
;
1981 cout
<<"add-meta ZONE KIND VALUE Add zone metadata, this adds to the existing KIND"<<endl
;
1982 cout
<<" [VALUE ...]"<<endl
;
1983 cout
<<"set-meta ZONE KIND [VALUE] [VALUE] Set zone metadata, optionally providing a value. *No* value clears meta"<<endl
;
1984 cout
<<" Note - this will replace all metadata records of KIND!"<<endl
;
1985 cout
<<"show-zone ZONE Show DNSSEC (public) key details about a zone"<<endl
;
1986 cout
<<"unset-nsec3 ZONE Switch back to NSEC"<<endl
;
1987 cout
<<"unset-presigned ZONE No longer use presigned RRSIGs"<<endl
;
1988 cout
<<"unset-publish-cdnskey ZONE Disable sending CDNSKEY responses for ZONE"<<endl
;
1989 cout
<<"unset-publish-cds ZONE Disable sending CDS responses for ZONE"<<endl
;
1990 cout
<<"test-schema ZONE Test DB schema - will create ZONE"<<endl
;
1995 loadMainConfig(g_vm
["config-dir"].as
<string
>());
1997 if (cmds
[0] == "test-algorithm") {
1998 if(cmds
.size() != 2) {
1999 cerr
<< "Syntax: pdnsutil test-algorithm algonum"<<endl
;
2002 if (testAlgorithm(pdns_stou(cmds
[1])))
2007 if(cmds
[0] == "test-algorithms") {
2008 if (testAlgorithms())
2013 if(cmds
[0] == "list-algorithms") {
2014 if((cmds
.size() == 2 && cmds
[1] != "with-backend") || cmds
.size() > 2) {
2015 cerr
<<"Syntax: pdnsutil list-algorithms [with-backend]"<<endl
;
2019 cout
<<"DNSKEY algorithms supported by this installation of PowerDNS:"<<endl
;
2021 auto algosWithBackend
= DNSCryptoKeyEngine::listAllAlgosWithBackend();
2022 for (auto const algoWithBackend
: algosWithBackend
){
2023 string algoName
= DNSSECKeeper::algorithm2name(algoWithBackend
.first
);
2024 cout
<<std::to_string(algoWithBackend
.first
)<<" - "<<algoName
;
2025 if (cmds
.size() == 2 && cmds
[1] == "with-backend")
2026 cout
<<" using "<<algoWithBackend
.second
;
2034 if(cmds
[0] == "create-bind-db") {
2036 if(cmds
.size() != 2) {
2037 cerr
<< "Syntax: pdnsutil create-bind-db FNAME"<<endl
;
2041 SSQLite3
db(cmds
[1], true); // create=ok
2042 vector
<string
> statements
;
2043 stringtok(statements
, sqlCreate
, ";");
2044 for(const string
& statement
: statements
) {
2045 db
.execute(statement
);
2048 catch(SSqlException
& se
) {
2049 throw PDNSException("Error creating database in BIND backend: "+se
.txtReason());
2053 cerr
<<"bind-dnssec-db requires building PowerDNS with SQLite3"<<endl
;
2060 if (cmds
[0] == "test-schema") {
2061 if(cmds
.size() != 2) {
2062 cerr
<< "Syntax: pdnsutil test-schema ZONE"<<endl
;
2065 testSchema(dk
, DNSName(cmds
[1]));
2068 if(cmds
[0] == "rectify-zone") {
2069 if(cmds
.size() < 2) {
2070 cerr
<< "Syntax: pdnsutil rectify-zone ZONE [ZONE..]"<<endl
;
2073 unsigned int exitCode
= 0;
2074 for(unsigned int n
= 1; n
< cmds
.size(); ++n
)
2075 if (!rectifyZone(dk
, DNSName(cmds
[n
])))
2079 else if (cmds
[0] == "rectify-all-zones") {
2080 bool quiet
= (cmds
.size() >= 2 && cmds
[1] == "quiet");
2081 if (!rectifyAllZones(dk
, quiet
)) {
2085 else if(cmds
[0] == "check-zone") {
2086 if(cmds
.size() != 2) {
2087 cerr
<< "Syntax: pdnsutil check-zone ZONE"<<endl
;
2090 UeberBackend
B("default");
2091 exit(checkZone(dk
, B
, DNSName(cmds
[1])));
2093 else if(cmds
[0] == "bench-db") {
2094 dbBench(cmds
.size() > 1 ? cmds
[1] : "");
2096 else if (cmds
[0] == "check-all-zones") {
2097 bool exitOnError
= ((cmds
.size() >= 2 ? cmds
[1] : "") == "exit-on-error");
2098 exit(checkAllZones(dk
, exitOnError
));
2100 else if (cmds
[0] == "list-all-zones") {
2101 if (cmds
.size() > 2) {
2102 cerr
<< "Syntax: pdnsutil list-all-zones [master|slave|native]"<<endl
;
2105 if (cmds
.size() == 2)
2106 return listAllZones(cmds
[1]);
2107 return listAllZones();
2109 else if (cmds
[0] == "test-zone") {
2110 cerr
<< "Did you mean check-zone?"<<endl
;
2113 else if (cmds
[0] == "test-all-zones") {
2114 cerr
<< "Did you mean check-all-zones?"<<endl
;
2118 else if(cmds
[0] == "signing-server" )
2122 else if(cmds
[0] == "signing-slave")
2124 launchSigningService(0);
2127 else if(cmds
[0] == "test-speed") {
2128 if(cmds
.size() < 2) {
2129 cerr
<< "Syntax: pdnsutil test-speed numcores [signing-server]"<<endl
;
2132 testSpeed(dk
, DNSName(cmds
[1]), (cmds
.size() > 3) ? cmds
[3] : "", pdns_stou(cmds
[2]));
2134 else if(cmds
[0] == "verify-crypto") {
2135 if(cmds
.size() != 2) {
2136 cerr
<< "Syntax: pdnsutil verify-crypto FILE"<<endl
;
2139 verifyCrypto(cmds
[1]);
2141 else if(cmds
[0] == "show-zone") {
2142 if(cmds
.size() != 2) {
2143 cerr
<< "Syntax: pdnsutil show-zone ZONE"<<endl
;
2146 if (!showZone(dk
, DNSName(cmds
[1]))) return 1;
2148 else if(cmds
[0] == "export-zone-ds") {
2149 if(cmds
.size() != 2) {
2150 cerr
<< "Syntax: pdnsutil export-zone-ds ZONE"<<endl
;
2153 if (!showZone(dk
, DNSName(cmds
[1]), true)) return 1;
2155 else if(cmds
[0] == "disable-dnssec") {
2156 if(cmds
.size() != 2) {
2157 cerr
<< "Syntax: pdnsutil disable-dnssec ZONE"<<endl
;
2160 DNSName
zone(cmds
[1]);
2161 if(!disableDNSSECOnZone(dk
, zone
)) {
2162 cerr
<< "Cannot disable DNSSEC on " << zone
<< endl
;
2166 else if(cmds
[0] == "activate-zone-key") {
2167 if(cmds
.size() != 3) {
2168 cerr
<< "Syntax: pdnsutil activate-zone-key ZONE KEY-ID"<<endl
;
2171 DNSName
zone(cmds
[1]);
2172 unsigned int id
=atoi(cmds
[2].c_str()); // if you make this pdns_stou, the error gets worse
2175 cerr
<<"Invalid KEY-ID '"<<cmds
[2]<<"'"<<endl
;
2178 if (!dk
.activateKey(zone
, id
)) {
2179 cerr
<<"Activation of key failed"<<endl
;
2184 else if(cmds
[0] == "deactivate-zone-key") {
2185 if(cmds
.size() != 3) {
2186 cerr
<< "Syntax: pdnsutil deactivate-zone-key ZONE KEY-ID"<<endl
;
2189 DNSName
zone(cmds
[1]);
2190 unsigned int id
=pdns_stou(cmds
[2]);
2193 cerr
<<"Invalid KEY-ID"<<endl
;
2196 if (!dk
.deactivateKey(zone
, id
)) {
2197 cerr
<<"Deactivation of key failed"<<endl
;
2202 else if(cmds
[0] == "add-zone-key") {
2203 if(cmds
.size() < 3 ) {
2204 cerr
<< "Syntax: pdnsutil add-zone-key ZONE zsk|ksk [BITS] [active|inactive] [rsasha1|rsasha256|rsasha512|ecdsa256|ecdsa384";
2205 #if defined(HAVE_LIBSODIUM) || defined(HAVE_LIBDECAF)
2208 #ifdef HAVE_LIBDECAF
2214 DNSName
zone(cmds
[1]);
2216 UeberBackend
B("default");
2219 if (!B
.getDomainInfo(zone
, di
)){
2220 cerr
<< "No such zone in the database" << endl
;
2224 // need to get algorithm, bits & ksk or zsk from commandline
2225 bool keyOrZone
=false;
2228 int algorithm
=DNSSECKeeper::ECDSA256
;
2230 for(unsigned int n
=2; n
< cmds
.size(); ++n
) {
2231 if(pdns_iequals(cmds
[n
], "zsk"))
2233 else if(pdns_iequals(cmds
[n
], "ksk"))
2235 else if((tmp_algo
= DNSSECKeeper::shorthand2algorithm(cmds
[n
]))>0) {
2236 algorithm
= tmp_algo
;
2237 } else if(pdns_iequals(cmds
[n
], "active")) {
2239 } else if(pdns_iequals(cmds
[n
], "inactive") || pdns_iequals(cmds
[n
], "passive")) { // 'passive' eventually needs to be removed
2241 } else if(pdns_stou(cmds
[n
])) {
2242 bits
= pdns_stou(cmds
[n
]);
2244 cerr
<<"Unknown algorithm, key flag or size '"<<cmds
[n
]<<"'"<<endl
;
2245 exit(EXIT_FAILURE
);;
2249 if (!dk
.addKey(zone
, keyOrZone
, algorithm
, id
, bits
, active
)) {
2250 cerr
<<"Adding key failed, perhaps DNSSEC not enabled in configuration?"<<endl
;
2253 cerr
<<"Added a " << (keyOrZone
? "KSK" : "ZSK")<<" with algorithm = "<<algorithm
<<", active="<<active
<<endl
;
2255 cerr
<<"Requested specific key size of "<<bits
<<" bits"<<endl
;
2257 cerr
<<std::to_string(id
)<<"Key was added, but backend does not support returning of key id"<<endl
;
2258 } else if (id
< -1) {
2259 cerr
<<std::to_string(id
)<<"Key was added, but there was a failure while returning the key id"<<endl
;
2261 cout
<<std::to_string(id
)<<endl
;
2265 else if(cmds
[0] == "remove-zone-key") {
2266 if(cmds
.size() < 3) {
2267 cerr
<<"Syntax: pdnsutil remove-zone-key ZONE KEY-ID"<<endl
;
2270 DNSName
zone(cmds
[1]);
2271 unsigned int id
=pdns_stou(cmds
[2]);
2272 if (!dk
.removeKey(zone
, id
)) {
2273 cerr
<<"Cannot remove key " << id
<< " from " << zone
<<endl
;
2278 else if(cmds
[0] == "delete-zone") {
2279 if(cmds
.size() != 2) {
2280 cerr
<<"Syntax: pdnsutil delete-zone ZONE"<<endl
;
2283 exit(deleteZone(DNSName(cmds
[1])));
2285 else if(cmds
[0] == "create-zone") {
2286 if(cmds
.size() != 2 && cmds
.size()!=3 ) {
2287 cerr
<<"Syntax: pdnsutil create-zone ZONE [nsname]"<<endl
;
2290 exit(createZone(DNSName(cmds
[1]), cmds
.size() > 2 ? DNSName(cmds
[2]): DNSName()));
2292 else if(cmds
[0] == "create-slave-zone") {
2293 if(cmds
.size() < 3 ) {
2294 cerr
<<"Syntax: pdnsutil create-slave-zone ZONE master-ip [master-ip..]"<<endl
;
2297 exit(createSlaveZone(cmds
));
2299 else if(cmds
[0] == "change-slave-zone-master") {
2300 if(cmds
.size() < 3 ) {
2301 cerr
<<"Syntax: pdnsutil change-slave-zone-master ZONE master-ip [master-ip..]"<<endl
;
2304 exit(changeSlaveZoneMaster(cmds
));
2306 else if(cmds
[0] == "add-record") {
2307 if(cmds
.size() < 5) {
2308 cerr
<<"Syntax: pdnsutil add-record ZONE name type [ttl] \"content\" [\"content\"...]"<<endl
;
2311 exit(addOrReplaceRecord(true, cmds
));
2313 else if(cmds
[0] == "replace-rrset") {
2314 if(cmds
.size() < 5) {
2315 cerr
<<"Syntax: pdnsutil replace-rrset ZONE name type [ttl] \"content\" [\"content\"...]"<<endl
;
2318 exit(addOrReplaceRecord(false , cmds
));
2320 else if(cmds
[0] == "delete-rrset") {
2321 if(cmds
.size() != 4) {
2322 cerr
<<"Syntax: pdnsutil delete-rrset ZONE name type"<<endl
;
2325 exit(deleteRRSet(cmds
[1], cmds
[2], cmds
[3]));
2327 else if(cmds
[0] == "list-zone") {
2328 if(cmds
.size() != 2) {
2329 cerr
<<"Syntax: pdnsutil list-zone ZONE"<<endl
;
2335 exit(listZone(DNSName(cmds
[1])));
2337 else if(cmds
[0] == "edit-zone") {
2338 if(cmds
.size() != 2) {
2339 cerr
<<"Syntax: pdnsutil edit-zone ZONE"<<endl
;
2345 exit(editZone(dk
, DNSName(cmds
[1])));
2347 else if(cmds
[0] == "clear-zone") {
2348 if(cmds
.size() != 2) {
2349 cerr
<<"Syntax: pdnsutil edit-zone ZONE"<<endl
;
2355 exit(clearZone(dk
, DNSName(cmds
[1])));
2357 else if(cmds
[0] == "list-keys") {
2358 if(cmds
.size() > 2) {
2359 cerr
<<"Syntax: pdnsutil list-keys [ZONE]"<<endl
;
2362 string zname
= (cmds
.size() == 2) ? cmds
[1] : "all";
2363 exit(listKeys(zname
, dk
));
2365 else if(cmds
[0] == "load-zone") {
2366 if(cmds
.size() < 3) {
2367 cerr
<<"Syntax: pdnsutil load-zone ZONE FILENAME [ZONE FILENAME] .."<<endl
;
2373 for(size_t n
=1; n
+ 2 <= cmds
.size(); n
+=2)
2374 loadZone(DNSName(cmds
[n
]), cmds
[n
+1]);
2377 else if(cmds
[0] == "secure-zone") {
2378 if(cmds
.size() < 2) {
2379 cerr
<< "Syntax: pdnsutil secure-zone ZONE"<<endl
;
2382 vector
<DNSName
> mustRectify
;
2383 unsigned int zoneErrors
=0;
2384 for(unsigned int n
= 1; n
< cmds
.size(); ++n
) {
2385 DNSName
zone(cmds
[n
]);
2386 dk
.startTransaction(zone
, -1);
2387 if(secureZone(dk
, zone
)) {
2388 mustRectify
.push_back(zone
);
2392 dk
.commitTransaction();
2395 for(const auto& zone
: mustRectify
)
2396 rectifyZone(dk
, zone
);
2403 else if (cmds
[0] == "secure-all-zones") {
2404 if (cmds
.size() >= 2 && !pdns_iequals(cmds
[1], "increase-serial")) {
2405 cerr
<< "Syntax: pdnsutil secure-all-zones [increase-serial]"<<endl
;
2409 UeberBackend
B("default");
2411 vector
<DomainInfo
> domainInfo
;
2412 B
.getAllDomains(&domainInfo
);
2414 unsigned int zonesSecured
=0, zoneErrors
=0;
2415 for(DomainInfo di
: domainInfo
) {
2416 if(!dk
.isSecuredZone(di
.zone
)) {
2417 cout
<<"Securing "<<di
.zone
<<": ";
2418 if (secureZone(dk
, di
.zone
)) {
2420 if (cmds
.size() == 2) {
2421 if (!increaseSerial(di
.zone
, dk
))
2430 cout
<<"Secured: "<<zonesSecured
<<" zones. Errors: "<<zoneErrors
<<endl
;
2437 else if(cmds
[0]=="set-kind") {
2438 if(cmds
.size() != 3) {
2439 cerr
<<"Syntax: pdnsutil set-kind ZONE KIND"<<endl
;
2442 DNSName
zone(cmds
[1]);
2443 auto kind
=DomainInfo::stringToKind(cmds
[2]);
2444 exit(setZoneKind(zone
, kind
));
2446 else if(cmds
[0]=="set-account") {
2447 if(cmds
.size() != 3) {
2448 cerr
<<"Syntax: pdnsutil set-account ZONE ACCOUNT"<<endl
;
2451 DNSName
zone(cmds
[1]);
2452 exit(setZoneAccount(zone
, cmds
[2]));
2454 else if(cmds
[0]=="set-nsec3") {
2455 if(cmds
.size() < 2) {
2456 cerr
<<"Syntax: pdnsutil set-nsec3 ZONE 'params' [narrow]"<<endl
;
2459 string nsec3params
= cmds
.size() > 2 ? cmds
[2] : "1 0 1 ab";
2460 bool narrow
= cmds
.size() > 3 && cmds
[3]=="narrow";
2461 NSEC3PARAMRecordContent
ns3pr(nsec3params
);
2463 DNSName
zone(cmds
[1]);
2464 if (zone
.wirelength() > 222) {
2465 cerr
<<"Cannot enable NSEC3 for " << zone
<< " as it is too long (" << zone
.wirelength() << " bytes, maximum is 222 bytes)"<<endl
;
2468 if(ns3pr
.d_algorithm
!= 1) {
2469 cerr
<<"NSEC3PARAM algorithm set to '"<<std::to_string(ns3pr
.d_algorithm
)<<"', but '1' is the only valid value"<<endl
;
2470 return EXIT_FAILURE
;
2472 if (! dk
.setNSEC3PARAM(zone
, ns3pr
, narrow
)) {
2473 cerr
<<"Cannot set NSEC3 param for " << zone
<< endl
;
2478 cerr
<<"NSEC3 set, ";
2480 cerr
<<"NSEC3 (opt-out) set, ";
2482 if(dk
.isSecuredZone(zone
))
2483 cerr
<<"please rectify your zone if your backend needs it"<<endl
;
2485 cerr
<<"please secure and rectify your zone."<<endl
;
2489 else if(cmds
[0]=="set-presigned") {
2490 if(cmds
.size() < 2) {
2491 cerr
<<"Syntax: pdnsutil set-presigned ZONE"<<endl
;
2494 if (! dk
.setPresigned(DNSName(cmds
[1]))) {
2495 cerr
<< "Could not set presigned for " << cmds
[1] << " (is DNSSEC enabled in your backend?)" << endl
;
2500 else if(cmds
[0]=="set-publish-cdnskey") {
2501 if(cmds
.size() < 2) {
2502 cerr
<<"Syntax: pdnsutil set-publish-cdnskey ZONE"<<endl
;
2505 if (! dk
.setPublishCDNSKEY(DNSName(cmds
[1]))) {
2506 cerr
<< "Could not set publishing for CDNSKEY records for "<< cmds
[1]<<endl
;
2511 else if(cmds
[0]=="set-publish-cds") {
2512 if(cmds
.size() < 2) {
2513 cerr
<<"Syntax: pdnsutil set-publish-cds ZONE [DIGESTALGOS]"<<endl
;
2517 // If DIGESTALGOS is unset
2518 if(cmds
.size() == 2)
2519 cmds
.push_back("1,2");
2521 if (! dk
.setPublishCDS(DNSName(cmds
[1]), cmds
[2])) {
2522 cerr
<< "Could not set publishing for CDS records for "<< cmds
[1]<<endl
;
2527 else if(cmds
[0]=="unset-presigned") {
2528 if(cmds
.size() < 2) {
2529 cerr
<<"Syntax: pdnsutil unset-presigned ZONE"<<endl
;
2532 if (! dk
.unsetPresigned(DNSName(cmds
[1]))) {
2533 cerr
<< "Could not unset presigned on for " << cmds
[1] << endl
;
2538 else if(cmds
[0]=="unset-publish-cdnskey") {
2539 if(cmds
.size() < 2) {
2540 cerr
<<"Syntax: pdnsutil unset-publish-cdnskey ZONE"<<endl
;
2543 if (! dk
.unsetPublishCDNSKEY(DNSName(cmds
[1]))) {
2544 cerr
<< "Could not unset publishing for CDNSKEY records for "<< cmds
[1]<<endl
;
2549 else if(cmds
[0]=="unset-publish-cds") {
2550 if(cmds
.size() < 2) {
2551 cerr
<<"Syntax: pdnsutil unset-publish-cds ZONE"<<endl
;
2554 if (! dk
.unsetPublishCDS(DNSName(cmds
[1]))) {
2555 cerr
<< "Could not unset publishing for CDS records for "<< cmds
[1]<<endl
;
2560 else if(cmds
[0]=="hash-zone-record") {
2561 if(cmds
.size() < 3) {
2562 cerr
<<"Syntax: pdnsutil hash-zone-record ZONE RNAME"<<endl
;
2565 DNSName
zone(cmds
[1]);
2566 DNSName
record(cmds
[2]);
2567 NSEC3PARAMRecordContent ns3pr
;
2569 if(!dk
.getNSEC3PARAM(zone
, &ns3pr
, &narrow
)) {
2570 cerr
<<"The '"<<zone
<<"' zone does not use NSEC3"<<endl
;
2574 cerr
<<"The '"<<zone
<<"' zone uses narrow NSEC3, but calculating hash anyhow"<<endl
;
2577 cout
<<toBase32Hex(hashQNameWithSalt(ns3pr
, record
))<<endl
;
2579 else if(cmds
[0]=="unset-nsec3") {
2580 if(cmds
.size() < 2) {
2581 cerr
<<"Syntax: pdnsutil unset-nsec3 ZONE"<<endl
;
2584 if ( ! dk
.unsetNSEC3PARAM(DNSName(cmds
[1]))) {
2585 cerr
<<"Cannot unset NSEC3 param for " << cmds
[1] << endl
;
2590 else if(cmds
[0]=="export-zone-key") {
2591 if(cmds
.size() < 3) {
2592 cerr
<<"Syntax: pdnsutil export-zone-key ZONE KEY-ID"<<endl
;
2596 string zone
=cmds
[1];
2597 unsigned int id
=pdns_stou(cmds
[2]);
2598 DNSSECPrivateKey dpk
=dk
.getKeyById(DNSName(zone
), id
);
2599 cout
<< dpk
.getKey()->convertToISC() <<endl
;
2601 else if(cmds
[0]=="increase-serial") {
2602 if (cmds
.size() < 2) {
2603 cerr
<<"Syntax: pdnsutil increase-serial ZONE"<<endl
;
2606 return increaseSerial(DNSName(cmds
[1]), dk
);
2608 else if(cmds
[0]=="import-zone-key-pem") {
2609 if(cmds
.size() < 4) {
2610 cerr
<<"Syntax: pdnsutil import-zone-key-pem ZONE FILE ALGORITHM {ksk|zsk}"<<endl
;
2613 string zone
=cmds
[1];
2614 string fname
=cmds
[2];
2616 ifstream
ifs(fname
.c_str());
2617 string tmp
, interim
, raw
;
2618 while(getline(ifs
, line
)) {
2624 B64Decode(interim
, raw
);
2625 DNSSECPrivateKey dpk
;
2626 DNSKEYRecordContent drc
;
2627 shared_ptr
<DNSCryptoKeyEngine
> key(DNSCryptoKeyEngine::makeFromPEMString(drc
, raw
));
2630 dpk
.d_algorithm
= pdns_stou(cmds
[3]);
2632 if(dpk
.d_algorithm
== DNSSECKeeper::RSASHA1NSEC3SHA1
)
2633 dpk
.d_algorithm
= DNSSECKeeper::RSASHA1
;
2635 cerr
<<(int)dpk
.d_algorithm
<<endl
;
2637 if(cmds
.size() > 4) {
2638 if(pdns_iequals(cmds
[4], "ZSK"))
2640 else if(pdns_iequals(cmds
[4], "KSK"))
2643 cerr
<<"Unknown key flag '"<<cmds
[4]<<"'"<<endl
;
2648 dpk
.d_flags
= 257; // ksk
2651 if (!dk
.addKey(DNSName(zone
), dpk
, id
)) {
2652 cerr
<<"Adding key failed, perhaps DNSSEC not enabled in configuration?"<<endl
;
2656 cerr
<<std::to_string(id
)<<"Key was added, but backend does not support returning of key id"<<endl
;
2657 } else if (id
< -1) {
2658 cerr
<<std::to_string(id
)<<"Key was added, but there was a failure while returning the key id"<<endl
;
2660 cout
<<std::to_string(id
)<<endl
;
2664 else if(cmds
[0]=="import-zone-key") {
2665 if(cmds
.size() < 3) {
2666 cerr
<<"Syntax: pdnsutil import-zone-key ZONE FILE [ksk|zsk] [active|inactive]"<<endl
;
2669 string zone
=cmds
[1];
2670 string fname
=cmds
[2];
2671 DNSSECPrivateKey dpk
;
2672 DNSKEYRecordContent drc
;
2673 shared_ptr
<DNSCryptoKeyEngine
> key(DNSCryptoKeyEngine::makeFromISCFile(drc
, fname
.c_str()));
2675 dpk
.d_algorithm
= drc
.d_algorithm
;
2677 if(dpk
.d_algorithm
== DNSSECKeeper::RSASHA1NSEC3SHA1
)
2678 dpk
.d_algorithm
= DNSSECKeeper::RSASHA1
;
2683 for(unsigned int n
= 3; n
< cmds
.size(); ++n
) {
2684 if(pdns_iequals(cmds
[n
], "ZSK"))
2686 else if(pdns_iequals(cmds
[n
], "KSK"))
2688 else if(pdns_iequals(cmds
[n
], "active"))
2690 else if(pdns_iequals(cmds
[n
], "passive") || pdns_iequals(cmds
[n
], "inactive")) // passive eventually needs to be removed
2693 cerr
<<"Unknown key flag '"<<cmds
[n
]<<"'"<<endl
;
2698 if (!dk
.addKey(DNSName(zone
), dpk
, id
, active
)) {
2699 cerr
<<"Adding key failed, perhaps DNSSEC not enabled in configuration?"<<endl
;
2703 cerr
<<std::to_string(id
)<<"Key was added, but backend does not support returning of key id"<<endl
;
2704 } else if (id
< -1) {
2705 cerr
<<std::to_string(id
)<<"Key was added, but there was a failure while returning the key id"<<endl
;
2707 cout
<<std::to_string(id
)<<endl
;
2710 else if(cmds
[0]=="export-zone-dnskey") {
2711 if(cmds
.size() < 3) {
2712 cerr
<<"Syntax: pdnsutil export-zone-dnskey ZONE KEY-ID"<<endl
;
2716 DNSName
zone(cmds
[1]);
2717 unsigned int id
=pdns_stou(cmds
[2]);
2718 DNSSECPrivateKey dpk
=dk
.getKeyById(zone
, id
);
2719 cout
<< zone
<<" IN DNSKEY "<<dpk
.getDNSKEY().getZoneRepresentation() <<endl
;
2721 else if(cmds
[0] == "generate-zone-key") {
2722 if(cmds
.size() < 2 ) {
2723 cerr
<< "Syntax: pdnsutil generate-zone-key zsk|ksk [rsasha1|rsasha256|rsasha512|ecdsa256|ecdsa384";
2724 #if defined(HAVE_LIBSODIUM) || defined(HAVE_LIBDECAF)
2727 #ifdef HAVE_LIBDECAF
2730 cerr
<< "] [bits]"<<endl
;
2733 // need to get algorithm, bits & ksk or zsk from commandline
2734 bool keyOrZone
=false;
2737 int algorithm
=DNSSECKeeper::ECDSA256
;
2738 for(unsigned int n
=1; n
< cmds
.size(); ++n
) {
2739 if(pdns_iequals(cmds
[n
], "zsk"))
2741 else if(pdns_iequals(cmds
[n
], "ksk"))
2743 else if((tmp_algo
= DNSSECKeeper::shorthand2algorithm(cmds
[n
]))>0) {
2744 algorithm
= tmp_algo
;
2745 } else if(pdns_stou(cmds
[n
]))
2746 bits
= pdns_stou(cmds
[n
]);
2748 cerr
<<"Unknown algorithm, key flag or size '"<<cmds
[n
]<<"'"<<endl
;
2752 cerr
<<"Generating a " << (keyOrZone
? "KSK" : "ZSK")<<" with algorithm = "<<algorithm
<<endl
;
2754 cerr
<<"Requesting specific key size of "<<bits
<<" bits"<<endl
;
2756 DNSSECPrivateKey dspk
;
2757 shared_ptr
<DNSCryptoKeyEngine
> dpk(DNSCryptoKeyEngine::make(algorithm
));
2760 bits
= keyOrZone
? 2048 : 1024;
2762 if(algorithm
== DNSSECKeeper::ECCGOST
|| algorithm
== DNSSECKeeper::ECDSA256
|| algorithm
== DNSSECKeeper::ED25519
)
2764 else if(algorithm
== DNSSECKeeper::ECDSA384
)
2766 else if(algorithm
== DNSSECKeeper::ED448
)
2769 throw runtime_error("Can not guess key size for algorithm "+std::to_string(algorithm
));
2775 dspk
.d_algorithm
= algorithm
;
2776 dspk
.d_flags
= keyOrZone
? 257 : 256;
2778 // print key to stdout
2779 cout
<< "Flags: " << dspk
.d_flags
<< endl
<<
2780 dspk
.getKey()->convertToISC() << endl
;
2781 } else if (cmds
[0]=="generate-tsig-key") {
2782 string usage
= "Syntax: " + cmds
[0] + " name (hmac-md5|hmac-sha1|hmac-sha224|hmac-sha256|hmac-sha384|hmac-sha512)";
2783 if (cmds
.size() < 3) {
2784 cerr
<< usage
<< endl
;
2787 DNSName
name(cmds
[1]);
2788 DNSName
algo(cmds
[2]);
2791 key
= makeTSIGKey(algo
);
2792 } catch(const PDNSException
& e
) {
2793 cerr
<< "Could not create new TSIG key " << name
<< " " << algo
<< ": "<< e
.reason
<< endl
;
2797 UeberBackend
B("default");
2798 if (B
.setTSIGKey(name
, DNSName(algo
), key
)) { // you are feeling bored, put up DNSName(algo) up earlier
2799 cout
<< "Create new TSIG key " << name
<< " " << algo
<< " " << key
<< endl
;
2801 cerr
<< "Failure storing new TSIG key " << name
<< " " << algo
<< " " << key
<< endl
;
2805 } else if (cmds
[0]=="import-tsig-key") {
2806 if (cmds
.size() < 4) {
2807 cerr
<< "Syntax: " << cmds
[0] << " name algorithm key" << endl
;
2810 DNSName
name(cmds
[1]);
2811 string algo
= cmds
[2];
2812 string key
= cmds
[3];
2814 UeberBackend
B("default");
2815 if (B
.setTSIGKey(name
, DNSName(algo
), key
)) {
2816 cout
<< "Imported TSIG key " << name
<< " " << algo
<< endl
;
2818 cerr
<< "Failure importing TSIG key " << name
<< " " << algo
<< endl
;
2822 } else if (cmds
[0]=="delete-tsig-key") {
2823 if (cmds
.size() < 2) {
2824 cerr
<< "Syntax: " << cmds
[0] << " name" << endl
;
2827 DNSName
name(cmds
[1]);
2829 UeberBackend
B("default");
2830 if (B
.deleteTSIGKey(name
)) {
2831 cout
<< "Deleted TSIG key " << name
<< endl
;
2833 cerr
<< "Failure deleting TSIG key " << name
<< endl
;
2837 } else if (cmds
[0]=="list-tsig-keys") {
2838 std::vector
<struct TSIGKey
> keys
;
2839 UeberBackend
B("default");
2840 if (B
.getTSIGKeys(keys
)) {
2841 for(const TSIGKey
&key
: keys
) {
2842 cout
<< key
.name
.toString() << " " << key
.algorithm
.toString() << " " << key
.key
<< endl
;
2846 } else if (cmds
[0]=="activate-tsig-key") {
2848 if (cmds
.size() < 4) {
2849 cerr
<< "Syntax: " << cmds
[0] << " ZONE NAME {master|slave}" << endl
;
2852 DNSName
zname(cmds
[1]);
2853 string name
= cmds
[2];
2854 if (cmds
[3] == "master")
2855 metaKey
= "TSIG-ALLOW-AXFR";
2856 else if (cmds
[3] == "slave")
2857 metaKey
= "AXFR-MASTER-TSIG";
2859 cerr
<< "Invalid parameter '" << cmds
[3] << "', expected master or slave" << endl
;
2862 UeberBackend
B("default");
2863 std::vector
<std::string
> meta
;
2864 if (!B
.getDomainMetadata(zname
, metaKey
, meta
)) {
2865 cerr
<< "Failure enabling TSIG key " << name
<< " for " << zname
<< endl
;
2869 for(std::string tmpname
: meta
) {
2870 if (tmpname
== name
) { found
= true; break; }
2872 if (!found
) meta
.push_back(name
);
2873 if (B
.setDomainMetadata(zname
, metaKey
, meta
)) {
2874 cout
<< "Enabled TSIG key " << name
<< " for " << zname
<< endl
;
2876 cerr
<< "Failure enabling TSIG key " << name
<< " for " << zname
<< endl
;
2880 } else if (cmds
[0]=="deactivate-tsig-key") {
2882 if (cmds
.size() < 4) {
2883 cerr
<< "Syntax: " << cmds
[0] << " ZONE NAME {master|slave}" << endl
;
2886 DNSName
zname(cmds
[1]);
2887 string name
= cmds
[2];
2888 if (cmds
[3] == "master")
2889 metaKey
= "TSIG-ALLOW-AXFR";
2890 else if (cmds
[3] == "slave")
2891 metaKey
= "AXFR-MASTER-TSIG";
2893 cerr
<< "Invalid parameter '" << cmds
[3] << "', expected master or slave" << endl
;
2897 UeberBackend
B("default");
2898 std::vector
<std::string
> meta
;
2899 if (!B
.getDomainMetadata(zname
, metaKey
, meta
)) {
2900 cerr
<< "Failure disabling TSIG key " << name
<< " for " << zname
<< endl
;
2903 std::vector
<std::string
>::iterator iter
= meta
.begin();
2904 for(;iter
!= meta
.end(); ++iter
) if (*iter
== name
) break;
2905 if (iter
!= meta
.end()) meta
.erase(iter
);
2906 if (B
.setDomainMetadata(zname
, metaKey
, meta
)) {
2907 cout
<< "Disabled TSIG key " << name
<< " for " << zname
<< endl
;
2909 cerr
<< "Failure disabling TSIG key " << name
<< " for " << zname
<< endl
;
2913 } else if (cmds
[0]=="get-meta") {
2914 UeberBackend
B("default");
2915 if (cmds
.size() < 2) {
2916 cerr
<< "Syntax: " << cmds
[0] << " zone [kind kind ..]" << endl
;
2919 DNSName
zone(cmds
[1]);
2920 vector
<string
> keys
;
2923 if (!B
.getDomainInfo(zone
, di
)) {
2924 cerr
<< "Invalid zone '" << zone
<< "'" << endl
;
2928 if (cmds
.size() > 2) {
2929 keys
.assign(cmds
.begin() + 2, cmds
.end());
2930 std::cout
<< "Metadata for '" << zone
<< "'" << endl
;
2931 for(const string kind
: keys
) {
2932 vector
<string
> meta
;
2934 if (B
.getDomainMetadata(zone
, kind
, meta
)) {
2935 cout
<< kind
<< " = " << boost::join(meta
, ", ") << endl
;
2939 std::map
<std::string
, std::vector
<std::string
> > meta
;
2940 std::cout
<< "Metadata for '" << zone
<< "'" << endl
;
2941 B
.getAllDomainMetadata(zone
, meta
);
2942 for(const auto& each_meta
: meta
) {
2943 cout
<< each_meta
.first
<< " = " << boost::join(each_meta
.second
, ", ") << endl
;
2948 } else if (cmds
[0]=="set-meta" || cmds
[0]=="add-meta") {
2949 if (cmds
.size() < 3) {
2950 cerr
<< "Syntax: " << cmds
[0] << " ZONE KIND [VALUE VALUE ..]" << endl
;
2953 DNSName
zone(cmds
[1]);
2954 string kind
= cmds
[2];
2955 static vector
<string
> multiMetaWhitelist
= {"ALLOW-AXFR-FROM", "ALLOW-DNSUPDATE-FROM",
2956 "ALSO-NOTIFY", "TSIG-ALLOW-AXFR", "TSIG-ALLOW-DNSUPDATE", "GSS-ALLOW-AXFR-PRINCIPAL",
2958 bool clobber
= true;
2959 if (cmds
[0] == "add-meta") {
2961 if (find(multiMetaWhitelist
.begin(), multiMetaWhitelist
.end(), kind
) == multiMetaWhitelist
.end() && kind
.find("X-") != 0) {
2962 cerr
<<"Refusing to add metadata to single-value metadata "<<kind
<<endl
;
2966 vector
<string
> meta(cmds
.begin() + 3, cmds
.end());
2967 return addOrSetMeta(zone
, kind
, meta
, clobber
);
2968 } else if (cmds
[0]=="hsm") {
2970 UeberBackend
B("default");
2971 if (cmds
.size() < 2) {
2972 cerr
<< "Missing sub-command for pdnsutil hsm"<< std::endl
;
2974 } else if (cmds
[1] == "assign") {
2975 DNSCryptoKeyEngine::storvector_t storvect
;
2977 std::vector
<DNSBackend::KeyData
> keys
;
2979 if (cmds
.size() < 9) {
2980 std::cout
<< "Usage: pdnsutil hsm assign ZONE ALGORITHM {ksk|zsk} MODULE TOKEN PIN LABEL (PUBLABEL)" << std::endl
;
2984 DNSName
zone(cmds
[2]);
2987 if (!B
.getDomainInfo(zone
, di
)) {
2988 cerr
<< "Unable to assign module to unknown zone '" << zone
<< "'" << std::endl
;
2992 int algorithm
= DNSSECKeeper::shorthand2algorithm(cmds
[3]);
2994 cerr
<< "Unable to use unknown algorithm '" << cmds
[3] << "'" << std::endl
;
2999 bool keyOrZone
= (cmds
[4] == "ksk" ? true : false);
3000 string module
= cmds
[5];
3001 string slot
= cmds
[6];
3002 string pin
= cmds
[7];
3003 string label
= cmds
[8];
3005 if (cmds
.size() > 9)
3006 pub_label
= cmds
[9];
3010 std::ostringstream iscString
;
3011 iscString
<< "Private-key-format: v1.2" << std::endl
<<
3012 "Algorithm: " << algorithm
<< std::endl
<<
3013 "Engine: " << module
<< std::endl
<<
3014 "Slot: " << slot
<< std::endl
<<
3015 "PIN: " << pin
<< std::endl
<<
3016 "Label: " << label
<< std::endl
<<
3017 "PubLabel: " << pub_label
<< std::endl
;
3019 DNSKEYRecordContent drc
;
3020 DNSSECPrivateKey dpk
;
3021 dpk
.d_flags
= (keyOrZone
? 257 : 256);
3023 shared_ptr
<DNSCryptoKeyEngine
> dke(DNSCryptoKeyEngine::makeFromISCString(drc
, iscString
.str()));
3024 if(!dke
->checkKey()) {
3025 cerr
<< "Invalid DNS Private Key in engine " << module
<< " slot " << slot
<< std::endl
;
3030 // make sure this key isn't being reused.
3031 B
.getDomainKeys(zone
, keys
);
3034 for(DNSBackend::KeyData
& kd
: keys
) {
3035 if (kd
.content
== iscString
.str()) {
3036 // it's this one, I guess...
3043 cerr
<< "You have already assigned this key with ID=" << id
<< std::endl
;
3047 if (!dk
.addKey(zone
, dpk
, id
)) {
3048 cerr
<< "Unable to assign module slot to zone" << std::endl
;
3052 cerr
<< "Module " << module
<< " slot " << slot
<< " assigned to " << zone
<< " with key id " << id
<< endl
;
3055 } else if (cmds
[1] == "create-key") {
3057 if (cmds
.size() < 4) {
3058 cerr
<< "Usage: pdnsutil hsm create-key ZONE KEY-ID [BITS]" << endl
;
3062 DNSName
zone(cmds
[2]);
3066 if (!B
.getDomainInfo(zone
, di
)) {
3067 cerr
<< "Unable to create key for unknown zone '" << zone
<< "'" << std::endl
;
3071 id
= pdns_stou(cmds
[3]);
3072 std::vector
<DNSBackend::KeyData
> keys
;
3073 if (!B
.getDomainKeys(zone
, keys
)) {
3074 cerr
<< "No keys found for zone " << zone
<< std::endl
;
3078 std::shared_ptr
<DNSCryptoKeyEngine
> dke
= nullptr;
3079 // lookup correct key
3080 for(DNSBackend::KeyData
&kd
: keys
) {
3083 DNSKEYRecordContent dkrc
;
3084 dke
= DNSCryptoKeyEngine::makeFromISCString(dkrc
, kd
.content
);
3089 cerr
<< "Could not find key with ID " << id
<< endl
;
3092 if (cmds
.size() > 4) {
3093 bits
= pdns_stou(cmds
[4]);
3096 cerr
<< "Invalid bit size " << bits
<< "given, must be positive integer";
3101 } catch (PDNSException
& e
) {
3102 cerr
<< e
.reason
<< endl
;
3106 cerr
<< "Key of size " << bits
<< " created" << std::endl
;
3110 cerr
<<"PKCS#11 support not enabled"<<endl
;
3113 } else if (cmds
[0] == "b2b-migrate") {
3114 if (cmds
.size() < 3) {
3115 cerr
<<"Usage: b2b-migrate OLD NEW"<<endl
;
3119 DNSBackend
*src
,*tgt
;
3122 for(DNSBackend
*b
: BackendMakers().all()) {
3123 if (b
->getPrefix() == cmds
[1]) src
= b
;
3124 if (b
->getPrefix() == cmds
[2]) tgt
= b
;
3127 cerr
<<"Unknown source backend '"<<cmds
[1]<<"'"<<endl
;
3131 cerr
<<"Unknown target backend '"<<cmds
[2]<<"'"<<endl
;
3135 cout
<<"Moving zone(s) from "<<src
->getPrefix()<<" to "<<tgt
->getPrefix()<<endl
;
3137 vector
<DomainInfo
> domains
;
3139 tgt
->getAllDomains(&domains
, true);
3140 if (domains
.size()>0)
3141 throw PDNSException("Target backend has domain(s), please clean it first");
3143 src
->getAllDomains(&domains
, true);
3145 for(const DomainInfo
& di
: domains
) {
3148 DNSResourceRecord rr
;
3149 cout
<<"Processing '"<<di
.zone
<<"'"<<endl
;
3151 if (!tgt
->createDomain(di
.zone
)) throw PDNSException("Failed to create zone");
3152 if (!tgt
->getDomainInfo(di
.zone
, di_new
)) throw PDNSException("Failed to create zone");
3153 tgt
->setKind(di_new
.zone
, di
.kind
);
3154 tgt
->setAccount(di_new
.zone
,di
.account
);
3157 for(const auto& master
: di
.masters
) {
3161 masters
+= master
.toStringWithPortExcept(53);
3163 tgt
->setMaster(di_new
.zone
, masters
);
3165 if (!src
->list(di
.zone
, di
.id
, true)) throw PDNSException("Failed to list records");
3167 while(src
->get(rr
)) {
3168 rr
.domain_id
= di_new
.id
;
3169 if (!tgt
->feedRecord(rr
, DNSName())) throw PDNSException("Failed to feed record");
3174 if (src
->listComments(di
.id
)) {
3176 while(src
->getComment(c
)) {
3177 c
.domain_id
= di_new
.id
;
3178 tgt
->feedComment(c
);
3184 std::map
<std::string
, std::vector
<std::string
> > meta
;
3185 if (src
->getAllDomainMetadata(di
.zone
, meta
)) {
3186 for (const auto& i
: meta
) {
3187 if (!tgt
->setDomainMetadata(di
.zone
, i
.first
, i
.second
)) throw PDNSException("Failed to feed domain metadata");
3193 // temp var for KeyID
3195 std::vector
<DNSBackend::KeyData
> keys
;
3196 if (src
->getDomainKeys(di
.zone
, keys
)) {
3197 for(const DNSBackend::KeyData
& k
: keys
) {
3198 tgt
->addDomainKey(di
.zone
, k
, keyID
);
3202 cout
<<"Moved "<<nr
<<" record(s), "<<nc
<<" comment(s), "<<nm
<<" metadata(s) and "<<nk
<<" cryptokey(s)"<<endl
;
3207 std::vector
<struct TSIGKey
> tkeys
;
3208 if (src
->getTSIGKeys(tkeys
)) {
3209 for(auto& tk
: tkeys
) {
3210 if (!tgt
->setTSIGKey(tk
.name
, tk
.algorithm
, tk
.key
)) throw PDNSException("Failed to feed TSIG key");
3214 cout
<<"Moved "<<ntk
<<" TSIG key(s)"<<endl
;
3216 cout
<<"Remember to drop the old backend and run rectify-all-zones"<<endl
;
3219 } else if (cmds
[0] == "backend-cmd") {
3220 if (cmds
.size() < 3) {
3221 cerr
<<"Usage: backend-cmd BACKEND CMD [CMD..]"<<endl
;
3228 for(DNSBackend
*b
: BackendMakers().all()) {
3229 if (b
->getPrefix() == cmds
[1]) db
= b
;
3233 cerr
<<"Unknown backend '"<<cmds
[1]<<"'"<<endl
;
3237 for(auto i
=next(begin(cmds
),2); i
!= end(cmds
); ++i
) {
3238 cerr
<<"== "<<*i
<<endl
;
3239 cout
<<db
->directBackendCmd(*i
);
3244 cerr
<<"Unknown command '"<<cmds
[0] <<"'"<< endl
;
3249 catch(PDNSException
& ae
) {
3250 cerr
<<"Error: "<<ae
.reason
<<endl
;
3253 catch(std::exception
& e
) {
3254 cerr
<<"Error: "<<e
.what()<<endl
;
3259 cerr
<<"Caught an unknown exception"<<endl
;