2012-04-29 10:35:59 +02:00
#!perl -w
2012-05-04 20:02:12 +02:00
=head1 NAME
domainkeys: validate a DomainKeys signature on an incoming mail
=head1 SYNOPSIS
2012-05-05 09:54:47 +02:00
domainkeys [reject 1]
2012-05-04 20:02:12 +02:00
2012-05-05 09:54:47 +02:00
Performs a DomainKeys validation on the message.
2012-05-04 20:02:12 +02:00
2013-03-28 22:47:02 +01:00
=head1 DEPRECATION
2013-04-20 22:13:51 +02:00
You should probably NOT be using this plugin. DomainKeys has been deprecated in favor of DKIM. That being said, it's March 2013 and I'm still seeing ham arrive with DomainKeys signatures.
2013-03-28 22:47:02 +01:00
2012-05-05 09:54:47 +02:00
=head1 CONFIGURATION
2012-05-04 20:02:12 +02:00
2012-05-05 09:54:47 +02:00
=head2 reject
reject 1
Reject is a boolean that toggles message rejection on or off. Messages failing
DomainKeys validation are rejected by default.
=head2 reject_type
reject_type [ temp | perm ]
The default rejection type is permanent.
=head2 warn_only
A deprecated option that disables message rejection. See reject instead.
2012-05-04 20:02:12 +02:00
=head1 COPYRIGHT
Copyright (C) 2005-2006 John Peacock.
Portions Copyright (C) 2004 Anthony D. Urso. All rights reserved. This
program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
2012-05-05 09:54:47 +02:00
=head1 AUTHORS
Matt Simerson - 2012
John Peacock - 2005-2006
Anthony D. Urso. - 2004
2012-05-04 20:02:12 +02:00
=cut
2012-05-05 09:54:47 +02:00
use strict;
use warnings;
use Qpsmtpd::Constants;
2006-07-12 20:10:00 +02:00
sub init {
2006-07-24 21:10:38 +02:00
my ($self, $qp, %args) = @_;
2006-07-12 20:10:00 +02:00
2013-04-21 06:50:39 +02:00
foreach my $key (%args) {
2012-05-04 20:02:12 +02:00
$self->{$key} = $args{$key};
2006-07-12 20:10:00 +02:00
}
2013-04-21 06:50:39 +02:00
$self->{reject} = 1 if !defined $self->{reject}; # default reject
$self->{reject_type} = 'perm' if !defined $self->{reject_type};
2012-05-05 09:54:47 +02:00
2013-04-21 06:50:39 +02:00
if ($args{'warn_only'}) {
2012-05-05 09:54:47 +02:00
$self->log(LOGNOTICE, "warn_only is deprecated. Use reject instead");
$self->{'reject'} = 0;
2013-04-21 06:50:39 +02:00
}
2006-07-12 20:10:00 +02:00
}
2012-05-21 11:59:44 +02:00
sub register {
my $self = shift;
2013-04-21 06:50:39 +02:00
for my $m (qw/ Mail::DomainKeys::Message Mail::DomainKeys::Policy /) {
2012-05-21 11:59:44 +02:00
eval "use $m";
2013-04-21 06:50:39 +02:00
if ($@) {
2012-05-21 11:59:44 +02:00
warn "skip: plugin disabled, could not load $m\n";
$self->log(LOGERROR, "skip: plugin disabled, is $m installed?");
return;
2013-04-21 06:50:39 +02:00
}
}
2012-05-21 11:59:44 +02:00
$self->register_hook('data_post', 'data_post_handler');
2013-04-21 06:50:39 +02:00
}
2012-05-21 11:59:44 +02:00
sub data_post_handler {
2006-07-11 19:41:48 +02:00
my ($self, $transaction) = @_;
2006-07-09 02:58:39 +02:00
2012-06-22 11:38:01 +02:00
return DECLINED if $self->is_immune();
2013-04-21 06:50:39 +02:00
if (!$transaction->header->get('DomainKey-Signature')) {
2012-06-25 08:41:43 +02:00
$self->log(LOGINFO, "skip, unsigned");
2012-05-05 09:54:47 +02:00
return DECLINED;
2013-04-21 06:50:39 +02:00
}
2012-06-03 03:44:46 +02:00
2013-04-21 06:50:39 +02:00
my $body = $self->assemble_body($transaction);
2006-07-09 02:58:39 +02:00
2013-04-21 06:50:39 +02:00
my $message =
load Mail::DomainKeys::Message(
HeadString => $transaction->header->as_string,
BodyReference => $body)
or do {
$self->log(LOGWARN, "skip, unable to load message"), return DECLINED;
};
2006-07-09 02:58:39 +02:00
2012-05-05 09:54:47 +02:00
# no sender domain means no verification
2013-04-21 06:50:39 +02:00
if (!$message->senderdomain) {
2012-06-25 08:41:43 +02:00
$self->log(LOGINFO, "skip, failed to parse sender domain"),
2013-04-21 06:50:39 +02:00
return DECLINED;
}
2006-07-09 02:58:39 +02:00
2013-04-21 06:50:39 +02:00
my $status = $self->get_message_status($message);
2006-07-09 02:58:39 +02:00
2013-04-21 06:50:39 +02:00
if (defined $status) {
2012-06-25 08:41:43 +02:00
$transaction->header->add("DomainKey-Status", $status, 0);
$self->log(LOGINFO, "pass, $status");
2012-05-05 09:54:47 +02:00
return DECLINED;
2013-04-21 06:50:39 +02:00
}
2012-05-05 09:54:47 +02:00
2012-06-25 08:41:43 +02:00
$self->log(LOGERROR, "fail, signature invalid");
2013-04-21 06:50:39 +02:00
return DECLINED if !$self->{reject};
2012-05-05 09:54:47 +02:00
my $deny = $self->{reject_type} eq 'temp' ? DENYSOFT : DENY;
2012-06-25 08:41:43 +02:00
return ($deny, "DomainKeys signature validation failed");
2006-07-09 02:58:39 +02:00
}
2012-05-05 09:54:47 +02:00
sub get_message_status {
my ($self, $message) = @_;
2013-04-21 06:50:39 +02:00
if ($message->testing) {
return "testing"; # key testing, don't do anything else
}
2012-05-05 09:54:47 +02:00
2013-04-21 06:50:39 +02:00
if ($message->signed && $message->verify) {
return $message->signature->status; # verified: add good header
}
2012-05-05 09:54:47 +02:00
# not signed or not verified
2013-04-21 06:50:39 +02:00
my $policy =
fetch Mail::DomainKeys::Policy(Protocol => 'dns',
Domain => $message->senderdomain);
2012-05-05 09:54:47 +02:00
2013-04-21 06:50:39 +02:00
if (!$policy) {
2012-05-05 09:54:47 +02:00
return $message->signed ? "non-participant" : "no signature";
2013-04-21 06:50:39 +02:00
}
2012-05-05 09:54:47 +02:00
2013-04-21 06:50:39 +02:00
if ($policy->testing) {
return "testing"; # Don't do anything else
}
2012-05-05 09:54:47 +02:00
2013-04-21 06:50:39 +02:00
if ($policy->signall) {
return undef; # policy requires all mail to be signed
}
2012-05-05 09:54:47 +02:00
# $policy->signsome
2013-04-21 06:50:39 +02:00
return "no signature"; # not signed and domain doesn't sign all
}
2012-05-05 09:54:47 +02:00
sub assemble_body {
my ($self, $transaction) = @_;
$transaction->body_resetpos;
2013-04-21 06:50:39 +02:00
$transaction->body_getline; # \r\n seperator is NOT part of the body
2012-05-05 09:54:47 +02:00
my @body;
while (my $line = $transaction->body_getline) {
push @body, $line;
}
return \@body;
2013-04-21 06:50:39 +02:00
}