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