blob: c2da2a8625432fe872b19536eae125fc7dc30866 [file] [log] [blame]
Darren Tucker9b42d322013-05-17 20:48:59 +10001# $OpenBSD: scp.sh,v 1.9 2013/05/17 10:35:43 dtucker Exp $
Darren Tucker50433a92004-06-16 20:15:59 +10002# Placed in the Public Domain.
3
4tid="scp"
5
6#set -x
7
Darren Tucker8a2f1b32004-06-17 15:18:32 +10008# Figure out if diff understands "-N"
9if diff -N ${SRC}/scp.sh ${SRC}/scp.sh 2>/dev/null; then
10 DIFFOPT="-rN"
11else
12 DIFFOPT="-r"
13fi
14
Darren Tucker50433a92004-06-16 20:15:59 +100015COPY2=${OBJ}/copy2
16DIR=${COPY}.dd
17DIR2=${COPY}.dd2
18
19SRC=`dirname ${SCRIPT}`
Darren Tucker0521dcb2004-08-29 19:39:09 +100020cp ${SRC}/scp-ssh-wrapper.sh ${OBJ}/scp-ssh-wrapper.scp
21chmod 755 ${OBJ}/scp-ssh-wrapper.scp
22scpopts="-q -S ${OBJ}/scp-ssh-wrapper.scp"
Damien Millerb0e0f762014-01-24 14:27:04 +110023export SCP # used in scp-ssh-wrapper.scp
Darren Tucker50433a92004-06-16 20:15:59 +100024
25scpclean() {
26 rm -rf ${COPY} ${COPY2} ${DIR} ${DIR2}
27 mkdir ${DIR} ${DIR2}
28}
29
Damien Miller0b996462006-01-31 22:05:23 +110030verbose "$tid: simple copy local file to local file"
31scpclean
32$SCP $scpopts ${DATA} ${COPY} || fail "copy failed"
33cmp ${DATA} ${COPY} || fail "corrupted copy"
34
Darren Tucker50433a92004-06-16 20:15:59 +100035verbose "$tid: simple copy local file to remote file"
36scpclean
37$SCP $scpopts ${DATA} somehost:${COPY} || fail "copy failed"
38cmp ${DATA} ${COPY} || fail "corrupted copy"
39
40verbose "$tid: simple copy remote file to local file"
41scpclean
42$SCP $scpopts somehost:${DATA} ${COPY} || fail "copy failed"
43cmp ${DATA} ${COPY} || fail "corrupted copy"
44
45verbose "$tid: simple copy local file to remote dir"
46scpclean
47cp ${DATA} ${COPY}
48$SCP $scpopts ${COPY} somehost:${DIR} || fail "copy failed"
49cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
50
Damien Miller0b996462006-01-31 22:05:23 +110051verbose "$tid: simple copy local file to local dir"
52scpclean
53cp ${DATA} ${COPY}
54$SCP $scpopts ${COPY} ${DIR} || fail "copy failed"
55cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
56
Darren Tucker50433a92004-06-16 20:15:59 +100057verbose "$tid: simple copy remote file to local dir"
58scpclean
59cp ${DATA} ${COPY}
60$SCP $scpopts somehost:${COPY} ${DIR} || fail "copy failed"
61cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
62
63verbose "$tid: recursive local dir to remote dir"
64scpclean
Darren Tucker6f0e35b2004-06-16 23:22:37 +100065rm -rf ${DIR2}
Darren Tucker50433a92004-06-16 20:15:59 +100066cp ${DATA} ${DIR}/copy
67$SCP $scpopts -r ${DIR} somehost:${DIR2} || fail "copy failed"
Darren Tucker8a2f1b32004-06-17 15:18:32 +100068diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
Darren Tucker50433a92004-06-16 20:15:59 +100069
Damien Miller0b996462006-01-31 22:05:23 +110070verbose "$tid: recursive local dir to local dir"
71scpclean
72rm -rf ${DIR2}
73cp ${DATA} ${DIR}/copy
74$SCP $scpopts -r ${DIR} ${DIR2} || fail "copy failed"
75diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
76
Darren Tucker50433a92004-06-16 20:15:59 +100077verbose "$tid: recursive remote dir to local dir"
78scpclean
Darren Tucker6f0e35b2004-06-16 23:22:37 +100079rm -rf ${DIR2}
Darren Tucker50433a92004-06-16 20:15:59 +100080cp ${DATA} ${DIR}/copy
81$SCP $scpopts -r somehost:${DIR} ${DIR2} || fail "copy failed"
Darren Tucker8a2f1b32004-06-17 15:18:32 +100082diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
Darren Tucker50433a92004-06-16 20:15:59 +100083
Damien Miller7410ad72006-01-31 22:06:14 +110084verbose "$tid: shell metacharacters"
85scpclean
86(cd ${DIR} && \
87touch '`touch metachartest`' && \
88$SCP $scpopts *metachar* ${DIR2} 2>/dev/null; \
89[ ! -f metachartest ] ) || fail "shell metacharacters"
90
Darren Tuckerccf07792004-12-06 23:03:27 +110091if [ ! -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 Miller10c5fa72006-01-31 22:01:42 +1100100 $SUDO diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
Darren Tuckerccf07792004-12-06 23:03:27 +1100101 $SUDO rm ${DIR2}/copy
102fi
103
Darren Tucker50433a92004-06-16 20:15:59 +1000104for 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"
116done
117
Damien Miller50c6eed2006-01-31 22:06:41 +1100118verbose "$tid: detect non-directory target"
119scpclean
120echo a > ${COPY}
121echo b > ${COPY2}
122$SCP $scpopts ${DATA} ${COPY} ${COPY2}
123cmp ${COPY} ${COPY2} >/dev/null && fail "corrupt target"
124
Darren Tucker50433a92004-06-16 20:15:59 +1000125scpclean
Darren Tucker0521dcb2004-08-29 19:39:09 +1000126rm -f ${OBJ}/scp-ssh-wrapper.scp