]> granicus.if.org Git - pdns/commitdiff
make sure webserver is bound *before* privileges are dropped. Noticed by Thomas Miesl...
authorBert Hubert <bert.hubert@netherlabs.nl>
Thu, 10 Jun 2010 06:53:09 +0000 (06:53 +0000)
committerBert Hubert <bert.hubert@netherlabs.nl>
Thu, 10 Jun 2010 06:53:09 +0000 (06:53 +0000)
git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@1629 d19b8d6e-7fed-0310-83ef-9ca221ded41b

pdns/common_startup.cc
pdns/webserver.cc
pdns/webserver.hh
pdns/ws.cc
pdns/ws.hh

index 2408c0a564304eb6a6afd3e4add8f972a168d813..123889034ba4e63d3ce9acc623fc266f98c18f44 100644 (file)
@@ -304,8 +304,8 @@ void mainthread()
        L<<Logger::Error<<"Chrooted to '"<<::arg()["chroot"]<<"'"<<endl;      
    }  
 #endif
-
-   Utility::dropPrivs(newuid, newgid);
+  StatWebServer sws;
+  Utility::dropPrivs(newuid, newgid);
 
   if(::arg().mustDo("recursor")){
     DP=new DNSProxy(::arg()["recursor"]);
@@ -316,7 +316,7 @@ void mainthread()
   dl->go();
 
   pthread_t qtid;
-  StatWebServer sws;
+
 
   if(::arg().mustDo("webserver"))
     sws.go();
index 02fd5b4b5d5dca05aca98eec87431d40ce1cf608..a2193f2dc74526aeeb377dc651a8ce5af7f68ad3 100644 (file)
@@ -1,6 +1,6 @@
 /*
     PowerDNS Versatile Database Driven Nameserver
-    Copyright (C) 2002-2007  PowerDNS.COM BV
+    Copyright (C) 2002-2010  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
@@ -190,19 +190,26 @@ WebServer::WebServer(const string &listenaddress, int port, const string &passwo
   d_listenaddress=listenaddress;
   d_port=port;
   d_password=password;
+  d_server = 0; // on exception, this class becomes a NOOP later on
+  try {
+    d_server = new Server(d_port, d_listenaddress);
+  }
+  catch(SessionException &e) {
+    L<<Logger::Error<<"Fatal error in webserver: "<<e.reason<<endl;
+  }
 }
 
 void WebServer::go()
 {
+  if(!d_server)
+    return;
   try {
-    Server *s=new Server(d_port, d_listenaddress);
-    
     Session *client;
     pthread_t tid;
     
     L<<Logger::Error<<"Launched webserver on "<<d_listenaddress<<":"<<d_port<<endl;
 
-    while((client=s->accept())) {
+    while((client=d_server->accept())) {
       pthread_create(&tid, 0 , &serveConnection, (void *)client);
     }
   }
index e49249e14e5f7702b0f8ba72a9d822cb26a0789c..ff58d0bbf009f96f0a5fe46331782f4a5f1221fc 100644 (file)
@@ -23,6 +23,7 @@
 
 
 using namespace std;
+class Server;
 
 class WebServer
 {
@@ -41,5 +42,6 @@ private:
   static map<string,HandlerFunction *>d_functions;
   static void *d_that;
   static string d_password;
+  Server* d_server;
 };
 #endif /* WEBSERVER_HH */
index af640413e39608de6081e2a78f9017f29f8532c5..8740d1a8897a969423a3eb96771cf2f1fecb89d9 100644 (file)
@@ -29,7 +29,8 @@ extern StatBag S;
 StatWebServer::StatWebServer()
 {
   d_start=time(0);
-        d_min10=d_min5=d_min1=0;
+  d_min10=d_min5=d_min1=0;
+  d_ws = new WebServer(arg()["webserver-address"], arg().asNum("webserver-port"),arg()["webserver-password"]);
 }
 
 void StatWebServer::go()
@@ -223,10 +224,10 @@ string StatWebServer::indexfunction(const map<string,string> &varmap, void *ptr,
 void StatWebServer::launch()
 {
   try {
-    WebServer ws(arg()["webserver-address"], arg().asNum("webserver-port"),arg()["webserver-password"]);
-    ws.setCaller(this);
-    ws.registerHandler("",&indexfunction);
-    ws.go();
+    
+    d_ws->setCaller(this);
+    d_ws->registerHandler("",&indexfunction);
+    d_ws->go();
   }
   catch(...) {
     L<<Logger::Error<<"StatWebserver thread caught an exception, dying"<<endl;
index 291bb3e281dbadcc7c07da706977a4cd32fac4bd..be072cf18380a6c26521bb77d1af4ffd9a9599c9 100644 (file)
@@ -75,6 +75,7 @@ private:
   double d_10, d_5, d_1, d_max;
 };
 
+class WebServer;
 
 class StatWebServer
 {
@@ -97,6 +98,7 @@ private:
   double d_min10, d_min5, d_min1;
   Ewma d_queries, d_cachehits, d_cachemisses;
   Ewma d_qcachehits, d_qcachemisses;
+  WebServer *d_ws;
 };
 
 #endif