diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-12-29 11:04:53 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-12-29 11:04:53 +0000 |
commit | 9fddf731cf2161288d70d50c182831124cbd377b (patch) | |
tree | 483f51d8bb3ef8e2fa0f2426901f69ccb0efd18c /src/plugins | |
parent | 2f2526e7005bc151a032dacec6a182107dde5a74 (diff) | |
download | rspamd-9fddf731cf2161288d70d50c182831124cbd377b.tar.gz rspamd-9fddf731cf2161288d70d50c182831124cbd377b.zip |
[Rework] Dcc: Rework DCC plugin
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/lua/dcc.lua | 156 |
1 files changed, 11 insertions, 145 deletions
diff --git a/src/plugins/lua/dcc.lua b/src/plugins/lua/dcc.lua index 8c5dddeeb..d82ceda94 100644 --- a/src/plugins/lua/dcc.lua +++ b/src/plugins/lua/dcc.lua @@ -21,10 +21,8 @@ local N = 'dcc' local symbol_bulk = "DCC_BULK" local opts = rspamd_config:get_all_opt(N) local rspamd_logger = require "rspamd_logger" -local lua_util = require "lua_util" -local tcp = require "rspamd_tcp" -local upstream_list = require "rspamd_upstream_list" -local fun = require "fun" +local dcc = require("lua_scanners").filter('dcc').dcc + if confighelp then rspamd_config:add_example(nil, 'dcc', @@ -39,145 +37,10 @@ dcc { return end -local function check_dcc (task) - -- Connection - local client = '0.0.0.0' - local client_ip = task:get_from_ip() - local dcc_upstream - local upstream - local addr - local port - local retransmits = 2 - - if opts['servers'] then - dcc_upstream = upstream_list.create(rspamd_config, opts['servers']) - upstream = dcc_upstream:get_upstream_round_robin() - addr = upstream:get_addr() - port = addr:get_port() - else - lua_util.debugm(N, task, 'using socket %s', opts['socket']) - addr = opts['socket'] - end - - if client_ip and client_ip:is_valid() then - client = client_ip:to_string() - end - local client_host = task:get_hostname() - if client_host then - client = client .. "\r" .. client_host - end - - -- HELO - local helo = task:get_helo() or '' - - -- Envelope From - local ef = task:get_from() - local envfrom = 'test@example.com' - if ef and ef[1] then - envfrom = ef[1]['addr'] - end - - -- Envelope To - local envrcpt = 'test@example.com' - local rcpts = task:get_recipients(); - if rcpts then - local r = table.concat(fun.totable(fun.map(function(rcpt) - return rcpt['addr'] end, - rcpts)), '\n') - if r then - envrcpt = r - end - end - - -- Callback function to receive async result from DCC - local function cb(err, data) - - if err then - if retransmits > 0 then - retransmits = retransmits - 1 - -- Select a different upstream or the socket again - if opts['servers'] then - upstream = dcc_upstream:get_upstream_round_robin() - addr = upstream:get_addr() - port = addr:get_port() - else - addr = opts['socket'] - end - - lua_util.debugm(N, task, "sending query to %s:%s",tostring(addr), port) +local rule - data = { - "header\n", - client .. "\n", - helo .. "\n", - envfrom .. "\n", - envrcpt .. "\n", - "\n", - task:get_content() - } - - tcp.request({ - task = task, - host = tostring(addr), - port = port or 1, - timeout = opts['timeout'] or 2.0, - shutdown = true, - data = data, - callback = cb - }) - - else - rspamd_logger.errx(task, 'failed to scan, maximum retransmits exceed') - if upstream then upstream:fail() end - end - else - -- Parse the response - if upstream then upstream:ok() end - local _,_,result,disposition,header = tostring(data):find("(.-)\n(.-)\n(.-)\n") - lua_util.debugm(N, task, 'DCC result=%1 disposition=%2 header="%3"', - result, disposition, header) - - if header then - local _,_,info = header:find("; (.-)$") - if (result == 'R') then - -- Reject - task:insert_result(symbol_bulk, 1.0, info) - elseif (result == 'T') then - -- Temporary failure - rspamd_logger.warnx(task, 'DCC returned a temporary failure result') - else - if result ~= 'A' and result ~= 'G' and result ~= 'S' then - -- Unknown result - rspamd_logger.warnx(task, 'DCC result error: %1', result); - end - end - end - end - end - - -- Build the DCC query - -- https://www.dcc-servers.net/dcc/dcc-tree/dccifd.html#Protocol - local data = { - "header\n", - client .. "\n", - helo .. "\n", - envfrom .. "\n", - envrcpt .. "\n", - "\n", - task:get_content() - } - - rspamd_logger.warnx(task, "sending to %s:%s",tostring(addr), port) - - tcp.request({ - task = task, - host = tostring(addr), - port = port or 1, - timeout = opts['timeout'] or 2.0, - shutdown = true, - data = data, - callback = cb - }) +local function check_dcc (task) + dcc.check(task, task:get_content(), nil, rule) end -- Configuration @@ -195,9 +58,12 @@ if opts['host'] ~= nil and not opts['port'] then end -- WORKAROUND for deprecated host and port settings -if opts and ( opts['servers'] or opts['socket'] ) then +if not opts.symbol then opts.symbol = symbol_bulk end +rule = dcc.configure(opts) + +if rule then rspamd_config:register_symbol({ - name = symbol_bulk, + name = opts.symbol, callback = check_dcc }) rspamd_config:set_metric_symbol({ @@ -205,7 +71,7 @@ if opts and ( opts['servers'] or opts['socket'] ) then score = 2.0, description = 'Detected as bulk mail by DCC', one_shot = true, - name = symbol_bulk + name = opts.symbol, }) else lua_util.disable_module(N, "config") |