#!/usr/bin/env perl

#
# Check if your mail-servers are listed in an RBL
# For use with "mon".
#
# rbl.monitor host [...host]
#
#
# For each host listed, it performs a DNS lookup against each 
# RBL domain (eg SpamCop, etc). 
# If the result for any lookup contains `127' (ie 127.0.0.n) 
# an error exit code is raised.
#
# Tim Haynes
# OpenLink Software
# http://www.openlinksw.com/
# thaynes+mon@openlinksw.co.uk
#
# Copyright (C) 1998, OpenLink Software
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

use Socket;
use Getopt::Long;
use English;

@sources=("bl.spamcop.net", "relays.mail-abuse.org", 
          "sbl.spamhaus.org", "xbl.spamhaus.org",
          "spam.dnsbl.sorbs.net"
    );

exit 0 if !ARGV;

$out="";
$errs=0;

foreach my $host (@ARGV) {
    foreach my $src (@sources) {
        $out=rblcheck($host, $src);
        $errs++ if $out=~/127/;
        $detail.="Looking up $host against $src : $out\n";
    }
}

print "$detail\n";

exit $errs;


sub rblcheck {
    my ($host, $s)=@_;
    my $h=$host;
    $h=join(".", reverse(unpack("C4", gethostbyname($h))));
    $r=join(".", unpack("C4", inet_aton("$h.$s")));
    return $r;
}
