1 # $OpenBSD: test-exec.sh,v 1.59 2017/02/07 23:03:11 dtucker 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` ;;
134 #SSH_PROTOCOLS=`$SSH -Q protocol-version`
135 if [ "x$TEST_SSH_PROTOCOLS" != "x" ]; then
136 SSH_PROTOCOLS="${TEST_SSH_PROTOCOLS}"
139 # Path to sshd must be absolute for rexec
142 *) SSHD=`which $SSHD` ;;
147 *) SSHAGENT=`which $SSHAGENT` ;;
150 # Record the actual binaries used.
153 SSHAGENT_BIN=${SSHAGENT}
155 SSHKEYGEN_BIN=${SSHKEYGEN}
156 SSHKEYSCAN_BIN=${SSHKEYSCAN}
158 SFTPSERVER_BIN=${SFTPSERVER}
161 if [ "x$USE_VALGRIND" != "x" ]; then
162 mkdir -p $OBJ/valgrind-out
163 VG_TEST=`basename $SCRIPT .sh`
165 # Some tests are difficult to fix.
167 connect-privsep|reexec)
171 if [ x"$VG_SKIP" = "x" ]; then
172 VG_IGNORE="/bin/*,/sbin/*,/usr/*,/var/*"
173 VG_LOG="$OBJ/valgrind-out/${VG_TEST}."
174 VG_OPTS="--track-origins=yes --leak-check=full"
175 VG_OPTS="$VG_OPTS --trace-children=yes"
176 VG_OPTS="$VG_OPTS --trace-children-skip=${VG_IGNORE}"
178 if [ "x$VALGRIND_PATH" != "x" ]; then
179 VG_PATH="$VALGRIND_PATH"
181 VG="$VG_PATH $VG_OPTS"
182 SSH="$VG --log-file=${VG_LOG}ssh.%p $SSH"
183 SSHD="$VG --log-file=${VG_LOG}sshd.%p $SSHD"
184 SSHAGENT="$VG --log-file=${VG_LOG}ssh-agent.%p $SSHAGENT"
185 SSHADD="$VG --log-file=${VG_LOG}ssh-add.%p $SSHADD"
186 SSHKEYGEN="$VG --log-file=${VG_LOG}ssh-keygen.%p $SSHKEYGEN"
187 SSHKEYSCAN="$VG --log-file=${VG_LOG}ssh-keyscan.%p $SSHKEYSCAN"
188 SFTP="$VG --log-file=${VG_LOG}sftp.%p ${SFTP}"
189 SCP="$VG --log-file=${VG_LOG}scp.%p $SCP"
190 cat > $OBJ/valgrind-sftp-server.sh << EOF
192 exec $VG --log-file=${VG_LOG}sftp-server.%p $SFTPSERVER "\$@"
194 chmod a+rx $OBJ/valgrind-sftp-server.sh
195 SFTPSERVER="$OBJ/valgrind-sftp-server.sh"
200 # SSH_LOGFILE should be the debug output of ssh(1) only
201 # SSHD_LOGFILE should be the debug output of sshd(8) only
202 # REGRESS_LOGFILE is the output of the test itself stdout and stderr
203 if [ "x$TEST_SSH_LOGFILE" = "x" ]; then
204 TEST_SSH_LOGFILE=$OBJ/ssh.log
206 if [ "x$TEST_SSHD_LOGFILE" = "x" ]; then
207 TEST_SSHD_LOGFILE=$OBJ/sshd.log
209 if [ "x$TEST_REGRESS_LOGFILE" = "x" ]; then
210 TEST_REGRESS_LOGFILE=$OBJ/regress.log
216 >$TEST_REGRESS_LOGFILE
218 # Create wrapper ssh with logging. We can't just specify "SSH=ssh -E..."
219 # because sftp and scp don't handle spaces in arguments.
220 SSHLOGWRAP=$OBJ/ssh-log-wrapper.sh
221 echo "#!/bin/sh" > $SSHLOGWRAP
222 echo "exec ${SSH} -E${TEST_SSH_LOGFILE} "'"$@"' >>$SSHLOGWRAP
224 chmod a+rx $OBJ/ssh-log-wrapper.sh
228 # Some test data. We make a copy because some tests will overwrite it.
229 # The tests may assume that $DATA exists and is writable and $COPY does
230 # not exist. Tests requiring larger data files can call increase_datafile_size
231 # [kbytes] to ensure the file is at least that large.
233 DATA=$OBJ/${DATANAME}
234 cat ${SSHAGENT_BIN} >${DATA}
239 increase_datafile_size()
241 while [ `du -k ${DATA} | cut -f1` -lt $1 ]; do
242 cat ${SSHAGENT_BIN} >>${DATA}
246 # these should be used in tests
247 export SSH SSHD SSHAGENT SSHADD SSHKEYGEN SSHKEYSCAN SFTP SFTPSERVER SCP
248 #echo $SSH $SSHD $SSHAGENT $SSHADD $SSHKEYGEN $SSHKEYSCAN $SFTP $SFTPSERVER $SCP
250 # Portable specific functions
257 if [ -x $i/$1 ]; then
267 awk "BEGIN { for (i = $2; i < $2 + $1; i++) { printf \"%d\n\", i } exit }"
270 # Check whether preprocessor symbols are defined in config.h.
274 while test "x$2" != "x" ; do
278 egrep "^#define.*($str)" ${BUILDDIR}/config.h >/dev/null 2>&1
282 if have_prog md5sum; then
284 elif have_prog openssl; then
286 elif have_prog cksum; then
288 elif have_prog sum; then
294 # End of portable specific functions
298 if [ -f $PIDFILE ]; then
299 pid=`$SUDO cat $PIDFILE`
300 if [ "X$pid" = "X" ]; then
303 if [ $pid -lt 2 ]; then
304 echo bad pid for sshd: $pid
307 trace "wait for sshd to exit"
309 while [ -f $PIDFILE -a $i -lt 5 ]; do
313 test -f $PIDFILE && \
314 fatal "sshd didn't exit port $PORT pid $pid"
323 if [ "x$SSH_PID" != "x" ]; then
324 if [ $SSH_PID -lt 2 ]; then
325 echo bad pid for ssh: $SSH_PID
335 echo "trace: $@" >$TEST_REGRESS_LOGFILE
336 echo "trace: $@" >$TEST_SSH_LOGFILE
337 echo "trace: $@" >$TEST_SSHD_LOGFILE
342 echo $@ >>$TEST_REGRESS_LOGFILE
343 echo $@ >>$TEST_SSH_LOGFILE
344 echo $@ >>$TEST_SSHD_LOGFILE
345 (cat $TEST_REGRESS_LOGFILE; echo) >>$OBJ/failed-regress.log
346 (cat $TEST_SSH_LOGFILE; echo) >>$OBJ/failed-ssh.log
347 (cat $TEST_SSHD_LOGFILE; echo) >>$OBJ/failed-sshd.log
353 if [ "X$TEST_SSH_TRACE" = "Xyes" ]; then
361 if [ "X$TEST_SSH_QUIET" != "Xyes" ]; then
368 echo "WARNING: $@" >>$TEST_SSH_LOGFILE
374 save_debug_log "FAIL: $@"
382 save_debug_log "FATAL: $@"
391 echo ${SSH_PROTOCOLS} | grep "$1" >/dev/null
399 if ssh_version 1; then
405 # create server config
406 cat << EOF > $OBJ/sshd_config
410 ListenAddress 127.0.0.1
413 AuthorizedKeysFile $OBJ/authorized_keys_%u
415 AcceptEnv _XXX_TEST_*
417 Subsystem sftp $SFTPSERVER
420 # This may be necessary if /usr/src and/or /usr/obj are group-writable,
421 # but if you aren't careful with permissions then the unit tests could
422 # be abused to locally escalate privileges.
423 if [ ! -z "$TEST_SSH_UNSAFE_PERMISSIONS" ]; then
424 echo "StrictModes no" >> $OBJ/sshd_config
427 if [ ! -z "$TEST_SSH_SSHD_CONFOPTS" ]; then
428 trace "adding sshd_config option $TEST_SSH_SSHD_CONFOPTS"
429 echo "$TEST_SSH_SSHD_CONFOPTS" >> $OBJ/sshd_config
432 # server config for proxy connects
433 cp $OBJ/sshd_config $OBJ/sshd_proxy
435 # allow group-writable directories in proxy-mode
436 echo 'StrictModes no' >> $OBJ/sshd_proxy
438 # create client config
439 cat << EOF > $OBJ/ssh_config
442 HostKeyAlias localhost-with-alias
445 GlobalKnownHostsFile $OBJ/known_hosts
446 UserKnownHostsFile $OBJ/known_hosts
447 PubkeyAuthentication yes
448 ChallengeResponseAuthentication no
449 HostbasedAuthentication no
450 PasswordAuthentication no
452 StrictHostKeyChecking yes
456 if [ ! -z "$TEST_SSH_SSH_CONFOPTS" ]; then
457 trace "adding ssh_config option $TEST_SSH_SSH_CONFOPTS"
458 echo "$TEST_SSH_SSH_CONFOPTS" >> $OBJ/ssh_config
461 rm -f $OBJ/known_hosts $OBJ/authorized_keys_$USER
463 if ssh_version 1; then
464 SSH_KEYTYPES="rsa rsa1"
466 SSH_KEYTYPES="rsa ed25519"
468 trace "generate keys"
469 for t in ${SSH_KEYTYPES}; do
471 if [ ! -f $OBJ/$t ] || [ ${SSHKEYGEN_BIN} -nt $OBJ/$t ]; then
473 ${SSHKEYGEN} -q -N '' -t $t -f $OBJ/$t ||\
474 fail "ssh-keygen for $t failed"
477 # known hosts file for client
479 printf 'localhost-with-alias,127.0.0.1,::1 '
481 ) >> $OBJ/known_hosts
483 # setup authorized keys
484 cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
485 echo IdentityFile $OBJ/$t >> $OBJ/ssh_config
487 # use key as host key, too
488 $SUDO cp $OBJ/$t $OBJ/host.$t
489 echo HostKey $OBJ/host.$t >> $OBJ/sshd_config
491 # don't use SUDO for proxy connect
492 echo HostKey $OBJ/$t >> $OBJ/sshd_proxy
494 chmod 644 $OBJ/authorized_keys_$USER
496 # Activate Twisted Conch tests if the binary is present
497 REGRESS_INTEROP_CONCH=no
498 if test -x "$CONCH" ; then
499 REGRESS_INTEROP_CONCH=yes
502 # If PuTTY is present and we are running a PuTTY test, prepare keys and
504 REGRESS_INTEROP_PUTTY=no
505 if test -x "$PUTTYGEN" -a -x "$PLINK" ; then
506 REGRESS_INTEROP_PUTTY=yes
510 *) REGRESS_INTEROP_PUTTY=no ;;
513 if test "$REGRESS_INTEROP_PUTTY" = "yes" ; then
514 mkdir -p ${OBJ}/.putty
516 # Add a PuTTY key to authorized_keys
517 rm -f ${OBJ}/putty.rsa2
518 if ! puttygen -t rsa -o ${OBJ}/putty.rsa2 \
519 --new-passphrase /dev/null < /dev/null > /dev/null; then
520 echo "Your installed version of PuTTY is too old to support --new-passphrase; trying without (may require manual interaction) ..." >&2
521 puttygen -t rsa -o ${OBJ}/putty.rsa2 < /dev/null > /dev/null
523 puttygen -O public-openssh ${OBJ}/putty.rsa2 \
524 >> $OBJ/authorized_keys_$USER
526 # Convert rsa2 host key to PuTTY format
527 ${SRC}/ssh2putty.sh 127.0.0.1 $PORT $OBJ/rsa > \
528 ${OBJ}/.putty/sshhostkeys
529 ${SRC}/ssh2putty.sh 127.0.0.1 22 $OBJ/rsa >> \
530 ${OBJ}/.putty/sshhostkeys
532 # Setup proxied session
533 mkdir -p ${OBJ}/.putty/sessions
534 rm -f ${OBJ}/.putty/sessions/localhost_proxy
535 echo "Protocol=ssh" >> ${OBJ}/.putty/sessions/localhost_proxy
536 echo "HostName=127.0.0.1" >> ${OBJ}/.putty/sessions/localhost_proxy
537 echo "PortNumber=$PORT" >> ${OBJ}/.putty/sessions/localhost_proxy
538 echo "ProxyMethod=5" >> ${OBJ}/.putty/sessions/localhost_proxy
539 echo "ProxyTelnetCommand=sh ${SRC}/sshd-log-wrapper.sh ${TEST_SSHD_LOGFILE} ${SSHD} -i -f $OBJ/sshd_proxy" >> ${OBJ}/.putty/sessions/localhost_proxy
540 echo "ProxyLocalhost=1" >> ${OBJ}/.putty/sessions/localhost_proxy
542 REGRESS_INTEROP_PUTTY=yes
545 # create a proxy version of the client config
548 echo proxycommand ${SUDO} sh ${SRC}/sshd-log-wrapper.sh ${TEST_SSHD_LOGFILE} ${SSHD} -i -f $OBJ/sshd_proxy
552 ${SSHD} -t -f $OBJ/sshd_proxy || fatal "sshd_proxy broken"
557 $SUDO ${SSHD} -f $OBJ/sshd_config "$@" -t || fatal "sshd_config broken"
558 $SUDO ${SSHD} -f $OBJ/sshd_config "$@" -E$TEST_SSHD_LOGFILE
560 trace "wait for sshd"
562 while [ ! -f $PIDFILE -a $i -lt 10 ]; do
567 test -f $PIDFILE || fatal "no sshd running on port $PORT"
575 if [ $RESULT -eq 0 ]; then