|
indvdum@0
|
1 |
<?php
|
|
indvdum@0
|
2 |
/**
|
|
indvdum@0
|
3 |
* Database auth plug-in for phpBB3
|
|
indvdum@0
|
4 |
*
|
|
indvdum@0
|
5 |
* Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
|
|
indvdum@0
|
6 |
*
|
|
indvdum@0
|
7 |
* This is for authentication via the integrated user table
|
|
indvdum@0
|
8 |
*
|
|
indvdum@0
|
9 |
* @package login
|
|
indvdum@0
|
10 |
* @version $Id$
|
|
indvdum@0
|
11 |
* @copyright (c) 2005 phpBB Group
|
|
indvdum@0
|
12 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
indvdum@0
|
13 |
*
|
|
indvdum@0
|
14 |
*/
|
|
indvdum@0
|
15 |
|
|
indvdum@0
|
16 |
/**
|
|
indvdum@0
|
17 |
* @ignore
|
|
indvdum@0
|
18 |
*/
|
|
indvdum@0
|
19 |
if (!defined('IN_PHPBB'))
|
|
indvdum@0
|
20 |
{
|
|
indvdum@0
|
21 |
exit;
|
|
indvdum@0
|
22 |
}
|
|
indvdum@0
|
23 |
|
|
indvdum@0
|
24 |
/**
|
|
indvdum@0
|
25 |
* Login function
|
|
indvdum@0
|
26 |
*
|
|
indvdum@0
|
27 |
* @param string $username
|
|
indvdum@0
|
28 |
* @param string $password
|
|
indvdum@0
|
29 |
* @param string $ip IP address the login is taking place from. Used to
|
|
indvdum@0
|
30 |
* limit the number of login attempts per IP address.
|
|
indvdum@0
|
31 |
* @param string $browser The user agent used to login
|
|
indvdum@0
|
32 |
* @param string $forwarded_for X_FORWARDED_FOR header sent with login request
|
|
indvdum@0
|
33 |
* @return array A associative array of the format
|
|
indvdum@0
|
34 |
* array(
|
|
indvdum@0
|
35 |
* 'status' => status constant
|
|
indvdum@0
|
36 |
* 'error_msg' => string
|
|
indvdum@0
|
37 |
* 'user_row' => array
|
|
indvdum@0
|
38 |
* )
|
|
indvdum@0
|
39 |
*/
|
|
indvdum@0
|
40 |
function login_db($username, $password, $ip = '', $browser = '', $forwarded_for = '')
|
|
indvdum@0
|
41 |
{
|
|
indvdum@0
|
42 |
global $db, $config;
|
|
indvdum@0
|
43 |
|
|
indvdum@0
|
44 |
// do not allow empty password
|
|
indvdum@0
|
45 |
if (!$password)
|
|
indvdum@0
|
46 |
{
|
|
indvdum@0
|
47 |
return array(
|
|
indvdum@0
|
48 |
'status' => LOGIN_ERROR_PASSWORD,
|
|
indvdum@0
|
49 |
'error_msg' => 'NO_PASSWORD_SUPPLIED',
|
|
indvdum@0
|
50 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
indvdum@0
|
51 |
);
|
|
indvdum@0
|
52 |
}
|
|
indvdum@0
|
53 |
|
|
indvdum@0
|
54 |
if (!$username)
|
|
indvdum@0
|
55 |
{
|
|
indvdum@0
|
56 |
return array(
|
|
indvdum@0
|
57 |
'status' => LOGIN_ERROR_USERNAME,
|
|
indvdum@0
|
58 |
'error_msg' => 'LOGIN_ERROR_USERNAME',
|
|
indvdum@0
|
59 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
indvdum@0
|
60 |
);
|
|
indvdum@0
|
61 |
}
|
|
indvdum@0
|
62 |
|
|
indvdum@0
|
63 |
$username_clean = utf8_clean_string($username);
|
|
indvdum@0
|
64 |
|
|
indvdum@0
|
65 |
$sql = 'SELECT user_id, username, user_password, user_passchg, user_pass_convert, user_email, user_type, user_login_attempts
|
|
indvdum@0
|
66 |
FROM ' . USERS_TABLE . "
|
|
indvdum@0
|
67 |
WHERE username_clean = '" . $db->sql_escape($username_clean) . "'";
|
|
indvdum@0
|
68 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
69 |
$row = $db->sql_fetchrow($result);
|
|
indvdum@0
|
70 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
71 |
|
|
indvdum@0
|
72 |
if (($ip && !$config['ip_login_limit_use_forwarded']) ||
|
|
indvdum@0
|
73 |
($forwarded_for && $config['ip_login_limit_use_forwarded']))
|
|
indvdum@0
|
74 |
{
|
|
indvdum@0
|
75 |
$sql = 'SELECT COUNT(*) AS attempts
|
|
indvdum@0
|
76 |
FROM ' . LOGIN_ATTEMPT_TABLE . '
|
|
indvdum@0
|
77 |
WHERE attempt_time > ' . (time() - (int) $config['ip_login_limit_time']);
|
|
indvdum@0
|
78 |
if ($config['ip_login_limit_use_forwarded'])
|
|
indvdum@0
|
79 |
{
|
|
indvdum@0
|
80 |
$sql .= " AND attempt_forwarded_for = '" . $db->sql_escape($forwarded_for) . "'";
|
|
indvdum@0
|
81 |
}
|
|
indvdum@0
|
82 |
else
|
|
indvdum@0
|
83 |
{
|
|
indvdum@0
|
84 |
$sql .= " AND attempt_ip = '" . $db->sql_escape($ip) . "' ";
|
|
indvdum@0
|
85 |
}
|
|
indvdum@0
|
86 |
|
|
indvdum@0
|
87 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
88 |
$attempts = (int) $db->sql_fetchfield('attempts');
|
|
indvdum@0
|
89 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
90 |
|
|
indvdum@0
|
91 |
$attempt_data = array(
|
|
indvdum@0
|
92 |
'attempt_ip' => $ip,
|
|
indvdum@0
|
93 |
'attempt_browser' => trim(substr($browser, 0, 149)),
|
|
indvdum@0
|
94 |
'attempt_forwarded_for' => $forwarded_for,
|
|
indvdum@0
|
95 |
'attempt_time' => time(),
|
|
indvdum@0
|
96 |
'user_id' => ($row) ? (int) $row['user_id'] : 0,
|
|
indvdum@0
|
97 |
'username' => $username,
|
|
indvdum@0
|
98 |
'username_clean' => $username_clean,
|
|
indvdum@0
|
99 |
);
|
|
indvdum@0
|
100 |
$sql = 'INSERT INTO ' . LOGIN_ATTEMPT_TABLE . $db->sql_build_array('INSERT', $attempt_data);
|
|
indvdum@0
|
101 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
102 |
}
|
|
indvdum@0
|
103 |
else
|
|
indvdum@0
|
104 |
{
|
|
indvdum@0
|
105 |
$attempts = 0;
|
|
indvdum@0
|
106 |
}
|
|
indvdum@0
|
107 |
|
|
indvdum@0
|
108 |
if (!$row)
|
|
indvdum@0
|
109 |
{
|
|
indvdum@0
|
110 |
if ($config['ip_login_limit_max'] && $attempts >= $config['ip_login_limit_max'])
|
|
indvdum@0
|
111 |
{
|
|
indvdum@0
|
112 |
return array(
|
|
indvdum@0
|
113 |
'status' => LOGIN_ERROR_ATTEMPTS,
|
|
indvdum@0
|
114 |
'error_msg' => 'LOGIN_ERROR_ATTEMPTS',
|
|
indvdum@0
|
115 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
indvdum@0
|
116 |
);
|
|
indvdum@0
|
117 |
}
|
|
indvdum@0
|
118 |
|
|
indvdum@0
|
119 |
return array(
|
|
indvdum@0
|
120 |
'status' => LOGIN_ERROR_USERNAME,
|
|
indvdum@0
|
121 |
'error_msg' => 'LOGIN_ERROR_USERNAME',
|
|
indvdum@0
|
122 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
indvdum@0
|
123 |
);
|
|
indvdum@0
|
124 |
}
|
|
indvdum@0
|
125 |
|
|
indvdum@0
|
126 |
$show_captcha = ($config['max_login_attempts'] && $row['user_login_attempts'] >= $config['max_login_attempts']) ||
|
|
indvdum@0
|
127 |
($config['ip_login_limit_max'] && $attempts >= $config['ip_login_limit_max']);
|
|
indvdum@0
|
128 |
|
|
indvdum@0
|
129 |
// If there are too much login attempts, we need to check for an confirm image
|
|
indvdum@0
|
130 |
// Every auth module is able to define what to do by itself...
|
|
indvdum@0
|
131 |
if ($show_captcha)
|
|
indvdum@0
|
132 |
{
|
|
indvdum@0
|
133 |
// Visual Confirmation handling
|
|
indvdum@0
|
134 |
if (!class_exists('phpbb_captcha_factory'))
|
|
indvdum@0
|
135 |
{
|
|
indvdum@0
|
136 |
global $phpbb_root_path, $phpEx;
|
|
indvdum@0
|
137 |
include ($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx);
|
|
indvdum@0
|
138 |
}
|
|
indvdum@0
|
139 |
|
|
indvdum@0
|
140 |
$captcha =& phpbb_captcha_factory::get_instance($config['captcha_plugin']);
|
|
indvdum@0
|
141 |
$captcha->init(CONFIRM_LOGIN);
|
|
indvdum@0
|
142 |
$vc_response = $captcha->validate($row);
|
|
indvdum@0
|
143 |
if ($vc_response)
|
|
indvdum@0
|
144 |
{
|
|
indvdum@0
|
145 |
return array(
|
|
indvdum@0
|
146 |
'status' => LOGIN_ERROR_ATTEMPTS,
|
|
indvdum@0
|
147 |
'error_msg' => 'LOGIN_ERROR_ATTEMPTS',
|
|
indvdum@0
|
148 |
'user_row' => $row,
|
|
indvdum@0
|
149 |
);
|
|
indvdum@0
|
150 |
}
|
|
indvdum@0
|
151 |
else
|
|
indvdum@0
|
152 |
{
|
|
indvdum@0
|
153 |
$captcha->reset();
|
|
indvdum@0
|
154 |
}
|
|
indvdum@0
|
155 |
|
|
indvdum@0
|
156 |
}
|
|
indvdum@0
|
157 |
|
|
indvdum@0
|
158 |
// If the password convert flag is set we need to convert it
|
|
indvdum@0
|
159 |
if ($row['user_pass_convert'])
|
|
indvdum@0
|
160 |
{
|
|
indvdum@0
|
161 |
// in phpBB2 passwords were used exactly as they were sent, with addslashes applied
|
|
indvdum@0
|
162 |
$password_old_format = isset($_REQUEST['password']) ? (string) $_REQUEST['password'] : '';
|
|
indvdum@0
|
163 |
$password_old_format = (!STRIP) ? addslashes($password_old_format) : $password_old_format;
|
|
indvdum@0
|
164 |
$password_new_format = '';
|
|
indvdum@0
|
165 |
|
|
indvdum@0
|
166 |
set_var($password_new_format, stripslashes($password_old_format), 'string');
|
|
indvdum@0
|
167 |
|
|
indvdum@0
|
168 |
if ($password == $password_new_format)
|
|
indvdum@0
|
169 |
{
|
|
indvdum@0
|
170 |
if (!function_exists('utf8_to_cp1252'))
|
|
indvdum@0
|
171 |
{
|
|
indvdum@0
|
172 |
global $phpbb_root_path, $phpEx;
|
|
indvdum@0
|
173 |
include($phpbb_root_path . 'includes/utf/data/recode_basic.' . $phpEx);
|
|
indvdum@0
|
174 |
}
|
|
indvdum@0
|
175 |
|
|
indvdum@0
|
176 |
// cp1252 is phpBB2's default encoding, characters outside ASCII range might work when converted into that encoding
|
|
indvdum@0
|
177 |
// plain md5 support left in for conversions from other systems.
|
|
indvdum@0
|
178 |
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'])))
|
|
indvdum@0
|
179 |
|| (strlen($row['user_password']) == 32 && (md5($password_old_format) == $row['user_password'] || md5(utf8_to_cp1252($password_old_format)) == $row['user_password'])))
|
|
indvdum@0
|
180 |
{
|
|
indvdum@0
|
181 |
$hash = phpbb_hash($password_new_format);
|
|
indvdum@0
|
182 |
|
|
indvdum@0
|
183 |
// Update the password in the users table to the new format and remove user_pass_convert flag
|
|
indvdum@0
|
184 |
$sql = 'UPDATE ' . USERS_TABLE . '
|
|
indvdum@0
|
185 |
SET user_password = \'' . $db->sql_escape($hash) . '\',
|
|
indvdum@0
|
186 |
user_pass_convert = 0
|
|
indvdum@0
|
187 |
WHERE user_id = ' . $row['user_id'];
|
|
indvdum@0
|
188 |
$db->sql_query($sql);
|
|
indvdum@0
|
189 |
|
|
indvdum@0
|
190 |
$row['user_pass_convert'] = 0;
|
|
indvdum@0
|
191 |
$row['user_password'] = $hash;
|
|
indvdum@0
|
192 |
}
|
|
indvdum@0
|
193 |
else
|
|
indvdum@0
|
194 |
{
|
|
indvdum@0
|
195 |
// Although we weren't able to convert this password we have to
|
|
indvdum@0
|
196 |
// increase login attempt count to make sure this cannot be exploited
|
|
indvdum@0
|
197 |
$sql = 'UPDATE ' . USERS_TABLE . '
|
|
indvdum@0
|
198 |
SET user_login_attempts = user_login_attempts + 1
|
|
indvdum@0
|
199 |
WHERE user_id = ' . (int) $row['user_id'] . '
|
|
indvdum@0
|
200 |
AND user_login_attempts < ' . LOGIN_ATTEMPTS_MAX;
|
|
indvdum@0
|
201 |
$db->sql_query($sql);
|
|
indvdum@0
|
202 |
|
|
indvdum@0
|
203 |
return array(
|
|
indvdum@0
|
204 |
'status' => LOGIN_ERROR_PASSWORD_CONVERT,
|
|
indvdum@0
|
205 |
'error_msg' => 'LOGIN_ERROR_PASSWORD_CONVERT',
|
|
indvdum@0
|
206 |
'user_row' => $row,
|
|
indvdum@0
|
207 |
);
|
|
indvdum@0
|
208 |
}
|
|
indvdum@0
|
209 |
}
|
|
indvdum@0
|
210 |
}
|
|
indvdum@0
|
211 |
|
|
indvdum@0
|
212 |
// Check password ...
|
|
indvdum@0
|
213 |
if (!$row['user_pass_convert'] && phpbb_check_hash($password, $row['user_password']))
|
|
indvdum@0
|
214 |
{
|
|
indvdum@0
|
215 |
// Check for old password hash...
|
|
indvdum@0
|
216 |
if (strlen($row['user_password']) == 32)
|
|
indvdum@0
|
217 |
{
|
|
indvdum@0
|
218 |
$hash = phpbb_hash($password);
|
|
indvdum@0
|
219 |
|
|
indvdum@0
|
220 |
// Update the password in the users table to the new format
|
|
indvdum@0
|
221 |
$sql = 'UPDATE ' . USERS_TABLE . "
|
|
indvdum@0
|
222 |
SET user_password = '" . $db->sql_escape($hash) . "',
|
|
indvdum@0
|
223 |
user_pass_convert = 0
|
|
indvdum@0
|
224 |
WHERE user_id = {$row['user_id']}";
|
|
indvdum@0
|
225 |
$db->sql_query($sql);
|
|
indvdum@0
|
226 |
|
|
indvdum@0
|
227 |
$row['user_password'] = $hash;
|
|
indvdum@0
|
228 |
}
|
|
indvdum@0
|
229 |
|
|
indvdum@0
|
230 |
$sql = 'DELETE FROM ' . LOGIN_ATTEMPT_TABLE . '
|
|
indvdum@0
|
231 |
WHERE user_id = ' . $row['user_id'];
|
|
indvdum@0
|
232 |
$db->sql_query($sql);
|
|
indvdum@0
|
233 |
|
|
indvdum@0
|
234 |
if ($row['user_login_attempts'] != 0)
|
|
indvdum@0
|
235 |
{
|
|
indvdum@0
|
236 |
// Successful, reset login attempts (the user passed all stages)
|
|
indvdum@0
|
237 |
$sql = 'UPDATE ' . USERS_TABLE . '
|
|
indvdum@0
|
238 |
SET user_login_attempts = 0
|
|
indvdum@0
|
239 |
WHERE user_id = ' . $row['user_id'];
|
|
indvdum@0
|
240 |
$db->sql_query($sql);
|
|
indvdum@0
|
241 |
}
|
|
indvdum@0
|
242 |
|
|
indvdum@0
|
243 |
// User inactive...
|
|
indvdum@0
|
244 |
if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
|
|
indvdum@0
|
245 |
{
|
|
indvdum@0
|
246 |
return array(
|
|
indvdum@0
|
247 |
'status' => LOGIN_ERROR_ACTIVE,
|
|
indvdum@0
|
248 |
'error_msg' => 'ACTIVE_ERROR',
|
|
indvdum@0
|
249 |
'user_row' => $row,
|
|
indvdum@0
|
250 |
);
|
|
indvdum@0
|
251 |
}
|
|
indvdum@0
|
252 |
|
|
indvdum@0
|
253 |
// Successful login... set user_login_attempts to zero...
|
|
indvdum@0
|
254 |
return array(
|
|
indvdum@0
|
255 |
'status' => LOGIN_SUCCESS,
|
|
indvdum@0
|
256 |
'error_msg' => false,
|
|
indvdum@0
|
257 |
'user_row' => $row,
|
|
indvdum@0
|
258 |
);
|
|
indvdum@0
|
259 |
}
|
|
indvdum@0
|
260 |
|
|
indvdum@0
|
261 |
// Password incorrect - increase login attempts
|
|
indvdum@0
|
262 |
$sql = 'UPDATE ' . USERS_TABLE . '
|
|
indvdum@0
|
263 |
SET user_login_attempts = user_login_attempts + 1
|
|
indvdum@0
|
264 |
WHERE user_id = ' . (int) $row['user_id'] . '
|
|
indvdum@0
|
265 |
AND user_login_attempts < ' . LOGIN_ATTEMPTS_MAX;
|
|
indvdum@0
|
266 |
$db->sql_query($sql);
|
|
indvdum@0
|
267 |
|
|
indvdum@0
|
268 |
// Give status about wrong password...
|
|
indvdum@0
|
269 |
return array(
|
|
indvdum@0
|
270 |
'status' => ($show_captcha) ? LOGIN_ERROR_ATTEMPTS : LOGIN_ERROR_PASSWORD,
|
|
indvdum@0
|
271 |
'error_msg' => ($show_captcha) ? 'LOGIN_ERROR_ATTEMPTS' : 'LOGIN_ERROR_PASSWORD',
|
|
indvdum@0
|
272 |
'user_row' => $row,
|
|
indvdum@0
|
273 |
);
|
|
indvdum@0
|
274 |
}
|
|
indvdum@0
|
275 |
|
|
indvdum@0
|
276 |
?>
|