ScanLegacyFormat.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Encryption\Command;
  25. use OC\Files\View;
  26. use OCA\Encryption\Util;
  27. use OCP\IConfig;
  28. use OCP\IUserManager;
  29. use Symfony\Component\Console\Command\Command;
  30. use Symfony\Component\Console\Helper\QuestionHelper;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. class ScanLegacyFormat extends Command {
  34. /** @var Util */
  35. protected $util;
  36. /** @var IConfig */
  37. protected $config;
  38. /** @var QuestionHelper */
  39. protected $questionHelper;
  40. /** @var IUserManager */
  41. private $userManager;
  42. /** @var View */
  43. private $rootView;
  44. /**
  45. * @param Util $util
  46. * @param IConfig $config
  47. * @param QuestionHelper $questionHelper
  48. */
  49. public function __construct(Util $util,
  50. IConfig $config,
  51. QuestionHelper $questionHelper,
  52. IUserManager $userManager) {
  53. parent::__construct();
  54. $this->util = $util;
  55. $this->config = $config;
  56. $this->questionHelper = $questionHelper;
  57. $this->userManager = $userManager;
  58. $this->rootView = new View();
  59. }
  60. protected function configure() {
  61. $this
  62. ->setName('encryption:scan:legacy-format')
  63. ->setDescription('Scan the files for the legacy format');
  64. }
  65. protected function execute(InputInterface $input, OutputInterface $output): int {
  66. $result = true;
  67. $output->writeln('Scanning all files for legacy encryption');
  68. foreach ($this->userManager->getBackends() as $backend) {
  69. $limit = 500;
  70. $offset = 0;
  71. do {
  72. $users = $backend->getUsers('', $limit, $offset);
  73. foreach ($users as $user) {
  74. $output->writeln('Scanning all files for ' . $user);
  75. $this->setupUserFS($user);
  76. $result &= $this->scanFolder($output, '/' . $user);
  77. }
  78. $offset += $limit;
  79. } while (count($users) >= $limit);
  80. }
  81. if ($result) {
  82. $output->writeln('All scanned files are properly encrypted. You can disable the legacy compatibility mode.');
  83. return 0;
  84. }
  85. return 1;
  86. }
  87. private function scanFolder(OutputInterface $output, string $folder): bool {
  88. $clean = true;
  89. foreach ($this->rootView->getDirectoryContent($folder) as $item) {
  90. $path = $folder . '/' . $item['name'];
  91. if ($this->rootView->is_dir($path)) {
  92. if ($this->scanFolder($output, $path) === false) {
  93. $clean = false;
  94. }
  95. } else {
  96. if (!$item->isEncrypted()) {
  97. // ignore
  98. continue;
  99. }
  100. $stats = $this->rootView->stat($path);
  101. if (!isset($stats['hasHeader']) || $stats['hasHeader'] === false) {
  102. $clean = false;
  103. $output->writeln($path . ' does not have a proper header');
  104. }
  105. }
  106. }
  107. return $clean;
  108. }
  109. /**
  110. * setup user file system
  111. *
  112. * @param string $uid
  113. */
  114. protected function setupUserFS($uid) {
  115. \OC_Util::tearDownFS();
  116. \OC_Util::setupFS($uid);
  117. }
  118. }