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