qpsmtpd/plugins/dkim

500 lines
15 KiB
Plaintext
Raw Normal View History

2012-06-27 09:26:38 +02:00
#!perl -w
=head1 NAME
dkim: validate DomainKeys and (DKIM) Domain Keys Indentified Messages
=head1 SYNOPSIS
2013-03-28 22:47:18 +01:00
Validate the DKIM and Domainkeys signatures of a message, enforce DKIM
sending policies, and DKIM sign outgoing messages.
2012-06-27 09:26:38 +02:00
=head1 CONFIGURATION
2013-03-28 22:47:18 +01:00
=head2 reject [ 0 | 1 | naughty ]
2012-06-27 09:26:38 +02:00
2013-03-28 22:47:18 +01:00
dkim reject 0
2012-06-27 09:26:38 +02:00
2013-04-15 03:42:21 +02:00
0 - do not reject
1 - reject messages that fail DKIM policy
naughty - defer rejection to the B<naughty> plugin
2012-06-27 09:26:38 +02:00
Default: 1
=head2 reject_type
dkim reject_type [ temp | perm ]
Default: perm
2013-03-28 22:47:18 +01:00
=head1 HOW TO SIGN
2013-04-15 03:42:21 +02:00
=head2 generate DKIM keys
=head3 the easy way
cd ~smtpd/config/dkim; ./dkim_key_gen.sh example.org
=head3 the manual way
2013-03-28 22:47:18 +01:00
mkdir -p ~smtpd/config/dkim/example.org
cd ~smtpd/config/dkim/example.org
2013-04-15 03:42:21 +02:00
echo 'may2013' > selector
2013-03-28 22:47:18 +01:00
openssl genrsa -out private 2048
chmod 400 private
openssl rsa -in private -out public -pubout
2013-04-15 03:42:21 +02:00
chown -R smtpd:smtpd ../example.org
2013-03-28 22:47:18 +01:00
2013-04-15 03:42:21 +02:00
After generating the keys, there will be three files in the example.org directory: selector, private, and public.
2013-03-28 22:47:18 +01:00
=head3 selector
The selector can be any value that is a valid DNS label.
=head3 key length
2013-04-15 03:42:21 +02:00
The minimum recommended key length for short duration keys (ones that will be replaced within a few months) is 1024. If you are unlikely to rotate your keys frequently, go with 2048, at the expense of a bit more CPU.
2013-03-28 22:47:18 +01:00
=head2 publish public key in DNS
2013-04-15 03:42:21 +02:00
If the DKIM keys were generated the easy way, there will be a fourth file named I<dns>. The contents contain the DNS formatted record of the public key, as well as suggestions for DKIM, SPF, and DMARC policy records. The records are ready to be copy/pasted into a BIND zone file, or better yet, NicTool, and published to most any DNS server. If you didn't create your keys the easy way, look inside the dkim_key_gen.sh script to see the commands used to format the DKIM public key.
The example DKIM, SPF, and DMARC policy records in the I<dns> file are strict, telling other mail servers that if a sender claims to be from example.org, but the message is not DKIM signed and not SPF aligned, then the message should be rejected. Many email servers, including the largest email providers (Gmail, Yahoo, Outlook/Live/Hotmail) will refuse to accept such messages, greatly reducing the harm caused by miscreants who send out spam with your domain name in the From header.
The DKIM record will look like this:
may2013._domainkey TXT "v=DKIM1;p=[public key stripped of whitespace];"
And the values in the address have the following meaning:
2013-03-28 22:47:18 +01:00
hash: h=[ sha1 | sha256 ]
test; t=[ s | s:y ]
granularity: g=[ ]
notes: n=[ ]
services: s=[email]
keytypes: [ rsa ]
2013-04-15 03:42:21 +02:00
=head2 testing
2013-03-28 22:47:18 +01:00
2013-04-15 03:42:21 +02:00
After confirming that the DKIM public key can be fetched with DNS (dig TXT may2013._domainkey.example.org. @ns1.example.org.), send test messages. There are a number of ways to test your DKIM:
2013-03-28 22:47:18 +01:00
2013-04-15 03:42:21 +02:00
* email to a Gmail address and inspect the Authentication-Results header.
* email to check-auth@verifier.port25.com
* email to checkmyauth@auth.returnpath.net
The two DKIM relays provide a nice email report with additional debugging information.
=head2 publish DKIM policy in DNS
_domainkey TXT "o=~; t=y; r=postmaster@example.org"
2013-03-28 22:47:18 +01:00
2013-04-15 03:42:21 +02:00
o=- - all are signed
o=~ - some are signed
t=y - test mode
r=[email] - responsible email address
n=[notes]
Once DKIM and SPF are tested and working, update the policy, changing o=~ to o=-, so that other mail servers reject unsigned messages claiming to be from your domain.
As of this writing, most mail servers do not reject messages that fail DKIM policy, unless they also fail SPF, and no DMARC policy is published. The same holds true for SPF. There are technical reasons for this. See DMARC for more information, how you can control change that behavior, as well as receiving feedback from remote servers about messages they have accepted and rejected from senders claiming the identity of your domain(s).
2013-03-28 22:47:18 +01:00
=head2 Sign for others
Following the directions above will configure QP to DKIM sign messages from authenticated senders from example.org. Suppose you host client.com and would like to DKIM sign their messages too? Do that as follows:
cd ~smtpd/config/dkim/example.org
ln -s example.org client.com
QP will follow the symlink target and sign client.com emails with the example.org DKIM key.
2013-04-15 03:42:21 +02:00
CAUTION: just because you can, doesn't mean you should. Even with a relaxed DKIM policy, if you don't have a suitable DMARC record published for client.com, they may encounter deliverability problems. It is better to have keys generated and published for each domain.
2012-06-27 09:26:38 +02:00
=head1 SEE ALSO
http://www.dkim.org/
2013-03-28 22:47:18 +01:00
http://tools.ietf.org/html/rfc6376 - DKIM Signatures
2012-06-27 09:26:38 +02:00
http://tools.ietf.org/html/rfc5863 - DKIM Development, Deployment, & Operations
http://tools.ietf.org/html/rfc5617 - DKIM ADSP (Author Domain Signing Practices)
http://tools.ietf.org/html/rfc5585 - DKIM Service Overview
http://tools.ietf.org/html/rfc5016 - DKIM Signing Practices Protocol
2013-03-28 22:47:18 +01:00
http://tools.ietf.org/html/rfc4871 - DKIM Signatures
2012-06-27 09:26:38 +02:00
http://tools.ietf.org/html/rfc4870 - DomainKeys
2013-03-28 22:47:18 +01:00
http://dkimcore.org/tools/
http://www.protodave.com/tools/dkim-key-checker/
2012-06-27 09:26:38 +02:00
=head1 AUTHORS
2013-04-15 03:42:21 +02:00
2013 - Matt Simerson - added DKIM signing and key creation script
2012-06-27 09:26:38 +02:00
2012 - Matt Simerson - initial plugin
=head1 ACKNOWLEDGEMENTS
David Summers - http://www.nntp.perl.org/group/perl.qpsmtpd/2010/08/msg9417.html
Matthew Harrell - http://alecto.bittwiddlers.com/files/qpsmtpd/dkimcheck
2012-11-16 20:35:19 +01:00
I first attempted to fix the dkimcheck plugin, but soon scrapped that effort and wrote this one. Why?
=over 4
2012-06-27 09:26:38 +02:00
The nine 'if' brackets with 19 conditionals, and my inability to easily determine which of the 15 possible permutations (5 signature validation results x 3 possible policy results) were covered.
The use of $dkim->fetch_author_policy, which is deprecated by Mail::DKIM.
The paradim of a single policy, when DKIM supports 0 or many. Although I may yet implement the 'local' policy idea, so long as I'm confident it will never result in a false positive.
The OBF programming style, which is nigh impossible to test.
2012-11-16 20:35:19 +01:00
=back
2012-06-27 09:26:38 +02:00
=cut
use strict;
use warnings;
use Qpsmtpd::Constants;
# use Mail::DKIM::Verifier; # eval'ed in register()
use Socket qw(:DEFAULT :crlf);
sub init {
my ($self, $qp) = (shift, shift);
$self->{_args} = { @_ };
$self->{_args}{reject} = 1 if ! defined $self->{_args}{reject};
$self->{_args}{reject_type} ||= 'perm';
}
sub register {
my $self = shift;
2013-03-28 22:47:18 +01:00
foreach my $mod ( qw/ Mail::DKIM::Verifier Mail::DKIM::Signer Mail::DKIM::TextWrap / ) {
eval "use $mod";
if ( $@ ) {
warn "error, plugin disabled, could not load $mod\n";
$self->log(LOGERROR, "skip, plugin disabled, is Mail::DKIM installed?");
return;
};
2012-06-27 09:26:38 +02:00
};
$self->register_hook('data_post', 'data_post_handler');
};
sub data_post_handler {
my ($self, $transaction) = @_;
2013-03-28 22:47:18 +01:00
if ( $self->qp->connection->relay_client() ) {
# this is one of our authenticated users sending a message.
return $self->sign_it( $transaction );
};
2012-06-27 09:26:38 +02:00
return DECLINED if $self->is_immune();
2013-03-28 22:47:18 +01:00
return $self->validate_it( $transaction );
};
sub validate_it {
my ($self, $transaction) = @_;
# Incoming message, perform DKIM validation
2012-06-27 09:26:38 +02:00
my $dkim = Mail::DKIM::Verifier->new() or do {
$self->log(LOGERROR, "error, could not instantiate a new Mail::DKIM::Verifier");
return DECLINED;
};
2013-03-28 22:47:18 +01:00
$self->send_message_to_dkim( $dkim, $transaction );
my $result = $dkim->result;
2012-06-27 09:26:38 +02:00
my $mess = $self->get_details( $dkim );
foreach my $r ( qw/ pass fail invalid temperror none / ) {
my $handler = 'handle_sig_' . $r;
if ( $result eq $r && $self->can( $handler ) ) {
#$self->log(LOGINFO, "dispatching $result to $handler");
return $self->$handler( $dkim, $mess );
};
};
$self->log( LOGERROR, "unknown result: $result, $mess" );
return DECLINED;
}
2013-03-28 22:47:18 +01:00
sub sign_it {
my ($self, $transaction) = @_;
my ($domain, $keydir) = $self->get_keydir($transaction) or return DECLINED;
my $selector = $self->get_selector($keydir);
my $dkim = Mail::DKIM::Signer->new(
Algorithm => "rsa-sha256",
Method => "relaxed",
Domain => $domain,
Selector => $selector,
KeyFile => "$keydir/private",
);
$self->send_message_to_dkim( $dkim, $transaction );
my $signature = $dkim->signature; # what is the signature result?
$self->qp->transaction->header->add(
'DKIM-Signature', $signature->as_string, 0 );
$self->log(LOGINFO, "pass, signed message, ", $signature->as_string );
return DECLINED;
};
2012-06-27 09:26:38 +02:00
sub get_details {
my ($self, $dkim ) = @_;
my @data;
my $string;
push @data, "domain: " . $dkim->signature->domain if $dkim->signature;
push @data, "selector: " . $dkim->signature->selector if $dkim->signature;
push @data, "result: " . $dkim->result_detail if $dkim->result_detail;
foreach my $policy ( $dkim->policies ) {
next if ! $policy;
push @data, "policy: " . $policy->as_string;
push @data, "name: " . $policy->name;
push @data, "policy_location: " . $policy->location if $policy->location;
my $policy_result;
$policy_result = $policy->apply($dkim);
$policy_result or next;
push @data, "policy_result: " . $policy_result if $policy_result;
};
return join(', ', @data);
};
sub handle_sig_fail {
my ( $self, $dkim, $mess ) = @_;
$self->adjust_karma( -1 );
2012-06-27 09:26:38 +02:00
return $self->get_reject( "DKIM signature invalid: " . $dkim->result_detail, $mess );
};
sub handle_sig_temperror {
my ( $self, $dkim, $mess ) = @_;
$self->log(LOGINFO, "error, $mess" );
return ( DENYSOFT, "Please try again later - $dkim->result_detail" );
};
sub handle_sig_invalid {
my ( $self, $dkim, $mess ) = @_;
my ( $prs, $policies) = $self->get_policy_results( $dkim );
2013-03-28 22:47:18 +01:00
foreach my $policy ( @$policies ) {
if ( $policy->signall && ! $policy->is_implied_default_policy ) {
$self->log(LOGINFO, $mess );
return $self->get_reject(
"invalid DKIM signature with sign-all policy",
"invalid signature, sign-all policy"
);
}
2012-06-27 09:26:38 +02:00
};
$self->adjust_karma( -1 );
2012-06-27 09:26:38 +02:00
$self->log(LOGINFO, $mess );
if ( $prs->{accept} ) {
$self->add_header( $mess );
$self->log( LOGERROR, "error, invalid signature but accept policy!?" );
return DECLINED;
}
elsif ( $prs->{neutral} ) {
$self->add_header( $mess );
$self->log( LOGERROR, "error, invalid signature but neutral policy?!" );
return DECLINED;
}
elsif ( $prs->{reject} ) {
2013-03-28 22:47:18 +01:00
return $self->get_reject(
2012-06-27 09:26:38 +02:00
"invalid DKIM signature: " . $dkim->result_detail,
2013-03-28 22:47:18 +01:00
"fail, invalid signature, reject policy"
2012-06-27 09:26:38 +02:00
);
}
# this should never happen
$self->log( LOGINFO, "error, invalid signature, unhandled" );
$self->add_header( $mess );
return DECLINED;
};
sub handle_sig_pass {
my ( $self, $dkim, $mess ) = @_;
my ($prs) = $self->get_policy_results( $dkim );
if ( $prs->{accept} ) {
$self->add_header( $mess );
$self->log(LOGINFO, "pass, valid signature, accept policy");
$self->adjust_karma( 1 );
2012-06-27 09:26:38 +02:00
return DECLINED;
}
elsif ( $prs->{neutral} ) {
$self->add_header( $mess );
$self->log(LOGINFO, "pass, valid signature, neutral policy");
$self->log(LOGINFO, $mess );
return DECLINED;
}
elsif ( $prs->{reject} ) {
$self->log(LOGINFO, $mess );
$self->adjust_karma( -1 );
2012-06-27 09:26:38 +02:00
return $self->get_reject(
"DKIM signature valid but fails policy, $mess",
"fail, valid sig, reject policy"
);
};
# this should never happen
$self->add_header( $mess );
$self->log(LOGERROR, "pass, valid sig, no policy results" );
$self->log(LOGINFO, $mess );
return DECLINED;
};
sub handle_sig_none {
my ( $self, $dkim, $mess ) = @_;
my ( $prs, $policies) = $self->get_policy_results( $dkim );
2013-03-28 22:47:18 +01:00
foreach my $policy ( @$policies ) {
if ( $policy->signall && ! $policy->is_implied_default_policy ) {
$self->log(LOGINFO, $mess );
return $self->get_reject(
"no DKIM signature with sign-all policy",
"no signature, sign-all policy"
);
}
2012-06-27 09:26:38 +02:00
};
if ( $prs->{accept} ) {
$self->log( LOGINFO, "pass, no signature, accept policy" );
return DECLINED;
}
elsif ( $prs->{neutral} ) {
$self->log( LOGINFO, "pass, no signature, neutral policy" );
return DECLINED;
}
elsif ( $prs->{reject} ) {
$self->log(LOGINFO, $mess );
2013-03-28 22:47:18 +01:00
$self->get_reject(
2012-06-27 09:26:38 +02:00
"no DKIM signature, policy says reject: " . $dkim->result_detail,
"no signature, reject policy"
);
};
# should never happen
$self->log( LOGINFO, "error, no signature, no policy" );
$self->log(LOGINFO, $mess );
return DECLINED;
};
2013-03-28 22:47:18 +01:00
sub get_keydir {
my ($self, $transaction) = @_;
my $domain = $transaction->sender->host;
my $dir = "config/dkim/$domain";
if ( -l $dir ) {
$dir = readlink($dir);
$dir = "config/dkim/$dir" if $dir !~ /\//; # no /, relative path
($domain) = (split /\//, $dir)[-1];
};
if ( ! -d $dir ) {
$self->log(LOGINFO, "skip, DKIM not configured for $domain");
return;
};
if ( ! -r $dir ) {
$self->log(LOGINFO, "error, unable to read key from $dir");
return;
};
if ( ! -r "$dir/private" ) {
$self->log(LOGINFO, "error, unable to read dkim key from $dir/private");
return;
};
return ($domain, $dir);
};
sub send_message_to_dkim {
my ($self, $dkim, $transaction) = @_;
2012-06-27 09:26:38 +02:00
foreach ( split ( /\n/s, $transaction->header->as_string ) ) {
$_ =~ s/\r?$//s;
eval { $dkim->PRINT ( $_ . CRLF ); };
$self->log(LOGERROR, $@ ) if $@;
}
$transaction->body_resetpos;
while (my $line = $transaction->body_getline) {
chomp $line;
2013-03-28 22:47:18 +01:00
$line =~ s/\015$//;
2012-06-27 09:26:38 +02:00
eval { $dkim->PRINT($line . CRLF ); };
$self->log(LOGERROR, $@ ) if $@;
};
$dkim->CLOSE;
};
sub get_policies {
my ($self, $dkim) = @_;
my @policies;
eval { @policies = $dkim->policies };
$self->log(LOGERROR, $@ ) if $@;
return @policies;
};
sub get_policy_results {
my ( $self, $dkim ) = @_;
my %prs;
my @policies = $self->get_policies( $dkim );
foreach my $policy ( @policies ) {
my $policy_result;
eval { $policy_result = $policy->apply($dkim); }; # accept, reject, neutral
if ( $@ ) {
$self->log(LOGERROR, $@ );
};
$prs{$policy_result}++ if $policy_result;
};
return \%prs, \@policies;
};
2013-03-28 22:47:18 +01:00
sub get_selector {
my ($self, $keydir) = @_;
open my $SFH, '<', "$keydir/selector" or do {
$self->log(LOGINFO, "error, unable to read selector from $keydir/selector");
return DECLINED;
};
my $selector = <$SFH>;
chomp $selector;
close $SFH;
$self->log(LOGINFO, "info, selector: $selector");
return $selector;
};
2012-06-27 09:26:38 +02:00
sub add_header {
my $self = shift;
my $header = shift or return;
2013-03-28 22:47:18 +01:00
# consider adding Authentication-Results header, (RFC 5451)
2012-06-27 09:26:38 +02:00
$self->qp->transaction->header->add( 'X-DKIM-Authentication', $header, 0 );
}