summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: 64023e056c7ad6de9e6a1c5bf978d503010ecfbf (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. # temp directory for temp gnupghome directories for add_revoker
  27. MHTMPDIR="${MHDATADIR}/tmp"
  28. export MHTMPDIR
  29. # host pub key files
  30. HOST_KEY_FILE="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  31. # UTC date in ISO 8601 format if needed
  32. DATE=$(date -u '+%FT%T')
  33. # unset some environment variables that could screw things up
  34. unset GREP_OPTIONS
  35. # default return code
  36. RETURN=0
  37. ########################################################################
  38. # FUNCTIONS
  39. ########################################################################
  40. usage() {
  41. cat <<EOF >&2
  42. usage: $PGRM <subcommand> [options] [args]
  43. Monkeysphere host admin tool.
  44. subcommands:
  45. show-key (s) output all host key information
  46. set-expire (e) EXPIRE set host key expiration
  47. add-hostname (n+) NAME[:PORT] add hostname user ID to host key
  48. revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
  49. add-revoker (o) FINGERPRINT add a revoker to the host key
  50. revoke-key (r) revoke host key
  51. publish-key (p) publish host key to keyserver
  52. expert <expert-subcommand> run expert command
  53. expert help expert command help
  54. version (v) show version number
  55. help (h,?) this help
  56. EOF
  57. }
  58. # function to interact with the gpg keyring
  59. gpg_host() {
  60. GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
  61. }
  62. # command to list the info about the host key, in colon format
  63. gpg_host_list() {
  64. gpg_host --list-keys --with-colons --fixed-list-mode \
  65. --with-fingerprint --with-fingerprint \
  66. "0x${HOST_FINGERPRINT}!"
  67. }
  68. # command for edit key scripts, takes scripts on stdin
  69. gpg_host_edit() {
  70. gpg_host --quiet --command-fd 0 --edit-key \
  71. "0x${HOST_FINGERPRINT}!" "$@"
  72. }
  73. # export the host public key to the monkeysphere gpg pub key file
  74. create_gpg_pub_file() {
  75. log debug "creating openpgp public key file..."
  76. gpg_host --export --armor --export-options export-minimal \
  77. "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
  78. log info "GPG host public key file: $HOST_KEY_FILE"
  79. }
  80. # load the host fingerprint into the fingerprint variable, using the
  81. # export gpg pub key file
  82. # FIXME: this seems much less than ideal, with all this temp keyring
  83. # stuff. is there a way we can do this without having to create temp
  84. # files?
  85. load_fingerprint() {
  86. if [ -f "$HOST_KEY_FILE" ] ; then
  87. HOST_FINGERPRINT=$( \
  88. (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
  89. && gpg --quiet --import \
  90. && gpg --quiet --list-keys --with-colons --with-fingerprint \
  91. && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
  92. | grep '^fpr:' | cut -d: -f10 )
  93. else
  94. HOST_FINGERPRINT=
  95. fi
  96. }
  97. # load the host fingerprint into the fingerprint variable, using the
  98. # gpg host secret key
  99. load_fingerprint_secret() {
  100. HOST_FINGERPRINT=$( \
  101. gpg_host --quiet --list-secret-key \
  102. --with-colons --with-fingerprint \
  103. | grep '^fpr:' | cut -d: -f10 )
  104. }
  105. # fail if host key present
  106. check_host_key() {
  107. [ -z "$HOST_FINGERPRINT" ] \
  108. || failure "An OpenPGP host key already exists."
  109. }
  110. # fail if host key not present
  111. check_host_no_key() {
  112. [ "$HOST_FINGERPRINT" ] \
  113. || failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-host expert import-key' first."
  114. }
  115. # output the index of a user ID on the host key
  116. # return 1 if user ID not found
  117. find_host_userid() {
  118. local userID="$1"
  119. local tmpuidMatch
  120. local line
  121. # match to only ultimately trusted user IDs
  122. tmpuidMatch="u:$(echo $userID | gpg_escape)"
  123. # find the index of the requsted user ID
  124. # NOTE: this is based on circumstantial evidence that the order of
  125. # this output is the appropriate index
  126. line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
  127. grep -n -x -F "$tmpuidMatch" 2>/dev/null)
  128. if [ "$line" ] ; then
  129. echo ${line%%:*}
  130. return 0
  131. else
  132. return 1
  133. fi
  134. }
  135. # show info about the host key
  136. show_key() {
  137. gpg_host --fingerprint --list-key --list-options show-unusable-uids \
  138. "0x${HOST_FINGERPRINT}!" 2>/dev/null || true
  139. # FIXME: make sure expiration date is shown
  140. echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
  141. echo -n "ssh fingerprint: "
  142. ssh-keygen -l -f /dev/stdin \
  143. <<<$( gpg_host --export FEE16FA3 2>/dev/null \
  144. | openpgp2ssh 8445B5203A8443B4B04F637DD4DE66B2FEE16FA3 2>/dev/null) \
  145. | awk '{ print $1, $2, $4 }'
  146. # FIXME: other relevant key parameters?
  147. }
  148. ########################################################################
  149. # MAIN
  150. ########################################################################
  151. # unset variables that should be defined only in config file
  152. unset KEYSERVER
  153. unset MONKEYSPHERE_USER
  154. # load configuration file
  155. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
  156. # set empty config variable with ones from the environment, or with
  157. # defaults
  158. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
  159. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
  160. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
  161. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  162. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  163. # other variables
  164. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  165. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  166. # export variables needed in su invocation
  167. export DATE
  168. export MODE
  169. export LOG_LEVEL
  170. export MONKEYSPHERE_USER
  171. export KEYSERVER
  172. export GNUPGHOME_HOST
  173. export GNUPGHOME
  174. export HOST_FINGERPRINT=
  175. # get subcommand
  176. COMMAND="$1"
  177. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  178. shift
  179. case $COMMAND in
  180. 'show-key'|'show'|'s')
  181. load_fingerprint
  182. check_host_no_key
  183. show_key
  184. ;;
  185. 'set-expire'|'extend-key'|'e')
  186. load_fingerprint
  187. check_host_no_key
  188. source "${MHSHAREDIR}/set_expire"
  189. set_expire "$@"
  190. ;;
  191. 'add-hostname'|'add-name'|'n+')
  192. load_fingerprint
  193. check_host_no_key
  194. source "${MHSHAREDIR}/add_hostname"
  195. add_hostname "$@"
  196. ;;
  197. 'revoke-hostname'|'revoke-name'|'n-')
  198. load_fingerprint
  199. check_host_no_key
  200. source "${MHSHAREDIR}/revoke_hostname"
  201. revoke_hostname "$@"
  202. ;;
  203. 'add-revoker'|'o')
  204. load_fingerprint
  205. check_host_no_key
  206. source "${MHSHAREDIR}/add_revoker"
  207. add_revoker "$@"
  208. ;;
  209. 'revoke-key'|'r')
  210. load_fingerprint
  211. check_host_no_key
  212. source "${MHSHAREDIR}/revoke_key"
  213. revoke_key "$@"
  214. ;;
  215. 'publish-key'|'publish'|'p')
  216. load_fingerprint
  217. check_host_no_key
  218. source "${MHSHAREDIR}/publish_key"
  219. publish_key
  220. ;;
  221. 'expert')
  222. SUBCOMMAND="$1"
  223. shift
  224. case "$SUBCOMMAND" in
  225. 'help'|'h'|'?')
  226. cat <<EOF
  227. usage: $PGRM expert <subcommand> [options] [args]
  228. expert subcommands:
  229. import-key (i) [NAME[:PORT]] import existing ssh key to gpg
  230. gen-key (g) [NAME[:PORT]] generate gpg key for the host
  231. --length (-l) BITS key length in bits (2048)
  232. diagnostics (d) monkeysphere host status
  233. EOF
  234. ;;
  235. 'import-key'|'i')
  236. load_fingerprint
  237. check_host_key
  238. source "${MHSHAREDIR}/import_key"
  239. import_key "$@"
  240. ;;
  241. 'gen-key'|'g')
  242. load_fingerprint
  243. check_host_key
  244. source "${MHSHAREDIR}/gen_key"
  245. gen_key "$@"
  246. ;;
  247. 'diagnostics'|'d')
  248. source "${MHSHAREDIR}/diagnostics"
  249. diagnostics
  250. ;;
  251. *)
  252. failure "Unknown expert subcommand: '$COMMAND'
  253. Type '$PGRM help' for usage."
  254. ;;
  255. esac
  256. ;;
  257. 'version'|'v')
  258. echo "$VERSION"
  259. ;;
  260. '--help'|'help'|'-h'|'h'|'?')
  261. usage
  262. ;;
  263. *)
  264. failure "Unknown command: '$COMMAND'
  265. Type '$PGRM help' for usage."
  266. ;;
  267. esac
  268. exit "$RETURN"