summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: baa7a87bf482f13706c1414a9f3f9e2056d5a689 (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. # FIXME: show revokers as well
  151. gpg --list-keys --fingerprint \
  152. --list-options show-unusable-uids 2>/dev/null \
  153. | grep -v "^${GNUPGHOME}/pubring.gpg$" \
  154. | egrep -v '^-+$'
  155. # list the pgp fingerprint
  156. echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
  157. # list the ssh fingerprint
  158. echo -n "ssh fingerprint: "
  159. ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
  160. # remove the tmp file
  161. trap - EXIT
  162. rm -rf "$GNUPGHOME"
  163. }
  164. ########################################################################
  165. # MAIN
  166. ########################################################################
  167. # load configuration file
  168. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
  169. && . "$MONKEYSPHERE_HOST_CONFIG"
  170. # set empty config variable with ones from the environment, or with
  171. # defaults
  172. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  173. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  174. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  175. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
  176. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  177. # other variables
  178. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  179. # export variables needed in su invocation
  180. export DATE
  181. export LOG_LEVEL
  182. export KEYSERVER
  183. export CHECK_KEYSERVER
  184. export MONKEYSPHERE_USER
  185. export PROMPT
  186. export GNUPGHOME_HOST
  187. export GNUPGHOME
  188. export HOST_FINGERPRINT
  189. # get subcommand
  190. COMMAND="$1"
  191. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  192. shift
  193. case $COMMAND in
  194. 'import-key'|'i')
  195. check_host_key
  196. source "${MHSHAREDIR}/import_key"
  197. import_key "$@"
  198. ;;
  199. 'show-key'|'show'|'s')
  200. check_host_no_key
  201. show_key
  202. ;;
  203. 'set-expire'|'extend-key'|'e')
  204. check_host_no_key
  205. load_fingerprint
  206. source "${MHSHAREDIR}/set_expire"
  207. set_expire "$@"
  208. ;;
  209. 'add-hostname'|'add-name'|'n+')
  210. check_host_no_key
  211. load_fingerprint
  212. source "${MHSHAREDIR}/add_hostname"
  213. add_hostname "$@"
  214. ;;
  215. 'revoke-hostname'|'revoke-name'|'n-')
  216. check_host_no_key
  217. load_fingerprint
  218. source "${MHSHAREDIR}/revoke_hostname"
  219. revoke_hostname "$@"
  220. ;;
  221. 'add-revoker'|'r+')
  222. check_host_no_key
  223. load_fingerprint
  224. source "${MHSHAREDIR}/add_revoker"
  225. add_revoker "$@"
  226. ;;
  227. 'revoke-key')
  228. check_host_no_key
  229. load_fingerprint
  230. source "${MHSHAREDIR}/revoke_key"
  231. revoke_key "$@"
  232. ;;
  233. 'publish-key'|'publish'|'p')
  234. check_host_no_key
  235. load_fingerprint
  236. source "${MHSHAREDIR}/publish_key"
  237. publish_key
  238. ;;
  239. 'diagnostics'|'d')
  240. load_fingerprint
  241. source "${MHSHAREDIR}/diagnostics"
  242. diagnostics
  243. ;;
  244. 'update-gpg-pub-file')
  245. load_fingerprint_secret
  246. update_gpg_pub_file
  247. ;;
  248. 'version'|'v')
  249. echo "$VERSION"
  250. ;;
  251. '--help'|'help'|'-h'|'h'|'?')
  252. usage
  253. ;;
  254. *)
  255. failure "Unknown command: '$COMMAND'
  256. Type '$PGRM help' for usage."
  257. ;;
  258. esac
  259. exit "$RETURN"