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