|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2000-2005 Rodolphe Quiedeville <[email protected]> |
|
3
|
|
|
* Copyright (C) 2003 Jean-Louis Bergamo <[email protected]> |
|
4
|
|
|
* Copyright (C) 2004-2009 Laurent Destailleur <[email protected]> |
|
5
|
|
|
* Copyright (C) 2005-2009 Regis Houssin <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* or see http://www.gnu.org/ |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* \file htdocs/core/class/antivir.class.php |
|
24
|
|
|
* \brief File of class to scan viruses |
|
25
|
|
|
* \author Laurent Destailleur. |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* \class AntiVir |
|
30
|
|
|
* \brief Class to scan for virus |
|
31
|
|
|
*/ |
|
32
|
|
|
class AntiVir |
|
33
|
|
|
{ |
|
34
|
|
|
var $error; |
|
35
|
|
|
/** |
|
36
|
|
|
* |
|
37
|
|
|
* @var string[] Error codes (or messages) |
|
38
|
|
|
*/ |
|
39
|
|
|
public $errors = array ();; |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
var $output; |
|
42
|
|
|
var $db; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Constructor |
|
46
|
|
|
* |
|
47
|
|
|
* @param DoliDB $db Database handler |
|
48
|
|
|
*/ |
|
49
|
|
|
function __construct($db) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->db=$db; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Scan a file with antivirus. |
|
56
|
|
|
* This function runs the command defined in setup. This antivirus command must return 0 if OK. |
|
57
|
|
|
* Return also true (virus found) if file end with '.virus' (so we can make test safely). |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $file File to scan |
|
60
|
|
|
* @return int <0 if KO (-98 if error, -99 if virus), 0 if OK |
|
61
|
|
|
*/ |
|
62
|
|
|
function dol_avscan_file($file) |
|
63
|
|
|
{ |
|
64
|
|
|
global $conf; |
|
65
|
|
|
|
|
66
|
|
|
$return = 0; |
|
67
|
|
|
|
|
68
|
|
|
if (preg_match('/\.virus$/i', $file)) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->errors='File has an extension saying file is a virus'; |
|
71
|
|
|
return -97; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$fullcommand=$this->getCliCommand($file); |
|
75
|
|
|
//$fullcommand='"c:\Program Files (x86)\ClamWin\bin\clamscan.exe" --database="C:\Program Files (x86)\ClamWin\lib" "c:\temp\aaa.txt"'; |
|
76
|
|
|
$fullcommand.=' 2>&1'; // This is to get error output |
|
77
|
|
|
|
|
78
|
|
|
$output=array(); |
|
79
|
|
|
$return_var=0; |
|
80
|
|
|
$safemode=ini_get("safe_mode"); |
|
81
|
|
|
// Create a clean fullcommand |
|
82
|
|
|
dol_syslog("AntiVir::dol_avscan_file Run command=".$fullcommand." with safe_mode ".($safemode?"on":"off")); |
|
83
|
|
|
// Run CLI command. If run of Windows, you can get return with echo %ERRORLEVEL% |
|
84
|
|
|
$lastline=exec($fullcommand, $output, $return_var); |
|
85
|
|
|
|
|
86
|
|
|
//print "x".$lastline." - ".join(',',$output)." - ".$return_var."y";exit; |
|
87
|
|
|
|
|
88
|
|
|
/* |
|
89
|
|
|
$outputfile=$conf->admin->dir_temp.'/dol_avscan_file.out.'.session_id(); |
|
90
|
|
|
$handle = fopen($outputfile, 'w'); |
|
91
|
|
|
if ($handle) |
|
92
|
|
|
{ |
|
93
|
|
|
$handlein = popen($fullcommand, 'r'); |
|
94
|
|
|
while (!feof($handlein)) |
|
95
|
|
|
{ |
|
96
|
|
|
$read = fgets($handlein); |
|
97
|
|
|
fwrite($handle,$read); |
|
98
|
|
|
} |
|
99
|
|
|
pclose($handlein); |
|
100
|
|
|
|
|
101
|
|
|
$errormsg = fgets($handle,2048); |
|
102
|
|
|
$this->output=$errormsg; |
|
103
|
|
|
|
|
104
|
|
|
fclose($handle); |
|
105
|
|
|
|
|
106
|
|
|
if (! empty($conf->global->MAIN_UMASK)) |
|
107
|
|
|
@chmod($outputfile, octdec($conf->global->MAIN_UMASK)); |
|
108
|
|
|
} |
|
109
|
|
|
else |
|
110
|
|
|
{ |
|
111
|
|
|
$langs->load("errors"); |
|
112
|
|
|
dol_syslog("Failed to open file ".$outputfile,LOG_ERR); |
|
113
|
|
|
$this->error="ErrorFailedToWriteInDir"; |
|
114
|
|
|
$return=-1; |
|
115
|
|
|
} |
|
116
|
|
|
*/ |
|
117
|
|
|
|
|
118
|
|
|
dol_syslog("AntiVir::dol_avscan_file Result return_var=".$return_var." output=".join(',',$output)); |
|
119
|
|
|
|
|
120
|
|
|
$returncodevirus=1; |
|
121
|
|
|
if ($return_var == $returncodevirus) // Virus found |
|
122
|
|
|
{ |
|
123
|
|
|
$this->errors=$output; |
|
124
|
|
|
return -99; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if ($return_var > 0) // If other error |
|
128
|
|
|
{ |
|
129
|
|
|
$this->errors=$output; |
|
130
|
|
|
return -98; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// If return code = 0 |
|
134
|
|
|
return 1; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get full Command Line to run |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $file File to scan |
|
143
|
|
|
* @return string Full command line to run |
|
144
|
|
|
*/ |
|
145
|
|
|
function getCliCommand($file) |
|
146
|
|
|
{ |
|
147
|
|
|
global $conf; |
|
148
|
|
|
|
|
149
|
|
|
$maxreclevel = 5 ; // maximal recursion level |
|
150
|
|
|
$maxfiles = 1000; // maximal number of files to be scanned within archive |
|
151
|
|
|
$maxratio = 200; // maximal compression ratio |
|
152
|
|
|
$bz2archivememlim = 0; // limit memory usage for bzip2 (0/1) |
|
153
|
|
|
$maxfilesize = 10485760; // archived files larger than this value (in bytes) will not be scanned |
|
154
|
|
|
|
|
155
|
|
|
$command=$conf->global->MAIN_ANTIVIRUS_COMMAND; |
|
156
|
|
|
$param=$conf->global->MAIN_ANTIVIRUS_PARAM; |
|
157
|
|
|
|
|
158
|
|
|
$param=preg_replace('/%maxreclevel/',$maxreclevel,$param); |
|
159
|
|
|
$param=preg_replace('/%maxfiles/',$maxfiles,$param); |
|
160
|
|
|
$param=preg_replace('/%maxratio/',$maxratio,$param); |
|
161
|
|
|
$param=preg_replace('/%bz2archivememlim/',$bz2archivememlim,$param); |
|
162
|
|
|
$param=preg_replace('/%maxfilesize/',$maxfilesize,$param); |
|
163
|
|
|
$param=preg_replace('/%file/',trim($file),$param); |
|
164
|
|
|
|
|
165
|
|
|
if (! preg_match('/%file/',$conf->global->MAIN_ANTIVIRUS_PARAM)) |
|
166
|
|
|
$param=$param." ".escapeshellarg(trim($file)); |
|
167
|
|
|
|
|
168
|
|
|
if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command. Using escapeshellcmd fails. |
|
169
|
|
|
|
|
170
|
|
|
$ret=$command.' '.$param; |
|
171
|
|
|
//$ret=$command.' '.$param.' 2>&1'; |
|
172
|
|
|
//print "xx".$ret."xx";exit; |
|
173
|
|
|
|
|
174
|
|
|
return $ret; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
|