1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is concerned pretty entirely, as you see from its name, with |
5
|
|
|
* logging in and out members, and the validation of that. |
6
|
|
|
* |
7
|
|
|
* Simple Machines Forum (SMF) |
8
|
|
|
* |
9
|
|
|
* @package SMF |
10
|
|
|
* @author Simple Machines https://www.simplemachines.org |
11
|
|
|
* @copyright 2022 Simple Machines and individual contributors |
12
|
|
|
* @license https://www.simplemachines.org/about/smf/license.php BSD |
13
|
|
|
* |
14
|
|
|
* @version 2.1.0 |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
if (!defined('SMF')) |
18
|
|
|
die('No direct access...'); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Ask them for their login information. (shows a page for the user to type |
22
|
|
|
* in their username and password.) |
23
|
|
|
* It caches the referring URL in $_SESSION['login_url']. |
24
|
|
|
* It is accessed from ?action=login. |
25
|
|
|
* |
26
|
|
|
* Uses Login template and language file with the login sub-template. |
27
|
|
|
*/ |
28
|
|
|
function Login() |
29
|
|
|
{ |
30
|
|
|
global $txt, $context, $scripturl, $user_info; |
31
|
|
|
|
32
|
|
|
// You are already logged in, go take a tour of the boards |
33
|
|
|
if (!empty($user_info['id'])) |
34
|
|
|
{ |
35
|
|
|
// This came from a valid hashed return url. Or something that knows our secrets... |
36
|
|
|
if (!empty($_REQUEST['return_hash']) && !empty($_REQUEST['return_to']) && hash_hmac('sha1', un_htmlspecialchars($_REQUEST['return_to']), get_auth_secret()) == $_REQUEST['return_hash']) |
37
|
|
|
redirectexit(un_htmlspecialchars($_REQUEST['return_to'])); |
38
|
|
|
else |
39
|
|
|
redirectexit(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// We need to load the Login template/language file. |
43
|
|
|
loadLanguage('Login'); |
44
|
|
|
loadTemplate('Login'); |
45
|
|
|
|
46
|
|
|
$context['sub_template'] = 'login'; |
47
|
|
|
|
48
|
|
|
/* This is true when: |
49
|
|
|
* We have a valid header indicating a JQXHR request. This is not sent during a cross domain request. |
50
|
|
|
* OR we have found: |
51
|
|
|
* 1. valid cors host |
52
|
|
|
* 2. A header indicating a SMF request |
53
|
|
|
* 3. The url has a ajax in either the GET or POST |
54
|
|
|
* These are not intended for security, but ensuring the request is intended for a JQXHR response. |
55
|
|
|
*/ |
56
|
|
|
if ( |
57
|
|
|
( |
58
|
|
|
!empty($_SERVER['HTTP_X_REQUESTED_WITH']) |
59
|
|
|
&& $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' |
60
|
|
|
) |
61
|
|
|
|| |
62
|
|
|
( |
63
|
|
|
!empty($context['valid_cors_found']) |
64
|
|
|
&& !empty($_SERVER['HTTP_X_SMF_AJAX']) |
65
|
|
|
&& isset($_REQUEST['ajax']) |
66
|
|
|
) |
67
|
|
|
) |
68
|
|
|
{ |
69
|
|
|
$context['from_ajax'] = true; |
70
|
|
|
$context['template_layers'] = array(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Get the template ready.... not really much else to do. |
74
|
|
|
$context['page_title'] = $txt['login']; |
75
|
|
|
$context['default_username'] = &$_REQUEST['u']; |
76
|
|
|
$context['default_password'] = ''; |
77
|
|
|
$context['never_expire'] = false; |
78
|
|
|
|
79
|
|
|
// Add the login chain to the link tree. |
80
|
|
|
$context['linktree'][] = array( |
81
|
|
|
'url' => $scripturl . '?action=login', |
82
|
|
|
'name' => $txt['login'], |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
// Set the login URL - will be used when the login process is done (but careful not to send us to an attachment). |
86
|
|
|
if (isset($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'dlattach') === false && preg_match('~(board|topic)[=,]~', $_SESSION['old_url']) != 0) |
87
|
|
|
$_SESSION['login_url'] = $_SESSION['old_url']; |
88
|
|
|
// This came from a valid hashed return url. Or something that knows our secrets... |
89
|
|
|
elseif (!empty($_REQUEST['return_hash']) && !empty($_REQUEST['return_to']) && hash_hmac('sha1', un_htmlspecialchars($_REQUEST['return_to']), get_auth_secret()) == $_REQUEST['return_hash']) |
90
|
|
|
$_SESSION['login_url'] = un_htmlspecialchars($_REQUEST['return_to']); |
91
|
|
|
elseif (isset($_SESSION['login_url']) && strpos($_SESSION['login_url'], 'dlattach') !== false) |
92
|
|
|
unset($_SESSION['login_url']); |
93
|
|
|
|
94
|
|
|
// Create a one time token. |
95
|
|
|
createToken('login'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Actually logs you in. |
100
|
|
|
* What it does: |
101
|
|
|
* - checks credentials and checks that login was successful. |
102
|
|
|
* - it employs protection against a specific IP or user trying to brute force |
103
|
|
|
* a login to an account. |
104
|
|
|
* - upgrades password encryption on login, if necessary. |
105
|
|
|
* - after successful login, redirects you to $_SESSION['login_url']. |
106
|
|
|
* - accessed from ?action=login2, by forms. |
107
|
|
|
* On error, uses the same templates Login() uses. |
108
|
|
|
*/ |
109
|
|
|
function Login2() |
110
|
|
|
{ |
111
|
|
|
global $txt, $scripturl, $user_info, $user_settings, $smcFunc; |
112
|
|
|
global $cookiename, $modSettings, $context, $sourcedir, $maintenance; |
113
|
|
|
|
114
|
|
|
// Check to ensure we're forcing SSL for authentication |
115
|
|
|
if (!empty($modSettings['force_ssl']) && empty($maintenance) && !httpsOn()) |
116
|
|
|
fatal_lang_error('login_ssl_required', false); |
117
|
|
|
|
118
|
|
|
// Load cookie authentication stuff. |
119
|
|
|
require_once($sourcedir . '/Subs-Auth.php'); |
120
|
|
|
|
121
|
|
|
/* This is true when: |
122
|
|
|
* We have a valid header indicating a JQXHR request. This is not sent during a cross domain request. |
123
|
|
|
* OR we have found: |
124
|
|
|
* 1. valid cors host |
125
|
|
|
* 2. A header indicating a SMF request |
126
|
|
|
* 3. The url has a ajax in either the GET or POST |
127
|
|
|
* These are not intended for security, but ensuring the request is intended for a JQXHR response. |
128
|
|
|
*/ |
129
|
|
|
if ( |
130
|
|
|
( |
131
|
|
|
!empty($_SERVER['HTTP_X_REQUESTED_WITH']) |
132
|
|
|
&& $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' |
133
|
|
|
) |
134
|
|
|
|| |
135
|
|
|
( |
136
|
|
|
!empty($context['valid_cors_found']) |
137
|
|
|
&& !empty($_SERVER['HTTP_X_SMF_AJAX']) |
138
|
|
|
&& isset($_REQUEST['ajax']) |
139
|
|
|
) |
140
|
|
|
) |
141
|
|
|
{ |
142
|
|
|
$context['from_ajax'] = true; |
143
|
|
|
$context['template_layers'] = array(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (isset($_GET['sa']) && $_GET['sa'] == 'salt' && !$user_info['is_guest']) |
147
|
|
|
{ |
148
|
|
|
// First check for 2.1 json-format cookie in $_COOKIE |
149
|
|
|
if (isset($_COOKIE[$cookiename]) && preg_match('~^{"0":\d+,"1":"[0-9a-f]*","2":\d+~', $_COOKIE[$cookiename]) === 1) |
150
|
|
|
list (,, $timeout) = $smcFunc['json_decode']($_COOKIE[$cookiename], true); |
151
|
|
|
|
152
|
|
|
// Try checking for 2.1 json-format cookie in $_SESSION |
153
|
|
|
elseif (isset($_SESSION['login_' . $cookiename]) && preg_match('~^{"0":\d+,"1":"[0-9a-f]*","2":\d+~', $_SESSION['login_' . $cookiename]) === 1) |
154
|
|
|
list (,, $timeout) = $smcFunc['json_decode']($_SESSION['login_' . $cookiename]); |
155
|
|
|
|
156
|
|
|
// Next, try checking for 2.0 serialized string cookie in $_COOKIE |
157
|
|
|
elseif (isset($_COOKIE[$cookiename]) && preg_match('~^a:[34]:\{i:0;i:\d+;i:1;s:(0|40):"([a-fA-F0-9]{40})?";i:2;[id]:\d+;~', $_COOKIE[$cookiename]) === 1) |
158
|
|
|
list (,, $timeout) = safe_unserialize($_COOKIE[$cookiename]); |
159
|
|
|
|
160
|
|
|
// Last, see if you need to fall back on checking for 2.0 serialized string cookie in $_SESSION |
161
|
|
|
elseif (isset($_SESSION['login_' . $cookiename]) && preg_match('~^a:[34]:\{i:0;i:\d+;i:1;s:(0|40):"([a-fA-F0-9]{40})?";i:2;[id]:\d+;~', $_SESSION['login_' . $cookiename]) === 1) |
162
|
|
|
list (,, $timeout) = safe_unserialize($_SESSION['login_' . $cookiename]); |
163
|
|
|
|
164
|
|
|
else |
165
|
|
|
{ |
166
|
|
|
loadLanguage('Errors'); |
167
|
|
|
trigger_error($txt['login_no_session_cookie'], E_USER_ERROR); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$user_settings['password_salt'] = bin2hex($smcFunc['random_bytes'](16)); |
171
|
|
|
updateMemberData($user_info['id'], array('password_salt' => $user_settings['password_salt'])); |
172
|
|
|
|
173
|
|
|
// Preserve the 2FA cookie? |
174
|
|
|
if (!empty($modSettings['tfa_mode']) && !empty($_COOKIE[$cookiename . '_tfa'])) |
175
|
|
|
{ |
176
|
|
|
list (,, $exp) = $smcFunc['json_decode']($_COOKIE[$cookiename . '_tfa'], true); |
177
|
|
|
setTFACookie((int) $exp - time(), $user_info['password_salt'], hash_salt($user_settings['tfa_backup'], $user_settings['password_salt'])); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
setLoginCookie((int) $timeout - time(), $user_info['id'], hash_salt($user_settings['passwd'], $user_settings['password_salt'])); |
|
|
|
|
181
|
|
|
|
182
|
|
|
redirectexit('action=login2;sa=check;member=' . $user_info['id'], $context['server']['needs_login_fix']); |
183
|
|
|
} |
184
|
|
|
// Double check the cookie... |
185
|
|
|
elseif (isset($_GET['sa']) && $_GET['sa'] == 'check') |
186
|
|
|
{ |
187
|
|
|
// Strike! You're outta there! |
188
|
|
|
if ($_GET['member'] != $user_info['id']) |
189
|
|
|
fatal_lang_error('login_cookie_error', false); |
190
|
|
|
|
191
|
|
|
$user_info['can_mod'] = allowedTo('access_mod_center') || (!$user_info['is_guest'] && ($user_info['mod_cache']['gq'] != '0=1' || $user_info['mod_cache']['bq'] != '0=1' || ($modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap'])))); |
192
|
|
|
|
193
|
|
|
// Some whitelisting for login_url... |
194
|
|
|
if (empty($_SESSION['login_url'])) |
195
|
|
|
redirectexit(empty($user_settings['tfa_secret']) ? '' : 'action=logintfa'); |
196
|
|
|
elseif (!empty($_SESSION['login_url']) && (strpos($_SESSION['login_url'], 'http://') === false && strpos($_SESSION['login_url'], 'https://') === false)) |
197
|
|
|
{ |
198
|
|
|
unset($_SESSION['login_url']); |
199
|
|
|
redirectexit(empty($user_settings['tfa_secret']) ? '' : 'action=logintfa'); |
200
|
|
|
} |
201
|
|
|
elseif (!empty($user_settings['tfa_secret'])) |
202
|
|
|
{ |
203
|
|
|
redirectexit('action=logintfa'); |
204
|
|
|
} |
205
|
|
|
else |
206
|
|
|
{ |
207
|
|
|
// Best not to clutter the session data too much... |
208
|
|
|
$temp = $_SESSION['login_url']; |
209
|
|
|
unset($_SESSION['login_url']); |
210
|
|
|
|
211
|
|
|
redirectexit($temp); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// Beyond this point you are assumed to be a guest trying to login. |
216
|
|
|
if (!$user_info['is_guest']) |
217
|
|
|
redirectexit(); |
218
|
|
|
|
219
|
|
|
// Are you guessing with a script? |
220
|
|
|
checkSession(); |
221
|
|
|
validateToken('login'); |
222
|
|
|
spamProtection('login'); |
223
|
|
|
|
224
|
|
|
// Set the login_url if it's not already set (but careful not to send us to an attachment). |
225
|
|
|
if ((empty($_SESSION['login_url']) && isset($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'dlattach') === false && preg_match('~(board|topic)[=,]~', $_SESSION['old_url']) != 0) || (isset($_GET['quicklogin']) && isset($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'login') === false)) |
226
|
|
|
$_SESSION['login_url'] = $_SESSION['old_url']; |
227
|
|
|
|
228
|
|
|
// Been guessing a lot, haven't we? |
229
|
|
|
if (isset($_SESSION['failed_login']) && $_SESSION['failed_login'] >= $modSettings['failed_login_threshold'] * 3) |
230
|
|
|
fatal_lang_error('login_threshold_fail', 'login'); |
231
|
|
|
|
232
|
|
|
// Set up the cookie length. (if it's invalid, just fall through and use the default.) |
233
|
|
|
if (isset($_POST['cookieneverexp']) || (!empty($_POST['cookielength']) && $_POST['cookielength'] == -1)) |
234
|
|
|
$modSettings['cookieTime'] = 3153600; |
235
|
|
|
elseif (!empty($_POST['cookielength']) && ($_POST['cookielength'] >= 1 && $_POST['cookielength'] <= 3153600)) |
236
|
|
|
$modSettings['cookieTime'] = (int) $_POST['cookielength']; |
237
|
|
|
|
238
|
|
|
loadLanguage('Login'); |
239
|
|
|
// Load the template stuff. |
240
|
|
|
loadTemplate('Login'); |
241
|
|
|
$context['sub_template'] = 'login'; |
242
|
|
|
|
243
|
|
|
// Create a one time token. |
244
|
|
|
createToken('login'); |
245
|
|
|
|
246
|
|
|
// Set up the default/fallback stuff. |
247
|
|
|
$context['default_username'] = isset($_POST['user']) ? preg_replace('~&#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', $smcFunc['htmlspecialchars']($_POST['user'])) : ''; |
248
|
|
|
$context['default_password'] = ''; |
249
|
|
|
$context['never_expire'] = $modSettings['cookieTime'] <= 525600; |
250
|
|
|
$context['login_errors'] = array($txt['error_occured']); |
251
|
|
|
$context['page_title'] = $txt['login']; |
252
|
|
|
|
253
|
|
|
// Add the login chain to the link tree. |
254
|
|
|
$context['linktree'][] = array( |
255
|
|
|
'url' => $scripturl . '?action=login', |
256
|
|
|
'name' => $txt['login'], |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
// You forgot to type your username, dummy! |
260
|
|
|
if (!isset($_POST['user']) || $_POST['user'] == '') |
261
|
|
|
{ |
262
|
|
|
$context['login_errors'] = array($txt['need_username']); |
263
|
|
|
return; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
// Hmm... maybe 'admin' will login with no password. Uhh... NO! |
267
|
|
|
if (!isset($_POST['passwrd']) || $_POST['passwrd'] == '') |
268
|
|
|
{ |
269
|
|
|
$context['login_errors'] = array($txt['no_password']); |
270
|
|
|
return; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
// No funky symbols either. |
274
|
|
|
if (preg_match('~[<>&"\'=\\\]~', preg_replace('~(&#(\\d{1,7}|x[0-9a-fA-F]{1,6});)~', '', $_POST['user'])) != 0) |
275
|
|
|
{ |
276
|
|
|
$context['login_errors'] = array($txt['error_invalid_characters_username']); |
277
|
|
|
return; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// And if it's too long, trim it back. |
281
|
|
|
if ($smcFunc['strlen']($_POST['user']) > 80) |
282
|
|
|
{ |
283
|
|
|
$_POST['user'] = $smcFunc['substr']($_POST['user'], 0, 79); |
284
|
|
|
$context['default_username'] = preg_replace('~&#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', $smcFunc['htmlspecialchars']($_POST['user'])); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
// Are we using any sort of integration to validate the login? |
288
|
|
|
if (in_array('retry', call_integration_hook('integrate_validate_login', array($_POST['user'], isset($_POST['passwrd']) ? $_POST['passwrd'] : null, $modSettings['cookieTime'])), true)) |
289
|
|
|
{ |
290
|
|
|
$context['login_errors'] = array($txt['incorrect_password']); |
291
|
|
|
return; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
// Load the data up! |
295
|
|
|
$request = $smcFunc['db_query']('', ' |
296
|
|
|
SELECT passwd, id_member, id_group, lngfile, is_activated, email_address, additional_groups, member_name, password_salt, |
297
|
|
|
passwd_flood, tfa_secret |
298
|
|
|
FROM {db_prefix}members |
299
|
|
|
WHERE ' . ($smcFunc['db_case_sensitive'] ? 'LOWER(member_name) = LOWER({string:user_name})' : 'member_name = {string:user_name}') . ' |
300
|
|
|
LIMIT 1', |
301
|
|
|
array( |
302
|
|
|
'user_name' => $smcFunc['db_case_sensitive'] ? strtolower($_POST['user']) : $_POST['user'], |
303
|
|
|
) |
304
|
|
|
); |
305
|
|
|
// Probably mistyped or their email, try it as an email address. (member_name first, though!) |
306
|
|
|
if ($smcFunc['db_num_rows']($request) == 0 && strpos($_POST['user'], '@') !== false) |
307
|
|
|
{ |
308
|
|
|
$smcFunc['db_free_result']($request); |
309
|
|
|
|
310
|
|
|
$request = $smcFunc['db_query']('', ' |
311
|
|
|
SELECT passwd, id_member, id_group, lngfile, is_activated, email_address, additional_groups, member_name, password_salt, |
312
|
|
|
passwd_flood, tfa_secret |
313
|
|
|
FROM {db_prefix}members |
314
|
|
|
WHERE email_address = {string:user_name} |
315
|
|
|
LIMIT 1', |
316
|
|
|
array( |
317
|
|
|
'user_name' => $_POST['user'], |
318
|
|
|
) |
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
// Let them try again, it didn't match anything... |
323
|
|
|
if ($smcFunc['db_num_rows']($request) == 0) |
324
|
|
|
{ |
325
|
|
|
$context['login_errors'] = array($txt['username_no_exist']); |
326
|
|
|
return; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
$user_settings = $smcFunc['db_fetch_assoc']($request); |
330
|
|
|
$smcFunc['db_free_result']($request); |
331
|
|
|
|
332
|
|
|
// Bad password! Thought you could fool the database?! |
333
|
|
|
if (!hash_verify_password($user_settings['member_name'], un_htmlspecialchars($_POST['passwrd']), $user_settings['passwd'])) |
334
|
|
|
{ |
335
|
|
|
// Let's be cautious, no hacking please. thanx. |
336
|
|
|
validatePasswordFlood($user_settings['id_member'], $user_settings['member_name'], $user_settings['passwd_flood']); |
337
|
|
|
|
338
|
|
|
// Maybe we were too hasty... let's try some other authentication methods. |
339
|
|
|
$other_passwords = array(); |
340
|
|
|
|
341
|
|
|
// None of the below cases will be used most of the time (because the salt is normally set.) |
342
|
|
|
if (!empty($modSettings['enable_password_conversion']) && $user_settings['password_salt'] == '') |
343
|
|
|
{ |
344
|
|
|
// YaBB SE, Discus, MD5 (used a lot), SHA-1 (used some), SMF 1.0.x, IkonBoard, and none at all. |
345
|
|
|
$other_passwords[] = crypt($_POST['passwrd'], substr($_POST['passwrd'], 0, 2)); |
346
|
|
|
$other_passwords[] = crypt($_POST['passwrd'], substr($user_settings['passwd'], 0, 2)); |
347
|
|
|
$other_passwords[] = md5($_POST['passwrd']); |
348
|
|
|
$other_passwords[] = sha1($_POST['passwrd']); |
349
|
|
|
$other_passwords[] = md5_hmac($_POST['passwrd'], strtolower($user_settings['member_name'])); |
350
|
|
|
$other_passwords[] = md5($_POST['passwrd'] . strtolower($user_settings['member_name'])); |
351
|
|
|
$other_passwords[] = md5(md5($_POST['passwrd'])); |
352
|
|
|
$other_passwords[] = $_POST['passwrd']; |
353
|
|
|
$other_passwords[] = crypt($_POST['passwrd'], $user_settings['passwd']); |
354
|
|
|
|
355
|
|
|
// This one is a strange one... MyPHP, crypt() on the MD5 hash. |
356
|
|
|
$other_passwords[] = crypt(md5($_POST['passwrd']), md5($_POST['passwrd'])); |
357
|
|
|
|
358
|
|
|
// Snitz style - SHA-256. Technically, this is a downgrade, but most PHP configurations don't support sha256 anyway. |
359
|
|
|
if (strlen($user_settings['passwd']) == 64 && function_exists('mhash') && defined('MHASH_SHA256')) |
360
|
|
|
$other_passwords[] = bin2hex(mhash(MHASH_SHA256, $_POST['passwrd'])); |
361
|
|
|
|
362
|
|
|
// phpBB3 users new hashing. We now support it as well ;). |
363
|
|
|
$other_passwords[] = phpBB3_password_check($_POST['passwrd'], $user_settings['passwd']); |
364
|
|
|
|
365
|
|
|
// APBoard 2 Login Method. |
366
|
|
|
$other_passwords[] = md5(crypt($_POST['passwrd'], 'CRYPT_MD5')); |
367
|
|
|
} |
368
|
|
|
// If the salt is set let's try some other options |
369
|
|
|
elseif (!empty($modSettings['enable_password_conversion']) && $user_settings['password_salt'] != '') |
370
|
|
|
{ |
371
|
|
|
// PHPBB 3 check this function exists in PHP 5.5 or higher |
372
|
|
|
if (function_exists('password_verify')) |
373
|
|
|
$other_passwords[] = password_verify($_POST['passwrd'],$user_settings['password_salt']); |
374
|
|
|
|
375
|
|
|
// PHP-Fusion |
376
|
|
|
$other_passwords[] = hash_hmac('sha256', $_POST['passwrd'], $user_settings['password_salt']); |
377
|
|
|
} |
378
|
|
|
// The hash should be 40 if it's SHA-1, so we're safe with more here too. |
379
|
|
|
elseif (!empty($modSettings['enable_password_conversion']) && strlen($user_settings['passwd']) == 32) |
380
|
|
|
{ |
381
|
|
|
// vBulletin 3 style hashing? Let's welcome them with open arms \o/. |
382
|
|
|
$other_passwords[] = md5(md5($_POST['passwrd']) . stripslashes($user_settings['password_salt'])); |
383
|
|
|
|
384
|
|
|
// Hmm.. p'raps it's Invision 2 style? |
385
|
|
|
$other_passwords[] = md5(md5($user_settings['password_salt']) . md5($_POST['passwrd'])); |
386
|
|
|
|
387
|
|
|
// Some common md5 ones. |
388
|
|
|
$other_passwords[] = md5($user_settings['password_salt'] . $_POST['passwrd']); |
389
|
|
|
$other_passwords[] = md5($_POST['passwrd'] . $user_settings['password_salt']); |
390
|
|
|
} |
391
|
|
|
elseif (strlen($user_settings['passwd']) == 40) |
392
|
|
|
{ |
393
|
|
|
// Maybe they are using a hash from before the password fix. |
394
|
|
|
// This is also valid for SMF 1.1 to 2.0 style of hashing, changed to bcrypt in SMF 2.1 |
395
|
|
|
$other_passwords[] = sha1(strtolower($user_settings['member_name']) . un_htmlspecialchars($_POST['passwrd'])); |
396
|
|
|
|
397
|
|
|
// BurningBoard3 style of hashing. |
398
|
|
|
if (!empty($modSettings['enable_password_conversion'])) |
399
|
|
|
$other_passwords[] = sha1($user_settings['password_salt'] . sha1($user_settings['password_salt'] . sha1($_POST['passwrd']))); |
400
|
|
|
|
401
|
|
|
// PunBB |
402
|
|
|
$other_passwords[] = sha1($user_settings['password_salt'] . sha1($_POST['passwrd'])); |
403
|
|
|
|
404
|
|
|
// Perhaps we converted to UTF-8 and have a valid password being hashed differently. |
405
|
|
|
if ($context['character_set'] == 'UTF-8' && !empty($modSettings['previousCharacterSet']) && $modSettings['previousCharacterSet'] != 'utf8') |
406
|
|
|
{ |
407
|
|
|
// Try iconv first, for no particular reason. |
408
|
|
|
if (function_exists('iconv')) |
409
|
|
|
$other_passwords['iconv'] = sha1(strtolower(iconv('UTF-8', $modSettings['previousCharacterSet'], $user_settings['member_name'])) . un_htmlspecialchars(iconv('UTF-8', $modSettings['previousCharacterSet'], $_POST['passwrd']))); |
410
|
|
|
|
411
|
|
|
// Say it aint so, iconv failed! |
412
|
|
|
if (empty($other_passwords['iconv']) && function_exists('mb_convert_encoding')) |
413
|
|
|
$other_passwords[] = sha1(strtolower(mb_convert_encoding($user_settings['member_name'], 'UTF-8', $modSettings['previousCharacterSet'])) . un_htmlspecialchars(mb_convert_encoding($_POST['passwrd'], 'UTF-8', $modSettings['previousCharacterSet']))); |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
// SMF's sha1 function can give a funny result on Linux (Not our fault!). If we've now got the real one let the old one be valid! |
418
|
|
|
if (stripos(PHP_OS, 'win') !== 0 && strlen($user_settings['passwd']) < hash_length()) |
419
|
|
|
{ |
420
|
|
|
require_once($sourcedir . '/Subs-Compat.php'); |
421
|
|
|
$other_passwords[] = sha1_smf(strtolower($user_settings['member_name']) . un_htmlspecialchars($_POST['passwrd'])); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
// Allows mods to easily extend the $other_passwords array |
425
|
|
|
call_integration_hook('integrate_other_passwords', array(&$other_passwords)); |
426
|
|
|
|
427
|
|
|
// Whichever encryption it was using, let's make it use SMF's now ;). |
428
|
|
|
if (in_array($user_settings['passwd'], $other_passwords)) |
429
|
|
|
{ |
430
|
|
|
$user_settings['passwd'] = hash_password($user_settings['member_name'], un_htmlspecialchars($_POST['passwrd'])); |
431
|
|
|
$user_settings['password_salt'] = bin2hex($smcFunc['random_bytes'](16)); |
432
|
|
|
|
433
|
|
|
// Update the password and set up the hash. |
434
|
|
|
updateMemberData($user_settings['id_member'], array('passwd' => $user_settings['passwd'], 'password_salt' => $user_settings['password_salt'], 'passwd_flood' => '')); |
435
|
|
|
} |
436
|
|
|
// Okay, they for sure didn't enter the password! |
437
|
|
|
else |
438
|
|
|
{ |
439
|
|
|
// They've messed up again - keep a count to see if they need a hand. |
440
|
|
|
$_SESSION['failed_login'] = isset($_SESSION['failed_login']) ? ($_SESSION['failed_login'] + 1) : 1; |
441
|
|
|
|
442
|
|
|
// Hmm... don't remember it, do you? Here, try the password reminder ;). |
443
|
|
|
if ($_SESSION['failed_login'] >= $modSettings['failed_login_threshold']) |
444
|
|
|
redirectexit('action=reminder'); |
445
|
|
|
// We'll give you another chance... |
446
|
|
|
else |
447
|
|
|
{ |
448
|
|
|
// Log an error so we know that it didn't go well in the error log. |
449
|
|
|
log_error($txt['incorrect_password'] . ' - <span class="remove">' . $user_settings['member_name'] . '</span>', 'user'); |
450
|
|
|
|
451
|
|
|
$context['login_errors'] = array($txt['incorrect_password']); |
452
|
|
|
return; |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
} |
456
|
|
|
elseif (!empty($user_settings['passwd_flood'])) |
457
|
|
|
{ |
458
|
|
|
// Let's be sure they weren't a little hacker. |
459
|
|
|
validatePasswordFlood($user_settings['id_member'], $user_settings['member_name'], $user_settings['passwd_flood'], true); |
460
|
|
|
|
461
|
|
|
// If we got here then we can reset the flood counter. |
462
|
|
|
updateMemberData($user_settings['id_member'], array('passwd_flood' => '')); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
// Correct password, but they've got no salt; fix it! |
466
|
|
|
if (strlen($user_settings['password_salt']) < 32) |
467
|
|
|
{ |
468
|
|
|
$user_settings['password_salt'] = bin2hex($smcFunc['random_bytes'](16)); |
469
|
|
|
updateMemberData($user_settings['id_member'], array('password_salt' => $user_settings['password_salt'])); |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
// Check their activation status. |
473
|
|
|
if (!checkActivation()) |
474
|
|
|
return; |
475
|
|
|
|
476
|
|
|
DoLogin(); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Allows the user to enter their Two-Factor Authentication code |
481
|
|
|
*/ |
482
|
|
|
function LoginTFA() |
483
|
|
|
{ |
484
|
|
|
global $sourcedir, $txt, $context, $user_info, $modSettings, $scripturl; |
485
|
|
|
|
486
|
|
|
if (!$user_info['is_guest'] || empty($context['tfa_member']) || empty($modSettings['tfa_mode'])) |
487
|
|
|
fatal_lang_error('no_access', false); |
488
|
|
|
|
489
|
|
|
loadLanguage('Profile'); |
490
|
|
|
require_once($sourcedir . '/Class-TOTP.php'); |
491
|
|
|
|
492
|
|
|
$member = $context['tfa_member']; |
493
|
|
|
|
494
|
|
|
// Prevent replay attacks by limiting at least 2 minutes before they can log in again via 2FA |
495
|
|
|
if (time() - $member['last_login'] < 120) |
496
|
|
|
fatal_lang_error('tfa_wait', false); |
497
|
|
|
|
498
|
|
|
$totp = new \TOTP\Auth($member['tfa_secret']); |
499
|
|
|
$totp->setRange(1); |
500
|
|
|
|
501
|
|
|
/* This is true when: |
502
|
|
|
* We have a valid header indicating a JQXHR request. This is not sent during a cross domain request. |
503
|
|
|
* OR we have found: |
504
|
|
|
* 1. valid cors host |
505
|
|
|
* 2. A header indicating a SMF request |
506
|
|
|
* 3. The url has a ajax in either the GET or POST |
507
|
|
|
* These are not intended for security, but ensuring the request is intended for a JQXHR response. |
508
|
|
|
*/ |
509
|
|
|
if ( |
510
|
|
|
( |
511
|
|
|
!empty($_SERVER['HTTP_X_REQUESTED_WITH']) |
512
|
|
|
&& $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' |
513
|
|
|
) |
514
|
|
|
|| |
515
|
|
|
( |
516
|
|
|
!empty($context['valid_cors_found']) |
517
|
|
|
&& !empty($_SERVER['HTTP_X_SMF_AJAX']) |
518
|
|
|
&& isset($_REQUEST['ajax']) |
519
|
|
|
) |
520
|
|
|
) |
521
|
|
|
{ |
522
|
|
|
$context['from_ajax'] = true; |
523
|
|
|
$context['template_layers'] = array(); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
if (!empty($_POST['tfa_code']) && empty($_POST['tfa_backup'])) |
527
|
|
|
{ |
528
|
|
|
// Check to ensure we're forcing SSL for authentication |
529
|
|
|
if (!empty($modSettings['force_ssl']) && empty($maintenance) && !httpsOn()) |
|
|
|
|
530
|
|
|
fatal_lang_error('login_ssl_required', false); |
531
|
|
|
|
532
|
|
|
$code = $_POST['tfa_code']; |
533
|
|
|
|
534
|
|
|
if (strlen($code) == $totp->getCodeLength() && $totp->validateCode($code)) |
535
|
|
|
{ |
536
|
|
|
updateMemberData($member['id_member'], array('last_login' => time())); |
537
|
|
|
|
538
|
|
|
setTFACookie(3153600, $member['id_member'], hash_salt($member['tfa_backup'], $member['password_salt'])); |
539
|
|
|
redirectexit(); |
540
|
|
|
} |
541
|
|
|
else |
542
|
|
|
{ |
543
|
|
|
validatePasswordFlood($member['id_member'], $member['member_name'], $member['passwd_flood'], false, true); |
544
|
|
|
|
545
|
|
|
$context['tfa_error'] = true; |
546
|
|
|
$context['tfa_value'] = $_POST['tfa_code']; |
547
|
|
|
} |
548
|
|
|
} |
549
|
|
|
elseif (!empty($_POST['tfa_backup'])) |
550
|
|
|
{ |
551
|
|
|
// Check to ensure we're forcing SSL for authentication |
552
|
|
|
if (!empty($modSettings['force_ssl']) && empty($maintenance) && !httpsOn()) |
553
|
|
|
fatal_lang_error('login_ssl_required', false); |
554
|
|
|
|
555
|
|
|
$backup = $_POST['tfa_backup']; |
556
|
|
|
|
557
|
|
|
if (hash_verify_password($member['member_name'], $backup, $member['tfa_backup'])) |
558
|
|
|
{ |
559
|
|
|
// Get rid of their current TFA settings |
560
|
|
|
updateMemberData($member['id_member'], array( |
561
|
|
|
'tfa_secret' => '', |
562
|
|
|
'tfa_backup' => '', |
563
|
|
|
'last_login' => time(), |
564
|
|
|
)); |
565
|
|
|
setTFACookie(3153600, $member['id_member'], hash_salt($member['tfa_backup'], $member['password_salt'])); |
566
|
|
|
redirectexit('action=profile;area=tfasetup;backup'); |
567
|
|
|
} |
568
|
|
|
else |
569
|
|
|
{ |
570
|
|
|
validatePasswordFlood($member['id_member'], $member['member_name'], $member['passwd_flood'], false, true); |
571
|
|
|
|
572
|
|
|
$context['tfa_backup_error'] = true; |
573
|
|
|
$context['tfa_value'] = $_POST['tfa_code']; |
574
|
|
|
$context['tfa_backup_value'] = $_POST['tfa_backup']; |
575
|
|
|
} |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
loadTemplate('Login'); |
579
|
|
|
$context['sub_template'] = 'login_tfa'; |
580
|
|
|
$context['page_title'] = $txt['login']; |
581
|
|
|
$context['tfa_url'] = $scripturl . '?action=logintfa'; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* Check activation status of the current user. |
586
|
|
|
*/ |
587
|
|
|
function checkActivation() |
588
|
|
|
{ |
589
|
|
|
global $context, $txt, $scripturl, $user_settings, $modSettings; |
590
|
|
|
|
591
|
|
|
if (!isset($context['login_errors'])) |
592
|
|
|
$context['login_errors'] = array(); |
593
|
|
|
|
594
|
|
|
// What is the true activation status of this account? |
595
|
|
|
$activation_status = $user_settings['is_activated'] > 10 ? $user_settings['is_activated'] - 10 : $user_settings['is_activated']; |
596
|
|
|
|
597
|
|
|
// Check if the account is activated - COPPA first... |
598
|
|
|
if ($activation_status == 5) |
599
|
|
|
{ |
600
|
|
|
$context['login_errors'][] = $txt['coppa_no_consent'] . ' <a href="' . $scripturl . '?action=coppa;member=' . $user_settings['id_member'] . '">' . $txt['coppa_need_more_details'] . '</a>'; |
601
|
|
|
return false; |
602
|
|
|
} |
603
|
|
|
// Awaiting approval still? |
604
|
|
|
elseif ($activation_status == 3) |
605
|
|
|
fatal_lang_error('still_awaiting_approval', 'user'); |
606
|
|
|
// Awaiting deletion, changed their mind? |
607
|
|
|
elseif ($activation_status == 4) |
608
|
|
|
{ |
609
|
|
|
if (isset($_REQUEST['undelete'])) |
610
|
|
|
{ |
611
|
|
|
updateMemberData($user_settings['id_member'], array('is_activated' => 1)); |
612
|
|
|
updateSettings(array('unapprovedMembers' => ($modSettings['unapprovedMembers'] > 0 ? $modSettings['unapprovedMembers'] - 1 : 0))); |
613
|
|
|
} |
614
|
|
|
else |
615
|
|
|
{ |
616
|
|
|
$context['disable_login_hashing'] = true; |
617
|
|
|
$context['login_errors'][] = $txt['awaiting_delete_account']; |
618
|
|
|
$context['login_show_undelete'] = true; |
619
|
|
|
return false; |
620
|
|
|
} |
621
|
|
|
} |
622
|
|
|
// Standard activation? |
623
|
|
|
elseif ($activation_status != 1) |
624
|
|
|
{ |
625
|
|
|
log_error($txt['activate_not_completed1'] . ' - <span class="remove">' . $user_settings['member_name'] . '</span>', 'user'); |
626
|
|
|
|
627
|
|
|
$context['login_errors'][] = $txt['activate_not_completed1'] . ' <a href="' . $scripturl . '?action=activate;sa=resend;u=' . $user_settings['id_member'] . '">' . $txt['activate_not_completed2'] . '</a>'; |
628
|
|
|
return false; |
629
|
|
|
} |
630
|
|
|
return true; |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
/** |
634
|
|
|
* Perform the logging in. (set cookie, call hooks, etc) |
635
|
|
|
*/ |
636
|
|
|
function DoLogin() |
637
|
|
|
{ |
638
|
|
|
global $user_info, $user_settings, $smcFunc; |
639
|
|
|
global $maintenance, $modSettings, $context, $sourcedir; |
640
|
|
|
|
641
|
|
|
// Load cookie authentication stuff. |
642
|
|
|
require_once($sourcedir . '/Subs-Auth.php'); |
643
|
|
|
|
644
|
|
|
// Call login integration functions. |
645
|
|
|
call_integration_hook('integrate_login', array($user_settings['member_name'], null, $modSettings['cookieTime'])); |
646
|
|
|
|
647
|
|
|
// Get ready to set the cookie... |
648
|
|
|
$user_info['id'] = $user_settings['id_member']; |
649
|
|
|
|
650
|
|
|
// Bam! Cookie set. A session too, just in case. |
651
|
|
|
setLoginCookie(60 * $modSettings['cookieTime'], $user_settings['id_member'], hash_salt($user_settings['passwd'], $user_settings['password_salt'])); |
652
|
|
|
|
653
|
|
|
// Reset the login threshold. |
654
|
|
|
if (isset($_SESSION['failed_login'])) |
655
|
|
|
unset($_SESSION['failed_login']); |
656
|
|
|
|
657
|
|
|
$user_info['is_guest'] = false; |
658
|
|
|
$user_settings['additional_groups'] = explode(',', $user_settings['additional_groups']); |
659
|
|
|
$user_info['is_admin'] = $user_settings['id_group'] == 1 || in_array(1, $user_settings['additional_groups']); |
660
|
|
|
|
661
|
|
|
// Are you banned? |
662
|
|
|
is_not_banned(true); |
663
|
|
|
|
664
|
|
|
// Don't stick the language or theme after this point. |
665
|
|
|
unset($_SESSION['language'], $_SESSION['id_theme']); |
666
|
|
|
|
667
|
|
|
// First login? |
668
|
|
|
$request = $smcFunc['db_query']('', ' |
669
|
|
|
SELECT last_login |
670
|
|
|
FROM {db_prefix}members |
671
|
|
|
WHERE id_member = {int:id_member} |
672
|
|
|
AND last_login = 0', |
673
|
|
|
array( |
674
|
|
|
'id_member' => $user_info['id'], |
675
|
|
|
) |
676
|
|
|
); |
677
|
|
|
if ($smcFunc['db_num_rows']($request) == 1) |
678
|
|
|
$_SESSION['first_login'] = true; |
679
|
|
|
else |
680
|
|
|
unset($_SESSION['first_login']); |
681
|
|
|
$smcFunc['db_free_result']($request); |
682
|
|
|
|
683
|
|
|
// You've logged in, haven't you? |
684
|
|
|
$update = array('member_ip' => $user_info['ip'], 'member_ip2' => $_SERVER['BAN_CHECK_IP']); |
685
|
|
|
if (empty($user_settings['tfa_secret'])) |
686
|
|
|
$update['last_login'] = time(); |
687
|
|
|
updateMemberData($user_info['id'], $update); |
688
|
|
|
|
689
|
|
|
// Get rid of the online entry for that old guest.... |
690
|
|
|
$smcFunc['db_query']('', ' |
691
|
|
|
DELETE FROM {db_prefix}log_online |
692
|
|
|
WHERE session = {string:session}', |
693
|
|
|
array( |
694
|
|
|
'session' => 'ip' . $user_info['ip'], |
695
|
|
|
) |
696
|
|
|
); |
697
|
|
|
$_SESSION['log_time'] = 0; |
698
|
|
|
|
699
|
|
|
// Log this entry, only if we have it enabled. |
700
|
|
|
if (!empty($modSettings['loginHistoryDays'])) |
701
|
|
|
$smcFunc['db_insert']('insert', |
702
|
|
|
'{db_prefix}member_logins', |
703
|
|
|
array( |
704
|
|
|
'id_member' => 'int', 'time' => 'int', 'ip' => 'inet', 'ip2' => 'inet', |
705
|
|
|
), |
706
|
|
|
array( |
707
|
|
|
$user_info['id'], time(), $user_info['ip'], $user_info['ip2'] |
708
|
|
|
), |
709
|
|
|
array( |
710
|
|
|
'id_member', 'time' |
711
|
|
|
) |
712
|
|
|
); |
713
|
|
|
|
714
|
|
|
// Just log you back out if it's in maintenance mode and you AREN'T an admin. |
715
|
|
|
if (empty($maintenance) || allowedTo('admin_forum')) |
716
|
|
|
redirectexit('action=login2;sa=check;member=' . $user_info['id'], $context['server']['needs_login_fix']); |
717
|
|
|
else |
718
|
|
|
redirectexit('action=logout;' . $context['session_var'] . '=' . $context['session_id'], $context['server']['needs_login_fix']); |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
/** |
722
|
|
|
* Logs the current user out of their account. |
723
|
|
|
* It requires that the session hash is sent as well, to prevent automatic logouts by images or javascript. |
724
|
|
|
* It redirects back to $_SESSION['logout_url'], if it exists. |
725
|
|
|
* It is accessed via ?action=logout;session_var=... |
726
|
|
|
* |
727
|
|
|
* @param bool $internal If true, it doesn't check the session |
728
|
|
|
* @param bool $redirect Whether or not to redirect the user after they log out |
729
|
|
|
*/ |
730
|
|
|
function Logout($internal = false, $redirect = true) |
731
|
|
|
{ |
732
|
|
|
global $sourcedir, $user_info, $user_settings, $context, $smcFunc, $cookiename, $modSettings; |
733
|
|
|
|
734
|
|
|
// They decided to cancel a logout? |
735
|
|
|
if (!$internal && isset($_POST['cancel']) && isset($_GET[$context['session_var']])) |
736
|
|
|
redirectexit(!empty($_SESSION['logout_return']) ? $_SESSION['logout_return'] : ''); |
737
|
|
|
// Prompt to logout? |
738
|
|
|
elseif (!$internal && !isset($_GET[$context['session_var']])) |
739
|
|
|
{ |
740
|
|
|
loadLanguage('Login'); |
741
|
|
|
loadTemplate('Login'); |
742
|
|
|
$context['sub_template'] = 'logout'; |
743
|
|
|
|
744
|
|
|
// This came from a valid hashed return url. Or something that knows our secrets... |
745
|
|
|
if (!empty($_REQUEST['return_hash']) && !empty($_REQUEST['return_to']) && hash_hmac('sha1', un_htmlspecialchars($_REQUEST['return_to']), get_auth_secret()) == $_REQUEST['return_hash']) |
746
|
|
|
{ |
747
|
|
|
$_SESSION['logout_url'] = un_htmlspecialchars($_REQUEST['return_to']); |
748
|
|
|
$_SESSION['logout_return'] = $_SESSION['logout_url']; |
749
|
|
|
} |
750
|
|
|
// Setup the return address. |
751
|
|
|
elseif (isset($_SESSION['old_url'])) |
752
|
|
|
$_SESSION['logout_return'] = $_SESSION['old_url']; |
753
|
|
|
|
754
|
|
|
// Don't go any further. |
755
|
|
|
return; |
756
|
|
|
} |
757
|
|
|
// Make sure they aren't being auto-logged out. |
758
|
|
|
elseif (!$internal && isset($_GET[$context['session_var']])) |
759
|
|
|
checkSession('get'); |
760
|
|
|
|
761
|
|
|
require_once($sourcedir . '/Subs-Auth.php'); |
762
|
|
|
|
763
|
|
|
if (isset($_SESSION['pack_ftp'])) |
764
|
|
|
$_SESSION['pack_ftp'] = null; |
765
|
|
|
|
766
|
|
|
// It won't be first login anymore. |
767
|
|
|
unset($_SESSION['first_login']); |
768
|
|
|
|
769
|
|
|
// Just ensure they aren't a guest! |
770
|
|
|
if (!$user_info['is_guest']) |
771
|
|
|
{ |
772
|
|
|
// Pass the logout information to integrations. |
773
|
|
|
call_integration_hook('integrate_logout', array($user_settings['member_name'])); |
774
|
|
|
|
775
|
|
|
// If you log out, you aren't online anymore :P. |
776
|
|
|
$smcFunc['db_query']('', ' |
777
|
|
|
DELETE FROM {db_prefix}log_online |
778
|
|
|
WHERE id_member = {int:current_member}', |
779
|
|
|
array( |
780
|
|
|
'current_member' => $user_info['id'], |
781
|
|
|
) |
782
|
|
|
); |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
$_SESSION['log_time'] = 0; |
786
|
|
|
|
787
|
|
|
// Empty the cookie! (set it in the past, and for id_member = 0) |
788
|
|
|
setLoginCookie(-3600, 0); |
789
|
|
|
|
790
|
|
|
// And some other housekeeping while we're at it. |
791
|
|
|
$salt = bin2hex($smcFunc['random_bytes'](16)); |
792
|
|
|
if (!empty($user_info['id'])) |
793
|
|
|
updateMemberData($user_info['id'], array('password_salt' => $salt)); |
794
|
|
|
|
795
|
|
|
if (!empty($modSettings['tfa_mode']) && !empty($user_info['id']) && !empty($_COOKIE[$cookiename . '_tfa'])) |
796
|
|
|
{ |
797
|
|
|
list (,, $exp) = $smcFunc['json_decode']($_COOKIE[$cookiename . '_tfa'], true); |
798
|
|
|
setTFACookie((int) $exp - time(), $salt, hash_salt($user_settings['tfa_backup'], $salt)); |
|
|
|
|
799
|
|
|
} |
800
|
|
|
|
801
|
|
|
session_destroy(); |
802
|
|
|
|
803
|
|
|
// Off to the merry board index we go! |
804
|
|
|
if ($redirect) |
805
|
|
|
{ |
806
|
|
|
if (empty($_SESSION['logout_url'])) |
807
|
|
|
redirectexit('', $context['server']['needs_login_fix']); |
808
|
|
|
elseif (!empty($_SESSION['logout_url']) && (strpos($_SESSION['logout_url'], 'http://') === false && strpos($_SESSION['logout_url'], 'https://') === false)) |
809
|
|
|
{ |
810
|
|
|
unset ($_SESSION['logout_url']); |
811
|
|
|
redirectexit(); |
812
|
|
|
} |
813
|
|
|
else |
814
|
|
|
{ |
815
|
|
|
$temp = $_SESSION['logout_url']; |
816
|
|
|
unset($_SESSION['logout_url']); |
817
|
|
|
|
818
|
|
|
redirectexit($temp, $context['server']['needs_login_fix']); |
819
|
|
|
} |
820
|
|
|
} |
821
|
|
|
} |
822
|
|
|
|
823
|
|
|
/** |
824
|
|
|
* MD5 Encryption used for older passwords. (SMF 1.0.x/YaBB SE 1.5.x hashing) |
825
|
|
|
* |
826
|
|
|
* @param string $data The data |
827
|
|
|
* @param string $key The key |
828
|
|
|
* @return string The HMAC MD5 of data with key |
829
|
|
|
*/ |
830
|
|
|
function md5_hmac($data, $key) |
831
|
|
|
{ |
832
|
|
|
$key = str_pad(strlen($key) <= 64 ? $key : pack('H*', md5($key)), 64, chr(0x00)); |
833
|
|
|
return md5(($key ^ str_repeat(chr(0x5c), 64)) . pack('H*', md5(($key ^ str_repeat(chr(0x36), 64)) . $data))); |
834
|
|
|
} |
835
|
|
|
|
836
|
|
|
/** |
837
|
|
|
* Custom encryption for phpBB3 based passwords. |
838
|
|
|
* |
839
|
|
|
* @param string $passwd The raw (unhashed) password |
840
|
|
|
* @param string $passwd_hash The hashed password |
841
|
|
|
* @return string The hashed version of $passwd |
842
|
|
|
*/ |
843
|
|
|
function phpBB3_password_check($passwd, $passwd_hash) |
844
|
|
|
{ |
845
|
|
|
// Too long or too short? |
846
|
|
|
if (strlen($passwd_hash) != 34) |
847
|
|
|
return; |
848
|
|
|
|
849
|
|
|
// Range of characters allowed. |
850
|
|
|
$range = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; |
851
|
|
|
|
852
|
|
|
// Tests |
853
|
|
|
$strpos = strpos($range, $passwd_hash[3]); |
854
|
|
|
$count = 1 << $strpos; |
855
|
|
|
$salt = substr($passwd_hash, 4, 8); |
856
|
|
|
|
857
|
|
|
$hash = md5($salt . $passwd, true); |
858
|
|
|
for (; $count != 0; --$count) |
859
|
|
|
$hash = md5($hash . $passwd, true); |
860
|
|
|
|
861
|
|
|
$output = substr($passwd_hash, 0, 12); |
862
|
|
|
$i = 0; |
863
|
|
|
while ($i < 16) |
864
|
|
|
{ |
865
|
|
|
$value = ord($hash[$i++]); |
866
|
|
|
$output .= $range[$value & 0x3f]; |
867
|
|
|
|
868
|
|
|
if ($i < 16) |
869
|
|
|
$value |= ord($hash[$i]) << 8; |
870
|
|
|
|
871
|
|
|
$output .= $range[($value >> 6) & 0x3f]; |
872
|
|
|
|
873
|
|
|
if ($i++ >= 16) |
874
|
|
|
break; |
875
|
|
|
|
876
|
|
|
if ($i < 16) |
877
|
|
|
$value |= ord($hash[$i]) << 16; |
878
|
|
|
|
879
|
|
|
$output .= $range[($value >> 12) & 0x3f]; |
880
|
|
|
|
881
|
|
|
if ($i++ >= 16) |
882
|
|
|
break; |
883
|
|
|
|
884
|
|
|
$output .= $range[($value >> 18) & 0x3f]; |
885
|
|
|
} |
886
|
|
|
|
887
|
|
|
// Return now. |
888
|
|
|
return $output; |
889
|
|
|
} |
890
|
|
|
|
891
|
|
|
/** |
892
|
|
|
* This protects against brute force attacks on a member's password. |
893
|
|
|
* Importantly, even if the password was right we DON'T TELL THEM! |
894
|
|
|
* |
895
|
|
|
* @param int $id_member The ID of the member |
896
|
|
|
* @param string $member_name The name of the member. |
897
|
|
|
* @param bool|string $password_flood_value False if we don't have a flood value, otherwise a string with a timestamp and number of tries separated by a | |
898
|
|
|
* @param bool $was_correct Whether or not the password was correct |
899
|
|
|
* @param bool $tfa Whether we're validating for two-factor authentication |
900
|
|
|
*/ |
901
|
|
|
function validatePasswordFlood($id_member, $member_name, $password_flood_value = false, $was_correct = false, $tfa = false) |
902
|
|
|
{ |
903
|
|
|
global $cookiename, $sourcedir; |
904
|
|
|
|
905
|
|
|
// As this is only brute protection, we allow 5 attempts every 10 seconds. |
906
|
|
|
|
907
|
|
|
// Destroy any session or cookie data about this member, as they validated wrong. |
908
|
|
|
// Only if they're not validating for 2FA |
909
|
|
|
if (!$tfa) |
910
|
|
|
{ |
911
|
|
|
require_once($sourcedir . '/Subs-Auth.php'); |
912
|
|
|
setLoginCookie(-3600, 0); |
913
|
|
|
|
914
|
|
|
if (isset($_SESSION['login_' . $cookiename])) |
915
|
|
|
unset($_SESSION['login_' . $cookiename]); |
916
|
|
|
} |
917
|
|
|
|
918
|
|
|
// We need a member! |
919
|
|
|
if (!$id_member) |
920
|
|
|
{ |
921
|
|
|
// Redirect back! |
922
|
|
|
redirectexit(); |
923
|
|
|
|
924
|
|
|
// Probably not needed, but still make sure... |
925
|
|
|
fatal_lang_error('no_access', false); |
926
|
|
|
} |
927
|
|
|
|
928
|
|
|
// Right, have we got a flood value? |
929
|
|
|
if ($password_flood_value !== false) |
930
|
|
|
@list ($time_stamp, $number_tries) = explode('|', $password_flood_value); |
|
|
|
|
931
|
|
|
|
932
|
|
|
// Timestamp or number of tries invalid? |
933
|
|
|
if (empty($number_tries) || empty($time_stamp)) |
934
|
|
|
{ |
935
|
|
|
$number_tries = 0; |
936
|
|
|
$time_stamp = time(); |
937
|
|
|
} |
938
|
|
|
|
939
|
|
|
// They've failed logging in already |
940
|
|
|
if (!empty($number_tries)) |
941
|
|
|
{ |
942
|
|
|
// Give them less chances if they failed before |
943
|
|
|
$number_tries = $time_stamp < time() - 20 ? 2 : $number_tries; |
944
|
|
|
|
945
|
|
|
// They are trying too fast, make them wait longer |
946
|
|
|
if ($time_stamp < time() - 10) |
947
|
|
|
$time_stamp = time(); |
948
|
|
|
} |
949
|
|
|
|
950
|
|
|
$number_tries++; |
951
|
|
|
|
952
|
|
|
// Broken the law? |
953
|
|
|
if ($number_tries > 5) |
954
|
|
|
fatal_lang_error('login_threshold_brute_fail', 'login', [$member_name]); |
955
|
|
|
|
956
|
|
|
// Otherwise set the members data. If they correct on their first attempt then we actually clear it, otherwise we set it! |
957
|
|
|
updateMemberData($id_member, array('passwd_flood' => $was_correct && $number_tries == 1 ? '' : $time_stamp . '|' . $number_tries)); |
958
|
|
|
|
959
|
|
|
} |
960
|
|
|
|
961
|
|
|
?> |