STS/Domain.pm

filter: Statementstmt 34.9% 44 / 126 Branchbran 0.0% 0 / 48 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 24.0% 6 / 25 Podpod 25.0% 1 / 4 totaltotal 25.9% 56 / 216 SCAR 56.9
CC
26
Cov
26%
retrieve_policy
20.0
check_policy_update
20.0
__ANON__
12.0
CRAP 295.8
« STS/Policy.pm ↑ Summary STS.pm »
1 package Mail::STS::Domain;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.03'; # VERSION
6 # ABSTRACT: class for MTA-STS domain lookups
7  
8 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Time::Seconds;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Mail::STS::STSRecord;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Mail::STS::TLSRPTRecord;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Mail::STS::Policy;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 1 has 'domain' => (
17 is => 'ro',
18 isa => 'Str',
19 required => 1,
20 );
21  
22 1 has 'resolver' => (
23 is => 'ro',
24 isa => 'Net::DNS::Resolver',
25 required => 1,
26 );
27  
28 1 has 'agent' => (
29 is => 'ro',
30 isa => 'LWP::UserAgent',
31 required => 1,
32 );
33  
34 1 has 'max_policy_size' => (
35 is => 'rw',
36 isa => 'Maybe[Int]',
37 default => 65536,
38 );
39  
40 my $RECORDS = {
41 'mx' => {
42 type => 'MX',
43 },
44 'a' => {
45 type => ['AAAA', 'A'],
46 },
47 'tlsa' => {
48 type => 'TLSA',
49 0 ▶ name => sub { '_25._tcp.'.shift },
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 from => 'primary',
51 },
52 'sts' => {
53 type => 'TXT',
54 0 ▶ name => sub { '_mta-sts.'.shift },
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 },
56 'tlsrpt' => {
57 type => 'TXT',
58 0 ▶ name => sub { '_smtp._tcp.'.shift },
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 },
60 1 };
61  
62 1 foreach my $record (keys %$RECORDS) {
63 5 my $is_secure = "is_${record}_secure";
64 5 my $accessor = "_${record}";
65 5 my $type = $RECORDS->{$record}->{'type'};
66 0 ▶ my $name = $RECORDS->{$record}->{'name'} || sub { shift };
Subroutine __ANON__: not called CC=1 SCAR=0.6
Condition66%2/3
$RECORDS->{$record}{'name'} || sub {
l!l&&r!l&&!r
320
MC/DC0%0/2
$RECORDS->{$record}{'name'} || sub { shift(); }
$RECORDS->{$record}{'name'} sub { shift(); }
Truth table66%2/3
A || B
A= $RECORDS->{$record}{'name'}B= sub { shift(); }
ABresult
1X1
011
000
67 5 ▶ my $from = $RECORDS->{$record}->{'from'} || 'domain';
Condition100%2/2
$RECORDS->{$record}{'from'} || 'domain'
l!l
14
MC/DC100%1/1
$RECORDS->{$record}{'from'} || 'domain'
$RECORDS->{$record}{'from'}
Truth table100%2/2
A
A= $RECORDS->{$record}{'from'}
Aresult
11
00
68  
69 has $is_secure => (
70 is => 'ro',
71 isa => 'Bool',
72 lazy => 1,
73 default => sub {
74 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
75 0 ▶ return 0 unless defined $self->$accessor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->$accessor 0 0 0
76 0 ▶ return $self->$accessor->header->ad ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->$accessor->header->ad ? : 0 0 0
77 },
78 5 );
79  
80 has $accessor => (
81 is => 'ro',
82 isa => 'Maybe[Net::DNS::Packet]',
83 lazy => 1,
84 default => sub {
85 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 0 my $domainname = $name->($self->$from);
87 0 return $self->resolver->query($domainname, $type);
88 },
89 5 clearer => "_reset_${accessor}",
90 );
91 }
92  
93  
94 has 'mx' => (
95 is => 'ro',
96 isa => 'ArrayRef[Str]',
97 lazy => 1,
98 default => sub {
99 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
100 0 ▶ return [] unless defined $self->_mx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_mx 0 0 0
101 0 my @mx;
102 0 ▶ if( $self->_mx->answer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mx->answer) 0 0 0
103 0 my @rr = grep { $_->type eq 'MX' } $self->_mx->answer;
104 0 @rr = sort { $a->preference <=> $b->preference } @rr;
105 0 @mx = map { $_->exchange } @rr;
106 }
107 0 return \@mx;
108 },
109 1 traits => ['Array'],
110 handles => {
111 'mx_count' => 'count',
112 },
113 );
114  
115  
116 has 'a' => (
117 is => 'ro',
118 isa => 'Maybe[Str]',
119 lazy => 1,
120 default => sub {
121 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
122 0 ▶ if( my @rr = $self->_a->answer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rr) = $self->_a->answer) 0 0 0
123 0 return $self->domain;
124 }
125 0 return;
126 },
127 1 );
128  
129  
130 has 'record_type' => (
131 is => 'ro',
132 isa => 'Str',
133 lazy => 1,
134 default => sub {
135 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
136 0 ▶ return 'mx' if $self->mx_count;
Branch: 1 branch
BranchTrueFalseTotal
if $self->mx_count 0 0 0
137 0 ▶ return 'a' if defined $self->a;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->a 0 0 0
138 0 return 'non-existent';
139 },
140 1 );
141  
142  
143 has 'primary' => (
144 is => 'ro',
145 isa => 'Maybe[Str]',
146 lazy => 1,
147 default => sub {
148 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
149 0 ▶ return $self->mx->[0] if $self->record_type eq 'mx';
Branch: 1 branch
BranchTrueFalseTotal
if $self->record_type eq "mx" 0 0 0
150 0 ▶ return $self->a if $self->record_type eq 'a';
Branch: 1 branch
BranchTrueFalseTotal
if $self->record_type eq "a" 0 0 0
151 0 return;
152 },
153 1 );
154  
155  
156 has 'is_primary_secure' => (
157 is => 'ro',
158 isa => 'Bool',
159 lazy => 1,
160 default => sub {
161 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
162 0 ▶ return $self->is_mx_secure if $self->record_type eq 'mx';
Branch: 1 branch
BranchTrueFalseTotal
if $self->record_type eq "mx" 0 0 0
163 0 ▶ return $self->is_a_secure if $self->record_type eq 'a';
Branch: 1 branch
BranchTrueFalseTotal
if $self->record_type eq "a" 0 0 0
164 0 return 0;
165 },
166 1 );
167  
168  
169  
170 has 'tlsa' => (
171 is => 'ro',
172 isa => 'Maybe[Net::DNS::RR]',
173 lazy => 1,
174 default => sub {
175 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
176 0 ▶ return unless defined $self->_tlsa;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_tlsa 0 0 0
177 0 ▶ if( my @rr = $self->_tlsa->answer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rr) = $self->_tlsa->answer) 0 0 0
178 0 return $rr[0];
179 }
180 0 return;
181 },
182 1 );
183  
184  
185 has 'tlsrpt' => (
186 is => 'ro',
187 isa => 'Maybe[Mail::STS::TLSRPTRecord]',
188 lazy => 1,
189 default => sub {
190 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
191 0 ▶ return unless defined $self->_tlsrpt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_tlsrpt 0 0 0
192 0 ▶ if( my @rr = $self->_tlsrpt->answer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rr) = $self->_tlsrpt->answer) 0 0 0
193 0 return Mail::STS::TLSRPTRecord->new_from_string($rr[0]->txtdata);
194 }
195 0 return;
196 },
197 1 );
198  
199  
200 has 'sts' => (
201 is => 'ro',
202 isa => 'Maybe[Mail::STS::STSRecord]',
203 lazy => 1,
204 default => sub {
205 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
206 0 ▶ return unless defined $self->_sts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_sts 0 0 0
207 0 ▶ if( my @rr = $self->_sts->answer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rr) = $self->_sts->answer) 0 0 0
208 0 return Mail::STS::STSRecord->new_from_string($rr[0]->txtdata);
209 }
210 0 return;
211 },
212 1 clearer => '_reset_sts',
213 );
214  
215  
216 1 has 'policy_id' => ( is => 'rw', isa => 'Maybe[Str]');
217 1 has 'policy_expires_at' => ( is => 'rw', isa => 'Maybe[Time::Piece]');
218  
219 sub set_policy_expire {
220 0 ▶ my ($self, $max_age) = @_;
Subroutine set_policy_expire: not called CC=1 SCAR=6.9
Pod undocumented
221 0 return Time::Piece->new + Time::Seconds->new($max_age)
222 }
223  
224 sub is_policy_expired {
225 0 ▶ my $self = shift;
Subroutine is_policy_expired: not called CC=2 SCAR=17.9
Pod undocumented
226 0 ▶ return 1 if Time::Piece->new > $self->policy_expires_at;
Branch: 1 branch
BranchTrueFalseTotal
if "Time::Piece"->new > $self->policy_expires_at 0 0 0
227 0 return 0;
228 }
229  
230 has 'policy' => (
231 is => 'ro',
232 isa => 'Mail::STS::Policy',
233 lazy => 1,
234 default => sub {
235 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
236 0 ▶ die('could not retrieve _mta_sts record') unless defined $self->sts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->sts 0 0 0
237 0 $self->policy_id( $self->sts->id );
238 0 my $policy = $self->retrieve_policy();
239 0 $self->set_policy_expire($policy->max_age);
240 0 return $policy;
241 },
242 1 clearer => '_reset_policy',
243 );
244  
245 sub retrieve_policy {
246 0 ▶ my $self = shift;
Subroutine retrieve_policy: not called CC=4 SCAR=30.0
Pod undocumented
247 0 my $url = 'https://mta-sts.'.$self->domain.'/.well-known/mta-sts.txt';
248 0 my $response = $self->agent->get($url);
249 0 my $content = $response->decoded_content;
250 0 ▶ if(defined $self->max_policy_size && length($content) > $self->max_policy_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->max_policy_size and length $content > $self->max_policy_size) 0 0 0
Condition0%0/3
defined $self->max_policy_size and length $content > $self->max_policy_size
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->max_policy_size and length $content > $self->max_policy_size
defined $self->max_policy_size length $content > $self->max_policy_size
Truth table0%0/3
A and B
A= defined $self->max_policy_sizeB= length $content > $self->max_policy_size
ABresult
0X0
100
111
251 0 die('policy exceeding maximum policy size limit');
252 }
253 0 ▶ die('could not retrieve policy: '.$response->status_line) unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
254 0 return Mail::STS::Policy->new_from_string($content);
255 }
256  
257  
258 sub check_policy_update {
259 0 ▶ my $self = shift;
Subroutine check_policy_update: not called CC=4 SCAR=30.0
Pod documented
260 0 ▶ return 0 unless $self->is_policy_expired;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_policy_expired 0 0 0
261  
262 0 $self->_reset__sts;
263 0 $self->_reset_sts;
264 0 ▶ die('could not retrieve _mta_sts record') unless $self->sts;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sts 0 0 0
265 0 my $new_id = $self->sts->id;
266 0 ▶ if($self->policy_id eq $new_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->policy_id eq $new_id) 0 0 0
267 0 $self->set_policy_expire($self->policy->max_age);
268 0 return 0;
269 }
270  
271 0 $self->_reset_policy;
272 0 return 1;
273 }
274  
275  
276  
277 1 1;
278  
279 __END__
280  
281 =pod
282  
283 =encoding UTF-8
284  
285 =head1 NAME
286  
287 Mail::STS::Domain - class for MTA-STS domain lookups
288  
289 =head1 VERSION
290  
291 version 0.03
292  
293 =head1 SYNOPSIS
294  
295 my $domain = $sts->domain('example.com');
296 # or construct it yourself
297 my $domain = Mail::STS::Domain(
298 resolver => $resolver, # Net::DNS::Resolver
299 agent => $agent, # LWP::UserAgent
300 domain => 'example.com',
301 );
302  
303 $domain->mx;
304 # [ 'mta1.example.com', ... ]
305 $domain->tlsa;
306 # undef or Net::DNS::RR:TLSA
307 $domain->primary
308 # mta1.example.com
309 $domain->tlsrpt;
310 # undef or Mail::STS::TLSRPTRecord
311 $domain->sts;
312 # undef or Mail::STS::STSRecord
313 $domain->policy;
314 # Mail::STS::Policy or will die()
315  
316 =head1 ATTRIBUTES
317  
318 =head2 domain (required)
319  
320 The domain to lookup.
321  
322 =head2 resolver (required)
323  
324 A Net::DNS::Resolver object to use for DNS lookups.
325  
326 =head2 agent (required)
327  
328 A LWP::UserAgent object to use for retrieving policy
329 documents by https.
330  
331 =head2 max_policy_size(default: 65536)
332  
333 Maximum size allowed for STS policy document.
334  
335 =head1 METHODS
336  
337 =head2 mx()
338  
339 Retrieves MX hostnames from DNS and returns a array reference.
340  
341 List is sorted by priority.
342  
343 $domain->mx;
344 # [ 'mta1.example.com', 'backup-mta1.example.com' ]
345  
346 =head2 a()
347  
348 Returns the domainname if a AAAA or A record exists for the domain.
349  
350 $domain->a;
351 # "example.com"
352  
353 =head2 record_type()
354  
355 Returns the type of record the domain resolves to:
356  
357 =over
358  
359 =item "mx"
360  
361 If domain has MX records.
362  
363 =item "a"
364  
365 If domain has an AAAA or A record.
366  
367 =item "non-existent"
368  
369 If the domain does not exist.
370  
371 =back
372  
373 =head2 primary()
374  
375 Returns the hostname of the primary MTA for this domain.
376  
377 In case of MX records the first element of mx().
378  
379 In case of an AAAA or A record the domainname.
380  
381 Or undef if the domain does not resolve at all.
382  
383 =head2 is_primary_secure()
384  
385 Returns 1 if resolver signaled successfull DNSSEC validation
386 for the hostname returned by primary().
387  
388 Otherwise returns 0.
389  
390 =head2 tlsa()
391  
392 Returns a Net::DNS::RR in case an TLSA record exists
393 for the hostname returned by primary() otherwise undef.
394  
395 =head2 tlsrpt()
396  
397 Returns an Mail::STS::TLSRPTRecord if a TLSRPT TXT
398 record for the domain could be lookup.
399  
400 =head2 sts()
401  
402 Returns an Mail::STS::STSRecord if a STS TXT
403 record for the domain could be lookup.
404  
405 =head2 policy()
406  
407 Returns a Mail::STS::Policy object if a policy for the domain
408 could be retrieved by the well known URL.
409  
410 Otherwise will die with an error.
411  
412 =head2 check_policy_update()
413  
414 Checks if a new version of the policy is available.
415  
416 First checks if the policy max_age has expired.
417 Then checks if the _mta_sts record lists a new policy version.
418  
419 If there is a new policy the current policy will be resettet
420 so the next call to ->policy() will return the new policy.
421  
422 Returns 1 if new policy was found otherwise 0.
423  
424 =head2 is_mx_secure()
425 =head2 is_a_secure()
426 =head2 is_tlsa_secure()
427 =head2 is_sts_secure()
428 =head2 is_tlsrpt_secure()
429  
430 Returns 1 if resolver signaled successfull DNSSEC validation
431 (ad flag) for returned record otherwise returns 0.
432  
433 =head1 AUTHOR
434  
435 Markus Benning <ich@markusbenning.de>
436  
437 =head1 COPYRIGHT AND LICENSE
438  
439 This software is copyright (c) 2018 by Markus Benning <ich@markusbenning.de>.
440  
441 This is free software; you can redistribute it and/or modify it under
442 the same terms as the Perl 5 programming language system itself.
443  
444 =cut
« STS/Policy.pm ↑ Summary STS.pm »