]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntpd-tls.t
82b63f3e443cee55820b614cf7442a94b4655600
[public-inbox.git] / t / nntpd-tls.t
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
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)) {
11         eval "require $mod";
12         plan skip_all => "$mod missing for $0" if $@;
13 }
14 Net::NNTP->can('starttls') or
15         plan skip_all => 'Net::NNTP does not support TLS';
16
17 my $cert = 'certs/server-cert.pem';
18 my $key = 'certs/server-key.pem';
19 unless (-r $key && -r $cert) {
20         plan skip_all =>
21                 "certs/ missing for $0, run ./create-certs.perl in certs/";
22 }
23
24 use_ok 'PublicInbox::TLS';
25 use_ok 'IO::Socket::SSL';
26 require './t/common.perl';
27 require PublicInbox::InboxWritable;
28 require PublicInbox::MIME;
29 require PublicInbox::SearchIdx;
30 my $version = 2; # v2 needs newer git
31 require_git('2.6') if $version >= 2;
32 my $tmpdir = tempdir('pi-nntpd-tls-XXXXXX', TMPDIR => 1, CLEANUP => 1);
33 my $err = "$tmpdir/stderr.log";
34 my $out = "$tmpdir/stdout.log";
35 my $mainrepo = "$tmpdir";
36 my $pi_config = "$tmpdir/pi_config";
37 my $group = 'test-nntpd-tls';
38 my $addr = $group . '@example.com';
39 my $nntpd = 'blib/script/public-inbox-nntpd';
40 my $starttls = tcp_server();
41 my $nntps = tcp_server();
42 my ($pid, $tail_pid);
43 END {
44         foreach ($pid, $tail_pid) {
45                 kill 'TERM', $_ if defined $_;
46         }
47 };
48
49 my $ibx = PublicInbox::Inbox->new({
50         mainrepo => $mainrepo,
51         name => 'nntpd-tls',
52         version => $version,
53         -primary_address => $addr,
54         indexlevel => 'basic',
55 });
56 $ibx = PublicInbox::InboxWritable->new($ibx, {nproc=>1});
57 $ibx->init_inbox(0);
58 {
59         open my $fh, '>', $pi_config or die "open: $!\n";
60         print $fh <<EOF
61 [publicinbox "nntpd-tls"]
62         mainrepo = $mainrepo
63         address = $addr
64         indexlevel = basic
65         newsgroup = $group
66 EOF
67         ;
68         close $fh or die "close: $!\n";
69 }
70
71 {
72         my $im = $ibx->importer(0);
73         my $mime = PublicInbox::MIME->new(do {
74                 open my $fh, '<', 't/data/0001.patch' or die;
75                 local $/;
76                 <$fh>
77         });
78         ok($im->add($mime), 'message added');
79         $im->done;
80         if ($version == 1) {
81                 my $s = PublicInbox::SearchIdx->new($ibx, 1);
82                 $s->index_sync;
83         }
84 }
85
86 my $nntps_addr = $nntps->sockhost . ':' . $nntps->sockport;
87 my $starttls_addr = $starttls->sockhost . ':' . $starttls->sockport;
88 my $env = { PI_CONFIG => $pi_config };
89
90 for my $args (
91         [ "--cert=$cert", "--key=$key",
92                 "-lnntps://$nntps_addr",
93                 "-lnntp://$starttls_addr" ],
94 ) {
95         for ($out, $err) {
96                 open my $fh, '>', $_ or die "truncate: $!";
97         }
98         if (my $tail_cmd = $ENV{TAIL}) { # don't assume GNU tail
99                 $tail_pid = fork;
100                 if (defined $tail_pid && $tail_pid == 0) {
101                         exec(split(' ', $tail_cmd), $out, $err);
102                 }
103         }
104         my $cmd = [ $nntpd, '-W0', @$args, "--stdout=$out", "--stderr=$err" ];
105         $pid = spawn_listener($env, $cmd, [ $starttls, $nntps ]);
106         my %o = (
107                 SSL_hostname => 'server.local',
108                 SSL_verifycn_name => 'server.local',
109                 SSL_verify_mode => SSL_VERIFY_PEER(),
110                 SSL_ca_file => 'certs/test-ca.pem',
111         );
112         my $expect = { $group => [qw(1 1 n)] };
113
114         # start negotiating a slow TLS connection
115         my $slow = IO::Socket::INET->new(
116                 Proto => 'tcp',
117                 PeerAddr => $nntps_addr,
118                 Type => SOCK_STREAM,
119                 Blocking => 0,
120         );
121         $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
122         my $slow_done = $slow->connect_SSL;
123         diag('W: connect_SSL early OK, slow client test invalid') if $slow_done;
124         my @poll = (fileno($slow), PublicInbox::TLS::epollbit());
125         # we should call connect_SSL much later...
126
127         # NNTPS
128         my $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
129         my $list = $c->list;
130         is_deeply($list, $expect, 'NNTPS LIST works');
131         is($c->command('QUIT')->response(), Net::Cmd::CMD_OK(), 'QUIT works');
132         is(0, sysread($c, my $buf, 1), 'got EOF after QUIT');
133
134         # STARTTLS
135         $c = Net::NNTP->new($starttls_addr, %o);
136         $list = $c->list;
137         is_deeply($list, $expect, 'plain LIST works');
138         ok($c->starttls, 'STARTTLS succeeds');
139         is($c->code, 382, 'got 382 for STARTTLS');
140         $list = $c->list;
141         is_deeply($list, $expect, 'LIST works after STARTTLS');
142
143         # Net::NNTP won't let us do dumb things, but we need to test
144         # dumb things, so use Net::Cmd directly:
145         my $n = $c->command('STARTTLS')->response();
146         is($n, Net::Cmd::CMD_ERROR(), 'error attempting STARTTLS again');
147         is($c->code, 502, '502 according to RFC 4642 sec#2.2.1');
148
149         # STARTTLS with bad hostname
150         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.invalid';
151         $c = Net::NNTP->new($starttls_addr, %o);
152         $list = $c->list;
153         is_deeply($list, $expect, 'plain LIST works again');
154         ok(!$c->starttls, 'STARTTLS fails with bad hostname');
155         $c = Net::NNTP->new($starttls_addr, %o);
156         $list = $c->list;
157         is_deeply($list, $expect, 'not broken after bad negotiation');
158
159         # NNTPS with bad hostname
160         $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
161         is($c, undef, 'NNTPS fails with bad hostname');
162         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
163         $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
164         ok($c, 'NNTPS succeeds again with valid hostname');
165
166         # slow TLS connection did not block the other fast clients while
167         # connecting, finish it off:
168         until ($slow_done) {
169                 IO::Poll::_poll(-1, @poll);
170                 $slow_done = $slow->connect_SSL and last;
171                 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
172         }
173         $slow->blocking(1);
174         ok(sysread($slow, my $greet, 4096) > 0, 'slow got greeting');
175         like($greet, qr/\A201 /, 'got expected greeting');
176         is(syswrite($slow, "QUIT\r\n"), 6, 'slow wrote QUIT');
177         ok(sysread($slow, my $end, 4096) > 0, 'got EOF');
178         is(sysread($slow, my $eof, 4096), 0, 'got EOF');
179         $slow = undef;
180
181         SKIP: {
182                 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
183                 my $var = Socket::TCP_DEFER_ACCEPT();
184                 defined(my $x = getsockopt($nntps, IPPROTO_TCP, $var)) or die;
185                 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on NNTPS');
186                 defined($x = getsockopt($starttls, IPPROTO_TCP, $var)) or die;
187                 is(unpack('i', $x), 0, 'TCP_DEFER_ACCEPT is 0 on plain NNTP');
188         };
189         SKIP: {
190                 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
191                 if (system('kldstat -m accf_data >/dev/null')) {
192                         skip 'accf_data not loaded? kldload accf_data', 2;
193                 }
194                 require PublicInbox::Daemon;
195                 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
196                 my $x = getsockopt($nntps, SOL_SOCKET, $var);
197                 like($x, qr/\Adataready\0+\z/, 'got dataready accf for NNTPS');
198                 $x = getsockopt($starttls, IPPROTO_TCP, $var);
199                 is($x, undef, 'no BSD accept filter for plain NNTP');
200         };
201
202         $c = undef;
203         kill('TERM', $pid);
204         is($pid, waitpid($pid, 0), 'nntpd exited successfully');
205         is($?, 0, 'no error in exited process');
206         $pid = undef;
207         my $eout = eval {
208                 open my $fh, '<', $err or die "open $err failed: $!";
209                 local $/;
210                 <$fh>;
211         };
212         unlike($eout, qr/wide/i, 'no Wide character warnings');
213         if (defined $tail_pid) {
214                 kill 'TERM', $tail_pid;
215                 waitpid($tail_pid, 0);
216                 $tail_pid = undef;
217         }
218 }
219 done_testing();
220 1;