2012-04-29 10:35:59 +02:00
|
|
|
#!perl -w
|
2004-08-01 03:54:16 +02:00
|
|
|
|
2012-05-04 20:02:12 +02:00
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
rhsbl - handle RHSBL lookups
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
Pluging that checks the host part of the sender's address against a
|
|
|
|
configurable set of RBL services.
|
|
|
|
|
|
|
|
=head1 CONFIGURATION
|
|
|
|
|
|
|
|
This plugin reads the lists to use from the rhsbl_zones configuration
|
|
|
|
file. Normal domain based dns blocking lists ("RBLs") which contain TXT
|
|
|
|
records are specified simply as:
|
|
|
|
|
|
|
|
dsn.rfc-ignorant.org
|
|
|
|
|
|
|
|
To configure RBL services which do not contain TXT records in the DNS,
|
|
|
|
but only A records, specify, after a whitespace, your own error message
|
|
|
|
to return in the SMTP conversation e.g.
|
|
|
|
|
|
|
|
abuse.rfc-ignorant.org does not support abuse@domain
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
2012-06-03 03:44:46 +02:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Qpsmtpd::Constants;
|
2012-05-04 20:02:12 +02:00
|
|
|
|
2009-07-20 12:59:32 +02:00
|
|
|
sub register {
|
2012-06-23 09:10:48 +02:00
|
|
|
my ($self, $qp ) = (shift, shift);
|
|
|
|
|
|
|
|
my $denial;
|
|
|
|
if ( @_ == 1 ) {
|
|
|
|
$denial = shift;
|
|
|
|
if ( defined $denial && $denial =~ /^disconnect$/i ) {
|
|
|
|
$self->{_args}{reject_type} = 'disconnect';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->{_args}{reject_type} = 'perm';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->{_args} = { @_ };
|
|
|
|
};
|
2012-06-26 07:03:00 +02:00
|
|
|
|
|
|
|
if ( ! defined $self->{_args}{reject} ) {
|
|
|
|
$self->{_args}{reject} = 1;
|
|
|
|
};
|
|
|
|
$self->{_args}{reject_type} ||= 'perm';
|
2009-07-20 12:59:32 +02:00
|
|
|
}
|
2006-03-09 13:37:25 +01:00
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_mail {
|
2012-06-23 09:10:48 +02:00
|
|
|
my ($self, $transaction, $sender, %param) = @_;
|
2004-08-01 03:54:16 +02:00
|
|
|
|
2012-06-03 03:44:46 +02:00
|
|
|
return DECLINED if $self->is_immune();
|
|
|
|
|
|
|
|
if ($sender->format eq '<>') {
|
2012-06-23 09:10:48 +02:00
|
|
|
$self->log(LOGINFO, 'pass, null sender');
|
2012-06-03 03:44:46 +02:00
|
|
|
return DECLINED;
|
|
|
|
};
|
|
|
|
|
2012-06-23 09:10:48 +02:00
|
|
|
my %rhsbl_zones = $self->populate_zones() or return DECLINED;
|
2004-08-01 03:54:16 +02:00
|
|
|
|
2012-06-23 09:10:48 +02:00
|
|
|
my $res = $self->init_resolver();
|
2004-08-01 03:54:16 +02:00
|
|
|
|
2012-06-23 09:10:48 +02:00
|
|
|
my @hosts = $sender->host;
|
2004-08-01 03:54:16 +02:00
|
|
|
for my $host (@hosts) {
|
2012-06-23 09:10:48 +02:00
|
|
|
for my $rhsbl (keys %rhsbl_zones) {
|
|
|
|
my $query;
|
|
|
|
# fix to find TXT records, if the rhsbl_zones line doesn't have second field
|
|
|
|
if (defined($rhsbl_zones{$rhsbl})) {
|
|
|
|
$self->log(LOGDEBUG, "Checking $host.$rhsbl for A record");
|
|
|
|
$query = $res->query("$host.$rhsbl");
|
|
|
|
} else {
|
|
|
|
$self->log(LOGDEBUG, "Checking $host.$rhsbl for TXT record");
|
|
|
|
$query = $res->query("$host.$rhsbl", 'TXT');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $query) {
|
|
|
|
if ( $res->errorstring ne 'NXDOMAIN' ) {
|
|
|
|
$self->log(LOGCRIT, "query failed: ", $res->errorstring);
|
|
|
|
};
|
|
|
|
next;
|
|
|
|
};
|
|
|
|
|
|
|
|
my $result;
|
|
|
|
foreach my $rr ($query->answer) {
|
|
|
|
$self->log(LOGDEBUG, 'got an ' . $rr->type . ' record ' . $rr->name);
|
|
|
|
if ($rr->type eq 'A') {
|
|
|
|
$self->log(LOGDEBUG, "A record found for $result with IP " . $rr->address);
|
|
|
|
$result = $rr->name;
|
|
|
|
}
|
|
|
|
elsif ($rr->type eq 'TXT') {
|
|
|
|
$result = $rr->txtdata;
|
|
|
|
$self->log(LOGDEBUG, "TXT record found: " . $rr->txtdata);
|
|
|
|
};
|
|
|
|
|
2012-06-26 07:03:00 +02:00
|
|
|
next if ! $result;
|
2012-06-23 09:10:48 +02:00
|
|
|
|
2012-06-26 07:03:00 +02:00
|
|
|
$self->log(LOGINFO, "fail, $result");
|
2012-06-23 09:10:48 +02:00
|
|
|
|
2012-06-26 07:03:00 +02:00
|
|
|
my $host = $transaction->sender->host;
|
|
|
|
if ($result =~ /^$host\./ ) {
|
|
|
|
return $self->get_reject( "Mail from $host rejected because it $result" );
|
2012-06-23 09:10:48 +02:00
|
|
|
};
|
2012-06-26 07:03:00 +02:00
|
|
|
|
|
|
|
my $hello = $self->qp->connection->hello_host;
|
|
|
|
return $self->get_reject( "Mail from HELO $hello rejected because it $result" );
|
2012-06-23 09:10:48 +02:00
|
|
|
}
|
|
|
|
}
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
|
|
|
|
2012-06-03 03:44:46 +02:00
|
|
|
$self->log(LOGINFO, "pass");
|
|
|
|
return DECLINED;
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
|
|
|
|
2012-06-23 09:10:48 +02:00
|
|
|
sub populate_zones {
|
|
|
|
my $self = shift;
|
2004-08-01 03:54:16 +02:00
|
|
|
|
2012-06-23 09:10:48 +02:00
|
|
|
my %rhsbl_zones
|
|
|
|
= map { (split /\s+/, $_, 2)[0,1] }
|
|
|
|
$self->qp->config('rhsbl_zones');
|
2004-08-01 03:54:16 +02:00
|
|
|
|
2012-06-23 09:10:48 +02:00
|
|
|
if ( ! keys %rhsbl_zones ) {
|
|
|
|
$self->log(LOGINFO, 'pass, no zones');
|
|
|
|
return;
|
|
|
|
};
|
2004-08-01 03:54:16 +02:00
|
|
|
|
2012-06-23 09:10:48 +02:00
|
|
|
return %rhsbl_zones;
|
|
|
|
};
|
|
|
|
|
|
|
|
sub init_resolver {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{_resolver} if $self->{_resolver};
|
|
|
|
$self->log( LOGDEBUG, "initializing Net::DNS::Resolver");
|
|
|
|
$self->{_resolver} = Net::DNS::Resolver->new(dnsrch => 0);
|
|
|
|
my $timeout = $self->{_args}{timeout} || 8;
|
|
|
|
$self->{_resolver}->tcp_timeout($timeout);
|
|
|
|
$self->{_resolver}->udp_timeout($timeout);
|
|
|
|
return $self->{_resolver};
|
|
|
|
};
|
2008-06-02 17:41:08 +02:00
|
|
|
|