Postfix

通過虛擬別名和別名被 spamassassin 退回的使用者未知的郵件

  • October 1, 2019

我在 /etc/postfix/virtual 中有以下條目:

v2@mydomain.org tibor@mydomain.org, tracking@works-organiser.com
tracking@works-organiser.com tracking.tracking@localhost

/etc/aliases 中的以下條目,將電子郵件轉發到命令:

tracking.tracking: |"/usr/local/v2_mail/v2/script/email_handler.sh"

如果從電子郵件伺服器本地發送到 v2@mydomain.org的郵件(通過 dovecot 服務發送),則此配置完美執行,****但如果從遠端伺服器發送(通過 spamassassin 服務發送)則不能。在第二種情況下,郵件被退回(使用者未知)。**看來,spamassassin 不知道 /etc/aliases。**我該如何解決?

通過郵件伺服器從遠端伺服器發送的消息的日誌,spamassassin 正在執行:

Sep 18 09:39:21 zg-3 postfix/smtpd[29296]: B7905A7318: client=zg-2.mydomain.org[94.231.88.101]
Sep 18 09:39:21 zg-3 postfix/cleanup[29301]: B7905A7318: message-id=<20190918074001.CFB7B40CDF@v2.zg-2.mydomain.org>
Sep 18 09:39:21 zg-3 postfix/qmgr[24943]: B7905A7318: from=<v2@mydomain.org>, size=1427, nrcpt=3 (queue active)
Sep 18 09:39:21 zg-3 postfix/pipe[29477]: B7905A7318: to=<tibor.nagy@mydomain.org>, orig_to=<v2@mydomain.org>, relay=spamassassin, delay=0.2, delays=0.01/0.01/0/0.18, dsn=2.0.0, status=sent (delivered via spamassassin service)
Sep 18 09:39:22 zg-3 postfix/pipe[29479]: B7905A7318: to=<tracking.tracking@localhost>, orig_to=<v2@mydomain.org>, relay=spamassassin, delay=0.43, delays=0.01/0.02/0/0.4, dsn=5.1.1, status=bounced (user unknown)
Sep 18 09:39:22 zg-3 postfix/bounce[29486]: B7905A7318: sender non-delivery notification: 2BBCAA731F
Sep 18 09:39:22 zg-3 postfix/qmgr[24943]: B7905A7318: removed

從郵件伺服器本地發送的消息日誌 spamassassin 未執行:

Sep 18 09:53:46 zg-3 postfix/pickup[29998]: 07DADA7318: uid=0 from=<root@zg-3.mydomain.org>
Sep 18 09:53:46 zg-3 postfix/cleanup[30146]: 07DADA7318: message-id=<20190918075346.07DADA7318@zg-3.mydomain.org>
Sep 18 09:53:46 zg-3 postfix/qmgr[24943]: 07DADA7318: from=<root@zg-3.mydomain.org>, size=399, nrcpt=3 (queue active)
Sep 18 09:53:46 zg-3 postfix/pipe[30466]: 07DADA7318: to=<tibor.nagy@mydomain.org>, orig_to=<v2@mydomain.org>, relay=dovecot, delay=0.05, delays=0.01/0.01/0/0.02, dsn=2.0.0, status=sent (delivered via dovecot service)
Sep 18 09:53:46 zg-3 postfix/local[30147]: 07DADA7318: to=<tracking.tracking@localhost>, orig_to=<v2@mydomain.org>, relay=local, delay=0.45, delays=0.01/0/0/0.43, dsn=2.0.0, status=sent (delivered to command: /usr/local/v2_mail/v2/script/email_handler.sh)
Sep 18 09:53:46 zg-3 postfix/qmgr[24943]: 07DADA7318: removed

我的 main.cf:

# Puppet module: postfix_server_primary, template: main.cf.erb

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

myhostname = zg-3.mydomain.org
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = mydomain.org
mydestination = zg-3.mydomain.org, localhost.mydomain.org, localhost
relayhost = 
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 94.231.88.102 172.16.0.0/16 80.254.175.85
# 50 MB
message_size_limit = 52428800

### TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

### Dovecot SMTP authentication
smtpd_sasl_type = dovecot                                                                           
smtpd_sasl_path = private/auth                                                                      
smtpd_sasl_auth_enable = yes
# With Postfix version before 2.10, use smtpd_recipient_restrictions
#smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
# Milter configuration
# OpenDKIM
milter_default_action = accept
milter_protocol = 6
smtpd_milters = local:/opendkim/opendkim.sock
non_smtpd_milters = local:/opendkim/opendkim.sock

