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:
Centos 4.3 http://centos.org
Zimbra 3.1.3 http://www.zimbra.com
putmail.py 1.3 http://putmail.sourceforge.net/home.html
python
grep
A wordlist of regular expression to Ban
filter.sh – described below
Zimbra was installed into a default installation of Centos 4.3
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.
Code:
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:
Code:
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:
Code:
mkdir /opt/zimbra/.putmail
chown zimbra:zimbra /opt/zimbra/.putmail
add the configuration file to this directory:
Code:
jmacs /opt/zimbra/.putmail/putmailrc
and make sure it contains the port number specified in the postfix master.cf file:
Code:
[config]
server = localhost
email = amin@your.org
port = 10026
We need to configure putmail, and edit the putmail program to tell it where its configuration is located:
Code:
emacs /usr/local/sbin/putmail.py
make sure this line points to the configuration home directory:
Code:
HOME_DIR = '/opt/zimbra/'
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:
Code:
\<badword\>
\<badword1\>
\<badword1
\<badword2\>
\<badword2
Call this file 'words' and place it into a configuration directory
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.
Code:
!/bin/sh
# Simple shell-based filter. It is meant to be invoked as follows:
# /path/to/script -f sender recipients...
# 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
# Exit codes from <sysexits.h>
EX_TEMPFAIL=75
EX_UNAVAILABLE=69
# Clean up when done or when aborting.
trap "rm -f in.$$" 0 1 2 3 15
#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; }
# 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 $?
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.
Code:
cd /opt/zimbra/postfix/conf
cp master.cf master.cf.orig
emacs master.cf
add this line (to the bit after maildrop, although I'm not sure if its position in the config makes any difference)
Code:
filter unix - n n - - pipe
flags=Rq user=zimbra argv=/usr/local/sbin/filter -f ${sender} -- ${recipient} zimbra
Navigate to this line and add the content filter named filter to the option -o content_filter=
Code:
127.0.0.1:10025 inet n - n - - smtpd
-o content_filter=filter:dummy Then add the following to the end of the file:
Code:
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)
Code:
su – zimbra
zmcontrol stop
zmcontrol start
job done.
Further reading (adding disclaimers etc):
http://www.postfix.org/FILTER_README.html
http://www.zimbra.com/forums/showthr...2&page=2&pp=10
http://putmail.sourceforge.net/home.html