1 # $OpenBSD: test-exec.sh,v 1.46 2013/06/21 02:26:26 djm Exp $
2 # Placed in the Public Domain.
10 case `uname -s 2>/dev/null` in
24 if [ ! -z "$TEST_SSH_PORT" ]; then
30 if [ -x /usr/ucb/whoami ]; then
31 USER=`/usr/ucb/whoami`
32 elif whoami >/dev/null 2>&1; then
34 elif logname >/dev/null 2>&1; then
41 if [ "x$OBJ" = "x" ]; then
42 echo '$OBJ not defined'
45 if [ ! -d $OBJ ]; then
46 echo "not a directory: $OBJ"
50 if [ "x$SCRIPT" = "x" ]; then
51 echo '$SCRIPT not defined'
54 if [ ! -f $SCRIPT ]; then
55 echo "not a file: $SCRIPT"
58 if $TEST_SHELL -n $SCRIPT; then
61 echo "syntax error in $SCRIPT"
66 SRC=`dirname ${SCRIPT}`
74 SSHKEYSCAN=ssh-keyscan
76 SFTPSERVER=/usr/libexec/openssh/sftp-server
84 if [ "x$TEST_SSH_SSH" != "x" ]; then
87 if [ "x$TEST_SSH_SSHD" != "x" ]; then
88 SSHD="${TEST_SSH_SSHD}"
90 if [ "x$TEST_SSH_SSHAGENT" != "x" ]; then
91 SSHAGENT="${TEST_SSH_SSHAGENT}"
93 if [ "x$TEST_SSH_SSHADD" != "x" ]; then
94 SSHADD="${TEST_SSH_SSHADD}"
96 if [ "x$TEST_SSH_SSHKEYGEN" != "x" ]; then
97 SSHKEYGEN="${TEST_SSH_SSHKEYGEN}"
99 if [ "x$TEST_SSH_SSHKEYSCAN" != "x" ]; then
100 SSHKEYSCAN="${TEST_SSH_SSHKEYSCAN}"
102 if [ "x$TEST_SSH_SFTP" != "x" ]; then
103 SFTP="${TEST_SSH_SFTP}"
105 if [ "x$TEST_SSH_SFTPSERVER" != "x" ]; then
106 SFTPSERVER="${TEST_SSH_SFTPSERVER}"
108 if [ "x$TEST_SSH_SCP" != "x" ]; then
109 SCP="${TEST_SSH_SCP}"
111 if [ "x$TEST_SSH_PLINK" != "x" ]; then
112 # Find real binary, if it exists
113 case "${TEST_SSH_PLINK}" in
114 /*) PLINK="${TEST_SSH_PLINK}" ;;
115 *) PLINK=`which ${TEST_SSH_PLINK} 2>/dev/null` ;;
118 if [ "x$TEST_SSH_PUTTYGEN" != "x" ]; then
119 # Find real binary, if it exists
120 case "${TEST_SSH_PUTTYGEN}" in
121 /*) PUTTYGEN="${TEST_SSH_PUTTYGEN}" ;;
122 *) PUTTYGEN=`which ${TEST_SSH_PUTTYGEN} 2>/dev/null` ;;
125 if [ "x$TEST_SSH_CONCH" != "x" ]; then
126 # Find real binary, if it exists
127 case "${TEST_SSH_CONCH}" in
128 /*) CONCH="${TEST_SSH_CONCH}" ;;
129 *) CONCH=`which ${TEST_SSH_CONCH} 2>/dev/null` ;;
133 # Path to sshd must be absolute for rexec
136 *) SSHD=`which sshd` ;;
140 # SSH_LOGFILE should be the debug output of ssh(1) only
141 # SSHD_LOGFILE should be the debug output of sshd(8) only
142 # REGRESS_LOGFILE is the output of the test itself stdout and stderr
143 if [ "x$TEST_SSH_LOGFILE" = "x" ]; then
144 TEST_SSH_LOGFILE=$OBJ/ssh.log
146 if [ "x$TEST_SSHD_LOGFILE" = "x" ]; then
147 TEST_SSHD_LOGFILE=$OBJ/sshd.log
149 if [ "x$TEST_REGRESS_LOGFILE" = "x" ]; then
150 TEST_REGRESS_LOGFILE=$OBJ/regress.log
156 >$TEST_REGRESS_LOGFILE
158 # Create wrapper ssh with logging. We can't just specify "SSH=ssh -E..."
159 # because sftp and scp don't handle spaces in arguments.
160 SSHLOGWRAP=$OBJ/ssh-log-wrapper.sh
161 echo "#!/bin/sh" > $SSHLOGWRAP
162 echo "exec ${SSH} -E${TEST_SSH_LOGFILE} "'"$@"' >>$SSHLOGWRAP
164 chmod a+rx $OBJ/ssh-log-wrapper.sh
167 # Some test data. We make a copy because some tests will overwrite it.
168 # The tests may assume that $DATA exists and is writable and $COPY does
171 DATA=$OBJ/${DATANAME}
172 cat $SSHD $SSHD $SSHD $SSHD >${DATA}
177 # these should be used in tests
178 export SSH SSHD SSHAGENT SSHADD SSHKEYGEN SSHKEYSCAN SFTP SFTPSERVER SCP
179 #echo $SSH $SSHD $SSHAGENT $SSHADD $SSHKEYGEN $SSHKEYSCAN $SFTP $SFTPSERVER $SCP
181 # Portable specific functions
188 if [ -x $i/$1 ]; then
198 awk "BEGIN { for (i = $2; i < $2 + $1; i++) { printf \"%d\n\", i } exit }"
201 # Check whether preprocessor symbols are defined in config.h.
205 while test "x$2" != "x" ; do
209 egrep "^#define.*($str)" ${BUILDDIR}/config.h >/dev/null 2>&1
213 if have_prog md5sum; then
215 elif have_prog openssl; then
217 elif have_prog cksum; then
219 elif have_prog sum; then
225 # End of portable specific functions
230 if [ -f $PIDFILE ]; then
231 pid=`$SUDO cat $PIDFILE`
232 if [ "X$pid" = "X" ]; then
235 if [ $pid -lt 2 ]; then
236 echo bad pid for ssh: $pid
239 trace "wait for sshd to exit"
241 while [ -f $PIDFILE -a $i -lt 5 ]; do
245 test -f $PIDFILE && \
246 fatal "sshd didn't exit port $PORT pid $pid"
254 echo "trace: $@" >$TEST_REGRESS_LOGFILE
255 echo "trace: $@" >$TEST_SSH_LOGFILE
256 echo "trace: $@" >$TEST_SSHD_LOGFILE
261 echo $@ >>$TEST_REGRESS_LOGFILE
262 echo $@ >>$TEST_SSH_LOGFILE
263 echo $@ >>$TEST_SSHD_LOGFILE
264 (cat $TEST_REGRESS_LOGFILE; echo) >>$OBJ/failed-regress.log
265 (cat $TEST_SSH_LOGFILE; echo) >>$OBJ/failed-ssh.log
266 (cat $TEST_SSHD_LOGFILE; echo) >>$OBJ/failed-sshd.log
272 if [ "X$TEST_SSH_TRACE" = "Xyes" ]; then
280 if [ "X$TEST_SSH_QUIET" != "Xyes" ]; then
287 echo "WARNING: $@" >>$TEST_SSH_LOGFILE
293 save_debug_log "FAIL: $@"
301 save_debug_log "FATAL: $@"
313 # create server config
314 cat << EOF > $OBJ/sshd_config
319 ListenAddress 127.0.0.1
322 AuthorizedKeysFile $OBJ/authorized_keys_%u
324 AcceptEnv _XXX_TEST_*
326 Subsystem sftp $SFTPSERVER
329 if [ ! -z "$TEST_SSH_SSHD_CONFOPTS" ]; then
330 trace "adding sshd_config option $TEST_SSH_SSHD_CONFOPTS"
331 echo "$TEST_SSH_SSHD_CONFOPTS" >> $OBJ/sshd_config
334 # server config for proxy connects
335 cp $OBJ/sshd_config $OBJ/sshd_proxy
337 # allow group-writable directories in proxy-mode
338 echo 'StrictModes no' >> $OBJ/sshd_proxy
340 # create client config
341 cat << EOF > $OBJ/ssh_config
345 HostKeyAlias localhost-with-alias
348 GlobalKnownHostsFile $OBJ/known_hosts
349 UserKnownHostsFile $OBJ/known_hosts
350 RSAAuthentication yes
351 PubkeyAuthentication yes
352 ChallengeResponseAuthentication no
353 HostbasedAuthentication no
354 PasswordAuthentication no
355 RhostsRSAAuthentication no
357 StrictHostKeyChecking yes
361 if [ ! -z "$TEST_SSH_SSH_CONFOPTS" ]; then
362 trace "adding ssh_config option $TEST_SSH_SSHD_CONFOPTS"
363 echo "$TEST_SSH_SSH_CONFOPTS" >> $OBJ/ssh_config
366 rm -f $OBJ/known_hosts $OBJ/authorized_keys_$USER
368 trace "generate keys"
369 for t in rsa rsa1; do
371 if [ ! -f $OBJ/$t ] || [ ${SSHKEYGEN} -nt $OBJ/$t ]; then
373 ${SSHKEYGEN} -q -N '' -t $t -f $OBJ/$t ||\
374 fail "ssh-keygen for $t failed"
377 # known hosts file for client
379 printf 'localhost-with-alias,127.0.0.1,::1 '
381 ) >> $OBJ/known_hosts
383 # setup authorized keys
384 cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
385 echo IdentityFile $OBJ/$t >> $OBJ/ssh_config
387 # use key as host key, too
388 $SUDO cp $OBJ/$t $OBJ/host.$t
389 echo HostKey $OBJ/host.$t >> $OBJ/sshd_config
391 # don't use SUDO for proxy connect
392 echo HostKey $OBJ/$t >> $OBJ/sshd_proxy
394 chmod 644 $OBJ/authorized_keys_$USER
396 # Activate Twisted Conch tests if the binary is present
397 REGRESS_INTEROP_CONCH=no
398 if test -x "$CONCH" ; then
399 REGRESS_INTEROP_CONCH=yes
402 # If PuTTY is present and we are running a PuTTY test, prepare keys and
404 REGRESS_INTEROP_PUTTY=no
405 if test -x "$PUTTYGEN" -a -x "$PLINK" ; then
406 REGRESS_INTEROP_PUTTY=yes
410 *) REGRESS_INTEROP_PUTTY=no ;;
413 if test "$REGRESS_INTEROP_PUTTY" = "yes" ; then
414 mkdir -p ${OBJ}/.putty
416 # Add a PuTTY key to authorized_keys
417 rm -f ${OBJ}/putty.rsa2
418 puttygen -t rsa -o ${OBJ}/putty.rsa2 < /dev/null > /dev/null
419 puttygen -O public-openssh ${OBJ}/putty.rsa2 \
420 >> $OBJ/authorized_keys_$USER
422 # Convert rsa2 host key to PuTTY format
423 ${SRC}/ssh2putty.sh 127.0.0.1 $PORT $OBJ/rsa > \
424 ${OBJ}/.putty/sshhostkeys
425 ${SRC}/ssh2putty.sh 127.0.0.1 22 $OBJ/rsa >> \
426 ${OBJ}/.putty/sshhostkeys
428 # Setup proxied session
429 mkdir -p ${OBJ}/.putty/sessions
430 rm -f ${OBJ}/.putty/sessions/localhost_proxy
431 echo "Hostname=127.0.0.1" >> ${OBJ}/.putty/sessions/localhost_proxy
432 echo "PortNumber=$PORT" >> ${OBJ}/.putty/sessions/localhost_proxy
433 echo "ProxyMethod=5" >> ${OBJ}/.putty/sessions/localhost_proxy
434 echo "ProxyTelnetCommand=sh ${SRC}/sshd-log-wrapper.sh ${SSHD} ${TEST_SSHD_LOGFILE} -i -f $OBJ/sshd_proxy" >> ${OBJ}/.putty/sessions/localhost_proxy
436 REGRESS_INTEROP_PUTTY=yes
439 # create a proxy version of the client config
442 echo proxycommand ${SUDO} sh ${SRC}/sshd-log-wrapper.sh ${SSHD} ${TEST_SSHD_LOGFILE} -i -f $OBJ/sshd_proxy
446 ${SSHD} -t -f $OBJ/sshd_proxy || fatal "sshd_proxy broken"
451 $SUDO ${SSHD} -f $OBJ/sshd_config "$@" -t || fatal "sshd_config broken"
452 $SUDO ${SSHD} -f $OBJ/sshd_config "$@" -E$TEST_SSHD_LOGFILE
454 trace "wait for sshd"
456 while [ ! -f $PIDFILE -a $i -lt 10 ]; do
461 test -f $PIDFILE || fatal "no sshd running on port $PORT"
469 if [ $RESULT -eq 0 ]; then