Clamd
view release on metacpan or search on metacpan
lib/Clamd.pm view on Meta::CPAN
local unix domain socket at F</tmp/clamd>. Options are passed
in as key/value pairs.
B<Available Options:>
=over 4
=item * port
A port or socket to connect to if you do not wish to use the
unix domain socket at F</tmp/clamd>. If the socket has been
setup as a TCP/IP socket (see the C<TCPSocket> option in the
F<clamav.conf> file), then specifying in a number will cause Clamd
to use a TCP socket.
Examples:
my $clamd = Clamd->new(); # Default - uses /tmp/clamd socket
# Use the unix domain socket at /var/sock/clam
my $clamd = Clamd->new(port => '/var/sock/clam');
# Use tcp/ip at port 3310
my $clamd = Clamd->new(port => 3310);
Note: there is no way to connect to a clamd on another machine.
The reason for this is that clamd can only scan local files,
so there would not be much point in doing this (unless you
had NFS shares). Plus if you are using TCP/IP clamd appears
to bind to all adaptors, so it is probably insecure.
=item * find_all
By default clamd will stop at the first virus it detects. This
is useful for performance, but sometimes you want to find all
possible viruses in all of the files. To do that, specify a
true value for find_all.
Examples:
# Stop at first virus
my $clamd = Clamd->new();
my ($file, $virus) = $clamd->scan('/home/bob');
# Return all viruses
my $clamd = Clamd->new(find_all => 1);
my %caught = $clamd->scan('/home/bob');
=cut
sub new {
my $class = shift;
my (%options) = @_;
$options{port} ||= '/tmp/clamd';
$options{find_all} ||= 0;
return bless \%options, $class;
}
=head2 ping()
Pings the clamd to check it is alive. Returns true if it is
alive, false if it is dead. Note that it is still possible for
a race condition to occur between your test for ping() and
any call to scan(). See below for more details.
=cut
sub ping {
my $self = shift;
my $response;
eval {
my $conn = $self->_get_connection();
print $conn "PING\n";
$response = $conn->getline;
1 while (<$conn>);
$conn->close;
};
$response = '' unless defined $response;
chomp($response);
return $response eq 'PONG';
}
=head2 scan($dir_or_file)
Scan a directory or a file. Note that the resource must be
readable by the user clamd is running as.
Returns a hash of C<< filename => virusname >> mappings.
If we cannot connect to the clamd backend for any reason, an
exception will be thrown.
If clamd encounters an error (for example it cannot read a
file) then it will throw an exception. If you wish to
continue in the presence of errors, you will need to pass
an option to scan() as follows:
$clamd->scan($dir, { RaiseError => 0 });
=cut
sub scan {
my $self = shift;
if ($self->{find_all}) {
return $self->_scan('SCAN', @_);
}
return $self->_scan_shallow('SCAN', @_);
}
=head2 rawscan($dir_or_file)
Same as scan(), but does not scan inside of archives.
=cut
sub rawscan {
my $self = shift;
if ($self->{find_all}) {
return $self->_scan('RAWSCAN', @_);
}
return $self->_scan_shallow('RAWSCAN', @_);
}
( run in 1.478 second using v1.01-cache-2.11-cpan-39bf76dae61 )