Skip to content

Commit 7fd453e

Browse files
authored
Merge pull request #38080 from nextcloud/enh/add-occ-command-for-legacy-filekey
Add an occ command to scan files for legacy file key in use and get rid of those
2 parents 25f3678 + 4910888 commit 7fd453e

File tree

7 files changed

+179
-5
lines changed

7 files changed

+179
-5
lines changed

apps/encryption/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Please read the documentation to know all implications before you decide to enab
4242
<command>OCA\Encryption\Command\ScanLegacyFormat</command>
4343
<command>OCA\Encryption\Command\FixEncryptedVersion</command>
4444
<command>OCA\Encryption\Command\FixKeyLocation</command>
45+
<command>OCA\Encryption\Command\DropLegacyFileKey</command>
4546
</commands>
4647

4748
<settings>

apps/encryption/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
1010
'OCA\\Encryption\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
1111
'OCA\\Encryption\\Command\\DisableMasterKey' => $baseDir . '/../lib/Command/DisableMasterKey.php',
12+
'OCA\\Encryption\\Command\\DropLegacyFileKey' => $baseDir . '/../lib/Command/DropLegacyFileKey.php',
1213
'OCA\\Encryption\\Command\\EnableMasterKey' => $baseDir . '/../lib/Command/EnableMasterKey.php',
1314
'OCA\\Encryption\\Command\\FixEncryptedVersion' => $baseDir . '/../lib/Command/FixEncryptedVersion.php',
1415
'OCA\\Encryption\\Command\\FixKeyLocation' => $baseDir . '/../lib/Command/FixKeyLocation.php',

apps/encryption/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ComposerStaticInitEncryption
2424
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
2525
'OCA\\Encryption\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
2626
'OCA\\Encryption\\Command\\DisableMasterKey' => __DIR__ . '/..' . '/../lib/Command/DisableMasterKey.php',
27+
'OCA\\Encryption\\Command\\DropLegacyFileKey' => __DIR__ . '/..' . '/../lib/Command/DropLegacyFileKey.php',
2728
'OCA\\Encryption\\Command\\EnableMasterKey' => __DIR__ . '/..' . '/../lib/Command/EnableMasterKey.php',
2829
'OCA\\Encryption\\Command\\FixEncryptedVersion' => __DIR__ . '/..' . '/../lib/Command/FixEncryptedVersion.php',
2930
'OCA\\Encryption\\Command\\FixKeyLocation' => __DIR__ . '/..' . '/../lib/Command/FixKeyLocation.php',

