Passed
Push — master ( 615cf6...cbbbbe )
by Morris
13:19 queued 12s
created

MountConfig::registerBackend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
41
namespace OCA\Files_External;
42
43
use OCA\Files_External\AppInfo\Application;
44
use OCA\Files_External\Config\IConfigHandler;
45
use OCA\Files_External\Config\UserContext;
46
use OCA\Files_External\Config\UserPlaceholderHandler;
47
use OCA\Files_External\Lib\Auth\Builtin;
48
use OCA\Files_External\Lib\Backend\Backend;
49
use OCA\Files_External\Lib\Backend\LegacyBackend;
50
use OCA\Files_External\Lib\StorageConfig;
51
use OCA\Files_External\Service\BackendService;
52
use OCA\Files_External\Service\GlobalStoragesService;
53
use OCA\Files_External\Service\UserGlobalStoragesService;
54
use OCA\Files_External\Service\UserStoragesService;
55
use OCP\Files\StorageNotAvailableException;
56
use OCP\IUserManager;
57
use phpseclib\Crypt\AES;
58
59
/**
60
 * Class to configure mount.json globally and for users
61
 */
62
class MountConfig {
63
	// TODO: make this class non-static and give it a proper namespace
64
65
	public const MOUNT_TYPE_GLOBAL = 'global';
66
	public const MOUNT_TYPE_GROUP = 'group';
67
	public const MOUNT_TYPE_USER = 'user';
68
	public const MOUNT_TYPE_PERSONAL = 'personal';
69
70
	// whether to skip backend test (for unit tests, as this static class is not mockable)
71
	public static $skipTest = false;
72
73
	/** @var Application */
74
	public static $app;
75
76
	/**
77
	 * @param string $class
78
	 * @param array $definition
79
	 * @return bool
80
	 * @deprecated 8.2.0 use \OCA\Files_External\Service\BackendService::registerBackend()
81
	 */
82
	public static function registerBackend($class, $definition) {
83
		$backendService = self::$app->getContainer()->query(BackendService::class);
84
		$auth = self::$app->getContainer()->query(Builtin::class);
85
86
		$backendService->registerBackend(new LegacyBackend($class, $definition, $auth));
87
88
		return true;
89
	}
90
91
	/**
92
	 * Returns the mount points for the given user.
93
	 * The mount point is relative to the data directory.
94
	 *
95
	 * @param string $uid user
96
	 * @return array of mount point string as key, mountpoint config as value
97
	 *
98
	 * @deprecated 8.2.0 use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages()
99
	 */
100
	public static function getAbsoluteMountPoints($uid) {
101
		$mountPoints = [];
102
103
		$userGlobalStoragesService = self::$app->getContainer()->query(UserGlobalStoragesService::class);
104
		$userStoragesService = self::$app->getContainer()->query(UserStoragesService::class);
105
		$user = self::$app->getContainer()->query(IUserManager::class)->get($uid);
106
107
		$userGlobalStoragesService->setUser($user);
108
		$userStoragesService->setUser($user);
109
110
		foreach ($userGlobalStoragesService->getStorages() as $storage) {
111
			/** @var \OCA\Files_External\Lib\StorageConfig $storage */
112
			$mountPoint = '/'.$uid.'/files'.$storage->getMountPoint();
113
			$mountEntry = self::prepareMountPointEntry($storage, false);
114
			foreach ($mountEntry['options'] as &$option) {
115
				$option = self::substitutePlaceholdersInConfig($option, $uid);
116
			}
117
			$mountPoints[$mountPoint] = $mountEntry;
118
		}
119
120
		foreach ($userStoragesService->getStorages() as $storage) {
121
			$mountPoint = '/'.$uid.'/files'.$storage->getMountPoint();
122
			$mountEntry = self::prepareMountPointEntry($storage, true);
123
			foreach ($mountEntry['options'] as &$option) {
124
				$option = self::substitutePlaceholdersInConfig($option, $uid);
125
			}
126
			$mountPoints[$mountPoint] = $mountEntry;
127
		}
128
129
		$userGlobalStoragesService->resetUser();
130
		$userStoragesService->resetUser();
131
132
		return $mountPoints;
133
	}
134
135
	/**
136
	 * Get the system mount points
137
	 *
138
	 * @return array
139
	 *
140
	 * @deprecated 8.2.0 use GlobalStoragesService::getStorages()
141
	 */
142
	public static function getSystemMountPoints() {
143
		$mountPoints = [];
144
		$service = self::$app->getContainer()->query(GlobalStoragesService::class);
145
146
		foreach ($service->getStorages() as $storage) {
147
			$mountPoints[] = self::prepareMountPointEntry($storage, false);
148
		}
149
150
		return $mountPoints;
151
	}
152
153
	/**
154
	 * Get the personal mount points of the current user
155
	 *
156
	 * @return array
157
	 *
158
	 * @deprecated 8.2.0 use UserStoragesService::getStorages()
159
	 */
160
	public static function getPersonalMountPoints() {
161
		$mountPoints = [];
162
		$service = self::$app->getContainer()->query(UserStoragesService::class);
163
164
		foreach ($service->getStorages() as $storage) {
165
			$mountPoints[] = self::prepareMountPointEntry($storage, true);
166
		}
167
168
		return $mountPoints;
169
	}
170
171
	/**
172
	 * Convert a StorageConfig to the legacy mountPoints array format
173
	 * There's a lot of extra information in here, to satisfy all of the legacy functions
174
	 *
175
	 * @param StorageConfig $storage
176
	 * @param bool $isPersonal
177
	 * @return array
178
	 */
179
	private static function prepareMountPointEntry(StorageConfig $storage, $isPersonal) {
180
		$mountEntry = [];
181
182
		$mountEntry['mountpoint'] = substr($storage->getMountPoint(), 1); // remove leading slash
183
		$mountEntry['class'] = $storage->getBackend()->getIdentifier();
184
		$mountEntry['backend'] = $storage->getBackend()->getText();
185
		$mountEntry['authMechanism'] = $storage->getAuthMechanism()->getIdentifier();
186
		$mountEntry['personal'] = $isPersonal;
187
		$mountEntry['options'] = self::decryptPasswords($storage->getBackendOptions());
188
		$mountEntry['mountOptions'] = $storage->getMountOptions();
189
		$mountEntry['priority'] = $storage->getPriority();
190
		$mountEntry['applicable'] = [
191
			'groups' => $storage->getApplicableGroups(),
192
			'users' => $storage->getApplicableUsers(),
193
		];
194
		// if mountpoint is applicable to all users the old API expects ['all']
195
		if (empty($mountEntry['applicable']['groups']) && empty($mountEntry['applicable']['users'])) {
196
			$mountEntry['applicable']['users'] = ['all'];
197
		}
198
199
		$mountEntry['id'] = $storage->getId();
200
201
		return $mountEntry;
202
	}
203
204
	/**
205
	 * fill in the correct values for $user
206
	 *
207
	 * @param string $user user value
208
	 * @param string|array $input
209
	 * @return string
210
	 * @deprecated use self::substitutePlaceholdersInConfig($input)
211
	 */
212
	public static function setUserVars($user, $input) {
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

212
	public static function setUserVars(/** @scrutinizer ignore-unused */ $user, $input) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
213
		$handler = self::$app->getContainer()->query(UserPlaceholderHandler::class);
214
		return $handler->handle($input);
215
	}
216
217
	/**
218
	 * @param mixed $input
219
	 * @param string|null $userId
220
	 * @return mixed
221
	 * @throws \OCP\AppFramework\QueryException
222
	 * @since 16.0.0
223
	 */
224
	public static function substitutePlaceholdersInConfig($input, string $userId = null) {
225
		/** @var BackendService $backendService */
226
		$backendService = self::$app->getContainer()->query(BackendService::class);
227
		/** @var IConfigHandler[] $handlers */
228
		$handlers = $backendService->getConfigHandlers();
229
		foreach ($handlers as $handler) {
230
			if ($handler instanceof UserContext && $userId !== null) {
231
				$handler->setUserId($userId);
232
			}
233
			$input = $handler->handle($input);
234
		}
235
		return $input;
236
	}
237
238
	/**
239
	 * Test connecting using the given backend configuration
240
	 *
241
	 * @param string $class backend class name
242
	 * @param array $options backend configuration options
243
	 * @param boolean $isPersonal
244
	 * @return int see self::STATUS_*
245
	 * @throws Exception
246
	 */
247
	public static function getBackendStatus($class, $options, $isPersonal, $testOnly = true) {
248
		if (self::$skipTest) {
249
			return StorageNotAvailableException::STATUS_SUCCESS;
250
		}
251
		foreach ($options as $key => &$option) {
252
			if ($key === 'password') {
253
				// no replacements in passwords
254
				continue;
255
			}
256
			$option = self::substitutePlaceholdersInConfig($option);
257
		}
258
		if (class_exists($class)) {
259
			try {
260
				/** @var \OC\Files\Storage\Common $storage */
261
				$storage = new $class($options);
262
263
				try {
264
					$result = $storage->test($isPersonal, $testOnly);
0 ignored issues
show
Unused Code introduced by
The call to OC\Files\Storage\Common::test() has too many arguments starting with $isPersonal. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

264
					/** @scrutinizer ignore-call */ 
265
     $result = $storage->test($isPersonal, $testOnly);

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.

Loading history...
265
					$storage->setAvailability($result);
266
					if ($result) {
267
						return StorageNotAvailableException::STATUS_SUCCESS;
268
					}
269
				} catch (\Exception $e) {
270
					$storage->setAvailability(false);
271
					throw $e;
272
				}
273
			} catch (Exception $exception) {
0 ignored issues
show
Bug introduced by
The type OCA\Files_External\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
274
				\OC::$server->getLogger()->logException($exception, ['app' => 'files_external']);
275
				throw $exception;
276
			}
277
		}
278
		return StorageNotAvailableException::STATUS_ERROR;
279
	}
280
281
	/**
282
	 * Read the mount points in the config file into an array
283
	 *
284
	 * @param string|null $user If not null, personal for $user, otherwise system
285
	 * @return array
286
	 */
287
	public static function readData($user = null) {
288
		if (isset($user)) {
289
			$jsonFile = \OC::$server->getUserManager()->get($user)->getHome() . '/mount.json';
290
		} else {
291
			$config = \OC::$server->getConfig();
292
			$datadir = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/');
293
			$jsonFile = $config->getSystemValue('mount_file', $datadir . '/mount.json');
294
		}
295
		if (is_file($jsonFile)) {
296
			$mountPoints = json_decode(file_get_contents($jsonFile), true);
297
			if (is_array($mountPoints)) {
298
				return $mountPoints;
299
			}
300
		}
301
		return [];
302
	}
303
304
	/**
305
	 * Get backend dependency message
306
	 * TODO: move into AppFramework along with templates
307
	 *
308
	 * @param Backend[] $backends
309
	 * @return string
310
	 */
311
	public static function dependencyMessage($backends) {
312
		$l = \OC::$server->getL10N('files_external');
313
		$message = '';
314
		$dependencyGroups = [];
315
316
		foreach ($backends as $backend) {
317
			foreach ($backend->checkDependencies() as $dependency) {
318
				if ($message = $dependency->getMessage()) {
319
					$message .= '<p>' . $message . '</p>';
320
				} else {
321
					$dependencyGroups[$dependency->getDependency()][] = $backend;
322
				}
323
			}
324
		}
325
326
		foreach ($dependencyGroups as $module => $dependants) {
327
			$backends = implode(', ', array_map(function ($backend) {
328
				return '"' . $backend->getText() . '"';
329
			}, $dependants));
330
			$message .= '<p>' . MountConfig::getSingleDependencyMessage($l, $module, $backends) . '</p>';
331
		}
332
333
		return $message;
334
	}
335
336
	/**
337
	 * Returns a dependency missing message
338
	 *
339
	 * @param \OCP\IL10N $l
340
	 * @param string $module
341
	 * @param string $backend
342
	 * @return string
343
	 */
344
	private static function getSingleDependencyMessage(\OCP\IL10N $l, $module, $backend) {
345
		switch (strtolower($module)) {
346
			case 'curl':
347
				return (string)$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]);
348
			case 'ftp':
349
				return (string)$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]);
