lib/Mail/Decency/ContentFilter/DKIM.pm

filter: Statementstmt 43.9% 36 / 82 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 77.7% 7 / 9 Podpod 100% 1 / 1 totaltotal 28.9% 44 / 152 CC 25 SCAR 52.1
CC
25
Cov
27%
init
306.0
handle
90.0
BEGIN
1.0
CRAP 182.4
« lib/Mail/Decency/Core/SessionItem/ContentFilter.pm ↑ Summary lib/Mail/Decency/Policy/Greylist.pm »
1 package Mail::Decency::ContentFilter::DKIM;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends qw/
5 Mail::Decency::ContentFilter::Core
6 /;
7 1 with qw/
8 Mail::Decency::ContentFilter::Core::Spam
9 /;
10  
11 1 ▶use version 0.74; our $VERSION = qv( "v0.1.6" );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Mail::Decency::ContentFilter::Core::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Mail::DKIM::Signer;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Mail::DKIM::Verifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 Mail::Decency::ContentFilter::DKIM
23  
24 =head1 DESCRIPTION
25  
26 This module can be used for signing OR verifying mails. DONT USE BOTH IN THE SAME INSTANCE!!
27  
28 =head1 CONFIG FOR SIGN
29  
30 ---
31
32 disable: 0
33 #max_size: 0
34 #timeout: 30
35
36 enable_sign: 1
37
38 # the default key. can be used only or as fallback
39 sign_key: /etc/decency/dkim/default.key
40
41 # a directory where the keys per (sender) domain are.
42 # /etc/dkim/some-domain.co.uk.key
43 # /etc/dkim/other-domain.com.key
44 sign_key_dir: /etc/decency/dkim/domains
45
46 # the algorithmus and method .. change if you know what you are doing
47 #sign_algo: rsa-sha1
48 #sign_method: relaxed
49  
50  
51 =head1 CONFIG FOR VERIFY
52  
53 ---
54 DKIM:
55 enable_verify: 1
56
57 # signature present and fitting
58 #weight_pass: 50
59
60 # signature present, but incorrect
61 #weight_fail: -100
62
63 # signature malformed .. cannot be processed
64 #weight_invalid: -50
65
66 # some temporary error occured. Probably nothing bad
67 #weight_temperror: -10
68
69 # no key whats-o-ever found in mail, cannot verify
70 #weight_none: 0
71  
72 =head1 CLASS ATTRIBUTES
73  
74 =cut
75  
76  
77 # signing
78 1 has enable_sign => ( is => 'rw', isa => 'Bool', default => 0 );
79 1 has sign_key => ( is => 'rw', isa => 'Str', predicate => 'has_sign_key' );
80 1 has sign_key_dir => ( is => 'rw', isa => 'Str', predicate => 'has_sign_key_dir' );
81 1 has sign_algo => ( is => 'rw', isa => 'Str', default => 'rsa-sha1' );
82 1 has sign_method => ( is => 'rw', isa => 'Str', default => 'relaxed' );
83  
84 # verification
85 1 has enable_verify => ( is => 'rw', isa => 'Bool', default => 0 );
86 1 has weight_pass => ( is => 'rw', isa => 'Int', default => 15 );
87 1 has weight_fail => ( is => 'rw', isa => 'Int', default => -50 );
88 1 has weight_invalid => ( is => 'rw', isa => 'Int', default => -25 );
89 1 has weight_temperror => ( is => 'rw', isa => 'Int', default => 0 );
90 1 has weight_none => ( is => 'rw', isa => 'Int', default => 0 );
91  
92  
93 =head1 METHODS
94  
95  
96 =head2 init
97  
98 =cut
99  
100 0 ▶sub init {
Subroutine init: not called CC=17 SCAR=57.2
101 0 my ( $self ) = @_;
102
103 # init base, assure we get mime encoded
104 0 $self->next::method();
105
106 # wheter signing is enabled
107 0 ▶ if ( $self->config->{ enable_sign } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"enable_sign"}) 0 0 0
108 0 $self->enable_sign( 1 );
109
110 # having sign key
111 0 ▶ if ( $self->config->{ sign_key } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"sign_key"}) 0 0 0
112 die "Sign key '". $self->config->{ sign_key }. "' does not exist or not readable\n"
113 0 ▶ unless -f $self->config->{ sign_key };
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->config->{"sign_key"} 0 0 0
114 $self->sign_key( $self->config->{ sign_key } )
115 0 }
116
117 # having sign key dir (domain.tld.key)
118 0 ▶ if ( $self->config->{ sign_key_dir } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"sign_key_dir"}) 0 0 0
119 die "Sign key dir '". $self->config->{ sign_key_dir }. "' is not a directory or not readable\n"
120 0 ▶ unless -d $self->config->{ sign_key_dir };
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->config->{"sign_key_dir"} 0 0 0
121 $self->sign_key( $self->config->{ sign_key_dir } )
122 0 }
123
124 # at least one
125 0 ▶ die "Require 'sign_key' and/or 'sign_key_dir'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_sign_key and $self->has_sign_key_dir 0 0 0
Condition0%0/3
$self->has_sign_key and $self->has_sign_key_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_sign_key and $self->has_sign_key_dir
$self->has_sign_key $self->has_sign_key_dir
Truth table0%0/3
A and B
A= $self->has_sign_keyB= $self->has_sign_key_dir
ABresult
0X0
100
111
126 unless $self->has_sign_key && $self->has_sign_key_dir;
127
128 # update other args
129 0 foreach my $attr( qw/ sign_algo sign_method / ) {
130 $self->$attr( $self->config->{ $attr } )
131 0 ▶ if defined $self->config->{ $attr };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->config->{$attr} 0 0 0
132 }
133
134 }
135
136 # enable verify
137 0 ▶ if ( $self->config->{ enable_verify } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"enable_verify"}) 0 0 0
138 0 $self->enable_verify( 1 );
139
140 # get weightings
141 0 foreach my $weight( qw/ weight_pass weight_fail weight_invalid weight_temperror weight_none / ) {
142 $self->$weight( $self->config->{ $weight } )
143 0 ▶ if defined $self->config->{ $weight };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->config->{$weight} 0 0 0
144 }
145 }
146
147 0 ▶ die "DKIM: Enable one of enable_sign or enable_verify. Never both, never none.!\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->enable_sign and $self->enable_verify or not $self->enable_sign || $self->enable_verify 0 0 0
Condition0%0/9
$self->enable_sign and $self->enable_verify or not $self->enable_sign || $self->enable_verify
l!l&&r!l&&!r
000
$self->enable_sign and $self->enable_verify
!ll&&!rl&&r
000
$self->enable_sign || $self->enable_verify
l!l&&r!l&&!r
000
MC/DC0%0/4
$self->enable_sign and $self->enable_verify or not $self->enable_sign || $self->enable_verify
$self->enable_sign $self->enable_verify $self->enable_sign $self->enable_verify
Truth table0%0/7
A and B or not(C || D)
A= $self->enable_signB= $self->enable_verifyC= $self->enable_signD= $self->enable_verify
ABCDresult
11XX1
0X001
10001
0X1X0
0X010
101X0
10010
148 if $self->enable_sign && $self->enable_verify || ( ! $self->enable_sign && ! $self->enable_verify );
149
150 }
151  
152  
153 =head2 handle
154  
155 Default handling for any content filter is getting info about the to be filterd file
156  
157 =cut
158  
159  
160 0 ▶sub handle {
Subroutine handle: not called CC=9 SCAR=45.0
Pod documented
161 0 my ( $self ) = @_;
162
163 # verify mail
164 0 ▶ if ( $self->enable_verify ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->enable_verify) { } 0 0 0
165
166 # open file for read
167 0 ▶ open my $fh, '<', $self->file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->file or die q[Cannot open file '] . $self->file . "' for DKIM read\n" 0 0 0
168 or die "Cannot open file '". $self->file. "' for DKIM read\n";
169
170 # init verifier and load file
171 0 my $verifier = Mail::DKIM::Verifier->new;
172 #$verifier->load( $fh );
173 0 ▶ while( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
174 0 chomp;
175 0 s/\015\012?$//;
176 0 $verifier->PRINT( "$_\015\012" );
177 }
178
179 # close verifier and file
180 0 close $fh;
181 0 $verifier->CLOSE;
182
183 # get result
184 0 my $res = $verifier->result;
185
186 # handle result, if found
187 0 ▶ if ( $res && ( my $meth = $self->can( "weight_$res" ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res and my $meth = $self->can("weight_$res")) { } 0 0 0
Condition0%0/3
$res and my $meth = $self->can("weight_$res")
!ll&&!rl&&r
000
MC/DC0%0/2
$res and my $meth = $self->can("weight_$res")
$res my $meth = $self->can("weight_$res")
Truth table0%0/3
A and B
A= $resB= my $meth = $self->can("weight_$res")
ABresult
0X0
100
111
188 0 $self->logger->debug2( "Got result '$res'" );
189 0 return $self->add_spam_score( $self->$meth, [ "Result: ". $verifier->result_detail ] );
190 }
191 else {
192 0 $self->logger->error( "Unknown DKIM result '$res'" );
193 }
194 }
195
196 # sign mail
197 else {
198 # determine domain
199 0 my ( $prefix, $domain ) = split( /@/, $self->from, 2 );
200
201 # determine key file (having dir, trye "domain.tld.key" there, then fallback to normal)
202 0 ▶ my $key_file = $self->has_sign_key_dir && -f $self->sign_key_dir . "/${domain}.key"
Branch: 1 branch
BranchTrueFalseTotal
$self->has_sign_key_dir && -f $self->sign_key_dir . "/$domain.key" ? : 0 0 0
Condition0%0/3
$self->has_sign_key_dir and -f $self->sign_key_dir . "/$domain.key"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_sign_key_dir and -f $self->sign_key_dir . "/$domain.key"
$self->has_sign_key_dir -f $self->sign_key_dir . "/$domain.key"
Truth table0%0/3
A and B
A= $self->has_sign_key_dirB= -f $self->sign_key_dir . "/$domain.key"
ABresult
0X0
100
111
203 ? $self->sign_key_dir . "/${domain}.key"
204 : $self->sign_key
205 ;
206
207 # found key file
208 0 ▶ if ( $key_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_file) 0 0 0
209 0 $self->logger->debug0( "Sign mail from '". $self->from. "' to '". $self->to. "' with '$key_file'" );
210
211 # create new signer
212 0 my $signer = Mail::DKIM::Signer->new(
213 Algorithm => $self->sign_algo,
214 Method => $self->sign_method,
215 Domain => $domain,
216 KeyFile => $key_file
217 );
218
219 # open file and load into signer
220 0 open my $fh, '<', $self->file;
221 0 $signer->load( $fh );
222
223 # close both
224 0 close $fh;
225 0 $signer->CLOSE;
226
227 # update header in mime
228 0 my $mime = $self->mime;
229 0 $mime->head->replace( 'DKIM-Signature' => $signer->signature->as_string );
230 0 $self->write_mime;
231 }
232 }
233
234 0 return ;
235 }
236  
237  
238  
239 =head1 AUTHOR
240  
241 Ulrich Kautz <uk@fortrabbit.de>
242  
243 =head1 COPYRIGHT
244  
245 Copyright (c) 2010 the L</AUTHOR> as listed above
246  
247 =head1 LICENCSE
248  
249 This library is free software and may be distributed under the same terms as perl itself.
250  
251 =cut
252  
253 1 1;
« lib/Mail/Decency/Core/SessionItem/ContentFilter.pm ↑ Summary lib/Mail/Decency/Policy/Greylist.pm »