You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Manager.php 7.1KB

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