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