2022-02-20 03:47:26 +00:00
|
|
|
# $OpenBSD: percent.sh,v 1.14 2022/02/20 03:47:26 dtucker Exp $
|
2020-04-03 02:33:31 +00:00
|
|
|
# Placed in the Public Domain.
|
|
|
|
|
|
|
|
tid="percent expansions"
|
|
|
|
|
2020-04-04 22:43:57 +00:00
|
|
|
if [ -x "/usr/xpg4/bin/id" ]; then
|
|
|
|
PATH=/usr/xpg4/bin:$PATH
|
|
|
|
export PATH
|
|
|
|
fi
|
|
|
|
|
2020-04-03 02:33:31 +00:00
|
|
|
USER=`id -u -n`
|
|
|
|
USERID=`id -u`
|
2021-02-05 22:03:40 +00:00
|
|
|
HOST=`hostname | cut -f1 -d.`
|
|
|
|
HOSTNAME=`hostname`
|
2020-04-03 02:33:31 +00:00
|
|
|
|
|
|
|
# Localcommand is evaluated after connection because %T is not available
|
|
|
|
# until then. Because of this we use a different method of exercising it,
|
|
|
|
# and we can't override the remote user otherwise authentication will fail.
|
|
|
|
# We also have to explicitly enable it.
|
|
|
|
echo "permitlocalcommand yes" >> $OBJ/ssh_proxy
|
|
|
|
|
|
|
|
trial()
|
|
|
|
{
|
2022-02-20 03:47:26 +00:00
|
|
|
opt="$1"; arg="$2"
|
|
|
|
expect=`echo "$3" | sed 's|^//|/|'` # approximate realpath
|
2020-04-03 02:33:31 +00:00
|
|
|
|
|
|
|
trace "test $opt=$arg $expect"
|
2020-04-03 03:14:03 +00:00
|
|
|
rm -f $OBJ/actual
|
2020-07-17 03:26:58 +00:00
|
|
|
got=""
|
2020-04-03 03:14:03 +00:00
|
|
|
case "$opt" in
|
|
|
|
localcommand)
|
2020-04-03 02:33:31 +00:00
|
|
|
${SSH} -F $OBJ/ssh_proxy -o $opt="echo '$arg' >$OBJ/actual" \
|
|
|
|
somehost true
|
|
|
|
got=`cat $OBJ/actual`
|
2020-04-03 03:14:03 +00:00
|
|
|
;;
|
2020-07-17 03:26:58 +00:00
|
|
|
userknownhostsfile)
|
|
|
|
# Move the userknownhosts file to what the expansion says,
|
|
|
|
# make sure ssh works then put it back.
|
|
|
|
mv "$OBJ/known_hosts" "$OBJ/$expect"
|
|
|
|
${SSH} -F $OBJ/ssh_proxy -o $opt="$OBJ/$arg" somehost true && \
|
|
|
|
got="$expect"
|
|
|
|
mv "$OBJ/$expect" "$OBJ/known_hosts"
|
|
|
|
;;
|
2020-04-03 03:14:03 +00:00
|
|
|
matchexec)
|
|
|
|
(cat $OBJ/ssh_proxy && \
|
|
|
|
echo "Match Exec \"echo '$arg' >$OBJ/actual\"") \
|
|
|
|
>$OBJ/ssh_proxy_match
|
|
|
|
${SSH} -F $OBJ/ssh_proxy_match remuser@somehost true || true
|
|
|
|
got=`cat $OBJ/actual`
|
|
|
|
;;
|
2020-04-10 00:54:03 +00:00
|
|
|
*forward)
|
|
|
|
# LocalForward and RemoteForward take two args and only
|
|
|
|
# operate on Unix domain socket paths
|
|
|
|
got=`${SSH} -F $OBJ/ssh_proxy -o $opt="/$arg /$arg" -G \
|
|
|
|
remuser@somehost | awk '$1=="'$opt'"{print $2" "$3}'`
|
|
|
|
expect="/$expect /$expect"
|
|
|
|
;;
|
2020-04-03 03:14:03 +00:00
|
|
|
*)
|
2020-04-03 02:33:31 +00:00
|
|
|
got=`${SSH} -F $OBJ/ssh_proxy -o $opt="$arg" -G \
|
|
|
|
remuser@somehost | awk '$1=="'$opt'"{print $2}'`
|
2020-04-03 03:14:03 +00:00
|
|
|
esac
|
2020-04-03 02:33:31 +00:00
|
|
|
if [ "$got" != "$expect" ]; then
|
|
|
|
fail "$opt=$arg expect $expect got $got"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-04-03 03:14:03 +00:00
|
|
|
for i in matchexec localcommand remotecommand controlpath identityagent \
|
2020-07-17 03:26:58 +00:00
|
|
|
forwardagent localforward remoteforward userknownhostsfile; do
|
2020-05-29 04:32:26 +00:00
|
|
|
verbose $tid $i percent
|
2020-07-17 03:26:58 +00:00
|
|
|
case "$i" in
|
|
|
|
localcommand|userknownhostsfile)
|
|
|
|
# Any test that's going to actually make a connection needs
|
|
|
|
# to use the real username.
|
|
|
|
REMUSER=$USER ;;
|
|
|
|
*)
|
|
|
|
REMUSER=remuser ;;
|
|
|
|
esac
|
|
|
|
if [ "$i" = "$localcommand" ]; then
|
2020-04-03 02:33:31 +00:00
|
|
|
trial $i '%T' NONE
|
|
|
|
fi
|
2020-04-03 07:53:10 +00:00
|
|
|
# Matches implementation in readconf.c:ssh_connection_hash()
|
|
|
|
HASH=`printf "${HOSTNAME}127.0.0.1${PORT}$REMUSER" |
|
2021-07-25 12:13:03 +00:00
|
|
|
$OPENSSL_BIN sha1 | cut -f2 -d' '`
|
2020-04-03 02:33:31 +00:00
|
|
|
trial $i '%%' '%'
|
2020-04-03 07:53:10 +00:00
|
|
|
trial $i '%C' $HASH
|
2020-04-03 02:33:31 +00:00
|
|
|
trial $i '%i' $USERID
|
|
|
|
trial $i '%h' 127.0.0.1
|
|
|
|
trial $i '%L' $HOST
|
|
|
|
trial $i '%l' $HOSTNAME
|
|
|
|
trial $i '%n' somehost
|
2020-07-17 07:10:24 +00:00
|
|
|
trial $i '%k' localhost-with-alias
|
2020-04-03 02:33:31 +00:00
|
|
|
trial $i '%p' $PORT
|
|
|
|
trial $i '%r' $REMUSER
|
|
|
|
trial $i '%u' $USER
|
2020-07-17 03:26:58 +00:00
|
|
|
# We can't specify a full path outside the regress dir, so skip tests
|
|
|
|
# containing %d for UserKnownHostsFile
|
|
|
|
if [ "$i" != "userknownhostsfile" ]; then
|
|
|
|
trial $i '%d' $HOME
|
|
|
|
trial $i '%%/%C/%i/%h/%d/%L/%l/%n/%p/%r/%u' \
|
|
|
|
"%/$HASH/$USERID/127.0.0.1/$HOME/$HOST/$HOSTNAME/somehost/$PORT/$REMUSER/$USER"
|
|
|
|
fi
|
2020-04-03 02:33:31 +00:00
|
|
|
done
|
|
|
|
|
2020-05-29 04:32:26 +00:00
|
|
|
# Subset of above since we don't expand shell-style variables on anything that
|
|
|
|
# runs a command because the shell will expand those.
|
2020-07-17 03:26:58 +00:00
|
|
|
for i in controlpath identityagent forwardagent localforward remoteforward \
|
|
|
|
userknownhostsfile; do
|
2020-05-29 04:32:26 +00:00
|
|
|
verbose $tid $i dollar
|
|
|
|
FOO=bar
|
|
|
|
export FOO
|
|
|
|
trial $i '${FOO}' $FOO
|
|
|
|
done
|
|
|
|
|
|
|
|
|
2020-04-03 02:33:31 +00:00
|
|
|
# A subset of options support tilde expansion
|
|
|
|
for i in controlpath identityagent forwardagent; do
|
2020-05-29 04:32:26 +00:00
|
|
|
verbose $tid $i tilde
|
2020-04-03 02:33:31 +00:00
|
|
|
trial $i '~' $HOME/
|
|
|
|
trial $i '~/.ssh' $HOME/.ssh
|
|
|
|
done
|