summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: a86a8c91d27ad30b283b261976ad60e0522ff222 (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. show-key (s) output all host key information
  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 (o) FINGERPRINT add a revoker to the host key
  47. revoke-key (r) revoke host key
  48. publish-key (p) publish host key to keyserver
  49. import-key (i) [NAME[:PORT]] import existing ssh key to gpg
  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. # unset variables that should be defined only in config file or in
  166. # MONKEYSPHERE_ variables
  167. unset LOG_LEVEL
  168. unset KEYSERVER
  169. unset MONKEYSPHERE_USER
  170. unset PROMPT
  171. # load configuration file
  172. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
  173. # set empty config variable with ones from the environment, or with
  174. # defaults
  175. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
  176. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
  177. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  178. PROMPT=${MONKEYSPHERE_PROMPT:=${PROMPT:="true"}}
  179. # other variables
  180. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  181. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  182. # export variables needed in su invocation
  183. export DATE
  184. export MODE
  185. export LOG_LEVEL
  186. export KEYSERVER
  187. export MONKEYSPHERE_USER
  188. export PROMPT
  189. export CHECK_KEYSERVER
  190. export GNUPGHOME_HOST
  191. export GNUPGHOME
  192. export HOST_FINGERPRINT=
  193. # get subcommand
  194. COMMAND="$1"
  195. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  196. shift
  197. case $COMMAND in
  198. 'show-key'|'show'|'s')
  199. check_host_no_key
  200. show_key
  201. ;;
  202. 'set-expire'|'extend-key'|'e')
  203. check_host_no_key
  204. load_fingerprint
  205. source "${MHSHAREDIR}/set_expire"
  206. set_expire "$@"
  207. ;;
  208. 'add-hostname'|'add-name'|'n+')
  209. check_host_no_key
  210. load_fingerprint
  211. source "${MHSHAREDIR}/add_hostname"
  212. add_hostname "$@"
  213. ;;
  214. 'revoke-hostname'|'revoke-name'|'n-')
  215. check_host_no_key
  216. load_fingerprint
  217. source "${MHSHAREDIR}/revoke_hostname"
  218. revoke_hostname "$@"
  219. ;;
  220. 'add-revoker'|'o')
  221. check_host_no_key
  222. load_fingerprint
  223. source "${MHSHAREDIR}/add_revoker"
  224. add_revoker "$@"
  225. ;;
  226. 'revoke-key'|'r')
  227. check_host_no_key
  228. load_fingerprint
  229. source "${MHSHAREDIR}/revoke_key"
  230. revoke_key "$@"
  231. ;;
  232. 'publish-key'|'publish'|'p')
  233. check_host_no_key
  234. load_fingerprint
  235. source "${MHSHAREDIR}/publish_key"
  236. publish_key
  237. ;;
  238. 'import-key'|'i')
  239. check_host_key
  240. source "${MHSHAREDIR}/import_key"
  241. import_key "$@"
  242. ;;
  243. 'diagnostics'|'d')
  244. load_fingerprint
  245. source "${MHSHAREDIR}/diagnostics"
  246. diagnostics
  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"