BLOB streaming documentation
[phpmyadmin/crack.git] / libraries / auth / cookie.auth.lib.php
blobc9f94ec549c331c162b0a5e38f6db54a85e76d8d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
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 * @version $Id$
9 */
11 if (! defined('PHPMYADMIN')) {
12 exit;
15 require './libraries/auth/swekey/swekey.auth.lib.php';
17 if (function_exists('mcrypt_encrypt')) {
18 /**
19 * Uses faster mcrypt library if available
20 * (as this is not called from anywhere else, put the code in-line
21 * for faster execution)
24 /**
25 * Initialization
26 * Store the initialization vector because it will be needed for
27 * further decryption. I don't think necessary to have one iv
28 * per server so I don't put the server number in the cookie name.
30 if (empty($_COOKIE['pma_mcrypt_iv'])
31 || false === ($iv = base64_decode($_COOKIE['pma_mcrypt_iv'], true))) {
32 srand((double) microtime() * 1000000);
33 $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC), MCRYPT_RAND);
34 PMA_setCookie('pma_mcrypt_iv', base64_encode($iv));
37 /**
38 * Encryption using blowfish algorithm (mcrypt)
40 * @param string original data
41 * @param string the secret
43 * @return string the encrypted result
45 * @access public
47 * @author lem9
49 function PMA_blowfish_encrypt($data, $secret)
51 global $iv;
52 return base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH, $secret, $data, MCRYPT_MODE_CBC, $iv));
55 /**
56 * Decryption using blowfish algorithm (mcrypt)
58 * @param string encrypted data
59 * @param string the secret
61 * @return string original data
63 * @access public
65 * @author lem9
67 function PMA_blowfish_decrypt($encdata, $secret)
69 global $iv;
70 return trim(mcrypt_decrypt(MCRYPT_BLOWFISH, $secret, base64_decode($encdata), MCRYPT_MODE_CBC, $iv));
73 } else {
74 require_once './libraries/blowfish.php';
75 trigger_error(PMA_sanitize(sprintf($strCantLoad, 'mcrypt')), E_USER_WARNING);
78 /**
79 * Returns blowfish secret or generates one if needed.
80 * @uses $cfg['blowfish_secret']
81 * @uses $_SESSION['auto_blowfish_secret']
83 * @access public
85 function PMA_get_blowfish_secret() {
86 if (empty($GLOBALS['cfg']['blowfish_secret'])) {
87 if (empty($_SESSION['auto_blowfish_secret'])) {
88 $_SESSION['auto_blowfish_secret'] = uniqid('', true);
90 return $_SESSION['auto_blowfish_secret'];
91 } else {
92 return $GLOBALS['cfg']['blowfish_secret'];
96 /**
97 * Displays authentication form
99 * this function MUST exit/quit the application
101 * @uses $GLOBALS['server']
102 * @uses $GLOBALS['PHP_AUTH_USER']
103 * @uses $GLOBALS['pma_auth_server']
104 * @uses $GLOBALS['text_dir']
105 * @uses $GLOBALS['pmaThemeImage']
106 * @uses $GLOBALS['charset']
107 * @uses $GLOBALS['target']
108 * @uses $GLOBALS['db']
109 * @uses $GLOBALS['table']
110 * @uses $GLOBALS['strWelcome']
111 * @uses $GLOBALS['strSecretRequired']
112 * @uses $GLOBALS['strError']
113 * @uses $GLOBALS['strLogin']
114 * @uses $GLOBALS['strLogServer']
115 * @uses $GLOBALS['strLogUsername']
116 * @uses $GLOBALS['strLogPassword']
117 * @uses $GLOBALS['strServerChoice']
118 * @uses $GLOBALS['strGo']
119 * @uses $GLOBALS['strCookiesRequired']
120 * @uses $GLOBALS['strPmaDocumentation']
121 * @uses $GLOBALS['pmaThemeImage']
122 * @uses $cfg['Servers']
123 * @uses $cfg['LoginCookieRecall']
124 * @uses $cfg['Lang']
125 * @uses $cfg['Server']
126 * @uses $cfg['ReplaceHelpImg']
127 * @uses $cfg['blowfish_secret']
128 * @uses $cfg['AllowArbitraryServer']
129 * @uses $_COOKIE
130 * @uses $_REQUEST['old_usr']
131 * @uses PMA_sendHeaderLocation()
132 * @uses PMA_select_language()
133 * @uses PMA_select_server()
134 * @uses file_exists()
135 * @uses sprintf()
136 * @uses count()
137 * @uses htmlspecialchars()
138 * @uses is_array()
139 * @global string the last connection error
141 * @access public
143 function PMA_auth()
145 global $conn_error;
147 /* Perform logout to custom URL */
148 if (! empty($_REQUEST['old_usr'])
149 && ! empty($GLOBALS['cfg']['Server']['LogoutURL'])) {
150 PMA_sendHeaderLocation($GLOBALS['cfg']['Server']['LogoutURL']);
151 exit;
154 /* No recall if blowfish secret is not configured as it would produce garbage */
155 if ($GLOBALS['cfg']['LoginCookieRecall'] && !empty($GLOBALS['cfg']['blowfish_secret'])) {
156 $default_user = $GLOBALS['PHP_AUTH_USER'];
157 $default_server = $GLOBALS['pma_auth_server'];
158 $autocomplete = '';
159 } else {
160 $default_user = '';
161 $default_server = '';
162 // skip the IE autocomplete feature.
163 $autocomplete = ' autocomplete="off"';
166 $cell_align = ($GLOBALS['text_dir'] == 'ltr') ? 'left' : 'right';
168 // Defines the charset to be used
169 header('Content-Type: text/html; charset=' . $GLOBALS['charset']);
170 // Defines the "item" image depending on text direction
171 $item_img = $GLOBALS['pmaThemeImage'] . 'item_' . $GLOBALS['text_dir'] . '.png';
173 /* HTML header; do not show here the PMA version to improve security */
174 $page_title = 'phpMyAdmin ';
175 require './libraries/header_meta_style.inc.php';
177 <script type="text/javascript">
178 //<![CDATA[
179 // show login form in top frame
180 if (top != self) {
181 window.top.location.href=location;
183 //]]>
184 </script>
185 </head>
187 <body class="loginform">
189 <?php
190 if (file_exists('./config.header.inc.php')) {
191 require './config.header.inc.php';
195 <div class="container">
196 <a href="http://www.phpmyadmin.net" target="_blank" class="logo"><?php
197 $logo_image = $GLOBALS['pmaThemeImage'] . 'logo_right.png';
198 if (@file_exists($logo_image)) {
199 echo '<img src="' . $logo_image . '" id="imLogo" name="imLogo" alt="phpMyAdmin" border="0" />';
200 } else {
201 echo '<img name="imLogo" id="imLogo" src="' . $GLOBALS['pmaThemeImage'] . 'pma_logo.png' . '" '
202 . 'border="0" width="88" height="31" alt="phpMyAdmin" />';
204 ?></a>
205 <h1>
206 <?php
207 echo sprintf($GLOBALS['strWelcome'],
208 '<bdo dir="ltr" xml:lang="en">' . $page_title . '</bdo>');
210 </h1>
211 <?php
213 // Show error message
214 if (! empty($conn_error)) {
215 PMA_Message::rawError($conn_error)->display();
218 // Displays the languages form
219 if (empty($GLOBALS['cfg']['Lang'])) {
220 require_once './libraries/display_select_lang.lib.php';
221 // use fieldset, don't show doc link
222 PMA_select_language(true, false);
226 <br />
227 <!-- Login form -->
228 <form method="post" action="index.php" name="login_form"<?php echo $autocomplete; ?> target="_top" class="login">
229 <fieldset>
230 <legend>
231 <?php
232 echo $GLOBALS['strLogin'];
233 // no real need to put a link to doc here, and it would reveal the
234 // version number
236 </legend>
238 <?php if ($GLOBALS['cfg']['AllowArbitraryServer']) { ?>
239 <div class="item">
240 <label for="input_servername" title="<?php echo $GLOBALS['strLogServerHelp']; ?>"><?php echo $GLOBALS['strLogServer']; ?></label>
241 <input type="text" name="pma_servername" id="input_servername" value="<?php echo htmlspecialchars($default_server); ?>" size="24" class="textfield" title="<?php echo $GLOBALS['strLogServerHelp']; ?>" />
242 </div>
243 <?php } ?>
244 <div class="item">
245 <label for="input_username"><?php echo $GLOBALS['strLogUsername']; ?></label>
246 <input type="text" name="pma_username" id="input_username" value="<?php echo htmlspecialchars($default_user); ?>" size="24" class="textfield"/>
247 </div>
248 <div class="item">
249 <label for="input_password"><?php echo $GLOBALS['strLogPassword']; ?></label>
250 <input type="password" name="pma_password" id="input_password" value="" size="24" class="textfield" />
251 </div>
252 <?php
253 if (count($GLOBALS['cfg']['Servers']) > 1) {
255 <div class="item">
256 <label for="select_server"><?php echo $GLOBALS['strServerChoice']; ?>:</label>
257 <select name="server" id="select_server"
258 <?php
259 if ($GLOBALS['cfg']['AllowArbitraryServer']) {
260 echo ' onchange="document.forms[\'login_form\'].elements[\'pma_servername\'].value = \'\'" ';
262 echo '>';
264 require_once './libraries/select_server.lib.php';
265 PMA_select_server(false, false);
267 echo '</select></div>';
268 } else {
269 echo ' <input type="hidden" name="server" value="' . $GLOBALS['server'] . '" />';
270 } // end if (server choice)
272 </fieldset>
273 <fieldset class="tblFooters">
274 <input value="<?php echo $GLOBALS['strGo']; ?>" type="submit" id="input_go" />
275 <?php
276 $_form_params = array();
277 if (! empty($GLOBALS['target'])) {
278 $_form_params['target'] = $GLOBALS['target'];
280 if (! empty($GLOBALS['db'])) {
281 $_form_params['db'] = $GLOBALS['db'];
283 if (! empty($GLOBALS['table'])) {
284 $_form_params['table'] = $GLOBALS['table'];
286 // do not generate a "server" hidden field as we want the "server"
287 // drop-down to have priority
288 echo PMA_generate_common_hidden_inputs($_form_params, '', 0, 'server');
290 </fieldset>
291 </form>
293 <?php
295 // BEGIN Swekey Integration
296 Swekey_login('input_username', 'input_go');
297 // END Swekey Integration
299 // show the "Cookies required" message only if cookies are disabled
300 // (we previously tried to set some cookies)
301 if (empty($_COOKIE)) {
302 trigger_error($GLOBALS['strCookiesRequired'], E_USER_NOTICE);
304 if ($GLOBALS['error_handler']->hasDisplayErrors()) {
305 echo '<div>';
306 $GLOBALS['error_handler']->dispErrors();
307 echo '</div>';
310 </div>
311 <script type="text/javascript">
312 // <![CDATA[
313 function PMA_focusInput()
315 var input_username = document.getElementById('input_username');
316 var input_password = document.getElementById('input_password');
317 if (input_username.value == '') {
318 input_username.focus();
319 } else {
320 input_password.focus();
324 window.setTimeout('PMA_focusInput()', 500);
325 // ]]>
326 </script>
327 <?php
328 if (file_exists('./config.footer.inc.php')) {
329 require './config.footer.inc.php';
332 </body>
333 </html>
334 <?php
335 exit;
336 } // end of the 'PMA_auth()' function
341 * Gets advanced authentication settings
343 * this function DOES NOT check authentication - it just checks/provides
344 * authentication credentials required to connect to the MySQL server
345 * usually with PMA_DBI_connect()
347 * it returns false if something is missing - which usually leads to
348 * PMA_auth() which displays login form
350 * it returns true if all seems ok which usually leads to PMA_auth_set_user()
352 * it directly switches to PMA_auth_fails() if user inactivity timout is reached
354 * @todo AllowArbitraryServer on does not imply that the user wants an
355 * arbitrary server, or? so we should also check if this is filled and
356 * not only if allowed
357 * @uses $GLOBALS['PHP_AUTH_USER']
358 * @uses $GLOBALS['PHP_AUTH_PW']
359 * @uses $GLOBALS['no_activity']
360 * @uses $GLOBALS['server']
361 * @uses $GLOBALS['from_cookie']
362 * @uses $GLOBALS['pma_auth_server']
363 * @uses $cfg['AllowArbitraryServer']
364 * @uses $cfg['LoginCookieValidity']
365 * @uses $cfg['Servers']
366 * @uses $_REQUEST['old_usr'] from logout link
367 * @uses $_REQUEST['pma_username'] from login form
368 * @uses $_REQUEST['pma_password'] from login form
369 * @uses $_REQUEST['pma_servername'] from login form
370 * @uses $_COOKIE
371 * @uses $_SESSION['last_access_time']
372 * @uses PMA_removeCookie()
373 * @uses PMA_blowfish_decrypt()
374 * @uses PMA_auth_fails()
375 * @uses time()
377 * @return boolean whether we get authentication settings or not
379 * @access public
381 function PMA_auth_check()
383 // Initialization
385 * @global $GLOBALS['pma_auth_server'] the user provided server to connect to
387 $GLOBALS['pma_auth_server'] = '';
389 $GLOBALS['PHP_AUTH_USER'] = $GLOBALS['PHP_AUTH_PW'] = '';
390 $GLOBALS['from_cookie'] = false;
392 // BEGIN Swekey Integration
393 if (! Swekey_auth_check()) {
394 return false;
396 // END Swekey Integration
398 if (defined('PMA_CLEAR_COOKIES')) {
399 foreach($GLOBALS['cfg']['Servers'] as $key => $val) {
400 PMA_removeCookie('pmaPass-' . $key);
401 PMA_removeCookie('pmaServer-' . $key);
402 PMA_removeCookie('pmaUser-' . $key);
404 return false;
407 if (! empty($_REQUEST['old_usr'])) {
408 // The user wants to be logged out
409 // -> delete his choices that were stored in session
411 // according to the PHP manual we should do this before the destroy:
412 //$_SESSION = array();
413 // but we still need some parts of the session information
414 // in libraries/header_meta_style.inc.php
416 session_destroy();
417 // -> delete password cookie(s)
418 if ($GLOBALS['cfg']['LoginCookieDeleteAll']) {
419 foreach($GLOBALS['cfg']['Servers'] as $key => $val) {
420 PMA_removeCookie('pmaPass-' . $key);
421 if (isset($_COOKIE['pmaPass-' . $key])) {
422 unset($_COOKIE['pmaPass-' . $key]);
425 } else {
426 PMA_removeCookie('pmaPass-' . $GLOBALS['server']);
427 if (isset($_COOKIE['pmaPass-' . $GLOBALS['server']])) {
428 unset($_COOKIE['pmaPass-' . $GLOBALS['server']]);
433 if (! empty($_REQUEST['pma_username'])) {
434 // The user just logged in
435 $GLOBALS['PHP_AUTH_USER'] = $_REQUEST['pma_username'];
436 $GLOBALS['PHP_AUTH_PW'] = empty($_REQUEST['pma_password']) ? '' : $_REQUEST['pma_password'];
437 if ($GLOBALS['cfg']['AllowArbitraryServer'] && isset($_REQUEST['pma_servername'])) {
438 $GLOBALS['pma_auth_server'] = $_REQUEST['pma_servername'];
440 return true;
443 // At the end, try to set the $GLOBALS['PHP_AUTH_USER']
444 // and $GLOBALS['PHP_AUTH_PW'] variables from cookies
446 // servername
447 if ($GLOBALS['cfg']['AllowArbitraryServer']
448 && ! empty($_COOKIE['pmaServer-' . $GLOBALS['server']])) {
449 $GLOBALS['pma_auth_server'] = $_COOKIE['pmaServer-' . $GLOBALS['server']];
452 // username
453 if (empty($_COOKIE['pmaUser-' . $GLOBALS['server']])) {
454 return false;
457 $GLOBALS['PHP_AUTH_USER'] = PMA_blowfish_decrypt(
458 $_COOKIE['pmaUser-' . $GLOBALS['server']],
459 PMA_get_blowfish_secret());
461 // user was never logged in since session start
462 if (empty($_SESSION['last_access_time'])) {
463 return false;
466 // User inactive too long
467 if ($_SESSION['last_access_time'] < time() - $GLOBALS['cfg']['LoginCookieValidity']) {
468 PMA_cacheUnset('is_create_db_priv', true);
469 PMA_cacheUnset('is_process_priv', true);
470 PMA_cacheUnset('is_reload_priv', true);
471 PMA_cacheUnset('db_to_create', true);
472 PMA_cacheUnset('dbs_where_create_table_allowed', true);
473 $GLOBALS['no_activity'] = true;
474 PMA_auth_fails();
475 exit;
478 // password
479 if (empty($_COOKIE['pmaPass-' . $GLOBALS['server']])) {
480 return false;
483 $GLOBALS['PHP_AUTH_PW'] = PMA_blowfish_decrypt(
484 $_COOKIE['pmaPass-' . $GLOBALS['server']],
485 PMA_get_blowfish_secret());
487 if ($GLOBALS['PHP_AUTH_PW'] == "\xff(blank)") {
488 $GLOBALS['PHP_AUTH_PW'] = '';
491 $GLOBALS['from_cookie'] = true;
493 return true;
494 } // end of the 'PMA_auth_check()' function
498 * Set the user and password after last checkings if required
500 * @uses $GLOBALS['PHP_AUTH_USER']
501 * @uses $GLOBALS['PHP_AUTH_PW']
502 * @uses $GLOBALS['server']
503 * @uses $GLOBALS['from_cookie']
504 * @uses $GLOBALS['pma_auth_server']
505 * @uses $cfg['Server']
506 * @uses $cfg['AllowArbitraryServer']
507 * @uses $cfg['LoginCookieStore']
508 * @uses $cfg['PmaAbsoluteUri']
509 * @uses $_SESSION['last_access_time']
510 * @uses PMA_COMING_FROM_COOKIE_LOGIN
511 * @uses PMA_setCookie()
512 * @uses PMA_blowfish_encrypt()
513 * @uses PMA_removeCookie()
514 * @uses PMA_sendHeaderLocation()
515 * @uses time()
516 * @uses define()
517 * @return boolean always true
519 * @access public
521 function PMA_auth_set_user()
523 global $cfg;
525 // Ensures valid authentication mode, 'only_db', bookmark database and
526 // table names and relation table name are used
527 if ($cfg['Server']['user'] != $GLOBALS['PHP_AUTH_USER']) {
528 foreach ($cfg['Servers'] as $idx => $current) {
529 if ($current['host'] == $cfg['Server']['host']
530 && $current['port'] == $cfg['Server']['port']
531 && $current['socket'] == $cfg['Server']['socket']
532 && $current['ssl'] == $cfg['Server']['ssl']
533 && $current['connect_type'] == $cfg['Server']['connect_type']
534 && $current['user'] == $GLOBALS['PHP_AUTH_USER']) {
535 $GLOBALS['server'] = $idx;
536 $cfg['Server'] = $current;
537 break;
539 } // end foreach
540 } // end if
542 if ($GLOBALS['cfg']['AllowArbitraryServer']
543 && ! empty($GLOBALS['pma_auth_server'])) {
544 /* Allow to specify 'host port' */
545 $parts = explode(' ', $GLOBALS['pma_auth_server']);
546 if (count($parts) == 2) {
547 $tmp_host = $parts[0];
548 $tmp_port = $parts[1];
549 } else {
550 $tmp_host = $GLOBALS['pma_auth_server'];
551 $tmp_port = '';
553 if ($cfg['Server']['host'] != $GLOBALS['pma_auth_server']) {
554 $cfg['Server']['host'] = $tmp_host;
555 if (!empty($tmp_port)) {
556 $cfg['Server']['port'] = $tmp_port;
559 unset($tmp_host, $tmp_port, $parts);
561 $cfg['Server']['user'] = $GLOBALS['PHP_AUTH_USER'];
562 $cfg['Server']['password'] = $GLOBALS['PHP_AUTH_PW'];
564 $_SESSION['last_access_time'] = time();
566 // Name and password cookies need to be refreshed each time
567 // Duration = one month for username
568 PMA_setCookie('pmaUser-' . $GLOBALS['server'],
569 PMA_blowfish_encrypt($cfg['Server']['user'],
570 PMA_get_blowfish_secret()));
572 // Duration = as configured
573 PMA_setCookie('pmaPass-' . $GLOBALS['server'],
574 PMA_blowfish_encrypt(!empty($cfg['Server']['password']) ? $cfg['Server']['password'] : "\xff(blank)",
575 PMA_get_blowfish_secret()),
576 null,
577 $GLOBALS['cfg']['LoginCookieStore']);
579 // Set server cookies if required (once per session) and, in this case, force
580 // reload to ensure the client accepts cookies
581 if (! $GLOBALS['from_cookie']) {
582 if ($GLOBALS['cfg']['AllowArbitraryServer']) {
583 if (! empty($GLOBALS['pma_auth_server'])) {
584 // Duration = one month for servername
585 PMA_setCookie('pmaServer-' . $GLOBALS['server'], $cfg['Server']['host']);
586 } else {
587 // Delete servername cookie
588 PMA_removeCookie('pmaServer-' . $GLOBALS['server']);
592 // URL where to go:
593 $redirect_url = $cfg['PmaAbsoluteUri'] . 'index.php';
595 // any parameters to pass?
596 $url_params = array();
597 if (strlen($GLOBALS['db'])) {
598 $url_params['db'] = $GLOBALS['db'];
600 if (strlen($GLOBALS['table'])) {
601 $url_params['table'] = $GLOBALS['table'];
603 // any target to pass?
604 if (! empty($GLOBALS['target']) && $GLOBALS['target'] != 'index.php') {
605 $url_params['target'] = $GLOBALS['target'];
609 * whether we come from a fresh cookie login
611 define('PMA_COMING_FROM_COOKIE_LOGIN', true);
612 PMA_sendHeaderLocation($redirect_url . PMA_generate_common_url($url_params, '&'));
613 exit();
614 } // end if
616 return true;
617 } // end of the 'PMA_auth_set_user()' function
621 * User is not allowed to login to MySQL -> authentication failed
623 * prepares error message and switches to PMA_auth() which display the error
624 * and the login form
626 * this function MUST exit/quit the application,
627 * currently doen by call to PMA_auth()
629 * @todo $php_errormsg is invalid here!? it will never be set in this scope
630 * @uses $GLOBALS['server']
631 * @uses $GLOBALS['allowDeny_forbidden']
632 * @uses $GLOBALS['strAccessDenied']
633 * @uses $GLOBALS['strNoActivity']
634 * @uses $GLOBALS['strCannotLogin']
635 * @uses $GLOBALS['no_activity']
636 * @uses $cfg['LoginCookieValidity']
637 * @uses PMA_removeCookie()
638 * @uses PMA_getenv()
639 * @uses PMA_DBI_getError()
640 * @uses PMA_sanitize()
641 * @uses PMA_auth()
642 * @uses sprintf()
643 * @uses basename()
644 * @access public
646 function PMA_auth_fails()
648 global $conn_error;
650 // Deletes password cookie and displays the login form
651 PMA_removeCookie('pmaPass-' . $GLOBALS['server']);
653 if (! empty($GLOBALS['allowDeny_forbidden'])) {
654 $conn_error = $GLOBALS['strAccessDenied'];
655 } elseif (! empty($GLOBALS['no_activity'])) {
656 $conn_error = sprintf($GLOBALS['strNoActivity'], $GLOBALS['cfg']['LoginCookieValidity']);
657 // Remember where we got timeout to return on same place
658 if (PMA_getenv('SCRIPT_NAME')) {
659 $GLOBALS['target'] = basename(PMA_getenv('SCRIPT_NAME'));
660 // avoid "missing parameter: field" on re-entry
661 if ('tbl_alter.php' == $GLOBALS['target']) {
662 $GLOBALS['target'] = 'tbl_structure.php';
665 } elseif (PMA_DBI_getError()) {
666 $conn_error = PMA_sanitize(PMA_DBI_getError());
667 } elseif (isset($php_errormsg)) {
668 $conn_error = $php_errormsg;
669 } else {
670 $conn_error = $GLOBALS['strCannotLogin'];
673 PMA_auth();
674 } // end of the 'PMA_auth_fails()' function