aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_encryption/ajax/adminrecovery.php
blob: 157fc8f313c466f150d793e2d98e94810b70de8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
setValue( $app, $key, $value )

<?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();

$return = $doSetup = false;

// Enable recoveryAdmin
if ( 
	isset( $_POST['adminEnableRecovery'] ) 
	&& 1 == $_POST['adminEnableRecovery'] 
// 	&& isset( $_POST['recoveryPassword'] ) 
// 	&& ! empty ( $_POST['recoveryPassword'] )
) {

	// TODO: Let the admin set this themselves
	$recoveryAdminUid = 'recoveryAdmin';
	
	// If desired recoveryAdmin UID is already in use
	if ( ! \OC_User::userExists( $recoveryAdminUid ) ) {
		
		// Create new recoveryAdmin user
		\OC_User::createUser( $recoveryAdminUid, $_POST['recoveryPassword'] );
		
		$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 {
		
			$return = false;
			
		}
		
	}
	
	// Setup recoveryAdmin user for encryption
	if ( $doSetup ) {
		
		$view = new \OC_FilesystemView( '/' );
		$util = new \OCA\Encryption\Util( $view, $recoveryAdminUid );
		
		// Ensure recoveryAdmin is ready for encryption (has usable keypair etc.)
		$util->setupServerSide( $_POST['recoveryPassword'] );
		
		// Store the UID in the DB
		OC_Appconfig::setValue( 'files_encryption', 'recoveryAdminUid', $recoveryAdminUid );
		
		$return = true;
		
	}
	
	// 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;

}

($return) ? OC_JSON::success() : OC_JSON::error();