key-options.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # $OpenBSD: key-options.sh,v 1.9 2018/07/03 13:53:26 djm Exp $
  2. # Placed in the Public Domain.
  3. tid="key options"
  4. origkeys="$OBJ/authkeys_orig"
  5. authkeys="$OBJ/authorized_keys_${USER}"
  6. cp $authkeys $origkeys
  7. # Allocating ptys can require privileges on some platforms.
  8. skip_pty=""
  9. if ! config_defined HAVE_OPENPTY && [ "x$SUDO" = "x" ]; then
  10. skip_pty="no openpty(3) and SUDO not set"
  11. fi
  12. # Test command= forced command
  13. for c in 'command="echo bar"' 'no-pty,command="echo bar"'; do
  14. sed "s/.*/$c &/" $origkeys > $authkeys
  15. verbose "key option $c"
  16. r=$(${SSH} -q -F $OBJ/ssh_proxy somehost echo foo)
  17. if [ "$r" = "foo" ]; then
  18. fail "key option forced command not restricted"
  19. fi
  20. if [ "$r" != "bar" ]; then
  21. fail "key option forced command not executed"
  22. fi
  23. done
  24. # Test no-pty
  25. expect_pty_succeed()
  26. {
  27. which=$1
  28. opts=$2
  29. rm -f $OBJ/data
  30. sed "s/.*/$opts &/" $origkeys > $authkeys
  31. verbose "key option pty $which"
  32. [ "x$skip_pty" != "x" ] && verbose "skipped because $skip_pty" && return
  33. ${SSH} -ttq -F $OBJ/ssh_proxy somehost "tty > $OBJ/data; exit 0"
  34. if [ $? -ne 0 ]; then
  35. fail "key option failed $which"
  36. else
  37. r=$(cat $OBJ/data)
  38. case "$r" in
  39. /dev/*) ;;
  40. *) fail "key option failed $which (pty $r)" ;;
  41. esac
  42. fi
  43. }
  44. expect_pty_fail()
  45. {
  46. which=$1
  47. opts=$2
  48. rm -f $OBJ/data
  49. sed "s/.*/$opts &/" $origkeys > $authkeys
  50. verbose "key option pty $which"
  51. [ "x$skip_pty" != "x" ] && verbose "skipped because $skip_pty" && return
  52. ${SSH} -ttq -F $OBJ/ssh_proxy somehost "tty > $OBJ/data; exit 0"
  53. if [ $? -eq 0 ]; then
  54. r=$(cat $OBJ/data)
  55. if [ -e "$r" ]; then
  56. fail "key option failed $which (pty $r)"
  57. fi
  58. case "$r" in
  59. /dev/*) fail "key option failed $which (pty $r)" ;;
  60. *) ;;
  61. esac
  62. fi
  63. }
  64. # First ensure that we can allocate a pty by default.
  65. expect_pty_succeed "default" ""
  66. expect_pty_fail "no-pty" "no-pty"
  67. expect_pty_fail "restrict" "restrict"
  68. expect_pty_succeed "restrict,pty" "restrict,pty"
  69. # Test environment=
  70. # XXX this can fail if ~/.ssh/environment exists for the user running the test
  71. echo 'PermitUserEnvironment yes' >> $OBJ/sshd_proxy
  72. sed 's/.*/environment="FOO=bar" &/' $origkeys > $authkeys
  73. verbose "key option environment"
  74. r=$(${SSH} -q -F $OBJ/ssh_proxy somehost 'echo $FOO')
  75. if [ "$r" != "bar" ]; then
  76. fail "key option environment not set"
  77. fi
  78. # Test from= restriction
  79. start_sshd
  80. for f in 127.0.0.1 '127.0.0.0\/8'; do
  81. cat $origkeys > $authkeys
  82. ${SSH} -q -F $OBJ/ssh_proxy somehost true
  83. if [ $? -ne 0 ]; then
  84. fail "key option failed without restriction"
  85. fi
  86. sed 's/.*/from="'"$f"'" &/' $origkeys > $authkeys
  87. from=$(head -1 $authkeys | cut -f1 -d ' ')
  88. verbose "key option $from"
  89. r=$(${SSH} -q -F $OBJ/ssh_proxy somehost 'echo true')
  90. if [ "$r" = "true" ]; then
  91. fail "key option $from not restricted"
  92. fi
  93. r=$(${SSH} -q -F $OBJ/ssh_config somehost 'echo true')
  94. if [ "$r" != "true" ]; then
  95. fail "key option $from not allowed but should be"
  96. fi
  97. done
  98. check_valid_before()
  99. {
  100. which=$1
  101. opts=$2
  102. expect=$3
  103. sed "s/.*/$opts &/" $origkeys > $authkeys
  104. verbose "key option expiry-time $which"
  105. ${SSH} -q -F $OBJ/ssh_proxy somehost true
  106. r=$?
  107. case "$expect" in
  108. fail) test $r -eq 0 && fail "key option succeeded $which" ;;
  109. pass) test $r -ne 0 && fail "key option failed $which" ;;
  110. *) fatal "unknown expectation $expect" ;;
  111. esac
  112. }
  113. check_valid_before "default" "" "pass"
  114. check_valid_before "invalid" 'expiry-time="INVALID"' "fail"
  115. check_valid_before "expired" 'expiry-time="19990101"' "fail"
  116. check_valid_before "valid" 'expiry-time="20380101"' "pass"