1 # Copyright (C) 2019-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(SOCK_STREAM IPPROTO_TCP SOL_SOCKET);
7 use PublicInbox::TestCommon;
8 # IO::Poll and Net::NNTP are part of the standard library, but
9 # distros may split them off...
10 require_mods(qw(DBD::SQLite IO::Socket::SSL Net::NNTP IO::Poll));
11 Net::NNTP->can('starttls') or
12 plan skip_all => 'Net::NNTP does not support TLS';
14 my $cert = 'certs/server-cert.pem';
15 my $key = 'certs/server-key.pem';
16 unless (-r $key && -r $cert) {
18 "certs/ missing for $0, run $^X ./create-certs.perl in certs/";
21 use_ok 'PublicInbox::TLS';
22 use_ok 'IO::Socket::SSL';
23 require PublicInbox::InboxWritable;
24 require PublicInbox::Eml;
25 require PublicInbox::SearchIdx;
27 eval { require Compress::Raw::Zlib } or
28 $need_zlib = 'Compress::Raw::Zlib missing';
29 my $version = 2; # v2 needs newer git
30 require_git('2.6') if $version >= 2;
31 my ($tmpdir, $for_destroy) = tmpdir();
32 my $err = "$tmpdir/stderr.log";
33 my $out = "$tmpdir/stdout.log";
34 my $inboxdir = "$tmpdir";
35 my $pi_config = "$tmpdir/pi_config";
36 my $group = 'test-nntpd-tls';
37 my $addr = $group . '@example.com';
38 my $starttls = tcp_server();
39 my $nntps = tcp_server();
40 my $ibx = PublicInbox::Inbox->new({
41 inboxdir => $inboxdir,
44 -primary_address => $addr,
45 indexlevel => 'basic',
47 $ibx = PublicInbox::InboxWritable->new($ibx, {nproc=>1});
50 open my $fh, '>', $pi_config or die "open: $!\n";
52 [publicinbox "nntpd-tls"]
59 close $fh or die "close: $!\n";
63 my $im = $ibx->importer(0);
64 my $mime = eml_load 't/data/0001.patch';
65 ok($im->add($mime), 'message added');
68 my $s = PublicInbox::SearchIdx->new($ibx, 1);
73 my $nntps_addr = $nntps->sockhost . ':' . $nntps->sockport;
74 my $starttls_addr = $starttls->sockhost . ':' . $starttls->sockport;
75 my $env = { PI_CONFIG => $pi_config };
79 [ "--cert=$cert", "--key=$key",
80 "-lnntps://$nntps_addr",
81 "-lnntp://$starttls_addr" ],
84 open my $fh, '>', $_ or die "truncate: $!";
86 my $cmd = [ '-nntpd', '-W0', @$args, "--stdout=$out", "--stderr=$err" ];
87 $td = start_script($cmd, $env, { 3 => $starttls, 4 => $nntps });
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 my $expect = { $group => [qw(1 1 n)] };
96 # start negotiating a slow TLS connection
97 my $slow = tcp_connect($nntps, Blocking => 0);
98 $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
99 my $slow_done = $slow->connect_SSL;
102 diag('W: connect_SSL early OK, slow client test invalid');
103 use PublicInbox::Syscall qw(EPOLLIN EPOLLOUT);
104 @poll = (fileno($slow), EPOLLIN | EPOLLOUT);
106 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
108 # we should call connect_SSL much later...
111 my $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
113 is_deeply($list, $expect, 'NNTPS LIST works');
114 unlike(get_capa($c), qr/\bSTARTTLS\r\n/,
115 'STARTTLS not advertised for NNTPS');
116 is($c->command('QUIT')->response(), Net::Cmd::CMD_OK(), 'QUIT works');
117 is(0, sysread($c, my $buf, 1), 'got EOF after QUIT');
120 $c = Net::NNTP->new($starttls_addr, %o);
122 is_deeply($list, $expect, 'plain LIST works');
123 ok($c->starttls, 'STARTTLS succeeds');
124 is($c->code, 382, 'got 382 for STARTTLS');
126 is_deeply($list, $expect, 'LIST works after STARTTLS');
127 unlike(get_capa($c), qr/\bSTARTTLS\r\n/,
128 'STARTTLS not advertised after STARTTLS');
130 # Net::NNTP won't let us do dumb things, but we need to test
131 # dumb things, so use Net::Cmd directly:
132 my $n = $c->command('STARTTLS')->response();
133 is($n, Net::Cmd::CMD_ERROR(), 'error attempting STARTTLS again');
134 is($c->code, 502, '502 according to RFC 4642 sec#2.2.1');
136 # STARTTLS with bad hostname
137 $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.invalid';
138 $c = Net::NNTP->new($starttls_addr, %o);
139 like(get_capa($c), qr/\bSTARTTLS\r\n/, 'STARTTLS advertised');
141 is_deeply($list, $expect, 'plain LIST works again');
142 ok(!$c->starttls, 'STARTTLS fails with bad hostname');
143 $c = Net::NNTP->new($starttls_addr, %o);
145 is_deeply($list, $expect, 'not broken after bad negotiation');
147 # NNTPS with bad hostname
148 $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
149 is($c, undef, 'NNTPS fails with bad hostname');
150 $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
151 $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
152 ok($c, 'NNTPS succeeds again with valid hostname');
154 # slow TLS connection did not block the other fast clients while
155 # connecting, finish it off:
157 IO::Poll::_poll(-1, @poll);
158 $slow_done = $slow->connect_SSL and last;
159 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
162 ok(sysread($slow, my $greet, 4096) > 0, 'slow got greeting');
163 like($greet, qr/\A201 /, 'got expected greeting');
164 is(syswrite($slow, "QUIT\r\n"), 6, 'slow wrote QUIT');
165 ok(sysread($slow, my $end, 4096) > 0, 'got EOF');
166 is(sysread($slow, my $eof, 4096), 0, 'got EOF');
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');
178 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
179 if (system('kldstat -m accf_data >/dev/null')) {
180 skip 'accf_data not loaded? kldload accf_data', 2;
182 require PublicInbox::Daemon;
183 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
184 my $x = getsockopt($nntps, SOL_SOCKET, $var);
185 like($x, qr/\Adataready\0+\z/, 'got dataready accf for NNTPS');
186 $x = getsockopt($starttls, IPPROTO_TCP, $var);
187 is($x, undef, 'no BSD accept filter for plain NNTP');
193 is($?, 0, 'no error in exited process');
195 open my $fh, '<', $err or die "open $err failed: $!";
199 unlike($eout, qr/wide/i, 'no Wide character warnings');
205 syswrite($sock, "CAPABILITIES\r\n");
208 my $r = sysread($sock, $capa, 8192, length($capa));
209 die "unexpected: $!" unless defined($r);
210 die 'unexpected EOF' if $r == 0;
211 } until $capa =~ /\.\r\n\z/;
213 my $deflate_capa = qr/\r\nCOMPRESS DEFLATE\r\n/;
215 unlike($capa, $deflate_capa,
216 'COMPRESS DEFLATE NOT advertised '.$need_zlib);
218 like($capa, $deflate_capa, 'COMPRESS DEFLATE advertised');