2015-03-24 17:29:10 -04:00
|
|
|
<?php
|
2024-05-28 10:42:42 -04:00
|
|
|
|
2015-03-24 17:29:10 -04:00
|
|
|
/**
|
2024-05-28 10:42:42 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-03-24 17:29:10 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Encryption;
|
|
|
|
|
|
2024-10-18 06:04:22 -04:00
|
|
|
use OC\Files\Storage\Storage;
|
2015-03-24 17:29:10 -04:00
|
|
|
use OC\Files\View;
|
|
|
|
|
use OCA\Encryption\Crypto\Crypt;
|
2024-10-18 06:40:17 -04:00
|
|
|
use OCP\Files\Storage\IStorage;
|
2015-03-24 17:29:10 -04:00
|
|
|
use OCP\IConfig;
|
|
|
|
|
use OCP\IUser;
|
2015-05-12 12:49:25 -04:00
|
|
|
use OCP\IUserManager;
|
2015-03-24 17:29:10 -04:00
|
|
|
use OCP\IUserSession;
|
|
|
|
|
use OCP\PreConditionNotMetException;
|
|
|
|
|
|
|
|
|
|
class Util {
|
2023-06-29 09:41:40 -04:00
|
|
|
private IUser|false $user;
|
2015-03-24 17:29:10 -04:00
|
|
|
|
2023-06-29 09:41:40 -04:00
|
|
|
public function __construct(
|
|
|
|
|
private View $files,
|
|
|
|
|
private Crypt $crypt,
|
|
|
|
|
IUserSession $userSession,
|
|
|
|
|
private IConfig $config,
|
|
|
|
|
private IUserManager $userManager,
|
2015-03-24 17:29:10 -04:00
|
|
|
) {
|
2023-06-29 09:41:40 -04:00
|
|
|
$this->user = $userSession->isLoggedIn() ? $userSession->getUser() : false;
|
2015-03-24 17:29:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-04-29 11:18:41 -04:00
|
|
|
* check if recovery key is enabled for user
|
|
|
|
|
*
|
|
|
|
|
* @param string $uid
|
2015-03-24 17:29:10 -04:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2015-04-29 11:18:41 -04:00
|
|
|
public function isRecoveryEnabledForUser($uid) {
|
|
|
|
|
$recoveryMode = $this->config->getUserValue($uid,
|
2015-03-24 17:29:10 -04:00
|
|
|
'encryption',
|
|
|
|
|
'recoveryEnabled',
|
2015-10-13 11:54:06 -04:00
|
|
|
'0');
|
2015-03-24 17:29:10 -04:00
|
|
|
|
|
|
|
|
return ($recoveryMode === '1');
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-13 11:54:06 -04:00
|
|
|
/**
|
|
|
|
|
* check if the home storage should be encrypted
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function shouldEncryptHomeStorage() {
|
|
|
|
|
$encryptHomeStorage = $this->config->getAppValue(
|
|
|
|
|
'encryption',
|
|
|
|
|
'encryptHomeStorage',
|
|
|
|
|
'1'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return ($encryptHomeStorage === '1');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:02:55 -05:00
|
|
|
* set the home storage encryption on/off
|
2015-10-13 11:54:06 -04:00
|
|
|
*
|
|
|
|
|
* @param bool $encryptHomeStorage
|
|
|
|
|
*/
|
|
|
|
|
public function setEncryptHomeStorage($encryptHomeStorage) {
|
|
|
|
|
$value = $encryptHomeStorage ? '1' : '0';
|
|
|
|
|
$this->config->setAppValue(
|
|
|
|
|
'encryption',
|
|
|
|
|
'encryptHomeStorage',
|
|
|
|
|
$value
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-07 05:38:44 -04:00
|
|
|
/**
|
|
|
|
|
* check if master key is enabled
|
|
|
|
|
*/
|
2023-06-29 09:41:40 -04:00
|
|
|
public function isMasterKeyEnabled(): bool {
|
2017-05-30 05:40:40 -04:00
|
|
|
$userMasterKey = $this->config->getAppValue('encryption', 'useMasterKey', '1');
|
2015-09-07 05:38:44 -04:00
|
|
|
return ($userMasterKey === '1');
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-24 17:29:10 -04:00
|
|
|
/**
|
|
|
|
|
* @param $enabled
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function setRecoveryForUser($enabled) {
|
|
|
|
|
$value = $enabled ? '1' : '0';
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->config->setUserValue($this->user->getUID(),
|
|
|
|
|
'encryption',
|
|
|
|
|
'recoveryEnabled',
|
|
|
|
|
$value);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (PreConditionNotMetException $e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $uid
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function userHasFiles($uid) {
|
|
|
|
|
return $this->files->file_exists($uid . '/files');
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 12:49:25 -04:00
|
|
|
/**
|
|
|
|
|
* get owner from give path, path relative to data/ expected
|
|
|
|
|
*
|
|
|
|
|
* @param string $path relative to data/
|
|
|
|
|
* @return string
|
|
|
|
|
* @throws \BadMethodCallException
|
|
|
|
|
*/
|
|
|
|
|
public function getOwner($path) {
|
|
|
|
|
$owner = '';
|
|
|
|
|
$parts = explode('/', $path, 3);
|
|
|
|
|
if (count($parts) > 1) {
|
|
|
|
|
$owner = $parts[1];
|
|
|
|
|
if ($this->userManager->userExists($owner) === false) {
|
2025-06-30 09:04:05 -04:00
|
|
|
throw new \BadMethodCallException('Unknown user: '
|
|
|
|
|
. 'method expects path to a user folder relative to the data folder');
|
2015-05-12 12:49:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $owner;
|
|
|
|
|
}
|
2015-03-24 17:29:10 -04:00
|
|
|
|
2024-10-18 06:40:17 -04:00
|
|
|
public function getStorage(string $path): ?IStorage {
|
2018-01-25 18:02:03 -05:00
|
|
|
return $this->files->getMount($path)->getStorage();
|
2015-10-13 11:54:06 -04:00
|
|
|
}
|
2024-09-24 17:37:18 -04:00
|
|
|
|
2015-03-24 17:29:10 -04:00
|
|
|
}
|