albertlast /
SMF2.1
| 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 2025 Simple Machines and individual contributors |
||||
| 12 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||||
| 13 | * |
||||
| 14 | * @version 2.1.5 |
||||
| 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 | throw new \Exception('login_no_session_cookie'); |
||||
| 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 | // MyBB |
||||
| 379 | $other_passwords[] = md5(md5($user_settings['password_salt']) . md5($_POST['passwrd'])); |
||||
| 380 | } |
||||
| 381 | // The hash should be 40 if it's SHA-1, so we're safe with more here too. |
||||
| 382 | elseif (!empty($modSettings['enable_password_conversion']) && strlen($user_settings['passwd']) == 32) |
||||
| 383 | { |
||||
| 384 | // vBulletin 3 style hashing? Let's welcome them with open arms \o/. |
||||
| 385 | $other_passwords[] = md5(md5($_POST['passwrd']) . stripslashes($user_settings['password_salt'])); |
||||
| 386 | |||||
| 387 | // Hmm.. p'raps it's Invision 2 style? |
||||
| 388 | $other_passwords[] = md5(md5($user_settings['password_salt']) . md5($_POST['passwrd'])); |
||||
| 389 | |||||
| 390 | // Some common md5 ones. |
||||
| 391 | $other_passwords[] = md5($user_settings['password_salt'] . $_POST['passwrd']); |
||||
| 392 | $other_passwords[] = md5($_POST['passwrd'] . $user_settings['password_salt']); |
||||
| 393 | } |
||||
| 394 | elseif (strlen($user_settings['passwd']) == 40) |
||||
| 395 | { |
||||
| 396 | // Maybe they are using a hash from before the password fix. |
||||
| 397 | // This is also valid for SMF 1.1 to 2.0 style of hashing, changed to bcrypt in SMF 2.1 |
||||
| 398 | $other_passwords[] = sha1(strtolower($user_settings['member_name']) . un_htmlspecialchars($_POST['passwrd'])); |
||||
| 399 | |||||
| 400 | // BurningBoard3 style of hashing. |
||||
| 401 | if (!empty($modSettings['enable_password_conversion'])) |
||||
| 402 | $other_passwords[] = sha1($user_settings['password_salt'] . sha1($user_settings['password_salt'] . sha1($_POST['passwrd']))); |
||||
| 403 | |||||
| 404 | // PunBB |
||||
| 405 | $other_passwords[] = sha1($user_settings['password_salt'] . sha1($_POST['passwrd'])); |
||||
| 406 | |||||
| 407 | // Perhaps we converted to UTF-8 and have a valid password being hashed differently. |
||||
| 408 | if ($context['character_set'] == 'UTF-8' && !empty($modSettings['previousCharacterSet']) && $modSettings['previousCharacterSet'] != 'utf8') |
||||
| 409 | { |
||||
| 410 | // Try iconv first, for no particular reason. |
||||
| 411 | if (function_exists('iconv')) |
||||
| 412 | $other_passwords['iconv'] = sha1(strtolower(iconv('UTF-8', $modSettings['previousCharacterSet'], $user_settings['member_name'])) . un_htmlspecialchars(iconv('UTF-8', $modSettings['previousCharacterSet'], $_POST['passwrd']))); |
||||
| 413 | |||||
| 414 | // Say it aint so, iconv failed! |
||||
| 415 | if (empty($other_passwords['iconv']) && function_exists('mb_convert_encoding')) |
||||
| 416 | $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']))); |
||||
| 417 | } |
||||
| 418 | } |
||||
| 419 | |||||
| 420 | // 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! |
||||
| 421 | if (stripos(PHP_OS, 'win') !== 0 && strlen($user_settings['passwd']) < hash_length()) |
||||
| 422 | { |
||||
| 423 | require_once($sourcedir . '/Subs-Compat.php'); |
||||
| 424 | $other_passwords[] = sha1_smf(strtolower($user_settings['member_name']) . un_htmlspecialchars($_POST['passwrd'])); |
||||
| 425 | } |
||||
| 426 | |||||
| 427 | // Allows mods to easily extend the $other_passwords array |
||||
| 428 | call_integration_hook('integrate_other_passwords', array(&$other_passwords)); |
||||
| 429 | |||||
| 430 | // Whichever encryption it was using, let's make it use SMF's now ;). |
||||
| 431 | if (in_array($user_settings['passwd'], $other_passwords)) |
||||
| 432 | { |
||||
| 433 | $user_settings['passwd'] = hash_password($user_settings['member_name'], un_htmlspecialchars($_POST['passwrd'])); |
||||
| 434 | $user_settings['password_salt'] = bin2hex($smcFunc['random_bytes'](16)); |
||||
| 435 | |||||
| 436 | // Update the password and set up the hash. |
||||
| 437 | updateMemberData($user_settings['id_member'], array('passwd' => $user_settings['passwd'], 'password_salt' => $user_settings['password_salt'], 'passwd_flood' => '')); |
||||
| 438 | } |
||||
| 439 | // Okay, they for sure didn't enter the password! |
||||
| 440 | else |
||||
| 441 | { |
||||
| 442 | // They've messed up again - keep a count to see if they need a hand. |
||||
| 443 | $_SESSION['failed_login'] = isset($_SESSION['failed_login']) ? ($_SESSION['failed_login'] + 1) : 1; |
||||
| 444 | |||||
| 445 | // Hmm... don't remember it, do you? Here, try the password reminder ;). |
||||
| 446 | if ($_SESSION['failed_login'] >= $modSettings['failed_login_threshold']) |
||||
| 447 | redirectexit('action=reminder'); |
||||
| 448 | // We'll give you another chance... |
||||
| 449 | else |
||||
| 450 | { |
||||
| 451 | // Log an error so we know that it didn't go well in the error log. |
||||
| 452 | log_error($txt['incorrect_password'] . ' - <span class="remove">' . $user_settings['member_name'] . '</span>', 'user'); |
||||
| 453 | |||||
| 454 | $context['login_errors'] = array($txt['incorrect_password']); |
||||
| 455 | return; |
||||
| 456 | } |
||||
| 457 | } |
||||
| 458 | } |
||||
| 459 | elseif (!empty($user_settings['passwd_flood'])) |
||||
| 460 | { |
||||
| 461 | // Let's be sure they weren't a little hacker. |
||||
| 462 | validatePasswordFlood($user_settings['id_member'], $user_settings['member_name'], $user_settings['passwd_flood'], true); |
||||
| 463 | |||||
| 464 | // If we got here then we can reset the flood counter. |
||||
| 465 | updateMemberData($user_settings['id_member'], array('passwd_flood' => '')); |
||||
| 466 | } |
||||
| 467 | |||||
| 468 | // Correct password, but they've got no salt; fix it! |
||||
| 469 | if (strlen($user_settings['password_salt']) < 32) |
||||
| 470 | { |
||||
| 471 | $user_settings['password_salt'] = bin2hex($smcFunc['random_bytes'](16)); |
||||
| 472 | updateMemberData($user_settings['id_member'], array('password_salt' => $user_settings['password_salt'])); |
||||
| 473 | } |
||||
| 474 | |||||
| 475 | // Check their activation status. |
||||
| 476 | if (!checkActivation()) |
||||
| 477 | return; |
||||
| 478 | |||||
| 479 | DoLogin(); |
||||
| 480 | } |
||||
| 481 | |||||
| 482 | /** |
||||
| 483 | * Allows the user to enter their Two-Factor Authentication code |
||||
| 484 | */ |
||||
| 485 | function LoginTFA() |
||||
| 486 | { |
||||
| 487 | global $sourcedir, $txt, $context, $user_info, $modSettings, $scripturl; |
||||
| 488 | |||||
| 489 | if (!$user_info['is_guest'] || empty($context['tfa_member']) || empty($modSettings['tfa_mode'])) |
||||
| 490 | fatal_lang_error('no_access', false); |
||||
| 491 | |||||
| 492 | loadLanguage('Profile'); |
||||
| 493 | require_once($sourcedir . '/Class-TOTP.php'); |
||||
| 494 | |||||
| 495 | $member = $context['tfa_member']; |
||||
| 496 | |||||
| 497 | // Prevent replay attacks by limiting at least 2 minutes before they can log in again via 2FA |
||||
| 498 | if (time() - $member['last_login'] < 120) |
||||
| 499 | fatal_lang_error('tfa_wait', false); |
||||
| 500 | |||||
| 501 | $totp = new \TOTP\Auth($member['tfa_secret']); |
||||
| 502 | $totp->setRange(1); |
||||
| 503 | |||||
| 504 | /* This is true when: |
||||
| 505 | * We have a valid header indicating a JQXHR request. This is not sent during a cross domain request. |
||||
| 506 | * OR we have found: |
||||
| 507 | * 1. valid cors host |
||||
| 508 | * 2. A header indicating a SMF request |
||||
| 509 | * 3. The url has a ajax in either the GET or POST |
||||
| 510 | * These are not intended for security, but ensuring the request is intended for a JQXHR response. |
||||
| 511 | */ |
||||
| 512 | if ( |
||||
| 513 | ( |
||||
| 514 | !empty($_SERVER['HTTP_X_REQUESTED_WITH']) |
||||
| 515 | && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' |
||||
| 516 | ) |
||||
| 517 | || |
||||
| 518 | ( |
||||
| 519 | !empty($context['valid_cors_found']) |
||||
| 520 | && !empty($_SERVER['HTTP_X_SMF_AJAX']) |
||||
| 521 | && isset($_REQUEST['ajax']) |
||||
| 522 | ) |
||||
| 523 | ) |
||||
| 524 | { |
||||
| 525 | $context['from_ajax'] = true; |
||||
| 526 | $context['template_layers'] = array(); |
||||
| 527 | } |
||||
| 528 | |||||
| 529 | if (!empty($_POST['tfa_code']) && empty($_POST['tfa_backup'])) |
||||
| 530 | { |
||||
| 531 | // Check to ensure we're forcing SSL for authentication |
||||
| 532 | if (!empty($modSettings['force_ssl']) && empty($maintenance) && !httpsOn()) |
||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||||
| 533 | fatal_lang_error('login_ssl_required', false); |
||||
| 534 | |||||
| 535 | $code = $_POST['tfa_code']; |
||||
| 536 | |||||
| 537 | if (strlen($code) == $totp->getCodeLength() && $totp->validateCode($code)) |
||||
| 538 | { |
||||
| 539 | updateMemberData($member['id_member'], array('last_login' => time())); |
||||
| 540 | |||||
| 541 | setTFACookie(3153600, $member['id_member'], hash_salt($member['tfa_backup'], $member['password_salt'])); |
||||
| 542 | redirectexit(); |
||||
| 543 | } |
||||
| 544 | else |
||||
| 545 | { |
||||
| 546 | validatePasswordFlood($member['id_member'], $member['member_name'], $member['passwd_flood'], false, true); |
||||
| 547 | |||||
| 548 | $context['tfa_error'] = true; |
||||
| 549 | $context['tfa_value'] = $_POST['tfa_code']; |
||||
| 550 | } |
||||
| 551 | } |
||||
| 552 | elseif (!empty($_POST['tfa_backup'])) |
||||
| 553 | { |
||||
| 554 | // Check to ensure we're forcing SSL for authentication |
||||
| 555 | if (!empty($modSettings['force_ssl']) && empty($maintenance) && !httpsOn()) |
||||
| 556 | fatal_lang_error('login_ssl_required', false); |
||||
| 557 | |||||
| 558 | $backup = $_POST['tfa_backup']; |
||||
| 559 | |||||
| 560 | if (hash_verify_password($member['member_name'], $backup, $member['tfa_backup'])) |
||||
| 561 | { |
||||
| 562 | // Get rid of their current TFA settings |
||||
| 563 | updateMemberData($member['id_member'], array( |
||||
| 564 | 'tfa_secret' => '', |
||||
| 565 | 'tfa_backup' => '', |
||||
| 566 | 'last_login' => time(), |
||||
| 567 | )); |
||||
| 568 | setTFACookie(3153600, $member['id_member'], hash_salt($member['tfa_backup'], $member['password_salt'])); |
||||
| 569 | redirectexit('action=profile;area=tfasetup;backup'); |
||||
| 570 | } |
||||
| 571 | else |
||||
| 572 | { |
||||
| 573 | validatePasswordFlood($member['id_member'], $member['member_name'], $member['passwd_flood'], false, true); |
||||
| 574 | |||||
| 575 | $context['tfa_backup_error'] = true; |
||||
| 576 | $context['tfa_value'] = $_POST['tfa_code']; |
||||
| 577 | $context['tfa_backup_value'] = $_POST['tfa_backup']; |
||||
| 578 | } |
||||
| 579 | } |
||||
| 580 | |||||
| 581 | loadTemplate('Login'); |
||||
| 582 | $context['sub_template'] = 'login_tfa'; |
||||
| 583 | $context['page_title'] = $txt['login']; |
||||
| 584 | $context['tfa_url'] = $scripturl . '?action=logintfa'; |
||||
| 585 | } |
||||
| 586 | |||||
| 587 | /** |
||||
| 588 | * Check activation status of the current user. |
||||
| 589 | */ |
||||
| 590 | function checkActivation() |
||||
| 591 | { |
||||
| 592 | global $context, $txt, $scripturl, $user_settings, $modSettings; |
||||
| 593 | |||||
| 594 | if (!isset($context['login_errors'])) |
||||
| 595 | $context['login_errors'] = array(); |
||||
| 596 | |||||
| 597 | // What is the true activation status of this account? |
||||
| 598 | $activation_status = $user_settings['is_activated'] > 10 ? $user_settings['is_activated'] - 10 : $user_settings['is_activated']; |
||||
| 599 | |||||
| 600 | // Check if the account is activated - COPPA first... |
||||
| 601 | if ($activation_status == 5) |
||||
| 602 | { |
||||
| 603 | $context['login_errors'][] = $txt['coppa_no_consent'] . ' <a href="' . $scripturl . '?action=coppa;member=' . $user_settings['id_member'] . '">' . $txt['coppa_need_more_details'] . '</a>'; |
||||
| 604 | return false; |
||||
| 605 | } |
||||
| 606 | // Awaiting approval still? |
||||
| 607 | elseif ($activation_status == 3) |
||||
| 608 | fatal_lang_error('still_awaiting_approval', 'user'); |
||||
| 609 | // Awaiting deletion, changed their mind? |
||||
| 610 | elseif ($activation_status == 4) |
||||
| 611 | { |
||||
| 612 | if (isset($_REQUEST['undelete'])) |
||||
| 613 | { |
||||
| 614 | updateMemberData($user_settings['id_member'], array('is_activated' => 1)); |
||||
| 615 | updateSettings(array('unapprovedMembers' => ($modSettings['unapprovedMembers'] > 0 ? $modSettings['unapprovedMembers'] - 1 : 0))); |
||||
| 616 | } |
||||
| 617 | else |
||||
| 618 | { |
||||
| 619 | $context['disable_login_hashing'] = true; |
||||
| 620 | $context['login_errors'][] = $txt['awaiting_delete_account']; |
||||
| 621 | $context['login_show_undelete'] = true; |
||||
| 622 | return false; |
||||
| 623 | } |
||||
| 624 | } |
||||
| 625 | // Standard activation? |
||||
| 626 | elseif ($activation_status != 1) |
||||
| 627 | { |
||||
| 628 | log_error($txt['activate_not_completed1'] . ' - <span class="remove">' . $user_settings['member_name'] . '</span>', 'user'); |
||||
| 629 | |||||
| 630 | $context['login_errors'][] = $txt['activate_not_completed1'] . ' <a href="' . $scripturl . '?action=activate;sa=resend;u=' . $user_settings['id_member'] . '">' . $txt['activate_not_completed2'] . '</a>'; |
||||
| 631 | return false; |
||||
| 632 | } |
||||
| 633 | return true; |
||||
| 634 | } |
||||
| 635 | |||||
| 636 | /** |
||||
| 637 | * Perform the logging in. (set cookie, call hooks, etc) |
||||
| 638 | */ |
||||
| 639 | function DoLogin() |
||||
| 640 | { |
||||
| 641 | global $user_info, $user_settings, $smcFunc; |
||||
| 642 | global $maintenance, $modSettings, $context, $sourcedir; |
||||
| 643 | |||||
| 644 | // Load cookie authentication stuff. |
||||
| 645 | require_once($sourcedir . '/Subs-Auth.php'); |
||||
| 646 | |||||
| 647 | // Call login integration functions. |
||||
| 648 | call_integration_hook('integrate_login', array($user_settings['member_name'], null, $modSettings['cookieTime'])); |
||||
| 649 | |||||
| 650 | // Get ready to set the cookie... |
||||
| 651 | $user_info['id'] = $user_settings['id_member']; |
||||
| 652 | |||||
| 653 | // Bam! Cookie set. A session too, just in case. |
||||
| 654 | setLoginCookie(60 * $modSettings['cookieTime'], $user_settings['id_member'], hash_salt($user_settings['passwd'], $user_settings['password_salt'])); |
||||
| 655 | |||||
| 656 | // Reset the login threshold. |
||||
| 657 | if (isset($_SESSION['failed_login'])) |
||||
| 658 | unset($_SESSION['failed_login']); |
||||
| 659 | |||||
| 660 | $user_info['is_guest'] = false; |
||||
| 661 | $user_settings['additional_groups'] = explode(',', $user_settings['additional_groups']); |
||||
| 662 | $user_info['is_admin'] = $user_settings['id_group'] == 1 || in_array(1, $user_settings['additional_groups']); |
||||
| 663 | |||||
| 664 | // Are you banned? |
||||
| 665 | is_not_banned(true); |
||||
| 666 | |||||
| 667 | // Don't stick the language or theme after this point. |
||||
| 668 | unset($_SESSION['language'], $_SESSION['id_theme']); |
||||
| 669 | |||||
| 670 | // First login? |
||||
| 671 | $request = $smcFunc['db_query']('', ' |
||||
| 672 | SELECT last_login |
||||
| 673 | FROM {db_prefix}members |
||||
| 674 | WHERE id_member = {int:id_member} |
||||
| 675 | AND last_login = 0', |
||||
| 676 | array( |
||||
| 677 | 'id_member' => $user_info['id'], |
||||
| 678 | ) |
||||
| 679 | ); |
||||
| 680 | if ($smcFunc['db_num_rows']($request) == 1) |
||||
| 681 | $_SESSION['first_login'] = true; |
||||
| 682 | else |
||||
| 683 | unset($_SESSION['first_login']); |
||||
| 684 | $smcFunc['db_free_result']($request); |
||||
| 685 | |||||
| 686 | // You've logged in, haven't you? |
||||
| 687 | $update = array('member_ip' => $user_info['ip'], 'member_ip2' => $_SERVER['BAN_CHECK_IP']); |
||||
| 688 | if (empty($user_settings['tfa_secret'])) |
||||
| 689 | $update['last_login'] = time(); |
||||
| 690 | updateMemberData($user_info['id'], $update); |
||||
| 691 | |||||
| 692 | // Get rid of the online entry for that old guest.... |
||||
| 693 | $smcFunc['db_query']('', ' |
||||
| 694 | DELETE FROM {db_prefix}log_online |
||||
| 695 | WHERE session = {string:session}', |
||||
| 696 | array( |
||||
| 697 | 'session' => 'ip' . $user_info['ip'], |
||||
| 698 | ) |
||||
| 699 | ); |
||||
| 700 | $_SESSION['log_time'] = 0; |
||||
| 701 | |||||
| 702 | // Log this entry, only if we have it enabled. |
||||
| 703 | if (!empty($modSettings['loginHistoryDays'])) |
||||
| 704 | $smcFunc['db_insert']('insert', |
||||
| 705 | '{db_prefix}member_logins', |
||||
| 706 | array( |
||||
| 707 | 'id_member' => 'int', 'time' => 'int', 'ip' => 'inet', 'ip2' => 'inet', |
||||
| 708 | ), |
||||
| 709 | array( |
||||
| 710 | $user_info['id'], time(), $user_info['ip'], $user_info['ip2'] |
||||
| 711 | ), |
||||
| 712 | array( |
||||
| 713 | 'id_member', 'time' |
||||
| 714 | ) |
||||
| 715 | ); |
||||
| 716 | |||||
| 717 | // Just log you back out if it's in maintenance mode and you AREN'T an admin. |
||||
| 718 | if (empty($maintenance) || allowedTo('admin_forum')) |
||||
| 719 | redirectexit('action=login2;sa=check;member=' . $user_info['id'], $context['server']['needs_login_fix']); |
||||
| 720 | else |
||||
| 721 | redirectexit('action=logout;' . $context['session_var'] . '=' . $context['session_id'], $context['server']['needs_login_fix']); |
||||
| 722 | } |
||||
| 723 | |||||
| 724 | /** |
||||
| 725 | * Logs the current user out of their account. |
||||
| 726 | * It requires that the session hash is sent as well, to prevent automatic logouts by images or javascript. |
||||
| 727 | * It redirects back to $_SESSION['logout_url'], if it exists. |
||||
| 728 | * It is accessed via ?action=logout;session_var=... |
||||
| 729 | * |
||||
| 730 | * @param bool $internal If true, it doesn't check the session |
||||
| 731 | * @param bool $redirect Whether or not to redirect the user after they log out |
||||
| 732 | */ |
||||
| 733 | function Logout($internal = false, $redirect = true) |
||||
| 734 | { |
||||
| 735 | global $sourcedir, $user_info, $user_settings, $context, $smcFunc, $cookiename, $modSettings; |
||||
| 736 | |||||
| 737 | // They decided to cancel a logout? |
||||
| 738 | if (!$internal && isset($_POST['cancel']) && isset($_GET[$context['session_var']])) |
||||
| 739 | redirectexit(!empty($_SESSION['logout_return']) ? $_SESSION['logout_return'] : ''); |
||||
| 740 | // Prompt to logout? |
||||
| 741 | elseif (!$internal && !isset($_GET[$context['session_var']])) |
||||
| 742 | { |
||||
| 743 | loadLanguage('Login'); |
||||
| 744 | loadTemplate('Login'); |
||||
| 745 | $context['sub_template'] = 'logout'; |
||||
| 746 | |||||
| 747 | // This came from a valid hashed return url. Or something that knows our secrets... |
||||
| 748 | if (!empty($_REQUEST['return_hash']) && !empty($_REQUEST['return_to']) && hash_hmac('sha1', un_htmlspecialchars($_REQUEST['return_to']), get_auth_secret()) == $_REQUEST['return_hash']) |
||||
| 749 | { |
||||
| 750 | $_SESSION['logout_url'] = un_htmlspecialchars($_REQUEST['return_to']); |
||||
| 751 | $_SESSION['logout_return'] = $_SESSION['logout_url']; |
||||
| 752 | } |
||||
| 753 | // Setup the return address. |
||||
| 754 | elseif (isset($_SESSION['old_url'])) |
||||
| 755 | $_SESSION['logout_return'] = $_SESSION['old_url']; |
||||
| 756 | |||||
| 757 | // Don't go any further. |
||||
| 758 | return; |
||||
| 759 | } |
||||
| 760 | // Make sure they aren't being auto-logged out. |
||||
| 761 | elseif (!$internal && isset($_GET[$context['session_var']])) |
||||
| 762 | checkSession('get'); |
||||
| 763 | |||||
| 764 | require_once($sourcedir . '/Subs-Auth.php'); |
||||
| 765 | |||||
| 766 | if (isset($_SESSION['pack_ftp'])) |
||||
| 767 | $_SESSION['pack_ftp'] = null; |
||||
| 768 | |||||
| 769 | // It won't be first login anymore. |
||||
| 770 | unset($_SESSION['first_login']); |
||||
| 771 | |||||
| 772 | // Just ensure they aren't a guest! |
||||
| 773 | if (!$user_info['is_guest']) |
||||
| 774 | { |
||||
| 775 | // Pass the logout information to integrations. |
||||
| 776 | call_integration_hook('integrate_logout', array($user_settings['member_name'])); |
||||
| 777 | |||||
| 778 | // If you log out, you aren't online anymore :P. |
||||
| 779 | $smcFunc['db_query']('', ' |
||||
| 780 | DELETE FROM {db_prefix}log_online |
||||
| 781 | WHERE id_member = {int:current_member}', |
||||
| 782 | array( |
||||
| 783 | 'current_member' => $user_info['id'], |
||||
| 784 | ) |
||||
| 785 | ); |
||||
| 786 | } |
||||
| 787 | |||||
| 788 | $_SESSION['log_time'] = 0; |
||||
| 789 | |||||
| 790 | // Empty the cookie! (set it in the past, and for id_member = 0) |
||||
| 791 | setLoginCookie(-3600, 0); |
||||
| 792 | |||||
| 793 | // And some other housekeeping while we're at it. |
||||
| 794 | $salt = bin2hex($smcFunc['random_bytes'](16)); |
||||
| 795 | if (!empty($user_info['id'])) |
||||
| 796 | updateMemberData($user_info['id'], array('password_salt' => $salt)); |
||||
| 797 | |||||
| 798 | if (!empty($modSettings['tfa_mode']) && !empty($user_info['id']) && !empty($_COOKIE[$cookiename . '_tfa'])) |
||||
| 799 | { |
||||
| 800 | list (,, $exp) = $smcFunc['json_decode']($_COOKIE[$cookiename . '_tfa'], true); |
||||
| 801 | setTFACookie((int) $exp - time(), $salt, hash_salt($user_settings['tfa_backup'], $salt)); |
||||
|
0 ignored issues
–
show
$salt of type string is incompatible with the type integer expected by parameter $id of setTFACookie().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 802 | } |
||||
| 803 | |||||
| 804 | session_destroy(); |
||||
| 805 | |||||
| 806 | // Off to the merry board index we go! |
||||
| 807 | if ($redirect) |
||||
| 808 | { |
||||
| 809 | if (empty($_SESSION['logout_url'])) |
||||
| 810 | redirectexit('', $context['server']['needs_login_fix']); |
||||
| 811 | elseif (!empty($_SESSION['logout_url']) && (strpos($_SESSION['logout_url'], 'http://') === false && strpos($_SESSION['logout_url'], 'https://') === false)) |
||||
| 812 | { |
||||
| 813 | unset ($_SESSION['logout_url']); |
||||
| 814 | redirectexit(); |
||||
| 815 | } |
||||
| 816 | else |
||||
| 817 | { |
||||
| 818 | $temp = $_SESSION['logout_url']; |
||||
| 819 | unset($_SESSION['logout_url']); |
||||
| 820 | |||||
| 821 | redirectexit($temp, $context['server']['needs_login_fix']); |
||||
| 822 | } |
||||
| 823 | } |
||||
| 824 | } |
||||
| 825 | |||||
| 826 | /** |
||||
| 827 | * MD5 Encryption used for older passwords. (SMF 1.0.x/YaBB SE 1.5.x hashing) |
||||
| 828 | * |
||||
| 829 | * @param string $data The data |
||||
| 830 | * @param string $key The key |
||||
| 831 | * @return string The HMAC MD5 of data with key |
||||
| 832 | */ |
||||
| 833 | function md5_hmac($data, $key) |
||||
| 834 | { |
||||
| 835 | $key = str_pad(strlen($key) <= 64 ? $key : pack('H*', md5($key)), 64, chr(0x00)); |
||||
| 836 | return md5(($key ^ str_repeat(chr(0x5c), 64)) . pack('H*', md5(($key ^ str_repeat(chr(0x36), 64)) . $data))); |
||||
| 837 | } |
||||
| 838 | |||||
| 839 | /** |
||||
| 840 | * Custom encryption for phpBB3 based passwords. |
||||
| 841 | * |
||||
| 842 | * @param string $passwd The raw (unhashed) password |
||||
| 843 | * @param string $passwd_hash The hashed password |
||||
| 844 | * @return string The hashed version of $passwd |
||||
| 845 | */ |
||||
| 846 | function phpBB3_password_check($passwd, $passwd_hash) |
||||
| 847 | { |
||||
| 848 | // Too long or too short? |
||||
| 849 | if (strlen($passwd_hash) != 34) |
||||
| 850 | return; |
||||
| 851 | |||||
| 852 | // Range of characters allowed. |
||||
| 853 | $range = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; |
||||
| 854 | |||||
| 855 | // Tests |
||||
| 856 | $strpos = strpos($range, $passwd_hash[3]); |
||||
| 857 | $count = 1 << $strpos; |
||||
| 858 | $salt = substr($passwd_hash, 4, 8); |
||||
| 859 | |||||
| 860 | $hash = md5($salt . $passwd, true); |
||||
| 861 | for (; $count != 0; --$count) |
||||
| 862 | $hash = md5($hash . $passwd, true); |
||||
| 863 | |||||
| 864 | $output = substr($passwd_hash, 0, 12); |
||||
| 865 | $i = 0; |
||||
| 866 | while ($i < 16) |
||||
| 867 | { |
||||
| 868 | $value = ord($hash[$i++]); |
||||
| 869 | $output .= $range[$value & 0x3f]; |
||||
| 870 | |||||
| 871 | if ($i < 16) |
||||
| 872 | $value |= ord($hash[$i]) << 8; |
||||
| 873 | |||||
| 874 | $output .= $range[($value >> 6) & 0x3f]; |
||||
| 875 | |||||
| 876 | if ($i++ >= 16) |
||||
| 877 | break; |
||||
| 878 | |||||
| 879 | if ($i < 16) |
||||
| 880 | $value |= ord($hash[$i]) << 16; |
||||
| 881 | |||||
| 882 | $output .= $range[($value >> 12) & 0x3f]; |
||||
| 883 | |||||
| 884 | if ($i++ >= 16) |
||||
| 885 | break; |
||||
| 886 | |||||
| 887 | $output .= $range[($value >> 18) & 0x3f]; |
||||
| 888 | } |
||||
| 889 | |||||
| 890 | // Return now. |
||||
| 891 | return $output; |
||||
| 892 | } |
||||
| 893 | |||||
| 894 | /** |
||||
| 895 | * This protects against brute force attacks on a member's password. |
||||
| 896 | * Importantly, even if the password was right we DON'T TELL THEM! |
||||
| 897 | * |
||||
| 898 | * @param int $id_member The ID of the member |
||||
| 899 | * @param string $member_name The name of the member. |
||||
| 900 | * @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 | |
||||
| 901 | * @param bool $was_correct Whether or not the password was correct |
||||
| 902 | * @param bool $tfa Whether we're validating for two-factor authentication |
||||
| 903 | */ |
||||
| 904 | function validatePasswordFlood($id_member, $member_name, $password_flood_value = false, $was_correct = false, $tfa = false) |
||||
| 905 | { |
||||
| 906 | global $cookiename, $sourcedir; |
||||
| 907 | |||||
| 908 | // As this is only brute protection, we allow 5 attempts every 10 seconds. |
||||
| 909 | |||||
| 910 | // Destroy any session or cookie data about this member, as they validated wrong. |
||||
| 911 | // Only if they're not validating for 2FA |
||||
| 912 | if (!$tfa) |
||||
| 913 | { |
||||
| 914 | require_once($sourcedir . '/Subs-Auth.php'); |
||||
| 915 | setLoginCookie(-3600, 0); |
||||
| 916 | |||||
| 917 | if (isset($_SESSION['login_' . $cookiename])) |
||||
| 918 | unset($_SESSION['login_' . $cookiename]); |
||||
| 919 | } |
||||
| 920 | |||||
| 921 | // We need a member! |
||||
| 922 | if (!$id_member) |
||||
| 923 | { |
||||
| 924 | // Redirect back! |
||||
| 925 | redirectexit(); |
||||
| 926 | |||||
| 927 | // Probably not needed, but still make sure... |
||||
| 928 | fatal_lang_error('no_access', false); |
||||
| 929 | } |
||||
| 930 | |||||
| 931 | // Right, have we got a flood value? |
||||
| 932 | if ($password_flood_value !== false) |
||||
| 933 | @list ($time_stamp, $number_tries) = explode('|', $password_flood_value); |
||||
|
0 ignored issues
–
show
It seems like
$password_flood_value can also be of type true; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 934 | |||||
| 935 | // Timestamp or number of tries invalid? |
||||
| 936 | if (empty($number_tries) || empty($time_stamp)) |
||||
| 937 | { |
||||
| 938 | $number_tries = 0; |
||||
| 939 | $time_stamp = time(); |
||||
| 940 | } |
||||
| 941 | |||||
| 942 | // They've failed logging in already |
||||
| 943 | if (!empty($number_tries)) |
||||
| 944 | { |
||||
| 945 | // Give them less chances if they failed before |
||||
| 946 | $number_tries = $time_stamp < time() - 20 ? 2 : $number_tries; |
||||
| 947 | |||||
| 948 | // They are trying too fast, make them wait longer |
||||
| 949 | if ($time_stamp < time() - 10) |
||||
| 950 | $time_stamp = time(); |
||||
| 951 | } |
||||
| 952 | |||||
| 953 | $number_tries++; |
||||
| 954 | |||||
| 955 | // Broken the law? |
||||
| 956 | if ($number_tries > 5) |
||||
| 957 | fatal_lang_error('login_threshold_brute_fail', 'login', [$member_name]); |
||||
| 958 | |||||
| 959 | // Otherwise set the members data. If they correct on their first attempt then we actually clear it, otherwise we set it! |
||||
| 960 | updateMemberData($id_member, array('passwd_flood' => $was_correct && $number_tries == 1 ? '' : $time_stamp . '|' . $number_tries)); |
||||
| 961 | |||||
| 962 | } |
||||
| 963 | |||||
| 964 | ?> |