1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
--[[
Copyright (c) 2011-2015, Vsevolod Stakhov <vsevolod@highsecure.ru>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--
-- Multimap is rspamd module designed to define and operate with different maps
local rules = {}
local rspamd_logger = require "rspamd_logger"
local cdb = require "rspamd_cdb"
local util = require "rspamd_util"
local regexp = require "rspamd_regexp"
local _ = require "fun"
--local dumper = require 'pl.pretty'.dump
local function ip_to_rbl(ip, rbl)
return table.concat(ip:inversed_str_octets(), ".") .. '.' .. rbl
end
local function check_multimap(task)
-- Applies specific filter for input
local function apply_filter(filter, input, rule)
if filter == 'email:addr' or filter == 'email' then
local addr = util.parse_mail_address(input)
if addr and addr[1] then
return addr[1]['addr']
end
elseif filter == 'email:user' then
local addr = util.parse_mail_address(input)
if addr and addr[1] then
return addr[1]['user']
end
elseif filter == 'email:domain' then
local addr = util.parse_mail_address(input)
if addr and addr[1] then
return addr[1]['domain']
end
elseif filter == 'email:name' then
local addr = util.parse_mail_address(input)
if addr and addr[1] then
return addr[1]['name']
end
else
-- regexp case
if not rule['regexp'] then
local type,pat = string.match(filter, '(regexp:)(.+)')
if type and pat then
rule['regexp'] = regexp.create(pat)
end
end
if not rule['regexp'] then
rspamd_logger.errx(task, 'bad search filter: %s', filter)
else
local results = rule['regexp']:search(input)
if results then
return results[1]
end
end
end
return input
end
-- Match a single value for against a single rule
local function match_rule(r, value)
local ret = false
if r['filter'] then
value = apply_filter(r['filter'], value, r)
end
if not value then
return false
end
if r['cdb'] then
local srch = value
if r['type'] == 'ip' then
srch = value:to_string()
end
ret = r['cdb']:lookup(srch)
elseif r['radix'] then
ret = r['radix']:get_key(value)
elseif r['hash'] then
ret = r['hash']:get_key(value)
end
if ret then
task:insert_result(r['symbol'], 1)
end
return ret
end
-- Match list of values according to the field
local function match_list(r, ls, fields)
local ret = false
if ls then
if fields then
_.each(function(e)
local match = e[fields[1]]
if match then
if fields[2] then
match = fields[2](match)
end
ret = match_rule(r, match)
end
end, ls)
else
_.each(function(e) ret = match_rule(r, e) end, ls)
end
end
return ret
end
local function match_addr(r, addr)
local ret = match_list(r, addr, {'addr'})
if not ret then
-- Try domain
ret = match_list(r, addr, {'domain', function(d) return '@' .. d end})
end
if not ret then
-- Try user
ret = match_list(r, addr, {'user', function(d) return d .. '@' end})
end
return ret
end
-- IP rules
local ip = task:get_from_ip()
if ip:is_valid() then
_.each(function(r) match_rule(r, ip) end,
_.filter(function(r) return r['type'] == 'ip' end, rules))
end
-- Header rules
_.each(function(r)
local hv = task:get_header_full(r['header'])
match_list(r, hv, {'decoded'})
end,
_.filter(function(r) return r['type'] == 'header' end, rules))
-- Rcpt rules
if task:has_recipients() then
local rcpts = task:get_recipients()
_.each(function(r)
match_addr(r, rcpts)
end,
_.filter(function(r) return r['type'] == 'rcpt' end, rules))
end
-- From rules
if task:has_from() then
local from = task:get_from()
if from then
_.each(function(r)
match_addr(r, from)
end,
_.filter(function(r) return r['type'] == 'from' end, rules))
end
end
-- RBL rules
if ip:is_valid() then
_.each(function(r)
local cb = function (resolver, to_resolve, results, err, rbl)
if results then
task:insert_result(r['symbol'], 1, r['map'])
end
end
task:get_resolver():resolve_a({task = task,
name = ip_to_rbl(ip, r['map']),
callback = cb,
})
end,
_.filter(function(r) return r['type'] == 'dnsbl' end, rules))
end
end
local function add_multimap_rule(key, newrule)
if not newrule['map'] then
rspamd_logger.errx(rspamd_config, 'incomplete rule, missing map')
return nil
end
if not newrule['symbol'] and key then
newrule['symbol'] = key
elseif not newrule['symbol'] then
rspamd_logger.errx(rspamd_config, 'incomplete rule, missing symbol')
return nil
end
-- Check cdb flag
if string.find(newrule['map'], '^cdb://.*$') then
local test = cdb.create(newrule['map'])
newrule['cdb'] = cdb.create(newrule['map'])
if newrule['cdb'] then
return newrule
else
rspamd_logger.warnx(rspamd_config, 'Cannot add rule: map doesn\'t exists: %1',
newrule['map'])
end
else
if newrule['type'] == 'ip' then
newrule['radix'] = rspamd_config:add_radix_map (newrule['map'],
newrule['description'])
if newrule['radix'] then
return newrule
else
rspamd_logger.warnx(rspamd_config, 'Cannot add rule: map doesn\'t exists: %1',
newrule['map'])
end
elseif newrule['type'] == 'header' or newrule['type'] == 'rcpt' or newrule['type'] == 'from' then
newrule['hash'] = rspamd_config:add_hash_map (newrule['map'], newrule['description'])
if newrule['hash'] then
return newrule
else
rspamd_logger.warnx(rspamd_config, 'Cannot add rule: map doesn\'t exists: %1',
newrule['map'])
end
elseif newrule['type'] == 'dnsbl' then
return newrule
end
end
return nil
end
-- Registration
local opts = rspamd_config:get_all_opt('multimap')
if opts and type(opts) == 'table' then
for k,m in pairs(opts) do
if type(m) == 'table' then
local rule = add_multimap_rule(k, m)
if not rule then
rspamd_logger.errx(rspamd_config, 'cannot add rule: "'..k..'"')
else
table.insert(rules, rule)
end
else
rspamd_logger.errx(rspamd_config, 'parameter ' .. k .. ' is invalid, must be an object')
end
end
-- add fake symbol to check all maps inside a single callback
if type(rspamd_config.get_api_version) ~= 'nil' then
local id = rspamd_config:register_callback_symbol_priority(1.0, -1,
check_multimap)
for i,rule in ipairs(rules) do
rspamd_config:register_virtual_symbol(rule['symbol'], 1.0, id)
end
end
end
|