From 8cb70f23571a2fa7e5505a3e668aa712096026cb Mon Sep 17 00:00:00 2001 From: Chris Hofstaedtler Date: Wed, 8 Dec 2021 20:46:39 +0100 Subject: [PATCH] auth: merge "main" code into new auth-main.(cc|hh) Merge common_startup.cc and receiver.cc into auth-main.cc, and rename common_startup.hh into receiver.hh. This is a very minimal merge with no cleanup. Its intention is to make understanding the startup code path easier, by avoiding having to look at two compilation units to determine the exact order. --- .not-formatted | 3 - configure.ac | 2 +- pdns/Makefile.am | 3 +- pdns/auth-carbon.cc | 2 +- pdns/{common_startup.cc => auth-main.cc} | 666 +++++++++++++++++++++- pdns/{common_startup.hh => auth-main.hh} | 0 pdns/dynhandler.cc | 2 +- pdns/lua-record.cc | 2 +- pdns/nameserver.cc | 4 +- pdns/packethandler.cc | 2 +- pdns/receiver.cc | 697 ----------------------- pdns/slavecommunicator.cc | 2 +- pdns/tcpreceiver.cc | 2 +- pdns/ws-auth.cc | 2 +- 14 files changed, 669 insertions(+), 720 deletions(-) rename pdns/{common_startup.cc => auth-main.cc} (66%) rename pdns/{common_startup.hh => auth-main.hh} (100%) delete mode 100644 pdns/receiver.cc diff --git a/.not-formatted b/.not-formatted index 22c9a461b422..1725c9bdaa2f 100644 --- a/.not-formatted +++ b/.not-formatted @@ -29,8 +29,6 @@ ./pdns/cdb.hh ./pdns/comfun.cc ./pdns/comment.hh -./pdns/common_startup.cc -./pdns/common_startup.hh ./pdns/communicator.cc ./pdns/communicator.hh ./pdns/dbdnsseckeeper.cc @@ -237,7 +235,6 @@ ./pdns/query-local-address.hh ./pdns/rcpgenerator.cc ./pdns/rcpgenerator.hh -./pdns/receiver.cc ./pdns/remote_logger.cc ./pdns/remote_logger.hh ./pdns/resolve-context.hh diff --git a/configure.ac b/configure.ac index 2359358f0716..42c1bd21ea4d 100644 --- a/configure.ac +++ b/configure.ac @@ -4,7 +4,7 @@ AC_INIT([pdns], m4_esyscmd([builder-support/gen-version])) AC_CONFIG_AUX_DIR([build-aux]) AM_INIT_AUTOMAKE([foreign dist-bzip2 no-dist-gzip tar-ustar -Wno-portability subdir-objects parallel-tests 1.11]) AM_SILENT_RULES([yes]) -AC_CONFIG_SRCDIR([pdns/receiver.cc]) +AC_CONFIG_SRCDIR([pdns/auth-main.cc]) AC_CONFIG_MACRO_DIR([m4]) AC_USE_SYSTEM_EXTENSIONS diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 2a5ed8852114..ec93fadc6f3e 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -189,6 +189,7 @@ pdns_server_SOURCES = \ auth-caches.cc auth-caches.hh \ auth-carbon.cc \ auth-catalogzone.cc auth-catalogzone.hh \ + auth-main.cc auth-main.hh \ auth-packetcache.cc auth-packetcache.hh \ auth-querycache.cc auth-querycache.hh \ auth-zonecache.cc auth-zonecache.hh \ @@ -204,7 +205,6 @@ pdns_server_SOURCES = \ cachecleaner.hh \ circular_buffer.hh \ comment.hh \ - common_startup.cc common_startup.hh \ communicator.cc communicator.hh \ credentials.cc credentials.hh \ dbdnsseckeeper.cc \ @@ -252,7 +252,6 @@ pdns_server_SOURCES = \ qtype.cc qtype.hh \ query-local-address.hh query-local-address.cc \ rcpgenerator.cc \ - receiver.cc \ resolver.cc resolver.hh \ responsestats.cc responsestats.hh responsestats-auth.cc \ rfc2136handler.cc \ diff --git a/pdns/auth-carbon.cc b/pdns/auth-carbon.cc index 41767bc7bfd4..e46ff8d0ed92 100644 --- a/pdns/auth-carbon.cc +++ b/pdns/auth-carbon.cc @@ -28,7 +28,7 @@ #include "iputils.hh" #include "sstuff.hh" #include "arguments.hh" -#include "common_startup.hh" +#include "auth-main.hh" #include "namespaces.hh" diff --git a/pdns/common_startup.cc b/pdns/auth-main.cc similarity index 66% rename from pdns/common_startup.cc rename to pdns/auth-main.cc index 34a41320e7bc..a76d2f60234a 100644 --- a/pdns/common_startup.cc +++ b/pdns/auth-main.cc @@ -22,24 +22,85 @@ #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "common_startup.hh" -#include "ws-auth.hh" -#include "secpoll-auth.hh" -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef HAVE_LIBSODIUM +#include +#endif +#ifdef HAVE_SYSTEMD +#include +#endif + +#include "auth-main.hh" +#include "secpoll-auth.hh" #include "dynhandler.hh" #include "dnsseckeeper.hh" #include "threadname.hh" #include "misc.hh" #include "query-local-address.hh" #include "trusted-notification-proxy.hh" +#include "packetcache.hh" #include "packethandler.hh" +#include "opensslsigners.hh" +#include "dns.hh" +#include "dnsbackend.hh" +#include "ueberbackend.hh" +#include "dnspacket.hh" +#include "nameserver.hh" +#include "distributor.hh" +#include "logger.hh" +#include "arguments.hh" +#include "packethandler.hh" +#include "statbag.hh" +#include "tcpreceiver.hh" +#include "misc.hh" +#include "dynlistener.hh" +#include "dynhandler.hh" +#include "communicator.hh" +#include "dnsproxy.hh" +#include "utility.hh" +#include "dnsrecords.hh" +#include "version.hh" +#include "ws-auth.hh" -#include +#ifdef HAVE_LUA_RECORDS +#include "minicurl.hh" +#endif /* HAVE_LUA_RECORDS */ -#ifdef HAVE_SYSTEMD -#include -#endif +time_t s_starttime; + +string s_programname="pdns"; // used in packethandler.cc + +const char *funnytext= +"*****************************************************************************\n"\ +"Ok, you just ran pdns_server through 'strings' hoping to find funny messages.\n"\ +"Well, you found one. \n"\ +"Two ions are flying through their particle accelerator, says the one to the\n" +"other 'I think I've lost an electron!' \n"\ +"So the other one says, 'Are you sure?'. 'YEAH! I'M POSITIVE!'\n"\ +" the pdns crew - pdns@powerdns.com\n" +"*****************************************************************************\n"; bool g_anyToTcp; bool g_8bitDNS; @@ -67,6 +128,8 @@ vector > g_udpReceivers; NetmaskGroup g_proxyProtocolACL; size_t g_proxyProtocolMaximumSize; +void mainthread(); + ArgvMap &arg() { return theArg; @@ -816,3 +879,590 @@ void mainthread() g_log<&parts, pid_t ppid) +{ + kill(cpid, SIGKILL); // why? + kill(cpid, SIGKILL); // why? + sleep(1); + return "ok"; +} + +static string DLRestHandler(const vector&parts, pid_t ppid) +{ + string line; + + for(vector::const_iterator i=parts.begin();i!=parts.end();++i) { + if(i!=parts.begin()) + line.append(1,' '); + line.append(*i); + } + line.append(1,'\n'); + + std::lock_guard l(g_guardian_lock); + + try { + writen2(g_fd1[1],line.c_str(),line.size()+1); + } + catch(PDNSException &ae) { + return "Error communicating with instance: "+ae.reason; + } + char mesg[512]; + string response; + while(fgets(mesg,sizeof(mesg),g_fp)) { + if(*mesg=='\0') + break; + response+=mesg; + } + boost::trim_right(response); + return response; +} + +static int guardian(int argc, char **argv) +{ + if(isGuarded(argv)) + return 0; + + int infd=0, outfd=1; + + DynListener dlg(s_programname); + dlg.registerFunc("QUIT",&DLQuitHandler, "quit daemon"); + dlg.registerFunc("CYCLE",&DLCycleHandler, "restart instance"); + dlg.registerFunc("PING",&DLPingHandler, "ping guardian"); + dlg.registerFunc("STATUS",&DLStatusHandler, "get instance status from guardian"); + dlg.registerRestFunc(&DLRestHandler); + dlg.go(); + string progname=argv[0]; + + bool first=true; + cpid=0; + + g_guardian_lock.lock(); + + for(;;) { + int pid; + setStatus("Launching child"); + + if(pipe(g_fd1)<0 || pipe(g_fd2)<0) { + g_log<((progname+"-instance").c_str())); + for(n=1;n0) { // parent + close(g_fd1[0]); + close(g_fd2[1]); + + if(first) { + first=false; + signal(SIGTERM, takedown); + + signal(SIGHUP, SIG_IGN); + signal(SIGUSR1, SIG_IGN); + signal(SIGUSR2, SIG_IGN); + + writePid(); + } + g_guardian_lock.unlock(); + int status; + cpid=pid; + for(;;) { + int ret=waitpid(pid,&status,WNOHANG); + + if(ret<0) { + g_log< +static void tbhandler(int num) +{ + g_log<= 0) + g_log.setFacility(val); + else + g_log< modules; + + stringtok(modules,::arg()["load-modules"], ", "); + if (!UeberBackend::loadModules(modules, ::arg()["module-dir"])) { + exit(1); + } + } + + BackendMakers().launch(::arg()["launch"]); // vrooooom! + + if(!::arg().getCommands().empty()) { + cerr<<"Fatal: non-option"; + if (::arg().getCommands().size() > 1) { + cerr<<"s"; + } + cerr<<" ("; + bool first = true; + for (const auto& c : ::arg().getCommands()) { + if (!first) { + cerr<<", "; + } + first = false; + cerr<(); // listens on stdin + } + else { + g_log<(); + else + dl = std::make_unique(s_programname); + + writePid(); + } + DynListener::registerFunc("SHOW",&DLShowHandler, "show a specific statistic or * to get a list", ""); + DynListener::registerFunc("RPING",&DLPingHandler, "ping instance"); + DynListener::registerFunc("QUIT",&DLRQuitHandler, "quit daemon"); + DynListener::registerFunc("UPTIME",&DLUptimeHandler, "get instance uptime"); + DynListener::registerFunc("NOTIFY-HOST", &DLNotifyHostHandler, "notify host for specific zone", " "); + DynListener::registerFunc("NOTIFY", &DLNotifyHandler, "queue a notification", ""); + DynListener::registerFunc("RELOAD",&DLReloadHandler, "reload all zones"); + DynListener::registerFunc("REDISCOVER",&DLRediscoverHandler, "discover any new zones"); + DynListener::registerFunc("VERSION",&DLVersionHandler, "get instance version"); + DynListener::registerFunc("PURGE",&DLPurgeHandler, "purge entries from packet cache", "[]"); + DynListener::registerFunc("CCOUNTS",&DLCCHandler, "get cache statistics"); + DynListener::registerFunc("QTYPES", &DLQTypesHandler, "get QType statistics"); + DynListener::registerFunc("RESPSIZES", &DLRSizesHandler, "get histogram of response sizes"); + DynListener::registerFunc("REMOTES", &DLRemotesHandler, "get top remotes"); + DynListener::registerFunc("SET",&DLSettingsHandler, "set config variables", " "); + DynListener::registerFunc("RETRIEVE", &DLNotifyRetrieveHandler, "retrieve slave zone", " []"); + DynListener::registerFunc("CURRENT-CONFIG",&DLCurrentConfigHandler, "retrieve the current configuration", "[diff]"); + DynListener::registerFunc("LIST-ZONES", &DLListZones, "show list of zones", "[primary|secondary|native]"); + DynListener::registerFunc("TOKEN-LOGIN", &DLTokenLogin, "Login to a PKCS#11 token", " "); + DynListener::registerFunc("XFR-QUEUE", &DLSuckRequests, "Get all requests for XFR in queue"); + + if(!::arg()["tcp-control-address"].empty()) { + DynListener* dlTCP=new DynListener(ComboAddress(::arg()["tcp-control-address"], ::arg().asNum("tcp-control-port"))); + dlTCP->go(); + } + + // reparse, with error checking + if(!::arg().mustDo("no-config")) + ::arg().file(configname.c_str()); + ::arg().parse(argc,argv); + + if(::arg()["server-id"].empty()) { + char tmp[128]; + if(gethostname(tmp, sizeof(tmp)-1) == 0) { + ::arg().set("server-id")=tmp; + } else { + g_log<(); // this fails when we are not root, throws exception + g_udpReceivers.push_back(N); + + size_t rthreads = ::arg().asNum("receiver-threads", 1); + if (rthreads > 1 && N->canReusePort()) { + g_udpReceivers.resize(rthreads); + + for (size_t idx = 1; idx < rthreads; idx++) { + try { + g_udpReceivers[idx] = std::make_shared(true); + } + catch(const PDNSException& e) { + g_log<(); + } + catch(const ArgException &A) { + g_log< #include "responsestats.hh" -#include "common_startup.hh" +#include "auth-main.hh" #include "dns.hh" #include "dnsbackend.hh" #include "dnspacket.hh" @@ -83,7 +83,7 @@ extern StatBag S; These statistics are made available via the UeberBackend on the same socket that is used for dynamic module commands. \section Main Main - The main() of PowerDNS can be found in receiver.cc - start reading there for further insights into the operation of the nameserver + The main() of PowerDNS can be found in auth-main.cc - start reading there for further insights into the operation of the nameserver */ vector g_localaddresses; // not static, our unit tests need to poke this diff --git a/pdns/packethandler.cc b/pdns/packethandler.cc index f34d83b8e91b..ac7f62a6ee7f 100644 --- a/pdns/packethandler.cc +++ b/pdns/packethandler.cc @@ -44,7 +44,7 @@ #include "communicator.hh" #include "dnsproxy.hh" #include "version.hh" -#include "common_startup.hh" +#include "auth-main.hh" #include "trusted-notification-proxy.hh" #if 0 diff --git a/pdns/receiver.cc b/pdns/receiver.cc deleted file mode 100644 index e5e67854d31e..000000000000 --- a/pdns/receiver.cc +++ /dev/null @@ -1,697 +0,0 @@ -/* - * 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 "packetcache.hh" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#ifdef HAVE_LIBSODIUM -#include -#endif -#include "opensslsigners.hh" - -#include "dns.hh" -#include "dnsbackend.hh" -#include "ueberbackend.hh" -#include "dnspacket.hh" -#include "nameserver.hh" -#include "distributor.hh" -#include "logger.hh" -#include "arguments.hh" -#include "packethandler.hh" -#include "statbag.hh" -#include "tcpreceiver.hh" -#include "misc.hh" -#include "dynlistener.hh" -#include "dynhandler.hh" -#include "communicator.hh" -#include "dnsproxy.hh" -#include "utility.hh" -#include "common_startup.hh" -#include "dnsrecords.hh" -#include "version.hh" - -#ifdef HAVE_LUA_RECORDS -#include "minicurl.hh" -#endif /* HAVE_LUA_RECORDS */ - -time_t s_starttime; - -string s_programname="pdns"; // used in packethandler.cc - -const char *funnytext= -"*****************************************************************************\n"\ -"Ok, you just ran pdns_server through 'strings' hoping to find funny messages.\n"\ -"Well, you found one. \n"\ -"Two ions are flying through their particle accelerator, says the one to the\n" -"other 'I think I've lost an electron!' \n"\ -"So the other one says, 'Are you sure?'. 'YEAH! I'M POSITIVE!'\n"\ -" the pdns crew - pdns@powerdns.com\n" -"*****************************************************************************\n"; - - -// start (sys)logging - - -/** -\file receiver.cc -\brief The main loop of powerdns - -This file is where it all happens - main is here, as are the two pivotal threads qthread() and athread() -*/ - -static void daemonize() -{ - if(fork()) - exit(0); // bye bye - - setsid(); - - int i=open("/dev/null",O_RDWR); /* open stdin */ - if(i < 0) - g_log<&parts, pid_t ppid) -{ - kill(cpid, SIGKILL); // why? - kill(cpid, SIGKILL); // why? - sleep(1); - return "ok"; -} - -static string DLRestHandler(const vector&parts, pid_t ppid) -{ - string line; - - for(vector::const_iterator i=parts.begin();i!=parts.end();++i) { - if(i!=parts.begin()) - line.append(1,' '); - line.append(*i); - } - line.append(1,'\n'); - - std::lock_guard l(g_guardian_lock); - - try { - writen2(g_fd1[1],line.c_str(),line.size()+1); - } - catch(PDNSException &ae) { - return "Error communicating with instance: "+ae.reason; - } - char mesg[512]; - string response; - while(fgets(mesg,sizeof(mesg),g_fp)) { - if(*mesg=='\0') - break; - response+=mesg; - } - boost::trim_right(response); - return response; -} - - - -static int guardian(int argc, char **argv) -{ - if(isGuarded(argv)) - return 0; - - int infd=0, outfd=1; - - DynListener dlg(s_programname); - dlg.registerFunc("QUIT",&DLQuitHandler, "quit daemon"); - dlg.registerFunc("CYCLE",&DLCycleHandler, "restart instance"); - dlg.registerFunc("PING",&DLPingHandler, "ping guardian"); - dlg.registerFunc("STATUS",&DLStatusHandler, "get instance status from guardian"); - dlg.registerRestFunc(&DLRestHandler); - dlg.go(); - string progname=argv[0]; - - bool first=true; - cpid=0; - - g_guardian_lock.lock(); - - for(;;) { - int pid; - setStatus("Launching child"); - - if(pipe(g_fd1)<0 || pipe(g_fd2)<0) { - g_log<((progname+"-instance").c_str())); - for(n=1;n0) { // parent - close(g_fd1[0]); - close(g_fd2[1]); - - if(first) { - first=false; - signal(SIGTERM, takedown); - - signal(SIGHUP, SIG_IGN); - signal(SIGUSR1, SIG_IGN); - signal(SIGUSR2, SIG_IGN); - - writePid(); - } - g_guardian_lock.unlock(); - int status; - cpid=pid; - for(;;) { - int ret=waitpid(pid,&status,WNOHANG); - - if(ret<0) { - g_log< -static void tbhandler(int num) -{ - g_log<= 0) - g_log.setFacility(val); - else - g_log< modules; - - stringtok(modules,::arg()["load-modules"], ", "); - if (!UeberBackend::loadModules(modules, ::arg()["module-dir"])) { - exit(1); - } - } - - BackendMakers().launch(::arg()["launch"]); // vrooooom! - - if(!::arg().getCommands().empty()) { - cerr<<"Fatal: non-option"; - if (::arg().getCommands().size() > 1) { - cerr<<"s"; - } - cerr<<" ("; - bool first = true; - for (const auto& c : ::arg().getCommands()) { - if (!first) { - cerr<<", "; - } - first = false; - cerr<(); // listens on stdin - } - else { - g_log<(); - else - dl = std::make_unique(s_programname); - - writePid(); - } - DynListener::registerFunc("SHOW",&DLShowHandler, "show a specific statistic or * to get a list", ""); - DynListener::registerFunc("RPING",&DLPingHandler, "ping instance"); - DynListener::registerFunc("QUIT",&DLRQuitHandler, "quit daemon"); - DynListener::registerFunc("UPTIME",&DLUptimeHandler, "get instance uptime"); - DynListener::registerFunc("NOTIFY-HOST", &DLNotifyHostHandler, "notify host for specific zone", " "); - DynListener::registerFunc("NOTIFY", &DLNotifyHandler, "queue a notification", ""); - DynListener::registerFunc("RELOAD",&DLReloadHandler, "reload all zones"); - DynListener::registerFunc("REDISCOVER",&DLRediscoverHandler, "discover any new zones"); - DynListener::registerFunc("VERSION",&DLVersionHandler, "get instance version"); - DynListener::registerFunc("PURGE",&DLPurgeHandler, "purge entries from packet cache", "[]"); - DynListener::registerFunc("CCOUNTS",&DLCCHandler, "get cache statistics"); - DynListener::registerFunc("QTYPES", &DLQTypesHandler, "get QType statistics"); - DynListener::registerFunc("RESPSIZES", &DLRSizesHandler, "get histogram of response sizes"); - DynListener::registerFunc("REMOTES", &DLRemotesHandler, "get top remotes"); - DynListener::registerFunc("SET",&DLSettingsHandler, "set config variables", " "); - DynListener::registerFunc("RETRIEVE", &DLNotifyRetrieveHandler, "retrieve slave zone", " []"); - DynListener::registerFunc("CURRENT-CONFIG",&DLCurrentConfigHandler, "retrieve the current configuration", "[diff]"); - DynListener::registerFunc("LIST-ZONES", &DLListZones, "show list of zones", "[primary|secondary|native]"); - DynListener::registerFunc("TOKEN-LOGIN", &DLTokenLogin, "Login to a PKCS#11 token", " "); - DynListener::registerFunc("XFR-QUEUE", &DLSuckRequests, "Get all requests for XFR in queue"); - - if(!::arg()["tcp-control-address"].empty()) { - DynListener* dlTCP=new DynListener(ComboAddress(::arg()["tcp-control-address"], ::arg().asNum("tcp-control-port"))); - dlTCP->go(); - } - - // reparse, with error checking - if(!::arg().mustDo("no-config")) - ::arg().file(configname.c_str()); - ::arg().parse(argc,argv); - - if(::arg()["server-id"].empty()) { - char tmp[128]; - if(gethostname(tmp, sizeof(tmp)-1) == 0) { - ::arg().set("server-id")=tmp; - } else { - g_log<(); // this fails when we are not root, throws exception - g_udpReceivers.push_back(N); - - size_t rthreads = ::arg().asNum("receiver-threads", 1); - if (rthreads > 1 && N->canReusePort()) { - g_udpReceivers.resize(rthreads); - - for (size_t idx = 1; idx < rthreads; idx++) { - try { - g_udpReceivers[idx] = std::make_shared(true); - } - catch(const PDNSException& e) { - g_log<(); - } - catch(const ArgException &A) { - g_log< #include "zoneparser-tng.hh" -#include "common_startup.hh" +#include "auth-main.hh" #include "auth-caches.hh" #include "auth-zonecache.hh" #include "threadname.hh"