Migration.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 Robin Appelman <robin@icewind.nl>
  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 OCA\Encryption;
  25. use OC\Files\View;
  26. use OCP\IConfig;
  27. use OCP\IDBConnection;
  28. use OCP\ILogger;
  29. class Migration {
  30. private $moduleId;
  31. /** @var \OC\Files\View */
  32. private $view;
  33. /** @var \OCP\IDBConnection */
  34. private $connection;
  35. /** @var IConfig */
  36. private $config;
  37. /** @var ILogger */
  38. private $logger;
  39. /** @var string*/
  40. protected $installedVersion;
  41. /**
  42. * @param IConfig $config
  43. * @param View $view
  44. * @param IDBConnection $connection
  45. * @param ILogger $logger
  46. */
  47. public function __construct(IConfig $config, View $view, IDBConnection $connection, ILogger $logger) {
  48. $this->view = $view;
  49. $this->view->disableCacheUpdate();
  50. $this->connection = $connection;
  51. $this->moduleId = \OCA\Encryption\Crypto\Encryption::ID;
  52. $this->config = $config;
  53. $this->logger = $logger;
  54. $this->installedVersion = $this->config->getAppValue('files_encryption', 'installed_version', '-1');
  55. }
  56. public function finalCleanUp() {
  57. $this->view->deleteAll('files_encryption/public_keys');
  58. $this->updateFileCache();
  59. $this->config->deleteAppValue('files_encryption', 'installed_version');
  60. }
  61. /**
  62. * update file cache, copy unencrypted_size to the 'size' column
  63. */
  64. private function updateFileCache() {
  65. // make sure that we don't update the file cache multiple times
  66. // only update during the first run
  67. if ($this->installedVersion !== '-1') {
  68. $query = $this->connection->getQueryBuilder();
  69. $query->update('filecache')
  70. ->set('size', 'unencrypted_size')
  71. ->where($query->expr()->eq('encrypted', $query->createParameter('encrypted')))
  72. ->setParameter('encrypted', 1);
  73. $query->execute();
  74. }
  75. }
  76. /**
  77. * iterate through users and reorganize the folder structure
  78. */
  79. public function reorganizeFolderStructure() {
  80. $this->reorganizeSystemFolderStructure();
  81. $limit = 500;
  82. $offset = 0;
  83. do {
  84. $users = \OCP\User::getUsers('', $limit, $offset);
  85. foreach ($users as $user) {
  86. $this->reorganizeFolderStructureForUser($user);
  87. }
  88. $offset += $limit;
  89. } while (count($users) >= $limit);
  90. }
  91. /**
  92. * reorganize system wide folder structure
  93. */
  94. public function reorganizeSystemFolderStructure() {
  95. $this->createPathForKeys('/files_encryption');
  96. // backup system wide folders
  97. $this->backupSystemWideKeys();
  98. // rename system wide mount point
  99. $this->renameFileKeys('', '/files_encryption/keys');
  100. // rename system private keys
  101. $this->renameSystemPrivateKeys();
  102. $storage = $this->view->getMount('')->getStorage();
  103. $storage->getScanner()->scan('files_encryption');
  104. }
  105. /**
  106. * reorganize folder structure for user
  107. *
  108. * @param string $user
  109. */
  110. public function reorganizeFolderStructureForUser($user) {
  111. // backup all keys
  112. \OC_Util::tearDownFS();
  113. \OC_Util::setupFS($user);
  114. if ($this->backupUserKeys($user)) {
  115. // rename users private key
  116. $this->renameUsersPrivateKey($user);
  117. $this->renameUsersPublicKey($user);
  118. // rename file keys
  119. $path = '/files_encryption/keys';
  120. $this->renameFileKeys($user, $path);
  121. $trashPath = '/files_trashbin/keys';
  122. if (\OC_App::isEnabled('files_trashbin') && $this->view->is_dir($user . '/' . $trashPath)) {
  123. $this->renameFileKeys($user, $trashPath, true);
  124. $this->view->deleteAll($trashPath);
  125. }
  126. // delete old folders
  127. $this->deleteOldKeys($user);
  128. $this->view->getMount('/' . $user)->getStorage()->getScanner()->scan('files_encryption');
  129. }
  130. }
  131. /**
  132. * update database
  133. */
  134. public function updateDB() {
  135. // make sure that we don't update the file cache multiple times
  136. // only update during the first run
  137. if ($this->installedVersion === '-1') {
  138. return;
  139. }
  140. // delete left-over from old encryption which is no longer needed
  141. $this->config->deleteAppValue('files_encryption', 'ocsid');
  142. $this->config->deleteAppValue('files_encryption', 'types');
  143. $this->config->deleteAppValue('files_encryption', 'enabled');
  144. $oldAppValues = $this->connection->getQueryBuilder();
  145. $oldAppValues->select('*')
  146. ->from('appconfig')
  147. ->where($oldAppValues->expr()->eq('appid', $oldAppValues->createParameter('appid')))
  148. ->setParameter('appid', 'files_encryption');
  149. $appSettings = $oldAppValues->execute();
  150. while ($row = $appSettings->fetch()) {
  151. // 'installed_version' gets deleted at the end of the migration process
  152. if ($row['configkey'] !== 'installed_version' ) {
  153. $this->config->setAppValue('encryption', $row['configkey'], $row['configvalue']);
  154. $this->config->deleteAppValue('files_encryption', $row['configkey']);
  155. }
  156. }
  157. $oldPreferences = $this->connection->getQueryBuilder();
  158. $oldPreferences->select('*')
  159. ->from('preferences')
  160. ->where($oldPreferences->expr()->eq('appid', $oldPreferences->createParameter('appid')))
  161. ->setParameter('appid', 'files_encryption');
  162. $preferenceSettings = $oldPreferences->execute();
  163. while ($row = $preferenceSettings->fetch()) {
  164. $this->config->setUserValue($row['userid'], 'encryption', $row['configkey'], $row['configvalue']);
  165. $this->config->deleteUserValue($row['userid'], 'files_encryption', $row['configkey']);
  166. }
  167. }
  168. /**
  169. * create backup of system-wide keys
  170. */
  171. private function backupSystemWideKeys() {
  172. $backupDir = 'encryption_migration_backup_' . date("Y-m-d_H-i-s");
  173. $this->view->mkdir($backupDir);
  174. $this->view->copy('files_encryption', $backupDir . '/files_encryption');
  175. }
  176. /**
  177. * create backup of user specific keys
  178. *
  179. * @param string $user
  180. * @return bool
  181. */
  182. private function backupUserKeys($user) {
  183. $encryptionDir = $user . '/files_encryption';
  184. if ($this->view->is_dir($encryptionDir)) {
  185. $backupDir = $user . '/encryption_migration_backup_' . date("Y-m-d_H-i-s");
  186. $this->view->mkdir($backupDir);
  187. $this->view->copy($encryptionDir, $backupDir);
  188. return true;
  189. }
  190. return false;
  191. }
  192. /**
  193. * rename system-wide private keys
  194. */
  195. private function renameSystemPrivateKeys() {
  196. $dh = $this->view->opendir('files_encryption');
  197. $this->createPathForKeys('/files_encryption/' . $this->moduleId );
  198. if (is_resource($dh)) {
  199. while (($privateKey = readdir($dh)) !== false) {
  200. if (!\OC\Files\Filesystem::isIgnoredDir($privateKey) ) {
  201. if (!$this->view->is_dir('/files_encryption/' . $privateKey)) {
  202. $this->view->rename('files_encryption/' . $privateKey, 'files_encryption/' . $this->moduleId . '/' . $privateKey);
  203. $this->renameSystemPublicKey($privateKey);
  204. }
  205. }
  206. }
  207. closedir($dh);
  208. }
  209. }
  210. /**
  211. * rename system wide public key
  212. *
  213. * @param string $privateKey private key for which we want to rename the corresponding public key
  214. */
  215. private function renameSystemPublicKey($privateKey) {
  216. $publicKey = substr($privateKey,0 , strrpos($privateKey, '.privateKey')) . '.publicKey';
  217. $this->view->rename('files_encryption/public_keys/' . $publicKey, 'files_encryption/' . $this->moduleId . '/' . $publicKey);
  218. }
  219. /**
  220. * rename user-specific private keys
  221. *
  222. * @param string $user
  223. */
  224. private function renameUsersPrivateKey($user) {
  225. $oldPrivateKey = $user . '/files_encryption/' . $user . '.privateKey';
  226. $newPrivateKey = $user . '/files_encryption/' . $this->moduleId . '/' . $user . '.privateKey';
  227. if ($this->view->file_exists($oldPrivateKey)) {
  228. $this->createPathForKeys(dirname($newPrivateKey));
  229. $this->view->rename($oldPrivateKey, $newPrivateKey);
  230. }
  231. }
  232. /**
  233. * rename user-specific public keys
  234. *
  235. * @param string $user
  236. */
  237. private function renameUsersPublicKey($user) {
  238. $oldPublicKey = '/files_encryption/public_keys/' . $user . '.publicKey';
  239. $newPublicKey = $user . '/files_encryption/' . $this->moduleId . '/' . $user . '.publicKey';
  240. if ($this->view->file_exists($oldPublicKey)) {
  241. $this->createPathForKeys(dirname($newPublicKey));
  242. $this->view->rename($oldPublicKey, $newPublicKey);
  243. }
  244. }
  245. /**
  246. * rename file keys
  247. *
  248. * @param string $user
  249. * @param string $path
  250. * @param bool $trash
  251. */
  252. private function renameFileKeys($user, $path, $trash = false) {
  253. if ($this->view->is_dir($user . '/' . $path) === false) {
  254. $this->logger->info('Skip dir /' . $user . '/' . $path . ': does not exist');
  255. return;
  256. }
  257. $dh = $this->view->opendir($user . '/' . $path);
  258. if (is_resource($dh)) {
  259. while (($file = readdir($dh)) !== false) {
  260. if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
  261. if ($this->view->is_dir($user . '/' . $path . '/' . $file)) {
  262. $this->renameFileKeys($user, $path . '/' . $file, $trash);
  263. } else {
  264. $target = $this->getTargetDir($user, $path, $file, $trash);
  265. if ($target !== false) {
  266. $this->createPathForKeys(dirname($target));
  267. $this->view->rename($user . '/' . $path . '/' . $file, $target);
  268. } else {
  269. $this->logger->warning(
  270. 'did not move key "' . $file
  271. . '" could not find the corresponding file in /data/' . $user . '/files.'
  272. . 'Most likely the key was already moved in a previous migration run and is already on the right place.');
  273. }
  274. }
  275. }
  276. }
  277. closedir($dh);
  278. }
  279. }
  280. /**
  281. * get system mount points
  282. * wrap static method so that it can be mocked for testing
  283. *
  284. * @internal
  285. * @return array
  286. */
  287. protected function getSystemMountPoints() {
  288. return \OC_Mount_Config::getSystemMountPoints();
  289. }
  290. /**
  291. * generate target directory
  292. *
  293. * @param string $user
  294. * @param string $keyPath
  295. * @param string $filename
  296. * @param bool $trash
  297. * @return string
  298. */
  299. private function getTargetDir($user, $keyPath, $filename, $trash) {
  300. if ($trash) {
  301. $filePath = substr($keyPath, strlen('/files_trashbin/keys/'));
  302. $targetDir = $user . '/files_encryption/keys/files_trashbin/' . $filePath . '/' . $this->moduleId . '/' . $filename;
  303. } else {
  304. $filePath = substr($keyPath, strlen('/files_encryption/keys/'));
  305. $targetDir = $user . '/files_encryption/keys/files/' . $filePath . '/' . $this->moduleId . '/' . $filename;
  306. }
  307. if ($user === '') {
  308. // for system wide mounts we need to check if the mount point really exists
  309. $normalized = \OC\Files\Filesystem::normalizePath($filePath);
  310. $systemMountPoints = $this->getSystemMountPoints();
  311. foreach ($systemMountPoints as $mountPoint) {
  312. $normalizedMountPoint = \OC\Files\Filesystem::normalizePath($mountPoint['mountpoint']) . '/';
  313. if (strpos($normalized, $normalizedMountPoint) === 0)
  314. return $targetDir;
  315. }
  316. } else if ($trash === false && $this->view->file_exists('/' . $user. '/files/' . $filePath)) {
  317. return $targetDir;
  318. } else if ($trash === true && $this->view->file_exists('/' . $user. '/files_trashbin/' . $filePath)) {
  319. return $targetDir;
  320. }
  321. return false;
  322. }
  323. /**
  324. * delete old keys
  325. *
  326. * @param string $user
  327. */
  328. private function deleteOldKeys($user) {
  329. $this->view->deleteAll($user . '/files_encryption/keyfiles');
  330. $this->view->deleteAll($user . '/files_encryption/share-keys');
  331. }
  332. /**
  333. * create directories for the keys recursively
  334. *
  335. * @param string $path
  336. */
  337. private function createPathForKeys($path) {
  338. if (!$this->view->file_exists($path)) {
  339. $sub_dirs = explode('/', $path);
  340. $dir = '';
  341. foreach ($sub_dirs as $sub_dir) {
  342. $dir .= '/' . $sub_dir;
  343. if (!$this->view->is_dir($dir)) {
  344. $this->view->mkdir($dir);
  345. }
  346. }
  347. }
  348. }
  349. }