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");
|
2002-09-10 18:49:10 +02:00
|
|
|
$self->register_hook("disconnect", "disconnect_handler");
|
2002-07-15 14:16:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 18:36:45 +02:00
|
|
|
my $sel = IO::Select->new();
|
2002-09-10 15:36:58 +02:00
|
|
|
|
2002-07-15 14:16:10 +02:00
|
|
|
for my $dnsbl (keys %dnsbl_zones) {
|
2002-09-10 18:49:10 +02:00
|
|
|
$self->log(7, "Checking $reversed_ip.$dnsbl in the background");
|
2002-09-10 18:36:45 +02:00
|
|
|
$sel->add($res->bgsend("$reversed_ip.$dnsbl", "TXT"));
|
2002-09-10 15:36:58 +02:00
|
|
|
}
|
|
|
|
|
2002-09-10 18:36:45 +02:00
|
|
|
$self->qp->connection->notes('dnsbl_sockets', $sel);
|
2002-09-10 15:36:58 +02:00
|
|
|
|
|
|
|
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;
|
2002-09-10 18:36:45 +02:00
|
|
|
my $sel = $conn->notes('dnsbl_sockets') or return "";
|
2002-09-10 15:36:58 +02:00
|
|
|
|
|
|
|
my $result;
|
|
|
|
|
2002-09-10 18:49:10 +02:00
|
|
|
$self->log(8, "waiting for dnsbl dns");
|
2002-09-10 18:36:45 +02:00
|
|
|
|
|
|
|
# don't wait more than 5 seconds here
|
|
|
|
my @ready = $sel->can_read(5);
|
|
|
|
|
2002-09-10 18:49:10 +02:00
|
|
|
$self->log(8, "DONE waiting for dnsbl dns, got " , scalar @ready, " answers ...") ;
|
2002-09-10 18:36:45 +02:00
|
|
|
|
|
|
|
for my $socket (@ready) {
|
|
|
|
my $query = $res->bgread($socket);
|
2002-09-10 15:36:58 +02:00
|
|
|
undef $socket;
|
|
|
|
|
2002-09-10 18:36:45 +02:00
|
|
|
my $dnsbl;
|
|
|
|
|
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";
|
2002-09-10 18:36:45 +02:00
|
|
|
my $name = $rr->name;
|
|
|
|
($dnsbl) = ($name =~ m/(?:\d+\.){4}(.*)/) unless $dnsbl;
|
|
|
|
$dnsbl = $name unless $dnsbl;
|
|
|
|
$self->log(9, "name ", $rr->name);
|
2002-07-15 14:16:10 +02:00
|
|
|
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 18:36:45 +02:00
|
|
|
|
|
|
|
last if $result;
|
|
|
|
|
2002-07-15 14:16:10 +02:00
|
|
|
}
|
|
|
|
|
2002-09-10 18:36:45 +02:00
|
|
|
# if there was more to read; then forget it
|
|
|
|
$conn->notes('dnsbl_sockets', undef);
|
2002-09-10 15:36:58 +02:00
|
|
|
|
|
|
|
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) = @_;
|
|
|
|
|
2002-09-10 18:49:10 +02:00
|
|
|
$self->qp->connection->notes('dnsbl_sockets', undef);
|
2002-09-10 15:36:58 +02:00
|
|
|
|
2002-07-15 14:16:10 +02:00
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
1;
|