1# $OpenBSD: test-exec.sh,v 1.92 2022/07/25 07:12:45 dtucker Exp $ 2# Placed in the Public Domain. 3 4#SUDO=sudo 5 6if [ ! -z "$TEST_SSH_ELAPSED_TIMES" ]; then 7 STARTTIME=`date '+%s'` 8fi 9 10if [ ! -z "$TEST_SSH_PORT" ]; then 11 PORT="$TEST_SSH_PORT" 12else 13 PORT=4242 14fi 15 16OBJ=$1 17if [ "x$OBJ" = "x" ]; then 18 echo '$OBJ not defined' 19 exit 2 20fi 21if [ ! -d $OBJ ]; then 22 echo "not a directory: $OBJ" 23 exit 2 24fi 25SCRIPT=$2 26if [ "x$SCRIPT" = "x" ]; then 27 echo '$SCRIPT not defined' 28 exit 2 29fi 30if [ ! -f $SCRIPT ]; then 31 echo "not a file: $SCRIPT" 32 exit 2 33fi 34if $TEST_SHELL -n $SCRIPT; then 35 true 36else 37 echo "syntax error in $SCRIPT" 38 exit 2 39fi 40unset SSH_AUTH_SOCK 41 42# Portable-specific settings. 43 44if [ -x /usr/ucb/whoami ]; then 45 USER=`/usr/ucb/whoami` 46elif whoami >/dev/null 2>&1; then 47 USER=`whoami` 48elif logname >/dev/null 2>&1; then 49 USER=`logname` 50else 51 USER=`id -un` 52fi 53if test -z "$LOGNAME"; then 54 LOGNAME="${USER}" 55 export LOGNAME 56fi 57 58# Unbreak GNU head(1) 59_POSIX2_VERSION=199209 60export _POSIX2_VERSION 61 62case `uname -s 2>/dev/null` in 63OSF1*) 64 BIN_SH=xpg4 65 export BIN_SH 66 ;; 67CYGWIN*) 68 os=cygwin 69 ;; 70esac 71 72# If configure tells us to use a different egrep, create a wrapper function 73# to call it. This means we don't need to change all the tests that depend 74# on a good implementation. 75if test "x${EGREP}" != "x"; then 76 egrep () 77{ 78 ${EGREP} "$@" 79} 80fi 81 82SRC=`dirname ${SCRIPT}` 83 84# defaults 85SSH=ssh 86SSHD=sshd 87SSHAGENT=ssh-agent 88SSHADD=ssh-add 89SSHKEYGEN=ssh-keygen 90SSHKEYSCAN=ssh-keyscan 91SFTP=sftp 92SFTPSERVER=/usr/libexec/openssh/sftp-server 93SCP=scp 94 95# Set by make_tmpdir() on demand (below). 96SSH_REGRESS_TMP= 97 98# Interop testing 99PLINK=plink 100PUTTYGEN=puttygen 101CONCH=conch 102 103# Tools used by multiple tests 104NC=$OBJ/netcat 105OPENSSL_BIN="${OPENSSL_BIN:-openssl}" 106 107if [ "x$TEST_SSH_SSH" != "x" ]; then 108 SSH="${TEST_SSH_SSH}" 109fi 110if [ "x$TEST_SSH_SSHD" != "x" ]; then 111 SSHD="${TEST_SSH_SSHD}" 112fi 113if [ "x$TEST_SSH_SSHAGENT" != "x" ]; then 114 SSHAGENT="${TEST_SSH_SSHAGENT}" 115fi 116if [ "x$TEST_SSH_SSHADD" != "x" ]; then 117 SSHADD="${TEST_SSH_SSHADD}" 118fi 119if [ "x$TEST_SSH_SSHKEYGEN" != "x" ]; then 120 SSHKEYGEN="${TEST_SSH_SSHKEYGEN}" 121fi 122if [ "x$TEST_SSH_SSHKEYSCAN" != "x" ]; then 123 SSHKEYSCAN="${TEST_SSH_SSHKEYSCAN}" 124fi 125if [ "x$TEST_SSH_SFTP" != "x" ]; then 126 SFTP="${TEST_SSH_SFTP}" 127fi 128if [ "x$TEST_SSH_SFTPSERVER" != "x" ]; then 129 SFTPSERVER="${TEST_SSH_SFTPSERVER}" 130fi 131if [ "x$TEST_SSH_SCP" != "x" ]; then 132 SCP="${TEST_SSH_SCP}" 133fi 134if [ "x$TEST_SSH_PLINK" != "x" ]; then 135 # Find real binary, if it exists 136 case "${TEST_SSH_PLINK}" in 137 /*) PLINK="${TEST_SSH_PLINK}" ;; 138 *) PLINK=`which ${TEST_SSH_PLINK} 2>/dev/null` ;; 139 esac 140fi 141if [ "x$TEST_SSH_PUTTYGEN" != "x" ]; then 142 # Find real binary, if it exists 143 case "${TEST_SSH_PUTTYGEN}" in 144 /*) PUTTYGEN="${TEST_SSH_PUTTYGEN}" ;; 145 *) PUTTYGEN=`which ${TEST_SSH_PUTTYGEN} 2>/dev/null` ;; 146 esac 147fi 148if [ "x$TEST_SSH_CONCH" != "x" ]; then 149 # Find real binary, if it exists 150 case "${TEST_SSH_CONCH}" in 151 /*) CONCH="${TEST_SSH_CONCH}" ;; 152 *) CONCH=`which ${TEST_SSH_CONCH} 2>/dev/null` ;; 153 esac 154fi 155if [ "x$TEST_SSH_PKCS11_HELPER" != "x" ]; then 156 SSH_PKCS11_HELPER="${TEST_SSH_PKCS11_HELPER}" 157fi 158if [ "x$TEST_SSH_SK_HELPER" != "x" ]; then 159 SSH_SK_HELPER="${TEST_SSH_SK_HELPER}" 160fi 161if [ "x$TEST_SSH_OPENSSL" != "x" ]; then 162 OPENSSL_BIN="${TEST_SSH_OPENSSL}" 163fi 164 165# Path to sshd must be absolute for rexec 166case "$SSHD" in 167/*) ;; 168*) SSHD=`which $SSHD` ;; 169esac 170 171case "$SSHAGENT" in 172/*) ;; 173*) SSHAGENT=`which $SSHAGENT` ;; 174esac 175 176# Record the actual binaries used. 177SSH_BIN=${SSH} 178SSHD_BIN=${SSHD} 179SSHAGENT_BIN=${SSHAGENT} 180SSHADD_BIN=${SSHADD} 181SSHKEYGEN_BIN=${SSHKEYGEN} 182SSHKEYSCAN_BIN=${SSHKEYSCAN} 183SFTP_BIN=${SFTP} 184SFTPSERVER_BIN=${SFTPSERVER} 185SCP_BIN=${SCP} 186 187if [ "x$USE_VALGRIND" != "x" ]; then 188 rm -rf $OBJ/valgrind-out $OBJ/valgrind-vgdb 189 mkdir -p $OBJ/valgrind-out $OBJ/valgrind-vgdb 190 # When using sudo ensure low-priv tests can write pipes and logs. 191 if [ "x$SUDO" != "x" ]; then 192 chmod 777 $OBJ/valgrind-out $OBJ/valgrind-vgdb 193 fi 194 VG_TEST=`basename $SCRIPT .sh` 195 196 # Some tests are difficult to fix. 197 case "$VG_TEST" in 198 reexec) 199 VG_SKIP=1 ;; 200 sftp-chroot) 201 if [ "x${SUDO}" != "x" ]; then 202 VG_SKIP=1 203 fi ;; 204 esac 205 206 if [ x"$VG_SKIP" = "x" ]; then 207 VG_LEAK="--leak-check=no" 208 if [ x"$VALGRIND_CHECK_LEAKS" != "x" ]; then 209 VG_LEAK="--leak-check=full" 210 fi 211 VG_IGNORE="/bin/*,/sbin/*,/usr/*,/var/*" 212 VG_LOG="$OBJ/valgrind-out/${VG_TEST}." 213 VG_OPTS="--track-origins=yes $VG_LEAK" 214 VG_OPTS="$VG_OPTS --trace-children=yes" 215 VG_OPTS="$VG_OPTS --trace-children-skip=${VG_IGNORE}" 216 VG_OPTS="$VG_OPTS --vgdb-prefix=$OBJ/valgrind-vgdb/" 217 VG_PATH="valgrind" 218 if [ "x$VALGRIND_PATH" != "x" ]; then 219 VG_PATH="$VALGRIND_PATH" 220 fi 221 VG="$VG_PATH $VG_OPTS" 222 SSH="$VG --log-file=${VG_LOG}ssh.%p $SSH" 223 SSHD="$VG --log-file=${VG_LOG}sshd.%p $SSHD" 224 SSHAGENT="$VG --log-file=${VG_LOG}ssh-agent.%p $SSHAGENT" 225 SSHADD="$VG --log-file=${VG_LOG}ssh-add.%p $SSHADD" 226 SSHKEYGEN="$VG --log-file=${VG_LOG}ssh-keygen.%p $SSHKEYGEN" 227 SSHKEYSCAN="$VG --log-file=${VG_LOG}ssh-keyscan.%p $SSHKEYSCAN" 228 SFTP="$VG --log-file=${VG_LOG}sftp.%p ${SFTP}" 229 SCP="$VG --log-file=${VG_LOG}scp.%p $SCP" 230 cat > $OBJ/valgrind-sftp-server.sh << EOF 231#!/bin/sh 232exec $VG --log-file=${VG_LOG}sftp-server.%p $SFTPSERVER "\$@" 233EOF 234 chmod a+rx $OBJ/valgrind-sftp-server.sh 235 SFTPSERVER="$OBJ/valgrind-sftp-server.sh" 236 fi 237fi 238 239# Logfiles. 240# SSH_LOGFILE should be the debug output of ssh(1) only 241# SSHD_LOGFILE should be the debug output of sshd(8) only 242# REGRESS_LOGFILE is the output of the test itself stdout and stderr 243if [ "x$TEST_SSH_LOGFILE" = "x" ]; then 244 TEST_SSH_LOGFILE=$OBJ/ssh.log 245fi 246if [ "x$TEST_SSHD_LOGFILE" = "x" ]; then 247 TEST_SSHD_LOGFILE=$OBJ/sshd.log 248fi 249if [ "x$TEST_REGRESS_LOGFILE" = "x" ]; then 250 TEST_REGRESS_LOGFILE=$OBJ/regress.log 251fi 252 253# If set, keep track of successful tests and skip them them if we've 254# previously completed that test. 255if [ "x$TEST_REGRESS_CACHE_DIR" != "x" ]; then 256 if [ ! -d "$TEST_REGRESS_CACHE_DIR" ]; then 257 mkdir -p "$TEST_REGRESS_CACHE_DIR" 258 fi 259 TEST="`basename $SCRIPT .sh`" 260 CACHE="${TEST_REGRESS_CACHE_DIR}/${TEST}.cache" 261 for i in ${SSH} ${SSHD} ${SSHAGENT} ${SSHADD} ${SSHKEYGEN} ${SCP} \ 262 ${SFTP} ${SFTPSERVER} ${SSHKEYSCAN}; do 263 case $i in 264 /*) bin="$i" ;; 265 *) bin="`which $i`" ;; 266 esac 267 if [ "$bin" -nt "$CACHE" ]; then 268 rm -f "$CACHE" 269 fi 270 done 271 if [ -f "$CACHE" ]; then 272 echo ok cached $CACHE 273 exit 0 274 fi 275fi 276 277# truncate logfiles 278>$TEST_SSH_LOGFILE 279>$TEST_SSHD_LOGFILE 280>$TEST_REGRESS_LOGFILE 281 282# Create wrapper ssh with logging. We can't just specify "SSH=ssh -E..." 283# because sftp and scp don't handle spaces in arguments. scp and sftp like 284# to use -q so we remove those to preserve our debug logging. In the rare 285# instance where -q is desirable -qq is equivalent and is not removed. 286SSHLOGWRAP=$OBJ/ssh-log-wrapper.sh 287cat >$SSHLOGWRAP <<EOD 288#!/bin/sh 289echo "Executing: ${SSH} \$@" >>${TEST_SSH_LOGFILE} 290for i in "\$@";do shift;case "\$i" in -q):;; *) set -- "\$@" "\$i";;esac;done 291exec ${SSH} -E${TEST_SSH_LOGFILE} "\$@" 292EOD 293 294chmod a+rx $OBJ/ssh-log-wrapper.sh 295REAL_SSH="$SSH" 296REAL_SSHD="$SSHD" 297SSH="$SSHLOGWRAP" 298 299# Some test data. We make a copy because some tests will overwrite it. 300# The tests may assume that $DATA exists and is writable and $COPY does 301# not exist. Tests requiring larger data files can call increase_datafile_size 302# [kbytes] to ensure the file is at least that large. 303DATANAME=data 304DATA=$OBJ/${DATANAME} 305cat ${SSHAGENT_BIN} >${DATA} 306chmod u+w ${DATA} 307COPY=$OBJ/copy 308rm -f ${COPY} 309 310increase_datafile_size() 311{ 312 while [ `du -k ${DATA} | cut -f1` -lt $1 ]; do 313 cat ${SSHAGENT_BIN} >>${DATA} 314 done 315} 316 317# these should be used in tests 318export SSH SSHD SSHAGENT SSHADD SSHKEYGEN SSHKEYSCAN SFTP SFTPSERVER SCP 319export SSH_PKCS11_HELPER SSH_SK_HELPER 320#echo $SSH $SSHD $SSHAGENT $SSHADD $SSHKEYGEN $SSHKEYSCAN $SFTP $SFTPSERVER $SCP 321 322# Portable specific functions 323which() 324{ 325 saved_IFS="$IFS" 326 IFS=":" 327 for i in $PATH 328 do 329 if [ -x $i/$1 ]; then 330 IFS="$saved_IFS" 331 echo "$i/$1" 332 return 0 333 fi 334 done 335 IFS="$saved_IFS" 336 echo "$i/$1" 337 return 1 338} 339 340have_prog() 341{ 342 which "$1" >/dev/null 2>&1 343 return $? 344} 345 346jot() { 347 awk "BEGIN { for (i = $2; i < $2 + $1; i++) { printf \"%d\n\", i } exit }" 348} 349if [ ! -x "`which rev`" ]; then 350rev() 351{ 352 awk '{for (i=length; i>0; i--) printf "%s", substr($0, i, 1); print ""}' 353} 354fi 355 356# Check whether preprocessor symbols are defined in config.h. 357config_defined () 358{ 359 str=$1 360 while test "x$2" != "x" ; do 361 str="$str|$2" 362 shift 363 done 364 egrep "^#define.*($str)" ${BUILDDIR}/config.h >/dev/null 2>&1 365} 366 367md5 () { 368 if have_prog md5sum; then 369 md5sum 370 elif have_prog openssl; then 371 openssl md5 372 elif have_prog cksum; then 373 cksum 374 elif have_prog sum; then 375 sum 376 elif [ -x ${OPENSSL_BIN} ]; then 377 ${OPENSSL_BIN} md5 378 else 379 wc -c 380 fi 381} 382 383# Some platforms don't have hostname at all, but on others uname -n doesn't 384# provide the fully qualified name we need, so in the former case we create 385# our own hostname function. 386if ! have_prog hostname; then 387 hostname() { 388 uname -n 389 } 390fi 391 392make_tmpdir () 393{ 394 SSH_REGRESS_TMP="$($OBJ/mkdtemp openssh-XXXXXXXX)" || \ 395 fatal "failed to create temporary directory" 396} 397# End of portable specific functions 398 399stop_sshd () 400{ 401 if [ -f $PIDFILE ]; then 402 pid=`$SUDO cat $PIDFILE` 403 if [ "X$pid" = "X" ]; then 404 echo no sshd running 405 else 406 if [ $pid -lt 2 ]; then 407 echo bad pid for sshd: $pid 408 else 409 $SUDO kill $pid 410 trace "wait for sshd to exit" 411 i=0; 412 while [ -f $PIDFILE -a $i -lt 5 ]; do 413 i=`expr $i + 1` 414 sleep $i 415 done 416 if test -f $PIDFILE; then 417 if $SUDO kill -0 $pid; then 418 echo "sshd didn't exit " \ 419 "port $PORT pid $pid" 420 else 421 echo "sshd died without cleanup" 422 fi 423 exit 1 424 fi 425 fi 426 fi 427 fi 428} 429 430# helper 431cleanup () 432{ 433 if [ "x$SSH_PID" != "x" ]; then 434 if [ $SSH_PID -lt 2 ]; then 435 echo bad pid for ssh: $SSH_PID 436 else 437 kill $SSH_PID 438 fi 439 fi 440 if [ "x$SSH_REGRESS_TMP" != "x" ]; then 441 rm -rf "$SSH_REGRESS_TMP" 442 fi 443 stop_sshd 444 if [ ! -z "$TEST_SSH_ELAPSED_TIMES" ]; then 445 now=`date '+%s'` 446 elapsed=$(($now - $STARTTIME)) 447 echo elapsed $elapsed `basename $SCRIPT .sh` 448 fi 449} 450 451start_debug_log () 452{ 453 echo "trace: $@" >$TEST_REGRESS_LOGFILE 454 echo "trace: $@" >$TEST_SSH_LOGFILE 455 echo "trace: $@" >$TEST_SSHD_LOGFILE 456} 457 458save_debug_log () 459{ 460 echo $@ >>$TEST_REGRESS_LOGFILE 461 echo $@ >>$TEST_SSH_LOGFILE 462 echo $@ >>$TEST_SSHD_LOGFILE 463 (cat $TEST_REGRESS_LOGFILE; echo) >>$OBJ/failed-regress.log 464 (cat $TEST_SSH_LOGFILE; echo) >>$OBJ/failed-ssh.log 465 (cat $TEST_SSHD_LOGFILE; echo) >>$OBJ/failed-sshd.log 466} 467 468trace () 469{ 470 start_debug_log $@ 471 if [ "X$TEST_SSH_TRACE" = "Xyes" ]; then 472 echo "$@" 473 fi 474} 475 476verbose () 477{ 478 start_debug_log $@ 479 if [ "X$TEST_SSH_QUIET" != "Xyes" ]; then 480 echo "$@" 481 fi 482} 483 484fail () 485{ 486 save_debug_log "FAIL: $@" 487 RESULT=1 488 echo "$@" 489 if test "x$TEST_SSH_FAIL_FATAL" != "x" ; then 490 cleanup 491 exit $RESULT 492 fi 493} 494 495fatal () 496{ 497 save_debug_log "FATAL: $@" 498 printf "FATAL: " 499 fail "$@" 500 cleanup 501 exit $RESULT 502} 503 504# Skip remaining tests in script. 505skip () 506{ 507 echo "SKIPPED: $@" 508 cleanup 509 exit $RESULT 510} 511 512RESULT=0 513PIDFILE=$OBJ/pidfile 514 515trap fatal 3 2 516 517# create server config 518cat << EOF > $OBJ/sshd_config 519 StrictModes no 520 Port $PORT 521 AddressFamily inet 522 ListenAddress 127.0.0.1 523 #ListenAddress ::1 524 PidFile $PIDFILE 525 AuthorizedKeysFile $OBJ/authorized_keys_%u 526 LogLevel DEBUG3 527 AcceptEnv _XXX_TEST_* 528 AcceptEnv _XXX_TEST 529 Subsystem sftp $SFTPSERVER 530EOF 531 532# This may be necessary if /usr/src and/or /usr/obj are group-writable, 533# but if you aren't careful with permissions then the unit tests could 534# be abused to locally escalate privileges. 535if [ ! -z "$TEST_SSH_UNSAFE_PERMISSIONS" ]; then 536 echo " StrictModes no" >> $OBJ/sshd_config 537else 538 # check and warn if excessive permissions are likely to cause failures. 539 unsafe="" 540 dir="${OBJ}" 541 while test ${dir} != "/"; do 542 if test -d "${dir}" && ! test -h "${dir}"; then 543 perms=`ls -ld ${dir}` 544 case "${perms}" in 545 ?????w????*|????????w?*) unsafe="${unsafe} ${dir}" ;; 546 esac 547 fi 548 dir=`dirname ${dir}` 549 done 550 if ! test -z "${unsafe}"; then 551 cat <<EOD 552 553WARNING: Unsafe (group or world writable) directory permissions found: 554${unsafe} 555 556These could be abused to locally escalate privileges. If you are 557sure that this is not a risk (eg there are no other users), you can 558bypass this check by setting TEST_SSH_UNSAFE_PERMISSIONS=1 559 560EOD 561 fi 562fi 563 564if [ ! -z "$TEST_SSH_MODULI_FILE" ]; then 565 trace "adding modulifile='$TEST_SSH_MODULI_FILE' to sshd_config" 566 echo " ModuliFile '$TEST_SSH_MODULI_FILE'" >> $OBJ/sshd_config 567fi 568 569if [ ! -z "$TEST_SSH_SSHD_CONFOPTS" ]; then 570 trace "adding sshd_config option $TEST_SSH_SSHD_CONFOPTS" 571 echo "$TEST_SSH_SSHD_CONFOPTS" >> $OBJ/sshd_config 572fi 573 574# server config for proxy connects 575cp $OBJ/sshd_config $OBJ/sshd_proxy 576 577# allow group-writable directories in proxy-mode 578echo 'StrictModes no' >> $OBJ/sshd_proxy 579 580# create client config 581cat << EOF > $OBJ/ssh_config 582Host * 583 Hostname 127.0.0.1 584 HostKeyAlias localhost-with-alias 585 Port $PORT 586 User $USER 587 GlobalKnownHostsFile $OBJ/known_hosts 588 UserKnownHostsFile $OBJ/known_hosts 589 PubkeyAuthentication yes 590 ChallengeResponseAuthentication no 591 PasswordAuthentication no 592 BatchMode yes 593 StrictHostKeyChecking yes 594 LogLevel DEBUG3 595EOF 596 597if [ ! -z "$TEST_SSH_SSH_CONFOPTS" ]; then 598 trace "adding ssh_config option $TEST_SSH_SSH_CONFOPTS" 599 echo "$TEST_SSH_SSH_CONFOPTS" >> $OBJ/ssh_config 600fi 601 602rm -f $OBJ/known_hosts $OBJ/authorized_keys_$USER 603 604SSH_SK_PROVIDER= 605if ! config_defined ENABLE_SK; then 606 trace skipping sk-dummy 607elif [ -f "${SRC}/misc/sk-dummy/obj/sk-dummy.so" ] ; then 608 SSH_SK_PROVIDER="${SRC}/misc/sk-dummy/obj/sk-dummy.so" 609elif [ -f "${OBJ}/misc/sk-dummy/sk-dummy.so" ] ; then 610 SSH_SK_PROVIDER="${OBJ}/misc/sk-dummy/sk-dummy.so" 611elif [ -f "${SRC}/misc/sk-dummy/sk-dummy.so" ] ; then 612 SSH_SK_PROVIDER="${SRC}/misc/sk-dummy/sk-dummy.so" 613fi 614export SSH_SK_PROVIDER 615 616if ! test -z "$SSH_SK_PROVIDER"; then 617 EXTRA_AGENT_ARGS='-P/*' # XXX want realpath(1)... 618 echo "SecurityKeyProvider $SSH_SK_PROVIDER" >> $OBJ/ssh_config 619 echo "SecurityKeyProvider $SSH_SK_PROVIDER" >> $OBJ/sshd_config 620 echo "SecurityKeyProvider $SSH_SK_PROVIDER" >> $OBJ/sshd_proxy 621fi 622export EXTRA_AGENT_ARGS 623 624maybe_filter_sk() { 625 if test -z "$SSH_SK_PROVIDER" ; then 626 grep -v ^sk 627 else 628 cat 629 fi 630} 631 632SSH_KEYTYPES=`$SSH -Q key-plain | maybe_filter_sk` 633SSH_HOSTKEY_TYPES=`$SSH -Q key-plain | maybe_filter_sk` 634 635for t in ${SSH_KEYTYPES}; do 636 # generate user key 637 if [ ! -f $OBJ/$t ] || [ ${SSHKEYGEN_BIN} -nt $OBJ/$t ]; then 638 trace "generating key type $t" 639 rm -f $OBJ/$t 640 ${SSHKEYGEN} -q -N '' -t $t -f $OBJ/$t ||\ 641 fail "ssh-keygen for $t failed" 642 else 643 trace "using cached key type $t" 644 fi 645 646 # setup authorized keys 647 cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER 648 echo IdentityFile $OBJ/$t >> $OBJ/ssh_config 649done 650 651for t in ${SSH_HOSTKEY_TYPES}; do 652 # known hosts file for client 653 ( 654 printf 'localhost-with-alias,127.0.0.1,::1 ' 655 cat $OBJ/$t.pub 656 ) >> $OBJ/known_hosts 657 658 # use key as host key, too 659 (umask 077; $SUDO cp $OBJ/$t $OBJ/host.$t) 660 echo HostKey $OBJ/host.$t >> $OBJ/sshd_config 661 662 # don't use SUDO for proxy connect 663 echo HostKey $OBJ/$t >> $OBJ/sshd_proxy 664done 665chmod 644 $OBJ/authorized_keys_$USER 666 667# Activate Twisted Conch tests if the binary is present 668REGRESS_INTEROP_CONCH=no 669if test -x "$CONCH" ; then 670 REGRESS_INTEROP_CONCH=yes 671fi 672 673# If PuTTY is present, new enough and we are running a PuTTY test, prepare 674# keys and configuration. 675REGRESS_INTEROP_PUTTY=no 676if test -x "$PUTTYGEN" -a -x "$PLINK" && 677 "$PUTTYGEN" --help 2>&1 | grep -- --new-passphrase >/dev/null; then 678 REGRESS_INTEROP_PUTTY=yes 679fi 680case "$SCRIPT" in 681*putty*) ;; 682*) REGRESS_INTEROP_PUTTY=no ;; 683esac 684 685if test "$REGRESS_INTEROP_PUTTY" = "yes" ; then 686 mkdir -p ${OBJ}/.putty 687 688 # Add a PuTTY key to authorized_keys 689 rm -f ${OBJ}/putty.rsa2 690 if ! "$PUTTYGEN" -t rsa -o ${OBJ}/putty.rsa2 \ 691 --random-device=/dev/urandom \ 692 --new-passphrase /dev/null < /dev/null > /dev/null; then 693 echo "Your installed version of PuTTY is too old to support --new-passphrase, skipping test" >&2 694 exit 1 695 fi 696 "$PUTTYGEN" -O public-openssh ${OBJ}/putty.rsa2 \ 697 >> $OBJ/authorized_keys_$USER 698 699 # Convert rsa2 host key to PuTTY format 700 cp $OBJ/ssh-rsa $OBJ/ssh-rsa_oldfmt 701 ${SSHKEYGEN} -p -N '' -m PEM -f $OBJ/ssh-rsa_oldfmt >/dev/null 702 ${SRC}/ssh2putty.sh 127.0.0.1 $PORT $OBJ/ssh-rsa_oldfmt > \ 703 ${OBJ}/.putty/sshhostkeys 704 ${SRC}/ssh2putty.sh 127.0.0.1 22 $OBJ/ssh-rsa_oldfmt >> \ 705 ${OBJ}/.putty/sshhostkeys 706 rm -f $OBJ/ssh-rsa_oldfmt 707 708 # Setup proxied session 709 mkdir -p ${OBJ}/.putty/sessions 710 rm -f ${OBJ}/.putty/sessions/localhost_proxy 711 echo "Protocol=ssh" >> ${OBJ}/.putty/sessions/localhost_proxy 712 echo "HostName=127.0.0.1" >> ${OBJ}/.putty/sessions/localhost_proxy 713 echo "PortNumber=$PORT" >> ${OBJ}/.putty/sessions/localhost_proxy 714 echo "ProxyMethod=5" >> ${OBJ}/.putty/sessions/localhost_proxy 715 echo "ProxyTelnetCommand=sh ${SRC}/sshd-log-wrapper.sh ${TEST_SSHD_LOGFILE} ${SSHD} -i -f $OBJ/sshd_proxy" >> ${OBJ}/.putty/sessions/localhost_proxy 716 echo "ProxyLocalhost=1" >> ${OBJ}/.putty/sessions/localhost_proxy 717 718 PUTTYDIR=${OBJ}/.putty 719 export PUTTYDIR 720fi 721 722# create a proxy version of the client config 723( 724 cat $OBJ/ssh_config 725 echo proxycommand ${SUDO} env SSH_SK_HELPER=\"$SSH_SK_HELPER\" sh ${SRC}/sshd-log-wrapper.sh ${TEST_SSHD_LOGFILE} ${SSHD} -i -f $OBJ/sshd_proxy 726) > $OBJ/ssh_proxy 727 728# check proxy config 729${SSHD} -t -f $OBJ/sshd_proxy || fatal "sshd_proxy broken" 730 731start_sshd () 732{ 733 # start sshd 734 $SUDO ${SSHD} -f $OBJ/sshd_config "$@" -t || fatal "sshd_config broken" 735 $SUDO env SSH_SK_HELPER="$SSH_SK_HELPER" \ 736 ${SSHD} -f $OBJ/sshd_config "$@" -E$TEST_SSHD_LOGFILE 737 738 trace "wait for sshd" 739 i=0; 740 while [ ! -f $PIDFILE -a $i -lt 10 ]; do 741 i=`expr $i + 1` 742 sleep $i 743 done 744 745 test -f $PIDFILE || fatal "no sshd running on port $PORT" 746} 747 748# source test body 749. $SCRIPT 750 751# kill sshd 752cleanup 753 754if [ "x$USE_VALGRIND" != "x" ]; then 755 # If there is an EXIT trap handler, invoke it now. 756 # Some tests set these to clean up processes such as ssh-agent. We 757 # need to wait for all valgrind processes to complete so we can check 758 # their logs, but since the EXIT traps are not invoked until 759 # test-exec.sh exits, waiting here will deadlock. 760 # This is not very portable but then neither is valgrind itself. 761 # As a bonus, dash (as used on the runners) has a "trap" that doesn't 762 # work in a pipeline (hence the temp file) or a subshell. 763 exithandler="" 764 trap >/tmp/trap.$$ && exithandler=$(cat /tmp/trap.$$ | \ 765 awk -F "'" '/EXIT$/{print $2}') 766 rm -f /tmp/trap.$$ 767 if [ "x${exithandler}" != "x" ]; then 768 verbose invoking EXIT trap handler early: ${exithandler} 769 eval "${exithandler}" 770 trap '' EXIT 771 fi 772 773 # wait for any running process to complete 774 wait; sleep 1 775 VG_RESULTS=$(find $OBJ/valgrind-out -type f -print) 776 VG_RESULT_COUNT=0 777 VG_FAIL_COUNT=0 778 for i in $VG_RESULTS; do 779 if grep "ERROR SUMMARY" $i >/dev/null; then 780 VG_RESULT_COUNT=$(($VG_RESULT_COUNT + 1)) 781 if ! grep "ERROR SUMMARY: 0 errors" $i >/dev/null; then 782 VG_FAIL_COUNT=$(($VG_FAIL_COUNT + 1)) 783 RESULT=1 784 verbose valgrind failure $i 785 cat $i 786 fi 787 fi 788 done 789 if [ x"$VG_SKIP" != "x" ]; then 790 verbose valgrind skipped 791 else 792 verbose valgrind results $VG_RESULT_COUNT failures $VG_FAIL_COUNT 793 fi 794fi 795 796if [ $RESULT -eq 0 ]; then 797 verbose ok $tid 798 if [ "x$CACHE" != "x" ]; then 799 touch "$CACHE" 800 fi 801else 802 echo failed $tid 803fi 804exit $RESULT 805