mirror of
https://github.com/moodle/moodle.git
synced 2025-08-08 02:16:41 +02:00
MDL-64075 antivirus_clamav: refactor scan_data for tcpsocket
- Fix whitespace accross scanner_test for codechecker - Deprecate \antivirus_clamav\scanner::scan_data_execute_unixsocket()
This commit is contained in:
parent
22b617569c
commit
90c6f0cfea
2 changed files with 127 additions and 42 deletions
|
@ -34,6 +34,7 @@ define('ANTIVIRUS_CLAMAV_SOCKET_CHUNKSIZE', 1024);
|
|||
/**
|
||||
* Class implementing ClamAV antivirus.
|
||||
* @copyright 2015 Ruslan Kabalin, Lancaster University.
|
||||
* @copyright 2019 Didier Raboud, Liip AG.
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class scanner extends \core\antivirus\scanner {
|
||||
|
@ -47,6 +48,8 @@ class scanner extends \core\antivirus\scanner {
|
|||
return (bool)$this->get_config('pathtoclam');
|
||||
} else if ($this->get_config('runningmethod') === 'unixsocket') {
|
||||
return (bool)$this->get_config('pathtounixsocket');
|
||||
} else if ($this->get_config('runningmethod') === 'tcpsocket') {
|
||||
return (bool)$this->get_config('tcpsockethost') && (bool)$this->get_config('tcpsocketport');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -92,10 +95,11 @@ class scanner extends \core\antivirus\scanner {
|
|||
* @return int Scanning result constant.
|
||||
*/
|
||||
public function scan_data($data) {
|
||||
// We can do direct stream scanning if unixsocket running method is in use,
|
||||
// We can do direct stream scanning if unixsocket or tcpsocket running methods are in use,
|
||||
// if not, use default process.
|
||||
if ($this->get_config('runningmethod') === 'unixsocket') {
|
||||
$return = $this->scan_data_execute_unixsocket($data);
|
||||
$runningmethod = $this->get_config('runningmethod');
|
||||
if (in_array($runningmethod, array('unixsocket', 'tcpsocket'))) {
|
||||
$return = $this->scan_data_execute_socket($data, $runningmethod);
|
||||
|
||||
if ($return === self::SCAN_RESULT_ERROR) {
|
||||
$this->message_admins($this->get_scanning_notice());
|
||||
|
@ -120,6 +124,15 @@ class scanner extends \core\antivirus\scanner {
|
|||
return 'unix://' . $this->get_config('pathtounixsocket');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Internet domain socket destination url
|
||||
*
|
||||
* @return string The socket url, fit for stream_socket_client()
|
||||
*/
|
||||
private function get_tcpsocket_destination() {
|
||||
return 'tcp://' . $this->get_config('tcpsockethost') . ':' . $this->get_config('tcpsocketport');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string equivalent of a numeric clam error code
|
||||
*
|
||||
|
@ -226,12 +239,12 @@ class scanner extends \core\antivirus\scanner {
|
|||
// After scanning we revert permissions to initial ones.
|
||||
chmod($file, $perms);
|
||||
// Parse the output.
|
||||
return $this->parse_unixsocket_response($output);
|
||||
return $this->parse_socket_response($output);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan data using unix socket.
|
||||
* Scan data socket.
|
||||
*
|
||||
* We are running INSTREAM command and passing data stream in chunks.
|
||||
* The format of the chunk is: <length><data> where <length> is the size of the following
|
||||
|
@ -240,11 +253,25 @@ class scanner extends \core\antivirus\scanner {
|
|||
* Do not exceed StreamMaxLength as defined in clamd.conf, otherwise clamd will
|
||||
* reply with INSTREAM size limit exceeded and close the connection.
|
||||
*
|
||||
* @param string $data The varaible containing the data to scan.
|
||||
* @param string $data The variable containing the data to scan.
|
||||
* @param string $type Either 'tcpsocket' or 'unixsocket'
|
||||
* @return int Scanning result constant.
|
||||
*/
|
||||
public function scan_data_execute_unixsocket($data) {
|
||||
$socket = stream_socket_client($this->get_unixsocket_destination(), $errno, $errstr, ANTIVIRUS_CLAMAV_SOCKET_TIMEOUT);
|
||||
public function scan_data_execute_socket($data, $type) {
|
||||
switch ($type) {
|
||||
case "tcpsocket":
|
||||
$socketurl = $this->get_tcpsocket_destination();
|
||||
break;
|
||||
case "unixsocket":
|
||||
$socketurl = $this->get_unixsocket_destination();
|
||||
break;
|
||||
default;
|
||||
// This should not happen.
|
||||
debugging('Unknown socket type!');
|
||||
return self::SCAN_RESULT_ERROR;
|
||||
}
|
||||
|
||||
$socket = stream_socket_client($socketurl, $errno, $errstr, ANTIVIRUS_CLAMAV_SOCKET_TIMEOUT);
|
||||
if (!$socket) {
|
||||
// Can't open socket for some reason, notify admins.
|
||||
$notice = get_string('errorcantopensocket', 'antivirus_clamav', "$errstr ($errno)");
|
||||
|
@ -270,17 +297,17 @@ class scanner extends \core\antivirus\scanner {
|
|||
fclose($socket);
|
||||
|
||||
// Parse the output.
|
||||
return $this->parse_unixsocket_response($output);
|
||||
return $this->parse_socket_response($output);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse unix socket command response.
|
||||
* Parse socket command response.
|
||||
*
|
||||
* @param string $output The unix socket command response.
|
||||
* @param string $output The socket response.
|
||||
* @return int Scanning result constant.
|
||||
*/
|
||||
private function parse_unixsocket_response($output) {
|
||||
private function parse_socket_response($output) {
|
||||
$splitoutput = explode(': ', $output);
|
||||
$message = trim($splitoutput[1]);
|
||||
if ($message === 'OK') {
|
||||
|
@ -298,4 +325,19 @@ class scanner extends \core\antivirus\scanner {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan data using Unix domain socket.
|
||||
*
|
||||
* @deprecated since Moodle 3.9 MDL-64075 - please do not use this function any more.
|
||||
* @see antivirus_clamav\scanner::scan_data_execute_socket()
|
||||
*
|
||||
* @param string $data The variable containing the data to scan.
|
||||
* @return int Scanning result constant.
|
||||
*/
|
||||
public function scan_data_execute_unixsocket($data) {
|
||||
debugging('antivirus_clamav\scanner::scan_data_execute_unixsocket() is deprecated. ' .
|
||||
'Use antivirus_clamav\scanner::scan_data_execute_socket() instead.', DEBUG_DEVELOPER);
|
||||
return $this->scan_data_execute_socket($data, "unixsocket");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue