EncryptAll.php 4.5 KB

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