scp.sh revision 255670
1255670Sdes# $OpenBSD: scp.sh,v 1.9 2013/05/17 10:35:43 dtucker Exp $ 2137015Sdes# Placed in the Public Domain. 3137015Sdes 4137015Sdestid="scp" 5137015Sdes 6137015Sdes#set -x 7137015Sdes 8137015Sdes# Figure out if diff understands "-N" 9137015Sdesif diff -N ${SRC}/scp.sh ${SRC}/scp.sh 2>/dev/null; then 10137015Sdes DIFFOPT="-rN" 11137015Sdeselse 12137015Sdes DIFFOPT="-r" 13137015Sdesfi 14137015Sdes 15137015SdesCOPY2=${OBJ}/copy2 16137015SdesDIR=${COPY}.dd 17137015SdesDIR2=${COPY}.dd2 18137015Sdes 19137015SdesSRC=`dirname ${SCRIPT}` 20146998Sdescp ${SRC}/scp-ssh-wrapper.sh ${OBJ}/scp-ssh-wrapper.scp 21146998Sdeschmod 755 ${OBJ}/scp-ssh-wrapper.scp 22146998Sdesscpopts="-q -S ${OBJ}/scp-ssh-wrapper.scp" 23137015Sdes 24137015Sdesscpclean() { 25137015Sdes rm -rf ${COPY} ${COPY2} ${DIR} ${DIR2} 26137015Sdes mkdir ${DIR} ${DIR2} 27137015Sdes} 28137015Sdes 29157016Sdesverbose "$tid: simple copy local file to local file" 30157016Sdesscpclean 31157016Sdes$SCP $scpopts ${DATA} ${COPY} || fail "copy failed" 32157016Sdescmp ${DATA} ${COPY} || fail "corrupted copy" 33157016Sdes 34137015Sdesverbose "$tid: simple copy local file to remote file" 35137015Sdesscpclean 36137015Sdes$SCP $scpopts ${DATA} somehost:${COPY} || fail "copy failed" 37137015Sdescmp ${DATA} ${COPY} || fail "corrupted copy" 38137015Sdes 39137015Sdesverbose "$tid: simple copy remote file to local file" 40137015Sdesscpclean 41137015Sdes$SCP $scpopts somehost:${DATA} ${COPY} || fail "copy failed" 42137015Sdescmp ${DATA} ${COPY} || fail "corrupted copy" 43137015Sdes 44137015Sdesverbose "$tid: simple copy local file to remote dir" 45137015Sdesscpclean 46137015Sdescp ${DATA} ${COPY} 47137015Sdes$SCP $scpopts ${COPY} somehost:${DIR} || fail "copy failed" 48137015Sdescmp ${COPY} ${DIR}/copy || fail "corrupted copy" 49137015Sdes 50157016Sdesverbose "$tid: simple copy local file to local dir" 51157016Sdesscpclean 52157016Sdescp ${DATA} ${COPY} 53157016Sdes$SCP $scpopts ${COPY} ${DIR} || fail "copy failed" 54157016Sdescmp ${COPY} ${DIR}/copy || fail "corrupted copy" 55157016Sdes 56137015Sdesverbose "$tid: simple copy remote file to local dir" 57137015Sdesscpclean 58137015Sdescp ${DATA} ${COPY} 59137015Sdes$SCP $scpopts somehost:${COPY} ${DIR} || fail "copy failed" 60137015Sdescmp ${COPY} ${DIR}/copy || fail "corrupted copy" 61137015Sdes 62137015Sdesverbose "$tid: recursive local dir to remote dir" 63137015Sdesscpclean 64137015Sdesrm -rf ${DIR2} 65137015Sdescp ${DATA} ${DIR}/copy 66137015Sdes$SCP $scpopts -r ${DIR} somehost:${DIR2} || fail "copy failed" 67137015Sdesdiff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 68137015Sdes 69157016Sdesverbose "$tid: recursive local dir to local dir" 70157016Sdesscpclean 71157016Sdesrm -rf ${DIR2} 72157016Sdescp ${DATA} ${DIR}/copy 73157016Sdes$SCP $scpopts -r ${DIR} ${DIR2} || fail "copy failed" 74157016Sdesdiff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 75157016Sdes 76137015Sdesverbose "$tid: recursive remote dir to local dir" 77137015Sdesscpclean 78137015Sdesrm -rf ${DIR2} 79137015Sdescp ${DATA} ${DIR}/copy 80137015Sdes$SCP $scpopts -r somehost:${DIR} ${DIR2} || fail "copy failed" 81137015Sdesdiff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 82137015Sdes 83157016Sdesverbose "$tid: shell metacharacters" 84157016Sdesscpclean 85157016Sdes(cd ${DIR} && \ 86157016Sdestouch '`touch metachartest`' && \ 87157016Sdes$SCP $scpopts *metachar* ${DIR2} 2>/dev/null; \ 88157016Sdes[ ! -f metachartest ] ) || fail "shell metacharacters" 89157016Sdes 90146998Sdesif [ ! -z "$SUDO" ]; then 91146998Sdes verbose "$tid: skipped file after scp -p with failed chown+utimes" 92146998Sdes scpclean 93146998Sdes cp -p ${DATA} ${DIR}/copy 94146998Sdes cp -p ${DATA} ${DIR}/copy2 95146998Sdes cp ${DATA} ${DIR2}/copy 96146998Sdes chmod 660 ${DIR2}/copy 97146998Sdes $SUDO chown root ${DIR2}/copy 98146998Sdes $SCP -p $scpopts somehost:${DIR}/\* ${DIR2} >/dev/null 2>&1 99157016Sdes $SUDO diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy" 100146998Sdes $SUDO rm ${DIR2}/copy 101146998Sdesfi 102146998Sdes 103137015Sdesfor i in 0 1 2 3 4; do 104137015Sdes verbose "$tid: disallow bad server #$i" 105137015Sdes SCPTESTMODE=badserver_$i 106137015Sdes export DIR SCPTESTMODE 107137015Sdes scpclean 108137015Sdes $SCP $scpopts somehost:${DATA} ${DIR} >/dev/null 2>/dev/null 109137015Sdes [ -d {$DIR}/rootpathdir ] && fail "allows dir relative to root dir" 110137015Sdes [ -d ${DIR}/dotpathdir ] && fail "allows dir creation in non-recursive mode" 111137015Sdes 112137015Sdes scpclean 113137015Sdes $SCP -r $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null 114137015Sdes [ -d ${DIR}/dotpathdir ] && fail "allows dir creation outside of subdir" 115137015Sdesdone 116137015Sdes 117157016Sdesverbose "$tid: detect non-directory target" 118137015Sdesscpclean 119157016Sdesecho a > ${COPY} 120157016Sdesecho b > ${COPY2} 121157016Sdes$SCP $scpopts ${DATA} ${COPY} ${COPY2} 122157016Sdescmp ${COPY} ${COPY2} >/dev/null && fail "corrupt target" 123157016Sdes 124157016Sdesscpclean 125146998Sdesrm -f ${OBJ}/scp-ssh-wrapper.scp 126