summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: a6fa62fbd80dafcb4fb218b7b7d3e58b82a50cdb (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. PGRM=$(basename $0)
  15. SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
  16. export SYSSHAREDIR
  17. . "${SYSSHAREDIR}/common" || exit 1
  18. SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
  19. export SYSDATADIR
  20. # sharedir for host functions
  21. MHSHAREDIR="${SYSSHAREDIR}/mh"
  22. # datadir for host functions
  23. MHDATADIR="${SYSDATADIR}/host"
  24. # UTC date in ISO 8601 format if needed
  25. DATE=$(date -u '+%FT%T')
  26. # unset some environment variables that could screw things up
  27. unset GREP_OPTIONS
  28. # default return code
  29. RETURN=0
  30. ########################################################################
  31. # FUNCTIONS
  32. ########################################################################
  33. usage() {
  34. cat <<EOF >&2
  35. usage: $PGRM <subcommand> [options] [args]
  36. Monkeysphere host admin tool.
  37. subcommands:
  38. show-key (s) output all host key information
  39. set-expire (e) EXPIRE set host key expiration
  40. add-hostname (n+) NAME[:PORT] add hostname user ID to host key
  41. revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
  42. add-revoker (o) FINGERPRINT add a revoker to the host key
  43. revoke-key (r) revoke host key
  44. publish-key (p) publish host key to keyserver
  45. expert <expert-subcommand> run expert command
  46. expert help expert command help
  47. version (v) show version number
  48. help (h,?) this help
  49. EOF
  50. }
  51. # function to run command as monkeysphere user
  52. su_monkeysphere_user() {
  53. # if the current user is the monkeysphere user, then just eval
  54. # command
  55. if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
  56. eval "$@"
  57. # otherwise su command as monkeysphere user
  58. else
  59. su "$MONKEYSPHERE_USER" -c "$@"
  60. fi
  61. }
  62. # function to interact with the gpg keyring
  63. gpg_host() {
  64. local returnCode
  65. GNUPGHOME="$GNUPGHOME_HOST"
  66. export GNUPGHOME
  67. # NOTE: we supress this warning because we need the monkeysphere
  68. # user to be able to read the host pubring. we realize this might
  69. # be problematic, but it's the simplest solution, without too much
  70. # loss of security.
  71. gpg "$@"
  72. }
  73. # output just key fingerprint
  74. fingerprint_host_key() {
  75. # set the pipefail option so functions fails if can't read sec key
  76. set -o pipefail
  77. gpg_host --list-secret-keys --fingerprint \
  78. --with-colons --fixed-list-mode 2> /dev/null | \
  79. grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
  80. }
  81. # function to check for host secret key
  82. check_host_keyring() {
  83. fingerprint_host_key >/dev/null \
  84. || failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-host import-key' first."
  85. }
  86. # show info about the host key
  87. show_key() {
  88. local fingerprintPGP
  89. local fingerprintSSH
  90. # FIXME: you shouldn't have to be root to see the host key fingerprint
  91. check_host_keyring
  92. fingerprintPGP=$(fingerprint_host_key)
  93. gpg_host --fingerprint --list-key --list-options show-unusable-uids "0x${fingerprintPGP}!" 2>/dev/null
  94. echo "OpenPGP fingerprint: $fingerprintPGP"
  95. if [ -f "${MHDATADIR}/ssh_host_rsa_key.pub" ] ; then
  96. fingerprintSSH=$(ssh-keygen -l -f "${MHDATADIR}/ssh_host_rsa_key.pub" | \
  97. awk '{ print $1, $2, $4 }')
  98. echo "ssh fingerprint: $fingerprintSSH"
  99. else
  100. log info "SSH host key not found."
  101. fi
  102. # FIXME: show expiration date
  103. # FIXME: other relevant key parameters?
  104. }
  105. ########################################################################
  106. # MAIN
  107. ########################################################################
  108. # unset variables that should be defined only in config file
  109. unset KEYSERVER
  110. unset MONKEYSPHERE_USER
  111. # load configuration file
  112. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
  113. # set empty config variable with ones from the environment, or with
  114. # defaults
  115. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
  116. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
  117. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
  118. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  119. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  120. # other variables
  121. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  122. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  123. # export variables needed in su invocation
  124. export DATE
  125. export MODE
  126. export LOG_LEVEL
  127. export MONKEYSPHERE_USER
  128. export KEYSERVER
  129. export GNUPGHOME_HOST
  130. export GNUPGHOME
  131. # get subcommand
  132. COMMAND="$1"
  133. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  134. shift
  135. case $COMMAND in
  136. 'show-key'|'show'|'s')
  137. check_host_keyring
  138. show_key
  139. ;;
  140. # FIXME: what should we call this command?
  141. 'set-expire'|'extend-key'|'e')
  142. check_host_keyring
  143. source "${MHSHAREDIR}/extend_key"
  144. extend_key "$@"
  145. ;;
  146. 'add-hostname'|'add-name'|'n+')
  147. check_host_keyring
  148. source "${MHSHAREDIR}/add_hostname"
  149. add_hostname "$@"
  150. ;;
  151. 'revoke-hostname'|'revoke-name'|'n-')
  152. check_host_keyring
  153. source "${MHSHAREDIR}/revoke_hostname"
  154. revoke_hostname "$@"
  155. ;;
  156. 'add-revoker'|'o')
  157. check_host_keyring
  158. source "${MHSHAREDIR}/add_revoker"
  159. add_revoker "$@"
  160. ;;
  161. 'revoke-key'|'r')
  162. check_host_keyring
  163. source "${MHSHAREDIR}/revoke_key"
  164. revoke_key "$@"
  165. ;;
  166. 'publish-key'|'publish'|'p')
  167. check_host_keyring
  168. source "${MHSHAREDIR}/publish_key"
  169. publish_key
  170. ;;
  171. 'expert'|'e')
  172. SUBCOMMAND="$1"
  173. shift
  174. case "$SUBCOMMAND" in
  175. 'help'|'h'|'?')
  176. cat <<EOF
  177. usage: $PGRM expert <subcommand> [options] [args]
  178. expert subcommands:
  179. import-key (i) [NAME[:PORT]] import existing ssh key to gpg
  180. gen-key (g) [NAME[:PORT]] generate gpg key for the host
  181. --length (-l) BITS key length in bits (2048)
  182. diagnostics (d) monkeysphere host status
  183. EOF
  184. ;;
  185. 'import-key'|'i')
  186. source "${MHSHAREDIR}/import_key"
  187. import_key "$@"
  188. ;;
  189. 'gen-key'|'g')
  190. source "${MHSHAREDIR}/gen_key"
  191. gen_key "$@"
  192. ;;
  193. 'diagnostics'|'d')
  194. source "${MHSHAREDIR}/diagnostics"
  195. diagnostics
  196. ;;
  197. *)
  198. failure "Unknown expert subcommand: '$COMMAND'
  199. Type '$PGRM help' for usage."
  200. ;;
  201. esac
  202. ;;
  203. 'version'|'v')
  204. echo "$VERSION"
  205. ;;
  206. '--help'|'help'|'-h'|'h'|'?')
  207. usage
  208. ;;
  209. *)
  210. failure "Unknown command: '$COMMAND'
  211. Type '$PGRM help' for usage."
  212. ;;
  213. esac
  214. exit "$RETURN"