AuthenticationHttp.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * HTTP Authentication plugin for phpMyAdmin.
  5. * NOTE: Requires PHP loaded as a Apache module.
  6. *
  7. * @package PhpMyAdmin-Authentication
  8. * @subpackage HTTP
  9. */
  10. namespace PhpMyAdmin\Plugins\Auth;
  11. use PhpMyAdmin\Plugins\AuthenticationPlugin;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Response;
  14. use PhpMyAdmin\Config;
  15. use PhpMyAdmin\Core;
  16. require_once './libraries/hash.lib.php';
  17. /**
  18. * Handles the HTTP authentication methods
  19. *
  20. * @package PhpMyAdmin-Authentication
  21. */
  22. class AuthenticationHttp extends AuthenticationPlugin
  23. {
  24. /**
  25. * Displays authentication form and redirect as necessary
  26. *
  27. * @return boolean always true (no return indeed)
  28. */
  29. public function showLoginForm()
  30. {
  31. $response = Response::getInstance();
  32. if ($response->isAjax()) {
  33. $response->setRequestStatus(false);
  34. // reload_flag removes the token parameter from the URL and reloads
  35. $response->addJSON('reload_flag', '1');
  36. if (defined('TESTSUITE')) {
  37. return true;
  38. } else {
  39. exit;
  40. }
  41. }
  42. return $this->authForm();
  43. }
  44. /**
  45. * Displays authentication form
  46. *
  47. * @return boolean
  48. */
  49. public function authForm()
  50. {
  51. if (empty($GLOBALS['cfg']['Server']['auth_http_realm'])) {
  52. if (empty($GLOBALS['cfg']['Server']['verbose'])) {
  53. $server_message = $GLOBALS['cfg']['Server']['host'];
  54. } else {
  55. $server_message = $GLOBALS['cfg']['Server']['verbose'];
  56. }
  57. $realm_message = 'phpMyAdmin ' . $server_message;
  58. } else {
  59. $realm_message = $GLOBALS['cfg']['Server']['auth_http_realm'];
  60. }
  61. $response = Response::getInstance();
  62. // remove non US-ASCII to respect RFC2616
  63. $realm_message = preg_replace('/[^\x20-\x7e]/i', '', $realm_message);
  64. $response->header('WWW-Authenticate: Basic realm="' . $realm_message . '"');
  65. $response->setHttpResponseCode(401);
  66. /* HTML header */
  67. $footer = $response->getFooter();
  68. $footer->setMinimal();
  69. $header = $response->getHeader();
  70. $header->setTitle(__('Access denied!'));
  71. $header->disableMenuAndConsole();
  72. $header->setBodyId('loginform');
  73. $response->addHTML('<h1>');
  74. $response->addHTML(sprintf(__('Welcome to %s'), ' phpMyAdmin'));
  75. $response->addHTML('</h1>');
  76. $response->addHTML('<h3>');
  77. $response->addHTML(
  78. Message::error(
  79. __('Wrong username/password. Access denied.')
  80. )
  81. );
  82. $response->addHTML('</h3>');
  83. $response->addHTML(Config::renderFooter());
  84. if (!defined('TESTSUITE')) {
  85. exit;
  86. } else {
  87. return false;
  88. }
  89. }
  90. /**
  91. * Gets authentication credentials
  92. *
  93. * @return boolean 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 (is_null($this->password)) {
  138. $this->password = '';
  139. }
  140. // Avoid showing the password in phpinfo()'s output
  141. unset($GLOBALS['PHP_AUTH_PW']);
  142. unset($_SERVER['PHP_AUTH_PW']);
  143. // Decode possibly encoded information (used by IIS/CGI/FastCGI)
  144. // (do not use explode() because a user might have a colon in his password
  145. if (strcmp(substr($this->user, 0, 6), 'Basic ') == 0) {
  146. $usr_pass = base64_decode(substr($this->user, 6));
  147. if (!empty($usr_pass)) {
  148. $colon = strpos($usr_pass, ':');
  149. if ($colon) {
  150. $this->user = substr($usr_pass, 0, $colon);
  151. $this->password = substr($usr_pass, $colon + 1);
  152. }
  153. unset($colon);
  154. }
  155. unset($usr_pass);
  156. }
  157. // sanitize username
  158. $this->user = Core::sanitizeMySQLUser($this->user);
  159. // User logged out -> ensure the new username is not the same
  160. $old_usr = isset($_REQUEST['old_usr']) ? $_REQUEST['old_usr'] : '';
  161. if (! empty($old_usr)
  162. && (isset($this->user) && hash_equals($old_usr, $this->user))
  163. ) {
  164. $this->user = '';
  165. }
  166. // Returns whether we get authentication settings or not
  167. return !empty($this->user);
  168. }
  169. /**
  170. * User is not allowed to login to MySQL -> authentication failed
  171. *
  172. * @param string $failure String describing why authentication has failed
  173. *
  174. * @return void
  175. */
  176. public function showFailure($failure)
  177. {
  178. parent::showFailure($failure);
  179. $error = $GLOBALS['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?old_usr=' . $this->user;
  194. }
  195. }