]>
Commit | Line | Data |
---|---|---|
12c86877 BH |
1 | /* |
2 | PowerDNS Versatile Database Driven Nameserver | |
379ab445 | 3 | Copyright (C) 2002 - 2008 PowerDNS.COM BV |
12c86877 BH |
4 | |
5 | This program is free software; you can redistribute it and/or modify | |
ea44d9d3 BH |
6 | it under the terms of the GNU General Public License version 2 as |
7 | published by the Free Software Foundation. | |
12c86877 BH |
8 | |
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
06bd9ccf | 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
12c86877 | 17 | */ |
379ab445 | 18 | #include "packetcache.hh" |
bd11bd1d | 19 | #include "utility.hh" |
12c86877 BH |
20 | #include "dynhandler.hh" |
21 | #include "statbag.hh" | |
22 | #include "logger.hh" | |
23 | #include "dns.hh" | |
24 | #include "arguments.hh" | |
25 | #include <signal.h> | |
bd11bd1d | 26 | #include "misc.hh" |
12c86877 BH |
27 | #include "communicator.hh" |
28 | ||
29 | static bool s_pleasequit; | |
30 | ||
31 | bool DLQuitPlease() | |
32 | { | |
33 | return s_pleasequit; | |
34 | } | |
35 | ||
36 | string DLQuitHandler(const vector<string>&parts, Utility::pid_t ppid) | |
37 | { | |
38 | string ret="No return value"; | |
39 | if(parts[0]=="QUIT") { | |
40 | s_pleasequit=true; | |
41 | ret="Scheduling exit"; | |
42 | L<<Logger::Error<<"Scheduling exit on remote request"<<endl; | |
43 | } | |
44 | return ret; | |
45 | ||
46 | } | |
47 | ||
48 | static void dokill(int) | |
49 | { | |
50 | exit(1); | |
51 | } | |
52 | ||
53 | string DLRQuitHandler(const vector<string>&parts, Utility::pid_t ppid) | |
54 | { | |
55 | #ifndef WIN32 | |
56 | signal(SIGALRM, dokill); | |
57 | ||
58 | alarm(1); | |
59 | ||
60 | #else | |
61 | ||
62 | if ( !PDNSService::instance()->isRunningAsService()) | |
63 | GenerateConsoleCtrlEvent( CTRL_C_EVENT, 0 ); | |
64 | else | |
65 | PDNSService::instance()->stop(); | |
66 | ||
67 | #endif // WIN32 | |
68 | ||
69 | return "Exiting"; | |
70 | } | |
71 | ||
72 | string DLPingHandler(const vector<string>&parts, Utility::pid_t ppid) | |
73 | { | |
74 | return "PONG"; | |
75 | } | |
76 | ||
77 | string DLShowHandler(const vector<string>&parts, Utility::pid_t ppid) | |
78 | { | |
79 | extern StatBag S; | |
80 | string ret("Wrong number of parameters"); | |
81 | if(parts.size()==2) { | |
82 | if(parts[1]=="*") | |
83 | ret=S.directory(); | |
84 | else | |
85 | ret=S.getValueStr(parts[1]); | |
86 | } | |
87 | ||
88 | return ret; | |
89 | } | |
90 | ||
91 | static string d_status; | |
92 | ||
93 | void setStatus(const string &str) | |
94 | { | |
95 | d_status=str; | |
96 | } | |
97 | ||
98 | string DLStatusHandler(const vector<string>&parts, Utility::pid_t ppid) | |
99 | { | |
100 | ostringstream os; | |
101 | os<<ppid<<": "<<d_status; | |
102 | return os.str(); | |
103 | } | |
104 | ||
105 | string DLUptimeHandler(const vector<string>&parts, Utility::pid_t ppid) | |
106 | { | |
107 | ostringstream os; | |
108 | os<<humanDuration(time(0)-s_starttime); | |
109 | return os.str(); | |
110 | } | |
111 | ||
112 | string DLPurgeHandler(const vector<string>&parts, Utility::pid_t ppid) | |
113 | { | |
114 | extern PacketCache PC; | |
115 | ostringstream os; | |
27fdc3fc | 116 | int ret=0; |
12c86877 | 117 | |
27fdc3fc BH |
118 | if(parts.size()>1) { |
119 | for (vector<string>::const_iterator i=++parts.begin();i<parts.end();++i) { | |
120 | ret+=PC.purge(*i); | |
121 | } | |
122 | } | |
12c86877 BH |
123 | else |
124 | ret=PC.purge(); | |
125 | os<<ret; | |
126 | return os.str(); | |
127 | } | |
128 | ||
129 | string DLCCHandler(const vector<string>&parts, Utility::pid_t ppid) | |
130 | { | |
131 | extern PacketCache PC; | |
132 | map<char,int> counts=PC.getCounts(); | |
133 | ostringstream os; | |
134 | bool first=true; | |
135 | for(map<char,int>::const_iterator i=counts.begin();i!=counts.end();++i) { | |
136 | if(!first) | |
137 | os<<", "; | |
138 | first=false; | |
139 | ||
140 | if(i->first=='!') | |
141 | os<<"negative queries: "; | |
142 | else if(i->first=='Q') | |
143 | os<<"queries: "; | |
144 | else if(i->first=='n') | |
145 | os<<"non-recursive packets: "; | |
146 | else if(i->first=='r') | |
147 | os<<"recursive packets: "; | |
148 | else | |
149 | os<<"unknown: "; | |
150 | ||
151 | os<<i->second; | |
152 | } | |
153 | ||
154 | return os.str(); | |
155 | } | |
156 | ||
157 | ||
158 | string DLSettingsHandler(const vector<string>&parts, Utility::pid_t ppid) | |
159 | { | |
160 | static const char *whitelist[]={"query-logging",0}; | |
161 | const char **p; | |
162 | ||
163 | if(parts.size()!=3) { | |
164 | return "Syntax: set variable value"; | |
165 | } | |
166 | ||
167 | for(p=whitelist;*p;p++) | |
168 | if(*p==parts[1]) | |
169 | break; | |
3120733f | 170 | if(*p) { |
379ab445 | 171 | ::arg().set(parts[1])=parts[2]; |
12c86877 BH |
172 | return "done"; |
173 | } | |
174 | else | |
3120733f | 175 | return "This setting cannot be changed at runtime, or no such setting"; |
12c86877 BH |
176 | |
177 | } | |
178 | ||
12c86877 BH |
179 | string DLVersionHandler(const vector<string>&parts, Utility::pid_t ppid) |
180 | { | |
12c86877 BH |
181 | return VERSION; |
182 | } | |
183 | ||
ef1d2f44 BH |
184 | string DLNotifyRetrieveHandler(const vector<string>&parts, Utility::pid_t ppid) |
185 | { | |
186 | extern CommunicatorClass Communicator; | |
187 | ostringstream os; | |
188 | if(parts.size()!=2) | |
189 | return "syntax: retrieve domain"; | |
190 | ||
191 | const string& domain=parts[1]; | |
192 | DomainInfo di; | |
193 | PacketHandler P; | |
194 | if(!P.getBackend()->getDomainInfo(domain, di)) | |
195 | return "Domain '"+domain+"' unknown"; | |
196 | ||
e5b11b2f | 197 | if(di.masters.empty()) |
f2c11a48 BH |
198 | return "Domain '"+domain+"' is not a slave domain (or has no master defined)"; |
199 | ||
e5b11b2f BH |
200 | random_shuffle(di.masters.begin(), di.masters.end()); |
201 | Communicator.addSuckRequest(domain, di.masters.front()); | |
202 | return "Added retrieval request for '"+domain+"' from master "+di.masters.front(); | |
ef1d2f44 | 203 | } |
12c86877 BH |
204 | |
205 | string DLNotifyHostHandler(const vector<string>&parts, Utility::pid_t ppid) | |
206 | { | |
207 | extern CommunicatorClass Communicator; | |
208 | ostringstream os; | |
209 | if(parts.size()!=3) | |
ea44d9d3 | 210 | return "syntax: notify-host domain ip"; |
2685008f PD |
211 | if(!::arg().mustDo("master")) |
212 | return "PowerDNS not configured as master"; | |
5762f14e BH |
213 | |
214 | struct in_addr inp; | |
215 | if(!Utility::inet_aton(parts[2].c_str(),&inp)) | |
216 | return "Unable to convert '"+parts[2]+"' to an IP address"; | |
217 | ||
12c86877 BH |
218 | L<<Logger::Warning<<"Notification request to host "<<parts[2]<<" for domain '"<<parts[1]<<"' received"<<endl; |
219 | Communicator.notify(parts[1],parts[2]); | |
220 | return "Added to queue"; | |
221 | } | |
222 | ||
223 | string DLNotifyHandler(const vector<string>&parts, Utility::pid_t ppid) | |
224 | { | |
225 | extern CommunicatorClass Communicator; | |
226 | ostringstream os; | |
227 | if(parts.size()!=2) | |
228 | return "syntax: notify domain"; | |
2685008f PD |
229 | if(!::arg().mustDo("master")) |
230 | return "PowerDNS not configured as master"; | |
11a45617 | 231 | L<<Logger::Warning<<"Notification request for domain '"<<parts[1]<<"' received from operator"<<endl; |
12c86877 BH |
232 | if(!Communicator.notifyDomain(parts[1])) |
233 | return "Failed to add to the queue - see log"; | |
234 | return "Added to queue"; | |
235 | } | |
236 | ||
237 | string DLRediscoverHandler(const vector<string>&parts, Utility::pid_t ppid) | |
238 | { | |
239 | PacketHandler P; | |
bd11bd1d BH |
240 | try { |
241 | L<<Logger::Error<<"Rediscovery was requested"<<endl; | |
973ad2b5 BH |
242 | string status="Ok"; |
243 | P.getBackend()->rediscover(&status); | |
244 | return status; | |
bd11bd1d BH |
245 | } |
246 | catch(AhuException &ae) { | |
247 | return ae.reason; | |
248 | } | |
249 | ||
12c86877 BH |
250 | } |
251 | ||
252 | string DLReloadHandler(const vector<string>&parts, Utility::pid_t ppid) | |
253 | { | |
254 | PacketHandler P; | |
255 | P.getBackend()->reload(); | |
256 | L<<Logger::Error<<"Reload was requested"<<endl; | |
257 | return "Ok"; | |
258 | } |