apps/encryption/composer/composer/installed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'name' => '__root__',
44
'pretty_version' => 'dev-master',
55
'version' => 'dev-master',
6-
'reference' => 'dd3d689e04a5e1d558da937ca72980e0e2c7c404',
6+
'reference' => 'ffa8d21f37c8ccf968974b6aeb828e3e84287b94',
77
'type' => 'library',
88
'install_path' => __DIR__ . '/../',
99
'aliases' => array(),
@@ -13,7 +13,7 @@
1313
'__root__' => array(
1414
'pretty_version' => 'dev-master',
1515
'version' => 'dev-master',
16-
'reference' => 'dd3d689e04a5e1d558da937ca72980e0e2c7c404',
16+
'reference' => 'ffa8d21f37c8ccf968974b6aeb828e3e84287b94',
1717
'type' => 'library',
1818
'install_path' => __DIR__ . '/../',
1919
'aliases' => array(),
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023, Côme Chilliet <come.chilliet@nextcloud.com>
7+
*
8+
* @author Côme Chilliet <come.chilliet@nextcloud.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
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
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\Encryption\Command;
28+
29+
use OC\Encryption\Exceptions\DecryptionFailedException;
30+
use OC\Files\FileInfo;
31+
use OC\Files\View;
32+
use OCA\Encryption\KeyManager;
33+
use OCP\Encryption\Exceptions\GenericEncryptionException;
34+
use OCP\IUserManager;
35+
use Symfony\Component\Console\Command\Command;
36+
use Symfony\Component\Console\Input\InputInterface;
37+
use Symfony\Component\Console\Output\OutputInterface;
38+
39+
class DropLegacyFileKey extends Command {
40+
private View $rootView;
41+
42+
public function __construct(
43+
private IUserManager $userManager,
44+
private KeyManager $keyManager,
45+
) {
46+
parent::__construct();
47+
48+
$this->rootView = new View();
49+
}
50+
51+
protected function configure(): void {
52+
$this
53+
->setName('encryption:drop-legacy-filekey')
54+
->setDescription('Scan the files for the legacy filekey format using RC4 and get rid of it (if master key is enabled)');
55+
}
56+
57+
protected function execute(InputInterface $input, OutputInterface $output): int {
58+
$result = true;
59+
60+
$output->writeln('<info>Scanning all files for legacy filekey</info>');
61+
62+
foreach ($this->userManager->getBackends() as $backend) {
63+
$limit = 500;
64+
$offset = 0;
65+
do {
66+
$users = $backend->getUsers('', $limit, $offset);
67+
foreach ($users as $user) {
68+
$output->writeln('Scanning all files for ' . $user);
69+
$this->setupUserFS($user);
70+
$result = $result && $this->scanFolder($output, '/' . $user);
71+
}
72+
$offset += $limit;
73+
} while (count($users) >= $limit);
74+
}
75+
76+
if ($result) {
77+
$output->writeln('All scanned files are properly encrypted.');
78+
return 0;
79+
}
80+
81+
return 1;
82+
}
83+
84+
private function scanFolder(OutputInterface $output, string $folder): bool {
85+
$clean = true;
86+
87+
foreach ($this->rootView->getDirectoryContent($folder) as $item) {
88+
$path = $folder . '/' . $item['name'];
89+
if ($this->rootView->is_dir($path)) {
90+
if ($this->scanFolder($output, $path) === false) {
91+
$clean = false;
92+
}
93+
} else {
94+
if (!$item->isEncrypted()) {
95+
// ignore
96+
continue;
97+
}
98+
99+
$stats = $this->rootView->stat($path);
100+
if (!isset($stats['hasHeader']) || $stats['hasHeader'] === false) {
101+
$clean = false;
102+
$output->writeln('<error>' . $path . ' does not have a proper header</error>');
103+
} else {
104+
try {
105+
$legacyFileKey = $this->keyManager->getFileKey($path, null, true);
106+
if ($legacyFileKey === '') {
107+
$output->writeln('Got an empty legacy filekey for ' . $path . ', continuing', OutputInterface::VERBOSITY_VERBOSE);
108+
continue;
109+
}
110+
} catch (GenericEncryptionException $e) {
111+
$output->writeln('Got a decryption error for legacy filekey for ' . $path . ', continuing', OutputInterface::VERBOSITY_VERBOSE);
112+
continue;
113+
}
114+
/* If that did not throw and filekey is not empty, a legacy filekey is used */
115+
$clean = false;
116+
$output->writeln($path . ' is using a legacy filekey, migrating');
117+
$this->migrateSinglefile($path, $item, $output);
118+
}
119+
}
120+
}
121+
122+
return $clean;
123+
}
124+
125+
private function migrateSinglefile(string $path, FileInfo $fileInfo, OutputInterface $output): void {
126+
$source = $path;
127+
$target = $path . '.reencrypted.' . time();
128+
129+
try {
130+
$this->rootView->copy($source, $target);
131+
$copyResource = $this->rootView->fopen($target, 'r');
132+
$sourceResource = $this->rootView->fopen($source, 'w');
133+
if ($copyResource === false || $sourceResource === false) {
134+
throw new DecryptionFailedException('Failed to open '.$source.' or '.$target);
135+
}
136+
if (stream_copy_to_stream($copyResource, $sourceResource) === false) {
137+
$output->writeln('<error>Failed to copy '.$target.' data into '.$source.'</error>');
138+
$output->writeln('<error>Leaving both files in there to avoid data loss</error>');
139+
return;
140+
}
141+
$this->rootView->touch($source, $fileInfo->getMTime());
142+
$this->rootView->unlink($target);
143+
$output->writeln('<info>Migrated ' . $source . '</info>', OutputInterface::VERBOSITY_VERBOSE);
144+
} catch (DecryptionFailedException $e) {
145+
if ($this->rootView->file_exists($target)) {
146+
$this->rootView->unlink($target);
147+
}
148+
$output->writeln('<error>Failed to migrate ' . $path . '</error>');
149+
$output->writeln('<error>' . $e . '</error>', OutputInterface::VERBOSITY_VERBOSE);
150+
} finally {
151+
if (is_resource($copyResource)) {
152+
fclose($copyResource);
153+
}
154+
if (is_resource($sourceResource)) {
155+
fclose($sourceResource);
156+
}
157+
}
158+
}
159+
160+
/**
161+
* setup user file system
162+
*/
163+
protected function setupUserFS(string $uid): void {
164+
\OC_Util::tearDownFS();
165+
\OC_Util::setupFS($uid);
166+
}
167+
}

apps/encryption/lib/Command/ScanLegacyFormat.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
use Symfony\Component\Console\Output\OutputInterface;
3737

3838
class ScanLegacyFormat extends Command {
39-
4039
/** @var Util */
4140
protected $util;
4241

@@ -89,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8988
foreach ($users as $user) {
9089
$output->writeln('Scanning all files for ' . $user);
9190
$this->setupUserFS($user);
92-
$result &= $this->scanFolder($output, '/' . $user);
91+
$result = $result && $this->scanFolder($output, '/' . $user);
9392
}
9493
$offset += $limit;
9594
} while (count($users) >= $limit);

apps/encryption/lib/Crypto/Encryption.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,12 @@ public function end($path, $position = '0') {
309309

310310
$publicKeys = $this->keyManager->addSystemKeys($this->accessList, $publicKeys, $this->getOwner($path));
311311
$shareKeys = $this->crypt->multiKeyEncrypt($this->fileKey, $publicKeys);
312-
$this->keyManager->deleteLegacyFileKey($this->path);
312+
if (!$this->keyManager->deleteLegacyFileKey($this->path)) {
313+
$this->logger->warning(
314+
'Failed to delete legacy filekey for {path}',
315+
['app' => 'encryption', 'path' => $path]
316+
);
317+
}
313318
foreach ($shareKeys as $uid => $keyFile) {
314319
$this->keyManager->setShareKey($this->path, $uid, $keyFile);
315320
}

0 commit comments

Comments
 (0)