[Mimedefang] Rejecting Some Recipients in filter_recipient() for Relay

imacat imacat at mail.imacat.idv.tw
Fri Mar 23 11:29:18 EDT 2007


On Fri, 23 Mar 2007 10:14:37 -0400
Kris Deugau <kdeugau at vianet.ca> wrote:
> imacat wrote:
> >     The gateway has rejected the 2 recipients, but it still ask the
> > intranet mail server for these 2 rejected recipients.  Why?
> Post your filter_recipient;  it sounds like you're not doing something 
> correctly.  I've used this on several systems without trouble.

    My filter_recipient() is attached below.  /etc/mail/local-users is
the available local users and aliases' list I synchronized from the
intranet every hour.  The /etc/mail/access has some problem with
recipient filtering so I have to filter them here.  Net::DNS and
Net::IPv4Addr is for my other tests.

    I suppose there shouldn't be any trouble here, since all it want is
a proper return list, and I did not mess up with global variables.

    Thank you very much in advance.

use Net::DNS qw();
use Email::Address qw();
use Net::IPv4Addr qw(ipv4_in_network);
# filter_recipient: Filter by the RCPT_TO: smtp talk
# See mimedefang-filter(5)
sub filter_recipient($$$$$$$$$) {
    local ($_, %_);
    my ($recipient, $sender, $ip, $hostname, $first, $helo, $rcpt_mailer, $rcpt_host, $rcpt_addr);
    my ($addr, $file, $FH, %locals, %denies);
    ($recipient, $sender, $ip, $hostname, $first, $helo, $rcpt_mailer, $rcpt_host, $rcpt_addr) = @_;
    %locals = qw();
    %denies = qw();
    foreach my $addr (Email::Address->parse($recipient)) {
        # Our targets are Pristine's local recipients
        next unless $addr->host =~ /(?<![^\.])pristine\.(?:com\.tw|net)$/i;
        # Read the local recipients' list if not read yet
        if (scalar(keys %locals) == 0) {
            $file = "/etc/mail/local-users";
            # Only work if the local recipients' list exists
            if (!-e $file) {
                md_syslog("warning", "filter_recipient(): $file: no such file or directory.");
                return ("CONTINUE", "ok");
            }
            if (!-f $file) {
                md_syslog("warning", "filter_recipient(): $file: not a file.");
                return ("CONTINUE", "ok");
            }
            if (!-r $file) {
                md_syslog("warning", "filter_recipient(): $file: permission denied.");
                return ("CONTINUE", "ok");
            }
            if (!open $FH, $file) {
                md_syslog("err", "filter_recipient(): $file: $!.");
                return ("CONTINUE", "ok");
            }
            %locals = map { lc $_ => 1 } split /\n/, join "", <$FH>;
            if (!close $FH) {
                md_syslog("err", "filter_recipient(): $file: $!.");
                return ("CONTINUE", "ok");
            }
        }
        # Check against the local recipients' list
        if (!exists $locals{lc $addr->user}) {
            # The reason is obvious that additional logging is not necessary
            return ("REJECT", "User unknown", 550, "5.1.1", 0);
        }
        # Check against the deny list
        # Read the deny list if not read yet
        if (scalar(keys %denies) == 0) {
            $file = "/etc/mail/access";
            # Only work if the access list exists
            return ("CONTINUE", "ok") unless -e $file && -f $file && -r $file;
            if (!open $FH, $file) {
                md_syslog("err", "filter_recipient(): $file: $!.");
                return ("CONTINUE", "ok");
            }
            %denies = map { lc $_ => 1 } grep s/^To:([^\t\@]+)\@\t+REJECTSPAM$/$1/, split /\n/, join "", <$FH>;
            if (!close $FH) {
                md_syslog("err", "filter_recipient(): $file: $!.");
                return ("CONTINUE", "ok");
            }
        }
        # Check against the deny list
        if (exists $denies{lc $addr->user}) {
            # The reason is obvious that additional logging is not necessary
            return ("REJECT", "User unknown", 550, "5.1.1", 0);
        }
    }
    # OK
    return ("CONTINUE", "ok");
}

--
Best regards,
imacat ^_*' <imacat at mail.imacat.idv.tw>
PGP Key: http://www.imacat.idv.tw/me/pgpkey.txt

<<Woman's Voice>> News: http://www.wov.idv.tw/
Tavern IMACAT's: http://www.imacat.idv.tw/
TLUG List Manager: http://lists.linux.org.tw/cgi-bin/mailman/listinfo/tlug
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
URL: <https://lists.mimedefang.org/pipermail/mimedefang_lists.mimedefang.org/attachments/20070323/5fa3920e/attachment.sig>


More information about the MIMEDefang mailing list