| 1 | /* |
|---|
| 2 | PowerDNS Versatile Database Driven Nameserver |
|---|
| 3 | Copyright (C) 2003 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 |
|---|
| 7 | as published by the Free Software Foundation |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | This program is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 18 | */ |
|---|
| 19 | // $Id$ |
|---|
| 20 | #ifdef WIN32 |
|---|
| 21 | # define WINDOWS_LEAN_AND_MEAN |
|---|
| 22 | # include <windows.h> |
|---|
| 23 | #else |
|---|
| 24 | # include <sys/socket.h> |
|---|
| 25 | # include <netinet/in.h> |
|---|
| 26 | # include <arpa/inet.h> |
|---|
| 27 | # include <sys/time.h> |
|---|
| 28 | # include <sys/wait.h> |
|---|
| 29 | # include <sys/mman.h> |
|---|
| 30 | #endif // WIN32 |
|---|
| 31 | |
|---|
| 32 | #include "utility.hh" |
|---|
| 33 | #include <cstdio> |
|---|
| 34 | #include <signal.h> |
|---|
| 35 | #include <cstring> |
|---|
| 36 | #include <cstdlib> |
|---|
| 37 | #include <sys/types.h> |
|---|
| 38 | #include <iostream> |
|---|
| 39 | #include <string> |
|---|
| 40 | #include <sys/stat.h> |
|---|
| 41 | #include <unistd.h> |
|---|
| 42 | #include <sys/types.h> |
|---|
| 43 | #include <errno.h> |
|---|
| 44 | #include <pthread.h> |
|---|
| 45 | #include <unistd.h> |
|---|
| 46 | #include <fcntl.h> |
|---|
| 47 | #include <fstream> |
|---|
| 48 | |
|---|
| 49 | #include "dns.hh" |
|---|
| 50 | #include "dnsbackend.hh" |
|---|
| 51 | #include "ueberbackend.hh" |
|---|
| 52 | #include "dnspacket.hh" |
|---|
| 53 | #include "nameserver.hh" |
|---|
| 54 | #include "distributor.hh" |
|---|
| 55 | #include "logger.hh" |
|---|
| 56 | #include "arguments.hh" |
|---|
| 57 | #include "packethandler.hh" |
|---|
| 58 | #include "statbag.hh" |
|---|
| 59 | #include "tcpreceiver.hh" |
|---|
| 60 | #include "packetcache.hh" |
|---|
| 61 | #include "ws.hh" |
|---|
| 62 | #include "misc.hh" |
|---|
| 63 | #include "dynlistener.hh" |
|---|
| 64 | #include "dynhandler.hh" |
|---|
| 65 | #include "communicator.hh" |
|---|
| 66 | #include "dnsproxy.hh" |
|---|
| 67 | #include "utility.hh" |
|---|
| 68 | #include "common_startup.hh" |
|---|
| 69 | |
|---|
| 70 | time_t s_starttime; |
|---|
| 71 | |
|---|
| 72 | string s_programname="pdns"; // used in packethandler.cc |
|---|
| 73 | |
|---|
| 74 | char *funnytext= |
|---|
| 75 | "*****************************************************************************\n"\ |
|---|
| 76 | "Ok, you just ran pdns_server through 'strings' hoping to find funny messages.\n"\ |
|---|
| 77 | "Well, you found one. \n"\ |
|---|
| 78 | "Two ions are flying through their particle accelerator, says the one to the\n" |
|---|
| 79 | "other 'I think I've lost an electron!' \n"\ |
|---|
| 80 | "So the other one says, 'Are you sure?'. 'YEAH! I'M POSITIVE!'\n"\ |
|---|
| 81 | " the pdns crew - pdns@powerdns.com\n" |
|---|
| 82 | "*****************************************************************************\n"; |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | // start (sys)logging |
|---|
| 86 | |
|---|
| 87 | /** \var Logger L |
|---|
| 88 | \brief All logging is done via L, a Logger instance |
|---|
| 89 | */ |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | \file receiver.cc |
|---|
| 94 | \brief The main loop of powerdns |
|---|
| 95 | |
|---|
| 96 | This file is where it all happens - main is here, as are the two pivotal threads qthread() and athread() |
|---|
| 97 | */ |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | static void WIN32_declareArguments() |
|---|
| 101 | { |
|---|
| 102 | arg().set("config-dir","Location of configuration directory (pdns.conf)")="./"; |
|---|
| 103 | //arg().set("config-name","Name of this virtual configuration - will rename the binary image")=""; |
|---|
| 104 | arg().set("module-dir","Default directory for modules")="/../lib"; |
|---|
| 105 | |
|---|
| 106 | arg().setSwitch( "register-service", "Register the service" )= "no"; |
|---|
| 107 | arg().setSwitch( "unregister-service", "Unregister the service" )= "no"; |
|---|
| 108 | arg().setSwitch( "ntservice", "Run as service" )= "no"; |
|---|
| 109 | |
|---|
| 110 | arg().setSwitch( "use-ntlog", "Use the NT logging facilities" )= "yes"; |
|---|
| 111 | arg().setSwitch( "use-logfile", "Use a log file" )= "no"; |
|---|
| 112 | arg().setSwitch( "logfile", "Filename of the log file" )= "powerdns.log"; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | static void loadModules() |
|---|
| 116 | { |
|---|
| 117 | L << Logger::Warning << Logger::NTLog << "The Windows version doesn't support dll loading (yet), none of the specified modules loaded" << std::endl; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | //! Console handler. |
|---|
| 121 | BOOL WINAPI consoleHandler( DWORD ctrl ) |
|---|
| 122 | { |
|---|
| 123 | L << Logger::Error << "PowerDNS shutting down..." << endl; |
|---|
| 124 | exit( 0 ); |
|---|
| 125 | |
|---|
| 126 | // This will never be reached. |
|---|
| 127 | return true; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | //! The main function of pdns, the pdns process |
|---|
| 132 | int main(int argc, char **argv) |
|---|
| 133 | { |
|---|
| 134 | s_programname="pdns"; |
|---|
| 135 | s_starttime=time(0); |
|---|
| 136 | |
|---|
| 137 | PDNSService pdns; |
|---|
| 138 | |
|---|
| 139 | // Initialize winsock. |
|---|
| 140 | WSAData wsaData; |
|---|
| 141 | |
|---|
| 142 | if ( WSAStartup( MAKEWORD( 2, 0 ), &wsaData ) != 0 ) |
|---|
| 143 | { |
|---|
| 144 | cerr << "Could not initialize winsock.dll" << endl; |
|---|
| 145 | return -1; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | L.toConsole(Logger::Warning); |
|---|
| 149 | try { |
|---|
| 150 | declareArguments(); |
|---|
| 151 | WIN32_declareArguments(); |
|---|
| 152 | |
|---|
| 153 | arg().laxParse(argc,argv); // do a lax parse |
|---|
| 154 | |
|---|
| 155 | // If we have to run as a nt service change the current directory to the executable directory. |
|---|
| 156 | if ( arg().mustDo( "ntservice" )) |
|---|
| 157 | { |
|---|
| 158 | char dir[ MAX_PATH ]; |
|---|
| 159 | string newdir; |
|---|
| 160 | |
|---|
| 161 | GetModuleFileName( NULL, dir, sizeof( dir )); |
|---|
| 162 | |
|---|
| 163 | newdir = dir; |
|---|
| 164 | newdir = newdir.substr( 0, newdir.find_last_of( "\\" )); |
|---|
| 165 | |
|---|
| 166 | SetCurrentDirectory( newdir.c_str()); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | if(arg()["config-name"]!="") |
|---|
| 170 | s_programname+="-"+arg()["config-name"]; |
|---|
| 171 | |
|---|
| 172 | (void)theL(s_programname); |
|---|
| 173 | |
|---|
| 174 | string configname=arg()["config-dir"]+"/"+s_programname+".conf"; |
|---|
| 175 | cleanSlashes(configname); |
|---|
| 176 | |
|---|
| 177 | if(!arg().mustDo("config") && !arg().mustDo("no-config")) // "config" == print a configuration file |
|---|
| 178 | arg().laxFile(configname.c_str()); |
|---|
| 179 | |
|---|
| 180 | arg().laxParse(argc,argv); // reparse so the commandline still wins |
|---|
| 181 | L.toConsole((Logger::Urgency)(arg().asNum("loglevel"))); |
|---|
| 182 | |
|---|
| 183 | if(arg().mustDo("help") || arg().mustDo("config")) { |
|---|
| 184 | arg().set("daemon")="no"; |
|---|
| 185 | arg().set("guardian")="no"; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | if ( arg().mustDo( "register-service" )) |
|---|
| 190 | { |
|---|
| 191 | if ( !pdns.registerService( "An advanced high performance authoritative nameserver.", true )) |
|---|
| 192 | { |
|---|
| 193 | cerr << "Could not register service." << endl; |
|---|
| 194 | exit( 99 ); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | // Exit. |
|---|
| 198 | exit( 0 ); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | if ( arg().mustDo( "unregister-service" )) |
|---|
| 202 | { |
|---|
| 203 | pdns.unregisterService(); |
|---|
| 204 | exit( 0 ); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | // we really need to do work - either standalone or as an instance |
|---|
| 208 | BackendMakers().launch(arg()["launch"]); // vrooooom! |
|---|
| 209 | |
|---|
| 210 | if(arg().mustDo("version")) { |
|---|
| 211 | cerr<<"Version: "VERSION", compiled on "<<__DATE__", "__TIME__<<endl; |
|---|
| 212 | exit(99); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | if(arg().mustDo("help")) { |
|---|
| 217 | cerr<<"syntax:"<<endl<<endl; |
|---|
| 218 | cerr<<arg().helpstring(arg()["help"])<<endl; |
|---|
| 219 | exit(99); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | if(arg().mustDo("config")) { |
|---|
| 223 | cout<<arg().configstring()<<endl; |
|---|
| 224 | exit(99); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | if(arg().mustDo("list-modules")) { |
|---|
| 228 | vector<string>modules=BackendMakers().getModules(); |
|---|
| 229 | cerr<<"Modules available:"<<endl; |
|---|
| 230 | for(vector<string>::const_iterator i=modules.begin();i!=modules.end();++i) |
|---|
| 231 | cout<<*i<<endl; |
|---|
| 232 | |
|---|
| 233 | exit(99); |
|---|
| 234 | } |
|---|
| 235 | if(!BackendMakers().numLauncheable()) { |
|---|
| 236 | L<<Logger::Error<<"Unable to launch, no backends configured for querying"<<endl; |
|---|
| 237 | exit(99); // this isn't going to fix itself either |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | if(arg().mustDo("control-console")) |
|---|
| 241 | dl=new DynListener(); |
|---|
| 242 | else |
|---|
| 243 | dl=new DynListener(s_programname); |
|---|
| 244 | |
|---|
| 245 | dl->registerFunc("SHOW",&DLShowHandler); |
|---|
| 246 | dl->registerFunc("RPING",&DLPingHandler); |
|---|
| 247 | dl->registerFunc("QUIT",&DLRQuitHandler); |
|---|
| 248 | dl->registerFunc("UPTIME",&DLUptimeHandler); |
|---|
| 249 | dl->registerFunc("NOTIFY-HOST",&DLNotifyHostHandler); |
|---|
| 250 | dl->registerFunc("NOTIFY",&DLNotifyHandler); |
|---|
| 251 | dl->registerFunc("RELOAD",&DLReloadHandler); |
|---|
| 252 | dl->registerFunc("REDISCOVER",&DLRediscoverHandler); |
|---|
| 253 | dl->registerFunc("VERSION",&DLVersionHandler); |
|---|
| 254 | dl->registerFunc("PURGE",&DLPurgeHandler); |
|---|
| 255 | dl->registerFunc("CCOUNTS",&DLCCHandler); |
|---|
| 256 | dl->registerFunc("SET",&DLSettingsHandler); |
|---|
| 257 | dl->registerFunc("RETRIEVE",&DLNotifyRetrieveHandler); |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | // reparse, with error checking |
|---|
| 261 | if(!arg().mustDo("no-config")) |
|---|
| 262 | arg().file(configname.c_str()); |
|---|
| 263 | arg().parse(argc,argv); |
|---|
| 264 | UeberBackend::go(); |
|---|
| 265 | N=new UDPNameserver; // this fails when we are not root, throws exception |
|---|
| 266 | |
|---|
| 267 | if(!arg().mustDo("disable-tcp")) |
|---|
| 268 | TN=new TCPNameserver; |
|---|
| 269 | } |
|---|
| 270 | catch(const ArgException &A) { |
|---|
| 271 | L<<Logger::Error<<"Fatal error: "<<A.reason<<endl; |
|---|
| 272 | exit(1); |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | declareStats(); |
|---|
| 276 | DLOG(L<<Logger::Warning<<"Verbose logging in effect"<<endl); |
|---|
| 277 | |
|---|
| 278 | if ( arg().mustDo( "use-ntlog" ) && arg().mustDo( "ntservice" )) |
|---|
| 279 | L.toNTLog(); |
|---|
| 280 | |
|---|
| 281 | if ( arg().mustDo( "use-logfile" )) |
|---|
| 282 | L.toFile( arg()[ "logfile" ] ); |
|---|
| 283 | |
|---|
| 284 | L<<Logger::Warning<<"PowerDNS "<<VERSION<<" (C) 2001-2003 PowerDNS.COM BV ("<<__DATE__", "__TIME__<<") starting up"<<endl; |
|---|
| 285 | |
|---|
| 286 | L<<Logger::Warning<<"PowerDNS comes with ABSOLUTELY NO WARRANTY. " |
|---|
| 287 | "This is free software, and you are welcome to redistribute it " |
|---|
| 288 | "according to the terms of the GPL version 2."<<endl; |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | // Register console control hander. |
|---|
| 292 | if ( !arg().mustDo( "ntservice" )) |
|---|
| 293 | SetConsoleCtrlHandler( consoleHandler, true ); |
|---|
| 294 | |
|---|
| 295 | PDNSService::instance()->start( argc, argv, arg().mustDo( "ntservice" )); |
|---|
| 296 | |
|---|
| 297 | WSACleanup(); |
|---|
| 298 | |
|---|
| 299 | exit(1); |
|---|
| 300 | |
|---|
| 301 | return 0; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|