Zimbra content filter

From Wiki

Revision as of 20:04, 6 December 2009 by MicrodigitUK (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to:navigation, search

I just got this working, any improvements gratefully received. (this is internal documentation but I thought I'd share :)

This howto was specifically written for a Zimbra installation but should work on any postfix server.

It is necessary for us to filter bad words from any email that has been sent internally or externally.

The plan is to use postfix after queue content filter to run a script that extracts email from the queue, process the email then injects it back into the queue for delivery. We will be using grep to check the email against a wordlist of regular expressions. We will avoid email loops by using putmail.py to re-inject the email into the mail queue.

The following software was used:

Zimbra was installed into a default installation of Centos 5.4 64bit

A directory must be created to spool the mail to whilst it is being processed. Here we will make it in the Zimbra directory, /var/spool/filter would be another sensible option.

We will make zimbra the owner of this directory and make sure it has read write execute access.

On a non zimbra install another non-root user should be created to run the script.

mkdir /opt/zimbra/postfix/spool/filter
chown zimbra:zimbra /opt/zimbra/postfix/spool/filter
chmod 700 /opt/zimbra/postfix/spool/filter


We will copy the putmail.py program to /usr/local/sbin and make sure the zimbra user can access it:

cp putmail.py /usr/local/sbin
chown zimbra /usr/local/sbin/putmail.py
chmod 500 /usr/local/sbin/putmail.py


Create the putmail configuration home directory:

mkdir /opt/zimbra/.putmail
chown zimbra:zimbra /opt/zimbra/.putmail


Add the configuration file to this directory:

nano /opt/zimbra/.putmail/putmailrc


and make sure it contains the port number specified in the postfix master.cf file:

<ini> [config] server = localhost email = admin@example.com port = 10026 </ini>

We need to configure putmail, and edit the putmail program to tell it where its configuration is located:

nano /usr/local/sbin/putmail.py


make sure this line points to the configuration home directory:

<python> HOME_DIR = '/opt/zimbra/' </python>

You now need to make a word list file containing the bad language, this is the most fun part of the entire exercise, I recommend doing this over a few beers. Your wordlist should contain the prohibited words in the following format:

\<badword\>
\<badword1\>
\<badword1 
\<badword2\>
\<badword2

Have a look here for some good words to give you a head-start: http://www.noswearing.com/list.php

Call this file 'words' and place it into a configuration directory

cp words /etc/


We now need to create and edit the script that ties it all together, probably you can copy it from here. This script was modified from the postfix website. Check that INSPECT_DIR points to your filter directory, SENDMAIL points to your putmail.py program, BADWORDS points to the location of your words file and that MAILADMIN is set to the email address of the administrator.

<bash>

  1. !/bin/sh
  1. Simple shell-based filter. It is meant to be invoked as follows:
  2. /path/to/script -f sender recipients...
  3. Localize these. The -G option does nothing before Postfix 2.3.

INSPECT_DIR=/opt/zimbra/postfix/spool/filter SENDMAIL=/usr/local/sbin/putmail.py MAILADMIN=email@address.of.your.org BADWORDS=/etc/words

  1. Exit codes from <sysexits.h>

EX_TEMPFAIL=75 EX_UNAVAILABLE=69

  1. Clean up when done or when aborting.

trap "rm -f in.$$" 0 1 2 3 15

  1. Start processing.

cd $INSPECT_DIR || { echo $INSPECT_DIR does not exist; exit $EX_TEMPFAIL; } cat >in.$$ || { echo Cannot save mail to file; exit $EX_TEMPFAIL; }

  1. Specify your content filter here.

cat in.$$ | while read line; do echo $line|grep -v 'Content-Disposition: attachment;' || break; done | /bin/grep -if /etc/words && { echo ": The message content has been rejected, your actions have been reported" ; $SENDMAIL ${@% -- *} $MAILADMIN <in.$$ ; exit $EX_UNAVAILABLE; }

$SENDMAIL "$@" <in.$$ exit $? </bash>

If you are using Zimbra version 5.0.12+, do not modify master.cf - instead you will need to change master.cf.in (same directory)

Now we need to adapt the postfix master.cf file to use the ammendments that we have just made. On a default zimbra install it lives in /opt/zimbra/postfix/conf/master.cf


Backup the original master.cf file then edit it.

cd /opt/zimbra/postfix/conf
cp master.cf master.cf.orig
nano master.cf


Add this line (to the bit after maildrop, although I'm not sure if its position in the config makes any difference)

filter    unix  -       n       n       -       -      pipe
        flags=Rq user=zimbra argv=/usr/local/sbin/filter -f ${sender} -- ${recipient}

Navigate to this line and add the content filter named filter to the option -o content_filter=

127.0.0.1:10025 inet n  -       n       -       -  smtpd
                  -o content_filter=filter:dummy

Then add the following to the end of the file:

127.0.0.1:10026 inet n  -       n       -       -  smtpd
        -o content_filter=
        -o smtpd_authorized_xforward_hosts=127.0.0.0/8
        -o local_recipient_maps=
        -o virtual_mailbox_maps=
        -o virtual_alias_maps=
        -o relay_recipient_maps=
        -o smtpd_restriction_classes=
        -o smtpd_delay_reject=no
        -o smtpd_client_restrictions=permit_mynetworks,reject
        -o smtpd_helo_restrictions=
        -o smtpd_sender_restrictions=
        -o smtpd_recipient_restrictions=permit_mynetworks,reject
        -o mynetworks_style=host
        -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=1000
        -o smtpd_client_connection_count_limit=0
        -o smtpd_client_connection_rate_limit=0
        -o receive_override_options=no_header_body_checks,no_unknown_recipient_checks,no_address_mappings


Full File:


#
# Postfix master process configuration file.  For details on the format
# of the file, see the Postfix master(5) manual page.
#
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
smtp      inet  n       -       n       -       -       smtpd
465    inet  n       -       n       -       -       smtpd
  -o smtpd_tls_wrappermode=yes 
  -o smtpd_sasl_auth_enable=yes
submission inet n      -       n       -       -       smtpd
	-o smtpd_etrn_restrictions=reject
	-o smtpd_sasl_auth_enable=%%zimbraMtaSaslAuthEnable%%
	-o smtpd_client_restrictions=permit_sasl_authenticated,reject
	-o smtpd_tls_security_level=%%zimbraMtaTlsSecurityLevel%%
pickup    fifo  n       -       n       60      1       pickup
cleanup   unix  n       -       n       -       0       cleanup
qmgr      fifo  n       -       n       300     1       qmgr
tlsmgr    unix  -       -       n       1000?   1       tlsmgr
rewrite   unix  -       -       n       -       -       trivial-rewrite
bounce    unix  -       -       n       -       0       bounce
defer     unix  -       -       n       -       0       bounce
trace     unix  -       -       n       -       0       bounce
verify    unix  -       -       n       -       1       verify
flush     unix  n       -       n       1000?   0       flush
proxymap  unix  -       -       n       -       -       proxymap
smtp      unix  -       -       n       -       -       smtp
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
relay     unix  -       -       n       -       -       smtp
	-o fallback_relay=
showq     unix  n       -       n       -       -       showq
error     unix  -       -       n       -       -       error
retry     unix  -       -       n       -       -       error
discard   unix  -       -       n       -       -       discard
local     unix  -       n       n       -       -       local
virtual   unix  -       n       n       -       -       virtual
lmtp      unix  -       -       n       -       -       lmtp
anvil     unix  -       -       n       -       1       anvil
scache	  unix	-	-	n	-	1	scache
%%uncomment LOCAL:postfix_enable_smtpd_policyd%%policy    unix  -       n       n       -       0       spawn
%%uncomment LOCAL:postfix_enable_smtpd_policyd%%	user=zimbra argv=/usr/bin/perl /opt/zimbra/libexec/zmpostfixpolicyd
#
# ====================================================================
# 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/local/bin/maildrop -d ${recipient}
#
######################################   DJ ######################################

filter    unix  -       n       n       -       -      pipe
        flags=Rq user=zimbra argv=/usr/local/sbin/filter -f ${sender} -- ${recipient}

#####################################    DJ ######################################

# The Cyrus deliver program has changed incompatibly, multiple times.
#
old-cyrus unix  -       n       n       -       -       pipe
  flags=R user=cyrus argv=/cyrus/bin/deliver -e -m ${extension} ${user}
# 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}
#
# 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=foo argv=/usr/local/sbin/bsmtp -f $sender $nexthop $recipient
#
# AMAVISD-NEW
#
smtp-amavis unix -      -       n       -       10  smtp
	-o smtp_data_done_timeout=1200
	-o smtp_send_xforward_command=yes
	-o disable_dns_lookups=yes
	-o max_use=20
127.0.0.1:10025 inet n  -       n       -       -  smtpd
	-o content_filter=filter:dummy
	-o local_recipient_maps=
	-o virtual_mailbox_maps=
	-o virtual_alias_maps=
	-o relay_recipient_maps=
	-o smtpd_restriction_classes=
	-o smtpd_delay_reject=no
	-o smtpd_client_restrictions=permit_mynetworks,reject
	-o smtpd_helo_restrictions=
  -o smtpd_milters=
	-o smtpd_sender_restrictions=
	-o smtpd_recipient_restrictions=permit_mynetworks,reject
	-o mynetworks_style=host
	-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=1000
	-o smtpd_client_connection_count_limit=0
	-o smtpd_client_connection_rate_limit=0
	-o receive_override_options=no_header_body_checks,no_unknown_recipient_checks,no_address_mappings
127.0.0.1:10026 inet n  -       n       -       -  smtpd
        -o content_filter=
        -o smtpd_authorized_xforward_hosts=127.0.0.0/8
        -o local_recipient_maps=
        -o virtual_mailbox_maps=
        -o virtual_alias_maps=
        -o relay_recipient_maps=
        -o smtpd_restriction_classes=
        -o smtpd_delay_reject=no
        -o smtpd_client_restrictions=permit_mynetworks,reject
        -o smtpd_helo_restrictions=
        -o smtpd_sender_restrictions=
        -o smtpd_recipient_restrictions=permit_mynetworks,reject
        -o mynetworks_style=host
        -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=1000
        -o smtpd_client_connection_count_limit=0
        -o smtpd_client_connection_rate_limit=0
        -o receive_override_options=no_header_body_checks,no_unknown_recipient_checks,no_address_mappings

Restart Zimbra (or postfix)

# su - zimbra
$ zmcontrol stop
$ zmcontrol start


When upgrading Zimbra, the master.cf file is overwritten. It must be replaced after an upgrade of the content filter will fail. You may also need to re-create the postfix spool directory.

Job done.

Further reading (adding disclaimers etc):

http://www.postfix.org/FILTER_README.html

http://www.zimbra.com/forums/showthread.php?t=132&page=2&pp=10

http://putmail.sourceforge.net/home.html