ClamAV-Client
view release on metacpan or search on metacpan
lib/ClamAV/Client.pm view on Meta::CPAN
#
# ClamAV::Client class,
# a client class for the ClamAV clamd virus scanner daemon.
#
# (C) 2004-2005 Julian Mehnle <julian@mehnle.net>
# $Id: Client.pm,v 1.6 2005/01/21 22:50:14 julian Exp $
#
##############################################################################
=head1 NAME
ClamAV::Client - A client class for the ClamAV C<clamd> virus scanner daemon
=cut
package ClamAV::Client;
=head1 VERSION
0.11
=cut
our $VERSION = '0.11';
=head1 SYNOPSIS
=head2 Creating a scanner client
use ClamAV::Client;
# Try using socket options from clamd.conf, or use default socket:
my $scanner = ClamAV::Client->new();
# Use a local Unix domain socket:
my $scanner = ClamAV::Client->new(
socket_name => '/var/run/clamav/clamd.ctl'
);
# Use a TCP socket:
my $scanner = ClamAV::Client->new(
socket_host => '127.0.0.1',
socket_port => 3310
);
die("ClamAV daemon not alive")
if not defined($scanner) or not $scanner->ping();
=head2 Daemon maintenance
my $version = $scanner->version;
# Retrieve the ClamAV version string.
$scanner->reload(); # Reload the malware pattern database.
$scanner->quit(); # Terminates the ClamAV daemon.
$scanner->shutdown(); # Likewise.
=head2 Path scanning (lazy)
# Scan a single file or a whole directory structure,
# and stop at the first infected file:
my ($path, $result) = $scanner->scan_path($path);
my ($path, $result) = $scanner->scan_path(
$path, ClamAV::Client::SCAN_MODE_NORMAL );
my ($path, $result) = $scanner->scan_path(
$path, ClamAV::Client::SCAN_MODE_RAW );
=head2 Path scanning (complete)
# Scan a single file or a whole directory structure,
# and scan all files without stopping at the first infected one:
my %results = $scanner->scan_path_complete($path);
while (my ($path, $result) = each %results) { ... }
=head2 Other scanning methods
# Scan a stream, i.e. read from an I/O handle:
my $result = $scanner->scan_stream($handle);
# Scan a scalar value:
my $result = $scanner->scan_scalar(\$value);
=cut
use warnings;
use strict;
use Error qw(:try);
use Carp;
use IO::Socket;
use ClamAV::Config;
use constant TRUE => (0 == 0);
use constant FALSE => not TRUE;
use constant SOCKET_TYPE_AUTO => 0;
use constant SOCKET_TYPE_UNIX => 1;
use constant SOCKET_TYPE_TCP => 2;
use constant DEFAULT_SOCKET_NAME => '/var/run/clamav/clamd.ctl';
use constant DEFAULT_SOCKET_HOST => '127.0.0.1';
use constant DEFAULT_SOCKET_PORT => 3310;
lib/ClamAV/Client.pm view on Meta::CPAN
# Caller hasn't specified anything.
# Try reading local clamd config file:
try {
ClamAV::Config->clamd_config;
}
catch ClamAV::Config::Error with {
# Ignore access problems to clamd configuration file.
};
# Try local Unix domain socket first...:
$options{socket_name} = ClamAV::Config->clamd_option('LocalSocket')
or
# ...otherwise try TCP socket:
$options{socket_host} = ClamAV::Config->clamd_option('TCPAddr'),
$options{socket_port} = ClamAV::Config->clamd_option('TCPSocket');
if ($options{socket_name}) {
# Local clamd config file has specified local Unix domain socket.
$options{socket_type} = SOCKET_TYPE_UNIX;
$options{socket_host} ||= DEFAULT_SOCKET_HOST;
}
elsif ($options{socket_host} or $options{socket_port}) {
# Local clamd config file has speficied TCP socket.
$options{socket_type} = SOCKET_TYPE_TCP;
$options{socket_host} ||= DEFAULT_SOCKET_HOST;
$options{socket_port} ||= DEFAULT_SOCKET_PORT;
}
else {
# Neither caller nor clamd config file have specified anything, set
# socket auto detection mode.
$options{socket_type} = SOCKET_TYPE_AUTO;
$options{socket_host} = DEFAULT_SOCKET_HOST;
$options{socket_name} = DEFAULT_SOCKET_NAME;
$options{socket_port} = DEFAULT_SOCKET_PORT;
}
}
my $self = {
socket_type => $options{socket_type},
socket_name => $options{socket_name},
socket_host => $options{socket_host},
socket_port => $options{socket_port}
};
bless($self, $class);
return $self;
}
=back
=head2 Instance methods
The following instance methods are provided:
=head3 Daemon maintenance
=over
=item B<ping>: RETURNS SCALAR; THROWS ClamAV::Client::Error
Returns B<true> ('PONG') if the ClamAV daemon is alive. Throws a
ClamAV::Client::Error exception otherwise.
=cut
sub ping {
my ($self) = @_;
return $self->_simple_command("PING");
}
=item B<version>: RETURNS SCALAR; THROWS ClamAV::Client::Error
Returns the version string of the ClamAV daemon.
=cut
sub version {
my ($self) = @_;
return $self->_simple_command("VERSION");
}
=item B<reload>: RETURNS SCALAR; THROWS ClamAV::Client::Error
Instructs the ClamAV daemon to reload its malware database. Returns B<true> if
the reloading succeeds, or throws a ClamAV::Client::Error exception otherwise.
=cut
sub reload {
my ($self) = @_;
return $self->_simple_command("RELOAD");
}
=item B<quit>: RETURNS SCALAR; THROWS ClamAV::Client::Error
=item B<shutdown>: RETURNS SCALAR; THROWS ClamAV::Client::Error
Terminates the ClamAV daemon. Returns B<true> if the termination succeeds,
or throws a ClamAV::Client::Error exception otherwise.
=cut
sub quit {
# Caution, this terminates the ClamAV daemon!
my ($self) = @_;
return $self->_simple_command("QUIT");
}
*shutdown = *shutdown = \&quit;
=item B<scan_path($path)>: RETURNS SCALAR, SCALAR; THROWS ClamAV::Client::Error
=item B<scan_path($path, $scan_mode)>: RETURNS SCALAR, SCALAR; THROWS
ClamAV::Client::Error
Scans a single file or a whole directory structure, and stops at the first
infected file found. The specified path must be absolute. A scan mode may be
specified: a mode of B<ClamAV::Client::SCAN_MODE_NORMAL> (which is the default)
causes a normal scan (C<SCAN>) with archive support enabled, a mode of
B<ClamAV::Client::SCAN_MODE_RAW> causes a raw scan with archive support
disabled.
( run in 1.418 second using v1.01-cache-2.11-cpan-39bf76dae61 )