| 1 | package Mail::Milter::Authentication::Handler::DKIM2Verify; | ||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use 5.20.0; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Mail::Milter::Authentication::Pragmas; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Handler class for DKIM2 signature verification | ||||||||||||||||||||||||||||||||||
| 7 | 3 | our $VERSION = '0.35'; | |||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use base 'Mail::Milter::Authentication::Handler'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Mail::DKIM2::Common qw(extract_mi_version parse_mime); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Mail::DKIM2::Key qw(parse_dkim_pubkey); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Mail::DKIM2::Verifier; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Mail::DKIM2::MessageInstance; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Mail::DKIM2::MessageStore; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Email::MIME; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use Crypt::PRNG qw(random_bytes); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | 130 | ▶ | sub default_config { | ||||||||||||||||||||||||||||||||
|
Subroutine
default_config: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 19 | return { | ||||||||||||||||||||||||||||||||||
| 20 | 130 | 'hide_none' => 0, | |||||||||||||||||||||||||||||||||
| 21 | 'extra_properties' => 0, | ||||||||||||||||||||||||||||||||||
| 22 | 'dns_overrides' => undef, # path to dns.json for testing | ||||||||||||||||||||||||||||||||||
| 23 | 'add_message_instance' => 1, # compute and add MI header on inbound | ||||||||||||||||||||||||||||||||||
| 24 | 'snapshot_directory' => undef, # store message snapshots for egress diffing | ||||||||||||||||||||||||||||||||||
| 25 | 'ignore_header_prefixes' => [], # our own fields, hashed by neither end (IgnorePrefixes) | ||||||||||||||||||||||||||||||||||
| 26 | 'check_envelope' => 1, # -09 section 14.4: MAIL FROM/RCPT TO against the top signature | ||||||||||||||||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | # The handler's configuration with default_config() filled in for every | ||||||||||||||||||||||||||||||||||
| 31 | # option left out (or undef); an explicit 0 still wins. The framework does | ||||||||||||||||||||||||||||||||||
| 32 | # not merge default_config() at runtime -- it only uses it to generate a | ||||||||||||||||||||||||||||||||||
| 33 | # sample config -- so without this add_message_instance, documented as on | ||||||||||||||||||||||||||||||||||
| 34 | # by default, was off unless set. | ||||||||||||||||||||||||||||||||||
| 35 | 130 | ▶ | sub _config { | ||||||||||||||||||||||||||||||||
|
Subroutine
_config: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 36 | 130 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 37 | 130 | ▶ | my $given = $self->handler_config() || {}; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->handler_config || {}
MC/DC0%0/1
$self->handler_config || {}
$self->handler_config
Truth table50%1/2
A
A= $self->handler_config
| |||||||||||||||||||||||||||||||||||
| 38 | 130 | my $defaults = default_config(); | |||||||||||||||||||||||||||||||||
| 39 | 130 | ▶ | return { %$given, map { $_ => $given->{$_} // $defaults->{$_} } keys %$defaults }; | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
$given->{$_} // $defaults->{$_}
MC/DC100%2/2
$given->{$_} // $defaults->{$_}
$given->{$_}
$defaults->{$_}
Truth table100%3/3
A // B
A= $given->{$_}B= $defaults->{$_}
| |||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub register_metrics { | ||||||||||||||||||||||||||||||||
|
Subroutine
register_metrics: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 43 | return { | ||||||||||||||||||||||||||||||||||
| 44 | 0 | 'dkim2_verify_total' => 'The number of emails processed for DKIM2 verification', | |||||||||||||||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | 25 | ▶ | sub envfrom_callback { | ||||||||||||||||||||||||||||||||
|
Subroutine
envfrom_callback: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 49 | 25 | my ( $self, $env_from ) = @_; | |||||||||||||||||||||||||||||||||
| 50 | 25 | $self->{'failmode'} = 0; | |||||||||||||||||||||||||||||||||
| 51 | 25 | $self->{'headers'} = []; | |||||||||||||||||||||||||||||||||
| 52 | 25 | $self->{'body'} = []; | |||||||||||||||||||||||||||||||||
| 53 | 25 | $self->{'has_dkim2'} = 0; | |||||||||||||||||||||||||||||||||
| 54 | 25 | $self->{'has_mi'} = 0; | |||||||||||||||||||||||||||||||||
| 55 | 25 | $self->{'carry'} = q{}; | |||||||||||||||||||||||||||||||||
| 56 | 25 | $self->{'env_from'} = $env_from; | |||||||||||||||||||||||||||||||||
| 57 | 25 | $self->{'env_rcpt'} = []; | |||||||||||||||||||||||||||||||||
| 58 | 25 | $self->destroy_object('dkim2_verifier'); | |||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | 22 | ▶ | sub envrcpt_callback { | ||||||||||||||||||||||||||||||||
|
Subroutine
envrcpt_callback: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 62 | 22 | my ( $self, $env_to ) = @_; | |||||||||||||||||||||||||||||||||
| 63 | 22 | push @{ $self->{'env_rcpt'} }, $env_to; | |||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | 225 | ▶ | sub header_callback { | ||||||||||||||||||||||||||||||||
|
Subroutine
header_callback: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 67 | 225 | my ( $self, $header, $value, $original ) = @_; | |||||||||||||||||||||||||||||||||
| 68 | 225 | ▶ | return if $self->{'failmode'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 69 | 225 | my $EOL = "\015\012"; | |||||||||||||||||||||||||||||||||
| 70 | 225 | my $chunk = $original . $EOL; | |||||||||||||||||||||||||||||||||
| 71 | 225 | $chunk =~ s/\015?\012/$EOL/g; | |||||||||||||||||||||||||||||||||
| 72 | 225 | push @{$self->{'headers'}}, $chunk; | |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | 225 | ▶ | if ( lc($header) eq 'dkim2-signature' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 75 | 24 | $self->{'has_dkim2'} = 1; | |||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||
| 77 | elsif ( lc($header) eq 'message-instance' ) { | ||||||||||||||||||||||||||||||||||
| 78 | 24 | $self->{'has_mi'} = 1; | |||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | 25 | ▶ | sub eoh_callback { | ||||||||||||||||||||||||||||||||
|
Subroutine
eoh_callback: called CC=11 SCAR=27.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 83 | 25 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 84 | 25 | ▶ | return if $self->{'failmode'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 85 | 25 | my $config = $self->_config(); | |||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | 25 | ▶ | unless ( $self->{'has_dkim2'} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 88 | 2 | $self->metric_count( 'dkim2_verify_total', { 'result' => 'none' } ); | |||||||||||||||||||||||||||||||||
| 89 | 2 | $self->dbgout( 'DKIM2Result', 'No DKIM2-Signature headers', LOG_DEBUG ); | |||||||||||||||||||||||||||||||||
| 90 | 2 | ▶ | unless ( $config->{'hide_none'} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 91 | 2 | my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dkim2' )->safe_set_value( 'none' ); | |||||||||||||||||||||||||||||||||
| 92 | 2 | $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'no signatures found' ) ); | |||||||||||||||||||||||||||||||||
| 93 | 2 | $self->add_auth_header( $header ); | |||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||
| 95 | 2 | delete $self->{'headers'}; | |||||||||||||||||||||||||||||||||
| 96 | 2 | return; | |||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||
| 99 | 23 | my $verifier; | |||||||||||||||||||||||||||||||||
| 100 | 23 | eval { | |||||||||||||||||||||||||||||||||
| 101 | # -09 section 14.4: the envelope this message arrived with must match | ||||||||||||||||||||||||||||||||||
| 102 | # its top signature, or a message signed for one recipient verifies | ||||||||||||||||||||||||||||||||||
| 103 | # when replayed to another. | ||||||||||||||||||||||||||||||||||
| 104 | my @envelope = $self->_config()->{'check_envelope'} ? ( | ||||||||||||||||||||||||||||||||||
| 105 | MailFrom => $self->{'env_from'} // '<>', | ||||||||||||||||||||||||||||||||||
| 106 | 23 | ▶ | RcptTo => [ @{ $self->{'env_rcpt'} || [] } ], | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$self->{"env_from"} // "<>"
$self->{'env_rcpt'} || []
MC/DC0%0/1
$self->{"env_from"} // "<>"
$self->{"env_from"}
MC/DC0%0/1
$self->{'env_rcpt'} || []
$self->{'env_rcpt'}
Truth table50%1/2
A
A= $self->{"env_from"}
Truth table50%1/2
A
A= $self->{'env_rcpt'}
| |||||||||||||||||||||||||||||||||||
| 107 | ) : (); | ||||||||||||||||||||||||||||||||||
| 108 | $verifier = Mail::DKIM2::Verifier->new( | ||||||||||||||||||||||||||||||||||
| 109 | 23 | IgnorePrefixes => $self->_config()->{'ignore_header_prefixes'}, | |||||||||||||||||||||||||||||||||
| 110 | @envelope, | ||||||||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||||||||
| 112 | 23 | $self->_setup_pubkey_callback($verifier); | |||||||||||||||||||||||||||||||||
| 113 | 23 | $self->set_object('dkim2_verifier', $verifier, 1); | |||||||||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||||||||
| 115 | 23 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 116 | 0 | $self->handle_exception( $error ); | |||||||||||||||||||||||||||||||||
| 117 | 0 | $self->log_error( 'DKIM2 Setup Error ' . $error ); | |||||||||||||||||||||||||||||||||
| 118 | 0 | $self->{'failmode'} = 1; | |||||||||||||||||||||||||||||||||
| 119 | 0 | $self->_check_error( $error ); | |||||||||||||||||||||||||||||||||
| 120 | 0 | $self->metric_count( 'dkim2_verify_total', { 'result' => 'error' } ); | |||||||||||||||||||||||||||||||||
| 121 | 0 | delete $self->{'headers'}; | |||||||||||||||||||||||||||||||||
| 122 | 0 | return; | |||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 23 | eval { | |||||||||||||||||||||||||||||||||
| 126 | $verifier->PRINT( join q{}, | ||||||||||||||||||||||||||||||||||
| 127 | 23 | @{ $self->{'headers'} }, | |||||||||||||||||||||||||||||||||
| 128 | "\015\012", | ||||||||||||||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||||||||||||||
| 130 | }; | ||||||||||||||||||||||||||||||||||
| 131 | 23 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 132 | 0 | $self->handle_exception( $error ); | |||||||||||||||||||||||||||||||||
| 133 | 0 | $self->log_error( 'DKIM2 Headers Error ' . $error ); | |||||||||||||||||||||||||||||||||
| 134 | 0 | $self->{'failmode'} = 1; | |||||||||||||||||||||||||||||||||
| 135 | 0 | $self->_check_error( $error ); | |||||||||||||||||||||||||||||||||
| 136 | 0 | $self->metric_count( 'dkim2_verify_total', { 'result' => 'error' } ); | |||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||
| 139 | 23 | $self->{'carry'} = q{}; | |||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | # The verifier hashes the body as it arrives and keeps none of it. Keep | ||||||||||||||||||||||||||||||||||
| 142 | # a copy only for what _add_mi_and_store builds from it: a snapshot, or | ||||||||||||||||||||||||||||||||||
| 143 | # a first Message-Instance. | ||||||||||||||||||||||||||||||||||
| 144 | $self->{'keep_body'} = $config->{'add_message_instance'} | ||||||||||||||||||||||||||||||||||
| 145 | 23 | ▶ | && ( $config->{'snapshot_directory'} || !$self->{'has_mi'} ); | ||||||||||||||||||||||||||||||||
|
Condition83%5/6
$config->{'add_message_instance'} && ($config->{'snapshot_directory'} || !$self->{'has_mi'})
$config->{"snapshot_directory"} or not $self->{"has_mi"}
MC/DC66%2/3
$config->{'add_message_instance'} && ($config->{'snapshot_directory'} || !$self->{'has_mi'})
$config->{'add_message_instance'}
$config->{"snapshot_directory"}
not $self->{"has_mi"}
Truth table75%3/4
A && B or not(C)
A= $config->{'add_message_instance'}B= $config->{"snapshot_directory"}C= not $self->{"has_mi"}
| |||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | 163 | ▶ | sub body_callback { | ||||||||||||||||||||||||||||||||
|
Subroutine
body_callback: called CC=8 SCAR=23.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 149 | 163 | my ( $self, $body_chunk ) = @_; | |||||||||||||||||||||||||||||||||
| 150 | 163 | ▶ | return if $self->{'failmode'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 151 | 163 | ▶ | return unless $self->{'has_dkim2'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 152 | # The verifier may already have its result from the headers. | ||||||||||||||||||||||||||||||||||
| 153 | 155 | my $verifier = $self->get_object('dkim2_verifier'); | |||||||||||||||||||||||||||||||||
| 154 | 155 | ▶ | return if $verifier->stopped; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 155 | 155 | my $EOL = "\015\012"; | |||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | 155 | my $chunk; | |||||||||||||||||||||||||||||||||
| 158 | 155 | ▶ | if ( $self->{'carry'} ne q{} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 159 | 0 | $chunk = $self->{'carry'} . $body_chunk; | |||||||||||||||||||||||||||||||||
| 160 | 0 | $self->{'carry'} = q{}; | |||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||
| 162 | else { | ||||||||||||||||||||||||||||||||||
| 163 | 155 | $chunk = $body_chunk; | |||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | 155 | ▶ | if ( substr( $chunk, -1 ) eq "\015" ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 167 | 0 | $self->{'carry'} = "\015"; | |||||||||||||||||||||||||||||||||
| 168 | 0 | $chunk = substr( $chunk, 0, -1 ); | |||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | 155 | $chunk =~ s/\015?\012/$EOL/g; | |||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||
| 173 | 155 | ▶ | push @{$self->{'body'}}, $chunk if $self->{'keep_body'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||
| 175 | 155 | eval { | |||||||||||||||||||||||||||||||||
| 176 | 155 | $verifier->PRINT( $chunk ); | |||||||||||||||||||||||||||||||||
| 177 | }; | ||||||||||||||||||||||||||||||||||
| 178 | 155 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 179 | 0 | $self->handle_exception( $error ); | |||||||||||||||||||||||||||||||||
| 180 | 0 | $self->log_error( 'DKIM2 Body Error ' . $error ); | |||||||||||||||||||||||||||||||||
| 181 | 0 | $self->{'failmode'} = 1; | |||||||||||||||||||||||||||||||||
| 182 | 0 | $self->_check_error( $error ); | |||||||||||||||||||||||||||||||||
| 183 | 0 | $self->metric_count( 'dkim2_verify_total', { 'result' => 'error' } ); | |||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||
| 187 | 25 | ▶ | sub eom_callback { | ||||||||||||||||||||||||||||||||
|
Subroutine
eom_callback: called CC=9 SCAR=22.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 188 | 25 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | # X-DKIM2-Snapshot names a snapshot this host took of the message; one | ||||||||||||||||||||||||||||||||||
| 191 | # that arrives from outside means nothing and is removed, whatever else | ||||||||||||||||||||||||||||||||||
| 192 | # happens to the message. | ||||||||||||||||||||||||||||||||||
| 193 | 25 | $self->_strip_snapshot_header(); | |||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||
| 195 | 25 | ▶ | return unless $self->{'has_dkim2'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 196 | 23 | ▶ | return if $self->{'failmode'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||
| 198 | 23 | my $config = $self->_config(); | |||||||||||||||||||||||||||||||||
| 199 | 23 | my $verifier = $self->get_object('dkim2_verifier'); | |||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||
| 201 | 23 | eval { | |||||||||||||||||||||||||||||||||
| 202 | 23 | $verifier->PRINT( $self->{'carry'} ); | |||||||||||||||||||||||||||||||||
| 203 | 23 | $verifier->CLOSE(); | |||||||||||||||||||||||||||||||||
| 204 | 23 | $self->check_timeout(); | |||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||
| 206 | 23 | my $result = $verifier->result; | |||||||||||||||||||||||||||||||||
| 207 | 23 | my $detail = $verifier->result_detail; | |||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||
| 209 | 23 | $self->metric_count( 'dkim2_verify_total', { 'result' => $result } ); | |||||||||||||||||||||||||||||||||
| 210 | 23 | $self->dbgout( 'DKIM2Result', $detail, LOG_DEBUG ); | |||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||
| 212 | 23 | my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dkim2' )->safe_set_value( $result ); | |||||||||||||||||||||||||||||||||
| 213 | 23 | ▶ | if ( my $details = $verifier->details ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 214 | 23 | $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( $details ) ); | |||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||
| 217 | # Add domain info from the highest-i signature | ||||||||||||||||||||||||||||||||||
| 218 | 23 | ▶ | if ( my $sig = $verifier->top_signature ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 219 | 23 | ▶ | $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'header.d' )->safe_set_value( $sig->domain || '' ) ); | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$sig->domain || ''
MC/DC0%0/1
$sig->domain || ''
$sig->domain
Truth table50%1/2
A
A= $sig->domain
| |||||||||||||||||||||||||||||||||||
| 220 | 23 | $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'header.i' )->safe_set_value( $sig->sequence ) ); | |||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||
| 223 | 23 | $self->add_auth_header( $header ); | |||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||
| 225 | # Compute and add Message-Instance header, then store snapshot | ||||||||||||||||||||||||||||||||||
| 226 | 23 | ▶ | if ( $result eq 'pass' && $config->{'add_message_instance'} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$result eq "pass" and $config->{"add_message_instance"}
MC/DC100%2/2
$result eq "pass" and $config->{"add_message_instance"}
$result eq "pass"
$config->{"add_message_instance"}
Truth table100%3/3
A and B
A= $result eq "pass"B= $config->{"add_message_instance"}
| |||||||||||||||||||||||||||||||||||
| 227 | 13 | $self->_add_mi_and_store(); | |||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||
| 229 | }; | ||||||||||||||||||||||||||||||||||
| 230 | 23 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 231 | 0 | $self->handle_exception( $error ); | |||||||||||||||||||||||||||||||||
| 232 | 0 | $self->log_error( 'DKIM2 EOM Error ' . $error ); | |||||||||||||||||||||||||||||||||
| 233 | 0 | $self->{'failmode'} = 1; | |||||||||||||||||||||||||||||||||
| 234 | 0 | $self->_check_error( $error ); | |||||||||||||||||||||||||||||||||
| 235 | 0 | $self->metric_count( 'dkim2_verify_total', { 'result' => 'error' } ); | |||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||
| 239 | 13 | ▶ | sub _add_mi_and_store { | ||||||||||||||||||||||||||||||||
|
Subroutine
_add_mi_and_store: called CC=9 SCAR=25.0
| |||||||||||||||||||||||||||||||||||
| 240 | 13 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 241 | 13 | my $config = $self->_config(); | |||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||
| 243 | 13 | eval { | |||||||||||||||||||||||||||||||||
| 244 | 13 | my $EOL = "\015\012"; | |||||||||||||||||||||||||||||||||
| 245 | 118 | my $message_data = join(q{}, grep { !/\AX-DKIM2-Snapshot[ \t]*:/i } @{$self->{'headers'}}) | |||||||||||||||||||||||||||||||||
| 246 | . $EOL | ||||||||||||||||||||||||||||||||||
| 247 | 13 | . join(q{}, @{$self->{'body'}}); | |||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||
| 249 | 13 | my $msg = parse_mime($message_data); | |||||||||||||||||||||||||||||||||
| 250 | 13 | my @mi_headers = $msg->header_raw('Message-Instance'); | |||||||||||||||||||||||||||||||||
| 251 | 13 | my $mi_value; | |||||||||||||||||||||||||||||||||
| 252 | my $snapshot; | ||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||
| 254 | 13 | ▶ | if ( @mi_headers ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 255 | # Case 2: Message has existing MI header(s). | ||||||||||||||||||||||||||||||||||
| 256 | # The topmost MI must match current content (already verified | ||||||||||||||||||||||||||||||||||
| 257 | # by the DKIM2 chain check). Use it as the snapshot key. | ||||||||||||||||||||||||||||||||||
| 258 | # Only the snapshot needs it: with no snapshot_directory there | ||||||||||||||||||||||||||||||||||
| 259 | # is nothing to store, so skip the verify entirely. | ||||||||||||||||||||||||||||||||||
| 260 | 13 | ▶ | return unless $config->{'snapshot_directory'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 261 | my $mi_ver = Mail::DKIM2::MessageInstance->verify($msg, | ||||||||||||||||||||||||||||||||||
| 262 | 11 | IgnorePrefixes => $config->{'ignore_header_prefixes'}); | |||||||||||||||||||||||||||||||||
| 263 | 11 | ▶ | unless ( $mi_ver ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 264 | # This shouldn't happen after successful DKIM2 verification | ||||||||||||||||||||||||||||||||||
| 265 | 0 | $self->log_error( 'DKIM2MI: MI headers present but none match current content' ); | |||||||||||||||||||||||||||||||||
| 266 | 0 | return; | |||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||
| 268 | 11 | ▶ | my %mi_by_v = map { (extract_mi_version($_) || 0) => $_ } @mi_headers; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
extract_mi_version($_) || 0
MC/DC0%0/1
extract_mi_version($_) || 0
extract_mi_version($_)
Truth table50%1/2
A
A= extract_mi_version($_)
| |||||||||||||||||||||||||||||||||||
| 269 | 11 | $mi_value = $mi_by_v{$mi_ver}; | |||||||||||||||||||||||||||||||||
| 270 | 11 | $snapshot = $message_data; | |||||||||||||||||||||||||||||||||
| 271 | 11 | $self->dbgout( 'DKIM2MI', "Existing MI m=$mi_ver matches, using as snapshot key", LOG_DEBUG ); | |||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||
| 273 | else { | ||||||||||||||||||||||||||||||||||
| 274 | # Case 1: No MI headers — first entry into DKIM2 ecosystem. | ||||||||||||||||||||||||||||||||||
| 275 | # Compute MI m=1 and prepend it. | ||||||||||||||||||||||||||||||||||
| 276 | my $mi = Mail::DKIM2::MessageInstance->calculate($msg, undef, | ||||||||||||||||||||||||||||||||||
| 277 | 0 | IgnorePrefixes => $config->{'ignore_header_prefixes'}); | |||||||||||||||||||||||||||||||||
| 278 | 0 | $mi_value = $mi->as_header_value; | |||||||||||||||||||||||||||||||||
| 279 | 0 | $self->prepend_header( 'Message-Instance', $mi_value ); | |||||||||||||||||||||||||||||||||
| 280 | 0 | $snapshot = "Message-Instance: $mi_value$EOL" . $message_data; | |||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | $self->dbgout( 'DKIM2MI', "Added Message-Instance m=" . ($mi->get_tag('m') || '?'), LOG_DEBUG ); | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$mi->get_tag('m') || '?'
MC/DC0%0/1
$mi->get_tag('m') || '?'
$mi->get_tag('m')
Truth table0%0/2
A
A= $mi->get_tag('m')
| |||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||
| 284 | # Keep the snapshot under a fresh random token and name it on the | ||||||||||||||||||||||||||||||||||
| 285 | # message (X-DKIM2-Snapshot), so the outbound signer can describe what | ||||||||||||||||||||||||||||||||||
| 286 | # local processing changes -- against this message's own snapshot, | ||||||||||||||||||||||||||||||||||
| 287 | # never one found by Message-Instance value (a replayed instance over | ||||||||||||||||||||||||||||||||||
| 288 | # an edited body would find a different message's). | ||||||||||||||||||||||||||||||||||
| 289 | 11 | ▶ | if ( $config->{'snapshot_directory'} && $mi_value ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$config->{"snapshot_directory"} and $mi_value
MC/DC0%0/2
$config->{"snapshot_directory"} and $mi_value
$config->{"snapshot_directory"}
$mi_value
Truth table33%1/3
A and B
A= $config->{"snapshot_directory"}B= $mi_value
| |||||||||||||||||||||||||||||||||||
| 290 | my $store = Mail::DKIM2::MessageStore->new( | ||||||||||||||||||||||||||||||||||
| 291 | 11 | directory => $config->{'snapshot_directory'}, | |||||||||||||||||||||||||||||||||
| 292 | ); | ||||||||||||||||||||||||||||||||||
| 293 | 11 | my $token = unpack('H*', random_bytes(16)); | |||||||||||||||||||||||||||||||||
| 294 | 11 | $store->store("X-DKIM2-Snapshot:$token", $snapshot); | |||||||||||||||||||||||||||||||||
| 295 | 11 | $self->prepend_header( 'X-DKIM2-Snapshot', $token ); | |||||||||||||||||||||||||||||||||
| 296 | 11 | $self->dbgout( 'DKIM2Snapshot', "Stored snapshot $token", LOG_DEBUG ); | |||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||
| 298 | }; | ||||||||||||||||||||||||||||||||||
| 299 | 13 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 300 | 0 | $self->handle_exception( $error ); | |||||||||||||||||||||||||||||||||
| 301 | 0 | $self->log_error( 'DKIM2 MI/Snapshot Error ' . $error ); | |||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||
| 305 | # Remove every X-DKIM2-Snapshot field the message arrived with (last first, | ||||||||||||||||||||||||||||||||||
| 306 | # by index among the fields as received). | ||||||||||||||||||||||||||||||||||
| 307 | 25 | ▶ | sub _strip_snapshot_header { | ||||||||||||||||||||||||||||||||
|
Subroutine
_strip_snapshot_header: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 308 | 25 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 309 | 25 | ▶ | my $n = grep { /\AX-DKIM2-Snapshot[ \t]*:/i } @{ $self->{'headers'} || [] }; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'headers'} || []
MC/DC100%1/1
$self->{'headers'} || []
$self->{'headers'}
Truth table100%2/2
A
A= $self->{'headers'}
| |||||||||||||||||||||||||||||||||||
| 310 | 25 | $self->change_header( 'X-DKIM2-Snapshot', $_, q{} ) for reverse 1 .. $n; | |||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||
| 313 | 2 | ▶ | sub close_callback { | ||||||||||||||||||||||||||||||||
|
Subroutine
close_callback: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 314 | 2 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||
| 315 | 2 | delete $self->{'failmode'}; | |||||||||||||||||||||||||||||||||
| 316 | 2 | delete $self->{'headers'}; | |||||||||||||||||||||||||||||||||
| 317 | 2 | delete $self->{'body'}; | |||||||||||||||||||||||||||||||||
| 318 | 2 | delete $self->{'carry'}; | |||||||||||||||||||||||||||||||||
| 319 | 2 | delete $self->{'keep_body'}; | |||||||||||||||||||||||||||||||||
| 320 | 2 | delete $self->{'has_dkim2'}; | |||||||||||||||||||||||||||||||||
| 321 | 2 | delete $self->{'has_mi'}; | |||||||||||||||||||||||||||||||||
| 322 | 2 | delete $self->{'env_from'}; | |||||||||||||||||||||||||||||||||
| 323 | 2 | delete $self->{'env_rcpt'}; | |||||||||||||||||||||||||||||||||
| 324 | 2 | $self->destroy_object('dkim2_verifier'); | |||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||
| 327 | 23 | ▶ | sub _setup_pubkey_callback { | ||||||||||||||||||||||||||||||||
|
Subroutine
_setup_pubkey_callback: called CC=2 SCAR=11.9
| |||||||||||||||||||||||||||||||||||
| 328 | 23 | my ( $self, $verifier ) = @_; | |||||||||||||||||||||||||||||||||
| 329 | 23 | my $config = $self->_config(); | |||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||
| 331 | # If dns_overrides is set (testing), load keys from that JSON file | ||||||||||||||||||||||||||||||||||
| 332 | 23 | ▶ | if ( $config->{'dns_overrides'} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 333 | 0 | my $dns_data = JSON::decode_json( File::Slurp::read_file($config->{'dns_overrides'}) ); | |||||||||||||||||||||||||||||||||
| 334 | 0 | ▶ | $verifier->set_pubkey_callback(sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 335 | 0 | my ($signature, $idx) = @_; | |||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | $idx //= 0; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$idx //= 0
MC/DC0%0/1
$idx //= 0
$idx
Truth table0%0/2
A
A= $idx
| |||||||||||||||||||||||||||||||||||
| 337 | 0 | my $sel = $signature->selector($idx); | |||||||||||||||||||||||||||||||||
| 338 | 0 | my $dom = $signature->domain; | |||||||||||||||||||||||||||||||||
| 339 | 0 | my $key_txt = $dns_data->{$dom}{"$sel._domainkey"}[0][1]; | |||||||||||||||||||||||||||||||||
| 340 | 0 | return parse_dkim_pubkey($key_txt); | |||||||||||||||||||||||||||||||||
| 341 | 0 | }); | |||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||
| 343 | else { | ||||||||||||||||||||||||||||||||||
| 344 | # Real DNS through the milter's own resolver (so its timeouts and | ||||||||||||||||||||||||||||||||||
| 345 | # caching apply). The library classifies the answer: no record is | ||||||||||||||||||||||||||||||||||
| 346 | # permerror, anything transient is temperror. | ||||||||||||||||||||||||||||||||||
| 347 | 23 | $verifier->resolver( $self->get_object('resolver') ); | |||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||
| 351 | 0 | ▶ | sub _check_error { | ||||||||||||||||||||||||||||||||
|
Subroutine
_check_error: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||
| 352 | 0 | my ( $self, $error ) = @_; | |||||||||||||||||||||||||||||||||
| 353 | 0 | ▶ | if ( $error =~ /^DNS error: query timed out/ | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$error =~ /^DNS error: query timed out/u or $error =~ /^DNS query timeout/u
MC/DC0%0/2
$error =~ /^DNS error: query timed out/u or $error =~ /^DNS query timeout/u
$error =~ /^DNS error: query timed out/u
$error =~ /^DNS query timeout/u
Truth table0%0/3
A or B
A= $error =~ /^DNS error: query timed out/uB= $error =~ /^DNS query timeout/u
| |||||||||||||||||||||||||||||||||||
| 354 | or $error =~ /^DNS query timeout/ | ||||||||||||||||||||||||||||||||||
| 355 | ){ | ||||||||||||||||||||||||||||||||||
| 356 | 0 | $self->log_error( 'Temp DKIM2 Error - ' . $error ); | |||||||||||||||||||||||||||||||||
| 357 | 0 | my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dkim2' )->safe_set_value( 'temperror' ); | |||||||||||||||||||||||||||||||||
| 358 | 0 | $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'dns timeout' ) ); | |||||||||||||||||||||||||||||||||
| 359 | 0 | $self->add_auth_header( $header ); | |||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||
| 361 | elsif ( $error =~ /^DNS error: SERVFAIL/ ){ | ||||||||||||||||||||||||||||||||||
| 362 | 0 | $self->log_error( 'Temp DKIM2 Error - ' . $error ); | |||||||||||||||||||||||||||||||||
| 363 | 0 | my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dkim2' )->safe_set_value( 'temperror' ); | |||||||||||||||||||||||||||||||||
| 364 | 0 | $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'dns servfail' ) ); | |||||||||||||||||||||||||||||||||
| 365 | 0 | $self->add_auth_header( $header ); | |||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||
| 367 | else { | ||||||||||||||||||||||||||||||||||
| 368 | 0 | $self->log_error( 'DKIM2 Error - ' . $error ); | |||||||||||||||||||||||||||||||||
| 369 | 0 | my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'dkim2' )->safe_set_value( 'temperror' ); | |||||||||||||||||||||||||||||||||
| 370 | 0 | $self->add_auth_header( $header ); | |||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||
| 374 | 3 | 1; | |||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||
| 376 | __END__ | ||||||||||||||||||||||||||||||||||