summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: 1b0de0cd7c4a60eec47af82d48eb663d3302da41 (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. local TMPSSH
  136. local revokers
  137. # tmp gpghome dir
  138. export GNUPGHOME=$(msmktempdir)
  139. # trap to remove tmp dir if break
  140. trap "rm -rf $GNUPGHOME" EXIT
  141. # import the host key into the tmp dir
  142. gpg --quiet --import <"$HOST_KEY_FILE"
  143. # create the ssh key
  144. TMPSSH="$GNUPGHOME"/ssh_host_key_rsa_pub
  145. openpgp2ssh <"$HOST_KEY_FILE" 2>/dev/null >"$TMPSSH"
  146. # get the gpg fingerprint
  147. HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
  148. | grep '^fpr:' | cut -d: -f10 )
  149. # list the host key info
  150. # FIXME: make no-show-keyring work so we don't have to do the grep'ing
  151. # FIXME: can we show uid validity somehow?
  152. gpg --list-keys --fingerprint \
  153. --list-options show-unusable-uids 2>/dev/null \
  154. | grep -v "^${GNUPGHOME}/pubring.gpg$" \
  155. | egrep -v '^-+$'
  156. # list revokers, if there are any
  157. revokers=$(gpg --list-keys --with-colons --fixed-list-mode \
  158. | awk -F: '/^rvk:/{ print $10 }' )
  159. if [ "$revokers" ] ; then
  160. echo "The following keys are allowed to revoke this host key:"
  161. for key in $revokers ; do
  162. echo "revoker: $key"
  163. done
  164. echo
  165. fi
  166. # list the pgp fingerprint
  167. echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
  168. # list the ssh fingerprint
  169. echo -n "ssh fingerprint: "
  170. ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
  171. # remove the tmp file
  172. trap - EXIT
  173. rm -rf "$GNUPGHOME"
  174. }
  175. ########################################################################
  176. # MAIN
  177. ########################################################################
  178. # load configuration file
  179. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
  180. && . "$MONKEYSPHERE_HOST_CONFIG"
  181. # set empty config variable with ones from the environment, or with
  182. # defaults
  183. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  184. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  185. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  186. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
  187. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  188. # other variables
  189. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  190. # export variables needed in su invocation
  191. export DATE
  192. export LOG_LEVEL
  193. export KEYSERVER
  194. export CHECK_KEYSERVER
  195. export MONKEYSPHERE_USER
  196. export PROMPT
  197. export GNUPGHOME_HOST
  198. export GNUPGHOME
  199. export HOST_FINGERPRINT
  200. # get subcommand
  201. COMMAND="$1"
  202. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  203. shift
  204. case $COMMAND in
  205. 'import-key'|'i')
  206. check_host_key
  207. source "${MHSHAREDIR}/import_key"
  208. import_key "$@"
  209. ;;
  210. 'show-key'|'show'|'s')
  211. check_host_no_key
  212. show_key
  213. ;;
  214. 'set-expire'|'extend-key'|'e')
  215. check_host_no_key
  216. load_fingerprint
  217. source "${MHSHAREDIR}/set_expire"
  218. set_expire "$@"
  219. ;;
  220. 'add-hostname'|'add-name'|'n+')
  221. check_host_no_key
  222. load_fingerprint
  223. source "${MHSHAREDIR}/add_hostname"
  224. add_hostname "$@"
  225. ;;
  226. 'revoke-hostname'|'revoke-name'|'n-')
  227. check_host_no_key
  228. load_fingerprint
  229. source "${MHSHAREDIR}/revoke_hostname"
  230. revoke_hostname "$@"
  231. ;;
  232. 'add-revoker'|'r+')
  233. check_host_no_key
  234. load_fingerprint
  235. source "${MHSHAREDIR}/add_revoker"
  236. add_revoker "$@"
  237. ;;
  238. 'revoke-key')
  239. check_host_no_key
  240. load_fingerprint
  241. source "${MHSHAREDIR}/revoke_key"
  242. revoke_key "$@"
  243. ;;
  244. 'publish-key'|'publish'|'p')
  245. check_host_no_key
  246. load_fingerprint
  247. source "${MHSHAREDIR}/publish_key"
  248. publish_key
  249. ;;
  250. 'diagnostics'|'d')
  251. load_fingerprint
  252. source "${MHSHAREDIR}/diagnostics"
  253. diagnostics
  254. ;;
  255. 'update-gpg-pub-file')
  256. load_fingerprint_secret
  257. update_gpg_pub_file
  258. ;;
  259. 'version'|'v')
  260. echo "$VERSION"
  261. ;;
  262. '--help'|'help'|'-h'|'h'|'?')
  263. usage
  264. ;;
  265. *)
  266. failure "Unknown command: '$COMMAND'
  267. Type '$PGRM help' for usage."
  268. ;;
  269. esac
  270. exit "$RETURN"