2 #***************************************************************************
4 # Project ___| | | | _ \| |
6 # | (__| |_| | _ <| |___
7 # \___|\___/|_| \_\_____|
9 # Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
19 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 # KIND, either express or implied.
22 #***************************************************************************
24 # Starts sshd for use in the SCP, SFTP and SOCKS curl test harness tests.
25 # Also creates the ssh configuration files needed for these tests.
31 #***************************************************************************
32 # Variables and subs imported from sshhelp module
68 #***************************************************************************
69 # Subs imported from serverhelp module
77 #***************************************************************************
79 my $verbose = 0; # set to 1 for debugging
80 my $debugprotocol = 0; # set to 1 for protocol debugging
81 my $port = 8999; # our default SCP/SFTP server port
82 my $socksport = $port + 1; # our default SOCKS4/5 server port
83 my $listenaddr = '127.0.0.1'; # default address on which to listen
84 my $ipvnum = 4; # default IP version of listener address
85 my $idnum = 1; # dafault ssh daemon instance number
86 my $proto = 'ssh'; # protocol the ssh daemon speaks
87 my $path = getcwd(); # current working directory
88 my $logdir = $path .'/log'; # directory for log files
89 my $username = $ENV{USER}; # default user
90 my $pidfile; # ssh daemon pid file
96 #***************************************************************************
97 # Parse command line options
100 if($ARGV[0] eq '--verbose') {
103 elsif($ARGV[0] eq '--debugprotocol') {
107 elsif($ARGV[0] eq '--user') {
109 $username = $ARGV[1];
113 elsif($ARGV[0] eq '--id') {
115 if($ARGV[1] =~ /^(\d+)$/) {
116 $idnum = $1 if($1 > 0);
121 elsif($ARGV[0] eq '--ipv4') {
123 $listenaddr = '127.0.0.1' if($listenaddr eq '::1');
125 elsif($ARGV[0] eq '--ipv6') {
127 $listenaddr = '::1' if($listenaddr eq '127.0.0.1');
129 elsif($ARGV[0] eq '--addr') {
131 my $tmpstr = $ARGV[1];
132 if($tmpstr =~ /^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/) {
133 $listenaddr = "$1.$2.$3.$4" if($ipvnum == 4);
136 elsif($ipvnum == 6) {
137 $listenaddr = $tmpstr;
138 $listenaddr =~ s/^\[(.*)\]$/$1/;
143 elsif($ARGV[0] eq '--pidfile') {
145 $pidfile = "$path/". $ARGV[1];
149 elsif($ARGV[0] eq '--sshport') {
151 if($ARGV[1] =~ /^(\d+)$/) {
157 elsif($ARGV[0] eq '--socksport') {
159 if($ARGV[1] =~ /^(\d+)$/) {
166 print STDERR "\nWarning: sshserver.pl unknown parameter: $ARGV[0]\n";
172 #***************************************************************************
173 # Default ssh daemon pid file name
176 $pidfile = "$path/". server_pidfilename($proto, $ipvnum, $idnum);
180 #***************************************************************************
181 # ssh, socks and sftp server log file names
183 $sshdlog = server_logfilename($logdir, 'ssh', $ipvnum, $idnum);
184 $sftplog = server_logfilename($logdir, 'sftp', $ipvnum, $idnum);
185 $sshlog = server_logfilename($logdir, 'socks', $ipvnum, $idnum);
188 #***************************************************************************
189 # Logging level for ssh server and client
191 my $loglevel = $debugprotocol?'DEBUG3':'DEBUG2';
194 #***************************************************************************
198 $error = 'Will not run ssh server without a user name';
200 elsif($username eq 'root') {
201 $error = 'Will not run ssh server as root to mitigate security risks';
209 #***************************************************************************
210 # Find out ssh daemon canonical file name
212 my $sshd = find_sshd();
214 logmsg "cannot find $sshdexe";
219 #***************************************************************************
220 # Find out ssh daemon version info
222 my ($sshdid, $sshdvernum, $sshdverstr, $sshderror) = sshversioninfo($sshd);
224 # Not an OpenSSH or SunSSH ssh daemon
225 logmsg $sshderror if($verbose);
226 logmsg 'SCP, SFTP and SOCKS tests require OpenSSH 2.9.9 or later';
229 logmsg "ssh server found $sshd is $sshdverstr" if($verbose);
232 #***************************************************************************
233 # ssh daemon command line options we might use and version support
235 # -e: log stderr : OpenSSH 2.9.0 and later
236 # -f: sshd config file : OpenSSH 1.2.1 and later
237 # -D: no daemon forking : OpenSSH 2.5.0 and later
238 # -o: command-line option : OpenSSH 3.1.0 and later
239 # -t: test config file : OpenSSH 2.9.9 and later
240 # -?: sshd version info : OpenSSH 1.2.1 and later
242 # -e: log stderr : SunSSH 1.0.0 and later
243 # -f: sshd config file : SunSSH 1.0.0 and later
244 # -D: no daemon forking : SunSSH 1.0.0 and later
245 # -o: command-line option : SunSSH 1.0.0 and later
246 # -t: test config file : SunSSH 1.0.0 and later
247 # -?: sshd version info : SunSSH 1.0.0 and later
250 #***************************************************************************
251 # Verify minimum ssh daemon version
253 if((($sshdid =~ /OpenSSH/) && ($sshdvernum < 299)) ||
254 (($sshdid =~ /SunSSH/) && ($sshdvernum < 100))) {
255 logmsg 'SCP, SFTP and SOCKS tests require OpenSSH 2.9.9 or later';
260 #***************************************************************************
261 # Find out sftp server plugin canonical file name
263 my $sftpsrv = find_sftpsrv();
265 logmsg "cannot find $sftpsrvexe";
268 logmsg "sftp server plugin found $sftpsrv" if($verbose);
271 #***************************************************************************
272 # Find out sftp client canonical file name
274 my $sftp = find_sftp();
276 logmsg "cannot find $sftpexe";
279 logmsg "sftp client found $sftp" if($verbose);
282 #***************************************************************************
283 # Find out ssh keygen canonical file name
285 my $sshkeygen = find_sshkeygen();
287 logmsg "cannot find $sshkeygenexe";
290 logmsg "ssh keygen found $sshkeygen" if($verbose);
293 #***************************************************************************
294 # Find out ssh client canonical file name
296 my $ssh = find_ssh();
298 logmsg "cannot find $sshexe";
303 #***************************************************************************
304 # Find out ssh client version info
306 my ($sshid, $sshvernum, $sshverstr, $ssherror) = sshversioninfo($ssh);
308 # Not an OpenSSH or SunSSH ssh client
309 logmsg $ssherror if($verbose);
310 logmsg 'SCP, SFTP and SOCKS tests require OpenSSH 2.9.9 or later';
313 logmsg "ssh client found $ssh is $sshverstr" if($verbose);
316 #***************************************************************************
317 # ssh client command line options we might use and version support
319 # -D: dynamic app port forwarding : OpenSSH 2.9.9 and later
320 # -F: ssh config file : OpenSSH 2.9.9 and later
321 # -N: no shell/command : OpenSSH 2.1.0 and later
322 # -p: connection port : OpenSSH 1.2.1 and later
323 # -v: verbose messages : OpenSSH 1.2.1 and later
324 # -vv: increase verbosity : OpenSSH 2.3.0 and later
325 # -V: ssh version info : OpenSSH 1.2.1 and later
327 # -D: dynamic app port forwarding : SunSSH 1.0.0 and later
328 # -F: ssh config file : SunSSH 1.0.0 and later
329 # -N: no shell/command : SunSSH 1.0.0 and later
330 # -p: connection port : SunSSH 1.0.0 and later
331 # -v: verbose messages : SunSSH 1.0.0 and later
332 # -vv: increase verbosity : SunSSH 1.0.0 and later
333 # -V: ssh version info : SunSSH 1.0.0 and later
336 #***************************************************************************
337 # Verify minimum ssh client version
339 if((($sshid =~ /OpenSSH/) && ($sshvernum < 299)) ||
340 (($sshid =~ /SunSSH/) && ($sshvernum < 100))) {
341 logmsg 'SCP, SFTP and SOCKS tests require OpenSSH 2.9.9 or later';
346 #***************************************************************************
347 # ssh keygen command line options we actually use and version support
349 # -C: identity comment : OpenSSH 1.2.1 and later
350 # -f: key filename : OpenSSH 1.2.1 and later
351 # -N: new passphrase : OpenSSH 1.2.1 and later
352 # -q: quiet keygen : OpenSSH 1.2.1 and later
353 # -t: key type : OpenSSH 2.5.0 and later
355 # -C: identity comment : SunSSH 1.0.0 and later
356 # -f: key filename : SunSSH 1.0.0 and later
357 # -N: new passphrase : SunSSH 1.0.0 and later
358 # -q: quiet keygen : SunSSH 1.0.0 and later
359 # -t: key type : SunSSH 1.0.0 and later
362 #***************************************************************************
363 # Generate host and client key files for curl's tests
365 if((! -e $hstprvkeyf) || (! -e $hstpubkeyf) ||
366 (! -e $cliprvkeyf) || (! -e $clipubkeyf)) {
367 # Make sure all files are gone so ssh-keygen doesn't complain
368 unlink($hstprvkeyf, $hstpubkeyf, $cliprvkeyf, $clipubkeyf);
369 logmsg 'generating host keys...' if($verbose);
370 if(system "$sshkeygen -q -t dsa -f $hstprvkeyf -C 'curl test server' -N ''") {
371 logmsg 'Could not generate host key';
374 logmsg 'generating client keys...' if($verbose);
375 if(system "$sshkeygen -q -t dsa -f $cliprvkeyf -C 'curl test client' -N ''") {
376 logmsg 'Could not generate client key';
382 #***************************************************************************
383 # ssh daemon configuration file options we might use and version support
385 # AFSTokenPassing : OpenSSH 1.2.1 and later [1]
386 # AcceptEnv : OpenSSH 3.9.0 and later
387 # AddressFamily : OpenSSH 4.0.0 and later
388 # AllowGroups : OpenSSH 1.2.1 and later
389 # AllowTcpForwarding : OpenSSH 2.3.0 and later
390 # AllowUsers : OpenSSH 1.2.1 and later
391 # AuthorizedKeysFile : OpenSSH 2.9.9 and later
392 # AuthorizedKeysFile2 : OpenSSH 2.9.9 and later
393 # Banner : OpenSSH 2.5.0 and later
394 # ChallengeResponseAuthentication : OpenSSH 2.5.0 and later
395 # Ciphers : OpenSSH 2.1.0 and later [3]
396 # ClientAliveCountMax : OpenSSH 2.9.0 and later
397 # ClientAliveInterval : OpenSSH 2.9.0 and later
398 # Compression : OpenSSH 3.3.0 and later
399 # DenyGroups : OpenSSH 1.2.1 and later
400 # DenyUsers : OpenSSH 1.2.1 and later
401 # ForceCommand : OpenSSH 4.4.0 and later [3]
402 # GatewayPorts : OpenSSH 2.1.0 and later
403 # GSSAPIAuthentication : OpenSSH 3.7.0 and later [1]
404 # GSSAPICleanupCredentials : OpenSSH 3.8.0 and later [1]
405 # GSSAPIKeyExchange : SunSSH 1.0.0 and later [1]
406 # GSSAPIStoreDelegatedCredentials : SunSSH 1.0.0 and later [1]
407 # GSSCleanupCreds : SunSSH 1.0.0 and later [1]
408 # GSSUseSessionCredCache : SunSSH 1.0.0 and later [1]
409 # HostbasedAuthentication : OpenSSH 2.9.0 and later
410 # HostbasedUsesNameFromPacketOnly : OpenSSH 2.9.0 and later
411 # HostKey : OpenSSH 1.2.1 and later
412 # IgnoreRhosts : OpenSSH 1.2.1 and later
413 # IgnoreUserKnownHosts : OpenSSH 1.2.1 and later
414 # KbdInteractiveAuthentication : OpenSSH 2.3.0 and later
415 # KeepAlive : OpenSSH 1.2.1 and later
416 # KerberosAuthentication : OpenSSH 1.2.1 and later [1]
417 # KerberosGetAFSToken : OpenSSH 3.8.0 and later [1]
418 # KerberosOrLocalPasswd : OpenSSH 1.2.1 and later [1]
419 # KerberosTgtPassing : OpenSSH 1.2.1 and later [1]
420 # KerberosTicketCleanup : OpenSSH 1.2.1 and later [1]
421 # KeyRegenerationInterval : OpenSSH 1.2.1 and later
422 # ListenAddress : OpenSSH 1.2.1 and later
423 # LoginGraceTime : OpenSSH 1.2.1 and later
424 # LogLevel : OpenSSH 1.2.1 and later
425 # LookupClientHostnames : SunSSH 1.0.0 and later
426 # MACs : OpenSSH 2.5.0 and later [3]
427 # Match : OpenSSH 4.4.0 and later [3]
428 # MaxAuthTries : OpenSSH 3.9.0 and later
429 # MaxStartups : OpenSSH 2.2.0 and later
430 # PAMAuthenticationViaKbdInt : OpenSSH 2.9.0 and later [2]
431 # PasswordAuthentication : OpenSSH 1.2.1 and later
432 # PermitEmptyPasswords : OpenSSH 1.2.1 and later
433 # PermitOpen : OpenSSH 4.4.0 and later [3]
434 # PermitRootLogin : OpenSSH 1.2.1 and later
435 # PermitTunnel : OpenSSH 4.3.0 and later
436 # PermitUserEnvironment : OpenSSH 3.5.0 and later
437 # PidFile : OpenSSH 2.1.0 and later
438 # Port : OpenSSH 1.2.1 and later
439 # PrintLastLog : OpenSSH 2.9.0 and later
440 # PrintMotd : OpenSSH 1.2.1 and later
441 # Protocol : OpenSSH 2.1.0 and later
442 # PubkeyAuthentication : OpenSSH 2.5.0 and later
443 # RhostsAuthentication : OpenSSH 1.2.1 and later
444 # RhostsRSAAuthentication : OpenSSH 1.2.1 and later
445 # RSAAuthentication : OpenSSH 1.2.1 and later
446 # ServerKeyBits : OpenSSH 1.2.1 and later
447 # SkeyAuthentication : OpenSSH 1.2.1 and later [1]
448 # StrictModes : OpenSSH 1.2.1 and later
449 # Subsystem : OpenSSH 2.2.0 and later
450 # SyslogFacility : OpenSSH 1.2.1 and later
451 # TCPKeepAlive : OpenSSH 3.8.0 and later
452 # UseDNS : OpenSSH 3.7.0 and later
453 # UseLogin : OpenSSH 1.2.1 and later
454 # UsePAM : OpenSSH 3.7.0 and later [1][2]
455 # UsePrivilegeSeparation : OpenSSH 3.2.2 and later
456 # VerifyReverseMapping : OpenSSH 3.1.0 and later
457 # X11DisplayOffset : OpenSSH 1.2.1 and later [3]
458 # X11Forwarding : OpenSSH 1.2.1 and later
459 # X11UseLocalhost : OpenSSH 3.1.0 and later
460 # XAuthLocation : OpenSSH 2.1.1 and later [3]
462 # [1] Option only available if activated at compile time
463 # [2] Option specific for portable versions
464 # [3] Option not used in our ssh server config file
467 #***************************************************************************
468 # Initialize sshd config with options actually supported in OpenSSH 2.9.9
470 logmsg 'generating ssh server config file...' if($verbose);
472 push @cfgarr, '# This is a generated file. Do not edit.';
473 push @cfgarr, "# $sshdverstr sshd configuration file for curl testing";
475 push @cfgarr, "DenyUsers !$username";
476 push @cfgarr, "AllowUsers $username";
477 push @cfgarr, 'DenyGroups';
478 push @cfgarr, 'AllowGroups';
480 push @cfgarr, "AuthorizedKeysFile $path/$clipubkeyf";
481 push @cfgarr, "AuthorizedKeysFile2 $path/$clipubkeyf";
482 push @cfgarr, "HostKey $path/$hstprvkeyf";
483 push @cfgarr, "PidFile $pidfile";
485 push @cfgarr, "Port $port";
486 push @cfgarr, "ListenAddress $listenaddr";
487 push @cfgarr, 'Protocol 2';
489 push @cfgarr, 'AllowTcpForwarding yes';
490 push @cfgarr, 'Banner none';
491 push @cfgarr, 'ChallengeResponseAuthentication no';
492 push @cfgarr, 'ClientAliveCountMax 3';
493 push @cfgarr, 'ClientAliveInterval 0';
494 push @cfgarr, 'GatewayPorts no';
495 push @cfgarr, 'HostbasedAuthentication no';
496 push @cfgarr, 'HostbasedUsesNameFromPacketOnly no';
497 push @cfgarr, 'IgnoreRhosts yes';
498 push @cfgarr, 'IgnoreUserKnownHosts yes';
499 push @cfgarr, 'KeyRegenerationInterval 0';
500 push @cfgarr, 'LoginGraceTime 30';
501 push @cfgarr, "LogLevel $loglevel";
502 push @cfgarr, 'MaxStartups 5';
503 push @cfgarr, 'PasswordAuthentication no';
504 push @cfgarr, 'PermitEmptyPasswords no';
505 push @cfgarr, 'PermitRootLogin no';
506 push @cfgarr, 'PrintLastLog no';
507 push @cfgarr, 'PrintMotd no';
508 push @cfgarr, 'PubkeyAuthentication yes';
509 push @cfgarr, 'RhostsRSAAuthentication no';
510 push @cfgarr, 'RSAAuthentication no';
511 push @cfgarr, 'ServerKeyBits 768';
512 push @cfgarr, 'StrictModes no';
513 push @cfgarr, "Subsystem sftp $sftpsrv";
514 push @cfgarr, 'SyslogFacility AUTH';
515 push @cfgarr, 'UseLogin no';
516 push @cfgarr, 'X11Forwarding no';
520 #***************************************************************************
521 # Write out initial sshd configuration file for curl's tests
523 $error = dump_array($sshdconfig, @cfgarr);
530 #***************************************************************************
531 # Verifies at run time if sshd supports a given configuration file option
533 sub sshd_supports_opt {
534 my ($option, $value) = @_;
537 if((($sshdid =~ /OpenSSH/) && ($sshdvernum >= 310)) ||
538 ($sshdid =~ /SunSSH/)) {
539 # ssh daemon supports command line options -t -f and -o
540 $err = grep /((Unsupported)|(Bad configuration)|(Deprecated)) option.*$option/,
541 qx($sshd -t -f $sshdconfig -o $option=$value 2>&1);
544 if(($sshdid =~ /OpenSSH/) && ($sshdvernum >= 299)) {
545 # ssh daemon supports command line options -t and -f
546 $err = dump_array($sshdconfig, (@cfgarr, "$option $value"));
551 $err = grep /((Unsupported)|(Bad configuration)|(Deprecated)) option.*$option/,
552 qx($sshd -t -f $sshdconfig 2>&1);
560 #***************************************************************************
561 # Kerberos Authentication support may have not been built into sshd
563 if(sshd_supports_opt('KerberosAuthentication','no')) {
564 push @cfgarr, 'KerberosAuthentication no';
566 if(sshd_supports_opt('KerberosGetAFSToken','no')) {
567 push @cfgarr, 'KerberosGetAFSToken no';
569 if(sshd_supports_opt('KerberosOrLocalPasswd','no')) {
570 push @cfgarr, 'KerberosOrLocalPasswd no';
572 if(sshd_supports_opt('KerberosTgtPassing','no')) {
573 push @cfgarr, 'KerberosTgtPassing no';
575 if(sshd_supports_opt('KerberosTicketCleanup','yes')) {
576 push @cfgarr, 'KerberosTicketCleanup yes';
580 #***************************************************************************
581 # Andrew File System support may have not been built into sshd
583 if(sshd_supports_opt('AFSTokenPassing','no')) {
584 push @cfgarr, 'AFSTokenPassing no';
588 #***************************************************************************
589 # S/Key authentication support may have not been built into sshd
591 if(sshd_supports_opt('SkeyAuthentication','no')) {
592 push @cfgarr, 'SkeyAuthentication no';
596 #***************************************************************************
597 # GSSAPI Authentication support may have not been built into sshd
599 my $sshd_builtwith_GSSAPI;
600 if(sshd_supports_opt('GSSAPIAuthentication','no')) {
601 push @cfgarr, 'GSSAPIAuthentication no';
602 $sshd_builtwith_GSSAPI = 1;
604 if(sshd_supports_opt('GSSAPICleanupCredentials','yes')) {
605 push @cfgarr, 'GSSAPICleanupCredentials yes';
607 if(sshd_supports_opt('GSSAPIKeyExchange','no')) {
608 push @cfgarr, 'GSSAPIKeyExchange no';
610 if(sshd_supports_opt('GSSAPIStoreDelegatedCredentials','no')) {
611 push @cfgarr, 'GSSAPIStoreDelegatedCredentials no';
613 if(sshd_supports_opt('GSSCleanupCreds','yes')) {
614 push @cfgarr, 'GSSCleanupCreds yes';
616 if(sshd_supports_opt('GSSUseSessionCredCache','no')) {
617 push @cfgarr, 'GSSUseSessionCredCache no';
622 #***************************************************************************
623 # Options that might be supported or not in sshd OpenSSH 2.9.9 and later
625 if(sshd_supports_opt('AcceptEnv','')) {
626 push @cfgarr, 'AcceptEnv';
628 if(sshd_supports_opt('AddressFamily','any')) {
629 # Address family must be specified before ListenAddress
630 splice @cfgarr, 14, 0, 'AddressFamily any';
632 if(sshd_supports_opt('Compression','no')) {
633 push @cfgarr, 'Compression no';
635 if(sshd_supports_opt('KbdInteractiveAuthentication','no')) {
636 push @cfgarr, 'KbdInteractiveAuthentication no';
638 if(sshd_supports_opt('KeepAlive','no')) {
639 push @cfgarr, 'KeepAlive no';
641 if(sshd_supports_opt('LookupClientHostnames','no')) {
642 push @cfgarr, 'LookupClientHostnames no';
644 if(sshd_supports_opt('MaxAuthTries','10')) {
645 push @cfgarr, 'MaxAuthTries 10';
647 if(sshd_supports_opt('PAMAuthenticationViaKbdInt','no')) {
648 push @cfgarr, 'PAMAuthenticationViaKbdInt no';
650 if(sshd_supports_opt('PermitTunnel','no')) {
651 push @cfgarr, 'PermitTunnel no';
653 if(sshd_supports_opt('PermitUserEnvironment','no')) {
654 push @cfgarr, 'PermitUserEnvironment no';
656 if(sshd_supports_opt('RhostsAuthentication','no')) {
657 push @cfgarr, 'RhostsAuthentication no';
659 if(sshd_supports_opt('TCPKeepAlive','no')) {
660 push @cfgarr, 'TCPKeepAlive no';
662 if(sshd_supports_opt('UseDNS','no')) {
663 push @cfgarr, 'UseDNS no';
665 if(sshd_supports_opt('UsePAM','no')) {
666 push @cfgarr, 'UsePAM no';
669 if($sshdid =~ /OpenSSH/) {
670 # http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=6492415
671 if(sshd_supports_opt('UsePrivilegeSeparation','no')) {
672 push @cfgarr, 'UsePrivilegeSeparation no';
676 if(sshd_supports_opt('VerifyReverseMapping','no')) {
677 push @cfgarr, 'VerifyReverseMapping no';
679 if(sshd_supports_opt('X11UseLocalhost','yes')) {
680 push @cfgarr, 'X11UseLocalhost yes';
685 #***************************************************************************
686 # Write out resulting sshd configuration file for curl's tests
688 $error = dump_array($sshdconfig, @cfgarr);
695 #***************************************************************************
696 # Verify that sshd actually supports our generated configuration file
698 if(system "$sshd -t -f $sshdconfig > $sshdlog 2>&1") {
699 logmsg "sshd configuration file $sshdconfig failed verification";
701 display_sshdconfig();
706 #***************************************************************************
707 # Generate ssh client host key database file for curl's tests
709 if(! -e $knownhosts) {
710 logmsg 'generating ssh client known hosts file...' if($verbose);
711 if(open(DSAKEYFILE, "<$hstpubkeyf")) {
712 my @dsahostkey = do { local $/ = ' '; <DSAKEYFILE> };
713 if(close(DSAKEYFILE)) {
714 if(open(KNOWNHOSTS, ">$knownhosts")) {
715 print KNOWNHOSTS "$listenaddr ssh-dss $dsahostkey[1]\n";
716 if(!close(KNOWNHOSTS)) {
717 $error = "Error: cannot close file $knownhosts";
721 $error = "Error: cannot write file $knownhosts";
725 $error = "Error: cannot close file $hstpubkeyf";
729 $error = "Error: cannot read file $hstpubkeyf";
738 #***************************************************************************
739 # ssh client configuration file options we might use and version support
741 # AddressFamily : OpenSSH 3.7.0 and later
742 # BatchMode : OpenSSH 1.2.1 and later
743 # BindAddress : OpenSSH 2.9.9 and later
744 # ChallengeResponseAuthentication : OpenSSH 2.5.0 and later
745 # CheckHostIP : OpenSSH 1.2.1 and later
746 # Cipher : OpenSSH 1.2.1 and later [3]
747 # Ciphers : OpenSSH 2.1.0 and later [3]
748 # ClearAllForwardings : OpenSSH 2.9.9 and later
749 # Compression : OpenSSH 1.2.1 and later
750 # CompressionLevel : OpenSSH 1.2.1 and later [3]
751 # ConnectionAttempts : OpenSSH 1.2.1 and later
752 # ConnectTimeout : OpenSSH 3.7.0 and later
753 # ControlMaster : OpenSSH 3.9.0 and later
754 # ControlPath : OpenSSH 3.9.0 and later
755 # DisableBanner : SunSSH 1.2.0 and later
756 # DynamicForward : OpenSSH 2.9.0 and later
757 # EnableSSHKeysign : OpenSSH 3.6.0 and later
758 # EscapeChar : OpenSSH 1.2.1 and later [3]
759 # ExitOnForwardFailure : OpenSSH 4.4.0 and later
760 # ForwardAgent : OpenSSH 1.2.1 and later
761 # ForwardX11 : OpenSSH 1.2.1 and later
762 # ForwardX11Trusted : OpenSSH 3.8.0 and later
763 # GatewayPorts : OpenSSH 1.2.1 and later
764 # GlobalKnownHostsFile : OpenSSH 1.2.1 and later
765 # GSSAPIAuthentication : OpenSSH 3.7.0 and later [1]
766 # GSSAPIDelegateCredentials : OpenSSH 3.7.0 and later [1]
767 # HashKnownHosts : OpenSSH 4.0.0 and later
768 # Host : OpenSSH 1.2.1 and later
769 # HostbasedAuthentication : OpenSSH 2.9.0 and later
770 # HostKeyAlgorithms : OpenSSH 2.9.0 and later [3]
771 # HostKeyAlias : OpenSSH 2.5.0 and later [3]
772 # HostName : OpenSSH 1.2.1 and later
773 # IdentitiesOnly : OpenSSH 3.9.0 and later
774 # IdentityFile : OpenSSH 1.2.1 and later
775 # IgnoreIfUnknown : SunSSH 1.2.0 and later
776 # KeepAlive : OpenSSH 1.2.1 and later
777 # KbdInteractiveAuthentication : OpenSSH 2.3.0 and later
778 # KbdInteractiveDevices : OpenSSH 2.3.0 and later [3]
779 # LocalCommand : OpenSSH 4.3.0 and later [3]
780 # LocalForward : OpenSSH 1.2.1 and later [3]
781 # LogLevel : OpenSSH 1.2.1 and later
782 # MACs : OpenSSH 2.5.0 and later [3]
783 # NoHostAuthenticationForLocalhost : OpenSSH 3.0.0 and later
784 # NumberOfPasswordPrompts : OpenSSH 1.2.1 and later
785 # PasswordAuthentication : OpenSSH 1.2.1 and later
786 # PermitLocalCommand : OpenSSH 4.3.0 and later
787 # Port : OpenSSH 1.2.1 and later
788 # PreferredAuthentications : OpenSSH 2.5.2 and later
789 # Protocol : OpenSSH 2.1.0 and later
790 # ProxyCommand : OpenSSH 1.2.1 and later [3]
791 # PubkeyAuthentication : OpenSSH 2.5.0 and later
792 # RekeyLimit : OpenSSH 3.7.0 and later
793 # RemoteForward : OpenSSH 1.2.1 and later [3]
794 # RhostsRSAAuthentication : OpenSSH 1.2.1 and later
795 # RSAAuthentication : OpenSSH 1.2.1 and later
796 # SendEnv : OpenSSH 3.9.0 and later
797 # ServerAliveCountMax : OpenSSH 3.8.0 and later
798 # ServerAliveInterval : OpenSSH 3.8.0 and later
799 # SmartcardDevice : OpenSSH 2.9.9 and later [1][3]
800 # StrictHostKeyChecking : OpenSSH 1.2.1 and later
801 # TCPKeepAlive : OpenSSH 3.8.0 and later
802 # Tunnel : OpenSSH 4.3.0 and later
803 # TunnelDevice : OpenSSH 4.3.0 and later [3]
804 # UsePAM : OpenSSH 3.7.0 and later [1][2][3]
805 # UsePrivilegedPort : OpenSSH 1.2.1 and later
806 # User : OpenSSH 1.2.1 and later
807 # UserKnownHostsFile : OpenSSH 1.2.1 and later
808 # VerifyHostKeyDNS : OpenSSH 3.8.0 and later
809 # XAuthLocation : OpenSSH 2.1.1 and later [3]
811 # [1] Option only available if activated at compile time
812 # [2] Option specific for portable versions
813 # [3] Option not used in our ssh client config file
816 #***************************************************************************
817 # Initialize ssh config with options actually supported in OpenSSH 2.9.9
819 logmsg 'generating ssh client config file...' if($verbose);
821 push @cfgarr, '# This is a generated file. Do not edit.';
822 push @cfgarr, "# $sshverstr ssh client configuration file for curl testing";
824 push @cfgarr, 'Host *';
826 push @cfgarr, "Port $port";
827 push @cfgarr, "HostName $listenaddr";
828 push @cfgarr, "User $username";
829 push @cfgarr, 'Protocol 2';
831 push @cfgarr, "BindAddress $listenaddr";
832 push @cfgarr, "DynamicForward $socksport";
834 push @cfgarr, "IdentityFile $path/curl_client_key";
835 push @cfgarr, "UserKnownHostsFile $path/$knownhosts";
837 push @cfgarr, 'BatchMode yes';
838 push @cfgarr, 'ChallengeResponseAuthentication no';
839 push @cfgarr, 'CheckHostIP no';
840 push @cfgarr, 'ClearAllForwardings no';
841 push @cfgarr, 'Compression no';
842 push @cfgarr, 'ConnectionAttempts 3';
843 push @cfgarr, 'ForwardAgent no';
844 push @cfgarr, 'ForwardX11 no';
845 push @cfgarr, 'GatewayPorts no';
846 push @cfgarr, 'GlobalKnownHostsFile /dev/null';
847 push @cfgarr, 'HostbasedAuthentication no';
848 push @cfgarr, 'KbdInteractiveAuthentication no';
849 push @cfgarr, "LogLevel $loglevel";
850 push @cfgarr, 'NumberOfPasswordPrompts 0';
851 push @cfgarr, 'PasswordAuthentication no';
852 push @cfgarr, 'PreferredAuthentications publickey';
853 push @cfgarr, 'PubkeyAuthentication yes';
854 push @cfgarr, 'RhostsRSAAuthentication no';
855 push @cfgarr, 'RSAAuthentication no';
856 push @cfgarr, 'StrictHostKeyChecking yes';
857 push @cfgarr, 'UsePrivilegedPort no';
861 #***************************************************************************
862 # Options supported in ssh client newer than OpenSSH 2.9.9
865 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 370)) {
866 push @cfgarr, 'AddressFamily any';
869 if((($sshid =~ /OpenSSH/) && ($sshvernum >= 370)) ||
870 (($sshid =~ /SunSSH/) && ($sshvernum >= 120))) {
871 push @cfgarr, 'ConnectTimeout 30';
874 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 390)) {
875 push @cfgarr, 'ControlMaster no';
878 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 420)) {
879 push @cfgarr, 'ControlPath none';
882 if(($sshid =~ /SunSSH/) && ($sshvernum >= 120)) {
883 push @cfgarr, 'DisableBanner yes';
886 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 360)) {
887 push @cfgarr, 'EnableSSHKeysign no';
890 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 440)) {
891 push @cfgarr, 'ExitOnForwardFailure yes';
894 if((($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) ||
895 (($sshid =~ /SunSSH/) && ($sshvernum >= 120))) {
896 push @cfgarr, 'ForwardX11Trusted no';
899 if(($sshd_builtwith_GSSAPI) && ($sshdid eq $sshid) &&
900 ($sshdvernum == $sshvernum)) {
901 push @cfgarr, 'GSSAPIAuthentication no';
902 push @cfgarr, 'GSSAPIDelegateCredentials no';
903 if($sshid =~ /SunSSH/) {
904 push @cfgarr, 'GSSAPIKeyExchange no';
908 if((($sshid =~ /OpenSSH/) && ($sshvernum >= 400)) ||
909 (($sshid =~ /SunSSH/) && ($sshvernum >= 120))) {
910 push @cfgarr, 'HashKnownHosts no';
913 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 390)) {
914 push @cfgarr, 'IdentitiesOnly yes';
917 if(($sshid =~ /SunSSH/) && ($sshvernum >= 120)) {
918 push @cfgarr, 'IgnoreIfUnknown no';
921 if((($sshid =~ /OpenSSH/) && ($sshvernum < 380)) ||
922 ($sshid =~ /SunSSH/)) {
923 push @cfgarr, 'KeepAlive no';
926 if((($sshid =~ /OpenSSH/) && ($sshvernum >= 300)) ||
927 ($sshid =~ /SunSSH/)) {
928 push @cfgarr, 'NoHostAuthenticationForLocalhost no';
931 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 430)) {
932 push @cfgarr, 'PermitLocalCommand no';
935 if((($sshid =~ /OpenSSH/) && ($sshvernum >= 370)) ||
936 (($sshid =~ /SunSSH/) && ($sshvernum >= 120))) {
937 push @cfgarr, 'RekeyLimit 1G';
940 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 390)) {
941 push @cfgarr, 'SendEnv';
944 if((($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) ||
945 (($sshid =~ /SunSSH/) && ($sshvernum >= 120))) {
946 push @cfgarr, 'ServerAliveCountMax 3';
947 push @cfgarr, 'ServerAliveInterval 0';
950 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) {
951 push @cfgarr, 'TCPKeepAlive no';
954 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 430)) {
955 push @cfgarr, 'Tunnel no';
958 if(($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) {
959 push @cfgarr, 'VerifyHostKeyDNS no';
965 #***************************************************************************
966 # Write out resulting ssh client configuration file for curl's tests
968 $error = dump_array($sshconfig, @cfgarr);
975 #***************************************************************************
976 # Initialize client sftp config with options actually supported.
978 logmsg 'generating sftp client config file...' if($verbose);
979 splice @cfgarr, 1, 1, "# $sshverstr sftp client configuration file for curl testing";
981 for(my $i = scalar(@cfgarr) - 1; $i > 0; $i--) {
982 if($cfgarr[$i] =~ /^DynamicForward/) {
983 splice @cfgarr, $i, 1;
986 if($cfgarr[$i] =~ /^ClearAllForwardings/) {
987 splice @cfgarr, $i, 1, "ClearAllForwardings yes";
993 #***************************************************************************
994 # Write out resulting sftp client configuration file for curl's tests
996 $error = dump_array($sftpconfig, @cfgarr);
1004 #***************************************************************************
1005 # Generate client sftp commands batch file for sftp server verification
1007 logmsg 'generating sftp client commands file...' if($verbose);
1008 push @cfgarr, 'pwd';
1009 push @cfgarr, 'quit';
1010 $error = dump_array($sftpcmds, @cfgarr);
1018 #***************************************************************************
1019 # Start the ssh server daemon without forking it
1021 logmsg "SCP/SFTP server listening on port $port" if($verbose);
1022 my $rc = system "$sshd -e -D -f $sshdconfig > $sshdlog 2>&1";
1024 logmsg "$sshd failed with: $!";
1027 logmsg sprintf("$sshd died with signal %d, and %s coredump",
1028 ($rc & 127), ($rc & 128)?'a':'no');
1030 elsif($verbose && ($rc >> 8)) {
1031 logmsg sprintf("$sshd exited with %d", $rc >> 8);
1035 #***************************************************************************
1036 # Clean up once the server has stopped
1038 unlink($hstprvkeyf, $hstpubkeyf, $cliprvkeyf, $clipubkeyf, $knownhosts);
1039 unlink($sshdconfig, $sshconfig, $sftpconfig);