summaryrefslogtreecommitdiff
path: root/src/monkeysphere-server
blob: 693c062d5554fd858a8671e1fb303536d86a6f4c (plain)
  1. #!/bin/bash
  2. # monkeysphere-server: MonkeySphere server admin tool
  3. #
  4. # The monkeysphere scripts are written by:
  5. # Jameson Rollins <jrollins@fifthhorseman.net>
  6. #
  7. # They are Copyright 2008, and are all released under the GPL, version 3
  8. # or later.
  9. ########################################################################
  10. PGRM=$(basename $0)
  11. SHAREDIR=${SHAREDIR:-"/usr/share/monkeysphere"}
  12. export SHAREDIR
  13. . "${SHAREDIR}/common"
  14. # date in UTF format if needed
  15. DATE=$(date -u '+%FT%T')
  16. # unset some environment variables that could screw things up
  17. GREP_OPTIONS=
  18. ########################################################################
  19. # FUNCTIONS
  20. ########################################################################
  21. usage() {
  22. cat <<EOF
  23. usage: $PGRM <subcommand> [args]
  24. MonkeySphere server admin tool.
  25. subcommands:
  26. update-users (s) [USER]... update users authorized_keys files
  27. gen-key (g) [HOSTNAME] generate gpg key for the server
  28. show-fingerprint (f) show server's host key fingerprint
  29. publish-key (p) publish server key to keyserver
  30. trust-keys (t) KEYID... mark keyids as trusted
  31. help (h,?) this help
  32. EOF
  33. }
  34. # generate server gpg key
  35. gen_key() {
  36. local hostName
  37. hostName=${1:-$(hostname --fqdn)}
  38. # set key defaults
  39. KEY_TYPE=${KEY_TYPE:-"RSA"}
  40. KEY_LENGTH=${KEY_LENGTH:-"2048"}
  41. KEY_USAGE=${KEY_USAGE:-"auth"}
  42. cat <<EOF
  43. Please specify how long the key should be valid.
  44. 0 = key does not expire
  45. <n> = key expires in n days
  46. <n>w = key expires in n weeks
  47. <n>m = key expires in n months
  48. <n>y = key expires in n years
  49. EOF
  50. read -p "Key is valid for? ($EXPIRE) " EXPIRE; EXPIRE=${EXPIRE:-"0"}
  51. SERVICE=${SERVICE:-"ssh"}
  52. USERID=${USERID:-"$SERVICE"://"$hostName"}
  53. # set key parameters
  54. keyParameters=$(cat <<EOF
  55. Key-Type: $KEY_TYPE
  56. Key-Length: $KEY_LENGTH
  57. Key-Usage: $KEY_USAGE
  58. Name-Real: $USERID
  59. Expire-Date: $EXPIRE
  60. EOF
  61. )
  62. # add the revoker field if requested
  63. # FIXME: the 1: below assumes that $REVOKER's key is an RSA key. why?
  64. # FIXME: why is this marked "sensitive"? how will this signature ever
  65. # be transmitted to the expected revoker?
  66. if [ "$REVOKER" ] ; then
  67. keyParameters="${keyParameters}"$(cat <<EOF
  68. Revoker: 1:$REVOKER sensitive
  69. EOF
  70. )
  71. fi
  72. echo "The following key parameters will be used:"
  73. echo "$keyParameters"
  74. read -p "generate key? [Y|n]: " OK; OK=${OK:=Y}
  75. if [ ${OK/y/Y} != 'Y' ] ; then
  76. failure "aborting."
  77. fi
  78. if gpg --list-key ="$USERID" > /dev/null 2>&1 ; then
  79. failure "key for '$USERID' already exists"
  80. fi
  81. # add commit command
  82. keyParameters="${keyParameters}"$(cat <<EOF
  83. %commit
  84. %echo done
  85. EOF
  86. )
  87. log -n "generating server key... "
  88. echo "$keyParameters" | gpg --batch --gen-key
  89. loge "done."
  90. fingerprint_server_key
  91. }
  92. fingerprint_server_key() {
  93. gpg --fingerprint --list-secret-keys =ssh://$(hostname --fqdn)
  94. }
  95. ########################################################################
  96. # MAIN
  97. ########################################################################
  98. COMMAND="$1"
  99. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  100. shift
  101. # set ms home directory
  102. MS_HOME=${MS_HOME:-"$ETC"}
  103. # load configuration file
  104. MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere-server.conf}
  105. [ -e "$MS_CONF" ] && . "$MS_CONF"
  106. # set empty config variable with defaults
  107. GNUPGHOME=${GNUPGHOME:-"${MS_HOME}/gnupg"}
  108. KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
  109. CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
  110. REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
  111. AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"%h/.config/monkeysphere/authorized_user_ids"}
  112. USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
  113. export GNUPGHOME
  114. # make sure the monkeysphere home directory exists
  115. mkdir -p "${MS_HOME}/authorized_user_ids"
  116. # make sure gpg home exists with proper permissions
  117. mkdir -p -m 0700 "$GNUPGHOME"
  118. # make sure the authorized_keys directory exists
  119. mkdir -p "${CACHE}/authorized_keys"
  120. case $COMMAND in
  121. 'update-users'|'update-user'|'s')
  122. if [ "$1" ] ; then
  123. # get users from command line
  124. unames="$@"
  125. else
  126. # or just look at all users if none specified
  127. unames=$(getent passwd | cut -d: -f1)
  128. fi
  129. # loop over users
  130. for uname in $unames ; do
  131. MODE="authorized_keys"
  132. # check all specified users exist
  133. if ! getent passwd "$uname" >/dev/null ; then
  134. error "----- unknown user '$uname' -----"
  135. continue
  136. fi
  137. # set authorized_user_ids variable,
  138. # translate ssh-style path variables
  139. authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
  140. # skip user if authorized_user_ids file does not exist
  141. if [ ! -f "$authorizedUserIDs" ] ; then
  142. continue
  143. fi
  144. log "----- user: $uname -----"
  145. # temporary authorized_keys file
  146. AUTHORIZED_KEYS=$(mktemp)
  147. # skip if the user's authorized_user_ids file is empty
  148. if [ ! -s "$authorizedUserIDs" ] ; then
  149. log "authorized_user_ids file '$authorizedUserIDs' is empty."
  150. continue
  151. fi
  152. # process authorized_user_ids file
  153. log "processing authorized_user_ids file..."
  154. process_authorized_user_ids "$authorizedUserIDs"
  155. # add user-controlled authorized_keys file path if specified
  156. if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" != '-' ] ; then
  157. userAuthorizedKeys=$(translate_ssh_variables "$uname" "$USER_CONTROLLED_AUTHORIZED_KEYS")
  158. if [ -f "$userAuthorizedKeys" ] ; then
  159. log -n "adding user's authorized_keys file... "
  160. cat "$userAuthorizedKeys" >> "$AUTHORIZED_KEYS"
  161. loge "done."
  162. fi
  163. fi
  164. # move the temp authorized_keys file into place
  165. mv -f "$AUTHORIZED_KEYS" "${CACHE}/authorized_keys/${uname}"
  166. log "authorized_keys file updated."
  167. done
  168. ;;
  169. 'gen-key'|'g')
  170. gen_key "$1"
  171. ;;
  172. 'show-fingerprint'|'f')
  173. fingerprint_server_key
  174. ;;
  175. 'publish-key'|'p')
  176. publish_server_key
  177. ;;
  178. 'trust-keys'|'trust-key'|'t')
  179. if [ -z "$1" ] ; then
  180. failure "You must specify at least one key to trust."
  181. fi
  182. # process key IDs
  183. for keyID ; do
  184. trust_key "$keyID"
  185. done
  186. ;;
  187. 'help'|'h'|'?')
  188. usage
  189. ;;
  190. *)
  191. failure "Unknown command: '$COMMAND'
  192. Type '$PGRM help' for usage."
  193. ;;
  194. esac
  195. exit "$ERR"