summaryrefslogtreecommitdiff
path: root/src/monkeysphere-server
blob: a198c33c762e7dd005f7c2f80e164cc662c6d7b2 (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. SHARE=${SHARE:-"/usr/share/monkeysphere"}
  12. export SHARE
  13. . "${SHARE}/common"
  14. VARLIB="/var/lib/monkeysphere"
  15. export VARLIB
  16. # date in UTF format if needed
  17. DATE=$(date -u '+%FT%T')
  18. # unset some environment variables that could screw things up
  19. GREP_OPTIONS=
  20. # default return code
  21. ERR=0
  22. ########################################################################
  23. # FUNCTIONS
  24. ########################################################################
  25. usage() {
  26. cat <<EOF
  27. usage: $PGRM <subcommand> [args]
  28. MonkeySphere server admin tool.
  29. subcommands:
  30. update-users (u) [USER]... update users authorized_keys files
  31. gen-key (g) [HOSTNAME] generate gpg key for the server
  32. show-fingerprint (f) show server's host key fingerprint
  33. publish-key (p) publish server's host key to keyserver
  34. trust-key (t) KEYID [LEVEL] set owner trust for keyid
  35. help (h,?) this help
  36. EOF
  37. }
  38. # generate server gpg key
  39. gen_key() {
  40. local hostName
  41. hostName=${1:-$(hostname --fqdn)}
  42. SERVICE=${SERVICE:-"ssh"}
  43. userID="${SERVICE}://${hostName}"
  44. if gpg --list-key ="$userID" > /dev/null 2>&1 ; then
  45. failure "Key for '$userID' already exists"
  46. fi
  47. # set key defaults
  48. KEY_TYPE=${KEY_TYPE:-"RSA"}
  49. KEY_LENGTH=${KEY_LENGTH:-"2048"}
  50. KEY_USAGE=${KEY_USAGE:-"auth"}
  51. KEY_EXPIRE=${KEY_EXPIRE:-"0"}
  52. cat <<EOF
  53. Please specify how long the key should be valid.
  54. 0 = key does not expire
  55. <n> = key expires in n days
  56. <n>w = key expires in n weeks
  57. <n>m = key expires in n months
  58. <n>y = key expires in n years
  59. EOF
  60. read -p "Key is valid for? ($KEY_EXPIRE) " KEY_EXPIRE; KEY_EXPIRE=${KEY_EXPIRE:-"0"}
  61. # set key parameters
  62. keyParameters=$(cat <<EOF
  63. Key-Type: $KEY_TYPE
  64. Key-Length: $KEY_LENGTH
  65. Key-Usage: $KEY_USAGE
  66. Name-Real: $userID
  67. Expire-Date: $KEY_EXPIRE
  68. EOF
  69. )
  70. # add the revoker field if requested
  71. # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key. why?
  72. # FIXME: why is this marked "sensitive"? how will this signature ever
  73. # be transmitted to the expected revoker?
  74. if [ "$REVOKER" ] ; then
  75. keyParameters="${keyParameters}"$(cat <<EOF
  76. Revoker: 1:$REVOKER sensitive
  77. EOF
  78. )
  79. fi
  80. echo "The following key parameters will be used:"
  81. echo "$keyParameters"
  82. read -p "Generate key? [Y|n]: " OK; OK=${OK:=Y}
  83. if [ ${OK/y/Y} != 'Y' ] ; then
  84. failure "aborting."
  85. fi
  86. # add commit command
  87. keyParameters="${keyParameters}"$(cat <<EOF
  88. %commit
  89. %echo done
  90. EOF
  91. )
  92. log "generating server key... "
  93. echo "$keyParameters" | gpg --batch --gen-key
  94. # output the server fingerprint
  95. fingerprint_server_key "=${userID}"
  96. # find the key fingerprint of the server primary key
  97. keyID=$(gpg --list-key --with-colons --with-fingerprint "=${userID}" | \
  98. grep '^fpr:' | head -1 | cut -d: -f10)
  99. # write the key to the file
  100. # NOTE: assumes that the primary key is the proper key to use
  101. (umask 077 && gpgsecret2ssh "$keyID" > "${MS_HOME}/ssh_host_rsa_key")
  102. log "Private SSH host key output to file: ${MS_HOME}/ssh_host_rsa_key"
  103. }
  104. # gpg output key fingerprint
  105. fingerprint_server_key() {
  106. local ID
  107. if [ -z "$1" ] ; then
  108. ID="$1"
  109. else
  110. ID="=ssh://$(hostname --fqdn)"
  111. fi
  112. gpg --fingerprint --list-secret-keys "$ID"
  113. }
  114. ########################################################################
  115. # MAIN
  116. ########################################################################
  117. COMMAND="$1"
  118. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  119. shift
  120. # set ms home directory
  121. MS_HOME=${MS_HOME:-"$ETC"}
  122. # load configuration file
  123. MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere-server.conf}
  124. [ -e "$MS_CONF" ] && . "$MS_CONF"
  125. # set empty config variable with defaults
  126. KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
  127. CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
  128. AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"%h/.config/monkeysphere/authorized_user_ids"}
  129. RAW_AUTHORIZED_KEYS=${RAW_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
  130. # other variables
  131. REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
  132. GNUPGHOME_HOST=${GNUPGHOME_HOST:-"${VARLIB}/gnupg-host"}
  133. GNUPGHOME_AUTHENTICATION=${GNUPGHOME_AUTHENTICATION:-"${VARLIB}/gnupg-authentication"}
  134. # set default GNUPGHOME, and make sure the directory exists
  135. GNUPGHOME="$GNUPGHOME_HOST"
  136. export GNUPGHOME
  137. mkdir -p -m 0700 "$GNUPGHOME"
  138. case $COMMAND in
  139. 'update-users'|'update-user'|'u')
  140. if [ "$1" ] ; then
  141. # get users from command line
  142. unames="$@"
  143. else
  144. # or just look at all users if none specified
  145. unames=$(getent passwd | cut -d: -f1)
  146. fi
  147. # set mode
  148. MODE="authorized_keys"
  149. # make sure the authorized_keys directory exists
  150. mkdir -p "${VARLIB}/authorized_keys"
  151. # set GNUPGHOME, and make sure the directory exists
  152. GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
  153. export GNUPGHOME
  154. mkdir -p -m 0700 "$GNUPGHOME"
  155. # loop over users
  156. for uname in $unames ; do
  157. # check all specified users exist
  158. if ! getent passwd "$uname" >/dev/null ; then
  159. error "----- unknown user '$uname' -----"
  160. continue
  161. fi
  162. # set authorized_user_ids and raw authorized_keys variables,
  163. # translating ssh-style path variables
  164. authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
  165. rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
  166. # if neither is found, skip user
  167. if [ ! -s "$authorizedUserIDs" -a ! -s "$rawAuthorizedKeys" ] ; then
  168. continue
  169. fi
  170. log "----- user: $uname -----"
  171. # temporary authorized_keys file
  172. AUTHORIZED_KEYS=$(mktemp)
  173. # trap to delete file on exit
  174. trap "rm -f $AUTHORIZE_KEYS" EXIT
  175. # process authorized_user_ids file
  176. if [ -s "$authorizedUserIDs" ] ; then
  177. log "processing authorized_user_ids file..."
  178. process_authorized_user_ids "$authorizedUserIDs"
  179. fi
  180. # add user-controlled authorized_keys file path if specified
  181. if [ "$RAW_AUTHORIZED_KEYS" != '-' ] ; then
  182. if [ -s "$rawAuthorizedKeys" ] ; then
  183. log -n "adding raw authorized_keys file... "
  184. cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
  185. loge "done."
  186. fi
  187. fi
  188. # if the resulting authorized_keys file is not empty, move
  189. # the temp authorized_keys file into place
  190. if [ -s "$AUTHORIZED_KEYS" ] ; then
  191. # openssh appears to check the contents of the
  192. # authorized_keys file as the user in question, so the
  193. # file must be readable by that user at least.
  194. # FIXME: is there a better way to do this?
  195. chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
  196. chmod g+r "$AUTHORIZED_KEYS"
  197. mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
  198. log "authorized_keys file updated."
  199. # else destroy it
  200. else
  201. rm -f "$AUTHORIZED_KEYS"
  202. fi
  203. done
  204. ;;
  205. 'gen-key'|'g')
  206. gen_key "$1"
  207. ;;
  208. 'show-fingerprint'|'f')
  209. fingerprint_server_key "$@"
  210. ;;
  211. 'publish-key'|'p')
  212. publish_server_key
  213. ;;
  214. 'trust-key'|'trust-key'|'t')
  215. trust_key "$@"
  216. ;;
  217. 'help'|'h'|'?')
  218. usage
  219. ;;
  220. *)
  221. failure "Unknown command: '$COMMAND'
  222. Type '$PGRM help' for usage."
  223. ;;
  224. esac
  225. exit "$ERR"