]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dynloader.cc
Merge remote-tracking branch 'origin/master' into dnsname
[thirdparty/pdns.git] / pdns / dynloader.cc
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 - 2008 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 <iostream>
26 #include <cstdio>
27 #include <cstring>
28 #include <cstdlib>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <climits>
32 #include <string>
33 #include <map>
34 #include <sys/mman.h>
35 #include <fcntl.h>
36 #include <sys/types.h>
37
38
39 #include <sys/stat.h>
40 #include "pdnsexception.hh"
41 #include "misc.hh"
42 #include "dynmessenger.hh"
43 #include "arguments.hh"
44 #include "statbag.hh"
45 #include "misc.hh"
46 #include "namespaces.hh"
47 #include "namespaces.hh"
48
49 ArgvMap &arg()
50 {
51 static ArgvMap arg;
52 return arg;
53 }
54
55 StatBag S;
56
57 int main(int argc, char **argv)
58 {
59 string s_programname="pdns";
60
61 ::arg().set("config-dir","Location of configuration directory (pdns.conf)")=SYSCONFDIR;
62 ::arg().set("socket-dir","Where the controlsocket will live")=LOCALSTATEDIR;
63 ::arg().set("remote-address","Remote address to query");
64 ::arg().set("remote-port","Remote port to query")="53000";
65 ::arg().set("secret","Secret needed to connect to remote PowerDNS");
66
67 ::arg().set("config-name","Name of this virtual configuration - will rename the binary image")="";
68 ::arg().setCmd("no-config","Don't parse configuration file");
69 ::arg().set("chroot","")="";
70 ::arg().setCmd("help","Provide a helpful message");
71 ::arg().laxParse(argc,argv);
72
73 if(::arg().mustDo("help")) {
74 cout<<"syntax:"<<endl<<endl;
75 cout<<::arg().helpstring(::arg()["help"])<<endl;
76 exit(0);
77 }
78
79 const vector<string>commands=::arg().getCommands();
80
81 if(commands.empty()) {
82 cerr<<"No command passed"<<endl;
83 return 0;
84 }
85
86 if(::arg()["config-name"]!="")
87 s_programname+="-"+::arg()["config-name"];
88
89 string configname=::arg()["config-dir"]+"/"+s_programname+".conf";
90 cleanSlashes(configname);
91
92 if(!::arg().mustDo("no-config")) {
93 ::arg().laxFile(configname.c_str());
94 ::arg().laxParse(argc,argv); // reparse so the commandline still wins
95 }
96
97 string socketname=::arg()["socket-dir"]+"/"+s_programname+".controlsocket";
98 cleanSlashes(socketname);
99
100 try {
101 string command=commands[0];
102 shared_ptr<DynMessenger> D;
103 if(::arg()["remote-address"].empty())
104 D=shared_ptr<DynMessenger>(new DynMessenger(socketname));
105 else {
106 uint16_t port;
107 try {
108 port = lexical_cast<uint16_t>(::arg()["remote-port"]);
109 }
110 catch(...) {
111 cerr<<"Unable to convert '"<<::arg()["remote-port"]<<"' to a port number for connecting to remote PowerDNS\n";
112 exit(99);
113 }
114
115 D=shared_ptr<DynMessenger>(new DynMessenger(ComboAddress(::arg()["remote-address"], port), ::arg()["secret"]));
116 }
117
118 string message;
119 for(vector<string>::const_iterator i=commands.begin();i!=commands.end();++i) {
120 if(i!=commands.begin())
121 message+=" ";
122 message+=*i;
123 }
124
125 if(command=="show") {
126 message="SHOW ";
127 for(unsigned int n=1;n<commands.size();n++) {
128 message+=commands[n];
129 message+=" ";
130 }
131 }
132 else if(command=="list") {
133 message="SHOW *";
134 command="show";
135 }
136 else if(command=="quit" || command=="QUIT") {
137 message="QUIT";
138 }
139 else if(command=="status" || command=="STATUS") {
140 message="STATUS";
141 }
142 else if(command=="version" || command=="VERSION") {
143 message="VERSION";
144 }
145
146
147 if(D->send(message)<0) {
148 cerr<<"Error sending command"<<endl;
149 return 1;
150 }
151
152 string resp=D->receive();
153 if(resp.compare(0, 7, "Unknown") == 0) {
154 cerr<<resp<<endl;
155 return 1;
156 }
157
158 cout<<resp<<endl;
159 }
160 catch(TimeoutException &ae) {
161 cerr<<"Timeout error: "<<ae.reason<<endl;
162 return 2;
163 }
164 catch(PDNSException &ae) {
165 cerr<<"Fatal error: "<<ae.reason<<endl;
166 return 1;
167 }
168 return 0;
169 }
170
171