summaryrefslogtreecommitdiff
path: root/rhesus/rhesus
blob: 7979e418c9e3e1a367adc2cab00e90c02467d360 (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. # user name of user to update
  13. USERNAME="$1"
  14. #AUTH_KEYS_DIR_BASE=/var/lib/monkeysphere/authorized_keys/
  15. AUTH_KEYS_DIR_BASE=~/ms/authorized_keys
  16. AUTH_KEYS_DIR="$AUTH_KEYS_DIR_BASE"/"$USERNAME"
  17. AUTH_KEYS_FILE="$AUTH_KEYS_DIR"/authorized_keys
  18. AUTH_USER_IDS="$AUTH_USER_IDS_DIR"/"$USERNAME"
  19. export GNUPGHOME
  20. ##################################################
  21. ### FUNCTIONS
  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. # make sure the gnupg home exists with proper permissions
  34. mkdir -p "$GNUPGHOME"
  35. chmod 0700 "$GNUPGHOME"
  36. # find number of user ids in auth_user_ids file
  37. NLINES=$(meat "$AUTH_USER_IDS" | wc -l)
  38. # clean out keys file and remake keys directory
  39. rm -rf "$AUTH_KEYS_DIR"/keys
  40. mkdir -p "$AUTH_KEYS_DIR"/keys
  41. # loop through all user ids, and generate ssh keys
  42. for (( N=1; N<=$NLINES; N=N+1 )) ; do
  43. # get user id
  44. USERID=$(meat "$AUTH_USER_IDS" | head --line="$N" | tail -1)
  45. USERID_HASH=$(echo "$USERID" | sha1sum | awk '{ print $1 }')
  46. # get key id from user id
  47. #KEYID=$(gpguser2key "$USERID")
  48. KEYID="$USERID"
  49. echo "Receiving keys for: $USERID ($KEYID)..."
  50. # is primary key revoked && kill
  51. # for all associated keys (primary and sub)
  52. # - type "A"
  53. # - not revoked
  54. # - signed by trusted user
  55. # output ssh key
  56. # Receive keys into key ring
  57. if gpg --recv-keys --keyserver "$KEYSERVER" "$KEYID" ; then
  58. # convert pgp key to ssh key, and write to cache file
  59. KEYFILE="$AUTH_KEYS_DIR"/keys/"$USERID_HASH"
  60. gpgkey2ssh "$KEYID" | sed -e "s/COMMENT/$USERID/" > "$KEYFILE"
  61. fi
  62. done
  63. echo "Writing authorized_keys file '$AUTH_KEYS_FILE'..."
  64. cat "$AUTH_KEYS_DIR"/keys/* > "$AUTH_KEYS_FILE" || > "$AUTH_KEYS_FILE"
  65. if [ -s ~"$USERNAME"/.ssh/authorized_keys ] ; then
  66. cat ~"$USERNAME"/.ssh/authorized_keys >> "$AUTH_KEYS_FILE"
  67. fi