1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use File::Temp qw(tempdir);
7 use Socket qw(SOCK_STREAM IPPROTO_TCP SOL_SOCKET);
8 # IO::Poll and Net::NNTP are part of the standard library, but
9 # distros may split them off...
10 foreach my $mod (qw(DBD::SQLite IO::Socket::SSL Net::NNTP IO::Poll)) {
12 plan skip_all => "$mod missing for $0" if $@;
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 ./create-certs.perl in certs/";
21 use_ok 'PublicInbox::TLS';
22 use_ok 'IO::Socket::SSL';
23 require './t/common.perl';
24 require PublicInbox::InboxWritable;
25 require PublicInbox::MIME;
26 require PublicInbox::SearchIdx;
27 my $version = 2; # v2 needs newer git
28 require_git('2.6') if $version >= 2;
29 my $tmpdir = tempdir('pi-nntpd-tls-XXXXXX', TMPDIR => 1, CLEANUP => 1);
30 my $err = "$tmpdir/stderr.log";
31 my $out = "$tmpdir/stdout.log";
32 my $mainrepo = "$tmpdir";
33 my $pi_config = "$tmpdir/pi_config";
34 my $group = 'test-nntpd-tls';
35 my $addr = $group . '@example.com';
36 my $nntpd = 'blib/script/public-inbox-nntpd';
38 LocalAddr => '127.0.0.1',
44 my $starttls = IO::Socket::INET->new(%opts);
45 my $nntps = IO::Socket::INET->new(%opts);
48 foreach ($pid, $tail_pid) {
49 kill 'TERM', $_ if defined $_;
53 my $ibx = PublicInbox::Inbox->new({
54 mainrepo => $mainrepo,
57 -primary_address => $addr,
58 indexlevel => 'basic',
60 $ibx = PublicInbox::InboxWritable->new($ibx, {nproc=>1});
63 open my $fh, '>', $pi_config or die "open: $!\n";
65 [publicinbox "nntpd-tls"]
72 close $fh or die "close: $!\n";
76 my $im = $ibx->importer(0);
77 my $mime = PublicInbox::MIME->new(do {
78 open my $fh, '<', 't/data/0001.patch' or die;
82 ok($im->add($mime), 'message added');
85 my $s = PublicInbox::SearchIdx->new($ibx, 1);
90 my $nntps_addr = $nntps->sockhost . ':' . $nntps->sockport;
91 my $starttls_addr = $starttls->sockhost . ':' . $starttls->sockport;
92 my $env = { PI_CONFIG => $pi_config };
95 [ "--cert=$cert", "--key=$key",
96 "-lnntps://$nntps_addr",
97 "-lnntp://$starttls_addr" ],
100 open my $fh, '>', $_ or die "truncate: $!";
102 if (my $tail_cmd = $ENV{TAIL}) { # don't assume GNU tail
104 if (defined $tail_pid && $tail_pid == 0) {
105 exec(split(' ', $tail_cmd), $out, $err);
108 my $cmd = [ $nntpd, '-W0', @$args, "--stdout=$out", "--stderr=$err" ];
109 $pid = spawn_listener($env, $cmd, [ $starttls, $nntps ]);
111 SSL_hostname => 'server.local',
112 SSL_verifycn_name => 'server.local',
113 SSL_verify_mode => SSL_VERIFY_PEER(),
114 SSL_ca_file => 'certs/test-ca.pem',
116 my $expect = { $group => [qw(1 1 n)] };
118 # start negotiating a slow TLS connection
119 my $slow = IO::Socket::INET->new(
121 PeerAddr => $nntps_addr,
125 $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
126 my $slow_done = $slow->connect_SSL;
127 diag('W: connect_SSL early OK, slow client test invalid') if $slow_done;
128 my @poll = (fileno($slow), PublicInbox::TLS::epollbit());
129 # we should call connect_SSL much later...
132 my $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
134 is_deeply($list, $expect, 'NNTPS LIST works');
135 is($c->command('QUIT')->response(), Net::Cmd::CMD_OK(), 'QUIT works');
136 is(0, sysread($c, my $buf, 1), 'got EOF after QUIT');
139 $c = Net::NNTP->new($starttls_addr, %o);
141 is_deeply($list, $expect, 'plain LIST works');
142 ok($c->starttls, 'STARTTLS succeeds');
143 is($c->code, 382, 'got 382 for STARTTLS');
145 is_deeply($list, $expect, 'LIST works after STARTTLS');
147 # Net::NNTP won't let us do dumb things, but we need to test
148 # dumb things, so use Net::Cmd directly:
149 my $n = $c->command('STARTTLS')->response();
150 is($n, Net::Cmd::CMD_ERROR(), 'error attempting STARTTLS again');
151 is($c->code, 502, '502 according to RFC 4642 sec#2.2.1');
153 # STARTTLS with bad hostname
154 $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.invalid';
155 $c = Net::NNTP->new($starttls_addr, %o);
157 is_deeply($list, $expect, 'plain LIST works again');
158 ok(!$c->starttls, 'STARTTLS fails with bad hostname');
159 $c = Net::NNTP->new($starttls_addr, %o);
161 is_deeply($list, $expect, 'not broken after bad negotiation');
163 # NNTPS with bad hostname
164 $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
165 is($c, undef, 'NNTPS fails with bad hostname');
166 $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
167 $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
168 ok($c, 'NNTPS succeeds again with valid hostname');
170 # slow TLS connection did not block the other fast clients while
171 # connecting, finish it off:
173 IO::Poll::_poll(-1, @poll);
174 $slow_done = $slow->connect_SSL and last;
175 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
178 ok(sysread($slow, my $greet, 4096) > 0, 'slow got greeting');
179 like($greet, qr/\A201 /, 'got expected greeting');
180 is(syswrite($slow, "QUIT\r\n"), 6, 'slow wrote QUIT');
181 ok(sysread($slow, my $end, 4096) > 0, 'got EOF');
182 is(sysread($slow, my $eof, 4096), 0, 'got EOF');
186 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
187 my $var = Socket::TCP_DEFER_ACCEPT();
188 defined(my $x = getsockopt($nntps, IPPROTO_TCP, $var)) or die;
189 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on NNTPS');
190 defined($x = getsockopt($starttls, IPPROTO_TCP, $var)) or die;
191 is(unpack('i', $x), 0, 'TCP_DEFER_ACCEPT is 0 on plain NNTP');
194 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
195 if (system('kldstat -m accf_data >/dev/null')) {
196 skip 'accf_data not loaded? kldload accf_data', 2;
198 require PublicInbox::Daemon;
199 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
200 my $x = getsockopt($nntps, SOL_SOCKET, $var);
201 like($x, qr/\Adataready\0+\z/, 'got dataready accf for NNTPS');
202 $x = getsockopt($starttls, IPPROTO_TCP, $var);
203 is($x, undef, 'no BSD accept filter for plain NNTP');
208 is($pid, waitpid($pid, 0), 'nntpd exited successfully');
209 is($?, 0, 'no error in exited process');
212 open my $fh, '<', $err or die "open $err failed: $!";
216 unlike($eout, qr/wide/i, 'no Wide character warnings');
217 if (defined $tail_pid) {
218 kill 'TERM', $tail_pid;
219 waitpid($tail_pid, 0);