ChangeKeyStorageRoot.php 7.2 KB

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