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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
|
--[[
Copyright (c) 2015-2026, Vsevolod Stakhov <vsevolod@rspamd.com>
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.
]] --
-- Dmarc policy filter
local rspamd_logger = require "rspamd_logger"
local rspamd_util = require "rspamd_util"
local lua_redis = require "lua_redis"
local lua_util = require "lua_util"
local dmarc_common = require "plugins/dmarc"
if confighelp then
return
end
local N = 'dmarc'
local settings = dmarc_common.default_settings
local redis_params = nil
local E = {}
-- Keys:
-- 1 = index key (string)
-- 2 = report key (string)
-- 3 = max report elements (number)
-- 4 = expiry time for elements (number)
-- Arguments
-- 1 = dmarc domain
-- 2 = dmarc report
local take_report_id
local take_report_script = [[
local index_key = KEYS[1]
local report_key = KEYS[2]
local max_entries = -(tonumber(KEYS[3]) + 1)
local keys_expiry = math.floor(tonumber(KEYS[4]) or 172800)
local dmarc_domain = ARGV[1]
local report = ARGV[2]
redis.call('SADD', index_key, report_key)
redis.call('EXPIRE', index_key, keys_expiry)
redis.call('ZINCRBY', report_key, 1, report)
redis.call('ZREMRANGEBYRANK', report_key, 0, max_entries)
redis.call('EXPIRE', report_key, keys_expiry)
]]
-- Publish the policy evaluation result so that other modules (e.g. fuzzy_check)
-- can use it without matching our symbol names, which are configurable.
-- Note that we report the *published* policy rather than the disposition we
-- ended up applying: sampling (pct=) and local overrides describe us, not the
-- sender.
local function publish_result(task, result)
if result then
task:get_mempool():set_variable('dmarc_result', result)
end
end
local function maybe_force_action(task, disposition)
if disposition then
local force_action = settings.actions[disposition]
if force_action then
-- Set least action
task:set_pre_result(force_action, 'Action set by DMARC', N, nil, nil, 'least')
end
end
end
-- Returns the A-label form of a domain that has U-labels: the From header
-- field of an EAI message has the latter, signatures and SMTP have either,
-- and RFC 8616 says that they are one domain
local function domain_to_alabel(task, domain)
if domain and domain:find('[\128-\255]') then
domain = task:get_resolver():idna_convert_utf8(domain, task:get_mempool())
-- What does not convert comes back with U+FFFD
if not domain or domain:find('[\128-\255]') then
return nil
end
end
return domain
end
local function dmarc_validate_policy(task, policy, hdrfromdom, dmarc_esld)
local reason = {}
-- Check dkim and spf symbols
local spf_ok = false
local dkim_ok = false
local spf_tmpfail = false
local dkim_tmpfail = false
-- SPF alignment is checked against the envelope domain as it was seen in
-- the message, before any rewrite (e.g. by the aliases module)
local spf_domain = ((task:get_from({ 'smtp', 'orig' }) or E)[1] or E).domain
if not spf_domain or spf_domain == '' then
spf_domain = task:get_helo() or ''
end
-- The From domain is in its A-label form here
spf_domain = domain_to_alabel(task, spf_domain) or ''
if task:has_symbol(settings.symbols['spf_allow_symbol']) then
if policy.strict_spf then
if rspamd_util.strequal_caseless(spf_domain, hdrfromdom) then
spf_ok = true
else
table.insert(reason, "SPF not aligned (strict)")
end
else
local spf_tld = rspamd_util.get_tld(spf_domain)
if rspamd_util.strequal_caseless(spf_tld, dmarc_esld) then
spf_ok = true
else
table.insert(reason, "SPF not aligned (relaxed)")
end
end
else
if task:has_symbol(settings.symbols['spf_tempfail_symbol']) then
if policy.strict_spf then
if rspamd_util.strequal_caseless(spf_domain, hdrfromdom) then
spf_tmpfail = true
end
else
local spf_tld = rspamd_util.get_tld(spf_domain)
if rspamd_util.strequal_caseless(spf_tld, dmarc_esld) then
spf_tmpfail = true
end
end
end
table.insert(reason, "No valid SPF")
end
local opts = ((task:get_symbol('DKIM_TRACE') or E)[1] or E).options
local dkim_results = {
pass = {},
temperror = {},
permerror = {},
fail = {},
}
if opts then
dkim_results.pass = {}
for _, opt in ipairs(opts) do
local check_res = string.sub(opt, -1)
local domain = string.sub(opt, 1, -3):lower()
if check_res == '+' then
table.insert(dkim_results.pass, domain)
elseif check_res == '?' then
table.insert(dkim_results.temperror, domain)
elseif check_res == '-' then
table.insert(dkim_results.fail, domain)
else
table.insert(dkim_results.permerror, domain)
end
end
-- The dkim module has already compared the signing domains against the
-- From domain; it knows which signatures verified, so it is the one place
-- where that comparison belongs. `relaxed` here means relaxed and not
-- strict, so a strict policy only accepts the strict verdict.
local mpool = task:get_mempool()
local function aligned(sense)
if not sense then
return false
end
if policy.strict_dkim then
return sense == 'strict'
end
return true
end
dkim_ok = aligned(mpool:get_variable('dkim_alignment', 'string'))
if not dkim_ok then
dkim_tmpfail = aligned(mpool:get_variable('dkim_alignment_tempfail', 'string'))
if next(dkim_results.pass) ~= nil then
table.insert(reason, policy.strict_dkim and "DKIM not aligned (strict)"
or "DKIM not aligned (relaxed)")
end
end
else
table.insert(reason, "No valid DKIM")
end
lua_util.debugm(N, task,
"validated dmarc policy for %s: %s; dkim_ok=%s, dkim_tempfail=%s, spf_ok=%s, spf_tempfail=%s",
policy.domain, policy.dmarc_policy,
dkim_ok, dkim_tmpfail,
spf_ok, spf_tmpfail)
local disposition = 'none'
local sampled_out = false
-- One "domain : reason" option per failure reason followed by the extra
-- options, so that reasons never need to be joined with commas
local function failure_options(reasons, ...)
local result = {}
for _, r in ipairs(reasons) do
result[#result + 1] = policy.domain .. ' : ' .. r
end
for _, extra in ipairs({ ... }) do
result[#result + 1] = extra
end
return result
end
local function handle_dmarc_failure(what, reasons)
if not policy.pct or policy.pct == 100 then
task:insert_result(settings.symbols[what], 1.0,
failure_options(reasons, policy.dmarc_policy))
disposition = what
else
local coin = math.random(100)
if (coin > policy.pct) then
if (not settings.no_sampling_domains or
not settings.no_sampling_domains:get_key(policy.domain)) then
if what == 'reject' then
disposition = 'quarantine'
else
disposition = 'softfail'
end
task:insert_result(settings.symbols[disposition], 1.0,
failure_options(reasons, policy.dmarc_policy, "sampled_out"))
sampled_out = true
lua_util.debugm(N, task,
'changed dmarc policy from %s to %s, sampled out: %s < %s',
what, disposition, coin, policy.pct)
else
task:insert_result(settings.symbols[what], 1.0,
failure_options(reasons, policy.dmarc_policy, "local_policy"))
disposition = what
end
else
task:insert_result(settings.symbols[what], 1.0,
failure_options(reasons, policy.dmarc_policy))
disposition = what
end
end
maybe_force_action(task, disposition)
end
if spf_ok or dkim_ok then
--[[
https://tools.ietf.org/html/rfc7489#section-6.6.2
DMARC evaluation can only yield a "pass" result after one of the
underlying authentication mechanisms passes for an aligned
identifier.
]] --
task:insert_result(settings.symbols['allow'], 1.0, policy.domain,
policy.dmarc_policy)
publish_result(task, 'pass')
else
--[[
https://tools.ietf.org/html/rfc7489#section-6.6.2
If neither passes and one or both of them fail due to a
temporary error, the Receiver evaluating the message is unable to
conclude that the DMARC mechanism had a permanent failure; they
therefore cannot apply the advertised DMARC policy.
]] --
if spf_tmpfail or dkim_tmpfail then
task:insert_result(settings.symbols['dnsfail'], 1.0, policy.domain ..
' : ' .. 'SPF/DKIM temp error', policy.dmarc_policy)
publish_result(task, 'temperror')
else
-- We can now check the failed policy and maybe send report data elt
if policy.dmarc_policy == 'quarantine' or policy.dmarc_policy == 'reject' then
publish_result(task, 'fail_' .. policy.dmarc_policy)
else
publish_result(task, 'fail_none')
end
if policy.dmarc_policy == 'quarantine' then
handle_dmarc_failure('quarantine', reason)
elseif policy.dmarc_policy == 'reject' then
handle_dmarc_failure('reject', reason)
else
task:insert_result(settings.symbols['softfail'], 1.0,
failure_options(reason, policy.dmarc_policy))
end
end
end
if policy.rua and redis_params and settings.reporting.enabled then
-- Helper function to perform the actual report generation
local function generate_dmarc_report()
local function dmarc_report_cb(err)
if not err then
rspamd_logger.infox(task, 'dmarc report saved for %s (rua = %s)',
hdrfromdom, policy.rua)
else
rspamd_logger.errx(task, 'dmarc report is not saved for %s: %s',
hdrfromdom, err)
end
end
local spf_result
if spf_ok then
spf_result = 'pass'
elseif spf_tmpfail then
spf_result = 'temperror'
else
if task:has_symbol(settings.symbols.spf_deny_symbol) then
spf_result = 'fail'
elseif task:has_symbol(settings.symbols.spf_softfail_symbol) then
spf_result = 'softfail'
elseif task:has_symbol(settings.symbols.spf_neutral_symbol) then
spf_result = 'neutral'
elseif task:has_symbol(settings.symbols.spf_permfail_symbol) then
spf_result = 'permerror'
else
spf_result = 'none'
end
end
-- Prepare and send redis report element
-- math.floor is required: task:get_date returns a fractional double and
-- PUC-Rio Lua 5.3+ rejects non-integer floats as the second arg to os.date
local period = os.date('%Y%m%d',
math.floor(task:get_date({ format = 'connect', gmt = false })))
-- Dmarc domain key must include dmarc domain, rua and period
local dmarc_domain_key = table.concat(
{ settings.reporting.redis_keys.report_prefix, policy.domain, policy.rua, period },
settings.reporting.redis_keys.join_char)
local report_data = dmarc_common.dmarc_report(task, settings, {
spf_ok = spf_ok and 'pass' or 'fail',
dkim_ok = dkim_ok and 'pass' or 'fail',
disposition = (disposition == "softfail") and "none" or disposition,
sampled_out = sampled_out,
domain = hdrfromdom,
spf_domain = spf_domain,
dkim_results = dkim_results,
spf_result = spf_result
})
local idx_key = table.concat({ settings.reporting.redis_keys.index_prefix, period },
settings.reporting.redis_keys.join_char)
if report_data then
lua_redis.exec_redis_script(take_report_id,
{ task = task, is_write = true },
dmarc_report_cb,
{ idx_key, dmarc_domain_key,
tostring(settings.reporting.max_entries), tostring(settings.reporting.keys_expire) },
{ hdrfromdom, report_data })
end
end
-- Helper function to check a map with support for both sync and external maps
local function check_map(map_obj, key, continue_cb, suppress_reason)
if not map_obj then
-- No map configured, continue
continue_cb()
return
end
if map_obj.__external then
-- External map, use async callback
map_obj:get_key(key, function(found, _, _, _)
if found then
rspamd_logger.infox(task, 'DMARC reporting suppressed: %s', suppress_reason)
else
continue_cb()
end
end, task)
else
-- Regular map, synchronous check
if map_obj:get_key(key) then
rspamd_logger.infox(task, 'DMARC reporting suppressed: %s', suppress_reason)
else
continue_cb()
end
end
end
-- Forward declarations for chain of exclusion checks
local check_only_domains, check_exclude_domains, check_exclude_recipients, check_exclude_rua
-- Chain exclusion checks together
check_only_domains = function()
if settings.reporting.only_domains then
if settings.reporting.only_domains.__external then
-- Check both domain and TLD for external maps
settings.reporting.only_domains:get_key(policy.domain, function(found1, _, _, _)
if found1 then
check_exclude_domains()
else
settings.reporting.only_domains:get_key(rspamd_util.get_tld(policy.domain), function(found2, _, _, _)
if found2 then
check_exclude_domains()
else
rspamd_logger.infox(task, 'DMARC reporting suppressed for sender domain %s (not in only_domains)', policy.domain)
end
end, task)
end
end, task)
else
-- Synchronous check for regular maps
if settings.reporting.only_domains:get_key(policy.domain) or
settings.reporting.only_domains:get_key(rspamd_util.get_tld(policy.domain)) then
check_exclude_domains()
else
rspamd_logger.infox(task, 'DMARC reporting suppressed for sender domain %s (not in only_domains)', policy.domain)
end
end
else
check_exclude_domains()
end
end
check_exclude_domains = function()
if settings.reporting.exclude_domains then
if settings.reporting.exclude_domains.__external then
-- Check both domain and TLD for external maps
settings.reporting.exclude_domains:get_key(policy.domain, function(found1, _, _, _)
if found1 then
rspamd_logger.infox(task, 'DMARC reporting suppressed for sender domain %s', policy.domain)
else
settings.reporting.exclude_domains:get_key(rspamd_util.get_tld(policy.domain), function(found2, _, _, _)
if found2 then
rspamd_logger.infox(task, 'DMARC reporting suppressed for sender domain %s', policy.domain)
else
check_exclude_recipients()
end
end, task)
end
end, task)
else
-- Synchronous check for regular maps
if settings.reporting.exclude_domains:get_key(policy.domain) or
settings.reporting.exclude_domains:get_key(rspamd_util.get_tld(policy.domain)) then
rspamd_logger.infox(task, 'DMARC reporting suppressed for sender domain %s', policy.domain)
else
check_exclude_recipients()
end
end
else
check_exclude_recipients()
end
end
check_exclude_recipients = function()
if settings.reporting.exclude_recipients then
local rcpt = task:get_principal_recipient()
if rcpt then
check_map(settings.reporting.exclude_recipients, rcpt, check_exclude_rua,
string.format('recipient %s', rcpt))
else
check_exclude_rua()
end
else
check_exclude_rua()
end
end
check_exclude_rua = function()
if policy.rua:match("^mailto:") and settings.reporting.exclude_rua_addresses then
local rua = policy.rua:gsub("^mailto:", "")
check_map(settings.reporting.exclude_rua_addresses, rua, generate_dmarc_report,
string.format('rua recipient %s', rua))
else
generate_dmarc_report()
end
end
-- Start the exclusion check chain
check_only_domains()
end
end
local function dmarc_callback(task)
-- DMARC must evaluate the RFC5322.From domain as it was seen in the
-- message; rewrites done via task:set_from (e.g. by the aliases module)
-- must not affect alignment, policy lookup or reporting
local from = task:get_from({ 'mime', 'orig' })
-- A U-label domain is handled as its A-label: this is what the alignment
-- compares, what is looked up in DNS and what goes to the reports
local hfromdom = domain_to_alabel(task, ((from or E)[1] or E).domain)
local dmarc_domain
local ip_addr = task:get_ip()
local dmarc_checks = task:get_mempool():get_variable('dmarc_checks', 'double') or 0
local seen_invalid = false
if dmarc_checks ~= 2 then
rspamd_logger.infox(task, "skip DMARC checks as either SPF or DKIM were not checked")
return
end
if lua_util.is_skip_local_or_authed(task, settings.auth_and_local_conf, ip_addr) then
rspamd_logger.infox(task, "skip DMARC checks for local networks and authorized users")
return
end
-- Do some initial sanity checks, detect tld domain if different
if hfromdom and hfromdom ~= '' and not (from or E)[2] then
-- Lowercase domain as per #3940
hfromdom = hfromdom:lower()
dmarc_domain = rspamd_util.get_tld(hfromdom)
elseif (from or E)[2] then
task:insert_result(settings.symbols['na'], 1.0, 'Duplicate From header')
publish_result(task, 'no_record')
return maybe_force_action(task, 'na')
elseif (from or E)[1] then
task:insert_result(settings.symbols['na'], 1.0, 'No domain in From header')
publish_result(task, 'no_record')
return maybe_force_action(task, 'na')
else
task:insert_result(settings.symbols['na'], 1.0, 'No From header')
publish_result(task, 'no_record')
return maybe_force_action(task, 'na')
end
local dns_checks_inflight = 0
local dmarc_domain_policy = {}
local dmarc_tld_policy = {}
local function process_dmarc_policy(policy, final)
lua_util.debugm(N, task, "validate DMARC policy (final=%s): %s",
true, policy)
if policy.err and policy.symbol then
-- In case of fatal errors or final check for tld, we give up and
-- insert result
if final or policy.fatal then
task:insert_result(policy.symbol, 1.0, policy.err)
publish_result(task, policy.result)
maybe_force_action(task, policy.disposition)
return true
end
elseif policy.dmarc_policy then
dmarc_validate_policy(task, policy, hfromdom, dmarc_domain)
return true -- We have a more specific version, use it
end
return false -- Missing record
end
local function gen_dmarc_cb(lookup_domain, is_tld)
local policy_target = dmarc_domain_policy
if is_tld then
policy_target = dmarc_tld_policy
end
return function(_, _, results, err)
dns_checks_inflight = dns_checks_inflight - 1
if not seen_invalid then
policy_target.domain = lookup_domain
if err then
if (err ~= 'requested record is not found' and
err ~= 'no records with this name') then
policy_target.err = lookup_domain .. ' : ' .. err
policy_target.symbol = settings.symbols['dnsfail']
policy_target.result = 'temperror'
else
policy_target.err = lookup_domain
policy_target.symbol = settings.symbols['na']
policy_target.result = 'no_record'
end
else
local has_valid_policy = false
for _, rec in ipairs(results) do
local ret, results_or_err = dmarc_common.dmarc_check_record(task, rec, is_tld)
if not ret then
if results_or_err then
-- We have a fatal parsing error, give up
policy_target.err = lookup_domain .. ' : ' .. results_or_err
policy_target.symbol = settings.symbols['badpolicy']
policy_target.result = 'permerror'
policy_target.fatal = true
seen_invalid = true
end
else
if has_valid_policy then
policy_target.err = lookup_domain .. ' : ' ..
'Multiple policies defined in DNS'
policy_target.symbol = settings.symbols['badpolicy']
policy_target.result = 'permerror'
policy_target.fatal = true
seen_invalid = true
end
has_valid_policy = true
for k, v in pairs(results_or_err) do
policy_target[k] = v
end
end
end
if not has_valid_policy and not seen_invalid then
policy_target.err = lookup_domain .. ':' .. ' no valid DMARC record'
policy_target.symbol = settings.symbols['na']
policy_target.result = 'no_record'
end
end
end
if dns_checks_inflight == 0 then
lua_util.debugm(N, task, "finished DNS queries, validate policies")
-- We have checked both tld and real domain (if different)
if not process_dmarc_policy(dmarc_domain_policy, false) then
-- Try tld policy as well
if not process_dmarc_policy(dmarc_tld_policy, true) then
process_dmarc_policy(dmarc_domain_policy, true)
end
end
end
end
end
local resolve_name = '_dmarc.' .. hfromdom
-- A request that cannot be made has no callback: a name too long for a
-- query, or a label over 63 bytes, which the domain of a From header can
-- be. It is not counted then, or the answer for the other name would wait
-- for it and the policy of the organisational domain would not be applied
local domain_cb = gen_dmarc_cb(hfromdom, false)
local tld_cb = dmarc_domain ~= hfromdom and gen_dmarc_cb(dmarc_domain, true)
dns_checks_inflight = dns_checks_inflight + 1
local domain_asked = task:get_resolver():resolve_txt({
task = task,
name = resolve_name,
callback = domain_cb,
forced = true
})
if tld_cb then
dns_checks_inflight = dns_checks_inflight + 1
if not task:get_resolver():resolve_txt({
task = task,
name = '_dmarc.' .. dmarc_domain,
callback = tld_cb,
forced = true
}) then
-- As if the name did not exist
tld_cb(nil, nil, nil, 'no records with this name')
end
end
if not domain_asked then
domain_cb(nil, nil, nil, 'no records with this name')
end
end
local opts = rspamd_config:get_all_opt('dmarc')
settings = lua_util.override_defaults(settings, opts)
settings.auth_and_local_conf = lua_util.config_check_local_or_authed(rspamd_config, N,
false, false)
-- Legacy...
if settings.reporting and not settings.reporting.exclude_domains and settings.no_reporting_domains then
settings.reporting.exclude_domains = settings.no_reporting_domains
end
local lua_maps = require "lua_maps"
lua_maps.fill_config_maps(N, settings, {
no_sampling_domains = {
optional = true,
type = 'map',
description = 'Domains not to apply DMARC sampling to'
},
})
if type(settings.reporting) == 'table' then
lua_maps.fill_config_maps(N, settings.reporting, {
exclude_domains = {
optional = true,
type = 'map',
description = 'Domains not to store DMARC reports about'
},
exclude_recipients = {
optional = true,
type = 'map',
description = 'Recipients not to store DMARC reports for'
},
exclude_rua_addresses = {
optional = true,
type = 'map',
description = 'RUA recipients not to store DMARC reports for'
},
only_domains = {
optional = true,
type = 'map',
description = 'Domains to store DMARC reports about'
},
})
end
if settings.reporting == true then
rspamd_logger.errx(rspamd_config, 'old style dmarc reporting is NO LONGER supported, please read the documentation')
elseif settings.reporting.enabled then
redis_params = lua_redis.parse_redis_server('dmarc', opts)
if not redis_params then
rspamd_logger.errx(rspamd_config, 'cannot parse servers parameter')
else
rspamd_logger.infox(rspamd_config, 'dmarc reporting is enabled')
take_report_id = lua_redis.add_redis_script(take_report_script, redis_params)
end
end
-- Check spf and dkim sections for changed symbols
local function check_mopt(var, m_opts, name)
if m_opts[name] then
settings.symbols[var] = tostring(m_opts[name])
end
end
local spf_opts = rspamd_config:get_all_opt('spf')
if spf_opts then
check_mopt('spf_deny_symbol', spf_opts, 'symbol_fail')
check_mopt('spf_allow_symbol', spf_opts, 'symbol_allow')
check_mopt('spf_softfail_symbol', spf_opts, 'symbol_softfail')
check_mopt('spf_neutral_symbol', spf_opts, 'symbol_neutral')
check_mopt('spf_tempfail_symbol', spf_opts, 'symbol_dnsfail')
check_mopt('spf_na_symbol', spf_opts, 'symbol_na')
end
local dkim_opts = rspamd_config:get_all_opt('dkim')
if dkim_opts then
check_mopt('dkim_deny_symbol', dkim_opts, 'symbol_reject')
check_mopt('dkim_allow_symbol', dkim_opts, 'symbol_allow')
check_mopt('dkim_tempfail_symbol', dkim_opts, 'symbol_tempfail')
check_mopt('dkim_na_symbol', dkim_opts, 'symbol_na')
end
local id = rspamd_config:register_symbol({
name = 'DMARC_CHECK',
type = 'callback',
callback = dmarc_callback
})
rspamd_config:register_symbol({
name = 'DMARC_CALLBACK', -- compatibility symbol
type = 'virtual,skip',
parent = id,
})
rspamd_config:register_symbol({
name = settings.symbols['allow'],
parent = id,
group = 'policies',
groups = { 'dmarc' },
type = 'virtual'
})
rspamd_config:register_symbol({
name = settings.symbols['reject'],
parent = id,
group = 'policies',
groups = { 'dmarc' },
type = 'virtual'
})
rspamd_config:register_symbol({
name = settings.symbols['quarantine'],
parent = id,
group = 'policies',
groups = { 'dmarc' },
type = 'virtual'
})
rspamd_config:register_symbol({
name = settings.symbols['softfail'],
parent = id,
group = 'policies',
groups = { 'dmarc' },
type = 'virtual'
})
rspamd_config:register_symbol({
name = settings.symbols['dnsfail'],
parent = id,
group = 'policies',
groups = { 'dmarc' },
type = 'virtual'
})
rspamd_config:register_symbol({
name = settings.symbols['badpolicy'],
parent = id,
group = 'policies',
groups = { 'dmarc' },
type = 'virtual'
})
rspamd_config:register_symbol({
name = settings.symbols['na'],
parent = id,
group = 'policies',
groups = { 'dmarc' },
type = 'virtual'
})
-- Weak deps: DMARC falls back to DKIM-only or SPF-only alignment if the other is absent
rspamd_config:register_dependency('DMARC_CHECK', settings.symbols['spf_allow_symbol'])
rspamd_config:register_dependency('DMARC_CHECK', settings.symbols['dkim_allow_symbol'])
-- DMARC munging support
if settings.munging then
local lua_maps_expressions = require "lua_maps_expressions"
local munging_defaults = {
reply_goes_to_list = false,
mitigate_allow_only = true, -- perform munging based on DMARC_POLICY_ALLOW only
mitigate_strict_only = false, -- perform mugning merely for reject/quarantine policies
munge_from = true, -- replace from with something like <orig name> via <rcpt user>
list_map = nil, -- map of maillist domains
munge_map_condition = nil, -- maps expression to enable munging
}
local munging_opts = lua_util.override_defaults(munging_defaults, settings.munging)
if not munging_opts.list_map then
rspamd_logger.errx(rspamd_config, 'cannot enable DMARC munging with no list_map parameter')
return
end
munging_opts.list_map = lua_maps.map_add_from_ucl(munging_opts.list_map,
'set', 'DMARC munging map of the recipients addresses to munge')
if not munging_opts.list_map then
rspamd_logger.errx(rspamd_config, 'cannot enable DMARC munging with invalid list_map (invalid map)')
return
end
if munging_opts.munge_map_condition then
munging_opts.munge_map_condition = lua_maps_expressions.create(rspamd_config,
munging_opts.munge_map_condition, N)
end
rspamd_config:register_symbol({
name = 'DMARC_MUNGED',
type = 'normal',
flags = 'nostat',
score = 0,
group = 'policies',
groups = { 'dmarc' },
callback = dmarc_common.gen_munging_callback(munging_opts, settings),
augmentations = { lua_util.dns_timeout_augmentation(rspamd_config) },
})
-- Hard: without DMARC policy there is nothing to munge
rspamd_config:register_dependency('DMARC_MUNGED', 'DMARC_CHECK', true)
-- Weak: signing works without munging, just ensures proper header rewrite order
rspamd_config:register_dependency('DKIM_SIGNED', 'DMARC_MUNGED')
rspamd_config:register_dependency('ARC_SIGNED', 'DMARC_MUNGED')
rspamd_logger.infox(rspamd_config, 'enabled DMARC munging')
end
|