Skip to content

Commit 19d7de6

Browse files
committed
Add legacy scanning command
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
1 parent 309c85d commit 19d7de6

File tree

5 files changed

+145
-1
lines changed

5 files changed

+145
-1
lines changed

apps/encryption/appinfo/info.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Please read the documentation to know all implications before you decide
1515
to enable server-side encryption.
1616
</description>
17-
<version>2.8.0</version>
17+
<version>2.8.1</version>
1818
<licence>agpl</licence>
1919
<author>Bjoern Schiessle</author>
2020
<author>Clark Tomlinson</author>
@@ -44,6 +44,7 @@
4444
<command>OCA\Encryption\Command\EnableMasterKey</command>
4545
<command>OCA\Encryption\Command\DisableMasterKey</command>
4646
<command>OCA\Encryption\Command\RecoverUser</command>
47+
<command>OCA\Encryption\Command\ScanLegacyFormat</command>
4748
</commands>
4849

4950
<settings>

apps/encryption/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'OCA\\Encryption\\Command\\DisableMasterKey' => $baseDir . '/../lib/Command/DisableMasterKey.php',
1111
'OCA\\Encryption\\Command\\EnableMasterKey' => $baseDir . '/../lib/Command/EnableMasterKey.php',
1212
'OCA\\Encryption\\Command\\RecoverUser' => $baseDir . '/../lib/Command/RecoverUser.php',
13+
'OCA\\Encryption\\Command\\ScanLegacyFormat' => $baseDir . '/../lib/Command/ScanLegacyFormat.php',
1314
'OCA\\Encryption\\Controller\\RecoveryController' => $baseDir . '/../lib/Controller/RecoveryController.php',
1415
'OCA\\Encryption\\Controller\\SettingsController' => $baseDir . '/../lib/Controller/SettingsController.php',
1516
'OCA\\Encryption\\Controller\\StatusController' => $baseDir . '/../lib/Controller/StatusController.php',

apps/encryption/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ComposerStaticInitEncryption
2525
'OCA\\Encryption\\Command\\DisableMasterKey' => __DIR__ . '/..' . '/../lib/Command/DisableMasterKey.php',
2626
'OCA\\Encryption\\Command\\EnableMasterKey' => __DIR__ . '/..' . '/../lib/Command/EnableMasterKey.php',
2727
'OCA\\Encryption\\Command\\RecoverUser' => __DIR__ . '/..' . '/../lib/Command/RecoverUser.php',
28+
'OCA\\Encryption\\Command\\ScanLegacyFormat' => __DIR__ . '/..' . '/../lib/Command/ScanLegacyFormat.php',
2829
'OCA\\Encryption\\Controller\\RecoveryController' => __DIR__ . '/..' . '/../lib/Controller/RecoveryController.php',
2930
'OCA\\Encryption\\Controller\\SettingsController' => __DIR__ . '/..' . '/../lib/Controller/SettingsController.php',
3031
'OCA\\Encryption\\Controller\\StatusController' => __DIR__ . '/..' . '/../lib/Controller/StatusController.php',
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
6+
*
7+
* @author Roeland Jago Douma <roeland@famdouma.nl>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
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
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OCA\Encryption\Command;
27+
28+
use OC\Files\View;
29+
use OCA\Encryption\Util;
30+
use OCP\IConfig;
31+
use OCP\IUserManager;
32+
use Symfony\Component\Console\Command\Command;
33+
use Symfony\Component\Console\Helper\QuestionHelper;
34+
use Symfony\Component\Console\Input\InputInterface;
35+
use Symfony\Component\Console\Output\OutputInterface;
36+
37+
class ScanLegacyFormat extends Command {
38+
39+
/** @var Util */
40+
protected $util;
41+
42+
/** @var IConfig */
43+
protected $config;
44+
45+
/** @var QuestionHelper */
46+
protected $questionHelper;
47+
48+
/** @var IUserManager */
49+
private $userManager;
50+
51+
/** @var View */
52+
private $rootView;
53+
54+
/**
55+
* @param Util $util
56+
* @param IConfig $config
57+
* @param QuestionHelper $questionHelper
58+
*/
59+
public function __construct(Util $util,
60+
IConfig $config,
61+
QuestionHelper $questionHelper,
62+
IUserManager $userManager) {
63+
parent::__construct();
64+
65+
$this->util = $util;
66+
$this->config = $config;
67+
$this->questionHelper = $questionHelper;
68+
$this->userManager = $userManager;
69+
$this->rootView = new View();
70+
}
71+
72+
protected function configure() {
73+
$this
74+
->setName('encryption:scan:legacy-format')
75+
->setDescription('Scan the files for the legacy format');
76+
}
77+
78+
protected function execute(InputInterface $input, OutputInterface $output): int {
79+
$result = true;
80+
81+
$output->writeln('Scanning all files for legacy encryption');
82+
83+
foreach ($this->userManager->getBackends() as $backend) {
84+
$limit = 500;
85+
$offset = 0;
86+
do {
87+
$users = $backend->getUsers('', $limit, $offset);
88+
foreach ($users as $user) {
89+
$output->writeln('Scanning all files for ' . $user);
90+
$this->setupUserFS($user);
91+
$result &= $this->scanFolder($output, '/' . $user);
92+
}
93+
$offset += $limit;
94+
} while (count($users) >= $limit);
95+
}
96+
97+
if ($result) {
98+
$output->writeln('All scanned files are propperly encrypted. You can disable the legacy compatibility mode.');
99+
return 0;
100+
}
101+
102+
return 1;
103+
}
104+
105+
private function scanFolder(OutputInterface $output, string $folder): bool {
106+
$clean = true;
107+
108+
foreach ($this->rootView->getDirectoryContent($folder) as $item) {
109+
$path = $folder . '/' . $item['name'];
110+
if ($this->rootView->is_dir($path)) {
111+
if ($this->scanFolder($output, $path) === false) {
112+
$clean = false;
113+
}
114+
} else {
115+
if (!$item->isEncrypted()) {
116+
// ignore
117+
continue;
118+
}
119+
120+
$stats = $this->rootView->stat($path);
121+
if (!isset($stats['hasHeader']) || $stats['hasHeader'] === false) {
122+
$clean = false;
123+
$output->writeln($path . ' does not have a proper header');
124+
}
125+
}
126+
}
127+
128+
return $clean;
129+
}
130+
131+
/**
132+
* setup user file system
133+
*
134+
* @param string $uid
135+
*/
136+
protected function setupUserFS($uid) {
137+
\OC_Util::tearDownFS();
138+
\OC_Util::setupFS($uid);
139+
}
140+
}

lib/private/Files/Storage/Wrapper/Encryption.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,7 @@ public function stat($path) {
818818
$fileSize = $this->filesize($path);
819819
$stat['size'] = $fileSize;
820820
$stat[7] = $fileSize;
821+
$stat['hasHeader'] = $this->getHeaderSize($path) > 0;
821822
return $stat;
822823
}
823824

0 commit comments

Comments
 (0)