123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- namespace OC\Encryption;
- use OC\Encryption\Keys\Storage;
- use OC\Files\Filesystem;
- use OC\Files\View;
- use OC\Memcache\ArrayCache;
- use OC\ServiceUnavailableException;
- use OCP\Encryption\IEncryptionModule;
- use OCP\Encryption\IManager;
- use OCP\Files\Mount\IMountPoint;
- use OCP\Files\Storage\IStorage;
- use OCP\IConfig;
- use OCP\IL10N;
- use Psr\Log\LoggerInterface;
- class Manager implements IManager {
- protected array $encryptionModules;
- public function __construct(
- protected IConfig $config,
- protected LoggerInterface $logger,
- protected IL10N $l,
- protected View $rootView,
- protected Util $util,
- protected ArrayCache $arrayCache,
- ) {
- $this->encryptionModules = [];
- }
-
- public function isEnabled() {
- $installed = $this->config->getSystemValueBool('installed', false);
- if (!$installed) {
- return false;
- }
- $enabled = $this->config->getAppValue('core', 'encryption_enabled', 'no');
- return $enabled === 'yes';
- }
-
- public function isReady() {
- if ($this->isKeyStorageReady() === false) {
- throw new ServiceUnavailableException('Key Storage is not ready');
- }
- return true;
- }
-
- public function isReadyForUser($user) {
- if (!$this->isReady()) {
- return false;
- }
- foreach ($this->getEncryptionModules() as $module) {
-
- $m = call_user_func($module['callback']);
- if (!$m->isReadyForUser($user)) {
- return false;
- }
- }
- return true;
- }
-
- public function registerEncryptionModule($id, $displayName, callable $callback) {
- if (isset($this->encryptionModules[$id])) {
- throw new Exceptions\ModuleAlreadyExistsException($id, $displayName);
- }
- $this->encryptionModules[$id] = [
- 'id' => $id,
- 'displayName' => $displayName,
- 'callback' => $callback,
- ];
- $defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId();
- if (empty($defaultEncryptionModuleId)) {
- $this->setDefaultEncryptionModule($id);
- }
- }
-
- public function unregisterEncryptionModule($moduleId) {
- unset($this->encryptionModules[$moduleId]);
- }
-
- public function getEncryptionModules() {
- return $this->encryptionModules;
- }
-
- public function getEncryptionModule($moduleId = '') {
- if (empty($moduleId)) {
- return $this->getDefaultEncryptionModule();
- }
- if (isset($this->encryptionModules[$moduleId])) {
- return call_user_func($this->encryptionModules[$moduleId]['callback']);
- }
- $message = "Module with ID: $moduleId does not exist.";
- $hint = $this->l->t('Module with ID: %s does not exist. Please enable it in your apps settings or contact your administrator.', [$moduleId]);
- throw new Exceptions\ModuleDoesNotExistsException($message, $hint);
- }
-
- protected function getDefaultEncryptionModule() {
- $defaultModuleId = $this->getDefaultEncryptionModuleId();
- if (empty($defaultModuleId)) {
- $message = 'No default encryption module defined';
- throw new Exceptions\ModuleDoesNotExistsException($message);
- }
- if (isset($this->encryptionModules[$defaultModuleId])) {
- return call_user_func($this->encryptionModules[$defaultModuleId]['callback']);
- }
- $message = 'Default encryption module not loaded';
- throw new Exceptions\ModuleDoesNotExistsException($message);
- }
-
- public function setDefaultEncryptionModule($moduleId) {
- try {
- $this->getEncryptionModule($moduleId);
- } catch (\Exception $e) {
- return false;
- }
- $this->config->setAppValue('core', 'default_encryption_module', $moduleId);
- return true;
- }
-
- public function getDefaultEncryptionModuleId() {
- return $this->config->getAppValue('core', 'default_encryption_module');
- }
-
- public function setupStorage() {
-
- if (!empty($this->encryptionModules) || $this->isEnabled()) {
- $encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger);
- Filesystem::addStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage'], 2);
- }
- }
- public function forceWrapStorage(IMountPoint $mountPoint, IStorage $storage) {
- $encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger);
- return $encryptionWrapper->wrapStorage($mountPoint->getMountPoint(), $storage, $mountPoint, true);
- }
-
- protected function isKeyStorageReady() {
- $rootDir = $this->util->getKeyStorageRoot();
-
- if ($rootDir === '') {
- return true;
- }
-
- if ($this->rootView->file_exists($rootDir . '/' . Storage::KEY_STORAGE_MARKER)) {
- return true;
- }
- return false;
- }
- }
|