summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: 3c2e3eead29106f3c15d43824ef13e47b98c9c6e (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 run command as monkeysphere user
  54. su_monkeysphere_user() {
  55. # if the current user is the monkeysphere user, then just eval
  56. # command
  57. if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
  58. eval "$@"
  59. # otherwise su command as monkeysphere user
  60. else
  61. su "$MONKEYSPHERE_USER" -c "$@"
  62. fi
  63. }
  64. # function to interact with the gpg keyring
  65. gpg_host() {
  66. GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
  67. }
  68. # command to list the info about the host key, in colon format
  69. gpg_host_list() {
  70. gpg_host --list-keys --with-colons --fixed-list-mode \
  71. --with-fingerprint --with-fingerprint \
  72. "0x${HOST_FINGERPRINT}!"
  73. }
  74. # command for edit key scripts, takes scripts on stdin
  75. gpg_host_edit() {
  76. gpg_host --quiet --command-fd 0 --edit-key \
  77. "0x${HOST_FINGERPRINT}!" "$@"
  78. }
  79. # export the host key to stdout
  80. gpg_host_export() {
  81. gpg_host --export --armor --export-options export-minimal \
  82. "0x${HOST_FINGERPRINT}!"
  83. }
  84. # export the host key to the monkeysphere host file key
  85. gpg_host_export_to_ssh_file() {
  86. log debug "exporting openpgp public key..."
  87. gpg_host_export > "$HOST_KEY_PUB_GPG"
  88. log info "SSH host public key in OpenPGP form: $HOST_KEY_PUB_GPG"
  89. }
  90. # output just key fingerprint
  91. fingerprint_host_key() {
  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 "$HOST_KEY_PUB" ] ; then
  131. fingerprintSSH=$(ssh-keygen -l -f "$HOST_KEY_PUB" | \
  132. awk '{ print $1, $2, $4 }')
  133. echo "ssh fingerprint: $fingerprintSSH"
  134. else
  135. log error "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. # host pub key files
  161. HOST_KEY_PUB="${SYSDATADIR}/ssh_host_rsa_key.pub"
  162. HOST_KEY_PUB_GPG="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  163. # export variables needed in su invocation
  164. export DATE
  165. export MODE
  166. export LOG_LEVEL
  167. export MONKEYSPHERE_USER
  168. export KEYSERVER
  169. export GNUPGHOME_HOST
  170. export GNUPGHOME
  171. export HOST_FINGERPRINT
  172. # get subcommand
  173. COMMAND="$1"
  174. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  175. shift
  176. case $COMMAND in
  177. 'show-key'|'show'|'s')
  178. check_host_fail
  179. show_key
  180. ;;
  181. 'set-expire'|'extend-key'|'e')
  182. check_host_fail
  183. source "${MHSHAREDIR}/set_expire"
  184. set_expire "$@"
  185. ;;
  186. 'add-hostname'|'add-name'|'n+')
  187. check_host_fail
  188. source "${MHSHAREDIR}/add_hostname"
  189. add_hostname "$@"
  190. ;;
  191. 'revoke-hostname'|'revoke-name'|'n-')
  192. check_host_fail
  193. source "${MHSHAREDIR}/revoke_hostname"
  194. revoke_hostname "$@"
  195. ;;
  196. 'add-revoker'|'o')
  197. check_host_fail
  198. source "${MHSHAREDIR}/add_revoker"
  199. add_revoker "$@"
  200. ;;
  201. 'revoke-key'|'r')
  202. check_host_fail
  203. source "${MHSHAREDIR}/revoke_key"
  204. revoke_key "$@"
  205. ;;
  206. 'publish-key'|'publish'|'p')
  207. check_host_fail
  208. source "${MHSHAREDIR}/publish_key"
  209. publish_key
  210. ;;
  211. 'expert')
  212. SUBCOMMAND="$1"
  213. shift
  214. case "$SUBCOMMAND" in
  215. 'help'|'h'|'?')
  216. cat <<EOF
  217. usage: $PGRM expert <subcommand> [options] [args]
  218. expert subcommands:
  219. import-key (i) [NAME[:PORT]] import existing ssh key to gpg
  220. gen-key (g) [NAME[:PORT]] generate gpg key for the host
  221. --length (-l) BITS key length in bits (2048)
  222. diagnostics (d) monkeysphere host status
  223. EOF
  224. ;;
  225. 'import-key'|'i')
  226. source "${MHSHAREDIR}/import_key"
  227. import_key "$@"
  228. ;;
  229. 'gen-key'|'g')
  230. source "${MHSHAREDIR}/gen_key"
  231. gen_key "$@"
  232. ;;
  233. 'diagnostics'|'d')
  234. source "${MHSHAREDIR}/diagnostics"
  235. diagnostics
  236. ;;
  237. *)
  238. failure "Unknown expert subcommand: '$COMMAND'
  239. Type '$PGRM help' for usage."
  240. ;;
  241. esac
  242. ;;
  243. 'version'|'v')
  244. echo "$VERSION"
  245. ;;
  246. '--help'|'help'|'-h'|'h'|'?')
  247. usage
  248. ;;
  249. *)
  250. failure "Unknown command: '$COMMAND'
  251. Type '$PGRM help' for usage."
  252. ;;
  253. esac
  254. exit "$RETURN"