1# $OpenBSD: scp.sh,v 1.18 2023/01/13 04:47:34 dtucker Exp $ 2# Placed in the Public Domain. 3 4tid="scp" 5 6#set -x 7 8COPY2=${OBJ}/copy2 9DIR=${COPY}.dd 10DIR2=${COPY}.dd2 11COPY3=${OBJ}/copy.glob[123] 12DIR3=${COPY}.dd.glob[456] 13DIFFOPT="-rN" 14 15# Figure out if diff does not understand "-N" 16if ! diff -N ${SRC}/scp.sh ${SRC}/scp.sh 2>/dev/null; then 17 DIFFOPT="-r" 18fi 19 20maybe_add_scp_path_to_sshd 21 22SRC=`dirname ${SCRIPT}` 23cp ${SRC}/scp-ssh-wrapper.sh ${OBJ}/scp-ssh-wrapper.scp 24chmod 755 ${OBJ}/scp-ssh-wrapper.scp 25export SCP # used in scp-ssh-wrapper.scp 26 27scpclean() { 28 rm -rf ${COPY} ${COPY2} ${DIR} ${DIR2} ${COPY3} ${DIR3} 29 mkdir ${DIR} ${DIR2} ${DIR3} 30 chmod 755 ${DIR} ${DIR2} ${DIR3} 31} 32 33for mode in scp sftp ; do 34 tag="$tid: $mode mode" 35 if test $mode = scp ; then 36 scpopts="-O -q -S ${OBJ}/scp-ssh-wrapper.scp" 37 else 38 scpopts="-s -D ${SFTPSERVER}" 39 fi 40 verbose "$tag: simple copy local file to local file" 41 scpclean 42 $SCP $scpopts ${DATA} ${COPY} || fail "copy failed" 43 cmp ${DATA} ${COPY} || fail "corrupted copy" 44 45 verbose "$tag: simple copy local file to remote file" 46 scpclean 47 $SCP $scpopts ${DATA} somehost:${COPY} || fail "copy failed" 48 cmp ${DATA} ${COPY} || fail "corrupted copy" 49 50 verbose "$tag: simple copy remote file to local file" 51 scpclean 52 $SCP $scpopts somehost:${DATA} ${COPY} || fail "copy failed" 53 cmp ${DATA} ${COPY} || fail "corrupted copy" 54 55 verbose "$tag: copy local file to remote file in place" 56 scpclean 57 cp ${DATA} ${COPY} 58 $SCP $scpopts ${COPY} somehost:${COPY} || fail "copy failed" 59 cmp ${DATA} ${COPY} || fail "corrupted copy" 60 61 verbose "$tag: copy remote file to local file in place" 62 scpclean 63 cp ${DATA} ${COPY} 64 $SCP $scpopts somehost:${COPY} ${COPY} || fail "copy failed" 65 cmp ${DATA} ${COPY} || fail "corrupted copy" 66 67 verbose "$tag: copy local file to remote file clobber" 68 scpclean 69 cat ${DATA} ${DATA} > ${COPY} 70 $SCP $scpopts ${DATA} somehost:${COPY} || fail "copy failed" 71 ls -l $DATA $COPY 72 cmp ${DATA} ${COPY} || fail "corrupted copy" 73 74 verbose "$tag: copy remote file to local file clobber" 75 scpclean 76 cat ${DATA} ${DATA} > ${COPY} 77 $SCP $scpopts somehost:${DATA} ${COPY} || fail "copy failed" 78 cmp ${DATA} ${COPY} || fail "corrupted copy" 79 80 verbose "$tag: simple copy local file to remote dir" 81 scpclean 82 cp ${DATA} ${COPY} 83 $SCP $scpopts ${COPY} somehost:${DIR} || fail "copy failed" 84 cmp ${COPY} ${DIR}/copy || fail "corrupted copy" 85 86 verbose "$tag: simple copy local file to local dir" 87 scpclean 88 cp ${DATA} ${COPY} 89 $SCP $scpopts ${COPY} ${DIR} || fail "copy failed" 90 cmp ${COPY} ${DIR}/copy || fail "corrupted copy" 91 92 verbose "$tag: simple copy remote file to local dir" 93 scpclean 94 cp ${DATA} ${COPY} 95 $SCP $scpopts somehost:${COPY} ${DIR} || fail "copy failed" 96 cmp ${COPY} ${DIR}/copy || fail "corrupted copy" 97 98 verbose "$tag: recursive local dir to remote dir" 99 scpclean 100 rm -rf ${DIR2} 101 cp ${DATA} ${DIR}/copy 102 $SCP $scpopts -r ${DIR} somehost:${DIR2} || fail "copy failed" 103 diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 104 105 verbose "$tag: recursive local dir to local dir" 106 scpclean 107 rm -rf ${DIR2} 108 cp ${DATA} ${DIR}/copy 109 $SCP $scpopts -r ${DIR} ${DIR2} || fail "copy failed" 110 diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 111 112 verbose "$tag: recursive remote dir to local dir" 113 scpclean 114 rm -rf ${DIR2} 115 cp ${DATA} ${DIR}/copy 116 $SCP $scpopts -r somehost:${DIR} ${DIR2} || fail "copy failed" 117 diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 118 119 verbose "$tag: unmatched glob file local->remote" 120 scpclean 121 $SCP $scpopts ${DATA} somehost:${COPY3} || fail "copy failed" 122 cmp ${DATA} ${COPY3} || fail "corrupted copy" 123 124 verbose "$tag: unmatched glob file remote->local" 125 # NB. no clean 126 $SCP $scpopts somehost:${COPY3} ${COPY2} || fail "copy failed" 127 cmp ${DATA} ${COPY2} || fail "corrupted copy" 128 129 verbose "$tag: unmatched glob dir recursive local->remote" 130 scpclean 131 rm -rf ${DIR3} 132 cp ${DATA} ${DIR}/copy 133 cp ${DATA} ${DIR}/copy.glob[1234] 134 $SCP $scpopts -r ${DIR} somehost:${DIR3} || fail "copy failed" 135 diff ${DIFFOPT} ${DIR} ${DIR3} || fail "corrupted copy" 136 137 verbose "$tag: unmatched glob dir recursive remote->local" 138 # NB. no clean 139 rm -rf ${DIR2} 140 $SCP $scpopts -r somehost:${DIR3} ${DIR2} || fail "copy failed" 141 diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 142 143 verbose "$tag: shell metacharacters" 144 scpclean 145 (cd ${DIR} && \ 146 touch '`touch metachartest`' && \ 147 $SCP $scpopts *metachar* ${DIR2} 2>/dev/null; \ 148 [ ! -f metachartest ] ) || fail "shell metacharacters" 149 150 if [ ! -z "$SUDO" ]; then 151 verbose "$tag: skipped file after scp -p with failed chown+utimes" 152 scpclean 153 cp -p ${DATA} ${DIR}/copy 154 cp -p ${DATA} ${DIR}/copy2 155 cp ${DATA} ${DIR2}/copy 156 chmod 660 ${DIR2}/copy 157 $SUDO chown root ${DIR2}/copy 158 $SCP -p $scpopts somehost:${DIR}/\* ${DIR2} >/dev/null 2>&1 159 $SUDO diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 160 $SUDO rm ${DIR2}/copy 161 fi 162 163 for i in 0 1 2 3 4 5 6 7; do 164 verbose "$tag: disallow bad server #$i" 165 SCPTESTMODE=badserver_$i 166 export DIR SCPTESTMODE 167 scpclean 168 $SCP $scpopts somehost:${DATA} ${DIR} >/dev/null 2>/dev/null 169 [ -d {$DIR}/rootpathdir ] && fail "allows dir relative to root dir" 170 [ -d ${DIR}/dotpathdir ] && fail "allows dir creation in non-recursive mode" 171 172 scpclean 173 $SCP -r $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 174 [ -d ${DIR}/dotpathdir ] && fail "allows dir creation outside of subdir" 175 176 scpclean 177 $SCP -pr $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 178 [ ! -w ${DIR2} ] && fail "allows target root attribute change" 179 180 scpclean 181 $SCP $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 182 [ -e ${DIR2}/extrafile ] && fail "allows unauth object creation" 183 rm -f ${DIR2}/extrafile 184 done 185 186 verbose "$tag: detect non-directory target" 187 scpclean 188 echo a > ${COPY} 189 echo b > ${COPY2} 190 $SCP $scpopts ${DATA} ${COPY} ${COPY2} 191 cmp ${COPY} ${COPY2} >/dev/null && fail "corrupt target" 192done 193 194scpclean 195rm -f ${OBJ}/scp-ssh-wrapper.scp 196