Manager.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Encryption;
  27. use OC\Encryption\Keys\Storage;
  28. use OC\Files\Filesystem;
  29. use OC\Files\View;
  30. use OC\Memcache\ArrayCache;
  31. use OC\ServiceUnavailableException;
  32. use OCP\Encryption\IEncryptionModule;
  33. use OCP\Encryption\IManager;
  34. use OCP\Files\Mount\IMountPoint;
  35. use OCP\Files\Storage\IStorage;
  36. use OCP\IConfig;
  37. use OCP\IL10N;
  38. use Psr\Log\LoggerInterface;
  39. class Manager implements IManager {
  40. /** @var array */
  41. protected $encryptionModules;
  42. /** @var IConfig */
  43. protected $config;
  44. protected LoggerInterface $logger;
  45. /** @var Il10n */
  46. protected $l;
  47. /** @var View */
  48. protected $rootView;
  49. /** @var Util */
  50. protected $util;
  51. /** @var ArrayCache */
  52. protected $arrayCache;
  53. public function __construct(IConfig $config, LoggerInterface $logger, IL10N $l10n, View $rootView, Util $util, ArrayCache $arrayCache) {
  54. $this->encryptionModules = [];
  55. $this->config = $config;
  56. $this->logger = $logger;
  57. $this->l = $l10n;
  58. $this->rootView = $rootView;
  59. $this->util = $util;
  60. $this->arrayCache = $arrayCache;
  61. }
  62. /**
  63. * Check if encryption is enabled
  64. *
  65. * @return bool true if enabled, false if not
  66. */
  67. public function isEnabled() {
  68. $installed = $this->config->getSystemValueBool('installed', false);
  69. if (!$installed) {
  70. return false;
  71. }
  72. $enabled = $this->config->getAppValue('core', 'encryption_enabled', 'no');
  73. return $enabled === 'yes';
  74. }
  75. /**
  76. * check if new encryption is ready
  77. *
  78. * @return bool
  79. * @throws ServiceUnavailableException
  80. */
  81. public function isReady() {
  82. if ($this->isKeyStorageReady() === false) {
  83. throw new ServiceUnavailableException('Key Storage is not ready');
  84. }
  85. return true;
  86. }
  87. /**
  88. * @param string $user
  89. */
  90. public function isReadyForUser($user) {
  91. if (!$this->isReady()) {
  92. return false;
  93. }
  94. foreach ($this->getEncryptionModules() as $module) {
  95. /** @var IEncryptionModule $m */
  96. $m = call_user_func($module['callback']);
  97. if (!$m->isReadyForUser($user)) {
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. /**
  104. * Registers an callback function which must return an encryption module instance
  105. *
  106. * @param string $id
  107. * @param string $displayName
  108. * @param callable $callback
  109. * @throws Exceptions\ModuleAlreadyExistsException
  110. */
  111. public function registerEncryptionModule($id, $displayName, callable $callback) {
  112. if (isset($this->encryptionModules[$id])) {
  113. throw new Exceptions\ModuleAlreadyExistsException($id, $displayName);
  114. }
  115. $this->encryptionModules[$id] = [
  116. 'id' => $id,
  117. 'displayName' => $displayName,
  118. 'callback' => $callback,
  119. ];
  120. $defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId();
  121. if (empty($defaultEncryptionModuleId)) {
  122. $this->setDefaultEncryptionModule($id);
  123. }
  124. }
  125. /**
  126. * Unregisters an encryption module
  127. *
  128. * @param string $moduleId
  129. */
  130. public function unregisterEncryptionModule($moduleId) {
  131. unset($this->encryptionModules[$moduleId]);
  132. }
  133. /**
  134. * get a list of all encryption modules
  135. *
  136. * @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]]
  137. */
  138. public function getEncryptionModules() {
  139. return $this->encryptionModules;
  140. }
  141. /**
  142. * get a specific encryption module
  143. *
  144. * @param string $moduleId
  145. * @return IEncryptionModule
  146. * @throws Exceptions\ModuleDoesNotExistsException
  147. */
  148. public function getEncryptionModule($moduleId = '') {
  149. if (empty($moduleId)) {
  150. return $this->getDefaultEncryptionModule();
  151. }
  152. if (isset($this->encryptionModules[$moduleId])) {
  153. return call_user_func($this->encryptionModules[$moduleId]['callback']);
  154. }
  155. $message = "Module with ID: $moduleId does not exist.";
  156. $hint = $this->l->t('Module with ID: %s does not exist. Please enable it in your apps settings or contact your administrator.', [$moduleId]);
  157. throw new Exceptions\ModuleDoesNotExistsException($message, $hint);
  158. }
  159. /**
  160. * get default encryption module
  161. *
  162. * @return \OCP\Encryption\IEncryptionModule
  163. * @throws Exceptions\ModuleDoesNotExistsException
  164. */
  165. protected function getDefaultEncryptionModule() {
  166. $defaultModuleId = $this->getDefaultEncryptionModuleId();
  167. if (empty($defaultModuleId)) {
  168. $message = 'No default encryption module defined';
  169. throw new Exceptions\ModuleDoesNotExistsException($message);
  170. }
  171. if (isset($this->encryptionModules[$defaultModuleId])) {
  172. return call_user_func($this->encryptionModules[$defaultModuleId]['callback']);
  173. }
  174. $message = 'Default encryption module not loaded';
  175. throw new Exceptions\ModuleDoesNotExistsException($message);
  176. }
  177. /**
  178. * set default encryption module Id
  179. *
  180. * @param string $moduleId
  181. * @return bool
  182. */
  183. public function setDefaultEncryptionModule($moduleId) {
  184. try {
  185. $this->getEncryptionModule($moduleId);
  186. } catch (\Exception $e) {
  187. return false;
  188. }
  189. $this->config->setAppValue('core', 'default_encryption_module', $moduleId);
  190. return true;
  191. }
  192. /**
  193. * get default encryption module Id
  194. *
  195. * @return string
  196. */
  197. public function getDefaultEncryptionModuleId() {
  198. return $this->config->getAppValue('core', 'default_encryption_module');
  199. }
  200. /**
  201. * Add storage wrapper
  202. */
  203. public function setupStorage() {
  204. // If encryption is disabled and there are no loaded modules it makes no sense to load the wrapper
  205. if (!empty($this->encryptionModules) || $this->isEnabled()) {
  206. $encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger);
  207. Filesystem::addStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage'], 2);
  208. }
  209. }
  210. public function forceWrapStorage(IMountPoint $mountPoint, IStorage $storage) {
  211. $encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger);
  212. return $encryptionWrapper->wrapStorage($mountPoint->getMountPoint(), $storage, $mountPoint, true);
  213. }
  214. /**
  215. * check if key storage is ready
  216. *
  217. * @return bool
  218. */
  219. protected function isKeyStorageReady() {
  220. $rootDir = $this->util->getKeyStorageRoot();
  221. // the default root is always valid
  222. if ($rootDir === '') {
  223. return true;
  224. }
  225. // check if key storage is mounted correctly
  226. if ($this->rootView->file_exists($rootDir . '/' . Storage::KEY_STORAGE_MARKER)) {
  227. return true;
  228. }
  229. return false;
  230. }
  231. }