This file is indexed.

/usr/share/qpsmtpd/plugins/tls is in qpsmtpd 0.94-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!perl -w

=head1 NAME

tls - plugin to support STARTTLS

=head1 SYNOPSIS

# in config/plugins

tls [B<cert_path priv_key_path ca_path>]

=over 4

=item B<cert_path>

Path to the server certificate file. Default: I<ssl/qpsmtpd-server.crt>

=item B<priv_key_path>

Path to the private key file. Default: I<ssl/qpsmtpd-server.key>

=item B<ca_path>

Path to the certificate authority file. Default: I<ssl/qpsmtpd-ca.crt>

=back

=head1 DESCRIPTION

This plugin implements basic TLS support.  It can also be used to support
port 465 (SMTP over SSL), but only with qpsmtpd-forkserver.  In this case,
be sure to load plugins/tls before any other connect plugins and start
qpsmtpd like this:

  qpsmtpd-forkserver --port 25 --port 465

You can also specify multiple --listen-address options as well; see the help
for qpsmtpd-forkserver for more details.

If TLS is successfully negotiated then the C<tls_enabled> field in the
Connection notes is set. If you wish to make TLS mandatory you should check
that field and take appropriate action. Note that you can only do that from
MAIL FROM onwards.

Use the script C<plugins/tls_cert> to automatically generate a self-signed
certificate with the appropriate characteristics.  Otherwise, you should
give absolute pathnames to the certificate, key, and the CA root cert
used to sign that certificate.

=head1 CIPHERS and COMPATIBILITY

By default, we use only the plugins that openssl considers to be
"high security". If you need to tweak the available ciphers for some
broken client (such as Versamail 3.x), have a look at the available
ciphers at L<http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS>,
and put a suitable string in config/tls_ciphers (e.g. "DEFAULT" or
"HIGH:MEDIUM")

=cut

use IO::Socket::SSL 0.98;

sub init {
    my ($self, $qp, $cert, $key, $ca) = @_;
    my $dir = -d 'ssl' ? 'ssl' : 'config/ssl';
    $cert ||= "$dir/qpsmtpd-server.crt";
    $key  ||= "$dir/qpsmtpd-server.key";
    $ca   ||= "$dir/qpsmtpd-ca.crt";
    unless (-f $cert && -f $key && -f $ca) {
        $self->log(LOGERROR,
                   "Cannot locate cert/key!  Run plugins/tls_cert to generate");
        return;
    }
    $self->tls_cert($cert);
    $self->tls_key($key);
    $self->tls_ca($ca);
    $self->tls_ciphers($self->qp->config('tls_ciphers') || 'HIGH');

    $self->log(LOGDEBUG, "ciphers: " . $self->tls_ciphers);

    local $^W;    # this bit is very noisy...
    my $ssl_ctx =
      IO::Socket::SSL::SSL_Context->new(
                                        SSL_use_cert    => 1,
                                        SSL_cert_file   => $self->tls_cert,
                                        SSL_key_file    => $self->tls_key,
                                        SSL_ca_file     => $self->tls_ca,
                                        SSL_cipher_list => $self->tls_ciphers,
                                        SSL_server      => 1
                                       )
      or die "Could not create SSL context: $!";

    # now extract the password...

    $self->ssl_context($ssl_ctx);

    # Check for possible AUTH mechanisms
  HOOK: foreach my $hook (keys %{$qp->hooks}) {
        no strict 'refs';
        if ($hook =~ m/^auth-?(.+)?$/) {
            if (defined $1) {
                my $hooksub = "hook_$hook";
                $hooksub =~ s/\W/_/g;
                *$hooksub = \&bad_ssl_hook;
            }
            else {    # at least one polymorphous auth provider
                *hook_auth = \&bad_ssl_hook;
            }
        }
    }
}

sub hook_ehlo {
    my ($self, $transaction) = @_;
    return DECLINED unless $self->can_do_tls;
    return DECLINED if $self->connection->notes('tls_enabled');
    return DENY, "Command refused due to lack of security"
      if $transaction->notes('ssl_failed');
    my $cap = $transaction->notes('capabilities') || [];
    push @$cap, 'STARTTLS';
    $transaction->notes('tls_enabled',  1);
    $transaction->notes('capabilities', $cap);
    return DECLINED;
}

sub hook_unrecognized_command {
    my ($self, $transaction, $cmd, @args) = @_;
    return DECLINED unless lc $cmd eq 'starttls';
    return DECLINED unless $transaction->notes('tls_enabled');
    return DENY, "Syntax error (no parameters allowed)" if @args;

    # OK, now we setup TLS
    $self->qp->respond(220, "Go ahead with TLS");

    unless (_convert_to_ssl($self)) {

        # SSL setup failed. Now we must respond to every command with 5XX
        warn("TLS failed: $@\n");
        $transaction->notes('ssl_failed', 1);
        return DENY, "TLS Negotiation Failed";
    }

    $self->log(LOGINFO, "TLS setup returning");
    return DONE;
}

sub hook_connect {
    my ($self, $transaction) = @_;

    my $local_port = $self->qp->connection->local_port;
    if ( ! defined $local_port || $local_port != 465 ) {  # SMTPS
        $self->log(LOGDEBUG, "skip, not SMTPS");
        return DECLINED;
    };

    unless (_convert_to_ssl($self)) {
        $self->log(LOGINFO, "fail, unable to establish SSL");
        return (DENY_DISCONNECT, "Cannot establish SSL session");
    }
    $self->log(LOGINFO, "pass, connect via SMTPS");
    return DECLINED;
}

