VirusScan/Engine/Daemon/FPROT/V6.pm

filter: Statementstmt 38.6% 34 / 88 Branchbran 11.1% 4 / 36 Conditioncond 30.0% 6 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 75.0% 9 / 12 Podpod 100% 2 / 2 totaltotal 32.7% 55 / 168 CC 29 SCAR 44.3
CC
29
Cov
31%
_scan
210.0
scan
42.0
new
14.6
CRAP 83.9
« VirusScan/Engine/Command/FPROT/FPROT.pm ↑ Summary VirusScan/Engine/Daemon/Sophos/Sophie.pm »
1 package File::VirusScan::Engine::Daemon::FPROT::V6;
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::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::VirusScan::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub new
15 6 ▶{
Subroutine new: called CC=10 SCAR=26.8
Pod documented
16 6 my ($class, $conf) = @_;
17  
18 6 ▶ if(!$conf->{host}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->{"host"}) 1 5 6
19 1 croak "Must supply a 'host' config value for $class";
20 }
21  
22 5 my $port;
23 5 ▶ if($conf->{host}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"host"}) 5 0 5
24 5 ▶ if($conf->{host} =~ s/:(\d+)\Z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"host"} =~ s/:(\d+)\Z//) 0 5 5
25 0 $port = $1;
26 }
27 }
28 my $self = {
29 host => $conf->{host} || 127.0.0.1,
30 port => $conf->{port} || $port || 10200,
31 connect_timeout => $conf->{connect_timeout} || 10,
32 read_timeout => $conf->{read_timeout} || 60,
33 options => $conf->{options} || [
34  
35 # Instructs the Daemon Scanner which scanlevel to use:
36 # 0 => Disable regular scanning (only heuristics).
37 # 1 => Skip suspicious data files. Not recommended if filename is unavailable.
38 # 2 => (Default) Unknown and/or wrong extensions will be emulated.
39 # 3 => Unknown binaries emulated.
40 # 4 => For scanning virus collections, no limits for emulation
41 5 ▶ '--scanlevel=2',
Condition46%6/13
$conf->{'host'} || 127.0.0.1
l!l
50
$conf->{'port'} || $port || 10200
l!l
05
$conf->{"port"} or $port
l!l&&r!l&&!r
005
$conf->{'connect_timeout'} || 10
l!l
05
$conf->{'read_timeout'} || 60
l!l
05
$conf->{'options'} || ['--scanlevel=2', '--archive=2', '--heurlevel=2', '--adware', '--applications']
l!l
05
MC/DC0%0/1
$conf->{'host'} || 127.0.0.1
$conf->{'host'}
MC/DC0%0/2
$conf->{'port'} || $port || 10200
$conf->{"port"} $port
MC/DC0%0/1
$conf->{'connect_timeout'} || 10
$conf->{'connect_timeout'}
MC/DC0%0/1
$conf->{'read_timeout'} || 60
$conf->{'read_timeout'}
MC/DC0%0/1
$conf->{'options'} || ['--scanlevel=2', '--archive=2', '--heurlevel=2', '--adware', '--applications']
$conf->{'options'}
Truth table50%1/2
A
A= $conf->{'host'}
Aresult
11
00
Truth table33%1/3
A or B
A= $conf->{"port"}B= $port
ABresult
1X1
011
000
Truth table50%1/2
A
A= $conf->{'connect_timeout'}
Aresult
11
00
Truth table50%1/2
A
A= $conf->{'read_timeout'}
Aresult
11
00
Truth table50%1/2
A
A= $conf->{'options'}
Aresult
11
00
42  
43 # archive depth
44 '--archive=2',
45  
46 # How aggressive heuristic should be used, 0..4
47 # the higher the more heuristic tests are done which increases
48 # both detection rates AND risk of false positives.
49 '--heurlevel=2',
50  
51 # to flag adware
52 '--adware',
53  
54 # to flag potentially unwanted applications
55 '--applications',
56 ],
57 };
58  
59 5 return bless $self, $class;
60 }
61  
62 sub scan
63 0 ▶{
Subroutine scan: not called CC=6 SCAR=37.4
Pod documented
64 0 my ($self, $path) = @_;
65  
66 0 my $abs = abs_path($path);
67 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
68 0 $path = $abs;
69 }
70  
71 # The F-Prot demon cannot scan directories, but files only
72 # hence, we recurse any directories manually
73 0 my @files = eval { $self->list_files($path) };
74 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
75 0 return File::VirusScan::Result->error($@);
76 }
77  
78 0 foreach my $file_path (@files) {
79 0 my $result = $self->_scan($file_path);
80  
81 0 ▶ if(!$result->is_clean()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->is_clean) 0 0 0
82 0 return $result;
83 }
84 }
85  
86 }
87  
88 # Scans a single path.
89 sub _scan
90 0 ▶{
Subroutine _scan: not called CC=14 SCAR=53.5
91 0 my ($self, $path) = @_;
92  
93 0 my $sock = eval { $self->_get_socket };
94 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
95 0 return File::VirusScan::Result->error($@);
96 }
97  
98 # Stringify our options
99 0 my $options = join(' ', $self->{options});
100  
101 # SCAN options FILE fnam\n (local daemon)
102 # -or- SCAN options STREAM fnam SIZE length\n (remote daemon)
103 # length bytes of data
104 # assume local daemon ==> implement FILE variant only
105 # , supports spaces in fnam
106 0 ▶ if(!$sock->print("SCAN $options FILE $path\n")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock->print("SCAN $options FILE $path\n")) 0 0 0
107 0 my $err = $!;
108 0 $sock->close;
109 0 return File::VirusScan::Result->error("Could not write to socket: $err");
110 }
111  
112 0 ▶ if(!$sock->flush) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock->flush) 0 0 0
113 0 my $err = $!;
114 0 $sock->close;
115 0 return File::VirusScan::Result->error("Could not flush socket: $err");
116 }
117  
118 0 my $s = IO::Select->new($sock);
119 0 ▶ if(!$s->can_read($self->{read_timeout})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s->can_read($self->{"read_timeout"})) 0 0 0
120 0 $sock->close;
121 0 return File::VirusScan::Result->error("Timeout reading from fprot daemon");
122 }
123  
124 0 my $resp = $sock->getline;
125 0 ▶ if(!$resp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp) 0 0 0
126 0 $sock->close;
127 0 return File::VirusScan::Result->error("Did not get response from fprot while scanning $path");
128 }
129  
130 0 my ($code, $desc, $name);
131 0 ▶ unless (($code, $desc, $name) = $resp =~ /\A(\d+)\s<(.*?)>\s(.*)\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (() = $resp =~ /\A(\d+)\s<(.*?)>\s(.*)\Z/) 0 0 0
132 0 return File::VirusScan::Result->error("Failed to parse response from fprotd: $path");
133 }
134  
135 # Clean up $desc
136 0 $desc =~ s/\A(?:contains infected objects|infected):\s//i;
137  
138 # Our output should contain:
139 # 1) A code. The code is a bitmask of:
140 # bit num Meaning
141 # 0 1 At least one virus-infected object was found (and remains).
142 # 1 2 At least one suspicious (heuristic match) object was found (and remains).
143 # 2 4 Interrupted by user. (SIGINT, SIGBREAK).
144 # 3 8 Scan restriction caused scan to skip files (maxdepth directories, maxdepth archives, exclusion list, etc).
145 # 4 16 Platform error (out of memory, real I/O errors, insufficient file permission etc.).
146 # 5 32 Internal engine error (whatever the engine fails at)
147 # 6 64 At least one object was not scanned (encrypted file, unsupported/unknown compression method, corrupted or invalid file).
148 # 7 128 At least one object was disinfected (clean now) (treat same as virus for File::VirusScan)
149 #
150 # 2) The description, including virus name
151 #
152 # 3) The item name, incl. member of archive etc. We ignore
153 # this for now.
154  
155 0 ▶ if($code & (1 | 128)) {
Branch: 5 branches
BranchTrueFalseTotal
if ($code & 129) { } 0 0 0
elsif ($code & 2) { } 0 0 0
elsif ($code & 4) { } 0 0 0
elsif ($code & 16) { } 0 0 0
elsif ($code & 32) { } 0 0 0
156 0 my $virus_name = $desc;
157 0 ▶ $virus_name ||= 'unknown-FPROTD-virus';
Condition0%0/2
$virus_name ||= "unknown-FPROTD-virus"
l!l
00
MC/DC0%0/1
$virus_name ||= "unknown-FPROTD-virus"
$virus_name
Truth table0%0/2
A
A= $virus_name
Aresult
11
00
158 0 return File::VirusScan::Result->virus($virus_name);
159 } elsif($code & 2) {
160 0 my $virus_name = $desc;
161 0 ▶ $virus_name ||= 'unknown-FPROTD-virus';
Condition0%0/2
$virus_name ||= "unknown-FPROTD-virus"
l!l
00
MC/DC0%0/1
$virus_name ||= "unknown-FPROTD-virus"
$virus_name
Truth table0%0/2
A
A= $virus_name
Aresult
11
00
162 0 return File::VirusScan::Result->virus($virus_name);
163 } elsif($code & 4) {
164 0 return File::VirusScan::Result->error('FPROTD scanning interrupted by user');
165 } elsif($code & 16) {
166 0 return File::VirusScan::Result->error('FPROTD platform error');
167 } elsif($code & 32) {
168 0 return File::VirusScan::Result->error('FPROTD internal engine error');
169 }
170  
171 0 return File::VirusScan::Result->clean();
172 }
173  
174 # Returns preconfigured socket, or opens a new connection.
175 sub _get_socket
176 0 ▶{
Subroutine _get_socket: not called CC=2 SCAR=17.9
177 0 my ($self) = @_;
178  
179 0 ▶ if(!$self->{sock}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sock"}) 0 0 0
180 $self->{sock} = IO::Socket::INET->new(
181 PeerAddr => $self->{host},
182 PeerPort => $self->{port},
183 Timeout => $self->{connect_timeout},
184 0 );
185 }
186  
187 0 return $self->{sock};
188 }
189  
190 1 1;
191 __END__
« VirusScan/Engine/Command/FPROT/FPROT.pm ↑ Summary VirusScan/Engine/Daemon/Sophos/Sophie.pm »