]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dynloader.cc
add OpenSSL exception to PowerDNS, Netherlabs, van Dijk and Hubert copyrights
[thirdparty/pdns.git] / pdns / dynloader.cc
CommitLineData
12c86877
BH
1/*
2 PowerDNS Versatile Database Driven Nameserver
040712e0 3 Copyright (C) 2002 - 2008 PowerDNS.COM BV
12c86877
BH
4
5 This program is free software; you can redistribute it and/or modify
894eff0f
BH
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation
12c86877 8
f782fe38
MH
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
12c86877
BH
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
06bd9ccf 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12c86877
BH
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>
040712e0 34#include <boost/shared_ptr.hpp>
12c86877
BH
35
36#include <sys/stat.h>
5c409fa2 37#include "pdnsexception.hh"
12c86877
BH
38#include "misc.hh"
39#include "dynmessenger.hh"
40#include "arguments.hh"
41#include "config.h"
42#include "statbag.hh"
43#include "misc.hh"
10f4eea8 44#include "namespaces.hh"
61b26744 45#include "namespaces.hh"
12c86877
BH
46
47ArgvMap &arg()
48{
49 static ArgvMap arg;
50 return arg;
51}
52
53StatBag S;
54
55int main(int argc, char **argv)
56{
57 string s_programname="pdns";
58 string localdir;
59
65efcdbc 60 ::arg().set("config-dir","Location of configuration directory (pdns.conf)")=SYSCONFDIR;
dfeb00b2
BH
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");
040712e0 65
dfeb00b2 66 ::arg().set("config-name","Name of this virtual configuration - will rename the binary image")="";
f78b02f3 67 ::arg().setCmd("no-config","Don't parse configuration file");
dfeb00b2
BH
68 ::arg().set("chroot","")="";
69 ::arg().setCmd("help","Provide a helpful message");
70 ::arg().laxParse(argc,argv);
12c86877 71
dfeb00b2 72 if(::arg().mustDo("help")) {
894eff0f 73 cerr<<"syntax:"<<endl<<endl;
dfeb00b2 74 cerr<<::arg().helpstring(::arg()["help"])<<endl;
894eff0f
BH
75 exit(99);
76 }
77
f78b02f3
PD
78 const vector<string>commands=::arg().getCommands();
79
80 if(commands.empty()) {
81 cerr<<"No command passed"<<endl;
82 return 0;
83 }
84
dfeb00b2
BH
85 if(::arg()["config-name"]!="")
86 s_programname+="-"+::arg()["config-name"];
12c86877 87
dfeb00b2 88 string configname=::arg()["config-dir"]+"/"+s_programname+".conf";
12c86877 89 cleanSlashes(configname);
f78b02f3
PD
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 }
12c86877 95
dfeb00b2 96 string socketname=::arg()["socket-dir"]+"/"+s_programname+".controlsocket";
f78b02f3
PD
97 cleanSlashes(socketname);
98
dfeb00b2 99 if(::arg()["chroot"].empty())
12c86877
BH
100 localdir="/tmp";
101 else
102 localdir=dirname(strdup(socketname.c_str()));
103
12c86877
BH
104 try {
105 string command=commands[0];
040712e0 106 shared_ptr<DynMessenger> D;
dfeb00b2 107 if(::arg()["remote-address"].empty())
040712e0
BH
108 D=shared_ptr<DynMessenger>(new DynMessenger(localdir,socketname));
109 else {
110 uint16_t port;
111 try {
4957a608 112 port = lexical_cast<uint16_t>(::arg()["remote-port"]);
040712e0
BH
113 }
114 catch(...) {
4957a608
BH
115 cerr<<"Unable to convert '"<<::arg()["remote-port"]<<"' to a port number for connecting to remote PowerDNS\n";
116 exit(99);
040712e0
BH
117 }
118
dfeb00b2 119 D=shared_ptr<DynMessenger>(new DynMessenger(ComboAddress(::arg()["remote-address"], port), ::arg()["secret"]));
040712e0 120 }
12c86877
BH
121
122 string message;
123 for(vector<string>::const_iterator i=commands.begin();i!=commands.end();++i) {
124 if(i!=commands.begin())
4957a608 125 message+=" ";
12c86877
BH
126 message+=*i;
127 }
128
129 if(command=="show") {
130 message="SHOW ";
131 for(unsigned int n=1;n<commands.size();n++) {
4957a608
BH
132 message+=commands[n];
133 message+=" ";
12c86877
BH
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
040712e0 151 if(D->send(message)<0) {
12c86877
BH
152 cerr<<"Error sending command"<<endl;
153 return 1;
154 }
155
040712e0 156 string resp=D->receive();
12c86877
BH
157
158 cout<<resp<<endl;
159 }
3f81d239 160 catch(PDNSException &ae) {
12c86877 161 cerr<<"Fatal error: "<<ae.reason<<endl;
20ca8e7d 162 return 1;
12c86877
BH
163 }
164 return 0;
165}
166
167