350
			default:
351
				return (string)$l->t('"%1$s" is not installed. Mounting of %2$s is not possible. Please ask your system administrator to install it.', [$module, $backend]);
352
		}
353
	}
354
355
	/**
356
	 * Encrypt passwords in the given config options
357
	 *
358
	 * @param array $options mount options
359
	 * @return array updated options
360
	 */
361
	public static function encryptPasswords($options) {
362
		if (isset($options['password'])) {
363
			$options['password_encrypted'] = self::encryptPassword($options['password']);
364
			// do not unset the password, we want to keep the keys order
365
			// on load... because that's how the UI currently works
366
			$options['password'] = '';
367
		}
368
		return $options;
369
	}
370
371
	/**
372
	 * Decrypt passwords in the given config options
373
	 *
374
	 * @param array $options mount options
375
	 * @return array updated options
376
	 */
377
	public static function decryptPasswords($options) {
378
		// note: legacy options might still have the unencrypted password in the "password" field
379
		if (isset($options['password_encrypted'])) {
380
			$options['password'] = self::decryptPassword($options['password_encrypted']);
381
			unset($options['password_encrypted']);
382
		}
383
		return $options;
384
	}
385
386
	/**
387
	 * Encrypt a single password
388
	 *
389
	 * @param string $password plain text password
390
	 * @return string encrypted password
391
	 */
