commit d161714: [Feature] external_relay: add ip_map strategy
Soha Jin
soha at lohu.info
Tue Jul 11 18:56:04 UTC 2023
Author: Soha Jin
Date: 2023-07-11 20:28:07 +0800
URL: https://github.com/rspamd/rspamd/commit/d16171495fbb469b9cf4ccd171b92f5a9f331d9b (refs/pull/4537/head)
[Feature] external_relay: add ip_map strategy
---
src/plugins/lua/external_relay.lua | 42 ++++++++++++++++++++++
test/functional/cases/380_external_relay.robot | 7 ++++
test/functional/configs/maps/external_relay.ip_map | 2 ++
test/functional/configs/merged-local.conf | 4 +++
.../messages/{received6.eml => received7.eml} | 34 +++++++++---------
5 files changed, 72 insertions(+), 17 deletions(-)
diff --git a/src/plugins/lua/external_relay.lua b/src/plugins/lua/external_relay.lua
index 9f65490e9..9c0f06ff8 100644
--- a/src/plugins/lua/external_relay.lua
+++ b/src/plugins/lua/external_relay.lua
@@ -61,6 +61,12 @@ local config_schema = ts.shape{
strategy = 'hostname_map',
symbol = ts.string:is_optional(),
},
+ ts.shape{
+ ip_map = lua_maps.map_schema,
+ priority = ts.number:is_optional(),
+ strategy = 'ip_map',
+ symbol = ts.string:is_optional(),
+ },
}
),
}
@@ -182,6 +188,42 @@ strategies.hostname_map = function(rule)
end
end
+strategies.ip_map = function(rule)
+ local ip_map = lua_maps.map_add_from_ucl(rule.ip_map, 'radix', 'external relay IPs')
+ if not ip_map then
+ rspamd_logger.errx(rspamd_config, "couldn't add map %s; won't register symbol %s",
+ rule.ip_map, rule.symbol)
+ return
+ end
+
+ return function(task)
+ local from_ip = task:get_from_ip()
+ if not (from_ip and from_ip:is_valid()) then
+ lua_util.debugm(N, task, 'sender\'s IP is missing')
+ return
+ end
+
+ if not ip_map:get_key(from_ip) then
+ lua_util.debugm(N, task, 'sender\'s ip (%s) is not a relay', from_ip)
+ return
+ end
+
+ local rcvd_hdrs = task:get_received_headers()
+ local num_rcvd = #rcvd_hdrs
+ -- Try find sending IP in Received headers
+ for i, rcvd in ipairs(rcvd_hdrs) do
+ if rcvd.real_ip then
+ local rcvd_ip = rcvd.real_ip
+ if rcvd_ip:is_valid() and (not ip_map:get_key(rcvd_ip) or i == num_rcvd) then
+ return set_from_rcvd(task, rcvd)
+ end
+ end
+ end
+
+ rspamd_logger.errx(task, 'found nothing useful in Received headers')
+ end
+end
+
strategies['local'] = function(rule)
return function(task)
local from_ip = task:get_from_ip()
diff --git a/test/functional/cases/380_external_relay.robot b/test/functional/cases/380_external_relay.robot
index ff301626e..459aaac78 100644
--- a/test/functional/cases/380_external_relay.robot
+++ b/test/functional/cases/380_external_relay.robot
@@ -33,6 +33,13 @@ EXTERNAL RELAY HOSTNAME MAP
Expect Symbol With Exact Options EXTERNAL_RELAY_TEST
... IP=192.0.2.1 HOSTNAME=mail.example.org HELO=mail.example.org
+EXTERNAL RELAY IP MAP
+ Scan File ${RSPAMD_TESTDIR}/messages/received7.eml
+ ... Settings={symbols_enabled [EXTERNAL_RELAY_TEST, EXTERNAL_RELAY_IP_MAP]}
+ ... IP=198.51.100.1
+ Expect Symbol With Exact Options EXTERNAL_RELAY_TEST
+ ... IP=4.31.198.44 HOSTNAME=foobar.example.org HELO=foobar.example.org
+
EXTERNAL RELAY LOCAL
Scan File ${RSPAMD_TESTDIR}/messages/ham.eml
... Settings={symbols_enabled [EXTERNAL_RELAY_TEST, EXTERNAL_RELAY_LOCAL]}
diff --git a/test/functional/configs/maps/external_relay.ip_map b/test/functional/configs/maps/external_relay.ip_map
new file mode 100644
index 000000000..f5b106f69
--- /dev/null
+++ b/test/functional/configs/maps/external_relay.ip_map
@@ -0,0 +1,2 @@
+2001:db8::/32
+198.51.100.0/24
diff --git a/test/functional/configs/merged-local.conf b/test/functional/configs/merged-local.conf
index 2cf05bf13..27738dd5e 100644
--- a/test/functional/configs/merged-local.conf
+++ b/test/functional/configs/merged-local.conf
@@ -50,6 +50,10 @@ external_relay {
hostname_map = "{= env.TESTDIR =}/configs/maps/external_relay.hostname_map";
strategy = "hostname_map";
}
+ EXTERNAL_RELAY_IP_MAP {
+ ip_map = "{= env.TESTDIR =}/configs/maps/external_relay.ip_map";
+ strategy = "ip_map";
+ }
EXTERNAL_RELAY_LOCAL {
strategy = "local";
}
diff --git a/test/functional/messages/received6.eml b/test/functional/messages/received7.eml
similarity index 52%
copy from test/functional/messages/received6.eml
copy to test/functional/messages/received7.eml
index 38dd801c6..fa946ec1e 100644
--- a/test/functional/messages/received6.eml
+++ b/test/functional/messages/received7.eml
@@ -1,17 +1,17 @@
-Received: from localhost (localhost [127.0.0.1])
- by ietfa.amsl.com (Postfix) with ESMTPA id 00E7712024B
- for <cfrg at ietfa.amsl.com>; Tue, 7 May 2019 14:01:07 -0700 (PDT)
-Received: from cool.example.org ([4.31.198.44])
- by lame.example.net (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024)
- with ESMTPA id k8UsBTUjeiTe for <cfrg at ietfa.amsl.com>;
- Tue, 7 May 2019 14:01:04 -0700 (PDT)
-Received: from mail.example.org ([192.0.3.1])
- by localhost (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024)
- with ESMTP id k8UsBTUjeiTe for <cfrg at ietfa.amsl.com>;
- Tue, 7 May 2019 14:01:04 -0700 (PDT)
-Received: from mail.example.org ([192.0.2.1])
- by cool.example.org (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024)
- with ESMTP id k8UsBTUjeiTe for <cfrg at ietfa.amsl.com>;
- Tue, 7 May 2019 14:01:04 -0700 (PDT)
-
-aa
+Received: from localhost (localhost [2001:db8:114::514])
+ by ietfa.amsl.com (Postfix) with ESMTP id 00E7712024B
+ for <cfrg at ietfa.amsl.com>; Tue, 7 May 2019 14:01:07 -0700 (PDT)
+Received: from mail.example.org ([198.51.100.233])
+ by lame.example.net (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024)
+ with ESMTP id k8UsBTUjeiTe for <cfrg at ietfa.amsl.com>;
+ Tue, 7 May 2019 14:01:04 -0700 (PDT)
+Received: from foobar.example.org ([4.31.198.44])
+ by localhost (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024)
+ with ESMTP id k8UsBTUjeiTe for <cfrg at ietfa.amsl.com>;
+ Tue, 7 May 2019 14:01:04 -0700 (PDT)
+Received: from mail.example.org ([198.51.100.2])
+ by foobar.example.org (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024)
+ with ESMTP id k8UsBTUjeiTe for <cfrg at ietfa.amsl.com>;
+ Tue, 7 May 2019 14:01:04 -0700 (PDT)
+
+haha
More information about the Commits
mailing list