summaryrefslogtreecommitdiff
path: root/src/share/mh/add_revoker
blob: 89e6fcf03210bc6d83da36b883b01a2d7b947f1e (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_GROUP" "$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:' | cut -d: -f10)
  63. # test that there is only a single fingerprint
  64. if (( $(echo "$fingerprint" | wc -l) != 1 )) ; then
  65. cat <<EOF
  66. More than one fingerprint found:
  67. $fingerprint
  68. Please use a more specific key ID.
  69. EOF
  70. failure
  71. fi
  72. log info "key found:"
  73. su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --fingerprint 0x${fingerprint}!"
  74. if [ "$PROMPT" = "true" ] ; then
  75. printf "Are you sure you want to add the above key as a revoker\nof the host key? (Y/n) " >&2
  76. read OK; OK=${OK:-Y}
  77. if [ "${OK/y/Y}" != 'Y' ] ; then
  78. failure "revoker not added."
  79. fi
  80. else
  81. log debug "adding revoker without prompting."
  82. fi
  83. # export the new key to the host keyring
  84. log debug "loading key into host keyring..."
  85. su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --quiet --export 0x${fingerprint}!" \
  86. | gpg_host --import
  87. fi
  88. # edit-key script to add revoker
  89. addrevokerCommand="addrevoker
  90. $fingerprint
  91. y
  92. save
  93. "
  94. # end script
  95. # core ltsigns the newly imported revoker key
  96. log debug "executing add revoker script..."
  97. if echo "$addrevokerCommand" | gpg_host_edit ; then
  98. update_gpg_pub_file
  99. log info "Revoker added."
  100. else
  101. failure "Problem adding revoker."
  102. fi
  103. # remove the temporary directory
  104. trap - EXIT
  105. rm -rf "$tmpDir"
  106. }