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