1#	$OpenBSD: scp.sh,v 1.19 2023/09/08 05:50:57 djm Exp $
2#	Placed in the Public Domain.
3
4tid="scp"
5
6#set -x
7
8COPY2=${OBJ}/copy2
9DIR=${COPY}.dd
10DIR2=${COPY}.dd2
11COPY3=${OBJ}/copy.glob[123]
12DIR3=${COPY}.dd.glob[456]
13DIFFOPT="-rN"
14
15maybe_add_scp_path_to_sshd
16
17SRC=`dirname ${SCRIPT}`
18cp ${SRC}/scp-ssh-wrapper.sh ${OBJ}/scp-ssh-wrapper.scp
19chmod 755 ${OBJ}/scp-ssh-wrapper.scp
20export SCP # used in scp-ssh-wrapper.scp
21
22scpclean() {
23	rm -rf ${COPY} ${COPY2} ${DIR} ${DIR2} ${COPY3} ${DIR3}
24	mkdir ${DIR} ${DIR2} ${DIR3}
25	chmod 755 ${DIR} ${DIR2} ${DIR3}
26}
27
28# Create directory structure for recursive copy tests.
29forest() {
30	scpclean
31	rm -rf ${DIR2}
32	cp ${DATA} ${DIR}/copy
33	ln -s ${DIR}/copy ${DIR}/copy-sym
34	mkdir ${DIR}/subdir
35	cp ${DATA} ${DIR}/subdir/copy
36	ln -s ${DIR}/subdir ${DIR}/subdir-sym
37}
38
39for mode in scp sftp ; do
40	tag="$tid: $mode mode"
41	if test $mode = scp ; then
42		scpopts="-O -q -S ${OBJ}/scp-ssh-wrapper.scp"
43	else
44		scpopts="-qs -D ${SFTPSERVER}"
45	fi
46
47	verbose "$tag: simple copy local file to local file"
48	scpclean
49	$SCP $scpopts ${DATA} ${COPY} || fail "copy failed"
50	cmp ${DATA} ${COPY} || fail "corrupted copy"
51
52	verbose "$tag: simple copy local file to remote file"
53	scpclean
54	$SCP $scpopts ${DATA} somehost:${COPY} || fail "copy failed"
55	cmp ${DATA} ${COPY} || fail "corrupted copy"
56
57	verbose "$tag: simple copy remote file to local file"
58	scpclean
59	$SCP $scpopts somehost:${DATA} ${COPY} || fail "copy failed"
60	cmp ${DATA} ${COPY} || fail "corrupted copy"
61
62	verbose "$tag: copy local file to remote file in place"
63	scpclean
64	cp ${DATA} ${COPY}
65	$SCP $scpopts ${COPY} somehost:${COPY} || fail "copy failed"
66	cmp ${DATA} ${COPY} || fail "corrupted copy"
67
68	verbose "$tag: copy remote file to local file in place"
69	scpclean
70	cp ${DATA} ${COPY}
71	$SCP $scpopts somehost:${COPY} ${COPY} || fail "copy failed"
72	cmp ${DATA} ${COPY} || fail "corrupted copy"
73
74	verbose "$tag: copy local file to remote file clobber"
75	scpclean
76	cat ${DATA} ${DATA} > ${COPY}
77	$SCP $scpopts ${DATA} somehost:${COPY} || fail "copy failed"
78	ls -l $DATA $COPY
79	cmp ${DATA} ${COPY} || fail "corrupted copy"
80
81	verbose "$tag: copy remote file to local file clobber"
82	scpclean
83	cat ${DATA} ${DATA} > ${COPY}
84	$SCP $scpopts somehost:${DATA} ${COPY} || fail "copy failed"
85	cmp ${DATA} ${COPY} || fail "corrupted copy"
86
87	verbose "$tag: simple copy local file to remote dir"
88	scpclean
89	cp ${DATA} ${COPY}
90	$SCP $scpopts ${COPY} somehost:${DIR} || fail "copy failed"
91	cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
92
93	verbose "$tag: simple copy local file to local dir"
94	scpclean
95	cp ${DATA} ${COPY}
96	$SCP $scpopts ${COPY} ${DIR} || fail "copy failed"
97	cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
98
99	verbose "$tag: simple copy remote file to local dir"
100	scpclean
101	cp ${DATA} ${COPY}
102	$SCP $scpopts somehost:${COPY} ${DIR} || fail "copy failed"
103	cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
104
105	verbose "$tag: recursive local dir to remote dir"
106	forest
107	$SCP $scpopts -r ${DIR} somehost:${DIR2} || fail "copy failed"
108	diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
109
110	verbose "$tag: recursive local dir to local dir"
111	forest
112	rm -rf ${DIR2}
113	cp ${DATA} ${DIR}/copy
114	$SCP $scpopts -r ${DIR} ${DIR2} || fail "copy failed"
115	diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
116
117	verbose "$tag: recursive remote dir to local dir"
118	forest
119	rm -rf ${DIR2}
120	cp ${DATA} ${DIR}/copy
121	$SCP $scpopts -r somehost:${DIR} ${DIR2} || fail "copy failed"
122	diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
123
124	verbose "$tag: unmatched glob file local->remote"
125	scpclean
126	$SCP $scpopts ${DATA} somehost:${COPY3} || fail "copy failed"
127	cmp ${DATA} ${COPY3} || fail "corrupted copy"
128
129	verbose "$tag: unmatched glob file remote->local"
130	# NB. no clean
131	$SCP $scpopts somehost:${COPY3} ${COPY2} || fail "copy failed"
132	cmp ${DATA} ${COPY2} || fail "corrupted copy"
133
134	verbose "$tag: unmatched glob dir recursive local->remote"
135	scpclean
136	rm -rf ${DIR3}
137	cp ${DATA} ${DIR}/copy
138	cp ${DATA} ${DIR}/copy.glob[1234]
139	$SCP $scpopts -r ${DIR} somehost:${DIR3} || fail "copy failed"
140	diff ${DIFFOPT} ${DIR} ${DIR3} || fail "corrupted copy"
141
142	verbose "$tag: unmatched glob dir recursive remote->local"
143	# NB. no clean
144	rm -rf ${DIR2}
145	$SCP $scpopts -r somehost:${DIR3} ${DIR2} || fail "copy failed"
146	diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
147
148	verbose "$tag: shell metacharacters"
149	scpclean
150	(cd ${DIR} && \
151	 touch '`touch metachartest`' && \
152	 $SCP $scpopts *metachar* ${DIR2} 2>/dev/null; \
153	 [ ! -f metachartest ] ) || fail "shell metacharacters"
154
155	if [ ! -z "$SUDO" ]; then
156		verbose "$tag: skipped file after scp -p with failed chown+utimes"
157		scpclean
158		cp -p ${DATA} ${DIR}/copy
159		cp -p ${DATA} ${DIR}/copy2
160		cp ${DATA} ${DIR2}/copy
161		chmod 660 ${DIR2}/copy
162		$SUDO chown root ${DIR2}/copy
163		$SCP -p $scpopts somehost:${DIR}/\* ${DIR2} >/dev/null 2>&1
164		$SUDO diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
165		$SUDO rm ${DIR2}/copy
166	fi
167
168	for i in 0 1 2 3 4 5 6 7; do
169		verbose "$tag: disallow bad server #$i"
170		SCPTESTMODE=badserver_$i
171		export DIR SCPTESTMODE
172		scpclean
173		$SCP $scpopts somehost:${DATA} ${DIR} >/dev/null 2>/dev/null
174		[ -d {$DIR}/rootpathdir ] && fail "allows dir relative to root dir"
175		[ -d ${DIR}/dotpathdir ] && fail "allows dir creation in non-recursive mode"
176
177		scpclean
178		$SCP -r $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null
179		[ -d ${DIR}/dotpathdir ] && fail "allows dir creation outside of subdir"
180
181		scpclean
182		$SCP -pr $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null
183		[ ! -w ${DIR2} ] && fail "allows target root attribute change"
184
185		scpclean
186		$SCP $scpopts somehost:${DATA} ${DIR2} >/dev/null 2>/dev/null
187		[ -e ${DIR2}/extrafile ] && fail "allows unauth object creation"
188		rm -f ${DIR2}/extrafile
189	done
190
191	verbose "$tag: detect non-directory target"
192	scpclean
193	echo a > ${COPY}
194	echo b > ${COPY2}
195	$SCP $scpopts ${DATA} ${COPY} ${COPY2}
196	cmp ${COPY} ${COPY2} >/dev/null && fail "corrupt target"
197done
198
199scpclean
200rm -f ${OBJ}/scp-ssh-wrapper.exe
201