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
|
|
|
|
|
|
|
|
|
2009-07-20 12:59:32 +02:00
|
|
|
sub register {
|
|
|
|
my ($self, $qp, $denial ) = @_;
|
|
|
|
if ( defined $denial and $denial =~ /^disconnect$/i ) {
|
|
|
|
$self->{_rhsbl}->{DENY} = DENY_DISCONNECT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->{_rhsbl}->{DENY} = DENY;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2006-03-09 13:37:25 +01:00
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_mail {
|
2006-04-07 20:58:02 +02:00
|
|
|
my ($self, $transaction, $sender, %param) = @_;
|
2004-08-01 03:54:16 +02:00
|
|
|
|
|
|
|
my $res = new Net::DNS::Resolver;
|
|
|
|
my $sel = IO::Select->new();
|
|
|
|
my %rhsbl_zones_map = ();
|
|
|
|
|
2006-04-07 20:58:02 +02:00
|
|
|
# Perform any RHS lookups in the background. We just send the query packets
|
|
|
|
# here and pick up any results in the RCPT handler.
|
2002-07-15 14:16:10 +02:00
|
|
|
# MTAs gets confused when you reject mail during MAIL FROM:
|
2004-08-01 03:54:16 +02:00
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
my %rhsbl_zones = map { (split /\s+/, $_, 2)[0,1] } $self->qp->config('rhsbl_zones');
|
2004-08-01 03:54:16 +02:00
|
|
|
|
|
|
|
if ($sender->format ne '<>' and %rhsbl_zones) {
|
|
|
|
push(my @hosts, $sender->host);
|
2005-03-24 20:17:46 +01:00
|
|
|
#my $helo = $self->qp->connection->hello_host;
|
|
|
|
#push(@hosts, $helo) if $helo && $helo ne $sender->host;
|
2004-08-01 03:54:16 +02:00
|
|
|
for my $host (@hosts) {
|
2002-07-08 04:30:11 +02:00
|
|
|
for my $rhsbl (keys %rhsbl_zones) {
|
2006-03-09 13:37:25 +01:00
|
|
|
# fix to find TXT records, if the rhsbl_zones line doesn't have second field
|
|
|
|
if (defined($rhsbl_zones{$rhsbl})) {
|
2004-08-01 03:54:16 +02:00
|
|
|
$self->log(LOGDEBUG, "Checking $host.$rhsbl for A record in the background");
|
|
|
|
$sel->add($res->bgsend("$host.$rhsbl"));
|
2006-03-09 13:37:25 +01:00
|
|
|
} else {
|
|
|
|
$self->log(LOGDEBUG, "Checking $host.$rhsbl for TXT record in the background");
|
|
|
|
$sel->add($res->bgsend("$host.$rhsbl", "TXT"));
|
|
|
|
}
|
2004-08-01 03:54:16 +02:00
|
|
|
$rhsbl_zones_map{"$host.$rhsbl"} = $rhsbl_zones{$rhsbl};
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
|
|
|
}
|
2004-08-01 03:54:16 +02:00
|
|
|
|
|
|
|
%{$self->{_rhsbl_zones_map}} = %rhsbl_zones_map;
|
|
|
|
$transaction->notes('rhsbl_sockets', $sel);
|
|
|
|
} else {
|
|
|
|
$self->log(LOGDEBUG, 'no RHS checks necessary');
|
|
|
|
}
|
|
|
|
|
2002-09-08 16:15:21 +02:00
|
|
|
return DECLINED;
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_rcpt {
|
2002-07-08 04:30:11 +02:00
|
|
|
my ($self, $transaction, $rcpt) = @_;
|
2004-08-01 03:54:16 +02:00
|
|
|
my $host = $transaction->sender->host;
|
|
|
|
my $hello = $self->qp->connection->hello_host;
|
|
|
|
|
|
|
|
my $result = $self->process_sockets;
|
|
|
|
if ($result && defined($self->{_rhsbl_zones_map}{$result})) {
|
|
|
|
if ($result =~ /^$host\./ ) {
|
2009-07-20 12:59:32 +02:00
|
|
|
return ($self->{_rhsbl}->{DENY}, "Mail from $host rejected because it " . $self->{_rhsbl_zones_map}{$result});
|
2004-08-01 03:54:16 +02:00
|
|
|
} else {
|
2009-07-20 12:59:32 +02:00
|
|
|
return ($self->{_rhsbl}->{DENY}, "Mail from HELO $hello rejected because it " . $self->{_rhsbl_zones_map}{$result});
|
2004-08-01 03:54:16 +02:00
|
|
|
}
|
|
|
|
}
|
2009-07-20 12:59:32 +02:00
|
|
|
return ($self->{_rhsbl}->{DENY}, $result) if $result;
|
2002-07-08 04:30:11 +02:00
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
2004-08-01 03:54:16 +02:00
|
|
|
sub process_sockets {
|
|
|
|
my ($self) = @_;
|
|
|
|
my $trans = $self->transaction;
|
|
|
|
my $result = '';
|
|
|
|
|
|
|
|
return $trans->notes('rhsbl') if $trans->notes('rhsbl');
|
|
|
|
|
|
|
|
my $res = new Net::DNS::Resolver;
|
|
|
|
my $sel = $trans->notes('rhsbl_sockets') or return '';
|
2002-07-08 04:30:11 +02:00
|
|
|
|
2004-08-01 03:54:16 +02:00
|
|
|
$self->log(LOGDEBUG, 'waiting for rhsbl dns');
|
2002-07-08 04:30:11 +02:00
|
|
|
|
2004-08-01 03:54:16 +02:00
|
|
|
# don't wait more than 8 seconds here
|
|
|
|
my @ready = $sel->can_read(8);
|
2002-07-08 04:30:11 +02:00
|
|
|
|
2004-08-01 03:54:16 +02:00
|
|
|
$self->log(LOGDEBUG, 'DONE waiting for rhsbl dns, got ' , scalar @ready, ' answers ...') ;
|
|
|
|
return '' unless @ready;
|
|
|
|
|
|
|
|
for my $socket (@ready) {
|
|
|
|
my $query = $res->bgread($socket);
|
|
|
|
$sel->remove($socket);
|
|
|
|
undef $socket;
|
|
|
|
|
|
|
|
if ($query) {
|
|
|
|
foreach my $rr ($query->answer) {
|
|
|
|
$self->log(LOGDEBUG, 'got an ' . $rr->type . ' record ' . $rr->name);
|
|
|
|
if ($rr->type eq 'A') {
|
|
|
|
$result = $rr->name;
|
|
|
|
$self->log(LOGDEBUG, "A record found for $result with IP " . $rr->address);
|
|
|
|
last;
|
2006-03-09 13:37:25 +01:00
|
|
|
} elsif ($rr->type eq 'TXT') {
|
|
|
|
$result = $rr->txtdata;
|
|
|
|
$self->log(LOGDEBUG, "TXT record found: " . $rr->txtdata);
|
|
|
|
last;
|
2004-08-01 03:54:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$self->log(LOGCRIT, "query failed: ", $res->errorstring) unless $res->errorstring eq 'NXDOMAIN';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
#kill any other pending I/O
|
|
|
|
$trans->notes('rhsbl_sockets', undef);
|
|
|
|
return $trans->notes('rhsbl', $result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($sel->count) {
|
|
|
|
# loop around if we have dns results left
|
|
|
|
return $self->process_sockets();
|
|
|
|
}
|
|
|
|
|
|
|
|
# if there was more to read; then forget it
|
|
|
|
$trans->notes('rhsbl_sockets', undef);
|
|
|
|
|
|
|
|
return $trans->notes('rhsbl', $result);
|
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_disconnect {
|
2004-08-01 03:54:16 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
|
|
|
$transaction->notes('rhsbl_sockets', undef);
|
|
|
|
return DECLINED;
|
|
|
|
}
|
2008-06-02 17:41:08 +02:00
|
|
|
|