| 1 |
package ProFTPD::TestSuite::Utils; |
| 2 |
|
| 3 |
use strict; |
| 4 |
|
| 5 |
use Carp; |
| 6 |
use File::Path; |
| 7 |
use File::Spec; |
| 8 |
use File::Temp qw(tempdir); |
| 9 |
use IO::Socket::INET; |
| 10 |
|
| 11 |
require Exporter; |
| 12 |
our @ISA = qw(Exporter); |
| 13 |
|
| 14 |
our @AUTH = qw( |
| 15 |
auth_group_write |
| 16 |
auth_user_write |
| 17 |
); |
| 18 |
|
| 19 |
our @CONFIG = qw( |
| 20 |
config_get_identity |
| 21 |
config_write |
| 22 |
); |
| 23 |
|
| 24 |
our @FEATURES = qw( |
| 25 |
feature_get_compiled_modules |
| 26 |
feature_get_shared_modules |
| 27 |
feature_get_version |
| 28 |
feature_have_feature_enabled |
| 29 |
feature_have_module_compiled |
| 30 |
feature_have_module_loaded |
| 31 |
); |
| 32 |
|
| 33 |
our @RUNNING = qw( |
| 34 |
server_restart |
| 35 |
server_signal |
| 36 |
server_start |
| 37 |
server_stop |
| 38 |
server_wait |
| 39 |
); |
| 40 |
|
| 41 |
our @TEST = qw( |
| 42 |
test_append_logfile |
| 43 |
test_cleanup |
| 44 |
test_get_logfile |
| 45 |
test_msg |
| 46 |
test_setup |
| 47 |
); |
| 48 |
|
| 49 |
our @TESTSUITE = qw( |
| 50 |
testsuite_empty_test |
| 51 |
testsuite_get_runnable_tests |
| 52 |
testsuite_get_tmp_dir |
| 53 |
); |
| 54 |
|
| 55 |
our @EXPORT_OK = (@AUTH, @CONFIG, @FEATURES, @RUNNING, @TEST, @TESTSUITE); |
| 56 |
|
| 57 |
our %EXPORT_TAGS = ( |
| 58 |
auth => [@AUTH], |
| 59 |
config => [@CONFIG], |
| 60 |
features => [@FEATURES], |
| 61 |
running => [@RUNNING], |
| 62 |
test => [@TEST], |
| 63 |
testsuite => [@TESTSUITE], |
| 64 |
); |
| 65 |
|
| 66 |
my $testno = 0; |
| 67 |
|
| 68 |
sub get_proftpd_bin { |
| 69 |
if ($ENV{PROFTPD_TEST_BIN}) { |
| 70 |
return $ENV{PROFTPD_TEST_BIN}; |
| 71 |
|
| 72 |
} else { |
| 73 |
# Guess. |
| 74 |
return '../proftpd'; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
sub get_high_numbered_port { |
| 79 |
|
| 80 |
# XXX There's a minor race condition here, between opening a listening |
| 81 |
# socket on a kernel-chosen random port, closing that socket, and returning |
| 82 |
# the port number for use in the proftpd config. |
| 83 |
|
| 84 |
my $sock = IO::Socket::INET->new( |
| 85 |
LocalAddr => '127.0.0.1', |
| 86 |
Listen => 5, |
| 87 |
Proto => 'tcp', |
| 88 |
ReuseAddr => 1, |
| 89 |
); |
| 90 |
|
| 91 |
my $port = $sock->sockport(); |
| 92 |
$sock->close(); |
| 93 |
|
| 94 |
return $port; |
| 95 |
} |
| 96 |
|
| 97 |
sub get_passwd { |
| 98 |
my $user_passwd = shift; |
| 99 |
|
| 100 |
if ($user_passwd eq '') { |
| 101 |
return ''; |
| 102 |
} |
| 103 |
|
| 104 |
# First, try to use MD5 hashing for passwords |
| 105 |
my $md5_salt = '$1$' . join('', (0..9, 'A'..'Z', 'a'..'z')[rand(62), rand(62), rand(62), rand(62), rand(62), rand(62), rand(62), rand(62)]); |
| 106 |
|
| 107 |
my $hash = crypt($user_passwd, $md5_salt); |
| 108 |
|
| 109 |
# If the first three characters of the hash are not "$1$", the crypt() |
| 110 |
# implementation doesn't support MD5. Some crypt()s will happily use |
| 111 |
# "$1" as a salt even though this is not a valid DES salt. Humf. |
| 112 |
|
| 113 |
my @string = split('', $hash); |
| 114 |
my $prefix = $string[0] . $string[1] . $string[2]; |
| 115 |
|
| 116 |
if ($prefix ne '$1$') { |
| 117 |
# OK, fall back to using a DES hash. |
| 118 |
my $des_salt = join('', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand(64), rand(64)]); |
| 119 |
|
| 120 |
$hash = crypt($user_passwd, $des_salt); |
| 121 |
} |
| 122 |
|
| 123 |
return $hash; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
sub auth_group_write { |
| 128 |
my $group_file = shift; |
| 129 |
croak("Missing group file argument") unless $group_file; |
| 130 |
my $group_name = shift; |
| 131 |
croak("Missing group name argument") unless $group_name; |
| 132 |
my $group_id = shift; |
| 133 |
croak("Missing group ID argument") unless defined($group_id); |
| 134 |
|
| 135 |
my @member_names = @_; |
| 136 |
|
| 137 |
my $existed = -f $group_file; |
| 138 |
my $prev_mode; |
| 139 |
|
| 140 |
if ($existed) { |
| 141 |
# Get current permissions |
| 142 |
$prev_mode = (stat($group_file))[2]; |
| 143 |
|
| 144 |
# Set needed permissions |
| 145 |
unless (chmod(0666, $group_file)) { |
| 146 |
croak("Can't set perms on $group_file: $!"); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if (open(my $fh, ">> $group_file")) { |
| 151 |
print $fh "$group_name:*:$group_id:" . join(',', @member_names) . "\n"; |
| 152 |
|
| 153 |
unless (close($fh)) { |
| 154 |
croak("Can't write $group_file: $!"); |
| 155 |
} |
| 156 |
|
| 157 |
} else { |
| 158 |
croak("Can't open $group_file: $!"); |
| 159 |
} |
| 160 |
|
| 161 |
if ($existed) { |
| 162 |
# Restore previous perms |
| 163 |
unless (chmod($prev_mode, $group_file)) { |
| 164 |
croak("Can't set perms on $group_file: $!"); |
| 165 |
} |
| 166 |
|
| 167 |
} else { |
| 168 |
# Set correct perms |
| 169 |
unless (chmod(0440, $group_file)) { |
| 170 |
croak("Can't set perms on $group_file: $!"); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
sub auth_user_write { |
| 176 |
my $user_file = shift; |
| 177 |
croak("Missing user file argument") unless $user_file; |
| 178 |
my $user_name = shift; |
| 179 |
croak("Missing user name argument") unless $user_name; |
| 180 |
my $user_passwd = shift; |
| 181 |
croak("Missing user password argument") unless defined($user_passwd); |
| 182 |
my $user_id = shift; |
| 183 |
croak("Missing user ID argument") unless defined($user_id); |
| 184 |
my $group_id = shift; |
| 185 |
croak("Missing group ID argument") unless defined($group_id); |
| 186 |
my $home = shift; |
| 187 |
croak("Missing home directory argument") unless $home; |
| 188 |
my $shell = shift; |
| 189 |
croak("Missing shell argument") unless $shell; |
| 190 |
my $gecos = shift; |
| 191 |
$gecos = '' unless defined($gecos); |
| 192 |
|
| 193 |
my $existed = -f $user_file; |
| 194 |
my $prev_mode; |
| 195 |
|
| 196 |
my $passwd = get_passwd($user_passwd); |
| 197 |
|
| 198 |
if ($existed) { |
| 199 |
# Get current permissions |
| 200 |
$prev_mode = (stat($user_file))[2]; |
| 201 |
|
| 202 |
# Set needed permissions |
| 203 |
unless (chmod(0666, $user_file)) { |
| 204 |
croak("Can't set perms on $user_file: $!"); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
if (open(my $fh, ">> $user_file")) { |
| 209 |
my $pw_entry = join(':', ($user_name, $passwd, $user_id, $group_id, |
| 210 |
$gecos, $home, $shell)); |
| 211 |
|
| 212 |
print $fh "$pw_entry\n"; |
| 213 |
unless (close($fh)) { |
| 214 |
croak("Can't write $user_file: $!"); |
| 215 |
} |
| 216 |
|
| 217 |
} else { |
| 218 |
croak("Can't open $user_file: $!"); |
| 219 |
} |
| 220 |
|
| 221 |
if ($existed) { |
| 222 |
# Restore previous perms |
| 223 |
unless (chmod($prev_mode, $user_file)) { |
| 224 |
croak("Can't set perms on $user_file: $!"); |
| 225 |
} |
| 226 |
|
| 227 |
} else { |
| 228 |
# Set correct perms |
| 229 |
unless (chmod(0440, $user_file)) { |
| 230 |
croak("Can't set perms on $user_file: $!"); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
sub config_get_identity { |
| 236 |
my ($user, $group); |
| 237 |
|
| 238 |
unless ($< == 0) { |
| 239 |
# Use $> (effective UID) rather than $< (real UID) |
| 240 |
my $ruid = $>; |
| 241 |
$user = (getpwuid($ruid))[0]; |
| 242 |
|
| 243 |
# Similarly, use $) (effective GID) rather than $( (real GID) |
| 244 |
my $rgid = (split/\s+/, $))[0]; |
| 245 |
$group = (getgrgid($rgid))[0]; |
| 246 |
|
| 247 |
} else { |
| 248 |
# If the real user ID is root, try to use some non-root user |
| 249 |
my $users = [qw(daemon www ftp adm nobody)]; |
| 250 |
my $groups = [qw(daemon www ftp staff adm nogroup)]; |
| 251 |
my $candidate; |
| 252 |
|
| 253 |
foreach $candidate (@$users) { |
| 254 |
my $candidate_uid = (getpwnam($candidate))[2]; |
| 255 |
|
| 256 |
if ($candidate_uid != 0) { |
| 257 |
$user = $candidate; |
| 258 |
last; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
foreach $candidate (@$groups) { |
| 263 |
my $candidate_gid = (getgrnam($candidate))[2]; |
| 264 |
|
| 265 |
if ($candidate_gid != 0) { |
| 266 |
$group = $candidate; |
| 267 |
last; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
return ($user, $group); |
| 273 |
} |
| 274 |
|
| 275 |
sub config_write_subsection { |
| 276 |
my $fh = shift; |
| 277 |
my $type = shift; |
| 278 |
my $config = shift; |
| 279 |
my $indent = shift; |
| 280 |
|
| 281 |
if ($type eq 'Directory') { |
| 282 |
my $sections = $config; |
| 283 |
|
| 284 |
foreach my $dir (keys(%$sections)) { |
| 285 |
print $fh "$indent<Directory $dir>\n"; |
| 286 |
|
| 287 |
my $section = $sections->{$dir}; |
| 288 |
|
| 289 |
if (ref($section) eq 'HASH') { |
| 290 |
while (my ($dir_k, $dir_v) = each(%$section)) { |
| 291 |
if (ref($dir_v) eq 'HASH' || |
| 292 |
ref($dir_v) eq 'ARRAY') { |
| 293 |
config_write_subsection($fh, $dir_k, $dir_v, "$indent "); |
| 294 |
|
| 295 |
} else { |
| 296 |
print $fh "$indent $dir_k $dir_v\n"; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
} elsif (ref($section) eq 'ARRAY') { |
| 301 |
foreach my $line (@$section) { |
| 302 |
print $fh "$indent $line\n"; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
print $fh "$indent</Directory>\n"; |
| 307 |
} |
| 308 |
|
| 309 |
} elsif ($type eq 'Limit') { |
| 310 |
my $sections = $config; |
| 311 |
|
| 312 |
foreach my $limits (keys(%$sections)) { |
| 313 |
print $fh "$indent<Limit $limits>\n"; |
| 314 |
|
| 315 |
my $section = $sections->{$limits}; |
| 316 |
|
| 317 |
if (ref($section) eq 'HASH') { |
| 318 |
while (my ($limit_k, $limit_v) = each(%$section)) { |
| 319 |
|
| 320 |
if (ref($limit_v) eq 'HASH' || |
| 321 |
ref($limit_v) eq 'ARRAY') { |
| 322 |
config_write_subsection($fh, $limit_k, $limit_v, "$indent "); |
| 323 |
|
| 324 |
} else { |
| 325 |
print $fh "$indent $limit_k $limit_v\n"; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
} elsif (ref($section) eq 'ARRAY') { |
| 330 |
foreach my $line (@$section) { |
| 331 |
print $fh "$indent $line\n"; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
print $fh "$indent</Limit>\n"; |
| 336 |
} |
| 337 |
|
| 338 |
} elsif ($type eq 'Class') { |
| 339 |
my $sections = $config; |
| 340 |
|
| 341 |
foreach my $class (keys(%$sections)) { |
| 342 |
print $fh "<Class $class>\n"; |
| 343 |
|
| 344 |
my $section = $sections->{$class}; |
| 345 |
|
| 346 |
if (ref($section) eq 'HASH') { |
| 347 |
while (my ($class_k, $class_v) = each(%$section)) { |
| 348 |
print $fh " $class_k $class_v\n"; |
| 349 |
} |
| 350 |
|
| 351 |
} elsif (ref($section) eq 'ARRAY') { |
| 352 |
foreach my $line (@$section) { |
| 353 |
print $fh " $line\n"; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
print $fh "</Class>\n"; |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
sub config_write { |
| 363 |
my $path = shift; |
| 364 |
my $config = shift; |
| 365 |
|
| 366 |
# The $opts hash can be used to tell this function to NOT write |
| 367 |
# default config values. |
| 368 |
my $opts = shift; |
| 369 |
$opts = {} unless defined($opts); |
| 370 |
|
| 371 |
my ($user_name, $group_name) = config_get_identity(); |
| 372 |
|
| 373 |
# Set a bunch of defaults, unless overridden by the caller |
| 374 |
my $port; |
| 375 |
|
| 376 |
if (ref($config) eq 'HASH') { |
| 377 |
unless (defined($config->{Port})) { |
| 378 |
my $dynport = get_high_numbered_port(); |
| 379 |
$config->{Port} = $dynport; |
| 380 |
$port = $dynport; |
| 381 |
|
| 382 |
} elsif ($config->{Port} == 0 || $config->{Port} eq '0') { |
| 383 |
my $dynport = get_high_numbered_port(); |
| 384 |
$port = $dynport; |
| 385 |
} |
| 386 |
|
| 387 |
unless (defined($config->{User})) { |
| 388 |
# Handle User names that may contain embedded backslashes and spaces |
| 389 |
$user_name =~ s/\\/\\\\/g; |
| 390 |
$config->{User} = "\"$user_name\""; |
| 391 |
|
| 392 |
if ($< == 0) { |
| 393 |
$config->{User} = 'root'; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
unless (defined($config->{Group})) { |
| 398 |
# Handle Group names that may contain embedded backslashes and spaces |
| 399 |
$group_name =~ s/\\/\\\\/g; |
| 400 |
$config->{Group} = "\"$group_name\""; |
| 401 |
} |
| 402 |
|
| 403 |
unless ($opts->{NoAllowOverride}) { |
| 404 |
unless (defined($config->{AllowOverride})) { |
| 405 |
$config->{AllowOverride} = 'off'; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
unless ($opts->{NoDefaultAddress}) { |
| 410 |
unless (defined($config->{DefaultAddress})) { |
| 411 |
$config->{DefaultAddress} = '127.0.0.1'; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
unless ($opts->{NoDefaultServer}) { |
| 416 |
unless (defined($config->{DefaultServer})) { |
| 417 |
$config->{DefaultServer} = 'on'; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
unless (defined($config->{RequireValidShell})) { |
| 422 |
$config->{RequireValidShell} = 'off'; |
| 423 |
} |
| 424 |
|
| 425 |
unless (defined($config->{ServerType})) { |
| 426 |
$config->{ServerType} = 'standalone'; |
| 427 |
} |
| 428 |
|
| 429 |
unless (defined($config->{TimeoutIdle})) { |
| 430 |
$config->{TimeoutIdle} = '10'; |
| 431 |
} |
| 432 |
|
| 433 |
unless (defined($config->{TimeoutLinger})) { |
| 434 |
$config->{TimeoutLinger} = '1'; |
| 435 |
} |
| 436 |
|
| 437 |
unless (defined($config->{TransferLog})) { |
| 438 |
$config->{TransferLog} = 'none'; |
| 439 |
} |
| 440 |
|
| 441 |
unless (defined($config->{UseFtpUsers})) { |
| 442 |
$config->{UseFtpUsers} = 'off'; |
| 443 |
} |
| 444 |
|
| 445 |
if (feature_have_feature_enabled('ipv6')) { |
| 446 |
unless (defined($config->{UseIPv6})) { |
| 447 |
$config->{UseIPv6} = 'off'; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
unless (defined($config->{UseReverseDNS})) { |
| 452 |
$config->{UseReverseDNS} = 'off'; |
| 453 |
} |
| 454 |
|
| 455 |
unless (defined($config->{WtmpLog})) { |
| 456 |
$config->{WtmpLog} = 'off'; |
| 457 |
} |
| 458 |
|
| 459 |
} elsif (ref($config) eq 'ARRAY') { |
| 460 |
unless (grep(/Port /, @$config) > 0) { |
| 461 |
$port = get_high_numbered_port(); |
| 462 |
push(@$config, "Port $port"); |
| 463 |
} |
| 464 |
|
| 465 |
unless (grep(/^User /, @$config) > 0) { |
| 466 |
push(@$config, "User $user_name"); |
| 467 |
|
| 468 |
if ($< == 0) { |
| 469 |
push(@$config, "User root"); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
unless (grep(/^Group /, @$config) > 0) { |
| 474 |
push(@$config, "Group $group_name"); |
| 475 |
} |
| 476 |
|
| 477 |
unless (grep(/^AllowOverride/, @$config) > 0) { |
| 478 |
push(@$config, "AllowOverride off"); |
| 479 |
} |
| 480 |
|
| 481 |
unless (grep(/^DefaultAddress/, @$config) > 0) { |
| 482 |
push(@$config, "DefaultAddress 127.0.0.1"); |
| 483 |
} |
| 484 |
|
| 485 |
unless (grep(/^DefaultServer/, @$config) > 0) { |
| 486 |
push(@$config, "DefaultServer on"); |
| 487 |
} |
| 488 |
|
| 489 |
unless (grep(/^RequireValidShell/, @$config) > 0) { |
| 490 |
push(@$config, "RequireValidShell off"); |
| 491 |
} |
| 492 |
|
| 493 |
unless (grep(/^ServerType/, @$config) > 0) { |
| 494 |
push(@$config, "ServerType standalone"); |
| 495 |
} |
| 496 |
|
| 497 |
unless (grep(/^TimeoutIdle/, @$config) > 0) { |
| 498 |
push(@$config, "TimeoutIdle 10"); |
| 499 |
} |
| 500 |
|
| 501 |
unless (grep(/^TimeoutLinger/, @$config) > 0) { |
| 502 |
push(@$config, "TimeoutLinger 1"); |
| 503 |
} |
| 504 |
|
| 505 |
unless (grep(/^TransferLog/, @$config) > 0) { |
| 506 |
push(@$config, "TransferLog none"); |
| 507 |
} |
| 508 |
|
| 509 |
unless (grep(/UseFtpUsers/, @$config) > 0) { |
| 510 |
push(@$config, "UseFtpUsers off"); |
| 511 |
} |
| 512 |
|
| 513 |
if (feature_have_feature_enabled('ipv6')) { |
| 514 |
unless (grep(/UseIPv6/, @$config) > 0) { |
| 515 |
push(@$config, "UseIPv6 off"); |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
unless (grep(/UseReverseDNS/, @$config) > 0) { |
| 520 |
push(@$config, "UseReverseDNS off"); |
| 521 |
} |
| 522 |
|
| 523 |
unless (grep(/WtmpLog/, @$config) > 0) { |
| 524 |
push(@$config, "WtmpLog off"); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
my $abs_path = File::Spec->rel2abs($path); |
| 529 |
|
| 530 |
if (open(my $fh, "> $abs_path")) { |
| 531 |
my $timestamp = scalar(localtime()); |
| 532 |
|
| 533 |
print $fh "# Auto-generated proftpd config file\n"; |
| 534 |
print $fh "# Written on: $timestamp\n\n"; |
| 535 |
|
| 536 |
if (ref($config) eq 'HASH') { |
| 537 |
# Write TraceLog, Trace directives at the top of the file, to aid in |
| 538 |
# debugging. |
| 539 |
if (defined($config->{TraceLog})) { |
| 540 |
print $fh "TraceLog $config->{TraceLog}\n"; |
| 541 |
delete($config->{TraceLog}); |
| 542 |
} |
| 543 |
|
| 544 |
if (defined($config->{Trace})) { |
| 545 |
print $fh "Trace $config->{Trace}\n"; |
| 546 |
delete($config->{Trace}); |
| 547 |
} |
| 548 |
|
| 549 |
while (my ($k, $v) = each(%$config)) { |
| 550 |
if ($k eq 'IfModules') { |
| 551 |
my $modules = $v; |
| 552 |
|
| 553 |
foreach my $mod (keys(%$modules)) { |
| 554 |
print $fh "<IfModule $mod>\n"; |
| 555 |
|
| 556 |
my $section = $modules->{$mod}; |
| 557 |
|
| 558 |
if (ref($section) eq 'HASH') { |
| 559 |
while (my ($mod_k, $mod_v) = each(%$section)) { |
| 560 |
|
| 561 |
if (ref($mod_v) eq 'HASH' || |
| 562 |
ref($mod_v) eq 'ARRAY') { |
| 563 |
config_write_subsection($fh, $mod_k, $mod_v, " "); |
| 564 |
|
| 565 |
} else { |
| 566 |
print $fh " $mod_k $mod_v\n"; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
} elsif (ref($section) eq 'ARRAY') { |
| 571 |
foreach my $line (@$section) { |
| 572 |
print $fh " $line\n"; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
print $fh "</IfModule>\n"; |
| 577 |
} |
| 578 |
|
| 579 |
} elsif ($k eq 'Anonymous') { |
| 580 |
my $sections = $v; |
| 581 |
|
| 582 |
foreach my $anon (keys(%$sections)) { |
| 583 |
print $fh "<Anonymous $anon>\n"; |
| 584 |
|
| 585 |
my $section = $sections->{$anon}; |
| 586 |
|
| 587 |
if (ref($section) eq 'HASH') { |
| 588 |
while (my ($anon_k, $anon_v) = each(%$section)) { |
| 589 |
if (ref($anon_v) eq 'HASH' || |
| 590 |
ref($anon_v) eq 'ARRAY') { |
| 591 |
config_write_subsection($fh, $anon_k, $anon_v, " "); |
| 592 |
|
| 593 |
} else { |
| 594 |
print $fh " $anon_k $anon_v\n"; |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
} elsif (ref($section) eq 'ARRAY') { |
| 599 |
foreach my $line (@$section) { |
| 600 |
print $fh " $line\n"; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
print $fh "</Anonymous>\n"; |
| 605 |
} |
| 606 |
|
| 607 |
} elsif ($k eq 'Directory') { |
| 608 |
my $sections = $v; |
| 609 |
|
| 610 |
foreach my $dir (keys(%$sections)) { |
| 611 |
print $fh "<Directory $dir>\n"; |
| 612 |
|
| 613 |
my $section = $sections->{$dir}; |
| 614 |
|
| 615 |
if (ref($section) eq 'HASH') { |
| 616 |
while (my ($dir_k, $dir_v) = each(%$section)) { |
| 617 |
if (ref($dir_v) eq 'HASH' || |
| 618 |
ref($dir_v) eq 'ARRAY') { |
| 619 |
config_write_subsection($fh, $dir_k, $dir_v, " "); |
| 620 |
|
| 621 |
} else { |
| 622 |
print $fh " $dir_k $dir_v\n"; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
} elsif (ref($section) eq 'ARRAY') { |
| 627 |
foreach my $line (@$section) { |
| 628 |
print $fh " $line\n"; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
print $fh "</Directory>\n"; |
| 633 |
} |
| 634 |
|
| 635 |
} elsif ($k eq 'Limit') { |
| 636 |
my $sections = $v; |
| 637 |
|
| 638 |
foreach my $limits (keys(%$sections)) { |
| 639 |
print $fh "<Limit $limits>\n"; |
| 640 |
|
| 641 |
my $section = $sections->{$limits}; |
| 642 |
|
| 643 |
if (ref($section) eq 'HASH') { |
| 644 |
while (my ($limit_k, $limit_v) = each(%$section)) { |
| 645 |
if (ref($limit_v) eq 'HASH' || |
| 646 |
ref($limit_v) eq 'ARRAY') { |
| 647 |
config_write_subsection($fh, $limit_k, $limit_v, " "); |
| 648 |
|
| 649 |
} else { |
| 650 |
print $fh " $limit_k $limit_v\n"; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
} elsif (ref($section) eq 'ARRAY') { |
| 655 |
foreach my $line (@$section) { |
| 656 |
print $fh " $line\n"; |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
print $fh "</Limit>\n"; |
| 661 |
} |
| 662 |
|
| 663 |
} elsif ($k eq 'Class') { |
| 664 |
my $sections = $v; |
| 665 |
|
| 666 |
foreach my $class (keys(%$sections)) { |
| 667 |
print $fh "<Class $class>\n"; |
| 668 |
|
| 669 |
my $section = $sections->{$class}; |
| 670 |
|
| 671 |
if (ref($section) eq 'HASH') { |
| 672 |
while (my ($class_k, $class_v) = each(%$section)) { |
| 673 |
print $fh " $class_k $class_v\n"; |
| 674 |
} |
| 675 |
|
| 676 |
} elsif (ref($section) eq 'ARRAY') { |
| 677 |
foreach my $line (@$section) { |
| 678 |
print $fh " $line\n"; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
print $fh "</Class>\n"; |
| 683 |
} |
| 684 |
|
| 685 |
} elsif ($k eq 'Global') { |
| 686 |
print $fh "<Global>\n"; |
| 687 |
|
| 688 |
foreach my $name (keys(%$v)) { |
| 689 |
print $fh " $name $v->{$name}\n"; |
| 690 |
} |
| 691 |
|
| 692 |
print $fh "</Global>\n"; |
| 693 |
|
| 694 |
} else { |
| 695 |
print $fh "$k $v\n"; |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
} elsif (ref($config) eq 'ARRAY') { |
| 700 |
foreach my $line (@$config) { |
| 701 |
print $fh "$line\n"; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
unless (close($fh)) { |
| 706 |
croak("Error writing $abs_path: $!"); |
| 707 |
} |
| 708 |
|
| 709 |
} else { |
| 710 |
croak("Error opening $abs_path: $!"); |
| 711 |
} |
| 712 |
|
| 713 |
if (wantarray()) { |
| 714 |
return ($port, $user_name, $group_name); |
| 715 |
} |
| 716 |
|
| 717 |
return 1; |
| 718 |
} |
| 719 |
|
| 720 |
sub feature_get_shared_modules { |
| 721 |
my $proftpd_bin = get_proftpd_bin(); |
| 722 |
|
| 723 |
my $shared_modules = []; |
| 724 |
|
| 725 |
my $configure_args; |
| 726 |
if (open(my $cmdh, "$proftpd_bin -V |")) { |
| 727 |
while (my $line = <$cmdh>) { |
| 728 |
chomp($line); |
| 729 |
|
| 730 |
next unless $line =~ /^\s+configure (.*?)$/; |
| 731 |
|
| 732 |
$configure_args = $1; |
| 733 |
last; |
| 734 |
} |
| 735 |
|
| 736 |
close($cmdh); |
| 737 |
|
| 738 |
my $args = [split(' ', $configure_args)]; |
| 739 |
|
| 740 |
foreach my $arg (@$args) { |
| 741 |
$arg =~ s/^'//; |
| 742 |
$arg =~ s/'$//; |
| 743 |
|
| 744 |
if ($arg =~ /^\-\-with\-shared=(.*?)$/) { |
| 745 |
my $module_list = $1; |
| 746 |
|
| 747 |
$shared_modules = [split(':', $module_list)]; |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
return $shared_modules; |
| 752 |
|
| 753 |
} else { |
| 754 |
croak("Error listing features"); |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
sub feature_get_version { |
| 759 |
my $proftpd_bin = get_proftpd_bin(); |
| 760 |
|
| 761 |
my ($version, $label); |
| 762 |
|
| 763 |
if (open(my $cmdh, "$proftpd_bin -V |")) { |
| 764 |
|
| 765 |
while (my $line = <$cmdh>) { |
| 766 |
chomp($line); |
| 767 |
|
| 768 |
next unless $line =~ /\s+Version:\s+(\S+)\s+\(\S+\)$/; |
| 769 |
|
| 770 |
$version = $1; |
| 771 |
$label = $2; |
| 772 |
last; |
| 773 |
} |
| 774 |
|
| 775 |
close($cmdh); |
| 776 |
|
| 777 |
if (wantarray()) { |
| 778 |
return ($version, $label); |
| 779 |
|
| 780 |
} else { |
| 781 |
return $version; |
| 782 |
} |
| 783 |
|
| 784 |
} else { |
| 785 |
croak("Error listing features"); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
sub feature_have_feature_enabled { |
| 790 |
my $feat = shift; |
| 791 |
|
| 792 |
my $proftpd_bin = get_proftpd_bin(); |
| 793 |
|
| 794 |
if (open(my $cmdh, "$proftpd_bin -V |")) { |
| 795 |
my $feat_list; |
| 796 |
|
| 797 |
while (my $line = <$cmdh>) { |
| 798 |
chomp($line); |
| 799 |
|
| 800 |
next unless $line =~ /\s+(\-|\+)\s+(\S+)\s+support/; |
| 801 |
|
| 802 |
my $flag = $1; |
| 803 |
my $feature = $2; |
| 804 |
|
| 805 |
if ($flag eq '+') { |
| 806 |
push(@$feat_list, $feature); |
| 807 |
|
| 808 |
# Special-case hack for FIPS-enabled OpenSSL |
| 809 |
if ($feature =~ /OpenSSL/i) { |
| 810 |
if ($line =~ /\(FIPS enabled\)/) { |
| 811 |
push(@$feat_list, "OpenSSL_FIPS"); |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
close($cmdh); |
| 818 |
|
| 819 |
my $matches = grep { /^$feat$/i } @$feat_list; |
| 820 |
return $matches; |
| 821 |
|
| 822 |
} else { |
| 823 |
croak("Error listing features"); |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
sub feature_get_compiled_modules { |
| 828 |
my $proftpd_bin = get_proftpd_bin(); |
| 829 |
my $mod_list = []; |
| 830 |
|
| 831 |
if (open(my $cmdh, "$proftpd_bin -l |")) { |
| 832 |
while (my $line = <$cmdh>) { |
| 833 |
chomp($line); |
| 834 |
|
| 835 |
next if $line =~ /Compiled\-in/; |
| 836 |
$line =~ s/^\s+//; |
| 837 |
|
| 838 |
push(@$mod_list, $line); |
| 839 |
} |
| 840 |
|
| 841 |
close($cmdh); |
| 842 |
|
| 843 |
} else { |
| 844 |
croak("Error listing compiled modules"); |
| 845 |
} |
| 846 |
|
| 847 |
return $mod_list; |
| 848 |
} |
| 849 |
|
| 850 |
sub feature_have_module_compiled { |
| 851 |
my $module = shift; |
| 852 |
|
| 853 |
my $mod_list = feature_get_compiled_modules(); |
| 854 |
my $matches = grep { /^$module$/ } @$mod_list; |
| 855 |
return $matches; |
| 856 |
} |
| 857 |
|
| 858 |
sub feature_have_module_loaded { |
| 859 |
my $module = shift; |
| 860 |
my $config_file = shift; |
| 861 |
|
| 862 |
my $proftpd_bin = get_proftpd_bin(); |
| 863 |
|
| 864 |
if (open(my $cmdh, "$proftpd_bin -vv -c $config_file |")) { |
| 865 |
my $mod_list; |
| 866 |
|
| 867 |
while (my $line = <$cmdh>) { |
| 868 |
chomp($line); |
| 869 |
|
| 870 |
next unless $line =~ /^\s+mod_/; |
| 871 |
$line =~ s/^\s+//; |
| 872 |
|
| 873 |
push(@$mod_list, $line); |
| 874 |
|
| 875 |
# Need to be able to handle the listing info for a module which |
| 876 |
# includes the module version, rather than a ".c" ending. |
| 877 |
my $alt_module = $module; |
| 878 |
$alt_module =~ s/\.c$/\//g; |
| 879 |
|
| 880 |
my $res = 0; |
| 881 |
if (grep { /^($module$|$alt_module)/ } @$mod_list) { |
| 882 |
$res = 1; |
| 883 |
} |
| 884 |
|
| 885 |
close($cmdh); |
| 886 |
return $res; |
| 887 |
} |
| 888 |
|
| 889 |
close($cmdh); |
| 890 |
|
| 891 |
} else { |
| 892 |
croak("Error listing loaded modules"); |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
sub server_signal { |
| 897 |
my $pid_file = shift; |
| 898 |
croak("Missing PID file argument") unless $pid_file; |
| 899 |
my $signal_name = shift; |
| 900 |
$signal_name = 'HUP' unless $signal_name; |
| 901 |
|
| 902 |
my $pid; |
| 903 |
if (open(my $fh, "< $pid_file")) { |
| 904 |
$pid = <$fh>; |
| 905 |
chomp($pid); |
| 906 |
close($fh); |
| 907 |
|
| 908 |
} else { |
| 909 |
croak("Can't read $pid_file: $!"); |
| 910 |
} |
| 911 |
|
| 912 |
my $cmd = "kill -$signal_name $pid"; |
| 913 |
|
| 914 |
if ($ENV{TEST_VERBOSE}) { |
| 915 |
my $label = 'Signalling'; |
| 916 |
if ($signal_name eq 'HUP') { |
| 917 |
$label = 'Restarting'; |
| 918 |
} |
| 919 |
|
| 920 |
print STDERR "$label server: $cmd\n"; |
| 921 |
} |
| 922 |
|
| 923 |
my @output = `$cmd`; |
| 924 |
if ($? != 0) { |
| 925 |
return undef; |
| 926 |
} |
| 927 |
|
| 928 |
return 1; |
| 929 |
} |
| 930 |
|
| 931 |
sub server_restart { |
| 932 |
my $pid_file = shift; |
| 933 |
croak("Missing PID file argument") unless $pid_file; |
| 934 |
|
| 935 |
return server_signal($pid_file, 'HUP'); |
| 936 |
} |
| 937 |
|
| 938 |
sub server_start { |
| 939 |
my $config_file = shift; |
| 940 |
croak("Missing config file argument") unless $config_file; |
| 941 |
my $pid_file = shift; |
| 942 |
my $server_opts = shift; |
| 943 |
$server_opts = {} unless defined($server_opts); |
| 944 |
|
| 945 |
# Make sure that the config file is an absolute path |
| 946 |
my $abs_config_file = File::Spec->rel2abs($config_file); |
| 947 |
|
| 948 |
my $proftpd_bin = get_proftpd_bin(); |
| 949 |
|
| 950 |
my $cmd = ''; |
| 951 |
|
| 952 |
if (defined($server_opts->{env})) { |
| 953 |
my $envs = $server_opts->{env}; |
| 954 |
|
| 955 |
while (my ($key, $value) = each(%$envs)) { |
| 956 |
# Assume Bourne-shell syntax |
| 957 |
$cmd .= "$key=$value "; |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
my $quiet = '-q'; |
| 962 |
if ($ENV{TEST_VERBOSE}) { |
| 963 |
$quiet = ''; |
| 964 |
} |
| 965 |
|
| 966 |
$cmd .= "$proftpd_bin $quiet -c $abs_config_file"; |
| 967 |
|
| 968 |
if (defined($server_opts->{define})) { |
| 969 |
my $defines = $server_opts->{define}; |
| 970 |
|
| 971 |
foreach my $define (@$defines) { |
| 972 |
$cmd .= " -D$define"; |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
if ($ENV{TEST_VERBOSE}) { |
| 977 |
$cmd .= " -d10"; |
| 978 |
|
| 979 |
} else { |
| 980 |
$cmd .= " > /dev/null 2>&1"; |
| 981 |
} |
| 982 |
|
| 983 |
if ($ENV{TEST_VERBOSE}) { |
| 984 |
print STDERR "Starting server: $cmd\n"; |
| 985 |
} |
| 986 |
|
| 987 |
my @output = `$cmd`; |
| 988 |
|
| 989 |
# Ideally we would use the return value from the command to determine |
| 990 |
# whether the server started successfully or not. But proftpd's exit |
| 991 |
# codes are not that nice yet, sadly. Instead, we'll use the PidFile |
| 992 |
# written out by the server, if provided. |
| 993 |
if ($pid_file) { |
| 994 |
sleep(1); |
| 995 |
|
| 996 |
my $pid; |
| 997 |
if (open(my $fh, "< $pid_file")) { |
| 998 |
$pid = <$fh>; |
| 999 |
chomp($pid); |
| 1000 |
close($fh); |
| 1001 |
|
| 1002 |
} else { |
| 1003 |
croak("Can't read $pid_file: $!"); |
| 1004 |
} |
| 1005 |
|
| 1006 |
$cmd = "kill -0 $pid"; |
| 1007 |
|
| 1008 |
if ($ENV{TEST_VERBOSE}) { |
| 1009 |
print STDERR "Testing server: $cmd\n"; |
| 1010 |
|
| 1011 |
} else { |
| 1012 |
$cmd .= " 2>/dev/null"; |
| 1013 |
} |
| 1014 |
|
| 1015 |
@output = `$cmd`; |
| 1016 |
if ($? != 0) { |
| 1017 |
croak("server failed to start"); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
sub server_stop { |
| 1023 |
my $pid_file = shift; |
| 1024 |
my $nowait = shift; |
| 1025 |
$nowait = 0 unless defined($nowait); |
| 1026 |
|
| 1027 |
my $pid; |
| 1028 |
if (open(my $fh, "< $pid_file")) { |
| 1029 |
$pid = <$fh>; |
| 1030 |
chomp($pid); |
| 1031 |
close($fh); |
| 1032 |
|
| 1033 |
} else { |
| 1034 |
croak("Can't read $pid_file: $!"); |
| 1035 |
} |
| 1036 |
|
| 1037 |
my $cmd = "kill -TERM $pid"; |
| 1038 |
|
| 1039 |
if ($ENV{TEST_VERBOSE}) { |
| 1040 |
print STDERR "Stopping server: $cmd\n"; |
| 1041 |
|
| 1042 |
} else { |
| 1043 |
$cmd .= " 2>/dev/null"; |
| 1044 |
} |
| 1045 |
|
| 1046 |
my @output = `$cmd`; |
| 1047 |
unless ($? == 0) { |
| 1048 |
return undef; |
| 1049 |
} |
| 1050 |
|
| 1051 |
unless ($nowait) { |
| 1052 |
# Wait until the PidFile has been deleted by the shutting-down server. |
| 1053 |
# We use select(), rather than alarm(), to do sub-second waits. |
| 1054 |
# |
| 1055 |
# We will wait for a period of up to 2 seconds, but not longer. |
| 1056 |
my $now = time(); |
| 1057 |
|
| 1058 |
while ((time() - $now) < 2 && |
| 1059 |
-e $pid_file) { |
| 1060 |
select(undef, undef, undef, 0.5); |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|
| 1064 |
return 1; |
| 1065 |
} |
| 1066 |
|
| 1067 |
my $server_wait_timeout = 0; |
| 1068 |
sub server_wait_alarm { |
| 1069 |
croak("Test timed out after $server_wait_timeout secs"); |
| 1070 |
} |
| 1071 |
|
| 1072 |
sub server_wait { |
| 1073 |
my $config_file = shift; |
| 1074 |
my $rfh = shift; |
| 1075 |
$server_wait_timeout = shift; |
| 1076 |
my $server_opts = {}; |
| 1077 |
|
| 1078 |
# Check to see if the timeout argument is a hashref (for additional |
| 1079 |
# server_start parameters) or not. If not, it's just the timeout. |
| 1080 |
|
| 1081 |
if (defined($server_wait_timeout)) { |
| 1082 |
if (ref($server_wait_timeout) eq 'HASH') { |
| 1083 |
$server_opts = $server_wait_timeout; |
| 1084 |
|
| 1085 |
if (defined($server_opts->{timeout})) { |
| 1086 |
$server_wait_timeout = $server_opts->{timeout}; |
| 1087 |
|
| 1088 |
} else { |
| 1089 |
$server_wait_timeout = 10; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
} else { |
| 1094 |
$server_wait_timeout = 10; |
| 1095 |
} |
| 1096 |
|
| 1097 |
# Start server |
| 1098 |
server_start($config_file, undef, $server_opts); |
| 1099 |
|
| 1100 |
$SIG{ALRM} = \&server_wait_alarm; |
| 1101 |
alarm($server_wait_timeout); |
| 1102 |
|
| 1103 |
# Wait until we receive word from the child that it has finished its test. |
| 1104 |
while (my $msg = <$rfh>) { |
| 1105 |
chomp($msg); |
| 1106 |
|
| 1107 |
if ($msg eq 'done') { |
| 1108 |
last; |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
alarm(0); |
| 1113 |
$SIG{ALRM} = 'DEFAULT'; |
| 1114 |
return 1; |
| 1115 |
} |
| 1116 |
|
| 1117 |
sub test_append_logfile { |
| 1118 |
my $log_file = shift; |
| 1119 |
my $ex = shift; |
| 1120 |
|
| 1121 |
my ($infh, $outfh); |
| 1122 |
|
| 1123 |
my $out_file = File::Spec->rel2abs('tests.log'); |
| 1124 |
|
| 1125 |
unless (open($outfh, ">> $out_file")) { |
| 1126 |
croak("Can't append to $out_file: $!"); |
| 1127 |
} |
| 1128 |
|
| 1129 |
my ($pkg, $filename, $lineno, $func) = (caller(1))[0, 1, 2, 3]; |
| 1130 |
if ($func =~ /test_cleanup/) { |
| 1131 |
($pkg, $filename, $lineno, $func) = (caller(2))[0, 1, 2, 3]; |
| 1132 |
} |
| 1133 |
|
| 1134 |
print $outfh "-----BEGIN $func-----\n"; |
| 1135 |
|
| 1136 |
if (open($infh, "+< $log_file")) { |
| 1137 |
while (my $line = <$infh>) { |
| 1138 |
print $outfh $line; |
| 1139 |
} |
| 1140 |
|
| 1141 |
close($infh); |
| 1142 |
} |
| 1143 |
|
| 1144 |
# If an exception was provided, write that out to the log file, too. |
| 1145 |
if (defined($ex)) { |
| 1146 |
print $outfh "\nException:\n\t$ex\n"; |
| 1147 |
} |
| 1148 |
|
| 1149 |
print $outfh "-----END $func-----\n"; |
| 1150 |
|
| 1151 |
unless (close($outfh)) { |
| 1152 |
croak("Can't write $out_file: $!"); |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
sub test_cleanup { |
| 1157 |
my $log_file = shift; |
| 1158 |
my $ex = shift; |
| 1159 |
my $keep_logfile = shift; |
| 1160 |
$keep_logfile = 0 unless $keep_logfile; |
| 1161 |
|
| 1162 |
if ($ex) { |
| 1163 |
test_append_logfile($log_file, $ex); |
| 1164 |
unlink($log_file); |
| 1165 |
|
| 1166 |
croak($ex); |
| 1167 |
} |
| 1168 |
|
| 1169 |
unlink($log_file) unless $keep_logfile; |
| 1170 |
} |
| 1171 |
|
| 1172 |
sub test_get_logfile { |
| 1173 |
# Returns the testcase-specific logfile name to use |
| 1174 |
my $depth = shift; |
| 1175 |
$depth = 1 unless defined($depth); |
| 1176 |
|
| 1177 |
my ($pkg, $filename, $lineno, $func) = (caller($depth))[0, 1, 2, 3]; |
| 1178 |
|
| 1179 |
# We use the function name as the basis for the testcase-specific log |
| 1180 |
# file name. We ignore the first two parts (which are always 'ProFTPD' and |
| 1181 |
# 'Tests'), and merge the rest into a log file name. |
| 1182 |
|
| 1183 |
my @parts = split('::', $func); |
| 1184 |
my $logfile = File::Spec->rel2abs(join('_', @parts) . '.log'); |
| 1185 |
|
| 1186 |
return $logfile; |
| 1187 |
} |
| 1188 |
|
| 1189 |
sub test_msg { |
| 1190 |
my $msg = shift; |
| 1191 |
|
| 1192 |
my ($pkg, $file, $lineno) = caller(); |
| 1193 |
|
| 1194 |
return "$msg (at $file:$lineno)"; |
| 1195 |
} |
| 1196 |
|
| 1197 |
sub test_setup { |
| 1198 |
my $tmpdir = shift; |
| 1199 |
croak("Missing temporary directory argument") unless $tmpdir; |
| 1200 |
my $name = shift; |
| 1201 |
croak("Missing test name argument") unless $name; |
| 1202 |
my $user = shift; |
| 1203 |
$user = 'proftpd' unless defined($user); |
| 1204 |
my $passwd = shift; |
| 1205 |
$passwd = 'test' unless defined($passwd); |
| 1206 |
my $group = shift; |
| 1207 |
$group = 'ftpd' unless defined($group); |
| 1208 |
my $uid = shift; |
| 1209 |
$uid = 500 unless defined($uid); |
| 1210 |
my $gid = shift; |
| 1211 |
$gid = 500 unless defined($gid); |
| 1212 |
my $home_dir = shift; |
| 1213 |
my $groups = shift; |
| 1214 |
$groups = $user unless defined($groups); |
| 1215 |
|
| 1216 |
my $config_file = "$tmpdir/$name.conf"; |
| 1217 |
my $pid_file = File::Spec->rel2abs("$tmpdir/$name.pid"); |
| 1218 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/$name.scoreboard"); |
| 1219 |
my $log_file = test_get_logfile(2); |
| 1220 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/$name.passwd"); |
| 1221 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/$name.group"); |
| 1222 |
|
| 1223 |
# If the caller provides the home directory, it is ASSUMED that they will |
| 1224 |
# have created it. |
| 1225 |
unless (defined($home_dir)) { |
| 1226 |
$home_dir = File::Spec->rel2abs($tmpdir); |
| 1227 |
|
| 1228 |
# Make sure that, if we're running as root, that the home directory has |
| 1229 |
# permissions/privs set for the account we create |
| 1230 |
if ($< == 0) { |
| 1231 |
unless (chmod(0755, $home_dir)) { |
| 1232 |
croak("Can't set perms on $home_dir to 0755: $!"); |
| 1233 |
} |
| 1234 |
|
| 1235 |
unless (chown($uid, $gid, $home_dir)) { |
| 1236 |
croak("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 1242 |
'/bin/bash'); |
| 1243 |
auth_group_write($auth_group_file, $group, $gid, $groups); |
| 1244 |
|
| 1245 |
# If we are NOT running with root privs, then the default UID/GID cannot |
| 1246 |
# be provided by the session. So we will use the current UID/GID in such |
| 1247 |
# cases. |
| 1248 |
if ($< != 0) { |
| 1249 |
$uid = $<; |
| 1250 |
$gid = $(; |
| 1251 |
|
| 1252 |
# If `$(` returns a space-separated list of numbers, we want to only |
| 1253 |
# use the first one. |
| 1254 |
if ($gid =~ / /) { |
| 1255 |
my $gids = [split(/\s+/, $gid)]; |
| 1256 |
$gid = $gids->[0]; |
| 1257 |
} |
| 1258 |
} |
| 1259 |
|
| 1260 |
my $setup = { |
| 1261 |
auth_user_file => $auth_user_file, |
| 1262 |
auth_group_file => $auth_group_file, |
| 1263 |
config_file => $config_file, |
| 1264 |
gid => $gid, |
| 1265 |
group => $group, |
| 1266 |
home_dir => $home_dir, |
| 1267 |
log_file => $log_file, |
| 1268 |
passwd => $passwd, |
| 1269 |
pid_file => $pid_file, |
| 1270 |
scoreboard_file => $scoreboard_file, |
| 1271 |
uid => $uid, |
| 1272 |
user => $user, |
| 1273 |
}; |
| 1274 |
|
| 1275 |
return $setup; |
| 1276 |
} |
| 1277 |
|
| 1278 |
sub testsuite_empty_test { |
| 1279 |
} |
| 1280 |
|
| 1281 |
sub testsuite_get_runnable_tests { |
| 1282 |
my $tests = shift; |
| 1283 |
return undef unless $tests; |
| 1284 |
|
| 1285 |
# Special handling of any 'feature_*' test classes; if the compiled proftpd |
| 1286 |
# has these features enabled, include those tests. |
| 1287 |
my $skip_tests = []; |
| 1288 |
foreach my $test (keys(%$tests)) { |
| 1289 |
foreach my $class (@{ $tests->{$test}->{test_class} }) { |
| 1290 |
if ($class =~ /^feature_(\S+)$/) { |
| 1291 |
my $feat = $1; |
| 1292 |
|
| 1293 |
unless (feature_have_feature_enabled($feat)) { |
| 1294 |
push(@$skip_tests, $test); |
| 1295 |
last; |
| 1296 |
} |
| 1297 |
} |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
foreach my $skip_test (@$skip_tests) { |
| 1302 |
delete($tests->{$skip_test}); |
| 1303 |
} |
| 1304 |
|
| 1305 |
# Special handling of any 'mod_*' test classes; if the compiled proftpd |
| 1306 |
# has these as static modules, include those tests. |
| 1307 |
$skip_tests = []; |
| 1308 |
foreach my $test (keys(%$tests)) { |
| 1309 |
foreach my $class (@{ $tests->{$test}->{test_class} }) { |
| 1310 |
if ($class =~ /^mod_\S+$/) { |
| 1311 |
my $module = $class; |
| 1312 |
|
| 1313 |
if ($module !~ /\.c$/) { |
| 1314 |
$module .= '.c'; |
| 1315 |
} |
| 1316 |
|
| 1317 |
unless (feature_have_module_compiled($module)) { |
| 1318 |
push(@$skip_tests, $test); |
| 1319 |
last; |
| 1320 |
} |
| 1321 |
} |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
foreach my $skip_test (@$skip_tests) { |
| 1326 |
delete($tests->{$skip_test}); |
| 1327 |
} |
| 1328 |
|
| 1329 |
# Special handling of the 'norootprivs' test class; unless we are running |
| 1330 |
# as non-root, we should exclude those test cases. |
| 1331 |
if ($< == 0) { |
| 1332 |
$skip_tests = []; |
| 1333 |
foreach my $test (keys(%$tests)) { |
| 1334 |
my $ok = 1; |
| 1335 |
foreach my $test_class (@{ $tests->{$test}->{test_class} }) { |
| 1336 |
if ($test_class eq 'norootprivs') { |
| 1337 |
$ok = 0; |
| 1338 |
last; |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
unless ($ok) { |
| 1343 |
push(@$skip_tests, $test); |
| 1344 |
} |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
foreach my $skip_test (@$skip_tests) { |
| 1349 |
delete($tests->{$skip_test}); |
| 1350 |
} |
| 1351 |
|
| 1352 |
# Special handling of any 'os_*' test classes; if the machine running |
| 1353 |
# these tests matches the configured OS, include these tests. |
| 1354 |
$skip_tests = []; |
| 1355 |
foreach my $test (keys(%$tests)) { |
| 1356 |
foreach my $class (@{ $tests->{$test}->{test_class} }) { |
| 1357 |
if ($class =~ /^os_(\S+)$/) { |
| 1358 |
my $os = $1; |
| 1359 |
|
| 1360 |
unless ($os =~ /$^O/i) { |
| 1361 |
push(@$skip_tests, $test); |
| 1362 |
last; |
| 1363 |
} |
| 1364 |
} |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
foreach my $skip_test (@$skip_tests) { |
| 1369 |
delete($tests->{$skip_test}); |
| 1370 |
} |
| 1371 |
|
| 1372 |
# Special handling of the 'rootprivs' test class: unless we are running |
| 1373 |
# as root, we should exclude those test cases. |
| 1374 |
unless ($< == 0) { |
| 1375 |
$skip_tests = []; |
| 1376 |
foreach my $test (keys(%$tests)) { |
| 1377 |
my $ok = 1; |
| 1378 |
foreach my $test_class (@{ $tests->{$test}->{test_class} }) { |
| 1379 |
if ($test_class eq 'rootprivs') { |
| 1380 |
$ok = 0; |
| 1381 |
last; |
| 1382 |
} |
| 1383 |
} |
| 1384 |
|
| 1385 |
unless ($ok) { |
| 1386 |
push(@$skip_tests, $test); |
| 1387 |
} |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
foreach my $skip_test (@$skip_tests) { |
| 1392 |
delete($tests->{$skip_test}); |
| 1393 |
} |
| 1394 |
|
| 1395 |
my $runnables = []; |
| 1396 |
|
| 1397 |
if (defined($ENV{PROFTPD_TEST_ENABLE_CLASS})) { |
| 1398 |
my $enabled_classes = [split(':', $ENV{PROFTPD_TEST_ENABLE_CLASS})]; |
| 1399 |
|
| 1400 |
foreach my $enabled_class (@$enabled_classes) { |
| 1401 |
foreach my $test (keys(%$tests)) { |
| 1402 |
foreach my $test_class (@{ $tests->{$test}->{test_class} }) { |
| 1403 |
if ($test_class eq $enabled_class) { |
| 1404 |
push(@$runnables, $test); |
| 1405 |
last; |
| 1406 |
} |
| 1407 |
} |
| 1408 |
} |
| 1409 |
} |
| 1410 |
|
| 1411 |
} else { |
| 1412 |
$runnables = [keys(%$tests)]; |
| 1413 |
} |
| 1414 |
|
| 1415 |
if (defined($ENV{PROFTPD_TEST_DISABLE_CLASS})) { |
| 1416 |
my $disabled_classes = [split(':', $ENV{PROFTPD_TEST_DISABLE_CLASS})]; |
| 1417 |
my $new_runnables = []; |
| 1418 |
|
| 1419 |
foreach my $test (@$runnables) { |
| 1420 |
my $skip_test = 0; |
| 1421 |
|
| 1422 |
foreach my $disabled_class (@$disabled_classes) { |
| 1423 |
foreach my $test_class (@{ $tests->{$test}->{test_class} }) { |
| 1424 |
if ($test_class eq $disabled_class) { |
| 1425 |
$skip_test = 1; |
| 1426 |
last; |
| 1427 |
} |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
unless ($skip_test) { |
| 1432 |
push(@$new_runnables, $test); |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
$runnables = $new_runnables; |
| 1437 |
} |
| 1438 |
|
| 1439 |
if (scalar(@$runnables) > 0) { |
| 1440 |
$runnables = [sort { $tests->{$a}->{order} <=> $tests->{$b}->{order} } @$runnables]; |
| 1441 |
|
| 1442 |
} else { |
| 1443 |
$runnables = [qw(testsuite_empty_test)]; |
| 1444 |
} |
| 1445 |
|
| 1446 |
return @$runnables; |
| 1447 |
} |
| 1448 |
|
| 1449 |
sub testsuite_get_tmp_dir { |
| 1450 |
++$testno; |
| 1451 |
|
| 1452 |
my $tmpdir = tempdir( |
| 1453 |
"proftpd-test-$$-test$testno-XXXXXXXXXX", |
| 1454 |
TMPDIR => 1, |
| 1455 |
CLEANUP => 0, |
| 1456 |
); |
| 1457 |
|
| 1458 |
unless (chmod(0755, $tmpdir)) { |
| 1459 |
croak("Can't chmod tmp directory $tmpdir: $!"); |
| 1460 |
} |
| 1461 |
|
| 1462 |
return $tmpdir; |
| 1463 |
} |
| 1464 |
|
| 1465 |
1; |