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';
13 IO::Socket::SSL->VERSION(2.007) or
14 plan skip_all => 'IO::Socket::SSL <2.007 not supported by Net::NNTP';
16 my $cert = 'certs/server-cert.pem';
17 my $key = 'certs/server-key.pem';
18 unless (-r $key && -r $cert) {
20 "certs/ missing for $0, run $^X ./create-certs.perl in certs/";
23 use_ok 'PublicInbox::TLS';
24 use_ok 'IO::Socket::SSL';
25 require PublicInbox::InboxWritable;
26 require PublicInbox::MIME;
27 require PublicInbox::SearchIdx;
29 eval { require Compress::Raw::Zlib } or
30 $need_zlib = 'Compress::Raw::Zlib missing';
31 my $version = 2; # v2 needs newer git
32 require_git('2.6') if $version >= 2;
33 my ($tmpdir, $for_destroy) = tmpdir();
34 my $err = "$tmpdir/stderr.log";
35 my $out = "$tmpdir/stdout.log";
36 my $inboxdir = "$tmpdir";
37 my $pi_config = "$tmpdir/pi_config";
38 my $group = 'test-nntpd-tls';
39 my $addr = $group . '@example.com';
40 my $starttls = tcp_server();
41 my $nntps = tcp_server();
42 my $ibx = PublicInbox::Inbox->new({
43 inboxdir => $inboxdir,
46 -primary_address => $addr,
47 indexlevel => 'basic',
49 $ibx = PublicInbox::InboxWritable->new($ibx, {nproc=>1});
52 open my $fh, '>', $pi_config or die "open: $!\n";
54 [publicinbox "nntpd-tls"]
61 close $fh or die "close: $!\n";
65 my $im = $ibx->importer(0);
66 my $eml = 't/data/0001.patch';
67 my $mime = PublicInbox::InboxWritable::mime_from_path($eml) or
69 ok($im->add($mime), 'message added');
72 my $s = PublicInbox::SearchIdx->new($ibx, 1);
77 my $nntps_addr = $nntps->sockhost . ':' . $nntps->sockport;
78 my $starttls_addr = $starttls->sockhost . ':' . $starttls->sockport;
79 my $env = { PI_CONFIG => $pi_config };
83 [ "--cert=$cert", "--key=$key",
84 "-lnntps://$nntps_addr",
85 "-lnntp://$starttls_addr" ],
88 open my $fh, '>', $_ or die "truncate: $!";
90 my $cmd = [ '-nntpd', '-W0', @$args, "--stdout=$out", "--stderr=$err" ];
91 $td = start_script($cmd, $env, { 3 => $starttls, 4 => $nntps });
93 SSL_hostname => 'server.local',
94 SSL_verifycn_name => 'server.local',
95 SSL_verify_mode => SSL_VERIFY_PEER(),
96 SSL_ca_file => 'certs/test-ca.pem',
98 my $expect = { $group => [qw(1 1 n)] };
100 # start negotiating a slow TLS connection
101 my $slow = tcp_connect($nntps, Blocking => 0);
102 $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
103 my $slow_done = $slow->connect_SSL;
106 diag('W: connect_SSL early OK, slow client test invalid');
107 use PublicInbox::Syscall qw(EPOLLIN EPOLLOUT);
108 @poll = (fileno($slow), EPOLLIN | EPOLLOUT);
110 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
112 # we should call connect_SSL much later...
115 my $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
117 is_deeply($list, $expect, 'NNTPS LIST works');
118 unlike(get_capa($c), qr/\bSTARTTLS\r\n/,
119 'STARTTLS not advertised for NNTPS');
120 is($c->command('QUIT')->response(), Net::Cmd::CMD_OK(), 'QUIT works');
121 is(0, sysread($c, my $buf, 1), 'got EOF after QUIT');
124 $c = Net::NNTP->new($starttls_addr, %o);
126 is_deeply($list, $expect, 'plain LIST works');
127 ok($c->starttls, 'STARTTLS succeeds');
128 is($c->code, 382, 'got 382 for STARTTLS');
130 is_deeply($list, $expect, 'LIST works after STARTTLS');
131 unlike(get_capa($c), qr/\bSTARTTLS\r\n/,
132 'STARTTLS not advertised after STARTTLS');
134 # Net::NNTP won't let us do dumb things, but we need to test
135 # dumb things, so use Net::Cmd directly:
136 my $n = $c->command('STARTTLS')->response();
137 is($n, Net::Cmd::CMD_ERROR(), 'error attempting STARTTLS again');
138 is($c->code, 502, '502 according to RFC 4642 sec#2.2.1');
140 # STARTTLS with bad hostname
141 $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.invalid';
142 $c = Net::NNTP->new($starttls_addr, %o);
143 like(get_capa($c), qr/\bSTARTTLS\r\n/, 'STARTTLS advertised');
145 is_deeply($list, $expect, 'plain LIST works again');
146 ok(!$c->starttls, 'STARTTLS fails with bad hostname');
147 $c = Net::NNTP->new($starttls_addr, %o);
149 is_deeply($list, $expect, 'not broken after bad negotiation');
151 # NNTPS with bad hostname
152 $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
153 is($c, undef, 'NNTPS fails with bad hostname');
154 $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
155 $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
156 ok($c, 'NNTPS succeeds again with valid hostname');
158 # slow TLS connection did not block the other fast clients while
159 # connecting, finish it off:
161 IO::Poll::_poll(-1, @poll);
162 $slow_done = $slow->connect_SSL and last;
163 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
166 ok(sysread($slow, my $greet, 4096) > 0, 'slow got greeting');
167 like($greet, qr/\A201 /, 'got expected greeting');
168 is(syswrite($slow, "QUIT\r\n"), 6, 'slow wrote QUIT');
169 ok(sysread($slow, my $end, 4096) > 0, 'got EOF');
170 is(sysread($slow, my $eof, 4096), 0, 'got EOF');
174 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
175 my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
176 defined(my $x = getsockopt($nntps, IPPROTO_TCP, $var)) or die;
177 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on NNTPS');
178 defined($x = getsockopt($starttls, IPPROTO_TCP, $var)) or die;
179 is(unpack('i', $x), 0, 'TCP_DEFER_ACCEPT is 0 on plain NNTP');
182 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
183 if (system('kldstat -m accf_data >/dev/null')) {
184 skip 'accf_data not loaded? kldload accf_data', 2;
186 require PublicInbox::Daemon;
187 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
188 my $x = getsockopt($nntps, SOL_SOCKET, $var);
189 like($x, qr/\Adataready\0+\z/, 'got dataready accf for NNTPS');
190 $x = getsockopt($starttls, IPPROTO_TCP, $var);
191 is($x, undef, 'no BSD accept filter for plain NNTP');
197 is($?, 0, 'no error in exited process');
199 open my $fh, '<', $err or die "open $err failed: $!";
203 unlike($eout, qr/wide/i, 'no Wide character warnings');
209 syswrite($sock, "CAPABILITIES\r\n");
212 my $r = sysread($sock, $capa, 8192, length($capa));
213 die "unexpected: $!" unless defined($r);
214 die 'unexpected EOF' if $r == 0;
215 } until $capa =~ /\.\r\n\z/;
217 my $deflate_capa = qr/\r\nCOMPRESS DEFLATE\r\n/;
219 unlike($capa, $deflate_capa,
220 'COMPRESS DEFLATE NOT advertised '.$need_zlib);
222 like($capa, $deflate_capa, 'COMPRESS DEFLATE advertised');