Based on Debian 11 "Bullseye" environment.
Now Postfix accepts relaying (sending out) the mails only from the localhost (e.g. cronjob). Set up authorization mechanisms to enable mailbox users to send out emails.
Here I use Dovecot for SMTP Auth and integrate with Postfix.
Because outbound port 25 is often blocked by the internet provider, we use port 587 (submission port) to connect from MUA to the server.
Before setting up authentication, let Postfix use a proper server certificate to encrypt the connection between Postfix and MUA.
The test certificate is set in /etc/postfix/main.cf by default, so change them to the valid certificate from Let's Encrypt (or anything else you have).
# TLS parameters smtpd_tls_cert_file=/etc/letsencrypt/live/example.jp/fullchain.pem # Change this line to valid certificate smtpd_tls_key_file=/etc/letsencrypt/live/example.jp/privkey.pem # Change this line to valid private key file smtpd_tls_security_level=may
Uncomment configurations in /etc/dovecot/conf.d/10-master.conf to enable smtp-auth for Postfix.
service auth { # auth_socket_path points to this userdb socket by default. It's typically # used by dovecot-lda, doveadm, possibly imap process, etc. Users that have # full permissions to this socket are able to get a list of all usernames and # get the results of everyone's userdb lookups. # # The default 0666 mode allows anyone to connect to the socket, but the # userdb lookups will succeed only if the userdb returns an "uid" field that # matches the caller process's UID. Also if caller's uid or gid matches the # socket's uid or gid the lookup succeeds. Anything else causes a failure. # # To give the caller full permissions to lookup all users, set the mode to # something else than 0666 and Dovecot lets the kernel enforce the # permissions (e.g. 0777 allows everyone full permissions). unix_listener auth-userdb { #mode = 0666 #user = #group = } # Postfix smtp-auth # Uncomment following lines unix_listener /var/spool/postfix/private/auth { mode = 0666 } # Auth process is run as this user. #user = $default_internal_user }
Comment out normal PAM (Linux user auth, auth-system.conf.ext) from the list unless you need it. /etc/dovecot/conf.d/10-auth.conf
#!include auth-system.conf.ext
#!include auth-sql.conf.ext
#!include auth-ldap.conf.ext
!include auth-passwdfile.conf.ext
#!include auth-checkpassword.conf.ext
#!include auth-vpopmail.conf.ext
#!include auth-static.conf.ext
Restart dovecot.
# systemctl restart dovecot
Add SASL configuration to /etc/postfix/main.cf.
# SASL smtpd_sasl_type = dovecot smtpd_sasl_path = private/auth smtpd_sasl_auth_enable = yes smtpd_tls_auth_only = yes
Reload Postfix
# systemctl reload postfix
If your provider doesn't block the Outbound port 25 connection, you can check what happens when sending out the mail.
If succeeded, the mail should reach the destination and the following log will show up in /var/log/mail.log.
postfix/smtpd[xxxx]: AFXXXXXXXX: client=xxx.ne.jp[000.000.000.000], sasl_method=PLAIN, sasl_username=info@mail.example.jp
For the mailbox users, port 587 is the normal port to connect from MUA.
Enable submission section in /etc/postfix/master.cf
submission inet n - y - - smtpd -o syslog_name=postfix/submission -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes -o smtpd_tls_auth_only=yes -o smtpd_reject_unlisted_recipient=no -o smtpd_client_restrictions=$mua_client_restrictions -o smtpd_helo_restrictions=$mua_helo_restrictions -o smtpd_sender_restrictions=$mua_sender_restrictions -o smtpd_recipient_restrictions= -o smtpd_relay_restrictions=permit_sasl_authenticated,reject -o milter_macro_daemon_name=ORIGINATING
Reload Postfix
# systemctl reload postfix
Like the port 25 case above, the successful log should appear in /etc/log/mail.log. The difference is, the log will show you it's connected via submission port.
postfix/submission/smtpd[xxxx]: AFXXXXXXXX: client=xxx.ne.jp[000.000.000.000], sasl_method=PLAIN, sasl_username=info@mail.example.jp
After opening the submission port, there will be continuing attacks. Fail2ban has the Pestfix preset to shut them.
Here I tweaked (included the latest version) to take countermeasures for the brute-force attack.
Create /etc/fail2ban/jail.d/postfix.conf with the contents below.
[postfix] enabled = true mode = aggressive findtime = 60m
Enable this configuration.
# systemctl restart fail2ban
You can check the result in the fail2ban logs: /var/log/fail2ban.log
2020-03-28
2021-09-08
2022-09-14