Debian Codegraph @

plugins/domainkeys

raw
1 #!perl -w
2
3 =head1 NAME
4
5 domainkeys: validate a DomainKeys signature on an incoming mail
6
7 =head1 SYNOPSIS
8
9 domainkeys [reject 1]
10
11 Performs a DomainKeys validation on the message.
12
13 =head1 DEPRECATION
14
15 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.
16
17 =head1 CONFIGURATION
18
19 =head2 reject
20
21 reject 1
22
23 Reject is a boolean that toggles message rejection on or off. Messages failing
24 DomainKeys validation are rejected by default.
25
26 =head2 reject_type
27
28 reject_type [ temp | perm ]
29
30 The default rejection type is permanent.
31
32 =head2 warn_only
33
34 A deprecated option that disables message rejection. See reject instead.
35
36 =head1 COPYRIGHT
37
38 Copyright (C) 2005-2006 John Peacock.
39
40 Portions Copyright (C) 2004 Anthony D. Urso. All rights reserved. This
41 program is free software; you can redistribute it and/or modify it under
42 the same terms as Perl itself.
43
44 =head1 AUTHORS
45
46 Matt Simerson - 2013 - save results to Authentication-Results header
47 instead of DomainKey-Status
48 Matt Simerson - 2012 - refactored, added tests, safe loading
49 John Peacock - 2005-2006
50 Anthony D. Urso. - 2004
51
52 =cut
53
54 use strict;
55 use warnings;
56
57 use Qpsmtpd::Constants;
58
59 sub init {
60 my ($self, $qp, %args) = @_;
61
62 foreach my $key (%args) {
63 $self->{$key} = $args{$key};
64 }
65 $self->{reject} = 1 if !defined $self->{reject}; # default reject
66 $self->{reject_type} = 'perm' if !defined $self->{reject_type};
67
68 if ($args{'warn_only'}) {
69 $self->log(LOGNOTICE, "warn_only is deprecated. Use reject instead");
70 $self->{'reject'} = 0;
71 }
72 }
73
74 sub register {
75 my $self = shift;
76
77 for my $m (qw/ Mail::DomainKeys::Message Mail::DomainKeys::Policy /) {
78 eval "use $m";
79 if ($@) {
80 warn "skip: plugin disabled, could not load $m\n";
81 $self->log(LOGERROR, "skip: plugin disabled, is $m installed?");
82 return;
83 }
84 }
85
86 $self->register_hook('data_post', 'data_post_handler');
87 }
88
89 sub data_post_handler {
90 my ($self, $transaction) = @_;
91
92 return DECLINED if $self->is_immune();
93
94 if (!$transaction->header->get('DomainKey-Signature')) {
95 $self->log(LOGINFO, "skip, unsigned");
96 return DECLINED;
97 }
98
99 my $body = $self->assemble_body($transaction);
100
101 my $message =
102 load Mail::DomainKeys::Message(
103 HeadString => $transaction->header->as_string,
104 BodyReference => $body)
105 or do {
106 $self->log(LOGWARN, "skip, unable to load message"), return DECLINED;
107 };
108
109 # no sender domain means no verification
110 if (!$message->senderdomain) {
111 $self->log(LOGINFO, "skip, failed to parse sender domain"),
112 return DECLINED;
113 }
114
115 my $status = $self->get_message_status($message);
116
117 if (defined $status) {
118 #$transaction->header->add("DomainKey-Status", $status, 0);
119 $self->store_auth_results('domainkey=' . $status);
120 $self->log(LOGINFO, "pass, $status");
121 return DECLINED;
122 }
123
124 $self->log(LOGERROR, "fail, signature invalid");
125 return DECLINED if !$self->{reject};
126 my $deny = $self->{reject_type} eq 'temp' ? DENYSOFT : DENY;
127 return ($deny, "DomainKeys signature validation failed");
128 }
129
130 sub get_message_status {
131 my ($self, $message) = @_;
132
133 if ($message->testing) {
134 return "testing"; # key testing, don't do anything else
135 }
136
137 if ($message->signed && $message->verify) {
138 return $message->signature->status; # verified: add good header
139 }
140
141 # not signed or not verified
142 my $policy =
143 fetch Mail::DomainKeys::Policy(Protocol => 'dns',
144 Domain => $message->senderdomain);
145
146 if (!$policy) {
147 return $message->signed ? "non-participant" : "no signature";
148 }
149
150 if ($policy->testing) {
151 return "testing"; # Don't do anything else
152 }
153
154 if ($policy->signall) {
155 return undef; # policy requires all mail to be signed
156 }
157
158 # $policy->signsome
159 return "no signature"; # not signed and domain doesn't sign all
160 }
161
162 sub assemble_body {
163 my ($self, $transaction) = @_;
164
165 $transaction->body_resetpos;
166 $transaction->body_getline; # \r\n seperator is NOT part of the body
167
168 my @body;
169 while (my $line = $transaction->body_getline) {
170 push @body, $line;
171 }
172 return \@body;
173 }