Storage.php 9.8 KB

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