| 1 |
package ProFTPD::Tests::Modules::mod_sql_passwd::FIPS; |
| 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 |
sql_passwd_fips_md5_base64 => { |
| 19 |
order => ++$order, |
| 20 |
test_class => [qw(forking)], |
| 21 |
}, |
| 22 |
|
| 23 |
sql_passwd_fips_sha1_base64 => { |
| 24 |
order => ++$order, |
| 25 |
test_class => [qw(forking)], |
| 26 |
}, |
| 27 |
|
| 28 |
}; |
| 29 |
|
| 30 |
sub new { |
| 31 |
return shift()->SUPER::new(@_); |
| 32 |
} |
| 33 |
|
| 34 |
sub list_tests { |
| 35 |
return testsuite_get_runnable_tests($TESTS); |
| 36 |
} |
| 37 |
|
| 38 |
sub sql_passwd_fips_md5_base64 { |
| 39 |
my $self = shift; |
| 40 |
my $tmpdir = $self->{tmpdir}; |
| 41 |
|
| 42 |
my $config_file = "$tmpdir/fips.conf"; |
| 43 |
my $pid_file = File::Spec->rel2abs("$tmpdir/fips.pid"); |
| 44 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/fips.scoreboard"); |
| 45 |
|
| 46 |
my $log_file = test_get_logfile(); |
| 47 |
|
| 48 |
my $user = 'proftpd'; |
| 49 |
|
| 50 |
# I used: |
| 51 |
# |
| 52 |
# `/bin/echo -n "test" | openssl dgst -binary -md5 | openssl enc -base64` |
| 53 |
# |
| 54 |
# to generate this password. |
| 55 |
my $passwd = 'CY9rzUYh03PK3k6DJie09g=='; |
| 56 |
|
| 57 |
my $group = 'ftpd'; |
| 58 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 59 |
my $uid = 500; |
| 60 |
my $gid = 500; |
| 61 |
|
| 62 |
my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db"); |
| 63 |
|
| 64 |
# Build up sqlite3 command to create users, groups tables and populate them |
| 65 |
my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql"); |
| 66 |
|
| 67 |
if (open(my $fh, "> $db_script")) { |
| 68 |
print $fh <<EOS; |
| 69 |
CREATE TABLE users ( |
| 70 |
userid TEXT, |
| 71 |
passwd TEXT, |
| 72 |
uid INTEGER, |
| 73 |
gid INTEGER, |
| 74 |
homedir TEXT, |
| 75 |
shell TEXT |
| 76 |
); |
| 77 |
INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash'); |
| 78 |
|
| 79 |
CREATE TABLE groups ( |
| 80 |
groupname TEXT, |
| 81 |
gid INTEGER, |
| 82 |
members TEXT |
| 83 |
); |
| 84 |
INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user'); |
| 85 |
EOS |
| 86 |
|
| 87 |
unless (close($fh)) { |
| 88 |
die("Can't write $db_script: $!"); |
| 89 |
} |
| 90 |
|
| 91 |
} else { |
| 92 |
die("Can't open $db_script: $!"); |
| 93 |
} |
| 94 |
|
| 95 |
my $cmd = "sqlite3 $db_file < $db_script"; |
| 96 |
|
| 97 |
if ($ENV{TEST_VERBOSE}) { |
| 98 |
print STDERR "Executing sqlite3: $cmd\n"; |
| 99 |
} |
| 100 |
|
| 101 |
my @output = `$cmd`; |
| 102 |
if (scalar(@output) && |
| 103 |
$ENV{TEST_VERBOSE}) { |
| 104 |
print STDERR "Output: ", join('', @output), "\n"; |
| 105 |
} |
| 106 |
|
| 107 |
my $config = { |
| 108 |
PidFile => $pid_file, |
| 109 |
ScoreboardFile => $scoreboard_file, |
| 110 |
SystemLog => $log_file, |
| 111 |
|
| 112 |
IfModules => { |
| 113 |
'mod_delay.c' => { |
| 114 |
DelayEngine => 'off', |
| 115 |
}, |
| 116 |
|
| 117 |
'mod_sql.c' => { |
| 118 |
SQLAuthTypes => 'md5', |
| 119 |
SQLBackend => 'sqlite3', |
| 120 |
SQLConnectInfo => $db_file, |
| 121 |
SQLLogFile => $log_file, |
| 122 |
}, |
| 123 |
|
| 124 |
'mod_sql_passwd.c' => { |
| 125 |
SQLPasswordEngine => 'on', |
| 126 |
SQLPasswordEncoding => 'base64', |
| 127 |
}, |
| 128 |
}, |
| 129 |
}; |
| 130 |
|
| 131 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 132 |
|
| 133 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 134 |
# the child will indicate when it's done with its test by writing a message |
| 135 |
# to the parent. |
| 136 |
my ($rfh, $wfh); |
| 137 |
unless (pipe($rfh, $wfh)) { |
| 138 |
die("Can't open pipe: $!"); |
| 139 |
} |
| 140 |
|
| 141 |
my $ex; |
| 142 |
|
| 143 |
# Fork child |
| 144 |
$self->handle_sigchld(); |
| 145 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 146 |
if ($pid) { |
| 147 |
eval { |
| 148 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 149 |
eval { $client->login($user, "test") }; |
| 150 |
unless ($@) { |
| 151 |
die("Login succeeded unexpectedly"); |
| 152 |
} |
| 153 |
|
| 154 |
my $resp_code = $client->response_code(); |
| 155 |
my $resp_msg = $client->response_msg(); |
| 156 |
|
| 157 |
my $expected; |
| 158 |
|
| 159 |
$expected = 530; |
| 160 |
$self->assert($expected == $resp_code, |
| 161 |
test_msg("Expected response code $expected, got $resp_code")); |
| 162 |
|
| 163 |
$expected = "Login incorrect."; |
| 164 |
$self->assert($expected eq $resp_msg, |
| 165 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 166 |
}; |
| 167 |
|
| 168 |
if ($@) { |
| 169 |
$ex = $@; |
| 170 |
} |
| 171 |
|
| 172 |
$wfh->print("done\n"); |
| 173 |
$wfh->flush(); |
| 174 |
|
| 175 |
} else { |
| 176 |
eval { server_wait($config_file, $rfh, { env => { OPENSSL_FIPS => 1 } }) }; |
| 177 |
if ($@) { |
| 178 |
warn($@); |
| 179 |
exit 1; |
| 180 |
} |
| 181 |
|
| 182 |
exit 0; |
| 183 |
} |
| 184 |
|
| 185 |
# Stop server |
| 186 |
server_stop($pid_file); |
| 187 |
|
| 188 |
$self->assert_child_ok($pid); |
| 189 |
|
| 190 |
if ($ex) { |
| 191 |
test_append_logfile($log_file, $ex); |
| 192 |
unlink($log_file); |
| 193 |
|
| 194 |
die($ex); |
| 195 |
} |
| 196 |
|
| 197 |
unlink($log_file); |
| 198 |
} |
| 199 |
|
| 200 |
sub sql_passwd_fips_sha1_base64 { |
| 201 |
my $self = shift; |
| 202 |
my $tmpdir = $self->{tmpdir}; |
| 203 |
|
| 204 |
my $config_file = "$tmpdir/fips.conf"; |
| 205 |
my $pid_file = File::Spec->rel2abs("$tmpdir/fips.pid"); |
| 206 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/fips.scoreboard"); |
| 207 |
|
| 208 |
my $log_file = test_get_logfile(); |
| 209 |
|
| 210 |
my $user = 'proftpd'; |
| 211 |
|
| 212 |
# I used: |
| 213 |
# |
| 214 |
# `/bin/echo -n "test" | openssl dgst -binary -sha1 | openssl enc -base64` |
| 215 |
# |
| 216 |
# to generate this password. |
| 217 |
my $passwd = 'qUqP5cyxm6YcTAhz05Hph5gvu9M='; |
| 218 |
|
| 219 |
my $group = 'ftpd'; |
| 220 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 221 |
my $uid = 500; |
| 222 |
my $gid = 500; |
| 223 |
|
| 224 |
my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db"); |
| 225 |
|
| 226 |
# Build up sqlite3 command to create users, groups tables and populate them |
| 227 |
my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql"); |
| 228 |
|
| 229 |
if (open(my $fh, "> $db_script")) { |
| 230 |
print $fh <<EOS; |
| 231 |
CREATE TABLE users ( |
| 232 |
userid TEXT, |
| 233 |
passwd TEXT, |
| 234 |
uid INTEGER, |
| 235 |
gid INTEGER, |
| 236 |
homedir TEXT, |
| 237 |
shell TEXT |
| 238 |
); |
| 239 |
INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash'); |
| 240 |
|
| 241 |
CREATE TABLE groups ( |
| 242 |
groupname TEXT, |
| 243 |
gid INTEGER, |
| 244 |
members TEXT |
| 245 |
); |
| 246 |
INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user'); |
| 247 |
EOS |
| 248 |
|
| 249 |
unless (close($fh)) { |
| 250 |
die("Can't write $db_script: $!"); |
| 251 |
} |
| 252 |
|
| 253 |
} else { |
| 254 |
die("Can't open $db_script: $!"); |
| 255 |
} |
| 256 |
|
| 257 |
my $cmd = "sqlite3 $db_file < $db_script"; |
| 258 |
|
| 259 |
if ($ENV{TEST_VERBOSE}) { |
| 260 |
print STDERR "Executing sqlite3: $cmd\n"; |
| 261 |
} |
| 262 |
|
| 263 |
my @output = `$cmd`; |
| 264 |
if (scalar(@output) && |
| 265 |
$ENV{TEST_VERBOSE}) { |
| 266 |
print STDERR "Output: ", join('', @output), "\n"; |
| 267 |
} |
| 268 |
|
| 269 |
my $config = { |
| 270 |
PidFile => $pid_file, |
| 271 |
ScoreboardFile => $scoreboard_file, |
| 272 |
SystemLog => $log_file, |
| 273 |
|
| 274 |
IfModules => { |
| 275 |
'mod_delay.c' => { |
| 276 |
DelayEngine => 'off', |
| 277 |
}, |
| 278 |
|
| 279 |
'mod_sql.c' => { |
| 280 |
SQLAuthTypes => 'sha1', |
| 281 |
SQLBackend => 'sqlite3', |
| 282 |
SQLConnectInfo => $db_file, |
| 283 |
SQLLogFile => $log_file, |
| 284 |
}, |
| 285 |
|
| 286 |
'mod_sql_passwd.c' => { |
| 287 |
SQLPasswordEngine => 'on', |
| 288 |
SQLPasswordEncoding => 'base64', |
| 289 |
}, |
| 290 |
}, |
| 291 |
}; |
| 292 |
|
| 293 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 294 |
|
| 295 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 296 |
# the child will indicate when it's done with its test by writing a message |
| 297 |
# to the parent. |
| 298 |
my ($rfh, $wfh); |
| 299 |
unless (pipe($rfh, $wfh)) { |
| 300 |
die("Can't open pipe: $!"); |
| 301 |
} |
| 302 |
|
| 303 |
my $ex; |
| 304 |
|
| 305 |
# Fork child |
| 306 |
$self->handle_sigchld(); |
| 307 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 308 |
if ($pid) { |
| 309 |
eval { |
| 310 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 311 |
$client->login($user, "test"); |
| 312 |
|
| 313 |
my $resp_msgs = $client->response_msgs(); |
| 314 |
my $nmsgs = scalar(@$resp_msgs); |
| 315 |
|
| 316 |
my $expected; |
| 317 |
|
| 318 |
$expected = 1; |
| 319 |
$self->assert($expected == $nmsgs, |
| 320 |
test_msg("Expected $expected, got $nmsgs")); |
| 321 |
|
| 322 |
$expected = "User proftpd logged in"; |
| 323 |
$self->assert($expected eq $resp_msgs->[0], |
| 324 |
test_msg("Expected '$expected', got '$resp_msgs->[0]'")); |
| 325 |
|
| 326 |
}; |
| 327 |
|
| 328 |
if ($@) { |
| 329 |
$ex = $@; |
| 330 |
} |
| 331 |
|
| 332 |
$wfh->print("done\n"); |
| 333 |
$wfh->flush(); |
| 334 |
|
| 335 |
} else { |
| 336 |
eval { server_wait($config_file, $rfh, { env => { OPENSSL_FIPS => 1 } }) }; |
| 337 |
if ($@) { |
| 338 |
warn($@); |
| 339 |
exit 1; |
| 340 |
} |
| 341 |
|
| 342 |
exit 0; |
| 343 |
} |
| 344 |
|
| 345 |
# Stop server |
| 346 |
server_stop($pid_file); |
| 347 |
|
| 348 |
$self->assert_child_ok($pid); |
| 349 |
|
| 350 |
if ($ex) { |
| 351 |
test_append_logfile($log_file, $ex); |
| 352 |
unlink($log_file); |
| 353 |
|
| 354 |
die($ex); |
| 355 |
} |
| 356 |
|
| 357 |
unlink($log_file); |
| 358 |
} |
| 359 |
|
| 360 |
1; |