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