aboutsummaryrefslogtreecommitdiffstats
path: root/apps/encryption/lib/Command/FixEncryptedVersion.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/encryption/lib/Command/FixEncryptedVersion.php')
-rw-r--r--apps/encryption/lib/Command/FixEncryptedVersion.php79
1 files changed, 33 insertions, 46 deletions
diff --git a/apps/encryption/lib/Command/FixEncryptedVersion.php b/apps/encryption/lib/Command/FixEncryptedVersion.php
index d0fbf1adb31..462e3a5cc2a 100644
--- a/apps/encryption/lib/Command/FixEncryptedVersion.php
+++ b/apps/encryption/lib/Command/FixEncryptedVersion.php
@@ -1,23 +1,9 @@
<?php
+
/**
- * @author Sujith Haridasan <sharidasan@owncloud.com>
- * @author Ilja Neumann <ineumann@owncloud.com>
- *
- * @copyright Copyright (c) 2019, ownCloud GmbH
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * SPDX-FileCopyrightText: 2021-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2019 ownCloud GmbH
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Encryption\Command;
@@ -26,6 +12,7 @@ use OC\Files\Storage\Wrapper\Encryption;
use OC\Files\View;
use OC\ServerNotAvailableException;
use OCA\Encryption\Util;
+use OCP\Encryption\Exceptions\InvalidHeaderException;
use OCP\Files\IRootFolder;
use OCP\HintException;
use OCP\IConfig;
@@ -39,7 +26,7 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class FixEncryptedVersion extends Command {
- private bool $supportLegacy;
+ private bool $supportLegacy = false;
public function __construct(
private IConfig $config,
@@ -49,8 +36,6 @@ class FixEncryptedVersion extends Command {
private Util $util,
private View $view,
) {
- $this->supportLegacy = false;
-
parent::__construct();
}
@@ -83,47 +68,49 @@ class FixEncryptedVersion extends Command {
if ($skipSignatureCheck) {
$output->writeln("<error>Repairing is not possible when \"encryption_skip_signature_check\" is set. Please disable this flag in the configuration.</error>\n");
- return 1;
+ return self::FAILURE;
}
if (!$this->util->isMasterKeyEnabled()) {
$output->writeln("<error>Repairing only works with master key encryption.</error>\n");
- return 1;
+ return self::FAILURE;
}
$user = $input->getArgument('user');
$all = $input->getOption('all');
$pathOption = \trim(($input->getOption('path') ?? ''), '/');
+ if (!$user && !$all) {
+ $output->writeln('Either a user id or --all needs to be provided');
+ return self::FAILURE;
+ }
+
if ($user) {
if ($all) {
- $output->writeln("Specifying a user id and --all are mutually exclusive");
- return 1;
+ $output->writeln('Specifying a user id and --all are mutually exclusive');
+ return self::FAILURE;
}
if ($this->userManager->get($user) === null) {
$output->writeln("<error>User id $user does not exist. Please provide a valid user id</error>");
- return 1;
+ return self::FAILURE;
}
return $this->runForUser($user, $pathOption, $output);
- } elseif ($all) {
- $result = 0;
- $this->userManager->callForSeenUsers(function (IUser $user) use ($pathOption, $output, &$result) {
- $output->writeln("Processing files for " . $user->getUID());
- $result = $this->runForUser($user->getUID(), $pathOption, $output);
- return $result === 0;
- });
- return $result;
- } else {
- $output->writeln("Either a user id or --all needs to be provided");
- return 1;
}
+
+ $result = 0;
+ $this->userManager->callForSeenUsers(function (IUser $user) use ($pathOption, $output, &$result) {
+ $output->writeln('Processing files for ' . $user->getUID());
+ $result = $this->runForUser($user->getUID(), $pathOption, $output);
+ return $result === 0;
+ });
+ return $result;
}
private function runForUser(string $user, string $pathOption, OutputInterface $output): int {
$pathToWalk = "/$user/files";
- if ($pathOption !== "") {
+ if ($pathOption !== '') {
$pathToWalk = "$pathToWalk/$pathOption";
}
return $this->walkPathOfUser($user, $pathToWalk, $output);
@@ -136,13 +123,13 @@ class FixEncryptedVersion extends Command {
$this->setupUserFs($user);
if (!$this->view->file_exists($path)) {
$output->writeln("<error>Path \"$path\" does not exist. Please provide a valid path.</error>");
- return 1;
+ return self::FAILURE;
}
if ($this->view->is_file($path)) {
$output->writeln("Verifying the content of file \"$path\"");
$this->verifyFileContent($path, $output);
- return 0;
+ return self::SUCCESS;
}
$directories = [];
$directories[] = $path;
@@ -158,7 +145,7 @@ class FixEncryptedVersion extends Command {
}
}
}
- return 0;
+ return self::SUCCESS;
}
/**
@@ -210,7 +197,7 @@ class FixEncryptedVersion extends Command {
\fclose($handle);
return true;
- } catch (ServerNotAvailableException $e) {
+ } catch (ServerNotAvailableException|InvalidHeaderException $e) {
// not a "bad signature" error and likely "legacy cipher" exception
// this could mean that the file is maybe not encrypted but the encrypted version is set
if (!$this->supportLegacy && $ignoreCorrectEncVersionCall === true) {
@@ -219,7 +206,7 @@ class FixEncryptedVersion extends Command {
}
return false;
} catch (HintException $e) {
- $this->logger->warning("Issue: " . $e->getMessage());
+ $this->logger->warning('Issue: ' . $e->getMessage());
// If allowOnce is set to false, this becomes recursive.
if ($ignoreCorrectEncVersionCall === true) {
// Lets rectify the file by correcting encrypted version
@@ -268,7 +255,7 @@ class FixEncryptedVersion extends Command {
// try with zero first
$cacheInfo = ['encryptedVersion' => 0, 'encrypted' => 0];
$cache->put($fileCache->getPath(), $cacheInfo);
- $output->writeln("<info>Set the encrypted version to 0 (unencrypted)</info>");
+ $output->writeln('<info>Set the encrypted version to 0 (unencrypted)</info>');
if ($this->verifyFileContent($path, $output, false) === true) {
$output->writeln("<info>Fixed the file: \"$path\" with version 0 (unencrypted)</info>");
return true;
@@ -282,7 +269,7 @@ class FixEncryptedVersion extends Command {
$cache->put($fileCache->getPath(), $cacheInfo);
$output->writeln("<info>Decrement the encrypted version to $encryptedVersion</info>");
if ($this->verifyFileContent($path, $output, false) === true) {
- $output->writeln("<info>Fixed the file: \"$path\" with version " . $encryptedVersion . "</info>");
+ $output->writeln("<info>Fixed the file: \"$path\" with version " . $encryptedVersion . '</info>');
return true;
}
$encryptedVersion--;
@@ -305,7 +292,7 @@ class FixEncryptedVersion extends Command {
$cache->put($fileCache->getPath(), $cacheInfo);
$output->writeln("<info>Increment the encrypted version to $newEncryptedVersion</info>");
if ($this->verifyFileContent($path, $output, false) === true) {
- $output->writeln("<info>Fixed the file: \"$path\" with version " . $newEncryptedVersion . "</info>");
+ $output->writeln("<info>Fixed the file: \"$path\" with version " . $newEncryptedVersion . '</info>');
return true;
}
$increment++;