| 1 |
package ProFTPD::Tests::Config::SyslogLevel; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use File::Path qw(mkpath); |
| 8 |
use File::Spec; |
| 9 |
use IO::Handle; |
| 10 |
|
| 11 |
use ProFTPD::TestSuite::FTP; |
| 12 |
use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite); |
| 13 |
|
| 14 |
$| = 1; |
| 15 |
|
| 16 |
my $order = 0; |
| 17 |
|
| 18 |
my $TESTS = { |
| 19 |
sysloglevel_debug => { |
| 20 |
order => ++$order, |
| 21 |
test_class => [qw(forking)], |
| 22 |
}, |
| 23 |
|
| 24 |
sysloglevel_debug_sftp => { |
| 25 |
order => ++$order, |
| 26 |
test_class => [qw(forking mod_sftp)], |
| 27 |
}, |
| 28 |
|
| 29 |
}; |
| 30 |
|
| 31 |
sub new { |
| 32 |
return shift()->SUPER::new(@_); |
| 33 |
} |
| 34 |
|
| 35 |
sub list_tests { |
| 36 |
return testsuite_get_runnable_tests($TESTS); |
| 37 |
} |
| 38 |
|
| 39 |
sub set_up { |
| 40 |
my $self = shift; |
| 41 |
$self->SUPER::set_up(@_); |
| 42 |
|
| 43 |
# Make sure that mod_sftp does not complain about permissions on the hostkey |
| 44 |
# files. |
| 45 |
|
| 46 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 47 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 48 |
|
| 49 |
unless (chmod(0400, $rsa_host_key, $dsa_host_key)) { |
| 50 |
die("Can't set perms on $rsa_host_key, $dsa_host_key: $!"); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
sub sysloglevel_debug { |
| 55 |
my $self = shift; |
| 56 |
my $tmpdir = $self->{tmpdir}; |
| 57 |
my $setup = test_setup($tmpdir, 'config'); |
| 58 |
|
| 59 |
my $config = [ |
| 60 |
"PidFile $setup->{pid_file}", |
| 61 |
"ScoreboardFile $setup->{scoreboard_file}", |
| 62 |
"SystemLog $setup->{log_file}", |
| 63 |
"SyslogLevel debug", |
| 64 |
"DebugLevel 10", |
| 65 |
"AuthUserFile $setup->{auth_user_file}", |
| 66 |
"AuthGroupFile $setup->{auth_group_file}", |
| 67 |
"AuthOrder mod_auth_file.c", |
| 68 |
]; |
| 69 |
|
| 70 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 71 |
$config); |
| 72 |
|
| 73 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 74 |
# the child will indicate when it's done with its test by writing a message |
| 75 |
# to the parent. |
| 76 |
my ($rfh, $wfh); |
| 77 |
unless (pipe($rfh, $wfh)) { |
| 78 |
die("Can't open pipe: $!"); |
| 79 |
} |
| 80 |
|
| 81 |
my $ex; |
| 82 |
|
| 83 |
# Fork child |
| 84 |
$self->handle_sigchld(); |
| 85 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 86 |
if ($pid) { |
| 87 |
eval { |
| 88 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 89 |
$client->login($setup->{user}, $setup->{passwd}); |
| 90 |
$client->quit(); |
| 91 |
}; |
| 92 |
|
| 93 |
if ($@) { |
| 94 |
$ex = $@; |
| 95 |
} |
| 96 |
|
| 97 |
$wfh->print("done\n"); |
| 98 |
$wfh->flush(); |
| 99 |
|
| 100 |
} else { |
| 101 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 102 |
if ($@) { |
| 103 |
warn($@); |
| 104 |
exit 1; |
| 105 |
} |
| 106 |
|
| 107 |
exit 0; |
| 108 |
} |
| 109 |
|
| 110 |
# Stop server |
| 111 |
server_stop($setup->{pid_file}); |
| 112 |
$self->assert_child_ok($pid); |
| 113 |
|
| 114 |
if ($ex) { |
| 115 |
test_cleanup($setup->{log_file}, $ex); |
| 116 |
} |
| 117 |
|
| 118 |
eval { |
| 119 |
if (open(my $fh, "< $setup->{log_file}")) { |
| 120 |
my $ok = 0; |
| 121 |
|
| 122 |
while (my $line = <$fh>) { |
| 123 |
chomp($line); |
| 124 |
|
| 125 |
if ($line =~ /dispatching PRE_CMD/) { |
| 126 |
$ok = 1; |
| 127 |
last; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
close($fh); |
| 132 |
|
| 133 |
$self->assert($ok, "Did not see expected debug messages in SystemLog"); |
| 134 |
|
| 135 |
} else { |
| 136 |
die("Can't read SystemLog $setup->{log_file}: $!"); |
| 137 |
} |
| 138 |
}; |
| 139 |
if ($@) { |
| 140 |
$ex = $@; |
| 141 |
} |
| 142 |
|
| 143 |
test_cleanup($setup->{log_file}, $ex); |
| 144 |
} |
| 145 |
|
| 146 |
sub sysloglevel_debug_sftp { |
| 147 |
my $self = shift; |
| 148 |
my $tmpdir = $self->{tmpdir}; |
| 149 |
my $setup = test_setup($tmpdir, 'config'); |
| 150 |
|
| 151 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 152 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 153 |
|
| 154 |
my $config = { |
| 155 |
PidFile => $setup->{pid_file}, |
| 156 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 157 |
SystemLog => $setup->{log_file}, |
| 158 |
SyslogLevel => 'debug', |
| 159 |
DebugLevel => '10', |
| 160 |
|
| 161 |
AuthUserFile => $setup->{auth_user_file}, |
| 162 |
AuthGroupFile => $setup->{auth_group_file}, |
| 163 |
AuthOrder => 'mod_auth_file.c', |
| 164 |
|
| 165 |
IfModules => { |
| 166 |
'mod_delay.c' => { |
| 167 |
DelayEngine => 'off', |
| 168 |
}, |
| 169 |
|
| 170 |
'mod_sftp.c' => [ |
| 171 |
"SFTPEngine on", |
| 172 |
"SFTPLog $setup->{log_file}", |
| 173 |
"SFTPHostKey $rsa_host_key", |
| 174 |
"SFTPHostKey $dsa_host_key", |
| 175 |
], |
| 176 |
}, |
| 177 |
}; |
| 178 |
|
| 179 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 180 |
$config); |
| 181 |
|
| 182 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 183 |
# the child will indicate when it's done with its test by writing a message |
| 184 |
# to the parent. |
| 185 |
my ($rfh, $wfh); |
| 186 |
unless (pipe($rfh, $wfh)) { |
| 187 |
die("Can't open pipe: $!"); |
| 188 |
} |
| 189 |
|
| 190 |
require Net::SSH2; |
| 191 |
my $ex; |
| 192 |
|
| 193 |
# Fork child |
| 194 |
$self->handle_sigchld(); |
| 195 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 196 |
if ($pid) { |
| 197 |
eval { |
| 198 |
sleep(1); |
| 199 |
|
| 200 |
my $ssh2 = Net::SSH2->new(); |
| 201 |
|
| 202 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 203 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 204 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 205 |
} |
| 206 |
|
| 207 |
unless ($ssh2->auth_password($setup->{user}, $setup->{passwd})) { |
| 208 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 209 |
die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 210 |
} |
| 211 |
|
| 212 |
my $sftp = $ssh2->sftp(); |
| 213 |
unless ($sftp) { |
| 214 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 215 |
die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str"); |
| 216 |
} |
| 217 |
|
| 218 |
$sftp = undef; |
| 219 |
$ssh2->disconnect(); |
| 220 |
}; |
| 221 |
|
| 222 |
if ($@) { |
| 223 |
$ex = $@; |
| 224 |
} |
| 225 |
|
| 226 |
$wfh->print("done\n"); |
| 227 |
$wfh->flush(); |
| 228 |
|
| 229 |
} else { |
| 230 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 231 |
if ($@) { |
| 232 |
warn($@); |
| 233 |
exit 1; |
| 234 |
} |
| 235 |
|
| 236 |
exit 0; |
| 237 |
} |
| 238 |
|
| 239 |
# Stop server |
| 240 |
server_stop($setup->{pid_file}); |
| 241 |
$self->assert_child_ok($pid); |
| 242 |
|
| 243 |
if ($ex) { |
| 244 |
test_cleanup($setup->{log_file}, $ex); |
| 245 |
} |
| 246 |
|
| 247 |
eval { |
| 248 |
if (open(my $fh, "< $setup->{log_file}")) { |
| 249 |
my $ok = 0; |
| 250 |
|
| 251 |
while (my $line = <$fh>) { |
| 252 |
chomp($line); |
| 253 |
|
| 254 |
if ($line =~ /dispatching LOG_CMD.*?KEXINIT/) { |
| 255 |
$ok = 1; |
| 256 |
last; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
close($fh); |
| 261 |
|
| 262 |
$self->assert($ok, "Did not see expected debug messages in SystemLog"); |
| 263 |
|
| 264 |
} else { |
| 265 |
die("Can't read SystemLog $setup->{log_file}: $!"); |
| 266 |
} |
| 267 |
}; |
| 268 |
if ($@) { |
| 269 |
$ex = $@; |
| 270 |
} |
| 271 |
|
| 272 |
test_cleanup($setup->{log_file}, $ex); |
| 273 |
} |
| 274 |
|
| 275 |
1; |