use session_destroy() instead of session_cleanup.lib.php
[phpmyadmin/crack.git] / libraries / auth / cookie.auth.lib.php
bloba695f1695543e3a4e64b126f6be75a478559d188
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 /**
12 * @todo replace by constant
13 * $coming_from_common can be set from outside with register_globals on
15 if (!isset($coming_from_common)) {
16 exit;
19 if (function_exists('mcrypt_encrypt') || PMA_dl('mcrypt')) {
20 /**
21 * Uses faster mcrypt library if available
22 * (Note: mcrypt.lib.php needs $cookie_path and $is_https)
24 require_once './libraries/mcrypt.lib.php';
25 } else {
26 require_once './libraries/blowfish.php';
27 /**
28 * display warning in main.php
30 define('PMA_WARN_FOR_MCRYPT', 1);
34 /**
35 * Displays authentication form
37 * this function MUST exit/quit the application
39 * @uses $GLOBALS['server']
40 * @uses $GLOBALS['PHP_AUTH_USER']
41 * @uses $GLOBALS['pma_auth_server']
42 * @uses $GLOBALS['text_dir']
43 * @uses $GLOBALS['pmaThemeImage']
44 * @uses $GLOBALS['charset']
45 * @uses $GLOBALS['target']
46 * @uses $GLOBALS['db']
47 * @uses $GLOBALS['table']
48 * @uses $GLOBALS['PMA_errors']
49 * @uses $GLOBALS['convcharset']
50 * @uses $GLOBALS['lang']
51 * @uses $GLOBALS['strWelcome']
52 * @uses $GLOBALS['strSecretRequired']
53 * @uses $GLOBALS['strError']
54 * @uses $GLOBALS['strLogin']
55 * @uses $GLOBALS['strLogServer']
56 * @uses $GLOBALS['strLogUsername']
57 * @uses $GLOBALS['strLogPassword']
58 * @uses $GLOBALS['strServerChoice']
59 * @uses $GLOBALS['strGo']
60 * @uses $GLOBALS['strCookiesRequired']
61 * @uses $GLOBALS['strPmaDocumentation']
62 * @uses $GLOBALS['pmaThemeImage']
63 * @uses $cfg['Servers']
64 * @uses $cfg['LoginCookieRecall']
65 * @uses $cfg['Lang']
66 * @uses $cfg['Server']
67 * @uses $cfg['ReplaceHelpImg']
68 * @uses $cfg['blowfish_secret']
69 * @uses $cfg['AllowArbitraryServer']
70 * @uses $_COOKIE
71 * @uses $_REQUEST['old_usr']
72 * @uses PMA_sendHeaderLocation()
73 * @uses PMA_select_language()
74 * @uses PMA_select_server()
75 * @uses PMA_VERSION
76 * @uses file_exists()
77 * @uses sprintf()
78 * @uses count()
79 * @uses htmlspecialchars()
80 * @uses is_array()
81 * @global string the last connection error
83 * @access public
85 function PMA_auth()
87 global $conn_error;
89 /* Perform logout to custom URL */
90 if (! empty($_REQUEST['old_usr'])
91 && ! empty($GLOBALS['cfg']['Server']['LogoutURL'])) {
92 PMA_sendHeaderLocation($GLOBALS['cfg']['Server']['LogoutURL']);
93 exit;
96 if ($GLOBALS['cfg']['LoginCookieRecall']) {
97 $default_user = $GLOBALS['PHP_AUTH_USER'];
98 $default_server = $GLOBALS['pma_auth_server'];
99 $autocomplete = '';
100 } else {
101 $default_user = '';
102 $default_server = '';
103 // skip the IE autocomplete feature.
104 $autocomplete = ' autocomplete="off"';
107 $cell_align = ($GLOBALS['text_dir'] == 'ltr') ? 'left' : 'right';
109 // Defines the charset to be used
110 header('Content-Type: text/html; charset=' . $GLOBALS['charset']);
111 // Defines the "item" image depending on text direction
112 $item_img = $GLOBALS['pmaThemeImage'] . 'item_' . $GLOBALS['text_dir'] . '.png';
114 /* HTML header */
115 $page_title = 'phpMyAdmin ' . PMA_VERSION;
116 require './libraries/header_meta_style.inc.php';
118 <script type="text/javascript">
119 //<![CDATA[
120 // show login form in top frame
121 if (top != self) {
122 window.top.location.href=location;
124 //]]>
125 </script>
126 </head>
128 <body class="loginform">
130 <?php
131 if (file_exists('./config.header.inc.php')) {
132 require './config.header.inc.php';
136 <div class="container">
137 <a href="http://www.phpmyadmin.net" target="_blank" class="logo"><?php
138 $logo_image = $GLOBALS['pmaThemeImage'] . 'logo_right.png';
139 if (@file_exists($logo_image)) {
140 echo '<img src="' . $logo_image . '" id="imLogo" name="imLogo" alt="phpMyAdmin" border="0" />';
141 } else {
142 echo '<img name="imLogo" id="imLogo" src="' . $GLOBALS['pmaThemeImage'] . 'pma_logo.png' . '" '
143 . 'border="0" width="88" height="31" alt="phpMyAdmin" />';
145 ?></a>
146 <h1>
147 <?php
148 echo sprintf($GLOBALS['strWelcome'],
149 '<bdo dir="ltr" xml:lang="en">' . $page_title . '</bdo>');
151 </h1>
152 <?php
154 // Show error message
155 if (! empty($conn_error)) {
156 echo '<div class="error"><h1>' . $GLOBALS['strError'] . '</h1>' . "\n";
157 echo $conn_error . '</div>' . "\n";
160 // Displays the languages form
161 if (empty($GLOBALS['cfg']['Lang'])) {
162 require_once './libraries/display_select_lang.lib.php';
163 PMA_select_language(true);
166 // Displays the warning message and the login form
167 if (empty($GLOBALS['cfg']['blowfish_secret'])) {
169 <div class="error"><h1><?php echo $GLOBALS['strError']; ?></h1>
170 <?php echo $GLOBALS['strSecretRequired']; ?>
171 </div>
172 <?php
173 echo '</div>' . "\n";
174 if (file_exists('./config.footer.inc.php')) {
175 require './config.footer.inc.php';
177 echo '</body></html>';
178 exit;
181 <br />
182 <!-- Login form -->
183 <form method="post" action="index.php" name="login_form"<?php echo $autocomplete; ?> target="_top" class="login">
184 <fieldset>
185 <legend>
186 <?php
187 echo $GLOBALS['strLogin'];
188 echo '<a href="./Documentation.html" target="documentation" ' .
189 'title="' . $GLOBALS['strPmaDocumentation'] . '">';
190 if ($GLOBALS['cfg']['ReplaceHelpImg']) {
191 echo '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_help.png" width="11" height="11" alt="' . $GLOBALS['strPmaDocumentation'] . '" />';
192 } else {
193 echo '(*)';
195 echo '</a>';
197 </legend>
199 <?php if ($GLOBALS['cfg']['AllowArbitraryServer']) { ?>
200 <div class="item">
201 <label for="input_servername"><?php echo $GLOBALS['strLogServer']; ?></label>
202 <input type="text" name="pma_servername" id="input_servername" value="<?php echo htmlspecialchars($default_server); ?>" size="24" class="textfield" />
203 </div>
204 <?php } ?>
205 <div class="item">
206 <label for="input_username"><?php echo $GLOBALS['strLogUsername']; ?></label>
207 <input type="text" name="pma_username" id="input_username" value="<?php echo htmlspecialchars($default_user); ?>" size="24" class="textfield" />
208 </div>
209 <div class="item">
210 <label for="input_password"><?php echo $GLOBALS['strLogPassword']; ?></label>
211 <input type="password" name="pma_password" id="input_password" value="" size="24" class="textfield" />
212 </div>
213 <?php
214 if (count($GLOBALS['cfg']['Servers']) > 1) {
216 <div class="item">
217 <label for="select_server"><?php echo $GLOBALS['strServerChoice']; ?>:</label>
218 <select name="server" id="select_server"
219 <?php
220 if ($GLOBALS['cfg']['AllowArbitraryServer']) {
221 echo ' onchange="document.forms[\'login_form\'].elements[\'pma_servername\'].value = \'\'" ';
223 echo '>';
225 require_once './libraries/select_server.lib.php';
226 PMA_select_server(false, false);
228 echo '</select></div>';
229 } else {
230 echo ' <input type="hidden" name="server" value="' . $GLOBALS['server'] . '" />';
231 } // end if (server choice)
233 </fieldset>
234 <fieldset class="tblFooters">
235 <input value="<?php echo $GLOBALS['strGo']; ?>" type="submit" />
236 <input type="hidden" name="lang" value="<?php echo $GLOBALS['lang']; ?>" />
237 <input type="hidden" name="convcharset" value="<?php echo $GLOBALS['convcharset']; ?>" />
238 <?php
239 if (!empty($GLOBALS['target'])) {
240 echo ' <input type="hidden" name="target" value="' . htmlspecialchars($GLOBALS['target']) . '" />' . "\n";
242 if (!empty($GLOBALS['db'])) {
243 echo ' <input type="hidden" name="db" value="' . htmlspecialchars($GLOBALS['db']) . '" />' . "\n";
245 if (!empty($GLOBALS['table'])) {
246 echo ' <input type="hidden" name="table" value="' . htmlspecialchars($GLOBALS['table']) . '" />' . "\n";
249 </fieldset>
250 </form>
251 <?php
252 // show the "Cookies required" message only if cookies are disabled
253 // (we previously tried to set some cookies)
254 if (empty($_COOKIE)) {
255 echo '<div class="notice">' . $GLOBALS['strCookiesRequired'] . '</div>' . "\n";
257 if (! empty($GLOBALS['PMA_errors']) && is_array($GLOBALS['PMA_errors'])) {
258 foreach ($GLOBALS['PMA_errors'] as $error) {
259 echo '<div class="error">' . $error . '</div>' . "\n";
263 </div>
264 <script type="text/javascript">
265 // <![CDATA[
266 function PMA_focusInput()
268 var input_username = document.getElementById('input_username');
269 var input_password = document.getElementById('input_password');
270 if (input_username.value == '') {
271 input_username.focus();
272 } else {
273 input_password.focus();
277 window.setTimeout('PMA_focusInput()', 500);
278 // ]]>
279 </script>
280 </body>
281 </html>
282 <?php
283 if (file_exists('./config.footer.inc.php')) {
284 require './config.footer.inc.php';
286 exit;
287 } // end of the 'PMA_auth()' function
291 * Gets advanced authentication settings
293 * this function DOES NOT check authentication - it just checks/provides
294 * authentication credentials required to connect to the MySQL server
295 * usally with PMA_DBI_connect()
297 * it returns false if there is missing something - which usally leads to
298 * PMA_auth() which displays login form
300 * it returns true if all seems ok which usally leads to PMA_auth_set_user()
302 * it directly switches to PMA_auth_fails() if user inactivity timout is reached
304 * @todo AllowArbitraryServer on does not imply that the user wnats an
305 * arbitrary server, or? so we should also check if this is filled and
306 * not only if allowed
307 * @uses $GLOBALS['PHP_AUTH_USER']
308 * @uses $GLOBALS['PHP_AUTH_PW']
309 * @uses $GLOBALS['no_activity']
310 * @uses $GLOBALS['server']
311 * @uses $GLOBALS['from_cookie']
312 * @uses $GLOBALS['pma_auth_server']
313 * @uses $cfg['blowfish_secret']
314 * @uses $cfg['AllowArbitraryServer']
315 * @uses $cfg['LoginCookieValidity']
316 * @uses $cfg['Servers']
317 * @uses $_REQUEST['old_usr'] from logout link
318 * @uses $_REQUEST['pma_username'] from login form
319 * @uses $_REQUEST['pma_password'] from login form
320 * @uses $_REQUEST['pma_servername'] from login form
321 * @uses $_COOKIE
322 * @uses $_SESSION['last_access_time']
323 * @uses PMA_removeCookie()
324 * @uses PMA_blowfish_decrypt()
325 * @uses PMA_auth_fails()
326 * @uses time()
328 * @return boolean whether we get authentication settings or not
330 * @access public
332 function PMA_auth_check()
334 // Initialization
336 * @global $GLOBALS['pma_auth_server'] the user provided server to connect to
338 $GLOBALS['pma_auth_server'] = '';
340 $GLOBALS['PHP_AUTH_USER'] = $GLOBALS['PHP_AUTH_PW'] = '';
341 $GLOBALS['from_cookie'] = false;
343 // avoid an error in mcrypt
344 if (empty($GLOBALS['cfg']['blowfish_secret'])) {
345 return false;
348 if (defined('PMA_CLEAR_COOKIES')) {
349 foreach($GLOBALS['cfg']['Servers'] as $key => $val) {
350 PMA_removeCookie('pmaPass-' . $key);
351 PMA_removeCookie('pmaServer-' . $key);
352 PMA_removeCookie('pmaUser-' . $key);
354 return false;
357 if (! empty($_REQUEST['old_usr'])) {
358 // The user wants to be logged out
359 // -> delete his choices that were stored in session
360 session_destroy();
361 // -> delete password cookie(s)
362 if ($GLOBALS['cfg']['LoginCookieDeleteAll']) {
363 foreach($GLOBALS['cfg']['Servers'] as $key => $val) {
364 PMA_removeCookie('pmaPass-' . $key);
365 if (isset($_COOKIE['pmaPass-' . $key])) {
366 unset($_COOKIE['pmaPass-' . $key]);
369 } else {
370 PMA_removeCookie('pmaPass-' . $GLOBALS['server']);
371 if (isset($_COOKIE['pmaPass-' . $GLOBALS['server']])) {
372 unset($_COOKIE['pmaPass-' . $GLOBALS['server']]);
377 if (! empty($_REQUEST['pma_username'])) {
378 // The user just logged in
379 $GLOBALS['PHP_AUTH_USER'] = $_REQUEST['pma_username'];
380 $GLOBALS['PHP_AUTH_PW'] = empty($_REQUEST['pma_password']) ? '' : $_REQUEST['pma_password'];
381 if ($GLOBALS['cfg']['AllowArbitraryServer']) {
382 $GLOBALS['pma_auth_server'] = $_REQUEST['pma_servername'];
384 return true;
387 // At the end, try to set the $GLOBALS['PHP_AUTH_USER']
388 // and $GLOBALS['PHP_AUTH_PW'] variables from cookies
390 // servername
391 if ($GLOBALS['cfg']['AllowArbitraryServer']
392 && ! empty($_COOKIE['pmaServer-' . $GLOBALS['server']])) {
393 $GLOBALS['pma_auth_server'] = $_COOKIE['pmaServer-' . $GLOBALS['server']];
396 // username
397 if (empty($_COOKIE['pmaUser-' . $GLOBALS['server']])) {
398 return false;
401 $GLOBALS['PHP_AUTH_USER'] = PMA_blowfish_decrypt(
402 $_COOKIE['pmaUser-' . $GLOBALS['server']],
403 $GLOBALS['cfg']['blowfish_secret']);
405 // user was never logged in since session start
406 if (empty($_SESSION['last_access_time'])) {
407 return false;
410 // User inactive too long
411 if ($_SESSION['last_access_time'] < time() - $GLOBALS['cfg']['LoginCookieValidity']) {
412 $GLOBALS['no_activity'] = true;
413 PMA_auth_fails();
414 exit;
417 // password
418 if (empty($_COOKIE['pmaPass-' . $GLOBALS['server']])) {
419 return false;
422 $GLOBALS['PHP_AUTH_PW'] = PMA_blowfish_decrypt(
423 $_COOKIE['pmaPass-' . $GLOBALS['server']],
424 $GLOBALS['cfg']['blowfish_secret'] /* . $_SESSION['last_access_time'] */);
426 if ($GLOBALS['PHP_AUTH_PW'] == "\xff(blank)") {
427 $GLOBALS['PHP_AUTH_PW'] = '';
430 $GLOBALS['from_cookie'] = true;
432 return true;
433 } // end of the 'PMA_auth_check()' function
437 * Set the user and password after last checkings if required
439 * @uses $GLOBALS['PHP_AUTH_USER']
440 * @uses $GLOBALS['PHP_AUTH_PW']
441 * @uses $GLOBALS['server']
442 * @uses $GLOBALS['from_cookie']
443 * @uses $GLOBALS['pma_auth_server']
444 * @uses $cfg['Server']
445 * @uses $cfg['AllowArbitraryServer']
446 * @uses $cfg['blowfish_secret']
447 * @uses $cfg['LoginCookieStore']
448 * @uses $cfg['PmaAbsoluteUri']
449 * @uses $_SESSION['last_access_time']
450 * @uses PMA_COMING_FROM_COOKIE_LOGIN
451 * @uses PMA_setCookie()
452 * @uses PMA_blowfish_encrypt()
453 * @uses PMA_removeCookie()
454 * @uses PMA_sendHeaderLocation()
455 * @uses time()
456 * @uses define()
457 * @return boolean always true
459 * @access public
461 function PMA_auth_set_user()
463 global $cfg;
465 // Ensures valid authentication mode, 'only_db', bookmark database and
466 // table names and relation table name are used
467 if ($cfg['Server']['user'] != $GLOBALS['PHP_AUTH_USER']) {
468 foreach ($cfg['Servers'] as $idx => $current) {
469 if ($current['host'] == $cfg['Server']['host']
470 && $current['port'] == $cfg['Server']['port']
471 && $current['socket'] == $cfg['Server']['socket']
472 && $current['ssl'] == $cfg['Server']['ssl']
473 && $current['connect_type'] == $cfg['Server']['connect_type']
474 && $current['user'] == $GLOBALS['PHP_AUTH_USER']) {
475 $GLOBALS['server'] = $idx;
476 $cfg['Server'] = $current;
477 break;
479 } // end foreach
480 } // end if
482 $pma_server_changed = false;
483 if ($GLOBALS['cfg']['AllowArbitraryServer']
484 && ! empty($GLOBALS['pma_auth_server'])
485 && $cfg['Server']['host'] != $GLOBALS['pma_auth_server']) {
486 $cfg['Server']['host'] = $GLOBALS['pma_auth_server'];
487 $pma_server_changed = true;
489 $cfg['Server']['user'] = $GLOBALS['PHP_AUTH_USER'];
490 $cfg['Server']['password'] = $GLOBALS['PHP_AUTH_PW'];
492 $_SESSION['last_access_time'] = time();
494 // Name and password cookies needs to be refreshed each time
495 // Duration = one month for username
496 PMA_setCookie('pmaUser-' . $GLOBALS['server'],
497 PMA_blowfish_encrypt($cfg['Server']['user'],
498 $GLOBALS['cfg']['blowfish_secret']));
500 // Duration = as configured
501 PMA_setCookie('pmaPass-' . $GLOBALS['server'],
502 PMA_blowfish_encrypt(!empty($cfg['Server']['password']) ? $cfg['Server']['password'] : "\xff(blank)",
503 $GLOBALS['cfg']['blowfish_secret'] /* . $_SESSION['last_access_time'] */),
504 null,
505 $GLOBALS['cfg']['LoginCookieStore']);
507 // Set server cookies if required (once per session) and, in this case, force
508 // reload to ensure the client accepts cookies
509 if (! $GLOBALS['from_cookie']) {
510 if ($GLOBALS['cfg']['AllowArbitraryServer']) {
511 if (! empty($GLOBALS['pma_auth_server'])) {
512 // Duration = one month for serverrname
513 PMA_setCookie('pmaServer-' . $GLOBALS['server'], $cfg['Server']['host']);
514 } else {
515 // Delete servername cookie
516 PMA_removeCookie('pmaServer-' . $GLOBALS['server']);
520 // URL where to go:
521 $redirect_url = $cfg['PmaAbsoluteUri'] . 'index.php';
523 // any parameters to pass?
524 $url_params = array();
525 if (strlen($GLOBALS['db'])) {
526 $url_params['db'] = $GLOBALS['db'];
528 if (strlen($GLOBALS['table'])) {
529 $url_params['table'] = $GLOBALS['table'];
531 // Language change from the login panel needs to be remembered
532 if (! empty($GLOBALS['lang'])) {
533 $url_params['lang'] = $GLOBALS['lang'];
535 // any target to pass?
536 if (! empty($GLOBALS['target']) && $GLOBALS['target'] != 'index.php') {
537 $url_params['target'] = $GLOBALS['target'];
541 * whether we come from a fresh cookie login
543 define('PMA_COMING_FROM_COOKIE_LOGIN', true);
544 PMA_sendHeaderLocation($redirect_url . PMA_generate_common_url($url_params, '&'));
545 exit();
546 } // end if
548 return true;
549 } // end of the 'PMA_auth_set_user()' function
553 * User is not allowed to login to MySQL -> authentication failed
555 * prepares error message and switches to PMA_auth() which display the error
556 * and the login form
558 * this function MUST exit/quit the application,
559 * currently doen by call to PMA_auth()
561 * @todo $php_errormsg is invalid here!? it will never be set in this scope
562 * @uses $GLOBALS['server']
563 * @uses $GLOBALS['allowDeny_forbidden']
564 * @uses $GLOBALS['strAccessDenied']
565 * @uses $GLOBALS['strNoActivity']
566 * @uses $GLOBALS['strCannotLogin']
567 * @uses $GLOBALS['no_activity']
568 * @uses $cfg['LoginCookieValidity']
569 * @uses PMA_removeCookie()
570 * @uses PMA_getenv()
571 * @uses PMA_DBI_getError()
572 * @uses PMA_sanitize()
573 * @uses PMA_auth()
574 * @uses sprintf()
575 * @uses basename()
576 * @access public
578 function PMA_auth_fails()
580 global $conn_error;
582 // Deletes password cookie and displays the login form
583 PMA_removeCookie('pmaPass-' . $GLOBALS['server']);
585 if (! empty($GLOBALS['allowDeny_forbidden'])) {
586 $conn_error = $GLOBALS['strAccessDenied'];
587 } elseif (! empty($GLOBALS['no_activity'])) {
588 $conn_error = sprintf($GLOBALS['strNoActivity'], $GLOBALS['cfg']['LoginCookieValidity']);
589 // Remember where we got timeout to return on same place
590 if (PMA_getenv('SCRIPT_NAME')) {
591 $GLOBALS['target'] = basename(PMA_getenv('SCRIPT_NAME'));
593 } elseif (PMA_DBI_getError()) {
594 $conn_error = PMA_sanitize(PMA_DBI_getError());
595 } elseif (isset($php_errormsg)) {
596 $conn_error = $php_errormsg;
597 } else {
598 $conn_error = $GLOBALS['strCannotLogin'];
601 PMA_auth();
602 } // end of the 'PMA_auth_fails()' function