This file is indexed.

/usr/share/qpsmtpd/plugins/virus/clamdscan is in qpsmtpd 0.94-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!perl -w

=head1 NAME

clamdscan

=head1 DESCRIPTION

A qpsmtpd plugin for virus scanning using the ClamAV scan daemon, clamd.

=head1 RESTRICTIONS

If connecting to clamd via TCP/IP host:port, then ignore this restriction.

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:

=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.

=item * Add group-execute permissions to the qpsmtpd spool directory.

=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
necessary for the group to have any read rights.

=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):

You must have the ClamAV::Client module installed to use the plugin.

=over 4

=item B<clamd_socket>

Full path to the clamd socket, if different from the ClamAV::Client defaults.

=item B<clamd_host>

IP address where clamd is listening.

Default: localhost

=item B<clamd_port>

The TCP port where the clamd service is running, typically 3310.

Default: disabled. When present, overrides clamd_socket.

=item B<deny_viruses>

Whether the scanner will automatically delete messages which have viruses.
Takes either 'yes' or 'no'. If set to 'no', adds a header with the virus name.

Default: yes

=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.

=item B<max_size>

The maximum size, in kilobytes, of messages to scan.

Default: 1024 (1 MB)

=item B<scan_all>

Scan all messages, even if there are no attachments

=back

=head1 REQUIREMENTS

This module requires the ClamAV::Client module, found on CPAN here:

L<http://search.cpan.org/dist/ClamAV-Client/>

=head1 AUTHOR

Originally written for the Clamd module by John Peacock <jpeacock@cpan.org>;
adjusted for ClamAV::Client by Devin Carraway <qpsmtpd/@/devin.com>.

=head1 COPYRIGHT AND LICENSE

 Copyright (c) 2005 John Peacock,
 Copyright (c) 2007 Devin Carraway
 Copyright (c) 2013 Matt Simerson

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

use strict;
use warnings;

#use ClamAV::Client;  # eval'ed in $self->register
use Socket qw(:DEFAULT :crlf);

use Qpsmtpd::Constants;

sub register {
    my $self = shift;
    my $qp = shift;

    $self->log(LOGERROR, "Bad parameters for the clamdscan plugin") if @_ % 2;
    $self->{'_args'} = {@_};

    eval 'use ClamAV::Client';
    if ($@) {
        warn "unable to load ClamAV::Client\n";
        $self->log(LOGERROR, "unable to load ClamAV::Client");
        return;
    }

    # Set some sensible defaults
    $self->{'_args'}{'deny_viruses'} ||= 'yes';
    $self->{'_args'}{'max_size'}     ||= 1024;
    $self->{'_args'}{'scan_all'}     ||= 1;
    for my $setting ('deny_viruses', 'defer_on_error') {
        next unless $self->{'_args'}{$setting};
        if (lc $self->{'_args'}{$setting} eq 'no') {
            $self->{'_args'}{$setting} = 0;
        }
    }

    $self->register_hook('data_post', 'data_post_handler');
}

