| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
resolvable_fromhost |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
Determine if the from host resolves to a valid MX or host. |
| 10 |
|
| 11 |
=head1 DESCRIPTION |
| 12 |
|
| 13 |
The fromhost is the part of the email address after the @ symbol, provided by |
| 14 |
the sending server during the SMTP conversation. This is usually, but not |
| 15 |
always, the same as the hostname in the From: header. |
| 16 |
|
| 17 |
B<resolvable_fromhost> tests to see if the fromhost resolves. It saves the |
| 18 |
results in the transaction note I<resolvable_fromhost> where other plugins can |
| 19 |
use that information. Typical results are: |
| 20 |
|
| 21 |
a - fromhost resolved as an A record |
| 22 |
mx - fromhost has valid MX record(s) |
| 23 |
ip - fromhost was an IP |
| 24 |
whitelist - skipped checks due to whitelisting |
| 25 |
null - null sender |
| 26 |
config - fromhost not resolvable, but I<reject 0> was set. |
| 27 |
|
| 28 |
Any other result is an error message with details of the failure. |
| 29 |
|
| 30 |
If B<resolvable_fromhost> is enabled, the from hostname is also stored in |
| 31 |
I<resolvable_fromhost_host>, making it accessible when $sender is not. |
| 32 |
|
| 33 |
=head1 CONFIGURATION |
| 34 |
|
| 35 |
=head2 reject < 0 | 1 | naughty > |
| 36 |
|
| 37 |
If I<reject 1> is set, the old require_resolvable_fromhost plugin behavior of |
| 38 |
temporary rejection is the default. |
| 39 |
|
| 40 |
resolvable_fromhost reject [ 0 | 1 | naughty ] |
| 41 |
|
| 42 |
Default: 1 |
| 43 |
|
| 44 |
=head2 reject_type |
| 45 |
|
| 46 |
reject_type [ perm | temp ] |
| 47 |
|
| 48 |
Set I<reject_type perm> to reject mail instead of deferring it. |
| 49 |
|
| 50 |
Default: temp (temporary, aka soft, aka 4xx). |
| 51 |
|
| 52 |
=head1 EXAMPLE LOG ENTRIES |
| 53 |
|
| 54 |
80072 (mail) resolvable_fromhost: pass, googlegroups.com has MX at gmr-smtp-in.l.google.com |
| 55 |
80108 (mail) resolvable_fromhost: pass, zerobarriers.net has MX at zerobarriers.net |
| 56 |
80148 (mail) resolvable_fromhost: pass, uhin.com has MX at filter.itsafemail.com |
| 57 |
86627 (mail) resolvable_fromhost: palmalar.com has no MX |
| 58 |
86627 (mail) resolvable_fromhost: fail, palmalar.com (SERVFAIL) |
| 59 |
|
| 60 |
=head1 AUTHORS |
| 61 |
|
| 62 |
2012 - Matt Simerson - refactored, added: POD, tests, reject, reject_type |
| 63 |
|
| 64 |
2002 - Ask Bjørn Hansen - intial plugin |
| 65 |
|
| 66 |
=cut |
| 67 |
|
| 68 |
use strict; |
| 69 |
use warnings; |
| 70 |
|
| 71 |
use lib 'lib'; |
| 72 |
use Qpsmtpd::Constants; |
| 73 |
use Qpsmtpd::DSN; |
| 74 |
use Qpsmtpd::TcpServer; |
| 75 |
|
| 76 |
use Socket; |
| 77 |
use Net::DNS qw(mx); |
| 78 |
use Net::IP qw(:PROC); |
| 79 |
|
| 80 |
my %invalid = (); |
| 81 |
my $has_ipv6 = Qpsmtpd::TcpServer::has_ipv6(); |
| 82 |
|
| 83 |
sub register { |
| 84 |
my ($self, $qp, %args) = @_; |
| 85 |
|
| 86 |
foreach (keys %args) { |
| 87 |
$self->{_args}->{$_} = $args{$_}; |
| 88 |
} |
| 89 |
if (!defined $self->{_args}{reject}) { |
| 90 |
$self->{_args}{reject} = 1; |
| 91 |
} |
| 92 |
$self->{_args}{reject_type} ||= 'soft'; |
| 93 |
} |
| 94 |
|
| 95 |
sub hook_mail { |
| 96 |
my ($self, $transaction, $sender, %param) = @_; |
| 97 |
|
| 98 |
return DECLINED if $self->is_immune(); |
| 99 |
|
| 100 |
if ($sender eq '<>') { |
| 101 |
$transaction->notes('resolvable_fromhost', 'null'); |
| 102 |
$self->log(LOGINFO, "pass, null sender"); |
| 103 |
return DECLINED; |
| 104 |
} |
| 105 |
|
| 106 |
$self->populate_invalid_networks(); |
| 107 |
my $resolved = $self->check_dns($sender->host, $transaction); |
| 108 |
|
| 109 |
return DECLINED if $resolved; # success, no need to continue |
| 110 |
#return DECLINED if $sender->host; # reject later |
| 111 |
|
| 112 |
my $result = $transaction->notes('resolvable_fromhost') or do { |
| 113 |
if ($self->{_args}{reject}) { |
| 114 |
; |
| 115 |
$self->log(LOGINFO, 'fail, missing result'); |
| 116 |
return Qpsmtpd::DSN->temp_resolver_failed($self->get_reject_type(), |
| 117 |
''); |
| 118 |
} |
| 119 |
$self->log(LOGINFO, 'fail, tolerated, missing result'); |
| 120 |
return DECLINED; |
| 121 |
}; |
| 122 |
|
| 123 |
return DECLINED if $result =~ /^(?:a|ip|mx)$/; # success |
| 124 |
return DECLINED if $result =~ /^(?:whitelist|null|naughty)$/; # immunity |
| 125 |
|
| 126 |
$self->adjust_karma(-1); |
| 127 |
|
| 128 |
if (!$self->{_args}{reject}) { |
| 129 |
; |
| 130 |
$self->log(LOGINFO, "fail, tolerated, $result"); |
| 131 |
return DECLINED; |
| 132 |
} |
| 133 |
|
| 134 |
$self->log(LOGINFO, "fail, $result"); # log error |
| 135 |
return |
| 136 |
Qpsmtpd::DSN->addr_bad_from_system($self->get_reject_type(), |
| 137 |
"FQDN required in the envelope sender"); |
| 138 |
} |
| 139 |
|
| 140 |
sub check_dns { |
| 141 |
my ($self, $host, $transaction) = @_; |
| 142 |
|
| 143 |
# we can't even parse a hostname out of the address |
| 144 |
if (!$host) { |
| 145 |
$transaction->notes('resolvable_fromhost', 'unparsable host'); |
| 146 |
$self->adjust_karma(-1); |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
$transaction->notes('resolvable_fromhost_host', $host); |
| 151 |
|
| 152 |
if ($host =~ m/^\[(\d{1,3}\.){3}\d{1,3}\]$/) { |
| 153 |
$self->log(LOGINFO, "skip, $host is an IP"); |
| 154 |
$transaction->notes('resolvable_fromhost', 'ip'); |
| 155 |
$self->adjust_karma(-1); |
| 156 |
return 1; |
| 157 |
} |
| 158 |
|
| 159 |
my $res = new Net::DNS::Resolver(dnsrch => 0); |
| 160 |
$res->tcp_timeout(30); |
| 161 |
$res->udp_timeout(30); |
| 162 |
|
| 163 |
my $has_mx = $self->get_and_validate_mx($res, $host, $transaction); |
| 164 |
return 1 if $has_mx == 1; # success, has MX! |
| 165 |
return if $has_mx == -1; # has invalid MX records |
| 166 |
# at this point, no MX for fh is resolvable |
| 167 |
|
| 168 |
my @host_answers = $self->get_host_records($res, $host, $transaction); |
| 169 |
foreach my $rr (@host_answers) { |
| 170 |
if ($rr->type eq 'A' || $rr->type eq 'AAAA') { |
| 171 |
$self->log(LOGINFO, "pass, found A for $host"); |
| 172 |
$transaction->notes('resolvable_fromhost', 'a'); |
| 173 |
return $self->ip_is_valid($rr->address); |
| 174 |
} |
| 175 |
if ($rr->type eq 'MX') { |
| 176 |
$self->log(LOGINFO, "pass, found MX for $host"); |
| 177 |
$transaction->notes('resolvable_fromhost', 'mx'); |
| 178 |
return $self->mx_address_resolves($rr->exchange, $host); |
| 179 |
} |
| 180 |
} |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
sub ip_is_valid { |
| 185 |
my ($self, $ip) = @_; |
| 186 |
my ($net, $mask); |
| 187 |
### while (($net,$mask) = each %invalid) { |
| 188 |
### ... does NOT reset to beginning, will start on |
| 189 |
### 2nd invocation after where it denied the first time..., so |
| 190 |
### 2nd time the same "MAIL FROM" would be accepted! |
| 191 |
foreach $net (keys %invalid) { |
| 192 |
$mask = $invalid{$net}; |
| 193 |
$mask = pack "B32", "1" x ($mask) . "0" x (32 - $mask); |
| 194 |
return if $net eq join('.', unpack("C4", inet_aton($ip) & $mask)); |
| 195 |
} |
| 196 |
return 1; |
| 197 |
} |
| 198 |
|
| 199 |
sub get_and_validate_mx { |
| 200 |
my ($self, $res, $host, $transaction) = @_; |
| 201 |
|
| 202 |
my @mx = mx($res, $host); |
| 203 |
if (!scalar @mx) { # no mx records |
| 204 |
$self->adjust_karma(-1); |
| 205 |
$self->log(LOGINFO, "$host has no MX"); |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
foreach my $mx (@mx) { |
| 210 |
|
| 211 |
# if any MX is valid, then we consider the domain resolvable |
| 212 |
if ($self->mx_address_resolves($mx->exchange, $host)) { |
| 213 |
$self->log(LOGINFO, "pass, $host has MX at " . $mx->exchange); |
| 214 |
$transaction->notes('resolvable_fromhost', 'mx'); |
| 215 |
return 1; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
# if there are MX records, and we got here, none are valid |
| 220 |
#$self->log(LOGINFO, "fail, invalid MX for $host"); |
| 221 |
$transaction->notes('resolvable_fromhost', "invalid MX for $host"); |
| 222 |
$self->adjust_karma(-1); |
| 223 |
return -1; |
| 224 |
} |
| 225 |
|
| 226 |
sub get_host_records { |
| 227 |
my ($self, $res, $host, $transaction) = @_; |
| 228 |
|
| 229 |
my @answers; |
| 230 |
my $query = $res->search($host); |
| 231 |
|
| 232 |
if ($query) { |
| 233 |
foreach my $rrA ($query->answer) { |
| 234 |
push(@answers, $rrA); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if ($has_ipv6) { |
| 239 |
$query = $res->search($host, 'AAAA'); |
| 240 |
if ($query) { |
| 241 |
foreach my $rrAAAA ($query->answer) { |
| 242 |
push(@answers, $rrAAAA); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if (!scalar @answers) { |
| 248 |
if ($res->errorstring ne 'NXDOMAIN') { |
| 249 |
$self->log(LOGWARN, "fail, query for $host, ", $res->errorstring); |
| 250 |
} |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
return @answers; |
| 255 |
} |
| 256 |
|
| 257 |
sub mx_address_resolves { |
| 258 |
my ($self, $name, $fromhost) = @_; |
| 259 |
|
| 260 |
# IP in MX |
| 261 |
return $self->ip_is_valid($name) if ip_is_ipv4($name) || ip_is_ipv6($name); |
| 262 |
|
| 263 |
my $res = new Net::DNS::Resolver(dnsrch => 0); |
| 264 |
my @mx_answers; |
| 265 |
my $query = $res->search($name, 'A'); |
| 266 |
if ($query) { |
| 267 |
foreach my $rrA ($query->answer) { |
| 268 |
push(@mx_answers, $rrA); |
| 269 |
} |
| 270 |
} |
| 271 |
if ($has_ipv6) { |
| 272 |
my $query = $res->search($name, 'AAAA'); |
| 273 |
if ($query) { |
| 274 |
foreach my $rrAAAA ($query->answer) { |
| 275 |
push(@mx_answers, $rrAAAA); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
if (!@mx_answers) { |
| 280 |
if ($res->errorstring eq 'NXDOMAIN') { |
| 281 |
$self->log(LOGWARN, "fail, query for $fromhost, ", |
| 282 |
$res->errorstring); |
| 283 |
} |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
foreach my $rr (@mx_answers) { |
| 288 |
next if ($rr->type ne 'A' && $rr->type ne 'AAAA'); |
| 289 |
return $self->ip_is_valid($rr->address); |
| 290 |
} |
| 291 |
|
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
sub populate_invalid_networks { |
| 296 |
my $self = shift; |
| 297 |
|
| 298 |
foreach my $i ($self->qp->config("invalid_resolvable_fromhost")) { |
| 299 |
$i =~ s/^\s*//; # trim leading spaces |
| 300 |
$i =~ s/\s*$//; # trim trailing spaces |
| 301 |
if ($i =~ m#^((\d{1,3}\.){3}\d{1,3})/(\d\d?)#) { |
| 302 |
$invalid{$1} = $3; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|