ServerConfigChecks.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. /**
  3. * Server config checks management
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Config;
  7. use PhpMyAdmin\Core;
  8. use PhpMyAdmin\Sanitize;
  9. use PhpMyAdmin\Setup\Index as SetupIndex;
  10. use PhpMyAdmin\Url;
  11. use PhpMyAdmin\Util;
  12. use function count;
  13. use function function_exists;
  14. use function htmlspecialchars;
  15. use function implode;
  16. use function ini_get;
  17. use function preg_match;
  18. use function sprintf;
  19. use function strlen;
  20. /**
  21. * Performs various compatibility, security and consistency checks on current config
  22. *
  23. * Outputs results to message list, must be called between SetupIndex::messagesBegin()
  24. * and SetupIndex::messagesEnd()
  25. */
  26. class ServerConfigChecks
  27. {
  28. /** @var ConfigFile configurations being checked */
  29. protected $cfg;
  30. /**
  31. * @param ConfigFile $cfg Configuration
  32. */
  33. public function __construct(ConfigFile $cfg)
  34. {
  35. $this->cfg = $cfg;
  36. }
  37. /**
  38. * Perform config checks
  39. *
  40. * @return void
  41. */
  42. public function performConfigChecks()
  43. {
  44. $blowfishSecret = $this->cfg->get('blowfish_secret');
  45. $blowfishSecretSet = false;
  46. $cookieAuthUsed = false;
  47. [$cookieAuthUsed, $blowfishSecret, $blowfishSecretSet]
  48. = $this->performConfigChecksServers(
  49. $cookieAuthUsed,
  50. $blowfishSecret,
  51. $blowfishSecretSet
  52. );
  53. $this->performConfigChecksCookieAuthUsed(
  54. $cookieAuthUsed,
  55. $blowfishSecretSet,
  56. $blowfishSecret
  57. );
  58. // $cfg['AllowArbitraryServer']
  59. // should be disabled
  60. if ($this->cfg->getValue('AllowArbitraryServer')) {
  61. $sAllowArbitraryServerWarn = sprintf(
  62. __(
  63. 'This %soption%s should be disabled as it allows attackers to '
  64. . 'bruteforce login to any MySQL server. If you feel this is necessary, '
  65. . 'use %srestrict login to MySQL server%s or %strusted proxies list%s. '
  66. . 'However, IP-based protection with trusted proxies list may not be '
  67. . 'reliable if your IP belongs to an ISP where thousands of users, '
  68. . 'including you, are connected to.'
  69. ),
  70. '[a@' . Url::getCommon(['page' => 'form', 'formset' => 'Features']) . '#tab_Security]',
  71. '[/a]',
  72. '[a@' . Url::getCommon(['page' => 'form', 'formset' => 'Features']) . '#tab_Security]',
  73. '[/a]',
  74. '[a@' . Url::getCommon(['page' => 'form', 'formset' => 'Features']) . '#tab_Security]',
  75. '[/a]'
  76. );
  77. SetupIndex::messagesSet(
  78. 'notice',
  79. 'AllowArbitraryServer',
  80. Descriptions::get('AllowArbitraryServer'),
  81. Sanitize::sanitizeMessage($sAllowArbitraryServerWarn)
  82. );
  83. }
  84. $this->performConfigChecksLoginCookie();
  85. $sDirectoryNotice = __(
  86. 'This value should be double checked to ensure that this directory is '
  87. . 'neither world accessible nor readable or writable by other users on '
  88. . 'your server.'
  89. );
  90. // $cfg['SaveDir']
  91. // should not be world-accessible
  92. if ($this->cfg->getValue('SaveDir') != '') {
  93. SetupIndex::messagesSet(
  94. 'notice',
  95. 'SaveDir',
  96. Descriptions::get('SaveDir'),
  97. Sanitize::sanitizeMessage($sDirectoryNotice)
  98. );
  99. }
  100. // $cfg['TempDir']
  101. // should not be world-accessible
  102. if ($this->cfg->getValue('TempDir') != '') {
  103. SetupIndex::messagesSet(
  104. 'notice',
  105. 'TempDir',
  106. Descriptions::get('TempDir'),
  107. Sanitize::sanitizeMessage($sDirectoryNotice)
  108. );
  109. }
  110. $this->performConfigChecksZips();
  111. }
  112. /**
  113. * Check config of servers
  114. *
  115. * @param bool $cookieAuthUsed Cookie auth is used
  116. * @param string $blowfishSecret Blowfish secret
  117. * @param bool $blowfishSecretSet Blowfish secret set
  118. *
  119. * @return array
  120. */
  121. protected function performConfigChecksServers(
  122. $cookieAuthUsed,
  123. $blowfishSecret,
  124. $blowfishSecretSet
  125. ) {
  126. $serverCnt = $this->cfg->getServerCount();
  127. for ($i = 1; $i <= $serverCnt; $i++) {
  128. $cookieAuthServer
  129. = ($this->cfg->getValue('Servers/' . $i . '/auth_type') === 'cookie');
  130. $cookieAuthUsed |= $cookieAuthServer;
  131. $serverName = $this->performConfigChecksServersGetServerName(
  132. $this->cfg->getServerName($i),
  133. $i
  134. );
  135. $serverName = htmlspecialchars($serverName);
  136. [$blowfishSecret, $blowfishSecretSet]
  137. = $this->performConfigChecksServersSetBlowfishSecret(
  138. $blowfishSecret,
  139. $cookieAuthServer,
  140. $blowfishSecretSet
  141. );
  142. // $cfg['Servers'][$i]['ssl']
  143. // should be enabled if possible
  144. if (! $this->cfg->getValue('Servers/' . $i . '/ssl')) {
  145. $title = Descriptions::get('Servers/1/ssl') . ' (' . $serverName . ')';
  146. SetupIndex::messagesSet(
  147. 'notice',
  148. 'Servers/' . $i . '/ssl',
  149. $title,
  150. __(
  151. 'You should use SSL connections if your database server '
  152. . 'supports it.'
  153. )
  154. );
  155. }
  156. $sSecurityInfoMsg = Sanitize::sanitizeMessage(sprintf(
  157. __(
  158. 'If you feel this is necessary, use additional protection settings - '
  159. . '%1$shost authentication%2$s settings and %3$strusted proxies list%4$s. '
  160. . 'However, IP-based protection may not be reliable if your IP belongs '
  161. . 'to an ISP where thousands of users, including you, are connected to.'
  162. ),
  163. '[a@' . Url::getCommon(['page' => 'servers', 'mode' => 'edit', 'id' => $i]) . '#tab_Server_config]',
  164. '[/a]',
  165. '[a@' . Url::getCommon(['page' => 'form', 'formset' => 'Features']) . '#tab_Security]',
  166. '[/a]'
  167. ));
  168. // $cfg['Servers'][$i]['auth_type']
  169. // warn about full user credentials if 'auth_type' is 'config'
  170. if ($this->cfg->getValue('Servers/' . $i . '/auth_type') === 'config'
  171. && $this->cfg->getValue('Servers/' . $i . '/user') != ''
  172. && $this->cfg->getValue('Servers/' . $i . '/password') != ''
  173. ) {
  174. $title = Descriptions::get('Servers/1/auth_type')
  175. . ' (' . $serverName . ')';
  176. SetupIndex::messagesSet(
  177. 'notice',
  178. 'Servers/' . $i . '/auth_type',
  179. $title,
  180. Sanitize::sanitizeMessage(sprintf(
  181. __(
  182. 'You set the [kbd]config[/kbd] authentication type and included '
  183. . 'username and password for auto-login, which is not a desirable '
  184. . 'option for live hosts. Anyone who knows or guesses your phpMyAdmin '
  185. . 'URL can directly access your phpMyAdmin panel. Set %1$sauthentication '
  186. . 'type%2$s to [kbd]cookie[/kbd] or [kbd]http[/kbd].'
  187. ),
  188. '[a@' . Url::getCommon(['page' => 'servers', 'mode' => 'edit', 'id' => $i]) . '#tab_Server]',
  189. '[/a]'
  190. ))
  191. . ' ' . $sSecurityInfoMsg
  192. );
  193. }
  194. // $cfg['Servers'][$i]['AllowRoot']
  195. // $cfg['Servers'][$i]['AllowNoPassword']
  196. // serious security flaw
  197. if (! $this->cfg->getValue('Servers/' . $i . '/AllowRoot')
  198. || ! $this->cfg->getValue('Servers/' . $i . '/AllowNoPassword')
  199. ) {
  200. continue;
  201. }
  202. $title = Descriptions::get('Servers/1/AllowNoPassword')
  203. . ' (' . $serverName . ')';
  204. SetupIndex::messagesSet(
  205. 'notice',
  206. 'Servers/' . $i . '/AllowNoPassword',
  207. $title,
  208. __('You allow for connecting to the server without a password.')
  209. . ' ' . $sSecurityInfoMsg
  210. );
  211. }
  212. return [
  213. $cookieAuthUsed,
  214. $blowfishSecret,
  215. $blowfishSecretSet,
  216. ];
  217. }
  218. /**
  219. * Set blowfish secret
  220. *
  221. * @param string|null $blowfishSecret Blowfish secret
  222. * @param bool $cookieAuthServer Cookie auth is used
  223. * @param bool $blowfishSecretSet Blowfish secret set
  224. *
  225. * @return array
  226. */
  227. protected function performConfigChecksServersSetBlowfishSecret(
  228. $blowfishSecret,
  229. $cookieAuthServer,
  230. $blowfishSecretSet
  231. ): array {
  232. if ($cookieAuthServer && $blowfishSecret === null) {
  233. $blowfishSecretSet = true;
  234. $this->cfg->set('blowfish_secret', Util::generateRandom(32));
  235. }
  236. return [
  237. $blowfishSecret,
  238. $blowfishSecretSet,
  239. ];
  240. }
  241. /**
  242. * Define server name
  243. *
  244. * @param string $serverName Server name
  245. * @param int $serverId Server id
  246. *
  247. * @return string Server name
  248. */
  249. protected function performConfigChecksServersGetServerName(
  250. $serverName,
  251. $serverId
  252. ) {
  253. if ($serverName === 'localhost') {
  254. return $serverName . ' [' . $serverId . ']';
  255. }
  256. return $serverName;
  257. }
  258. /**
  259. * Perform config checks for zip part.
  260. *
  261. * @return void
  262. */
  263. protected function performConfigChecksZips()
  264. {
  265. $this->performConfigChecksServerGZipdump();
  266. $this->performConfigChecksServerBZipdump();
  267. $this->performConfigChecksServersZipdump();
  268. }
  269. /**
  270. * Perform config checks for zip part.
  271. *
  272. * @return void
  273. */
  274. protected function performConfigChecksServersZipdump()
  275. {
  276. // $cfg['ZipDump']
  277. // requires zip_open in import
  278. if ($this->cfg->getValue('ZipDump') && ! $this->functionExists('zip_open')) {
  279. SetupIndex::messagesSet(
  280. 'error',
  281. 'ZipDump_import',
  282. Descriptions::get('ZipDump'),
  283. Sanitize::sanitizeMessage(sprintf(
  284. __(
  285. '%sZip decompression%s requires functions (%s) which are unavailable '
  286. . 'on this system.'
  287. ),
  288. '[a@' . Url::getCommon(['page' => 'form', 'formset' => 'Features']) . '#tab_Import_export]',
  289. '[/a]',
  290. 'zip_open'
  291. ))
  292. );
  293. }
  294. // $cfg['ZipDump']
  295. // requires gzcompress in export
  296. if (! $this->cfg->getValue('ZipDump') || $this->functionExists('gzcompress')) {
  297. return;
  298. }
  299. SetupIndex::messagesSet(
  300. 'error',
  301. 'ZipDump_export',
  302. Descriptions::get('ZipDump'),
  303. Sanitize::sanitizeMessage(sprintf(
  304. __(
  305. '%sZip compression%s requires functions (%s) which are unavailable on '
  306. . 'this system.'
  307. ),
  308. '[a@' . Url::getCommon(['page' => 'form', 'formset' => 'Features']) . '#tab_Import_export]',
  309. '[/a]',
  310. 'gzcompress'
  311. ))
  312. );
  313. }
  314. /**
  315. * Check config of servers
  316. *
  317. * @param bool $cookieAuthUsed Cookie auth is used
  318. * @param bool $blowfishSecretSet Blowfish secret set
  319. * @param string $blowfishSecret Blowfish secret
  320. *
  321. * @return void
  322. */
  323. protected function performConfigChecksCookieAuthUsed(
  324. $cookieAuthUsed,
  325. $blowfishSecretSet,
  326. $blowfishSecret
  327. ) {
  328. // $cfg['blowfish_secret']
  329. // it's required for 'cookie' authentication
  330. if (! $cookieAuthUsed) {
  331. return;
  332. }
  333. if ($blowfishSecretSet) {
  334. // 'cookie' auth used, blowfish_secret was generated
  335. SetupIndex::messagesSet(
  336. 'notice',
  337. 'blowfish_secret_created',
  338. Descriptions::get('blowfish_secret'),
  339. Sanitize::sanitizeMessage(__(
  340. 'You didn\'t have blowfish secret set and have enabled '
  341. . '[kbd]cookie[/kbd] authentication, so a key was automatically '
  342. . 'generated for you. It is used to encrypt cookies; you don\'t need to '
  343. . 'remember it.'
  344. ))
  345. );
  346. } else {
  347. $blowfishWarnings = [];
  348. // check length
  349. if (strlen($blowfishSecret) < 32) {
  350. // too short key
  351. $blowfishWarnings[] = __(
  352. 'Key is too short, it should have at least 32 characters.'
  353. );
  354. }
  355. // check used characters
  356. $hasDigits = (bool) preg_match('/\d/', $blowfishSecret);
  357. $hasChars = (bool) preg_match('/\S/', $blowfishSecret);
  358. $hasNonword = (bool) preg_match('/\W/', $blowfishSecret);
  359. if (! $hasDigits || ! $hasChars || ! $hasNonword) {
  360. $blowfishWarnings[] = Sanitize::sanitizeMessage(
  361. __(
  362. 'Key should contain letters, numbers [em]and[/em] '
  363. . 'special characters.'
  364. )
  365. );
  366. }
  367. if (! empty($blowfishWarnings)) {
  368. SetupIndex::messagesSet(
  369. 'error',
  370. 'blowfish_warnings' . count($blowfishWarnings),
  371. Descriptions::get('blowfish_secret'),
  372. implode('<br>', $blowfishWarnings)
  373. );
  374. }
  375. }
  376. }
  377. /**
  378. * Check configuration for login cookie
  379. *
  380. * @return void
  381. */
  382. protected function performConfigChecksLoginCookie()
  383. {
  384. // $cfg['LoginCookieValidity']
  385. // value greater than session.gc_maxlifetime will cause
  386. // random session invalidation after that time
  387. $loginCookieValidity = $this->cfg->getValue('LoginCookieValidity');
  388. if ($loginCookieValidity > ini_get('session.gc_maxlifetime')
  389. ) {
  390. SetupIndex::messagesSet(
  391. 'error',
  392. 'LoginCookieValidity',
  393. Descriptions::get('LoginCookieValidity'),
  394. Sanitize::sanitizeMessage(sprintf(
  395. __(
  396. '%1$sLogin cookie validity%2$s greater than %3$ssession.gc_maxlifetime%4$s may '
  397. . 'cause random session invalidation (currently session.gc_maxlifetime '
  398. . 'is %5$d).'
  399. ),
  400. '[a@' . Url::getCommon(['page' => 'form', 'formset' => 'Features']) . '#tab_Security]',
  401. '[/a]',
  402. '[a@' . Core::getPHPDocLink('session.configuration.php#ini.session.gc-maxlifetime') . ']',
  403. '[/a]',
  404. ini_get('session.gc_maxlifetime')
  405. ))
  406. );
  407. }
  408. // $cfg['LoginCookieValidity']
  409. // should be at most 1800 (30 min)
  410. if ($loginCookieValidity > 1800) {
  411. SetupIndex::messagesSet(
  412. 'notice',
  413. 'LoginCookieValidity',
  414. Descriptions::get('LoginCookieValidity'),
  415. Sanitize::sanitizeMessage(sprintf(
  416. __(
  417. '%sLogin cookie validity%s should be set to 1800 seconds (30 minutes) '
  418. . 'at most. Values larger than 1800 may pose a security risk such as '
  419. . 'impersonation.'
  420. ),
  421. '[a@' . Url::getCommon(['page' => 'form', 'formset' => 'Features']) . '#tab_Security]',
  422. '[/a]'
  423. ))
  424. );
  425. }
  426. // $cfg['LoginCookieValidity']
  427. // $cfg['LoginCookieStore']
  428. // LoginCookieValidity must be less or equal to LoginCookieStore
  429. if (($this->cfg->getValue('LoginCookieStore') == 0)
  430. || ($loginCookieValidity <= $this->cfg->getValue('LoginCookieStore'))
  431. ) {
  432. return;
  433. }
  434. SetupIndex::messagesSet(
  435. 'error',
  436. 'LoginCookieValidity',
  437. Descriptions::get('LoginCookieValidity'),
  438. Sanitize::sanitizeMessage(sprintf(
  439. __(
  440. 'If using [kbd]cookie[/kbd] authentication and %sLogin cookie store%s '
  441. . 'is not 0, %sLogin cookie validity%s must be set to a value less or '
  442. . 'equal to it.'
  443. ),
  444. '[a@' . Url::getCommon(['page' => 'form', 'formset' => 'Features']) . '#tab_Security]',
  445. '[/a]',
  446. '[a@' . Url::getCommon(['page' => 'form', 'formset' => 'Features']) . '#tab_Security]',
  447. '[/a]'
  448. ))
  449. );
  450. }
  451. /**
  452. * Check GZipDump configuration
  453. *
  454. * @return void
  455. */
  456. protected function performConfigChecksServerBZipdump()
  457. {
  458. // $cfg['BZipDump']
  459. // requires bzip2 functions
  460. if (! $this->cfg->getValue('BZipDump')
  461. || ($this->functionExists('bzopen') && $this->functionExists('bzcompress'))
  462. ) {
  463. return;
  464. }
  465. $functions = $this->functionExists('bzopen')
  466. ? '' :
  467. 'bzopen';
  468. $functions .= $this->functionExists('bzcompress')
  469. ? ''
  470. : ($functions ? ', ' : '') . 'bzcompress';
  471. SetupIndex::messagesSet(
  472. 'error',
  473. 'BZipDump',
  474. Descriptions::get('BZipDump'),
  475. Sanitize::sanitizeMessage(
  476. sprintf(
  477. __(
  478. '%1$sBzip2 compression and decompression%2$s requires functions (%3$s) which '
  479. . 'are unavailable on this system.'
  480. ),
  481. '[a@' . Url::getCommon(['page' => 'form', 'formset' => 'Features']) . '#tab_Import_export]',
  482. '[/a]',
  483. $functions
  484. )
  485. )
  486. );
  487. }
  488. /**
  489. * Check GZipDump configuration
  490. *
  491. * @return void
  492. */
  493. protected function performConfigChecksServerGZipdump()
  494. {
  495. // $cfg['GZipDump']
  496. // requires zlib functions
  497. if (! $this->cfg->getValue('GZipDump')
  498. || ($this->functionExists('gzopen') && $this->functionExists('gzencode'))
  499. ) {
  500. return;
  501. }
  502. SetupIndex::messagesSet(
  503. 'error',
  504. 'GZipDump',
  505. Descriptions::get('GZipDump'),
  506. Sanitize::sanitizeMessage(sprintf(
  507. __(
  508. '%1$sGZip compression and decompression%2$s requires functions (%3$s) which '
  509. . 'are unavailable on this system.'
  510. ),
  511. '[a@' . Url::getCommon(['page' => 'form', 'formset' => 'Features']) . '#tab_Import_export]',
  512. '[/a]',
  513. 'gzencode'
  514. ))
  515. );
  516. }
  517. /**
  518. * Wrapper around function_exists to allow mock in test
  519. *
  520. * @param string $name Function name
  521. *
  522. * @return bool
  523. */
  524. protected function functionExists($name)
  525. {
  526. return function_exists($name);
  527. }
  528. }