Skip to content

Instantly share code, notes, and snippets.

@zerwes
Created May 11, 2022 03:05
Show Gist options
  • Save zerwes/c655820612613ccb2293b03b4d0574e9 to your computer and use it in GitHub Desktop.
Save zerwes/c655820612613ccb2293b03b4d0574e9 to your computer and use it in GitHub Desktop.
Rspamd: Simple per user Whitelist / Blacklist
-- When using Whitelist and Blacklist last result wins!
-- So maybe redis latency will decide ;)
rspamd_config.PER_USER_WL = {
-- CAUTION: This will whitelist the mail for every recipient when there is
-- match for any recipient
-- ADD a WL entry with: SADD [email protected] [email protected]
-- This function searches redis like this:
-- "SSCAN" "[email protected]" "0" "MATCH" "[email protected]"
callback = function(task)
local rspamd_logger = require "rspamd_logger"
local lua_redis = require "lua_redis"
local redis_params = lua_redis.parse_redis_server('multimaps')
local rcpts = task:get_recipients()
local sender = task:get_from() -- Normally SMTP From
--local sender = task:get_from('mime') -- Header From
local sender_address = sender[1]['addr']
local prefix = 'RS_UWL_'
local function redis_get_cb(err, data)
if err ~=nil then
rspamd_logger.errx(task, 'redis_get_cb received error: %1', err)
end
if #data[2] > 0 then
task:set_pre_result('accept', 'ACCEPT by Per user Whitelist.')
return true
end
end
for _,rcpt in ipairs(rcpts) do
local ret = lua_redis.redis_make_request(task,
redis_params, -- connect params
nil, -- hash key
false, -- is write
redis_get_cb, --callback
'SSCAN', -- command
{ prefix..rcpt['addr'], 0, 'MATCH', sender_address, 'COUNT', '1000' } -- arguments
)
end
return false
end,
score = 1.0,
group = 'per_user_config',
description = 'Per user sender address whitelist',
type = prefilter,
priority = 2
}
rspamd_config.PER_USER_BL = {
-- CAUTION: This will REJECT the mail for every recipient when there is
-- match for any recipient
-- ADD a BL entry with: SADD [email protected] [email protected]
-- This function searches redis like this:
-- "SSCAN" "[email protected]" "0" "MATCH" "[email protected]"
callback = function(task)
local rspamd_logger = require "rspamd_logger"
local lua_redis = require "lua_redis"
local redis_params = lua_redis.parse_redis_server('multimaps')
local rcpts = task:get_recipients()
local sender = task:get_from() -- Normally SMTP From
--local sender = task:get_from('mime') -- Header From
local sender_address = sender[1]['addr']
local prefix = 'RS_UBL_'
local function redis_get_cb(err, data)
if err ~=nil then
rspamd_logger.errx(task, 'redis_get_cb received error: %1', err)
end
if #data[2] > 0 then
task:set_pre_result('reject', 'REJECT by Per user Blacklist.')
return true
end
end
for _,rcpt in ipairs(rcpts) do
local ret = lua_redis.redis_make_request(task,
redis_params, -- connect params
nil, -- hash key
false, -- is write
redis_get_cb, --callback
'SSCAN', -- command
{ prefix..rcpt['addr'], 0, 'MATCH', sender_address } -- arguments
)
end
return false
end,
score = 1.0,
group = 'per_user_config',
description = 'Per user sender address blacklist',
type = prefilter,
priority = 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment