Browse Source

[Minor] Rbl: More fixes

tags/2.0
Vsevolod Stakhov 4 years ago
parent
commit
00547ef4b0
1 changed files with 86 additions and 71 deletions
  1. 86
    71
      src/plugins/lua/rbl.lua

+ 86
- 71
src/plugins/lua/rbl.lua View File

forced = forced, forced = forced,
n = to_resolve, n = to_resolve,
orig = orign, orig = orign,
is_ip = resolve_ip,
resolve_ip = resolve_ip,
what = what, what = what,
} }
requests_table[req] = nreq requests_table[req] = nreq
local function check_dkim(task, requests_table, whitelist) local function check_dkim(task, requests_table, whitelist)
local das = task:get_symbol('DKIM_TRACE') local das = task:get_symbol('DKIM_TRACE')
local mime_from_domain local mime_from_domain
local ret = false


if das and das[1] and das[1].options then if das and das[1] and das[1].options then


if mime_from_domain and mime_from_domain == domain_tld then if mime_from_domain and mime_from_domain == domain_tld then
add_dns_request(task, domain_tld, true, false, requests_table, add_dns_request(task, domain_tld, true, false, requests_table,
'dkim', whitelist) 'dkim', whitelist)
ret = true
end end
else else
if rule.dkim_domainonly then if rule.dkim_domainonly then
add_dns_request(task, rspamd_util.get_tld(domain), add_dns_request(task, rspamd_util.get_tld(domain),
false, false, requests_table, 'dkim', whitelist) false, false, requests_table, 'dkim', whitelist)
ret = true
else else
add_dns_request(task, domain, false, false, requests_table, add_dns_request(task, domain, false, false, requests_table,
'dkim', whitelist) 'dkim', whitelist)
ret = true
end end
end end
end end
end end
end end


return ret
return true
end end


local function check_emails(task, requests_table, whitelist) local function check_emails(task, requests_table, whitelist)
end end


local emails = lua_util.extract_specific_urls(ex_params) local emails = lua_util.extract_specific_urls(ex_params)
if not emails or #emails == 0 then
return false
end


for _,email in ipairs(emails) do for _,email in ipairs(emails) do
if rule.emails_domainonly then if rule.emails_domainonly then
} }


local urls = lua_util.extract_specific_urls(ex_params) local urls = lua_util.extract_specific_urls(ex_params)
if not urls or #urls == 0 then
return false
end


for _,u in ipairs(urls) do for _,u in ipairs(urls) do
add_dns_request(task, u:get_tld(), false, add_dns_request(task, u:get_tld(), false,
'sel' .. rule.selector_id, whitelist) 'sel' .. rule.selector_id, whitelist)
end end
end end

return true
end end


-- Create function pipeline depending on rbl settings -- Create function pipeline depending on rbl settings
local pipeline = { local pipeline = {
is_alive, -- generic for all is_alive, -- generic for all
} }
local description = {
'alive',
}


