2015-08-24 09:56:04 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-08-24 09:56:04 -04:00
|
|
|
/**
|
2024-05-24 13:43:47 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-08-24 09:56:04 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Core\Command\Encryption;
|
|
|
|
|
|
|
|
|
|
use OCP\App\IAppManager;
|
|
|
|
|
use OCP\Encryption\IManager;
|
|
|
|
|
use OCP\IConfig;
|
2025-11-26 14:05:29 -05:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-08-24 09:56:04 -04:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper;
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
|
|
|
|
|
|
|
|
|
class EncryptAll extends Command {
|
2022-04-12 11:55:01 -04:00
|
|
|
protected bool $wasTrashbinEnabled = false;
|
2015-08-24 09:56:04 -04:00
|
|
|
|
|
|
|
|
public function __construct(
|
2023-06-12 10:22:30 -04:00
|
|
|
protected IManager $encryptionManager,
|
|
|
|
|
protected IAppManager $appManager,
|
|
|
|
|
protected IConfig $config,
|
|
|
|
|
protected QuestionHelper $questionHelper,
|
2025-11-26 14:05:29 -05:00
|
|
|
private LoggerInterface $logger,
|
2015-08-24 09:56:04 -04:00
|
|
|
) {
|
|
|
|
|
parent::__construct();
|
2015-09-18 02:53:02 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-26 14:05:29 -05:00
|
|
|
protected function configure(): void {
|
2015-08-24 09:56:04 -04:00
|
|
|
parent::configure();
|
|
|
|
|
|
2025-11-26 14:05:29 -05:00
|
|
|
$this
|
|
|
|
|
->setName('encryption:encrypt-all')
|
|
|
|
|
->setDescription('Encrypt all users\' files using Nextcloud Server Side Encryption')
|
|
|
|
|
->setHelp(
|
|
|
|
|
"This will encrypt all files server-side for all users.\n" .
|
|
|
|
|
"Maintenance mode will be enabled automatically.\n" .
|
|
|
|
|
"Users should not access their files during this process!\n" .
|
|
|
|
|
"WARNING: Please read the documentation prior to utilizing this feature to avoid data loss!"
|
2015-08-24 09:56:04 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-26 08:54:51 -04:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
2025-11-26 14:05:29 -05:00
|
|
|
|
|
|
|
|
// Handle container environment TTY problems to avoid confusion
|
2020-04-09 10:07:47 -04:00
|
|
|
if (!$input->isInteractive()) {
|
2025-11-26 14:05:29 -05:00
|
|
|
$output->writeln('<error>Invalid TTY.</error>');
|
|
|
|
|
$output->writeln("<comment>If running inside Docker, use 'docker exec -it' or equivalent.</comment>");
|
2018-07-29 11:02:42 -04:00
|
|
|
$output->writeln('');
|
2025-11-26 14:05:29 -05:00
|
|
|
return self::FAILURE;
|
2018-07-29 11:02:42 -04:00
|
|
|
}
|
2015-08-24 09:56:04 -04:00
|
|
|
|
2025-11-26 14:05:29 -05:00
|
|
|
// Prevent running if SSE isn't enabled
|
2015-08-24 09:56:04 -04:00
|
|
|
if ($this->encryptionManager->isEnabled() === false) {
|
2025-11-26 14:05:29 -05:00
|
|
|
$output->writeln('<error>Server Side Encryption is not enabled; unable to encrypt files.</error>');
|
|
|
|
|
return self::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prevent running if no valid SSE modules are registered
|
|
|
|
|
$modules = $this->encryptionManager->getEncryptionModules();
|
|
|
|
|
if (empty($modules)) {
|
|
|
|
|
$output->writeln('<error>No Server Side Encryption modules are registered; unable to encrypt files.</error>');
|
|
|
|
|
return self::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prevent running if a default SSE module isn't already configured
|
|
|
|
|
$defaultModuleId = $this->encryptionManager->getDefaultEncryptionModuleId();
|
|
|
|
|
if ($defaultModuleId === '') {
|
|
|
|
|
$output->writeln('<error>A default Server Side Encryption module is not configured; unable to encrypt files.</error>');
|
|
|
|
|
return self::FAILURE;
|
2015-08-24 09:56:04 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-26 14:05:29 -05:00
|
|
|
// Prevent running if the default SSE module isn't valid
|
|
|
|
|
if (!isset($modules[$defaultModuleId])) {
|
|
|
|
|
$output->writeln('<error>The current default Server Side Encryption module does not exist: ' . $defaultModuleId . '; unable to encrypt files.</error>');
|
|
|
|
|
return self::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prevent running if maintenance mode is already enabled
|
2024-11-21 11:19:33 -05:00
|
|
|
if ($this->config->getSystemValueBool('maintenance')) {
|
|
|
|
|
$output->writeln('<error>This command cannot be run with maintenance mode enabled.</error>');
|
|
|
|
|
return self::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-26 14:05:29 -05:00
|
|
|
// TODO: Might make sense to add some additional readiness checks here such as the readiness of key storage/etc
|
|
|
|
|
|
2015-08-24 09:56:04 -04:00
|
|
|
$output->writeln("\n");
|
2017-03-19 23:58:12 -04:00
|
|
|
$output->writeln('You are about to encrypt all files stored in your Nextcloud installation.');
|
2025-11-26 14:05:29 -05:00
|
|
|
$output->writeln('Depending on the number and size of files, this may take a long time.');
|
|
|
|
|
$output->writeln('Please ensure that no user accesses their files during this process!');
|
|
|
|
|
$output->writeln('Note: The encryption module you use and its settings determine which files get encrypted.');
|
|
|
|
|
$output->writeln('Reminder: If External Storage is included in encryption, those files will no longer be accessible outside Nextcloud.');
|
|
|
|
|
$output->writeln('WARNING: Please read the documentation prior to utilizing this feature to avoid data loss!');
|
2015-08-24 09:56:04 -04:00
|
|
|
$output->writeln('');
|
2015-09-18 02:53:02 -04:00
|
|
|
|
2025-11-26 14:05:29 -05:00
|
|
|
// require "yes" to be typed in fully since this is a sensitive action
|
|
|
|
|
$question = new ConfirmationQuestion('Do you really want to continue? (yes/NO) ', false, '/^yes$/i');
|
|
|
|
|
if (!$this->questionHelper->ask($input, $output, $question)) {
|
|
|
|
|
$output->writeln("\n" . 'Aborted.');
|
|
|
|
|
return self::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Requirements before proceeding: disable trash bin, enable maintenance mode
|
|
|
|
|
$this->forceMaintenanceAndTrashbin();
|
|
|
|
|
|
|
|
|
|
// Encrypt all the files
|
|
|
|
|
try {
|
|
|
|
|
$defaultModule = $this->encryptionManager->getEncryptionModule();
|
|
|
|
|
$defaultModule->encryptAll($input, $output);
|
|
|
|
|
} catch (\Throwable $ex) {
|
|
|
|
|
$output->writeln('<error>Encryption failed: ' . $ex->getMessage() . '</error>');
|
|
|
|
|
$this->logger->error('encryption:encrypt-all failed', ['exception' => $ex]);
|
|
|
|
|
return self::FAILURE;
|
|
|
|
|
} finally {
|
|
|
|
|
// restore state no matter what (XXX: Better coverage than prior behavior; though I'm not convinced either is ideal)
|
2017-02-23 00:02:31 -05:00
|
|
|
$this->resetMaintenanceAndTrashbin();
|
2015-08-24 09:56:04 -04:00
|
|
|
}
|
2025-11-26 14:05:29 -05:00
|
|
|
// If we made it here, we're good
|
|
|
|
|
return self::SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set maintenance mode and disable the trashbin app
|
|
|
|
|
* TODO: The "why?" should be noted here.
|
|
|
|
|
*/
|
|
|
|
|
protected function forceMaintenanceAndTrashbin(): void {
|
|
|
|
|
$this->wasTrashbinEnabled = (bool)$this->appManager->isEnabledForUser('files_trashbin');
|
|
|
|
|
$this->config->setSystemValue('maintenance', true);
|
|
|
|
|
$this->appManager->disableApp('files_trashbin');
|
|
|
|
|
// TODO: Determine whether files_versions should be disabled temporarily too
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset the maintenance mode and re-enable the trashbin app
|
|
|
|
|
*/
|
|
|
|
|
protected function resetMaintenanceAndTrashbin(): void {
|
|
|
|
|
$this->config->setSystemValue('maintenance', false);
|
|
|
|
|
if ($this->wasTrashbinEnabled) {
|
|
|
|
|
$this->appManager->enableApp('files_trashbin');
|
|
|
|
|
}
|
2015-08-24 09:56:04 -04:00
|
|
|
}
|
|
|
|
|
}
|