test-exec.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. #!/usr/bin/env sh
  2. # $OpenBSD: test-exec.sh,v 1.76 2020/04/04 23:04:41 dtucker Exp $
  3. # Placed in the Public Domain.
  4. #SUDO=sudo
  5. # Unbreak GNU head(1)
  6. _POSIX2_VERSION=199209
  7. export _POSIX2_VERSION
  8. case `uname -s 2>/dev/null` in
  9. OSF1*)
  10. BIN_SH=xpg4
  11. export BIN_SH
  12. ;;
  13. CYGWIN*)
  14. os=cygwin
  15. ;;
  16. esac
  17. if [ ! -z "$TEST_SSH_PORT" ]; then
  18. PORT="$TEST_SSH_PORT"
  19. else
  20. PORT=4242
  21. fi
  22. # If configure tells us to use a different egrep, create a wrapper function
  23. # to call it. This means we don't need to change all the tests that depend
  24. # on a good implementation.
  25. if test "x${EGREP}" != "x"; then
  26. egrep ()
  27. {
  28. ${EGREP} "$@"
  29. }
  30. fi
  31. if [ -x /usr/ucb/whoami ]; then
  32. USER=`/usr/ucb/whoami`
  33. elif whoami >/dev/null 2>&1; then
  34. USER=`whoami`
  35. elif logname >/dev/null 2>&1; then
  36. USER=`logname`
  37. else
  38. USER=`id -un`
  39. fi
  40. if test -z "$LOGNAME"; then
  41. LOGNAME="${USER}"
  42. export LOGNAME
  43. fi
  44. OBJ=$1
  45. if [ "x$OBJ" = "x" ]; then
  46. echo '$OBJ not defined'
  47. exit 2
  48. fi
  49. if [ ! -d $OBJ ]; then
  50. echo "not a directory: $OBJ"
  51. exit 2
  52. fi
  53. SCRIPT=$2
  54. if [ "x$SCRIPT" = "x" ]; then
  55. echo '$SCRIPT not defined'
  56. exit 2
  57. fi
  58. if [ ! -f $SCRIPT ]; then
  59. echo "not a file: $SCRIPT"
  60. exit 2
  61. fi
  62. if $TEST_SHELL -n $SCRIPT; then
  63. true
  64. else
  65. echo "syntax error in $SCRIPT"
  66. exit 2
  67. fi
  68. unset SSH_AUTH_SOCK
  69. SRC=`dirname ${SCRIPT}`
  70. # defaults
  71. SSH=ssh
  72. SSHD=sshd
  73. SSHAGENT=ssh-agent
  74. SSHADD=ssh-add
  75. SSHKEYGEN=ssh-keygen
  76. SSHKEYSCAN=ssh-keyscan
  77. SFTP=sftp
  78. SFTPSERVER=/usr/libexec/openssh/sftp-server
  79. SCP=scp
  80. # Set by make_tmpdir() on demand (below).
  81. SSH_REGRESS_TMP=
  82. # Interop testing
  83. PLINK=plink
  84. PUTTYGEN=puttygen
  85. CONCH=conch
  86. # Tools used by multiple tests
  87. NC=$OBJ/netcat
  88. if [ "x$TEST_SSH_SSH" != "x" ]; then
  89. SSH="${TEST_SSH_SSH}"
  90. fi
  91. if [ "x$TEST_SSH_SSHD" != "x" ]; then
  92. SSHD="${TEST_SSH_SSHD}"
  93. fi
  94. if [ "x$TEST_SSH_SSHAGENT" != "x" ]; then
  95. SSHAGENT="${TEST_SSH_SSHAGENT}"
  96. fi
  97. if [ "x$TEST_SSH_SSHADD" != "x" ]; then
  98. SSHADD="${TEST_SSH_SSHADD}"
  99. fi
  100. if [ "x$TEST_SSH_SSHKEYGEN" != "x" ]; then
  101. SSHKEYGEN="${TEST_SSH_SSHKEYGEN}"
  102. fi
  103. if [ "x$TEST_SSH_SSHKEYSCAN" != "x" ]; then
  104. SSHKEYSCAN="${TEST_SSH_SSHKEYSCAN}"
  105. fi
  106. if [ "x$TEST_SSH_SFTP" != "x" ]; then
  107. SFTP="${TEST_SSH_SFTP}"
  108. fi
  109. if [ "x$TEST_SSH_SFTPSERVER" != "x" ]; then
  110. SFTPSERVER="${TEST_SSH_SFTPSERVER}"
  111. fi
  112. if [ "x$TEST_SSH_SCP" != "x" ]; then
  113. SCP="${TEST_SSH_SCP}"
  114. fi
  115. if [ "x$TEST_SSH_PLINK" != "x" ]; then
  116. # Find real binary, if it exists
  117. case "${TEST_SSH_PLINK}" in
  118. /*) PLINK="${TEST_SSH_PLINK}" ;;
  119. *) PLINK=`which ${TEST_SSH_PLINK} 2>/dev/null` ;;
  120. esac
  121. fi
  122. if [ "x$TEST_SSH_PUTTYGEN" != "x" ]; then
  123. # Find real binary, if it exists
  124. case "${TEST_SSH_PUTTYGEN}" in
  125. /*) PUTTYGEN="${TEST_SSH_PUTTYGEN}" ;;
  126. *) PUTTYGEN=`which ${TEST_SSH_PUTTYGEN} 2>/dev/null` ;;
  127. esac
  128. fi
  129. if [ "x$TEST_SSH_CONCH" != "x" ]; then
  130. # Find real binary, if it exists
  131. case "${TEST_SSH_CONCH}" in
  132. /*) CONCH="${TEST_SSH_CONCH}" ;;
  133. *) CONCH=`which ${TEST_SSH_CONCH} 2>/dev/null` ;;
  134. esac
  135. fi
  136. if [ "x$TEST_SSH_PKCS11_HELPER" != "x" ]; then
  137. SSH_PKCS11_HELPER="${TEST_SSH_PKCS11_HELPER}"
  138. fi
  139. if [ "x$TEST_SSH_SK_HELPER" != "x" ]; then
  140. SSH_SK_HELPER="${TEST_SSH_SK_HELPER}"
  141. fi
  142. # Path to sshd must be absolute for rexec
  143. case "$SSHD" in
  144. /*) ;;
  145. *) SSHD=`which $SSHD` ;;
  146. esac
  147. case "$SSHAGENT" in
  148. /*) ;;
  149. *) SSHAGENT=`which $SSHAGENT` ;;
  150. esac
  151. # Record the actual binaries used.
  152. SSH_BIN=${SSH}
  153. SSHD_BIN=${SSHD}
  154. SSHAGENT_BIN=${SSHAGENT}
  155. SSHADD_BIN=${SSHADD}
  156. SSHKEYGEN_BIN=${SSHKEYGEN}
  157. SSHKEYSCAN_BIN=${SSHKEYSCAN}
  158. SFTP_BIN=${SFTP}
  159. SFTPSERVER_BIN=${SFTPSERVER}
  160. SCP_BIN=${SCP}
  161. if [ "x$USE_VALGRIND" != "x" ]; then
  162. rm -rf $OBJ/valgrind-out $OBJ/valgrind-vgdb
  163. mkdir -p $OBJ/valgrind-out $OBJ/valgrind-vgdb
  164. # When using sudo ensure low-priv tests can write pipes and logs.
  165. if [ "x$SUDO" != "x" ]; then
  166. chmod 777 $OBJ/valgrind-out $OBJ/valgrind-vgdb
  167. fi
  168. VG_TEST=`basename $SCRIPT .sh`
  169. # Some tests are difficult to fix.
  170. case "$VG_TEST" in
  171. reexec)
  172. VG_SKIP=1 ;;
  173. sftp-chroot)
  174. if [ "x${SUDO}" != "x" ]; then
  175. VG_SKIP=1
  176. fi ;;
  177. esac
  178. if [ x"$VG_SKIP" = "x" ]; then
  179. VG_LEAK="--leak-check=no"
  180. if [ x"$VALGRIND_CHECK_LEAKS" != "x" ]; then
  181. VG_LEAK="--leak-check=full"
  182. fi
  183. VG_IGNORE="/bin/*,/sbin/*,/usr/*,/var/*"
  184. VG_LOG="$OBJ/valgrind-out/${VG_TEST}."
  185. VG_OPTS="--track-origins=yes $VG_LEAK"
  186. VG_OPTS="$VG_OPTS --trace-children=yes"
  187. VG_OPTS="$VG_OPTS --trace-children-skip=${VG_IGNORE}"
  188. VG_OPTS="$VG_OPTS --vgdb-prefix=$OBJ/valgrind-vgdb/"
  189. VG_PATH="valgrind"
  190. if [ "x$VALGRIND_PATH" != "x" ]; then
  191. VG_PATH="$VALGRIND_PATH"
  192. fi
  193. VG="$VG_PATH $VG_OPTS"
  194. SSH="$VG --log-file=${VG_LOG}ssh.%p $SSH"
  195. SSHD="$VG --log-file=${VG_LOG}sshd.%p $SSHD"
  196. SSHAGENT="$VG --log-file=${VG_LOG}ssh-agent.%p $SSHAGENT"
  197. SSHADD="$VG --log-file=${VG_LOG}ssh-add.%p $SSHADD"
  198. SSHKEYGEN="$VG --log-file=${VG_LOG}ssh-keygen.%p $SSHKEYGEN"
  199. SSHKEYSCAN="$VG --log-file=${VG_LOG}ssh-keyscan.%p $SSHKEYSCAN"
  200. SFTP="$VG --log-file=${VG_LOG}sftp.%p ${SFTP}"
  201. SCP="$VG --log-file=${VG_LOG}scp.%p $SCP"
  202. cat > $OBJ/valgrind-sftp-server.sh << EOF
  203. #!/bin/sh
  204. exec $VG --log-file=${VG_LOG}sftp-server.%p $SFTPSERVER "\$@"
  205. EOF
  206. chmod a+rx $OBJ/valgrind-sftp-server.sh
  207. SFTPSERVER="$OBJ/valgrind-sftp-server.sh"
  208. fi
  209. fi
  210. # Logfiles.
  211. # SSH_LOGFILE should be the debug output of ssh(1) only
  212. # SSHD_LOGFILE should be the debug output of sshd(8) only
  213. # REGRESS_LOGFILE is the output of the test itself stdout and stderr
  214. if [ "x$TEST_SSH_LOGFILE" = "x" ]; then
  215. TEST_SSH_LOGFILE=$OBJ/ssh.log
  216. fi
  217. if [ "x$TEST_SSHD_LOGFILE" = "x" ]; then
  218. TEST_SSHD_LOGFILE=$OBJ/sshd.log
  219. fi
  220. if [ "x$TEST_REGRESS_LOGFILE" = "x" ]; then
  221. TEST_REGRESS_LOGFILE=$OBJ/regress.log
  222. fi
  223. # truncate logfiles
  224. >$TEST_SSH_LOGFILE
  225. >$TEST_SSHD_LOGFILE
  226. >$TEST_REGRESS_LOGFILE
  227. # Create wrapper ssh with logging. We can't just specify "SSH=ssh -E..."
  228. # because sftp and scp don't handle spaces in arguments.
  229. SSHLOGWRAP=$OBJ/ssh-log-wrapper.sh
  230. echo "#!/bin/sh" > $SSHLOGWRAP
  231. echo "exec ${SSH} -E${TEST_SSH_LOGFILE} "'"$@"' >>$SSHLOGWRAP
  232. chmod a+rx $OBJ/ssh-log-wrapper.sh
  233. REAL_SSH="$SSH"
  234. REAL_SSHD="$SSHD"
  235. SSH="$SSHLOGWRAP"
  236. # Some test data. We make a copy because some tests will overwrite it.
  237. # The tests may assume that $DATA exists and is writable and $COPY does
  238. # not exist. Tests requiring larger data files can call increase_datafile_size
  239. # [kbytes] to ensure the file is at least that large.
  240. DATANAME=data
  241. DATA=$OBJ/${DATANAME}
  242. cat ${SSHAGENT_BIN} >${DATA}
  243. chmod u+w ${DATA}
  244. COPY=$OBJ/copy
  245. rm -f ${COPY}
  246. increase_datafile_size()
  247. {
  248. while [ `du -k ${DATA} | cut -f1` -lt $1 ]; do
  249. cat ${SSHAGENT_BIN} >>${DATA}
  250. done
  251. }
  252. # these should be used in tests
  253. export SSH SSHD SSHAGENT SSHADD SSHKEYGEN SSHKEYSCAN SFTP SFTPSERVER SCP
  254. export SSH_PKCS11_HELPER SSH_SK_HELPER
  255. #echo $SSH $SSHD $SSHAGENT $SSHADD $SSHKEYGEN $SSHKEYSCAN $SFTP $SFTPSERVER $SCP
  256. # Portable specific functions
  257. have_prog()
  258. {
  259. saved_IFS="$IFS"
  260. IFS=":"
  261. for i in $PATH
  262. do
  263. if [ -x $i/$1 ]; then
  264. IFS="$saved_IFS"
  265. return 0
  266. fi
  267. done
  268. IFS="$saved_IFS"
  269. return 1
  270. }
  271. jot() {
  272. awk "BEGIN { for (i = $2; i < $2 + $1; i++) { printf \"%d\n\", i } exit }"
  273. }
  274. # Check whether preprocessor symbols are defined in config.h.
  275. config_defined ()
  276. {
  277. str=$1
  278. while test "x$2" != "x" ; do
  279. str="$str|$2"
  280. shift
  281. done
  282. egrep "^#define.*($str)" ${BUILDDIR}/config.h >/dev/null 2>&1
  283. }
  284. md5 () {
  285. if have_prog md5sum; then
  286. md5sum
  287. elif have_prog openssl; then
  288. openssl md5
  289. elif have_prog cksum; then
  290. cksum
  291. elif have_prog sum; then
  292. sum
  293. else
  294. wc -c
  295. fi
  296. }
  297. # Some platforms don't have hostname at all, but on others uname -n doesn't
  298. # provide the fully qualified name we need, so in the former case we create
  299. # our own hostname function.
  300. if ! have_prog hostname; then
  301. hostname() {
  302. uname -n
  303. }
  304. fi
  305. # End of portable specific functions
  306. stop_sshd ()
  307. {
  308. if [ -f $PIDFILE ]; then
  309. pid=`$SUDO cat $PIDFILE`
  310. if [ "X$pid" = "X" ]; then
  311. echo no sshd running
  312. else
  313. if [ $pid -lt 2 ]; then
  314. echo bad pid for sshd: $pid
  315. else
  316. $SUDO kill $pid
  317. trace "wait for sshd to exit"
  318. i=0;
  319. while [ -f $PIDFILE -a $i -lt 5 ]; do
  320. i=`expr $i + 1`
  321. sleep $i
  322. done
  323. if test -f $PIDFILE; then
  324. if $SUDO kill -0 $pid; then
  325. echo "sshd didn't exit " \
  326. "port $PORT pid $pid"
  327. else
  328. echo "sshd died without cleanup"
  329. fi
  330. exit 1
  331. fi
  332. fi
  333. fi
  334. fi
  335. }
  336. make_tmpdir ()
  337. {
  338. SSH_REGRESS_TMP="$($OBJ/mkdtemp openssh-XXXXXXXX)" || \
  339. fatal "failed to create temporary directory"
  340. }
  341. # helper
  342. cleanup ()
  343. {
  344. if [ "x$SSH_PID" != "x" ]; then
  345. if [ $SSH_PID -lt 2 ]; then
  346. echo bad pid for ssh: $SSH_PID
  347. else
  348. kill $SSH_PID
  349. fi
  350. fi
  351. if [ "x$SSH_REGRESS_TMP" != "x" ]; then
  352. rm -rf "$SSH_REGRESS_TMP"
  353. fi
  354. stop_sshd
  355. }
  356. start_debug_log ()
  357. {
  358. echo "trace: $@" >$TEST_REGRESS_LOGFILE
  359. echo "trace: $@" >$TEST_SSH_LOGFILE
  360. echo "trace: $@" >$TEST_SSHD_LOGFILE
  361. }
  362. save_debug_log ()
  363. {
  364. echo $@ >>$TEST_REGRESS_LOGFILE
  365. echo $@ >>$TEST_SSH_LOGFILE
  366. echo $@ >>$TEST_SSHD_LOGFILE
  367. (cat $TEST_REGRESS_LOGFILE; echo) >>$OBJ/failed-regress.log
  368. (cat $TEST_SSH_LOGFILE; echo) >>$OBJ/failed-ssh.log
  369. (cat $TEST_SSHD_LOGFILE; echo) >>$OBJ/failed-sshd.log
  370. }
  371. trace ()
  372. {
  373. start_debug_log $@
  374. if [ "X$TEST_SSH_TRACE" = "Xyes" ]; then
  375. echo "$@"
  376. fi
  377. }
  378. verbose ()
  379. {
  380. start_debug_log $@
  381. if [ "X$TEST_SSH_QUIET" != "Xyes" ]; then
  382. echo "$@"
  383. fi
  384. }
  385. warn ()
  386. {
  387. echo "WARNING: $@" >>$TEST_SSH_LOGFILE
  388. echo "WARNING: $@"
  389. }
  390. fail ()
  391. {
  392. save_debug_log "FAIL: $@"
  393. RESULT=1
  394. echo "$@"
  395. if test "x$TEST_SSH_FAIL_FATAL" != "x" ; then
  396. cleanup
  397. exit $RESULT
  398. fi
  399. }
  400. fatal ()
  401. {
  402. save_debug_log "FATAL: $@"
  403. printf "FATAL: "
  404. fail "$@"
  405. cleanup
  406. exit $RESULT
  407. }
  408. RESULT=0
  409. PIDFILE=$OBJ/pidfile
  410. trap fatal 3 2
  411. # create server config
  412. cat << EOF > $OBJ/sshd_config
  413. StrictModes no
  414. Port $PORT
  415. AddressFamily inet
  416. ListenAddress 127.0.0.1
  417. #ListenAddress ::1
  418. PidFile $PIDFILE
  419. AuthorizedKeysFile $OBJ/authorized_keys_%u
  420. LogLevel DEBUG3
  421. AcceptEnv _XXX_TEST_*
  422. AcceptEnv _XXX_TEST
  423. Subsystem sftp $SFTPSERVER
  424. EOF
  425. # This may be necessary if /usr/src and/or /usr/obj are group-writable,
  426. # but if you aren't careful with permissions then the unit tests could
  427. # be abused to locally escalate privileges.
  428. if [ ! -z "$TEST_SSH_UNSAFE_PERMISSIONS" ]; then
  429. echo "StrictModes no" >> $OBJ/sshd_config
  430. else
  431. # check and warn if excessive permissions are likely to cause failures.
  432. unsafe=""
  433. dir="${OBJ}"
  434. while test ${dir} != "/"; do
  435. if test -d "${dir}" && ! test -h "${dir}"; then
  436. perms=`ls -ld ${dir}`
  437. case "${perms}" in
  438. ?????w????*|????????w?*) unsafe="${unsafe} ${dir}" ;;
  439. esac
  440. fi
  441. dir=`dirname ${dir}`
  442. done
  443. if ! test -z "${unsafe}"; then
  444. cat <<EOD
  445. WARNING: Unsafe (group or world writable) directory permissions found:
  446. ${unsafe}
  447. These could be abused to locally escalate privileges. If you are
  448. sure that this is not a risk (eg there are no other users), you can
  449. bypass this check by setting TEST_SSH_UNSAFE_PERMISSIONS=1
  450. EOD
  451. fi
  452. fi
  453. if [ ! -z "$TEST_SSH_SSHD_CONFOPTS" ]; then
  454. trace "adding sshd_config option $TEST_SSH_SSHD_CONFOPTS"
  455. echo "$TEST_SSH_SSHD_CONFOPTS" >> $OBJ/sshd_config
  456. fi
  457. # server config for proxy connects
  458. cp $OBJ/sshd_config $OBJ/sshd_proxy
  459. # allow group-writable directories in proxy-mode
  460. echo 'StrictModes no' >> $OBJ/sshd_proxy
  461. # create client config
  462. cat << EOF > $OBJ/ssh_config
  463. Host *
  464. Hostname 127.0.0.1
  465. HostKeyAlias localhost-with-alias
  466. Port $PORT
  467. User $USER
  468. GlobalKnownHostsFile $OBJ/known_hosts
  469. UserKnownHostsFile $OBJ/known_hosts
  470. PubkeyAuthentication yes
  471. ChallengeResponseAuthentication no
  472. HostbasedAuthentication no
  473. PasswordAuthentication no
  474. BatchMode yes
  475. StrictHostKeyChecking yes
  476. LogLevel DEBUG3
  477. EOF
  478. if [ ! -z "$TEST_SSH_SSH_CONFOPTS" ]; then
  479. trace "adding ssh_config option $TEST_SSH_SSH_CONFOPTS"
  480. echo "$TEST_SSH_SSH_CONFOPTS" >> $OBJ/ssh_config
  481. fi
  482. rm -f $OBJ/known_hosts $OBJ/authorized_keys_$USER
  483. SSH_SK_PROVIDER=
  484. if ! config_defined ENABLE_SK; then
  485. trace skipping sk-dummy
  486. elif [ -f "${SRC}/misc/sk-dummy/obj/sk-dummy.so" ] ; then
  487. SSH_SK_PROVIDER="${SRC}/misc/sk-dummy/obj/sk-dummy.so"
  488. elif [ -f "${SRC}/misc/sk-dummy/sk-dummy.so" ] ; then
  489. SSH_SK_PROVIDER="${SRC}/misc/sk-dummy/sk-dummy.so"
  490. fi
  491. export SSH_SK_PROVIDER
  492. if ! test -z "$SSH_SK_PROVIDER"; then
  493. EXTRA_AGENT_ARGS='-P/*' # XXX want realpath(1)...
  494. echo "SecurityKeyProvider $SSH_SK_PROVIDER" >> $OBJ/ssh_config
  495. echo "SecurityKeyProvider $SSH_SK_PROVIDER" >> $OBJ/sshd_config
  496. echo "SecurityKeyProvider $SSH_SK_PROVIDER" >> $OBJ/sshd_proxy
  497. fi
  498. export EXTRA_AGENT_ARGS
  499. maybe_filter_sk() {
  500. if test -z "$SSH_SK_PROVIDER" ; then
  501. grep -v ^sk
  502. else
  503. cat
  504. fi
  505. }
  506. SSH_KEYTYPES=`$SSH -Q key-plain | maybe_filter_sk`
  507. SSH_HOSTKEY_TYPES=`$SSH -Q key-plain | maybe_filter_sk`
  508. for t in ${SSH_KEYTYPES}; do
  509. # generate user key
  510. if [ ! -f $OBJ/$t ] || [ ${SSHKEYGEN_BIN} -nt $OBJ/$t ]; then
  511. trace "generating key type $t"
  512. rm -f $OBJ/$t
  513. ${SSHKEYGEN} -q -N '' -t $t -f $OBJ/$t ||\
  514. fail "ssh-keygen for $t failed"
  515. else
  516. trace "using cached key type $t"
  517. fi
  518. # setup authorized keys
  519. cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
  520. echo IdentityFile $OBJ/$t >> $OBJ/ssh_config
  521. done
  522. for t in ${SSH_HOSTKEY_TYPES}; do
  523. # known hosts file for client
  524. (
  525. printf 'localhost-with-alias,127.0.0.1,::1 '
  526. cat $OBJ/$t.pub
  527. ) >> $OBJ/known_hosts
  528. # use key as host key, too
  529. $SUDO cp $OBJ/$t $OBJ/host.$t
  530. echo HostKey $OBJ/host.$t >> $OBJ/sshd_config
  531. # don't use SUDO for proxy connect
  532. echo HostKey $OBJ/$t >> $OBJ/sshd_proxy
  533. done
  534. chmod 644 $OBJ/authorized_keys_$USER
  535. # Activate Twisted Conch tests if the binary is present
  536. REGRESS_INTEROP_CONCH=no
  537. if test -x "$CONCH" ; then
  538. REGRESS_INTEROP_CONCH=yes
  539. fi
  540. case "$SCRIPT" in
  541. *conch*) ;;
  542. *) REGRESS_INTEROP_CONCH=no
  543. esac
  544. if test "$REGRESS_INTEROP_CONCH" = "yes" ; then
  545. # Convert rsa key to old format to work around
  546. # https://twistedmatrix.com/trac/ticket/9515
  547. cp $OBJ/ssh-rsa $OBJ/ssh-rsa_oldfmt
  548. cp $OBJ/ssh-rsa.pub $OBJ/ssh-rsa_oldfmt.pub
  549. ${SSHKEYGEN} -p -N '' -m PEM -f $OBJ/ssh-rsa_oldfmt >/dev/null
  550. fi
  551. # If PuTTY is present, new enough and we are running a PuTTY test, prepare
  552. # keys and configuration.
  553. REGRESS_INTEROP_PUTTY=no
  554. if test -x "$PUTTYGEN" -a -x "$PLINK" &&
  555. "$PUTTYGEN" --help 2>&1 | grep -- --new-passphrase >/dev/null; then
  556. REGRESS_INTEROP_PUTTY=yes
  557. fi
  558. case "$SCRIPT" in
  559. *putty*) ;;
  560. *) REGRESS_INTEROP_PUTTY=no ;;
  561. esac
  562. if test "$REGRESS_INTEROP_PUTTY" = "yes" ; then
  563. mkdir -p ${OBJ}/.putty
  564. # Add a PuTTY key to authorized_keys
  565. rm -f ${OBJ}/putty.rsa2
  566. if ! "$PUTTYGEN" -t rsa -o ${OBJ}/putty.rsa2 \
  567. --random-device=/dev/urandom \
  568. --new-passphrase /dev/null < /dev/null > /dev/null; then
  569. echo "Your installed version of PuTTY is too old to support --new-passphrase, skipping test" >&2
  570. exit 1
  571. fi
  572. "$PUTTYGEN" -O public-openssh ${OBJ}/putty.rsa2 \
  573. >> $OBJ/authorized_keys_$USER
  574. # Convert rsa2 host key to PuTTY format
  575. cp $OBJ/ssh-rsa $OBJ/ssh-rsa_oldfmt
  576. ${SSHKEYGEN} -p -N '' -m PEM -f $OBJ/ssh-rsa_oldfmt >/dev/null
  577. ${SRC}/ssh2putty.sh 127.0.0.1 $PORT $OBJ/ssh-rsa_oldfmt > \
  578. ${OBJ}/.putty/sshhostkeys
  579. ${SRC}/ssh2putty.sh 127.0.0.1 22 $OBJ/ssh-rsa_oldfmt >> \
  580. ${OBJ}/.putty/sshhostkeys
  581. rm -f $OBJ/ssh-rsa_oldfmt
  582. # Setup proxied session
  583. mkdir -p ${OBJ}/.putty/sessions
  584. rm -f ${OBJ}/.putty/sessions/localhost_proxy
  585. echo "Protocol=ssh" >> ${OBJ}/.putty/sessions/localhost_proxy
  586. echo "HostName=127.0.0.1" >> ${OBJ}/.putty/sessions/localhost_proxy
  587. echo "PortNumber=$PORT" >> ${OBJ}/.putty/sessions/localhost_proxy
  588. echo "ProxyMethod=5" >> ${OBJ}/.putty/sessions/localhost_proxy
  589. echo "ProxyTelnetCommand=sh ${SRC}/sshd-log-wrapper.sh ${TEST_SSHD_LOGFILE} ${SSHD} -i -f $OBJ/sshd_proxy" >> ${OBJ}/.putty/sessions/localhost_proxy
  590. echo "ProxyLocalhost=1" >> ${OBJ}/.putty/sessions/localhost_proxy
  591. PUTTYDIR=${OBJ}/.putty
  592. export PUTTYDIR
  593. fi
  594. # create a proxy version of the client config
  595. (
  596. cat $OBJ/ssh_config
  597. 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
  598. ) > $OBJ/ssh_proxy
  599. # check proxy config
  600. ${SSHD} -t -f $OBJ/sshd_proxy || fatal "sshd_proxy broken"
  601. start_sshd ()
  602. {
  603. # start sshd
  604. $SUDO ${SSHD} -f $OBJ/sshd_config "$@" -t || fatal "sshd_config broken"
  605. $SUDO env SSH_SK_HELPER="$SSH_SK_HELPER" \
  606. ${SSHD} -f $OBJ/sshd_config "$@" -E$TEST_SSHD_LOGFILE
  607. trace "wait for sshd"
  608. i=0;
  609. while [ ! -f $PIDFILE -a $i -lt 10 ]; do
  610. i=`expr $i + 1`
  611. sleep $i
  612. done
  613. test -f $PIDFILE || fatal "no sshd running on port $PORT"
  614. }
  615. # source test body
  616. . $SCRIPT
  617. # kill sshd
  618. cleanup
  619. if [ "x$USE_VALGRIND" != "x" ]; then
  620. # wait for any running process to complete
  621. wait; sleep 1
  622. VG_RESULTS=$(find $OBJ/valgrind-out -type f -print)
  623. VG_RESULT_COUNT=0
  624. VG_FAIL_COUNT=0
  625. for i in $VG_RESULTS; do
  626. if grep "ERROR SUMMARY" $i >/dev/null; then
  627. VG_RESULT_COUNT=$(($VG_RESULT_COUNT + 1))
  628. if ! grep "ERROR SUMMARY: 0 errors" $i >/dev/null; then
  629. VG_FAIL_COUNT=$(($VG_FAIL_COUNT + 1))
  630. RESULT=1
  631. verbose valgrind failure $i
  632. cat $i
  633. fi
  634. fi
  635. done
  636. if [ x"$VG_SKIP" != "x" ]; then
  637. verbose valgrind skipped
  638. else
  639. verbose valgrind results $VG_RESULT_COUNT failures $VG_FAIL_COUNT
  640. fi
  641. fi
  642. if [ $RESULT -eq 0 ]; then
  643. verbose ok $tid
  644. else
  645. echo failed $tid
  646. fi
  647. exit $RESULT