Browse Source

[Feature] Allow to use global lua maps in settings

Issue: #1802
tags/1.7.0
Vsevolod Stakhov 6 years ago
parent
commit
dc3fd1d5f9
2 changed files with 74 additions and 31 deletions
  1. 31
    0
      lualib/maps.lua
  2. 43
    31
      src/plugins/lua/settings.lua

+ 31
- 0
lualib/maps.lua View File



exports.rspamd_map_add = rspamd_map_add exports.rspamd_map_add = rspamd_map_add


-- Check `what` for being lua_map name, otherwise just compares key with what
local function rspamd_maybe_check_map(key, what)
local fun = require "fun"

local function starts(where,st)
return string.sub(where,1,string.len(st))==st
end

if type(what) == "table" then
return fun.any(function(elt) return rspamd_maybe_check_map(key, elt) end, what)
end
if type(rspamd_maps) == "table" then
local mn
if starts(what, "map:") then
mn = string.sub(what, 4)
elseif starts(what, "map://") then
mn = string.sub(what, 6)
end

if mn and rspamd_maps[mn] then
return rspamd_maps[mn]:get_key(key)
end
else
return what:lower() == key
end

return false
end

exports.rspamd_maybe_check_map = rspamd_maybe_check_map

return exports return exports

+ 43
- 31
src/plugins/lua/settings.lua View File

-- https://rspamd.com/doc/configuration/settings.html -- https://rspamd.com/doc/configuration/settings.html


local rspamd_logger = require "rspamd_logger" local rspamd_logger = require "rspamd_logger"
local rspamd_maps = require "maps"
local redis_params local redis_params


local settings = {} local settings = {}
local function check_addr_setting(rule, addr) local function check_addr_setting(rule, addr)
local function check_specific_addr(elt) local function check_specific_addr(elt)
if rule['name'] then if rule['name'] then
if rule['name']:lower() == elt['addr']:lower() then
if rspamd_maps.rspamd_maybe_check_map(rule['name'], elt['addr']) then
return true return true
end end
end end
if rule['user'] then if rule['user'] then
if rule['user']:lower() == elt['user']:lower() then
if rspamd_maps.rspamd_maybe_check_map(rule['user'], elt['user']) then
return true return true
end end
end end
if rule['domain'] and elt['domain'] then if rule['domain'] and elt['domain'] then
if rule['domain']:lower() == elt['domain']:lower() then
if rspamd_maps.rspamd_maybe_check_map(rule['domain'], elt['domain']) then
return true return true
end end
end end
end end


local function check_ip_setting(rule, ip) local function check_ip_setting(rule, ip)
if rule[2] ~= 0 then
local nip = ip:apply_mask(rule[2])
if nip and nip:to_string() == rule[1]:to_string() then
if not rule[2] then
if rspamd_maps.rspamd_maybe_check_map(rule[1], ip:to_string()) then
return true
end
else
if rule[2] ~= 0 then
local nip = ip:apply_mask(rule[2])
if nip and nip:to_string() == rule[1]:to_string() then
return true
end
elseif ip:to_string() == rule[1]:to_string() then
return true return true
end end
elseif ip:to_string() == rule[1]:to_string() then
return true
end end


return false return false
out[1] = res out[1] = res
out[2] = 0 out[2] = 0
else else
rspamd_logger.errx(rspamd_config, "bad IP address: " .. ip)
return nil
-- It can still be a map
out[1] = res
end end
else else
local res = rspamd_ip.from_string(string.sub(ip, 1, slash - 1)) local res = rspamd_ip.from_string(string.sub(ip, 1, slash - 1))
table.insert(out, process_addr(v)) table.insert(out, process_addr(v))
end end
elseif type(addr) == "string" then elseif type(addr) == "string" then
local start = string.sub(addr, 1, 1)
if start == '/' then
-- It is a regexp
local re = rspamd_regexp.create(addr)
if re then
out['regexp'] = re
else
rspamd_logger.errx(rspamd_config, "bad regexp: " .. addr)
return nil
end

elseif start == '@' then
-- It is a domain if form @domain
out['domain'] = string.sub(addr, 2)
if string.sub(addr, 1, 4) == "map:" then
-- It is map, don't apply any extra logic
out['name'] = addr
else else
-- Check user@domain parts
local at = string.find(addr, '@')
if at then
-- It is full address
out['name'] = addr
local start = string.sub(addr, 1, 1)
if start == '/' then
-- It is a regexp
local re = rspamd_regexp.create(addr)
if re then
out['regexp'] = re
else
rspamd_logger.errx(rspamd_config, "bad regexp: " .. addr)
return nil
end

elseif start == '@' then
-- It is a domain if form @domain
out['domain'] = string.sub(addr, 2)
else else
-- It is a user
out['user'] = addr
-- Check user@domain parts
local at = string.find(addr, '@')
if at then
-- It is full address
out['name'] = addr
else
-- It is a user
out['user'] = addr
end
end end
end end
else else

Loading…
Cancel
Save