summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: 8452e6c082dfddba52ace4638677da8c4e0b869d (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 "Your host keyring contains multiple keys.
  116. Please specify one to act on (see 'monkeysphere-host show-key')."
  117. fi
  118. ;;
  119. esac
  120. printf '%s\n' "${fprs[@]}" | grep "${keyID}$" \
  121. || failure "Host key '$keyID' not found."
  122. }
  123. # return 0 if user ID was found.
  124. # return 1 if user ID not found.
  125. check_key_userid() {
  126. local keyID="$1"
  127. local userID="$2"
  128. local tmpuidMatch
  129. # match to only "unknown" user IDs (host has no need for ultimate trust)
  130. tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
  131. # See whether the requsted user ID is present
  132. gpg_host_list_keys "$keyID" | cut -f1,2,10 -d: | \
  133. grep -q -x -F "$tmpuidMatch" 2>/dev/null
  134. }
  135. # run command looped over keys
  136. multi_key() {
  137. local cmd="$1"
  138. shift
  139. local keys=$@
  140. local i=0
  141. local fprs=($(host_fingerprints))
  142. local key
  143. check_no_keys
  144. if [[ -z "$1" || "$1" == '--all' ]] ; then
  145. keys="${fprs[@]}"
  146. fi
  147. for key in $keys ; do
  148. if (( i++ > 0 )) ; then
  149. echo "##############################"
  150. fi
  151. eval "$cmd" "$key"
  152. done
  153. }
  154. # show info about the a key
  155. show_key() {
  156. local id="$1"
  157. local GNUPGHOME
  158. local TMPSSH
  159. local fingerprint
  160. local revokers
  161. # tmp gpghome dir
  162. export GNUPGHOME=$(msmktempdir)
  163. # trap to remove tmp dir if break
  164. trap "rm -rf $GNUPGHOME" EXIT
  165. # import the host key into the tmp dir
  166. gpg --quiet --import <"$HOST_KEY_FILE"
  167. # create the ssh key
  168. TMPSSH="$GNUPGHOME"/ssh_host_key_rsa_pub
  169. if ! gpg --export "$id" 2>/dev/null \
  170. | openpgp2ssh 2>/dev/null >"$TMPSSH" ; then
  171. failure "Key '$id' not found."
  172. fi
  173. # get the gpg fingerprint
  174. fingerprint=$(gpg --quiet --list-keys \
  175. --with-colons --with-fingerprint "$id" \
  176. | grep '^fpr:' | cut -d: -f10 )
  177. # list the host key info
  178. # FIXME: make no-show-keyring work so we don't have to do the grep'ing
  179. # FIXME: can we show uid validity somehow?
  180. gpg --list-keys --list-options show-unusable-uids "$id" 2>/dev/null \
  181. | grep -v "^${GNUPGHOME}/pubring.gpg$" \
  182. | egrep -v '^-+$'
  183. # list revokers, if there are any
  184. revokers=$(gpg --list-keys --with-colons --fixed-list-mode "$id" \
  185. | awk -F: '/^rvk:/{ print $10 }' )
  186. if [ "$revokers" ] ; then
  187. echo "The following keys are allowed to revoke this host key:"
  188. for key in $revokers ; do
  189. echo "revoker: $key"
  190. done
  191. echo
  192. fi
  193. # list the pgp fingerprint
  194. echo "OpenPGP fingerprint: $fingerprint"
  195. # list the ssh fingerprint
  196. echo -n "ssh fingerprint: "
  197. ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
  198. # remove the tmp file
  199. trap - EXIT
  200. rm -rf "$GNUPGHOME"
  201. }
  202. ########################################################################
  203. # MAIN
  204. ########################################################################
  205. # load configuration file
  206. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
  207. && . "$MONKEYSPHERE_HOST_CONFIG"
  208. # set empty config variable with ones from the environment, or with
  209. # defaults
  210. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  211. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  212. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  213. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
  214. MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
  215. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  216. # other variables
  217. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  218. LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
  219. # export variables needed in su invocation
  220. export DATE
  221. export LOG_LEVEL
  222. export KEYSERVER
  223. export CHECK_KEYSERVER
  224. export MONKEYSPHERE_USER
  225. export MONKEYSPHERE_GROUP
  226. export PROMPT
  227. export GNUPGHOME_HOST
  228. export GNUPGHOME
  229. export HOST_FINGERPRINT
  230. export LOG_PREFIX
  231. if [ "$#" -eq 0 ] ; then
  232. usage
  233. failure "Please supply a subcommand."
  234. fi
  235. # get subcommand
  236. COMMAND="$1"
  237. shift
  238. case $COMMAND in
  239. 'import-key'|'i')
  240. source "${MHSHAREDIR}/import_key"
  241. import_key "$@"
  242. ;;
  243. 'show-keys'|'show-key'|'show'|'s')
  244. multi_key show_key "$@"
  245. ;;
  246. 'set-expire'|'extend-key'|'e')
  247. source "${MHSHAREDIR}/set_expire"
  248. set_expire "$@"
  249. ;;
  250. 'add-servicename'|'add-hostname'|'add-name'|'n+')
  251. source "${MHSHAREDIR}/add_name"
  252. add_name "$@"
  253. ;;
  254. 'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
  255. source "${MHSHAREDIR}/revoke_name"
  256. revoke_name "$@"
  257. ;;
  258. 'add-revoker'|'r+')
  259. source "${MHSHAREDIR}/add_revoker"
  260. add_revoker "$@"
  261. ;;
  262. 'revoke-key')
  263. source "${MHSHAREDIR}/revoke_key"
  264. revoke_key "$@"
  265. ;;
  266. 'publish-keys'|'publish-key'|'publish'|'p')
  267. source "${MHSHAREDIR}/publish_key"
  268. multi_key publish_key "$@"
  269. ;;
  270. 'diagnostics'|'d')
  271. source "${MHSHAREDIR}/diagnostics"
  272. diagnostics
  273. ;;
  274. 'update-gpg-pub-file')
  275. update_gpg_pub_file
  276. ;;
  277. 'version'|'v')
  278. version
  279. ;;
  280. '--help'|'help'|'-h'|'h'|'?')
  281. usage
  282. ;;
  283. *)
  284. failure "Unknown command: '$COMMAND'
  285. Try '$PGRM help' for usage."
  286. ;;
  287. esac