| 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.33'; | |||||||||||||||||||||||||||
| 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=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 41 | 0 | my $message = $self->message; | |||||||||||||||||||||||||||
| 42 | 0 | my $remote_ip = $message->remote_ip; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | $self->debug( 'Remote IP' => $remote_ip ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my %spf_query = ( ip_address => $remote_ip ); | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | my $dmarc = eval { | ||||||||||||||||||||||||||||
| 78 | Mail::DMARC::PurePerl->new( | ||||||||||||||||||||||||||||
| 79 | source_ip => $remote_ip, | ||||||||||||||||||||||||||||
| 80 | envelope_to => domain( ( $message->to )[0] ), | ||||||||||||||||||||||||||||
| 81 | if_set( | ||||||||||||||||||||||||||||
| 82 | envelope_from => domain( $message->from ), | ||||||||||||||||||||||||||||
| 83 | \&is_valid_domain | ||||||||||||||||||||||||||||
| 84 | ), | ||||||||||||||||||||||||||||
| 85 | if_set( | ||||||||||||||||||||||||||||
| 86 | header_from => $header_from_domain, | ||||||||||||||||||||||||||||
| 87 | \&is_valid_domain | ||||||||||||||||||||||||||||
| 88 | ), | ||||||||||||||||||||||||||||
| 89 | dkim => $dkim, | ||||||||||||||||||||||||||||
| 90 | spf => { | ||||||||||||||||||||||||||||
| 91 | if_set( domain => $header_from_domain ), | ||||||||||||||||||||||||||||
| 92 | scope => $spf_query{scope}, | ||||||||||||||||||||||||||||
| 93 | 0 | result => $spf_result->code, | |||||||||||||||||||||||||||
| 94 | }, | ||||||||||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | ) | ||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 0 | my $dmarc_text = ( my $dmarc_result = $dmarc->validate )->result; | |||||||||||||||||||||||||||
| 100 | 0 | $self->debug( 'DMARC result' => $dmarc_text ); | |||||||||||||||||||||||||||
| 101 | 0 | $message->add_header("DMARC-Status: $dmarc_text"); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( $dmarc_text ne 'pass' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | my $disposition = $dmarc_result->disposition; | |||||||||||||||||||||||||||
| 105 | 0 | $self->debug( 'DMARC disposition' => $disposition ); | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | $self->reject( $self->reject_text ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$disposition eq "reject" and not $self->dry_run
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
| |||||||||||||||||||||||||||||
| 107 | if $disposition eq 'reject' && !$self->dry_run; | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||
| 111 | 0 | chomp( my $error = $@ ); | |||||||||||||||||||||||||||
| 112 | 0 | $error =~ y/\n/ /; | |||||||||||||||||||||||||||
| 113 | 0 | $self->debug("DMARC test failed: $error"); | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | $self->reject( $self->reject_text ) unless $self->dry_run; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | 1; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||