manager.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Thomas Müller <thomas.mueller@tmit.eu>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Encryption;
  23. use OC\Files\Storage\Wrapper\Encryption;
  24. use OCP\Encryption\IEncryptionModule;
  25. use OCP\Files\Mount\IMountPoint;
  26. class Manager implements \OCP\Encryption\IManager {
  27. /** @var array */
  28. protected $encryptionModules;
  29. /** @var \OCP\IConfig */
  30. protected $config;
  31. /**
  32. * @param \OCP\IConfig $config
  33. */
  34. public function __construct(\OCP\IConfig $config) {
  35. $this->encryptionModules = array();
  36. $this->config = $config;
  37. }
  38. /**
  39. * Check if encryption is enabled
  40. *
  41. * @return bool true if enabled, false if not
  42. */
  43. public function isEnabled() {
  44. $installed = $this->config->getSystemValue('installed', false);
  45. if (!$installed) {
  46. return false;
  47. }
  48. $enabled = $this->config->getAppValue('core', 'encryption_enabled', 'no');
  49. return $enabled === 'yes';
  50. }
  51. /**
  52. * Registers an encryption module
  53. *
  54. * @param IEncryptionModule $module
  55. * @throws Exceptions\ModuleAlreadyExistsException
  56. */
  57. public function registerEncryptionModule(IEncryptionModule $module) {
  58. $id = $module->getId();
  59. $name = $module->getDisplayName();
  60. if (isset($this->encryptionModules[$id])) {
  61. throw new Exceptions\ModuleAlreadyExistsException($id, $name);
  62. }
  63. $defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId();
  64. if (empty($defaultEncryptionModuleId)) {
  65. $this->setDefaultEncryptionModule($id);
  66. }
  67. $this->encryptionModules[$id] = $module;
  68. }
  69. /**
  70. * Unregisters an encryption module
  71. *
  72. * @param IEncryptionModule $module
  73. */
  74. public function unregisterEncryptionModule(IEncryptionModule $module) {
  75. unset($this->encryptionModules[$module->getId()]);
  76. }
  77. /**
  78. * get a list of all encryption modules
  79. *
  80. * @return IEncryptionModule[]
  81. */
  82. public function getEncryptionModules() {
  83. return $this->encryptionModules;
  84. }
  85. /**
  86. * get a specific encryption module
  87. *
  88. * @param string $moduleId
  89. * @return IEncryptionModule
  90. * @throws Exceptions\ModuleDoesNotExistsException
  91. */
  92. public function getEncryptionModule($moduleId = '') {
  93. if (!empty($moduleId)) {
  94. if (isset($this->encryptionModules[$moduleId])) {
  95. return $this->encryptionModules[$moduleId];
  96. } else {
  97. $message = "Module with id: $moduleId does not exists.";
  98. throw new Exceptions\ModuleDoesNotExistsException($message);
  99. }
  100. } else { // get default module and return this
  101. // For now we simply return the first module until we have a way
  102. // to enable multiple modules and define a default module
  103. $module = reset($this->encryptionModules);
  104. if ($module) {
  105. return $module;
  106. } else {
  107. $message = 'No encryption module registered';
  108. throw new Exceptions\ModuleDoesNotExistsException($message);
  109. }
  110. }
  111. }
  112. /**
  113. * get default encryption module
  114. *
  115. * @return \OCP\Encryption\IEncryptionModule
  116. * @throws Exceptions\ModuleDoesNotExistsException
  117. */
  118. public function getDefaultEncryptionModule() {
  119. $defaultModuleId = $this->getDefaultEncryptionModuleId();
  120. if (!empty($defaultModuleId)) {
  121. if (isset($this->encryptionModules[$defaultModuleId])) {
  122. return $this->encryptionModules[$defaultModuleId];
  123. } else {
  124. $message = 'Default encryption module not loaded';
  125. throw new Exceptions\ModuleDoesNotExistsException($message);
  126. }
  127. } else {
  128. $message = 'No default encryption module defined';
  129. throw new Exceptions\ModuleDoesNotExistsException($message);
  130. }
  131. }
  132. /**
  133. * set default encryption module Id
  134. *
  135. * @param string $moduleId
  136. * @return bool
  137. */
  138. public function setDefaultEncryptionModule($moduleId) {
  139. try {
  140. $this->config->setAppValue('core', 'default_encryption_module', $moduleId);
  141. return true;
  142. } catch (\Exception $e) {
  143. return false;
  144. }
  145. }
  146. /**
  147. * get default encryption module Id
  148. *
  149. * @return string
  150. */
  151. protected function getDefaultEncryptionModuleId() {
  152. try {
  153. return $this->config->getAppValue('core', 'default_encryption_module');
  154. } catch (\Exception $e) {
  155. return '';
  156. }
  157. }
  158. public static function setupStorage() {
  159. \OC\Files\Filesystem::addStorageWrapper('oc_encryption', function ($mountPoint, $storage, IMountPoint $mount) {
  160. $parameters = [
  161. 'storage' => $storage,
  162. 'mountPoint' => $mountPoint,
  163. 'mount' => $mount];
  164. if (!($storage instanceof \OC\Files\Storage\Shared)) {
  165. $manager = \OC::$server->getEncryptionManager();
  166. $util = new \OC\Encryption\Util(
  167. new \OC\Files\View(), \OC::$server->getUserManager(), \OC::$server->getConfig());
  168. $user = \OC::$server->getUserSession()->getUser();
  169. $logger = \OC::$server->getLogger();
  170. $uid = $user ? $user->getUID() : null;
  171. $fileHelper = \OC::$server->getEncryptionFilesHelper();
  172. return new Encryption($parameters, $manager, $util, $logger, $fileHelper, $uid);
  173. } else {
  174. return $storage;
  175. }
  176. }, 2);
  177. }
  178. }