percent.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # $OpenBSD: percent.sh,v 1.9 2020/07/17 07:10:24 dtucker Exp $
  2. # Placed in the Public Domain.
  3. tid="percent expansions"
  4. if [ -x "/usr/xpg4/bin/id" ]; then
  5. PATH=/usr/xpg4/bin:$PATH
  6. export PATH
  7. fi
  8. USER=$(id -u -n)
  9. USERID=$(id -u)
  10. HOST=$(hostname | cut -f1 -d.)
  11. HOSTNAME=$(hostname)
  12. # Localcommand is evaluated after connection because %T is not available
  13. # until then. Because of this we use a different method of exercising it,
  14. # and we can't override the remote user otherwise authentication will fail.
  15. # We also have to explicitly enable it.
  16. echo "permitlocalcommand yes" >> $OBJ/ssh_proxy
  17. trial()
  18. {
  19. opt="$1"
  20. arg="$2"
  21. expect="$3"
  22. trace "test $opt=$arg $expect"
  23. rm -f $OBJ/actual
  24. got=""
  25. case "$opt" in
  26. localcommand)
  27. ${SSH} -F $OBJ/ssh_proxy -o $opt="echo '$arg' >$OBJ/actual" \
  28. somehost true
  29. got=$(cat $OBJ/actual)
  30. ;;
  31. userknownhostsfile)
  32. # Move the userknownhosts file to what the expansion says,
  33. # make sure ssh works then put it back.
  34. mv "$OBJ/known_hosts" "$OBJ/$expect"
  35. ${SSH} -F $OBJ/ssh_proxy -o $opt="$OBJ/$arg" somehost true &&
  36. got="$expect"
  37. mv "$OBJ/$expect" "$OBJ/known_hosts"
  38. ;;
  39. matchexec)
  40. (cat $OBJ/ssh_proxy &&
  41. echo "Match Exec \"echo '$arg' >$OBJ/actual\"") \
  42. > $OBJ/ssh_proxy_match
  43. ${SSH} -F $OBJ/ssh_proxy_match remuser@somehost true || true
  44. got=$(cat $OBJ/actual)
  45. ;;
  46. *forward)
  47. # LocalForward and RemoteForward take two args and only
  48. # operate on Unix domain socket paths
  49. got=$(${SSH} -F $OBJ/ssh_proxy -o $opt="/$arg /$arg" -G \
  50. remuser@somehost | awk '$1=="'$opt'"{print $2" "$3}')
  51. expect="/$expect /$expect"
  52. ;;
  53. *)
  54. got=$(${SSH} -F $OBJ/ssh_proxy -o $opt="$arg" -G \
  55. remuser@somehost | awk '$1=="'$opt'"{print $2}')
  56. ;;
  57. esac
  58. if [ "$got" != "$expect" ]; then
  59. fail "$opt=$arg expect $expect got $got"
  60. fi
  61. }
  62. for i in matchexec localcommand remotecommand controlpath identityagent \
  63. forwardagent localforward remoteforward userknownhostsfile; do
  64. verbose $tid $i percent
  65. case "$i" in
  66. localcommand | userknownhostsfile)
  67. # Any test that's going to actually make a connection needs
  68. # to use the real username.
  69. REMUSER=$USER
  70. ;;
  71. *)
  72. REMUSER=remuser
  73. ;;
  74. esac
  75. if [ "$i" = "$localcommand" ]; then
  76. trial $i '%T' NONE
  77. fi
  78. # Matches implementation in readconf.c:ssh_connection_hash()
  79. HASH=$(printf "${HOSTNAME}127.0.0.1${PORT}$REMUSER" |
  80. openssl sha1 | cut -f2 -d' ')
  81. trial $i '%%' '%'
  82. trial $i '%C' $HASH
  83. trial $i '%i' $USERID
  84. trial $i '%h' 127.0.0.1
  85. trial $i '%L' $HOST
  86. trial $i '%l' $HOSTNAME
  87. trial $i '%n' somehost
  88. trial $i '%k' localhost-with-alias
  89. trial $i '%p' $PORT
  90. trial $i '%r' $REMUSER
  91. trial $i '%u' $USER
  92. # We can't specify a full path outside the regress dir, so skip tests
  93. # containing %d for UserKnownHostsFile
  94. if [ "$i" != "userknownhostsfile" ]; then
  95. trial $i '%d' $HOME
  96. trial $i '%%/%C/%i/%h/%d/%L/%l/%n/%p/%r/%u' \
  97. "%/$HASH/$USERID/127.0.0.1/$HOME/$HOST/$HOSTNAME/somehost/$PORT/$REMUSER/$USER"
  98. fi
  99. done
  100. # Subset of above since we don't expand shell-style variables on anything that
  101. # runs a command because the shell will expand those.
  102. for i in controlpath identityagent forwardagent localforward remoteforward \
  103. userknownhostsfile; do
  104. verbose $tid $i dollar
  105. FOO=bar
  106. export FOO
  107. trial $i '${FOO}' $FOO
  108. done
  109. # A subset of options support tilde expansion
  110. for i in controlpath identityagent forwardagent; do
  111. verbose $tid $i tilde
  112. trial $i '~' $HOME/
  113. trial $i '~/.ssh' $HOME/.ssh
  114. done