principals-command.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/sbin/env sh
  2. # $OpenBSD: principals-command.sh,v 1.11 2019/12/16 02:39:05 djm Exp $
  3. # Placed in the Public Domain.
  4. tid="authorized principals command"
  5. rm -f $OBJ/user_ca_key* $OBJ/cert_user_key*
  6. cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
  7. if [ -z "$SUDO" -a ! -w /var/run ]; then
  8. echo "skipped (SUDO not set)"
  9. echo "need SUDO to create file in /var/run, test won't work without"
  10. exit 0
  11. fi
  12. case "$SSH_KEYTYPES" in
  13. *ssh-rsa*) userkeytype=rsa ;;
  14. *) userkeytype=ed25519 ;;
  15. esac
  16. SERIAL=$$
  17. # Create a CA key and a user certificate.
  18. ${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_ca_key ||
  19. fatal "ssh-keygen of user_ca_key failed"
  20. ${SSHKEYGEN} -q -N '' -t ${userkeytype} -f $OBJ/cert_user_key ||
  21. fatal "ssh-keygen of cert_user_key failed"
  22. ${SSHKEYGEN} -q -s $OBJ/user_ca_key -I "Joanne User" \
  23. -z $$ -n ${USER},mekmitasdigoat $OBJ/cert_user_key ||
  24. fatal "couldn't sign cert_user_key"
  25. CERT_BODY=$(cat $OBJ/cert_user_key-cert.pub | awk '{ print $2 }')
  26. CA_BODY=$(cat $OBJ/user_ca_key.pub | awk '{ print $2 }')
  27. CERT_FP=$(${SSHKEYGEN} -lf $OBJ/cert_user_key-cert.pub | awk '{ print $2 }')
  28. CA_FP=$(${SSHKEYGEN} -lf $OBJ/user_ca_key.pub | awk '{ print $2 }')
  29. # Establish a AuthorizedPrincipalsCommand in /var/run where it will have
  30. # acceptable directory permissions.
  31. PRINCIPALS_COMMAND="/var/run/principals_command_${LOGNAME}.$$"
  32. trap "$SUDO rm -f ${PRINCIPALS_COMMAND}" 0
  33. cat << _EOF | $SUDO sh -c "cat > '$PRINCIPALS_COMMAND'"
  34. #!/bin/sh
  35. test "x\$1" != "x${LOGNAME}" && exit 1
  36. test "x\$2" != "xssh-${userkeytype}-cert-v01@openssh.com" && exit 1
  37. test "x\$3" != "xssh-ed25519" && exit 1
  38. test "x\$4" != "xJoanne User" && exit 1
  39. test "x\$5" != "x${SERIAL}" && exit 1
  40. test "x\$6" != "x${CA_FP}" && exit 1
  41. test "x\$7" != "x${CERT_FP}" && exit 1
  42. test "x\$8" != "x${CERT_BODY}" && exit 1
  43. test "x\$9" != "x${CA_BODY}" && exit 1
  44. test -f "$OBJ/authorized_principals_${LOGNAME}" &&
  45. exec cat "$OBJ/authorized_principals_${LOGNAME}"
  46. _EOF
  47. test $? -eq 0 || fatal "couldn't prepare principals command"
  48. $SUDO chmod 0755 "$PRINCIPALS_COMMAND"
  49. if ! $OBJ/check-perm -m keys-command $PRINCIPALS_COMMAND; then
  50. echo "skipping: $PRINCIPALS_COMMAND is unsuitable as " \
  51. "AuthorizedPrincipalsCommand"
  52. $SUDO rm -f $PRINCIPALS_COMMAND
  53. exit 0
  54. fi
  55. if [ -x $PRINCIPALS_COMMAND ]; then
  56. # Test explicitly-specified principals
  57. for privsep in yes; do
  58. _prefix="privsep $privsep"
  59. # Setup for AuthorizedPrincipalsCommand
  60. rm -f $OBJ/authorized_keys_$USER
  61. (
  62. cat $OBJ/sshd_proxy_bak
  63. echo "UsePrivilegeSeparation $privsep"
  64. echo "AuthorizedKeysFile none"
  65. echo "AuthorizedPrincipalsCommand $PRINCIPALS_COMMAND" \
  66. "%u %t %T %i %s %F %f %k %K"
  67. echo "AuthorizedPrincipalsCommandUser ${LOGNAME}"
  68. echo "TrustedUserCAKeys $OBJ/user_ca_key.pub"
  69. ) > $OBJ/sshd_proxy
  70. # XXX test missing command
  71. # XXX test failing command
  72. # Empty authorized_principals
  73. verbose "$tid: ${_prefix} empty authorized_principals"
  74. echo > $OBJ/authorized_principals_$USER
  75. ${SSH} -i $OBJ/cert_user_key \
  76. -F $OBJ/ssh_proxy somehost true > /dev/null 2>&1
  77. if [ $? -eq 0 ]; then
  78. fail "ssh cert connect succeeded unexpectedly"
  79. fi
  80. # Wrong authorized_principals
  81. verbose "$tid: ${_prefix} wrong authorized_principals"
  82. echo gregorsamsa > $OBJ/authorized_principals_$USER
  83. ${SSH} -i $OBJ/cert_user_key \
  84. -F $OBJ/ssh_proxy somehost true > /dev/null 2>&1
  85. if [ $? -eq 0 ]; then
  86. fail "ssh cert connect succeeded unexpectedly"
  87. fi
  88. # Correct authorized_principals
  89. verbose "$tid: ${_prefix} correct authorized_principals"
  90. echo mekmitasdigoat > $OBJ/authorized_principals_$USER
  91. ${SSH} -i $OBJ/cert_user_key \
  92. -F $OBJ/ssh_proxy somehost true > /dev/null 2>&1
  93. if [ $? -ne 0 ]; then
  94. fail "ssh cert connect failed"
  95. fi
  96. # authorized_principals with bad key option
  97. verbose "$tid: ${_prefix} authorized_principals bad key opt"
  98. echo 'blah mekmitasdigoat' > $OBJ/authorized_principals_$USER
  99. ${SSH} -i $OBJ/cert_user_key \
  100. -F $OBJ/ssh_proxy somehost true > /dev/null 2>&1
  101. if [ $? -eq 0 ]; then
  102. fail "ssh cert connect succeeded unexpectedly"
  103. fi
  104. # authorized_principals with command=false
  105. verbose "$tid: ${_prefix} authorized_principals command=false"
  106. echo 'command="false" mekmitasdigoat' > \
  107. $OBJ/authorized_principals_$USER
  108. ${SSH} -i $OBJ/cert_user_key \
  109. -F $OBJ/ssh_proxy somehost true > /dev/null 2>&1
  110. if [ $? -eq 0 ]; then
  111. fail "ssh cert connect succeeded unexpectedly"
  112. fi
  113. # authorized_principals with command=true
  114. verbose "$tid: ${_prefix} authorized_principals command=true"
  115. echo 'command="true" mekmitasdigoat' > \
  116. $OBJ/authorized_principals_$USER
  117. ${SSH} -i $OBJ/cert_user_key \
  118. -F $OBJ/ssh_proxy somehost false > /dev/null 2>&1
  119. if [ $? -ne 0 ]; then
  120. fail "ssh cert connect failed"
  121. fi
  122. # Setup for principals= key option
  123. rm -f $OBJ/authorized_principals_$USER
  124. (
  125. cat $OBJ/sshd_proxy_bak
  126. echo "UsePrivilegeSeparation $privsep"
  127. ) > $OBJ/sshd_proxy
  128. # Wrong principals list
  129. verbose "$tid: ${_prefix} wrong principals key option"
  130. (
  131. printf 'cert-authority,principals="gregorsamsa" '
  132. cat $OBJ/user_ca_key.pub
  133. ) > $OBJ/authorized_keys_$USER
  134. ${SSH} -i $OBJ/cert_user_key \
  135. -F $OBJ/ssh_proxy somehost true > /dev/null 2>&1
  136. if [ $? -eq 0 ]; then
  137. fail "ssh cert connect succeeded unexpectedly"
  138. fi
  139. # Correct principals list
  140. verbose "$tid: ${_prefix} correct principals key option"
  141. (
  142. printf 'cert-authority,principals="mekmitasdigoat" '
  143. cat $OBJ/user_ca_key.pub
  144. ) > $OBJ/authorized_keys_$USER
  145. ${SSH} -i $OBJ/cert_user_key \
  146. -F $OBJ/ssh_proxy somehost true > /dev/null 2>&1
  147. if [ $? -ne 0 ]; then
  148. fail "ssh cert connect failed"
  149. fi
  150. done
  151. else
  152. echo "SKIPPED: $PRINCIPALS_COMMAND not executable " \
  153. "(/var/run mounted noexec?)"
  154. fi