1 # $OpenBSD: test-exec.sh,v 1.53 2016/04/15 02:57:10 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 SSH_PROTOCOLS=`$SSH -Q protocol-version`
134 if [ "x$TEST_SSH_PROTOCOLS" != "x" ]; then
135 SSH_PROTOCOLS="${TEST_SSH_PROTOCOLS}"
138 # Path to sshd must be absolute for rexec
141 *) SSHD=`which $SSHD` ;;
146 *) SSHAGENT=`which $SSHAGENT` ;;
149 # Record the actual binaries used.
152 SSHAGENT_BIN=${SSHAGENT}
154 SSHKEYGEN_BIN=${SSHKEYGEN}
155 SSHKEYSCAN_BIN=${SSHKEYSCAN}
157 SFTPSERVER_BIN=${SFTPSERVER}
160 if [ "x$USE_VALGRIND" != "x" ]; then
161 mkdir -p $OBJ/valgrind-out
162 VG_TEST=`basename $SCRIPT .sh`
164 # Some tests are difficult to fix.
166 connect-privsep|reexec)
170 if [ x"$VG_SKIP" = "x" ]; then
171 VG_IGNORE="/bin/*,/sbin/*,/usr/*,/var/*"
172 VG_LOG="$OBJ/valgrind-out/${VG_TEST}."
173 VG_OPTS="--track-origins=yes --leak-check=full"
174 VG_OPTS="$VG_OPTS --trace-children=yes"
175 VG_OPTS="$VG_OPTS --trace-children-skip=${VG_IGNORE}"
177 if [ "x$VALGRIND_PATH" != "x" ]; then
178 VG_PATH="$VALGRIND_PATH"
180 VG="$VG_PATH $VG_OPTS"
181 SSH="$VG --log-file=${VG_LOG}ssh.%p $SSH"
182 SSHD="$VG --log-file=${VG_LOG}sshd.%p $SSHD"
183 SSHAGENT="$VG --log-file=${VG_LOG}ssh-agent.%p $SSHAGENT"
184 SSHADD="$VG --log-file=${VG_LOG}ssh-add.%p $SSHADD"
185 SSHKEYGEN="$VG --log-file=${VG_LOG}ssh-keygen.%p $SSHKEYGEN"
186 SSHKEYSCAN="$VG --log-file=${VG_LOG}ssh-keyscan.%p $SSHKEYSCAN"
187 SFTP="$VG --log-file=${VG_LOG}sftp.%p ${SFTP}"
188 SCP="$VG --log-file=${VG_LOG}scp.%p $SCP"
189 cat > $OBJ/valgrind-sftp-server.sh << EOF
191 exec $VG --log-file=${VG_LOG}sftp-server.%p $SFTPSERVER "\$@"
193 chmod a+rx $OBJ/valgrind-sftp-server.sh
194 SFTPSERVER="$OBJ/valgrind-sftp-server.sh"
199 # SSH_LOGFILE should be the debug output of ssh(1) only
200 # SSHD_LOGFILE should be the debug output of sshd(8) only
201 # REGRESS_LOGFILE is the output of the test itself stdout and stderr
202 if [ "x$TEST_SSH_LOGFILE" = "x" ]; then
203 TEST_SSH_LOGFILE=$OBJ/ssh.log
205 if [ "x$TEST_SSHD_LOGFILE" = "x" ]; then
206 TEST_SSHD_LOGFILE=$OBJ/sshd.log
208 if [ "x$TEST_REGRESS_LOGFILE" = "x" ]; then
209 TEST_REGRESS_LOGFILE=$OBJ/regress.log
215 >$TEST_REGRESS_LOGFILE
217 # Create wrapper ssh with logging. We can't just specify "SSH=ssh -E..."
218 # because sftp and scp don't handle spaces in arguments.
219 SSHLOGWRAP=$OBJ/ssh-log-wrapper.sh
220 echo "#!/bin/sh" > $SSHLOGWRAP
221 echo "exec ${SSH} -E${TEST_SSH_LOGFILE} "'"$@"' >>$SSHLOGWRAP
223 chmod a+rx $OBJ/ssh-log-wrapper.sh
227 # Some test data. We make a copy because some tests will overwrite it.
228 # The tests may assume that $DATA exists and is writable and $COPY does
229 # not exist. Tests requiring larger data files can call increase_datafile_size
230 # [kbytes] to ensure the file is at least that large.
232 DATA=$OBJ/${DATANAME}
233 cat ${SSHAGENT_BIN} >${DATA}
238 increase_datafile_size()
240 while [ `du -k ${DATA} | cut -f1` -lt $1 ]; do
241 cat ${SSHAGENT_BIN} >>${DATA}
245 # these should be used in tests
246 export SSH SSHD SSHAGENT SSHADD SSHKEYGEN SSHKEYSCAN SFTP SFTPSERVER SCP
247 #echo $SSH $SSHD $SSHAGENT $SSHADD $SSHKEYGEN $SSHKEYSCAN $SFTP $SFTPSERVER $SCP
249 # Portable specific functions
256 if [ -x $i/$1 ]; then
266 awk "BEGIN { for (i = $2; i < $2 + $1; i++) { printf \"%d\n\", i } exit }"
269 # Check whether preprocessor symbols are defined in config.h.
273 while test "x$2" != "x" ; do
277 egrep "^#define.*($str)" ${BUILDDIR}/config.h >/dev/null 2>&1
281 if have_prog md5sum; then
283 elif have_prog openssl; then
285 elif have_prog cksum; then
287 elif have_prog sum; then
293 # End of portable specific functions
298 if [ "x$SSH_PID" != "x" ]; then
299 if [ $SSH_PID -lt 2 ]; then
300 echo bad pid for ssh: $SSH_PID
305 if [ -f $PIDFILE ]; then
306 pid=`$SUDO cat $PIDFILE`
307 if [ "X$pid" = "X" ]; then
310 if [ $pid -lt 2 ]; then
311 echo bad pid for sshd: $pid
314 trace "wait for sshd to exit"
316 while [ -f $PIDFILE -a $i -lt 5 ]; do
320 test -f $PIDFILE && \
321 fatal "sshd didn't exit port $PORT pid $pid"
329 echo "trace: $@" >$TEST_REGRESS_LOGFILE
330 echo "trace: $@" >$TEST_SSH_LOGFILE
331 echo "trace: $@" >$TEST_SSHD_LOGFILE
336 echo $@ >>$TEST_REGRESS_LOGFILE
337 echo $@ >>$TEST_SSH_LOGFILE
338 echo $@ >>$TEST_SSHD_LOGFILE
339 (cat $TEST_REGRESS_LOGFILE; echo) >>$OBJ/failed-regress.log
340 (cat $TEST_SSH_LOGFILE; echo) >>$OBJ/failed-ssh.log
341 (cat $TEST_SSHD_LOGFILE; echo) >>$OBJ/failed-sshd.log
347 if [ "X$TEST_SSH_TRACE" = "Xyes" ]; then
355 if [ "X$TEST_SSH_QUIET" != "Xyes" ]; then
362 echo "WARNING: $@" >>$TEST_SSH_LOGFILE
368 save_debug_log "FAIL: $@"
376 save_debug_log "FATAL: $@"
385 echo ${SSH_PROTOCOLS} | grep "$1" >/dev/null
393 if ssh_version 1; then
399 # create server config
400 cat << EOF > $OBJ/sshd_config
405 ListenAddress 127.0.0.1
408 AuthorizedKeysFile $OBJ/authorized_keys_%u
410 AcceptEnv _XXX_TEST_*
412 Subsystem sftp $SFTPSERVER
415 # This may be necessary if /usr/src and/or /usr/obj are group-writable,
416 # but if you aren't careful with permissions then the unit tests could
417 # be abused to locally escalate privileges.
418 if [ ! -z "$TEST_SSH_UNSAFE_PERMISSIONS" ]; then
419 echo "StrictModes no" >> $OBJ/sshd_config
422 if [ ! -z "$TEST_SSH_SSHD_CONFOPTS" ]; then
423 trace "adding sshd_config option $TEST_SSH_SSHD_CONFOPTS"
424 echo "$TEST_SSH_SSHD_CONFOPTS" >> $OBJ/sshd_config
427 # server config for proxy connects
428 cp $OBJ/sshd_config $OBJ/sshd_proxy
430 # allow group-writable directories in proxy-mode
431 echo 'StrictModes no' >> $OBJ/sshd_proxy
433 # create client config
434 cat << EOF > $OBJ/ssh_config
438 HostKeyAlias localhost-with-alias
441 GlobalKnownHostsFile $OBJ/known_hosts
442 UserKnownHostsFile $OBJ/known_hosts
443 RSAAuthentication yes
444 PubkeyAuthentication yes
445 ChallengeResponseAuthentication no
446 HostbasedAuthentication no
447 PasswordAuthentication no
448 RhostsRSAAuthentication no
450 StrictHostKeyChecking yes
454 if [ ! -z "$TEST_SSH_SSH_CONFOPTS" ]; then
455 trace "adding ssh_config option $TEST_SSH_SSH_CONFOPTS"
456 echo "$TEST_SSH_SSH_CONFOPTS" >> $OBJ/ssh_config
459 rm -f $OBJ/known_hosts $OBJ/authorized_keys_$USER
461 if ssh_version 1; then
462 SSH_KEYTYPES="rsa rsa1"
464 SSH_KEYTYPES="rsa ed25519"
466 trace "generate keys"
467 for t in ${SSH_KEYTYPES}; do
469 if [ ! -f $OBJ/$t ] || [ ${SSHKEYGEN_BIN} -nt $OBJ/$t ]; then
471 ${SSHKEYGEN} -q -N '' -t $t -f $OBJ/$t ||\
472 fail "ssh-keygen for $t failed"
475 # known hosts file for client
477 printf 'localhost-with-alias,127.0.0.1,::1 '
479 ) >> $OBJ/known_hosts
481 # setup authorized keys
482 cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
483 echo IdentityFile $OBJ/$t >> $OBJ/ssh_config
485 # use key as host key, too
486 $SUDO cp $OBJ/$t $OBJ/host.$t
487 echo HostKey $OBJ/host.$t >> $OBJ/sshd_config
489 # don't use SUDO for proxy connect
490 echo HostKey $OBJ/$t >> $OBJ/sshd_proxy
492 chmod 644 $OBJ/authorized_keys_$USER
494 # Activate Twisted Conch tests if the binary is present
495 REGRESS_INTEROP_CONCH=no
496 if test -x "$CONCH" ; then
497 REGRESS_INTEROP_CONCH=yes
500 # If PuTTY is present and we are running a PuTTY test, prepare keys and
502 REGRESS_INTEROP_PUTTY=no
503 if test -x "$PUTTYGEN" -a -x "$PLINK" ; then
504 REGRESS_INTEROP_PUTTY=yes
508 *) REGRESS_INTEROP_PUTTY=no ;;
511 if test "$REGRESS_INTEROP_PUTTY" = "yes" ; then
512 mkdir -p ${OBJ}/.putty
514 # Add a PuTTY key to authorized_keys
515 rm -f ${OBJ}/putty.rsa2
516 puttygen -t rsa -o ${OBJ}/putty.rsa2 < /dev/null > /dev/null
517 puttygen -O public-openssh ${OBJ}/putty.rsa2 \
518 >> $OBJ/authorized_keys_$USER
520 # Convert rsa2 host key to PuTTY format
521 ${SRC}/ssh2putty.sh 127.0.0.1 $PORT $OBJ/rsa > \
522 ${OBJ}/.putty/sshhostkeys
523 ${SRC}/ssh2putty.sh 127.0.0.1 22 $OBJ/rsa >> \
524 ${OBJ}/.putty/sshhostkeys
526 # Setup proxied session
527 mkdir -p ${OBJ}/.putty/sessions
528 rm -f ${OBJ}/.putty/sessions/localhost_proxy
529 echo "Hostname=127.0.0.1" >> ${OBJ}/.putty/sessions/localhost_proxy
530 echo "PortNumber=$PORT" >> ${OBJ}/.putty/sessions/localhost_proxy
531 echo "ProxyMethod=5" >> ${OBJ}/.putty/sessions/localhost_proxy
532 echo "ProxyTelnetCommand=sh ${SRC}/sshd-log-wrapper.sh ${TEST_SSHD_LOGFILE} ${SSHD} -i -f $OBJ/sshd_proxy" >> ${OBJ}/.putty/sessions/localhost_proxy
534 REGRESS_INTEROP_PUTTY=yes
537 # create a proxy version of the client config
540 echo proxycommand ${SUDO} sh ${SRC}/sshd-log-wrapper.sh ${TEST_SSHD_LOGFILE} ${SSHD} -i -f $OBJ/sshd_proxy
544 ${SSHD} -t -f $OBJ/sshd_proxy || fatal "sshd_proxy broken"
549 $SUDO ${SSHD} -f $OBJ/sshd_config "$@" -t || fatal "sshd_config broken"
550 $SUDO ${SSHD} -f $OBJ/sshd_config "$@" -E$TEST_SSHD_LOGFILE
552 trace "wait for sshd"
554 while [ ! -f $PIDFILE -a $i -lt 10 ]; do
559 test -f $PIDFILE || fatal "no sshd running on port $PORT"
567 if [ $RESULT -eq 0 ]; then