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