diff --git a/config.inc.php b/config.inc.php index ce99aaf5..6ca3cf2e 100644 --- a/config.inc.php +++ b/config.inc.php @@ -790,6 +790,8 @@ $CONF['theme_custom_css'] = ''; // XMLRPC Interface. // This should be only of use if you wish to use e.g the // Postfixadmin-Squirrelmail package +// XML-RPC has no TOTP challenge flow. Mailbox accounts with a TOTP +// secret cannot authenticate through this interface. // change to boolean true to enable xmlrpc $CONF['xmlrpc_enabled'] = false; diff --git a/model/TotpPf.php b/model/TotpPf.php index 278bd9f1..498b48cf 100644 --- a/model/TotpPf.php +++ b/model/TotpPf.php @@ -133,6 +133,18 @@ class TotpPf return (is_array($result) && isset($result['totp_secret']) && !empty($result['totp_secret'])); } + /** + * Authenticate an interface that cannot complete a TOTP challenge. + */ + public function passwordOnlyLogin(string $username, string $password): bool + { + if (!$this->login->login($username, $password)) { + return false; + } + + return !$this->usesTOTP($username); + } + /** * Check if a TOTP code is valid for a user */ diff --git a/public/xmlrpc.php b/public/xmlrpc.php index dd1d5647..f944c1b0 100644 --- a/public/xmlrpc.php +++ b/public/xmlrpc.php @@ -48,7 +48,8 @@ $server = new Zend_XmlRpc_Server(); function login($username, $password) { $login = new Login('mailbox'); - if ($login->login($username, $password)) { + $totp = new TotpPf('mailbox', $login); + if ($totp->passwordOnlyLogin($username, $password)) { session_regenerate_id(); $_SESSION['authenticated'] = true; $_SESSION['sessid'] = array(); diff --git a/tests/TotpPfTest.php b/tests/TotpPfTest.php index 0f639e6c..9b3ef3b1 100644 --- a/tests/TotpPfTest.php +++ b/tests/TotpPfTest.php @@ -76,6 +76,24 @@ class TotpPfTest extends TestCase $this->assertNotEmpty($currentCode); } + public function testPasswordOnlyLoginRejectsAccountsUsingTotp(): void + { + global $CONF; + Config::write('totp', 'YES'); + $CONF['totp'] = 'YES'; + + $totp = new TotpPf('mailbox', new Login('mailbox')); + $this->assertTrue($totp->passwordOnlyLogin('test@example.com', 'foobar')); + + db_execute( + 'UPDATE mailbox SET totp_secret = :secret WHERE username = :username', + ['username' => 'test@example.com', 'secret' => 'existing-secret'] + ); + + $this->assertFalse($totp->passwordOnlyLogin('test@example.com', 'foobar')); + $this->assertFalse($totp->passwordOnlyLogin('test@example.com', 'incorrect-password')); + } + public function testDbOperations() { $x = new TotpPf('mailbox', new Login('mailbox'));