| 1 | package Mail::Decency::Policy::SPF; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | extends 'Mail::Decency::Policy::Core'; | |||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use version 0.74; our $VERSION = qv( "v0.1.4" ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | ▶ | use Mail::Decency::Helper::IP qw/ is_local_host /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use Mail::SPF; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | Mail::Decency::Policy::SPF | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | A SPF implementation (http://www.openspf.org/) for decency based on Mail::SPF | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | Weight's incoming mail based on the sender policy framework. | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | The SPF suggests that your (sender) domain has an additional TXT record providing a list/range of server ip's which are allowed to send mails. | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | For an explanation of the spf result codes have a look here: http://www.openspf.org/SPF_Received_Header | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | =head1 CONFIG | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | --- | ||||||||||||||||||||
| 31 | versions: [ 1, 2 ] | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # the sender ip is allowed to send from this ip | ||||||||||||||||||||
| 34 | weight_pass: 20 | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | # no spf support from sender domain | ||||||||||||||||||||
| 37 | weight_none: 0 | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | # it is a fail, but sender domain admin has not the balls to use hard restrictions | ||||||||||||||||||||
| 40 | # neutral is a bit more wishy-washy then softail is, but essentially both | ||||||||||||||||||||
| 41 | # are saying: the particular sender is not really permitted, but it won't deny it | ||||||||||||||||||||
| 42 | # the ..by_default neutral status i don't get.. | ||||||||||||||||||||
| 43 | weight_neutral: -10 | ||||||||||||||||||||
| 44 | weight_neutral_by_default: -10 | ||||||||||||||||||||
| 45 | weight_softfail: -10 | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | # this admin has the bals. the sender ip is not permitted for this domain | ||||||||||||||||||||
| 48 | weight_fail: -50 | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | # some temporary dns problem. should not be weighted negativly, cause this | ||||||||||||||||||||
| 51 | # could happen to any of us | ||||||||||||||||||||
| 52 | weight_temperror: 0 | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # the permanent error says: we received something, but dont know what. | ||||||||||||||||||||
| 55 | weight_permerror: -10 | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | # this seems to catch error's which could not be further determined.. | ||||||||||||||||||||
| 58 | weight_error: 0 | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =head2 CRITICS | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | Critics say the SPF could break existing structures, eg you cannot send mails from gmx.com via your company's mails server. Advocates reply: that's the idea and it is good. | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =head1 CLASS ATTRIBUTES | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =head2 versions : ArrayRef[Int] | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | What SPF Versions to use (actually there is no SPF version2, read http://www.openspf.org/SPF_vs_Sender_ID) | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | ▶ | has versions => ( is => 'rw', isa => 'ArrayRef[Int]', default => sub { [ 1, 2 ] } ); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =head2 weight_pass : Int | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | Weighting for passed (allowed) mails | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 1 | has weight_pass => ( is => 'rw', isa => 'Int', default => 10 ); | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | =head2 weight_pass : Int | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | Weighting for passed (allowed) mails | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 1 | has weight_none => ( is => 'rw', isa => 'Int', default => 0 ); | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =head2 weight_neutral : Int | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | Weighting for neutral (probably not allowed, but not rejected) | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 1 | has weight_neutral => ( is => 'rw', isa => 'Int', default => -20 ); | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =head2 weight_neutral_by_default : Int | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | Kind of the same as neutral | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 1 | has weight_neutral_by_default => ( is => 'rw', isa => 'Int', default => -20 ); | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =head2 weight_softfail : Int | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | Soft fail.. kind of same as neutral | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 1 | has weight_softfail => ( is => 'rw', isa => 'Int', default => -20 ); | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =head2 weight_fail : Int | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | Really failed. SPF records says: not allowed. | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 1 | has weight_fail => ( is => 'rw', isa => 'Int', default => -50 ); | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =head2 weight_temperror : Int | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | Tempororay error.. maybe on your side ? | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 1 | has weight_temperror => ( is => 'rw', isa => 'Int', default => 0 ); | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =head2 weight_permerror : Int | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | Permanent error.. should never happen. Something is really weird. | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 1 | has weight_permerror => ( is => 'rw', isa => 'Int', default => -20 ); | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =head2 weight_error : Int | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | Some error. | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 1 | has weight_error => ( is => 'rw', isa => 'Int', default => 0 ); | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =head2 spf_server : Mail::SPF::Server | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 1 | has spf_server => ( is => 'rw', isa => 'Mail::SPF::Server' ); | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =head1 METHODS | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =head2 init | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 0 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 162 | 0 | my ( $self ) = @_; | |||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | # got versions ? | ||||||||||||||||||||
| 165 | 0 | ▶ | if ( defined $self->config->{ versions } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | die "versions has to be an arrayref of integers (1, 2 so far)\n" | ||||||||||||||||||||
| 167 | unless ref( $self->config->{ versions } ) eq 'ARRAY' | ||||||||||||||||||||
| 168 | 0 | ▶ | && scalar grep { /^\d+$/ } @{ $self->config->{ versions } }; | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $self->config->{"versions"} eq "ARRAY" and scalar grep({/^\d+$/;} @{$self->config->{"versions"};})
MC/DC0%0/1
ref $self->config->{"versions"} eq "ARRAY" and scalar grep({/^\d+$/;} @{$self->config->{"versions"};})
ref $self->config->{"versions"} eq "ARRAY"
Truth table0%0/2
A
A= ref $self->config->{"versions"} eq "ARRAY"
| |||||||||||||||||||||
| 169 | 0 | $self->versions( $self->config->{ versions } ); | |||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | # set weight | ||||||||||||||||||||
| 173 | 0 | foreach my $weight( qw/ | |||||||||||||||||||
| 174 | weight_pass | ||||||||||||||||||||
| 175 | weight_none | ||||||||||||||||||||
| 176 | weight_neutral | ||||||||||||||||||||
| 177 | weight_neutral_by_default | ||||||||||||||||||||
| 178 | weight_softfail | ||||||||||||||||||||
| 179 | weight_fail | ||||||||||||||||||||
| 180 | weight_temperror | ||||||||||||||||||||
| 181 | weight_permerror | ||||||||||||||||||||
| 182 | weight_error | ||||||||||||||||||||
| 183 | / ) { | ||||||||||||||||||||
| 184 | $self->$weight( $self->config->{ $weight } ) | ||||||||||||||||||||
| 185 | 0 | ▶ | if defined $self->config->{ $weight }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | # init spf server | ||||||||||||||||||||
| 189 | 0 | $self->spf_server( Mail::SPF::Server->new ); | |||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 0 | return ; | |||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | =head2 handle | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 0 | ▶ | sub handle { | ||||||||||||||||||
|
Subroutine
handle: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 200 | 0 | my ( $self, $server, $attrs_ref ) = @_; | |||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | # don bother with loopback addresses! EVEN IF ENABLED BY FORCE! | ||||||||||||||||||||
| 203 | 0 | ▶ | return if is_local_host( $attrs_ref->{ client_address } ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | # check mfrom scope | ||||||||||||||||||||
| 206 | 0 | ▶ | if ( $attrs_ref->{ sender_address } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 207 | $self->check_spf( | ||||||||||||||||||||
| 208 | 0 | mfrom => $attrs_ref->{ sender_address }, $attrs_ref ); | |||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | # check helo scope instead (eg bounce mail with empty mail from) | ||||||||||||||||||||
| 212 | else { | ||||||||||||||||||||
| 213 | $self->check_spf( | ||||||||||||||||||||
| 214 | 0 | helo => $attrs_ref->{ helo_name }, $attrs_ref ); | |||||||||||||||||||
| 215 | } | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | # nothing to return | ||||||||||||||||||||
| 218 | 0 | return; | |||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | =head2 check_spf | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | Peforms the check for either helo or mfrom scope | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | =cut | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | 0 | ▶ | sub check_spf { | ||||||||||||||||||
|
Subroutine
check_spf: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 230 | 0 | my ( $self, $scope, $identity, $attrs_ref ) = @_; | |||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | # build request | ||||||||||||||||||||
| 233 | my $req = Mail::SPF::Request->new( | ||||||||||||||||||||
| 234 | versions => $self->versions, | ||||||||||||||||||||
| 235 | scope => $scope, | ||||||||||||||||||||
| 236 | identity => $identity, | ||||||||||||||||||||
| 237 | ip_address => $attrs_ref->{ client_address }, | ||||||||||||||||||||
| 238 | 0 | ); | |||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | # send to spf server | ||||||||||||||||||||
| 241 | 0 | my $res = $self->spf_server->process( $req ); | |||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | # get code .. | ||||||||||||||||||||
| 244 | 0 | my $code = $res->code; | |||||||||||||||||||
| 245 | 0 | ▶ | if ( $code ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 246 | 0 | $code =~ s/-/_/g; | |||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | # check wieght, if having.. | ||||||||||||||||||||
| 249 | 0 | my $weight_meth = "weight_$code"; | |||||||||||||||||||
| 250 | 0 | ▶ | if ( $self->can( $weight_meth ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 251 | 0 | $self->logger->debug0( "Found SPF Result: $code for '$attrs_ref->{ sender_address }' from '$attrs_ref->{ client_address }'" ); | |||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | # parse headers, if any | ||||||||||||||||||||
| 254 | 0 | ▶ | ( my $header = $res->received_spf_header || "" ) =~ s/^Received-SPF: //; | ||||||||||||||||||
|
Condition0%0/2
$res->received_spf_header || ''
MC/DC0%0/1
$res->received_spf_header || ''
$res->received_spf_header
Truth table0%0/2
A
A= $res->received_spf_header
| |||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | 0 | ▶ | if ( $code eq 'passed' ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 257 | 0 | $self->session_data->set_flag( 'spf_pass' ); | |||||||||||||||||||
| 258 | } | ||||||||||||||||||||
| 259 | elsif ( $code ne 'none' ) { | ||||||||||||||||||||
| 260 | 0 | $self->session_data->set_flag( 'spf_nopass' ); | |||||||||||||||||||
| 261 | } | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | # return scoring result (throws final exception, if final) | ||||||||||||||||||||
| 264 | 0 | return $self->add_spam_score( $self->$weight_meth, join( "; ", | |||||||||||||||||||
| 265 | "SPF Result: $code", | ||||||||||||||||||||
| 266 | "SPF Header: $header" | ||||||||||||||||||||
| 267 | ), "IP not on SPF Record for $attrs_ref->{ sender_domain }" ); | ||||||||||||||||||||
| 268 | } | ||||||||||||||||||||
| 269 | |||||||||||||||||||||
| 270 | # oops, this should not happen | ||||||||||||||||||||
| 271 | else { | ||||||||||||||||||||
| 272 | 0 | $self->logger->error( "Malformed SPF result: $code for '$attrs_ref->{ sender_address }' from '$attrs_ref->{ client_address }'" ); | |||||||||||||||||||
| 273 | } | ||||||||||||||||||||
| 274 | } | ||||||||||||||||||||
| 275 | 0 | return ; | |||||||||||||||||||
| 276 | } | ||||||||||||||||||||
| 277 | |||||||||||||||||||||
| 278 | =head1 AUTHOR | ||||||||||||||||||||
| 279 | |||||||||||||||||||||
| 280 | Ulrich Kautz <uk@fortrabbit.de> | ||||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | =head1 COPYRIGHT | ||||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||||||||||||||||||||
| 285 | |||||||||||||||||||||
| 286 | =head1 LICENCSE | ||||||||||||||||||||
| 287 | |||||||||||||||||||||
| 288 | This library is free software and may be distributed under the same terms as perl itself. | ||||||||||||||||||||
| 289 | |||||||||||||||||||||
| 290 | =cut | ||||||||||||||||||||
| 291 | |||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | |||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | 1 | 1; | |||||||||||||||||||