Debian Codegraph @

lualib/plugins/dmarc.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3 Copyright (c) 2015-2016, Andrew Lewis <[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 -- Common dmarc stuff
19 local rspamd_logger = require "rspamd_logger"
20 local lua_util = require "lua_util"
21 local N = "dmarc"
22
23 local exports = {}
24
25 exports.default_settings = {
26 auth_and_local_conf = false,
27 symbols = {
28 spf_allow_symbol = 'R_SPF_ALLOW',
29 spf_deny_symbol = 'R_SPF_FAIL',
30 spf_softfail_symbol = 'R_SPF_SOFTFAIL',
31 spf_neutral_symbol = 'R_SPF_NEUTRAL',
32 spf_tempfail_symbol = 'R_SPF_DNSFAIL',
33 spf_permfail_symbol = 'R_SPF_PERMFAIL',
34 spf_na_symbol = 'R_SPF_NA',
35
36 dkim_allow_symbol = 'R_DKIM_ALLOW',
37 dkim_deny_symbol = 'R_DKIM_REJECT',
38 dkim_tempfail_symbol = 'R_DKIM_TEMPFAIL',
39 dkim_na_symbol = 'R_DKIM_NA',
40 dkim_permfail_symbol = 'R_DKIM_PERMFAIL',
41
42 -- DMARC symbols
43 allow = 'DMARC_POLICY_ALLOW',
44 badpolicy = 'DMARC_BAD_POLICY',
45 dnsfail = 'DMARC_DNSFAIL',
46 na = 'DMARC_NA',
47 reject = 'DMARC_POLICY_REJECT',
48 softfail = 'DMARC_POLICY_SOFTFAIL',
49 quarantine = 'DMARC_POLICY_QUARANTINE',
50 },
51 no_sampling_domains = nil,
52 no_reporting_domains = nil,
53 reporting = {
54 report_local_controller = false, -- Store reports for local/controller scans (for testing only)
55 redis_keys = {
56 index_prefix = 'dmarc_idx',
57 report_prefix = 'dmarc_rpt',
58 join_char = ';',
59 },
60 helo = 'rspamd.localhost',
61 smtp = '127.0.0.1',
62 smtp_port = 25,
63 retries = 2,
64 from_name = 'Rspamd',
65 msgid_from = 'rspamd',
66 enabled = false,
67 max_entries = 1000,
68 keys_expire = 172800,
69 only_domains = nil,
70 },
71 actions = {},
72 }
73
74
75 -- Returns a key used to be inserted into dmarc report sample
76 exports.dmarc_report = function(task, settings, data)
77 local rspamd_lua_utils = require "lua_util"
78 local E = {}
79
80 local ip = task:get_from_ip()
81 if not ip or not ip:is_valid() then
82 rspamd_logger.infox(task, 'cannot store dmarc report for %s: no valid source IP',
83 data.domain)
84 return nil
85 end
86
87 ip = ip:to_string()
88
89 if rspamd_lua_utils.is_rspamc_or_controller(task) and not settings.reporting.report_local_controller then
90 rspamd_logger.infox(task, 'cannot store dmarc report for %s from IP %s: has come from controller/rspamc',
91 data.domain, ip)
92 return
93 end
94
95 local dkim_pass = table.concat(data.dkim_results.pass or E, '|')
96 local dkim_fail = table.concat(data.dkim_results.fail or E, '|')
97 local dkim_temperror = table.concat(data.dkim_results.temperror or E, '|')
98 local dkim_permerror = table.concat(data.dkim_results.permerror or E, '|')
99 local disposition_to_return = data.disposition
100 local res = table.concat({
101 ip, data.spf_ok, data.dkim_ok,
102 disposition_to_return, (data.sampled_out and 'sampled_out' or ''), data.domain,
103 dkim_pass, dkim_fail, dkim_temperror, dkim_permerror, data.spf_domain, data.spf_result }, ',')
104
105 return res
106 end
107
108 exports.gen_munging_callback = function(munging_opts, settings)
109 local lua_mime = require "lua_mime"
110 return function(task)
111 if munging_opts.mitigate_allow_only then
112 if not task:has_symbol(settings.symbols.allow) then
113 lua_util.debugm(N, task, 'skip munging, no %s symbol',
114 settings.symbols.allow)
115 -- Excepted
116 return
117 end
118 else
119 local has_dmarc = task:has_symbol(settings.symbols.allow) or
120 task:has_symbol(settings.symbols.quarantine) or
121 task:has_symbol(settings.symbols.reject) or
122 task:has_symbol(settings.symbols.softfail)
123
124 if not has_dmarc then
125 lua_util.debugm(N, task, 'skip munging, no %s symbol',
126 settings.symbols.allow)
127 -- Excepted
128 return
129 end
130 end
131 if munging_opts.mitigate_strict_only then
132 local s = task:get_symbol(settings.symbols.allow) or { [1] = {} }
133 local sopts = s[1].options or {}
134
135 local seen_strict
136 for _, o in ipairs(sopts) do
137 if o == 'reject' or o == 'quarantine' then
138 seen_strict = true
139 break
140 end
141 end
142
143 if not seen_strict then
144 lua_util.debugm(N, task, 'skip munging, no strict policy found in %s',
145 settings.symbols.allow)
146 -- Excepted
147 return
148 end
149 end
150 if munging_opts.munge_map_condition then
151 local accepted, trace = munging_opts.munge_map_condition:process(task)
152 if not accepted then
153 lua_util.debugm(N, task, 'skip munging, maps condition not satisfied: (%s)',
154 trace)
155 -- Excepted
156 return
157 end
158 end
159 -- Now, look for domain for munging
160 local mr = task:get_recipients({ 'mime', 'orig' })
161 local rcpt_found
162 if mr then
163 for _, r in ipairs(mr) do
164 if r.domain and munging_opts.list_map:get_key(r.addr) then
165 rcpt_found = r
166 break
167 end
168 end
169 end
170
171 if not rcpt_found then
172 lua_util.debugm(N, task, 'skip munging, recipients are not in list_map')
173 -- Excepted
174 return
175 end
176
177 local from = task:get_from({ 'mime', 'orig' })
178
179 if not from or not from[1] then
180 lua_util.debugm(N, task, 'skip munging, from is bad')
181 -- Excepted
182 return
183 end
184
185 from = from[1]
186 local via_user = rcpt_found.user
187 local via_addr = rcpt_found.addr
188 local via_name
189
190 if from.name == "" then
191 via_name = string.format('%s via %s', from.user or 'unknown', via_user)
192 else
193 via_name = string.format('%s via %s', from.name, via_user)
194 end
195
196 local via_from_folded = lua_util.fold_header_with_encoding(task, 'From',
197 string.format('%s <%s>', via_name, via_addr), { structured = true })
198 local orig_from_folded = lua_util.fold_header_with_encoding(task, 'X-Original-From',
199 string.format('%s <%s>', from.name or '', from.addr), { structured = true })
200 local add_hdrs = {
201 ['From'] = { order = 1, value = via_from_folded },
202 ['X-Original-From'] = { order = 0, value = orig_from_folded },
203 }
204 local remove_hdrs = { ['From'] = 0 }
205
206 local nreply = from.addr
207 if munging_opts.reply_goes_to_list then
208 -- Reply-to goes to the list
209 nreply = via_addr
210 end
211
212 if task:has_header('Reply-To') then
213 -- If we have reply-to header, then we need to insert an additional
214 -- address there
215 local orig_reply = task:get_header_full('Reply-To')[1]
216 if orig_reply.value then
217 nreply = string.format('%s, %s', orig_reply.value, nreply)
218 end
219 remove_hdrs['Reply-To'] = 1
220 end
221
222 add_hdrs['Reply-To'] = { order = 0, value = nreply }
223
224 lua_mime.modify_headers(task, {
225 remove = remove_hdrs,
226 add = add_hdrs
227 })
228 lua_util.debugm(N, task, 'munged DMARC header for %s: %s -> %s',
229 from.domain, via_from_folded, from.addr)
230 rspamd_logger.infox(task, 'munged DMARC header for %s', from.addr)
231 task:insert_result('DMARC_MUNGED', 1.0, from.addr)
232 end
233 end
234
235 local function gen_dmarc_grammar()
236 local lpeg = require "lpeg"
237 lpeg.locale(lpeg)
238 local space = lpeg.space ^ 0
239 local name = lpeg.C(lpeg.alpha ^ 1)
240 local sep = space * lpeg.S("\\;") * space
241 local value = lpeg.C(((lpeg.space ^ 1 * lpeg.graph + lpeg.graph) - sep) ^ 1)
242 local pair = lpeg.Cg(name * "=" * space * value) * sep ^ -1
243 local list = lpeg.Cf(lpeg.Ct("") * pair ^ 0, rawset)
244 local version = "v" * space * "=" * space * "DMARC1"
245 local record = version * sep * list
246
247 return record
248 end
249
250 local dmarc_grammar = gen_dmarc_grammar()
251
252 local function dmarc_key_value_case(elts)
253 if type(elts) ~= "table" then
254 return elts
255 end
256 local result = {}
257 for k, v in pairs(elts) do
258 k = k:lower()
259 if k ~= "v" then
260 v = v:lower()
261 end
262
263 result[k] = v
264 end
265
266 return result
267 end
268
269 --[[
270 -- Used to check dmarc record, check elements and produce dmarc policy processed
271 -- result.
272 -- Returns:
273 -- false,false - record is garbage
274 -- false,error_message - record is invalid
275 -- true,policy_table - record is valid and parsed
276 ]]
277 local function dmarc_check_record(log_obj, record, is_tld)
278 local failed_policy
279 local result = {
280 dmarc_policy = 'none'
281 }
282
283 local elts = dmarc_grammar:match(record)
284 lua_util.debugm(N, log_obj, "got DMARC record: %s, tld_flag=%s, processed=%s",
285 record, is_tld, elts)
286
287 if elts then
288 elts = dmarc_key_value_case(elts)
289
290 local dkim_pol = elts['adkim']
291 if dkim_pol then
292 if dkim_pol == 's' then
293 result.strict_dkim = true
294 elseif dkim_pol ~= 'r' then
295 failed_policy = 'adkim tag has invalid value: ' .. dkim_pol
296 return false, failed_policy
297 end
298 end
299
300 local spf_pol = elts['aspf']
301 if spf_pol then
302 if spf_pol == 's' then
303 result.strict_spf = true
304 elseif spf_pol ~= 'r' then
305 failed_policy = 'aspf tag has invalid value: ' .. spf_pol
306 return false, failed_policy
307 end
308 end
309
310 local policy = elts['p']
311 if policy then
312 if (policy == 'reject') then
313 result.dmarc_policy = 'reject'
314 elseif (policy == 'quarantine') then
315 result.dmarc_policy = 'quarantine'
316 elseif (policy ~= 'none') then
317 failed_policy = 'p tag has invalid value: ' .. policy
318 return false, failed_policy
319 end
320 end
321
322 -- Adjust policy if we are in tld mode
323 local subdomain_policy = elts['sp']
324 if elts['sp'] and is_tld then
325 result.subdomain_policy = elts['sp']
326
327 if (subdomain_policy == 'reject') then
328 result.dmarc_policy = 'reject'
329 elseif (subdomain_policy == 'quarantine') then
330 result.dmarc_policy = 'quarantine'
331 elseif (subdomain_policy == 'none') then
332 result.dmarc_policy = 'none'
333 elseif (subdomain_policy ~= 'none') then
334 failed_policy = 'sp tag has invalid value: ' .. subdomain_policy
335 return false, failed_policy
336 end
337 end
338 result.pct = elts['pct']
339 if result.pct then
340 result.pct = tonumber(result.pct)
341 end
342
343 if elts.rua then
344 result.rua = elts['rua']
345 end
346 result.raw_elts = elts
347 else
348 return false, false -- Ignore garbage
349 end
350
351 return true, result
352 end
353
354 exports.dmarc_check_record = dmarc_check_record
355
356 return exports