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
|
-- Multimap is rspamd module designed to define and operate with different maps
local rules = {}
local rspamd_logger = require "rspamd_logger"
local rspamd_cdb = require "rspamd_cdb"
local function ip_to_rbl(ip, rbl)
return table.concat(ip:inversed_str_octets(), ".") .. '.' .. rbl
end
local function check_multimap(task)
local function multimap_rbl_cb(resolver, to_resolve, results, err, rbl)
task:inc_dns_req()
if results then
-- Get corresponding rule by rbl name
for _,rule in pairs(rules) do
if rule == rbl then
task:insert_result(rule['symbol'], 1, rule['map'])
return
end
end
end
end
for _,rule in pairs(rules) do
if rule['type'] == 'ip' then
if rule['cdb'] then
local ip = task:get_from_ip()
if ip:is_valid() and rule['hash']:lookup(ip:to_string()) then
task:insert_result(rule['symbol'], 1)
end
else
local ip = task:get_from_ip()
if ip:is_valid() and rule['ips'] and rule['ips']:get_key(ip:to_number()) then
task:insert_result(rule['symbol'], 1)
end
end
elseif rule['type'] == 'header' then
local headers = task:get_header_full(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['decoded'], 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['decoded']) then
task:insert_result(rule['symbol'], 1)
end
else
if rule['hash']:get_key(hv['decoded']) 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:is_valid() then
if ip:get_version() == 6 and rule['ipv6'] then
task:get_resolver():resolve_a(task:get_session(), task:get_mempool(),
ip_to_rbl(ip, rule['map']), multimap_rbl_cb, rule['map'])
elseif ip:get_version() == 4 then
task:get_resolver():resolve_a(task:get_session(), task:get_mempool(),
ip_to_rbl(ip, rule['map']), multimap_rbl_cb, rule['map'])
end
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
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
end
end
end
end
local function add_multimap_rule(key, newrule)
if not newrule['map'] then
rspamd_logger.err('incomplete rule')
return nil
end
if not newrule['symbol'] and key then
newrule['symbol'] = key
elseif not newrule['symbol'] 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'])
newrule['cdb'] = true
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
if newrule['type'] == 'ip' then
newrule['ips'] = rspamd_config:add_radix_map (newrule['map'], newrule['description'])
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'], newrule['description'])
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 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.err('cannot add rule: "'..k..'"')
else
if type(rspamd_config.get_api_version) ~= 'nil' then
rspamd_config:register_virtual_symbol(rule['symbol'], 1.0)
end
end
else
rspamd_logger.err('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
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
end
end
|