blib/lib/Mail/CheckUser.pm

filter: Statementstmt 68.2% 174 / 255 Branchbran 37.5% 42 / 112 Conditioncond 31.7% 13 / 41 MC/DCmcdc 20.8% 5 / 24 Subroutinesub 96.0% 24 / 25 Podpod 33.3% 2 / 6 totaltotal 56.1% 260 / 463 SCAR 64.1
CC
79
Cov
56%
check_network
351.1
check_user_on_host
240.0
_result
15.5
CRAP 606.0
↑ Summary
1 # Copyright (c) 1999-2003 by Ilya Martynov. All rights
2 # reserved.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the same terms as Perl itself.
6  
7 package Mail::CheckUser;
8  
9 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 require Exporter;
13  
14 6 @ISA = qw(Exporter);
15  
16 6 @EXPORT_OK = qw(check_email
17 last_check
18 check_hostname
19 check_username);
20 6 $EXPORT_TAGS{constants} = [qw(CU_OK
21 CU_BAD_SYNTAX
22 CU_UNKNOWN_DOMAIN
23 CU_DNS_TIMEOUT
24 CU_UNKNOWN_USER
25 CU_SMTP_TIMEOUT
26 CU_SMTP_UNREACHABLE
27 CU_MAILBOX_FULL
28 CU_TRY_AGAIN)];
29 6 push @EXPORT_OK, @{$EXPORT_TAGS{constants}};
30  
31 6 $VERSION = '1.23';
32  
33 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 7 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 6 ▶use Net::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 6 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 6 use vars qw($Skip_Network_Checks $Skip_SMTP_Checks
39 $Skip_SYN $Net_DNS_Resolver
40 $NXDOMAIN
41 $Timeout $Treat_Timeout_As_Fail $Debug
42 $Treat_Full_As_Fail
43 $Treat_Grey_As_Fail
44 6 ▶ $Sender_Addr $Helo_Domain $Last_Check);
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 # if it is true Mail::CheckUser doesn't make network checks
47 6 $Skip_Network_Checks = 0;
48 # if it is true Mail::CheckUser doesn't try to connect to mail
49 # server to check if user is valid
50 6 $Skip_SMTP_Checks = 0;
51 # timeout in seconds for network checks
52 6 $Timeout = 60;
53 # if it is true the Net::Ping SYN/ACK check will be skipped
54 6 $Skip_SYN = 0;
55 # if it is true Mail::CheckUser treats timeouted checks as
56 # failed checks
57 6 $Treat_Timeout_As_Fail = 0;
58 # if it is true Mail::CheckUser treats mailbox full message
59 # as failed checks
60 6 $Treat_Full_As_Fail = 0;
61 # if it is true Mail::CheckUser treats temporary (400's)
62 # as failed checks
63 6 $Treat_Grey_As_Fail = 0;
64 # sender addr used in MAIL/RCPT check
65 6 $Sender_Addr = 'check@user.com';
66 # sender domain used in HELO SMTP command - if undef lets
67 # Net::SMTP use its default value
68 6 $Helo_Domain = undef;
69 # Default Net::DNS::Resolver override object
70 6 $Net_DNS_Resolver = undef;
71 # if true then enable debug mode
72 6 $Debug = 0;
73 # Wildcard gTLD always denote bogus domains
74 # (http://www.imperialviolet.org/dnsfix.html)
75 ## gTLD Wildcard IPs
76 6 $NXDOMAIN = {
77 # com/net
78 "64.94.110.11" => 1, # A
79  
80 # ac
81 "194.205.62.122" => 1, # A
82  
83 # cc
84 "206.253.214.102" => 1, # A
85 "snubby.enic.cc" => 1, # MX
86 "206.191.159.103" => 1, # MX
87  
88 # cx
89 "219.88.106.80" => 1, # A
90 "mail.nonregistered.nic.cx" => 1, # MX
91  
92 # mp
93 "202.128.12.163" => 1, # A
94  
95 # museum
96 "195.7.77.20" => 1, # A
97  
98 # nu
99 "64.55.105.9" => 1, # A
100 "212.181.91.6" => 1, # A
101  
102 # ph
103 "203.119.4.6" => 1, # A
104 "45.79.222.138" => 1, # A
105  
106 # pw
107 "216.98.141.250" => 1, # A
108 "65.125.231.178" => 1, # A
109 "wfb.dnsvr.com" => 1, # CNAME
110  
111 # sh
112 "194.205.62.62" => 1, # A
113  
114 # td
115 "146.101.245.154" => 1, # A
116 "www.nic.td" => 1, # CNAME
117  
118 # tk
119 "195.20.32.83" => 1, # A
120 "195.20.32.86" => 1, # A
121 "nukumatau.taloha.com" => 1, # MX
122 "195.20.32.99" => 1, # MX
123  
124 # tm
125 "194.205.62.42" => 1, # A
126  
127 # tw
128 "203.73.24.11" => 1, # A
129  
130 # ws
131 "216.35.187.246" => 1, # A
132 "mail.worldsite.ws" => 1, # MX
133 "mail.hope-mail.com" => 1, # MX
134 "216.35.187.251" => 1, # MX
135  
136 };
137  
138 # check_email EMAIL
139 sub check_email( $ );
140 # last_check
141 sub last_check( );
142 # check_hostname_syntax HOSTNAME
143 sub check_hostname_syntax( $ );
144 # check_username_syntax USERNAME
145 sub check_username_syntax( $ );
146 # check_network HOSTNAME, USERNAME
147 sub check_network( $$ );
148 # check_user_on_host MSERVER, USERNAME, HOSTNAME, TIMEOUT
149 sub check_user_on_host( $$$$ );
150 # _calc_timeout FULL_TIMEOUT START_TIME
151 sub _calc_timeout( $$ );
152 # _pm_log LOG_STR
153 sub _pm_log( $ );
154 # _result RESULT, REASON
155 sub _result( $$ );
156  
157 # check result codes
158 6 ▶use constant CU_OK => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
159 6 ▶use constant CU_BAD_SYNTAX => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
160 6 ▶use constant CU_UNKNOWN_DOMAIN => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 6 ▶use constant CU_DNS_TIMEOUT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
162 6 ▶use constant CU_UNKNOWN_USER => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 6 ▶use constant CU_SMTP_TIMEOUT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 6 ▶use constant CU_SMTP_UNREACHABLE => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 6 ▶use constant CU_MAILBOX_FULL => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
166 6 ▶use constant CU_TRY_AGAIN => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
167  
168 sub check_email($) {
169 50 ▶ my($email) = @_;
Subroutine check_email: called CC=8 SCAR=21.1
Pod documented
170  
171 50 ▶ unless(defined $email) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $email) 0 50 50
172 0 croak __PACKAGE__ . "::check_email: \$email is undefined";
173 }
174  
175 50 _pm_log '=' x 40;
176 50 _pm_log "check_email: checking \"$email\"";
177  
178 # split email address on username and hostname
179 50 my($username, $hostname) = $email =~ /^(.*)@(.*)$/;
180 # return false if it impossible
181 50 ▶ unless(defined $hostname) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hostname) 2 48 50
182 2 return _result(CU_BAD_SYNTAX, 'bad address format: missing @');
183 }
184  
185 48 my $ok = 1;
186 48 ▶ $ok &&= check_hostname_syntax $hostname;
Condition66%2/3
$ok &&= &check_hostname_syntax($hostname)
!ll&&!rl&&r
0840
MC/DC50%1/2
$ok &&= &check_hostname_syntax($hostname)
$ok &check_hostname_syntax($hostname)
Truth table66%2/3
A &&= B
A= $okB= &check_hostname_syntax($hostname)
ABresult
0X0
100
111
187 48 ▶ $ok &&= check_username_syntax $username;
Condition100%3/3
$ok &&= &check_username_syntax($username)
!ll&&!rl&&r
8832
MC/DC100%2/2
$ok &&= &check_username_syntax($username)
$ok &check_username_syntax($username)
Truth table100%3/3
A &&= B
A= $okB= &check_username_syntax($username)
ABresult
0X0
100
111
188 48 ▶ if($Skip_Network_Checks) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Mail::CheckUser::Skip_Network_Checks) { } 28 20 48
elsif ($ok) { } 20 0 20
189 28 _pm_log "check_email: skipping network checks";
190 } elsif ($ok) {
191 20 ▶ $ok &&= check_network $hostname, $username;
Condition66%2/3
$ok &&= &check_network($hostname, $username)
!ll&&!rl&&r
0164
MC/DC50%1/2
$ok &&= &check_network($hostname, $username)
$ok &check_network($hostname, $username)
Truth table66%2/3
A &&= B
A= $okB= &check_network($hostname, $username)
ABresult
0X0
100
111
192 }
193  
194 45 return $ok;
195 }
196  
197 sub last_check() {
198 6 ▶ return $Mail::CheckUser::Last_Check;
Subroutine last_check: called CC=1 SCAR=0.0
Pod documented
199 }
200  
201 # build hostname regexp
202 # NOTE: it doesn't strictly follow RFC822
203 # because of what registrars now allow.
204 6 my $DOMAIN_RE = qr/(?:[\da-zA-Z]+ -+)* [\da-zA-Z]+/x;
205 6 my $HOSTNAME_RE = qr/^ (?:$DOMAIN_RE \.)+ [a-zA-Z]+ $/xo;
206  
207 sub check_hostname_syntax($) {
208 48 ▶ my($hostname) = @_;
Subroutine check_hostname_syntax: called CC=2 SCAR=6.9
Pod undocumented
209  
210 48 _pm_log "check_hostname_syntax: checking \"$hostname\"";
211  
212 # check if hostname syntax is correct
213 48 ▶ if($hostname =~ $HOSTNAME_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hostname =~ /$HOSTNAME_RE/x) { } 40 8 48
214 40 return _result(CU_OK, 'correct hostname syntax');
215 } else {
216 8 return _result(CU_BAD_SYNTAX, 'bad hostname syntax');
217 }
218 }
219  
220 # build username regexp
221 # NOTE: it doesn't strictly follow RFC821
222 6 my $STRING_RE = ('[' . quotemeta(join '',
223 grep(!/[<>()\[\]\\\.,;:\@"]/, # ["], UnBug Emacs
224 map chr, 33 .. 126)) . ']');
225 6 my $USERNAME_RE = qr/^ (?:$STRING_RE+ \.)* $STRING_RE+ $/xo;
226  
227  
228 sub check_username_syntax($) {
229 40 ▶ my($username) = @_;
Subroutine check_username_syntax: called CC=2 SCAR=6.9
Pod undocumented
230  
231 40 _pm_log "check_username_syntax: checking \"$username\"";
232  
233 # check if username syntax is correct
234 40 ▶ if($username =~ $USERNAME_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($username =~ /$USERNAME_RE/x) { } 32 8 40
235 32 return _result(CU_OK, 'correct username syntax');
236 } else {
237 8 return _result(CU_BAD_SYNTAX, 'bad username syntax');
238 }
239 }
240  
241 sub check_network($$) {
242 20 ▶ my($hostname, $username) = @_;
Subroutine check_network: called CC=43 SCAR=58.6
Pod undocumented
243  
244 20 _pm_log "check_network: checking \"$username\" on \"$hostname\"";
245  
246 # list of mail servers for hostname
247 20 my @mservers = ();
248  
249 20 my $timeout = $Timeout;
250 20 my $start_time = time;
251  
252 20 ▶ my $resolver = $Mail::CheckUser::Net_DNS_Resolver || new Net::DNS::Resolver;
Condition33%1/3
$Mail::CheckUser::Net_DNS_Resolver || 'Net::DNS::Resolver'->new
l!l&&r!l&&!r
0200
MC/DC0%0/2
$Mail::CheckUser::Net_DNS_Resolver || 'Net::DNS::Resolver'->new
$Mail::CheckUser::Net_DNS_Resolver 'Net::DNS::Resolver'->new
Truth table33%1/3
A || B
A= $Mail::CheckUser::Net_DNS_ResolverB= 'Net::DNS::Resolver'->new
ABresult
1X1
011
000
253 20 my $tout = _calc_timeout($timeout, $start_time);
254 20 ▶ return _result(CU_DNS_TIMEOUT, 'DNS timeout') if $tout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tout == 0 0 20 20
255 20 $resolver->udp_timeout($tout);
256  
257 20 my @mx = mx($resolver, "$hostname.");
258 20 $tout = _calc_timeout($timeout, $start_time);
259 20 ▶ return _result(CU_DNS_TIMEOUT, 'DNS timeout') if $tout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tout == 0 1 19 20
260  
261 # check result of query
262 19 ▶ if(@mx) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mx) { } 7 12 19
263 # if MX record exists,
264 # then it's already sorted by preference
265 7 @mservers = map {$_->exchange} @mx;
266 } else {
267 # if there is no MX record try hostname as mail server
268 12 my $tout = _calc_timeout($timeout, $start_time);
269 12 ▶ return _result(CU_DNS_TIMEOUT, 'DNS timeout') if $tout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tout == 0 0 12 12
270 12 $resolver->udp_timeout($tout);
271  
272 12 my $res = $resolver->search("$hostname.", 'A');
273 # check if timeout has happen
274 12 $tout = _calc_timeout($timeout, $start_time);
275 12 ▶ return _result(CU_DNS_TIMEOUT, 'DNS timeout') if $tout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tout == 0 0 12 12
276  
277 # check result of query
278 12 ▶ if($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) { } 1 11 12
279 1 @mservers = ($hostname);
280 1 my $ip;
281 1 foreach my $rr ($res->answer) {
282 1 ▶ if ($rr->type eq "A") {
Branch: 2 branches
BranchTrueFalseTotal
if ($rr->type eq 'A') { } 1 0 1
elsif ($rr->type eq 'CNAME') { } 0 0 0
283 1 $ip = $rr->address;
284 1 last;
285 } elsif ($rr->type eq "CNAME") {
286 0 $ip = $rr->cname;
287 } else {
288 # Should never happen!
289 0 $ip = "";
290 }
291 }
292 1 _pm_log "check_network: \"$ip\" Wildcard gTLD check";
293 1 ▶ return _result(CU_UNKNOWN_DOMAIN, 'Wildcard gTLD') if $NXDOMAIN->{lc $ip};
Branch: 1 branch
BranchTrueFalseTotal
if $$Mail::CheckUser::NXDOMAIN{lc $ip} 1 0 1
294 } else {
295 11 return _result(CU_UNKNOWN_DOMAIN, 'DNS failure: ' . $resolver->errorstring);
296 }
297 }
298  
299 7 foreach my $mserver (@mservers) {
300 15 _pm_log "check_network: \"$mserver\" Wildcard gTLD check";
301 15 ▶ return _result(CU_UNKNOWN_DOMAIN, 'Wildcard gTLD') if $NXDOMAIN->{lc $mserver};
Branch: 1 branch
BranchTrueFalseTotal
if $$Mail::CheckUser::NXDOMAIN{lc $mserver} 1 14 15
302 }
303  
304 6 ▶ if($Skip_SMTP_Checks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::CheckUser::Skip_SMTP_Checks) { } 3 3 6
305 3 return _result(CU_OK, 'skipping SMTP checks');
306 } else {
307 3 ▶ if ($Skip_SYN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::CheckUser::Skip_SYN) { } 0 3 3
308 # Skip SYN/ACK check.
309 # Just check user on each mail server one at a time.
310 0 foreach my $mserver (@mservers) {
311 0 my $tout = _calc_timeout($timeout, $start_time);
312 0 ▶ if ($mserver !~ /^\d+\.\d+\.\d+\.\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mserver =~ /^\d+\.\d+\.\d+\.\d+$/) 0 0 0
313 # Resolve it to an IP
314 0 ▶ return _result(CU_DNS_TIMEOUT, 'DNS timeout') if $tout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tout == 0 0 0 0
315 0 $resolver->udp_timeout($tout);
316 0 ▶ if (my $ans = $resolver->query($mserver)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ans = $resolver->query($mserver)) 0 0 0
317 0 foreach my $rr_a ($ans->answer) {
318 0 ▶ if ($rr_a->type eq "A") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr_a->type eq "A") 0 0 0
319 0 $mserver = $rr_a->address;
320 0 last;
321 }
322 }
323 }
324 0 $tout = _calc_timeout($timeout, $start_time);
325 }
326 0 ▶ return _result(CU_SMTP_TIMEOUT, 'SMTP timeout') if $tout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tout == 0 0 0 0
327  
328 0 my $res = check_user_on_host $mserver, $username, $hostname, $tout;
329  
330 0 ▶ return 1 if $res == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $res == 1 0 0 0
331 0 ▶ return 0 if $res == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $res == 0 0 0 0
332 }
333 } else {
334 # Determine which mail servers are on
335 3 my $resolve = {};
336 3 my $tout = _calc_timeout($timeout, $start_time);
337 3 foreach my $mserver (@mservers) {
338 # All mservers need to be resolved to IPs before the SYN check
339 11 ▶ if ($mserver =~ /^\d+\.\d+\.\d+\.\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mserver =~ /^\d+\.\d+\.\d+\.\d+$/) { } 0 11 11
340 0 $resolve->{$mserver} = 1;
341 } else {
342 11 _pm_log "check_network: \"$mserver\" resolving";
343 11 ▶ return _result(CU_DNS_TIMEOUT, 'DNS timeout') if $tout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tout == 0 0 11 11
344 11 $resolver->udp_timeout($tout);
345 11 ▶ if (my $ans = $resolver->query($mserver)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ans = $resolver->query($mserver)) { } 11 0 11
346 11 foreach my $rr_a ($ans->answer) {
347 11 ▶ if ($rr_a->type eq "A") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr_a->type eq "A") 11 0 11
348 11 $mserver = $rr_a->address;
349 11 $resolve->{$mserver} = 1;
350 11 _pm_log "check_network: resolved to IP \"$mserver\"";
351 11 last;
352 }
353 }
354 } else {
355 0 _pm_log "check_network: \"$mserver\" host not found!";
356 }
357 11 $tout = _calc_timeout($timeout, $start_time);
358 }
359 }
360  
361 3 require Net::Ping;
362 3 import Net::Ping 2.24;
363 # Use only three-fourths of the full timeout for lookups
364 # in order to leave time to actually speak to the server.
365 3 my $ping = Net::Ping->new("syn", _calc_timeout($timeout, $start_time) * 3 / 4 + 1);
366 0 $ping->{port_num} = getservbyname("smtp", "tcp");
367 0 $ping->tcp_service_check(1);
368 0 foreach my $mserver (@mservers) {
369 0 _pm_log "check_network: \"$mserver\" sending SYN...";
370 # untaint before passing to Net::Ping
371 0 my ($tainted) = $mserver =~ /(\d+\.\d+\.\d+\.\d+)/;
372 0 ▶ if ($tainted and $tainted eq $mserver and
Branch: 1 branch
BranchTrueFalseTotal
if ($tainted and $tainted eq $mserver and $resolve->{$tainted} and $ping->ping($tainted)) { } 0 0 0
Condition0%0/9
$tainted and $tainted eq $mserver and $resolve->{$tainted} and $ping->ping($tainted)
!ll&&!rl&&r
000
$tainted and $tainted eq $mserver and $resolve->{$tainted}
!ll&&!rl&&r
000
$tainted and $tainted eq $mserver
!ll&&!rl&&r
000
MC/DC0%0/4
$tainted and $tainted eq $mserver and $resolve->{$tainted} and $ping->ping($tainted)
$tainted $tainted eq $mserver $resolve->{$tainted} $ping->ping($tainted)
Truth table0%0/5
A and B and C and D
A= $taintedB= $tainted eq $mserverC= $resolve->{$tainted}D= $ping->ping($tainted)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
373 $resolve->{$tainted} and $ping->ping($tainted)) {
374 0 _pm_log "check_network: \"$tainted\" SYN packet sent.";
375 } else {
376 0 _pm_log "check_network: \"$mserver\" host not found!";
377 }
378 }
379 0 foreach my $mserver (@mservers) {
380 0 my $tout = _calc_timeout($timeout, $start_time);
381 0 ▶ return _result(CU_SMTP_TIMEOUT, 'SMTP timeout') if $tout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tout == 0 0 0 0
382  
383 0 _pm_log "check_network: \"$mserver\" waiting for ACK";
384 0 ▶ if ($resolve->{$mserver}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolve->{$mserver}) { } 0 0 0
385 # untaint before passing to Net::Ping
386 0 my($mserver) = $mserver =~ /(\d+\.\d+\.\d+\.\d+)/;
387 0 ▶ if ($ping->ack($mserver)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ping->ack($mserver)) { } 0 0 0
388 0 _pm_log "check_network: \"$mserver\" ACK received.";
389 # check user on this mail server
390 0 my $res = check_user_on_host $mserver, $username, $hostname, $tout;
391  
392 0 ▶ return 1 if $res == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $res == 1 0 0 0
393 0 ▶ return 0 if $res == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $res == 0 0 0 0
394 } else {
395 0 ▶ _pm_log "check_network: \"$mserver\" no ACK received: [".
Condition0%0/2
$ping->nack($mserver) || 'no SYN sent'
l!l
00
MC/DC0%0/1
$ping->nack($mserver) || 'no SYN sent'
$ping->nack($mserver)
Truth table0%0/2
A
A= $ping->nack($mserver)
Aresult
11
00
396 ($ping->nack($mserver) || "no SYN sent")."]";
397 }
398 } else {
399 0 _pm_log "check_network: skipping check_user_on_host \"$mserver\" since it did not resolve";
400 }
401 }
402 }
403  
404 0 return _result(CU_SMTP_UNREACHABLE,
405 'Cannot connect SMTP servers: ' .
406 join(', ', @mservers));
407 }
408  
409 # it should be impossible to reach this statement
410 0 die "Internal error";
411 }
412  
413 sub check_user_on_host($$$$) {
414 0 ▶ my($mserver, $username, $hostname, $timeout) = @_;
Subroutine check_user_on_host: not called CC=15 SCAR=54.8
Pod undocumented
415  
416 0 _pm_log "check_user_on_host: checking user \"$username\" on \"$mserver\"";
417  
418 0 my $start_time = time;
419  
420 # disable warnings because Net::SMTP can generate some on timeout
421 # conditions
422 0 local $^W = 0;
423  
424 # try to connect to mail server
425 0 my $tout = _calc_timeout($timeout, $start_time);
426 0 ▶ return _result(CU_SMTP_TIMEOUT, 'SMTP timeout') if $tout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tout == 0 0 0 0
427  
428 0 ▶ my @hello_params = defined $Helo_Domain ? (Hello => $Helo_Domain) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $Mail::CheckUser::Helo_Domain ? : 0 0 0
429 0 my $smtp = Net::SMTP->new($mserver, Timeout => $tout, @hello_params);
430 0 ▶ unless(defined $smtp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $smtp) 0 0 0
431 0 _pm_log "check_user_on_host: unable to connect to \"$mserver\"";
432 0 return -1;
433 }
434  
435 # try to check if user is valid with MAIL/RCPT commands
436 0 $tout = _calc_timeout($timeout, $start_time);
437 0 ▶ return _result(CU_SMTP_TIMEOUT, 'SMTP timeout') if $tout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tout == 0 0 0 0
438 0 $smtp->timeout($tout);
439  
440 # send MAIL FROM command
441 0 ▶ unless($smtp->mail($Sender_Addr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($smtp->mail($Mail::CheckUser::Sender_Addr)) 0 0 0
442 # something wrong?
443  
444 # check for timeout
445 0 ▶ return _result(CU_SMTP_TIMEOUT, 'SMTP timeout') if $tout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tout == 0 0 0 0
446  
447 0 _pm_log "check_user_on_host: can't say MAIL - " . $smtp->message;
448 0 return -1;
449 }
450  
451 # send RCPT TO command
452 0 ▶ if($smtp->to("$username\@$hostname")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp->to("$username\@$hostname")) { } 0 0 0
453 # give server opportunity to exist gracefully by telling it QUIT
454 0 my $tout = _calc_timeout($timeout, $start_time);
455 0 ▶ if($tout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tout) 0 0 0
456 0 $smtp->timeout($tout);
457 0 $smtp->quit;
458 }
459  
460 0 return _result(CU_OK, 'SMTP server accepts username');
461 } else {
462 # check if verify returned error because of timeout
463 0 my $tout = _calc_timeout($timeout, $start_time);
464 0 ▶ return _result(CU_SMTP_TIMEOUT, 'SMTP timeout') if $tout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tout == 0 0 0 0
465  
466 0 my $code = $smtp->code;
467  
468 # give server opportunity to exist gracefully by telling it QUIT
469 0 $smtp->timeout($tout);
470 0 $smtp->quit;
471  
472 0 ▶ if($code == 550 or $code == 551 or $code == 553) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code == 550 or $code == 551 or $code == 553) { } 0 0 0
elsif ($code == 552) { } 0 0 0
elsif ($code =~ /^4/) { } 0 0 0
Condition0%0/6
$code == 550 or $code == 551 or $code == 553
l!l&&r!l&&!r
000
$code == 550 or $code == 551
l!l&&r!l&&!r
000
MC/DC0%0/3
$code == 550 or $code == 551 or $code == 553
$code == 550 $code == 551 $code == 553
Truth table0%0/4
A or B or C
A= $code == 550B= $code == 551C= $code == 553
ABCresult
1XX1
01X1
0011
0000
473 0 return _result(CU_UNKNOWN_USER, 'no such user');
474 } elsif($code == 552) {
475 0 return _result(CU_MAILBOX_FULL, 'mailbox full');
476 } elsif($code =~ /^4/) {
477 0 return _result(CU_TRY_AGAIN, 'temporary delivery failure');
478 } else {
479 0 _pm_log "check_user_on_host: unknown error in response";
480 0 return _result(CU_OK, 'unknown error in response');
481 }
482 }
483  
484  
485 # it should be impossible to reach this statement
486 0 die "Internal error";
487 }
488  
489 sub _calc_timeout($$) {
490 81 ▶ my($full_timeout, $start_time) = @_;
Subroutine _calc_timeout: called CC=2 SCAR=7.1
491  
492 81 my $now_time = time;
493 81 my $passed_time = $now_time - $start_time;
494 81 _pm_log "_calc_timeout: start - $start_time, now - $now_time";
495 81 _pm_log "_calc_timeout: timeout - $full_timeout, passed - $passed_time";
496  
497 81 my $timeout = $full_timeout - $passed_time;
498  
499 81 ▶ if($timeout < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout < 0) { } 0 81 81
500 0 return 0;
501 } else {
502 81 return $timeout;
503 }
504 }
505  
506 sub _pm_log($) {
507 543 ▶ my($log_str) = @_;
Subroutine _pm_log: called CC=2 SCAR=8.1
508  
509 543 ▶ if($Debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::CheckUser::Debug) 0 543 543
510 0 print STDERR "$log_str\n";
511 }
512 }
513  
514 sub _result($$) {
515 107 ▶ my($code, $reason) = @_;
Subroutine _result: called CC=12 SCAR=27.4
516  
517 107 my $ok = 0;
518  
519 107 ▶ $ok = 1 if $code == CU_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 0 75 32 107
520 107 ▶ $ok = 1 if $code == CU_SMTP_UNREACHABLE;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 6 0 107 107
521 107 ▶ $ok = 1 if $code == CU_MAILBOX_FULL and not $Treat_Full_As_Fail;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 7 and not $Mail::CheckUser::Treat_Full_As_Fail 0 107 107
Condition33%1/3
$code == 7 and not $Mail::CheckUser::Treat_Full_As_Fail
!ll&&!rl&&r
10700
MC/DC0%0/2
$code == 7 and not $Mail::CheckUser::Treat_Full_As_Fail
$code == 7 not $Mail::CheckUser::Treat_Full_As_Fail
Truth table33%1/3
A and not(B)
A= $code == 7B= not $Mail::CheckUser::Treat_Full_As_Fail
ABresult
0X0
100
111
522 107 ▶ $ok = 1 if $code == CU_DNS_TIMEOUT and not $Treat_Timeout_As_Fail;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 3 and not $Mail::CheckUser::Treat_Timeout_As_Fail 1 106 107
Condition66%2/3
$code == 3 and not $Mail::CheckUser::Treat_Timeout_As_Fail
!ll&&!rl&&r
10601
MC/DC50%1/2
$code == 3 and not $Mail::CheckUser::Treat_Timeout_As_Fail
$code == 3 not $Mail::CheckUser::Treat_Timeout_As_Fail
Truth table66%2/3
A and not(B)
A= $code == 3B= not $Mail::CheckUser::Treat_Timeout_As_Fail
ABresult
0X0
100
111
523 107 ▶ $ok = 1 if $code == CU_SMTP_TIMEOUT and not $Treat_Timeout_As_Fail;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 5 and not $Mail::CheckUser::Treat_Timeout_As_Fail 0 107 107
Condition33%1/3
$code == 5 and not $Mail::CheckUser::Treat_Timeout_As_Fail
!ll&&!rl&&r
10700
MC/DC0%0/2
$code == 5 and not $Mail::CheckUser::Treat_Timeout_As_Fail
$code == 5 not $Mail::CheckUser::Treat_Timeout_As_Fail
Truth table33%1/3
A and not(B)
A= $code == 5B= not $Mail::CheckUser::Treat_Timeout_As_Fail
ABresult
0X0
100
111
524 107 ▶ $ok = 1 if $code == CU_TRY_AGAIN and not $Treat_Grey_As_Fail;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 8 and not $Mail::CheckUser::Treat_Grey_As_Fail 0 107 107
Condition33%1/3
$code == 8 and not $Mail::CheckUser::Treat_Grey_As_Fail
!ll&&!rl&&r
10700
MC/DC0%0/2
$code == 8 and not $Mail::CheckUser::Treat_Grey_As_Fail
$code == 8 not $Mail::CheckUser::Treat_Grey_As_Fail
Truth table33%1/3
A and not(B)
A= $code == 8B= not $Mail::CheckUser::Treat_Grey_As_Fail
ABresult
0X0
100
111
525  
526 107 $Last_Check = { ok => $ok,
527 code => $code,
528 reason => $reason };
529  
530 107 my($sub) = (caller(1))[3] =~ /^.*::(.*)$/;
531  
532 107 ▶ _pm_log "$sub: check result is " .
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 76 31 107
533 ($ok ? 'ok' : 'not ok') .
534 ": [$code] $reason";
535  
536 107 return $ok;
537 }
538  
539 6 1;
540 __END__
↑ Summary