summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: 540a8ab1c0e3a531ef13abf72cc5afbf761c27fa (plain)
  1. #!/usr/bin/env bash
  2. # monkeysphere-host: Monkeysphere host admin tool
  3. #
  4. # The monkeysphere scripts are written by:
  5. # Jameson Rollins <jrollins@finestructure.net>
  6. # Jamie McClelland <jm@mayfirst.org>
  7. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  8. # Micah Anderson <micah@riseup.net>
  9. #
  10. # They are Copyright 2008-2009, and are all released under the GPL,
  11. # version 3 or later.
  12. ########################################################################
  13. set -e
  14. # set the pipefail option so pipelines fail on first command failure
  15. set -o pipefail
  16. PGRM=$(basename $0)
  17. SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
  18. export SYSSHAREDIR
  19. . "${SYSSHAREDIR}/common" || exit 1
  20. SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
  21. export SYSDATADIR
  22. # sharedir for host functions
  23. MHSHAREDIR="${SYSSHAREDIR}/mh"
  24. # datadir for host functions
  25. MHDATADIR="${SYSDATADIR}/host"
  26. # host pub key files
  27. HOST_KEY_FILE="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  28. # UTC date in ISO 8601 format if needed
  29. DATE=$(date -u '+%FT%T')
  30. # unset some environment variables that could screw things up
  31. unset GREP_OPTIONS
  32. # default return code
  33. RETURN=0
  34. ########################################################################
  35. # FUNCTIONS
  36. ########################################################################
  37. usage() {
  38. cat <<EOF >&2
  39. usage: $PGRM <subcommand> [options] [args]
  40. Monkeysphere host admin tool.
  41. subcommands:
  42. import-key (i) FILE NAME[:PORT] import existing ssh key to gpg
  43. show-key (s) output all host key information
  44. publish-key (p) publish host key to keyserver
  45. set-expire (e) [EXPIRE] set host key expiration
  46. add-hostname (n+) NAME[:PORT] add hostname user ID to host key
  47. revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
  48. add-revoker (r+) [KEYID|FILE] add a revoker to the host key
  49. revoke-key generate and/or publish revocation
  50. certificate for host key
  51. version (v) show version number
  52. help (h,?) this help
  53. See ${PGRM}(8) for more info.
  54. EOF
  55. }
  56. # function to interact with the gpg keyring
  57. gpg_host() {
  58. GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
  59. }
  60. # command to list the info about the host key, in colon format, to
  61. # stdout
  62. gpg_host_list() {
  63. gpg_host --list-keys --with-colons --fixed-list-mode \
  64. --with-fingerprint --with-fingerprint \
  65. "0x${HOST_FINGERPRINT}!"
  66. }
  67. # command for edit key scripts, takes scripts on stdin
  68. gpg_host_edit() {
  69. gpg_host --command-fd 0 --edit-key "0x${HOST_FINGERPRINT}!" "$@"
  70. }
  71. # export the host public key to the monkeysphere gpg pub key file
  72. update_gpg_pub_file() {
  73. log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
  74. gpg_host --export --armor --export-options export-minimal \
  75. "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
  76. }
  77. # load the host fingerprint into the fingerprint variable, using the
  78. # export gpg pub key file
  79. # FIXME: this seems much less than ideal, with all this temp keyring
  80. # stuff. is there a way we can do this without having to create temp
  81. # files? what if we stored the fingerprint in MHDATADIR/fingerprint?
  82. load_fingerprint() {
  83. if [ -f "$HOST_KEY_FILE" ] ; then
  84. HOST_FINGERPRINT=$( \
  85. (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
  86. && gpg --quiet --import \
  87. && gpg --quiet --list-keys --with-colons --with-fingerprint \
  88. && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
  89. | grep '^fpr:' | cut -d: -f10 )
  90. else
  91. failure "host key gpg pub file not found."
  92. fi
  93. }
  94. # load the host fingerprint into the fingerprint variable, using the
  95. # gpg host secret key
  96. load_fingerprint_secret() {
  97. HOST_FINGERPRINT=$( \
  98. gpg_host --list-secret-key --with-colons --with-fingerprint \
  99. | grep '^fpr:' | cut -d: -f10 )
  100. }
  101. # fail if host key present
  102. check_host_key() {
  103. [ ! -s "$HOST_KEY_FILE" ] \
  104. || failure "An OpenPGP host key already exists."
  105. }
  106. # fail if host key not present
  107. check_host_no_key() {
  108. [ -s "$HOST_KEY_FILE" ] \
  109. || failure "You don't appear to have a Monkeysphere host key on this server.
  110. Please run 'monkeysphere-host import-key...' first."
  111. }
  112. # output the index of a user ID on the host key
  113. # return 1 if user ID not found
  114. find_host_userid() {
  115. local userID="$1"
  116. local tmpuidMatch
  117. local line
  118. # match to only ultimately trusted user IDs
  119. tmpuidMatch="u:$(echo $userID | gpg_escape)"
  120. # find the index of the requsted user ID
  121. # NOTE: this is based on circumstantial evidence that the order of
  122. # this output is the appropriate index
  123. line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
  124. grep -n -x -F "$tmpuidMatch" 2>/dev/null)
  125. if [ "$line" ] ; then
  126. echo ${line%%:*}
  127. return 0
  128. else
  129. return 1
  130. fi
  131. }
  132. # show info about the host key
  133. show_key() {
  134. local GNUPGHOME
  135. # tmp gpghome dir
  136. export GNUPGHOME=$(msmktempdir)
  137. # trap to remove tmp dir if break
  138. trap "rm -rf $GNUPGHOME" EXIT
  139. # import the host key into the tmp dir
  140. gpg --quiet --import <"$HOST_KEY_FILE"
  141. # create the ssh key
  142. TMPSSH="$GNUPGHOME"/ssh_host_key_rsa_pub
  143. openpgp2ssh <"$HOST_KEY_FILE" 2>/dev/null >"$TMPSSH"
  144. # get the gpg fingerprint
  145. HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
  146. | grep '^fpr:' | cut -d: -f10 )
  147. # list the host key info
  148. # FIXME: make no-show-keyring work so we don't have to do the grep'ing
  149. # FIXME: can we show uid validity somehow?
  150. gpg --list-keys --fingerprint \
  151. --list-options show-unusable-uids 2>/dev/null \
  152. | grep -v "^${GNUPGHOME}/pubring.gpg$" \
  153. | egrep -v '^-+$'
  154. # list the pgp fingerprint
  155. echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
  156. # list the ssh fingerprint
  157. echo -n "ssh fingerprint: "
  158. ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
  159. # remove the tmp file
  160. trap - EXIT
  161. rm -rf "$GNUPGHOME"
  162. }
  163. ########################################################################
  164. # MAIN
  165. ########################################################################
  166. # load configuration file
  167. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
  168. && . "$MONKEYSPHERE_HOST_CONFIG"
  169. # set empty config variable with ones from the environment, or with
  170. # defaults
  171. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  172. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  173. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  174. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
  175. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  176. # other variables
  177. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  178. # export variables needed in su invocation
  179. export DATE
  180. export LOG_LEVEL
  181. export KEYSERVER
  182. export CHECK_KEYSERVER
  183. export MONKEYSPHERE_USER
  184. export PROMPT
  185. export GNUPGHOME_HOST
  186. export GNUPGHOME
  187. export HOST_FINGERPRINT
  188. # get subcommand
  189. COMMAND="$1"
  190. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  191. shift
  192. case $COMMAND in
  193. 'import-key'|'i')
  194. check_host_key
  195. source "${MHSHAREDIR}/import_key"
  196. import_key "$@"
  197. ;;
  198. 'show-key'|'show'|'s')
  199. check_host_no_key
  200. show_key
  201. ;;
  202. 'set-expire'|'extend-key'|'e')
  203. check_host_no_key
  204. load_fingerprint
  205. source "${MHSHAREDIR}/set_expire"
  206. set_expire "$@"
  207. ;;
  208. 'add-hostname'|'add-name'|'n+')
  209. check_host_no_key
  210. load_fingerprint
  211. source "${MHSHAREDIR}/add_hostname"
  212. add_hostname "$@"
  213. ;;
  214. 'revoke-hostname'|'revoke-name'|'n-')
  215. check_host_no_key
  216. load_fingerprint
  217. source "${MHSHAREDIR}/revoke_hostname"
  218. revoke_hostname "$@"
  219. ;;
  220. 'add-revoker'|'r+')
  221. check_host_no_key
  222. load_fingerprint
  223. source "${MHSHAREDIR}/add_revoker"
  224. add_revoker "$@"
  225. ;;
  226. 'revoke-key')
  227. check_host_no_key
  228. load_fingerprint
  229. source "${MHSHAREDIR}/revoke_key"
  230. revoke_key "$@"
  231. ;;
  232. 'publish-key'|'publish'|'p')
  233. check_host_no_key
  234. load_fingerprint
  235. source "${MHSHAREDIR}/publish_key"
  236. publish_key
  237. ;;
  238. 'diagnostics'|'d')
  239. load_fingerprint
  240. source "${MHSHAREDIR}/diagnostics"
  241. diagnostics
  242. ;;
  243. 'update-gpg-pub-file')
  244. load_fingerprint_secret
  245. update_gpg_pub_file
  246. ;;
  247. 'version'|'v')
  248. echo "$VERSION"
  249. ;;
  250. '--help'|'help'|'-h'|'h'|'?')
  251. usage
  252. ;;
  253. *)
  254. failure "Unknown command: '$COMMAND'
  255. Type '$PGRM help' for usage."
  256. ;;
  257. esac
  258. exit "$RETURN"