lib/Mail/DMARC/Base.pm

filter: Statementstmt 81.9% 172 / 210 Branchbran 57.1% 72 / 126 Conditioncond 47.6% 20 / 42 MC/DCmcdc 13.0% 3 / 23 Subroutinesub 97.3% 36 / 37 Podpod 28.0% 7 / 25 totaltotal 66.9% 310 / 463 CC 89 SCAR 30.4
CC
89
Cov
70%
has_dns_rr
103.6
update_psl_file
72.0
find_psl_file
17.0
CRAP 20.9
« blib/lib/Mail/DMARC/Report/URI.pm ↑ Summary lib/Mail/DMARC/Report/Sender.pm »
1 package Mail::DMARC::Base;
2 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶no warnings 'experimental::args_array_with_signatures'; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 21 our $VERSION = '2.20260827';
8  
9 21 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 21 ▶use Net::DNS::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 21 require URI::_idna;
17  
18 298 ▶sub new( $class, @args ) {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
19 298 ▶ croak "invalid args" if @args % 2 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 != 0 0 298 298
20 298 return bless {
21 config_file => 'mail-dmarc.ini',
22 @args, # this may override config_file
23 }, $class;
24 }
25  
26 21 my $_fake_time;
27  
28 72 ▶sub time ($self) { ## no critic
Subroutine time: called CC=2 SCAR=6.9
Pod undocumented
29 # Ability to return a fake time for testing
30 72 ▶ my $time
Branch: 1 branch
BranchTrueFalseTotal
defined $Mail::DMARC::Base::_fake_time ? : 54 18 72
31 = defined $Mail::DMARC::Base::_fake_time
32 ? $Mail::DMARC::Base::_fake_time
33 : time;
34 72 return $time;
35 }
36  
37 20 ▶sub set_fake_time ( $self, $time ) {
Subroutine set_fake_time: called CC=1 SCAR=0.0
Pod undocumented
38 20 $Mail::DMARC::Base::_fake_time = $time;
39 20 return;
40 }
41  
42 6811 ▶sub config( $self, $file = undef, @too_many ) {
Subroutine config: called CC=5 SCAR=16.1
Pod undocumented
43 6811 ▶ croak "invalid args" if @too_many;
Branch: 1 branch
BranchTrueFalseTotal
if @too_many 0 6811 6811
44 6811 ▶ return $self->{config} if ref $self->{config} && !$file;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"config"} and not $file 6638 173 6811
Condition100%3/3
ref $self->{"config"} and not $file
!ll&&!rl&&r
17216638
MC/DC100%2/2
ref $self->{"config"} and not $file
ref $self->{"config"} not $file
Truth table100%3/3
A and not(B)
A= ref $self->{"config"}B= not $file
ABresult
0X0
100
111
45 173 return $self->{config} = $self->get_config($file);
46 }
47  
48 43 ▶sub get_prefix( $self, $subdir = undef ) {
Subroutine get_prefix: called CC=3 SCAR=11.0
Pod undocumented
49 return
50 43 ▶ map { $_ . ( $subdir ? $subdir : '' ) } qw[ /usr/local/ /opt/local/ / ./ ];
Branch: 1 branch
BranchTrueFalseTotal
$subdir ? : 168 4 172
51 }
52  
53 27 ▶sub get_sharefile( $self, $file ) {
Subroutine get_sharefile: called CC=2 SCAR=7.1
Pod undocumented
54 27 my $match = File::ShareDir::dist_file( 'Mail-DMARC', $file );
55 27 ▶ print "using $match for $file\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 27 27
56 27 return $match;
57 }
58  
59 175 ▶sub get_config( $self, $file = undef ) {
Subroutine get_config: called CC=11 SCAR=24.2
Pod undocumented
60 175 ▶ $file ||= $ENV{MAIL_DMARC_CONFIG_FILE} || $self->{config_file};
Condition66%4/6
$file ||= $ENV{"MAIL_DMARC_CONFIG_FILE"} || $self->{"config_file"}
l!l&&r!l&&!r
91660
$ENV{'MAIL_DMARC_CONFIG_FILE'} || $self->{'config_file'}
l!l&&r!l&&!r
139270
MC/DC0%0/3
$file ||= $ENV{"MAIL_DMARC_CONFIG_FILE"} || $self->{"config_file"}
$file $ENV{'MAIL_DMARC_CONFIG_FILE'} $self->{'config_file'}
Truth table0%0/4
A ||= B || C
A= $fileB= $ENV{'MAIL_DMARC_CONFIG_FILE'}C= $self->{'config_file'}
ABCresult
1XX1
01X1
0011
0000
61 175 ▶ croak if !$file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 175 175
62 175 ▶ return Config::Tiny->read($file) if -r $file; # fully qualified
Branch: 1 branch
BranchTrueFalseTotal
if -r $file 147 28 175
63  
64 # search <prefix>/etc and the mail/ subdir where mail daemons (and
65 # FreeBSD ports, e.g. authentication_milter) conventionally install it
66 28 foreach my $d ( map { ( $_, "$_/mail" ) } $self->get_prefix('etc') ) {
67 224 ▶ next if !-d $d;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $d 166 58 224
68 58 ▶ next if !-e "$d/$file";
Branch: 1 branch
BranchTrueFalseTotal
unless -e "$d/$file" 57 1 58
69 1 ▶ croak "unreadable file: $d/$file" if !-r "$d/$file";
Branch: 1 branch
BranchTrueFalseTotal
unless -r "$d/$file" 0 1 1
70 1 return Config::Tiny->read("$d/$file");
71 }
72  
73 27 ▶ if ( $file ne 'mail-dmarc.ini' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file ne "mail-dmarc.ini") 1 26 27
74 1 croak "unable to find requested config file $file\n";
75 }
76  
77 26 carp "no mail-dmarc.ini found; using bundled defaults.\n";
78 26 return Config::Tiny->read( $self->get_sharefile('mail-dmarc.ini') );
79 }
80  
81 79 ▶sub any_inet_ntop( $self, $ip_bin ) {
Subroutine any_inet_ntop: called CC=3 SCAR=11.0
Pod undocumented
82 79 ▶ $ip_bin or croak "missing IP in request";
Branch: 1 branch
BranchTrueFalseTotal
$ip_bin or croak("missing IP in request") 79 0 79
83  
84 79 ▶ if ( length $ip_bin == 16 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ip_bin == 16) 6 73 79
85 6 return Socket::inet_ntop( AF_INET6, $ip_bin );
86 }
87  
88 73 return Socket::inet_ntop( AF_INET, $ip_bin );
89 }
90  
91 51 ▶sub any_inet_pton( $self, $ip_txt ) {
Subroutine any_inet_pton: called CC=5 SCAR=18.1
Pod undocumented
92 51 ▶ $ip_txt or croak "missing IP in request";
Branch: 1 branch
BranchTrueFalseTotal
$ip_txt or croak("missing IP in request") 51 0 51
93  
94 51 ▶ if ( $ip_txt =~ /:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip_txt =~ /:/) 4 47 51
95 4 ▶ return Socket::inet_pton( AF_INET6, $ip_txt )
Condition33%1/3
Socket::inet_pton(10, $ip_txt) || croak("invalid IPv6: $ip_txt")
l!l&&r!l&&!r
400
MC/DC0%0/2
Socket::inet_pton(10, $ip_txt) || croak("invalid IPv6: $ip_txt")
Socket::inet_pton(10, $ip_txt) croak("invalid IPv6: $ip_txt")
Truth table33%1/3
A || B
A= Socket::inet_pton(10, $ip_txt)B= croak("invalid IPv6: $ip_txt")
ABresult
1X1
011
000
96 || croak "invalid IPv6: $ip_txt";
97 }
98  
99 47 ▶ return Socket::inet_pton( AF_INET, $ip_txt )
Condition33%1/3
Socket::inet_pton(2, $ip_txt) || croak("invalid IPv4: $ip_txt")
l!l&&r!l&&!r
4700
MC/DC0%0/2
Socket::inet_pton(2, $ip_txt) || croak("invalid IPv4: $ip_txt")
Socket::inet_pton(2, $ip_txt) croak("invalid IPv4: $ip_txt")
Truth table33%1/3
A || B
A= Socket::inet_pton(2, $ip_txt)B= croak("invalid IPv4: $ip_txt")
ABresult
1X1
011
000
100 || croak "invalid IPv4: $ip_txt";
101 }
102  
103 {
104 21 my $public_suffixes;
105 21 my $public_suffixes_stamp;
106  
107 259 ▶ sub get_public_suffix_list($self) {
Subroutine get_public_suffix_list: called CC=3 SCAR=11.0
Pod undocumented
108 259 ▶ if ($public_suffixes) { return $public_suffixes; }
Branch: 1 branch
BranchTrueFalseTotal
if ($public_suffixes) 250 9 259
109 21 ▶ no warnings 'once'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
110 9 $Mail::DMARC::psl_loads++;
111 9 my $file = $self->find_psl_file();
112 9 $public_suffixes_stamp = ( stat($file) )[9];
113  
114 9 ▶ open my $fh, '<:encoding(UTF-8)', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $file or croak("unable to open $file for read: $!\n") 9 0 9
115 or croak "unable to open $file for read: $!\n";
116  
117 # load PSL into hash for fast lookups, esp. for long running daemons
118 110817 my %psl = map { $_ => 1 }
119 147816 grep { $_ !~ /^[\/\s]/ } # weed out comments & whitespace
120 9 map { chomp($_); $_ } ## no critic, remove line endings
121 <$fh>;
122 9 close $fh;
123 9 return $public_suffixes = \%psl;
124 }
125  
126 2 ▶ sub check_public_suffix_list($self) {
Subroutine check_public_suffix_list: called CC=2 SCAR=6.9
Pod undocumented
127 2 my $file = $self->find_psl_file();
128 2 my $new_public_suffixes_stamp = ( stat($file) )[9];
129 2 ▶ if ( $new_public_suffixes_stamp != $public_suffixes_stamp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_public_suffixes_stamp != $public_suffixes_stamp) 1 1 2
130 1 $public_suffixes = undef;
131 1 $self->get_public_suffix_list();
132 1 return 1;
133 }
134 1 return 0;
135 }
136 }
137  
138 21 ▶sub is_public_suffix( $self, $zone ) {
Subroutine is_public_suffix: called CC=8 SCAR=20.9
Pod documented
139 264 ▶ croak "missing zone name!" if !$zone;
Branch: 1 branch
BranchTrueFalseTotal
unless $zone 0 264 264
140  
141 264 my $public_suffixes = $self->get_public_suffix_list();
142  
143 264 ▶ $zone = URI::_idna::decode( $zone, 0 ) // $zone if $zone =~ /xn--/;
Branch: 1 branch
BranchTrueFalseTotal
if $zone =~ /xn--/ 2 262 264
Condition33%1/3
URI::_idna::decode($zone, 0) // $zone
l!l&&r!l&&!r
200
MC/DC0%0/2
URI::_idna::decode($zone, 0) // $zone
URI::_idna::decode($zone, 0) $zone
Truth table33%1/3
A // B
A= URI::_idna::decode($zone, 0)B= $zone
ABresult
1X1
011
000
144  
145 # Check for exception rules
146 264 ▶ return 0 if $public_suffixes->{"!$zone"};
Branch: 1 branch
BranchTrueFalseTotal
if $$public_suffixes{"!$zone"} 1 263 264
147  
148 # Check for direct match
149 263 ▶ return 1 if $public_suffixes->{$zone};
Branch: 1 branch
BranchTrueFalseTotal
if $public_suffixes->{$zone} 184 79 263
150  
151 # Check for wildcard match
152 79 my @labels = split /\./, $zone;
153 79 ▶ if ( @labels > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@labels > 1) 67 12 79
154 67 my $wildcard = join '.', '*', (@labels)[ 1 .. scalar(@labels) - 1 ];
155 67 ▶ return 1 if $public_suffixes->{$wildcard};
Branch: 1 branch
BranchTrueFalseTotal
if $public_suffixes->{$wildcard} 2 65 67
156 }
157  
158 77 return 0;
159 }
160  
161 0 ▶sub update_psl_file( $self, $dryrun = undef ) {
Subroutine update_psl_file: not called CC=8 SCAR=42.8
Pod documented
162 0 my $psl_file = $self->find_psl_file();
163  
164 0 ▶ die "No Public Suffix List file found\n" if ( !$psl_file );
Branch: 1 branch
BranchTrueFalseTotal
unless $psl_file 0 0 0
165 0 ▶ die "Public suffix list file $psl_file not found\n" if ( !-f $psl_file );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $psl_file 0 0 0
166 0 ▶ die "Cannot write to Public Suffix List file $psl_file\n" if ( !-w $psl_file );
Branch: 1 branch
BranchTrueFalseTotal
unless -w $psl_file 0 0 0
167  
168 0 my $url = 'https://publicsuffix.org/list/effective_tld_names.dat';
169 0 ▶ if ($dryrun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dryrun) 0 0 0
170 0 print
171 "Will attempt to update the Public Suffix List file at $psl_file (dryrun mode)\n";
172 0 return;
173 }
174  
175 0 my $response = HTTP::Tiny->new->mirror( $url, $psl_file );
176 0 my $content = $response->{'content'};
177 0 ▶ if ( !$response->{'success'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->{'success'}) { } 0 0 0
178 0 my $status = $response->{'status'};
179 0 die
180 "HTTP Request for Public Suffix List file failed with error $status ($content)\n";
181 }
182 else {
183 0 ▶ if ( $response->{'status'} eq '304' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'status'} eq '304') { } 0 0 0
184 0 print "Public Suffix List file $psl_file not modified\n";
185 }
186 else {
187 0 print "Public Suffix List file $psl_file updated\n";
188 }
189 }
190 0 return;
191 }
192  
193 12 ▶sub find_psl_file($self) {
Subroutine find_psl_file: called CC=9 SCAR=28.3
Pod undocumented
194 my $file
195 12 ▶ = $self->config->{dns}{public_suffix_list} || 'share/public_suffix_list';
Condition50%1/2
$self->config->{'dns'}{'public_suffix_list'} || 'share/public_suffix_list'
l!l
120
MC/DC0%0/1
$self->config->{'dns'}{'public_suffix_list'} || 'share/public_suffix_list'
$self->config->{'dns'}{'public_suffix_list'}
Truth table50%1/2
A
A= $self->config->{'dns'}{'public_suffix_list'}
Aresult
11
00
196 12 ▶ if ( $file =~ /^\// && -f $file && -r $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ m[^/] and -f $file and -r $file) 0 12 12
Condition33%2/6
$file =~ m[^/] and -f $file and -r $file
!ll&&!rl&&r
1200
$file =~ m[^/] and -f $file
!ll&&!rl&&r
1200
MC/DC0%0/3
$file =~ m[^/] and -f $file and -r $file
$file =~ m[^/] -f $file -r $file
Truth table25%1/4
A and B and C
A= $file =~ m[^/]B= -f $fileC= -r $file
ABCresult
0XX0
10X0
1100
1111
197 0 ▶ print "using $file for Public Suffix List\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
198 0 return $file;
199 }
200  
201 12 foreach my $path ( $self->get_prefix($file) ) {
202 48 ▶ if ( -f $path && -r $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path and -r $path) 12 36 48
Condition66%2/3
-f $path and -r $path
!ll&&!rl&&r
36012
MC/DC50%1/2
-f $path and -r $path
-f $path -r $path
Truth table66%2/3
A and B
A= -f $pathB= -r $path
ABresult
0X0
100
111
203 12 print "using $path for Public Suffix List\n"; # if $self->verbose;
204 12 return $path;
205 }
206 }
207  
208 # Fallback to included suffic list
209 0 return $self->get_sharefile('public_suffix_list');
210 }
211  
212 5 ▶sub has_dns_rr( $self, $type, $domain ) {
Subroutine has_dns_rr: called CC=13 SCAR=46.4
Pod documented
213 5 my @matches;
214 5 my $res = $self->get_resolver();
215 5 ▶ my $query = $res->query( $domain, $type ) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $query = $res->query($domain, $type) or do { return 0 unless wantarray; return @matches } 0 5 5
216 5 ▶ return 0 if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 5 0 5
217 0 return @matches;
218 };
219 0 for my $rr ( $query->answer ) {
220 0 ▶ next if $rr->type ne $type;
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type ne $type 0 0 0
221 0 ▶ push @matches,
Branch: 7 branches
BranchTrueFalseTotal
$rr->type eq 'A' ? : 0 0 0
$rr->type eq 'PTR' ? : 0 0 0
$rr->type eq 'NS' ? : 0 0 0
$rr->type eq 'TXT' ? : 0 0 0
$rr->type eq 'SPF' ? : 0 0 0
$rr->type eq 'AAAA' ? : 0 0 0
$rr->type eq 'MX' ? : 0 0 0
222 $rr->type eq 'A' ? $rr->address
223 : $rr->type eq 'PTR' ? $rr->ptrdname
224 : $rr->type eq 'NS' ? $rr->nsdname
225 : $rr->type eq 'TXT' ? $rr->txtdata
226 : $rr->type eq 'SPF' ? $rr->txtdata
227 : $rr->type eq 'AAAA' ? $rr->address
228 : $rr->type eq 'MX' ? $rr->exchange
229 : $rr->answer;
230 }
231 0 ▶ return scalar @matches if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
232 0 return @matches;
233 }
234  
235 65 ▶sub epoch_to_iso( $self, $epoch ) {
Subroutine epoch_to_iso: called CC=1 SCAR=0.0
Pod undocumented
236 65 my @fields = localtime($epoch);
237  
238 65 my $ss = sprintf( "%02i", $fields[0] ); # seconds
239 65 my $mn = sprintf( "%02i", $fields[1] ); # minutes
240 65 my $hh = sprintf( "%02i", $fields[2] ); # hours (24 hour clock)
241  
242 65 my $dd = sprintf( "%02i", $fields[3] ); # day of month
243 65 my $mm = sprintf( "%02i", $fields[4] + 1 ); # month
244 65 my $yy = ( $fields[5] + 1900 ); # year
245  
246 65 return "$yy-$mm-$dd" . 'T' . "$hh:$mn:$ss";
247 }
248  
249 259 ▶sub get_resolver( $self, $timeout = undef, $retrans = undef ) {
Subroutine get_resolver: called CC=8 SCAR=21.7
Pod documented
250 259 ▶ $timeout ||= $self->config->{dns}{timeout} || 5;
Condition40%2/5
$timeout ||= $self->config->{"dns"}{"timeout"} || 5
l!l&&r!l&&!r
02590
$self->config->{'dns'}{'timeout'} || 5
l!l
2590
MC/DC0%0/2
$timeout ||= $self->config->{"dns"}{"timeout"} || 5
$timeout $self->config->{'dns'}{'timeout'}
Truth table33%1/3
A ||= B
A= $timeoutB= $self->config->{'dns'}{'timeout'}
ABresult
1X1
011
000
251 259 ▶ $retrans ||= $self->config->{dns}{retrans} || 5;
Condition60%3/5
$retrans ||= $self->config->{"dns"}{"retrans"} || 5
l!l&&r!l&&!r
02590
$self->config->{'dns'}{'retrans'} || 5
l!l
132127
MC/DC0%0/2
$retrans ||= $self->config->{"dns"}{"retrans"} || 5
$retrans $self->config->{'dns'}{'retrans'}
Truth table33%1/3
A ||= B
A= $retransB= $self->config->{'dns'}{'retrans'}
ABresult
1X1
011
000
252 259 ▶ return $self->{resolver} if defined $self->{resolver};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"resolver"} 256 3 259
253 3 $self->{resolver} = Net::DNS::Resolver->new( dnsrch => 0 );
254 3 $self->{resolver}->tcp_timeout($timeout);
255 3 $self->{resolver}->udp_timeout($timeout);
256 3 $self->{resolver}->retrans($retrans);
257 3 return $self->{resolver};
258 }
259  
260 60 ▶sub set_resolver( $self, $resolver ) {
Subroutine set_resolver: called CC=1 SCAR=0.0
Pod documented
261 60 $self->{resolver} = $resolver;
262 60 return;
263 }
264  
265 9 ▶sub to_ascii_domain( $self, $domain ) {
Subroutine to_ascii_domain: called CC=4 SCAR=26.7
Pod undocumented
266 9 ▶ return $domain unless $domain =~ /[^\x00-\x7F]/; # fast path: ASCII only
Branch: 1 branch
BranchTrueFalseTotal
unless $domain =~ /[^\x00-\x7F]/ 9 0 9
267 # Convert each U-label to its A-label (punycode) equivalent per RFC 8616 §6
268 my @ascii_labels = map {
269 0 ▶ if (/[^\x00-\x7F]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/[^\x00-\x7F]/) { } 0 0 0
270 0 my $ascii = eval { URI::_idna::encode($_) };
271 0 ▶ $ascii // $_;
Condition0%0/3
$ascii // $_
l!l&&r!l&&!r
000
MC/DC0%0/2
$ascii // $_
$ascii $_
Truth table0%0/3
A // B
A= $asciiB= $_
ABresult
1X1
011
000
272 }
273 else {
274 0 $_;
275 }
276 } split /\./, $domain;
277 0 return join '.', @ascii_labels;
278 }
279  
280 41 ▶sub is_valid_ip( $self, $ip ) {
Subroutine is_valid_ip: called CC=4 SCAR=13.9
Pod documented
281 41 ▶ return 0 if !defined $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 1 40 41
282  
283 40 ▶ my $family = $ip =~ /:/ ? AF_INET6 : AF_INET;
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /:/ ? : 1 39 40
284 40 ▶ return defined Socket::inet_pton( $family, $ip ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined Socket::inet_pton($family, $ip) ? : 34 6 40
285 }
286  
287 # RFC 1101
288 21 my $dotted_quad = qr/(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})/;
289 21 my $not_an_ip = qr/(?!(?:$dotted_quad[.]$dotted_quad[.]$dotted_quad[.]$dotted_quad)(?:[.]|$))/;
290 21 my $host_label = qr/$not_an_ip[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?/;
291  
292 136 ▶sub is_valid_domain( $self, $domain ) {
Subroutine is_valid_domain: called CC=7 SCAR=19.7
Pod documented
293 136 ▶ return 0 if $domain !~ /^$host_label(?:\.$host_label)*$/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $domain =~ /^$host_label(?:\.$host_label)*$/x 3 133 136
294 133 my $tld = ( split /\./, $domain )[-1];
295 133 ▶ return 1 if $self->is_public_suffix($tld);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_public_suffix($tld) 127 6 133
296 6 ▶ return 0 if $domain eq 'localhost';
Branch: 1 branch
BranchTrueFalseTotal
if $domain eq "localhost" 0 6 6
297 6 ▶ return 0 if $tld eq 'localdomain';
Branch: 1 branch
BranchTrueFalseTotal
if $tld eq "localdomain" 0 6 6
298 6 ▶ return 0 if -1 == index( $domain, '.' );
Branch: 1 branch
BranchTrueFalseTotal
if index($domain, ".") == -1 0 6 6
299 6 $tld = join( '.', ( split /\./, $domain )[ -2, -1 ] );
300 6 ▶ return 1 if $self->is_public_suffix($tld);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_public_suffix($tld) 1 5 6
301 5 return 0;
302 }
303  
304 92 ▶sub is_valid_spf_scope( $self, $scope ) {
Subroutine is_valid_spf_scope: called CC=3 SCAR=13.3
Pod undocumented
305 92 ▶ return if !defined $scope;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scope 0 92 92
306 92 ▶ return lc $scope if grep { lc $scope eq $_ } qw/ mfrom helo /;
Branch: 1 branch
BranchTrueFalseTotal
if grep {lc $scope eq $_;} "mfrom", "helo" 92 0 92
307 0 carp "$scope is not a valid SPF scope";
308 0 return;
309 }
310  
311 94 ▶sub is_valid_spf_result( $self, $result ) {
Subroutine is_valid_spf_result: called CC=3 SCAR=12.0
Pod undocumented
312 94 ▶ return if !defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 1 93 94
313 return 1
314 93 ▶ if grep { lc $result eq $_ }
Branch: 1 branch
BranchTrueFalseTotal
if grep {lc $result eq $_;} "fail", "neutral", "none", "pass", "permerror", "softfail", "temperror" 93 0 93
315 qw/ fail neutral none pass permerror softfail temperror /;
316 0 carp "$result is not a valid SPF result";
317 0 return;
318 }
319  
320 26 ▶sub slurp( $self, $file ) {
Subroutine slurp: called CC=2 SCAR=7.0
Pod undocumented
321 26 ▶ open my $FH, '<', $file or croak "unable to read $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $FH, "<", $file or croak("unable to read ${file}: $!") 26 0 26
322 26 my $contents = do { local $/; <$FH> }; ## no critic (Local)
323 26 close $FH;
324 26 return $contents;
325 }
326  
327 533 ▶sub verbose( $self, $val = undef ) {
Subroutine verbose: called CC=3 SCAR=11.0
Pod undocumented
328 533 ▶ return $self->{verbose} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 519 14 533
329 14 return $self->{verbose} = $val;
330 }
331  
332 21 1;
333  
334 __END__
335  
336 =pod
337  
338 =head1 NAME
339  
340 Mail::DMARC::Base - DMARC utility functions
341  
342 =head1 VERSION
343  
344 version 2.20260827
345  
346 =head1 METHODS
347  
348 =head2 is_public_suffix
349  
350 Determines if part of a domain is a Top Level Domain (TLD). Examples of TLDs are com, net, org, co.ok, am, and us.
351  
352 Determination is made by consulting a Public Suffix List. The included PSL is from mozilla.org. See http://publicsuffix.org/list/ for more information, and a link to download the latest PSL.
353  
354 =head2 update_psl_file
355  
356 Download a new Public Suffix List file from mozilla and update the installed file with the new copy.
357  
358 =head2 has_dns_rr
359  
360 Determine if a DNS Resource Record of the specified type exists at the DNS name provided.
361  
362 =head2 get_resolver
363  
364 Returns a (cached) Net::DNS::Resolver object
365  
366 =head2 set_resolver
367  
368 Set the Net::DNS::Resolver object to be used for lookups
369  
370 =head2 is_valid_ip
371  
372 Determines if the supplied IP address is a valid IPv4 or IPv6 address.
373  
374 =head2 is_valid_domain
375  
376 Determine if a string is a legal RFC 1034 or 1101 host name.
377  
378 Half the reason to test for domain validity is to shave seconds off our processing time by not having to process DNS queries for illegal host names. The other half is to raise exceptions if methods are being called incorrectly.
379  
380 =head1 SEE ALSO
381  
382 Mozilla Public Suffix List: http://publicsuffix.org/list/
383  
384 =head1 AUTHORS
385  
386 =over 4
387  
388 =item *
389  
390 Matt Simerson <msimerson@cpan.org>
391  
392 =item *
393  
394 Davide Migliavacca <shari@cpan.org>
395  
396 =item *
397  
398 Marc Bradshaw <marc@marcbradshaw.net>
399  
400 =back
401  
402 =head1 COPYRIGHT AND LICENSE
403  
404 This software is copyright (c) 2026 by Matt Simerson.
405  
406 This is free software; you can redistribute it and/or modify it under
407 the same terms as the Perl 5 programming language system itself.
408  
409 =cut
« blib/lib/Mail/DMARC/Report/URI.pm ↑ Summary lib/Mail/DMARC/Report/Sender.pm »