ChangeKeyStorageRoot.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  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\Core\Command\Encryption;
  27. use OC\Encryption\Keys\Storage;
  28. use OC\Encryption\Util;
  29. use OC\Files\Filesystem;
  30. use OC\Files\View;
  31. use OCP\IConfig;
  32. use OCP\IUserManager;
  33. use Symfony\Component\Console\Command\Command;
  34. use Symfony\Component\Console\Helper\ProgressBar;
  35. use Symfony\Component\Console\Helper\QuestionHelper;
  36. use Symfony\Component\Console\Input\InputArgument;
  37. use Symfony\Component\Console\Input\InputInterface;
  38. use Symfony\Component\Console\Output\OutputInterface;
  39. use Symfony\Component\Console\Question\ConfirmationQuestion;
  40. class ChangeKeyStorageRoot extends Command {
  41. public function __construct(
  42. protected View $rootView,
  43. protected IUserManager $userManager,
  44. protected IConfig $config,
  45. protected Util $util,
  46. protected QuestionHelper $questionHelper,
  47. ) {
  48. parent::__construct();
  49. }
  50. protected function configure() {
  51. parent::configure();
  52. $this
  53. ->setName('encryption:change-key-storage-root')
  54. ->setDescription('Change key storage root')
  55. ->addArgument(
  56. 'newRoot',
  57. InputArgument::OPTIONAL,
  58. 'new root of the key storage relative to the data folder'
  59. );
  60. }
  61. protected function execute(InputInterface $input, OutputInterface $output): int {
  62. $oldRoot = $this->util->getKeyStorageRoot();
  63. $newRoot = $input->getArgument('newRoot');
  64. if ($newRoot === null) {
  65. $question = new ConfirmationQuestion('No storage root given, do you want to reset the key storage root to the default location? (y/n) ', false);
  66. if (!$this->questionHelper->ask($input, $output, $question)) {
  67. return 1;
  68. }
  69. $newRoot = '';
  70. }
  71. $oldRootDescription = $oldRoot !== '' ? $oldRoot : 'default storage location';
  72. $newRootDescription = $newRoot !== '' ? $newRoot : 'default storage location';
  73. $output->writeln("Change key storage root from <info>$oldRootDescription</info> to <info>$newRootDescription</info>");
  74. $success = $this->moveAllKeys($oldRoot, $newRoot, $output);
  75. if ($success) {
  76. $this->util->setKeyStorageRoot($newRoot);
  77. $output->writeln('');
  78. $output->writeln("Key storage root successfully changed to <info>$newRootDescription</info>");
  79. return 0;
  80. }
  81. return 1;
  82. }
  83. /**
  84. * move keys to new key storage root
  85. *
  86. * @param string $oldRoot
  87. * @param string $newRoot
  88. * @param OutputInterface $output
  89. * @return bool
  90. * @throws \Exception
  91. */
  92. protected function moveAllKeys($oldRoot, $newRoot, OutputInterface $output) {
  93. $output->writeln("Start to move keys:");
  94. if ($this->rootView->is_dir($oldRoot) === false) {
  95. $output->writeln("No old keys found: Nothing needs to be moved");
  96. return false;
  97. }
  98. $this->prepareNewRoot($newRoot);
  99. $this->moveSystemKeys($oldRoot, $newRoot);
  100. $this->moveUserKeys($oldRoot, $newRoot, $output);
  101. return true;
  102. }
  103. /**
  104. * prepare new key storage
  105. *
  106. * @param string $newRoot
  107. * @throws \Exception
  108. */
  109. protected function prepareNewRoot($newRoot) {
  110. if ($this->rootView->is_dir($newRoot) === false) {
  111. throw new \Exception("New root folder doesn't exist. Please create the folder or check the permissions and try again.");
  112. }
  113. $result = $this->rootView->file_put_contents(
  114. $newRoot . '/' . Storage::KEY_STORAGE_MARKER,
  115. 'Nextcloud will detect this folder as key storage root only if this file exists'
  116. );
  117. if (!$result) {
  118. throw new \Exception("Can't access the new root folder. Please check the permissions and make sure that the folder is in your data folder");
  119. }
  120. }
  121. /**
  122. * move system key folder
  123. *
  124. * @param string $oldRoot
  125. * @param string $newRoot
  126. */
  127. protected function moveSystemKeys($oldRoot, $newRoot) {
  128. if (
  129. $this->rootView->is_dir($oldRoot . '/files_encryption') &&
  130. $this->targetExists($newRoot . '/files_encryption') === false
  131. ) {
  132. $this->rootView->rename($oldRoot . '/files_encryption', $newRoot . '/files_encryption');
  133. }
  134. }
  135. /**
  136. * setup file system for the given user
  137. *
  138. * @param string $uid
  139. */
  140. protected function setupUserFS($uid) {
  141. \OC_Util::tearDownFS();
  142. \OC_Util::setupFS($uid);
  143. }
  144. /**
  145. * iterate over each user and move the keys to the new storage
  146. *
  147. * @param string $oldRoot
  148. * @param string $newRoot
  149. * @param OutputInterface $output
  150. */
  151. protected function moveUserKeys($oldRoot, $newRoot, OutputInterface $output) {
  152. $progress = new ProgressBar($output);
  153. $progress->start();
  154. foreach ($this->userManager->getBackends() as $backend) {
  155. $limit = 500;
  156. $offset = 0;
  157. do {
  158. $users = $backend->getUsers('', $limit, $offset);
  159. foreach ($users as $user) {
  160. $progress->advance();
  161. $this->setupUserFS($user);
  162. $this->moveUserEncryptionFolder($user, $oldRoot, $newRoot);
  163. }
  164. $offset += $limit;
  165. } while (count($users) >= $limit);
  166. }
  167. $progress->finish();
  168. }
  169. /**
  170. * move user encryption folder to new root folder
  171. *
  172. * @param string $user
  173. * @param string $oldRoot
  174. * @param string $newRoot
  175. * @throws \Exception
  176. */
  177. protected function moveUserEncryptionFolder($user, $oldRoot, $newRoot) {
  178. if ($this->userManager->userExists($user)) {
  179. $source = $oldRoot . '/' . $user . '/files_encryption';
  180. $target = $newRoot . '/' . $user . '/files_encryption';
  181. if (
  182. $this->rootView->is_dir($source) &&
  183. $this->targetExists($target) === false
  184. ) {
  185. $this->prepareParentFolder($newRoot . '/' . $user);
  186. $this->rootView->rename($source, $target);
  187. }
  188. }
  189. }
  190. /**
  191. * Make preparations to filesystem for saving a key file
  192. *
  193. * @param string $path relative to data/
  194. */
  195. protected function prepareParentFolder($path) {
  196. $path = Filesystem::normalizePath($path);
  197. // If the file resides within a subdirectory, create it
  198. if ($this->rootView->file_exists($path) === false) {
  199. $sub_dirs = explode('/', ltrim($path, '/'));
  200. $dir = '';
  201. foreach ($sub_dirs as $sub_dir) {
  202. $dir .= '/' . $sub_dir;
  203. if ($this->rootView->file_exists($dir) === false) {
  204. $this->rootView->mkdir($dir);
  205. }
  206. }
  207. }
  208. }
  209. /**
  210. * check if target already exists
  211. *
  212. * @param $path
  213. * @return bool
  214. * @throws \Exception
  215. */
  216. protected function targetExists($path) {
  217. if ($this->rootView->file_exists($path)) {
  218. throw new \Exception("new folder '$path' already exists");
  219. }
  220. return false;
  221. }
  222. }