Storage.php 14 KB

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