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