trustedservers.php 5.7 KB

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