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

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