| 1 |
package ProFTPD::Tests::Modules::mod_sftp_pam; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use Cwd; |
| 8 |
use Digest::MD5; |
| 9 |
use File::Copy; |
| 10 |
use File::Path qw(mkpath rmtree); |
| 11 |
use File::Spec; |
| 12 |
use IO::Handle; |
| 13 |
use IPC::Open3; |
| 14 |
use POSIX qw(:fcntl_h); |
| 15 |
use Socket; |
| 16 |
|
| 17 |
use ProFTPD::TestSuite::FTP; |
| 18 |
use ProFTPD::TestSuite::Utils qw(:auth :config :features :running :test :testsuite); |
| 19 |
|
| 20 |
$| = 1; |
| 21 |
|
| 22 |
my $order = 0; |
| 23 |
|
| 24 |
my $TESTS = { |
| 25 |
sftp_pam_failed_login_attempts_bug3921 => { |
| 26 |
order => ++$order, |
| 27 |
test_class => [qw(bug forking pam rootprivs ssh2)], |
| 28 |
}, |
| 29 |
|
| 30 |
sftp_pam_with_sql_auth => { |
| 31 |
order => ++$order, |
| 32 |
test_class => [qw(forking mod_sql_sqlite pam rootprivs ssh2)], |
| 33 |
}, |
| 34 |
|
| 35 |
}; |
| 36 |
|
| 37 |
sub new { |
| 38 |
return shift()->SUPER::new(@_); |
| 39 |
} |
| 40 |
|
| 41 |
sub list_tests { |
| 42 |
# Check for the required Perl modules: |
| 43 |
# |
| 44 |
# Net-SSH2 |
| 45 |
# Net-SSH2-SFTP |
| 46 |
|
| 47 |
my $required = [qw( |
| 48 |
Net::SSH2 |
| 49 |
Net::SSH2::SFTP |
| 50 |
)]; |
| 51 |
|
| 52 |
foreach my $req (@$required) { |
| 53 |
eval "use $req"; |
| 54 |
if ($@) { |
| 55 |
print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n"; |
| 56 |
|
| 57 |
if ($ENV{TEST_VERBOSE}) { |
| 58 |
print STDERR "Unable to load $req: $@\n"; |
| 59 |
} |
| 60 |
|
| 61 |
return qw(testsuite_empty_test); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return testsuite_get_runnable_tests($TESTS); |
| 66 |
} |
| 67 |
|
| 68 |
sub set_up { |
| 69 |
my $self = shift; |
| 70 |
$self->SUPER::set_up(@_); |
| 71 |
|
| 72 |
# Make sure that mod_sftp does not complain about permissions on the hostkey |
| 73 |
# files. |
| 74 |
|
| 75 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 76 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 77 |
|
| 78 |
unless (chmod(0400, $rsa_host_key, $dsa_host_key)) { |
| 79 |
die("Can't set perms on $rsa_host_key, $dsa_host_key: $!"); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
sub sftp_pam_failed_login_attempts_bug3921 { |
| 84 |
my $self = shift; |
| 85 |
my $tmpdir = $self->{tmpdir}; |
| 86 |
|
| 87 |
unless ($ENV{SFTP_PAM_USER} && $ENV{SFTP_PAM_PASSWD}) { |
| 88 |
print STDERR " + unable to run 'sftp_pam_failed_login_attempts_bug3921' test without SFTP_PAM_USER and SFTP_PAM_PASSWD environment variables set, skipping\n"; |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
my $config_file = "$tmpdir/sftp.conf"; |
| 93 |
my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid"); |
| 94 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard"); |
| 95 |
|
| 96 |
my $log_file = test_get_logfile(); |
| 97 |
|
| 98 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd"); |
| 99 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group"); |
| 100 |
|
| 101 |
my $user = 'proftpd'; |
| 102 |
my $passwd = 'test'; |
| 103 |
my $group = 'ftpd'; |
| 104 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 105 |
my $uid = 500; |
| 106 |
my $gid = 500; |
| 107 |
|
| 108 |
# Make sure that, if we're running as root, that the home directory has |
| 109 |
# permissions/privs set for the account we create |
| 110 |
if ($< == 0) { |
| 111 |
unless (chmod(0755, $home_dir)) { |
| 112 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 113 |
} |
| 114 |
|
| 115 |
unless (chown($uid, $gid, $home_dir)) { |
| 116 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 121 |
'/bin/bash'); |
| 122 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 123 |
|
| 124 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 125 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 126 |
|
| 127 |
my $config = { |
| 128 |
PidFile => $pid_file, |
| 129 |
ScoreboardFile => $scoreboard_file, |
| 130 |
SystemLog => $log_file, |
| 131 |
TraceLog => $log_file, |
| 132 |
Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20', |
| 133 |
|
| 134 |
AuthUserFile => $auth_user_file, |
| 135 |
AuthGroupFile => $auth_group_file, |
| 136 |
AuthOrder => 'mod_auth_file.c', |
| 137 |
|
| 138 |
IfModules => { |
| 139 |
'mod_delay.c' => { |
| 140 |
DelayEngine => 'off', |
| 141 |
}, |
| 142 |
|
| 143 |
'mod_sftp.c' => [ |
| 144 |
"SFTPEngine on", |
| 145 |
"SFTPLog $log_file", |
| 146 |
"SFTPHostKey $rsa_host_key", |
| 147 |
"SFTPHostKey $dsa_host_key", |
| 148 |
|
| 149 |
"SFTPAuthMethods keyboard-interactive", |
| 150 |
], |
| 151 |
|
| 152 |
'mod_sftp_pam.c' => { |
| 153 |
AuthOrder => 'mod_sftp_pam.c* mod_auth_unix.c', |
| 154 |
}, |
| 155 |
}, |
| 156 |
}; |
| 157 |
|
| 158 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 159 |
|
| 160 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 161 |
# the child will indicate when it's done with its test by writing a message |
| 162 |
# to the parent. |
| 163 |
my ($rfh, $wfh); |
| 164 |
unless (pipe($rfh, $wfh)) { |
| 165 |
die("Can't open pipe: $!"); |
| 166 |
} |
| 167 |
|
| 168 |
require Net::SSH2; |
| 169 |
|
| 170 |
my $ex; |
| 171 |
|
| 172 |
# Fork child |
| 173 |
$self->handle_sigchld(); |
| 174 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 175 |
if ($pid) { |
| 176 |
eval { |
| 177 |
my $ssh2 = Net::SSH2->new(); |
| 178 |
|
| 179 |
sleep(1); |
| 180 |
|
| 181 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 182 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 183 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 184 |
} |
| 185 |
|
| 186 |
$user = $ENV{SFTP_PAM_USER}; |
| 187 |
my $bad_passwd = 'foobar'; |
| 188 |
my $good_passwd = $ENV{SFTP_PAM_PASSWD}; |
| 189 |
|
| 190 |
if ($ssh2->auth_keyboard($user, $bad_passwd)) { |
| 191 |
die("Keyboard-interactive authentication succeeded unexpectedly"); |
| 192 |
} |
| 193 |
|
| 194 |
if ($ssh2->auth_keyboard($user, $bad_passwd)) { |
| 195 |
die("Keyboard-interactive authentication succeeded unexpectedly"); |
| 196 |
} |
| 197 |
|
| 198 |
unless ($ssh2->auth_keyboard($user, $good_passwd)) { |
| 199 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 200 |
die("Can't authenticate to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 201 |
} |
| 202 |
|
| 203 |
$ssh2->disconnect(); |
| 204 |
}; |
| 205 |
|
| 206 |
if ($@) { |
| 207 |
$ex = $@; |
| 208 |
} |
| 209 |
|
| 210 |
$wfh->print("done\n"); |
| 211 |
$wfh->flush(); |
| 212 |
|
| 213 |
} else { |
| 214 |
eval { server_wait($config_file, $rfh) }; |
| 215 |
if ($@) { |
| 216 |
warn($@); |
| 217 |
exit 1; |
| 218 |
} |
| 219 |
|
| 220 |
exit 0; |
| 221 |
} |
| 222 |
|
| 223 |
# Stop server |
| 224 |
server_stop($pid_file); |
| 225 |
|
| 226 |
$self->assert_child_ok($pid); |
| 227 |
|
| 228 |
if ($ex) { |
| 229 |
test_append_logfile($log_file, $ex); |
| 230 |
unlink($log_file); |
| 231 |
|
| 232 |
die($ex); |
| 233 |
} |
| 234 |
|
| 235 |
unlink($log_file); |
| 236 |
} |
| 237 |
|
| 238 |
sub build_db { |
| 239 |
my $cmd = shift; |
| 240 |
my $db_script = shift; |
| 241 |
my $check_exit_status = shift; |
| 242 |
$check_exit_status = 0 unless defined $check_exit_status; |
| 243 |
|
| 244 |
if ($ENV{TEST_VERBOSE}) { |
| 245 |
print STDERR "Executing sqlite3: $cmd\n"; |
| 246 |
} |
| 247 |
|
| 248 |
my @output = `$cmd`; |
| 249 |
my $exit_status = $?; |
| 250 |
|
| 251 |
if ($ENV{TEST_VERBOSE}) { |
| 252 |
print STDERR "Output: ", join('', @output), "\n"; |
| 253 |
} |
| 254 |
|
| 255 |
if ($check_exit_status) { |
| 256 |
if ($? != 0) { |
| 257 |
croak("'$cmd' failed"); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
unlink($db_script); |
| 262 |
return 1; |
| 263 |
} |
| 264 |
|
| 265 |
sub sftp_pam_with_sql_auth { |
| 266 |
my $self = shift; |
| 267 |
my $tmpdir = $self->{tmpdir}; |
| 268 |
|
| 269 |
my $config_file = "$tmpdir/sftp.conf"; |
| 270 |
my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid"); |
| 271 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard"); |
| 272 |
|
| 273 |
my $log_file = test_get_logfile(); |
| 274 |
|
| 275 |
my $user = 'proftpd'; |
| 276 |
my $passwd = 'test'; |
| 277 |
my $group = 'ftpd'; |
| 278 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 279 |
my $uid = 500; |
| 280 |
my $gid = 500; |
| 281 |
|
| 282 |
my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db"); |
| 283 |
|
| 284 |
# Build up sqlite3 command to create users, groups tables and populate them |
| 285 |
my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql"); |
| 286 |
|
| 287 |
if (open(my $fh, "> $db_script")) { |
| 288 |
print $fh <<EOS; |
| 289 |
CREATE TABLE users ( |
| 290 |
userid TEXT, |
| 291 |
passwd TEXT, |
| 292 |
uid INTEGER, |
| 293 |
gid INTEGER, |
| 294 |
homedir TEXT, |
| 295 |
shell TEXT, |
| 296 |
lastdir TEXT |
| 297 |
); |
| 298 |
INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash'); |
| 299 |
|
| 300 |
CREATE TABLE groups ( |
| 301 |
groupname TEXT, |
| 302 |
gid INTEGER, |
| 303 |
members TEXT |
| 304 |
); |
| 305 |
INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user'); |
| 306 |
EOS |
| 307 |
|
| 308 |
unless (close($fh)) { |
| 309 |
die("Can't write $db_script: $!"); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
} else { |
| 314 |
die("Can't open $db_script: $!"); |
| 315 |
} |
| 316 |
|
| 317 |
my $cmd = "sqlite3 $db_file < $db_script"; |
| 318 |
build_db($cmd, $db_script); |
| 319 |
|
| 320 |
# Make sure that, if we're running as root, the database file has |
| 321 |
# the permissions/privs set for use by proftpd |
| 322 |
if ($< == 0) { |
| 323 |
unless (chmod(0666, $db_file)) { |
| 324 |
die("Can't set perms on $db_file to 0666: $!"); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
# Make sure that, if we're running as root, that the home directory has |
| 329 |
# permissions/privs set for the account we create |
| 330 |
if ($< == 0) { |
| 331 |
unless (chmod(0755, $home_dir)) { |
| 332 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 333 |
} |
| 334 |
|
| 335 |
unless (chown($uid, $gid, $home_dir)) { |
| 336 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 341 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 342 |
|
| 343 |
my $config = { |
| 344 |
PidFile => $pid_file, |
| 345 |
ScoreboardFile => $scoreboard_file, |
| 346 |
SystemLog => $log_file, |
| 347 |
|
| 348 |
IfModules => { |
| 349 |
'mod_delay.c' => { |
| 350 |
DelayEngine => 'off', |
| 351 |
}, |
| 352 |
|
| 353 |
'mod_sftp.c' => [ |
| 354 |
"SFTPEngine on", |
| 355 |
"SFTPLog $log_file", |
| 356 |
"SFTPHostKey $rsa_host_key", |
| 357 |
"SFTPHostKey $dsa_host_key", |
| 358 |
|
| 359 |
"SFTPAuthMethods keyboard-interactive", |
| 360 |
], |
| 361 |
|
| 362 |
'mod_sftp_pam.c' => { |
| 363 |
AuthOrder => 'mod_sftp_pam.c* mod_sql.c', |
| 364 |
}, |
| 365 |
|
| 366 |
'mod_sql.c' => { |
| 367 |
SQLAuthTypes => 'plaintext', |
| 368 |
SQLBackend => 'sqlite3', |
| 369 |
SQLConnectInfo => $db_file, |
| 370 |
SQLLogFile => $log_file, |
| 371 |
}, |
| 372 |
}, |
| 373 |
}; |
| 374 |
|
| 375 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 376 |
|
| 377 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 378 |
# the child will indicate when it's done with its test by writing a message |
| 379 |
# to the parent. |
| 380 |
my ($rfh, $wfh); |
| 381 |
unless (pipe($rfh, $wfh)) { |
| 382 |
die("Can't open pipe: $!"); |
| 383 |
} |
| 384 |
|
| 385 |
require Net::SSH2; |
| 386 |
|
| 387 |
my $ex; |
| 388 |
|
| 389 |
# Fork child |
| 390 |
$self->handle_sigchld(); |
| 391 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 392 |
if ($pid) { |
| 393 |
eval { |
| 394 |
my $ssh2 = Net::SSH2->new(); |
| 395 |
|
| 396 |
sleep(1); |
| 397 |
|
| 398 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 399 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 400 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 401 |
} |
| 402 |
|
| 403 |
if ($ssh2->auth_keyboard($user, $passwd)) { |
| 404 |
die("Keyboard-interactive authentication succeeded unexpectedly"); |
| 405 |
} |
| 406 |
|
| 407 |
$ssh2->disconnect(); |
| 408 |
}; |
| 409 |
|
| 410 |
if ($@) { |
| 411 |
$ex = $@; |
| 412 |
} |
| 413 |
|
| 414 |
$wfh->print("done\n"); |
| 415 |
$wfh->flush(); |
| 416 |
|
| 417 |
} else { |
| 418 |
eval { server_wait($config_file, $rfh) }; |
| 419 |
if ($@) { |
| 420 |
warn($@); |
| 421 |
exit 1; |
| 422 |
} |
| 423 |
|
| 424 |
exit 0; |
| 425 |
} |
| 426 |
|
| 427 |
# Stop server |
| 428 |
server_stop($pid_file); |
| 429 |
|
| 430 |
$self->assert_child_ok($pid); |
| 431 |
|
| 432 |
if ($ex) { |
| 433 |
test_append_logfile($log_file, $ex); |
| 434 |
unlink($log_file); |
| 435 |
|
| 436 |
die($ex); |
| 437 |
} |
| 438 |
|
| 439 |
unlink($log_file); |
| 440 |
} |
| 441 |
|
| 442 |
1; |