ScanLegacyFormat.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author essys <essys@users.noreply.github.com>
  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. namespace OCA\Encryption\Command;
  26. use OC\Files\View;
  27. use OCA\Encryption\Util;
  28. use OCP\IConfig;
  29. use OCP\IUserManager;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Helper\QuestionHelper;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class ScanLegacyFormat extends Command {
  35. /** @var Util */
  36. protected $util;
  37. /** @var IConfig */
  38. protected $config;
  39. /** @var QuestionHelper */
  40. protected $questionHelper;
  41. /** @var IUserManager */
  42. private $userManager;
  43. /** @var View */
  44. private $rootView;
  45. /**
  46. * @param Util $util
  47. * @param IConfig $config
  48. * @param QuestionHelper $questionHelper
  49. */
  50. public function __construct(Util $util,
  51. IConfig $config,
  52. QuestionHelper $questionHelper,
  53. IUserManager $userManager) {
  54. parent::__construct();
  55. $this->util = $util;
  56. $this->config = $config;
  57. $this->questionHelper = $questionHelper;
  58. $this->userManager = $userManager;
  59. $this->rootView = new View();
  60. }
  61. protected function configure() {
  62. $this
  63. ->setName('encryption:scan:legacy-format')
  64. ->setDescription('Scan the files for the legacy format');
  65. }
  66. protected function execute(InputInterface $input, OutputInterface $output): int {
  67. $result = true;
  68. $output->writeln('Scanning all files for legacy encryption');
  69. foreach ($this->userManager->getBackends() as $backend) {
  70. $limit = 500;
  71. $offset = 0;
  72. do {
  73. $users = $backend->getUsers('', $limit, $offset);
  74. foreach ($users as $user) {
  75. $output->writeln('Scanning all files for ' . $user);
  76. $this->setupUserFS($user);
  77. $result &= $this->scanFolder($output, '/' . $user);
  78. }
  79. $offset += $limit;
  80. } while (count($users) >= $limit);
  81. }
  82. if ($result) {
  83. $output->writeln('All scanned files are properly encrypted. You can disable the legacy compatibility mode.');
  84. return 0;
  85. }
  86. return 1;
  87. }
  88. private function scanFolder(OutputInterface $output, string $folder): bool {
  89. $clean = true;
  90. foreach ($this->rootView->getDirectoryContent($folder) as $item) {
  91. $path = $folder . '/' . $item['name'];
  92. if ($this->rootView->is_dir($path)) {
  93. if ($this->scanFolder($output, $path) === false) {
  94. $clean = false;
  95. }
  96. } else {
  97. if (!$item->isEncrypted()) {
  98. // ignore
  99. continue;
  100. }
  101. $stats = $this->rootView->stat($path);
  102. if (!isset($stats['hasHeader']) || $stats['hasHeader'] === false) {
  103. $clean = false;
  104. $output->writeln($path . ' does not have a proper header');
  105. }
  106. }
  107. }
  108. return $clean;
  109. }
  110. /**
  111. * setup user file system
  112. *
  113. * @param string $uid
  114. */
  115. protected function setupUserFS($uid) {
  116. \OC_Util::tearDownFS();
  117. \OC_Util::setupFS($uid);
  118. }
  119. }