Debian Codegraph @

lualib/lua_scanners/spamassassin.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3 Copyright (c) 2019, Carsten Rosenberg <[email protected]>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ]]--
17
18 --[[[
19 -- @module spamassassin
20 -- This module contains spamd access functions.
21 --]]
22
23 local lua_util = require "lua_util"
24 local tcp = require "rspamd_tcp"
25 local upstream_list = require "rspamd_upstream_list"
26 local rspamd_logger = require "rspamd_logger"
27 local common = require "lua_scanners/common"
28
29 local N = 'spamassassin'
30
31 local function spamassassin_config(opts)
32
33 local spamassassin_conf = {
34 N = N,
35 scan_mime_parts = false,
36 scan_text_mime = false,
37 scan_image_mime = false,
38 default_port = 783,
39 timeout = 15.0,
40 log_clean = false,
41 retransmits = 2,
42 cache_expire = 3600, -- expire redis in one hour
43 symbol = "SPAMD",
44 message = '${SCANNER}: Spamassassin bulk message found: "${VIRUS}"',
45 detection_category = "spam",
46 default_score = 1,
47 action = false,
48 extended = false,
49 symbol_type = 'postfilter',
50 dynamic_scan = true,
51 }
52
53 spamassassin_conf = lua_util.override_defaults(spamassassin_conf, opts)
54
55 if not spamassassin_conf.prefix then
56 spamassassin_conf.prefix = 'rs_' .. spamassassin_conf.name .. '_'
57 end
58
59 if not spamassassin_conf.log_prefix then
60 if spamassassin_conf.name:lower() == spamassassin_conf.type:lower() then
61 spamassassin_conf.log_prefix = spamassassin_conf.name
62 else
63 spamassassin_conf.log_prefix = spamassassin_conf.name .. ' (' .. spamassassin_conf.type .. ')'
64 end
65 end
66
67 if not spamassassin_conf.servers then
68 rspamd_logger.errx(rspamd_config, 'no servers defined')
69
70 return nil
71 end
72
73 spamassassin_conf.upstreams = upstream_list.create(rspamd_config,
74 spamassassin_conf.servers,
75 spamassassin_conf.default_port)
76
77 if spamassassin_conf.upstreams then
78 lua_util.add_debug_alias('external_services', spamassassin_conf.N)
79 return spamassassin_conf
80 end
81
82 rspamd_logger.errx(rspamd_config, 'cannot parse servers %s',
83 spamassassin_conf.servers)
84 return nil
85 end
86
87 local function spamassassin_check(task, content, digest, rule)
88 local function spamassassin_check_uncached ()
89 local upstream = rule.upstreams:get_upstream_round_robin()
90 local addr = upstream:get_addr()
91 local retransmits = rule.retransmits
92
93 -- Build the spamd query
94 -- https://svn.apache.org/repos/asf/spamassassin/trunk/spamd/PROTOCOL
95 local request_data = {
96 "HEADERS SPAMC/1.5\r\n",
97 "User: root\r\n",
98 "Content-length: " .. #content .. "\r\n",
99 "\r\n",
100 content,
101 }
102
103 local function spamassassin_callback(err, data)
104
105 local function spamassassin_requery(error)
106
107 -- retry with another upstream until retransmits exceeds
108 if retransmits > 0 then
109
110 retransmits = retransmits - 1
111
112 lua_util.debugm(rule.N, task, '%s: Request Error: %s - retries left: %s',
113 rule.log_prefix, error, retransmits)
114
115 -- Select a different upstream!
116 upstream = rule.upstreams:get_upstream_round_robin()
117 addr = upstream:get_addr()
118
119 lua_util.debugm(rule.N, task, '%s: retry IP: %s:%s',
120 rule.log_prefix, addr, addr:get_port())
121
122 tcp.request({
123 task = task,
124 host = addr:to_string(),
125 port = addr:get_port(),
126 upstream = upstream,
127 timeout = rule['timeout'],
128 data = request_data,
129 callback = spamassassin_callback,
130 })
131 else
132 rspamd_logger.errx(task, '%s: failed to scan, maximum retransmits ' ..
133 'exceed - err: %s', rule.log_prefix, error)
134 common.yield_result(task, rule, 'failed to scan and retransmits exceed: ' .. error, 0.0, 'fail')
135 end
136 end
137
138 if err then
139
140 spamassassin_requery(err)
141
142 else
143 --lua_util.debugm(rule.N, task, '%s: returned result: %s', rule.log_prefix, data)
144
145 --[[
146 patterns tested against Spamassassin 3.4.6
147
148 X-Spam-Status: No, score=1.1 required=5.0 tests=HTML_MESSAGE,MIME_HTML_ONLY,
149 TVD_RCVD_SPACE_BRACKET,UNPARSEABLE_RELAY autolearn=no
150 autolearn_force=no version=3.4.6
151 ]] --
152 local header = string.gsub(tostring(data), "[\r\n]+[\t ]", " ")
153 --lua_util.debugm(rule.N, task, '%s: returned header: %s', rule.log_prefix, header)
154
155 local symbols = ""
156 local spam_score = 0
157 for s in header:gmatch("[^\r\n]+") do
158 if string.find(s, 'X%-Spam%-Status: %S+, score') then
159 local pattern_symbols = "X%-Spam%-Status: %S+, score%=([%-%d%.]+)%s.*tests%=(.*,?)(%s*%S+)%sautolearn.*"
160 spam_score = string.gsub(s, pattern_symbols, "%1")
161 symbols = string.gsub(s, pattern_symbols, "%2%3")
162 symbols = string.gsub(symbols, "%s", "")
163 end
164 end
165
166 lua_util.debugm(rule.N, task, '%s: spam_score: %s, symbols: %s, int spam_score: |%s|, type spam_score: |%s|',
167 rule.log_prefix, spam_score, symbols, tonumber(spam_score), type(spam_score))
168
169 if tonumber(spam_score) > 0 and #symbols > 0 and symbols ~= "none" then
170
171 if rule.extended == false then
172 common.yield_result(task, rule, symbols, spam_score)
173 common.save_cache(task, digest, rule, symbols, spam_score)
174 else
175 local symbols_table = lua_util.str_split(symbols, ",")
176 lua_util.debugm(rule.N, task, '%s: returned symbols as table: %s', rule.log_prefix, symbols_table)
177
178 common.yield_result(task, rule, symbols_table, spam_score)
179 common.save_cache(task, digest, rule, symbols_table, spam_score)
180 end
181 else
182 common.save_cache(task, digest, rule, 'OK')
183 common.log_clean(task, rule, 'no spam detected - spam score: ' .. spam_score .. ', symbols: ' .. symbols)
184 end
185 end
186 end
187
188 tcp.request({
189 task = task,
190 host = addr:to_string(),
191 port = addr:get_port(),
192 upstream = upstream,
193 timeout = rule['timeout'],
194 data = request_data,
195 callback = spamassassin_callback,
196 })
197 end
198
199 if common.condition_check_and_continue(task, content, rule, digest, spamassassin_check_uncached) then
200 return
201 else
202 spamassassin_check_uncached()
203 end
204
205 end
206
207 return {
208 type = { N, 'spam', 'scanner' },
209 description = 'spamassassin spam scanner',
210 configure = spamassassin_config,
211 check = spamassassin_check,
212 name = N
213 }