DecryptAll.php 6.4 KB

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