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