]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/rec_control.cc
Merge pull request #4920 from plzz/chopoff
[thirdparty/pdns.git] / pdns / rec_control.cc
1 /*
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 */
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 ArgvMap &arg()
33 {
34 static ArgvMap arg;
35 return arg;
36 }
37
38 static void initArguments(int argc, char** argv)
39 {
40 arg().set("config-dir","Location of configuration directory (recursor.conf)")=SYSCONFDIR;
41
42 arg().set("socket-dir",string("Where the controlsocket will live, ")+LOCALSTATEDIR+" when unset and not chrooted" )="";
43 arg().set("chroot","switch to chroot jail")="";
44 arg().set("process","When controlling multiple recursors, the target process number")="";
45 arg().set("timeout", "Number of seconds to wait for the recursor to respond")="5";
46 arg().set("config-name","Name of this virtual configuration - will rename the binary image")="";
47 arg().setCmd("help","Provide this helpful message");
48 arg().setCmd("version","Show the version of this program");
49
50 arg().laxParse(argc,argv);
51 if(arg().mustDo("help") || arg().getCommands().empty()) {
52 cout<<"syntax: rec_control [options] command, options as below: "<<endl<<endl;
53 cout<<arg().helpstring(arg()["help"])<<endl;
54 cout<<"In addition, 'rec_control help' can be used to retrieve a list\nof available commands from PowerDNS"<<endl;
55 exit(arg().mustDo("help") ? 0 : 99);
56 }
57
58 if(arg().mustDo("version")) {
59 cout<<"rec_control version "<<VERSION<<endl;
60 exit(0);
61 }
62
63 string configname=::arg()["config-dir"]+"/recursor.conf";
64 if (::arg()["config-name"] != "")
65 configname=::arg()["config-dir"]+"/recursor-"+::arg()["config-name"]+".conf";
66
67 cleanSlashes(configname);
68
69 if(!::arg().preParseFile(configname.c_str(), "socket-dir", ""))
70 cerr<<"Warning: unable to parse configuration file '"<<configname<<"'"<<endl;
71 if(!::arg().preParseFile(configname.c_str(), "chroot", ""))
72 cerr<<"Warning: unable to parse configuration file '"<<configname<<"'"<<endl;
73
74 arg().laxParse(argc,argv); // make sure the commandline wins
75
76 if (::arg()["socket-dir"].empty()) {
77 if (::arg()["chroot"].empty())
78 ::arg().set("socket-dir") = LOCALSTATEDIR;
79 else
80 ::arg().set("socket-dir") = ::arg()["chroot"] + "/";
81 } else if (!::arg()["chroot"].empty()) {
82 ::arg().set("socket-dir") = ::arg()["chroot"] + "/" + ::arg()["socket-dir"];
83 }
84 }
85
86 int main(int argc, char** argv)
87 try
88 {
89 initArguments(argc, argv);
90 RecursorControlChannel rccS;
91 string sockname="pdns_recursor";
92
93 if (arg()["config-name"] != "")
94 sockname+="-"+arg()["config-name"];
95
96 if(!arg()["process"].empty())
97 sockname+="."+arg()["process"];
98
99 sockname.append(".controlsocket");
100
101 rccS.connect(arg()["socket-dir"], sockname);
102
103 const vector<string>&commands=arg().getCommands();
104 string command;
105 for(unsigned int i=0; i< commands.size(); ++i) {
106 if(i>0)
107 command+=" ";
108 command+=commands[i];
109 }
110 rccS.send(command);
111 string receive=rccS.recv(0, arg().asNum("timeout"));
112 if(receive.compare(0, 7, "Unknown") == 0) {
113 cerr<<receive<<endl;
114 return 1;
115 }
116 cout<<receive;
117 return 0;
118 }
119 catch(PDNSException& ae)
120 {
121 cerr<<"Fatal: "<<ae.reason<<"\n";
122 return 1;
123 }