2013-03-28 21:29:18 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
\OCP\JSON::checkAppEnabled( 'files_encryption' );
|
|
|
|
\OCP\JSON::callCheck();
|
|
|
|
|
2013-04-10 19:37:03 +04:00
|
|
|
$return = $doSetup = false;
|
|
|
|
|
2013-05-01 21:18:31 +04:00
|
|
|
// Enable recoveryAdmin
|
2013-03-28 21:29:18 +04:00
|
|
|
if (
|
|
|
|
isset( $_POST['adminEnableRecovery'] )
|
2013-05-01 21:18:31 +04:00
|
|
|
&& 1 == $_POST['adminEnableRecovery']
|
|
|
|
// && isset( $_POST['recoveryPassword'] )
|
|
|
|
// && ! empty ( $_POST['recoveryPassword'] )
|
2013-03-28 21:29:18 +04:00
|
|
|
) {
|
|
|
|
|
|
|
|
// TODO: Let the admin set this themselves
|
|
|
|
$recoveryAdminUid = 'recoveryAdmin';
|
|
|
|
|
|
|
|
// If desired recoveryAdmin UID is already in use
|
|
|
|
if ( ! \OC_User::userExists( $recoveryAdminUid ) ) {
|
2013-05-01 21:18:31 +04:00
|
|
|
|
2013-03-28 21:29:18 +04:00
|
|
|
// Create new recoveryAdmin user
|
|
|
|
\OC_User::createUser( $recoveryAdminUid, $_POST['recoveryPassword'] );
|
|
|
|
|
2013-05-04 18:14:38 +04:00
|
|
|
// Make recovery user an administrator
|
|
|
|
\OC_Group::addToGroup ( $recoveryAdminUid, 'admin' );
|
|
|
|
|
2013-03-28 21:29:18 +04:00
|
|
|
$doSetup = true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Get list of admin users
|
|
|
|
$admins = OC_Group::usersInGroup( 'admin' );
|
|
|
|
|
|
|
|
// If the existing recoveryAdmin UID is an admin
|
|
|
|
if ( in_array( $recoveryAdminUid, $admins ) ) {
|
|
|
|
|
|
|
|
// The desired recoveryAdmi UID pre-exists and can be used
|
|
|
|
$doSetup = true;
|
|
|
|
|
|
|
|
// If the recoveryAdmin UID exists but doesn't have admin rights
|
|
|
|
} else {
|
|
|
|
|
2013-04-10 19:37:03 +04:00
|
|
|
$return = false;
|
2013-03-28 21:29:18 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-01 21:18:31 +04:00
|
|
|
// Setup recoveryAdmin user for encryption
|
2013-03-28 21:29:18 +04:00
|
|
|
if ( $doSetup ) {
|
|
|
|
|
|
|
|
$view = new \OC_FilesystemView( '/' );
|
2013-05-01 21:18:31 +04:00
|
|
|
$util = new \OCA\Encryption\Util( $view, $recoveryAdminUid );
|
2013-03-28 21:29:18 +04:00
|
|
|
|
|
|
|
// Ensure recoveryAdmin is ready for encryption (has usable keypair etc.)
|
|
|
|
$util->setupServerSide( $_POST['recoveryPassword'] );
|
|
|
|
|
|
|
|
// Store the UID in the DB
|
2013-04-10 19:37:03 +04:00
|
|
|
OC_Appconfig::setValue( 'files_encryption', 'recoveryAdminUid', $recoveryAdminUid );
|
2013-03-28 21:29:18 +04:00
|
|
|
|
2013-04-10 19:37:03 +04:00
|
|
|
$return = true;
|
2013-03-28 21:29:18 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-01 21:18:31 +04:00
|
|
|
// Set recoveryAdmin as enabled
|
|
|
|
OC_Appconfig::setValue( 'files_encryption', 'recoveryAdminEnabled', 1 );
|
|
|
|
|
|
|
|
// Disable recoveryAdmin
|
|
|
|
} elseif (
|
|
|
|
isset( $_POST['adminEnableRecovery'] )
|
|
|
|
&& 0 == $_POST['adminEnableRecovery']
|
|
|
|
) {
|
|
|
|
|
|
|
|
// Set recoveryAdmin as enabled
|
|
|
|
OC_Appconfig::setValue( 'files_encryption', 'recoveryAdminEnabled', 0 );
|
|
|
|
|
|
|
|
$return = true;
|
|
|
|
|
2013-04-10 19:37:03 +04:00
|
|
|
}
|
|
|
|
|
2013-05-07 18:17:38 +04:00
|
|
|
// Return success or failure
|
|
|
|
( $return ) ? \OCP\JSON::success() : \OCP\JSON::error();
|