xref: /freebsd/crypto/openssh/regress/sftp-chroot.sh (revision a90b9d0159070121c221b966469c3e36d912bf82)
1#	$OpenBSD: sftp-chroot.sh,v 1.9 2023/05/12 06:37:42 djm Exp $
2#	Placed in the Public Domain.
3
4tid="sftp in chroot"
5
6CHROOT=/var/run
7FILENAME=testdata_${USER}.$$
8PRIVDATA=${CHROOT}/${FILENAME}
9trap "${SUDO} rm -f ${PRIVDATA}" 0
10
11if [ -z "$SUDO" -a ! -w /var/run ]; then
12	skip "need SUDO to create file in /var/run, test won't work without"
13fi
14
15if ! $OBJ/check-perm -m chroot "$CHROOT" ; then
16  skip "$CHROOT is unsuitable as ChrootDirectory"
17fi
18
19$SUDO sh -c "echo mekmitastdigoat > $PRIVDATA" || \
20	fatal "create $PRIVDATA failed"
21
22echo "ForceCommand internal-sftp -d /" >> $OBJ/sshd_config
23
24start_sshd -oChrootDirectory=$CHROOT
25
26verbose "test $tid: get"
27${SFTP} -S "$SSH" -F $OBJ/ssh_config host:/${FILENAME} $COPY \
28    >>$TEST_REGRESS_LOGFILE 2>&1 || \
29	fatal "Fetch ${FILENAME} failed"
30cmp $PRIVDATA $COPY || fail "$PRIVDATA $COPY differ"
31
32stop_sshd
33
34verbose "test $tid: match"
35cat << EOF >> $OBJ/sshd_config
36Match All
37	ChrootDirectory $CHROOT
38EOF
39start_sshd
40$SUDO sh -c "echo orpheanbeholder > $PRIVDATA" || \
41	fatal "create $PRIVDATA failed"
42${SFTP} -S "$SSH" -F $OBJ/ssh_config host:/${FILENAME} $COPY \
43    >>$TEST_REGRESS_LOGFILE 2>&1 || \
44	fatal "Fetch ${FILENAME} failed"
45cmp $PRIVDATA $COPY || fail "$PRIVDATA $COPY differ"
46
47stop_sshd
48