[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/antivirus/clamav/classes/ -> scanner.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * ClamAV antivirus integration.
  19   *
  20   * @package    antivirus_clamav
  21   * @copyright  2015 Ruslan Kabalin, Lancaster University.
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace antivirus_clamav;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Class implemeting ClamAV antivirus.
  31   * @copyright  2015 Ruslan Kabalin, Lancaster University.
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class scanner extends \core\antivirus\scanner {
  35      /**
  36       * Are the necessary antivirus settings configured?
  37       *
  38       * @return bool True if all necessary config settings been entered
  39       */
  40      public function is_configured() {
  41          return (bool)$this->get_config('pathtoclam');
  42      }
  43      /**
  44       * Scan file, throws exception in case of infected file.
  45       *
  46       * @param string $file Full path to the file.
  47       * @param string $filename Name of the file (could be different from physical file if temp file is used).
  48       * @param bool $deleteinfected whether infected file needs to be deleted.
  49       * @throws moodle_exception If file is infected.
  50       * @return void
  51       */
  52      public function scan_file($file, $filename, $deleteinfected) {
  53          global $CFG;
  54  
  55          if (!is_readable($file)) {
  56              // This should not happen.
  57              debugging('File is not readable.');
  58              return;
  59          }
  60  
  61          // Execute the scan using preferable method.
  62          list($return, $notice) = $this->scan_file_execute_commandline($file);
  63  
  64          if ($return == 0) {
  65              // Perfect, no problem found, file is clean.
  66              return;
  67          } else if ($return == 1) {
  68              // Infection found.
  69              if ($deleteinfected) {
  70                  unlink($file);
  71              }
  72              throw new \core\antivirus\scanner_exception('virusfounduser', '', array('filename' => $filename));
  73          } else {
  74              // Unknown problem.
  75              $this->message_admins($notice);
  76              if ($this->get_config('clamfailureonupload') === 'actlikevirus') {
  77                  if ($deleteinfected) {
  78                      unlink($file);
  79                  }
  80                  throw new \core\antivirus\scanner_exception('virusfounduser', '', array('filename' => $filename));
  81              } else {
  82                  return;
  83              }
  84          }
  85      }
  86  
  87      /**
  88       * Returns the string equivalent of a numeric clam error code
  89       *
  90       * @param int $returncode The numeric error code in question.
  91       * @return string The definition of the error code
  92       */
  93      private function get_clam_error_code($returncode) {
  94          $returncodes = array();
  95          $returncodes[0] = 'No virus found.';
  96          $returncodes[1] = 'Virus(es) found.';
  97          $returncodes[2] = ' An error occured'; // Specific to clamdscan.
  98          // All after here are specific to clamscan.
  99          $returncodes[40] = 'Unknown option passed.';
 100          $returncodes[50] = 'Database initialization error.';
 101          $returncodes[52] = 'Not supported file type.';
 102          $returncodes[53] = 'Can\'t open directory.';
 103          $returncodes[54] = 'Can\'t open file. (ofm)';
 104          $returncodes[55] = 'Error reading file. (ofm)';
 105          $returncodes[56] = 'Can\'t stat input file / directory.';
 106          $returncodes[57] = 'Can\'t get absolute path name of current working directory.';
 107          $returncodes[58] = 'I/O error, please check your filesystem.';
 108          $returncodes[59] = 'Can\'t get information about current user from /etc/passwd.';
 109          $returncodes[60] = 'Can\'t get information about user \'clamav\' (default name) from /etc/passwd.';
 110          $returncodes[61] = 'Can\'t fork.';
 111          $returncodes[63] = 'Can\'t create temporary files/directories (check permissions).';
 112          $returncodes[64] = 'Can\'t write to temporary directory (please specify another one).';
 113          $returncodes[70] = 'Can\'t allocate and clear memory (calloc).';
 114          $returncodes[71] = 'Can\'t allocate memory (malloc).';
 115          if (isset($returncodes[$returncode])) {
 116              return $returncodes[$returncode];
 117          }
 118          return get_string('unknownerror', 'antivirus_clamav');
 119      }
 120  
 121      /**
 122       * Scan file using command line utility.
 123       *
 124       * @param string $file Full path to the file.
 125       * @return array ($return, $notice) Execution return code and notification text.
 126       */
 127      public function scan_file_execute_commandline($file) {
 128          $pathtoclam = trim($this->get_config('pathtoclam'));
 129  
 130          if (!file_exists($pathtoclam) or !is_executable($pathtoclam)) {
 131              // Misconfigured clam, notify admins.
 132              $notice = get_string('invalidpathtoclam', 'antivirus_clamav', $pathtoclam);
 133              return array(-1, $notice);
 134          }
 135  
 136          $clamparam = ' --stdout ';
 137          // If we are dealing with clamdscan, clamd is likely run as a different user
 138          // that might not have permissions to access your file.
 139          // To make clamdscan work, we use --fdpass parameter that passes the file
 140          // descriptor permissions to clamd, which allows it to scan given file
 141          // irrespective of directory and file permissions.
 142          if (basename($pathtoclam) == 'clamdscan') {
 143              $clamparam .= '--fdpass ';
 144          }
 145          // Execute scan.
 146          $cmd = escapeshellcmd($pathtoclam).$clamparam.escapeshellarg($file);
 147          exec($cmd, $output, $return);
 148          $notice = '';
 149          if ($return > 1) {
 150              $notice = get_string('clamfailed', 'antivirus_clamav', $this->get_clam_error_code($return));
 151              $notice .= "\n\n". implode("\n", $output);
 152          }
 153  
 154          return array($return, $notice);
 155      }
 156  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1