summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: 506dcf9eed02bc9fb68c5174e96618d83c3a6e6e (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. # set the pipefail option so pipelines fail on first command failure
  15. set -o pipefail
  16. PGRM=$(basename $0)
  17. SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
  18. export SYSSHAREDIR
  19. . "${SYSSHAREDIR}/common" || exit 1
  20. SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
  21. export SYSDATADIR
  22. # sharedir for host functions
  23. MHSHAREDIR="${SYSSHAREDIR}/mh"
  24. # datadir for host functions
  25. MHDATADIR="${SYSDATADIR}/host"
  26. # UTC date in ISO 8601 format if needed
  27. DATE=$(date -u '+%FT%T')
  28. # unset some environment variables that could screw things up
  29. unset GREP_OPTIONS
  30. # default return code
  31. RETURN=0
  32. ########################################################################
  33. # FUNCTIONS
  34. ########################################################################
  35. usage() {
  36. cat <<EOF >&2
  37. usage: $PGRM <subcommand> [options] [args]
  38. Monkeysphere host admin tool.
  39. subcommands:
  40. show-key (s) output all host key information
  41. set-expire (e) EXPIRE set host key expiration
  42. add-hostname (n+) NAME[:PORT] add hostname user ID to host key
  43. revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
  44. add-revoker (o) FINGERPRINT add a revoker to the host key
  45. revoke-key (r) revoke host key
  46. publish-key (p) publish host key to keyserver
  47. expert <expert-subcommand> run expert command
  48. expert help expert command help
  49. version (v) show version number
  50. help (h,?) this help
  51. EOF
  52. }
  53. # function to interact with the gpg keyring
  54. gpg_host() {
  55. GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
  56. }
  57. # command to list the info about the host key, in colon format
  58. gpg_host_list() {
  59. gpg_host --list-keys --with-colons --fixed-list-mode \
  60. --with-fingerprint --with-fingerprint \
  61. "0x${HOST_FINGERPRINT}!"
  62. }
  63. # command for edit key scripts, takes scripts on stdin
  64. gpg_host_edit() {
  65. gpg_host --quiet --command-fd 0 --edit-key \
  66. "0x${HOST_FINGERPRINT}!" "$@"
  67. }
  68. # export the host key to stdout
  69. gpg_host_export() {
  70. gpg_host --export --armor --export-options export-minimal \
  71. "0x${HOST_FINGERPRINT}!"
  72. }
  73. # export the host key to the monkeysphere host file key
  74. gpg_host_export_to_ssh_file() {
  75. log debug "exporting openpgp public key..."
  76. gpg_host_export > "$HOST_KEY_PUB_GPG"
  77. log info "SSH host public key in OpenPGP form: $HOST_KEY_PUB_GPG"
  78. }
  79. # output just key fingerprint
  80. fingerprint_host_key() {
  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 "$HOST_KEY_PUB" ] ; then
  120. fingerprintSSH=$(ssh-keygen -l -f "$HOST_KEY_PUB" | \
  121. awk '{ print $1, $2, $4 }')
  122. echo "ssh fingerprint: $fingerprintSSH"
  123. else
  124. log error "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. # host pub key files
  150. HOST_KEY_PUB="${SYSDATADIR}/ssh_host_rsa_key.pub"
  151. HOST_KEY_PUB_GPG="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  152. # export variables needed in su invocation
  153. export DATE
  154. export MODE
  155. export LOG_LEVEL
  156. export MONKEYSPHERE_USER
  157. export KEYSERVER
  158. export GNUPGHOME_HOST
  159. export GNUPGHOME
  160. export HOST_FINGERPRINT
  161. # get subcommand
  162. COMMAND="$1"
  163. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  164. shift
  165. case $COMMAND in
  166. 'show-key'|'show'|'s')
  167. check_host_fail
  168. show_key
  169. ;;
  170. 'set-expire'|'extend-key'|'e')
  171. check_host_fail
  172. source "${MHSHAREDIR}/set_expire"
  173. set_expire "$@"
  174. ;;
  175. 'add-hostname'|'add-name'|'n+')
  176. check_host_fail
  177. source "${MHSHAREDIR}/add_hostname"
  178. add_hostname "$@"
  179. ;;
  180. 'revoke-hostname'|'revoke-name'|'n-')
  181. check_host_fail
  182. source "${MHSHAREDIR}/revoke_hostname"
  183. revoke_hostname "$@"
  184. ;;
  185. 'add-revoker'|'o')
  186. check_host_fail
  187. source "${MHSHAREDIR}/add_revoker"
  188. add_revoker "$@"
  189. ;;
  190. 'revoke-key'|'r')
  191. check_host_fail
  192. source "${MHSHAREDIR}/revoke_key"
  193. revoke_key "$@"
  194. ;;
  195. 'publish-key'|'publish'|'p')
  196. check_host_fail
  197. source "${MHSHAREDIR}/publish_key"
  198. publish_key
  199. ;;
  200. 'expert')
  201. SUBCOMMAND="$1"
  202. shift
  203. case "$SUBCOMMAND" in
  204. 'help'|'h'|'?')
  205. cat <<EOF
  206. usage: $PGRM expert <subcommand> [options] [args]
  207. expert subcommands:
  208. import-key (i) [NAME[:PORT]] import existing ssh key to gpg
  209. gen-key (g) [NAME[:PORT]] generate gpg key for the host
  210. --length (-l) BITS key length in bits (2048)
  211. diagnostics (d) monkeysphere host status
  212. EOF
  213. ;;
  214. 'import-key'|'i')
  215. source "${MHSHAREDIR}/import_key"
  216. import_key "$@"
  217. ;;
  218. 'gen-key'|'g')
  219. source "${MHSHAREDIR}/gen_key"
  220. gen_key "$@"
  221. ;;
  222. 'diagnostics'|'d')
  223. source "${MHSHAREDIR}/diagnostics"
  224. diagnostics
  225. ;;
  226. *)
  227. failure "Unknown expert subcommand: '$COMMAND'
  228. Type '$PGRM help' for usage."
  229. ;;
  230. esac
  231. ;;
  232. 'version'|'v')
  233. echo "$VERSION"
  234. ;;
  235. '--help'|'help'|'-h'|'h'|'?')
  236. usage
  237. ;;
  238. *)
  239. failure "Unknown command: '$COMMAND'
  240. Type '$PGRM help' for usage."
  241. ;;
  242. esac
  243. exit "$RETURN"