AuthenticationSignon.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * SignOn Authentication plugin for phpMyAdmin
  5. *
  6. * @package PhpMyAdmin-Authentication
  7. * @subpackage SignOn
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. /* Get the authentication interface */
  13. require_once 'libraries/plugins/AuthenticationPlugin.class.php';
  14. /**
  15. * Handles the SignOn authentication method
  16. *
  17. * @package PhpMyAdmin-Authentication
  18. */
  19. class AuthenticationSignon extends AuthenticationPlugin
  20. {
  21. /**
  22. * Displays authentication form
  23. *
  24. * @global string the font face to use in case of failure
  25. * @global string the default font size to use in case of failure
  26. * @global string the big font size to use in case of failure
  27. *
  28. * @return boolean always true (no return indeed)
  29. */
  30. public function auth()
  31. {
  32. unset($_SESSION['LAST_SIGNON_URL']);
  33. if (empty($GLOBALS['cfg']['Server']['SignonURL'])) {
  34. PMA_fatalError('You must set SignonURL!');
  35. } elseif (! empty($_REQUEST['old_usr'])
  36. && ! empty($GLOBALS['cfg']['Server']['LogoutURL'])
  37. ) {
  38. /* Perform logout to custom URL */
  39. PMA_sendHeaderLocation($GLOBALS['cfg']['Server']['LogoutURL']);
  40. } else {
  41. PMA_sendHeaderLocation($GLOBALS['cfg']['Server']['SignonURL']);
  42. }
  43. if (! defined('TESTSUITE')) {
  44. exit();
  45. } else {
  46. return false;
  47. }
  48. }
  49. /**
  50. * Gets advanced authentication settings
  51. *
  52. * @global string $PHP_AUTH_USER the username if register_globals is on
  53. * @global string $PHP_AUTH_PW the password if register_globals is on
  54. * @global array the array of server variables if
  55. * register_globals is off
  56. * @global array the array of environment variables if
  57. * register_globals is off
  58. * @global string the username for the ? server
  59. * @global string the password for the ? server
  60. * @global string the username for the WebSite Professional
  61. * server
  62. * @global string the password for the WebSite Professional
  63. * server
  64. * @global string the username of the user who logs out
  65. *
  66. * @return boolean whether we get authentication settings or not
  67. */
  68. public function authCheck()
  69. {
  70. global $PHP_AUTH_USER, $PHP_AUTH_PW;
  71. /* Check if we're using same sigon server */
  72. $signon_url = $GLOBALS['cfg']['Server']['SignonURL'];
  73. if (isset($_SESSION['LAST_SIGNON_URL'])
  74. && $_SESSION['LAST_SIGNON_URL'] != $signon_url
  75. ) {
  76. return false;
  77. }
  78. /* Script name */
  79. $script_name = $GLOBALS['cfg']['Server']['SignonScript'];
  80. /* Session name */
  81. $session_name = $GLOBALS['cfg']['Server']['SignonSession'];
  82. /* Login URL */
  83. $signon_url = $GLOBALS['cfg']['Server']['SignonURL'];
  84. /* Current host */
  85. $single_signon_host = $GLOBALS['cfg']['Server']['host'];
  86. /* Current port */
  87. $single_signon_port = $GLOBALS['cfg']['Server']['port'];
  88. /* No configuration updates */
  89. $single_signon_cfgupdate = array();
  90. /* Are we requested to do logout? */
  91. $do_logout = !empty($_REQUEST['old_usr']);
  92. /* Handle script based auth */
  93. if (!empty($script_name)) {
  94. if (! file_exists($script_name)) {
  95. PMA_fatalError(
  96. __('Can not find signon authentication script:')
  97. . ' ' . $script_name
  98. );
  99. }
  100. include $script_name;
  101. list ($PHP_AUTH_USER, $PHP_AUTH_PW)
  102. = get_login_credentials($GLOBALS['cfg']['Server']['user']);
  103. } elseif (isset($_COOKIE[$session_name])) { /* Does session exist? */
  104. /* End current session */
  105. $old_session = session_name();
  106. $old_id = session_id();
  107. if (! defined('TESTSUITE')) {
  108. session_write_close();
  109. }
  110. /* Load single signon session */
  111. session_name($session_name);
  112. session_id($_COOKIE[$session_name]);
  113. if (! defined('TESTSUITE')) {
  114. session_start();
  115. }
  116. /* Clear error message */
  117. unset($_SESSION['PMA_single_signon_error_message']);
  118. /* Grab credentials if they exist */
  119. if (isset($_SESSION['PMA_single_signon_user'])) {
  120. if ($do_logout) {
  121. $PHP_AUTH_USER = '';
  122. } else {
  123. $PHP_AUTH_USER = $_SESSION['PMA_single_signon_user'];
  124. }
  125. }
  126. if (isset($_SESSION['PMA_single_signon_password'])) {
  127. if ($do_logout) {
  128. $PHP_AUTH_PW = '';
  129. } else {
  130. $PHP_AUTH_PW = $_SESSION['PMA_single_signon_password'];
  131. }
  132. }
  133. if (isset($_SESSION['PMA_single_signon_host'])) {
  134. $single_signon_host = $_SESSION['PMA_single_signon_host'];
  135. }
  136. if (isset($_SESSION['PMA_single_signon_port'])) {
  137. $single_signon_port = $_SESSION['PMA_single_signon_port'];
  138. }
  139. if (isset($_SESSION['PMA_single_signon_cfgupdate'])) {
  140. $single_signon_cfgupdate = $_SESSION['PMA_single_signon_cfgupdate'];
  141. }
  142. /* Also get token as it is needed to access subpages */
  143. if (isset($_SESSION['PMA_single_signon_token'])) {
  144. /* No need to care about token on logout */
  145. $pma_token = $_SESSION['PMA_single_signon_token'];
  146. }
  147. /* End single signon session */
  148. if (! defined('TESTSUITE')) {
  149. session_write_close();
  150. }
  151. /* Restart phpMyAdmin session */
  152. session_name($old_session);
  153. if (!empty($old_id)) {
  154. session_id($old_id);
  155. }
  156. if (! defined('TESTSUITE')) {
  157. session_start();
  158. }
  159. /* Set the single signon host */
  160. $GLOBALS['cfg']['Server']['host'] = $single_signon_host;
  161. /* Set the single signon port */
  162. $GLOBALS['cfg']['Server']['port'] = $single_signon_port;
  163. /* Configuration update */
  164. $GLOBALS['cfg']['Server'] = array_merge(
  165. $GLOBALS['cfg']['Server'],
  166. $single_signon_cfgupdate
  167. );
  168. /* Restore our token */
  169. if (!empty($pma_token)) {
  170. $_SESSION[' PMA_token '] = $pma_token;
  171. }
  172. /**
  173. * Clear user cache.
  174. */
  175. PMA_Util::clearUserCache();
  176. }
  177. // Returns whether we get authentication settings or not
  178. if (empty($PHP_AUTH_USER)) {
  179. unset($_SESSION['LAST_SIGNON_URL']);
  180. return false;
  181. } else {
  182. $_SESSION['LAST_SIGNON_URL'] = $GLOBALS['cfg']['Server']['SignonURL'];
  183. return true;
  184. }
  185. }
  186. /**
  187. * Set the user and password after last checkings if required
  188. *
  189. * @global array $cfg the valid servers settings
  190. * @global integer the id of the current server
  191. * @global array the current server settings
  192. * @global string $PHP_AUTH_USER the current username
  193. * @global string $PHP_AUTH_PW the current password
  194. *
  195. * @return boolean always true
  196. */
  197. public function authSetUser()
  198. {
  199. global $cfg;
  200. global $PHP_AUTH_USER, $PHP_AUTH_PW;
  201. $cfg['Server']['user'] = $PHP_AUTH_USER;
  202. $cfg['Server']['password'] = $PHP_AUTH_PW;
  203. return true;
  204. }
  205. /**
  206. * User is not allowed to login to MySQL -> authentication failed
  207. *
  208. * @return boolean always true (no return indeed)
  209. */
  210. public function authFails()
  211. {
  212. /* Session name */
  213. $session_name = $GLOBALS['cfg']['Server']['SignonSession'];
  214. /* Does session exist? */
  215. if (isset($_COOKIE[$session_name])) {
  216. /* End current session */
  217. if (! defined('TESTSUITE')) {
  218. session_write_close();
  219. }
  220. /* Load single signon session */
  221. session_name($session_name);
  222. session_id($_COOKIE[$session_name]);
  223. if (! defined('TESTSUITE')) {
  224. session_start();
  225. }
  226. /* Set error message */
  227. if (! empty($GLOBALS['login_without_password_is_forbidden'])) {
  228. $_SESSION['PMA_single_signon_error_message'] = __(
  229. 'Login without a password is forbidden by configuration '
  230. . '(see AllowNoPassword)'
  231. );
  232. } elseif (! empty($GLOBALS['allowDeny_forbidden'])) {
  233. $_SESSION['PMA_single_signon_error_message'] = __('Access denied!');
  234. } elseif (! empty($GLOBALS['no_activity'])) {
  235. $_SESSION['PMA_single_signon_error_message'] = sprintf(
  236. __('No activity within %s seconds; please log in again.'),
  237. $GLOBALS['cfg']['LoginCookieValidity']
  238. );
  239. } elseif ($GLOBALS['dbi']->getError()) {
  240. $_SESSION['PMA_single_signon_error_message'] = PMA_sanitize(
  241. $GLOBALS['dbi']->getError()
  242. );
  243. } else {
  244. $_SESSION['PMA_single_signon_error_message'] = __(
  245. 'Cannot log in to the MySQL server'
  246. );
  247. }
  248. }
  249. $this->auth();
  250. }
  251. /**
  252. * This method is called when any PluginManager to which the observer
  253. * is attached calls PluginManager::notify()
  254. *
  255. * @param SplSubject $subject The PluginManager notifying the observer
  256. * of an update.
  257. *
  258. * @return void
  259. */
  260. public function update (SplSubject $subject)
  261. {
  262. }
  263. }