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