1# $OpenBSD: test-exec.sh,v 1.37 2010/02/24 06:21:56 djm Exp $ 2# Placed in the Public Domain. 3 4#SUDO=sudo 5 6# Unbreak GNU head(1) 7_POSIX2_VERSION=199209 8export _POSIX2_VERSION 9 10case `uname -s 2>/dev/null` in 11OSF1*) 12 BIN_SH=xpg4 13 export BIN_SH 14 ;; 15esac 16 17if [ ! -z "$TEST_SSH_PORT" ]; then 18 PORT="$TEST_SSH_PORT" 19else 20 PORT=4242 21fi 22 23if [ -x /usr/ucb/whoami ]; then 24 USER=`/usr/ucb/whoami` 25elif whoami >/dev/null 2>&1; then 26 USER=`whoami` 27elif logname >/dev/null 2>&1; then 28 USER=`logname` 29else 30 USER=`id -un` 31fi 32 33OBJ=$1 34if [ "x$OBJ" = "x" ]; then 35 echo '$OBJ not defined' 36 exit 2 37fi 38if [ ! -d $OBJ ]; then 39 echo "not a directory: $OBJ" 40 exit 2 41fi 42SCRIPT=$2 43if [ "x$SCRIPT" = "x" ]; then 44 echo '$SCRIPT not defined' 45 exit 2 46fi 47if [ ! -f $SCRIPT ]; then 48 echo "not a file: $SCRIPT" 49 exit 2 50fi 51if $TEST_SHELL -n $SCRIPT; then 52 true 53else 54 echo "syntax error in $SCRIPT" 55 exit 2 56fi 57unset SSH_AUTH_SOCK 58 59SRC=`dirname ${SCRIPT}` 60 61# defaults 62SSH=ssh 63SSHD=sshd 64SSHAGENT=ssh-agent 65SSHADD=ssh-add 66SSHKEYGEN=ssh-keygen 67SSHKEYSCAN=ssh-keyscan 68SFTP=sftp 69SFTPSERVER=/usr/libexec/openssh/sftp-server 70SCP=scp 71 72# Interop testing 73PLINK=plink 74PUTTYGEN=puttygen 75CONCH=conch 76 77if [ "x$TEST_SSH_SSH" != "x" ]; then 78 SSH="${TEST_SSH_SSH}" 79fi 80if [ "x$TEST_SSH_SSHD" != "x" ]; then 81 SSHD="${TEST_SSH_SSHD}" 82fi 83if [ "x$TEST_SSH_SSHAGENT" != "x" ]; then 84 SSHAGENT="${TEST_SSH_SSHAGENT}" 85fi 86if [ "x$TEST_SSH_SSHADD" != "x" ]; then 87 SSHADD="${TEST_SSH_SSHADD}" 88fi 89if [ "x$TEST_SSH_SSHKEYGEN" != "x" ]; then 90 SSHKEYGEN="${TEST_SSH_SSHKEYGEN}" 91fi 92if [ "x$TEST_SSH_SSHKEYSCAN" != "x" ]; then 93 SSHKEYSCAN="${TEST_SSH_SSHKEYSCAN}" 94fi 95if [ "x$TEST_SSH_SFTP" != "x" ]; then 96 SFTP="${TEST_SSH_SFTP}" 97fi 98if [ "x$TEST_SSH_SFTPSERVER" != "x" ]; then 99 SFTPSERVER="${TEST_SSH_SFTPSERVER}" 100fi 101if [ "x$TEST_SSH_SCP" != "x" ]; then 102 SCP="${TEST_SSH_SCP}" 103fi 104if [ "x$TEST_SSH_PLINK" != "x" ]; then 105 # Find real binary, if it exists 106 case "${TEST_SSH_PLINK}" in 107 /*) PLINK="${TEST_SSH_PLINK}" ;; 108 *) PLINK=`which ${TEST_SSH_PLINK} 2>/dev/null` ;; 109 esac 110fi 111if [ "x$TEST_SSH_PUTTYGEN" != "x" ]; then 112 # Find real binary, if it exists 113 case "${TEST_SSH_PUTTYGEN}" in 114 /*) PUTTYGEN="${TEST_SSH_PUTTYGEN}" ;; 115 *) PUTTYGEN=`which ${TEST_SSH_PUTTYGEN} 2>/dev/null` ;; 116 esac 117fi 118if [ "x$TEST_SSH_CONCH" != "x" ]; then 119 # Find real binary, if it exists 120 case "${TEST_SSH_CONCH}" in 121 /*) CONCH="${TEST_SSH_CONCH}" ;; 122 *) CONCH=`which ${TEST_SSH_CONCH} 2>/dev/null` ;; 123 esac 124fi 125 126# Path to sshd must be absolute for rexec 127case "$SSHD" in 128/*) ;; 129*) SSHD=`which sshd` ;; 130esac 131 132if [ "x$TEST_SSH_LOGFILE" = "x" ]; then 133 TEST_SSH_LOGFILE=/dev/null 134fi 135 136# these should be used in tests 137export SSH SSHD SSHAGENT SSHADD SSHKEYGEN SSHKEYSCAN SFTP SFTPSERVER SCP 138#echo $SSH $SSHD $SSHAGENT $SSHADD $SSHKEYGEN $SSHKEYSCAN $SFTP $SFTPSERVER $SCP 139 140# helper 141echon() 142{ 143 if [ "x`echo -n`" = "x" ]; then 144 echo -n "$@" 145 elif [ "x`echo '\c'`" = "x" ]; then 146 echo "$@\c" 147 else 148 fatal "Don't know how to echo without newline." 149 fi 150} 151 152have_prog() 153{ 154 saved_IFS="$IFS" 155 IFS=":" 156 for i in $PATH 157 do 158 if [ -x $i/$1 ]; then 159 IFS="$saved_IFS" 160 return 0 161 fi 162 done 163 IFS="$saved_IFS" 164 return 1 165} 166 167cleanup () 168{ 169 if [ -f $PIDFILE ]; then 170 pid=`$SUDO cat $PIDFILE` 171 if [ "X$pid" = "X" ]; then 172 echo no sshd running 173 else 174 if [ $pid -lt 2 ]; then 175 echo bad pid for ssh: $pid 176 else 177 $SUDO kill $pid 178 trace "wait for sshd to exit" 179 i=0; 180 while [ -f $PIDFILE -a $i -lt 5 ]; do 181 i=`expr $i + 1` 182 sleep $i 183 done 184 test -f $PIDFILE && \ 185 fatal "sshd didn't exit port $PORT pid $pid" 186 fi 187 fi 188 fi 189} 190 191trace () 192{ 193 echo "trace: $@" >>$TEST_SSH_LOGFILE 194 if [ "X$TEST_SSH_TRACE" = "Xyes" ]; then 195 echo "$@" 196 fi 197} 198 199verbose () 200{ 201 echo "verbose: $@" >>$TEST_SSH_LOGFILE 202 if [ "X$TEST_SSH_QUIET" != "Xyes" ]; then 203 echo "$@" 204 fi 205} 206 207warn () 208{ 209 echo "WARNING: $@" >>$TEST_SSH_LOGFILE 210 echo "WARNING: $@" 211} 212 213fail () 214{ 215 echo "FAIL: $@" >>$TEST_SSH_LOGFILE 216 RESULT=1 217 echo "$@" 218} 219 220fatal () 221{ 222 echo "FATAL: $@" >>$TEST_SSH_LOGFILE 223 echon "FATAL: " 224 fail "$@" 225 cleanup 226 exit $RESULT 227} 228 229# Check whether preprocessor symbols are defined in config.h. 230config_defined () 231{ 232 str=$1 233 while test "x$2" != "x" ; do 234 str="$str|$2" 235 shift 236 done 237 egrep "^#define.*($str)" ${BUILDDIR}/config.h >/dev/null 2>&1 238} 239 240RESULT=0 241PIDFILE=$OBJ/pidfile 242 243trap fatal 3 2 244 245# create server config 246cat << EOF > $OBJ/sshd_config 247 StrictModes no 248 Port $PORT 249 Protocol 2,1 250 AddressFamily inet 251 ListenAddress 127.0.0.1 252 #ListenAddress ::1 253 PidFile $PIDFILE 254 AuthorizedKeysFile $OBJ/authorized_keys_%u 255 LogLevel VERBOSE 256 AcceptEnv _XXX_TEST_* 257 AcceptEnv _XXX_TEST 258 Subsystem sftp $SFTPSERVER 259EOF 260 261if [ ! -z "$TEST_SSH_SSHD_CONFOPTS" ]; then 262 trace "adding sshd_config option $TEST_SSH_SSHD_CONFOPTS" 263 echo "$TEST_SSH_SSHD_CONFOPTS" >> $OBJ/sshd_config 264fi 265 266# server config for proxy connects 267cp $OBJ/sshd_config $OBJ/sshd_proxy 268 269# allow group-writable directories in proxy-mode 270echo 'StrictModes no' >> $OBJ/sshd_proxy 271 272# create client config 273cat << EOF > $OBJ/ssh_config 274Host * 275 Protocol 2,1 276 Hostname 127.0.0.1 277 HostKeyAlias localhost-with-alias 278 Port $PORT 279 User $USER 280 GlobalKnownHostsFile $OBJ/known_hosts 281 UserKnownHostsFile $OBJ/known_hosts 282 RSAAuthentication yes 283 PubkeyAuthentication yes 284 ChallengeResponseAuthentication no 285 HostbasedAuthentication no 286 PasswordAuthentication no 287 BatchMode yes 288 StrictHostKeyChecking yes 289EOF 290 291if [ ! -z "$TEST_SSH_SSH_CONFOPTS" ]; then 292 trace "adding ssh_config option $TEST_SSH_SSHD_CONFOPTS" 293 echo "$TEST_SSH_SSH_CONFOPTS" >> $OBJ/ssh_config 294fi 295 296rm -f $OBJ/known_hosts $OBJ/authorized_keys_$USER 297 298trace "generate keys" 299for t in rsa rsa1; do 300 # generate user key 301 rm -f $OBJ/$t 302 ${SSHKEYGEN} -b 1024 -q -N '' -t $t -f $OBJ/$t ||\ 303 fail "ssh-keygen for $t failed" 304 305 # known hosts file for client 306 ( 307 echon 'localhost-with-alias,127.0.0.1,::1 ' 308 cat $OBJ/$t.pub 309 ) >> $OBJ/known_hosts 310 311 # setup authorized keys 312 cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER 313 echo IdentityFile $OBJ/$t >> $OBJ/ssh_config 314 315 # use key as host key, too 316 $SUDO cp $OBJ/$t $OBJ/host.$t 317 echo HostKey $OBJ/host.$t >> $OBJ/sshd_config 318 319 # don't use SUDO for proxy connect 320 echo HostKey $OBJ/$t >> $OBJ/sshd_proxy 321done 322chmod 644 $OBJ/authorized_keys_$USER 323 324# Activate Twisted Conch tests if the binary is present 325REGRESS_INTEROP_CONCH=no 326if test -x "$CONCH" ; then 327 REGRESS_INTEROP_CONCH=yes 328fi 329 330# If PuTTY is present and we are running a PuTTY test, prepare keys and 331# configuration 332REGRESS_INTEROP_PUTTY=no 333if test -x "$PUTTYGEN" -a -x "$PLINK" ; then 334 REGRESS_INTEROP_PUTTY=yes 335fi 336case "$SCRIPT" in 337*putty*) ;; 338*) REGRESS_INTEROP_PUTTY=no ;; 339esac 340 341if test "$REGRESS_INTEROP_PUTTY" = "yes" ; then 342 mkdir -p ${OBJ}/.putty 343 344 # Add a PuTTY key to authorized_keys 345 rm -f ${OBJ}/putty.rsa2 346 puttygen -t rsa -o ${OBJ}/putty.rsa2 < /dev/null > /dev/null 347 puttygen -O public-openssh ${OBJ}/putty.rsa2 \ 348 >> $OBJ/authorized_keys_$USER 349 350 # Convert rsa2 host key to PuTTY format 351 ${SRC}/ssh2putty.sh 127.0.0.1 $PORT $OBJ/rsa > \ 352 ${OBJ}/.putty/sshhostkeys 353 ${SRC}/ssh2putty.sh 127.0.0.1 22 $OBJ/rsa >> \ 354 ${OBJ}/.putty/sshhostkeys 355 356 # Setup proxied session 357 mkdir -p ${OBJ}/.putty/sessions 358 rm -f ${OBJ}/.putty/sessions/localhost_proxy 359 echo "Hostname=127.0.0.1" >> ${OBJ}/.putty/sessions/localhost_proxy 360 echo "PortNumber=$PORT" >> ${OBJ}/.putty/sessions/localhost_proxy 361 echo "ProxyMethod=5" >> ${OBJ}/.putty/sessions/localhost_proxy 362 echo "ProxyTelnetCommand=sh ${SRC}/sshd-log-wrapper.sh ${SSHD} ${TEST_SSH_LOGFILE} -i -f $OBJ/sshd_proxy" >> ${OBJ}/.putty/sessions/localhost_proxy 363 364 REGRESS_INTEROP_PUTTY=yes 365fi 366 367# create a proxy version of the client config 368( 369 cat $OBJ/ssh_config 370 echo proxycommand ${SUDO} sh ${SRC}/sshd-log-wrapper.sh ${SSHD} ${TEST_SSH_LOGFILE} -i -f $OBJ/sshd_proxy 371) > $OBJ/ssh_proxy 372 373# check proxy config 374${SSHD} -t -f $OBJ/sshd_proxy || fatal "sshd_proxy broken" 375 376start_sshd () 377{ 378 # start sshd 379 $SUDO ${SSHD} -f $OBJ/sshd_config "$@" -t || fatal "sshd_config broken" 380 $SUDO ${SSHD} -f $OBJ/sshd_config -e "$@" >>$TEST_SSH_LOGFILE 2>&1 381 382 trace "wait for sshd" 383 i=0; 384 while [ ! -f $PIDFILE -a $i -lt 10 ]; do 385 i=`expr $i + 1` 386 sleep $i 387 done 388 389 test -f $PIDFILE || fatal "no sshd running on port $PORT" 390} 391 392# source test body 393. $SCRIPT 394 395# kill sshd 396cleanup 397if [ $RESULT -eq 0 ]; then 398 verbose ok $tid 399else 400 echo failed $tid 401fi 402exit $RESULT 403