]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Move more DNSCrypt bindings into the right Lua file
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 12 Nov 2021 10:56:50 +0000 (11:56 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 22 Dec 2021 08:30:44 +0000 (09:30 +0100)
pdns/dnsdist-lua.cc
pdns/dnsdist-lua.hh
pdns/dnsdistdist/dnsdist-lua-bindings-dnscrypt.cc

index 768a8983b29483284172f20359f58aed4fcd3db7..e37b998c6bc138aace267342ada2f5322870cb30 100644 (file)
@@ -1638,87 +1638,6 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
     return g_dnsCryptLocals.size();
   });
 
-  luaCtx.writeFunction("generateDNSCryptProviderKeys", [client](const std::string& publicKeyFile, const std::string privateKeyFile) {
-    setLuaNoSideEffect();
-#ifdef HAVE_DNSCRYPT
-    if (client) {
-      return;
-    }
-    unsigned char publicKey[DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE];
-    unsigned char privateKey[DNSCRYPT_PROVIDER_PRIVATE_KEY_SIZE];
-    sodium_mlock(privateKey, sizeof(privateKey));
-
-    try {
-      DNSCryptContext::generateProviderKeys(publicKey, privateKey);
-
-      ofstream pubKStream(publicKeyFile);
-      pubKStream.write((char*)publicKey, sizeof(publicKey));
-      pubKStream.close();
-
-      ofstream privKStream(privateKeyFile);
-      privKStream.write((char*)privateKey, sizeof(privateKey));
-      privKStream.close();
-
-      g_outputBuffer = "Provider fingerprint is: " + DNSCryptContext::getProviderFingerprint(publicKey) + "\n";
-    }
-    catch (std::exception& e) {
-      errlog(e.what());
-      g_outputBuffer = "Error: " + string(e.what()) + "\n";
-    }
-
-    sodium_memzero(privateKey, sizeof(privateKey));
-    sodium_munlock(privateKey, sizeof(privateKey));
-#else
-      g_outputBuffer = "Error: DNSCrypt support is not enabled.\n";
-#endif
-  });
-
-  luaCtx.writeFunction("printDNSCryptProviderFingerprint", [](const std::string& publicKeyFile) {
-    setLuaNoSideEffect();
-#ifdef HAVE_DNSCRYPT
-    unsigned char publicKey[DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE];
-
-    try {
-      ifstream file(publicKeyFile);
-      file.read((char*)&publicKey, sizeof(publicKey));
-
-      if (file.fail())
-        throw std::runtime_error("Invalid dnscrypt provider public key file " + publicKeyFile);
-
-      file.close();
-      g_outputBuffer = "Provider fingerprint is: " + DNSCryptContext::getProviderFingerprint(publicKey) + "\n";
-    }
-    catch (std::exception& e) {
-      errlog(e.what());
-      g_outputBuffer = "Error: " + string(e.what()) + "\n";
-    }
-#else
-      g_outputBuffer = "Error: DNSCrypt support is not enabled.\n";
-#endif
-  });
-
-#ifdef HAVE_DNSCRYPT
-  luaCtx.writeFunction("generateDNSCryptCertificate", [client](const std::string& providerPrivateKeyFile, const std::string& certificateFile, const std::string privateKeyFile, uint32_t serial, time_t begin, time_t end, boost::optional<DNSCryptExchangeVersion> version) {
-    setLuaNoSideEffect();
-    if (client) {
-      return;
-    }
-    DNSCryptPrivateKey privateKey;
-    DNSCryptCert cert;
-
-    try {
-      if (generateDNSCryptCertificate(providerPrivateKeyFile, serial, begin, end, version ? *version : DNSCryptExchangeVersion::VERSION1, cert, privateKey)) {
-        privateKey.saveToFile(privateKeyFile);
-        DNSCryptContext::saveCertFromFile(cert, certificateFile);
-      }
-    }
-    catch (const std::exception& e) {
-      errlog(e.what());
-      g_outputBuffer = "Error: " + string(e.what()) + "\n";
-    }
-  });
-#endif
-
   luaCtx.writeFunction("showPools", []() {
     setLuaNoSideEffect();
     try {
@@ -2875,7 +2794,7 @@ vector<std::function<void(void)>> setupLua(LuaContext& luaCtx, bool client, bool
   setupLuaActions(luaCtx);
   setupLuaConfig(luaCtx, client, configCheck);
   setupLuaBindings(luaCtx, client);
-  setupLuaBindingsDNSCrypt(luaCtx);
+  setupLuaBindingsDNSCrypt(luaCtx, client);
   setupLuaBindingsDNSQuestion(luaCtx);
   setupLuaBindingsKVS(luaCtx, client);
   setupLuaBindingsPacketCache(luaCtx, client);
index 8f6157b532736888c79512db7478214f4719293f..9611963c06b6e830adc69d2aa7b0b7f7c65f31e2 100644 (file)
@@ -139,7 +139,7 @@ typedef NetmaskTree<DynBlock, AddressAndPortRange> nmts_t;
 vector<std::function<void(void)>> setupLua(LuaContext& luaCtx, bool client, bool configCheck, const std::string& config);
 void setupLuaActions(LuaContext& luaCtx);
 void setupLuaBindings(LuaContext& luaCtx, bool client);
-void setupLuaBindingsDNSCrypt(LuaContext& luaCtx);
+void setupLuaBindingsDNSCrypt(LuaContext& luaCtx, bool client);
 void setupLuaBindingsDNSQuestion(LuaContext& luaCtx);
 void setupLuaBindingsKVS(LuaContext& luaCtx, bool client);
 void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client);
index 9ea3fc87c349010fa63f5e89e455d4657e0e95f5..8de02805e9f14ea986ac37a3152bed39c6d30cd6 100644 (file)
@@ -26,7 +26,7 @@
 
 #include "dolog.hh"
 
-void setupLuaBindingsDNSCrypt(LuaContext& luaCtx)
+void setupLuaBindingsDNSCrypt(LuaContext& luaCtx, bool client)
 {
 #ifdef HAVE_DNSCRYPT
     /* DNSCryptContext bindings */
@@ -151,5 +151,85 @@ void setupLuaBindingsDNSCrypt(LuaContext& luaCtx)
     luaCtx.registerFunction<uint32_t(DNSCryptCert::*)()const>("getSerial", [](const DNSCryptCert& cert) { return cert.getSerial(); });
     luaCtx.registerFunction<uint32_t(DNSCryptCert::*)()const>("getTSStart", [](const DNSCryptCert& cert) { return ntohl(cert.getTSStart()); });
     luaCtx.registerFunction<uint32_t(DNSCryptCert::*)()const>("getTSEnd", [](const DNSCryptCert& cert) { return ntohl(cert.getTSEnd()); });
+
+    luaCtx.writeFunction("generateDNSCryptCertificate", [client](const std::string& providerPrivateKeyFile, const std::string& certificateFile, const std::string privateKeyFile, uint32_t serial, time_t begin, time_t end, boost::optional<DNSCryptExchangeVersion> version) {
+      setLuaNoSideEffect();
+      if (client) {
+        return;
+      }
+      DNSCryptPrivateKey privateKey;
+      DNSCryptCert cert;
+
+      try {
+        if (generateDNSCryptCertificate(providerPrivateKeyFile, serial, begin, end, version ? *version : DNSCryptExchangeVersion::VERSION1, cert, privateKey)) {
+          privateKey.saveToFile(privateKeyFile);
+          DNSCryptContext::saveCertFromFile(cert, certificateFile);
+        }
+      }
+      catch (const std::exception& e) {
+        errlog(e.what());
+        g_outputBuffer = "Error: " + string(e.what()) + "\n";
+      }
+    });
 #endif
+
+    luaCtx.writeFunction("generateDNSCryptProviderKeys", [client](const std::string& publicKeyFile, const std::string privateKeyFile) {
+      setLuaNoSideEffect();
+#ifdef HAVE_DNSCRYPT
+      if (client) {
+        return;
+      }
+      unsigned char publicKey[DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE];
+      unsigned char privateKey[DNSCRYPT_PROVIDER_PRIVATE_KEY_SIZE];
+      sodium_mlock(privateKey, sizeof(privateKey));
+
+      try {
+        DNSCryptContext::generateProviderKeys(publicKey, privateKey);
+
+        ofstream pubKStream(publicKeyFile);
+        pubKStream.write(reinterpret_cast<char*>(publicKey), sizeof(publicKey));
+        pubKStream.close();
+
+        ofstream privKStream(privateKeyFile);
+        privKStream.write(reinterpret_cast<char*>(privateKey), sizeof(privateKey));
+        privKStream.close();
+
+        g_outputBuffer = "Provider fingerprint is: " + DNSCryptContext::getProviderFingerprint(publicKey) + "\n";
+      }
+      catch (const std::exception& e) {
+        errlog(e.what());
+        g_outputBuffer = "Error: " + string(e.what()) + "\n";
+      }
+
+      sodium_memzero(privateKey, sizeof(privateKey));
+      sodium_munlock(privateKey, sizeof(privateKey));
+#else
+      g_outputBuffer = "Error: DNSCrypt support is not enabled.\n";
+#endif
+    });
+
+    luaCtx.writeFunction("printDNSCryptProviderFingerprint", [](const std::string& publicKeyFile) {
+      setLuaNoSideEffect();
+#ifdef HAVE_DNSCRYPT
+      unsigned char publicKey[DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE];
+
+      try {
+        ifstream file(publicKeyFile);
+        file.read(reinterpret_cast<char*>(&publicKey), sizeof(publicKey));
+
+        if (file.fail()) {
+          throw std::runtime_error("Invalid dnscrypt provider public key file " + publicKeyFile);
+        }
+
+        file.close();
+        g_outputBuffer = "Provider fingerprint is: " + DNSCryptContext::getProviderFingerprint(publicKey) + "\n";
+      }
+      catch (const std::exception& e) {
+        errlog(e.what());
+        g_outputBuffer = "Error: " + string(e.what()) + "\n";
+      }
+#else
+      g_outputBuffer = "Error: DNSCrypt support is not enabled.\n";
+#endif
+    });
 }