2002-07-15 14:16:10 +02:00
|
|
|
|
|
|
|
sub register {
|
|
|
|
my ($self, $qp) = @_;
|
|
|
|
$self->register_hook("connect", "connect_handler");
|
|
|
|
$self->register_hook("rcpt", "rcpt_handler");
|
|
|
|
#$self->register_hook("disconnect", "disconnect_handler");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub connect_handler {
|
|
|
|
my ($self, $transaction) = @_;
|
2002-09-10 15:42:06 +02:00
|
|
|
|
2002-07-15 14:16:10 +02:00
|
|
|
my $remote_ip = $self->qp->connection->remote_ip;
|
|
|
|
|
|
|
|
my %dnsbl_zones = map { (split /\s+/, $_, 2)[0,1] } $self->qp->config('dnsbl_zones');
|
2002-09-10 15:42:06 +02:00
|
|
|
return DECLINED unless %dnsbl_zones;
|
2002-09-10 15:36:58 +02:00
|
|
|
|
2002-07-15 14:16:10 +02:00
|
|
|
my $reversed_ip = join(".", reverse(split(/\./, $remote_ip)));
|
|
|
|
|
|
|
|
# we should queue these lookups in the background and just fetch the
|
|
|
|
# results in the first rcpt handler ... oh well.
|
|
|
|
|
|
|
|
my $res = new Net::DNS::Resolver;
|
2002-09-10 15:36:58 +02:00
|
|
|
my $sockets = [];
|
|
|
|
|
2002-07-15 14:16:10 +02:00
|
|
|
for my $dnsbl (keys %dnsbl_zones) {
|
2002-09-10 15:36:58 +02:00
|
|
|
$self->log(5, "Checking $reversed_ip.$dnsbl");
|
|
|
|
push @{$sockets}, [$dnsbl, $res->bgsend("$reversed_ip.$dnsbl", "TXT")];
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->qp->connection->notes('dnsbl_sockets', $sockets);
|
|
|
|
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub process_sockets {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $conn = $self->qp->connection;
|
|
|
|
|
|
|
|
return $conn->notes('dnsbl')
|
|
|
|
if $conn->notes('dnsbl');
|
|
|
|
|
|
|
|
my $res = new Net::DNS::Resolver;
|
|
|
|
my $sockets = $conn->notes('dnsbl_sockets') or return "";
|
|
|
|
|
|
|
|
my $result;
|
|
|
|
|
|
|
|
for my $socket (@{$sockets}) {
|
|
|
|
my $query = $res->bgread($socket->[1]);
|
|
|
|
my $dnsbl = $socket->[0];
|
|
|
|
undef $socket;
|
|
|
|
|
2002-07-15 14:16:10 +02:00
|
|
|
if ($query) {
|
|
|
|
my $a_record = 0;
|
|
|
|
foreach my $rr ($query->answer) {
|
|
|
|
$a_record = 1 if $rr->type eq "A";
|
|
|
|
next unless $rr->type eq "TXT";
|
|
|
|
$self->log(10, "got txt record");
|
|
|
|
$result = $rr->txtdata and last;
|
|
|
|
}
|
|
|
|
$a_record and $result = "Blocked by $dnsbl";
|
|
|
|
}
|
|
|
|
else {
|
2002-09-10 15:36:58 +02:00
|
|
|
$self->log(4, "$dnsbl query failed: ", $res->errorstring)
|
2002-07-15 14:16:10 +02:00
|
|
|
unless $res->errorstring eq "NXDOMAIN";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-10 15:36:58 +02:00
|
|
|
# if there were more to read; then forget about them again ...
|
|
|
|
undef $_ for (@{$sockets});
|
|
|
|
|
|
|
|
return $conn->notes('dnsbl', $result);
|
2002-07-15 14:16:10 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rcpt_handler {
|
|
|
|
my ($self, $transaction, $rcpt) = @_;
|
2002-09-10 15:36:58 +02:00
|
|
|
my $note = $self->process_sockets;
|
2002-07-15 14:16:10 +02:00
|
|
|
return (DENY, $note) if $note;
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub disconnect_handler {
|
2002-09-10 15:36:58 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
|
|
|
my $sockets = $self->qp->connection->notes('dnsbl_sockets');
|
|
|
|
# if there were more to read; then forget about them again ...
|
|
|
|
undef $_ for (@{$sockets});
|
|
|
|
|
2002-07-15 14:16:10 +02:00
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
1;
|