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.

SharedStorage.php 15KB

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