From: Remi Gacogne Date: Tue, 1 Aug 2023 11:54:30 +0000 (+0200) Subject: ixfrdist: Ensure exceptions are properly caught X-Git-Tag: rec-5.0.0-alpha1~77^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32b355c43a2dce77e500f02264b7a4c38fcd4d69;p=thirdparty%2Fpdns.git ixfrdist: Ensure exceptions are properly caught Reported by Coverity as CIDs 1504095, 1504097, 1504109, 1504112, 1504113, 1504114, 1504137 and 1504138. --- diff --git a/pdns/ixfrdist.cc b/pdns/ixfrdist.cc index 81d7b03cef..d4d319927d 100644 --- a/pdns/ixfrdist.cc +++ b/pdns/ixfrdist.cc @@ -1186,6 +1186,8 @@ int main(int argc, char** argv) { g_log.disableSyslog(true); g_log.setTimestamps(false); po::variables_map g_vm; + std::string configPath; + try { po::options_description desc("IXFR distribution tool"); desc.add_options() @@ -1208,10 +1210,17 @@ int main(int argc, char** argv) { cout<<"ixfrdist "<(); + } + catch (const po::error &e) { g_log<(), config)) { - // parseAndCheckConfig already logged whatever was wrong - return EXIT_FAILURE; - } + try { + YAML::Node config; + if (!parseAndCheckConfig(configPath, config)) { + // parseAndCheckConfig already logged whatever was wrong + return EXIT_FAILURE; + } /* From hereon out, we known that all the values in config are valid. */ - for (auto const &domain : config["domains"]) { - set s; - s.insert(domain["master"].as()); - g_domainConfigs[domain["domain"].as()].masters = s; - if (domain["max-soa-refresh"]) { - g_domainConfigs[domain["domain"].as()].maxSOARefresh = domain["max-soa-refresh"].as(); + for (auto const &domain : config["domains"]) { + set s; + s.insert(domain["master"].as()); + g_domainConfigs[domain["domain"].as()].masters = s; + if (domain["max-soa-refresh"]) { + g_domainConfigs[domain["domain"].as()].maxSOARefresh = domain["max-soa-refresh"].as(); + } + g_stats.registerDomain(domain["domain"].as()); + } + + for (const auto &addr : config["acl"].as>()) { + try { + g_acl.addMask(addr); + } + catch (const std::exception& exp) { + g_log<()); - } - for (const auto &addr : config["acl"].as>()) { try { - g_acl.addMask(addr); - } catch (const NetmaskException &e) { - g_log<(); - if (g_compress) { - g_log<(); + if (g_compress) { + g_log< allSockets; - for (const auto& addr : config["listen"].as>()) { - for (const auto& stype : {SOCK_DGRAM, SOCK_STREAM}) { - try { - int s = SSocket(addr.sin4.sin_family, stype, 0); - setNonBlocking(s); - setReuseAddr(s); - SBind(s, addr); - if (stype == SOCK_STREAM) { - SListen(s, 30); // TODO make this configurable + set allSockets; + for (const auto& addr : config["listen"].as>()) { + for (const auto& stype : {SOCK_DGRAM, SOCK_STREAM}) { + try { + int s = SSocket(addr.sin4.sin_family, stype, 0); + setNonBlocking(s); + setReuseAddr(s); + SBind(s, addr); + if (stype == SOCK_STREAM) { + SListen(s, 30); // TODO make this configurable + } + fdm->addReadFD(s, stype == SOCK_DGRAM ? handleUDPRequest : handleTCPRequest); + allSockets.insert(s); + } + catch (const runtime_error& exp) { + g_log<addReadFD(s, stype == SOCK_DGRAM ? handleUDPRequest : handleTCPRequest); - allSockets.insert(s); - } catch(runtime_error &e) { - g_log<(); - if (!(newgid = atoi(gid.c_str()))) { - struct group *gr = getgrnam(gid.c_str()); - if (gr == nullptr) { - g_log<(); + if (!(newgid = atoi(gid.c_str()))) { + struct group *gr = getgrnam(gid.c_str()); + if (gr == nullptr) { + g_log<gr_gid; + } + } + g_log<gr_gid; } } - g_log<>()) { - wsACL.addMask(acl); + if (config["webserver-address"]) { + NetmaskGroup wsACL; + try { + wsACL.addMask("127.0.0.0/8"); + wsACL.addMask("::1/128"); + + if (config["webserver-acl"]) { + wsACL.clear(); + for (const auto &acl : config["webserver-acl"].as>()) { + wsACL.addMask(acl); + } + } + } + catch (const NetmaskException& ne) { + g_log<(); - } + string loglevel = "normal"; + if (config["webserver-loglevel"]) { + loglevel = config["webserver-loglevel"].as(); + } - // Launch the webserver! - try { - std::thread(&IXFRDistWebServer::go, IXFRDistWebServer(config["webserver-address"].as(), wsACL, loglevel)).detach(); - } catch (const PDNSException &e) { - g_log<(), wsACL, loglevel)).detach(); + } + catch (const std::exception& exp) { + g_log<(); + if (!(newuid = atoi(uid.c_str()))) { + struct passwd *pw = getpwnam(uid.c_str()); + if (pw == nullptr) { + g_log<pw_uid; + } + } - if (config["uid"]) { - string uid = config["uid"].as(); - if (!(newuid = atoi(uid.c_str()))) { - struct passwd *pw = getpwnam(uid.c_str()); + struct passwd *pw = getpwuid(newuid); if (pw == nullptr) { - g_log<pw_uid; + if (initgroups(pw->pw_name, newgid) < 0) { + g_log<pw_name, newgid) < 0) { - g_log<(), - config["keep"].as(), - config["axfr-timeout"].as(), - config["failed-soa-retry"].as(), - config["axfr-max-records"].as()); - vector tcpHandlers; - tcpHandlers.reserve(config["tcp-in-threads"].as()); - for (size_t i = 0; i < tcpHandlers.capacity(); ++i) { - tcpHandlers.push_back(std::thread(tcpWorker, i)); - } + // It all starts here + signal(SIGTERM, handleSignal); + signal(SIGINT, handleSignal); + signal(SIGPIPE, SIG_IGN); + + // Init the things we need + reportAllTypes(); + + std::thread ut(updateThread, + config["work-dir"].as(), + config["keep"].as(), + config["axfr-timeout"].as(), + config["failed-soa-retry"].as(), + config["axfr-max-records"].as()); + + vector tcpHandlers; + tcpHandlers.reserve(config["tcp-in-threads"].as()); + for (size_t i = 0; i < tcpHandlers.capacity(); ++i) { + tcpHandlers.push_back(std::thread(tcpWorker, i)); + } - struct timeval now; - for(;;) { - gettimeofday(&now, 0); - fdm->run(&now); - if (g_exiting) { - g_log<run(&now); + if (g_exiting) { + g_log<