392
	private static function encryptPassword($password) {
393
		$cipher = self::getCipher();
394
		$iv = \OC::$server->getSecureRandom()->generate(16);
395
		$cipher->setIV($iv);
396
		return base64_encode($iv . $cipher->encrypt($password));
397
	}
398
399
	/**
400
	 * Decrypts a single password
401
	 *
402
	 * @param string $encryptedPassword encrypted password
403
	 * @return string plain text password
404
	 */
405
	private static function decryptPassword($encryptedPassword) {
406
		$cipher = self::getCipher();
407
		$binaryPassword = base64_decode($encryptedPassword);
408
		$iv = substr($binaryPassword, 0, 16);
409
		$cipher->setIV($iv);
410
		$binaryPassword = substr($binaryPassword, 16);
411
		return $cipher->decrypt($binaryPassword);
412
	}
413
414
	/**
415
	 * Returns the encryption cipher
416
	 *
417
	 * @return AES
418
	 */
419
	private static function getCipher() {
420
		$cipher = new AES(AES::MODE_CBC);
421
		$cipher->setKey(\OC::$server->getConfig()->getSystemValue('passwordsalt', null));
422
		return $cipher;
423
	}
424
425
	/**
426
	 * Computes a hash based on the given configuration.
427
	 * This is mostly used to find out whether configurations
428
	 * are the same.
429
	 *
430
	 * @param array $config
431
	 * @return string
432
	 */
433
	public static function makeConfigHash($config) {
434
		$data = json_encode(
435
			[
436
				'c' => $config['backend'],
437
				'a' => $config['authMechanism'],
438
				'm' => $config['mountpoint'],
439
				'o' => $config['options'],
440
				'p' => isset($config['priority']) ? $config['priority'] : -1,
441
				'mo' => isset($config['mountOptions']) ? $config['mountOptions'] : [],
442
			]
443
		);
444
		return hash('md5', $data);
445
	}
446
}
447