| 1 |
package ProFTPD::Tests::Modules::mod_tls_fscache; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use Carp; |
| 8 |
use File::Copy; |
| 9 |
use File::Path qw(mkpath); |
| 10 |
use File::Spec; |
| 11 |
use IO::Handle; |
| 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_stapling_on_fscache_bug4175 => { |
| 23 |
order => ++$order, |
| 24 |
test_class => [qw(bug forking mod_tls_fscache)], |
| 25 |
}, |
| 26 |
|
| 27 |
}; |
| 28 |
|
| 29 |
sub new { |
| 30 |
return shift()->SUPER::new(@_); |
| 31 |
} |
| 32 |
|
| 33 |
sub list_tests { |
| 34 |
# Check for the required Perl modules: |
| 35 |
# |
| 36 |
# Net-SSLeay |
| 37 |
# IO-Socket-SSL |
| 38 |
# Net-FTPSSL |
| 39 |
|
| 40 |
my $required = [qw( |
| 41 |
Net::SSLeay |
| 42 |
IO::Socket::SSL |
| 43 |
Net::FTPSSL |
| 44 |
)]; |
| 45 |
|
| 46 |
foreach my $req (@$required) { |
| 47 |
eval "use $req"; |
| 48 |
if ($@) { |
| 49 |
print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n"; |
| 50 |
|
| 51 |
if ($ENV{TEST_VERBOSE}) { |
| 52 |
print STDERR "Unable to load $req: $@\n"; |
| 53 |
} |
| 54 |
|
| 55 |
return qw(testsuite_empty_test); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return testsuite_get_runnable_tests($TESTS); |
| 60 |
} |
| 61 |
|
| 62 |
sub starttls_ftp { |
| 63 |
my $port = shift; |
| 64 |
my $ssl_opts = shift; |
| 65 |
|
| 66 |
my $client = IO::Socket::INET->new( |
| 67 |
PeerHost => '127.0.0.1', |
| 68 |
PeerPort => $port, |
| 69 |
Proto => 'tcp', |
| 70 |
Type => SOCK_STREAM, |
| 71 |
Timeout => 10 |
| 72 |
); |
| 73 |
unless ($client) { |
| 74 |
croak("Can't connect to 127.0.0.1:$port: $!"); |
| 75 |
} |
| 76 |
|
| 77 |
# Read the banner |
| 78 |
my $banner = <$client>; |
| 79 |
if ($ENV{TEST_VERBOSE}) { |
| 80 |
print STDOUT "# Received banner: $banner\n"; |
| 81 |
} |
| 82 |
|
| 83 |
# Send the AUTH command |
| 84 |
my $cmd = "AUTH TLS\r\n"; |
| 85 |
if ($ENV{TEST_VERBOSE}) { |
| 86 |
print STDOUT "# Sending command: $cmd"; |
| 87 |
} |
| 88 |
|
| 89 |
$client->print($cmd); |
| 90 |
$client->flush(); |
| 91 |
|
| 92 |
# Read the AUTH response |
| 93 |
my $resp = <$client>; |
| 94 |
if ($ENV{TEST_VERBOSE}) { |
| 95 |
print STDOUT "# Received response: $resp\n"; |
| 96 |
} |
| 97 |
|
| 98 |
my $expected = "234 AUTH TLS successful\r\n"; |
| 99 |
unless ($expected eq $resp) { |
| 100 |
croak("Expected response '$expected', got '$resp'"); |
| 101 |
} |
| 102 |
|
| 103 |
# Now perform the SSL handshake |
| 104 |
if ($ENV{TEST_VERBOSE}) { |
| 105 |
$IO::Socket::SSL::DEBUG = 3; |
| 106 |
} |
| 107 |
|
| 108 |
my $res = IO::Socket::SSL->start_SSL($client, $ssl_opts); |
| 109 |
unless ($res) { |
| 110 |
croak("Failed SSL handshake: " . IO::Socket::SSL::errstr()); |
| 111 |
} |
| 112 |
|
| 113 |
$cmd = "QUIT\r\n"; |
| 114 |
if ($ENV{TEST_VERBOSE}) { |
| 115 |
print STDOUT "# Sending command: $cmd"; |
| 116 |
} |
| 117 |
|
| 118 |
print $client $cmd; |
| 119 |
$client->flush(); |
| 120 |
$client->close(); |
| 121 |
} |
| 122 |
|
| 123 |
sub tls_stapling_on_fscache_bug4175 { |
| 124 |
my $self = shift; |
| 125 |
my $tmpdir = $self->{tmpdir}; |
| 126 |
my $setup = test_setup($tmpdir, 'tls_fscache'); |
| 127 |
|
| 128 |
my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem'); |
| 129 |
my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem'); |
| 130 |
|
| 131 |
my $cache_tab = File::Spec->rel2abs("$tmpdir/var/tls/cache/ocsp"); |
| 132 |
mkpath($cache_tab); |
| 133 |
|
| 134 |
my $config = { |
| 135 |
PidFile => $setup->{pid_file}, |
| 136 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 137 |
SystemLog => $setup->{log_file}, |
| 138 |
TraceLog => $setup->{log_file}, |
| 139 |
Trace => 'tls:20 tls.fscache:20', |
| 140 |
|
| 141 |
AuthUserFile => $setup->{auth_user_file}, |
| 142 |
AuthGroupFile => $setup->{auth_group_file}, |
| 143 |
|
| 144 |
IfModules => { |
| 145 |
'mod_delay.c' => { |
| 146 |
DelayEngine => 'off', |
| 147 |
}, |
| 148 |
|
| 149 |
'mod_tls.c' => { |
| 150 |
TLSEngine => 'on', |
| 151 |
TLSLog => $setup->{log_file}, |
| 152 |
TLSRequired => 'on', |
| 153 |
TLSRSACertificateFile => $cert_file, |
| 154 |
TLSCACertificateFile => $ca_file, |
| 155 |
TLSOptions => 'EnableDiags', |
| 156 |
TLSStapling => 'on', |
| 157 |
TLSStaplingCache => "fs:/path=$cache_tab", |
| 158 |
}, |
| 159 |
}, |
| 160 |
}; |
| 161 |
|
| 162 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 163 |
$config); |
| 164 |
|
| 165 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 166 |
# the child will indicate when it's done with its test by writing a message |
| 167 |
# to the parent. |
| 168 |
my ($rfh, $wfh); |
| 169 |
unless (pipe($rfh, $wfh)) { |
| 170 |
die("Can't open pipe: $!"); |
| 171 |
} |
| 172 |
|
| 173 |
require IO::Socket::INET; |
| 174 |
require IO::Socket::SSL; |
| 175 |
|
| 176 |
my $ex; |
| 177 |
|
| 178 |
# Fork child |
| 179 |
$self->handle_sigchld(); |
| 180 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 181 |
if ($pid) { |
| 182 |
eval { |
| 183 |
# Give the server a chance to start up |
| 184 |
sleep(2); |
| 185 |
|
| 186 |
# Manually simulate the STARTTLS protocol |
| 187 |
|
| 188 |
my $ssl_opts = { |
| 189 |
SSL_ocsp_mode => IO::Socket::SSL::SSL_OCSP_TRY_STAPLE(), |
| 190 |
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(), |
| 191 |
SSL_alpn_protocols => [qw(ftp)], |
| 192 |
}; |
| 193 |
|
| 194 |
starttls_ftp($port, $ssl_opts); |
| 195 |
|
| 196 |
my $delay = 5; |
| 197 |
if ($delay > 0) { |
| 198 |
if ($ENV{TEST_VERBOSE}) { |
| 199 |
print STDOUT "# Sleeping for $delay seconds\n"; |
| 200 |
} |
| 201 |
|
| 202 |
sleep($delay); |
| 203 |
} |
| 204 |
|
| 205 |
# Do it again, see if we actually read our our cached OCSP response |
| 206 |
starttls_ftp($port, $ssl_opts); |
| 207 |
}; |
| 208 |
|
| 209 |
if ($@) { |
| 210 |
$ex = $@; |
| 211 |
} |
| 212 |
|
| 213 |
$wfh->print("done\n"); |
| 214 |
$wfh->flush(); |
| 215 |
|
| 216 |
} else { |
| 217 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 218 |
if ($@) { |
| 219 |
warn($@); |
| 220 |
exit 1; |
| 221 |
} |
| 222 |
|
| 223 |
exit 0; |
| 224 |
} |
| 225 |
|
| 226 |
# Stop server |
| 227 |
server_stop($setup->{pid_file}); |
| 228 |
|
| 229 |
$self->assert_child_ok($pid); |
| 230 |
|
| 231 |
test_cleanup($setup->{log_file}, $ex); |
| 232 |
} |
| 233 |
|
| 234 |
1; |