/
IMAP.pm
294 lines (218 loc) · 6.69 KB
/
IMAP.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package Net::OpenVPN::Auth::IMAP;
@ISA = qw(Net::OpenVPN::Auth);
use strict;
use warnings;
use IO::Socket;
use Log::Log4perl;
=head1 NAME IMAP
Dovecot IMAP server backend authentication module
=cut
=head1 OBJECT CONSTRUCTOR
=head2 Inherited parameters
B<required> (boolean, 1) successfull authentication result is required for authentication chain to return successful authentication
B<sufficient> (boolean, 0) successful authentication result is sufficient for entire authentication chain
=over
=head2 Module specific parameters
B<host> (string, "localhost") IMAP server hostname
B<port> (integer, 143) IMAP server listening port
B<tls> (boolean, 0) Enable TLS/SSLv3 transport security. This option requires IO::Socket::SSL module
B<tls_version> (string, "tlsv1") Transport protocol type. Valid values: B<tlsv1, sslv2, sslv3>
B<tls_ciphers> (string, "HIGH") See IO::Socket::SSL
B<tls_cert_file> (string, undef) Certificate file
B<tls_key_file> (string, undef) Certificate key file
B<tls_ca_file> (string, undef) CA certificate file
B<tls_ca_path> (string, undef) CA authority directory
B<tls_verify> (hex integer, 0x00) See perldoc IO::Socket::SSL
B<timeout> (integer, 2) Timeout for socket operations
=cut
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = $class->SUPER::new(@_);
##################################################
# PUBLIC VARS #
##################################################
##################################################
# PRIVATE VARS #
##################################################
$self->{_name} = "IMAP";
$self->{_log} = Log::Log4perl->get_logger(__PACKAGE__);
bless($self, $class);
$self->clearParams();
$self->_init();
$self->setParams(@_);
return $self;
}
sub clearParams {
my $self = shift;
$self->SUPER::clearParams();
$self->{host} = "localhost";
$self->{port} = 143;
$self->{timeout} = 2;
$self->{tls} = 0;
$self->{tls_version} = "tlsv1";
$self->{tls_ciphers} = "HIGH";
$self->{tls_cert_file} = undef;
$self->{tls_key_file} = undef;
$self->{tls_ca_file} = undef;
$self->{tls_ca_path} = undef;
$self->{tls_verify} = 0x00;
$self->{_imap} = undef;
$self->{_has_ssl} = 0;
return 1;
}
sub authenticate {
my ($self, $struct) = @_;
return 0 unless ($self->validateParamsStruct($struct));
# connect to server
return 0 unless ($self->_connect());
# send username & password
$self->{_log}->debug("Sending username '" . $struct->{username} . "' with password '" . $struct->{password} . "'");
my $r = $self->_imapCmd("LOGIN " . $struct->{username} . " " . $struct->{password});
# close connection
$self->_disconnect();
return $r;
}
sub _init {
my ($self) = @_;
# check for IO::Socket::SSL availability
eval {
require IO::Socket::SSL;
};
$self->{_has_ssl} = ($@) ? 0 : 1;
return 1;
}
sub _connect {
my ($self) = @_;
my $sock = undef;
if ($self->{tls} && ! $self->{_has_ssl}) {
$self->{error} = "Unable to use SSL/TLS secured session: Module IO::Socket::SSL is not available.";
$self->{_log}->error($self->{error});
return 0;
}
if ($self->{tls} && lc(substr($self->{tls_version}, 0, 3)) eq 'ssl') {
$self->{_log}->debug("Connecting to host: " . $self->{host} . ":" . $self->{port} . " using SSL.");
$sock = IO::Socket::SSL->new(
PeerAddr => $self->{host},
PeerPort => $self->{port},
Proto => "tcp",
ReuseAddr => 1,
Timeout => $self->{timeout},
$self->_getTlsHash()
);
} else {
$self->{_log}->debug("Connecting to host: " . $self->{host} . ":" . $self->{port} . ".");
$sock = IO::Socket::INET->new(
PeerAddr => $self->{host},
PeerPort => $self->{port},
Proto => "tcp",
ReuseAddr => 1,
Timeout => $self->{timeout}
);
}
unless (defined $sock) {
$self->{error} = "Unable to connect to server '" . $self->{host} . ":" . $self->{port} . "': $@";
$self->{_log}->error($self->{error});
return 0;
}
# read server greeting
return 0 unless ($self->_readResponse($sock));
# TLS anyone? Upgrade normal socket to SSL-one :)
if ($self->{tls} && lc($self->{tls_version}) eq 'tlsv1') {
# send starttls command
return 0 unless ($self->_imapCmd("STARTTLS", $sock));
# start TLS session
$self->{_log}->debug("Starting TLS session on unsecured socket.");
my $r = IO::Socket::SSL->start_SSL(
$sock,
$self->_getTlsHash()
);
unless ($r) {
$self->{error} = "Unable to start TLS secured session: " . $sock->errstr();
$self->{_log}->error($self->{error});
return 0;
}
$self->{_log}->debug("Successfully started TLS secured session.");
}
$self->{_log}->debug("Successfully connected.");
$self->{_imap} = $sock;
return 1;
}
sub _disconnect {
my ($self) = @_;
if ($self->{_imap}->connected()) {
$self->_imapCmd("LOGOUT");
$self->{_imap} = undef;
}
return 1;
}
sub _imapCmd {
my ($self, $cmd, $sock) = @_;
$sock = $self->{_imap} unless (defined $sock);
$self->{_cmd_idx}++;
unless (defined $sock && $sock->connected()) {
$self->{error} = "Invalid socket.";
$self->{_log}->error($self->{error});
return 0;
}
# send command
my $str = $self->{_cmd_idx} . " " . $cmd;
$self->{_log}->debug("Sending IMAP command: $str");
print $sock $str, "\r\n";
# ... & read response
return $self->_readResponse($sock);
}
sub _readResponse {
my ($self, $sock) = @_;
$sock = $self->{_imap} unless (defined $sock);
unless (defined $sock && $sock->connected()) {
$self->{error} = "Invalid socket.";
$self->{_log}->error($self->{error});
return undef;
}
while (1) {
my $line = $sock->getline();
unless (defined $line && length($line) > 0) {
$self->{error} = "Unable to read response from IMAP server: $!";
$self->{_log}->error($self->{error});
return 0;
}
$line =~ s/\s+$//g;
$self->{_log}->debug("IMAP server response: '$line'");
# mostly for welcome message
return 1 if ($line =~ m/^\*\s+OK/i);
# ignore lines starting with *
next if ($line =~ m/^\*/);
if ($line =~ m/^(\d+)\s+(OK|NO|BAD)\s+(.+)/i) {
my $code = uc($2);
return 1 if ($code eq 'OK');
$self->{error} = "Negative response received from IMAP server: $3";
$self->{_log}->error($self->{error});
return 0;
}
}
}
sub _getTlsHash {
my ($self) = @_;
my %h = (
SSL_version => $self->{tls_version},
SSL_chiper_list => $self->{tls_ciphers},
SSL_use_cert => (defined $self->{tls_cert_file}) ? 1 : 0,
SSL_cert_file => $self->{tls_cert_file},
SSL_key_file => $self->{tls_key_file},
SSL_ca_file => $self->{tls_ca_file},
SSL_ca_path => $self->{tls_ca_path},
SSL_verify_mode => $self->{tls_verify},
);
return %h;
}
=head1 AUTHOR
Brane F. Gracnar
=cut
=head1 SEE ALSO
L<Net::OpenVPN::Auth>
L<Net::OpenVPN::AuthChain>
L<IO::Socket::SSL>
=cut
# This line is mandatory...
1;