CheckSetupController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 OC\Settings\Controller;
  27. use GuzzleHttp\Exception\ClientException;
  28. use OC\AppFramework\Http;
  29. use OC\IntegrityCheck\Checker;
  30. use OCP\AppFramework\Controller;
  31. use OCP\AppFramework\Http\DataDisplayResponse;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\AppFramework\Http\RedirectResponse;
  34. use OCP\Http\Client\IClientService;
  35. use OCP\IConfig;
  36. use OCP\IL10N;
  37. use OCP\ILogger;
  38. use OCP\IRequest;
  39. use OC_Util;
  40. use OCP\IURLGenerator;
  41. /**
  42. * @package OC\Settings\Controller
  43. */
  44. class CheckSetupController extends Controller {
  45. /** @var IConfig */
  46. private $config;
  47. /** @var IClientService */
  48. private $clientService;
  49. /** @var \OC_Util */
  50. private $util;
  51. /** @var IURLGenerator */
  52. private $urlGenerator;
  53. /** @var IL10N */
  54. private $l10n;
  55. /** @var Checker */
  56. private $checker;
  57. /** @var ILogger */
  58. private $logger;
  59. /**
  60. * @param string $AppName
  61. * @param IRequest $request
  62. * @param IConfig $config
  63. * @param IClientService $clientService
  64. * @param IURLGenerator $urlGenerator
  65. * @param \OC_Util $util
  66. * @param IL10N $l10n
  67. * @param Checker $checker
  68. * @param ILogger $logger
  69. */
  70. public function __construct($AppName,
  71. IRequest $request,
  72. IConfig $config,
  73. IClientService $clientService,
  74. IURLGenerator $urlGenerator,
  75. \OC_Util $util,
  76. IL10N $l10n,
  77. Checker $checker,
  78. ILogger $logger) {
  79. parent::__construct($AppName, $request);
  80. $this->config = $config;
  81. $this->clientService = $clientService;
  82. $this->util = $util;
  83. $this->urlGenerator = $urlGenerator;
  84. $this->l10n = $l10n;
  85. $this->checker = $checker;
  86. $this->logger = $logger;
  87. }
  88. /**
  89. * Checks if the ownCloud server can connect to the internet using HTTPS and HTTP
  90. * @return bool
  91. */
  92. private function isInternetConnectionWorking() {
  93. if ($this->config->getSystemValue('has_internet_connection', true) === false) {
  94. return false;
  95. }
  96. $siteArray = ['www.nextcloud.com',
  97. 'www.google.com',
  98. 'www.github.com'];
  99. foreach($siteArray as $site) {
  100. if ($this->isSiteReachable($site)) {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * Chceks if the ownCloud server can connect to a specific URL using both HTTPS and HTTP
  108. * @return bool
  109. */
  110. private function isSiteReachable($sitename) {
  111. $httpSiteName = 'http://' . $sitename . '/';
  112. $httpsSiteName = 'https://' . $sitename . '/';
  113. try {
  114. $client = $this->clientService->newClient();
  115. $client->get($httpSiteName);
  116. $client->get($httpsSiteName);
  117. } catch (\Exception $e) {
  118. $this->logger->logException($e, ['app' => 'internet_connection_check']);
  119. return false;
  120. }
  121. return true;
  122. }
  123. /**
  124. * Checks whether a local memcache is installed or not
  125. * @return bool
  126. */
  127. private function isMemcacheConfigured() {
  128. return $this->config->getSystemValue('memcache.local', null) !== null;
  129. }
  130. /**
  131. * Whether /dev/urandom is available to the PHP controller
  132. *
  133. * @return bool
  134. */
  135. private function isUrandomAvailable() {
  136. if(@file_exists('/dev/urandom')) {
  137. $file = fopen('/dev/urandom', 'rb');
  138. if($file) {
  139. fclose($file);
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. /**
  146. * Public for the sake of unit-testing
  147. *
  148. * @return array
  149. */
  150. protected function getCurlVersion() {
  151. return curl_version();
  152. }
  153. /**
  154. * Check if the used SSL lib is outdated. Older OpenSSL and NSS versions do
  155. * have multiple bugs which likely lead to problems in combination with
  156. * functionality required by ownCloud such as SNI.
  157. *
  158. * @link https://github.com/owncloud/core/issues/17446#issuecomment-122877546
  159. * @link https://bugzilla.redhat.com/show_bug.cgi?id=1241172
  160. * @return string
  161. */
  162. private function isUsedTlsLibOutdated() {
  163. // Appstore is disabled by default in EE
  164. $appStoreDefault = false;
  165. if (\OC_Util::getEditionString() === '') {
  166. $appStoreDefault = true;
  167. }
  168. // Don't run check when:
  169. // 1. Server has `has_internet_connection` set to false
  170. // 2. AppStore AND S2S is disabled
  171. if(!$this->config->getSystemValue('has_internet_connection', true)) {
  172. return '';
  173. }
  174. if(!$this->config->getSystemValue('appstoreenabled', $appStoreDefault)
  175. && $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'no'
  176. && $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') === 'no') {
  177. return '';
  178. }
  179. $versionString = $this->getCurlVersion();
  180. if(isset($versionString['ssl_version'])) {
  181. $versionString = $versionString['ssl_version'];
  182. } else {
  183. return '';
  184. }
  185. $features = (string)$this->l10n->t('installing and updating apps via the app store or Federated Cloud Sharing');
  186. if(!$this->config->getSystemValue('appstoreenabled', $appStoreDefault)) {
  187. $features = (string)$this->l10n->t('Federated Cloud Sharing');
  188. }
  189. // Check if at least OpenSSL after 1.01d or 1.0.2b
  190. if(strpos($versionString, 'OpenSSL/') === 0) {
  191. $majorVersion = substr($versionString, 8, 5);
  192. $patchRelease = substr($versionString, 13, 6);
  193. if(($majorVersion === '1.0.1' && ord($patchRelease) < ord('d')) ||
  194. ($majorVersion === '1.0.2' && ord($patchRelease) < ord('b'))) {
  195. return (string) $this->l10n->t('cURL is using an outdated %s version (%s). Please update your operating system or features such as %s will not work reliably.', ['OpenSSL', $versionString, $features]);
  196. }
  197. }
  198. // Check if NSS and perform heuristic check
  199. if(strpos($versionString, 'NSS/') === 0) {
  200. try {
  201. $firstClient = $this->clientService->newClient();
  202. $firstClient->get('https://www.owncloud.org/');
  203. $secondClient = $this->clientService->newClient();
  204. $secondClient->get('https://owncloud.org/');
  205. } catch (ClientException $e) {
  206. if($e->getResponse()->getStatusCode() === 400) {
  207. return (string) $this->l10n->t('cURL is using an outdated %s version (%s). Please update your operating system or features such as %s will not work reliably.', ['NSS', $versionString, $features]);
  208. }
  209. }
  210. }
  211. return '';
  212. }
  213. /**
  214. * Whether the version is outdated
  215. *
  216. * @return bool
  217. */
  218. protected function isPhpOutdated() {
  219. if (version_compare(PHP_VERSION, '5.5.0') === -1) {
  220. return true;
  221. }
  222. return false;
  223. }
  224. /**
  225. * Whether the php version is still supported (at time of release)
  226. * according to: https://secure.php.net/supported-versions.php
  227. *
  228. * @return array
  229. */
  230. private function isPhpSupported() {
  231. return ['eol' => $this->isPhpOutdated(), 'version' => PHP_VERSION];
  232. }
  233. /**
  234. * Check if the reverse proxy configuration is working as expected
  235. *
  236. * @return bool
  237. */
  238. private function forwardedForHeadersWorking() {
  239. $trustedProxies = $this->config->getSystemValue('trusted_proxies', []);
  240. $remoteAddress = $this->request->getRemoteAddress();
  241. if (is_array($trustedProxies) && in_array($remoteAddress, $trustedProxies)) {
  242. return false;
  243. }
  244. // either not enabled or working correctly
  245. return true;
  246. }
  247. /**
  248. * Checks if the correct memcache module for PHP is installed. Only
  249. * fails if memcached is configured and the working module is not installed.
  250. *
  251. * @return bool
  252. */
  253. private function isCorrectMemcachedPHPModuleInstalled() {
  254. if ($this->config->getSystemValue('memcache.distributed', null) !== '\OC\Memcache\Memcached') {
  255. return true;
  256. }
  257. // there are two different memcached modules for PHP
  258. // we only support memcached and not memcache
  259. // https://code.google.com/p/memcached/wiki/PHPClientComparison
  260. return !(!extension_loaded('memcached') && extension_loaded('memcache'));
  261. }
  262. /**
  263. * @return RedirectResponse
  264. */
  265. public function rescanFailedIntegrityCheck() {
  266. $this->checker->runInstanceVerification();
  267. return new RedirectResponse(
  268. $this->urlGenerator->linkToRoute('settings.AdminSettings.index')
  269. );
  270. }
  271. /**
  272. * @NoCSRFRequired
  273. * @return DataResponse
  274. */
  275. public function getFailedIntegrityCheckFiles() {
  276. if(!$this->checker->isCodeCheckEnforced()) {
  277. return new DataDisplayResponse('Integrity checker has been disabled. Integrity cannot be verified.');
  278. }
  279. $completeResults = $this->checker->getResults();
  280. if(!empty($completeResults)) {
  281. $formattedTextResponse = 'Technical information
  282. =====================
  283. The following list covers which files have failed the integrity check. Please read
  284. the previous linked documentation to learn more about the errors and how to fix
  285. them.
  286. Results
  287. =======
  288. ';
  289. foreach($completeResults as $context => $contextResult) {
  290. $formattedTextResponse .= "- $context\n";
  291. foreach($contextResult as $category => $result) {
  292. $formattedTextResponse .= "\t- $category\n";
  293. if($category !== 'EXCEPTION') {
  294. foreach ($result as $key => $results) {
  295. $formattedTextResponse .= "\t\t- $key\n";
  296. }
  297. } else {
  298. foreach ($result as $key => $results) {
  299. $formattedTextResponse .= "\t\t- $results\n";
  300. }
  301. }
  302. }
  303. }
  304. $formattedTextResponse .= '
  305. Raw output
  306. ==========
  307. ';
  308. $formattedTextResponse .= print_r($completeResults, true);
  309. } else {
  310. $formattedTextResponse = 'No errors have been found.';
  311. }
  312. $response = new DataDisplayResponse(
  313. $formattedTextResponse,
  314. Http::STATUS_OK,
  315. [
  316. 'Content-Type' => 'text/plain',
  317. ]
  318. );
  319. return $response;
  320. }
  321. /**
  322. * @return DataResponse
  323. */
  324. public function check() {
  325. return new DataResponse(
  326. [
  327. 'serverHasInternetConnection' => $this->isInternetConnectionWorking(),
  328. 'isMemcacheConfigured' => $this->isMemcacheConfigured(),
  329. 'memcacheDocs' => $this->urlGenerator->linkToDocs('admin-performance'),
  330. 'isUrandomAvailable' => $this->isUrandomAvailable(),
  331. 'securityDocs' => $this->urlGenerator->linkToDocs('admin-security'),
  332. 'isUsedTlsLibOutdated' => $this->isUsedTlsLibOutdated(),
  333. 'phpSupported' => $this->isPhpSupported(),
  334. 'forwardedForHeadersWorking' => $this->forwardedForHeadersWorking(),
  335. 'reverseProxyDocs' => $this->urlGenerator->linkToDocs('admin-reverse-proxy'),
  336. 'isCorrectMemcachedPHPModuleInstalled' => $this->isCorrectMemcachedPHPModuleInstalled(),
  337. 'hasPassedCodeIntegrityCheck' => $this->checker->hasPassedCheck(),
  338. 'codeIntegrityCheckerDocumentation' => $this->urlGenerator->linkToDocs('admin-code-integrity'),
  339. ]
  340. );
  341. }
  342. }