SharedStorage.php 15 KB

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