DecryptAll.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 davitol <dtoledo@solidgear.es>
  8. * @author Evgeny Golyshev <eugulixes@gmail.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Marius Blüm <marius@lineone.io>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Ruben Homs <ruben@homs.codes>
  13. * @author Sergio Bertolín <sbertolin@solidgear.es>
  14. * @author Vincent Petry <vincent@nextcloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\Core\Command\Encryption;
  32. use OCP\App\IAppManager;
  33. use OCP\Encryption\IManager;
  34. use OCP\IConfig;
  35. use Symfony\Component\Console\Command\Command;
  36. use Symfony\Component\Console\Helper\QuestionHelper;
  37. use Symfony\Component\Console\Input\InputArgument;
  38. use Symfony\Component\Console\Input\InputInterface;
  39. use Symfony\Component\Console\Output\OutputInterface;
  40. use Symfony\Component\Console\Question\ConfirmationQuestion;
  41. class DecryptAll extends Command {
  42. protected bool $wasTrashbinEnabled = false;
  43. protected bool $wasMaintenanceModeEnabled = false;
  44. public function __construct(
  45. protected IManager $encryptionManager,
  46. protected IAppManager $appManager,
  47. protected IConfig $config,
  48. protected \OC\Encryption\DecryptAll $decryptAll,
  49. protected QuestionHelper $questionHelper,
  50. ) {
  51. parent::__construct();
  52. }
  53. /**
  54. * Set maintenance mode and disable the trashbin app
  55. */
  56. protected function forceMaintenanceAndTrashbin(): void {
  57. $this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin');
  58. $this->wasMaintenanceModeEnabled = $this->config->getSystemValueBool('maintenance');
  59. $this->config->setSystemValue('maintenance', true);
  60. $this->appManager->disableApp('files_trashbin');
  61. }
  62. /**
  63. * Reset the maintenance mode and re-enable the trashbin app
  64. */
  65. protected function resetMaintenanceAndTrashbin(): void {
  66. $this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled);
  67. if ($this->wasTrashbinEnabled) {
  68. $this->appManager->enableApp('files_trashbin');
  69. }
  70. }
  71. protected function configure() {
  72. parent::configure();
  73. $this->setName('encryption:decrypt-all');
  74. $this->setDescription('Disable server-side encryption and decrypt all files');
  75. $this->setHelp(
  76. 'This will disable server-side encryption and decrypt all files for '
  77. . 'all users if it is supported by your encryption module. '
  78. . 'Please make sure that no user access his files during this process!'
  79. );
  80. $this->addArgument(
  81. 'user',
  82. InputArgument::OPTIONAL,
  83. 'user for which you want to decrypt all files (optional)',
  84. ''
  85. );
  86. }
  87. protected function execute(InputInterface $input, OutputInterface $output): int {
  88. if (!$input->isInteractive()) {
  89. $output->writeln('Invalid TTY.');
  90. $output->writeln('If you are trying to execute the command in a Docker ');
  91. $output->writeln("container, do not forget to execute 'docker exec' with");
  92. $output->writeln("the '-i' and '-t' options.");
  93. $output->writeln('');
  94. return 1;
  95. }
  96. $isMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
  97. if ($isMaintenanceModeEnabled) {
  98. $output->writeln("Maintenance mode must be disabled when starting decryption,");
  99. $output->writeln("in order to load the relevant encryption modules correctly.");
  100. $output->writeln("Your instance will automatically be put to maintenance mode");
  101. $output->writeln("during the actual decryption of the files.");
  102. return 1;
  103. }
  104. try {
  105. if ($this->encryptionManager->isEnabled() === true) {
  106. $output->write('Disable server side encryption... ');
  107. $this->config->setAppValue('core', 'encryption_enabled', 'no');
  108. $output->writeln('done.');
  109. } else {
  110. $output->writeln('Server side encryption not enabled. Nothing to do.');
  111. return 0;
  112. }
  113. $uid = $input->getArgument('user');
  114. if ($uid === '') {
  115. $message = 'your Nextcloud';
  116. } else {
  117. $message = "$uid's account";
  118. }
  119. $output->writeln("\n");
  120. $output->writeln("You are about to start to decrypt all files stored in $message.");
  121. $output->writeln('It will depend on the encryption module and your setup if this is possible.');
  122. $output->writeln('Depending on the number and size of your files this can take some time');
  123. $output->writeln('Please make sure that no user access his files during this process!');
  124. $output->writeln('');
  125. $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
  126. if ($this->questionHelper->ask($input, $output, $question)) {
  127. $this->forceMaintenanceAndTrashbin();
  128. $user = $input->getArgument('user');
  129. $result = $this->decryptAll->decryptAll($input, $output, $user);
  130. if ($result === false) {
  131. $output->writeln(' aborted.');
  132. $output->writeln('Server side encryption remains enabled');
  133. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  134. } elseif ($uid !== '') {
  135. $output->writeln('Server side encryption remains enabled');
  136. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  137. }
  138. $this->resetMaintenanceAndTrashbin();
  139. return 0;
  140. }
  141. $output->write('Enable server side encryption... ');
  142. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  143. $output->writeln('done.');
  144. $output->writeln('aborted');
  145. return 1;
  146. } catch (\Exception $e) {
  147. // enable server side encryption again if something went wrong
  148. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  149. $this->resetMaintenanceAndTrashbin();
  150. throw $e;
  151. }
  152. }
  153. }