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