xref: /openssh-portable/regress/scp.sh (revision b4a7c9d2)
1#	$OpenBSD: scp.sh,v 1.11 2019/07/19 03:45:44 djm 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	chmod 755 ${DIR} ${DIR2}
29}
30
31verbose "$tid: simple copy local file to local file"
32scpclean
33$SCP $scpopts ${DATA} ${COPY} || fail "copy failed"
34cmp ${DATA} ${COPY} || fail "corrupted copy"
35
36verbose "$tid: simple copy local file to remote file"
37scpclean
38$SCP $scpopts ${DATA} somehost:${COPY} || fail "copy failed"
39cmp ${DATA} ${COPY} || fail "corrupted copy"
40
41verbose "$tid: simple copy remote file to local file"
42scpclean
43$SCP $scpopts somehost:${DATA} ${COPY} || fail "copy failed"
44cmp ${DATA} ${COPY} || fail "corrupted copy"
45
46verbose "$tid: simple copy local file to remote dir"
47scpclean
48cp ${DATA} ${COPY}
49$SCP $scpopts ${COPY} somehost:${DIR} || fail "copy failed"
50cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
51
52verbose "$tid: simple copy local file to local dir"
53scpclean
54cp ${DATA} ${COPY}
55$SCP $scpopts ${COPY} ${DIR} || fail "copy failed"
56cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
57
58verbose "$tid: simple copy remote file to local dir"
59scpclean
60cp ${DATA} ${COPY}
61$SCP $scpopts somehost:${COPY} ${DIR} || fail "copy failed"
62cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
63
64verbose "$tid: recursive local dir to remote dir"
65scpclean
66rm -rf ${DIR2}
67cp ${DATA} ${DIR}/copy
68$SCP $scpopts -r ${DIR} somehost:${DIR2} || fail "copy failed"
69diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
70
71verbose "$tid: recursive local dir to local dir"
72scpclean
73rm -rf ${DIR2}
74cp ${DATA} ${DIR}/copy
75$SCP $scpopts -r ${DIR} ${DIR2} || fail "copy failed"
76diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
77
78verbose "$tid: recursive remote dir to local dir"
79scpclean
80rm -rf ${DIR2}
81cp ${DATA} ${DIR}/copy
82$SCP $scpopts -r somehost:${DIR} ${DIR2} || fail "copy failed"
83diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
84
85verbose "$tid: shell metacharacters"
86scpclean
87(cd ${DIR} && \
88touch '`touch metachartest`' && \
89$SCP $scpopts *metachar* ${DIR2} 2>/dev/null; \
90[ ! -f metachartest ] ) || fail "shell metacharacters"
91
92if [ ! -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
101	$SUDO diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
102	$SUDO rm ${DIR2}/copy
103fi
104
105for i in 0 1 2 3 4 5 6 7; 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
118	scpclean
119	$SCP -pr $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null
120	[ ! -w ${DIR2} ] && fail "allows target root attribute change"
121
122	scpclean
123	$SCP $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null
124	[ -e ${DIR2}/extrafile ] && fail "allows unauth object creation"
125	rm -f ${DIR2}/extrafile
126done
127
128verbose "$tid: detect non-directory target"
129scpclean
130echo a > ${COPY}
131echo b > ${COPY2}
132$SCP $scpopts ${DATA} ${COPY} ${COPY2}
133cmp ${COPY} ${COPY2} >/dev/null && fail "corrupt target"
134
135scpclean
136rm -f ${OBJ}/scp-ssh-wrapper.scp
137