1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
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';
20 my $cert = 'certs/server-cert.pem';
21 my $key = 'certs/server-key.pem';
22 unless (-r $key && -r $cert) {
24 "certs/ missing for $0, run $^X ./create-certs.perl in certs/";
26 use_ok 'PublicInbox::TLS';
27 use_ok 'IO::Socket::SSL';
28 use PublicInbox::InboxWritable;
29 require PublicInbox::SearchIdx;
30 my $version = 1; # v2 needs newer git
31 require_git('2.6') if $version >= 2;
32 my ($tmpdir, $for_destroy) = tmpdir();
33 my $err = "$tmpdir/stderr.log";
34 my $out = "$tmpdir/stdout.log";
35 my $inboxdir = "$tmpdir";
36 my $pi_config = "$tmpdir/pi_config";
37 my $group = 'test-imapd-tls';
38 my $addr = $group . '@example.com';
39 my $starttls = tcp_server();
40 my $imaps = tcp_server();
41 my $ibx = PublicInbox::Inbox->new({
42 inboxdir => $inboxdir,
45 -primary_address => $addr,
46 indexlevel => 'basic',
48 $ibx = PublicInbox::InboxWritable->new($ibx, {nproc=>1});
51 open my $fh, '>', $pi_config or BAIL_OUT "open: $!";
53 [publicinbox "imapd-tls"]
60 close $fh or BAIL_OUT "close: $!\n";
64 my $im = $ibx->importer(0);
65 ok($im->add(eml_load('t/data/0001.patch')), 'message added');
68 my $s = PublicInbox::SearchIdx->new($ibx, 1);
73 my $imaps_addr = $imaps->sockhost . ':' . $imaps->sockport;
74 my $starttls_addr = $starttls->sockhost . ':' . $starttls->sockport;
75 my $env = { PI_CONFIG => $pi_config };
79 [ "--cert=$cert", "--key=$key",
80 "-limaps://$imaps_addr",
81 "-limap://$starttls_addr" ],
84 open my $fh, '>', $_ or BAIL_OUT "truncate: $!";
86 my $cmd = [ '-imapd', '-W0', @$args, "--stdout=$out", "--stderr=$err" ];
87 $td = start_script($cmd, $env, { 3 => $starttls, 4 => $imaps });
89 SSL_hostname => 'server.local',
90 SSL_verifycn_name => 'server.local',
91 SSL_verify_mode => SSL_VERIFY_PEER(),
92 SSL_ca_file => 'certs/test-ca.pem',
94 # start negotiating a slow TLS connection
95 my $slow = tcp_connect($imaps, Blocking => 0);
96 $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
97 my $slow_done = $slow->connect_SSL;
100 diag('W: connect_SSL early OK, slow client test invalid');
101 use PublicInbox::Syscall qw(EPOLLIN EPOLLOUT);
102 @poll = (fileno($slow), EPOLLIN | EPOLLOUT);
104 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
106 # we should call connect_SSL much later...
107 my %imaps_opt = (User => 'a', Password => 'b',
108 Server => $imaps->sockhost,
109 Port => $imaps->sockport);
111 my $c = $imap_client->new(%imaps_opt, Ssl => [ %o ]);
112 ok($c && $c->IsAuthenticated, 'authenticated');
113 ok($c->select($group), 'SELECT works');
114 ok(!(scalar $c->has_capability('STARTTLS')),
115 'starttls not advertised with IMAPS');
116 ok(!$c->starttls, "starttls fails");
117 ok($c->has_capability('COMPRESS') ||
118 $c->has_capability('COMPRESS=DEFLATE'),
119 'compress advertised');
120 ok($c->compress, 'compression enabled with IMAPS');
121 ok(!$c->starttls, 'starttls still fails');
122 ok($c->noop, 'noop succeeds');
123 ok($c->logout, 'logout succeeds');
126 my %imap_opt = (Server => $starttls->sockhost,
127 Port => $starttls->sockport);
128 $c = $imap_client->new(%imap_opt);
129 ok(scalar $c->has_capability('STARTTLS'),
130 'starttls advertised');
131 ok($c->Starttls([ %o ]), 'set starttls options');
132 ok($c->starttls, '->starttls works');
133 ok(!(scalar($c->has_capability('STARTTLS'))),
134 'starttls not advertised');
135 ok(!$c->starttls, '->starttls again fails');
136 ok(!(scalar($c->has_capability('STARTTLS'))),
137 'starttls still not advertised');
138 ok($c->examine($group), 'EXAMINE works');
139 ok($c->noop, 'NOOP works');
140 ok($c->compress, 'compression enabled with IMAPS');
141 ok($c->noop, 'NOOP works after compress');
142 ok($c->logout, 'logout succeeds after compress');
144 # STARTTLS with bad hostname
145 $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.invalid';
146 $c = $imap_client->new(%imap_opt);
147 ok(scalar $c->has_capability('STARTTLS'), 'starttls advertised');
148 ok($c->Starttls([ %o ]), 'set starttls options');
149 ok(!$c->starttls, '->starttls fails with bad hostname');
151 $c = $imap_client->new(%imap_opt);
152 ok($c->noop, 'NOOP still works from plain IMAP');
154 # IMAPS with bad hostname
155 $c = $imap_client->new(%imaps_opt, Ssl => [ %o ]);
156 is($c, undef, 'IMAPS fails with bad hostname');
158 # make hostname valid
159 $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
160 $c = $imap_client->new(%imaps_opt, Ssl => [ %o ]);
161 ok($c, 'IMAPS succeeds again with valid hostname');
163 # slow TLS connection did not block the other fast clients while
164 # connecting, finish it off:
166 IO::Poll::_poll(-1, @poll);
167 $slow_done = $slow->connect_SSL and last;
168 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
171 ok(sysread($slow, my $greet, 4096) > 0, 'slow got a greeting');
172 like($greet, qr/\A\* OK \[CAPABILITY IMAP4rev1 /, 'got greeting');
173 is(syswrite($slow, "1 LOGOUT\r\n"), 10, 'slow wrote LOGOUT');
174 ok(sysread($slow, my $end, 4096) > 0, 'got end');
175 is(sysread($slow, my $eof, 4096), 0, 'got EOF');
178 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
179 my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
180 defined(my $x = getsockopt($imaps, IPPROTO_TCP, $var)) or die;
181 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on IMAPS');
182 defined($x = getsockopt($starttls, IPPROTO_TCP, $var)) or die;
183 is(unpack('i', $x), 0, 'TCP_DEFER_ACCEPT is 0 on plain IMAP');
186 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
187 if (system('kldstat -m accf_data >/dev/null')) {
188 skip 'accf_data not loaded? kldload accf_data', 2;
190 require PublicInbox::Daemon;
191 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
192 my $x = getsockopt($imaps, SOL_SOCKET, $var);
193 like($x, qr/\Adataready\0+\z/, 'got dataready accf for IMAPS');
194 $x = getsockopt($starttls, IPPROTO_TCP, $var);
195 is($x, undef, 'no BSD accept filter for plain IMAP');
201 is($?, 0, 'no error in exited process');
202 open my $fh, '<', $err or BAIL_OUT "open $err failed: $!";
203 my $eout = do { local $/; <$fh> };
204 unlike($eout, qr/wide/i, 'no Wide character warnings');