2007-03-24 04:01:28 +03:00
|
|
|
#/usr/bin/env perl
|
|
|
|
# $Id$
|
2007-06-08 01:42:33 +04:00
|
|
|
# Starts sshd for use in the SCP, SFTP and SOCKS curl test harness tests.
|
|
|
|
# Also creates the ssh configuration files (this could be moved to a
|
|
|
|
# separate script).
|
2007-03-24 04:01:28 +03:00
|
|
|
|
|
|
|
# Options:
|
|
|
|
# -u user
|
|
|
|
# -v
|
|
|
|
# target_port
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use File::Spec;
|
2007-12-22 21:25:43 +03:00
|
|
|
use Cwd;
|
2007-03-24 04:01:28 +03:00
|
|
|
|
2007-11-27 23:57:22 +03:00
|
|
|
my $verbose=1; # set to 1 for debugging
|
2007-11-26 05:45:24 +03:00
|
|
|
my $showfiles=0;
|
2007-03-24 04:01:28 +03:00
|
|
|
|
|
|
|
my $port = 8999; # just our default, weird enough
|
2007-09-18 01:39:34 +04:00
|
|
|
my $listenaddr = "127.0.0.1"; # address on which to listen
|
2007-03-24 04:01:28 +03:00
|
|
|
|
2007-11-27 23:57:22 +03:00
|
|
|
my $conffile="curl_sshd_config"; # sshd configuration data
|
|
|
|
my $conffile_ssh="curl_ssh_config"; # ssh configuration data
|
|
|
|
my $knownhostsfile="curl_client_knownhosts"; # ssh knownhosts file
|
|
|
|
|
2007-12-22 21:25:43 +03:00
|
|
|
my $path = getcwd();
|
2007-03-24 04:01:28 +03:00
|
|
|
|
|
|
|
my $exeext;
|
|
|
|
if ($^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'msys' || $^O eq 'dos' || $^O eq 'os2') {
|
|
|
|
$exeext = '.exe';
|
|
|
|
}
|
|
|
|
|
|
|
|
# Where to look for sftp-server
|
2007-11-17 05:28:54 +03:00
|
|
|
my @sftppath = qw(
|
|
|
|
/usr/lib/openssh
|
|
|
|
/usr/libexec/openssh
|
|
|
|
/usr/libexec
|
|
|
|
/usr/local/libexec
|
|
|
|
/opt/local/libexec
|
|
|
|
/usr/lib/ssh
|
|
|
|
/usr/libexec/ssh
|
|
|
|
/usr/sbin
|
|
|
|
/usr/lib
|
|
|
|
/usr/lib/ssh/openssh
|
|
|
|
/usr/lib64/ssh
|
|
|
|
/usr/lib64/misc
|
|
|
|
/usr/lib/misc
|
|
|
|
/usr/local/sbin
|
|
|
|
/usr/freeware/bin
|
2007-11-18 04:16:44 +03:00
|
|
|
/opt/ssh/sbin
|
|
|
|
/opt/ssh/libexec
|
2007-11-17 05:28:54 +03:00
|
|
|
);
|
2007-03-24 04:01:28 +03:00
|
|
|
|
|
|
|
my $username = $ENV{USER};
|
|
|
|
|
|
|
|
# Find a file somewhere in the given path
|
|
|
|
sub searchpath {
|
|
|
|
my $fn = $_[0] . $exeext;
|
|
|
|
shift;
|
|
|
|
my @path = @_;
|
|
|
|
foreach (@path) {
|
2007-03-30 14:11:49 +04:00
|
|
|
my $file = File::Spec->catfile($_, $fn);
|
|
|
|
if (-e $file) {
|
|
|
|
return $file;
|
|
|
|
}
|
2007-03-24 04:01:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-20 17:10:09 +03:00
|
|
|
# Display contents of the given file.
|
|
|
|
sub displayfile {
|
|
|
|
my ($file) = @_;
|
|
|
|
print "=== Start of file $file\n";
|
|
|
|
if(open(SINGLE, "<$file")) {
|
|
|
|
while(my $string = <SINGLE>) {
|
|
|
|
print "$string";
|
|
|
|
}
|
|
|
|
close(SINGLE);
|
|
|
|
}
|
|
|
|
print "=== End of file $file\n";
|
|
|
|
}
|
|
|
|
|
2007-11-27 23:57:22 +03:00
|
|
|
# Append a string to sshd config file
|
|
|
|
sub set_sshd_option {
|
|
|
|
my ($string) = @_;
|
|
|
|
if (open(FILE, ">>$conffile")) {
|
|
|
|
print FILE "$string\n";
|
|
|
|
close FILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Append a string to ssh config file
|
|
|
|
sub set_ssh_option {
|
|
|
|
my ($string) = @_;
|
|
|
|
if (open(FILE, ">>$conffile_ssh")) {
|
|
|
|
print FILE "$string\n";
|
|
|
|
close FILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-24 04:01:28 +03:00
|
|
|
# Parse options
|
|
|
|
do {
|
|
|
|
if($ARGV[0] eq "-v") {
|
|
|
|
$verbose=1;
|
|
|
|
}
|
|
|
|
elsif($ARGV[0] eq "-u") {
|
|
|
|
$username=$ARGV[1];
|
|
|
|
shift @ARGV;
|
|
|
|
}
|
2007-09-18 01:39:34 +04:00
|
|
|
elsif($ARGV[0] eq "-l") {
|
|
|
|
$listenaddr=$ARGV[1];
|
|
|
|
shift @ARGV;
|
|
|
|
}
|
2007-03-24 04:01:28 +03:00
|
|
|
elsif($ARGV[0] =~ /^(\d+)$/) {
|
|
|
|
$port = $1;
|
|
|
|
}
|
|
|
|
} while(shift @ARGV);
|
|
|
|
|
2007-03-30 14:11:49 +04:00
|
|
|
# Searching for sshd and sftp-server will be done first
|
|
|
|
# in the PATH and afterwards in other common locations.
|
|
|
|
my @spath;
|
|
|
|
push(@spath, File::Spec->path());
|
|
|
|
push(@spath, @sftppath);
|
|
|
|
|
|
|
|
# sshd insists on being called with an absolute path.
|
|
|
|
my $sshd = searchpath("sshd", @spath);
|
2007-03-24 04:01:28 +03:00
|
|
|
if (!$sshd) {
|
2007-03-30 14:11:49 +04:00
|
|
|
print "sshd$exeext not found\n";
|
|
|
|
exit 1;
|
2007-03-24 04:01:28 +03:00
|
|
|
}
|
|
|
|
if ($verbose) {
|
2007-11-28 04:46:28 +03:00
|
|
|
print "SSH server found is $sshd\n";
|
2007-03-24 04:01:28 +03:00
|
|
|
}
|
|
|
|
|
2007-03-30 14:11:49 +04:00
|
|
|
my $sftp = searchpath("sftp-server", @spath);
|
2007-03-24 04:01:28 +03:00
|
|
|
if (!$sftp) {
|
2007-03-30 14:11:49 +04:00
|
|
|
print "Could not find sftp-server$exeext plugin\n";
|
|
|
|
exit 1;
|
2007-03-24 04:01:28 +03:00
|
|
|
}
|
|
|
|
if ($verbose) {
|
2007-11-28 04:46:28 +03:00
|
|
|
print "SFTP server plugin found is $sftp\n";
|
2007-03-24 04:01:28 +03:00
|
|
|
}
|
|
|
|
|
2007-03-29 09:25:11 +04:00
|
|
|
if ($username eq "root") {
|
2007-03-30 14:11:49 +04:00
|
|
|
print "Will not run ssh daemon as root to mitigate security risks\n";
|
|
|
|
exit 1;
|
2007-03-29 09:25:11 +04:00
|
|
|
}
|
|
|
|
|
2007-04-02 05:21:57 +04:00
|
|
|
# Find out sshd version.
|
|
|
|
my $tmpstr;
|
|
|
|
my $ssh_daemon;
|
|
|
|
my $ssh_ver_major;
|
|
|
|
my $ssh_ver_minor;
|
|
|
|
my $ssh_ver_patch;
|
2007-11-27 23:57:22 +03:00
|
|
|
my $ssh_version;
|
|
|
|
foreach $tmpstr (qx($sshd -V 2>&1)) {
|
|
|
|
if($tmpstr =~ /OpenSSH[_-](\d+)\.(\d+)(\.(\d+))*/i) {
|
|
|
|
($ssh_ver_major, $ssh_ver_minor, $ssh_ver_patch) = ($1, $2, $4);
|
|
|
|
$ssh_daemon = 'OpenSSH';
|
|
|
|
$ssh_version = 10 * $ssh_ver_major + $ssh_ver_minor;
|
|
|
|
if($ssh_version == 36) {
|
|
|
|
$showfiles=1;
|
|
|
|
}
|
|
|
|
last;
|
2007-11-26 05:45:24 +03:00
|
|
|
}
|
2007-11-27 23:57:22 +03:00
|
|
|
if($tmpstr =~ /Sun[_-]SSH[_-](\d+)\.(\d+)/i) {
|
2007-11-19 20:20:32 +03:00
|
|
|
($ssh_ver_major, $ssh_ver_minor) = ($1, $2);
|
|
|
|
$ssh_daemon = 'SunSSH';
|
2007-11-27 23:57:22 +03:00
|
|
|
$ssh_version = 10 * $ssh_ver_major + $ssh_ver_minor;
|
|
|
|
if($ssh_version == 11) {
|
2007-11-26 05:45:24 +03:00
|
|
|
$showfiles=1;
|
|
|
|
}
|
2007-11-27 23:57:22 +03:00
|
|
|
last;
|
2007-11-19 20:20:32 +03:00
|
|
|
}
|
|
|
|
}
|
2007-04-02 05:21:57 +04:00
|
|
|
|
2007-11-19 20:20:32 +03:00
|
|
|
# Verify minimum SSH daemon version.
|
|
|
|
my $sshd_ver_ok = 1;
|
2007-11-27 23:57:22 +03:00
|
|
|
if(!$ssh_daemon) {
|
|
|
|
if($verbose) {
|
2007-11-28 04:46:28 +03:00
|
|
|
print "unsupported SSH server daemon found\n";
|
2007-11-27 23:57:22 +03:00
|
|
|
chomp($tmpstr = qx($sshd -V 2>&1));
|
2007-11-28 04:46:28 +03:00
|
|
|
print "$tmpstr\n";
|
2007-11-27 23:57:22 +03:00
|
|
|
}
|
2007-11-19 20:20:32 +03:00
|
|
|
$sshd_ver_ok = 0;
|
|
|
|
}
|
2007-11-27 23:57:22 +03:00
|
|
|
elsif(($ssh_daemon =~ /OpenSSH/) && ($ssh_version < 36)) {
|
|
|
|
if($verbose) {
|
2007-11-28 04:46:28 +03:00
|
|
|
print "sshd found is $ssh_daemon $ssh_ver_major.$ssh_ver_minor\n";
|
2007-11-27 23:57:22 +03:00
|
|
|
}
|
2007-11-19 20:20:32 +03:00
|
|
|
$sshd_ver_ok = 0;
|
|
|
|
}
|
2007-11-27 23:57:22 +03:00
|
|
|
elsif(($ssh_daemon =~ /SunSSH/) && ($ssh_version < 11)) {
|
|
|
|
if($verbose) {
|
2007-11-28 04:46:28 +03:00
|
|
|
print "sshd found is $ssh_daemon $ssh_ver_major.$ssh_ver_minor\n";
|
2007-11-27 23:57:22 +03:00
|
|
|
}
|
2007-11-19 20:20:32 +03:00
|
|
|
$sshd_ver_ok = 0;
|
|
|
|
}
|
|
|
|
if(!$sshd_ver_ok) {
|
2007-11-18 04:16:44 +03:00
|
|
|
print "SCP, SFTP and SOCKS tests require OpenSSH 3.7 or later\n";
|
2007-04-02 05:21:57 +04:00
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
2007-04-03 06:36:55 +04:00
|
|
|
# Initialize sshd configuration file for curl's tests.
|
2007-06-08 01:42:33 +04:00
|
|
|
open(CONF, ">$conffile") || die "Could not write $conffile";
|
|
|
|
print CONF "# This is a generated file! Do not edit!\n";
|
2007-11-28 04:46:28 +03:00
|
|
|
print CONF "# $ssh_daemon $ssh_ver_major.$ssh_ver_minor sshd configuration file for curl testing\n";
|
2007-06-08 01:42:33 +04:00
|
|
|
close CONF;
|
2007-04-03 06:36:55 +04:00
|
|
|
|
2007-03-31 07:21:08 +04:00
|
|
|
# Support for some options might have not been built into sshd. On some
|
|
|
|
# platforms specifying an unsupported option prevents sshd from starting.
|
|
|
|
# Check here for possible unsupported options, avoiding its use in sshd.
|
|
|
|
sub sshd_supports_opt($) {
|
|
|
|
my ($option) = @_;
|
2007-11-20 17:10:09 +03:00
|
|
|
my $err = grep /((Unsupported)|(Bad configuration)|(Deprecated)) option.*$option/,
|
|
|
|
qx($sshd -t -f $conffile -o $option=no 2>&1);
|
2007-03-31 07:21:08 +04:00
|
|
|
return !$err;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $supports_UsePAM = sshd_supports_opt('UsePAM');
|
|
|
|
my $supports_UseDNS = sshd_supports_opt('UseDNS');
|
|
|
|
my $supports_ChReAu = sshd_supports_opt('ChallengeResponseAuthentication');
|
|
|
|
|
2007-03-24 04:01:28 +03:00
|
|
|
if (! -e "curl_client_key.pub") {
|
2007-03-30 14:11:49 +04:00
|
|
|
if ($verbose) {
|
2007-11-28 04:46:28 +03:00
|
|
|
print "Generating host and client keys...\n";
|
2007-03-30 14:11:49 +04:00
|
|
|
}
|
|
|
|
# Make sure all files are gone so ssh-keygen doesn't complain
|
|
|
|
unlink("curl_host_dsa_key", "curl_client_key","curl_host_dsa_key.pub", "curl_client_key.pub");
|
2007-07-19 05:42:22 +04:00
|
|
|
system "ssh-keygen -q -t dsa -f curl_host_dsa_key -C 'curl test server' -N ''" and die "Could not generate host key";
|
|
|
|
system "ssh-keygen -q -t dsa -f curl_client_key -C 'curl test client' -N ''" and die "Could not generate client key";
|
2007-03-24 04:01:28 +03:00
|
|
|
}
|
2007-06-08 01:42:33 +04:00
|
|
|
|
2007-06-08 21:32:24 +04:00
|
|
|
open(FILE, ">>$conffile") || die "Could not write $conffile";
|
2007-06-08 01:42:33 +04:00
|
|
|
print FILE <<EOFSSHD
|
2007-03-24 04:01:28 +03:00
|
|
|
AllowUsers $username
|
2007-03-27 08:01:39 +04:00
|
|
|
DenyUsers
|
|
|
|
DenyGroups
|
2007-03-24 04:01:28 +03:00
|
|
|
AuthorizedKeysFile $path/curl_client_key.pub
|
|
|
|
HostKey $path/curl_host_dsa_key
|
|
|
|
PidFile $path/.ssh.pid
|
|
|
|
Port $port
|
2007-09-18 01:39:34 +04:00
|
|
|
ListenAddress $listenaddr
|
2007-03-24 04:01:28 +03:00
|
|
|
Protocol 2
|
2007-06-05 17:50:59 +04:00
|
|
|
AllowTcpForwarding yes
|
2007-03-27 08:01:39 +04:00
|
|
|
GatewayPorts no
|
2007-03-24 04:01:28 +03:00
|
|
|
HostbasedAuthentication no
|
|
|
|
IgnoreRhosts yes
|
|
|
|
IgnoreUserKnownHosts yes
|
|
|
|
KeepAlive no
|
|
|
|
PasswordAuthentication no
|
|
|
|
PermitEmptyPasswords no
|
2007-03-29 22:46:09 +04:00
|
|
|
PermitUserEnvironment no
|
2007-03-24 04:01:28 +03:00
|
|
|
PermitRootLogin no
|
|
|
|
PrintLastLog no
|
|
|
|
PrintMotd no
|
|
|
|
StrictModes no
|
|
|
|
Subsystem sftp $sftp
|
|
|
|
UseLogin no
|
2007-06-05 17:50:59 +04:00
|
|
|
PrintLastLog no
|
2007-03-24 04:01:28 +03:00
|
|
|
X11Forwarding no
|
2007-03-27 08:01:39 +04:00
|
|
|
UsePrivilegeSeparation no
|
2007-03-24 04:01:28 +03:00
|
|
|
# Newer OpenSSH options
|
2007-06-05 17:50:59 +04:00
|
|
|
EOFSSHD
|
2007-03-24 04:01:28 +03:00
|
|
|
;
|
2007-06-08 01:42:33 +04:00
|
|
|
close FILE || die "Could not close $conffile";
|
2007-03-31 07:21:08 +04:00
|
|
|
|
|
|
|
if ($supports_UsePAM) {
|
|
|
|
set_sshd_option('UsePAM no');
|
|
|
|
}
|
|
|
|
if ($supports_UseDNS) {
|
|
|
|
set_sshd_option('UseDNS no');
|
|
|
|
}
|
|
|
|
if ($supports_ChReAu) {
|
|
|
|
set_sshd_option('ChallengeResponseAuthentication no');
|
|
|
|
}
|
2007-03-24 04:01:28 +03:00
|
|
|
|
2007-06-08 01:42:33 +04:00
|
|
|
|
|
|
|
# Now, set up some configuration files for the ssh client
|
2007-06-08 21:32:24 +04:00
|
|
|
open(DSAKEYFILE, "<curl_host_dsa_key.pub") || die 'Could not read curl_host_dsa_key.pub';
|
2007-06-08 01:42:33 +04:00
|
|
|
my @dsahostkey = do { local $/ = ' '; <DSAKEYFILE> };
|
2007-06-11 21:49:25 +04:00
|
|
|
close DSAKEYFILE || die "Could not close DSAKEYFILE";
|
2007-06-08 01:42:33 +04:00
|
|
|
|
2007-06-08 21:32:24 +04:00
|
|
|
open(KNOWNHOSTS, ">$knownhostsfile") || die "Could not write $knownhostsfile";
|
2007-09-18 01:39:34 +04:00
|
|
|
print KNOWNHOSTS "[$listenaddr]:$port ssh-dss $dsahostkey[1]\n" || die 'Could not write to KNOWNHOSTS';
|
2007-06-08 01:42:33 +04:00
|
|
|
close KNOWNHOSTS || die "Could not close KNOWNHOSTS";
|
|
|
|
|
2007-06-08 21:32:24 +04:00
|
|
|
open(SSHFILE, ">$conffile_ssh") || die "Could not write $conffile_ssh";
|
2007-06-08 01:42:33 +04:00
|
|
|
print SSHFILE <<EOFSSH
|
|
|
|
IdentityFile $path/curl_client_key
|
|
|
|
UserKnownHostsFile $path/$knownhostsfile
|
|
|
|
StrictHostKeyChecking no
|
|
|
|
Protocol 2
|
|
|
|
BatchMode yes
|
|
|
|
CheckHostIP no
|
|
|
|
Compression no
|
|
|
|
ForwardX11 no
|
2007-11-27 03:52:30 +03:00
|
|
|
GatewayPorts no
|
2007-06-08 01:42:33 +04:00
|
|
|
HostbasedAuthentication yes
|
|
|
|
NoHostAuthenticationForLocalhost no
|
|
|
|
# Newer OpenSSH options
|
|
|
|
#SetupTimeOut 20
|
|
|
|
EOFSSH
|
|
|
|
;
|
|
|
|
close SSHFILE || die "Could not close $conffile_ssh";
|
|
|
|
|
2007-11-27 23:57:22 +03:00
|
|
|
if(($ssh_daemon =~ /OpenSSH/) && ($ssh_version >= 37)) {
|
|
|
|
set_ssh_option('ConnectTimeout 20'); # Supported in OpenSSH 3.7 and later
|
|
|
|
}
|
|
|
|
|
2007-06-08 01:42:33 +04:00
|
|
|
|
2007-11-20 17:10:09 +03:00
|
|
|
# Verify that sshd supports our configuration file
|
|
|
|
if (system "$sshd -t -f $conffile > log/sshd.log 2>&1") {
|
2007-11-28 04:46:28 +03:00
|
|
|
print "sshd configuration file failed verification\n";
|
2007-11-20 17:10:09 +03:00
|
|
|
displayfile("log/sshd.log");
|
|
|
|
displayfile("$conffile");
|
|
|
|
unlink "log/sshd.log";
|
2007-03-30 14:11:49 +04:00
|
|
|
unlink $conffile;
|
|
|
|
exit 1;
|
2007-03-24 04:01:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Start the server
|
2007-11-28 04:46:28 +03:00
|
|
|
my $rc = system "$sshd -e -D -f $conffile > log/sshd.log 2>&1";
|
|
|
|
if($rc == -1) {
|
|
|
|
print "$sshd failed with: $!\n";
|
|
|
|
$showfiles=1;
|
2007-03-24 04:01:28 +03:00
|
|
|
}
|
2007-11-28 04:46:28 +03:00
|
|
|
elsif($rc & 127) {
|
2007-11-20 19:47:56 +03:00
|
|
|
printf("$sshd died with signal %d, and %s coredump.\n",
|
2007-11-28 04:46:28 +03:00
|
|
|
($rc & 127), ($rc & 128)?"a":"no");
|
|
|
|
$showfiles=1;
|
2007-11-20 17:10:09 +03:00
|
|
|
}
|
2007-11-28 04:46:28 +03:00
|
|
|
elsif($verbose && ($rc >> 8)) {
|
|
|
|
printf("$sshd exited with %d \n", $rc >> 8);
|
2007-11-20 19:47:56 +03:00
|
|
|
}
|
2007-03-24 04:01:28 +03:00
|
|
|
|
2007-11-26 05:45:24 +03:00
|
|
|
if($showfiles) {
|
|
|
|
displayfile("log/sshd.log");
|
|
|
|
displayfile("$conffile");
|
|
|
|
}
|
|
|
|
|
2007-11-28 04:46:28 +03:00
|
|
|
unlink "log/sshd.log";
|
2007-03-24 04:01:28 +03:00
|
|
|
unlink $conffile;
|
|
|
|
|
2007-11-28 04:46:28 +03:00
|
|
|
exit $rc >> 8;
|