Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_tls_memcache.pm

raw
1 package ProFTPD::Tests::Modules::mod_tls_memcache;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use Cache::Memcached;
8 use Carp;
9 use File::Spec;
10 use IO::Handle;
11 use IPC::Open3;
12 use Socket;
13
14 use ProFTPD::TestSuite::FTP;
15 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
16
17 $| = 1;
18
19 my $order = 0;
20
21 my $TESTS = {
22 tls_sess_cache_memcache => {
23 order => ++$order,
24 test_class => [qw(forking)],
25 },
26
27 tls_sess_cache_memcache_json_bug4057 => {
28 order => ++$order,
29 test_class => [qw(bug forking)],
30 },
31
32 tls_stapling_on_memcache_bug4175 => {
33 order => ++$order,
34 test_class => [qw(bug forking)],
35 },
36
37 };
38
39 sub new {
40 return shift()->SUPER::new(@_);
41 }
42
43 sub set_up {
44 my $self = shift;
45 $self->SUPER::set_up(@_);
46
47 # Clear the memcached servers before each unit test
48 my $memcached_servers = $ENV{MEMCACHED_SERVERS} ? $ENV{MEMCACHED_SERVERS} : "127.0.0.1:11211";
49 unless ($memcached_servers =~ /:/) {
50 $memcached_servers .= ':11211';
51 }
52
53 $memcached_servers = [split(/,?\s+?/, $memcached_servers)];
54
55 my $mc = Cache::Memcached->new({
56 servers => $memcached_servers,
57 debug => 0,
58 });
59
60 # First, make sure that a memcached is running
61 my $stats = $mc->stats('misc');
62 unless ($stats) {
63 die("Can't obtain stats from memached servers '$memcached_servers'");
64 }
65
66 $mc->flush_all();
67 $mc->disconnect_all();
68 }
69
70 sub list_tests {
71 # Check for the required Perl modules:
72 #
73 # Net-SSLeay
74 # IO-Socket-SSL
75 # Net-FTPSSL
76
77 my $required = [qw(
78 Net::SSLeay
79 IO::Socket::SSL
80 Net::FTPSSL
81 )];
82
83 foreach my $req (@$required) {
84 eval "use $req";
85 if ($@) {
86 print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n";
87
88 if ($ENV{TEST_VERBOSE}) {
89 print STDERR "Unable to load $req: $@\n";
90 }
91
92 return qw(testsuite_empty_test);
93 }
94 }
95
96 return testsuite_get_runnable_tests($TESTS);
97 }
98
99 sub tls_sess_cache_memcache {
100 my $self = shift;
101 my $tmpdir = $self->{tmpdir};
102 my $setup = test_setup($tmpdir, 'tls_memcache');
103
104 my $memcached_servers = $ENV{MEMCACHED_SERVERS} ? $ENV{MEMCACHED_SERVERS} : '127.0.0.1:11211';
105
106 my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
107 my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
108
109 my $sessid_file = File::Spec->rel2abs("$tmpdir/sessid.pem");
110
111 my $config = {
112 PidFile => $setup->{pid_file},
113 ScoreboardFile => $setup->{scoreboard_file},
114 SystemLog => $setup->{log_file},
115 TraceLog => $setup->{log_file},
116 Trace => 'tls:20 memcache:30 tls.memcache:20',
117
118 AuthUserFile => $setup->{auth_user_file},
119 AuthGroupFile => $setup->{auth_group_file},
120 AuthOrder => 'mod_auth_file.c',
121
122 IfModules => {
123 'mod_delay.c' => {
124 DelayEngine => 'off',
125 },
126
127 'mod_memcache.c' => {
128 MemcacheEngine => 'on',
129 MemcacheLog => $setup->{log_file},
130 MemcacheServers => $memcached_servers,
131 },
132
133 'mod_tls.c' => {
134 TLSEngine => 'on',
135 TLSLog => $setup->{log_file},
136 TLSRequired => 'on',
137 TLSRSACertificateFile => $cert_file,
138 TLSCACertificateFile => $ca_file,
139 TLSVerifyClient => 'off',
140 TLSOptions => 'EnableDiags',
141
142 # Necessary for session ID caching; TLSv1.3 uses session tickets
143 TLSProtocol => 'TLSv1.2',
144 },
145
146 'mod_tls_memcache.c' => {
147 TLSSessionCache => 'memcache:',
148 },
149 },
150 };
151
152 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
153 $config);
154
155 # Open pipes, for use between the parent and child processes. Specifically,
156 # the child will indicate when it's done with its test by writing a message
157 # to the parent.
158 my ($rfh, $wfh);
159 unless (pipe($rfh, $wfh)) {
160 die("Can't open pipe: $!");
161 }
162
163 my $ex;
164
165 # Fork child
166 $self->handle_sigchld();
167 defined(my $pid = fork()) or die("Can't fork: $!");
168 if ($pid) {
169 eval {
170 # Give the server a chance to start up
171 sleep(2);
172
173 # To test SSL session resumption, we use the command-line
174 # openssl s_client tool, rather than any Perl module.
175
176 # XXX Some OpenSSL versions' of s_client do not support the 'ftp'
177 # parameter for -starttls; in this case, point the openssl binary
178 # to be used to a version which does support this.
179 my $openssl = 'openssl';
180
181 my @cmd = (
182 $openssl,
183 's_client',
184 '-connect',
185 "127.0.0.1:$port",
186 '-starttls',
187 'ftp',
188 '-sess_out',
189 $sessid_file,
190 '-CAfile',
191 $ca_file,
192 '-noservername',
193 '-no_check_time',
194 );
195
196 my $tls_rh = IO::Handle->new();
197 my $tls_wh = IO::Handle->new();
198 my $tls_eh = IO::Handle->new();
199
200 $tls_wh->autoflush(1);
201
202 local $SIG{CHLD} = 'DEFAULT';
203
204 if ($ENV{TEST_VERBOSE}) {
205 print STDERR "Executing: ", join(' ', @cmd), "\n";
206 }
207
208 my $tls_pid = open3($tls_wh, $tls_rh, $tls_eh, @cmd);
209 print $tls_wh "QUIT\r\n";
210 waitpid($tls_pid, 0);
211
212 my ($res, $cipher_str, $err_str, $out_str);
213 if ($? >> 8) {
214 $err_str = join('', <$tls_eh>);
215 $res = 0;
216
217 } else {
218 my $output = [<$tls_rh>];
219
220 # Specifically look for the line containing 'Cipher is'
221 foreach my $line (@$output) {
222 if ($line =~ /Cipher is/) {
223 $cipher_str = $line;
224 chomp($cipher_str);
225 }
226 }
227
228 if ($ENV{TEST_VERBOSE}) {
229 $out_str = join('', @$output);
230 print STDERR "Stdout: $out_str\n";
231 }
232
233 if ($ENV{TEST_VERBOSE}) {
234 $err_str = join('', <$tls_eh>);
235 print STDERR "Stderr: $err_str\n";
236 }
237
238 $res = 1;
239 }
240
241 unless ($res) {
242 die("Can't talk to server: $err_str");
243 }
244
245 my $expected = '^New';
246 $self->assert(qr/$expected/, $cipher_str,
247 test_msg("Expected '$expected', got '$cipher_str'"));
248
249 @cmd = (
250 $openssl,
251 's_client',
252 '-connect',
253 "127.0.0.1:$port",
254 '-starttls',
255 'ftp',
256 '-sess_in',
257 $sessid_file,
258 '-CAfile',
259 $ca_file,
260 '-noservername',
261 '-no_check_time',
262 );
263
264 $tls_rh = IO::Handle->new();
265 $tls_wh = IO::Handle->new();
266 $tls_eh = IO::Handle->new();
267
268 $tls_wh->autoflush(1);
269
270 if ($ENV{TEST_VERBOSE}) {
271 print STDERR "Executing: ", join(' ', @cmd), "\n";
272 }
273
274 $tls_pid = open3($tls_wh, $tls_rh, $tls_eh, @cmd);
275 print $tls_wh "QUIT\r\n";
276 waitpid($tls_pid, 0);
277
278 $res = 0;
279 $cipher_str = undef;
280 $err_str = undef;
281 $out_str = undef;
282
283 if ($? >> 8) {
284 $err_str = join('', <$tls_eh>);
285 $res = 0;
286
287 } else {
288 my $output = [<$tls_rh>];
289
290 # Specifically look for the line containing 'Cipher is'
291 foreach my $line (@$output) {
292 if ($line =~ /Cipher is/) {
293 $cipher_str = $line;
294 chomp($cipher_str);
295 }
296 }
297
298 if ($ENV{TEST_VERBOSE}) {
299 $out_str = join('', @$output);
300 print STDERR "Stdout: $out_str\n";
301 }
302
303 if ($ENV{TEST_VERBOSE}) {
304 $err_str = join('', <$tls_eh>);
305 print STDERR "Stderr: $err_str\n";
306 }
307
308 $res = 1;
309 }
310
311 unless ($res) {
312 die("Can't talk to server: $err_str");
313 }
314
315 $expected = '^Reused';
316 $self->assert(qr/$expected/, $cipher_str,
317 test_msg("Expected '$expected', got '$cipher_str'"));
318 };
319 if ($@) {
320 $ex = $@;
321 }
322
323 $wfh->print("done\n");
324 $wfh->flush();
325
326 } else {
327 eval { server_wait($setup->{config_file}, $rfh, 45) };
328 if ($@) {
329 warn($@);
330 exit 1;
331 }
332
333 exit 0;
334 }
335
336 # Stop server
337 server_stop($setup->{pid_file});
338 $self->assert_child_ok($pid);
339
340 test_cleanup($setup->{log_file}, $ex);
341 }
342
343 sub tls_sess_cache_memcache_json_bug4057 {
344 my $self = shift;
345 my $tmpdir = $self->{tmpdir};
346 my $setup = test_setup($tmpdir, 'tls_memcache');
347
348 my $memcached_servers = $ENV{MEMCACHED_SERVERS} ? $ENV{MEMCACHED_SERVERS} : '127.0.0.1:11211';
349
350 my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
351 my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
352
353 my $sessid_file = File::Spec->rel2abs("$tmpdir/sessid.pem");
354
355 my $config = {
356 PidFile => $setup->{pid_file},
357 ScoreboardFile => $setup->{scoreboard_file},
358 SystemLog => $setup->{log_file},
359 TraceLog => $setup->{log_file},
360 Trace => 'tls:20 memcache:30 tls.memcache:20',
361
362 AuthUserFile => $setup->{auth_user_file},
363 AuthGroupFile => $setup->{auth_group_file},
364 AuthOrder => 'mod_auth_file.c',
365
366 IfModules => {
367 'mod_delay.c' => {
368 DelayEngine => 'off',
369 },
370
371 'mod_memcache.c' => {
372 MemcacheEngine => 'on',
373 MemcacheLog => $setup->{log_file},
374 MemcacheServers => $memcached_servers,
375 },
376
377 'mod_tls.c' => {
378 TLSEngine => 'on',
379 TLSLog => $setup->{log_file},
380 TLSRequired => 'on',
381 TLSRSACertificateFile => $cert_file,
382 TLSCACertificateFile => $ca_file,
383 TLSVerifyClient => 'off',
384 TLSOptions => 'EnableDiags',
385
386 # Necessary for session ID caching; TLSv1.3 uses session tickets
387 TLSProtocol => 'TLSv1.2',
388 },
389
390 'mod_tls_memcache.c' => {
391 TLSSessionCache => 'memcache:/json',
392 },
393 },
394 };
395
396 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
397 $config);
398
399 # Open pipes, for use between the parent and child processes. Specifically,
400 # the child will indicate when it's done with its test by writing a message
401 # to the parent.
402 my ($rfh, $wfh);
403 unless (pipe($rfh, $wfh)) {
404 die("Can't open pipe: $!");
405 }
406
407 my $ex;
408
409 # Fork child
410 $self->handle_sigchld();
411 defined(my $pid = fork()) or die("Can't fork: $!");
412 if ($pid) {
413 eval {
414 # Give the server a chance to start up
415 sleep(2);
416
417 # To test SSL session resumption, we use the command-line
418 # openssl s_client tool, rather than any Perl module.
419
420 # XXX Some OpenSSL versions' of s_client do not support the 'ftp'
421 # parameter for -starttls; in this case, point the openssl binary
422 # to be used to a version which does support this.
423 my $openssl = 'openssl';
424
425 my @cmd = (
426 $openssl,
427 's_client',
428 '-connect',
429 "127.0.0.1:$port",
430 '-starttls',
431 'ftp',
432 '-sess_out',
433 $sessid_file,
434 '-CAfile',
435 $ca_file,
436 '-noservername',
437 '-no_check_time',
438 );
439
440 my $tls_rh = IO::Handle->new();
441 my $tls_wh = IO::Handle->new();
442 my $tls_eh = IO::Handle->new();
443
444 $tls_wh->autoflush(1);
445
446 local $SIG{CHLD} = 'DEFAULT';
447
448 if ($ENV{TEST_VERBOSE}) {
449 print STDERR "Executing: ", join(' ', @cmd), "\n";
450 }
451
452 my $tls_pid = open3($tls_wh, $tls_rh, $tls_eh, @cmd);
453 print $tls_wh "QUIT\r\n";
454 waitpid($tls_pid, 0);
455
456 my ($res, $cipher_str, $err_str, $out_str);
457 if ($? >> 8) {
458 $err_str = join('', <$tls_eh>);
459 $res = 0;
460
461 } else {
462 my $output = [<$tls_rh>];
463
464 # Specifically look for the line containing 'Cipher is'
465 foreach my $line (@$output) {
466 if ($line =~ /Cipher is/) {
467 $cipher_str = $line;
468 chomp($cipher_str);
469 }
470 }
471
472 if ($ENV{TEST_VERBOSE}) {
473 $out_str = join('', @$output);
474 print STDERR "Stdout: $out_str\n";
475 }
476
477 if ($ENV{TEST_VERBOSE}) {
478 $err_str = join('', <$tls_eh>);
479 print STDERR "Stderr: $err_str\n";
480 }
481
482 $res = 1;
483 }
484
485 unless ($res) {
486 die("Can't talk to server: $err_str");
487 }
488
489 my $expected = '^New';
490 $self->assert(qr/$expected/, $cipher_str,
491 test_msg("Expected '$expected', got '$cipher_str'"));
492
493 @cmd = (
494 $openssl,
495 's_client',
496 '-connect',
497 "127.0.0.1:$port",
498 '-starttls',
499 'ftp',
500 '-sess_in',
501 $sessid_file,
502 '-CAfile',
503 $ca_file,
504 '-noservername',
505 '-no_check_time',
506 );
507
508 $tls_rh = IO::Handle->new();
509 $tls_wh = IO::Handle->new();
510 $tls_eh = IO::Handle->new();
511
512 $tls_wh->autoflush(1);
513
514 if ($ENV{TEST_VERBOSE}) {
515 print STDERR "Executing: ", join(' ', @cmd), "\n";
516 }
517
518 $tls_pid = open3($tls_wh, $tls_rh, $tls_eh, @cmd);
519 print $tls_wh "QUIT\r\n";
520 waitpid($tls_pid, 0);
521
522 $res = 0;
523 $cipher_str = undef;
524 $err_str = undef;
525 $out_str = undef;
526
527 if ($? >> 8) {
528 $err_str = join('', <$tls_eh>);
529 $res = 0;
530
531 } else {
532 my $output = [<$tls_rh>];
533
534 # Specifically look for the line containing 'Cipher is'
535 foreach my $line (@$output) {
536 if ($line =~ /Cipher is/) {
537 $cipher_str = $line;
538 chomp($cipher_str);
539 }
540 }
541
542 if ($ENV{TEST_VERBOSE}) {
543 $out_str = join('', @$output);
544 print STDERR "Stdout: $out_str\n";
545 }
546
547 if ($ENV{TEST_VERBOSE}) {
548 $err_str = join('', <$tls_eh>);
549 print STDERR "Stderr: $err_str\n";
550 }
551
552 $res = 1;
553 }
554
555 unless ($res) {
556 die("Can't talk to server: $err_str");
557 }
558
559 $expected = '^Reused';
560 $self->assert(qr/$expected/, $cipher_str,
561 test_msg("Expected '$expected', got '$cipher_str'"));
562 };
563 if ($@) {
564 $ex = $@;
565 }
566
567 $wfh->print("done\n");
568 $wfh->flush();
569
570 } else {
571 eval { server_wait($setup->{config_file}, $rfh, 45) };
572 if ($@) {
573 warn($@);
574 exit 1;
575 }
576
577 exit 0;
578 }
579
580 # Stop server
581 server_stop($setup->{pid_file});
582 $self->assert_child_ok($pid);
583
584 test_cleanup($setup->{log_file}, $ex);
585 }
586
587 sub starttls_ftp {
588 my $port = shift;
589 my $ssl_opts = shift;
590
591 my $client = IO::Socket::INET->new(
592 PeerHost => '127.0.0.1',
593 PeerPort => $port,
594 Proto => 'tcp',
595 Type => SOCK_STREAM,
596 Timeout => 10
597 );
598 unless ($client) {
599 croak("Can't connect to 127.0.0.1:$port: $!");
600 }
601
602 # Read the banner
603 my $banner = <$client>;
604 if ($ENV{TEST_VERBOSE}) {
605 print STDOUT "# Received banner: $banner\n";
606 }
607
608 # Send the AUTH command
609 my $cmd = "AUTH TLS\r\n";
610 if ($ENV{TEST_VERBOSE}) {
611 print STDOUT "# Sending command: $cmd";
612 }
613
614 $client->print($cmd);
615 $client->flush();
616
617 # Read the AUTH response
618 my $resp = <$client>;
619 if ($ENV{TEST_VERBOSE}) {
620 print STDOUT "# Received response: $resp\n";
621 }
622
623 my $expected = "234 AUTH TLS successful\r\n";
624 unless ($expected eq $resp) {
625 croak("Expected response '$expected', got '$resp'");
626 }
627
628 # Now perform the SSL handshake
629 if ($ENV{TEST_VERBOSE}) {
630 $IO::Socket::SSL::DEBUG = 3;
631 }
632
633 my $res = IO::Socket::SSL->start_SSL($client, $ssl_opts);
634 unless ($res) {
635 croak("Failed SSL handshake: " . IO::Socket::SSL::errstr());
636 }
637
638 $cmd = "QUIT\r\n";
639 if ($ENV{TEST_VERBOSE}) {
640 print STDOUT "# Sending command: $cmd";
641 }
642
643 print $client $cmd;
644 $client->flush();
645 $client->close();
646 }
647
648 sub tls_stapling_on_memcache_bug4175 {
649 my $self = shift;
650 my $tmpdir = $self->{tmpdir};
651 my $setup = test_setup($tmpdir, 'tls_memcache');
652
653 my $memcached_servers = $ENV{MEMCACHED_SERVERS} ? $ENV{MEMCACHED_SERVERS} : '127.0.0.1:11211';
654
655 my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
656 my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
657
658 my $config = {
659 PidFile => $setup->{pid_file},
660 ScoreboardFile => $setup->{scoreboard_file},
661 SystemLog => $setup->{log_file},
662 TraceLog => $setup->{log_file},
663 Trace => 'tls:20 tls.memcache:20',
664
665 AuthUserFile => $setup->{auth_user_file},
666 AuthGroupFile => $setup->{auth_group_file},
667 AuthOrder => 'mod_auth_file.c',
668
669 IfModules => {
670 'mod_delay.c' => {
671 DelayEngine => 'off',
672 },
673
674 'mod_memcache.c' => {
675 MemcacheEngine => 'on',
676 MemcacheLog => $setup->{log_file},
677 MemcacheServers => $memcached_servers,
678 },
679
680 'mod_tls.c' => {
681 TLSEngine => 'on',
682 TLSLog => $setup->{log_file},
683 TLSRequired => 'on',
684 TLSRSACertificateFile => $cert_file,
685 TLSCACertificateFile => $ca_file,
686 TLSOptions => 'EnableDiags',
687 TLSStapling => 'on',
688 TLSStaplingCache => "memcache:/",
689 },
690 },
691 };
692
693 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
694 $config);
695
696 # Open pipes, for use between the parent and child processes. Specifically,
697 # the child will indicate when it's done with its test by writing a message
698 # to the parent.
699 my ($rfh, $wfh);
700 unless (pipe($rfh, $wfh)) {
701 die("Can't open pipe: $!");
702 }
703
704 require IO::Socket::INET;
705 require IO::Socket::SSL;
706
707 my $ex;
708
709 # Fork child
710 $self->handle_sigchld();
711 defined(my $pid = fork()) or die("Can't fork: $!");
712 if ($pid) {
713 eval {
714 # Give the server a chance to start up
715 sleep(2);
716
717 # Manually simulate the STARTTLS protocol
718
719 my $ssl_opts = {
720 SSL_ocsp_mode => IO::Socket::SSL::SSL_OCSP_TRY_STAPLE(),
721 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
722 SSL_alpn_protocols => [qw(ftp)],
723 };
724
725 starttls_ftp($port, $ssl_opts);
726
727 my $delay = 5;
728 if ($delay > 0) {
729 if ($ENV{TEST_VERBOSE}) {
730 print STDOUT "# Sleeping for $delay seconds\n";
731 }
732
733 sleep($delay);
734 }
735
736 # Do it again, see if we actually read our our cached OCSP response
737 starttls_ftp($port, $ssl_opts);
738 };
739 if ($@) {
740 $ex = $@;
741 }
742
743 $wfh->print("done\n");
744 $wfh->flush();
745
746 } else {
747 eval { server_wait($setup->{config_file}, $rfh) };
748 if ($@) {
749 warn($@);
750 exit 1;
751 }
752
753 exit 0;
754 }
755
756 # Stop server
757 server_stop($setup->{pid_file});
758 $self->assert_child_ok($pid);
759
760 test_cleanup($setup->{log_file}, $ex);
761 }
762
763 1;