diff --git a/functions.inc.php b/functions.inc.php index c86830fd..470f3db5 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -35,12 +35,12 @@ function authentication_mfa_incomplete(): bool } /** - * check_session - * Action: Check if a session already exists, if not redirect to login.php - * Call: check_session () + * Action: Check if a session already exists, if not redirect to login.php. Returns current user's login. + * + * @param bool $requireMfaComplete - false only for login-mfa.php * @return string username (e.g. foo@example.com) or CLI or SETUP.PHP */ -function authentication_get_username() +function authentication_get_username(bool $requireMfaComplete = true) { if (defined('POSTFIXADMIN_CLI')) { return 'CLI'; @@ -54,6 +54,16 @@ function authentication_get_username() header("Location: login.php"); exit(0); } + + /** + * when an mfa user logs in (username+password) but haven't yet done the MFA code, $_SESSION['sessid']['username'] will be populated + * but the user hasn't finished authentication, so we should not treat them as having logged in. + */ + if ($requireMfaComplete && authentication_mfa_incomplete()) { + header("Location: login-mfa.php"); + exit(0); + } + return $_SESSION['sessid']['username']; } diff --git a/public/login-mfa.php b/public/login-mfa.php index 9e06b018..81b1287b 100644 --- a/public/login-mfa.php +++ b/public/login-mfa.php @@ -66,18 +66,18 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") { $fTotp = safepost('fTOTP_code'); $h = new AdminHandler(); - if (authentication_mfa_incomplete() && $totppf->checkUserTOTP(authentication_get_username(), $fTotp)) { - init_session(authentication_get_username(), true, true); + if (authentication_mfa_incomplete() && $totppf->checkUserTOTP(authentication_get_username(false), $fTotp)) { + init_session(authentication_get_username(false), true, true); // get superadmin status and store in session - $h->init(authentication_get_username()); + $h->init(authentication_get_username(false)); if ($h->result()['superadmin'] == 1) { $_SESSION['sessid']['roles'][] = 'global-admin'; } header("Location: main.php"); exit(0); } else { - error_log("PostfixAdmin admin second factor login failed (username: " . authentication_get_username() . ", ip_address: {$_SERVER['REMOTE_ADDR']})"); + error_log("PostfixAdmin admin second factor login failed (username: " . authentication_get_username(false) . ", ip_address: {$_SERVER['REMOTE_ADDR']})"); $error = $PALANG['pTotp_failed']; flash_error($PALANG['pTotp_failed']); } diff --git a/public/users/login-mfa.php b/public/users/login-mfa.php index 9524bdc6..4bc7bc8c 100644 --- a/public/users/login-mfa.php +++ b/public/users/login-mfa.php @@ -60,12 +60,12 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") { $totppf = new TotpPf('mailbox', new Login('mailbox')); $fTotp = safepost('fTOTP_code'); - if (authentication_mfa_incomplete() && $totppf->checkUserTOTP(authentication_get_username(), $fTotp)) { - init_session(authentication_get_username(), false, true); + if (authentication_mfa_incomplete() && $totppf->checkUserTOTP(authentication_get_username(false), $fTotp)) { + init_session(authentication_get_username(false), false, true); header("Location: main.php"); exit(0); } else { # $h->login failed - error_log("PostfixAdmin admin second factor login failed (username: " . authentication_get_username() . ", ip_address: {$_SERVER['REMOTE_ADDR']})"); + error_log("PostfixAdmin admin second factor login failed (username: " . authentication_get_username(false) . ", ip_address: {$_SERVER['REMOTE_ADDR']})"); flash_error($PALANG['pTotp_failed']); } }