summaryrefslogtreecommitdiff
path: root/src/share/mh/add_revoker
blob: 638f052e078c70b68f4dd78ae7082e98cf67ed91 (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. # create a temporary directory for storing the downloaded key
  52. local GNUPGHOME="$tmpDir"
  53. chmod 0700 "$GNUPGHOME"
  54. chown "$MONKEYSPHERE_USER":"$MONKEYSPHERE_USER" "$GNUPGHOME"
  55. export GNUPGHOME
  56. # download the key from the keyserver as the monkeysphere user
  57. log verbose "searching keyserver $KEYSERVER for keyID $keyID..."
  58. su_monkeysphere_user "gpg --quiet --keyserver $KEYSERVER --recv-key 0x${keyID}!" \
  59. || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
  60. # get the full fingerprint of new revoker key
  61. log debug "getting fingerprint of revoker key..."
  62. fingerprint=$(su_monkeysphere_user "gpg --list-key --with-colons --with-fingerprint 0x${keyID}!" \
  63. | grep '^fpr:' | grep "$keyID" | cut -d: -f10)
  64. log info "key found:"
  65. su_monkeysphere_user "gpg --fingerprint 0x${fingerprint}!"
  66. if [ "$PROMPT" = "true" ] ; then
  67. echo "Are you sure you want to add the above key as a"
  68. read -p "revoker of the host key? (Y/n) " OK; OK=${OK:-Y}
  69. if [ "${OK/y/Y}" != 'Y' ] ; then
  70. failure "revoker not added."
  71. fi
  72. else
  73. log debug "adding revoker without prompting."
  74. fi
  75. # export the new key to the host keyring
  76. log debug "loading key into host keyring..."
  77. su_monkeysphere_user "gpg --quiet --export 0x${fingerprint}!" \
  78. | gpg_host --import
  79. fi
  80. # edit-key script to add revoker
  81. addrevokerCommand=$(cat <<EOF
  82. addrevoker
  83. $fingerprint
  84. B65D085B94117B813160B659ED34CEABE27BAABC
  85. y
  86. save
  87. EOF
  88. )
  89. # core ltsigns the newly imported revoker key
  90. log debug "executing add revoker script..."
  91. if echo "$addrevokerCommand" | gpg_host_edit ; then
  92. update_gpg_pub_file
  93. log info "Revoker added."
  94. else
  95. failure "Problem adding revoker."
  96. fi
  97. # remove the temporary directory
  98. trap - EXIT
  99. rm -rf "$tmpDir"
  100. }