diff options
author | Carsten Rosenberg <c.rosenberg@heinlein-support.de> | 2019-10-02 16:47:41 +0200 |
---|---|---|
committer | Carsten Rosenberg <c.rosenberg@heinlein-support.de> | 2019-10-02 16:47:41 +0200 |
commit | adc10228b43a7b1a8d6203579611d9cca04545ea (patch) | |
tree | cac934740fc56a46a2d1e58e17f516f9ab76dc73 /lualib/lua_scanners/vadesecure.lua | |
parent | e273a7d2cb8a94949e5aaf6b8efd9b78b7c6a17e (diff) | |
download | rspamd-adc10228b43a7b1a8d6203579611d9cca04545ea.tar.gz rspamd-adc10228b43a7b1a8d6203579611d9cca04545ea.zip |
[Minor] lua_scanners - fix need_check, lua warnings #2, use lua_util
Diffstat (limited to 'lualib/lua_scanners/vadesecure.lua')
-rw-r--r-- | lualib/lua_scanners/vadesecure.lua | 254 |
1 files changed, 130 insertions, 124 deletions
diff --git a/lualib/lua_scanners/vadesecure.lua b/lualib/lua_scanners/vadesecure.lua index 0e539ef46..77a9e4dee 100644 --- a/lualib/lua_scanners/vadesecure.lua +++ b/lualib/lua_scanners/vadesecure.lua @@ -152,164 +152,170 @@ local function vade_config(opts) end local function vade_check(task, content, digest, rule) - local function vade_url(addr) - local url - if rule.use_https then - url = string.format('https://%s:%d%s', tostring(addr), - rule.default_port, rule.url) - else - url = string.format('http://%s:%d%s', tostring(addr), - rule.default_port, rule.url) + local function vade_check_uncached() + local function vade_url(addr) + local url + if rule.use_https then + url = string.format('https://%s:%d%s', tostring(addr), + rule.default_port, rule.url) + else + url = string.format('http://%s:%d%s', tostring(addr), + rule.default_port, rule.url) + end + + return url end - return url - end + local upstream = rule.upstreams:get_upstream_round_robin() + local addr = upstream:get_addr() + local retransmits = rule.retransmits - local upstream = rule.upstreams:get_upstream_round_robin() - local addr = upstream:get_addr() - local retransmits = rule.retransmits + local url = vade_url(addr) + local hdrs = {} - local url = vade_url(addr) - local hdrs = {} + local helo = task:get_helo() + if helo then + hdrs['X-Helo'] = helo + end + local mail_from = task:get_from('smtp') or {} + if mail_from[1] and #mail_from[1].addr > 1 then + hdrs['X-Mailfrom'] = mail_from[1].addr + end - local helo = task:get_helo() - if helo then - hdrs['X-Helo'] = helo - end - local mail_from = task:get_from('smtp') or {} - if mail_from[1] and #mail_from[1].addr > 1 then - hdrs['X-Mailfrom'] = mail_from[1].addr - end + local rcpt_to = task:get_recipients('smtp') + if rcpt_to then + hdrs['X-Rcptto'] = {} + for _, r in ipairs(rcpt_to) do + table.insert(hdrs['X-Rcptto'], r.addr) + end + end - local rcpt_to = task:get_recipients('smtp') - if rcpt_to then - hdrs['X-Rcptto'] = {} - for _, r in ipairs(rcpt_to) do - table.insert(hdrs['X-Rcptto'], r.addr) + local fip = task:get_from_ip() + if fip and fip:is_valid() then + hdrs['X-Inet'] = tostring(fip) end - end - local fip = task:get_from_ip() - if fip and fip:is_valid() then - hdrs['X-Inet'] = tostring(fip) - end + local request_data = { + task = task, + url = url, + body = task:get_content(), + headers = hdrs, + timeout = rule.timeout, + } - local request_data = { - task = task, - url = url, - body = task:get_content(), - headers = hdrs, - timeout = rule.timeout, - } + local function vade_callback(http_err, code, body, headers) - local function vade_callback(http_err, code, body, headers) + local function vade_requery() + -- set current upstream to fail because an error occurred + upstream:fail() - local function vade_requery() - -- set current upstream to fail because an error occurred - upstream:fail() + -- retry with another upstream until retransmits exceeds + if retransmits > 0 then - -- retry with another upstream until retransmits exceeds - if retransmits > 0 then + retransmits = retransmits - 1 - retransmits = retransmits - 1 + lua_util.debugm(rule.name, task, + '%s: Request Error: %s - retries left: %s', + rule.log_prefix, http_err, retransmits) - lua_util.debugm(rule.name, task, - '%s: Request Error: %s - retries left: %s', - rule.log_prefix, http_err, retransmits) + -- Select a different upstream! + upstream = rule.upstreams:get_upstream_round_robin() + addr = upstream:get_addr() + url = vade_url(addr) - -- Select a different upstream! - upstream = rule.upstreams:get_upstream_round_robin() - addr = upstream:get_addr() - url = vade_url(addr) + lua_util.debugm(rule.name, task, '%s: retry IP: %s:%s', + rule.log_prefix, addr, addr:get_port()) + request_data.url = url - lua_util.debugm(rule.name, task, '%s: retry IP: %s:%s', - rule.log_prefix, addr, addr:get_port()) - request_data.url = url + http.request(request_data) + else + rspamd_logger.errx(task, '%s: failed to scan, maximum retransmits '.. + 'exceed', rule.log_prefix) + task:insert_result(rule['symbol_fail'], 0.0, 'failed to scan and '.. + 'retransmits exceed') + end + end - http.request(request_data) + if http_err then + vade_requery() else - rspamd_logger.errx(task, '%s: failed to scan, maximum retransmits '.. - 'exceed', rule.log_prefix) - task:insert_result(rule['symbol_fail'], 0.0, 'failed to scan and '.. - 'retransmits exceed') - end - end + -- Parse the response + if upstream then upstream:ok() end + if code ~= 200 then + rspamd_logger.errx(task, 'invalid HTTP code: %s, body: %s, headers: %s', code, body, headers) + task:insert_result(rule.symbol_fail, 1.0, 'Bad HTTP code: ' .. code) + return + end + local parser = ucl.parser() + local ret, err = parser:parse_string(body) + if not ret then + rspamd_logger.errx(task, 'vade: bad response body (raw): %s', body) + task:insert_result(rule.symbol_fail, 1.0, 'Parser error: ' .. err) + return + end + local obj = parser:get_object() + local verdict = obj.verdict + if not verdict then + rspamd_logger.errx(task, 'vade: bad response JSON (no verdict): %s', obj) + task:insert_result(rule.symbol_fail, 1.0, 'No verdict/unknown verdict') + return + end + local vparts = lua_util.str_split(verdict, ":") + verdict = table.remove(vparts, 1) or verdict - if http_err then - vade_requery() - else - -- Parse the response - if upstream then upstream:ok() end - if code ~= 200 then - rspamd_logger.errx(task, 'invalid HTTP code: %s, body: %s, headers: %s', code, body, headers) - task:insert_result(rule.symbol_fail, 1.0, 'Bad HTTP code: ' .. code) - return - end - local parser = ucl.parser() - local ret, err = parser:parse_string(body) - if not ret then - rspamd_logger.errx(task, 'vade: bad response body (raw): %s', body) - task:insert_result(rule.symbol_fail, 1.0, 'Parser error: ' .. err) - return - end - local obj = parser:get_object() - local verdict = obj.verdict - if not verdict then - rspamd_logger.errx(task, 'vade: bad response JSON (no verdict): %s', obj) - task:insert_result(rule.symbol_fail, 1.0, 'No verdict/unknown verdict') - return - end - local vparts = lua_util.rspamd_str_split(verdict, ":") - verdict = table.remove(vparts, 1) or verdict + local sym = rule.symbols[verdict] + if not sym then + sym = rule.symbols.other + end - local sym = rule.symbols[verdict] - if not sym then - sym = rule.symbols.other - end + if not sym.symbol then + -- Subcategory match + local lvl = 'low' + if vparts and vparts[1] then + lvl = vparts[1] + end + + if sym[lvl] then + sym = sym[lvl] + else + sym = rule.symbols.other + end + end - if not sym.symbol then - -- Subcategory match - local lvl = 'low' - if vparts and vparts[1] then - lvl = vparts[1] + local opts = {} + if obj.score then + table.insert(opts, 'score=' .. obj.score) + end + if obj.elapsed then + table.insert(opts, 'elapsed=' .. obj.elapsed) end - if sym[lvl] then - sym = sym[lvl] + if rule.log_spamcause and obj.spamcause then + rspamd_logger.infox(task, 'vadesecure verdict="%s", score=%s, spamcause="%s", message-id="%s"', + verdict, obj.score, obj.spamcause, task:get_message_id()) else - sym = rule.symbols.other + lua_util.debugm(rule.name, task, 'vadesecure returned verdict="%s", score=%s, spamcause="%s"', + verdict, obj.score, obj.spamcause) end - end - - local opts = {} - if obj.score then - table.insert(opts, 'score=' .. obj.score) - end - if obj.elapsed then - table.insert(opts, 'elapsed=' .. obj.elapsed) - end - if rule.log_spamcause and obj.spamcause then - rspamd_logger.infox(task, 'vadesecure verdict="%s", score=%s, spamcause="%s", message-id="%s"', - verdict, obj.score, obj.spamcause, task:get_message_id()) - else - lua_util.debugm(rule.name, task, 'vadesecure returned verdict="%s", score=%s, spamcause="%s"', - verdict, obj.score, obj.spamcause) - end + if #vparts > 0 then + table.insert(opts, 'verdict=' .. verdict .. ';' .. table.concat(vparts, ':')) + end - if #vparts > 0 then - table.insert(opts, 'verdict=' .. verdict .. ';' .. table.concat(vparts, ':')) + task:insert_result(sym.symbol, 1.0, opts) end - - task:insert_result(sym.symbol, 1.0, opts) end - end - if common.need_check(task, content, rule, digest) then request_data.callback = vade_callback http.request(request_data) end + if common.need_check(task, content, rule, digest, vade_check_uncached) then + return + else + vade_check_uncached() + end + end return { |