summaryrefslogtreecommitdiff
path: root/src/seckey2sshagent
blob: ecfd7aa6ebdeee0bb64dfacdb92a213549a00524 (plain)
  1. #!/bin/bash
  2. # seckey2sshagent: this is a hack of a script to cope with the fact
  3. # that openpgp2ssh currently cannot support encrypted secret keys.
  4. # the basic operating principal is:
  5. # export the secret key in encrypted format to a new keyring
  6. # remove the passphrase in that keyring
  7. # use that keyring with openpgp2ssh
  8. # Authors: Daniel Kahn Gillmor <dkg@fifthhorseman.net>,
  9. # Jameson Rollins <jrollins@fifthhorseman.net>
  10. cleanup() {
  11. echo -n "removing temp gpg home... " 1>&2
  12. rm -rf "$TMPPRIVATE"
  13. echo "done." 1>&2
  14. }
  15. explanation() {
  16. echo -n "The basic strategy of seckey2sshagent is to dump your
  17. OpenPGP authentication key(s) into your agent.
  18. The first argument to the command should be your gpg key id (the 8
  19. character hex string; try gpg --list-key your@emailaddress.org to
  20. lookup your key id).
  21. This script is a gross hack at the moment. It is done by creating a
  22. new, temporary private keyring, letting the user remove the
  23. passphrases from the keys, and then exporting them. The temporary
  24. private keyring is purged from the system.
  25. When you use this command, you'll find yourself dropped into a GPG
  26. 'edit-key' dialog relevant *only* to the temporary private keyring.
  27. At that point, you should clear the password from your key, with:
  28. passwd
  29. <enter your current password>
  30. followed by the empty string for the new password. GPG will ask you
  31. if you're really sure. Answer yes, because this is only relevant to
  32. the temporary keyring. Then, do:
  33. save
  34. At this point, your key will be added to your running ssh-agent with
  35. the alias 'monkeysphere-key' and seckey2sshagent should terminate.
  36. You can check on it with:
  37. ssh-add -l
  38. "
  39. }
  40. # if no hex string is supplied, just print an explanation.
  41. # this covers seckey2sshagent --help, --usage, -h, etc...
  42. if [ -z "$1" ] || [ "$(echo "$1" | tr -d '0-9a-fA-F')" ]; then
  43. explanation
  44. exit
  45. fi
  46. trap cleanup EXIT
  47. GPGIDS="$1"
  48. if [ -z "$GPGIDS" ]; then
  49. # default to using all fingerprints of authentication-enabled keys
  50. GPGIDS=$(gpg --with-colons --fingerprint --fingerprint --list-secret-keys "$GPGID" | egrep -A1 '^(ssb|sec):.*:[^:]*a[^:]*:$' | grep ^fpr: | cut -d: -f10)
  51. fi
  52. for GPGID in $GPGIDS; do
  53. TMPPRIVATE=$(mktemp -d)
  54. gpg --export-secret-key "$GPGID" | GNUPGHOME="$TMPPRIVATE" gpg --import
  55. # idea to script the password stuff. not working.
  56. # read -s -p "enter gpg password: " PASSWD; echo
  57. # cmd=$(cat <<EOF
  58. # passwd
  59. # $PASSWD
  60. # \n
  61. # \n
  62. # \n
  63. # yes
  64. # save
  65. # EOF
  66. # )
  67. # echo -e "$cmd" | GNUPGHOME="$TMPPRIVATE" gpg --command-fd 0 --edit-key $GPGID
  68. GNUPGHOME="$TMPPRIVATE" gpg --edit-key "$GPGID"
  69. KEYNAME='MonkeySphere Key '$(echo "$GPGID" | tr -c -d '0-9a-fA-F')''
  70. # creating this alias so the key is named "monkeysphere-key" in the
  71. # comment stored by the agent, while never being written to disk in
  72. # SSH form:
  73. ln -s /dev/stdin "$TMPPRIVATE/$KEYNAME"
  74. GNUPGHOME="$TMPPRIVATE" gpg --export-secret-keys "$GPGID" | \
  75. openpgp2ssh $GPGID | (cd "$TMPPRIVATE" && ssh-add -c "$KEYNAME")
  76. cleanup
  77. done