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