Debian Codegraph @

plugins/fcrdns

raw
1 #!perl -w
2
3 =head1 NAME
4
5 Forward Confirmed RDNS - http://en.wikipedia.org/wiki/FCrDNS
6
7 =head1 DESCRIPTION
8
9 Determine if the SMTP sender has matching forward and reverse DNS.
10
11 Sets the connection note fcrdns.
12
13 =head1 WHY IT WORKS
14
15 The reverse DNS of zombie PCs is out of the spam operators control. Their
16 only way to pass this test is to limit themselves to hosts with matching
17 forward and reverse DNS. At present, this presents a significant hurdle.
18
19 =head1 VALIDATION TESTS
20
21 =over 4
22
23 =item has_reverse_dns
24
25 Determine if the senders IP address resolves to a hostname.
26
27 =item has_forward_dns
28
29 If the remote IP has a PTR hostname(s), see if that host has an A or AAAA. If
30 so, see if any of the host IPs (A or AAAA records) match the remote IP.
31
32 Since the dawn of SMTP, having matching DNS has been a standard expected and
33 oft required of mail servers. While requiring matching DNS is prudent,
34 requiring an exact match will reject valid email. This often hinders the
35 use of FcRDNS. While testing this plugin, I noticed that mx0.slc.paypal.com
36 sends mail from an IP that reverses to mx1.slc.paypal.com. While that's
37 technically an error, so too would rejecting that connection.
38
39 To avoid false positives, matches are extended to the first 3 octets of the
40 IP and the last two labels of the FQDN. The following are considered a match:
41
42 192.0.1.2, 192.0.1.3
43
44 foo.example.com, bar.example.com
45
46 This allows FcRDNS to be used without rejecting mail from orgs with
47 pools of servers where the HELO name and IP don't exactly match. This list
48 includes Yahoo, Gmail, PayPal, cheaptickets.com, exchange.microsoft.com, etc.
49
50 =back
51
52 =head1 CONFIGURATION
53
54 =head2 timeout [seconds]
55
56 Default: 5
57
58 The number of seconds before DNS queries timeout.
59
60 =head2 reject [ 0 | 1 | naughty ]
61
62 Default: 1
63
64 0: do not reject
65
66 1: reject
67
68 naughty: naughty plugin handles rejection
69
70 =head2 reject_type [ temp | perm | disconnect ]
71
72 Default: disconnect
73
74 What type of rejection should be sent? See docs/config.pod
75
76 =head2 loglevel
77
78 Adjust the quantity of logging for this plugin. See docs/logging.pod
79
80
81 =head1 RFC 1912, RFC 5451
82
83 From Wikipedia summary:
84
85 1. First a reverse DNS lookup (PTR query) is performed on the IP address, which returns a list of zero or more PTR records. (has_reverse_dns)
86
87 2. For each domain name returned in the PTR query results, a regular 'forward' DNS lookup (type A or AAAA query) is then performed on that domain name. (has_forward_dns)
88
89 3. Any A or AAAA record returned by the second query is then compared against the original IP address (check_ip_match), and if there is a match, then the FCrDNS check passes.
90
91 =head1 iprev
92
93 # https://www.ietf.org/rfc/rfc5451.txt
94
95 2.4.3. "iprev" Results
96
97 The result values are used by the "iprev" method, defined in
98 Section 3, are as follows:
99
100 pass: The DNS evaluation succeeded, i.e., the "reverse" and
101 "forward" lookup results were returned and were in agreement.
102
103 fail: The DNS evaluation failed. In particular, the "reverse" and
104 "forward" lookups each produced results but they were not in
105 agreement, or the "forward" query completed but produced no
106 result, e.g., a DNS RCODE of 3, commonly known as NXDOMAIN, or an
107 RCODE of 0 (NOERROR) in a reply containing no answers, was
108 returned.
109
110 temperror: The DNS evaluation could not be completed due to some
111 error that is likely transient in nature, such as a temporary DNS
112 error, e.g., a DNS RCODE of 2, commonly known as SERVFAIL, or
113 other error condition resulted. A later attempt may produce a
114 final result.
115
116 permerror: The DNS evaluation could not be completed because no PTR
117 data are published for the connecting IP address, e.g., a DNS
118 RCODE of 3, commonly known as NXDOMAIN, or an RCODE of 0 (NOERROR)
119 in a reply containing no answers, was returned. This prevented
120 completion of the evaluation.
121
122 =head1 AUTHOR
123
124 2013 - Matt Simerson
125
126 =cut
127
128 use strict;
129 use warnings;
130
131 use Qpsmtpd::Constants;
132
133 sub register {
134 my ($self, $qp) = (shift, shift);
135 $self->{_args} = {@_};
136 $self->{_args}{reject_type} = 'temp';
137 $self->{_args}{timeout} ||= 5;
138 $self->{_args}{ptr_hosts} = {};
139
140 if (!defined $self->{_args}{reject}) {
141 $self->{_args}{reject} = 0;
142 }
143
144 $self->init_resolver() or return;
145
146 $self->register_hook('connect', 'connect_handler');
147 }
148
149 sub connect_handler {
150 my ($self) = @_;
151
152 return DECLINED if $self->is_immune();
153
154 # run a couple cheap tests before the more expensive DNS tests
155 foreach my $test (qw/ invalid_localhost is_not_fqdn /) {
156 $self->$test() or return DECLINED;
157 }
158
159 $self->has_reverse_dns() or return DECLINED;
160 $self->has_forward_dns() or return DECLINED;
161
162 $self->log(LOGINFO, "pass");
163 return DECLINED;
164 }
165
166 sub invalid_localhost {
167 my ($self) = @_;
168 return 1 if lc $self->qp->connection->remote_host ne 'localhost';
169 if ( $self->qp->connection->remote_ip ne '127.0.0.1'
170 && $self->qp->connection->remote_ip ne '::1')
171 {
172 $self->adjust_karma(-1);
173 $self->log(LOGINFO, "fail, not localhost");
174 return;
175 }
176 $self->adjust_karma(1);
177 $self->log(LOGDEBUG, "pass, is localhost");
178 return 1;
179 }
180
181 sub is_not_fqdn {
182 my ($self) = @_;
183 my $host = $self->qp->connection->remote_host or return 1;
184 return 1 if $host eq 'Unknown'; # QP assigns this to a "no DNS result"
185
186 # Since QP looked it up, perform some quick validation
187 if ($host !~ /\./) { # has no dots
188 $self->adjust_karma(-1);
189 $self->log(LOGINFO, "fail, not FQDN");
190 return;
191 }
192 if ($host =~ /[^a-zA-Z0-9\-\.]/) {
193 $self->adjust_karma(-1);
194 $self->log(LOGINFO, "fail, invalid FQDN chars");
195 return;
196 }
197 return 1;
198 }
199
200 sub has_reverse_dns {
201 my ($self) = @_;
202
203 my $res = $self->init_resolver();
204 my $ip = $self->qp->connection->remote_ip;
205
206 my $query = $res->query($ip, 'PTR') or do {
207 if ($res->errorstring eq 'NXDOMAIN') {
208 $self->adjust_karma(-1);
209 $self->store_auth_results("iprev=permerror");
210 $self->log(LOGINFO, "fail, no rDNS: " . $res->errorstring);
211 return;
212 }
213 if ( $res->errorstring eq 'SERVFAIL' ) {
214 $self->log(LOGINFO, "fail, error getting rDNS: " . $res->errorstring);
215 $self->store_auth_results("iprev=temperror");
216 }
217 elsif ( $res->errorstring eq 'NOERROR' ) {
218 $self->log(LOGINFO, "fail, no PTR (NOERROR)" );
219 $self->store_auth_results("iprev=permerror");
220 }
221 else {
222 $self->store_auth_results("iprev=fail");
223 $self->log(LOGINFO, "fail, error getting rDNS: " . $res->errorstring);
224 };
225 return;
226 };
227
228 my $hits = 0;
229 $self->{_args}{ptr_hosts} = {}; # reset hash
230 for my $rr ($query->answer) {
231 next if $rr->type ne 'PTR';
232 $hits++;
233 $self->{_args}{ptr_hosts}{$rr->ptrdname} = 1;
234 $self->log(LOGDEBUG, "PTR: " . $rr->ptrdname);
235 }
236 if (!$hits) {
237 $self->adjust_karma(-1);
238 $self->log(LOGINFO, "fail, no PTR records");
239 $self->store_auth_results("iprev=permerror");
240 return;
241 }
242
243 $self->log(LOGDEBUG, "has rDNS");
244 return 1;
245 }
246
247 sub has_forward_dns {
248 my ($self) = @_;
249
250 my $res = $self->init_resolver();
251
252 foreach my $host (keys %{$self->{_args}{ptr_hosts}}) {
253
254 $host .= '.' if '.' ne substr($host, -1, 1); # fully qualify name
255 my $query = $res->query($host) or do {
256 if ($res->errorstring eq 'NXDOMAIN') {
257 $self->store_auth_results("iprev=permerror");
258 $self->log(LOGDEBUG, "host $host does not exist");
259 next;
260 }
261 $self->store_auth_results("iprev=fail");
262 $self->log(LOGDEBUG, "query for $host failed (",
263 $res->errorstring, ")");
264 next;
265 };
266
267 my $hits = 0;
268 foreach my $rr ($query->answer) {
269 next unless $rr->type =~ /^(?:A|AAAA)$/;
270 $hits++;
271 $self->check_ip_match($rr->address) and return 1;
272 }
273 if ($hits) {
274 $self->store_auth_results("iprev=fail");
275 $self->log(LOGDEBUG, "PTR host has forward DNS") if $hits;
276 return 1;
277 }
278 }
279 $self->adjust_karma(-1);
280 $self->store_auth_results("iprev=fail");
281 $self->log(LOGINFO, "fail, no PTR hosts have forward DNS");
282 return;
283 }
284
285 sub check_ip_match {
286 my $self = shift;
287 my $ip = shift or return;
288
289 if ($ip eq $self->qp->connection->remote_ip) {
290 $self->log(LOGDEBUG, "forward ip match");
291 $self->store_auth_results("iprev=pass");
292 $self->adjust_karma(1);
293 return 1;
294 }
295
296 # TODO: make this IPv6 compatible
297 my $dns_net = join('.', (split(/\./, $ip))[0, 1, 2]);
298 my $rem_net =
299 join('.', (split(/\./, $self->qp->connection->remote_ip))[0, 1, 2]);
300
301 if ($dns_net eq $rem_net) {
302 $self->log(LOGNOTICE, "forward network match");
303 $self->store_auth_results("iprev=pass");
304 return 1;
305 }
306 return;
307 }
308