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