| 1 | package File::VirusScan::Engine::Daemon::ClamAV::Clamd; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use File::VirusScan::Engine::Daemon; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use vars qw( @ISA ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | @ISA = qw( File::VirusScan::Engine::Daemon ); | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use IO::Socket::UNIX; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Cwd 'abs_path'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use File::VirusScan::Result; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | sub new | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 8 | my ($class, $conf) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 8 | ▶ | if(!$conf->{socket_name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | croak "Must supply a 'socket_name' config value for $class"; | |||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | ▶ | if(exists $conf->{zip_fallback}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | unless (blessed($conf->{zip_fallback}) && $conf->{zip_fallback}->isa('File::VirusScan::Engine::Daemon')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($conf->{"zip_fallback"}) and $conf->{"zip_fallback"}->isa("File::VirusScan::Engine::Daemon")
MC/DC0%0/2
builtin::blessed($conf->{"zip_fallback"}) and $conf->{"zip_fallback"}->isa("File::VirusScan::Engine::Daemon")
builtin::blessed($conf->{"zip_fallback"})
$conf->{"zip_fallback"}->isa("File::VirusScan::Engine::Daemon")
Truth table0%0/3
A and B
A= builtin::blessed($conf->{"zip_fallback"})B= $conf->{"zip_fallback"}->isa("File::VirusScan::Engine::Daemon")
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | croak q{The 'zip_fallback' config value must be an object inheriting from File::VirusScan::Engine::Daemon}; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | my $self = { | ||||||||||||||||||||||||||||||||||||||||||
| 32 | socket_name => $conf->{socket_name}, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ping_timeout => $conf->{ping_timeout} || 5, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | read_timeout => $conf->{read_timeout} || 60, | ||||||||||||||||||||||||||||||||||||||||||
| 35 | write_timeout => $conf->{write_timeout} || 30, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | zip_fallback => $conf->{zip_fallback} || undef, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 7 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%4/8
$conf->{'ping_timeout'} || 5
$conf->{'read_timeout'} || 60
$conf->{'write_timeout'} || 30
$conf->{'zip_fallback'} || undef
MC/DC0%0/1
$conf->{'ping_timeout'} || 5
$conf->{'ping_timeout'}
MC/DC0%0/1
$conf->{'read_timeout'} || 60
$conf->{'read_timeout'}
MC/DC0%0/1
$conf->{'write_timeout'} || 30
$conf->{'write_timeout'}
MC/DC0%0/1
$conf->{'zip_fallback'} || undef
$conf->{'zip_fallback'}
Truth table50%1/2
A
A= $conf->{'ping_timeout'}
Truth table50%1/2
A
A= $conf->{'read_timeout'}
Truth table50%1/2
A
A= $conf->{'write_timeout'}
Truth table50%1/2
A
A= $conf->{'zip_fallback'}
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | return bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | sub _get_socket | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_socket: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | my $sock = IO::Socket::UNIX->new(Peer => $self->{socket_name}); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | if(!defined $sock) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | croak "Error: Could not connect to clamd daemon at $self->{socket_name}"; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | return $sock; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | sub scan | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
scan: not called CC=22 SCAR=62.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my $abs = abs_path($path); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ($abs && $abs ne $path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$abs and $abs ne $path
MC/DC0%0/2
$abs and $abs ne $path
$abs
$abs ne $path
Truth table0%0/3
A and B
A= $absB= $abs ne $path
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | $path = $abs; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $sock = eval { $self->_get_socket }; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | return File::VirusScan::Result->error($@); | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my $s = IO::Select->new($sock); | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if(!$s->can_write($self->{ping_timeout})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $sock->close; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | return File::VirusScan::result->error("Timeout waiting to write PING to clamd daemon at $self->{socket_name}"); | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if(!$sock->print("nIDSESSION\nnPING\n")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $sock->close; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | return File::VirusScan::Result->error('Could not ping clamd'); | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if(!$sock->flush) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $sock->close; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | return File::VirusScan::Result->error('Could not flush clamd socket'); | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if(!$s->can_read($self->{ping_timeout})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $sock->close; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | return File::VirusScan::Result->error("Timeout reading from clamd daemon at $self->{socket_name}"); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my $ping_response; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if(!$sock->sysread($ping_response, 256)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $sock->close; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | return File::VirusScan::Result->error('Did not get ping response from clamd'); | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if(!defined $ping_response || $ping_response ne "1: PONG\n") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $ping_response or $ping_response ne "1: PONG\n"
MC/DC0%0/2
not defined $ping_response or $ping_response ne "1: PONG\n"
not defined $ping_response
$ping_response ne "1: PONG\n"
Truth table0%0/3
not(A) or B
A= not defined $ping_responseB= $ping_response ne "1: PONG\n"
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $sock->close; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | return File::VirusScan::Result->error('Did not get ping response from clamd'); | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if(!$s->can_write($self->{write_timeout})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $sock->close; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | return File::VirusScan::result->error("Timeout waiting to write SCAN to clamd daemon at $self->{socket_name}"); | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if(!$sock->print("nSCAN $path\n")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $sock->close; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return File::VirusScan::Result->error("Could not get clamd to scan $path"); | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if(!$sock->flush) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $sock->close; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | return File::VirusScan::Result->error("Could not get clamd to scan $path"); | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if(!$s->can_read($self->{read_timeout})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $sock->close; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | return File::VirusScan::Result->error("Timeout reading from clamd daemon at $self->{socket_name}"); | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # Discard our IO::Select object. | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | undef $s; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my $scan_response; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if(!$sock->sysread($scan_response, 256)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | $sock->close; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | return File::VirusScan::Result->error("Did not get response from clamd while scanning $path"); | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | # End session | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | my $rc = $sock->print("nEND\n"); | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | $sock->close(); | |||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if(!$rc) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | return File::VirusScan::Result->error("Could not get clamd to scan $path"); | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | my ($id, $file, $status) = $scan_response =~ m/(\d+):\s+([^:]+):\s+(.*)/; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if( ! $id ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | return File::VirusScan::Result->error("IDSESSION response didn't contain an ID"); | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # TODO: what if more than one virus found? | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # TODO: can/should we capture infected filenames? | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if($status =~ m/(.+) FOUND/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | return File::VirusScan::Result->virus($1); | |||||||||||||||||||||||||||||||||||||||||
| 146 | } elsif($scan_response =~ m/(.+) ERROR/) { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my $err_detail = $1; | |||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | # The clam daemon may not understand certain zip files, | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # and cannot use an external decompression tool. The | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # standalone 'clamscan' utility can, though. So, we | ||||||||||||||||||||||||||||||||||||||||||
| 152 | # allow another engine to be configured as a fallback. | ||||||||||||||||||||||||||||||||||||||||||
| 153 | # It's usually File::VirusScan::ClamAV::ClamScan, but | ||||||||||||||||||||||||||||||||||||||||||
| 154 | # doesn't have to be. | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if( $self->{zip_fallback} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"zip_fallback"} and $err_detail =~ /(?:zip module failure|not supported data format)/i
MC/DC0%0/2
$self->{"zip_fallback"} and $err_detail =~ /(?:zip module failure|not supported data format)/i
$self->{"zip_fallback"}
$err_detail =~ /(?:zip module failure|not supported data format)/i
Truth table0%0/3
A and B
A= $self->{"zip_fallback"}B= $err_detail =~ /(?:zip module failure|not supported data format)/i
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | && $err_detail =~ /(?:zip module failure|not supported data format)/i) | ||||||||||||||||||||||||||||||||||||||||||
| 157 | { | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | return $self->{zip_fallback}->scan($path); | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | return File::VirusScan::Result->error("Clamd returned error: $err_detail"); | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | return File::VirusScan::Result->clean(); | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 167 | __END__ | ||||||||||||||||||||||||||||||||||||||||||