]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/dynloader.cc
rec: ensure correct service user on debian
[thirdparty/pdns.git] / pdns / dynloader.cc
index a32bafb2b456fe5f2975b54a120bb8afa80d9f3d..23bb80b7d180a2b5d1627608a988e30d08509823 100644 (file)
@@ -1,24 +1,27 @@
 /*
-    PowerDNS Versatile Database Driven Nameserver
-    Copyright (C) 2002 - 2008  PowerDNS.COM BV
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License version 2 as 
-    published by the Free Software Foundation
-
-    Additionally, the license of this program contains a special
-    exception which allows to distribute the program in binary form when
-    it is linked against OpenSSL.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-*/
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <iostream>
 #include <cstdio>
 #include <cstring>
 #include <sys/mman.h>
 #include <fcntl.h>
 #include <sys/types.h>
-#include <boost/shared_ptr.hpp>
+
 
 #include <sys/stat.h>
 #include "pdnsexception.hh"
 #include "misc.hh"
 #include "dynmessenger.hh"
 #include "arguments.hh"
-#include "config.h"
 #include "statbag.hh"
 #include "misc.hh"
 #include "namespaces.hh"
@@ -55,10 +57,9 @@ StatBag S;
 int main(int argc, char **argv)
 {
   string s_programname="pdns";
-  string localdir;
 
   ::arg().set("config-dir","Location of configuration directory (pdns.conf)")=SYSCONFDIR;
-  ::arg().set("socket-dir","Where the controlsocket will live")=LOCALSTATEDIR;
+  ::arg().set("socket-dir",string("Where the controlsocket will live, ")+LOCALSTATEDIR+" when unset and not chrooted" )="";
   ::arg().set("remote-address","Remote address to query");
   ::arg().set("remote-port","Remote port to query")="53000";
   ::arg().set("secret","Secret needed to connect to remote PowerDNS");
@@ -72,6 +73,7 @@ int main(int argc, char **argv)
   if(::arg().mustDo("help")) {
     cout<<"syntax:"<<endl<<endl;
     cout<<::arg().helpstring(::arg()["help"])<<endl;
+    cout<<"In addition, 'pdns_control help' can be used to retrieve a list\nof available commands from PowerDNS"<<endl;
     exit(0);
   }
 
@@ -92,24 +94,29 @@ int main(int argc, char **argv)
     ::arg().laxFile(configname.c_str());
     ::arg().laxParse(argc,argv); // reparse so the commandline still wins
   }
-  
-  string socketname=::arg()["socket-dir"]+"/"+s_programname+".controlsocket";
+
+  string socketname=::arg()["socket-dir"];
+  if (::arg()["socket-dir"].empty()) {
+    if (::arg()["chroot"].empty())
+      socketname = LOCALSTATEDIR;
+    else
+      socketname = ::arg()["chroot"] + "/";
+  } else if (!::arg()["socket-dir"].empty() && !::arg()["chroot"].empty()) {
+    socketname = ::arg()["chroot"] + ::arg()["socket-dir"];
+  }
+
+  socketname += "/" + s_programname + ".controlsocket";
   cleanSlashes(socketname);
   
-  if(::arg()["chroot"].empty())
-    localdir="/tmp";
-  else
-    localdir=dirname(strdup(socketname.c_str()));
-
   try {
     string command=commands[0];
     shared_ptr<DynMessenger> D;
     if(::arg()["remote-address"].empty())
-      D=shared_ptr<DynMessenger>(new DynMessenger(localdir,socketname));
+      D=shared_ptr<DynMessenger>(new DynMessenger(socketname));
     else {
       uint16_t port;
       try {
-        port  = lexical_cast<uint16_t>(::arg()["remote-port"]);
+        port = static_cast<uint16_t>(pdns_stou(::arg()["remote-port"]));
       }
       catch(...) {
         cerr<<"Unable to convert '"<<::arg()["remote-port"]<<"' to a port number for connecting to remote PowerDNS\n";
@@ -154,6 +161,10 @@ int main(int argc, char **argv)
     }
     
     string resp=D->receive();
+    if(resp.compare(0, 7, "Unknown") == 0) {
+      cerr<<resp<<endl;
+      return 1;
+    }
     
     cout<<resp<<endl;
   }
@@ -165,6 +176,10 @@ int main(int argc, char **argv)
     cerr<<"Fatal error: "<<ae.reason<<endl;
     return 1;
   }
+  catch(const std::runtime_error& e) {
+    cerr<<"Runtime error: "<<e.what()<<endl;
+    return 2;
+  }
   return 0;
 }