summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: 903e3335c8f34a6735defbda1b5cf05f9b28c132 (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-2010, 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}/defaultenv"
  20. . "${SYSSHAREDIR}/common"
  21. SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
  22. export SYSDATADIR
  23. # sharedir for host functions
  24. MHSHAREDIR="${SYSSHAREDIR}/mh"
  25. # datadir for host functions
  26. MHDATADIR="${SYSDATADIR}/host"
  27. # host pub key files
  28. HOST_KEY_FILE="${SYSDATADIR}/host_keys.pub.gpg"
  29. # host pub key fingerprints file
  30. HOST_KEY_FPR_FILE="${SYSDATADIR}/host_keys.fprs"
  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. ########################################################################
  36. # FUNCTIONS
  37. ########################################################################
  38. usage() {
  39. cat <<EOF >&2
  40. usage: $PGRM <subcommand> [options] [args]
  41. Monkeysphere host admin tool.
  42. subcommands:
  43. import-key (i) FILE SERVICENAME import PEM-encoded key from file
  44. show-keys (s) [KEYID ...] output host key information
  45. publish-keys (p) [KEYID ...] publish key(s) to keyserver
  46. set-expire (e) EXPIRE [KEYID] set key expiration
  47. add-servicename (n+) SERVICENAME [KEYID]
  48. add a service name to key
  49. revoke-servicename (n-) SERVICENAME [KEYID]
  50. revoke a service name from key
  51. add-revoker (r+) REVOKER_KEYID|FILE [KEYID]
  52. add a revoker to key
  53. revoke-key [KEYID] generate and/or publish revocation
  54. certificate for key
  55. version (v) show version number
  56. help (h,?) this help
  57. See ${PGRM}(8) for more info.
  58. EOF
  59. }
  60. # function to interact with the gpg keyring
  61. gpg_host() {
  62. GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
  63. }
  64. # list the info about the a key, in colon format, to stdout
  65. gpg_host_list_keys() {
  66. gpg_host --list-keys --with-colons --fixed-list-mode \
  67. --with-fingerprint --with-fingerprint \
  68. "$1"
  69. }
  70. # edit key scripts, takes scripts on stdin, and keyID as first input
  71. gpg_host_edit() {
  72. gpg_host --command-fd 0 --edit-key "$@"
  73. }
  74. # export the monkeysphere gpg pub key file
  75. update_gpg_pub_file() {
  76. log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
  77. gpg_host --export --armor --export-options export-minimal > "$HOST_KEY_FILE"
  78. log debug "updating fingerprint file '$HOST_KEY_FPR_FILE'..."
  79. gpg_host --list-secret-key --with-colons --with-fingerprint \
  80. | awk -F: '/^fpr:/{ print $10 }' > "$HOST_KEY_FPR_FILE"
  81. }
  82. host_fingerprints() {
  83. local fprs=($(cat "$HOST_KEY_FPR_FILE"))
  84. log debug "host key fingerprints:"
  85. printf '%s\n' "${fprs[@]}" | log debug
  86. printf '%s\n' "${fprs[@]}"
  87. }
  88. # check that the service name is well formed
  89. check_service_name() {
  90. local name="$1"
  91. log error "FIX ME: check service name"
  92. }
  93. # fail if host key not present
  94. check_no_keys() {
  95. [ -s "$HOST_KEY_FILE" ] || [ -s "$HOST_KEY_FPR_FILE" ] \
  96. || failure "You don't appear to have a Monkeysphere host key on this server.
  97. Please run 'monkeysphere-host import-key' import a key."
  98. }
  99. # key input to functions, outputs full fingerprint of specified key if
  100. # found
  101. check_key_input() {
  102. local keyID="$1"
  103. # array of fingerprints
  104. local fprs=($(host_fingerprints))
  105. case ${#fprs[@]} in
  106. 0)
  107. failure "You don't appear to have any Monkeysphere host keys.
  108. Please run 'monkeysphere-host import-key' to import a key."
  109. ;;
  110. 1)
  111. :
  112. ;;
  113. *)
  114. if [ -z "$keyID" ] ; then
  115. failure "Keyring contains multiple keys. Please specify one to act on (see 'monkeysphere-host show-key')."
  116. fi
  117. ;;
  118. esac
  119. printf '%s\n' "${fprs[@]}" | grep "${keyID}$" \
  120. || failure "Key '$keyID' not found."
  121. }
  122. # return 0 if user ID was found.
  123. # return 1 if user ID not found.
  124. check_key_userid() {
  125. local keyID="$1"
  126. local userID="$2"
  127. local tmpuidMatch
  128. # match to only "unknown" user IDs (host has no need for ultimate trust)
  129. tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
  130. # See whether the requsted user ID is present
  131. gpg_host_list_keys "$keyID" | cut -f1,2,10 -d: | \
  132. grep -q -x -F "$tmpuidMatch" 2>/dev/null
  133. }
  134. # run command looped over keys
  135. multi_key() {
  136. local cmd="$1"
  137. shift
  138. local keys=$@
  139. local i=0
  140. local fprs=($(host_fingerprints))
  141. local key
  142. check_no_keys
  143. if [[ -z "$1" || "$1" == '--all' ]] ; then
  144. keys="${fprs[@]}"
  145. fi
  146. for key in $keys ; do
  147. if (( i++ > 0 )) ; then
  148. echo "##############################"
  149. fi
  150. eval "$cmd" "$key"
  151. done
  152. }
  153. # show info about the a key
  154. show_key() {
  155. local id="$1"
  156. local GNUPGHOME
  157. local TMPSSH
  158. local fingerprint
  159. local revokers
  160. # tmp gpghome dir
  161. export GNUPGHOME=$(msmktempdir)
  162. # trap to remove tmp dir if break
  163. trap "rm -rf $GNUPGHOME" EXIT
  164. # import the host key into the tmp dir
  165. gpg --quiet --import <"$HOST_KEY_FILE"
  166. # create the ssh key
  167. TMPSSH="$GNUPGHOME"/ssh_host_key_rsa_pub
  168. if ! gpg --export "$id" 2>/dev/null \
  169. | openpgp2ssh 2>/dev/null >"$TMPSSH" ; then
  170. failure "Key '$id' not found."
  171. fi
  172. # get the gpg fingerprint
  173. fingerprint=$(gpg --quiet --list-keys \
  174. --with-colons --with-fingerprint "$id" \
  175. | grep '^fpr:' | cut -d: -f10 )
  176. # list the host key info
  177. # FIXME: make no-show-keyring work so we don't have to do the grep'ing
  178. # FIXME: can we show uid validity somehow?
  179. gpg --list-keys --list-options show-unusable-uids "$id" 2>/dev/null \
  180. | grep -v "^${GNUPGHOME}/pubring.gpg$" \
  181. | egrep -v '^-+$'
  182. # list revokers, if there are any
  183. revokers=$(gpg --list-keys --with-colons --fixed-list-mode "$id" \
  184. | awk -F: '/^rvk:/{ print $10 }' )
  185. if [ "$revokers" ] ; then
  186. echo "The following keys are allowed to revoke this host key:"
  187. for key in $revokers ; do
  188. echo "revoker: $key"
  189. done
  190. echo
  191. fi
  192. # list the pgp fingerprint
  193. echo "OpenPGP fingerprint: $fingerprint"
  194. # list the ssh fingerprint
  195. echo -n "ssh fingerprint: "
  196. ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
  197. # remove the tmp file
  198. trap - EXIT
  199. rm -rf "$GNUPGHOME"
  200. }
  201. ########################################################################
  202. # MAIN
  203. ########################################################################
  204. # load configuration file
  205. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
  206. && . "$MONKEYSPHERE_HOST_CONFIG"
  207. # set empty config variable with ones from the environment, or with
  208. # defaults
  209. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  210. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  211. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  212. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
  213. MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
  214. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  215. # other variables
  216. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  217. LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
  218. # export variables needed in su invocation
  219. export DATE
  220. export LOG_LEVEL
  221. export KEYSERVER
  222. export CHECK_KEYSERVER
  223. export MONKEYSPHERE_USER
  224. export MONKEYSPHERE_GROUP
  225. export PROMPT
  226. export GNUPGHOME_HOST
  227. export GNUPGHOME
  228. export HOST_FINGERPRINT
  229. export LOG_PREFIX
  230. if [ "$#" -eq 0 ] ; then
  231. usage
  232. failure "Please supply a subcommand."
  233. fi
  234. # get subcommand
  235. COMMAND="$1"
  236. shift
  237. case $COMMAND in
  238. 'import-key'|'i')
  239. source "${MHSHAREDIR}/import_key"
  240. import_key "$@"
  241. ;;
  242. 'show-keys'|'show-key'|'show'|'s')
  243. multi_key show_key "$@"
  244. ;;
  245. 'set-expire'|'extend-key'|'e')
  246. source "${MHSHAREDIR}/set_expire"
  247. set_expire "$@"
  248. ;;
  249. 'add-servicename'|'add-hostname'|'add-name'|'n+')
  250. source "${MHSHAREDIR}/add_name"
  251. add_name "$@"
  252. ;;
  253. 'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
  254. source "${MHSHAREDIR}/revoke_name"
  255. revoke_name "$@"
  256. ;;
  257. 'add-revoker'|'r+')
  258. source "${MHSHAREDIR}/add_revoker"
  259. add_revoker "$@"
  260. ;;
  261. 'revoke-key')
  262. source "${MHSHAREDIR}/revoke_key"
  263. revoke_key "$@"
  264. ;;
  265. 'publish-keys'|'publish-key'|'publish'|'p')
  266. source "${MHSHAREDIR}/publish_key"
  267. multi_key publish_key "$@"
  268. ;;
  269. 'diagnostics'|'d')
  270. source "${MHSHAREDIR}/diagnostics"
  271. diagnostics
  272. ;;
  273. 'update-gpg-pub-file')
  274. update_gpg_pub_file
  275. ;;
  276. 'version'|'v')
  277. version
  278. ;;
  279. '--help'|'help'|'-h'|'h'|'?')
  280. usage
  281. ;;
  282. *)
  283. failure "Unknown command: '$COMMAND'
  284. Try '$PGRM help' for usage."
  285. ;;
  286. esac