1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ownCloud - files_antivirus |
5
|
|
|
* |
6
|
|
|
* @author Manuel Deglado |
7
|
|
|
* @copyright 2012 Manuel Deglado [email protected] |
8
|
|
|
* |
9
|
|
|
* This library is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
11
|
|
|
* License as published by the Free Software Foundation; either |
12
|
|
|
* version 3 of the License, or any later version. |
13
|
|
|
* |
14
|
|
|
* This library is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public |
20
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\Files_Antivirus\Scanner; |
25
|
|
|
|
26
|
|
|
use OCA\Files_Antivirus\AppConfig; |
27
|
|
|
use OCA\Files_Antivirus\IScannable; |
28
|
|
|
use OCA\Files_Antivirus\Status; |
29
|
|
|
use OCP\ILogger; |
30
|
|
|
|
31
|
|
|
abstract class AbstractScanner { |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Scan result |
35
|
|
|
* @var Status |
36
|
|
|
*/ |
37
|
|
|
protected $status; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* If scanning was done part by part |
41
|
|
|
* the first detected infected part is stored here |
42
|
|
|
* @var Status |
43
|
|
|
*/ |
44
|
|
|
protected $infectedStatus; |
45
|
|
|
|
46
|
|
|
/** @var int */ |
47
|
|
|
protected $byteCount; |
48
|
|
|
|
49
|
|
|
/** @var resource */ |
50
|
|
|
protected $writeHandle; |
51
|
|
|
|
52
|
|
|
/** @var AppConfig */ |
53
|
|
|
protected $appConfig; |
54
|
|
|
|
55
|
|
|
/** @var ILogger */ |
56
|
|
|
protected $logger; |
57
|
|
|
|
58
|
|
|
/** @var string */ |
59
|
|
|
protected $lastChunk; |
60
|
|
|
|
61
|
|
|
/** @var bool */ |
62
|
|
|
protected $isLogUsed = false; |
63
|
|
|
|
64
|
|
|
/** @var bool */ |
65
|
|
|
protected $isAborted = false; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Close used resources |
69
|
|
|
*/ |
70
|
|
|
abstract protected function shutdownScanner(); |
71
|
|
|
|
72
|
12 |
|
public function __construct(AppConfig $config, ILogger $logger){ |
73
|
12 |
|
$this->appConfig = $config; |
74
|
12 |
|
$this->logger = $logger; |
75
|
12 |
|
} |
76
|
|
|
|
77
|
7 |
|
public function getStatus(){ |
78
|
7 |
|
if ($this->infectedStatus instanceof Status){ |
79
|
|
|
return $this->infectedStatus; |
80
|
|
|
} |
81
|
7 |
|
if ($this->status instanceof Status){ |
82
|
7 |
|
return $this->status; |
83
|
|
|
} |
84
|
|
|
return new Status(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Synchronous scan |
89
|
|
|
* @param IScannable $item |
90
|
|
|
* @return Status |
91
|
|
|
*/ |
92
|
2 |
|
public function scan(IScannable $item) { |
93
|
2 |
|
$this->initScanner(); |
94
|
|
|
|
95
|
2 |
|
while (false !== ($chunk = $item->fread())) { |
96
|
2 |
|
$this->writeChunk($chunk); |
97
|
2 |
|
} |
98
|
|
|
|
99
|
2 |
|
$this->shutdownScanner(); |
100
|
2 |
|
return $this->getStatus(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Async scan - new portion of data is available |
105
|
|
|
* @param string $data |
106
|
|
|
*/ |
107
|
5 |
|
public function onAsyncData($data){ |
108
|
5 |
|
$this->writeChunk($data); |
109
|
5 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Async scan - resource is closed |
113
|
|
|
* @return Status |
114
|
|
|
*/ |
115
|
5 |
|
public function completeAsyncScan(){ |
116
|
5 |
|
$this->shutdownScanner(); |
117
|
5 |
|
return $this->getStatus(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get write handle here. |
122
|
|
|
* Do NOT open connection in constructor because this method is used for reconnection |
123
|
|
|
*/ |
124
|
9 |
|
public function initScanner(){ |
125
|
9 |
|
$this->byteCount = 0; |
126
|
9 |
|
if ($this->status instanceof Status && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED){ |
127
|
|
|
$this->infectedStatus = clone $this->status; |
128
|
|
|
} |
129
|
9 |
|
$this->status = new Status(); |
130
|
9 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $chunk |
134
|
|
|
*/ |
135
|
7 |
|
protected function writeChunk($chunk){ |
136
|
7 |
|
$this->fwrite( |
137
|
7 |
|
$this->prepareChunk($chunk) |
138
|
7 |
|
); |
139
|
7 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $data |
143
|
|
|
*/ |
144
|
7 |
|
protected final function fwrite($data){ |
145
|
7 |
|
if ($this->isAborted){ |
146
|
|
|
return; |
147
|
|
|
} |
148
|
|
|
|
149
|
7 |
|
$dataLength = strlen($data); |
150
|
7 |
|
$streamSizeLimit = intval($this->appConfig->getAvStreamMaxLength()); |
151
|
7 |
|
if ($this->byteCount + $dataLength > $streamSizeLimit){ |
152
|
3 |
|
$this->logger->debug( |
153
|
3 |
|
'reinit scanner', |
154
|
3 |
|
['app' => 'files_antivirus'] |
155
|
3 |
|
); |
156
|
3 |
|
$this->shutdownScanner(); |
157
|
3 |
|
$isReopenSuccessful = $this->retry(); |
158
|
3 |
|
} else { |
159
|
5 |
|
$isReopenSuccessful = true; |
160
|
|
|
} |
161
|
|
|
|
162
|
7 |
|
if (!$isReopenSuccessful || !$this->writeRaw($data)){ |
163
|
|
|
if (!$this->isLogUsed) { |
164
|
|
|
$this->isLogUsed = true; |
165
|
|
|
$this->logger->warning( |
166
|
|
|
'Failed to write a chunk. Check if Stream Length matches StreamMaxLength in ClamAV daemon settings', |
167
|
|
|
['app' => 'files_antivirus'] |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
// retry on error |
171
|
|
|
$isRetrySuccessful = $this->retry() && $this->writeRaw($data); |
172
|
|
|
$this->isAborted = !$isRetrySuccessful; |
173
|
|
|
} |
174
|
7 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
3 |
|
protected function retry(){ |
180
|
3 |
|
$this->initScanner(); |
181
|
3 |
|
if (!is_null($this->lastChunk)) { |
182
|
1 |
|
return $this->writeRaw($this->lastChunk); |
183
|
|
|
} |
184
|
2 |
|
return true; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param $data |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
7 |
|
protected function writeRaw($data){ |
192
|
7 |
|
$dataLength = strlen($data); |
193
|
7 |
|
$bytesWritten = @fwrite($this->getWriteHandle(), $data); |
194
|
7 |
|
if ($bytesWritten === $dataLength){ |
195
|
7 |
|
$this->byteCount += $bytesWritten; |
196
|
7 |
|
$this->lastChunk = $data; |
197
|
7 |
|
return true; |
198
|
|
|
} |
199
|
|
|
return false; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get a resource to write data into |
204
|
|
|
* @return resource |
205
|
|
|
*/ |
206
|
9 |
|
protected function getWriteHandle(){ |
207
|
9 |
|
return $this->writeHandle; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Prepare chunk (if required) |
212
|
|
|
* @param string $data |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
3 |
|
protected function prepareChunk($data){ |
216
|
3 |
|
return $data; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|