1
0

CheckSetupController.php 12 KB

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