| 1 |
package ProFTPD::Tests::Signals::HUP; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use File::Path qw(mkpath); |
| 8 |
use File::Spec; |
| 9 |
use IO::Handle; |
| 10 |
use Time::HiRes qw(gettimeofday tv_interval); |
| 11 |
|
| 12 |
use ProFTPD::TestSuite::FTP; |
| 13 |
use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite); |
| 14 |
|
| 15 |
$| = 1; |
| 16 |
|
| 17 |
my $order = 0; |
| 18 |
|
| 19 |
my $TESTS = { |
| 20 |
hup_daemon_ok => { |
| 21 |
order => ++$order, |
| 22 |
test_class => [qw(bug)], |
| 23 |
}, |
| 24 |
|
| 25 |
hup_directory_bug3610 => { |
| 26 |
order => ++$order, |
| 27 |
test_class => [qw(bug mod_sql_sqlite)], |
| 28 |
}, |
| 29 |
|
| 30 |
hup_allowoverwrite_bug3740 => { |
| 31 |
order => ++$order, |
| 32 |
test_class => [qw(bug)], |
| 33 |
}, |
| 34 |
|
| 35 |
}; |
| 36 |
|
| 37 |
sub new { |
| 38 |
return shift()->SUPER::new(@_); |
| 39 |
} |
| 40 |
|
| 41 |
sub list_tests { |
| 42 |
return testsuite_get_runnable_tests($TESTS); |
| 43 |
} |
| 44 |
|
| 45 |
sub hup_daemon_ok { |
| 46 |
my $self = shift; |
| 47 |
my $tmpdir = $self->{tmpdir}; |
| 48 |
my $setup = test_setup($tmpdir, 'signals'); |
| 49 |
|
| 50 |
my $config = { |
| 51 |
PidFile => $setup->{pid_file}, |
| 52 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 53 |
SystemLog => $setup->{log_file}, |
| 54 |
|
| 55 |
ServerIdent => 'on foo', |
| 56 |
|
| 57 |
IfModules => { |
| 58 |
'mod_delay.c' => { |
| 59 |
DelayEngine => 'off', |
| 60 |
}, |
| 61 |
}, |
| 62 |
}; |
| 63 |
|
| 64 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 65 |
$config); |
| 66 |
|
| 67 |
my $ex; |
| 68 |
|
| 69 |
# Start server |
| 70 |
server_start($setup->{config_file}); |
| 71 |
|
| 72 |
eval { |
| 73 |
sleep(2); |
| 74 |
|
| 75 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 76 |
|
| 77 |
my $resp_code = $client->response_code(); |
| 78 |
my $resp_msg = $client->response_msg(); |
| 79 |
|
| 80 |
my $expected = 220; |
| 81 |
$self->assert($expected == $resp_code, |
| 82 |
test_msg("Expected response code $expected, got $resp_code")); |
| 83 |
|
| 84 |
$expected = "foo"; |
| 85 |
$self->assert($expected eq $resp_msg, |
| 86 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 87 |
|
| 88 |
$client->quit(); |
| 89 |
}; |
| 90 |
if ($@) { |
| 91 |
$ex = $@; |
| 92 |
} |
| 93 |
|
| 94 |
# Now change the config a little, and send the HUP signal. Since we |
| 95 |
# are telling the running `proftpd` process to re-read its config file, |
| 96 |
# the process will continue to use its original port, even though calling |
| 97 |
# `config_write()` here will try to use a different port. And for that |
| 98 |
# reason, we do NOT want to use the port value provided by this following |
| 99 |
# `config_write()` invocation. |
| 100 |
$config->{ServerIdent} = 'on bar'; |
| 101 |
my $next_port; |
| 102 |
($next_port, $config_user, $config_group) = config_write($setup->{config_file}, $config); |
| 103 |
server_restart($setup->{pid_file}); |
| 104 |
|
| 105 |
eval { |
| 106 |
sleep(2); |
| 107 |
|
| 108 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 109 |
|
| 110 |
my $resp_code = $client->response_code(); |
| 111 |
my $resp_msg = $client->response_msg(); |
| 112 |
|
| 113 |
my $expected = 220; |
| 114 |
$self->assert($expected == $resp_code, |
| 115 |
test_msg("Expected response code $expected, got $resp_code")); |
| 116 |
|
| 117 |
$expected = "bar"; |
| 118 |
$self->assert($expected eq $resp_msg, |
| 119 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 120 |
|
| 121 |
$client->quit(); |
| 122 |
}; |
| 123 |
if ($@) { |
| 124 |
$ex = $@; |
| 125 |
} |
| 126 |
|
| 127 |
server_stop($setup->{pid_file}); |
| 128 |
test_cleanup($setup->{log_file}, $ex); |
| 129 |
} |
| 130 |
|
| 131 |
sub hup_directory_bug3610 { |
| 132 |
my $self = shift; |
| 133 |
my $tmpdir = $self->{tmpdir}; |
| 134 |
|
| 135 |
my $config_file = "$tmpdir/signals.conf"; |
| 136 |
my $pid_file = File::Spec->rel2abs("$tmpdir/signals.pid"); |
| 137 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/signals.scoreboard"); |
| 138 |
|
| 139 |
my $log_file = test_get_logfile(); |
| 140 |
|
| 141 |
my $user = 'proftpd'; |
| 142 |
|
| 143 |
my $db_file = File::Spec->rel2abs("$tmpdir/signals.db"); |
| 144 |
|
| 145 |
my $include_file = File::Spec->rel2abs("$tmpdir/include.conf"); |
| 146 |
if (open(my $fh, "> $include_file")) { |
| 147 |
|
| 148 |
for (my $i = 1; $i <= 50; $i++) { |
| 149 |
my $dir = File::Spec->rel2abs("$tmpdir/u$i"); |
| 150 |
mkpath($dir); |
| 151 |
if ($ENV{TEST_VERBOSE}) { |
| 152 |
print STDERR "# Created directory $dir\n"; |
| 153 |
} |
| 154 |
|
| 155 |
print $fh <<EOC; |
| 156 |
<Directory $dir> |
| 157 |
HideFiles _.+__\$ user !proftpdadm |
| 158 |
<Limit WRITE> |
| 159 |
Order allow,deny |
| 160 |
AllowUser $user |
| 161 |
DenyAll |
| 162 |
</Limit> |
| 163 |
</Directory> |
| 164 |
|
| 165 |
EOC |
| 166 |
|
| 167 |
for (my $j = 0; $j < 5; $j++) { |
| 168 |
my $subdir = File::Spec->rel2abs("$dir/d$j"); |
| 169 |
mkpath($subdir); |
| 170 |
if ($ENV{TEST_VERBOSE}) { |
| 171 |
print STDERR "# Created directory $subdir\n"; |
| 172 |
} |
| 173 |
|
| 174 |
print $fh <<EOC; |
| 175 |
<Directory $subdir/*> |
| 176 |
<Limit WRITE> |
| 177 |
Order allow,deny |
| 178 |
AllowUser $user |
| 179 |
DenyAll |
| 180 |
</Limit> |
| 181 |
</Directory> |
| 182 |
|
| 183 |
EOC |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
unless (close($fh)) { |
| 188 |
die("Can't write $include_file: $!"); |
| 189 |
} |
| 190 |
|
| 191 |
} else { |
| 192 |
die("Can't open $include_file: $!"); |
| 193 |
} |
| 194 |
|
| 195 |
my $config = { |
| 196 |
PidFile => $pid_file, |
| 197 |
ScoreboardFile => $scoreboard_file, |
| 198 |
SystemLog => $log_file, |
| 199 |
TraceLog => $log_file, |
| 200 |
Trace => 'config:12-12', |
| 201 |
|
| 202 |
ServerIdent => 'on foo', |
| 203 |
|
| 204 |
Global => { |
| 205 |
AccessGrantMsg => "Welcome %u", |
| 206 |
AllowOverride => 'off', |
| 207 |
ListOptions => '-al', |
| 208 |
MaxLoginAttempts => 3, |
| 209 |
PathAllowFilter => '^([A-Za-z0-9._-]|\/)+$', |
| 210 |
RequireValidShell => 'no', |
| 211 |
RootLogin => 'off', |
| 212 |
UseFtpUsers => 'off', |
| 213 |
Umask => '0002', |
| 214 |
|
| 215 |
Include => $include_file, |
| 216 |
}, |
| 217 |
|
| 218 |
IfModules => { |
| 219 |
'mod_delay.c' => { |
| 220 |
DelayEngine => 'off', |
| 221 |
}, |
| 222 |
}, |
| 223 |
}; |
| 224 |
|
| 225 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 226 |
|
| 227 |
if (open(my $fh, ">> $config_file")) { |
| 228 |
my $vhost_port1 = $port + 200; |
| 229 |
my $vhost_port2 = $port + 400; |
| 230 |
|
| 231 |
print $fh <<EOC; |
| 232 |
<Global> |
| 233 |
<Limit ALL> |
| 234 |
AllowAll |
| 235 |
</Limit> |
| 236 |
|
| 237 |
<Limit ABOR SITE_CHMOD SITE_CHGRP SITE_MKDIR SITE_RMDIR SITE_SYMLINK SITE_UTIME APPE STOU> |
| 238 |
DenyAll |
| 239 |
</Limit> |
| 240 |
|
| 241 |
<IfModule mod_sql.c> |
| 242 |
SQLEngine on |
| 243 |
|
| 244 |
SQLLogFile /var/log/proftpd/proftpd.mysql.log |
| 245 |
SQLBackend sqlite3 |
| 246 |
SQLAuthenticate users usersetfast |
| 247 |
SQLAuthTypes Plaintext Crypt |
| 248 |
SQLConnectInfo $db_file ftpuser ftppw |
| 249 |
SQLUserInfo users username password uid gid homedir shell |
| 250 |
SQLUserWhereClause "LoginAllowed = 'true' " |
| 251 |
SQLMinUserUID 1000 |
| 252 |
SQLMinUserGID 1000 |
| 253 |
SQLNegativeCache on |
| 254 |
|
| 255 |
SQLLog PASS,ERR_PASS updatecount |
| 256 |
SQLNamedQuery updatecount UPDATE "count=count+1 , datecount=now() WHERE username='%u'" users |
| 257 |
SQLLog PASS,ERR_PASS debutconnexions |
| 258 |
SQLNamedQuery debutconnexions INSERT "'','%u',CURDATE(),now(),NULL,NULL,'%L','%h','%r','%s',%P,'%{env:UNIQUE_ID}'" connexions |
| 259 |
SQLLog EXIT,ERR_EXIT finconnexions |
| 260 |
SQLNamedQuery finconnexions UPDATE "DateFinConn=CURDATE(),HFinConn=now(), CommandConn='%r', CodeConn='%s' WHERE UniqueIdFtpd='%{env:UNIQUE_ID}'" connexions |
| 261 |
|
| 262 |
SQLLog RETR,ERR_RETR,STOR,ERR_STOR,STOU,ERR_STOU,MKD,ERR_MKD,XMKD,ERR_XMKD,RMD,ERR_RMD,XRMD,ERR_XRMD,DELE,ERR_DELE,RNFR,ERR_RNFR transferts |
| 263 |
SQLNamedQuery transferts INSERT "'',CURDATE(),now(), '%f', %b, '%m', '%r','%s','%{env:UNIQUE_ID}'" transferts |
| 264 |
SQLLog RNTO,ERR_RNTO renames |
| 265 |
SQLNamedQuery renames INSERT "'',CURDATE(),now(), '%f', %b, '%m', 'RNFR %w','%s','%{env:UNIQUE_ID}'" transferts |
| 266 |
</IfModule> |
| 267 |
|
| 268 |
<IfModule mod_tls.c> |
| 269 |
</IfModule> |
| 270 |
</Global> |
| 271 |
|
| 272 |
<VirtualHost 127.0.0.1> |
| 273 |
ServerIdent on "FTPS" |
| 274 |
Port $vhost_port1 |
| 275 |
PassivePorts 49152 59151 |
| 276 |
</VirtualHost> |
| 277 |
|
| 278 |
<VirtualHost 127.0.0.1> |
| 279 |
ServerIdent on "SFTP" |
| 280 |
Port $vhost_port2 |
| 281 |
</VirtualHost> |
| 282 |
|
| 283 |
EOC |
| 284 |
unless (close($fh)) { |
| 285 |
die("Can't write $config_file: $!"); |
| 286 |
} |
| 287 |
|
| 288 |
} else { |
| 289 |
die("Can't open $config_file: $!"); |
| 290 |
} |
| 291 |
|
| 292 |
# Start server |
| 293 |
server_start($config_file); |
| 294 |
|
| 295 |
# Give the server some time to start up |
| 296 |
sleep(2); |
| 297 |
|
| 298 |
# Restart the server |
| 299 |
server_restart($pid_file); |
| 300 |
|
| 301 |
sleep(2); |
| 302 |
|
| 303 |
my $conn_start = [gettimeofday]; |
| 304 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 305 |
my $conn_elapsed = tv_interval($conn_start); |
| 306 |
if ($ENV{TEST_VERBOSE}) { |
| 307 |
print STDERR " + Connect elapsed time: $conn_elapsed\n"; |
| 308 |
} |
| 309 |
|
| 310 |
# If we can connect in under 5 secs, we've made progress on Bug#3610 |
| 311 |
my $max_elapsed = 5; |
| 312 |
$self->assert($conn_elapsed < $max_elapsed, |
| 313 |
test_msg("Expected connection elapsed time < $max_elapsed, got $conn_elapsed")); |
| 314 |
|
| 315 |
my $resp_code = $client->response_code(); |
| 316 |
my $resp_msg = $client->response_msg(); |
| 317 |
|
| 318 |
my $expected; |
| 319 |
|
| 320 |
$expected = 220; |
| 321 |
$self->assert($expected == $resp_code, |
| 322 |
test_msg("Expected $expected, got $resp_code")); |
| 323 |
|
| 324 |
$expected = "foo"; |
| 325 |
$self->assert($expected eq $resp_msg, |
| 326 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 327 |
|
| 328 |
server_stop($pid_file); |
| 329 |
unlink($log_file); |
| 330 |
} |
| 331 |
|
| 332 |
sub upload_file { |
| 333 |
my $port = shift; |
| 334 |
my $user = shift; |
| 335 |
my $passwd = shift; |
| 336 |
my $path = shift; |
| 337 |
|
| 338 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 339 |
$client->login($user, $passwd); |
| 340 |
|
| 341 |
my $conn = $client->stor_raw($path); |
| 342 |
unless ($conn) { |
| 343 |
die("Failed to STOR $path: " . $client->response_code() . " " . |
| 344 |
$client->response_msg()); |
| 345 |
} |
| 346 |
|
| 347 |
my $buf = "Hello again\n"; |
| 348 |
$conn->write($buf, length($buf), 30); |
| 349 |
eval { $conn->close() }; |
| 350 |
|
| 351 |
my $resp_code = $client->response_code(); |
| 352 |
my $resp_msg = $client->response_msg(); |
| 353 |
|
| 354 |
$client->quit(); |
| 355 |
|
| 356 |
my $expected; |
| 357 |
|
| 358 |
$expected = 226; |
| 359 |
unless ($resp_code == $expected) { |
| 360 |
die("Expected response code $expected, got $resp_code"); |
| 361 |
} |
| 362 |
|
| 363 |
$expected = "Transfer complete"; |
| 364 |
unless ($resp_msg eq $expected) { |
| 365 |
die("Expected response message '$expected', got '$resp_msg'"); |
| 366 |
} |
| 367 |
|
| 368 |
return 1; |
| 369 |
} |
| 370 |
|
| 371 |
sub hup_allowoverwrite_bug3740 { |
| 372 |
my $self = shift; |
| 373 |
my $tmpdir = $self->{tmpdir}; |
| 374 |
|
| 375 |
my $config_file = "$tmpdir/signals.conf"; |
| 376 |
my $pid_file = File::Spec->rel2abs("$tmpdir/signals.pid"); |
| 377 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/signals.scoreboard"); |
| 378 |
|
| 379 |
my $log_file = test_get_logfile(); |
| 380 |
|
| 381 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd"); |
| 382 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group"); |
| 383 |
|
| 384 |
my $user = 'proftpd'; |
| 385 |
my $passwd = 'test'; |
| 386 |
my $group = 'ftpd'; |
| 387 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 388 |
my $uid = 500; |
| 389 |
my $gid = 500; |
| 390 |
|
| 391 |
# Make sure that, if we're running as root, that the home directory has |
| 392 |
# permissions/privs set for the account we create |
| 393 |
if ($< == 0) { |
| 394 |
unless (chmod(0755, $home_dir)) { |
| 395 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 396 |
} |
| 397 |
|
| 398 |
unless (chown($uid, $gid, $home_dir)) { |
| 399 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 404 |
'/bin/bash'); |
| 405 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 406 |
|
| 407 |
my $test_file = File::Spec->rel2abs("$tmpdir/test.txt"); |
| 408 |
if (open(my $fh, "> $test_file")) { |
| 409 |
print $fh "Hello, World!\n"; |
| 410 |
|
| 411 |
unless (close($fh)) { |
| 412 |
die("Can't write $test_file: $!"); |
| 413 |
} |
| 414 |
|
| 415 |
} else { |
| 416 |
die("Can't open $test_file: $!"); |
| 417 |
} |
| 418 |
|
| 419 |
my $config = { |
| 420 |
PidFile => $pid_file, |
| 421 |
ScoreboardFile => $scoreboard_file, |
| 422 |
SystemLog => $log_file, |
| 423 |
|
| 424 |
AuthUserFile => $auth_user_file, |
| 425 |
AuthGroupFile => $auth_group_file, |
| 426 |
AuthOrder => 'mod_auth_file.c', |
| 427 |
|
| 428 |
AllowOverwrite => 'on', |
| 429 |
|
| 430 |
IfModules => { |
| 431 |
'mod_delay.c' => { |
| 432 |
DelayEngine => 'off', |
| 433 |
}, |
| 434 |
}, |
| 435 |
}; |
| 436 |
|
| 437 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 438 |
|
| 439 |
# Add a <VirtualHost> to the config. This appears to be a key factor |
| 440 |
# in causing Bug#3740, according to the bug report. |
| 441 |
if (open(my $fh, ">> $config_file")) { |
| 442 |
my $vhost_port = $port + 200; |
| 443 |
|
| 444 |
print $fh <<EOC; |
| 445 |
<VirtualHost 127.0.0.1> |
| 446 |
Port $vhost_port |
| 447 |
ServerName "Bug#3740 vhost" |
| 448 |
ServerIdent on "Bug#3740 vhost" |
| 449 |
</VirtualHost> |
| 450 |
EOC |
| 451 |
unless (close($fh)) { |
| 452 |
die("Can't write $config_file: $!"); |
| 453 |
} |
| 454 |
|
| 455 |
} else { |
| 456 |
die("Can't read $config_file: $!"); |
| 457 |
} |
| 458 |
|
| 459 |
# Start server |
| 460 |
server_start($config_file); |
| 461 |
|
| 462 |
my $nattempts = 10; |
| 463 |
|
| 464 |
for (my $i = 0; $i < $nattempts; $i++) { |
| 465 |
if ($ENV{TEST_VERBOSE}) { |
| 466 |
print STDERR "Upload attempt #", $i + 1, "..."; |
| 467 |
} |
| 468 |
|
| 469 |
eval { upload_file($port, $user, $passwd, 'test.txt') }; |
| 470 |
if ($@) { |
| 471 |
my $ex = $@; |
| 472 |
|
| 473 |
if ($ENV{TEST_VERBOSE}) { |
| 474 |
print STDERR "FAILED\n"; |
| 475 |
} |
| 476 |
|
| 477 |
server_stop($pid_file); |
| 478 |
test_append_logfile($log_file, $ex); |
| 479 |
unlink($log_file); |
| 480 |
|
| 481 |
die($ex); |
| 482 |
} |
| 483 |
|
| 484 |
if ($ENV{TEST_VERBOSE}) { |
| 485 |
print STDERR "OK\n"; |
| 486 |
} |
| 487 |
|
| 488 |
server_restart($pid_file); |
| 489 |
} |
| 490 |
|
| 491 |
server_stop($pid_file); |
| 492 |
unlink($log_file); |
| 493 |
} |
| 494 |
|
| 495 |
1; |