Debian Codegraph @

lualib/lua_scanners/sophos.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]]--
16
17 --[[[
18 -- @module savapi
19 -- This module contains avira savapi antivirus access functions
20 --]]
21
22 local lua_util = require "lua_util"
23 local tcp = require "rspamd_tcp"
24 local upstream_list = require "rspamd_upstream_list"
25 local rspamd_logger = require "rspamd_logger"
26 local common = require "lua_scanners/common"
27
28 local N = "sophos"
29
30 local default_message = '${SCANNER}: virus found: "${VIRUS}"'
31
32 local function sophos_config(opts)
33 local sophos_conf = {
34 name = N,
35 scan_mime_parts = true,
36 scan_text_mime = false,
37 scan_image_mime = false,
38 default_port = 4010,
39 timeout = 15.0,
40 log_clean = false,
41 retransmits = 2,
42 cache_expire = 3600, -- expire redis in one hour
43 message = default_message,
44 detection_category = "virus",
45 }
46
47 sophos_conf = lua_util.override_defaults(sophos_conf, opts)
48
49 if not sophos_conf.prefix then
50 sophos_conf.prefix = 'rs_' .. sophos_conf.name .. '_'
51 end
52
53 if not sophos_conf.log_prefix then
54 if sophos_conf.name:lower() == sophos_conf.type:lower() then
55 sophos_conf.log_prefix = sophos_conf.name
56 else
57 sophos_conf.log_prefix = sophos_conf.name .. ' (' .. sophos_conf.type .. ')'
58 end
59 end
60
61 if not sophos_conf['servers'] then
62 rspamd_logger.errx(rspamd_config, 'no servers defined')
63
64 return nil
65 end
66
67 sophos_conf['upstreams'] = upstream_list.create(rspamd_config,
68 sophos_conf['servers'],
69 sophos_conf.default_port)
70
71 if sophos_conf['upstreams'] then
72 lua_util.add_debug_alias('antivirus', sophos_conf.name)
73 return sophos_conf
74 end
75
76 rspamd_logger.errx(rspamd_config, 'cannot parse servers %s',
77 sophos_conf['servers'])
78 return nil
79 end
80
81 local function sophos_check(task, content, digest, rule, maybe_part)
82 local function sophos_check_uncached ()
83 local upstream = rule.upstreams:get_upstream_round_robin()
84 local addr = upstream:get_addr()
85 local retransmits = rule.retransmits
86 local protocol = 'SSSP/1.0\n'
87 local streamsize = string.format('SCANDATA %d\n', #content)
88 local bye = 'BYE\n'
89
90 local function sophos_callback(err, data, conn)
91
92 if err then
93 -- retry with another upstream until retransmits exceeds
94 if retransmits > 0 then
95
96 retransmits = retransmits - 1
97
98 -- Select a different upstream!
99 upstream = rule.upstreams:get_upstream_round_robin()
100 addr = upstream:get_addr()
101
102 lua_util.debugm(rule.name, task, '%s: error: %s; retry IP: %s; retries left: %s',
103 rule.log_prefix, err, addr, retransmits)
104
105 tcp.request({
106 task = task,
107 host = addr:to_string(),
108 port = addr:get_port(),
109 upstream = upstream,
110 timeout = rule['timeout'],
111 callback = sophos_callback,
112 data = { protocol, streamsize, content, bye }
113 })
114 else
115 rspamd_logger.errx(task, '%s [%s]: failed to scan, maximum retransmits exceed', rule['symbol'], rule['type'])
116 common.yield_result(task, rule, 'failed to scan and retransmits exceed',
117 0.0, 'fail', maybe_part)
118 end
119 else
120 data = tostring(data)
121 lua_util.debugm(rule.name, task,
122 '%s [%s]: got reply: %s', rule['symbol'], rule['type'], data)
123 local vname = string.match(data, 'VIRUS (%S+) ')
124 local cached
125 if vname then
126 common.yield_result(task, rule, vname, 1.0, nil, maybe_part)
127 common.save_cache(task, digest, rule, vname, 1.0, maybe_part)
128 else
129 if string.find(data, 'DONE OK') then
130 if rule['log_clean'] then
131 rspamd_logger.infox(task, '%s: message or mime_part is clean', rule.log_prefix)
132 else
133 lua_util.debugm(rule.name, task,
134 '%s: message or mime_part is clean', rule.log_prefix)
135 end
136 cached = 'OK'
137 -- not finished - continue
138 elseif string.find(data, 'ACC') or string.find(data, 'OK SSSP') then
139 conn:add_read(sophos_callback)
140 elseif string.find(data, 'FAIL 0212') then
141 rspamd_logger.warnx(task, 'Message is encrypted (FAIL 0212): %s', data)
142 common.yield_result(task, rule, 'SAVDI: Message is encrypted (FAIL 0212)',
143 0.0, 'encrypted', maybe_part)
144 cached = 'ENCRYPTED'
145 elseif string.find(data, 'REJ 4') then
146 rspamd_logger.warnx(task, 'Message is oversized (REJ 4): %s', data)
147 common.yield_result(task, rule, 'SAVDI: Message oversized (REJ 4)',
148 0.0, 'fail', maybe_part)
149 -- explicitly set REJ1 message when SAVDIreports a protocol error
150 elseif string.find(data, 'REJ 1') then
151 rspamd_logger.errx(task, 'SAVDI (Protocol error (REJ 1)): %s', data)
152 common.yield_result(task, rule, 'SAVDI: Protocol error (REJ 1)',
153 0.0, 'fail', maybe_part)
154 else
155 rspamd_logger.errx(task, 'unhandled response: %s', data)
156 common.yield_result(task, rule, 'unhandled response: ' .. data,
157 0.0, 'fail', maybe_part)
158 end
159 if cached then
160 common.save_cache(task, digest, rule, cached, 1.0, maybe_part)
161 end
162 end
163 end
164 end
165
166 tcp.request({
167 task = task,
168 host = addr:to_string(),
169 port = addr:get_port(),
170 upstream = upstream,
171 timeout = rule['timeout'],
172 callback = sophos_callback,
173 data = { protocol, streamsize, content, bye }
174 })
175 end
176
177 if common.condition_check_and_continue(task, content, rule, digest,
178 sophos_check_uncached, maybe_part) then
179 return
180 else
181 sophos_check_uncached()
182 end
183
184 end
185
186 return {
187 type = 'antivirus',
188 description = 'sophos antivirus',
189 configure = sophos_config,
190 check = sophos_check,
191 name = N
192 }