| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
clamdscan |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
A qpsmtpd plugin for virus scanning using the ClamAV scan daemon, clamd. |
| 10 |
|
| 11 |
=head1 RESTRICTIONS |
| 12 |
|
| 13 |
If connecting to clamd via TCP/IP host:port, then ignore this restriction. |
| 14 |
|
| 15 |
The ClamAV scan daemon, clamd, must have at least execute access to the qpsmtpd |
| 16 |
spool directory in order to sucessfully scan the messages. You can ensure this |
| 17 |
by running clamd as the same user as qpsmtpd does, or by doing the following: |
| 18 |
|
| 19 |
=over 4 |
| 20 |
|
| 21 |
=item * Change the group ownership of the spool directory to be a group |
| 22 |
of which clamav is a member or add clamav to the same group as the qpsmtpd |
| 23 |
user. |
| 24 |
|
| 25 |
=item * Enable the "AllowSupplementaryGroups" option in clamd.conf. |
| 26 |
|
| 27 |
=item * Add group-execute permissions to the qpsmtpd spool directory. |
| 28 |
|
| 29 |
=item * Make sure that all directories above the spool directory (to the |
| 30 |
root) are g+x so that the group has directory traversal rights; it is not |
| 31 |
necessary for the group to have any read rights. |
| 32 |
|
| 33 |
=back |
| 34 |
|
| 35 |
It may be helpful to temporary grant the clamav user a shell and test to |
| 36 |
make sure you can cd into the spool directory and read files located there. |
| 37 |
Remember to remove the shell from the clamav user when you are done |
| 38 |
testing. |
| 39 |
|
| 40 |
=head1 INSTALL AND CONFIG |
| 41 |
|
| 42 |
Place this plugin in the plugin/virus directory beneath the standard |
| 43 |
qpsmtpd installation. If you installed clamd with the default path, you |
| 44 |
can use this plugin with default options (nothing specified): |
| 45 |
|
| 46 |
You must have the ClamAV::Client module installed to use the plugin. |
| 47 |
|
| 48 |
=over 4 |
| 49 |
|
| 50 |
=item B<clamd_socket> |
| 51 |
|
| 52 |
Full path to the clamd socket, if different from the ClamAV::Client defaults. |
| 53 |
|
| 54 |
=item B<clamd_host> |
| 55 |
|
| 56 |
IP address where clamd is listening. |
| 57 |
|
| 58 |
Default: localhost |
| 59 |
|
| 60 |
=item B<clamd_port> |
| 61 |
|
| 62 |
The TCP port where the clamd service is running, typically 3310. |
| 63 |
|
| 64 |
Default: disabled. When present, overrides clamd_socket. |
| 65 |
|
| 66 |
=item B<deny_viruses> |
| 67 |
|
| 68 |
Whether the scanner will automatically delete messages which have viruses. |
| 69 |
Takes either 'yes' or 'no'. If set to 'no', adds a header with the virus name. |
| 70 |
|
| 71 |
Default: yes |
| 72 |
|
| 73 |
=item B<defer_on_error> |
| 74 |
|
| 75 |
Whether to defer the mail (with a soft-failure error, which will incur a retry) |
| 76 |
if an unrecoverable error occurs during the scan. The default is to accept |
| 77 |
the mail under these conditions. This can permit viruses to be accepted when |
| 78 |
the clamd daemon is malfunctioning or unreadable, but will not allow mail to |
| 79 |
backlog or be lost if the condition persists. |
| 80 |
|
| 81 |
=item B<max_size> |
| 82 |
|
| 83 |
The maximum size, in kilobytes, of messages to scan. |
| 84 |
|
| 85 |
Default: 1024 (1 MB) |
| 86 |
|
| 87 |
=item B<scan_all> |
| 88 |
|
| 89 |
Scan all messages, even if there are no attachments |
| 90 |
|
| 91 |
=back |
| 92 |
|
| 93 |
=head1 REQUIREMENTS |
| 94 |
|
| 95 |
This module requires the ClamAV::Client module, found on CPAN here: |
| 96 |
|
| 97 |
L<http://search.cpan.org/dist/ClamAV-Client/> |
| 98 |
|
| 99 |
=head1 AUTHOR |
| 100 |
|
| 101 |
Originally written for the Clamd module by John Peacock <[email protected]>; |
| 102 |
adjusted for ClamAV::Client by Devin Carraway <qpsmtpd/@/devin.com>. |
| 103 |
|
| 104 |
=head1 COPYRIGHT AND LICENSE |
| 105 |
|
| 106 |
Copyright (c) 2005 John Peacock, |
| 107 |
Copyright (c) 2007 Devin Carraway |
| 108 |
Copyright (c) 2013 Matt Simerson |
| 109 |
|
| 110 |
Based heavily on the clamav plugin |
| 111 |
|
| 112 |
This plugin is licensed under the same terms as the qpsmtpd package itself. |
| 113 |
Please see the LICENSE file included with qpsmtpd for details. |
| 114 |
|
| 115 |
=cut |
| 116 |
|
| 117 |
use strict; |
| 118 |
use warnings; |
| 119 |
|
| 120 |
#use ClamAV::Client; # eval'ed in $self->register |
| 121 |
use Socket qw(:DEFAULT :crlf); |
| 122 |
|
| 123 |
use Qpsmtpd::Constants; |
| 124 |
|
| 125 |
sub register { |
| 126 |
my $self = shift; |
| 127 |
my $qp = shift; |
| 128 |
|
| 129 |
$self->log(LOGERROR, "Bad parameters for the clamdscan plugin") if @_ % 2; |
| 130 |
$self->{'_args'} = {@_}; |
| 131 |
|
| 132 |
eval 'use ClamAV::Client'; |
| 133 |
if ($@) { |
| 134 |
warn "unable to load ClamAV::Client\n"; |
| 135 |
$self->log(LOGERROR, "unable to load ClamAV::Client"); |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
# Set some sensible defaults |
| 140 |
$self->{'_args'}{'deny_viruses'} ||= 'yes'; |
| 141 |
$self->{'_args'}{'max_size'} ||= 1024; |
| 142 |
$self->{'_args'}{'scan_all'} ||= 1; |
| 143 |
for my $setting ('deny_viruses', 'defer_on_error') { |
| 144 |
next unless $self->{'_args'}{$setting}; |
| 145 |
if (lc $self->{'_args'}{$setting} eq 'no') { |
| 146 |
$self->{'_args'}{$setting} = 0; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
$self->register_hook('data_post', 'data_post_handler'); |
| 151 |
} |
| 152 |
|
| 153 |
sub data_post_handler { |
| 154 |
my ($self, $transaction) = @_; |
| 155 |
|
| 156 |
|
| 157 |
if ($self->connection->notes('naughty')) { |
| 158 |
$self->log(LOGINFO, "skip, naughty"); |
| 159 |
return (DECLINED); |
| 160 |
} |
| 161 |
return (DECLINED) if $self->is_too_big($transaction); |
| 162 |
return (DECLINED) if $self->is_not_multipart($transaction); |
| 163 |
|
| 164 |
my $clamd = $self->get_clamd() |
| 165 |
or return $self->err_and_return("Cannot instantiate ClamAV::Client"); |
| 166 |
|
| 167 |
unless (eval { $clamd->ping() }) { |
| 168 |
return $self->err_and_return("Cannot ping clamd server: $@"); |
| 169 |
} |
| 170 |
|
| 171 |
my ($version) = split(/\//, $clamd->version); |
| 172 |
$version ||= 'ClamAV'; |
| 173 |
|
| 174 |
my ($path, $found); |
| 175 |
if ( $self->{_args}{clamd_port} ) { |
| 176 |
my $message = $self->assemble_message($transaction); |
| 177 |
$found = eval { $clamd->scan_scalar(\$message) }; # pass scalar ref |
| 178 |
# $found = eval { $clamd->scan_stream() }; # pass IO handle |
| 179 |
} |
| 180 |
else { |
| 181 |
my $filename = $self->get_filename($transaction) or return DECLINED; |
| 182 |
$self->set_permission($filename) or return DECLINED; |
| 183 |
($path, $found) = eval { $clamd->scan_path($filename) }; |
| 184 |
}; |
| 185 |
|
| 186 |
if ($@) { |
| 187 |
return $self->err_and_return("Error scanning mail: $@"); |
| 188 |
} |
| 189 |
|
| 190 |
if ($found) { |
| 191 |
$self->log(LOGNOTICE, "fail, found virus $found"); |
| 192 |
|
| 193 |
$self->is_naughty(1); # see plugins/naughty |
| 194 |
$self->adjust_karma(-1); |
| 195 |
|
| 196 |
if ($self->{_args}{deny_viruses}) { |
| 197 |
return (DENY, "Virus found: $found"); |
| 198 |
} |
| 199 |
|
| 200 |
$transaction->header->add('X-Virus-Found', 'Yes', 0); |
| 201 |
$transaction->header->add('X-Virus-Details', $found, 0); |
| 202 |
return (DECLINED); |
| 203 |
} |
| 204 |
|
| 205 |
$self->log(LOGINFO, "pass, clean"); |
| 206 |
$transaction->header->add('X-Virus-Found', 'No', 0); |
| 207 |
$transaction->header->add('X-Virus-Checked', |
| 208 |
"by $version on " . $self->qp->config('me'), 0); |
| 209 |
return (DECLINED); |
| 210 |
} |
| 211 |
|
| 212 |
sub assemble_message { |
| 213 |
my ($self, $transaction) = @_; |
| 214 |
$transaction->body_resetpos; |
| 215 |
my $message = $transaction->header->as_string . "\n\n"; |
| 216 |
while (my $line = $transaction->body_getline) { $message .= $line; } |
| 217 |
$message = join(CRLF, split /\n/, $message); |
| 218 |
return $message . CRLF; |
| 219 |
} |
| 220 |
|
| 221 |
sub err_and_return { |
| 222 |
my $self = shift; |
| 223 |
my $message = shift; |
| 224 |
if ($message) { |
| 225 |
$self->log(LOGERROR, $message); |
| 226 |
} |
| 227 |
return (DENYSOFT, "Unable to scan for viruses") |
| 228 |
if $self->{_args}{defer_on_error}; |
| 229 |
return (DECLINED, "skip"); |
| 230 |
} |
| 231 |
|
| 232 |
sub get_filename { |
| 233 |
my $self = shift; |
| 234 |
my $transaction = shift || $self->qp->transaction; |
| 235 |
|
| 236 |
my $filename = $transaction->body_filename; |
| 237 |
|
| 238 |
if (!$filename) { |
| 239 |
$self->log(LOGWARN, "Cannot process due to lack of filename"); |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
if (!-f $filename) { |
| 244 |
$self->log(LOGERROR, "spool file missing! Attempting to respool"); |
| 245 |
$transaction->body_spool; |
| 246 |
$filename = $transaction->body_filename; |
| 247 |
if (!-f $filename) { |
| 248 |
$self->log(LOGERROR, "skip: failed spool to $filename! Giving up"); |
| 249 |
return; |
| 250 |
} |
| 251 |
my $size = (stat($filename))[7]; |
| 252 |
$self->log(LOGDEBUG, "Spooled $size bytes to $filename"); |
| 253 |
} |
| 254 |
|
| 255 |
return $filename; |
| 256 |
} |
| 257 |
|
| 258 |
sub set_permission { |
| 259 |
my ($self, $filename) = @_; |
| 260 |
|
| 261 |
# the spool directory must be readable and executable by the scanner; |
| 262 |
# this generally means either group or world exec; if |
| 263 |
# neither of these is set, issue a warning but try to proceed anyway |
| 264 |
my $dir_mode = (stat($self->spool_dir()))[2]; |
| 265 |
$self->log(LOGDEBUG, "spool dir mode: $dir_mode"); |
| 266 |
|
| 267 |
if ($dir_mode & 0010 || $dir_mode & 0001) { |
| 268 |
|
| 269 |
# match the spool file mode with the mode of the directory -- add |
| 270 |
# the read bit for group, world, or both, depending on what the |
| 271 |
# spool dir had, and strip all other bits, especially the sticky bit |
| 272 |
my $fmode = |
| 273 |
($dir_mode & 0044) | ($dir_mode & 0010 ? 0040 : 0) | |
| 274 |
($dir_mode & 0001 ? 0004 : 0); |
| 275 |
|
| 276 |
unless (chmod $fmode, $filename) { |
| 277 |
$self->log(LOGERROR, "chmod: $filename: $!"); |
| 278 |
return; |
| 279 |
} |
| 280 |
return 1; |
| 281 |
} |
| 282 |
$self->log(LOGWARN, |
| 283 |
"spool directory permissions do not permit scanner access"); |
| 284 |
return 1; |
| 285 |
} |
| 286 |
|
| 287 |
sub get_clamd { |
| 288 |
my $self = shift; |
| 289 |
|
| 290 |
my $port = $self->{'_args'}{'clamd_port'}; |
| 291 |
my $host = $self->{'_args'}{'clamd_host'} || 'localhost'; |
| 292 |
|
| 293 |
if ($port && $port =~ /^(\d+)/) { |
| 294 |
return new ClamAV::Client(socket_host => $host, socket_port => $1); |
| 295 |
} |
| 296 |
|
| 297 |
my $socket = $self->{'_args'}{'clamd_socket'}; |
| 298 |
if ($socket) { |
| 299 |
if ($socket =~ /([\w\/.]+)/) { |
| 300 |
return new ClamAV::Client(socket_name => $1); |
| 301 |
} |
| 302 |
$self->log(LOGERROR, "invalid characters in socket name"); |
| 303 |
} |
| 304 |
|
| 305 |
return new ClamAV::Client; |
| 306 |
} |
| 307 |
|
| 308 |
sub is_too_big { |
| 309 |
my $self = shift; |
| 310 |
my $transaction = shift || $self->qp->transaction; |
| 311 |
|
| 312 |
my $size = $transaction->data_size; |
| 313 |
if ($size > $self->{_args}{max_size} * 1024) { |
| 314 |
$self->log(LOGINFO, "skip, too big ($size)"); |
| 315 |
return 1; |
| 316 |
} |
| 317 |
|
| 318 |
$self->log(LOGDEBUG, "data_size, $size"); |
| 319 |
return; |
| 320 |
} |
| 321 |
|
| 322 |
sub is_not_multipart { |
| 323 |
my $self = shift; |
| 324 |
my $transaction = shift || $self->qp->transaction; |
| 325 |
|
| 326 |
return if $self->{'_args'}{'scan_all'}; |
| 327 |
|
| 328 |
return 1 if !$transaction->header; |
| 329 |
|
| 330 |
# Ignore non-multipart emails |
| 331 |
my $content_type = $transaction->header->get('Content-Type') or return 1; |
| 332 |
$content_type =~ s/\s/ /g; |
| 333 |
if ($content_type !~ m!\bmultipart/.*\bboundary="?([^"]+)!i) { |
| 334 |
$self->log(LOGNOTICE, "skip, not multipart"); |
| 335 |
return 1; |
| 336 |
} |
| 337 |
|
| 338 |
return; |
| 339 |
} |