sub hook_post_connection {
    my ($self, $transaction) = @_;

    my $tls_socket = $self->connection->notes('tls_socket');
    if (defined $tls_socket && $self->connection->notes('tls_socket_is_duped'))
    {
        $tls_socket->close;
        $self->connection->notes('tls_socket',          undef);
        $self->connection->notes('tls_socked_is_duped', 0);
    }

    return DECLINED;
}

sub _convert_to_ssl {
    my ($self) = @_;

    if ($self->qp->isa('Qpsmtpd::PollServer')) {
        return _convert_to_ssl_async($self);
    }

    eval {
        my $tlssocket =
          IO::Socket::SSL->new_from_fd(
                                       fileno(STDIN), '+>',
                                       SSL_use_cert    => 1,
                                       SSL_cert_file   => $self->tls_cert,
                                       SSL_key_file    => $self->tls_key,
                                       SSL_ca_file     => $self->tls_ca,
                                       SSL_cipher_list => $self->tls_ciphers,
                                       SSL_server      => 1,
                                       SSL_reuse_ctx   => $self->ssl_context,
                                      )
          or die "Could not create SSL socket: $!";

        # Clone connection object (without data received from client)
        $self->qp->connection($self->connection->clone());
        $self->qp->reset_transaction;
        *STDIN = *STDOUT = $self->connection->notes('tls_socket', $tlssocket);
        $self->connection->notes('tls_socket_is_duped', 1);
        $self->connection->notes('tls_enabled',         1);
    };
    if ($@) {
        return 0;
    }
    return 1;
}

sub _convert_to_ssl_async {
    my ($self) = @_;
    my $upgrader =
      $self->connection->notes('tls_upgrader', UpgradeClientSSL->new($self));
    $upgrader->upgrade_socket();
    return 1;
}

sub can_do_tls {
    my ($self) = @_;
    $self->tls_cert && -r $self->tls_cert;
}

sub tls_cert {
    my $self = shift;
    @_ and $self->{_tls_cert} = shift;
    $self->{_tls_cert};
}

sub tls_key {
    my $self = shift;
    @_ and $self->{_tls_key} = shift;
    $self->{_tls_key};
}

sub tls_ca {
    my $self = shift;
    @_ and $self->{_tls_ca} = shift;
    $self->{_tls_ca};
}

sub tls_ciphers {
    my $self = shift;
    @_ and $self->{_tls_ciphers} = shift;
    $self->{_tls_ciphers};
}

sub ssl_context {
    my $self = shift;
    @_ and $self->{_ssl_ctx} = shift;
    $self->{_ssl_ctx};
}

# Fulfill RFC 2487 secn 5.1
sub bad_ssl_hook {
    my ($self, $transaction) = @_;
    return DENY, "Command refused due to lack of security"
      if $transaction->notes('ssl_failed');
    return DECLINED;
}
*hook_helo = *hook_data = *hook_rcpt = *hook_mail = \&bad_ssl_hook;

package UpgradeClientSSL;

# borrowed heavily from Perlbal::SocketSSL

use strict;
use warnings;
no warnings qw(deprecated);

use IO::Socket::SSL 0.98;
use Errno qw( EAGAIN );

use fields qw( _stashed_qp _stashed_plugin _ssl_started );

sub new {
    my UpgradeClientSSL $self = shift;
    $self = fields::new($self) unless ref $self;
    $self->{_stashed_plugin} = shift;
    $self->{_stashed_qp}     = $self->{_stashed_plugin}->qp;
    return $self;
}

sub upgrade_socket {
    my UpgradeClientSSL $self = shift;

    unless ($self->{_ssl_started}) {
        $self->{_stashed_qp}->clear_data();
        IO::Socket::SSL->start_SSL(
                      $self->{_stashed_qp}->{sock},
                      {
                       SSL_use_cert    => 1,
                       SSL_cert_file   => $self->{_stashed_plugin}->tls_cert,
                       SSL_key_file    => $self->{_stashed_plugin}->tls_key,
                       SSL_ca_file     => $self->{_stashed_plugin}->tls_ca,
                       SSL_cipher_list => $self->{_stashed_plugin}->tls_ciphers,
                       SSL_startHandshake => 0,
                       SSL_server         => 1,
                       SSL_reuse_ctx => $self->{_stashed_plugin}->ssl_context,
                      }
          )
          or die "Could not upgrade socket to SSL: $!";
        $self->{_ssl_started} = 1;
    }

    $self->event_read($self->{_stashed_qp});
}

sub event_read {
    my UpgradeClientSSL $self = shift;
    my $qp = shift;

    $qp->watch_read(0);

    my $sock = $qp->{sock}->accept_SSL;

    if (defined $sock) {
        $qp->connection($qp->connection->clone);
        $qp->reset_transaction;
        $self->connection->notes('tls_socket',  $sock);
        $self->connection->notes('tls_enabled', 1);
        $qp->watch_read(1);
        return 1;
    }

    # nope, let's see if we can continue the process
    if ($! == EAGAIN) {
        $qp->set_reader_object($self);
        if ($SSL_ERROR == SSL_WANT_READ) {
            $qp->watch_read(1);
        }
        elsif ($SSL_ERROR == SSL_WANT_WRITE) {
            $qp->watch_write(1);
        }
        else {
            $qp->disconnect();
        }
    }
    else {
        $qp->disconnect();
    }
}