summaryrefslogtreecommitdiff
path: root/src/monkeysphere-server
blob: bfd5db84477cc40c5c3c9e044ebbd241cd7a0e98 (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. AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"%h/.config/monkeysphere/authorized_user_ids"}
  114. USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
  115. export GNUPGHOME
  116. # make sure the monkeysphere home directory exists
  117. mkdir -p "${MS_HOME}/authorized_user_ids"
  118. # make sure gpg home exists with proper permissions
  119. mkdir -p -m 0700 "$GNUPGHOME"
  120. # make sure the authorized_keys directory exists
  121. mkdir -p "${CACHE}/authorized_keys"
  122. case $COMMAND in
  123. 'update-users'|'update-user'|'s')
  124. if [ "$1" ] ; then
  125. # get users from command line
  126. unames="$@"
  127. else
  128. # or just look at all users if none specified
  129. unames=$(getent passwd | cut -d: -f1)
  130. fi
  131. # loop over users
  132. for uname in $unames ; do
  133. MODE="authorized_keys"
  134. # set authorized_user_ids variable,
  135. # translate ssh-style path variables
  136. authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
  137. # skip user if authorized_user_ids file does not exist
  138. if [ ! -f "$authorizedUserIDs" ] ; then
  139. continue
  140. fi
  141. log "----- user: $uname -----"
  142. # temporary authorized_keys file
  143. AUTHORIZED_KEYS=$(mktemp)
  144. # skip if the user's authorized_user_ids file is empty
  145. if [ ! -s "$authorizedUserIDs" ] ; then
  146. log "authorized_user_ids file '$authorizedUserIDs' is empty."
  147. continue
  148. fi
  149. # process authorized_user_ids file
  150. log "processing authorized_user_ids file..."
  151. process_authorized_user_ids "$authorizedUserIDs"
  152. # add user-controlled authorized_keys file path if specified
  153. if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" != '-' ] ; then
  154. userAuthorizedKeys=$(translate_ssh_variables "$uname" "$USER_CONTROLLED_AUTHORIZED_KEYS")
  155. if [ -f "$userAuthorizedKeys" ] ; then
  156. log -n "adding user's authorized_keys file... "
  157. cat "$userAuthorizedKeys" >> "$AUTHORIZED_KEYS"
  158. loge "done."
  159. fi
  160. fi
  161. # move the temp authorized_keys file into place
  162. mv -f "$AUTHORIZED_KEYS" "${CACHE}/authorized_keys/${uname}"
  163. log "authorized_keys file updated."
  164. done
  165. log "----- done. -----"
  166. ;;
  167. 'gen-key'|'g')
  168. gen_key "$1"
  169. ;;
  170. 'show-fingerprint'|'f')
  171. fingerprint_server_key
  172. ;;
  173. 'publish-key'|'p')
  174. publish_server_key
  175. ;;
  176. 'trust-keys'|'trust-key'|'t')
  177. if [ -z "$1" ] ; then
  178. failure "You must specify at least one key to trust."
  179. fi
  180. # process key IDs
  181. for keyID ; do
  182. trust_key "$keyID"
  183. done
  184. ;;
  185. 'update-user-userids'|'update-user-userid'|'u')
  186. uname="$1"
  187. shift
  188. if [ -z "$uname" ] ; then
  189. failure "You must specify user."
  190. fi
  191. if [ -z "$1" ] ; then
  192. failure "You must specify at least one user ID."
  193. fi
  194. # set authorized_user_ids variable,
  195. # translate ssh-style path variables
  196. authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
  197. # make sure user's authorized_user_ids file exists
  198. touch "$authorizedUserIDs"
  199. # process the user IDs
  200. for userID ; do
  201. update_userid "$userID" "$authorizedUserIDs"
  202. done
  203. log "Run the following to update user's authorized_keys file:"
  204. log "$PGRM update-users $uname"
  205. ;;
  206. 'remove-user-userids'|'remove-user-userid'|'r')
  207. uname="$1"
  208. shift
  209. if [ -z "$uname" ] ; then
  210. failure "You must specify user."
  211. fi
  212. if [ -z "$1" ] ; then
  213. failure "You must specify at least one user ID."
  214. fi
  215. # set authorized_user_ids variable,
  216. # translate ssh-style path variables
  217. authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
  218. # make sure user's authorized_user_ids file exists
  219. if [ ! -f "$authorizedUserIDs" ] ; then
  220. failure "authorized_user_ids file '$authorizedUserIDs' does not exist."
  221. fi
  222. # process the user IDs
  223. for userID ; do
  224. remove_userid "$userID" "$authorizedUserIDs"
  225. done
  226. log "Run the following to update user's authorized_keys file:"
  227. log "$PGRM update-users $uname"
  228. ;;
  229. 'help'|'h'|'?')
  230. usage
  231. ;;
  232. *)
  233. failure "Unknown command: '$COMMAND'
  234. Type '$PGRM help' for usage."
  235. ;;
  236. esac