2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "auth-caches.hh"
26 #include "auth-querycache.hh"
27 #include "auth-packetcache.hh"
29 #include "dynhandler.hh"
33 #include "arguments.hh"
36 #include "communicator.hh"
37 #include "dnsseckeeper.hh"
38 #include "nameserver.hh"
39 #include "responsestats.hh"
40 #include "ueberbackend.hh"
41 #include "common_startup.hh"
43 extern ResponseStats g_rs
;
45 static bool s_pleasequit
;
46 static string d_status
;
53 string
DLQuitHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
55 string ret
="No return value";
56 if(parts
[0]=="QUIT") {
58 ret
="Scheduling exit";
59 g_log
<<Logger::Error
<<"Scheduling exit on remote request"<<endl
;
64 static void dokill(int)
69 string
DLCurrentConfigHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
71 return ::arg().configstring(true);
74 string
DLRQuitHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
76 signal(SIGALRM
, dokill
);
81 string
DLPingHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
86 string
DLShowHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
90 string
ret("Wrong number of parameters");
95 ret
=S
.getValueStr(parts
[1]);
105 void setStatus(const string
&str
)
110 string
DLStatusHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
113 os
<<ppid
<<": "<<d_status
;
117 string
DLUptimeHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
120 os
<<humanDuration(time(0)-s_starttime
);
124 string
DLPurgeHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
131 for (vector
<string
>::const_iterator i
=++parts
.begin();i
<parts
.end();++i
) {
132 ret
+=purgeAuthCaches(*i
);
133 if(!boost::ends_with(*i
, "$"))
134 dk
.clearCaches(DNSName(*i
));
136 dk
.clearAllCaches(); // at least we do what we promise.. and a bit more!
140 ret
= purgeAuthCaches();
148 string
DLCCHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
150 extern AuthPacketCache PC
;
151 extern AuthQueryCache QC
;
152 map
<char,uint64_t> counts
=QC
.getCounts();
153 uint64_t packetEntries
= PC
.size();
156 for(map
<char,uint64_t>::const_iterator i
=counts
.begin();i
!=counts
.end();++i
) {
162 os
<<"negative queries: ";
163 else if(i
->first
=='Q')
170 os
<<"packets: "<<packetEntries
;
175 string
DLQTypesHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
177 return g_rs
.getQTypeReport();
180 string
DLRSizesHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
182 typedef map
<uint16_t, uint64_t> respsizes_t
;
183 respsizes_t respsizes
= g_rs
.getSizeResponseCounts();
185 boost::format
fmt("%d\t%d\n");
186 for(const respsizes_t::value_type
& val
: respsizes
) {
187 os
<< (fmt
% val
.first
% val
.second
).str();
192 string
DLRemotesHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
195 typedef vector
<pair
<string
, unsigned int> > totals_t
;
196 totals_t totals
= S
.getRing("remotes");
198 boost::format
fmt("%s\t%d\n");
199 for(totals_t::value_type
& val
: totals
) {
200 ret
+= (fmt
% val
.first
% val
.second
).str();
205 string
DLSettingsHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
207 static const char *whitelist
[]={"query-logging",0};
210 if(parts
.size()!=3) {
211 return "Syntax: set variable value";
214 for(p
=whitelist
;*p
;p
++)
218 ::arg().set(parts
[1])=parts
[2];
222 return "This setting cannot be changed at runtime, or no such setting";
226 string
DLVersionHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
231 string
DLNotifyRetrieveHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
233 extern CommunicatorClass Communicator
;
236 return "syntax: retrieve domain";
240 domain
= DNSName(parts
[1]);
242 return "Failed to parse domain as valid DNS name";
247 if(!B
.getDomainInfo(domain
, di
))
248 return "Domain '"+domain
.toString()+"' unknown";
250 if(di
.kind
!= DomainInfo::Slave
|| di
.masters
.empty())
251 return "Domain '"+domain
.toString()+"' is not a slave domain (or has no master defined)";
253 random_shuffle(di
.masters
.begin(), di
.masters
.end());
254 Communicator
.addSuckRequest(domain
, di
.masters
.front());
255 return "Added retrieval request for '"+domain
.toString()+"' from master "+di
.masters
.front().toLogString();
258 string
DLNotifyHostHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
260 extern CommunicatorClass Communicator
;
263 return "syntax: notify-host domain ip";
264 if(!::arg().mustDo("master") && !::arg().mustDo("slave-renotify"))
265 return "PowerDNS not configured as master or slave with re-notifications";
269 domain
= DNSName(parts
[1]);
271 return "Failed to parse domain as valid DNS name";
275 ComboAddress
ca(parts
[2]);
278 return "Unable to convert '"+parts
[2]+"' to an IP address";
281 g_log
<<Logger::Warning
<<"Notification request to host "<<parts
[2]<<" for domain '"<<domain
<<"' received from operator"<<endl
;
282 Communicator
.notify(domain
, parts
[2]);
283 return "Added to queue";
286 string
DLNotifyHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
288 extern CommunicatorClass Communicator
;
291 return "syntax: notify domain";
292 if(!::arg().mustDo("master") && !::arg().mustDo("slave-renotify"))
293 return "PowerDNS not configured as master or slave with re-notifications";
294 g_log
<<Logger::Warning
<<"Notification request for domain '"<<parts
[1]<<"' received from operator"<<endl
;
296 if (parts
[1] == "*") {
297 vector
<DomainInfo
> domains
;
298 B
.getAllDomains(&domains
);
302 for (vector
<DomainInfo
>::const_iterator di
=domains
.begin(); di
!= domains
.end(); di
++) {
303 if (di
->kind
== 0) { // MASTER
305 if(Communicator
.notifyDomain(di
->zone
))
310 if (total
!= notified
)
311 return itoa(notified
)+" out of "+itoa(total
)+" zones added to queue - see log";
312 return "Added "+itoa(total
)+" MASTER zones to queue";
316 domain
= DNSName(parts
[1]);
318 return "Failed to parse domain as valid DNS name";
320 if(!Communicator
.notifyDomain(DNSName(parts
[1])))
321 return "Failed to add to the queue - see log";
322 return "Added to queue";
326 string
DLRediscoverHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
330 g_log
<<Logger::Error
<<"Rediscovery was requested"<<endl
;
332 B
.rediscover(&status
);
335 catch(PDNSException
&ae
) {
341 string
DLReloadHandler(const vector
<string
>&parts
, Utility::pid_t ppid
)
345 g_log
<<Logger::Error
<<"Reload was requested"<<endl
;
350 string
DLListZones(const vector
<string
>&parts
, Utility::pid_t ppid
)
353 g_log
<<Logger::Notice
<<"Received request to list zones."<<endl
;
354 vector
<DomainInfo
> domains
;
355 B
.getAllDomains(&domains
);
358 if (parts
.size() > 1) {
359 if (toUpper(parts
[1]) == "MASTER")
361 else if (toUpper(parts
[1]) == "SLAVE")
363 else if (toUpper(parts
[1]) == "NATIVE")
369 for (vector
<DomainInfo
>::const_iterator di
=domains
.begin(); di
!= domains
.end(); di
++) {
370 if (di
->kind
== kindFilter
|| kindFilter
== -1) {
371 ret
<<di
->zone
.toString()<<endl
;
375 if (kindFilter
!= -1)
376 ret
<<parts
[1]<<" zonecount:"<<count
;
378 ret
<<"All zonecount:"<<count
;
384 extern bool PKCS11ModuleSlotLogin(const std::string
& module
, const string
& tokenId
, const std::string
& pin
);
387 string
DLTokenLogin(const vector
<string
>&parts
, Utility::pid_t ppid
)
390 return "PKCS#11 support not compiled in";
392 if (parts
.size() != 4) {
393 return "invalid number of parameters, needs 4, got " + std::to_string(parts
.size());
396 if (PKCS11ModuleSlotLogin(parts
[1], parts
[2], parts
[3])) {
399 return "could not log in, check logs";