2013-06-03 17:27:31 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013, Bjoern Schiessle <schiessle@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
2014-05-19 19:50:53 +04:00
|
|
|
* Script to change recovery key password
|
2013-06-03 17:27:31 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
use OCA\Encryption;
|
|
|
|
|
|
|
|
\OCP\JSON::checkLoggedIn();
|
|
|
|
\OCP\JSON::checkAppEnabled('files_encryption');
|
|
|
|
\OCP\JSON::callCheck();
|
|
|
|
|
2014-08-31 12:05:59 +04:00
|
|
|
$l = \OC::$server->getL10N('core');
|
2013-06-03 17:27:31 +04:00
|
|
|
|
|
|
|
$return = false;
|
2014-11-06 13:11:46 +03:00
|
|
|
$errorMessage = $l->t('Could not update the private key password.');
|
2013-06-03 17:27:31 +04:00
|
|
|
|
|
|
|
$oldPassword = $_POST['oldPassword'];
|
|
|
|
$newPassword = $_POST['newPassword'];
|
|
|
|
|
|
|
|
$view = new \OC\Files\View('/');
|
|
|
|
$session = new \OCA\Encryption\Session($view);
|
|
|
|
$user = \OCP\User::getUser();
|
2014-11-24 17:44:43 +03:00
|
|
|
$loginName = \OC::$server->getUserSession()->getLoginName();
|
2013-06-03 17:27:31 +04:00
|
|
|
|
2014-11-06 13:11:46 +03:00
|
|
|
// check new password
|
2014-11-24 17:44:43 +03:00
|
|
|
$passwordCorrect = \OCP\User::checkPassword($loginName, $newPassword);
|
2014-11-06 13:11:46 +03:00
|
|
|
|
|
|
|
if ($passwordCorrect !== false) {
|
|
|
|
|
2013-06-03 17:27:31 +04:00
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = false;
|
|
|
|
|
2013-06-03 20:42:13 +04:00
|
|
|
$keyPath = '/' . $user . '/files_encryption/' . $user . '.private.key';
|
2013-06-03 17:27:31 +04:00
|
|
|
|
|
|
|
$encryptedKey = $view->file_get_contents($keyPath);
|
|
|
|
$decryptedKey = \OCA\Encryption\Crypt::decryptPrivateKey($encryptedKey, $oldPassword);
|
|
|
|
|
|
|
|
if ($decryptedKey) {
|
2014-07-21 15:02:28 +04:00
|
|
|
$cipher = \OCA\Encryption\Helper::getCipher();
|
|
|
|
$encryptedKey = \OCA\Encryption\Crypt::symmetricEncryptFileContent($decryptedKey, $newPassword, $cipher);
|
|
|
|
if ($encryptedKey) {
|
|
|
|
\OCA\Encryption\Keymanager::setPrivateKey($encryptedKey, $user);
|
|
|
|
$session->setPrivateKey($decryptedKey);
|
|
|
|
$return = true;
|
|
|
|
}
|
2014-11-06 13:11:46 +03:00
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
$errorMessage = $l->t('The old password was not correct, please try again.');
|
2013-06-03 17:27:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
|
|
|
|
2014-11-06 13:11:46 +03:00
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
$errorMessage = $l->t('The current log-in password was not correct, please try again.');
|
|
|
|
}
|
|
|
|
|
2013-06-03 17:27:31 +04:00
|
|
|
// success or failure
|
|
|
|
if ($return) {
|
2013-09-06 14:27:40 +04:00
|
|
|
$session->setInitialized(\OCA\Encryption\Session::INIT_SUCCESSFUL);
|
2013-06-03 17:27:31 +04:00
|
|
|
\OCP\JSON::success(array('data' => array('message' => $l->t('Private key password successfully updated.'))));
|
|
|
|
} else {
|
2014-11-06 13:11:46 +03:00
|
|
|
\OCP\JSON::error(array('data' => array('message' => $errorMessage)));
|
2013-08-18 13:02:08 +04:00
|
|
|
}
|