### Virtual aliases to push emails into V2 systems: 
# 1. virtual alias to <system_name>.email_address@localhost
# 2. alias <system_name>.email_address@localhost |"/usr/local/v2_mail/v2/script/email_handler.sh..."
# See http://www.postfix.org/VIRTUAL_README.html#virtual_alias for virtual aliases
virtual_alias_domains = works-organiser.com
# can be used more than one e.g. for different domains
virtual_alias_maps = hash:/etc/postfix/virtual

mailbox_size_limit = 0
virtual_mailbox_limit = 0
recipient_delimiter = +
inet_interfaces = all

### Configuration for virtual mailboxes
home_mailbox = Maildir/
virtual_mailbox_domains = /etc/postfix/vhosts
virtual_mailbox_base = /home/vmail
virtual_mailbox_maps = hash:/etc/postfix/vmaps
virtual_minimum_uid = 1000
virtual_uid_maps = static:1002
virtual_gid_maps = static:1002

virtual_transport = dovecot
spamassassin_destination_recipient_limit = 1
dovecot_destination_recipient_limit = 1

我的主人.cf:

# Puppet module: postfix_server_primary, template: master.cf.erb
#
# Postfix master process configuration file.  For details on the format
# of the file, see the master(5) manual page (command: "man 5 master").
#
# Do not forget to execute "postfix reload" after editing this file.
#
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
smtp      inet  n       -       -       -       -       smtpd
 -o content_filter=spamassassin
#smtp      inet  n       -       -       -       1       postscreen
#smtpd     pass  -       -       -       -       -       smtpd
#dnsblog   unix  -       -       -       -       0       dnsblog
#tlsproxy  unix  -       -       -       -       0       tlsproxy
submission inet n       -       -       -       -       smtpd
 -o syslog_name=postfix/submission
 # SSL/TLS
 -o smtpd_tls_wrappermode=yes
 # STARTTLS
 #  -o smtpd_tls_security_level=encrypt
 -o smtpd_sasl_auth_enable=yes
 -o smtpd_client_restrictions=permit_sasl_authenticated,reject
 -o milter_macro_daemon_name=ORIGINATING
smtps     inet  n       -       -       -       -       smtpd
 -o syslog_name=postfix/smtps
 # SSL/TLS
 -o smtpd_tls_wrappermode=yes
 # STARTTLS
 #  -o smtpd_tls_security_level=encrypt
 -o smtpd_sasl_auth_enable=yes
 -o smtpd_client_restrictions=permit_sasl_authenticated,reject
 -o milter_macro_daemon_name=ORIGINATING
#628       inet  n       -       -       -       -       qmqpd
pickup    fifo  n       -       -       60      1       pickup
cleanup   unix  n       -       -       -       0       cleanup
qmgr      fifo  n       -       n       300     1       qmgr
#qmgr     fifo  n       -       n       300     1       oqmgr
tlsmgr    unix  -       -       -       1000?   1       tlsmgr
rewrite   unix  -       -       -       -       -       trivial-rewrite
bounce    unix  -       -       -       -       0       bounce
defer     unix  -       -       -       -       0       bounce
trace     unix  -       -       -       -       0       bounce
verify    unix  -       -       -       -       1       verify
flush     unix  n       -       -       1000?   0       flush
proxymap  unix  -       -       n       -       -       proxymap
proxywrite unix -       -       n       -       1       proxymap
smtp      unix  -       -       -       -       -       smtp
relay     unix  -       -       -       -       -       smtp
#       -o smtp_helo_timeout=5 -o smtp_connect_timeout=5
showq     unix  n       -       -       -       -       showq
error     unix  -       -       -       -       -       error
retry     unix  -       -       -       -       -       error
discard   unix  -       -       -       -       -       discard
local     unix  -       n       n       -       -       local
virtual   unix  -       n       n       -       -       virtual
lmtp      unix  -       -       -       -       -       lmtp
anvil     unix  -       -       -       -       1       anvil
scache    unix  -       -       -       -       1       scache
#
# ====================================================================
# Interfaces to non-Postfix software. Be sure to examine the manual
# pages of the non-Postfix software to find out what options it wants.
#
# Many of the following services use the Postfix pipe(8) delivery
# agent.  See the pipe(8) man page for information about ${recipient}
# and other message envelope options.
# ====================================================================
#
# maildrop. See the Postfix MAILDROP_README file for details.
# Also specify in main.cf: maildrop_destination_recipient_limit=1
#
maildrop  unix  -       n       n       -       -       pipe
 flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}
