Authentication/Handler/SenderID.pm

filter: Statementstmt 74.4% 64 / 86 Branchbran 78.5% 22 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 14 / 16 Podpod 11.1% 1 / 9 totaltotal 70.8% 102 / 144 CC 16 SCAR 17.9
CC
16
Cov
74%
eoh_callback
14.3
header_callback
5.0
envfrom_callback
4.0
CRAP 6.0
« Authentication/Handler/AlignedFrom.pm ↑ Summary Authentication/Handler/PTR.pm »
1 package Mail::Milter::Authentication::Handler::SenderID;
2 32 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Handler class for SenderID
7 32 our $VERSION = '4.20261111'; # VERSION
8 32 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use Mail::Milter::Authentication::Handler::SPF;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use Mail::SPF;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub default_config {
Subroutine default_config: not called CC=1 SCAR=6.9
Pod undocumented
13 return {
14 0 'hide_none' => 1,
15 };
16 }
17  
18 0 ▶sub grafana_rows {
Subroutine grafana_rows: not called CC=1 SCAR=6.9
Pod undocumented
19 0 my ( $self ) = @_;
20 0 my @rows;
21 0 push @rows, $self->get_json( 'SenderID_metrics' );
22 0 return \@rows;
23 }
24  
25 69 ▶sub setup_callback {
Subroutine setup_callback: called CC=1 SCAR=0.0
Pod undocumented
26 69 my ( $self ) = @_;
27 # Call connect_callback from SPF handler to setup object creation
28 # Required if SenderID is enabled but SPF is disabled.
29 69 return Mail::Milter::Authentication::Handler::SPF::setup_callback( $self );
30 }
31  
32 31 ▶sub register_metrics {
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
33 return {
34 31 'senderid_total' => 'The number of emails processed for Sender ID',
35 };
36 }
37  
38 74 ▶sub helo_callback {
Subroutine helo_callback: called CC=1 SCAR=0.0
Pod undocumented
39 74 my ( $self, $helo_host ) = @_;
40 74 $self->{'helo_name'} = $helo_host;
41 }
42  
43 74 ▶sub envfrom_callback {
Subroutine envfrom_callback: called CC=4 SCAR=13.9
Pod undocumented
44 74 my ( $self, $env_from ) = @_;
45 74 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 8 66 74
46 66 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 2 64 66
47 64 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 64 64
48 64 delete $self->{'from_header'};
49 }
50  
51 875 ▶sub header_callback {
Subroutine header_callback: called CC=5 SCAR=16.1
Pod undocumented
52 875 my ( $self, $header, $value ) = @_;
53 875 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 117 758 875
54 758 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 30 728 758
55 728 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 728 728
56 728 ▶ if ( lc $header eq 'from' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "from") 66 662 728
57 66 $self->{'from_header'} = $value;
58 }
59 }
60  
61 74 ▶sub eoh_callback {
Subroutine eoh_callback: called CC=9 SCAR=26.6
Pod undocumented
62 74 my ($self) = @_;
63 74 my $config = $self->handler_config();
64 74 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 8 66 74
65 66 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 2 64 66
66 64 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 64 64
67  
68 64 my $spf_server = $self->get_object('spf_server');
69 64 ▶ if ( ! $spf_server ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spf_server) 0 64 64
70 0 $self->log_error( 'SenderID Setup Error' );
71 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'senderid' )->safe_set_value( 'temperror' );
72 0 $self->add_auth_header($header);
73 0 $self->metric_count( 'senderid_total', { 'result' => 'error' } );
74 0 return;
75 }
76  
77 64 my $scope = 'pra';
78  
79 64 my $identity = $self->get_address_from( $self->{'from_header'} );
80  
81 64 ▶ if ( ! $identity ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($identity) 7 57 64
82 7 $self->log_error( 'SENDERID Error No Identity' );
83 7 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'senderid' )->safe_set_value( 'permerror' );
84 7 $self->add_auth_header( $header );
85 7 $self->metric_count( 'senderid_total', { 'result' => 'permerror' } );
86 7 return;
87 }
88  
89 57 eval {
90 my $spf_request = Mail::SPF::Request->new(
91 'versions' => [2],
92 'scope' => $scope,
93 'identity' => $identity,
94 'ip_address' => $self->ip_address(),
95 57 'helo_identity' => $self->{'helo_name'},
96 );
97  
98 57 my $spf_result = $spf_server->process($spf_request);
99  
100 57 my $result_code = $spf_result->code();
101 57 $self->metric_count( 'senderid_total', {'result' => $result_code } );
102 57 $self->dbgout( 'SenderIdCode', $result_code, LOG_DEBUG );
103  
104 57 ▶ if ( ! ( $config->{'hide_none'} && $result_code eq 'none' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_none"} and $result_code eq "none") 0 57 57
Condition33%1/3
$config->{"hide_none"} and $result_code eq "none"
!ll&&!rl&&r
0057
MC/DC0%0/2
$config->{"hide_none"} and $result_code eq "none"
$config->{"hide_none"} $result_code eq "none"
Truth table33%1/3
A and B
A= $config->{"hide_none"}B= $result_code eq "none"
ABresult
0X0
100
111
105 0 my $auth_header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'senderid' )->safe_set_value( $result_code );
106 0 $self->add_auth_header( $auth_header );
107 #my $result_local = $spf_result->local_explanation;
108 #my $result_auth = $spf_result->can( 'authority_explanation' ) ? $spf_result->authority_explanation() : '';
109 0 my $result_header = $spf_result->received_spf_header();
110 0 my ( $header, $value ) = split( ': ', $result_header, 2 );
111 0 $self->prepend_header( $header, $value );
112 0 $self->dbgout( 'SPFHeader', $result_header, LOG_DEBUG );
113 }
114 };
115 57 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 57 57
116 0 $self->handle_exception( $error );
117 0 $self->log_error( 'SENDERID Error ' . $error );
118 0 $self->metric_count( 'senderid_total', { 'result' => 'error' } );
119 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'senderid' )->safe_set_value( 'temperror' );
120 0 $self->add_auth_header($header);
121 0 return;
122 }
123 }
124  
125 117 ▶sub close_callback {
Subroutine close_callback: called CC=1 SCAR=0.0
Pod undocumented
126 117 my ( $self ) = @_;
127 117 delete $self->{'from_header'};
128 117 delete $self->{'helo_name'};
129 }
130  
131 32 1;
132  
133 __END__
« Authentication/Handler/AlignedFrom.pm ↑ Summary Authentication/Handler/PTR.pm »