2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to run cookie based authentication.
5 * Thanks to Piotr Roszatycki <d3xter at users.sourceforge.net> and
6 * Dan Wilson who built this patch for the Debian package.
8 * @package phpMyAdmin-Auth-Cookie
12 if (! defined('PHPMYADMIN')) {
17 * Swekey authentication functions.
19 require './libraries/auth/swekey/swekey.auth.lib.php';
21 if (function_exists('mcrypt_encrypt')) {
23 * Uses faster mcrypt library if available
24 * (as this is not called from anywhere else, put the code in-line
25 * for faster execution)
30 * Store the initialization vector because it will be needed for
31 * further decryption. I don't think necessary to have one iv
32 * per server so I don't put the server number in the cookie name.
34 if (empty($_COOKIE['pma_mcrypt_iv'])
35 ||
false === ($iv = base64_decode($_COOKIE['pma_mcrypt_iv'], true))) {
36 srand((double) microtime() * 1000000);
37 $td = mcrypt_module_open(MCRYPT_BLOWFISH
, '', MCRYPT_MODE_CBC
, '');
39 trigger_error(PMA_sanitize(sprintf($strCantLoad, 'mcrypt')), E_USER_WARNING
);
41 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND
);
42 PMA_setCookie('pma_mcrypt_iv', base64_encode($iv));
46 * Encryption using blowfish algorithm (mcrypt)
48 * @param string original data
49 * @param string the secret
51 * @return string the encrypted result
57 function PMA_blowfish_encrypt($data, $secret)
60 return base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH
, $secret, $data, MCRYPT_MODE_CBC
, $iv));
64 * Decryption using blowfish algorithm (mcrypt)
66 * @param string encrypted data
67 * @param string the secret
69 * @return string original data
75 function PMA_blowfish_decrypt($encdata, $secret)
78 return trim(mcrypt_decrypt(MCRYPT_BLOWFISH
, $secret, base64_decode($encdata), MCRYPT_MODE_CBC
, $iv));
82 require_once './libraries/blowfish.php';
83 if (!$GLOBALS['cfg']['McryptDisableWarning']) {
84 trigger_error(PMA_sanitize(sprintf($strCantLoad, 'mcrypt')), E_USER_WARNING
);
89 * Returns blowfish secret or generates one if needed.
90 * @uses $cfg['blowfish_secret']
91 * @uses $_SESSION['auto_blowfish_secret']
95 function PMA_get_blowfish_secret() {
96 if (empty($GLOBALS['cfg']['blowfish_secret'])) {
97 if (empty($_SESSION['auto_blowfish_secret'])) {
98 // this returns 23 characters
99 $_SESSION['auto_blowfish_secret'] = uniqid('', true);
101 return $_SESSION['auto_blowfish_secret'];
103 // apply md5() to work around too long secrets (returns 32 characters)
104 return md5($GLOBALS['cfg']['blowfish_secret']);
109 * Displays authentication form
111 * this function MUST exit/quit the application
113 * @uses $GLOBALS['server']
114 * @uses $GLOBALS['PHP_AUTH_USER']
115 * @uses $GLOBALS['pma_auth_server']
116 * @uses $GLOBALS['text_dir']
117 * @uses $GLOBALS['pmaThemeImage']
118 * @uses $GLOBALS['charset']
119 * @uses $GLOBALS['target']
120 * @uses $GLOBALS['db']
121 * @uses $GLOBALS['table']
122 * @uses $GLOBALS['strWelcome']
123 * @uses $GLOBALS['strSecretRequired']
124 * @uses $GLOBALS['strError']
125 * @uses $GLOBALS['strLogin']
126 * @uses $GLOBALS['strLogServer']
127 * @uses $GLOBALS['strLogUsername']
128 * @uses $GLOBALS['strLogPassword']
129 * @uses $GLOBALS['strServerChoice']
130 * @uses $GLOBALS['strGo']
131 * @uses $GLOBALS['strCookiesRequired']
132 * @uses $GLOBALS['strPmaDocumentation']
133 * @uses $GLOBALS['pmaThemeImage']
134 * @uses $cfg['Servers']
135 * @uses $cfg['LoginCookieRecall']
137 * @uses $cfg['Server']
138 * @uses $cfg['ReplaceHelpImg']
139 * @uses $cfg['blowfish_secret']
140 * @uses $cfg['AllowArbitraryServer']
142 * @uses $_REQUEST['old_usr']
143 * @uses PMA_sendHeaderLocation()
144 * @uses PMA_select_language()
145 * @uses PMA_select_server()
146 * @uses file_exists()
149 * @uses htmlspecialchars()
151 * @global string the last connection error
159 /* Perform logout to custom URL */
160 if (! empty($_REQUEST['old_usr'])
161 && ! empty($GLOBALS['cfg']['Server']['LogoutURL'])) {
162 PMA_sendHeaderLocation($GLOBALS['cfg']['Server']['LogoutURL']);
166 /* No recall if blowfish secret is not configured as it would produce garbage */
167 if ($GLOBALS['cfg']['LoginCookieRecall'] && !empty($GLOBALS['cfg']['blowfish_secret'])) {
168 $default_user = $GLOBALS['PHP_AUTH_USER'];
169 $default_server = $GLOBALS['pma_auth_server'];
173 $default_server = '';
174 // skip the IE autocomplete feature.
175 $autocomplete = ' autocomplete="off"';
178 $cell_align = ($GLOBALS['text_dir'] == 'ltr') ?
'left' : 'right';
180 // Defines the charset to be used
181 header('Content-Type: text/html; charset=' . $GLOBALS['charset']);
182 // Defines the "item" image depending on text direction
183 $item_img = $GLOBALS['pmaThemeImage'] . 'item_' . $GLOBALS['text_dir'] . '.png';
185 /* HTML header; do not show here the PMA version to improve security */
186 $page_title = 'phpMyAdmin ';
187 require './libraries/header_meta_style.inc.php';
189 <script type
="text/javascript">
191 // show login form in top frame
193 window
.top
.location
.href
=location
;
199 <body
class="loginform">
202 if (file_exists('./config.header.inc.php')) {
203 require './config.header.inc.php';
207 <div
class="container">
208 <a href
="http://www.phpmyadmin.net" target
="_blank" class="logo"><?php
209 $logo_image = $GLOBALS['pmaThemeImage'] . 'logo_right.png';
210 if (@file_exists
($logo_image)) {
211 echo '<img src="' . $logo_image . '" id="imLogo" name="imLogo" alt="phpMyAdmin" border="0" />';
213 echo '<img name="imLogo" id="imLogo" src="' . $GLOBALS['pmaThemeImage'] . 'pma_logo.png' . '" '
214 . 'border="0" width="88" height="31" alt="phpMyAdmin" />';
219 echo sprintf($GLOBALS['strWelcome'],
220 '<bdo dir="ltr" xml:lang="en">' . $page_title . '</bdo>');
225 // Show error message
226 if (! empty($conn_error)) {
227 PMA_Message
::rawError($conn_error)->display();
230 // Displays the languages form
231 if (empty($GLOBALS['cfg']['Lang'])) {
232 require_once './libraries/display_select_lang.lib.php';
233 // use fieldset, don't show doc link
234 PMA_select_language(true, false);
240 <form method
="post" action
="index.php" name
="login_form"<?php
echo $autocomplete; ?
> target
="_top" class="login">
244 echo $GLOBALS['strLogin'];
245 echo '<a href="./Documentation.html" target="documentation" ' .
246 'title="' . $GLOBALS['strPmaDocumentation'] . '">';
247 if ($GLOBALS['cfg']['ReplaceHelpImg']) {
248 echo '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_help.png" width="11" height="11" alt="' . $GLOBALS['strPmaDocumentation'] . '" />';
256 <?php
if ($GLOBALS['cfg']['AllowArbitraryServer']) { ?
>
258 <label
for="input_servername" title
="<?php echo $GLOBALS['strLogServerHelp']; ?>"><?php
echo $GLOBALS['strLogServer']; ?
></label
>
259 <input type
="text" name
="pma_servername" id
="input_servername" value
="<?php echo htmlspecialchars($default_server); ?>" size
="24" class="textfield" title
="<?php echo $GLOBALS['strLogServerHelp']; ?>" />
263 <label
for="input_username"><?php
echo $GLOBALS['strLogUsername']; ?
></label
>
264 <input type
="text" name
="pma_username" id
="input_username" value
="<?php echo htmlspecialchars($default_user); ?>" size
="24" class="textfield"/>
267 <label
for="input_password"><?php
echo $GLOBALS['strLogPassword']; ?
></label
>
268 <input type
="password" name
="pma_password" id
="input_password" value
="" size
="24" class="textfield" />
271 if (count($GLOBALS['cfg']['Servers']) > 1) {
274 <label
for="select_server"><?php
echo $GLOBALS['strServerChoice']; ?
>:</label
>
275 <select name
="server" id
="select_server"
277 if ($GLOBALS['cfg']['AllowArbitraryServer']) {
278 echo ' onchange="document.forms[\'login_form\'].elements[\'pma_servername\'].value = \'\'" ';
282 require_once './libraries/select_server.lib.php';
283 PMA_select_server(false, false);
285 echo '</select></div>';
287 echo ' <input type="hidden" name="server" value="' . $GLOBALS['server'] . '" />';
288 } // end if (server choice)
291 <fieldset
class="tblFooters">
292 <input value
="<?php echo $GLOBALS['strGo']; ?>" type
="submit" id
="input_go" />
294 $_form_params = array();
295 if (! empty($GLOBALS['target'])) {
296 $_form_params['target'] = $GLOBALS['target'];
298 if (! empty($GLOBALS['db'])) {
299 $_form_params['db'] = $GLOBALS['db'];
301 if (! empty($GLOBALS['table'])) {
302 $_form_params['table'] = $GLOBALS['table'];
304 // do not generate a "server" hidden field as we want the "server"
305 // drop-down to have priority
306 echo PMA_generate_common_hidden_inputs($_form_params, '', 0, 'server');
313 // BEGIN Swekey Integration
314 Swekey_login('input_username', 'input_go');
315 // END Swekey Integration
317 // show the "Cookies required" message only if cookies are disabled
318 // (we previously tried to set some cookies)
319 if (empty($_COOKIE)) {
320 trigger_error($GLOBALS['strCookiesRequired'], E_USER_NOTICE
);
322 if ($GLOBALS['error_handler']->hasDisplayErrors()) {
324 $GLOBALS['error_handler']->dispErrors();
329 <script type
="text/javascript">
331 function PMA_focusInput()
333 var input_username
= document
.getElementById('input_username');
334 var input_password
= document
.getElementById('input_password');
335 if (input_username
.value
== '') {
336 input_username
.focus();
338 input_password
.focus();
342 window
.setTimeout('PMA_focusInput()', 500);
346 if (file_exists('./config.footer.inc.php')) {
347 require './config.footer.inc.php';
354 } // end of the 'PMA_auth()' function
359 * Gets advanced authentication settings
361 * this function DOES NOT check authentication - it just checks/provides
362 * authentication credentials required to connect to the MySQL server
363 * usually with PMA_DBI_connect()
365 * it returns false if something is missing - which usually leads to
366 * PMA_auth() which displays login form
368 * it returns true if all seems ok which usually leads to PMA_auth_set_user()
370 * it directly switches to PMA_auth_fails() if user inactivity timout is reached
372 * @todo AllowArbitraryServer on does not imply that the user wants an
373 * arbitrary server, or? so we should also check if this is filled and
374 * not only if allowed
375 * @uses $GLOBALS['PHP_AUTH_USER']
376 * @uses $GLOBALS['PHP_AUTH_PW']
377 * @uses $GLOBALS['no_activity']
378 * @uses $GLOBALS['server']
379 * @uses $GLOBALS['from_cookie']
380 * @uses $GLOBALS['pma_auth_server']
381 * @uses $cfg['AllowArbitraryServer']
382 * @uses $cfg['LoginCookieValidity']
383 * @uses $cfg['Servers']
384 * @uses $_REQUEST['old_usr'] from logout link
385 * @uses $_REQUEST['pma_username'] from login form
386 * @uses $_REQUEST['pma_password'] from login form
387 * @uses $_REQUEST['pma_servername'] from login form
389 * @uses $_SESSION['last_access_time']
390 * @uses PMA_removeCookie()
391 * @uses PMA_blowfish_decrypt()
392 * @uses PMA_auth_fails()
395 * @return boolean whether we get authentication settings or not
399 function PMA_auth_check()
403 * @global $GLOBALS['pma_auth_server'] the user provided server to connect to
405 $GLOBALS['pma_auth_server'] = '';
407 $GLOBALS['PHP_AUTH_USER'] = $GLOBALS['PHP_AUTH_PW'] = '';
408 $GLOBALS['from_cookie'] = false;
410 // BEGIN Swekey Integration
411 if (! Swekey_auth_check()) {
414 // END Swekey Integration
416 if (defined('PMA_CLEAR_COOKIES')) {
417 foreach($GLOBALS['cfg']['Servers'] as $key => $val) {
418 PMA_removeCookie('pmaPass-' . $key);
419 PMA_removeCookie('pmaServer-' . $key);
420 PMA_removeCookie('pmaUser-' . $key);
425 if (! empty($_REQUEST['old_usr'])) {
426 // The user wants to be logged out
427 // -> delete his choices that were stored in session
429 // according to the PHP manual we should do this before the destroy:
430 //$_SESSION = array();
431 // but we still need some parts of the session information
432 // in libraries/header_meta_style.inc.php
435 // -> delete password cookie(s)
436 if ($GLOBALS['cfg']['LoginCookieDeleteAll']) {
437 foreach($GLOBALS['cfg']['Servers'] as $key => $val) {
438 PMA_removeCookie('pmaPass-' . $key);
439 if (isset($_COOKIE['pmaPass-' . $key])) {
440 unset($_COOKIE['pmaPass-' . $key]);
444 PMA_removeCookie('pmaPass-' . $GLOBALS['server']);
445 if (isset($_COOKIE['pmaPass-' . $GLOBALS['server']])) {
446 unset($_COOKIE['pmaPass-' . $GLOBALS['server']]);
451 if (! empty($_REQUEST['pma_username'])) {
452 // The user just logged in
453 $GLOBALS['PHP_AUTH_USER'] = $_REQUEST['pma_username'];
454 $GLOBALS['PHP_AUTH_PW'] = empty($_REQUEST['pma_password']) ?
'' : $_REQUEST['pma_password'];
455 if ($GLOBALS['cfg']['AllowArbitraryServer'] && isset($_REQUEST['pma_servername'])) {
456 $GLOBALS['pma_auth_server'] = $_REQUEST['pma_servername'];
461 // At the end, try to set the $GLOBALS['PHP_AUTH_USER']
462 // and $GLOBALS['PHP_AUTH_PW'] variables from cookies
465 if ($GLOBALS['cfg']['AllowArbitraryServer']
466 && ! empty($_COOKIE['pmaServer-' . $GLOBALS['server']])) {
467 $GLOBALS['pma_auth_server'] = $_COOKIE['pmaServer-' . $GLOBALS['server']];
471 if (empty($_COOKIE['pmaUser-' . $GLOBALS['server']])) {
475 $GLOBALS['PHP_AUTH_USER'] = PMA_blowfish_decrypt(
476 $_COOKIE['pmaUser-' . $GLOBALS['server']],
477 PMA_get_blowfish_secret());
479 // user was never logged in since session start
480 if (empty($_SESSION['last_access_time'])) {
484 // User inactive too long
485 if ($_SESSION['last_access_time'] < time() - $GLOBALS['cfg']['LoginCookieValidity']) {
486 PMA_cacheUnset('is_create_db_priv', true);
487 PMA_cacheUnset('is_process_priv', true);
488 PMA_cacheUnset('is_reload_priv', true);
489 PMA_cacheUnset('db_to_create', true);
490 PMA_cacheUnset('dbs_where_create_table_allowed', true);
491 $GLOBALS['no_activity'] = true;
497 if (empty($_COOKIE['pmaPass-' . $GLOBALS['server']])) {
501 $GLOBALS['PHP_AUTH_PW'] = PMA_blowfish_decrypt(
502 $_COOKIE['pmaPass-' . $GLOBALS['server']],
503 PMA_get_blowfish_secret());
505 if ($GLOBALS['PHP_AUTH_PW'] == "\xff(blank)") {
506 $GLOBALS['PHP_AUTH_PW'] = '';
509 $GLOBALS['from_cookie'] = true;
512 } // end of the 'PMA_auth_check()' function
516 * Set the user and password after last checkings if required
518 * @uses $GLOBALS['PHP_AUTH_USER']
519 * @uses $GLOBALS['PHP_AUTH_PW']
520 * @uses $GLOBALS['server']
521 * @uses $GLOBALS['from_cookie']
522 * @uses $GLOBALS['pma_auth_server']
523 * @uses $cfg['Server']
524 * @uses $cfg['AllowArbitraryServer']
525 * @uses $cfg['LoginCookieStore']
526 * @uses $cfg['PmaAbsoluteUri']
527 * @uses $_SESSION['last_access_time']
528 * @uses PMA_COMING_FROM_COOKIE_LOGIN
529 * @uses PMA_setCookie()
530 * @uses PMA_blowfish_encrypt()
531 * @uses PMA_removeCookie()
532 * @uses PMA_sendHeaderLocation()
535 * @return boolean always true
539 function PMA_auth_set_user()
543 // Ensures valid authentication mode, 'only_db', bookmark database and
544 // table names and relation table name are used
545 if ($cfg['Server']['user'] != $GLOBALS['PHP_AUTH_USER']) {
546 foreach ($cfg['Servers'] as $idx => $current) {
547 if ($current['host'] == $cfg['Server']['host']
548 && $current['port'] == $cfg['Server']['port']
549 && $current['socket'] == $cfg['Server']['socket']
550 && $current['ssl'] == $cfg['Server']['ssl']
551 && $current['connect_type'] == $cfg['Server']['connect_type']
552 && $current['user'] == $GLOBALS['PHP_AUTH_USER']) {
553 $GLOBALS['server'] = $idx;
554 $cfg['Server'] = $current;
560 if ($GLOBALS['cfg']['AllowArbitraryServer']
561 && ! empty($GLOBALS['pma_auth_server'])) {
562 /* Allow to specify 'host port' */
563 $parts = explode(' ', $GLOBALS['pma_auth_server']);
564 if (count($parts) == 2) {
565 $tmp_host = $parts[0];
566 $tmp_port = $parts[1];
568 $tmp_host = $GLOBALS['pma_auth_server'];
571 if ($cfg['Server']['host'] != $GLOBALS['pma_auth_server']) {
572 $cfg['Server']['host'] = $tmp_host;
573 if (!empty($tmp_port)) {
574 $cfg['Server']['port'] = $tmp_port;
577 unset($tmp_host, $tmp_port, $parts);
579 $cfg['Server']['user'] = $GLOBALS['PHP_AUTH_USER'];
580 $cfg['Server']['password'] = $GLOBALS['PHP_AUTH_PW'];
582 $_SESSION['last_access_time'] = time();
584 // Name and password cookies need to be refreshed each time
585 // Duration = one month for username
586 PMA_setCookie('pmaUser-' . $GLOBALS['server'],
587 PMA_blowfish_encrypt($cfg['Server']['user'],
588 PMA_get_blowfish_secret()));
590 // Duration = as configured
591 PMA_setCookie('pmaPass-' . $GLOBALS['server'],
592 PMA_blowfish_encrypt(!empty($cfg['Server']['password']) ?
$cfg['Server']['password'] : "\xff(blank)",
593 PMA_get_blowfish_secret()),
595 $GLOBALS['cfg']['LoginCookieStore']);
597 // Set server cookies if required (once per session) and, in this case, force
598 // reload to ensure the client accepts cookies
599 if (! $GLOBALS['from_cookie']) {
600 if ($GLOBALS['cfg']['AllowArbitraryServer']) {
601 if (! empty($GLOBALS['pma_auth_server'])) {
602 // Duration = one month for servername
603 PMA_setCookie('pmaServer-' . $GLOBALS['server'], $cfg['Server']['host']);
605 // Delete servername cookie
606 PMA_removeCookie('pmaServer-' . $GLOBALS['server']);
611 $redirect_url = $cfg['PmaAbsoluteUri'] . 'index.php';
613 // any parameters to pass?
614 $url_params = array();
615 if (strlen($GLOBALS['db'])) {
616 $url_params['db'] = $GLOBALS['db'];
618 if (strlen($GLOBALS['table'])) {
619 $url_params['table'] = $GLOBALS['table'];
621 // any target to pass?
622 if (! empty($GLOBALS['target']) && $GLOBALS['target'] != 'index.php') {
623 $url_params['target'] = $GLOBALS['target'];
627 * whether we come from a fresh cookie login
629 define('PMA_COMING_FROM_COOKIE_LOGIN', true);
630 PMA_sendHeaderLocation($redirect_url . PMA_generate_common_url($url_params, '&'));
635 } // end of the 'PMA_auth_set_user()' function
639 * User is not allowed to login to MySQL -> authentication failed
641 * prepares error message and switches to PMA_auth() which display the error
644 * this function MUST exit/quit the application,
645 * currently doen by call to PMA_auth()
647 * @uses $GLOBALS['server']
648 * @uses $GLOBALS['allowDeny_forbidden']
649 * @uses $GLOBALS['strAccessDenied']
650 * @uses $GLOBALS['strNoActivity']
651 * @uses $GLOBALS['strCannotLogin']
652 * @uses $GLOBALS['no_activity']
653 * @uses $cfg['LoginCookieValidity']
654 * @uses PMA_removeCookie()
656 * @uses PMA_DBI_getError()
657 * @uses PMA_sanitize()
663 function PMA_auth_fails()
667 // Deletes password cookie and displays the login form
668 PMA_removeCookie('pmaPass-' . $GLOBALS['server']);
670 if (! empty($GLOBALS['login_without_password_is_forbidden'])) {
671 $conn_error = $GLOBALS['strLoginWithoutPassword'];
672 } elseif (! empty($GLOBALS['allowDeny_forbidden'])) {
673 $conn_error = $GLOBALS['strAccessDenied'];
674 } elseif (! empty($GLOBALS['no_activity'])) {
675 $conn_error = sprintf($GLOBALS['strNoActivity'], $GLOBALS['cfg']['LoginCookieValidity']);
676 // Remember where we got timeout to return on same place
677 if (PMA_getenv('SCRIPT_NAME')) {
678 $GLOBALS['target'] = basename(PMA_getenv('SCRIPT_NAME'));
679 // avoid "missing parameter: field" on re-entry
680 if ('tbl_alter.php' == $GLOBALS['target']) {
681 $GLOBALS['target'] = 'tbl_structure.php';
684 } elseif (PMA_DBI_getError()) {
685 $conn_error = '#' . $GLOBALS['errno'] . ' ' . $GLOBALS['strCannotLogin'];
687 $conn_error = $GLOBALS['strCannotLogin'];
690 // needed for PHP-CGI (not need for FastCGI or mod-php)
691 header('Cache-Control: no-store, no-cache, must-revalidate');
692 header('Pragma: no-cache');
695 } // end of the 'PMA_auth_fails()' function