VirusScan/Engine/Daemon/ClamAV/Clamd.pm

filter: Statementstmt 40.3% 42 / 104 Branchbran 9.0% 4 / 44 Conditioncond 20.0% 4 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 92.3% 12 / 13 Podpod 100% 2 / 2 totaltotal 32.8% 64 / 195 CC 31 SCAR 55.7
CC
31
Cov
30%
scan
506.0
new
18.0
_get_socket
2.1
CRAP 263.0
« VirusScan/Engine/Daemon/FPROT/V4.pm ↑ Summary VirusScan/Engine/Command/NAI/Uvscan.pm »
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
BranchTrueFalseTotal
unless ($conf->{"socket_name"}) 1 7 8
22 1 croak "Must supply a 'socket_name' config value for $class";
23 }
24  
25 7 ▶ if(exists $conf->{zip_fallback}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf->{"zip_fallback"}) 0 7 7
26 0 ▶ unless (blessed($conf->{zip_fallback}) && $conf->{zip_fallback}->isa('File::VirusScan::Engine::Daemon')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($conf->{"zip_fallback"}) and $conf->{"zip_fallback"}->isa("File::VirusScan::Engine::Daemon")) 0 0 0
Condition0%0/3
builtin::blessed($conf->{"zip_fallback"}) and $conf->{"zip_fallback"}->isa("File::VirusScan::Engine::Daemon")
!ll&&!rl&&r
000
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")
ABresult
0X0
100
111
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
l!l
07
$conf->{'read_timeout'} || 60
l!l
07
$conf->{'write_timeout'} || 30
l!l
07
$conf->{'zip_fallback'} || undef
l!l
07
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'}
Aresult
11
00
Truth table50%1/2
A
A= $conf->{'read_timeout'}
Aresult
11
00
Truth table50%1/2
A
A= $conf->{'write_timeout'}
Aresult
11
00
Truth table50%1/2
A
A= $conf->{'zip_fallback'}
Aresult
11
00
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
BranchTrueFalseTotal
unless (defined $sock) 1 0 1
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
BranchTrueFalseTotal
if ($abs and $abs ne $path) 0 0 0
Condition0%0/3
$abs and $abs ne $path
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
60 0 $path = $abs;
61 }
62  
63 0 my $sock = eval { $self->_get_socket };
64 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
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
BranchTrueFalseTotal
unless ($s->can_write($self->{"ping_timeout"})) 0 0 0
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
BranchTrueFalseTotal
unless ($sock->print("nIDSESSION\nnPING\n")) 0 0 0
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
BranchTrueFalseTotal
unless ($sock->flush) 0 0 0
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
BranchTrueFalseTotal
unless ($s->can_read($self->{"ping_timeout"})) 0 0 0
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
BranchTrueFalseTotal
unless ($sock->sysread($ping_response, 256)) 0 0 0
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
BranchTrueFalseTotal
if (not defined $ping_response or $ping_response ne "1: PONG\n") 0 0 0
Condition0%0/3
not defined $ping_response or $ping_response ne "1: PONG\n"
l!l&&r!l&&!r
000
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"
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless ($s->can_write($self->{"write_timeout"})) 0 0 0
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
BranchTrueFalseTotal
unless ($sock->print("nSCAN $path\n")) 0 0 0
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
BranchTrueFalseTotal
unless ($sock->flush) 0 0 0
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
BranchTrueFalseTotal
unless ($s->can_read($self->{"read_timeout"})) 0 0 0
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
BranchTrueFalseTotal
unless ($sock->sysread($scan_response, 256)) 0 0 0
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
BranchTrueFalseTotal
unless ($rc) 0 0 0
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
BranchTrueFalseTotal
unless ($id) 0 0 0
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
BranchTrueFalseTotal
if ($status =~ /(.+) FOUND/) { } 0 0 0
elsif ($scan_response =~ /(.+) ERROR/) { } 0 0 0
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
BranchTrueFalseTotal
if ($self->{"zip_fallback"} and $err_detail =~ /(?:zip module failure|not supported data format)/i) 0 0 0
Condition0%0/3
$self->{"zip_fallback"} and $err_detail =~ /(?:zip module failure|not supported data format)/i
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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__
« VirusScan/Engine/Daemon/FPROT/V4.pm ↑ Summary VirusScan/Engine/Command/NAI/Uvscan.pm »