summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: 4c978c94809b3075056d5b9045b61b3b207a0629 (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. local ret=0
  88. # FIXME: you shouldn't have to be root to see the host key fingerprint
  89. if is_root ; then
  90. check_host_keyring
  91. fingerprintPGP=$(fingerprint_server_key)
  92. gpg_authentication "--fingerprint --list-key --list-options show-unusable-uids $fingerprintPGP" 2>/dev/null
  93. echo "OpenPGP fingerprint: $fingerprintPGP"
  94. else
  95. log info "You must be root to see host OpenPGP fingerprint."
  96. ret='1'
  97. fi
  98. if [ -f "${SYSDATADIR}/ssh_host_rsa_key.pub" ] ; then
  99. fingerprintSSH=$(ssh-keygen -l -f "${SYSDATADIR}/ssh_host_rsa_key.pub" | \
  100. awk '{ print $1, $2, $4 }')
  101. echo "ssh fingerprint: $fingerprintSSH"
  102. else
  103. log info "SSH host key not found."
  104. ret='1'
  105. fi
  106. return $ret
  107. }
  108. ########################################################################
  109. # MAIN
  110. ########################################################################
  111. # unset variables that should be defined only in config file
  112. unset KEYSERVER
  113. unset MONKEYSPHERE_USER
  114. # load configuration file
  115. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
  116. # set empty config variable with ones from the environment, or with
  117. # defaults
  118. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
  119. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
  120. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
  121. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  122. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  123. # other variables
  124. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  125. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${SYSDATADIR}/host"}
  126. # export variables needed in su invocation
  127. export DATE
  128. export MODE
  129. export LOG_LEVEL
  130. export MONKEYSPHERE_USER
  131. export KEYSERVER
  132. export GNUPGHOME_HOST
  133. export GNUPGHOME
  134. # get subcommand
  135. COMMAND="$1"
  136. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  137. shift
  138. case $COMMAND in
  139. 'show-key'|'show'|'s')
  140. check_host_keyring
  141. show_key
  142. ;;
  143. 'extend-key'|'e')
  144. check_host_keyring
  145. source "${MHSHAREDIR}/extend_key"
  146. extend_key "$@"
  147. ;;
  148. 'add-hostname'|'add-name'|'n+')
  149. check_host_keyring
  150. source "${MHSHAREDIR}/add_hostname"
  151. add_hostname "$@"
  152. ;;
  153. 'revoke-hostname'|'revoke-name'|'n-')
  154. check_host_keyring
  155. source "${MHSHAREDIR}/revoke_hostname"
  156. revoke_hostname "$@"
  157. ;;
  158. 'add-revoker'|'o')
  159. check_host_keyring
  160. source "${MHSHAREDIR}/add_revoker"
  161. add_revoker "$@"
  162. ;;
  163. 'revoke-key'|'r')
  164. check_host_keyring
  165. source "${MHSHAREDIR}/revoke_key"
  166. revoke_key "$@"
  167. ;;
  168. 'publish-key'|'publish'|'p')
  169. check_host_keyring
  170. source "${MHSHAREDIR}/publish_key"
  171. publish_key
  172. ;;
  173. 'expert'|'e')
  174. SUBCOMMAND="$1"
  175. shift
  176. case "$SUBCOMMAND" in
  177. 'help'|'h'|'?')
  178. cat <<EOF
  179. usage: $PGRM expert <subcommand> [options] [args]
  180. expert subcommands:
  181. import-key (i) [NAME[:PORT]] import existing ssh key to gpg
  182. --keyfile (-f) FILE key file to import
  183. --expire (-e) EXPIRE date to expire
  184. gen-key (g) [NAME[:PORT]] generate gpg key for the host
  185. --length (-l) BITS key length in bits (2048)
  186. --expire (-e) EXPIRE date to expire
  187. diagnostics (d) monkeysphere host status
  188. EOF
  189. ;;
  190. 'import-key'|'i')
  191. source "${MHSHAREDIR}/import_key"
  192. import_key "$@"
  193. ;;
  194. 'gen-key'|'g')
  195. source "${MHSHAREDIR}/gen_key"
  196. gen_key "$@"
  197. ;;
  198. 'diagnostics'|'d')
  199. source "${MHSHAREDIR}/diagnostics"
  200. diagnostics
  201. ;;
  202. *)
  203. failure "Unknown expert subcommand: '$COMMAND'
  204. Type '$PGRM help' for usage."
  205. ;;
  206. esac
  207. ;;
  208. 'version'|'v')
  209. echo "$VERSION"
  210. ;;
  211. '--help'|'help'|'-h'|'h'|'?')
  212. usage
  213. ;;
  214. *)
  215. failure "Unknown command: '$COMMAND'
  216. Type '$PGRM help' for usage."
  217. ;;
  218. esac
  219. exit "$RETURN"