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