Unfortunatly, Github BT doesn't allow attachment of patches in issues,
so, I'm posting it here. Here's a simple patch to scan uploads using
ClamAV. It's quite minimal for now

- it's limited to grant uploads (but it's easy to also add it for
tickets)

- if a virus is found, the interface will display the generic internal
error message. Would be better to tell the user a virus was found, but I
don't know how to propagate the info in uploadErrorStr (sorry, I'm not a
PHP guru ;-))

- I've only tested on a local clamd process using a UNIX socket. It
might work with a TCP socket (in which case clamd could also run on a
separate machine)

Comments are welcom :-)

Regards, Daniel
-- 
Daniel Berteaud
FIREWALL-SERVICES SARL.
Société de Services en Logiciels Libres
Technopôle Montesquieu
33650 MARTILLAC
Tel : 05 56 64 15 32
Fax : 05 56 64 15 32
Web : http://www.firewall-services.com
diff -Nur -x '*.orig' -x '*.rej' dl-0.11/htdocs/include/config.php.dist mezzanine_patched_dl-0.11/htdocs/include/config.php.dist
--- dl-0.11/htdocs/include/config.php.dist	2013-07-05 19:09:30.000000000 +0200
+++ mezzanine_patched_dl-0.11/htdocs/include/config.php.dist	2013-11-19 22:55:22.618030012 +0100
@@ -77,4 +77,13 @@
 // gcLimit: Maximum number of tickets to remove at every expiration.
 //          If 0 is used, all expired tickets are removed at once.
 //$gcLimit = 0;
+
+// clamdSocket: define the path of clamd UNIX socket if you want
+//              files uploaded through grants to be scanned
+//              Leave it undefined to disable this functionnality
+//$clamdScan = 'unix:///var/clamav/clamd.socket';
+
+// scanMax: define the maximum file size to be scan (in bytes). Files above this
+//          limit won't be scanned. Should be under StreamMaxLength in clamd.conf
+//$scanMax = 20 * 1024 *1024;
 ?>
diff -Nur -x '*.orig' -x '*.rej' dl-0.11/htdocs/include/funcs.php mezzanine_patched_dl-0.11/htdocs/include/funcs.php
--- dl-0.11/htdocs/include/funcs.php	2013-07-04 14:44:22.000000000 +0200
+++ mezzanine_patched_dl-0.11/htdocs/include/funcs.php	2013-11-19 22:50:48.372360158 +0100
@@ -344,4 +344,21 @@
   return array("user" => $user, "pass" => $pass);
 }
 
+function clamdScan($file)
+{
+  global $clamdSocket;
+  $return  = null;
+
+  $socket = fsockopen($clamdSocket);
+  fwrite($socket, "zINSTREAM\0");
+  $fh = fopen($file, 'r');
+  $size = filesize($file);
+  fwrite($socket,pack("N", $size));
+  stream_copy_to_stream($fh, $socket);
+  fwrite($socket, pack("N", 0));
+  $return = fread($socket,8192);
+  $return = str_replace(' FOUND', '', $return);
+  return trim(str_replace('stream: ', '', $return));
+}
+
 ?>
diff -Nur -x '*.orig' -x '*.rej' dl-0.11/htdocs/include/grant.php mezzanine_patched_dl-0.11/htdocs/include/grant.php
--- dl-0.11/htdocs/include/grant.php	2013-05-29 21:02:30.000000000 +0200
+++ mezzanine_patched_dl-0.11/htdocs/include/grant.php	2013-11-19 22:50:06.909256069 +0100
@@ -45,10 +45,20 @@
 
 function handleUpload($GRANT, $FILE)
 {
-  global $dataDir, $db;
+  global $dataDir, $db, $clamdSocket, $scanMax;
 
   // generate new unique id/file name
   list($id, $tmpFile) = genTicketId($FILE["name"]);
+  $size = filesize($FILE["tmp_name"]);
+  // stream the file to clamd if $clamdSocket is configured
+  if (isset($clamdSocket) && !empty($clamdSocket)){
+    if ($size > 0 && $size <= $scanMax){
+      $res = clamdScan($FILE["tmp_name"]);
+      if ($res != 'OK'){
+        return failUpload($tmpFile);
+      }
+    }
+  }
   if(!move_uploaded_file($FILE["tmp_name"], $tmpFile))
     return failUpload($tmpFile);
 

Reply via email to