]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/attachment.pm
add virus checking to attachments plugin
[ikiwiki.git] / IkiWiki / Plugin / attachment.pm
index 3bbe27b1a42cbfbdf2f63568b19c856e00385a03..cb762e453e9f92c368fed3784c1ad5820455e6f6 100644 (file)
@@ -20,7 +20,7 @@ sub formbuilder_setup (@) { #{{{
        my $form=$params{form};
        my $q=$params{cgi};
 
-       if ($form->field("do") eq "edit") {
+       if (defined $form->field("do") && $form->field("do") eq "edit") {
                $form->field(name => 'attachment', type => 'file');
                # These buttons are not put in the usual place, so
                # are not added to the normal formbuilder button list.
@@ -76,7 +76,7 @@ sub formbuilder (@) { #{{{
        my $form=$params{form};
        my $q=$params{cgi};
 
-       return if $form->field("do") ne "edit";
+       return if ! defined $form->field("do") || $form->field("do") ne "edit";
 
        my $filename=$q->param('attachment');
        if (defined $filename && length $filename &&
@@ -333,6 +333,44 @@ sub match_mimetype ($$;@) { #{{{
        }
 } #}}}
 
+sub match_virusfree ($$;@) { #{{{
+       shift;
+       my $wanted=shift;
+
+       my %params=@_;
+       if (! exists $params{file}) {
+               return IkiWiki::FailReason->new("no file specified");
+       }
+
+       if (! exists $IkiWiki::config{virus_checker} ||
+           ! length $IkiWiki::config{virus_checker}) {
+               return IkiWiki::FailReason->new("no virus_checker configured");
+       }
+
+       # The file needs to be fed into the virus checker on stdin,
+       # because the file is not world-readable, and if clamdscan is
+       # used, clamd would fail to read it.
+       eval q{use IPC::Open2};
+       error($@) if $@;
+       open (IN, "<", $params{file}) || return IkiWiki::FailReason->new("failed to read file");
+       binmode(IN);
+       my $sigpipe=0;
+       $SIG{PIPE} = sub { $sigpipe=1 };
+       my $pid=open2(\*CHECKER_OUT, "<&IN", $IkiWiki::config{virus_checker}); 
+       my $reason=<CHECKER_OUT>;
+       chomp $reason;
+       1 while (<CHECKER_OUT>);
+       close(CHECKER_OUT);
+       waitpid $pid, 0;
+       $SIG{PIPE}="DEFAULT";
+       if ($sigpipe || $?) {
+               return IkiWiki::FailReason->new("file seems to contain a virus ($reason)");
+       }
+       else {
+               return IkiWiki::SuccessReason->new("file seems virusfree ($reason)");
+       }
+} #}}}
+
 sub match_ispage ($$;@) { #{{{
        my $filename=shift;