Verify.pm

filter: Statementstmt 62.6% 121 / 193 Branchbran 38.8% 21 / 54 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 68.1% 15 / 22 Podpod 37.5% 3 / 8 totaltotal 56.4% 162 / 287 CC 36 SCAR 23.2
CC
36
Cov
57%
resolve
31.8
check_tlsa
20.0
check_openpgpkey
20.0
CRAP 10.2
↑ Summary Verify/ResultSet.pm »
1 package Net::SMTP::Verify;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '1.04'; # VERSION
6 # ABSTRACT: verify SMTP recipient addresses
7  
8 2 ▶use Net::SMTP::Verify::ResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Net::DNS::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Net::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::Cmd qw( CMD_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Digest::SHA qw(sha224_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 2 has 'host' => ( is => 'rw', isa => 'Maybe[Str]' );
18 2 has 'port' => ( is => 'rw', isa => 'Int', default => 25 );
19  
20 has 'helo_name' => (
21 is => 'rw', isa => 'Str', lazy => 1,
22 1 ▶ default => sub { Sys::Hostname::hostname },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 2 );
24 2 has 'timeout' => ( is => 'rw', isa => 'Int', default => 30 );
25  
26 has 'resolver' => (
27 is => 'rw', isa => 'Net::DNS::Resolver', lazy => 1,
28 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 Net::DNS::Resolver->new(
30 dnssec => 1,
31 adflag => 1,
32 );
33 },
34 2 );
35  
36 2 has 'tlsa' => ( is => 'rw', isa => 'Bool', default => 0 );
37 2 has 'openpgpkey' => ( is => 'rw', isa => 'Bool', default => 0 );
38  
39 has 'logging_callback' => (
40 is => 'rw', isa => 'CodeRef', lazy => 1,
41 traits => [ 'Code' ],
42 handles => {
43 log => 'execute',
44 },
45 0 ▶ default => sub { sub {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 2 );
47  
48 2 has 'debug' => ( is => 'ro', isa => 'Bool', default => 0 );
49  
50 2 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=9.2
Pod undocumented
51 2 my $self = shift;
52 2 ▶ if( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 2 2
53 0 ▶ $self->logging_callback( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 print STDERR shift."\n";
55 0 } );
56 }
57 }
58  
59 has '_known_hosts' => (
60 is => 'ro', isa => 'ArrayRef', lazy => 1,
61 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
62 2 traits => [ 'Array' ],
63 handles => {
64 '_reset_known_hosts' => 'clear',
65 '_add_known_host' => 'push',
66 }
67 );
68  
69 0 ▶sub _is_known_host {
Subroutine _is_known_host: not called CC=2 SCAR=17.9
70 0 my ( $self, $host ) = @_;
71 0 ▶ if( grep { $_ eq $host } @{$self->_known_hosts} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $host;} @{$self->_known_hosts;}) 0 0 0
72 0 return 1;
73 }
74 0 return 0;
75 }
76  
77  
78 4 ▶sub resolve {
Subroutine resolve: called CC=6 SCAR=34.6
Pod documented
79 4 my ( $self, $domain ) = @_;
80  
81 4 ▶ if( defined $self->host ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->host) { } 4 0 4
82 4 return $self->host;
83 } else {
84 0 $self->log('looking up MX for '.$domain.'...');
85 0 my $reply = $self->resolver->query( $domain, 'MX' );
86 0 ▶ if( $reply->answer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->answer) 0 0 0
87 0 my @mx = grep { $_->type eq 'MX' } $reply->answer;
88 0 @mx = sort { $a->preference <=> $b->preference } @mx;
89 0 my @known_hosts = grep { $self->_is_known_host($_->exchange) } @mx;
90  
91 0 my $ex;
92 0 ▶ if( @known_hosts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@known_hosts) { } 0 0 0
93 0 $ex = $known_hosts[0]->exchange;
94 } else {
95 0 $ex = $mx[0]->exchange;
96 0 $self->_add_known_host( $ex );
97 }
98 0 ▶ $self->log('found '.scalar(@mx).' records. using: '.$ex.
Branch: 1 branch
BranchTrueFalseTotal
@known_hosts ? : 0 0 0
99 ( @known_hosts ? ' (reuse)' : '') );
100 0 return $ex;
101 }
102 0 $self->log('looking up AAAA,A for '.$domain.'...');
103 0 $reply = $self->resolver->query( $domain, 'AAAA', 'A' );
104 0 ▶ if( my @rr = $reply->answer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rr) = $reply->answer) 0 0 0
105 0 $self->log('found '.scalar(@rr).' address records');
106 0 return $domain;
107 }
108 0 $self->log('unable to resolve domain '.$domain);
109 0 return; # lookup failed
110 }
111  
112 0 die('unknown mode: '.$self->mode);
113 0 return;
114 }
115  
116  
117 0 ▶sub check_tlsa {
Subroutine check_tlsa: not called CC=4 SCAR=30.0
Pod documented
118 0 my ( $self, $host, $port ) = @_;
119 0 ▶ if( ! defined $port ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 0 0 0
120 0 $port = 25;
121 }
122 0 my $tlsa_name = '_'.$port.'._tcp.'.$host;
123 0 $self->log('looking up TLSA for '.$tlsa_name.'...');
124 0 my $reply = $self->resolver->send( $tlsa_name, 'TLSA' );
125  
126 0 ▶ if( ! $reply->header->ad ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply->header->ad) 0 0 0
127 0 $self->log('no adflag set in response');
128 0 return 0;
129 }
130  
131 0 ▶ if( ! $reply->answer ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply->answer) 0 0 0
132 0 $self->log('no TLSA record published');
133 0 return 0;
134 }
135  
136 0 return 1;
137 }
138  
139 0 ▶sub check_openpgpkey {
Subroutine check_openpgpkey: not called CC=4 SCAR=30.0
Pod undocumented
140 0 my ( $self, $rs, @rcpts ) = @_;
141  
142 0 foreach my $rcpt ( @rcpts ) {
143 0 my ( $local, $domain ) = split('@', $rcpt, 2);
144 0 my $name = join('.', sha224_hex($local), '_openpgpkey', $domain);
145 0 $self->log('looking up OPENPGPKEY: '.$name.'...');
146 0 my $reply = $self->resolver->send( $name, 'TYPE61' );
147 0 ▶ if( ! $reply->header->ad ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $reply->header->ad) { } 0 0 0
elsif (not $reply->answer) { } 0 0 0
148 0 $self->log('no adflag set in response');
149 0 $rs->set( $rcpt, 'has_openpgpkey', 0 );
150 } elsif( ! $reply->answer ) {
151 0 $self->log('no OPENPGPKEY record found');
152 0 $rs->set( $rcpt, 'has_openpgpkey', 0 );
153 } else {
154 0 $self->log('OPENPGPKEY record found');
155 0 $rs->set( $rcpt, 'has_openpgpkey', 1 );
156 }
157 }
158  
159 0 return;
160 }
161  
162 4 ▶sub check_smtp {
Subroutine check_smtp: called CC=4 SCAR=14.5
Pod undocumented
163 4 my ( $self, $rs, $host, $size, $sender, @rcpts ) = @_;
164  
165 4 $self->log('connecting to '.$host.' on port '.$self->port.'...');
166 4 my $smtp = Net::SMTP->new( $host,
167 Port => $self->port,
168 Hello => $self->helo_name,
169 Timeout => $self->timeout,
170 );
171 4 ▶ if( ! defined $smtp ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $smtp) 0 4 4
172 0 $self->log('connection failed: '.$@);
173 0 $rs->set( \@rcpts, 'error', 'connection failed: '.$@ );
174 0 return;
175 }
176  
177 4 ▶ $rs->set( \@rcpts, 'has_starttls',
Branch: 1 branch
BranchTrueFalseTotal
defined $smtp->supports('STARTTLS') ? : 4 0 4
178 defined $smtp->supports('STARTTLS') ? 1 : 0 );
179  
180 4 ▶ if( defined $smtp->supports('PIPELINING') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $smtp->supports('PIPELINING')) { } 3 1 4
181 3 $self->check_smtp_addresses_pipelining( $rs, $smtp, $size, $sender, @rcpts );
182 } else {
183 1 $self->check_smtp_addresses( $rs, $smtp, $size, $sender, @rcpts );
184 }
185  
186 4 $self->log('sending QUIT...');
187 4 $smtp->quit;
188 4 return;
189 }
190  
191 1 ▶sub check_smtp_addresses {
Subroutine check_smtp_addresses: called CC=5 SCAR=17.0
Pod undocumented
192 1 my ( $self, $rs, $smtp, $size, $sender, @rcpts ) = @_;
193 1 $self->log('sending MAIL '.$sender.'...');
194 1 ▶ my $mail_ok = $smtp->mail( $sender,
Branch: 1 branch
BranchTrueFalseTotal
defined $size && $smtp->supports('SIZE') ? : 0 1 1
Condition33%1/3
defined $size and $smtp->supports("SIZE")
!ll&&!rl&&r
010
MC/DC0%0/2
defined $size and $smtp->supports("SIZE")
defined $size $smtp->supports("SIZE")
Truth table33%1/3
A and B
A= defined $sizeB= $smtp->supports("SIZE")
ABresult
0X0
100
111
195 defined $size && $smtp->supports('SIZE') ? ( Size => $size ):()
196 );
197 1 my $msg = $smtp->message; chomp($msg);
198 1 $self->log('server said: '.$msg);
199 1 ▶ if( ! $mail_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mail_ok) 0 1 1
200 0 $rs->set( \@rcpts, 'smtp_message', $msg );
201 0 $rs->set( \@rcpts, 'smtp_code', $smtp->code );
202 0 return;
203 }
204  
205 1 foreach my $rcpt ( @rcpts ) {
206 3 $self->log('sending RCPT '.$rcpt.'...');
207 3 my $rcpt_ok = $smtp->recipient( $rcpt );
208 3 my $msg = $smtp->message; chomp( $msg );
209 3 $self->log( 'server said: '.$msg );
210 3 $rs->set( $rcpt, 'smtp_message', $msg );
211 3 $rs->set( $rcpt, 'smtp_code', $smtp->code );
212 }
213 1 return;
214 }
215  
216 2 has 'rcpt_bulk_size' => ( is => 'ro', isa => 'Int', default => 10 );
217  
218 3 ▶sub check_smtp_addresses_pipelining {
Subroutine check_smtp_addresses_pipelining: called CC=8 SCAR=20.9
Pod undocumented
219 3 my ( $self, $rs, $smtp, $size, $sender, @rcpts ) = @_;
220 3 my $mail_sent = 0;
221  
222 3 ▶ while( my @bulk_rcpts = splice(@rcpts, 0, $self->rcpt_bulk_size) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bulk_rcpts) = splice(@rcpts, 0, $self->rcpt_bulk_size)) 3 2 5
223 3 $self->log('sending pipelined bulk...');
224 3 my $bulk = '';
225 3 ▶ if( ! $mail_sent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mail_sent) 3 0 3
226 3 ▶ $bulk .= 'MAIL FROM: <'.$sender.'>'
Branch: 1 branch
BranchTrueFalseTotal
defined $size && $smtp->supports('SIZE') ? : 0 3 3
Condition33%1/3
defined $size and $smtp->supports("SIZE")
!ll&&!rl&&r
030
MC/DC0%0/2
defined $size and $smtp->supports("SIZE")
defined $size $smtp->supports("SIZE")
Truth table33%1/3
A and B
A= defined $sizeB= $smtp->supports("SIZE")
ABresult
0X0
100
111
227 .( defined $size && $smtp->supports('SIZE') ? ' SIZE='.$size : '' )
228 ."\n"
229 }
230 $bulk .= join("\n",
231 3 map { 'RCPT TO: <'.$_.'>' } @bulk_rcpts,
232 )."\n";
233  
234 3 $smtp->datasend( $bulk );
235  
236 3 ▶ if( ! $mail_sent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mail_sent) 3 0 3
237 3 my $resp = $smtp->response;
238 3 my $msg = $smtp->message; chomp( $msg );
239 3 $self->log("server response to MAIL: ".$msg );
240 3 ▶ if( $resp != CMD_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp != 2) 1 2 3
241 1 $rs->set( [ @bulk_rcpts, @rcpts ], 'smtp_code', $smtp->code );
242 1 $rs->set( [ @bulk_rcpts, @rcpts ], 'smtp_message', $msg );
243 1 return;
244 }
245 2 $mail_sent = 1;
246 }
247  
248 2 foreach my $rcpt ( @bulk_rcpts ) {
249 6 $smtp->response;
250 6 my $msg = $smtp->message; chomp( $msg );
251 6 $self->log("server response to RCPT $rcpt: ".$msg );
252 6 $rs->set( $rcpt, 'smtp_code', $smtp->code );
253 6 $rs->set( $rcpt, 'smtp_message', $msg );
254 }
255 }
256 2 return;
257 }
258  
259  
260 4 ▶sub check {
Subroutine check: called CC=9 SCAR=22.7
Pod documented
261 4 my ( $self, $size, $sender, @rcpts ) = @_;
262 4 my $rs = Net::SMTP::Verify::ResultSet->new;
263  
264 4 my $by_domain = {};
265 4 foreach my $rcpt ( @rcpts ) {
266 12 my ( $user, $domain ) = split('@', $rcpt, 2);
267 12 ▶ if( ! defined $by_domain->{$domain} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $by_domain->{$domain}) 4 8 12
268 4 $by_domain->{$domain} = [];
269 }
270 12 push( @{$by_domain->{$domain}}, $rcpt );
271 }
272  
273 4 my $by_host = {};
274 4 $self->_reset_known_hosts;
275 4 foreach my $domain ( keys %$by_domain ) {
276 4 my $host = $self->resolve( $domain );
277 4 ▶ if( ! defined $host ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $host) 0 4 4
278 0 $rs->set( $by_domain->{$domain},
279 'error', 'unable to lookup '.$domain );
280 0 return;
281 }
282 4 ▶ if( ! defined $by_host->{$host} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $by_host->{$host}) 4 0 4
283 4 $by_host->{$host} = [];
284 }
285 4 push( @{$by_host->{$host}}, @{$by_domain->{$domain}} );
286 }
287  
288 4 foreach my $host ( keys %$by_host ) {
289 4 ▶ if( $self->tlsa ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tlsa) 0 4 4
290 0 $rs->set( $by_host->{$host},
291 'has_tlsa', $self->check_tlsa( $host ) );
292 }
293 4 $self->check_smtp( $rs, $host, $size, $sender, @{$by_host->{$host}} );
294 }
295  
296 4 ▶ if( $self->openpgpkey ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->openpgpkey) 0 4 4
297 0 $self->check_openpgpkey( $rs, @rcpts );
298 }
299  
300 4 return $rs;
301 }
302  
303 2 1;
304  
305 __END__
↑ Summary Verify/ResultSet.pm »