0

I have the following entry in /etc/postfix/virtual:

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

The following entry in /etc/aliases, which forwrds the email to a command:

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

This configuration works perfectly if the mail sent to v2@mydomain.org locally from the email server (delivered via dovecot service), but not if sent from a remote server (delivered via spamassassin service). In the second case the message is bounced (user unknown). It seems, that spamassassin has no idea about /etc/aliases. How can I solve that?

The log of the message sent from a remote server through the mail server, spamassassin is running:

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

The log of a message sent locally from the mail server, spamassassin doesn't run:

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

My 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

My master.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}
0

The problem is really, that dovecot (not spamassassin) as local delivery agent (LDA) has no idea about /etc/aliases. So I've decided to implement my own LDA as a simple shell script:

#!/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

The script is in /usr/local/mail/script/lda.sh. So I've changed the spamassassin line in 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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.