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
|
|
62598
|
use vars qw ( $VERSION @ISA ); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
84
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$VERSION = "0.03"; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
6
|
use base qw ( Net::SMTP ); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
3137
|
|
14
|
1
|
|
|
1
|
|
80374
|
use Net::Cmd; # import CMD_OK, CMD_MORE, ... |
|
1
|
|
|
|
|
9
|
|
|
1
|
|
|
|
|
65
|
|
15
|
1
|
|
|
1
|
|
6
|
use Net::Config; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
110
|
|
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
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1052
|
|
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
|
|
|
|
|
|
|
|
42
|
0
|
0
|
|
|
|
|
my $hosts = defined $host ? $host : $NetConfig{smtp_hosts}; |
43
|
0
|
|
|
|
|
|
my $obj; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# eliminate IO::Socket::SSL from @ISA for multiple call of new. |
46
|
0
|
|
|
|
|
|
@ISA = grep { !/IO::Socket::SSL/ } @ISA; |
|
0
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
my $h; |
49
|
0
|
0
|
|
|
|
|
foreach $h (@{ref($hosts) ? $hosts : [$hosts]}) { |
|
0
|
|
|
|
|
|
|
50
|
0
|
0
|
0
|
|
|
|
$obj = $type->SUPER::new( |
|
|
0
|
|
|
|
|
|
51
|
|
|
|
|
|
|
PeerAddr => ($host = $h), |
52
|
|
|
|
|
|
|
PeerPort => $arg{Port} || 'smtp(25)', |
53
|
|
|
|
|
|
|
LocalAddr => $arg{LocalAddr}, |
54
|
|
|
|
|
|
|
LocalPort => $arg{LocalPort}, |
55
|
|
|
|
|
|
|
Proto => 'tcp', |
56
|
|
|
|
|
|
|
Timeout => defined $arg{Timeout} |
57
|
|
|
|
|
|
|
? $arg{Timeout} |
58
|
|
|
|
|
|
|
: 120 |
59
|
|
|
|
|
|
|
) |
60
|
|
|
|
|
|
|
and last; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return undef |
64
|
0
|
0
|
|
|
|
|
unless defined $obj; |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
$obj->autoflush(1); |
67
|
|
|
|
|
|
|
|
68
|
0
|
0
|
|
|
|
|
$obj->debug(exists $arg{Debug} ? $arg{Debug} : undef); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# common in SSL |
71
|
0
|
|
|
|
|
|
my %ssl_args; |
72
|
0
|
0
|
|
|
|
|
if ($ssl) { |
73
|
|
|
|
|
|
|
eval { |
74
|
0
|
|
|
|
|
|
require IO::Socket::SSL; |
75
|
0
|
0
|
|
|
|
|
} or do { |
76
|
0
|
|
|
|
|
|
$obj->set_status(500, ["Need working IO::Socket::SSL"]); |
77
|
0
|
|
|
|
|
|
$obj->close; |
78
|
0
|
|
|
|
|
|
return undef; |
79
|
|
|
|
|
|
|
}; |
80
|
0
|
|
|
|
|
|
%ssl_args = map { +"$_" => $arg{$_} } grep {/^SSL/} keys %arg; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
|
$IO::Socket::SSL::DEBUG = (exists $arg{Debug} ? $arg{Debug} : undef); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# OverSSL |
85
|
0
|
0
|
0
|
|
|
|
if (defined($ssl) && $ssl =~ /ssl/i) { |
86
|
|
|
|
|
|
|
$obj->ssl_start(\%ssl_args) |
87
|
0
|
0
|
|
|
|
|
or do { |
88
|
0
|
|
|
|
|
|
$obj->set_status(500, ["Cannot start SSL"]); |
89
|
0
|
|
|
|
|
|
$obj->close; |
90
|
0
|
|
|
|
|
|
return undef; |
91
|
|
|
|
|
|
|
}; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
unless ($obj->response() == CMD_OK) { |
95
|
0
|
|
|
|
|
|
$obj->close(); |
96
|
0
|
|
|
|
|
|
return undef; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
${*$obj}{'net_smtp_exact_addr'} = $arg{ExactAddresses}; |
|
0
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
${*$obj}{'net_smtp_host'} = $host; |
|
0
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
(${*$obj}{'net_smtp_banner'}) = $obj->message; |
|
0
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
(${*$obj}{'net_smtp_domain'}) = $obj->message =~ /\A\s*(\S+)/; |
|
0
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
0
|
0
|
0
|
|
|
|
unless ($obj->hello($arg{Hello} || "")) { |
106
|
0
|
|
|
|
|
|
$obj->close(); |
107
|
0
|
|
|
|
|
|
return undef; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# STARTTLS |
111
|
0
|
0
|
0
|
|
|
|
if (defined($ssl) && $ssl =~ /starttls/i && $obj->supports('STARTTLS') ) { |
|
|
|
0
|
|
|
|
|
112
|
|
|
|
|
|
|
(($obj->command('STARTTLS')->response() == CMD_OK) |
113
|
|
|
|
|
|
|
and $obj->ssl_start(\%ssl_args) |
114
|
|
|
|
|
|
|
and $obj->hello($arg{Hello} || "")) |
115
|
0
|
0
|
0
|
|
|
|
or do { |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
116
|
0
|
|
|
|
|
|
$obj->set_status(500, ["Cannot start SSL session"]); |
117
|
0
|
|
|
|
|
|
$obj->close(); |
118
|
0
|
|
|
|
|
|
return undef; |
119
|
|
|
|
|
|
|
}; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
$obj; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub ssl_start { |
126
|
0
|
|
|
0
|
0
|
|
my ($self, $args) = @_; |
127
|
0
|
|
|
|
|
|
my $type = ref($self); |
128
|
|
|
|
|
|
|
|
129
|
0
|
0
|
0
|
|
|
|
(unshift @ISA, 'IO::Socket::SSL' |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
130
|
|
|
|
|
|
|
and IO::Socket::SSL->start_SSL($self, %$args) |
131
|
|
|
|
|
|
|
and $self->isa('IO::Socket::SSL') |
132
|
|
|
|
|
|
|
and bless $self, $type # re-bless 'cause IO::Socket::SSL blesses himself. |
133
|
|
|
|
|
|
|
) or return undef; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# Override to specify a certain auth mechanism. |
137
|
|
|
|
|
|
|
sub auth { |
138
|
0
|
|
|
0
|
1
|
|
my ($self, $username, $password, $mech) = @_; |
139
|
|
|
|
|
|
|
|
140
|
0
|
0
|
|
|
|
|
eval { |
141
|
0
|
|
|
|
|
|
require MIME::Base64; |
142
|
0
|
|
|
|
|
|
require Authen::SASL; |
143
|
|
|
|
|
|
|
} or $self->set_status(500, ["Need MIME::Base64 and Authen::SASL todo auth"]), return 0; |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
my $mechanisms = $self->supports('AUTH', 500, ["Command unknown: 'AUTH'"]); |
146
|
0
|
0
|
|
|
|
|
if ($mech) { |
147
|
0
|
|
|
|
|
|
$mechanisms = $mech; |
148
|
|
|
|
|
|
|
} |
149
|
0
|
0
|
|
|
|
|
return unless $mechanisms; |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
my $sasl; |
152
|
|
|
|
|
|
|
|
153
|
0
|
0
|
0
|
|
|
|
if (ref($username) and UNIVERSAL::isa($username, 'Authen::SASL')) { |
154
|
0
|
|
|
|
|
|
$sasl = $username; |
155
|
0
|
|
|
|
|
|
$sasl->mechanism($mechanisms); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
else { |
158
|
0
|
0
|
|
|
|
|
die "auth(username, password)" if not length $username; |
159
|
0
|
|
|
|
|
|
$sasl = Authen::SASL->new( |
160
|
|
|
|
|
|
|
mechanism => $mechanisms, |
161
|
|
|
|
|
|
|
callback => { |
162
|
|
|
|
|
|
|
user => $username, |
163
|
|
|
|
|
|
|
pass => $password, |
164
|
|
|
|
|
|
|
authname => $username, |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
); |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# We should probably allow the user to pass the host, but I don't |
170
|
|
|
|
|
|
|
# currently know and SASL mechanisms that are used by smtp that need it |
171
|
0
|
|
|
|
|
|
my $client = $sasl->client_new('smtp', ${*$self}{'net_smtp_host'}, 0); |
|
0
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
my $str = $client->client_start; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# We dont support sasl mechanisms that encrypt the socket traffic. |
175
|
|
|
|
|
|
|
# todo that we would really need to change the ISA hierarchy |
176
|
|
|
|
|
|
|
# so we dont inherit from IO::Socket, but instead hold it in an attribute |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
my @cmd = ("AUTH", $client->mechanism); |
179
|
0
|
|
|
|
|
|
my $code; |
180
|
|
|
|
|
|
|
|
181
|
0
|
0
|
0
|
|
|
|
push @cmd, MIME::Base64::encode_base64($str, '') |
182
|
|
|
|
|
|
|
if defined $str and length $str; |
183
|
|
|
|
|
|
|
|
184
|
0
|
|
|
|
|
|
while (($code = $self->command(@cmd)->response()) == CMD_MORE) { |
185
|
0
|
|
|
|
|
|
@cmd = ( |
186
|
|
|
|
|
|
|
MIME::Base64::encode_base64( |
187
|
|
|
|
|
|
|
$client->client_step(MIME::Base64::decode_base64(($self->message)[0])), '' |
188
|
|
|
|
|
|
|
) |
189
|
|
|
|
|
|
|
); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
0
|
|
|
|
|
|
$code == CMD_OK; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
1; |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
__END__ |