]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dynloader.cc
auth: switch circleci mssql image
[thirdparty/pdns.git] / pdns / dynloader.cc
CommitLineData
12c86877 1/*
12471842
PL
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 */
870a0fe4
AT
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
12c86877
BH
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>
dd7da6cd 37
12c86877
BH
38
39#include <sys/stat.h>
5c409fa2 40#include "pdnsexception.hh"
12c86877
BH
41#include "misc.hh"
42#include "dynmessenger.hh"
43#include "arguments.hh"
12c86877
BH
44#include "statbag.hh"
45#include "misc.hh"
10f4eea8 46#include "namespaces.hh"
61b26744 47#include "namespaces.hh"
12c86877
BH
48
49ArgvMap &arg()
50{
51 static ArgvMap arg;
52 return arg;
53}
54
55StatBag S;
56
57int main(int argc, char **argv)
58{
59 string s_programname="pdns";
12c86877 60
65efcdbc 61 ::arg().set("config-dir","Location of configuration directory (pdns.conf)")=SYSCONFDIR;
f0f3f0b0 62 ::arg().set("socket-dir",string("Where the controlsocket will live, ")+LOCALSTATEDIR+" when unset and not chrooted" )="";
dfeb00b2
BH
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");
040712e0 66
dfeb00b2 67 ::arg().set("config-name","Name of this virtual configuration - will rename the binary image")="";
f78b02f3 68 ::arg().setCmd("no-config","Don't parse configuration file");
dfeb00b2
BH
69 ::arg().set("chroot","")="";
70 ::arg().setCmd("help","Provide a helpful message");
71 ::arg().laxParse(argc,argv);
12c86877 72
dfeb00b2 73 if(::arg().mustDo("help")) {
ff5ba4f9
WA
74 cout<<"syntax:"<<endl<<endl;
75 cout<<::arg().helpstring(::arg()["help"])<<endl;
b710dd81 76 cout<<"In addition, 'pdns_control help' can be used to retrieve a list\nof available commands from PowerDNS"<<endl;
ff5ba4f9 77 exit(0);
894eff0f
BH
78 }
79
f78b02f3
PD
80 const vector<string>commands=::arg().getCommands();
81
82 if(commands.empty()) {
83 cerr<<"No command passed"<<endl;
84 return 0;
85 }
86
dfeb00b2
BH
87 if(::arg()["config-name"]!="")
88 s_programname+="-"+::arg()["config-name"];
12c86877 89
dfeb00b2 90 string configname=::arg()["config-dir"]+"/"+s_programname+".conf";
12c86877 91 cleanSlashes(configname);
f78b02f3
PD
92
93 if(!::arg().mustDo("no-config")) {
94 ::arg().laxFile(configname.c_str());
95 ::arg().laxParse(argc,argv); // reparse so the commandline still wins
96 }
f0f3f0b0
PL
97
98 string socketname=::arg()["socket-dir"];
99 if (::arg()["socket-dir"].empty()) {
100 if (::arg()["chroot"].empty())
101 socketname = LOCALSTATEDIR;
102 else
103 socketname = ::arg()["chroot"] + "/";
104 } else if (!::arg()["socket-dir"].empty() && !::arg()["chroot"].empty()) {
105 socketname = ::arg()["chroot"] + ::arg()["socket-dir"];
106 }
107
108 socketname += "/" + s_programname + ".controlsocket";
f78b02f3
PD
109 cleanSlashes(socketname);
110
12c86877
BH
111 try {
112 string command=commands[0];
040712e0 113 shared_ptr<DynMessenger> D;
dfeb00b2 114 if(::arg()["remote-address"].empty())
79d65b3b 115 D=shared_ptr<DynMessenger>(new DynMessenger(socketname));
040712e0
BH
116 else {
117 uint16_t port;
118 try {
335da0ba 119 port = static_cast<uint16_t>(pdns_stou(::arg()["remote-port"]));
040712e0
BH
120 }
121 catch(...) {
4957a608
BH
122 cerr<<"Unable to convert '"<<::arg()["remote-port"]<<"' to a port number for connecting to remote PowerDNS\n";
123 exit(99);
040712e0
BH
124 }
125
dfeb00b2 126 D=shared_ptr<DynMessenger>(new DynMessenger(ComboAddress(::arg()["remote-address"], port), ::arg()["secret"]));
040712e0 127 }
12c86877
BH
128
129 string message;
130 for(vector<string>::const_iterator i=commands.begin();i!=commands.end();++i) {
131 if(i!=commands.begin())
4957a608 132 message+=" ";
12c86877
BH
133 message+=*i;
134 }
135
136 if(command=="show") {
137 message="SHOW ";
138 for(unsigned int n=1;n<commands.size();n++) {
4957a608
BH
139 message+=commands[n];
140 message+=" ";
12c86877
BH
141 }
142 }
143 else if(command=="list") {
144 message="SHOW *";
145 command="show";
146 }
147 else if(command=="quit" || command=="QUIT") {
148 message="QUIT";
149 }
150 else if(command=="status" || command=="STATUS") {
151 message="STATUS";
152 }
153 else if(command=="version" || command=="VERSION") {
154 message="VERSION";
155 }
156
157
040712e0 158 if(D->send(message)<0) {
12c86877
BH
159 cerr<<"Error sending command"<<endl;
160 return 1;
161 }
162
040712e0 163 string resp=D->receive();
72ab36f4
RK
164 if(resp.compare(0, 7, "Unknown") == 0) {
165 cerr<<resp<<endl;
166 return 1;
167 }
12c86877
BH
168
169 cout<<resp<<endl;
170 }
c7c7edb5
IM
171 catch(TimeoutException &ae) {
172 cerr<<"Timeout error: "<<ae.reason<<endl;
173 return 2;
174 }
3f81d239 175 catch(PDNSException &ae) {
12c86877 176 cerr<<"Fatal error: "<<ae.reason<<endl;
20ca8e7d 177 return 1;
12c86877 178 }
34c513f9
RG
179 catch(const std::runtime_error& e) {
180 cerr<<"Runtime error: "<<e.what()<<endl;
181 return 2;
182 }
12c86877
BH
183 return 0;
184}
185
186