line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ==== |
2
|
|
|
|
|
|
|
# SSL/STARTTLS extention for Graham Barr's Net::POP3. |
3
|
|
|
|
|
|
|
# plus, enable arbitrary POP auth mechanism selection. |
4
|
|
|
|
|
|
|
# IO::Socket::SSL (also Net::SSLeay openssl), |
5
|
|
|
|
|
|
|
# Authen::SASL, MIME::Base64 should be installed. |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
package Net::POP3S; |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
24096
|
use vars qw ( $VERSION @ISA ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
54
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$VERSION = '0.07'; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
3
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
18
|
|
14
|
1
|
|
|
1
|
|
3
|
use base qw ( Net::POP3 ); |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
481
|
|
15
|
1
|
|
|
1
|
|
77672
|
use Net::Cmd; # import CMD_OK, CMD_MORE, ... |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
58
|
|
16
|
1
|
|
|
1
|
|
4
|
use Net::Config; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
935
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
eval { |
19
|
|
|
|
|
|
|
require IO::Socket::IP |
20
|
|
|
|
|
|
|
and unshift @ISA, 'IO::Socket::IP'; |
21
|
|
|
|
|
|
|
} or eval { |
22
|
|
|
|
|
|
|
require IO::Socket::INET6 |
23
|
|
|
|
|
|
|
and unshift @ISA, 'IO::Socket::INET6'; |
24
|
|
|
|
|
|
|
} or do { |
25
|
|
|
|
|
|
|
require IO::Socket::INET |
26
|
|
|
|
|
|
|
and unshift @ISA, 'IO::Socket::INET'; |
27
|
|
|
|
|
|
|
}; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Override to support SSL/TLS. |
30
|
|
|
|
|
|
|
sub new { |
31
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
32
|
0
|
|
0
|
|
|
|
my $type = ref($self) || $self; |
33
|
0
|
|
|
|
|
|
my ($host, %arg); |
34
|
0
|
0
|
|
|
|
|
if (@_ % 2) { |
35
|
0
|
|
|
|
|
|
$host = shift; |
36
|
0
|
|
|
|
|
|
%arg = @_; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
else { |
39
|
0
|
|
|
|
|
|
%arg = @_; |
40
|
0
|
|
|
|
|
|
$host = delete $arg{Host}; |
41
|
|
|
|
|
|
|
} |
42
|
0
|
|
|
|
|
|
my $ssl = delete $arg{doSSL}; |
43
|
0
|
0
|
|
|
|
|
$ssl = 'ssl' if delete $arg{SSL}; |
44
|
|
|
|
|
|
|
|
45
|
0
|
0
|
|
|
|
|
my $hosts = defined $host ? $host : $NetConfig{pop3_hosts}; |
46
|
0
|
|
|
|
|
|
my $obj; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# eliminate IO::Socket::SSL from @ISA for multiple call of new. |
49
|
0
|
|
|
|
|
|
@ISA = grep { !/IO::Socket::SSL/ } @ISA; |
|
0
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
my %_args = map { +"$_" => $arg{$_} } grep {! /^SSL/} keys %arg; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
my $h; |
54
|
0
|
|
0
|
|
|
|
$_args{PeerPort} = $_args{Port} || 'pop3(110)'; |
55
|
0
|
|
|
|
|
|
$_args{Proto} = 'tcp'; |
56
|
0
|
0
|
|
|
|
|
$_args{Timeout} = defined $_args{Timeout} ? $_args{Timeout} : 120; |
57
|
0
|
0
|
|
|
|
|
if (exists $_args{ResvPort}) { |
58
|
0
|
|
|
|
|
|
$_args{LocalPort} = delete $_args{ResvPort}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
|
foreach $h (@{ref($hosts) ? $hosts : [$hosts]}) { |
|
0
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
$_args{PeerAddr} = ($host = $h); |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
|
|
|
|
$obj = $type->SUPER::new( |
65
|
|
|
|
|
|
|
%_args |
66
|
|
|
|
|
|
|
) |
67
|
|
|
|
|
|
|
and last; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
return undef |
71
|
0
|
0
|
|
|
|
|
unless defined $obj; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
${*$obj}{'net_pop3_host'} = $host; |
|
0
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
$obj->autoflush(1); |
76
|
0
|
0
|
|
|
|
|
$obj->debug(exists $arg{Debug} ? $arg{Debug} : undef); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# common in SSL |
79
|
0
|
|
|
|
|
|
my %ssl_args; |
80
|
0
|
0
|
|
|
|
|
if ($ssl) { |
81
|
|
|
|
|
|
|
eval { |
82
|
0
|
|
|
|
|
|
require IO::Socket::SSL; |
83
|
0
|
0
|
|
|
|
|
} or do { |
84
|
0
|
|
|
|
|
|
$obj->set_status(500, ["Need working IO::Socket::SSL"]); |
85
|
0
|
|
|
|
|
|
$obj->close; |
86
|
0
|
|
|
|
|
|
return undef; |
87
|
|
|
|
|
|
|
}; |
88
|
0
|
|
|
|
|
|
%ssl_args = map { +"$_" => $arg{$_} } grep {/^SSL/} keys %arg; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
|
$IO::Socket::SSL::DEBUG = (exists $arg{Debug} ? $arg{Debug} : undef); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# OverSSL |
93
|
0
|
0
|
0
|
|
|
|
if (defined($ssl) && $ssl =~ /ssl/i) { |
94
|
|
|
|
|
|
|
$obj->ssl_start(\%ssl_args) |
95
|
0
|
0
|
|
|
|
|
or do { |
96
|
0
|
|
|
|
|
|
$obj->set_status(500, ["Cannot start SSL"]); |
97
|
0
|
|
|
|
|
|
$obj->close; |
98
|
0
|
|
|
|
|
|
return undef; |
99
|
|
|
|
|
|
|
}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
unless ($obj->response() == CMD_OK) { |
103
|
0
|
|
|
|
|
|
$obj->close(); |
104
|
0
|
|
|
|
|
|
return undef; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
${*$obj}{'net_pop3_banner'} = $obj->message; |
|
0
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# STARTTLS |
110
|
0
|
0
|
0
|
|
|
|
if (defined($ssl) && $ssl =~ /starttls|stls/i ) { |
111
|
0
|
|
|
|
|
|
my $capa; |
112
|
|
|
|
|
|
|
($capa = $obj->capa |
113
|
|
|
|
|
|
|
and exists $capa->{STLS} |
114
|
|
|
|
|
|
|
and ($obj->command('STLS')->response() == CMD_OK) |
115
|
|
|
|
|
|
|
and $obj->ssl_start(\%ssl_args)) |
116
|
0
|
0
|
0
|
|
|
|
or do { |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
117
|
0
|
|
|
|
|
|
$obj->set_status(500, ["Cannot start SSL session"]); |
118
|
0
|
|
|
|
|
|
$obj->close(); |
119
|
0
|
|
|
|
|
|
return undef; |
120
|
|
|
|
|
|
|
}; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
$obj; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub ssl_start { |
127
|
0
|
|
|
0
|
0
|
|
my ($self, $args) = @_; |
128
|
0
|
|
|
|
|
|
my $type = ref($self); |
129
|
|
|
|
|
|
|
|
130
|
0
|
0
|
0
|
|
|
|
(unshift @ISA, 'IO::Socket::SSL' |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
131
|
|
|
|
|
|
|
and IO::Socket::SSL->start_SSL($self, %$args) |
132
|
|
|
|
|
|
|
and $self->isa('IO::Socket::SSL') |
133
|
|
|
|
|
|
|
and bless $self, $type # re-bless 'cause IO::Socket::SSL blesses himself. |
134
|
|
|
|
|
|
|
) or return undef; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub capa { |
139
|
0
|
|
|
0
|
1
|
|
my $this = shift; |
140
|
|
|
|
|
|
|
|
141
|
0
|
0
|
|
|
|
|
if (exists ${*$this}{'net_pop3e_capabilities'}) { |
|
0
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
return ${*$this}{'net_pop3e_capabilities'}; |
|
0
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
} |
144
|
0
|
|
|
|
|
|
$this->SUPER::capa(); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# Override to specify a certain auth mechanism. |
148
|
|
|
|
|
|
|
sub auth { |
149
|
0
|
|
|
0
|
1
|
|
my ($self, $username, $password, $mech) = @_; |
150
|
|
|
|
|
|
|
|
151
|
0
|
0
|
|
|
|
|
if ($mech) { |
152
|
0
|
0
|
|
|
|
|
$self->debug_print(1, "my favorite: ". $mech . "\n") if $self->debug; |
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
|
my @cl_mech = split /\s+/, $mech; |
155
|
0
|
|
|
|
|
|
my @matched = (); |
156
|
0
|
|
0
|
|
|
|
my $sv = $self->capa->{SASL} || 'CRAM-MD5'; |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
foreach my $i (@cl_mech) { |
159
|
0
|
0
|
0
|
|
|
|
if (index($sv, $i) >= 0 && grep(/$i/i, @matched) == () ) { |
160
|
0
|
|
|
|
|
|
push @matched, uc($i); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
} |
163
|
0
|
0
|
|
|
|
|
if (@matched) { |
164
|
|
|
|
|
|
|
## override AUTH mech as specified. |
165
|
|
|
|
|
|
|
## if multiple mechs are specified, priority is still up to Authen::SASL module. |
166
|
0
|
|
|
|
|
|
${*$self}{'net_pop3e_capabilities'}->{'SASL'} = join " ", @matched; |
|
0
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
} |
169
|
0
|
|
|
|
|
|
$self->SUPER::auth($username, $password); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# Fix #121006 no timeout issue. |
173
|
|
|
|
|
|
|
sub getline { |
174
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
175
|
0
|
|
|
|
|
|
$self->Net::Cmd::getline(@_); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
1; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
__END__ |