1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
4
|
|
|
* |
5
|
|
|
* @author Andreas Fischer <[email protected]> |
6
|
|
|
* @author Arthur Schiwon <[email protected]> |
7
|
|
|
* @author Bart Visscher <[email protected]> |
8
|
|
|
* @author Björn Schießle <[email protected]> |
9
|
|
|
* @author Christoph Wurst <[email protected]> |
10
|
|
|
* @author Frank Karlitschek <[email protected]> |
11
|
|
|
* @author Jesús Macias <[email protected]> |
12
|
|
|
* @author Joas Schilling <[email protected]> |
13
|
|
|
* @author Juan Pablo Villafáñez <[email protected]> |
14
|
|
|
* @author Julius Härtl <[email protected]> |
15
|
|
|
* @author Lukas Reschke <[email protected]> |
16
|
|
|
* @author Michael Gapczynski <[email protected]> |
17
|
|
|
* @author Morris Jobke <[email protected]> |
18
|
|
|
* @author Philipp Kapfer <[email protected]> |
19
|
|
|
* @author Robin Appelman <[email protected]> |
20
|
|
|
* @author Robin McCorkell <[email protected]> |
21
|
|
|
* @author Roeland Jago Douma <[email protected]> |
22
|
|
|
* @author Thomas Müller <[email protected]> |
23
|
|
|
* @author Vincent Petry <[email protected]> |
24
|
|
|
* |
25
|
|
|
* @license AGPL-3.0 |
26
|
|
|
* |
27
|
|
|
* This code is free software: you can redistribute it and/or modify |
28
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
29
|
|
|
* as published by the Free Software Foundation. |
30
|
|
|
* |
31
|
|
|
* This program is distributed in the hope that it will be useful, |
32
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
33
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
34
|
|
|
* GNU Affero General Public License for more details. |
35
|
|
|
* |
36
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
37
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
|
|
namespace OCA\Files_External; |
41
|
|
|
|
42
|
|
|
use OCA\Files_External\Config\IConfigHandler; |
43
|
|
|
use OCA\Files_External\Config\UserContext; |
44
|
|
|
use OCA\Files_External\Lib\Backend\Backend; |
45
|
|
|
use OCA\Files_External\Service\BackendService; |
46
|
|
|
use OCA\Files_External\Service\GlobalStoragesService; |
47
|
|
|
use OCA\Files_External\Service\UserGlobalStoragesService; |
48
|
|
|
use OCA\Files_External\Service\UserStoragesService; |
49
|
|
|
use OCP\Files\StorageNotAvailableException; |
50
|
|
|
use phpseclib\Crypt\AES; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Class to configure mount.json globally and for users |
54
|
|
|
*/ |
55
|
|
|
class MountConfig { |
56
|
|
|
// TODO: make this class non-static and give it a proper namespace |
57
|
|
|
|
58
|
|
|
public const MOUNT_TYPE_GLOBAL = 'global'; |
59
|
|
|
public const MOUNT_TYPE_GROUP = 'group'; |
60
|
|
|
public const MOUNT_TYPE_USER = 'user'; |
61
|
|
|
public const MOUNT_TYPE_PERSONAL = 'personal'; |
62
|
|
|
|
63
|
|
|
// whether to skip backend test (for unit tests, as this static class is not mockable) |
64
|
|
|
public static $skipTest = false; |
65
|
|
|
|
66
|
|
|
/** @var UserGlobalStoragesService */ |
67
|
|
|
private $userGlobalStorageService; |
68
|
|
|
/** @var UserStoragesService */ |
69
|
|
|
private $userStorageService; |
70
|
|
|
/** @var GlobalStoragesService */ |
71
|
|
|
private $globalStorageService; |
72
|
|
|
|
73
|
|
|
public function __construct( |
74
|
|
|
UserGlobalStoragesService $userGlobalStorageService, |
75
|
|
|
UserStoragesService $userStorageService, |
76
|
|
|
GlobalStoragesService $globalStorageService |
77
|
|
|
) { |
78
|
|
|
$this->userGlobalStorageService = $userGlobalStorageService; |
79
|
|
|
$this->userStorageService = $userStorageService; |
80
|
|
|
$this->globalStorageService = $globalStorageService; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param mixed $input |
85
|
|
|
* @param string|null $userId |
86
|
|
|
* @return mixed |
87
|
|
|
* @throws \OCP\AppFramework\QueryException |
88
|
|
|
* @since 16.0.0 |
89
|
|
|
*/ |
90
|
|
|
public static function substitutePlaceholdersInConfig($input, string $userId = null) { |
91
|
|
|
/** @var BackendService $backendService */ |
92
|
|
|
$backendService = \OC::$server->get(BackendService::class); |
93
|
|
|
/** @var IConfigHandler[] $handlers */ |
94
|
|
|
$handlers = $backendService->getConfigHandlers(); |
95
|
|
|
foreach ($handlers as $handler) { |
96
|
|
|
if ($handler instanceof UserContext && $userId !== null) { |
97
|
|
|
$handler->setUserId($userId); |
98
|
|
|
} |
99
|
|
|
$input = $handler->handle($input); |
100
|
|
|
} |
101
|
|
|
return $input; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Test connecting using the given backend configuration |
106
|
|
|
* |
107
|
|
|
* @param string $class backend class name |
108
|
|
|
* @param array $options backend configuration options |
109
|
|
|
* @param boolean $isPersonal |
110
|
|
|
* @return int see self::STATUS_* |
111
|
|
|
* @throws \Exception |
112
|
|
|
*/ |
113
|
|
|
public static function getBackendStatus($class, $options, $isPersonal, $testOnly = true) { |
114
|
|
|
if (self::$skipTest) { |
115
|
|
|
return StorageNotAvailableException::STATUS_SUCCESS; |
116
|
|
|
} |
117
|
|
|
foreach ($options as $key => &$option) { |
118
|
|
|
if ($key === 'password') { |
119
|
|
|
// no replacements in passwords |
120
|
|
|
continue; |
121
|
|
|
} |
122
|
|
|
$option = self::substitutePlaceholdersInConfig($option); |
123
|
|
|
} |
124
|
|
|
if (class_exists($class)) { |
125
|
|
|
try { |
126
|
|
|
/** @var \OC\Files\Storage\Common $storage */ |
127
|
|
|
$storage = new $class($options); |
128
|
|
|
|
129
|
|
|
try { |
130
|
|
|
$result = $storage->test($isPersonal, $testOnly); |
|
|
|
|
131
|
|
|
$storage->setAvailability($result); |
132
|
|
|
if ($result) { |
133
|
|
|
return StorageNotAvailableException::STATUS_SUCCESS; |
134
|
|
|
} |
135
|
|
|
} catch (\Exception $e) { |
136
|
|
|
$storage->setAvailability(false); |
137
|
|
|
throw $e; |
138
|
|
|
} |
139
|
|
|
} catch (\Exception $exception) { |
140
|
|
|
\OC::$server->getLogger()->logException($exception, ['app' => 'files_external']); |
141
|
|
|
throw $exception; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
return StorageNotAvailableException::STATUS_ERROR; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Read the mount points in the config file into an array |
149
|
|
|
* |
150
|
|
|
* @param string|null $user If not null, personal for $user, otherwise system |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public static function readData($user = null) { |
154
|
|
|
if (isset($user)) { |
155
|
|
|
$jsonFile = \OC::$server->getUserManager()->get($user)->getHome() . '/mount.json'; |
156
|
|
|
} else { |
157
|
|
|
$config = \OC::$server->getConfig(); |
158
|
|
|
$datadir = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/'); |
159
|
|
|
$jsonFile = $config->getSystemValue('mount_file', $datadir . '/mount.json'); |
160
|
|
|
} |
161
|
|
|
if (is_file($jsonFile)) { |
162
|
|
|
$mountPoints = json_decode(file_get_contents($jsonFile), true); |
163
|
|
|
if (is_array($mountPoints)) { |
164
|
|
|
return $mountPoints; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
return []; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get backend dependency message |
172
|
|
|
* TODO: move into AppFramework along with templates |
173
|
|
|
* |
174
|
|
|
* @param Backend[] $backends |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public static function dependencyMessage($backends) { |
178
|
|
|
$l = \OC::$server->getL10N('files_external'); |
179
|
|
|
$message = ''; |
180
|
|
|
$dependencyGroups = []; |
181
|
|
|
|
182
|
|
|
foreach ($backends as $backend) { |
183
|
|
|
foreach ($backend->checkDependencies() as $dependency) { |
184
|
|
|
if ($message = $dependency->getMessage()) { |
185
|
|
|
$message .= '<p>' . $message . '</p>'; |
186
|
|
|
} else { |
187
|
|
|
$dependencyGroups[$dependency->getDependency()][] = $backend; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
foreach ($dependencyGroups as $module => $dependants) { |
193
|
|
|
$backends = implode(', ', array_map(function ($backend) { |
194
|
|
|
return '"' . $backend->getText() . '"'; |
195
|
|
|
}, $dependants)); |
196
|
|
|
$message .= '<p>' . MountConfig::getSingleDependencyMessage($l, $module, $backends) . '</p>'; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $message; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Returns a dependency missing message |
204
|
|
|
* |
205
|
|
|
* @param \OCP\IL10N $l |
206
|
|
|
* @param string $module |
207
|
|
|
* @param string $backend |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
private static function getSingleDependencyMessage(\OCP\IL10N $l, $module, $backend) { |
211
|
|
|
switch (strtolower($module)) { |
212
|
|
|
case 'curl': |
213
|
|
|
return $l->t('The cURL support in PHP is not enabled or installed. Mounting of %s is not possible. Please ask your system administrator to install it.', [$backend]); |
214
|
|
|
case 'ftp': |
215
|
|
|
return $l->t('The FTP support in PHP is not enabled or installed. Mounting of %s is not possible. Please ask your system administrator to install it.', [$backend]); |
216
|
|
|
default: |
217
|
|
|
return $l->t('"%1$s" is not installed. Mounting of %2$s is not possible. Please ask your system administrator to install it.', [$module, $backend]); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Encrypt passwords in the given config options |
223
|
|
|
* |
224
|
|
|
* @param array $options mount options |
225
|
|
|
* @return array updated options |
226
|
|
|
*/ |
227
|
|
|
public static function encryptPasswords($options) { |
228
|
|
|
if (isset($options['password'])) { |
229
|
|
|
$options['password_encrypted'] = self::encryptPassword($options['password']); |
230
|
|
|
// do not unset the password, we want to keep the keys order |
231
|
|
|
// on load... because that's how the UI currently works |
232
|
|
|
$options['password'] = ''; |
233
|
|
|
} |
234
|
|
|
return $options; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Decrypt passwords in the given config options |
239
|
|
|
* |
240
|
|
|
* @param array $options mount options |
241
|
|
|
* @return array updated options |
242
|
|
|
*/ |
243
|
|
|
public static function decryptPasswords($options) { |
244
|
|
|
// note: legacy options might still have the unencrypted password in the "password" field |
245
|
|
|
if (isset($options['password_encrypted'])) { |
246
|
|
|
$options['password'] = self::decryptPassword($options['password_encrypted']); |
247
|
|
|
unset($options['password_encrypted']); |
248
|
|
|
} |
249
|
|
|
return $options; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Encrypt a single password |
254
|
|
|
* |
255
|
|
|
* @param string $password plain text password |
256
|
|
|
* @return string encrypted password |
257
|
|
|
*/ |
258
|
|
|
private static function encryptPassword($password) { |
259
|
|
|
$cipher = self::getCipher(); |
260
|
|
|
$iv = \OC::$server->getSecureRandom()->generate(16); |
261
|
|
|
$cipher->setIV($iv); |
262
|
|
|
return base64_encode($iv . $cipher->encrypt($password)); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Decrypts a single password |
267
|
|
|
* |
268
|
|
|
* @param string $encryptedPassword encrypted password |
269
|
|
|
* @return string plain text password |
270
|
|
|
*/ |
271
|
|
|
private static function decryptPassword($encryptedPassword) { |
272
|
|
|
$cipher = self::getCipher(); |
273
|
|
|
$binaryPassword = base64_decode($encryptedPassword); |
274
|
|
|
$iv = substr($binaryPassword, 0, 16); |
275
|
|
|
$cipher->setIV($iv); |
276
|
|
|
$binaryPassword = substr($binaryPassword, 16); |
277
|
|
|
return $cipher->decrypt($binaryPassword); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Returns the encryption cipher |
282
|
|
|
* |
283
|
|
|
* @return AES |
284
|
|
|
*/ |
285
|
|
|
private static function getCipher() { |
286
|
|
|
$cipher = new AES(AES::MODE_CBC); |
287
|
|
|
$cipher->setKey(\OC::$server->getConfig()->getSystemValue('passwordsalt', null)); |
288
|
|
|
return $cipher; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Computes a hash based on the given configuration. |
293
|
|
|
* This is mostly used to find out whether configurations |
294
|
|
|
* are the same. |
295
|
|
|
* |
296
|
|
|
* @param array $config |
297
|
|
|
* @return string |
298
|
|
|
*/ |
299
|
|
|
public static function makeConfigHash($config) { |
300
|
|
|
$data = json_encode( |
301
|
|
|
[ |
302
|
|
|
'c' => $config['backend'], |
303
|
|
|
'a' => $config['authMechanism'], |
304
|
|
|
'm' => $config['mountpoint'], |
305
|
|
|
'o' => $config['options'], |
306
|
|
|
'p' => isset($config['priority']) ? $config['priority'] : -1, |
307
|
|
|
'mo' => isset($config['mountOptions']) ? $config['mountOptions'] : [], |
308
|
|
|
] |
309
|
|
|
); |
310
|
|
|
return hash('md5', $data); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.