OwnershipTransferService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 TransferOwnershipException
  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. $sourceFileInfo = $view->getFileInfo($sourcePath, false);
  210. if ($sourceFileInfo === false) {
  211. throw new TransferOwnershipException("Unknown path provided: $sourcePath", 1);
  212. }
  213. $size = $sourceFileInfo->getSize(false);
  214. $freeSpace = $view->free_space($destinationUid . '/files/');
  215. if ($size > $freeSpace && $freeSpace !== FileInfo::SPACE_UNKNOWN) {
  216. throw new TransferOwnershipException('Target user does not have enough free space available.', 1);
  217. }
  218. $output->writeln("Analysing files of $sourceUid ...");
  219. $progress = new ProgressBar($output);
  220. $progress->start();
  221. if ($this->encryptionManager->isEnabled()) {
  222. $masterKeyEnabled = \OCP\Server::get(\OCA\Encryption\Util::class)->isMasterKeyEnabled();
  223. } else {
  224. $masterKeyEnabled = false;
  225. }
  226. $encryptedFiles = [];
  227. if ($sourceFileInfo->getType() === FileInfo::TYPE_FOLDER) {
  228. if ($sourceFileInfo->isEncrypted()) {
  229. /* Encrypted folder means e2ee encrypted */
  230. $encryptedFiles[] = $sourceFileInfo;
  231. } else {
  232. $this->walkFiles($view, $sourcePath,
  233. function (FileInfo $fileInfo) use ($progress, $masterKeyEnabled, &$encryptedFiles) {
  234. if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) {
  235. // only analyze into folders from main storage,
  236. if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) {
  237. return false;
  238. }
  239. if ($fileInfo->isEncrypted()) {
  240. /* Encrypted folder means e2ee encrypted, we cannot transfer it */
  241. $encryptedFiles[] = $fileInfo;
  242. }
  243. return true;
  244. }
  245. $progress->advance();
  246. if ($fileInfo->isEncrypted() && !$masterKeyEnabled) {
  247. /* Encrypted file means SSE, we can only transfer it if master key is enabled */
  248. $encryptedFiles[] = $fileInfo;
  249. }
  250. return true;
  251. });
  252. }
  253. } elseif ($sourceFileInfo->isEncrypted() && !$masterKeyEnabled) {
  254. /* Encrypted file means SSE, we can only transfer it if master key is enabled */
  255. $encryptedFiles[] = $sourceFileInfo;
  256. }
  257. $progress->finish();
  258. $output->writeln('');
  259. // no file is allowed to be encrypted
  260. if (!empty($encryptedFiles)) {
  261. $output->writeln("<error>Some files are encrypted - please decrypt them first.</error>");
  262. foreach ($encryptedFiles as $encryptedFile) {
  263. /** @var FileInfo $encryptedFile */
  264. $output->writeln(" " . $encryptedFile->getPath());
  265. }
  266. throw new TransferOwnershipException('Some files are encrypted - please decrypt them first.', 1);
  267. }
  268. }
  269. /**
  270. * @return array<array{share: IShare, suffix: string}>
  271. */
  272. private function collectUsersShares(
  273. string $sourceUid,
  274. OutputInterface $output,
  275. View $view,
  276. string $path,
  277. ): array {
  278. $output->writeln("Collecting all share information for files and folders of $sourceUid ...");
  279. $shares = [];
  280. $progress = new ProgressBar($output);
  281. $normalizedPath = Filesystem::normalizePath($path);
  282. $supportedShareTypes = [
  283. IShare::TYPE_GROUP,
  284. IShare::TYPE_USER,
  285. IShare::TYPE_LINK,
  286. IShare::TYPE_REMOTE,
  287. IShare::TYPE_ROOM,
  288. IShare::TYPE_EMAIL,
  289. IShare::TYPE_CIRCLE,
  290. IShare::TYPE_DECK,
  291. IShare::TYPE_SCIENCEMESH,
  292. ];
  293. foreach ($supportedShareTypes as $shareType) {
  294. $offset = 0;
  295. while (true) {
  296. $sharePage = $this->shareManager->getSharesBy($sourceUid, $shareType, null, true, 50, $offset);
  297. $progress->advance(count($sharePage));
  298. if (empty($sharePage)) {
  299. break;
  300. }
  301. if ($path !== "$sourceUid/files") {
  302. $sharePage = array_filter($sharePage, function (IShare $share) use ($view, $normalizedPath) {
  303. try {
  304. $relativePath = $view->getPath($share->getNodeId());
  305. $singleFileTranfer = $view->is_file($normalizedPath);
  306. if ($singleFileTranfer) {
  307. return Filesystem::normalizePath($relativePath) === $normalizedPath;
  308. }
  309. return mb_strpos(
  310. Filesystem::normalizePath($relativePath . '/', false),
  311. $normalizedPath . '/') === 0;
  312. } catch (\Exception $e) {
  313. return false;
  314. }
  315. });
  316. }
  317. $shares = array_merge($shares, $sharePage);
  318. $offset += 50;
  319. }
  320. }
  321. $progress->finish();
  322. $output->writeln('');
  323. return array_map(fn (IShare $share) => [
  324. 'share' => $share,
  325. 'suffix' => substr(Filesystem::normalizePath($view->getPath($share->getNodeId())), strlen($normalizedPath)),
  326. ], $shares);
  327. }
  328. private function collectIncomingShares(string $sourceUid,
  329. OutputInterface $output,
  330. View $view,
  331. bool $addKeys = false): array {
  332. $output->writeln("Collecting all incoming share information for files and folders of $sourceUid ...");
  333. $shares = [];
  334. $progress = new ProgressBar($output);
  335. $offset = 0;
  336. while (true) {
  337. $sharePage = $this->shareManager->getSharedWith($sourceUid, IShare::TYPE_USER, null, 50, $offset);
  338. $progress->advance(count($sharePage));
  339. if (empty($sharePage)) {
  340. break;
  341. }
  342. if ($addKeys) {
  343. foreach ($sharePage as $singleShare) {
  344. $shares[$singleShare->getNodeId()] = $singleShare;
  345. }
  346. } else {
  347. foreach ($sharePage as $singleShare) {
  348. $shares[] = $singleShare;
  349. }
  350. }
  351. $offset += 50;
  352. }
  353. $progress->finish();
  354. $output->writeln('');
  355. return $shares;
  356. }
  357. /**
  358. * @throws TransferOwnershipException
  359. */
  360. protected function transferFiles(string $sourceUid,
  361. string $sourcePath,
  362. string $finalTarget,
  363. View $view,
  364. OutputInterface $output): void {
  365. $output->writeln("Transferring files to $finalTarget ...");
  366. // This change will help user to transfer the folder specified using --path option.
  367. // Else only the content inside folder is transferred which is not correct.
  368. if ($sourcePath !== "$sourceUid/files") {
  369. $view->mkdir($finalTarget);
  370. $finalTarget = $finalTarget . '/' . basename($sourcePath);
  371. }
  372. if ($view->rename($sourcePath, $finalTarget) === false) {
  373. throw new TransferOwnershipException("Could not transfer files.", 1);
  374. }
  375. if (!is_dir("$sourceUid/files")) {
  376. // because the files folder is moved away we need to recreate it
  377. $view->mkdir("$sourceUid/files");
  378. }
  379. }
  380. /**
  381. * @param string $targetLocation New location of the transfered node
  382. * @param array<array{share: IShare, suffix: string}> $shares previously collected share information
  383. */
  384. private function restoreShares(
  385. string $sourceUid,
  386. string $destinationUid,
  387. string $targetLocation,
  388. array $shares,
  389. OutputInterface $output,
  390. ):void {
  391. $output->writeln("Restoring shares ...");
  392. $progress = new ProgressBar($output, count($shares));
  393. $rootFolder = \OCP\Server::get(IRootFolder::class);
  394. foreach ($shares as ['share' => $share, 'suffix' => $suffix]) {
  395. try {
  396. if ($share->getShareType() === IShare::TYPE_USER &&
  397. $share->getSharedWith() === $destinationUid) {
  398. // Unmount the shares before deleting, so we don't try to get the storage later on.
  399. $shareMountPoint = $this->mountManager->find('/' . $destinationUid . '/files' . $share->getTarget());
  400. if ($shareMountPoint) {
  401. $this->mountManager->removeMount($shareMountPoint->getMountPoint());
  402. }
  403. $this->shareManager->deleteShare($share);
  404. } else {
  405. if ($share->getShareOwner() === $sourceUid) {
  406. $share->setShareOwner($destinationUid);
  407. }
  408. if ($share->getSharedBy() === $sourceUid) {
  409. $share->setSharedBy($destinationUid);
  410. }
  411. if ($share->getShareType() === IShare::TYPE_USER &&
  412. !$this->userManager->userExists($share->getSharedWith())) {
  413. // stray share with deleted user
  414. $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted user "' . $share->getSharedWith() . '", deleting</error>');
  415. $this->shareManager->deleteShare($share);
  416. continue;
  417. } else {
  418. // trigger refetching of the node so that the new owner and mountpoint are taken into account
  419. // otherwise the checks on the share update will fail due to the original node not being available in the new user scope
  420. $this->userMountCache->clear();
  421. try {
  422. // Try to get the "old" id.
  423. // Normally the ID is preserved,
  424. // but for transferes between different storages the ID might change
  425. $newNodeId = $share->getNode()->getId();
  426. } catch (\OCP\Files\NotFoundException) {
  427. // ID has changed due to transfer between different storages
  428. // Try to get the new ID from the target path and suffix of the share
  429. $node = $rootFolder->get(Filesystem::normalizePath($targetLocation . '/' . $suffix));
  430. $newNodeId = $node->getId();
  431. }
  432. $share->setNodeId($newNodeId);
  433. $this->shareManager->updateShare($share);
  434. }
  435. }
  436. } catch (\OCP\Files\NotFoundException $e) {
  437. $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>');
  438. } catch (\Throwable $e) {
  439. $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getMessage() . ' : ' . $e->getTraceAsString() . '</error>');
  440. }
  441. $progress->advance();
  442. }
  443. $progress->finish();
  444. $output->writeln('');
  445. }
  446. private function transferIncomingShares(string $sourceUid,
  447. string $destinationUid,
  448. array $sourceShares,
  449. array $destinationShares,
  450. OutputInterface $output,
  451. string $path,
  452. string $finalTarget,
  453. bool $move): void {
  454. $output->writeln("Restoring incoming shares ...");
  455. $progress = new ProgressBar($output, count($sourceShares));
  456. $prefix = "$destinationUid/files";
  457. $finalShareTarget = '';
  458. if (str_starts_with($finalTarget, $prefix)) {
  459. $finalShareTarget = substr($finalTarget, strlen($prefix));
  460. }
  461. foreach ($sourceShares as $share) {
  462. try {
  463. // Only restore if share is in given path.
  464. $pathToCheck = '/';
  465. if (trim($path, '/') !== '') {
  466. $pathToCheck = '/' . trim($path) . '/';
  467. }
  468. if (!str_starts_with($share->getTarget(), $pathToCheck)) {
  469. continue;
  470. }
  471. $shareTarget = $share->getTarget();
  472. $shareTarget = $finalShareTarget . $shareTarget;
  473. if ($share->getShareType() === IShare::TYPE_USER &&
  474. $share->getSharedBy() === $destinationUid) {
  475. $this->shareManager->deleteShare($share);
  476. } elseif (isset($destinationShares[$share->getNodeId()])) {
  477. $destinationShare = $destinationShares[$share->getNodeId()];
  478. // Keep the share which has the most permissions and discard the other one.
  479. if ($destinationShare->getPermissions() < $share->getPermissions()) {
  480. $this->shareManager->deleteShare($destinationShare);
  481. $share->setSharedWith($destinationUid);
  482. // trigger refetching of the node so that the new owner and mountpoint are taken into account
  483. // otherwise the checks on the share update will fail due to the original node not being available in the new user scope
  484. $this->userMountCache->clear();
  485. $share->setNodeId($share->getNode()->getId());
  486. $this->shareManager->updateShare($share);
  487. // The share is already transferred.
  488. $progress->advance();
  489. if ($move) {
  490. continue;
  491. }
  492. $share->setTarget($shareTarget);
  493. $this->shareManager->moveShare($share, $destinationUid);
  494. continue;
  495. }
  496. $this->shareManager->deleteShare($share);
  497. } elseif ($share->getShareOwner() === $destinationUid) {
  498. $this->shareManager->deleteShare($share);
  499. } else {
  500. $share->setSharedWith($destinationUid);
  501. $share->setNodeId($share->getNode()->getId());
  502. $this->shareManager->updateShare($share);
  503. // trigger refetching of the node so that the new owner and mountpoint are taken into account
  504. // otherwise the checks on the share update will fail due to the original node not being available in the new user scope
  505. $this->userMountCache->clear();
  506. // The share is already transferred.
  507. $progress->advance();
  508. if ($move) {
  509. continue;
  510. }
  511. $share->setTarget($shareTarget);
  512. $this->shareManager->moveShare($share, $destinationUid);
  513. continue;
  514. }
  515. } catch (\OCP\Files\NotFoundException $e) {
  516. $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>');
  517. } catch (\Throwable $e) {
  518. $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>');
  519. }
  520. $progress->advance();
  521. }
  522. $progress->finish();
  523. $output->writeln('');
  524. }
  525. }