Debian Codegraph @

lualib/lua_scanners/fprot.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 fprot
19 -- This module contains fprot 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 = "fprot"
29
30 local default_message = '${SCANNER}: virus found: "${VIRUS}"'
31
32 local function fprot_config(opts)
33 local fprot_conf = {
34 name = N,
35 scan_mime_parts = true,
36 scan_text_mime = false,
37 scan_image_mime = false,
38 default_port = 10200,
39 timeout = 5.0, -- FIXME: this will break task_timeout!
40 log_clean = false,
41 detection_category = "virus",
42 retransmits = 2,
43 cache_expire = 3600, -- expire redis in one hour
44 message = default_message,
45 }
46
47 fprot_conf = lua_util.override_defaults(fprot_conf, opts)
48
49 if not fprot_conf.prefix then
50 fprot_conf.prefix = 'rs_' .. fprot_conf.name .. '_'
51 end
52
53 if not fprot_conf.log_prefix then
54 if fprot_conf.name:lower() == fprot_conf.type:lower() then
55 fprot_conf.log_prefix = fprot_conf.name
56 else
57 fprot_conf.log_prefix = fprot_conf.name .. ' (' .. fprot_conf.type .. ')'
58 end
59 end
60
61 if not fprot_conf['servers'] then
62 rspamd_logger.errx(rspamd_config, 'no servers defined')
63
64 return nil
65 end
66
67 fprot_conf['upstreams'] = upstream_list.create(rspamd_config,
68 fprot_conf['servers'],
69 fprot_conf.default_port)
70
71 if fprot_conf['upstreams'] then
72 lua_util.add_debug_alias('antivirus', fprot_conf.name)
73 return fprot_conf
74 end
75
76 rspamd_logger.errx(rspamd_config, 'cannot parse servers %s',
77 fprot_conf['servers'])
78 return nil
79 end
80
81 local function fprot_check(task, content, digest, rule, maybe_part)
82 local function fprot_check_uncached ()
83 local upstream = rule.upstreams:get_upstream_round_robin()
84 local addr = upstream:get_addr()
85 local retransmits = rule.retransmits
86 local scan_id = task:get_queue_id()
87 if not scan_id then
88 scan_id = task:get_uid()
89 end
90 local header = string.format('SCAN STREAM %s SIZE %d\n', scan_id,
91 #content)
92 local footer = '\n'
93
94 local function fprot_callback(err, data)
95 if err then
96 -- retry with another upstream until retransmits exceeds
97 if retransmits > 0 then
98
99 retransmits = retransmits - 1
100
101 -- Select a different upstream!
102 upstream = rule.upstreams:get_upstream_round_robin()
103 addr = upstream:get_addr()
104
105 lua_util.debugm(rule.name, task, '%s: error: %s; retry IP: %s; retries left: %s',
106 rule.log_prefix, err, addr, retransmits)
107
108 tcp.request({
109 task = task,
110 host = addr:to_string(),
111 port = addr:get_port(),
112 upstream = upstream,
113 timeout = rule['timeout'],
114 callback = fprot_callback,
115 data = { header, content, footer },
116 stop_pattern = '\n'
117 })
118 else
119 rspamd_logger.errx(task,
120 '%s [%s]: failed to scan, maximum retransmits exceed',
121 rule['symbol'], rule['type'])
122 common.yield_result(task, rule, 'failed to scan and retransmits exceed',
123 0.0, 'fail', maybe_part)
124 end
125 else
126 upstream:ok()
127 data = tostring(data)
128 local cached
129 local clean = string.match(data, '^0 <clean>')
130 if clean then
131 cached = 'OK'
132 if rule['log_clean'] then
133 rspamd_logger.infox(task,
134 '%s [%s]: message or mime_part is clean',
135 rule['symbol'], rule['type'])
136 end
137 else
138 -- returncodes: 1: infected, 2: suspicious, 3: both, 4-255: some error occurred
139 -- see http://www.f-prot.com/support/helpfiles/unix/appendix_c.html for more detail
140 local vname = string.match(data, '^[1-3] <[%w%s]-: (.-)>')
141 if not vname then
142 rspamd_logger.errx(task, 'Unhandled response: %s', data)
143 else
144 common.yield_result(task, rule, vname, 1.0, nil, maybe_part)
145 cached = vname
146 end
147 end
148 if cached then
149 common.save_cache(task, digest, rule, cached, 1.0, maybe_part)
150 end
151 end
152 end
153
154 tcp.request({
155 task = task,
156 host = addr:to_string(),
157 port = addr:get_port(),
158 upstream = upstream,
159 timeout = rule['timeout'],
160 callback = fprot_callback,
161 data = { header, content, footer },
162 stop_pattern = '\n'
163 })
164 end
165
166 if common.condition_check_and_continue(task, content, rule, digest,
167 fprot_check_uncached, maybe_part) then
168 return
169 else
170 fprot_check_uncached()
171 end
172
173 end
174
175 return {
176 type = 'antivirus',
177 description = 'fprot antivirus',
178 configure = fprot_config,
179 check = fprot_check,
180 name = N
181 }