AuthenticationSignon.class.php 9.3 KB

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