| 1 |
package ProFTPD::Tests::Config::Limit::SubDirectories; |
| 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 |
subdirs_mkd_denied_pwd_allowed_bug3077 => { |
| 19 |
order => ++$order, |
| 20 |
test_class => [qw(bug forking)], |
| 21 |
}, |
| 22 |
|
| 23 |
subdirs_mkd_denied_limit_xmkd_bug3077 => { |
| 24 |
order => ++$order, |
| 25 |
test_class => [qw(bug forking)], |
| 26 |
}, |
| 27 |
|
| 28 |
subdirs_xmkd_allowed_limit_mkd_bug3077 => { |
| 29 |
order => ++$order, |
| 30 |
test_class => [qw(bug forking)], |
| 31 |
}, |
| 32 |
|
| 33 |
}; |
| 34 |
|
| 35 |
sub new { |
| 36 |
return shift()->SUPER::new(@_); |
| 37 |
} |
| 38 |
|
| 39 |
sub list_tests { |
| 40 |
return testsuite_get_runnable_tests($TESTS); |
| 41 |
} |
| 42 |
|
| 43 |
sub subdirs_mkd_denied_pwd_allowed_bug3077 { |
| 44 |
my $self = shift; |
| 45 |
my $tmpdir = $self->{tmpdir}; |
| 46 |
|
| 47 |
my $config_file = "$tmpdir/limit.conf"; |
| 48 |
my $pid_file = File::Spec->rel2abs("$tmpdir/limit.pid"); |
| 49 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/limit.scoreboard"); |
| 50 |
|
| 51 |
my $log_file = File::Spec->rel2abs('tests.log'); |
| 52 |
|
| 53 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/limit.passwd"); |
| 54 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/limit.group"); |
| 55 |
|
| 56 |
my $user = 'proftpd'; |
| 57 |
my $passwd = 'test'; |
| 58 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 59 |
my $uid = 500; |
| 60 |
my $gid = 500; |
| 61 |
|
| 62 |
my $sub_dir = File::Spec->rel2abs("$tmpdir/foo"); |
| 63 |
|
| 64 |
# Make sure that, if we're running as root, that the home directory has |
| 65 |
# permissions/privs set for the account we create |
| 66 |
if ($< == 0) { |
| 67 |
unless (chmod(0755, $home_dir)) { |
| 68 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 69 |
} |
| 70 |
|
| 71 |
unless (chown($uid, $gid, $home_dir)) { |
| 72 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 77 |
'/bin/bash'); |
| 78 |
auth_group_write($auth_group_file, 'ftpd', $gid, $user); |
| 79 |
|
| 80 |
my $config = { |
| 81 |
PidFile => $pid_file, |
| 82 |
ScoreboardFile => $scoreboard_file, |
| 83 |
SystemLog => $log_file, |
| 84 |
|
| 85 |
AuthUserFile => $auth_user_file, |
| 86 |
AuthGroupFile => $auth_group_file, |
| 87 |
AuthOrder => 'mod_auth_file.c', |
| 88 |
|
| 89 |
IfModules => { |
| 90 |
'mod_delay.c' => { |
| 91 |
DelayEngine => 'off', |
| 92 |
}, |
| 93 |
}, |
| 94 |
|
| 95 |
Limit => { |
| 96 |
'ALL' => { |
| 97 |
DenyAll => '', |
| 98 |
}, |
| 99 |
|
| 100 |
'CDUP CWD PWD LIST NLST RNFR RNTO TYPE' => { |
| 101 |
AllowAll => '', |
| 102 |
}, |
| 103 |
}, |
| 104 |
}; |
| 105 |
|
| 106 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 107 |
|
| 108 |
if (open(my $fh, ">> $config_file")) { |
| 109 |
print $fh <<EOT; |
| 110 |
<Directory $home_dir> |
| 111 |
<Limit STOR STOU RETR DELE PWD RMD> |
| 112 |
AllowAll |
| 113 |
</Limit> |
| 114 |
</Directory> |
| 115 |
EOT |
| 116 |
unless (close($fh)) { |
| 117 |
die("Can't write $config_file: $!"); |
| 118 |
} |
| 119 |
|
| 120 |
} else { |
| 121 |
die("Can't open $config_file: $!"); |
| 122 |
} |
| 123 |
|
| 124 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 125 |
# the child will indicate when it's done with its test by writing a message |
| 126 |
# to the parent. |
| 127 |
my ($rfh, $wfh); |
| 128 |
unless (pipe($rfh, $wfh)) { |
| 129 |
die("Can't open pipe: $!"); |
| 130 |
} |
| 131 |
|
| 132 |
my $ex; |
| 133 |
|
| 134 |
# Fork child |
| 135 |
$self->handle_sigchld(); |
| 136 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 137 |
if ($pid) { |
| 138 |
eval { |
| 139 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 140 |
$client->login($user, $passwd); |
| 141 |
|
| 142 |
my ($resp_code, $resp_msg); |
| 143 |
eval { $client->mkd($sub_dir) }; |
| 144 |
unless ($@) { |
| 145 |
die("MKD succeeded unexpectedly"); |
| 146 |
} |
| 147 |
|
| 148 |
$resp_code = $client->response_code(); |
| 149 |
$resp_msg = $client->response_msg(); |
| 150 |
|
| 151 |
my $expected; |
| 152 |
|
| 153 |
$expected = 550; |
| 154 |
$self->assert($expected == $resp_code, |
| 155 |
test_msg("Expected $expected, got $resp_code")); |
| 156 |
|
| 157 |
$expected = "$sub_dir: Permission denied"; |
| 158 |
$self->assert($expected eq $resp_msg, |
| 159 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 160 |
|
| 161 |
($resp_code, $resp_msg) = $client->pwd(); |
| 162 |
|
| 163 |
$expected = 257; |
| 164 |
$self->assert($expected == $resp_code, |
| 165 |
test_msg("Expected $expected, got $resp_code")); |
| 166 |
|
| 167 |
$expected = "\"$home_dir\" is the current directory"; |
| 168 |
$self->assert($expected eq $resp_msg, |
| 169 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 170 |
}; |
| 171 |
|
| 172 |
if ($@) { |
| 173 |
$ex = $@; |
| 174 |
} |
| 175 |
|
| 176 |
$wfh->print("done\n"); |
| 177 |
$wfh->flush(); |
| 178 |
|
| 179 |
} else { |
| 180 |
eval { server_wait($config_file, $rfh) }; |
| 181 |
if ($@) { |
| 182 |
warn($@); |
| 183 |
exit 1; |
| 184 |
} |
| 185 |
|
| 186 |
exit 0; |
| 187 |
} |
| 188 |
|
| 189 |
# Stop server |
| 190 |
server_stop($pid_file); |
| 191 |
|
| 192 |
$self->assert_child_ok($pid); |
| 193 |
|
| 194 |
if ($ex) { |
| 195 |
die($ex); |
| 196 |
} |
| 197 |
|
| 198 |
unlink($log_file); |
| 199 |
} |
| 200 |
|
| 201 |
sub subdirs_mkd_denied_limit_xmkd_bug3077 { |
| 202 |
my $self = shift; |
| 203 |
my $tmpdir = $self->{tmpdir}; |
| 204 |
|
| 205 |
my $config_file = "$tmpdir/limit.conf"; |
| 206 |
my $pid_file = File::Spec->rel2abs("$tmpdir/limit.pid"); |
| 207 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/limit.scoreboard"); |
| 208 |
|
| 209 |
my $log_file = File::Spec->rel2abs('tests.log'); |
| 210 |
|
| 211 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/limit.passwd"); |
| 212 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/limit.group"); |
| 213 |
|
| 214 |
my $user = 'proftpd'; |
| 215 |
my $passwd = 'test'; |
| 216 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 217 |
my $uid = 500; |
| 218 |
my $gid = 500; |
| 219 |
|
| 220 |
my $sub_dir = File::Spec->rel2abs("$tmpdir/foo"); |
| 221 |
|
| 222 |
# Make sure that, if we're running as root, that the home directory has |
| 223 |
# permissions/privs set for the account we create |
| 224 |
if ($< == 0) { |
| 225 |
unless (chmod(0755, $home_dir)) { |
| 226 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 227 |
} |
| 228 |
|
| 229 |
unless (chown($uid, $gid, $home_dir)) { |
| 230 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 235 |
'/bin/bash'); |
| 236 |
auth_group_write($auth_group_file, 'ftpd', $gid, $user); |
| 237 |
|
| 238 |
my $config = { |
| 239 |
PidFile => $pid_file, |
| 240 |
ScoreboardFile => $scoreboard_file, |
| 241 |
SystemLog => $log_file, |
| 242 |
|
| 243 |
AuthUserFile => $auth_user_file, |
| 244 |
AuthGroupFile => $auth_group_file, |
| 245 |
AuthOrder => 'mod_auth_file.c', |
| 246 |
|
| 247 |
IfModules => { |
| 248 |
'mod_delay.c' => { |
| 249 |
DelayEngine => 'off', |
| 250 |
}, |
| 251 |
}, |
| 252 |
|
| 253 |
}; |
| 254 |
|
| 255 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 256 |
|
| 257 |
if (open(my $fh, ">> $config_file")) { |
| 258 |
print $fh <<EOT; |
| 259 |
<Directory $home_dir> |
| 260 |
<Limit XMKD> |
| 261 |
DenyAll |
| 262 |
</Limit> |
| 263 |
</Directory> |
| 264 |
EOT |
| 265 |
unless (close($fh)) { |
| 266 |
die("Can't write $config_file: $!"); |
| 267 |
} |
| 268 |
|
| 269 |
} else { |
| 270 |
die("Can't open $config_file: $!"); |
| 271 |
} |
| 272 |
|
| 273 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 274 |
# the child will indicate when it's done with its test by writing a message |
| 275 |
# to the parent. |
| 276 |
my ($rfh, $wfh); |
| 277 |
unless (pipe($rfh, $wfh)) { |
| 278 |
die("Can't open pipe: $!"); |
| 279 |
} |
| 280 |
|
| 281 |
my $ex; |
| 282 |
|
| 283 |
# Fork child |
| 284 |
$self->handle_sigchld(); |
| 285 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 286 |
if ($pid) { |
| 287 |
eval { |
| 288 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 289 |
$client->login($user, $passwd); |
| 290 |
|
| 291 |
my ($resp_code, $resp_msg); |
| 292 |
eval { $client->mkd($sub_dir) }; |
| 293 |
unless ($@) { |
| 294 |
die("MKD succeeded unexpectedly"); |
| 295 |
} |
| 296 |
|
| 297 |
$resp_code = $client->response_code(); |
| 298 |
$resp_msg = $client->response_msg(); |
| 299 |
|
| 300 |
my $expected; |
| 301 |
|
| 302 |
$expected = 550; |
| 303 |
$self->assert($expected == $resp_code, |
| 304 |
test_msg("Expected $expected, got $resp_code")); |
| 305 |
|
| 306 |
$expected = "$sub_dir: Permission denied"; |
| 307 |
$self->assert($expected eq $resp_msg, |
| 308 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 309 |
|
| 310 |
($resp_code, $resp_msg) = $client->pwd(); |
| 311 |
|
| 312 |
$expected = 257; |
| 313 |
$self->assert($expected == $resp_code, |
| 314 |
test_msg("Expected $expected, got $resp_code")); |
| 315 |
|
| 316 |
$expected = "\"$home_dir\" is the current directory"; |
| 317 |
$self->assert($expected eq $resp_msg, |
| 318 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 319 |
}; |
| 320 |
|
| 321 |
if ($@) { |
| 322 |
$ex = $@; |
| 323 |
} |
| 324 |
|
| 325 |
$wfh->print("done\n"); |
| 326 |
$wfh->flush(); |
| 327 |
|
| 328 |
} else { |
| 329 |
eval { server_wait($config_file, $rfh) }; |
| 330 |
if ($@) { |
| 331 |
warn($@); |
| 332 |
exit 1; |
| 333 |
} |
| 334 |
|
| 335 |
exit 0; |
| 336 |
} |
| 337 |
|
| 338 |
# Stop server |
| 339 |
server_stop($pid_file); |
| 340 |
|
| 341 |
$self->assert_child_ok($pid); |
| 342 |
|
| 343 |
if ($ex) { |
| 344 |
die($ex); |
| 345 |
} |
| 346 |
|
| 347 |
unlink($log_file); |
| 348 |
} |
| 349 |
|
| 350 |
sub subdirs_xmkd_allowed_limit_mkd_bug3077 { |
| 351 |
my $self = shift; |
| 352 |
my $tmpdir = $self->{tmpdir}; |
| 353 |
|
| 354 |
my $config_file = "$tmpdir/limit.conf"; |
| 355 |
my $pid_file = File::Spec->rel2abs("$tmpdir/limit.pid"); |
| 356 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/limit.scoreboard"); |
| 357 |
|
| 358 |
my $log_file = File::Spec->rel2abs('tests.log'); |
| 359 |
|
| 360 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/limit.passwd"); |
| 361 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/limit.group"); |
| 362 |
|
| 363 |
my $user = 'proftpd'; |
| 364 |
my $passwd = 'test'; |
| 365 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 366 |
my $uid = 500; |
| 367 |
my $gid = 500; |
| 368 |
|
| 369 |
my $sub_dir = File::Spec->rel2abs("$tmpdir/foo"); |
| 370 |
|
| 371 |
# Make sure that, if we're running as root, that the home directory has |
| 372 |
# permissions/privs set for the account we create |
| 373 |
if ($< == 0) { |
| 374 |
unless (chmod(0755, $home_dir)) { |
| 375 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 376 |
} |
| 377 |
|
| 378 |
unless (chown($uid, $gid, $home_dir)) { |
| 379 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 384 |
'/bin/bash'); |
| 385 |
auth_group_write($auth_group_file, 'ftpd', $gid, $user); |
| 386 |
|
| 387 |
my $config = { |
| 388 |
PidFile => $pid_file, |
| 389 |
ScoreboardFile => $scoreboard_file, |
| 390 |
SystemLog => $log_file, |
| 391 |
|
| 392 |
AuthUserFile => $auth_user_file, |
| 393 |
AuthGroupFile => $auth_group_file, |
| 394 |
AuthOrder => 'mod_auth_file.c', |
| 395 |
|
| 396 |
IfModules => { |
| 397 |
'mod_delay.c' => { |
| 398 |
DelayEngine => 'off', |
| 399 |
}, |
| 400 |
}, |
| 401 |
|
| 402 |
}; |
| 403 |
|
| 404 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 405 |
|
| 406 |
if (open(my $fh, ">> $config_file")) { |
| 407 |
print $fh <<EOT; |
| 408 |
<Directory $home_dir> |
| 409 |
<Limit WRITE> |
| 410 |
DenyAll |
| 411 |
</Limit> |
| 412 |
|
| 413 |
<Limit MKD> |
| 414 |
AllowAll |
| 415 |
</Limit> |
| 416 |
</Directory> |
| 417 |
EOT |
| 418 |
unless (close($fh)) { |
| 419 |
die("Can't write $config_file: $!"); |
| 420 |
} |
| 421 |
|
| 422 |
} else { |
| 423 |
die("Can't open $config_file: $!"); |
| 424 |
} |
| 425 |
|
| 426 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 427 |
# the child will indicate when it's done with its test by writing a message |
| 428 |
# to the parent. |
| 429 |
my ($rfh, $wfh); |
| 430 |
unless (pipe($rfh, $wfh)) { |
| 431 |
die("Can't open pipe: $!"); |
| 432 |
} |
| 433 |
|
| 434 |
my $ex; |
| 435 |
|
| 436 |
# Fork child |
| 437 |
$self->handle_sigchld(); |
| 438 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 439 |
if ($pid) { |
| 440 |
eval { |
| 441 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 442 |
$client->login($user, $passwd); |
| 443 |
|
| 444 |
my ($resp_code, $resp_msg); |
| 445 |
($resp_code, $resp_msg) = $client->xmkd($sub_dir); |
| 446 |
|
| 447 |
my $expected; |
| 448 |
|
| 449 |
$expected = 257; |
| 450 |
$self->assert($expected == $resp_code, |
| 451 |
test_msg("Expected $expected, got $resp_code")); |
| 452 |
|
| 453 |
$expected = "\"$sub_dir\" - Directory successfully created"; |
| 454 |
$self->assert($expected eq $resp_msg, |
| 455 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 456 |
}; |
| 457 |
|
| 458 |
if ($@) { |
| 459 |
$ex = $@; |
| 460 |
} |
| 461 |
|
| 462 |
$wfh->print("done\n"); |
| 463 |
$wfh->flush(); |
| 464 |
|
| 465 |
} else { |
| 466 |
eval { server_wait($config_file, $rfh) }; |
| 467 |
if ($@) { |
| 468 |
warn($@); |
| 469 |
exit 1; |
| 470 |
} |
| 471 |
|
| 472 |
exit 0; |
| 473 |
} |
| 474 |
|
| 475 |
# Stop server |
| 476 |
server_stop($pid_file); |
| 477 |
|
| 478 |
$self->assert_child_ok($pid); |
| 479 |
|
| 480 |
if ($ex) { |
| 481 |
die($ex); |
| 482 |
} |
| 483 |
|
| 484 |
unlink($log_file); |
| 485 |
} |
| 486 |
|
| 487 |
1; |