12

How can I force postfix to check (and log/flag/etc) all outgoing mail for spam?

Just want to make sure I'm doing my part on my servers.

EDIT: Postfix config, as requested.

main.cf

# See /usr/share/postfix/main.cf.dist for a commented, more complete version


# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
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

# 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

smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth

smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes

smtpd_recipient_restrictions =
   permit_sasl_authenticated,
   permit_mynetworks,
   reject_unauth_destination

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

alias_maps = hash:/etc/aliases

myhostname = example.com
myorigin = /etc/mailname
mydestination = localhost, localhost.localdomain, localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
content_filter = smtp-amavis:[127.0.0.1]:10024

transport_maps = mysql:/etc/postfix/mysql/transport_maps.cf

virtual_mailbox_domains = mysql:/etc/postfix/mysql/virtual_alias_domains.cf
virtual_alias_maps = mysql:/etc/postfix/mysql/virtual_alias_maps.cf


virtual_mailbox_base = /var/mail
virtual_mailbox_maps = mysql:/etc/postfix/mysql/virtual_mailbox_maps.cf
virtual_minimum_uid = 100
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
dovecot_destination_recipient_limit = 1

master.cf

#
# 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
submission inet n       -       -       -       -       smtpd
#  -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 smtpd_tls_wrappermode=yes
#  -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       -       -       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
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
relay     unix  -       -       -       -       -       smtp
    -o smtp_fallback_relay=
#       -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

127.0.0.1:10025     inet    n   -   y   -   -   smtpd
    -o content_filter=
    -o local_recipient_maps=
    -o relay_recipient_maps=
    -o smtpd_restriction_classes=
    -o smtpd_helo_restrictions=
    -o smtpd_sender_restrictions=
    -o smtpd_recipient_restrictions=permit_mynetworks,reject

    -o mynetworks=127.0.0.0/8
    -o strict_rfc821_envelopes=yes
    -o smtpd_error_sleep_time=0
    -o smtpd_soft_error_limit=1001
    -o smtpd_hard_error_limit=100

smtp-amavis unix -  -   y   -   2   smtp
    -o smtp_data_done_timeout=1200
    -o disable_dns_lookups=yes
#
# ====================================================================
# 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}
#
# 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}

dovecot   unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -f ${sender} -d ${recipient}

mysql/transport_maps.cf

user = user
password = password
hosts = 127.0.0.1

dbname = postfix

query = SELECT target FROM transports WHERE hostname = '%d' AND active = 1

mysql/virtual_alias_domains.cf

user = user
password = password
hosts = 127.0.0.1

dbname = postfix
select_field = domain
table = domains
where_field = domain
additional_conditions = AND active = 1

mysql/virtual_alias_maps.cf

user = user
password = password
hosts = 127.0.0.1

dbname = postfix
select_field = forward
table = aliases
where_field = source
additional_conditions = AND active = 1

mysql/virtual_mailbox_maps.cf

user = user
password = password
hosts = 127.0.0.1

dbname = postfix

query = SELECT mailbox_path FROM users WHERE address = '%s' AND active = 1
5
  • Have you considered using something like MIMEDefang (or amavisd-new) with SpamAssassin?
    – Cry Havok
    Mar 25, 2010 at 16:37
  • Is all outbound email currently being fed to your Postfix server or does it go out via a different route? Any chance you could post your configuration files? Mar 29, 2010 at 14:56
  • @Brian: All config files have been posted. Also, all mail runs through postfix. Mar 30, 2010 at 13:08
  • 1
    Looks like this configuration should already scan outgoing emails for SPAM. Sorry to ask, but are you sure they are not getting scanned for SPAM? Apr 3, 2010 at 16:39
  • Any spam tags you add on outbound email are likely dropped by the incoming mail filters at the remote site. They don't want email to arrive with spam tag headers already in place. However, you can check for scanning on your amavis by sending out a test message and searching (grep) for this in your amavisd log file.
    – labradort
    Apr 7, 2010 at 14:19

4 Answers 4

8

Below is a config "stub" for also checking outgoing messages.

In main.cf:

smtpd_sender_restrictions = 
   check_client_access cidr:/etc/postfix/internal_clients_filter

