1#	$OpenBSD: sftp-uri.sh,v 1.1 2017/10/24 19:33:32 millert Exp $
2#	Placed in the Public Domain.
3
4tid="sftp-uri"
5
6#set -x
7
8COPY2=${OBJ}/copy2
9DIR=${COPY}.dd
10DIR2=${COPY}.dd2
11SRC=`dirname ${SCRIPT}`
12
13sftpclean() {
14	rm -rf ${COPY} ${COPY2} ${DIR} ${DIR2}
15	mkdir ${DIR} ${DIR2}
16}
17
18start_sshd -oForceCommand="internal-sftp -d /"
19
20# Remove Port and User from ssh_config, we want to rely on the URI
21cp $OBJ/ssh_config $OBJ/ssh_config.orig
22egrep -v '^	+(Port|User)	+.*$' $OBJ/ssh_config.orig > $OBJ/ssh_config
23
24verbose "$tid: non-interactive fetch to local file"
25sftpclean
26${SFTP} -q -S "$SSH" -F $OBJ/ssh_config "sftp://${USER}@somehost:${PORT}/${DATA}" ${COPY} || fail "copy failed"
27cmp ${DATA} ${COPY} || fail "corrupted copy"
28
29verbose "$tid: non-interactive fetch to local dir"
30sftpclean
31cp ${DATA} ${COPY}
32${SFTP} -q -S "$SSH" -F $OBJ/ssh_config "sftp://${USER}@somehost:${PORT}/${COPY}" ${DIR} || fail "copy failed"
33cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
34
35verbose "$tid: put to remote directory (trailing slash)"
36sftpclean
37${SFTP} -q -S "$SSH" -F $OBJ/ssh_config -b - \
38    "sftp://${USER}@somehost:${PORT}/${DIR}/" > /dev/null 2>&1 << EOF
39	version
40	put ${DATA} copy
41EOF
42r=$?
43if [ $r -ne 0 ]; then
44	fail "sftp failed with $r"
45else
46	cmp ${DATA} ${DIR}/copy || fail "corrupted copy"
47fi
48
49verbose "$tid: put to remote directory (no slash)"
50sftpclean
51${SFTP} -q -S "$SSH" -F $OBJ/ssh_config -b - \
52    "sftp://${USER}@somehost:${PORT}/${DIR}" > /dev/null 2>&1 << EOF
53	version
54	put ${DATA} copy
55EOF
56r=$?
57if [ $r -ne 0 ]; then
58	fail "sftp failed with $r"
59else
60	cmp ${DATA} ${DIR}/copy || fail "corrupted copy"
61fi
62
63sftpclean
64