procmail
[Top] [All Lists]

Re: Filtering outbound email

1999-03-29 19:24:02
"John D. Hardin" <jhardin(_at_)wolfenet(_dot_)com> writes:
Has anybody got any pointers on using procmail to filter outbound
email? The sendmail rule offered on the man page presumes you know
which domains you want to filter for (unless I'm missing something
obvious).

The general idea is to have a pseudo-domain, say, ".filtered", whereby
if a message is sent to someone(_at_)somewhere(_dot_)filtered, the ".filtered"
will be stripped and the message sent to the resulting address, while
receipients without such a tacked-on domain will have it tacked on and
then be thrown to a mailer that'll act as a filter by eventually
refeeding the message back into sendmail.

When that filtering mailer is the procmail mailer, procmail is invoked
with the -m flag and the specified procmailrc will generally start
with:
        SENDER = $1
        SHIFT = 1

(The envelope sender is passed as the first argument to the procmail
mailer) and finish with the recipe:

        :0
        ! -f "$SENDER" "$@"

So, a message sent to "guenther(_at_)gac(_dot_)edu" would go through the 
following
stages:

    sendmail -f sender guenther(_at_)gac(_dot_)edu
        no trailing ".filtered" -> send to procmail mailer:
    procmail -m /etc/procmailrc/foo sender 
guenther(_at_)gac(_dot_)edu(_dot_)filtered
        process, resend via sendmail
    sendmail -f sender guenther(_at_)gac(_dot_)edu(_dot_)filtered
        has trailing ".filtered" -> strip and treat normally

Yes, each message would require the initial sendmail plus two processes
(one procmail, one sendmail) for each recipient.  With some careful
procmail and sendmail configuration you could trim that to three, but
it's not easy to get right.


A minimal blob of sendmail m4 config code (for a .mc file) would look
something like:

LOCAL_CONFIG
# If an address has this pseudo-domain on it, it won't be filtered
CPfiltered

# What's the mailer:host of the filter?
# (Probably procmail:/etc/procmailrcs/something)
D{filter} procmail:/etc/procmailrcs/filter

LOCAL_RULE_0
# Strip the pseudo-domain, reparse and continue
R$*<$*.filtered.>$*             $@ $>Parse0 $>3 $1<$2>$3
# No pseudo-domain? send to the filtering mailer
R$*<$*.>$*                      $: $>95 <${filter}> $1<$2.filtered>$3
R$*<$*>$*                       $: $>95 <${filter}> $1<$2.filtered>$3


That has at least one defect -- it doesn't interact correctly with
other LOCAL_RULE_0 items -- but it should serve as a base to work
from.


Philip Guenther

<Prev in Thread] Current Thread [Next in Thread>