1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/auth/auth_db.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,276 @@
1.4 +<?php
1.5 +/**
1.6 +* Database auth plug-in for phpBB3
1.7 +*
1.8 +* Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
1.9 +*
1.10 +* This is for authentication via the integrated user table
1.11 +*
1.12 +* @package login
1.13 +* @version $Id$
1.14 +* @copyright (c) 2005 phpBB Group
1.15 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.16 +*
1.17 +*/
1.18 +
1.19 +/**
1.20 +* @ignore
1.21 +*/
1.22 +if (!defined('IN_PHPBB'))
1.23 +{
1.24 + exit;
1.25 +}
1.26 +
1.27 +/**
1.28 +* Login function
1.29 +*
1.30 +* @param string $username
1.31 +* @param string $password
1.32 +* @param string $ip IP address the login is taking place from. Used to
1.33 +* limit the number of login attempts per IP address.
1.34 +* @param string $browser The user agent used to login
1.35 +* @param string $forwarded_for X_FORWARDED_FOR header sent with login request
1.36 +* @return array A associative array of the format
1.37 +* array(
1.38 +* 'status' => status constant
1.39 +* 'error_msg' => string
1.40 +* 'user_row' => array
1.41 +* )
1.42 +*/
1.43 +function login_db($username, $password, $ip = '', $browser = '', $forwarded_for = '')
1.44 +{
1.45 + global $db, $config;
1.46 +
1.47 + // do not allow empty password
1.48 + if (!$password)
1.49 + {
1.50 + return array(
1.51 + 'status' => LOGIN_ERROR_PASSWORD,
1.52 + 'error_msg' => 'NO_PASSWORD_SUPPLIED',
1.53 + 'user_row' => array('user_id' => ANONYMOUS),
1.54 + );
1.55 + }
1.56 +
1.57 + if (!$username)
1.58 + {
1.59 + return array(
1.60 + 'status' => LOGIN_ERROR_USERNAME,
1.61 + 'error_msg' => 'LOGIN_ERROR_USERNAME',
1.62 + 'user_row' => array('user_id' => ANONYMOUS),
1.63 + );
1.64 + }
1.65 +
1.66 + $username_clean = utf8_clean_string($username);
1.67 +
1.68 + $sql = 'SELECT user_id, username, user_password, user_passchg, user_pass_convert, user_email, user_type, user_login_attempts
1.69 + FROM ' . USERS_TABLE . "
1.70 + WHERE username_clean = '" . $db->sql_escape($username_clean) . "'";
1.71 + $result = $db->sql_query($sql);
1.72 + $row = $db->sql_fetchrow($result);
1.73 + $db->sql_freeresult($result);
1.74 +
1.75 + if (($ip && !$config['ip_login_limit_use_forwarded']) ||
1.76 + ($forwarded_for && $config['ip_login_limit_use_forwarded']))
1.77 + {
1.78 + $sql = 'SELECT COUNT(*) AS attempts
1.79 + FROM ' . LOGIN_ATTEMPT_TABLE . '
1.80 + WHERE attempt_time > ' . (time() - (int) $config['ip_login_limit_time']);
1.81 + if ($config['ip_login_limit_use_forwarded'])
1.82 + {
1.83 + $sql .= " AND attempt_forwarded_for = '" . $db->sql_escape($forwarded_for) . "'";
1.84 + }
1.85 + else
1.86 + {
1.87 + $sql .= " AND attempt_ip = '" . $db->sql_escape($ip) . "' ";
1.88 + }
1.89 +
1.90 + $result = $db->sql_query($sql);
1.91 + $attempts = (int) $db->sql_fetchfield('attempts');
1.92 + $db->sql_freeresult($result);
1.93 +
1.94 + $attempt_data = array(
1.95 + 'attempt_ip' => $ip,
1.96 + 'attempt_browser' => trim(substr($browser, 0, 149)),
1.97 + 'attempt_forwarded_for' => $forwarded_for,
1.98 + 'attempt_time' => time(),
1.99 + 'user_id' => ($row) ? (int) $row['user_id'] : 0,
1.100 + 'username' => $username,
1.101 + 'username_clean' => $username_clean,
1.102 + );
1.103 + $sql = 'INSERT INTO ' . LOGIN_ATTEMPT_TABLE . $db->sql_build_array('INSERT', $attempt_data);
1.104 + $result = $db->sql_query($sql);
1.105 + }
1.106 + else
1.107 + {
1.108 + $attempts = 0;
1.109 + }
1.110 +
1.111 + if (!$row)
1.112 + {
1.113 + if ($config['ip_login_limit_max'] && $attempts >= $config['ip_login_limit_max'])
1.114 + {
1.115 + return array(
1.116 + 'status' => LOGIN_ERROR_ATTEMPTS,
1.117 + 'error_msg' => 'LOGIN_ERROR_ATTEMPTS',
1.118 + 'user_row' => array('user_id' => ANONYMOUS),
1.119 + );
1.120 + }
1.121 +
1.122 + return array(
1.123 + 'status' => LOGIN_ERROR_USERNAME,
1.124 + 'error_msg' => 'LOGIN_ERROR_USERNAME',
1.125 + 'user_row' => array('user_id' => ANONYMOUS),
1.126 + );
1.127 + }
1.128 +
1.129 + $show_captcha = ($config['max_login_attempts'] && $row['user_login_attempts'] >= $config['max_login_attempts']) ||
1.130 + ($config['ip_login_limit_max'] && $attempts >= $config['ip_login_limit_max']);
1.131 +
1.132 + // If there are too much login attempts, we need to check for an confirm image
1.133 + // Every auth module is able to define what to do by itself...
1.134 + if ($show_captcha)
1.135 + {
1.136 + // Visual Confirmation handling
1.137 + if (!class_exists('phpbb_captcha_factory'))
1.138 + {
1.139 + global $phpbb_root_path, $phpEx;
1.140 + include ($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx);
1.141 + }
1.142 +
1.143 + $captcha =& phpbb_captcha_factory::get_instance($config['captcha_plugin']);
1.144 + $captcha->init(CONFIRM_LOGIN);
1.145 + $vc_response = $captcha->validate($row);
1.146 + if ($vc_response)
1.147 + {
1.148 + return array(
1.149 + 'status' => LOGIN_ERROR_ATTEMPTS,
1.150 + 'error_msg' => 'LOGIN_ERROR_ATTEMPTS',
1.151 + 'user_row' => $row,
1.152 + );
1.153 + }
1.154 + else
1.155 + {
1.156 + $captcha->reset();
1.157 + }
1.158 +
1.159 + }
1.160 +
1.161 + // If the password convert flag is set we need to convert it
1.162 + if ($row['user_pass_convert'])
1.163 + {
1.164 + // in phpBB2 passwords were used exactly as they were sent, with addslashes applied
1.165 + $password_old_format = isset($_REQUEST['password']) ? (string) $_REQUEST['password'] : '';
1.166 + $password_old_format = (!STRIP) ? addslashes($password_old_format) : $password_old_format;
1.167 + $password_new_format = '';
1.168 +
1.169 + set_var($password_new_format, stripslashes($password_old_format), 'string');
1.170 +
1.171 + if ($password == $password_new_format)
1.172 + {
1.173 + if (!function_exists('utf8_to_cp1252'))
1.174 + {
1.175 + global $phpbb_root_path, $phpEx;
1.176 + include($phpbb_root_path . 'includes/utf/data/recode_basic.' . $phpEx);
1.177 + }
1.178 +
1.179 + // cp1252 is phpBB2's default encoding, characters outside ASCII range might work when converted into that encoding
1.180 + // plain md5 support left in for conversions from other systems.
1.181 + if ((strlen($row['user_password']) == 34 && (phpbb_check_hash(md5($password_old_format), $row['user_password']) || phpbb_check_hash(md5(utf8_to_cp1252($password_old_format)), $row['user_password'])))
1.182 + || (strlen($row['user_password']) == 32 && (md5($password_old_format) == $row['user_password'] || md5(utf8_to_cp1252($password_old_format)) == $row['user_password'])))
1.183 + {
1.184 + $hash = phpbb_hash($password_new_format);
1.185 +
1.186 + // Update the password in the users table to the new format and remove user_pass_convert flag
1.187 + $sql = 'UPDATE ' . USERS_TABLE . '
1.188 + SET user_password = \'' . $db->sql_escape($hash) . '\',
1.189 + user_pass_convert = 0
1.190 + WHERE user_id = ' . $row['user_id'];
1.191 + $db->sql_query($sql);
1.192 +
1.193 + $row['user_pass_convert'] = 0;
1.194 + $row['user_password'] = $hash;
1.195 + }
1.196 + else
1.197 + {
1.198 + // Although we weren't able to convert this password we have to
1.199 + // increase login attempt count to make sure this cannot be exploited
1.200 + $sql = 'UPDATE ' . USERS_TABLE . '
1.201 + SET user_login_attempts = user_login_attempts + 1
1.202 + WHERE user_id = ' . (int) $row['user_id'] . '
1.203 + AND user_login_attempts < ' . LOGIN_ATTEMPTS_MAX;
1.204 + $db->sql_query($sql);
1.205 +
1.206 + return array(
1.207 + 'status' => LOGIN_ERROR_PASSWORD_CONVERT,
1.208 + 'error_msg' => 'LOGIN_ERROR_PASSWORD_CONVERT',
1.209 + 'user_row' => $row,
1.210 + );
1.211 + }
1.212 + }
1.213 + }
1.214 +
1.215 + // Check password ...
1.216 + if (!$row['user_pass_convert'] && phpbb_check_hash($password, $row['user_password']))
1.217 + {
1.218 + // Check for old password hash...
1.219 + if (strlen($row['user_password']) == 32)
1.220 + {
1.221 + $hash = phpbb_hash($password);
1.222 +
1.223 + // Update the password in the users table to the new format
1.224 + $sql = 'UPDATE ' . USERS_TABLE . "
1.225 + SET user_password = '" . $db->sql_escape($hash) . "',
1.226 + user_pass_convert = 0
1.227 + WHERE user_id = {$row['user_id']}";
1.228 + $db->sql_query($sql);
1.229 +
1.230 + $row['user_password'] = $hash;
1.231 + }
1.232 +
1.233 + $sql = 'DELETE FROM ' . LOGIN_ATTEMPT_TABLE . '
1.234 + WHERE user_id = ' . $row['user_id'];
1.235 + $db->sql_query($sql);
1.236 +
1.237 + if ($row['user_login_attempts'] != 0)
1.238 + {
1.239 + // Successful, reset login attempts (the user passed all stages)
1.240 + $sql = 'UPDATE ' . USERS_TABLE . '
1.241 + SET user_login_attempts = 0
1.242 + WHERE user_id = ' . $row['user_id'];
1.243 + $db->sql_query($sql);
1.244 + }
1.245 +
1.246 + // User inactive...
1.247 + if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
1.248 + {
1.249 + return array(
1.250 + 'status' => LOGIN_ERROR_ACTIVE,
1.251 + 'error_msg' => 'ACTIVE_ERROR',
1.252 + 'user_row' => $row,
1.253 + );
1.254 + }
1.255 +
1.256 + // Successful login... set user_login_attempts to zero...
1.257 + return array(
1.258 + 'status' => LOGIN_SUCCESS,
1.259 + 'error_msg' => false,
1.260 + 'user_row' => $row,
1.261 + );
1.262 + }
1.263 +
1.264 + // Password incorrect - increase login attempts
1.265 + $sql = 'UPDATE ' . USERS_TABLE . '
1.266 + SET user_login_attempts = user_login_attempts + 1
1.267 + WHERE user_id = ' . (int) $row['user_id'] . '
1.268 + AND user_login_attempts < ' . LOGIN_ATTEMPTS_MAX;
1.269 + $db->sql_query($sql);
1.270 +
1.271 + // Give status about wrong password...
1.272 + return array(
1.273 + 'status' => ($show_captcha) ? LOGIN_ERROR_ATTEMPTS : LOGIN_ERROR_PASSWORD,
1.274 + 'error_msg' => ($show_captcha) ? 'LOGIN_ERROR_ATTEMPTS' : 'LOGIN_ERROR_PASSWORD',
1.275 + 'user_row' => $row,
1.276 + );
1.277 +}
1.278 +
1.279 +?>