]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Add an (optional) table of domains to addProxyMapping().
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 5 Apr 2022 08:01:19 +0000 (10:01 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 5 Apr 2022 08:15:24 +0000 (10:15 +0200)
The table is used to construct a DNSSuffixMatchGroup. Only apply mapping
if the qname in the query matches the DNSSuffixMatchGroup.

pdns/pdns_recursor.cc
pdns/rec-lua-conf.cc
pdns/rec-lua-conf.hh
pdns/recursordist/rec-tcp.cc

index 89f03882dbf6c7142e4267043452f4851de6d95a..7eff2aa6e810c3a9e3c801c7377137f44b08702d 100644 (file)
@@ -925,7 +925,23 @@ void startDoResolve(void* p)
 #ifdef HAVE_FSTRM
     sr.setFrameStreamServers(t_frameStreamServers);
 #endif
-    sr.setQuerySource(dc->d_mappedSource, g_useIncomingECS && !dc->d_ednssubnet.source.empty() ? boost::optional<const EDNSSubnetOpts&>(dc->d_ednssubnet) : boost::none);
+    bool useMapped = true;
+    // If proxy by table is active and had a match, we only want to use the mapped address if it also has a domain match
+    // (if a domain suffix match table is present in the config)
+    if (t_proxyMapping && dc->d_source != dc->d_mappedSource) {
+      if (auto it = t_proxyMapping->lookup(dc->d_source)) {
+        if (it->second.suffixMatchNode) {
+          if (!it->second.suffixMatchNode->check(dc->d_mdp.d_qname)) {
+            // No match in domains, use original source
+            useMapped = false;
+          }
+        }
+        // No suffix match node defined, use mapped address
+      }
+      // lookup failing cannot happen as dc->d_source != dc->d_mappedSource
+    }
+    sr.setQuerySource(useMapped ? dc->d_mappedSource : dc->d_source, g_useIncomingECS && !dc->d_ednssubnet.source.empty() ? boost::optional<const EDNSSubnetOpts&>(dc->d_ednssubnet) : boost::none);
+
     sr.setQueryReceivedOverTCP(dc->d_tcp);
 
     bool tracedQuery = false; // we could consider letting Lua know about this too
@@ -2151,7 +2167,7 @@ static void handleNewUDPQuestion(int fd, FDMultiplexer::funcparam_t& var)
       ComboAddress mappedSource = source;
       if (t_proxyMapping) {
         if (auto it = t_proxyMapping->lookup(source)) {
-          mappedSource = it->second;
+          mappedSource = it->second.address;
         }
       }
       if (t_remotes) {
index 70d2017175be526d1eafdaa440714a7cbfb43a89..305afe588a03ea0633531b7272d8b2713fa66673 100644 (file)
@@ -725,11 +725,18 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de
     lci.allowAdditionalQTypes.insert_or_assign(qtype, pair(targets, mode));
   });
 
-  Lua->writeFunction("addProxyMapping", [&proxyMapping](const string& netmaskArg, const string& addressArg) {
+  Lua->writeFunction("addProxyMapping", [&proxyMapping](const string& netmaskArg, const string& addressArg, boost::optional<std::vector<pair<int,std::string>>> smnStrings) {
     try {
       Netmask netmask(netmaskArg);
       ComboAddress address(addressArg);
-      proxyMapping.insert_or_assign(netmask, address);
+      boost::optional<SuffixMatchNode> smn;
+      if (smnStrings) {
+        smn = boost::make_optional(SuffixMatchNode{});
+        for (const auto& el : *smnStrings) {
+          smn->add(el.second);
+        }
+      }
+      proxyMapping.insert_or_assign(netmask, {address, smn});
     }
     catch (std::exception& e) {
       g_log << Logger::Error << "Error processing addProxyMapping: " << e.what() << endl;
index 482ffaa49972d04b15d0225656a620430588ec67..33faf1d101f6af047c165ab4ed8a5fee16e6f523 100644 (file)
@@ -72,7 +72,13 @@ enum class AdditionalMode : uint8_t
   ResolveDeferred
 };
 
-using ProxyMapping = NetmaskTree<ComboAddress, Netmask>;
+struct ProxyByTableValue
+{
+  ComboAddress address;
+  boost::optional<SuffixMatchNode> suffixMatchNode;
+};
+
+using ProxyMapping = NetmaskTree<ProxyByTableValue, Netmask>;
 
 class LuaConfigItems
 {
index 406d9df933c9e408acf10e293b8aaf0136bd0e83..c4723653063ea72ff02f1e7d38db6f00e3921160 100644 (file)
@@ -263,7 +263,7 @@ static void handleRunningTCPQuestion(int fd, FDMultiplexer::funcparam_t& var)
       conn->d_mappedSource = conn->d_source;
       if (t_proxyMapping) {
         if (auto it = t_proxyMapping->lookup(conn->d_source)) {
-          conn->d_mappedSource = it->second;
+          conn->d_mappedSource = it->second.address;
         }
       }
       if (t_allowFrom && !t_allowFrom->match(&conn->d_mappedSource)) {
@@ -625,7 +625,7 @@ void handleNewTCPQuestion(int fd, FDMultiplexer::funcparam_t&)
     ComboAddress mappedSource = addr;
     if (!fromProxyProtocolSource && t_proxyMapping) {
       if (auto it = t_proxyMapping->lookup(addr)) {
-        mappedSource = it->second;
+        mappedSource = it->second.address;
       }
     }
     if (!fromProxyProtocolSource && t_allowFrom && !t_allowFrom->match(&mappedSource)) {