RepairShareOwnership.php 7.1 KB

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