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