Modify test harness so that the minimum SSH version required to run
[platform/upstream/curl.git] / tests / sshserver.pl
1 #!/usr/bin/env perl
2 #***************************************************************************
3 #                                  _   _ ____  _
4 #  Project                     ___| | | |  _ \| |
5 #                             / __| | | | |_) | |
6 #                            | (__| |_| |  _ <| |___
7 #                             \___|\___/|_| \_\_____|
8 #
9 # Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
10 #
11 # This software is licensed as described in the file COPYING, which
12 # you should have received as part of this distribution. The terms
13 # are also available at http://curl.haxx.se/docs/copyright.html.
14 #
15 # You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 # copies of the Software, and permit persons to whom the Software is
17 # furnished to do so, under the terms of the COPYING file.
18 #
19 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 # KIND, either express or implied.
21 #
22 # $Id$
23 #***************************************************************************
24
25 # Starts sshd for use in the SCP, SFTP and SOCKS curl test harness tests.
26 # Also creates the ssh configuration files needed for these tests.
27
28 # Options:
29 #
30 # -v
31 # -d
32 # -u user
33 # -l listen address
34 # -p SCP/SFTP server port
35 # -s SOCKS4/5 server port
36
37 use strict;
38 #use warnings;
39 use Cwd;
40
41 #***************************************************************************
42 # Variables and subs imported from sshhelp module
43 #
44 use sshhelp qw(
45     $sshdexe
46     $sshexe
47     $sftpexe
48     $sshkeygenexe
49     $sshdconfig
50     $sshconfig
51     $knownhosts
52     $sshdlog
53     $sshlog
54     $hstprvkeyf
55     $hstpubkeyf
56     $cliprvkeyf
57     $clipubkeyf
58     display_sshdconfig
59     display_sshconfig
60     display_sshdlog
61     display_sshlog
62     dump_array
63     find_sshd
64     find_ssh
65     find_sftp
66     find_sshkeygen
67     logmsg
68     sshversioninfo
69     );
70
71 #***************************************************************************
72
73 my $verbose = 1;              # set to 1 for debugging
74 my $debugprotocol = 0;        # set to 1 for protocol debugging
75 my $port = 8999;              # our default SCP/SFTP server port
76 my $socksport = $port + 1;    # our default SOCKS4/5 server port
77 my $listenaddr = '127.0.0.1'; # default address on which to listen
78 my $path = getcwd();          # current working directory
79 my $username = $ENV{USER};    # default user
80
81 my $error;
82 my @cfgarr;
83
84
85 #***************************************************************************
86 # Parse command line options
87 #
88 while(@ARGV) {
89     if($ARGV[0] eq '-v') {
90         $verbose = 1;
91     }
92     elsif($ARGV[0] eq '-d') {
93         $verbose = 1;
94         $debugprotocol = 1;
95     }
96     elsif($ARGV[0] eq '-u') {
97         $username = $ARGV[1];
98         shift @ARGV;
99     }
100     elsif($ARGV[0] eq '-l') {
101         $listenaddr = $ARGV[1];
102         shift @ARGV;
103     }
104     elsif($ARGV[0] eq '-p') {
105         if($ARGV[1] =~ /^(\d+)$/) {
106             $port = $1;
107         }
108         shift @ARGV;
109     }
110     elsif($ARGV[0] eq '-s') {
111         if($ARGV[1] =~ /^(\d+)$/) {
112             $socksport = $1;
113         }
114         shift @ARGV;
115     }
116     shift @ARGV;
117 };
118
119
120 #***************************************************************************
121 # Logging level for ssh server and client
122 #
123 my $loglevel = $debugprotocol?'DEBUG2':'INFO';
124
125
126 #***************************************************************************
127 # Validate username
128 #
129 if(!$username) {
130     $error = 'Will not run ssh server without a user name';
131 }
132 elsif($username eq 'root') {
133     $error = 'Will not run ssh server as root to mitigate security risks';
134 }
135 if($error) {
136     logmsg $error;
137     exit 1;
138 }
139
140
141 #***************************************************************************
142 # Find out ssh daemon canonical file name
143 #
144 my $sshd = find_sshd();
145 if(!$sshd) {
146     logmsg "cannot find $sshdexe";
147     exit 1;
148 }
149
150
151 #***************************************************************************
152 # Find out ssh daemon version info
153 #
154 my ($sshdid, $sshdvernum, $sshdverstr, $sshderror) = sshversioninfo($sshd);
155 if(!$sshdid) {
156     # Not an OpenSSH or SunSSH ssh daemon
157     logmsg $sshderror if($verbose);
158     logmsg 'SCP, SFTP and SOCKS tests require OpenSSH 2.9.9 or later';
159     exit 1;
160 }
161 logmsg "ssh server found $sshd is $sshdverstr" if($verbose);
162
163
164 #***************************************************************************
165 #  ssh daemon command line options we might use and version support
166 #
167 #  -e:  log stderr           : OpenSSH 2.9.0 and later
168 #  -f:  sshd config file     : OpenSSH 1.2.1 and later
169 #  -D:  no daemon forking    : OpenSSH 2.5.0 and later
170 #  -o:  command-line option  : OpenSSH 3.1.0 and later
171 #  -t:  test config file     : OpenSSH 2.9.9 and later
172 #  -?:  sshd version info    : OpenSSH 1.2.1 and later
173 #
174 #  -e:  log stderr           : SunSSH 1.0.0 and later
175 #  -f:  sshd config file     : SunSSH 1.0.0 and later
176 #  -D:  no daemon forking    : SunSSH 1.0.0 and later
177 #  -o:  command-line option  : SunSSH 1.0.0 and later
178 #  -t:  test config file     : SunSSH 1.0.0 and later
179 #  -?:  sshd version info    : SunSSH 1.0.0 and later
180
181
182 #***************************************************************************
183 # Verify minimum ssh daemon version
184 #
185 if((($sshdid =~ /OpenSSH/) && ($sshdvernum < 299)) ||
186    (($sshdid =~ /SunSSH/)  && ($sshdvernum < 100))) {
187     logmsg 'SCP, SFTP and SOCKS tests require OpenSSH 2.9.9 or later';
188     exit 1;
189 }
190
191
192 #***************************************************************************
193 # Find out sftp server plugin canonical file name
194 #
195 my $sftp = find_sftp();
196 if(!$sftp) {
197     logmsg "cannot find $sftpexe";
198     exit 1;
199 }
200 logmsg "sftp server plugin found $sftp" if($verbose);
201
202
203 #***************************************************************************
204 # Find out ssh keygen canonical file name
205 #
206 my $sshkeygen = find_sshkeygen();
207 if(!$sshkeygen) {
208     logmsg "cannot find $sshkeygenexe";
209     exit 1;
210 }
211 logmsg "ssh keygen found $sshkeygen" if($verbose);
212
213
214 #***************************************************************************
215 # Find out ssh client canonical file name
216 #
217 my $ssh = find_ssh();
218 if(!$ssh) {
219     logmsg "cannot find $sshexe";
220     exit 1;
221 }
222
223
224 #***************************************************************************
225 # Find out ssh client version info
226 #
227 my ($sshid, $sshvernum, $sshverstr, $ssherror) = sshversioninfo($ssh);
228 if(!$sshid) {
229     # Not an OpenSSH or SunSSH ssh client
230     logmsg $ssherror if($verbose);
231     logmsg 'SCP, SFTP and SOCKS tests require OpenSSH 2.9.9 or later';
232     exit 1;
233 }
234 logmsg "ssh client found $ssh is $sshverstr" if($verbose);
235
236
237 #***************************************************************************
238 #  ssh client command line options we might use and version support
239 #
240 #  -D:  dynamic app port forwarding  : OpenSSH 2.9.9 and later
241 #  -F:  ssh config file              : OpenSSH 2.9.9 and later
242 #  -N:  no shell/command             : OpenSSH 2.1.0 and later
243 #  -p:  connection port              : OpenSSH 1.2.1 and later
244 #  -v:  verbose messages             : OpenSSH 1.2.1 and later
245 # -vv:  increase verbosity           : OpenSSH 2.3.0 and later
246 #  -V:  ssh version info             : OpenSSH 1.2.1 and later
247 #
248 #  -D:  dynamic app port forwarding  : SunSSH 1.0.0 and later
249 #  -F:  ssh config file              : SunSSH 1.0.0 and later
250 #  -N:  no shell/command             : SunSSH 1.0.0 and later
251 #  -p:  connection port              : SunSSH 1.0.0 and later
252 #  -v:  verbose messages             : SunSSH 1.0.0 and later
253 # -vv:  increase verbosity           : SunSSH 1.0.0 and later
254 #  -V:  ssh version info             : SunSSH 1.0.0 and later
255
256
257 #***************************************************************************
258 # Verify minimum ssh client version
259 #
260 if((($sshid =~ /OpenSSH/) && ($sshvernum < 299)) ||
261    (($sshid =~ /SunSSH/)  && ($sshvernum < 100))) {
262     logmsg 'SCP, SFTP and SOCKS tests require OpenSSH 2.9.9 or later';
263     exit 1;
264 }
265
266
267 #***************************************************************************
268 #  ssh keygen command line options we actually use and version support
269 #
270 #  -C:  identity comment : OpenSSH 1.2.1 and later
271 #  -f:  key filename     : OpenSSH 1.2.1 and later
272 #  -N:  new passphrase   : OpenSSH 1.2.1 and later
273 #  -q:  quiet keygen     : OpenSSH 1.2.1 and later
274 #  -t:  key type         : OpenSSH 2.5.0 and later
275 #
276 #  -C:  identity comment : SunSSH 1.0.0 and later
277 #  -f:  key filename     : SunSSH 1.0.0 and later
278 #  -N:  new passphrase   : SunSSH 1.0.0 and later
279 #  -q:  quiet keygen     : SunSSH 1.0.0 and later
280 #  -t:  key type         : SunSSH 1.0.0 and later
281
282
283 #***************************************************************************
284 # Generate host and client key files for curl's tests
285 #
286 if((! -e $hstprvkeyf) || (! -e $hstpubkeyf) ||
287    (! -e $cliprvkeyf) || (! -e $clipubkeyf)) {
288     # Make sure all files are gone so ssh-keygen doesn't complain
289     unlink($hstprvkeyf, $hstpubkeyf, $cliprvkeyf, $clipubkeyf);
290     logmsg 'generating host keys...' if($verbose);
291     if(system "$sshkeygen -q -t dsa -f $hstprvkeyf -C 'curl test server' -N ''") {
292         logmsg 'Could not generate host key';
293         exit 1;
294     }
295     logmsg 'generating client keys...' if($verbose);
296     if(system "$sshkeygen -q -t dsa -f $cliprvkeyf -C 'curl test client' -N ''") {
297         logmsg 'Could not generate client key';
298         exit 1;
299     }
300 }
301
302
303 #***************************************************************************
304 #  ssh daemon configuration file options we might use and version support
305 #
306 #  AFSTokenPassing                  : OpenSSH 1.2.1 and later [1]
307 #  AcceptEnv                        : OpenSSH 3.9.0 and later
308 #  AddressFamily                    : OpenSSH 4.0.0 and later
309 #  AllowGroups                      : OpenSSH 1.2.1 and later
310 #  AllowTcpForwarding               : OpenSSH 2.3.0 and later
311 #  AllowUsers                       : OpenSSH 1.2.1 and later
312 #  AuthorizedKeysFile               : OpenSSH 2.9.9 and later
313 #  Banner                           : OpenSSH 2.5.0 and later
314 #  ChallengeResponseAuthentication  : OpenSSH 2.5.0 and later
315 #  Ciphers                          : OpenSSH 2.1.0 and later [3]
316 #  ClientAliveCountMax              : OpenSSH 2.9.0 and later
317 #  ClientAliveInterval              : OpenSSH 2.9.0 and later
318 #  Compression                      : OpenSSH 3.3.0 and later
319 #  DenyGroups                       : OpenSSH 1.2.1 and later
320 #  DenyUsers                        : OpenSSH 1.2.1 and later
321 #  ForceCommand                     : OpenSSH 4.4.0 and later [3]
322 #  GatewayPorts                     : OpenSSH 2.1.0 and later
323 #  GSSAPIAuthentication             : OpenSSH 3.7.0 and later [1]
324 #  GSSAPICleanupCredentials         : OpenSSH 3.8.0 and later [1]
325 #  HostbasedAuthentication          : OpenSSH 2.9.0 and later
326 #  HostbasedUsesNameFromPacketOnly  : OpenSSH 2.9.0 and later
327 #  HostKey                          : OpenSSH 1.2.1 and later
328 #  IgnoreRhosts                     : OpenSSH 1.2.1 and later
329 #  IgnoreUserKnownHosts             : OpenSSH 1.2.1 and later
330 #  KeepAlive                        : OpenSSH 1.2.1 and later
331 #  KerberosAuthentication           : OpenSSH 1.2.1 and later [1]
332 #  KerberosGetAFSToken              : OpenSSH 3.8.0 and later [1]
333 #  KerberosOrLocalPasswd            : OpenSSH 1.2.1 and later [1]
334 #  KerberosTgtPassing               : OpenSSH 1.2.1 and later [1]
335 #  KerberosTicketCleanup            : OpenSSH 1.2.1 and later [1]
336 #  KeyRegenerationInterval          : OpenSSH 1.2.1 and later
337 #  ListenAddress                    : OpenSSH 1.2.1 and later
338 #  LoginGraceTime                   : OpenSSH 1.2.1 and later
339 #  LogLevel                         : OpenSSH 1.2.1 and later
340 #  MACs                             : OpenSSH 2.5.0 and later [3]
341 #  Match                            : OpenSSH 4.4.0 and later [3]
342 #  MaxAuthTries                     : OpenSSH 3.9.0 and later
343 #  MaxStartups                      : OpenSSH 2.2.0 and later
344 #  PasswordAuthentication           : OpenSSH 1.2.1 and later
345 #  PermitEmptyPasswords             : OpenSSH 1.2.1 and later
346 #  PermitOpen                       : OpenSSH 4.4.0 and later [3]
347 #  PermitRootLogin                  : OpenSSH 1.2.1 and later
348 #  PermitTunnel                     : OpenSSH 4.3.0 and later
349 #  PermitUserEnvironment            : OpenSSH 3.5.0 and later
350 #  PidFile                          : OpenSSH 2.1.0 and later
351 #  Port                             : OpenSSH 1.2.1 and later
352 #  PrintLastLog                     : OpenSSH 2.9.0 and later
353 #  PrintMotd                        : OpenSSH 1.2.1 and later
354 #  Protocol                         : OpenSSH 2.1.0 and later
355 #  PubkeyAuthentication             : OpenSSH 2.5.0 and later
356 #  RhostsRSAAuthentication          : OpenSSH 1.2.1 and later
357 #  RSAAuthentication                : OpenSSH 1.2.1 and later
358 #  ServerKeyBits                    : OpenSSH 1.2.1 and later
359 #  SkeyAuthentication               : OpenSSH 1.2.1 and later [1]
360 #  StrictModes                      : OpenSSH 1.2.1 and later
361 #  Subsystem                        : OpenSSH 2.2.0 and later
362 #  SyslogFacility                   : OpenSSH 1.2.1 and later
363 #  TCPKeepAlive                     : OpenSSH 3.8.0 and later
364 #  UseDNS                           : OpenSSH 3.7.0 and later
365 #  UseLogin                         : OpenSSH 1.2.1 and later
366 #  UsePAM                           : OpenSSH 3.7.0 and later [1][2]
367 #  UsePrivilegeSeparation           : OpenSSH 3.2.2 and later
368 #  X11DisplayOffset                 : OpenSSH 1.2.1 and later [3]
369 #  X11Forwarding                    : OpenSSH 1.2.1 and later
370 #  X11UseLocalhost                  : OpenSSH 3.1.0 and later
371 #  XAuthLocation                    : OpenSSH 2.1.1 and later [3]
372 #
373 #  [1] Option only available if activated at compile time
374 #  [2] Option specific for portable versions
375 #  [3] Option not used in our ssh server config file
376
377
378 #***************************************************************************
379 # Initialize sshd config with options actually supported in OpenSSH 2.9.9
380 #
381 logmsg 'generating ssh server config file...' if($verbose);
382 @cfgarr = ();
383 push @cfgarr, '# This is a generated file.  Do not edit.';
384 push @cfgarr, "# $sshdverstr sshd configuration file for curl testing";
385 push @cfgarr, '#';
386 push @cfgarr, "DenyUsers !$username";
387 push @cfgarr, "AllowUsers $username";
388 push @cfgarr, 'DenyGroups';
389 push @cfgarr, 'AllowGroups';
390 push @cfgarr, '#';
391 push @cfgarr, "AuthorizedKeysFile $path/$clipubkeyf";
392 push @cfgarr, "HostKey $path/$hstprvkeyf";
393 push @cfgarr, "PidFile $path/.ssh.pid";
394 push @cfgarr, '#';
395 push @cfgarr, "Port $port";
396 push @cfgarr, "ListenAddress $listenaddr";
397 push @cfgarr, 'Protocol 2';
398 push @cfgarr, '#';
399 push @cfgarr, 'AllowTcpForwarding yes';
400 push @cfgarr, 'Banner none';
401 push @cfgarr, 'ChallengeResponseAuthentication no';
402 push @cfgarr, 'ClientAliveCountMax 3';
403 push @cfgarr, 'ClientAliveInterval 0';
404 push @cfgarr, 'GatewayPorts no';
405 push @cfgarr, 'HostbasedAuthentication no';
406 push @cfgarr, 'HostbasedUsesNameFromPacketOnly no';
407 push @cfgarr, 'IgnoreRhosts yes';
408 push @cfgarr, 'IgnoreUserKnownHosts yes';
409 push @cfgarr, 'KeyRegenerationInterval 0';
410 push @cfgarr, 'LoginGraceTime 30';
411 push @cfgarr, "LogLevel $loglevel";
412 push @cfgarr, 'MaxStartups 5';
413 push @cfgarr, 'PasswordAuthentication no';
414 push @cfgarr, 'PermitEmptyPasswords no';
415 push @cfgarr, 'PermitRootLogin no';
416 push @cfgarr, 'PrintLastLog no';
417 push @cfgarr, 'PrintMotd no';
418 push @cfgarr, 'PubkeyAuthentication yes';
419 push @cfgarr, 'RhostsRSAAuthentication no';
420 push @cfgarr, 'RSAAuthentication no';
421 push @cfgarr, 'ServerKeyBits 768';
422 push @cfgarr, 'StrictModes no';
423 push @cfgarr, "Subsystem sftp $sftp";
424 push @cfgarr, 'SyslogFacility AUTH';
425 push @cfgarr, 'UseLogin no';
426 push @cfgarr, 'X11Forwarding no';
427 push @cfgarr, '#';
428
429
430 #***************************************************************************
431 # Write out initial sshd configuration file for curl's tests
432 #
433 $error = dump_array($sshdconfig, @cfgarr);
434 if($error) {
435     logmsg $error;
436     exit 1;
437 }
438
439
440 #***************************************************************************
441 # Verifies at run time if sshd supports a given configuration file option
442 #
443 sub sshd_supports_opt {
444     my ($option, $value) = @_;
445     my $err;
446     #
447     if((($sshdid =~ /OpenSSH/) && ($sshdvernum >= 310)) ||
448         ($sshdid =~ /SunSSH/)) {
449         # ssh daemon supports command line options -t -f and -o
450         $err = grep /((Unsupported)|(Bad configuration)|(Deprecated)) option.*$option/,
451                     qx($sshd -t -f $sshdconfig -o $option=$value 2>&1);
452         return !$err;
453     }
454     if(($sshdid =~ /OpenSSH/) && ($sshdvernum >= 299)) {
455         # ssh daemon supports command line options -t and -f
456         $err = dump_array($sshdconfig, (@cfgarr, "$option $value"));
457         if($err) {
458             logmsg $err;
459             return 0;
460         }
461         $err = grep /((Unsupported)|(Bad configuration)|(Deprecated)) option.*$option/,
462                     qx($sshd -t -f $sshdconfig 2>&1);
463         unlink $sshdconfig;
464         return !$err;
465     }
466     return 0;
467 }
468
469
470 #***************************************************************************
471 # Kerberos Authentication support may have not been built into sshd
472 #
473 if(sshd_supports_opt('KerberosAuthentication','no')) {
474     push @cfgarr, 'KerberosAuthentication no';
475 }
476 if(sshd_supports_opt('KerberosGetAFSToken','no')) {
477     push @cfgarr, 'KerberosGetAFSToken no';
478 }
479 if(sshd_supports_opt('KerberosOrLocalPasswd','no')) {
480     push @cfgarr, 'KerberosOrLocalPasswd no';
481 }
482 if(sshd_supports_opt('KerberosTgtPassing','no')) {
483     push @cfgarr, 'KerberosTgtPassing no';
484 }
485 if(sshd_supports_opt('KerberosTicketCleanup','yes')) {
486     push @cfgarr, 'KerberosTicketCleanup yes';
487 }
488
489
490 #***************************************************************************
491 # Andrew File System support may have not been built into sshd
492 #
493 if(sshd_supports_opt('AFSTokenPassing','no')) {
494     push @cfgarr, 'AFSTokenPassing no';
495 }
496
497
498 #***************************************************************************
499 # S/Key authentication support may have not been built into sshd
500 #
501 if(sshd_supports_opt('SkeyAuthentication','no')) {
502     push @cfgarr, 'SkeyAuthentication no';
503 }
504
505
506 #***************************************************************************
507 # GSSAPI Authentication support may have not been built into sshd
508 #
509 if(sshd_supports_opt('GSSAPIAuthentication','no')) {
510     push @cfgarr, 'GSSAPIAuthentication no';
511 }
512 if(sshd_supports_opt('GSSAPICleanupCredentials','yes')) {
513     push @cfgarr, 'GSSAPICleanupCredentials yes';
514 }
515 push @cfgarr, '#';
516
517
518 #***************************************************************************
519 # Options that might be supported or not in sshd OpenSSH 2.9.9 and later
520 #
521 if(sshd_supports_opt('AcceptEnv','')) {
522     push @cfgarr, 'AcceptEnv';
523 }
524 if(sshd_supports_opt('AddressFamily','any')) {
525     # Address family must be specified before ListenAddress
526     splice @cfgarr, 13, 0, 'AddressFamily any';
527 }
528 if(sshd_supports_opt('Compression','no')) {
529     push @cfgarr, 'Compression no';
530 }
531 if(sshd_supports_opt('KeepAlive','no')) {
532     push @cfgarr, 'KeepAlive no';
533 }
534 if(sshd_supports_opt('MaxAuthTries','0')) {
535     push @cfgarr, 'MaxAuthTries 0';
536 }
537 if(sshd_supports_opt('PermitTunnel','no')) {
538     push @cfgarr, 'PermitTunnel no';
539 }
540 if(sshd_supports_opt('PermitUserEnvironment','no')) {
541     push @cfgarr, 'PermitUserEnvironment no';
542 }
543 if(sshd_supports_opt('TCPKeepAlive','no')) {
544     push @cfgarr, 'TCPKeepAlive no';
545 }
546 if(sshd_supports_opt('UseDNS','no')) {
547     push @cfgarr, 'UseDNS no';
548 }
549 if(sshd_supports_opt('UsePAM','no')) {
550     push @cfgarr, 'UsePAM no';
551 }
552 if(sshd_supports_opt('UsePrivilegeSeparation','no')) {
553     push @cfgarr, 'UsePrivilegeSeparation no';
554 }
555 if(sshd_supports_opt('X11UseLocalhost','yes')) {
556     push @cfgarr, 'X11UseLocalhost yes';
557 }
558 push @cfgarr, '#';
559
560
561 #***************************************************************************
562 # Write out resulting sshd configuration file for curl's tests
563 #
564 $error = dump_array($sshdconfig, @cfgarr);
565 if($error) {
566     logmsg $error;
567     exit 1;
568 }
569
570
571 #***************************************************************************
572 # Verify that sshd actually supports our generated configuration file
573 #
574 if(system "$sshd -t -f $sshdconfig > $sshdlog 2>&1") {
575     logmsg "sshd configuration file $sshdconfig failed verification";
576     display_sshdlog();
577     display_sshdconfig();
578     exit 1;
579 }
580
581
582 #***************************************************************************
583 # Generate ssh client host key database file for curl's tests
584 #
585 if(! -e $knownhosts) {
586     logmsg 'generating ssh client known hosts file...' if($verbose);
587     if(open(DSAKEYFILE, "<$hstpubkeyf")) {
588         my @dsahostkey = do { local $/ = ' '; <DSAKEYFILE> };
589         if(close(DSAKEYFILE)) {
590             if(open(KNOWNHOSTS, ">$knownhosts")) {
591                 print KNOWNHOSTS "$listenaddr ssh-dss $dsahostkey[1]\n";
592                 if(!close(KNOWNHOSTS)) {
593                     $error = "Error: cannot close file $knownhosts";
594                 }
595             }
596             else {
597                 $error = "Error: cannot write file $knownhosts";
598             }
599         }
600         else {
601             $error = "Error: cannot close file $hstpubkeyf";
602         }
603     }
604     else {
605         $error = "Error: cannot read file $hstpubkeyf";
606     }
607     if($error) {
608         logmsg $error;
609         exit 1;
610     }
611 }
612
613 #***************************************************************************
614 #  ssh client configuration file options we might use and version support
615 #
616 #  AddressFamily                     : OpenSSH 3.7.0 and later
617 #  BatchMode                         : OpenSSH 1.2.1 and later
618 #  BindAddress                       : OpenSSH 2.9.9 and later
619 #  ChallengeResponseAuthentication   : OpenSSH 2.5.0 and later
620 #  CheckHostIP                       : OpenSSH 1.2.1 and later
621 #  Cipher                            : OpenSSH 1.2.1 and later [3]
622 #  Ciphers                           : OpenSSH 2.1.0 and later [3]
623 #  ClearAllForwardings               : OpenSSH 2.9.9 and later
624 #  Compression                       : OpenSSH 1.2.1 and later
625 #  CompressionLevel                  : OpenSSH 1.2.1 and later [3]
626 #  ConnectionAttempts                : OpenSSH 1.2.1 and later
627 #  ConnectTimeout                    : OpenSSH 3.7.0 and later
628 #  ControlMaster                     : OpenSSH 3.9.0 and later
629 #  ControlPath                       : OpenSSH 3.9.0 and later
630 #  DynamicForward                    : OpenSSH 2.9.0 and later
631 #  EnableSSHKeysign                  : OpenSSH 3.6.0 and later
632 #  EscapeChar                        : OpenSSH 1.2.1 and later [3]
633 #  ExitOnForwardFailure              : OpenSSH 4.4.0 and later
634 #  ForwardAgent                      : OpenSSH 1.2.1 and later
635 #  ForwardX11                        : OpenSSH 1.2.1 and later
636 #  ForwardX11Trusted                 : OpenSSH 3.8.0 and later
637 #  GatewayPorts                      : OpenSSH 1.2.1 and later
638 #  GlobalKnownHostsFile              : OpenSSH 1.2.1 and later
639 #  GSSAPIAuthentication              : OpenSSH 3.7.0 and later [1][3]
640 #  GSSAPIDelegateCredentials         : OpenSSH 3.7.0 and later [1][3]
641 #  HashKnownHosts                    : OpenSSH 4.0.0 and later
642 #  Host                              : OpenSSH 1.2.1 and later
643 #  HostbasedAuthentication           : OpenSSH 2.9.0 and later
644 #  HostKeyAlgorithms                 : OpenSSH 2.9.0 and later [3]
645 #  HostKeyAlias                      : OpenSSH 2.5.0 and later [3]
646 #  HostName                          : OpenSSH 1.2.1 and later
647 #  IdentitiesOnly                    : OpenSSH 3.9.0 and later
648 #  IdentityFile                      : OpenSSH 1.2.1 and later
649 #  KeepAlive                         : OpenSSH 1.2.1 and later
650 #  KbdInteractiveAuthentication      : OpenSSH 2.3.0 and later
651 #  KbdInteractiveDevices             : OpenSSH 2.3.0 and later [3]
652 #  LocalCommand                      : OpenSSH 4.3.0 and later
653 #  LocalForward                      : OpenSSH 1.2.1 and later [3]
654 #  LogLevel                          : OpenSSH 1.2.1 and later
655 #  MACs                              : OpenSSH 2.5.0 and later [3]
656 #  NoHostAuthenticationForLocalhost  : OpenSSH 3.0.0 and later
657 #  NumberOfPasswordPrompts           : OpenSSH 1.2.1 and later
658 #  PasswordAuthentication            : OpenSSH 1.2.1 and later
659 #  PermitLocalCommand                : OpenSSH 4.3.0 and later
660 #  Port                              : OpenSSH 1.2.1 and later
661 #  PreferredAuthentications          : OpenSSH 2.5.2 and later
662 #  Protocol                          : OpenSSH 2.1.0 and later
663 #  ProxyCommand                      : OpenSSH 1.2.1 and later [3]
664 #  PubkeyAuthentication              : OpenSSH 2.5.0 and later
665 #  RekeyLimit                        : OpenSSH 3.7.0 and later
666 #  RemoteForward                     : OpenSSH 1.2.1 and later [3]
667 #  RhostsRSAAuthentication           : OpenSSH 1.2.1 and later
668 #  RSAAuthentication                 : OpenSSH 1.2.1 and later
669 #  SendEnv                           : OpenSSH 3.9.0 and later
670 #  ServerAliveCountMax               : OpenSSH 3.8.0 and later
671 #  ServerAliveInterval               : OpenSSH 3.8.0 and later
672 #  SmartcardDevice                   : OpenSSH 2.9.9 and later [1][3]
673 #  StrictHostKeyChecking             : OpenSSH 1.2.1 and later
674 #  TCPKeepAlive                      : OpenSSH 3.8.0 and later
675 #  Tunnel                            : OpenSSH 4.3.0 and later
676 #  TunnelDevice                      : OpenSSH 4.3.0 and later [3]
677 #  UsePAM                            : OpenSSH 3.7.0 and later [1][2][3]
678 #  UsePrivilegedPort                 : OpenSSH 1.2.1 and later
679 #  User                              : OpenSSH 1.2.1 and later
680 #  UserKnownHostsFile                : OpenSSH 1.2.1 and later
681 #  VerifyHostKeyDNS                  : OpenSSH 3.8.0 and later
682 #  XAuthLocation                     : OpenSSH 2.1.1 and later [3]
683 #
684 #  [1] Option only available if activated at compile time
685 #  [2] Option specific for portable versions
686 #  [3] Option not used in our ssh client config file
687
688
689 #***************************************************************************
690 # Initialize ssh config with options actually supported in OpenSSH 2.9.9
691 #
692 logmsg 'generating ssh client config file...' if($verbose);
693 @cfgarr = ();
694 push @cfgarr, '# This is a generated file.  Do not edit.';
695 push @cfgarr, "# $sshverstr ssh client configuration file for curl testing";
696 push @cfgarr, '#';
697 push @cfgarr, 'Host *';
698 push @cfgarr, '#';
699 push @cfgarr, "Port $port";
700 push @cfgarr, "HostName $listenaddr";
701 push @cfgarr, "User $username";
702 push @cfgarr, 'Protocol 2';
703 push @cfgarr, '#';
704 push @cfgarr, "BindAddress $listenaddr";
705 push @cfgarr, "DynamicForward $socksport";
706 push @cfgarr, '#';
707 push @cfgarr, "IdentityFile $path/curl_client_key";
708 push @cfgarr, "UserKnownHostsFile $path/$knownhosts";
709 push @cfgarr, '#';
710 push @cfgarr, 'BatchMode yes';
711 push @cfgarr, 'ChallengeResponseAuthentication no';
712 push @cfgarr, 'CheckHostIP no';
713 push @cfgarr, 'ClearAllForwardings no';
714 push @cfgarr, 'Compression no';
715 push @cfgarr, 'ConnectionAttempts 3';
716 push @cfgarr, 'ForwardAgent no';
717 push @cfgarr, 'ForwardX11 no';
718 push @cfgarr, 'GatewayPorts no';
719 push @cfgarr, 'GlobalKnownHostsFile /dev/null';
720 push @cfgarr, 'HostbasedAuthentication no';
721 push @cfgarr, 'KbdInteractiveAuthentication no';
722 push @cfgarr, "LogLevel $loglevel";
723 push @cfgarr, 'NumberOfPasswordPrompts 0';
724 push @cfgarr, 'PasswordAuthentication no';
725 push @cfgarr, 'PreferredAuthentications publickey';
726 push @cfgarr, 'PubkeyAuthentication yes';
727 push @cfgarr, 'RhostsRSAAuthentication no';
728 push @cfgarr, 'RSAAuthentication no';
729 push @cfgarr, 'StrictHostKeyChecking yes';
730 push @cfgarr, 'UsePrivilegedPort no';
731 push @cfgarr, '#';
732
733
734 #***************************************************************************
735 # Options supported in ssh client newer than OpenSSH 2.9.9
736 #
737
738 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 370)) {
739     push @cfgarr, 'AddressFamily any';
740 }
741
742 if((($sshid =~ /OpenSSH/) && ($sshvernum >= 370)) ||
743     ($sshid =~ /SunSSH/)) {
744     push @cfgarr, 'ConnectTimeout 30';
745 }
746
747 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 390)) {
748     push @cfgarr, 'ControlMaster no';
749     push @cfgarr, 'ControlPath none';
750 }
751
752 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 360)) {
753     push @cfgarr, 'EnableSSHKeysign no';
754 }
755
756 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 440)) {
757     push @cfgarr, 'ExitOnForwardFailure yes';
758 }
759
760 if((($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) ||
761     ($sshid =~ /SunSSH/)) {
762     push @cfgarr, 'ForwardX11Trusted no';
763 }
764
765 if((($sshid =~ /OpenSSH/) && ($sshvernum >= 400)) ||
766     ($sshid =~ /SunSSH/)) {
767     push @cfgarr, 'HashKnownHosts no';
768 }
769
770 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 390)) {
771     push @cfgarr, 'IdentitiesOnly yes';
772 }
773
774 if((($sshid =~ /OpenSSH/) && ($sshvernum < 380)) ||
775     ($sshid =~ /SunSSH/)) {
776     push @cfgarr, 'KeepAlive no';
777 }
778
779 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 430)) {
780     push @cfgarr, 'LocalCommand';
781 }
782
783 if((($sshid =~ /OpenSSH/) && ($sshvernum >= 300)) ||
784     ($sshid =~ /SunSSH/)) {
785     push @cfgarr, 'NoHostAuthenticationForLocalhost no';
786 }
787
788 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 430)) {
789     push @cfgarr, 'PermitLocalCommand no';
790 }
791
792 if((($sshid =~ /OpenSSH/) && ($sshvernum >= 370)) ||
793     ($sshid =~ /SunSSH/)) {
794     push @cfgarr, 'RekeyLimit 1G';
795 }
796
797 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 390)) {
798     push @cfgarr, 'SendEnv';
799 }
800
801 if((($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) ||
802     ($sshid =~ /SunSSH/)) {
803     push @cfgarr, 'ServerAliveCountMax 3';
804     push @cfgarr, 'ServerAliveInterval 0';
805 }
806
807 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) {
808     push @cfgarr, 'TCPKeepAlive no';
809 }
810
811 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 430)) {
812     push @cfgarr, 'Tunnel no';
813 }
814
815 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) {
816     push @cfgarr, 'VerifyHostKeyDNS no';
817 }
818
819 push @cfgarr, '#';
820
821
822 #***************************************************************************
823 # Write out resulting ssh client configuration file for curl's tests
824 #
825 $error = dump_array($sshconfig, @cfgarr);
826 if($error) {
827     logmsg $error;
828     exit 1;
829 }
830 @cfgarr = ();
831
832
833 #***************************************************************************
834 # Start the ssh server daemon without forking it
835 #
836 my $rc = system "$sshd -e -D -f $sshdconfig > $sshdlog 2>&1";
837 if($rc == -1) {
838     logmsg "$sshd failed with: $!";
839 }
840 elsif($rc & 127) {
841     logmsg sprintf("$sshd died with signal %d, and %s coredump",
842                    ($rc & 127), ($rc & 128)?'a':'no');
843 }
844 elsif($verbose && ($rc >> 8)) {
845     logmsg sprintf("$sshd exited with %d", $rc >> 8);
846 }
847
848
849 #***************************************************************************
850 # Clean up once the server has stopped
851 #
852 unlink($hstprvkeyf, $hstpubkeyf, $cliprvkeyf, $clipubkeyf, $knownhosts);
853 unlink($sshdconfig, $sshconfig);
854
855
856 exit 0;