Only show exit status in verbose mode.
[platform/upstream/curl.git] / tests / sshserver.pl
1 #/usr/bin/env perl
2 # $Id$
3 # Start sshd for use in the SCP and SFTP curl test harness tests
4
5 # Options:
6 # -u user
7 # -v
8 # target_port
9
10 use strict;
11 use File::Spec;
12
13 my $verbose=0; # set to 1 for debugging
14
15 my $port = 8999;        # just our default, weird enough
16
17 my $path = `pwd`;
18 chomp $path;
19
20 my $exeext;
21 if ($^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'msys' || $^O eq 'dos' || $^O eq 'os2') {
22     $exeext = '.exe';
23 }
24
25 # Where to look for sftp-server
26 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);
27
28 my $username = $ENV{USER};
29
30 # Find a file somewhere in the given path
31 sub searchpath {
32   my $fn = $_[0] . $exeext;
33   shift;
34   my @path = @_;
35   foreach (@path) {
36         my $file = File::Spec->catfile($_, $fn);
37         if (-e $file) {
38                 return $file;
39         }
40   }
41 }
42
43 # Parse options
44 do {
45     if($ARGV[0] eq "-v") {
46         $verbose=1;
47     }
48     elsif($ARGV[0] eq "-u") {
49         $username=$ARGV[1];
50         shift @ARGV;
51     }
52     elsif($ARGV[0] =~ /^(\d+)$/) {
53         $port = $1;
54     }
55 } while(shift @ARGV);
56
57 my $conffile="curl_sshd_config";        # sshd configuration data
58
59 # Search the PATH for sshd.  sshd insists on being called with an absolute
60 # path for some reason.
61 my $sshd = searchpath("sshd", File::Spec->path());
62 if (!$sshd) {
63         print "sshd is not available\n";
64         exit 1;
65 }
66 if ($verbose) {
67         print STDERR "SSH server found at $sshd\n";
68 }
69
70 my $sftp = searchpath("sftp-server", @sftppath);
71 if (!$sftp) {
72         print "Could not find sftp-server plugin\n";
73         exit 1;
74 }
75 if ($verbose) {
76         print STDERR "SFTP server plugin found at $sftp\n";
77 }
78
79 if (! -e "curl_client_key.pub") {
80         if ($verbose) {
81                 print STDERR "Generating host and client keys...\n";
82         }
83         # Make sure all files are gone so ssh-keygen doesn't complain
84         unlink("curl_host_dsa_key", "curl_client_key","curl_host_dsa_key.pub", "curl_client_key.pub"); 
85         system "ssh-keygen -q -t dsa -f curl_host_dsa_key -C 'curl test server' -N ''" and die "Could not generate key";
86         system "ssh-keygen -q -t dsa -f curl_client_key -C 'curl test client' -N ''" and die "Could not generate key";
87 }
88
89 open(FILE, ">$conffile") || die "Could not write $conffile";
90 print FILE <<EOF
91 # This is a generated file!  Do not edit!
92 # OpenSSH sshd configuration file for curl testing
93 AllowUsers $username
94 DenyUsers
95 DenyGroups
96 AuthorizedKeysFile $path/curl_client_key.pub
97 HostKey $path/curl_host_dsa_key
98 PidFile $path/.ssh.pid
99 Port $port
100 ListenAddress localhost
101 Protocol 2
102 AllowTcpForwarding no
103 GatewayPorts no
104 HostbasedAuthentication no
105 IgnoreRhosts yes
106 IgnoreUserKnownHosts yes
107 KeepAlive no
108 PasswordAuthentication no
109 PermitEmptyPasswords no
110 PermitRootLogin no
111 PrintLastLog no
112 PrintMotd no
113 StrictModes no
114 Subsystem sftp $sftp
115 UseLogin no
116 X11Forwarding no
117 UsePrivilegeSeparation no
118 # Newer OpenSSH options
119 UsePam no
120 UseDNS no
121 ChallengeResponseAuthentication no
122 EOF
123 ;
124 close FILE;
125
126 if (system "$sshd -t -q -f $conffile") {
127         # This is likely due to missing support for UsePam
128         print "$sshd is too old and is not supported\n";
129         unlink $conffile;
130         exit 1;
131 }
132
133 # Start the server
134 my $rc = system "$sshd -e -D -f $conffile > log/ssh.log 2>&1";
135 $rc >>= 8;
136 if($rc && $verbose) {
137     print STDERR "$sshd exited with $rc!\n";
138 }
139
140 unlink $conffile;
141
142 exit $rc;