2015-01-14 22:39:23 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
2015-04-07 18:02:49 +03:00
|
|
|
* @license AGPL-3.0
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
2015-04-07 18:02:49 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
2015-04-07 18:02:49 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2015-01-14 22:39:23 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-04-07 18:02:49 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Encryption\Keys;
|
|
|
|
|
|
|
|
use OC\Encryption\Util;
|
2015-04-22 12:18:18 +03:00
|
|
|
use OC\Files\Filesystem;
|
2015-01-14 22:39:23 +03:00
|
|
|
use OC\Files\View;
|
2020-06-22 16:35:52 +03:00
|
|
|
use OC\ServerNotAvailableException;
|
2017-01-18 19:42:07 +03:00
|
|
|
use OC\User\NoUserException;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\Encryption\Keys\IStorage;
|
2020-06-22 16:35:52 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\Security\ICrypto;
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2015-04-22 12:18:18 +03:00
|
|
|
class Storage implements IStorage {
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2015-07-24 13:24:18 +03:00
|
|
|
// hidden file which indicate that the folder is a valid key storage
|
2020-04-10 17:54:27 +03:00
|
|
|
public const KEY_STORAGE_MARKER = '.oc_key_storage';
|
2015-07-24 13:24:18 +03:00
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
/** @var View */
|
|
|
|
private $view;
|
|
|
|
|
|
|
|
/** @var Util */
|
|
|
|
private $util;
|
|
|
|
|
|
|
|
// base dir where all the file related keys are stored
|
2015-04-22 14:12:52 +03:00
|
|
|
/** @var string */
|
2015-01-14 22:39:23 +03:00
|
|
|
private $keys_base_dir;
|
2015-04-22 14:12:52 +03:00
|
|
|
|
2015-07-24 13:24:18 +03:00
|
|
|
// root of the key storage default is empty which means that we use the data folder
|
|
|
|
/** @var string */
|
|
|
|
private $root_dir;
|
|
|
|
|
2015-04-22 14:12:52 +03:00
|
|
|
/** @var string */
|
2015-01-14 22:39:23 +03:00
|
|
|
private $encryption_base_dir;
|
|
|
|
|
2017-01-02 23:24:37 +03:00
|
|
|
/** @var string */
|
|
|
|
private $backup_base_dir;
|
|
|
|
|
2015-04-22 14:12:52 +03:00
|
|
|
/** @var array */
|
|
|
|
private $keyCache = [];
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2020-06-22 16:35:52 +03:00
|
|
|
/** @var ICrypto */
|
|
|
|
private $crypto;
|
|
|
|
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
/**
|
|
|
|
* @param View $view
|
|
|
|
* @param Util $util
|
|
|
|
*/
|
2020-06-22 16:35:52 +03:00
|
|
|
public function __construct(View $view, Util $util, ICrypto $crypto, IConfig $config) {
|
2015-01-14 22:39:23 +03:00
|
|
|
$this->view = $view;
|
|
|
|
$this->util = $util;
|
|
|
|
|
|
|
|
$this->encryption_base_dir = '/files_encryption';
|
|
|
|
$this->keys_base_dir = $this->encryption_base_dir .'/keys';
|
2017-01-02 23:24:37 +03:00
|
|
|
$this->backup_base_dir = $this->encryption_base_dir .'/backup';
|
2015-07-24 13:24:18 +03:00
|
|
|
$this->root_dir = $this->util->getKeyStorageRoot();
|
2020-06-22 16:35:52 +03:00
|
|
|
$this->crypto = $crypto;
|
|
|
|
$this->config = $config;
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function getUserKey($uid, $keyId, $encryptionModuleId) {
|
|
|
|
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
|
2020-06-22 16:35:52 +03:00
|
|
|
return base64_decode($this->getKeyWithUid($path, $uid));
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function getFileKey($path, $keyId, $encryptionModuleId) {
|
2015-05-22 13:12:31 +03:00
|
|
|
$realFile = $this->util->stripPartialFileExtension($path);
|
|
|
|
$keyDir = $this->getFileKeyDir($encryptionModuleId, $realFile);
|
2020-06-22 16:35:52 +03:00
|
|
|
$key = $this->getKey($keyDir . $keyId)['key'];
|
2015-06-08 16:36:54 +03:00
|
|
|
|
|
|
|
if ($key === '' && $realFile !== $path) {
|
|
|
|
// Check if the part file has keys and use them, if no normal keys
|
|
|
|
// exist. This is required to fix copyBetweenStorage() when we
|
|
|
|
// rename a .part file over storage borders.
|
|
|
|
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
|
2020-06-22 16:35:52 +03:00
|
|
|
$key = $this->getKey($keyDir . $keyId)['key'];
|
2015-06-08 16:36:54 +03:00
|
|
|
}
|
|
|
|
|
2020-06-22 16:35:52 +03:00
|
|
|
return base64_decode($key);
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function getSystemUserKey($keyId, $encryptionModuleId) {
|
|
|
|
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
|
2020-06-22 16:35:52 +03:00
|
|
|
return base64_decode($this->getKeyWithUid($path, null));
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function setUserKey($uid, $keyId, $key, $encryptionModuleId) {
|
|
|
|
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
|
2020-06-22 16:35:52 +03:00
|
|
|
return $this->setKey($path, [
|
|
|
|
'key' => base64_encode($key),
|
|
|
|
'uid' => $uid,
|
|
|
|
]);
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function setFileKey($path, $keyId, $key, $encryptionModuleId) {
|
|
|
|
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
|
2020-06-22 16:35:52 +03:00
|
|
|
return $this->setKey($keyDir . $keyId, [
|
|
|
|
'key' => base64_encode($key),
|
|
|
|
]);
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function setSystemUserKey($keyId, $key, $encryptionModuleId) {
|
|
|
|
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
|
2020-06-22 16:35:52 +03:00
|
|
|
return $this->setKey($path, [
|
|
|
|
'key' => base64_encode($key),
|
|
|
|
'uid' => null,
|
|
|
|
]);
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:24:44 +03:00
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-03-20 18:24:44 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function deleteUserKey($uid, $keyId, $encryptionModuleId) {
|
2017-01-18 19:42:07 +03:00
|
|
|
try {
|
|
|
|
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
|
|
|
|
return !$this->view->file_exists($path) || $this->view->unlink($path);
|
|
|
|
} catch (NoUserException $e) {
|
|
|
|
// this exception can come from initMountPoints() from setupUserMounts()
|
|
|
|
// for a deleted user.
|
|
|
|
//
|
|
|
|
// It means, that:
|
|
|
|
// - we are not running in alternative storage mode because we don't call
|
|
|
|
// initMountPoints() in that mode
|
|
|
|
// - the keys were in the user's home but since the user was deleted, the
|
|
|
|
// user's home is gone and so are the keys
|
|
|
|
//
|
|
|
|
// So there is nothing to do, just ignore.
|
|
|
|
}
|
2015-03-20 18:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-03-20 18:24:44 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function deleteFileKey($path, $keyId, $encryptionModuleId) {
|
|
|
|
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
|
2015-04-09 11:28:02 +03:00
|
|
|
return !$this->view->file_exists($keyDir . $keyId) || $this->view->unlink($keyDir . $keyId);
|
2015-03-20 18:24:44 +03:00
|
|
|
}
|
|
|
|
|
2015-03-26 11:24:28 +03:00
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-03-26 11:24:28 +03:00
|
|
|
*/
|
2015-05-13 15:39:27 +03:00
|
|
|
public function deleteAllFileKeys($path) {
|
|
|
|
$keyDir = $this->getFileKeyDir('', $path);
|
|
|
|
return !$this->view->file_exists($keyDir) || $this->view->deleteAll($keyDir);
|
2015-03-26 11:24:28 +03:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:24:44 +03:00
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-03-20 18:24:44 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function deleteSystemUserKey($keyId, $encryptionModuleId) {
|
|
|
|
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
|
2015-04-09 11:28:02 +03:00
|
|
|
return !$this->view->file_exists($path) || $this->view->unlink($path);
|
2015-03-20 18:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* construct path to users key
|
|
|
|
*
|
2015-04-22 14:12:52 +03:00
|
|
|
* @param string $encryptionModuleId
|
2015-03-20 18:24:44 +03:00
|
|
|
* @param string $keyId
|
|
|
|
* @param string $uid
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
protected function constructUserKeyPath($encryptionModuleId, $keyId, $uid) {
|
2015-03-20 18:24:44 +03:00
|
|
|
if ($uid === null) {
|
2015-07-24 13:24:18 +03:00
|
|
|
$path = $this->root_dir . '/' . $this->encryption_base_dir . '/' . $encryptionModuleId . '/' . $keyId;
|
2015-03-20 18:24:44 +03:00
|
|
|
} else {
|
2015-07-24 13:24:18 +03:00
|
|
|
$path = $this->root_dir . '/' . $uid . $this->encryption_base_dir . '/'
|
2015-04-22 12:18:18 +03:00
|
|
|
. $encryptionModuleId . '/' . $uid . '.' . $keyId;
|
2015-03-20 18:24:44 +03:00
|
|
|
}
|
|
|
|
|
2015-07-24 13:24:18 +03:00
|
|
|
return \OC\Files\Filesystem::normalizePath($path);
|
2015-03-20 18:24:44 +03:00
|
|
|
}
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2020-06-22 16:35:52 +03:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string|null $uid
|
|
|
|
* @return string
|
|
|
|
* @throws ServerNotAvailableException
|
|
|
|
*
|
|
|
|
* Small helper function to fetch the key and verify the value for user and system keys
|
|
|
|
*/
|
|
|
|
private function getKeyWithUid(string $path, ?string $uid): string {
|
|
|
|
$data = $this->getKey($path);
|
|
|
|
|
|
|
|
if (!isset($data['key'])) {
|
|
|
|
throw new ServerNotAvailableException('Key is invalid');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($data['key'] === '') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists('uid', $data) || $data['uid'] !== $uid) {
|
|
|
|
// If the migration is done we error out
|
|
|
|
$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
|
|
|
|
if (version_compare($versionFromBeforeUpdate, '20.0.0.1', '<=')) {
|
|
|
|
return $data['key'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->config->getSystemValueBool('encryption.key_storage_migrated', true)) {
|
|
|
|
throw new ServerNotAvailableException('Key has been modified');
|
|
|
|
} else {
|
|
|
|
//Otherwise we migrate
|
|
|
|
$data['uid'] = $uid;
|
|
|
|
$this->setKey($path, $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data['key'];
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
/**
|
|
|
|
* read key from hard disk
|
|
|
|
*
|
|
|
|
* @param string $path to key
|
2020-06-22 16:35:52 +03:00
|
|
|
* @return array containing key as base64encoded key, and possible the uid
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2020-06-22 16:35:52 +03:00
|
|
|
private function getKey($path): array {
|
|
|
|
$key = [
|
|
|
|
'key' => '',
|
|
|
|
];
|
2015-01-14 22:39:23 +03:00
|
|
|
|
|
|
|
if ($this->view->file_exists($path)) {
|
|
|
|
if (isset($this->keyCache[$path])) {
|
2020-10-05 16:12:57 +03:00
|
|
|
$key = $this->keyCache[$path];
|
2015-01-14 22:39:23 +03:00
|
|
|
} else {
|
2020-06-22 16:35:52 +03:00
|
|
|
$data = $this->view->file_get_contents($path);
|
|
|
|
|
|
|
|
// Version <20.0.0.1 doesn't have this
|
|
|
|
$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
|
|
|
|
if (version_compare($versionFromBeforeUpdate, '20.0.0.1', '<=')) {
|
|
|
|
$key = [
|
|
|
|
'key' => base64_encode($data),
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
if ($this->config->getSystemValueBool('encryption.key_storage_migrated', true)) {
|
|
|
|
try {
|
|
|
|
$clearData = $this->crypto->decrypt($data);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new ServerNotAvailableException('Could not decrypt key', 0, $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
$dataArray = json_decode($clearData, true);
|
|
|
|
if ($dataArray === null) {
|
|
|
|
throw new ServerNotAvailableException('Invalid encryption key');
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = $dataArray;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Even if not all keys are migrated we should still try to decrypt it (in case some have moved).
|
|
|
|
* However it is only a failure now if it is an array and decryption fails
|
|
|
|
*/
|
|
|
|
$fallback = false;
|
|
|
|
try {
|
|
|
|
$clearData = $this->crypto->decrypt($data);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$fallback = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$fallback) {
|
|
|
|
$dataArray = json_decode($clearData, true);
|
|
|
|
if ($dataArray === null) {
|
|
|
|
throw new ServerNotAvailableException('Invalid encryption key');
|
|
|
|
}
|
|
|
|
$key = $dataArray;
|
|
|
|
} else {
|
|
|
|
$key = [
|
|
|
|
'key' => base64_encode($data),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
$this->keyCache[$path] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* write key to disk
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param string $path path to key directory
|
2020-06-22 16:35:52 +03:00
|
|
|
* @param array $key key
|
2015-01-14 22:39:23 +03:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function setKey($path, $key) {
|
|
|
|
$this->keySetPreparation(dirname($path));
|
|
|
|
|
2020-06-22 16:35:52 +03:00
|
|
|
$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
|
|
|
|
if (version_compare($versionFromBeforeUpdate, '20.0.0.1', '<=')) {
|
|
|
|
// Only store old format if this happens during the migration.
|
|
|
|
// TODO: Remove for 21
|
|
|
|
$data = base64_decode($key['key']);
|
|
|
|
} else {
|
|
|
|
// Wrap the data
|
|
|
|
$data = $this->crypto->encrypt(json_encode($key));
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->view->file_put_contents($path, $data);
|
2015-01-14 22:39:23 +03:00
|
|
|
|
|
|
|
if (is_int($result) && $result > 0) {
|
|
|
|
$this->keyCache[$path] = $key;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get path to key folder for a given file
|
|
|
|
*
|
2015-04-22 14:12:52 +03:00
|
|
|
* @param string $encryptionModuleId
|
2015-01-14 22:39:23 +03:00
|
|
|
* @param string $path path to the file, relative to data/
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
private function getFileKeyDir($encryptionModuleId, $path) {
|
2015-01-14 22:39:23 +03:00
|
|
|
list($owner, $filename) = $this->util->getUidAndFilename($path);
|
|
|
|
|
|
|
|
// in case of system wide mount points the keys are stored directly in the data directory
|
2015-04-15 14:19:17 +03:00
|
|
|
if ($this->util->isSystemWideMountPoint($filename, $owner)) {
|
2015-07-24 13:24:18 +03:00
|
|
|
$keyPath = $this->root_dir . '/' . $this->keys_base_dir . $filename . '/';
|
2015-01-14 22:39:23 +03:00
|
|
|
} else {
|
2015-07-24 13:24:18 +03:00
|
|
|
$keyPath = $this->root_dir . '/' . $owner . $this->keys_base_dir . $filename . '/';
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
2015-04-22 12:18:18 +03:00
|
|
|
return Filesystem::normalizePath($keyPath . $encryptionModuleId . '/', false);
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
2015-03-26 11:24:28 +03:00
|
|
|
/**
|
|
|
|
* move keys if a file was renamed
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @param string $target
|
2015-04-24 11:16:06 +03:00
|
|
|
* @return boolean
|
2015-03-26 11:24:28 +03:00
|
|
|
*/
|
2015-04-02 17:25:01 +03:00
|
|
|
public function renameKeys($source, $target) {
|
2015-06-03 12:12:44 +03:00
|
|
|
$sourcePath = $this->getPathToKeys($source);
|
|
|
|
$targetPath = $this->getPathToKeys($target);
|
2015-03-26 11:24:28 +03:00
|
|
|
|
|
|
|
if ($this->view->file_exists($sourcePath)) {
|
|
|
|
$this->keySetPreparation(dirname($targetPath));
|
|
|
|
$this->view->rename($sourcePath, $targetPath);
|
2015-04-24 11:16:06 +03:00
|
|
|
|
|
|
|
return true;
|
2015-03-26 11:24:28 +03:00
|
|
|
}
|
2015-04-24 11:16:06 +03:00
|
|
|
|
|
|
|
return false;
|
2015-03-26 11:24:28 +03:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:12:44 +03:00
|
|
|
|
2015-04-02 17:25:01 +03:00
|
|
|
/**
|
|
|
|
* copy keys if a file was renamed
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @param string $target
|
2015-04-24 11:16:06 +03:00
|
|
|
* @return boolean
|
2015-04-02 17:25:01 +03:00
|
|
|
*/
|
|
|
|
public function copyKeys($source, $target) {
|
2015-06-03 12:12:44 +03:00
|
|
|
$sourcePath = $this->getPathToKeys($source);
|
|
|
|
$targetPath = $this->getPathToKeys($target);
|
2015-04-02 17:25:01 +03:00
|
|
|
|
|
|
|
if ($this->view->file_exists($sourcePath)) {
|
|
|
|
$this->keySetPreparation(dirname($targetPath));
|
|
|
|
$this->view->copy($sourcePath, $targetPath);
|
2015-04-24 11:16:06 +03:00
|
|
|
return true;
|
2015-04-02 17:25:01 +03:00
|
|
|
}
|
2015-04-24 11:16:06 +03:00
|
|
|
|
|
|
|
return false;
|
2015-04-02 17:25:01 +03:00
|
|
|
}
|
|
|
|
|
2017-01-02 23:24:37 +03:00
|
|
|
/**
|
|
|
|
* backup keys of a given encryption module
|
|
|
|
*
|
|
|
|
* @param string $encryptionModuleId
|
|
|
|
* @param string $purpose
|
|
|
|
* @param string $uid
|
|
|
|
* @return bool
|
|
|
|
* @since 12.0.0
|
|
|
|
*/
|
|
|
|
public function backupUserKeys($encryptionModuleId, $purpose, $uid) {
|
|
|
|
$source = $uid . $this->encryption_base_dir . '/' . $encryptionModuleId;
|
|
|
|
$backupDir = $uid . $this->backup_base_dir;
|
|
|
|
if (!$this->view->file_exists($backupDir)) {
|
|
|
|
$this->view->mkdir($backupDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
$backupDir = $backupDir . '/' . $purpose . '.' . $encryptionModuleId . '.' . $this->getTimestamp();
|
|
|
|
$this->view->mkdir($backupDir);
|
|
|
|
|
|
|
|
return $this->view->copy($source, $backupDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the current timestamp
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function getTimestamp() {
|
|
|
|
return time();
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:12:44 +03:00
|
|
|
/**
|
|
|
|
* get system wide path and detect mount points
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getPathToKeys($path) {
|
|
|
|
list($owner, $relativePath) = $this->util->getUidAndFilename($path);
|
|
|
|
$systemWideMountPoint = $this->util->isSystemWideMountPoint($relativePath, $owner);
|
|
|
|
|
|
|
|
if ($systemWideMountPoint) {
|
2015-07-24 13:24:18 +03:00
|
|
|
$systemPath = $this->root_dir . '/' . $this->keys_base_dir . $relativePath . '/';
|
2015-06-03 12:12:44 +03:00
|
|
|
} else {
|
2015-07-24 13:24:18 +03:00
|
|
|
$systemPath = $this->root_dir . '/' . $owner . $this->keys_base_dir . $relativePath . '/';
|
2015-06-03 12:12:44 +03:00
|
|
|
}
|
|
|
|
|
2015-07-24 13:24:18 +03:00
|
|
|
return Filesystem::normalizePath($systemPath, false);
|
2015-06-03 12:12:44 +03:00
|
|
|
}
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
/**
|
2015-04-22 14:12:52 +03:00
|
|
|
* Make preparations to filesystem for saving a key file
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
|
|
|
* @param string $path relative to the views root
|
|
|
|
*/
|
|
|
|
protected function keySetPreparation($path) {
|
|
|
|
// If the file resides within a subdirectory, create it
|
|
|
|
if (!$this->view->file_exists($path)) {
|
2015-04-02 17:42:28 +03:00
|
|
|
$sub_dirs = explode('/', ltrim($path, '/'));
|
2015-01-14 22:39:23 +03:00
|
|
|
$dir = '';
|
|
|
|
foreach ($sub_dirs as $sub_dir) {
|
|
|
|
$dir .= '/' . $sub_dir;
|
|
|
|
if (!$this->view->is_dir($dir)) {
|
|
|
|
$this->view->mkdir($dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|