2012-06-12 04:13:50 +02:00
|
|
|
#!perl -w
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
helo - validate the HELO message presented by a connecting host.
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
Validate the HELO hostname. This plugin includes a suite of optional tests,
|
|
|
|
selectable by the I<policy> setting. The policy section details which tests
|
|
|
|
are enforced by each policy option.
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2012-06-15 18:44:33 +02:00
|
|
|
It sets the connection notes helo_forward_match and helo_reverse_match when
|
|
|
|
I<policy rfc> or I<policy strict> are used.
|
|
|
|
|
|
|
|
Adds an X-HELO header with the HELO hostname to the message.
|
2012-06-13 23:49:25 +02:00
|
|
|
|
|
|
|
Using I<policy rfc> will reject a very large portion of the spam from hosts
|
|
|
|
that have yet to get blacklisted.
|
|
|
|
|
|
|
|
=head1 WHY IT WORKS
|
|
|
|
|
|
|
|
The reverse DNS of the zombie PCs is out of the spam operators control. Their
|
|
|
|
only way to get past these tests is to limit themselves to hosts with matching
|
|
|
|
forward and reverse DNS, and then use the proper HELO hostname when spamming.
|
|
|
|
At present, this presents a very high hurdle.
|
|
|
|
|
|
|
|
=head1 HELO VALIDATION TESTS
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
|
|
|
=item is_in_badhelo
|
|
|
|
|
|
|
|
Matches in the I<badhelo> config file, including yahoo.com and aol.com, which
|
2012-06-13 23:49:25 +02:00
|
|
|
neither the real Yahoo or the real AOL use, but which spammers use a lot.
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
Like qmail with the qregex patch, the B<badhelo> file can also contain perl
|
|
|
|
regular expressions. In addition to normal regexp processing, a pattern can
|
|
|
|
start with a ! character, and get a negated (!~) match.
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
=item invalid_localhost
|
|
|
|
|
|
|
|
Assure that if a sender uses the 'localhost' hostname, they are coming from
|
|
|
|
the localhost IP.
|
|
|
|
|
|
|
|
=item is_plain_ip
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
Disallow plain IP addresses. They are neither a FQDN nor an address literal.
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
=item is_address_literal [N.N.N.N]
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
An address literal (an IP enclosed in brackets) is legal but rarely, if ever,
|
|
|
|
encountered from legit senders.
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
=item is_forged_literal
|
|
|
|
|
|
|
|
If a literal is presented, make sure it matches the senders IP.
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
=item is_not_fqdn
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
Makes sure the HELO hostname contains at least one dot and has only those
|
|
|
|
characters specifically allowed in domain names (RFC 1035).
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
=item no_forward_dns
|
|
|
|
|
|
|
|
Make sure the HELO hostname resolves.
|
|
|
|
|
|
|
|
=item no_reverse_dns
|
|
|
|
|
|
|
|
Make sure the senders IP address resolves to a hostname.
|
|
|
|
|
|
|
|
=item no_matching_dns
|
|
|
|
|
|
|
|
Make sure the HELO hostname has an A or AAAA record that matches the senders
|
|
|
|
IP address, and make sure that the senders IP has a PTR that resolves to the
|
|
|
|
HELO hostname.
|
|
|
|
|
2013-03-11 04:22:44 +01:00
|
|
|
Per RFC 5321 section 4.1.4, it is impermissible to block a message I<soley>
|
|
|
|
on the basis of the HELO hostname not matching the senders IP.
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
Since the dawn of SMTP, having matching DNS has been a minimum standard
|
|
|
|
expected and oft required of mail servers. While requiring matching DNS is
|
|
|
|
prudent, requiring an exact match will reject valid email. While testing this
|
|
|
|
plugin with rejection disabled, I noticed that mx0.slc.paypal.com sends email
|
|
|
|
from an IP that reverses to mx1.slc.paypal.com. While that's technically an
|
|
|
|
error, I believe it's an error to reject mail based on it. Especially since
|
|
|
|
SLD and TLD match.
|
|
|
|
|
|
|
|
To avoid snagging false positives, matches are extended to the first
|
|
|
|
3 octets of the IP and the last two labels of the FQDN. The following are
|
|
|
|
considered a match:
|
|
|
|
|
|
|
|
192.0.1.2, 192.0.1.3
|
|
|
|
|
|
|
|
foo.example.com, bar.example.com
|
|
|
|
|
|
|
|
This allows I<no_matching_dns> to be used without rejecting mail from orgs with
|
|
|
|
pools of servers where the HELO name and IP don't exactly match. This list
|
|
|
|
includes Yahoo, Gmail, PayPal, cheaptickets.com, exchange.microsoft.com, and
|
|
|
|
likely many more.
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
=head1 CONFIGURATION
|
|
|
|
|
|
|
|
=head2 policy [ lenient | rfc | strict ]
|
|
|
|
|
|
|
|
Default: lenient
|
|
|
|
|
|
|
|
=head3 lenient
|
|
|
|
|
2013-03-28 00:03:13 +01:00
|
|
|
Runs the following tests: is_in_badhelo, invalid_localhost,
|
2012-06-30 20:18:16 +02:00
|
|
|
is_forged_literal, and is_plain_ip.
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
This setting is lenient enough not to cause problems for your Windows users.
|
|
|
|
It is comparable to running check_spamhelo, but with the addition of regexp
|
2012-06-30 20:18:16 +02:00
|
|
|
support, the prevention of forged localhost, forged IP literals, and plain
|
|
|
|
IPs.
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
=head3 rfc
|
|
|
|
|
2012-06-15 18:44:33 +02:00
|
|
|
Per RFC 2821, the HELO hostname is the FQDN of the sending server or an
|
2012-06-12 04:13:50 +02:00
|
|
|
address literal. When I<policy rfc> is selected, all the lenient checks and
|
2013-03-28 00:03:13 +01:00
|
|
|
the following are tested: is_not_fqdn, no_forward_dns, and no_reverse_dns.
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
If you have Windows users that send mail via your server, do not choose
|
2013-03-28 00:03:13 +01:00
|
|
|
I<policy rfc> without setting I<reject> to 0 or naughty.
|
|
|
|
Windows PCs often send unqualified HELO names and will have trouble
|
2013-03-11 04:22:44 +01:00
|
|
|
sending mail. The B<naughty> plugin defers the rejection, giving the user
|
|
|
|
the opportunity to authenticate and bypass the rejection.
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
=head3 strict
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
Strict includes all the RFC tests and the following: no_matching_dns, and
|
|
|
|
is_address_literal.
|
|
|
|
|
|
|
|
I have yet to see an address literal being used by a hammy sender. But I am
|
|
|
|
not certain that blocking them all is prudent.
|
|
|
|
|
|
|
|
It is recommended that I<policy strict> be used with <reject 0> and that you
|
2013-03-28 00:03:13 +01:00
|
|
|
examine your logs for false positives.
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
=head2 badhelo
|
|
|
|
|
|
|
|
Add domains, hostnames, or perl regexp patterns to the F<badhelo> config
|
|
|
|
file; one per line.
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
=head2 timeout [seconds]
|
|
|
|
|
|
|
|
Default: 5
|
|
|
|
|
|
|
|
The number of seconds before DNS queries timeout.
|
|
|
|
|
2012-06-12 04:13:50 +02:00
|
|
|
=head2 reject [ 0 | 1 | naughty ]
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
Default: 1
|
|
|
|
|
2012-06-12 04:13:50 +02:00
|
|
|
0: do not reject
|
|
|
|
|
|
|
|
1: reject
|
|
|
|
|
|
|
|
naughty: naughty plugin handles rejection
|
|
|
|
|
|
|
|
=head2 reject_type [ temp | perm | disconnect ]
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
Default: disconnect
|
|
|
|
|
2012-06-12 04:13:50 +02:00
|
|
|
What type of rejection should be sent? See docs/config.pod
|
|
|
|
|
|
|
|
=head2 loglevel
|
|
|
|
|
|
|
|
Adjust the quantity of logging for this plugin. See docs/logging.pod
|
|
|
|
|
|
|
|
=head1 RFC 2821
|
|
|
|
|
|
|
|
=head2 4.1.1.1
|
|
|
|
|
|
|
|
The HELO hostname "...contains the fully-qualified domain name of the SMTP
|
|
|
|
client if one is available. In situations in which the SMTP client system
|
|
|
|
does not have a meaningful domain name (e.g., when its address is dynamically
|
|
|
|
allocated and no reverse mapping record is available), the client SHOULD send
|
|
|
|
an address literal (see section 4.1.3), optionally followed by information
|
|
|
|
that will help to identify the client system."
|
|
|
|
|
|
|
|
=head2 2.3.5
|
|
|
|
|
|
|
|
The domain name, as described in this document and in [22], is the
|
|
|
|
entire, fully-qualified name (often referred to as an "FQDN"). A domain name
|
|
|
|
that is not in FQDN form is no more than a local alias. Local aliases MUST
|
|
|
|
NOT appear in any SMTP transaction.
|
|
|
|
|
|
|
|
|
2013-03-11 04:22:44 +01:00
|
|
|
=head1 RFC 5321
|
|
|
|
|
|
|
|
=head2 4.1.4
|
|
|
|
|
|
|
|
An SMTP server MAY verify that the domain name argument in the EHLO
|
|
|
|
command actually corresponds to the IP address of the client.
|
|
|
|
However, if the verification fails, the server MUST NOT refuse to
|
|
|
|
accept a message on that basis. Information captured in the
|
|
|
|
verification attempt is for logging and tracing purposes. Note that
|
|
|
|
this prohibition applies to the matching of the parameter to its IP
|
|
|
|
address only; see Section 7.9 for a more extensive discussion of
|
|
|
|
rejecting incoming connections or mail messages.
|
|
|
|
|
2013-04-30 04:06:47 +02:00
|
|
|
=head1 TODO
|
|
|
|
|
|
|
|
is_forged_literal, if the forged IP is an internal IP, it's likely one
|
|
|
|
of our clients that should have authenticated. Perhaps when we check back
|
|
|
|
later in data_post, if they have added relay_client, then give back the
|
|
|
|
karma.
|
2013-03-11 04:22:44 +01:00
|
|
|
|
2012-06-12 04:13:50 +02:00
|
|
|
=head1 AUTHOR
|
|
|
|
|
|
|
|
2012 - Matt Simerson
|
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS
|
|
|
|
|
|
|
|
badhelo processing from check_badhelo plugin
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
badhelo regex processing idea from qregex patch
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2014-03-21 17:28:05 +01:00
|
|
|
additional check ideas from Haraka helo plugin
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2014-09-11 22:34:32 +02:00
|
|
|
use Net::IP;
|
|
|
|
|
2012-06-12 04:13:50 +02:00
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
|
|
|
|
sub register {
|
2012-06-23 01:24:16 +02:00
|
|
|
my ($self, $qp) = (shift, shift);
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->{_args} = {@_};
|
2013-03-28 00:03:13 +01:00
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
$self->{_args}{reject_type} = 'disconnect';
|
2012-06-12 04:13:50 +02:00
|
|
|
$self->{_args}{policy} ||= 'lenient';
|
2013-03-28 00:03:13 +01:00
|
|
|
$self->{_args}{dns_timeout} ||= $self->{_args}{timeout} || 5;
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if (!defined $self->{_args}{reject}) {
|
2012-06-12 04:13:50 +02:00
|
|
|
$self->{_args}{reject} = 1;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
$self->populate_tests();
|
2014-11-06 03:02:58 +01:00
|
|
|
$self->get_resolver() or return;
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->register_hook('helo', 'helo_handler');
|
|
|
|
$self->register_hook('ehlo', 'helo_handler');
|
2012-06-13 23:49:25 +02:00
|
|
|
$self->register_hook('data_post', 'data_post_handler');
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
sub helo_handler {
|
|
|
|
my ($self, $transaction, $host) = @_;
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if (!$host) {
|
2013-04-24 22:25:31 +02:00
|
|
|
$self->log(LOGINFO, "fail, tolerated, no helo host");
|
2013-12-18 06:16:02 +01:00
|
|
|
$self->adjust_karma(-2);
|
2012-06-12 04:13:50 +02:00
|
|
|
return DECLINED;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-13 23:49:25 +02:00
|
|
|
|
|
|
|
return DECLINED if $self->is_immune();
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
foreach my $test (@{$self->{_helo_tests}}) {
|
|
|
|
my @err = $self->$test($host);
|
|
|
|
if (scalar @err) {
|
|
|
|
$self->adjust_karma(-1);
|
|
|
|
return $self->get_reject(@err);
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
$self->log(LOGINFO, "pass");
|
2012-06-12 04:13:50 +02:00
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
sub data_post_handler {
|
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
|
|
|
$transaction->header->delete('X-HELO');
|
2013-04-21 06:50:39 +02:00
|
|
|
$transaction->header->add('X-HELO', $self->qp->connection->hello_host, 0);
|
2012-06-13 23:49:25 +02:00
|
|
|
|
2014-09-18 03:28:51 +02:00
|
|
|
return DECLINED;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-13 23:49:25 +02:00
|
|
|
|
2012-06-12 04:13:50 +02:00
|
|
|
sub populate_tests {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $policy = $self->{_args}{policy};
|
2013-04-21 06:50:39 +02:00
|
|
|
@{$self->{_helo_tests}} =
|
|
|
|
qw/ is_in_badhelo invalid_localhost is_forged_literal is_plain_ip /;
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($policy eq 'rfc' || $policy eq 'strict') {
|
|
|
|
push @{$self->{_helo_tests}},
|
|
|
|
qw/ is_not_fqdn no_forward_dns no_reverse_dns /;
|
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($policy eq 'strict') {
|
|
|
|
push @{$self->{_helo_tests}}, qw/ is_address_literal no_matching_dns /;
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
sub is_in_badhelo {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $host) = @_;
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
my $error = "I do not believe you are $host.";
|
|
|
|
|
|
|
|
$host = lc $host;
|
|
|
|
foreach my $bad ($self->qp->config('badhelo')) {
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($bad =~ /[\{\}\[\]\(\)\^\$\|\*\+\?\\\!]/) { # it's a regexp
|
2015-12-17 19:24:57 +01:00
|
|
|
if ($self->is_regex_match($host, $bad)) {
|
|
|
|
return $error, "in badhelo";
|
|
|
|
}
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
|
|
|
if ($host eq lc $bad) {
|
2014-09-18 03:28:51 +02:00
|
|
|
return $error, "in badhelo";
|
2012-06-12 04:13:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
sub is_regex_match {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $host, $pattern) = @_;
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
my $error = "Your HELO hostname is not allowed";
|
|
|
|
|
|
|
|
#$self->log( LOGDEBUG, "is regex ($pattern)");
|
2013-04-21 06:50:39 +02:00
|
|
|
if (substr($pattern, 0, 1) eq '!') {
|
2012-06-12 04:13:50 +02:00
|
|
|
$pattern = substr $pattern, 1;
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($host !~ /$pattern/) {
|
|
|
|
|
2012-06-12 04:13:50 +02:00
|
|
|
#$self->log( LOGDEBUG, "matched ($pattern)");
|
2014-09-18 03:28:51 +02:00
|
|
|
return $error, "badhelo pattern match ($pattern)";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($host =~ /$pattern/) {
|
|
|
|
|
2012-06-12 04:13:50 +02:00
|
|
|
#$self->log( LOGDEBUG, "matched ($pattern)");
|
2014-09-18 03:28:51 +02:00
|
|
|
return $error, "badhelo pattern match ($pattern)";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub invalid_localhost {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $host) = @_;
|
2014-11-13 23:29:52 +01:00
|
|
|
if ($self->is_localhost($self->qp->connection->remote_ip)) {
|
2014-09-09 23:44:50 +02:00
|
|
|
$self->log(LOGDEBUG, "pass, is localhost");
|
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2015-05-26 12:27:33 +02:00
|
|
|
if ($host && lc $host ne 'localhost') {
|
2014-09-11 00:12:52 +02:00
|
|
|
$self->log(LOGDEBUG, "pass, host is localhost");
|
|
|
|
return;
|
|
|
|
};
|
2014-09-09 23:44:50 +02:00
|
|
|
|
|
|
|
#$self->log( LOGINFO, "fail, not localhost" );
|
2014-09-18 03:28:51 +02:00
|
|
|
return "You are not localhost", "invalid localhost";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
sub is_plain_ip {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $host) = @_;
|
2014-11-13 23:29:52 +01:00
|
|
|
return if ! $self->is_valid_ip($host);
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->log(LOGDEBUG, "fail, plain IP");
|
2014-09-18 03:28:51 +02:00
|
|
|
return "Plain IP is invalid HELO hostname (RFC 2821)", "plain IP";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
sub is_address_literal {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $host) = @_;
|
2014-09-11 22:34:32 +02:00
|
|
|
|
|
|
|
my ($ip) = $host =~ /^\[(.*)\]/; # strip off any brackets
|
|
|
|
return if !$ip; # no brackets, not a literal
|
|
|
|
|
2014-11-13 23:29:52 +01:00
|
|
|
return if ! $self->is_valid_ip($ip);
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->log(LOGDEBUG, "fail, bracketed IP");
|
2014-09-18 03:28:51 +02:00
|
|
|
return "RFC 2821 allows an address literal, but we do not","bracketed IP";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
sub is_forged_literal {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $host) = @_;
|
2014-11-13 23:29:52 +01:00
|
|
|
return if ! $self->is_valid_ip($host);
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2014-09-11 22:34:32 +02:00
|
|
|
# should we add exceptions for reserved internal IP space? (192.168,10., etc)
|
2012-06-12 04:13:50 +02:00
|
|
|
$host = substr $host, 1, -1;
|
|
|
|
return if $host eq $self->qp->connection->remote_ip;
|
2014-09-18 03:28:51 +02:00
|
|
|
return "Forged IPs not accepted here", "forged IP literal";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
sub is_not_fqdn {
|
|
|
|
my ($self, $host) = @_;
|
2013-04-21 06:50:39 +02:00
|
|
|
return if $host =~ m/^\[(\d{1,3}\.){3}\d{1,3}\]$/; # address literal, skip
|
|
|
|
if ($host !~ /\./) { # has no dots
|
2014-09-18 03:28:51 +02:00
|
|
|
return "HELO name is not fully qualified. Read RFC 2821", "not FQDN";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
|
|
|
if ($host =~ /[^a-zA-Z0-9\-\.]/) {
|
2014-09-18 03:28:51 +02:00
|
|
|
return "HELO name contains invalid FQDN characters. Read RFC 1035","invalid FQDN chars";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
sub no_forward_dns {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $host) = @_;
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
return if $self->is_address_literal($host);
|
2013-03-23 06:43:32 +01:00
|
|
|
|
2014-11-06 03:02:58 +01:00
|
|
|
my $res = $self->get_resolver();
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$host = "$host." if $host !~ /\.$/; # fully qualify name
|
2014-09-11 20:37:24 +02:00
|
|
|
my $query = $res->query($host);
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if (!$query) {
|
|
|
|
if ($res->errorstring eq 'NXDOMAIN') {
|
2014-09-18 03:28:51 +02:00
|
|
|
return "HELO hostname does not exist", "no such host";
|
2012-06-12 04:13:50 +02:00
|
|
|
}
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->log(LOGERROR, "skip, query failed (", $res->errorstring, ")");
|
2012-06-12 04:13:50 +02:00
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
my $hits = 0;
|
|
|
|
foreach my $rr ($query->answer) {
|
|
|
|
next unless $rr->type =~ /^(?:A|AAAA)$/;
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->check_ip_match($rr->address);
|
2012-06-12 04:13:50 +02:00
|
|
|
$hits++;
|
2013-03-27 03:14:26 +01:00
|
|
|
last if $self->connection->notes('helo_forward_match');
|
2012-06-12 04:13:50 +02:00
|
|
|
}
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($hits) {
|
2012-06-12 04:13:50 +02:00
|
|
|
$self->log(LOGDEBUG, "pass, forward DNS") if $hits;
|
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2014-09-18 03:28:51 +02:00
|
|
|
return "HELO hostname did not resolve", "no forward DNS";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
sub no_reverse_dns {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $host, $ip) = @_;
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2014-11-06 03:02:58 +01:00
|
|
|
my $res = $self->get_resolver();
|
2012-06-12 04:13:50 +02:00
|
|
|
$ip ||= $self->qp->connection->remote_ip;
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
my $query = $res->query($ip) or do {
|
|
|
|
if ($res->errorstring eq 'NXDOMAIN') {
|
2014-09-18 03:28:51 +02:00
|
|
|
return "no rDNS for $ip", "no rDNS";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
|
|
|
$self->log(LOGINFO, $res->errorstring);
|
2014-09-18 03:28:51 +02:00
|
|
|
return "error getting reverse DNS for $ip", "rDNS " . $res->errorstring;
|
2012-06-12 04:13:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
my $hits = 0;
|
|
|
|
for my $rr ($query->answer) {
|
|
|
|
next if $rr->type ne 'PTR';
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->log(LOGDEBUG, "PTR: " . $rr->ptrdname);
|
|
|
|
$self->check_name_match(lc $rr->ptrdname, lc $host);
|
2012-06-12 04:13:50 +02:00
|
|
|
$hits++;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
|
|
|
if ($hits) {
|
2012-06-13 23:49:25 +02:00
|
|
|
$self->log(LOGDEBUG, "has rDNS");
|
2012-06-12 04:13:50 +02:00
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2014-09-18 03:28:51 +02:00
|
|
|
return "no reverse DNS for $ip", "no rDNS";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
|
|
|
sub no_matching_dns {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $host) = @_;
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
# this is called iprev, or "Forward-confirmed reverse DNS" and is discussed
|
|
|
|
# in RFC 5451. FCrDNS is done for the remote IP in the fcrdns plugin. Here
|
|
|
|
# we do it on the HELO hostname.
|
|
|
|
# consider adding status to Authentication-Results header
|
2013-03-16 06:16:06 +01:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ( $self->connection->notes('helo_forward_match')
|
|
|
|
&& $self->connection->notes('helo_reverse_match'))
|
|
|
|
{
|
|
|
|
$self->log(LOGDEBUG, "foward and reverse match");
|
|
|
|
$self->adjust_karma(1); # a perfect match
|
2012-06-12 04:13:50 +02:00
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($self->connection->notes('helo_forward_match')) {
|
|
|
|
$self->log(LOGDEBUG, "name matches IP");
|
2012-06-12 04:13:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($self->connection->notes('helo_reverse_match')) {
|
|
|
|
$self->log(LOGDEBUG, "reverse matches name");
|
2012-06-12 04:13:50 +02:00
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->log(LOGINFO, "fail, no forward or reverse DNS match");
|
2014-09-18 03:28:51 +02:00
|
|
|
return "That HELO hostname fails FCrDNS", "no matching DNS";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
sub check_ip_match {
|
|
|
|
my $self = shift;
|
|
|
|
my $ip = shift or return;
|
|
|
|
|
2014-09-11 22:34:32 +02:00
|
|
|
my $rip = $self->qp->connection->remote_ip;
|
|
|
|
if ($ip eq $rip) {
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->log(LOGDEBUG, "forward ip match");
|
2012-06-13 23:49:25 +02:00
|
|
|
$self->connection->notes('helo_forward_match', 1);
|
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-13 23:49:25 +02:00
|
|
|
|
2014-09-11 22:34:32 +02:00
|
|
|
my ($dns_net, $rem_net);
|
|
|
|
if ($ip =~ /:/) {
|
|
|
|
if ($ip =~ /::/) { $ip = Net::IP::ip_expand_address($ip, 6); }
|
|
|
|
if ($rip =~ /::/) { $rip = Net::IP::ip_expand_address($rip, 6); }
|
|
|
|
|
|
|
|
$dns_net = join(':', (split(/:/, $ip ))[0, 1, 2, 3, 4, 5]);
|
|
|
|
$rem_net = join(':', (split(/:/, $rip))[0, 1, 2, 3, 4, 5]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$dns_net = join('.', (split(/\./, $ip))[0, 1, 2]);
|
|
|
|
$rem_net = join('.', (split(/\./, $rip))[0, 1, 2]);
|
|
|
|
}
|
2012-06-13 23:49:25 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($dns_net eq $rem_net) {
|
|
|
|
$self->log(LOGNOTICE, "forward network match");
|
2012-06-13 23:49:25 +02:00
|
|
|
$self->connection->notes('helo_forward_match', 1);
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-13 23:49:25 +02:00
|
|
|
|
|
|
|
sub check_name_match {
|
|
|
|
my $self = shift;
|
|
|
|
my ($dns_name, $helo_name) = @_;
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
return if !$dns_name;
|
2016-04-19 18:18:03 +02:00
|
|
|
my @dots = split(/\./, $dns_name);
|
|
|
|
return if scalar @dots < 2; # not a FQDN
|
2012-11-19 18:32:21 +01:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($dns_name eq $helo_name) {
|
|
|
|
$self->log(LOGDEBUG, "reverse name match");
|
2012-06-13 23:49:25 +02:00
|
|
|
$self->connection->notes('helo_reverse_match', 1);
|
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-06-13 23:49:25 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
my $dns_dom = join('.', (split(/\./, $dns_name))[-2, -1]);
|
|
|
|
my $helo_dom = join('.', (split(/\./, $helo_name))[-2, -1]);
|
2012-06-13 23:49:25 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($dns_dom eq $helo_dom) {
|
|
|
|
$self->log(LOGNOTICE, "reverse domain match");
|
2012-06-13 23:49:25 +02:00
|
|
|
$self->connection->notes('helo_reverse_match', 1);
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-13 23:49:25 +02:00
|
|
|
|