#
# ====================================================================
#
# Recent Cyrus versions can use the existing "lmtp" master.cf entry.
#
# Specify in cyrus.conf:
#   lmtp    cmd="lmtpd -a" listen="localhost:lmtp" proto=tcp4
#
# Specify in main.cf one or more of the following:
#  mailbox_transport = lmtp:inet:localhost
#  virtual_transport = lmtp:inet:localhost
#
# ====================================================================
#
# Cyrus 2.1.5 (Amos Gouaux)
# Also specify in main.cf: cyrus_destination_recipient_limit=1
#
#cyrus     unix  -       n       n       -       -       pipe
#  user=cyrus argv=/cyrus/bin/deliver -e -r ${sender} -m ${extension} ${user}
#
# ====================================================================
# Old example of delivery via Cyrus.
#
#old-cyrus unix  -       n       n       -       -       pipe
#  flags=R user=cyrus argv=/cyrus/bin/deliver -e -m ${extension} ${user}
#
# ====================================================================
#
# See the Postfix UUCP_README file for configuration details.
#
uucp      unix  -       n       n       -       -       pipe
 flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
#
# Other external delivery methods.
#
ifmail    unix  -       n       n       -       -       pipe
 flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
bsmtp     unix  -       n       n       -       -       pipe
 flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t$nexthop -f$sender $recipient
scalemail-backend unix  -       n       n       -       2       pipe
 flags=R user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store ${nexthop} ${user} ${extension}
mailman   unix  -       n       n       -       -       pipe
 flags=FR user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py
 ${nexthop} ${user}
spamassassin unix -     n       n       -       -       pipe
 flags=DROhu user=vmail:vmail argv=/usr/bin/spamc -f -e
 /usr/lib/dovecot/deliver -f ${sender} -d ${user}@${nexthop}
dovecot unix -     n       n       -       -       pipe
 flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -f ${sender} -d ${user}@${nexthop}

問題是,dovecot(不是 spamassassin)作為本地傳遞代理 (LDA) 不知道 /etc/aliases。所以我決定將我自己的 LDA 實現為一個簡單的 shell 腳本:

#!/bin/bash

# This script started by spamassassin (spamc) as defined in /etc/postfix/master.cf:
# spamassassin unix -     n       n       -       -       pipe
#  flags=DROhu user=vmail:vmail argv=/usr/bin/spamc -f -e /usr/local/mail/script/lda.sh ${sender} ${user}@${nexthop}
#
# So the first argument is the "from address", the 2nd is the "to address".
# If the "to address" is in format address@localhost and address is a line in /etc/aliases, and
# aliased to /usr/local/v2_mail/v2/script/email_handler.sh, than do the forwarding here
# otherwise forward to /usr/lib/dovecot/deliver or to /usr/bin/sendmail
# depending on local or remote delivery
#
# This workaround is necessary because dovecot has no idea about /etc/aliases

shopt -s extglob

if [[ "$2" =~ "@localhost" ]]; then
       # Get address without "@localhost"
       ADDRESS=${2%%*(@localhost)}
       # Look for that line in /etc/aliases
       LINE=`grep "^$ADDRESS: |\"/usr/local/v2_mail/v2/script/email_handler.sh" /etc/aliases`
       if [ $? -eq 0 ]; then
               # Line found, extract and run the command
               CMD=${LINE//$ADDRESS: |\"/}
               CMD=${CMD//\"/}
               # Log the command execution
               echo v2_lda.sh: $CMD
               exec $CMD
       fi
fi

# Local or remote delivery
if [[ "$2" =~ "@mydomain.org" ]] || [[ "$2" =~ "@mymail.com" ]] || [[ "$2" =~ "@localhost" ]]
then
       # DO FURTHER PROCESSING OF /etc/aliases HERE IF YOU HAVE MORE
       # ENTRIES IN IT

       # Log the command execution
       echo v2_lda.sh: /usr/lib/dovecot/deliver -f $1 -d $2
       /usr/lib/dovecot/deliver -f $1 -d $2
       exit $?
else
       # Log the command execution
       echo v2_lda.sh: /usr/sbin/sendmail -f $1 $2
       /usr/sbin/sendmail -f $1 $2
       exit $?
fi

該腳本位於 /usr/local/mail/script/lda.sh 中。所以我改變了 master.cf 中的 spamassassin 行:

spamassassin unix -     n       n       -       -       pipe
 flags=DROhu user=vmail:vmail argv=/usr/bin/spamc -f -e /usr/local/mail/script/lda.sh ${sender} ${user}@${nexthop}

引用自:https://serverfault.com/questions/984652