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