SPF/Server.pm

filter: Statementstmt 50.6% 84 / 166 Branchbran 30.0% 15 / 50 Conditioncond 13.3% 4 / 30 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 53.1% 25 / 47 Podpod 76.9% 10 / 13 totaltotal 42.4% 138 / 325 CC 38 SCAR 30.3
CC
38
Cov
42%
select_record
90.0
get_acceptable_records_from_packet
72.0
__ANON__
12.0
CRAP 20.8
« SPF/Mech/MX.pm ↑ Summary SPF/v2/Record.pm »
1 #
2 # Mail::SPF::Server
3 # Server class for processing SPF requests.
4 #
5 # (C) 2005-2012 Julian Mehnle <julian@mehnle.net>
6 # 2005 Shevek <cpan@anarres.org>
7 # $Id: Server.pm 61 2013-07-22 03:45:15Z julian $
8 #
9 ##############################################################################
10  
11 package Mail::SPF::Server;
12  
13 =head1 NAME
14  
15 Mail::SPF::Server - Server class for processing SPF requests
16  
17 =head1 VERSION
18  
19 version 3.20250505
20  
21 =cut
22  
23 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 3 ▶use base 'Mail::SPF::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 3 ▶use Error ':try';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use Net::DNS::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 3 ▶use Mail::SPF::MacroString;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 ▶use Mail::SPF::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use Mail::SPF::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 3 ▶use constant TRUE => (0 == 0);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use constant FALSE => not TRUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 3 use constant record_classes_by_version => {
39 1 => 'Mail::SPF::v1::Record',
40 2 => 'Mail::SPF::v2::Record'
41 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 3 ▶use constant result_base_class => 'Mail::SPF::Result';
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 3 ▶use constant query_rr_type_all => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use constant query_rr_type_txt => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 ▶use constant query_rr_type_spf => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 3 use constant default_default_authority_explanation =>
50 3 ▶ 'Rejected by SPF record.';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 7 ▶sub default_query_rr_types { shift->query_rr_type_txt };
Subroutine default_query_rr_types: called CC=1 SCAR=0.0
Pod undocumented
53  
54 3 ▶use constant default_max_dns_interactive_terms => 10; # RFC 4408, 10.1/6
Subroutine BEGIN: called CC=1 SCAR=0.0
55 3 ▶use constant default_max_name_lookups_per_term => 10; # RFC 4408, 10.1/7
Subroutine BEGIN: called CC=1 SCAR=0.0
56 6 ▶sub default_max_name_lookups_per_mx_mech { shift->max_name_lookups_per_term };
Subroutine default_max_name_lookups_per_mx_mech: called CC=1 SCAR=0.0
Pod undocumented
57 7 ▶sub default_max_name_lookups_per_ptr_mech { shift->max_name_lookups_per_term };
Subroutine default_max_name_lookups_per_ptr_mech: called CC=1 SCAR=0.0
Pod undocumented
58  
59 3 ▶use constant default_max_void_dns_lookups => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 # Interface:
62 ##############################################################################
63  
64 =head1 SYNOPSIS
65  
66 use Mail::SPF;
67  
68 my $spf_server = Mail::SPF::Server->new(
69 # Optional custom default for authority explanation:
70 default_authority_explanation =>
71 'See http://www.%{d}/why/id=%{S};ip=%{I};r=%{R}'
72 );
73  
74 my $result = $spf_server->process($request);
75  
76 =cut
77  
78 # Implementation:
79 ##############################################################################
80  
81 =head1 DESCRIPTION
82  
83 B<Mail::SPF::Server> is a server class for processing SPF requests. Each
84 server instance can be configured with specific processing parameters. Also,
85 the default I<Net::DNS::Resolver> DNS resolver used for making DNS look-ups can
86 be overridden with a custom resolver object.
87  
88 =head2 Constructor
89  
90 The following constructor is provided:
91  
92 =over
93  
94 =item B<new(%options)>: returns I<Mail::SPF::Server>
95  
96 Creates a new server object for processing SPF requests.
97  
98 %options is a list of key/value pairs representing any of the following
99 options:
100  
101 =over
102  
103 =item B<default_authority_explanation>
104  
105 A I<string> denoting the default (not macro-expanded) authority explanation
106 string to use if the authority domain does not specify an explanation string of
107 its own. Defaults to:
108  
109 'Rejected by SPF record.'
110  
111 As can be seen from the default, a non-standard C<_scope> pseudo macro is
112 supported that expands to the name of the identity's scope. (Note: Do I<not>
113 use any non-standard macros in explanation strings published in DNS.)
114  
115 =item B<hostname>
116  
117 A I<string> denoting the local system's fully qualified host name that should
118 be used for expanding the C<r> macro in explanation strings. Defaults to the
119 system's configured host name.
120  
121 =item B<dns_resolver>
122  
123 An optional DNS resolver object. If none is specified, a new I<Net::DNS::Resolver>
124 object is used. The resolver object may be of a different class, but it must
125 provide an interface similar to I<Net::DNS::Resolver> -- at least the C<send>
126 and C<errorstring> methods must be supported, and the C<send> method must
127 return either an object of class I<Net::DNS::Packet>, or, in the case of an
128 error, B<undef>.
129  
130 =item B<query_rr_types>
131  
132 For which RR types to query when looking up and selecting SPF records. The
133 following values are supported:
134  
135 =over
136  
137 =item B<< Mail::SPF::Server->query_rr_type_all >>
138  
139 Both C<TXT> and C<SPF> type RRs.
140  
141 =item B<< Mail::SPF::Server->query_rr_type_txt >> (default)
142  
143 C<TXT> type RRs only.
144  
145 =item B<< Mail::SPF::Server->query_rr_type_spf >>
146  
147 C<SPF> type RRs only.
148  
149 =back
150  
151 For years B<Mail::SPF> has defaulted to looking up both C<SPF> and C<TXT> type
152 RRs as recommended by RFC 4408. Experience has shown, however, that a
153 significant portion of name servers suffer from serious brain damage with
154 regard to the handling of queries for RR types that are unknown to them, such
155 as the C<SPF> RR type. Consequently B<Mail::SPF> now defaults to looking up
156 only C<TXT> type RRs. This may be overridden by setting the B<query_rr_types>
157 option.
158  
159 See RFC 4408, 3.1.1, for a discussion of the topic, as well as the description
160 of the L</select_record> method.
161  
162 =item B<max_dns_interactive_terms>
163  
164 An I<integer> denoting the maximum number of terms (mechanisms and modifiers)
165 per SPF check that perform DNS look-ups, as defined in RFC 4408, 10.1,
166 paragraph 6. If B<undef> is specified, there is no limit on the number of such
167 terms. Defaults to B<10>, which is the value defined in RFC 4408.
168  
169 A value above the default is I<strongly discouraged> for security reasons. A
170 value below the default has implications with regard to the predictability of
171 SPF results. Only deviate from the default if you know what you are doing!
172  
173 =item B<max_name_lookups_per_term>
174  
175 An I<integer> denoting the maximum number of DNS name look-ups per term
176 (mechanism or modifier), as defined in RFC 4408, 10.1, paragraph 7. If
177 B<undef> is specified, there is no limit on the number of look-ups performed.
178 Defaults to B<10>, which is the value defined in RFC 4408.
179  
180 A value above the default is I<strongly discouraged> for security reasons. A
181 value below the default has implications with regard to the predictability of
182 SPF results. Only deviate from the default if you know what you are doing!
183  
184 =item B<max_name_lookups_per_mx_mech>
185  
186 =item B<max_name_lookups_per_ptr_mech>
187  
188 An I<integer> denoting the maximum number of DNS name look-ups per B<mx> or B<ptr>
189 mechanism, respectively. Defaults to the value of the C<max_name_lookups_per_term>
190 option. See there for additional information and security notes.
191  
192 =item B<max_void_dns_lookups>
193  
194 An I<integer> denoting the maximum number of "void" DNS look-ups per SPF check,
195 i.e. the number of DNS look-ups that were caused by DNS-interactive terms and
196 macros (as defined in RFC 4408, 10.1, paragraphs 6 and 7) and that are allowed
197 to return an empty answer with RCODE 0 or RCODE 3 (C<NXDOMAIN>) before
198 processing is aborted with a C<permerror> result. If B<undef> is specified,
199 there is no stricter limit on the number of void DNS look-ups beyond the usual
200 processing limits. Defaults to B<2>.
201  
202 Specifically, the DNS look-ups that are subject to this limit are those caused
203 by the C<a>, C<mx>, C<ptr>, and C<exists> mechanisms and the C<p> macro.
204  
205 A value of B<2> is likely to prevent effective DoS attacks against third-party
206 victim domains. However, a definite limit may cause C<permerror> results even
207 with certain (overly complex) innocent sender policies where useful results
208 would normally be returned.
209  
210 =back
211  
212 =cut
213  
214 sub new {
215 7 ▶ my ($self, %options) = @_;
Subroutine new: called CC=10 SCAR=24.0
Pod documented
216 7 $self = $self->SUPER::new(%options);
217  
218 # Do not cache a Mail::SPF::MacroString instance with
219 # context provided, this causes a circular reference that leaks
220 # both objects.
221 $self->{default_authority_explanation} = $self->default_default_authority_explanation
222 7 ▶ if not defined($self->{default_authority_explanation});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"default_authority_explanation"} 7 0 7
223  
224 7 ▶ $self->{hostname} ||= Mail::SPF::Util->hostname;
Condition33%1/3
$self->{"hostname"} ||= "Mail::SPF::Util"->hostname
l!l&&r!l&&!r
070
MC/DC0%0/2
$self->{"hostname"} ||= "Mail::SPF::Util"->hostname
$self->{"hostname"} "Mail::SPF::Util"->hostname
Truth table33%1/3
A ||= B
A= $self->{"hostname"}B= "Mail::SPF::Util"->hostname
ABresult
1X1
011
000
225  
226 7 ▶ $self->{dns_resolver} ||= Net::DNS::Resolver->new();
Condition66%2/3
$self->{"dns_resolver"} ||= "Net::DNS::Resolver"->new
l!l&&r!l&&!r
610
MC/DC0%0/2
$self->{"dns_resolver"} ||= "Net::DNS::Resolver"->new
$self->{"dns_resolver"} "Net::DNS::Resolver"->new
Truth table66%2/3
A ||= B
A= $self->{"dns_resolver"}B= "Net::DNS::Resolver"->new
ABresult
1X1
011
000
227  
228 $self->{query_rr_types} = $self->default_query_rr_types
229 7 ▶ if not defined($self->{query_rr_types});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"query_rr_types"} 7 0 7
230  
231 $self->{max_dns_interactive_terms} = $self->default_max_dns_interactive_terms
232 7 ▶ if not exists($self->{max_dns_interactive_terms});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"max_dns_interactive_terms"} 6 1 7
233 $self->{max_name_lookups_per_term} = $self->default_max_name_lookups_per_term
234 7 ▶ if not exists($self->{max_name_lookups_per_term});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"max_name_lookups_per_term"} 6 1 7
235 $self->{max_name_lookups_per_mx_mech} = $self->default_max_name_lookups_per_mx_mech
236 7 ▶ if not exists($self->{max_name_lookups_per_mx_mech});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"max_name_lookups_per_mx_mech"} 6 1 7
237 $self->{max_name_lookups_per_ptr_mech} = $self->default_max_name_lookups_per_ptr_mech
238 7 ▶ if not exists($self->{max_name_lookups_per_ptr_mech});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"max_name_lookups_per_ptr_mech"} 7 0 7
239  
240 $self->{max_void_dns_lookups} = $self->default_max_void_dns_lookups
241 7 ▶ if not exists($self->{max_void_dns_lookups});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"max_void_dns_lookups"} 7 0 7
242  
243 7 return $self;
244 }
245  
246 =back
247  
248 =head2 Class methods
249  
250 The following class methods are provided:
251  
252 =over
253  
254 =item B<result_class>: returns I<class>
255  
256 =item B<result_class($name)>: returns I<class>
257  
258 Returns a I<Mail::SPF::Result> descendent class determined from the given
259 result name via the server's inherent result base class, or returns the
260 server's inherent result base class if no result name is given. This method
261 may also be used as an instance method.
262  
263 I<Note>: Do not write code invoking class methods on I<literal> result class
264 names as this would ignore any derivative result classes provided by
265 B<Mail::SPF> extension modules.
266  
267 =cut
268  
269 sub result_class {
270 0 ▶ my ($self, $name) = @_;
Subroutine result_class: not called CC=2 SCAR=17.9
Pod documented
271 return
272 defined($name) ?
273 0 ▶ $self->result_base_class->result_classes->{$name}
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 0 0
274 : $self->result_base_class;
275 }
276  
277 =item B<throw_result($name, $request)>: throws I<Mail::SPF::Result>
278  
279 =item B<throw_result($name, $request, $text)>: throws I<Mail::SPF::Result>
280  
281 Throws a I<Mail::SPF::Result> descendant determined from the given result name
282 via the server's inherent result base class, passing an optional result text
283 and associating the given I<Mail::SPF::Request> object with the result object.
284 This method may also be used as an instance method.
285  
286 I<Note>: Do not write code invoking C<throw> on I<literal> result class names
287 as this would ignore any derivative result classes provided by B<Mail::SPF>
288 extension modules.
289  
290 =cut
291  
292 sub throw_result {
293 0 ▶ my ($self, $name, $request, @text) = @_;
Subroutine throw_result: not called CC=1 SCAR=6.9
Pod documented
294 0 $self->result_class($name)->throw($self, $request, @text);
295 }
296  
297 =back
298  
299 =head2 Instance methods
300  
301 The following instance methods are provided:
302  
303 =over
304  
305 =item B<process($request)>: returns I<Mail::SPF::Result>
306  
307 Processes the given I<Mail::SPF::Request> object, queries the authoritative
308 domain for an SPF sender policy (see the description of the L</select_record>
309 method), evaluates the policy with regard to the given identity and other
310 request parameters, and returns a I<Mail::SPF::Result> object denoting the
311 result of the policy evaluation. See RFC 4408, 4, and RFC 4406, 4, for
312 details.
313  
314 =cut
315  
316 sub process {
317 0 ▶ my ($self, $request) = @_;
Subroutine process: not called CC=1 SCAR=6.9
Pod documented
318  
319 0 $request->state('authority_explanation', undef);
320 0 $request->state('dns_interactive_terms_count', 0);
321 0 $request->state('void_dns_lookups_count', 0);
322  
323 0 my $result;
324 try {
325 0 ▶ my $record = $self->select_record($request);
Subroutine __ANON__: not called CC=1 SCAR=6.9
326 0 $request->record($record);
327 0 $record->eval($self, $request);
328 }
329 catch Mail::SPF::Result with {
330 0 ▶ $result = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
331 }
332 catch Mail::SPF::EDNSError with {
333 0 ▶ $result = $self->result_class('temperror')->new($self, $request, shift->text);
Subroutine __ANON__: not called CC=1 SCAR=6.9
334 }
335 catch Mail::SPF::ENoAcceptableRecord with {
336 0 ▶ if((not defined $request->{record}->{terms}[0]->{domain_spec}->{text}) or ($request->{record}->{terms}[0]->{domain_spec}->{text} !~ /\.\./)) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $request->{'record'}{'terms'}[0]{'domain_spec'}{'text'} && $request->{'record'}{'terms'}[0]{'domain_spec'}{'text'} =~ /\.\./) { } 0 0 0
Condition0%0/3
defined $request->{'record'}{'terms'}[0]{'domain_spec'}{'text'} && $request->{'record'}{'terms'}[0]{'domain_spec'}{'text'} =~ /\.\./
!ll&&!rl&&r
000
MC/DC0%0/2
defined $request->{'record'}{'terms'}[0]{'domain_spec'}{'text'} && $request->{'record'}{'terms'}[0]{'domain_spec'}{'text'} =~ /\.\./
defined $request->{'record'}{'terms'}[0]{'domain_spec'}{'text'} $request->{'record'}{'terms'}[0]{'domain_spec'}{'text'} =~ /\.\./
Truth table0%0/3
A && B
A= defined $request->{'record'}{'terms'}[0]{'domain_spec'}{'text'}B= $request->{'record'}{'terms'}[0]{'domain_spec'}{'text'} =~ /\.\./
ABresult
0X0
100
111
337 # remove empty record structure in order to stringify it correctly
338 0 undef $request->{record};
339 0 $result = $self->result_class('none' )->new($self, $request, shift->text);
340 } else {
341 0 $result = $self->result_class('permerror')->new($self, $request, shift->text);
342 }
343 }
344 catch Mail::SPF::ERedundantAcceptableRecords with {
345 0 ▶ $result = $self->result_class('permerror')->new($self, $request, shift->text);
Subroutine __ANON__: not called CC=1 SCAR=6.9
346 }
347 catch Mail::SPF::ESyntaxError with {
348 0 ▶ $result = $self->result_class('permerror')->new($self, $request, shift->text);
Subroutine __ANON__: not called CC=1 SCAR=6.9
349 }
350 catch Mail::SPF::EProcessingLimitExceeded with {
351 0 ▶ $result = $self->result_class('permerror')->new($self, $request, shift->text);
Subroutine __ANON__: not called CC=1 SCAR=6.9
352 0 };
353 # Propagate other, unknown errors.
354 # This should not happen, but if it does, it helps exposing the bug!
355  
356 0 return $result;
357 }
358  
359 =item B<select_record($request)>: returns I<Mail::SPF::Record>;
360 throws I<Mail::SPF::EDNSError>,
361 I<Mail::SPF::ENoAcceptableRecord>, I<Mail::SPF::ERedundantAcceptableRecords>,
362 I<Mail::SPF::ESyntaxError>
363  
364 Queries the authority domain of the given I<Mail::SPF::Request> object for SPF
365 sender policy records and, if multiple records are available, selects the
366 record of the highest acceptable record version that covers the requested
367 scope.
368  
369 More precisely, the following algorithm is performed (assuming that both C<TXT>
370 and C<SPF> RR types are being queried):
371  
372 =over
373  
374 =item 1.
375  
376 Determine the authority domain, the set of acceptable SPF record versions, and
377 the identity scope from the given request object.
378  
379 =item 2.
380  
381 Query the authority domain for SPF records of the C<SPF> DNS RR type,
382 discarding any records that are of an inacceptable version or do not cover the
383 desired scope.
384  
385 If this yields no SPF records, query the authority domain for SPF records of
386 the C<TXT> DNS RR type, discarding any records that are of an inacceptable
387 version or do not cover the desired scope.
388  
389 If still no acceptable SPF records could be found, throw a
390 I<Mail::SPF::ENoAcceptableRecord> exception.
391  
392 =item 3.
393  
394 Discard all records but those of the highest acceptable version found.
395  
396 If exactly one record remains, return it. Otherwise, throw a
397 I<Mail::SPF::ERedundantAcceptableRecords> exception.
398  
399 =back
400  
401 If the querying of either RR type has been disabled via the L</new>
402 constructor's C<query_rr_types> option, the respective part in step 2 will
403 be skipped.
404  
405 I<Mail::SPF::EDNSError> exceptions due to DNS look-ups and
406 I<Mail::SPF::ESyntaxError> exceptions due to invalid acceptable records may
407 also be thrown.
408  
409 =cut
410  
411 sub select_record {
412 0 ▶ my ($self, $request) = @_;
Subroutine select_record: not called CC=9 SCAR=45.0
Pod documented
413  
414 0 my $domain = $request->authority_domain;
415 0 my @versions = $request->versions;
416 0 my $scope = $request->scope;
417  
418 # Employ identical behavior for 'v=spf1' and 'spf2.0' records, both of
419 # which support SPF (code 99) and TXT type records (this may be different
420 # in future revisions of SPF):
421 # Query for SPF type records first, then fall back to TXT type records.
422  
423 0 my @records;
424 0 my $query_count = 0;
425 0 my @dns_errors;
426  
427 # Query for SPF-type RRs first:
428 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->query_rr_types == $self->query_rr_type_all or $self->query_rr_types & $self->query_rr_type_spf) 0 0 0
Condition0%0/3
$self->query_rr_types == $self->query_rr_type_all or $self->query_rr_types & $self->query_rr_type_spf
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->query_rr_types == $self->query_rr_type_all or $self->query_rr_types & $self->query_rr_type_spf
$self->query_rr_types == $self->query_rr_type_all $self->query_rr_types & $self->query_rr_type_spf
Truth table0%0/3
A or B
A= $self->query_rr_types == $self->query_rr_type_allB= $self->query_rr_types & $self->query_rr_type_spf
ABresult
1X1
011
000
429 $self->query_rr_types == $self->query_rr_type_all or
430 $self->query_rr_types & $self->query_rr_type_spf
431 ) {
432 try {
433 0 ▶ $query_count++;
Subroutine __ANON__: not called CC=1 SCAR=6.9
434 0 my $packet = $self->dns_lookup($domain, 'SPF');
435 0 push(
436 @records,
437 $self->get_acceptable_records_from_packet(
438 $packet, 'SPF', \@versions, $scope, $domain)
439 );
440 }
441 catch Mail::SPF::EDNSError with {
442 0 ▶ push(@dns_errors, shift);
Subroutine __ANON__: not called CC=1 SCAR=6.9
443 0 };
444 #catch Mail::SPF::EDNSTimeout with {
445 # # FIXME Ignore DNS time-outs on SPF type lookups?
446 # # Apparrently some brain-dead DNS servers time out on SPF-type queries.
447 #};
448 }
449  
450 # If no usable SPF-type RRs, try TXT-type RRs:
451 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not @records and $self->query_rr_types == $self->query_rr_type_all || $self->query_rr_types & $self->query_rr_type_txt) 0 0 0
Condition0%0/6
not @records and $self->query_rr_types == $self->query_rr_type_all || $self->query_rr_types & $self->query_rr_type_txt
!ll&&!rl&&r
000
$self->query_rr_types == $self->query_rr_type_all or $self->query_rr_types & $self->query_rr_type_txt
l!l&&r!l&&!r
000
MC/DC0%0/3
not @records and $self->query_rr_types == $self->query_rr_type_all || $self->query_rr_types & $self->query_rr_type_txt
not @records $self->query_rr_types == $self->query_rr_type_all $self->query_rr_types & $self->query_rr_type_txt
Truth table0%0/4
not(A) and B or C
A= not @recordsB= $self->query_rr_types == $self->query_rr_type_allC= $self->query_rr_types & $self->query_rr_type_txt
ABCresult
0XX0
1000
11X1
1011
452 not @records and
453 (
454 $self->query_rr_types == $self->query_rr_type_all or
455 $self->query_rr_types & $self->query_rr_type_txt
456 )
457 ) {
458 # NOTE:
459 # This deliberately violates RFC 4406 (Sender ID), 4.4/3 (4.4.1):
460 # TXT-type RRs are still tried if there _are_ SPF-type RRs but all of
461 # them are inapplicable (i.e. "Hi!", or even "spf2.0/pra" for an
462 # 'mfrom' scope request). This conforms to the spirit of the more
463 # sensible algorithm in RFC 4408 (SPF), 4.5.
464 # Implication: Sender ID processing may make use of existing TXT-
465 # type records where a result of "None" would normally be returned
466 # under a strict interpretation of RFC 4406.
467  
468 try {
469 0 ▶ $query_count++;
Subroutine __ANON__: not called CC=1 SCAR=6.9
470 0 my $packet = $self->dns_lookup($domain, 'TXT');
471 0 push(
472 @records,
473 $self->get_acceptable_records_from_packet(
474 $packet, 'TXT', \@versions, $scope, $domain)
475 );
476 }
477 catch Mail::SPF::EDNSError with {
478 0 ▶ push(@dns_errors, shift);
Subroutine __ANON__: not called CC=1 SCAR=6.9
479 0 };
480 }
481  
482 0 ▶ @dns_errors < $query_count
Branch: 1 branch
BranchTrueFalseTotal
@dns_errors < $query_count or $dns_errors[0]->throw 0 0 0
483 or $dns_errors[0]->throw;
484 # Unless at least one query succeeded, re-throw the first DNS error that occurred.
485  
486 @records
487 0 ▶ or throw Mail::SPF::ENoAcceptableRecord(
Branch: 1 branch
BranchTrueFalseTotal
@records or "Mail::SPF::ENoAcceptableRecord"->throw("No applicable sender policy available") 0 0 0
488 "No applicable sender policy available"); # RFC 4408, 4.5/7
489  
490 # Discard all records but the highest acceptable version:
491 0 my $preferred_record_class = $records[0]->class;
492 0 @records = grep($_->isa($preferred_record_class), @records);
493  
494 0 ▶ @records == 1
Branch: 1 branch
BranchTrueFalseTotal
@records == 1 or "Mail::SPF::ERedundantAcceptableRecords"->throw(q[Redundant applicable '] . $preferred_record_class->version_tag . q[' ] . "sender policies found") 0 0 0
495 or throw Mail::SPF::ERedundantAcceptableRecords(
496 "Redundant applicable '" . $preferred_record_class->version_tag . "' " .
497 "sender policies found"); # RFC 4408, 4.5/6
498  
499 0 return $records[0];
500 }
501  
502 =item B<get_acceptable_records_from_packet($packet, $rr_type, \@versions, $scope, $domain)>:
503 returns I<list> of I<Mail::SPF::Record>
504  
505 Filters from the given I<Net::DNS::Packet> object all resource records of the
506 given RR type and for the given domain name, discarding any records that are
507 not SPF records at all, that are of an inacceptable SPF record version, or that
508 do not cover the given scope. Returns a list of acceptable records.
509  
510 =cut
511  
512 sub get_acceptable_records_from_packet {
513 0 ▶ my ($self, $packet, $rr_type, $versions, $scope, $domain) = @_;
Subroutine get_acceptable_records_from_packet: not called CC=8 SCAR=42.8
Pod documented
514  
515 0 my @versions = sort { $b <=> $a } @$versions;
516 # Try higher record versions first.
517 # (This may be too simplistic for future revisions of SPF.)
518  
519 0 my @records;
520  
521 0 foreach my $rr ($packet->answer) {
522 0 ▶ next if $rr->type ne $rr_type; # Ignore RRs of unexpected type.
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type ne $rr_type 0 0 0
523  
524 # char_str_list method is 'historical', use as a fallback for Net::DNS prior to 0.69
525 # where txtdata is not available.
526 # join with no intervening spaces, RFC 6376
527 # must call txtdata() in a list context
528 0 ▶ my $text = $rr->can('txtdata')
Branch: 1 branch
BranchTrueFalseTotal
$rr->can('txtdata') ? : 0 0 0
529 ? join('', $rr->txtdata)
530 : join('', $rr->char_str_list);
531 0 my $record;
532  
533 # Try to parse RR as each of the requested record versions,
534 # starting from the highest version:
535 VERSION:
536 0 foreach my $version (@versions) {
537 0 my $class = $self->record_classes_by_version->{$version};
538 0 eval("require $class");
539 try {
540 0 ▶ $record = $class->new_from_string($text);
Subroutine __ANON__: not called CC=1 SCAR=6.9
541 }
542 0 ▶ catch Mail::SPF::EInvalidRecordVersion with {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
543 # Ignore non-SPF and unknown-version records.
544 # Propagate other errors (including syntax errors), though.
545 0 ▶ last VERSION if defined($record);
Branch: 1 branch
BranchTrueFalseTotal
if defined $record 0 0 0
546 }
547  
548 0 ▶ push(@records, $record)
Branch: 1 branch
BranchTrueFalseTotal
if defined $record and grep(($scope eq $_), $record->scopes) 0 0 0
Condition0%0/3
defined $record and grep(($scope eq $_), $record->scopes)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $record and grep(($scope eq $_), $record->scopes)
defined $record grep(($scope eq $_), $record->scopes)
Truth table0%0/3
A and B
A= defined $recordB= grep(($scope eq $_), $record->scopes)
ABresult
0X0
100
111
549 if defined($record)
550 and grep($scope eq $_, $record->scopes); # record covers requested scope?
551 }
552 0 return @records;
553 }
554  
555 =item B<dns_lookup($domain, $rr_type)>: returns I<Net::DNS::Packet>;
556 throws I<Mail::SPF::EDNSTimeout>, I<Mail::SPF::EDNSError>
557  
558 Queries the DNS using the configured resolver for resource records of the
559 desired type at the specified domain and returns a I<Net::DNS::Packet> object
560 if an answer packet was received. Throws a I<Mail::SPF::EDNSTimeout> exception
561 if a DNS time-out occurred. Throws a I<Mail::SPF::EDNSError> exception if an
562 error (other than RCODE 3 AKA C<NXDOMAIN>) occurred.
563  
564 =cut
565  
566 sub dns_lookup {
567 4 ▶ my ($self, $domain, $rr_type) = @_;
Subroutine dns_lookup: called CC=7 SCAR=23.3
Pod documented
568  
569 4 my $packet;
570  
571 4 ▶ if (UNIVERSAL::isa($domain, 'Mail::SPF::MacroString')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($domain, "Mail::SPF::MacroString")) 0 4 4
572 0 $domain = $domain->expand;
573 # Truncate overlong labels at 63 bytes (RFC 4408, 8.1/27):
574 0 $domain =~ s/([^.]{63})[^.]+/$1/g;
575 # Drop labels from the head of domain if longer than 253 bytes (RFC 4408, 8.1/25):
576 0 ▶ $domain =~ s/^[^.]+\.(.*)$/$1/
Branch: 1 branch
BranchTrueFalseTotal
length $domain > 253 and do { $domain =~ s/^[^.]+\.(.*)$/$1/ } 0 0 0
577 while length($domain) > 253;
578 }
579  
580 4 $domain =~ s/^(.*?)\.?$/\L$1/; # Normalize domain.
581  
582 try {
583 4 ▶ $packet = $self->dns_resolver->send($domain, $rr_type);
Subroutine __ANON__: called CC=1 SCAR=0.0
584 }
585 otherwise {
586 0 ▶ throw Mail::SPF::ENoAcceptableRecord($self->dns_resolver->errorstring);
Subroutine __ANON__: not called CC=1 SCAR=6.9
587 4 };
588  
589 # Throw DNS exception unless an answer packet with RCODE 0 or 3 (NXDOMAIN)
590 # was received (thereby treating NXDOMAIN as an acceptable but empty answer packet):
591 4 ▶ throw Mail::SPF::EDNSTimeout("Time-out on DNS '$rr_type' lookup of '$domain'")
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->dns_resolver->errorstring and $self->dns_resolver->errorstring =~ /^(timeout|query timed out)$/ 0 4 4
Condition33%1/3
defined $self->dns_resolver->errorstring and $self->dns_resolver->errorstring =~ /^(timeout|query timed out)$/
!ll&&!rl&&r
040
MC/DC0%0/2
defined $self->dns_resolver->errorstring and $self->dns_resolver->errorstring =~ /^(timeout|query timed out)$/
defined $self->dns_resolver->errorstring $self->dns_resolver->errorstring =~ /^(timeout|query timed out)$/
Truth table33%1/3
A and B
A= defined $self->dns_resolver->errorstringB= $self->dns_resolver->errorstring =~ /^(timeout|query timed out)$/
ABresult
0X0
100
111
592 if defined $self->dns_resolver->errorstring && $self->dns_resolver->errorstring =~ /^(timeout|query timed out)$/;
593 4 ▶ throw Mail::SPF::EDNSError("Unknown error on DNS '$rr_type' lookup of '$domain'")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $packet 0 4 4
594 unless defined $packet;
595 4 ▶ throw Mail::SPF::EDNSError("'" . $packet->header->rcode . "' error on DNS '$rr_type' lookup of '$domain'")
Branch: 1 branch
BranchTrueFalseTotal
unless $packet->header->rcode =~ /^(NOERROR|NXDOMAIN)$/ 1 3 4
596 unless $packet->header->rcode =~ /^(NOERROR|NXDOMAIN)$/;
597  
598 3 return $packet;
599 }
600  
601 =item B<count_dns_interactive_term($request)>: throws I<Mail::SPF::EProcessingLimitExceeded>
602  
603 Increments by one the count of DNS-interactive mechanisms and modifiers that
604 have been processed so far during the evaluation of the given
605 I<Mail::SPF::Request> object. If this exceeds the configured limit (see the
606 L</new> constructor's C<max_dns_interactive_terms> option), throws a
607 I<Mail::SPF::EProcessingLimitExceeded> exception.
608  
609 This method is supposed to be called by the C<match> and C<process> methods of
610 I<Mail::SPF::Mech> and I<Mail::SPF::Mod> sub-classes before (and only if) they
611 do any DNS look-ups.
612  
613 =cut
614  
615 sub count_dns_interactive_term {
616 0 ▶ my ($self, $request) = @_;
Subroutine count_dns_interactive_term: not called CC=3 SCAR=24.8
Pod documented
617 0 my $dns_interactive_terms_count = ++$request->root_request->state('dns_interactive_terms_count');
618 0 my $max_dns_interactive_terms = $self->max_dns_interactive_terms;
619 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_dns_interactive_terms and $dns_interactive_terms_count > $max_dns_interactive_terms) 0 0 0
Condition0%0/3
defined $max_dns_interactive_terms and $dns_interactive_terms_count > $max_dns_interactive_terms
!ll&&!rl&&r
000
MC/DC0%0/2
defined $max_dns_interactive_terms and $dns_interactive_terms_count > $max_dns_interactive_terms
defined $max_dns_interactive_terms $dns_interactive_terms_count > $max_dns_interactive_terms
Truth table0%0/3
A and B
A= defined $max_dns_interactive_termsB= $dns_interactive_terms_count > $max_dns_interactive_terms
ABresult
0X0
100
111
620 defined($max_dns_interactive_terms) and
621 $dns_interactive_terms_count > $max_dns_interactive_terms
622 ) {
623 0 throw Mail::SPF::EProcessingLimitExceeded(
624 "Maximum DNS-interactive terms limit ($max_dns_interactive_terms) exceeded");
625 }
626 0 return;
627 }
628  
629 =item B<count_void_dns_lookup($request)>: throws I<Mail::SPF::EProcessingLimitExceeded>
630  
631 Increments by one the count of "void" DNS look-ups that have occurred so far
632 during the evaluation of the given I<Mail::SPF::Request> object. If this
633 exceeds the configured limit (see the L</new> constructor's C<max_void_dns_lookups>
634 option), throws a I<Mail::SPF::EProcessingLimitExceeded> exception.
635  
636 This method is supposed to be called by any code after any calls to the
637 L</dns_lookup> method whenever (i) no answer records were returned, and (ii)
638 this fact is a possible indication of a DoS attack against a third-party victim
639 domain, and (iii) the number of "void" look-ups is not already constrained
640 otherwise (as for example is the case with the C<include> mechanism and the
641 C<redirect> modifier). Specifically, this applies to look-ups performed by the
642 C<a>, C<mx>, C<ptr>, and C<exists> mechanisms and the C<p> macro.
643  
644 =cut
645  
646 sub count_void_dns_lookup {
647 0 ▶ my ($self, $request) = @_;
Subroutine count_void_dns_lookup: not called CC=3 SCAR=24.8
Pod documented
648 0 my $void_dns_lookups_count = ++$request->root_request->state('void_dns_lookups_count');
649 0 my $max_void_dns_lookups = $self->max_void_dns_lookups;
650 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_void_dns_lookups and $void_dns_lookups_count > $max_void_dns_lookups) 0 0 0
Condition0%0/3
defined $max_void_dns_lookups and $void_dns_lookups_count > $max_void_dns_lookups
!ll&&!rl&&r
000
MC/DC0%0/2
defined $max_void_dns_lookups and $void_dns_lookups_count > $max_void_dns_lookups
defined $max_void_dns_lookups $void_dns_lookups_count > $max_void_dns_lookups
Truth table0%0/3
A and B
A= defined $max_void_dns_lookupsB= $void_dns_lookups_count > $max_void_dns_lookups
ABresult
0X0
100
111
651 defined($max_void_dns_lookups) and
652 $void_dns_lookups_count > $max_void_dns_lookups
653 ) {
654 0 throw Mail::SPF::EProcessingLimitExceeded(
655 "Maximum void DNS look-ups limit ($max_void_dns_lookups) exceeded");
656 }
657 0 return;
658 }
659  
660 =item B<default_authority_explanation>: returns I<Mail::SPF::MacroString>
661  
662 Returns the default authority explanation as a I<MacroString> object. See the
663 description of the L</new> constructor's C<default_authority_explanation>
664 option.
665  
666 =cut
667  
668 sub default_authority_explanation {
669 0 ▶ my ($self) = @_;
Subroutine default_authority_explanation: not called CC=1 SCAR=6.9
Pod documented
670  
671 return Mail::SPF::MacroString->new(
672 text => $self->{default_authority_explanation},
673 0 server => $self,
674 is_explanation => TRUE
675 );
676 }
677  
678 =item B<hostname>: returns I<string>
679  
680 Returns the local system's host name. See the description of the L</new>
681 constructor's C<hostname> option.
682  
683 =item B<dns_resolver>: returns I<Net::DNS::Resolver> or compatible object
684  
685 Returns the DNS resolver object of the server object. See the description of
686 the L</new> constructor's C<dns_resolver> option.
687  
688 =item B<query_rr_types>: returns I<integer>
689  
690 Returns a value denoting the RR types for which to query when looking up and
691 selecting SPF records. See the description of the L</new> constructor's
692 C<query_rr_types> option.
693  
694 =item B<max_dns_interactive_terms>: returns I<integer>
695  
696 =item B<max_name_lookups_per_term>: returns I<integer>
697  
698 =item B<max_name_lookups_per_mx_mech>: returns I<integer>
699  
700 =item B<max_name_lookups_per_ptr_mech>: returns I<integer>
701  
702 =item B<max_void_dns_lookups>: returns I<integer>
703  
704 Return the limit values of the server object. See the description of the
705 L</new> constructor's corresponding options.
706  
707 =cut
708  
709 # Make read-only accessors:
710 __PACKAGE__->make_accessor($_, TRUE)
711 3 foreach qw(
712 hostname
713  
714 dns_resolver
715 query_rr_types
716  
717 max_dns_interactive_terms
718 max_name_lookups_per_term
719 max_name_lookups_per_mx_mech
720 max_name_lookups_per_ptr_mech
721  
722 max_void_dns_lookups
723 );
724  
725 =back
726  
727 =head1 SEE ALSO
728  
729 L<Mail::SPF>, L<Mail::SPF::Request>, L<Mail::SPF::Result>
730  
731 L<http://tools.ietf.org/html/rfc4408>
732  
733 For availability, support, and license information, see the README file
734 included with Mail::SPF.
735  
736 =head1 AUTHORS
737  
738 Julian Mehnle <julian@mehnle.net>, Shevek <cpan@anarres.org>
739  
740 =cut
741  
742 3 TRUE;
« SPF/Mech/MX.pm ↑ Summary SPF/v2/Record.pm »