Storage.php 13 KB

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