summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: 5e7a9312759f0e8e2772cd868d8a8c79d6eba57c (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. ########################################################################
  33. # FUNCTIONS
  34. ########################################################################
  35. usage() {
  36. cat <<EOF >&2
  37. usage: $PGRM <subcommand> [options] [args]
  38. Monkeysphere host admin tool.
  39. subcommands:
  40. import-key (i) FILE NAME[:PORT] import existing ssh key to gpg
  41. show-key (s) output all host key information
  42. publish-key (p) publish host key to keyserver
  43. set-expire (e) [EXPIRE] set host key expiration
  44. add-hostname (n+) NAME[:PORT] add hostname user ID to host key
  45. revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
  46. add-revoker (r+) [KEYID|FILE] add a revoker to the host key
  47. revoke-key generate and/or publish revocation
  48. certificate for host key
  49. version (v) show version number
  50. help (h,?) this help
  51. See ${PGRM}(8) for more info.
  52. EOF
  53. }
  54. # function to interact with the gpg keyring
  55. gpg_host() {
  56. GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
  57. }
  58. # command to list the info about the host key, in colon format, to
  59. # stdout
  60. gpg_host_list() {
  61. gpg_host --list-keys --with-colons --fixed-list-mode \
  62. --with-fingerprint --with-fingerprint \
  63. "0x${HOST_FINGERPRINT}!"
  64. }
  65. # command for edit key scripts, takes scripts on stdin
  66. gpg_host_edit() {
  67. gpg_host --command-fd 0 --edit-key "0x${HOST_FINGERPRINT}!" "$@"
  68. }
  69. # export the host public key to the monkeysphere gpg pub key file
  70. update_gpg_pub_file() {
  71. log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
  72. gpg_host --export --armor --export-options export-minimal \
  73. "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
  74. }
  75. # load the host fingerprint into the fingerprint variable, using the
  76. # export gpg pub key file
  77. # FIXME: this seems much less than ideal, with all this temp keyring
  78. # stuff. is there a way we can do this without having to create temp
  79. # files? what if we stored the fingerprint in MHDATADIR/fingerprint?
  80. load_fingerprint() {
  81. if [ -f "$HOST_KEY_FILE" ] ; then
  82. HOST_FINGERPRINT=$( \
  83. (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
  84. && gpg --quiet --import \
  85. && gpg --quiet --list-keys --with-colons --with-fingerprint \
  86. && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
  87. | grep '^fpr:' | cut -d: -f10 )
  88. else
  89. failure "host key gpg pub file not found."
  90. fi
  91. }
  92. # load the host fingerprint into the fingerprint variable, using the
  93. # gpg host secret key
  94. load_fingerprint_secret() {
  95. HOST_FINGERPRINT=$( \
  96. gpg_host --list-secret-key --with-colons --with-fingerprint \
  97. | grep '^fpr:' | cut -d: -f10 )
  98. }
  99. # fail if host key present
  100. check_host_key() {
  101. [ ! -s "$HOST_KEY_FILE" ] \
  102. || failure "An OpenPGP host key already exists."
  103. }
  104. # fail if host key not present
  105. check_host_no_key() {
  106. [ -s "$HOST_KEY_FILE" ] \
  107. || failure "You don't appear to have a Monkeysphere host key on this server.
  108. Please run 'monkeysphere-host import-key...' first."
  109. }
  110. # output the index of a user ID on the host key
  111. # return 1 if user ID not found
  112. find_host_userid() {
  113. local userID="$1"
  114. local tmpuidMatch
  115. local line
  116. # match to only ultimately trusted user IDs
  117. tmpuidMatch="u:$(echo $userID | gpg_escape)"
  118. # find the index of the requsted user ID
  119. # NOTE: this is based on circumstantial evidence that the order of
  120. # this output is the appropriate index
  121. line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
  122. grep -n -x -F "$tmpuidMatch" 2>/dev/null)
  123. if [ "$line" ] ; then
  124. echo ${line%%:*}
  125. return 0
  126. else
  127. return 1
  128. fi
  129. }
  130. # show info about the host key
  131. show_key() {
  132. local GNUPGHOME
  133. local TMPSSH
  134. local revokers
  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 revokers, if there are any
  155. revokers=$(gpg --list-keys --with-colons --fixed-list-mode \
  156. | awk -F: '/^rvk:/{ print $10 }' )
  157. if [ "$revokers" ] ; then
  158. echo "The following keys are allowed to revoke this host key:"
  159. for key in $revokers ; do
  160. echo "revoker: $key"
  161. done
  162. echo
  163. fi
  164. # list the pgp fingerprint
  165. echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
  166. # list the ssh fingerprint
  167. echo -n "ssh fingerprint: "
  168. ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
  169. # remove the tmp file
  170. trap - EXIT
  171. rm -rf "$GNUPGHOME"
  172. }
  173. ########################################################################
  174. # MAIN
  175. ########################################################################
  176. # load configuration file
  177. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
  178. && . "$MONKEYSPHERE_HOST_CONFIG"
  179. # set empty config variable with ones from the environment, or with
  180. # defaults
  181. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  182. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  183. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  184. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
  185. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  186. # other variables
  187. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  188. # export variables needed in su invocation
  189. export DATE
  190. export LOG_LEVEL
  191. export KEYSERVER
  192. export CHECK_KEYSERVER
  193. export MONKEYSPHERE_USER
  194. export PROMPT
  195. export GNUPGHOME_HOST
  196. export GNUPGHOME
  197. export HOST_FINGERPRINT
  198. # get subcommand
  199. COMMAND="$1"
  200. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  201. shift
  202. case $COMMAND in
  203. 'import-key'|'i')
  204. check_host_key
  205. source "${MHSHAREDIR}/import_key"
  206. import_key "$@"
  207. ;;
  208. 'show-key'|'show'|'s')
  209. check_host_no_key
  210. show_key
  211. ;;
  212. 'set-expire'|'extend-key'|'e')
  213. check_host_no_key
  214. load_fingerprint
  215. source "${MHSHAREDIR}/set_expire"
  216. set_expire "$@"
  217. ;;
  218. 'add-hostname'|'add-name'|'n+')
  219. check_host_no_key
  220. load_fingerprint
  221. source "${MHSHAREDIR}/add_hostname"
  222. add_hostname "$@"
  223. ;;
  224. 'revoke-hostname'|'revoke-name'|'n-')
  225. check_host_no_key
  226. load_fingerprint
  227. source "${MHSHAREDIR}/revoke_hostname"
  228. revoke_hostname "$@"
  229. ;;
  230. 'add-revoker'|'r+')
  231. check_host_no_key
  232. load_fingerprint
  233. source "${MHSHAREDIR}/add_revoker"
  234. add_revoker "$@"
  235. ;;
  236. 'revoke-key')
  237. check_host_no_key
  238. load_fingerprint
  239. source "${MHSHAREDIR}/revoke_key"
  240. revoke_key "$@"
  241. ;;
  242. 'publish-key'|'publish'|'p')
  243. check_host_no_key
  244. load_fingerprint
  245. source "${MHSHAREDIR}/publish_key"
  246. publish_key
  247. ;;
  248. 'diagnostics'|'d')
  249. load_fingerprint
  250. source "${MHSHAREDIR}/diagnostics"
  251. diagnostics
  252. ;;
  253. 'update-gpg-pub-file')
  254. load_fingerprint_secret
  255. update_gpg_pub_file
  256. ;;
  257. 'version'|'v')
  258. version
  259. ;;
  260. '--help'|'help'|'-h'|'h'|'?')
  261. usage
  262. ;;
  263. *)
  264. failure "Unknown command: '$COMMAND'
  265. Type '$PGRM help' for usage."
  266. ;;
  267. esac