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 $mime = PublicInbox::MIME->new(do {
67 open my $fh, '<', 't/data/0001.patch' or die;
71 ok($im->add($mime), 'message added');
74 my $s = PublicInbox::SearchIdx->new($ibx, 1);
79 my $nntps_addr = $nntps->sockhost . ':' . $nntps->sockport;
80 my $starttls_addr = $starttls->sockhost . ':' . $starttls->sockport;
81 my $env = { PI_CONFIG => $pi_config };
85 [ "--cert=$cert", "--key=$key",
86 "-lnntps://$nntps_addr",
87 "-lnntp://$starttls_addr" ],
90 open my $fh, '>', $_ or die "truncate: $!";
92 my $cmd = [ '-nntpd', '-W0', @$args, "--stdout=$out", "--stderr=$err" ];
93 $td = start_script($cmd, $env, { 3 => $starttls, 4 => $nntps });
95 SSL_hostname => 'server.local',
96 SSL_verifycn_name => 'server.local',
97 SSL_verify_mode => SSL_VERIFY_PEER(),
98 SSL_ca_file => 'certs/test-ca.pem',
100 my $expect = { $group => [qw(1 1 n)] };
102 # start negotiating a slow TLS connection
103 my $slow = tcp_connect($nntps, Blocking => 0);
104 $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
105 my $slow_done = $slow->connect_SSL;
108 diag('W: connect_SSL early OK, slow client test invalid');
109 use PublicInbox::Syscall qw(EPOLLIN EPOLLOUT);
110 @poll = (fileno($slow), EPOLLIN | EPOLLOUT);
112 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
114 # we should call connect_SSL much later...
117 my $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
119 is_deeply($list, $expect, 'NNTPS LIST works');
120 unlike(get_capa($c), qr/\bSTARTTLS\r\n/,
121 'STARTTLS not advertised for NNTPS');
122 is($c->command('QUIT')->response(), Net::Cmd::CMD_OK(), 'QUIT works');
123 is(0, sysread($c, my $buf, 1), 'got EOF after QUIT');
126 $c = Net::NNTP->new($starttls_addr, %o);
128 is_deeply($list, $expect, 'plain LIST works');
129 ok($c->starttls, 'STARTTLS succeeds');
130 is($c->code, 382, 'got 382 for STARTTLS');
132 is_deeply($list, $expect, 'LIST works after STARTTLS');
133 unlike(get_capa($c), qr/\bSTARTTLS\r\n/,
134 'STARTTLS not advertised after STARTTLS');
136 # Net::NNTP won't let us do dumb things, but we need to test
137 # dumb things, so use Net::Cmd directly:
138 my $n = $c->command('STARTTLS')->response();
139 is($n, Net::Cmd::CMD_ERROR(), 'error attempting STARTTLS again');
140 is($c->code, 502, '502 according to RFC 4642 sec#2.2.1');
142 # STARTTLS with bad hostname
143 $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.invalid';
144 $c = Net::NNTP->new($starttls_addr, %o);
145 like(get_capa($c), qr/\bSTARTTLS\r\n/, 'STARTTLS advertised');
147 is_deeply($list, $expect, 'plain LIST works again');
148 ok(!$c->starttls, 'STARTTLS fails with bad hostname');
149 $c = Net::NNTP->new($starttls_addr, %o);
151 is_deeply($list, $expect, 'not broken after bad negotiation');
153 # NNTPS with bad hostname
154 $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
155 is($c, undef, 'NNTPS fails with bad hostname');
156 $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
157 $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
158 ok($c, 'NNTPS succeeds again with valid hostname');
160 # slow TLS connection did not block the other fast clients while
161 # connecting, finish it off:
163 IO::Poll::_poll(-1, @poll);
164 $slow_done = $slow->connect_SSL and last;
165 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
168 ok(sysread($slow, my $greet, 4096) > 0, 'slow got greeting');
169 like($greet, qr/\A201 /, 'got expected greeting');
170 is(syswrite($slow, "QUIT\r\n"), 6, 'slow wrote QUIT');
171 ok(sysread($slow, my $end, 4096) > 0, 'got EOF');
172 is(sysread($slow, my $eof, 4096), 0, 'got EOF');
176 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
177 my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
178 defined(my $x = getsockopt($nntps, IPPROTO_TCP, $var)) or die;
179 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on NNTPS');
180 defined($x = getsockopt($starttls, IPPROTO_TCP, $var)) or die;
181 is(unpack('i', $x), 0, 'TCP_DEFER_ACCEPT is 0 on plain NNTP');
184 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
185 if (system('kldstat -m accf_data >/dev/null')) {
186 skip 'accf_data not loaded? kldload accf_data', 2;
188 require PublicInbox::Daemon;
189 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
190 my $x = getsockopt($nntps, SOL_SOCKET, $var);
191 like($x, qr/\Adataready\0+\z/, 'got dataready accf for NNTPS');
192 $x = getsockopt($starttls, IPPROTO_TCP, $var);
193 is($x, undef, 'no BSD accept filter for plain NNTP');
199 is($?, 0, 'no error in exited process');
201 open my $fh, '<', $err or die "open $err failed: $!";
205 unlike($eout, qr/wide/i, 'no Wide character warnings');
211 syswrite($sock, "CAPABILITIES\r\n");
214 my $r = sysread($sock, $capa, 8192, length($capa));
215 die "unexpected: $!" unless defined($r);
216 die 'unexpected EOF' if $r == 0;
217 } until $capa =~ /\.\r\n\z/;
219 my $deflate_capa = qr/\r\nCOMPRESS DEFLATE\r\n/;
221 unlike($capa, $deflate_capa,
222 'COMPRESS DEFLATE NOT advertised '.$need_zlib);
224 like($capa, $deflate_capa, 'COMPRESS DEFLATE advertised');