DecryptAll.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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->getSystemValue('maintenance', false);
  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. try {
  118. if ($this->encryptionManager->isEnabled() === true) {
  119. $output->write('Disable server side encryption... ');
  120. $this->config->setAppValue('core', 'encryption_enabled', 'no');
  121. $output->writeln('done.');
  122. } else {
  123. $output->writeln('Server side encryption not enabled. Nothing to do.');
  124. return;
  125. }
  126. $uid = $input->getArgument('user');
  127. if ($uid === '') {
  128. $message = 'your Nextcloud';
  129. } else {
  130. $message = "$uid's account";
  131. }
  132. $output->writeln("\n");
  133. $output->writeln("You are about to start to decrypt all files stored in $message.");
  134. $output->writeln('It will depend on the encryption module and your setup if this is possible.');
  135. $output->writeln('Depending on the number and size of your files this can take some time');
  136. $output->writeln('Please make sure that no user access his files during this process!');
  137. $output->writeln('');
  138. $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
  139. if ($this->questionHelper->ask($input, $output, $question)) {
  140. $this->forceMaintenanceAndTrashbin();
  141. $user = $input->getArgument('user');
  142. $result = $this->decryptAll->decryptAll($input, $output, $user);
  143. if ($result === false) {
  144. $output->writeln(' aborted.');
  145. $output->writeln('Server side encryption remains enabled');
  146. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  147. } else if ($uid !== '') {
  148. $output->writeln('Server side encryption remains enabled');
  149. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  150. }
  151. $this->resetMaintenanceAndTrashbin();
  152. } else {
  153. $output->write('Enable server side encryption... ');
  154. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  155. $output->writeln('done.');
  156. $output->writeln('aborted');
  157. }
  158. } catch (\Exception $e) {
  159. // enable server side encryption again if something went wrong
  160. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  161. $this->resetMaintenanceAndTrashbin();
  162. throw $e;
  163. }
  164. }
  165. }