2014-05-12 15:55:23 +04:00
|
|
|
<?php
|
2015-02-27 16:14:57 +03:00
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@owncloud.com>
|
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-02-26 13:37:37 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
2014-05-12 15:55:23 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +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.
|
2014-05-12 15:55:23 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-05-12 15:55:23 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-05-12 15:55:23 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +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/>
|
2014-05-12 15:55:23 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-05-12 15:55:23 +04:00
|
|
|
namespace OCA\Files_Encryption;
|
|
|
|
|
|
|
|
|
|
|
|
class Migration {
|
|
|
|
|
2014-11-18 19:25:36 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\View
|
|
|
|
*/
|
|
|
|
private $view;
|
|
|
|
private $public_share_key_id;
|
|
|
|
private $recovery_key_id;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->view = new \OC\Files\View();
|
2015-02-27 16:14:57 +03:00
|
|
|
$this->view->getUpdater()->disable();
|
2014-12-03 18:52:44 +03:00
|
|
|
$this->public_share_key_id = Helper::getPublicShareKeyId();
|
|
|
|
$this->recovery_key_id = Helper::getRecoveryKeyId();
|
2014-11-18 19:25:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function reorganizeFolderStructure() {
|
2015-01-22 18:10:45 +03:00
|
|
|
$this->reorganizeSystemFolderStructure();
|
|
|
|
|
2015-02-06 19:37:05 +03:00
|
|
|
$limit = 500;
|
|
|
|
$offset = 0;
|
|
|
|
do {
|
|
|
|
$users = \OCP\User::getUsers('', $limit, $offset);
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$this->reorganizeFolderStructureForUser($user);
|
|
|
|
}
|
|
|
|
$offset += $limit;
|
2015-02-27 16:14:57 +03:00
|
|
|
} while (count($users) >= $limit);
|
2015-01-22 18:10:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function reorganizeSystemFolderStructure() {
|
2014-11-18 19:25:36 +03:00
|
|
|
|
|
|
|
$this->createPathForKeys('/files_encryption');
|
|
|
|
|
|
|
|
// backup system wide folders
|
|
|
|
$this->backupSystemWideKeys();
|
|
|
|
|
|
|
|
// rename public keys
|
|
|
|
$this->renamePublicKeys();
|
|
|
|
|
|
|
|
// rename system wide mount point
|
|
|
|
$this->renameFileKeys('', '/files_encryption/keyfiles');
|
|
|
|
|
|
|
|
// rename system private keys
|
|
|
|
$this->renameSystemPrivateKeys();
|
|
|
|
|
|
|
|
// delete old system wide folders
|
|
|
|
$this->view->deleteAll('/public-keys');
|
|
|
|
$this->view->deleteAll('/owncloud_private_key');
|
|
|
|
$this->view->deleteAll('/files_encryption/share-keys');
|
|
|
|
$this->view->deleteAll('/files_encryption/keyfiles');
|
2015-02-27 16:14:57 +03:00
|
|
|
$storage = $this->view->getMount('')->getStorage();
|
|
|
|
$storage->getScanner()->scan('files_encryption');
|
|
|
|
$storage->getCache()->remove('owncloud_private_key');
|
|
|
|
$storage->getCache()->remove('public-keys');
|
2015-01-22 18:10:45 +03:00
|
|
|
}
|
2014-11-18 19:25:36 +03:00
|
|
|
|
2015-01-22 18:10:45 +03:00
|
|
|
|
|
|
|
public function reorganizeFolderStructureForUser($user) {
|
|
|
|
// backup all keys
|
2015-03-25 12:55:54 +03:00
|
|
|
\OC_Util::tearDownFS();
|
2015-01-22 21:16:28 +03:00
|
|
|
\OC_Util::setupFS($user);
|
2015-01-22 18:10:45 +03:00
|
|
|
if ($this->backupUserKeys($user)) {
|
|
|
|
// create new 'key' folder
|
|
|
|
$this->view->mkdir($user . '/files_encryption/keys');
|
|
|
|
// rename users private key
|
|
|
|
$this->renameUsersPrivateKey($user);
|
|
|
|
// rename file keys
|
|
|
|
$path = $user . '/files_encryption/keyfiles';
|
|
|
|
$this->renameFileKeys($user, $path);
|
|
|
|
$trashPath = $user . '/files_trashbin/keyfiles';
|
|
|
|
if (\OC_App::isEnabled('files_trashbin') && $this->view->is_dir($trashPath)) {
|
|
|
|
$this->renameFileKeys($user, $trashPath, true);
|
|
|
|
$this->view->deleteAll($trashPath);
|
|
|
|
$this->view->deleteAll($user . '/files_trashbin/share-keys');
|
2014-11-18 19:25:36 +03:00
|
|
|
}
|
2015-01-22 18:10:45 +03:00
|
|
|
// delete old folders
|
|
|
|
$this->deleteOldKeys($user);
|
2015-02-27 16:14:57 +03:00
|
|
|
$this->view->getMount('/' . $user)->getStorage()->getScanner()->scan('files_encryption');
|
2014-11-18 19:25:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function backupSystemWideKeys() {
|
|
|
|
$backupDir = 'encryption_migration_backup_' . date("Y-m-d_H-i-s");
|
|
|
|
$this->view->mkdir($backupDir);
|
|
|
|
$this->view->copy('owncloud_private_key', $backupDir . '/owncloud_private_key');
|
|
|
|
$this->view->copy('public-keys', $backupDir . '/public-keys');
|
|
|
|
$this->view->copy('files_encryption', $backupDir . '/files_encryption');
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function renamePublicKeys() {
|
|
|
|
$dh = $this->view->opendir('public-keys');
|
|
|
|
|
|
|
|
$this->createPathForKeys('files_encryption/public_keys');
|
|
|
|
|
|
|
|
if (is_resource($dh)) {
|
|
|
|
while (($oldPublicKey = readdir($dh)) !== false) {
|
|
|
|
if (!\OC\Files\Filesystem::isIgnoredDir($oldPublicKey)) {
|
|
|
|
$newPublicKey = substr($oldPublicKey, 0, strlen($oldPublicKey) - strlen('.public.key')) . '.publicKey';
|
2015-02-27 16:14:57 +03:00
|
|
|
$this->view->rename('public-keys/' . $oldPublicKey, 'files_encryption/public_keys/' . $newPublicKey);
|
2014-11-18 19:25:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dh);
|
|
|
|
}
|
2014-05-12 15:55:23 +04:00
|
|
|
}
|
|
|
|
|
2014-11-18 19:25:36 +03:00
|
|
|
private function renameSystemPrivateKeys() {
|
|
|
|
$dh = $this->view->opendir('owncloud_private_key');
|
|
|
|
|
|
|
|
if (is_resource($dh)) {
|
|
|
|
while (($oldPrivateKey = readdir($dh)) !== false) {
|
|
|
|
if (!\OC\Files\Filesystem::isIgnoredDir($oldPrivateKey)) {
|
|
|
|
$newPrivateKey = substr($oldPrivateKey, 0, strlen($oldPrivateKey) - strlen('.private.key')) . '.privateKey';
|
2015-02-27 16:14:57 +03:00
|
|
|
$this->view->rename('owncloud_private_key/' . $oldPrivateKey, 'files_encryption/' . $newPrivateKey);
|
2014-11-18 19:25:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dh);
|
2014-05-12 15:55:23 +04:00
|
|
|
}
|
2014-11-18 19:25:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function renameUsersPrivateKey($user) {
|
2015-02-27 16:14:57 +03:00
|
|
|
$oldPrivateKey = $user . '/files_encryption/' . $user . '.private.key';
|
|
|
|
$newPrivateKey = substr($oldPrivateKey, 0, strlen($oldPrivateKey) - strlen('.private.key')) . '.privateKey';
|
2014-11-18 19:25:36 +03:00
|
|
|
|
2015-02-27 16:14:57 +03:00
|
|
|
$this->view->rename($oldPrivateKey, $newPrivateKey);
|
2014-11-18 19:25:36 +03:00
|
|
|
}
|
2014-05-12 15:55:23 +04:00
|
|
|
|
2014-11-18 19:25:36 +03:00
|
|
|
private function getFileName($file, $trash) {
|
|
|
|
|
|
|
|
$extLength = strlen('.key');
|
|
|
|
|
|
|
|
if ($trash) {
|
|
|
|
$parts = explode('.', $file);
|
|
|
|
if ($parts[count($parts) - 1] !== 'key') {
|
|
|
|
$extLength = $extLength + strlen('.' . $parts[count($parts) - 1]);
|
|
|
|
}
|
2014-05-12 15:55:23 +04:00
|
|
|
}
|
|
|
|
|
2014-11-18 19:25:36 +03:00
|
|
|
$filename = substr($file, 0, strlen($file) - $extLength);
|
|
|
|
|
|
|
|
return $filename;
|
2014-05-12 15:55:23 +04:00
|
|
|
}
|
|
|
|
|
2014-11-18 19:25:36 +03:00
|
|
|
private function getExtension($file, $trash) {
|
|
|
|
|
|
|
|
$extension = '';
|
|
|
|
|
|
|
|
if ($trash) {
|
|
|
|
$parts = explode('.', $file);
|
|
|
|
if ($parts[count($parts) - 1] !== 'key') {
|
|
|
|
$extension = '.' . $parts[count($parts) - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getFilePath($path, $user, $trash) {
|
2015-02-27 16:14:57 +03:00
|
|
|
$offset = $trash ? strlen($user . '/files_trashbin/keyfiles') : strlen($user . '/files_encryption/keyfiles');
|
2014-11-18 19:25:36 +03:00
|
|
|
return substr($path, $offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTargetDir($user, $filePath, $filename, $extension, $trash) {
|
|
|
|
if ($trash) {
|
|
|
|
$targetDir = $user . '/files_trashbin/keys/' . $filePath . '/' . $filename . $extension;
|
|
|
|
} else {
|
|
|
|
$targetDir = $user . '/files_encryption/keys/' . $filePath . '/' . $filename . $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $targetDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function renameFileKeys($user, $path, $trash = false) {
|
|
|
|
|
|
|
|
$dh = $this->view->opendir($path);
|
|
|
|
|
|
|
|
if (is_resource($dh)) {
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
|
|
|
if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
|
|
|
|
if ($this->view->is_dir($path . '/' . $file)) {
|
|
|
|
$this->renameFileKeys($user, $path . '/' . $file, $trash);
|
|
|
|
} else {
|
|
|
|
$filename = $this->getFileName($file, $trash);
|
|
|
|
$filePath = $this->getFilePath($path, $user, $trash);
|
|
|
|
$extension = $this->getExtension($file, $trash);
|
|
|
|
$targetDir = $this->getTargetDir($user, $filePath, $filename, $extension, $trash);
|
|
|
|
$this->createPathForKeys($targetDir);
|
2015-02-27 18:08:05 +03:00
|
|
|
$this->view->rename($path . '/' . $file, $targetDir . '/fileKey');
|
2014-11-18 19:25:36 +03:00
|
|
|
$this->renameShareKeys($user, $filePath, $filename, $targetDir, $trash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getOldShareKeyPath($user, $filePath, $trash) {
|
|
|
|
if ($trash) {
|
|
|
|
$oldShareKeyPath = $user . '/files_trashbin/share-keys/' . $filePath;
|
|
|
|
} else {
|
|
|
|
$oldShareKeyPath = $user . '/files_encryption/share-keys/' . $filePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $oldShareKeyPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getUidFromShareKey($file, $filename, $trash) {
|
|
|
|
$extLength = strlen('.shareKey');
|
|
|
|
if ($trash) {
|
|
|
|
$parts = explode('.', $file);
|
|
|
|
if ($parts[count($parts) - 1] !== 'shareKey') {
|
|
|
|
$extLength = $extLength + strlen('.' . $parts[count($parts) - 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$uid = substr($file, strlen($filename) + 1, $extLength * -1);
|
|
|
|
|
|
|
|
return $uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function renameShareKeys($user, $filePath, $filename, $target, $trash) {
|
|
|
|
$oldShareKeyPath = $this->getOldShareKeyPath($user, $filePath, $trash);
|
|
|
|
$dh = $this->view->opendir($oldShareKeyPath);
|
|
|
|
|
|
|
|
if (is_resource($dh)) {
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
|
|
|
if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
|
|
|
|
if ($this->view->is_dir($oldShareKeyPath . '/' . $file)) {
|
|
|
|
continue;
|
|
|
|
} else {
|
2015-02-27 16:14:57 +03:00
|
|
|
if (substr($file, 0, strlen($filename) + 1) === $filename . '.') {
|
2014-11-18 19:25:36 +03:00
|
|
|
|
|
|
|
$uid = $this->getUidFromShareKey($file, $filename, $trash);
|
2015-02-27 18:08:05 +03:00
|
|
|
$this->view->rename($oldShareKeyPath . '/' . $file, $target . '/' . $uid . '.shareKey');
|
2014-11-18 19:25:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function deleteOldKeys($user) {
|
|
|
|
$this->view->deleteAll($user . '/files_encryption/keyfiles');
|
|
|
|
$this->view->deleteAll($user . '/files_encryption/share-keys');
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-12 15:55:23 +04:00
|
|
|
}
|