2012-04-29 05:41:31 +02:00
|
|
|
#!perl -Tw
|
2007-04-10 00:19:40 +02:00
|
|
|
|
|
|
|
use Qpsmtpd::DSN;
|
|
|
|
use ParaDNS;
|
|
|
|
use Socket;
|
2007-05-26 02:48:09 +02:00
|
|
|
use Qpsmtpd::TcpServer;
|
2007-04-10 00:19:40 +02:00
|
|
|
|
|
|
|
my %invalid = ();
|
2007-05-26 02:48:09 +02:00
|
|
|
my $has_ipv6 = Qpsmtpd::TcpServer::has_ipv6();
|
2007-04-10 00:19:40 +02:00
|
|
|
|
|
|
|
sub register {
|
|
|
|
my ( $self, $qp ) = @_;
|
|
|
|
|
|
|
|
foreach my $i ( $self->qp->config("invalid_resolvable_fromhost") ) {
|
|
|
|
$i =~ s/^\s*//;
|
|
|
|
$i =~ s/\s*$//;
|
|
|
|
if ( $i =~ m#^((\d{1,3}\.){3}\d{1,3})/(\d\d?)# ) {
|
|
|
|
$invalid{$1} = $3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->register_hook( mail => 'hook_mail_start' );
|
|
|
|
$self->register_hook( mail => 'hook_mail_done' );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub hook_mail_start {
|
|
|
|
my ( $self, $transaction, $sender ) = @_;
|
|
|
|
|
|
|
|
return DECLINED
|
2008-06-02 17:32:39 +02:00
|
|
|
if ( $self->qp->connection->notes('whitelisthost') );
|
2007-04-10 00:19:40 +02:00
|
|
|
|
|
|
|
if ( $sender ne "<>" ) {
|
|
|
|
|
|
|
|
unless ( $sender->host ) {
|
|
|
|
# default of addr_bad_from_system is DENY, we use DENYSOFT here to
|
|
|
|
# get the same behaviour as without Qpsmtpd::DSN...
|
|
|
|
return Qpsmtpd::DSN->addr_bad_from_system( DENYSOFT,
|
|
|
|
"FQDN required in the envelope sender" );
|
|
|
|
}
|
2008-03-13 20:59:15 +01:00
|
|
|
|
|
|
|
return DECLINED if $sender->host =~ m/^\[(\d{1,3}\.){3}\d{1,3}\]$/;
|
|
|
|
|
|
|
|
unless ($self->check_dns( $sender->host )) {
|
|
|
|
return Qpsmtpd::DSN->temp_resolver_failed(
|
|
|
|
"Could not resolve " . $sender->host );
|
|
|
|
}
|
|
|
|
|
2007-04-10 00:19:40 +02:00
|
|
|
return YIELD;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub hook_mail_done {
|
|
|
|
my ( $self, $transaction, $sender ) = @_;
|
|
|
|
|
|
|
|
return DECLINED
|
2008-06-02 17:32:39 +02:00
|
|
|
if ( $self->qp->connection->notes('whitelisthost') );
|
2007-04-10 00:19:40 +02:00
|
|
|
|
2008-03-13 21:00:33 +01:00
|
|
|
if ( $sender ne "<>" && !$transaction->notes('resolvable_fromhost') ) {
|
2007-04-10 00:19:40 +02:00
|
|
|
# default of temp_resolver_failed is DENYSOFT
|
|
|
|
return Qpsmtpd::DSN->temp_resolver_failed(
|
|
|
|
"Could not resolve " . $sender->host );
|
|
|
|
}
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub check_dns {
|
|
|
|
my ( $self, $host ) = @_;
|
|
|
|
my @host_answers;
|
|
|
|
|
|
|
|
my $qp = $self->qp;
|
2008-03-13 20:59:15 +01:00
|
|
|
$qp->input_sock->pause_read;
|
2007-04-10 00:19:40 +02:00
|
|
|
|
|
|
|
my $a_records = [];
|
2008-05-01 08:18:46 +02:00
|
|
|
my $num_queries = 1; # queries in progress
|
|
|
|
my $mx_found = 0;
|
2008-03-13 20:59:15 +01:00
|
|
|
|
2007-04-10 00:19:40 +02:00
|
|
|
ParaDNS->new(
|
|
|
|
callback => sub {
|
|
|
|
my $mx = shift;
|
|
|
|
return if $mx =~ /^[A-Z]+$/; # error
|
2008-03-13 20:59:15 +01:00
|
|
|
|
2007-04-10 00:19:40 +02:00
|
|
|
my $addr = $mx->[0];
|
2008-05-01 08:18:46 +02:00
|
|
|
$mx_found = 1;
|
2008-03-13 20:59:15 +01:00
|
|
|
|
2007-04-10 00:19:40 +02:00
|
|
|
$num_queries++;
|
|
|
|
ParaDNS->new(
|
|
|
|
callback => sub { push @$a_records, $_[0] if $_[0] !~ /^[A-Z]+$/; },
|
2008-03-13 20:59:15 +01:00
|
|
|
finished => sub { $num_queries--; $self->finish_up($qp, $a_records, $num_queries) },
|
2007-04-10 00:19:40 +02:00
|
|
|
host => $addr,
|
|
|
|
type => 'A',
|
|
|
|
);
|
2008-03-13 20:59:15 +01:00
|
|
|
|
2007-04-10 00:19:40 +02:00
|
|
|
if ($has_ipv6) {
|
|
|
|
$num_queries++;
|
|
|
|
ParaDNS->new(
|
|
|
|
callback => sub { push @$a_records, $_[0] if $_[0] !~ /^[A-Z]+$/; },
|
2008-03-13 20:59:15 +01:00
|
|
|
finished => sub { $num_queries--; $self->finish_up($qp, $a_records, $num_queries) },
|
2007-04-10 00:19:40 +02:00
|
|
|
host => $addr,
|
|
|
|
type => 'AAAA',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2008-05-01 08:18:46 +02:00
|
|
|
finished => sub {
|
|
|
|
|
|
|
|
unless ($mx_found) {
|
|
|
|
|
|
|
|
$num_queries++;
|
|
|
|
ParaDNS->new(
|
|
|
|
callback => sub { push @$a_records, $_[0] if $_[0] !~ /^[A-Z]+$/; },
|
|
|
|
finished => sub { $num_queries--; $self->finish_up($qp, $a_records, $num_queries) },
|
|
|
|
host => $host,
|
|
|
|
type => 'A',
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($has_ipv6) {
|
|
|
|
$num_queries++;
|
|
|
|
ParaDNS->new(
|
|
|
|
callback => sub { push @$a_records, $_[0] if $_[0] !~ /^[A-Z]+$/; },
|
|
|
|
finished => sub { $num_queries--; $self->finish_up($qp, $a_records, $num_queries) },
|
|
|
|
host => $host,
|
|
|
|
type => 'AAAA',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$num_queries--;
|
|
|
|
$self->finish_up($qp, $a_records, $num_queries);
|
|
|
|
},
|
2007-04-10 00:19:40 +02:00
|
|
|
host => $host,
|
|
|
|
type => 'MX',
|
2008-05-01 08:18:46 +02:00
|
|
|
) or $qp->input_sock->continue_read, return;
|
2008-03-13 20:59:15 +01:00
|
|
|
|
|
|
|
return 1;
|
2007-04-10 00:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub finish_up {
|
2008-05-01 08:18:46 +02:00
|
|
|
my ($self, $qp, $a_records, $num_queries) = @_;
|
2008-03-13 20:59:15 +01:00
|
|
|
|
|
|
|
return if defined $qp->transaction->notes('resolvable_fromhost');
|
|
|
|
|
2007-04-10 00:19:40 +02:00
|
|
|
foreach my $addr (@$a_records) {
|
|
|
|
if (is_valid($addr)) {
|
|
|
|
$qp->transaction->notes('resolvable_fromhost', 1);
|
2008-03-13 20:59:15 +01:00
|
|
|
$qp->input_sock->continue_read;
|
|
|
|
$qp->run_continuation;
|
|
|
|
return;
|
2007-04-10 00:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-13 20:59:15 +01:00
|
|
|
unless ($num_queries) {
|
|
|
|
# all queries returned no valid response
|
|
|
|
$qp->transaction->notes('resolvable_fromhost', 0);
|
|
|
|
$qp->input_sock->continue_read;
|
|
|
|
$qp->run_continuation;
|
|
|
|
}
|
2007-04-10 00:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub is_valid {
|
|
|
|
my $ip = shift;
|
|
|
|
my ( $net, $mask );
|
|
|
|
foreach $net ( keys %invalid ) {
|
|
|
|
$mask = $invalid{$net};
|
|
|
|
$mask = pack "B32", "1" x ($mask) . "0" x ( 32 - $mask );
|
|
|
|
return 0
|
|
|
|
if join( ".", unpack( "C4", inet_aton($ip) & $mask ) ) eq $net;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# vim: ts=4 sw=4 expandtab syn=perl
|