scp.sh revision 262566
1#	$OpenBSD: scp.sh,v 1.9 2013/05/17 10:35:43 dtucker Exp $
2#	Placed in the Public Domain.
3
4tid="scp"
5
6#set -x
7
8# 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
15COPY2=${OBJ}/copy2
16DIR=${COPY}.dd
17DIR2=${COPY}.dd2
18
19SRC=`dirname ${SCRIPT}`
20cp ${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"
23export SCP # used in scp-ssh-wrapper.scp
24
25scpclean() {
26	rm -rf ${COPY} ${COPY2} ${DIR} ${DIR2}
27	mkdir ${DIR} ${DIR2}
28}
29
30verbose "$tid: simple copy local file to local file"
31scpclean
32$SCP $scpopts ${DATA} ${COPY} || fail "copy failed"
33cmp ${DATA} ${COPY} || fail "corrupted copy"
34
35verbose "$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
51verbose "$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
57verbose "$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
65rm -rf ${DIR2}
66cp ${DATA} ${DIR}/copy
67$SCP $scpopts -r ${DIR} somehost:${DIR2} || fail "copy failed"
68diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
69
70verbose "$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
77verbose "$tid: recursive remote dir to local dir"
78scpclean
79rm -rf ${DIR2}
80cp ${DATA} ${DIR}/copy
81$SCP $scpopts -r somehost:${DIR} ${DIR2} || fail "copy failed"
82diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
83
84verbose "$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
91if [ ! -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
100	$SUDO diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
101	$SUDO rm ${DIR2}/copy
102fi
103
104for 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
118verbose "$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
125scpclean
126rm -f ${OBJ}/scp-ssh-wrapper.scp
127