| 1 | # | ||
| 2 | # ClamAV::Client class, | ||
| 3 | # a client class for the ClamAV clamd virus scanner daemon. | ||
| 4 | # | ||
| 5 | # (C) 2004-2005 Julian Mehnle <julian@mehnle.net> | ||
| 6 | # $Id: Client.pm,v 1.6 2005/01/21 22:50:14 julian Exp $ | ||
| 7 | # | ||
| 8 | ############################################################################## | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | ClamAV::Client - A client class for the ClamAV C<clamd> virus scanner daemon | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | package ClamAV::Client; | ||
| 17 | |||
| 18 | =head1 VERSION | ||
| 19 | |||
| 20 | 0.11 | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | our $VERSION = '0.11'; | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | =head2 Creating a scanner client | ||
| 29 | |||
| 30 | use ClamAV::Client; | ||
| 31 | |||
| 32 | # Try using socket options from clamd.conf, or use default socket: | ||
| 33 | my $scanner = ClamAV::Client->new(); | ||
| 34 | |||
| 35 | # Use a local Unix domain socket: | ||
| 36 | my $scanner = ClamAV::Client->new( | ||
| 37 | socket_name => '/var/run/clamav/clamd.ctl' | ||
| 38 | ); | ||
| 39 | |||
| 40 | # Use a TCP socket: | ||
| 41 | my $scanner = ClamAV::Client->new( | ||
| 42 | socket_host => '127.0.0.1', | ||
| 43 | socket_port => 3310 | ||
| 44 | ); | ||
| 45 | |||
| 46 | die("ClamAV daemon not alive") | ||
| 47 | if not defined($scanner) or not $scanner->ping(); | ||
| 48 | |||
| 49 | =head2 Daemon maintenance | ||
| 50 | |||
| 51 | my $version = $scanner->version; | ||
| 52 | # Retrieve the ClamAV version string. | ||
| 53 | |||
| 54 | $scanner->reload(); # Reload the malware pattern database. | ||
| 55 | |||
| 56 | $scanner->quit(); # Terminates the ClamAV daemon. | ||
| 57 | $scanner->shutdown(); # Likewise. | ||
| 58 | |||
| 59 | =head2 Path scanning (lazy) | ||
| 60 | |||
| 61 | # Scan a single file or a whole directory structure, | ||
| 62 | # and stop at the first infected file: | ||
| 63 | my ($path, $result) = $scanner->scan_path($path); | ||
| 64 | my ($path, $result) = $scanner->scan_path( | ||
| 65 | $path, ClamAV::Client::SCAN_MODE_NORMAL ); | ||
| 66 | my ($path, $result) = $scanner->scan_path( | ||
| 67 | $path, ClamAV::Client::SCAN_MODE_RAW ); | ||
| 68 | |||
| 69 | =head2 Path scanning (complete) | ||
| 70 | |||
| 71 | # Scan a single file or a whole directory structure, | ||
| 72 | # and scan all files without stopping at the first infected one: | ||
| 73 | my %results = $scanner->scan_path_complete($path); | ||
| 74 | while (my ($path, $result) = each %results) { ... } | ||
| 75 | |||
| 76 | =head2 Other scanning methods | ||
| 77 | |||
| 78 | # Scan a stream, i.e. read from an I/O handle: | ||
| 79 | my $result = $scanner->scan_stream($handle); | ||
| 80 | |||
| 81 | # Scan a scalar value: | ||
| 82 | my $result = $scanner->scan_scalar(\$value); | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | use warnings; | ||
| 87 | use strict; | ||
| 88 | |||
| 89 | use Error qw(:try); | ||
| 90 | |||
| 91 | use Carp; | ||
| 92 | use IO::Socket; | ||
| 93 | |||
| 94 | use ClamAV::Config; | ||
| 95 | |||
| 96 | use constant TRUE => (0 == 0); | ||
| 97 | use constant FALSE => not TRUE; | ||
| 98 | |||
| 99 | use constant SOCKET_TYPE_AUTO => 0; | ||
| 100 | use constant SOCKET_TYPE_UNIX => 1; | ||
| 101 | use constant SOCKET_TYPE_TCP => 2; | ||
| 102 | |||
| 103 | use constant DEFAULT_SOCKET_NAME => '/var/run/clamav/clamd.ctl'; | ||
| 104 | use constant DEFAULT_SOCKET_HOST => '127.0.0.1'; | ||
| 105 | use constant DEFAULT_SOCKET_PORT => 3310; | ||
| 106 | |||
| 107 | use constant SCAN_MODE_NORMAL => FALSE; | ||
| 108 | use constant SCAN_MODE_RAW => TRUE; | ||
| 109 | |||
| 110 | use constant STREAM_BLOCK_SIZE => 4096; | ||
| 111 | |||
| 112 | # Interface: | ||
| 113 | ############################################################################## | ||
| 114 | |||
| 115 | =head1 DESCRIPTION | ||
| 116 | |||
| 117 | B<ClamAV::Client> is a class acting as a client for a ClamAV C<clamd> virus | ||
| 118 | scanner daemon. The daemon may run locally or on a remote system as | ||
| 119 | B<ClamAV::Client> can use both Unix domain sockets and TCP/IP sockets. The | ||
| 120 | full functionality of the C<clamd> client/server protocol is supported. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub new; | ||
| 125 | |||
| 126 | sub ping; | ||
| 127 | sub version; | ||
| 128 | sub reload; | ||
| 129 | sub quit; | ||
| 130 | |||
| 131 | sub scan_path; | ||
| 132 | sub scan_path_complete; | ||
| 133 | sub scan_stream; | ||
| 134 | sub scan_scalar; | ||
| 135 | |||
| 136 | # Implementation: | ||
| 137 | ############################################################################## | ||
| 138 | |||
| 139 | =head2 Constructor | ||
| 140 | |||
| 141 | The following constructor is provided: | ||
| 142 | |||
| 143 | =over | ||
| 144 | |||
| 145 | =item B<new(%options)>: RETURNS ClamAV::Client | ||
| 146 | |||
| 147 | Creates a new C<ClamAV::Client> object. If I<no> socket options are specified, | ||
| 148 | first the socket options from the local C<clamd.conf> configuration file are | ||
| 149 | tried, then the Unix domain socket C</var/run/clamav/clamd.ctl> is tried, then | ||
| 150 | finally the TCP/IP socket at C<127.0.0.1> on port C<3310> is tried. If either | ||
| 151 | Unix domain or TCP/IP socket options are explicitly specified, only these are | ||
| 152 | used. | ||
| 153 | |||
| 154 | C<%options> is a list of key/value pairs representing any of the following | ||
| 155 | options: | ||
| 156 | |||
| 157 | =over | ||
| 158 | |||
| 159 | =item B<socket_name> | ||
| 160 | |||
| 161 | A scalar containing the absolute name of the local Unix domain socket. | ||
| 162 | Defaults to B<'/var/run/clamav/clamd.ctl'>. | ||
| 163 | |||
| 164 | =item B<socket_host> | ||
| 165 | |||
| 166 | A scalar containing the name or IP address of the TCP/IP socket. Defaults to | ||
| 167 | B<'127.0.0.1'>. | ||
| 168 | |||
| 169 | =item B<socket_port> | ||
| 170 | |||
| 171 | A scalar containing the port number of the TCP/IP socket. Defaults to B<3310>. | ||
| 172 | |||
| 173 | =back | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | sub new { | ||
| 178 | my ($class, %options) = @_; | ||
| 179 | |||
| 180 | if ($options{socket_name}) { | ||
| 181 | # Caller explicitly specified local Unix domain socket. | ||
| 182 | $options{socket_type} = SOCKET_TYPE_UNIX; | ||
| 183 | $options{socket_host} ||= DEFAULT_SOCKET_HOST; | ||
| 184 | } | ||
| 185 | elsif ($options{socket_host} or $options{socket_port}) { | ||
| 186 | # Caller explicitly specified TCP socket. | ||
| 187 | $options{socket_type} = SOCKET_TYPE_TCP; | ||
| 188 | $options{socket_host} ||= DEFAULT_SOCKET_HOST; | ||
| 189 | $options{socket_port} ||= DEFAULT_SOCKET_PORT; | ||
| 190 | } | ||
| 191 | else { | ||
| 192 | # Caller hasn't specified anything. | ||
| 193 | |||
| 194 | # Try reading local clamd config file: | ||
| 195 | try { | ||
| 196 | ClamAV::Config->clamd_config; | ||
| 197 | } | ||
| 198 | catch ClamAV::Config::Error with { | ||
| 199 | # Ignore access problems to clamd configuration file. | ||
| 200 | }; | ||
| 201 | |||
| 202 | # Try local Unix domain socket first...: | ||
| 203 | $options{socket_name} = ClamAV::Config->clamd_option('LocalSocket') | ||
| 204 | or | ||
| 205 | # ...otherwise try TCP socket: | ||
| 206 | $options{socket_host} = ClamAV::Config->clamd_option('TCPAddr'), | ||
| 207 | $options{socket_port} = ClamAV::Config->clamd_option('TCPSocket'); | ||
| 208 | |||
| 209 | if ($options{socket_name}) { | ||
| 210 | # Local clamd config file has specified local Unix domain socket. | ||
| 211 | $options{socket_type} = SOCKET_TYPE_UNIX; | ||
| 212 | $options{socket_host} ||= DEFAULT_SOCKET_HOST; | ||
| 213 | } | ||
| 214 | elsif ($options{socket_host} or $options{socket_port}) { | ||
| 215 | # Local clamd config file has speficied TCP socket. | ||
| 216 | $options{socket_type} = SOCKET_TYPE_TCP; | ||
| 217 | $options{socket_host} ||= DEFAULT_SOCKET_HOST; | ||
| 218 | $options{socket_port} ||= DEFAULT_SOCKET_PORT; | ||
| 219 | } | ||
| 220 | else { | ||
| 221 | # Neither caller nor clamd config file have specified anything, set | ||
| 222 | # socket auto detection mode. | ||
| 223 | $options{socket_type} = SOCKET_TYPE_AUTO; | ||
| 224 | $options{socket_host} = DEFAULT_SOCKET_HOST; | ||
| 225 | $options{socket_name} = DEFAULT_SOCKET_NAME; | ||
| 226 | $options{socket_port} = DEFAULT_SOCKET_PORT; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | my $self = { | ||
| 231 | socket_type => $options{socket_type}, | ||
| 232 | socket_name => $options{socket_name}, | ||
| 233 | socket_host => $options{socket_host}, | ||
| 234 | socket_port => $options{socket_port} | ||
| 235 | }; | ||
| 236 | bless($self, $class); | ||
| 237 | return $self; | ||
| 238 | } | ||
| 239 | |||
| 240 | =back | ||
| 241 | |||
| 242 | =head2 Instance methods | ||
| 243 | |||
| 244 | The following instance methods are provided: | ||
| 245 | |||
| 246 | =head3 Daemon maintenance | ||
| 247 | |||
| 248 | =over | ||
| 249 | |||
| 250 | =item B<ping>: RETURNS SCALAR; THROWS ClamAV::Client::Error | ||
| 251 | |||
| 252 | Returns B<true> ('PONG') if the ClamAV daemon is alive. Throws a | ||
| 253 | ClamAV::Client::Error exception otherwise. | ||
| 254 | |||
| 255 | =cut | ||
| 256 | |||
| 257 | sub ping { | ||
| 258 | my ($self) = @_; | ||
| 259 | return $self->_simple_command("PING"); | ||
| 260 | } | ||
| 261 | |||
| 262 | =item B<version>: RETURNS SCALAR; THROWS ClamAV::Client::Error | ||
| 263 | |||
| 264 | Returns the version string of the ClamAV daemon. | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | sub version { | ||
| 269 | my ($self) = @_; | ||
| 270 | return $self->_simple_command("VERSION"); | ||
| 271 | } | ||
| 272 | |||
| 273 | =item B<reload>: RETURNS SCALAR; THROWS ClamAV::Client::Error | ||
| 274 | |||
| 275 | Instructs the ClamAV daemon to reload its malware database. Returns B<true> if | ||
| 276 | the reloading succeeds, or throws a ClamAV::Client::Error exception otherwise. | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | sub reload { | ||
| 281 | my ($self) = @_; | ||
| 282 | return $self->_simple_command("RELOAD"); | ||
| 283 | } | ||
| 284 | |||
| 285 | =item B<quit>: RETURNS SCALAR; THROWS ClamAV::Client::Error | ||
| 286 | |||
| 287 | =item B<shutdown>: RETURNS SCALAR; THROWS ClamAV::Client::Error | ||
| 288 | |||
| 289 | Terminates the ClamAV daemon. Returns B<true> if the termination succeeds, | ||
| 290 | or throws a ClamAV::Client::Error exception otherwise. | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub quit { | ||
| 295 | # Caution, this terminates the ClamAV daemon! | ||
| 296 | my ($self) = @_; | ||
| 297 | return $self->_simple_command("QUIT"); | ||
| 298 | } | ||
| 299 | |||
| 300 | *shutdown = *shutdown = \&quit; | ||
| 301 | |||
| 302 | =item B<scan_path($path)>: RETURNS SCALAR, SCALAR; THROWS ClamAV::Client::Error | ||
| 303 | |||
| 304 | =item B<scan_path($path, $scan_mode)>: RETURNS SCALAR, SCALAR; THROWS | ||
| 305 | ClamAV::Client::Error | ||
| 306 | |||
| 307 | Scans a single file or a whole directory structure, and stops at the first | ||
| 308 | infected file found. The specified path must be absolute. A scan mode may be | ||
| 309 | specified: a mode of B<ClamAV::Client::SCAN_MODE_NORMAL> (which is the default) | ||
| 310 | causes a normal scan (C<SCAN>) with archive support enabled, a mode of | ||
| 311 | B<ClamAV::Client::SCAN_MODE_RAW> causes a raw scan with archive support | ||
| 312 | disabled. | ||
| 313 | |||
| 314 | If an infected file is found, returns a list consisting of the path of the file | ||
| 315 | and the name of the malware signature that matched the file. Otherwise, | ||
| 316 | returns the originally specified path and B<undef>. | ||
| 317 | |||
| 318 | =cut | ||
| 319 | |||
| 320 | sub scan_path { | ||
| 321 | my ($self, $path, $scan_mode_raw) = @_; | ||
| 322 | |||
| 323 | my $command = ($scan_mode_raw ? 'RAWSCAN' : 'SCAN'); | ||
| 324 | my $response = $self->_simple_command("$command $path"); | ||
| 325 | return $self->_parse_scan_response($response); | ||
| 326 | } | ||
| 327 | |||
| 328 | =item B<scan_path_complete($path)>: RETURNS HASH; THROWS ClamAV::Client::Error | ||
| 329 | |||
| 330 | Scans a single file or a whole directory structure I<completely>, not stopping | ||
| 331 | at the first infected file found. The specified path must be absolute. Only | ||
| 332 | the normal, non-raw mode is supported for complete scans by ClamAV. | ||
| 333 | |||
| 334 | Returns a hash with a list of infected files found, with the file paths as | ||
| 335 | the keys and the matched malware signature names as the values. | ||
| 336 | |||
| 337 | =cut | ||
| 338 | |||
| 339 | sub scan_path_complete { | ||
| 340 | my ($self, $path, $scan_mode_raw) = @_; | ||
| 341 | |||
| 342 | if ($scan_mode_raw) { | ||
| 343 | throw ClamAV::Client::Error("Raw mode not supported for path complete (CONTSCAN) scanning"); | ||
| 344 | } | ||
| 345 | |||
| 346 | my $socket = $self->_socket; | ||
| 347 | $socket->print("CONTSCAN $path\n"); | ||
| 348 | |||
| 349 | my %results; | ||
| 350 | while (my $response = $socket->getline()) { | ||
| 351 | my ($file_name, $result) = $self->_parse_scan_response($response); | ||
| 352 | $results{$file_name} = $result; | ||
| 353 | } | ||
| 354 | |||
| 355 | $socket->close(); | ||
| 356 | |||
| 357 | %results = () | ||
| 358 | if values(%results) == 1 and not defined((values(%results))[0]); | ||
| 359 | |||
| 360 | return %results; | ||
| 361 | } | ||
| 362 | |||
| 363 | =item B<scan_stream($handle)>: RETURNS SCALAR; THROWS ClamAV::Client::Error | ||
| 364 | |||
| 365 | Scans a stream, that is, reads from an I/O handle. If the stream is found to | ||
| 366 | be infected, returns the name of the matching malware signature, B<undef> | ||
| 367 | otherwise. | ||
| 368 | |||
| 369 | =cut | ||
| 370 | |||
| 371 | sub scan_stream { | ||
| 372 | my ($self, $handle, $scan_mode_raw) = @_; | ||
| 373 | |||
| 374 | if ($scan_mode_raw) { | ||
| 375 | throw ClamAV::Client::Error("Raw mode not supported for stream (STREAM) scanning"); | ||
| 376 | } | ||
| 377 | |||
| 378 | my $socket = $self->_socket; | ||
| 379 | $socket->print("STREAM\n"); | ||
| 380 | my $port_spec = $socket->getline(); | ||
| 381 | if (not $port_spec =~ /^PORT (\d+)$/i) { | ||
| 382 | throw ClamAV::Client::Error("Invalid server response to STREAM command: \"$port_spec\""); | ||
| 383 | } | ||
| 384 | |||
| 385 | my $port = $1; | ||
| 386 | |||
| 387 | require IO::Socket::INET; | ||
| 388 | my $stream_socket = IO::Socket::INET->new( | ||
| 389 | Proto => 'tcp', | ||
| 390 | PeerHost => $self->{socket_host}, | ||
| 391 | PeerPort => $port | ||
| 392 | ); | ||
| 393 | |||
| 394 | # If we didn't manage to gain a connection, throw exception: | ||
| 395 | if (not defined($stream_socket)) { | ||
| 396 | throw ClamAV::Client::Error( | ||
| 397 | "Could not establish TCP socket connection on port $port for STREAM scan" | ||
| 398 | ); | ||
| 399 | } | ||
| 400 | |||
| 401 | $stream_socket->autoflush(TRUE); | ||
| 402 | my $block; | ||
| 403 | $stream_socket->print($block) | ||
| 404 | while $handle->read($block, STREAM_BLOCK_SIZE); | ||
| 405 | $stream_socket->close(); | ||
| 406 | |||
| 407 | my $response = $self->{'socket'}->getline(); | ||
| 408 | |||
| 409 | $socket->close(); | ||
| 410 | |||
| 411 | my (undef, $result) = $self->_parse_scan_response($response); | ||
| 412 | return $result; | ||
| 413 | } | ||
| 414 | |||
| 415 | =item B<scan_scalar(\$value)>: RETURNS SCALAR; THROWS ClamAV::Client::Error | ||
| 416 | |||
| 417 | Scans the value referenced by the given scalarref. If the value is found to be | ||
| 418 | infected, returns the name of the matching malware signature, B<undef> | ||
| 419 | otherwise. | ||
| 420 | |||
| 421 | =cut | ||
| 422 | |||
| 423 | sub scan_scalar { | ||
| 424 | my ($self, $scalar_ref, $scan_mode_raw) = @_; | ||
| 425 | |||
| 426 | open(my $handle, '<', $scalar_ref); | ||
| 427 | return $self->scan_stream($handle, $scan_mode_raw); | ||
| 428 | } | ||
| 429 | |||
| 430 | =back | ||
| 431 | |||
| 432 | =cut | ||
| 433 | |||
| 434 | sub _socket { | ||
| 435 | my ($self) = @_; | ||
| 436 | |||
| 437 | # Try to reuse cached socket connection: | ||
| 438 | my $socket = $self->{'socket'}; | ||
| 439 | |||
| 440 | while (not defined($socket) or not $socket->opened) { | ||
| 441 | # (Re-)establish socket connection. | ||
| 442 | |||
| 443 | # Try to connect through Unix domain socket: | ||
| 444 | if ( | ||
| 445 | $self->{socket_type} == SOCKET_TYPE_UNIX or | ||
| 446 | $self->{socket_type} == SOCKET_TYPE_AUTO | ||
| 447 | ) { | ||
| 448 | require IO::Socket::UNIX; | ||
| 449 | $socket = IO::Socket::UNIX->new( | ||
| 450 | Peer => $self->{socket_name} | ||
| 451 | ); | ||
| 452 | last if defined($socket); | ||
| 453 | } | ||
| 454 | |||
| 455 | # Try to connect through TCP socket: | ||
| 456 | if ( | ||
| 457 | $self->{socket_type} == SOCKET_TYPE_TCP or | ||
| 458 | $self->{socket_type} == SOCKET_TYPE_AUTO | ||
| 459 | ) { | ||
| 460 | require IO::Socket::INET; | ||
| 461 | $socket = IO::Socket::INET->new( | ||
| 462 | Proto => 'tcp', | ||
| 463 | PeerHost => $self->{socket_host}, | ||
| 464 | PeerPort => $self->{socket_port} | ||
| 465 | ); | ||
| 466 | last if defined($socket); | ||
| 467 | } | ||
| 468 | |||
| 469 | # We haven't managed to gain a connection, throw exception: | ||
| 470 | throw ClamAV::Client::Error( | ||
| 471 | "Could not establish socket connection, tried UNIX domain and TCP sockets" | ||
| 472 | ); | ||
| 473 | } | ||
| 474 | |||
| 475 | $socket->autoflush(TRUE); | ||
| 476 | return $self->{'socket'} = $socket; | ||
| 477 | } | ||
| 478 | |||
| 479 | sub _simple_command { | ||
| 480 | my ($self, $command) = @_; | ||
| 481 | |||
| 482 | my $socket = $self->_socket; | ||
| 483 | $socket->print("$command\n"); | ||
| 484 | chomp(my $response = $socket->getline()); | ||
| 485 | $socket->close(); | ||
| 486 | return $response; | ||
| 487 | } | ||
| 488 | |||
| 489 | sub _parse_scan_response { | ||
| 490 | my ($self, $response) = @_; | ||
| 491 | chomp($response); | ||
| 492 | if (not $response =~ /^(.*): (?:OK|(.*) FOUND)$/i) { | ||
| 493 | throw ClamAV::Client::Error("Invalid server response to scan command: \"$response\""); | ||
| 494 | } | ||
| 495 | return ($1, $2); # (<file-name>, <virus-name> | undef) | ||
| 496 | } | ||
| 497 | |||
| 498 | =head1 SEE ALSO | ||
| 499 | |||
| 500 | The L<clamd> and L<clamav> man-pages. | ||
| 501 | |||
| 502 | =head1 AVAILABILITY and SUPPORT | ||
| 503 | |||
| 504 | The latest version of ClamAV::Client is available on CPAN and at | ||
| 505 | L<http://www.mehnle.net/software/clamav-client>. | ||
| 506 | |||
| 507 | Support is usually (but not guaranteed to be) given by the author, Julian | ||
| 508 | Mehnle <julian@mehnle.net>. | ||
| 509 | |||
| 510 | =head1 AUTHOR and LICENSE | ||
| 511 | |||
| 512 | ClamAV::Client is Copyright (C) 2004-2005 Julian Mehnle <julian@mehnle.net>. | ||
| 513 | |||
| 514 | ClamAV::Client is free software. You may use, modify, and distribute it under | ||
| 515 | the same terms as Perl itself, i.e. under the GNU GPL or the Artistic License. | ||
| 516 | |||
| 517 | =cut | ||
| 518 | |||
| 519 | package ClamAV::Client::Error; | ||
| 520 | use base qw(Error::Simple); | ||
| 521 | |||
| 522 | package ClamAV::Client; | ||
| 523 | |||
| 524 | TRUE; | ||
| 525 | |||
| 526 | # vim:tw=79 |