| 1 | # | ||
| 2 | # Courier::Filter::Module::SPF class | ||
| 3 | # | ||
| 4 | # (C) 2004-2008 Julian Mehnle <julian@mehnle.net> | ||
| 5 | # $Id: SPF.pm 211 2008-03-23 01:25:20Z julian $ | ||
| 6 | # | ||
| 7 | ############################################################################### | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Courier::Filter::Module::SPF - SPF filter module for the Courier::Filter | ||
| 12 | framework | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | package Courier::Filter::Module::SPF; | ||
| 17 | |||
| 18 | use warnings; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | use base 'Courier::Filter::Module'; | ||
| 22 | |||
| 23 | use Courier::Filter::Util qw( | ||
| 24 | ipv4_address_pattern | ||
| 25 | ipv6_address_pattern | ||
| 26 | loopback_address_pattern | ||
| 27 | ); | ||
| 28 | |||
| 29 | use Mail::SPF; | ||
| 30 | |||
| 31 | use constant TRUE => (0 == 0); | ||
| 32 | use constant FALSE => not TRUE; | ||
| 33 | |||
| 34 | use constant match_on_default => ['fail', 'permerror', 'temperror']; | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | use Courier::Filter::Module::SPF; | ||
| 39 | |||
| 40 | my $module = Courier::Filter::Module::SPF->new( | ||
| 41 | scope => 'mfrom' || 'helo', | ||
| 42 | match_on => ['fail', 'permerror', 'temperror'], | ||
| 43 | default_response => $default_response_text, | ||
| 44 | spf_options => { | ||
| 45 | # any Mail::SPF::Server options | ||
| 46 | }, | ||
| 47 | |||
| 48 | logger => $logger, | ||
| 49 | inverse => 0, | ||
| 50 | trusting => 0, | ||
| 51 | testing => 0, | ||
| 52 | debugging => 0 | ||
| 53 | ); | ||
| 54 | |||
| 55 | my $filter = Courier::Filter->new( | ||
| 56 | ... | ||
| 57 | modules => [ $module ], | ||
| 58 | ... | ||
| 59 | ); | ||
| 60 | |||
| 61 | =head1 DESCRIPTION | ||
| 62 | |||
| 63 | This class is a filter module class for use with Courier::Filter. By default, | ||
| 64 | it matches a message if the sending machine's IP address is I<not> authorized | ||
| 65 | to send mail from the envelope sender's (MAIL FROM) domain according to that | ||
| 66 | domain's SPF (Sender Policy Framework) DNS record. This is classic I<inbound> | ||
| 67 | SPF checking. | ||
| 68 | |||
| 69 | The point of inbound SPF checking is for receivers to protect I<themselves> | ||
| 70 | against forged envelope sender addresses in messages sent by others. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | # Implementation: | ||
| 75 | ############################################################################### | ||
| 76 | |||
| 77 | =head2 Constructor | ||
| 78 | |||
| 79 | The following constructor is provided: | ||
| 80 | |||
| 81 | =over | ||
| 82 | |||
| 83 | =item B<new(%options)>: returns I<Courier::Filter::Module::SPF> | ||
| 84 | |||
| 85 | Creates a new B<SPF> filter module. | ||
| 86 | |||
| 87 | %options is a list of key/value pairs representing any of the following | ||
| 88 | options: | ||
| 89 | |||
| 90 | =over | ||
| 91 | |||
| 92 | =item B<scope> | ||
| 93 | |||
| 94 | A string denoting the authorization scope, i.e., identity, on which the SPF | ||
| 95 | check is to be performed. Defaults to C<'mfrom'>. | ||
| 96 | See L<Mail::SPF::Request/scope> for a detailed explanation. | ||
| 97 | |||
| 98 | =item B<match_on> | ||
| 99 | |||
| 100 | A reference to an array containing the set of SPF result codes which should | ||
| 101 | cause the filter module to match a message. Possible result codes are C<pass>, | ||
| 102 | C<fail>, C<softfail>, C<neutral>, C<none>, C<permerror>, and C<temperror>. See | ||
| 103 | the SPF specification for details on the meaning of those. If C<temperror> is | ||
| 104 | listed, a C<temperror> result will by definition never cause a I<permanent> | ||
| 105 | rejection, but only a I<temporary> one. Defaults to B<['fail', 'permerror', | ||
| 106 | 'temperror']>. | ||
| 107 | |||
| 108 | I<Note>: With early SPF specification drafts as well as the obsolete | ||
| 109 | Mail::SPF::Query module, the C<permerror> and C<temperror> result codes were | ||
| 110 | known as C<unknown> and C<error>, respectively; the old codes are now | ||
| 111 | deprecated but still supported for the time being. | ||
| 112 | |||
| 113 | =item B<default_response> | ||
| 114 | |||
| 115 | A string that is to be returned as the module's match result in case of a | ||
| 116 | match, that is when the C<match_on> option includes the result code of the SPF | ||
| 117 | check (by default when a message fails the SPF check). However, this default | ||
| 118 | response is used only if the (claimed) MAIL FROM or HELO domain does not | ||
| 119 | provide a result explanation of its own. | ||
| 120 | See L<Mail::SPF::Server/default_authority_explanation> for more information. | ||
| 121 | |||
| 122 | SPF macro substitution is performed on the default response, just like on | ||
| 123 | explanations provided by domain owners. If B<undef>, | ||
| 124 | L<Mail::SPF's default explanation|Mail::SPF::Server/default_authority_explanation> | ||
| 125 | will be used. Defaults to B<undef>. | ||
| 126 | |||
| 127 | =item B<spf_options> | ||
| 128 | |||
| 129 | A hash-ref specifying options for the Mail::SPF server object used by this | ||
| 130 | filter module. See L<Mail::SPF::Server/new> for the supported options. If any | ||
| 131 | of L<Mail::SPF::BlackMagic's additional options|Mail::SPF::Server/new>, such as | ||
| 132 | C<default_policy> (best-guess) or C<tfwl> (C<trusted-forwarder.org> | ||
| 133 | accreditation checking), are specified, a black-magic SPF server object will be | ||
| 134 | created instead. | ||
| 135 | |||
| 136 | =item B<fallback_guess> | ||
| 137 | |||
| 138 | =item B<trusted_forwarders> | ||
| 139 | |||
| 140 | I<Deprecated>. These options should now be specified as C<default_policy> and | ||
| 141 | C<tfwl> keys of the L</spf_options> option instead, although these legacy | ||
| 142 | options will continue to work for the time being. Furthermore, due to the move | ||
| 143 | from the obsolete L<Mail::SPF::Query> module to the L<Mail::SPF> reference | ||
| 144 | implementation, the L<Mail::SPF::BlackMagic> extension module is now required | ||
| 145 | when using these non-standard options. | ||
| 146 | |||
| 147 | =back | ||
| 148 | |||
| 149 | All options of the B<Courier::Filter::Module> constructor are also supported. | ||
| 150 | Please see L<Courier::Filter::Module/new> for their descriptions. | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub new { | ||
| 155 | my ($class, %options) = @_; | ||
| 156 | |||
| 157 | $options{scope} ||= 'mfrom'; | ||
| 158 | |||
| 159 | if (defined($options{reject_on})) { | ||
| 160 | $class->warn('"reject_on" option is deprecated! Use "match_on" option instead.'); | ||
| 161 | $options{match_on} ||= $options{reject_on}; | ||
| 162 | } | ||
| 163 | $options{match_on} ||= $class->match_on_default; | ||
| 164 | |||
| 165 | my $spf_options = $options{spf_options} || {}; | ||
| 166 | |||
| 167 | if (defined($options{fallback_guess})) { | ||
| 168 | $class->warn('"fallback_guess" option is deprecated! Use "default_policy" key in "spf_options" option instead.'); | ||
| 169 | $spf_options->{default_policy} ||= $options{fallback_guess}; | ||
| 170 | } | ||
| 171 | |||
| 172 | if (defined($options{trusted_forwarders})) { | ||
| 173 | $class->warn('"trusted_forwarders" option is deprecated! Use "tfwl" key in "spf_options" option instead.'); | ||
| 174 | $spf_options->{tfwl} ||= $options{trusted_forwarders}; | ||
| 175 | } | ||
| 176 | |||
| 177 | my $spf_server_class = 'Mail::SPF::Server'; | ||
| 178 | foreach my $spf_option (keys(%$spf_options)) { | ||
| 179 | if (not Mail::SPF->can($spf_option)) { | ||
| 180 | $spf_server_class = 'Mail::SPF::BlackMagic::Server'; | ||
| 181 | eval { require Mail::SPF::BlackMagic }; | ||
| 182 | if ($@) { | ||
| 183 | $class->warn("Mail::SPF::BlackMagic not installed. Ignoring unsupported \"$spf_option\" SPF option."); | ||
| 184 | } | ||
| 185 | elsif (not $spf_server_class->can($spf_option)) { | ||
| 186 | $class->warn("Ignoring unsupported \"$spf_option\" SPF option. Perhaps newer Mail::SPF or Mail::SPF::BlackMagic required?"); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | my $spf_server = $spf_server_class->new( | ||
| 192 | default_authority_explanation => $options{default_response}, | ||
| 193 | %$spf_options | ||
| 194 | ); | ||
| 195 | |||
| 196 | my $self = $class->SUPER::new( | ||
| 197 | %options, | ||
| 198 | spf_server => $spf_server | ||
| 199 | ); | ||
| 200 | return $self; | ||
| 201 | } | ||
| 202 | |||
| 203 | =back | ||
| 204 | |||
| 205 | =head2 Instance methods | ||
| 206 | |||
| 207 | See L<Courier::Filter::Module/Instance methods> for a description of the | ||
| 208 | provided instance methods. | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | sub match { | ||
| 213 | my ($self, $message) = @_; | ||
| 214 | |||
| 215 | return undef | ||
| 216 | if $message->remote_host =~ / ^ ${\loopback_address_pattern} $ /x; | ||
| 217 | # Exempt IPv4/IPv6 loopback addresses. | ||
| 218 | |||
| 219 | my $scope = $self->{scope}; | ||
| 220 | my $identities = { | ||
| 221 | helo => $message->remote_host_helo, | ||
| 222 | mfrom => $message->sender | ||
| 223 | }; | ||
| 224 | my $identity = $identities->{$scope}; | ||
| 225 | |||
| 226 | return undef | ||
| 227 | if $identity eq ''; | ||
| 228 | # Exempt empty identities (esp. empty MAIL FROM, i.e., bounces). | ||
| 229 | |||
| 230 | return undef | ||
| 231 | if $identity =~ / ^ \[ (?: ${\ipv4_address_pattern} | ${\ipv6_address_pattern} ) \] $ /x; | ||
| 232 | # Exempt IP address literals ("[<ip-address>]"). | ||
| 233 | |||
| 234 | my $request = Mail::SPF::Request->new( | ||
| 235 | scope => $scope, | ||
| 236 | identity => $identity, | ||
| 237 | ip_address => $message->remote_host, | ||
| 238 | helo_identity => $message->remote_host_helo | ||
| 239 | ); | ||
| 240 | |||
| 241 | my $result = $self->{spf_server}->process($request); | ||
| 242 | my $result_code = $result->code; | ||
| 243 | my $response = | ||
| 244 | $result->can('authority_explanation') ? | ||
| 245 | $result->authority_explanation | ||
| 246 | : $result->local_explanation; | ||
| 247 | |||
| 248 | my %match_on; | ||
| 249 | @match_on{ @{$self->{match_on}} } = (); # Hash-ify match-on result codes list. | ||
| 250 | |||
| 251 | return "SPF: $response", ($result_code eq 'temperror' ? 451 : ()) | ||
| 252 | if exists($match_on{$result_code}); | ||
| 253 | |||
| 254 | return undef; | ||
| 255 | } | ||
| 256 | |||
| 257 | =head1 SEE ALSO | ||
| 258 | |||
| 259 | L<Courier::Filter::Module>, L<Courier::Filter::Overview>, L<Mail::SPF>. | ||
| 260 | |||
| 261 | For AVAILABILITY, SUPPORT, and LICENSE information, see | ||
| 262 | L<Courier::Filter::Overview>. | ||
| 263 | |||
| 264 | =head1 REFERENCES | ||
| 265 | |||
| 266 | =over | ||
| 267 | |||
| 268 | =item B<SPF website> (Sender Policy Framework) | ||
| 269 | |||
| 270 | L<http://www.openspf.org> | ||
| 271 | |||
| 272 | =item B<SPF specification> | ||
| 273 | |||
| 274 | L<http://www.openspf.org/Specifications> | ||
| 275 | |||
| 276 | =back | ||
| 277 | |||
| 278 | =head1 AUTHOR | ||
| 279 | |||
| 280 | Julian Mehnle <julian@mehnle.net> | ||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | TRUE; |