line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ==== |
2
|
|
|
|
|
|
|
# SSL/STARTTLS extention for G.Barr's Net::SMTP. |
3
|
|
|
|
|
|
|
# plus, enable arbitrary SMTP auth mechanism selection. |
4
|
|
|
|
|
|
|
# IO::Socket::SSL (also Net::SSLeay openssl), |
5
|
|
|
|
|
|
|
# Authen::SASL, MIME::Base64 should be installed. |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
package Net::SMTPS; |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
54080
|
use vars qw ( $VERSION @ISA ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
56
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$VERSION = '0.07'; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
19
|
|
14
|
1
|
|
|
1
|
|
4
|
use base qw ( Net::SMTP ); |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
330
|
|
15
|
1
|
|
|
1
|
|
68344
|
use Net::Cmd; # import CMD_OK, CMD_MORE, ... |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
54
|
|
16
|
1
|
|
|
1
|
|
5
|
use Net::Config; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1006
|
|
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{smtp_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} || 'smtp(25)'; |
55
|
0
|
|
|
|
|
|
$_args{Proto} = 'tcp'; |
56
|
0
|
0
|
|
|
|
|
$_args{Timeout} = defined $_args{Timeout} ? $_args{Timeout} : 120; |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
foreach $h (@{ref($hosts) ? $hosts : [$hosts]}) { |
|
0
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
$_args{PeerAddr} = ($host = $h); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
#if ($_args{Debug}) { |
62
|
|
|
|
|
|
|
# foreach my $i (keys %_args) { |
63
|
|
|
|
|
|
|
# print STDERR "$type>>> arg $i: $_args{$i}\n"; |
64
|
|
|
|
|
|
|
# } |
65
|
|
|
|
|
|
|
#} |
66
|
|
|
|
|
|
|
|
67
|
0
|
0
|
|
|
|
|
$obj = $type->SUPER::new( |
68
|
|
|
|
|
|
|
%_args |
69
|
|
|
|
|
|
|
) |
70
|
|
|
|
|
|
|
and last; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
return undef |
74
|
0
|
0
|
|
|
|
|
unless defined $obj; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
$obj->autoflush(1); |
77
|
|
|
|
|
|
|
|
78
|
0
|
0
|
|
|
|
|
$obj->debug(exists $arg{Debug} ? $arg{Debug} : undef); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# common in SSL |
81
|
0
|
|
|
|
|
|
my %ssl_args; |
82
|
0
|
0
|
|
|
|
|
if ($ssl) { |
83
|
|
|
|
|
|
|
eval { |
84
|
0
|
|
|
|
|
|
require IO::Socket::SSL; |
85
|
0
|
0
|
|
|
|
|
} or do { |
86
|
0
|
|
|
|
|
|
$obj->set_status(500, ["Need working IO::Socket::SSL"]); |
87
|
0
|
|
|
|
|
|
$obj->close; |
88
|
0
|
|
|
|
|
|
return undef; |
89
|
|
|
|
|
|
|
}; |
90
|
0
|
|
|
|
|
|
%ssl_args = map { +"$_" => $arg{$_} } grep {/^SSL/} keys %arg; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
91
|
0
|
0
|
|
|
|
|
$IO::Socket::SSL::DEBUG = (exists $arg{Debug} ? $arg{Debug} : undef); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# OverSSL |
95
|
0
|
0
|
0
|
|
|
|
if (defined($ssl) && $ssl =~ /ssl/i) { |
96
|
|
|
|
|
|
|
$obj->ssl_start(\%ssl_args) |
97
|
0
|
0
|
|
|
|
|
or do { |
98
|
0
|
|
|
|
|
|
$obj->set_status(500, ["Cannot start SSL"]); |
99
|
0
|
|
|
|
|
|
$obj->close; |
100
|
0
|
|
|
|
|
|
return undef; |
101
|
|
|
|
|
|
|
}; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
0
|
0
|
|
|
|
|
unless ($obj->response() == CMD_OK) { |
105
|
0
|
|
|
|
|
|
$obj->close(); |
106
|
0
|
|
|
|
|
|
return undef; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
${*$obj}{'net_smtp_exact_addr'} = $arg{ExactAddresses}; |
|
0
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
${*$obj}{'net_smtp_host'} = $host; |
|
0
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
(${*$obj}{'net_smtp_banner'}) = $obj->message; |
|
0
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
(${*$obj}{'net_smtp_domain'}) = $obj->message =~ /\A\s*(\S+)/; |
|
0
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
0
|
0
|
0
|
|
|
|
unless ($obj->hello($arg{Hello} || "")) { |
116
|
0
|
|
|
|
|
|
$obj->close(); |
117
|
0
|
|
|
|
|
|
return undef; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# STARTTLS |
121
|
0
|
0
|
0
|
|
|
|
if (defined($ssl) && $ssl =~ /starttls/i && defined($obj->supports('STARTTLS')) ) { |
|
|
|
0
|
|
|
|
|
122
|
|
|
|
|
|
|
#123006 $obj->supports('STARTTLS') returns '' issue. |
123
|
|
|
|
|
|
|
(($obj->command('STARTTLS')->response() == CMD_OK) |
124
|
|
|
|
|
|
|
and $obj->ssl_start(\%ssl_args) |
125
|
|
|
|
|
|
|
and $obj->hello($arg{Hello} || "")) |
126
|
0
|
0
|
0
|
|
|
|
or do { |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
127
|
0
|
|
|
|
|
|
$obj->set_status(500, ["Cannot start SSL session"]); |
128
|
0
|
|
|
|
|
|
$obj->close(); |
129
|
0
|
|
|
|
|
|
return undef; |
130
|
|
|
|
|
|
|
}; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
$obj; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub ssl_start { |
137
|
0
|
|
|
0
|
0
|
|
my ($self, $args) = @_; |
138
|
0
|
|
|
|
|
|
my $type = ref($self); |
139
|
|
|
|
|
|
|
|
140
|
0
|
0
|
0
|
|
|
|
(unshift @ISA, 'IO::Socket::SSL' |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
141
|
|
|
|
|
|
|
and IO::Socket::SSL->start_SSL($self, %$args) |
142
|
|
|
|
|
|
|
and $self->isa('IO::Socket::SSL') |
143
|
|
|
|
|
|
|
and bless $self, $type # re-bless 'cause IO::Socket::SSL blesses himself. |
144
|
|
|
|
|
|
|
) or return undef; |
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
|
|
|
|
|
if (exists ${*$self}{'net_smtp_esmtp'}->{'AUTH'}) { |
|
0
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
my $sv = ${*$self}{'net_smtp_esmtp'}->{'AUTH'}; |
|
0
|
|
|
|
|
|
|
158
|
0
|
0
|
|
|
|
|
$self->debug_print(1, "AUTH-server offerred: ". $sv . "\n") if $self->debug; |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
foreach my $i (@cl_mech) { |
161
|
0
|
0
|
0
|
|
|
|
if (index($sv, $i) >= 0 && grep(/$i/i, @matched) == () ) { |
162
|
0
|
|
|
|
|
|
push @matched, uc($i); |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
} |
166
|
0
|
0
|
|
|
|
|
if (@matched) { |
167
|
|
|
|
|
|
|
## override AUTH mech as specified. |
168
|
|
|
|
|
|
|
## if multiple mechs are specified, priority is still up to Authen::SASL module. |
169
|
0
|
|
|
|
|
|
${*$self}{'net_smtp_esmtp'}->{'AUTH'} = join " ", @matched; |
|
0
|
|
|
|
|
|
|
170
|
0
|
0
|
|
|
|
|
$self->debug_print(1, "AUTH-negotiated: ". ${*$self}{'net_smtp_esmtp'}->{'AUTH'} . "\n") if $self->debug; |
|
0
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
0
|
|
|
|
|
|
$self->SUPER::auth($username, $password); |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Fix #121006 no timeout issue. |
177
|
|
|
|
|
|
|
sub getline { |
178
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
179
|
0
|
|
|
|
|
|
$self->Net::Cmd::getline(@_); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
1; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
__END__ |