1*edf85781SEd Maste# $OpenBSD: scp.sh,v 1.19 2023/09/08 05:50:57 djm Exp $ 2ce3adf43SDag-Erling Smørgrav# Placed in the Public Domain. 3ce3adf43SDag-Erling Smørgrav 4ce3adf43SDag-Erling Smørgravtid="scp" 5ce3adf43SDag-Erling Smørgrav 6ce3adf43SDag-Erling Smørgrav#set -x 7ce3adf43SDag-Erling Smørgrav 8ce3adf43SDag-Erling SmørgravCOPY2=${OBJ}/copy2 9ce3adf43SDag-Erling SmørgravDIR=${COPY}.dd 10ce3adf43SDag-Erling SmørgravDIR2=${COPY}.dd2 11f374ba41SEd MasteCOPY3=${OBJ}/copy.glob[123] 12f374ba41SEd MasteDIR3=${COPY}.dd.glob[456] 13f374ba41SEd MasteDIFFOPT="-rN" 14f374ba41SEd Maste 15f374ba41SEd Maste# Figure out if diff does not understand "-N" 16f374ba41SEd Masteif ! diff -N ${SRC}/scp.sh ${SRC}/scp.sh 2>/dev/null; then 17f374ba41SEd Maste DIFFOPT="-r" 18f374ba41SEd Mastefi 19f374ba41SEd Maste 20f374ba41SEd Mastemaybe_add_scp_path_to_sshd 21ce3adf43SDag-Erling Smørgrav 22ce3adf43SDag-Erling SmørgravSRC=`dirname ${SCRIPT}` 23ce3adf43SDag-Erling Smørgravcp ${SRC}/scp-ssh-wrapper.sh ${OBJ}/scp-ssh-wrapper.scp 24ce3adf43SDag-Erling Smørgravchmod 755 ${OBJ}/scp-ssh-wrapper.scp 25f7167e0eSDag-Erling Smørgravexport SCP # used in scp-ssh-wrapper.scp 26ce3adf43SDag-Erling Smørgrav 27ce3adf43SDag-Erling Smørgravscpclean() { 28f374ba41SEd Maste rm -rf ${COPY} ${COPY2} ${DIR} ${DIR2} ${COPY3} ${DIR3} 29f374ba41SEd Maste mkdir ${DIR} ${DIR2} ${DIR3} 30f374ba41SEd Maste chmod 755 ${DIR} ${DIR2} ${DIR3} 31ce3adf43SDag-Erling Smørgrav} 32ce3adf43SDag-Erling Smørgrav 33*edf85781SEd Maste# Create directory structure for recursive copy tests. 34*edf85781SEd Masteforest() { 35*edf85781SEd Maste scpclean 36*edf85781SEd Maste rm -rf ${DIR2} 37*edf85781SEd Maste cp ${DATA} ${DIR}/copy 38*edf85781SEd Maste ln -s ${DIR}/copy ${DIR}/copy-sym 39*edf85781SEd Maste mkdir ${DIR}/subdir 40*edf85781SEd Maste cp ${DATA} ${DIR}/subdir/copy 41*edf85781SEd Maste ln -s ${DIR}/subdir ${DIR}/subdir-sym 42*edf85781SEd Maste} 43*edf85781SEd Maste 4419261079SEd Mastefor mode in scp sftp ; do 4519261079SEd Maste tag="$tid: $mode mode" 4619261079SEd Maste if test $mode = scp ; then 4719261079SEd Maste scpopts="-O -q -S ${OBJ}/scp-ssh-wrapper.scp" 4819261079SEd Maste else 49*edf85781SEd Maste scpopts="-qs -D ${SFTPSERVER}" 5019261079SEd Maste fi 51*edf85781SEd Maste 52f374ba41SEd Maste verbose "$tag: simple copy local file to local file" 53ce3adf43SDag-Erling Smørgrav scpclean 54ce3adf43SDag-Erling Smørgrav $SCP $scpopts ${DATA} ${COPY} || fail "copy failed" 55ce3adf43SDag-Erling Smørgrav cmp ${DATA} ${COPY} || fail "corrupted copy" 56ce3adf43SDag-Erling Smørgrav 5719261079SEd Maste verbose "$tag: simple copy local file to remote file" 58ce3adf43SDag-Erling Smørgrav scpclean 59ce3adf43SDag-Erling Smørgrav $SCP $scpopts ${DATA} somehost:${COPY} || fail "copy failed" 60ce3adf43SDag-Erling Smørgrav cmp ${DATA} ${COPY} || fail "corrupted copy" 61ce3adf43SDag-Erling Smørgrav 6219261079SEd Maste verbose "$tag: simple copy remote file to local file" 63ce3adf43SDag-Erling Smørgrav scpclean 64ce3adf43SDag-Erling Smørgrav $SCP $scpopts somehost:${DATA} ${COPY} || fail "copy failed" 65ce3adf43SDag-Erling Smørgrav cmp ${DATA} ${COPY} || fail "corrupted copy" 66ce3adf43SDag-Erling Smørgrav 6738a52bd3SEd Maste verbose "$tag: copy local file to remote file in place" 6838a52bd3SEd Maste scpclean 6938a52bd3SEd Maste cp ${DATA} ${COPY} 7038a52bd3SEd Maste $SCP $scpopts ${COPY} somehost:${COPY} || fail "copy failed" 7138a52bd3SEd Maste cmp ${DATA} ${COPY} || fail "corrupted copy" 7238a52bd3SEd Maste 7338a52bd3SEd Maste verbose "$tag: copy remote file to local file in place" 7438a52bd3SEd Maste scpclean 7538a52bd3SEd Maste cp ${DATA} ${COPY} 7638a52bd3SEd Maste $SCP $scpopts somehost:${COPY} ${COPY} || fail "copy failed" 7738a52bd3SEd Maste cmp ${DATA} ${COPY} || fail "corrupted copy" 7838a52bd3SEd Maste 7938a52bd3SEd Maste verbose "$tag: copy local file to remote file clobber" 8038a52bd3SEd Maste scpclean 8138a52bd3SEd Maste cat ${DATA} ${DATA} > ${COPY} 8238a52bd3SEd Maste $SCP $scpopts ${DATA} somehost:${COPY} || fail "copy failed" 8338a52bd3SEd Maste ls -l $DATA $COPY 8438a52bd3SEd Maste cmp ${DATA} ${COPY} || fail "corrupted copy" 8538a52bd3SEd Maste 8638a52bd3SEd Maste verbose "$tag: copy remote file to local file clobber" 8738a52bd3SEd Maste scpclean 8838a52bd3SEd Maste cat ${DATA} ${DATA} > ${COPY} 8938a52bd3SEd Maste $SCP $scpopts somehost:${DATA} ${COPY} || fail "copy failed" 9038a52bd3SEd Maste cmp ${DATA} ${COPY} || fail "corrupted copy" 9138a52bd3SEd Maste 9219261079SEd Maste verbose "$tag: simple copy local file to remote dir" 93ce3adf43SDag-Erling Smørgrav scpclean 94ce3adf43SDag-Erling Smørgrav cp ${DATA} ${COPY} 95ce3adf43SDag-Erling Smørgrav $SCP $scpopts ${COPY} somehost:${DIR} || fail "copy failed" 96ce3adf43SDag-Erling Smørgrav cmp ${COPY} ${DIR}/copy || fail "corrupted copy" 97ce3adf43SDag-Erling Smørgrav 9819261079SEd Maste verbose "$tag: simple copy local file to local dir" 99ce3adf43SDag-Erling Smørgrav scpclean 100ce3adf43SDag-Erling Smørgrav cp ${DATA} ${COPY} 101ce3adf43SDag-Erling Smørgrav $SCP $scpopts ${COPY} ${DIR} || fail "copy failed" 102ce3adf43SDag-Erling Smørgrav cmp ${COPY} ${DIR}/copy || fail "corrupted copy" 103ce3adf43SDag-Erling Smørgrav 10419261079SEd Maste verbose "$tag: simple copy remote file to local dir" 105ce3adf43SDag-Erling Smørgrav scpclean 106ce3adf43SDag-Erling Smørgrav cp ${DATA} ${COPY} 107ce3adf43SDag-Erling Smørgrav $SCP $scpopts somehost:${COPY} ${DIR} || fail "copy failed" 108ce3adf43SDag-Erling Smørgrav cmp ${COPY} ${DIR}/copy || fail "corrupted copy" 109ce3adf43SDag-Erling Smørgrav 11019261079SEd Maste verbose "$tag: recursive local dir to remote dir" 111*edf85781SEd Maste forest 112ce3adf43SDag-Erling Smørgrav $SCP $scpopts -r ${DIR} somehost:${DIR2} || fail "copy failed" 113ce3adf43SDag-Erling Smørgrav diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 114ce3adf43SDag-Erling Smørgrav 11519261079SEd Maste verbose "$tag: recursive local dir to local dir" 116*edf85781SEd Maste forest 117ce3adf43SDag-Erling Smørgrav rm -rf ${DIR2} 118ce3adf43SDag-Erling Smørgrav cp ${DATA} ${DIR}/copy 119ce3adf43SDag-Erling Smørgrav $SCP $scpopts -r ${DIR} ${DIR2} || fail "copy failed" 120ce3adf43SDag-Erling Smørgrav diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 121ce3adf43SDag-Erling Smørgrav 12219261079SEd Maste verbose "$tag: recursive remote dir to local dir" 123*edf85781SEd Maste forest 124ce3adf43SDag-Erling Smørgrav rm -rf ${DIR2} 125ce3adf43SDag-Erling Smørgrav cp ${DATA} ${DIR}/copy 126ce3adf43SDag-Erling Smørgrav $SCP $scpopts -r somehost:${DIR} ${DIR2} || fail "copy failed" 127ce3adf43SDag-Erling Smørgrav diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 128ce3adf43SDag-Erling Smørgrav 129f374ba41SEd Maste verbose "$tag: unmatched glob file local->remote" 130f374ba41SEd Maste scpclean 131f374ba41SEd Maste $SCP $scpopts ${DATA} somehost:${COPY3} || fail "copy failed" 132f374ba41SEd Maste cmp ${DATA} ${COPY3} || fail "corrupted copy" 133f374ba41SEd Maste 134f374ba41SEd Maste verbose "$tag: unmatched glob file remote->local" 135f374ba41SEd Maste # NB. no clean 136f374ba41SEd Maste $SCP $scpopts somehost:${COPY3} ${COPY2} || fail "copy failed" 137f374ba41SEd Maste cmp ${DATA} ${COPY2} || fail "corrupted copy" 138f374ba41SEd Maste 139f374ba41SEd Maste verbose "$tag: unmatched glob dir recursive local->remote" 140f374ba41SEd Maste scpclean 141f374ba41SEd Maste rm -rf ${DIR3} 142f374ba41SEd Maste cp ${DATA} ${DIR}/copy 143f374ba41SEd Maste cp ${DATA} ${DIR}/copy.glob[1234] 144f374ba41SEd Maste $SCP $scpopts -r ${DIR} somehost:${DIR3} || fail "copy failed" 145f374ba41SEd Maste diff ${DIFFOPT} ${DIR} ${DIR3} || fail "corrupted copy" 146f374ba41SEd Maste 147f374ba41SEd Maste verbose "$tag: unmatched glob dir recursive remote->local" 148f374ba41SEd Maste # NB. no clean 149f374ba41SEd Maste rm -rf ${DIR2} 150f374ba41SEd Maste $SCP $scpopts -r somehost:${DIR3} ${DIR2} || fail "copy failed" 151f374ba41SEd Maste diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 152f374ba41SEd Maste 15319261079SEd Maste verbose "$tag: shell metacharacters" 154ce3adf43SDag-Erling Smørgrav scpclean 155ce3adf43SDag-Erling Smørgrav (cd ${DIR} && \ 156ce3adf43SDag-Erling Smørgrav touch '`touch metachartest`' && \ 157ce3adf43SDag-Erling Smørgrav $SCP $scpopts *metachar* ${DIR2} 2>/dev/null; \ 158ce3adf43SDag-Erling Smørgrav [ ! -f metachartest ] ) || fail "shell metacharacters" 159ce3adf43SDag-Erling Smørgrav 160ce3adf43SDag-Erling Smørgrav if [ ! -z "$SUDO" ]; then 16119261079SEd Maste verbose "$tag: skipped file after scp -p with failed chown+utimes" 162ce3adf43SDag-Erling Smørgrav scpclean 163ce3adf43SDag-Erling Smørgrav cp -p ${DATA} ${DIR}/copy 164ce3adf43SDag-Erling Smørgrav cp -p ${DATA} ${DIR}/copy2 165ce3adf43SDag-Erling Smørgrav cp ${DATA} ${DIR2}/copy 166ce3adf43SDag-Erling Smørgrav chmod 660 ${DIR2}/copy 167ce3adf43SDag-Erling Smørgrav $SUDO chown root ${DIR2}/copy 168ce3adf43SDag-Erling Smørgrav $SCP -p $scpopts somehost:${DIR}/\* ${DIR2} >/dev/null 2>&1 169ce3adf43SDag-Erling Smørgrav $SUDO diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 170ce3adf43SDag-Erling Smørgrav $SUDO rm ${DIR2}/copy 171ce3adf43SDag-Erling Smørgrav fi 172ce3adf43SDag-Erling Smørgrav 17319261079SEd Maste for i in 0 1 2 3 4 5 6 7; do 17419261079SEd Maste verbose "$tag: disallow bad server #$i" 175ce3adf43SDag-Erling Smørgrav SCPTESTMODE=badserver_$i 176ce3adf43SDag-Erling Smørgrav export DIR SCPTESTMODE 177ce3adf43SDag-Erling Smørgrav scpclean 178ce3adf43SDag-Erling Smørgrav $SCP $scpopts somehost:${DATA} ${DIR} >/dev/null 2>/dev/null 179ce3adf43SDag-Erling Smørgrav [ -d {$DIR}/rootpathdir ] && fail "allows dir relative to root dir" 180ce3adf43SDag-Erling Smørgrav [ -d ${DIR}/dotpathdir ] && fail "allows dir creation in non-recursive mode" 181ce3adf43SDag-Erling Smørgrav 182ce3adf43SDag-Erling Smørgrav scpclean 183ce3adf43SDag-Erling Smørgrav $SCP -r $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 184ce3adf43SDag-Erling Smørgrav [ -d ${DIR}/dotpathdir ] && fail "allows dir creation outside of subdir" 18519261079SEd Maste 18619261079SEd Maste scpclean 18719261079SEd Maste $SCP -pr $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 18819261079SEd Maste [ ! -w ${DIR2} ] && fail "allows target root attribute change" 18919261079SEd Maste 19019261079SEd Maste scpclean 19119261079SEd Maste $SCP $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 19219261079SEd Maste [ -e ${DIR2}/extrafile ] && fail "allows unauth object creation" 19319261079SEd Maste rm -f ${DIR2}/extrafile 194ce3adf43SDag-Erling Smørgrav done 195ce3adf43SDag-Erling Smørgrav 19619261079SEd Maste verbose "$tag: detect non-directory target" 197ce3adf43SDag-Erling Smørgrav scpclean 198ce3adf43SDag-Erling Smørgrav echo a > ${COPY} 199ce3adf43SDag-Erling Smørgrav echo b > ${COPY2} 200ce3adf43SDag-Erling Smørgrav $SCP $scpopts ${DATA} ${COPY} ${COPY2} 201ce3adf43SDag-Erling Smørgrav cmp ${COPY} ${COPY2} >/dev/null && fail "corrupt target" 20219261079SEd Mastedone 203ce3adf43SDag-Erling Smørgrav 204ce3adf43SDag-Erling Smørgravscpclean 205ce3adf43SDag-Erling Smørgravrm -f ${OBJ}/scp-ssh-wrapper.scp 206