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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
-- Multimap is rspamd module designed to define and operate with different maps
local rules = {}
function split(str, delim, maxNb)
-- Eliminate bad cases...
if string.find(str, delim) == nil then
return { str }
end
if maxNb == nil or maxNb < 1 then
maxNb = 0 -- No limit
end
local result = {}
local pat = "(.-)" .. delim .. "()"
local nb = 0
local lastPos
for part, pos in string.gfind(str, pat) do
nb = nb + 1
result[nb] = part
lastPos = pos
if nb == maxNb then break end
end
-- Handle the last field
if nb ~= maxNb then
result[nb + 1] = string.sub(str, lastPos)
end
return result
end
function multimap_rbl_cb(task, to_resolve, results, err)
if results then
local _,_,o4,o3,o2,o1,in_rbl = string.find(to_resolve, '(%d+)%.(%d+)%.(%d+)%.(%d+)%.(.+)')
-- Get corresponding rule by rbl name
for _,rule in ipairs(rules) do
if rule['map'] == in_rbl then
task:insert_result(rule['symbol'], 1, rule['map'])
return
end
end
end
end
function check_multimap(task)
for _,rule in ipairs(rules) do
if rule['type'] == 'ip' then
if rule['cdb'] then
local ip = task:get_from_ip()
if rule['hash']:lookup(ip) then
task:insert_result(rule['symbol'], 1)
end
else
local ip = task:get_from_ip_num()
if rule['ips']:get_key(ip) then
task:insert_result(rule['symbol'], 1)
end
end
elseif rule['type'] == 'header' then
local headers = task:get_message():get_header(rule['header'])
if headers then
for _,hv in ipairs(headers) do
if rule['pattern'] then
-- extract a part from header
local _,_,ext = string.find(hv, rule['pattern'])
if ext then
if rule['cdb'] then
if rule['hash']:lookup(ext) then
task:insert_result(rule['symbol'], 1)
end
else
if rule['hash']:get_key(ext) then
task:insert_result(rule['symbol'], 1)
end
end
end
else
if rule['cdb'] then
if rule['hash']:lookup(hv) then
task:insert_result(rule['symbol'], 1)
end
else
if rule['hash']:get_key(hv) then
task:insert_result(rule['symbol'], 1)
end
end
end
end
end
elseif rule['type'] == 'dnsbl' then
local ip = task:get_from_ip()
if ip then
local _,_,o1,o2,o3,o4 = string.find(ip, '(%d+)%.(%d+)%.(%d+)%.(%d+)')
local rbl_str = o4 .. '.' .. o3 .. '.' .. o2 .. '.' .. o1 .. '.' .. rule['map']
task:resolve_dns_a(rbl_str, 'multimap_rbl_cb')
end
elseif rule['type'] == 'rcpt' then
-- First try to get rcpt field
local rcpts = task:get_recipients()
if rcpts then
for _,r in ipairs(rcpts) do
if r['addr'] then
if rule['pattern'] then
-- extract a part from header
local _,_,ext = string.find(r['addr'], rule['pattern'])
if ext then
if rule['cdb'] then
if rule['hash']:lookup(ext) then
task:insert_result(rule['symbol'], 1)
end
else
if rule['hash']:get_key(ext) then
task:insert_result(rule['symbol'], 1)
end
end
end
else
if rule['cdb'] then
if rule['hash']:lookup(r['addr']) then
task:insert_result(rule['symbol'], 1)
end
else
if rule['hash']:get_key(r['addr']) then
task:insert_result(rule['symbol'], 1)
end
end
end
end
end
else
-- Get from headers
local rcpts = task:get_recipients_headers()
if rcpts then
for _,r in ipairs(rcpts) do
if r['addr'] then
if rule['pattern'] then
-- extract a part from header
local _,_,ext = string.find(r['addr'], rule['pattern'])
if ext then
if rule['cdb'] then
if rule['hash']:lookup(ext) then
task:insert_result(rule['symbol'], 1)
end
else
if rule['hash']:get_key(ext) then
task:insert_result(rule['symbol'], 1)
end
end
end
else
if rule['cdb'] then
if rule['hash']:lookup(r['addr']) then
task:insert_result(rule['symbol'], 1)
end
else
if rule['hash']:get_key(r['addr']) then
task:insert_result(rule['symbol'], 1)
end
end
end
end
end
end
end
elseif rule['type'] == 'from' then
-- First try to get from field
local from = task:get_from()
if from then
for _,r in ipairs(from) do
if r['addr'] then
if rule['pattern'] then
-- extract a part from header
local _,_,ext = string.find(r['addr'], rule['pattern'])
if ext then
if rule['cdb'] then
if rule['hash']:lookup(ext) then
task:insert_result(rule['symbol'], 1)
end
else
if rule['hash']:get_key(ext) then
task:insert_result(rule['symbol'], 1)
end
end
end
else
if rule['cdb'] then
if rule['hash']:lookup(r['addr']) then
task:insert_result(rule['symbol'], 1)
end
else
if rule['hash']:get_key(r['addr']) then
task:insert_result(rule['symbol'], 1)
end
end
end
end
end
else
-- Get from headers
local from = task:get_from_headers()
if from then
for _,r in ipairs(from) do
if r['addr'] then
if rule['pattern'] then
-- extract a part from header
local _,_,ext = string.find(r['addr'], rule['pattern'])
if ext then
if rule['cdb'] then
if rule['hash']:lookup(ext) then
task:insert_result(rule['symbol'], 1)
end
else
if rule['hash']:get_key(ext) then
task:insert_result(rule['symbol'], 1)
end
end
end
else
if rule['cdb'] then
if rule['hash']:lookup(r['addr']) then
task:insert_result(rule['symbol'], 1)
end
else
if rule['hash']:get_key(r['addr']) then
task:insert_result(rule['symbol'], 1)
end
end
end
end
end
end
end
end
end
end
local function add_multimap_rule(params)
local newrule = {
type = 'ip',
header = nil,
pattern = nil,
map = nil,
symbol = nil
}
for _,param in ipairs(params) do
local _,_,name,value = string.find(param, '(%w+)%s*=%s*(.+)')
if not name or not value then
rspamd_logger.err('invalid rule: '..param)
return nil
end
if name == 'type' then
if value == 'ip' then
newrule['type'] = 'ip'
elseif value == 'dnsbl' then
newrule['type'] = 'dnsbl'
elseif value == 'header' then
newrule['type'] = 'header'
elseif value == 'rcpt' then
newrule['type'] = 'rcpt'
elseif value == 'from' then
newrule['type'] = 'from'
else
rspamd_logger.err('invalid rule type: '.. value)
return nil
end
elseif name == 'header' then
newrule['header'] = value
elseif name == 'pattern' then
newrule['pattern'] = value
elseif name == 'map' then
newrule['map'] = value
elseif name == 'symbol' then
newrule['symbol'] = value
else
rspamd_logger.err('invalid rule option: '.. name)
return nil
end
end
if not newrule['symbol'] or not newrule['map'] then
rspamd_logger.err('incomplete rule')
return nil
end
-- Check cdb flag
if string.find(newrule['map'], '^cdb://.*$') then
local test = cdb.create(newrule['map'])
newrule['hash'] = cdb.create(newrule['map'])
if newrule['hash'] then
table.insert(rules, newrule)
return newrule
else
rspamd_logger.warn('Cannot add rule: map doesn\'t exists: ' .. newrule['map'])
end
newrule['cdb'] = true
else
if newrule['type'] == 'ip' then
newrule['ips'] = rspamd_config:add_radix_map (newrule['map'])
if newrule['ips'] then
table.insert(rules, newrule)
return newrule
else
rspamd_logger.warn('Cannot add rule: map doesn\'t exists: ' .. 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'])
if newrule['hash'] then
table.insert(rules, newrule)
return newrule
else
rspamd_logger.warn('Cannot add rule: map doesn\'t exists: ' .. newrule['map'])
end
elseif newrule['type'] == 'cdb' then
newrule['hash'] = rspamd_cdb.create(newrule['map'])
if newrule['hash'] then
table.insert(rules, newrule)
return newrule
else
rspamd_logger.warn('Cannot add rule: map doesn\'t exists: ' .. newrule['map'])
end
else
table.insert(rules, newrule)
return newrule
end
end
return nil
end
-- Registration
if type(rspamd_config.get_api_version) ~= 'nil' then
if rspamd_config:get_api_version() >= 1 then
rspamd_config:register_module_option('multimap', 'rule', 'string')
end
end
local opts = rspamd_config:get_all_opt('multimap')
if opts then
local strrules = opts['rule']
if strrules then
if type(strrules) == 'table' then
for _,value in ipairs(strrules) do
local params = split(value, ',')
local rule = add_multimap_rule (params)
if not rule then
rspamd_logger.err('cannot add rule: "'..value..'"')
else
if type(rspamd_config.get_api_version) ~= 'nil' then
rspamd_config:register_virtual_symbol(rule['symbol'], 1.0)
end
end
end
elseif type(strrules) == 'string' then
local params = split(strrules, ',')
local rule = add_multimap_rule (params)
if not rule then
rspamd_logger.err('cannot add rule: "'..strrules..'"')
else
if type(rspamd_config.get_api_version) ~= 'nil' then
rspamd_config:register_virtual_symbol(rule['symbol'], 1.0)
end
end
end
end
end
if table.maxn(rules) > 0 then
-- add fake symbol to check all maps inside a single callback
if type(rspamd_config.get_api_version) ~= 'nil' then
if rspamd_config.get_api_version() >= 4 then
rspamd_config:register_callback_symbol_priority('MULTIMAP', 1.0, -1, 'check_multimap')
else
rspamd_config:register_callback_symbol('MULTIMAP', 1.0, 'check_multimap')
end
else
rspamd_config:register_symbol('MULTIMAP', 1.0, 'check_multimap')
end
end
|