Authentication/Handler/SenderID.pm

filter: Statementstmt 81.4% 70 / 86 Branchbran 82.1% 23 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 87.5% 14 / 16 Podpod 11.1% 1 / 9 totaltotal 77.0% 111 / 144 CC 16 SCAR 15.8
CC
16
Cov
81%
eoh_callback
10.5
header_callback
5.0
envfrom_callback
4.0
CRAP 4.9
« Authentication/Handler/LocalIP.pm ↑ Summary Authentication/Handler/TrustedIP.pm »
1 package Mail::Milter::Authentication::Handler::SenderID;
2 33 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Handler class for SenderID
7 33 our $VERSION = '4.20261116'; # VERSION
8 33 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use Mail::Milter::Authentication::Handler::SPF;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶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 70 ▶sub setup_callback {
Subroutine setup_callback: called CC=1 SCAR=0.0
Pod undocumented
26 70 my ( $self ) = @_;
27 # Call connect_callback from SPF handler to setup object creation
28 # Required if SenderID is enabled but SPF is disabled.
29 70 return Mail::Milter::Authentication::Handler::SPF::setup_callback( $self );
30 }
31  
32 32 ▶sub register_metrics {
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
33 return {
34 32 'senderid_total' => 'The number of emails processed for Sender ID',
35 };
36 }
37  
38 75 ▶sub helo_callback {
Subroutine helo_callback: called CC=1 SCAR=0.0
Pod undocumented
39 75 my ( $self, $helo_host ) = @_;
40 75 $self->{'helo_name'} = $helo_host;
41 }
42  
43 75 ▶sub envfrom_callback {
Subroutine envfrom_callback: called CC=4 SCAR=13.9
Pod undocumented
44 75 my ( $self, $env_from ) = @_;
45 75 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 8 67 75
46 67 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 2 65 67
47 65 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 65 65
48 65 delete $self->{'from_header'};
49 }
50  
51 881 ▶sub header_callback {
Subroutine header_callback: called CC=5 SCAR=16.1
Pod undocumented
52 881 my ( $self, $header, $value ) = @_;
53 881 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 117 764 881
54 764 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 30 734 764
55 734 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 734 734
56 734 ▶ if ( lc $header eq 'from' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "from") 67 667 734
57 67 $self->{'from_header'} = $value;
58 }
59 }
60  
61 75 ▶sub eoh_callback {
Subroutine eoh_callback: called CC=9 SCAR=23.5
Pod undocumented
62 75 my ($self) = @_;
63 75 my $config = $self->handler_config();
64 75 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 8 67 75
65 67 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 2 65 67
66 65 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 65 65
67  
68 65 my $spf_server = $self->get_object('spf_server');
69 65 ▶ if ( ! $spf_server ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spf_server) 0 65 65
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 65 my $scope = 'pra';
78  
79 65 my $identity = $self->get_address_from( $self->{'from_header'} );
80  
81 65 ▶ if ( ! $identity ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($identity) 7 58 65
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 58 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 58 'helo_identity' => $self->{'helo_name'},
96 );
97  
98 58 my $spf_result = $spf_server->process($spf_request);
99  
100 58 my $result_code = $spf_result->code();
101 58 $self->metric_count( 'senderid_total', {'result' => $result_code } );
102 58 $self->dbgout( 'SenderIdCode', $result_code, LOG_DEBUG );
103  
104 58 ▶ if ( ! ( $config->{'hide_none'} && $result_code eq 'none' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_none"} and $result_code eq "none") 1 57 58
Condition66%2/3
$config->{"hide_none"} and $result_code eq "none"
!ll&&!rl&&r
1057
MC/DC50%1/2
$config->{"hide_none"} and $result_code eq "none"
$config->{"hide_none"} $result_code eq "none"
Truth table66%2/3
A and B
A= $config->{"hide_none"}B= $result_code eq "none"
ABresult
0X0
100
111
105 1 my $auth_header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'senderid' )->safe_set_value( $result_code );
106 1 $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 1 my $result_header = $spf_result->received_spf_header();
110 1 my ( $header, $value ) = split( ': ', $result_header, 2 );
111 1 $self->prepend_header( $header, $value );
112 1 $self->dbgout( 'SPFHeader', $result_header, LOG_DEBUG );
113 }
114 };
115 58 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 58 58
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 118 ▶sub close_callback {
Subroutine close_callback: called CC=1 SCAR=0.0
Pod undocumented
126 118 my ( $self ) = @_;
127 118 delete $self->{'from_header'};
128 118 delete $self->{'helo_name'};
129 }
130  
131 33 1;
132  
133 __END__
« Authentication/Handler/LocalIP.pm ↑ Summary Authentication/Handler/TrustedIP.pm »