|
| 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 | +} |
0 commit comments