1
0

TrustedServers.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Federation;
  8. use OCA\Federation\BackgroundJob\RequestSharedSecret;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\BackgroundJob\IJobList;
  12. use OCP\DB\Exception;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\Federation\Events\TrustedServerRemovedEvent;
  15. use OCP\HintException;
  16. use OCP\Http\Client\IClientService;
  17. use OCP\IConfig;
  18. use OCP\Security\ISecureRandom;
  19. use Psr\Log\LoggerInterface;
  20. class TrustedServers {
  21. /** after a user list was exchanged at least once successfully */
  22. public const STATUS_OK = 1;
  23. /** waiting for shared secret or initial user list exchange */
  24. public const STATUS_PENDING = 2;
  25. /** something went wrong, misconfigured server, software bug,... user interaction needed */
  26. public const STATUS_FAILURE = 3;
  27. /** remote server revoked access */
  28. public const STATUS_ACCESS_REVOKED = 4;
  29. /** @var list<array{id: int, url: string, url_hash: string, shared_secret: ?string, status: int, sync_token: ?string}>|null */
  30. private ?array $trustedServersCache = null;
  31. public function __construct(
  32. private DbHandler $dbHandler,
  33. private IClientService $httpClientService,
  34. private LoggerInterface $logger,
  35. private IJobList $jobList,
  36. private ISecureRandom $secureRandom,
  37. private IConfig $config,
  38. private IEventDispatcher $dispatcher,
  39. private ITimeFactory $timeFactory,
  40. ) {
  41. }
  42. /**
  43. * Add server to the list of trusted servers
  44. */
  45. public function addServer(string $url): int {
  46. $url = $this->updateProtocol($url);
  47. $result = $this->dbHandler->addServer($url);
  48. if ($result) {
  49. $token = $this->secureRandom->generate(16);
  50. $this->dbHandler->addToken($url, $token);
  51. $this->jobList->add(
  52. RequestSharedSecret::class,
  53. [
  54. 'url' => $url,
  55. 'token' => $token,
  56. 'created' => $this->timeFactory->getTime()
  57. ]
  58. );
  59. }
  60. return $result;
  61. }
  62. /**
  63. * Get shared secret for the given server
  64. */
  65. public function getSharedSecret(string $url): string {
  66. return $this->dbHandler->getSharedSecret($url);
  67. }
  68. /**
  69. * Add shared secret for the given server
  70. */
  71. public function addSharedSecret(string $url, string $sharedSecret): void {
  72. $this->dbHandler->addSharedSecret($url, $sharedSecret);
  73. }
  74. /**
  75. * Remove server from the list of trusted servers
  76. */
  77. public function removeServer(int $id): void {
  78. $server = $this->dbHandler->getServerById($id);
  79. $this->dbHandler->removeServer($id);
  80. $this->dispatcher->dispatchTyped(new TrustedServerRemovedEvent($server['url_hash']));
  81. }
  82. /**
  83. * Get all trusted servers
  84. *
  85. * @return list<array{id: int, url: string, url_hash: string, shared_secret: ?string, status: int, sync_token: ?string}>
  86. * @throws Exception
  87. */
  88. public function getServers() {
  89. if ($this->trustedServersCache === null) {
  90. $this->trustedServersCache = $this->dbHandler->getAllServer();
  91. }
  92. return $this->trustedServersCache;
  93. }
  94. /**
  95. * Check if given server is a trusted Nextcloud server
  96. */
  97. public function isTrustedServer(string $url): bool {
  98. return $this->dbHandler->serverExists($url);
  99. }
  100. /**
  101. * Set server status
  102. */
  103. public function setServerStatus(string $url, int $status): void {
  104. $this->dbHandler->setServerStatus($url, $status);
  105. }
  106. /**
  107. * Get server status
  108. */
  109. public function getServerStatus(string $url): int {
  110. return $this->dbHandler->getServerStatus($url);
  111. }
  112. /**
  113. * Check if URL point to a ownCloud/Nextcloud server
  114. */
  115. public function isNextcloudServer(string $url): bool {
  116. $isValidNextcloud = false;
  117. $client = $this->httpClientService->newClient();
  118. try {
  119. $result = $client->get(
  120. $url . '/status.php',
  121. [
  122. 'timeout' => 3,
  123. 'connect_timeout' => 3,
  124. ]
  125. );
  126. if ($result->getStatusCode() === Http::STATUS_OK) {
  127. $body = $result->getBody();
  128. if (is_resource($body)) {
  129. $body = stream_get_contents($body) ?: '';
  130. }
  131. $isValidNextcloud = $this->checkNextcloudVersion($body);
  132. }
  133. } catch (\Exception $e) {
  134. $this->logger->error('No Nextcloud server.', [
  135. 'app' => 'federation',
  136. 'exception' => $e,
  137. ]);
  138. return false;
  139. }
  140. return $isValidNextcloud;
  141. }
  142. /**
  143. * Check if ownCloud/Nextcloud version is >= 9.0
  144. * @throws HintException
  145. */
  146. protected function checkNextcloudVersion(string $status): bool {
  147. $decoded = json_decode($status, true);
  148. if (!empty($decoded) && isset($decoded['version'])) {
  149. if (!version_compare($decoded['version'], '9.0.0', '>=')) {
  150. throw new HintException('Remote server version is too low. 9.0 is required.');
  151. }
  152. return true;
  153. }
  154. return false;
  155. }
  156. /**
  157. * Check if the URL contain a protocol, if not add https
  158. */
  159. protected function updateProtocol(string $url): string {
  160. if (
  161. strpos($url, 'https://') === 0
  162. || strpos($url, 'http://') === 0
  163. ) {
  164. return $url;
  165. }
  166. return 'https://' . $url;
  167. }
  168. }