| 1 | # | ||
| 2 | # Courier::Filter::Module::ClamAVd class | ||
| 3 | # | ||
| 4 | # (C) 2004-2008 Julian Mehnle <julian@mehnle.net> | ||
| 5 | # $Id: ClamAVd.pm 210 2008-03-21 19:30:31Z julian $ | ||
| 6 | # | ||
| 7 | ############################################################################### | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Courier::Filter::Module::ClamAVd - ClamAV clamd filter module for the | ||
| 12 | Courier::Filter framework | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | package Courier::Filter::Module::ClamAVd; | ||
| 17 | |||
| 18 | use warnings; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | use base 'Courier::Filter::Module'; | ||
| 22 | |||
| 23 | use MIME::Parser 5.4; | ||
| 24 | use ClamAV::Client; | ||
| 25 | use File::Spec; | ||
| 26 | # In-memory processing doesn't work, see comments in match_mime_part(). | ||
| 27 | |||
| 28 | use constant TRUE => (0 == 0); | ||
| 29 | use constant FALSE => not TRUE; | ||
| 30 | |||
| 31 | use constant default_response => 'Malware detected:'; | ||
| 32 | |||
| 33 | =head1 SYNOPSIS | ||
| 34 | |||
| 35 | use Courier::Filter::Module::ClamAVd; | ||
| 36 | |||
| 37 | my $module = Courier::Filter::Module::ClamAVd->new( | ||
| 38 | # See the socket options description for details. | ||
| 39 | socket_name => '/var/run/clamav/clamd.ctl', | ||
| 40 | socket_host => 'clamav.example.com', | ||
| 41 | socket_port => '3310', | ||
| 42 | |||
| 43 | max_message_size | ||
| 44 | => $max_message_size, | ||
| 45 | max_part_size => $max_part_size, | ||
| 46 | response => $response_text, | ||
| 47 | |||
| 48 | logger => $logger, | ||
| 49 | inverse => 0, | ||
| 50 | trusting => 0, | ||
| 51 | testing => 0, | ||
| 52 | debugging => 0 | ||
| 53 | ); | ||
| 54 | |||
| 55 | my $filter = Courier::Filter->new( | ||
| 56 | ... | ||
| 57 | modules => [ $module ], | ||
| 58 | ... | ||
| 59 | ); | ||
| 60 | |||
| 61 | =head1 DESCRIPTION | ||
| 62 | |||
| 63 | This class is a filter module class for use with Courier::Filter. It matches a | ||
| 64 | message if the configured ClamAV C<clamd> daemon detects malware in it. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | # Implementation: | ||
| 69 | ############################################################################### | ||
| 70 | |||
| 71 | =head2 Constructor | ||
| 72 | |||
| 73 | The following constructor is provided: | ||
| 74 | |||
| 75 | =over | ||
| 76 | |||
| 77 | =item B<new(%options)>: returns I<Courier::Filter::Module::ClamAVd> | ||
| 78 | |||
| 79 | Creates a new B<ClamAVd> filter module. | ||
| 80 | |||
| 81 | %options is a list of key/value pairs representing any of the following | ||
| 82 | options: | ||
| 83 | |||
| 84 | =over | ||
| 85 | |||
| 86 | =item B<socket_name> | ||
| 87 | |||
| 88 | =item B<socket_host> | ||
| 89 | |||
| 90 | =item B<socket_port> | ||
| 91 | |||
| 92 | These options describe the Unix domain or TCP/IP socket that should be used to | ||
| 93 | connect to the ClamAV daemon. If I<no> socket options are specified, first the | ||
| 94 | socket options from the local C<clamd.conf> configuration file are tried, then | ||
| 95 | the Unix domain socket B</var/run/clamav/clamd.ctl> is tried, then finally the | ||
| 96 | TCP/IP socket at B<127.0.0.1> on port B<3310> is tried. If either Unix domain | ||
| 97 | or TCP/IP socket options are explicitly specified, only these are used. | ||
| 98 | |||
| 99 | =item B<max_message_size> | ||
| 100 | |||
| 101 | An integer value controlling the maximum size (in bytes) of the overall message | ||
| 102 | text for a message to be processed by this filter module. Messages larger than | ||
| 103 | this value will never be processed, and thus will never match. If B<undef>, | ||
| 104 | there is no size limit. Defaults to B<1024**2> (1MB). | ||
| 105 | |||
| 106 | As MIME multipart processing can be quite CPU- and memory-intensive, you should | ||
| 107 | definitely restrict the message size to some sensible value that easily fits in | ||
| 108 | your server's memory. 1024**2 (1MB) should be appropriate for most uses of | ||
| 109 | this filter module. | ||
| 110 | |||
| 111 | =item B<max_part_size> | ||
| 112 | |||
| 113 | An integer value controlling the maximum size (in bytes) of any single MIME | ||
| 114 | part for that part to be processed by this filter module. Parts larger than | ||
| 115 | this value will never be processed, and thus will never match. If B<undef>, | ||
| 116 | there is no size limit. | ||
| 117 | |||
| 118 | Defaults to the value of the C<max_message_size> option, so you don't really | ||
| 119 | need to specify a part size limit if you are comfortable with using the same | ||
| 120 | value for both. See the C<max_message_size> option for its default. | ||
| 121 | |||
| 122 | =item B<response> | ||
| 123 | |||
| 124 | A string that is to be returned as the match result in case of a match. The | ||
| 125 | name of the detected malware is appended to the response text. Defaults to | ||
| 126 | B<"Malware detected:">. | ||
| 127 | |||
| 128 | =back | ||
| 129 | |||
| 130 | All options of the B<Courier::Filter::Module> constructor are also supported | ||
| 131 | by the constructor of the B<ClamAVd> filter module. Please see | ||
| 132 | L<Courier::Filter::Module/"new"> for their descriptions. | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub new { | ||
| 137 | my ($class, %options) = @_; | ||
| 138 | |||
| 139 | my $mime_parser = MIME::Parser->new(); | ||
| 140 | #$mime_parser->output_to_core(TRUE); | ||
| 141 | # In-memory processing doesn't work, see comments in match_mime_part(). | ||
| 142 | $mime_parser->output_under(File::Spec->tmpdir); | ||
| 143 | #$mime_parser->tmp_to_core(TRUE); | ||
| 144 | # In-memory processing doesn't work, see comments in match_mime_part(). | ||
| 145 | $mime_parser->use_inner_files(TRUE); | ||
| 146 | |||
| 147 | my $clamav_client = ClamAV::Client->new( | ||
| 148 | socket_name => $options{socket_name}, | ||
| 149 | socket_host => $options{socket_host}, | ||
| 150 | socket_port => $options{socket_port} | ||
| 151 | ); | ||
| 152 | |||
| 153 | my $self = $class->SUPER::new( | ||
| 154 | %options, | ||
| 155 | mime_parser => $mime_parser, | ||
| 156 | clamav_client => $clamav_client | ||
| 157 | ); | ||
| 158 | |||
| 159 | # Default "max_message_size" option to 1024**2 (1MB): | ||
| 160 | $self->{max_message_size} = 1024**2 | ||
| 161 | if not exists($self->{max_message_size}); | ||
| 162 | |||
| 163 | # Default "max_part_size" option to the "max_message_size" option: | ||
| 164 | $self->{max_part_size} = $self->{max_message_size} | ||
| 165 | if not exists($self->{max_part_size}); | ||
| 166 | |||
| 167 | return $self; | ||
| 168 | } | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | =head2 Instance methods | ||
| 173 | |||
| 174 | See L<Courier::Filter::Module/"Instance methods"> for a description of the | ||
| 175 | provided instance methods. | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub match { | ||
| 180 | my ($self, $message) = @_; | ||
| 181 | |||
| 182 | return undef | ||
| 183 | if defined($self->{max_message_size}) | ||
| 184 | and -s $message->file_name > $self->{max_message_size}; | ||
| 185 | |||
| 186 | #my $text = $message->text; | ||
| 187 | #my $part = $self->{mime_parser}->parse_data($text); | ||
| 188 | # In-memory processing doesn't work, see comments in match_mime_part(). | ||
| 189 | my $part = $self->{mime_parser}->parse_open($message->file_name); | ||
| 190 | my ($result, @code) = $self->match_mime_part($part); | ||
| 191 | |||
| 192 | $result &&= 'ClamAVd: ' . $result; | ||
| 193 | |||
| 194 | $self->{mime_parser}->filer->purge(); | ||
| 195 | # In-memory processing doesn't work, see comments in match_mime_part(). | ||
| 196 | rmdir($self->{mime_parser}->filer->output_dir); | ||
| 197 | #if MIME::Tools->VERSION < 6.0; | ||
| 198 | # Purging also doesn't work properly | ||
| 199 | # (bug filed: <http://rt.cpan.org/NoAuth/Bug.html?id=7858>). | ||
| 200 | |||
| 201 | return ($result, @code); | ||
| 202 | } | ||
| 203 | |||
| 204 | sub match_mime_part { | ||
| 205 | my ($self, $part) = @_; | ||
| 206 | |||
| 207 | if (my $body = $part->bodyhandle) { | ||
| 208 | # No sub-parts, match this part. | ||
| 209 | my $handle = $body->open('r'); | ||
| 210 | my $malware_name = $self->{clamav_client}->scan_stream($handle); | ||
| 211 | return ($self->{response} || $self->default_response) . ' ' . $malware_name | ||
| 212 | if defined($malware_name); | ||
| 213 | } | ||
| 214 | else { | ||
| 215 | # Match all sub-parts: | ||
| 216 | foreach my $subpart ($part->parts) { | ||
| 217 | my ($result, @code) = $self->match_mime_part($subpart); | ||
| 218 | return ($result, @code) if defined($result); | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | return undef; | ||
| 223 | } | ||
| 224 | |||
| 225 | =head1 SEE ALSO | ||
| 226 | |||
| 227 | L<Courier::Filter::Module>, L<Courier::Filter::Overview>. | ||
| 228 | |||
| 229 | For AVAILABILITY, SUPPORT, and LICENSE information, see | ||
| 230 | L<Courier::Filter::Overview>. | ||
| 231 | |||
| 232 | =head1 AUTHOR | ||
| 233 | |||
| 234 | Julian Mehnle <julian@mehnle.net> | ||
| 235 | |||
| 236 | =cut | ||
| 237 | |||
| 238 | TRUE; |