EncryptAll.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Evgeny Golyshev <eugulixes@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Matthew Setter <matthew@matthewsetter.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Core\Command\Encryption;
  28. use OCP\App\IAppManager;
  29. use OCP\Encryption\IManager;
  30. use OCP\IConfig;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Helper\QuestionHelper;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. use Symfony\Component\Console\Question\ConfirmationQuestion;
  36. class EncryptAll extends Command {
  37. /** @var IManager */
  38. protected $encryptionManager;
  39. /** @var IAppManager */
  40. protected $appManager;
  41. /** @var IConfig */
  42. protected $config;
  43. /** @var QuestionHelper */
  44. protected $questionHelper;
  45. /** @var bool */
  46. protected $wasTrashbinEnabled;
  47. /** @var bool */
  48. protected $wasMaintenanceModeEnabled;
  49. /**
  50. * @param IManager $encryptionManager
  51. * @param IAppManager $appManager
  52. * @param IConfig $config
  53. * @param QuestionHelper $questionHelper
  54. */
  55. public function __construct(
  56. IManager $encryptionManager,
  57. IAppManager $appManager,
  58. IConfig $config,
  59. QuestionHelper $questionHelper
  60. ) {
  61. parent::__construct();
  62. $this->appManager = $appManager;
  63. $this->encryptionManager = $encryptionManager;
  64. $this->config = $config;
  65. $this->questionHelper = $questionHelper;
  66. }
  67. /**
  68. * Set maintenance mode and disable the trashbin app
  69. */
  70. protected function forceMaintenanceAndTrashbin() {
  71. $this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin');
  72. $this->wasMaintenanceModeEnabled = $this->config->getSystemValueBool('maintenance');
  73. $this->config->setSystemValue('maintenance', true);
  74. $this->appManager->disableApp('files_trashbin');
  75. }
  76. /**
  77. * Reset the maintenance mode and re-enable the trashbin app
  78. */
  79. protected function resetMaintenanceAndTrashbin() {
  80. $this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled);
  81. if ($this->wasTrashbinEnabled) {
  82. $this->appManager->enableApp('files_trashbin');
  83. }
  84. }
  85. protected function configure() {
  86. parent::configure();
  87. $this->setName('encryption:encrypt-all');
  88. $this->setDescription('Encrypt all files for all users');
  89. $this->setHelp(
  90. 'This will encrypt all files for all users. '
  91. . 'Please make sure that no user access his files during this process!'
  92. );
  93. }
  94. protected function execute(InputInterface $input, OutputInterface $output) {
  95. if (!$input->isInteractive()) {
  96. $output->writeln('Invalid TTY.');
  97. $output->writeln('If you are trying to execute the command in a Docker ');
  98. $output->writeln("container, do not forget to execute 'docker exec' with");
  99. $output->writeln("the '-i' and '-t' options.");
  100. $output->writeln('');
  101. return;
  102. }
  103. if ($this->encryptionManager->isEnabled() === false) {
  104. throw new \Exception('Server side encryption is not enabled');
  105. }
  106. $output->writeln("\n");
  107. $output->writeln('You are about to encrypt all files stored in your Nextcloud installation.');
  108. $output->writeln('Depending on the number of available files, and their size, this may take quite some time.');
  109. $output->writeln('Please ensure that no user accesses their files during this time!');
  110. $output->writeln('Note: The encryption module you use determines which files get encrypted.');
  111. $output->writeln('');
  112. $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
  113. if ($this->questionHelper->ask($input, $output, $question)) {
  114. $this->forceMaintenanceAndTrashbin();
  115. try {
  116. $defaultModule = $this->encryptionManager->getEncryptionModule();
  117. $defaultModule->encryptAll($input, $output);
  118. } catch (\Exception $ex) {
  119. $this->resetMaintenanceAndTrashbin();
  120. throw $ex;
  121. }
  122. $this->resetMaintenanceAndTrashbin();
  123. } else {
  124. $output->writeln('aborted');
  125. }
  126. }
  127. }