summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: 4c7df88e31a72fdd18b63a8e59ee87d47e0ec4d9 (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. 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) [KEYID|FILE] 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. gpg_host_edit() {
  68. gpg_host --command-fd 0 --edit-key "0x${HOST_FINGERPRINT}!" "$@"
  69. }
  70. # export the host public key to the monkeysphere gpg pub key file
  71. update_gpg_pub_file() {
  72. log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
  73. gpg_host --export --armor --export-options export-minimal \
  74. "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
  75. }
  76. # load the host fingerprint into the fingerprint variable, using the
  77. # export gpg pub key file
  78. # FIXME: this seems much less than ideal, with all this temp keyring
  79. # stuff. is there a way we can do this without having to create temp
  80. # files? what if we stored the fingerprint in MHDATADIR/fingerprint?
  81. load_fingerprint() {
  82. if [ -f "$HOST_KEY_FILE" ] ; then
  83. HOST_FINGERPRINT=$( \
  84. (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
  85. && gpg --quiet --import \
  86. && gpg --quiet --list-keys --with-colons --with-fingerprint \
  87. && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
  88. | grep '^fpr:' | cut -d: -f10 )
  89. else
  90. failure "host key gpg pub file not found."
  91. fi
  92. }
  93. # load the host fingerprint into the fingerprint variable, using the
  94. # gpg host secret key
  95. load_fingerprint_secret() {
  96. HOST_FINGERPRINT=$( \
  97. gpg_host --list-secret-key --with-colons --with-fingerprint \
  98. | grep '^fpr:' | cut -d: -f10 )
  99. }
  100. # fail if host key present
  101. check_host_key() {
  102. [ ! -s "$HOST_KEY_FILE" ] \
  103. || failure "An OpenPGP host key already exists."
  104. }
  105. # fail if host key not present
  106. check_host_no_key() {
  107. [ -s "$HOST_KEY_FILE" ] \
  108. || failure "You don't appear to have a Monkeysphere host key on this server.
  109. Please run 'monkeysphere-host import-key...' first."
  110. }
  111. # output the index of a user ID on the host key
  112. # return 1 if user ID not found
  113. find_host_userid() {
  114. local userID="$1"
  115. local tmpuidMatch
  116. local line
  117. # match to only ultimately trusted user IDs
  118. tmpuidMatch="u:$(echo $userID | gpg_escape)"
  119. # find the index of the requsted user ID
  120. # NOTE: this is based on circumstantial evidence that the order of
  121. # this output is the appropriate index
  122. line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
  123. grep -n -x -F "$tmpuidMatch" 2>/dev/null)
  124. if [ "$line" ] ; then
  125. echo ${line%%:*}
  126. return 0
  127. else
  128. return 1
  129. fi
  130. }
  131. # show info about the host key
  132. show_key() {
  133. local GNUPGHOME
  134. # tmp gpghome dir
  135. export GNUPGHOME=$(msmktempdir)
  136. # trap to remove tmp dir if break
  137. trap "rm -rf $GNUPGHOME" EXIT
  138. # import the host key into the tmp dir
  139. gpg --quiet --import <"$HOST_KEY_FILE"
  140. # create the ssh key
  141. TMPSSH="$GNUPGHOME"/ssh_host_key_rsa_pub
  142. openpgp2ssh <"$HOST_KEY_FILE" 2>/dev/null >"$TMPSSH"
  143. # get the gpg fingerprint
  144. HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
  145. | grep '^fpr:' | cut -d: -f10 )
  146. # list the host key info
  147. # FIXME: make no-show-keyring work so we don't have to do the grep'ing
  148. # FIXME: can we show uid validity somehow?
  149. gpg --list-keys --fingerprint \
  150. --list-options show-unusable-uids 2>/dev/null \
  151. | grep -v "^${GNUPGHOME}/pubring.gpg$" \
  152. | egrep -v '^-+$'
  153. # list the pgp fingerprint
  154. echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
  155. # list the ssh fingerprint
  156. echo -n "ssh fingerprint: "
  157. ssh-keygen -l -f "$TMPSSH" | 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. 'import-key'|'i')
  193. check_host_key
  194. source "${MHSHAREDIR}/import_key"
  195. import_key "$@"
  196. ;;
  197. 'show-key'|'show'|'s')
  198. check_host_no_key
  199. show_key
  200. ;;
  201. 'set-expire'|'extend-key'|'e')
  202. check_host_no_key
  203. load_fingerprint
  204. source "${MHSHAREDIR}/set_expire"
  205. set_expire "$@"
  206. ;;
  207. 'add-hostname'|'add-name'|'n+')
  208. check_host_no_key
  209. load_fingerprint
  210. source "${MHSHAREDIR}/add_hostname"
  211. add_hostname "$@"
  212. ;;
  213. 'revoke-hostname'|'revoke-name'|'n-')
  214. check_host_no_key
  215. load_fingerprint
  216. source "${MHSHAREDIR}/revoke_hostname"
  217. revoke_hostname "$@"
  218. ;;
  219. 'add-revoker'|'o')
  220. check_host_no_key
  221. load_fingerprint
  222. source "${MHSHAREDIR}/add_revoker"
  223. add_revoker "$@"
  224. ;;
  225. 'revoke-key'|'r')
  226. check_host_no_key
  227. load_fingerprint
  228. source "${MHSHAREDIR}/revoke_key"
  229. revoke_key "$@"
  230. ;;
  231. 'publish-key'|'publish'|'p')
  232. check_host_no_key
  233. load_fingerprint
  234. source "${MHSHAREDIR}/publish_key"
  235. publish_key
  236. ;;
  237. 'diagnostics'|'d')
  238. load_fingerprint
  239. source "${MHSHAREDIR}/diagnostics"
  240. diagnostics
  241. ;;
  242. 'update-gpg-pub-file')
  243. load_fingerprint_secret
  244. update_gpg_pub_file
  245. ;;
  246. 'version'|'v')
  247. echo "$VERSION"
  248. ;;
  249. '--help'|'help'|'-h'|'h'|'?')
  250. usage
  251. ;;
  252. *)
  253. failure "Unknown command: '$COMMAND'
  254. Type '$PGRM help' for usage."
  255. ;;
  256. esac
  257. exit "$RETURN"