migration.php 12 KB

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