]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Switch to `pdns::UniqueFilePtr`
authorRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 18 Mar 2024 09:20:51 +0000 (10:20 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 18 Mar 2024 09:20:51 +0000 (10:20 +0100)
pdns/dnsdistdist/dnsdist-cache.cc
pdns/dnsdistdist/dnsdist-lua-inspection.cc
pdns/dnsdistdist/doh.cc
pdns/libssl.cc
pdns/libssl.hh
pdns/tcpiohandler.cc

index fa17f0dbaa0655cba025962427b463de79068d76..d6323d8b3224844bc78f0eeec935acaad077344e 100644 (file)
@@ -483,7 +483,7 @@ uint64_t DNSDistPacketCache::getEntriesCount()
 
 uint64_t DNSDistPacketCache::dump(int fileDesc)
 {
-  auto filePtr = std::unique_ptr<FILE, int (*)(FILE*)>(fdopen(dup(fileDesc), "w"), fclose);
+  auto filePtr = pdns::UniqueFilePtr(fdopen(dup(fileDesc), "w"));
   if (filePtr == nullptr) {
     return 0;
   }
index f5fdcca8a70b2f793fc55f36b8a2166edc4fc192..2f0e6fa37b727ae8c2b9962310e18cdaa56653cb 100644 (file)
@@ -261,7 +261,7 @@ struct GrepQParams
 {
   boost::optional<Netmask> netmask;
   boost::optional<DNSName> name;
-  std::unique_ptr<FILE, decltype(&fclose)> outputFile{nullptr, fclose};
+  pdns::UniqueFilePtr outputFile{nullptr};
   int msec = -1;
 };
 
@@ -277,7 +277,7 @@ static std::optional<GrepQParams> parseGrepQParams(const LuaTypeOrArrayOf<std::s
         g_outputBuffer = "Error opening dump file for writing: " + stringerror() + "\n";
         return std::nullopt;
       }
-      result.outputFile = std::unique_ptr<FILE, decltype(&fclose)>(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);
index aecb627bdbd6ecc0d1e8484bab81382456fc3e75..7497aa2b9d4bc988b1de8effeb716aee33b9b626 100644 (file)
@@ -160,7 +160,7 @@ public:
 
   std::map<int, std::string> d_ocspResponses;
   std::unique_ptr<OpenSSLTLSTicketKeysRing> d_ticketKeys{nullptr};
-  std::unique_ptr<FILE, int(*)(FILE*)> d_keyLogFile{nullptr, fclose};
+  pdns::UniqueFilePtr d_keyLogFile{nullptr};
   ClientState* d_cs{nullptr};
   time_t d_ticketsKeyRotationDelay{0};
 
index ca35757f0c2ede7a21c55b00dd78eeca5de9110a..b2d848bdbec8f44df5fc369aab807337d95986a9 100644 (file)
@@ -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<FILE, int(*)(FILE*)>(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<X509, void(*)(X509*)>(PEM_read_X509_AUX(fp.get(), nullptr, nullptr, nullptr), X509_free);
 
-  fp = std::unique_ptr<FILE, int(*)(FILE*)>(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<X509, void(*)(X509*)>(PEM_read_X509_AUX(fp.get(), nullptr, nullptr, nullptr), X509_free);
-  fp = std::unique_ptr<FILE, int(*)(FILE*)>(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::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>, std::vector<std::st
     if (!pair.d_key) {
 #if defined(HAVE_SSL_CTX_USE_CERT_AND_KEY)
       // If no separate key is given, treat it as a pkcs12 file
-      auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(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<FILE, int(*)(FILE*)> libssl_set_key_log_file(std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>& ctx, const std::string& logFile)
+pdns::UniqueFilePtr libssl_set_key_log_file(std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>& 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<FILE, int(*)(FILE*)>(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<FILE, int(*)(FILE*)> libssl_set_key_log_file(std::unique_ptr<SSL
 
   return fp;
 #else
-  return std::unique_ptr<FILE, int(*)(FILE*)>(nullptr, fclose);
+  return pdns::UniqueFilePtr(nullptr);
 #endif /* HAVE_SSL_CTX_SET_KEYLOG_CALLBACK */
 }
 
index 327fed32a67756300f5b4c904d68f02f87fda528..8dd7ff373bf6b49546a225655239ff39aa3567c7 100644 (file)
@@ -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<SSL_CTX, decltype(&SSL_CTX_free)
 std::pair<std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>, std::vector<std::string>> libssl_init_server_context(const TLSConfig& config,
                                                                                                             std::map<int, std::string>& ocspResponses);
 
-std::unique_ptr<FILE, int(*)(FILE*)> libssl_set_key_log_file(std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>& ctx, const std::string& logFile);
+pdns::UniqueFilePtr libssl_set_key_log_file(std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>& 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
index b048c9d1b12405474f7a3bd79e320d1da0cc8ced..cf82471ba84dfc36fde17bb78af544533769a952 100644 (file)
@@ -52,7 +52,7 @@ public:
   OpenSSLTLSTicketKeysRing d_ticketKeys;
   std::map<int, std::string> d_ocspResponses;
   std::unique_ptr<SSL_CTX, void(*)(SSL_CTX*)> d_tlsCtx{nullptr, SSL_CTX_free};
-  std::unique_ptr<FILE, int(*)(FILE*)> d_keyLogFile{nullptr, fclose};
+  pdns::UniqueFilePtr d_keyLogFile{nullptr};
 };
 
 class OpenSSLSession : public TLSSession