summaryrefslogtreecommitdiff
path: root/src/share/mh/add_revoker
blob: 18ad2b7a8f8a76b117fa2ff208063f8be65cdd32 (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, and are all released under the GPL, version 3
  11. # or later.
  12. # add a revoker to the host key
  13. add_revoker() {
  14. local keyID
  15. local tmpDir
  16. local fingerprint
  17. local addrevokerCommand
  18. keyID="$1"
  19. # check that key ID or file is specified
  20. if [ -z "$keyID" ] ; 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. # make a temporary directory for storing keys during import, and set
  24. # the trap to delete it on exit
  25. tmpDir=$(msmktempdir)
  26. trap "rm -rf $tmpDir" EXIT
  27. # if file is specified
  28. if [ -f "$keyID" -o "$keyID" = '-' ] ; then
  29. # load the key from stdin
  30. if [ "$keyID" = '-' ] ; then
  31. # make a temporary file to hold the key from stdin
  32. keyID="$tmpDir"/importkey
  33. log verbose "reading key from stdin..."
  34. cat > "$keyID"
  35. # load the key from the file
  36. elif [ -f "$keyID" ] ; then
  37. log verbose "reading key from file '$keyID'..."
  38. fi
  39. # check the key is ok as monkeysphere user before loading
  40. log debug "checking keys in file..."
  41. fingerprint=$(su_monkeysphere_user \
  42. ". ${SYSSHAREDIR}/common; list_primary_fingerprints" < "$keyID")
  43. if [ $(printf "%s" "$fingerprint" | egrep -c '^[A-F0-9]{40}$') -ne 1 ] ; then
  44. failure "There was not exactly one gpg key in the file."
  45. fi
  46. # load the key
  47. gpg_host --import <"$keyID" \
  48. || failure "could not read key from '$keyID'"
  49. # else, get the key from the keyserver
  50. else
  51. # fix permissions and ownership on temporary directory which will
  52. # be used by monkeysphere user for storing the downloaded key
  53. chmod 0700 "$tmpDir"
  54. chown "$MONKEYSPHERE_USER":"$MONKEYSPHERE_USER" "$tmpDir"
  55. # download the key from the keyserver as the monkeysphere user
  56. log verbose "searching keyserver $KEYSERVER for keyID $keyID..."
  57. su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --quiet --keyserver $KEYSERVER --recv-key 0x${keyID}!" \
  58. || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
  59. # get the full fingerprint of new revoker key
  60. log debug "getting fingerprint of revoker key..."
  61. fingerprint=$(su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --list-key --with-colons --with-fingerprint 0x${keyID}!" \
  62. | grep '^fpr:' | grep "$keyID" | cut -d: -f10)
  63. log info "key found:"
  64. su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --fingerprint 0x${fingerprint}!"
  65. if [ "$PROMPT" = "true" ] ; then
  66. echo "Are you sure you want to add the above key as a revoker"
  67. read -p "of the host key? (Y/n) " OK; OK=${OK:-Y}
  68. if [ "${OK/y/Y}" != 'Y' ] ; then
  69. failure "revoker not added."
  70. fi
  71. else
  72. log debug "adding revoker without prompting."
  73. fi
  74. # export the new key to the host keyring
  75. log debug "loading key into host keyring..."
  76. su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --quiet --export 0x${fingerprint}!" \
  77. | gpg_host --import
  78. fi
  79. # edit-key script to add revoker
  80. addrevokerCommand=$(cat <<EOF
  81. addrevoker
  82. $fingerprint
  83. y
  84. save
  85. EOF
  86. )
  87. # core ltsigns the newly imported revoker key
  88. log debug "executing add revoker script..."
  89. if echo "$addrevokerCommand" | gpg_host_edit ; then
  90. update_gpg_pub_file
  91. log info "Revoker added."
  92. else
  93. failure "Problem adding revoker."
  94. fi
  95. # remove the temporary directory
  96. trap - EXIT
  97. rm -rf "$tmpDir"
  98. }