@@ -38,251 +38,251 @@ |
||
| 38 | 38 | |
| 39 | 39 | class Manager implements IManager { |
| 40 | 40 | |
| 41 | - /** @var array */ |
|
| 42 | - protected $encryptionModules; |
|
| 43 | - |
|
| 44 | - /** @var IConfig */ |
|
| 45 | - protected $config; |
|
| 46 | - |
|
| 47 | - /** @var ILogger */ |
|
| 48 | - protected $logger; |
|
| 49 | - |
|
| 50 | - /** @var Il10n */ |
|
| 51 | - protected $l; |
|
| 52 | - |
|
| 53 | - /** @var View */ |
|
| 54 | - protected $rootView; |
|
| 55 | - |
|
| 56 | - /** @var Util */ |
|
| 57 | - protected $util; |
|
| 58 | - |
|
| 59 | - /** @var ArrayCache */ |
|
| 60 | - protected $arrayCache; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @param IConfig $config |
|
| 64 | - * @param ILogger $logger |
|
| 65 | - * @param IL10N $l10n |
|
| 66 | - * @param View $rootView |
|
| 67 | - * @param Util $util |
|
| 68 | - * @param ArrayCache $arrayCache |
|
| 69 | - */ |
|
| 70 | - public function __construct(IConfig $config, ILogger $logger, IL10N $l10n, View $rootView, Util $util, ArrayCache $arrayCache) { |
|
| 71 | - $this->encryptionModules = array(); |
|
| 72 | - $this->config = $config; |
|
| 73 | - $this->logger = $logger; |
|
| 74 | - $this->l = $l10n; |
|
| 75 | - $this->rootView = $rootView; |
|
| 76 | - $this->util = $util; |
|
| 77 | - $this->arrayCache = $arrayCache; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Check if encryption is enabled |
|
| 82 | - * |
|
| 83 | - * @return bool true if enabled, false if not |
|
| 84 | - */ |
|
| 85 | - public function isEnabled() { |
|
| 86 | - |
|
| 87 | - $installed = $this->config->getSystemValue('installed', false); |
|
| 88 | - if (!$installed) { |
|
| 89 | - return false; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $enabled = $this->config->getAppValue('core', 'encryption_enabled', 'no'); |
|
| 93 | - return $enabled === 'yes'; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * check if new encryption is ready |
|
| 98 | - * |
|
| 99 | - * @return bool |
|
| 100 | - * @throws ServiceUnavailableException |
|
| 101 | - */ |
|
| 102 | - public function isReady() { |
|
| 103 | - // check if we are still in transit between the old and the new encryption |
|
| 104 | - $oldEncryption = $this->config->getAppValue('files_encryption', 'installed_version'); |
|
| 105 | - if (!empty($oldEncryption)) { |
|
| 106 | - $warning = 'Installation is in transit between the old Encryption (ownCloud <= 8.0) |
|
| 41 | + /** @var array */ |
|
| 42 | + protected $encryptionModules; |
|
| 43 | + |
|
| 44 | + /** @var IConfig */ |
|
| 45 | + protected $config; |
|
| 46 | + |
|
| 47 | + /** @var ILogger */ |
|
| 48 | + protected $logger; |
|
| 49 | + |
|
| 50 | + /** @var Il10n */ |
|
| 51 | + protected $l; |
|
| 52 | + |
|
| 53 | + /** @var View */ |
|
| 54 | + protected $rootView; |
|
| 55 | + |
|
| 56 | + /** @var Util */ |
|
| 57 | + protected $util; |
|
| 58 | + |
|
| 59 | + /** @var ArrayCache */ |
|
| 60 | + protected $arrayCache; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @param IConfig $config |
|
| 64 | + * @param ILogger $logger |
|
| 65 | + * @param IL10N $l10n |
|
| 66 | + * @param View $rootView |
|
| 67 | + * @param Util $util |
|
| 68 | + * @param ArrayCache $arrayCache |
|
| 69 | + */ |
|
| 70 | + public function __construct(IConfig $config, ILogger $logger, IL10N $l10n, View $rootView, Util $util, ArrayCache $arrayCache) { |
|
| 71 | + $this->encryptionModules = array(); |
|
| 72 | + $this->config = $config; |
|
| 73 | + $this->logger = $logger; |
|
| 74 | + $this->l = $l10n; |
|
| 75 | + $this->rootView = $rootView; |
|
| 76 | + $this->util = $util; |
|
| 77 | + $this->arrayCache = $arrayCache; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Check if encryption is enabled |
|
| 82 | + * |
|
| 83 | + * @return bool true if enabled, false if not |
|
| 84 | + */ |
|
| 85 | + public function isEnabled() { |
|
| 86 | + |
|
| 87 | + $installed = $this->config->getSystemValue('installed', false); |
|
| 88 | + if (!$installed) { |
|
| 89 | + return false; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $enabled = $this->config->getAppValue('core', 'encryption_enabled', 'no'); |
|
| 93 | + return $enabled === 'yes'; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * check if new encryption is ready |
|
| 98 | + * |
|
| 99 | + * @return bool |
|
| 100 | + * @throws ServiceUnavailableException |
|
| 101 | + */ |
|
| 102 | + public function isReady() { |
|
| 103 | + // check if we are still in transit between the old and the new encryption |
|
| 104 | + $oldEncryption = $this->config->getAppValue('files_encryption', 'installed_version'); |
|
| 105 | + if (!empty($oldEncryption)) { |
|
| 106 | + $warning = 'Installation is in transit between the old Encryption (ownCloud <= 8.0) |
|
| 107 | 107 | and the new encryption. Please enable the "Default encryption module" |
| 108 | 108 | and run \'occ encryption:migrate\''; |
| 109 | - $this->logger->warning($warning); |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - if ($this->isKeyStorageReady() === false) { |
|
| 114 | - throw new ServiceUnavailableException('Key Storage is not ready'); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - return true; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @param string $user |
|
| 122 | - */ |
|
| 123 | - public function isReadyForUser($user) { |
|
| 124 | - if (!$this->isReady()) { |
|
| 125 | - return false; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - foreach ($this->getEncryptionModules() as $module) { |
|
| 129 | - /** @var IEncryptionModule $m */ |
|
| 130 | - $m = call_user_func($module['callback']); |
|
| 131 | - if (!$m->isReadyForUser($user)) { |
|
| 132 | - return false; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - return true; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Registers an callback function which must return an encryption module instance |
|
| 141 | - * |
|
| 142 | - * @param string $id |
|
| 143 | - * @param string $displayName |
|
| 144 | - * @param callable $callback |
|
| 145 | - * @throws Exceptions\ModuleAlreadyExistsException |
|
| 146 | - */ |
|
| 147 | - public function registerEncryptionModule($id, $displayName, callable $callback) { |
|
| 148 | - |
|
| 149 | - if (isset($this->encryptionModules[$id])) { |
|
| 150 | - throw new Exceptions\ModuleAlreadyExistsException($id, $displayName); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - $this->encryptionModules[$id] = [ |
|
| 154 | - 'id' => $id, |
|
| 155 | - 'displayName' => $displayName, |
|
| 156 | - 'callback' => $callback, |
|
| 157 | - ]; |
|
| 158 | - |
|
| 159 | - $defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId(); |
|
| 160 | - |
|
| 161 | - if (empty($defaultEncryptionModuleId)) { |
|
| 162 | - $this->setDefaultEncryptionModule($id); |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Unregisters an encryption module |
|
| 168 | - * |
|
| 169 | - * @param string $moduleId |
|
| 170 | - */ |
|
| 171 | - public function unregisterEncryptionModule($moduleId) { |
|
| 172 | - unset($this->encryptionModules[$moduleId]); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * get a list of all encryption modules |
|
| 177 | - * |
|
| 178 | - * @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]] |
|
| 179 | - */ |
|
| 180 | - public function getEncryptionModules() { |
|
| 181 | - return $this->encryptionModules; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * get a specific encryption module |
|
| 186 | - * |
|
| 187 | - * @param string $moduleId |
|
| 188 | - * @return IEncryptionModule |
|
| 189 | - * @throws Exceptions\ModuleDoesNotExistsException |
|
| 190 | - */ |
|
| 191 | - public function getEncryptionModule($moduleId = '') { |
|
| 192 | - if (!empty($moduleId)) { |
|
| 193 | - if (isset($this->encryptionModules[$moduleId])) { |
|
| 194 | - return call_user_func($this->encryptionModules[$moduleId]['callback']); |
|
| 195 | - } else { |
|
| 196 | - $message = "Module with id: $moduleId does not exist."; |
|
| 197 | - $hint = $this->l->t('Module with id: %s does not exist. Please enable it in your apps settings or contact your administrator.', [$moduleId]); |
|
| 198 | - throw new Exceptions\ModuleDoesNotExistsException($message, $hint); |
|
| 199 | - } |
|
| 200 | - } else { |
|
| 201 | - return $this->getDefaultEncryptionModule(); |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * get default encryption module |
|
| 207 | - * |
|
| 208 | - * @return \OCP\Encryption\IEncryptionModule |
|
| 209 | - * @throws Exceptions\ModuleDoesNotExistsException |
|
| 210 | - */ |
|
| 211 | - protected function getDefaultEncryptionModule() { |
|
| 212 | - $defaultModuleId = $this->getDefaultEncryptionModuleId(); |
|
| 213 | - if (!empty($defaultModuleId)) { |
|
| 214 | - if (isset($this->encryptionModules[$defaultModuleId])) { |
|
| 215 | - return call_user_func($this->encryptionModules[$defaultModuleId]['callback']); |
|
| 216 | - } else { |
|
| 217 | - $message = 'Default encryption module not loaded'; |
|
| 218 | - throw new Exceptions\ModuleDoesNotExistsException($message); |
|
| 219 | - } |
|
| 220 | - } else { |
|
| 221 | - $message = 'No default encryption module defined'; |
|
| 222 | - throw new Exceptions\ModuleDoesNotExistsException($message); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * set default encryption module Id |
|
| 229 | - * |
|
| 230 | - * @param string $moduleId |
|
| 231 | - * @return bool |
|
| 232 | - */ |
|
| 233 | - public function setDefaultEncryptionModule($moduleId) { |
|
| 234 | - try { |
|
| 235 | - $this->getEncryptionModule($moduleId); |
|
| 236 | - } catch (\Exception $e) { |
|
| 237 | - return false; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - $this->config->setAppValue('core', 'default_encryption_module', $moduleId); |
|
| 241 | - return true; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * get default encryption module Id |
|
| 246 | - * |
|
| 247 | - * @return string |
|
| 248 | - */ |
|
| 249 | - public function getDefaultEncryptionModuleId() { |
|
| 250 | - return $this->config->getAppValue('core', 'default_encryption_module'); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Add storage wrapper |
|
| 255 | - */ |
|
| 256 | - public function setupStorage() { |
|
| 257 | - // If encryption is disabled and there are no loaded modules it makes no sense to load the wrapper |
|
| 258 | - if (!empty($this->encryptionModules) || $this->isEnabled()) { |
|
| 259 | - $encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger); |
|
| 260 | - Filesystem::addStorageWrapper('oc_encryption', array($encryptionWrapper, 'wrapStorage'), 2); |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * check if key storage is ready |
|
| 267 | - * |
|
| 268 | - * @return bool |
|
| 269 | - */ |
|
| 270 | - protected function isKeyStorageReady() { |
|
| 271 | - |
|
| 272 | - $rootDir = $this->util->getKeyStorageRoot(); |
|
| 273 | - |
|
| 274 | - // the default root is always valid |
|
| 275 | - if ($rootDir === '') { |
|
| 276 | - return true; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - // check if key storage is mounted correctly |
|
| 280 | - if ($this->rootView->file_exists($rootDir . '/' . Storage::KEY_STORAGE_MARKER)) { |
|
| 281 | - return true; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - return false; |
|
| 285 | - } |
|
| 109 | + $this->logger->warning($warning); |
|
| 110 | + return false; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + if ($this->isKeyStorageReady() === false) { |
|
| 114 | + throw new ServiceUnavailableException('Key Storage is not ready'); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + return true; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @param string $user |
|
| 122 | + */ |
|
| 123 | + public function isReadyForUser($user) { |
|
| 124 | + if (!$this->isReady()) { |
|
| 125 | + return false; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + foreach ($this->getEncryptionModules() as $module) { |
|
| 129 | + /** @var IEncryptionModule $m */ |
|
| 130 | + $m = call_user_func($module['callback']); |
|
| 131 | + if (!$m->isReadyForUser($user)) { |
|
| 132 | + return false; |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + return true; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Registers an callback function which must return an encryption module instance |
|
| 141 | + * |
|
| 142 | + * @param string $id |
|
| 143 | + * @param string $displayName |
|
| 144 | + * @param callable $callback |
|
| 145 | + * @throws Exceptions\ModuleAlreadyExistsException |
|
| 146 | + */ |
|
| 147 | + public function registerEncryptionModule($id, $displayName, callable $callback) { |
|
| 148 | + |
|
| 149 | + if (isset($this->encryptionModules[$id])) { |
|
| 150 | + throw new Exceptions\ModuleAlreadyExistsException($id, $displayName); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + $this->encryptionModules[$id] = [ |
|
| 154 | + 'id' => $id, |
|
| 155 | + 'displayName' => $displayName, |
|
| 156 | + 'callback' => $callback, |
|
| 157 | + ]; |
|
| 158 | + |
|
| 159 | + $defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId(); |
|
| 160 | + |
|
| 161 | + if (empty($defaultEncryptionModuleId)) { |
|
| 162 | + $this->setDefaultEncryptionModule($id); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Unregisters an encryption module |
|
| 168 | + * |
|
| 169 | + * @param string $moduleId |
|
| 170 | + */ |
|
| 171 | + public function unregisterEncryptionModule($moduleId) { |
|
| 172 | + unset($this->encryptionModules[$moduleId]); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * get a list of all encryption modules |
|
| 177 | + * |
|
| 178 | + * @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]] |
|
| 179 | + */ |
|
| 180 | + public function getEncryptionModules() { |
|
| 181 | + return $this->encryptionModules; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * get a specific encryption module |
|
| 186 | + * |
|
| 187 | + * @param string $moduleId |
|
| 188 | + * @return IEncryptionModule |
|
| 189 | + * @throws Exceptions\ModuleDoesNotExistsException |
|
| 190 | + */ |
|
| 191 | + public function getEncryptionModule($moduleId = '') { |
|
| 192 | + if (!empty($moduleId)) { |
|
| 193 | + if (isset($this->encryptionModules[$moduleId])) { |
|
| 194 | + return call_user_func($this->encryptionModules[$moduleId]['callback']); |
|
| 195 | + } else { |
|
| 196 | + $message = "Module with id: $moduleId does not exist."; |
|
| 197 | + $hint = $this->l->t('Module with id: %s does not exist. Please enable it in your apps settings or contact your administrator.', [$moduleId]); |
|
| 198 | + throw new Exceptions\ModuleDoesNotExistsException($message, $hint); |
|
| 199 | + } |
|
| 200 | + } else { |
|
| 201 | + return $this->getDefaultEncryptionModule(); |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * get default encryption module |
|
| 207 | + * |
|
| 208 | + * @return \OCP\Encryption\IEncryptionModule |
|
| 209 | + * @throws Exceptions\ModuleDoesNotExistsException |
|
| 210 | + */ |
|
| 211 | + protected function getDefaultEncryptionModule() { |
|
| 212 | + $defaultModuleId = $this->getDefaultEncryptionModuleId(); |
|
| 213 | + if (!empty($defaultModuleId)) { |
|
| 214 | + if (isset($this->encryptionModules[$defaultModuleId])) { |
|
| 215 | + return call_user_func($this->encryptionModules[$defaultModuleId]['callback']); |
|
| 216 | + } else { |
|
| 217 | + $message = 'Default encryption module not loaded'; |
|
| 218 | + throw new Exceptions\ModuleDoesNotExistsException($message); |
|
| 219 | + } |
|
| 220 | + } else { |
|
| 221 | + $message = 'No default encryption module defined'; |
|
| 222 | + throw new Exceptions\ModuleDoesNotExistsException($message); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * set default encryption module Id |
|
| 229 | + * |
|
| 230 | + * @param string $moduleId |
|
| 231 | + * @return bool |
|
| 232 | + */ |
|
| 233 | + public function setDefaultEncryptionModule($moduleId) { |
|
| 234 | + try { |
|
| 235 | + $this->getEncryptionModule($moduleId); |
|
| 236 | + } catch (\Exception $e) { |
|
| 237 | + return false; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + $this->config->setAppValue('core', 'default_encryption_module', $moduleId); |
|
| 241 | + return true; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * get default encryption module Id |
|
| 246 | + * |
|
| 247 | + * @return string |
|
| 248 | + */ |
|
| 249 | + public function getDefaultEncryptionModuleId() { |
|
| 250 | + return $this->config->getAppValue('core', 'default_encryption_module'); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Add storage wrapper |
|
| 255 | + */ |
|
| 256 | + public function setupStorage() { |
|
| 257 | + // If encryption is disabled and there are no loaded modules it makes no sense to load the wrapper |
|
| 258 | + if (!empty($this->encryptionModules) || $this->isEnabled()) { |
|
| 259 | + $encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger); |
|
| 260 | + Filesystem::addStorageWrapper('oc_encryption', array($encryptionWrapper, 'wrapStorage'), 2); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * check if key storage is ready |
|
| 267 | + * |
|
| 268 | + * @return bool |
|
| 269 | + */ |
|
| 270 | + protected function isKeyStorageReady() { |
|
| 271 | + |
|
| 272 | + $rootDir = $this->util->getKeyStorageRoot(); |
|
| 273 | + |
|
| 274 | + // the default root is always valid |
|
| 275 | + if ($rootDir === '') { |
|
| 276 | + return true; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + // check if key storage is mounted correctly |
|
| 280 | + if ($this->rootView->file_exists($rootDir . '/' . Storage::KEY_STORAGE_MARKER)) { |
|
| 281 | + return true; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + return false; |
|
| 285 | + } |
|
| 286 | 286 | |
| 287 | 287 | |
| 288 | 288 | } |