| 1 |
package ProFTPD::Tests::Commands::DELE; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use Cwd; |
| 8 |
use File::Path qw(mkpath); |
| 9 |
use File::Spec; |
| 10 |
use IO::Handle; |
| 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 |
dele_file_ok => { |
| 21 |
order => ++$order, |
| 22 |
test_class => [qw(forking)], |
| 23 |
}, |
| 24 |
|
| 25 |
dele_fails_enoent => { |
| 26 |
order => ++$order, |
| 27 |
test_class => [qw(forking)], |
| 28 |
}, |
| 29 |
|
| 30 |
dele_fails_eperm => { |
| 31 |
order => ++$order, |
| 32 |
test_class => [qw(forking)], |
| 33 |
}, |
| 34 |
|
| 35 |
dele_fails_eisdir => { |
| 36 |
order => ++$order, |
| 37 |
test_class => [qw(forking)], |
| 38 |
}, |
| 39 |
|
| 40 |
dele_symlink_ok => { |
| 41 |
order => ++$order, |
| 42 |
test_class => [qw(forking)], |
| 43 |
}, |
| 44 |
|
| 45 |
dele_symlink_bug3754 => { |
| 46 |
order => ++$order, |
| 47 |
test_class => [qw(bug forking)], |
| 48 |
}, |
| 49 |
|
| 50 |
}; |
| 51 |
|
| 52 |
sub new { |
| 53 |
return shift()->SUPER::new(@_); |
| 54 |
} |
| 55 |
|
| 56 |
sub list_tests { |
| 57 |
return testsuite_get_runnable_tests($TESTS); |
| 58 |
} |
| 59 |
|
| 60 |
sub dele_file_ok { |
| 61 |
my $self = shift; |
| 62 |
my $tmpdir = $self->{tmpdir}; |
| 63 |
|
| 64 |
my $config_file = "$tmpdir/cmds.conf"; |
| 65 |
my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid"); |
| 66 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard"); |
| 67 |
|
| 68 |
my $log_file = test_get_logfile(); |
| 69 |
|
| 70 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd"); |
| 71 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group"); |
| 72 |
|
| 73 |
my $user = 'proftpd'; |
| 74 |
my $passwd = 'test'; |
| 75 |
my $group = 'ftpd'; |
| 76 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 77 |
my $uid = 500; |
| 78 |
my $gid = 500; |
| 79 |
|
| 80 |
my $test_file = File::Spec->rel2abs("$tmpdir/foo"); |
| 81 |
if (open(my $fh, "> $test_file")) { |
| 82 |
close($fh); |
| 83 |
|
| 84 |
} else { |
| 85 |
die("Can't create $test_file: $!"); |
| 86 |
} |
| 87 |
|
| 88 |
# Make sure that, if we're running as root, that the home directory has |
| 89 |
# permissions/privs set for the account we create |
| 90 |
if ($< == 0) { |
| 91 |
unless (chmod(0755, $home_dir)) { |
| 92 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 93 |
} |
| 94 |
|
| 95 |
unless (chown($uid, $gid, $home_dir)) { |
| 96 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 101 |
'/bin/bash'); |
| 102 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 103 |
|
| 104 |
my $config = { |
| 105 |
PidFile => $pid_file, |
| 106 |
ScoreboardFile => $scoreboard_file, |
| 107 |
SystemLog => $log_file, |
| 108 |
|
| 109 |
AuthUserFile => $auth_user_file, |
| 110 |
AuthGroupFile => $auth_group_file, |
| 111 |
AuthOrder => 'mod_auth_file.c', |
| 112 |
|
| 113 |
IfModules => { |
| 114 |
'mod_delay.c' => { |
| 115 |
DelayEngine => 'off', |
| 116 |
}, |
| 117 |
}, |
| 118 |
}; |
| 119 |
|
| 120 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 121 |
|
| 122 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 123 |
# the child will indicate when it's done with its test by writing a message |
| 124 |
# to the parent. |
| 125 |
my ($rfh, $wfh); |
| 126 |
unless (pipe($rfh, $wfh)) { |
| 127 |
die("Can't open pipe: $!"); |
| 128 |
} |
| 129 |
|
| 130 |
my $ex; |
| 131 |
|
| 132 |
# Fork child |
| 133 |
$self->handle_sigchld(); |
| 134 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 135 |
if ($pid) { |
| 136 |
eval { |
| 137 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 138 |
$client->login($user, $passwd); |
| 139 |
|
| 140 |
my ($resp_code, $resp_msg); |
| 141 |
($resp_code, $resp_msg) = $client->dele($test_file); |
| 142 |
|
| 143 |
my $expected; |
| 144 |
|
| 145 |
$expected = 250; |
| 146 |
$self->assert($expected == $resp_code, |
| 147 |
test_msg("Expected $expected, got $resp_code")); |
| 148 |
|
| 149 |
$expected = "DELE command successful"; |
| 150 |
$self->assert($expected eq $resp_msg, |
| 151 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 152 |
}; |
| 153 |
|
| 154 |
if ($@) { |
| 155 |
$ex = $@; |
| 156 |
} |
| 157 |
|
| 158 |
$wfh->print("done\n"); |
| 159 |
$wfh->flush(); |
| 160 |
|
| 161 |
} else { |
| 162 |
eval { server_wait($config_file, $rfh) }; |
| 163 |
if ($@) { |
| 164 |
warn($@); |
| 165 |
exit 1; |
| 166 |
} |
| 167 |
|
| 168 |
exit 0; |
| 169 |
} |
| 170 |
|
| 171 |
# Stop server |
| 172 |
server_stop($pid_file); |
| 173 |
|
| 174 |
$self->assert_child_ok($pid); |
| 175 |
|
| 176 |
if ($ex) { |
| 177 |
test_append_logfile($log_file, $ex); |
| 178 |
unlink($log_file); |
| 179 |
|
| 180 |
die($ex); |
| 181 |
} |
| 182 |
|
| 183 |
unlink($log_file); |
| 184 |
} |
| 185 |
|
| 186 |
sub dele_fails_enoent { |
| 187 |
my $self = shift; |
| 188 |
my $tmpdir = $self->{tmpdir}; |
| 189 |
|
| 190 |
my $config_file = "$tmpdir/cmds.conf"; |
| 191 |
my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid"); |
| 192 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard"); |
| 193 |
|
| 194 |
my $log_file = test_get_logfile(); |
| 195 |
|
| 196 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd"); |
| 197 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group"); |
| 198 |
|
| 199 |
my $user = 'proftpd'; |
| 200 |
my $passwd = 'test'; |
| 201 |
my $group = 'ftpd'; |
| 202 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 203 |
my $uid = 500; |
| 204 |
my $gid = 500; |
| 205 |
|
| 206 |
my $test_file = File::Spec->rel2abs("$tmpdir/foo"); |
| 207 |
|
| 208 |
# Make sure that, if we're running as root, that the home directory has |
| 209 |
# permissions/privs set for the account we create |
| 210 |
if ($< == 0) { |
| 211 |
unless (chmod(0755, $home_dir)) { |
| 212 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 213 |
} |
| 214 |
|
| 215 |
unless (chown($uid, $gid, $home_dir)) { |
| 216 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 221 |
'/bin/bash'); |
| 222 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 223 |
|
| 224 |
my $config = { |
| 225 |
PidFile => $pid_file, |
| 226 |
ScoreboardFile => $scoreboard_file, |
| 227 |
SystemLog => $log_file, |
| 228 |
|
| 229 |
AuthUserFile => $auth_user_file, |
| 230 |
AuthGroupFile => $auth_group_file, |
| 231 |
AuthOrder => 'mod_auth_file.c', |
| 232 |
|
| 233 |
IfModules => { |
| 234 |
'mod_delay.c' => { |
| 235 |
DelayEngine => 'off', |
| 236 |
}, |
| 237 |
}, |
| 238 |
}; |
| 239 |
|
| 240 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 241 |
|
| 242 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 243 |
# the child will indicate when it's done with its test by writing a message |
| 244 |
# to the parent. |
| 245 |
my ($rfh, $wfh); |
| 246 |
unless (pipe($rfh, $wfh)) { |
| 247 |
die("Can't open pipe: $!"); |
| 248 |
} |
| 249 |
|
| 250 |
my $ex; |
| 251 |
|
| 252 |
# Fork child |
| 253 |
$self->handle_sigchld(); |
| 254 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 255 |
if ($pid) { |
| 256 |
eval { |
| 257 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 258 |
$client->login($user, $passwd); |
| 259 |
|
| 260 |
my ($resp_code, $resp_msg); |
| 261 |
eval { ($resp_code, $resp_msg) = $client->dele($test_file) }; |
| 262 |
unless ($@) { |
| 263 |
die("DELE succeeded unexpectedly"); |
| 264 |
|
| 265 |
} else { |
| 266 |
$resp_code = $client->response_code(); |
| 267 |
$resp_msg = $client->response_msg(); |
| 268 |
} |
| 269 |
|
| 270 |
my $expected; |
| 271 |
|
| 272 |
$expected = 550; |
| 273 |
$self->assert($expected == $resp_code, |
| 274 |
test_msg("Expected $expected, got $resp_code")); |
| 275 |
|
| 276 |
$expected = "$test_file: No such file or directory"; |
| 277 |
$self->assert($expected eq $resp_msg, |
| 278 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 279 |
}; |
| 280 |
|
| 281 |
if ($@) { |
| 282 |
$ex = $@; |
| 283 |
} |
| 284 |
|
| 285 |
$wfh->print("done\n"); |
| 286 |
$wfh->flush(); |
| 287 |
|
| 288 |
} else { |
| 289 |
eval { server_wait($config_file, $rfh) }; |
| 290 |
if ($@) { |
| 291 |
warn($@); |
| 292 |
exit 1; |
| 293 |
} |
| 294 |
|
| 295 |
exit 0; |
| 296 |
} |
| 297 |
|
| 298 |
# Stop server |
| 299 |
server_stop($pid_file); |
| 300 |
|
| 301 |
$self->assert_child_ok($pid); |
| 302 |
|
| 303 |
if ($ex) { |
| 304 |
test_append_logfile($log_file, $ex); |
| 305 |
unlink($log_file); |
| 306 |
|
| 307 |
die($ex); |
| 308 |
} |
| 309 |
|
| 310 |
unlink($log_file); |
| 311 |
} |
| 312 |
|
| 313 |
sub dele_fails_eperm { |
| 314 |
my $self = shift; |
| 315 |
my $tmpdir = $self->{tmpdir}; |
| 316 |
|
| 317 |
my $config_file = "$tmpdir/cmds.conf"; |
| 318 |
my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid"); |
| 319 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard"); |
| 320 |
|
| 321 |
my $log_file = test_get_logfile(); |
| 322 |
|
| 323 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd"); |
| 324 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group"); |
| 325 |
|
| 326 |
my $user = 'proftpd'; |
| 327 |
my $passwd = 'test'; |
| 328 |
my $group = 'ftpd'; |
| 329 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 330 |
my $uid = 500; |
| 331 |
my $gid = 500; |
| 332 |
|
| 333 |
my $test_file = File::Spec->rel2abs("$tmpdir/foo"); |
| 334 |
if (open(my $fh, "> $test_file")) { |
| 335 |
close($fh); |
| 336 |
|
| 337 |
} else { |
| 338 |
die("Can't create $test_file: $!"); |
| 339 |
} |
| 340 |
|
| 341 |
# Make sure that, if we're running as root, that the home directory has |
| 342 |
# permissions/privs set for the account we create |
| 343 |
if ($< == 0) { |
| 344 |
unless (chmod(0755, $home_dir)) { |
| 345 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 346 |
} |
| 347 |
|
| 348 |
unless (chown($uid, $gid, $home_dir)) { |
| 349 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 354 |
'/bin/bash'); |
| 355 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 356 |
|
| 357 |
my $config = { |
| 358 |
PidFile => $pid_file, |
| 359 |
ScoreboardFile => $scoreboard_file, |
| 360 |
SystemLog => $log_file, |
| 361 |
|
| 362 |
AuthUserFile => $auth_user_file, |
| 363 |
AuthGroupFile => $auth_group_file, |
| 364 |
AuthOrder => 'mod_auth_file.c', |
| 365 |
|
| 366 |
IfModules => { |
| 367 |
'mod_delay.c' => { |
| 368 |
DelayEngine => 'off', |
| 369 |
}, |
| 370 |
}, |
| 371 |
}; |
| 372 |
|
| 373 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 374 |
|
| 375 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 376 |
# the child will indicate when it's done with its test by writing a message |
| 377 |
# to the parent. |
| 378 |
my ($rfh, $wfh); |
| 379 |
unless (pipe($rfh, $wfh)) { |
| 380 |
die("Can't open pipe: $!"); |
| 381 |
} |
| 382 |
|
| 383 |
my $ex; |
| 384 |
|
| 385 |
# Fork child |
| 386 |
$self->handle_sigchld(); |
| 387 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 388 |
if ($pid) { |
| 389 |
eval { |
| 390 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 391 |
$client->login($user, $passwd); |
| 392 |
|
| 393 |
# Make it such that perms on the dir do not allow writes |
| 394 |
my $perms = (stat($home_dir))[2]; |
| 395 |
unless (chmod(0550, $home_dir)) { |
| 396 |
die("Failed to change perms on $home_dir: $!"); |
| 397 |
} |
| 398 |
|
| 399 |
my ($resp_code, $resp_msg); |
| 400 |
eval { ($resp_code, $resp_msg) = $client->dele($test_file) }; |
| 401 |
unless ($@) { |
| 402 |
die("DELE succeeded unexpectedly"); |
| 403 |
|
| 404 |
} else { |
| 405 |
$resp_code = $client->response_code(); |
| 406 |
$resp_msg = $client->response_msg(); |
| 407 |
} |
| 408 |
|
| 409 |
chmod($perms, $home_dir); |
| 410 |
|
| 411 |
my $expected; |
| 412 |
|
| 413 |
$expected = 550; |
| 414 |
$self->assert($expected == $resp_code, |
| 415 |
test_msg("Expected $expected, got $resp_code")); |
| 416 |
|
| 417 |
$expected = "\/foo: (Operation not permitted|Permission denied)"; |
| 418 |
$self->assert(qr/$expected/, $resp_msg, |
| 419 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 420 |
}; |
| 421 |
|
| 422 |
if ($@) { |
| 423 |
$ex = $@; |
| 424 |
} |
| 425 |
|
| 426 |
$wfh->print("done\n"); |
| 427 |
$wfh->flush(); |
| 428 |
|
| 429 |
} else { |
| 430 |
eval { server_wait($config_file, $rfh) }; |
| 431 |
if ($@) { |
| 432 |
warn($@); |
| 433 |
exit 1; |
| 434 |
} |
| 435 |
|
| 436 |
exit 0; |
| 437 |
} |
| 438 |
|
| 439 |
# Stop server |
| 440 |
server_stop($pid_file); |
| 441 |
|
| 442 |
$self->assert_child_ok($pid); |
| 443 |
|
| 444 |
if ($ex) { |
| 445 |
test_append_logfile($log_file, $ex); |
| 446 |
unlink($log_file); |
| 447 |
|
| 448 |
die($ex); |
| 449 |
} |
| 450 |
|
| 451 |
unlink($log_file); |
| 452 |
} |
| 453 |
|
| 454 |
sub dele_fails_eisdir { |
| 455 |
my $self = shift; |
| 456 |
my $tmpdir = $self->{tmpdir}; |
| 457 |
|
| 458 |
my $config_file = "$tmpdir/cmds.conf"; |
| 459 |
my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid"); |
| 460 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard"); |
| 461 |
|
| 462 |
my $log_file = test_get_logfile(); |
| 463 |
|
| 464 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd"); |
| 465 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group"); |
| 466 |
|
| 467 |
my $user = 'proftpd'; |
| 468 |
my $passwd = 'test'; |
| 469 |
my $group = 'ftpd'; |
| 470 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 471 |
my $uid = 500; |
| 472 |
my $gid = 500; |
| 473 |
|
| 474 |
my $test_dir = File::Spec->rel2abs("$tmpdir/foo"); |
| 475 |
mkpath($test_dir); |
| 476 |
|
| 477 |
# Make sure that, if we're running as root, that the home directory has |
| 478 |
# permissions/privs set for the account we create |
| 479 |
if ($< == 0) { |
| 480 |
unless (chmod(0755, $home_dir)) { |
| 481 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 482 |
} |
| 483 |
|
| 484 |
unless (chown($uid, $gid, $home_dir)) { |
| 485 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 490 |
'/bin/bash'); |
| 491 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 492 |
|
| 493 |
my $config = { |
| 494 |
PidFile => $pid_file, |
| 495 |
ScoreboardFile => $scoreboard_file, |
| 496 |
SystemLog => $log_file, |
| 497 |
|
| 498 |
AuthUserFile => $auth_user_file, |
| 499 |
AuthGroupFile => $auth_group_file, |
| 500 |
AuthOrder => 'mod_auth_file.c', |
| 501 |
|
| 502 |
IfModules => { |
| 503 |
'mod_delay.c' => { |
| 504 |
DelayEngine => 'off', |
| 505 |
}, |
| 506 |
}, |
| 507 |
}; |
| 508 |
|
| 509 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 510 |
|
| 511 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 512 |
# the child will indicate when it's done with its test by writing a message |
| 513 |
# to the parent. |
| 514 |
my ($rfh, $wfh); |
| 515 |
unless (pipe($rfh, $wfh)) { |
| 516 |
die("Can't open pipe: $!"); |
| 517 |
} |
| 518 |
|
| 519 |
my $ex; |
| 520 |
|
| 521 |
# Fork child |
| 522 |
$self->handle_sigchld(); |
| 523 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 524 |
if ($pid) { |
| 525 |
eval { |
| 526 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 527 |
$client->login($user, $passwd); |
| 528 |
|
| 529 |
my ($resp_code, $resp_msg); |
| 530 |
eval { ($resp_code, $resp_msg) = $client->dele($test_dir) }; |
| 531 |
unless ($@) { |
| 532 |
die("DELE succeeded unexpectedly"); |
| 533 |
} |
| 534 |
|
| 535 |
$resp_code = $client->response_code(); |
| 536 |
$resp_msg = $client->response_msg(); |
| 537 |
|
| 538 |
my $expected; |
| 539 |
|
| 540 |
$expected = 550; |
| 541 |
$self->assert($expected == $resp_code, |
| 542 |
test_msg("Expected $expected, got $resp_code")); |
| 543 |
|
| 544 |
$expected = "$test_dir: Is a directory"; |
| 545 |
$self->assert($expected eq $resp_msg, |
| 546 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 547 |
}; |
| 548 |
|
| 549 |
if ($@) { |
| 550 |
$ex = $@; |
| 551 |
} |
| 552 |
|
| 553 |
$wfh->print("done\n"); |
| 554 |
$wfh->flush(); |
| 555 |
|
| 556 |
} else { |
| 557 |
eval { server_wait($config_file, $rfh) }; |
| 558 |
if ($@) { |
| 559 |
warn($@); |
| 560 |
exit 1; |
| 561 |
} |
| 562 |
|
| 563 |
exit 0; |
| 564 |
} |
| 565 |
|
| 566 |
# Stop server |
| 567 |
server_stop($pid_file); |
| 568 |
|
| 569 |
$self->assert_child_ok($pid); |
| 570 |
|
| 571 |
if ($ex) { |
| 572 |
test_append_logfile($log_file, $ex); |
| 573 |
unlink($log_file); |
| 574 |
|
| 575 |
die($ex); |
| 576 |
} |
| 577 |
|
| 578 |
unlink($log_file); |
| 579 |
} |
| 580 |
|
| 581 |
sub dele_symlink_ok { |
| 582 |
my $self = shift; |
| 583 |
my $tmpdir = $self->{tmpdir}; |
| 584 |
my $setup = test_setup($tmpdir, 'cmds'); |
| 585 |
|
| 586 |
my $test_file = File::Spec->rel2abs("$tmpdir/foo"); |
| 587 |
if (open(my $fh, "> $test_file")) { |
| 588 |
close($fh); |
| 589 |
|
| 590 |
} else { |
| 591 |
die("Can't create $test_file: $!"); |
| 592 |
} |
| 593 |
|
| 594 |
# Create a symlink to the test file |
| 595 |
my $cwd = getcwd(); |
| 596 |
unless (chdir($setup->{home_dir})) { |
| 597 |
die("Can't chdir to $setup->{home_dir}: $!"); |
| 598 |
} |
| 599 |
|
| 600 |
unless (symlink('./foo', 'bar')) { |
| 601 |
die("Can't symlink './foo' to 'bar': $!"); |
| 602 |
} |
| 603 |
|
| 604 |
unless (chdir($cwd)) { |
| 605 |
die("Can't chdir to $cwd: $!"); |
| 606 |
} |
| 607 |
|
| 608 |
my $test_symlink = File::Spec->rel2abs("$tmpdir/bar"); |
| 609 |
|
| 610 |
my $config = { |
| 611 |
PidFile => $setup->{pid_file}, |
| 612 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 613 |
SystemLog => $setup->{log_file}, |
| 614 |
|
| 615 |
AuthUserFile => $setup->{auth_user_file}, |
| 616 |
AuthGroupFile => $setup->{auth_group_file}, |
| 617 |
AuthOrder => 'mod_auth_file.c', |
| 618 |
|
| 619 |
IfModules => { |
| 620 |
'mod_delay.c' => { |
| 621 |
DelayEngine => 'off', |
| 622 |
}, |
| 623 |
}, |
| 624 |
}; |
| 625 |
|
| 626 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 627 |
$config); |
| 628 |
|
| 629 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 630 |
# the child will indicate when it's done with its test by writing a message |
| 631 |
# to the parent. |
| 632 |
my ($rfh, $wfh); |
| 633 |
unless (pipe($rfh, $wfh)) { |
| 634 |
die("Can't open pipe: $!"); |
| 635 |
} |
| 636 |
|
| 637 |
my $ex; |
| 638 |
|
| 639 |
# Fork child |
| 640 |
$self->handle_sigchld(); |
| 641 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 642 |
if ($pid) { |
| 643 |
eval { |
| 644 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1); |
| 645 |
$client->login($setup->{user}, $setup->{passwd}); |
| 646 |
|
| 647 |
my ($resp_code, $resp_msg) = $client->dele('bar'); |
| 648 |
$client->quit(); |
| 649 |
|
| 650 |
my $expected = 250; |
| 651 |
$self->assert($expected == $resp_code, |
| 652 |
test_msg("Expected response code $expected, got $resp_code")); |
| 653 |
|
| 654 |
$expected = "DELE command successful"; |
| 655 |
$self->assert($expected eq $resp_msg, |
| 656 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 657 |
|
| 658 |
unless (-f $test_file) { |
| 659 |
die("File $test_file does not exist as expected"); |
| 660 |
} |
| 661 |
|
| 662 |
if (-l $test_symlink) { |
| 663 |
die("Symlink $test_symlink exists unexpectedly"); |
| 664 |
} |
| 665 |
}; |
| 666 |
if ($@) { |
| 667 |
$ex = $@; |
| 668 |
} |
| 669 |
|
| 670 |
$wfh->print("done\n"); |
| 671 |
$wfh->flush(); |
| 672 |
|
| 673 |
} else { |
| 674 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 675 |
if ($@) { |
| 676 |
warn($@); |
| 677 |
exit 1; |
| 678 |
} |
| 679 |
|
| 680 |
exit 0; |
| 681 |
} |
| 682 |
|
| 683 |
# Stop server |
| 684 |
server_stop($setup->{pid_file}); |
| 685 |
$self->assert_child_ok($pid); |
| 686 |
|
| 687 |
test_cleanup($setup->{log_file}, $ex); |
| 688 |
} |
| 689 |
|
| 690 |
sub dele_symlink_bug3754 { |
| 691 |
my $self = shift; |
| 692 |
my $tmpdir = $self->{tmpdir}; |
| 693 |
my $setup = test_setup($tmpdir, 'cmds'); |
| 694 |
|
| 695 |
my $test_file = File::Spec->rel2abs("$tmpdir/foo"); |
| 696 |
if (open(my $fh, "> $test_file")) { |
| 697 |
close($fh); |
| 698 |
|
| 699 |
} else { |
| 700 |
die("Can't create $test_file: $!"); |
| 701 |
} |
| 702 |
|
| 703 |
# Create a different directory; this directory will contain the symlink |
| 704 |
# which points to the test file. We then use a <Limit> to deny writes |
| 705 |
# to the main directory containing the test file, but allow writes in the |
| 706 |
# directory containing the symlink. A DELE command for the symlink path |
| 707 |
# should still work by deleting the symlink (and NOT the target file). |
| 708 |
|
| 709 |
my $sub_dir = File::Spec->rel2abs("$tmpdir/sub.d"); |
| 710 |
mkpath($sub_dir); |
| 711 |
|
| 712 |
# Create a symlink to the test file |
| 713 |
my $cwd = getcwd(); |
| 714 |
unless (chdir($sub_dir)) { |
| 715 |
die("Can't chdir to $sub_dir: $!"); |
| 716 |
} |
| 717 |
|
| 718 |
unless (symlink('../foo', 'bar')) { |
| 719 |
die("Can't symlink '../foo' to 'bar': $!"); |
| 720 |
} |
| 721 |
|
| 722 |
unless (chdir($cwd)) { |
| 723 |
die("Can't chdir to $cwd: $!"); |
| 724 |
} |
| 725 |
|
| 726 |
my $test_symlink = File::Spec->rel2abs("$sub_dir/bar"); |
| 727 |
|
| 728 |
# Make sure that, if we're running as root, that the home directory has |
| 729 |
# permissions/privs set for the account we create |
| 730 |
if ($< == 0) { |
| 731 |
unless (chmod(0755, $sub_dir)) { |
| 732 |
die("Can't set perms on $sub_dir to 0755: $!"); |
| 733 |
} |
| 734 |
|
| 735 |
unless (chown($setup->{uid}, $setup->{gid}, $sub_dir)) { |
| 736 |
die("Can't set owner of $sub_dir to $setup->{uid}/$setup->{gid}: $!"); |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
my $config = { |
| 741 |
PidFile => $setup->{pid_file}, |
| 742 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 743 |
SystemLog => $setup->{log_file}, |
| 744 |
|
| 745 |
AuthUserFile => $setup->{auth_user_file}, |
| 746 |
AuthGroupFile => $setup->{auth_group_file}, |
| 747 |
AuthOrder => 'mod_auth_file.c', |
| 748 |
|
| 749 |
IfModules => { |
| 750 |
'mod_delay.c' => { |
| 751 |
DelayEngine => 'off', |
| 752 |
}, |
| 753 |
}, |
| 754 |
}; |
| 755 |
|
| 756 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 757 |
$config); |
| 758 |
|
| 759 |
if (open(my $fh, ">> $setup->{config_file}")) { |
| 760 |
print $fh <<EOC; |
| 761 |
<Directory ~> |
| 762 |
<Limit WRITE> |
| 763 |
DenyAll |
| 764 |
</Limit> |
| 765 |
</Directory> |
| 766 |
|
| 767 |
<Directory ~/sub.d> |
| 768 |
<Limit WRITE> |
| 769 |
AllowAll |
| 770 |
</Limit> |
| 771 |
</Directory> |
| 772 |
EOC |
| 773 |
unless (close($fh)) { |
| 774 |
die("Can't write $setup->{config_file}: $!"); |
| 775 |
} |
| 776 |
|
| 777 |
} else { |
| 778 |
die("Can't open $setup->{config_file}: $!"); |
| 779 |
} |
| 780 |
|
| 781 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 782 |
# the child will indicate when it's done with its test by writing a message |
| 783 |
# to the parent. |
| 784 |
my ($rfh, $wfh); |
| 785 |
unless (pipe($rfh, $wfh)) { |
| 786 |
die("Can't open pipe: $!"); |
| 787 |
} |
| 788 |
|
| 789 |
my $ex; |
| 790 |
|
| 791 |
# Fork child |
| 792 |
$self->handle_sigchld(); |
| 793 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 794 |
if ($pid) { |
| 795 |
eval { |
| 796 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1); |
| 797 |
$client->login($setup->{user}, $setup->{passwd}); |
| 798 |
$client->cwd('sub.d'); |
| 799 |
|
| 800 |
my ($resp_code, $resp_msg) = $client->dele('bar'); |
| 801 |
$client->quit(); |
| 802 |
|
| 803 |
my $expected = 250; |
| 804 |
$self->assert($expected == $resp_code, |
| 805 |
test_msg("Expected response code $expected, got $resp_code")); |
| 806 |
|
| 807 |
$expected = "DELE command successful"; |
| 808 |
$self->assert($expected eq $resp_msg, |
| 809 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 810 |
|
| 811 |
unless (-f $test_file) { |
| 812 |
die("File $test_file does not exist as expected"); |
| 813 |
} |
| 814 |
|
| 815 |
if (-l $test_symlink) { |
| 816 |
die("Symlink $test_symlink exists unexpectedly"); |
| 817 |
} |
| 818 |
}; |
| 819 |
if ($@) { |
| 820 |
$ex = $@; |
| 821 |
} |
| 822 |
|
| 823 |
$wfh->print("done\n"); |
| 824 |
$wfh->flush(); |
| 825 |
|
| 826 |
} else { |
| 827 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 828 |
if ($@) { |
| 829 |
warn($@); |
| 830 |
exit 1; |
| 831 |
} |
| 832 |
|
| 833 |
exit 0; |
| 834 |
} |
| 835 |
|
| 836 |
# Stop server |
| 837 |
server_stop($setup->{pid_file}); |
| 838 |
$self->assert_child_ok($pid); |
| 839 |
|
| 840 |
test_cleanup($setup->{log_file}, $ex); |
| 841 |
} |
| 842 |
|
| 843 |
1; |