xref: /freebsd/crypto/openssh/regress/percent.sh (revision 535af610a4fdace6d50960c0ad9be0597eea7a1b)
1*535af610SEd Maste#	$OpenBSD: percent.sh,v 1.17 2023/03/27 03:56:50 dtucker Exp $
219261079SEd Maste#	Placed in the Public Domain.
319261079SEd Maste
419261079SEd Mastetid="percent expansions"
519261079SEd Maste
619261079SEd Masteif [ -x "/usr/xpg4/bin/id" ]; then
719261079SEd Maste	PATH=/usr/xpg4/bin:$PATH
819261079SEd Maste	export PATH
919261079SEd Mastefi
1019261079SEd Maste
1119261079SEd MasteUSER=`id -u -n`
1219261079SEd MasteUSERID=`id -u`
1319261079SEd MasteHOST=`hostname | cut -f1 -d.`
1419261079SEd MasteHOSTNAME=`hostname`
15f374ba41SEd MasteHASH=""
1619261079SEd Maste
1719261079SEd Maste# Localcommand is evaluated after connection because %T is not available
1819261079SEd Maste# until then.  Because of this we use a different method of exercising it,
1919261079SEd Maste# and we can't override the remote user otherwise authentication will fail.
2019261079SEd Maste# We also have to explicitly enable it.
2119261079SEd Masteecho "permitlocalcommand yes" >> $OBJ/ssh_proxy
2219261079SEd Maste
2319261079SEd Mastetrial()
2419261079SEd Maste{
251323ec57SEd Maste	opt="$1"; arg="$2"
261323ec57SEd Maste	expect=`echo "$3" | sed 's|^//|/|'` # approximate realpath
2719261079SEd Maste
2819261079SEd Maste	trace "test $opt=$arg $expect"
2919261079SEd Maste	rm -f $OBJ/actual
3019261079SEd Maste	got=""
3119261079SEd Maste	case "$opt" in
3219261079SEd Maste	localcommand)
3319261079SEd Maste		${SSH} -F $OBJ/ssh_proxy -o $opt="echo '$arg' >$OBJ/actual" \
3419261079SEd Maste		    somehost true
3519261079SEd Maste		got=`cat $OBJ/actual`
3619261079SEd Maste		;;
3719261079SEd Maste	userknownhostsfile)
3819261079SEd Maste		# Move the userknownhosts file to what the expansion says,
3919261079SEd Maste		# make sure ssh works then put it back.
4019261079SEd Maste		mv "$OBJ/known_hosts" "$OBJ/$expect"
4119261079SEd Maste		${SSH} -F $OBJ/ssh_proxy -o $opt="$OBJ/$arg" somehost true && \
4219261079SEd Maste			got="$expect"
4319261079SEd Maste		mv "$OBJ/$expect" "$OBJ/known_hosts"
4419261079SEd Maste		;;
4519261079SEd Maste	matchexec)
4619261079SEd Maste		(cat $OBJ/ssh_proxy && \
4719261079SEd Maste		 echo "Match Exec \"echo '$arg' >$OBJ/actual\"") \
4819261079SEd Maste		    >$OBJ/ssh_proxy_match
4919261079SEd Maste		${SSH} -F $OBJ/ssh_proxy_match remuser@somehost true || true
5019261079SEd Maste		got=`cat $OBJ/actual`
5119261079SEd Maste		;;
5219261079SEd Maste	*forward)
5319261079SEd Maste		# LocalForward and RemoteForward take two args and only
5419261079SEd Maste		# operate on Unix domain socket paths
5519261079SEd Maste		got=`${SSH} -F $OBJ/ssh_proxy -o $opt="/$arg /$arg" -G \
5619261079SEd Maste		    remuser@somehost | awk '$1=="'$opt'"{print $2" "$3}'`
5719261079SEd Maste		expect="/$expect /$expect"
5819261079SEd Maste		;;
5919261079SEd Maste	*)
6019261079SEd Maste		got=`${SSH} -F $OBJ/ssh_proxy -o $opt="$arg" -G \
6119261079SEd Maste		    remuser@somehost | awk '$1=="'$opt'"{print $2}'`
6219261079SEd Maste	esac
6319261079SEd Maste	if [ "$got" != "$expect" ]; then
6419261079SEd Maste		fail "$opt=$arg expect $expect got $got"
6519261079SEd Maste	fi
6619261079SEd Maste}
6719261079SEd Maste
6819261079SEd Mastefor i in matchexec localcommand remotecommand controlpath identityagent \
69*535af610SEd Maste    forwardagent localforward remoteforward revokedhostkeys \
70*535af610SEd Maste    userknownhostsfile; do
7119261079SEd Maste	verbose $tid $i percent
7219261079SEd Maste	case "$i" in
7319261079SEd Maste	localcommand|userknownhostsfile)
7419261079SEd Maste		# Any test that's going to actually make a connection needs
7519261079SEd Maste		# to use the real username.
7619261079SEd Maste		REMUSER=$USER ;;
7719261079SEd Maste	*)
7819261079SEd Maste		REMUSER=remuser ;;
7919261079SEd Maste	esac
8019261079SEd Maste	if [ "$i" = "$localcommand" ]; then
8119261079SEd Maste		trial $i '%T' NONE
8219261079SEd Maste	fi
8319261079SEd Maste	# Matches implementation in readconf.c:ssh_connection_hash()
84f374ba41SEd Maste	if [ ! -z "${OPENSSL_BIN}" ]; then
8519261079SEd Maste		HASH=`printf "${HOSTNAME}127.0.0.1${PORT}$REMUSER" |
8619261079SEd Maste		    $OPENSSL_BIN sha1 | cut -f2 -d' '`
8719261079SEd Maste		trial $i '%C' $HASH
88f374ba41SEd Maste	fi
89f374ba41SEd Maste	trial $i '%%' '%'
9019261079SEd Maste	trial $i '%i' $USERID
9119261079SEd Maste	trial $i '%h' 127.0.0.1
9219261079SEd Maste	trial $i '%L' $HOST
9319261079SEd Maste	trial $i '%l' $HOSTNAME
9419261079SEd Maste	trial $i '%n' somehost
9519261079SEd Maste	trial $i '%k' localhost-with-alias
9619261079SEd Maste	trial $i '%p' $PORT
9719261079SEd Maste	trial $i '%r' $REMUSER
9819261079SEd Maste	trial $i '%u' $USER
9919261079SEd Maste	# We can't specify a full path outside the regress dir, so skip tests
10019261079SEd Maste	# containing %d for UserKnownHostsFile
10119261079SEd Maste	if [ "$i" != "userknownhostsfile" ]; then
10219261079SEd Maste		trial $i '%d' $HOME
103f374ba41SEd Maste		in='%%/%i/%h/%d/%L/%l/%n/%p/%r/%u'
104f374ba41SEd Maste		out="%/$USERID/127.0.0.1/$HOME/$HOST/$HOSTNAME/somehost/$PORT/$REMUSER/$USER"
105f374ba41SEd Maste		if [ ! -z "${HASH}" ]; then
106f374ba41SEd Maste			in="$in/%C"
107f374ba41SEd Maste			out="$out/$HASH"
108f374ba41SEd Maste		fi
109f374ba41SEd Maste		trial $i "$in" "$out"
11019261079SEd Maste	fi
11119261079SEd Mastedone
11219261079SEd Maste
11319261079SEd Maste# Subset of above since we don't expand shell-style variables on anything that
11419261079SEd Maste# runs a command because the shell will expand those.
11519261079SEd Mastefor i in controlpath identityagent forwardagent localforward remoteforward \
11619261079SEd Maste    userknownhostsfile; do
11719261079SEd Maste	verbose $tid $i dollar
11819261079SEd Maste	FOO=bar
11919261079SEd Maste	export FOO
12019261079SEd Maste	trial $i '${FOO}' $FOO
12119261079SEd Mastedone
12219261079SEd Maste
12319261079SEd Maste
12419261079SEd Maste# A subset of options support tilde expansion
12519261079SEd Mastefor i in controlpath identityagent forwardagent; do
12619261079SEd Maste	verbose $tid $i tilde
12719261079SEd Maste	trial $i '~' $HOME/
12819261079SEd Maste	trial $i '~/.ssh' $HOME/.ssh
12919261079SEd Mastedone
130