From 23fc7b3fe628252d1969df7bd72ab125a7792e28 Mon Sep 17 00:00:00 2001
From: Daniel Winzen ';
echo '';
}
diff --git a/composer.json b/composer.json
index dfb7451..db1dfb1 100644
--- a/composer.json
+++ b/composer.json
@@ -5,6 +5,8 @@
"ext-pdo": "*",
"ext-gnupg": "*",
"ext-gettext": "*",
- "ext-intl": "*"
+ "ext-intl": "*",
+ "endroid/qr-code": "^5.0",
+ "ext-gd": "*"
}
}
diff --git a/setup.php b/setup.php
index c7ea5b4..0241fc6 100644
--- a/setup.php
+++ b/setup.php
@@ -24,7 +24,7 @@ try{
}
}
$createTableStatements = [
- 'admin' => "CREATE TABLE IF NOT EXISTS `admin` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `superadmin` tinyint(1) NOT NULL DEFAULT 0, `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `modified` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '{MD5-CRYPT}', PRIMARY KEY (`username`), KEY `active` (`active`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
+ 'admin' => "CREATE TABLE IF NOT EXISTS `admin` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `superadmin` tinyint(1) NOT NULL DEFAULT 0, `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `modified` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '{MD5-CRYPT}', `totp_secret` varchar(64) DEFAULT NULL, PRIMARY KEY (`username`), KEY `active` (`active`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
'domain' => "CREATE TABLE IF NOT EXISTS `domain` (`domain` varchar(255) NOT NULL, `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `modified` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`domain`), KEY `active` (`active`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
'alias' => "CREATE TABLE IF NOT EXISTS `alias` (`address` varchar(255) NOT NULL, `goto` text NOT NULL, `domain` varchar(255) NOT NULL, `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `modified` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`address`), KEY `domain` (`domain`), KEY `active` (`active`), CONSTRAINT `alias_ibfk_1` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
'alias_domain' => "CREATE TABLE IF NOT EXISTS `alias_domain` (`alias_domain` varchar(255) NOT NULL DEFAULT '', `target_domain` varchar(255) NOT NULL DEFAULT '', `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `modified` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`alias_domain`), KEY `active` (`active`), KEY `target_domain` (`target_domain`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
@@ -36,7 +36,7 @@ $createTableStatements = [
'dmarc_report_record_reason' => "CREATE TABLE IF NOT EXISTS `dmarc_report_record_reason` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `record_id` bigint(20) unsigned NOT NULL, `type` varchar(255) NOT NULL, `comment` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `record_id` (`record_id`), CONSTRAINT `dmarc_report_record_reason_ibfk_1` FOREIGN KEY (`record_id`) REFERENCES `dmarc_report_records` (`id`) ON DELETE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
'dmarc_report_record_spf_result' => "CREATE TABLE IF NOT EXISTS `dmarc_report_record_spf_result` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `record_id` bigint(20) unsigned NOT NULL, `domain` varchar(255) NOT NULL, `result` varchar(255) NOT NULL, `scope` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `record_id` (`record_id`), CONSTRAINT `dmarc_report_record_spf_result_ibfk_2` FOREIGN KEY (`record_id`) REFERENCES `dmarc_report_records` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;",
'domain_admins' => "CREATE TABLE IF NOT EXISTS `domain_admins` (`username` varchar(255) NOT NULL, `domain` varchar(255) NOT NULL, `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, `id` bigint(20) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), KEY `username` (`username`), KEY `active` (`active`), KEY `domain` (`domain`), CONSTRAINT `domain_admins_ibfk_1` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `domain_admins_ibfk_2` FOREIGN KEY (`username`) REFERENCES `admin` (`username`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
- 'mailbox' => "CREATE TABLE IF NOT EXISTS `mailbox` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL DEFAULT '', `quota` bigint(20) NOT NULL DEFAULT 0, `local_part` varchar(255) NOT NULL DEFAULT '', `domain` varchar(255) NOT NULL DEFAULT '', `created` datetime NOT NULL DEFAULT current_timestamp(), `modified` datetime NOT NULL DEFAULT current_timestamp(), `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '', `openpgpkey_wkd` char(32) NOT NULL DEFAULT '', `pgp_key` text DEFAULT NULL, `pgp_verified` tinyint(1) NOT NULL DEFAULT 0, `tfa` tinyint(1) NOT NULL DEFAULT 0, `last_login` bigint(20) unsigned DEFAULT NULL, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), CONSTRAINT `mailbox_ibfk_2` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
+ 'mailbox' => "CREATE TABLE IF NOT EXISTS `mailbox` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL DEFAULT '', `quota` bigint(20) NOT NULL DEFAULT 0, `local_part` varchar(255) NOT NULL DEFAULT '', `domain` varchar(255) NOT NULL DEFAULT '', `created` datetime NOT NULL DEFAULT current_timestamp(), `modified` datetime NOT NULL DEFAULT current_timestamp(), `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '', `openpgpkey_wkd` char(32) NOT NULL DEFAULT '', `pgp_key` text DEFAULT NULL, `pgp_verified` tinyint(1) NOT NULL DEFAULT 0, `tfa` tinyint(1) NOT NULL DEFAULT 0, `totp_secret` varchar(64) DEFAULT NULL, `last_login` bigint(20) unsigned DEFAULT NULL, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), CONSTRAINT `mailbox_ibfk_2` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
'monitored_mailboxes' => "CREATE TABLE IF NOT EXISTS `monitored_mailboxes` (`username` varchar(255) NOT NULL, `last_login` int(11) NOT NULL, PRIMARY KEY (`username`), KEY `lastlogin` (`last_login`), CONSTRAINT `monitored_mailboxes_ibfk_1` FOREIGN KEY (`username`) REFERENCES `mailbox` (`username`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
'tls_report' => "CREATE TABLE IF NOT EXISTS `tls_report` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `report_id` varchar(255) NOT NULL, `organization` varchar(255) NOT NULL, `contact` varchar(255) NOT NULL, `start` datetime NOT NULL, `end` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `report_id_organization` (`report_id`,`organization`), KEY `start_end` (`start`,`end`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
'tls_report_policy' => "CREATE TABLE IF NOT EXISTS `tls_report_policy` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `report_id` bigint(20) unsigned NOT NULL, `policy_type` varchar(255) NOT NULL, `policy_string` text NOT NULL, `policy_domain` varchar(255) NOT NULL, `mx_host` text NOT NULL, `success` int(11) NOT NULL, `failure` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `report_id` (`report_id`), KEY `policy_domain` (`policy_domain`), CONSTRAINT `tls_report_policy_ibfk_1` FOREIGN KEY (`report_id`) REFERENCES `tls_report` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `tls_report_policy_ibfk_3` FOREIGN KEY (`policy_domain`) REFERENCES `domain` (`domain`) ON DELETE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
@@ -49,8 +49,14 @@ try{
$version=$stmt->fetch(PDO::FETCH_NUM)[0];
try {
foreach($createTableStatements as $tableName => $statement){
- if ($tableName !== 'settings') {
- $db->exec($statement);
+ $db->exec($statement);
+ }
+ if ( (int) $version < 3 ) {
+ foreach ( [ 'admin', 'mailbox' ] as $tableName ) {
+ $stmt = $db->query( "SHOW COLUMNS FROM `$tableName` LIKE 'totp_secret';" );
+ if ( ! $stmt->fetch() ) {
+ $db->exec( "ALTER TABLE `$tableName` ADD COLUMN `totp_secret` varchar(64) DEFAULT NULL;" );
+ }
}
}
$db->beginTransaction();
diff --git a/www/admin.php b/www/admin.php
index dec9c34..29629bb 100644
--- a/www/admin.php
+++ b/www/admin.php
@@ -26,7 +26,23 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
if ( $_SESSION[ 'csrf_token' ] !== $_POST[ 'csrf_token' ] ?? '' ) {
die( 'Invalid CSRF token' );
}
- if ( $_POST[ 'action' ] === 'logout' ) {
+ if ( ! empty( $_SESSION[ 'admin_totp_pending' ] ) ) {
+ $stmt = $db->prepare( 'SELECT username, superadmin, totp_secret FROM admin WHERE username = ? AND active = 1;' );
+ $stmt->execute( [ $_SESSION[ 'admin_totp_pending' ] ] );
+ $pending = $stmt->fetch( PDO::FETCH_ASSOC );
+ if ( $pending && ! empty( $pending[ 'totp_secret' ] ) && verify_totp_code( $pending[ 'totp_secret' ], $_POST[ 'totp_code' ] ?? '' ) ) {
+ unset( $_SESSION[ 'admin_totp_pending' ] );
+ session_regenerate_id( true );
+ $_SESSION[ 'csrf_token' ] = sha1( uniqid() );
+ $_SESSION[ 'email_admin_user' ] = $pending[ 'username' ];
+ $_SESSION[ 'email_admin_superadmin' ] = (bool) $pending[ 'superadmin' ];
+ } elseif ( ! $pending || empty( $pending[ 'totp_secret' ] ) ) {
+ unset( $_SESSION[ 'admin_totp_pending' ] );
+ $msg .= '
+ + + + + + '.htmlspecialchars(_('Oops, this admin doesn\'t seem to exist.')) . ''; diff --git a/www/manage_account.php b/www/manage_account.php index 2278d7a..60a7b6a 100644 --- a/www/manage_account.php +++ b/www/manage_account.php @@ -22,7 +22,25 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) { if ( $_SESSION[ 'csrf_token' ] !== $_POST[ 'csrf_token' ] ?? '' ) { die( 'Invalid CSRF token' ); } - if ( isset( $_SESSION[ '2fa_code' ] ) ) { + if ( isset( $_SESSION[ 'user_totp_pending' ] ) ) { + $db = get_db_instance(); + $stmt = $db->prepare( 'SELECT totp_secret FROM mailbox WHERE username = ? AND active = 1;' ); + $stmt->execute( [ $_SESSION[ 'user_totp_pending' ] ] ); + $pending = $stmt->fetch( PDO::FETCH_ASSOC ); + if ( ! $pending || empty( $pending[ 'totp_secret' ] ) ) { + unset( $_SESSION[ 'user_totp_pending' ] ); + $msg .= '
'.htmlspecialchars(_('Wrong 2FA code')).'
'; } } - if ( ! isset( $_SESSION[ '2fa_code' ] ) && isset( $_POST[ 'action' ] ) ) { + if ( ! isset( $_SESSION[ '2fa_code' ] ) && ! isset( $_SESSION[ 'user_totp_pending' ] ) && isset( $_POST[ 'action' ] ) ) { if ( $_POST[ 'action' ] === 'logout' ) { $_SESSION = []; session_regenerate_id( true ); @@ -55,26 +73,33 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) { if ( $tmp = $stmt->fetch( PDO::FETCH_ASSOC ) ) { $domain = $tmp[ 'target_domain' ]; } - $stmt = $db->prepare( 'SELECT username, password, password_hash_type, tfa, pgp_key FROM mailbox WHERE username = ? AND active = 1;' ); + $stmt = $db->prepare( 'SELECT username, password, password_hash_type, tfa, pgp_key, totp_secret FROM mailbox WHERE username = ? AND active = 1;' ); $stmt->execute( [ "$user@$domain" ] ); if ( $tmp = $stmt->fetch( PDO::FETCH_ASSOC ) ) { if ( empty( $_POST[ 'pwd' ] ) || ! password_verify( $_POST[ 'pwd' ], $tmp[ 'password' ] ) ) { $ok = false; $msg .= ''.htmlspecialchars(_('Successfully enabled 2FA')).'
'; } + } elseif ( $_POST[ 'action' ] === 'start_totp_setup' && ! empty( $_SESSION[ 'email_user' ] ) ) { + $_SESSION[ 'user_totp_setup' ] = generate_totp_secret(); + } elseif ( $_POST[ 'action' ] === 'enable_totp' && ! empty( $_SESSION[ 'email_user' ] ) && ! empty( $_SESSION[ 'user_totp_setup' ] ) ) { + if ( verify_totp_code( $_SESSION[ 'user_totp_setup' ], $_POST[ 'totp_code' ] ?? '' ) ) { + $stmt = $db->prepare( 'UPDATE mailbox SET totp_secret = ? WHERE username = ? AND active = 1;' ); + $stmt->execute( [ $_SESSION[ 'user_totp_setup' ], $_SESSION[ 'email_user' ] ] ); + unset( $_SESSION[ 'user_totp_setup' ] ); + $msg .= ''.htmlspecialchars(_('Successfully enabled TOTP two-factor authentication')).'
'; + } else { + $msg .= ''.htmlspecialchars(_('Invalid TOTP code')).'
'; + } + } elseif ( $_POST[ 'action' ] === 'disable_totp' && ! empty( $_SESSION[ 'email_user' ] ) ) { + $stmt = $db->prepare( 'SELECT totp_secret FROM mailbox WHERE username = ? AND active = 1;' ); + $stmt->execute( [ $_SESSION[ 'email_user' ] ] ); + $totp_secret = $stmt->fetchColumn(); + if ( is_string( $totp_secret ) && verify_totp_code( $totp_secret, $_POST[ 'totp_code' ] ?? '' ) ) { + $stmt = $db->prepare( 'UPDATE mailbox SET totp_secret = NULL WHERE username = ? AND active = 1;' ); + $stmt->execute( [ $_SESSION[ 'email_user' ] ] ); + unset( $_SESSION[ 'user_totp_setup' ] ); + $msg .= ''.htmlspecialchars(_('Successfully disabled TOTP two-factor authentication')).'
'; + } else { + $msg .= ''.htmlspecialchars(_('Invalid TOTP code')).'
'; + } } } } @@ -197,7 +245,23 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {