SharedStorage.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. /**
  57. * Convert target path to source path and pass the function call to the correct storage provider
  58. */
  59. class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage, IDisableEncryptionStorage {
  60. /** @var \OCP\Share\IShare */
  61. private $superShare;
  62. /** @var \OCP\Share\IShare[] */
  63. private $groupedShares;
  64. /**
  65. * @var \OC\Files\View
  66. */
  67. private $ownerView;
  68. private $initialized = false;
  69. /**
  70. * @var ICacheEntry
  71. */
  72. private $sourceRootInfo;
  73. /** @var string */
  74. private $user;
  75. /**
  76. * @var \OCP\ILogger
  77. */
  78. private $logger;
  79. /** @var IStorage */
  80. private $nonMaskedStorage;
  81. private array $mountOptions = [];
  82. /** @var boolean */
  83. private $sharingDisabledForUser;
  84. /** @var ?Folder $ownerUserFolder */
  85. private $ownerUserFolder = null;
  86. private string $sourcePath = '';
  87. public function __construct($arguments) {
  88. $this->ownerView = $arguments['ownerView'];
  89. $this->logger = \OC::$server->getLogger();
  90. $this->superShare = $arguments['superShare'];
  91. $this->groupedShares = $arguments['groupedShares'];
  92. $this->user = $arguments['user'];
  93. if (isset($arguments['sharingDisabledForUser'])) {
  94. $this->sharingDisabledForUser = $arguments['sharingDisabledForUser'];
  95. } else {
  96. $this->sharingDisabledForUser = false;
  97. }
  98. parent::__construct([
  99. 'storage' => null,
  100. 'root' => null,
  101. ]);
  102. }
  103. /**
  104. * @return ICacheEntry
  105. */
  106. private function getSourceRootInfo() {
  107. if (is_null($this->sourceRootInfo)) {
  108. if (is_null($this->superShare->getNodeCacheEntry())) {
  109. $this->init();
  110. $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath);
  111. } else {
  112. $this->sourceRootInfo = $this->superShare->getNodeCacheEntry();
  113. }
  114. }
  115. return $this->sourceRootInfo;
  116. }
  117. private function init() {
  118. if ($this->initialized) {
  119. return;
  120. }
  121. $this->initialized = true;
  122. try {
  123. /** @var IRootFolder $rootFolder */
  124. $rootFolder = \OC::$server->get(IRootFolder::class);
  125. $this->ownerUserFolder = $rootFolder->getUserFolder($this->superShare->getShareOwner());
  126. $sourceId = $this->superShare->getNodeId();
  127. $ownerNodes = $this->ownerUserFolder->getById($sourceId);
  128. /** @var Node|false $ownerNode */
  129. $ownerNode = current($ownerNodes);
  130. if (!$ownerNode) {
  131. $this->storage = new FailedStorage(['exception' => new NotFoundException("File by id $sourceId not found")]);
  132. $this->cache = new FailedCache();
  133. $this->rootPath = '';
  134. } else {
  135. $this->nonMaskedStorage = $ownerNode->getStorage();
  136. $this->sourcePath = $ownerNode->getPath();
  137. $this->rootPath = $ownerNode->getInternalPath();
  138. $this->storage = new PermissionsMask([
  139. 'storage' => $this->nonMaskedStorage,
  140. 'mask' => $this->superShare->getPermissions(),
  141. ]);
  142. }
  143. } catch (NotFoundException $e) {
  144. // original file not accessible or deleted, set FailedStorage
  145. $this->storage = new FailedStorage(['exception' => $e]);
  146. $this->cache = new FailedCache();
  147. $this->rootPath = '';
  148. } catch (NoUserException $e) {
  149. // sharer user deleted, set FailedStorage
  150. $this->storage = new FailedStorage(['exception' => $e]);
  151. $this->cache = new FailedCache();
  152. $this->rootPath = '';
  153. } catch (\Exception $e) {
  154. $this->storage = new FailedStorage(['exception' => $e]);
  155. $this->cache = new FailedCache();
  156. $this->rootPath = '';
  157. $this->logger->logException($e);
  158. }
  159. if (!$this->nonMaskedStorage) {
  160. $this->nonMaskedStorage = $this->storage;
  161. }
  162. }
  163. /**
  164. * @inheritdoc
  165. */
  166. public function instanceOfStorage($class): bool {
  167. if ($class === '\OC\Files\Storage\Common' || $class == Common::class) {
  168. return true;
  169. }
  170. if (in_array($class, [
  171. '\OC\Files\Storage\Home',
  172. '\OC\Files\ObjectStore\HomeObjectStoreStorage',
  173. '\OCP\Files\IHomeStorage',
  174. Home::class,
  175. HomeObjectStoreStorage::class,
  176. IHomeStorage::class
  177. ])) {
  178. return false;
  179. }
  180. return parent::instanceOfStorage($class);
  181. }
  182. /**
  183. * @return string
  184. */
  185. public function getShareId() {
  186. return $this->superShare->getId();
  187. }
  188. private function isValid(): bool {
  189. return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
  190. }
  191. /**
  192. * get id of the mount point
  193. *
  194. * @return string
  195. */
  196. public function getId(): string {
  197. return 'shared::' . $this->getMountPoint();
  198. }
  199. /**
  200. * Get the permissions granted for a shared file
  201. *
  202. * @param string $target Shared target file path
  203. * @return int CRUDS permissions granted
  204. */
  205. public function getPermissions($target = ''): int {
  206. if (!$this->isValid()) {
  207. return 0;
  208. }
  209. $permissions = parent::getPermissions($target) & $this->superShare->getPermissions();
  210. // part files and the mount point always have delete permissions
  211. if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') {
  212. $permissions |= \OCP\Constants::PERMISSION_DELETE;
  213. }
  214. if ($this->sharingDisabledForUser) {
  215. $permissions &= ~\OCP\Constants::PERMISSION_SHARE;
  216. }
  217. return $permissions;
  218. }
  219. public function isCreatable($path): bool {
  220. return (bool)($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE);
  221. }
  222. public function isReadable($path): bool {
  223. if (!$this->isValid()) {
  224. return false;
  225. }
  226. if (!$this->file_exists($path)) {
  227. return false;
  228. }
  229. /** @var IStorage $storage */
  230. /** @var string $internalPath */
  231. [$storage, $internalPath] = $this->resolvePath($path);
  232. return $storage->isReadable($internalPath);
  233. }
  234. public function isUpdatable($path): bool {
  235. return (bool)($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE);
  236. }
  237. public function isDeletable($path): bool {
  238. return (bool)($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE);
  239. }
  240. public function isSharable($path): bool {
  241. if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) {
  242. return false;
  243. }
  244. return (bool)($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE);
  245. }
  246. public function fopen($path, $mode) {
  247. $source = $this->getUnjailedPath($path);
  248. switch ($mode) {
  249. case 'r+':
  250. case 'rb+':
  251. case 'w+':
  252. case 'wb+':
  253. case 'x+':
  254. case 'xb+':
  255. case 'a+':
  256. case 'ab+':
  257. case 'w':
  258. case 'wb':
  259. case 'x':
  260. case 'xb':
  261. case 'a':
  262. case 'ab':
  263. $creatable = $this->isCreatable(dirname($path));
  264. $updatable = $this->isUpdatable($path);
  265. // if neither permissions given, no need to continue
  266. if (!$creatable && !$updatable) {
  267. if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
  268. $updatable = $this->isUpdatable(dirname($path));
  269. }
  270. if (!$updatable) {
  271. return false;
  272. }
  273. }
  274. $exists = $this->file_exists($path);
  275. // if a file exists, updatable permissions are required
  276. if ($exists && !$updatable) {
  277. return false;
  278. }
  279. // part file is allowed if !$creatable but the final file is $updatable
  280. if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') {
  281. if (!$exists && !$creatable) {
  282. return false;
  283. }
  284. }
  285. }
  286. $info = [
  287. 'target' => $this->getMountPoint() . '/' . $path,
  288. 'source' => $source,
  289. 'mode' => $mode,
  290. ];
  291. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
  292. return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode);
  293. }
  294. /**
  295. * see https://www.php.net/manual/en/function.rename.php
  296. *
  297. * @param string $source
  298. * @param string $target
  299. * @return bool
  300. */
  301. public function rename($source, $target): bool {
  302. $this->init();
  303. $isPartFile = pathinfo($source, PATHINFO_EXTENSION) === 'part';
  304. $targetExists = $this->file_exists($target);
  305. $sameFolder = dirname($source) === dirname($target);
  306. if ($targetExists || ($sameFolder && !$isPartFile)) {
  307. if (!$this->isUpdatable('')) {
  308. return false;
  309. }
  310. } else {
  311. if (!$this->isCreatable('')) {
  312. return false;
  313. }
  314. }
  315. return $this->nonMaskedStorage->rename($this->getUnjailedPath($source), $this->getUnjailedPath($target));
  316. }
  317. /**
  318. * return mount point of share, relative to data/user/files
  319. *
  320. * @return string
  321. */
  322. public function getMountPoint(): string {
  323. return $this->superShare->getTarget();
  324. }
  325. /**
  326. * @param string $path
  327. */
  328. public function setMountPoint($path): void {
  329. $this->superShare->setTarget($path);
  330. foreach ($this->groupedShares as $share) {
  331. $share->setTarget($path);
  332. }
  333. }
  334. /**
  335. * get the user who shared the file
  336. *
  337. * @return string
  338. */
  339. public function getSharedFrom(): string {
  340. return $this->superShare->getShareOwner();
  341. }
  342. /**
  343. * @return \OCP\Share\IShare
  344. */
  345. public function getShare(): IShare {
  346. return $this->superShare;
  347. }
  348. /**
  349. * return share type, can be "file" or "folder"
  350. *
  351. * @return string
  352. */
  353. public function getItemType(): string {
  354. return $this->superShare->getNodeType();
  355. }
  356. public function getCache($path = '', $storage = null) {
  357. if ($this->cache) {
  358. return $this->cache;
  359. }
  360. if (!$storage) {
  361. $storage = $this;
  362. }
  363. $sourceRoot = $this->getSourceRootInfo();
  364. if ($this->storage instanceof FailedStorage) {
  365. return new FailedCache();
  366. }
  367. $this->cache = new \OCA\Files_Sharing\Cache(
  368. $storage,
  369. $sourceRoot,
  370. \OC::$server->get(DisplayNameCache::class)
  371. );
  372. return $this->cache;
  373. }
  374. public function getScanner($path = '', $storage = null) {
  375. if (!$storage) {
  376. $storage = $this;
  377. }
  378. return new \OCA\Files_Sharing\Scanner($storage);
  379. }
  380. public function getOwner($path): string {
  381. return $this->superShare->getShareOwner();
  382. }
  383. public function getWatcher($path = '', $storage = null): Watcher {
  384. $mountManager = \OC::$server->getMountManager();
  385. // Get node information
  386. $node = $this->getShare()->getNodeCacheEntry();
  387. if ($node) {
  388. $mount = $mountManager->findByNumericId($node->getStorageId());
  389. // If the share is originating from an external storage
  390. if (count($mount) > 0 && $mount[0] instanceof ExternalMountPoint) {
  391. // Propagate original storage scan
  392. return parent::getWatcher($path, $storage);
  393. }
  394. }
  395. // cache updating is handled by the share source
  396. return new NullWatcher();
  397. }
  398. /**
  399. * unshare complete storage, also the grouped shares
  400. *
  401. * @return bool
  402. */
  403. public function unshareStorage(): bool {
  404. foreach ($this->groupedShares as $share) {
  405. \OC::$server->getShareManager()->deleteFromSelf($share, $this->user);
  406. }
  407. return true;
  408. }
  409. /**
  410. * @param string $path
  411. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  412. * @param \OCP\Lock\ILockingProvider $provider
  413. * @throws \OCP\Lock\LockedException
  414. */
  415. public function acquireLock($path, $type, ILockingProvider $provider) {
  416. /** @var \OCP\Files\Storage $targetStorage */
  417. [$targetStorage, $targetInternalPath] = $this->resolvePath($path);
  418. $targetStorage->acquireLock($targetInternalPath, $type, $provider);
  419. // lock the parent folders of the owner when locking the share as recipient
  420. if ($path === '') {
  421. $sourcePath = $this->ownerUserFolder->getRelativePath($this->sourcePath);
  422. $this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
  423. }
  424. }
  425. /**
  426. * @param string $path
  427. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  428. * @param \OCP\Lock\ILockingProvider $provider
  429. */
  430. public function releaseLock($path, $type, ILockingProvider $provider) {
  431. /** @var \OCP\Files\Storage $targetStorage */
  432. [$targetStorage, $targetInternalPath] = $this->resolvePath($path);
  433. $targetStorage->releaseLock($targetInternalPath, $type, $provider);
  434. // unlock the parent folders of the owner when unlocking the share as recipient
  435. if ($path === '') {
  436. $sourcePath = $this->ownerUserFolder->getRelativePath($this->sourcePath);
  437. $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
  438. }
  439. }
  440. /**
  441. * @param string $path
  442. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  443. * @param \OCP\Lock\ILockingProvider $provider
  444. */
  445. public function changeLock($path, $type, ILockingProvider $provider) {
  446. /** @var \OCP\Files\Storage $targetStorage */
  447. [$targetStorage, $targetInternalPath] = $this->resolvePath($path);
  448. $targetStorage->changeLock($targetInternalPath, $type, $provider);
  449. }
  450. /**
  451. * @return array [ available, last_checked ]
  452. */
  453. public function getAvailability() {
  454. // shares do not participate in availability logic
  455. return [
  456. 'available' => true,
  457. 'last_checked' => 0,
  458. ];
  459. }
  460. /**
  461. * @param bool $available
  462. */
  463. public function setAvailability($available) {
  464. // shares do not participate in availability logic
  465. }
  466. public function getSourceStorage() {
  467. $this->init();
  468. return $this->nonMaskedStorage;
  469. }
  470. public function getWrapperStorage() {
  471. $this->init();
  472. return $this->storage;
  473. }
  474. public function file_get_contents($path) {
  475. $info = [
  476. 'target' => $this->getMountPoint() . '/' . $path,
  477. 'source' => $this->getUnjailedPath($path),
  478. ];
  479. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
  480. return parent::file_get_contents($path);
  481. }
  482. public function file_put_contents($path, $data) {
  483. $info = [
  484. 'target' => $this->getMountPoint() . '/' . $path,
  485. 'source' => $this->getUnjailedPath($path),
  486. ];
  487. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
  488. return parent::file_put_contents($path, $data);
  489. }
  490. /**
  491. * @return void
  492. */
  493. public function setMountOptions(array $options) {
  494. /* Note: This value is never read */
  495. $this->mountOptions = $options;
  496. }
  497. public function getUnjailedPath($path) {
  498. $this->init();
  499. return parent::getUnjailedPath($path);
  500. }
  501. }