2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to run cookie based authentication.
6 * @package phpMyAdmin-Auth-Cookie
9 if (! defined('PHPMYADMIN')) {
14 * Swekey authentication functions.
16 require './libraries/auth/swekey/swekey.auth.lib.php';
18 if (function_exists('mcrypt_encrypt')) {
20 * Uses faster mcrypt library if available
21 * (as this is not called from anywhere else, put the code in-line
22 * for faster execution)
27 * Store the initialization vector because it will be needed for
28 * further decryption. I don't think necessary to have one iv
29 * per server so I don't put the server number in the cookie name.
31 if (empty($_COOKIE['pma_mcrypt_iv'])
32 ||
false === ($iv = base64_decode($_COOKIE['pma_mcrypt_iv'], true))) {
33 srand((double) microtime() * 1000000);
34 $td = mcrypt_module_open(MCRYPT_BLOWFISH
, '', MCRYPT_MODE_CBC
, '');
36 PMA_warnMissingExtension('mcrypt');
38 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND
);
39 $GLOBALS['PMA_Config']->setCookie('pma_mcrypt_iv', base64_encode($iv));
43 * Encryption using blowfish algorithm (mcrypt)
45 * @param string original data
46 * @param string the secret
48 * @return string the encrypted result
53 function PMA_blowfish_encrypt($data, $secret)
56 return base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH
, $secret, $data, MCRYPT_MODE_CBC
, $iv));
60 * Decryption using blowfish algorithm (mcrypt)
62 * @param string encrypted data
63 * @param string the secret
65 * @return string original data
70 function PMA_blowfish_decrypt($encdata, $secret)
73 return trim(mcrypt_decrypt(MCRYPT_BLOWFISH
, $secret, base64_decode($encdata), MCRYPT_MODE_CBC
, $iv));
77 require_once './libraries/blowfish.php';
78 if (!$GLOBALS['cfg']['McryptDisableWarning']) {
79 PMA_warnMissingExtension('mcrypt');
84 * Returns blowfish secret or generates one if needed.
85 * @uses $cfg['blowfish_secret']
86 * @uses $_SESSION['auto_blowfish_secret']
90 function PMA_get_blowfish_secret() {
91 if (empty($GLOBALS['cfg']['blowfish_secret'])) {
92 if (empty($_SESSION['auto_blowfish_secret'])) {
93 // this returns 23 characters
94 $_SESSION['auto_blowfish_secret'] = uniqid('', true);
96 return $_SESSION['auto_blowfish_secret'];
98 // apply md5() to work around too long secrets (returns 32 characters)
99 return md5($GLOBALS['cfg']['blowfish_secret']);
104 * Displays authentication form
106 * this function MUST exit/quit the application
108 * @uses $GLOBALS['server']
109 * @uses $GLOBALS['PHP_AUTH_USER']
110 * @uses $GLOBALS['pma_auth_server']
111 * @uses $GLOBALS['text_dir']
112 * @uses $GLOBALS['pmaThemeImage']
113 * @uses $GLOBALS['charset']
114 * @uses $GLOBALS['target']
115 * @uses $GLOBALS['db']
116 * @uses $GLOBALS['table']
117 * @uses $GLOBALS['pmaThemeImage']
118 * @uses $cfg['Servers']
119 * @uses $cfg['LoginCookieRecall']
121 * @uses $cfg['Server']
122 * @uses $cfg['ReplaceHelpImg']
123 * @uses $cfg['blowfish_secret']
124 * @uses $cfg['AllowArbitraryServer']
126 * @uses $_REQUEST['old_usr']
127 * @uses PMA_sendHeaderLocation()
128 * @uses PMA_select_language()
129 * @uses PMA_select_server()
130 * @uses file_exists()
133 * @uses htmlspecialchars()
135 * @global string the last connection error
143 /* Perform logout to custom URL */
144 if (! empty($_REQUEST['old_usr'])
145 && ! empty($GLOBALS['cfg']['Server']['LogoutURL'])) {
146 PMA_sendHeaderLocation($GLOBALS['cfg']['Server']['LogoutURL']);
150 /* No recall if blowfish secret is not configured as it would produce garbage */
151 if ($GLOBALS['cfg']['LoginCookieRecall'] && !empty($GLOBALS['cfg']['blowfish_secret'])) {
152 $default_user = $GLOBALS['PHP_AUTH_USER'];
153 $default_server = $GLOBALS['pma_auth_server'];
157 $default_server = '';
158 // skip the IE autocomplete feature.
159 $autocomplete = ' autocomplete="off"';
162 $cell_align = ($GLOBALS['text_dir'] == 'ltr') ?
'left' : 'right';
164 // Defines the charset to be used
165 header('Content-Type: text/html; charset=' . $GLOBALS['charset']);
166 // Defines the "item" image depending on text direction
167 $item_img = $GLOBALS['pmaThemeImage'] . 'item_' . $GLOBALS['text_dir'] . '.png';
169 /* HTML header; do not show here the PMA version to improve security */
170 $page_title = 'phpMyAdmin ';
171 require './libraries/header_meta_style.inc.php';
172 require './libraries/header_scripts.inc.php';
174 <script type
="text/javascript">
176 // show login form in top frame
178 window
.top
.location
.href
=location
;
184 <body
class="loginform">
187 if (file_exists(CUSTOM_HEADER_FILE
)) {
188 require CUSTOM_HEADER_FILE
;
192 <div
class="container">
193 <a href
="<?php echo PMA_linkURL('http://www.phpmyadmin.net/'); ?>" target
="_blank" class="logo"><?php
194 $logo_image = $GLOBALS['pmaThemeImage'] . 'logo_right.png';
195 if (@file_exists
($logo_image)) {
196 echo '<img src="' . $logo_image . '" id="imLogo" name="imLogo" alt="phpMyAdmin" border="0" />';
198 echo '<img name="imLogo" id="imLogo" src="' . $GLOBALS['pmaThemeImage'] . 'pma_logo.png' . '" '
199 . 'border="0" width="88" height="31" alt="phpMyAdmin" />';
204 echo sprintf(__('Welcome to %s'),
205 '<bdo dir="ltr" xml:lang="en">' . $page_title . '</bdo>');
210 // Show error message
211 if (! empty($conn_error)) {
212 PMA_Message
::rawError($conn_error)->display();
215 // Displays the languages form
216 if (empty($GLOBALS['cfg']['Lang'])) {
217 require_once './libraries/display_select_lang.lib.php';
218 // use fieldset, don't show doc link
219 PMA_select_language(true, false);
225 <form method
="post" action
="index.php" name
="login_form"<?php
echo $autocomplete; ?
> target
="_top" class="login">
230 echo '<a href="./Documentation.html" target="documentation" ' .
231 'title="' . __('phpMyAdmin documentation') . '">';
232 if ($GLOBALS['cfg']['ReplaceHelpImg']) {
233 echo '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_help.png" width="11" height="11" alt="' . __('phpMyAdmin documentation') . '" />';
241 <?php
if ($GLOBALS['cfg']['AllowArbitraryServer']) { ?
>
243 <label
for="input_servername" title
="<?php echo __('You can enter hostname/IP address and port separated by space.'); ?>"><?php
echo __('Server:'); ?
></label
>
244 <input type
="text" name
="pma_servername" id
="input_servername" value
="<?php echo htmlspecialchars($default_server); ?>" size
="24" class="textfield" title
="<?php echo __('You can enter hostname/IP address and port separated by space.'); ?>" />
248 <label
for="input_username"><?php
echo __('Username:'); ?
></label
>
249 <input type
="text" name
="pma_username" id
="input_username" value
="<?php echo htmlspecialchars($default_user); ?>" size
="24" class="textfield"/>
252 <label
for="input_password"><?php
echo __('Password:'); ?
></label
>
253 <input type
="password" name
="pma_password" id
="input_password" value
="" size
="24" class="textfield" />
256 if (count($GLOBALS['cfg']['Servers']) > 1) {
259 <label
for="select_server"><?php
echo __('Server Choice'); ?
>:</label
>
260 <select name
="server" id
="select_server"
262 if ($GLOBALS['cfg']['AllowArbitraryServer']) {
263 echo ' onchange="document.forms[\'login_form\'].elements[\'pma_servername\'].value = \'\'" ';
267 require_once './libraries/select_server.lib.php';
268 PMA_select_server(false, false);
270 echo '</select></div>';
272 echo ' <input type="hidden" name="server" value="' . $GLOBALS['server'] . '" />';
273 } // end if (server choice)
276 <fieldset
class="tblFooters">
277 <input value
="<?php echo __('Go'); ?>" type
="submit" id
="input_go" />
279 $_form_params = array();
280 if (! empty($GLOBALS['target'])) {
281 $_form_params['target'] = $GLOBALS['target'];
283 if (! empty($GLOBALS['db'])) {
284 $_form_params['db'] = $GLOBALS['db'];
286 if (! empty($GLOBALS['table'])) {
287 $_form_params['table'] = $GLOBALS['table'];
289 // do not generate a "server" hidden field as we want the "server"
290 // drop-down to have priority
291 echo PMA_generate_common_hidden_inputs($_form_params, '', 0, 'server');
298 // BEGIN Swekey Integration
299 Swekey_login('input_username', 'input_go');
300 // END Swekey Integration
302 // show the "Cookies required" message only if cookies are disabled
303 // (we previously tried to set some cookies)
304 if (empty($_COOKIE)) {
305 trigger_error(__('Cookies must be enabled past this point.'), E_USER_NOTICE
);
307 if ($GLOBALS['error_handler']->hasDisplayErrors()) {
309 $GLOBALS['error_handler']->dispErrors();
315 if (file_exists(CUSTOM_FOOTER_FILE
)) {
316 require CUSTOM_FOOTER_FILE
;
323 } // end of the 'PMA_auth()' function
328 * Gets advanced authentication settings
330 * this function DOES NOT check authentication - it just checks/provides
331 * authentication credentials required to connect to the MySQL server
332 * usually with PMA_DBI_connect()
334 * it returns false if something is missing - which usually leads to
335 * PMA_auth() which displays login form
337 * it returns true if all seems ok which usually leads to PMA_auth_set_user()
339 * it directly switches to PMA_auth_fails() if user inactivity timout is reached
341 * @todo AllowArbitraryServer on does not imply that the user wants an
342 * arbitrary server, or? so we should also check if this is filled and
343 * not only if allowed
344 * @uses $GLOBALS['PHP_AUTH_USER']
345 * @uses $GLOBALS['PHP_AUTH_PW']
346 * @uses $GLOBALS['no_activity']
347 * @uses $GLOBALS['server']
348 * @uses $GLOBALS['from_cookie']
349 * @uses $GLOBALS['pma_auth_server']
350 * @uses $cfg['AllowArbitraryServer']
351 * @uses $cfg['LoginCookieValidity']
352 * @uses $cfg['Servers']
353 * @uses $_REQUEST['old_usr'] from logout link
354 * @uses $_REQUEST['pma_username'] from login form
355 * @uses $_REQUEST['pma_password'] from login form
356 * @uses $_REQUEST['pma_servername'] from login form
358 * @uses $_SESSION['last_access_time']
359 * @uses $GLOBALS['PMA_Config']->removeCookie()
360 * @uses PMA_blowfish_decrypt()
361 * @uses PMA_auth_fails()
364 * @return boolean whether we get authentication settings or not
368 function PMA_auth_check()
372 * @global $GLOBALS['pma_auth_server'] the user provided server to connect to
374 $GLOBALS['pma_auth_server'] = '';
376 $GLOBALS['PHP_AUTH_USER'] = $GLOBALS['PHP_AUTH_PW'] = '';
377 $GLOBALS['from_cookie'] = false;
379 // BEGIN Swekey Integration
380 if (! Swekey_auth_check()) {
383 // END Swekey Integration
385 if (defined('PMA_CLEAR_COOKIES')) {
386 foreach($GLOBALS['cfg']['Servers'] as $key => $val) {
387 $GLOBALS['PMA_Config']->removeCookie('pmaPass-' . $key);
388 $GLOBALS['PMA_Config']->removeCookie('pmaServer-' . $key);
389 $GLOBALS['PMA_Config']->removeCookie('pmaUser-' . $key);
394 if (! empty($_REQUEST['old_usr'])) {
395 // The user wants to be logged out
396 // -> delete his choices that were stored in session
398 // according to the PHP manual we should do this before the destroy:
399 //$_SESSION = array();
400 // but we still need some parts of the session information
401 // in libraries/header_meta_style.inc.php
404 // -> delete password cookie(s)
405 if ($GLOBALS['cfg']['LoginCookieDeleteAll']) {
406 foreach($GLOBALS['cfg']['Servers'] as $key => $val) {
407 $GLOBALS['PMA_Config']->removeCookie('pmaPass-' . $key);
408 if (isset($_COOKIE['pmaPass-' . $key])) {
409 unset($_COOKIE['pmaPass-' . $key]);
413 $GLOBALS['PMA_Config']->removeCookie('pmaPass-' . $GLOBALS['server']);
414 if (isset($_COOKIE['pmaPass-' . $GLOBALS['server']])) {
415 unset($_COOKIE['pmaPass-' . $GLOBALS['server']]);
420 if (! empty($_REQUEST['pma_username'])) {
421 // The user just logged in
422 $GLOBALS['PHP_AUTH_USER'] = $_REQUEST['pma_username'];
423 $GLOBALS['PHP_AUTH_PW'] = empty($_REQUEST['pma_password']) ?
'' : $_REQUEST['pma_password'];
424 if ($GLOBALS['cfg']['AllowArbitraryServer'] && isset($_REQUEST['pma_servername'])) {
425 $GLOBALS['pma_auth_server'] = $_REQUEST['pma_servername'];
430 // At the end, try to set the $GLOBALS['PHP_AUTH_USER']
431 // and $GLOBALS['PHP_AUTH_PW'] variables from cookies
434 if ($GLOBALS['cfg']['AllowArbitraryServer']
435 && ! empty($_COOKIE['pmaServer-' . $GLOBALS['server']])) {
436 $GLOBALS['pma_auth_server'] = $_COOKIE['pmaServer-' . $GLOBALS['server']];
440 if (empty($_COOKIE['pmaUser-' . $GLOBALS['server']])) {
444 $GLOBALS['PHP_AUTH_USER'] = PMA_blowfish_decrypt(
445 $_COOKIE['pmaUser-' . $GLOBALS['server']],
446 PMA_get_blowfish_secret());
448 // user was never logged in since session start
449 if (empty($_SESSION['last_access_time'])) {
453 // User inactive too long
454 if ($_SESSION['last_access_time'] < time() - $GLOBALS['cfg']['LoginCookieValidity']) {
455 PMA_cacheUnset('is_create_db_priv', true);
456 PMA_cacheUnset('is_process_priv', true);
457 PMA_cacheUnset('is_reload_priv', true);
458 PMA_cacheUnset('db_to_create', true);
459 PMA_cacheUnset('dbs_where_create_table_allowed', true);
460 $GLOBALS['no_activity'] = true;
466 if (empty($_COOKIE['pmaPass-' . $GLOBALS['server']])) {
470 $GLOBALS['PHP_AUTH_PW'] = PMA_blowfish_decrypt(
471 $_COOKIE['pmaPass-' . $GLOBALS['server']],
472 PMA_get_blowfish_secret());
474 if ($GLOBALS['PHP_AUTH_PW'] == "\xff(blank)") {
475 $GLOBALS['PHP_AUTH_PW'] = '';
478 $GLOBALS['from_cookie'] = true;
481 } // end of the 'PMA_auth_check()' function
485 * Set the user and password after last checkings if required
487 * @uses $GLOBALS['PHP_AUTH_USER']
488 * @uses $GLOBALS['PHP_AUTH_PW']
489 * @uses $GLOBALS['server']
490 * @uses $GLOBALS['from_cookie']
491 * @uses $GLOBALS['pma_auth_server']
492 * @uses $cfg['Server']
493 * @uses $cfg['AllowArbitraryServer']
494 * @uses $cfg['LoginCookieStore']
495 * @uses $cfg['PmaAbsoluteUri']
496 * @uses $_SESSION['last_access_time']
497 * @uses PMA_COMING_FROM_COOKIE_LOGIN
498 * @uses $GLOBALS['PMA_Config']->setCookie()
499 * @uses PMA_blowfish_encrypt()
500 * @uses $GLOBALS['PMA_Config']->removeCookie()
501 * @uses PMA_sendHeaderLocation()
504 * @return boolean always true
508 function PMA_auth_set_user()
512 // Ensures valid authentication mode, 'only_db', bookmark database and
513 // table names and relation table name are used
514 if ($cfg['Server']['user'] != $GLOBALS['PHP_AUTH_USER']) {
515 foreach ($cfg['Servers'] as $idx => $current) {
516 if ($current['host'] == $cfg['Server']['host']
517 && $current['port'] == $cfg['Server']['port']
518 && $current['socket'] == $cfg['Server']['socket']
519 && $current['ssl'] == $cfg['Server']['ssl']
520 && $current['connect_type'] == $cfg['Server']['connect_type']
521 && $current['user'] == $GLOBALS['PHP_AUTH_USER']) {
522 $GLOBALS['server'] = $idx;
523 $cfg['Server'] = $current;
529 if ($GLOBALS['cfg']['AllowArbitraryServer']
530 && ! empty($GLOBALS['pma_auth_server'])) {
531 /* Allow to specify 'host port' */
532 $parts = explode(' ', $GLOBALS['pma_auth_server']);
533 if (count($parts) == 2) {
534 $tmp_host = $parts[0];
535 $tmp_port = $parts[1];
537 $tmp_host = $GLOBALS['pma_auth_server'];
540 if ($cfg['Server']['host'] != $GLOBALS['pma_auth_server']) {
541 $cfg['Server']['host'] = $tmp_host;
542 if (!empty($tmp_port)) {
543 $cfg['Server']['port'] = $tmp_port;
546 unset($tmp_host, $tmp_port, $parts);
548 $cfg['Server']['user'] = $GLOBALS['PHP_AUTH_USER'];
549 $cfg['Server']['password'] = $GLOBALS['PHP_AUTH_PW'];
551 $_SESSION['last_access_time'] = time();
553 // Name and password cookies need to be refreshed each time
554 // Duration = one month for username
555 $GLOBALS['PMA_Config']->setCookie('pmaUser-' . $GLOBALS['server'],
556 PMA_blowfish_encrypt($cfg['Server']['user'],
557 PMA_get_blowfish_secret()));
559 // Duration = as configured
560 $GLOBALS['PMA_Config']->setCookie('pmaPass-' . $GLOBALS['server'],
561 PMA_blowfish_encrypt(!empty($cfg['Server']['password']) ?
$cfg['Server']['password'] : "\xff(blank)",
562 PMA_get_blowfish_secret()),
564 $GLOBALS['cfg']['LoginCookieStore']);
566 // Set server cookies if required (once per session) and, in this case, force
567 // reload to ensure the client accepts cookies
568 if (! $GLOBALS['from_cookie']) {
569 if ($GLOBALS['cfg']['AllowArbitraryServer']) {
570 if (! empty($GLOBALS['pma_auth_server'])) {
571 // Duration = one month for servername
572 $GLOBALS['PMA_Config']->setCookie('pmaServer-' . $GLOBALS['server'], $cfg['Server']['host']);
574 // Delete servername cookie
575 $GLOBALS['PMA_Config']->removeCookie('pmaServer-' . $GLOBALS['server']);
580 $redirect_url = $cfg['PmaAbsoluteUri'] . 'index.php';
582 // any parameters to pass?
583 $url_params = array();
584 if (strlen($GLOBALS['db'])) {
585 $url_params['db'] = $GLOBALS['db'];
587 if (strlen($GLOBALS['table'])) {
588 $url_params['table'] = $GLOBALS['table'];
590 // any target to pass?
591 if (! empty($GLOBALS['target']) && $GLOBALS['target'] != 'index.php') {
592 $url_params['target'] = $GLOBALS['target'];
596 * whether we come from a fresh cookie login
598 define('PMA_COMING_FROM_COOKIE_LOGIN', true);
603 PMA_clearUserCache();
605 PMA_sendHeaderLocation($redirect_url . PMA_generate_common_url($url_params, '&'));
610 } // end of the 'PMA_auth_set_user()' function
614 * User is not allowed to login to MySQL -> authentication failed
616 * prepares error message and switches to PMA_auth() which display the error
619 * this function MUST exit/quit the application,
620 * currently doen by call to PMA_auth()
622 * @uses $GLOBALS['server']
623 * @uses $GLOBALS['allowDeny_forbidden']
624 * @uses $GLOBALS['no_activity']
625 * @uses $cfg['LoginCookieValidity']
626 * @uses $GLOBALS['PMA_Config']->removeCookie()
628 * @uses PMA_DBI_getError()
629 * @uses PMA_sanitize()
635 function PMA_auth_fails()
639 // Deletes password cookie and displays the login form
640 $GLOBALS['PMA_Config']->removeCookie('pmaPass-' . $GLOBALS['server']);
642 if (! empty($GLOBALS['login_without_password_is_forbidden'])) {
643 $conn_error = __('Login without a password is forbidden by configuration (see AllowNoPassword)');
644 } elseif (! empty($GLOBALS['allowDeny_forbidden'])) {
645 $conn_error = __('Access denied');
646 } elseif (! empty($GLOBALS['no_activity'])) {
647 $conn_error = sprintf(__('No activity within %s seconds; please log in again'), $GLOBALS['cfg']['LoginCookieValidity']);
648 // Remember where we got timeout to return on same place
649 if (PMA_getenv('SCRIPT_NAME')) {
650 $GLOBALS['target'] = basename(PMA_getenv('SCRIPT_NAME'));
651 // avoid "missing parameter: field" on re-entry
652 if ('tbl_alter.php' == $GLOBALS['target']) {
653 $GLOBALS['target'] = 'tbl_structure.php';
656 } elseif (PMA_DBI_getError()) {
657 $conn_error = '#' . $GLOBALS['errno'] . ' ' . __('Cannot log in to the MySQL server');
659 $conn_error = __('Cannot log in to the MySQL server');
662 // needed for PHP-CGI (not need for FastCGI or mod-php)
663 header('Cache-Control: no-store, no-cache, must-revalidate');
664 header('Pragma: no-cache');
667 } // end of the 'PMA_auth_fails()' function