You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DecryptAll.php 6.5KB

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