summaryrefslogtreecommitdiff
path: root/rhesus/rhesus
blob: fe98b39da09d23b0959bb713dff3299f5c74a8c5 (plain)
  1. #!/bin/sh
  2. # rhesus: monkeysphere authorized_keys update script
  3. #
  4. # Written by
  5. # Jameson Rollins <jrollins@fifthhorseman.net>
  6. #
  7. # Copyright 2008, released under the GPL, version 3 or later
  8. ##################################################
  9. # load conf file
  10. #. /etc/monkeysphere/monkeysphere.conf
  11. . ~/ms/monkeysphere.conf
  12. #AUTH_KEYS_DIR_BASE=/var/lib/monkeysphere/authorized_keys/
  13. AUTH_KEYS_DIR_BASE=~/ms/authorized_keys
  14. export GNUPGHOME
  15. ##################################################
  16. CMD=$(basename $0)
  17. usage() {
  18. cat <<EOF
  19. usage: $CMD USERNAME
  20. EOF
  21. }
  22. failure() {
  23. echo "$1" >&2
  24. exit ${2:-'1'}
  25. }
  26. meat() {
  27. grep -v -e "^[[:space:]]*#" -e '^$' "$1"
  28. }
  29. cutline() {
  30. head --line="$1" | tail -1
  31. }
  32. ### MAIN
  33. if [ -z "$1" ] ; then
  34. usage
  35. exit 1
  36. fi
  37. # user name of user to update
  38. USERNAME="$1"
  39. if ! id "$USERNAME" > /dev/null ; then
  40. failure "User '$USERNAME' does not exist."
  41. fi
  42. AUTH_USER_IDS="$AUTH_USER_IDS_DIR"/"$USERNAME"
  43. if [ ! -e "$AUTH_USER_IDS" ] ; then
  44. failure "No auth_user_ids file for user '$USERNAME'."
  45. fi
  46. AUTH_KEYS_DIR="$AUTH_KEYS_DIR_BASE"/"$USERNAME"/keys
  47. AUTH_KEYS_FILE="$AUTH_KEYS_DIR_BASE"/authorized_keys
  48. # make sure the gnupg home exists with proper permissions
  49. mkdir -p "$GNUPGHOME"
  50. chmod 0700 "$GNUPGHOME"
  51. # find number of user ids in auth_user_ids file
  52. NLINES=$(meat "$AUTH_USER_IDS" | wc -l)
  53. # clean out keys file and remake keys directory
  54. rm -rf "$AUTH_KEYS_DIR"
  55. mkdir -p "$AUTH_KEYS_DIR"
  56. # loop through all user ids, and generate ssh keys
  57. for (( N=1; N<=$NLINES; N=N+1 )) ; do
  58. # get user id
  59. USERID=$(meat "$AUTH_USER_IDS" | cutline "$N" )
  60. USERID_HASH=$(echo "$USERID" | sha1sum | awk '{ print $1 }')
  61. KEYFILE="$AUTH_KEYS_DIR"/"$USERID_HASH"
  62. # search for key on keyserver
  63. echo -n "ms: finding key for '$USERID'..."
  64. RETURN=$(echo 1 | gpg --quiet --batch --command-fd 0 --with-colons --keyserver "$KEYSERVER" --search ="$USERID" 2> /dev/null)
  65. # if the key was found...
  66. if [ "$RETURN" ] ; then
  67. echo " found."
  68. # checking key attributes
  69. # see /usr/share/doc/gnupg/DETAILS.gz:
  70. PUB_INFO=$(gpg --fixed-list-mode --with-colons --list-keys --with-fingerprint ="$USERID" | grep '^pub:')
  71. echo -n "ms: "
  72. # # if not an authorization key exit
  73. # if echo "$PUB_INFO" | cut -d: -f12 | grep -v -q '[aA]' ; then
  74. # echo "not an authorization key --> SKIPPING"
  75. # continue
  76. # fi
  77. # if key is not fully trusted exit
  78. # (this includes not revoked or expired)
  79. # determine trust
  80. TRUST=$(echo "$PUB_INFO" | cut -d: -f2)
  81. case "$TRUST" in
  82. 'i')
  83. echo -n "invalid" ;;
  84. 'r')
  85. echo -n "revoked" ;;
  86. 'e')
  87. echo -n "expired" ;;
  88. '-'|'q'|'n'|'m')
  89. echo -n "unacceptable trust" ;;
  90. 'f'|'u')
  91. echo -n "fully trusted"
  92. # convert pgp key to ssh key, and write to cache file
  93. echo " -> generating ssh key..."
  94. gpgkey2ssh "$KEYID" | sed -e "s/COMMENT/$USERID/" > "$KEYFILE"
  95. continue
  96. ;;
  97. *)
  98. echo -n "unknown trust" ;;
  99. esac
  100. echo " -> SKIPPING"
  101. fi
  102. done
  103. if [ $(ls "$AUTH_KEYS_DIR") ] ; then
  104. echo "ms: writing ms authorized_keys file..."
  105. cat "$AUTH_KEYS_DIR"/* > "$AUTH_KEYS_FILE"
  106. else
  107. echo "ms: no gpg keys to add to authorized_keys file."
  108. fi
  109. if [ -s ~"$USERNAME"/.ssh/authorized_keys ] ; then
  110. echo "ms: adding user authorized_keys..."
  111. cat ~"$USERNAME"/.ssh/authorized_keys >> "$AUTH_KEYS_FILE"
  112. fi