aboutsummaryrefslogtreecommitdiffstats
path: root/lualib
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2019-12-02 17:01:47 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2019-12-02 17:01:47 +0000
commit813496a14ddbdfc6c01be9e133476c63a8cbc3e6 (patch)
treedafda9ca646e3c492a6db906b071596a18ca280e /lualib
parent946c6d5af333c78914a37ada3ace683c1b782988 (diff)
downloadrspamd-813496a14ddbdfc6c01be9e133476c63a8cbc3e6.tar.gz
rspamd-813496a14ddbdfc6c01be9e133476c63a8cbc3e6.zip
[Minor] Add generic methods to deal with check_local/check_authed flags
Diffstat (limited to 'lualib')
-rw-r--r--lualib/lua_util.lua58
1 files changed, 58 insertions, 0 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua
index 207bdc0dc..b9334a1b3 100644
--- a/lualib/lua_util.lua
+++ b/lualib/lua_util.lua
@@ -1293,4 +1293,62 @@ exports.toboolean = function(v)
end
end
+---[[[
+-- @function lua_util.config_check_local_or_authed(config, modname)
+-- Reads check_local and check_authed from the config as this is used in many modules
+-- @param {rspamd_config} config `rspamd_config` global
+-- @param {name} module name
+-- @return {boolean} v converted to boolean
+--]]]
+exports.config_check_local_or_authed = function(rspamd_config, modname, def_local, def_authed)
+ local check_local = def_local or false
+ local check_authed = def_authed or false
+
+ local function try_section(where)
+ local ret = false
+ local opts = rspamd_config:get_all_opt(where)
+ if type(opts) == 'table' then
+ if type(opts['check_local']) == 'boolean' then
+ check_local = opts['check_local']
+ ret = true
+ end
+ if type(opts['check_authed']) == 'boolean' then
+ check_authed = opts['check_authed']
+ ret = true
+ end
+ end
+
+ return ret
+ end
+
+ if not try_section(modname) then
+ try_section('options')
+ end
+
+ return {check_local, check_authed}
+end
+
+---[[[
+-- @function lua_util.is_skip_local_or_authed(task, conf[, ip])
+-- Returns `true` if local or authenticated task should be skipped for this module
+-- @param {rspamd_task} task
+-- @param {table} conf table returned from `config_check_local_or_authed`
+-- @param {rspamd_ip} ip optional ip address (can be obtained from a task)
+-- @return {boolean} true if check should be skipped
+--]]]
+exports.is_skip_local_or_authed = function(task, conf, ip)
+ if not ip then
+ ip = task:get_from_ip()
+ end
+ if not conf then
+ conf = {false, false}
+ end
+ if ((not conf[2] and task:get_user()) or
+ (not conf[1] and ip and ip:is_local())) then
+ return true
+ end
+
+ return false
+end
+
return exports