summaryrefslogtreecommitdiff
path: root/src/share/m/subkey_to_ssh_agent
blob: 7fb2fdb302e98f341a36858eedafcb219599ce0f (plain)
  1. # -*-shell-script-*-
  2. # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
  3. # Monkeysphere subkey-to-ssh-agent 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-2009, and are all released under the GPL,
  11. # version 3 or later.
  12. # try to add all authentication subkeys to the agent
  13. subkey_to_ssh_agent() {
  14. local sshaddresponse=0
  15. local secretkeys
  16. local authsubkeys
  17. local workingdir
  18. local keysuccess=0
  19. local subkey
  20. local publine
  21. local kname
  22. if ! test_gnu_dummy_s2k_extension ; then
  23. failure "Your version of GnuTLS does not seem capable of using with gpg's exported subkeys.
  24. You may want to consider patching or upgrading to GnuTLS 2.6 or later.
  25. For more details, see:
  26. http://lists.gnu.org/archive/html/gnutls-devel/2008-08/msg00005.html"
  27. fi
  28. # if there's no agent running, don't bother:
  29. if [ -z "$SSH_AUTH_SOCK" ] || ! which ssh-add >/dev/null ; then
  30. failure "No ssh-agent available."
  31. fi
  32. # and if it looks like it's running, but we can't actually talk to
  33. # it, bail out:
  34. ssh-add -l >/dev/null || sshaddresponse="$?"
  35. if [ "$sshaddresponse" = "2" ]; then
  36. failure "Could not connect to ssh-agent"
  37. fi
  38. # get list of secret keys (to work around bug
  39. # https://bugs.g10code.com/gnupg/issue945):
  40. secretkeys=$(gpg_user --list-secret-keys --with-colons --fixed-list-mode \
  41. --fingerprint | \
  42. grep '^fpr:' | cut -f10 -d: | awk '{ print "0x" $1 "!" }')
  43. if [ -z "$secretkeys" ]; then
  44. failure "You have no secret keys in your keyring!
  45. You might want to run 'gpg --gen-key'."
  46. fi
  47. authsubkeys=$(gpg_user --list-secret-keys --with-colons --fixed-list-mode \
  48. --fingerprint --fingerprint $secretkeys | \
  49. cut -f1,5,10,12 -d: | grep -A1 '^ssb:[^:]*::[^:]*a[^:]*$' | \
  50. grep '^fpr::' | cut -f3 -d: | sort -u)
  51. if [ -z "$authsubkeys" ]; then
  52. failure "no authentication-capable subkeys available.
  53. You might want to 'monkeysphere gen-subkey'"
  54. fi
  55. workingdir=$(msmktempdir)
  56. trap "rm -rf $workingdir" EXIT
  57. umask 077
  58. mkfifo "$workingdir/passphrase"
  59. keysuccess=1
  60. # FIXME: we're currently allowing any other options to get passed
  61. # through to ssh-add. should we limit it to known ones? For
  62. # example: -d or -c and/or -t <lifetime>
  63. for subkey in $authsubkeys; do
  64. # choose a label by which this key will be known in the agent:
  65. # we are labelling the key by User ID instead of by
  66. # fingerprint, but filtering out all / characters to make sure
  67. # the filename is legit.
  68. primaryuid=$(gpg_user --with-colons --list-key "0x${subkey}!" | grep '^pub:' | cut -f10 -d: | tr -d /)
  69. #kname="[monkeysphere] $primaryuid"
  70. kname="$primaryuid"
  71. if [ "$1" = '-d' ]; then
  72. # we're removing the subkey:
  73. gpg_user --export "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname"
  74. (cd "$workingdir" && ssh-add -d "$kname")
  75. else
  76. # we're adding the subkey:
  77. mkfifo "$workingdir/$kname"
  78. gpg_user --passphrase-fd 3 3<"$workingdir/passphrase" \
  79. --export-options export-reset-subkey-passwd,export-minimal,no-export-attributes \
  80. --export-secret-subkeys "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname" &
  81. (cd "$workingdir" && DISPLAY=nosuchdisplay SSH_ASKPASS=/bin/false ssh-add "$@" "$kname" </dev/null )&
  82. passphrase_prompt "Enter passphrase for key $kname: " "$workingdir/passphrase"
  83. wait %2
  84. fi || keysuccess="$?"
  85. rm -f "$workingdir/$kname"
  86. done
  87. trap - EXIT
  88. rm -rf "$workingdir"
  89. # FIXME: sort out the return values: we're just returning the
  90. # success or failure of the final authentication subkey in this
  91. # case. What if earlier ones failed?
  92. return "$keysuccess"
  93. }