blib/lib/Mail/CheckUser.pm

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