Storage.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\Files_Sharing\External;
  9. use GuzzleHttp\Exception\ClientException;
  10. use GuzzleHttp\Exception\ConnectException;
  11. use GuzzleHttp\Exception\RequestException;
  12. use OC\Files\Storage\DAV;
  13. use OC\ForbiddenException;
  14. use OCA\Files_Sharing\External\Manager as ExternalShareManager;
  15. use OCA\Files_Sharing\ISharedStorage;
  16. use OCP\AppFramework\Http;
  17. use OCP\Constants;
  18. use OCP\Federation\ICloudId;
  19. use OCP\Files\NotFoundException;
  20. use OCP\Files\Storage\IDisableEncryptionStorage;
  21. use OCP\Files\Storage\IReliableEtagStorage;
  22. use OCP\Files\StorageInvalidException;
  23. use OCP\Files\StorageNotAvailableException;
  24. use OCP\Http\Client\IClientService;
  25. use OCP\Http\Client\LocalServerException;
  26. use OCP\ICacheFactory;
  27. use OCP\IConfig;
  28. use OCP\OCM\Exceptions\OCMArgumentException;
  29. use OCP\OCM\Exceptions\OCMProviderException;
  30. use OCP\OCM\IOCMDiscoveryService;
  31. use OCP\Server;
  32. use Psr\Log\LoggerInterface;
  33. class Storage extends DAV implements ISharedStorage, IDisableEncryptionStorage, IReliableEtagStorage {
  34. private ICloudId $cloudId;
  35. private string $mountPoint;
  36. private string $token;
  37. private ICacheFactory $memcacheFactory;
  38. private IClientService $httpClient;
  39. private bool $updateChecked = false;
  40. private ExternalShareManager $manager;
  41. private IConfig $config;
  42. /**
  43. * @param array{HttpClientService: IClientService, manager: ExternalShareManager, cloudId: ICloudId, mountpoint: string, token: string, password: ?string}|array $options
  44. */
  45. public function __construct($options) {
  46. $this->memcacheFactory = \OC::$server->getMemCacheFactory();
  47. $this->httpClient = $options['HttpClientService'];
  48. $this->manager = $options['manager'];
  49. $this->cloudId = $options['cloudId'];
  50. $this->logger = Server::get(LoggerInterface::class);
  51. $discoveryService = Server::get(IOCMDiscoveryService::class);
  52. $this->config = Server::get(IConfig::class);
  53. // use default path to webdav if not found on discovery
  54. try {
  55. $ocmProvider = $discoveryService->discover($this->cloudId->getRemote());
  56. $webDavEndpoint = $ocmProvider->extractProtocolEntry('file', 'webdav');
  57. $remote = $ocmProvider->getEndPoint();
  58. } catch (OCMProviderException|OCMArgumentException $e) {
  59. $this->logger->notice('exception while retrieving webdav endpoint', ['exception' => $e]);
  60. $webDavEndpoint = '/public.php/webdav';
  61. $remote = $this->cloudId->getRemote();
  62. }
  63. $host = parse_url($remote, PHP_URL_HOST);
  64. $port = parse_url($remote, PHP_URL_PORT);
  65. $host .= ($port === null) ? '' : ':' . $port; // we add port if available
  66. // in case remote NC is on a sub folder and using deprecated ocm provider
  67. $tmpPath = rtrim(parse_url($this->cloudId->getRemote(), PHP_URL_PATH) ?? '', '/');
  68. if (!str_starts_with($webDavEndpoint, $tmpPath)) {
  69. $webDavEndpoint = $tmpPath . $webDavEndpoint;
  70. }
  71. $this->mountPoint = $options['mountpoint'];
  72. $this->token = $options['token'];
  73. parent::__construct(
  74. [
  75. 'secure' => ((parse_url($remote, PHP_URL_SCHEME) ?? 'https') === 'https'),
  76. 'host' => $host,
  77. 'root' => $webDavEndpoint,
  78. 'user' => $options['token'],
  79. 'authType' => \Sabre\DAV\Client::AUTH_BASIC,
  80. 'password' => (string)$options['password']
  81. ]
  82. );
  83. }
  84. public function getWatcher($path = '', $storage = null) {
  85. if (!$storage) {
  86. $storage = $this;
  87. }
  88. if (!isset($this->watcher)) {
  89. $this->watcher = new Watcher($storage);
  90. $this->watcher->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE);
  91. }
  92. return $this->watcher;
  93. }
  94. public function getRemoteUser(): string {
  95. return $this->cloudId->getUser();
  96. }
  97. public function getRemote(): string {
  98. return $this->cloudId->getRemote();
  99. }
  100. public function getMountPoint(): string {
  101. return $this->mountPoint;
  102. }
  103. public function getToken(): string {
  104. return $this->token;
  105. }
  106. public function getPassword(): ?string {
  107. return $this->password;
  108. }
  109. /**
  110. * Get id of the mount point.
  111. * @return string
  112. */
  113. public function getId() {
  114. return 'shared::' . md5($this->token . '@' . $this->getRemote());
  115. }
  116. public function getCache($path = '', $storage = null) {
  117. if (is_null($this->cache)) {
  118. $this->cache = new Cache($this, $this->cloudId);
  119. }
  120. return $this->cache;
  121. }
  122. /**
  123. * @param string $path
  124. * @param \OC\Files\Storage\Storage $storage
  125. * @return \OCA\Files_Sharing\External\Scanner
  126. */
  127. public function getScanner($path = '', $storage = null) {
  128. if (!$storage) {
  129. $storage = $this;
  130. }
  131. if (!isset($this->scanner)) {
  132. $this->scanner = new Scanner($storage);
  133. }
  134. return $this->scanner;
  135. }
  136. /**
  137. * Check if a file or folder has been updated since $time
  138. *
  139. * @param string $path
  140. * @param int $time
  141. * @throws \OCP\Files\StorageNotAvailableException
  142. * @throws \OCP\Files\StorageInvalidException
  143. * @return bool
  144. */
  145. public function hasUpdated($path, $time) {
  146. // since for owncloud webdav servers we can rely on etag propagation we only need to check the root of the storage
  147. // because of that we only do one check for the entire storage per request
  148. if ($this->updateChecked) {
  149. return false;
  150. }
  151. $this->updateChecked = true;
  152. try {
  153. return parent::hasUpdated('', $time);
  154. } catch (StorageInvalidException $e) {
  155. // check if it needs to be removed
  156. $this->checkStorageAvailability();
  157. throw $e;
  158. } catch (StorageNotAvailableException $e) {
  159. // check if it needs to be removed or just temp unavailable
  160. $this->checkStorageAvailability();
  161. throw $e;
  162. }
  163. }
  164. public function test() {
  165. try {
  166. return parent::test();
  167. } catch (StorageInvalidException $e) {
  168. // check if it needs to be removed
  169. $this->checkStorageAvailability();
  170. throw $e;
  171. } catch (StorageNotAvailableException $e) {
  172. // check if it needs to be removed or just temp unavailable
  173. $this->checkStorageAvailability();
  174. throw $e;
  175. }
  176. }
  177. /**
  178. * Check whether this storage is permanently or temporarily
  179. * unavailable
  180. *
  181. * @throws \OCP\Files\StorageNotAvailableException
  182. * @throws \OCP\Files\StorageInvalidException
  183. */
  184. public function checkStorageAvailability() {
  185. // see if we can find out why the share is unavailable
  186. try {
  187. $this->getShareInfo(0);
  188. } catch (NotFoundException $e) {
  189. // a 404 can either mean that the share no longer exists or there is no Nextcloud on the remote
  190. if ($this->testRemote()) {
  191. // valid Nextcloud instance means that the public share no longer exists
  192. // since this is permanent (re-sharing the file will create a new token)
  193. // we remove the invalid storage
  194. $this->manager->removeShare($this->mountPoint);
  195. $this->manager->getMountManager()->removeMount($this->mountPoint);
  196. throw new StorageInvalidException("Remote share not found", 0, $e);
  197. } else {
  198. // Nextcloud instance is gone, likely to be a temporary server configuration error
  199. throw new StorageNotAvailableException("No nextcloud instance found at remote", 0, $e);
  200. }
  201. } catch (ForbiddenException $e) {
  202. // auth error, remove share for now (provide a dialog in the future)
  203. $this->manager->removeShare($this->mountPoint);
  204. $this->manager->getMountManager()->removeMount($this->mountPoint);
  205. throw new StorageInvalidException("Auth error when getting remote share");
  206. } catch (\GuzzleHttp\Exception\ConnectException $e) {
  207. throw new StorageNotAvailableException("Failed to connect to remote instance", 0, $e);
  208. } catch (\GuzzleHttp\Exception\RequestException $e) {
  209. throw new StorageNotAvailableException("Error while sending request to remote instance", 0, $e);
  210. }
  211. }
  212. public function file_exists($path) {
  213. if ($path === '') {
  214. return true;
  215. } else {
  216. return parent::file_exists($path);
  217. }
  218. }
  219. /**
  220. * Check if the configured remote is a valid federated share provider
  221. *
  222. * @return bool
  223. */
  224. protected function testRemote(): bool {
  225. try {
  226. return $this->testRemoteUrl($this->getRemote() . '/ocm-provider/index.php')
  227. || $this->testRemoteUrl($this->getRemote() . '/ocm-provider/')
  228. || $this->testRemoteUrl($this->getRemote() . '/status.php');
  229. } catch (\Exception $e) {
  230. return false;
  231. }
  232. }
  233. private function testRemoteUrl(string $url): bool {
  234. $cache = $this->memcacheFactory->createDistributed('files_sharing_remote_url');
  235. $cached = $cache->get($url);
  236. if ($cached !== null) {
  237. return (bool)$cached;
  238. }
  239. $client = $this->httpClient->newClient();
  240. try {
  241. $result = $client->get($url, [
  242. 'timeout' => 10,
  243. 'connect_timeout' => 10,
  244. 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates', false),
  245. ])->getBody();
  246. $data = json_decode($result);
  247. $returnValue = (is_object($data) && !empty($data->version));
  248. } catch (ConnectException $e) {
  249. $returnValue = false;
  250. } catch (ClientException $e) {
  251. $returnValue = false;
  252. } catch (RequestException $e) {
  253. $returnValue = false;
  254. }
  255. $cache->set($url, $returnValue, 60 * 60 * 24);
  256. return $returnValue;
  257. }
  258. /**
  259. * Check whether the remote is an ownCloud/Nextcloud. This is needed since some sharing
  260. * features are not standardized.
  261. *
  262. * @throws LocalServerException
  263. */
  264. public function remoteIsOwnCloud(): bool {
  265. if (defined('PHPUNIT_RUN') || !$this->testRemoteUrl($this->getRemote() . '/status.php')) {
  266. return false;
  267. }
  268. return true;
  269. }
  270. /**
  271. * @return mixed
  272. * @throws ForbiddenException
  273. * @throws NotFoundException
  274. * @throws \Exception
  275. */
  276. public function getShareInfo(int $depth = -1) {
  277. $remote = $this->getRemote();
  278. $token = $this->getToken();
  279. $password = $this->getPassword();
  280. try {
  281. // If remote is not an ownCloud do not try to get any share info
  282. if (!$this->remoteIsOwnCloud()) {
  283. return ['status' => 'unsupported'];
  284. }
  285. } catch (LocalServerException $e) {
  286. // throw this to be on the safe side: the share will still be visible
  287. // in the UI in case the failure is intermittent, and the user will
  288. // be able to decide whether to remove it if it's really gone
  289. throw new StorageNotAvailableException();
  290. }
  291. $url = rtrim($remote, '/') . '/index.php/apps/files_sharing/shareinfo?t=' . $token;
  292. // TODO: DI
  293. $client = \OC::$server->getHTTPClientService()->newClient();
  294. try {
  295. $response = $client->post($url, [
  296. 'body' => ['password' => $password, 'depth' => $depth],
  297. 'timeout' => 10,
  298. 'connect_timeout' => 10,
  299. ]);
  300. } catch (\GuzzleHttp\Exception\RequestException $e) {
  301. if ($e->getCode() === Http::STATUS_UNAUTHORIZED || $e->getCode() === Http::STATUS_FORBIDDEN) {
  302. throw new ForbiddenException();
  303. }
  304. if ($e->getCode() === Http::STATUS_NOT_FOUND) {
  305. throw new NotFoundException();
  306. }
  307. // throw this to be on the safe side: the share will still be visible
  308. // in the UI in case the failure is intermittent, and the user will
  309. // be able to decide whether to remove it if it's really gone
  310. throw new StorageNotAvailableException();
  311. }
  312. return json_decode($response->getBody(), true);
  313. }
  314. public function getOwner($path) {
  315. return $this->cloudId->getDisplayId();
  316. }
  317. public function isSharable($path): bool {
  318. if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) {
  319. return false;
  320. }
  321. return (bool)($this->getPermissions($path) & Constants::PERMISSION_SHARE);
  322. }
  323. public function getPermissions($path): int {
  324. $response = $this->propfind($path);
  325. if ($response === false) {
  326. return 0;
  327. }
  328. $ocsPermissions = $response['{http://open-collaboration-services.org/ns}share-permissions'] ?? null;
  329. $ocmPermissions = $response['{http://open-cloud-mesh.org/ns}share-permissions'] ?? null;
  330. $ocPermissions = $response['{http://owncloud.org/ns}permissions'] ?? null;
  331. // old federated sharing permissions
  332. if ($ocsPermissions !== null) {
  333. $permissions = (int)$ocsPermissions;
  334. } elseif ($ocmPermissions !== null) {
  335. // permissions provided by the OCM API
  336. $permissions = $this->ocmPermissions2ncPermissions($ocmPermissions, $path);
  337. } elseif ($ocPermissions !== null) {
  338. return $this->parsePermissions($ocPermissions);
  339. } else {
  340. // use default permission if remote server doesn't provide the share permissions
  341. $permissions = $this->getDefaultPermissions($path);
  342. }
  343. return $permissions;
  344. }
  345. public function needsPartFile() {
  346. return false;
  347. }
  348. /**
  349. * Translate OCM Permissions to Nextcloud permissions
  350. *
  351. * @param string $ocmPermissions json encoded OCM permissions
  352. * @param string $path path to file
  353. * @return int
  354. */
  355. protected function ocmPermissions2ncPermissions(string $ocmPermissions, string $path): int {
  356. try {
  357. $ocmPermissions = json_decode($ocmPermissions);
  358. $ncPermissions = 0;
  359. foreach ($ocmPermissions as $permission) {
  360. switch (strtolower($permission)) {
  361. case 'read':
  362. $ncPermissions += Constants::PERMISSION_READ;
  363. break;
  364. case 'write':
  365. $ncPermissions += Constants::PERMISSION_CREATE + Constants::PERMISSION_UPDATE;
  366. break;
  367. case 'share':
  368. $ncPermissions += Constants::PERMISSION_SHARE;
  369. break;
  370. default:
  371. throw new \Exception();
  372. }
  373. }
  374. } catch (\Exception $e) {
  375. $ncPermissions = $this->getDefaultPermissions($path);
  376. }
  377. return $ncPermissions;
  378. }
  379. /**
  380. * Calculate the default permissions in case no permissions are provided
  381. */
  382. protected function getDefaultPermissions(string $path): int {
  383. if ($this->is_dir($path)) {
  384. $permissions = Constants::PERMISSION_ALL;
  385. } else {
  386. $permissions = Constants::PERMISSION_ALL & ~Constants::PERMISSION_CREATE;
  387. }
  388. return $permissions;
  389. }
  390. public function free_space($path) {
  391. return parent::free_space("");
  392. }
  393. }