if rule.exclude_users then if rule.exclude_users then
pipeline[#pipeline + 1] = check_user pipeline[#pipeline + 1] = check_user
description[#description + 1] = 'user'
end end


if rule.exclude_local or rule.exclude_private_ips then if rule.exclude_local or rule.exclude_private_ips then
pipeline[#pipeline + 1] = check_local pipeline[#pipeline + 1] = check_local
description[#description + 1] = 'local'
end end


if rule.helo then if rule.helo then
pipeline[#pipeline + 1] = check_helo pipeline[#pipeline + 1] = check_helo
description[#description + 1] = 'helo'
end end


if rule.dkim then if rule.dkim then
pipeline[#pipeline + 1] = check_dkim pipeline[#pipeline + 1] = check_dkim
description[#description + 1] = 'dkim'
end end


if rule.emails then if rule.emails then
pipeline[#pipeline + 1] = check_emails pipeline[#pipeline + 1] = check_emails
description[#description + 1] = 'emails'
end end


if rule.urls then if rule.urls then
pipeline[#pipeline + 1] = check_urls pipeline[#pipeline + 1] = check_urls
description[#description + 1] = 'urls'
end end


if rule.from then if rule.from then
pipeline[#pipeline + 1] = check_from pipeline[#pipeline + 1] = check_from
description[#description + 1] = 'ip'
end end


if rule.received then if rule.received then
pipeline[#pipeline + 1] = check_received pipeline[#pipeline + 1] = check_received
description[#description + 1] = 'received'
end end


if rule.rdns then if rule.rdns then
pipeline[#pipeline + 1] = check_rdns pipeline[#pipeline + 1] = check_rdns
description[#description + 1] = 'rdns'
end end


if rule.selector then if rule.selector then
pipeline[#pipeline + 1] = check_selector pipeline[#pipeline + 1] = check_selector
description[#description + 1] = 'selector'
end end


return function(task)
local callback_f = function(task)
-- DNS requests to issue (might be hashed afterwards) -- DNS requests to issue (might be hashed afterwards)
local dns_req = {} local dns_req = {}
local whitelist = task:cache_get('rbl_whitelisted') or {} local whitelist = task:cache_get('rbl_whitelisted') or {}
end end


-- Execute functions pipeline -- Execute functions pipeline
for _,f in ipairs(pipeline) do
for i,f in ipairs(pipeline) do
if not f(task, dns_req, whitelist) then if not f(task, dns_req, whitelist) then
lua_util.debugm(N, task, lua_util.debugm(N, task,
"skip rbl check: %s; pipeline condition returned false",
rule.symbol)
"skip rbl check: %s; pipeline condition %s returned false",
rule.symbol, i)
return return
end end
end end
end end
end end
end end

return callback_f,string.format('checks: %s', table.concat(description, ','))
end end


local function add_rbl(key, rbl) local function add_rbl(key, rbl)
'RBL whitelist for ' .. rbl.symbol) 'RBL whitelist for ' .. rbl.symbol)
end end


local id = rspamd_config:register_symbol{
type = 'callback',
callback = gen_rbl_callback(rbl),
name = rbl.symbol,
flags = table.concat(flags_tbl, ',')
}
local callback,description = gen_rbl_callback(rbl)


if rbl.dkim then
rspamd_config:register_dependency(rbl.symbol, 'DKIM_CHECK')
end
if callback then
local id = rspamd_config:register_symbol{
type = 'callback',
callback = callback,
name = rbl.symbol,
flags = table.concat(flags_tbl, ',')
}


-- Failure symbol
rspamd_config:register_symbol{
type = 'virtual,nostat',
name = rbl.symbol .. '_FAIL',
parent = id,
score = 0.0,
}
rspamd_logger.infox(rspamd_config, 'added rbl rule %s: %s',
rbl.symbol, description)

if rbl.dkim then
rspamd_config:register_dependency(rbl.symbol, 'DKIM_CHECK')
end


local function process_return_code(s)
rspamd_config:register_symbol({
name = s,
-- Failure symbol
rspamd_config:register_symbol{
type = 'virtual,nostat',
name = rbl.symbol .. '_FAIL',
parent = id, parent = id,
type = 'virtual'
})

if rbl.is_whitelist then
if rbl.whitelist_exception then
local found_exception = false
for _, e in ipairs(rbl.whitelist_exception) do
if e == s then
found_exception = true
break
score = 0.0,
}

local function process_return_code(s)
rspamd_config:register_symbol({
name = s,
parent = id,
type = 'virtual'
})

if rbl.is_whitelist then
if rbl.whitelist_exception then
local found_exception = false
for _, e in ipairs(rbl.whitelist_exception) do
if e == s then
found_exception = true
break
end
end end
end
if not found_exception then
if not found_exception then
table.insert(white_symbols, s)
end
else
table.insert(white_symbols, s) table.insert(white_symbols, s)
end end
else else
table.insert(white_symbols, s)
end
else
if rbl.ignore_whitelist == false then
table.insert(black_symbols, s)
if rbl.ignore_whitelist == false then
table.insert(black_symbols, s)
end
end end
end end
end


if rbl.returncodes then
for s,_ in pairs(rbl.returncodes) do
process_return_code(s)
if rbl.returncodes then
for s,_ in pairs(rbl.returncodes) do
process_return_code(s)
end
end end
end


if rbl.returnbits then
for s,_ in pairs(rbl.returnbits) do
process_return_code(s)
if rbl.returnbits then
for s,_ in pairs(rbl.returnbits) do
process_return_code(s)
end
end end
end


if not rbl.is_whitelist and rbl.ignore_whitelist == false then
table.insert(black_symbols, rbl.symbol)
end
-- Process monitored
if not rbl.disable_monitoring then
if not monitored_addresses[rbl.rbl] then
monitored_addresses[rbl.rbl] = true
rbl.monitored = rspamd_config:register_monitored(rbl.rbl, 'dns',
get_monitored(rbl))
if not rbl.is_whitelist and rbl.ignore_whitelist == false then
table.insert(black_symbols, rbl.symbol)
end end
-- Process monitored
if not rbl.disable_monitoring then
if not monitored_addresses[rbl.rbl] then
monitored_addresses[rbl.rbl] = true
rbl.monitored = rspamd_config:register_monitored(rbl.rbl, 'dns',
get_monitored(rbl))
end
end
return true
end end


return true
return false
end end


-- Configuration -- Configuration

Loading…
Cancel
Save