And: /etc/postfix/internal_clients_filter

192.168.0.0/24 FILTER smtp:[127.0.0.1]:12501
10.0.0.0/24 FILTER smtp:[127.0.0.1]:12501

(you could also do this in other ways for logged in users, ip, from etc..)

Use a policybank in Amavisd-new:

$interface_policy{'12501'} = 'INTERNAL'; 
$policy_bank{'INTERNAL'} = {  # mail originating from clients in cidr:/etc/postfix/internal_clients_filter
  bypass_spam_checks_maps   => [0],  # spam-check outgoing mail 
  bypass_banned_checks_maps => [0],  # banned-check outgoing mail 
  bypass_header_checks_maps => [0],  # header-check outgoing mail  
  forward_method => 'smtp:[127.0.0.1]:12502', # relay to Postfix listener on port 12502
};

And the reinject path in postfix:

127.0.0.1:12502 inet    n    -    n    -    -    smtpd
    -o smtpd_recipient_restrictions=permit_mynetworks,reject
    -o smtpd_restriction_classes=
    -o smtpd_delay_reject=no
    -o smtpd_client_restrictions=
    -o smtpd_helo_restrictions=
    -o smtpd_sender_restrictions=
    -o mynetworks=127.0.0.0/8
    -o smtpd_data_restrictions=
    -o smtpd_end_of_data_restrictions=
    -o local_header_rewrite_clients=
    -o smtpd_error_sleep_time=0
    -o smtpd_soft_error_limit=1001
    -o smtpd_hard_error_limit=1000
    -o smtpd_client_connection_count_limit=0
    -o smtpd_client_connection_rate_limit=0
    -o smtpd_milters=
    -o local_recipient_maps=
    -o relay_recipient_maps=
    -o receive_override_options=no_header_body_checks,no_unknown_recipient_checks,no_address_mappings
1
  • Thanks for this answer. Just wanted to point out that your master.cf stanza is missing a "=" after smtpd_data_restrictions. This will break the config. Nov 2, 2019 at 15:31
2

This is an old question but I thought I would share some configuration that allows use of an outbound Procmail filter. Firstly, in master.cf:

submission inet n       -       n       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=may
  -o smtpd_sasl_auth_enable=yes
  -o broken_sasl_auth_clients=yes
  -o smtpd_client_restrictions=permit_mynetworks,permit_sasl_authenticated,reject
  -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
  -o content_filter=procmail-outbound

procmail-outbound unix  -       n       n       -       -       pipe
  flags=Rq user=mail argv=/usr/bin/procmail -t -m SENDER=${sender} RECIPIENT=${recipient} /etc/procmail/outbound.rc

and then you can run whatever procmail recipes you need in outbound.rcbefore reinjecting the processed message with sendmail like the below example recipe does:

# Send mail
:0 w
| /usr/bin/sendmail -G -i -f $SENDER $RECIPIENT

(Note the example accepts mail on the submission service (port 587) rather than the smtp service which is for relaying and delivery.)

1

Your clients send mail using an smtp server - presumably that is this postfix server.

Outgoing mail gets passed through Postfix's smtp transport, and the config above is passing that all through amavisd via the content_filter - so I think your outbound mail is getting processed already.

I suggest you test to satisfy yourself whether this is the case.

If the server is not very busy you tail your maillog, and watch what happens when you send a test message containing the GTUBE string (XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X). Amavisd/spamassassin should catch the message. If so then you know your mail is being scanned on the way out.

2
  • The GTUBE string you pasted is corrupted by some of the characters being interpreted as formatting.
    – ruief
    Dec 30, 2016 at 22:09
  • backtick to the rescue
    – dunxd
    Jan 5, 2017 at 12:53
0

If you define content_filter in your main.cf, the filter will be applied on any inbound and outbound interfaces (smtp, smtps, submission).

content_filter=clamav:{{ HOST_ALIAS_CLAMAV }}:{{ CLAMAV_SERVER_PORT }}

Make sure that you set -o content_filter= on the clamav process section to not get an infinite loop.

0.0.0.0:{{ CLAMAV_FORWARD_PORT }} inet  n -       n       -       16      smtpd
    -o content_filter=
    ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .