Mail/Qmail/Filter/DMARC.pm

filter: Statementstmt 35.5% 16 / 45 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 32.8% 21 / 64 CC 6 SCAR 32.4
CC
6
Cov
29%
filter
42.0
BEGIN
1.0
BEGIN
1.0
CRAP 25.6
« Mail/Qmail/Filter/SpamAssassin.pm ↑ Summary Mail/Qmail/Filter/ClamAV.pm »
1 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Mail::Qmail::Filter::DMARC;
5  
6 1 our $VERSION = '1.21';
7  
8 sub domain {
9 shift =~ s/.*\@//r;
10 }
11  
12 sub if_set {
13 my ( $key, $value, @additional_checks ) = @_;
14 return unless defined $value && length $value;
15 $_->($value) or return for @additional_checks;
16 $key => $value;
17 }
18  
19 sub is_valid_domain {
20 require Mail::DMARC::Base;
21 Mail::DMARC::Base->new->is_valid_domain(shift);
22 }
23  
24 sub spf_query {
25 require Mail::SPF;
26 my $request = Mail::SPF::Request->new(@_);
27 state $server = Mail::SPF::Server->new;
28 $server->process($request);
29 }
30  
31 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶use Mo qw(coerce default);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 extends 'Mail::Qmail::Filter';
35  
36 1 has 'dry_run';
37 1 has 'reject_text' => 'Failed DMARC test.';
38  
39 0 ▶sub filter {
Subroutine filter: not called CC=6 SCAR=37.4
Pod documented
40 0 my $self = shift;
41 0 my $message = $self->message;
42  
43 0 require Mail::DKIM::Verifier; # lazy load because filter might be skipped
44 0 my $dkim = Mail::DKIM::Verifier->new;
45 0 $dkim->PRINT( $message->body =~ s/\cM?\cJ/\cM\cJ/gr );
46 0 $dkim->CLOSE;
47 0 $self->debug( 'DKIM result' => $dkim->result );
48  
49 0 ▶ if ( $dkim->result ne 'pass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim->result ne "pass") 0 0 0
50  
51 0 $self->debug( 'Remote IP' => $ENV{TCPREMOTEIP} );
52  
53 0 my %spf_query = ( ip_address => $ENV{TCPREMOTEIP} );
54  
55 0 $self->debug( helo => $spf_query{helo_identity} = $message->helo );
56  
57 0 my $header_from = $message->header_from;
58 0 my $header_from_domain;
59 0 ▶ if ($header_from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_from) { } 0 0 0
60 $self->debug( 'RFC5322.From' => $spf_query{identity} =
61 0 $header_from->address );
62 0 $header_from_domain = $header_from->host;
63 0 $spf_query{scope} = 'mfrom';
64 }
65 else {
66 0 $spf_query{scope} = 'helo';
67  
68 # identity required by Mail::SPF:
69 0 $spf_query{identity} = $spf_query{helo_identity};
70 }
71  
72 0 $self->debug( 'SPF result' => my $spf_result = spf_query(%spf_query) );
73 0 $message->add_header( $spf_result->received_spf_header );
74  
75 0 require Mail::DMARC::PurePerl;
76 my $dmarc_text = (
77 my $dmarc_result = Mail::DMARC::PurePerl->new(
78 source_ip => $ENV{TCPREMOTEIP},
79 envelope_to => domain( ( $message->to )[0] ),
80 if_set(
81 envelope_from => domain( $message->from ),
82 \&is_valid_domain
83 ),
84 if_set(
85 header_from => $header_from_domain,
86 \&is_valid_domain
87 ),
88 dkim => $dkim,
89 spf => {
90 if_set( domain => $header_from_domain ),
91 scope => $spf_query{scope},
92 0 result => $spf_result->code,
93 },
94 )->validate
95 )->result;
96 0 $self->debug( 'DMARC result' => $dmarc_text );
97 0 $message->add_header("DMARC-Status: $dmarc_text");
98  
99 0 ▶ if ( $dmarc_result->result ne 'pass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc_result->result ne "pass") 0 0 0
100 0 my $disposition = $dmarc_result->disposition;
101 0 $self->debug( 'DMARC disposition' => $disposition );
102 0 ▶ $self->reject( $self->reject_text )
Branch: 1 branch
BranchTrueFalseTotal
if $disposition eq "reject" and not $self->dry_run 0 0 0
Condition0%0/3
$disposition eq "reject" and not $self->dry_run
!ll&&!rl&&r
000
MC/DC0%0/2
$disposition eq "reject" and not $self->dry_run
$disposition eq "reject" not $self->dry_run
Truth table0%0/3
A and not(B)
A= $disposition eq "reject"B= not $self->dry_run
ABresult
0X0
100
111
103 if $disposition eq 'reject' && !$self->dry_run;
104 }
105 }
106 }
107  
108 1 1;
109  
110 __END__
« Mail/Qmail/Filter/SpamAssassin.pm ↑ Summary Mail/Qmail/Filter/ClamAV.pm »