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