1# $OpenBSD: scp.sh,v 1.13 2021/08/10 03:35:45 djm Exp $ 2# Placed in the Public Domain. 3 4tid="scp" 5 6#set -x 7 8# Figure out if diff understands "-N" 9if diff -N ${SRC}/scp.sh ${SRC}/scp.sh 2>/dev/null; then 10 DIFFOPT="-rN" 11else 12 DIFFOPT="-r" 13fi 14 15COPY2=${OBJ}/copy2 16DIR=${COPY}.dd 17DIR2=${COPY}.dd2 18 19SRC=`dirname ${SCRIPT}` 20cp ${SRC}/scp-ssh-wrapper.sh ${OBJ}/scp-ssh-wrapper.scp 21chmod 755 ${OBJ}/scp-ssh-wrapper.scp 22export SCP # used in scp-ssh-wrapper.scp 23 24scpclean() { 25 rm -rf ${COPY} ${COPY2} ${DIR} ${DIR2} 26 mkdir ${DIR} ${DIR2} 27 chmod 755 ${DIR} ${DIR2} 28} 29 30for mode in scp sftp ; do 31 tag="$tid: $mode mode" 32 if test $mode = scp ; then 33 scpopts="-O -q -S ${OBJ}/scp-ssh-wrapper.scp" 34 else 35 scpopts="-s -D ${SFTPSERVER}" 36 fi 37 verbose "tid: simple copy local file to local file" 38 scpclean 39 $SCP $scpopts ${DATA} ${COPY} || fail "copy failed" 40 cmp ${DATA} ${COPY} || fail "corrupted copy" 41 42 verbose "$tag: simple copy local file to remote file" 43 scpclean 44 $SCP $scpopts ${DATA} somehost:${COPY} || fail "copy failed" 45 cmp ${DATA} ${COPY} || fail "corrupted copy" 46 47 verbose "$tag: simple copy remote file to local file" 48 scpclean 49 $SCP $scpopts somehost:${DATA} ${COPY} || fail "copy failed" 50 cmp ${DATA} ${COPY} || fail "corrupted copy" 51 52 verbose "$tag: simple copy local file to remote dir" 53 scpclean 54 cp ${DATA} ${COPY} 55 $SCP $scpopts ${COPY} somehost:${DIR} || fail "copy failed" 56 cmp ${COPY} ${DIR}/copy || fail "corrupted copy" 57 58 verbose "$tag: simple copy local file to local dir" 59 scpclean 60 cp ${DATA} ${COPY} 61 $SCP $scpopts ${COPY} ${DIR} || fail "copy failed" 62 cmp ${COPY} ${DIR}/copy || fail "corrupted copy" 63 64 verbose "$tag: simple copy remote file to local dir" 65 scpclean 66 cp ${DATA} ${COPY} 67 $SCP $scpopts somehost:${COPY} ${DIR} || fail "copy failed" 68 cmp ${COPY} ${DIR}/copy || fail "corrupted copy" 69 70 verbose "$tag: recursive local dir to remote dir" 71 scpclean 72 rm -rf ${DIR2} 73 cp ${DATA} ${DIR}/copy 74 $SCP $scpopts -r ${DIR} somehost:${DIR2} || fail "copy failed" 75 diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 76 77 verbose "$tag: recursive local dir to local dir" 78 scpclean 79 rm -rf ${DIR2} 80 cp ${DATA} ${DIR}/copy 81 $SCP $scpopts -r ${DIR} ${DIR2} || fail "copy failed" 82 diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 83 84 verbose "$tag: recursive remote dir to local dir" 85 scpclean 86 rm -rf ${DIR2} 87 cp ${DATA} ${DIR}/copy 88 $SCP $scpopts -r somehost:${DIR} ${DIR2} || fail "copy failed" 89 diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 90 91 verbose "$tag: shell metacharacters" 92 scpclean 93 (cd ${DIR} && \ 94 touch '`touch metachartest`' && \ 95 $SCP $scpopts *metachar* ${DIR2} 2>/dev/null; \ 96 [ ! -f metachartest ] ) || fail "shell metacharacters" 97 98 if [ ! -z "$SUDO" ]; then 99 verbose "$tag: skipped file after scp -p with failed chown+utimes" 100 scpclean 101 cp -p ${DATA} ${DIR}/copy 102 cp -p ${DATA} ${DIR}/copy2 103 cp ${DATA} ${DIR2}/copy 104 chmod 660 ${DIR2}/copy 105 $SUDO chown root ${DIR2}/copy 106 $SCP -p $scpopts somehost:${DIR}/\* ${DIR2} >/dev/null 2>&1 107 $SUDO diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 108 $SUDO rm ${DIR2}/copy 109 fi 110 111 for i in 0 1 2 3 4 5 6 7; do 112 verbose "$tag: disallow bad server #$i" 113 SCPTESTMODE=badserver_$i 114 export DIR SCPTESTMODE 115 scpclean 116 $SCP $scpopts somehost:${DATA} ${DIR} >/dev/null 2>/dev/null 117 [ -d {$DIR}/rootpathdir ] && fail "allows dir relative to root dir" 118 [ -d ${DIR}/dotpathdir ] && fail "allows dir creation in non-recursive mode" 119 120 scpclean 121 $SCP -r $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 122 [ -d ${DIR}/dotpathdir ] && fail "allows dir creation outside of subdir" 123 124 scpclean 125 $SCP -pr $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 126 [ ! -w ${DIR2} ] && fail "allows target root attribute change" 127 128 scpclean 129 $SCP $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 130 [ -e ${DIR2}/extrafile ] && fail "allows unauth object creation" 131 rm -f ${DIR2}/extrafile 132 done 133 134 verbose "$tag: detect non-directory target" 135 scpclean 136 echo a > ${COPY} 137 echo b > ${COPY2} 138 $SCP $scpopts ${DATA} ${COPY} ${COPY2} 139 cmp ${COPY} ${COPY2} >/dev/null && fail "corrupt target" 140done 141 142scpclean 143rm -f ${OBJ}/scp-ssh-wrapper.scp 144