sub data_post_handler {
    my ($self, $transaction) = @_;


    if ($self->connection->notes('naughty')) {
        $self->log(LOGINFO, "skip, naughty");
        return (DECLINED);
    }
    return (DECLINED) if $self->is_too_big($transaction);
    return (DECLINED) if $self->is_not_multipart($transaction);

    my $clamd = $self->get_clamd()
      or return $self->err_and_return("Cannot instantiate ClamAV::Client");

    unless (eval { $clamd->ping() }) {
        return $self->err_and_return("Cannot ping clamd server: $@");
    }

    my ($version) = split(/\//, $clamd->version);
    $version ||= 'ClamAV';

    my ($path, $found);
    if ( $self->{_args}{clamd_port} ) {
        my $message = $self->assemble_message($transaction);
        $found = eval { $clamd->scan_scalar(\$message) };  # pass scalar ref
#       $found = eval { $clamd->scan_stream() };           # pass IO handle
    }
    else {
        my $filename = $self->get_filename($transaction) or return DECLINED;
        $self->set_permission($filename) or return DECLINED;
        ($path, $found) = eval { $clamd->scan_path($filename) };
    };

    if ($@) {
        return $self->err_and_return("Error scanning mail: $@");
    }

    if ($found) {
        $self->log(LOGNOTICE, "fail, found virus $found");

        $self->is_naughty(1);       # see plugins/naughty
        $self->adjust_karma(-1);

        if ($self->{_args}{deny_viruses}) {
            return (DENY, "Virus found: $found");
        }

        $transaction->header->add('X-Virus-Found',   'Yes',  0);
        $transaction->header->add('X-Virus-Details', $found, 0);
        return (DECLINED);
    }

    $self->log(LOGINFO, "pass, clean");
    $transaction->header->add('X-Virus-Found', 'No', 0);
    $transaction->header->add('X-Virus-Checked',
                              "by $version on " . $self->qp->config('me'), 0);
    return (DECLINED);
}

sub assemble_message {
    my ($self, $transaction) = @_;
    $transaction->body_resetpos;
    my $message = $transaction->header->as_string . "\n\n";
    while (my $line = $transaction->body_getline) { $message .= $line; }
    $message = join(CRLF, split /\n/, $message);
    return $message . CRLF;
}

sub err_and_return {
    my $self    = shift;
    my $message = shift;
    if ($message) {
        $self->log(LOGERROR, $message);
    }
    return (DENYSOFT, "Unable to scan for viruses")
      if $self->{_args}{defer_on_error};
    return (DECLINED, "skip");
}

sub get_filename {
    my $self = shift;
    my $transaction = shift || $self->qp->transaction;

    my $filename = $transaction->body_filename;

    if (!$filename) {
        $self->log(LOGWARN, "Cannot process due to lack of filename");
        return;
    }

    if (!-f $filename) {
        $self->log(LOGERROR, "spool file missing! Attempting to respool");
        $transaction->body_spool;
        $filename = $transaction->body_filename;
        if (!-f $filename) {
            $self->log(LOGERROR, "skip: failed spool to $filename! Giving up");
            return;
        }
        my $size = (stat($filename))[7];
        $self->log(LOGDEBUG, "Spooled $size bytes to $filename");
    }

    return $filename;
}

sub set_permission {
    my ($self, $filename) = @_;

    # 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
    my $dir_mode = (stat($self->spool_dir()))[2];
    $self->log(LOGDEBUG, "spool dir mode: $dir_mode");

    if ($dir_mode & 0010 || $dir_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 =
          ($dir_mode & 0044) | ($dir_mode & 0010 ? 0040 : 0) |
          ($dir_mode & 0001 ? 0004 : 0);

        unless (chmod $fmode, $filename) {
            $self->log(LOGERROR, "chmod: $filename: $!");
            return;
        }
        return 1;
    }
    $self->log(LOGWARN,
               "spool directory permissions do not permit scanner access");
    return 1;
}

sub get_clamd {
    my $self = shift;

    my $port = $self->{'_args'}{'clamd_port'};
    my $host = $self->{'_args'}{'clamd_host'} || 'localhost';

    if ($port && $port =~ /^(\d+)/) {
        return new ClamAV::Client(socket_host => $host, socket_port => $1);
    }

    my $socket = $self->{'_args'}{'clamd_socket'};
    if ($socket) {
        if ($socket =~ /([\w\/.]+)/) {
            return new ClamAV::Client(socket_name => $1);
        }
        $self->log(LOGERROR, "invalid characters in socket name");
    }

    return new ClamAV::Client;
}

sub is_too_big {
    my $self = shift;
    my $transaction = shift || $self->qp->transaction;

    my $size = $transaction->data_size;
    if ($size > $self->{_args}{max_size} * 1024) {
        $self->log(LOGINFO, "skip, too big ($size)");
        return 1;
    }

    $self->log(LOGDEBUG, "data_size, $size");
    return;
}

sub is_not_multipart {
    my $self = shift;
    my $transaction = shift || $self->qp->transaction;

    return if $self->{'_args'}{'scan_all'};

    return 1 if !$transaction->header;

    # Ignore non-multipart emails
    my $content_type = $transaction->header->get('Content-Type') or return 1;
    $content_type =~ s/\s/ /g;
    if ($content_type !~ m!\bmultipart/.*\bboundary="?([^"]+)!i) {
        $self->log(LOGNOTICE, "skip, not multipart");
        return 1;
    }

    return;
}