Skip to content

Commit

Permalink
Item10700: Get existing attachment scan working
Browse files Browse the repository at this point in the history
Some of the interface still needs some work - First pass to prove out
the operation.

git-svn-id: http://svn.foswiki.org/trunk/ClamAVScanPlugin@11648 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed May 8, 2011
1 parent 076c6af commit 48f645a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
14 changes: 11 additions & 3 deletions data/System/ClamAVScanPlugin.txt
Expand Up @@ -49,12 +49,20 @@ The current attachments and attachment rcs files can be scanned using a rest han
Each attachment and corresponding rcs file if it exists is passed to clamd for
scanning. Results of the scan are reported back in the rest results.

All attachments are scanned. There is no facility to request scan of an
All attachments and the corresponding rcs =,v= files are scanned. There is no facility to request scan of an
individual attachment.

This operation is restricted to users in the Admin group
.
%SCRIPTURL{rest}%/ClamAVScanPlugin/scan/Web/Topic
---++++ Web interface:

* %SCRIPTURL{rest}%/ClamAVScanPlugin/scan?topic=System.PatternSkin

---++++ Shell interface

<verbatim>
cd [Foswiki/bin] directory
./rest /ClamAVScanPlugin/scan -topic System.PatternSkin
</verbatim>

---+++ Reloading the Virus Signatures.

Expand Down
55 changes: 41 additions & 14 deletions lib/Foswiki/Plugins/ClamAVScanPlugin.pm
Expand Up @@ -28,7 +28,7 @@ my $clamdPort; # Unix socket used to communicate with clamd daemon

=begin TML
---++ ClassMethod initPlugin($topic, $web, $user) -> $boolean
---++ StaticMethod initPlugin($topic, $web, $user) -> $boolean
=cut

Expand All @@ -50,18 +50,18 @@ sub initPlugin {
Foswiki::Func::registerTagHandler( 'CLAMAVSTATUS', \&_CLAMAVSTATUS );

# Request clamd to reload the virus signatures
Foswiki::Func::registerRESTHandler( 'reload', \&reloadSignatures );
Foswiki::Func::registerRESTHandler( 'reload', \&_reloadSignatures );

# Request clamd to scan the attachments of a topic
Foswiki::Func::registerRESTHandler( 'scan', \&scanAttachments );
Foswiki::Func::registerRESTHandler( 'scan', \&_scanAttachments );

# Plugin correctly initialized
return 1;
}

=begin TML
---++ ClassMethod _CLAMAVSTATUS() -> $string
---++ StaticMethod _CLAMAVSTATUS() -> $string
Registered Handler: Implements the CLAMAVSTATUS macro. Returns the status string.
Expand Down Expand Up @@ -105,7 +105,7 @@ sub _CLAMAVSTATUS {

=begin TML
---++ ClassMethod beforeUploadHandler()
---++ StaticMethod beforeUploadHandler()
Intercepts the newly uploaded attachment before it has been stored in Foswiki.
Expand Down Expand Up @@ -145,7 +145,7 @@ sub beforeUploadHandler {

=begin TML
---++ ClassMethod beforeSaveHandler()
---++ StaticMethod beforeSaveHandler()
Intercepts an upated topic prior to save.
Expand Down Expand Up @@ -174,7 +174,7 @@ sub beforeSaveHandler {

=begin TML
---++ ClassMethod reloadSignatures($session) -> $text
---++ StaticMethod reloadSignatures($session) -> $text
Implements the rest handler "reload"
Expand All @@ -183,7 +183,7 @@ This function is only available to administrators.
=cut

sub reloadSignatures {
sub _reloadSignatures {
my ( $session, $subject, $verb, $response ) = @_;

return "Not authorized" unless Foswiki::Func::isAnAdmin();
Expand All @@ -200,21 +200,48 @@ sub reloadSignatures {

=begin TML
---++ ClassMethod scanAttachments($session) -> $text
---++ StaticMethod scanAttachments($session) -> $text
Implements the rest handler "scan"
Performs a virus scan of all attachment for a topic.
Performs a virus scan of all attachment for a topic. This includes the rcs ",v"
files. It's important to scan data stored in the prior revisions of a file.
This function is only available to administrators.
=cut

sub scanAttachments {
my ( $session, $subject, $verb, $response ) = @_;

sub _scanAttachments {
my $session = shift;
return "Not authorized" unless Foswiki::Func::isAnAdmin();
return "SCAN initiated for $subject \n\n";

my $query = Foswiki::Func::getCgiQuery();
my $resp = '';

my $nl = ( Foswiki::Func::getContext()->{'command_line'} ) ? "\n" : '<br />';

my $topic = $query->param('topic');
my $web;
( $web, $topic ) = Foswiki::Func::normalizeWebTopicName( undef, $topic );

my $dir = "$Foswiki::cfg{PubDir}/$web/$topic";
my $dh;
opendir( $dh, $dir ) || return "No attachment directory found for $topic $nl";

my $av =
new Foswiki::Plugins::ClamAVScanPlugin::ClamAV( port => "$clamdPort", find_all => 1, forceScan => 1);

foreach my $fn ( grep { -f "$dir/$_"}readdir($dh) ) {
my @results = $av->scan( "$dir/$fn" );

foreach my $x ( @results) {
$resp .= "@$x[0] - @$x[1] - @$x[2] $nl";
}
}
closedir($dh);
return $resp . $nl;
}

1;

__END__
Expand Down
25 changes: 16 additions & 9 deletions lib/Foswiki/Plugins/ClamAVScanPlugin/ClamAV.pm
Expand Up @@ -6,6 +6,13 @@ 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) = @_;
Expand All @@ -14,7 +21,7 @@ sub new {
}

=begin TML
---++ Version
---++ ObjectMethod Version -> $string
Return the clamd and database version information.
Expand All @@ -39,7 +46,7 @@ sub version {
}

=begin TML
---++ Ping
---++ ObjectMethod Ping -> $string
Pings the clamd to check it is alive. Returns true if it is alive, false if it is dead.
Expand All @@ -65,7 +72,7 @@ sub ping {
}

=begin TML
---++ scan($dir_or_file)
---++ ObjectMethod scan($dir_or_file) -> @array
Scan a directory or a file.
Expand Down Expand Up @@ -97,7 +104,7 @@ sub scan {

=begin TML
---++ scan_stream($stream);
---++ ObjectMethod scan_stream($stream);
Preform a scan on a stream of data for viruses with the ClamAV clamd module.
Expand Down Expand Up @@ -143,7 +150,7 @@ sub scan_stream {

=begin TML
---++ scan_string($text);
---++ ObjectMethod scan_string($text);
Preform a scan on a string using the ClamAV clamd module.
Expand Down Expand Up @@ -181,7 +188,7 @@ sub scan_string {

=begin TML
---++ reload();
---++ ObjectMethod reload();
Cause ClamAV clamd service to reload its virus database.
Expand All @@ -200,7 +207,7 @@ sub reload {

=begin TML
---++ ClassMethod errstr($err) -> $string;
---++ ObjectMethod errstr($err) -> $string;
If called with a value, sets the error string and returns false, otherwise returns the error string.
Expand All @@ -219,7 +226,7 @@ sub errstr {

=begin TML
---++ ClassMethod _scan();
---++ ObjectMethod _scan();
Internal function to scan a file or directory of files.
Expand Down Expand Up @@ -266,7 +273,7 @@ sub _scan {

=begin TML
---++ ClassMethod _scan_shallow();
---++ ObjectMethod _scan_shallow();
Internal function to scan files, stopping on the first occurrence.
Expand Down

0 comments on commit 48f645a

Please sign in to comment.