Filter/Module/SPFout.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 134 CC 12 SCAR 35.6
CC
12
Cov
0%
match
90.0
new
20.0
BEGIN
2.0
CRAP 35.2
1 #
2 # Courier::Filter::Module::SPFout class
3 #
4 # (C) 2005-2008 Julian Mehnle <julian@mehnle.net>
5 # $Id: SPFout.pm 211 2008-03-23 01:25:20Z julian $
6 #
7 ###############################################################################
8  
9 =head1 NAME
10  
11 Courier::Filter::Module::SPFout - Outbound SPF filter module for the
12 Courier::Filter framework
13  
14 =cut
15  
16 package Courier::Filter::Module::SPFout;
17  
18 use warnings;
19 use strict;
20  
21 use base 'Courier::Filter::Module';
22  
23 use Error ':try';
24  
25 use Mail::SPF;
26 use Mail::SPF::MacroString;
27 use Mail::SPF::Util;
28 use Net::Address::IP::Local;
29  
30 use Courier::Filter::Util qw(
31 ipv4_address_pattern
32 ipv6_address_pattern
33 loopback_address_pattern
34 );
35  
36 use Courier::Error;
37  
38 use constant TRUE => (0 == 0);
39 use constant FALSE => not TRUE;
40  
41 use constant match_on_default => ['fail', 'permerror', 'temperror'];
42  
43 =head1 SYNOPSIS
44  
45 use Courier::Filter::Module::SPFout;
46
47 my $module = Courier::Filter::Module::SPFout->new(
48 match_on => ['fail', 'permerror', 'temperror'],
49 default_response => $default_response_text,
50 force_response => $force_response_text,
51 outbound_ip_addresses
52 => ['129.257.16.1', '2001:6ag:10e1::1'],
53 spf_options => {
54 # any Mail::SPF::Server options
55 },
56
57 logger => $logger,
58 inverse => 0,
59 trusting => 0,
60 testing => 0,
61 debugging => 0
62 );
63
64 my $filter = Courier::Filter->new(
65 ...
66 modules => [ $module ],
67 ...
68 );
69  
70 =head1 DESCRIPTION
71  
72 This class is a filter module for use with Courier::Filter. It matches a
73 message if any of the receiving (local) machine's outbound IP addresses are
74 I<not> authorized to send mail from the envelope sender's (MAIL FROM) domain
75 according to that domain's DNS SPF (Sender Policy Framework) record. This is
76 I<outbound> SPF checking.
77  
78 The point of inbound SPF checking is for message submission agents (MSAs,
79 smarthosts) to protect I<others> against forged envelope sender addresses in
80 messages submitted by the MSA's users.
81  
82 =cut
83  
84 # Implementation:
85 ###############################################################################
86  
87 =head2 Constructor
88  
89 The following constructor is provided:
90  
91 =over
92  
93 =item B<new(%options)>: returns I<Courier::Filter::Module::SPFout>
94  
95 Creates a new B<SPFout> filter module.
96  
97 %options is a list of key/value pairs representing any of the following
98 options:
99  
100 =over
101  
102 =item B<trusting>
103  
104 I<Disabled>. Since I<outbound> SPF checking, as opposed to I<inbound> SPF
105 checking, is applied to trusted (authenticated) messages only, setting this
106 module to be B<trusting> does not make sense. This property is thus locked to
107 B<false>. Also see the description of
108 L<< Courier::Message's C<trusted> property | Courier::Message/trusted >>.
109  
110 =item B<match_on>
111  
112 A reference to an array containing the set of SPF result codes which should
113 cause the filter module to match a message. Possible result codes are C<pass>,
114 C<fail>, C<softfail>, C<neutral>, C<none>, C<permerror>, and C<temperror>. See
115 the SPF specification for details on the meaning of those. If C<temperror> is
116 listed, an C<temperror> result will by definition never cause a I<permanent>
117 rejection, but only a I<temporary> one. Defaults to B<['fail', 'permerror',
118 'temperror']>.
119  
120 I<Note>: With early SPF specification drafts as well as the obsolete
121 Mail::SPF::Query module, the C<permerror> and C<temperror> result codes were
122 known as C<unknown> and C<error>, respectively; the old codes are now
123 deprecated but still supported for the time being.
124  
125 =item B<default_response>
126  
127 A string that is to be returned as the module's match result in case of a
128 match, that is when the C<match_on> option includes the result code of the SPF
129 check (by default when a message fails the SPF check). However, this default
130 response is used only if the (claimed) envelope sender domain does not provide
131 an explicit response. See L<Mail::SPF::Server/default_authority_explanation>
132 for more information.
133  
134 SPF macro substitution is performed on the default response, just like on
135 explanations provided by domain owners. If B<undef>,
136 L<< Mail::SPF's default explanation | Mail::SPF::Server/default_authority_explanation >>
137 will be used. Defaults to B<undef>.
138  
139 =item B<force_response>
140  
141 Instead of merely specifying a default response for cases where the sender
142 domain does not provide an explicit response, you can also specify a response
143 to be used in I<all> cases, even if the sender domain does provide one. This
144 may be useful if you do not want to confuse your own users with I<3rd-party>
145 provided explanations when in fact they are only dealing with I<your> server
146 not wanting to relay their messages. Defaults to B<undef>.
147  
148 =item B<outbound_ip_addresses>
149  
150 A reference to an array containing the local system's set of outbound IP
151 addresses that will be assumed as the sender IP address in outbound SPF
152 checks. This set should include I<all> public IP addresses that are used for
153 relaying mail. By default, automatic discovery of one public IP address that
154 is "en route" to "the internet" is attempted for each of IPv4 and IPv6.
155 Auto-discovery does not work from behind NATs.
156  
157 =item B<spf_options>
158  
159 A hash-ref specifying options for the Mail::SPF server object used by this
160 filter module. See L<Mail::SPF::Server/new> for the supported options.
161  
162 =back
163  
164 All options of the B<Courier::Filter::Module> constructor (except for the
165 B<trusting> option) are also supported. Please see
166 L<Courier::Filter::Module/new> for their descriptions.
167  
168 =cut
169  
170 sub new {
171 my ($class, %options) = @_;
172
173 $options{trusting} = FALSE; # Locked to FALSE.
174
175 $options{scope} ||= 'mfrom';
176 # The "scope" option has been deliberately left undocumented because
177 # outbound SPF checking does not make sense for the HELO identity and
178 # we do not want to promote the use of the PRA identity for outbound
179 # checking.
180 # TODO: Croak on scope = 'helo'? What about 'pra'?
181
182 $options{match_on} ||= $class->match_on_default;
183
184 my $spf_options = $options{spf_options} || {};
185
186 foreach my $spf_option (keys(%$spf_options)) {
187 if (not Mail::SPF->can($spf_option)) {
188 $class->warn("Ignoring unsupported \"$spf_option\" SPF option. Perhaps newer Mail::SPF required?");
189 }
190 }
191
192 my $spf_server = Mail::SPF::Server->new(
193 default_authority_explanation => $options{default_response},
194 %$spf_options
195 );
196
197 if (defined($options{force_response})) {
198 $options{force_response} = Mail::SPF::MacroString->new(
199 text => $options{force_response},
200 is_explanation => TRUE
201 );
202 }
203
204 if (not defined($options{outbound_ip_addresses})) {
205 # Attempt auto-discovery of public IP addresses:
206 $options{outbound_ip_addresses} = \my @outbound_ip_addresses;
207 try { push(@outbound_ip_addresses, Net::Address::IP::Local->public_ipv4) };
208 try { push(@outbound_ip_addresses, Net::Address::IP::Local->public_ipv6) };
209 }
210
211 my $self = $class->SUPER::new(
212 %options,
213 spf_server => $spf_server
214 );
215 return $self;
216 }
217  
218 =back
219  
220 =head2 Instance methods
221  
222 See L<Courier::Filter::Module/"Instance methods"> for a description of the
223 provided instance methods.
224  
225 =cut
226  
227 sub match {
228 my ($self, $message) = @_;
229
230 return undef
231 if not $message->trusted;
232 # This filter module applies to trusted (authenticated) messages only.
233
234 return undef
235 if $message->remote_host =~ / ^ ${\loopback_address_pattern} $ /x;
236 # Exempt IPv4/IPv6 loopback addresses, i.e., self submissions.
237  
238 my $scope = $self->{scope};
239 my $identities = {
240 helo => undef, # No outbound HELO checks supported.
241 mfrom => $message->sender
242 };
243 my $identity = $identities->{$scope};
244 my $helo_identity = Mail::SPF::Util->hostname; # Local system's host name.
245
246 return undef
247 if $identity eq '';
248 # Empty identity (esp. empty MAIL FROM, i.e., bounces) on submission?? Weird. O_o
249
250 return undef
251 if $identity =~ / ^ \[ (?: ${\ipv4_address_pattern} | ${\ipv6_address_pattern} ) \] $ /x;
252 # Exempt IP address literals ("[<ip-address>]").
253
254 my %match_on;
255 @match_on{ @{$self->{match_on}} } = (); # Hash-ify match-on result codes list.
256
257 foreach my $ip_address (@{ $self->{outbound_ip_addresses} }) {
258 my $request = Mail::SPF::Request->new(
259 scope => $scope,
260 identity => $identity,
261 ip_address => $ip_address,
262 helo_identity => $helo_identity
263 );
264
265 my $result = $self->{spf_server}->process($request);
266 my $result_code = $result->code;
267
268 if (exists($match_on{$result_code})) {
269 # Match!
270
271 my $response;
272 if (defined($self->{force_response})) {
273 $response = $self->{force_response}->expand($self->{spf_server}, $request);
274 }
275 else {
276 $response =
277 $result->can('authority_explanation') ?
278 $result->authority_explanation
279 : $result->local_explanation;
280 }
281
282 return "SPF: $response", ($result_code eq 'temperror' ? 451 : ());
283 }
284 }
285
286 return undef;
287 }
288  
289 =head1 SEE ALSO
290  
291 L<Courier::Filter::Module::SPF>, L<Courier::Filter::Module>,
292 L<Courier::Filter::Overview>, L<Mail::SPF>.
293  
294 For AVAILABILITY, SUPPORT, and LICENSE information, see
295 L<Courier::Filter::Overview>.
296  
297 =head1 REFERENCES
298  
299 =over
300  
301 =item B<SPF website> (Sender Policy Framework)
302  
303 L<http://www.openspf.org>
304  
305 =item B<SPF specification>
306  
307 L<http://www.openspf.org/Specifications>
308  
309 =back
310  
311 =head1 AUTHOR
312  
313 Julian Mehnle <julian@mehnle.net>
314  
315 =cut
316  
317 TRUE;