ChangeKeyStorageRoot.php 7.4 KB

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