decryptall.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Core\Command\Encryption;
  23. use OCP\App\IAppManager;
  24. use OCP\Encryption\IManager;
  25. use OCP\IConfig;
  26. use Symfony\Component\Console\Command\Command;
  27. use Symfony\Component\Console\Helper\QuestionHelper;
  28. use Symfony\Component\Console\Input\InputArgument;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. use Symfony\Component\Console\Question\ConfirmationQuestion;
  32. class DecryptAll extends Command {
  33. /** @var IManager */
  34. protected $encryptionManager;
  35. /** @var IAppManager */
  36. protected $appManager;
  37. /** @var IConfig */
  38. protected $config;
  39. /** @var QuestionHelper */
  40. protected $questionHelper;
  41. /** @var bool */
  42. protected $wasTrashbinEnabled;
  43. /** @var bool */
  44. protected $wasSingleUserModeEnabled;
  45. /** @var \OC\Encryption\DecryptAll */
  46. protected $decryptAll;
  47. /**
  48. * @param IManager $encryptionManager
  49. * @param IAppManager $appManager
  50. * @param IConfig $config
  51. * @param \OC\Encryption\DecryptAll $decryptAll
  52. * @param QuestionHelper $questionHelper
  53. */
  54. public function __construct(
  55. IManager $encryptionManager,
  56. IAppManager $appManager,
  57. IConfig $config,
  58. \OC\Encryption\DecryptAll $decryptAll,
  59. QuestionHelper $questionHelper
  60. ) {
  61. parent::__construct();
  62. $this->appManager = $appManager;
  63. $this->encryptionManager = $encryptionManager;
  64. $this->config = $config;
  65. $this->decryptAll = $decryptAll;
  66. $this->questionHelper = $questionHelper;
  67. }
  68. /**
  69. * Set single user mode and disable the trashbin app
  70. */
  71. protected function forceSingleUserAndTrashbin() {
  72. $this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin');
  73. $this->wasSingleUserModeEnabled = $this->config->getSystemValue('singleuser', false);
  74. $this->config->setSystemValue('singleuser', true);
  75. $this->appManager->disableApp('files_trashbin');
  76. }
  77. /**
  78. * Reset the single user mode and re-enable the trashbin app
  79. */
  80. protected function resetSingleUserAndTrashbin() {
  81. $this->config->setSystemValue('singleuser', $this->wasSingleUserModeEnabled);
  82. if ($this->wasTrashbinEnabled) {
  83. $this->appManager->enableApp('files_trashbin');
  84. }
  85. }
  86. protected function configure() {
  87. parent::configure();
  88. $this->setName('encryption:decrypt-all');
  89. $this->setDescription('Disable server-side encryption and decrypt all files');
  90. $this->setHelp(
  91. 'This will disable server-side encryption and decrypt all files for '
  92. . 'all users if it is supported by your encryption module. '
  93. . 'Please make sure that no user access his files during this process!'
  94. );
  95. $this->addArgument(
  96. 'user',
  97. InputArgument::OPTIONAL,
  98. 'user for which you want to decrypt all files (optional)'
  99. );
  100. }
  101. protected function execute(InputInterface $input, OutputInterface $output) {
  102. try {
  103. if ($this->encryptionManager->isEnabled() === true) {
  104. $output->write('Disable server side encryption... ');
  105. $this->config->setAppValue('core', 'encryption_enabled', 'no');
  106. $output->writeln('done.');
  107. } else {
  108. $output->writeln('Server side encryption not enabled. Nothing to do.');
  109. return;
  110. }
  111. $output->writeln("\n");
  112. $output->writeln('You are about to start to decrypt all files stored in your ownCloud.');
  113. $output->writeln('It will depend on the encryption module and your setup if this is possible.');
  114. $output->writeln('Depending on the number and size of your files this can take some time');
  115. $output->writeln('Please make sure that no user access his files during this process!');
  116. $output->writeln('');
  117. $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
  118. if ($this->questionHelper->ask($input, $output, $question)) {
  119. $this->forceSingleUserAndTrashbin();
  120. $user = $input->getArgument('user');
  121. $result = $this->decryptAll->decryptAll($input, $output, $user);
  122. if ($result === false) {
  123. $output->writeln(' aborted.');
  124. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  125. }
  126. $this->resetSingleUserAndTrashbin();
  127. } else {
  128. $output->write('Enable server side encryption... ');
  129. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  130. $output->writeln('done.');
  131. $output->writeln('aborted');
  132. }
  133. } catch (\Exception $e) {
  134. // enable server side encryption again if something went wrong
  135. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  136. $this->resetSingleUserAndTrashbin();
  137. throw $e;
  138. }
  139. }
  140. }