lib/Net/validMX.pm

filter: Statementstmt 73.9% 261 / 353 Branchbran 58.3% 181 / 310 Conditioncond 56.7% 46 / 81 MC/DCmcdc 36.0% 18 / 50 Subroutinesub 75.0% 15 / 20 Podpod 42.8% 6 / 14 totaltotal 63.6% 527 / 828 CC 185 SCAR 52.4
CC
185
Cov
66%
check_valid_mx
348.4
check_spf_for_domain
110.0
check_implicit_mx
64.7
CRAP 188.5
↑ Summary blib/script/check_email_and_mx.pl »
1 # The "Artistic License"
2 #
3 # Preamble
4 #
5 # The intent of this document is to state the conditions under which a
6 # Package may be copied, such that the Copyright Holder maintains some
7 # semblance of artistic control over the development of the package,
8 # while giving the users of the package the right to use and distribute
9 # the Package in a more-or-less customary fashion, plus the right to make
10 # reasonable modifications.
11 #
12 # Definitions:
13 #
14 # "Package" refers to the collection of files distributed by the
15 # Copyright Holder, and derivatives of that collection of files
16 # created through textual modification.
17 #
18 # "Standard Version" refers to such a Package if it has not been
19 # modified, or has been modified in accordance with the wishes
20 # of the Copyright Holder as specified below.
21 #
22 # "Copyright Holder" is whoever is named in the copyright or
23 # copyrights for the package.
24 #
25 # "You" is you, if you're thinking about copying or distributing
26 # this Package.
27 #
28 # "Reasonable copying fee" is whatever you can justify on the
29 # basis of media cost, duplication charges, time of people involved,
30 # and so on. (You will not be required to justify it to the
31 # Copyright Holder, but only to the computing community at large
32 # as a market that must bear the fee.)
33 #
34 # "Freely Available" means that no fee is charged for the item
35 # itself, though there may be fees involved in handling the item.
36 # It also means that recipients of the item may redistribute it
37 # under the same conditions they received it.
38 #
39 # 1. You may make and give away verbatim copies of the source form of the
40 # Standard Version of this Package without restriction, provided that you
41 # duplicate all of the original copyright notices and associated disclaimers.
42 #
43 # 2. You may apply bug fixes, portability fixes and other modifications
44 # derived from the Public Domain or from the Copyright Holder. A Package
45 # modified in such a way shall still be considered the Standard Version.
46 #
47 # 3. You may otherwise modify your copy of this Package in any way, provided
48 # that you insert a prominent notice in each changed file stating how and
49 # when you changed that file, and provided that you do at least ONE of the
50 # following:
51 #
52 # a) place your modifications in the Public Domain or otherwise make them
53 # Freely Available, such as by posting said modifications to Usenet or
54 # an equivalent medium, or placing the modifications on a major archive
55 # site such as uunet.uu.net, or by allowing the Copyright Holder to include
56 # your modifications in the Standard Version of the Package.
57 #
58 # b) use the modified Package only within your corporation or organization.
59 #
60 # c) rename any non-standard executables so the names do not conflict
61 # with standard executables, which must also be provided, and provide
62 # a separate manual page for each non-standard executable that clearly
63 # documents how it differs from the Standard Version.
64 #
65 # d) make other distribution arrangements with the Copyright Holder.
66 #
67 # 4. You may distribute the programs of this Package in object code or
68 # executable form, provided that you do at least ONE of the following:
69 #
70 # a) distribute a Standard Version of the executables and library files,
71 # together with instructions (in the manual page or equivalent) on where
72 # to get the Standard Version.
73 #
74 # b) accompany the distribution with the machine-readable source of
75 # the Package with your modifications.
76 #
77 # c) give non-standard executables non-standard names, and clearly
78 # document the differences in manual pages (or equivalent), together
79 # with instructions on where to get the Standard Version.
80 #
81 # d) make other distribution arrangements with the Copyright Holder.
82 #
83 # 5. You may charge a reasonable copying fee for any distribution of this
84 # Package. You may charge any fee you choose for support of this
85 # Package. You may not charge a fee for this Package itself. However,
86 # you may distribute this Package in aggregate with other (possibly
87 # commercial) programs as part of a larger (possibly commercial) software
88 # distribution provided that you do not advertise this Package as a
89 # product of your own. You may embed this Package's interpreter within
90 # an executable of yours (by linking); this shall be construed as a mere
91 # form of aggregation, provided that the complete Standard Version of the
92 # interpreter is so embedded.
93 #
94 # 6. The scripts and library files supplied as input to or produced as
95 # output from the programs of this Package do not automatically fall
96 # under the copyright of this Package, but belong to whoever generated
97 # them, and may be sold commercially, and may be aggregated with this
98 # Package. If such scripts or library files are aggregated with this
99 # Package via the so-called "undump" or "unexec" methods of producing a
100 # binary executable image, then distribution of such an image shall
101 # neither be construed as a distribution of this Package nor shall it
102 # fall under the restrictions of Paragraphs 3 and 4, provided that you do
103 # not represent such an executable image as a Standard Version of this
104 # Package.
105 #
106 # 7. C subroutines (or comparably compiled subroutines in other
107 # languages) supplied by you and linked into this Package in order to
108 # emulate subroutines and variables of the language defined by this
109 # Package shall not be considered part of this Package, but are the
110 # equivalent of input as in Paragraph 6, provided these subroutines do
111 # not change the language in any way that would cause it to fail the
112 # regression tests for the language.
113 #
114 # 8. Aggregation of this Package with a commercial distribution is always
115 # permitted provided that the use of this Package is embedded; that is,
116 # when no overt attempt is made to make this Package's interfaces visible
117 # to the end user of the commercial distribution. Such use shall not be
118 # construed as a distribution of this Package.
119 #
120 # 9. The name of the Copyright Holder may not be used to endorse or promote
121 # products derived from this software without specific prior written permission.
122 #
123 # 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
124 # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
125 # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
126  
127 package Net::validMX;
128  
129 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 8 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 8 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
135 $VERSION
136 @ISA
137 @EXPORT_OK
138 $DEBUG
139 $ALLOW_IP_ADDRESS_AS_MX
140 $FLAG_INTRANETS
141 $RESOLUTION_PROBLEM_RETURN
142 $QUERY_TIMEOUT
143 8 $RESOLVER);
144  
145 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
146 8 require DynaLoader;
147 8 require Exporter;
148  
149 8 @ISA = qw(Exporter DynaLoader);
150 8 $VERSION = '2.6.0';
151 8 $DEBUG = 0;
152 8 $ALLOW_IP_ADDRESS_AS_MX = 1;
153 8 $FLAG_INTRANETS = 1;
154 8 $RESOLUTION_PROBLEM_RETURN = 1;
155 8 $QUERY_TIMEOUT = 4;
156 8 $RESOLVER = undef;
157 }
158  
159 0 ▶sub version { $VERSION; }
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
160  
161 8 @EXPORT_OK = qw(check_valid_mx get_output_result check_email_and_mx check_email_validity get_domain_from_email);
162  
163 2 ▶sub new {
Subroutine new: called CC=11 SCAR=27.4
Pod undocumented
164 2 my ($class, %params) = @_;
165 2 my $self = bless {%params}, $class;
166  
167 2 ▶ $DEBUG = $self->{'debug'} if (defined $self->{'debug'} and $self->{'debug'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"debug"} and $self->{"debug"} ne "" 1 1 2
Condition66%2/3
defined $self->{"debug"} and $self->{"debug"} ne ""
!ll&&!rl&&r
101
MC/DC50%1/2
defined $self->{"debug"} and $self->{"debug"} ne ""
defined $self->{"debug"} $self->{"debug"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"debug"}B= $self->{"debug"} ne ""
ABresult
0X0
100
111
168 2 ▶ $ALLOW_IP_ADDRESS_AS_MX = $self->{'allow_ip_address_as_mx'} if (defined $self->{'allow_ip_address_as_mx'} and $self->{'allow_ip_address_as_mx'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"allow_ip_address_as_mx"} and $self->{"allow_ip_address_as_mx"} ne "" 1 1 2
Condition66%2/3
defined $self->{"allow_ip_address_as_mx"} and $self->{"allow_ip_address_as_mx"} ne ""
!ll&&!rl&&r
101
MC/DC50%1/2
defined $self->{"allow_ip_address_as_mx"} and $self->{"allow_ip_address_as_mx"} ne ""
defined $self->{"allow_ip_address_as_mx"} $self->{"allow_ip_address_as_mx"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"allow_ip_address_as_mx"}B= $self->{"allow_ip_address_as_mx"} ne ""
ABresult
0X0
100
111
169 2 ▶ $FLAG_INTRANETS = $self->{'flag_intranets'} if (defined $self->{'flag_intranets'} and $self->{'flag_intranets'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"flag_intranets"} and $self->{"flag_intranets"} ne "" 0 2 2
Condition33%1/3
defined $self->{"flag_intranets"} and $self->{"flag_intranets"} ne ""
!ll&&!rl&&r
200
MC/DC0%0/2
defined $self->{"flag_intranets"} and $self->{"flag_intranets"} ne ""
defined $self->{"flag_intranets"} $self->{"flag_intranets"} ne ""
Truth table33%1/3
A and B
A= defined $self->{"flag_intranets"}B= $self->{"flag_intranets"} ne ""
ABresult
0X0
100
111
170 2 ▶ $RESOLUTION_PROBLEM_RETURN = $self->{'resolution_problem_return'} if (defined $self->{'resolution_problem_return'} and $self->{'resolution_problem_return'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"resolution_problem_return"} and $self->{"resolution_problem_return"} ne "" 0 2 2
Condition33%1/3
defined $self->{"resolution_problem_return"} and $self->{"resolution_problem_return"} ne ""
!ll&&!rl&&r
200
MC/DC0%0/2
defined $self->{"resolution_problem_return"} and $self->{"resolution_problem_return"} ne ""
defined $self->{"resolution_problem_return"} $self->{"resolution_problem_return"} ne ""
Truth table33%1/3
A and B
A= defined $self->{"resolution_problem_return"}B= $self->{"resolution_problem_return"} ne ""
ABresult
0X0
100
111
171 2 ▶ $QUERY_TIMEOUT = $self->{'query_timeout'} if (defined $self->{'query_timeout'} and $self->{'query_timeout'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"query_timeout"} and $self->{"query_timeout"} ne "" 0 2 2
Condition33%1/3
defined $self->{"query_timeout"} and $self->{"query_timeout"} ne ""
!ll&&!rl&&r
200
MC/DC0%0/2
defined $self->{"query_timeout"} and $self->{"query_timeout"} ne ""
defined $self->{"query_timeout"} $self->{"query_timeout"} ne ""
Truth table33%1/3
A and B
A= defined $self->{"query_timeout"}B= $self->{"query_timeout"} ne ""
ABresult
0X0
100
111
172  
173 2 return $self;
174 }
175  
176 0 ▶sub get_debug {
Subroutine get_debug: not called CC=1 SCAR=6.9
Pod undocumented
177 0 return $DEBUG;
178 }
179  
180 0 ▶sub set_debug {
Subroutine set_debug: not called CC=1 SCAR=6.9
Pod undocumented
181 0 my $debug = shift;
182 0 $DEBUG = $debug;
183 }
184  
185 37 ▶sub get_output_result {
Subroutine get_output_result: called CC=2 SCAR=6.9
Pod documented
186 37 my ($email, $rv, $reason) = @_;
187 37 my ($output);
188  
189 37 $output = "$email\n\tValid MX? ".Net::validMX::int_to_truefalse($rv);
190 37 ▶ if ($reason ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reason ne "") 13 24 37
191 13 $output .= " - $reason";
192 }
193 37 $output .= "\n\n";
194  
195 37 return $output;
196 }
197  
198 41 ▶sub check_valid_mx {
Subroutine check_valid_mx: called CC=99 SCAR=58.5
Pod documented
199 #Based on Idea from Les Miksell and much input from Jan Pieter Cornet
200 #KAM 9-12-05 updated 10-24-05 & 11-3-05.
201 #takes the email address, extracts the domain name and performs multiple MX tests to see if the domain has valid
202 #MX exchange records
203  
204 41 my ($res, $packet, @answer, $domain, @answer2, @answer3, $rv, $reason, $i, @unsorted_answer);
205 41 my ($check_implicit_mx, %params, $self, $ref, $resolution_problem_status);
206  
207 #print "DEBUG: ref for \$_[0] ".ref($_[0]). "\n";
208 #IN OO INSTEAD OF PROCEDURAL MODE?
209 41 ▶ if (uc(ref($_[0])) eq 'NET::VALIDMX') {
Branch: 1 branch
BranchTrueFalseTotal
if (uc ref $_[0] eq "NET::VALIDMX") 3 38 41
210 3 $self = shift(@_);
211 #foreach $ref (keys %$self) {
212 # print "DEBUG: OO MODE - $ref: $self->{$ref} \n";
213 #}
214 }
215  
216 #DID WE RECEIVE A HASH INSTEAD OF A SINGLE EMAIL?
217 41 ▶ if ($#_ % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ % 2 == 0) { } 35 6 41
218 35 ($params{'email'}) = @_;
219 } else {
220 6 %params = @_;
221 }
222  
223 41 ▶ $params{'email'} || $params{'sender'} || return (0, 'A blank email address will not be tested.');
Branch: 1 branch
BranchTrueFalseTotal
$params{"email"} or $params{"sender"} or return 0, "A blank email address will not be tested." 41 0 41
Condition33%1/3
$params{"email"} or $params{"sender"}
l!l&&r!l&&!r
4100
MC/DC0%0/2
$params{"email"} or $params{"sender"}
$params{"email"} $params{"sender"}
Truth table33%1/3
A or B
A= $params{"email"}B= $params{"sender"}
ABresult
1X1
011
000
224  
225 #CONSTANTS / SETTABLE OPTIONS
226 41 ▶ $params{'debug'} = $DEBUG unless (defined $params{'debug'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"debug"} 40 1 41
227 41 ▶ $params{'allow_ip_address_as_mx'} = $ALLOW_IP_ADDRESS_AS_MX unless (defined $params{'allow_ip_address_as_mx'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"allow_ip_address_as_mx"} 40 1 41
228 41 ▶ $params{'resolution_problem_return'} = $RESOLUTION_PROBLEM_RETURN unless (defined $params{'resolution_problem_return'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"resolution_problem_return"} 41 0 41
229 41 ▶ $params{'query_timeout'} = $QUERY_TIMEOUT unless (defined $params{'query_timeout'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"query_timeout"} 41 0 41
230 41 ▶ $params{'resolver'} = $self->{'resolver'} if (not defined $params{'resolver'} and defined $self);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $params{"resolver"} and defined $self 2 39 41
Condition100%3/3
not defined $params{"resolver"} and defined $self
!ll&&!rl&&r
3362
MC/DC100%2/2
not defined $params{"resolver"} and defined $self
not defined $params{"resolver"} defined $self
Truth table100%3/3
not(A) and B
A= not defined $params{"resolver"}B= defined $self
ABresult
0X0
100
111
231 41 ▶ $params{'resolver'} = $RESOLVER unless (defined $params{'resolver'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"resolver"} 37 4 41
232  
233 41 ▶ if ($params{'resolution_problem_return'} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'resolution_problem_return'} > 0) { } 41 0 41
234 41 $resolution_problem_status = 'Passed';
235 } else {
236 0 $resolution_problem_status = 'Failed';
237 }
238  
239 41 ▶ print "DEBUG: function debug setting is $params{'debug'}\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 40 41
240 41 ▶ print "DEBUG: function allow_ip_address_as_mx setting is $params{'allow_ip_address_as_mx'}\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 40 41
241 41 ▶ print "DEBUG: function resolution_problem_return setting is $params{'resolution_problem_return'}\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 40 41
242 41 ▶ print "DEBUG: function query_timeout setting is $params{'query_timeout'}\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 40 41
243  
244 #FLAGS - I THINK THIS HAS A LOGIC ISSUE - I LIKELY MEANT ALLOW_IMPLICIT_MX as an option FIX
245 41 $check_implicit_mx = 0;
246  
247 #Setup a DNS Resolver Resource, a resolver passed by the caller is used as is
248 41 ▶ if (defined $params{'resolver'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'resolver'}) { } 5 36 41
249 5 $res = $params{'resolver'};
250 5 ▶ print "DEBUG: using the resolver passed by the caller, query_timeout is not applied\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 5 5
251 } else {
252 36 $res = Net::DNS::Resolver->new;
253 36 ▶ if (defined ($res)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) 36 0 36
254 36 $res->defnames(0); #Turn off appending the default domain for names that have no dots just in case
255 36 $res->searchlist(); #Set the search list to undefined just in case
256  
257 #We have also set the default timeout to only 4 seconds which means we might get network
258 #delays which we do not want to handle as an error.
259 36 $res->tcp_timeout($params{'query_timeout'}); #Number of Seconds before query will fail
260 36 $res->udp_timeout($params{'query_timeout'}); #Number of Seconds before query will fail
261 }
262 }
263  
264 41 ▶ if (defined ($res)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) { } 41 0 41
265 41 $check_implicit_mx = 0;
266  
267 #Strip domain name from an email address
268 41 $domain = get_domain_from_email($params{'email'});
269  
270 #Deny Explicit IP Address Domains
271 41 ▶ if ($domain =~ /^\[.*\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain =~ /^\[.*\]$/) 1 40 41
272 1 $reason = "Use of IP Address $domain instead of a hostname is not allowed";
273 1 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 1 1
274 1 return (0, $reason);
275 }
276  
277 #Perform the DNS Query - Changed to Send instead of Query method to utilize the ancount method
278 40 $packet = $res->send($domain,'MX');
279  
280 #Net::DNS::Resolver had an error
281 40 ▶ if (!defined $packet) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 40 40
282 0 ▶ print "DEBUG: There was an error retrieving the MX Records for $domain\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
283 0 ▶ print "DEBUG: Test Passed by Default\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
284 0 return($params{'resolution_problem_return'}, "Test $resolution_problem_status due to a Resolution Problem retrieving the MX Records");
285 }
286  
287 40 ▶ print "DEBUG: Number of Answers in the MX resolution packet is: ".$packet->header->ancount."\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 39 40
288 #Parse the Query
289 40 ▶ if ($packet->header->ancount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet->header->ancount > 0) { } 38 2 40
290 38 ▶ if (defined ($packet->answer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packet->answer) 38 0 38
291 38 @answer = $packet->answer;
292  
293 38 ▶ for ($i = 0; $i < scalar(@answer); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @answer) 48 38 86
294 48 ▶ if ($answer[$i]->type ne 'MX') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer[$i]->type ne 'MX') { } 4 44 48
295 #DISCARD ANSWER IF THE RECORD IS NOT AN MX RECORD SUCH AS THE CNAME FOR londo.cysticercus.com
296 4 ▶ print "DEBUG: Discarding one non-MX answer of type: ".$answer[$i]->type."\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 4 4
297 } else {
298 44 push @unsorted_answer, $answer[$i];
299 }
300 }
301  
302 38 undef @answer;
303  
304 38 ▶ print "DEBUG: Number of Answers Left to Check after discarding all but MX: ".scalar(@unsorted_answer)."\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 37 38
305 38 ▶ if (scalar(@unsorted_answer) < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @unsorted_answer < 1) { } 2 36 38
306 2 $check_implicit_mx++;
307 } else {
308 #Sort to put answers into ascending order by mail exchange preference
309 36 @answer = sort {$a->preference <=> $b->preference} @unsorted_answer;
310 }
311  
312 #LOOP THROUGH THE ANSWERS WE HAVE
313 38 ▶ for ($i = 0; $i < scalar(@answer); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @answer) 43 2 45
314 43 undef $packet;
315 43 ▶ print "DEBUG: $i - MX Answer - Type: ".$answer[$i]->type." - Exchange: ".$answer[$i]->exchange." - Length: ".length($answer[$i]->exchange)."\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 42 43
316  
317 #localhost isn't a valid MX so return false
318 43 ▶ if ($answer[$i]->exchange eq 'localhost') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer[$i]->exchange eq "localhost") 0 43 43
319 0 $reason = 'Invalid use of Localhost as an MX record';
320 0 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
321 0 return (0, $reason);
322 }
323  
324 #IF the exchange is blank and the priority is 0 and it's the last answer, let's fail
325 43 ▶ if (_blank_exchange($answer[$i]) && int($answer[$i]->preference) == 0 && $i == $#answer) {
Branch: 1 branch
BranchTrueFalseTotal
if (_blank_exchange($answer[$i]) and int $answer[$i]->preference == 0 and $i == $#answer) 2 41 43
Condition83%5/6
_blank_exchange($answer[$i]) and int $answer[$i]->preference == 0 and $i == $#answer
!ll&&!rl&&r
4102
_blank_exchange($answer[$i]) and int $answer[$i]->preference == 0
!ll&&!rl&&r
3832
MC/DC66%2/3
_blank_exchange($answer[$i]) and int $answer[$i]->preference == 0 and $i == $#answer
_blank_exchange($answer[$i]) int $answer[$i]->preference == 0 $i == $#answer
Truth table75%3/4
A and B and C
A= _blank_exchange($answer[$i])B= int $answer[$i]->preference == 0C= $i == $#answer
ABCresult
0XX0
10X0
1100
1111
326 #Test if there is a Blank MX record in the first slot Per Jan-Pieter Cornet recommendation
327 #and based on http://ietfreport.isoc.org/all-ids/draft-delany-nullmx-00.txt
328 2 $reason = 'Domain is publishing a blank MX record at Priority 0';
329 2 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 2 2
330 2 return (0, $reason);
331 }
332  
333 #resolve the exchange record
334 41 ▶ if (!_blank_exchange($answer[$i]) and $answer[$i]->exchange !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_blank_exchange($answer[$i]) or $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) 36 5 41
Condition100%3/3
_blank_exchange($answer[$i]) or $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
l!l&&r!l&&!r
3236
MC/DC100%2/2
_blank_exchange($answer[$i]) or $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
_blank_exchange($answer[$i]) $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
Truth table100%3/3
A or B
A= _blank_exchange($answer[$i])B= $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
ABresult
1X1
011
000
335 36 $packet = $res->send($answer[$i]->exchange, 'A');
336
337 36 ▶ if (!defined ($packet)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 36 36
338 #THERE WAS AN ERROR TRYING TO RESOLVE THE MAIL EXCHANGE
339 0 ▶ print "DEBUG: Test Passed by Default\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
340 0 return ($params{'resolution_problem_return'}, 'Test '.$resolution_problem_status.' due to a Resolution Problem');
341 }
342 36 ▶ print "DEBUG: $i - Number of Answers in the MX->A resolution packet is: ".$packet->header->ancount."\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 35 36
343  
344 #TEST TO SEE IF IT'S AN AAAA IPv6 RECORD - Thanks to Subramanian MOONESAMY sm@megawatt.resistor.net for pointing this out!
345 36 ▶ if (defined $packet && $packet->header->ancount < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packet and $packet->header->ancount < 1) 3 33 36
Condition66%2/3
defined $packet and $packet->header->ancount < 1
!ll&&!rl&&r
0333
MC/DC50%1/2
defined $packet and $packet->header->ancount < 1
defined $packet $packet->header->ancount < 1
Truth table66%2/3
A and B
A= defined $packetB= $packet->header->ancount < 1
ABresult
0X0
100
111
346 3 $packet = $res->send($answer[$i]->exchange, 'AAAA');
347  
348 3 ▶ if (!defined ($packet)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 3 3
349 #THERE WAS AN ERROR TRYING TO RESOLVE THE MAIL EXCHANGE
350 0 ▶ print "DEBUG: Test Passed by Default\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
351 0 return ($params{'resolution_problem_return'}, 'Test '.$resolution_problem_status.' due to a Resolution Problem');
352 }
353 3 ▶ print "DEBUG: $i - Number of Answers in the MX->AAAA resolution packet is: ".$packet->header->ancount."\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 3 3
354 }
355 }
356  
357 41 ▶ if (defined $packet && $packet->header->ancount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packet and $packet->header->ancount > 0) { } 35 6 41
Condition100%3/3
defined $packet and $packet->header->ancount > 0
!ll&&!rl&&r
5135
MC/DC100%2/2
defined $packet and $packet->header->ancount > 0
defined $packet $packet->header->ancount > 0
Truth table100%3/3
A and B
A= defined $packetB= $packet->header->ancount > 0
ABresult
0X0
100
111
358 35 @answer2 = $packet->answer;
359  
360 35 ▶ print "DEBUG: $i - Resolution type of ".$answer[$i]->exchange.": ".$answer2[0]->type."\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 34 35
361 35 ▶ if ($answer2[0]->type =~ /^A{1,4}/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($answer2[0]->type =~ /^A{1,4}/) { } 33 2 35
elsif ($answer2[0]->type eq 'CNAME') { } 2 0 2
362 33 ▶ print "DEBUG: $i - A Name Address for ".$answer[$i]->exchange.": ".$answer2[0]->address."\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 32 33
363 33 ($rv, $reason) = invalid_mx($answer2[0]->address);
364 33 ▶ if ($rv == 1 or ($rv == 2 && $i == $#answer)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv == 1 or $rv == 2 and $i == $#answer) { } 2 31 33
elsif ($rv < 1) { } 26 5 31
Condition100%6/6
$rv == 1 or $rv == 2 and $i == $#answer
l!l&&r!l&&!r
1131
$rv == 2 and $i == $#answer
!ll&&!rl&&r
2651
MC/DC100%3/3
$rv == 1 or $rv == 2 and $i == $#answer
$rv == 1 $rv == 2 $i == $#answer
Truth table100%4/4
A or B and C
A= $rv == 1B= $rv == 2C= $i == $#answer
ABCresult
1XX1
0111
00X0
0100
365 2 ▶ if ($rv == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == 2) 1 1 2
366 1 $reason .= ' - All MX Records Failed';
367 }
368 2 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 2 2
369 2 return (0, $reason);
370 } elsif ($rv < 1) {
371 26 ▶ print "DEBUG: Test Passed ".$answer2[0]->address." looks good\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 25 26
372 26 return (1, '');
373 }
374 } elsif ($answer2[0]->type eq "CNAME") {
375 2 $packet = $res->send($answer2[0]->cname,'A');
376  
377 2 ▶ if (!defined ($packet)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 2 2
378 #THERE WAS AN ERROR TRYING TO RESOLVE THE CNAME FOR THE MAIL EXCHANGE
379 0 ▶ print "DEBUG: Test Passed by Default\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
380 0 return ($params{'resolution_problem_return'}, 'Test '.$resolution_problem_status.' due to a Resolution Problem');
381 }
382  
383 2 ▶ if ($packet->header->ancount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet->header->ancount > 0) { } 2 0 2
384 2 ▶ if (defined ($packet->answer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packet->answer) 2 0 2
385 2 @answer3 = $packet->answer;
386 2 ▶ print "DEBUG: $i - CNAME Resolution of Type: ".$answer3[0]->type." - Address: ".$answer3[0]->address."\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 2 2
387 2 ▶ if ($answer3[0]->type eq "A") {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer3[0]->type eq 'A') { } 1 1 2
388 1 ($rv, $reason) = invalid_mx($answer3[0]->address);
389 1 ▶ if ($rv == 1 or ($rv == 2 && $i == $#answer)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv == 1 or $rv == 2 and $i == $#answer) { } 0 1 1
elsif ($rv < 1) { } 1 0 1
Condition33%2/6
$rv == 1 or $rv == 2 and $i == $#answer
l!l&&r!l&&!r
001
$rv == 2 and $i == $#answer
!ll&&!rl&&r
100
MC/DC0%0/3
$rv == 1 or $rv == 2 and $i == $#answer
$rv == 1 $rv == 2 $i == $#answer
Truth table25%1/4
A or B and C
A= $rv == 1B= $rv == 2C= $i == $#answer
ABCresult
1XX1
0111
00X0
0100
390 0 ▶ if ($rv == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == 2) 0 0 0
391 0 $reason .= ' - All MX Records Failed';
392 }
393 0 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
394 0 return (0, $reason);
395 } elsif ($rv < 1) {
396 1 ▶ print "DEBUG: Test Passed ".$answer3[0]->address." looks good\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 1 1
397 1 return (1,'');
398 }
399 } else {
400 #CNAMEs aren't RFC valid for MX's so if they chained two together, I'm not recursively resolving anymore levels, I'm just failing it
401 1 $reason = 'Invalid use of CNAME for MX record';
402 1 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 1 1
403 1 return (0, $reason);
404 }
405 }
406 } else {
407 0 ▶ if ($params{'allow_ip_address_as_mx'} > 0 && $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"allow_ip_address_as_mx"} > 0 and $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) 0 0 0
Condition0%0/3
$params{"allow_ip_address_as_mx"} > 0 and $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
!ll&&!rl&&r
000
MC/DC0%0/2
$params{"allow_ip_address_as_mx"} > 0 and $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
$params{"allow_ip_address_as_mx"} > 0 $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
Truth table0%0/3
A and B
A= $params{"allow_ip_address_as_mx"} > 0B= $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
ABresult
0X0
100
111
408 0 ($rv, $reason) = invalid_mx($answer[$i]->exchange);
409 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 0 0 0
410 0 return (0, $reason);
411 } else {
412 0 ▶ print "DEBUG: Test Passed - Allowing IP Address as Hostname\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
413 0 return (1, '');
414 }
415 }
416  
417 #MX RECORD IS A CNAME WHICH DOES NOT RESOLVE
418 0 $reason = "MX Record: ".$answer2[0]->cname." does not resolve";
419 0 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
420 0 return (0, $reason);
421 }
422 }
423 } else { # ! $packet->header->ancount > 0
424  
425 #IF THIS IS THE LAST MX RECORD AND THE EXCHANGE IS BLANK, WE FAIL IT
426 6 ▶ if (_blank_exchange($answer[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_blank_exchange($answer[$i])) { } 3 3 6
427 3 ▶ if ($i == $#answer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#answer) 1 2 3
428 1 $reason = 'Domain is publishing only invalid and/or blank MX records';
429 1 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 1 1
430 1 return (0, $reason);
431 }
432 } else {
433 #PERHAPS WE'LL ALLOW AN IP ADDRESS AS AN MX FOR CLOWNS WHO CONFIGURE DNS INCORRECTLY
434 3 ▶ if ($params{'allow_ip_address_as_mx'} > 0 && $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"allow_ip_address_as_mx"} > 0 and $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) 2 1 3
Condition66%2/3
$params{"allow_ip_address_as_mx"} > 0 and $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
!ll&&!rl&&r
012
MC/DC50%1/2
$params{"allow_ip_address_as_mx"} > 0 and $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
$params{"allow_ip_address_as_mx"} > 0 $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
Truth table66%2/3
A and B
A= $params{"allow_ip_address_as_mx"} > 0B= $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
ABresult
0X0
100
111
435 2 ($rv, $reason) = invalid_mx($answer[$i]->exchange);
436 2 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 1 1 2
437 1 return (0, $reason);
438 } else {
439 1 ▶ print "DEBUG: Test Passed - Allowing IP Address as Hostname\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 1 1
440 1 return (1, '');
441 }
442 }
443 }
444  
445 # Keep looping, unless this was the last answer in the MX
446 # resolution packet.
447 # XXX $packet->header->ancount, in the case of corrupt packets,
448 # may differ from the actual number of records and may return unwanted failures
449 3 ▶ if ($i == $#answer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#answer) 1 2 3
450  
451 #MX RECORD RETURNED DOES NOT RESOLVE
452 1 $reason = "MX Record: ".$answer[$i]->exchange." does not resolve";
453 1 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 1 1
454 1 return (0, $reason);
455 }
456 }
457  
458 } # for
459 }
460 } else {
461 2 ($rv, $reason) = $check_implicit_mx++;
462 }
463  
464 4 ▶ print "DEBUG: Checking Implicit MX is set to $check_implicit_mx\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 4 4
465  
466 4 ▶ if ($check_implicit_mx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_implicit_mx > 0) 4 0 4
467 4 ($rv, $reason) = check_implicit_mx($domain, $res, $params{'debug'}, $params{'resolution_problem_return'});
468 4 ▶ if (defined $rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) 4 0 4
469 4 return ($rv, $reason);
470 }
471 }
472 } else {
473 0 ▶ print "DEBUG: There was an error setting up a Net::DNS::Resolver resource\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
474 0 ▶ print "DEBUG: Test Passed by Default\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
475 0 return ($params{'resolution_problem_return'}, 'Test '.$resolution_problem_status.' due to a Resolution Problem');
476 }
477  
478 0 ▶ print "DEBUG: Test Passed\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
479 0 return (1,'');
480 }
481  
482 # Null MX (RFC 7505), recent Net::DNS versions return '.' instead of ''
483 90 ▶sub _blank_exchange {
Subroutine _blank_exchange: called CC=4 SCAR=14.6
484 90 my ($rr) = @_;
485 90 my $exchange = $rr->exchange;
486 90 ▶ return (!defined $exchange or $exchange eq '' or $exchange eq '.') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!defined($exchange) || $exchange eq '' || $exchange eq '.' ? : 11 79 90
Condition50%3/6
not defined $exchange or $exchange eq "" or $exchange eq "."
l!l&&r!l&&!r
01179
not defined $exchange or $exchange eq ""
l!l&&r!l&&!r
0090
MC/DC33%1/3
not defined $exchange or $exchange eq "" or $exchange eq "."
not defined $exchange $exchange eq "" $exchange eq "."
Truth table50%2/4
not(A) or B or C
A= not defined $exchangeB= $exchange eq ""C= $exchange eq "."
ABCresult
1XX1
01X1
0011
0000
487 }
488  
489 4 ▶sub check_implicit_mx {
Subroutine check_implicit_mx: called CC=26 SCAR=41.7
Pod undocumented
490 4 my ($SenderDomain, $res, $debug, $resolution_problem_return) = @_;
491
492 4 my ($rv, $reason, $packet, @answer, @answer2, $resolution_problem_status);
493  
494 #CONSTANTS/SETTABLE OPTIONS
495 4 ▶ $resolution_problem_return ||= $RESOLUTION_PROBLEM_RETURN;
Condition33%1/3
$resolution_problem_return ||= $Net::validMX::RESOLUTION_PROBLEM_RETURN
l!l&&r!l&&!r
400
MC/DC0%0/2
$resolution_problem_return ||= $Net::validMX::RESOLUTION_PROBLEM_RETURN
$resolution_problem_return $Net::validMX::RESOLUTION_PROBLEM_RETURN
Truth table33%1/3
A ||= B
A= $resolution_problem_returnB= $Net::validMX::RESOLUTION_PROBLEM_RETURN
ABresult
1X1
011
000
496  
497 4 ▶ if ($resolution_problem_return > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolution_problem_return > 0) { } 4 0 4
498 4 $resolution_problem_status = 'Passed';
499 } else {
500 0 $resolution_problem_status = 'Failed';
501 }
502  
503 4 ▶ print "DEBUG: Checking for Implicit MX Records\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 4 4
504 #NO MX RECORDS RETURNED - CHECK FOR IMPLICIT MX RECORD BY A RECORD per Jan-Pieter Cornet recommendation
505 4 $packet = $res->send($SenderDomain,'A');
506 4 ▶ if (!defined ($packet)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 4 4
507 #THERE WAS AN ERROR - NO IMPLICIT A RECORD COULD BE RESOLVED
508 0 ▶ print "DEBUG: Test Passed by Default\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
509 0 return ($resolution_problem_return, 'Test '.$resolution_problem_status.' due to a Resolution Problem');
510 }
511  
512 4 ▶ print "DEBUG: Number of Answers in the Implicit A record resolution packet is: ".$packet->header->ancount."\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 4 4
513 4 ▶ if ($packet->header->ancount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet->header->ancount > 0) { } 3 1 4
514 3 @answer = $packet->answer;
515 3 ▶ if ($answer[0]->type eq "A") {
Branch: 2 branches
BranchTrueFalseTotal
if ($answer[0]->type eq 'A') { } 1 2 3
elsif ($answer[0]->type eq 'CNAME') { } 2 0 2
516 1 ▶ print "DEBUG: $SenderDomain has no MX Records - Using Implicit A Record: ".$answer[0]->address."\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
517 1 ($rv, $reason) = invalid_mx($answer[0]->address);
518 1 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 0 1 1
519 0 ▶ print "DEBUG: Test Failed - $reason\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
520 0 return (0, $reason);
521 } else {
522 1 ▶ print "DEBUG: Test Passed ".$answer[0]->address." looks good\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
523 1 return (1, '');
524 }
525 } elsif ($answer[0]->type eq "CNAME") {
526 #IS THIS REALLY A NECESSARY TEST? SHOULD WE BE TESTING FOR IMPLICIT CNAME RECORDS?
527 2 ▶ print "DEBUG: $SenderDomain has no MX Records - Using CNAME to Check for Implicit A Record: ".$answer[0]->cname."\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
528 2 $packet = $res->send($answer[0]->cname,'A');
529  
530 2 ▶ if (!defined ($packet)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 2 2
531 #THERE WAS AN ERROR TRYING TO RESOLVE THE CNAME FOR THE MAIL EXCHANGE
532 0 ▶ print "DEBUG: Test Passed by Default\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
533 0 return (1, '');
534 }
535  
536 2 ▶ if ($packet->header->ancount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet->header->ancount > 0) 2 0 2
537 2 ▶ if (defined ($packet->answer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packet->answer) 2 0 2
538 2 @answer2 = $packet->answer;
539 2 ▶ if ($answer2[0]->type eq "A") {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer2[0]->type eq 'A') { } 1 1 2
540 1 ▶ print "DEBUG: CNAME Resolution of Type: ".$answer2[0]->type." - Address: ".$answer2[0]->address."\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
541 1 ($rv, $reason) = invalid_mx($answer2[0]->address);
542 1 ▶ if ($rv > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv > 0) { } 0 1 1
543 0 ▶ print "DEBUG: Test Failed - $reason\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
544 0 return (0, $reason);
545 } else {
546 1 ▶ print "DEBUG: Test Passed ".$answer2[0]->address." looks good\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
547 1 return (1, '');
548 }
549 } else {
550 #CNAMEs aren't RFC valid for MX's so if they chained two together, I'm not recursively resolving anymore levels, I'm just failing it
551 1 $reason = 'Invalid use of CNAME for Implicit MX record';
552 1 ▶ print "DEBUG: Test Failed - $reason\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
553 1 return (0, $reason);
554 }
555 }
556 }
557 }
558 } else {
559 1 $reason = "No MX or A Records Exist for $SenderDomain";
560 1 ▶ print "DEBUG: Test Failed - $reason\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
561 1 return (0, $reason);
562 }
563 0 return;
564 }
565  
566 38 ▶sub invalid_mx {
Subroutine invalid_mx: called CC=18 SCAR=36.1
Pod undocumented
567 38 my ($ip) = @_;
568 38 my ($flag_intranets);
569  
570 #UPDATED MORE ON 11-18-2011 based on RFC 5735
571  
572 #0/8, 255/8, 127/8 aren't a valid MX so return false - added per Matthew van Eerde recomendation
573 38 ▶ if ($ip =~ /^(255|127|0)\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^(255|127|0)\./) 0 38 38
574 0 return (1, "Invalid use of 0/8, 255/8 or 127/8 ($ip) as an MX record");
575 }
576  
577 38 $flag_intranets = $FLAG_INTRANETS;
578  
579 #10/8
580 38 ▶ if ($flag_intranets && $ip =~ /^10\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_intranets and $ip =~ /^10\./) 3 35 38
Condition66%2/3
$flag_intranets and $ip =~ /^10\./
!ll&&!rl&&r
0353
MC/DC50%1/2
$flag_intranets and $ip =~ /^10\./
$flag_intranets $ip =~ /^10\./
Truth table66%2/3
A and B
A= $flag_intranetsB= $ip =~ /^10\./
ABresult
0X0
100
111
581 3 return (2, "Invalid use of private IP (e.g. $ip) range for MX");
582 }
583 #172.16/12 - Fixed per Matthen van Eerde
584 35 ▶ if ($flag_intranets && $ip =~ /^172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_intranets and $ip =~ /^172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\./) 0 35 35
Condition33%1/3
$flag_intranets and $ip =~ /^172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\./
!ll&&!rl&&r
0350
MC/DC0%0/2
$flag_intranets and $ip =~ /^172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\./
$flag_intranets $ip =~ /^172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\./
Truth table33%1/3
A and B
A= $flag_intranetsB= $ip =~ /^172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\./
ABresult
0X0
100
111
585 0 return (2, "Invalid use of private IP (e.g. $ip) range for MX");
586 }
587 #192.168/16
588 35 ▶ if ($flag_intranets && $ip =~ /^192\.168\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_intranets and $ip =~ /^192\.168\./) 4 31 35
Condition66%2/3
$flag_intranets and $ip =~ /^192\.168\./
!ll&&!rl&&r
0314
MC/DC50%1/2
$flag_intranets and $ip =~ /^192\.168\./
$flag_intranets $ip =~ /^192\.168\./
Truth table66%2/3
A and B
A= $flag_intranetsB= $ip =~ /^192\.168\./
ABresult
0X0
100
111
589 4 return (2, "Invalid use of private IP (e.g. $ip) range for MX");
590 }
591  
592 #fc00::/7
593 31 ▶ if ($flag_intranets && $ip =~ /^fc00\:0\:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_intranets and $ip =~ /^fc00\:0\:/i) 0 31 31
Condition33%1/3
$flag_intranets and $ip =~ /^fc00\:0\:/i
!ll&&!rl&&r
0310
MC/DC0%0/2
$flag_intranets and $ip =~ /^fc00\:0\:/i
$flag_intranets $ip =~ /^fc00\:0\:/i
Truth table33%1/3
A and B
A= $flag_intranetsB= $ip =~ /^fc00\:0\:/i
ABresult
0X0
100
111
594 0 return (2, "Invalid use of unique local address (e.g. $ip) range for MX");
595 }
596  
597 #fd00::/8
598 31 ▶ if ($flag_intranets && $ip =~ /^fd00\:0\:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_intranets and $ip =~ /^fd00\:0\:/i) 0 31 31
Condition33%1/3
$flag_intranets and $ip =~ /^fd00\:0\:/i
!ll&&!rl&&r
0310
MC/DC0%0/2
$flag_intranets and $ip =~ /^fd00\:0\:/i
$flag_intranets $ip =~ /^fd00\:0\:/i
Truth table33%1/3
A and B
A= $flag_intranetsB= $ip =~ /^fd00\:0\:/i
ABresult
0X0
100
111
599 0 return (2, "Invalid use of private IP (e.g. $ip) range for MX");
600 }
601  
602 #DHCP auto-discover added per Matthew van Eerde recomendation 169.254/16
603 31 ▶ if ($ip =~ /^169\.254\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^169\.254\./) 0 31 31
604 0 return (1, "Invalid use of a DHCP auto-discover IP range ($ip) as an MX record");
605 }
606  
607 #IPv6 link-local addresses fe80::/10
608 31 ▶ if ($ip =~ /^fe80\:0\:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^fe80\:0\:/i) 1 30 31
609 1 return (1, "Invalid use of a link-local IP range ($ip) as an MX record");
610 }
611  
612 #Multicast 224/8 through 239/8 added per Matthew van Eerde recomendation
613 30 ▶ if ($ip =~ /^(224|225|226|227|228|229|230|231|232|233|234|235|236|237|238|239)\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^(224|225|226|227|228|229|230|231|232|233|234|235|236|237|238|239)\./) 0 30 30
614 0 return (1, "Invalid use of a Multicast IP range ($ip) as an MX record");
615 }
616  
617 #Experimental block - Former Class E - 240.0.0.0/4 courtesy of Mark Damrose
618 30 ▶ if ($ip =~ /^2[45]\d\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^2[45]\d\./) 0 30 30
619 0 return (1, "Invalid use of an experimental IP ($ip) as an MX record");
620 }
621  
622 #Reserved for benchmark tests of interconnect devices 192.18.0.0/15 courtesy of Mark Damrose
623 30 ▶ if ($ip =~ /^192\.1[89]\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^192\.1[89]\./) 0 30 30
624 0 return (1, "Invalid use of a reserved IP ($ip) as an MX record");
625 }
626  
627 #Reserved for documentation or published examples 192.0.2.0/24 courtesy of Mark Damrose
628 30 ▶ if ($ip =~ /^192\.0\.2\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^192\.0\.2\./) 0 30 30
629 0 return (1, "Invalid use of a reserved IP ($ip) as an MX record");
630 }
631  
632
633 30 return (0,'');
634 }
635  
636 37 ▶sub int_to_truefalse {
Subroutine int_to_truefalse: called CC=2 SCAR=6.9
Pod undocumented
637 37 my ($int) = @_;
638  
639 37 ▶ if ($int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int) { } 25 12 37
640 25 return "True";
641 } else {
642 12 return "False";
643 }
644 }
645  
646 3 ▶sub check_email_and_mx {
Subroutine check_email_and_mx: called CC=6 SCAR=18.9
Pod documented
647 3 my ($email) = @_;
648 3 my ($rv, $fail_reason, $status, $debug);
649  
650 3 $debug = 0;
651  
652 3 ▶ $email || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$email or return 0 3 0 3
653
654 3 ▶ print "DEBUG: e-mail address is: $email<br>\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
655
656 # SANITIZE THE E-MAIL ADDRESS OF SPACES
657 3 $email =~ s/ //g;
658  
659 # CHECK FOR INCOMPLETE ADDRESSES AT LARGE ISPS
660 3 $email =~ s/\@aol\.?$/\@aol.com/i;
661 3 $email =~ s/\@hotmail\.?$/\@hotmail.com/i;
662 3 $email =~ s/\@gmail\.?$/\@gmail.com/i;
663  
664 3 ▶ print "DEBUG: e-mail address is now: $email<br>\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
665  
666 # CHECK FOR A VALIDLY CONSTRUCTED E-MAIL ADDRESS
667 3 ($rv) = Net::validMX::check_email_validity($email);
668
669 3 ▶ if ($rv < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv < 1) 0 3 3
670 0 return($rv, "Failed check_email_validity", $email);
671 }
672  
673 # CHECK FOR VALID MX RECORD
674 3 ($rv, $fail_reason) = Net::validMX::check_valid_mx($email);
675  
676 3 ▶ if ($rv < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv < 1) 0 3 3
677 0 return($rv, $fail_reason, $email);
678 }
679  
680 3 return($rv, "Passed", $email);
681 }
682  
683 11 ▶sub check_email_validity {
Subroutine check_email_validity: called CC=13 SCAR=30.0
Pod documented
684 11 my ($email) = @_;
685 11 my ($local);
686  
687 #allows an email address that contains -()/!#$%&*+~. A through Z a through Z and 0 through 9 in a format of [valid]@([valid].[valid]...).[valid]. = will also be
688 #allowed in the username. Thanks to Paul Whittney for reporting the issue.
689  
690 11 ▶ return 0 if not defined $email;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $email 0 11 11
691  
692 #PER WIKIPEDIA
693 #Per Wikipedia:
694  
695 #The format of email addresses is local-part@domain where the local-part may be up to 64 characters long and the domain name may have a maximum of 253 characters - but the maximum 256 characters length of a forward or reverse path restricts the entire email address to be no more than 254 characters.[1] - formally defined in RFC 5322 (sections 3.2.3 and 3.4.1) and by RFC 5321.
696
697 #Can't have two dots
698 11 ▶ if ($email =~ /\.\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($email =~ /\.\./) 0 11 11
699 0 return 0;
700 }
701  
702 #Can't be longer than 254 chars
703 11 ▶ if (length($email) > 254) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $email > 254) 0 11 11
704 0 return 0;
705 }
706  
707 #Can't end in a period
708 11 ▶ if ($email =~ /\.$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($email =~ /\.$/) 0 11 11
709 0 return 0;
710 }
711  
712 11 ▶ if ($email =~ /^(.*)@[-()\/!#$%&*+~_A-Za-z0-9\.]+\.[-()\/!#$%&*+~_A-Za-z0-9\.]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($email =~ m[^(.*)\@[-()/!#$%&*+~_A-Za-z0-9\.]+\.[-()/!#$%&*+~_A-Za-z0-9\.]+$]) 8 3 11
713
714 8 $local = $1;
715
716 #check local length
717 8 ▶ if (length($local) > 64) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $local > 64) 2 6 8
718 2 return 0;
719 }
720 #no need to check if domain is over 253 chars, as it would not pass both overall length and regex if it was
721  
722 # per RFC 3696 section 3 the local part of an address cannot begin or end with a period
723 6 ▶ if ($local =~ /^\./ or $local =~ /\.$/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local =~ /^\./ or $local =~ /\.$/g) 0 6 6
Condition33%1/3
$local =~ /^\./ or $local =~ /\.$/g
l!l&&r!l&&!r
006
MC/DC0%0/2
$local =~ /^\./ or $local =~ /\.$/g
$local =~ /^\./ $local =~ /\.$/g
Truth table33%1/3
A or B
A= $local =~ /^\./B= $local =~ /\.$/g
ABresult
1X1
011
000
724 0 return 0;
725 }
726
727 # PURGE BACKSLASHES
728 6 $local =~ s/\\//g;
729
730 # per RFC 3696 section 3 the local part of the email can be quoted, which allows any character to appear if inside quotes
731 # PURGE BEGINNING AND END QUOTE IF IT CONTAINS QUOTES
732 6 ▶ if ($local =~ /"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local =~ /"/) 1 5 6
733 1 ▶ if($local =~ /^".*"$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local =~ /^".*"$/) 0 1 1
734 0 $local =~ s/^"//g;
735 0 $local =~ s/"$//g;
736 }
737
738 # IF IT STILL CONTAINS A QUOTE, IT IS INVALID, OTHERWISE THE LOCAL PART IS VALID
739 1 ▶ if ($local =~ /"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local =~ /"/) { } 1 0 1
740 1 return 0;
741 } else {
742 0 return 1;
743 }
744 }
745
746 # check for allowed characters, per RFC 3696 section 3
747 5 ▶ if ($local =~ /^[\@'-`\/!\?=#\$\%&*+~_A-Za-z0-9\.{}|]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local =~ m[^[\@'-`/!\?=#\$\%&*+~_A-Za-z0-9\.{}|]+$]) { } 5 0 5
748 5 return 1;
749 } else {
750 0 return 0;
751 }
752  
753 }
754 3 return 0;
755  
756 }
757  
758 #get domain name from an email address
759 43 ▶sub get_domain_from_email {
Subroutine get_domain_from_email: called CC=3 SCAR=11.0
Pod documented
760 43 my ($email, %params) = @_;
761  
762 43 my ($domain, $local);
763  
764 43 $domain = $email;
765  
766 #REMOVE ANY LEADING/TRAILING <>'s
767 43 $domain =~ s/(^<|>$)//g;
768 #REMOVE ANY LEADING/TRAILING SPACE'S
769 43 $domain =~ s/^ *//g;
770 43 $domain =~ s/ *$//g;
771 #REMOVE EVERYTHING UP TO THE @ SYMBOL
772 43 $domain =~ s/(.*)\@//g;
773  
774 43 $local = $1;
775  
776 43 ▶ print "\nDEBUG: Extracted Sender Domain: $domain / Local: $local from $params{'email'}\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 43 43
777  
778 43 ▶ return wantarray ? ($local,$domain) : $domain;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 42 43
779 }
780  
781 0 ▶sub dns_lookup {
Subroutine dns_lookup: not called CC=2 SCAR=17.9
Pod undocumented
782 0 my ($domain, $type) = @_;
783 0 my ($dns, $query);
784  
785 0 $dns = Net::DNS::Resolver->new;
786 0 $query = $dns->search($domain, $type);
787 0 ▶ if ($query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query) { } 0 0 0
788 0 return $query->answer;
789 } else {
790 0 warn "Error performing $type query for $domain! ". $dns->errorstring;
791 }
792 }
793  
794 0 ▶sub check_spf_for_domain {
Subroutine check_spf_for_domain: not called CC=10 SCAR=47.0
Pod documented
795 0 my ($domain, %params) = @_;
796 0 my ($dns, $query, $spf_line, @clauses, $found_spf);
797  
798 0 $dns = Net::DNS::Resolver->new;
799 0 $query = $dns->search($domain, 'TXT');
800 0 ▶ if (not $query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
801 0 warn "Error performing TXT query for $domain! ". $dns->errorstring;
802 0 return ("suspect", "no TXT record found");
803 }
804  
805 0 foreach my $result ($query->answer) {
806 0 ▶ next unless $result->type eq 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
unless $result->type eq "TXT" 0 0 0
807 0 $spf_line = $result->txtdata;
808  
809 0 ▶ if ($spf_line =~ /^v=spf[12]/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf_line =~ /^v=spf[12]/i) 0 0 0
810 0 $found_spf++;
811  
812 # split into clauses
813 0 @clauses = split / /, $spf_line;
814  
815 0 foreach my $clause (@clauses) {
816 # ignore clauses that reject email - only false accepts are good spam indicators
817 0 ▶ next if $clause =~ /^[-~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $clause =~ /^[-~]/ 0 0 0
818 0 ▶ if ($clause =~ /^.?all/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause =~ /^.?all/) 0 0 0
819 # if accepting email from all, rule is clearly useless
820 0 return ("bad", "use of universal pass rule $clause");
821 }
822 }
823 }
824 }
825  
826 0 ▶ if ($found_spf == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($found_spf == 0) { } 0 0 0
elsif ($found_spf > 1) { } 0 0 0
827 0 return ("suspect", "no TXT record matching SPF format found");
828 } elsif ($found_spf > 1) {
829 0 return ("suspect", "multiple TXT records matching SPF format found");
830 }
831  
832 0 return ("valid", undef);
833 }
834  
835 8 1;
836  
837 __END__
↑ Summary blib/script/check_email_and_mx.pl »