]> Sergey Matveev's repositories - public-inbox.git/blob - t/imapd-tls.t
t/imapd-tls: switch to create_inbox
[public-inbox.git] / t / imapd-tls.t
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use Socket qw(IPPROTO_TCP SOL_SOCKET);
7 use PublicInbox::TestCommon;
8 # IO::Poll is part of the standard library, but distros may split it off...
9 require_mods(qw(DBD::SQLite IO::Socket::SSL Mail::IMAPClient IO::Poll
10         Email::Address::XS||Mail::Address Parse::RecDescent));
11 my $imap_client = 'Mail::IMAPClient';
12 $imap_client->can('starttls') or
13         plan skip_all => 'Mail::IMAPClient does not support TLS';
14 my $can_compress = $imap_client->can('compress');
15 if ($can_compress) { # hope this gets fixed upstream, soon
16         require PublicInbox::IMAPClient;
17         $imap_client = 'PublicInbox::IMAPClient';
18 }
19
20 my $cert = 'certs/server-cert.pem';
21 my $key = 'certs/server-key.pem';
22 unless (-r $key && -r $cert) {
23         plan skip_all =>
24                 "certs/ missing for $0, run $^X ./create-certs.perl in certs/";
25 }
26 use_ok 'PublicInbox::TLS';
27 use_ok 'IO::Socket::SSL';
28 my $version = 1; # v2 needs newer git
29 require_git('2.6') if $version >= 2;
30 my ($tmpdir, $for_destroy) = tmpdir();
31 my $err = "$tmpdir/stderr.log";
32 my $out = "$tmpdir/stdout.log";
33 my $pi_config;
34 my $group = 'test-imapd-tls';
35 my $addr = $group . '@example.com';
36 my $starttls = tcp_server();
37 my $imaps = tcp_server();
38 my $ibx = create_inbox 'imapd-tls', version => $version,
39                         -primary_address => $addr, indexlevel => 'basic', sub {
40         my ($im, $ibx) = @_;
41         $im->add(eml_load('t/data/0001.patch')) or BAIL_OUT '->add';
42         $pi_config = "$ibx->{inboxdir}/pi_config";
43         open my $fh, '>', $pi_config or BAIL_OUT "open: $!";
44         print $fh <<EOF or BAIL_OUT "print: $!";
45 [publicinbox "imapd-tls"]
46         inboxdir = $ibx->{inboxdir}
47         address = $addr
48         indexlevel = basic
49         newsgroup = $group
50 EOF
51         close $fh or BAIL_OUT "close: $!\n";
52 };
53 $pi_config //= "$ibx->{inboxdir}/pi_config";
54
55 my $imaps_addr = tcp_host_port($imaps);
56 my $starttls_addr = tcp_host_port($starttls);
57 my $env = { PI_CONFIG => $pi_config };
58 my $td;
59
60 for my $args (
61         [ "--cert=$cert", "--key=$key",
62                 "-limaps://$imaps_addr",
63                 "-limap://$starttls_addr" ],
64 ) {
65         for ($out, $err) {
66                 open my $fh, '>', $_ or BAIL_OUT "truncate: $!";
67         }
68         my $cmd = [ '-imapd', '-W0', @$args, "--stdout=$out", "--stderr=$err" ];
69         $td = start_script($cmd, $env, { 3 => $starttls, 4 => $imaps });
70         my %o = (
71                 SSL_hostname => 'server.local',
72                 SSL_verifycn_name => 'server.local',
73                 SSL_verify_mode => SSL_VERIFY_PEER(),
74                 SSL_ca_file => 'certs/test-ca.pem',
75         );
76         # start negotiating a slow TLS connection
77         my $slow = tcp_connect($imaps, Blocking => 0);
78         $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
79         my $slow_done = $slow->connect_SSL;
80         my @poll;
81         if ($slow_done) {
82                 diag('W: connect_SSL early OK, slow client test invalid');
83                 use PublicInbox::Syscall qw(EPOLLIN EPOLLOUT);
84                 @poll = (fileno($slow), EPOLLIN | EPOLLOUT);
85         } else {
86                 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
87         }
88         # we should call connect_SSL much later...
89         my %imaps_opt = (User => 'a', Password => 'b',
90                         Server => $imaps->sockhost,
91                         Port => $imaps->sockport);
92         # IMAPS
93         my $c = $imap_client->new(%imaps_opt, Ssl => [ %o ]);
94         ok($c && $c->IsAuthenticated, 'authenticated');
95         ok($c->select($group), 'SELECT works');
96         ok(!(scalar $c->has_capability('STARTTLS')),
97                 'starttls not advertised with IMAPS');
98         ok(!$c->starttls, "starttls fails");
99         ok($c->has_capability('COMPRESS') ||
100                 $c->has_capability('COMPRESS=DEFLATE'),
101                 'compress advertised');
102         ok($c->compress, 'compression enabled with IMAPS');
103         ok(!$c->starttls, 'starttls still fails');
104         ok($c->noop, 'noop succeeds');
105         ok($c->logout, 'logout succeeds');
106
107         # STARTTLS
108         my %imap_opt = (Server => $starttls->sockhost,
109                         Port => $starttls->sockport);
110         $c = $imap_client->new(%imap_opt);
111         ok(scalar $c->has_capability('STARTTLS'),
112                 'starttls advertised');
113         ok($c->Starttls([ %o ]), 'set starttls options');
114         ok($c->starttls, '->starttls works');
115         ok(!(scalar($c->has_capability('STARTTLS'))),
116                 'starttls not advertised');
117         ok(!$c->starttls, '->starttls again fails');
118         ok(!(scalar($c->has_capability('STARTTLS'))),
119                 'starttls still not advertised');
120         ok($c->examine($group), 'EXAMINE works');
121         ok($c->noop, 'NOOP works');
122         ok($c->compress, 'compression enabled with IMAPS');
123         ok($c->noop, 'NOOP works after compress');
124         ok($c->logout, 'logout succeeds after compress');
125
126         # STARTTLS with bad hostname
127         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.invalid';
128         $c = $imap_client->new(%imap_opt);
129         ok(scalar $c->has_capability('STARTTLS'), 'starttls advertised');
130         ok($c->Starttls([ %o ]), 'set starttls options');
131         ok(!$c->starttls, '->starttls fails with bad hostname');
132
133         $c = $imap_client->new(%imap_opt);
134         ok($c->noop, 'NOOP still works from plain IMAP');
135
136         # IMAPS with bad hostname
137         $c = $imap_client->new(%imaps_opt, Ssl => [ %o ]);
138         is($c, undef, 'IMAPS fails with bad hostname');
139
140         # make hostname valid
141         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
142         $c = $imap_client->new(%imaps_opt, Ssl => [ %o ]);
143         ok($c, 'IMAPS succeeds again with valid hostname');
144
145         # slow TLS connection did not block the other fast clients while
146         # connecting, finish it off:
147         until ($slow_done) {
148                 IO::Poll::_poll(-1, @poll);
149                 $slow_done = $slow->connect_SSL and last;
150                 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
151         }
152         $slow->blocking(1);
153         ok(sysread($slow, my $greet, 4096) > 0, 'slow got a greeting');
154         like($greet, qr/\A\* OK \[CAPABILITY IMAP4rev1 /, 'got greeting');
155         is(syswrite($slow, "1 LOGOUT\r\n"), 10, 'slow wrote LOGOUT');
156         ok(sysread($slow, my $end, 4096) > 0, 'got end');
157         is(sysread($slow, my $eof, 4096), 0, 'got EOF');
158
159         SKIP: {
160                 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
161                 my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
162                 defined(my $x = getsockopt($imaps, IPPROTO_TCP, $var)) or die;
163                 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on IMAPS');
164                 defined($x = getsockopt($starttls, IPPROTO_TCP, $var)) or die;
165                 is(unpack('i', $x), 0, 'TCP_DEFER_ACCEPT is 0 on plain IMAP');
166         };
167         SKIP: {
168                 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
169                 if (system('kldstat -m accf_data >/dev/null')) {
170                         skip 'accf_data not loaded? kldload accf_data', 2;
171                 }
172                 require PublicInbox::Daemon;
173                 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
174                 my $x = getsockopt($imaps, SOL_SOCKET, $var);
175                 like($x, qr/\Adataready\0+\z/, 'got dataready accf for IMAPS');
176                 $x = getsockopt($starttls, IPPROTO_TCP, $var);
177                 is($x, undef, 'no BSD accept filter for plain IMAP');
178         };
179
180         $c = undef;
181         $td->kill;
182         $td->join;
183         is($?, 0, 'no error in exited process');
184         open my $fh, '<', $err or BAIL_OUT "open $err failed: $!";
185         my $eout = do { local $/; <$fh> };
186         unlike($eout, qr/wide/i, 'no Wide character warnings');
187 }
188
189 done_testing;