/
ClamAV.pm
375 lines (269 loc) · 7.73 KB
/
ClamAV.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package Foswiki::Plugins::ClamAVScanPlugin::ClamAV;
use strict;
use warnings;
use File::Find qw(find);
use IO::Socket;
#use Socket::PassAccessRights; # included by "eval" in scan subroutine
=begin TML
---++ ClassMethod new()
Create a new ClamAV Connection object.
=cut
sub new {
my $this = shift;
my (%options) = @_;
$options{port} ||= '/tmp/clamd';
return bless \%options, $this;
}
=begin TML
---++ ObjectMethod Version -> $string
Return the clamd and database version information.
=cut
sub version {
my ($this) = @_;
my $conn = $this->_get_connection || return;
my $results = '';
my $response = '';
$this->_send( $conn, "zVERSION\x00" );
for my $result ( $conn->getline ) {
chop($result);
$response .= $result . "\n";
}
$conn->close;
return $response;
}
=begin TML
---++ ObjectMethod Ping -> $string
Pings the clamd to check it is alive. Returns true if it is alive, false if it is dead.
=cut
sub ping {
my ($this) = @_;
my $conn = $this->_get_connection || return;
$this->_send( $conn, "zPING\x00" );
chop( my $response = $conn->getline );
# Run out the buffer?
1 while (<$conn>);
$conn->close;
return (
$response eq "PONG"
? 1
: $this->errstr("Unknown reponse from ClamAV service: $response")
);
}
=begin TML
---++ ObjectMethod scan($dir_or_file) -> @array
Scan a directory or a file.
If Socket::PassAccessRights is available, then a file descriptor will be passed to clamd. Otherwise the file name
is passed, and __the resource must be readable by the user the ClamdAV clamd service is running as__.
Returns an array of
On error nothing is returned and the errstr() error handler is set.
=cut
sub scan {
my $this = shift;
my @results;
my $cmd = ( eval "use Socket::PassAccessRights;1;" ? 'FILDES' : 'SCAN' ) ;
$cmd = 'SCAN' if ( $this->{forceScan} ); # test purposes
if ( $this->{find_all} ) {
@results = $this->_scan( $cmd, @_ );
}
else {
@results = $this->_scan_shallow( $cmd, @_ );
}
return @results;
}
=begin TML
---++ ObjectMethod scan_stream($stream);
Preform a scan on a stream of data for viruses with the ClamAV clamd module.
Returns a list of two arguments: the first being the response which will be 'OK' or 'FOUND' the second being the virus found - if a virus is found.
On failure it sets the errstr() error handler.
=cut
sub scan_stream {
my ( $this, $st ) = @_;
$this->errstr();
my $conn = $this->_get_connection || die "no connection";
$this->_send( $conn, "zINSTREAM\x00" );
my @return;
# transfer 512KB blocks
my $transfer;
while ( my $r = sysread( $st, $transfer, 0x80000 ) ) {
if ( !defined $r ) {
next if ( $! == Errno::EINTR );
die "system read error: $!\n";
}
my $out = pack( 'N', ($r) ) . $transfer;
$this->_send( $conn, $out );
}
$this->_send( $conn, pack( 'N', (0) ) );
chomp( my $r = $conn->getline );
$conn->close;
if ( $r =~ m/stream: (.+) FOUND/i ) {
return ( 'FOUND', $1 );
}
else {
return ('OK');
}
}
=begin TML
---++ ObjectMethod scan_string($text);
Preform a scan on a string using the ClamAV clamd module.
Returns a list of two arguments: the first being the response which will be 'OK' or 'FOUND' the second being the virus found - if a virus is found.
On failure it sets the errstr() error handler.
=cut
sub scan_string {
my ( $this, $st ) = @_;
$this->errstr();
my $conn = $this->_get_connection || die "no connection";
$this->_send( $conn, "zINSTREAM\x00" );
my @return;
my $out = pack( 'N', ( length $st ) ) . $st;
$this->_send( $conn, $out );
$this->_send( $conn, pack( 'N', (0) ) ); # Mark end of stream.
chomp( my $r = $conn->getline );
$conn->close;
if ( $r =~ m/stream: (.+) FOUND/i ) {
return ( 'FOUND', $1 );
}
else {
return ('OK');
}
}
=begin TML
---++ ObjectMethod reload();
Cause ClamAV clamd service to reload its virus database.
=cut
sub reload {
my $this = shift;
my $conn = $this->_get_connection || return;
$this->_send( $conn, "zRELOAD\x00" );
my $response = $conn->getline;
1 while (<$conn>);
$conn->close;
return 1;
}
=begin TML
---++ ObjectMethod errstr($err) -> $string;
If called with a value, sets the error string and returns false, otherwise returns the error string.
=cut
sub errstr {
my ( $this, $err ) = @_;
if ($err) {
$this->{'.errstr'} = $err;
return 0;
}
else {
return $this->{'.errstr'};
}
}
=begin TML
---++ ObjectMethod _scan();
Internal function to scan a file or directory of files.
=cut
sub _scan {
my $this = shift;
my $cmd = shift;
my $options = {};
if ( ref( $_[-1] ) eq 'HASH' ) {
$options = pop(@_);
}
# Files
my @files = grep { -f $_ } @_;
# Directories
for my $dir (@_) {
next unless -d $dir;
find(
sub {
if ( -f $File::Find::name ) {
push @files, $File::Find::name;
}
},
$dir
);
}
if ( !@files ) {
$this->errstr(
"scan() requires that you specify a directory or file to scan");
return undef;
}
my @results;
for (@files) {
push @results, $this->_scan_shallow( $cmd, $_, $options );
}
return @results;
}
=begin TML
---++ ObjectMethod _scan_shallow();
Internal function to scan files, stopping on the first occurrence.
=cut
sub _scan_shallow {
# same as _scan, but stops at first virus
my $this = shift;
my $cmd = shift;
my $options = {};
if ( ref( $_[-1] ) eq 'HASH' ) {
$options = pop(@_);
}
my @dirs = @_;
my @results;
my $fd;
for my $file (@dirs) {
my $conn = $this->_get_connection || return;
if ( $cmd eq 'SCAN') {
$this->_send( $conn, "zSCAN $file\x00" );
}
else {
$this->_send( $conn, "zFILDES\x00" );
open( $fd, '<', $file );
Socket::PassAccessRights::sendfd(fileno($conn), fileno($fd)) or die;
close $fd;
}
for my $result ( $conn->getline ) {
chomp($result);
$result =~ s/\x00$//g; # remove null terminator if present;
my ($fn, $msg, $code) = $result =~ m/^(.*?):\s?(.*?)\s?(OK|ERROR|FOUND)$/;
my $fname = ( $cmd eq 'SCAN' ) ? $fn : $file;
push @results, [ $fname, $msg, $code ];
}
$conn->close;
}
return @results;
}
sub _send {
my ( $this, $fh, $data ) = @_;
return syswrite $fh, $data, length($data);
}
sub _get_connection {
my ($this) = @_;
if ( $this->{port} =~ /\D/ ) {
return $this->_get_unix_connection;
}
else {
return $this->_get_tcp_connection;
}
}
sub _get_tcp_connection {
my ( $this, $port ) = @_;
$port ||= $this->{port};
return IO::Socket::INET->new(
PeerAddr => 'localhost',
PeerPort => $port,
Proto => 'tcp',
Type => SOCK_STREAM,
Timeout => 10
) || $this->errstr("Cannot connect to 'localhost:$port': $@");
}
sub _get_unix_connection {
my ($this) = @_;
return IO::Socket::UNIX->new(
Type => SOCK_STREAM,
Peer => $this->{port}
)
|| $this->errstr("Cannot connect to unix socket '$this->{port}': $@");
}
1;
__END__
=head1 AUTHOR
George Clark, derived from previous work by:
Colin Faber <cfaber@fpsn.net> All Rights Reserved.
Originally based on the Clamd module authored by Matt Sergeant.
=head1 LICENSE
This is free software and may be used and distribute under terms of perl itself.
=cut