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