scp.sh revision 262566
1260847Sbryanv#	$OpenBSD: scp.sh,v 1.9 2013/05/17 10:35:43 dtucker Exp $
2260847Sbryanv#	Placed in the Public Domain.
3260847Sbryanv
4260847Sbryanvtid="scp"
5260847Sbryanv
6260847Sbryanv#set -x
7260847Sbryanv
8260847Sbryanv# Figure out if diff understands "-N"
9260847Sbryanvif diff -N ${SRC}/scp.sh ${SRC}/scp.sh 2>/dev/null; then
10260847Sbryanv	DIFFOPT="-rN"
11260847Sbryanvelse
12260847Sbryanv	DIFFOPT="-r"
13260847Sbryanvfi
14260847Sbryanv
15260847SbryanvCOPY2=${OBJ}/copy2
16260847SbryanvDIR=${COPY}.dd
17260847SbryanvDIR2=${COPY}.dd2
18260847Sbryanv
19260847SbryanvSRC=`dirname ${SCRIPT}`
20260847Sbryanvcp ${SRC}/scp-ssh-wrapper.sh ${OBJ}/scp-ssh-wrapper.scp
21260847Sbryanvchmod 755 ${OBJ}/scp-ssh-wrapper.scp
22260847Sbryanvscpopts="-q -S ${OBJ}/scp-ssh-wrapper.scp"
23260847Sbryanvexport SCP # used in scp-ssh-wrapper.scp
24260847Sbryanv
25260847Sbryanvscpclean() {
26260847Sbryanv	rm -rf ${COPY} ${COPY2} ${DIR} ${DIR2}
27260847Sbryanv	mkdir ${DIR} ${DIR2}
28260847Sbryanv}
29260847Sbryanv
30260847Sbryanvverbose "$tid: simple copy local file to local file"
31260847Sbryanvscpclean
32260847Sbryanv$SCP $scpopts ${DATA} ${COPY} || fail "copy failed"
33260847Sbryanvcmp ${DATA} ${COPY} || fail "corrupted copy"
34260847Sbryanv
35260847Sbryanvverbose "$tid: simple copy local file to remote file"
36260847Sbryanvscpclean
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