ChangeKeyStorageRoot.php 7.2 KB

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