sshcfgparse.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # $OpenBSD: sshcfgparse.sh,v 1.6 2019/12/21 02:33:07 djm Exp $
  2. # Placed in the Public Domain.
  3. tid="ssh config parse"
  4. dsa=0
  5. for t in $SSH_KEYTYPES; do
  6. case "$t" in
  7. ssh-dss) dsa=1 ;;
  8. esac
  9. done
  10. expect_result_present()
  11. {
  12. _str="$1"
  13. shift
  14. for _expect in "$@"; do
  15. echo "$f" | tr ',' '\n' | grep "^$_expect\$" > /dev/null
  16. if test $? -ne 0; then
  17. fail "missing expected \"$_expect\" from \"$_str\""
  18. fi
  19. done
  20. }
  21. expect_result_absent()
  22. {
  23. _str="$1"
  24. shift
  25. for _expect in "$@"; do
  26. echo "$f" | tr ',' '\n' | grep "^$_expect\$" > /dev/null
  27. if test $? -eq 0; then
  28. fail "unexpected \"$_expect\" present in \"$_str\""
  29. fi
  30. done
  31. }
  32. verbose "reparse minimal config"
  33. (${SSH} -G -F $OBJ/ssh_config somehost > $OBJ/ssh_config.1 &&
  34. ${SSH} -G -F $OBJ/ssh_config.1 somehost > $OBJ/ssh_config.2 &&
  35. diff $OBJ/ssh_config.1 $OBJ/ssh_config.2) || fail "reparse minimal config"
  36. verbose "ssh -W opts"
  37. f=$(${SSH} -GF $OBJ/ssh_config host | awk '/exitonforwardfailure/{print $2}')
  38. test "$f" = "no" || fail "exitonforwardfailure default"
  39. f=$(${SSH} -GF $OBJ/ssh_config -W a:1 h | awk '/exitonforwardfailure/{print $2}')
  40. test "$f" = "yes" || fail "exitonforwardfailure enable"
  41. f=$(${SSH} -GF $OBJ/ssh_config -W a:1 -o exitonforwardfailure=no h |
  42. awk '/exitonforwardfailure/{print $2}')
  43. test "$f" = "no" || fail "exitonforwardfailure override"
  44. f=$(${SSH} -GF $OBJ/ssh_config host | awk '/clearallforwardings/{print $2}')
  45. test "$f" = "no" || fail "clearallforwardings default"
  46. f=$(${SSH} -GF $OBJ/ssh_config -W a:1 h | awk '/clearallforwardings/{print $2}')
  47. test "$f" = "yes" || fail "clearallforwardings enable"
  48. f=$(${SSH} -GF $OBJ/ssh_config -W a:1 -o clearallforwardings=no h |
  49. awk '/clearallforwardings/{print $2}')
  50. test "$f" = "no" || fail "clearallforwardings override"
  51. verbose "user first match"
  52. user=$(awk '$1=="User" {print $2}' $OBJ/ssh_config)
  53. f=$(${SSH} -GF $OBJ/ssh_config host | awk '/^user /{print $2}')
  54. test "$f" = "$user" || fail "user from config, expected '$user' got '$f'"
  55. f=$(${SSH} -GF $OBJ/ssh_config -o user=foo -l bar baz@host | awk '/^user /{print $2}')
  56. test "$f" = "foo" || fail "user first match -oUser, expected 'foo' got '$f' "
  57. f=$(${SSH} -GF $OBJ/ssh_config -lbar baz@host user=foo baz@host | awk '/^user /{print $2}')
  58. test "$f" = "bar" || fail "user first match -l, expected 'bar' got '$f'"
  59. f=$(${SSH} -GF $OBJ/ssh_config baz@host -o user=foo -l bar baz@host | awk '/^user /{print $2}')
  60. test "$f" = "baz" || fail "user first match user@host, expected 'baz' got '$f'"
  61. verbose "pubkeyacceptedkeyalgorithms"
  62. # Default set
  63. f=$(${SSH} -GF none host | awk '/^pubkeyacceptedalgorithms /{print $2}')
  64. expect_result_present "$f" "ssh-ed25519" "ssh-ed25519-cert-v01.*"
  65. expect_result_absent "$f" "ssh-dss"
  66. # Explicit override
  67. f=$(${SSH} -GF none -opubkeyacceptedalgorithms=ssh-ed25519 host | awk '/^pubkeyacceptedalgorithms /{print $2}')
  68. expect_result_present "$f" "ssh-ed25519"
  69. expect_result_absent "$f" "ssh-ed25519-cert-v01.*" "ssh-dss"
  70. # Removal from default set
  71. f=$(${SSH} -GF none -opubkeyacceptedalgorithms=-ssh-ed25519-cert* host | awk '/^pubkeyacceptedalgorithms /{print $2}')
  72. expect_result_present "$f" "ssh-ed25519"
  73. expect_result_absent "$f" "ssh-ed25519-cert-v01.*" "ssh-dss"
  74. f=$(${SSH} -GF none -opubkeyacceptedalgorithms=-ssh-ed25519 host | awk '/^pubkeyacceptedalgorithms /{print $2}')
  75. expect_result_present "$f" "ssh-ed25519-cert-v01.*"
  76. expect_result_absent "$f" "ssh-ed25519" "ssh-dss"
  77. # Append to default set.
  78. # This is not tested when built !WITH_OPENSSL
  79. if [ "$dsa" = "1" ]; then
  80. f=$(${SSH} -GF none -opubkeyacceptedalgorithms=+ssh-dss-cert* host | awk '/^pubkeyacceptedalgorithms /{print $2}')
  81. expect_result_present "$f" "ssh-ed25519" "ssh-dss-cert-v01.*"
  82. expect_result_absent "$f" "ssh-dss"
  83. f=$(${SSH} -GF none -opubkeyacceptedalgorithms=+ssh-dss host | awk '/^pubkeyacceptedalgorithms /{print $2}')
  84. expect_result_present "$f" "ssh-ed25519" "ssh-ed25519-cert-v01.*" "ssh-dss"
  85. expect_result_absent "$f" "ssh-dss-cert-v01.*"
  86. fi
  87. verbose "agentforwarding"
  88. f=$(${SSH} -GF none host | awk '/^forwardagent /{print$2}')
  89. expect_result_present "$f" "no"
  90. f=$(${SSH} -GF none -oforwardagent=no host | awk '/^forwardagent /{print$2}')
  91. expect_result_present "$f" "no"
  92. f=$(${SSH} -GF none -oforwardagent=yes host | awk '/^forwardagent /{print$2}')
  93. expect_result_present "$f" "yes"
  94. f=$(${SSH} -GF none '-oforwardagent=SSH_AUTH_SOCK.forward' host | awk '/^forwardagent /{print$2}')
  95. expect_result_present "$f" "SSH_AUTH_SOCK.forward"
  96. # cleanup
  97. rm -f $OBJ/ssh_config.[012]