| 1 |
package ProFTPD::Tests::Config::MaxLoginAttempts; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use File::Spec; |
| 8 |
use IO::Handle; |
| 9 |
|
| 10 |
use ProFTPD::TestSuite::FTP; |
| 11 |
use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite); |
| 12 |
|
| 13 |
$| = 1; |
| 14 |
|
| 15 |
my $order = 0; |
| 16 |
|
| 17 |
my $TESTS = { |
| 18 |
maxloginattempts_one => { |
| 19 |
order => ++$order, |
| 20 |
test_class => [qw(forking)], |
| 21 |
}, |
| 22 |
|
| 23 |
maxloginattempts_absent => { |
| 24 |
order => ++$order, |
| 25 |
test_class => [qw(forking)], |
| 26 |
}, |
| 27 |
|
| 28 |
}; |
| 29 |
|
| 30 |
sub new { |
| 31 |
return shift()->SUPER::new(@_); |
| 32 |
} |
| 33 |
|
| 34 |
sub list_tests { |
| 35 |
return testsuite_get_runnable_tests($TESTS); |
| 36 |
} |
| 37 |
|
| 38 |
sub maxloginattempts_one { |
| 39 |
my $self = shift; |
| 40 |
my $tmpdir = $self->{tmpdir}; |
| 41 |
my $setup = test_setup($tmpdir, 'config'); |
| 42 |
|
| 43 |
my $max_logins = 1; |
| 44 |
|
| 45 |
my $config = { |
| 46 |
PidFile => $setup->{pid_file}, |
| 47 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 48 |
SystemLog => $setup->{log_file}, |
| 49 |
|
| 50 |
AuthUserFile => $setup->{auth_user_file}, |
| 51 |
AuthGroupFile => $setup->{auth_group_file}, |
| 52 |
AuthOrder => 'mod_auth_file.c', |
| 53 |
|
| 54 |
MaxLoginAttempts => $max_logins, |
| 55 |
|
| 56 |
IfModules => { |
| 57 |
'mod_delay.c' => { |
| 58 |
DelayEngine => 'off', |
| 59 |
}, |
| 60 |
}, |
| 61 |
}; |
| 62 |
|
| 63 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 64 |
$config); |
| 65 |
|
| 66 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 67 |
# the child will indicate when it's done with its test by writing a message |
| 68 |
# to the parent. |
| 69 |
my ($rfh, $wfh); |
| 70 |
unless (pipe($rfh, $wfh)) { |
| 71 |
die("Can't open pipe: $!"); |
| 72 |
} |
| 73 |
|
| 74 |
my $ex; |
| 75 |
|
| 76 |
# Fork child |
| 77 |
$self->handle_sigchld(); |
| 78 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 79 |
if ($pid) { |
| 80 |
eval { |
| 81 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 82 |
|
| 83 |
eval { $client->login($setup->{user}, 'foo') }; |
| 84 |
unless ($@) { |
| 85 |
die("Logged in unexpectedly"); |
| 86 |
} |
| 87 |
|
| 88 |
my $resp_code = $client->response_code(); |
| 89 |
my $resp_msg = $client->response_msg(0); |
| 90 |
|
| 91 |
my $expected = 530; |
| 92 |
$self->assert($expected == $resp_code, |
| 93 |
test_msg("Expected response code $expected, got $resp_code")); |
| 94 |
|
| 95 |
$expected = "Login incorrect."; |
| 96 |
$self->assert($expected eq $resp_msg, |
| 97 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 98 |
|
| 99 |
# A MaxLoginAttempts of one should have caused our connection to be |
| 100 |
# closed above. |
| 101 |
|
| 102 |
eval { ($resp_code, $resp_msg) = $client->login($setup->{user}, 'foo') }; |
| 103 |
unless ($@) { |
| 104 |
die("Logged in unexpectedly ($resp_code $resp_msg)"); |
| 105 |
} |
| 106 |
|
| 107 |
$resp_code = $client->response_code(); |
| 108 |
$resp_msg = $client->response_msg(0); |
| 109 |
|
| 110 |
# Perl's Net::Cmd module uses a very non-standard 599 code to |
| 111 |
# indicate that the connection is closed, depending on version. |
| 112 |
$self->assert($resp_code == 421 || $resp_code == 599, |
| 113 |
test_msg("Expected response code 421 or 599, got $resp_code")); |
| 114 |
}; |
| 115 |
if ($@) { |
| 116 |
$ex = $@; |
| 117 |
} |
| 118 |
|
| 119 |
$wfh->print("done\n"); |
| 120 |
$wfh->flush(); |
| 121 |
|
| 122 |
} else { |
| 123 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 124 |
if ($@) { |
| 125 |
warn($@); |
| 126 |
exit 1; |
| 127 |
} |
| 128 |
|
| 129 |
exit 0; |
| 130 |
} |
| 131 |
|
| 132 |
# Stop server |
| 133 |
server_stop($setup->{pid_file}); |
| 134 |
$self->assert_child_ok($pid); |
| 135 |
|
| 136 |
# We can peruse the generated debug log messages for what we want, but |
| 137 |
# only if the TEST_VERBOSE environment variable is true. |
| 138 |
unless ($ENV{TEST_VERBOSE}) { |
| 139 |
test_cleanup($setup->{log_file}, $ex); |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
eval { |
| 144 |
if (open(my $fh, "< $setup->{log_file}")) { |
| 145 |
my $expected_post_cmd_err = 0; |
| 146 |
my $expected_log_cmd_err = 0; |
| 147 |
|
| 148 |
while (my $line = <$fh>) { |
| 149 |
chomp($line); |
| 150 |
|
| 151 |
if ($ENV{TEST_VERBOSE}) { |
| 152 |
print STDERR "# $line\n"; |
| 153 |
} |
| 154 |
|
| 155 |
if ($line =~ /POST_CMD_ERR command 'PASS/) { |
| 156 |
$expected_post_cmd_err = 1; |
| 157 |
next; |
| 158 |
} |
| 159 |
|
| 160 |
if ($line =~ /LOG_CMD_ERR command 'PASS/) { |
| 161 |
$expected_log_cmd_err = 1; |
| 162 |
next; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
close($fh); |
| 167 |
|
| 168 |
$self->assert($expected_post_cmd_err && $expected_log_cmd_err, |
| 169 |
test_msg("Did not see expected PASS POST_CMD_ERR and LOG_CMD_ERR log messages")); |
| 170 |
|
| 171 |
} else { |
| 172 |
die("Can't read $setup->{log_file}: $!"); |
| 173 |
} |
| 174 |
}; |
| 175 |
if ($@) { |
| 176 |
$ex = $@; |
| 177 |
} |
| 178 |
|
| 179 |
test_cleanup($setup->{log_file}, $ex); |
| 180 |
} |
| 181 |
|
| 182 |
sub maxloginattempts_absent { |
| 183 |
my $self = shift; |
| 184 |
my $tmpdir = $self->{tmpdir}; |
| 185 |
my $setup = test_setup($tmpdir, 'config'); |
| 186 |
|
| 187 |
my $config = { |
| 188 |
PidFile => $setup->{pid_file}, |
| 189 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 190 |
SystemLog => $setup->{log_file}, |
| 191 |
|
| 192 |
AuthUserFile => $setup->{auth_user_file}, |
| 193 |
AuthGroupFile => $setup->{auth_group_file}, |
| 194 |
AuthOrder => 'mod_auth_file.c', |
| 195 |
|
| 196 |
IfModules => { |
| 197 |
'mod_delay.c' => { |
| 198 |
DelayEngine => 'off', |
| 199 |
}, |
| 200 |
}, |
| 201 |
}; |
| 202 |
|
| 203 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 204 |
$config); |
| 205 |
|
| 206 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 207 |
# the child will indicate when it's done with its test by writing a message |
| 208 |
# to the parent. |
| 209 |
my ($rfh, $wfh); |
| 210 |
unless (pipe($rfh, $wfh)) { |
| 211 |
die("Can't open pipe: $!"); |
| 212 |
} |
| 213 |
|
| 214 |
my $ex; |
| 215 |
|
| 216 |
# Fork child |
| 217 |
$self->handle_sigchld(); |
| 218 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 219 |
if ($pid) { |
| 220 |
eval { |
| 221 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 222 |
|
| 223 |
for (my $i = 0; $i < 3; $i++) { |
| 224 |
eval { $client->login($setup->{user}, 'foo') }; |
| 225 |
unless ($@) { |
| 226 |
die("Logged in unexpectedly"); |
| 227 |
} |
| 228 |
|
| 229 |
my $resp_code = $client->response_code(); |
| 230 |
my $resp_msg = $client->response_msg(0); |
| 231 |
|
| 232 |
my $expected = 530; |
| 233 |
$self->assert($expected == $resp_code, |
| 234 |
test_msg("Expected response code $expected, got $resp_code")); |
| 235 |
|
| 236 |
$expected = "Login incorrect."; |
| 237 |
$self->assert($expected eq $resp_msg, |
| 238 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 239 |
} |
| 240 |
|
| 241 |
eval { $client->login($setup->{user}, 'foo') }; |
| 242 |
unless ($@) { |
| 243 |
die("Logged in unexpectedly"); |
| 244 |
} |
| 245 |
|
| 246 |
my $resp_code = $client->response_code(); |
| 247 |
my $resp_msg = $client->response_msg(0); |
| 248 |
|
| 249 |
# Perl's Net::Cmd module uses a very non-standard 599 code to |
| 250 |
# indicate that the connection is closed, depending on version. |
| 251 |
$self->assert($resp_code == 421 || $resp_code == 599, |
| 252 |
test_msg("Expected response code 421 or 599, got $resp_code")); |
| 253 |
}; |
| 254 |
if ($@) { |
| 255 |
$ex = $@; |
| 256 |
} |
| 257 |
|
| 258 |
$wfh->print("done\n"); |
| 259 |
$wfh->flush(); |
| 260 |
|
| 261 |
} else { |
| 262 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 263 |
if ($@) { |
| 264 |
warn($@); |
| 265 |
exit 1; |
| 266 |
} |
| 267 |
|
| 268 |
exit 0; |
| 269 |
} |
| 270 |
|
| 271 |
# Stop server |
| 272 |
server_stop($setup->{pid_file}); |
| 273 |
$self->assert_child_ok($pid); |
| 274 |
|
| 275 |
test_cleanup($setup->{log_file}, $ex); |
| 276 |
} |
| 277 |
|
| 278 |
1; |