1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - files_antivirus |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Viktar Dubiniuk <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @copyright Viktar Dubiniuk 2014-2018 |
11
|
|
|
* @license AGPL-3.0 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OCA\Files_Antivirus; |
15
|
|
|
|
16
|
|
|
class Status { |
17
|
|
|
|
18
|
|
|
/* |
19
|
|
|
* The file was not checked (e.g. because the AV daemon wasn't running). |
20
|
|
|
*/ |
21
|
|
|
const SCANRESULT_UNCHECKED = -1; |
22
|
|
|
|
23
|
|
|
/* |
24
|
|
|
* The file was checked and found to be clean. |
25
|
|
|
*/ |
26
|
|
|
const SCANRESULT_CLEAN = 0; |
27
|
|
|
|
28
|
|
|
/* |
29
|
|
|
* The file was checked and found to be infected. |
30
|
|
|
*/ |
31
|
|
|
const SCANRESULT_INFECTED = 1; |
32
|
|
|
|
33
|
|
|
/* |
34
|
|
|
* Should be SCANRESULT_UNCHECKED | SCANRESULT_INFECTED | SCANRESULT_CLEAN |
35
|
|
|
*/ |
36
|
|
|
protected $numericStatus; |
37
|
|
|
|
38
|
|
|
/* |
39
|
|
|
* Virus name or error message |
40
|
|
|
*/ |
41
|
|
|
protected $details = ""; |
42
|
|
|
|
43
|
|
|
protected $ruleMapper; |
44
|
|
|
|
45
|
10 |
|
public function __construct() { |
46
|
10 |
|
$this->numericStatus = self::SCANRESULT_UNCHECKED; |
47
|
10 |
|
$this->ruleMapper = new Db\RuleMapper(\OC::$server->getDb()); |
48
|
10 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get scan status as integer |
52
|
|
|
* |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
8 |
|
public function getNumericStatus() { |
56
|
8 |
|
return $this->numericStatus; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get scan status as string |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
4 |
|
public function getDetails() { |
65
|
4 |
|
return $this->details; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $rawResponse |
70
|
|
|
* @param integer $result |
71
|
|
|
*/ |
72
|
8 |
|
public function parseResponse($rawResponse, $result = null) { |
73
|
8 |
|
$matches = array(); |
74
|
8 |
|
$ruleMapper = new Db\RuleMapper(\OC::$server->getDb()); |
75
|
8 |
|
if (\is_null($result)) { // Daemon or socket mode |
76
|
|
|
try{ |
77
|
5 |
|
$allRules = $this->getResponseRules(); |
78
|
|
|
} catch (\Exception $e){ |
79
|
|
|
\OCP\Util::writeLog( |
80
|
|
|
'files_antivirus', |
81
|
|
|
__METHOD__.', exception: '.$e->getMessage(), |
82
|
|
|
\OCP\Util::ERROR |
83
|
|
|
); |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
5 |
|
$isMatched = false; |
88
|
5 |
|
foreach ($allRules as $rule) { |
89
|
5 |
|
if (\preg_match($rule->getMatch(), $rawResponse, $matches)) { |
90
|
4 |
|
$isMatched = true; |
91
|
4 |
|
$this->numericStatus = \intval($rule->getStatus()); |
92
|
4 |
|
if (\intval($rule->getStatus()) === self::SCANRESULT_CLEAN) { |
93
|
1 |
|
$this->details = ''; |
94
|
|
|
} else { |
95
|
4 |
|
$this->details = isset($matches[1]) ? $matches[1] : 'unknown'; |
96
|
|
|
} |
97
|
5 |
|
break; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
5 |
|
if (!$isMatched) { |
102
|
3 |
|
$this->numericStatus = self::SCANRESULT_UNCHECKED; |
103
|
5 |
|
$this->details = 'No matching rules. Please check antivirus rules.'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} else { // Executable mode |
107
|
4 |
|
$scanStatus = $ruleMapper->findByResult($result); |
108
|
4 |
|
if (\is_array($scanStatus) && \count($scanStatus)) { |
109
|
4 |
|
$this->numericStatus = \intval($scanStatus[0]->getStatus()); |
110
|
4 |
|
$this->details = $scanStatus[0]->getDescription(); |
111
|
|
|
} |
112
|
|
|
|
113
|
4 |
|
switch($this->numericStatus) { |
114
|
4 |
|
case self::SCANRESULT_INFECTED: |
115
|
2 |
|
$report = array(); |
116
|
2 |
|
$rawResponse = \explode("\n", $rawResponse); |
117
|
|
|
|
118
|
2 |
|
foreach ($rawResponse as $line) { |
119
|
2 |
|
if (\preg_match('/.*: (.*) FOUND\s*$/', $line, $matches)) { |
120
|
2 |
|
$report[] = $matches[1]; |
121
|
|
|
} |
122
|
|
|
} |
123
|
2 |
|
$this->details = \implode(', ', $report); |
124
|
|
|
|
125
|
2 |
|
break; |
126
|
4 |
|
case self::SCANRESULT_UNCHECKED: |
127
|
1 |
|
if (!$this->details) { |
128
|
|
|
$this->details = 'No matching rule for exit code ' . $this->numericStatus .'. Please check antivirus rules configuration.' ; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
8 |
|
} |
133
|
|
|
|
134
|
5 |
|
protected function getResponseRules() { |
135
|
5 |
|
$infectedRules = $this->ruleMapper->findAllMatchedByStatus(self::SCANRESULT_INFECTED); |
136
|
5 |
|
$uncheckedRules = $this->ruleMapper->findAllMatchedByStatus(self::SCANRESULT_UNCHECKED); |
137
|
5 |
|
$cleanRules = $this->ruleMapper->findAllMatchedByStatus(self::SCANRESULT_CLEAN); |
138
|
|
|
|
139
|
5 |
|
$infectedRules = $infectedRules ? $infectedRules : []; |
140
|
5 |
|
$uncheckedRules = $uncheckedRules ? $uncheckedRules : []; |
141
|
5 |
|
$cleanRules = $cleanRules ? $cleanRules : []; |
142
|
|
|
|
143
|
|
|
// order: clean, infected, try to guess error |
144
|
5 |
|
$allRules = \array_merge($cleanRules, $infectedRules, $uncheckedRules); |
145
|
5 |
|
return $allRules; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function dispatch($item, $isBackground = false) { |
149
|
|
|
switch($this->getNumericStatus()) { |
150
|
|
|
case self::SCANRESULT_UNCHECKED: |
151
|
|
|
$item->processUnchecked($this, $isBackground); |
152
|
|
|
break; |
153
|
|
|
case self::SCANRESULT_INFECTED: |
154
|
|
|
$item->processInfected($this, $isBackground); |
155
|
|
|
break; |
156
|
|
|
case self::SCANRESULT_CLEAN: |
157
|
|
|
$item->processClean($this, $isBackground); |
158
|
|
|
break; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|