AuthenticationHttp.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * HTTP Authentication plugin for phpMyAdmin.
  4. * NOTE: Requires PHP loaded as a Apache module.
  5. */
  6. declare(strict_types=1);
  7. namespace PhpMyAdmin\Plugins\Auth;
  8. use PhpMyAdmin\Config;
  9. use PhpMyAdmin\Core;
  10. use PhpMyAdmin\Message;
  11. use PhpMyAdmin\Plugins\AuthenticationPlugin;
  12. use PhpMyAdmin\Response;
  13. use function base64_decode;
  14. use function defined;
  15. use function hash_equals;
  16. use function preg_replace;
  17. use function sprintf;
  18. use function strcmp;
  19. use function strpos;
  20. use function substr;
  21. /**
  22. * Handles the HTTP authentication methods
  23. */
  24. class AuthenticationHttp extends AuthenticationPlugin
  25. {
  26. /**
  27. * Displays authentication form and redirect as necessary
  28. *
  29. * @return bool always true (no return indeed)
  30. */
  31. public function showLoginForm()
  32. {
  33. $response = Response::getInstance();
  34. if ($response->isAjax()) {
  35. $response->setRequestStatus(false);
  36. // reload_flag removes the token parameter from the URL and reloads
  37. $response->addJSON('reload_flag', '1');
  38. if (defined('TESTSUITE')) {
  39. return true;
  40. }
  41. exit;
  42. }
  43. return $this->authForm();
  44. }
  45. /**
  46. * Displays authentication form
  47. *
  48. * @return bool
  49. */
  50. public function authForm()
  51. {
  52. if (empty($GLOBALS['cfg']['Server']['auth_http_realm'])) {
  53. if (empty($GLOBALS['cfg']['Server']['verbose'])) {
  54. $server_message = $GLOBALS['cfg']['Server']['host'];
  55. } else {
  56. $server_message = $GLOBALS['cfg']['Server']['verbose'];
  57. }
  58. $realm_message = 'phpMyAdmin ' . $server_message;
  59. } else {
  60. $realm_message = $GLOBALS['cfg']['Server']['auth_http_realm'];
  61. }
  62. $response = Response::getInstance();
  63. // remove non US-ASCII to respect RFC2616
  64. $realm_message = preg_replace('/[^\x20-\x7e]/i', '', $realm_message);
  65. $response->header('WWW-Authenticate: Basic realm="' . $realm_message . '"');
  66. $response->setHttpResponseCode(401);
  67. /* HTML header */
  68. $footer = $response->getFooter();
  69. $footer->setMinimal();
  70. $header = $response->getHeader();
  71. $header->setTitle(__('Access denied!'));
  72. $header->disableMenuAndConsole();
  73. $header->setBodyId('loginform');
  74. $response->addHTML('<h1>');
  75. $response->addHTML(sprintf(__('Welcome to %s'), ' phpMyAdmin'));
  76. $response->addHTML('</h1>');
  77. $response->addHTML('<h3>');
  78. $response->addHTML(
  79. Message::error(
  80. __('Wrong username/password. Access denied.')
  81. )
  82. );
  83. $response->addHTML('</h3>');
  84. $response->addHTML(Config::renderFooter());
  85. if (! defined('TESTSUITE')) {
  86. exit;
  87. }
  88. return false;
  89. }
  90. /**
  91. * Gets authentication credentials
  92. *
  93. * @return bool whether we get authentication settings or not
  94. */
  95. public function readCredentials()
  96. {
  97. // Grabs the $PHP_AUTH_USER variable
  98. if (isset($GLOBALS['PHP_AUTH_USER'])) {
  99. $this->user = $GLOBALS['PHP_AUTH_USER'];
  100. }
  101. if (empty($this->user)) {
  102. if (Core::getenv('PHP_AUTH_USER')) {
  103. $this->user = Core::getenv('PHP_AUTH_USER');
  104. } elseif (Core::getenv('REMOTE_USER')) {
  105. // CGI, might be encoded, see below
  106. $this->user = Core::getenv('REMOTE_USER');
  107. } elseif (Core::getenv('REDIRECT_REMOTE_USER')) {
  108. // CGI, might be encoded, see below
  109. $this->user = Core::getenv('REDIRECT_REMOTE_USER');
  110. } elseif (Core::getenv('AUTH_USER')) {
  111. // WebSite Professional
  112. $this->user = Core::getenv('AUTH_USER');
  113. } elseif (Core::getenv('HTTP_AUTHORIZATION')) {
  114. // IIS, might be encoded, see below
  115. $this->user = Core::getenv('HTTP_AUTHORIZATION');
  116. } elseif (Core::getenv('Authorization')) {
  117. // FastCGI, might be encoded, see below
  118. $this->user = Core::getenv('Authorization');
  119. }
  120. }
  121. // Grabs the $PHP_AUTH_PW variable
  122. if (isset($GLOBALS['PHP_AUTH_PW'])) {
  123. $this->password = $GLOBALS['PHP_AUTH_PW'];
  124. }
  125. if (empty($this->password)) {
  126. if (Core::getenv('PHP_AUTH_PW')) {
  127. $this->password = Core::getenv('PHP_AUTH_PW');
  128. } elseif (Core::getenv('REMOTE_PASSWORD')) {
  129. // Apache/CGI
  130. $this->password = Core::getenv('REMOTE_PASSWORD');
  131. } elseif (Core::getenv('AUTH_PASSWORD')) {
  132. // WebSite Professional
  133. $this->password = Core::getenv('AUTH_PASSWORD');
  134. }
  135. }
  136. // Sanitize empty password login
  137. if ($this->password === null) {
  138. $this->password = '';
  139. }
  140. // Avoid showing the password in phpinfo()'s output
  141. unset($GLOBALS['PHP_AUTH_PW'], $_SERVER['PHP_AUTH_PW']);
  142. // Decode possibly encoded information (used by IIS/CGI/FastCGI)
  143. // (do not use explode() because a user might have a colon in their password
  144. if (strcmp(substr($this->user, 0, 6), 'Basic ') == 0) {
  145. $usr_pass = base64_decode(substr($this->user, 6));
  146. if (! empty($usr_pass)) {
  147. $colon = strpos($usr_pass, ':');
  148. if ($colon) {
  149. $this->user = substr($usr_pass, 0, $colon);
  150. $this->password = substr($usr_pass, $colon + 1);
  151. }
  152. unset($colon);
  153. }
  154. unset($usr_pass);
  155. }
  156. // sanitize username
  157. $this->user = Core::sanitizeMySQLUser($this->user);
  158. // User logged out -> ensure the new username is not the same
  159. $old_usr = $_REQUEST['old_usr'] ?? '';
  160. if (! empty($old_usr)
  161. && (isset($this->user) && hash_equals($old_usr, $this->user))
  162. ) {
  163. $this->user = '';
  164. }
  165. // Returns whether we get authentication settings or not
  166. return ! empty($this->user);
  167. }
  168. /**
  169. * User is not allowed to login to MySQL -> authentication failed
  170. *
  171. * @param string $failure String describing why authentication has failed
  172. *
  173. * @return void
  174. */
  175. public function showFailure($failure)
  176. {
  177. global $dbi;
  178. parent::showFailure($failure);
  179. $error = $dbi->getError();
  180. if ($error && $GLOBALS['errno'] != 1045) {
  181. Core::fatalError($error);
  182. } else {
  183. $this->authForm();
  184. }
  185. }
  186. /**
  187. * Returns URL for login form.
  188. *
  189. * @return string
  190. */
  191. public function getLoginFormURL()
  192. {
  193. return './index.php?route=/&old_usr=' . $this->user;
  194. }
  195. }