keys-command.sh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env sh
  2. # $OpenBSD: keys-command.sh,v 1.6 2019/07/25 08:48:11 dtucker Exp $
  3. # Placed in the Public Domain.
  4. tid="authorized keys from command"
  5. if [ -z "$SUDO" -a ! -w /var/run ]; then
  6. echo "skipped (SUDO not set)"
  7. echo "need SUDO to create file in /var/run, test won't work without"
  8. exit 0
  9. fi
  10. rm -f $OBJ/keys-command-args
  11. touch $OBJ/keys-command-args
  12. chmod a+rw $OBJ/keys-command-args
  13. expected_key_text=$(awk '{ print $2 }' < $OBJ/ssh-ed25519.pub)
  14. expected_key_fp=$($SSHKEYGEN -lf $OBJ/ssh-ed25519.pub | awk '{ print $2 }')
  15. # Establish a AuthorizedKeysCommand in /var/run where it will have
  16. # acceptable directory permissions.
  17. KEY_COMMAND="/var/run/keycommand_${LOGNAME}.$$"
  18. trap "${SUDO} rm -f ${KEY_COMMAND}" 0
  19. cat << _EOF | $SUDO sh -c "rm -f '$KEY_COMMAND' ; cat > '$KEY_COMMAND'"
  20. #!/bin/sh
  21. echo args: "\$@" >> $OBJ/keys-command-args
  22. echo "$PATH" | grep -q mekmitasdigoat && exit 7
  23. test "x\$1" != "x${LOGNAME}" && exit 1
  24. if test $# -eq 6 ; then
  25. test "x\$2" != "xblah" && exit 2
  26. test "x\$3" != "x${expected_key_text}" && exit 3
  27. test "x\$4" != "xssh-rsa" && exit 4
  28. test "x\$5" != "x${expected_key_fp}" && exit 5
  29. test "x\$6" != "xblah" && exit 6
  30. fi
  31. exec cat "$OBJ/authorized_keys_${LOGNAME}"
  32. _EOF
  33. $SUDO chmod 0755 "$KEY_COMMAND"
  34. if ! $OBJ/check-perm -m keys-command $KEY_COMMAND; then
  35. echo "skipping: $KEY_COMMAND is unsuitable as AuthorizedKeysCommand"
  36. $SUDO rm -f $KEY_COMMAND
  37. exit 0
  38. fi
  39. if [ -x $KEY_COMMAND ]; then
  40. cp $OBJ/sshd_proxy $OBJ/sshd_proxy.bak
  41. verbose "AuthorizedKeysCommand with arguments"
  42. (
  43. grep -vi AuthorizedKeysFile $OBJ/sshd_proxy.bak
  44. echo AuthorizedKeysFile none
  45. echo AuthorizedKeysCommand $KEY_COMMAND %u blah %k %t %f blah
  46. echo AuthorizedKeysCommandUser ${LOGNAME}
  47. ) > $OBJ/sshd_proxy
  48. # Ensure that $PATH is sanitised in sshd
  49. env PATH=$PATH:/sbin/mekmitasdigoat \
  50. ${SSH} -F $OBJ/ssh_proxy somehost true
  51. if [ $? -ne 0 ]; then
  52. fail "connect failed"
  53. fi
  54. verbose "AuthorizedKeysCommand without arguments"
  55. # Check legacy behavior of no-args resulting in username being passed.
  56. (
  57. grep -vi AuthorizedKeysFile $OBJ/sshd_proxy.bak
  58. echo AuthorizedKeysFile none
  59. echo AuthorizedKeysCommand $KEY_COMMAND
  60. echo AuthorizedKeysCommandUser ${LOGNAME}
  61. ) > $OBJ/sshd_proxy
  62. # Ensure that $PATH is sanitised in sshd
  63. env PATH=$PATH:/sbin/mekmitasdigoat \
  64. ${SSH} -F $OBJ/ssh_proxy somehost true
  65. if [ $? -ne 0 ]; then
  66. fail "connect failed"
  67. fi
  68. else
  69. echo "SKIPPED: $KEY_COMMAND not executable (/var/run mounted noexec?)"
  70. fi