You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

migration.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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\DB\Connection;
  24. use OC\Files\View;
  25. use OCP\IConfig;
  26. class Migration {
  27. private $moduleId;
  28. /** @var \OC\Files\View */
  29. private $view;
  30. /** @var \OC\DB\Connection */
  31. private $connection;
  32. /** @var IConfig */
  33. private $config;
  34. /**
  35. * @param IConfig $config
  36. * @param View $view
  37. * @param Connection $connection
  38. */
  39. public function __construct(IConfig $config, View $view, Connection $connection) {
  40. $this->view = $view;
  41. $this->view->getUpdater()->disable();
  42. $this->connection = $connection;
  43. $this->moduleId = \OCA\Encryption\Crypto\Encryption::ID;
  44. $this->config = $config;
  45. }
  46. public function __destruct() {
  47. $this->view->deleteAll('files_encryption/public_keys');
  48. $this->updateFileCache();
  49. $this->config->deleteAppValue('files_encryption', 'installed_version');
  50. }
  51. /**
  52. * update file cache, copy unencrypted_size to the 'size' column
  53. */
  54. private function updateFileCache() {
  55. $query = $this->connection->createQueryBuilder();
  56. $query->update('`*PREFIX*filecache`')
  57. ->set('`size`', '`unencrypted_size`')
  58. ->where($query->expr()->eq('`encrypted`', ':encrypted'))
  59. ->setParameter('encrypted', 1);
  60. $query->execute();
  61. }
  62. /**
  63. * iterate through users and reorganize the folder structure
  64. */
  65. public function reorganizeFolderStructure() {
  66. $this->reorganizeSystemFolderStructure();
  67. $limit = 500;
  68. $offset = 0;
  69. do {
  70. $users = \OCP\User::getUsers('', $limit, $offset);
  71. foreach ($users as $user) {
  72. $this->reorganizeFolderStructureForUser($user);
  73. }
  74. $offset += $limit;
  75. } while (count($users) >= $limit);
  76. }
  77. /**
  78. * reorganize system wide folder structure
  79. */
  80. public function reorganizeSystemFolderStructure() {
  81. $this->createPathForKeys('/files_encryption');
  82. // backup system wide folders
  83. $this->backupSystemWideKeys();
  84. // rename system wide mount point
  85. $this->renameFileKeys('', '/files_encryption/keys');
  86. // rename system private keys
  87. $this->renameSystemPrivateKeys();
  88. $storage = $this->view->getMount('')->getStorage();
  89. $storage->getScanner()->scan('files_encryption');
  90. }
  91. /**
  92. * reorganize folder structure for user
  93. *
  94. * @param string $user
  95. */
  96. public function reorganizeFolderStructureForUser($user) {
  97. // backup all keys
  98. \OC_Util::tearDownFS();
  99. \OC_Util::setupFS($user);
  100. if ($this->backupUserKeys($user)) {
  101. // rename users private key
  102. $this->renameUsersPrivateKey($user);
  103. $this->renameUsersPublicKey($user);
  104. // rename file keys
  105. $path = '/files_encryption/keys';
  106. $this->renameFileKeys($user, $path);
  107. $trashPath = '/files_trashbin/keys';
  108. if (\OC_App::isEnabled('files_trashbin') && $this->view->is_dir($user . '/' . $trashPath)) {
  109. $this->renameFileKeys($user, $trashPath, true);
  110. $this->view->deleteAll($trashPath);
  111. }
  112. // delete old folders
  113. $this->deleteOldKeys($user);
  114. $this->view->getMount('/' . $user)->getStorage()->getScanner()->scan('files_encryption');
  115. }
  116. }
  117. /**
  118. * update database
  119. */
  120. public function updateDB() {
  121. // delete left-over from old encryption which is no longer needed
  122. $this->config->deleteAppValue('files_encryption', 'ocsid');
  123. $this->config->deleteAppValue('files_encryption', 'types');
  124. $this->config->deleteAppValue('files_encryption', 'enabled');
  125. $query = $this->connection->createQueryBuilder();
  126. $query->update('`*PREFIX*appconfig`')
  127. ->set('`appid`', ':newappid')
  128. ->where($query->expr()->eq('`appid`', ':oldappid'))
  129. ->setParameter('oldappid', 'files_encryption')
  130. ->setParameter('newappid', 'encryption');
  131. $query->execute();
  132. $query = $this->connection->createQueryBuilder();
  133. $query->update('`*PREFIX*preferences`')
  134. ->set('`appid`', ':newappid')
  135. ->where($query->expr()->eq('`appid`', ':oldappid'))
  136. ->setParameter('oldappid', 'files_encryption')
  137. ->setParameter('newappid', 'encryption');
  138. $query->execute();
  139. }
  140. /**
  141. * create backup of system-wide keys
  142. */
  143. private function backupSystemWideKeys() {
  144. $backupDir = 'encryption_migration_backup_' . date("Y-m-d_H-i-s");
  145. $this->view->mkdir($backupDir);
  146. $this->view->copy('files_encryption', $backupDir . '/files_encryption');
  147. }
  148. /**
  149. * create backup of user specific keys
  150. *
  151. * @param string $user
  152. * @return bool
  153. */
  154. private function backupUserKeys($user) {
  155. $encryptionDir = $user . '/files_encryption';
  156. if ($this->view->is_dir($encryptionDir)) {
  157. $backupDir = $user . '/encryption_migration_backup_' . date("Y-m-d_H-i-s");
  158. $this->view->mkdir($backupDir);
  159. $this->view->copy($encryptionDir, $backupDir);
  160. return true;
  161. }
  162. return false;
  163. }
  164. /**
  165. * rename system-wide private keys
  166. */
  167. private function renameSystemPrivateKeys() {
  168. $dh = $this->view->opendir('files_encryption');
  169. $this->createPathForKeys('/files_encryption/' . $this->moduleId );
  170. if (is_resource($dh)) {
  171. while (($privateKey = readdir($dh)) !== false) {
  172. if (!\OC\Files\Filesystem::isIgnoredDir($privateKey) ) {
  173. if (!$this->view->is_dir('/files_encryption/' . $privateKey)) {
  174. $this->view->rename('files_encryption/' . $privateKey, 'files_encryption/' . $this->moduleId . '/' . $privateKey);
  175. $this->renameSystemPublicKey($privateKey);
  176. }
  177. }
  178. }
  179. closedir($dh);
  180. }
  181. }
  182. /**
  183. * rename system wide public key
  184. *
  185. * @param $privateKey private key for which we want to rename the corresponding public key
  186. */
  187. private function renameSystemPublicKey($privateKey) {
  188. $publicKey = substr($privateKey,0 , strrpos($privateKey, '.privateKey')) . '.publicKey';
  189. $this->view->rename('files_encryption/public_keys/' . $publicKey, 'files_encryption/' . $this->moduleId . '/' . $publicKey);
  190. }
  191. /**
  192. * rename user-specific private keys
  193. *
  194. * @param string $user
  195. */
  196. private function renameUsersPrivateKey($user) {
  197. $oldPrivateKey = $user . '/files_encryption/' . $user . '.privateKey';
  198. $newPrivateKey = $user . '/files_encryption/' . $this->moduleId . '/' . $user . '.privateKey';
  199. $this->createPathForKeys(dirname($newPrivateKey));
  200. $this->view->rename($oldPrivateKey, $newPrivateKey);
  201. }
  202. /**
  203. * rename user-specific public keys
  204. *
  205. * @param string $user
  206. */
  207. private function renameUsersPublicKey($user) {
  208. $oldPublicKey = '/files_encryption/public_keys/' . $user . '.publicKey';
  209. $newPublicKey = $user . '/files_encryption/' . $this->moduleId . '/' . $user . '.publicKey';
  210. $this->createPathForKeys(dirname($newPublicKey));
  211. $this->view->rename($oldPublicKey, $newPublicKey);
  212. }
  213. /**
  214. * rename file keys
  215. *
  216. * @param string $user
  217. * @param string $path
  218. * @param bool $trash
  219. */
  220. private function renameFileKeys($user, $path, $trash = false) {
  221. $dh = $this->view->opendir($user . '/' . $path);
  222. if (is_resource($dh)) {
  223. while (($file = readdir($dh)) !== false) {
  224. if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
  225. if ($this->view->is_dir($user . '/' . $path . '/' . $file)) {
  226. $this->renameFileKeys($user, $path . '/' . $file, $trash);
  227. } else {
  228. $target = $this->getTargetDir($user, $path, $file, $trash);
  229. $this->createPathForKeys(dirname($target));
  230. $this->view->rename($user . '/' . $path . '/' . $file, $target);
  231. }
  232. }
  233. }
  234. closedir($dh);
  235. }
  236. }
  237. /**
  238. * generate target directory
  239. *
  240. * @param string $user
  241. * @param string $filePath
  242. * @param string $filename
  243. * @param bool $trash
  244. * @return string
  245. */
  246. private function getTargetDir($user, $filePath, $filename, $trash) {
  247. if ($trash) {
  248. $targetDir = $user . '/files_encryption/keys/files_trashbin/' . substr($filePath, strlen('/files_trashbin/keys/')) . '/' . $this->moduleId . '/' . $filename;
  249. } else {
  250. $targetDir = $user . '/files_encryption/keys/files/' . substr($filePath, strlen('/files_encryption/keys/')) . '/' . $this->moduleId . '/' . $filename;
  251. }
  252. return $targetDir;
  253. }
  254. /**
  255. * delete old keys
  256. *
  257. * @param string $user
  258. */
  259. private function deleteOldKeys($user) {
  260. $this->view->deleteAll($user . '/files_encryption/keyfiles');
  261. $this->view->deleteAll($user . '/files_encryption/share-keys');
  262. }
  263. /**
  264. * create directories for the keys recursively
  265. *
  266. * @param string $path
  267. */
  268. private function createPathForKeys($path) {
  269. if (!$this->view->file_exists($path)) {
  270. $sub_dirs = explode('/', $path);
  271. $dir = '';
  272. foreach ($sub_dirs as $sub_dir) {
  273. $dir .= '/' . $sub_dir;
  274. if (!$this->view->is_dir($dir)) {
  275. $this->view->mkdir($dir);
  276. }
  277. }
  278. }
  279. }
  280. }