|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use strict; |
|
|
use warnings; |
|
|
use Cwd; |
|
|
use Cwd 'abs_path'; |
|
|
use Digest::MD5; |
|
|
use Digest::MD5 'md5_hex'; |
|
|
use Digest::SHA; |
|
|
use Digest::SHA 'sha256_base64'; |
|
|
use MIME::Base64; |
|
|
use File::Basename; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use sshhelp qw( |
|
|
$sshdexe |
|
|
$sshexe |
|
|
$sftpsrvexe |
|
|
$sftpexe |
|
|
$sshkeygenexe |
|
|
$sshdconfig |
|
|
$sshconfig |
|
|
$sftpconfig |
|
|
$knownhosts |
|
|
$sshdlog |
|
|
$sshlog |
|
|
$sftplog |
|
|
$sftpcmds |
|
|
$hstprvkeyf |
|
|
$hstpubkeyf |
|
|
$hstpubmd5f |
|
|
$hstpubsha256f |
|
|
$cliprvkeyf |
|
|
$clipubkeyf |
|
|
display_sshdconfig |
|
|
display_sshconfig |
|
|
display_sftpconfig |
|
|
display_sshdlog |
|
|
display_sshlog |
|
|
display_sftplog |
|
|
dump_array |
|
|
find_sshd |
|
|
find_ssh |
|
|
find_sftpsrv |
|
|
find_sftp |
|
|
find_sshkeygen |
|
|
sshversioninfo |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use serverhelp qw( |
|
|
$logfile |
|
|
server_pidfilename |
|
|
server_logfilename |
|
|
); |
|
|
|
|
|
use pathhelp; |
|
|
|
|
|
|
|
|
|
|
|
my $verbose = 0; |
|
|
my $debugprotocol = 0; |
|
|
my $port = 8999; |
|
|
my $listenaddr = '127.0.0.1'; |
|
|
my $ipvnum = 4; |
|
|
my $idnum = 1; |
|
|
my $proto = 'ssh'; |
|
|
my $path = getcwd(); |
|
|
my $logdir = $path .'/log'; |
|
|
my $piddir; |
|
|
my $username = $ENV{USER}; |
|
|
my $pidfile; |
|
|
my $identity = 'curl_client_key'; |
|
|
|
|
|
my $error; |
|
|
my @cfgarr; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub pp { |
|
|
my $file = $_[0]; |
|
|
return "$piddir/$file"; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub logmsg { |
|
|
my $msg = $_[0]; |
|
|
serverhelp::logmsg $msg; |
|
|
print $msg; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(@ARGV) { |
|
|
if($ARGV[0] eq '--verbose') { |
|
|
$verbose = 1; |
|
|
} |
|
|
elsif($ARGV[0] eq '--debugprotocol') { |
|
|
$verbose = 1; |
|
|
$debugprotocol = 1; |
|
|
} |
|
|
elsif($ARGV[0] eq '--user') { |
|
|
if($ARGV[1]) { |
|
|
$username = $ARGV[1]; |
|
|
shift @ARGV; |
|
|
} |
|
|
} |
|
|
elsif($ARGV[0] eq '--id') { |
|
|
if($ARGV[1]) { |
|
|
if($ARGV[1] =~ /^(\d+)$/) { |
|
|
$idnum = $1 if($1 > 0); |
|
|
shift @ARGV; |
|
|
} |
|
|
} |
|
|
} |
|
|
elsif($ARGV[0] eq '--ipv4') { |
|
|
$ipvnum = 4; |
|
|
$listenaddr = '127.0.0.1' if($listenaddr eq '::1'); |
|
|
} |
|
|
elsif($ARGV[0] eq '--ipv6') { |
|
|
$ipvnum = 6; |
|
|
$listenaddr = '::1' if($listenaddr eq '127.0.0.1'); |
|
|
} |
|
|
elsif($ARGV[0] eq '--addr') { |
|
|
if($ARGV[1]) { |
|
|
my $tmpstr = $ARGV[1]; |
|
|
if($tmpstr =~ /^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/) { |
|
|
$listenaddr = "$1.$2.$3.$4" if($ipvnum == 4); |
|
|
shift @ARGV; |
|
|
} |
|
|
elsif($ipvnum == 6) { |
|
|
$listenaddr = $tmpstr; |
|
|
$listenaddr =~ s/^\[(.*)\]$/$1/; |
|
|
shift @ARGV; |
|
|
} |
|
|
} |
|
|
} |
|
|
elsif($ARGV[0] eq '--pidfile') { |
|
|
if($ARGV[1]) { |
|
|
$pidfile = "$path/". $ARGV[1]; |
|
|
shift @ARGV; |
|
|
} |
|
|
} |
|
|
elsif($ARGV[0] eq '--logdir') { |
|
|
if($ARGV[1]) { |
|
|
$logdir = "$path/". $ARGV[1]; |
|
|
shift @ARGV; |
|
|
} |
|
|
} |
|
|
elsif($ARGV[0] eq '--sshport') { |
|
|
if($ARGV[1]) { |
|
|
if($ARGV[1] =~ /^(\d+)$/) { |
|
|
$port = $1; |
|
|
shift @ARGV; |
|
|
} |
|
|
} |
|
|
} |
|
|
else { |
|
|
print STDERR "\nWarning: sshserver.pl unknown parameter: $ARGV[0]\n"; |
|
|
} |
|
|
shift @ARGV; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if($pidfile) { |
|
|
|
|
|
$piddir = dirname($pidfile); |
|
|
} |
|
|
else { |
|
|
|
|
|
$piddir = $path; |
|
|
$pidfile = server_pidfilename($piddir, $proto, $ipvnum, $idnum); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$sshdlog = server_logfilename($logdir, 'ssh', $ipvnum, $idnum); |
|
|
$sftplog = server_logfilename($logdir, 'sftp', $ipvnum, $idnum); |
|
|
$logfile = "$logdir/sshserver.log"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $loglevel = $debugprotocol?'DEBUG3':'DEBUG2'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!$username) { |
|
|
$error = 'Will not run ssh server without a user name'; |
|
|
} |
|
|
elsif($username eq 'root') { |
|
|
$error = 'Will not run ssh server as root to mitigate security risks'; |
|
|
} |
|
|
if($error) { |
|
|
logmsg "$error\n"; |
|
|
exit 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $sshd = find_sshd(); |
|
|
if(!$sshd) { |
|
|
logmsg "cannot find $sshdexe\n"; |
|
|
exit 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my ($sshdid, $sshdvernum, $sshdverstr, $sshderror) = sshversioninfo($sshd); |
|
|
if(!$sshdid) { |
|
|
|
|
|
logmsg "$sshderror\n" if($verbose); |
|
|
logmsg "SCP and SFTP tests require OpenSSH 2.9.9 or later\n"; |
|
|
exit 1; |
|
|
} |
|
|
logmsg "ssh server found $sshd is $sshdverstr\n" if($verbose); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if((($sshdid =~ /OpenSSH/) && ($sshdvernum < 299)) || |
|
|
(($sshdid =~ /SunSSH/) && ($sshdvernum < 100))) { |
|
|
logmsg "SCP and SFTP tests require OpenSSH 2.9.9 or later\n"; |
|
|
exit 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $sftpsrv = find_sftpsrv(); |
|
|
if(!$sftpsrv) { |
|
|
logmsg "cannot find $sftpsrvexe\n"; |
|
|
exit 1; |
|
|
} |
|
|
logmsg "sftp server plugin found $sftpsrv\n" if($verbose); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $sftp = find_sftp(); |
|
|
if(!$sftp) { |
|
|
logmsg "cannot find $sftpexe\n"; |
|
|
exit 1; |
|
|
} |
|
|
logmsg "sftp client found $sftp\n" if($verbose); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $sshkeygen = find_sshkeygen(); |
|
|
if(!$sshkeygen) { |
|
|
logmsg "cannot find $sshkeygenexe\n"; |
|
|
exit 1; |
|
|
} |
|
|
logmsg "ssh keygen found $sshkeygen\n" if($verbose); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $ssh = find_ssh(); |
|
|
if(!$ssh) { |
|
|
logmsg "cannot find $sshexe\n"; |
|
|
exit 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my ($sshid, $sshvernum, $sshverstr, $ssherror) = sshversioninfo($ssh); |
|
|
if(!$sshid) { |
|
|
|
|
|
logmsg "$ssherror\n" if($verbose); |
|
|
logmsg "SCP and SFTP tests require OpenSSH 2.9.9 or later\n"; |
|
|
exit 1; |
|
|
} |
|
|
logmsg "ssh client found $ssh is $sshverstr\n" if($verbose); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if((($sshid =~ /OpenSSH/) && ($sshvernum < 299)) || |
|
|
(($sshid =~ /SunSSH/) && ($sshvernum < 100))) { |
|
|
logmsg "SCP and SFTP tests require OpenSSH 2.9.9 or later\n"; |
|
|
exit 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$sshdconfig = pp($sshdconfig); |
|
|
$sshconfig = pp($sshconfig); |
|
|
$sftpconfig = pp($sftpconfig); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if((! -e pp($hstprvkeyf)) || (! -s pp($hstprvkeyf)) || |
|
|
(! -e pp($hstpubkeyf)) || (! -s pp($hstpubkeyf)) || |
|
|
(! -e pp($hstpubmd5f)) || (! -s pp($hstpubmd5f)) || |
|
|
(! -e pp($hstpubsha256f)) || (! -s pp($hstpubsha256f)) || |
|
|
(! -e pp($cliprvkeyf)) || (! -s pp($cliprvkeyf)) || |
|
|
(! -e pp($clipubkeyf)) || (! -s pp($clipubkeyf))) { |
|
|
|
|
|
unlink(pp($hstprvkeyf), pp($hstpubkeyf), pp($hstpubmd5f), |
|
|
pp($hstpubsha256f), pp($cliprvkeyf), pp($clipubkeyf)); |
|
|
logmsg "generating host keys...\n" if($verbose); |
|
|
if(system "\"$sshkeygen\" -q -t rsa -f " . pp($hstprvkeyf) . " -C 'curl test server' -N ''") { |
|
|
logmsg "Could not generate host key\n"; |
|
|
exit 1; |
|
|
} |
|
|
logmsg "generating client keys...\n" if($verbose); |
|
|
if(system "\"$sshkeygen\" -q -t rsa -f " . pp($cliprvkeyf) . " -C 'curl test client' -N ''") { |
|
|
logmsg "Could not generate client key\n"; |
|
|
exit 1; |
|
|
} |
|
|
|
|
|
system "chmod 600 " . pp($hstprvkeyf); |
|
|
system "chmod 600 " . pp($cliprvkeyf); |
|
|
if(pathhelp::os_is_win()) { |
|
|
|
|
|
$ENV{'MSYS2_ARG_CONV_EXCL'} = '/reset'; |
|
|
system("icacls \"" . pathhelp::sys_native_abs_path(pp($hstprvkeyf)) . "\" /reset"); |
|
|
system("icacls \"" . pathhelp::sys_native_abs_path(pp($hstprvkeyf)) . "\" /grant:r \"$username:(R)\""); |
|
|
system("icacls \"" . pathhelp::sys_native_abs_path(pp($hstprvkeyf)) . "\" /inheritance:r"); |
|
|
} |
|
|
|
|
|
open(my $rsakeyfile, "<", pp($hstpubkeyf)); |
|
|
my @rsahostkey = do { local $/ = ' '; <$rsakeyfile> }; |
|
|
close($rsakeyfile); |
|
|
if(!$rsahostkey[1]) { |
|
|
logmsg "Failed parsing base64 encoded RSA host key\n"; |
|
|
exit 1; |
|
|
} |
|
|
open(my $pubmd5file, ">", pp($hstpubmd5f)); |
|
|
print $pubmd5file md5_hex(decode_base64($rsahostkey[1])); |
|
|
close($pubmd5file); |
|
|
if((! -e pp($hstpubmd5f)) || (! -s pp($hstpubmd5f))) { |
|
|
logmsg "Failed writing md5 hash of RSA host key\n"; |
|
|
exit 1; |
|
|
} |
|
|
open(my $pubsha256file, ">", pp($hstpubsha256f)); |
|
|
print $pubsha256file sha256_base64(decode_base64($rsahostkey[1])); |
|
|
close($pubsha256file); |
|
|
if((! -e pp($hstpubsha256f)) || (! -s pp($hstpubsha256f))) { |
|
|
logmsg "Failed writing sha256 hash of RSA host key\n"; |
|
|
exit 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $clipubkeyf_config; |
|
|
my $hstprvkeyf_config; |
|
|
my $pidfile_config; |
|
|
my $sftpsrv_config; |
|
|
my $sshdconfig_abs; |
|
|
if ($sshdid =~ /OpenSSH-Windows/) { |
|
|
|
|
|
$clipubkeyf_config = pathhelp::sys_native_abs_path(pp($clipubkeyf)); |
|
|
$hstprvkeyf_config = pathhelp::sys_native_abs_path(pp($hstprvkeyf)); |
|
|
$pidfile_config = pathhelp::sys_native_abs_path($pidfile); |
|
|
$sftpsrv_config = pathhelp::sys_native_abs_path($sftpsrv); |
|
|
$sshdconfig_abs = pathhelp::sys_native_abs_path($sshdconfig); |
|
|
} |
|
|
elsif (pathhelp::os_is_win()) { |
|
|
|
|
|
$clipubkeyf_config = pathhelp::build_sys_abs_path(pp($clipubkeyf)); |
|
|
$hstprvkeyf_config = pathhelp::build_sys_abs_path(pp($hstprvkeyf)); |
|
|
$pidfile_config = pathhelp::build_sys_abs_path($pidfile); |
|
|
$sftpsrv_config = "internal-sftp"; |
|
|
$sshdconfig_abs = pathhelp::build_sys_abs_path($sshdconfig); |
|
|
} |
|
|
else { |
|
|
$clipubkeyf_config = abs_path(pp($clipubkeyf)); |
|
|
$hstprvkeyf_config = abs_path(pp($hstprvkeyf)); |
|
|
$pidfile_config = $pidfile; |
|
|
$sftpsrv_config = $sftpsrv; |
|
|
$sshdconfig_abs = abs_path($sshdconfig); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logmsg "generating ssh server config file...\n" if($verbose); |
|
|
@cfgarr = (); |
|
|
push @cfgarr, '# This is a generated file. Do not edit.'; |
|
|
push @cfgarr, "# $sshdverstr sshd configuration file for curl testing"; |
|
|
push @cfgarr, '#'; |
|
|
|
|
|
|
|
|
|
|
|
if ($sshdid =~ /OpenSSH-Windows/) { |
|
|
my $username_lc = lc $username; |
|
|
push @cfgarr, "AllowUsers " . $username_lc =~ s/ /\?/gr; |
|
|
if (exists $ENV{USERDOMAIN}) { |
|
|
my $userdomain_lc = lc $ENV{USERDOMAIN}; |
|
|
$username_lc = "$userdomain_lc\\$username_lc"; |
|
|
$username_lc =~ s/ /\?/g; |
|
|
push @cfgarr, "AllowUsers " . $username_lc =~ s/ /\?/gr; |
|
|
} |
|
|
} else { |
|
|
push @cfgarr, "AllowUsers $username"; |
|
|
} |
|
|
|
|
|
push @cfgarr, "AuthorizedKeysFile $clipubkeyf_config"; |
|
|
if(!($sshdid =~ /OpenSSH/) || ($sshdvernum <= 730)) { |
|
|
push @cfgarr, "AuthorizedKeysFile2 $clipubkeyf_config"; |
|
|
} |
|
|
push @cfgarr, "HostKey $hstprvkeyf_config"; |
|
|
if ($sshdid !~ /OpenSSH-Windows/) { |
|
|
push @cfgarr, "PidFile $pidfile_config"; |
|
|
push @cfgarr, '#'; |
|
|
} |
|
|
if(($sshdid =~ /OpenSSH/) && ($sshdvernum >= 880)) { |
|
|
push @cfgarr, 'HostKeyAlgorithms +ssh-rsa'; |
|
|
push @cfgarr, 'PubkeyAcceptedKeyTypes +ssh-rsa'; |
|
|
} |
|
|
push @cfgarr, '#'; |
|
|
push @cfgarr, "Port $port"; |
|
|
push @cfgarr, "ListenAddress $listenaddr"; |
|
|
push @cfgarr, 'Protocol 2'; |
|
|
push @cfgarr, '#'; |
|
|
push @cfgarr, 'AllowTcpForwarding yes'; |
|
|
push @cfgarr, 'Banner none'; |
|
|
push @cfgarr, 'ChallengeResponseAuthentication no'; |
|
|
push @cfgarr, 'ClientAliveCountMax 3'; |
|
|
push @cfgarr, 'ClientAliveInterval 0'; |
|
|
push @cfgarr, 'GatewayPorts no'; |
|
|
push @cfgarr, 'HostbasedAuthentication no'; |
|
|
push @cfgarr, 'HostbasedUsesNameFromPacketOnly no'; |
|
|
push @cfgarr, 'IgnoreRhosts yes'; |
|
|
push @cfgarr, 'IgnoreUserKnownHosts yes'; |
|
|
push @cfgarr, 'LoginGraceTime 30'; |
|
|
push @cfgarr, "LogLevel $loglevel"; |
|
|
push @cfgarr, 'MaxStartups 5'; |
|
|
push @cfgarr, 'PasswordAuthentication no'; |
|
|
push @cfgarr, 'PermitEmptyPasswords no'; |
|
|
push @cfgarr, 'PermitRootLogin no'; |
|
|
push @cfgarr, 'PrintLastLog no'; |
|
|
push @cfgarr, 'PrintMotd no'; |
|
|
push @cfgarr, 'PubkeyAuthentication yes'; |
|
|
push @cfgarr, 'StrictModes no'; |
|
|
push @cfgarr, "Subsystem sftp \"$sftpsrv_config\""; |
|
|
push @cfgarr, 'SyslogFacility AUTH'; |
|
|
if(!($sshdid =~ /OpenSSH/) || ($sshdvernum <= 730)) { |
|
|
push @cfgarr, 'KeyRegenerationInterval 0'; |
|
|
push @cfgarr, 'RhostsRSAAuthentication no'; |
|
|
push @cfgarr, 'RSAAuthentication no'; |
|
|
push @cfgarr, 'ServerKeyBits 768'; |
|
|
push @cfgarr, 'UseLogin no'; |
|
|
} |
|
|
push @cfgarr, 'X11Forwarding no'; |
|
|
push @cfgarr, '#'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$error = dump_array($sshdconfig, @cfgarr); |
|
|
if($error) { |
|
|
logmsg "$error\n"; |
|
|
exit 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub sshd_supports_opt { |
|
|
my ($option, $value) = @_; |
|
|
my $err; |
|
|
|
|
|
if((($sshdid =~ /OpenSSH/) && ($sshdvernum >= 310)) || |
|
|
($sshdid =~ /SunSSH/)) { |
|
|
|
|
|
$err = grep /((Unsupported)|(Bad configuration)|(Deprecated)) option.*$option/, |
|
|
`\"$sshd\" -t -f $sshdconfig_abs -o \"$option=$value\" 2>&1`; |
|
|
return !$err; |
|
|
} |
|
|
if(($sshdid =~ /OpenSSH/) && ($sshdvernum >= 299)) { |
|
|
|
|
|
$err = dump_array($sshdconfig, (@cfgarr, "$option $value")); |
|
|
if($err) { |
|
|
logmsg "$err\n"; |
|
|
return 0; |
|
|
} |
|
|
$err = grep /((Unsupported)|(Bad configuration)|(Deprecated)) option.*$option/, |
|
|
`\"$sshd\" -t -f $sshdconfig_abs 2>&1`; |
|
|
unlink $sshdconfig; |
|
|
return !$err; |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(sshd_supports_opt('KerberosAuthentication','no')) { |
|
|
push @cfgarr, 'KerberosAuthentication no'; |
|
|
} |
|
|
if(sshd_supports_opt('KerberosGetAFSToken','no')) { |
|
|
push @cfgarr, 'KerberosGetAFSToken no'; |
|
|
} |
|
|
if(sshd_supports_opt('KerberosOrLocalPasswd','no')) { |
|
|
push @cfgarr, 'KerberosOrLocalPasswd no'; |
|
|
} |
|
|
if(sshd_supports_opt('KerberosTgtPassing','no')) { |
|
|
push @cfgarr, 'KerberosTgtPassing no'; |
|
|
} |
|
|
if(sshd_supports_opt('KerberosTicketCleanup','yes')) { |
|
|
push @cfgarr, 'KerberosTicketCleanup yes'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(sshd_supports_opt('AFSTokenPassing','no')) { |
|
|
push @cfgarr, 'AFSTokenPassing no'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(sshd_supports_opt('SkeyAuthentication','no')) { |
|
|
push @cfgarr, 'SkeyAuthentication no'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $sshd_builtwith_GSSAPI; |
|
|
if(sshd_supports_opt('GSSAPIAuthentication','no')) { |
|
|
push @cfgarr, 'GSSAPIAuthentication no'; |
|
|
$sshd_builtwith_GSSAPI = 1; |
|
|
} |
|
|
if(sshd_supports_opt('GSSAPICleanupCredentials','yes')) { |
|
|
push @cfgarr, 'GSSAPICleanupCredentials yes'; |
|
|
} |
|
|
if(sshd_supports_opt('GSSAPIKeyExchange','no')) { |
|
|
push @cfgarr, 'GSSAPIKeyExchange no'; |
|
|
} |
|
|
if(sshd_supports_opt('GSSAPIStoreDelegatedCredentials','no')) { |
|
|
push @cfgarr, 'GSSAPIStoreDelegatedCredentials no'; |
|
|
} |
|
|
if(sshd_supports_opt('GSSCleanupCreds','yes')) { |
|
|
push @cfgarr, 'GSSCleanupCreds yes'; |
|
|
} |
|
|
if(sshd_supports_opt('GSSUseSessionCredCache','no')) { |
|
|
push @cfgarr, 'GSSUseSessionCredCache no'; |
|
|
} |
|
|
push @cfgarr, '#'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(sshd_supports_opt('AddressFamily','any')) { |
|
|
|
|
|
splice @cfgarr, 11, 0, 'AddressFamily any'; |
|
|
} |
|
|
if(sshd_supports_opt('Compression','no')) { |
|
|
push @cfgarr, 'Compression no'; |
|
|
} |
|
|
if(sshd_supports_opt('KbdInteractiveAuthentication','no')) { |
|
|
push @cfgarr, 'KbdInteractiveAuthentication no'; |
|
|
} |
|
|
if(sshd_supports_opt('KeepAlive','no')) { |
|
|
push @cfgarr, 'KeepAlive no'; |
|
|
} |
|
|
if(sshd_supports_opt('LookupClientHostnames','no')) { |
|
|
push @cfgarr, 'LookupClientHostnames no'; |
|
|
} |
|
|
if(sshd_supports_opt('MaxAuthTries','10')) { |
|
|
push @cfgarr, 'MaxAuthTries 10'; |
|
|
} |
|
|
if(sshd_supports_opt('PAMAuthenticationViaKbdInt','no')) { |
|
|
push @cfgarr, 'PAMAuthenticationViaKbdInt no'; |
|
|
} |
|
|
if(sshd_supports_opt('PermitTunnel','no')) { |
|
|
push @cfgarr, 'PermitTunnel no'; |
|
|
} |
|
|
if(sshd_supports_opt('PermitUserEnvironment','no')) { |
|
|
push @cfgarr, 'PermitUserEnvironment no'; |
|
|
} |
|
|
if(sshd_supports_opt('RhostsAuthentication','no')) { |
|
|
push @cfgarr, 'RhostsAuthentication no'; |
|
|
} |
|
|
if(sshd_supports_opt('TCPKeepAlive','no')) { |
|
|
push @cfgarr, 'TCPKeepAlive no'; |
|
|
} |
|
|
if(sshd_supports_opt('UseDNS','no')) { |
|
|
push @cfgarr, 'UseDNS no'; |
|
|
} |
|
|
if(sshd_supports_opt('UsePAM','no')) { |
|
|
push @cfgarr, 'UsePAM no'; |
|
|
} |
|
|
|
|
|
if($sshdid =~ /OpenSSH/) { |
|
|
|
|
|
if(sshd_supports_opt('UsePrivilegeSeparation','no')) { |
|
|
push @cfgarr, 'UsePrivilegeSeparation no'; |
|
|
} |
|
|
} |
|
|
|
|
|
if(sshd_supports_opt('VerifyReverseMapping','no')) { |
|
|
push @cfgarr, 'VerifyReverseMapping no'; |
|
|
} |
|
|
if(sshd_supports_opt('X11UseLocalhost','yes')) { |
|
|
push @cfgarr, 'X11UseLocalhost yes'; |
|
|
} |
|
|
push @cfgarr, '#'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$error = dump_array($sshdconfig, @cfgarr); |
|
|
if($error) { |
|
|
logmsg "$error\n"; |
|
|
exit 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(system "\"$sshd\" -t -f $sshdconfig_abs > $sshdlog 2>&1") { |
|
|
logmsg "sshd configuration file $sshdconfig failed verification\n"; |
|
|
display_sshdlog(); |
|
|
display_sshdconfig(); |
|
|
exit 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if((! -e pp($knownhosts)) || (! -s pp($knownhosts))) { |
|
|
logmsg "generating ssh client known hosts file...\n" if($verbose); |
|
|
unlink(pp($knownhosts)); |
|
|
if(open(my $rsakeyfile, "<", pp($hstpubkeyf))) { |
|
|
my @rsahostkey = do { local $/ = ' '; <$rsakeyfile> }; |
|
|
if(close($rsakeyfile)) { |
|
|
if(open(my $knownhostsh, ">", pp($knownhosts))) { |
|
|
print $knownhostsh "$listenaddr ssh-rsa $rsahostkey[1]\n"; |
|
|
if(!close($knownhostsh)) { |
|
|
$error = "Error: cannot close file $knownhosts"; |
|
|
} |
|
|
} |
|
|
else { |
|
|
$error = "Error: cannot write file $knownhosts"; |
|
|
} |
|
|
} |
|
|
else { |
|
|
$error = "Error: cannot close file $hstpubkeyf"; |
|
|
} |
|
|
} |
|
|
else { |
|
|
$error = "Error: cannot read file $hstpubkeyf"; |
|
|
} |
|
|
if($error) { |
|
|
logmsg "$error\n"; |
|
|
exit 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $identity_config; |
|
|
my $knownhosts_config; |
|
|
if ($sshdid =~ /OpenSSH-Windows/) { |
|
|
|
|
|
$identity_config = pathhelp::sys_native_abs_path(pp($identity)); |
|
|
$knownhosts_config = pathhelp::sys_native_abs_path(pp($knownhosts)); |
|
|
} |
|
|
elsif (pathhelp::os_is_win()) { |
|
|
|
|
|
$identity_config = pathhelp::build_sys_abs_path(pp($identity)); |
|
|
$knownhosts_config = pathhelp::build_sys_abs_path(pp($knownhosts)); |
|
|
} |
|
|
else { |
|
|
$identity_config = abs_path(pp($identity)); |
|
|
$knownhosts_config = abs_path(pp($knownhosts)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logmsg "generating ssh client config file...\n" if($verbose); |
|
|
@cfgarr = (); |
|
|
push @cfgarr, '# This is a generated file. Do not edit.'; |
|
|
push @cfgarr, "# $sshverstr ssh client configuration file for curl testing"; |
|
|
push @cfgarr, '#'; |
|
|
push @cfgarr, 'Host *'; |
|
|
push @cfgarr, '#'; |
|
|
push @cfgarr, "Port $port"; |
|
|
push @cfgarr, "HostName $listenaddr"; |
|
|
push @cfgarr, "User $username"; |
|
|
push @cfgarr, 'Protocol 2'; |
|
|
push @cfgarr, '#'; |
|
|
|
|
|
|
|
|
if (!($sshdid =~ /OpenSSH-Windows/)) { |
|
|
push @cfgarr, "BindAddress $listenaddr"; |
|
|
} |
|
|
|
|
|
push @cfgarr, '#'; |
|
|
push @cfgarr, "IdentityFile $identity_config"; |
|
|
push @cfgarr, "UserKnownHostsFile $knownhosts_config"; |
|
|
push @cfgarr, '#'; |
|
|
push @cfgarr, 'BatchMode yes'; |
|
|
push @cfgarr, 'ChallengeResponseAuthentication no'; |
|
|
push @cfgarr, 'CheckHostIP no'; |
|
|
push @cfgarr, 'ClearAllForwardings no'; |
|
|
push @cfgarr, 'Compression no'; |
|
|
push @cfgarr, 'ConnectionAttempts 3'; |
|
|
push @cfgarr, 'ForwardAgent no'; |
|
|
push @cfgarr, 'ForwardX11 no'; |
|
|
push @cfgarr, 'GatewayPorts no'; |
|
|
push @cfgarr, 'GlobalKnownHostsFile /dev/null'; |
|
|
push @cfgarr, 'HostbasedAuthentication no'; |
|
|
push @cfgarr, 'KbdInteractiveAuthentication no'; |
|
|
push @cfgarr, "LogLevel $loglevel"; |
|
|
push @cfgarr, 'NumberOfPasswordPrompts 0'; |
|
|
push @cfgarr, 'PasswordAuthentication no'; |
|
|
push @cfgarr, 'PreferredAuthentications publickey'; |
|
|
push @cfgarr, 'PubkeyAuthentication yes'; |
|
|
|
|
|
|
|
|
if (!($sshdid =~ /OpenSSH-Windows/ || pathhelp::os_is_win())) { |
|
|
push @cfgarr, 'RhostsRSAAuthentication no'; |
|
|
push @cfgarr, 'RSAAuthentication no'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
push @cfgarr, 'StrictHostKeyChecking no'; |
|
|
push @cfgarr, 'UsePrivilegedPort no'; |
|
|
push @cfgarr, '#'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(($sshid =~ /OpenSSH/) && ($sshvernum >= 370)) { |
|
|
push @cfgarr, 'AddressFamily any'; |
|
|
} |
|
|
|
|
|
if((($sshid =~ /OpenSSH/) && ($sshvernum >= 370)) || |
|
|
(($sshid =~ /SunSSH/) && ($sshvernum >= 120))) { |
|
|
push @cfgarr, 'ConnectTimeout 30'; |
|
|
} |
|
|
|
|
|
if(($sshid =~ /OpenSSH/) && ($sshvernum >= 390)) { |
|
|
push @cfgarr, 'ControlMaster no'; |
|
|
} |
|
|
|
|
|
if(($sshid =~ /OpenSSH/) && ($sshvernum >= 420)) { |
|
|
push @cfgarr, 'ControlPath none'; |
|
|
} |
|
|
|
|
|
if(($sshid =~ /SunSSH/) && ($sshvernum >= 120)) { |
|
|
push @cfgarr, 'DisableBanner yes'; |
|
|
} |
|
|
|
|
|
if(($sshid =~ /OpenSSH/) && ($sshvernum >= 360)) { |
|
|
push @cfgarr, 'EnableSSHKeysign no'; |
|
|
} |
|
|
|
|
|
if(($sshid =~ /OpenSSH/) && ($sshvernum >= 440)) { |
|
|
push @cfgarr, 'ExitOnForwardFailure yes'; |
|
|
} |
|
|
|
|
|
if((($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) || |
|
|
(($sshid =~ /SunSSH/) && ($sshvernum >= 120))) { |
|
|
push @cfgarr, 'ForwardX11Trusted no'; |
|
|
} |
|
|
|
|
|
if(($sshd_builtwith_GSSAPI) && ($sshdid eq $sshid) && |
|
|
($sshdvernum == $sshvernum)) { |
|
|
push @cfgarr, 'GSSAPIAuthentication no'; |
|
|
push @cfgarr, 'GSSAPIDelegateCredentials no'; |
|
|
if($sshid =~ /SunSSH/) { |
|
|
push @cfgarr, 'GSSAPIKeyExchange no'; |
|
|
} |
|
|
} |
|
|
|
|
|
if((($sshid =~ /OpenSSH/) && ($sshvernum >= 400)) || |
|
|
(($sshid =~ /SunSSH/) && ($sshvernum >= 120))) { |
|
|
push @cfgarr, 'HashKnownHosts no'; |
|
|
} |
|
|
|
|
|
if(($sshid =~ /OpenSSH/) && ($sshvernum >= 390)) { |
|
|
push @cfgarr, 'IdentitiesOnly yes'; |
|
|
} |
|
|
|
|
|
if(($sshid =~ /SunSSH/) && ($sshvernum >= 120)) { |
|
|
push @cfgarr, 'IgnoreIfUnknown no'; |
|
|
} |
|
|
|
|
|
if((($sshid =~ /OpenSSH/) && ($sshvernum < 380)) || |
|
|
($sshid =~ /SunSSH/)) { |
|
|
push @cfgarr, 'KeepAlive no'; |
|
|
} |
|
|
|
|
|
if((($sshid =~ /OpenSSH/) && ($sshvernum >= 300)) || |
|
|
($sshid =~ /SunSSH/)) { |
|
|
push @cfgarr, 'NoHostAuthenticationForLocalhost no'; |
|
|
} |
|
|
|
|
|
if(($sshid =~ /OpenSSH/) && ($sshvernum >= 430)) { |
|
|
push @cfgarr, 'PermitLocalCommand no'; |
|
|
} |
|
|
|
|
|
if((($sshid =~ /OpenSSH/) && ($sshvernum >= 370)) || |
|
|
(($sshid =~ /SunSSH/) && ($sshvernum >= 120))) { |
|
|
push @cfgarr, 'RekeyLimit 1G'; |
|
|
} |
|
|
|
|
|
if((($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) || |
|
|
(($sshid =~ /SunSSH/) && ($sshvernum >= 120))) { |
|
|
push @cfgarr, 'ServerAliveCountMax 3'; |
|
|
push @cfgarr, 'ServerAliveInterval 0'; |
|
|
} |
|
|
|
|
|
if(($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) { |
|
|
push @cfgarr, 'TCPKeepAlive no'; |
|
|
} |
|
|
|
|
|
if(($sshid =~ /OpenSSH/) && ($sshvernum >= 430)) { |
|
|
push @cfgarr, 'Tunnel no'; |
|
|
} |
|
|
|
|
|
if(($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) { |
|
|
push @cfgarr, 'VerifyHostKeyDNS no'; |
|
|
} |
|
|
|
|
|
push @cfgarr, '#'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$error = dump_array($sshconfig, @cfgarr); |
|
|
if($error) { |
|
|
logmsg "$error\n"; |
|
|
exit 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logmsg "generating sftp client config file...\n" if($verbose); |
|
|
splice @cfgarr, 1, 1, "# $sshverstr sftp client configuration file for curl testing"; |
|
|
|
|
|
for(my $i = scalar(@cfgarr) - 1; $i > 0; $i--) { |
|
|
if($cfgarr[$i] =~ /^DynamicForward/) { |
|
|
splice @cfgarr, $i, 1; |
|
|
next; |
|
|
} |
|
|
if($cfgarr[$i] =~ /^ClearAllForwardings/) { |
|
|
splice @cfgarr, $i, 1, "ClearAllForwardings yes"; |
|
|
next; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$error = dump_array($sftpconfig, @cfgarr); |
|
|
if($error) { |
|
|
logmsg "$error\n"; |
|
|
exit 1; |
|
|
} |
|
|
@cfgarr = (); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logmsg "generating sftp client commands file...\n" if($verbose); |
|
|
push @cfgarr, 'pwd'; |
|
|
push @cfgarr, 'quit'; |
|
|
$error = dump_array(pp($sftpcmds), @cfgarr); |
|
|
if($error) { |
|
|
logmsg "$error\n"; |
|
|
exit 1; |
|
|
} |
|
|
@cfgarr = (); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $cmd = "\"$sshd\" -e -D -f $sshdconfig_abs > $sshdlog 2>&1"; |
|
|
logmsg "SCP/SFTP server listening on port $port\n" if($verbose); |
|
|
logmsg "RUN: $cmd\n" if($verbose); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($sshdid =~ /OpenSSH-Windows/) { |
|
|
|
|
|
if(open(my $out, ">", "$pidfile")) { |
|
|
print $out $$ . "\n"; |
|
|
close($out); |
|
|
} |
|
|
|
|
|
|
|
|
$| = 1; |
|
|
|
|
|
|
|
|
|
|
|
exec("exec $cmd") || die "Can't exec() $cmd: $!"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
die "error: exec() has returned"; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $rc = system("exec " . $cmd); |
|
|
if($rc == -1) { |
|
|
logmsg "\"$sshd\" failed with: $!\n"; |
|
|
} |
|
|
elsif($rc & 127) { |
|
|
logmsg sprintf("\"$sshd\" died with signal %d, and %s coredump\n", |
|
|
($rc & 127), ($rc & 128)?'a':'no'); |
|
|
} |
|
|
elsif($verbose && ($rc >> 8)) { |
|
|
logmsg sprintf("\"$sshd\" exited with %d\n", $rc >> 8); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unlink(pp($hstprvkeyf), pp($hstpubkeyf), pp($hstpubmd5f), pp($hstpubsha256f), |
|
|
pp($cliprvkeyf), pp($clipubkeyf), pp($knownhosts), |
|
|
$sshdconfig, $sshconfig, $sftpconfig); |
|
|
|
|
|
exit 0; |
|
|
|