blib/lib/Mail/SPF/Iterator.pm

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