| 1 |
package ProFTPD::Tests::Modules::mod_sftp::wrap2; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use File::Copy; |
| 8 |
use File::Path qw(mkpath rmtree); |
| 9 |
use File::Spec; |
| 10 |
use IO::Handle; |
| 11 |
use IPC::Open3; |
| 12 |
use POSIX qw(:fcntl_h); |
| 13 |
use Socket; |
| 14 |
|
| 15 |
use ProFTPD::TestSuite::FTP; |
| 16 |
use ProFTPD::TestSuite::Utils qw(:auth :config :features :running :test :testsuite); |
| 17 |
|
| 18 |
$| = 1; |
| 19 |
|
| 20 |
my $order = 0; |
| 21 |
|
| 22 |
my $TESTS = { |
| 23 |
sftp_wrap2_file_login => { |
| 24 |
order => ++$order, |
| 25 |
test_class => [qw(bug forking mod_wrap2_file sftp ssh2)], |
| 26 |
}, |
| 27 |
|
| 28 |
sftp_wrap2_sql_login => { |
| 29 |
order => ++$order, |
| 30 |
test_class => [qw(bug forking mod_sql_sqlite mod_wrap2_sql sftp ssh2)], |
| 31 |
}, |
| 32 |
|
| 33 |
sftp_wrap2_deny_msg_on_connect_bug3670 => { |
| 34 |
order => ++$order, |
| 35 |
test_class => [qw(bug forking mod_wrap2_file sftp ssh2)], |
| 36 |
}, |
| 37 |
|
| 38 |
sftp_wrap2_deny_msg_on_auth_bug3670 => { |
| 39 |
order => ++$order, |
| 40 |
test_class => [qw(bug forking mod_wrap2_file sftp ssh2)], |
| 41 |
}, |
| 42 |
|
| 43 |
sftp_wrap2_deny_msg_var_u => { |
| 44 |
order => ++$order, |
| 45 |
test_class => [qw(bug forking mod_wrap2_file sftp ssh2)], |
| 46 |
}, |
| 47 |
|
| 48 |
}; |
| 49 |
|
| 50 |
sub new { |
| 51 |
return shift()->SUPER::new(@_); |
| 52 |
} |
| 53 |
|
| 54 |
sub list_tests { |
| 55 |
# Check for the required Perl modules: |
| 56 |
# |
| 57 |
# Net-SSH2 |
| 58 |
# Net-SSH2-SFTP |
| 59 |
|
| 60 |
my $required = [qw( |
| 61 |
Net::SSH2 |
| 62 |
Net::SSH2::SFTP |
| 63 |
)]; |
| 64 |
|
| 65 |
foreach my $req (@$required) { |
| 66 |
eval "use $req"; |
| 67 |
if ($@) { |
| 68 |
print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n"; |
| 69 |
|
| 70 |
if ($ENV{TEST_VERBOSE}) { |
| 71 |
print STDERR "Unable to load $req: $@\n"; |
| 72 |
} |
| 73 |
|
| 74 |
return qw(testsuite_empty_test); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return testsuite_get_runnable_tests($TESTS); |
| 79 |
} |
| 80 |
|
| 81 |
sub set_up { |
| 82 |
my $self = shift; |
| 83 |
$self->SUPER::set_up(@_); |
| 84 |
|
| 85 |
# Make sure that mod_sftp does not complain about permissions on the hostkey |
| 86 |
# files. |
| 87 |
|
| 88 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 89 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 90 |
|
| 91 |
unless (chmod(0400, $rsa_host_key, $dsa_host_key)) { |
| 92 |
die("Can't set perms on $rsa_host_key, $dsa_host_key: $!"); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
sub sftp_wrap2_file_login { |
| 97 |
my $self = shift; |
| 98 |
my $tmpdir = $self->{tmpdir}; |
| 99 |
|
| 100 |
my $config_file = "$tmpdir/sftp.conf"; |
| 101 |
my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid"); |
| 102 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard"); |
| 103 |
|
| 104 |
my $log_file = test_get_logfile(); |
| 105 |
|
| 106 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd"); |
| 107 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group"); |
| 108 |
|
| 109 |
my $fh; |
| 110 |
my $allow_file = File::Spec->rel2abs("$tmpdir/sftp.allow"); |
| 111 |
if (open($fh, "> $allow_file")) { |
| 112 |
print $fh "proftpd: ALL\n"; |
| 113 |
|
| 114 |
unless (close($fh)) { |
| 115 |
die("Can't write $allow_file: $!"); |
| 116 |
} |
| 117 |
|
| 118 |
} else { |
| 119 |
die("Can't open $allow_file: $!"); |
| 120 |
} |
| 121 |
|
| 122 |
my $deny_file = File::Spec->rel2abs("$tmpdir/sftp.deny"); |
| 123 |
if (open($fh, "> $deny_file")) { |
| 124 |
print $fh "ALL: ALL\n"; |
| 125 |
|
| 126 |
unless (close($fh)) { |
| 127 |
die("Can't write $deny_file: $!"); |
| 128 |
} |
| 129 |
|
| 130 |
} else { |
| 131 |
die("Can't open $deny_file: $!"); |
| 132 |
} |
| 133 |
|
| 134 |
my $user = 'proftpd'; |
| 135 |
my $passwd = 'test'; |
| 136 |
my $group = 'ftpd'; |
| 137 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 138 |
my $uid = 500; |
| 139 |
my $gid = 500; |
| 140 |
|
| 141 |
# Make sure that, if we're running as root, that the home directory has |
| 142 |
# permissions/privs set for the account we create |
| 143 |
if ($< == 0) { |
| 144 |
unless (chmod(0755, $home_dir)) { |
| 145 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 146 |
} |
| 147 |
|
| 148 |
unless (chown($uid, $gid, $home_dir)) { |
| 149 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 154 |
'/bin/bash'); |
| 155 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 156 |
|
| 157 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 158 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 159 |
|
| 160 |
my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key'); |
| 161 |
my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub'); |
| 162 |
|
| 163 |
my $config = { |
| 164 |
PidFile => $pid_file, |
| 165 |
ScoreboardFile => $scoreboard_file, |
| 166 |
SystemLog => $log_file, |
| 167 |
TraceLog => $log_file, |
| 168 |
Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20', |
| 169 |
|
| 170 |
AuthUserFile => $auth_user_file, |
| 171 |
AuthGroupFile => $auth_group_file, |
| 172 |
AuthOrder => 'mod_auth_file.c', |
| 173 |
|
| 174 |
IfModules => { |
| 175 |
'mod_delay.c' => { |
| 176 |
DelayEngine => 'off', |
| 177 |
}, |
| 178 |
|
| 179 |
'mod_sftp.c' => [ |
| 180 |
"SFTPEngine on", |
| 181 |
"SFTPLog $log_file", |
| 182 |
"SFTPHostKey $rsa_host_key", |
| 183 |
"SFTPHostKey $dsa_host_key", |
| 184 |
], |
| 185 |
|
| 186 |
'mod_wrap2.c' => { |
| 187 |
WrapEngine => 'on', |
| 188 |
WrapLog => $log_file, |
| 189 |
WrapTables => "file:$allow_file file:$deny_file", |
| 190 |
WrapDenyMsg => "User '%u' denied by access rules", |
| 191 |
WrapAllowMsg => "User '%u' allowed by access rules", |
| 192 |
}, |
| 193 |
}, |
| 194 |
}; |
| 195 |
|
| 196 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 197 |
|
| 198 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 199 |
# the child will indicate when it's done with its test by writing a message |
| 200 |
# to the parent. |
| 201 |
my ($rfh, $wfh); |
| 202 |
unless (pipe($rfh, $wfh)) { |
| 203 |
die("Can't open pipe: $!"); |
| 204 |
} |
| 205 |
|
| 206 |
require Net::SSH2; |
| 207 |
|
| 208 |
my $ex; |
| 209 |
|
| 210 |
# Fork child |
| 211 |
$self->handle_sigchld(); |
| 212 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 213 |
if ($pid) { |
| 214 |
eval { |
| 215 |
my $ssh2 = Net::SSH2->new(); |
| 216 |
|
| 217 |
sleep(1); |
| 218 |
|
| 219 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 220 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 221 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 222 |
} |
| 223 |
|
| 224 |
if ($ssh2->auth_publickey($user, $rsa_pub_key, $rsa_priv_key)) { |
| 225 |
die("Publickey auth succeeded unexpectedly"); |
| 226 |
} |
| 227 |
|
| 228 |
unless ($ssh2->auth_password($user, $passwd)) { |
| 229 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 230 |
die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 231 |
} |
| 232 |
|
| 233 |
my $sftp = $ssh2->sftp(); |
| 234 |
unless ($sftp) { |
| 235 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 236 |
die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str"); |
| 237 |
} |
| 238 |
|
| 239 |
$sftp = undef; |
| 240 |
$ssh2->disconnect(); |
| 241 |
}; |
| 242 |
|
| 243 |
if ($@) { |
| 244 |
$ex = $@; |
| 245 |
} |
| 246 |
|
| 247 |
$wfh->print("done\n"); |
| 248 |
$wfh->flush(); |
| 249 |
|
| 250 |
} else { |
| 251 |
eval { server_wait($config_file, $rfh) }; |
| 252 |
if ($@) { |
| 253 |
warn($@); |
| 254 |
exit 1; |
| 255 |
} |
| 256 |
|
| 257 |
exit 0; |
| 258 |
} |
| 259 |
|
| 260 |
# Stop server |
| 261 |
server_stop($pid_file); |
| 262 |
|
| 263 |
$self->assert_child_ok($pid); |
| 264 |
|
| 265 |
if ($ex) { |
| 266 |
test_append_logfile($log_file, $ex); |
| 267 |
unlink($log_file); |
| 268 |
|
| 269 |
die($ex); |
| 270 |
} |
| 271 |
|
| 272 |
unlink($log_file); |
| 273 |
} |
| 274 |
|
| 275 |
sub sftp_wrap2_sql_login { |
| 276 |
my $self = shift; |
| 277 |
my $tmpdir = $self->{tmpdir}; |
| 278 |
|
| 279 |
my $config_file = "$tmpdir/sftp.conf"; |
| 280 |
my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid"); |
| 281 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard"); |
| 282 |
|
| 283 |
my $log_file = test_get_logfile(); |
| 284 |
|
| 285 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/wrap2.passwd"); |
| 286 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/wrap2.group"); |
| 287 |
|
| 288 |
my $user = 'proftpd'; |
| 289 |
my $passwd = 'test'; |
| 290 |
my $group = 'ftpd'; |
| 291 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 292 |
my $uid = 500; |
| 293 |
my $gid = 500; |
| 294 |
|
| 295 |
# Make sure that, if we're running as root, that the home directory has |
| 296 |
# permissions/privs set for the account we create |
| 297 |
if ($< == 0) { |
| 298 |
unless (chmod(0755, $home_dir)) { |
| 299 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 300 |
} |
| 301 |
|
| 302 |
unless (chown($uid, $gid, $home_dir)) { |
| 303 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 308 |
'/bin/bash'); |
| 309 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 310 |
|
| 311 |
my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db"); |
| 312 |
|
| 313 |
# Build up sqlite3 command to create allow, deny tables and populate them |
| 314 |
my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql"); |
| 315 |
|
| 316 |
my $fh; |
| 317 |
if (open($fh, "> $db_script")) { |
| 318 |
print $fh <<EOS; |
| 319 |
CREATE TABLE ftpallow ( |
| 320 |
name TEXT, |
| 321 |
allowed TEXT |
| 322 |
); |
| 323 |
INSERT INTO ftpallow (name, allowed) VALUES ('$user', 'ALL'); |
| 324 |
|
| 325 |
CREATE TABLE ftpdeny ( |
| 326 |
name TEXT, |
| 327 |
denied TEXT |
| 328 |
); |
| 329 |
|
| 330 |
EOS |
| 331 |
|
| 332 |
unless (close($fh)) { |
| 333 |
die("Can't write $db_script: $!"); |
| 334 |
} |
| 335 |
|
| 336 |
} else { |
| 337 |
die("Can't open $db_script: $!"); |
| 338 |
} |
| 339 |
|
| 340 |
my $cmd = "sqlite3 $db_file < $db_script"; |
| 341 |
|
| 342 |
if ($ENV{TEST_VERBOSE}) { |
| 343 |
print STDERR "Executing sqlite3: $cmd\n"; |
| 344 |
} |
| 345 |
|
| 346 |
my @output = `$cmd`; |
| 347 |
if (scalar(@output) > 0 && |
| 348 |
$ENV{TEST_VERBOSE}) { |
| 349 |
print STDERR "Output: ", join('', @output), "\n"; |
| 350 |
} |
| 351 |
|
| 352 |
unlink($db_script); |
| 353 |
|
| 354 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 355 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 356 |
|
| 357 |
my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key'); |
| 358 |
my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub'); |
| 359 |
|
| 360 |
my $config = { |
| 361 |
PidFile => $pid_file, |
| 362 |
ScoreboardFile => $scoreboard_file, |
| 363 |
SystemLog => $log_file, |
| 364 |
TraceLog => $log_file, |
| 365 |
Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20', |
| 366 |
|
| 367 |
AuthUserFile => $auth_user_file, |
| 368 |
AuthGroupFile => $auth_group_file, |
| 369 |
AuthOrder => 'mod_auth_file.c', |
| 370 |
|
| 371 |
IfModules => { |
| 372 |
'mod_delay.c' => { |
| 373 |
DelayEngine => 'off', |
| 374 |
}, |
| 375 |
|
| 376 |
'mod_sftp.c' => [ |
| 377 |
"SFTPEngine on", |
| 378 |
"SFTPLog $log_file", |
| 379 |
"SFTPHostKey $rsa_host_key", |
| 380 |
"SFTPHostKey $dsa_host_key", |
| 381 |
], |
| 382 |
|
| 383 |
'mod_sql.c' => [ |
| 384 |
'SQLAuthenticate off', |
| 385 |
"SQLConnectInfo $db_file", |
| 386 |
'SQLNamedQuery get-allowed-clients SELECT "allowed FROM ftpallow WHERE name = \'%u\'"', |
| 387 |
'SQLNamedQuery get-denied-clients SELECT "denied FROM ftpdeny WHERE name = \'%u\'"', |
| 388 |
"SQLLogFile $log_file", |
| 389 |
], |
| 390 |
|
| 391 |
'mod_wrap2.c' => { |
| 392 |
WrapEngine => 'on', |
| 393 |
WrapLog => $log_file, |
| 394 |
WrapTables => "sql:/get-allowed-clients sql:/get-denied-clients", |
| 395 |
WrapDenyMsg => "User '%u' denied by access rules", |
| 396 |
WrapAllowMsg => "User '%u' allowed by access rules", |
| 397 |
}, |
| 398 |
}, |
| 399 |
}; |
| 400 |
|
| 401 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 402 |
|
| 403 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 404 |
# the child will indicate when it's done with its test by writing a message |
| 405 |
# to the parent. |
| 406 |
my ($rfh, $wfh); |
| 407 |
unless (pipe($rfh, $wfh)) { |
| 408 |
die("Can't open pipe: $!"); |
| 409 |
} |
| 410 |
|
| 411 |
require Net::SSH2; |
| 412 |
|
| 413 |
my $ex; |
| 414 |
|
| 415 |
# Fork child |
| 416 |
$self->handle_sigchld(); |
| 417 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 418 |
if ($pid) { |
| 419 |
eval { |
| 420 |
my $ssh2 = Net::SSH2->new(); |
| 421 |
|
| 422 |
sleep(1); |
| 423 |
|
| 424 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 425 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 426 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 427 |
} |
| 428 |
|
| 429 |
if ($ssh2->auth_publickey($user, $rsa_pub_key, $rsa_priv_key)) { |
| 430 |
die("Publickey auth succeeded unexpectedly"); |
| 431 |
} |
| 432 |
|
| 433 |
unless ($ssh2->auth_password($user, $passwd)) { |
| 434 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 435 |
die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 436 |
} |
| 437 |
|
| 438 |
my $sftp = $ssh2->sftp(); |
| 439 |
unless ($sftp) { |
| 440 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 441 |
die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str"); |
| 442 |
} |
| 443 |
|
| 444 |
$sftp = undef; |
| 445 |
$ssh2->disconnect(); |
| 446 |
}; |
| 447 |
|
| 448 |
if ($@) { |
| 449 |
$ex = $@; |
| 450 |
} |
| 451 |
|
| 452 |
$wfh->print("done\n"); |
| 453 |
$wfh->flush(); |
| 454 |
|
| 455 |
} else { |
| 456 |
eval { server_wait($config_file, $rfh) }; |
| 457 |
if ($@) { |
| 458 |
warn($@); |
| 459 |
exit 1; |
| 460 |
} |
| 461 |
|
| 462 |
exit 0; |
| 463 |
} |
| 464 |
|
| 465 |
# Stop server |
| 466 |
server_stop($pid_file); |
| 467 |
|
| 468 |
$self->assert_child_ok($pid); |
| 469 |
|
| 470 |
if ($ex) { |
| 471 |
test_append_logfile($log_file, $ex); |
| 472 |
unlink($log_file); |
| 473 |
|
| 474 |
die($ex); |
| 475 |
} |
| 476 |
|
| 477 |
unlink($log_file); |
| 478 |
} |
| 479 |
|
| 480 |
sub sftp_wrap2_deny_msg_on_connect_bug3670 { |
| 481 |
my $self = shift; |
| 482 |
my $tmpdir = $self->{tmpdir}; |
| 483 |
|
| 484 |
my $config_file = "$tmpdir/sftp.conf"; |
| 485 |
my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid"); |
| 486 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard"); |
| 487 |
|
| 488 |
my $log_file = test_get_logfile(); |
| 489 |
|
| 490 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd"); |
| 491 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group"); |
| 492 |
|
| 493 |
my $fh; |
| 494 |
my $allow_file = File::Spec->rel2abs("$tmpdir/sftp.allow"); |
| 495 |
if (open($fh, "> $allow_file")) { |
| 496 |
close($fh); |
| 497 |
|
| 498 |
} else { |
| 499 |
die("Can't open $allow_file: $!"); |
| 500 |
} |
| 501 |
|
| 502 |
my $deny_file = File::Spec->rel2abs("$tmpdir/sftp.deny"); |
| 503 |
if (open($fh, "> $deny_file")) { |
| 504 |
print $fh "ALL: ALL\n"; |
| 505 |
|
| 506 |
unless (close($fh)) { |
| 507 |
die("Can't write $deny_file: $!"); |
| 508 |
} |
| 509 |
|
| 510 |
} else { |
| 511 |
die("Can't open $deny_file: $!"); |
| 512 |
} |
| 513 |
|
| 514 |
my $user = 'proftpd'; |
| 515 |
my $passwd = 'test'; |
| 516 |
my $group = 'ftpd'; |
| 517 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 518 |
my $uid = 500; |
| 519 |
my $gid = 500; |
| 520 |
|
| 521 |
# Make sure that, if we're running as root, that the home directory has |
| 522 |
# permissions/privs set for the account we create |
| 523 |
if ($< == 0) { |
| 524 |
unless (chmod(0755, $home_dir)) { |
| 525 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 526 |
} |
| 527 |
|
| 528 |
unless (chown($uid, $gid, $home_dir)) { |
| 529 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 534 |
'/bin/bash'); |
| 535 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 536 |
|
| 537 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 538 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 539 |
|
| 540 |
my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key'); |
| 541 |
my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub'); |
| 542 |
|
| 543 |
my $deny_msg = '"SFTP denied by access rules"'; |
| 544 |
|
| 545 |
# Note: This test only passes correctly IFF mod_sftp is loaded BEFORE |
| 546 |
# mod_wrap2, not after. This means that mod_sftp needs to appear AFTER |
| 547 |
# mod_wrap2 in the --with-modules list, or appear BEFORE mod_wrap2 in |
| 548 |
# a list of LoadModule directives. |
| 549 |
# |
| 550 |
# mod_sftp's sess_init() callback is where the 'SSH2' protocol is set |
| 551 |
# for the session. The event listener for mod_wrap2's "disconnect this |
| 552 |
# client on connect because it isn't allowed by the access rules" has |
| 553 |
# a condition that checks for whether the session is an SSH2 session or |
| 554 |
# not (lest it trigger improperly for an FTP session). So if mod_wrap2's |
| 555 |
# sess_init() callback triggers first, it will disconnect the session |
| 556 |
# (and generate the event) before mod_sftp's sess_init() callback has had |
| 557 |
# a chance to set the "SSH2" bit for the session, then mod_sftp won't |
| 558 |
# properly send the WrapDenyMsg to the SSH2 client. |
| 559 |
|
| 560 |
my $config = { |
| 561 |
PidFile => $pid_file, |
| 562 |
ScoreboardFile => $scoreboard_file, |
| 563 |
SystemLog => $log_file, |
| 564 |
TraceLog => $log_file, |
| 565 |
Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20', |
| 566 |
|
| 567 |
AuthUserFile => $auth_user_file, |
| 568 |
AuthGroupFile => $auth_group_file, |
| 569 |
AuthOrder => 'mod_auth_file.c', |
| 570 |
|
| 571 |
IfModules => { |
| 572 |
'mod_delay.c' => { |
| 573 |
DelayEngine => 'off', |
| 574 |
}, |
| 575 |
|
| 576 |
'mod_sftp.c' => [ |
| 577 |
"SFTPEngine on", |
| 578 |
"SFTPLog $log_file", |
| 579 |
"SFTPHostKey $rsa_host_key", |
| 580 |
"SFTPHostKey $dsa_host_key", |
| 581 |
"SFTPOptions PessimisticKexinit", |
| 582 |
], |
| 583 |
|
| 584 |
'mod_wrap2.c' => { |
| 585 |
WrapEngine => 'on', |
| 586 |
WrapLog => $log_file, |
| 587 |
WrapTables => "file:$allow_file file:$deny_file", |
| 588 |
WrapDenyMsg => $deny_msg, |
| 589 |
WrapOptions => 'CheckOnConnect', |
| 590 |
}, |
| 591 |
}, |
| 592 |
}; |
| 593 |
|
| 594 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 595 |
|
| 596 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 597 |
# the child will indicate when it's done with its test by writing a message |
| 598 |
# to the parent. |
| 599 |
my ($rfh, $wfh); |
| 600 |
unless (pipe($rfh, $wfh)) { |
| 601 |
die("Can't open pipe: $!"); |
| 602 |
} |
| 603 |
|
| 604 |
require Net::SSH2; |
| 605 |
|
| 606 |
my $ex; |
| 607 |
|
| 608 |
# Fork child |
| 609 |
$self->handle_sigchld(); |
| 610 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 611 |
if ($pid) { |
| 612 |
eval { |
| 613 |
sleep(1); |
| 614 |
|
| 615 |
my $proto = getprotobyname('tcp'); |
| 616 |
|
| 617 |
my $sock; |
| 618 |
unless (socket($sock, PF_INET, SOCK_STREAM, $proto)) { |
| 619 |
die("Can't create socket: $!"); |
| 620 |
} |
| 621 |
|
| 622 |
my $in_addr = inet_aton('127.0.0.1'); |
| 623 |
my $addr = sockaddr_in($port, $in_addr); |
| 624 |
|
| 625 |
unless (connect($sock, $addr)) { |
| 626 |
die("Can't connect to 127.0.0.1:$port: $!"); |
| 627 |
} |
| 628 |
|
| 629 |
print $sock "SSH-2.0-MySFTPTestClient\r\n"; |
| 630 |
|
| 631 |
my $buflen = 1024; |
| 632 |
my $buf = ''; |
| 633 |
my $tmp; |
| 634 |
my $res; |
| 635 |
|
| 636 |
while (my $res = read($sock, $tmp, $buflen)) { |
| 637 |
$buf .= $tmp; |
| 638 |
} |
| 639 |
|
| 640 |
close($sock); |
| 641 |
|
| 642 |
chomp($buf); |
| 643 |
$buf =~ s/"//g; |
| 644 |
|
| 645 |
$deny_msg =~ s/'//g; |
| 646 |
$deny_msg =~ s/"//g; |
| 647 |
|
| 648 |
$self->assert(qr/$deny_msg/, $buf, |
| 649 |
test_msg("Expected response '$deny_msg', got '$buf'")); |
| 650 |
}; |
| 651 |
|
| 652 |
if ($@) { |
| 653 |
$ex = $@; |
| 654 |
} |
| 655 |
|
| 656 |
$wfh->print("done\n"); |
| 657 |
$wfh->flush(); |
| 658 |
|
| 659 |
} else { |
| 660 |
eval { server_wait($config_file, $rfh) }; |
| 661 |
if ($@) { |
| 662 |
warn($@); |
| 663 |
exit 1; |
| 664 |
} |
| 665 |
|
| 666 |
exit 0; |
| 667 |
} |
| 668 |
|
| 669 |
# Stop server |
| 670 |
server_stop($pid_file); |
| 671 |
|
| 672 |
$self->assert_child_ok($pid); |
| 673 |
|
| 674 |
if ($ex) { |
| 675 |
test_append_logfile($log_file, $ex); |
| 676 |
unlink($log_file); |
| 677 |
|
| 678 |
die($ex); |
| 679 |
} |
| 680 |
|
| 681 |
unlink($log_file); |
| 682 |
} |
| 683 |
|
| 684 |
sub sftp_wrap2_deny_msg_on_auth_bug3670 { |
| 685 |
my $self = shift; |
| 686 |
my $tmpdir = $self->{tmpdir}; |
| 687 |
|
| 688 |
my $config_file = "$tmpdir/sftp.conf"; |
| 689 |
my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid"); |
| 690 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard"); |
| 691 |
|
| 692 |
my $log_file = test_get_logfile(); |
| 693 |
|
| 694 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd"); |
| 695 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group"); |
| 696 |
|
| 697 |
my $fh; |
| 698 |
my $allow_file = File::Spec->rel2abs("$tmpdir/sftp.allow"); |
| 699 |
if (open($fh, "> $allow_file")) { |
| 700 |
close($fh); |
| 701 |
|
| 702 |
} else { |
| 703 |
die("Can't open $allow_file: $!"); |
| 704 |
} |
| 705 |
|
| 706 |
my $deny_file = File::Spec->rel2abs("$tmpdir/sftp.deny"); |
| 707 |
if (open($fh, "> $deny_file")) { |
| 708 |
print $fh "ALL: ALL\n"; |
| 709 |
|
| 710 |
unless (close($fh)) { |
| 711 |
die("Can't write $deny_file: $!"); |
| 712 |
} |
| 713 |
|
| 714 |
} else { |
| 715 |
die("Can't open $deny_file: $!"); |
| 716 |
} |
| 717 |
|
| 718 |
my $user = 'proftpd'; |
| 719 |
my $passwd = 'test'; |
| 720 |
my $group = 'ftpd'; |
| 721 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 722 |
my $uid = 500; |
| 723 |
my $gid = 500; |
| 724 |
|
| 725 |
# Make sure that, if we're running as root, that the home directory has |
| 726 |
# permissions/privs set for the account we create |
| 727 |
if ($< == 0) { |
| 728 |
unless (chmod(0755, $home_dir)) { |
| 729 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 730 |
} |
| 731 |
|
| 732 |
unless (chown($uid, $gid, $home_dir)) { |
| 733 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 738 |
'/bin/bash'); |
| 739 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 740 |
|
| 741 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 742 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 743 |
|
| 744 |
my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key'); |
| 745 |
my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub'); |
| 746 |
|
| 747 |
my $deny_msg = '"SFTP denied by access rules"'; |
| 748 |
|
| 749 |
my $config = { |
| 750 |
PidFile => $pid_file, |
| 751 |
ScoreboardFile => $scoreboard_file, |
| 752 |
SystemLog => $log_file, |
| 753 |
TraceLog => $log_file, |
| 754 |
Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20', |
| 755 |
|
| 756 |
AuthUserFile => $auth_user_file, |
| 757 |
AuthGroupFile => $auth_group_file, |
| 758 |
AuthOrder => 'mod_auth_file.c', |
| 759 |
|
| 760 |
IfModules => { |
| 761 |
'mod_delay.c' => { |
| 762 |
DelayEngine => 'off', |
| 763 |
}, |
| 764 |
|
| 765 |
'mod_sftp.c' => [ |
| 766 |
"SFTPEngine on", |
| 767 |
"SFTPLog $log_file", |
| 768 |
"SFTPHostKey $rsa_host_key", |
| 769 |
"SFTPHostKey $dsa_host_key", |
| 770 |
], |
| 771 |
|
| 772 |
'mod_wrap2.c' => { |
| 773 |
WrapEngine => 'on', |
| 774 |
WrapLog => $log_file, |
| 775 |
WrapTables => "file:$allow_file file:$deny_file", |
| 776 |
WrapDenyMsg => $deny_msg, |
| 777 |
}, |
| 778 |
}, |
| 779 |
}; |
| 780 |
|
| 781 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 782 |
|
| 783 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 784 |
# the child will indicate when it's done with its test by writing a message |
| 785 |
# to the parent. |
| 786 |
my ($rfh, $wfh); |
| 787 |
unless (pipe($rfh, $wfh)) { |
| 788 |
die("Can't open pipe: $!"); |
| 789 |
} |
| 790 |
|
| 791 |
require Net::SSH2; |
| 792 |
|
| 793 |
my $ex; |
| 794 |
|
| 795 |
# Fork child |
| 796 |
$self->handle_sigchld(); |
| 797 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 798 |
if ($pid) { |
| 799 |
eval { |
| 800 |
my $ssh2 = Net::SSH2->new(); |
| 801 |
|
| 802 |
sleep(1); |
| 803 |
|
| 804 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 805 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 806 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str (see unit test notes)"); |
| 807 |
} |
| 808 |
|
| 809 |
if ($ssh2->auth_password($user, $passwd)) { |
| 810 |
die("Password authentication succeeded unexpectedly"); |
| 811 |
} |
| 812 |
|
| 813 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 814 |
|
| 815 |
$ssh2->disconnect(); |
| 816 |
|
| 817 |
# Unfortunately, the Net::SSH2::SFTP API doesn't provide the error |
| 818 |
# text as sent by the SFTP server. |
| 819 |
}; |
| 820 |
|
| 821 |
if ($@) { |
| 822 |
$ex = $@; |
| 823 |
} |
| 824 |
|
| 825 |
$wfh->print("done\n"); |
| 826 |
$wfh->flush(); |
| 827 |
|
| 828 |
} else { |
| 829 |
eval { server_wait($config_file, $rfh) }; |
| 830 |
if ($@) { |
| 831 |
warn($@); |
| 832 |
exit 1; |
| 833 |
} |
| 834 |
|
| 835 |
exit 0; |
| 836 |
} |
| 837 |
|
| 838 |
# Stop server |
| 839 |
server_stop($pid_file); |
| 840 |
|
| 841 |
$self->assert_child_ok($pid); |
| 842 |
|
| 843 |
if ($ex) { |
| 844 |
test_append_logfile($log_file, $ex); |
| 845 |
unlink($log_file); |
| 846 |
|
| 847 |
die($ex); |
| 848 |
} |
| 849 |
|
| 850 |
unlink($log_file); |
| 851 |
} |
| 852 |
|
| 853 |
sub sftp_wrap2_deny_msg_var_u { |
| 854 |
my $self = shift; |
| 855 |
my $tmpdir = $self->{tmpdir}; |
| 856 |
|
| 857 |
my $config_file = "$tmpdir/sftp.conf"; |
| 858 |
my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid"); |
| 859 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard"); |
| 860 |
|
| 861 |
my $log_file = test_get_logfile(); |
| 862 |
|
| 863 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd"); |
| 864 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group"); |
| 865 |
|
| 866 |
my $fh; |
| 867 |
my $allow_file = File::Spec->rel2abs("$tmpdir/sftp.allow"); |
| 868 |
if (open($fh, "> $allow_file")) { |
| 869 |
close($fh); |
| 870 |
|
| 871 |
} else { |
| 872 |
die("Can't open $allow_file: $!"); |
| 873 |
} |
| 874 |
|
| 875 |
my $deny_file = File::Spec->rel2abs("$tmpdir/sftp.deny"); |
| 876 |
if (open($fh, "> $deny_file")) { |
| 877 |
print $fh "ALL: ALL\n"; |
| 878 |
|
| 879 |
unless (close($fh)) { |
| 880 |
die("Can't write $deny_file: $!"); |
| 881 |
} |
| 882 |
|
| 883 |
} else { |
| 884 |
die("Can't open $deny_file: $!"); |
| 885 |
} |
| 886 |
|
| 887 |
my $user = 'proftpd'; |
| 888 |
my $passwd = 'test'; |
| 889 |
my $group = 'ftpd'; |
| 890 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 891 |
my $uid = 500; |
| 892 |
my $gid = 500; |
| 893 |
|
| 894 |
# Make sure that, if we're running as root, that the home directory has |
| 895 |
# permissions/privs set for the account we create |
| 896 |
if ($< == 0) { |
| 897 |
unless (chmod(0755, $home_dir)) { |
| 898 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 899 |
} |
| 900 |
|
| 901 |
unless (chown($uid, $gid, $home_dir)) { |
| 902 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 907 |
'/bin/bash'); |
| 908 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 909 |
|
| 910 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 911 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 912 |
|
| 913 |
my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key'); |
| 914 |
my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub'); |
| 915 |
|
| 916 |
my $deny_msg = '"User %u denied by access rules"'; |
| 917 |
|
| 918 |
my $config = { |
| 919 |
PidFile => $pid_file, |
| 920 |
ScoreboardFile => $scoreboard_file, |
| 921 |
SystemLog => $log_file, |
| 922 |
TraceLog => $log_file, |
| 923 |
Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20', |
| 924 |
|
| 925 |
AuthUserFile => $auth_user_file, |
| 926 |
AuthGroupFile => $auth_group_file, |
| 927 |
AuthOrder => 'mod_auth_file.c', |
| 928 |
|
| 929 |
IfModules => { |
| 930 |
'mod_delay.c' => { |
| 931 |
DelayEngine => 'off', |
| 932 |
}, |
| 933 |
|
| 934 |
'mod_sftp.c' => [ |
| 935 |
"SFTPEngine on", |
| 936 |
"SFTPLog $log_file", |
| 937 |
"SFTPHostKey $rsa_host_key", |
| 938 |
"SFTPHostKey $dsa_host_key", |
| 939 |
], |
| 940 |
|
| 941 |
'mod_wrap2.c' => { |
| 942 |
WrapEngine => 'on', |
| 943 |
WrapLog => $log_file, |
| 944 |
WrapTables => "file:$allow_file file:$deny_file", |
| 945 |
WrapDenyMsg => $deny_msg, |
| 946 |
}, |
| 947 |
}, |
| 948 |
}; |
| 949 |
|
| 950 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 951 |
|
| 952 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 953 |
# the child will indicate when it's done with its test by writing a message |
| 954 |
# to the parent. |
| 955 |
my ($rfh, $wfh); |
| 956 |
unless (pipe($rfh, $wfh)) { |
| 957 |
die("Can't open pipe: $!"); |
| 958 |
} |
| 959 |
|
| 960 |
require Net::SSH2; |
| 961 |
|
| 962 |
my $ex; |
| 963 |
|
| 964 |
# Fork child |
| 965 |
$self->handle_sigchld(); |
| 966 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 967 |
if ($pid) { |
| 968 |
eval { |
| 969 |
my $ssh2 = Net::SSH2->new(); |
| 970 |
|
| 971 |
sleep(1); |
| 972 |
|
| 973 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 974 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 975 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str (see unit test notes)"); |
| 976 |
} |
| 977 |
|
| 978 |
if ($ssh2->auth_password($user, $passwd)) { |
| 979 |
die("Password authentication succeeded unexpectedly"); |
| 980 |
} |
| 981 |
|
| 982 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 983 |
|
| 984 |
$ssh2->disconnect(); |
| 985 |
|
| 986 |
# Unfortunately, the Net::SSH2::SFTP API doesn't provide the error |
| 987 |
# text as sent by the SFTP server. |
| 988 |
}; |
| 989 |
|
| 990 |
if ($@) { |
| 991 |
$ex = $@; |
| 992 |
} |
| 993 |
|
| 994 |
$wfh->print("done\n"); |
| 995 |
$wfh->flush(); |
| 996 |
|
| 997 |
} else { |
| 998 |
eval { server_wait($config_file, $rfh) }; |
| 999 |
if ($@) { |
| 1000 |
warn($@); |
| 1001 |
exit 1; |
| 1002 |
} |
| 1003 |
|
| 1004 |
exit 0; |
| 1005 |
} |
| 1006 |
|
| 1007 |
# Stop server |
| 1008 |
server_stop($pid_file); |
| 1009 |
|
| 1010 |
$self->assert_child_ok($pid); |
| 1011 |
|
| 1012 |
if ($ex) { |
| 1013 |
test_append_logfile($log_file, $ex); |
| 1014 |
unlink($log_file); |
| 1015 |
|
| 1016 |
die($ex); |
| 1017 |
} |
| 1018 |
|
| 1019 |
unlink($log_file); |
| 1020 |
} |
| 1021 |
|
| 1022 |
1; |