Storage.php 14 KB

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