blib/lib/Mail/Milter/Authentication/Handler/BIMI.pm untested

filter: Statementstmt 0.0% 0 / 143 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 2 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 213 CC 22 SCAR 43.2
CC
22
Cov
0%
header_callback
132.0
eom_callback
132.0
remove_bimi_header
6.0
CRAP 75.5
1 package Mail::Milter::Authentication::Handler::BIMI;
2 use strict;
3 use warnings;
4 use Mail::Milter::Authentication 2.20180510;
5 use base 'Mail::Milter::Authentication::Handler';
6 our $VERSION = '2.20180510'; # VERSION
7 # ABSTRACT: BIMI handler for authentication milter
8  
9 use English qw{ -no_match_vars };
10 use Mail::BIMI;
11 use Sys::Syslog qw{:standard :macros};
12 use Mail::AuthenticationResults::Header::Entry;
13 use Mail::AuthenticationResults::Header::SubEntry;
14 use Mail::AuthenticationResults::Header::Comment;
15  
16 sub default_config {
17 return {
18 };
19 }
20  
21 sub register_metrics {
22 return {
23 'bimi_total' => 'The number of emails processed for BIMI',
24 'bimi_removed_total' => 'The number BIMI-Location headers removed',
25 };
26 }
27  
28 sub remove_bimi_header {
29 my ( $self, $value ) = @_;
30 $self->metric_count( 'bimi_remove_total' );
31 if ( !exists( $self->{'remove_bimi_headers'} ) ) {
32 $self->{'remove_bimi_headers'} = [];
33 }
34 push @{ $self->{'remove_bimi_headers'} }, $value;
35 return;
36 }
37  
38 sub envfrom_callback {
39 my ( $self, $env_from ) = @_;
40 delete $self->{'bimi_header_index'};
41 delete $self->{'remove_bimi_headers'};
42 $self->{ 'header_added' } = 0;
43 return;
44 }
45  
46 sub header_callback {
47 my ( $self, $header, $value ) = @_;
48  
49 # Not sure where this should go in the flow, so it's going here!
50 # Which is clearly, or at least probably the wrong place.
51 if ( lc $header eq 'bimi-location' ) {
52 if ( !exists $self->{'bimi_header_index'} ) {
53 $self->{'bimi_header_index'} = 0;
54 }
55 $self->{'bimi_header_index'} =
56 $self->{'bimi_header_index'} + 1;
57 $self->remove_bimi_header( $self->{'bimi_header_index'} );
58 my $forged_header =
59 '(Received BIMI-Location header removed by '
60 . $self->get_my_hostname()
61 . ')' . "\n"
62 . ' '
63 . $value;
64 $self->append_header( 'X-Received-BIMI-Location',
65 $forged_header );
66 }
67  
68 return if ( $self->is_local_ip_address() );
69 return if ( $self->is_trusted_ip_address() );
70 return if ( $self->is_authenticated() );
71 return if ( $self->{'failmode'} );
72  
73 if ( lc $header eq 'bimi-selector' ) {
74 if ( exists $self->{'selector'} ) {
75 $self->dbgout( 'BIMIFail', 'Multiple BIMI-Selector fields', LOG_INFO );
76 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'bimi' )->safe_set_value( 'fail' );
77 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'multiple BIMI-Selector fields in message' ) );
78 $self->add_auth_header( $header );
79 $self->metric_count( 'bimi_total', { 'result' => 'fail', 'reason' => 'bad_selector_header' } );
80 $self->{ 'header_added' } = 1;
81 $self->{'failmode'} = 1;
82 return;
83 }
84 $self->{'selector'} = $value;
85 }
86 if ( lc $header eq 'from' ) {
87 if ( exists $self->{'from_header'} ) {
88 $self->dbgout( 'BIMIFail', 'Multiple RFC5322 from fields', LOG_INFO );
89 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'bimi' )->safe_set_value( 'fail' );
90 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'multiple RFC5322 from fields in message' ) );
91 $self->add_auth_header( $header );
92 $self->metric_count( 'bimi_total', { 'result' => 'fail', 'reason' => 'bad_from_header' } );
93 $self->{ 'header_added' } = 1;
94 $self->{'failmode'} = 1;
95 return;
96 }
97 $self->{'from_header'} = $value;
98 }
99 ## ToDo remove/rename existing headers here
100 return;
101 }
102  
103 sub eom_requires {
104 my ($self) = @_;
105 my @requires = qw{ DMARC };
106 return \@requires;
107 }
108  
109 sub eom_callback {
110 my ($self) = @_;
111 my $config = $self->handler_config();
112  
113 # Again, not sure where this should go, so it's going here.
114 if ( exists( $self->{'remove_bimi_headers'} ) ) {
115 foreach my $header ( reverse @{ $self->{'remove_bimi_headers'} } ) {
116 $self->dbgout( 'RemoveBIMILocationHeader', $header, LOG_DEBUG );
117 $self->change_header( 'BIMI-Location', $header, q{} );
118 }
119 }
120  
121 return if ( $self->{ 'header_added' } );
122 return if ( $self->is_local_ip_address() );
123 return if ( $self->is_trusted_ip_address() );
124 return if ( $self->is_authenticated() );
125 return if ( $self->{'failmode'} );
126 eval {
127 my $Domain = $self->get_domain_from( $self->{'from_header'} );
128 my $Selector = $self->{ 'selector' } || 'default';
129 $Selector = lc $Selector;
130 my $BIMI = Mail::BIMI->new();
131  
132 # Rework this to allow for multiple dmarc_result objects as per new DMARC handler
133 my $DMARCResult = $self->get_object( 'dmarc_result' );
134  
135 if ( ! $DMARCResult ) {
136  
137 $self->log_error( 'BIMI Error No DMARC Results object');
138 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'bimi' )->safe_set_value( 'temperror' );
139 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'Internal DMARC error' ) );
140 $self->add_auth_header( $header );
141 $self->{ 'header_added' } = 1;
142  
143 }
144 else {
145  
146 $BIMI->set_resolver( $self->get_object( 'resolver' ) );
147 $BIMI->set_dmarc_object( $DMARCResult );
148 $BIMI->set_from_domain( $Domain );
149 $BIMI->set_selector( $Selector );
150 $BIMI->validate();
151  
152 my $Result = $BIMI->result();
153 my $AuthResults = $Result->get_authentication_results_object();
154 $self->add_auth_header( $AuthResults );
155 $self->{ 'header_added' } = 1;
156 my $Record = $BIMI->record();
157 my $URLList = $Record->url_list();
158 if ( $Result->result() eq 'pass' ) {
159 $self->prepend_header( 'BIMI-Location', join( "\n",
160 'v=BIMI1;',
161 ' l=' . join( ',', @$URLList ) ) );
162 }
163  
164 $self->metric_count( 'bimi_total', { 'result' => $Result->result() } );
165 }
166  
167 };
168 if ( my $error = $@ ) {
169 $self->handle_exception( $error );
170 $self->log_error( 'BIMI Error ' . $error );
171 if ( ! $self->{ 'header_added' } ) {
172 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'bimi' )->safe_set_value( 'temperror' );
173 $self->add_auth_header( $header );
174 $self->{ 'header_added' } = 1;
175 }
176 }
177  
178 return;
179 }
180  
181 sub close_callback {
182 my ( $self ) = @_;
183 delete $self->{'selector'};
184 delete $self->{'from_header'};
185 delete $self->{'failmode'};
186 delete $self->{'remove_bimi_headers'};
187 delete $self->{'bimi_header_index'};
188 delete $self->{ 'header_added' };
189 return;
190 }
191  
192 1;
193  
194 __END__
195  
196 =head1 NAME
197  
198 Authentication Milter - BIMI Module
199  
200 =head1 DESCRIPTION
201  
202 Module implementing the BIMI standard checks.
203  
204 This handler requires the DMARC handler and its dependencies to be installed and active.
205  
206 =head1 CONFIGURATION
207  
208 "BIMI" : { | Config for the BIMI Module
209 | Requires DMARC
210 },
211  
212 =head1 SYNOPSIS
213  
214 =head1 AUTHORS
215  
216 Marc Bradshaw E<lt>marc@marcbradshaw.netE<gt>
217  
218 =head1 COPYRIGHT
219  
220 Copyright 2018
221  
222 This library is free software; you may redistribute it and/or
223 modify it under the same terms as Perl itself.
224