RepairShareOwnership.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Command\Maintenance;
  8. use OCP\DB\QueryBuilder\IQueryBuilder;
  9. use OCP\IDBConnection;
  10. use OCP\IUser;
  11. use OCP\IUserManager;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Helper\QuestionHelper;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Question\ConfirmationQuestion;
  19. class RepairShareOwnership extends Command {
  20. public function __construct(
  21. private IDBConnection $dbConnection,
  22. private IUserManager $userManager,
  23. ) {
  24. parent::__construct();
  25. }
  26. protected function configure() {
  27. $this
  28. ->setName('maintenance:repair-share-owner')
  29. ->setDescription('repair invalid share-owner entries in the database')
  30. ->addOption('no-confirm', 'y', InputOption::VALUE_NONE, "Don't ask for confirmation before repairing the shares")
  31. ->addArgument('user', InputArgument::OPTIONAL, 'User to fix incoming shares for, if omitted all users will be fixed');
  32. }
  33. protected function execute(InputInterface $input, OutputInterface $output): int {
  34. $noConfirm = $input->getOption('no-confirm');
  35. $userId = $input->getArgument('user');
  36. if ($userId) {
  37. $user = $this->userManager->get($userId);
  38. if (!$user) {
  39. $output->writeln("<error>user $userId not found</error>");
  40. return 1;
  41. }
  42. $shares = $this->getWrongShareOwnershipForUser($user);
  43. } else {
  44. $shares = $this->getWrongShareOwnership();
  45. }
  46. if ($shares) {
  47. $output->writeln('');
  48. $output->writeln('Found ' . count($shares) . ' shares with invalid share owner');
  49. foreach ($shares as $share) {
  50. /** @var array{shareId: int, fileTarget: string, initiator: string, receiver: string, owner: string, mountOwner: string} $share */
  51. $output->writeln(" - share {$share['shareId']} from \"{$share['initiator']}\" to \"{$share['receiver']}\" at \"{$share['fileTarget']}\", owned by \"{$share['owner']}\", that should be owned by \"{$share['mountOwner']}\"");
  52. }
  53. $output->writeln('');
  54. if (!$noConfirm) {
  55. /** @var QuestionHelper $helper */
  56. $helper = $this->getHelper('question');
  57. $question = new ConfirmationQuestion('Repair these shares? [y/N]', false);
  58. if (!$helper->ask($input, $output, $question)) {
  59. return 0;
  60. }
  61. }
  62. $output->writeln('Repairing ' . count($shares) . ' shares');
  63. $this->repairShares($shares);
  64. } else {
  65. $output->writeln('Found no shares with invalid share owner');
  66. }
  67. return 0;
  68. }
  69. /**
  70. * @return array{shareId: int, fileTarget: string, initiator: string, receiver: string, owner: string, mountOwner: string}[]
  71. * @throws \OCP\DB\Exception
  72. */
  73. protected function getWrongShareOwnership(): array {
  74. $qb = $this->dbConnection->getQueryBuilder();
  75. $brokenShares = $qb
  76. ->select('s.id', 'm.user_id', 's.uid_owner', 's.uid_initiator', 's.share_with', 's.file_target')
  77. ->from('share', 's')
  78. ->join('s', 'filecache', 'f', $qb->expr()->eq($qb->expr()->castColumn('s.item_source', IQueryBuilder::PARAM_INT), 'f.fileid'))
  79. ->join('s', 'mounts', 'm', $qb->expr()->eq('f.storage', 'm.storage_id'))
  80. ->where($qb->expr()->neq('m.user_id', 's.uid_owner'))
  81. ->andWhere($qb->expr()->eq($qb->func()->concat($qb->expr()->literal('/'), 'm.user_id', $qb->expr()->literal('/')), 'm.mount_point'))
  82. ->executeQuery()
  83. ->fetchAll();
  84. $found = [];
  85. foreach ($brokenShares as $share) {
  86. $found[] = [
  87. 'shareId' => (int)$share['id'],
  88. 'fileTarget' => $share['file_target'],
  89. 'initiator' => $share['uid_initiator'],
  90. 'receiver' => $share['share_with'],
  91. 'owner' => $share['uid_owner'],
  92. 'mountOwner' => $share['user_id'],
  93. ];
  94. }
  95. return $found;
  96. }
  97. /**
  98. * @param IUser $user
  99. * @return array{shareId: int, fileTarget: string, initiator: string, receiver: string, owner: string, mountOwner: string}[]
  100. * @throws \OCP\DB\Exception
  101. */
  102. protected function getWrongShareOwnershipForUser(IUser $user): array {
  103. $qb = $this->dbConnection->getQueryBuilder();
  104. $brokenShares = $qb
  105. ->select('s.id', 'm.user_id', 's.uid_owner', 's.uid_initiator', 's.share_with', 's.file_target')
  106. ->from('share', 's')
  107. ->join('s', 'filecache', 'f', $qb->expr()->eq('s.item_source', $qb->expr()->castColumn('f.fileid', IQueryBuilder::PARAM_STR)))
  108. ->join('s', 'mounts', 'm', $qb->expr()->eq('f.storage', 'm.storage_id'))
  109. ->where($qb->expr()->neq('m.user_id', 's.uid_owner'))
  110. ->andWhere($qb->expr()->eq($qb->func()->concat($qb->expr()->literal('/'), 'm.user_id', $qb->expr()->literal('/')), 'm.mount_point'))
  111. ->andWhere($qb->expr()->eq('s.share_with', $qb->createNamedParameter($user->getUID())))
  112. ->executeQuery()
  113. ->fetchAll();
  114. $found = [];
  115. foreach ($brokenShares as $share) {
  116. $found[] = [
  117. 'shareId' => (int)$share['id'],
  118. 'fileTarget' => $share['file_target'],
  119. 'initiator' => $share['uid_initiator'],
  120. 'receiver' => $share['share_with'],
  121. 'owner' => $share['uid_owner'],
  122. 'mountOwner' => $share['user_id'],
  123. ];
  124. }
  125. return $found;
  126. }
  127. /**
  128. * @param array{shareId: int, fileTarget: string, initiator: string, receiver: string, owner: string, mountOwner: string}[] $shares
  129. * @return void
  130. */
  131. protected function repairShares(array $shares) {
  132. $this->dbConnection->beginTransaction();
  133. $update = $this->dbConnection->getQueryBuilder();
  134. $update->update('share')
  135. ->set('uid_owner', $update->createParameter('share_owner'))
  136. ->set('uid_initiator', $update->createParameter('share_initiator'))
  137. ->where($update->expr()->eq('id', $update->createParameter('share_id')));
  138. foreach ($shares as $share) {
  139. /** @var array{shareId: int, fileTarget: string, initiator: string, receiver: string, owner: string, mountOwner: string} $share */
  140. $update->setParameter('share_id', $share['shareId'], IQueryBuilder::PARAM_INT);
  141. $update->setParameter('share_owner', $share['mountOwner']);
  142. // if the broken owner is also the initiator it's safe to update them both, otherwise we don't touch the initiator
  143. if ($share['initiator'] === $share['owner']) {
  144. $update->setParameter('share_initiator', $share['mountOwner']);
  145. } else {
  146. $update->setParameter('share_initiator', $share['initiator']);
  147. }
  148. $update->executeStatement();
  149. }
  150. $this->dbConnection->commit();
  151. }
  152. }