Skip to content

Commit a1cc65d

Browse files
committed
Add legacy scanning command
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
1 parent 7302c7f commit a1cc65d

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-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: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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 OCP\Lock\LockedException;
33+
use Symfony\Component\Console\Command\Command;
34+
use Symfony\Component\Console\Helper\QuestionHelper;
35+
use Symfony\Component\Console\Input\InputInterface;
36+
use Symfony\Component\Console\Output\OutputInterface;
37+
use Symfony\Component\Console\Question\ConfirmationQuestion;
38+
39+
class ScanLegacyFormat extends Command {
40+
41+
/** @var Util */
42+
protected $util;
43+
44+
/** @var IConfig */
45+
protected $config;
46+
47+
/** @var QuestionHelper */
48+
protected $questionHelper;
49+
50+
/** @var IUserManager */
51+
private $userManager;
52+
53+
/** @var View */
54+
private $rootView;
55+
56+
/**
57+
* @param Util $util
58+
* @param IConfig $config
59+
* @param QuestionHelper $questionHelper
60+
*/
61+
public function __construct(Util $util,
62+
IConfig $config,
63+
QuestionHelper $questionHelper,
64+
IUserManager $userManager) {
65+
parent::__construct();
66+
67+
$this->util = $util;
68+
$this->config = $config;
69+
$this->questionHelper = $questionHelper;
70+
$this->userManager = $userManager;
71+
$this->rootView = new View();
72+
}
73+
74+
protected function configure() {
75+
$this
76+
->setName('encryption:scan:legacy-format')
77+
->setDescription('Scan the files for the legacy format');
78+
}
79+
80+
protected function execute(InputInterface $input, OutputInterface $output): int {
81+
$result = true;
82+
83+
$output->writeln('Scanning all files for legacy encryption');
84+
85+
foreach ($this->userManager->getBackends() as $backend) {
86+
$limit = 500;
87+
$offset = 0;
88+
do {
89+
$users = $backend->getUsers('', $limit, $offset);
90+
foreach ($users as $user) {
91+
$output->writeln('Scanning all files for ' . $user);
92+
$this->setupUserFS($user);
93+
$result &= $this->scanFolder($output, '/' . $user);
94+
}
95+
$offset += $limit;
96+
} while (count($users) >= $limit);
97+
}
98+
99+
if ($result) {
100+
$output->writeln('All scanned files are propperly encrypted. You can disable the legacy compatibility mode.');
101+
return 0;
102+
}
103+
104+
return 1;
105+
}
106+
107+
private function scanFolder(OutputInterface $output, string $folder): bool {
108+
$clean = true;
109+
110+
foreach ($this->rootView->getDirectoryContent($folder) as $item) {
111+
$path = $folder . '/' . $item['name'];
112+
if ($this->rootView->is_dir($path)) {
113+
if ($this->scanFolder($output, $path) === false) {
114+
$clean = false;
115+
}
116+
} else {
117+
if (!$item->isEncrypted()) {
118+
// ignore
119+
continue;
120+
}
121+
122+
$stats = $this->rootView->stat($path);
123+
if (!isset($stats['hasHeader']) || $stats['hasHeader'] === false) {
124+
$clean = false;
125+
$output->writeln($path . ' does not have a proper header');
126+
}
127+
}
128+
}
129+
130+
return $clean;
131+
}
132+
133+
/**
134+
* setup user file system
135+
*
136+
* @param string $uid
137+
*/
138+
protected function setupUserFS($uid) {
139+
\OC_Util::tearDownFS();
140+
\OC_Util::setupFS($uid);
141+
}
142+
}

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)