lib/Net/validMX.pm

filter: Statementstmt 70.9% 242 / 341 Branchbran 54.7% 162 / 296 Conditioncond 47.2% 34 / 72 MC/DCmcdc 22.2% 10 / 45 Subroutinesub 73.6% 14 / 19 Podpod 35.7% 5 / 14 totaltotal 59.3% 467 / 787 CC 175 SCAR 55.3
CC
175
Cov
62%
check_valid_mx
480.0
check_spf_for_domain
110.0
check_implicit_mx
64.7
CRAP 252.0
↑ 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 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 7 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 7 ▶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 7 $QUERY_TIMEOUT);
143  
144 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
145 7 require DynaLoader;
146 7 require Exporter;
147  
148 7 @ISA = qw(Exporter DynaLoader);
149 7 $VERSION = '2.5.2';
150 7 $DEBUG = 0;
151 7 $ALLOW_IP_ADDRESS_AS_MX = 1;
152 7 $FLAG_INTRANETS = 1;
153 7 $RESOLUTION_PROBLEM_RETURN = 1;
154 7 $QUERY_TIMEOUT = 4;
155 }
156  
157 0 ▶sub version { $VERSION; }
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
158  
159 7 @EXPORT_OK = qw(check_valid_mx get_output_result check_email_and_mx check_email_validity get_domain_from_email);
160  
161 1 ▶sub new {
Subroutine new: called CC=11 SCAR=31.6
Pod undocumented
162 1 my $self = bless {}, shift;
163  
164 1 ▶ $DEBUG = $self->{'debug'} if (defined $self->{'debug'} and $self->{'debug'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"debug"} and $self->{"debug"} ne "" 0 1 1
Condition33%1/3
defined $self->{"debug"} and $self->{"debug"} ne ""
!ll&&!rl&&r
100
MC/DC0%0/2
defined $self->{"debug"} and $self->{"debug"} ne ""
defined $self->{"debug"} $self->{"debug"} ne ""
Truth table33%1/3
A and B
A= defined $self->{"debug"}B= $self->{"debug"} ne ""
ABresult
0X0
100
111
165 1 ▶ $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 "" 0 1 1
Condition33%1/3
defined $self->{"allow_ip_address_as_mx"} and $self->{"allow_ip_address_as_mx"} ne ""
!ll&&!rl&&r
100
MC/DC0%0/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 table33%1/3
A and B
A= defined $self->{"allow_ip_address_as_mx"}B= $self->{"allow_ip_address_as_mx"} ne ""
ABresult
0X0
100
111
166 1 ▶ $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 1 1
Condition33%1/3
defined $self->{"flag_intranets"} and $self->{"flag_intranets"} ne ""
!ll&&!rl&&r
100
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
167 1 ▶ $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 1 1
Condition33%1/3
defined $self->{"resolution_problem_return"} and $self->{"resolution_problem_return"} ne ""
!ll&&!rl&&r
100
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
168 1 ▶ $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 1 1
Condition33%1/3
defined $self->{"query_timeout"} and $self->{"query_timeout"} ne ""
!ll&&!rl&&r
100
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
169  
170 1 return $self;
171 }
172  
173 0 ▶sub get_debug {
Subroutine get_debug: not called CC=1 SCAR=6.9
Pod undocumented
174 0 return $DEBUG;
175 }
176  
177 0 ▶sub set_debug {
Subroutine set_debug: not called CC=1 SCAR=6.9
Pod undocumented
178 0 my $debug = shift;
179 0 $DEBUG = $debug;
180 }
181  
182 37 ▶sub get_output_result {
Subroutine get_output_result: called CC=2 SCAR=6.9
Pod undocumented
183 37 my ($email, $rv, $reason) = @_;
184 37 my ($output);
185  
186 37 $output = "$email\n\tValid MX? ".Net::validMX::int_to_truefalse($rv);
187 37 ▶ if ($reason ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reason ne "") 13 24 37
188 13 $output .= " - $reason";
189 }
190 37 $output .= "\n\n";
191  
192 37 return $output;
193 }
194  
195 36 ▶sub check_valid_mx {
Subroutine check_valid_mx: called CC=93 SCAR=61.7
Pod documented
196 #Based on Idea from Les Miksell and much input from Jan Pieter Cornet
197 #KAM 9-12-05 updated 10-24-05 & 11-3-05.
198 #takes the email address, extracts the domain name and performs multiple MX tests to see if the domain has valid
199 #MX exchange records
200  
201 36 my ($res, $packet, @answer, $domain, @answer2, @answer3, $rv, $reason, $i, @unsorted_answer);
202 36 my ($check_implicit_mx, %params, $self, $ref, $resolution_problem_status);
203  
204 #print "DEBUG: ref for \$_[0] ".ref($_[0]). "\n";
205 #IN OO INSTEAD OF PROCEDURAL MODE?
206 36 ▶ if (uc(ref($_[0])) eq 'NET::VALIDMX') {
Branch: 1 branch
BranchTrueFalseTotal
if (uc ref $_[0] eq "NET::VALIDMX") 1 35 36
207 1 $self = shift(@_);
208 #foreach $ref (keys %$self) {
209 # print "DEBUG: OO MODE - $ref: $self->{$ref} \n";
210 #}
211 }
212  
213 #DID WE RECEIVE A HASH INSTEAD OF A SINGLE EMAIL?
214 36 ▶ if ($#_ % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ % 2 == 0) { } 34 2 36
215 34 ($params{'email'}) = @_;
216 } else {
217 2 %params = @_;
218 }
219  
220 36 ▶ $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." 36 0 36
Condition33%1/3
$params{"email"} or $params{"sender"}
l!l&&r!l&&!r
3600
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
221  
222 #CONSTANTS / SETTABLE OPTIONS
223 36 ▶ $params{'debug'} = $DEBUG unless (defined $params{'debug'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"debug"} 35 1 36
224 36 ▶ $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"} 35 1 36
225 36 ▶ $params{'resolution_problem_return'} = $RESOLUTION_PROBLEM_RETURN unless (defined $params{'resolution_problem_return'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"resolution_problem_return"} 36 0 36
226 36 ▶ $params{'query_timeout'} = $QUERY_TIMEOUT unless (defined $params{'query_timeout'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"query_timeout"} 36 0 36
227  
228 36 ▶ if ($params{'resolution_problem_return'} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'resolution_problem_return'} > 0) { } 36 0 36
229 36 $resolution_problem_status = 'Passed';
230 } else {
231 0 $resolution_problem_status = 'Failed';
232 }
233  
234 36 ▶ print "DEBUG: function debug setting is $params{'debug'}\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 35 36
235 36 ▶ 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 35 36
236 36 ▶ print "DEBUG: function resolution_problem_return setting is $params{'resolution_problem_return'}\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 35 36
237 36 ▶ print "DEBUG: function query_timeout setting is $params{'query_timeout'}\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 35 36
238  
239 #FLAGS - I THINK THIS HAS A LOGIC ISSUE - I LIKELY MEANT ALLOW_IMPLICIT_MX as an option FIX
240 36 $check_implicit_mx = 0;
241  
242 #Setup a DNS Resolver Resource
243 36 $res = Net::DNS::Resolver->new;
244  
245 36 ▶ if (defined ($res)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) { } 36 0 36
246 36 $check_implicit_mx = 0;
247 36 $res->defnames(0); #Turn off appending the default domain for names that have no dots just in case
248 36 $res->searchlist(); #Set the search list to undefined just in case
249  
250 #We have also set the default timeout to only 4 seconds which means we might get network
251 #delays which we do not want to handle as an error.
252 36 $res->tcp_timeout($params{'query_timeout'}); #Number of Seconds before query will fail
253 36 $res->udp_timeout($params{'query_timeout'}); #Number of Seconds before query will fail
254  
255 #Strip domain name from an email address
256 36 $domain = get_domain_from_email($params{'email'});
257  
258 #Deny Explicit IP Address Domains
259 36 ▶ if ($domain =~ /^\[.*\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain =~ /^\[.*\]$/) 1 35 36
260 1 $reason = "Use of IP Address $domain instead of a hostname is not allowed";
261 1 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 1 1
262 1 return (0, $reason);
263 }
264  
265 #Perform the DNS Query - Changed to Send instead of Query method to utilize the ancount method
266 35 $packet = $res->send($domain,'MX');
267  
268 #Net::DNS::Resolver had an error
269 35 ▶ if (!defined $packet) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 35 35
270 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
271 0 ▶ print "DEBUG: Test Passed by Default\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
272 0 return($params{'resolution_problem_return'}, "Test $resolution_problem_status due to a Resolution Problem retrieving the MX Records");
273 }
274  
275 35 ▶ 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 34 35
276 #Parse the Query
277 35 ▶ if ($packet->header->ancount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet->header->ancount > 0) { } 33 2 35
278 33 ▶ if (defined ($packet->answer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packet->answer) 33 0 33
279 33 @answer = $packet->answer;
280  
281 33 ▶ for ($i = 0; $i < scalar(@answer); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @answer) 43 33 76
282 43 ▶ if ($answer[$i]->type ne 'MX') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer[$i]->type ne 'MX') { } 4 39 43
283 #DISCARD ANSWER IF THE RECORD IS NOT AN MX RECORD SUCH AS THE CNAME FOR londo.cysticercus.com
284 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
285 } else {
286 39 push @unsorted_answer, $answer[$i];
287 }
288 }
289  
290 33 undef @answer;
291  
292 33 ▶ 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 32 33
293 33 ▶ if (scalar(@unsorted_answer) < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @unsorted_answer < 1) { } 2 31 33
294 2 $check_implicit_mx++;
295 } else {
296 #Sort to put answers into ascending order by mail exchange preference
297 31 @answer = sort {$a->preference <=> $b->preference} @unsorted_answer;
298 }
299  
300 #LOOP THROUGH THE ANSWERS WE HAVE
301 33 ▶ for ($i = 0; $i < scalar(@answer); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @answer) 38 2 40
302 38 undef $packet;
303 38 ▶ 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 37 38
304  
305 #localhost isn't a valid MX so return false
306 38 ▶ if ($answer[$i]->exchange eq 'localhost') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer[$i]->exchange eq "localhost") 0 38 38
307 0 $reason = 'Invalid use of Localhost as an MX record';
308 0 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
309 0 return (0, $reason);
310 }
311  
312 #IF the exchange is blank and the priority is 0 and it's the last answer, let's fail
313 38 ▶ if ($answer[$i]->exchange eq '' && int($answer[$i]->preference) == 0 && $i == $#answer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer[$i]->exchange eq "" and int $answer[$i]->preference == 0 and $i == $#answer) 0 38 38
Condition33%2/6
$answer[$i]->exchange eq "" and int $answer[$i]->preference == 0 and $i == $#answer
!ll&&!rl&&r
3800
$answer[$i]->exchange eq "" and int $answer[$i]->preference == 0
!ll&&!rl&&r
3800
MC/DC0%0/3
$answer[$i]->exchange eq "" and int $answer[$i]->preference == 0 and $i == $#answer
$answer[$i]->exchange eq "" int $answer[$i]->preference == 0 $i == $#answer
Truth table25%1/4
A and B and C
A= $answer[$i]->exchange eq ""B= int $answer[$i]->preference == 0C= $i == $#answer
ABCresult
0XX0
10X0
1100
1111
314 #Test if there is a Blank MX record in the first slot Per Jan-Pieter Cornet recommendation
315 #and based on http://ietfreport.isoc.org/all-ids/draft-delany-nullmx-00.txt
316 0 $reason = 'Domain is publishing a blank MX record at Priority 0';
317 0 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
318 0 return (0, $reason);
319 }
320  
321 #resolve the exchange record
322 38 ▶ if ($answer[$i]->exchange ne '' and $answer[$i]->exchange !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer[$i]->exchange ne "" and not $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) 36 2 38
Condition66%2/3
$answer[$i]->exchange ne "" and not $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
!ll&&!rl&&r
0236
MC/DC50%1/2
$answer[$i]->exchange ne "" and not $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
$answer[$i]->exchange ne "" not $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
Truth table66%2/3
A and not(B)
A= $answer[$i]->exchange ne ""B= not $answer[$i]->exchange =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
ABresult
0X0
100
111
323 36 $packet = $res->send($answer[$i]->exchange, 'A');
324
325 36 ▶ if (!defined ($packet)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 36 36
326 #THERE WAS AN ERROR TRYING TO RESOLVE THE MAIL EXCHANGE
327 0 ▶ print "DEBUG: Test Passed by Default\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
328 0 return ($params{'resolution_problem_return'}, 'Test '.$resolution_problem_status.' due to a Resolution Problem');
329 }
330 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
331  
332 #TEST TO SEE IF IT'S AN AAAA IPv6 RECORD - Thanks to Subramanian MOONESAMY sm@megawatt.resistor.net for pointing this out!
333 36 ▶ if (defined $packet && $packet->header->ancount < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packet and $packet->header->ancount < 1) 7 29 36
Condition66%2/3
defined $packet and $packet->header->ancount < 1
!ll&&!rl&&r
0297
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
334 7 $packet = $res->send($answer[$i]->exchange, 'AAAA');
335  
336 7 ▶ if (!defined ($packet)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 7 7
337 #THERE WAS AN ERROR TRYING TO RESOLVE THE MAIL EXCHANGE
338 0 ▶ print "DEBUG: Test Passed by Default\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
339 0 return ($params{'resolution_problem_return'}, 'Test '.$resolution_problem_status.' due to a Resolution Problem');
340 }
341 7 ▶ 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 7 7
342 }
343 }
344  
345 38 ▶ if (defined $packet && $packet->header->ancount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packet and $packet->header->ancount > 0) { } 31 7 38
Condition100%3/3
defined $packet and $packet->header->ancount > 0
!ll&&!rl&&r
2531
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
346 31 @answer2 = $packet->answer;
347  
348 31 ▶ print "DEBUG: $i - Resolution type of ".$answer[$i]->exchange.": ".$answer2[0]->type."\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 30 31
349 31 ▶ if ($answer2[0]->type =~ /^A{1,4}/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($answer2[0]->type =~ /^A{1,4}/) { } 29 2 31
elsif ($answer2[0]->type eq 'CNAME') { } 2 0 2
350 29 ▶ 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 28 29
351 29 ($rv, $reason) = invalid_mx($answer2[0]->address);
352 29 ▶ if ($rv == 1 or ($rv == 2 && $i == $#answer)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv == 1 or $rv == 2 and $i == $#answer) { } 2 27 29
elsif ($rv < 1) { } 22 5 27
Condition100%6/6
$rv == 1 or $rv == 2 and $i == $#answer
l!l&&r!l&&!r
1127
$rv == 2 and $i == $#answer
!ll&&!rl&&r
2251
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
353 2 ▶ if ($rv == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == 2) 1 1 2
354 1 $reason .= ' - All MX Records Failed';
355 }
356 2 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 2 2
357 2 return (0, $reason);
358 } elsif ($rv < 1) {
359 22 ▶ print "DEBUG: Test Passed ".$answer2[0]->address." looks good\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 1 21 22
360 22 return (1, '');
361 }
362 } elsif ($answer2[0]->type eq "CNAME") {
363 2 $packet = $res->send($answer2[0]->cname,'A');
364  
365 2 ▶ if (!defined ($packet)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 2 2
366 #THERE WAS AN ERROR TRYING TO RESOLVE THE CNAME FOR THE MAIL EXCHANGE
367 0 ▶ print "DEBUG: Test Passed by Default\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
368 0 return ($params{'resolution_problem_return'}, 'Test '.$resolution_problem_status.' due to a Resolution Problem');
369 }
370  
371 2 ▶ if ($packet->header->ancount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet->header->ancount > 0) { } 2 0 2
372 2 ▶ if (defined ($packet->answer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packet->answer) 2 0 2
373 2 @answer3 = $packet->answer;
374 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
375 2 ▶ if ($answer3[0]->type eq "A") {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer3[0]->type eq 'A') { } 1 1 2
376 1 ($rv, $reason) = invalid_mx($answer3[0]->address);
377 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
378 0 ▶ if ($rv == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == 2) 0 0 0
379 0 $reason .= ' - All MX Records Failed';
380 }
381 0 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
382 0 return (0, $reason);
383 } elsif ($rv < 1) {
384 1 ▶ print "DEBUG: Test Passed ".$answer3[0]->address." looks good\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 1 1
385 1 return (1,'');
386 }
387 } else {
388 #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
389 1 $reason = 'Invalid use of CNAME for MX record';
390 1 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 1 1
391 1 return (0, $reason);
392 }
393 }
394 } else {
395 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
396 0 ($rv, $reason) = invalid_mx($answer[$i]->exchange);
397 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 0 0 0
398 0 return (0, $reason);
399 } else {
400 0 ▶ print "DEBUG: Test Passed - Allowing IP Address as Hostname\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
401 0 return (1, '');
402 }
403 }
404  
405 #MX RECORD IS A CNAME WHICH DOES NOT RESOLVE
406 0 $reason = "MX Record: ".$answer2[0]->cname." does not resolve";
407 0 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
408 0 return (0, $reason);
409 }
410 }
411 } else { # ! $packet->header->ancount > 0
412  
413 #IF THIS IS THE LAST MX RECORD AND THE EXCHANGE IS BLANK, WE FAIL IT
414 7 ▶ if ($answer[$i]->exchange eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer[$i]->exchange eq '') { } 0 7 7
415 0 ▶ if ($i == $#answer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#answer) 0 0 0
416 0 $reason = 'Domain is publishing only invalid and/or blank MX records';
417 0 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
418 0 return (0, $reason);
419 }
420 } else {
421 #PERHAPS WE'LL ALLOW AN IP ADDRESS AS AN MX FOR CLOWNS WHO CONFIGURE DNS INCORRECTLY
422 7 ▶ 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 5 7
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
052
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
423 2 ($rv, $reason) = invalid_mx($answer[$i]->exchange);
424 2 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 1 1 2
425 1 return (0, $reason);
426 } else {
427 1 ▶ print "DEBUG: Test Passed - Allowing IP Address as Hostname\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 1 1
428 1 return (1, '');
429 }
430 }
431 }
432  
433 # Keep looping, unless this was the last answer in the MX
434 # resolution packet.
435 # XXX $packet->header->ancount, in the case of corrupt packets,
436 # may differ from the actual number of records and may return unwanted failures
437 5 ▶ if ($i == $#answer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#answer) 3 2 5
438  
439 #MX RECORD RETURNED DOES NOT RESOLVE
440 3 $reason = "MX Record: ".$answer[$i]->exchange." does not resolve";
441 3 ▶ print "DEBUG: Test Failed - $reason\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 3 3
442 3 return (0, $reason);
443 }
444 }
445  
446 } # for
447 }
448 } else {
449 2 ($rv, $reason) = $check_implicit_mx++;
450 }
451  
452 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
453  
454 4 ▶ if ($check_implicit_mx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_implicit_mx > 0) 4 0 4
455 4 ($rv, $reason) = check_implicit_mx($domain, $res, $params{'debug'}, $params{'resolution_problem_return'});
456 4 ▶ if (defined $rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) 4 0 4
457 4 return ($rv, $reason);
458 }
459 }
460 } else {
461 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
462 0 ▶ print "DEBUG: Test Passed by Default\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
463 0 return ($params{'resolution_problem_return'}, 'Test '.$resolution_problem_status.' due to a Resolution Problem');
464 }
465  
466 0 ▶ print "DEBUG: Test Passed\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
467 0 return (1,'');
468 }
469  
470 4 ▶sub check_implicit_mx {
Subroutine check_implicit_mx: called CC=26 SCAR=41.7
Pod undocumented
471 4 my ($SenderDomain, $res, $debug, $resolution_problem_return) = @_;
472
473 4 my ($rv, $reason, $packet, @answer, @answer2, $resolution_problem_status);
474  
475 #CONSTANTS/SETTABLE OPTIONS
476 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
477  
478 4 ▶ if ($resolution_problem_return > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolution_problem_return > 0) { } 4 0 4
479 4 $resolution_problem_status = 'Passed';
480 } else {
481 0 $resolution_problem_status = 'Failed';
482 }
483  
484 4 ▶ print "DEBUG: Checking for Implicit MX Records\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 4 4
485 #NO MX RECORDS RETURNED - CHECK FOR IMPLICIT MX RECORD BY A RECORD per Jan-Pieter Cornet recommendation
486 4 $packet = $res->send($SenderDomain,'A');
487 4 ▶ if (!defined ($packet)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 4 4
488 #THERE WAS AN ERROR - NO IMPLICIT A RECORD COULD BE RESOLVED
489 0 ▶ print "DEBUG: Test Passed by Default\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
490 0 return ($resolution_problem_return, 'Test '.$resolution_problem_status.' due to a Resolution Problem');
491 }
492  
493 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
494 4 ▶ if ($packet->header->ancount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet->header->ancount > 0) { } 3 1 4
495 3 @answer = $packet->answer;
496 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
497 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
498 1 ($rv, $reason) = invalid_mx($answer[0]->address);
499 1 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 0 1 1
500 0 ▶ print "DEBUG: Test Failed - $reason\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
501 0 return (0, $reason);
502 } else {
503 1 ▶ print "DEBUG: Test Passed ".$answer[0]->address." looks good\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
504 1 return (1, '');
505 }
506 } elsif ($answer[0]->type eq "CNAME") {
507 #IS THIS REALLY A NECESSARY TEST? SHOULD WE BE TESTING FOR IMPLICIT CNAME RECORDS?
508 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
509 2 $packet = $res->send($answer[0]->cname,'A');
510  
511 2 ▶ if (!defined ($packet)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 2 2
512 #THERE WAS AN ERROR TRYING TO RESOLVE THE CNAME FOR THE MAIL EXCHANGE
513 0 ▶ print "DEBUG: Test Passed by Default\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
514 0 return (1, '');
515 }
516  
517 2 ▶ if ($packet->header->ancount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet->header->ancount > 0) 2 0 2
518 2 ▶ if (defined ($packet->answer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packet->answer) 2 0 2
519 2 @answer2 = $packet->answer;
520 2 ▶ if ($answer2[0]->type eq "A") {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer2[0]->type eq 'A') { } 1 1 2
521 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
522 1 ($rv, $reason) = invalid_mx($answer2[0]->address);
523 1 ▶ if ($rv > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv > 0) { } 0 1 1
524 0 ▶ print "DEBUG: Test Failed - $reason\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
525 0 return (0, $reason);
526 } else {
527 1 ▶ print "DEBUG: Test Passed ".$answer2[0]->address." looks good\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
528 1 return (1, '');
529 }
530 } else {
531 #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
532 1 $reason = 'Invalid use of CNAME for Implicit MX record';
533 1 ▶ print "DEBUG: Test Failed - $reason\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
534 1 return (0, $reason);
535 }
536 }
537 }
538 }
539 } else {
540 1 $reason = "No MX or A Records Exist for $SenderDomain";
541 1 ▶ print "DEBUG: Test Failed - $reason\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
542 1 return (0, $reason);
543 }
544 0 return;
545 }
546  
547 34 ▶sub invalid_mx {
Subroutine invalid_mx: called CC=18 SCAR=36.1
Pod undocumented
548 34 my ($ip) = @_;
549 34 my ($flag_intranets);
550  
551 #UPDATED MORE ON 11-18-2011 based on RFC 5735
552  
553 #0/8, 255/8, 127/8 aren't a valid MX so return false - added per Matthew van Eerde recomendation
554 34 ▶ if ($ip =~ /^(255|127|0)\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^(255|127|0)\./) 0 34 34
555 0 return (1, "Invalid use of 0/8, 255/8 or 127/8 ($ip) as an MX record");
556 }
557  
558 34 $flag_intranets = $FLAG_INTRANETS;
559  
560 #10/8
561 34 ▶ if ($flag_intranets && $ip =~ /^10\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_intranets and $ip =~ /^10\./) 3 31 34
Condition66%2/3
$flag_intranets and $ip =~ /^10\./
!ll&&!rl&&r
0313
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
562 3 return (2, "Invalid use of private IP (e.g. $ip) range for MX");
563 }
564 #172.16/12 - Fixed per Matthen van Eerde
565 31 ▶ 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 31 31
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
0310
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
566 0 return (2, "Invalid use of private IP (e.g. $ip) range for MX");
567 }
568 #192.168/16
569 31 ▶ if ($flag_intranets && $ip =~ /^192\.168\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_intranets and $ip =~ /^192\.168\./) 4 27 31
Condition66%2/3
$flag_intranets and $ip =~ /^192\.168\./
!ll&&!rl&&r
0274
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
570 4 return (2, "Invalid use of private IP (e.g. $ip) range for MX");
571 }
572  
573 #fc00::/7
574 27 ▶ if ($flag_intranets && $ip =~ /^fc00\:0\:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_intranets and $ip =~ /^fc00\:0\:/i) 0 27 27
Condition33%1/3
$flag_intranets and $ip =~ /^fc00\:0\:/i
!ll&&!rl&&r
0270
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
575 0 return (2, "Invalid use of unique local address (e.g. $ip) range for MX");
576 }
577  
578 #fd00::/8
579 27 ▶ if ($flag_intranets && $ip =~ /^fd00\:0\:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_intranets and $ip =~ /^fd00\:0\:/i) 0 27 27
Condition33%1/3
$flag_intranets and $ip =~ /^fd00\:0\:/i
!ll&&!rl&&r
0270
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
580 0 return (2, "Invalid use of private IP (e.g. $ip) range for MX");
581 }
582  
583 #DHCP auto-discover added per Matthew van Eerde recomendation 169.254/16
584 27 ▶ if ($ip =~ /^169\.254\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^169\.254\./) 0 27 27
585 0 return (1, "Invalid use of a DHCP auto-discover IP range ($ip) as an MX record");
586 }
587  
588 #IPv6 link-local addresses fe80::/10
589 27 ▶ if ($ip =~ /^fe80\:0\:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^fe80\:0\:/i) 1 26 27
590 1 return (1, "Invalid use of a link-local IP range ($ip) as an MX record");
591 }
592  
593 #Multicast 224/8 through 239/8 added per Matthew van Eerde recomendation
594 26 ▶ 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 26 26
595 0 return (1, "Invalid use of a Multicast IP range ($ip) as an MX record");
596 }
597  
598 #Experimental block - Former Class E - 240.0.0.0/4 courtesy of Mark Damrose
599 26 ▶ if ($ip =~ /^2[45]\d\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^2[45]\d\./) 0 26 26
600 0 return (1, "Invalid use of an experimental IP ($ip) as an MX record");
601 }
602  
603 #Reserved for benchmark tests of interconnect devices 192.18.0.0/15 courtesy of Mark Damrose
604 26 ▶ if ($ip =~ /^192\.1[89]\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^192\.1[89]\./) 0 26 26
605 0 return (1, "Invalid use of a reserved IP ($ip) as an MX record");
606 }
607  
608 #Reserved for documentation or published examples 192.0.2.0/24 courtesy of Mark Damrose
609 26 ▶ if ($ip =~ /^192\.0\.2\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^192\.0\.2\./) 0 26 26
610 0 return (1, "Invalid use of a reserved IP ($ip) as an MX record");
611 }
612  
613
614 26 return (0,'');
615 }
616  
617 37 ▶sub int_to_truefalse {
Subroutine int_to_truefalse: called CC=2 SCAR=6.9
Pod undocumented
618 37 my ($int) = @_;
619  
620 37 ▶ if ($int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int) { } 25 12 37
621 25 return "True";
622 } else {
623 12 return "False";
624 }
625 }
626  
627 3 ▶sub check_email_and_mx {
Subroutine check_email_and_mx: called CC=6 SCAR=18.9
Pod documented
628 3 my ($email) = @_;
629 3 my ($rv, $fail_reason, $status, $debug);
630  
631 3 $debug = 0;
632  
633 3 ▶ $email || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$email or return 0 3 0 3
634
635 3 ▶ print "DEBUG: e-mail address is: $email<br>\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
636
637 # SANITIZE THE E-MAIL ADDRESS OF SPACES
638 3 $email =~ s/ //g;
639  
640 # CHECK FOR INCOMPLETE ADDRESSES AT LARGE ISPS
641 3 $email =~ s/\@aol\.?$/\@aol.com/i;
642 3 $email =~ s/\@hotmail\.?$/\@hotmail.com/i;
643 3 $email =~ s/\@gmail\.?$/\@gmail.com/i;
644  
645 3 ▶ print "DEBUG: e-mail address is now: $email<br>\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
646  
647 # CHECK FOR A VALIDLY CONSTRUCTED E-MAIL ADDRESS
648 3 ($rv) = Net::validMX::check_email_validity($email);
649
650 3 ▶ if ($rv < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv < 1) 0 3 3
651 0 return($rv, "Failed check_email_validity", $email);
652 }
653  
654 # CHECK FOR VALID MX RECORD
655 3 ($rv, $fail_reason) = Net::validMX::check_valid_mx($email);
656  
657 3 ▶ if ($rv < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv < 1) 0 3 3
658 0 return($rv, $fail_reason, $email);
659 }
660  
661 3 return($rv, "Passed", $email);
662 }
663  
664 11 ▶sub check_email_validity {
Subroutine check_email_validity: called CC=12 SCAR=28.9
Pod documented
665 11 my ($email) = @_;
666 11 my ($local);
667  
668 #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
669 #allowed in the username. Thanks to Paul Whittney for reporting the issue.
670  
671 #PER WIKIPEDIA
672 #Per Wikipedia:
673  
674 #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.
675
676 #Can't have two dots
677 11 ▶ if ($email =~ /\.\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($email =~ /\.\./) 0 11 11
678 0 return 0;
679 }
680  
681 #Can't be longer than 254 chars
682 11 ▶ if (length($email) > 254) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $email > 254) 0 11 11
683 0 return 0;
684 }
685  
686 #Can't end in a period
687 11 ▶ if ($email =~ /\.$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($email =~ /\.$/) 0 11 11
688 0 return 0;
689 }
690  
691 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
692
693 8 $local = $1;
694
695 #check local length
696 8 ▶ if (length($local) > 64) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $local > 64) 2 6 8
697 2 return 0;
698 }
699 #no need to check if domain is over 253 chars, as it would not pass both overall length and regex if it was
700  
701 # per RFC 3696 section 3 the local part of an address cannot begin or end with a period
702 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
703 0 return 0;
704 }
705
706 # PURGE BACKSLASHES
707 6 $local =~ s/\\//g;
708
709 # per RFC 3696 section 3 the local part of the email can be quoted, which allows any character to appear if inside quotes
710 # PURGE BEGINNING AND END QUOTE IF IT CONTAINS QUOTES
711 6 ▶ if ($local =~ /"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local =~ /"/) 1 5 6
712 1 ▶ if($local =~ /^".*"$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local =~ /^".*"$/) 0 1 1
713 0 $local =~ s/^"//g;
714 0 $local =~ s/"$//g;
715 }
716
717 # IF IT STILL CONTAINS A QUOTE, IT IS INVALID, OTHERWISE THE LOCAL PART IS VALID
718 1 ▶ if ($local =~ /"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local =~ /"/) { } 1 0 1
719 1 return 0;
720 } else {
721 0 return 1;
722 }
723 }
724
725 # check for allowed characters, per RFC 3696 section 3
726 5 ▶ if ($local =~ /^[\@'-`\/!\?=#\$\%&*+~_A-Za-z0-9\.{}|]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local =~ m[^[\@'-`/!\?=#\$\%&*+~_A-Za-z0-9\.{}|]+$]) { } 5 0 5
727 5 return 1;
728 } else {
729 0 return 0;
730 }
731  
732 }
733 3 return 0;
734  
735 }
736  
737 #get domain name from an email address
738 38 ▶sub get_domain_from_email {
Subroutine get_domain_from_email: called CC=3 SCAR=11.0
Pod documented
739 38 my ($email, %params) = @_;
740  
741 38 my ($domain, $local);
742  
743 38 $domain = $email;
744  
745 #REMOVE ANY LEADING/TRAILING <>'s
746 38 $domain =~ s/(^<|>$)//g;
747 #REMOVE ANY LEADING/TRAILING SPACE'S
748 38 $domain =~ s/^ *//g;
749 38 $domain =~ s/ *$//g;
750 #REMOVE EVERYTHING UP TO THE @ SYMBOL
751 38 $domain =~ s/(.*)\@//g;
752  
753 38 $local = $1;
754  
755 38 ▶ print "\nDEBUG: Extracted Sender Domain: $domain / Local: $local from $params{'email'}\n" if $params{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 38 38
756  
757 38 ▶ return wantarray ? ($local,$domain) : $domain;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 37 38
758 }
759  
760 0 ▶sub dns_lookup {
Subroutine dns_lookup: not called CC=2 SCAR=17.9
Pod undocumented
761 0 my ($domain, $type) = @_;
762 0 my ($dns, $query);
763  
764 0 $dns = Net::DNS::Resolver->new;
765 0 $query = $dns->search($domain, $type);
766 0 ▶ if ($query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query) { } 0 0 0
767 0 return $query->answer;
768 } else {
769 0 warn "Error performing $type query for $domain! ". $dns->errorstring;
770 }
771 }
772  
773 0 ▶sub check_spf_for_domain {
Subroutine check_spf_for_domain: not called CC=10 SCAR=47.0
Pod documented
774 0 my ($domain, %params) = @_;
775 0 my ($dns, $query, $spf_line, @clauses, $found_spf);
776  
777 0 $dns = Net::DNS::Resolver->new;
778 0 $query = $dns->search($domain, 'TXT');
779 0 ▶ if (not $query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
780 0 warn "Error performing TXT query for $domain! ". $dns->errorstring;
781 0 return ("suspect", "no TXT record found");
782 }
783  
784 0 foreach my $result ($query->answer) {
785 0 ▶ next unless $result->type eq 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
unless $result->type eq "TXT" 0 0 0
786 0 $spf_line = $result->txtdata;
787  
788 0 ▶ if ($spf_line =~ /^v=spf[12]/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf_line =~ /^v=spf[12]/i) 0 0 0
789 0 $found_spf++;
790  
791 # split into clauses
792 0 @clauses = split / /, $spf_line;
793  
794 0 foreach my $clause (@clauses) {
795 # ignore clauses that reject email - only false accepts are good spam indicators
796 0 ▶ next if $clause =~ /^[-~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $clause =~ /^[-~]/ 0 0 0
797 0 ▶ if ($clause =~ /^.?all/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause =~ /^.?all/) 0 0 0
798 # if accepting email from all, rule is clearly useless
799 0 return ("bad", "use of universal pass rule $clause");
800 }
801 }
802 }
803 }
804  
805 0 ▶ if ($found_spf == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($found_spf == 0) { } 0 0 0
elsif ($found_spf > 1) { } 0 0 0
806 0 return ("suspect", "no TXT record matching SPF format found");
807 } elsif ($found_spf > 1) {
808 0 return ("suspect", "multiple TXT records matching SPF format found");
809 }
810  
811 0 return ("valid", undef);
812 }
813  
814 7 1;
815  
816 __END__
↑ Summary blib/script/check_email_and_mx.pl »