DecryptAll.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Question\ConfirmationQuestion;
  17. class DecryptAll extends Command {
  18. protected bool $wasTrashbinEnabled = false;
  19. protected bool $wasMaintenanceModeEnabled = false;
  20. public function __construct(
  21. protected IManager $encryptionManager,
  22. protected IAppManager $appManager,
  23. protected IConfig $config,
  24. protected \OC\Encryption\DecryptAll $decryptAll,
  25. protected QuestionHelper $questionHelper,
  26. ) {
  27. parent::__construct();
  28. }
  29. /**
  30. * Set maintenance mode and disable the trashbin app
  31. */
  32. protected function forceMaintenanceAndTrashbin(): void {
  33. $this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin');
  34. $this->wasMaintenanceModeEnabled = $this->config->getSystemValueBool('maintenance');
  35. $this->config->setSystemValue('maintenance', true);
  36. $this->appManager->disableApp('files_trashbin');
  37. }
  38. /**
  39. * Reset the maintenance mode and re-enable the trashbin app
  40. */
  41. protected function resetMaintenanceAndTrashbin(): void {
  42. $this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled);
  43. if ($this->wasTrashbinEnabled) {
  44. $this->appManager->enableApp('files_trashbin');
  45. }
  46. }
  47. protected function configure() {
  48. parent::configure();
  49. $this->setName('encryption:decrypt-all');
  50. $this->setDescription('Disable server-side encryption and decrypt all files');
  51. $this->setHelp(
  52. 'This will disable server-side encryption and decrypt all files for '
  53. . 'all users if it is supported by your encryption module. '
  54. . 'Please make sure that no user access his files during this process!'
  55. );
  56. $this->addArgument(
  57. 'user',
  58. InputArgument::OPTIONAL,
  59. 'user for which you want to decrypt all files (optional)',
  60. ''
  61. );
  62. }
  63. protected function execute(InputInterface $input, OutputInterface $output): int {
  64. if (!$input->isInteractive()) {
  65. $output->writeln('Invalid TTY.');
  66. $output->writeln('If you are trying to execute the command in a Docker ');
  67. $output->writeln("container, do not forget to execute 'docker exec' with");
  68. $output->writeln("the '-i' and '-t' options.");
  69. $output->writeln('');
  70. return 1;
  71. }
  72. $isMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
  73. if ($isMaintenanceModeEnabled) {
  74. $output->writeln("Maintenance mode must be disabled when starting decryption,");
  75. $output->writeln("in order to load the relevant encryption modules correctly.");
  76. $output->writeln("Your instance will automatically be put to maintenance mode");
  77. $output->writeln("during the actual decryption of the files.");
  78. return 1;
  79. }
  80. try {
  81. if ($this->encryptionManager->isEnabled() === true) {
  82. $output->write('Disable server side encryption... ');
  83. $this->config->setAppValue('core', 'encryption_enabled', 'no');
  84. $output->writeln('done.');
  85. } else {
  86. $output->writeln('Server side encryption not enabled. Nothing to do.');
  87. return 0;
  88. }
  89. $uid = $input->getArgument('user');
  90. if ($uid === '') {
  91. $message = 'your Nextcloud';
  92. } else {
  93. $message = "$uid's account";
  94. }
  95. $output->writeln("\n");
  96. $output->writeln("You are about to start to decrypt all files stored in $message.");
  97. $output->writeln('It will depend on the encryption module and your setup if this is possible.');
  98. $output->writeln('Depending on the number and size of your files this can take some time');
  99. $output->writeln('Please make sure that no user access his files during this process!');
  100. $output->writeln('');
  101. $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
  102. if ($this->questionHelper->ask($input, $output, $question)) {
  103. $this->forceMaintenanceAndTrashbin();
  104. $user = $input->getArgument('user');
  105. $result = $this->decryptAll->decryptAll($input, $output, $user);
  106. if ($result === false) {
  107. $output->writeln(' aborted.');
  108. $output->writeln('Server side encryption remains enabled');
  109. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  110. } elseif ($uid !== '') {
  111. $output->writeln('Server side encryption remains enabled');
  112. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  113. }
  114. $this->resetMaintenanceAndTrashbin();
  115. return 0;
  116. }
  117. $output->write('Enable server side encryption... ');
  118. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  119. $output->writeln('done.');
  120. $output->writeln('aborted');
  121. return 1;
  122. } catch (\Exception $e) {
  123. // enable server side encryption again if something went wrong
  124. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  125. $this->resetMaintenanceAndTrashbin();
  126. throw $e;
  127. }
  128. }
  129. }