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.

145 lines
4.5 KiB

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