EncryptAll.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Evgeny Golyshev <eugulixes@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Matthew Setter <matthew@matthewsetter.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Core\Command\Encryption;
  28. use OCP\App\IAppManager;
  29. use OCP\Encryption\IManager;
  30. use OCP\IConfig;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Helper\QuestionHelper;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. use Symfony\Component\Console\Question\ConfirmationQuestion;
  36. class EncryptAll extends Command {
  37. protected IManager $encryptionManager;
  38. protected IAppManager $appManager;
  39. protected IConfig $config;
  40. protected QuestionHelper $questionHelper;
  41. protected bool $wasTrashbinEnabled = false;
  42. protected bool $wasMaintenanceModeEnabled;
  43. public function __construct(
  44. IManager $encryptionManager,
  45. IAppManager $appManager,
  46. IConfig $config,
  47. QuestionHelper $questionHelper
  48. ) {
  49. parent::__construct();
  50. $this->appManager = $appManager;
  51. $this->encryptionManager = $encryptionManager;
  52. $this->config = $config;
  53. $this->questionHelper = $questionHelper;
  54. }
  55. /**
  56. * Set maintenance mode and disable the trashbin app
  57. */
  58. protected function forceMaintenanceAndTrashbin(): void {
  59. $this->wasTrashbinEnabled = (bool)$this->appManager->isEnabledForUser('files_trashbin');
  60. $this->wasMaintenanceModeEnabled = $this->config->getSystemValueBool('maintenance');
  61. $this->config->setSystemValue('maintenance', true);
  62. $this->appManager->disableApp('files_trashbin');
  63. }
  64. /**
  65. * Reset the maintenance mode and re-enable the trashbin app
  66. */
  67. protected function resetMaintenanceAndTrashbin(): void {
  68. $this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled);
  69. if ($this->wasTrashbinEnabled) {
  70. $this->appManager->enableApp('files_trashbin');
  71. }
  72. }
  73. protected function configure() {
  74. parent::configure();
  75. $this->setName('encryption:encrypt-all');
  76. $this->setDescription('Encrypt all files for all users');
  77. $this->setHelp(
  78. 'This will encrypt all files for all users. '
  79. . 'Please make sure that no user access his files during this process!'
  80. );
  81. }
  82. protected function execute(InputInterface $input, OutputInterface $output): int {
  83. if (!$input->isInteractive()) {
  84. $output->writeln('Invalid TTY.');
  85. $output->writeln('If you are trying to execute the command in a Docker ');
  86. $output->writeln("container, do not forget to execute 'docker exec' with");
  87. $output->writeln("the '-i' and '-t' options.");
  88. $output->writeln('');
  89. return 1;
  90. }
  91. if ($this->encryptionManager->isEnabled() === false) {
  92. throw new \Exception('Server side encryption is not enabled');
  93. }
  94. $output->writeln("\n");
  95. $output->writeln('You are about to encrypt all files stored in your Nextcloud installation.');
  96. $output->writeln('Depending on the number of available files, and their size, this may take quite some time.');
  97. $output->writeln('Please ensure that no user accesses their files during this time!');
  98. $output->writeln('Note: The encryption module you use determines which files get encrypted.');
  99. $output->writeln('');
  100. $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
  101. if ($this->questionHelper->ask($input, $output, $question)) {
  102. $this->forceMaintenanceAndTrashbin();
  103. try {
  104. $defaultModule = $this->encryptionManager->getEncryptionModule();
  105. $defaultModule->encryptAll($input, $output);
  106. } catch (\Exception $ex) {
  107. $this->resetMaintenanceAndTrashbin();
  108. throw $ex;
  109. }
  110. $this->resetMaintenanceAndTrashbin();
  111. return 0;
  112. }
  113. $output->writeln('aborted');
  114. return 1;
  115. }
  116. }