2007-07-02 05:51:26 +00:00
< ? php
2010-04-21 12:01:32 +00:00
$connection = '' ;
2013-10-25 02:10:50 +00:00
$has_token = $_SESSION [ " token " ];
if ( ! $has_token ) {
2010-04-21 12:01:32 +00:00
$_SESSION [ " token " ] = rand ( 1 , 1e6 ); // defense against cross-site request forgery
}
2013-10-25 02:10:50 +00:00
$token = get_token (); ///< @var string CSRF protection
2010-04-21 12:01:32 +00:00
2010-05-07 14:31:14 +00:00
$permanent = array ();
if ( $_COOKIE [ " adminer_permanent " ]) {
foreach ( explode ( " " , $_COOKIE [ " adminer_permanent " ]) as $val ) {
list ( $key ) = explode ( " : " , $val );
$permanent [ $key ] = $val ;
}
}
2014-03-22 05:47:34 +00:00
function add_invalid_login () {
global $adminer ;
2018-01-24 11:04:53 +00:00
$fp = file_open_lock ( get_temp_dir () . " /adminer.invalid " );
if ( ! $fp ) {
return ;
2014-03-22 05:47:34 +00:00
}
$invalids = unserialize ( stream_get_contents ( $fp ));
$time = time ();
if ( $invalids ) {
foreach ( $invalids as $ip => $val ) {
if ( $val [ 0 ] < $time ) {
unset ( $invalids [ $ip ]);
}
}
}
$invalid = & $invalids [ $adminer -> bruteForceKey ()];
if ( ! $invalid ) {
$invalid = array ( $time + 30 * 60 , 0 ); // active for 30 minutes
}
$invalid [ 1 ] ++ ;
2018-01-24 11:04:53 +00:00
file_write_unlock ( $fp , serialize ( $invalids ));
2014-03-22 05:47:34 +00:00
}
2018-01-16 09:56:35 +00:00
function check_invalid_login () {
global $adminer ;
2014-03-22 05:47:34 +00:00
$invalids = unserialize ( @ file_get_contents ( get_temp_dir () . " /adminer.invalid " )); // @ - may not exist
2021-02-12 08:53:20 +00:00
$invalid = ( $invalids ? $invalids [ $adminer -> bruteForceKey ()] : array ());
2018-01-24 11:04:53 +00:00
$next_attempt = ( $invalid [ 1 ] > 29 ? $invalid [ 0 ] - time () : 0 ); // allow 30 invalid attempts
2014-03-22 05:47:34 +00:00
if ( $next_attempt > 0 ) { //! do the same with permanent login
auth_error ( lang ( 'Too many unsuccessful logins, try again in %d minute(s).' , ceil ( $next_attempt / 60 )));
}
2018-01-16 09:56:35 +00:00
}
$auth = $_POST [ " auth " ];
if ( $auth ) {
2009-08-30 22:21:36 +00:00
session_regenerate_id (); // defense against session fixation
2014-04-21 16:45:15 +00:00
$vendor = $auth [ " driver " ];
2013-08-11 02:11:35 +00:00
$server = $auth [ " server " ];
$username = $auth [ " username " ];
2014-02-21 16:53:58 +00:00
$password = ( string ) $auth [ " password " ];
2013-08-11 02:11:35 +00:00
$db = $auth [ " db " ];
2014-04-21 16:45:15 +00:00
set_password ( $vendor , $server , $username , $password );
$_SESSION [ " db " ][ $vendor ][ $server ][ $username ][ $db ] = true ;
2013-10-25 05:40:05 +00:00
if ( $auth [ " permanent " ]) {
2014-04-21 16:45:15 +00:00
$key = base64_encode ( $vendor ) . " - " . base64_encode ( $server ) . " - " . base64_encode ( $username ) . " - " . base64_encode ( $db );
2013-06-11 09:02:17 +00:00
$private = $adminer -> permanentLogin ( true );
2013-08-11 02:11:35 +00:00
$permanent [ $key ] = " $key : " . base64_encode ( $private ? encrypt_string ( $password , $private ) : " " );
2010-05-07 14:31:14 +00:00
cookie ( " adminer_permanent " , implode ( " " , $permanent ));
2009-12-18 17:39:48 +00:00
}
2012-05-14 07:08:32 +00:00
if ( count ( $_POST ) == 1 // 1 - auth
2014-04-21 16:45:15 +00:00
|| DRIVER != $vendor
2013-08-11 02:11:35 +00:00
|| SERVER != $server
|| $_GET [ " username " ] !== $username // "0" == "00"
|| DB != $db
2010-04-21 12:01:32 +00:00
) {
2014-04-21 16:45:15 +00:00
redirect ( auth_url ( $vendor , $server , $username , $db ));
2009-08-30 22:21:36 +00:00
}
2013-05-02 01:28:04 +00:00
2021-02-07 13:23:41 +00:00
} elseif ( $_POST [ " logout " ] && ( ! $has_token || verify_token ())) {
foreach ( array ( " pwds " , " db " , " dbs " , " queries " ) as $key ) {
set_session ( $key , null );
2008-04-10 15:10:10 +00:00
}
2021-02-07 13:23:41 +00:00
unset_permanent ();
redirect ( substr ( preg_replace ( '~\b(username|db|ns)=[^&]*&~' , '' , ME ), 0 , - 1 ), lang ( 'Logout successful.' ) . ' ' . lang ( 'Thanks for using Adminer, consider <a href="https://www.adminer.org/en/donation/">donating</a>.' ));
2013-05-02 01:28:04 +00:00
2010-10-15 08:58:08 +00:00
} elseif ( $permanent && ! $_SESSION [ " pwds " ]) {
2010-05-07 14:31:14 +00:00
session_regenerate_id ();
2013-06-11 09:02:17 +00:00
$private = $adminer -> permanentLogin ();
2010-05-07 14:31:14 +00:00
foreach ( $permanent as $key => $val ) {
list (, $cipher ) = explode ( " : " , $val );
2013-07-05 15:28:37 +00:00
list ( $vendor , $server , $username , $db ) = array_map ( 'base64_decode' , explode ( " - " , $key ));
2013-08-11 02:06:21 +00:00
set_password ( $vendor , $server , $username , decrypt_string ( base64_decode ( $cipher ), $private ));
2013-07-05 15:28:37 +00:00
$_SESSION [ " db " ][ $vendor ][ $server ][ $username ][ $db ] = true ;
2009-12-18 17:39:48 +00:00
}
}
2012-09-09 03:54:02 +00:00
function unset_permanent () {
global $permanent ;
2012-09-09 04:29:16 +00:00
foreach ( $permanent as $key => $val ) {
2013-07-05 15:28:37 +00:00
list ( $vendor , $server , $username , $db ) = array_map ( 'base64_decode' , explode ( " - " , $key ));
if ( $vendor == DRIVER && $server == SERVER && $username == $_GET [ " username " ] && $db == DB ) {
2012-09-09 04:29:16 +00:00
unset ( $permanent [ $key ]);
}
2012-09-09 03:54:02 +00:00
}
2012-09-09 04:29:16 +00:00
cookie ( " adminer_permanent " , implode ( " " , $permanent ));
2012-09-09 03:54:02 +00:00
}
2015-02-07 18:40:51 +00:00
/** Renders an error message and a login form
* @ param string plain text
* @ return null exits
*/
2014-03-22 05:47:34 +00:00
function auth_error ( $error ) {
global $adminer , $has_token ;
2009-10-06 15:33:48 +00:00
$session_name = session_name ();
2014-09-13 19:03:13 +00:00
if ( isset ( $_GET [ " username " ])) {
header ( " HTTP/1.1 403 Forbidden " ); // 401 requires sending WWW-Authenticate header
2013-10-25 02:10:50 +00:00
if (( $_COOKIE [ $session_name ] || $_GET [ $session_name ]) && ! $has_token ) {
2010-04-21 12:01:32 +00:00
$error = lang ( 'Session expired, please login again.' );
} else {
2018-02-20 22:10:05 +00:00
restart_session ();
2014-03-22 05:47:34 +00:00
add_invalid_login ();
2013-08-11 02:06:21 +00:00
$password = get_password ();
2012-05-14 06:54:07 +00:00
if ( $password !== null ) {
2013-06-11 09:02:17 +00:00
if ( $password === false ) {
2021-02-08 15:06:22 +00:00
$error .= ( $error ? '<br>' : '' ) . lang ( 'Master password expired. <a href="https://www.adminer.org/en/extension/"%s>Implement</a> %s method to make it permanent.' , target_blank (), '<code>permanentLogin()</code>' );
2013-06-11 09:02:17 +00:00
}
2013-08-11 02:06:21 +00:00
set_password ( DRIVER , SERVER , $_GET [ " username " ], null );
2010-04-21 12:01:32 +00:00
}
2012-09-09 03:54:02 +00:00
unset_permanent ();
2010-04-21 12:01:32 +00:00
}
}
2014-09-13 19:03:13 +00:00
if ( ! $_COOKIE [ $session_name ] && $_GET [ $session_name ] && ini_bool ( " session.use_only_cookies " )) {
$error = lang ( 'Session support must be enabled.' );
}
2013-08-11 16:26:18 +00:00
$params = session_get_cookie_params ();
cookie ( " adminer_key " , ( $_COOKIE [ " adminer_key " ] ? $_COOKIE [ " adminer_key " ] : rand_string ()), $params [ " lifetime " ]);
2010-04-21 12:01:32 +00:00
page_header ( lang ( 'Login' ), $error , null );
2012-05-14 09:16:10 +00:00
echo " <form action='' method='post'> \n " ;
2009-12-18 17:49:21 +00:00
echo " <div> " ;
2018-02-08 15:49:56 +00:00
if ( hidden_fields ( $_POST , array ( " auth " ))) { // expired session
echo " <p class='message'> " . lang ( 'The action will be performed after successful login with the same credentials.' ) . " \n " ;
}
2009-12-18 17:49:21 +00:00
echo " </div> \n " ;
2018-02-08 15:49:56 +00:00
$adminer -> loginForm ();
2009-12-18 17:49:21 +00:00
echo " </form> \n " ;
2007-07-02 05:51:26 +00:00
page_footer ( " auth " );
2014-03-22 05:47:34 +00:00
exit ;
2007-07-23 11:57:26 +00:00
}
2018-02-20 22:10:05 +00:00
if ( isset ( $_GET [ " username " ]) && ! class_exists ( " Min_DB " )) {
unset ( $_SESSION [ " pwds " ][ DRIVER ]);
unset_permanent ();
page_header ( lang ( 'No extension' ), lang ( 'None of the supported PHP extensions (%s) are available.' , implode ( " , " , $possible_drivers )), false );
page_footer ( " auth " );
exit ;
}
2018-02-20 22:14:36 +00:00
stop_session ( true );
2018-02-20 22:10:05 +00:00
2019-05-14 07:07:51 +00:00
if ( isset ( $_GET [ " username " ]) && is_string ( get_password ())) {
2018-01-16 10:18:32 +00:00
list ( $host , $port ) = explode ( " : " , SERVER , 2 );
2021-01-12 20:32:04 +00:00
if ( preg_match ( '~^\s*([-+]?\d+)~' , $port , $match ) && ( $match [ 1 ] < 1024 || $match [ 1 ] > 65535 )) { // is_numeric('80#') would still connect to port 80
2018-01-24 17:21:18 +00:00
auth_error ( lang ( 'Connecting to privileged ports is not allowed.' ));
2018-01-16 10:18:32 +00:00
}
2018-01-16 09:56:35 +00:00
check_invalid_login ();
2010-04-21 12:01:32 +00:00
$connection = connect ();
2018-01-22 10:42:17 +00:00
$driver = new Min_Driver ( $connection );
2008-08-27 16:43:30 +00:00
}
2013-05-02 01:28:04 +00:00
2018-02-07 10:37:53 +00:00
$login = null ;
2016-06-01 16:57:17 +00:00
if ( ! is_object ( $connection ) || ( $login = $adminer -> login ( $_GET [ " username " ], get_password ())) !== true ) {
2018-09-13 14:08:57 +00:00
$error = ( is_string ( $connection ) ? h ( $connection ) : ( is_string ( $login ) ? $login : lang ( 'Invalid credentials.' )));
auth_error ( $error . ( preg_match ( '~^ | $~' , get_password ()) ? '<br>' . lang ( 'There is a space in the input password which might be the cause.' ) : '' ));
2007-07-02 05:51:26 +00:00
}
2009-11-20 17:29:35 +00:00
2021-02-07 13:23:41 +00:00
if ( $_POST [ " logout " ] && $has_token && ! verify_token ()) {
page_header ( lang ( 'Logout' ), lang ( 'Invalid CSRF token. Send the form again.' ));
page_footer ( " db " );
exit ;
}
2012-05-14 07:08:32 +00:00
if ( $auth && $_POST [ " token " ]) {
2010-04-21 12:01:32 +00:00
$_POST [ " token " ] = $token ; // reset token after explicit login
2010-02-24 11:59:25 +00:00
}
2013-04-02 01:45:26 +00:00
$error = '' ; ///< @var string
if ( $_POST ) {
2013-10-25 02:10:50 +00:00
if ( ! verify_token ()) {
2013-04-02 01:45:26 +00:00
$ini = " max_input_vars " ;
$max_vars = ini_get ( $ini );
if ( extension_loaded ( " suhosin " )) {
foreach ( array ( " suhosin.request.max_vars " , " suhosin.post.max_vars " ) as $key ) {
$val = ini_get ( $key );
if ( $val && ( ! $max_vars || $val < $max_vars )) {
$ini = $key ;
$max_vars = $val ;
}
}
}
$error = ( ! $_POST [ " token " ] && $max_vars
? lang ( 'Maximum number of allowed fields exceeded. Please increase %s.' , " ' $ini ' " )
2014-10-28 16:24:13 +00:00
: lang ( 'Invalid CSRF token. Send the form again.' ) . ' ' . lang ( 'If you did not send this request from Adminer then close this page.' )
2013-04-02 01:45:26 +00:00
);
}
2013-05-02 01:28:04 +00:00
2013-04-02 01:45:26 +00:00
} elseif ( $_SERVER [ " REQUEST_METHOD " ] == " POST " ) {
// posted form with no data means that post_max_size exceeded because Adminer always sends token at least
$error = lang ( 'Too big POST data. Reduce the data or increase the %s configuration directive.' , " 'post_max_size' " );
2013-06-24 13:12:13 +00:00
if ( isset ( $_GET [ " sql " ])) {
$error .= ' ' . lang ( 'You can upload a big SQL file via FTP and import it from server.' );
}
2013-04-02 01:45:26 +00:00
}