SharedStorage.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author J0WI <J0WI@users.noreply.github.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Robin McCorkell <robin@mccorkell.me.uk>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author scambra <sergio@entrecables.com>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Vincent Petry <vincent@nextcloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCA\Files_Sharing;
  34. use OC\Files\Cache\FailedCache;
  35. use OC\Files\Cache\NullWatcher;
  36. use OC\Files\Cache\Watcher;
  37. use OC\Files\ObjectStore\HomeObjectStoreStorage;
  38. use OC\Files\Storage\Common;
  39. use OC\Files\Storage\Home;
  40. use OC\User\DisplayNameCache;
  41. use OCP\Files\Folder;
  42. use OCP\Files\IHomeStorage;
  43. use OCP\Files\Node;
  44. use OC\Files\Storage\FailedStorage;
  45. use OC\Files\Storage\Wrapper\PermissionsMask;
  46. use OC\User\NoUserException;
  47. use OCA\Files_External\Config\ExternalMountPoint;
  48. use OCP\Constants;
  49. use OCP\Files\Cache\ICacheEntry;
  50. use OCP\Files\IRootFolder;
  51. use OCP\Files\NotFoundException;
  52. use OCP\Files\Storage\IDisableEncryptionStorage;
  53. use OCP\Files\Storage\IStorage;
  54. use OCP\Lock\ILockingProvider;
  55. use OCP\Share\IShare;
  56. use Psr\Log\LoggerInterface;
  57. /**
  58. * Convert target path to source path and pass the function call to the correct storage provider
  59. */
  60. class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage, IDisableEncryptionStorage {
  61. /** @var \OCP\Share\IShare */
  62. private $superShare;
  63. /** @var \OCP\Share\IShare[] */
  64. private $groupedShares;
  65. /**
  66. * @var \OC\Files\View
  67. */
  68. private $ownerView;
  69. private $initialized = false;
  70. /**
  71. * @var ICacheEntry
  72. */
  73. private $sourceRootInfo;
  74. /** @var string */
  75. private $user;
  76. private LoggerInterface $logger;
  77. /** @var IStorage */
  78. private $nonMaskedStorage;
  79. private array $mountOptions = [];
  80. /** @var boolean */
  81. private $sharingDisabledForUser;
  82. /** @var ?Folder $ownerUserFolder */
  83. private $ownerUserFolder = null;
  84. private string $sourcePath = '';
  85. public function __construct($arguments) {
  86. $this->ownerView = $arguments['ownerView'];
  87. $this->logger = \OC::$server->get(LoggerInterface::class);
  88. $this->superShare = $arguments['superShare'];
  89. $this->groupedShares = $arguments['groupedShares'];
  90. $this->user = $arguments['user'];
  91. if (isset($arguments['sharingDisabledForUser'])) {
  92. $this->sharingDisabledForUser = $arguments['sharingDisabledForUser'];
  93. } else {
  94. $this->sharingDisabledForUser = false;
  95. }
  96. parent::__construct([
  97. 'storage' => null,
  98. 'root' => null,
  99. ]);
  100. }
  101. /**
  102. * @return ICacheEntry
  103. */
  104. private function getSourceRootInfo() {
  105. if (is_null($this->sourceRootInfo)) {
  106. if (is_null($this->superShare->getNodeCacheEntry())) {
  107. $this->init();
  108. $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath);
  109. } else {
  110. $this->sourceRootInfo = $this->superShare->getNodeCacheEntry();
  111. }
  112. }
  113. return $this->sourceRootInfo;
  114. }
  115. private function init() {
  116. if ($this->initialized) {
  117. return;
  118. }
  119. $this->initialized = true;
  120. try {
  121. /** @var IRootFolder $rootFolder */
  122. $rootFolder = \OC::$server->get(IRootFolder::class);
  123. $this->ownerUserFolder = $rootFolder->getUserFolder($this->superShare->getShareOwner());
  124. $sourceId = $this->superShare->getNodeId();
  125. $ownerNodes = $this->ownerUserFolder->getById($sourceId);
  126. /** @var Node|false $ownerNode */
  127. $ownerNode = current($ownerNodes);
  128. if (!$ownerNode) {
  129. $this->storage = new FailedStorage(['exception' => new NotFoundException("File by id $sourceId not found")]);
  130. $this->cache = new FailedCache();
  131. $this->rootPath = '';
  132. } else {
  133. $this->nonMaskedStorage = $ownerNode->getStorage();
  134. $this->sourcePath = $ownerNode->getPath();
  135. $this->rootPath = $ownerNode->getInternalPath();
  136. $this->storage = new PermissionsMask([
  137. 'storage' => $this->nonMaskedStorage,
  138. 'mask' => $this->superShare->getPermissions(),
  139. ]);
  140. }
  141. } catch (NotFoundException $e) {
  142. // original file not accessible or deleted, set FailedStorage
  143. $this->storage = new FailedStorage(['exception' => $e]);
  144. $this->cache = new FailedCache();
  145. $this->rootPath = '';
  146. } catch (NoUserException $e) {
  147. // sharer user deleted, set FailedStorage
  148. $this->storage = new FailedStorage(['exception' => $e]);
  149. $this->cache = new FailedCache();
  150. $this->rootPath = '';
  151. } catch (\Exception $e) {
  152. $this->storage = new FailedStorage(['exception' => $e]);
  153. $this->cache = new FailedCache();
  154. $this->rootPath = '';
  155. $this->logger->error($e->getMessage(), ['exception' => $e]);
  156. }
  157. if (!$this->nonMaskedStorage) {
  158. $this->nonMaskedStorage = $this->storage;
  159. }
  160. }
  161. /**
  162. * @inheritdoc
  163. */
  164. public function instanceOfStorage($class): bool {
  165. if ($class === '\OC\Files\Storage\Common' || $class == Common::class) {
  166. return true;
  167. }
  168. if (in_array($class, [
  169. '\OC\Files\Storage\Home',
  170. '\OC\Files\ObjectStore\HomeObjectStoreStorage',
  171. '\OCP\Files\IHomeStorage',
  172. Home::class,
  173. HomeObjectStoreStorage::class,
  174. IHomeStorage::class
  175. ])) {
  176. return false;
  177. }
  178. return parent::instanceOfStorage($class);
  179. }
  180. /**
  181. * @return string
  182. */
  183. public function getShareId() {
  184. return $this->superShare->getId();
  185. }
  186. private function isValid(): bool {
  187. return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
  188. }
  189. /**
  190. * get id of the mount point
  191. *
  192. * @return string
  193. */
  194. public function getId(): string {
  195. return 'shared::' . $this->getMountPoint();
  196. }
  197. /**
  198. * Get the permissions granted for a shared file
  199. *
  200. * @param string $path Shared target file path
  201. * @return int CRUDS permissions granted
  202. */
  203. public function getPermissions($path = ''): int {
  204. if (!$this->isValid()) {
  205. return 0;
  206. }
  207. $permissions = parent::getPermissions($path) & $this->superShare->getPermissions();
  208. // part files and the mount point always have delete permissions
  209. if ($path === '' || pathinfo($path, PATHINFO_EXTENSION) === 'part') {
  210. $permissions |= \OCP\Constants::PERMISSION_DELETE;
  211. }
  212. if ($this->sharingDisabledForUser) {
  213. $permissions &= ~\OCP\Constants::PERMISSION_SHARE;
  214. }
  215. return $permissions;
  216. }
  217. public function isCreatable($path): bool {
  218. return (bool)($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE);
  219. }
  220. public function isReadable($path): bool {
  221. if (!$this->isValid()) {
  222. return false;
  223. }
  224. if (!$this->file_exists($path)) {
  225. return false;
  226. }
  227. /** @var IStorage $storage */
  228. /** @var string $internalPath */
  229. [$storage, $internalPath] = $this->resolvePath($path);
  230. return $storage->isReadable($internalPath);
  231. }
  232. public function isUpdatable($path): bool {
  233. return (bool)($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE);
  234. }
  235. public function isDeletable($path): bool {
  236. return (bool)($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE);
  237. }
  238. public function isSharable($path): bool {
  239. if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) {
  240. return false;
  241. }
  242. return (bool)($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE);
  243. }
  244. public function fopen($path, $mode) {
  245. $source = $this->getUnjailedPath($path);
  246. switch ($mode) {
  247. case 'r+':
  248. case 'rb+':
  249. case 'w+':
  250. case 'wb+':
  251. case 'x+':
  252. case 'xb+':
  253. case 'a+':
  254. case 'ab+':
  255. case 'w':
  256. case 'wb':
  257. case 'x':
  258. case 'xb':
  259. case 'a':
  260. case 'ab':
  261. $creatable = $this->isCreatable(dirname($path));
  262. $updatable = $this->isUpdatable($path);
  263. // if neither permissions given, no need to continue
  264. if (!$creatable && !$updatable) {
  265. if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
  266. $updatable = $this->isUpdatable(dirname($path));
  267. }
  268. if (!$updatable) {
  269. return false;
  270. }
  271. }
  272. $exists = $this->file_exists($path);
  273. // if a file exists, updatable permissions are required
  274. if ($exists && !$updatable) {
  275. return false;
  276. }
  277. // part file is allowed if !$creatable but the final file is $updatable
  278. if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') {
  279. if (!$exists && !$creatable) {
  280. return false;
  281. }
  282. }
  283. }
  284. $info = [
  285. 'target' => $this->getMountPoint() . '/' . $path,
  286. 'source' => $source,
  287. 'mode' => $mode,
  288. ];
  289. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
  290. return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode);
  291. }
  292. /**
  293. * see https://www.php.net/manual/en/function.rename.php
  294. *
  295. * @param string $source
  296. * @param string $target
  297. * @return bool
  298. */
  299. public function rename($source, $target): bool {
  300. $this->init();
  301. $isPartFile = pathinfo($source, PATHINFO_EXTENSION) === 'part';
  302. $targetExists = $this->file_exists($target);
  303. $sameFolder = dirname($source) === dirname($target);
  304. if ($targetExists || ($sameFolder && !$isPartFile)) {
  305. if (!$this->isUpdatable('')) {
  306. return false;
  307. }
  308. } else {
  309. if (!$this->isCreatable('')) {
  310. return false;
  311. }
  312. }
  313. return $this->nonMaskedStorage->rename($this->getUnjailedPath($source), $this->getUnjailedPath($target));
  314. }
  315. /**
  316. * return mount point of share, relative to data/user/files
  317. *
  318. * @return string
  319. */
  320. public function getMountPoint(): string {
  321. return $this->superShare->getTarget();
  322. }
  323. /**
  324. * @param string $path
  325. */
  326. public function setMountPoint($path): void {
  327. $this->superShare->setTarget($path);
  328. foreach ($this->groupedShares as $share) {
  329. $share->setTarget($path);
  330. }
  331. }
  332. /**
  333. * get the user who shared the file
  334. *
  335. * @return string
  336. */
  337. public function getSharedFrom(): string {
  338. return $this->superShare->getShareOwner();
  339. }
  340. /**
  341. * @return \OCP\Share\IShare
  342. */
  343. public function getShare(): IShare {
  344. return $this->superShare;
  345. }
  346. /**
  347. * return share type, can be "file" or "folder"
  348. *
  349. * @return string
  350. */
  351. public function getItemType(): string {
  352. return $this->superShare->getNodeType();
  353. }
  354. public function getCache($path = '', $storage = null) {
  355. if ($this->cache) {
  356. return $this->cache;
  357. }
  358. if (!$storage) {
  359. $storage = $this;
  360. }
  361. $sourceRoot = $this->getSourceRootInfo();
  362. if ($this->storage instanceof FailedStorage) {
  363. return new FailedCache();
  364. }
  365. $this->cache = new \OCA\Files_Sharing\Cache(
  366. $storage,
  367. $sourceRoot,
  368. \OC::$server->get(DisplayNameCache::class),
  369. $this->getShare()
  370. );
  371. return $this->cache;
  372. }
  373. public function getScanner($path = '', $storage = null) {
  374. if (!$storage) {
  375. $storage = $this;
  376. }
  377. return new \OCA\Files_Sharing\Scanner($storage);
  378. }
  379. public function getOwner($path): string {
  380. return $this->superShare->getShareOwner();
  381. }
  382. public function getWatcher($path = '', $storage = null): Watcher {
  383. $mountManager = \OC::$server->getMountManager();
  384. // Get node information
  385. $node = $this->getShare()->getNodeCacheEntry();
  386. if ($node) {
  387. $mount = $mountManager->findByNumericId($node->getStorageId());
  388. // If the share is originating from an external storage
  389. if (count($mount) > 0 && $mount[0] instanceof ExternalMountPoint) {
  390. // Propagate original storage scan
  391. return parent::getWatcher($path, $storage);
  392. }
  393. }
  394. // cache updating is handled by the share source
  395. return new NullWatcher();
  396. }
  397. /**
  398. * unshare complete storage, also the grouped shares
  399. *
  400. * @return bool
  401. */
  402. public function unshareStorage(): bool {
  403. foreach ($this->groupedShares as $share) {
  404. \OC::$server->getShareManager()->deleteFromSelf($share, $this->user);
  405. }
  406. return true;
  407. }
  408. /**
  409. * @param string $path
  410. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  411. * @param \OCP\Lock\ILockingProvider $provider
  412. * @throws \OCP\Lock\LockedException
  413. */
  414. public function acquireLock($path, $type, ILockingProvider $provider) {
  415. /** @var \OCP\Files\Storage $targetStorage */
  416. [$targetStorage, $targetInternalPath] = $this->resolvePath($path);
  417. $targetStorage->acquireLock($targetInternalPath, $type, $provider);
  418. // lock the parent folders of the owner when locking the share as recipient
  419. if ($path === '') {
  420. $sourcePath = $this->ownerUserFolder->getRelativePath($this->sourcePath);
  421. $this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
  422. }
  423. }
  424. /**
  425. * @param string $path
  426. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  427. * @param \OCP\Lock\ILockingProvider $provider
  428. */
  429. public function releaseLock($path, $type, ILockingProvider $provider) {
  430. /** @var \OCP\Files\Storage $targetStorage */
  431. [$targetStorage, $targetInternalPath] = $this->resolvePath($path);
  432. $targetStorage->releaseLock($targetInternalPath, $type, $provider);
  433. // unlock the parent folders of the owner when unlocking the share as recipient
  434. if ($path === '') {
  435. $sourcePath = $this->ownerUserFolder->getRelativePath($this->sourcePath);
  436. $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
  437. }
  438. }
  439. /**
  440. * @param string $path
  441. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  442. * @param \OCP\Lock\ILockingProvider $provider
  443. */
  444. public function changeLock($path, $type, ILockingProvider $provider) {
  445. /** @var \OCP\Files\Storage $targetStorage */
  446. [$targetStorage, $targetInternalPath] = $this->resolvePath($path);
  447. $targetStorage->changeLock($targetInternalPath, $type, $provider);
  448. }
  449. /**
  450. * @return array [ available, last_checked ]
  451. */
  452. public function getAvailability() {
  453. // shares do not participate in availability logic
  454. return [
  455. 'available' => true,
  456. 'last_checked' => 0,
  457. ];
  458. }
  459. /**
  460. * @param bool $isAvailable
  461. */
  462. public function setAvailability($isAvailable) {
  463. // shares do not participate in availability logic
  464. }
  465. public function getSourceStorage() {
  466. $this->init();
  467. return $this->nonMaskedStorage;
  468. }
  469. public function getWrapperStorage() {
  470. $this->init();
  471. return $this->storage;
  472. }
  473. public function file_get_contents($path) {
  474. $info = [
  475. 'target' => $this->getMountPoint() . '/' . $path,
  476. 'source' => $this->getUnjailedPath($path),
  477. ];
  478. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
  479. return parent::file_get_contents($path);
  480. }
  481. public function file_put_contents($path, $data) {
  482. $info = [
  483. 'target' => $this->getMountPoint() . '/' . $path,
  484. 'source' => $this->getUnjailedPath($path),
  485. ];
  486. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
  487. return parent::file_put_contents($path, $data);
  488. }
  489. /**
  490. * @return void
  491. */
  492. public function setMountOptions(array $options) {
  493. /* Note: This value is never read */
  494. $this->mountOptions = $options;
  495. }
  496. public function getUnjailedPath($path) {
  497. $this->init();
  498. return parent::getUnjailedPath($path);
  499. }
  500. }