diff options
author | Bjoern Schiessle <schiessle@owncloud.com> | 2015-03-31 16:23:31 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-04-07 13:30:29 +0200 |
commit | e4895bda01f9c94fc33e094ae9466e1cf5502916 (patch) | |
tree | f78a79ef0c3035d443fc10e2eaedcb4f436d7091 /apps | |
parent | bd933b1c85dff950e83591a6245ba2e15db33caf (diff) | |
download | nextcloud-server-e4895bda01f9c94fc33e094ae9466e1cf5502916.tar.gz nextcloud-server-e4895bda01f9c94fc33e094ae9466e1cf5502916.zip |
add helper class accessible for encryption modules to ask for a list of users with access to a file, needed to apply the recovery key to all files
Diffstat (limited to 'apps')
-rw-r--r-- | apps/encryption/appinfo/application.php | 4 | ||||
-rw-r--r-- | apps/encryption/appinfo/routes.php | 5 | ||||
-rw-r--r-- | apps/encryption/controller/recoverycontroller.php | 61 | ||||
-rw-r--r-- | apps/encryption/js/settings-personal.js | 29 | ||||
-rw-r--r-- | apps/encryption/lib/recovery.php | 87 | ||||
-rw-r--r-- | apps/encryption/settings/settings-personal.php | 6 | ||||
-rw-r--r-- | apps/encryption/templates/settings-personal.php | 2 |
7 files changed, 137 insertions, 57 deletions
diff --git a/apps/encryption/appinfo/application.php b/apps/encryption/appinfo/application.php index e8f10798bb4..372d49e5ef7 100644 --- a/apps/encryption/appinfo/application.php +++ b/apps/encryption/appinfo/application.php @@ -139,7 +139,9 @@ class Application extends \OCP\AppFramework\App { $server->getSecureRandom(), $c->query('KeyManager'), $server->getConfig(), - $server->getEncryptionKeyStorage(\OCA\Encryption\Crypto\Encryption::ID)); + $server->getEncryptionKeyStorage(\OCA\Encryption\Crypto\Encryption::ID), + $server->getEncryptionFilesHelper(), + new \OC\Files\View()); }); $container->registerService('RecoveryController', function (IAppContainer $c) { diff --git a/apps/encryption/appinfo/routes.php b/apps/encryption/appinfo/routes.php index 030e7617816..1a6cf18fbed 100644 --- a/apps/encryption/appinfo/routes.php +++ b/apps/encryption/appinfo/routes.php @@ -38,6 +38,11 @@ namespace OCA\Encryption\AppInfo; 'name' => 'Recovery#changeRecoveryPassword', 'url' => '/ajax/changeRecoveryPassword', 'verb' => 'POST' + ], + [ + 'name' => 'Recovery#userSetRecovery', + 'url' => '/ajax/userSetRecovery', + 'verb' => 'POST' ] diff --git a/apps/encryption/controller/recoverycontroller.php b/apps/encryption/controller/recoverycontroller.php index e7bfd374903..d115feb8e39 100644 --- a/apps/encryption/controller/recoverycontroller.php +++ b/apps/encryption/controller/recoverycontroller.php @@ -61,61 +61,72 @@ class RecoveryController extends Controller { public function adminRecovery($recoveryPassword, $confirmPassword, $adminEnableRecovery) { // Check if both passwords are the same if (empty($recoveryPassword)) { - $errorMessage = $this->l->t('Missing recovery key password'); + $errorMessage = (string) $this->l->t('Missing recovery key password'); return new DataResponse(['data' => ['message' => $errorMessage]], 500); } if (empty($confirmPassword)) { - $errorMessage = $this->l->t('Please repeat the recovery key password'); + $errorMessage = (string) $this->l->t('Please repeat the recovery key password'); return new DataResponse(['data' => ['message' => $errorMessage]], 500); } if ($recoveryPassword !== $confirmPassword) { - $errorMessage = $this->l->t('Repeated recovery key password does not match the provided recovery key password'); + $errorMessage = (string) $this->l->t('Repeated recovery key password does not match the provided recovery key password'); return new DataResponse(['data' => ['message' => $errorMessage]], 500); } if (isset($adminEnableRecovery) && $adminEnableRecovery === '1') { if ($this->recovery->enableAdminRecovery($recoveryPassword)) { - return new DataResponse(['status' =>'success', 'data' => array('message' => $this->l->t('Recovery key successfully enabled'))]); + return new DataResponse(['status' =>'success', 'data' => array('message' => (string) $this->l->t('Recovery key successfully enabled'))]); } - return new DataResponse(['data' => array('message' => $this->l->t('Could not enable recovery key. Please check your recovery key password!'))]); + return new DataResponse(['data' => array('message' => (string) $this->l->t('Could not enable recovery key. Please check your recovery key password!'))]); } elseif (isset($adminEnableRecovery) && $adminEnableRecovery === '0') { if ($this->recovery->disableAdminRecovery($recoveryPassword)) { - return new DataResponse(['data' => array('message' => $this->l->t('Recovery key successfully disabled'))]); + return new DataResponse(['data' => array('message' => (string) $this->l->t('Recovery key successfully disabled'))]); } - return new DataResponse(['data' => array('message' => $this->l->t('Could not disable recovery key. Please check your recovery key password!'))]); + return new DataResponse(['data' => array('message' => (string) $this->l->t('Could not disable recovery key. Please check your recovery key password!'))]); } } public function changeRecoveryPassword($newPassword, $oldPassword, $confirmPassword) { //check if both passwords are the same if (empty($oldPassword)) { - $errorMessage = $this->l->t('Please provide the old recovery password'); + $errorMessage = (string) $this->l->t('Please provide the old recovery password'); return new DataResponse(array('data' => array('message' => $errorMessage))); } if (empty($newPassword)) { - $errorMessage = $this->l->t('Please provide a new recovery password'); + $errorMessage = (string) $this->l->t('Please provide a new recovery password'); return new DataResponse (array('data' => array('message' => $errorMessage))); } if (empty($confirmPassword)) { - $errorMessage = $this->l->t('Please repeat the new recovery password'); + $errorMessage = (string) $this->l->t('Please repeat the new recovery password'); return new DataResponse(array('data' => array('message' => $errorMessage))); } if ($newPassword !== $confirmPassword) { - $errorMessage = $this->l->t('Repeated recovery key password does not match the provided recovery key password'); + $errorMessage = (string) $this->l->t('Repeated recovery key password does not match the provided recovery key password'); return new DataResponse(array('data' => array('message' => $errorMessage))); } $result = $this->recovery->changeRecoveryKeyPassword($newPassword, $oldPassword); if ($result) { - return new DataResponse(array('status' => 'success' ,'data' => array('message' => $this->l->t('Password successfully changed.')))); + return new DataResponse( + array( + 'status' => 'success' , + 'data' => array( + 'message' => (string) $this->l->t('Password successfully changed.')) + ) + ); } else { - return new DataResponse(array('data' => array('message' => $this->l->t('Could not change the password. Maybe the old password was not correct.')))); + return new DataResponse( + array( + 'data' => array + ('message' => (string) $this->l->t('Could not change the password. Maybe the old password was not correct.')) + ) + ); } } @@ -131,4 +142,28 @@ class RecoveryController extends Controller { } } + public function userSetRecovery($userEnableRecovery) { + if ($userEnableRecovery === '0' || $userEnableRecovery === '1') { + + $result = $this->recovery->setRecoveryForUser($userEnableRecovery); + + if ($result) { + return new DataResponse( + array( + 'status' => 'success', + 'data' => array( + 'message' => (string) $this->l->t('Recovery Key enabled')) + ) + ); + } else { + return new DataResponse( + array( + 'data' => array + ('message' => (string) $this->l->t('Could not enable the recovery key, please try again or contact your administrator')) + ) + ); + } + } + } + } diff --git a/apps/encryption/js/settings-personal.js b/apps/encryption/js/settings-personal.js index b798ba7e4e1..7f0f4c6c26d 100644 --- a/apps/encryption/js/settings-personal.js +++ b/apps/encryption/js/settings-personal.js @@ -29,7 +29,7 @@ $(document).ready(function(){ var recoveryStatus = $( this ).val(); OC.msg.startAction('#userEnableRecovery .msg', 'Updating recovery keys. This can take some time...'); $.post( - OC.filePath( 'files_encryption', 'ajax', 'userrecovery.php' ) + OC.generateUrl('/apps/encryption/ajax/userSetRecovery') , { userEnableRecovery: recoveryStatus } , function( data ) { OC.msg.finishedAction('#userEnableRecovery .msg', data); @@ -40,33 +40,6 @@ $(document).ready(function(){ } ); - $("#encryptAll").click( - function(){ - - // Hide feedback messages in case they're already visible - $('#encryptAllSuccess').hide(); - $('#encryptAllError').hide(); - - var userPassword = $( '#userPassword' ).val(); - var encryptAll = $( '#encryptAll' ).val(); - - $.post( - OC.filePath( 'files_encryption', 'ajax', 'encryptall.php' ) - , { encryptAll: encryptAll, userPassword: userPassword } - , function( data ) { - if ( data.status == "success" ) { - $('#encryptAllSuccess').show(); - } else { - $('#encryptAllError').show(); - } - } - ); - // Ensure page is not reloaded on form submit - return false; - } - - ); - // update private key password $('input:password[name="changePrivateKeyPassword"]').keyup(function(event) { diff --git a/apps/encryption/lib/recovery.php b/apps/encryption/lib/recovery.php index 0426c3746ed..701c0934c95 100644 --- a/apps/encryption/lib/recovery.php +++ b/apps/encryption/lib/recovery.php @@ -29,7 +29,8 @@ use OCP\IUser; use OCP\IUserSession; use OCP\PreConditionNotMetException; use OCP\Security\ISecureRandom; -use OCP\Share; +use OC\Files\View; +use OCP\Encryption\IFile; class Recovery { @@ -58,7 +59,17 @@ class Recovery { * @var IStorage */ private $keyStorage; - + /** + * @var View + */ + private $view; + /** + * @var IFile + */ + private $file; + /** + * @var string + */ private $recoveryKeyId; /** @@ -68,19 +79,25 @@ class Recovery { * @param KeyManager $keyManager * @param IConfig $config * @param IStorage $keyStorage + * @param IFile $file + * @param View $view */ public function __construct(IUserSession $user, Crypt $crypt, ISecureRandom $random, KeyManager $keyManager, IConfig $config, - IStorage $keyStorage) { + IStorage $keyStorage, + IFile $file, + View $view) { $this->user = $user && $user->isLoggedIn() ? $user->getUser() : false; $this->crypt = $crypt; $this->random = $random; $this->keyManager = $keyManager; $this->config = $config; $this->keyStorage = $keyStorage; + $this->view = $view; + $this->file = $file; } /** @@ -138,14 +155,6 @@ class Recovery { return false; } - public function addRecoveryKeys($keyId) { - // No idea new way to do this.... - } - - public function removeRecoveryKeys() { - // No idea new way to do this.... - } - /** * @return bool */ @@ -159,17 +168,23 @@ class Recovery { } /** - * @param $enabled + * @param string $value * @return bool */ - public function setRecoveryForUser($enabled) { - $value = $enabled ? '1' : '0'; + public function setRecoveryForUser($value) { try { $this->config->setUserValue($this->user->getUID(), 'encryption', 'recoveryEnabled', $value); + + if ($value === '1') { + $this->addRecoveryKeys('/' . $this->user . '/files/'); + } else { + $this->removeRecoveryKeys(); + } + return true; } catch (PreConditionNotMetException $e) { return false; @@ -177,6 +192,50 @@ class Recovery { } /** + * add recovery key to all encrypted files + */ + private function addRecoveryKeys($path = '/') { + $dirContent = $this->view->getDirectoryContent($path); + foreach ($dirContent as $item) { + // get relative path from files_encryption/keyfiles/ + $filePath = $item['path']; + if ($item['type'] === 'dir') { + $this->addRecoveryKeys($filePath . '/'); + } else { + $fileKey = $this->keyManager->getFileKey($filePath, $this->user); + if (!empty($fileKey)) { + $accessList = $this->file->getAccessList($path); + $publicKeys = array(); + foreach ($accessList['users'] as $uid) { + $publicKeys[$uid] = $this->keymanager->getPublicKey($uid); + } + + $encryptedKeyfiles = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys); + $this->keymanager->setAllFileKeys($path, $encryptedKeyfiles); + } + } + } + } + + /** + * remove recovery key to all encrypted files + */ + private function removeRecoveryKeys($path = '/') { + $dirContent = $this->view->getDirectoryContent($this->keyfilesPath . $path); + foreach ($dirContent as $item) { + // get relative path from files_encryption/keyfiles + $filePath = substr($item['path'], strlen('files_encryption/keyfiles')); + if ($item['type'] === 'dir') { + $this->removeRecoveryKeys($filePath . '/'); + } else { + // remove '.key' extension from path e.g. 'file.txt.key' to 'file.txt' + $file = substr($filePath, 0, -4); + $this->view->unlink($this->shareKeysPath . '/' . $file . '.' . $this->recoveryKeyId . '.shareKey'); + } + } + } + + /** * @param $recoveryPassword */ public function recoverUsersFiles($recoveryPassword) { diff --git a/apps/encryption/settings/settings-personal.php b/apps/encryption/settings/settings-personal.php index a4173dbd40f..8caacbd19ca 100644 --- a/apps/encryption/settings/settings-personal.php +++ b/apps/encryption/settings/settings-personal.php @@ -29,7 +29,11 @@ $user = \OCP\User::getUser(); $view = new \OC\Files\View('/'); $util = new \OCA\Encryption\Util( - new \OC\Files\View(), $crypt, $keymanager, \OC::$server->getLogger(), \OC::$server->getUserSession(), \OC::$server->getConfig()); + new \OC\Files\View(), + $crypt, $keymanager, + \OC::$server->getLogger(), + \OC::$server->getUserSession(), + \OC::$server->getConfig()); $privateKeySet = $session->isPrivateKeySet(); // did we tried to initialize the keys for this session? diff --git a/apps/encryption/templates/settings-personal.php b/apps/encryption/templates/settings-personal.php index cefd6f4ad5c..6b8821ca8a8 100644 --- a/apps/encryption/templates/settings-personal.php +++ b/apps/encryption/templates/settings-personal.php @@ -1,6 +1,8 @@ <?php /** @var array $_ */ /** @var OC_L10N $l */ +script('encryption', 'settings-personal'); +script('core', 'multiselect'); ?> <form id="encryption" class="section"> <h2><?php p($l->t('ownCloud basic encryption module')); ?></h2> |