]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dynhandler.cc
auth: switch circleci mssql image
[thirdparty/pdns.git] / pdns / dynhandler.cc
CommitLineData
12c86877 1/*
12471842
PL
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
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.
8 *
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.
12 *
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.
17 *
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.
21 */
870a0fe4
AT
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
bf269e28
RG
25#include "auth-caches.hh"
26#include "auth-querycache.hh"
27#include "auth-packetcache.hh"
bd11bd1d 28#include "utility.hh"
12c86877
BH
29#include "dynhandler.hh"
30#include "statbag.hh"
31#include "logger.hh"
32#include "dns.hh"
33#include "arguments.hh"
34#include <signal.h>
bd11bd1d 35#include "misc.hh"
12c86877 36#include "communicator.hh"
627d2ca2 37#include "dnsseckeeper.hh"
ba7244a5 38#include "nameserver.hh"
09425ce1 39#include "responsestats.hh"
767da1a0 40#include "ueberbackend.hh"
3e8216c8 41#include "common_startup.hh"
09425ce1
F
42
43extern ResponseStats g_rs;
12c86877
BH
44
45static bool s_pleasequit;
64bcb6be 46static string d_status;
12c86877
BH
47
48bool DLQuitPlease()
49{
50 return s_pleasequit;
51}
52
53string DLQuitHandler(const vector<string>&parts, Utility::pid_t ppid)
54{
55 string ret="No return value";
56 if(parts[0]=="QUIT") {
57 s_pleasequit=true;
58 ret="Scheduling exit";
e6a9dde5 59 g_log<<Logger::Error<<"Scheduling exit on remote request"<<endl;
12c86877
BH
60 }
61 return ret;
12c86877
BH
62}
63
64static void dokill(int)
65{
e7f0140b 66 exit(0);
12c86877
BH
67}
68
64bcb6be
RA
69string DLCurrentConfigHandler(const vector<string>&parts, Utility::pid_t ppid)
70{
71 return ::arg().configstring(true);
72}
73
12c86877
BH
74string DLRQuitHandler(const vector<string>&parts, Utility::pid_t ppid)
75{
12c86877 76 signal(SIGALRM, dokill);
12c86877 77 alarm(1);
12c86877
BH
78 return "Exiting";
79}
80
81string DLPingHandler(const vector<string>&parts, Utility::pid_t ppid)
82{
83 return "PONG";
84}
85
86string DLShowHandler(const vector<string>&parts, Utility::pid_t ppid)
02feff9f 87try
12c86877
BH
88{
89 extern StatBag S;
90 string ret("Wrong number of parameters");
91 if(parts.size()==2) {
92 if(parts[1]=="*")
93 ret=S.directory();
94 else
95 ret=S.getValueStr(parts[1]);
96 }
97
98 return ret;
99}
02feff9f 100catch(...)
101{
102 return "Unknown";
103}
12c86877
BH
104
105void setStatus(const string &str)
106{
107 d_status=str;
108}
109
110string DLStatusHandler(const vector<string>&parts, Utility::pid_t ppid)
111{
112 ostringstream os;
113 os<<ppid<<": "<<d_status;
114 return os.str();
115}
116
117string DLUptimeHandler(const vector<string>&parts, Utility::pid_t ppid)
118{
119 ostringstream os;
120 os<<humanDuration(time(0)-s_starttime);
121 return os.str();
122}
123
124string DLPurgeHandler(const vector<string>&parts, Utility::pid_t ppid)
125{
627d2ca2 126 DNSSECKeeper dk;
12c86877 127 ostringstream os;
27fdc3fc 128 int ret=0;
12c86877 129
27fdc3fc
BH
130 if(parts.size()>1) {
131 for (vector<string>::const_iterator i=++parts.begin();i<parts.end();++i) {
bf269e28 132 ret+=purgeAuthCaches(*i);
478748c3 133 if(!boost::ends_with(*i, "$"))
134 dk.clearCaches(DNSName(*i));
135 else
136 dk.clearAllCaches(); // at least we do what we promise.. and a bit more!
27fdc3fc
BH
137 }
138 }
627d2ca2 139 else {
bf269e28 140 ret = purgeAuthCaches();
627d2ca2
PD
141 dk.clearAllCaches();
142 }
143
12c86877
BH
144 os<<ret;
145 return os.str();
146}
147
148string DLCCHandler(const vector<string>&parts, Utility::pid_t ppid)
149{
bf269e28
RG
150 extern AuthPacketCache PC;
151 extern AuthQueryCache QC;
152 map<char,uint64_t> counts=QC.getCounts();
153 uint64_t packetEntries = PC.size();
12c86877
BH
154 ostringstream os;
155 bool first=true;
bf269e28 156 for(map<char,uint64_t>::const_iterator i=counts.begin();i!=counts.end();++i) {
12c86877
BH
157 if(!first)
158 os<<", ";
159 first=false;
160
161 if(i->first=='!')
162 os<<"negative queries: ";
163 else if(i->first=='Q')
164 os<<"queries: ";
12c86877
BH
165 else
166 os<<"unknown: ";
167
168 os<<i->second;
169 }
bf269e28 170 os<<"packets: "<<packetEntries;
12c86877
BH
171
172 return os.str();
173}
174
ba7244a5
PD
175string DLQTypesHandler(const vector<string>&parts, Utility::pid_t ppid)
176{
09425ce1 177 return g_rs.getQTypeReport();
ba7244a5 178}
12c86877 179
93698ef3
PD
180string DLRSizesHandler(const vector<string>&parts, Utility::pid_t ppid)
181{
182 typedef map<uint16_t, uint64_t> respsizes_t;
183 respsizes_t respsizes = g_rs.getSizeResponseCounts();
184 ostringstream os;
185 boost::format fmt("%d\t%d\n");
ef7cd021 186 for(const respsizes_t::value_type& val : respsizes) {
93698ef3
PD
187 os << (fmt % val.first % val.second).str();
188 }
189 return os.str();
190}
191
192string DLRemotesHandler(const vector<string>&parts, Utility::pid_t ppid)
193{
194 extern StatBag S;
195 typedef vector<pair<string, unsigned int> > totals_t;
196 totals_t totals = S.getRing("remotes");
197 string ret;
198 boost::format fmt("%s\t%d\n");
ef7cd021 199 for(totals_t::value_type& val : totals) {
93698ef3
PD
200 ret += (fmt % val.first % val.second).str();
201 }
202 return ret;
203}
204
12c86877
BH
205string DLSettingsHandler(const vector<string>&parts, Utility::pid_t ppid)
206{
207 static const char *whitelist[]={"query-logging",0};
208 const char **p;
209
210 if(parts.size()!=3) {
211 return "Syntax: set variable value";
212 }
213
214 for(p=whitelist;*p;p++)
215 if(*p==parts[1])
216 break;
3120733f 217 if(*p) {
379ab445 218 ::arg().set(parts[1])=parts[2];
12c86877
BH
219 return "done";
220 }
221 else
3120733f 222 return "This setting cannot be changed at runtime, or no such setting";
12c86877
BH
223
224}
225
12c86877
BH
226string DLVersionHandler(const vector<string>&parts, Utility::pid_t ppid)
227{
12c86877
BH
228 return VERSION;
229}
230
ef1d2f44
BH
231string DLNotifyRetrieveHandler(const vector<string>&parts, Utility::pid_t ppid)
232{
233 extern CommunicatorClass Communicator;
234 ostringstream os;
235 if(parts.size()!=2)
236 return "syntax: retrieve domain";
237
63faa7c9
CH
238 DNSName domain;
239 try {
240 domain = DNSName(parts[1]);
241 } catch (...) {
242 return "Failed to parse domain as valid DNS name";
243 }
244
ef1d2f44 245 DomainInfo di;
295c4a00 246 UeberBackend B;
63faa7c9
CH
247 if(!B.getDomainInfo(domain, di))
248 return "Domain '"+domain.toString()+"' unknown";
ef1d2f44 249
889500d7 250 if(di.kind != DomainInfo::Slave || di.masters.empty())
63faa7c9 251 return "Domain '"+domain.toString()+"' is not a slave domain (or has no master defined)";
f2c11a48 252
e5b11b2f 253 random_shuffle(di.masters.begin(), di.masters.end());
d3ee36f2 254 Communicator.addSuckRequest(domain, di.masters.front());
9b0f144f 255 return "Added retrieval request for '"+domain.toString()+"' from master "+di.masters.front().toLogString();
ef1d2f44 256}
12c86877
BH
257
258string DLNotifyHostHandler(const vector<string>&parts, Utility::pid_t ppid)
259{
260 extern CommunicatorClass Communicator;
261 ostringstream os;
262 if(parts.size()!=3)
ea44d9d3 263 return "syntax: notify-host domain ip";
0c541891 264 if(!::arg().mustDo("master") && !(::arg().mustDo("slave") && ::arg().mustDo("slave-renotify")))
aef133df 265 return "PowerDNS not configured as master or slave with re-notifications";
5762f14e 266
c1a295ca
CH
267 DNSName domain;
268 try {
269 domain = DNSName(parts[1]);
270 } catch (...) {
271 return "Failed to parse domain as valid DNS name";
272 }
273
c069c1f2
BH
274 try {
275 ComboAddress ca(parts[2]);
276 } catch(...)
277 {
5762f14e 278 return "Unable to convert '"+parts[2]+"' to an IP address";
c069c1f2
BH
279 }
280
e6a9dde5 281 g_log<<Logger::Warning<<"Notification request to host "<<parts[2]<<" for domain '"<<domain<<"' received from operator"<<endl;
c1a295ca 282 Communicator.notify(domain, parts[2]);
12c86877
BH
283 return "Added to queue";
284}
285
286string DLNotifyHandler(const vector<string>&parts, Utility::pid_t ppid)
287{
288 extern CommunicatorClass Communicator;
f1b4b713 289 UeberBackend B;
12c86877
BH
290 if(parts.size()!=2)
291 return "syntax: notify domain";
0c541891 292 if(!::arg().mustDo("master") && !(::arg().mustDo("slave") && ::arg().mustDo("slave-renotify")))
aef133df 293 return "PowerDNS not configured as master or slave with re-notifications";
e6a9dde5 294 g_log<<Logger::Warning<<"Notification request for domain '"<<parts[1]<<"' received from operator"<<endl;
f1b4b713
KM
295
296 if (parts[1] == "*") {
297 vector<DomainInfo> domains;
298 B.getAllDomains(&domains);
299
300 int total = 0;
301 int notified = 0;
be207d3d 302 for (const auto& di : domains) {
472dcfac 303 if (di.kind == DomainInfo::Master || di.kind == DomainInfo::Slave) { // MASTER and Slave if slave-renotify is enabled
f1b4b713 304 total++;
cb167afd 305 if(Communicator.notifyDomain(di.zone))
f1b4b713
KM
306 notified++;
307 }
308 }
309
310 if (total != notified)
311 return itoa(notified)+" out of "+itoa(total)+" zones added to queue - see log";
ec7849d4 312 return "Added "+itoa(total)+" MASTER/SLAVE zones to queue";
f1b4b713 313 } else {
100cf78b
CH
314 DNSName domain;
315 try {
316 domain = DNSName(parts[1]);
317 } catch (...) {
318 return "Failed to parse domain as valid DNS name";
319 }
290a083d 320 if(!Communicator.notifyDomain(DNSName(parts[1])))
f1b4b713
KM
321 return "Failed to add to the queue - see log";
322 return "Added to queue";
323 }
12c86877
BH
324}
325
326string DLRediscoverHandler(const vector<string>&parts, Utility::pid_t ppid)
327{
295c4a00 328 UeberBackend B;
6242d8a4 329 try {
e6a9dde5 330 g_log<<Logger::Error<<"Rediscovery was requested"<<endl;
6242d8a4 331 string status="Ok";
295c4a00 332 B.rediscover(&status);
6242d8a4
KM
333 return status;
334 }
335 catch(PDNSException &ae) {
336 return ae.reason;
337 }
338
12c86877
BH
339}
340
341string DLReloadHandler(const vector<string>&parts, Utility::pid_t ppid)
342{
295c4a00
CH
343 UeberBackend B;
344 B.reload();
e6a9dde5 345 g_log<<Logger::Error<<"Reload was requested"<<endl;
12c86877
BH
346 return "Ok";
347}
64bcb6be 348
f45a622c 349
ca8e1742 350string DLListZones(const vector<string>&parts, Utility::pid_t ppid)
767da1a0
RA
351{
352 UeberBackend B;
e6a9dde5 353 g_log<<Logger::Notice<<"Received request to list zones."<<endl;
767da1a0
RA
354 vector<DomainInfo> domains;
355 B.getAllDomains(&domains);
356 ostringstream ret;
357 int kindFilter = -1;
358 if (parts.size() > 1) {
359 if (toUpper(parts[1]) == "MASTER")
360 kindFilter = 0;
361 else if (toUpper(parts[1]) == "SLAVE")
362 kindFilter = 1;
363 else if (toUpper(parts[1]) == "NATIVE")
364 kindFilter = 2;
365 }
366
367 int count = 0;
368
cb167afd
CHB
369 for (const auto& di: domains) {
370 if (di.kind == kindFilter || kindFilter == -1) {
371 ret<<di.zone.toString()<<endl;
767da1a0
RA
372 count++;
373 }
374 }
375 if (kindFilter != -1)
36811022 376 ret<<parts[1]<<" zonecount:"<<count;
767da1a0 377 else
36811022 378 ret<<"All zonecount:"<<count;
767da1a0
RA
379
380 return ret.str();
381}
3e8216c8 382
24e0b305 383#ifdef HAVE_P11KIT1
248d701f 384extern bool PKCS11ModuleSlotLogin(const std::string& module, const string& tokenId, const std::string& pin);
24e0b305
AT
385#endif
386
387string DLTokenLogin(const vector<string>&parts, Utility::pid_t ppid)
388{
389#ifndef HAVE_P11KIT1
390 return "PKCS#11 support not compiled in";
391#else
392 if (parts.size() != 4) {
335da0ba 393 return "invalid number of parameters, needs 4, got " + std::to_string(parts.size());
24e0b305
AT
394 }
395
248d701f 396 if (PKCS11ModuleSlotLogin(parts[1], parts[2], parts[3])) {
24e0b305
AT
397 return "logged in";
398 } else {
399 return "could not log in, check logs";
400 }
401#endif
402}