| 1 |
#!perl -w |
| 2 |
# Kasperski-AV plugin. |
| 3 |
|
| 4 |
=head1 NAME |
| 5 |
|
| 6 |
kavscanner - plugin for qpsmtpd which calls the Kasperski anti virus scanner |
| 7 |
|
| 8 |
=head1 DESCRIPTION |
| 9 |
|
| 10 |
Check a mail with the B<kavscanner> and deny if it matches a configured virus |
| 11 |
list. |
| 12 |
|
| 13 |
=head1 VERSION |
| 14 |
|
| 15 |
this is B<kavscanner> version 1.0 |
| 16 |
|
| 17 |
=head1 CONFIGURATION |
| 18 |
|
| 19 |
Add (perl-)regexps to the F<kav_deny> configuration file, one per line for the |
| 20 |
virii you want to block, e.g.: |
| 21 |
|
| 22 |
I-Worm\.Sober\..* |
| 23 |
I-Worm\.NetSky\..* |
| 24 |
|
| 25 |
NOTE: untested and disabled currently, need volunteers :-) |
| 26 |
|
| 27 |
If this list does not match the virus found in the mail, you may set |
| 28 |
I<bcc_virusadmin [email protected]> in the plugin config to send a |
| 29 |
B<Bcc:> to the given mail address, i.e. the line |
| 30 |
|
| 31 |
kavscanner bcc_virusadmin [email protected] |
| 32 |
|
| 33 |
in the F<config/plugin> file instead of just |
| 34 |
|
| 35 |
kavscanner |
| 36 |
|
| 37 |
Set the location of the binary with |
| 38 |
|
| 39 |
kavscanner kavscanner_bin /path/to/kavscanner |
| 40 |
|
| 41 |
(default: F</opt/AVP/kavscanner>), NOTE: this may be broken, you want to |
| 42 |
set B<kavscanner_bin> explicitly ;-) |
| 43 |
|
| 44 |
=head1 NOTES |
| 45 |
|
| 46 |
This is a merge of the clam_av plugin for qpsmtpd and qmail-scanner-queue.pl |
| 47 |
L<http://qmail-scanner.sourceforge.net/> with my own improvements ;-) |
| 48 |
Only tested with kavscanner 4.0.x, and bcc_virusadmin untested, as we have no |
| 49 |
use for it currently. I wait for an official change in Qpsmtpd::Transaction |
| 50 |
(reset/set the RCPT TO list) to activate and test the currently disabled |
| 51 |
B<to_virusadmin> option. |
| 52 |
|
| 53 |
=cut |
| 54 |
|
| 55 |
use File::Temp qw(tempfile); |
| 56 |
use Mail::Address; |
| 57 |
|
| 58 |
sub register { |
| 59 |
my ($self, $qp, @args) = @_; |
| 60 |
|
| 61 |
if (@args % 2) { |
| 62 |
$self->log(LOGWARN, "kavscanner: Wrong number of arguments"); |
| 63 |
$self->{_kavscanner_bin} = "/opt/AVP/kavscanner"; |
| 64 |
} |
| 65 |
else { |
| 66 |
my %args = @args; |
| 67 |
foreach my $key (keys %args) { |
| 68 |
my $arg = $key; |
| 69 |
$key =~ s/^/_/; |
| 70 |
$self->{$key} = $args{$arg}; |
| 71 |
} |
| 72 |
|
| 73 |
# Untaint scanner location |
| 74 |
if (exists $self->{_kavscanner_bin} |
| 75 |
&& $self->{_kavscanner_bin} =~ /^(\/[\/\-\_\.a-z0-9A-Z]*)$/) |
| 76 |
{ |
| 77 |
$self->{_kavscanner_bin} = $1; |
| 78 |
} |
| 79 |
else { |
| 80 |
$self->log(LOGALERT, |
| 81 |
"FATAL ERROR: Unexpected characters in kavscanner argument"); |
| 82 |
exit 3; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
sub hook_data_post { |
| 88 |
my ($self, $transaction) = @_; |
| 89 |
|
| 90 |
my ($temp_fh, $filename) = tempfile(); |
| 91 |
print $temp_fh $transaction->header->as_string; |
| 92 |
print $temp_fh "\n"; |
| 93 |
$transaction->body_resetpos; |
| 94 |
while (my $line = $transaction->body_getline) { |
| 95 |
print $temp_fh $line; |
| 96 |
} |
| 97 |
seek($temp_fh, 0, 0); |
| 98 |
|
| 99 |
# Now do the actual scanning! |
| 100 |
my $cmd = $self->{_kavscanner_bin} . " -Y -P -B -MP -MD -* $filename 2>&1"; |
| 101 |
$self->log(LOGNOTICE, "Running: $cmd"); |
| 102 |
my @output = `$cmd`; |
| 103 |
chomp(@output); |
| 104 |
|
| 105 |
my $result = ($? >> 8); |
| 106 |
my $signal = ($? & 127); |
| 107 |
|
| 108 |
unlink($filename); |
| 109 |
close $temp_fh; |
| 110 |
|
| 111 |
if ($signal) { |
| 112 |
$self->log(LOGWARN, "kavscanner exited with signal: $signal"); |
| 113 |
return (DECLINED); |
| 114 |
} |
| 115 |
|
| 116 |
my $description = 'clean'; |
| 117 |
my @infected = (); |
| 118 |
my @suspicious = (); |
| 119 |
if ($result > 0) { |
| 120 |
if ($result =~ /^(2|3|4|8)$/) { |
| 121 |
foreach (@output) { |
| 122 |
if (/^.* infected: (.*)$/) { |
| 123 |
|
| 124 |
# This covers the specific |
| 125 |
push @infected, $1; |
| 126 |
} |
| 127 |
elsif (/^\s*.* suspicion: (.*)$/) { |
| 128 |
|
| 129 |
# This covers the potential viruses |
| 130 |
push @suspicious, $1; |
| 131 |
} |
| 132 |
} |
| 133 |
$description = |
| 134 |
"infected by: " |
| 135 |
. join(", ", @infected) . "; " |
| 136 |
. "suspicions: " |
| 137 |
. join(", ", @suspicious); |
| 138 |
|
| 139 |
# else we may get a veeeery long X-Virus-Details: line or log entry |
| 140 |
$description = substr($description, 0, 60); |
| 141 |
$self->log(LOGWARN, "There be a virus! ($description)"); |
| 142 |
### Untested by now, need volunteers ;-) |
| 143 |
#if ($self->qp->config("kav_deny")) { |
| 144 |
# foreach my $d (keys %{$self->qp->config("kav_deny", "map")}) { |
| 145 |
# foreach my $v (@infected) { |
| 146 |
# return(DENY, "Virus found: $description") |
| 147 |
# if ($v =~ /^$d$/i); |
| 148 |
# } |
| 149 |
# foreach my $s (@suspicious) { |
| 150 |
# return(DENY, "Virus found: $description") |
| 151 |
# if ($s =~ /^$d$/i); |
| 152 |
# } |
| 153 |
# } |
| 154 |
#} |
| 155 |
$transaction->header->add('X-Virus-Found', 'Yes'); |
| 156 |
$transaction->header->add('X-Virus-Details', $description); |
| 157 |
### maybe the spamassassin plugin can skip this mail if a virus |
| 158 |
### was found (and $transaction->notes('virus_flag') exists :)) |
| 159 |
### ...ok, works with our spamassassin plugin version |
| 160 |
### -- hah |
| 161 |
$transaction->notes('virus', $description); |
| 162 |
$transaction->notes('virus_flag', 'Yes'); |
| 163 |
|
| 164 |
#### requires modification of Qpsmtpd/Transaction.pm: |
| 165 |
# if ($self->{_to_virusadmin}) { |
| 166 |
# my @addrs = (); |
| 167 |
# foreach (@{$transaction->recipients}) { |
| 168 |
# push @addr, $_->address; |
| 169 |
# } |
| 170 |
# $transaction->header->add('X-Virus-Orig-RcptTo', join(", ", @addrs)); |
| 171 |
# $transaction->set_recipients(@{ Mail::Address->parse($self->{_to_virusadmin}) }); |
| 172 |
# } elsif ($self->{_bcc_virusadmin}) { |
| 173 |
if ($self->{_bcc_virusadmin}) { |
| 174 |
foreach (@{Mail::Address->parse($self->{_bcc_virusadmin})}) { |
| 175 |
$transaction->add_recipient($_); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
else { |
| 180 |
$self->log(LOGEMERG, |
| 181 |
"corrupt or unknown Kaspersky scanner/resource problems - exit status $result" |
| 182 |
); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
$self->log(LOGINFO, "kavscanner results: $description"); |
| 187 |
|
| 188 |
$transaction->header->add('X-Virus-Checked', |
| 189 |
'Checked by ' . $self->qp->config("me")); |
| 190 |
return (DECLINED); |
| 191 |
} |
| 192 |
|