TrustedServers.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  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 Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Federation;
  29. use OC\HintException;
  30. use OCA\Federation\BackgroundJob\RequestSharedSecret;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\Utility\ITimeFactory;
  33. use OCP\BackgroundJob\IJobList;
  34. use OCP\Http\Client\IClientService;
  35. use OCP\IConfig;
  36. use OCP\ILogger;
  37. use OCP\Security\ISecureRandom;
  38. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  39. use Symfony\Component\EventDispatcher\GenericEvent;
  40. class TrustedServers {
  41. /** after a user list was exchanged at least once successfully */
  42. public const STATUS_OK = 1;
  43. /** waiting for shared secret or initial user list exchange */
  44. public const STATUS_PENDING = 2;
  45. /** something went wrong, misconfigured server, software bug,... user interaction needed */
  46. public const STATUS_FAILURE = 3;
  47. /** remote server revoked access */
  48. public const STATUS_ACCESS_REVOKED = 4;
  49. /** @var dbHandler */
  50. private $dbHandler;
  51. /** @var IClientService */
  52. private $httpClientService;
  53. /** @var ILogger */
  54. private $logger;
  55. /** @var IJobList */
  56. private $jobList;
  57. /** @var ISecureRandom */
  58. private $secureRandom;
  59. /** @var IConfig */
  60. private $config;
  61. /** @var EventDispatcherInterface */
  62. private $dispatcher;
  63. /** @var ITimeFactory */
  64. private $timeFactory;
  65. /**
  66. * @param DbHandler $dbHandler
  67. * @param IClientService $httpClientService
  68. * @param ILogger $logger
  69. * @param IJobList $jobList
  70. * @param ISecureRandom $secureRandom
  71. * @param IConfig $config
  72. * @param EventDispatcherInterface $dispatcher
  73. * @param ITimeFactory $timeFactory
  74. */
  75. public function __construct(
  76. DbHandler $dbHandler,
  77. IClientService $httpClientService,
  78. ILogger $logger,
  79. IJobList $jobList,
  80. ISecureRandom $secureRandom,
  81. IConfig $config,
  82. EventDispatcherInterface $dispatcher,
  83. ITimeFactory $timeFactory
  84. ) {
  85. $this->dbHandler = $dbHandler;
  86. $this->httpClientService = $httpClientService;
  87. $this->logger = $logger;
  88. $this->jobList = $jobList;
  89. $this->secureRandom = $secureRandom;
  90. $this->config = $config;
  91. $this->dispatcher = $dispatcher;
  92. $this->timeFactory = $timeFactory;
  93. }
  94. /**
  95. * add server to the list of trusted servers
  96. *
  97. * @param $url
  98. * @return int server id
  99. */
  100. public function addServer($url) {
  101. $url = $this->updateProtocol($url);
  102. $result = $this->dbHandler->addServer($url);
  103. if ($result) {
  104. $token = $this->secureRandom->generate(16);
  105. $this->dbHandler->addToken($url, $token);
  106. $this->jobList->add(
  107. RequestSharedSecret::class,
  108. [
  109. 'url' => $url,
  110. 'token' => $token,
  111. 'created' => $this->timeFactory->getTime()
  112. ]
  113. );
  114. }
  115. return $result;
  116. }
  117. /**
  118. * enable/disable to automatically add servers to the list of trusted servers
  119. * once a federated share was created and accepted successfully
  120. *
  121. * @param bool $status
  122. */
  123. public function setAutoAddServers($status) {
  124. $value = $status ? '1' : '0';
  125. $this->config->setAppValue('federation', 'autoAddServers', $value);
  126. }
  127. /**
  128. * return if we automatically add servers to the list of trusted servers
  129. * once a federated share was created and accepted successfully
  130. *
  131. * @return bool
  132. */
  133. public function getAutoAddServers() {
  134. $value = $this->config->getAppValue('federation', 'autoAddServers', '0');
  135. return $value === '1';
  136. }
  137. /**
  138. * get shared secret for the given server
  139. *
  140. * @param string $url
  141. * @return string
  142. */
  143. public function getSharedSecret($url) {
  144. return $this->dbHandler->getSharedSecret($url);
  145. }
  146. /**
  147. * add shared secret for the given server
  148. *
  149. * @param string $url
  150. * @param $sharedSecret
  151. */
  152. public function addSharedSecret($url, $sharedSecret) {
  153. $this->dbHandler->addSharedSecret($url, $sharedSecret);
  154. }
  155. /**
  156. * remove server from the list of trusted servers
  157. *
  158. * @param int $id
  159. */
  160. public function removeServer($id) {
  161. $server = $this->dbHandler->getServerById($id);
  162. $this->dbHandler->removeServer($id);
  163. $event = new GenericEvent($server['url_hash']);
  164. $this->dispatcher->dispatch('OCP\Federation\TrustedServerEvent::remove', $event);
  165. }
  166. /**
  167. * get all trusted servers
  168. *
  169. * @return array
  170. */
  171. public function getServers() {
  172. return $this->dbHandler->getAllServer();
  173. }
  174. /**
  175. * check if given server is a trusted Nextcloud server
  176. *
  177. * @param string $url
  178. * @return bool
  179. */
  180. public function isTrustedServer($url) {
  181. return $this->dbHandler->serverExists($url);
  182. }
  183. /**
  184. * set server status
  185. *
  186. * @param string $url
  187. * @param int $status
  188. */
  189. public function setServerStatus($url, $status) {
  190. $this->dbHandler->setServerStatus($url, $status);
  191. }
  192. /**
  193. * @param string $url
  194. * @return int
  195. */
  196. public function getServerStatus($url) {
  197. return $this->dbHandler->getServerStatus($url);
  198. }
  199. /**
  200. * check if URL point to a ownCloud/Nextcloud server
  201. *
  202. * @param string $url
  203. * @return bool
  204. */
  205. public function isOwnCloudServer($url) {
  206. $isValidOwnCloud = false;
  207. $client = $this->httpClientService->newClient();
  208. try {
  209. $result = $client->get(
  210. $url . '/status.php',
  211. [
  212. 'timeout' => 3,
  213. 'connect_timeout' => 3,
  214. ]
  215. );
  216. if ($result->getStatusCode() === Http::STATUS_OK) {
  217. $isValidOwnCloud = $this->checkOwnCloudVersion($result->getBody());
  218. }
  219. } catch (\Exception $e) {
  220. \OC::$server->getLogger()->logException($e, [
  221. 'message' => 'No Nextcloud server.',
  222. 'level' => ILogger::DEBUG,
  223. 'app' => 'federation',
  224. ]);
  225. return false;
  226. }
  227. return $isValidOwnCloud;
  228. }
  229. /**
  230. * check if ownCloud version is >= 9.0
  231. *
  232. * @param $status
  233. * @return bool
  234. * @throws HintException
  235. */
  236. protected function checkOwnCloudVersion($status) {
  237. $decoded = json_decode($status, true);
  238. if (!empty($decoded) && isset($decoded['version'])) {
  239. if (!version_compare($decoded['version'], '9.0.0', '>=')) {
  240. throw new HintException('Remote server version is too low. 9.0 is required.');
  241. }
  242. return true;
  243. }
  244. return false;
  245. }
  246. /**
  247. * check if the URL contain a protocol, if not add https
  248. *
  249. * @param string $url
  250. * @return string
  251. */
  252. protected function updateProtocol($url) {
  253. if (
  254. strpos($url, 'https://') === 0
  255. || strpos($url, 'http://') === 0
  256. ) {
  257. return $url;
  258. }
  259. return 'https://' . $url;
  260. }
  261. }