2013-03-28 17:29:18 +00:00
|
|
|
<?php
|
2013-05-13 15:26:21 +00:00
|
|
|
|
2013-03-28 17:29:18 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2013, Sam Tuke <samtuke@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
* @brief Script to handle admin settings for encrypted key recovery
|
|
|
|
*/
|
|
|
|
use OCA\Encryption;
|
|
|
|
|
|
|
|
\OCP\JSON::checkAdminUser();
|
2013-05-13 15:26:21 +00:00
|
|
|
\OCP\JSON::checkAppEnabled('files_encryption');
|
2013-03-28 17:29:18 +00:00
|
|
|
\OCP\JSON::callCheck();
|
|
|
|
|
2013-05-13 15:26:21 +00:00
|
|
|
$return = false;
|
2013-04-10 15:37:03 +00:00
|
|
|
|
2013-05-01 17:18:31 +00:00
|
|
|
// Enable recoveryAdmin
|
2013-05-13 15:26:21 +00:00
|
|
|
|
2013-05-15 12:02:13 +00:00
|
|
|
$recoveryKeyId = OC_Appconfig::getValue('files_encryption', 'recoveryKeyId');
|
2013-03-28 17:29:18 +00:00
|
|
|
|
2013-05-15 12:02:13 +00:00
|
|
|
if (isset($_POST['adminEnableRecovery']) && $_POST['adminEnableRecovery'] == 1){
|
2013-05-13 15:26:21 +00:00
|
|
|
|
2013-05-15 12:02:13 +00:00
|
|
|
$view = new \OC\Files\View('/');
|
2013-05-13 15:26:21 +00:00
|
|
|
|
|
|
|
if ($recoveryKeyId === null) {
|
|
|
|
$recoveryKeyId = 'recovery_' . substr(md5(time()), 0, 8);
|
|
|
|
\OC_Appconfig::setValue('files_encryption', 'recoveryKeyId', $recoveryKeyId);
|
2013-03-28 17:29:18 +00:00
|
|
|
}
|
2013-05-13 15:26:21 +00:00
|
|
|
|
|
|
|
if (!$view->is_dir('/owncloud_private_key')) {
|
|
|
|
$view->mkdir('/owncloud_private_key');
|
2013-03-28 17:29:18 +00:00
|
|
|
}
|
2013-05-13 15:26:21 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
(!$view->file_exists("/public-keys/" . $recoveryKeyId . ".public.key")
|
|
|
|
|| !$view->file_exists("/owncloud_private_key/" . $recoveryKeyId . ".private.key"))
|
|
|
|
) {
|
|
|
|
|
|
|
|
$keypair = \OCA\Encryption\Crypt::createKeypair();
|
|
|
|
|
|
|
|
\OC_FileProxy::$enabled = false;
|
|
|
|
|
|
|
|
// Save public key
|
|
|
|
|
|
|
|
if (!$view->is_dir('/public-keys')) {
|
|
|
|
$view->mkdir('/public-keys');
|
|
|
|
}
|
|
|
|
|
|
|
|
$view->file_put_contents('/public-keys/' . $recoveryKeyId . '.public.key', $keypair['publicKey']);
|
|
|
|
|
|
|
|
// Encrypt private key empthy passphrase
|
|
|
|
$encryptedPrivateKey = \OCA\Encryption\Crypt::symmetricEncryptFileContent($keypair['privateKey'], $_POST['recoveryPassword']);
|
|
|
|
|
|
|
|
// Save private key
|
|
|
|
$view->file_put_contents('/owncloud_private_key/' . $recoveryKeyId . '.private.key', $encryptedPrivateKey);
|
|
|
|
|
2013-05-15 12:02:13 +00:00
|
|
|
// create control file which let us check later on if the entered password was correct.
|
|
|
|
$encryptedControlData = \OCA\Encryption\Crypt::keyEncrypt("ownCloud", $keypair['publicKey']);
|
|
|
|
if (!$view->is_dir('/control-file')) {
|
|
|
|
$view->mkdir('/control-file');
|
|
|
|
}
|
|
|
|
$view->file_put_contents('/control-file/controlfile.enc', $encryptedControlData);
|
|
|
|
|
2013-05-13 15:26:21 +00:00
|
|
|
\OC_FileProxy::$enabled = true;
|
|
|
|
|
2013-05-15 12:02:13 +00:00
|
|
|
// Set recoveryAdmin as enabled
|
|
|
|
OC_Appconfig::setValue('files_encryption', 'recoveryAdminEnabled', 1);
|
2013-05-13 15:26:21 +00:00
|
|
|
|
2013-05-15 12:02:13 +00:00
|
|
|
$return = true;
|
2013-05-13 15:26:21 +00:00
|
|
|
|
2013-05-15 12:02:13 +00:00
|
|
|
} else { // get recovery key and check the password
|
2013-05-15 14:12:20 +00:00
|
|
|
$util = new \OCA\Encryption\Util(new \OC_FilesystemView('/'), \OCP\User::getUser());
|
|
|
|
$return = $util->checkRecoveryPassword($_POST['recoveryPassword']);
|
2013-05-15 12:02:13 +00:00
|
|
|
if ($return) {
|
|
|
|
OC_Appconfig::setValue('files_encryption', 'recoveryAdminEnabled', 1);
|
|
|
|
}
|
|
|
|
}
|
2013-05-01 17:18:31 +00:00
|
|
|
|
|
|
|
// Disable recoveryAdmin
|
2013-05-13 15:26:21 +00:00
|
|
|
} elseif (
|
|
|
|
isset($_POST['adminEnableRecovery'])
|
|
|
|
&& 0 == $_POST['adminEnableRecovery']
|
2013-05-01 17:18:31 +00:00
|
|
|
) {
|
2013-05-15 14:12:20 +00:00
|
|
|
$util = new \OCA\Encryption\Util(new \OC_FilesystemView('/'), \OCP\User::getUser());
|
|
|
|
$return = $util->checkRecoveryPassword($_POST['recoveryPassword']);
|
2013-05-01 17:18:31 +00:00
|
|
|
|
2013-05-15 12:02:13 +00:00
|
|
|
if ($return) {
|
|
|
|
// Set recoveryAdmin as disabled
|
2013-05-13 15:26:21 +00:00
|
|
|
OC_Appconfig::setValue('files_encryption', 'recoveryAdminEnabled', 0);
|
2013-05-15 12:02:13 +00:00
|
|
|
}
|
2013-04-10 15:37:03 +00:00
|
|
|
}
|
|
|
|
|
2013-05-07 14:17:38 +00:00
|
|
|
// Return success or failure
|
|
|
|
( $return ) ? \OCP\JSON::success() : \OCP\JSON::error();
|