2005-03-08 23:58:09 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use Danga::DNS;
|
2002-07-08 04:30:11 +02:00
|
|
|
|
|
|
|
sub register {
|
2005-03-08 23:58:09 +01:00
|
|
|
my ($self) = @_;
|
|
|
|
$self->register_hook("mail", "mail_handler");
|
|
|
|
$self->register_hook("rcpt", "rcpt_handler");
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub mail_handler {
|
2005-03-08 23:58:09 +01:00
|
|
|
my ($self, $transaction, $sender) = @_;
|
|
|
|
|
2005-06-23 14:27:38 +02:00
|
|
|
$self->transaction->notes('resolvable', 1);
|
|
|
|
return DECLINED if $sender->format eq "<>";
|
|
|
|
return $self->check_dns($sender->host);
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub check_dns {
|
2005-03-08 23:58:09 +01:00
|
|
|
my ($self, $host) = @_;
|
|
|
|
|
|
|
|
# for stuff where we can't even parse a hostname out of the address
|
2005-06-23 14:27:38 +02:00
|
|
|
return DECLINED unless $host;
|
2005-03-08 23:58:09 +01:00
|
|
|
|
2005-06-23 14:27:38 +02:00
|
|
|
if( $host =~ m/^\[(\d{1,3}\.){3}\d{1,3}\]$/ ) {
|
|
|
|
$self->transaction->notes('resolvable', 1);
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
2005-11-28 20:07:56 +01:00
|
|
|
my $total_queries = 2;
|
2005-06-23 14:27:38 +02:00
|
|
|
my $qp = $self->qp;
|
|
|
|
$self->log(LOGDEBUG, "Checking $host for MX record in the background");
|
2005-03-08 23:58:09 +01:00
|
|
|
Danga::DNS->new(
|
2005-06-23 14:27:38 +02:00
|
|
|
callback => sub { dns_result($qp, @_) },
|
2005-11-28 20:07:56 +01:00
|
|
|
finished => sub { $total_queries--; finished($qp, $total_queries) },
|
2005-03-08 23:58:09 +01:00
|
|
|
host => $host,
|
|
|
|
type => "MX",
|
2005-06-23 14:27:38 +02:00
|
|
|
client => $qp->input_sock,
|
2005-03-08 23:58:09 +01:00
|
|
|
);
|
2005-06-23 14:27:38 +02:00
|
|
|
$self->log(LOGDEBUG, "Checking $host for A record in the background");
|
2005-03-08 23:58:09 +01:00
|
|
|
Danga::DNS->new(
|
2005-06-23 14:27:38 +02:00
|
|
|
callback => sub { dns_result($qp, @_) },
|
2005-11-28 20:07:56 +01:00
|
|
|
finished => sub { $total_queries--; finished($qp, $total_queries) },
|
2005-03-08 23:58:09 +01:00
|
|
|
host => $host,
|
2005-06-23 14:27:38 +02:00
|
|
|
client => $qp->input_sock,
|
2005-03-08 23:58:09 +01:00
|
|
|
);
|
2005-06-23 14:27:38 +02:00
|
|
|
return CONTINUATION;
|
2005-03-08 23:58:09 +01:00
|
|
|
}
|
2002-07-08 04:30:11 +02:00
|
|
|
|
2005-11-28 20:07:56 +01:00
|
|
|
sub finished {
|
|
|
|
my ($qp, $total_zones) = @_;
|
|
|
|
$qp->finish_continuation unless $total_zones;
|
|
|
|
}
|
2005-06-23 14:27:38 +02:00
|
|
|
|
2005-03-08 23:58:09 +01:00
|
|
|
sub dns_result {
|
2005-06-23 14:27:38 +02:00
|
|
|
my ($qp, $result, $query) = @_;
|
2005-03-08 23:58:09 +01:00
|
|
|
|
|
|
|
if ($result =~ /^[A-Z]+$/) {
|
|
|
|
# probably an error
|
2005-06-23 14:27:38 +02:00
|
|
|
$qp->log(LOGDEBUG, "DNS error: $result looking up $query");
|
|
|
|
} else {
|
|
|
|
$qp->transaction->notes('resolvable', 1);
|
|
|
|
$qp->log(LOGDEBUG, "DNS lookup $query returned: $result");
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-23 14:27:38 +02:00
|
|
|
|
2005-03-08 23:58:09 +01:00
|
|
|
sub rcpt_handler {
|
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
|
|
|
if (!$transaction->notes('resolvable')) {
|
|
|
|
my $sender = $transaction->sender;
|
2005-06-23 14:27:38 +02:00
|
|
|
$self->log(LOGDEBUG, "Could not resolve " .$sender->host) if $sender->host;
|
2005-03-08 23:58:09 +01:00
|
|
|
return (DENYSOFT,
|
|
|
|
($sender->host
|
|
|
|
? "Could not resolve ". $sender->host
|
|
|
|
: "FQDN required in the envelope sender"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return DECLINED;
|
|
|
|
}
|