1
0

EncryptAll.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 bool $wasTrashbinEnabled = false;
  38. protected bool $wasMaintenanceModeEnabled = false;
  39. public function __construct(
  40. protected IManager $encryptionManager,
  41. protected IAppManager $appManager,
  42. protected IConfig $config,
  43. protected QuestionHelper $questionHelper,
  44. ) {
  45. parent::__construct();
  46. }
  47. /**
  48. * Set maintenance mode and disable the trashbin app
  49. */
  50. protected function forceMaintenanceAndTrashbin(): void {
  51. $this->wasTrashbinEnabled = (bool)$this->appManager->isEnabledForUser('files_trashbin');
  52. $this->wasMaintenanceModeEnabled = $this->config->getSystemValueBool('maintenance');
  53. $this->config->setSystemValue('maintenance', true);
  54. $this->appManager->disableApp('files_trashbin');
  55. }
  56. /**
  57. * Reset the maintenance mode and re-enable the trashbin app
  58. */
  59. protected function resetMaintenanceAndTrashbin(): void {
  60. $this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled);
  61. if ($this->wasTrashbinEnabled) {
  62. $this->appManager->enableApp('files_trashbin');
  63. }
  64. }
  65. protected function configure() {
  66. parent::configure();
  67. $this->setName('encryption:encrypt-all');
  68. $this->setDescription('Encrypt all files for all users');
  69. $this->setHelp(
  70. 'This will encrypt all files for all users. '
  71. . 'Please make sure that no user access his files during this process!'
  72. );
  73. }
  74. protected function execute(InputInterface $input, OutputInterface $output): int {
  75. if (!$input->isInteractive()) {
  76. $output->writeln('Invalid TTY.');
  77. $output->writeln('If you are trying to execute the command in a Docker ');
  78. $output->writeln("container, do not forget to execute 'docker exec' with");
  79. $output->writeln("the '-i' and '-t' options.");
  80. $output->writeln('');
  81. return 1;
  82. }
  83. if ($this->encryptionManager->isEnabled() === false) {
  84. throw new \Exception('Server side encryption is not enabled');
  85. }
  86. $output->writeln("\n");
  87. $output->writeln('You are about to encrypt all files stored in your Nextcloud installation.');
  88. $output->writeln('Depending on the number of available files, and their size, this may take quite some time.');
  89. $output->writeln('Please ensure that no user accesses their files during this time!');
  90. $output->writeln('Note: The encryption module you use determines which files get encrypted.');
  91. $output->writeln('');
  92. $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
  93. if ($this->questionHelper->ask($input, $output, $question)) {
  94. $this->forceMaintenanceAndTrashbin();
  95. try {
  96. $defaultModule = $this->encryptionManager->getEncryptionModule();
  97. $defaultModule->encryptAll($input, $output);
  98. } catch (\Exception $ex) {
  99. $this->resetMaintenanceAndTrashbin();
  100. throw $ex;
  101. }
  102. $this->resetMaintenanceAndTrashbin();
  103. return 0;
  104. }
  105. $output->writeln('aborted');
  106. return 1;
  107. }
  108. }