summaryrefslogtreecommitdiff
path: root/src/share/mh/add_revoker
blob: 5a2032c94ae44de40b3e4e266fa2c83a6547f8cf (plain)
  1. # -*-shell-script-*-
  2. # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
  3. # Monkeysphere host add-revoker subcommand
  4. #
  5. # The monkeysphere scripts are written by:
  6. # Jameson Rollins <jrollins@finestructure.net>
  7. # Jamie McClelland <jm@mayfirst.org>
  8. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  9. #
  10. # They are Copyright 2008-2010, and are all released under the GPL,
  11. # version 3 or later.
  12. # add a revoker to the host key
  13. add_revoker() {
  14. local revokerKeyID
  15. local keyID
  16. local tmpDir
  17. local fingerprint
  18. local addrevokerCommand
  19. # check that key ID or file is specified
  20. if [ -z "$1" ] ; then
  21. failure "You must specify the key ID of a revoker key, or specify a file to read the key from."
  22. fi
  23. revokerKeyID="$1"
  24. shift
  25. keyID=$(check_key_input "$@")
  26. # make a temporary directory for storing keys during import, and set
  27. # the trap to delete it on exit
  28. tmpDir=$(msmktempdir)
  29. trap "rm -rf $tmpDir" EXIT
  30. # if file is specified
  31. if [ -f "$revokerKeyID" -o "$revokerKeyID" = '-' ] ; then
  32. # load the key from stdin
  33. if [ "$revokerKeyID" = '-' ] ; then
  34. # make a temporary file to hold the key from stdin
  35. revokerKeyID="$tmpDir"/importkey
  36. log verbose "reading revoker key from stdin..."
  37. cat > "$revokerKeyID"
  38. # load the key from the file
  39. elif [ -f "$revokerKeyID" ] ; then
  40. log verbose "reading revoker key from file '$revokerKeyID'..."
  41. fi
  42. # check the key is ok as monkeysphere user before loading
  43. log debug "checking keys in file..."
  44. fingerprint=$(su_monkeysphere_user \
  45. "${SYSSHAREDIR}/common list_primary_fingerprints" < "$revokerKeyID")
  46. if [ $(printf "%s" "$fingerprint" | egrep -c '^[A-F0-9]{40}$') -ne 1 ] ; then
  47. failure "There was not exactly one gpg key in the file."
  48. fi
  49. # load the key
  50. gpg_host --import <"$revokerKeyID" \
  51. || failure "could not read revoker key from '$revokerKeyID'"
  52. # else, get the revoker key from the keyserver
  53. else
  54. # fix permissions and ownership on temporary directory which will
  55. # be used by monkeysphere user for storing the downloaded key
  56. chmod 0700 "$tmpDir"
  57. chown "$MONKEYSPHERE_USER":"$MONKEYSPHERE_GROUP" "$tmpDir"
  58. # download the key from the keyserver as the monkeysphere user
  59. log verbose "searching keyserver $KEYSERVER for revoker keyID $revokerKeyID..."
  60. su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --quiet --keyserver $KEYSERVER --recv-key 0x${revokerKeyID}!" \
  61. || failure "Could not receive a key with this ID from keyserver '$KEYSERVER'."
  62. # get the full fingerprint of new revoker key
  63. log debug "getting fingerprint of revoker key..."
  64. fingerprint=$(su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --list-key --with-colons --with-fingerprint ${revokerKeyID}" \
  65. | grep '^fpr:' | cut -d: -f10)
  66. # test that there is only a single fingerprint
  67. if (( $(echo "$fingerprint" | wc -l) != 1 )) ; then
  68. cat <<EOF
  69. More than one fingerprint found:
  70. $fingerprint
  71. Please use a more specific key ID.
  72. EOF
  73. failure
  74. fi
  75. log info "revoker key found:"
  76. su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --fingerprint 0x${fingerprint}!"
  77. if [ "$PROMPT" = "true" ] ; then
  78. printf "Are you sure you want to add the above key as a revoker\nof the key '$keyID'? (Y/n) " >&2
  79. read OK; OK=${OK:-Y}
  80. if [ "${OK/y/Y}" != 'Y' ] ; then
  81. failure "revoker not added."
  82. fi
  83. else
  84. log debug "adding revoker without prompting."
  85. fi
  86. # export the new key to the host keyring
  87. log debug "loading revoker key into host keyring..."
  88. su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --quiet --export 0x${fingerprint}!" \
  89. | gpg_host --import
  90. fi
  91. # edit-key script to add revoker
  92. addrevokerCommand="addrevoker
  93. $fingerprint
  94. y
  95. save
  96. "
  97. # end script
  98. # core ltsigns the newly imported revoker key
  99. log debug "executing add revoker script..."
  100. if echo "$addrevokerCommand" | gpg_host_edit "0x${keyID}!" ; then
  101. update_pgp_pub_file
  102. log info "Revoker added."
  103. else
  104. failure "Problem adding revoker."
  105. fi
  106. # remove the temporary directory
  107. trap - EXIT
  108. rm -rf "$tmpDir"
  109. }