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