]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/rec_control.cc
Merge pull request #3802 from Habbie/robust-doresolve
[thirdparty/pdns.git] / pdns / rec_control.cc
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2006 - 2015 PowerDNS.COM BV
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation
8
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "rec_channel.hh"
26 #include <iostream>
27 #include "pdnsexception.hh"
28 #include "arguments.hh"
29
30 #include "namespaces.hh"
31
32 #ifndef RECURSOR
33 #include "statbag.hh"
34 StatBag S;
35 #endif
36
37 ArgvMap &arg()
38 {
39 static ArgvMap arg;
40 return arg;
41 }
42
43 static void initArguments(int argc, char** argv)
44 {
45 arg().set("config-dir","Location of configuration directory (recursor.conf)")=SYSCONFDIR;
46
47 arg().set("socket-dir",string("Where the controlsocket will live, ")+LOCALSTATEDIR+" when unset and not chrooted" )="";
48 arg().set("chroot","switch to chroot jail")="";
49 arg().set("process","When controlling multiple recursors, the target process number")="";
50 arg().set("timeout", "Number of seconds to wait for the recursor to respond")="5";
51 arg().set("config-name","Name of this virtual configuration - will rename the binary image")="";
52 arg().setCmd("help","Provide this helpful message");
53 arg().setCmd("version","Show the version of this program");
54
55 arg().laxParse(argc,argv);
56 if(arg().mustDo("help") || arg().getCommands().empty()) {
57 cout<<"syntax: rec_control [options] command, options as below: "<<endl<<endl;
58 cout<<arg().helpstring(arg()["help"])<<endl;
59 cout<<"In addition, 'rec_control help' can be used to retrieve a list\nof available commands from PowerDNS"<<endl;
60 exit(arg().mustDo("help") ? 0 : 99);
61 }
62
63 if(arg().mustDo("version")) {
64 cout<<"rec_control version "<<VERSION<<endl;
65 exit(0);
66 }
67
68 string configname=::arg()["config-dir"]+"/recursor.conf";
69 if (::arg()["config-name"] != "")
70 configname=::arg()["config-dir"]+"/recursor-"+::arg()["config-name"]+".conf";
71
72 cleanSlashes(configname);
73
74 if(!::arg().preParseFile(configname.c_str(), "socket-dir", ""))
75 cerr<<"Warning: unable to parse configuration file '"<<configname<<"'"<<endl;
76 if(!::arg().preParseFile(configname.c_str(), "chroot", ""))
77 cerr<<"Warning: unable to parse configuration file '"<<configname<<"'"<<endl;
78
79 arg().laxParse(argc,argv); // make sure the commandline wins
80
81 if (::arg()["socket-dir"].empty()) {
82 if (::arg()["chroot"].empty())
83 ::arg().set("socket-dir") = LOCALSTATEDIR;
84 else
85 ::arg().set("socket-dir") = ::arg()["chroot"] + "/";
86 } else if (!::arg()["chroot"].empty()) {
87 ::arg().set("socket-dir") = ::arg()["chroot"] + "/" + ::arg()["socket-dir"];
88 }
89 }
90
91 int main(int argc, char** argv)
92 try
93 {
94 initArguments(argc, argv);
95 RecursorControlChannel rccS;
96 string sockname="pdns_recursor";
97
98 if (arg()["config-name"] != "")
99 sockname+="-"+arg()["config-name"];
100
101 if(!arg()["process"].empty())
102 sockname+="."+arg()["process"];
103
104 sockname.append(".controlsocket");
105
106 rccS.connect(arg()["socket-dir"], sockname);
107
108 const vector<string>&commands=arg().getCommands();
109 string command;
110 for(unsigned int i=0; i< commands.size(); ++i) {
111 if(i>0)
112 command+=" ";
113 command+=commands[i];
114 }
115 rccS.send(command);
116 string receive=rccS.recv(0, arg().asNum("timeout"));
117 if(receive.compare(0, 7, "Unknown") == 0) {
118 cerr<<receive<<endl;
119 return 1;
120 }
121 cout<<receive;
122 return 0;
123 }
124 catch(PDNSException& ae)
125 {
126 cerr<<"Fatal: "<<ae.reason<<"\n";
127 return 1;
128 }