EncryptAll.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\Encryption;
  8. use OCP\App\IAppManager;
  9. use OCP\Encryption\IManager;
  10. use OCP\IConfig;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Helper\QuestionHelper;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Question\ConfirmationQuestion;
  16. class EncryptAll extends Command {
  17. protected bool $wasTrashbinEnabled = false;
  18. public function __construct(
  19. protected IManager $encryptionManager,
  20. protected IAppManager $appManager,
  21. protected IConfig $config,
  22. protected QuestionHelper $questionHelper,
  23. ) {
  24. parent::__construct();
  25. }
  26. /**
  27. * Set maintenance mode and disable the trashbin app
  28. */
  29. protected function forceMaintenanceAndTrashbin(): void {
  30. $this->wasTrashbinEnabled = (bool)$this->appManager->isEnabledForUser('files_trashbin');
  31. $this->config->setSystemValue('maintenance', true);
  32. $this->appManager->disableApp('files_trashbin');
  33. }
  34. /**
  35. * Reset the maintenance mode and re-enable the trashbin app
  36. */
  37. protected function resetMaintenanceAndTrashbin(): void {
  38. $this->config->setSystemValue('maintenance', false);
  39. if ($this->wasTrashbinEnabled) {
  40. $this->appManager->enableApp('files_trashbin');
  41. }
  42. }
  43. protected function configure() {
  44. parent::configure();
  45. $this->setName('encryption:encrypt-all');
  46. $this->setDescription('Encrypt all files for all users');
  47. $this->setHelp(
  48. 'This will encrypt all files for all users. '
  49. . 'Please make sure that no user access his files during this process!'
  50. );
  51. }
  52. protected function execute(InputInterface $input, OutputInterface $output): int {
  53. if (!$input->isInteractive()) {
  54. $output->writeln('Invalid TTY.');
  55. $output->writeln('If you are trying to execute the command in a Docker ');
  56. $output->writeln("container, do not forget to execute 'docker exec' with");
  57. $output->writeln("the '-i' and '-t' options.");
  58. $output->writeln('');
  59. return 1;
  60. }
  61. if ($this->encryptionManager->isEnabled() === false) {
  62. throw new \Exception('Server side encryption is not enabled');
  63. }
  64. if ($this->config->getSystemValueBool('maintenance')) {
  65. $output->writeln('<error>This command cannot be run with maintenance mode enabled.</error>');
  66. return self::FAILURE;
  67. }
  68. $output->writeln("\n");
  69. $output->writeln('You are about to encrypt all files stored in your Nextcloud installation.');
  70. $output->writeln('Depending on the number of available files, and their size, this may take quite some time.');
  71. $output->writeln('Please ensure that no user accesses their files during this time!');
  72. $output->writeln('Note: The encryption module you use determines which files get encrypted.');
  73. $output->writeln('');
  74. $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
  75. if ($this->questionHelper->ask($input, $output, $question)) {
  76. $this->forceMaintenanceAndTrashbin();
  77. try {
  78. $defaultModule = $this->encryptionManager->getEncryptionModule();
  79. $defaultModule->encryptAll($input, $output);
  80. } catch (\Exception $ex) {
  81. $this->resetMaintenanceAndTrashbin();
  82. throw $ex;
  83. }
  84. $this->resetMaintenanceAndTrashbin();
  85. return self::SUCCESS;
  86. }
  87. $output->writeln('aborted');
  88. return self::FAILURE;
  89. }
  90. }