| 1 |
#!perl -w |
| 2 |
# H+B EDV-AV plugin. |
| 3 |
|
| 4 |
=head1 NAME |
| 5 |
|
| 6 |
hbedv - plugin for qpsmtpd which calls the H+BEDV anti virus scanner |
| 7 |
|
| 8 |
=head1 DESCRIPTION |
| 9 |
|
| 10 |
The B<hbedv> plugin checks a mail for viruses with the H+BEDV anti virus |
| 11 |
scanner (see L<http://www.antivir.de/> for info). It can deny mails if a |
| 12 |
virus was found with a configurable deny list. |
| 13 |
|
| 14 |
=head1 VERSION |
| 15 |
|
| 16 |
this is B<hbedv> version 1.1 |
| 17 |
|
| 18 |
=head1 CONFIGURATION |
| 19 |
|
| 20 |
Add (perl-)regexps to the F<hbedv_deny> configuration file, one per line for the |
| 21 |
virii you want to block, e.g.: |
| 22 |
|
| 23 |
Worm\/Sober\..* |
| 24 |
Worm\/NetSky\..* |
| 25 |
|
| 26 |
or just |
| 27 |
|
| 28 |
.* |
| 29 |
|
| 30 |
to block any virus ;) |
| 31 |
|
| 32 |
Set the location of the binary with |
| 33 |
|
| 34 |
hbedv hbedvscanner /path/to/antivir |
| 35 |
|
| 36 |
in the plugin config if qpsmtpd, the location defaults to I</usr/bin/antivir>. |
| 37 |
|
| 38 |
=head1 NOTES |
| 39 |
|
| 40 |
If the hbedv_deny config file is empty or could not be found, any virus |
| 41 |
will be blocked. |
| 42 |
|
| 43 |
This plugin started life as a copy of the B<clamav> plugin. |
| 44 |
|
| 45 |
=head1 LICENCE |
| 46 |
|
| 47 |
Written by Hanno Hecker E<lt>[email protected]<gt>. |
| 48 |
|
| 49 |
The B<hbedv> plugin is published under the same licence as qpsmtpd itself. |
| 50 |
|
| 51 |
=cut |
| 52 |
|
| 53 |
sub register { |
| 54 |
my ($self, $qp, @args) = @_; |
| 55 |
|
| 56 |
if (@args % 2) { |
| 57 |
$self->log(LOGERROR, "FATAL ERROR: odd number of arguments"); |
| 58 |
exit 3; |
| 59 |
} |
| 60 |
my %args = @args; |
| 61 |
if (!exists $args{hbedvscanner}) { |
| 62 |
$self->{_hbedvscan_loc} = "/usr/bin/antivir"; |
| 63 |
} |
| 64 |
else { |
| 65 |
if ($args{hbedvscanner} =~ /^(\/[\/\-\_\.a-z0-9A-Z]*)$/) { |
| 66 |
$self->{_hbedvscan_loc} = $1; |
| 67 |
} |
| 68 |
else { |
| 69 |
$self->log(LOGERROR, |
| 70 |
"FATAL ERROR: Unexpected characters in hbedvscanner argument"); |
| 71 |
exit 3; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
sub hook_data_post { |
| 77 |
my ($self, $transaction) = @_; |
| 78 |
|
| 79 |
my $filename = $transaction->body_filename; |
| 80 |
unless (defined $filename) { |
| 81 |
$self->log(LOGWARN, "didn't get a file name"); |
| 82 |
return (DECLINED); |
| 83 |
} |
| 84 |
|
| 85 |
# Now do the actual scanning! |
| 86 |
my $cmd = $self->{_hbedvscan_loc} |
| 87 |
. " --archive-max-recursion=50 --alltypes -z -noboot -nombr -rs $filename 2>&1"; |
| 88 |
$self->log(LOGDEBUG, "Running: $cmd"); |
| 89 |
my @output = `$cmd`; |
| 90 |
|
| 91 |
my $result = ($? >> 8); |
| 92 |
my $signal = ($? & 127); |
| 93 |
|
| 94 |
chomp(@output); |
| 95 |
my @virii = (); |
| 96 |
foreach my $line (@output) { |
| 97 |
next |
| 98 |
unless $line =~ |
| 99 |
/^ALERT: \[([^\]]+)\s+(\w+)?\]/; # $2 =~ /^(virus|worm)$/; |
| 100 |
push @virii, $1; |
| 101 |
} |
| 102 |
@virii = unique(@virii); |
| 103 |
|
| 104 |
$self->log(LOGDEBUG, "results: " . join("//", @output)); |
| 105 |
|
| 106 |
if ($signal) { |
| 107 |
$self->log(LOGWARN, "scanner exited with signal: $signal"); |
| 108 |
return (DECLINED); |
| 109 |
} |
| 110 |
my $output = join(", ", @virii); |
| 111 |
$output = substr($output, 0, 60); |
| 112 |
if ($result == 1 || $result == 3) { |
| 113 |
$self->log(LOGWARN, "Virus(es) found: $output"); |
| 114 |
|
| 115 |
# return (DENY, "Virus Found: $output"); |
| 116 |
# $transaction->header->add('X-Virus-Found', 'Yes', 0); |
| 117 |
# $transaction->header->add('X-Virus-Details', $output, 0); |
| 118 |
$transaction->header->add('X-H+BEDV-Virus-Found', 'Yes', 0); |
| 119 |
$transaction->header->add('X-H+BEDV-Virus-Details', $output, 0); |
| 120 |
} |
| 121 |
elsif ($result == 200) { |
| 122 |
$self->log(LOGWARN, "Program aborted, not enough memory available"); |
| 123 |
} |
| 124 |
elsif ($result == 211) { |
| 125 |
$self->log(LOGWARN, "Programm aborted, because the self check failed"); |
| 126 |
} |
| 127 |
elsif ($result == 214) { |
| 128 |
$self->log(LOGWARN, "License key not found"); |
| 129 |
} |
| 130 |
elsif ($result) { |
| 131 |
$self->log(LOGWARN, |
| 132 |
"Error: $result, look for exit codes in the output of '" |
| 133 |
. $self->{_hbedvscan_loc} |
| 134 |
. " --help' for more info\n" |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
# $transaction->header->add('X-Virus-Checked', 'Checked', 0); |
| 139 |
$transaction->header->add('X-H+BEDV-Virus-Checked', 'Checked', 0); |
| 140 |
return (DECLINED) unless $result; |
| 141 |
|
| 142 |
if (@virii) { |
| 143 |
return (DENY, "Virus found: $output") |
| 144 |
unless $self->qp->config("hbedv_deny"); |
| 145 |
foreach my $d ($self->qp->config("hbedv_deny")) { |
| 146 |
foreach my $v (@virii) { |
| 147 |
if ($v =~ /^$d$/i) { |
| 148 |
$self->log(LOGWARN, "Denying mail with virus '$v'"); |
| 149 |
return (DENY, "Virus found: $output"); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
return (DECLINED); |
| 155 |
} |
| 156 |
|
| 157 |
sub unique { |
| 158 |
## This is the short version, I haven't tried if any warnings |
| 159 |
## are generated by perl if you use just this... if you need |
| 160 |
## every cpu cycle, try this: |
| 161 |
## my %h;foreach (@_) { ++$h{$_}; }; return keys(%h); |
| 162 |
my @list = @_; |
| 163 |
my %hash; |
| 164 |
foreach my $item (@list) { |
| 165 |
exists $hash{$item} || ($hash{$item} = 1); |
| 166 |
} |
| 167 |
return keys(%hash); |
| 168 |
} |