| 1 |
package ProFTPD::Tests::Modules::mod_radius; |
| 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 |
radius_auth => { |
| 19 |
order => ++$order, |
| 20 |
test_class => [qw(forking)], |
| 21 |
}, |
| 22 |
|
| 23 |
radius_acct => { |
| 24 |
order => ++$order, |
| 25 |
test_class => [qw(forking)], |
| 26 |
}, |
| 27 |
|
| 28 |
radius_acct_aborted_xfer_bug3278 => { |
| 29 |
order => ++$order, |
| 30 |
test_class => [qw(bug forking)], |
| 31 |
}, |
| 32 |
|
| 33 |
radius_userinfo_var_u => { |
| 34 |
order => ++$order, |
| 35 |
test_class => [qw(forking)], |
| 36 |
}, |
| 37 |
|
| 38 |
}; |
| 39 |
|
| 40 |
sub new { |
| 41 |
return shift()->SUPER::new(@_); |
| 42 |
} |
| 43 |
|
| 44 |
sub list_tests { |
| 45 |
return testsuite_get_runnable_tests($TESTS); |
| 46 |
} |
| 47 |
|
| 48 |
sub radius_auth { |
| 49 |
my $self = shift; |
| 50 |
my $tmpdir = $self->{tmpdir}; |
| 51 |
|
| 52 |
my $config_file = "$tmpdir/radius.conf"; |
| 53 |
my $pid_file = File::Spec->rel2abs("$tmpdir/radius.pid"); |
| 54 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/radius.scoreboard"); |
| 55 |
|
| 56 |
my $log_file = test_get_logfile(); |
| 57 |
|
| 58 |
my $user = $ENV{RADIUS_USER} ? $ENV{RADIUS_USER} : "proftpd"; |
| 59 |
my $passwd = $ENV{RADIUS_PASSWD} ? $ENV{RADIUS_PASSWD} : "test"; |
| 60 |
my $group = $ENV{RADIUS_GROUP} ? $ENV{RADIUS_GROUP} : 'ftpd'; |
| 61 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 62 |
my $uid = 1000; |
| 63 |
my $gid = 1000; |
| 64 |
|
| 65 |
# Make sure that, if we're running as root, that the home directory has |
| 66 |
# permissions/privs set for the account we create |
| 67 |
if ($< == 0) { |
| 68 |
unless (chmod(0755, $home_dir)) { |
| 69 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 70 |
} |
| 71 |
|
| 72 |
unless (chown($uid, $gid, $home_dir)) { |
| 73 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
my $config = { |
| 78 |
PidFile => $pid_file, |
| 79 |
ScoreboardFile => $scoreboard_file, |
| 80 |
SystemLog => $log_file, |
| 81 |
TraceLog => $log_file, |
| 82 |
Trace => 'auth:10', |
| 83 |
|
| 84 |
IfModules => { |
| 85 |
'mod_delay.c' => { |
| 86 |
DelayEngine => 'off', |
| 87 |
}, |
| 88 |
|
| 89 |
'mod_radius.c' => { |
| 90 |
RadiusEngine => 'on', |
| 91 |
RadiusLog => $log_file, |
| 92 |
RadiusAuthServer => "localhost:1812 testing123 5", |
| 93 |
RadiusUserInfo => "$uid $gid $home_dir /bin/bash", |
| 94 |
RadiusGroupInfo => "$group $user $gid", |
| 95 |
}, |
| 96 |
}, |
| 97 |
}; |
| 98 |
|
| 99 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 100 |
|
| 101 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 102 |
# the child will indicate when it's done with its test by writing a message |
| 103 |
# to the parent. |
| 104 |
my ($rfh, $wfh); |
| 105 |
unless (pipe($rfh, $wfh)) { |
| 106 |
die("Can't open pipe: $!"); |
| 107 |
} |
| 108 |
|
| 109 |
my $ex; |
| 110 |
|
| 111 |
# Make sure that radiusd is running before running these tests, e.g.: |
| 112 |
# |
| 113 |
# sudo /path/to/freeradius-dir/sbin/radiusd -X -f -xx |
| 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); |
| 124 |
$resp_code = $client->response_code(); |
| 125 |
$resp_msg = $client->response_msg(); |
| 126 |
|
| 127 |
my $expected; |
| 128 |
|
| 129 |
$expected = 230; |
| 130 |
$self->assert($expected == $resp_code, |
| 131 |
test_msg("Expected $expected, got $resp_code")); |
| 132 |
|
| 133 |
$expected = "User $user logged in"; |
| 134 |
$self->assert($expected eq $resp_msg, |
| 135 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 136 |
}; |
| 137 |
|
| 138 |
if ($@) { |
| 139 |
$ex = $@; |
| 140 |
} |
| 141 |
|
| 142 |
$wfh->print("done\n"); |
| 143 |
$wfh->flush(); |
| 144 |
|
| 145 |
} else { |
| 146 |
eval { server_wait($config_file, $rfh) }; |
| 147 |
if ($@) { |
| 148 |
warn($@); |
| 149 |
exit 1; |
| 150 |
} |
| 151 |
|
| 152 |
exit 0; |
| 153 |
} |
| 154 |
|
| 155 |
server_stop($pid_file); |
| 156 |
|
| 157 |
$self->assert_child_ok($pid); |
| 158 |
|
| 159 |
if ($ex) { |
| 160 |
test_append_logfile($log_file, $ex); |
| 161 |
unlink($log_file); |
| 162 |
|
| 163 |
die($ex); |
| 164 |
} |
| 165 |
|
| 166 |
unlink($log_file); |
| 167 |
} |
| 168 |
|
| 169 |
sub radius_acct { |
| 170 |
my $self = shift; |
| 171 |
my $tmpdir = $self->{tmpdir}; |
| 172 |
|
| 173 |
my $config_file = "$tmpdir/radius.conf"; |
| 174 |
my $pid_file = File::Spec->rel2abs("$tmpdir/radius.pid"); |
| 175 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/radius.scoreboard"); |
| 176 |
|
| 177 |
my $log_file = test_get_logfile(); |
| 178 |
|
| 179 |
my $user = $ENV{RADIUS_USER} ? $ENV{RADIUS_USER} : "proftpd"; |
| 180 |
my $passwd = $ENV{RADIUS_PASSWD} ? $ENV{RADIUS_PASSWD} : "test"; |
| 181 |
my $group = $ENV{RADIUS_GROUP} ? $ENV{RADIUS_GROUP} : 'ftpd'; |
| 182 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 183 |
my $uid = 1000; |
| 184 |
my $gid = 1000; |
| 185 |
|
| 186 |
# Make sure that, if we're running as root, that the home directory has |
| 187 |
# permissions/privs set for the account we create |
| 188 |
if ($< == 0) { |
| 189 |
unless (chmod(0755, $home_dir)) { |
| 190 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 191 |
} |
| 192 |
|
| 193 |
unless (chown($uid, $gid, $home_dir)) { |
| 194 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
my $config = { |
| 199 |
PidFile => $pid_file, |
| 200 |
ScoreboardFile => $scoreboard_file, |
| 201 |
SystemLog => $log_file, |
| 202 |
TraceLog => $log_file, |
| 203 |
Trace => 'auth:10', |
| 204 |
|
| 205 |
IfModules => { |
| 206 |
'mod_delay.c' => { |
| 207 |
DelayEngine => 'off', |
| 208 |
}, |
| 209 |
|
| 210 |
'mod_radius.c' => { |
| 211 |
RadiusEngine => 'on', |
| 212 |
RadiusLog => $log_file, |
| 213 |
RadiusAuthServer => "localhost:1812 testing123 5", |
| 214 |
RadiusAcctServer => "localhost:1813 testing123 5", |
| 215 |
RadiusUserInfo => "$uid $gid $home_dir /bin/bash", |
| 216 |
RadiusGroupInfo => "$group $user $gid", |
| 217 |
}, |
| 218 |
}, |
| 219 |
}; |
| 220 |
|
| 221 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 222 |
|
| 223 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 224 |
# the child will indicate when it's done with its test by writing a message |
| 225 |
# to the parent. |
| 226 |
my ($rfh, $wfh); |
| 227 |
unless (pipe($rfh, $wfh)) { |
| 228 |
die("Can't open pipe: $!"); |
| 229 |
} |
| 230 |
|
| 231 |
my $ex; |
| 232 |
|
| 233 |
# Make sure that radiusd is running before running these tests, e.g.: |
| 234 |
# |
| 235 |
# sudo /path/to/freeradius-dir/sbin/radiusd -X -f -xx |
| 236 |
|
| 237 |
# Fork child |
| 238 |
$self->handle_sigchld(); |
| 239 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 240 |
if ($pid) { |
| 241 |
eval { |
| 242 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 243 |
$client->login($user, $passwd); |
| 244 |
|
| 245 |
my ($resp_code, $resp_msg); |
| 246 |
$resp_code = $client->response_code(); |
| 247 |
$resp_msg = $client->response_msg(); |
| 248 |
|
| 249 |
my $expected; |
| 250 |
|
| 251 |
$expected = 230; |
| 252 |
$self->assert($expected == $resp_code, |
| 253 |
test_msg("Expected $expected, got $resp_code")); |
| 254 |
|
| 255 |
$expected = "User $user logged in"; |
| 256 |
$self->assert($expected eq $resp_msg, |
| 257 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 258 |
}; |
| 259 |
|
| 260 |
if ($@) { |
| 261 |
$ex = $@; |
| 262 |
} |
| 263 |
|
| 264 |
$wfh->print("done\n"); |
| 265 |
$wfh->flush(); |
| 266 |
|
| 267 |
} else { |
| 268 |
eval { server_wait($config_file, $rfh) }; |
| 269 |
if ($@) { |
| 270 |
warn($@); |
| 271 |
exit 1; |
| 272 |
} |
| 273 |
|
| 274 |
exit 0; |
| 275 |
} |
| 276 |
|
| 277 |
server_stop($pid_file); |
| 278 |
|
| 279 |
$self->assert_child_ok($pid); |
| 280 |
|
| 281 |
if ($ex) { |
| 282 |
test_append_logfile($log_file, $ex); |
| 283 |
unlink($log_file); |
| 284 |
|
| 285 |
die($ex); |
| 286 |
} |
| 287 |
|
| 288 |
unlink($log_file); |
| 289 |
} |
| 290 |
|
| 291 |
sub radius_acct_aborted_xfer_bug3278 { |
| 292 |
my $self = shift; |
| 293 |
my $tmpdir = $self->{tmpdir}; |
| 294 |
|
| 295 |
my $config_file = "$tmpdir/radius.conf"; |
| 296 |
my $pid_file = File::Spec->rel2abs("$tmpdir/radius.pid"); |
| 297 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/radius.scoreboard"); |
| 298 |
|
| 299 |
my $log_file = test_get_logfile(); |
| 300 |
|
| 301 |
my $user = $ENV{RADIUS_USER} ? $ENV{RADIUS_USER} : "proftpd"; |
| 302 |
my $passwd = $ENV{RADIUS_PASSWD} ? $ENV{RADIUS_PASSWD} : "test"; |
| 303 |
my $group = $ENV{RADIUS_GROUP} ? $ENV{RADIUS_GROUP} : 'ftpd'; |
| 304 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 305 |
my $uid = 1000; |
| 306 |
my $gid = 1000; |
| 307 |
|
| 308 |
# Make sure that, if we're running as root, that the home directory has |
| 309 |
# permissions/privs set for the account we create |
| 310 |
if ($< == 0) { |
| 311 |
unless (chmod(0755, $home_dir)) { |
| 312 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 313 |
} |
| 314 |
|
| 315 |
unless (chown($uid, $gid, $home_dir)) { |
| 316 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
my $config = { |
| 321 |
PidFile => $pid_file, |
| 322 |
ScoreboardFile => $scoreboard_file, |
| 323 |
SystemLog => $log_file, |
| 324 |
TraceLog => $log_file, |
| 325 |
Trace => 'auth:10', |
| 326 |
|
| 327 |
IfModules => { |
| 328 |
'mod_delay.c' => { |
| 329 |
DelayEngine => 'off', |
| 330 |
}, |
| 331 |
|
| 332 |
'mod_radius.c' => { |
| 333 |
RadiusEngine => 'on', |
| 334 |
RadiusLog => $log_file, |
| 335 |
RadiusAuthServer => "localhost:1812 testing123 5", |
| 336 |
RadiusAcctServer => "localhost:1813 testing123 5", |
| 337 |
RadiusUserInfo => "$uid $gid $home_dir /bin/bash", |
| 338 |
RadiusGroupInfo => "$group $user $gid", |
| 339 |
}, |
| 340 |
}, |
| 341 |
}; |
| 342 |
|
| 343 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 344 |
|
| 345 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 346 |
# the child will indicate when it's done with its test by writing a message |
| 347 |
# to the parent. |
| 348 |
my ($rfh, $wfh); |
| 349 |
unless (pipe($rfh, $wfh)) { |
| 350 |
die("Can't open pipe: $!"); |
| 351 |
} |
| 352 |
|
| 353 |
my $ex; |
| 354 |
|
| 355 |
# Make sure that radiusd is running before running these tests, e.g.: |
| 356 |
# |
| 357 |
# sudo /path/to/freeradius-dir/sbin/radiusd -X -f -xx |
| 358 |
|
| 359 |
# Fork child |
| 360 |
$self->handle_sigchld(); |
| 361 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 362 |
if ($pid) { |
| 363 |
eval { |
| 364 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 365 |
$client->login($user, $passwd); |
| 366 |
|
| 367 |
my ($resp_code, $resp_msg); |
| 368 |
$resp_code = $client->response_code(); |
| 369 |
$resp_msg = $client->response_msg(); |
| 370 |
|
| 371 |
my $expected; |
| 372 |
|
| 373 |
$expected = 230; |
| 374 |
$self->assert($expected == $resp_code, |
| 375 |
test_msg("Expected $expected, got $resp_code")); |
| 376 |
|
| 377 |
$expected = "User $user logged in"; |
| 378 |
$self->assert($expected eq $resp_msg, |
| 379 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 380 |
|
| 381 |
my $conn = $client->stor_raw('test.txt'); |
| 382 |
unless ($conn) { |
| 383 |
die("Failed to STOR test.txt: " . $client->response_code() . " " . |
| 384 |
$client->response_msg()); |
| 385 |
} |
| 386 |
|
| 387 |
for (my $i = 0; $i < 1000; $i++) { |
| 388 |
my $buf = "ABCD" x 1024; |
| 389 |
$conn->write($buf, length($buf)); |
| 390 |
} |
| 391 |
|
| 392 |
$client->quit(); |
| 393 |
}; |
| 394 |
|
| 395 |
if ($@) { |
| 396 |
$ex = $@; |
| 397 |
} |
| 398 |
|
| 399 |
$wfh->print("done\n"); |
| 400 |
$wfh->flush(); |
| 401 |
|
| 402 |
} else { |
| 403 |
eval { server_wait($config_file, $rfh) }; |
| 404 |
if ($@) { |
| 405 |
warn($@); |
| 406 |
exit 1; |
| 407 |
} |
| 408 |
|
| 409 |
exit 0; |
| 410 |
} |
| 411 |
|
| 412 |
server_stop($pid_file); |
| 413 |
|
| 414 |
$self->assert_child_ok($pid); |
| 415 |
|
| 416 |
if ($ex) { |
| 417 |
test_append_logfile($log_file, $ex); |
| 418 |
unlink($log_file); |
| 419 |
|
| 420 |
die($ex); |
| 421 |
} |
| 422 |
|
| 423 |
unlink($log_file); |
| 424 |
} |
| 425 |
|
| 426 |
sub radius_userinfo_var_u { |
| 427 |
my $self = shift; |
| 428 |
my $tmpdir = $self->{tmpdir}; |
| 429 |
|
| 430 |
my $config_file = "$tmpdir/radius.conf"; |
| 431 |
my $pid_file = File::Spec->rel2abs("$tmpdir/radius.pid"); |
| 432 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/radius.scoreboard"); |
| 433 |
|
| 434 |
my $log_file = test_get_logfile(); |
| 435 |
|
| 436 |
my $user = $ENV{RADIUS_USER} ? $ENV{RADIUS_USER} : "proftpd"; |
| 437 |
my $passwd = $ENV{RADIUS_PASSWD} ? $ENV{RADIUS_PASSWD} : "test"; |
| 438 |
my $group = $ENV{RADIUS_GROUP} ? $ENV{RADIUS_GROUP} : 'ftpd'; |
| 439 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 440 |
my $uid = 1000; |
| 441 |
my $gid = 1000; |
| 442 |
|
| 443 |
# Make sure that, if we're running as root, that the home directory has |
| 444 |
# permissions/privs set for the account we create |
| 445 |
if ($< == 0) { |
| 446 |
unless (chmod(0755, $home_dir)) { |
| 447 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 448 |
} |
| 449 |
|
| 450 |
unless (chown($uid, $gid, $home_dir)) { |
| 451 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
my $config = { |
| 456 |
PidFile => $pid_file, |
| 457 |
ScoreboardFile => $scoreboard_file, |
| 458 |
SystemLog => $log_file, |
| 459 |
TraceLog => $log_file, |
| 460 |
Trace => 'auth:10', |
| 461 |
|
| 462 |
IfModules => { |
| 463 |
'mod_delay.c' => { |
| 464 |
DelayEngine => 'off', |
| 465 |
}, |
| 466 |
|
| 467 |
'mod_radius.c' => { |
| 468 |
RadiusEngine => 'on', |
| 469 |
RadiusLog => $log_file, |
| 470 |
RadiusAuthServer => "localhost:1812 testing123 5", |
| 471 |
RadiusUserInfo => "$uid $gid /home/%u /bin/bash", |
| 472 |
RadiusGroupInfo => "$group $user $gid", |
| 473 |
}, |
| 474 |
}, |
| 475 |
}; |
| 476 |
|
| 477 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 478 |
|
| 479 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 480 |
# the child will indicate when it's done with its test by writing a message |
| 481 |
# to the parent. |
| 482 |
my ($rfh, $wfh); |
| 483 |
unless (pipe($rfh, $wfh)) { |
| 484 |
die("Can't open pipe: $!"); |
| 485 |
} |
| 486 |
|
| 487 |
my $ex; |
| 488 |
|
| 489 |
# Make sure that radiusd is running before running these tests, e.g.: |
| 490 |
# |
| 491 |
# sudo /path/to/freeradius-dir/sbin/radiusd -X -f -xx |
| 492 |
|
| 493 |
# Fork child |
| 494 |
$self->handle_sigchld(); |
| 495 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 496 |
if ($pid) { |
| 497 |
eval { |
| 498 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 499 |
$client->login($user, $passwd); |
| 500 |
|
| 501 |
my ($resp_code, $resp_msg); |
| 502 |
$resp_code = $client->response_code(); |
| 503 |
$resp_msg = $client->response_msg(); |
| 504 |
|
| 505 |
my $expected; |
| 506 |
|
| 507 |
$expected = 230; |
| 508 |
$self->assert($expected == $resp_code, |
| 509 |
test_msg("Expected $expected, got $resp_code")); |
| 510 |
|
| 511 |
$expected = "User $user logged in"; |
| 512 |
$self->assert($expected eq $resp_msg, |
| 513 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 514 |
}; |
| 515 |
|
| 516 |
if ($@) { |
| 517 |
$ex = $@; |
| 518 |
} |
| 519 |
|
| 520 |
$wfh->print("done\n"); |
| 521 |
$wfh->flush(); |
| 522 |
|
| 523 |
} else { |
| 524 |
eval { server_wait($config_file, $rfh) }; |
| 525 |
if ($@) { |
| 526 |
warn($@); |
| 527 |
exit 1; |
| 528 |
} |
| 529 |
|
| 530 |
exit 0; |
| 531 |
} |
| 532 |
|
| 533 |
server_stop($pid_file); |
| 534 |
|
| 535 |
$self->assert_child_ok($pid); |
| 536 |
|
| 537 |
if ($ex) { |
| 538 |
test_append_logfile($log_file, $ex); |
| 539 |
unlink($log_file); |
| 540 |
|
| 541 |
die($ex); |
| 542 |
} |
| 543 |
|
| 544 |
unlink($log_file); |
| 545 |
} |
| 546 |
|
| 547 |
1; |