DecryptAll.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @author Björn Schießle <bjoern@schiessle.org>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2016, 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. }
  102. protected function execute(InputInterface $input, OutputInterface $output) {
  103. try {
  104. if ($this->encryptionManager->isEnabled() === true) {
  105. $output->write('Disable server side encryption... ');
  106. $this->config->setAppValue('core', 'encryption_enabled', 'no');
  107. $output->writeln('done.');
  108. } else {
  109. $output->writeln('Server side encryption not enabled. Nothing to do.');
  110. return;
  111. }
  112. $uid = $input->getArgument('user');
  113. if ($uid === '') {
  114. $message = 'your ownCloud';
  115. } else {
  116. $message = "$uid's account";
  117. }
  118. $output->writeln("\n");
  119. $output->writeln("You are about to start to decrypt all files stored in $message.");
  120. $output->writeln('It will depend on the encryption module and your setup if this is possible.');
  121. $output->writeln('Depending on the number and size of your files this can take some time');
  122. $output->writeln('Please make sure that no user access his files during this process!');
  123. $output->writeln('');
  124. $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
  125. if ($this->questionHelper->ask($input, $output, $question)) {
  126. $this->forceSingleUserAndTrashbin();
  127. $user = $input->getArgument('user');
  128. $result = $this->decryptAll->decryptAll($input, $output, $user);
  129. if ($result === false) {
  130. $output->writeln(' aborted.');
  131. $output->writeln('Server side encryption remains enabled');
  132. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  133. }
  134. $this->resetSingleUserAndTrashbin();
  135. } else {
  136. $output->write('Enable server side encryption... ');
  137. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  138. $output->writeln('done.');
  139. $output->writeln('aborted');
  140. }
  141. } catch (\Exception $e) {
  142. // enable server side encryption again if something went wrong
  143. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  144. $this->resetSingleUserAndTrashbin();
  145. throw $e;
  146. }
  147. }
  148. }