| 1 |
package ProFTPD::Tests::Modules::mod_sftp::ban; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use File::Path qw(mkpath rmtree); |
| 8 |
use File::Spec; |
| 9 |
use IO::Handle; |
| 10 |
use POSIX qw(:fcntl_h); |
| 11 |
use Socket; |
| 12 |
use Time::HiRes qw(gettimeofday tv_interval); |
| 13 |
|
| 14 |
use ProFTPD::TestSuite::FTP; |
| 15 |
use ProFTPD::TestSuite::Utils qw(:auth :config :features :running :test :testsuite); |
| 16 |
|
| 17 |
$| = 1; |
| 18 |
|
| 19 |
my $order = 0; |
| 20 |
|
| 21 |
my $TESTS = { |
| 22 |
sftp_ban_max_login_attempts => { |
| 23 |
order => ++$order, |
| 24 |
test_class => [qw(bug forking mod_ban sftp ssh2)], |
| 25 |
}, |
| 26 |
|
| 27 |
sftp_ban_maxcmdrate_exceeded => { |
| 28 |
order => ++$order, |
| 29 |
test_class => [qw(bug forking mod_ban sftp ssh2)], |
| 30 |
}, |
| 31 |
|
| 32 |
sftp_ban_max_login_attempts_per_user_issue2009 => { |
| 33 |
order => ++$order, |
| 34 |
test_class => [qw(bug forking mod_ban sftp ssh2)], |
| 35 |
}, |
| 36 |
|
| 37 |
}; |
| 38 |
|
| 39 |
sub new { |
| 40 |
return shift()->SUPER::new(@_); |
| 41 |
} |
| 42 |
|
| 43 |
sub list_tests { |
| 44 |
# Check for the required Perl modules: |
| 45 |
# |
| 46 |
# Net-SSH2 |
| 47 |
# Net-SSH2-SFTP |
| 48 |
|
| 49 |
my $required = [qw( |
| 50 |
Net::SSH2 |
| 51 |
Net::SSH2::SFTP |
| 52 |
)]; |
| 53 |
|
| 54 |
foreach my $req (@$required) { |
| 55 |
eval "use $req"; |
| 56 |
if ($@) { |
| 57 |
print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n"; |
| 58 |
|
| 59 |
if ($ENV{TEST_VERBOSE}) { |
| 60 |
print STDERR "Unable to load $req: $@\n"; |
| 61 |
} |
| 62 |
|
| 63 |
return qw(testsuite_empty_test); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return testsuite_get_runnable_tests($TESTS); |
| 68 |
} |
| 69 |
|
| 70 |
sub set_up { |
| 71 |
my $self = shift; |
| 72 |
$self->SUPER::set_up(@_); |
| 73 |
|
| 74 |
# Make sure that mod_sftp does not complain about permissions on the hostkey |
| 75 |
# files. |
| 76 |
|
| 77 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 78 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 79 |
|
| 80 |
unless (chmod(0400, $rsa_host_key, $dsa_host_key)) { |
| 81 |
die("Can't set perms on $rsa_host_key, $dsa_host_key: $!"); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
sub sftp_ban_max_login_attempts { |
| 86 |
my $self = shift; |
| 87 |
my $tmpdir = $self->{tmpdir}; |
| 88 |
my $setup = test_setup($tmpdir, 'sftp-ban'); |
| 89 |
|
| 90 |
my $ban_tab = File::Spec->rel2abs("$tmpdir/sftp-ban.tab"); |
| 91 |
|
| 92 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 93 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 94 |
|
| 95 |
my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key'); my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub'); |
| 96 |
|
| 97 |
my $config = { |
| 98 |
PidFile => $setup->{pid_file}, |
| 99 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 100 |
SystemLog => $setup->{log_file}, |
| 101 |
TraceLog => $setup->{log_file}, |
| 102 |
Trace => 'DEFAULT:10 ban:20 ssh2:20 sftp:20', |
| 103 |
|
| 104 |
AuthUserFile => $setup->{auth_user_file}, |
| 105 |
AuthGroupFile => $setup->{auth_group_file}, |
| 106 |
AuthOrder => 'mod_auth_file.c', |
| 107 |
|
| 108 |
MaxLoginAttempts => 1, |
| 109 |
|
| 110 |
IfModules => { |
| 111 |
'mod_ban.c' => { |
| 112 |
BanEngine => 'on', |
| 113 |
BanLog => $setup->{log_file}, |
| 114 |
|
| 115 |
# This says to ban a client which exceeds the MaxLoginAttempts |
| 116 |
# limit once within the last 1 minute will be banned for 10 secs |
| 117 |
BanOnEvent => 'MaxLoginAttempts 1/00:01:00 00:00:10', |
| 118 |
|
| 119 |
BanTable => $ban_tab, |
| 120 |
}, |
| 121 |
|
| 122 |
'mod_delay.c' => { |
| 123 |
DelayEngine => 'off', |
| 124 |
}, |
| 125 |
|
| 126 |
'mod_sftp.c' => [ |
| 127 |
"SFTPEngine on", |
| 128 |
"SFTPLog $setup->{log_file}", |
| 129 |
"SFTPHostKey $rsa_host_key", |
| 130 |
"SFTPHostKey $dsa_host_key", |
| 131 |
], |
| 132 |
}, |
| 133 |
}; |
| 134 |
|
| 135 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 136 |
$config); |
| 137 |
|
| 138 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 139 |
# the child will indicate when it's done with its test by writing a message |
| 140 |
# to the parent. |
| 141 |
my ($rfh, $wfh); |
| 142 |
unless (pipe($rfh, $wfh)) { |
| 143 |
die("Can't open pipe: $!"); |
| 144 |
} |
| 145 |
|
| 146 |
require Net::SSH2; |
| 147 |
|
| 148 |
my $ex; |
| 149 |
|
| 150 |
# Fork child |
| 151 |
$self->handle_sigchld(); |
| 152 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 153 |
if ($pid) { |
| 154 |
eval { |
| 155 |
my $ssh2 = Net::SSH2->new(); |
| 156 |
|
| 157 |
sleep(1); |
| 158 |
|
| 159 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 160 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 161 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 162 |
} |
| 163 |
|
| 164 |
if ($ssh2->auth_password($setup->{user}, 'foo')) { |
| 165 |
die("Password auth (wrong password) succeeded unexpectedly"); |
| 166 |
} |
| 167 |
|
| 168 |
$ssh2->disconnect(); |
| 169 |
|
| 170 |
# Now try the correct password for authentication, which would succeed -- |
| 171 |
# except that mod_ban should now have a ban in place, and prevent us |
| 172 |
# from doing this. Note that we need to create a new connection to test |
| 173 |
# the mod_ban rules. |
| 174 |
|
| 175 |
$ssh2 = Net::SSH2->new(); |
| 176 |
if ($ssh2->connect('127.0.0.1', $port)) { |
| 177 |
die("Connecting to SSH2 server succeeded unexpectedly"); |
| 178 |
} |
| 179 |
}; |
| 180 |
if ($@) { |
| 181 |
$ex = $@; |
| 182 |
} |
| 183 |
|
| 184 |
$wfh->print("done\n"); |
| 185 |
$wfh->flush(); |
| 186 |
|
| 187 |
} else { |
| 188 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 189 |
if ($@) { |
| 190 |
warn($@); |
| 191 |
exit 1; |
| 192 |
} |
| 193 |
|
| 194 |
exit 0; |
| 195 |
} |
| 196 |
|
| 197 |
# Stop server |
| 198 |
server_stop($setup->{pid_file}); |
| 199 |
$self->assert_child_ok($pid); |
| 200 |
|
| 201 |
test_cleanup($setup->{log_file}, $ex); |
| 202 |
} |
| 203 |
|
| 204 |
sub sftp_ban_maxcmdrate_exceeded { |
| 205 |
my $self = shift; |
| 206 |
my $tmpdir = $self->{tmpdir}; |
| 207 |
|
| 208 |
my $config_file = "$tmpdir/sftp.conf"; |
| 209 |
my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid"); |
| 210 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard"); |
| 211 |
|
| 212 |
my $log_file = test_get_logfile(); |
| 213 |
|
| 214 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd"); |
| 215 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group"); |
| 216 |
|
| 217 |
my $ban_tab = File::Spec->rel2abs("$tmpdir/sftp-ban.tab"); |
| 218 |
|
| 219 |
my $user = 'proftpd'; |
| 220 |
my $passwd = 'test'; |
| 221 |
my $group = 'ftpd'; |
| 222 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 223 |
my $uid = 500; |
| 224 |
my $gid = 500; |
| 225 |
|
| 226 |
# Make sure that, if we're running as root, that the home directory has |
| 227 |
# permissions/privs set for the account we create |
| 228 |
if ($< == 0) { |
| 229 |
unless (chmod(0755, $home_dir)) { |
| 230 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 231 |
} |
| 232 |
|
| 233 |
unless (chown($uid, $gid, $home_dir)) { |
| 234 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 239 |
'/bin/bash'); |
| 240 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 241 |
|
| 242 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 243 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 244 |
|
| 245 |
my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key'); my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub'); |
| 246 |
|
| 247 |
my $max_cmd_rate = 10; |
| 248 |
|
| 249 |
my $config = { |
| 250 |
PidFile => $pid_file, |
| 251 |
ScoreboardFile => $scoreboard_file, |
| 252 |
SystemLog => $log_file, |
| 253 |
TraceLog => $log_file, |
| 254 |
Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20', |
| 255 |
|
| 256 |
AuthUserFile => $auth_user_file, |
| 257 |
AuthGroupFile => $auth_group_file, |
| 258 |
AuthOrder => 'mod_auth_file.c', |
| 259 |
|
| 260 |
MaxCommandRate => $max_cmd_rate, |
| 261 |
|
| 262 |
IfModules => { |
| 263 |
'mod_ban.c' => { |
| 264 |
BanEngine => 'on', |
| 265 |
BanLog => $log_file, |
| 266 |
|
| 267 |
# This says to ban a client which exceeds the MaxCommandRate |
| 268 |
# limit once within the last 1 minute will be banned for 10 secs |
| 269 |
BanOnEvent => 'MaxCommandRate 1/00:01:00 00:00:10', |
| 270 |
|
| 271 |
BanTable => $ban_tab, |
| 272 |
}, |
| 273 |
|
| 274 |
'mod_delay.c' => { |
| 275 |
DelayEngine => 'off', |
| 276 |
}, |
| 277 |
|
| 278 |
'mod_sftp.c' => [ |
| 279 |
"SFTPEngine on", |
| 280 |
"SFTPLog $log_file", |
| 281 |
"SFTPHostKey $rsa_host_key", |
| 282 |
"SFTPHostKey $dsa_host_key", |
| 283 |
], |
| 284 |
}, |
| 285 |
}; |
| 286 |
|
| 287 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 288 |
|
| 289 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 290 |
# the child will indicate when it's done with its test by writing a message |
| 291 |
# to the parent. |
| 292 |
my ($rfh, $wfh); |
| 293 |
unless (pipe($rfh, $wfh)) { |
| 294 |
die("Can't open pipe: $!"); |
| 295 |
} |
| 296 |
|
| 297 |
require Net::SSH2; |
| 298 |
|
| 299 |
my $ex; |
| 300 |
|
| 301 |
# Fork child |
| 302 |
$self->handle_sigchld(); |
| 303 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 304 |
if ($pid) { |
| 305 |
eval { |
| 306 |
my $ssh2 = Net::SSH2->new(); |
| 307 |
|
| 308 |
sleep(1); |
| 309 |
|
| 310 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 311 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 312 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 313 |
} |
| 314 |
|
| 315 |
unless ($ssh2->auth_password($user, $passwd)) { |
| 316 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 317 |
die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 318 |
} |
| 319 |
|
| 320 |
my $sftp = $ssh2->sftp(); |
| 321 |
unless ($sftp) { |
| 322 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 323 |
die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str"); |
| 324 |
} |
| 325 |
|
| 326 |
for (my $i = 0; $i < ($max_cmd_rate * 5); $i++) { |
| 327 |
my $start = [gettimeofday()]; |
| 328 |
unless ($sftp->realpath('.')) { |
| 329 |
last; |
| 330 |
} |
| 331 |
|
| 332 |
my $elapsed = tv_interval($start); |
| 333 |
|
| 334 |
if ($ENV{TEST_VERBOSE}) { |
| 335 |
print STDERR "$i: elapsed = $elapsed\n"; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
eval { $ssh2->disconnect() }; |
| 340 |
|
| 341 |
# Now try 'password' authentication, which would succeed -- except that |
| 342 |
# mod_ban should now have a ban in place, and prevent us from doing this. |
| 343 |
# Note that we need to create a new connection to test the mod_ban |
| 344 |
# rules. |
| 345 |
|
| 346 |
$ssh2 = Net::SSH2->new(); |
| 347 |
if ($ssh2->connect('127.0.0.1', $port)) { |
| 348 |
die("Connecting to SSH2 server succeeded unexpectedly"); |
| 349 |
} |
| 350 |
}; |
| 351 |
|
| 352 |
if ($@) { |
| 353 |
$ex = $@; |
| 354 |
} |
| 355 |
|
| 356 |
$wfh->print("done\n"); |
| 357 |
$wfh->flush(); |
| 358 |
|
| 359 |
} else { |
| 360 |
eval { server_wait($config_file, $rfh) }; |
| 361 |
if ($@) { |
| 362 |
warn($@); |
| 363 |
exit 1; |
| 364 |
} |
| 365 |
|
| 366 |
exit 0; |
| 367 |
} |
| 368 |
|
| 369 |
# Stop server |
| 370 |
server_stop($pid_file); |
| 371 |
|
| 372 |
$self->assert_child_ok($pid); |
| 373 |
|
| 374 |
if ($ex) { |
| 375 |
test_append_logfile($log_file, $ex); |
| 376 |
unlink($log_file); |
| 377 |
|
| 378 |
die($ex); |
| 379 |
} |
| 380 |
|
| 381 |
unlink($log_file); |
| 382 |
} |
| 383 |
|
| 384 |
sub sftp_ban_max_login_attempts_per_user_issue2009 { |
| 385 |
my $self = shift; |
| 386 |
my $tmpdir = $self->{tmpdir}; |
| 387 |
my $setup = test_setup($tmpdir, 'sftp-ban'); |
| 388 |
|
| 389 |
my $ban_tab = File::Spec->rel2abs("$tmpdir/sftp-ban.tab"); |
| 390 |
|
| 391 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key'); |
| 392 |
my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key'); |
| 393 |
|
| 394 |
my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key'); my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub'); |
| 395 |
|
| 396 |
my $config = { |
| 397 |
PidFile => $setup->{pid_file}, |
| 398 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 399 |
SystemLog => $setup->{log_file}, |
| 400 |
TraceLog => $setup->{log_file}, |
| 401 |
Trace => 'DEFAULT:10 ban:20 ssh2:20 sftp:20', |
| 402 |
|
| 403 |
AuthUserFile => $setup->{auth_user_file}, |
| 404 |
AuthGroupFile => $setup->{auth_group_file}, |
| 405 |
AuthOrder => 'mod_auth_file.c', |
| 406 |
|
| 407 |
MaxLoginAttempts => 1, |
| 408 |
|
| 409 |
IfModules => { |
| 410 |
'mod_ban.c' => { |
| 411 |
BanEngine => 'on', |
| 412 |
BanLog => $setup->{log_file}, |
| 413 |
|
| 414 |
# This says to ban a user which exceeds the MaxLoginAttempts |
| 415 |
# limit once within the last 1 minute will be banned for 10 secs |
| 416 |
BanOnEvent => 'MaxLoginAttemptsFromUser 1/00:01:00 00:00:10', |
| 417 |
|
| 418 |
BanTable => $ban_tab, |
| 419 |
}, |
| 420 |
|
| 421 |
'mod_delay.c' => { |
| 422 |
DelayEngine => 'off', |
| 423 |
}, |
| 424 |
|
| 425 |
'mod_sftp.c' => [ |
| 426 |
"SFTPEngine on", |
| 427 |
"SFTPLog $setup->{log_file}", |
| 428 |
"SFTPHostKey $rsa_host_key", |
| 429 |
"SFTPHostKey $dsa_host_key", |
| 430 |
], |
| 431 |
}, |
| 432 |
}; |
| 433 |
|
| 434 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 435 |
$config); |
| 436 |
|
| 437 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 438 |
# the child will indicate when it's done with its test by writing a message |
| 439 |
# to the parent. |
| 440 |
my ($rfh, $wfh); |
| 441 |
unless (pipe($rfh, $wfh)) { |
| 442 |
die("Can't open pipe: $!"); |
| 443 |
} |
| 444 |
|
| 445 |
require Net::SSH2; |
| 446 |
|
| 447 |
my $ex; |
| 448 |
|
| 449 |
# Fork child |
| 450 |
$self->handle_sigchld(); |
| 451 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 452 |
if ($pid) { |
| 453 |
eval { |
| 454 |
my $ssh2 = Net::SSH2->new(); |
| 455 |
|
| 456 |
sleep(1); |
| 457 |
|
| 458 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 459 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 460 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 461 |
} |
| 462 |
|
| 463 |
if ($ssh2->auth_password($setup->{user}, 'foo')) { |
| 464 |
die("Password auth (wrong password) succeeded unexpectedly"); |
| 465 |
} |
| 466 |
|
| 467 |
$ssh2->disconnect(); |
| 468 |
|
| 469 |
# Now try the correct password for authentication, which would succeed -- |
| 470 |
# except that mod_ban should now have a ban in place, and prevent us |
| 471 |
# from doing this. Note that we need to create a new connection to test |
| 472 |
# the mod_ban rules. |
| 473 |
|
| 474 |
$ssh2 = Net::SSH2->new(); |
| 475 |
|
| 476 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 477 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 478 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 479 |
} |
| 480 |
|
| 481 |
if ($ssh2->auth_password($setup->{user}, $setup->{passwd})) { |
| 482 |
die("Password auth (wrong password) succeeded unexpectedly"); |
| 483 |
} |
| 484 |
|
| 485 |
$ssh2->disconnect(); |
| 486 |
}; |
| 487 |
if ($@) { |
| 488 |
$ex = $@; |
| 489 |
} |
| 490 |
|
| 491 |
$wfh->print("done\n"); |
| 492 |
$wfh->flush(); |
| 493 |
|
| 494 |
} else { |
| 495 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 496 |
if ($@) { |
| 497 |
warn($@); |
| 498 |
exit 1; |
| 499 |
} |
| 500 |
|
| 501 |
exit 0; |
| 502 |
} |
| 503 |
|
| 504 |
# Stop server |
| 505 |
server_stop($setup->{pid_file}); |
| 506 |
$self->assert_child_ok($pid); |
| 507 |
|
| 508 |
test_cleanup($setup->{log_file}, $ex); |
| 509 |
} |
| 510 |
|
| 511 |
1; |