summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: bcb570b98df166310938b1f795c445537fc33a95 (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. # export the host key to stdout
  78. gpg_host_export() {
  79. gpg_host --export --armor --export-options export-minimal \
  80. "0x${HOST_FINGERPRINT}!"
  81. }
  82. # export the host key to the monkeysphere host file key
  83. gpg_host_export_to_ssh_file() {
  84. log debug "exporting openpgp public key..."
  85. gpg_host_export > "${MHDATADIR}/ssh_host_rsa_key.pub.gpg"
  86. log info "SSH host public key in OpenPGP form: ${MHDATADIR}/ssh_host_rsa_key.pub.gpg"
  87. }
  88. # output just key fingerprint
  89. fingerprint_host_key() {
  90. # set the pipefail option so functions fails if can't read sec key
  91. set -o pipefail
  92. gpg_host --list-secret-keys --fingerprint \
  93. --with-colons --fixed-list-mode 2> /dev/null | \
  94. grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
  95. }
  96. # output the index of a user ID on the host key
  97. # return 1 if user ID not found
  98. find_host_userid() {
  99. local userID="$1"
  100. local tmpuidMatch
  101. local line
  102. # match to only ultimately trusted user IDs
  103. tmpuidMatch="u:$(echo $userID | gpg_escape)"
  104. # find the index of the requsted user ID
  105. # NOTE: this is based on circumstantial evidence that the order of
  106. # this output is the appropriate index
  107. line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
  108. grep -n -x -F "$tmpuidMatch" 2>/dev/null)
  109. if [ "$line" ] ; then
  110. echo ${line%%:*}
  111. return 0
  112. else
  113. return 1
  114. fi
  115. }
  116. # function to check for host secret key
  117. check_host_fail() {
  118. [ "$HOST_FINGERPRINT" ] || \
  119. failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-host expert import-key' first."
  120. }
  121. # show info about the host key
  122. show_key() {
  123. local fingerprintSSH
  124. # FIXME: should not have to be priviledged user to see this info.
  125. # should be taken from publicly accessible key files, instead of
  126. # the keyring.
  127. gpg_host --fingerprint --list-key --list-options show-unusable-uids \
  128. "0x${HOST_FINGERPRINT}!" 2>/dev/null
  129. echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
  130. if [ -f "${MHDATADIR}/ssh_host_rsa_key.pub" ] ; then
  131. fingerprintSSH=$(ssh-keygen -l -f "${MHDATADIR}/ssh_host_rsa_key.pub" | \
  132. awk '{ print $1, $2, $4 }')
  133. echo "ssh fingerprint: $fingerprintSSH"
  134. else
  135. log info "SSH host key not found."
  136. fi
  137. # FIXME: show expiration date
  138. # FIXME: other relevant key parameters?
  139. }
  140. ########################################################################
  141. # MAIN
  142. ########################################################################
  143. # unset variables that should be defined only in config file
  144. unset KEYSERVER
  145. unset MONKEYSPHERE_USER
  146. # load configuration file
  147. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
  148. # set empty config variable with ones from the environment, or with
  149. # defaults
  150. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
  151. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
  152. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
  153. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  154. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  155. # other variables
  156. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  157. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  158. # host key fingerprint
  159. HOST_FINGERPRINT=$(fingerprint_host_key)
  160. # export variables needed in su invocation
  161. export DATE
  162. export MODE
  163. export LOG_LEVEL
  164. export MONKEYSPHERE_USER
  165. export KEYSERVER
  166. export GNUPGHOME_HOST
  167. export GNUPGHOME
  168. export HOST_FINGERPRINT
  169. # get subcommand
  170. COMMAND="$1"
  171. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  172. shift
  173. case $COMMAND in
  174. 'show-key'|'show'|'s')
  175. check_host_keyring
  176. show_key
  177. ;;
  178. 'set-expire'|'extend-key'|'e')
  179. check_host_keyring
  180. source "${MHSHAREDIR}/set_expire"
  181. set_expire "$@"
  182. ;;
  183. 'add-hostname'|'add-name'|'n+')
  184. check_host_keyring
  185. source "${MHSHAREDIR}/add_hostname"
  186. add_hostname "$@"
  187. ;;
  188. 'revoke-hostname'|'revoke-name'|'n-')
  189. check_host_keyring
  190. source "${MHSHAREDIR}/revoke_hostname"
  191. revoke_hostname "$@"
  192. ;;
  193. 'add-revoker'|'o')
  194. check_host_keyring
  195. source "${MHSHAREDIR}/add_revoker"
  196. add_revoker "$@"
  197. ;;
  198. 'revoke-key'|'r')
  199. check_host_keyring
  200. source "${MHSHAREDIR}/revoke_key"
  201. revoke_key "$@"
  202. ;;
  203. 'publish-key'|'publish'|'p')
  204. check_host_keyring
  205. source "${MHSHAREDIR}/publish_key"
  206. publish_key
  207. ;;
  208. 'expert')
  209. SUBCOMMAND="$1"
  210. shift
  211. case "$SUBCOMMAND" in
  212. 'help'|'h'|'?')
  213. cat <<EOF
  214. usage: $PGRM expert <subcommand> [options] [args]
  215. expert subcommands:
  216. import-key (i) [NAME[:PORT]] import existing ssh key to gpg
  217. gen-key (g) [NAME[:PORT]] generate gpg key for the host
  218. --length (-l) BITS key length in bits (2048)
  219. diagnostics (d) monkeysphere host status
  220. EOF
  221. ;;
  222. 'import-key'|'i')
  223. source "${MHSHAREDIR}/import_key"
  224. import_key "$@"
  225. ;;
  226. 'gen-key'|'g')
  227. source "${MHSHAREDIR}/gen_key"
  228. gen_key "$@"
  229. ;;
  230. 'diagnostics'|'d')
  231. source "${MHSHAREDIR}/diagnostics"
  232. diagnostics
  233. ;;
  234. *)
  235. failure "Unknown expert subcommand: '$COMMAND'
  236. Type '$PGRM help' for usage."
  237. ;;
  238. esac
  239. ;;
  240. 'version'|'v')
  241. echo "$VERSION"
  242. ;;
  243. '--help'|'help'|'-h'|'h'|'?')
  244. usage
  245. ;;
  246. *)
  247. failure "Unknown command: '$COMMAND'
  248. Type '$PGRM help' for usage."
  249. ;;
  250. esac
  251. exit "$RETURN"