DecryptAll.php 6.7 KB

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