2015-04-07 19:05:54 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2015-04-07 19:05:54 +03:00
|
|
|
*
|
2015-06-25 12:43:55 +03:00
|
|
|
* @license AGPL-3.0
|
2015-04-07 19:05:54 +03:00
|
|
|
*
|
2015-06-25 12:43:55 +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-04-07 19:05:54 +03:00
|
|
|
*
|
2015-06-25 12:43:55 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2015-04-07 19:05:54 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-25 12:43:55 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2015-04-07 19:05:54 +03:00
|
|
|
*
|
2015-06-25 12:43:55 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-04-07 19:05:54 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Encryption;
|
|
|
|
|
|
|
|
|
|
|
|
use OC\Files\View;
|
|
|
|
use OCP\IConfig;
|
2015-07-06 13:34:19 +03:00
|
|
|
use OCP\IDBConnection;
|
2015-07-17 13:49:45 +03:00
|
|
|
use OCP\ILogger;
|
2015-04-07 19:05:54 +03:00
|
|
|
|
|
|
|
class Migration {
|
|
|
|
|
|
|
|
private $moduleId;
|
|
|
|
/** @var \OC\Files\View */
|
|
|
|
private $view;
|
2015-07-06 13:34:19 +03:00
|
|
|
/** @var \OCP\IDBConnection */
|
2015-04-07 19:05:54 +03:00
|
|
|
private $connection;
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
2015-07-17 13:49:45 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2015-07-30 16:43:15 +03:00
|
|
|
/** @var string*/
|
|
|
|
protected $installedVersion;
|
2015-07-17 13:49:45 +03:00
|
|
|
|
2015-04-07 19:05:54 +03:00
|
|
|
/**
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param View $view
|
2015-07-06 13:34:19 +03:00
|
|
|
* @param IDBConnection $connection
|
2015-07-17 13:49:45 +03:00
|
|
|
* @param ILogger $logger
|
2015-04-07 19:05:54 +03:00
|
|
|
*/
|
2015-07-06 13:34:19 +03:00
|
|
|
public function __construct(IConfig $config, View $view, IDBConnection $connection, ILogger $logger) {
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->view = $view;
|
2015-11-25 15:53:31 +03:00
|
|
|
$this->view->disableCacheUpdate();
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->connection = $connection;
|
|
|
|
$this->moduleId = \OCA\Encryption\Crypto\Encryption::ID;
|
|
|
|
$this->config = $config;
|
2015-07-17 13:49:45 +03:00
|
|
|
$this->logger = $logger;
|
2015-07-30 16:43:15 +03:00
|
|
|
$this->installedVersion = $this->config->getAppValue('files_encryption', 'installed_version', '-1');
|
2015-04-07 19:05:54 +03:00
|
|
|
}
|
|
|
|
|
2015-07-08 19:23:18 +03:00
|
|
|
public function finalCleanUp() {
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->view->deleteAll('files_encryption/public_keys');
|
|
|
|
$this->updateFileCache();
|
2015-06-19 15:15:56 +03:00
|
|
|
$this->config->deleteAppValue('files_encryption', 'installed_version');
|
2015-04-07 19:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update file cache, copy unencrypted_size to the 'size' column
|
|
|
|
*/
|
|
|
|
private function updateFileCache() {
|
2015-07-30 16:43:15 +03:00
|
|
|
// make sure that we don't update the file cache multiple times
|
|
|
|
// only update during the first run
|
|
|
|
if ($this->installedVersion !== '-1') {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2015-08-10 17:21:41 +03:00
|
|
|
$query->update('filecache')
|
2015-07-30 16:43:15 +03:00
|
|
|
->set('size', 'unencrypted_size')
|
|
|
|
->where($query->expr()->eq('encrypted', $query->createParameter('encrypted')))
|
|
|
|
->setParameter('encrypted', 1);
|
|
|
|
$query->execute();
|
|
|
|
}
|
2015-04-07 19:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* iterate through users and reorganize the folder structure
|
|
|
|
*/
|
|
|
|
public function reorganizeFolderStructure() {
|
|
|
|
$this->reorganizeSystemFolderStructure();
|
|
|
|
|
|
|
|
$limit = 500;
|
|
|
|
$offset = 0;
|
|
|
|
do {
|
|
|
|
$users = \OCP\User::getUsers('', $limit, $offset);
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$this->reorganizeFolderStructureForUser($user);
|
|
|
|
}
|
|
|
|
$offset += $limit;
|
|
|
|
} while (count($users) >= $limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* reorganize system wide folder structure
|
|
|
|
*/
|
|
|
|
public function reorganizeSystemFolderStructure() {
|
|
|
|
|
|
|
|
$this->createPathForKeys('/files_encryption');
|
|
|
|
|
|
|
|
// backup system wide folders
|
|
|
|
$this->backupSystemWideKeys();
|
|
|
|
|
|
|
|
// rename system wide mount point
|
|
|
|
$this->renameFileKeys('', '/files_encryption/keys');
|
|
|
|
|
|
|
|
// rename system private keys
|
|
|
|
$this->renameSystemPrivateKeys();
|
|
|
|
|
|
|
|
$storage = $this->view->getMount('')->getStorage();
|
|
|
|
$storage->getScanner()->scan('files_encryption');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* reorganize folder structure for user
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
*/
|
|
|
|
public function reorganizeFolderStructureForUser($user) {
|
|
|
|
// backup all keys
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_Util::setupFS($user);
|
|
|
|
if ($this->backupUserKeys($user)) {
|
|
|
|
// rename users private key
|
|
|
|
$this->renameUsersPrivateKey($user);
|
|
|
|
$this->renameUsersPublicKey($user);
|
|
|
|
// rename file keys
|
|
|
|
$path = '/files_encryption/keys';
|
|
|
|
$this->renameFileKeys($user, $path);
|
|
|
|
$trashPath = '/files_trashbin/keys';
|
|
|
|
if (\OC_App::isEnabled('files_trashbin') && $this->view->is_dir($user . '/' . $trashPath)) {
|
|
|
|
$this->renameFileKeys($user, $trashPath, true);
|
|
|
|
$this->view->deleteAll($trashPath);
|
|
|
|
}
|
|
|
|
// delete old folders
|
|
|
|
$this->deleteOldKeys($user);
|
|
|
|
$this->view->getMount('/' . $user)->getStorage()->getScanner()->scan('files_encryption');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update database
|
|
|
|
*/
|
|
|
|
public function updateDB() {
|
|
|
|
|
2015-07-30 16:43:15 +03:00
|
|
|
// make sure that we don't update the file cache multiple times
|
|
|
|
// only update during the first run
|
|
|
|
if ($this->installedVersion === '-1') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:05:54 +03:00
|
|
|
// delete left-over from old encryption which is no longer needed
|
|
|
|
$this->config->deleteAppValue('files_encryption', 'ocsid');
|
|
|
|
$this->config->deleteAppValue('files_encryption', 'types');
|
|
|
|
$this->config->deleteAppValue('files_encryption', 'enabled');
|
|
|
|
|
2015-07-06 13:34:19 +03:00
|
|
|
$oldAppValues = $this->connection->getQueryBuilder();
|
2015-07-08 10:25:42 +03:00
|
|
|
$oldAppValues->select('*')
|
2015-08-10 17:21:41 +03:00
|
|
|
->from('appconfig')
|
2015-07-07 23:08:21 +03:00
|
|
|
->where($oldAppValues->expr()->eq('appid', $oldAppValues->createParameter('appid')))
|
2015-07-08 10:25:42 +03:00
|
|
|
->setParameter('appid', 'files_encryption');
|
|
|
|
$appSettings = $oldAppValues->execute();
|
|
|
|
|
|
|
|
while ($row = $appSettings->fetch()) {
|
|
|
|
// 'installed_version' gets deleted at the end of the migration process
|
|
|
|
if ($row['configkey'] !== 'installed_version' ) {
|
|
|
|
$this->config->setAppValue('encryption', $row['configkey'], $row['configvalue']);
|
|
|
|
$this->config->deleteAppValue('files_encryption', $row['configkey']);
|
|
|
|
}
|
|
|
|
}
|
2015-04-07 19:05:54 +03:00
|
|
|
|
2015-07-06 13:34:19 +03:00
|
|
|
$oldPreferences = $this->connection->getQueryBuilder();
|
2015-07-08 10:25:42 +03:00
|
|
|
$oldPreferences->select('*')
|
2015-08-10 17:21:41 +03:00
|
|
|
->from('preferences')
|
2015-07-07 23:08:21 +03:00
|
|
|
->where($oldPreferences->expr()->eq('appid', $oldPreferences->createParameter('appid')))
|
2015-07-08 10:25:42 +03:00
|
|
|
->setParameter('appid', 'files_encryption');
|
|
|
|
$preferenceSettings = $oldPreferences->execute();
|
2015-04-07 19:05:54 +03:00
|
|
|
|
2015-07-08 10:25:42 +03:00
|
|
|
while ($row = $preferenceSettings->fetch()) {
|
|
|
|
$this->config->setUserValue($row['userid'], 'encryption', $row['configkey'], $row['configvalue']);
|
|
|
|
$this->config->deleteUserValue($row['userid'], 'files_encryption', $row['configkey']);
|
|
|
|
}
|
2015-04-07 19:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create backup of system-wide keys
|
|
|
|
*/
|
|
|
|
private function backupSystemWideKeys() {
|
|
|
|
$backupDir = 'encryption_migration_backup_' . date("Y-m-d_H-i-s");
|
|
|
|
$this->view->mkdir($backupDir);
|
|
|
|
$this->view->copy('files_encryption', $backupDir . '/files_encryption');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create backup of user specific keys
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function backupUserKeys($user) {
|
|
|
|
$encryptionDir = $user . '/files_encryption';
|
|
|
|
if ($this->view->is_dir($encryptionDir)) {
|
|
|
|
$backupDir = $user . '/encryption_migration_backup_' . date("Y-m-d_H-i-s");
|
|
|
|
$this->view->mkdir($backupDir);
|
|
|
|
$this->view->copy($encryptionDir, $backupDir);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rename system-wide private keys
|
|
|
|
*/
|
|
|
|
private function renameSystemPrivateKeys() {
|
|
|
|
$dh = $this->view->opendir('files_encryption');
|
|
|
|
$this->createPathForKeys('/files_encryption/' . $this->moduleId );
|
|
|
|
if (is_resource($dh)) {
|
|
|
|
while (($privateKey = readdir($dh)) !== false) {
|
|
|
|
if (!\OC\Files\Filesystem::isIgnoredDir($privateKey) ) {
|
|
|
|
if (!$this->view->is_dir('/files_encryption/' . $privateKey)) {
|
|
|
|
$this->view->rename('files_encryption/' . $privateKey, 'files_encryption/' . $this->moduleId . '/' . $privateKey);
|
|
|
|
$this->renameSystemPublicKey($privateKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rename system wide public key
|
|
|
|
*
|
2015-11-27 18:32:44 +03:00
|
|
|
* @param string $privateKey private key for which we want to rename the corresponding public key
|
2015-04-07 19:05:54 +03:00
|
|
|
*/
|
|
|
|
private function renameSystemPublicKey($privateKey) {
|
|
|
|
$publicKey = substr($privateKey,0 , strrpos($privateKey, '.privateKey')) . '.publicKey';
|
|
|
|
$this->view->rename('files_encryption/public_keys/' . $publicKey, 'files_encryption/' . $this->moduleId . '/' . $publicKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rename user-specific private keys
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
*/
|
|
|
|
private function renameUsersPrivateKey($user) {
|
|
|
|
$oldPrivateKey = $user . '/files_encryption/' . $user . '.privateKey';
|
|
|
|
$newPrivateKey = $user . '/files_encryption/' . $this->moduleId . '/' . $user . '.privateKey';
|
2015-07-17 13:49:45 +03:00
|
|
|
if ($this->view->file_exists($oldPrivateKey)) {
|
|
|
|
$this->createPathForKeys(dirname($newPrivateKey));
|
|
|
|
$this->view->rename($oldPrivateKey, $newPrivateKey);
|
|
|
|
}
|
2015-04-07 19:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rename user-specific public keys
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
*/
|
|
|
|
private function renameUsersPublicKey($user) {
|
|
|
|
$oldPublicKey = '/files_encryption/public_keys/' . $user . '.publicKey';
|
|
|
|
$newPublicKey = $user . '/files_encryption/' . $this->moduleId . '/' . $user . '.publicKey';
|
2015-07-17 13:49:45 +03:00
|
|
|
if ($this->view->file_exists($oldPublicKey)) {
|
|
|
|
$this->createPathForKeys(dirname($newPublicKey));
|
|
|
|
$this->view->rename($oldPublicKey, $newPublicKey);
|
|
|
|
}
|
2015-04-07 19:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rename file keys
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @param string $path
|
|
|
|
* @param bool $trash
|
|
|
|
*/
|
|
|
|
private function renameFileKeys($user, $path, $trash = false) {
|
|
|
|
|
2015-07-17 13:49:45 +03:00
|
|
|
if ($this->view->is_dir($user . '/' . $path) === false) {
|
|
|
|
$this->logger->info('Skip dir /' . $user . '/' . $path . ': does not exist');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:05:54 +03:00
|
|
|
$dh = $this->view->opendir($user . '/' . $path);
|
|
|
|
|
|
|
|
if (is_resource($dh)) {
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
|
|
|
if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
|
|
|
|
if ($this->view->is_dir($user . '/' . $path . '/' . $file)) {
|
|
|
|
$this->renameFileKeys($user, $path . '/' . $file, $trash);
|
|
|
|
} else {
|
|
|
|
$target = $this->getTargetDir($user, $path, $file, $trash);
|
2015-07-20 17:00:33 +03:00
|
|
|
if ($target !== false) {
|
2015-07-17 13:49:45 +03:00
|
|
|
$this->createPathForKeys(dirname($target));
|
|
|
|
$this->view->rename($user . '/' . $path . '/' . $file, $target);
|
|
|
|
} else {
|
|
|
|
$this->logger->warning(
|
|
|
|
'did not move key "' . $file
|
|
|
|
. '" could not find the corresponding file in /data/' . $user . '/files.'
|
|
|
|
. 'Most likely the key was already moved in a previous migration run and is already on the right place.');
|
|
|
|
}
|
2015-04-07 19:05:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-17 13:49:45 +03:00
|
|
|
/**
|
|
|
|
* get system mount points
|
|
|
|
* wrap static method so that it can be mocked for testing
|
|
|
|
*
|
2015-07-20 17:00:33 +03:00
|
|
|
* @internal
|
2015-07-17 13:49:45 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getSystemMountPoints() {
|
|
|
|
return \OC_Mount_Config::getSystemMountPoints();
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:05:54 +03:00
|
|
|
/**
|
|
|
|
* generate target directory
|
|
|
|
*
|
|
|
|
* @param string $user
|
2015-07-17 13:49:45 +03:00
|
|
|
* @param string $keyPath
|
2015-04-07 19:05:54 +03:00
|
|
|
* @param string $filename
|
|
|
|
* @param bool $trash
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-07-17 13:49:45 +03:00
|
|
|
private function getTargetDir($user, $keyPath, $filename, $trash) {
|
2015-04-07 19:05:54 +03:00
|
|
|
if ($trash) {
|
2015-07-17 13:49:45 +03:00
|
|
|
$filePath = substr($keyPath, strlen('/files_trashbin/keys/'));
|
|
|
|
$targetDir = $user . '/files_encryption/keys/files_trashbin/' . $filePath . '/' . $this->moduleId . '/' . $filename;
|
2015-04-07 19:05:54 +03:00
|
|
|
} else {
|
2015-07-17 13:49:45 +03:00
|
|
|
$filePath = substr($keyPath, strlen('/files_encryption/keys/'));
|
|
|
|
$targetDir = $user . '/files_encryption/keys/files/' . $filePath . '/' . $this->moduleId . '/' . $filename;
|
2015-04-07 19:05:54 +03:00
|
|
|
}
|
|
|
|
|
2015-07-17 13:49:45 +03:00
|
|
|
if ($user === '') {
|
|
|
|
// for system wide mounts we need to check if the mount point really exists
|
2015-07-20 17:00:33 +03:00
|
|
|
$normalized = \OC\Files\Filesystem::normalizePath($filePath);
|
2015-07-17 13:49:45 +03:00
|
|
|
$systemMountPoints = $this->getSystemMountPoints();
|
|
|
|
foreach ($systemMountPoints as $mountPoint) {
|
2015-07-20 17:00:33 +03:00
|
|
|
$normalizedMountPoint = \OC\Files\Filesystem::normalizePath($mountPoint['mountpoint']) . '/';
|
|
|
|
if (strpos($normalized, $normalizedMountPoint) === 0)
|
2015-07-17 13:49:45 +03:00
|
|
|
return $targetDir;
|
|
|
|
}
|
|
|
|
} else if ($trash === false && $this->view->file_exists('/' . $user. '/files/' . $filePath)) {
|
|
|
|
return $targetDir;
|
|
|
|
} else if ($trash === true && $this->view->file_exists('/' . $user. '/files_trashbin/' . $filePath)) {
|
|
|
|
return $targetDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-04-07 19:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete old keys
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
*/
|
|
|
|
private function deleteOldKeys($user) {
|
|
|
|
$this->view->deleteAll($user . '/files_encryption/keyfiles');
|
|
|
|
$this->view->deleteAll($user . '/files_encryption/share-keys');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create directories for the keys recursively
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*/
|
|
|
|
private function createPathForKeys($path) {
|
|
|
|
if (!$this->view->file_exists($path)) {
|
|
|
|
$sub_dirs = explode('/', $path);
|
|
|
|
$dir = '';
|
|
|
|
foreach ($sub_dirs as $sub_dir) {
|
|
|
|
$dir .= '/' . $sub_dir;
|
|
|
|
if (!$this->view->is_dir($dir)) {
|
|
|
|
$this->view->mkdir($dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|