2012-04-29 10:35:59 +02:00
#!perl -w
2003-06-27 14:25:52 +02:00
=head1 NAME
SPF - plugin to implement Sender Permitted From
=head1 SYNOPSIS
2010-05-11 07:41:08 +02:00
Prevents email sender address spoofing by checking the SPF policy of the purported senders domain.
2003-06-27 14:25:52 +02:00
2010-05-11 07:41:08 +02:00
=head1 DESCRIPTION
2003-06-27 19:27:35 +02:00
2010-05-11 07:41:08 +02:00
Sender Policy Framework (SPF) is an e-mail validation system designed to prevent spam by addressing source address spoofing. SPF allows administrators to specify which hosts are allowed to send e-mail from a given domain by creating a specific SPF record in the public DNS. Mail exchangers then use the DNS to check that mail from a given domain is being sent by a host sanctioned by that domain's administrators. -- http://en.wikipedia.org/wiki/Sender_Policy_Framework
2012-05-09 06:03:44 +02:00
The results of a SPF query are stored in a transaction note named 'spfquery';
2010-05-11 07:41:08 +02:00
=head1 CONFIGURATION
2003-06-27 19:27:35 +02:00
2010-05-11 07:41:08 +02:00
In config/plugins, add arguments to the sender_permitted_from line.
2004-02-03 03:57:04 +01:00
2012-05-09 06:03:44 +02:00
sender_permitted_from reject 3
=head2 reject
Set to a value between 1 and 6 to enable the following SPF behaviors:
1 annotate-only, add Received-SPF header, no rejections.
2 defer on DNS failures. Assure there's always a meaningful SPF header.
3 rejected if SPF record says 'fail'
4 stricter reject. Also rejects 'softfail'
5 reject 'neutral'
6 reject if no SPF records, or a syntax error
Most sites should start at level 3. It temporarily defers connections (4xx) that have soft SFP failures and only rejects (5xx) messages when the sending domains policy suggests it.
SPF levels above 4 are for crusaders who don't mind rejecting some valid mail when the sending server administrator hasn't dotted his i's and crossed his t's. May the deities bless theirobsessive little hearts.
=head1 SEE ALSO
2004-02-03 03:57:04 +01:00
2012-05-09 06:03:44 +02:00
http://spf.pobox.com/
http://en.wikipedia.org/wiki/Sender_Policy_Framework
2004-02-03 03:57:04 +01:00
2012-05-09 06:03:44 +02:00
=head1 ACKNOWLDGEMENTS
2004-02-03 03:57:04 +01:00
2012-05-09 06:03:44 +02:00
The reject options are modeled after, and aim to match the functionality of those found in the SPF patch for qmail-smtpd.
2003-06-28 01:00:52 +02:00
2010-05-11 07:41:08 +02:00
=head1 AUTHOR
2012-06-23 06:52:05 +02:00
Matt Simerson - 2012 - increased policy options from 3 to 6
2012-05-07 09:36:02 +02:00
Matt Simerson - 2011 - rewrote using Mail::SPF
2010-05-11 07:41:08 +02:00
2012-05-07 09:36:02 +02:00
Matt Sergeant - 2003 - initial plugin
2010-05-11 07:41:08 +02:00
2003-06-27 14:25:52 +02:00
=cut
2010-05-11 07:41:08 +02:00
use strict;
2012-05-09 06:03:44 +02:00
use warnings;
#use Mail::SPF 2.000; # eval'ed in ->register
2012-04-08 02:11:16 +02:00
use Qpsmtpd::Constants;
2003-06-27 14:25:52 +02:00
sub register {
2012-05-09 06:03:44 +02:00
my ($self, $qp, %args) = @_;
2012-06-22 11:38:01 +02:00
eval 'use Mail::SPF';
2012-05-09 06:03:44 +02:00
if ( $@ ) {
2012-06-23 06:52:05 +02:00
warn "skip: plugin disabled, is Mail::SPF installed?\n";
2012-05-09 06:03:44 +02:00
$self->log(LOGERROR, "skip: plugin disabled, is Mail::SPF installed?");
return;
};
$self->{_args} = { %args };
if ( $self->{_args}{spf_deny} ) {
$self->{_args}{reject} = 3 if $self->{_args}{spf_deny} == 1;
$self->{_args}{reject} = 4 if $self->{_args}{spf_deny} == 2;
};
if ( ! $self->{_args}{reject} && $self->qp->config('spfbehavior') ) {
$self->{_args}{reject} = $self->qp->config('spfbehavior');
};
2012-06-23 06:52:05 +02:00
$self->register_hook('mail', 'mail_handler');
$self->register_hook('data_post', 'data_post_handler');
2003-06-27 14:25:52 +02:00
}
2012-06-23 06:52:05 +02:00
sub mail_handler {
2010-05-11 07:41:08 +02:00
my ($self, $transaction, $sender, %param) = @_;
2012-06-03 03:44:46 +02:00
return (DECLINED) if $self->is_immune();
2012-05-09 06:03:44 +02:00
my $format = $sender->format;
2012-05-07 09:36:02 +02:00
if ( $format eq '<>' || ! $sender->host || ! $sender->user ) {
2012-06-23 06:52:05 +02:00
$self->log( LOGINFO, "skip, null sender" );
2012-05-07 09:36:02 +02:00
return (DECLINED, "SPF - null sender");
};
2012-04-08 04:03:30 +02:00
2012-06-03 03:44:46 +02:00
if ( $self->is_in_relayclients() ) {
2012-06-23 06:52:05 +02:00
$self->log( LOGINFO, "skip, in relayclients" );
2012-05-09 06:03:44 +02:00
return (DECLINED, "SPF - relaying permitted");
2012-05-07 09:36:02 +02:00
};
2010-05-11 07:41:08 +02:00
2012-06-23 06:52:05 +02:00
if ( ! $self->{_args}{reject} ) {
$self->log( LOGINFO, "skip, reject disabled" );
return (DECLINED);
};
2012-05-09 06:03:44 +02:00
my $client_ip = $self->qp->connection->remote_ip;
my $from = $sender->user . '@' . lc($sender->host);
my $helo = $self->qp->connection->hello_host;
my $scope = $from ? 'mfrom' : 'helo';
my %req_params = ( versions => [1, 2], # optional
scope => $scope,
ip_address => $client_ip,
2010-05-11 07:41:08 +02:00
);
2012-05-09 06:03:44 +02:00
if ($scope =~ /^mfrom|pra$/) {
2010-05-11 07:41:08 +02:00
$req_params{identity} = $from;
$req_params{helo_identity} = $helo if $helo;
}
elsif ($scope eq 'helo') {
$req_params{identity} = $helo;
$req_params{helo_identity} = $helo;
}
my $spf_server = Mail::SPF::Server->new();
my $request = Mail::SPF::Request->new(%req_params);
2012-06-23 06:52:05 +02:00
my $result = $spf_server->process($request) or return DECLINED;
2010-05-11 07:41:08 +02:00
$transaction->notes('spfquery', $result);
my $code = $result->code;
my $why = $result->local_explanation;
2012-05-09 06:03:44 +02:00
my $reject = $self->{_args}{reject};
if ( ! $code ) {
return (DENYSOFT, "SPF - no response") if $reject >= 2;
return (DECLINED, "SPF - no response");
};
2010-05-11 07:41:08 +02:00
2012-05-09 06:03:44 +02:00
return (DECLINED, "SPF - $code: $why") if ! $reject;
2003-06-28 01:00:52 +02:00
2012-05-09 06:03:44 +02:00
# SPF result codes: pass fail softfail neutral none error permerror temperror
if ( $code eq 'pass' ) { }
elsif ( $code eq 'fail' ) {
return (DENY, "SPF - forgery: $why") if $reject >= 3;
return (DENYSOFT, "SPF - $code: $why") if $reject >= 2;
}
elsif ( $code eq 'softfail' ) {
return (DENY, "SPF - forgery: $why") if $reject >= 4;
return (DENYSOFT, "SPF - $code: $why") if $reject >= 3;
}
elsif ( $code eq 'neutral' ) {
return (DENY, "SPF - forgery: $why") if $reject >= 5;
}
elsif ( $code eq 'none' ) {
return (DENY, "SPF - forgery: $why") if $reject >= 6;
}
elsif ( $code eq 'error' ) {
return (DENY, "SPF - $code: $why") if $reject >= 6;
return (DENYSOFT, "SPF - $code: $why") if $reject >= 2;
}
elsif ( $code eq 'permerror' ) {
return (DENY, "SPF - $code: $why") if $reject >= 6;
2012-05-21 03:46:47 +02:00
return (DENYSOFT, "SPF - $code: $why") if $reject >= 3;
2012-05-09 06:03:44 +02:00
}
elsif ( $code eq 'temperror' ) {
return (DENYSOFT, "SPF - $code: $why") if $reject >= 2;
2010-05-11 07:41:08 +02:00
}
2012-06-23 06:52:05 +02:00
$self->log(LOGDEBUG, "SPF from $from was $code: $why");
2012-05-09 06:03:44 +02:00
return (DECLINED, "SPF - $code: $why");
2003-06-27 14:25:52 +02:00
}
2012-06-23 06:52:05 +02:00
sub data_post_handler {
2010-05-11 07:41:08 +02:00
my ($self, $transaction) = @_;
2004-02-03 03:57:04 +01:00
2010-05-11 07:41:08 +02:00
my $result = $transaction->notes('spfquery') or return DECLINED;
2003-07-08 05:12:04 +02:00
2010-05-11 07:41:08 +02:00
$self->log(LOGDEBUG, "result was $result->code");
2003-06-27 19:27:35 +02:00
2012-06-22 11:38:01 +02:00
if ( ! $transaction->header ) {
$self->log(LOGERROR, "missing headers!");
return DECLINED;
};
2012-06-23 06:52:05 +02:00
$transaction->header->add('Received-SPF', $result->received_spf_header, 0);
2004-02-03 03:57:04 +01:00
2010-05-11 07:41:08 +02:00
return DECLINED;
2003-06-27 19:27:35 +02:00
}
2012-06-03 03:44:46 +02:00
sub is_in_relayclients {
2012-05-09 06:03:44 +02:00
my $self = shift;
my $client_ip = $self->qp->connection->remote_ip;
my @relay_clients = $self->qp->config('relayclients');
my $more_relay_clients = $self->qp->config('morerelayclients', 'map');
my %relay_clients = map { $_ => 1 } @relay_clients;
while ($client_ip) {
if ( exists $relay_clients{$client_ip} ||
exists $more_relay_clients->{$client_ip} ) {
2012-06-23 06:52:05 +02:00
$self->log( LOGDEBUG, "skip, IP in relayclients" );
2012-05-09 06:03:44 +02:00
return 1;
};
$client_ip =~ s/\d+\.?$// or last; # strip off another 8 bits
}
return;
};
sub is_special_recipient {
my ($self, $rcpt) = @_;
if ( ! $rcpt ) {
$self->log(LOGINFO, "skip: missing recipient");
return 1;
};
if ( ! $rcpt->user ) {
$self->log(LOGINFO, "skip: missing user");
return 1;
};
# special addresses don't get SPF-tested.
if ( $rcpt->user =~ /^(?:postmaster|abuse|mailer-daemon|root)$/i ) {
$self->log(LOGINFO, "skip: special user (".$rcpt->user.")");
return 1;
};
return;
};