You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OwnershipTransferService.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Sascha Wiswedel <sascha.wiswedel@nextcloud.com>
  12. * @author Tobia De Koninck <LEDfan@users.noreply.github.com>
  13. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  14. *
  15. * @license GNU AGPL version 3 or any later version
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License as
  19. * published by the Free Software Foundation, either version 3 of the
  20. * License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. */
  31. namespace OCA\Files\Service;
  32. use Closure;
  33. use OC\Encryption\Manager as EncryptionManager;
  34. use OC\Files\Filesystem;
  35. use OC\Files\View;
  36. use OCA\Files\Exception\TransferOwnershipException;
  37. use OCP\Encryption\IManager as IEncryptionManager;
  38. use OCP\Files\Config\IUserMountCache;
  39. use OCP\Files\FileInfo;
  40. use OCP\Files\IHomeStorage;
  41. use OCP\Files\InvalidPathException;
  42. use OCP\Files\IRootFolder;
  43. use OCP\Files\Mount\IMountManager;
  44. use OCP\IUser;
  45. use OCP\IUserManager;
  46. use OCP\Share\IManager as IShareManager;
  47. use OCP\Share\IShare;
  48. use Symfony\Component\Console\Helper\ProgressBar;
  49. use Symfony\Component\Console\Output\NullOutput;
  50. use Symfony\Component\Console\Output\OutputInterface;
  51. use function array_merge;
  52. use function basename;
  53. use function count;
  54. use function date;
  55. use function is_dir;
  56. use function rtrim;
  57. class OwnershipTransferService {
  58. private IEncryptionManager|EncryptionManager $encryptionManager;
  59. public function __construct(
  60. IEncryptionManager $encryptionManager,
  61. private IShareManager $shareManager,
  62. private IMountManager $mountManager,
  63. private IUserMountCache $userMountCache,
  64. private IUserManager $userManager,
  65. ) {
  66. $this->encryptionManager = $encryptionManager;
  67. }
  68. /**
  69. * @param IUser $sourceUser
  70. * @param IUser $destinationUser
  71. * @param string $path
  72. *
  73. * @param OutputInterface|null $output
  74. * @param bool $move
  75. * @throws TransferOwnershipException
  76. * @throws \OC\User\NoUserException
  77. */
  78. public function transfer(
  79. IUser $sourceUser,
  80. IUser $destinationUser,
  81. string $path,
  82. ?OutputInterface $output = null,
  83. bool $move = false,
  84. bool $firstLogin = false,
  85. bool $transferIncomingShares = false,
  86. ): void {
  87. $output = $output ?? new NullOutput();
  88. $sourceUid = $sourceUser->getUID();
  89. $destinationUid = $destinationUser->getUID();
  90. $sourcePath = rtrim($sourceUid . '/files/' . $path, '/');
  91. // If encryption is on we have to ensure the user has logged in before and that all encryption modules are ready
  92. if (($this->encryptionManager->isEnabled() && $destinationUser->getLastLogin() === 0)
  93. || !$this->encryptionManager->isReadyForUser($destinationUid)) {
  94. throw new TransferOwnershipException("The target user is not ready to accept files. The user has at least to have logged in once.", 2);
  95. }
  96. // setup filesystem
  97. // Requesting the user folder will set it up if the user hasn't logged in before
  98. // We need a setupFS for the full filesystem setup before as otherwise we will just return
  99. // a lazy root folder which does not create the destination users folder
  100. \OC_Util::setupFS($destinationUser->getUID());
  101. \OC::$server->getUserFolder($destinationUser->getUID());
  102. Filesystem::initMountPoints($sourceUid);
  103. Filesystem::initMountPoints($destinationUid);
  104. $view = new View();
  105. if ($move) {
  106. $finalTarget = "$destinationUid/files/";
  107. } else {
  108. $date = date('Y-m-d H-i-s');
  109. // Remove some characters which are prone to cause errors
  110. $cleanUserName = str_replace(['\\', '/', ':', '.', '?', '#', '\'', '"'], '-', $sourceUser->getDisplayName());
  111. // Replace multiple dashes with one dash
  112. $cleanUserName = preg_replace('/-{2,}/s', '-', $cleanUserName);
  113. $cleanUserName = $cleanUserName ?: $sourceUid;
  114. $finalTarget = "$destinationUid/files/transferred from $cleanUserName on $date";
  115. try {
  116. $view->verifyPath(dirname($finalTarget), basename($finalTarget));
  117. } catch (InvalidPathException $e) {
  118. $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date";
  119. }
  120. }
  121. if (!($view->is_dir($sourcePath) || $view->is_file($sourcePath))) {
  122. throw new TransferOwnershipException("Unknown path provided: $path", 1);
  123. }
  124. if ($move && !$view->is_dir($finalTarget)) {
  125. // Initialize storage
  126. \OC_Util::setupFS($destinationUser->getUID());
  127. }
  128. if ($move && !$firstLogin && count($view->getDirectoryContent($finalTarget)) > 0) {
  129. throw new TransferOwnershipException("Destination path does not exists or is not empty", 1);
  130. }
  131. // analyse source folder
  132. $this->analyse(
  133. $sourceUid,
  134. $destinationUid,
  135. $sourcePath,
  136. $view,
  137. $output
  138. );
  139. // collect all the shares
  140. $shares = $this->collectUsersShares(
  141. $sourceUid,
  142. $output,
  143. $view,
  144. $sourcePath
  145. );
  146. // transfer the files
  147. $this->transferFiles(
  148. $sourceUid,
  149. $sourcePath,
  150. $finalTarget,
  151. $view,
  152. $output
  153. );
  154. $destinationPath = $finalTarget . '/' . $path;
  155. // restore the shares
  156. $this->restoreShares(
  157. $sourceUid,
  158. $destinationUid,
  159. $destinationPath,
  160. $shares,
  161. $output
  162. );
  163. // transfer the incoming shares
  164. if ($transferIncomingShares === true) {
  165. $sourceShares = $this->collectIncomingShares(
  166. $sourceUid,
  167. $output,
  168. $view
  169. );
  170. $destinationShares = $this->collectIncomingShares(
  171. $destinationUid,
  172. $output,
  173. $view,
  174. true
  175. );
  176. $this->transferIncomingShares(
  177. $sourceUid,
  178. $destinationUid,
  179. $sourceShares,
  180. $destinationShares,
  181. $output,
  182. $path,
  183. $finalTarget,
  184. $move
  185. );
  186. }
  187. }
  188. private function walkFiles(View $view, $path, Closure $callBack) {
  189. foreach ($view->getDirectoryContent($path) as $fileInfo) {
  190. if (!$callBack($fileInfo)) {
  191. return;
  192. }
  193. if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) {
  194. $this->walkFiles($view, $fileInfo->getPath(), $callBack);
  195. }
  196. }
  197. }
  198. /**
  199. * @param OutputInterface $output
  200. *
  201. * @throws \Exception
  202. */
  203. protected function analyse(string $sourceUid,
  204. string $destinationUid,
  205. string $sourcePath,
  206. View $view,
  207. OutputInterface $output): void {
  208. $output->writeln('Validating quota');
  209. $size = $view->getFileInfo($sourcePath, false)->getSize(false);
  210. $freeSpace = $view->free_space($destinationUid . '/files/');
  211. if ($size > $freeSpace && $freeSpace !== FileInfo::SPACE_UNKNOWN) {
  212. $output->writeln('<error>Target user does not have enough free space available.</error>');
  213. throw new \Exception('Execution terminated.');
  214. }
  215. $output->writeln("Analysing files of $sourceUid ...");
  216. $progress = new ProgressBar($output);
  217. $progress->start();
  218. $encryptedFiles = [];
  219. $this->walkFiles($view, $sourcePath,
  220. function (FileInfo $fileInfo) use ($progress) {
  221. if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) {
  222. // only analyze into folders from main storage,
  223. if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) {
  224. return false;
  225. }
  226. return true;
  227. }
  228. $progress->advance();
  229. if ($fileInfo->isEncrypted()) {
  230. $encryptedFiles[] = $fileInfo;
  231. }
  232. return true;
  233. });
  234. $progress->finish();
  235. $output->writeln('');
  236. // no file is allowed to be encrypted
  237. if (!empty($encryptedFiles)) {
  238. $output->writeln("<error>Some files are encrypted - please decrypt them first.</error>");
  239. foreach ($encryptedFiles as $encryptedFile) {
  240. /** @var FileInfo $encryptedFile */
  241. $output->writeln(" " . $encryptedFile->getPath());
  242. }
  243. throw new \Exception('Execution terminated.');
  244. }
  245. }
  246. /**
  247. * @return array<array{share: IShare, suffix: string}>
  248. */
  249. private function collectUsersShares(
  250. string $sourceUid,
  251. OutputInterface $output,
  252. View $view,
  253. string $path,
  254. ): array {
  255. $output->writeln("Collecting all share information for files and folders of $sourceUid ...");
  256. $shares = [];
  257. $progress = new ProgressBar($output);
  258. $normalizedPath = Filesystem::normalizePath($path);
  259. $supportedShareTypes = [
  260. IShare::TYPE_GROUP,
  261. IShare::TYPE_USER,
  262. IShare::TYPE_LINK,
  263. IShare::TYPE_REMOTE,
  264. IShare::TYPE_ROOM,
  265. IShare::TYPE_EMAIL,
  266. IShare::TYPE_CIRCLE,
  267. IShare::TYPE_DECK,
  268. IShare::TYPE_SCIENCEMESH,
  269. ];
  270. foreach ($supportedShareTypes as $shareType) {
  271. $offset = 0;
  272. while (true) {
  273. $sharePage = $this->shareManager->getSharesBy($sourceUid, $shareType, null, true, 50, $offset);
  274. $progress->advance(count($sharePage));
  275. if (empty($sharePage)) {
  276. break;
  277. }
  278. if ($path !== "$sourceUid/files") {
  279. $sharePage = array_filter($sharePage, function (IShare $share) use ($view, $normalizedPath) {
  280. try {
  281. $relativePath = $view->getPath($share->getNodeId());
  282. $singleFileTranfer = $view->is_file($normalizedPath);
  283. if ($singleFileTranfer) {
  284. return Filesystem::normalizePath($relativePath) === $normalizedPath;
  285. }
  286. return mb_strpos(
  287. Filesystem::normalizePath($relativePath . '/', false),
  288. $normalizedPath . '/') === 0;
  289. } catch (\Exception $e) {
  290. return false;
  291. }
  292. });
  293. }
  294. $shares = array_merge($shares, $sharePage);
  295. $offset += 50;
  296. }
  297. }
  298. $progress->finish();
  299. $output->writeln('');
  300. return array_map(fn (IShare $share) => [
  301. 'share' => $share,
  302. 'suffix' => substr(Filesystem::normalizePath($view->getPath($share->getNodeId())), strlen($normalizedPath)),
  303. ], $shares);
  304. }
  305. private function collectIncomingShares(string $sourceUid,
  306. OutputInterface $output,
  307. View $view,
  308. bool $addKeys = false): array {
  309. $output->writeln("Collecting all incoming share information for files and folders of $sourceUid ...");
  310. $shares = [];
  311. $progress = new ProgressBar($output);
  312. $offset = 0;
  313. while (true) {
  314. $sharePage = $this->shareManager->getSharedWith($sourceUid, IShare::TYPE_USER, null, 50, $offset);
  315. $progress->advance(count($sharePage));
  316. if (empty($sharePage)) {
  317. break;
  318. }
  319. if ($addKeys) {
  320. foreach ($sharePage as $singleShare) {
  321. $shares[$singleShare->getNodeId()] = $singleShare;
  322. }
  323. } else {
  324. foreach ($sharePage as $singleShare) {
  325. $shares[] = $singleShare;
  326. }
  327. }
  328. $offset += 50;
  329. }
  330. $progress->finish();
  331. $output->writeln('');
  332. return $shares;
  333. }
  334. /**
  335. * @throws TransferOwnershipException
  336. */
  337. protected function transferFiles(string $sourceUid,
  338. string $sourcePath,
  339. string $finalTarget,
  340. View $view,
  341. OutputInterface $output): void {
  342. $output->writeln("Transferring files to $finalTarget ...");
  343. // This change will help user to transfer the folder specified using --path option.
  344. // Else only the content inside folder is transferred which is not correct.
  345. if ($sourcePath !== "$sourceUid/files") {
  346. $view->mkdir($finalTarget);
  347. $finalTarget = $finalTarget . '/' . basename($sourcePath);
  348. }
  349. if ($view->rename($sourcePath, $finalTarget) === false) {
  350. throw new TransferOwnershipException("Could not transfer files.", 1);
  351. }
  352. if (!is_dir("$sourceUid/files")) {
  353. // because the files folder is moved away we need to recreate it
  354. $view->mkdir("$sourceUid/files");
  355. }
  356. }
  357. /**
  358. * @param string $targetLocation New location of the transfered node
  359. * @param array<array{share: IShare, suffix: string}> $shares previously collected share information
  360. */
  361. private function restoreShares(
  362. string $sourceUid,
  363. string $destinationUid,
  364. string $targetLocation,
  365. array $shares,
  366. OutputInterface $output,
  367. ):void {
  368. $output->writeln("Restoring shares ...");
  369. $progress = new ProgressBar($output, count($shares));
  370. $rootFolder = \OCP\Server::get(IRootFolder::class);
  371. foreach ($shares as ['share' => $share, 'suffix' => $suffix]) {
  372. try {
  373. if ($share->getShareType() === IShare::TYPE_USER &&
  374. $share->getSharedWith() === $destinationUid) {
  375. // Unmount the shares before deleting, so we don't try to get the storage later on.
  376. $shareMountPoint = $this->mountManager->find('/' . $destinationUid . '/files' . $share->getTarget());
  377. if ($shareMountPoint) {
  378. $this->mountManager->removeMount($shareMountPoint->getMountPoint());
  379. }
  380. $this->shareManager->deleteShare($share);
  381. } else {
  382. if ($share->getShareOwner() === $sourceUid) {
  383. $share->setShareOwner($destinationUid);
  384. }
  385. if ($share->getSharedBy() === $sourceUid) {
  386. $share->setSharedBy($destinationUid);
  387. }
  388. if ($share->getShareType() === IShare::TYPE_USER &&
  389. !$this->userManager->userExists($share->getSharedWith())) {
  390. // stray share with deleted user
  391. $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted user "' . $share->getSharedWith() . '", deleting</error>');
  392. $this->shareManager->deleteShare($share);
  393. continue;
  394. } else {
  395. // trigger refetching of the node so that the new owner and mountpoint are taken into account
  396. // otherwise the checks on the share update will fail due to the original node not being available in the new user scope
  397. $this->userMountCache->clear();
  398. try {
  399. // Try to get the "old" id.
  400. // Normally the ID is preserved,
  401. // but for transferes between different storages the ID might change
  402. $newNodeId = $share->getNode()->getId();
  403. } catch (\OCP\Files\NotFoundException) {
  404. // ID has changed due to transfer between different storages
  405. // Try to get the new ID from the target path and suffix of the share
  406. $node = $rootFolder->get(Filesystem::normalizePath($targetLocation . '/' . $suffix));
  407. $newNodeId = $node->getId();
  408. }
  409. $share->setNodeId($newNodeId);
  410. $this->shareManager->updateShare($share);
  411. }
  412. }
  413. } catch (\OCP\Files\NotFoundException $e) {
  414. $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>');
  415. } catch (\Throwable $e) {
  416. $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getMessage() . ' : ' . $e->getTraceAsString() . '</error>');
  417. }
  418. $progress->advance();
  419. }
  420. $progress->finish();
  421. $output->writeln('');
  422. }
  423. private function transferIncomingShares(string $sourceUid,
  424. string $destinationUid,
  425. array $sourceShares,
  426. array $destinationShares,
  427. OutputInterface $output,
  428. string $path,
  429. string $finalTarget,
  430. bool $move): void {
  431. $output->writeln("Restoring incoming shares ...");
  432. $progress = new ProgressBar($output, count($sourceShares));
  433. $prefix = "$destinationUid/files";
  434. $finalShareTarget = '';
  435. if (str_starts_with($finalTarget, $prefix)) {
  436. $finalShareTarget = substr($finalTarget, strlen($prefix));
  437. }
  438. foreach ($sourceShares as $share) {
  439. try {
  440. // Only restore if share is in given path.
  441. $pathToCheck = '/';
  442. if (trim($path, '/') !== '') {
  443. $pathToCheck = '/' . trim($path) . '/';
  444. }
  445. if (!str_starts_with($share->getTarget(), $pathToCheck)) {
  446. continue;
  447. }
  448. $shareTarget = $share->getTarget();
  449. $shareTarget = $finalShareTarget . $shareTarget;
  450. if ($share->getShareType() === IShare::TYPE_USER &&
  451. $share->getSharedBy() === $destinationUid) {
  452. $this->shareManager->deleteShare($share);
  453. } elseif (isset($destinationShares[$share->getNodeId()])) {
  454. $destinationShare = $destinationShares[$share->getNodeId()];
  455. // Keep the share which has the most permissions and discard the other one.
  456. if ($destinationShare->getPermissions() < $share->getPermissions()) {
  457. $this->shareManager->deleteShare($destinationShare);
  458. $share->setSharedWith($destinationUid);
  459. // trigger refetching of the node so that the new owner and mountpoint are taken into account
  460. // otherwise the checks on the share update will fail due to the original node not being available in the new user scope
  461. $this->userMountCache->clear();
  462. $share->setNodeId($share->getNode()->getId());
  463. $this->shareManager->updateShare($share);
  464. // The share is already transferred.
  465. $progress->advance();
  466. if ($move) {
  467. continue;
  468. }
  469. $share->setTarget($shareTarget);
  470. $this->shareManager->moveShare($share, $destinationUid);
  471. continue;
  472. }
  473. $this->shareManager->deleteShare($share);
  474. } elseif ($share->getShareOwner() === $destinationUid) {
  475. $this->shareManager->deleteShare($share);
  476. } else {
  477. $share->setSharedWith($destinationUid);
  478. $share->setNodeId($share->getNode()->getId());
  479. $this->shareManager->updateShare($share);
  480. // trigger refetching of the node so that the new owner and mountpoint are taken into account
  481. // otherwise the checks on the share update will fail due to the original node not being available in the new user scope
  482. $this->userMountCache->clear();
  483. // The share is already transferred.
  484. $progress->advance();
  485. if ($move) {
  486. continue;
  487. }
  488. $share->setTarget($shareTarget);
  489. $this->shareManager->moveShare($share, $destinationUid);
  490. continue;
  491. }
  492. } catch (\OCP\Files\NotFoundException $e) {
  493. $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>');
  494. } catch (\Throwable $e) {
  495. $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>');
  496. }
  497. $progress->advance();
  498. }
  499. $progress->finish();
  500. $output->writeln('');
  501. }
  502. }