2003-06-27 14:25:52 +02:00
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
SPF - plugin to implement Sender Permitted From
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
# in config/plugins
|
|
|
|
sender_permitted_from
|
|
|
|
|
2003-06-27 19:27:35 +02:00
|
|
|
Or if you wish to issue 5xx on SPF fail:
|
|
|
|
|
|
|
|
sender_permitted_from spf_deny 1
|
|
|
|
|
2004-02-03 03:57:04 +01:00
|
|
|
Other arguments are 'trust 0' and 'guess 0'. These turn off processing of
|
|
|
|
spf.trusted-forwarders.org and the best_guess functionality. It is unlikely
|
|
|
|
that you want to turn these off.
|
|
|
|
|
|
|
|
Adding 'spf_deny 2' will also issue a 5xx on a softfail response.
|
|
|
|
|
|
|
|
You can also specify local SPF policy with
|
|
|
|
|
|
|
|
include '<spf mechanism list>'
|
|
|
|
|
2003-06-28 01:00:52 +02:00
|
|
|
See also http://spf.pobox.com/
|
|
|
|
|
2003-06-27 14:25:52 +02:00
|
|
|
=cut
|
|
|
|
|
2004-02-03 03:57:04 +01:00
|
|
|
use Mail::SPF::Query 1.991;
|
2003-06-27 14:25:52 +02:00
|
|
|
|
|
|
|
sub register {
|
2003-06-27 19:27:35 +02:00
|
|
|
my ($self, $qp, @args) = @_;
|
|
|
|
%{$self->{_args}} = @args;
|
2003-06-27 14:25:52 +02:00
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_mail {
|
2003-06-27 14:25:52 +02:00
|
|
|
my ($self, $transaction, $sender) = @_;
|
|
|
|
|
|
|
|
return (DECLINED) unless ($sender->format ne "<>"
|
|
|
|
and $sender->host && $sender->user);
|
|
|
|
|
2004-02-03 03:57:04 +01:00
|
|
|
# If we are receving from a relay permitted host, then we are probably
|
|
|
|
# not the delivery system, and so we shouldn't check
|
|
|
|
|
2004-12-02 08:26:11 +01:00
|
|
|
return (DECLINED) if $self->qp->connection->relay_client();
|
2004-02-03 03:57:04 +01:00
|
|
|
my @relay_clients = $self->qp->config("relayclients");
|
|
|
|
my $more_relay_clients = $self->qp->config("morerelayclients", "map");
|
|
|
|
my %relay_clients = map { $_ => 1 } @relay_clients;
|
|
|
|
my $client_ip = $self->qp->connection->remote_ip;
|
|
|
|
while ($client_ip) {
|
|
|
|
return (DECLINED) if exists $relay_clients{$client_ip};
|
|
|
|
return (DECLINED) if exists $more_relay_clients->{$client_ip};
|
|
|
|
$client_ip =~ s/\d+\.?$//; # strip off another 8 bits
|
|
|
|
}
|
|
|
|
|
2003-06-27 14:25:52 +02:00
|
|
|
my $host = lc $sender->host;
|
|
|
|
my $from = $sender->user . '@' . $host;
|
|
|
|
|
|
|
|
my $ip = $self->qp->connection->remote_ip;
|
2003-12-11 10:07:51 +01:00
|
|
|
my $helo = $self->qp->connection->hello_host;
|
2004-02-03 03:57:04 +01:00
|
|
|
|
|
|
|
my $query = Mail::SPF::Query->new(ip => $ip, sender => $from, helo => $helo,
|
|
|
|
sanitize => 1,
|
|
|
|
local => $self->{_args}{local},
|
|
|
|
guess => defined($self->{_args}{guess}) ? $self->{_args}{guess} : 1,
|
|
|
|
trusted => defined($self->{_args}{trust}) ? $self->{_args}{trust} : 1)
|
2003-06-27 14:25:52 +02:00
|
|
|
|| die "Couldn't construct Mail::SPF::Query object";
|
|
|
|
$transaction->notes('spfquery', $query);
|
|
|
|
|
|
|
|
return (DECLINED);
|
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_rcpt {
|
2003-06-27 14:25:52 +02:00
|
|
|
my ($self, $transaction, $rcpt) = @_;
|
2003-06-28 01:00:52 +02:00
|
|
|
|
|
|
|
# special addresses don't get SPF-tested.
|
|
|
|
return DECLINED if $rcpt and $rcpt->user and $rcpt->user =~ /^(?:postmaster|abuse|mailer-daemon|root)$/i;
|
|
|
|
|
2003-06-27 14:25:52 +02:00
|
|
|
my $query = $transaction->notes('spfquery');
|
2004-02-03 03:57:04 +01:00
|
|
|
|
|
|
|
return DECLINED if !$query;
|
|
|
|
my ($result, $smtp_comment, $comment) = $query->result2($rcpt->address);
|
2003-06-27 14:25:52 +02:00
|
|
|
|
2004-02-03 03:57:04 +01:00
|
|
|
if ($result eq "error") {
|
|
|
|
return (DENYSOFT, "SPF error: $smtp_comment");
|
|
|
|
}
|
2003-06-27 19:27:35 +02:00
|
|
|
|
|
|
|
if ($result eq "fail" and $self->{_args}{spf_deny}) {
|
2004-02-03 03:57:04 +01:00
|
|
|
return (DENY, "SPF forgery: $smtp_comment");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result eq "softfail" and $self->{_args}{spf_deny} > 1) {
|
|
|
|
return (DENY, "SPF probable forgery: $smtp_comment");
|
|
|
|
}
|
2003-06-28 01:00:52 +02:00
|
|
|
|
2004-02-03 03:57:04 +01:00
|
|
|
if ($result eq 'fail' or $result eq 'softfail') {
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGDEBUG, "result for $rcpt->address was $result: $comment");
|
2003-06-27 14:25:52 +02:00
|
|
|
}
|
|
|
|
|
2003-06-28 01:00:52 +02:00
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _uri_escape {
|
|
|
|
my $str = shift;
|
|
|
|
$str =~ s/([^A-Za-z0-9\-_.!~*\'()])/sprintf "%%%X", ord($1)/eg;
|
|
|
|
return $str;
|
2003-06-27 14:25:52 +02:00
|
|
|
}
|
|
|
|
|
2005-07-18 13:13:17 +02:00
|
|
|
sub hook_data_post {
|
2004-02-03 03:57:04 +01:00
|
|
|
my ($self, $transaction) = @_;
|
2003-07-08 05:12:04 +02:00
|
|
|
|
2004-02-03 03:57:04 +01:00
|
|
|
my $query = $transaction->notes('spfquery');
|
|
|
|
return DECLINED if !$query;
|
|
|
|
|
|
|
|
my ($result, $smtp_comment, $comment) = $query->message_result2();
|
2003-07-08 05:12:04 +02:00
|
|
|
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGDEBUG, "result was $result: $comment") if ($result);
|
2003-06-27 19:27:35 +02:00
|
|
|
|
2004-02-03 03:57:04 +01:00
|
|
|
$transaction->header->add('Received-SPF' => "$result ($comment)", 0);
|
|
|
|
|
|
|
|
return DECLINED;
|
2003-06-27 19:27:35 +02:00
|
|
|
}
|
|
|
|
|