| 1 |
package ProFTPD::Tests::Modules::mod_snmp::ban; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use Data::Dumper; |
| 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 |
snmp_ban_v1_get_conn_info => { |
| 20 |
order => ++$order, |
| 21 |
test_class => [qw(forking ban snmp)], |
| 22 |
}, |
| 23 |
|
| 24 |
snmp_ban_v1_get_ban_info => { |
| 25 |
order => ++$order, |
| 26 |
test_class => [qw(forking ban snmp)], |
| 27 |
}, |
| 28 |
|
| 29 |
}; |
| 30 |
|
| 31 |
sub new { |
| 32 |
return shift()->SUPER::new(@_); |
| 33 |
} |
| 34 |
|
| 35 |
sub list_tests { |
| 36 |
# Check for the required Perl modules: |
| 37 |
# |
| 38 |
# Net-SNMP |
| 39 |
|
| 40 |
my $required = [qw( |
| 41 |
Net::SNMP |
| 42 |
)]; |
| 43 |
|
| 44 |
foreach my $req (@$required) { |
| 45 |
eval "use $req"; |
| 46 |
if ($@) { |
| 47 |
print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n |
| 48 |
"; |
| 49 |
|
| 50 |
if ($ENV{TEST_VERBOSE}) { |
| 51 |
print STDERR "Unable to load $req: $@\n"; |
| 52 |
} |
| 53 |
|
| 54 |
return qw(testsuite_empty_test); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return testsuite_get_runnable_tests($TESTS); |
| 59 |
} |
| 60 |
|
| 61 |
# Support routines |
| 62 |
|
| 63 |
sub get_ban_conn_info { |
| 64 |
my $agent_port = shift; |
| 65 |
my $snmp_community = shift; |
| 66 |
|
| 67 |
my ($snmp_sess, $snmp_err) = Net::SNMP->session( |
| 68 |
-hostname => '127.0.0.1', |
| 69 |
-port => $agent_port, |
| 70 |
-version => 'snmpv1', |
| 71 |
-community => $snmp_community, |
| 72 |
-retries => 1, |
| 73 |
-timeout => 3, |
| 74 |
-translate => 1, |
| 75 |
); |
| 76 |
unless ($snmp_sess) { |
| 77 |
die("Unable to create Net::SNMP session: $snmp_err"); |
| 78 |
} |
| 79 |
|
| 80 |
if ($ENV{TEST_VERBOSE}) { |
| 81 |
# From the Net::SNMP debug perldocs |
| 82 |
my $debug_mask = (0x02|0x10|0x20); |
| 83 |
$snmp_sess->debug($debug_mask); |
| 84 |
} |
| 85 |
|
| 86 |
# ban.connections.connectionBannedTotal |
| 87 |
my $conn_banned_total_oid = '1.3.6.1.4.1.17852.2.2.9.1.1.0'; |
| 88 |
|
| 89 |
# ban.connections.userBannedTotal |
| 90 |
my $user_banned_total_oid = '1.3.6.1.4.1.17852.2.2.9.1.2.0'; |
| 91 |
|
| 92 |
# ban.connections.hostBannedTotal |
| 93 |
my $host_banned_total_oid = '1.3.6.1.4.1.17852.2.2.9.1.3.0'; |
| 94 |
|
| 95 |
# ban.connections.classBannedTotal |
| 96 |
my $class_banned_total_oid = '1.3.6.1.4.1.17852.2.2.9.1.4.0'; |
| 97 |
|
| 98 |
my $oids = [ |
| 99 |
$conn_banned_total_oid, |
| 100 |
$user_banned_total_oid, |
| 101 |
$host_banned_total_oid, |
| 102 |
$class_banned_total_oid, |
| 103 |
]; |
| 104 |
|
| 105 |
my $snmp_resp = $snmp_sess->get_request( |
| 106 |
-varbindList => $oids, |
| 107 |
); |
| 108 |
unless ($snmp_resp) { |
| 109 |
die("No SNMP response received: " . $snmp_sess->error()); |
| 110 |
} |
| 111 |
|
| 112 |
my ($conn_banned_total, $user_banned_total, $host_banned_total, $class_banned_total); |
| 113 |
|
| 114 |
# Do we have the requested OIDs in the response? |
| 115 |
|
| 116 |
foreach my $oid (@$oids) { |
| 117 |
unless (defined($snmp_resp->{$oid})) { |
| 118 |
die("Missing required OID $oid in response"); |
| 119 |
} |
| 120 |
|
| 121 |
my $value = $snmp_resp->{$oid}; |
| 122 |
if ($ENV{TEST_VERBOSE}) { |
| 123 |
print STDERR "Requested OID $oid = $value\n"; |
| 124 |
} |
| 125 |
|
| 126 |
if ($oid eq $conn_banned_total_oid) { |
| 127 |
$conn_banned_total = $value; |
| 128 |
|
| 129 |
} elsif ($oid eq $user_banned_total_oid) { |
| 130 |
$user_banned_total = $value; |
| 131 |
|
| 132 |
} elsif ($oid eq $host_banned_total_oid) { |
| 133 |
$host_banned_total = $value; |
| 134 |
|
| 135 |
} elsif ($oid eq $class_banned_total_oid) { |
| 136 |
$class_banned_total = $value; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
$snmp_sess->close(); |
| 141 |
$snmp_sess = undef; |
| 142 |
|
| 143 |
return ($conn_banned_total, $user_banned_total, $host_banned_total, $class_banned_total); |
| 144 |
} |
| 145 |
|
| 146 |
sub get_ban_ban_info { |
| 147 |
my $agent_port = shift; |
| 148 |
my $snmp_community = shift; |
| 149 |
|
| 150 |
my ($snmp_sess, $snmp_err) = Net::SNMP->session( |
| 151 |
-hostname => '127.0.0.1', |
| 152 |
-port => $agent_port, |
| 153 |
-version => 'snmpv1', |
| 154 |
-community => $snmp_community, |
| 155 |
-retries => 1, |
| 156 |
-timeout => 3, |
| 157 |
-translate => 1, |
| 158 |
); |
| 159 |
unless ($snmp_sess) { |
| 160 |
die("Unable to create Net::SNMP session: $snmp_err"); |
| 161 |
} |
| 162 |
|
| 163 |
if ($ENV{TEST_VERBOSE}) { |
| 164 |
# From the Net::SNMP debug perldocs |
| 165 |
my $debug_mask = (0x02|0x10|0x20); |
| 166 |
$snmp_sess->debug($debug_mask); |
| 167 |
} |
| 168 |
|
| 169 |
# ban.bans.banCount |
| 170 |
my $ban_count_oid = '1.3.6.1.4.1.17852.2.2.9.2.1.0'; |
| 171 |
|
| 172 |
# ban.bans.banTotal |
| 173 |
my $ban_total_oid = '1.3.6.1.4.1.17852.2.2.9.2.2.0'; |
| 174 |
|
| 175 |
# ban.bans.userBanCount |
| 176 |
my $user_ban_count_oid = '1.3.6.1.4.1.17852.2.2.9.2.3.0'; |
| 177 |
|
| 178 |
# ban.bans.userBanTotal |
| 179 |
my $user_ban_total_oid = '1.3.6.1.4.1.17852.2.2.9.2.4.0'; |
| 180 |
|
| 181 |
# ban.bans.hostBanCount |
| 182 |
my $host_ban_count_oid = '1.3.6.1.4.1.17852.2.2.9.2.5.0'; |
| 183 |
|
| 184 |
# ban.bans.hostBanTotal |
| 185 |
my $host_ban_total_oid = '1.3.6.1.4.1.17852.2.2.9.2.6.0'; |
| 186 |
|
| 187 |
# ban.bans.classBanCount |
| 188 |
my $class_ban_count_oid = '1.3.6.1.4.1.17852.2.2.9.2.7.0'; |
| 189 |
|
| 190 |
# ban.bans.classBanTotal |
| 191 |
my $class_ban_total_oid = '1.3.6.1.4.1.17852.2.2.9.2.8.0'; |
| 192 |
|
| 193 |
my $oids = [ |
| 194 |
$ban_count_oid, |
| 195 |
$ban_total_oid, |
| 196 |
$user_ban_count_oid, |
| 197 |
$user_ban_total_oid, |
| 198 |
$host_ban_count_oid, |
| 199 |
$host_ban_total_oid, |
| 200 |
$class_ban_count_oid, |
| 201 |
$class_ban_total_oid, |
| 202 |
]; |
| 203 |
|
| 204 |
my $snmp_resp = $snmp_sess->get_request( |
| 205 |
-varbindList => $oids, |
| 206 |
); |
| 207 |
unless ($snmp_resp) { |
| 208 |
die("No SNMP response received: " . $snmp_sess->error()); |
| 209 |
} |
| 210 |
|
| 211 |
my ($ban_count, $ban_total); |
| 212 |
my ($user_ban_count, $user_ban_total); |
| 213 |
my ($host_ban_count, $host_ban_total); |
| 214 |
my ($class_ban_count, $class_ban_total); |
| 215 |
|
| 216 |
# Do we have the requested OIDs in the response? |
| 217 |
|
| 218 |
foreach my $oid (@$oids) { |
| 219 |
unless (defined($snmp_resp->{$oid})) { |
| 220 |
die("Missing required OID $oid in response"); |
| 221 |
} |
| 222 |
|
| 223 |
my $value = $snmp_resp->{$oid}; |
| 224 |
if ($ENV{TEST_VERBOSE}) { |
| 225 |
print STDERR "Requested OID $oid = $value\n"; |
| 226 |
} |
| 227 |
|
| 228 |
if ($oid eq $ban_count_oid) { |
| 229 |
$ban_count = $value; |
| 230 |
|
| 231 |
} elsif ($oid eq $ban_total_oid) { |
| 232 |
$ban_total = $value; |
| 233 |
|
| 234 |
} elsif ($oid eq $user_ban_count_oid) { |
| 235 |
$user_ban_count = $value; |
| 236 |
|
| 237 |
} elsif ($oid eq $user_ban_total_oid) { |
| 238 |
$user_ban_total = $value; |
| 239 |
|
| 240 |
} elsif ($oid eq $host_ban_count_oid) { |
| 241 |
$host_ban_count = $value; |
| 242 |
|
| 243 |
} elsif ($oid eq $host_ban_total_oid) { |
| 244 |
$host_ban_total = $value; |
| 245 |
|
| 246 |
} elsif ($oid eq $class_ban_count_oid) { |
| 247 |
$class_ban_count = $value; |
| 248 |
|
| 249 |
} elsif ($oid eq $class_ban_total_oid) { |
| 250 |
$class_ban_total = $value; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
$snmp_sess->close(); |
| 255 |
$snmp_sess = undef; |
| 256 |
|
| 257 |
return { |
| 258 |
ban_count => $ban_count, |
| 259 |
ban_total => $ban_total, |
| 260 |
user_ban_count => $user_ban_count, |
| 261 |
user_ban_total => $user_ban_total, |
| 262 |
host_ban_count => $host_ban_count, |
| 263 |
host_ban_total => $host_ban_total, |
| 264 |
class_ban_count => $class_ban_count, |
| 265 |
class_ban_total => $class_ban_total, |
| 266 |
}; |
| 267 |
} |
| 268 |
|
| 269 |
# Test cases |
| 270 |
|
| 271 |
sub snmp_ban_v1_get_conn_info { |
| 272 |
my $self = shift; |
| 273 |
my $tmpdir = $self->{tmpdir}; |
| 274 |
|
| 275 |
my $config_file = "$tmpdir/snmp.conf"; |
| 276 |
my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid"); |
| 277 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard"); |
| 278 |
|
| 279 |
my $log_file = test_get_logfile(); |
| 280 |
|
| 281 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd"); |
| 282 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group"); |
| 283 |
|
| 284 |
my $user = 'proftpd'; |
| 285 |
my $passwd = 'test'; |
| 286 |
my $group = 'ftpd'; |
| 287 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 288 |
my $uid = 500; |
| 289 |
my $gid = 500; |
| 290 |
|
| 291 |
my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab"); |
| 292 |
my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp"); |
| 293 |
|
| 294 |
# Make sure that, if we're running as root, that the home directory has |
| 295 |
# permissions/privs set for the account we create |
| 296 |
if ($< == 0) { |
| 297 |
unless (chmod(0755, $home_dir, $table_dir)) { |
| 298 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 299 |
} |
| 300 |
|
| 301 |
unless (chown($uid, $gid, $home_dir, $table_dir)) { |
| 302 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 307 |
'/bin/bash'); |
| 308 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 309 |
|
| 310 |
my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port(); |
| 311 |
my $snmp_community = "public"; |
| 312 |
|
| 313 |
my $ban_expiry_secs = 5; |
| 314 |
my $timeout_idle = 20; |
| 315 |
|
| 316 |
my $config = { |
| 317 |
TraceLog => $log_file, |
| 318 |
Trace => 'event:10 snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20', |
| 319 |
PidFile => $pid_file, |
| 320 |
ScoreboardFile => $scoreboard_file, |
| 321 |
SystemLog => $log_file, |
| 322 |
|
| 323 |
AuthUserFile => $auth_user_file, |
| 324 |
AuthGroupFile => $auth_group_file, |
| 325 |
AuthOrder => 'mod_auth_file.c', |
| 326 |
|
| 327 |
MaxLoginAttempts => 2, |
| 328 |
|
| 329 |
IfModules => { |
| 330 |
'mod_ban.c' => { |
| 331 |
BanEngine => 'on', |
| 332 |
BanLog => $log_file, |
| 333 |
|
| 334 |
# This says to ban a client which exceeds the MaxLoginAttempts |
| 335 |
# limit once within the last 1 minute will be banned for 5 secs |
| 336 |
BanOnEvent => 'MaxLoginAttempts 1/00:01:00 00:00:05', |
| 337 |
|
| 338 |
BanTable => $ban_tab, |
| 339 |
}, |
| 340 |
|
| 341 |
'mod_delay.c' => { |
| 342 |
DelayEngine => 'off', |
| 343 |
}, |
| 344 |
|
| 345 |
'mod_snmp.c' => { |
| 346 |
SNMPAgent => "master 127.0.0.1:$agent_port", |
| 347 |
SNMPCommunity => $snmp_community, |
| 348 |
SNMPEngine => 'on', |
| 349 |
SNMPLog => $log_file, |
| 350 |
SNMPTables => $table_dir, |
| 351 |
}, |
| 352 |
}, |
| 353 |
}; |
| 354 |
|
| 355 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 356 |
|
| 357 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 358 |
# the child will indicate when it's done with its test by writing a message |
| 359 |
# to the parent. |
| 360 |
my ($rfh, $wfh); |
| 361 |
unless (pipe($rfh, $wfh)) { |
| 362 |
die("Can't open pipe: $!"); |
| 363 |
} |
| 364 |
|
| 365 |
require Net::SNMP; |
| 366 |
|
| 367 |
my $ex; |
| 368 |
|
| 369 |
# Fork child |
| 370 |
$self->handle_sigchld(); |
| 371 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 372 |
if ($pid) { |
| 373 |
eval { |
| 374 |
# Give the server time to start up |
| 375 |
sleep(1); |
| 376 |
|
| 377 |
my $expected; |
| 378 |
|
| 379 |
# First, get the conn stats |
| 380 |
my ($conn_banned, $user_banned, $host_banned, $class_banned) = get_ban_conn_info($agent_port, $snmp_community); |
| 381 |
|
| 382 |
$expected = 0; |
| 383 |
$self->assert($conn_banned == $expected, |
| 384 |
test_msg("Expected connection banned total $expected, got $conn_banned")); |
| 385 |
|
| 386 |
$expected = 0; |
| 387 |
$self->assert($user_banned == $expected, |
| 388 |
test_msg("Expected user banned total $expected, got $user_banned")); |
| 389 |
|
| 390 |
$expected = 0; |
| 391 |
$self->assert($host_banned == $expected, |
| 392 |
test_msg("Expected host banned total $expected, got $host_banned")); |
| 393 |
|
| 394 |
$expected = 0; |
| 395 |
$self->assert($class_banned == $expected, |
| 396 |
test_msg("Expected class banned total $expected, got $class_banned")); |
| 397 |
|
| 398 |
# Now connect, and trigger a ban |
| 399 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 400 |
eval { $client->login($user, 'foo') }; |
| 401 |
unless ($@) { |
| 402 |
die("Login succeeded unexpectedly"); |
| 403 |
} |
| 404 |
|
| 405 |
my $resp_code = $client->response_code(); |
| 406 |
my $resp_msg = $client->response_msg(); |
| 407 |
|
| 408 |
$expected = 530; |
| 409 |
$self->assert($expected == $resp_code, |
| 410 |
test_msg("Expected response code $expected, got $resp_code")); |
| 411 |
|
| 412 |
$expected = "Login incorrect."; |
| 413 |
$self->assert($expected eq $resp_msg, |
| 414 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 415 |
|
| 416 |
eval { $client->login($user, 'foo') }; |
| 417 |
unless ($@) { |
| 418 |
die("Login succeeded unexpectedly"); |
| 419 |
} |
| 420 |
|
| 421 |
$resp_code = $client->response_code(); |
| 422 |
$resp_msg = $client->response_msg(); |
| 423 |
|
| 424 |
$expected = 530; |
| 425 |
$self->assert($expected == $resp_code, |
| 426 |
test_msg("Expected response code $expected, got $resp_code")); |
| 427 |
|
| 428 |
$expected = "Login incorrect."; |
| 429 |
$self->assert($expected eq $resp_msg, |
| 430 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 431 |
|
| 432 |
# Now try again with the correct info; we should be banned. Note |
| 433 |
# that we have to create a separate connection for this. |
| 434 |
eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, |
| 435 |
undef, 0) }; |
| 436 |
unless ($@) { |
| 437 |
die("Connect succeeded unexpectedly"); |
| 438 |
} |
| 439 |
|
| 440 |
# Double-check that the conn stats are now what we expect |
| 441 |
($conn_banned, $user_banned, $host_banned, $class_banned) = get_ban_conn_info($agent_port, $snmp_community); |
| 442 |
|
| 443 |
$expected = 1; |
| 444 |
$self->assert($conn_banned == $expected, |
| 445 |
test_msg("Expected connection banned total $expected, got $conn_banned")); |
| 446 |
|
| 447 |
$expected = 0; |
| 448 |
$self->assert($user_banned == $expected, |
| 449 |
test_msg("Expected user banned total $expected, got $user_banned")); |
| 450 |
|
| 451 |
$expected = 1; |
| 452 |
$self->assert($host_banned == $expected, |
| 453 |
test_msg("Expected host banned total $expected, got $host_banned")); |
| 454 |
|
| 455 |
$expected = 0; |
| 456 |
$self->assert($class_banned == $expected, |
| 457 |
test_msg("Expected class banned total $expected, got $class_banned")); |
| 458 |
|
| 459 |
# Now wait for the ban to expire, then try again (and check stats) |
| 460 |
sleep($ban_expiry_secs + 1); |
| 461 |
|
| 462 |
$client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 463 |
eval { $client->login($user, 'foo') }; |
| 464 |
unless ($@) { |
| 465 |
die("Login succeeded unexpectedly"); |
| 466 |
} |
| 467 |
|
| 468 |
$resp_code = $client->response_code(); |
| 469 |
$resp_msg = $client->response_msg(); |
| 470 |
|
| 471 |
$expected = 530; |
| 472 |
$self->assert($expected == $resp_code, |
| 473 |
test_msg("Expected response code $expected, got $resp_code")); |
| 474 |
|
| 475 |
$expected = "Login incorrect."; |
| 476 |
$self->assert($expected eq $resp_msg, |
| 477 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 478 |
|
| 479 |
($conn_banned, $user_banned, $host_banned, $class_banned) = get_ban_conn_info($agent_port, $snmp_community); |
| 480 |
|
| 481 |
$expected = 1; |
| 482 |
$self->assert($conn_banned == $expected, |
| 483 |
test_msg("Expected connection banned total $expected, got $conn_banned")); |
| 484 |
|
| 485 |
$expected = 0; |
| 486 |
$self->assert($user_banned == $expected, |
| 487 |
test_msg("Expected user banned total $expected, got $user_banned")); |
| 488 |
|
| 489 |
$expected = 1; |
| 490 |
$self->assert($host_banned == $expected, |
| 491 |
test_msg("Expected host banned total $expected, got $host_banned")); |
| 492 |
|
| 493 |
$expected = 0; |
| 494 |
$self->assert($class_banned == $expected, |
| 495 |
test_msg("Expected class banned total $expected, got $class_banned")); |
| 496 |
|
| 497 |
}; |
| 498 |
|
| 499 |
if ($@) { |
| 500 |
$ex = $@; |
| 501 |
} |
| 502 |
|
| 503 |
$wfh->print("done\n"); |
| 504 |
$wfh->flush(); |
| 505 |
|
| 506 |
} else { |
| 507 |
eval { server_wait($config_file, $rfh, $timeout_idle) }; |
| 508 |
if ($@) { |
| 509 |
warn($@); |
| 510 |
exit 1; |
| 511 |
} |
| 512 |
|
| 513 |
exit 0; |
| 514 |
} |
| 515 |
|
| 516 |
# Stop server |
| 517 |
server_stop($pid_file); |
| 518 |
|
| 519 |
$self->assert_child_ok($pid); |
| 520 |
|
| 521 |
if ($ex) { |
| 522 |
test_append_logfile($log_file, $ex); |
| 523 |
unlink($log_file); |
| 524 |
|
| 525 |
die($ex); |
| 526 |
} |
| 527 |
|
| 528 |
unlink($log_file); |
| 529 |
} |
| 530 |
|
| 531 |
sub snmp_ban_v1_get_ban_info { |
| 532 |
my $self = shift; |
| 533 |
my $tmpdir = $self->{tmpdir}; |
| 534 |
|
| 535 |
my $config_file = "$tmpdir/snmp.conf"; |
| 536 |
my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid"); |
| 537 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard"); |
| 538 |
|
| 539 |
my $log_file = test_get_logfile(); |
| 540 |
|
| 541 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd"); |
| 542 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group"); |
| 543 |
|
| 544 |
my $user = 'proftpd'; |
| 545 |
my $passwd = 'test'; |
| 546 |
my $group = 'ftpd'; |
| 547 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 548 |
my $uid = 500; |
| 549 |
my $gid = 500; |
| 550 |
|
| 551 |
my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab"); |
| 552 |
my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp"); |
| 553 |
|
| 554 |
# Make sure that, if we're running as root, that the home directory has |
| 555 |
# permissions/privs set for the account we create |
| 556 |
if ($< == 0) { |
| 557 |
unless (chmod(0755, $home_dir, $table_dir)) { |
| 558 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 559 |
} |
| 560 |
|
| 561 |
unless (chown($uid, $gid, $home_dir, $table_dir)) { |
| 562 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 567 |
'/bin/bash'); |
| 568 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 569 |
|
| 570 |
my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port(); |
| 571 |
my $snmp_community = "public"; |
| 572 |
|
| 573 |
my $ban_expiry_secs = 5; |
| 574 |
my $timeout_idle = 45; |
| 575 |
|
| 576 |
my $config = { |
| 577 |
TraceLog => $log_file, |
| 578 |
Trace => 'event:10 snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20', |
| 579 |
PidFile => $pid_file, |
| 580 |
ScoreboardFile => $scoreboard_file, |
| 581 |
SystemLog => $log_file, |
| 582 |
|
| 583 |
AuthUserFile => $auth_user_file, |
| 584 |
AuthGroupFile => $auth_group_file, |
| 585 |
AuthOrder => 'mod_auth_file.c', |
| 586 |
|
| 587 |
MaxClientsPerHost => 1, |
| 588 |
|
| 589 |
IfModules => { |
| 590 |
'mod_ban.c' => { |
| 591 |
BanEngine => 'on', |
| 592 |
BanLog => $log_file, |
| 593 |
|
| 594 |
# This says to ban a client which exceeds the MaxClientsPerHost |
| 595 |
# limit once within the last 2 minutes will be banned for 5 secs |
| 596 |
BanOnEvent => 'MaxClientsPerHost 1/00:02:00 00:00:05', |
| 597 |
|
| 598 |
BanTable => $ban_tab, |
| 599 |
}, |
| 600 |
|
| 601 |
'mod_delay.c' => { |
| 602 |
DelayEngine => 'off', |
| 603 |
}, |
| 604 |
|
| 605 |
'mod_snmp.c' => { |
| 606 |
SNMPAgent => "master 127.0.0.1:$agent_port", |
| 607 |
SNMPCommunity => $snmp_community, |
| 608 |
SNMPEngine => 'on', |
| 609 |
SNMPLog => $log_file, |
| 610 |
SNMPTables => $table_dir, |
| 611 |
}, |
| 612 |
}, |
| 613 |
}; |
| 614 |
|
| 615 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 616 |
|
| 617 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 618 |
# the child will indicate when it's done with its test by writing a message |
| 619 |
# to the parent. |
| 620 |
my ($rfh, $wfh); |
| 621 |
unless (pipe($rfh, $wfh)) { |
| 622 |
die("Can't open pipe: $!"); |
| 623 |
} |
| 624 |
|
| 625 |
require Net::SNMP; |
| 626 |
|
| 627 |
my $ex; |
| 628 |
|
| 629 |
# Fork child |
| 630 |
$self->handle_sigchld(); |
| 631 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 632 |
if ($pid) { |
| 633 |
eval { |
| 634 |
# Give the server time to start up |
| 635 |
sleep(1); |
| 636 |
|
| 637 |
my $expected; |
| 638 |
|
| 639 |
# First, get the ban stats |
| 640 |
my $ban_info = get_ban_ban_info($agent_port, $snmp_community); |
| 641 |
|
| 642 |
$expected = 0; |
| 643 |
$self->assert($ban_info->{ban_count} == $expected, |
| 644 |
test_msg("Expected ban count $expected, got $ban_info->{ban_count}")); |
| 645 |
|
| 646 |
$expected = 0; |
| 647 |
$self->assert($ban_info->{ban_total} == $expected, |
| 648 |
test_msg("Expected ban total $expected, got $ban_info->{ban_total}")); |
| 649 |
|
| 650 |
$expected = 0; |
| 651 |
$self->assert($ban_info->{user_ban_count} == $expected, |
| 652 |
test_msg("Expected user ban count $expected, got $ban_info->{user_ban_count}")); |
| 653 |
|
| 654 |
$expected = 0; |
| 655 |
$self->assert($ban_info->{user_ban_total} == $expected, |
| 656 |
test_msg("Expected user ban total $expected, got $ban_info->{user_ban_total}")); |
| 657 |
|
| 658 |
$expected = 0; |
| 659 |
$self->assert($ban_info->{host_ban_count} == $expected, |
| 660 |
test_msg("Expected host ban count $expected, got $ban_info->{host_ban_count}")); |
| 661 |
|
| 662 |
$expected = 0; |
| 663 |
$self->assert($ban_info->{host_ban_total} == $expected, |
| 664 |
test_msg("Expected host ban total $expected, got $ban_info->{host_ban_total}")); |
| 665 |
|
| 666 |
$expected = 0; |
| 667 |
$self->assert($ban_info->{class_ban_count} == $expected, |
| 668 |
test_msg("Expected class ban count $expected, got $ban_info->{class_ban_count}")); |
| 669 |
|
| 670 |
$expected = 0; |
| 671 |
$self->assert($ban_info->{class_ban_total} == $expected, |
| 672 |
test_msg("Expected class ban total $expected, got $ban_info->{class_ban_total}")); |
| 673 |
|
| 674 |
# Now connect, and trigger a ban |
| 675 |
my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 676 |
$client1->login($user, $passwd); |
| 677 |
|
| 678 |
my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 679 |
eval { $client2->login($user, $passwd) }; |
| 680 |
|
| 681 |
# Now try again with the correct info; we should be banned. Note |
| 682 |
# that we have to create a separate connection for this. |
| 683 |
eval { my $client3 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, |
| 684 |
undef, 0) }; |
| 685 |
unless ($@) { |
| 686 |
die("Connect succeeded unexpectedly"); |
| 687 |
} |
| 688 |
|
| 689 |
# Double-check that the ban stats are now what we expect |
| 690 |
$ban_info = get_ban_ban_info($agent_port, $snmp_community); |
| 691 |
|
| 692 |
$expected = 1; |
| 693 |
$self->assert($ban_info->{ban_count} == $expected, |
| 694 |
test_msg("Expected ban count $expected, got $ban_info->{ban_count}")); |
| 695 |
|
| 696 |
$expected = 1; |
| 697 |
$self->assert($ban_info->{ban_total} == $expected, |
| 698 |
test_msg("Expected ban total $expected, got $ban_info->{ban_total}")); |
| 699 |
|
| 700 |
$expected = 0; |
| 701 |
$self->assert($ban_info->{user_ban_count} == $expected, |
| 702 |
test_msg("Expected user ban count $expected, got $ban_info->{user_ban_count}")); |
| 703 |
|
| 704 |
$expected = 0; |
| 705 |
$self->assert($ban_info->{user_ban_total} == $expected, |
| 706 |
test_msg("Expected user ban total $expected, got $ban_info->{user_ban_total}")); |
| 707 |
|
| 708 |
$expected = 1; |
| 709 |
$self->assert($ban_info->{host_ban_count} == $expected, |
| 710 |
test_msg("Expected host ban count $expected, got $ban_info->{host_ban_count}")); |
| 711 |
|
| 712 |
$expected = 1; |
| 713 |
$self->assert($ban_info->{host_ban_total} == $expected, |
| 714 |
test_msg("Expected host ban total $expected, got $ban_info->{host_ban_total}")); |
| 715 |
|
| 716 |
$expected = 0; |
| 717 |
$self->assert($ban_info->{class_ban_count} == $expected, |
| 718 |
test_msg("Expected class ban count $expected, got $ban_info->{class_ban_count}")); |
| 719 |
|
| 720 |
$expected = 0; |
| 721 |
$self->assert($ban_info->{class_ban_total} == $expected, |
| 722 |
test_msg("Expected class ban total $expected, got $ban_info->{class_ban_total}")); |
| 723 |
|
| 724 |
# Now wait for the ban to expire, then try again (and check stats) |
| 725 |
sleep($ban_expiry_secs + 2); |
| 726 |
|
| 727 |
$client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 728 |
$client1->login($user, $passwd); |
| 729 |
$client1->quit(); |
| 730 |
|
| 731 |
# And check the ban stats one more time |
| 732 |
$ban_info = get_ban_ban_info($agent_port, $snmp_community); |
| 733 |
|
| 734 |
$expected = 0; |
| 735 |
$self->assert($ban_info->{ban_count} == $expected, |
| 736 |
test_msg("Expected ban count $expected, got $ban_info->{ban_count}")); |
| 737 |
|
| 738 |
$expected = 1; |
| 739 |
$self->assert($ban_info->{ban_total} == $expected, |
| 740 |
test_msg("Expected ban total $expected, got $ban_info->{ban_total}")); |
| 741 |
|
| 742 |
$expected = 0; |
| 743 |
$self->assert($ban_info->{user_ban_count} == $expected, |
| 744 |
test_msg("Expected user ban count $expected, got $ban_info->{user_ban_count}")); |
| 745 |
|
| 746 |
$expected = 0; |
| 747 |
$self->assert($ban_info->{user_ban_total} == $expected, |
| 748 |
test_msg("Expected user ban total $expected, got $ban_info->{user_ban_total}")); |
| 749 |
|
| 750 |
$expected = 0; |
| 751 |
$self->assert($ban_info->{host_ban_count} == $expected, |
| 752 |
test_msg("Expected host ban count $expected, got $ban_info->{host_ban_count}")); |
| 753 |
|
| 754 |
$expected = 1; |
| 755 |
$self->assert($ban_info->{host_ban_total} == $expected, |
| 756 |
test_msg("Expected host ban total $expected, got $ban_info->{host_ban_total}")); |
| 757 |
|
| 758 |
$expected = 0; |
| 759 |
$self->assert($ban_info->{class_ban_count} == $expected, |
| 760 |
test_msg("Expected class ban count $expected, got $ban_info->{class_ban_count}")); |
| 761 |
|
| 762 |
$expected = 0; |
| 763 |
$self->assert($ban_info->{class_ban_total} == $expected, |
| 764 |
test_msg("Expected class ban total $expected, got $ban_info->{class_ban_total}")); |
| 765 |
}; |
| 766 |
|
| 767 |
if ($@) { |
| 768 |
$ex = $@; |
| 769 |
} |
| 770 |
|
| 771 |
$wfh->print("done\n"); |
| 772 |
$wfh->flush(); |
| 773 |
|
| 774 |
} else { |
| 775 |
eval { server_wait($config_file, $rfh, $timeout_idle) }; |
| 776 |
if ($@) { |
| 777 |
warn($@); |
| 778 |
exit 1; |
| 779 |
} |
| 780 |
|
| 781 |
exit 0; |
| 782 |
} |
| 783 |
|
| 784 |
# Stop server |
| 785 |
server_stop($pid_file); |
| 786 |
|
| 787 |
$self->assert_child_ok($pid); |
| 788 |
|
| 789 |
if ($ex) { |
| 790 |
test_append_logfile($log_file, $ex); |
| 791 |
unlink($log_file); |
| 792 |
|
| 793 |
die($ex); |
| 794 |
} |
| 795 |
|
| 796 |
unlink($log_file); |
| 797 |
} |
| 798 |
|
| 799 |
1; |