summaryrefslogtreecommitdiff
path: root/src/monkeysphere-server
blob: 96a1070ea173f917d098cd711cc6f80b6abddf64 (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. gen-key (g) [HOSTNAME] generate gpg key for the server
  27. show-fingerprint (f) show server's host key fingerprint
  28. publish-key (p) publish server key to keyserver
  29. trust-keys (t) KEYID... mark keyids as trusted
  30. update-users (s) [USER]... update users authorized_keys files
  31. update-user-userids (u) USER UID... add/update user IDs for a user
  32. remove-user-userids (r) USER UID... remove user IDs for a user
  33. help (h,?) this help
  34. EOF
  35. }
  36. # generate server gpg key
  37. gen_key() {
  38. local hostName
  39. hostName=${1:-$(hostname --fqdn)}
  40. # set key defaults
  41. KEY_TYPE=${KEY_TYPE:-"RSA"}
  42. KEY_LENGTH=${KEY_LENGTH:-"2048"}
  43. KEY_USAGE=${KEY_USAGE:-"auth"}
  44. cat <<EOF
  45. Please specify how long the key should be valid.
  46. 0 = key does not expire
  47. <n> = key expires in n days
  48. <n>w = key expires in n weeks
  49. <n>m = key expires in n months
  50. <n>y = key expires in n years
  51. EOF
  52. read -p "Key is valid for? ($EXPIRE) " EXPIRE; EXPIRE=${EXPIRE:-"0"}
  53. SERVICE=${SERVICE:-"ssh"}
  54. USERID=${USERID:-"$SERVICE"://"$hostName"}
  55. # set key parameters
  56. keyParameters=$(cat <<EOF
  57. Key-Type: $KEY_TYPE
  58. Key-Length: $KEY_LENGTH
  59. Key-Usage: $KEY_USAGE
  60. Name-Real: $USERID
  61. Expire-Date: $EXPIRE
  62. EOF
  63. )
  64. # add the revoker field if requested
  65. # FIXME: the 1: below assumes that $REVOKER's key is an RSA key. why?
  66. # FIXME: why is this marked "sensitive"? how will this signature ever
  67. # be transmitted to the expected revoker?
  68. if [ "$REVOKER" ] ; then
  69. keyParameters="${keyParameters}"$(cat <<EOF
  70. Revoker: 1:$REVOKER sensitive
  71. EOF
  72. )
  73. fi
  74. echo "The following key parameters will be used:"
  75. echo "$keyParameters"
  76. read -p "generate key? [Y|n]: " OK; OK=${OK:=Y}
  77. if [ ${OK/y/Y} != 'Y' ] ; then
  78. failure "aborting."
  79. fi
  80. if gpg --list-key ="$USERID" > /dev/null 2>&1 ; then
  81. failure "key for '$USERID' already exists"
  82. fi
  83. # add commit command
  84. keyParameters="${keyParameters}"$(cat <<EOF
  85. %commit
  86. %echo done
  87. EOF
  88. )
  89. log -n "generating server key... "
  90. echo "$keyParameters" | gpg --batch --gen-key
  91. loge "done."
  92. fingerprint_server_key
  93. }
  94. fingerprint_server_key() {
  95. gpg --fingerprint --list-secret-keys =ssh://$(hostname --fqdn)
  96. }
  97. ########################################################################
  98. # MAIN
  99. ########################################################################
  100. COMMAND="$1"
  101. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  102. shift
  103. # set ms home directory
  104. MS_HOME=${MS_HOME:-"$ETC"}
  105. # load configuration file
  106. MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere-server.conf}
  107. [ -e "$MS_CONF" ] && . "$MS_CONF"
  108. # set empty config variable with defaults
  109. GNUPGHOME=${GNUPGHOME:-"${MS_HOME}/gnupg"}
  110. KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
  111. CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
  112. REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
  113. USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
  114. export GNUPGHOME
  115. # make sure the monkeysphere home directory exists
  116. mkdir -p "${MS_HOME}/authorized_user_ids"
  117. # make sure gpg home exists with proper permissions
  118. mkdir -p -m 0700 "$GNUPGHOME"
  119. # make sure the authorized_keys directory exists
  120. mkdir -p "${CACHE}/authorized_keys"
  121. case $COMMAND in
  122. 'update-users'|'update-user'|'s')
  123. if [ "$1" ] ; then
  124. unames="$@"
  125. else
  126. unames=$(ls -1 "${MS_HOME}/authorized_user_ids")
  127. fi
  128. for uname in $unames ; do
  129. MODE="authorized_keys"
  130. log "----- user: $uname -----"
  131. # set variables for the user
  132. AUTHORIZED_USER_IDS="${MS_HOME}/authorized_user_ids/${uname}"
  133. # temporary authorized_keys file
  134. AUTHORIZED_KEYS="${CACHE}/authorized_keys/${uname}.tmp"
  135. # make sure user's authorized_user_ids file exists
  136. touch "$AUTHORIZED_USER_IDS"
  137. # make sure the authorized_keys file exists and is clear
  138. > "$AUTHORIZED_KEYS"
  139. # skip if the user's authorized_user_ids file is empty
  140. if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
  141. log "authorized_user_ids file for '$uname' is empty."
  142. continue
  143. fi
  144. # process authorized_user_ids file
  145. log "processing authorized_user_ids file..."
  146. process_authorized_user_ids
  147. # add user-controlled authorized_keys file path if specified
  148. if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" != '-' ] ; then
  149. userHome=$(getent passwd "$uname" | cut -d: -f6)
  150. userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$userHome"}
  151. if [ -f "$userAuthorizedKeys" ] ; then
  152. log -n "adding user's authorized_keys file... "
  153. cat "$userAuthorizedKeys" >> "$AUTHORIZED_KEYS"
  154. loge "done."
  155. fi
  156. fi
  157. # move the temp authorized_keys file into place
  158. mv -f "${CACHE}/authorized_keys/${uname}.tmp" "${CACHE}/authorized_keys/${uname}"
  159. log "authorized_keys file updated."
  160. done
  161. log "----- done. -----"
  162. ;;
  163. 'gen-key'|'g')
  164. gen_key "$1"
  165. ;;
  166. 'show-fingerprint'|'f')
  167. fingerprint_server_key
  168. ;;
  169. 'publish-key'|'p')
  170. publish_server_key
  171. ;;
  172. 'trust-keys'|'trust-key'|'t')
  173. if [ -z "$1" ] ; then
  174. failure "You must specify at least one key to trust."
  175. fi
  176. # process key IDs
  177. for keyID ; do
  178. trust_key "$keyID"
  179. done
  180. ;;
  181. 'update-user-userids'|'update-user-userid'|'u')
  182. uname="$1"
  183. shift
  184. if [ -z "$uname" ] ; then
  185. failure "You must specify user."
  186. fi
  187. if [ -z "$1" ] ; then
  188. failure "You must specify at least one user ID."
  189. fi
  190. # set variables for the user
  191. AUTHORIZED_USER_IDS="$MS_HOME"/authorized_user_ids/"$uname"
  192. # make sure user's authorized_user_ids file exists
  193. touch "$AUTHORIZED_USER_IDS"
  194. # process the user IDs
  195. for userID ; do
  196. update_userid "$userID"
  197. done
  198. log "Run the following to update user's authorized_keys file:"
  199. log "$PGRM update-users $uname"
  200. ;;
  201. 'remove-user-userids'|'remove-user-userid'|'r')
  202. uname="$1"
  203. shift
  204. if [ -z "$uname" ] ; then
  205. failure "You must specify user."
  206. fi
  207. if [ -z "$1" ] ; then
  208. failure "You must specify at least one user ID."
  209. fi
  210. # set variables for the user
  211. AUTHORIZED_USER_IDS="$MS_HOME"/authorized_user_ids/"$uname"
  212. # make sure user's authorized_user_ids file exists
  213. touch "$AUTHORIZED_USER_IDS"
  214. # process the user IDs
  215. for userID ; do
  216. remove_userid "$userID"
  217. done
  218. log "Run the following to update user's authorized_keys file:"
  219. log "$PGRM update-users $uname"
  220. ;;
  221. 'help'|'h'|'?')
  222. usage
  223. ;;
  224. *)
  225. failure "Unknown command: '$COMMAND'
  226. Type '$PGRM help' for usage."
  227. ;;
  228. esac