blib/lib/Mail/SPF/Iterator.pm

filter: Statementstmt 82.2% 682 / 829 Branchbran 63.2% 444 / 702 Conditioncond 55.9% 66 / 118 MC/DCmcdc 32.8% 23 / 70 Subroutinesub 90.2% 46 / 51 Podpod 75.0% 6 / 8 totaltotal 71.2% 1267 / 1778 SCAR 83.1
CC
414
Cov
72%
lookup_blocking
182.0
next
136.5
_parse_spf
76.5
CRAP 4062.3
↑ Summary
1 =head1 NAME
2  
3 Mail::SPF::Iterator - iterative SPF lookup
4  
5 =head1 SYNOPSIS
6  
7 use Net::DNS;
8 use Mail::SPF::Iterator;
9 use Mail::SPF::Iterator Debug =>1; # enable debugging
10 my $spf = Mail::SPF::Iterator->new(
11 $ip, # IP4|IP6 of client
12 $mailfrom, # from MAIL FROM:
13 $helo, # from HELO|EHLO
14 $myname, # optional: my hostname
15 {
16 default_spf => 'mx/24 ?all', # in case no record was found in DNS
17 pass_all => SPF_SoftFail, # treat records like '+all' as error
18 # rfc4408 => 1, # for compatibility only
19 }
20 );
21  
22 # could be other resolvers too
23 my $resolver = Net::DNS::Resolver->new;
24  
25 ### with nonblocking, but still in loop
26 ### (callbacks are preferred with non-blocking)
27 my ($result,@ans) = $spf->next; # initial query
28 while ( ! $result ) {
29 my @query = @ans;
30 die "no queries" if ! @query;
31 for my $q (@query) {
32 # resolve query
33 my $socket = $resolver->bgsend( $q );
34 ... wait...
35 my $answer = $resolver->bgread($socket);
36 ($result,@ans) = $spf->next(
37 $answer # valid answer
38 || [ $q, $resolver->errorstring ] # or DNS problem
39 );
40 last if $result; # got final result
41 last if @ans; # got more DNS queries
42 }
43 }
44  
45 ### OR with blocking:
46 ### ($result,@ans) = $spf->lookup_blocking( undef,$resolver );
47  
48 ### print mailheader
49 print "Received-SPF: ".$spf->mailheader;
50  
51 # $result = Fail|Pass|...
52 # $ans[0] = comment for Received-SPF
53 # $ans[1] = %hash with infos for Received-SPF
54 # $ans[2] = explanation in case of Fail
55  
56  
57  
58 =head1 DESCRIPTION
59  
60 This module provides an iterative resolving of SPF records. Contrary to
61 Mail::SPF, which does blocking DNS lookups, this module just returns the DNS
62 queries and later expects the responses.
63  
64 Lookup of the DNS records will be done outside of the module and can be done
65 in a event driven way. It is also possible to do many parallel SPF checks
66 in parallel without needing multiple threads or processes.
67  
68 This module can also make use of SenderID records for checking the C<mfrom>
69 part, but it will prefer SPF. It will only use DNS TXT records for looking up
70 SPF policies unless compatibility with RFC 4408 is explicitly enabled.
71  
72 See RFC 7208 (old RFC 4408) for SPF and RFC 4406 for SenderID.
73  
74 =head1 METHODS
75  
76 =over 4
77  
78 =item new( IP, MAILFROM, HELO, [ MYNAME ], [ \%OPT ] )
79  
80 Construct a new Mail::SPF::Iterator object, which maintains the state
81 between the steps of the iteration. For each new SPF check a new object has
82 to be created.
83  
84 IP is the IP if the client as string (IP4 or IP6).
85  
86 MAILFROM is the user@domain part from the MAIL FROM handshake, e.g. '<','>'
87 and any parameters removed. If only '<>' was given (like in bounces) the
88 value is empty.
89  
90 HELO is the string send within the HELO|EHLO dialog which should be a domain
91 according to the RFC but often is not.
92  
93 MYNAME is the name of the local host. It's only used if required by macros
94 inside the SPF record.
95  
96 OPT is used for additional arguments. Currently B<default_spf> can be used
97 to set a default SPF record in case no SPF/TXT records are
98 returned from DNS (useful values are for example 'mx ?all' or 'mx/24 ?all').
99 B<rfc4408> can be set to true in case stricter compatibility is needed with RFC
100 4408 instead of RFC 7208, i.e. lookup of DNS SPF records, no limit on void DNS
101 lookups etc.
102 B<pass_all> can be set to the expected outcome in case a SPF policy gets found,
103 which would pass everything. Such policies are common used domains used by
104 spammers.
105  
106 Returns the new object.
107  
108 =item next([ ANSWER ])
109  
110 C<next> will be initially called with no arguments to get initial DNS queries
111 and then will be called with the DNS answers.
112  
113 ANSWER is either a DNS packet with the response to a former query or C<< [
114 QUERY, REASON ] >> on failures, where QUERY is the DNS packet containing the
115 failed query and REASON the reason, why the query failed (like TIMEOUT).
116  
117 If a final result was achieved it will return
118 C<< ( RESULT, COMMENT, HASH, EXPLAIN ) >>. RESULT is the result, e.g. "Fail",
119 "Pass",.... COMMENT is the comment for the Received-SPF header. HASH contains
120 information about problem, mechanism for the Received-SPF header.
121 EXPLAIN will be set to the explain string if RESULT is Fail.
122  
123 If no final result was achieved yet it will either return
124 C<< (undef,@QUERIES) >> with a list of new queries to continue, C<< ('') >>
125 in case the ANSWER produced an error but got ignored, because there are
126 other queries open, or C<< () >> in case the ANSWER was ignored because it
127 did not match any open queries.
128  
129 =item mailheader
130  
131 Creates value for Received-SPF header based on the final answer from next().
132 Returns header as string (one line, no folding) or undef, if no final result
133 was found.
134 This creates only the value, not the 'Received-SPF' prefix.
135  
136 =item result
137  
138 Returns ( RESULT, COMMENT, HASH, EXPLAIN ) like the final C<next> does or () if
139 the final result wasn't found yet.
140  
141 If the SPF record had an explain modifier, which needed DNS lookups to resolve
142 this method might return the result (although with incomplete explain) before
143 C<next> does it.
144  
145 =item explain_default ( [ EXPLAIN ] )
146  
147 Sets default explanation string if EXPLAIN is given.
148 If it's called as a class method the default explanation string for the class
149 will be set, otherwise the default explanation string for the object.
150  
151 Returns the current default explanation string for the object or if non
152 given or if called as a class method the default explanation string for the
153 class.
154  
155 =item lookup_blocking ( [ TIMEOUT, RESOLVER ] )
156  
157 Quick way to get the SPF status.
158 This will simply call C<next> until it gets a final result.
159  
160 TIMEOUT limits the lookup time and defaults to 20.
161 RESOLVER is a Net::DNS::Resolver object (or similar) and defaults to
162 C<< Net::DNS::Resolver->new >>.
163 Returns ( RESULT, COMMENT, HASH ) like the final C<next> does.
164  
165 This is not the preferred way to use this module, because it's blocking, so
166 no lookups can be done in parallel in a single process/thread.
167  
168 =back
169  
170 =head1 EXPORTED SYMBOLS
171  
172 For convenience the constants SPF_TempError, SPF_PermError, SPF_Pass, SPF_Fail,
173 SPF_SoftFail, SPF_Neutral, SPF_None are by default exported, which have the values
174 C<"TempError">, C<"PermError"> ...
175  
176 =head2 Arguments to C<use>/C<import>
177  
178 The C<SPF_*> symbols are available for import and are exported if no arguments
179 are given to C<use> or C<import>. Same effect with adding C<:DEFAULT> as an
180 argument. Additionally the following arguments are supported:
181  
182 =over 4
183  
184 =item DebugFunc => \&coderef
185  
186 Sets a custom debug function, which just takes on argument. If given it will be
187 called on all debug messages when debugging is active. This function takes as
188 the only argument the debug message.
189  
190 =item Debug => 1|0
191  
192 Switches debugging on/off.
193  
194 =back
195  
196 =head1 AUTHOR
197  
198 Steffen Ullrich <sullr@cpan.org>
199  
200 =head1 COPYRIGHT
201  
202 Copyright by Steffen Ullrich.
203  
204 This module is free software; you can redistribute it and/or
205 modify it under the same terms as Perl itself.
206  
207 =cut
208  
209  
210 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
211 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
212  
213 package Mail::SPF::Iterator;
214  
215 5 our $VERSION = '1.119';
216  
217 use fields (
218 # values given in or derived from params to new()
219 5 'helo', # helo given in new()
220 'myname', # myname given in new()
221 'clientip4', # packed ip from new() if IP4
222 'clientip6', # packed ip from new() if IP6
223 'sender', # mailfrom|helo given in new()
224 'domain', # extracted from mailfrom|helo
225 'identity', # 'mailfrom' if sender is mailfrom, else 'helo'
226 'opt', # additional options like default_spf
227 # internal states and values
228 'mech', # list of unhandled mechanism for current SPF
229 'include_stack', # stack for handling includes
230 'redirect', # set to domain of redirect modifier of current SPF
231 'explain', # set to explain modifier of current SPF
232 'cb', # [$sub,@arg] for callback to DNS replies
233 'cbq', # list of queries from last mech incl state
234 'validated', # cache used in validation of hostnames for ptr and %{p}
235 'limit_dns_mech', # countdown for number of mechanism using DNS queries
236 'limit_dns_void', # countdown for number of void DNS queries
237 'explain_default', # default explanation of object specific
238 'result', # contains final result
239 'tmpresult', # contains the best result we have so far
240 'used_default_spf', # set to the default_spf from opt if used
241 5 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
242  
243 5 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
244 5 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
245 5 ▶use URI::Escape 'uri_escape';
Subroutine BEGIN: called CC=1 SCAR=0.0
246 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
247 5 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
248  
249  
250 ### check if IPv6 support is in Socket, otherwise try Socket6
251 5 my $can_ip6;
252 BEGIN {
253 $can_ip6 = eval {
254 5 require Socket;
255 5 Socket->import(qw(inet_pton inet_ntop));
256 5 ▶ Socket->import('AF_INET6') if ! defined &AF_INET6;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Mail::SPF::Iterator::AF_INET6 0 5 5
257 5 1;
258 5 ▶ } || eval {
Subroutine BEGIN: called CC=5 SCAR=25.2
Condition33%1/3
eval { do { require Socket; 'Socket'->import('inet_pton', 'inet_ntop'); 'Socket'->import('AF_INET6') unless defined &Mail::SPF::Iterator::AF_INET6; 1 } } || eval { do { require Socket6; 'Socket6'->import('inet_pton', 'inet_ntop'); 'Socket6'->import('AF_INET6') unless defined &Mail::SPF::Iterator::AF_INET6; 1 } }
l!l&&r!l&&!r
500
MC/DC0%0/2
eval { do { require Socket; 'Socket'->import('inet_pton', 'inet_ntop'); 'Socket'->import('AF_INET6') unless defined &Mail::SPF::Iterator::AF_INET6; 1 } } || eval { do { require Socket6; 'Socket6'->import('inet_pton', 'inet_ntop'); 'Socket6'->import('AF_INET6') unless defined &Mail::SPF::Iterator::AF_INET6; 1 } }
eval { do { require Socket; 'Socket'->import('inet_pton', 'inet_ntop'); 'Socket'->import('AF_INET6') unless defined &Mail::SPF::Iterator::AF_INET6; 1 } } eval { do { require Socket6; 'Socket6'->import('inet_pton', 'inet_ntop'); 'Socket6'->import('AF_INET6') unless defined &Mail::SPF::Iterator::AF_INET6; 1 } }
Truth table33%1/3
A || B
A= eval { do { require Socket; 'Socket'->import('inet_pton', 'inet_ntop'); 'Socket'->import('AF_INET6') unless defined &Mail::SPF::Iterator::AF_INET6; 1 } }B= eval { do { require Socket6; 'Socket6'->import('inet_pton', 'inet_ntop'); 'Socket6'->import('AF_INET6') unless defined &Mail::SPF::Iterator::AF_INET6; 1 } }
ABresult
1X1
011
000
259 0 require Socket6;
260 0 Socket6->import(qw( inet_pton inet_ntop));
261 0 ▶ Socket6->import('AF_INET6') if ! defined &AF_INET6;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Mail::SPF::Iterator::AF_INET6 0 0 0
262 0 1;
263 };
264 5 ▶ if ( ! $can_ip6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $can_ip6) 0 5 5
265 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
266 0 *{'AF_INET6'} = *{'inet_pton'} = *{'inet_ntop'}
267 0 ▶ = sub { die "no IPv6 support" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
268 }
269 }
270  
271 ### create SPF_* constants and export them
272 5 our @EXPORT;
273 5 our @EXPORT_OK = '$DEBUG';
274 5 ▶use constant SPF_Noop => '_NOOP';
Subroutine BEGIN: called CC=1 SCAR=0.0
275 5 my %ResultQ;
276 BEGIN {
277 5 ▶ my $i = 0;
Subroutine BEGIN: called CC=2 SCAR=6.9
278 5 $ResultQ{ &SPF_Noop } = $i++;
279 5 for (qw(None PermError TempError Neutral SoftFail Fail Pass)) {
280 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
281 35 *{"SPF_$_"} = eval "sub () { '$_' }";
282 35 push @EXPORT, "SPF_$_";
283 35 $ResultQ{$_} = $i++;
284 }
285 }
286  
287 my $DEBUGFUNC;
288 5 our $DEBUG=0;
289 sub import {
290 12 ▶ goto &Exporter::import if @_ == 1; # implicit :DEFAULT
Subroutine import: called CC=6 SCAR=19.7
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 6 6 12
291 6 my $i = 1;
292 6 ▶ while ( $i<@_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 6 6 12
293 6 ▶ if ( $_[$i] eq 'DebugFunc' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[$i] eq 'DebugFunc') { } 0 6 6
elsif ($_[$i] eq 'Debug') { } 6 0 6
294 0 $DEBUGFUNC = $_[$i+1];
295 0 splice( @_,$i,2 );
296 0 next;
297 } elsif ( $_[$i] eq 'Debug' ) {
298 6 $DEBUG = $_[$i+1];
299 6 splice( @_,$i,2 );
300 6 next;
301 }
302 0 ++$i;
303 }
304 6 ▶ goto &Exporter::import if @_ >1; # not implicit :DEFAULT
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 6 6
305 }
306  
307  
308  
309 ### Debugging
310 sub DEBUG {
311 6868 ▶ $DEBUG or return; # check against debug level
Subroutine DEBUG: called CC=4 SCAR=14.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG or return 0 6868 6868
312 6868 ▶ goto &$DEBUGFUNC if $DEBUGFUNC;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUGFUNC 0 6868 6868
313 6868 my ($pkg,$file,$line) = caller;
314 6868 my $msg = shift;
315 6868 ▶ $msg = sprintf $msg,@_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 6868 6868
316 6868 print STDERR "DEBUG: $pkg#$line: $msg\n";
317 }
318  
319 ### pre-compute masks for IP4, IP6
320 5 my (@mask4,@mask6);
321 {
322 5 my $m = '0' x 32;
323 5 $mask4[0] = pack( "B32",$m);
324 5 for (1..32) {
325 160 substr( $m,$_-1,1) = '1';
326 160 $mask4[$_] = pack( "B32",$m);
327 }
328  
329 5 $m = '0' x 128;
330 5 $mask6[0] = pack( "B32",$m);
331 5 for (1..128) {
332 640 substr( $m,$_-1,1) = '1';
333 640 $mask6[$_] = pack( "B128",$m);
334 }
335 }
336  
337 ### mapping char to result
338 5 my %qual2rv = (
339 '+' => SPF_Pass,
340 '-' => SPF_Fail,
341 '~' => SPF_SoftFail,
342 '?' => SPF_Neutral,
343 );
344  
345 ############################################################################
346 # NEW
347 # creates new SPF processing object
348 # Args: ($class,$ip,$mailfrom,$helo,?$myname,?\%opt)
349 # $ip: IP4/IP6 as string
350 # $mailfrom: user@domain of "mail from"
351 # $helo: info from helo|ehlo - should be domain name
352 # $myname: local name, used only for expanding macros (optional)
353 # %opt: optional additional arguments
354 # default_spf => ... : default SPF record if none from DNS
355 # Returns: $self
356 ############################################################################
357 sub new {
358 994 ▶ my ($class,$ip,$mailfrom,$helo,$myname,$opt) = @_;
Subroutine new: called CC=12 SCAR=31.0
Pod documented
359 994 my Mail::SPF::Iterator $self = fields::new($class);
360  
361 994 ▶ my $domain =
Branch: 4 branches
BranchTrueFalseTotal
$mailfrom =~ /\@([\w\-.]+)$/ ? : 970 24 994
$mailfrom =~ /\@\[([\da-f:\.]+)\]$/i ? : 6 18 24
$helo =~ /\@([\w\-.]+)$/ ? : 0 18 18
$helo =~ /\@\[([\da-f:\.]+)\]$/i ? : 0 18 18
362 $mailfrom =~m{\@([\w\-.]+)$} ? $1 :
363 $mailfrom =~m{\@\[([\da-f:\.]+)\]$}i ? $1 :
364 $helo =~m{\@([\w\-.]+)$} ? $1 :
365 $helo =~m{\@\[([\da-f:\.]+)\]$}i ? $1 :
366 $helo;
367 994 ▶ my ($sender,$identity) = $mailfrom ne ''
Branch: 1 branch
BranchTrueFalseTotal
$mailfrom ne '' ? : 976 18 994
368 ? ( $mailfrom,'mailfrom' )
369 : ( $helo,'helo' );
370  
371 994 my $ip4 = eval { inet_aton($ip) };
372 994 ▶ my $ip6 = ! $ip4 && $can_ip6 && eval { inet_pton(AF_INET6,$ip) };
Condition33%2/6
!$ip4 && $can_ip6 && eval { do { inet_pton(10, $ip) } }
!ll&&!rl&&r
99400
not $ip4 and $can_ip6
!ll&&!rl&&r
99400
MC/DC0%0/3
!$ip4 && $can_ip6 && eval { do { inet_pton(10, $ip) } }
not $ip4 $can_ip6 eval { do { inet_pton(10, $ip) } }
Truth table25%1/4
not(A) and B && C
A= not $ip4B= $can_ip6C= eval { do { inet_pton(10, $ip) } }
ABCresult
0XX0
10X0
1100
1111
373 994 ▶ die "no client IP4 or IP6 known (can_ip6=$can_ip6): $ip"
Branch: 1 branch
BranchTrueFalseTotal
unless $ip4 or $ip6 0 994 994
Condition33%1/3
$ip4 or $ip6
l!l&&r!l&&!r
99400
MC/DC0%0/2
$ip4 or $ip6
$ip4 $ip6
Truth table33%1/3
A or B
A= $ip4B= $ip6
ABresult
1X1
011
000
374 if ! $ip4 and ! $ip6;
375  
376 994 ▶ if ( $ip6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip6) 0 994 994
377 0 my $m = inet_pton( AF_INET6,'::ffff:0.0.0.0' );
378 0 ▶ if ( ($ip6 & $m) eq $m ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($ip6 & $m) eq $m) 0 0 0
379 # mapped IPv4
380 0 $ip4 = substr( $ip6,-4 );
381 0 $ip6 = undef;
382 }
383 }
384  
385 994 %$self = (
386 clientip4 => $ip4, # IP of client
387 clientip6 => $ip6, # IP of client
388 domain => $domain, # current domain
389 sender => $sender, # sender (mailfrom|helo)
390 helo => $helo, # helo
391 identity => $identity, # 'helo'|'mailfrom'
392 myname => $myname, # name of mail host itself
393 include_stack => [], # stack in case of include
394 cb => undef, # callback for next DNS reply
395 cbq => [], # the DNS queries for cb
396 validated => {}, # validated IP/domain names for PTR and %{p}
397 limit_dns_mech => 10, # Limit on Number of DNS mechanism
398 limit_dns_void => 2, # Limit on Number of void DNS answers
399 mech => undef, # list of spf mechanism
400 redirect => undef, # redirect from SPF record
401 explain => undef, # explain from SPF record
402 result => undef, # final result [ SPF_*, info, \%hash ]
403 opt => $opt,
404 );
405 994 return $self;
406 }
407  
408 ############################################################################
409 # return result
410 # Args: $self
411 # Returns: ($status,$info,$hash,$explain)
412 # $status: SPF_Pass|SPF_Fail|...
413 # $info: comment for Received-SPF header
414 # $hash: param for Received-SPF header
415 # $explain: explanation string on SPF_Fail
416 ############################################################################
417 sub result {
418 0 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine result: not called CC=2 SCAR=17.9
Pod documented
419 0 ▶ my $r = $self->{result} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $r = $self->{"result"} or return 0 0 0
420 0 return @$r;
421 }
422  
423 ############################################################################
424 # get/set default explanation string
425 # Args: ($self,[$explain])
426 # $explain: default explanation string (will be set)
427 # Returns: $explain
428 # $explain: default explanation string
429 ############################################################################
430 {
431 5 my $default = 'SPF Check Failed';
432 sub explain_default {
433 266 ▶ if ( ref $_[0] ) {
Subroutine explain_default: called CC=5 SCAR=21.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 266 0 266
434 266 my Mail::SPF::Iterator $self = shift;
435 266 ▶ $self->{explain_default} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 266 266
436 return defined $self->{explain_default}
437 ? $self->{explain_default}
438 266 ▶ : $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'explain_default'} ? : 0 266 266
439 } else {
440 0 shift; # class
441 0 ▶ $default = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
442 0 return $default;
443 }
444 }
445 }
446  
447 ############################################################################
448 # lookup blocking
449 # not the intended way to use the module, but sometimes one needs to quickly
450 # lookup something, even if it's blocking
451 # Args: ($self,[$timeout,$resolver])
452 # $timeout: total timeout for lookups, default 20
453 # $resolver: Resolver object compatible to Net::DNS::Resolver, if not
454 # given a new Net::DNS::Resolver object will be created
455 # Returns: ($status,$info,$hash,$explain)
456 # see result()
457 ############################################################################
458 5 sub lookup_blocking {
459 0 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine lookup_blocking: not called CC=13 SCAR=52.0
Pod documented
460 0 my ($timeout,$resolver) = @_;
461  
462 0 ▶ my $expire = time() + ( $timeout || 20 ); # 20s: RFC4408, 10.1
Condition0%0/2
$timeout || 20
l!l
00
MC/DC0%0/1
$timeout || 20
$timeout
Truth table0%0/2
A
A= $timeout
Aresult
11
00
463 0 ▶ $resolver ||= Net::DNS::Resolver->new;
Condition0%0/3
$resolver ||= "Net::DNS::Resolver"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$resolver ||= "Net::DNS::Resolver"->new
$resolver "Net::DNS::Resolver"->new
Truth table0%0/3
A ||= B
A= $resolverB= "Net::DNS::Resolver"->new
ABresult
1X1
011
000
464  
465 0 my ($status,@ans) = $self->next; # get initial queries
466 0 ▶ while ( ! $status ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 0 0 0
467  
468 # expired ?
469 0 $timeout = $expire - time();
470 0 ▶ last if $timeout < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < 0 0 0 0
471  
472 0 my @query = @ans;
473 0 ▶ die "no more queries but no final status" if ! @query;
Branch: 1 branch
BranchTrueFalseTotal
unless @query 0 0 0
474 0 for my $q (@query) {
475 #DEBUG( "next query: ".$q->string );
476 0 my $socket = $resolver->bgsend( $q );
477  
478 0 my $rin = '';
479 0 vec( $rin,fileno($socket),1) = 1;
480 0 ▶ select( $rin,undef,undef,$timeout ) or last;
Branch: 1 branch
BranchTrueFalseTotal
select $rin, undef, undef, $timeout or last 0 0 0
481  
482 0 my $answer = $resolver->bgread( $socket );
483 0 ▶ ($status,@ans) = $self->next(
Condition0%0/2
$answer || [$q, $resolver->errorstring]
l!l
00
MC/DC0%0/1
$answer || [$q, $resolver->errorstring]
$answer
Truth table0%0/2
A
A= $answer
Aresult
11
00
484 $answer || [ $q, $resolver->errorstring ]
485 );
486 0 ▶ last if $status or @ans;
Branch: 1 branch
BranchTrueFalseTotal
if $status or @ans 0 0 0
Condition0%0/3
$status or @ans
l!l&&r!l&&!r
000
MC/DC0%0/2
$status or @ans
$status @ans
Truth table0%0/3
A or B
A= $statusB= @ans
ABresult
1X1
011
000
487 }
488 }
489 0 ▶ my @rv = ! $status
Branch: 1 branch
BranchTrueFalseTotal
!$status ? : 0 0 0
490 ? ( SPF_TempError,'', { problem => 'DNS lookups timed out' } )
491 : ($status,@ans);
492 0 ▶ return wantarray ? @rv : $status;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
493 }
494  
495 ############################################################################
496 # mailheader
497 # create value for Received-SPF header for final response
498 # Args: $self
499 # Returns: $hdrvalue
500 ############################################################################
501 sub mailheader {
502 994 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine mailheader: called CC=5 SCAR=16.2
Pod documented
503 994 ▶ my ($result,$info,$hash) = @{ $self->{result} || return };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"result"} or return 0 994 994
504 $result .= " (using default SPF of \"$self->{used_default_spf}\")"
505 994 ▶ if $self->{used_default_spf};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"used_default_spf"} 0 994 994
506 return $result ." ". join( "; ", map {
507 994 my $v = $hash->{$_};
508 4725 $v =~ s{([\"\\])}{\\$1}g;
509 4725 $v =~ s{[\r\n]+}{ }g;
510 4725 $v =~ s{^\s+}{};
511 4725 $v =~ s{\s+$}{};
512 4725 ▶ $v = qq("$v") if $v eq '' or $v =~ m{[^0-9a-zA-Z!#$%&'*+\-/=?^_`{|}~]};
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "" or $v =~ m[[^0-9a-zA-Z!#$%&'*+\-/=?^_`{|}~]] 3289 1436 4725
Condition66%2/3
$v eq "" or $v =~ m[[^0-9a-zA-Z!#$%&'*+\-/=?^_`{|}~]]
l!l&&r!l&&!r
032891436
MC/DC50%1/2
$v eq "" or $v =~ m[[^0-9a-zA-Z!#$%&'*+\-/=?^_`{|}~]]
$v eq "" $v =~ m[[^0-9a-zA-Z!#$%&'*+\-/=?^_`{|}~]]
Truth table66%2/3
A or B
A= $v eq ""B= $v =~ m[[^0-9a-zA-Z!#$%&'*+\-/=?^_`{|}~]]
ABresult
1X1
011
000
513 4725 "$_=$v"
514 } sort keys %$hash );
515 }
516  
517  
518 ############################################################################
519 # next step in SPF lookup
520 # - verify that there are open queries for the DNS reply and that parameter
521 # in query match question+answer in reply
522 # - process dnsresp by the current callback
523 # - process callbacks result using _next_process_cbrv which returns either
524 # final result or more DNS questions
525 # Args: ($self,$dnsresp)
526 # $dnsresp: DNS reply
527 # Returns: (undef,@dnsq) | ($status,$info,\%param,$explain) | ()
528 # (undef,@dnsq): @dnsq are more DNS questions
529 # ($status,$info,\%param,$explain): final response
530 # (''): reply processed, but answer ignored (likely error)
531 # (): reply ignored, does not matching outstanding request
532 ############################################################################
533 sub next {
534 2861 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine next: called CC=52 SCAR=49.2
Pod documented
535 2861 my $dnsresp = shift;
536  
537 2861 ▶ if ( ! $dnsresp ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dnsresp) 994 1867 2861
538 # no DNS response - must be initial call to next
539 994 ▶ die "no DNS reply but callback given" if $self->{cb};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cb"} 0 994 994
540 994 return $self->_next_process_cbrv( $self->_query_txt_spf );
541 }
542  
543 # handle DNS reply
544 1867 ▶ my $callback = $self->{cb} or die "no callback but DNS reply";
Branch: 1 branch
BranchTrueFalseTotal
my $callback = $self->{"cb"} or die "no callback but DNS reply" 0 1867 1867
545 1867 my $cb_queries = $self->{cbq};
546 1867 ▶ if ( ! @$cb_queries ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$cb_queries) 0 1867 1867
547 # we've got a reply, but no outstanding queries - ignore
548 0 ▶ $DEBUG && DEBUG( "got reply w/o queries, ignoring" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("got reply w/o queries, ignoring") 0 0 0
549 0 return;
550 }
551  
552 # extract query from reply
553 1867 my ($question,$err,$qid);
554 1867 ▶ if ( ! UNIVERSAL::isa( $dnsresp, 'Net::DNS::Packet' )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not UNIVERSAL::isa($dnsresp, 'Net::DNS::Packet')) { } 34 1833 1867
555 # probably [ $question, $errorstring ]
556 34 (my $query,$err) = @$dnsresp;
557 34 ($question) = $query->question;
558 34 $qid = $query->header->id;
559 34 ▶ $err ||= 'unknown error';
Condition50%1/2
$err ||= "unknown error"
l!l
340
MC/DC0%0/1
$err ||= "unknown error"
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
560 34 $dnsresp = $err;
561 34 ▶ $DEBUG && DEBUG( "error '$err' to query ".$question->string );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("error '${err}' to query " . $question->string) 34 0 34
562 } else {
563 1833 ($question) = $dnsresp->question;
564 1833 $qid = $dnsresp->header->id;
565 }
566 1867 my $qtype = $question->qtype;
567  
568 # check if the reply matches one of the open queries
569 1867 my $found;
570 1867 for (@$cb_queries) {
571 1891 ▶ next if $qid != $_->{id}; # ID mismatch
Branch: 1 branch
BranchTrueFalseTotal
if $qid != $_->{"id"} 0 1891 1891
572 1891 ▶ next if $qtype ne $_->{q}->qtype; # type mismatch
Branch: 1 branch
BranchTrueFalseTotal
if $qtype ne $_->{"q"}->qtype 24 1867 1891
573  
574 1867 ▶ if ( lc($question->qname) eq lc($_->{q}->qname) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $question->qname eq lc $_->{"q"}->qname) 1867 0 1867
575 1867 $found = $_;
576 1867 last;
577 }
578  
579 # in case of special characters the names might have the
580 # wire presentation \DDD or the raw presentation
581 # actual behavior depends on the Net::DNS version, so normalize
582 0 my $rname = lc($question->qname);
583 0 my $qname = lc($_->{q}->qname);
584 0 ▶ s{\\(?:(\d\d\d)|(.))}{ $2 || chr($1) }esg for($rname,$qname);
Branch: 1 branch
BranchTrueFalseTotal
$2 or chr $1 0 0 0
585 0 ▶ if ( $rname eq $qname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rname eq $qname) 0 0 0
586 0 $found = $_;
587 0 last;
588 }
589 }
590  
591 1867 ▶ if ( ! $found ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $found) { } 0 1867 1867
elsif (not $found->{'pkt'}) { } 0 1867 1867
592 # packet does not match our queries
593 0 ▶ $DEBUG && DEBUG( "found no open query for ".$question->string );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG('found no open query for ' . $question->string) 0 0 0
594 0 return; # ignore problem
595 } elsif ( ! $found->{pkt} ) {
596 # duplicate response - ignore
597 0 ▶ $DEBUG && DEBUG( "duplicate response, ignoring" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("duplicate response, ignoring") 0 0 0
598 0 return;
599 }
600  
601 1867 delete $found->{pkt}; # no longer needed
602  
603 # found matching query
604 # check for error
605 1867 ▶ if ( $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 34 1833 1867
606 # if this temporary error is the best we have so far set it as tmpresult
607 34 ▶ if (! $self->{tmpresult} or
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"tmpresult"} or $ResultQ{$self->{"tmpresult"}[0]} < $ResultQ{&SPF_TempError}) 30 4 34
Condition66%2/3
not $self->{"tmpresult"} or $ResultQ{$self->{"tmpresult"}[0]} < $ResultQ{&SPF_TempError}
l!l&&r!l&&!r
3004
MC/DC50%1/2
not $self->{"tmpresult"} or $ResultQ{$self->{"tmpresult"}[0]} < $ResultQ{&SPF_TempError}
not $self->{"tmpresult"} $ResultQ{$self->{"tmpresult"}[0]} < $ResultQ{&SPF_TempError}
Truth table66%2/3
not(A) or B
A= not $self->{"tmpresult"}B= $ResultQ{$self->{"tmpresult"}[0]} < $ResultQ{&SPF_TempError}
ABresult
1X1
011
000
608 $ResultQ{ $self->{tmpresult}[0] } < $ResultQ{ &SPF_TempError }) {
609 $self->{tmpresult} = [ SPF_TempError,
610 "getting ".$found->{q}->qtype." for ".$found->{q}->qname,
611 30 { problem => "error getting DNS response: $err" }
612 ]
613 }
614  
615 34 ▶ if ( grep { $_->{pkt} } @$cb_queries ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$_->{'pkt'};} @$cb_queries) { } 6 28 34
elsif (my $r = $self->{'result'}) { } 6 22 28
616 # we still have outstanding queries, so we might still get answers
617 # -> return ('') as a sign, that we got an error to an outstanding
618 # request, but otherwise ignore this error
619 6 ▶ $DEBUG && DEBUG( "ignore error '$err', we still have oustanding queries" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("ignore error '${err}', we still have oustanding queries") 6 0 6
620 6 return ('');
621  
622 } elsif ( my $r = $self->{result} ) {
623 # we have a final result already, so this error occured only while
624 # trying to expand %{p} for explain
625 # -> ignore error, set to default explain and return final result
626 6 ▶ $DEBUG && DEBUG( "error looking up data for explain: $err" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("error looking up data for explain: $err") 6 0 6
627 6 return @$r;
628  
629 } else {
630 # we have no final result - pick the best error we have so far
631 22 ▶ $DEBUG && DEBUG( "TempError: $err" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("TempError: $err") 22 0 22
632 22 $self->{result} = $self->{tmpresult};
633 22 _update_result_info($self);
634 22 return @{$self->{result}};
635 }
636 }
637  
638 # call callback with no records on error
639 1833 my $rcode = $dnsresp->header->rcode;
640 1833 my @answer = $dnsresp->answer;
641 1833 ▶ if (!@answer or $rcode ne 'NOERROR') {
Branch: 1 branch
BranchTrueFalseTotal
if (not @answer or $rcode ne "NOERROR") 248 1585 1833
Condition66%2/3
not @answer or $rcode ne "NOERROR"
l!l&&r!l&&!r
24801585
MC/DC50%1/2
not @answer or $rcode ne "NOERROR"
not @answer $rcode ne "NOERROR"
Truth table66%2/3
not(A) or B
A= not @answerB= $rcode ne "NOERROR"
ABresult
1X1
011
000
642 248 my ($sub,@arg) = @$callback;
643 248 ▶ if ($sub != \&_got_TXT_exp
Branch: 1 branch
BranchTrueFalseTotal
if ($sub != \&Mail::SPF::Iterator::_got_TXT_exp and not $self->{"opt"}{"rfc4408"} and --$self->{"limit_dns_void"} < 0) 8 240 248
Condition100%6/6
$sub != \&Mail::SPF::Iterator::_got_TXT_exp and not $self->{"opt"}{"rfc4408"} and --$self->{"limit_dns_void"} < 0
!ll&&!rl&&r
1321088
$sub != \&Mail::SPF::Iterator::_got_TXT_exp and not $self->{"opt"}{"rfc4408"}
!ll&&!rl&&r
10122116
MC/DC100%3/3
$sub != \&Mail::SPF::Iterator::_got_TXT_exp and not $self->{"opt"}{"rfc4408"} and --$self->{"limit_dns_void"} < 0
$sub != \&Mail::SPF::Iterator::_got_TXT_exp not $self->{"opt"}{"rfc4408"} --$self->{"limit_dns_void"} < 0
Truth table100%4/4
A and not(B) and C
A= $sub != \&Mail::SPF::Iterator::_got_TXT_expB= not $self->{"opt"}{"rfc4408"}C= --$self->{"limit_dns_void"} < 0
ABCresult
0XX0
10X0
1100
1111
644 and ! $self->{opt}{rfc4408}
645 and --$self->{limit_dns_void} < 0) {
646 8 $self->{result} = [ SPF_PermError, "",
647 { problem => "Number of void DNS queries exceeded" }];
648 8 _update_result_info($self);
649 8 return @{$self->{result}};
650 }
651  
652 240 return $self->_next_process_cbrv(
653 $sub->($self,$qtype,$rcode,[],[],@arg));
654 }
655  
656 # extract answer and additional data
657 # verify if names and types in answer records match query
658 # handle CNAMEs
659 1585 my $qname = $question->qname;
660 1585 ▶ $qname =~s{\\(?:(\d\d\d)|(.))}{ $2 || chr($1) }esg; # presentation -> raw
Branch: 1 branch
BranchTrueFalseTotal
$2 or chr $1 12 0 12
661 1585 $qname = lc($qname);
662 1585 my (%cname,%ans);
663 1585 for my $rr (@answer) {
664 1891 my $rtype = $rr->type;
665 # changed between Net::DNS 0.63 and 0.64
666 # it reports now the presentation name instead of the raw name
667 1891 ▶ ( my $name = $rr->name ) =~s{\\(?:(\d\d\d)|(.))}{ $2 || chr($1) }esg;
Branch: 1 branch
BranchTrueFalseTotal
$2 or chr $1 12 0 12
668 1891 $name = lc($name);
669 1891 ▶ if ( $rtype eq 'CNAME' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rtype eq 'CNAME') { } 4 1887 1891
elsif ($rtype eq $qtype) { } 1887 0 1887
670 # remember CNAME so that we can check that the answer record
671 # for $qtype matches name from query or CNAME which is an alias
672 # for name
673 4 ▶ if ( exists $cname{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cname{$name}) 0 4 4
674 0 ▶ $DEBUG && DEBUG( "more than one CNAME for same name" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("more than one CNAME for same name") 0 0 0
675 0 next; # XXX should we TempError instead of ignoring?
676 }
677 4 $cname{$name} = $rr->cname;
678 } elsif ( $rtype eq $qtype ) {
679 1887 push @{ $ans{$name}},$rr;
680 } else {
681 # XXXX should we TempError instead of ignoring?
682 0 ▶ $DEBUG && DEBUG( "unexpected answer record for $qtype:$qname" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("unexpected answer record for ${qtype}:$qname") 0 0 0
683 }
684 }
685  
686 # find all valid names, usually there should be at most one CNAME
687 # works by starting with name from query, finding CNAMEs for it,
688 # adding these to set and finding next CNAMEs etc
689 # if there are unconnected CNAMEs they will be left in %cname
690 1585 my @names = ($qname);
691 1585 ▶ while ( %cname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%cname) 4 1585 1589
692 4 ▶ my @n = grep { defined $_ } delete @cname{@names} or last;
Branch: 1 branch
BranchTrueFalseTotal
my(@n) = grep({defined $_;} delete @cname{@names}) or last 0 4 4
693 4 push @names, map { lc($_) } @n;
694 }
695 1585 ▶ if ( %cname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%cname) 0 1585 1585
696 # Report but ignore - XXX should we TempError instead?
697 0 ▶ $DEBUG && DEBUG( "unrelated CNAME records ".Dumper(\%cname));
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG('unrelated CNAME records ' . Dumper(\%cname)) 0 0 0
698 }
699  
700 # collect the RR for all valid names
701 1585 my @ans;
702 1585 for (@names) {
703 1589 ▶ my $rrs = delete $ans{$_} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $rrs = delete $ans{$_} or next 4 1585 1589
704 1585 push @ans,@$rrs;
705 }
706 1585 ▶ if ( %ans ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%ans) 0 1585 1585
707 # answer records which don't match name from query or via CNAME
708 # derived names
709 # Report but ignore - XXX should we TempError instead?
710 0 ▶ $DEBUG && DEBUG( "unrelated answer records for $qtype names=@names ".Dumper(\%ans));
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("unrelated answer records for $qtype names=@names " . Dumper(\%ans)) 0 0 0
711 }
712  
713 1585 ▶ if ( ! @ans and @names>1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @ans and @names > 1) 0 1585 1585
Condition33%1/3
not @ans and @names > 1
!ll&&!rl&&r
158500
MC/DC0%0/2
not @ans and @names > 1
not @ans @names > 1
Truth table33%1/3
not(A) and B
A= not @ansB= @names > 1
ABresult
0X0
100
111
714 # according to RFC1034 all RR for the type should be put into
715 # the answer section together with the CNAMEs
716 # so if there are no RRs in this answer, we should assume, that
717 # there will be no RRs at all
718 0 ▶ $DEBUG && DEBUG( "no answer records for $qtype, but names @names" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no answer records for $qtype, but names @names") 0 0 0
719 }
720  
721 1585 my ($sub,@arg) = @$callback;
722 1585 return $self->_next_process_cbrv(
723 $sub->($self,$qtype,$rcode,\@ans,[ $dnsresp->additional ],@arg));
724 }
725  
726 ############################################################################
727 # return list of DNS queries which are still open
728 # Args: ($self)
729 # Returns: @dnsq
730 ############################################################################
731 sub todo {
732 return
733 0 ▶ map { $_->{pkt} ? ($_->{pkt}):() }
Branch: 1 branch
BranchTrueFalseTotal
$_->{'pkt'} ? : 0 0 0
734 0 ▶ @{ shift->{cbq} }
Subroutine todo: not called CC=2 SCAR=17.9
Pod undocumented
735 }
736  
737 ############################################################################
738 # fill information in hash of final result
739 # Args: ($self)
740 ############################################################################
741 sub _update_result_info {
742 1100 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _update_result_info: called CC=5 SCAR=16.5
743 1100 ▶ my $h = $self->{result} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $h = $self->{"result"} or return 0 1100 1100
744 1100 ▶ $h = $h->[2] or return;
Branch: 1 branch
BranchTrueFalseTotal
$h = $h->[2] or return 49 1051 1100
745 $h->{'client-ip'} = $self->{clientip4}
746 ? inet_ntoa($self->{clientip4})
747 1051 ▶ : inet_ntop(AF_INET6,$self->{clientip6});
Branch: 1 branch
BranchTrueFalseTotal
$self->{'clientip4'} ? : 1051 0 1051
748 1051 $h->{helo} = $self->{helo};
749 1051 $h->{identity} = $self->{identity};
750 1051 ▶ $h->{'envelope-from'} = "<$self->{sender}>" if $self->{sender};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sender"} 1051 0 1051
751 }
752  
753 ############################################################################
754 # process results from callback to DNS reply, called from next
755 # Args: ($self,@rv)
756 # @rv: result from callback, either
757 # @query - List of new Net::DNS::Packet queries for next step
758 # () - no result (go on with next step)
759 # (status,...) - final response
760 # Returns: ... - see sub next
761 ############################################################################
762 sub _next_process_cbrv {
763 2819 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _next_process_cbrv: called CC=26 SCAR=33.0
764 2819 my @rv = @_; # results from callback to _mech*
765  
766 # resolving of %{p} in exp= mod or explain TXT results in @rv = ()
767 # see sub _validate_*
768 2819 ▶ if ( $self->{result} && ! @rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"result"} and not @rv) 20 2799 2819
Condition100%3/3
$self->{"result"} and not @rv
!ll&&!rl&&r
268711220
MC/DC100%2/2
$self->{"result"} and not @rv
$self->{"result"} not @rv
Truth table100%3/3
A and not(B)
A= $self->{"result"}B= not @rv
ABresult
0X0
100
111
769 # set to final result
770 20 @rv = @{ $self->{result}};
771 }
772  
773 # if the last mech (which was called with the DNS reply in sub next) got
774 # no match and no further questions we need to find the match or questions
775 # either by processing the next mech in the current SPF record, following
776 # a redirect or going the include stack up
777 2819 ▶ @rv = $self->_next_mech if ! @rv;
Branch: 1 branch
BranchTrueFalseTotal
unless @rv 1049 1770 2819
778  
779 2819 ▶ if ( UNIVERSAL::isa( $rv[0],'Net::DNS::Packet' )) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($rv[0], "Net::DNS::Packet")) 1735 1084 2819
780 # @rv is list of DNS packets
781 1735 return $self->_next_rv_dnsq(@rv)
782 }
783  
784 # @rv is (status,...)
785 # status of SPF_Noop is special in that it returns nothing as a sign, that
786 # it just waits for more input
787 # Only used when we could get multiple responses, e.g when multiple DNS
788 # requests were send like in the query for SPF+TXT
789 1084 ▶ if ( $rv[0] eq SPF_Noop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv[0] eq "_NOOP") 18 1066 1084
790 die "NOOP but no open queries"
791 18 ▶ if ! grep { $_->{pkt} } @{$self->{cbq}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->{"pkt"};} @{$$self{"cbq"};} 0 18 18
792 18 return ('');
793 }
794  
795 # inside include the response is only pre-final,
796 # propagate it the include stack up:
797 # see RFC4408, 5.2 for propagation of results
798 1066 ▶ while ( my $top = pop @{ $self->{include_stack} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $top = pop @{$$self{"include_stack"};}) 116 1064 1180
799 116 ▶ $DEBUG && DEBUG( "pre-final response $rv[0]" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("pre-final response $rv[0]") 116 0 116
800  
801 116 ▶ if ( $rv[0] eq SPF_TempError || $rv[0] eq SPF_PermError ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv[0] eq 'TempError' or $rv[0] eq 'PermError') { } 72 44 116
elsif ($rv[0] eq 'None') { } 0 44 44
Condition66%2/3
$rv[0] eq "TempError" or $rv[0] eq "PermError"
l!l&&r!l&&!r
07244
MC/DC50%1/2
$rv[0] eq "TempError" or $rv[0] eq "PermError"
$rv[0] eq "TempError" $rv[0] eq "PermError"
Truth table66%2/3
A or B
A= $rv[0] eq "TempError"B= $rv[0] eq "PermError"
ABresult
1X1
011
000
802 # keep
803 } elsif ( $rv[0] eq SPF_None ) {
804 0 $rv[0] = SPF_PermError; # change None to PermError
805 } else {
806 # go stack up, restore saved data
807 44 my $qual = delete $top->{qual};
808 44 ▶ while ( my ($k,$v) = each %$top ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$top) 176 44 220
809 176 $self->{$k} = $v;
810 }
811 44 ▶ if ( $rv[0] eq SPF_Pass ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv[0] eq 'Pass') { } 16 28 44
812 # Pass == match -> set status to $qual
813 16 $rv[0] = $qual;
814 } else {
815 # ! Pass == non-match
816 # -> restart with @rv=() and go on with next mech
817 28 @rv = $self->_next_mech;
818 28 ▶ if ( UNIVERSAL::isa( $rv[0],'Net::DNS::Packet' )) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($rv[0], "Net::DNS::Packet")) 2 26 28
819 # @rv is list of DNS packets
820 2 return $self->_next_rv_dnsq(@rv)
821 }
822 }
823 }
824 }
825  
826 # no more include stack
827 # -> @rv is the probably the final result, but check if we had a better
828 # one already
829 1064 my $final;
830 1064 ▶ if ($self->{tmpresult} and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'tmpresult'} and $ResultQ{$self->{'tmpresult'}[0]} > $ResultQ{$rv[0]}) { } 0 1064 1064
Condition66%2/3
$self->{"tmpresult"} and $ResultQ{$self->{"tmpresult"}[0]} > $ResultQ{$rv[0]}
!ll&&!rl&&r
106220
MC/DC0%0/2
$self->{"tmpresult"} and $ResultQ{$self->{"tmpresult"}[0]} > $ResultQ{$rv[0]}
$self->{"tmpresult"} $ResultQ{$self->{"tmpresult"}[0]} > $ResultQ{$rv[0]}
Truth table66%2/3
A and B
A= $self->{"tmpresult"}B= $ResultQ{$self->{"tmpresult"}[0]} > $ResultQ{$rv[0]}
ABresult
0X0
100
111
831 $ResultQ{ $self->{tmpresult}[0] } > $ResultQ{ $rv[0] }) {
832 0 $final = $self->{result} = $self->{tmpresult};
833 } else {
834 1064 $final = $self->{result} = [ @rv ];
835 }
836 1064 _update_result_info($self);
837  
838 # now the only things left is to handle explain in case of SPF_Fail
839 1064 ▶ return @$final if $final->[0] ne SPF_Fail; # finally done
Branch: 1 branch
BranchTrueFalseTotal
if $final->[0] ne "Fail" 740 324 1064
840  
841 # set default explanation
842 324 ▶ $final->[3] = $self->explain_default if ! defined $final->[3];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $final->[3] 224 100 324
843  
844 # lookup TXT record for explain
845 324 ▶ if ( my $exp = delete $self->{explain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exp = delete $self->{"explain"}) 106 218 324
846 106 ▶ if (ref $exp) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exp) 4 102 106
847 4 ▶ if ( my @dnsq = $self->_resolve_macro_p($exp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@dnsq) = $self->_resolve_macro_p($exp)) 2 2 4
848 # we need to do more DNS lookups for resolving %{p} macros
849 # inside the exp=... modifier, before we get the domain name
850 # which contains the TXT for explain
851 2 ▶ $DEBUG && DEBUG( "need to resolve %{p} in $exp->{macro}" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG(qq[need to resolve %{p} in $exp->{"macro"}]) 2 0 2
852 2 $self->{explain} = $exp; # put back until resolved
853 2 return $self->_next_rv_dnsq(@dnsq)
854 }
855 2 $exp = $exp->{expanded};
856 }
857 104 ▶ if ( my @err = _check_domain( $exp, "explain:$exp" )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@err) = _check_domain($exp, "explain:$exp")) 0 104 104
858 # bad domain: return unmodified final
859 0 return @$final;
860 }
861 104 ▶ $DEBUG && DEBUG( "lookup TXT for '$exp' for explain" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("lookup TXT for '${exp}' for explain") 104 0 104
862 104 $self->{cb} = [ \&_got_TXT_exp ];
863 104 return $self->_next_rv_dnsq( Net::DNS::Packet->new($exp,'TXT','IN'));
864 }
865  
866 # resolve macros in TXT record for explain
867 218 ▶ if ( my $exp = delete $final->[4] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exp = delete $final->[4]) 12 206 218
868 # we had a %{p} to resolve in the TXT we got for explain,
869 # see _got_TXT_exp -> should be expanded now
870 12 $final->[3] = $exp->{expanded};
871  
872 }
873  
874 # This was the last action needed
875 218 return @$final;
876 }
877  
878 ############################################################################
879 # try to match or give more questions by
880 # - trying the next mechanism in the current SPF record
881 # - if there is no next mech try to redirect to another SPF record
882 # - if there is no redirect try to go include stack up
883 # - if there is no include stack return SPF_Neutral
884 # Args: $self
885 # Returns: @query|@final
886 # @query: new queries as list of Net::DNS::Packets
887 # @final: final SPF result (see sub next)
888 ############################################################################
889 sub _next_mech {
890 1077 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _next_mech: called CC=12 SCAR=26.1
891  
892 1077 for my $dummy (1) {
893  
894 # if we have more mechanisms in the current SPF record take next
895 1201 ▶ if ( my $next = shift @{$self->{mech}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = shift @{$$self{"mech"};}) 1036 165 1201
896 1036 my ($sub,$id,@arg) = @$next;
897 1036 my @rv = $sub->($self,@arg);
898 1036 ▶ redo if ! @rv; # still no match and no queries
Branch: 1 branch
BranchTrueFalseTotal
unless @rv 116 920 1036
899 920 return @rv;
900 }
901  
902 # if no mechanisms in current SPF record but we have a redirect
903 # continue with the SPF record from the new location
904 165 ▶ if ( my $domain = $self->{redirect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $domain = $self->{"redirect"}) 108 57 165
905 108 ▶ if ( ref $domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $domain) 0 108 108
906 # need to resolve %{p}
907 0 ▶ if ( $domain->{macro} and
Branch: 1 branch
BranchTrueFalseTotal
if ($domain->{"macro"} and my(@rv) = $self->_resolve_macro_p($domain)) 0 0 0
Condition0%0/3
$domain->{"macro"} and my(@rv) = $self->_resolve_macro_p($domain)
!ll&&!rl&&r
000
MC/DC0%0/2
$domain->{"macro"} and my(@rv) = $self->_resolve_macro_p($domain)
$domain->{"macro"} my(@rv) = $self->_resolve_macro_p($domain)
Truth table0%0/3
A and B
A= $domain->{"macro"}B= my(@rv) = $self->_resolve_macro_p($domain)
ABresult
0X0
100
111
908 ( my @rv = $self->_resolve_macro_p($domain))) {
909 0 return @rv;
910 }
911 0 $self->{redirect} = $domain = $domain->{expanded};
912 }
913 108 ▶ if ( my @err = _check_domain($domain,"redirect:$domain" )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@err) = _check_domain($domain, "redirect:$domain")) 0 108 108
914 0 return @err;
915 }
916  
917 return ( SPF_PermError, "",
918 { problem => "Number of DNS mechanism exceeded" })
919 108 ▶ if --$self->{limit_dns_mech} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$self->{"limit_dns_mech"} < 0 6 102 108
920  
921 # reset state information
922 102 $self->{mech} = [];
923 102 $self->{explain} = undef;
924 102 $self->{redirect} = undef;
925  
926 # set domain to domain from redirect
927 102 $self->{domain} = $domain;
928  
929 # restart with new SPF record
930 102 return $self->_query_txt_spf;
931 }
932  
933 # if there are still no more mechanisms available and we are inside
934 # an include go up the include stack
935 57 my $st = $self->{include_stack};
936 57 ▶ if (@$st) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$st) 8 49 57
937 8 my $top = pop @$st;
938 8 delete $top->{qual};
939 8 ▶ while ( my ($k,$v) = each %$top ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$top) 32 8 40
940 32 $self->{$k} = $v;
941 }
942 # continue with mech or redirect of upper SPF record
943 8 redo;
944 }
945 }
946  
947 # no mech, no redirect and no include stack
948 # -> give up finally and return SPF_Neutral
949 49 return ( SPF_Neutral,'no matches' );
950 }
951  
952 ############################################################################
953 # if @rv is list of DNS packets return them as (undef,@dnspkt)
954 # remember the queries so that the answers can later (sub next) verified
955 # against the queries
956 # Args: ($self,@dnsq)
957 # @dnsq: list of Net::DNS::Packet's
958 # Returns: (undef,@dnsq)
959 ############################################################################
960 sub _next_rv_dnsq {
961 1843 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _next_rv_dnsq: called CC=2 SCAR=7.0
962 1843 my @dnsq = @_;
963 # track queries for later verification
964 $self->{cbq} = [ map {
965 1843 { q => ($_->question)[0], id => $_->header->id, pkt => $_ }
966 } @dnsq ];
967 $DEBUG && DEBUG( "need to lookup ".join( " | ",
968 1843 ▶ map { "'".$_->{id}.'/'.$_->{q}->string."'" } @{$self->{cbq}}));
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG('need to lookup ' . join(" | ", map({q['] . $_->{"id"} . '/' . $_->{"q"}->string . q['];} @{$$self{"cbq"};}))) 1843 0 1843
969 1843 return ( undef,@dnsq );
970 }
971  
972 ############################################################################
973 # check if the domain has the right format
974 # this checks the domain before the macros got expanded
975 ############################################################################
976 sub _check_macro_domain {
977 458 ▶ my ($domain,$why) = @_;
Subroutine _check_macro_domain: called CC=1 SCAR=0.0
978 # 'domain-spec': see RFC4408 Appendix A for ABNF
979 458 my $rx = qr{
980 # macro-string
981 (?:
982 [^%\s]+ |
983 % (?: { [slodipvh] \d* r? [.\-+,/_=]* } | [%\-_] )
984 )*
985 # domain-end
986 (?:(?:
987 # toplabel
988 \. [\da-z]*[a-z][\da-z]* |
989 \. [\da-z]+-[\-a-z\d]*[\da-z]
990 ) | (?:
991 # macro-expand
992 % (?: { [slodipvh] \d* r? [.\-+,/_=]* } | [%\-_] )
993 ))
994 }xi;
995 458 _check_domain( $domain,$why,$rx);
996 }
997  
998 ############################################################################
999 # check if the domain has the right format
1000 # this checks the domain after the macros got expanded
1001 ############################################################################
1002 sub _check_domain {
1003 2462 ▶ my ($domain,$why,$rx) = @_;
Subroutine _check_domain: called CC=9 SCAR=22.2
1004 2462 ▶ $why = '' if ! defined $why;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $why 458 2004 2462
1005  
1006 # domain name according to RFC2181 can be anything binary!
1007 # this is not only for host names
1008 2462 ▶ $rx ||= qr{.*?};
Condition66%2/3
$rx ||= qr/.*?/
l!l&&r!l&&!r
45820040
MC/DC0%0/2
$rx ||= qr/.*?/
$rx qr/.*?/
Truth table66%2/3
A ||= B
A= $rxB= qr/.*?/
ABresult
1X1
011
000
1009  
1010 2462 my @rv;
1011 2462 ▶ if ( $domain =~m{[^\d.]}
Branch: 1 branch
BranchTrueFalseTotal
if ($domain =~ /[^\d.]/ and $domain =~ s/^($rx)\.?$/$1/) { } 2378 84 2462
Condition100%3/3
$domain =~ /[^\d.]/ and $domain =~ s/^($rx)\.?$/$1/
!ll&&!rl&&r
12722378
MC/DC100%2/2
$domain =~ /[^\d.]/ and $domain =~ s/^($rx)\.?$/$1/
$domain =~ /[^\d.]/ $domain =~ s/^($rx)\.?$/$1/
Truth table100%3/3
A and B
A= $domain =~ /[^\d.]/B= $domain =~ s/^($rx)\.?$/$1/
ABresult
0X0
100
111
1012 && $domain =~s{^($rx)\.?$}{$1} ) {
1013 # looks like valid domain name
1014 2378 ▶ if ( grep { length == 0 || length>63 } split( m{\.},$domain,-1 )) {
Branch: 3 branches
BranchTrueFalseTotal
if (grep {length $_ == 0 or length $_ > 63;} split(/\./, $domain, -1)) { } 32 2346 2378
elsif (length $domain > 253) { } 0 2346 2346
length $_ == 0 or length $_ > 63 7364 30 7394
1015 32 @rv = ( SPF_PermError,"query $why", { problem =>
1016 "DNS labels limited to 63 chars and should not be empty." });
1017 } elsif ( length($domain)>253 ) {
1018 0 @rv = ( SPF_PermError,"query $why",
1019 { problem => "Domain names limited to 253 chars." });
1020 } else {
1021 #DEBUG( "domain name ist OK" );
1022 return
1023 2346 }
1024 } else {
1025 84 @rv = ( SPF_PermError, "query $why",
1026 { problem => "Invalid domain name" });
1027 }
1028  
1029 116 ▶ $DEBUG && DEBUG( "error with '$domain': ".$rv[2]{problem} );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("error with '${domain}': " . $rv[2]{"problem"}) 116 0 116
1030 116 return @rv; # have error
1031 }
1032  
1033 ############################################################################
1034 # initial query
1035 # returns queries for SPF and TXT record, next state is _got_txt_spf
1036 ############################################################################
1037 sub _query_txt_spf {
1038 1226 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _query_txt_spf: called CC=5 SCAR=16.2
1039 1226 ▶ $DEBUG && DEBUG( "want SPF/TXT for $self->{domain}" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG(qq[want SPF/TXT for $self->{"domain"}]) 1226 0 1226
1040 # return query for SPF and TXT, we see what we get first
1041 1226 ▶ if ( my @err = _check_domain( $self->{domain}, "SPF/TXT record" )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@err) = _check_domain($self->{"domain"}, "SPF/TXT record")) 20 1206 1226
1042 20 ▶ if ( ! $self->{cb} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"cb"}) 20 0 20
1043 # for initial query return SPF_None on errors
1044 20 $err[0] = SPF_None;
1045 }
1046 20 return @err;
1047 }
1048  
1049 1206 $self->{cb} = [ \&_got_txt_spf ];
1050 return (
1051 # use SPF DNS record only if rfc4408 compatibility is required
1052 $self->{opt}{rfc4408}
1053 ? (scalar(Net::DNS::Packet->new( $self->{domain}, 'SPF','IN' ))):(),
1054 1206 ▶ scalar(Net::DNS::Packet->new( $self->{domain}, 'TXT','IN' )),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'opt'}{'rfc4408'} ? : 370 836 1206
1055 );
1056 }
1057  
1058 ############################################################################
1059 # processes response to SPF|TXT query
1060 # parses response and starts processing
1061 ############################################################################
1062 sub _got_txt_spf {
1063 1202 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _got_txt_spf: called CC=18 SCAR=30.1
1064 1202 my ($qtype,$rcode,$ans,$add) = @_;
1065  
1066 {
1067 1202 ▶ last if ! @$ans;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ans 42 1160 1202
1068  
1069 # RFC4408 says in 4.5:
1070 # 2. If any records of type SPF are in the set, then all records of
1071 # type TXT are discarded.
1072 # But it says that if both SPF and TXT are given they should be the
1073 # same (3.1.1)
1074 # so I think we can ignore the requirement 4.5.2 and just use the
1075 # first record which is valid SPF, if the admin of the domain sets
1076 # TXT and SPF to different values it's his own problem
1077  
1078 1160 my (@spfdata,@senderid);
1079 1160 for my $rr (@$ans) {
1080 1194 my $txtdata = join( '', $rr->char_str_list );
1081 1194 ▶ $txtdata =~m{^
Branch: 1 branch
BranchTrueFalseTotal
$txtdata =~ m[^ (?: (v=spf1) | spf2\.\d/(?:[\w,]*\bmfrom\b[\w,]*) ) (?:$|\040\s*)(.*) ]xi or next 30 1164 1194
1082 (?:
1083 (v=spf1)
1084 | spf2\.\d/(?:[\w,]*\bmfrom\b[\w,]*)
1085 )
1086 (?:$|\040\s*)(.*)
1087 }xi or next;
1088 1164 ▶ if ( $1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1) { } 1156 8 1164
1089 1156 push @spfdata,$2;
1090 1156 ▶ $DEBUG && DEBUG( "got spf data for $qtype: $txtdata" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("got spf data for ${qtype}: $txtdata") 1156 0 1156
1091 } else {
1092 8 push @senderid,$2;
1093 8 ▶ $DEBUG && DEBUG( "got senderid data for $qtype: $txtdata" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("got senderid data for ${qtype}: $txtdata") 8 0 8
1094 }
1095 }
1096  
1097 # if SenderID and SPF are given prefer SPF, else use any
1098 1160 ▶ @spfdata = @senderid if ! @spfdata;
Branch: 1 branch
BranchTrueFalseTotal
unless @spfdata 22 1138 1160
1099  
1100 1160 ▶ @spfdata or last; # no usable SPF reply
Branch: 1 branch
BranchTrueFalseTotal
@spfdata or last 16 1144 1160
1101 1144 ▶ if (@spfdata>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@spfdata > 1) 18 1126 1144
1102 18 return ( SPF_PermError,
1103 "checking $qtype for $self->{domain}",
1104 { problem => "multiple SPF records" }
1105 );
1106 }
1107 1126 ▶ unless ( eval { $self->_parse_spf( $spfdata[0] ) }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->_parse_spf($spfdata[0]) } }) 354 772 1126
1108 # this is an invalid SPF record
1109 # make it a permanent error
1110 # it does not matter if the other type of record is good
1111 # because according to RFC if both provide SPF (v=spf1..)
1112 # they should be the same, so the other one should be bad too
1113 354 return ( SPF_PermError,
1114 "checking $qtype for $self->{domain}",
1115 { problem => "invalid SPF record: $@" }
1116 );
1117 }
1118  
1119 # looks good, return so that next() processes the next query
1120 772 return;
1121 }
1122  
1123 # If this is the first response, wait for the other
1124 58 ▶ $DEBUG && DEBUG( "no records for $qtype ($rcode)" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no records for $qtype ($rcode)") 58 0 58
1125 58 ▶ if ( grep { $_->{pkt} } @{ $self->{cbq}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->{"pkt"};} @{$$self{"cbq"};}) 18 40 58
1126 18 return (SPF_Noop);
1127 }
1128  
1129 # otherwise it means that we got no SPF or TXT records
1130  
1131 # if we have a default record and we are at the first level use this
1132 40 ▶ if (!$self->{mech} and my $default = $self->{opt}{default_spf}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"mech"} and my $default = $self->{"opt"}{"default_spf"}) 0 40 40
Condition66%2/3
not $self->{"mech"} and my $default = $self->{"opt"}{"default_spf"}
!ll&&!rl&&r
12280
MC/DC0%0/2
not $self->{"mech"} and my $default = $self->{"opt"}{"default_spf"}
not $self->{"mech"} my $default = $self->{"opt"}{"default_spf"}
Truth table66%2/3
not(A) and B
A= not $self->{"mech"}B= my $default = $self->{"opt"}{"default_spf"}
ABresult
0X0
100
111
1133 0 ▶ if (eval { $self->_parse_spf($default) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $self->_parse_spf($default) } }) 0 0 0
1134 # good
1135 0 $self->{used_default_spf} = $default;
1136 0 return;
1137 }
1138 0 return (SPF_PermError,
1139 "checking default SPF for $self->{domain}",
1140 { problem => "invalid default SPF record: $@" }
1141 );
1142 }
1143  
1144 # return SPF_None if this was the initial query ($self->{mech} is undef)
1145 # and SPF_PermError if as a result from redirect or include
1146 # ($self->{mech} is [])
1147 40 ▶ $DEBUG && DEBUG( "no usable SPF/TXT records" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no usable SPF/TXT records") 40 0 40
1148 40 ▶ return ( $self->{mech} ? SPF_PermError : SPF_None,
Branch: 1 branch
BranchTrueFalseTotal
$self->{'mech'} ? : 12 28 40
1149 'query SPF/TXT record',
1150 { problem => 'no SPF records found' });
1151 }
1152  
1153  
1154 ############################################################################
1155 # parse SPF record, returns 1 if record looks valid,
1156 # otherwise die()s with somewhat helpful error message
1157 ############################################################################
1158 sub _parse_spf {
1159 1126 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _parse_spf: called CC=69 SCAR=43.4
1160 1126 my $data = shift;
1161  
1162 1126 my (@mech,$redirect,$explain);
1163 1126 for ( split( ' ', $data )) {
1164 1954 ▶ my ($qual,$mech,$mod,$arg) = m{^(?:
Branch: 1 branch
BranchTrueFalseTotal
() = /^(?: ([~\-+?]?) # Qualifier (all|ip[46]|a|mx|ptr|exists|include) # Mechanism |(redirect|exp) # Modifier |[a-zA-Z][\w.\-]*= # unknown modifier + '=' )([ \t\x20-\x7e]*) # Arguments $/x or die "bad SPF part: $_\n" 54 1900 1954
1165 ([~\-+?]?) # Qualifier
1166 (all|ip[46]|a|mx|ptr|exists|include) # Mechanism
1167 |(redirect|exp) # Modifier
1168 |[a-zA-Z][\w.\-]*= # unknown modifier + '='
1169 )([ \t\x20-\x7e]*) # Arguments
1170 $}x
1171 or die "bad SPF part: $_\n";
1172  
1173 1900 ▶ if ( $mech ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mech) { } 1568 332 1900
elsif ($mod) { } 302 30 332
1174 1568 ▶ $qual = $qual2rv{ $qual || '+' };
Condition100%2/2
$qual || '+'
l!l
5341034
MC/DC100%1/1
$qual || '+'
$qual
Truth table100%2/2
A
A= $qual
Aresult
11
00
1175  
1176 1568 ▶ if ( $mech eq 'all' ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($mech eq 'all') { } 520 1048 1568
elsif ($mech eq 'ip4') { } 238 810 1048
elsif ($mech eq 'ip6') { } 24 786 810
elsif ($mech eq 'a' or $mech eq 'mx') { } 514 272 786
elsif ($mech eq 'ptr') { } 50 222 272
elsif ($mech eq 'exists') { } 58 164 222
elsif ($mech eq 'include') { } 164 0 164
1177 520 ▶ die "no arguments allowed with mechanism 'all': '$_'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $arg ne "" 18 502 520
1178 if $arg ne '';
1179 502 push @mech, [ \&_mech_all, $_, $qual ]
1180  
1181 } elsif ( $mech eq 'ip4' ) {
1182 238 ▶ my ($ip,$plen) =
Branch: 1 branch
BranchTrueFalseTotal
() = $arg =~ m[^:(\d+\.\d+\.\d+\.\d+)(?:/([1-9]\d*|0))?$] or die "bad argument for mechanism 'ip4' in '${_}'\n" 30 208 238
1183 $arg =~m{^:(\d+\.\d+\.\d+\.\d+)(?:/([1-9]\d*|0))?$}
1184 or die "bad argument for mechanism 'ip4' in '$_'\n";
1185 208 ▶ $plen = 32 if ! defined $plen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plen 184 24 208
1186 208 ▶ $plen>32 and die "invalid prefix len >32 in '$_'\n";
Branch: 1 branch
BranchTrueFalseTotal
$plen > 32 and die "invalid prefix len >32 in '${_}'\n" 6 202 208
1187 202 ▶ eval { $ip = inet_aton( $ip ) }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $ip = inet_aton($ip) } } or die "bad ip '${ip}' in '${_}'\n" 0 202 202
1188 or die "bad ip '$ip' in '$_'\n";
1189 202 ▶ next if ! $self->{clientip4}; # don't use for IP6
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"clientip4"} 0 202 202
1190 202 push @mech, [ \&_mech_ip4, $_, $qual, $ip,$plen ];
1191  
1192 } elsif ( $mech eq 'ip6' ) {
1193 24 ▶ my ($ip,$plen) =
Branch: 1 branch
BranchTrueFalseTotal
() = $arg =~ m[^:([\da-fA-F:\.]+)(?:/([1-9]\d*|0))?$] or die "bad argument for mechanism 'ip6' in '${_}'\n" 6 18 24
1194 $arg =~m{^:([\da-fA-F:\.]+)(?:/([1-9]\d*|0))?$}
1195 or die "bad argument for mechanism 'ip6' in '$_'\n";
1196 18 ▶ $plen = 128 if ! defined $plen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plen 0 18 18
1197 18 ▶ $plen>128 and die "invalid prefix len >128 in '$_'\n";
Branch: 1 branch
BranchTrueFalseTotal
$plen > 128 and die "invalid prefix len >128 in '${_}'\n" 6 12 18
1198 12 ▶ eval { $ip = inet_pton( AF_INET6,$ip ) }
Branch: 1 branch
BranchTrueFalseTotal
if $can_ip6 12 0 12
Condition40%2/5
$can_ip6 && (eval { do { $ip = inet_pton(10, $ip) } } || die("bad ip '${ip}' in '${_}'\n"))
!ll&&!rl&&r
0120
eval { do { $ip = inet_pton(10, $ip) } } or die "bad ip '${ip}' in '${_}'\n"
l!l
120
MC/DC0%0/2
$can_ip6 && (eval { do { $ip = inet_pton(10, $ip) } } || die("bad ip '${ip}' in '${_}'\n"))
$can_ip6 eval { do { $ip = inet_pton(10, $ip) } }
Truth table0%0/3
A && B
A= $can_ip6B= eval { do { $ip = inet_pton(10, $ip) } }
ABresult
0X0
100
111
1199 or die "bad ip '$ip' in '$_'\n"
1200 if $can_ip6;
1201 12 ▶ next if ! $self->{clientip6}; # don't use for IP4
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"clientip6"} 12 0 12
1202 0 push @mech, [ \&_mech_ip6, $_, $qual, $ip,$plen ];
1203  
1204 ▶ } elsif ( $mech eq 'a' or $mech eq 'mx' ) {
Condition100%3/3
$mech eq "a" or $mech eq "mx"
l!l&&r!l&&!r
358156272
MC/DC100%2/2
$mech eq "a" or $mech eq "mx"
$mech eq "a" $mech eq "mx"
Truth table100%3/3
A or B
A= $mech eq "a"B= $mech eq "mx"
ABresult
1X1
011
000
1205 514 ▶ $arg ||= '';
Condition100%2/2
$arg ||= ""
l!l
282232
MC/DC100%1/1
$arg ||= ""
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
1206 514 ▶ my ($domain,$plen4,$plen6) =
Branch: 1 branch
BranchTrueFalseTotal
() = $arg =~ m[^ (?: : (.+?))? # [ ":" domain-spec ] (?: / (?: ([1-9]\d*|0) ))? # [ ip4-cidr-length ] (?: // (?: ([1-9]\d*|0) ))? # [ "/" ip6-cidr-length ] $]x or die "bad argument for mechanism '${mech}' in '${_}'\n" 16 498 514
1207 $arg =~m{^
1208 (?: : (.+?))? # [ ":" domain-spec ]
1209 (?: / (?: ([1-9]\d*|0) ))? # [ ip4-cidr-length ]
1210 (?: // (?: ([1-9]\d*|0) ))? # [ "/" ip6-cidr-length ]
1211 $}x or die "bad argument for mechanism '$mech' in '$_'\n";
1212  
1213 498 ▶ $plen4 = 32 if ! defined $plen4;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plen4 434 64 498
1214 498 ▶ $plen6 = 128 if ! defined $plen6;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plen6 454 44 498
1215 498 ▶ die "invalid prefix len >32 in '$_'\n" if $plen4>32;
Branch: 1 branch
BranchTrueFalseTotal
if $plen4 > 32 12 486 498
1216 486 ▶ die "invalid prefix len >128 in '$_'\n" if $plen6>128;
Branch: 1 branch
BranchTrueFalseTotal
if $plen6 > 128 12 474 486
1217 474 ▶ if ( ! $domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $domain) { } 288 186 474
1218 288 $domain = $self->{domain};
1219 } else {
1220 186 ▶ if ( my @err = _check_macro_domain($domain)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@err) = _check_macro_domain($domain)) 72 114 186
1221 72 ▶ die(($err[2]->{problem}||"Invalid domain name")."\n");
Condition50%1/2
$err[2]{'problem'} || 'Invalid domain name'
l!l
720
MC/DC0%0/1
$err[2]{'problem'} || 'Invalid domain name'
$err[2]{'problem'}
Truth table50%1/2
A
A= $err[2]{'problem'}
Aresult
11
00
1222 }
1223 114 $domain = $self->_macro_expand($domain);
1224 }
1225 402 ▶ my $sub = $mech eq 'a' ? \&_mech_a : \&_mech_mx;
Branch: 1 branch
BranchTrueFalseTotal
$mech eq 'a' ? : 282 120 402
1226 402 ▶ push @mech, [ \&_resolve_macro_p, $domain ] if ref($domain);
Branch: 1 branch
BranchTrueFalseTotal
if ref $domain 0 402 402
1227 push @mech, [ $sub, $_, $qual, $domain,
1228 402 ▶ $self->{clientip4} ? $plen4:$plen6 ];
Branch: 1 branch
BranchTrueFalseTotal
$self->{'clientip4'} ? : 402 0 402
1229  
1230 } elsif ( $mech eq 'ptr' ) {
1231 50 ▶ my ($domain) = ( $arg || '' )=~m{^(?::([^/]+))?$}
Branch: 1 branch
BranchTrueFalseTotal
my($domain) = ($arg || "") =~ m[^(?::([^/]+))?$] or die "bad argument for mechanism '${mech}' in '${_}'\n" 12 38 50
Condition100%2/2
$arg || ''
l!l
2030
MC/DC100%1/1
$arg || ''
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
1232 or die "bad argument for mechanism '$mech' in '$_'\n";
1233 $domain = $domain
1234 ? $self->_macro_expand($domain)
1235 38 ▶ : $self->{domain};
Branch: 1 branch
BranchTrueFalseTotal
$domain ? : 8 30 38
1236 38 ▶ push @mech, [ \&_resolve_macro_p, $_, $domain ] if ref($domain);
Branch: 1 branch
BranchTrueFalseTotal
if ref $domain 0 38 38
1237 38 push @mech, [ \&_mech_ptr, $_, $qual, $domain ];
1238  
1239 } elsif ( $mech eq 'exists' ) {
1240 58 ▶ my ($domain) = ( $arg || '' )=~m{^:([^/]+)$}
Branch: 1 branch
BranchTrueFalseTotal
my($domain) = ($arg || "") =~ m[^:([^/]+)$] or die "bad argument for mechanism '${mech}' in '${_}'\n" 18 40 58
Condition100%2/2
$arg || ''
l!l
526
MC/DC100%1/1
$arg || ''
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
1241 or die "bad argument for mechanism '$mech' in '$_'\n";
1242 40 $domain = $self->_macro_expand($domain);
1243 26 ▶ push @mech, [ \&_resolve_macro_p, $_, $domain ] if ref($domain);
Branch: 1 branch
BranchTrueFalseTotal
if ref $domain 12 14 26
1244 26 push @mech, [ \&_mech_exists, $_, $qual, $domain ];
1245  
1246 } elsif ( $mech eq 'include' ) {
1247 164 ▶ my ($domain) = ( $arg || '' )=~m{^:([^/]+)$}
Branch: 1 branch
BranchTrueFalseTotal
my($domain) = ($arg || "") =~ m[^:([^/]+)$] or die "bad argument for mechanism '${mech}' in '${_}'\n" 24 140 164
Condition100%2/2
$arg || ''
l!l
15212
MC/DC100%1/1
$arg || ''
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
1248 or die "bad argument for mechanism '$mech' in '$_'\n";
1249 140 $domain = $self->_macro_expand($domain);
1250 140 ▶ push @mech, [ \&_resolve_macro_p, $_, $domain ] if ref($domain);
Branch: 1 branch
BranchTrueFalseTotal
if ref $domain 0 140 140
1251 140 push @mech, [ \&_mech_include, $_, $qual, $domain ];
1252  
1253 } else {
1254 0 die "unhandled mechanism '$mech'\n"
1255 }
1256  
1257 } elsif ( $mod ) {
1258 # multiple redirect or explain will be considered an error
1259 302 ▶ if ( $mod eq 'redirect' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($mod eq 'redirect') { } 144 158 302
elsif ($mod eq 'exp') { } 158 0 158
elsif ($mod) { } 0 0 0
1260 144 ▶ die "redirect was specified more than once\n" if $redirect;
Branch: 1 branch
BranchTrueFalseTotal
if $redirect 6 138 144
1261 138 ▶ my ($domain) = ( $arg || '' )=~m{^=([^/]+)$}
Branch: 1 branch
BranchTrueFalseTotal
my($domain) = ($arg || "") =~ m[^=([^/]+)$] or die "bad argument for modifier '${mod}' in '${_}'\n" 12 126 138
Condition50%1/2
$arg || ''
l!l
1380
MC/DC0%0/1
$arg || ''
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
1262 or die "bad argument for modifier '$mod' in '$_'\n";
1263 126 ▶ if ( my @err = _check_macro_domain($domain)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@err) = _check_macro_domain($domain)) 6 120 126
1264 6 ▶ die(( $err[2]->{problem} || "Invalid domain name" )."\n" );
Condition50%1/2
$err[2]{'problem'} || 'Invalid domain name'
l!l
60
MC/DC0%0/1
$err[2]{'problem'} || 'Invalid domain name'
$err[2]{'problem'}
Truth table50%1/2
A
A= $err[2]{'problem'}
Aresult
11
00
1265 }
1266 120 $redirect = $self->_macro_expand($domain);
1267  
1268 } elsif ( $mod eq 'exp' ) {
1269 158 ▶ die "$explain was specified more than once\n" if $explain;
Branch: 1 branch
BranchTrueFalseTotal
if $explain 6 152 158
1270 152 ▶ my ($domain) = ( $arg || '' )=~m{^=([^/]+)$}
Branch: 1 branch
BranchTrueFalseTotal
my($domain) = ($arg || "") =~ m[^=([^/]+)$] or die "bad argument for modifier '${mod}' in '${_}'\n" 6 146 152
Condition50%1/2
$arg || ''
l!l
1520
MC/DC0%0/1
$arg || ''
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
1271 or die "bad argument for modifier '$mod' in '$_'\n";
1272 146 ▶ if ( my @err = _check_macro_domain($domain)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@err) = _check_macro_domain($domain)) 12 134 146
1273 12 ▶ die(( $err[2]->{problem} || "Invalid domain name" )."\n" );
Condition50%1/2
$err[2]{'problem'} || 'Invalid domain name'
l!l
120
MC/DC0%0/1
$err[2]{'problem'} || 'Invalid domain name'
$err[2]{'problem'}
Truth table50%1/2
A
A= $err[2]{'problem'}
Aresult
11
00
1274 }
1275 134 $explain = $self->_macro_expand($domain);
1276  
1277 } elsif ( $mod ) {
1278 0 die "unhandled modifier '$mod'\n"
1279 }
1280 } else {
1281 # unknown modifier - check if arg is valid macro-string
1282 # (will die() on error) but ignore modifier
1283 30 ▶ $self->_macro_expand($arg || '');
Condition50%1/2
$arg || ''
l!l
300
MC/DC0%0/1
$arg || ''
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
1284 }
1285 }
1286  
1287 772 ▶ if ($self->{opt}{pass_all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"pass_all"}) 256 516 772
1288 256 my $r = 0;
1289 256 for (@mech) {
1290 380 my $qual = $_->[2];
1291 380 ▶ last if $_->[0] == \&_mech_include;
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] == \&Mail::SPF::Iterator::_mech_include 46 334 380
1292 334 ▶ $r=-1,last if $qual eq SPF_Fail;
Branch: 1 branch
BranchTrueFalseTotal
if $qual eq "Fail" 108 226 334
1293 226 ▶ $r=+1,last if $qual eq SPF_Pass and $_->[0] == \&_mech_all;
Branch: 1 branch
BranchTrueFalseTotal
if $qual eq "Pass" and $_->[0] == \&Mail::SPF::Iterator::_mech_all 6 220 226
Condition100%3/3
$qual eq "Pass" and $_->[0] == \&Mail::SPF::Iterator::_mech_all
!ll&&!rl&&r
202006
MC/DC100%2/2
$qual eq "Pass" and $_->[0] == \&Mail::SPF::Iterator::_mech_all
$qual eq "Pass" $_->[0] == \&Mail::SPF::Iterator::_mech_all
Truth table100%3/3
A and B
A= $qual eq "Pass"B= $_->[0] == \&Mail::SPF::Iterator::_mech_all
ABresult
0X0
100
111
1294 }
1295 256 ▶ if ($r == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r == 1) 6 250 256
1296 # looks like a pass all rule
1297 $self->{result} = [
1298 6 $self->{opt}{pass_all}, "",
1299 { problem => "record designed to allow every sender" }
1300 ];
1301 6 _update_result_info($self);
1302 }
1303 }
1304 772 $self->{mech} = \@mech;
1305 772 $self->{explain} = $explain;
1306 772 $self->{redirect} = $redirect;
1307 772 return 1;
1308 }
1309  
1310 ############################################################################
1311 # handles mechanism 'all'
1312 # matches all time
1313 ############################################################################
1314 sub _mech_all {
1315 302 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _mech_all: called CC=2 SCAR=7.0
1316 302 my $qual = shift;
1317 302 ▶ $DEBUG && DEBUG( "match mech all with qual=$qual" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("match mech all with qual=$qual") 302 0 302
1318 302 return ( $qual,'matches default', { mechanism => 'all' });
1319 }
1320  
1321 ############################################################################
1322 # handle mechanism 'ip4'
1323 # matches if clients IP4 address is in ip/mask
1324 ############################################################################
1325 sub _mech_ip4 {
1326 162 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _mech_ip4: called CC=5 SCAR=16.4
1327 162 my ($qual,$ip,$plen) = @_;
1328 162 ▶ defined $self->{clientip4} or return (); # ignore rule, no IP4 address
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"clientip4"} or return () 0 162 162
1329 162 ▶ if ( ($self->{clientip4} & $mask4[$plen]) eq ($ip & $mask4[$plen]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"clientip4"} & $mask4[$plen]) eq ($ip & $mask4[$plen])) 46 116 162
1330 # rules matches
1331 46 ▶ $DEBUG && DEBUG( "match mech ip4:".inet_ntoa($ip)."/$plen with qual=$qual" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG('match mech ip4:' . inet_ntoa($ip) . "/$plen with qual=$qual") 46 0 46
1332 46 return ($qual,"matches ip4:".inet_ntoa($ip)."/$plen",
1333 { mechanism => 'ip4' } )
1334 }
1335 116 ▶ $DEBUG && DEBUG( "no match mech ip4:".inet_ntoa($ip)."/$plen" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG('no match mech ip4:' . inet_ntoa($ip) . "/$plen") 116 0 116
1336 116 return (); # ignore, no match
1337 }
1338  
1339 ############################################################################
1340 # handle mechanism 'ip6'
1341 # matches if clients IP6 address is in ip/mask
1342 ############################################################################
1343 sub _mech_ip6 {
1344 0 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _mech_ip6: not called CC=5 SCAR=34.0
1345 0 my ($qual,$ip,$plen) = @_;
1346 0 ▶ defined $self->{clientip6} or return (); # ignore rule, no IP6 address
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"clientip6"} or return () 0 0 0
1347 0 ▶ if ( ($self->{clientip6} & $mask6[$plen]) eq ($ip & $mask6[$plen])) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"clientip6"} & $mask6[$plen]) eq ($ip & $mask6[$plen])) 0 0 0
1348 # rules matches
1349 0 ▶ $DEBUG && DEBUG( "match mech ip6:".inet_ntop(AF_INET6,$ip)."/$plen with qual=$qual" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG('match mech ip6:' . inet_ntop(10, $ip) . "/$plen with qual=$qual") 0 0 0
1350 0 return ($qual,"matches ip6:".inet_ntop(AF_INET6,$ip)."/$plen",
1351 { mechanism => 'ip6' } )
1352 }
1353 0 ▶ $DEBUG && DEBUG( "no match ip6:".inet_ntop(AF_INET6,$ip)."/$plen" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG('no match ip6:' . inet_ntop(10, $ip) . "/$plen") 0 0 0
1354 0 return (); # ignore, no match
1355 }
1356  
1357 ############################################################################
1358 # handle mechanism 'a'
1359 # check if one of the A/AAAA records for $domain resolves to
1360 # clientip/plen,
1361 ############################################################################
1362 sub _mech_a {
1363 266 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _mech_a: called CC=7 SCAR=19.8
1364 266 my ($qual,$domain,$plen) = @_;
1365 266 ▶ $domain = $domain->{expanded} if ref $domain;
Branch: 1 branch
BranchTrueFalseTotal
if ref $domain 0 266 266
1366 266 ▶ $DEBUG && DEBUG( "check mech a:$domain/$plen with qual=$qual" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("check mech a:$domain/$plen with qual=$qual") 266 0 266
1367 266 ▶ if ( my @err = _check_domain($domain, "a:$domain/$plen")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@err) = _check_domain($domain, "a:$domain/$plen")) 6 260 266
1368 # spec is not clear here:
1369 # variante1: no match on invalid domain name -> return
1370 # variante2: propagate err -> return @err
1371 # we use variante2 for now
1372 6 ▶ $DEBUG && DEBUG( "no match mech a:$domain/$plen - @err" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech a:$domain/$plen - @err") 6 0 6
1373 6 return @err;
1374 }
1375  
1376 return ( SPF_PermError, "",
1377 { problem => "Number of DNS mechanism exceeded" })
1378 260 ▶ if --$self->{limit_dns_mech} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$self->{"limit_dns_mech"} < 0 8 252 260
1379  
1380 252 ▶ my $typ = $self->{clientip4} ? 'A':'AAAA';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'clientip4'} ? : 252 0 252
1381 252 $self->{cb} = [ \&_got_A, $qual,$plen,[ $domain ],'a' ];
1382 252 return scalar(Net::DNS::Packet->new( $domain, $typ,'IN' ));
1383 }
1384  
1385 ############################################################################
1386 # this is used in _mech_a and in _mech_mx if the address for an MX is not
1387 # sent inside the additional data
1388 # in the case of MX $names might contain more than one name to resolve, it
1389 # will try to resolve names to addresses and to match them until @$names
1390 # is empty
1391 ############################################################################
1392 sub _got_A {
1393 299 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _got_A: called CC=6 SCAR=19.9
1394 299 my ($qtype,$rcode,$ans,$add,$qual,$plen,$names,$mech) = @_;
1395 299 my $domain = shift(@$names);
1396  
1397 299 ▶ $DEBUG && DEBUG( "got response to $qtype for $domain: $rcode" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("got response to $qtype for ${domain}: $rcode") 299 0 299
1398 299 ▶ if ( $rcode eq 'NXDOMAIN' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rcode eq 'NXDOMAIN') { } 48 251 299
elsif ($rcode ne 'NOERROR') { } 0 251 251
1399 48 ▶ $DEBUG && DEBUG( "no match mech a:$domain/$plen - $rcode" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech a:$domain/$plen - $rcode") 48 0 48
1400 # no records found
1401 } elsif ( $rcode ne 'NOERROR' ) {
1402 0 ▶ $DEBUG && DEBUG( "temperror mech a:$domain/$plen - $rcode" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("temperror mech a:$domain/$plen - $rcode") 0 0 0
1403 0 return ( SPF_TempError,
1404 "getting $qtype for $domain",
1405 { problem => "error resolving $domain" }
1406 );
1407 }
1408  
1409 299 my @addr = map { $_->address } @$ans;
1410 299 return _check_A_match($self,$qual,$domain,$plen,\@addr,$names,$mech);
1411 }
1412  
1413 sub _check_A_match {
1414 365 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _check_A_match: called CC=19 SCAR=40.6
1415 365 my ($qual,$domain,$plen,$addr,$names,$mech) = @_;
1416  
1417 # process all found addresses
1418 365 ▶ if ( $self->{clientip4} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'clientip4'}) { } 365 0 365
1419 365 ▶ $plen = 32 if ! defined $plen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plen 0 365 365
1420 365 my $mask = $mask4[$plen];
1421 365 for my $addr (@$addr) {
1422 275 ▶ $DEBUG && DEBUG( "check a:$domain($addr)/$plen for mech $mech" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("check a:$domain($addr)/$plen for mech $mech") 275 0 275
1423 275 ▶ my $packed = $addr=~m{^[\d.]+$} && eval { inet_aton($addr) }
Branch: 1 branch
BranchTrueFalseTotal
my $packed = $addr =~ /^[\d.]+$/ && eval { do { inet_aton($addr) } } or return "TempError", "getting A for $domain", {"problem", "bad address in A record"} 0 275 275
Condition33%1/3
$addr =~ /^[\d.]+$/ && eval { do { inet_aton($addr) } }
!ll&&!rl&&r
00275
MC/DC0%0/2
$addr =~ /^[\d.]+$/ && eval { do { inet_aton($addr) } }
$addr =~ /^[\d.]+$/ eval { do { inet_aton($addr) } }
Truth table33%1/3
A && B
A= $addr =~ /^[\d.]+$/B= eval { do { inet_aton($addr) } }
ABresult
0X0
100
111
1424 or return ( SPF_TempError,
1425 "getting A for $domain",
1426 { problem => "bad address in A record" }
1427 );
1428  
1429 275 ▶ if ( ($packed & $mask) eq ($self->{clientip4} & $mask) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($packed & $mask) eq ($self->{"clientip4"} & $mask)) 87 188 275
1430 # match!
1431 87 ▶ $DEBUG && DEBUG( "match mech a:.../$plen for mech $mech with qual $qual" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("match mech a:.../$plen for mech $mech with qual $qual") 87 0 87
1432 87 return ($qual,"matches domain: $domain/$plen with IP4 $addr",
1433 { mechanism => $mech })
1434 }
1435 }
1436 } else { # AAAA
1437 0 ▶ $plen = 128 if ! defined $plen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plen 0 0 0
1438 0 my $mask = $mask6[$plen];
1439 0 for my $addr (@$addr) {
1440 0 ▶ $DEBUG && DEBUG( "check a:$domain($addr)//$plen for mech $mech" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("check a:$domain($addr)//$plen for mech $mech") 0 0 0
1441 0 ▶ my $packed = eval { inet_pton(AF_INET6,$addr) }
Branch: 1 branch
BranchTrueFalseTotal
my $packed = eval { do { inet_pton(10, $addr) } } or return "TempError", "getting AAAA for $domain", {"problem", "bad address in AAAA record"} 0 0 0
1442 or return ( SPF_TempError,
1443 "getting AAAA for $domain",
1444 { problem => "bad address in AAAA record" }
1445 );
1446 0 ▶ if ( ($packed & $mask) eq ($self->{clientip6} & $mask) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($packed & $mask) eq ($self->{"clientip6"} & $mask)) 0 0 0
1447 # match!
1448 0 ▶ $DEBUG && DEBUG( "match mech a:...//$plen for mech $mech with qual $qual" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("match mech a:...//$plen for mech $mech with qual $qual") 0 0 0
1449 0 return ($qual,"matches domain: $domain//$plen with IP6 $addr",
1450 { mechanism => $mech })
1451 }
1452 }
1453 }
1454  
1455 # no match yet, can we resolve another name?
1456 278 ▶ if ( @$names ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$names) 51 227 278
1457 51 ▶ my $typ = $self->{clientip4} ? 'A':'AAAA';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'clientip4'} ? : 51 0 51
1458 51 ▶ $DEBUG && DEBUG( "check mech a:$names->[0]/$plen for mech $mech with qual $qual" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("check mech a:$names->[0]/$plen for mech $mech with qual $qual") 51 0 51
1459 51 $self->{cb} = [ \&_got_A, $qual,$plen,$names,$mech ];
1460 51 return scalar(Net::DNS::Packet->new( $names->[0], $typ,'IN' ));
1461 }
1462  
1463 # finally no match
1464 227 ▶ $DEBUG && DEBUG( "no match mech $mech:$domain/$plen" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech ${mech}:$domain/$plen") 227 0 227
1465 227 return;
1466 }
1467  
1468  
1469  
1470 ############################################################################
1471 # handle mechanism 'mx'
1472 # similar to mech 'a', we expect the A/AAAA records for the MX in the
1473 # additional section of the DNS response
1474 ############################################################################
1475 sub _mech_mx {
1476 110 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _mech_mx: called CC=6 SCAR=21.6
1477 110 my ($qual,$domain,$plen) = @_;
1478 110 ▶ $domain = $domain->{expanded} if ref $domain;
Branch: 1 branch
BranchTrueFalseTotal
if ref $domain 0 110 110
1479 110 ▶ if ( my @err = _check_domain($domain,
Branch: 2 branches
BranchTrueFalseTotal
if (my(@err) = _check_domain($domain, "mx:$domain" . (defined $plen ? "/$plen" : ""))) 0 110 110
defined $plen ? : 110 0 110
1480 "mx:$domain".( defined $plen ? "/$plen":"" ))) {
1481 0 ▶ $DEBUG && DEBUG( "no mech mx:$domain/$plen - @err" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no mech mx:$domain/$plen - @err") 0 0 0
1482 return @err
1483 0 }
1484  
1485 return ( SPF_PermError, "",
1486 { problem => "Number of DNS mechanism exceeded" })
1487 110 ▶ if --$self->{limit_dns_mech} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$self->{"limit_dns_mech"} < 0 0 110 110
1488  
1489 110 $self->{cb} = [ \&_got_MX,$qual,$domain,$plen ];
1490 110 return scalar(Net::DNS::Packet->new( $domain, 'MX','IN' ));
1491 }
1492  
1493 sub _got_MX {
1494 106 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _got_MX: called CC=16 SCAR=29.6
1495 106 my ($qtype,$rcode,$ans,$add,$qual,$domain,$plen) = @_;
1496  
1497 106 ▶ if ( $rcode eq 'NXDOMAIN' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rcode eq 'NXDOMAIN') { } 0 106 106
elsif ($rcode ne 'NOERROR') { } 0 106 106
elsif (not @$ans) { } 36 70 106
1498 0 ▶ $DEBUG && DEBUG( "no match mech mx:$domain/$plen - $rcode" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech mx:$domain/$plen - $rcode") 0 0 0
1499 # no records found
1500 } elsif ( $rcode ne 'NOERROR' ) {
1501 0 ▶ $DEBUG && DEBUG( "no match mech mx:$domain/$plen - $rcode" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech mx:$domain/$plen - $rcode") 0 0 0
1502 0 return ( SPF_TempError,
1503 "getting MX form $domain",
1504 { problem => "error resolving $domain" }
1505 );
1506 } elsif ( ! @$ans ) {
1507 36 ▶ $DEBUG && DEBUG( "no match mech mx:$domain/$plen - no MX records" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech mx:$domain/$plen - no MX records") 36 0 36
1508 36 return; # domain has no MX -> no match
1509 }
1510  
1511 # all MX, with best (lowest) preference first
1512 130 my @mx = map { $_->[0] }
1513 138 sort { $a->[1] <=> $b->[1] }
1514 70 map { [ $_->exchange, $_->preference ] }
1515 @$ans;
1516 70 my %mx = map { $_ => [] } @mx;
1517  
1518 70 ▶ if (!$self->{opt}{rfc4408}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"opt"}{"rfc4408"}) 52 18 70
1519 # RFC 4408 limited the number of MX to query to 10
1520 # RFC 7208 instead said that ALL returned MX should count
1521 # against the limit and the test suite suggest that this limit
1522 # should be enforced before even asking the MX
1523 return ( SPF_PermError, "",
1524 { problem => "Number of DNS mechanism exceeded" })
1525 52 ▶ if $self->{limit_dns_mech}-@mx < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"limit_dns_mech"} - @mx < 0 4 48 52
1526 }
1527  
1528 # try to find A|AAAA records in additional data
1529 66 ▶ my $atyp = $self->{clientip4} ? 'A':'AAAA';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'clientip4'} ? : 66 0 66
1530 66 for my $rr (@$add) {
1531 46 ▶ if ( $rr->type eq $atyp && exists $mx{$rr->name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type eq $atyp and exists $mx{$rr->name}) 40 6 46
Condition66%2/3
$rr->type eq $atyp and exists $mx{$rr->name}
!ll&&!rl&&r
6040
MC/DC50%1/2
$rr->type eq $atyp and exists $mx{$rr->name}
$rr->type eq $atyp exists $mx{$rr->name}
Truth table66%2/3
A and B
A= $rr->type eq $atypB= exists $mx{$rr->name}
ABresult
0X0
100
111
1532 40 push @{$mx{$rr->name}},$rr->address;
1533 }
1534 }
1535 $DEBUG && DEBUG( "found mx for $domain: ".join( " ",
1536 66 ▶ map { $mx{$_} ? "$_(".join(",",@{$mx{$_}}).")" : $_ } @mx ));
Branch: 2 branches
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("found mx for ${domain}: " . join(" ", map({$mx{$_} ? "$_(" . join(",", @{$mx{$_};}) . ')' : $_;} @mx))) 66 0 66
$mx{$_} ? : 86 0 86
1537  
1538 # remove from @mx where I've found addresses
1539 66 @mx = grep { ! @{$mx{$_}} } @mx;
1540 # limit the Rest to 10 records (rfc4408,10.1)
1541 66 ▶ splice(@mx,10) if @mx>10;
Branch: 1 branch
BranchTrueFalseTotal
if @mx > 10 1 65 66
1542  
1543 66 my @addr = map { @$_ } values %mx;
1544 66 return _check_A_match( $self,$qual,"(mx)".$domain,$plen,\@addr,\@mx,'mx');
1545 }
1546  
1547 ############################################################################
1548 # handle mechanis 'exists'
1549 # just check, if I get any A record for the domain (lookup for A even if
1550 # I use IP6 - this is RBL style)
1551 ############################################################################
1552 sub _mech_exists {
1553 20 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _mech_exists: called CC=5 SCAR=18.1
1554 20 my ($qual,$domain) = @_;
1555 20 ▶ $domain = $domain->{expanded} if ref $domain;
Branch: 1 branch
BranchTrueFalseTotal
if ref $domain 6 14 20
1556 20 ▶ if ( my @err = _check_domain($domain, "exists:$domain" )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@err) = _check_domain($domain, "exists:$domain")) 0 20 20
1557 0 ▶ $DEBUG && DEBUG( "no match mech exists:$domain - @err" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech exists:$domain - @err") 0 0 0
1558 return @err
1559 0 }
1560  
1561 return ( SPF_PermError, "",
1562 { problem => "Number of DNS mechanism exceeded" })
1563 20 ▶ if --$self->{limit_dns_mech} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$self->{"limit_dns_mech"} < 0 0 20 20
1564  
1565 20 $self->{cb} = [ \&_got_A_exists,$qual,$domain ];
1566 20 return scalar(Net::DNS::Packet->new( $domain, 'A','IN' ));
1567 }
1568  
1569 sub _got_A_exists {
1570 20 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _got_A_exists: called CC=6 SCAR=25.6
1571 20 my ($qtype,$rcode,$ans,$add,$qual,$domain) = @_;
1572  
1573 20 ▶ if ( $rcode ne 'NOERROR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rcode ne 'NOERROR') { } 0 20 20
elsif (not @$ans) { } 0 20 20
1574 0 ▶ $DEBUG && DEBUG( "no match mech exists:$domain - $rcode" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech exists:$domain - $rcode") 0 0 0
1575 0 return;
1576 } elsif ( ! @$ans ) {
1577 0 ▶ $DEBUG && DEBUG( "no match mech exists:$domain - no A records" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech exists:$domain - no A records") 0 0 0
1578 0 return;
1579 }
1580 20 ▶ $DEBUG && DEBUG( "match mech exists:$domain with qual $qual" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("match mech exists:$domain with qual $qual") 20 0 20
1581 20 return ($qual,"domain $domain exists", { mechanism => 'exists' } )
1582 }
1583  
1584  
1585  
1586 ############################################################################
1587 # PTR
1588 # this is the most complex and most expensive mechanism:
1589 # - first get domains from PTR records for IP (clientip4|clientip6)
1590 # - filter for domains which match $domain (because only these are interesting
1591 # for matching)
1592 # - then verify the domains, if they point back to the IP by doing A|AAAA
1593 # lookups until one domain can be validated
1594 ############################################################################
1595 sub _mech_ptr {
1596 34 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _mech_ptr: called CC=11 SCAR=34.8
1597 34 my ($qual,$domain) = @_;
1598 34 ▶ $domain = $domain->{expanded} if ref $domain;
Branch: 1 branch
BranchTrueFalseTotal
if ref $domain 0 34 34
1599 34 ▶ if ( my @err = _check_domain($domain, "ptr:$domain" )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@err) = _check_domain($domain, "ptr:$domain")) 0 34 34
1600 0 ▶ $DEBUG && DEBUG( "no match mech ptr:$domain - @err" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech ptr:$domain - @err") 0 0 0
1601 return @err
1602 0 }
1603  
1604 return ( SPF_PermError, "",
1605 { problem => "Number of DNS mechanism exceeded" })
1606 34 ▶ if --$self->{limit_dns_mech} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$self->{"limit_dns_mech"} < 0 0 34 34
1607  
1608 34 ▶ my $ip = $self->{clientip4} || $self->{clientip6};
Condition33%1/3
$self->{'clientip4'} || $self->{'clientip6'}
l!l&&r!l&&!r
3400
MC/DC0%0/2
$self->{'clientip4'} || $self->{'clientip6'}
$self->{'clientip4'} $self->{'clientip6'}
Truth table33%1/3
A || B
A= $self->{'clientip4'}B= $self->{'clientip6'}
ABresult
1X1
011
000
1609 34 ▶ if ( exists $self->{validated}{$ip}{$domain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"validated"}{$ip}{$domain}) 0 34 34
1610 # already checked
1611 0 ▶ if ( ! $self->{validated}{$ip}{$domain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'validated'}{$ip}{$domain}) { } 0 0 0
1612 # could not be validated
1613 0 ▶ $DEBUG && DEBUG( "no match mech ptr:$domain - cannot validate $ip/$domain" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech ptr:$domain - cannot validate $ip/$domain") 0 0 0
1614 0 return; # ignore
1615 } else {
1616 0 ▶ $DEBUG && DEBUG( "match mech ptr:$domain with qual $qual" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("match mech ptr:$domain with qual $qual") 0 0 0
1617 0 return ($qual,"$domain validated" );
1618 }
1619 }
1620  
1621 34 my $query;
1622 34 ▶ if ( $self->{clientip4} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'clientip4'}) { } 34 0 34
1623 $query = join( '.', reverse split( m/\./,
1624 34 inet_ntoa($self->{clientip4}) ))
1625 .'.in-addr.arpa'
1626 } else {
1627 $query = join( '.', split( //,
1628 0 reverse unpack("H*",$self->{clientip6}) ))
1629 .'.ip6.arpa';
1630 }
1631  
1632 34 $self->{cb} = [ \&_got_PTR,$qual,$query,$domain ];
1633 34 return scalar(Net::DNS::Packet->new( $query, 'PTR','IN' ));
1634 }
1635  
1636 sub _got_PTR {
1637 34 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _got_PTR: called CC=10 SCAR=26.9
1638 34 my ($qtype,$rcode,$ans,$add,$qual,$query,$domain) = @_;
1639  
1640 # ignore mech if it can not be validated
1641 34 ▶ $rcode eq 'NOERROR' or do {
Branch: 1 branch
BranchTrueFalseTotal
$rcode eq "NOERROR" or do { $Mail::SPF::Iterator::DEBUG and DEBUG("no match mech ptr:$domain - $rcode"); return } 8 26 34
1642 8 ▶ $DEBUG && DEBUG( "no match mech ptr:$domain - $rcode" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech ptr:$domain - $rcode") 8 0 8
1643 8 return;
1644 };
1645 26 ▶ my @names = map { $_->ptrdname } @$ans or do {
Branch: 1 branch
BranchTrueFalseTotal
my(@names) = map({$_->ptrdname;} @$ans) or do { $Mail::SPF::Iterator::DEBUG and DEBUG("no match mech ptr:$domain - no names in PTR lookup"); return } 0 26 26
1646 0 ▶ $DEBUG && DEBUG( "no match mech ptr:$domain - no names in PTR lookup" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech ptr:$domain - no names in PTR lookup") 0 0 0
1647 0 return;
1648 };
1649  
1650 # strip records, which do not end in $domain
1651 26 ▶ @names = grep { $_ eq $domain || m{\.\Q$domain\E$} } @names;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $domain or /\.\Q$domain\E$/ 102 20 122
1652 26 ▶ if ( ! @names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 0 26 26
1653 0 ▶ $DEBUG && DEBUG( "no match mech ptr:$domain - no names in PTR lookup match $domain" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech ptr:$domain - no names in PTR lookup match $domain") 0 0 0
1654 # return if no matches inside $domain
1655 0 return;
1656 }
1657  
1658 # limit to no more then 10 names (see RFC4408, 10.1)
1659 26 ▶ splice(@names,10) if @names>10;
Branch: 1 branch
BranchTrueFalseTotal
if @names > 10 0 26 26
1660  
1661 # validate the rest by looking up the IP and verifying it
1662 # with the original IP (clientip)
1663 26 ▶ my $typ = $self->{clientip4} ? 'A':'AAAA';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'clientip4'} ? : 26 0 26
1664  
1665 26 $self->{cb} = [ \&_got_A_ptr, $qual,\@names ];
1666 26 return scalar(Net::DNS::Packet->new( $names[0], $typ,'IN' ));
1667 }
1668  
1669 sub _got_A_ptr {
1670 26 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _got_A_ptr: called CC=14 SCAR=31.0
1671 26 my ($qtype,$rcode,$ans,$add,$qual,$names) = @_;
1672  
1673 26 ▶ for my $dummy ( $rcode eq 'NOERROR' ? (1):() ) {
Branch: 1 branch
BranchTrueFalseTotal
$rcode eq 'NOERROR' ? : 26 0 26
1674 26 ▶ @$ans or last; # no addr for domain? - try next
Branch: 1 branch
BranchTrueFalseTotal
@$ans or last 6 20 26
1675 20 my @addr = map { $_->address } @$ans;
1676  
1677 # check if @addr contains clientip
1678 20 my ($match,$ip);
1679 20 ▶ if ( $ip = $self->{clientip4} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip = $self->{'clientip4'}) { } 20 0 20
1680 20 for(@addr) {
1681 20 ▶ m{^[\d\.]+$} or next;
Branch: 1 branch
BranchTrueFalseTotal
/^[\d\.]+$/ or next 0 20 20
1682 20 ▶ eval { inet_aton($_) } eq $ip or next;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { inet_aton($_) } } eq $ip or next 0 20 20
1683 20 $match = 1;
1684 20 last;
1685 }
1686 } else {
1687 0 $ip = $self->{clientip6};
1688 0 for(@addr) {
1689 0 ▶ eval { inet_pton(AF_INET6,$_) } eq $ip or next;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { inet_pton(10, $_) } } eq $ip or next 0 0 0
1690 0 $match = 1;
1691 0 last;
1692 }
1693 }
1694  
1695 # cache verification status
1696 20 $self->{validated}{$ip}{$names->[0]} = $match;
1697  
1698 # return $qual if we have verified the ptr
1699 20 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 20 0 20
1700 20 ▶ $DEBUG && DEBUG( "match mech ptr:... with qual $qual" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("match mech ptr:... with qual $qual") 20 0 20
1701 20 return ( $qual,"verified clientip with ptr", { mechanism => 'ptr' })
1702 }
1703 }
1704  
1705 # try next
1706 6 shift @$names;
1707 6 ▶ @$names or do {
Branch: 1 branch
BranchTrueFalseTotal
@$names or do { $Mail::SPF::Iterator::DEBUG and DEBUG("no match mech ptr:... - no more names for clientip"); return } 6 0 6
1708 # no next
1709 6 ▶ $DEBUG && DEBUG( "no match mech ptr:... - no more names for clientip" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no match mech ptr:... - no more names for clientip") 6 0 6
1710 6 return;
1711 };
1712  
1713 # cb stays the same
1714 0 return scalar(Net::DNS::Packet->new( $names->[0], $qtype,'IN' ));
1715 }
1716  
1717  
1718 ############################################################################
1719 # mechanism include
1720 # include SPF from other domain, propagate errors and consider Pass
1721 # from this inner SPF as match for the include mechanism
1722 ############################################################################
1723 sub _mech_include {
1724 136 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _mech_include: called CC=6 SCAR=19.2
1725 136 my ($qual,$domain) = @_;
1726 136 ▶ $domain = $domain->{expanded} if ref $domain;
Branch: 1 branch
BranchTrueFalseTotal
if ref $domain 0 136 136
1727 136 ▶ if ( my @err = _check_domain($domain, "include:$domain" )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@err) = _check_domain($domain, "include:$domain")) 0 136 136
1728 0 ▶ $DEBUG && DEBUG( "failed mech include:$domain - @err" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("failed mech include:$domain - @err") 0 0 0
1729 return @err
1730 0 }
1731  
1732 136 ▶ $DEBUG && DEBUG( "mech include:$domain with qual=$qual" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("mech include:$domain with qual=$qual") 136 0 136
1733  
1734 return ( SPF_PermError, "",
1735 { problem => "Number of DNS mechanism exceeded" })
1736 136 ▶ if --$self->{limit_dns_mech} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$self->{"limit_dns_mech"} < 0 6 130 136
1737  
1738 # push and reset current domain and SPF record
1739 130 push @{$self->{include_stack}}, {
1740 domain => $self->{domain},
1741 mech => $self->{mech},
1742 explain => $self->{explain},
1743 redirect => $self->{redirect},
1744 130 qual => $qual,
1745 };
1746 130 $self->{domain} = $domain;
1747 130 $self->{mech} = [];
1748 130 $self->{explain} = undef;
1749 130 $self->{redirect} = undef;
1750  
1751 # start with new SPF record
1752 130 return $self->_query_txt_spf;
1753 }
1754  
1755  
1756 ############################################################################
1757 # create explain message from TXT record
1758 ############################################################################
1759 sub _got_TXT_exp {
1760 98 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _got_TXT_exp: called CC=14 SCAR=26.8
1761 98 my ($qtype,$rcode,$ans,$add) = @_;
1762 98 my $final = $self->{result};
1763  
1764 98 ▶ if ( $rcode ne 'NOERROR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rcode ne "NOERROR") 4 94 98
1765 4 ▶ $DEBUG && DEBUG( "DNS error for exp TXT lookup" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("DNS error for exp TXT lookup") 4 0 4
1766 # just return the final rv
1767 4 return @$final;
1768 }
1769  
1770 94 my ($txtdata,$t2) = grep { length } map { $_->txtdata } @$ans;;
1771 94 ▶ if ( $t2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t2) { } 10 84 94
elsif (not $txtdata) { } 6 78 84
1772 # only one record should be returned
1773 10 ▶ $DEBUG && DEBUG( "got more than one TXT -> error" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("got more than one TXT -> error") 10 0 10
1774 10 return @$final;
1775 } elsif ( ! $txtdata ) {
1776 6 ▶ $DEBUG && DEBUG( "no text in TXT for explain" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("no text in TXT for explain") 6 0 6
1777 6 return @$final;
1778 }
1779  
1780 78 ▶ $DEBUG && DEBUG( "got TXT $txtdata" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("got TXT $txtdata") 78 0 78
1781  
1782 # valid TXT record found -> expand macros
1783 78 my $exp = eval { $self->_macro_expand( $txtdata,'exp' ) };
1784 78 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 6 72 78
1785 6 ▶ $DEBUG && DEBUG( "macro expansion of '$txtdata' failed: $@" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG("macro expansion of '${txtdata}' failed: $@") 6 0 6
1786 6 return @$final;
1787 }
1788  
1789 # explain
1790 72 ▶ if (ref $exp) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exp) 12 60 72
1791 12 ▶ if ( my @xrv = $self->_resolve_macro_p($exp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@xrv) = $self->_resolve_macro_p($exp)) 12 0 12
1792 # we need to do more DNS lookups for resolving %{p} macros
1793 12 ▶ $DEBUG && DEBUG( "need to resolve %{p} in $exp->{macro}" );
Branch: 1 branch
BranchTrueFalseTotal
$Mail::SPF::Iterator::DEBUG and DEBUG(qq[need to resolve %{p} in $exp->{"macro"}]) 12 0 12
1794 12 $final->[4] = $exp;
1795 12 return @xrv;
1796 }
1797 0 $exp = $exp->{expanded};
1798 }
1799  
1800 # result should be limited to US-ASCII!
1801 # further limit to printable chars
1802 60 ▶ $final->[3] = $exp if $exp !~m{[\x00-\x1f\x7e-\xff]};
Branch: 1 branch
BranchTrueFalseTotal
unless $exp =~ /[\x00-\x1f\x7e-\xff]/ 56 4 60
1803  
1804 60 return @$final;
1805 }
1806  
1807 ############################################################################
1808 # expand Macros
1809 ############################################################################
1810 sub _macro_expand {
1811 698 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _macro_expand: called CC=41 SCAR=38.7
1812 698 my ($domain,$explain) = @_;
1813 698 my $new_domain = '';
1814 698 ▶ my $mchars = $explain ? qr{[slodipvhcrt]}i : qr{[slodipvh]}i;
Branch: 1 branch
BranchTrueFalseTotal
$explain ? : 78 620 698
1815 698 my $need_validated;
1816 #DEBUG( Carp::longmess("no domain" )) if ! $domain;
1817 #DEBUG( "domain=$domain" );
1818 698 ▶ while ( $domain =~ m{\G (?:
Branch: 1 branch
BranchTrueFalseTotal
if ($domain =~ m[\G (?: ([^%]+) | # text %(?: ([%_\-]) | # char: %_, %-, %% { # macro: l1r+- -> (l)(1)(r)(+-) ($mchars) (\d*)(r?) ([.\-+,/_=]*) } | (.|$) # bad char ))]gx) 1028 672 1700
1819 ([^%]+) | # text
1820 %(?:
1821 ([%_\-]) | # char: %_, %-, %%
1822 {
1823 # macro: l1r+- -> (l)(1)(r)(+-)
1824 ($mchars) (\d*)(r?) ([.\-+,/_=]*)
1825 } |
1826 (.|$) # bad char
1827 ))}xg ) {
1828 1028 my ($text,$char,$macro,$macro_n,$macro_r,$macro_delim,$bad)
1829 = ($1,$2,$3,$4,$5,$6,$7);
1830  
1831 1028 ▶ if ( defined $text ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $text) { } 746 282 1028
elsif (defined $char) { } 24 258 282
elsif ($macro) { } 232 26 258
1832 746 $new_domain .= $text;
1833  
1834 } elsif ( defined $char ) {
1835 24 ▶ $new_domain .=
Branch: 2 branches
BranchTrueFalseTotal
$char eq '%' ? : 6 18 24
$char eq '_' ? : 12 6 18
1836 $char eq '%' ? '%' :
1837 $char eq '_' ? ' ' :
1838 '%20'
1839  
1840 } elsif ( $macro ) {
1841 232 ▶ $macro_delim ||= '.';
Condition100%2/2
$macro_delim ||= "."
l!l
10222
MC/DC100%1/1
$macro_delim ||= "."
$macro_delim
Truth table100%2/2
A
A= $macro_delim
Aresult
11
00
1842 232 my $imacro = lc($macro);
1843 my $expand =
1844 $imacro eq 's' ? $self->{sender} :
1845 $imacro eq 'l' ? $self->{sender} =~m{^([^@]+)\@}
1846 ? $1 : 'postmaster' :
1847 $imacro eq 'o' ? $self->{sender} =~m{\@(.*)}
1848 ? $1 : $self->{sender} :
1849 $imacro eq 'd' ? $self->{domain} :
1850 $imacro eq 'i' ? $self->{clientip4} ?
1851 inet_ntoa($self->{clientip4}) :
1852 0 ▶ join('.',map { uc } split(//,
Branch: 8 branches
BranchTrueFalseTotal
$imacro eq 'v' ? : 6 104 110
$self->{'clientip4'} ? : 6 0 6
$imacro eq 'h' ? : 30 74 104
$imacro eq 'c' ? : 12 62 74
$self->{'clientip4'} ? : 12 0 12
$imacro eq 'r' ? : 0 62 62
$imacro eq 't' ? : 0 62 62
$imacro eq 'p' ? : 62 0 62
Condition0%0/2
$self->{"myname"} or "unknown"
l!l
00
MC/DC0%0/1
$self->{"myname"} or "unknown"
$self->{"myname"}
Truth table0%0/2
A
A= $self->{"myname"}
Aresult
11
00
1853 unpack( "H*",$self->{clientip6}))) :
1854 $imacro eq 'v' ? $self->{clientip4} ? 'in-addr' : 'ip6':
1855 $imacro eq 'h' ? $self->{helo} :
1856 $imacro eq 'c' ? $self->{clientip4} ?
1857 inet_ntoa($self->{clientip4}) :
1858 inet_ntop(AF_INET6,$self->{clientip6}) :
1859 $imacro eq 'r' ? $self->{myname} || 'unknown' :
1860 $imacro eq 't' ? time() :
1861 232 ▶ $imacro eq 'p' ? do {
Branch: 8 branches
BranchTrueFalseTotal
$imacro eq 's' ? : 0 232 232
$imacro eq 'l' ? : 28 204 232
$self->{'sender'} =~ /^([^\@]+)\@/ ? : 22 6 28
$imacro eq 'o' ? : 48 156 204
$self->{'sender'} =~ /\@(.*)/ ? : 48 0 48
$imacro eq 'd' ? : 28 128 156
$imacro eq 'i' ? : 18 110 128
$self->{'clientip4'} ? : 18 0 18
1862 62 ▶ my $ip = $self->{clientip4} || $self->{clientip6};
Condition33%1/3
$self->{'clientip4'} || $self->{'clientip6'}
l!l&&r!l&&!r
6200
MC/DC0%0/2
$self->{'clientip4'} || $self->{'clientip6'}
$self->{'clientip4'} $self->{'clientip6'}
Truth table33%1/3
A || B
A= $self->{'clientip4'}B= $self->{'clientip6'}
ABresult
1X1
011
000
1863 62 my $v = $self->{validated}{$ip};
1864 62 my $d = $self->{domain};
1865 62 ▶ if ( ! $v ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $v) { } 46 16 62
elsif ($v->{$d}) { } 0 16 16
elsif (my(@xd) = grep({$v->{$_};} keys %$v)) { } 16 0 16
1866 # nothing validated pointing to IP
1867 46 $need_validated = { ip => $ip, domain => $d };
1868 46 'unknown'
1869 } elsif ( $v->{$d} ) {
1870 # <domain> itself is validated
1871 0 $d;
1872 16 } elsif ( my @xd = grep { $v->{$_} } keys %$v ) {
1873 16 ▶ if ( my @sd = grep { m{\.\Q$d\E$} } @xd ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@sd) = grep({/\.\Q$d\E$/;} @xd)) { } 6 10 16
1874 # subdomain if <domain> is validated
1875 6 $sd[0]
1876 } else {
1877 # any other domain pointing to IP
1878 10 $xd[0]
1879 }
1880 } else {
1881 0 'unknown'
1882 }
1883 } :
1884 die "unknown macro $macro\n";
1885  
1886 232 my $rx = eval "qr{[$macro_delim]}";
1887 232 my @parts = split( $rx, $expand );
1888 232 ▶ @parts = reverse @parts if $macro_r;
Branch: 1 branch
BranchTrueFalseTotal
if $macro_r 28 204 232
1889 232 ▶ if ( length $macro_n ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $macro_n) 38 194 232
1890 38 ▶ die "bad macro definition '$domain'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $macro_n 0 38 38
1891 if ! $macro_n; # must be != 0
1892 38 ▶ @parts = splice( @parts,-$macro_n ) if @parts>$macro_n;
Branch: 1 branch
BranchTrueFalseTotal
if @parts > $macro_n 34 4 38
1893 }
1894 232 ▶ if ( $imacro ne $macro ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imacro ne $macro) 36 196 232
1895 # upper case - URI escape
1896 36 @parts = map { uri_escape($_) } @parts;
1897 }
1898 232 $new_domain .= join('.',@parts);
1899  
1900 } else {
1901 26 die "bad macro definition '$domain'\n";
1902 }
1903 }
1904  
1905 672 ▶ if ( ! $explain ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($explain) 600 72 672
1906 # should be less than 253 bytes
1907 600 ▶ while ( length($new_domain)>253 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $new_domain > 253) 6 600 606
1908 6 ▶ $new_domain =~s{^[^.]*\.}{} or last;
Branch: 1 branch
BranchTrueFalseTotal
$new_domain =~ s/^[^.]*\.// or last 0 6 6
1909 }
1910 600 ▶ $new_domain = '' if length($new_domain)>253;
Branch: 1 branch
BranchTrueFalseTotal
if length $new_domain > 253 0 600 600
1911 }
1912  
1913 672 ▶ if ( $need_validated ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_validated) { } 46 626 672
1914 46 return { expanded => $new_domain, %$need_validated, macro => $domain }
1915 } else {
1916 626 return $new_domain;
1917 }
1918 }
1919  
1920 ############################################################################
1921 # resolve macro %{p}, e.g. find validated domain name for IP and replace
1922 # %{p} with it. This has many thing similar with the ptr: method
1923 ############################################################################
1924 sub _resolve_macro_p {
1925 22 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _resolve_macro_p: called CC=6 SCAR=19.7
1926 22 my $rec = shift;
1927 22 ▶ my $ip = ref($rec) && $rec->{ip} or return; # nothing to resolve
Branch: 1 branch
BranchTrueFalseTotal
my $ip = ref $rec && $rec->{"ip"} or return 2 20 22
Condition66%2/3
ref $rec && $rec->{'ip'}
!ll&&!rl&&r
0220
MC/DC50%1/2
ref $rec && $rec->{'ip'}
ref $rec $rec->{'ip'}
Truth table66%2/3
A && B
A= ref $recB= $rec->{'ip'}
ABresult
0X0
100
111
1928  
1929 # could it already be resolved w/o further lookups?
1930 20 my $d = eval { $self->_macro_expand( $rec->{macro} ) };
1931 20 ▶ if ( ! ref $d ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $d) 0 20 20
1932 0 ▶ %$rec = ( expanded => $d ) if ! $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
1933 0 return;
1934 }
1935  
1936 20 my $query;
1937 20 ▶ if ( length($ip) == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ip == 4) { } 20 0 20
1938 20 $query = join( '.', reverse split( m/\./,
1939 inet_ntoa($ip) )) .'.in-addr.arpa'
1940 } else {
1941 0 $query = join( '.', split( //,
1942 reverse unpack("H*",$ip) )) .'.ip6.arpa';
1943 }
1944  
1945 20 $self->{cb} = [ \&_validate_got_PTR, $rec ];
1946 20 return scalar(Net::DNS::Packet->new( $query, 'PTR','IN' ));
1947 }
1948  
1949 sub _validate_got_PTR {
1950 20 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _validate_got_PTR: called CC=6 SCAR=18.4
1951 20 my ($qtype,$rcode,$ans,$add,$rec ) = @_;
1952  
1953 # no validation possible if no records
1954 20 ▶ return if $rcode ne 'NOERROR' or ! @$ans;
Branch: 1 branch
BranchTrueFalseTotal
if $rcode ne "NOERROR" or not @$ans 0 20 20
Condition33%1/3
$rcode ne "NOERROR" or not @$ans
l!l&&r!l&&!r
0020
MC/DC0%0/2
$rcode ne "NOERROR" or not @$ans
$rcode ne "NOERROR" not @$ans
Truth table33%1/3
A or not(B)
A= $rcode ne "NOERROR"B= not @$ans
ABresult
1X1
011
000
1955  
1956 20 my @names = map { lc($_->ptrdname) } @$ans;
1957  
1958 # prefer records, which are $domain or end in $domain
1959 20 ▶ if ( my $domain = $rec->{domain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $domain = $rec->{"domain"}) 20 0 20
1960 20 unshift @names, grep { $_ eq $domain } @names;
1961 20 unshift @names, grep { m{\.\Q$domain\E$} } @names;
1962 20 { my %n; @names = grep { !$n{$_}++ } @names } # uniq
1963 }
1964  
1965 # limit to no more then 10 names (RFC4408, 10.1)
1966 20 ▶ splice(@names,10) if @names>10;
Branch: 1 branch
BranchTrueFalseTotal
if @names > 10 0 20 20
1967  
1968 # validate the rest by looking up the IP and verifying it
1969 # with the original IP (clientip)
1970 20 ▶ my $typ = length($rec->{ip}) == 4 ? 'A':'AAAA';
Branch: 1 branch
BranchTrueFalseTotal
length $rec->{'ip'} == 4 ? : 20 0 20
1971  
1972 20 $self->{cb} = [ \&_validate_got_A_ptr, $rec,\@names ];
1973 20 return scalar(Net::DNS::Packet->new( $names[0], $typ,'IN' ));
1974 }
1975  
1976 sub _validate_got_A_ptr {
1977 20 ▶ my Mail::SPF::Iterator $self = shift;
Subroutine _validate_got_A_ptr: called CC=12 SCAR=28.1
1978 20 my ($qtype,$rcode,$ans,$add,$rec,$names) = @_;
1979  
1980 20 ▶ if ( $rcode eq 'NOERROR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rcode eq "NOERROR") 20 0 20
1981 20 ▶ my @addr = map { $_->address } @$ans or do {
Branch: 1 branch
BranchTrueFalseTotal
my(@addr) = map({$_->address;} @$ans) or do { return } 0 20 20
1982 # no addr for domain? -> ignore - maybe
1983 # the domain only provides the other kind of records?
1984 0 return;
1985 };
1986  
1987 # check if @addr contains clientip
1988 20 my $match;
1989 20 my $ip = $rec->{ip};
1990 20 ▶ if ( length($ip) == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ip == 4) { } 20 0 20
1991 20 for(@addr) {
1992 26 ▶ m{^[\d\.]+$} or next;
Branch: 1 branch
BranchTrueFalseTotal
/^[\d\.]+$/ or next 0 26 26
1993 26 ▶ eval { inet_aton($_) } eq $ip or next;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { inet_aton($_) } } eq $ip or next 12 14 26
1994 14 $match = 1;
1995 14 last;
1996 }
1997 } else {
1998 0 for(@addr) {
1999 0 ▶ eval { inet_pton(AF_INET6,$_) } eq $ip or next;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { inet_pton(10, $_) } } eq $ip or next 0 0 0
2000 0 $match = 1;
2001 0 last;
2002 }
2003 }
2004  
2005 # cache verification status
2006 20 $self->{validated}{$ip}{$names->[0]} = $match;
2007  
2008 # expand macro if we have verified the ptr
2009 20 ▶ if ( $match ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 14 6 20
2010 14 ▶ if ( my $t = eval { $self->_macro_expand( $rec->{macro} ) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = eval { do { $self->_macro_expand($rec->{"macro"}) } }) 14 0 14
2011 14 %$rec = ( expanded => $t );
2012 }
2013 14 return;
2014 }
2015 }
2016  
2017 # try next
2018 6 shift @$names;
2019 6 ▶ @$names or return; # no next
Branch: 1 branch
BranchTrueFalseTotal
@$names or return 6 0 6
2020  
2021 # cb stays the same
2022 0 return scalar(Net::DNS::Packet->new( $names->[0], $qtype,'IN' ));
2023 }
2024  
2025  
2026 5 1;
↑ Summary