Databases, Systems & Networks La caverne d'Ali Data

Accueil Logiciel

Serveur mail sur Ubuntu 26.04 LTS : Postfix, MariaDB, Dovecot, Amavis, SpamAssassin, ClamAV

13. Postfix

Tables de correspondance SQL :

. /root/mail.env
install -d -o root -g postfix -m 750 /etc/postfix/sql
creer_map() {
  cat > "/etc/postfix/sql/$1.cf" <<EOF
hosts = 127.0.0.1
user = mailread
password = $MDP_LECTURE
dbname = postfix
query = $2
EOF
}
creer_map domains "SELECT domain FROM domain WHERE domain='%s' AND active=1 AND backupmx=0"
creer_map mailboxes "SELECT maildir FROM mailbox WHERE username='%s' AND active=1"
creer_map aliases "SELECT goto FROM alias WHERE address='%s' AND active=1"
creer_map alias_domain_mailboxes "SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain='%d' AND mailbox.username=CONCAT('%u','@',alias_domain.target_domain) AND mailbox.active=1 AND alias_domain.active=1"
creer_map alias_domain_aliases "SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain='%d' AND alias.address=CONCAT('%u','@',alias_domain.target_domain) AND alias.active=1 AND alias_domain.active=1"
creer_map alias_domain_catchall "SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain='%d' AND alias.address=CONCAT('@',alias_domain.target_domain) AND alias.active=1 AND alias_domain.active=1"
creer_map own_domain "SELECT 'REJECT Authentification requise pour ce domaine expediteur' FROM domain WHERE domain='%s' AND active=1"
chown root:postfix /etc/postfix/sql/*.cf
chmod 640 /etc/postfix/sql/*.cf

Filtrage des pièces jointes exécutables, puis main.cf complet :

. /root/mail.env
cat > /etc/postfix/mime_header_checks.pcre <<'EOF'
/^\s*Content-(?:Disposition|Type):.*name\s*=\s*"?[^";]*\.(exe|scr|pif|com|bat|cmd|vbs|vbe|js|jse|wsf|hta|msi|lnk|ps1)"?\s*(?:;|$)/ REJECT Piece jointe interdite : .$1
EOF

[ -f /etc/postfix/main.cf.orig ] || cp /etc/postfix/main.cf /etc/postfix/main.cf.orig
cat > /etc/postfix/main.cf <<'EOF'
compatibility_level = 3.10

myhostname = __HOTE__
mydomain = __DOMAINE__
myorigin = $mydomain
mydestination = $myhostname, localhost.$mydomain, localhost
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
inet_interfaces = all
inet_protocols = all

default_database_type = lmdb
alias_maps = lmdb:/etc/aliases
alias_database = lmdb:/etc/aliases

smtpd_banner = $myhostname ESMTP
biff = no
append_dot_mydomain = no
recipient_delimiter = +
message_size_limit = 52428800
mailbox_size_limit = 0
smtpd_helo_required = yes
strict_rfc821_envelopes = yes
disable_vrfy_command = yes

# TLS
smtpd_tls_chain_files = /etc/ssl/mail/privkey.pem, /etc/ssl/mail/fullchain.pem
smtpd_tls_security_level = may
smtpd_tls_auth_only = yes
smtpd_tls_protocols = >=TLSv1.2
smtpd_tls_mandatory_protocols = >=TLSv1.2
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtp_tls_security_level = may
smtp_tls_protocols = >=TLSv1.2
smtp_tls_CApath = /etc/ssl/certs
smtp_tls_loglevel = 1
smtp_tls_session_cache_database = lmdb:${data_directory}/smtp_scache

# SASL (activé uniquement sur les ports 587 et 465)
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = no

# Domaines et boîtes virtuels
virtual_mailbox_domains = proxy:mysql:/etc/postfix/sql/domains.cf
virtual_mailbox_maps =
  proxy:mysql:/etc/postfix/sql/mailboxes.cf,
  proxy:mysql:/etc/postfix/sql/alias_domain_mailboxes.cf
virtual_alias_maps =
  proxy:mysql:/etc/postfix/sql/aliases.cf,
  proxy:mysql:/etc/postfix/sql/alias_domain_aliases.cf,
  proxy:mysql:/etc/postfix/sql/alias_domain_catchall.cf
virtual_transport = lmtp:unix:private/dovecot-lmtp
# une adresse d'expéditeur n'est utilisable que par son ou ses destinataires
smtpd_sender_login_maps =
  proxy:mysql:/etc/postfix/sql/aliases.cf,
  proxy:mysql:/etc/postfix/sql/alias_domain_aliases.cf

# Restrictions du port 25
smtpd_helo_restrictions =
  permit_mynetworks,
  reject_invalid_helo_hostname,
  reject_non_fqdn_helo_hostname
smtpd_sender_restrictions =
  reject_non_fqdn_sender,
  permit_mynetworks,
  reject_unknown_sender_domain,
  check_sender_access mysql:/etc/postfix/sql/own_domain.cf
smtpd_relay_restrictions =
  permit_mynetworks,
  reject_unauth_destination
smtpd_recipient_restrictions =
  reject_non_fqdn_recipient,
  permit_mynetworks,
  reject_unknown_recipient_domain,
  reject_unlisted_recipient,
  check_policy_service unix:private/quota-status
smtpd_data_restrictions = reject_unauth_pipelining

# Restrictions des ports 587 et 465 (référencées dans master.cf)
mua_sender_restrictions =
  reject_non_fqdn_sender,
  reject_sender_login_mismatch
mua_relay_restrictions =
  permit_sasl_authenticated,
  reject
mua_recipient_restrictions =
  reject_non_fqdn_recipient,
  check_policy_service inet:127.0.0.1:10040

mime_header_checks = pcre:/etc/postfix/mime_header_checks.pcre

# Postscreen : filtrage des robots avant smtpd
postscreen_access_list = permit_mynetworks
postscreen_greet_action = enforce
postscreen_dnsbl_action = enforce
postscreen_dnsbl_threshold = 2
postscreen_dnsbl_sites =
  zen.spamhaus.org=127.0.0.[2..11]*2,
  bl.spamcop.net=127.0.0.2*1,
  bl.mailspike.net=127.0.0.2*1

# Filtrage Amavis
content_filter = smtp-amavis:[127.0.0.1]:10024
EOF
appliquer /etc/postfix/main.cf
chmod 644 /etc/postfix/main.cf /etc/postfix/mime_header_checks.pcre
newaliases

master.cf (modifié par postconf, sans édition manuelle) :

# postscreen sur le port 25
postconf -M "smtp/inet=smtp inet n - y - 1 postscreen"
postconf -M "smtpd/pass=smtpd pass - - y - - smtpd"
postconf -M "dnsblog/unix=dnsblog unix - - y - 0 dnsblog"
postconf -M "tlsproxy/unix=tlsproxy unix - - y - 0 tlsproxy"

# soumission authentifiée : 587 (STARTTLS) et 465 (TLS implicite)
postconf -M "submission/inet=submission inet n - y - - smtpd"
postconf -M "submissions/inet=submissions inet n - y - - smtpd"
for s in submission submissions; do
  postconf -P "$s/inet/syslog_name=postfix/$s" \
    "$s/inet/smtpd_sasl_auth_enable=yes" \
    "$s/inet/smtpd_client_restrictions=" \
    "$s/inet/smtpd_helo_restrictions=" \
    "$s/inet/smtpd_sender_restrictions=\$mua_sender_restrictions" \
    "$s/inet/smtpd_relay_restrictions=\$mua_relay_restrictions" \
    "$s/inet/smtpd_recipient_restrictions=\$mua_recipient_restrictions" \
    "$s/inet/content_filter=smtp-amavis:[127.0.0.1]:10026"
done
postconf -P "submission/inet/smtpd_tls_security_level=encrypt" \
  "submissions/inet/smtpd_tls_wrappermode=yes"

# courrier local (cron, système) : pas de filtrage
postconf -P "pickup/unix/content_filter=" \
  "pickup/unix/receive_override_options=no_header_body_checks"

# transport vers Amavis
postconf -M "smtp-amavis/unix=smtp-amavis unix - - y - 2 smtp"
postconf -P "smtp-amavis/unix/smtp_data_done_timeout=1200" \
  "smtp-amavis/unix/smtp_send_xforward_command=yes" \
  "smtp-amavis/unix/smtp_dns_support_level=disabled" \
  "smtp-amavis/unix/smtp_tls_security_level=none" \
  "smtp-amavis/unix/max_use=20"

# retour d'Amavis vers Postfix
r="127.0.0.1:10025/inet"
postconf -M "$r=127.0.0.1:10025 inet n - y - - smtpd"
postconf -P "$r/syslog_name=postfix/reinjection" \
  "$r/content_filter=" \
  "$r/mynetworks=127.0.0.0/8" \
  "$r/smtpd_tls_security_level=none" \
  "$r/smtpd_helo_required=no" \
  "$r/smtpd_client_restrictions=" \
  "$r/smtpd_helo_restrictions=" \
  "$r/smtpd_sender_restrictions=" \
  "$r/smtpd_relay_restrictions=permit_mynetworks,reject" \
  "$r/smtpd_recipient_restrictions=permit_mynetworks,reject" \
  "$r/smtpd_data_restrictions=" \
  "$r/local_recipient_maps=" \
  "$r/relay_recipient_maps=" \
  "$r/smtpd_client_connection_count_limit=0" \
  "$r/smtpd_client_connection_rate_limit=0" \
  "$r/receive_override_options=no_unknown_recipient_checks,no_header_body_checks,no_milters,no_address_mappings"

14. Limitation des envois (postfwd, remplace Policyd v2)

200 destinataires par heure et par compte authentifié :

cat > /etc/postfix/postfwd.cf <<'EOF'
id=QUOTA_ENVOI
  sasl_username=!!(^$)
  action=rate(sasl_username/200/3600/450 4.7.1 Limite d'envoi atteinte, reessayez plus tard)
id=DEFAUT
  action=DUNNO
EOF
chmod 644 /etc/postfix/postfwd.cf
sed -i 's/^STARTUP=.*/STARTUP=1/' /etc/default/postfwd
systemctl enable postfwd
systemctl restart postfwd
postfix check
systemctl enable postfix
systemctl restart postfix

Page suivante : Quarantaine, entretien, pare-feu et certificat

Pages : ⬅ Page précédente Page suivante ➡

Lire aussi:  Thunderbird/Change account order