xref: /freebsd/crypto/openssh/regress/test-exec.sh (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1#	$OpenBSD: test-exec.sh,v 1.89 2022/01/06 22:14:25 dtucker Exp $
2#	Placed in the Public Domain.
3
4#SUDO=sudo
5
6if [ ! -x "$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# truncate logfiles
254>$TEST_SSH_LOGFILE
255>$TEST_SSHD_LOGFILE
256>$TEST_REGRESS_LOGFILE
257
258# Create wrapper ssh with logging.  We can't just specify "SSH=ssh -E..."
259# because sftp and scp don't handle spaces in arguments.  scp and sftp like
260# to use -q so we remove those to preserve our debug logging.  In the rare
261# instance where -q is desirable -qq is equivalent and is not removed.
262SSHLOGWRAP=$OBJ/ssh-log-wrapper.sh
263cat >$SSHLOGWRAP <<EOD
264#!/bin/sh
265echo "Executing: ${SSH} \$@" >>${TEST_SSH_LOGFILE}
266for i in "\$@";do shift;case "\$i" in -q):;; *) set -- "\$@" "\$i";;esac;done
267exec ${SSH} -E${TEST_SSH_LOGFILE} "\$@"
268EOD
269
270chmod a+rx $OBJ/ssh-log-wrapper.sh
271REAL_SSH="$SSH"
272REAL_SSHD="$SSHD"
273SSH="$SSHLOGWRAP"
274
275# Some test data.  We make a copy because some tests will overwrite it.
276# The tests may assume that $DATA exists and is writable and $COPY does
277# not exist.  Tests requiring larger data files can call increase_datafile_size
278# [kbytes] to ensure the file is at least that large.
279DATANAME=data
280DATA=$OBJ/${DATANAME}
281cat ${SSHAGENT_BIN} >${DATA}
282chmod u+w ${DATA}
283COPY=$OBJ/copy
284rm -f ${COPY}
285
286increase_datafile_size()
287{
288	while [ `du -k ${DATA} | cut -f1` -lt $1 ]; do
289		cat ${SSHAGENT_BIN} >>${DATA}
290	done
291}
292
293# these should be used in tests
294export SSH SSHD SSHAGENT SSHADD SSHKEYGEN SSHKEYSCAN SFTP SFTPSERVER SCP
295export SSH_PKCS11_HELPER SSH_SK_HELPER
296#echo $SSH $SSHD $SSHAGENT $SSHADD $SSHKEYGEN $SSHKEYSCAN $SFTP $SFTPSERVER $SCP
297
298# Portable specific functions
299have_prog()
300{
301	saved_IFS="$IFS"
302	IFS=":"
303	for i in $PATH
304	do
305		if [ -x $i/$1 ]; then
306			IFS="$saved_IFS"
307			return 0
308		fi
309	done
310	IFS="$saved_IFS"
311	return 1
312}
313
314jot() {
315	awk "BEGIN { for (i = $2; i < $2 + $1; i++) { printf \"%d\n\", i } exit }"
316}
317if [ ! -x "`which rev`" ]; then
318rev()
319{
320	awk '{for (i=length; i>0; i--) printf "%s", substr($0, i, 1); print ""}'
321}
322fi
323
324# Check whether preprocessor symbols are defined in config.h.
325config_defined ()
326{
327	str=$1
328	while test "x$2" != "x" ; do
329		str="$str|$2"
330		shift
331	done
332	egrep "^#define.*($str)" ${BUILDDIR}/config.h >/dev/null 2>&1
333}
334
335md5 () {
336	if have_prog md5sum; then
337		md5sum
338	elif have_prog openssl; then
339		openssl md5
340	elif have_prog cksum; then
341		cksum
342	elif have_prog sum; then
343		sum
344	elif [ -x ${OPENSSL_BIN} ]; then
345		${OPENSSL_BIN} md5
346	else
347		wc -c
348	fi
349}
350
351# Some platforms don't have hostname at all, but on others uname -n doesn't
352# provide the fully qualified name we need, so in the former case we create
353# our own hostname function.
354if ! have_prog hostname; then
355	hostname() {
356		uname -n
357	}
358fi
359
360make_tmpdir ()
361{
362	SSH_REGRESS_TMP="$($OBJ/mkdtemp openssh-XXXXXXXX)" || \
363	    fatal "failed to create temporary directory"
364}
365# End of portable specific functions
366
367stop_sshd ()
368{
369	if [ -f $PIDFILE ]; then
370		pid=`$SUDO cat $PIDFILE`
371		if [ "X$pid" = "X" ]; then
372			echo no sshd running
373		else
374			if [ $pid -lt 2 ]; then
375				echo bad pid for sshd: $pid
376			else
377				$SUDO kill $pid
378				trace "wait for sshd to exit"
379				i=0;
380				while [ -f $PIDFILE -a $i -lt 5 ]; do
381					i=`expr $i + 1`
382					sleep $i
383				done
384				if test -f $PIDFILE; then
385					if $SUDO kill -0 $pid; then
386						echo "sshd didn't exit " \
387						    "port $PORT pid $pid"
388					else
389						echo "sshd died without cleanup"
390					fi
391					exit 1
392				fi
393			fi
394		fi
395	fi
396}
397
398# helper
399cleanup ()
400{
401	if [ "x$SSH_PID" != "x" ]; then
402		if [ $SSH_PID -lt 2 ]; then
403			echo bad pid for ssh: $SSH_PID
404		else
405			kill $SSH_PID
406		fi
407	fi
408	if [ "x$SSH_REGRESS_TMP" != "x" ]; then
409		rm -rf "$SSH_REGRESS_TMP"
410	fi
411	stop_sshd
412	if [ ! -z "$TEST_SSH_ELAPSED_TIMES" ]; then
413		now=`date '+%s'`
414		elapsed=$(($now - $STARTTIME))
415		echo elapsed $elapsed `basename $SCRIPT .sh`
416	fi
417}
418
419start_debug_log ()
420{
421	echo "trace: $@" >$TEST_REGRESS_LOGFILE
422	echo "trace: $@" >$TEST_SSH_LOGFILE
423	echo "trace: $@" >$TEST_SSHD_LOGFILE
424}
425
426save_debug_log ()
427{
428	echo $@ >>$TEST_REGRESS_LOGFILE
429	echo $@ >>$TEST_SSH_LOGFILE
430	echo $@ >>$TEST_SSHD_LOGFILE
431	(cat $TEST_REGRESS_LOGFILE; echo) >>$OBJ/failed-regress.log
432	(cat $TEST_SSH_LOGFILE; echo) >>$OBJ/failed-ssh.log
433	(cat $TEST_SSHD_LOGFILE; echo) >>$OBJ/failed-sshd.log
434}
435
436trace ()
437{
438	start_debug_log $@
439	if [ "X$TEST_SSH_TRACE" = "Xyes" ]; then
440		echo "$@"
441	fi
442}
443
444verbose ()
445{
446	start_debug_log $@
447	if [ "X$TEST_SSH_QUIET" != "Xyes" ]; then
448		echo "$@"
449	fi
450}
451
452fail ()
453{
454	save_debug_log "FAIL: $@"
455	RESULT=1
456	echo "$@"
457	if test "x$TEST_SSH_FAIL_FATAL" != "x" ; then
458		cleanup
459		exit $RESULT
460	fi
461}
462
463fatal ()
464{
465	save_debug_log "FATAL: $@"
466	printf "FATAL: "
467	fail "$@"
468	cleanup
469	exit $RESULT
470}
471
472# Skip remaining tests in script.
473skip ()
474{
475	echo "SKIPPED: $@"
476	cleanup
477	exit $RESULT
478}
479
480RESULT=0
481PIDFILE=$OBJ/pidfile
482
483trap fatal 3 2
484
485# create server config
486cat << EOF > $OBJ/sshd_config
487	StrictModes		no
488	Port			$PORT
489	AddressFamily		inet
490	ListenAddress		127.0.0.1
491	#ListenAddress		::1
492	PidFile			$PIDFILE
493	AuthorizedKeysFile	$OBJ/authorized_keys_%u
494	LogLevel		DEBUG3
495	AcceptEnv		_XXX_TEST_*
496	AcceptEnv		_XXX_TEST
497	Subsystem	sftp	$SFTPSERVER
498EOF
499
500# This may be necessary if /usr/src and/or /usr/obj are group-writable,
501# but if you aren't careful with permissions then the unit tests could
502# be abused to locally escalate privileges.
503if [ ! -z "$TEST_SSH_UNSAFE_PERMISSIONS" ]; then
504	echo "	StrictModes no" >> $OBJ/sshd_config
505else
506	# check and warn if excessive permissions are likely to cause failures.
507	unsafe=""
508	dir="${OBJ}"
509	while test ${dir} != "/"; do
510		if test -d "${dir}" && ! test -h "${dir}"; then
511			perms=`ls -ld ${dir}`
512			case "${perms}" in
513			?????w????*|????????w?*) unsafe="${unsafe} ${dir}" ;;
514			esac
515		fi
516		dir=`dirname ${dir}`
517	done
518	if ! test  -z "${unsafe}"; then
519		cat <<EOD
520
521WARNING: Unsafe (group or world writable) directory permissions found:
522${unsafe}
523
524These could be abused to locally escalate privileges.  If you are
525sure that this is not a risk (eg there are no other users), you can
526bypass this check by setting TEST_SSH_UNSAFE_PERMISSIONS=1
527
528EOD
529	fi
530fi
531
532if [ ! -z "$TEST_SSH_MODULI_FILE" ]; then
533	trace "adding modulifile='$TEST_SSH_MODULI_FILE' to sshd_config"
534	echo "	ModuliFile '$TEST_SSH_MODULI_FILE'" >> $OBJ/sshd_config
535fi
536
537if [ ! -z "$TEST_SSH_SSHD_CONFOPTS" ]; then
538	trace "adding sshd_config option $TEST_SSH_SSHD_CONFOPTS"
539	echo "$TEST_SSH_SSHD_CONFOPTS" >> $OBJ/sshd_config
540fi
541
542# server config for proxy connects
543cp $OBJ/sshd_config $OBJ/sshd_proxy
544
545# allow group-writable directories in proxy-mode
546echo 'StrictModes no' >> $OBJ/sshd_proxy
547
548# create client config
549cat << EOF > $OBJ/ssh_config
550Host *
551	Hostname		127.0.0.1
552	HostKeyAlias		localhost-with-alias
553	Port			$PORT
554	User			$USER
555	GlobalKnownHostsFile	$OBJ/known_hosts
556	UserKnownHostsFile	$OBJ/known_hosts
557	PubkeyAuthentication	yes
558	ChallengeResponseAuthentication	no
559	PasswordAuthentication	no
560	BatchMode		yes
561	StrictHostKeyChecking	yes
562	LogLevel		DEBUG3
563EOF
564
565if [ ! -z "$TEST_SSH_SSH_CONFOPTS" ]; then
566	trace "adding ssh_config option $TEST_SSH_SSH_CONFOPTS"
567	echo "$TEST_SSH_SSH_CONFOPTS" >> $OBJ/ssh_config
568fi
569
570rm -f $OBJ/known_hosts $OBJ/authorized_keys_$USER
571
572SSH_SK_PROVIDER=
573if ! config_defined ENABLE_SK; then
574	trace skipping sk-dummy
575elif [ -f "${SRC}/misc/sk-dummy/obj/sk-dummy.so" ] ; then
576	SSH_SK_PROVIDER="${SRC}/misc/sk-dummy/obj/sk-dummy.so"
577elif [ -f "${OBJ}/misc/sk-dummy/sk-dummy.so" ] ; then
578	SSH_SK_PROVIDER="${OBJ}/misc/sk-dummy/sk-dummy.so"
579elif [ -f "${SRC}/misc/sk-dummy/sk-dummy.so" ] ; then
580	SSH_SK_PROVIDER="${SRC}/misc/sk-dummy/sk-dummy.so"
581fi
582export SSH_SK_PROVIDER
583
584if ! test -z "$SSH_SK_PROVIDER"; then
585	EXTRA_AGENT_ARGS='-P/*' # XXX want realpath(1)...
586	echo "SecurityKeyProvider $SSH_SK_PROVIDER" >> $OBJ/ssh_config
587	echo "SecurityKeyProvider $SSH_SK_PROVIDER" >> $OBJ/sshd_config
588	echo "SecurityKeyProvider $SSH_SK_PROVIDER" >> $OBJ/sshd_proxy
589fi
590export EXTRA_AGENT_ARGS
591
592maybe_filter_sk() {
593	if test -z "$SSH_SK_PROVIDER" ; then
594		grep -v ^sk
595	else
596		cat
597	fi
598}
599
600SSH_KEYTYPES=`$SSH -Q key-plain | maybe_filter_sk`
601SSH_HOSTKEY_TYPES=`$SSH -Q key-plain | maybe_filter_sk`
602
603for t in ${SSH_KEYTYPES}; do
604	# generate user key
605	if [ ! -f $OBJ/$t ] || [ ${SSHKEYGEN_BIN} -nt $OBJ/$t ]; then
606		trace "generating key type $t"
607		rm -f $OBJ/$t
608		${SSHKEYGEN} -q -N '' -t $t  -f $OBJ/$t ||\
609			fail "ssh-keygen for $t failed"
610	else
611		trace "using cached key type $t"
612	fi
613
614	# setup authorized keys
615	cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
616	echo IdentityFile $OBJ/$t >> $OBJ/ssh_config
617done
618
619for t in ${SSH_HOSTKEY_TYPES}; do
620	# known hosts file for client
621	(
622		printf 'localhost-with-alias,127.0.0.1,::1 '
623		cat $OBJ/$t.pub
624	) >> $OBJ/known_hosts
625
626	# use key as host key, too
627	(umask 077; $SUDO cp $OBJ/$t $OBJ/host.$t)
628	echo HostKey $OBJ/host.$t >> $OBJ/sshd_config
629
630	# don't use SUDO for proxy connect
631	echo HostKey $OBJ/$t >> $OBJ/sshd_proxy
632done
633chmod 644 $OBJ/authorized_keys_$USER
634
635# Activate Twisted Conch tests if the binary is present
636REGRESS_INTEROP_CONCH=no
637if test -x "$CONCH" ; then
638	REGRESS_INTEROP_CONCH=yes
639fi
640
641# If PuTTY is present, new enough and we are running a PuTTY test, prepare
642# keys and configuration.
643REGRESS_INTEROP_PUTTY=no
644if test -x "$PUTTYGEN" -a -x "$PLINK" &&
645    "$PUTTYGEN" --help 2>&1 | grep -- --new-passphrase >/dev/null; then
646	REGRESS_INTEROP_PUTTY=yes
647fi
648case "$SCRIPT" in
649*putty*)	;;
650*)		REGRESS_INTEROP_PUTTY=no ;;
651esac
652
653if test "$REGRESS_INTEROP_PUTTY" = "yes" ; then
654	mkdir -p ${OBJ}/.putty
655
656	# Add a PuTTY key to authorized_keys
657	rm -f ${OBJ}/putty.rsa2
658	if ! "$PUTTYGEN" -t rsa -o ${OBJ}/putty.rsa2 \
659	    --random-device=/dev/urandom \
660	    --new-passphrase /dev/null < /dev/null > /dev/null; then
661		echo "Your installed version of PuTTY is too old to support --new-passphrase, skipping test" >&2
662		exit 1
663	fi
664	"$PUTTYGEN" -O public-openssh ${OBJ}/putty.rsa2 \
665	    >> $OBJ/authorized_keys_$USER
666
667	# Convert rsa2 host key to PuTTY format
668	cp $OBJ/ssh-rsa $OBJ/ssh-rsa_oldfmt
669	${SSHKEYGEN} -p -N '' -m PEM -f $OBJ/ssh-rsa_oldfmt >/dev/null
670	${SRC}/ssh2putty.sh 127.0.0.1 $PORT $OBJ/ssh-rsa_oldfmt > \
671	    ${OBJ}/.putty/sshhostkeys
672	${SRC}/ssh2putty.sh 127.0.0.1 22 $OBJ/ssh-rsa_oldfmt >> \
673	    ${OBJ}/.putty/sshhostkeys
674	rm -f $OBJ/ssh-rsa_oldfmt
675
676	# Setup proxied session
677	mkdir -p ${OBJ}/.putty/sessions
678	rm -f ${OBJ}/.putty/sessions/localhost_proxy
679	echo "Protocol=ssh" >> ${OBJ}/.putty/sessions/localhost_proxy
680	echo "HostName=127.0.0.1" >> ${OBJ}/.putty/sessions/localhost_proxy
681	echo "PortNumber=$PORT" >> ${OBJ}/.putty/sessions/localhost_proxy
682	echo "ProxyMethod=5" >> ${OBJ}/.putty/sessions/localhost_proxy
683	echo "ProxyTelnetCommand=sh ${SRC}/sshd-log-wrapper.sh ${TEST_SSHD_LOGFILE} ${SSHD} -i -f $OBJ/sshd_proxy" >> ${OBJ}/.putty/sessions/localhost_proxy
684	echo "ProxyLocalhost=1" >> ${OBJ}/.putty/sessions/localhost_proxy
685
686	PUTTYDIR=${OBJ}/.putty
687	export PUTTYDIR
688fi
689
690# create a proxy version of the client config
691(
692	cat $OBJ/ssh_config
693	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
694) > $OBJ/ssh_proxy
695
696# check proxy config
697${SSHD} -t -f $OBJ/sshd_proxy	|| fatal "sshd_proxy broken"
698
699start_sshd ()
700{
701	# start sshd
702	$SUDO ${SSHD} -f $OBJ/sshd_config "$@" -t || fatal "sshd_config broken"
703	$SUDO env SSH_SK_HELPER="$SSH_SK_HELPER" \
704	    ${SSHD} -f $OBJ/sshd_config "$@" -E$TEST_SSHD_LOGFILE
705
706	trace "wait for sshd"
707	i=0;
708	while [ ! -f $PIDFILE -a $i -lt 10 ]; do
709		i=`expr $i + 1`
710		sleep $i
711	done
712
713	test -f $PIDFILE || fatal "no sshd running on port $PORT"
714}
715
716# source test body
717. $SCRIPT
718
719# kill sshd
720cleanup
721
722if [ "x$USE_VALGRIND" != "x" ]; then
723	# If there is an EXIT trap handler, invoke it now.
724	# Some tests set these to clean up processes such as ssh-agent.  We
725	# need to wait for all valgrind processes to complete so we can check
726	# their logs, but since the EXIT traps are not invoked until
727	# test-exec.sh exits, waiting here will deadlock.
728	# This is not very portable but then neither is valgrind itself.
729	# As a bonus, dash (as used on the runners) has a "trap" that doesn't
730	# work in a pipeline (hence the temp file) or a subshell.
731	exithandler=""
732	trap >/tmp/trap.$$ && exithandler=$(cat /tmp/trap.$$ | \
733	    awk -F "'" '/EXIT$/{print $2}')
734	rm -f /tmp/trap.$$
735	if [ "x${exithandler}" != "x" ]; then
736		verbose invoking EXIT trap handler early: ${exithandler}
737		eval "${exithandler}"
738		trap '' EXIT
739	fi
740
741	# wait for any running process to complete
742	wait; sleep 1
743	VG_RESULTS=$(find $OBJ/valgrind-out -type f -print)
744	VG_RESULT_COUNT=0
745	VG_FAIL_COUNT=0
746	for i in $VG_RESULTS; do
747		if grep "ERROR SUMMARY" $i >/dev/null; then
748			VG_RESULT_COUNT=$(($VG_RESULT_COUNT + 1))
749			if ! grep "ERROR SUMMARY: 0 errors" $i >/dev/null; then
750				VG_FAIL_COUNT=$(($VG_FAIL_COUNT + 1))
751				RESULT=1
752				verbose valgrind failure $i
753				cat $i
754			fi
755		fi
756	done
757	if [ x"$VG_SKIP" != "x" ]; then
758		verbose valgrind skipped
759	else
760		verbose valgrind results $VG_RESULT_COUNT failures $VG_FAIL_COUNT
761	fi
762fi
763
764if [ $RESULT -eq 0 ]; then
765	verbose ok $tid
766else
767	echo failed $tid
768fi
769exit $RESULT
770