FederatedShareProvider.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Georg Ehrke <oc.list@georgehrke.com>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Maxence Lange <maxence@artificial-owl.com>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Robin Appelman <robin@icewind.nl>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author Sergej Pupykin <pupykin.s@gmail.com>
  18. * @author Stefan Weil <sw@weilnetz.de>
  19. * @author Thomas Müller <thomas.mueller@tmit.eu>
  20. * @author Valdnet <47037905+Valdnet@users.noreply.github.com>
  21. * @author Vincent Petry <vincent@nextcloud.com>
  22. *
  23. * @license AGPL-3.0
  24. *
  25. * This code is free software: you can redistribute it and/or modify
  26. * it under the terms of the GNU Affero General Public License, version 3,
  27. * as published by the Free Software Foundation.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU Affero General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU Affero General Public License, version 3,
  35. * along with this program. If not, see <http://www.gnu.org/licenses/>
  36. *
  37. */
  38. namespace OCA\FederatedFileSharing;
  39. use OC\Share20\Exception\InvalidShare;
  40. use OC\Share20\Share;
  41. use OCP\Constants;
  42. use OCP\DB\QueryBuilder\IQueryBuilder;
  43. use OCP\Federation\ICloudFederationProviderManager;
  44. use OCP\Federation\ICloudIdManager;
  45. use OCP\Files\Folder;
  46. use OCP\Files\IRootFolder;
  47. use OCP\Files\Node;
  48. use OCP\Files\NotFoundException;
  49. use OCP\IConfig;
  50. use OCP\IDBConnection;
  51. use OCP\IL10N;
  52. use OCP\ILogger;
  53. use OCP\IUserManager;
  54. use OCP\Share\Exceptions\GenericShareException;
  55. use OCP\Share\Exceptions\ShareNotFound;
  56. use OCP\Share\IShare;
  57. use OCP\Share\IShareProvider;
  58. /**
  59. * Class FederatedShareProvider
  60. *
  61. * @package OCA\FederatedFileSharing
  62. */
  63. class FederatedShareProvider implements IShareProvider {
  64. public const SHARE_TYPE_REMOTE = 6;
  65. /** @var IDBConnection */
  66. private $dbConnection;
  67. /** @var AddressHandler */
  68. private $addressHandler;
  69. /** @var Notifications */
  70. private $notifications;
  71. /** @var TokenHandler */
  72. private $tokenHandler;
  73. /** @var IL10N */
  74. private $l;
  75. /** @var ILogger */
  76. private $logger;
  77. /** @var IRootFolder */
  78. private $rootFolder;
  79. /** @var IConfig */
  80. private $config;
  81. /** @var string */
  82. private $externalShareTable = 'share_external';
  83. /** @var IUserManager */
  84. private $userManager;
  85. /** @var ICloudIdManager */
  86. private $cloudIdManager;
  87. /** @var \OCP\GlobalScale\IConfig */
  88. private $gsConfig;
  89. /** @var ICloudFederationProviderManager */
  90. private $cloudFederationProviderManager;
  91. /** @var array list of supported share types */
  92. private $supportedShareType = [IShare::TYPE_REMOTE_GROUP, IShare::TYPE_REMOTE, IShare::TYPE_CIRCLE];
  93. /**
  94. * DefaultShareProvider constructor.
  95. *
  96. * @param IDBConnection $connection
  97. * @param AddressHandler $addressHandler
  98. * @param Notifications $notifications
  99. * @param TokenHandler $tokenHandler
  100. * @param IL10N $l10n
  101. * @param ILogger $logger
  102. * @param IRootFolder $rootFolder
  103. * @param IConfig $config
  104. * @param IUserManager $userManager
  105. * @param ICloudIdManager $cloudIdManager
  106. * @param \OCP\GlobalScale\IConfig $globalScaleConfig
  107. * @param ICloudFederationProviderManager $cloudFederationProviderManager
  108. */
  109. public function __construct(
  110. IDBConnection $connection,
  111. AddressHandler $addressHandler,
  112. Notifications $notifications,
  113. TokenHandler $tokenHandler,
  114. IL10N $l10n,
  115. ILogger $logger,
  116. IRootFolder $rootFolder,
  117. IConfig $config,
  118. IUserManager $userManager,
  119. ICloudIdManager $cloudIdManager,
  120. \OCP\GlobalScale\IConfig $globalScaleConfig,
  121. ICloudFederationProviderManager $cloudFederationProviderManager
  122. ) {
  123. $this->dbConnection = $connection;
  124. $this->addressHandler = $addressHandler;
  125. $this->notifications = $notifications;
  126. $this->tokenHandler = $tokenHandler;
  127. $this->l = $l10n;
  128. $this->logger = $logger;
  129. $this->rootFolder = $rootFolder;
  130. $this->config = $config;
  131. $this->userManager = $userManager;
  132. $this->cloudIdManager = $cloudIdManager;
  133. $this->gsConfig = $globalScaleConfig;
  134. $this->cloudFederationProviderManager = $cloudFederationProviderManager;
  135. }
  136. /**
  137. * Return the identifier of this provider.
  138. *
  139. * @return string Containing only [a-zA-Z0-9]
  140. */
  141. public function identifier() {
  142. return 'ocFederatedSharing';
  143. }
  144. /**
  145. * Share a path
  146. *
  147. * @param IShare $share
  148. * @return IShare The share object
  149. * @throws ShareNotFound
  150. * @throws \Exception
  151. */
  152. public function create(IShare $share) {
  153. $shareWith = $share->getSharedWith();
  154. $itemSource = $share->getNodeId();
  155. $itemType = $share->getNodeType();
  156. $permissions = $share->getPermissions();
  157. $sharedBy = $share->getSharedBy();
  158. $shareType = $share->getShareType();
  159. $expirationDate = $share->getExpirationDate();
  160. if ($shareType === IShare::TYPE_REMOTE_GROUP &&
  161. !$this->isOutgoingServer2serverGroupShareEnabled()
  162. ) {
  163. $message = 'It is not allowed to send federated group shares from this server.';
  164. $message_t = $this->l->t('It is not allowed to send federated group shares from this server.');
  165. $this->logger->debug($message, ['app' => 'Federated File Sharing']);
  166. throw new \Exception($message_t);
  167. }
  168. /*
  169. * Check if file is not already shared with the remote user
  170. */
  171. $alreadyShared = $this->getSharedWith($shareWith, IShare::TYPE_REMOTE, $share->getNode(), 1, 0);
  172. $alreadySharedGroup = $this->getSharedWith($shareWith, IShare::TYPE_REMOTE_GROUP, $share->getNode(), 1, 0);
  173. if (!empty($alreadyShared) || !empty($alreadySharedGroup)) {
  174. $message = 'Sharing %1$s failed, because this item is already shared with %2$s';
  175. $message_t = $this->l->t('Sharing %1$s failed, because this item is already shared with user %2$s', [$share->getNode()->getName(), $shareWith]);
  176. $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
  177. throw new \Exception($message_t);
  178. }
  179. // don't allow federated shares if source and target server are the same
  180. $cloudId = $this->cloudIdManager->resolveCloudId($shareWith);
  181. $currentServer = $this->addressHandler->generateRemoteURL();
  182. $currentUser = $sharedBy;
  183. if ($this->addressHandler->compareAddresses($cloudId->getUser(), $cloudId->getRemote(), $currentUser, $currentServer)) {
  184. $message = 'Not allowed to create a federated share with the same user.';
  185. $message_t = $this->l->t('Not allowed to create a federated share with the same user');
  186. $this->logger->debug($message, ['app' => 'Federated File Sharing']);
  187. throw new \Exception($message_t);
  188. }
  189. // Federated shares always have read permissions
  190. if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
  191. $message = 'Federated shares require read permissions';
  192. $message_t = $this->l->t('Federated shares require read permissions');
  193. $this->logger->debug($message, ['app' => 'Federated File Sharing']);
  194. throw new \Exception($message_t);
  195. }
  196. $share->setSharedWith($cloudId->getId());
  197. try {
  198. $remoteShare = $this->getShareFromExternalShareTable($share);
  199. } catch (ShareNotFound $e) {
  200. $remoteShare = null;
  201. }
  202. if ($remoteShare) {
  203. try {
  204. $ownerCloudId = $this->cloudIdManager->getCloudId($remoteShare['owner'], $remoteShare['remote']);
  205. $shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time(), $shareType, $expirationDate);
  206. $share->setId($shareId);
  207. [$token, $remoteId] = $this->askOwnerToReShare($shareWith, $share, $shareId);
  208. // remote share was create successfully if we get a valid token as return
  209. $send = is_string($token) && $token !== '';
  210. } catch (\Exception $e) {
  211. // fall back to old re-share behavior if the remote server
  212. // doesn't support flat re-shares (was introduced with Nextcloud 9.1)
  213. $this->removeShareFromTable($share);
  214. $shareId = $this->createFederatedShare($share);
  215. }
  216. if ($send) {
  217. $this->updateSuccessfulReshare($shareId, $token);
  218. $this->storeRemoteId($shareId, $remoteId);
  219. } else {
  220. $this->removeShareFromTable($share);
  221. $message_t = $this->l->t('File is already shared with %s', [$shareWith]);
  222. throw new \Exception($message_t);
  223. }
  224. } else {
  225. $shareId = $this->createFederatedShare($share);
  226. }
  227. $data = $this->getRawShare($shareId);
  228. return $this->createShareObject($data);
  229. }
  230. /**
  231. * create federated share and inform the recipient
  232. *
  233. * @param IShare $share
  234. * @return int
  235. * @throws ShareNotFound
  236. * @throws \Exception
  237. */
  238. protected function createFederatedShare(IShare $share) {
  239. $token = $this->tokenHandler->generateToken();
  240. $shareId = $this->addShareToDB(
  241. $share->getNodeId(),
  242. $share->getNodeType(),
  243. $share->getSharedWith(),
  244. $share->getSharedBy(),
  245. $share->getShareOwner(),
  246. $share->getPermissions(),
  247. $token,
  248. $share->getShareType(),
  249. $share->getExpirationDate()
  250. );
  251. $failure = false;
  252. try {
  253. $sharedByFederatedId = $share->getSharedBy();
  254. if ($this->userManager->userExists($sharedByFederatedId)) {
  255. $cloudId = $this->cloudIdManager->getCloudId($sharedByFederatedId, $this->addressHandler->generateRemoteURL());
  256. $sharedByFederatedId = $cloudId->getId();
  257. }
  258. $ownerCloudId = $this->cloudIdManager->getCloudId($share->getShareOwner(), $this->addressHandler->generateRemoteURL());
  259. $send = $this->notifications->sendRemoteShare(
  260. $token,
  261. $share->getSharedWith(),
  262. $share->getNode()->getName(),
  263. $shareId,
  264. $share->getShareOwner(),
  265. $ownerCloudId->getId(),
  266. $share->getSharedBy(),
  267. $sharedByFederatedId,
  268. $share->getShareType()
  269. );
  270. if ($send === false) {
  271. $failure = true;
  272. }
  273. } catch (\Exception $e) {
  274. $this->logger->logException($e, [
  275. 'message' => 'Failed to notify remote server of federated share, removing share.',
  276. 'level' => ILogger::ERROR,
  277. 'app' => 'federatedfilesharing',
  278. ]);
  279. $failure = true;
  280. }
  281. if ($failure) {
  282. $this->removeShareFromTableById($shareId);
  283. $message_t = $this->l->t('Sharing %1$s failed, could not find %2$s, maybe the server is currently unreachable or uses a self-signed certificate.',
  284. [$share->getNode()->getName(), $share->getSharedWith()]);
  285. throw new \Exception($message_t);
  286. }
  287. return $shareId;
  288. }
  289. /**
  290. * @param string $shareWith
  291. * @param IShare $share
  292. * @param string $shareId internal share Id
  293. * @return array
  294. * @throws \Exception
  295. */
  296. protected function askOwnerToReShare($shareWith, IShare $share, $shareId) {
  297. $remoteShare = $this->getShareFromExternalShareTable($share);
  298. $token = $remoteShare['share_token'];
  299. $remoteId = $remoteShare['remote_id'];
  300. $remote = $remoteShare['remote'];
  301. [$token, $remoteId] = $this->notifications->requestReShare(
  302. $token,
  303. $remoteId,
  304. $shareId,
  305. $remote,
  306. $shareWith,
  307. $share->getPermissions(),
  308. $share->getNode()->getName()
  309. );
  310. return [$token, $remoteId];
  311. }
  312. /**
  313. * get federated share from the share_external table but exclude mounted link shares
  314. *
  315. * @param IShare $share
  316. * @return array
  317. * @throws ShareNotFound
  318. */
  319. protected function getShareFromExternalShareTable(IShare $share) {
  320. $query = $this->dbConnection->getQueryBuilder();
  321. $query->select('*')->from($this->externalShareTable)
  322. ->where($query->expr()->eq('user', $query->createNamedParameter($share->getShareOwner())))
  323. ->andWhere($query->expr()->eq('mountpoint', $query->createNamedParameter($share->getTarget())));
  324. $qResult = $query->execute();
  325. $result = $qResult->fetchAll();
  326. $qResult->closeCursor();
  327. if (isset($result[0]) && (int)$result[0]['remote_id'] > 0) {
  328. return $result[0];
  329. }
  330. throw new ShareNotFound('share not found in share_external table');
  331. }
  332. /**
  333. * add share to the database and return the ID
  334. *
  335. * @param int $itemSource
  336. * @param string $itemType
  337. * @param string $shareWith
  338. * @param string $sharedBy
  339. * @param string $uidOwner
  340. * @param int $permissions
  341. * @param string $token
  342. * @param int $shareType
  343. * @param \DateTime $expirationDate
  344. * @return int
  345. */
  346. private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $shareType, $expirationDate) {
  347. $qb = $this->dbConnection->getQueryBuilder();
  348. $qb->insert('share')
  349. ->setValue('share_type', $qb->createNamedParameter($shareType))
  350. ->setValue('item_type', $qb->createNamedParameter($itemType))
  351. ->setValue('item_source', $qb->createNamedParameter($itemSource))
  352. ->setValue('file_source', $qb->createNamedParameter($itemSource))
  353. ->setValue('share_with', $qb->createNamedParameter($shareWith))
  354. ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
  355. ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
  356. ->setValue('permissions', $qb->createNamedParameter($permissions))
  357. ->setValue('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE))
  358. ->setValue('token', $qb->createNamedParameter($token))
  359. ->setValue('stime', $qb->createNamedParameter(time()));
  360. /*
  361. * Added to fix https://github.com/owncloud/core/issues/22215
  362. * Can be removed once we get rid of ajax/share.php
  363. */
  364. $qb->setValue('file_target', $qb->createNamedParameter(''));
  365. $qb->execute();
  366. return $qb->getLastInsertId();
  367. }
  368. /**
  369. * Update a share
  370. *
  371. * @param IShare $share
  372. * @return IShare The share object
  373. */
  374. public function update(IShare $share) {
  375. /*
  376. * We allow updating the permissions of federated shares
  377. */
  378. $qb = $this->dbConnection->getQueryBuilder();
  379. $qb->update('share')
  380. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  381. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  382. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  383. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  384. ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
  385. ->execute();
  386. // send the updated permission to the owner/initiator, if they are not the same
  387. if ($share->getShareOwner() !== $share->getSharedBy()) {
  388. $this->sendPermissionUpdate($share);
  389. }
  390. return $share;
  391. }
  392. /**
  393. * send the updated permission to the owner/initiator, if they are not the same
  394. *
  395. * @param IShare $share
  396. * @throws ShareNotFound
  397. * @throws \OCP\HintException
  398. */
  399. protected function sendPermissionUpdate(IShare $share) {
  400. $remoteId = $this->getRemoteId($share);
  401. // if the local user is the owner we send the permission change to the initiator
  402. if ($this->userManager->userExists($share->getShareOwner())) {
  403. [, $remote] = $this->addressHandler->splitUserRemote($share->getSharedBy());
  404. } else { // ... if not we send the permission change to the owner
  405. [, $remote] = $this->addressHandler->splitUserRemote($share->getShareOwner());
  406. }
  407. $this->notifications->sendPermissionChange($remote, $remoteId, $share->getToken(), $share->getPermissions());
  408. }
  409. /**
  410. * update successful reShare with the correct token
  411. *
  412. * @param int $shareId
  413. * @param string $token
  414. */
  415. protected function updateSuccessfulReShare($shareId, $token) {
  416. $query = $this->dbConnection->getQueryBuilder();
  417. $query->update('share')
  418. ->where($query->expr()->eq('id', $query->createNamedParameter($shareId)))
  419. ->set('token', $query->createNamedParameter($token))
  420. ->execute();
  421. }
  422. /**
  423. * store remote ID in federated reShare table
  424. *
  425. * @param $shareId
  426. * @param $remoteId
  427. */
  428. public function storeRemoteId(int $shareId, string $remoteId): void {
  429. $query = $this->dbConnection->getQueryBuilder();
  430. $query->insert('federated_reshares')
  431. ->values(
  432. [
  433. 'share_id' => $query->createNamedParameter($shareId),
  434. 'remote_id' => $query->createNamedParameter($remoteId),
  435. ]
  436. );
  437. $query->execute();
  438. }
  439. /**
  440. * get share ID on remote server for federated re-shares
  441. *
  442. * @param IShare $share
  443. * @return string
  444. * @throws ShareNotFound
  445. */
  446. public function getRemoteId(IShare $share): string {
  447. $query = $this->dbConnection->getQueryBuilder();
  448. $query->select('remote_id')->from('federated_reshares')
  449. ->where($query->expr()->eq('share_id', $query->createNamedParameter((int)$share->getId())));
  450. $result = $query->execute();
  451. $data = $result->fetch();
  452. $result->closeCursor();
  453. if (!is_array($data) || !isset($data['remote_id'])) {
  454. throw new ShareNotFound();
  455. }
  456. return (string)$data['remote_id'];
  457. }
  458. /**
  459. * @inheritdoc
  460. */
  461. public function move(IShare $share, $recipient) {
  462. /*
  463. * This function does nothing yet as it is just for outgoing
  464. * federated shares.
  465. */
  466. return $share;
  467. }
  468. /**
  469. * Get all children of this share
  470. *
  471. * @param IShare $parent
  472. * @return IShare[]
  473. */
  474. public function getChildren(IShare $parent) {
  475. $children = [];
  476. $qb = $this->dbConnection->getQueryBuilder();
  477. $qb->select('*')
  478. ->from('share')
  479. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
  480. ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
  481. ->orderBy('id');
  482. $cursor = $qb->execute();
  483. while ($data = $cursor->fetch()) {
  484. $children[] = $this->createShareObject($data);
  485. }
  486. $cursor->closeCursor();
  487. return $children;
  488. }
  489. /**
  490. * Delete a share (owner unShares the file)
  491. *
  492. * @param IShare $share
  493. * @throws ShareNotFound
  494. * @throws \OCP\HintException
  495. */
  496. public function delete(IShare $share) {
  497. [, $remote] = $this->addressHandler->splitUserRemote($share->getSharedWith());
  498. // if the local user is the owner we can send the unShare request directly...
  499. if ($this->userManager->userExists($share->getShareOwner())) {
  500. $this->notifications->sendRemoteUnShare($remote, $share->getId(), $share->getToken());
  501. $this->revokeShare($share, true);
  502. } else { // ... if not we need to correct ID for the unShare request
  503. $remoteId = $this->getRemoteId($share);
  504. $this->notifications->sendRemoteUnShare($remote, $remoteId, $share->getToken());
  505. $this->revokeShare($share, false);
  506. }
  507. // only remove the share when all messages are send to not lose information
  508. // about the share to early
  509. $this->removeShareFromTable($share);
  510. }
  511. /**
  512. * in case of a re-share we need to send the other use (initiator or owner)
  513. * a message that the file was unshared
  514. *
  515. * @param IShare $share
  516. * @param bool $isOwner the user can either be the owner or the user who re-sahred it
  517. * @throws ShareNotFound
  518. * @throws \OCP\HintException
  519. */
  520. protected function revokeShare($share, $isOwner) {
  521. if ($this->userManager->userExists($share->getShareOwner()) && $this->userManager->userExists($share->getSharedBy())) {
  522. // If both the owner and the initiator of the share are local users we don't have to notify anybody else
  523. return;
  524. }
  525. // also send a unShare request to the initiator, if this is a different user than the owner
  526. if ($share->getShareOwner() !== $share->getSharedBy()) {
  527. if ($isOwner) {
  528. [, $remote] = $this->addressHandler->splitUserRemote($share->getSharedBy());
  529. } else {
  530. [, $remote] = $this->addressHandler->splitUserRemote($share->getShareOwner());
  531. }
  532. $remoteId = $this->getRemoteId($share);
  533. $this->notifications->sendRevokeShare($remote, $remoteId, $share->getToken());
  534. }
  535. }
  536. /**
  537. * remove share from table
  538. *
  539. * @param IShare $share
  540. */
  541. public function removeShareFromTable(IShare $share) {
  542. $this->removeShareFromTableById($share->getId());
  543. }
  544. /**
  545. * remove share from table
  546. *
  547. * @param string $shareId
  548. */
  549. private function removeShareFromTableById($shareId) {
  550. $qb = $this->dbConnection->getQueryBuilder();
  551. $qb->delete('share')
  552. ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)))
  553. ->andWhere($qb->expr()->neq('share_type', $qb->createNamedParameter(IShare::TYPE_CIRCLE)));
  554. $qb->execute();
  555. $qb = $this->dbConnection->getQueryBuilder();
  556. $qb->delete('federated_reshares')
  557. ->where($qb->expr()->eq('share_id', $qb->createNamedParameter($shareId)));
  558. $qb->execute();
  559. }
  560. /**
  561. * @inheritdoc
  562. */
  563. public function deleteFromSelf(IShare $share, $recipient) {
  564. // nothing to do here. Technically deleteFromSelf in the context of federated
  565. // shares is a umount of an external storage. This is handled here
  566. // apps/files_sharing/lib/external/manager.php
  567. // TODO move this code over to this app
  568. }
  569. public function restore(IShare $share, string $recipient): IShare {
  570. throw new GenericShareException('not implemented');
  571. }
  572. public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = true) {
  573. $qb = $this->dbConnection->getQueryBuilder();
  574. $qb->select('*')
  575. ->from('share', 's')
  576. ->andWhere($qb->expr()->orX(
  577. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  578. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  579. ))
  580. ->andWhere(
  581. $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_REMOTE))
  582. );
  583. /**
  584. * Reshares for this user are shares where they are the owner.
  585. */
  586. if ($reshares === false) {
  587. $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
  588. } else {
  589. $qb->andWhere(
  590. $qb->expr()->orX(
  591. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  592. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  593. )
  594. );
  595. }
  596. $qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
  597. if ($shallow) {
  598. $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
  599. } else {
  600. $qb->andWhere($qb->expr()->like('f.path', $qb->createNamedParameter($this->dbConnection->escapeLikeParameter($node->getInternalPath()) . '/%')));
  601. }
  602. $qb->orderBy('id');
  603. $cursor = $qb->execute();
  604. $shares = [];
  605. while ($data = $cursor->fetch()) {
  606. $shares[$data['fileid']][] = $this->createShareObject($data);
  607. }
  608. $cursor->closeCursor();
  609. return $shares;
  610. }
  611. /**
  612. * @inheritdoc
  613. */
  614. public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
  615. $qb = $this->dbConnection->getQueryBuilder();
  616. $qb->select('*')
  617. ->from('share');
  618. $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
  619. /**
  620. * Reshares for this user are shares where they are the owner.
  621. */
  622. if ($reshares === false) {
  623. //Special case for old shares created via the web UI
  624. $or1 = $qb->expr()->andX(
  625. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  626. $qb->expr()->isNull('uid_initiator')
  627. );
  628. $qb->andWhere(
  629. $qb->expr()->orX(
  630. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
  631. $or1
  632. )
  633. );
  634. } else {
  635. $qb->andWhere(
  636. $qb->expr()->orX(
  637. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  638. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  639. )
  640. );
  641. }
  642. if ($node !== null) {
  643. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  644. }
  645. if ($limit !== -1) {
  646. $qb->setMaxResults($limit);
  647. }
  648. $qb->setFirstResult($offset);
  649. $qb->orderBy('id');
  650. $cursor = $qb->execute();
  651. $shares = [];
  652. while ($data = $cursor->fetch()) {
  653. $shares[] = $this->createShareObject($data);
  654. }
  655. $cursor->closeCursor();
  656. return $shares;
  657. }
  658. /**
  659. * @inheritdoc
  660. */
  661. public function getShareById($id, $recipientId = null) {
  662. $qb = $this->dbConnection->getQueryBuilder();
  663. $qb->select('*')
  664. ->from('share')
  665. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  666. ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)));
  667. $cursor = $qb->execute();
  668. $data = $cursor->fetch();
  669. $cursor->closeCursor();
  670. if ($data === false) {
  671. throw new ShareNotFound('Can not find share with ID: ' . $id);
  672. }
  673. try {
  674. $share = $this->createShareObject($data);
  675. } catch (InvalidShare $e) {
  676. throw new ShareNotFound();
  677. }
  678. return $share;
  679. }
  680. /**
  681. * Get shares for a given path
  682. *
  683. * @param \OCP\Files\Node $path
  684. * @return IShare[]
  685. */
  686. public function getSharesByPath(Node $path) {
  687. $qb = $this->dbConnection->getQueryBuilder();
  688. // get federated user shares
  689. $cursor = $qb->select('*')
  690. ->from('share')
  691. ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
  692. ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
  693. ->execute();
  694. $shares = [];
  695. while ($data = $cursor->fetch()) {
  696. $shares[] = $this->createShareObject($data);
  697. }
  698. $cursor->closeCursor();
  699. return $shares;
  700. }
  701. /**
  702. * @inheritdoc
  703. */
  704. public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
  705. /** @var IShare[] $shares */
  706. $shares = [];
  707. //Get shares directly with this user
  708. $qb = $this->dbConnection->getQueryBuilder();
  709. $qb->select('*')
  710. ->from('share');
  711. // Order by id
  712. $qb->orderBy('id');
  713. // Set limit and offset
  714. if ($limit !== -1) {
  715. $qb->setMaxResults($limit);
  716. }
  717. $qb->setFirstResult($offset);
  718. $qb->where($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)));
  719. $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
  720. // Filter by node if provided
  721. if ($node !== null) {
  722. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  723. }
  724. $cursor = $qb->execute();
  725. while ($data = $cursor->fetch()) {
  726. $shares[] = $this->createShareObject($data);
  727. }
  728. $cursor->closeCursor();
  729. return $shares;
  730. }
  731. /**
  732. * Get a share by token
  733. *
  734. * @param string $token
  735. * @return IShare
  736. * @throws ShareNotFound
  737. */
  738. public function getShareByToken($token) {
  739. $qb = $this->dbConnection->getQueryBuilder();
  740. $cursor = $qb->select('*')
  741. ->from('share')
  742. ->where($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
  743. ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
  744. ->execute();
  745. $data = $cursor->fetch();
  746. if ($data === false) {
  747. throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
  748. }
  749. try {
  750. $share = $this->createShareObject($data);
  751. } catch (InvalidShare $e) {
  752. throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
  753. }
  754. return $share;
  755. }
  756. /**
  757. * get database row of a give share
  758. *
  759. * @param $id
  760. * @return array
  761. * @throws ShareNotFound
  762. */
  763. private function getRawShare($id) {
  764. // Now fetch the inserted share and create a complete share object
  765. $qb = $this->dbConnection->getQueryBuilder();
  766. $qb->select('*')
  767. ->from('share')
  768. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  769. $cursor = $qb->execute();
  770. $data = $cursor->fetch();
  771. $cursor->closeCursor();
  772. if ($data === false) {
  773. throw new ShareNotFound;
  774. }
  775. return $data;
  776. }
  777. /**
  778. * Create a share object from an database row
  779. *
  780. * @param array $data
  781. * @return IShare
  782. * @throws InvalidShare
  783. * @throws ShareNotFound
  784. */
  785. private function createShareObject($data) {
  786. $share = new Share($this->rootFolder, $this->userManager);
  787. $share->setId((int)$data['id'])
  788. ->setShareType((int)$data['share_type'])
  789. ->setPermissions((int)$data['permissions'])
  790. ->setTarget($data['file_target'])
  791. ->setMailSend((bool)$data['mail_send'])
  792. ->setToken($data['token']);
  793. $shareTime = new \DateTime();
  794. $shareTime->setTimestamp((int)$data['stime']);
  795. $share->setShareTime($shareTime);
  796. $share->setSharedWith($data['share_with']);
  797. if ($data['uid_initiator'] !== null) {
  798. $share->setShareOwner($data['uid_owner']);
  799. $share->setSharedBy($data['uid_initiator']);
  800. } else {
  801. //OLD SHARE
  802. $share->setSharedBy($data['uid_owner']);
  803. $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
  804. $owner = $path->getOwner();
  805. $share->setShareOwner($owner->getUID());
  806. }
  807. $share->setNodeId((int)$data['file_source']);
  808. $share->setNodeType($data['item_type']);
  809. $share->setProviderId($this->identifier());
  810. if ($data['expiration'] !== null) {
  811. $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
  812. $share->setExpirationDate($expiration);
  813. }
  814. return $share;
  815. }
  816. /**
  817. * Get the node with file $id for $user
  818. *
  819. * @param string $userId
  820. * @param int $id
  821. * @return \OCP\Files\File|\OCP\Files\Folder
  822. * @throws InvalidShare
  823. */
  824. private function getNode($userId, $id) {
  825. try {
  826. $userFolder = $this->rootFolder->getUserFolder($userId);
  827. } catch (NotFoundException $e) {
  828. throw new InvalidShare();
  829. }
  830. $nodes = $userFolder->getById($id);
  831. if (empty($nodes)) {
  832. throw new InvalidShare();
  833. }
  834. return $nodes[0];
  835. }
  836. /**
  837. * A user is deleted from the system
  838. * So clean up the relevant shares.
  839. *
  840. * @param string $uid
  841. * @param int $shareType
  842. */
  843. public function userDeleted($uid, $shareType) {
  844. //TODO: probably a good idea to send unshare info to remote servers
  845. $qb = $this->dbConnection->getQueryBuilder();
  846. $qb->delete('share')
  847. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_REMOTE)))
  848. ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
  849. ->execute();
  850. }
  851. /**
  852. * This provider does not handle groups
  853. *
  854. * @param string $gid
  855. */
  856. public function groupDeleted($gid) {
  857. // We don't handle groups here
  858. }
  859. /**
  860. * This provider does not handle groups
  861. *
  862. * @param string $uid
  863. * @param string $gid
  864. */
  865. public function userDeletedFromGroup($uid, $gid) {
  866. // We don't handle groups here
  867. }
  868. /**
  869. * check if users from other Nextcloud instances are allowed to mount public links share by this instance
  870. *
  871. * @return bool
  872. */
  873. public function isOutgoingServer2serverShareEnabled() {
  874. if ($this->gsConfig->onlyInternalFederation()) {
  875. return false;
  876. }
  877. $result = $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes');
  878. return ($result === 'yes');
  879. }
  880. /**
  881. * check if users are allowed to mount public links from other Nextclouds
  882. *
  883. * @return bool
  884. */
  885. public function isIncomingServer2serverShareEnabled() {
  886. if ($this->gsConfig->onlyInternalFederation()) {
  887. return false;
  888. }
  889. $result = $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes');
  890. return ($result === 'yes');
  891. }
  892. /**
  893. * check if users from other Nextcloud instances are allowed to send federated group shares
  894. *
  895. * @return bool
  896. */
  897. public function isOutgoingServer2serverGroupShareEnabled() {
  898. if ($this->gsConfig->onlyInternalFederation()) {
  899. return false;
  900. }
  901. $result = $this->config->getAppValue('files_sharing', 'outgoing_server2server_group_share_enabled', 'no');
  902. return ($result === 'yes');
  903. }
  904. /**
  905. * check if users are allowed to receive federated group shares
  906. *
  907. * @return bool
  908. */
  909. public function isIncomingServer2serverGroupShareEnabled() {
  910. if ($this->gsConfig->onlyInternalFederation()) {
  911. return false;
  912. }
  913. $result = $this->config->getAppValue('files_sharing', 'incoming_server2server_group_share_enabled', 'no');
  914. return ($result === 'yes');
  915. }
  916. /**
  917. * check if federated group sharing is supported, therefore the OCM API need to be enabled
  918. *
  919. * @return bool
  920. */
  921. public function isFederatedGroupSharingSupported() {
  922. return $this->cloudFederationProviderManager->isReady();
  923. }
  924. /**
  925. * Check if querying sharees on the lookup server is enabled
  926. *
  927. * @return bool
  928. */
  929. public function isLookupServerQueriesEnabled() {
  930. // in a global scale setup we should always query the lookup server
  931. if ($this->gsConfig->isGlobalScaleEnabled()) {
  932. return true;
  933. }
  934. $result = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'yes');
  935. return ($result === 'yes');
  936. }
  937. /**
  938. * Check if it is allowed to publish user specific data to the lookup server
  939. *
  940. * @return bool
  941. */
  942. public function isLookupServerUploadEnabled() {
  943. // in a global scale setup the admin is responsible to keep the lookup server up-to-date
  944. if ($this->gsConfig->isGlobalScaleEnabled()) {
  945. return false;
  946. }
  947. $result = $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes');
  948. return ($result === 'yes');
  949. }
  950. /**
  951. * @inheritdoc
  952. */
  953. public function getAccessList($nodes, $currentAccess) {
  954. $ids = [];
  955. foreach ($nodes as $node) {
  956. $ids[] = $node->getId();
  957. }
  958. $qb = $this->dbConnection->getQueryBuilder();
  959. $qb->select('share_with', 'token', 'file_source')
  960. ->from('share')
  961. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_REMOTE)))
  962. ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
  963. ->andWhere($qb->expr()->orX(
  964. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  965. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  966. ));
  967. $cursor = $qb->execute();
  968. if ($currentAccess === false) {
  969. $remote = $cursor->fetch() !== false;
  970. $cursor->closeCursor();
  971. return ['remote' => $remote];
  972. }
  973. $remote = [];
  974. while ($row = $cursor->fetch()) {
  975. $remote[$row['share_with']] = [
  976. 'node_id' => $row['file_source'],
  977. 'token' => $row['token'],
  978. ];
  979. }
  980. $cursor->closeCursor();
  981. return ['remote' => $remote];
  982. }
  983. public function getAllShares(): iterable {
  984. $qb = $this->dbConnection->getQueryBuilder();
  985. $qb->select('*')
  986. ->from('share')
  987. ->where(
  988. $qb->expr()->orX(
  989. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_REMOTE)),
  990. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_REMOTE_GROUP))
  991. )
  992. );
  993. $cursor = $qb->execute();
  994. while ($data = $cursor->fetch()) {
  995. try {
  996. $share = $this->createShareObject($data);
  997. } catch (InvalidShare $e) {
  998. continue;
  999. } catch (ShareNotFound $e) {
  1000. continue;
  1001. }
  1002. yield $share;
  1003. }
  1004. $cursor->closeCursor();
  1005. }
  1006. }