2012-04-29 05:41:31 +02:00
|
|
|
#!perl -Tw
|
2005-03-01 21:11:09 +01:00
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
clamdscan
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
A qpsmtpd plugin for virus scanning using the ClamAV scan daemon, clamd.
|
|
|
|
|
|
|
|
=head1 RESTRICTIONS
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
The ClamAV scan daemon, clamd, must have at least execute access to the qpsmtpd
|
|
|
|
spool directory in order to sucessfully scan the messages. You can ensure this
|
|
|
|
by running clamd as the same user as qpsmtpd does, or by doing the following:
|
2005-03-01 21:11:09 +01:00
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
|
|
|
=item * Change the group ownership of the spool directory to be a group
|
|
|
|
of which clamav is a member or add clamav to the same group as the qpsmtpd
|
|
|
|
user.
|
|
|
|
|
|
|
|
=item * Enable the "AllowSupplementaryGroups" option in clamd.conf.
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
=item * Add group-execute permissions to the qpsmtpd spool directory.
|
2005-03-01 21:11:09 +01:00
|
|
|
|
|
|
|
=item * Make sure that all directories above the spool directory (to the
|
|
|
|
root) are g+x so that the group has directory traversal rights; it is not
|
2009-04-17 01:10:55 +02:00
|
|
|
necessary for the group to have any read rights.
|
2005-03-01 21:11:09 +01:00
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
It may be helpful to temporary grant the clamav user a shell and test to
|
|
|
|
make sure you can cd into the spool directory and read files located there.
|
|
|
|
Remember to remove the shell from the clamav user when you are done
|
|
|
|
testing.
|
|
|
|
|
|
|
|
=head1 INSTALL AND CONFIG
|
|
|
|
|
|
|
|
Place this plugin in the plugin/virus directory beneath the standard
|
|
|
|
qpsmtpd installation. If you installed clamd with the default path, you
|
|
|
|
can use this plugin with default options (nothing specified):
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
You must have the ClamAV::Client module installed to use the plugin.
|
|
|
|
|
2005-03-01 21:11:09 +01:00
|
|
|
=over 4
|
|
|
|
|
|
|
|
=item B<clamd_socket>
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
Full path to the clamd socket (the recommended mode), if different from the
|
|
|
|
ClamAV::Client defaults.
|
2005-03-01 21:11:09 +01:00
|
|
|
|
|
|
|
=item B<clamd_port>
|
|
|
|
|
|
|
|
If present, must be the TCP port where the clamd service is running,
|
2005-03-04 17:04:59 +01:00
|
|
|
typically 3310; default disabled. If present, overrides the clamd_socket.
|
2005-03-01 21:11:09 +01:00
|
|
|
|
|
|
|
=item B<deny_viruses>
|
|
|
|
|
|
|
|
Whether the scanner will automatically delete messages which have viruses.
|
|
|
|
Takes either 'yes' or 'no' (defaults to 'yes'). If set to 'no' it will add
|
|
|
|
a header to the message with the virus results.
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
=item B<defer_on_error>
|
|
|
|
|
|
|
|
Whether to defer the mail (with a soft-failure error, which will incur a retry)
|
|
|
|
if an unrecoverable error occurs during the scan. The default is to accept
|
|
|
|
the mail under these conditions. This can permit viruses to be accepted when
|
|
|
|
the clamd daemon is malfunctioning or unreadable, but will not allow mail to
|
|
|
|
backlog or be lost if the condition persists.
|
|
|
|
|
2005-03-01 21:11:09 +01:00
|
|
|
=item B<max_size>
|
|
|
|
|
|
|
|
The maximum size, in kilobytes, of messages to scan; defaults to 128k.
|
|
|
|
|
2007-11-17 08:55:32 +01:00
|
|
|
=item B<scan_all>
|
|
|
|
|
|
|
|
Scan all messages, even if there are no attachments
|
|
|
|
|
2005-03-01 21:11:09 +01:00
|
|
|
=back
|
|
|
|
|
|
|
|
=head1 REQUIREMENTS
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
This module requires the ClamAV::Client module, found on CPAN here:
|
2005-03-01 21:11:09 +01:00
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
L<http://search.cpan.org/dist/ClamAV-Client/>
|
2005-03-01 21:11:09 +01:00
|
|
|
|
|
|
|
=head1 AUTHOR
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
Originally written for the Clamd module by John Peacock <jpeacock@cpan.org>;
|
|
|
|
adjusted for ClamAV::Client by Devin Carraway <qpsmtpd/@/devin.com>.
|
2005-03-01 21:11:09 +01:00
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
Copyright (c) 2005 John Peacock,
|
|
|
|
Copyright (c) 2007 Devin Carraway
|
2005-03-01 21:11:09 +01:00
|
|
|
|
|
|
|
Based heavily on the clamav plugin
|
|
|
|
|
|
|
|
This plugin is licensed under the same terms as the qpsmtpd package itself.
|
|
|
|
Please see the LICENSE file included with qpsmtpd for details.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
use ClamAV::Client;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
2005-03-01 21:11:09 +01:00
|
|
|
|
|
|
|
sub register {
|
|
|
|
my ( $self, $qp, @args ) = @_;
|
|
|
|
|
2010-05-02 07:48:05 +02:00
|
|
|
$self->log(LOGERROR, "Bad parameters for the clamdscan plugin") if @_ % 2;
|
2005-03-01 21:11:09 +01:00
|
|
|
%{ $self->{"_clamd"} } = @args;
|
|
|
|
|
|
|
|
# Set some sensible defaults
|
|
|
|
$self->{"_clamd"}->{"deny_viruses"} ||= "yes";
|
|
|
|
$self->{"_clamd"}->{"max_size"} ||= 128;
|
2007-11-17 08:55:32 +01:00
|
|
|
$self->{"_clamd"}->{"scan_all"} ||= 0;
|
2009-04-17 01:10:55 +02:00
|
|
|
for my $setting ('deny_viruses', 'defer_on_error') {
|
|
|
|
next unless $self->{"_clamd"}->{$setting};
|
|
|
|
$self->{"_clamd"}->{$setting} = 0
|
|
|
|
if lc $self->{"_clamd"}->{$setting} eq 'no';
|
|
|
|
}
|
2005-03-01 21:11:09 +01:00
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_data_post {
|
2005-03-01 21:11:09 +01:00
|
|
|
my ( $self, $transaction ) = @_;
|
|
|
|
$DB::single = 1;
|
|
|
|
|
2006-12-16 12:56:48 +01:00
|
|
|
if ( $transaction->data_size > $self->{"_clamd"}->{"max_size"} * 1024 ) {
|
|
|
|
$self->log( LOGNOTICE, "Declining due to data_size" );
|
2005-03-01 21:11:09 +01:00
|
|
|
return (DECLINED);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Ignore non-multipart emails
|
|
|
|
my $content_type = $transaction->header->get('Content-Type');
|
|
|
|
$content_type =~ s/\s/ /g if defined $content_type;
|
2007-11-17 09:16:46 +01:00
|
|
|
unless ( $self->{"_clamd"}->{"scan_all"}
|
2007-11-17 08:55:32 +01:00
|
|
|
|| $content_type
|
2005-03-01 21:11:09 +01:00
|
|
|
&& $content_type =~ m!\bmultipart/.*\bboundary="?([^"]+)!i )
|
|
|
|
{
|
2005-12-10 22:11:04 +01:00
|
|
|
$self->log( LOGNOTICE, "non-multipart mail - skipping" );
|
2005-03-01 21:11:09 +01:00
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $filename = $transaction->body_filename;
|
|
|
|
unless ($filename) {
|
|
|
|
$self->log( LOGWARN, "Cannot process due to lack of filename" );
|
|
|
|
return (DECLINED); # unless $filename;
|
|
|
|
}
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
# the spool directory must be readable and executable by the scanner;
|
|
|
|
# this generally means either group or world exec; if
|
|
|
|
# neither of these is set, issue a warning but try to proceed anyway
|
2005-03-01 21:11:09 +01:00
|
|
|
my $mode = ( stat( $self->spool_dir() ) )[2];
|
2009-04-17 01:10:55 +02:00
|
|
|
if ( $mode & 0010 || $mode & 0001 ) {
|
|
|
|
# match the spool file mode with the mode of the directory -- add
|
|
|
|
# the read bit for group, world, or both, depending on what the
|
|
|
|
# spool dir had, and strip all other bits, especially the sticky bit
|
|
|
|
my $fmode = ($mode & 0044) |
|
|
|
|
($mode & 0010 ? 0040 : 0) |
|
|
|
|
($mode & 0001 ? 0004 : 0);
|
|
|
|
unless ( chmod $fmode, $filename ) {
|
|
|
|
$self->log( LOGERROR, "chmod: $filename: $!" );
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
} else {
|
2005-03-01 21:11:09 +01:00
|
|
|
$self->log( LOGWARN,
|
2009-04-17 01:10:55 +02:00
|
|
|
"Permission on spool directory do not permit scanner access" );
|
2005-03-01 21:11:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $clamd;
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
if ( ($self->{"_clamd"}->{"clamd_port"} || '') =~ /^(\d+)/ ) {
|
|
|
|
$clamd = new ClamAV::Client( socket_host =>
|
|
|
|
$self->{_clamd}->{clamd_host},
|
|
|
|
socket_port => $1 );
|
|
|
|
}
|
|
|
|
elsif ( ($self->{"_clamd"}->{"clamd_socket"} || '') =~ /([\w\/.]+)/ ) {
|
|
|
|
$clamd = new ClamAV::Client( socket_name => $1 );
|
2005-03-01 21:11:09 +01:00
|
|
|
}
|
|
|
|
else {
|
2009-04-17 01:10:55 +02:00
|
|
|
$clamd = new ClamAV::Client;
|
2005-03-01 21:11:09 +01:00
|
|
|
}
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
unless ( $clamd ) {
|
|
|
|
$self->log( LOGERROR, "Cannot instantiate ClamAV::Client" );
|
|
|
|
return (DENYSOFT, "Unable to scan for viruses")
|
|
|
|
if $self->{"_clamd"}->{"defer_on_error"};
|
|
|
|
return DECLINED;
|
2005-12-10 22:11:04 +01:00
|
|
|
}
|
2005-03-01 21:11:09 +01:00
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
unless ( eval { $clamd->ping() } ) {
|
|
|
|
$self->log( LOGERROR, "Cannot ping clamd server: $@" );
|
|
|
|
return (DENYSOFT, "Unable to scan for viruses")
|
|
|
|
if $self->{"_clamd"}->{"defer_on_error"};
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
2010-05-02 07:48:05 +02:00
|
|
|
my @clamd_version = split(/\//, $clamd->version);
|
|
|
|
$self->{"_clamd"}->{'version'} = $clamd_version[0] || 'ClamAV';
|
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
my ( $path, $found ) = eval { $clamd->scan_path( $filename ) };
|
|
|
|
if ($@) {
|
|
|
|
$self->log( LOGERROR, "Error scanning mail: $@" );
|
|
|
|
return (DENYSOFT, "Unable to scan for viruses")
|
|
|
|
if $self->{"_clamd"}->{"defer_on_error"};
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
elsif ( $found ) {
|
|
|
|
$self->log( LOGERROR, "Virus found: $found" );
|
2005-03-01 21:11:09 +01:00
|
|
|
|
2009-04-17 01:10:55 +02:00
|
|
|
if ( $self->{"_clamd"}->{"deny_viruses"} ) {
|
|
|
|
return ( DENY, "Virus found: $found" );
|
2005-03-01 21:11:09 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$transaction->header->add( 'X-Virus-Found', 'Yes' );
|
2009-04-17 01:10:55 +02:00
|
|
|
$transaction->header->add( 'X-Virus-Details', $found );
|
2005-03-01 21:11:09 +01:00
|
|
|
return (DECLINED);
|
|
|
|
}
|
|
|
|
}
|
2009-04-17 01:10:55 +02:00
|
|
|
else {
|
2010-05-02 07:48:05 +02:00
|
|
|
$transaction->header->add( 'X-Virus-Found', 'No' );
|
2009-04-17 01:10:55 +02:00
|
|
|
$self->log( LOGINFO, "ClamAV scan reports clean");
|
|
|
|
}
|
2005-03-01 21:11:09 +01:00
|
|
|
|
|
|
|
$transaction->header->add( 'X-Virus-Checked',
|
2010-05-02 07:48:05 +02:00
|
|
|
"Checked by $self->{'_clamd'}->{'version'} on " . $self->qp->config("me") );
|
2005-03-01 21:11:09 +01:00
|
|
|
|
|
|
|
return (DECLINED);
|
|
|
|
}
|
2009-04-17 01:10:55 +02:00
|
|
|
|
|
|
|
# vi: set ts=4 sw=4 et:
|