Milter/Module/SPF.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 205 CC 12 SCAR 23.7
CC
12
Cov
0%
envfrom_callback
42.0
ignore_softfail
6.0
ignore_tempfail
6.0
CRAP 10.7
1 # $Id: SPF.pm,v 1.3 2006/03/22 15:48:23 tvierling Exp $
2 #
3 # Copyright (c) 2002-2006 Todd Vierling <tv@pobox.com> <tv@duh.org>
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 #
16 # 3. Neither the name of the author nor the names of contributors may be used
17 # to endorse or promote products derived from this software without specific
18 # prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31  
32 package Mail::Milter::Module::SPF;
33  
34 use 5.006;
35 use base Exporter;
36 use base Mail::Milter::Object;
37  
38 use strict;
39 use warnings;
40  
41 use Carp;
42 use Mail::SPF::Query 1.996;
43 use Sendmail::Milter 0.18; # get needed constants
44 use Socket;
45 use UNIVERSAL;
46  
47 our $VERSION = '0.01';
48  
49 =pod
50  
51 =head1 NAME
52  
53 Mail::Milter::Module::SPF - milter to use Sender Policy Framework for accept/reject
54  
55 =head1 SYNOPSIS
56  
57 use Mail::Milter::Module::SPF;
58  
59 my $milter = new Mail::Milter::Module::SPF;
60  
61 my $milter2 = &SPF; # convenience
62  
63 =head1 WARNING
64  
65 This module is known to have major problems. It should NOT be used in a
66 production environment at this time.
67  
68 =head1 DESCRIPTION
69  
70 This milter module rejects any mail from a sender (in the MAIL FROM part of
71 the SMTP transaction, not in the From: header) if that sender's domain
72 publishes a Sender Policy Framework (SPF) record denying access to the
73 connection host.
74  
75 The pass/fail result from SPF is configurable as to whether mail will be
76 accepted or rejected immediately. By default, this module will reject a
77 sender whose SPF lookup returns "fail", and allow others to pass, setting a
78 Received-SPF: header with the SPF lookup result. See the methods below for
79 knobs tunable for different situations.
80  
81 This module requires the Mail::SPF::Query module (version 1.996 or later) to
82 be installed in order to fetch the SPF record.
83  
84 Be sure to read BUGS at the bottom of this documentation for a list of
85 currently unsupported features.
86  
87 =head1 METHODS
88  
89 =over 4
90  
91 =cut
92  
93 our @EXPORT = qw(&SPF);
94  
95 sub SPF {
96 new Mail::Milter::Module::SPF(@_);
97 }
98  
99 =pod
100  
101 =item new()
102  
103 Creates a SPF object. There are no arguments to configure this module from
104 the constructor; see the methods below for changeable options.
105  
106 =cut
107  
108 sub new ($) {
109 my $this = Mail::Milter::Object::new(shift);
110  
111 $this->{_addheader} = 'Received-SPF';
112 $this->{_ignoresoftfail} = 1;
113 $this->{_ignoretempfail} = 0;
114 $this->{_message} = 'Mail from %M failed SPF check: %E';
115 $this->{_spfopts} = {};
116 $this->{_whitelistpass} = 0;
117  
118 $this;
119 }
120  
121 =pod
122  
123 =item add_header(HEADERNAME)
124  
125 Tell this module to append a header on messages which are not rejected,
126 indicating the SPF result value and a comment explaining the result. By
127 default, this is enabled with the standard header name C<Received-SPF>.
128  
129 Note that this header is not appended if C<whitelist_pass(1)> is in effect,
130 and a sender is whitelisted by a SPF "pass" result. This is because
131 whitelisting skips all other mail processing, so this module cannot add
132 headers at the end of processing.
133  
134 If HEADERNAME is undef, the header is disabled and will not be appended to
135 any message.
136  
137 This method returns a reference to the object itself, allowing this method
138 call to be chained.
139  
140 =cut
141  
142 sub add_header ($$) {
143 my $this = shift;
144 my $headername = shift;
145  
146 $this->{_addheader} = $headername;
147  
148 $this;
149 }
150  
151 =pod
152  
153 =item ignore_softfail(FLAG)
154  
155 If FLAG is 0, a SPF record resulting in "softfail" will be rejected as if
156 the result were "fail".
157  
158 If FLAG is 1 (the default), a "softfail" is ignored, treated as if it
159 returned "neutral".
160  
161 This method returns a reference to the object itself, allowing this method
162 call to be chained.
163  
164 =cut
165  
166 sub ignore_softfail ($$) {
167 my $this = shift;
168 my $flag = shift;
169  
170 croak 'ignore_softfail: flag argument is undef' unless defined($flag);
171 $this->{_ignoresoftfail} = $flag;
172  
173 $this;
174 }
175  
176 =pod
177  
178 =item ignore_tempfail(FLAG)
179  
180 If FLAG is 0 (the default), a DNS lookup which fails the underlying DNS
181 query (a SPF "error" result) will cause the milter to return a temporary
182 failure result (SMFIS_TEMPFAIL).
183  
184 If FLAG is 1, a temporary DNS failure will be treated as if the lookup
185 resulted in an empty record set (and thus a SPF "none" result).
186  
187 This method returns a reference to the object itself, allowing this method
188 call to be chained.
189  
190 =cut
191  
192 sub ignore_tempfail ($$) {
193 my $this = shift;
194 my $flag = shift;
195  
196 croak 'ignore_tempfail: flag argument is undef' unless defined($flag);
197 $this->{_ignoretempfail} = $flag;
198  
199 $this;
200 }
201  
202 =pod
203  
204 =item local_rules(RULETEXT)
205  
206 Add one or more SPF rules to try before a "-all" or "?all" record is
207 encountered, in an attempt to validate the mail. This is useful for
208 enumerating secondary MX servers or non-SRS-compliant forwarding systems
209 which send mail to this host.
210  
211 The rules must be contained in a single string, separated by spaces.
212  
213 This method returns a reference to the object itself, allowing this method
214 call to be chained.
215  
216 =cut
217  
218 sub local_rules ($$) {
219 my $this = shift;
220 my $locals = shift;
221  
222 if (defined($this->{_spfopts}{local})) {
223 $this->{_spfopts}{local} .= " $locals";
224 } else {
225 $this->{_spfopts}{local} = $locals;
226 }
227  
228 $this;
229 }
230  
231 =pod
232  
233 =item set_message(MESSAGE)
234  
235 Sets the message used when rejecting messages. This string may contain the
236 substring C<%M>, which will be replaced by the matching e-mail address,
237 and/or C<%E>, which will be replaced by the SPF explanatory URL and text.
238  
239 This method returns a reference to the object itself, allowing this method
240 call to be chained.
241  
242 =cut
243  
244 sub set_message ($$) {
245 my $this = shift;
246  
247 $this->{_message} = shift;
248  
249 $this;
250 }
251  
252 =pod
253  
254 =item whitelist_pass(FLAG)
255  
256 If FLAG is 0 (the default), a SPF "pass" result will be treated like any
257 other non-failure result, allowing the message to pass through without other
258 special handling.
259  
260 If FLAG is 1, a SPF "pass" result will cause this module to return
261 SMFIS_ACCEPT, a value that is used by the accept_break(1) behavior of
262 Mail::Milter::Chain, to ignore the results of other modules in the chain.
263 Note that because SPF does not accept or reject until the MAIL FROM: stage,
264 it may be necessary to embed a DeferToRCPT wrapper into the whitelisting
265 chain. For example,
266  
267 use Mail::Milter::Chain;
268 use Mail::Milter::Module::SPF;
269 use Mail::Milter::Wrapper::DeferToRCPT;
270  
271 my $spf_whitelisted_chain = new Mail::Milter::Chain(
272 &SPF->whitelist_pass(1),
273 &DeferToRCPT(new Mail::Milter::Chain(
274 $milter1, ...
275 ))
276 )->accept_break(1);
277  
278 This method returns a reference to the object itself, allowing this method
279 call to be chained.
280  
281 =cut
282  
283 sub whitelist_pass ($$) {
284 my $this = shift;
285 my $flag = shift;
286  
287 croak 'whitelist_pass: flag argument is undef' unless defined($flag);
288 $this->{_whitelistpass} = $flag;
289  
290 $this;
291 }
292  
293 sub connect_callback {
294 my $this = shift;
295 my $ctx = shift;
296 shift; # $hostname
297 my $pack = shift; # XXX should handle IPv6 via getsymval parsing
298  
299 my $addr = eval {
300 my @unpack = unpack_sockaddr_in($pack);
301 $unpack[1];
302 };
303  
304 return SMFIS_CONTINUE unless defined($addr);
305  
306 my $spfopts = {};
307 $ctx->setpriv({ _spfopts => $spfopts });
308  
309 $spfopts->{helo} = 'UNKNOWN'; # in case MTA allows skipping HELO step
310 $spfopts->{ip} = join('.', unpack('C4', $addr));
311 $spfopts->{myhostname} = $ctx->getsymval('j');
312  
313 SMFIS_CONTINUE;
314 }
315  
316 sub helo_callback {
317 my $this = shift;
318 my $ctx = shift;
319 my $helo = shift;
320 my $spfopts = $ctx->getpriv()->{_spfopts};
321  
322 $spfopts->{helo} = $helo;
323  
324 SMFIS_CONTINUE;
325 }
326  
327 sub envfrom_callback {
328 my $this = shift;
329 my $ctx = shift;
330 my $from = shift;
331  
332 $from =~ s/^<//;
333 $from =~ s/>$//;
334  
335 return SMFIS_CONTINUE if ($from eq ''); # null <> sender
336  
337 my $data = $ctx->getpriv();
338 my $query = new Mail::SPF::Query(
339 %{$this->{_spfopts}}, %{$data->{_spfopts}}, sender => $from
340 );
341 if (defined($@) && $@ ne '') {
342 warn "SPF query problem: $@";
343 return SMFIS_TEMPFAIL;
344 }
345  
346 my ($result, $smtp_comment, $header_comment) = $query->result();
347  
348 $data->{result} = $result;
349 $data->{header_comment} = $header_comment;
350  
351 if ($result eq 'fail' || ($result eq 'softfail' && !$this->{_ignoresoftfail})) {
352 my $msg = $this->{_message};
353 $msg =~ s/%M/$from/g;
354 $msg =~ s/%E/$smtp_comment/g;
355  
356 $ctx->setreply('554', '5.7.1', $msg);
357 return SMFIS_REJECT;
358 } elsif ($result eq 'error' && !$this->{_ignoretempfail}) {
359 my $domain = $from;
360 $domain =~ s/^.*\@([^\@]+)$/$1/;
361  
362 $ctx->setreply('451', '4.7.0', "Temporary DNS error encountered while fetching SPF record for $domain");
363 return SMFIS_TEMPFAIL;
364 } elsif ($result eq 'pass' && $this->{_whitelistpass}) {
365 return SMFIS_ACCEPT;
366 }
367  
368 SMFIS_CONTINUE; # don't whitelist a fallthrough
369 }
370  
371 sub eom_callback {
372 my $this = shift;
373 my $ctx = shift;
374 my $data = $ctx->getpriv();
375  
376 $ctx->addheader($this->{_addheader}, ($data->{result}).' ('.($data->{header_comment}).')')
377 if defined($this->{_addheader});
378  
379 SMFIS_CONTINUE;
380 }
381  
382 1;
383 __END__