summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: aa764dbcd54cdabb415b23b8f226318d1eb080fc (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. set -e
  14. PGRM=$(basename $0)
  15. SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
  16. export SYSSHAREDIR
  17. . "${SYSSHAREDIR}/common" || exit 1
  18. SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
  19. export SYSDATADIR
  20. # sharedir for host functions
  21. MHSHAREDIR="${SYSSHAREDIR}/mh"
  22. # datadir for host functions
  23. MHDATADIR="${SYSDATADIR}/host"
  24. # UTC date in ISO 8601 format if needed
  25. DATE=$(date -u '+%FT%T')
  26. # unset some environment variables that could screw things up
  27. unset GREP_OPTIONS
  28. # default return code
  29. RETURN=0
  30. ########################################################################
  31. # FUNCTIONS
  32. ########################################################################
  33. usage() {
  34. cat <<EOF >&2
  35. usage: $PGRM <subcommand> [options] [args]
  36. Monkeysphere host admin tool.
  37. subcommands:
  38. show-key (s) output all host key information
  39. set-expire (e) EXPIRE set host key expiration
  40. add-hostname (n+) NAME[:PORT] add hostname user ID to host key
  41. revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
  42. add-revoker (o) FINGERPRINT add a revoker to the host key
  43. revoke-key (r) revoke host key
  44. publish-key (p) publish host key to keyserver
  45. expert <expert-subcommand> run expert command
  46. expert help expert command help
  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 gpg keyring
  63. gpg_host() {
  64. GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
  65. }
  66. # command to list the info about the host key, in colon format
  67. gpg_host_list() {
  68. gpg_host --list-keys --with-colons --fixed-list-mode \
  69. --with-fingerprint --with-fingerprint \
  70. "0x${HOST_FINGERPRINT}!"
  71. }
  72. # command for edit key scripts, takes scripts on stdin
  73. gpg_host_edit() {
  74. gpg_host --quiet --command-fd 0 --edit-key \
  75. "0x${HOST_FINGERPRINT}!" "$@"
  76. }
  77. # output just key fingerprint
  78. fingerprint_host_key() {
  79. # set the pipefail option so functions fails if can't read sec key
  80. set -o pipefail
  81. gpg_host --list-secret-keys --fingerprint \
  82. --with-colons --fixed-list-mode 2> /dev/null | \
  83. grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
  84. }
  85. # output the index of a user ID on the host key
  86. # return 1 if user ID not found
  87. find_host_userid() {
  88. local userID="$1"
  89. local tmpuidMatch
  90. local line
  91. # match to only ultimately trusted user IDs
  92. tmpuidMatch="u:$(echo $userID | gpg_escape)"
  93. # find the index of the requsted user ID
  94. # NOTE: this is based on circumstantial evidence that the order of
  95. # this output is the appropriate index
  96. line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
  97. grep -n -x -F "$tmpuidMatch" 2>/dev/null)
  98. if [ "$line" ] ; then
  99. echo ${line%%:*}
  100. return 0
  101. else
  102. return 1
  103. fi
  104. }
  105. # function to check for host secret key
  106. check_host_fail() {
  107. [ "$HOST_FINGERPRINT" ] || \
  108. failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-host expert import-key' first."
  109. }
  110. # show info about the host key
  111. show_key() {
  112. local fingerprintSSH
  113. # FIXME: should not have to be priviledged user to see this info.
  114. # should be taken from publicly accessible key files, instead of
  115. # the keyring.
  116. gpg_host --fingerprint --list-key --list-options show-unusable-uids \
  117. "0x${HOST_FINGERPRINT}!" 2>/dev/null
  118. echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
  119. if [ -f "${MHDATADIR}/ssh_host_rsa_key.pub" ] ; then
  120. fingerprintSSH=$(ssh-keygen -l -f "${MHDATADIR}/ssh_host_rsa_key.pub" | \
  121. awk '{ print $1, $2, $4 }')
  122. echo "ssh fingerprint: $fingerprintSSH"
  123. else
  124. log info "SSH host key not found."
  125. fi
  126. # FIXME: show expiration date
  127. # FIXME: other relevant key parameters?
  128. }
  129. ########################################################################
  130. # MAIN
  131. ########################################################################
  132. # unset variables that should be defined only in config file
  133. unset KEYSERVER
  134. unset MONKEYSPHERE_USER
  135. # load configuration file
  136. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
  137. # set empty config variable with ones from the environment, or with
  138. # defaults
  139. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
  140. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
  141. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
  142. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  143. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  144. # other variables
  145. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  146. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  147. # host key fingerprint
  148. HOST_FINGERPRINT=$(fingerprint_host_key)
  149. # export variables needed in su invocation
  150. export DATE
  151. export MODE
  152. export LOG_LEVEL
  153. export MONKEYSPHERE_USER
  154. export KEYSERVER
  155. export GNUPGHOME_HOST
  156. export GNUPGHOME
  157. export HOST_FINGERPRINT
  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. check_host_keyring
  165. show_key
  166. ;;
  167. 'set-expire'|'extend-key'|'e')
  168. check_host_keyring
  169. source "${MHSHAREDIR}/set_expire"
  170. set_expire "$@"
  171. ;;
  172. 'add-hostname'|'add-name'|'n+')
  173. check_host_keyring
  174. source "${MHSHAREDIR}/add_hostname"
  175. add_hostname "$@"
  176. ;;
  177. 'revoke-hostname'|'revoke-name'|'n-')
  178. check_host_keyring
  179. source "${MHSHAREDIR}/revoke_hostname"
  180. revoke_hostname "$@"
  181. ;;
  182. 'add-revoker'|'o')
  183. check_host_keyring
  184. source "${MHSHAREDIR}/add_revoker"
  185. add_revoker "$@"
  186. ;;
  187. 'revoke-key'|'r')
  188. check_host_keyring
  189. source "${MHSHAREDIR}/revoke_key"
  190. revoke_key "$@"
  191. ;;
  192. 'publish-key'|'publish'|'p')
  193. check_host_keyring
  194. source "${MHSHAREDIR}/publish_key"
  195. publish_key
  196. ;;
  197. 'expert')
  198. SUBCOMMAND="$1"
  199. shift
  200. case "$SUBCOMMAND" in
  201. 'help'|'h'|'?')
  202. cat <<EOF
  203. usage: $PGRM expert <subcommand> [options] [args]
  204. expert subcommands:
  205. import-key (i) [NAME[:PORT]] import existing ssh key to gpg
  206. gen-key (g) [NAME[:PORT]] generate gpg key for the host
  207. --length (-l) BITS key length in bits (2048)
  208. diagnostics (d) monkeysphere host status
  209. EOF
  210. ;;
  211. 'import-key'|'i')
  212. source "${MHSHAREDIR}/import_key"
  213. import_key "$@"
  214. ;;
  215. 'gen-key'|'g')
  216. source "${MHSHAREDIR}/gen_key"
  217. gen_key "$@"
  218. ;;
  219. 'diagnostics'|'d')
  220. source "${MHSHAREDIR}/diagnostics"
  221. diagnostics
  222. ;;
  223. *)
  224. failure "Unknown expert subcommand: '$COMMAND'
  225. Type '$PGRM help' for usage."
  226. ;;
  227. esac
  228. ;;
  229. 'version'|'v')
  230. echo "$VERSION"
  231. ;;
  232. '--help'|'help'|'-h'|'h'|'?')
  233. usage
  234. ;;
  235. *)
  236. failure "Unknown command: '$COMMAND'
  237. Type '$PGRM help' for usage."
  238. ;;
  239. esac
  240. exit "$RETURN"