Handler/ClamAV.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 146 CC 7 SCAR 28.1
CC
7
Cov
0%
eom_callback
56.0
default_config
2.0
grafana_rows
2.0
CRAP 16.5
1 package Mail::Milter::Authentication::Handler::ClamAV;
2 use strict;
3 use warnings;
4 use base 'Mail::Milter::Authentication::Handler';
5 our $VERSION = '2.20180510'; # VERSION
6 # ABSTRACT: Clam AV Scanner for Authentication Milter
7  
8 use English qw{ -no_match_vars };
9 use Sys::Syslog qw{:standard :macros};
10 use ClamAV::Client;
11 use JSON;
12 use Mail::AuthenticationResults::Header::Entry;
13 use Mail::AuthenticationResults::Header::SubEntry;
14 use Mail::AuthenticationResults::Header::Comment;
15  
16 use Data::Dumper;
17  
18 sub default_config {
19 return {
20 'ca_name' => '/var/run/clamav/clamd.ctl',
21 'ca_host' => '127.0.0.1',
22 'ca_port' => '3310',
23 'hard_reject' => 1,
24 };
25 }
26  
27 sub grafana_rows {
28 my ( $self ) = @_;
29 my @rows;
30 push @rows, $self->get_json( 'ClamAV_metrics' );
31 return \@rows;
32 }
33  
34 sub register_metrics {
35 return {
36 'clamav_total' => 'The number of emails processed for ClamAV',
37 };
38 }
39  
40 sub envfrom_callback {
41 my ($self, $from) = @_;
42 $self->{'lines'} = [];
43 return;
44 }
45  
46 sub header_callback {
47 my ( $self, $header, $value ) = @_;
48 push @{$self->{'lines'}} ,$header . ': ' . $value . "\r\n";
49 my $config = $self->handler_config();
50 return;
51 }
52  
53 sub eoh_callback {
54 my ( $self ) = @_;
55 push @{$self->{'lines'}} , "\r\n";
56 return;
57 }
58  
59 sub body_callback {
60 my ( $self, $chunk ) = @_;
61 push @{$self->{'lines'}} , $chunk;
62 return;
63 }
64  
65 sub eom_callback {
66 my ($self) = @_;
67  
68 my $config = $self->handler_config();
69  
70 my %args;
71 foreach my $param ( qw{ name host port } ) {
72 $args{ 'socket_' . $param } = $config->{ 'socket_' . $param } if $config->{ 'socket_' . $param };
73 }
74  
75 my $scanner = ClamAV::Client->new( %args );
76  
77 if ( ! $scanner ) {
78 $self->log_error( 'ClamAVError: No Scanner' );
79 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-virus' )->safe_set_value( 'temperror' );
80 $self->add_auth_header( $header );
81 $self->metric_count( 'clamav_total', { 'result' => 'noscanner' } );
82 return;
83 }
84  
85 if ( ! $scanner->ping() ) {
86 $self->log_error( 'ClamAVError: Scanner Ping Failed' );
87 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-virus' )->safe_set_value( 'temperror' );
88 $self->add_auth_header( $header );
89 $self->metric_count( 'clamav_total', { 'result' => 'scannerpingfail' } );
90 return;
91 }
92  
93 my $message = join( q{} , @{$self->{'lines'} } );
94 my $result = $scanner->scan_scalar( \$message );
95  
96 if ( $result ) {
97 $self->dbgout( 'ClamAV: Virus Found', $result, LOG_INFO );
98 $self->metric_count( 'clamav_total', { 'result' => 'fail' } );
99 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-virus' )->safe_set_value( 'fail' );
100 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( $result ) );
101 $self->add_auth_header($header);
102 if ( $config->{'hard_reject'} ) {
103 if ( ( ! $self->is_local_ip_address() ) && ( ! $self->is_trusted_ip_address() ) ) {
104 $self->reject_mail( '550 5.7.0 Virus policy violation' );
105 $self->dbgout( 'ClamAVReject', "Policy reject", LOG_INFO );
106 }
107 }
108 }
109 else {
110 $self->metric_count( 'clamav_total', { 'result' => 'pass' } );
111 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-virus' )->safe_set_value( 'pass' );
112 $self->add_auth_header($header);
113 }
114  
115 return;
116 }
117  
118 sub close_callback {
119 my ( $self ) = @_;
120 delete $self->{'lines'};
121 return;
122 }
123  
124 1;
125  
126 __END__
127  
128 =pod
129  
130 =encoding UTF-8
131  
132 =head1 NAME
133  
134 Mail::Milter::Authentication::Handler::ClamAV - Clam AV Scanner for Authentication Milter
135  
136 =head1 VERSION
137  
138 version 2.20180510
139  
140 =head1 DESCRIPTION
141  
142 Virus check email for using clamav.
143  
144 =head1 CONFIGURATION
145  
146 "ClamAV" : {
147 "hard_reject" : "1",
148 "ca_name" : "/var/run/clamav/clamd.ctl",
149 "ca_host" : "127.0.0.1",
150 "ca_port" : "3310"
151 },
152  
153 =head2 CONFIG
154  
155 Add a block to the handlers section of your config as follows.
156  
157 "ClamAV" : {
158 "hard_reject" : "1",
159 "ca_name" : "/var/run/clamav/clamd.ctl",
160 "ca_host" : "127.0.0.1",
161 "ca_port" : "3310"
162 },
163  
164 =head1 AUTHOR
165  
166 Marc Bradshaw <marc@marcbradshaw.net>
167  
168 =head1 COPYRIGHT AND LICENSE
169  
170 This software is copyright (c) 2018 by Marc Bradshaw.
171  
172 This is free software; you can redistribute it and/or modify it under
173 the same terms as the Perl 5 programming language system itself.
174  
175 =cut