TrustedServers.php 6.9 KB

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