1# $OpenBSD: scp.sh,v 1.14 2022/05/15 23:48:07 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: copy local file to remote file in place" 53 scpclean 54 cp ${DATA} ${COPY} 55 $SCP $scpopts ${COPY} somehost:${COPY} || fail "copy failed" 56 cmp ${DATA} ${COPY} || fail "corrupted copy" 57 58 verbose "$tag: copy remote file to local file in place" 59 scpclean 60 cp ${DATA} ${COPY} 61 $SCP $scpopts somehost:${COPY} ${COPY} || fail "copy failed" 62 cmp ${DATA} ${COPY} || fail "corrupted copy" 63 64 verbose "$tag: copy local file to remote file clobber" 65 scpclean 66 cat ${DATA} ${DATA} > ${COPY} 67 $SCP $scpopts ${DATA} somehost:${COPY} || fail "copy failed" 68 ls -l $DATA $COPY 69 cmp ${DATA} ${COPY} || fail "corrupted copy" 70 71 verbose "$tag: copy remote file to local file clobber" 72 scpclean 73 cat ${DATA} ${DATA} > ${COPY} 74 $SCP $scpopts somehost:${DATA} ${COPY} || fail "copy failed" 75 cmp ${DATA} ${COPY} || fail "corrupted copy" 76 77 verbose "$tag: simple copy local file to remote dir" 78 scpclean 79 cp ${DATA} ${COPY} 80 $SCP $scpopts ${COPY} somehost:${DIR} || fail "copy failed" 81 cmp ${COPY} ${DIR}/copy || fail "corrupted copy" 82 83 verbose "$tag: simple copy local file to local dir" 84 scpclean 85 cp ${DATA} ${COPY} 86 $SCP $scpopts ${COPY} ${DIR} || fail "copy failed" 87 cmp ${COPY} ${DIR}/copy || fail "corrupted copy" 88 89 verbose "$tag: simple copy remote file to local dir" 90 scpclean 91 cp ${DATA} ${COPY} 92 $SCP $scpopts somehost:${COPY} ${DIR} || fail "copy failed" 93 cmp ${COPY} ${DIR}/copy || fail "corrupted copy" 94 95 verbose "$tag: recursive local dir to remote dir" 96 scpclean 97 rm -rf ${DIR2} 98 cp ${DATA} ${DIR}/copy 99 $SCP $scpopts -r ${DIR} somehost:${DIR2} || fail "copy failed" 100 diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 101 102 verbose "$tag: recursive local dir to local dir" 103 scpclean 104 rm -rf ${DIR2} 105 cp ${DATA} ${DIR}/copy 106 $SCP $scpopts -r ${DIR} ${DIR2} || fail "copy failed" 107 diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 108 109 verbose "$tag: recursive remote dir to local dir" 110 scpclean 111 rm -rf ${DIR2} 112 cp ${DATA} ${DIR}/copy 113 $SCP $scpopts -r somehost:${DIR} ${DIR2} || fail "copy failed" 114 diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 115 116 verbose "$tag: shell metacharacters" 117 scpclean 118 (cd ${DIR} && \ 119 touch '`touch metachartest`' && \ 120 $SCP $scpopts *metachar* ${DIR2} 2>/dev/null; \ 121 [ ! -f metachartest ] ) || fail "shell metacharacters" 122 123 if [ ! -z "$SUDO" ]; then 124 verbose "$tag: skipped file after scp -p with failed chown+utimes" 125 scpclean 126 cp -p ${DATA} ${DIR}/copy 127 cp -p ${DATA} ${DIR}/copy2 128 cp ${DATA} ${DIR2}/copy 129 chmod 660 ${DIR2}/copy 130 $SUDO chown root ${DIR2}/copy 131 $SCP -p $scpopts somehost:${DIR}/\* ${DIR2} >/dev/null 2>&1 132 $SUDO diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 133 $SUDO rm ${DIR2}/copy 134 fi 135 136 for i in 0 1 2 3 4 5 6 7; do 137 verbose "$tag: disallow bad server #$i" 138 SCPTESTMODE=badserver_$i 139 export DIR SCPTESTMODE 140 scpclean 141 $SCP $scpopts somehost:${DATA} ${DIR} >/dev/null 2>/dev/null 142 [ -d {$DIR}/rootpathdir ] && fail "allows dir relative to root dir" 143 [ -d ${DIR}/dotpathdir ] && fail "allows dir creation in non-recursive mode" 144 145 scpclean 146 $SCP -r $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 147 [ -d ${DIR}/dotpathdir ] && fail "allows dir creation outside of subdir" 148 149 scpclean 150 $SCP -pr $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 151 [ ! -w ${DIR2} ] && fail "allows target root attribute change" 152 153 scpclean 154 $SCP $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 155 [ -e ${DIR2}/extrafile ] && fail "allows unauth object creation" 156 rm -f ${DIR2}/extrafile 157 done 158 159 verbose "$tag: detect non-directory target" 160 scpclean 161 echo a > ${COPY} 162 echo b > ${COPY2} 163 $SCP $scpopts ${DATA} ${COPY} ${COPY2} 164 cmp ${COPY} ${COPY2} >/dev/null && fail "corrupt target" 165done 166 167scpclean 168rm -f ${OBJ}/scp-ssh-wrapper.scp 169