From: Remi Gacogne Date: Mon, 18 Mar 2024 09:20:51 +0000 (+0100) Subject: dnsdist: Switch to `pdns::UniqueFilePtr` X-Git-Tag: rec-5.1.0-alpha1~117^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=931536a53367efbf9be3884b8970498aa92be774;p=thirdparty%2Fpdns.git dnsdist: Switch to `pdns::UniqueFilePtr` --- diff --git a/pdns/dnsdistdist/dnsdist-cache.cc b/pdns/dnsdistdist/dnsdist-cache.cc index fa17f0dbaa..d6323d8b32 100644 --- a/pdns/dnsdistdist/dnsdist-cache.cc +++ b/pdns/dnsdistdist/dnsdist-cache.cc @@ -483,7 +483,7 @@ uint64_t DNSDistPacketCache::getEntriesCount() uint64_t DNSDistPacketCache::dump(int fileDesc) { - auto filePtr = std::unique_ptr(fdopen(dup(fileDesc), "w"), fclose); + auto filePtr = pdns::UniqueFilePtr(fdopen(dup(fileDesc), "w")); if (filePtr == nullptr) { return 0; } diff --git a/pdns/dnsdistdist/dnsdist-lua-inspection.cc b/pdns/dnsdistdist/dnsdist-lua-inspection.cc index f5fdcca8a7..2f0e6fa37b 100644 --- a/pdns/dnsdistdist/dnsdist-lua-inspection.cc +++ b/pdns/dnsdistdist/dnsdist-lua-inspection.cc @@ -261,7 +261,7 @@ struct GrepQParams { boost::optional netmask; boost::optional name; - std::unique_ptr outputFile{nullptr, fclose}; + pdns::UniqueFilePtr outputFile{nullptr}; int msec = -1; }; @@ -277,7 +277,7 @@ static std::optional parseGrepQParams(const LuaTypeOrArrayOf(fdopen(fileDesc, "w"), fclose); + result.outputFile = pdns::UniqueFilePtr(fdopen(fileDesc, "w")); if (result.outputFile == nullptr) { g_outputBuffer = "Error opening dump file for writing: " + stringerror() + "\n"; close(fileDesc); diff --git a/pdns/dnsdistdist/doh.cc b/pdns/dnsdistdist/doh.cc index aecb627bdb..7497aa2b9d 100644 --- a/pdns/dnsdistdist/doh.cc +++ b/pdns/dnsdistdist/doh.cc @@ -160,7 +160,7 @@ public: std::map d_ocspResponses; std::unique_ptr d_ticketKeys{nullptr}; - std::unique_ptr d_keyLogFile{nullptr, fclose}; + pdns::UniqueFilePtr d_keyLogFile{nullptr}; ClientState* d_cs{nullptr}; time_t d_ticketsKeyRotationDelay{0}; diff --git a/pdns/libssl.cc b/pdns/libssl.cc index ca35757f0c..b2d848bdbe 100644 --- a/pdns/libssl.cc +++ b/pdns/libssl.cc @@ -471,18 +471,18 @@ bool libssl_generate_ocsp_response(const std::string& certFile, const std::strin { const EVP_MD* rmd = EVP_sha256(); - auto fp = std::unique_ptr(fopen(certFile.c_str(), "r"), fclose); + auto fp = pdns::UniqueFilePtr(fopen(certFile.c_str(), "r")); if (!fp) { throw std::runtime_error("Unable to open '" + certFile + "' when loading the certificate to generate an OCSP response"); } auto cert = std::unique_ptr(PEM_read_X509_AUX(fp.get(), nullptr, nullptr, nullptr), X509_free); - fp = std::unique_ptr(fopen(caCert.c_str(), "r"), fclose); + fp = pdns::UniqueFilePtr(fopen(caCert.c_str(), "r")); if (!fp) { throw std::runtime_error("Unable to open '" + caCert + "' when loading the issuer certificate to generate an OCSP response"); } auto issuer = std::unique_ptr(PEM_read_X509_AUX(fp.get(), nullptr, nullptr, nullptr), X509_free); - fp = std::unique_ptr(fopen(caKey.c_str(), "r"), fclose); + fp = pdns::UniqueFilePtr(fopen(caKey.c_str(), "r")); if (!fp) { throw std::runtime_error("Unable to open '" + caKey + "' when loading the issuer key to generate an OCSP response"); } @@ -939,7 +939,7 @@ std::pair, std::vector(fopen(pair.d_cert.c_str(), "r"), fclose); + auto fp = pdns::UniqueFilePtr(fopen(pair.d_cert.c_str(), "r")); if (!fp) { throw std::runtime_error("Unable to open file " + pair.d_cert); } @@ -1050,14 +1050,14 @@ static void libssl_key_log_file_callback(const SSL* ssl, const char* line) } #endif /* HAVE_SSL_CTX_SET_KEYLOG_CALLBACK */ -std::unique_ptr libssl_set_key_log_file(std::unique_ptr& ctx, const std::string& logFile) +pdns::UniqueFilePtr libssl_set_key_log_file(std::unique_ptr& ctx, const std::string& logFile) { #ifdef HAVE_SSL_CTX_SET_KEYLOG_CALLBACK int fd = open(logFile.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0600); if (fd == -1) { unixDie("Error opening TLS log file '" + logFile + "'"); } - auto fp = std::unique_ptr(fdopen(fd, "a"), fclose); + auto fp = pdns::UniqueFilePtr(fdopen(fd, "a")); if (!fp) { int error = errno; // close might clobber errno close(fd); @@ -1069,7 +1069,7 @@ std::unique_ptr libssl_set_key_log_file(std::unique_ptr(nullptr, fclose); + return pdns::UniqueFilePtr(nullptr); #endif /* HAVE_SSL_CTX_SET_KEYLOG_CALLBACK */ } diff --git a/pdns/libssl.hh b/pdns/libssl.hh index 327fed32a6..8dd7ff373b 100644 --- a/pdns/libssl.hh +++ b/pdns/libssl.hh @@ -12,6 +12,7 @@ #include "config.h" #include "circular_buffer.hh" #include "lock.hh" +#include "misc.hh" enum class LibsslTLSVersion : uint8_t { Unknown, TLS10, TLS11, TLS12, TLS13 }; @@ -154,7 +155,7 @@ bool libssl_set_min_tls_version(std::unique_ptr, std::vector> libssl_init_server_context(const TLSConfig& config, std::map& ocspResponses); -std::unique_ptr libssl_set_key_log_file(std::unique_ptr& ctx, const std::string& logFile); +pdns::UniqueFilePtr libssl_set_key_log_file(std::unique_ptr& ctx, const std::string& logFile); /* called in a client context, if the client advertised more than one ALPN values and the server returned more than one as well, to select the one to use. */ #ifndef DISABLE_NPN diff --git a/pdns/tcpiohandler.cc b/pdns/tcpiohandler.cc index b048c9d1b1..cf82471ba8 100644 --- a/pdns/tcpiohandler.cc +++ b/pdns/tcpiohandler.cc @@ -52,7 +52,7 @@ public: OpenSSLTLSTicketKeysRing d_ticketKeys; std::map d_ocspResponses; std::unique_ptr d_tlsCtx{nullptr, SSL_CTX_free}; - std::unique_ptr d_keyLogFile{nullptr, fclose}; + pdns::UniqueFilePtr d_keyLogFile{nullptr}; }; class OpenSSLSession : public TLSSession