Debian Codegraph @

plugins/helo

raw
1 #!perl -w
2
3 =head1 NAME
4
5 helo - validate the HELO message presented by a connecting host.
6
7 =head1 DESCRIPTION
8
9 Validate the HELO hostname. This plugin includes a suite of optional tests,
10 selectable by the I<policy> setting. The policy section details which tests
11 are enforced by each policy option.
12
13 It sets the connection notes helo_forward_match and helo_reverse_match when
14 I<policy rfc> or I<policy strict> are used.
15
16 Adds an X-HELO header with the HELO hostname to the message.
17
18 Using I<policy rfc> will reject a very large portion of the spam from hosts
19 that have yet to get blacklisted.
20
21 =head1 WHY IT WORKS
22
23 The reverse DNS of the zombie PCs is out of the spam operators control. Their
24 only way to get past these tests is to limit themselves to hosts with matching
25 forward and reverse DNS, and then use the proper HELO hostname when spamming.
26 At present, this presents a very high hurdle.
27
28 =head1 HELO VALIDATION TESTS
29
30 =over 4
31
32 =item is_in_badhelo
33
34 Matches in the I<badhelo> config file, including yahoo.com and aol.com, which
35 neither the real Yahoo or the real AOL use, but which spammers use a lot.
36
37 Like qmail with the qregex patch, the B<badhelo> file can also contain perl
38 regular expressions. In addition to normal regexp processing, a pattern can
39 start with a ! character, and get a negated (!~) match.
40
41 =item invalid_localhost
42
43 Assure that if a sender uses the 'localhost' hostname, they are coming from
44 the localhost IP.
45
46 =item is_plain_ip
47
48 Disallow plain IP addresses. They are neither a FQDN nor an address literal.
49
50 =item is_address_literal [N.N.N.N]
51
52 An address literal (an IP enclosed in brackets) is legal but rarely, if ever,
53 encountered from legit senders.
54
55 =item is_forged_literal
56
57 If a literal is presented, make sure it matches the senders IP.
58
59 =item is_not_fqdn
60
61 Makes sure the HELO hostname contains at least one dot and has only those
62 characters specifically allowed in domain names (RFC 1035).
63
64 =item no_forward_dns
65
66 Make sure the HELO hostname resolves.
67
68 =item no_reverse_dns
69
70 Make sure the senders IP address resolves to a hostname.
71
72 =item no_matching_dns
73
74 Make sure the HELO hostname has an A or AAAA record that matches the senders
75 IP address, and make sure that the senders IP has a PTR that resolves to the
76 HELO hostname.
77
78 Per RFC 5321 section 4.1.4, it is impermissible to block a message I<soley>
79 on the basis of the HELO hostname not matching the senders IP.
80
81 Since the dawn of SMTP, having matching DNS has been a minimum standard
82 expected and oft required of mail servers. While requiring matching DNS is
83 prudent, requiring an exact match will reject valid email. While testing this
84 plugin with rejection disabled, I noticed that mx0.slc.paypal.com sends email
85 from an IP that reverses to mx1.slc.paypal.com. While that's technically an
86 error, I believe it's an error to reject mail based on it. Especially since
87 SLD and TLD match.
88
89 To avoid snagging false positives, matches are extended to the first
90 3 octets of the IP and the last two labels of the FQDN. The following are
91 considered a match:
92
93 192.0.1.2, 192.0.1.3
94
95 foo.example.com, bar.example.com
96
97 This allows I<no_matching_dns> to be used without rejecting mail from orgs with
98 pools of servers where the HELO name and IP don't exactly match. This list
99 includes Yahoo, Gmail, PayPal, cheaptickets.com, exchange.microsoft.com, and
100 likely many more.
101
102 =back
103
104 =head1 CONFIGURATION
105
106 =head2 policy [ lenient | rfc | strict ]
107
108 Default: lenient
109
110 =head3 lenient
111
112 Runs the following tests: is_in_badhelo, invalid_localhost,
113 is_forged_literal, and is_plain_ip.
114
115 This setting is lenient enough not to cause problems for your Windows users.
116 It is comparable to running check_spamhelo, but with the addition of regexp
117 support, the prevention of forged localhost, forged IP literals, and plain
118 IPs.
119
120 =head3 rfc
121
122 Per RFC 2821, the HELO hostname is the FQDN of the sending server or an
123 address literal. When I<policy rfc> is selected, all the lenient checks and
124 the following are tested: is_not_fqdn, no_forward_dns, and no_reverse_dns.
125
126 If you have Windows users that send mail via your server, do not choose
127 I<policy rfc> without setting I<reject> to 0 or naughty.
128 Windows PCs often send unqualified HELO names and will have trouble
129 sending mail. The B<naughty> plugin defers the rejection, giving the user
130 the opportunity to authenticate and bypass the rejection.
131
132 =head3 strict
133
134 Strict includes all the RFC tests and the following: no_matching_dns, and
135 is_address_literal.
136
137 I have yet to see an address literal being used by a hammy sender. But I am
138 not certain that blocking them all is prudent.
139
140 It is recommended that I<policy strict> be used with <reject 0> and that you
141 examine your logs for false positives.
142
143 =head2 badhelo
144
145 Add domains, hostnames, or perl regexp patterns to the F<badhelo> config
146 file; one per line.
147
148 =head2 timeout [seconds]
149
150 Default: 5
151
152 The number of seconds before DNS queries timeout.
153
154 =head2 reject [ 0 | 1 | naughty ]
155
156 Default: 1
157
158 0: do not reject
159
160 1: reject
161
162 naughty: naughty plugin handles rejection
163
164 =head2 reject_type [ temp | perm | disconnect ]
165
166 Default: disconnect
167
168 What type of rejection should be sent? See docs/config.pod
169
170 =head2 loglevel
171
172 Adjust the quantity of logging for this plugin. See docs/logging.pod
173
174 =head1 RFC 2821
175
176 =head2 4.1.1.1
177
178 The HELO hostname "...contains the fully-qualified domain name of the SMTP
179 client if one is available. In situations in which the SMTP client system
180 does not have a meaningful domain name (e.g., when its address is dynamically
181 allocated and no reverse mapping record is available), the client SHOULD send
182 an address literal (see section 4.1.3), optionally followed by information
183 that will help to identify the client system."
184
185 =head2 2.3.5
186
187 The domain name, as described in this document and in [22], is the
188 entire, fully-qualified name (often referred to as an "FQDN"). A domain name
189 that is not in FQDN form is no more than a local alias. Local aliases MUST
190 NOT appear in any SMTP transaction.
191
192
193 =head1 RFC 5321
194
195 =head2 4.1.4
196
197 An SMTP server MAY verify that the domain name argument in the EHLO
198 command actually corresponds to the IP address of the client.
199 However, if the verification fails, the server MUST NOT refuse to
200 accept a message on that basis. Information captured in the
201 verification attempt is for logging and tracing purposes. Note that
202 this prohibition applies to the matching of the parameter to its IP
203 address only; see Section 7.9 for a more extensive discussion of
204 rejecting incoming connections or mail messages.
205
206 =head1 TODO
207
208 is_forged_literal, if the forged IP is an internal IP, it's likely one
209 of our clients that should have authenticated. Perhaps when we check back
210 later in data_post, if they have added relay_client, then give back the
211 karma.
212
213 =head1 AUTHOR
214
215 2012 - Matt Simerson
216
217 =head1 ACKNOWLEDGEMENTS
218
219 badhelo processing from check_badhelo plugin
220
221 badhelo regex processing idea from qregex patch
222
223 additional check ideas from Haraka helo plugin
224
225 =cut
226
227 use strict;
228 use warnings;
229
230 use Qpsmtpd::Constants;
231
232 sub register {
233 my ($self, $qp) = (shift, shift);
234 $self->{_args} = {@_};
235
236 $self->{_args}{reject_type} = 'disconnect';
237 $self->{_args}{policy} ||= 'lenient';
238 $self->{_args}{dns_timeout} ||= $self->{_args}{timeout} || 5;
239
240 if (!defined $self->{_args}{reject}) {
241 $self->{_args}{reject} = 1;
242 }
243 $self->populate_tests();
244 $self->init_resolver() or return;
245
246 $self->register_hook('helo', 'helo_handler');
247 $self->register_hook('ehlo', 'helo_handler');
248 $self->register_hook('data_post', 'data_post_handler');
249 }
250
251 sub helo_handler {
252 my ($self, $transaction, $host) = @_;
253
254 if (!$host) {
255 $self->log(LOGINFO, "fail, tolerated, no helo host");
256 $self->adjust_karma(-2);
257 return DECLINED;
258 }
259
260 return DECLINED if $self->is_immune();
261
262 foreach my $test (@{$self->{_helo_tests}}) {
263 my @err = $self->$test($host);
264 if (scalar @err) {
265 $self->adjust_karma(-1);
266 return $self->get_reject(@err);
267 }
268 }
269
270 $self->log(LOGINFO, "pass");
271 return DECLINED;
272 }
273
274 sub data_post_handler {
275 my ($self, $transaction) = @_;
276
277 $transaction->header->delete('X-HELO');
278 $transaction->header->add('X-HELO', $self->qp->connection->hello_host, 0);
279
280 return (DECLINED);
281 }
282
283 sub populate_tests {
284 my $self = shift;
285
286 my $policy = $self->{_args}{policy};
287 @{$self->{_helo_tests}} =
288 qw/ is_in_badhelo invalid_localhost is_forged_literal is_plain_ip /;
289
290 if ($policy eq 'rfc' || $policy eq 'strict') {
291 push @{$self->{_helo_tests}},
292 qw/ is_not_fqdn no_forward_dns no_reverse_dns /;
293 }
294
295 if ($policy eq 'strict') {
296 push @{$self->{_helo_tests}}, qw/ is_address_literal no_matching_dns /;
297 }
298 }
299
300 sub is_in_badhelo {
301 my ($self, $host) = @_;
302
303 my $error = "I do not believe you are $host.";
304
305 $host = lc $host;
306 foreach my $bad ($self->qp->config('badhelo')) {
307 if ($bad =~ /[\{\}\[\]\(\)\^\$\|\*\+\?\\\!]/) { # it's a regexp
308 return $self->is_regex_match($host, $bad);
309 }
310 if ($host eq lc $bad) {
311 return ($error, "in badhelo");
312 }
313 }
314 return;
315 }
316
317 sub is_regex_match {
318 my ($self, $host, $pattern) = @_;
319
320 my $error = "Your HELO hostname is not allowed";
321
322 #$self->log( LOGDEBUG, "is regex ($pattern)");
323 if (substr($pattern, 0, 1) eq '!') {
324 $pattern = substr $pattern, 1;
325 if ($host !~ /$pattern/) {
326
327 #$self->log( LOGDEBUG, "matched ($pattern)");
328 return ($error, "badhelo pattern match ($pattern)");
329 }
330 return;
331 }
332 if ($host =~ /$pattern/) {
333
334 #$self->log( LOGDEBUG, "matched ($pattern)");
335 return ($error, "badhelo pattern match ($pattern)");
336 }
337 return;
338 }
339
340 sub invalid_localhost {
341 my ($self, $host) = @_;
342 return if lc $host ne 'localhost';
343 if ($self->qp->connection->remote_ip ne '127.0.0.1') {
344
345 #$self->log( LOGINFO, "fail, not localhost" );
346 return ("You are not localhost", "invalid localhost");
347 }
348 $self->log(LOGDEBUG, "pass, is localhost");
349 return;
350 }
351
352 sub is_plain_ip {
353 my ($self, $host) = @_;
354 return if $host =~ /[^\d\.]+/; # has chars other than digits and a dot
355 return if $host !~ m/^(\d{1,3}\.){3}\d{1,3}$/;
356
357 $self->log(LOGDEBUG, "fail, plain IP");
358 return ("Plain IP is invalid HELO hostname (RFC 2821)", "plain IP");
359 }
360
361 sub is_address_literal {
362 my ($self, $host) = @_;
363 return if $host !~ m/^\[(\d{1,3}\.){3}\d{1,3}\]$/;
364
365 $self->log(LOGDEBUG, "fail, bracketed IP");
366 return ("RFC 2821 allows an address literal, but we do not",
367 "bracketed IP");
368 }
369
370 sub is_forged_literal {
371 my ($self, $host) = @_;
372 return if $host !~ m/^\[(\d{1,3}\.){3}\d{1,3}\]$/;
373
374 # should we add exceptions for reserved internal IP space? (192.168,10., etc?)
375 $host = substr $host, 1, -1;
376 return if $host eq $self->qp->connection->remote_ip;
377 return ("Forged IPs not accepted here", "forged IP literal");
378 }
379
380 sub is_not_fqdn {
381 my ($self, $host) = @_;
382 return if $host =~ m/^\[(\d{1,3}\.){3}\d{1,3}\]$/; # address literal, skip
383 if ($host !~ /\./) { # has no dots
384 return ("HELO name is not fully qualified. Read RFC 2821", "not FQDN");
385 }
386 if ($host =~ /[^a-zA-Z0-9\-\.]/) {
387 return ("HELO name contains invalid FQDN characters. Read RFC 1035",
388 "invalid FQDN chars");
389 }
390 return;
391 }
392
393 sub no_forward_dns {
394 my ($self, $host) = @_;
395
396 return if $self->is_address_literal($host);
397
398 my $res = $self->init_resolver();
399
400 $host = "$host." if $host !~ /\.$/; # fully qualify name
401 my $query = $res->search($host);
402
403 if (!$query) {
404 if ($res->errorstring eq 'NXDOMAIN') {
405 return ("HELO hostname does not exist", "no such host");
406 }
407 $self->log(LOGERROR, "skip, query failed (", $res->errorstring, ")");
408 return;
409 }
410 my $hits = 0;
411 foreach my $rr ($query->answer) {
412 next unless $rr->type =~ /^(?:A|AAAA)$/;
413 $self->check_ip_match($rr->address);
414 $hits++;
415 last if $self->connection->notes('helo_forward_match');
416 }
417 if ($hits) {
418 $self->log(LOGDEBUG, "pass, forward DNS") if $hits;
419 return;
420 }
421 return ("HELO hostname did not resolve", "no forward DNS");
422 }
423
424 sub no_reverse_dns {
425 my ($self, $host, $ip) = @_;
426
427 my $res = $self->init_resolver();
428 $ip ||= $self->qp->connection->remote_ip;
429
430 my $query = $res->query($ip) or do {
431 if ($res->errorstring eq 'NXDOMAIN') {
432 return ("no rDNS for $ip", "no rDNS");
433 }
434 $self->log(LOGINFO, $res->errorstring);
435 return ("error getting reverse DNS for $ip",
436 "rDNS " . $res->errorstring);
437 };
438
439 my $hits = 0;
440 for my $rr ($query->answer) {
441 next if $rr->type ne 'PTR';
442 $self->log(LOGDEBUG, "PTR: " . $rr->ptrdname);
443 $self->check_name_match(lc $rr->ptrdname, lc $host);
444 $hits++;
445 }
446 if ($hits) {
447 $self->log(LOGDEBUG, "has rDNS");
448 return;
449 }
450 return ("no reverse DNS for $ip", "no rDNS");
451 }
452
453 sub no_matching_dns {
454 my ($self, $host) = @_;
455
456 # this is called iprev, or "Forward-confirmed reverse DNS" and is discussed
457 # in RFC 5451. FCrDNS is done for the remote IP in the fcrdns plugin. Here
458 # we do it on the HELO hostname.
459 # consider adding status to Authentication-Results header
460
461 if ( $self->connection->notes('helo_forward_match')
462 && $self->connection->notes('helo_reverse_match'))
463 {
464 $self->log(LOGDEBUG, "foward and reverse match");
465 $self->adjust_karma(1); # a perfect match
466 return;
467 }
468
469 if ($self->connection->notes('helo_forward_match')) {
470 $self->log(LOGDEBUG, "name matches IP");
471 return;
472 }
473 if ($self->connection->notes('helo_reverse_match')) {
474 $self->log(LOGDEBUG, "reverse matches name");
475 return;
476 }
477
478 $self->log(LOGINFO, "fail, no forward or reverse DNS match");
479 return ("That HELO hostname fails FCrDNS", "no matching DNS");
480 }
481
482 sub check_ip_match {
483 my $self = shift;
484 my $ip = shift or return;
485
486 if ($ip eq $self->qp->connection->remote_ip) {
487 $self->log(LOGDEBUG, "forward ip match");
488 $self->connection->notes('helo_forward_match', 1);
489 return;
490 }
491
492 my $dns_net = join('.', (split(/\./, $ip))[0, 1, 2]);
493 my $rem_net =
494 join('.', (split(/\./, $self->qp->connection->remote_ip))[0, 1, 2]);
495
496 if ($dns_net eq $rem_net) {
497 $self->log(LOGNOTICE, "forward network match");
498 $self->connection->notes('helo_forward_match', 1);
499 }
500 }
501
502 sub check_name_match {
503 my $self = shift;
504 my ($dns_name, $helo_name) = @_;
505
506 return if !$dns_name;
507 return if split(/\./, $dns_name) < 2; # not a FQDN
508
509 if ($dns_name eq $helo_name) {
510 $self->log(LOGDEBUG, "reverse name match");
511 $self->connection->notes('helo_reverse_match', 1);
512 return;
513 }
514
515 my $dns_dom = join('.', (split(/\./, $dns_name))[-2, -1]);
516 my $helo_dom = join('.', (split(/\./, $helo_name))[-2, -1]);
517
518 if ($dns_dom eq $helo_dom) {
519 $self->log(LOGNOTICE, "reverse domain match");
520 $self->connection->notes('helo_reverse_match', 1);
521 }
522 }
523