Handler/SpamAssassin.pm

filter: Statementstmt 20.6% 32 / 155 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 45.4% 10 / 22 Podpod 8.3% 1 / 12 totaltotal 17.9% 43 / 240 CC 28 SCAR 49.3
CC
28
Cov
16%
eom_callback
342.0
header_callback
72.0
remove_header
12.0
CRAP 138.9
↑ Summary Handler/RSpamD.pm »
1 package Mail::Milter::Authentication::Handler::SpamAssassin;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '2.20180318'; # VERSION
6 # ABSTRACT: SpamAssassin scanner for Authentication Milter
7  
8 1 ▶use English qw{ -no_match_vars };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Sys::Syslog qw{:standard :macros};
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Mail::AuthenticationResults::Header::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Mail::AuthenticationResults::Header::SubEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Mail::AuthenticationResults::Header::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Mail::SpamAssassin;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Mail::SpamAssassin::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Issues
18 #
19 # Message may have multiple rcpt to addresses, in this
20 # case we can't load individual configs, would need to
21 # split the message and re-inject, which is a bloody
22 # meess!
23 # HOWEVER, spamassass-milter doesn't appear to do the
24 # right thing either, so we're actually no worse off.
25  
26 sub default_config {
27 return {
28 0 ▶ 'default_user' => 'nobody',
Subroutine default_config: not called CC=1 SCAR=6.9
Pod undocumented
29 'sa_host' => 'localhost',
30 'sa_port' => '783',
31 'hard_reject_at' => 10,
32 'remove_headers' => 'yes',
33 }
34 }
35  
36 sub grafana_rows {
37 0 ▶ my ( $self ) = @_;
Subroutine grafana_rows: not called CC=1 SCAR=6.9
Pod undocumented
38 0 my @rows;
39 0 push @rows, $self->get_json( 'SpamAssassin_metrics' );
40 0 return \@rows;
41 }
42  
43 sub register_metrics {
44 return {
45 0 ▶ 'spamassassin_total' => 'The number of emails processed for SpamAssassin',
Subroutine register_metrics: not called CC=1 SCAR=6.9
Pod documented
46 };
47 }
48  
49 sub get_user {
50 0 ▶ my ( $self ) = @_;
Subroutine get_user: not called CC=2 SCAR=17.9
Pod undocumented
51 0 my $user_handler = $self->get_handler('UserDB');
52 0 my $user = $user_handler->{'local_user'};
53 0 ▶ return $user if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
54 0 my $config = $self->handler_config();
55 0 return $config->{'default_user'};
56 }
57  
58 sub remove_header {
59 0 ▶ my ( $self, $key, $value ) = @_;
Subroutine remove_header: not called CC=3 SCAR=24.8
Pod undocumented
60 0 ▶ if ( !exists( $self->{'remove_headers'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"remove_headers"}) 0 0 0
61 0 $self->{'remove_headers'} = {};
62 }
63 0 ▶ if ( !exists( $self->{'remove_headers'}->{ lc $key } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"remove_headers"}{lc $key}) 0 0 0
64 0 $self->{'remove_headers'}->{ $key } = [];
65 }
66 0 push @{ $self->{'remove_headers'}->{ lc $key } }, $value;
67 0 return;
68 }
69  
70 sub envfrom_callback {
71 0 ▶ my ($self) = @_;
Subroutine envfrom_callback: not called CC=1 SCAR=6.9
Pod undocumented
72 0 $self->{'lines'} = [];
73 0 $self->{'rcpt_to'} = q{};
74 0 delete $self->{'header_index'};
75 0 delete $self->{'remove_headers'};
76 0 $self->{'metrics_data'} = {};
77 0 $self->{ 'metrics_data' }->{ 'header_removed' } = 'no';
78 0 return;
79 }
80  
81 sub envrcpt_callback {
82 0 ▶ my ( $self, $env_to ) = @_;
Subroutine envrcpt_callback: not called CC=1 SCAR=6.9
Pod undocumented
83 0 $self->{'rcpt_to'} = $env_to;
84 0 return;
85 }
86  
87 sub header_callback {
88 0 ▶ my ( $self, $header, $value ) = @_;
Subroutine header_callback: not called CC=8 SCAR=42.8
Pod undocumented
89 0 push @{$self->{'lines'}} ,$header . ': ' . $value . "\r\n";
90 0 my $config = $self->handler_config();
91  
92 0 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 0 0 0
93 0 ▶ return if ( lc $config->{'remove_headers'} eq 'no' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $config->{"remove_headers"} eq "no" 0 0 0
94  
95 0 foreach my $header_type ( qw{ X-Spam-score X-Spam-Status X-Spam-hits } ) {
96 0 ▶ if ( lc $header eq lc $header_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq lc $header_type) 0 0 0
97 0 ▶ if ( !exists $self->{'header_index'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"header_index"}) 0 0 0
98 0 $self->{'header_index'} = {};
99 }
100 0 ▶ if ( !exists $self->{'header_index'}->{ lc $header_type } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"header_index"}{lc $header_type}) 0 0 0
101 0 $self->{'header_index'}->{ lc $header_type } = 0;
102 }
103 $self->{'header_index'}->{ lc $header_type } =
104 0 $self->{'header_index'}->{ lc $header_type } + 1;
105 0 $self->remove_header( $header_type, $self->{'header_index'}->{ lc $header_type } );
106 0 $self->{ 'metrics_data' }->{ 'header_removed' } = 'yes';
107 0 ▶ if ( lc $config->{'remove_headers'} ne 'silent' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $config->{"remove_headers"} ne "silent") 0 0 0
108 0 my $forged_header =
109 '(Received ' . $header_type . ' header removed by '
110 . $self->get_my_hostname()
111 . ')' . "\n"
112 . ' '
113 . $value;
114 0 $self->append_header( 'X-Received-' . $header_type,
115 $forged_header );
116 }
117 }
118 }
119  
120 0 return;
121 }
122  
123 sub eoh_callback {
124 0 ▶ my ( $self ) = @_;
Subroutine eoh_callback: not called CC=1 SCAR=6.9
Pod undocumented
125 0 push @{$self->{'lines'}} , "\r\n";
126 0 return;
127 }
128  
129 sub body_callback {
130 0 ▶ my ( $self, $chunk ) = @_;
Subroutine body_callback: not called CC=1 SCAR=6.9
Pod undocumented
131 0 push @{$self->{'lines'}} , $chunk;
132 0 return;
133 }
134  
135 sub eom_callback {
136 0 ▶ my ($self) = @_;
Subroutine eom_callback: not called CC=18 SCAR=58.3
Pod undocumented
137  
138 0 my $config = $self->handler_config();
139  
140 0 ▶ my $host = $config->{'sa_host'} || 'localhost';
Condition0%0/2
$config->{'sa_host'} || 'localhost'
l!l
00
MC/DC0%0/1
$config->{'sa_host'} || 'localhost'
$config->{'sa_host'}
Truth table0%0/2
A
A= $config->{'sa_host'}
Aresult
11
00
141 0 ▶ my $port = $config->{'sa_port'} || 783;
Condition0%0/2
$config->{'sa_port'} || 783
l!l
00
MC/DC0%0/1
$config->{'sa_port'} || 783
$config->{'sa_port'}
Truth table0%0/2
A
A= $config->{'sa_port'}
Aresult
11
00
142 0 my $user = $self->get_user();
143  
144 0 $self->dbgout( 'SpamAssassinUser', $user, LOG_INFO );
145  
146 0 my $sa_client = Mail::SpamAssassin::Client->new({
147 'port' => $port,
148 'host' => $host,
149 'username' => $user,
150 });
151  
152 0 ▶ if ( ! $sa_client->ping() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sa_client->ping) 0 0 0
153 0 $self->log_error( 'SpamAssassin could not connect to server' );
154 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-spam' )->safe_set_value( 'temperror' );
155 0 $self->add_auth_header( $header );
156 0 $self->{ 'metrics_data' }->{ 'result' } = 'servererror';
157 0 $self->metric_count( 'spamassassin_total', $self->{ 'metrics_data' } );
158 0 return;
159 }
160  
161 0 my $message = join( q{} , @{$self->{'lines'} } );
162  
163 0 my $sa_status = $sa_client->_filter( $message, 'SYMBOLS' );
164 #my $sa_status = $sa_client->check( $message );
165  
166 my $status = join( q{},
167 ( $sa_status->{'isspam'} eq 'False' ? 'No, ' : 'Yes, ' ),
168 'score=', sprintf( '%.02f', $sa_status->{'score'} ),
169 ' ',
170 0 ▶ 'required=', sprintf( '%.02f', $sa_status->{'threshold'} ),
Branch: 1 branch
BranchTrueFalseTotal
$sa_status->{'isspam'} eq 'False' ? : 0 0 0
171 );
172  
173 0 my $hits = $sa_status->{'message'};
174 # Wrap hits header
175 {
176 0 my @hitsplit = split ',', $hits;
177 0 my $header = q{};
178 0 my $max = 74;
179 0 my $part = q{};
180 0 my $last_hit = pop @hitsplit;
181 0 @hitsplit = map { "$_," } @hitsplit;
182 0 push @hitsplit, $last_hit;
183 0 foreach my $hit ( @hitsplit ) {
184 0 ▶ if ( length ( $part . $hit ) > $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $part . $hit > $max) 0 0 0
185 0 $header .= $part . "\n ";
186 0 $part = q{};
187 }
188 0 $part .= $hit;
189 }
190 0 $header .= $part;
191 0 $hits = $header;
192 }
193  
194 0 $self->prepend_header( 'X-Spam-score', sprintf( '%.02f', $sa_status->{'score'} ) );
195 0 $self->prepend_header( 'X-Spam-Status', $status );
196 0 $self->prepend_header( 'X-Spam-hits', $hits );
197  
198 0 ▶ my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-spam' )->safe_set_value( ( $sa_status->{'isspam'} eq 'False' ? 'pass' : 'fail' ) );
Branch: 1 branch
BranchTrueFalseTotal
$sa_status->{'isspam'} eq 'False' ? : 0 0 0
199 0 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'score' )->safe_set_value( sprintf ( '%.02f', $sa_status->{'score'} ) ) );
200 0 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'required' )->safe_set_value( sprintf ( '%.02f', $sa_status->{'threshold'} ) ) );
201 0 $self->add_auth_header($header);
202  
203 0 ▶ $self->{ 'metrics_data' }->{ 'result' } = ( $sa_status->{'isspam'} eq 'False' ? 'pass' : 'fail' );
Branch: 1 branch
BranchTrueFalseTotal
$sa_status->{'isspam'} eq 'False' ? : 0 0 0
204  
205 0 ▶ if ( $sa_status->{'isspam'} eq 'True' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sa_status->{"isspam"} eq "True") 0 0 0
206 0 ▶ if ( $config->{'hard_reject_at'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"hard_reject_at"}) 0 0 0
207 0 ▶ if ( $sa_status->{'score'} >= $config->{'hard_reject_at'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sa_status->{"score"} >= $config->{"hard_reject_at"}) 0 0 0
208 0 ▶ if ( ( ! $self->is_local_ip_address() ) && ( ! $self->is_trusted_ip_address() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_local_ip_address or $self->is_trusted_ip_address) 0 0 0
Condition0%0/3
$self->is_local_ip_address or $self->is_trusted_ip_address
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->is_local_ip_address or $self->is_trusted_ip_address
$self->is_local_ip_address $self->is_trusted_ip_address
Truth table0%0/3
A or B
A= $self->is_local_ip_addressB= $self->is_trusted_ip_address
ABresult
1X1
011
000
209 0 $self->reject_mail( '550 5.7.0 SPAM policy violation' );
210 0 $self->dbgout( 'SpamAssassinReject', "Policy reject", LOG_INFO );
211 }
212 }
213 }
214 }
215  
216 0 $self->metric_count( 'spamassassin_total', $self->{ 'metrics_data' } );
217 0 ▶ return if ( lc $config->{'remove_headers'} eq 'no' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $config->{"remove_headers"} eq "no" 0 0 0
218  
219 0 foreach my $header_type ( qw{ X-Spam-score X-Spam-Status X-Spam-hits } ) {
220 0 ▶ if ( exists( $self->{'remove_headers'}->{ lc $header_type } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"remove_headers"}{lc $header_type}) 0 0 0
221 0 foreach my $header ( reverse @{ $self->{'remove_headers'}->{ lc $header_type } } ) {
222 0 $self->dbgout( 'RemoveSpamHeader', $header_type . ', ' . $header, LOG_DEBUG );
223 0 $self->change_header( lc $header_type, $header, q{} );
224 }
225 }
226 }
227  
228 0 return;
229 }
230  
231 sub close_callback {
232 0 ▶ my ( $self ) = @_;
Subroutine close_callback: not called CC=1 SCAR=6.9
Pod undocumented
233  
234 0 delete $self->{'lines'};
235 0 delete $self->{'rcpt_to'};
236 0 delete $self->{'remove_headers'};
237 0 delete $self->{'header_index'};
238 0 delete $self->{'metrics_data'};
239 0 return;
240 }
241  
242 1 1;
243  
244 __END__
245  
246 =pod
247  
248 =encoding UTF-8
249  
250 =head1 NAME
251  
252 Mail::Milter::Authentication::Handler::SpamAssassin - SpamAssassin scanner for Authentication Milter
253  
254 =head1 VERSION
255  
256 version 2.20180318
257  
258 =head1 DESCRIPTION
259  
260 Check email for spam using SpamAssassin spamd.
261  
262 =head1 CONFIGURATION
263  
264 "SpamAssassin" : {
265 "default_user" : "nobody",
266 "sa_host" : "localhost",
267 "sa_port" : "783",
268 "hard_reject_at" : "10",
269 "remove_headers" : "yes"
270 },
271  
272 =head2 CONFIG
273  
274 Add a block to the handlers section of your config as follows.
275  
276 "SpamAssassin" : {
277 "default_user" : "nobody",
278 "sa_host" : "localhost",
279 "sa_port" : "783",
280 "hard_reject_at" : "10",
281 "remove_headers" : "yes"
282 },
283  
284 =head1 AUTHOR
285  
286 Marc Bradshaw <marc@marcbradshaw.net>
287  
288 =head1 COPYRIGHT AND LICENSE
289  
290 This software is copyright (c) 2018 by Marc Bradshaw.
291  
292 This is free software; you can redistribute it and/or modify it under
293 the same terms as the Perl 5 programming language system itself.
294  
295 =cut
↑ Summary Handler/RSpamD.pm »