SPF/Mech/MX.pm

filter: Statementstmt 54.5% 24 / 44 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 70.0% 7 / 10 Podpod 66.6% 2 / 3 totaltotal 40.7% 33 / 81 CC 11 SCAR 32.4
CC
11
Cov
38%
params
42.0
match
42.0
parse_params
2.0
CRAP 25.6
« SPF/Mech.pm ↑ Summary SPF/Server.pm »
1 #
2 # Mail::SPF::Mech::MX
3 # SPF record "mx" mechanism class.
4 #
5 # (C) 2005-2012 Julian Mehnle <julian@mehnle.net>
6 # 2005 Shevek <cpan@anarres.org>
7 # $Id: MX.pm 57 2012-01-30 08:15:31Z julian $
8 #
9 ##############################################################################
10  
11 package Mail::SPF::Mech::MX;
12  
13 =head1 NAME
14  
15 Mail::SPF::Mech::MX - SPF record C<mx> mechanism class
16  
17 =head1 VERSION
18  
19 version 3.20250505
20  
21 =cut
22  
23 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use base 'Mail::SPF::SenderIPAddrMech';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶use constant TRUE => (0 == 0);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use constant FALSE => not TRUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶use constant name => 'mx';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use constant name_pattern => qr/${\name}/i;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 =head1 DESCRIPTION
35  
36 An object of class B<Mail::SPF::Mech::MX> represents an SPF record mechanism of
37 type C<mx>.
38  
39 =head2 Constructors
40  
41 The following constructors are provided:
42  
43 =over
44  
45 =item B<new(%options)>: returns I<Mail::SPF::Mech::MX>
46  
47 Creates a new SPF record C<mx> mechanism object.
48  
49 %options is a list of key/value pairs representing any of the following
50 options:
51  
52 =over
53  
54 =item B<qualifier>
55  
56 =item B<domain_spec>
57  
58 =item B<ipv4_prefix_length>
59  
60 =item B<ipv6_prefix_length>
61  
62 See L<Mail::SPF::Mech/new>.
63  
64 =back
65  
66 =item B<new_from_string($text, %options)>: returns I<Mail::SPF::Mech::MX>;
67 throws I<Mail::SPF::ENothingToParse>, I<Mail::SPF::EInvalidMech>
68  
69 Creates a new SPF record C<mx> mechanism object by parsing the string and
70 any options given.
71  
72 =back
73  
74 =head2 Class methods
75  
76 The following class methods are provided:
77  
78 =over
79  
80 =item B<default_qualifier>
81  
82 =item B<default_ipv4_prefix_length>
83  
84 =item B<default_ipv6_prefix_length>
85  
86 =item B<qualifier_pattern>
87  
88 See L<Mail::SPF::Mech/Class methods>.
89  
90 =item B<name>: returns I<string>
91  
92 Returns B<'mx'>.
93  
94 =item B<name_pattern>: returns I<Regexp>
95  
96 Returns a regular expression that matches a mechanism name of B<'mx'>.
97  
98 =back
99  
100 =head2 Instance methods
101  
102 The following instance methods are provided:
103  
104 =over
105  
106 =cut
107  
108 sub parse_params {
109 0 ▶ my ($self) = @_;
Subroutine parse_params: not called CC=1 SCAR=6.9
Pod undocumented
110 0 $self->parse_domain_spec();
111 0 $self->parse_ipv4_ipv6_prefix_lengths();
112 0 return;
113 }
114  
115 =item B<text>
116  
117 =item B<qualifier>
118  
119 =item B<params>
120  
121 =cut
122  
123 sub params {
124 0 ▶ my ($self) = @_;
Subroutine params: not called CC=6 SCAR=37.4
Pod documented
125 0 my $params;
126 $params .= ':' . $self->{domain_spec}
127 0 ▶ if defined($self->{domain_spec});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"domain_spec"} 0 0 0
128 $params .= '/' . $self->{ipv4_prefix_length}
129 if defined($self->{ipv4_prefix_length})
130 0 ▶ and $self->{ipv4_prefix_length} != $self->default_ipv4_prefix_length;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ipv4_prefix_length"} and $self->{"ipv4_prefix_length"} != $self->default_ipv4_prefix_length 0 0 0
Condition0%0/3
defined $self->{"ipv4_prefix_length"} and $self->{"ipv4_prefix_length"} != $self->default_ipv4_prefix_length
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"ipv4_prefix_length"} and $self->{"ipv4_prefix_length"} != $self->default_ipv4_prefix_length
defined $self->{"ipv4_prefix_length"} $self->{"ipv4_prefix_length"} != $self->default_ipv4_prefix_length
Truth table0%0/3
A and B
A= defined $self->{"ipv4_prefix_length"}B= $self->{"ipv4_prefix_length"} != $self->default_ipv4_prefix_length
ABresult
0X0
100
111
131 $params .= '//' . $self->{ipv6_prefix_length}
132 if defined($self->{ipv6_prefix_length})
133 0 ▶ and $self->{ipv6_prefix_length} != $self->default_ipv6_prefix_length;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ipv6_prefix_length"} and $self->{"ipv6_prefix_length"} != $self->default_ipv6_prefix_length 0 0 0
Condition0%0/3
defined $self->{"ipv6_prefix_length"} and $self->{"ipv6_prefix_length"} != $self->default_ipv6_prefix_length
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"ipv6_prefix_length"} and $self->{"ipv6_prefix_length"} != $self->default_ipv6_prefix_length
defined $self->{"ipv6_prefix_length"} $self->{"ipv6_prefix_length"} != $self->default_ipv6_prefix_length
Truth table0%0/3
A and B
A= defined $self->{"ipv6_prefix_length"}B= $self->{"ipv6_prefix_length"} != $self->default_ipv6_prefix_length
ABresult
0X0
100
111
134 0 return $params;
135 }
136  
137 =item B<stringify>
138  
139 =item B<domain>
140  
141 =item B<match_in_domain>
142  
143 See L<Mail::SPF::Mech/Instance methods>.
144  
145 =item B<domain_spec>: returns I<Mail::SPF::MacroString>
146  
147 Returns the C<domain-spec> parameter of the mechanism.
148  
149 =item B<ipv4_prefix_length>: returns I<integer>
150  
151 Returns the IPv4 network prefix length of the mechanism.
152  
153 =item B<ipv6_prefix_length>: returns I<integer>
154  
155 Returns the IPv6 network prefix length of the mechanism.
156  
157 =cut
158  
159 # Make read-only accessors:
160 __PACKAGE__->make_accessor($_, TRUE)
161 1 foreach qw(domain_spec ipv4_prefix_length ipv6_prefix_length);
162  
163 =item B<match($server, $request)>: returns I<boolean>
164  
165 Checks whether any MX hosts of the mechanism's target domain name (that is, any
166 of the host addresses of its DNS C<MX> records) matches the given request's IP
167 address (see L<Mail::SPF::Request/ip_address>), and returns B<true> if it does,
168 or B<false> otherwise. The mechanism's IP network prefix lengths are respected
169 when matching address records against the request's IP address. See RFC 4408,
170 5 and 5.4, for the exact algorithm used.
171  
172 =cut
173  
174 sub match {
175 0 ▶ my ($self, $server, $request) = @_;
Subroutine match: not called CC=6 SCAR=37.4
Pod documented
176  
177 0 $server->count_dns_interactive_term($request);
178  
179 0 my $target_domain = $self->domain($server, $request);
180 0 my $mx_packet = $server->dns_lookup($target_domain, 'MX');
181 0 ▶ my @mx_rrs = $mx_packet->answer
Branch: 1 branch
BranchTrueFalseTotal
my(@mx_rrs) = $mx_packet->answer or $server->count_void_dns_lookup($request) 0 0 0
182 or $server->count_void_dns_lookup($request);
183  
184 # Respect the MX mechanism lookups limit (RFC 4408, 5.4/3/4):
185 0 ▶ @mx_rrs = splice(@mx_rrs, 0, $server->max_name_lookups_per_mx_mech)
Branch: 1 branch
BranchTrueFalseTotal
if defined $server->max_name_lookups_per_mx_mech 0 0 0
186 if defined($server->max_name_lookups_per_mx_mech);
187  
188 # TODO Use A records from packet's "additional" section? Probably not.
189  
190 # Check MX records:
191 0 foreach my $rr (@mx_rrs) {
192 0 ▶ if ($rr->type eq 'MX') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type eq 'MX') { } 0 0 0
193 0 ▶ return TRUE
Branch: 1 branch
BranchTrueFalseTotal
if $self->match_in_domain($server, $request, $rr->exchange) 0 0 0
194 if $self->match_in_domain($server, $request, $rr->exchange);
195 }
196 else {
197 # Unexpected RR type.
198 # TODO Generate debug info or ignore silently.
199 }
200 }
201  
202 0 return FALSE;
203 }
204  
205 =back
206  
207 =head1 SEE ALSO
208  
209 L<Mail::SPF>, L<Mail::SPF::Record>, L<Mail::SPF::Term>, L<Mail::SPF::Mech>
210  
211 L<http://tools.ietf.org/html/rfc4408>
212  
213 For availability, support, and license information, see the README file
214 included with Mail::SPF.
215  
216 =head1 AUTHORS
217  
218 Julian Mehnle <julian@mehnle.net>, Shevek <cpan@anarres.org>
219  
220 =cut
221  
222 1 TRUE;
« SPF/Mech.pm ↑ Summary SPF/Server.pm »