lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / nntpd-tls.t
bloba16cc015efc1d6e15aee75cfd19f8e379e34b450
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12;
5 use PublicInbox::TestCommon;
6 use Socket qw(SOCK_STREAM IPPROTO_TCP SOL_SOCKET);
7 # IO::Poll and Net::NNTP are part of the standard library, but
8 # distros may split them off...
9 require_mods(qw(DBD::SQLite IO::Socket::SSL Net::NNTP IO::Poll));
10 Net::NNTP->can('starttls') or
11         plan skip_all => 'Net::NNTP does not support TLS';
13 my $cert = 'certs/server-cert.pem';
14 my $key = 'certs/server-key.pem';
15 unless (-r $key && -r $cert) {
16         plan skip_all =>
17                 "certs/ missing for $0, run $^X ./create-certs.perl in certs/";
20 use_ok 'PublicInbox::TLS';
21 use_ok 'IO::Socket::SSL';
22 our $need_zlib;
23 eval { require Compress::Raw::Zlib } or
24         $need_zlib = 'Compress::Raw::Zlib missing';
25 my $version = 2; # v2 needs newer git
26 require_git('2.6') if $version >= 2;
27 my ($tmpdir, $for_destroy) = tmpdir();
28 my $err = "$tmpdir/stderr.log";
29 my $out = "$tmpdir/stdout.log";
30 my $group = 'test-nntpd-tls';
31 my $addr = $group . '@example.com';
32 my $starttls = tcp_server();
33 my $nntps = tcp_server();
34 my $pi_config;
35 my $ibx = create_inbox "v$version", version => $version, indexlevel => 'basic',
36                         sub {
37         my ($im, $ibx) = @_;
38         $pi_config = "$ibx->{inboxdir}/pi_config";
39         open my $fh, '>', $pi_config or BAIL_OUT "open: $!";
40         print $fh <<EOF or BAIL_OUT;
41 [publicinbox "nntpd-tls"]
42         inboxdir = $ibx->{inboxdir}
43         address = $addr
44         indexlevel = basic
45         newsgroup = $group
46 EOF
47         close $fh or BAIL_OUT "close: $!";
48         $im->add(eml_load 't/data/0001.patch') or BAIL_OUT;
50 $pi_config //= "$ibx->{inboxdir}/pi_config";
51 undef $ibx;
52 my $nntps_addr = tcp_host_port($nntps);
53 my $starttls_addr = tcp_host_port($starttls);
54 my $env = { PI_CONFIG => $pi_config };
55 my $td;
57 for my $args (
58         [ "--cert=$cert", "--key=$key",
59                 "-lnntps://$nntps_addr",
60                 "-lnntp://$starttls_addr" ],
61 ) {
62         for ($out, $err) {
63                 open my $fh, '>', $_ or die "truncate: $!";
64         }
65         my $cmd = [ '-nntpd', '-W0', @$args, "--stdout=$out", "--stderr=$err" ];
66         $td = start_script($cmd, $env, { 3 => $starttls, 4 => $nntps });
67         my %o = (
68                 SSL_hostname => 'server.local',
69                 SSL_verifycn_name => 'server.local',
70                 SSL_verify_mode => SSL_VERIFY_PEER(),
71                 SSL_ca_file => 'certs/test-ca.pem',
72         );
73         my $expect = { $group => [qw(1 1 n)] };
75         # start negotiating a slow TLS connection
76         my $slow = tcp_connect($nntps, Blocking => 0);
77         $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
78         my $slow_done = $slow->connect_SSL;
79         my @poll;
80         if ($slow_done) {
81                 diag('W: connect_SSL early OK, slow client test invalid');
82                 use PublicInbox::Syscall qw(EPOLLIN EPOLLOUT);
83                 @poll = (fileno($slow), EPOLLIN | EPOLLOUT);
84         } else {
85                 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
86         }
87         # we should call connect_SSL much later...
89         # NNTPS
90         my $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
91         my $list = $c->list;
92         is_deeply($list, $expect, 'NNTPS LIST works');
93         unlike(get_capa($c), qr/\bSTARTTLS\r\n/,
94                 'STARTTLS not advertised for NNTPS');
95         is($c->command('QUIT')->response(), Net::Cmd::CMD_OK(), 'QUIT works');
96         is(0, sysread($c, my $buf, 1), 'got EOF after QUIT');
98         # STARTTLS
99         $c = Net::NNTP->new($starttls_addr, %o);
100         $list = $c->list;
101         is_deeply($list, $expect, 'plain LIST works');
102         ok($c->starttls, 'STARTTLS succeeds');
103         is($c->code, 382, 'got 382 for STARTTLS');
104         $list = $c->list;
105         is_deeply($list, $expect, 'LIST works after STARTTLS');
106         unlike(get_capa($c), qr/\bSTARTTLS\r\n/,
107                 'STARTTLS not advertised after STARTTLS');
109         # Net::NNTP won't let us do dumb things, but we need to test
110         # dumb things, so use Net::Cmd directly:
111         my $n = $c->command('STARTTLS')->response();
112         is($n, Net::Cmd::CMD_ERROR(), 'error attempting STARTTLS again');
113         is($c->code, 502, '502 according to RFC 4642 sec#2.2.1');
115         # STARTTLS with bad hostname
116         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.invalid';
117         $c = Net::NNTP->new($starttls_addr, %o);
118         like(get_capa($c), qr/\bSTARTTLS\r\n/, 'STARTTLS advertised');
119         $list = $c->list;
120         is_deeply($list, $expect, 'plain LIST works again');
121         ok(!$c->starttls, 'STARTTLS fails with bad hostname');
122         $c = Net::NNTP->new($starttls_addr, %o);
123         $list = $c->list;
124         is_deeply($list, $expect, 'not broken after bad negotiation');
126         # NNTPS with bad hostname
127         $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
128         is($c, undef, 'NNTPS fails with bad hostname');
129         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
130         $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
131         ok($c, 'NNTPS succeeds again with valid hostname');
133         # slow TLS connection did not block the other fast clients while
134         # connecting, finish it off:
135         until ($slow_done) {
136                 IO::Poll::_poll(-1, @poll);
137                 $slow_done = $slow->connect_SSL and last;
138                 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
139         }
140         $slow->blocking(1);
141         ok(sysread($slow, my $greet, 4096) > 0, 'slow got greeting');
142         like($greet, qr/\A201 /, 'got expected greeting');
143         is(syswrite($slow, "QUIT\r\n"), 6, 'slow wrote QUIT');
144         ok(sysread($slow, my $end, 4096) > 0, 'got EOF');
145         is(sysread($slow, my $eof, 4096), 0, 'got EOF');
146         $slow = undef;
148         test_lei(sub {
149                 lei_ok qw(ls-mail-source), "nntp://$starttls_addr",
150                         \'STARTTLS not used by default';
151                 my $plain_out = $lei_out;
152                 ok(!lei(qw(ls-mail-source -c nntp.starttls),
153                         "nntp://$starttls_addr"), 'STARTTLS verify fails');
154                 like $lei_err, qr/STARTTLS requested/,
155                         'STARTTLS noted in stderr';
156                 unlike $lei_err, qr!W: nntp\.starttls= .*? is not boolean!i,
157                         'no non-boolean warning';
158                 lei_ok qw(-c nntp.starttls -c nntp.sslVerify= ls-mail-source),
159                         "nntp://$starttls_addr",
160                         \'disabling nntp.sslVerify works w/ STARTTLS';
161                 is $lei_out, $plain_out, 'sslVerify=false w/ STARTTLS output';
163                 lei_ok qw(ls-mail-source -c nntp.sslVerify=false),
164                         "nntps://$nntps_addr",
165                         \'disabling nntp.sslVerify works w/ nntps://';
166                 is $lei_out, $plain_out, 'sslVerify=false w/ NNTPS output';
167         });
169         SKIP: {
170                 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
171                 my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
172                 defined(my $x = getsockopt($nntps, IPPROTO_TCP, $var)) or die;
173                 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on NNTPS');
174                 defined($x = getsockopt($starttls, IPPROTO_TCP, $var)) or die;
175                 is(unpack('i', $x), 0, 'TCP_DEFER_ACCEPT is 0 on plain NNTP');
176         };
177         SKIP: {
178                 require_mods '+accf_data';
179                 require PublicInbox::Daemon;
180                 my $x = getsockopt($nntps, SOL_SOCKET,
181                                 $PublicInbox::Daemon::SO_ACCEPTFILTER);
182                 like($x, qr/\Adataready\0+\z/, 'got dataready accf for NNTPS');
183                 $x = getsockopt($starttls, IPPROTO_TCP,
184                                 $PublicInbox::Daemon::SO_ACCEPTFILTER);
185                 is($x, undef, 'no BSD accept filter for plain NNTP');
186         };
188         my $s = tcp_connect($nntps);
189         syswrite($s, '->accept_SSL_ will fail on this!');
190         my @r;
191         do { # some platforms or OpenSSL versions need an extra read
192                 push @r, sysread($s, my $rbuf, 128);
193         } while ($r[-1] && @r < 2);
194         ok(!$r[-1], 'EOF or ECONNRESET on ->accept_SSL fail') or
195                 diag explain(\@r);
196         $c = undef;
197         $td->kill;
198         $td->join;
199         is($?, 0, 'no error in exited process');
200         my $eout = eval {
201                 open my $fh, '<', $err or die "open $err failed: $!";
202                 local $/;
203                 <$fh>;
204         };
205         unlike($eout, qr/wide/i, 'no Wide character warnings');
206         unlike($eout, qr/^E:/, 'no other errors');
208 done_testing();
210 sub get_capa {
211         my ($sock) = @_;
212         syswrite($sock, "CAPABILITIES\r\n");
213         my $capa = '';
214         do {
215                 my $r = sysread($sock, $capa, 8192, length($capa));
216                 die "unexpected: $!" unless defined($r);
217                 die 'unexpected EOF' if $r == 0;
218         } until $capa =~ /\.\r\n\z/;
220         my $deflate_capa = qr/\r\nCOMPRESS DEFLATE\r\n/;
221         if ($need_zlib) {
222                 unlike($capa, $deflate_capa,
223                         'COMPRESS DEFLATE NOT advertised '.$need_zlib);
224         } else {
225                 like($capa, $deflate_capa, 'COMPRESS DEFLATE advertised');
226         }
227         $capa;