Storage.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Encryption\Keys;
  25. use OC\Encryption\Util;
  26. use OC\Files\Filesystem;
  27. use OC\Files\View;
  28. use OCP\Encryption\Keys\IStorage;
  29. use OC\User\NoUserException;
  30. class Storage implements IStorage {
  31. // hidden file which indicate that the folder is a valid key storage
  32. const KEY_STORAGE_MARKER = '.oc_key_storage';
  33. /** @var View */
  34. private $view;
  35. /** @var Util */
  36. private $util;
  37. // base dir where all the file related keys are stored
  38. /** @var string */
  39. private $keys_base_dir;
  40. // root of the key storage default is empty which means that we use the data folder
  41. /** @var string */
  42. private $root_dir;
  43. /** @var string */
  44. private $encryption_base_dir;
  45. /** @var string */
  46. private $backup_base_dir;
  47. /** @var array */
  48. private $keyCache = [];
  49. /**
  50. * @param View $view
  51. * @param Util $util
  52. */
  53. public function __construct(View $view, Util $util) {
  54. $this->view = $view;
  55. $this->util = $util;
  56. $this->encryption_base_dir = '/files_encryption';
  57. $this->keys_base_dir = $this->encryption_base_dir .'/keys';
  58. $this->backup_base_dir = $this->encryption_base_dir .'/backup';
  59. $this->root_dir = $this->util->getKeyStorageRoot();
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function getUserKey($uid, $keyId, $encryptionModuleId) {
  65. $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
  66. return $this->getKey($path);
  67. }
  68. /**
  69. * @inheritdoc
  70. */
  71. public function getFileKey($path, $keyId, $encryptionModuleId) {
  72. $realFile = $this->util->stripPartialFileExtension($path);
  73. $keyDir = $this->getFileKeyDir($encryptionModuleId, $realFile);
  74. $key = $this->getKey($keyDir . $keyId);
  75. if ($key === '' && $realFile !== $path) {
  76. // Check if the part file has keys and use them, if no normal keys
  77. // exist. This is required to fix copyBetweenStorage() when we
  78. // rename a .part file over storage borders.
  79. $keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
  80. $key = $this->getKey($keyDir . $keyId);
  81. }
  82. return $key;
  83. }
  84. /**
  85. * @inheritdoc
  86. */
  87. public function getSystemUserKey($keyId, $encryptionModuleId) {
  88. $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
  89. return $this->getKey($path);
  90. }
  91. /**
  92. * @inheritdoc
  93. */
  94. public function setUserKey($uid, $keyId, $key, $encryptionModuleId) {
  95. $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
  96. return $this->setKey($path, $key);
  97. }
  98. /**
  99. * @inheritdoc
  100. */
  101. public function setFileKey($path, $keyId, $key, $encryptionModuleId) {
  102. $keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
  103. return $this->setKey($keyDir . $keyId, $key);
  104. }
  105. /**
  106. * @inheritdoc
  107. */
  108. public function setSystemUserKey($keyId, $key, $encryptionModuleId) {
  109. $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
  110. return $this->setKey($path, $key);
  111. }
  112. /**
  113. * @inheritdoc
  114. */
  115. public function deleteUserKey($uid, $keyId, $encryptionModuleId) {
  116. try {
  117. $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
  118. return !$this->view->file_exists($path) || $this->view->unlink($path);
  119. } catch (NoUserException $e) {
  120. // this exception can come from initMountPoints() from setupUserMounts()
  121. // for a deleted user.
  122. //
  123. // It means, that:
  124. // - we are not running in alternative storage mode because we don't call
  125. // initMountPoints() in that mode
  126. // - the keys were in the user's home but since the user was deleted, the
  127. // user's home is gone and so are the keys
  128. //
  129. // So there is nothing to do, just ignore.
  130. }
  131. }
  132. /**
  133. * @inheritdoc
  134. */
  135. public function deleteFileKey($path, $keyId, $encryptionModuleId) {
  136. $keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
  137. return !$this->view->file_exists($keyDir . $keyId) || $this->view->unlink($keyDir . $keyId);
  138. }
  139. /**
  140. * @inheritdoc
  141. */
  142. public function deleteAllFileKeys($path) {
  143. $keyDir = $this->getFileKeyDir('', $path);
  144. return !$this->view->file_exists($keyDir) || $this->view->deleteAll($keyDir);
  145. }
  146. /**
  147. * @inheritdoc
  148. */
  149. public function deleteSystemUserKey($keyId, $encryptionModuleId) {
  150. $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
  151. return !$this->view->file_exists($path) || $this->view->unlink($path);
  152. }
  153. /**
  154. * construct path to users key
  155. *
  156. * @param string $encryptionModuleId
  157. * @param string $keyId
  158. * @param string $uid
  159. * @return string
  160. */
  161. protected function constructUserKeyPath($encryptionModuleId, $keyId, $uid) {
  162. if ($uid === null) {
  163. $path = $this->root_dir . '/' . $this->encryption_base_dir . '/' . $encryptionModuleId . '/' . $keyId;
  164. } else {
  165. $path = $this->root_dir . '/' . $uid . $this->encryption_base_dir . '/'
  166. . $encryptionModuleId . '/' . $uid . '.' . $keyId;
  167. }
  168. return \OC\Files\Filesystem::normalizePath($path);
  169. }
  170. /**
  171. * read key from hard disk
  172. *
  173. * @param string $path to key
  174. * @return string
  175. */
  176. private function getKey($path) {
  177. $key = '';
  178. if ($this->view->file_exists($path)) {
  179. if (isset($this->keyCache[$path])) {
  180. $key = $this->keyCache[$path];
  181. } else {
  182. $key = $this->view->file_get_contents($path);
  183. $this->keyCache[$path] = $key;
  184. }
  185. }
  186. return $key;
  187. }
  188. /**
  189. * write key to disk
  190. *
  191. *
  192. * @param string $path path to key directory
  193. * @param string $key key
  194. * @return bool
  195. */
  196. private function setKey($path, $key) {
  197. $this->keySetPreparation(dirname($path));
  198. $result = $this->view->file_put_contents($path, $key);
  199. if (is_int($result) && $result > 0) {
  200. $this->keyCache[$path] = $key;
  201. return true;
  202. }
  203. return false;
  204. }
  205. /**
  206. * get path to key folder for a given file
  207. *
  208. * @param string $encryptionModuleId
  209. * @param string $path path to the file, relative to data/
  210. * @return string
  211. */
  212. private function getFileKeyDir($encryptionModuleId, $path) {
  213. list($owner, $filename) = $this->util->getUidAndFilename($path);
  214. // in case of system wide mount points the keys are stored directly in the data directory
  215. if ($this->util->isSystemWideMountPoint($filename, $owner)) {
  216. $keyPath = $this->root_dir . '/' . $this->keys_base_dir . $filename . '/';
  217. } else {
  218. $keyPath = $this->root_dir . '/' . $owner . $this->keys_base_dir . $filename . '/';
  219. }
  220. return Filesystem::normalizePath($keyPath . $encryptionModuleId . '/', false);
  221. }
  222. /**
  223. * move keys if a file was renamed
  224. *
  225. * @param string $source
  226. * @param string $target
  227. * @return boolean
  228. */
  229. public function renameKeys($source, $target) {
  230. $sourcePath = $this->getPathToKeys($source);
  231. $targetPath = $this->getPathToKeys($target);
  232. if ($this->view->file_exists($sourcePath)) {
  233. $this->keySetPreparation(dirname($targetPath));
  234. $this->view->rename($sourcePath, $targetPath);
  235. return true;
  236. }
  237. return false;
  238. }
  239. /**
  240. * copy keys if a file was renamed
  241. *
  242. * @param string $source
  243. * @param string $target
  244. * @return boolean
  245. */
  246. public function copyKeys($source, $target) {
  247. $sourcePath = $this->getPathToKeys($source);
  248. $targetPath = $this->getPathToKeys($target);
  249. if ($this->view->file_exists($sourcePath)) {
  250. $this->keySetPreparation(dirname($targetPath));
  251. $this->view->copy($sourcePath, $targetPath);
  252. return true;
  253. }
  254. return false;
  255. }
  256. /**
  257. * backup keys of a given encryption module
  258. *
  259. * @param string $encryptionModuleId
  260. * @param string $purpose
  261. * @param string $uid
  262. * @return bool
  263. * @since 12.0.0
  264. */
  265. public function backupUserKeys($encryptionModuleId, $purpose, $uid) {
  266. $source = $uid . $this->encryption_base_dir . '/' . $encryptionModuleId;
  267. $backupDir = $uid . $this->backup_base_dir;
  268. if (!$this->view->file_exists($backupDir)) {
  269. $this->view->mkdir($backupDir);
  270. }
  271. $backupDir = $backupDir . '/' . $purpose . '.' . $encryptionModuleId . '.' . $this->getTimestamp();
  272. $this->view->mkdir($backupDir);
  273. return $this->view->copy($source, $backupDir);
  274. }
  275. /**
  276. * get the current timestamp
  277. *
  278. * @return int
  279. */
  280. protected function getTimestamp() {
  281. return time();
  282. }
  283. /**
  284. * get system wide path and detect mount points
  285. *
  286. * @param string $path
  287. * @return string
  288. */
  289. protected function getPathToKeys($path) {
  290. list($owner, $relativePath) = $this->util->getUidAndFilename($path);
  291. $systemWideMountPoint = $this->util->isSystemWideMountPoint($relativePath, $owner);
  292. if ($systemWideMountPoint) {
  293. $systemPath = $this->root_dir . '/' . $this->keys_base_dir . $relativePath . '/';
  294. } else {
  295. $systemPath = $this->root_dir . '/' . $owner . $this->keys_base_dir . $relativePath . '/';
  296. }
  297. return Filesystem::normalizePath($systemPath, false);
  298. }
  299. /**
  300. * Make preparations to filesystem for saving a key file
  301. *
  302. * @param string $path relative to the views root
  303. */
  304. protected function keySetPreparation($path) {
  305. // If the file resides within a subdirectory, create it
  306. if (!$this->view->file_exists($path)) {
  307. $sub_dirs = explode('/', ltrim($path, '/'));
  308. $dir = '';
  309. foreach ($sub_dirs as $sub_dir) {
  310. $dir .= '/' . $sub_dir;
  311. if (!$this->view->is_dir($dir)) {
  312. $this->view->mkdir($dir);
  313. }
  314. }
  315. }
  316. }
  317. }