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"
23 #include "ipcipher.hh"
25 #include <termios.h> //termios, TCSANOW, ECHO, ICANON
26 #include "opensslsigners.hh"
31 #include "ssqlite3.hh"
32 #include "bind-dnssec.schema.sqlite3.sql.h"
39 namespace po
= boost::program_options
;
40 po::variables_map g_vm
;
42 string s_programname
="pdns";
54 void loadMainConfig(const std::string
& configdir
)
56 ::arg().set("config-dir","Location of configuration directory (pdns.conf)")=configdir
;
57 ::arg().set("default-ttl","Seconds a result is valid if not set otherwise")="3600";
58 ::arg().set("launch","Which backends to launch");
59 ::arg().set("dnssec","if we should do dnssec")="true";
60 ::arg().set("config-name","Name of this virtual configuration - will rename the binary image")=g_vm
["config-name"].as
<string
>();
61 ::arg().setCmd("help","Provide a helpful message");
62 ::arg().set("load-modules","Load this module - supply absolute or relative path")="";
63 //::arg().laxParse(argc,argv);
65 if(::arg().mustDo("help")) {
66 cout
<<"syntax:"<<endl
<<endl
;
67 cout
<<::arg().helpstring(::arg()["help"])<<endl
;
71 if(::arg()["config-name"]!="")
72 s_programname
+="-"+::arg()["config-name"];
74 string configname
=::arg()["config-dir"]+"/"+s_programname
+".conf";
75 cleanSlashes(configname
);
77 ::arg().set("resolver","Use this resolver for ALIAS and the internal stub resolver")="no";
78 ::arg().set("default-ksk-algorithm","Default KSK algorithm")="ecdsa256";
79 ::arg().set("default-ksk-size","Default KSK size (0 means default)")="0";
80 ::arg().set("default-zsk-algorithm","Default ZSK algorithm")="";
81 ::arg().set("default-zsk-size","Default ZSK size (0 means default)")="0";
82 ::arg().set("default-soa-edit","Default SOA-EDIT value")="";
83 ::arg().set("default-soa-edit-signed","Default SOA-EDIT value for signed zones")="";
84 ::arg().set("max-ent-entries", "Maximum number of empty non-terminals in a zone")="100000";
85 ::arg().set("module-dir","Default directory for modules")=PKGLIBDIR
;
86 ::arg().set("entropy-source", "If set, read entropy from this file")="/dev/urandom";
87 ::arg().setSwitch("query-logging","Hint backends that queries should be logged")="no";
88 ::arg().set("loglevel","Amount of logging. Higher is more.")="3";
89 ::arg().setSwitch("direct-dnskey","Fetch DNSKEY, CDS and CDNSKEY RRs from backend during DNSKEY or CDS/CDNSKEY synthesis")="no";
90 ::arg().set("max-nsec3-iterations","Limit the number of NSEC3 hash iterations")="500"; // RFC5155 10.3
91 ::arg().set("max-signature-cache-entries", "Maximum number of signatures cache entries")="";
92 ::arg().set("rng", "Specify random number generator to use. Valid values are auto,sodium,openssl,getrandom,arc4random,urandom.")="auto";
93 ::arg().set("max-generate-steps", "Maximum number of $GENERATE steps when loading a zone from a file")="0";
94 ::arg().laxFile(configname
.c_str());
96 if(!::arg()["load-modules"].empty()) {
97 vector
<string
> modules
;
99 stringtok(modules
,::arg()["load-modules"], ", ");
100 if (!UeberBackend::loadModules(modules
, ::arg()["module-dir"])) {
105 g_log
.toConsole(Logger::Error
); // so we print any errors
106 BackendMakers().launch(::arg()["launch"]); // vrooooom!
107 if(::arg().asNum("loglevel") >= 3) // so you can't kill our errors
108 g_log
.toConsole((Logger::Urgency
)::arg().asNum("loglevel"));
110 //cerr<<"Backend: "<<::arg()["launch"]<<", '" << ::arg()["gmysql-dbname"] <<"'" <<endl;
112 S
.declare("qsize-q","Number of questions waiting for database attention");
114 ::arg().set("max-cache-entries", "Maximum number of cache entries")="1000000";
115 ::arg().set("cache-ttl","Seconds to store packets in the PacketCache")="20";
116 ::arg().set("negquery-cache-ttl","Seconds to store negative query results in the QueryCache")="60";
117 ::arg().set("query-cache-ttl","Seconds to store query results in the QueryCache")="20";
118 ::arg().set("default-soa-name","name to insert in the SOA record if none set in the backend")="a.misconfigured.powerdns.server";
119 ::arg().set("default-soa-mail","mail address to insert in the SOA record if none set in the backend")="";
120 ::arg().set("soa-refresh-default","Default SOA refresh")="10800";
121 ::arg().set("soa-retry-default","Default SOA retry")="3600";
122 ::arg().set("soa-expire-default","Default SOA expire")="604800";
123 ::arg().set("soa-minimum-ttl","Default SOA minimum ttl")="3600";
124 ::arg().set("chroot","Switch to this chroot jail")="";
125 ::arg().set("dnssec-key-cache-ttl","Seconds to cache DNSSEC keys from the database")="30";
126 ::arg().set("domain-metadata-cache-ttl","Seconds to cache domain metadata from the database")="60";
128 // Keep this line below all ::arg().set() statements
129 if (! ::arg().laxFile(configname
.c_str()))
130 cerr
<<"Warning: unable to read configuration file '"<<configname
<<"': "<<stringerror()<<endl
;
132 #ifdef HAVE_LIBSODIUM
133 if (sodium_init() == -1) {
134 cerr
<<"Unable to initialize sodium crypto library"<<endl
;
139 /* init rng before chroot */
142 if (!::arg()["chroot"].empty()) {
143 if (chroot(::arg()["chroot"].c_str())<0 || chdir("/") < 0) {
144 cerr
<<"Unable to chroot to '"+::arg()["chroot"]+"': "<<strerror (errno
)<<endl
;
152 bool rectifyZone(DNSSECKeeper
& dk
, const DNSName
& zone
, bool quiet
= false, bool rectifyTransaction
= true)
156 bool ret
= dk
.rectifyZone(zone
, error
, output
, rectifyTransaction
);
157 if (!quiet
|| !ret
) {
158 // When quiet, only print output if there was an error
159 if (!output
.empty()) {
162 if (!ret
&& !error
.empty()) {
169 void dbBench(const std::string
& fname
)
171 ::arg().set("query-cache-ttl")="0";
172 ::arg().set("negquery-cache-ttl")="0";
173 UeberBackend
B("default");
175 vector
<string
> domains
;
177 ifstream
ifs(fname
.c_str());
179 cerr
<<"Could not open '"<<fname
<<"' for reading domain names to query"<<endl
;
182 while(getline(ifs
,line
)) {
184 domains
.push_back(line
);
188 domains
.push_back("powerdns.com");
194 unsigned int hits
=0, misses
=0;
195 for(; n
< 10000; ++n
) {
196 DNSName
domain(domains
[dns_random(domains
.size())]);
197 B
.lookup(QType(QType::NS
), domain
, -1);
201 B
.lookup(QType(QType::A
), DNSName(std::to_string(random()))+domain
, -1);
207 cout
<<0.001*dt
.udiff()/n
<<" millisecond/lookup"<<endl
;
208 cout
<<"Retrieved "<<hits
<<" records, did "<<misses
<<" queries which should have no match"<<endl
;
209 cout
<<"Packet cache reports: "<<S
.read("query-cache-hit")<<" hits (should be 0) and "<<S
.read("query-cache-miss") <<" misses"<<endl
;
212 bool rectifyAllZones(DNSSECKeeper
&dk
, bool quiet
= false)
214 UeberBackend
B("default");
215 vector
<DomainInfo
> domainInfo
;
218 B
.getAllDomains(&domainInfo
);
219 for(DomainInfo di
: domainInfo
) {
221 cerr
<<"Rectifying "<<di
.zone
<<": ";
223 if (!rectifyZone(dk
, di
.zone
, quiet
)) {
228 cout
<<"Rectified "<<domainInfo
.size()<<" zones."<<endl
;
233 int checkZone(DNSSECKeeper
&dk
, UeberBackend
&B
, const DNSName
& zone
, const vector
<DNSResourceRecord
>* suppliedrecords
=0)
235 uint64_t numerrors
=0, numwarnings
=0;
239 if (!B
.getDomainInfo(zone
, di
)) {
240 cout
<<"[Error] Unable to get domain information for zone '"<<zone
<<"'"<<endl
;
243 } catch(const PDNSException
&e
) {
244 if (di
.kind
== DomainInfo::Slave
) {
245 cout
<<"[Error] non-IP address for masters: "<<e
.reason
<<endl
;
251 if(!B
.getSOAUncached(zone
, sd
)) {
252 cout
<<"[Error] No SOA record present, or active, in zone '"<<zone
<<"'"<<endl
;
254 cout
<<"Checked 0 records of '"<<zone
<<"', "<<numerrors
<<" errors, 0 warnings."<<endl
;
258 NSEC3PARAMRecordContent ns3pr
;
260 bool haveNSEC3
= dk
.getNSEC3PARAM(zone
, &ns3pr
, &narrow
);
261 bool isOptOut
=(haveNSEC3
&& ns3pr
.d_flags
);
263 bool isSecure
=dk
.isSecuredZone(zone
);
264 bool presigned
=dk
.isPresigned(zone
);
265 vector
<string
> checkKeyErrors
;
266 bool validKeys
=dk
.checkKeys(zone
, &checkKeyErrors
);
269 if(isSecure
&& zone
.wirelength() > 222) {
271 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
;
274 vector
<DNSBackend::KeyData
> dbkeyset
;
275 B
.getDomainKeys(zone
, dbkeyset
);
277 for(DNSBackend::KeyData
& kd
: dbkeyset
) {
278 DNSKEYRecordContent dkrc
;
279 shared_ptr
<DNSCryptoKeyEngine
>(DNSCryptoKeyEngine::makeFromISCString(dkrc
, kd
.content
));
281 if(dkrc
.d_algorithm
== DNSSECKeeper::RSASHA1
) {
282 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
;
290 cout
<<"[Error] zone '" << zone
<< "' has at least one invalid DNS Private Key." << endl
;
291 for (const auto &msg
: checkKeyErrors
) {
292 cout
<<"\t"<<msg
<<endl
;
296 // Check for delegation in parent zone
297 DNSName
parent(zone
);
298 while(parent
.chopOff()) {
300 if(B
.getSOAUncached(parent
, sd_p
)) {
303 B
.lookup(QType(QType::ANY
), zone
, sd_p
.domain_id
);
305 ns
|= (zr
.dr
.d_type
== QType::NS
);
307 cout
<<"[Error] No delegation for zone '"<<zone
<<"' in parent '"<<parent
<<"'"<<endl
;
315 bool hasNsAtApex
= false;
316 set
<DNSName
> tlsas
, cnames
, noncnames
, glue
, checkglue
;
317 set
<pair
<DNSName
, QType
> > checkOcclusion
;
318 set
<string
> recordcontents
;
319 map
<string
, unsigned int> ttl
;
321 ostringstream content
;
322 pair
<map
<string
, unsigned int>::iterator
,bool> ret
;
324 vector
<DNSResourceRecord
> records
;
325 if(!suppliedrecords
) {
326 DNSResourceRecord drr
;
327 sd
.db
->list(zone
, sd
.domain_id
, g_verbose
);
328 while(sd
.db
->get(drr
)) {
329 records
.push_back(drr
);
333 records
=*suppliedrecords
;
335 for(auto &rr
: records
) { // we modify this
336 if(rr
.qtype
.getCode() == QType::TLSA
)
337 tlsas
.insert(rr
.qname
);
338 if(rr
.qtype
.getCode() == QType::SOA
) {
340 stringtok(parts
, rr
.content
);
342 if(parts
.size() < 7) {
343 cout
<<"[Warning] SOA autocomplete is deprecated, missing field(s) in SOA content: "<<rr
.qname
<<" IN " <<rr
.qtype
.getName()<< " '" << rr
.content
<<"'"<<endl
;
348 for(int pleft
=parts
.size(); pleft
< 7; ++pleft
) {
354 if(rr
.qtype
.getCode() == QType::TXT
&& !rr
.content
.empty() && rr
.content
[0]!='"')
355 rr
.content
= "\""+rr
.content
+"\"";
358 shared_ptr
<DNSRecordContent
> drc(DNSRecordContent::mastermake(rr
.qtype
.getCode(), 1, rr
.content
));
359 string tmp
=drc
->serialize(rr
.qname
);
360 tmp
= drc
->getZoneRepresentation(true);
361 if (rr
.qtype
.getCode() != QType::AAAA
) {
362 if (!pdns_iequals(tmp
, rr
.content
)) {
363 if(rr
.qtype
.getCode() == QType::SOA
) {
364 tmp
= drc
->getZoneRepresentation(false);
366 if(!pdns_iequals(tmp
, rr
.content
)) {
367 cout
<<"[Warning] Parsed and original record content are not equal: "<<rr
.qname
<<" IN " <<rr
.qtype
.getName()<< " '" << rr
.content
<<"' (Content parsed as '"<<tmp
<<"')"<<endl
;
372 struct in6_addr tmpbuf
;
373 if (inet_pton(AF_INET6
, rr
.content
.c_str(), &tmpbuf
) != 1 || rr
.content
.find('.') != string::npos
) {
374 cout
<<"[Warning] Following record is not a valid IPv6 address: "<<rr
.qname
<<" IN " <<rr
.qtype
.getName()<< " '" << rr
.content
<<"'"<<endl
;
379 catch(std::exception
& e
)
381 cout
<<"[Error] Following record had a problem: \""<<rr
.qname
<<" IN "<<rr
.qtype
.getName()<<" "<<rr
.content
<<"\""<<endl
;
382 cout
<<"[Error] Error was: "<<e
.what()<<endl
;
387 if(!rr
.qname
.isPartOf(zone
)) {
388 cout
<<"[Error] Record '"<<rr
.qname
<<" IN "<<rr
.qtype
.getName()<<" "<<rr
.content
<<"' in zone '"<<zone
<<"' is out-of-zone."<<endl
;
394 content
<<rr
.qname
<<" "<<rr
.qtype
.getName()<<" "<<rr
.content
;
395 string contentstr
= content
.str();
396 if (rr
.qtype
.getCode() != QType::TXT
) {
397 contentstr
=toLower(contentstr
);
399 if (recordcontents
.count(contentstr
)) {
400 cout
<<"[Error] Duplicate record found in rrset: '"<<rr
.qname
<<" IN "<<rr
.qtype
.getName()<<" "<<rr
.content
<<"'"<<endl
;
404 recordcontents
.insert(contentstr
);
407 content
<<rr
.qname
<<" "<<rr
.qtype
.getName();
408 if (rr
.qtype
.getCode() == QType::RRSIG
) {
409 RRSIGRecordContent
rrc(rr
.content
);
410 content
<<" ("<<DNSRecordContent::NumberToType(rrc
.d_type
)<<")";
412 ret
= ttl
.insert(pair
<string
, unsigned int>(toLower(content
.str()), rr
.ttl
));
413 if (ret
.second
== false && ret
.first
->second
!= rr
.ttl
) {
414 cout
<<"[Error] TTL mismatch in rrset: '"<<rr
.qname
<<" IN " <<rr
.qtype
.getName()<<" "<<rr
.content
<<"' ("<<ret
.first
->second
<<" != "<<rr
.ttl
<<")"<<endl
;
419 if (isSecure
&& isOptOut
&& (rr
.qname
.countLabels() && rr
.qname
.getRawLabels()[0] == "*")) {
420 cout
<<"[Warning] wildcard record '"<<rr
.qname
<<" IN " <<rr
.qtype
.getName()<<" "<<rr
.content
<<"' is insecure"<<endl
;
421 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
;
426 if (rr
.qtype
.getCode() == QType::NS
) {
428 } else if (rr
.qtype
.getCode() == QType::DS
) {
429 cout
<<"[Warning] DS at apex in zone '"<<zone
<<"', should not be here."<<endl
;
433 if (rr
.qtype
.getCode() == QType::SOA
) {
434 cout
<<"[Error] SOA record not at apex '"<<rr
.qname
<<" IN "<<rr
.qtype
.getName()<<" "<<rr
.content
<<"' in zone '"<<zone
<<"'"<<endl
;
437 } else if (rr
.qtype
.getCode() == QType::DNSKEY
) {
438 cout
<<"[Warning] DNSKEY record not at apex '"<<rr
.qname
<<" IN "<<rr
.qtype
.getName()<<" "<<rr
.content
<<"' in zone '"<<zone
<<"', should not be here."<<endl
;
440 } else if (rr
.qtype
.getCode() == QType::NS
) {
441 if (DNSName(rr
.content
).isPartOf(rr
.qname
)) {
442 checkglue
.insert(DNSName(toLower(rr
.content
)));
444 checkOcclusion
.insert({rr
.qname
, rr
.qtype
});
445 } else if (rr
.qtype
.getCode() == QType::A
|| rr
.qtype
.getCode() == QType::AAAA
) {
446 glue
.insert(rr
.qname
);
447 } else if (rr
.qtype
== QType::DNAME
) {
448 checkOcclusion
.insert({rr
.qname
, rr
.qtype
});
451 if((rr
.qtype
.getCode() == QType::A
|| rr
.qtype
.getCode() == QType::AAAA
) && !rr
.qname
.isWildcard() && !rr
.qname
.isHostname())
452 cout
<<"[Info] "<<rr
.qname
.toString()<<" record for '"<<rr
.qtype
.getName()<<"' is not a valid hostname."<<endl
;
454 // Check if the DNSNames that should be hostnames, are hostnames
456 checkHostnameCorrectness(rr
);
457 } catch (const std::exception
& e
) {
458 cout
<< "[Warning] " << rr
.qtype
.getName() << " record in zone '" << zone
<< ": " << e
.what() << endl
;
462 if (rr
.qtype
.getCode() == QType::CNAME
) {
463 if (!cnames
.count(rr
.qname
))
464 cnames
.insert(rr
.qname
);
466 cout
<<"[Error] Duplicate CNAME found at '"<<rr
.qname
<<"'"<<endl
;
471 if (rr
.qtype
.getCode() == QType::RRSIG
) {
473 cout
<<"[Error] RRSIG found at '"<<rr
.qname
<<"' in non-presigned zone. These do not belong in the database."<<endl
;
478 noncnames
.insert(rr
.qname
);
481 if(rr
.qtype
.getCode() == QType::NSEC
|| rr
.qtype
.getCode() == QType::NSEC3
)
483 cout
<<"[Error] NSEC or NSEC3 found at '"<<rr
.qname
<<"'. These do not belong in the database."<<endl
;
488 if(!presigned
&& rr
.qtype
.getCode() == QType::DNSKEY
)
490 if(::arg().mustDo("direct-dnskey"))
492 if(rr
.ttl
!= sd
.default_ttl
)
494 cout
<<"[Warning] DNSKEY TTL of "<<rr
.ttl
<<" at '"<<rr
.qname
<<"' differs from SOA minimum of "<<sd
.default_ttl
<<endl
;
500 cout
<<"[Warning] DNSKEY at '"<<rr
.qname
<<"' in non-presigned zone will mostly be ignored and can cause problems."<<endl
;
506 for(auto &i
: cnames
) {
507 if (noncnames
.find(i
) != noncnames
.end()) {
508 cout
<<"[Error] CNAME "<<i
<<" found, but other records with same label exist."<<endl
;
513 for(const auto &i
: tlsas
) {
514 DNSName name
= DNSName(i
);
515 name
.trimToLabels(name
.countLabels()-2);
516 if (cnames
.find(name
) == cnames
.end() && noncnames
.find(name
) == noncnames
.end()) {
517 // No specific record for the name in the TLSA record exists, this
518 // is already worth emitting a warning. Let's see if a wildcard exist.
520 DNSName
wcname(name
);
522 wcname
.prependRawLabel("*");
523 if (cnames
.find(wcname
) != cnames
.end() || noncnames
.find(wcname
) != noncnames
.end()) {
524 cout
<<"A wildcard record exist for '"<<wcname
<<"' and a TLSA record for '"<<i
<<"'.";
526 cout
<<"No record for '"<<name
<<"' exists, but a TLSA record for '"<<i
<<"' does.";
529 cout
<<" A query for '"<<name
<<"' will yield an empty response. This is most likely a mistake, please create records for '"<<name
<<"'."<<endl
;
534 cout
<<"[Error] No NS record at zone apex in zone '"<<zone
<<"'"<<endl
;
538 for(const auto &qname
: checkglue
) {
539 if (!glue
.count(qname
)) {
540 cout
<<"[Warning] Missing glue for '"<<qname
<<"' in zone '"<<zone
<<"'"<<endl
;
545 for( const auto &qname
: checkOcclusion
) {
546 for( const auto &rr
: records
) {
547 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
) ) ) {
550 if( rr
.qname
.isPartOf( qname
.first
) ) {
551 if( qname
.second
== QType::DNAME
|| ( rr
.qtype
!= QType::ENT
&& rr
.qtype
.getCode() != QType::A
&& rr
.qtype
.getCode() != QType::AAAA
) ) {
552 cout
<< "[Warning] '" << rr
.qname
<< "|" << rr
.qtype
.getName() << "' in zone '" << zone
<< "' is occluded by a ";
553 if( qname
.second
== QType::NS
) {
554 cout
<< "delegation";
558 cout
<< " at '" << qname
.first
<< "'" << endl
;
565 bool ok
, ds_ns
, done
;
566 for( const auto &rr
: records
) {
567 ok
= ( rr
.auth
== 1 );
569 done
= (suppliedrecords
|| !sd
.db
->doesDNSSEC());
570 for( const auto &qname
: checkOcclusion
) {
571 if( qname
.second
== QType::NS
) {
572 if( qname
.first
== rr
.qname
) {
579 if( rr
.qname
.isPartOf( qname
.first
) && ( qname
.first
!= rr
.qname
|| rr
.qtype
!= QType::DS
) ) {
582 if( rr
.qtype
== QType::ENT
&& qname
.first
.isPartOf( rr
.qname
) ) {
585 } else if( rr
.qname
.isPartOf( qname
.first
) && ( ( qname
.first
!= rr
.qname
|| rr
.qtype
!= QType::DS
) || rr
.qtype
== QType::NS
) ) {
591 if( ! ds_ns
&& rr
.qtype
.getCode() == QType::DS
&& rr
.qname
!= zone
) {
592 cout
<< "[Warning] DS record without a delegation '" << rr
.qname
<<"'." << endl
;
595 if( ! ok
&& ! suppliedrecords
) {
596 cout
<< "[Error] Following record is auth=" << rr
.auth
<< ", run pdnsutil rectify-zone?: " << rr
.qname
<< " IN " << rr
.qtype
.getName() << " " << rr
.content
<< endl
;
601 cout
<<"Checked "<<records
.size()<<" records of '"<<zone
<<"', "<<numerrors
<<" errors, "<<numwarnings
<<" warnings."<<endl
;
607 int checkAllZones(DNSSECKeeper
&dk
, bool exitOnError
)
609 UeberBackend
B("default");
610 vector
<DomainInfo
> domainInfo
;
611 multi_index_container
<
614 ordered_non_unique
< member
<DomainInfo
,DNSName
,&DomainInfo::zone
>, CanonDNSNameCompare
>,
615 ordered_non_unique
< member
<DomainInfo
,uint32_t,&DomainInfo::id
> >
618 auto& seenNames
= seenInfos
.get
<0>();
619 auto& seenIds
= seenInfos
.get
<1>();
621 B
.getAllDomains(&domainInfo
, true);
623 for(auto di
: domainInfo
) {
624 if (checkZone(dk
, B
, di
.zone
) > 0) {
628 auto seenName
= seenNames
.find(di
.zone
);
629 if (seenName
!= seenNames
.end()) {
630 cout
<<"[Error] Another SOA for zone '"<<di
.zone
<<"' (serial "<<di
.serial
<<") has already been seen (serial "<<seenName
->serial
<<")."<<endl
;
634 auto seenId
= seenIds
.find(di
.id
);
635 if (seenId
!= seenIds
.end()) {
636 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
;
640 seenInfos
.insert(di
);
642 if(errors
&& exitOnError
)
645 cout
<<"Checked "<<domainInfo
.size()<<" zones, "<<errors
<<" had errors."<<endl
;
651 int increaseSerial(const DNSName
& zone
, DNSSECKeeper
&dk
)
653 UeberBackend
B("default");
655 if(!B
.getSOAUncached(zone
, sd
)) {
656 cerr
<<"No SOA for zone '"<<zone
<<"'"<<endl
;
660 if (dk
.isPresigned(zone
)) {
661 cerr
<<"Serial increase of presigned zone '"<<zone
<<"' is not allowed."<<endl
;
666 dk
.getSoaEdit(zone
, soaEditKind
);
668 DNSResourceRecord rr
;
669 makeIncreasedSOARecord(sd
, "SOA-EDIT-INCREASE", soaEditKind
, rr
);
671 sd
.db
->startTransaction(zone
, -1);
673 if (!sd
.db
->replaceRRSet(sd
.domain_id
, zone
, rr
.qtype
, vector
<DNSResourceRecord
>(1, rr
))) {
674 sd
.db
->abortTransaction();
675 cerr
<<"Backend did not replace SOA record. Backend might not support this operation."<<endl
;
679 if (sd
.db
->doesDNSSEC()) {
680 NSEC3PARAMRecordContent ns3pr
;
682 bool haveNSEC3
=dk
.getNSEC3PARAM(zone
, &ns3pr
, &narrow
);
687 ordername
=DNSName(toBase32Hex(hashQNameWithSalt(ns3pr
, zone
)));
689 ordername
=DNSName("");
691 cerr
<<"'"<<rr
.qname
<<"' -> '"<< ordername
<<"'"<<endl
;
692 sd
.db
->updateDNSSECOrderNameAndAuth(sd
.domain_id
, rr
.qname
, ordername
, true);
695 sd
.db
->commitTransaction();
697 cout
<<"SOA serial for zone "<<zone
<<" set to "<<sd
.serial
<<endl
;
701 int deleteZone(const DNSName
&zone
) {
704 if (! B
.getDomainInfo(zone
, di
)) {
705 cerr
<<"Domain '"<<zone
<<"' not found!"<<endl
;
709 if(di
.backend
->deleteDomain(zone
))
712 cerr
<<"Failed to delete domain '"<<zone
<<"'"<<endl
;;
716 void listKey(DomainInfo
const &di
, DNSSECKeeper
& dk
, bool printHeader
= true) {
718 cout
<<"Zone Type Size Algorithm ID Location Keytag"<<endl
;
719 cout
<<"----------------------------------------------------------------------------------"<<endl
;
721 unsigned int spacelen
= 0;
722 for (auto const &key
: dk
.getKeys(di
.zone
)) {
724 if (di
.zone
.toStringNoDot().length() > 29)
725 cout
<<endl
<<string(30, ' ');
727 cout
<<string(30 - di
.zone
.toStringNoDot().length(), ' ');
729 cout
<<DNSSECKeeper::keyTypeToString(key
.second
.keyType
)<<" ";
731 spacelen
= (std::to_string(key
.first
.getKey()->getBits()).length() >= 8) ? 1 : 8 - std::to_string(key
.first
.getKey()->getBits()).length();
732 if (key
.first
.getKey()->getBits() < 1) {
733 cout
<<"invalid "<<endl
;
736 cout
<<key
.first
.getKey()->getBits()<<string(spacelen
, ' ');
739 string algname
= DNSSECKeeper::algorithm2name(key
.first
.d_algorithm
);
740 spacelen
= (algname
.length() >= 13) ? 1 : 13 - algname
.length();
741 cout
<<algname
<<string(spacelen
, ' ');
743 spacelen
= (std::to_string(key
.second
.id
).length() > 5) ? 1 : 5 - std::to_string(key
.second
.id
).length();
744 cout
<<key
.second
.id
<<string(spacelen
, ' ');
747 auto stormap
= key
.first
.getKey()->convertToISCVector();
748 string engine
, slot
, label
= "";
749 for (auto const &elem
: stormap
) {
750 //cout<<elem.first<<" "<<elem.second<<endl;
751 if (elem
.first
== "Engine")
752 engine
= elem
.second
;
753 if (elem
.first
== "Slot")
755 if (elem
.first
== "Label")
758 if (engine
.empty() || slot
.empty()){
761 spacelen
= (engine
.length()+slot
.length()+label
.length()+2 >= 12) ? 1 : 12 - engine
.length()-slot
.length()-label
.length()-2;
762 cout
<<engine
<<","<<slot
<<","<<label
<<string(spacelen
, ' ');
767 cout
<<key
.first
.getDNSKEY().getTag()<<endl
;
771 int listKeys(const string
&zname
, DNSSECKeeper
& dk
){
772 UeberBackend
B("default");
774 if (zname
!= "all") {
776 if(!B
.getDomainInfo(DNSName(zname
), di
)) {
777 cerr
<< "Zone "<<zname
<<" not found."<<endl
;
782 vector
<DomainInfo
> domainInfo
;
783 B
.getAllDomains(&domainInfo
);
784 bool printHeader
= true;
785 for (auto const di
: domainInfo
) {
786 listKey(di
, dk
, printHeader
);
793 int listZone(const DNSName
&zone
) {
797 if (! B
.getDomainInfo(zone
, di
)) {
798 cerr
<<"Domain '"<<zone
<<"' not found!"<<endl
;
801 di
.backend
->list(zone
, di
.id
);
802 DNSResourceRecord rr
;
803 cout
<<"$ORIGIN ."<<endl
;
804 cout
.sync_with_stdio(false);
806 while(di
.backend
->get(rr
)) {
807 if(rr
.qtype
.getCode()) {
808 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] != '.')
809 rr
.content
.append(1, '.');
811 cout
<<rr
.qname
<<"\t"<<rr
.ttl
<<"\tIN\t"<<rr
.qtype
.getName()<<"\t"<<rr
.content
<<"\n";
818 // lovingly copied from http://stackoverflow.com/questions/1798511/how-to-avoid-press-enter-with-any-getchar
821 static struct termios oldt
, newt
;
823 /*tcgetattr gets the parameters of the current terminal
824 STDIN_FILENO will tell tcgetattr that it should write the settings
826 tcgetattr( STDIN_FILENO
, &oldt
);
827 /*now the settings will be copied*/
830 /*ICANON normally takes care that one line at a time will be processed
831 that means it will return if it sees a "\n" or an EOF or an EOL*/
832 newt
.c_lflag
&= ~(ICANON
);
834 /*Those new settings will be set to STDIN
835 TCSANOW tells tcsetattr to change attributes immediately. */
836 tcsetattr( STDIN_FILENO
, TCSANOW
, &newt
);
840 /*restore the old settings*/
841 tcsetattr( STDIN_FILENO
, TCSANOW
, &oldt
);
846 int clearZone(DNSSECKeeper
& dk
, const DNSName
&zone
) {
850 if (! B
.getDomainInfo(zone
, di
)) {
851 cerr
<<"Domain '"<<zone
<<"' not found!"<<endl
;
854 if(!di
.backend
->startTransaction(zone
, di
.id
)) {
855 cerr
<<"Unable to start transaction for load of zone '"<<zone
<<"'"<<endl
;
858 di
.backend
->commitTransaction();
862 int editZone(const DNSName
&zone
) {
867 if (! B
.getDomainInfo(zone
, di
)) {
868 cerr
<<"Domain '"<<zone
<<"' not found!"<<endl
;
871 vector
<DNSRecord
> pre
, post
;
872 char tmpnam
[]="/tmp/pdnsutil-XXXXXX";
873 int tmpfd
=mkstemp(tmpnam
);
875 unixDie("Making temporary filename in "+string(tmpnam
));
877 ~deleteme() { unlink(d_name
.c_str()); }
878 deleteme(string name
) : d_name(name
) {}
882 vector
<DNSResourceRecord
> checkrr
;
884 string editor
="editor";
885 if(auto e
=getenv("EDITOR")) // <3
889 di
.backend
->list(zone
, di
.id
);
890 pre
.clear(); post
.clear();
892 if(tmpfd
< 0 && (tmpfd
=open(tmpnam
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0600)) < 0)
893 unixDie("Error reopening temporary file "+string(tmpnam
));
894 string
header("; Warning - every name in this file is ABSOLUTE!\n$ORIGIN .\n");
895 if(write(tmpfd
, header
.c_str(), header
.length()) < 0)
896 unixDie("Writing zone to temporary file");
897 DNSResourceRecord rr
;
898 while(di
.backend
->get(rr
)) {
899 if(!rr
.qtype
.getCode())
904 sort(pre
.begin(), pre
.end(), DNSRecord::prettyCompare
);
905 for(const auto& dr
: pre
) {
907 os
<<dr
.d_name
<<"\t"<<dr
.d_ttl
<<"\tIN\t"<<DNSRecordContent::NumberToType(dr
.d_type
)<<"\t"<<dr
.d_content
->getZoneRepresentation(true)<<endl
;
908 if(write(tmpfd
, os
.str().c_str(), os
.str().length()) < 0)
909 unixDie("Writing zone to temporary file");
917 cmdline
+="+"+std::to_string(gotoline
)+" ";
919 int err
=system(cmdline
.c_str());
921 unixDie("Editing file with: '"+cmdline
+"', perhaps set EDITOR variable");
924 ZoneParserTNG
zpt(tmpnam
, g_rootdnsname
);
925 zpt
.setMaxGenerateSteps(::arg().asNum("max-generate-steps"));
926 DNSResourceRecord zrr
;
927 map
<pair
<DNSName
,uint16_t>, vector
<DNSRecord
> > grouped
;
929 while(zpt
.get(zrr
)) {
932 grouped
[{dr
.d_name
,dr
.d_type
}].push_back(dr
);
935 catch(std::exception
& e
) {
936 cerr
<<"Problem: "<<e
.what()<<" "<<zpt
.getLineOfFile()<<endl
;
937 auto fnum
= zpt
.getLineNumAndFile();
938 gotoline
= fnum
.second
;
942 sort(post
.begin(), post
.end(), DNSRecord::prettyCompare
);
945 for(const DNSRecord
& rr
: post
) {
946 DNSResourceRecord drr
= DNSResourceRecord::fromWire(rr
);
947 drr
.domain_id
= di
.id
;
948 checkrr
.push_back(drr
);
950 if(checkZone(dk
, B
, zone
, &checkrr
)) {
952 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
;
968 vector
<DNSRecord
> diff
;
970 map
<pair
<DNSName
,uint16_t>, string
> changed
;
971 set_difference(pre
.cbegin(), pre
.cend(), post
.cbegin(), post
.cend(), back_inserter(diff
), DNSRecord::prettyCompare
);
972 for(const auto& d
: diff
) {
974 str
<<"\033[0;31m-"<< d
.d_name
<<" "<<d
.d_ttl
<<" IN "<<DNSRecordContent::NumberToType(d
.d_type
)<<" "<<d
.d_content
->getZoneRepresentation(true)<<"\033[0m"<<endl
;
975 changed
[{d
.d_name
,d
.d_type
}] += str
.str();
979 set_difference(post
.cbegin(), post
.cend(), pre
.cbegin(), pre
.cend(), back_inserter(diff
), DNSRecord::prettyCompare
);
980 for(const auto& d
: diff
) {
983 str
<<"\033[0;32m+"<< d
.d_name
<<" "<<d
.d_ttl
<<" IN "<<DNSRecordContent::NumberToType(d
.d_type
)<<" "<<d
.d_content
->getZoneRepresentation(true)<<"\033[0m"<<endl
;
984 changed
[{d
.d_name
,d
.d_type
}]+=str
.str();
986 cout
<<"Detected the following changes:"<<endl
;
987 for(const auto& c
: changed
) {
990 if (changed
.size() > 0) {
991 if (changed
.find({zone
, QType::SOA
}) == changed
.end()) {
992 cout
<<endl
<<"You have not updated the SOA record! Would you like to increase-serial?"<<endl
;
993 cout
<<"(y)es - increase serial, (n)o - leave SOA record as is, (e)dit your changes, (q)uit:"<<endl
;
998 DNSRecord oldSoaDR
= grouped
[{zone
, QType::SOA
}].at(0); // there should be only one SOA record, so we can use .at(0);
1000 str
<<"\033[0;31m-"<< oldSoaDR
.d_name
<<" "<<oldSoaDR
.d_ttl
<<" IN "<<DNSRecordContent::NumberToType(oldSoaDR
.d_type
)<<" "<<oldSoaDR
.d_content
->getZoneRepresentation(true)<<"\033[0m"<<endl
;
1003 B
.getSOAUncached(zone
, sd
);
1004 // TODO: do we need to check for presigned? here or maybe even all the way before edit-zone starts?
1007 dk
.getSoaEdit(zone
, soaEditKind
);
1009 DNSResourceRecord rr
;
1010 makeIncreasedSOARecord(sd
, "SOA-EDIT-INCREASE", soaEditKind
, rr
);
1012 str
<<"\033[0;32m+"<< dr
.d_name
<<" "<<dr
.d_ttl
<<" IN "<<DNSRecordContent::NumberToType(dr
.d_type
)<<" "<<dr
.d_content
->getZoneRepresentation(true)<<"\033[0m"<<endl
;
1014 changed
[{dr
.d_name
, dr
.d_type
}]+=str
.str();
1015 grouped
[{dr
.d_name
, dr
.d_type
}].at(0) = dr
;
1019 return EXIT_FAILURE
;
1032 if(changed
.empty()) {
1033 cout
<<endl
<<"No changes to apply."<<endl
;
1034 return(EXIT_SUCCESS
);
1036 cout
<<endl
<<"(a)pply these changes, (e)dit again, (r)etry with original zone, (q)uit: ";
1041 return(EXIT_SUCCESS
);
1046 else if(changed
.empty() || c
!='a')
1049 di
.backend
->startTransaction(zone
, -1);
1050 for(const auto& change
: changed
) {
1051 vector
<DNSResourceRecord
> vrr
;
1052 for(const DNSRecord
& rr
: grouped
[change
.first
]) {
1053 DNSResourceRecord crr
= DNSResourceRecord::fromWire(rr
);
1054 crr
.domain_id
= di
.id
;
1057 di
.backend
->replaceRRSet(di
.id
, change
.first
.first
, QType(change
.first
.second
), vrr
);
1059 rectifyZone(dk
, zone
, false, false);
1060 di
.backend
->commitTransaction();
1061 return EXIT_SUCCESS
;
1064 static int xcryptIP(const std::string
& cmd
, const std::string
& ip
, const std::string
& rkey
)
1067 ComboAddress
ca(ip
), ret
;
1069 if(cmd
=="ipencrypt")
1070 ret
= encryptCA(ca
, rkey
);
1072 ret
= decryptCA(ca
, rkey
);
1074 cout
<<ret
.toString()<<endl
;
1075 return EXIT_SUCCESS
;
1079 int loadZone(DNSName zone
, const string
& fname
) {
1083 if (B
.getDomainInfo(zone
, di
)) {
1084 cerr
<<"Domain '"<<zone
<<"' exists already, replacing contents"<<endl
;
1087 cerr
<<"Creating '"<<zone
<<"'"<<endl
;
1088 B
.createDomain(zone
);
1090 if(!B
.getDomainInfo(zone
, di
)) {
1091 cerr
<<"Domain '"<<zone
<<"' was not created - perhaps backend ("<<::arg()["launch"]<<") does not support storing new zones."<<endl
;
1092 return EXIT_FAILURE
;
1095 DNSBackend
* db
= di
.backend
;
1096 ZoneParserTNG
zpt(fname
, zone
);
1097 zpt
.setMaxGenerateSteps(::arg().asNum("max-generate-steps"));
1099 DNSResourceRecord rr
;
1100 if(!db
->startTransaction(zone
, di
.id
)) {
1101 cerr
<<"Unable to start transaction for load of zone '"<<zone
<<"'"<<endl
;
1102 return EXIT_FAILURE
;
1105 bool haveSOA
= false;
1106 while(zpt
.get(rr
)) {
1107 if(!rr
.qname
.isPartOf(zone
) && rr
.qname
!=zone
) {
1108 cerr
<<"File contains record named '"<<rr
.qname
<<"' which is not part of zone '"<<zone
<<"'"<<endl
;
1109 return EXIT_FAILURE
;
1111 if (rr
.qtype
== QType::SOA
) {
1117 db
->feedRecord(rr
, DNSName());
1119 db
->commitTransaction();
1120 return EXIT_SUCCESS
;
1123 int createZone(const DNSName
&zone
, const DNSName
& nsname
) {
1126 if (B
.getDomainInfo(zone
, di
)) {
1127 cerr
<<"Domain '"<<zone
<<"' exists already"<<endl
;
1128 return EXIT_FAILURE
;
1130 cerr
<<"Creating empty zone '"<<zone
<<"'"<<endl
;
1131 B
.createDomain(zone
);
1132 if(!B
.getDomainInfo(zone
, di
)) {
1133 cerr
<<"Domain '"<<zone
<<"' was not created!"<<endl
;
1134 return EXIT_FAILURE
;
1137 DNSResourceRecord rr
;
1140 rr
.ttl
= ::arg().asNum("default-ttl");
1143 string soa
= (boost::format("%s %s 1")
1144 % (nsname
.empty() ? ::arg()["default-soa-name"] : nsname
.toString())
1145 % (::arg().isEmpty("default-soa-mail") ? (DNSName("hostmaster.") + zone
).toString() : ::arg()["default-soa-mail"])
1148 fillSOAData(soa
, sd
); // fills out default values for us
1149 rr
.content
= makeSOAContent(sd
)->getZoneRepresentation(true);
1150 rr
.domain_id
= di
.id
;
1151 di
.backend
->startTransaction(zone
, di
.id
);
1152 di
.backend
->feedRecord(rr
, DNSName());
1153 if(!nsname
.empty()) {
1154 cout
<<"Also adding one NS record"<<endl
;
1156 rr
.content
=nsname
.toStringNoDot();
1157 di
.backend
->feedRecord(rr
, DNSName());
1160 di
.backend
->commitTransaction();
1162 return EXIT_SUCCESS
;
1165 int createSlaveZone(const vector
<string
>& cmds
) {
1168 DNSName
zone(cmds
[1]);
1169 if (B
.getDomainInfo(zone
, di
)) {
1170 cerr
<<"Domain '"<<zone
<<"' exists already"<<endl
;
1171 return EXIT_FAILURE
;
1173 vector
<string
> masters
;
1174 for (unsigned i
=2; i
< cmds
.size(); i
++) {
1175 ComboAddress
master(cmds
[i
], 53);
1176 masters
.push_back(master
.toStringWithPort());
1178 cerr
<<"Creating slave zone '"<<zone
<<"', with master(s) '"<<boost::join(masters
, ",")<<"'"<<endl
;
1179 B
.createDomain(zone
);
1180 if(!B
.getDomainInfo(zone
, di
)) {
1181 cerr
<<"Domain '"<<zone
<<"' was not created!"<<endl
;
1182 return EXIT_FAILURE
;
1184 di
.backend
->setKind(zone
, DomainInfo::Slave
);
1185 di
.backend
->setMaster(zone
, boost::join(masters
, ","));
1186 return EXIT_SUCCESS
;
1189 int changeSlaveZoneMaster(const vector
<string
>& cmds
) {
1192 DNSName
zone(cmds
[1]);
1193 if (!B
.getDomainInfo(zone
, di
)) {
1194 cerr
<<"Domain '"<<zone
<<"' doesn't exist"<<endl
;
1195 return EXIT_FAILURE
;
1197 vector
<string
> masters
;
1198 for (unsigned i
=2; i
< cmds
.size(); i
++) {
1199 ComboAddress
master(cmds
[i
], 53);
1200 masters
.push_back(master
.toStringWithPort());
1202 cerr
<<"Updating slave zone '"<<zone
<<"', master(s) to '"<<boost::join(masters
, ",")<<"'"<<endl
;
1204 di
.backend
->setMaster(zone
, boost::join(masters
, ","));
1205 return EXIT_SUCCESS
;
1207 catch (PDNSException
& e
) {
1208 cerr
<<"Setting master for zone '"<<zone
<<"' failed: "<<e
.reason
<<endl
;
1209 return EXIT_FAILURE
;
1213 // add-record ZONE name type [ttl] "content" ["content"]
1214 int addOrReplaceRecord(bool addOrReplace
, const vector
<string
>& cmds
) {
1215 DNSResourceRecord rr
;
1216 vector
<DNSResourceRecord
> newrrs
;
1217 DNSName
zone(cmds
[1]);
1222 name
=DNSName(cmds
[2])+zone
;
1224 rr
.qtype
= DNSRecordContent::TypeToNumber(cmds
[3]);
1225 rr
.ttl
= ::arg().asNum("default-ttl");
1230 if(!B
.getDomainInfo(zone
, di
)) {
1231 cerr
<<"Domain '"<<zone
<<"' does not exist"<<endl
;
1232 return EXIT_FAILURE
;
1235 rr
.domain_id
= di
.id
;
1237 DNSResourceRecord oldrr
;
1239 di
.backend
->startTransaction(zone
, -1);
1241 if(addOrReplace
) { // the 'add' case
1242 di
.backend
->lookup(rr
.qtype
, rr
.qname
, di
.id
);
1244 while(di
.backend
->get(oldrr
))
1245 newrrs
.push_back(oldrr
);
1248 unsigned int contentStart
= 4;
1249 if(cmds
.size() > 5) {
1250 rr
.ttl
=atoi(cmds
[4].c_str());
1251 if(std::to_string(rr
.ttl
)==cmds
[4]) {
1255 rr
.ttl
= ::arg().asNum("default-ttl");
1259 di
.backend
->lookup(QType(QType::ANY
), rr
.qname
, di
.id
);
1261 if(rr
.qtype
.getCode() == QType::CNAME
) { // this will save us SO many questions
1263 while(di
.backend
->get(oldrr
)) {
1264 if(addOrReplace
|| oldrr
.qtype
.getCode() != QType::CNAME
) // the replace case is ok if we replace one CNAME by the other
1268 cerr
<<"Attempting to add CNAME to "<<rr
.qname
<<" which already had existing records"<<endl
;
1269 return EXIT_FAILURE
;
1273 while(di
.backend
->get(oldrr
)) {
1274 if(oldrr
.qtype
.getCode() == QType::CNAME
)
1278 cerr
<<"Attempting to add record to "<<rr
.qname
<<" which already had a CNAME record"<<endl
;
1279 return EXIT_FAILURE
;
1284 cout
<<"Current records for "<<rr
.qname
<<" IN "<<rr
.qtype
.getName()<<" will be replaced"<<endl
;
1286 for(auto i
= contentStart
; i
< cmds
.size() ; ++i
) {
1287 rr
.content
= DNSRecordContent::mastermake(rr
.qtype
.getCode(), QClass::IN
, cmds
[i
])->getZoneRepresentation(true);
1289 newrrs
.push_back(rr
);
1293 di
.backend
->replaceRRSet(di
.id
, name
, rr
.qtype
, newrrs
);
1294 // need to be explicit to bypass the ueberbackend cache!
1295 di
.backend
->lookup(rr
.qtype
, name
, di
.id
);
1296 cout
<<"New rrset:"<<endl
;
1297 while(di
.backend
->get(rr
)) {
1298 cout
<<rr
.qname
.toString()<<" "<<rr
.ttl
<<" IN "<<rr
.qtype
.getName()<<" "<<rr
.content
<<endl
;
1300 di
.backend
->commitTransaction();
1301 return EXIT_SUCCESS
;
1304 // delete-rrset zone name type
1305 int deleteRRSet(const std::string
& zone_
, const std::string
& name_
, const std::string
& type_
)
1309 DNSName
zone(zone_
);
1310 if(!B
.getDomainInfo(zone
, di
)) {
1311 cerr
<<"Domain '"<<zone
<<"' does not exist"<<endl
;
1312 return EXIT_FAILURE
;
1319 name
=DNSName(name_
)+zone
;
1321 QType
qt(QType::chartocode(type_
.c_str()));
1322 di
.backend
->startTransaction(zone
, -1);
1323 di
.backend
->replaceRRSet(di
.id
, name
, qt
, vector
<DNSResourceRecord
>());
1324 di
.backend
->commitTransaction();
1325 return EXIT_SUCCESS
;
1328 int listAllZones(const string
&type
="") {
1330 int kindFilter
= -1;
1332 if (toUpper(type
) == "MASTER")
1334 else if (toUpper(type
) == "SLAVE")
1336 else if (toUpper(type
) == "NATIVE")
1339 cerr
<<"Syntax: pdnsutil list-all-zones [master|slave|native]"<<endl
;
1344 UeberBackend
B("default");
1346 vector
<DomainInfo
> domains
;
1347 B
.getAllDomains(&domains
, true);
1350 for (const auto& di
: domains
) {
1351 if (di
.kind
== kindFilter
|| kindFilter
== -1) {
1352 cout
<<di
.zone
<<endl
;
1358 if (kindFilter
!= -1)
1359 cout
<<type
<<" zonecount: "<<count
<<endl
;
1361 cout
<<"All zonecount: "<<count
<<endl
;
1367 bool testAlgorithm(int algo
)
1369 return DNSCryptoKeyEngine::testOne(algo
);
1372 bool testAlgorithms()
1374 return DNSCryptoKeyEngine::testAll();
1377 void testSpeed(DNSSECKeeper
& dk
, const DNSName
& zone
, const string
& remote
, int cores
)
1379 DNSResourceRecord rr
;
1380 rr
.qname
=DNSName("blah")+zone
;
1384 rr
.qclass
= QClass::IN
;
1386 UeberBackend
db("key-only");
1388 if ( ! db
.backends
.size() )
1390 throw runtime_error("No backends available for DNSSEC key storage");
1393 ChunkedSigningPipe
csp(DNSName(zone
), 1, cores
);
1395 vector
<DNSZoneRecord
> signatures
;
1397 unsigned char* octets
= (unsigned char*)&rnd
;
1401 for(unsigned int n
=0; n
< 100000; ++n
) {
1402 rnd
= dns_random(UINT32_MAX
);
1403 snprintf(tmp
, sizeof(tmp
), "%d.%d.%d.%d",
1404 octets
[0], octets
[1], octets
[2], octets
[3]);
1407 snprintf(tmp
, sizeof(tmp
), "r-%u", rnd
);
1408 rr
.qname
=DNSName(tmp
)+zone
;
1410 dzr
.dr
=DNSRecord(rr
);
1412 while(signatures
= csp
.getChunk(), !signatures
.empty())
1415 cerr
<<"Flushing the pipe, "<<csp
.d_signed
<<" signed, "<<csp
.d_queued
<<" queued, "<<csp
.d_outstanding
<<" outstanding"<< endl
;
1416 cerr
<<"Net speed: "<<csp
.d_signed
/ (dt
.udiffNoReset()/1000000.0) << " sigs/s"<<endl
;
1417 while(signatures
= csp
.getChunk(true), !signatures
.empty())
1419 cerr
<<"Done, "<<csp
.d_signed
<<" signed, "<<csp
.d_queued
<<" queued, "<<csp
.d_outstanding
<<" outstanding"<< endl
;
1420 cerr
<<"Net speed: "<<csp
.d_signed
/ (dt
.udiff()/1000000.0) << " sigs/s"<<endl
;
1423 void verifyCrypto(const string
& zone
)
1425 ZoneParserTNG
zpt(zone
);
1426 zpt
.setMaxGenerateSteps(::arg().asNum("max-generate-steps"));
1427 DNSResourceRecord rr
;
1428 DNSKEYRecordContent drc
;
1429 RRSIGRecordContent rrc
;
1430 DSRecordContent dsrc
;
1431 sortedRecords_t toSign
;
1432 DNSName qname
, apex
;
1433 dsrc
.d_digesttype
=0;
1434 while(zpt
.get(rr
)) {
1435 if(rr
.qtype
.getCode() == QType::DNSKEY
) {
1436 cerr
<<"got DNSKEY!"<<endl
;
1438 drc
= *std::dynamic_pointer_cast
<DNSKEYRecordContent
>(DNSRecordContent::mastermake(QType::DNSKEY
, QClass::IN
, rr
.content
));
1440 else if(rr
.qtype
.getCode() == QType::RRSIG
) {
1441 cerr
<<"got RRSIG"<<endl
;
1442 rrc
= *std::dynamic_pointer_cast
<RRSIGRecordContent
>(DNSRecordContent::mastermake(QType::RRSIG
, QClass::IN
, rr
.content
));
1444 else if(rr
.qtype
.getCode() == QType::DS
) {
1445 cerr
<<"got DS"<<endl
;
1446 dsrc
= *std::dynamic_pointer_cast
<DSRecordContent
>(DNSRecordContent::mastermake(QType::DS
, QClass::IN
, rr
.content
));
1450 toSign
.insert(DNSRecordContent::mastermake(rr
.qtype
.getCode(), QClass::IN
, rr
.content
));
1454 string msg
= getMessageForRRSET(qname
, rrc
, toSign
);
1455 cerr
<<"Verify: "<<DNSCryptoKeyEngine::makeFromPublicKeyString(drc
.d_algorithm
, drc
.d_key
)->verify(msg
, rrc
.d_signature
)<<endl
;
1456 if(dsrc
.d_digesttype
) {
1457 cerr
<<"Calculated DS: "<<apex
.toString()<<" IN DS "<<makeDSFromDNSKey(apex
, drc
, dsrc
.d_digesttype
).getZoneRepresentation()<<endl
;
1458 cerr
<<"Original DS: "<<apex
.toString()<<" IN DS "<<dsrc
.getZoneRepresentation()<<endl
;
1461 std::shared_ptr
<DNSCryptoKeyEngine
> key
=DNSCryptoKeyEngine::makeFromISCString(drc
, "Private-key-format: v1.2\n"
1462 "Algorithm: 12 (ECC-GOST)\n"
1463 "GostAsn1: MEUCAQAwHAYGKoUDAgITMBIGByqFAwICIwEGByqFAwICHgEEIgQg/9MiXtXKg9FDXDN/R9CmVhJDyuzRAIgh4tPwCu4NHIs=\n");
1464 string resign
=key
->sign(hash
);
1465 cerr
<<Base64Encode(resign
)<<endl
;
1466 cerr
<<"Verify: "<<DNSCryptoKeyEngine::makeFromPublicKeyString(drc
.d_algorithm
, drc
.d_key
)->verify(hash
, resign
)<<endl
;
1470 bool disableDNSSECOnZone(DNSSECKeeper
& dk
, const DNSName
& zone
)
1472 UeberBackend
B("default");
1475 if (!B
.getDomainInfo(zone
, di
)){
1476 cerr
<< "No such zone in the database" << endl
;
1481 bool ret
= dk
.unSecureZone(zone
, error
, info
);
1483 cerr
<< error
<< endl
;
1488 int setZoneAccount(const DNSName
& zone
, const string
&account
)
1490 UeberBackend
B("default");
1493 if (!B
.getDomainInfo(zone
, di
)){
1494 cerr
<< "No such zone "<<zone
<<" in the database" << endl
;
1495 return EXIT_FAILURE
;
1497 if(!di
.backend
->setAccount(zone
, account
)) {
1498 cerr
<<"Could not find backend willing to accept new zone configuration"<<endl
;
1499 return EXIT_FAILURE
;
1501 return EXIT_SUCCESS
;
1504 int setZoneKind(const DNSName
& zone
, const DomainInfo::DomainKind kind
)
1506 UeberBackend
B("default");
1509 if (!B
.getDomainInfo(zone
, di
)){
1510 cerr
<< "No such zone "<<zone
<<" in the database" << endl
;
1511 return EXIT_FAILURE
;
1513 if(!di
.backend
->setKind(zone
, kind
)) {
1514 cerr
<<"Could not find backend willing to accept new zone configuration"<<endl
;
1515 return EXIT_FAILURE
;
1517 return EXIT_SUCCESS
;
1520 bool showZone(DNSSECKeeper
& dk
, const DNSName
& zone
, bool exportDS
= false)
1522 UeberBackend
B("default");
1525 if (!B
.getDomainInfo(zone
, di
)){
1526 cerr
<< "No such zone in the database" << endl
;
1530 if (!di
.account
.empty()) {
1531 cout
<<"This zone is owned by "<<di
.account
<<endl
;
1534 cout
<<"This is a "<<DomainInfo::getKindString(di
.kind
)<<" zone"<<endl
;
1535 if(di
.kind
== DomainInfo::Master
) {
1536 cout
<<"Last SOA serial number we notified: "<<di
.notified_serial
<<" ";
1538 if(B
.getSOAUncached(zone
, sd
)) {
1539 if(sd
.serial
== di
.notified_serial
)
1543 cout
<<sd
.serial
<<" (serial in the database)"<<endl
;
1546 else if(di
.kind
== DomainInfo::Slave
) {
1547 cout
<<"Master"<<addS(di
.masters
)<<": ";
1548 for(const auto& m
: di
.masters
)
1549 cout
<<m
.toStringWithPort()<<" ";
1552 localtime_r(&di
.last_check
, &tm
);
1555 strftime(buf
, sizeof(buf
)-1, "%a %F %H:%M:%S", &tm
);
1557 strncpy(buf
, "Never", sizeof(buf
)-1);
1558 buf
[sizeof(buf
)-1] = '\0';
1559 cout
<<"Last time we got update from master: "<<buf
<<endl
;
1561 if(B
.getSOAUncached(zone
, sd
)) {
1562 cout
<<"SOA serial in database: "<<sd
.serial
<<endl
;
1563 cout
<<"Refresh interval: "<<sd
.refresh
<<" seconds"<<endl
;
1566 cout
<<"No SOA serial found in database"<<endl
;
1570 if(!dk
.isSecuredZone(zone
)) {
1571 auto &outstream
= (exportDS
? cerr
: cout
);
1572 outstream
<< "Zone is not actively secured" << endl
;
1574 // it does not make sense to proceed here, and it might be useful
1575 // for scripts to know that something is odd here
1580 NSEC3PARAMRecordContent ns3pr
;
1582 bool haveNSEC3
=dk
.getNSEC3PARAM(zone
, &ns3pr
, &narrow
);
1584 DNSSECKeeper::keyset_t keyset
=dk
.getKeys(zone
);
1587 std::vector
<std::string
> meta
;
1589 if (B
.getDomainMetadata(zone
, "TSIG-ALLOW-AXFR", meta
) && meta
.size() > 0) {
1590 cout
<< "Zone has following allowed TSIG key(s): " << boost::join(meta
, ",") << endl
;
1594 if (B
.getDomainMetadata(zone
, "AXFR-MASTER-TSIG", meta
) && meta
.size() > 0) {
1595 cout
<< "Zone uses following TSIG key(s): " << boost::join(meta
, ",") << endl
;
1598 std::map
<std::string
, std::vector
<std::string
> > metamap
;
1599 if(B
.getAllDomainMetadata(zone
, metamap
)) {
1600 cout
<<"Metadata items: ";
1605 for(const auto& m
: metamap
) {
1606 for(const auto i
: m
.second
)
1607 cout
<< '\t' << m
.first
<<'\t' << i
<<endl
;
1613 if (dk
.isPresigned(zone
)) {
1615 cout
<<"Zone is presigned"<<endl
;
1619 vector
<DNSKEYRecordContent
> keys
;
1622 di
.backend
->lookup(QType(QType::DNSKEY
), zone
, di
.id
);
1623 while(di
.backend
->get(zr
)) {
1624 keys
.push_back(*getRR
<DNSKEYRecordContent
>(zr
.dr
));
1628 cerr
<< "No keys for zone '"<<zone
<<"'."<<endl
;
1634 cout
<<"Zone has NSEC semantics"<<endl
;
1636 cout
<<"Zone has " << (narrow
? "NARROW " : "") <<"hashed NSEC3 semantics, configuration: "<<ns3pr
.getZoneRepresentation()<<endl
;
1637 cout
<< "keys: "<<endl
;
1640 sort(keys
.begin(),keys
.end());
1641 reverse(keys
.begin(),keys
.end());
1642 for(const auto& key
: keys
) {
1643 string algname
= DNSSECKeeper::algorithm2name(key
.d_algorithm
);
1647 std::shared_ptr
<DNSCryptoKeyEngine
> engine(DNSCryptoKeyEngine::makeFromPublicKeyString(key
.d_algorithm
, key
.d_key
)); // throws on unknown algo or bad key
1648 bits
=engine
->getBits();
1650 catch(std::exception
& e
) {
1651 cerr
<<"Could not process key to extract metadata: "<<e
.what()<<endl
;
1654 cout
<< (key
.d_flags
== 257 ? "KSK" : "ZSK") << ", tag = " << key
.getTag() << ", algo = "<<(int)key
.d_algorithm
<< ", bits = " << bits
<< endl
;
1655 cout
<< "DNSKEY = " <<zone
.toString()<<" IN DNSKEY "<< key
.getZoneRepresentation() << "; ( " + algname
+ " ) " <<endl
;
1658 const std::string
prefix(exportDS
? "" : "DS = ");
1659 cout
<<prefix
<<zone
.toString()<<" IN DS "<<makeDSFromDNSKey(zone
, key
, DNSSECKeeper::DIGEST_SHA1
).getZoneRepresentation() << " ; ( SHA1 digest )" << endl
;
1660 cout
<<prefix
<<zone
.toString()<<" IN DS "<<makeDSFromDNSKey(zone
, key
, DNSSECKeeper::DIGEST_SHA256
).getZoneRepresentation() << " ; ( SHA256 digest )" << endl
;
1662 string output
=makeDSFromDNSKey(zone
, key
, DNSSECKeeper::DIGEST_GOST
).getZoneRepresentation();
1663 cout
<<prefix
<<zone
.toString()<<" IN DS "<<output
<< " ; ( GOST R 34.11-94 digest )" << endl
;
1668 string output
=makeDSFromDNSKey(zone
, key
, DNSSECKeeper::DIGEST_SHA384
).getZoneRepresentation();
1669 cout
<<prefix
<<zone
.toString()<<" IN DS "<<output
<< " ; ( SHA-384 digest )" << endl
;
1675 else if(keyset
.empty()) {
1676 cerr
<< "No keys for zone '"<<zone
<<"'."<<endl
;
1681 cout
<<"Zone has NSEC semantics"<<endl
;
1683 cout
<<"Zone has " << (narrow
? "NARROW " : "") <<"hashed NSEC3 semantics, configuration: "<<ns3pr
.getZoneRepresentation()<<endl
;
1684 cout
<< "keys: "<<endl
;
1687 for(DNSSECKeeper::keyset_t::value_type value
: keyset
) {
1688 string algname
= DNSSECKeeper::algorithm2name(value
.first
.d_algorithm
);
1690 cout
<<"ID = "<<value
.second
.id
<<" ("<<DNSSECKeeper::keyTypeToString(value
.second
.keyType
)<<")";
1692 if (value
.first
.getKey()->getBits() < 1) {
1693 cerr
<<" <key missing or defunct>" <<endl
;
1697 cout
<<", flags = "<<std::to_string(value
.first
.d_flags
);
1698 cout
<<", tag = "<<value
.first
.getDNSKEY().getTag();
1699 cout
<<", algo = "<<(int)value
.first
.d_algorithm
<<", bits = "<<value
.first
.getKey()->getBits()<<"\t"<<((int)value
.second
.active
== 1 ? " A" : "Ina")<<"ctive ( " + algname
+ " ) "<<endl
;
1703 if (value
.second
.keyType
== DNSSECKeeper::KSK
|| value
.second
.keyType
== DNSSECKeeper::CSK
|| ::arg().mustDo("direct-dnskey")) {
1704 cout
<<DNSSECKeeper::keyTypeToString(value
.second
.keyType
)<<" DNSKEY = "<<zone
.toString()<<" IN DNSKEY "<< value
.first
.getDNSKEY().getZoneRepresentation() << " ; ( " + algname
+ " )" << endl
;
1707 if (value
.second
.keyType
== DNSSECKeeper::KSK
|| value
.second
.keyType
== DNSSECKeeper::CSK
) {
1708 const auto &key
= value
.first
.getDNSKEY();
1709 const std::string
prefix(exportDS
? "" : "DS = ");
1710 cout
<<prefix
<<zone
.toString()<<" IN DS "<<makeDSFromDNSKey(zone
, key
, DNSSECKeeper::DIGEST_SHA1
).getZoneRepresentation() << " ; ( SHA1 digest )" << endl
;
1711 cout
<<prefix
<<zone
.toString()<<" IN DS "<<makeDSFromDNSKey(zone
, key
, DNSSECKeeper::DIGEST_SHA256
).getZoneRepresentation() << " ; ( SHA256 digest )" << endl
;
1713 string output
=makeDSFromDNSKey(zone
, key
, DNSSECKeeper::DIGEST_GOST
).getZoneRepresentation();
1714 cout
<<prefix
<<zone
.toString()<<" IN DS "<<output
<< " ; ( GOST R 34.11-94 digest )" << endl
;
1719 string output
=makeDSFromDNSKey(zone
, key
, DNSSECKeeper::DIGEST_SHA384
).getZoneRepresentation();
1720 cout
<<prefix
<<zone
.toString()<<" IN DS "<<output
<< " ; ( SHA-384 digest )" << endl
;
1730 bool secureZone(DNSSECKeeper
& dk
, const DNSName
& zone
)
1735 // temp var for addKey
1738 string k_algo
= ::arg()["default-ksk-algorithm"];
1739 k_size
= ::arg().asNum("default-ksk-size");
1740 string z_algo
= ::arg()["default-zsk-algorithm"];
1741 z_size
= ::arg().asNum("default-zsk-size");
1744 throw runtime_error("KSK key size must be equal to or greater than 0");
1747 if (k_algo
== "" && z_algo
== "") {
1748 throw runtime_error("Zero algorithms given for KSK+ZSK in total");
1752 throw runtime_error("ZSK key size must be equal to or greater than 0");
1755 if(dk
.isSecuredZone(zone
)) {
1756 cerr
<< "Zone '"<<zone
<<"' already secure, remove keys with pdnsutil remove-zone-key if needed"<<endl
;
1761 UeberBackend
B("default");
1762 if(!B
.getDomainInfo(zone
, di
) || !di
.backend
) { // di.backend and B are mostly identical
1763 cerr
<<"Can't find a zone called '"<<zone
<<"'"<<endl
;
1767 if(di
.kind
== DomainInfo::Slave
)
1769 cerr
<<"Warning! This is a slave domain! If this was a mistake, please run"<<endl
;
1770 cerr
<<"pdnsutil disable-dnssec "<<zone
<<" right now!"<<endl
;
1773 if (k_algo
!= "") { // Add a KSK
1775 cout
<< "Securing zone with key size " << k_size
<< endl
;
1777 cout
<< "Securing zone with default key size" << endl
;
1779 cout
<< "Adding "<<(z_algo
== "" ? "CSK (257)" : "KSK")<<" with algorithm " << k_algo
<< endl
;
1781 int k_real_algo
= DNSSECKeeper::shorthand2algorithm(k_algo
);
1783 if (!dk
.addKey(zone
, true, k_real_algo
, id
, k_size
, true)) {
1784 cerr
<<"No backend was able to secure '"<<zone
<<"', most likely because no DNSSEC"<<endl
;
1785 cerr
<<"capable backends are loaded, or because the backends have DNSSEC disabled."<<endl
;
1786 cerr
<<"For the Generic SQL backends, set the 'gsqlite3-dnssec', 'gmysql-dnssec' or"<<endl
;
1787 cerr
<<"'gpgsql-dnssec' flag. Also make sure the schema has been updated for DNSSEC!"<<endl
;
1793 cout
<< "Adding "<<(k_algo
== "" ? "CSK (256)" : "ZSK")<<" with algorithm " << z_algo
<< endl
;
1795 int z_real_algo
= DNSSECKeeper::shorthand2algorithm(z_algo
);
1797 if (!dk
.addKey(zone
, false, z_real_algo
, id
, z_size
, true)) {
1798 cerr
<<"No backend was able to secure '"<<zone
<<"', most likely because no DNSSEC"<<endl
;
1799 cerr
<<"capable backends are loaded, or because the backends have DNSSEC disabled."<<endl
;
1800 cerr
<<"For the Generic SQL backends, set the 'gsqlite3-dnssec', 'gmysql-dnssec' or"<<endl
;
1801 cerr
<<"'gpgsql-dnssec' flag. Also make sure the schema has been updated for DNSSEC!"<<endl
;
1806 if(!dk
.isSecuredZone(zone
)) {
1807 cerr
<<"Failed to secure zone. Is your backend dnssec enabled? (set "<<endl
;
1808 cerr
<<"gsqlite3-dnssec, or gmysql-dnssec etc). Check this first."<<endl
;
1809 cerr
<<"If you run with the BIND backend, make sure you have configured"<<endl
;
1810 cerr
<<"it to use DNSSEC with 'bind-dnssec-db=/path/fname' and"<<endl
;
1811 cerr
<<"'pdnsutil create-bind-db /path/fname'!"<<endl
;
1815 // rectifyZone(dk, zone);
1816 // showZone(dk, zone);
1817 cout
<<"Zone "<<zone
<<" secured"<<endl
;
1821 void testSchema(DNSSECKeeper
& dk
, const DNSName
& zone
)
1823 cout
<<"Note: test-schema will try to create the zone, but it will not remove it."<<endl
;
1824 cout
<<"Please clean up after this."<<endl
;
1826 cout
<<"If this test reports an error and aborts, please check your database schema."<<endl
;
1827 cout
<<"Constructing UeberBackend"<<endl
;
1828 UeberBackend
B("default");
1829 cout
<<"Picking first backend - if this is not what you want, edit launch line!"<<endl
;
1830 DNSBackend
*db
= B
.backends
[0];
1831 cout
<<"Creating slave domain "<<zone
<<endl
;
1832 db
->createSlaveDomain("127.0.0.1", zone
, "", "_testschema");
1833 cout
<<"Slave domain created"<<endl
;
1836 if(!B
.getDomainInfo(zone
, di
) || !di
.backend
) { // di.backend and B are mostly identical
1837 cout
<<"Can't find domain we just created, aborting"<<endl
;
1841 DNSResourceRecord rr
, rrget
;
1842 cout
<<"Starting transaction to feed records"<<endl
;
1843 db
->startTransaction(zone
, di
.id
);
1845 rr
.qtype
=QType::SOA
;
1850 rr
.content
="ns1.example.com. ahu.example.com. 2012081039 7200 3600 1209600 3600";
1851 cout
<<"Feeding SOA"<<endl
;
1852 db
->feedRecord(rr
, DNSName());
1853 rr
.qtype
=QType::TXT
;
1855 rr
.content
="\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"";
1856 cout
<<"Feeding overlong TXT"<<endl
;
1857 db
->feedRecord(rr
, DNSName());
1858 cout
<<"Committing"<<endl
;
1859 db
->commitTransaction();
1860 cout
<<"Querying TXT"<<endl
;
1861 db
->lookup(QType(QType::TXT
), zone
, di
.id
);
1864 DNSResourceRecord rrthrowaway
;
1865 if(db
->get(rrthrowaway
)) // should not touch rr but don't assume anything
1867 cout
<<"Expected one record, got multiple, aborting"<<endl
;
1870 int size
=rrget
.content
.size();
1873 cout
<<"Expected 302 bytes, got "<<size
<<", aborting"<<endl
;
1877 cout
<<"[+] content field is over 255 bytes"<<endl
;
1879 cout
<<"Dropping all records, inserting SOA+2xA"<<endl
;
1880 db
->startTransaction(zone
, di
.id
);
1882 rr
.qtype
=QType::SOA
;
1887 rr
.content
="ns1.example.com. ahu.example.com. 2012081039 7200 3600 1209600 3600";
1888 cout
<<"Feeding SOA"<<endl
;
1889 db
->feedRecord(rr
, DNSName());
1892 rr
.qname
=DNSName("_underscore")+zone
;
1893 rr
.content
="127.0.0.1";
1894 db
->feedRecord(rr
, DNSName());
1896 rr
.qname
=DNSName("bla")+zone
;
1897 cout
<<"Committing"<<endl
;
1898 db
->commitTransaction();
1900 cout
<<"Securing zone"<<endl
;
1901 secureZone(dk
, zone
);
1902 cout
<<"Rectifying zone"<<endl
;
1903 rectifyZone(dk
, zone
);
1904 cout
<<"Checking underscore ordering"<<endl
;
1905 DNSName before
, after
;
1906 db
->getBeforeAndAfterNames(di
.id
, zone
, DNSName("z")+zone
, before
, after
);
1907 cout
<<"got '"<<before
.toString()<<"' < 'z."<<zone
.toString()<<"' < '"<<after
.toString()<<"'"<<endl
;
1908 if(before
!= DNSName("_underscore")+zone
)
1910 cout
<<"before is wrong, got '"<<before
.toString()<<"', expected '_underscore."<<zone
.toString()<<"', aborting"<<endl
;
1915 cout
<<"after is wrong, got '"<<after
.toString()<<"', expected '"<<zone
.toString()<<"', aborting"<<endl
;
1918 cout
<<"[+] ordername sorting is correct for names starting with _"<<endl
;
1919 cout
<<"Setting low notified serial"<<endl
;
1920 db
->setNotified(di
.id
, 500);
1921 db
->getDomainInfo(zone
, di
);
1922 if(di
.notified_serial
!= 500) {
1923 cout
<<"[-] Set serial 500, got back "<<di
.notified_serial
<<", aborting"<<endl
;
1926 cout
<<"Setting serial that needs 32 bits"<<endl
;
1928 db
->setNotified(di
.id
, 2147484148);
1929 } catch(const PDNSException
&pe
) {
1930 cout
<<"While setting serial, got error: "<<pe
.reason
<<endl
;
1931 cout
<<"aborting"<<endl
;
1934 db
->getDomainInfo(zone
, di
);
1935 if(di
.notified_serial
!= 2147484148) {
1936 cout
<<"[-] Set serial 2147484148, got back "<<di
.notified_serial
<<", aborting"<<endl
;
1939 cout
<<"[+] Big serials work correctly"<<endl
;
1942 cout
<<"End of tests, please remove "<<zone
<<" from domains+records"<<endl
;
1945 int addOrSetMeta(const DNSName
& zone
, const string
& kind
, const vector
<string
>& values
, bool clobber
) {
1946 UeberBackend
B("default");
1949 if (!B
.getDomainInfo(zone
, di
)) {
1950 cerr
<< "Invalid zone '" << zone
<< "'" << endl
;
1954 vector
<string
> all_metadata
;
1957 B
.getDomainMetadata(zone
, kind
, all_metadata
);
1960 all_metadata
.insert(all_metadata
.end(), values
.begin(), values
.end());
1962 if (!B
.setDomainMetadata(zone
, kind
, all_metadata
)) {
1963 cerr
<< "Unable to set meta for '" << zone
<< "'" << endl
;
1967 cout
<< "Set '" << zone
<< "' meta " << kind
<< " = " << boost::join(all_metadata
, ", ") << endl
;
1971 int main(int argc
, char** argv
)
1974 po::options_description
desc("Allowed options");
1976 ("help,h", "produce help message")
1977 ("version", "show version")
1978 ("verbose,v", "be verbose")
1979 ("force", "force an action")
1980 ("config-name", po::value
<string
>()->default_value(""), "virtual configuration name")
1981 ("config-dir", po::value
<string
>()->default_value(SYSCONFDIR
), "location of pdns.conf")
1982 ("commands", po::value
<vector
<string
> >());
1984 po::positional_options_description p
;
1985 p
.add("commands", -1);
1986 po::store(po::command_line_parser(argc
, argv
).options(desc
).positional(p
).run(), g_vm
);
1989 vector
<string
> cmds
;
1991 if(g_vm
.count("commands"))
1992 cmds
= g_vm
["commands"].as
<vector
<string
> >();
1994 g_verbose
= g_vm
.count("verbose");
1996 if (g_vm
.count("version")) {
1997 cout
<<"pdnsutil "<<VERSION
<<endl
;
2001 if(cmds
.empty() || g_vm
.count("help") || cmds
[0] == "help") {
2002 cout
<<"Usage: \npdnsutil [options] <command> [params ..]\n"<<endl
;
2003 cout
<<"Commands:"<<endl
;
2004 cout
<<"activate-tsig-key ZONE NAME {master|slave}"<<endl
;
2005 cout
<<" Enable TSIG authenticated AXFR using the key NAME for ZONE"<<endl
;
2006 cout
<<"activate-zone-key ZONE KEY-ID Activate the key with key id KEY-ID in ZONE"<<endl
;
2007 cout
<<"add-record ZONE NAME TYPE [ttl] content"<<endl
;
2008 cout
<<" [content..] Add one or more records to ZONE"<<endl
;
2009 cout
<<"add-zone-key ZONE {zsk|ksk} [BITS] [active|inactive]"<<endl
;
2010 cout
<<" [rsasha1|rsasha1-nsec3-sha1|rsasha256|rsasha512|ecdsa256|ecdsa384";
2011 #if defined(HAVE_LIBSODIUM) || defined(HAVE_LIBDECAF) || defined(HAVE_LIBCRYPTO_ED25519)
2014 #if defined(HAVE_LIBDECAF) || defined(HAVE_LIBCRYPTO_ED448)
2018 cout
<<" Add a ZSK or KSK to zone and specify algo&bits"<<endl
;
2019 cout
<<"backend-cmd BACKEND CMD [CMD..] Perform one or more backend commands"<<endl
;
2020 cout
<<"b2b-migrate OLD NEW Move all data from one backend to another"<<endl
;
2021 cout
<<"bench-db [filename] Bench database backend with queries, one domain per line"<<endl
;
2022 cout
<<"check-zone ZONE Check a zone for correctness"<<endl
;
2023 cout
<<"check-all-zones [exit-on-error] Check all zones for correctness. Set exit-on-error to exit immediately"<<endl
;
2024 cout
<<" after finding an error in a zone."<<endl
;
2025 cout
<<"clear-zone ZONE Clear all records of a zone, but keep everything else"<<endl
;
2026 cout
<<"create-bind-db FNAME Create DNSSEC db for BIND backend (bind-dnssec-db)"<<endl
;
2027 cout
<<"create-slave-zone ZONE master-ip [master-ip..]"<<endl
;
2028 cout
<<" Create slave zone ZONE with master IP address master-ip"<<endl
;
2029 cout
<<"change-slave-zone-master ZONE master-ip [master-ip..]"<<endl
;
2030 cout
<<" Change slave zone ZONE master IP address to master-ip"<<endl
;
2031 cout
<<"create-zone ZONE [nsname] Create empty zone ZONE"<<endl
;
2032 cout
<<"deactivate-tsig-key ZONE NAME {master|slave}"<<endl
;
2033 cout
<<" Disable TSIG authenticated AXFR using the key NAME for ZONE"<<endl
;
2034 cout
<<"deactivate-zone-key ZONE KEY-ID Deactivate the key with key id KEY-ID in ZONE"<<endl
;
2035 cout
<<"delete-rrset ZONE NAME TYPE Delete named RRSET from zone"<<endl
;
2036 cout
<<"delete-tsig-key NAME Delete TSIG key (warning! will not unmap key!)"<<endl
;
2037 cout
<<"delete-zone ZONE Delete the zone"<<endl
;
2038 cout
<<"disable-dnssec ZONE Deactivate all keys and unset PRESIGNED in ZONE"<<endl
;
2039 cout
<<"edit-zone ZONE Edit zone contents using $EDITOR"<<endl
;
2040 cout
<<"export-zone-dnskey ZONE KEY-ID Export to stdout the public DNSKEY described"<<endl
;
2041 cout
<<"export-zone-ds ZONE Export to stdout all KSK DS records for ZONE"<<endl
;
2042 cout
<<"export-zone-key ZONE KEY-ID Export to stdout the private key described"<<endl
;
2043 cout
<<"generate-tsig-key NAME ALGORITHM Generate new TSIG key"<<endl
;
2044 cout
<<"generate-zone-key {zsk|ksk} [ALGORITHM] [BITS]"<<endl
;
2045 cout
<<" Generate a ZSK or KSK to stdout with specified ALGORITHM and BITS"<<endl
;
2046 cout
<<"get-meta ZONE [KIND ...] Get zone metadata. If no KIND given, lists all known"<<endl
;
2047 cout
<<"hash-zone-record ZONE RNAME Calculate the NSEC3 hash for RNAME in ZONE"<<endl
;
2049 cout
<<"hsm assign ZONE ALGORITHM {ksk|zsk} MODULE SLOT PIN LABEL"<<endl
<<
2050 " Assign a hardware signing module to a ZONE"<<endl
;
2051 cout
<<"hsm create-key ZONE KEY-ID [BITS] Create a key using hardware signing module for ZONE (use assign first)"<<endl
;
2052 cout
<<" BITS defaults to 2048"<<endl
;
2054 cout
<<"increase-serial ZONE Increases the SOA-serial by 1. Uses SOA-EDIT"<<endl
;
2055 cout
<<"import-tsig-key NAME ALGORITHM KEY Import TSIG key"<<endl
;
2056 cout
<<"import-zone-key ZONE FILE Import from a file a private key, ZSK or KSK"<<endl
;
2057 cout
<<" [active|inactive] [ksk|zsk] Defaults to KSK and active"<<endl
;
2058 cout
<<"ipdecrypt IP passphrase/key [key] Encrypt IP address using passphrase or base64 key"<<endl
;
2059 cout
<<"ipencrypt IP passphrase/key [key] Encrypt IP address using passphrase or base64 key"<<endl
;
2060 cout
<<"load-zone ZONE FILE Load ZONE from FILE, possibly creating zone or atomically"<<endl
;
2061 cout
<<" replacing contents"<<endl
;
2062 cout
<<"list-algorithms [with-backend] List all DNSSEC algorithms supported, optionally also listing the crypto library used"<<endl
;
2063 cout
<<"list-keys [ZONE] List DNSSEC keys for ZONE. When ZONE is unset or \"all\", display all keys for all zones"<<endl
;
2064 cout
<<"list-zone ZONE List zone contents"<<endl
;
2065 cout
<<"list-all-zones [master|slave|native]"<<endl
;
2066 cout
<<" List all zone names"<<endl
;;
2067 cout
<<"list-tsig-keys List all TSIG keys"<<endl
;
2068 cout
<<"rectify-zone ZONE [ZONE ..] Fix up DNSSEC fields (order, auth)"<<endl
;
2069 cout
<<"rectify-all-zones [quiet] Rectify all zones. Optionally quiet output with errors only"<<endl
;
2070 cout
<<"remove-zone-key ZONE KEY-ID Remove key with KEY-ID from ZONE"<<endl
;
2071 cout
<<"replace-rrset ZONE NAME TYPE [ttl] Replace named RRSET from zone"<<endl
;
2072 cout
<<" content [content..]"<<endl
;
2073 cout
<<"secure-all-zones [increase-serial] Secure all zones without keys"<<endl
;
2074 cout
<<"secure-zone ZONE [ZONE ..] Add DNSSEC to zone ZONE"<<endl
;
2075 cout
<<"set-kind ZONE KIND Change the kind of ZONE to KIND (master, slave native)"<<endl
;
2076 cout
<<"set-account ZONE ACCOUNT Change the account (owner) of ZONE to ACCOUNT"<<endl
;
2077 cout
<<"set-nsec3 ZONE ['PARAMS' [narrow]] Enable NSEC3 with PARAMS. Optionally narrow"<<endl
;
2078 cout
<<"set-presigned ZONE Use presigned RRSIGs from storage"<<endl
;
2079 cout
<<"set-publish-cdnskey ZONE Enable sending CDNSKEY responses for ZONE"<<endl
;
2080 cout
<<"set-publish-cds ZONE [DIGESTALGOS] Enable sending CDS responses for ZONE, using DIGESTALGOS as signature algorithms"<<endl
;
2081 cout
<<" DIGESTALGOS should be a comma separated list of numbers, it is '2' by default"<<endl
;
2082 cout
<<"add-meta ZONE KIND VALUE Add zone metadata, this adds to the existing KIND"<<endl
;
2083 cout
<<" [VALUE ...]"<<endl
;
2084 cout
<<"set-meta ZONE KIND [VALUE] [VALUE] Set zone metadata, optionally providing a value. *No* value clears meta"<<endl
;
2085 cout
<<" Note - this will replace all metadata records of KIND!"<<endl
;
2086 cout
<<"show-zone ZONE Show DNSSEC (public) key details about a zone"<<endl
;
2087 cout
<<"unset-nsec3 ZONE Switch back to NSEC"<<endl
;
2088 cout
<<"unset-presigned ZONE No longer use presigned RRSIGs"<<endl
;
2089 cout
<<"unset-publish-cdnskey ZONE Disable sending CDNSKEY responses for ZONE"<<endl
;
2090 cout
<<"unset-publish-cds ZONE Disable sending CDS responses for ZONE"<<endl
;
2091 cout
<<"test-schema ZONE Test DB schema - will create ZONE"<<endl
;
2096 loadMainConfig(g_vm
["config-dir"].as
<string
>());
2098 if (cmds
[0] == "test-algorithm") {
2099 if(cmds
.size() != 2) {
2100 cerr
<< "Syntax: pdnsutil test-algorithm algonum"<<endl
;
2103 if (testAlgorithm(pdns_stou(cmds
[1])))
2108 if(cmds
[0] == "ipencrypt" || cmds
[0]=="ipdecrypt") {
2109 if(cmds
.size() < 3 || (cmds
.size()== 4 && cmds
[3]!="key")) {
2110 cerr
<<"Syntax: pdnsutil [ipencrypt|ipdecrypt] IP passphrase [key]"<<endl
;
2114 if(cmds
.size()==4) {
2115 if(B64Decode(cmds
[2], key
) < 0) {
2116 cerr
<<"Could not parse '"<<cmds
[3]<<"' as base64"<<endl
;
2121 key
= makeIPCipherKey(cmds
[2]);
2123 exit(xcryptIP(cmds
[0], cmds
[1], key
));
2127 if(cmds
[0] == "test-algorithms") {
2128 if (testAlgorithms())
2133 if(cmds
[0] == "list-algorithms") {
2134 if((cmds
.size() == 2 && cmds
[1] != "with-backend") || cmds
.size() > 2) {
2135 cerr
<<"Syntax: pdnsutil list-algorithms [with-backend]"<<endl
;
2139 cout
<<"DNSKEY algorithms supported by this installation of PowerDNS:"<<endl
;
2141 auto algosWithBackend
= DNSCryptoKeyEngine::listAllAlgosWithBackend();
2142 for (auto const algoWithBackend
: algosWithBackend
){
2143 string algoName
= DNSSECKeeper::algorithm2name(algoWithBackend
.first
);
2144 cout
<<std::to_string(algoWithBackend
.first
)<<" - "<<algoName
;
2145 if (cmds
.size() == 2 && cmds
[1] == "with-backend")
2146 cout
<<" using "<<algoWithBackend
.second
;
2154 if(cmds
[0] == "create-bind-db") {
2156 if(cmds
.size() != 2) {
2157 cerr
<< "Syntax: pdnsutil create-bind-db FNAME"<<endl
;
2161 SSQLite3
db(cmds
[1], "", true); // create=ok
2162 vector
<string
> statements
;
2163 stringtok(statements
, sqlCreate
, ";");
2164 for(const string
& statement
: statements
) {
2165 db
.execute(statement
);
2168 catch(SSqlException
& se
) {
2169 throw PDNSException("Error creating database in BIND backend: "+se
.txtReason());
2173 cerr
<<"bind-dnssec-db requires building PowerDNS with SQLite3"<<endl
;
2180 if (cmds
[0] == "test-schema") {
2181 if(cmds
.size() != 2) {
2182 cerr
<< "Syntax: pdnsutil test-schema ZONE"<<endl
;
2185 testSchema(dk
, DNSName(cmds
[1]));
2188 if(cmds
[0] == "rectify-zone") {
2189 if(cmds
.size() < 2) {
2190 cerr
<< "Syntax: pdnsutil rectify-zone ZONE [ZONE..]"<<endl
;
2193 unsigned int exitCode
= 0;
2194 for(unsigned int n
= 1; n
< cmds
.size(); ++n
)
2195 if (!rectifyZone(dk
, DNSName(cmds
[n
])))
2199 else if (cmds
[0] == "rectify-all-zones") {
2200 bool quiet
= (cmds
.size() >= 2 && cmds
[1] == "quiet");
2201 if (!rectifyAllZones(dk
, quiet
)) {
2205 else if(cmds
[0] == "check-zone") {
2206 if(cmds
.size() != 2) {
2207 cerr
<< "Syntax: pdnsutil check-zone ZONE"<<endl
;
2210 UeberBackend
B("default");
2211 exit(checkZone(dk
, B
, DNSName(cmds
[1])));
2213 else if(cmds
[0] == "bench-db") {
2214 dbBench(cmds
.size() > 1 ? cmds
[1] : "");
2216 else if (cmds
[0] == "check-all-zones") {
2217 bool exitOnError
= ((cmds
.size() >= 2 ? cmds
[1] : "") == "exit-on-error");
2218 exit(checkAllZones(dk
, exitOnError
));
2220 else if (cmds
[0] == "list-all-zones") {
2221 if (cmds
.size() > 2) {
2222 cerr
<< "Syntax: pdnsutil list-all-zones [master|slave|native]"<<endl
;
2225 if (cmds
.size() == 2)
2226 return listAllZones(cmds
[1]);
2227 return listAllZones();
2229 else if (cmds
[0] == "test-zone") {
2230 cerr
<< "Did you mean check-zone?"<<endl
;
2233 else if (cmds
[0] == "test-all-zones") {
2234 cerr
<< "Did you mean check-all-zones?"<<endl
;
2238 else if(cmds
[0] == "signing-server" )
2242 else if(cmds
[0] == "signing-slave")
2244 launchSigningService(0);
2247 else if(cmds
[0] == "test-speed") {
2248 if(cmds
.size() < 2) {
2249 cerr
<< "Syntax: pdnsutil test-speed numcores [signing-server]"<<endl
;
2252 testSpeed(dk
, DNSName(cmds
[1]), (cmds
.size() > 3) ? cmds
[3] : "", pdns_stou(cmds
[2]));
2254 else if(cmds
[0] == "verify-crypto") {
2255 if(cmds
.size() != 2) {
2256 cerr
<< "Syntax: pdnsutil verify-crypto FILE"<<endl
;
2259 verifyCrypto(cmds
[1]);
2261 else if(cmds
[0] == "show-zone") {
2262 if(cmds
.size() != 2) {
2263 cerr
<< "Syntax: pdnsutil show-zone ZONE"<<endl
;
2266 if (!showZone(dk
, DNSName(cmds
[1]))) return 1;
2268 else if(cmds
[0] == "export-zone-ds") {
2269 if(cmds
.size() != 2) {
2270 cerr
<< "Syntax: pdnsutil export-zone-ds ZONE"<<endl
;
2273 if (!showZone(dk
, DNSName(cmds
[1]), true)) return 1;
2275 else if(cmds
[0] == "disable-dnssec") {
2276 if(cmds
.size() != 2) {
2277 cerr
<< "Syntax: pdnsutil disable-dnssec ZONE"<<endl
;
2280 DNSName
zone(cmds
[1]);
2281 if(!disableDNSSECOnZone(dk
, zone
)) {
2282 cerr
<< "Cannot disable DNSSEC on " << zone
<< endl
;
2286 else if(cmds
[0] == "activate-zone-key") {
2287 if(cmds
.size() != 3) {
2288 cerr
<< "Syntax: pdnsutil activate-zone-key ZONE KEY-ID"<<endl
;
2291 DNSName
zone(cmds
[1]);
2292 unsigned int id
=atoi(cmds
[2].c_str()); // if you make this pdns_stou, the error gets worse
2295 cerr
<<"Invalid KEY-ID '"<<cmds
[2]<<"'"<<endl
;
2298 if (!dk
.activateKey(zone
, id
)) {
2299 cerr
<<"Activation of key failed"<<endl
;
2304 else if(cmds
[0] == "deactivate-zone-key") {
2305 if(cmds
.size() != 3) {
2306 cerr
<< "Syntax: pdnsutil deactivate-zone-key ZONE KEY-ID"<<endl
;
2309 DNSName
zone(cmds
[1]);
2310 unsigned int id
=pdns_stou(cmds
[2]);
2313 cerr
<<"Invalid KEY-ID"<<endl
;
2316 if (!dk
.deactivateKey(zone
, id
)) {
2317 cerr
<<"Deactivation of key failed"<<endl
;
2322 else if(cmds
[0] == "add-zone-key") {
2323 if(cmds
.size() < 3 ) {
2324 cerr
<< "Syntax: pdnsutil add-zone-key ZONE zsk|ksk [BITS] [active|inactive] [rsasha1|rsasha1-nsec3-sha1|rsasha256|rsasha512|ecdsa256|ecdsa384";
2325 #if defined(HAVE_LIBSODIUM) || defined(HAVE_LIBDECAF) || defined(HAVE_LIBCRYPTO_ED25519)
2328 #if defined(HAVE_LIBDECAF) || defined(HAVE_LIBCRYPTO_ED448)
2334 DNSName
zone(cmds
[1]);
2336 UeberBackend
B("default");
2339 if (!B
.getDomainInfo(zone
, di
)){
2340 cerr
<< "No such zone in the database" << endl
;
2344 // need to get algorithm, bits & ksk or zsk from commandline
2345 bool keyOrZone
=false;
2348 int algorithm
=DNSSECKeeper::ECDSA256
;
2350 for(unsigned int n
=2; n
< cmds
.size(); ++n
) {
2351 if(pdns_iequals(cmds
[n
], "zsk"))
2353 else if(pdns_iequals(cmds
[n
], "ksk"))
2355 else if((tmp_algo
= DNSSECKeeper::shorthand2algorithm(cmds
[n
]))>0) {
2356 algorithm
= tmp_algo
;
2357 } else if(pdns_iequals(cmds
[n
], "active")) {
2359 } else if(pdns_iequals(cmds
[n
], "inactive") || pdns_iequals(cmds
[n
], "passive")) { // 'passive' eventually needs to be removed
2361 } else if(pdns_stou(cmds
[n
])) {
2362 bits
= pdns_stou(cmds
[n
]);
2364 cerr
<<"Unknown algorithm, key flag or size '"<<cmds
[n
]<<"'"<<endl
;
2365 exit(EXIT_FAILURE
);;
2369 if (!dk
.addKey(zone
, keyOrZone
, algorithm
, id
, bits
, active
)) {
2370 cerr
<<"Adding key failed, perhaps DNSSEC not enabled in configuration?"<<endl
;
2373 cerr
<<"Added a " << (keyOrZone
? "KSK" : "ZSK")<<" with algorithm = "<<algorithm
<<", active="<<active
<<endl
;
2375 cerr
<<"Requested specific key size of "<<bits
<<" bits"<<endl
;
2377 cerr
<<std::to_string(id
)<<"Key was added, but backend does not support returning of key id"<<endl
;
2378 } else if (id
< -1) {
2379 cerr
<<std::to_string(id
)<<"Key was added, but there was a failure while returning the key id"<<endl
;
2381 cout
<<std::to_string(id
)<<endl
;
2385 else if(cmds
[0] == "remove-zone-key") {
2386 if(cmds
.size() < 3) {
2387 cerr
<<"Syntax: pdnsutil remove-zone-key ZONE KEY-ID"<<endl
;
2390 DNSName
zone(cmds
[1]);
2391 unsigned int id
=pdns_stou(cmds
[2]);
2392 if (!dk
.removeKey(zone
, id
)) {
2393 cerr
<<"Cannot remove key " << id
<< " from " << zone
<<endl
;
2398 else if(cmds
[0] == "delete-zone") {
2399 if(cmds
.size() != 2) {
2400 cerr
<<"Syntax: pdnsutil delete-zone ZONE"<<endl
;
2403 exit(deleteZone(DNSName(cmds
[1])));
2405 else if(cmds
[0] == "create-zone") {
2406 if(cmds
.size() != 2 && cmds
.size()!=3 ) {
2407 cerr
<<"Syntax: pdnsutil create-zone ZONE [nsname]"<<endl
;
2410 exit(createZone(DNSName(cmds
[1]), cmds
.size() > 2 ? DNSName(cmds
[2]): DNSName()));
2412 else if(cmds
[0] == "create-slave-zone") {
2413 if(cmds
.size() < 3 ) {
2414 cerr
<<"Syntax: pdnsutil create-slave-zone ZONE master-ip [master-ip..]"<<endl
;
2417 exit(createSlaveZone(cmds
));
2419 else if(cmds
[0] == "change-slave-zone-master") {
2420 if(cmds
.size() < 3 ) {
2421 cerr
<<"Syntax: pdnsutil change-slave-zone-master ZONE master-ip [master-ip..]"<<endl
;
2424 exit(changeSlaveZoneMaster(cmds
));
2426 else if(cmds
[0] == "add-record") {
2427 if(cmds
.size() < 5) {
2428 cerr
<<"Syntax: pdnsutil add-record ZONE name type [ttl] \"content\" [\"content\"...]"<<endl
;
2431 exit(addOrReplaceRecord(true, cmds
));
2433 else if(cmds
[0] == "replace-rrset") {
2434 if(cmds
.size() < 5) {
2435 cerr
<<"Syntax: pdnsutil replace-rrset ZONE name type [ttl] \"content\" [\"content\"...]"<<endl
;
2438 exit(addOrReplaceRecord(false , cmds
));
2440 else if(cmds
[0] == "delete-rrset") {
2441 if(cmds
.size() != 4) {
2442 cerr
<<"Syntax: pdnsutil delete-rrset ZONE name type"<<endl
;
2445 exit(deleteRRSet(cmds
[1], cmds
[2], cmds
[3]));
2447 else if(cmds
[0] == "list-zone") {
2448 if(cmds
.size() != 2) {
2449 cerr
<<"Syntax: pdnsutil list-zone ZONE"<<endl
;
2455 exit(listZone(DNSName(cmds
[1])));
2457 else if(cmds
[0] == "edit-zone") {
2458 if(cmds
.size() != 2) {
2459 cerr
<<"Syntax: pdnsutil edit-zone ZONE"<<endl
;
2465 exit(editZone(DNSName(cmds
[1])));
2467 else if(cmds
[0] == "clear-zone") {
2468 if(cmds
.size() != 2) {
2469 cerr
<<"Syntax: pdnsutil edit-zone ZONE"<<endl
;
2475 exit(clearZone(dk
, DNSName(cmds
[1])));
2477 else if(cmds
[0] == "list-keys") {
2478 if(cmds
.size() > 2) {
2479 cerr
<<"Syntax: pdnsutil list-keys [ZONE]"<<endl
;
2482 string zname
= (cmds
.size() == 2) ? cmds
[1] : "all";
2483 exit(listKeys(zname
, dk
));
2485 else if(cmds
[0] == "load-zone") {
2486 if(cmds
.size() < 3) {
2487 cerr
<<"Syntax: pdnsutil load-zone ZONE FILENAME [ZONE FILENAME] .."<<endl
;
2493 for(size_t n
=1; n
+ 2 <= cmds
.size(); n
+=2)
2494 loadZone(DNSName(cmds
[n
]), cmds
[n
+1]);
2497 else if(cmds
[0] == "secure-zone") {
2498 if(cmds
.size() < 2) {
2499 cerr
<< "Syntax: pdnsutil secure-zone ZONE"<<endl
;
2502 vector
<DNSName
> mustRectify
;
2503 unsigned int zoneErrors
=0;
2504 for(unsigned int n
= 1; n
< cmds
.size(); ++n
) {
2505 DNSName
zone(cmds
[n
]);
2506 dk
.startTransaction(zone
, -1);
2507 if(secureZone(dk
, zone
)) {
2508 mustRectify
.push_back(zone
);
2512 dk
.commitTransaction();
2515 for(const auto& zone
: mustRectify
)
2516 rectifyZone(dk
, zone
);
2523 else if (cmds
[0] == "secure-all-zones") {
2524 if (cmds
.size() >= 2 && !pdns_iequals(cmds
[1], "increase-serial")) {
2525 cerr
<< "Syntax: pdnsutil secure-all-zones [increase-serial]"<<endl
;
2529 UeberBackend
B("default");
2531 vector
<DomainInfo
> domainInfo
;
2532 B
.getAllDomains(&domainInfo
);
2534 unsigned int zonesSecured
=0, zoneErrors
=0;
2535 for(DomainInfo di
: domainInfo
) {
2536 if(!dk
.isSecuredZone(di
.zone
)) {
2537 cout
<<"Securing "<<di
.zone
<<": ";
2538 if (secureZone(dk
, di
.zone
)) {
2540 if (cmds
.size() == 2) {
2541 if (!increaseSerial(di
.zone
, dk
))
2550 cout
<<"Secured: "<<zonesSecured
<<" zones. Errors: "<<zoneErrors
<<endl
;
2557 else if(cmds
[0]=="set-kind") {
2558 if(cmds
.size() != 3) {
2559 cerr
<<"Syntax: pdnsutil set-kind ZONE KIND"<<endl
;
2562 DNSName
zone(cmds
[1]);
2563 auto kind
=DomainInfo::stringToKind(cmds
[2]);
2564 exit(setZoneKind(zone
, kind
));
2566 else if(cmds
[0]=="set-account") {
2567 if(cmds
.size() != 3) {
2568 cerr
<<"Syntax: pdnsutil set-account ZONE ACCOUNT"<<endl
;
2571 DNSName
zone(cmds
[1]);
2572 exit(setZoneAccount(zone
, cmds
[2]));
2574 else if(cmds
[0]=="set-nsec3") {
2575 if(cmds
.size() < 2) {
2576 cerr
<<"Syntax: pdnsutil set-nsec3 ZONE 'params' [narrow]"<<endl
;
2579 string nsec3params
= cmds
.size() > 2 ? cmds
[2] : "1 0 1 ab";
2580 bool narrow
= cmds
.size() > 3 && cmds
[3]=="narrow";
2581 NSEC3PARAMRecordContent
ns3pr(nsec3params
);
2583 DNSName
zone(cmds
[1]);
2584 if (zone
.wirelength() > 222) {
2585 cerr
<<"Cannot enable NSEC3 for " << zone
<< " as it is too long (" << zone
.wirelength() << " bytes, maximum is 222 bytes)"<<endl
;
2588 if(ns3pr
.d_algorithm
!= 1) {
2589 cerr
<<"NSEC3PARAM algorithm set to '"<<std::to_string(ns3pr
.d_algorithm
)<<"', but '1' is the only valid value"<<endl
;
2590 return EXIT_FAILURE
;
2592 if (! dk
.setNSEC3PARAM(zone
, ns3pr
, narrow
)) {
2593 cerr
<<"Cannot set NSEC3 param for " << zone
<< endl
;
2598 cerr
<<"NSEC3 set, ";
2600 cerr
<<"NSEC3 (opt-out) set, ";
2602 if(dk
.isSecuredZone(zone
))
2603 cerr
<<"please rectify your zone if your backend needs it"<<endl
;
2605 cerr
<<"please secure and rectify your zone."<<endl
;
2609 else if(cmds
[0]=="set-presigned") {
2610 if(cmds
.size() < 2) {
2611 cerr
<<"Syntax: pdnsutil set-presigned ZONE"<<endl
;
2614 if (! dk
.setPresigned(DNSName(cmds
[1]))) {
2615 cerr
<< "Could not set presigned for " << cmds
[1] << " (is DNSSEC enabled in your backend?)" << endl
;
2620 else if(cmds
[0]=="set-publish-cdnskey") {
2621 if(cmds
.size() < 2) {
2622 cerr
<<"Syntax: pdnsutil set-publish-cdnskey ZONE"<<endl
;
2625 if (! dk
.setPublishCDNSKEY(DNSName(cmds
[1]))) {
2626 cerr
<< "Could not set publishing for CDNSKEY records for "<< cmds
[1]<<endl
;
2631 else if(cmds
[0]=="set-publish-cds") {
2632 if(cmds
.size() < 2) {
2633 cerr
<<"Syntax: pdnsutil set-publish-cds ZONE [DIGESTALGOS]"<<endl
;
2637 // If DIGESTALGOS is unset
2638 if(cmds
.size() == 2)
2639 cmds
.push_back("2");
2641 if (! dk
.setPublishCDS(DNSName(cmds
[1]), cmds
[2])) {
2642 cerr
<< "Could not set publishing for CDS records for "<< cmds
[1]<<endl
;
2647 else if(cmds
[0]=="unset-presigned") {
2648 if(cmds
.size() < 2) {
2649 cerr
<<"Syntax: pdnsutil unset-presigned ZONE"<<endl
;
2652 if (! dk
.unsetPresigned(DNSName(cmds
[1]))) {
2653 cerr
<< "Could not unset presigned on for " << cmds
[1] << endl
;
2658 else if(cmds
[0]=="unset-publish-cdnskey") {
2659 if(cmds
.size() < 2) {
2660 cerr
<<"Syntax: pdnsutil unset-publish-cdnskey ZONE"<<endl
;
2663 if (! dk
.unsetPublishCDNSKEY(DNSName(cmds
[1]))) {
2664 cerr
<< "Could not unset publishing for CDNSKEY records for "<< cmds
[1]<<endl
;
2669 else if(cmds
[0]=="unset-publish-cds") {
2670 if(cmds
.size() < 2) {
2671 cerr
<<"Syntax: pdnsutil unset-publish-cds ZONE"<<endl
;
2674 if (! dk
.unsetPublishCDS(DNSName(cmds
[1]))) {
2675 cerr
<< "Could not unset publishing for CDS records for "<< cmds
[1]<<endl
;
2680 else if(cmds
[0]=="hash-zone-record") {
2681 if(cmds
.size() < 3) {
2682 cerr
<<"Syntax: pdnsutil hash-zone-record ZONE RNAME"<<endl
;
2685 DNSName
zone(cmds
[1]);
2686 DNSName
record(cmds
[2]);
2687 NSEC3PARAMRecordContent ns3pr
;
2689 if(!dk
.getNSEC3PARAM(zone
, &ns3pr
, &narrow
)) {
2690 cerr
<<"The '"<<zone
<<"' zone does not use NSEC3"<<endl
;
2694 cerr
<<"The '"<<zone
<<"' zone uses narrow NSEC3, but calculating hash anyhow"<<endl
;
2697 cout
<<toBase32Hex(hashQNameWithSalt(ns3pr
, record
))<<endl
;
2699 else if(cmds
[0]=="unset-nsec3") {
2700 if(cmds
.size() < 2) {
2701 cerr
<<"Syntax: pdnsutil unset-nsec3 ZONE"<<endl
;
2704 if ( ! dk
.unsetNSEC3PARAM(DNSName(cmds
[1]))) {
2705 cerr
<<"Cannot unset NSEC3 param for " << cmds
[1] << endl
;
2710 else if(cmds
[0]=="export-zone-key") {
2711 if(cmds
.size() < 3) {
2712 cerr
<<"Syntax: pdnsutil export-zone-key ZONE KEY-ID"<<endl
;
2716 string zone
=cmds
[1];
2717 unsigned int id
=pdns_stou(cmds
[2]);
2718 DNSSECPrivateKey dpk
=dk
.getKeyById(DNSName(zone
), id
);
2719 cout
<< dpk
.getKey()->convertToISC() <<endl
;
2721 else if(cmds
[0]=="increase-serial") {
2722 if (cmds
.size() < 2) {
2723 cerr
<<"Syntax: pdnsutil increase-serial ZONE"<<endl
;
2726 return increaseSerial(DNSName(cmds
[1]), dk
);
2728 else if(cmds
[0]=="import-zone-key-pem") {
2729 if(cmds
.size() < 4) {
2730 cerr
<<"Syntax: pdnsutil import-zone-key-pem ZONE FILE ALGORITHM {ksk|zsk}"<<endl
;
2733 string zone
=cmds
[1];
2734 string fname
=cmds
[2];
2736 ifstream
ifs(fname
.c_str());
2737 string tmp
, interim
, raw
;
2738 while(getline(ifs
, line
)) {
2744 B64Decode(interim
, raw
);
2745 DNSSECPrivateKey dpk
;
2746 DNSKEYRecordContent drc
;
2747 shared_ptr
<DNSCryptoKeyEngine
> key(DNSCryptoKeyEngine::makeFromPEMString(drc
, raw
));
2750 dpk
.d_algorithm
= pdns_stou(cmds
[3]);
2752 if(dpk
.d_algorithm
== DNSSECKeeper::RSASHA1NSEC3SHA1
)
2753 dpk
.d_algorithm
= DNSSECKeeper::RSASHA1
;
2755 cerr
<<(int)dpk
.d_algorithm
<<endl
;
2757 if(cmds
.size() > 4) {
2758 if(pdns_iequals(cmds
[4], "ZSK"))
2760 else if(pdns_iequals(cmds
[4], "KSK"))
2763 cerr
<<"Unknown key flag '"<<cmds
[4]<<"'"<<endl
;
2768 dpk
.d_flags
= 257; // ksk
2771 if (!dk
.addKey(DNSName(zone
), dpk
, id
)) {
2772 cerr
<<"Adding key failed, perhaps DNSSEC not enabled in configuration?"<<endl
;
2776 cerr
<<std::to_string(id
)<<"Key was added, but backend does not support returning of key id"<<endl
;
2777 } else if (id
< -1) {
2778 cerr
<<std::to_string(id
)<<"Key was added, but there was a failure while returning the key id"<<endl
;
2780 cout
<<std::to_string(id
)<<endl
;
2784 else if(cmds
[0]=="import-zone-key") {
2785 if(cmds
.size() < 3) {
2786 cerr
<<"Syntax: pdnsutil import-zone-key ZONE FILE [ksk|zsk] [active|inactive]"<<endl
;
2789 string zone
=cmds
[1];
2790 string fname
=cmds
[2];
2791 DNSSECPrivateKey dpk
;
2792 DNSKEYRecordContent drc
;
2793 shared_ptr
<DNSCryptoKeyEngine
> key(DNSCryptoKeyEngine::makeFromISCFile(drc
, fname
.c_str()));
2795 dpk
.d_algorithm
= drc
.d_algorithm
;
2797 if(dpk
.d_algorithm
== DNSSECKeeper::RSASHA1NSEC3SHA1
)
2798 dpk
.d_algorithm
= DNSSECKeeper::RSASHA1
;
2803 for(unsigned int n
= 3; n
< cmds
.size(); ++n
) {
2804 if(pdns_iequals(cmds
[n
], "ZSK"))
2806 else if(pdns_iequals(cmds
[n
], "KSK"))
2808 else if(pdns_iequals(cmds
[n
], "active"))
2810 else if(pdns_iequals(cmds
[n
], "passive") || pdns_iequals(cmds
[n
], "inactive")) // passive eventually needs to be removed
2813 cerr
<<"Unknown key flag '"<<cmds
[n
]<<"'"<<endl
;
2818 if (!dk
.addKey(DNSName(zone
), dpk
, id
, active
)) {
2819 cerr
<<"Adding key failed, perhaps DNSSEC not enabled in configuration?"<<endl
;
2823 cerr
<<std::to_string(id
)<<"Key was added, but backend does not support returning of key id"<<endl
;
2824 } else if (id
< -1) {
2825 cerr
<<std::to_string(id
)<<"Key was added, but there was a failure while returning the key id"<<endl
;
2827 cout
<<std::to_string(id
)<<endl
;
2830 else if(cmds
[0]=="export-zone-dnskey") {
2831 if(cmds
.size() < 3) {
2832 cerr
<<"Syntax: pdnsutil export-zone-dnskey ZONE KEY-ID"<<endl
;
2836 DNSName
zone(cmds
[1]);
2837 unsigned int id
=pdns_stou(cmds
[2]);
2838 DNSSECPrivateKey dpk
=dk
.getKeyById(zone
, id
);
2839 cout
<< zone
<<" IN DNSKEY "<<dpk
.getDNSKEY().getZoneRepresentation() <<endl
;
2841 else if(cmds
[0] == "generate-zone-key") {
2842 if(cmds
.size() < 2 ) {
2843 cerr
<< "Syntax: pdnsutil generate-zone-key zsk|ksk [rsasha1|rsasha1-nsec3-sha1|rsasha256|rsasha512|ecdsa256|ecdsa384";
2844 #if defined(HAVE_LIBSODIUM) || defined(HAVE_LIBDECAF) || defined(HAVE_LIBCRYPTO_ED25519)
2847 #if defined(HAVE_LIBDECAF) || defined(HAVE_LIBCRYPTO_ED448)
2850 cerr
<< "] [bits]"<<endl
;
2853 // need to get algorithm, bits & ksk or zsk from commandline
2854 bool keyOrZone
=false;
2857 int algorithm
=DNSSECKeeper::ECDSA256
;
2858 for(unsigned int n
=1; n
< cmds
.size(); ++n
) {
2859 if(pdns_iequals(cmds
[n
], "zsk"))
2861 else if(pdns_iequals(cmds
[n
], "ksk"))
2863 else if((tmp_algo
= DNSSECKeeper::shorthand2algorithm(cmds
[n
]))>0) {
2864 algorithm
= tmp_algo
;
2865 } else if(pdns_stou(cmds
[n
]))
2866 bits
= pdns_stou(cmds
[n
]);
2868 cerr
<<"Unknown algorithm, key flag or size '"<<cmds
[n
]<<"'"<<endl
;
2872 cerr
<<"Generating a " << (keyOrZone
? "KSK" : "ZSK")<<" with algorithm = "<<algorithm
<<endl
;
2874 cerr
<<"Requesting specific key size of "<<bits
<<" bits"<<endl
;
2876 DNSSECPrivateKey dspk
;
2877 shared_ptr
<DNSCryptoKeyEngine
> dpk(DNSCryptoKeyEngine::make(algorithm
));
2880 bits
= keyOrZone
? 2048 : 1024;
2882 if(algorithm
== DNSSECKeeper::ECCGOST
|| algorithm
== DNSSECKeeper::ECDSA256
|| algorithm
== DNSSECKeeper::ED25519
)
2884 else if(algorithm
== DNSSECKeeper::ECDSA384
)
2886 else if(algorithm
== DNSSECKeeper::ED448
)
2889 throw runtime_error("Can not guess key size for algorithm "+std::to_string(algorithm
));
2895 dspk
.d_algorithm
= algorithm
;
2896 dspk
.d_flags
= keyOrZone
? 257 : 256;
2898 // print key to stdout
2899 cout
<< "Flags: " << dspk
.d_flags
<< endl
<<
2900 dspk
.getKey()->convertToISC() << endl
;
2901 } else if (cmds
[0]=="generate-tsig-key") {
2902 string usage
= "Syntax: " + cmds
[0] + " name (hmac-md5|hmac-sha1|hmac-sha224|hmac-sha256|hmac-sha384|hmac-sha512)";
2903 if (cmds
.size() < 3) {
2904 cerr
<< usage
<< endl
;
2907 DNSName
name(cmds
[1]);
2908 DNSName
algo(cmds
[2]);
2911 key
= makeTSIGKey(algo
);
2912 } catch(const PDNSException
& e
) {
2913 cerr
<< "Could not create new TSIG key " << name
<< " " << algo
<< ": "<< e
.reason
<< endl
;
2917 UeberBackend
B("default");
2918 if (B
.setTSIGKey(name
, DNSName(algo
), key
)) { // you are feeling bored, put up DNSName(algo) up earlier
2919 cout
<< "Create new TSIG key " << name
<< " " << algo
<< " " << key
<< endl
;
2921 cerr
<< "Failure storing new TSIG key " << name
<< " " << algo
<< " " << key
<< endl
;
2925 } else if (cmds
[0]=="import-tsig-key") {
2926 if (cmds
.size() < 4) {
2927 cerr
<< "Syntax: " << cmds
[0] << " name algorithm key" << endl
;
2930 DNSName
name(cmds
[1]);
2931 string algo
= cmds
[2];
2932 string key
= cmds
[3];
2934 UeberBackend
B("default");
2935 if (B
.setTSIGKey(name
, DNSName(algo
), key
)) {
2936 cout
<< "Imported TSIG key " << name
<< " " << algo
<< endl
;
2938 cerr
<< "Failure importing TSIG key " << name
<< " " << algo
<< endl
;
2942 } else if (cmds
[0]=="delete-tsig-key") {
2943 if (cmds
.size() < 2) {
2944 cerr
<< "Syntax: " << cmds
[0] << " name" << endl
;
2947 DNSName
name(cmds
[1]);
2949 UeberBackend
B("default");
2950 if (B
.deleteTSIGKey(name
)) {
2951 cout
<< "Deleted TSIG key " << name
<< endl
;
2953 cerr
<< "Failure deleting TSIG key " << name
<< endl
;
2957 } else if (cmds
[0]=="list-tsig-keys") {
2958 std::vector
<struct TSIGKey
> keys
;
2959 UeberBackend
B("default");
2960 if (B
.getTSIGKeys(keys
)) {
2961 for(const TSIGKey
&key
: keys
) {
2962 cout
<< key
.name
.toString() << " " << key
.algorithm
.toString() << " " << key
.key
<< endl
;
2966 } else if (cmds
[0]=="activate-tsig-key") {
2968 if (cmds
.size() < 4) {
2969 cerr
<< "Syntax: " << cmds
[0] << " ZONE NAME {master|slave}" << endl
;
2972 DNSName
zname(cmds
[1]);
2973 string name
= cmds
[2];
2974 if (cmds
[3] == "master")
2975 metaKey
= "TSIG-ALLOW-AXFR";
2976 else if (cmds
[3] == "slave")
2977 metaKey
= "AXFR-MASTER-TSIG";
2979 cerr
<< "Invalid parameter '" << cmds
[3] << "', expected master or slave" << endl
;
2982 UeberBackend
B("default");
2983 std::vector
<std::string
> meta
;
2984 if (!B
.getDomainMetadata(zname
, metaKey
, meta
)) {
2985 cerr
<< "Failure enabling TSIG key " << name
<< " for " << zname
<< endl
;
2989 for(std::string tmpname
: meta
) {
2990 if (tmpname
== name
) { found
= true; break; }
2992 if (!found
) meta
.push_back(name
);
2993 if (B
.setDomainMetadata(zname
, metaKey
, meta
)) {
2994 cout
<< "Enabled TSIG key " << name
<< " for " << zname
<< endl
;
2996 cerr
<< "Failure enabling TSIG key " << name
<< " for " << zname
<< endl
;
3000 } else if (cmds
[0]=="deactivate-tsig-key") {
3002 if (cmds
.size() < 4) {
3003 cerr
<< "Syntax: " << cmds
[0] << " ZONE NAME {master|slave}" << endl
;
3006 DNSName
zname(cmds
[1]);
3007 string name
= cmds
[2];
3008 if (cmds
[3] == "master")
3009 metaKey
= "TSIG-ALLOW-AXFR";
3010 else if (cmds
[3] == "slave")
3011 metaKey
= "AXFR-MASTER-TSIG";
3013 cerr
<< "Invalid parameter '" << cmds
[3] << "', expected master or slave" << endl
;
3017 UeberBackend
B("default");
3018 std::vector
<std::string
> meta
;
3019 if (!B
.getDomainMetadata(zname
, metaKey
, meta
)) {
3020 cerr
<< "Failure disabling TSIG key " << name
<< " for " << zname
<< endl
;
3023 std::vector
<std::string
>::iterator iter
= meta
.begin();
3024 for(;iter
!= meta
.end(); ++iter
) if (*iter
== name
) break;
3025 if (iter
!= meta
.end()) meta
.erase(iter
);
3026 if (B
.setDomainMetadata(zname
, metaKey
, meta
)) {
3027 cout
<< "Disabled TSIG key " << name
<< " for " << zname
<< endl
;
3029 cerr
<< "Failure disabling TSIG key " << name
<< " for " << zname
<< endl
;
3033 } else if (cmds
[0]=="get-meta") {
3034 UeberBackend
B("default");
3035 if (cmds
.size() < 2) {
3036 cerr
<< "Syntax: " << cmds
[0] << " zone [kind kind ..]" << endl
;
3039 DNSName
zone(cmds
[1]);
3040 vector
<string
> keys
;
3043 if (!B
.getDomainInfo(zone
, di
)) {
3044 cerr
<< "Invalid zone '" << zone
<< "'" << endl
;
3048 if (cmds
.size() > 2) {
3049 keys
.assign(cmds
.begin() + 2, cmds
.end());
3050 std::cout
<< "Metadata for '" << zone
<< "'" << endl
;
3051 for(const string kind
: keys
) {
3052 vector
<string
> meta
;
3054 if (B
.getDomainMetadata(zone
, kind
, meta
)) {
3055 cout
<< kind
<< " = " << boost::join(meta
, ", ") << endl
;
3059 std::map
<std::string
, std::vector
<std::string
> > meta
;
3060 std::cout
<< "Metadata for '" << zone
<< "'" << endl
;
3061 B
.getAllDomainMetadata(zone
, meta
);
3062 for(const auto& each_meta
: meta
) {
3063 cout
<< each_meta
.first
<< " = " << boost::join(each_meta
.second
, ", ") << endl
;
3068 } else if (cmds
[0]=="set-meta" || cmds
[0]=="add-meta") {
3069 if (cmds
.size() < 3) {
3070 cerr
<< "Syntax: " << cmds
[0] << " ZONE KIND [VALUE VALUE ..]" << endl
;
3073 DNSName
zone(cmds
[1]);
3074 string kind
= cmds
[2];
3075 static vector
<string
> multiMetaWhitelist
= {"ALLOW-AXFR-FROM", "ALLOW-DNSUPDATE-FROM",
3076 "ALSO-NOTIFY", "TSIG-ALLOW-AXFR", "TSIG-ALLOW-DNSUPDATE", "GSS-ALLOW-AXFR-PRINCIPAL",
3078 bool clobber
= true;
3079 if (cmds
[0] == "add-meta") {
3081 if (find(multiMetaWhitelist
.begin(), multiMetaWhitelist
.end(), kind
) == multiMetaWhitelist
.end() && kind
.find("X-") != 0) {
3082 cerr
<<"Refusing to add metadata to single-value metadata "<<kind
<<endl
;
3086 vector
<string
> meta(cmds
.begin() + 3, cmds
.end());
3087 return addOrSetMeta(zone
, kind
, meta
, clobber
);
3088 } else if (cmds
[0]=="hsm") {
3090 UeberBackend
B("default");
3091 if (cmds
.size() < 2) {
3092 cerr
<< "Missing sub-command for pdnsutil hsm"<< std::endl
;
3094 } else if (cmds
[1] == "assign") {
3095 DNSCryptoKeyEngine::storvector_t storvect
;
3097 std::vector
<DNSBackend::KeyData
> keys
;
3099 if (cmds
.size() < 9) {
3100 std::cout
<< "Usage: pdnsutil hsm assign ZONE ALGORITHM {ksk|zsk} MODULE TOKEN PIN LABEL (PUBLABEL)" << std::endl
;
3104 DNSName
zone(cmds
[2]);
3107 if (!B
.getDomainInfo(zone
, di
)) {
3108 cerr
<< "Unable to assign module to unknown zone '" << zone
<< "'" << std::endl
;
3112 int algorithm
= DNSSECKeeper::shorthand2algorithm(cmds
[3]);
3114 cerr
<< "Unable to use unknown algorithm '" << cmds
[3] << "'" << std::endl
;
3119 bool keyOrZone
= (cmds
[4] == "ksk" ? true : false);
3120 string module
= cmds
[5];
3121 string slot
= cmds
[6];
3122 string pin
= cmds
[7];
3123 string label
= cmds
[8];
3125 if (cmds
.size() > 9)
3126 pub_label
= cmds
[9];
3130 std::ostringstream iscString
;
3131 iscString
<< "Private-key-format: v1.2" << std::endl
<<
3132 "Algorithm: " << algorithm
<< std::endl
<<
3133 "Engine: " << module
<< std::endl
<<
3134 "Slot: " << slot
<< std::endl
<<
3135 "PIN: " << pin
<< std::endl
<<
3136 "Label: " << label
<< std::endl
<<
3137 "PubLabel: " << pub_label
<< std::endl
;
3139 DNSKEYRecordContent drc
;
3140 DNSSECPrivateKey dpk
;
3141 dpk
.d_flags
= (keyOrZone
? 257 : 256);
3143 shared_ptr
<DNSCryptoKeyEngine
> dke(DNSCryptoKeyEngine::makeFromISCString(drc
, iscString
.str()));
3144 if(!dke
->checkKey()) {
3145 cerr
<< "Invalid DNS Private Key in engine " << module
<< " slot " << slot
<< std::endl
;
3150 // make sure this key isn't being reused.
3151 B
.getDomainKeys(zone
, keys
);
3154 for(DNSBackend::KeyData
& kd
: keys
) {
3155 if (kd
.content
== iscString
.str()) {
3156 // it's this one, I guess...
3163 cerr
<< "You have already assigned this key with ID=" << id
<< std::endl
;
3167 if (!dk
.addKey(zone
, dpk
, id
)) {
3168 cerr
<< "Unable to assign module slot to zone" << std::endl
;
3172 cerr
<< "Module " << module
<< " slot " << slot
<< " assigned to " << zone
<< " with key id " << id
<< endl
;
3175 } else if (cmds
[1] == "create-key") {
3177 if (cmds
.size() < 4) {
3178 cerr
<< "Usage: pdnsutil hsm create-key ZONE KEY-ID [BITS]" << endl
;
3182 DNSName
zone(cmds
[2]);
3186 if (!B
.getDomainInfo(zone
, di
)) {
3187 cerr
<< "Unable to create key for unknown zone '" << zone
<< "'" << std::endl
;
3191 id
= pdns_stou(cmds
[3]);
3192 std::vector
<DNSBackend::KeyData
> keys
;
3193 if (!B
.getDomainKeys(zone
, keys
)) {
3194 cerr
<< "No keys found for zone " << zone
<< std::endl
;
3198 std::shared_ptr
<DNSCryptoKeyEngine
> dke
= nullptr;
3199 // lookup correct key
3200 for(DNSBackend::KeyData
&kd
: keys
) {
3203 DNSKEYRecordContent dkrc
;
3204 dke
= DNSCryptoKeyEngine::makeFromISCString(dkrc
, kd
.content
);
3209 cerr
<< "Could not find key with ID " << id
<< endl
;
3212 if (cmds
.size() > 4) {
3213 bits
= pdns_stou(cmds
[4]);
3216 cerr
<< "Invalid bit size " << bits
<< "given, must be positive integer";
3221 } catch (PDNSException
& e
) {
3222 cerr
<< e
.reason
<< endl
;
3226 cerr
<< "Key of size " << bits
<< " created" << std::endl
;
3230 cerr
<<"PKCS#11 support not enabled"<<endl
;
3233 } else if (cmds
[0] == "b2b-migrate") {
3234 if (cmds
.size() < 3) {
3235 cerr
<<"Usage: b2b-migrate OLD NEW"<<endl
;
3239 DNSBackend
*src
,*tgt
;
3242 for(DNSBackend
*b
: BackendMakers().all()) {
3243 if (b
->getPrefix() == cmds
[1]) src
= b
;
3244 if (b
->getPrefix() == cmds
[2]) tgt
= b
;
3247 cerr
<<"Unknown source backend '"<<cmds
[1]<<"'"<<endl
;
3251 cerr
<<"Unknown target backend '"<<cmds
[2]<<"'"<<endl
;
3255 cout
<<"Moving zone(s) from "<<src
->getPrefix()<<" to "<<tgt
->getPrefix()<<endl
;
3257 vector
<DomainInfo
> domains
;
3259 tgt
->getAllDomains(&domains
, true);
3260 if (domains
.size()>0)
3261 throw PDNSException("Target backend has domain(s), please clean it first");
3263 src
->getAllDomains(&domains
, true);
3265 for(const DomainInfo
& di
: domains
) {
3268 DNSResourceRecord rr
;
3269 cout
<<"Processing '"<<di
.zone
<<"'"<<endl
;
3271 if (!tgt
->createDomain(di
.zone
)) throw PDNSException("Failed to create zone");
3272 if (!tgt
->getDomainInfo(di
.zone
, di_new
)) throw PDNSException("Failed to create zone");
3273 tgt
->setKind(di_new
.zone
, di
.kind
);
3274 tgt
->setAccount(di_new
.zone
,di
.account
);
3277 for(const auto& master
: di
.masters
) {
3281 masters
+= master
.toStringWithPortExcept(53);
3283 tgt
->setMaster(di_new
.zone
, masters
);
3285 if (!src
->list(di
.zone
, di
.id
, true)) throw PDNSException("Failed to list records");
3288 tgt
->startTransaction(di
.zone
, di_new
.id
);
3290 while(src
->get(rr
)) {
3291 rr
.domain_id
= di_new
.id
;
3292 if (!tgt
->feedRecord(rr
, DNSName())) throw PDNSException("Failed to feed record");
3298 if (src
->listComments(di
.id
)) {
3300 while(src
->getComment(c
)) {
3301 c
.domain_id
= di_new
.id
;
3302 tgt
->feedComment(c
);
3308 std::map
<std::string
, std::vector
<std::string
> > meta
;
3309 if (src
->getAllDomainMetadata(di
.zone
, meta
)) {
3310 for (const auto& i
: meta
) {
3311 if (!tgt
->setDomainMetadata(di
.zone
, i
.first
, i
.second
)) throw PDNSException("Failed to feed domain metadata");
3317 // temp var for KeyID
3319 std::vector
<DNSBackend::KeyData
> keys
;
3320 if (src
->getDomainKeys(di
.zone
, keys
)) {
3321 for(const DNSBackend::KeyData
& k
: keys
) {
3322 tgt
->addDomainKey(di
.zone
, k
, keyID
);
3326 tgt
->commitTransaction();
3327 cout
<<"Moved "<<nr
<<" record(s), "<<nc
<<" comment(s), "<<nm
<<" metadata(s) and "<<nk
<<" cryptokey(s)"<<endl
;
3332 std::vector
<struct TSIGKey
> tkeys
;
3333 if (src
->getTSIGKeys(tkeys
)) {
3334 for(auto& tk
: tkeys
) {
3335 if (!tgt
->setTSIGKey(tk
.name
, tk
.algorithm
, tk
.key
)) throw PDNSException("Failed to feed TSIG key");
3339 cout
<<"Moved "<<ntk
<<" TSIG key(s)"<<endl
;
3341 cout
<<"Remember to drop the old backend and run rectify-all-zones"<<endl
;
3344 } else if (cmds
[0] == "backend-cmd") {
3345 if (cmds
.size() < 3) {
3346 cerr
<<"Usage: backend-cmd BACKEND CMD [CMD..]"<<endl
;
3353 for(DNSBackend
*b
: BackendMakers().all()) {
3354 if (b
->getPrefix() == cmds
[1]) db
= b
;
3358 cerr
<<"Unknown backend '"<<cmds
[1]<<"'"<<endl
;
3362 for(auto i
=next(begin(cmds
),2); i
!= end(cmds
); ++i
) {
3363 cerr
<<"== "<<*i
<<endl
;
3364 cout
<<db
->directBackendCmd(*i
);
3369 cerr
<<"Unknown command '"<<cmds
[0] <<"'"<< endl
;
3374 catch(PDNSException
& ae
) {
3375 cerr
<<"Error: "<<ae
.reason
<<endl
;
3378 catch(std::exception
& e
) {
3379 cerr
<<"Error: "<<e
.what()<<endl
;
3384 cerr
<<"Caught an unknown exception"<<endl
;