Add login tracking to smtp sessions

This commit is contained in:
danwin1210 committed 2026-10-17 23:16:14 +02:00
1 parent df186e33d7
commit 3106dc2003
4 files changed
+54 -2

No files matched your search

+2
View File
@@ -84,6 +84,8 @@ Configure DKIM signing maps by replacing the example hostname and token in `/etc
The authenticated-user ratelimit map is configured in `/etc/rspamd/custom_ratelimit.lua`; replace its example hostname and token with your map endpoint settings. Accounts registered within the last 14 days receive lower sending limits. A mailbox marked as trusted in the admin editor receives higher limits; other authenticated mailboxes receive the default limits.
Postfix updates a mailbox's `last_login` timestamp when an authenticated SMTP session reaches the `MAIL FROM` command. Install the policy script at `/var/www/mail/tools/postfix_last_login_policy.php` and reload Postfix after updating `/etc/postfix/main.cf` and `/etc/postfix/master.cf`. An SMTP session that authenticates but disconnects before `MAIL FROM` cannot be recorded by this policy hook.
Create a password used for your TURN server and replace all `YOUR_SECRET` in `/etc/prosody/prosody.cfg.lua` with it.
Install [acme.sh](https://github.com/acmesh-official/acme.sh) or [certbot](https://certbot.eff.org/) to obtain a free letsencrypt SSL certificate, then update the path to this new certificate in the following files:
+1 -1
View File
@@ -65,7 +65,7 @@ smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_client_restrictions = reject_unauth_pipelining
smtpd_recipient_restrictions = check_policy_service inet:127.0.0.1:12340, permit_sasl_authenticated, check_recipient_access proxy:mysql:/etc/postfix/sql/mysql_tls_policy_in.cf
smtpd_sender_restrictions = reject_sender_login_mismatch, check_sender_access inline:{{<> = REJECT}}, permit_sasl_authenticated
smtpd_sender_restrictions = reject_sender_login_mismatch, check_sender_access inline:{{<> = REJECT}}, check_policy_service { unix:private/last-login, timeout=5s, default_action=DUNNO }, permit_sasl_authenticated
smtpd_relay_restrictions = permit_sasl_authenticated, permit_auth_destination, reject_unauth_destination
smtpd_sender_login_maps = proxy:mysql:/etc/postfix/sql/mysql_virtual_auth_maps.cf
+2 -1
View File
@@ -12,6 +12,8 @@
smtp inet n - n - 2 smtpd
-o smtpd_sasl_auth_enable=no
-o { smtpd_sender_restrictions = check_sender_access inline:{double-bounce@mail.danwin1210.de=OK} check_sender_access mysql:/etc/postfix/sql/mysql_virtual_domain_unauth_block.cf }
last-login unix - n n - 0 spawn
user=www-data argv=/usr/bin/php /var/www/mail/tools/postfix_last_login_policy.php
26 inet n - n - 2 smtpd
-o smtpd_sasl_auth_enable=no
-o { smtpd_sender_restrictions = check_sender_access mysql:/etc/postfix/sql/mysql_virtual_domain_unauth_block.cf }
@@ -129,4 +131,3 @@ scache unix - - y - 1 scache
#mailman unix - n n - - pipe
# flags=FR user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py
# ${nexthop} ${user}
+49
View File
@@ -0,0 +1,49 @@
<?php
require_once __DIR__ . '/../common_config.php';
function handle_postfix_policy_request(): bool
{
$attributes = [];
while ( ( $line = fgets( STDIN ) ) !== false ) {
$line = rtrim( $line, "\r\n" );
if ( $line === '' ) {
if ( $attributes ) {
break;
}
continue;
}
$separator = strpos( $line, '=' );
if ( $separator !== false ) {
$attributes[ substr( $line, 0, $separator ) ] = substr( $line, $separator + 1 );
}
}
if ( ! $attributes ) {
return false;
}
if (
( $attributes[ 'request' ] ?? '' ) === 'smtpd_access_policy'
&& ( $attributes[ 'protocol_state' ] ?? '' ) === 'MAIL'
&& is_string( $attributes[ 'sasl_username' ] ?? null )
&& preg_match( '/^[^@\s]+@[^@\s]+$/D', $attributes[ 'sasl_username' ] )
) {
try {
$db = new PDO(
'mysql:host=' . DBHOST . ';dbname=' . DBNAME . ';charset=utf8mb4',
DBUSER,
DBPASS,
[ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => PERSISTENT, PDO::ATTR_TIMEOUT => 5 ]
);
$stmt = $db->prepare( 'UPDATE mailbox SET last_login = ? WHERE username = ? AND active = 1;' );
$stmt->execute( [ time(), mb_strtolower( $attributes[ 'sasl_username' ] ) ] );
} catch ( PDOException $exception ) {
error_log( 'Unable to update mailbox last_login from Postfix policy request: ' . $exception->getMessage() );
}
}
fwrite( STDOUT, "action=DUNNO\n\n" );
return true;
}
while ( handle_postfix_policy_request() ) {
}