Debian Codegraph @

plugins/tls

raw
1 #!perl -w
2
3 =head1 NAME
4
5 tls - plugin to support STARTTLS
6
7 =head1 SYNOPSIS
8
9 # in config/plugins
10
11 tls [B<cert_path priv_key_path ca_path>]
12
13 =over 4
14
15 =item B<cert_path>
16
17 Path to the server certificate file. Default: I<ssl/qpsmtpd-server.crt>
18
19 =item B<priv_key_path>
20
21 Path to the private key file. Default: I<ssl/qpsmtpd-server.key>
22
23 =item B<ca_path>
24
25 Path to the certificate authority file. Default: I<ssl/qpsmtpd-ca.crt>
26
27 =back
28
29 =head1 DESCRIPTION
30
31 This plugin implements basic TLS support. It can also be used to support
32 port 465 (SMTP over SSL), but only with qpsmtpd-forkserver. In this case,
33 be sure to load plugins/tls before any other connect plugins and start
34 qpsmtpd like this:
35
36 qpsmtpd-forkserver --port 25 --port 465
37
38 You can also specify multiple --listen-address options as well; see the help
39 for qpsmtpd-forkserver for more details.
40
41 If TLS is successfully negotiated then the C<tls_enabled> field in the
42 Connection notes is set. If you wish to make TLS mandatory you should check
43 that field and take appropriate action. Note that you can only do that from
44 MAIL FROM onwards.
45
46 Use the script C<plugins/tls_cert> to automatically generate a self-signed
47 certificate with the appropriate characteristics. Otherwise, you should
48 give absolute pathnames to the certificate, key, and the CA root cert
49 used to sign that certificate.
50
51 =head1 CIPHERS and COMPATIBILITY
52
53 By default, we use only the plugins that openssl considers to be
54 "high security". If you need to tweak the available ciphers for some
55 broken client (such as Versamail 3.x), have a look at the available
56 ciphers at L<http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS>,
57 and put a suitable string in config/tls_ciphers (e.g. "DEFAULT" or
58 "HIGH:MEDIUM")
59
60 =cut
61
62 use IO::Socket::SSL 0.98;
63
64 sub init {
65 my ($self, $qp, $cert, $key, $ca) = @_;
66 my $dir = -d 'ssl' ? 'ssl' : 'config/ssl';
67 $cert ||= "$dir/qpsmtpd-server.crt";
68 $key ||= "$dir/qpsmtpd-server.key";
69 $ca ||= "$dir/qpsmtpd-ca.crt";
70 unless (-f $cert && -f $key && -f $ca) {
71 $self->log(LOGERROR,
72 "Cannot locate cert/key! Run plugins/tls_cert to generate");
73 return;
74 }
75 $self->tls_cert($cert);
76 $self->tls_key($key);
77 $self->tls_ca($ca);
78 $self->tls_ciphers($self->qp->config('tls_ciphers') || 'HIGH');
79
80 $self->log(LOGDEBUG, "ciphers: " . $self->tls_ciphers);
81
82 local $^W; # this bit is very noisy...
83 my $ssl_ctx =
84 IO::Socket::SSL::SSL_Context->new(
85 SSL_use_cert => 1,
86 SSL_cert_file => $self->tls_cert,
87 SSL_key_file => $self->tls_key,
88 SSL_ca_file => $self->tls_ca,
89 SSL_cipher_list => $self->tls_ciphers,
90 SSL_server => 1
91 )
92 or die "Could not create SSL context: $!";
93
94 # now extract the password...
95
96 $self->ssl_context($ssl_ctx);
97
98 # Check for possible AUTH mechanisms
99 HOOK: foreach my $hook (keys %{$qp->hooks}) {
100 no strict 'refs';
101 if ($hook =~ m/^auth-?(.+)?$/) {
102 if (defined $1) {
103 my $hooksub = "hook_$hook";
104 $hooksub =~ s/\W/_/g;
105 *$hooksub = \&bad_ssl_hook;
106 }
107 else { # at least one polymorphous auth provider
108 *hook_auth = \&bad_ssl_hook;
109 }
110 }
111 }
112 }
113
114 sub hook_ehlo {
115 my ($self, $transaction) = @_;
116 return DECLINED unless $self->can_do_tls;
117 return DECLINED if $self->connection->notes('tls_enabled');
118 return DENY, "Command refused due to lack of security"
119 if $transaction->notes('ssl_failed');
120 my $cap = $transaction->notes('capabilities') || [];
121 push @$cap, 'STARTTLS';
122 $transaction->notes('tls_enabled', 1);
123 $transaction->notes('capabilities', $cap);
124 return DECLINED;
125 }
126
127 sub hook_unrecognized_command {
128 my ($self, $transaction, $cmd, @args) = @_;
129 return DECLINED unless lc $cmd eq 'starttls';
130 return DECLINED unless $transaction->notes('tls_enabled');
131 return DENY, "Syntax error (no parameters allowed)" if @args;
132
133 # OK, now we setup TLS
134 $self->qp->respond(220, "Go ahead with TLS");
135
136 unless (_convert_to_ssl($self)) {
137
138 # SSL setup failed. Now we must respond to every command with 5XX
139 warn("TLS failed: $@\n");
140 $transaction->notes('ssl_failed', 1);
141 return DENY, "TLS Negotiation Failed";
142 }
143
144 $self->log(LOGINFO, "TLS setup returning");
145 return DONE;
146 }
147
148 sub hook_connect {
149 my ($self, $transaction) = @_;
150
151 my $local_port = $self->qp->connection->local_port;
152 if ( ! defined $local_port || $local_port != 465 ) { # SMTPS
153 $self->log(LOGDEBUG, "skip, not SMTPS");
154 return DECLINED;
155 };
156
157 unless (_convert_to_ssl($self)) {
158 $self->log(LOGINFO, "fail, unable to establish SSL");
159 return (DENY_DISCONNECT, "Cannot establish SSL session");
160 }
161 $self->log(LOGINFO, "pass, connect via SMTPS");
162 return DECLINED;
163 }
164
165 sub hook_post_connection {
166 my ($self, $transaction) = @_;
167
168 my $tls_socket = $self->connection->notes('tls_socket');
169 if (defined $tls_socket && $self->connection->notes('tls_socket_is_duped'))
170 {
171 $tls_socket->close;
172 $self->connection->notes('tls_socket', undef);
173 $self->connection->notes('tls_socked_is_duped', 0);
174 }
175
176 return DECLINED;
177 }
178
179 sub _convert_to_ssl {
180 my ($self) = @_;
181
182 if ($self->qp->isa('Qpsmtpd::PollServer')) {
183 return _convert_to_ssl_async($self);
184 }
185
186 eval {
187 my $tlssocket =
188 IO::Socket::SSL->new_from_fd(
189 fileno(STDIN), '+>',
190 SSL_use_cert => 1,
191 SSL_cert_file => $self->tls_cert,
192 SSL_key_file => $self->tls_key,
193 SSL_ca_file => $self->tls_ca,
194 SSL_cipher_list => $self->tls_ciphers,
195 SSL_server => 1,
196 SSL_reuse_ctx => $self->ssl_context,
197 )
198 or die "Could not create SSL socket: $!";
199
200 # Clone connection object (without data received from client)
201 $self->qp->connection($self->connection->clone());
202 $self->qp->reset_transaction;
203 *STDIN = *STDOUT = $self->connection->notes('tls_socket', $tlssocket);
204 $self->connection->notes('tls_socket_is_duped', 1);
205 $self->connection->notes('tls_enabled', 1);
206 };
207 if ($@) {
208 return 0;
209 }
210 return 1;
211 }
212
213 sub _convert_to_ssl_async {
214 my ($self) = @_;
215 my $upgrader =
216 $self->connection->notes('tls_upgrader', UpgradeClientSSL->new($self));
217 $upgrader->upgrade_socket();
218 return 1;
219 }
220
221 sub can_do_tls {
222 my ($self) = @_;
223 $self->tls_cert && -r $self->tls_cert;
224 }
225
226 sub tls_cert {
227 my $self = shift;
228 @_ and $self->{_tls_cert} = shift;
229 $self->{_tls_cert};
230 }
231
232 sub tls_key {
233 my $self = shift;
234 @_ and $self->{_tls_key} = shift;
235 $self->{_tls_key};
236 }
237
238 sub tls_ca {
239 my $self = shift;
240 @_ and $self->{_tls_ca} = shift;
241 $self->{_tls_ca};
242 }
243
244 sub tls_ciphers {
245 my $self = shift;
246 @_ and $self->{_tls_ciphers} = shift;
247 $self->{_tls_ciphers};
248 }
249
250 sub ssl_context {
251 my $self = shift;
252 @_ and $self->{_ssl_ctx} = shift;
253 $self->{_ssl_ctx};
254 }
255
256 # Fulfill RFC 2487 secn 5.1
257 sub bad_ssl_hook {
258 my ($self, $transaction) = @_;
259 return DENY, "Command refused due to lack of security"
260 if $transaction->notes('ssl_failed');
261 return DECLINED;
262 }
263 *hook_helo = *hook_data = *hook_rcpt = *hook_mail = \&bad_ssl_hook;
264
265 package UpgradeClientSSL;
266
267 # borrowed heavily from Perlbal::SocketSSL
268
269 use strict;
270 use warnings;
271 no warnings qw(deprecated);
272
273 use IO::Socket::SSL 0.98;
274 use Errno qw( EAGAIN );
275
276 use fields qw( _stashed_qp _stashed_plugin _ssl_started );
277
278 sub new {
279 my UpgradeClientSSL $self = shift;
280 $self = fields::new($self) unless ref $self;
281 $self->{_stashed_plugin} = shift;
282 $self->{_stashed_qp} = $self->{_stashed_plugin}->qp;
283 return $self;
284 }
285
286 sub upgrade_socket {
287 my UpgradeClientSSL $self = shift;
288
289 unless ($self->{_ssl_started}) {
290 $self->{_stashed_qp}->clear_data();
291 IO::Socket::SSL->start_SSL(
292 $self->{_stashed_qp}->{sock},
293 {
294 SSL_use_cert => 1,
295 SSL_cert_file => $self->{_stashed_plugin}->tls_cert,
296 SSL_key_file => $self->{_stashed_plugin}->tls_key,
297 SSL_ca_file => $self->{_stashed_plugin}->tls_ca,
298 SSL_cipher_list => $self->{_stashed_plugin}->tls_ciphers,
299 SSL_startHandshake => 0,
300 SSL_server => 1,
301 SSL_reuse_ctx => $self->{_stashed_plugin}->ssl_context,
302 }
303 )
304 or die "Could not upgrade socket to SSL: $!";
305 $self->{_ssl_started} = 1;
306 }
307
308 $self->event_read($self->{_stashed_qp});
309 }
310
311 sub event_read {
312 my UpgradeClientSSL $self = shift;
313 my $qp = shift;
314
315 $qp->watch_read(0);
316
317 my $sock = $qp->{sock}->accept_SSL;
318
319 if (defined $sock) {
320 $qp->connection($qp->connection->clone);
321 $qp->reset_transaction;
322 $self->connection->notes('tls_socket', $sock);
323 $self->connection->notes('tls_enabled', 1);
324 $qp->watch_read(1);
325 return 1;
326 }
327
328 # nope, let's see if we can continue the process
329 if ($! == EAGAIN) {
330 $qp->set_reader_object($self);
331 if ($SSL_ERROR == SSL_WANT_READ) {
332 $qp->watch_read(1);
333 }
334 elsif ($SSL_ERROR == SSL_WANT_WRITE) {
335 $qp->watch_write(1);
336 }
337 else {
338 $qp->disconnect();
339 }
340 }
341 else {
342 $qp->disconnect();
343 }
344 }