[Mimedefang] MX resolves to localhost

Damrose, Mark mdamrose at elgin.edu
Mon Aug 16 18:09:30 EDT 2004


> -----Original Message-----
> From: Mike Batchelor [mailto:mikebat at gmail.com]


> I want to reject mail for domains that resolve to 127.0.0.1 or that
> have MX records with hostnames that resolve to 127.0.0.1.  I have tons
> of double bounces due to "MX loops back to me" because the damn sender
> domain resolves to localhost.  Any ideas how to proceed?  Does
> mimedefang provide any functions along these lines, or do I need to
> dig (haha!) into Net::DNS to do this?

WARNING, I just threw this together.
It's not fully tested, although the queries are pretty much direct 
copies from man Net::DNS.
I have no idea what this will do to load on a real server.

sub local_get_mx($) {
#
# Procedure local_get_mx
# arguments:
#   $name: either a domain name or an e-mail address
# returns: 
#   array of IP addresses of all MX servers for a 
#   domain.
#   If there are no MX records, will return IP 
#   addresses of domain
#   If there are no MX or A records, and/or domain
#   doesn't exist, will return an empty array.

    use Net::DNS;

    my ($name) = @_;

    $name =~ s/^.*\@//;
    $name =~ s/\>$//;

    my $res  = Net::DNS::Resolver->new;
    my @mx   = mx($res, $name);
    my @mxservers;
    my @mxip;

    if (@mx) {
        foreach $rr (@mx) {
            my $mxs = $rr->exchange;
            push( @mxservers, $mxs);
        }
    } else {
        if ($res->errorstring eq "NOERROR") {
            push ( @mxservers, $name);
        }
    }

    foreach $mh (@mxservers) {
        my $query = $res->send($mh);

        if ($query) {
            foreach my $rr ($query->answer) {
                next unless $rr->type eq "A";
                push(@mxip, $rr->address);
            }
        }

    }

    return @mxip;
}



# An example of how to use:

my @mxhosts = local_get_mx($Sender);
foreach $mxh (@mxhosts) {
    if ( $mxh eq "127.0.0.1") {
        # code to run when it matches goes here
    } else {
        # code to run when it doesn't match goes here
    }
}



More information about the MIMEDefang mailing list