summaryrefslogtreecommitdiff
path: root/src/share/m/subkey_to_ssh_agent
blob: 012c95f80f5bbce1640821fb3eada10e15b1b078 (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
  15. local secretkeys
  16. local authsubkeys
  17. local workingdir
  18. local keysuccess
  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
  35. sshaddresponse="$?"
  36. if [ "$sshaddresponse" = "2" ]; then
  37. failure "Could not connect to ssh-agent"
  38. fi
  39. # get list of secret keys (to work around bug
  40. # https://bugs.g10code.com/gnupg/issue945):
  41. secretkeys=$(gpg --list-secret-keys --with-colons --fixed-list-mode --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 --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=$(mktemp -d ${TMPDIR:-/tmp}/tmp.XXXXXXXXXX)
  56. umask 077
  57. mkfifo "$workingdir/passphrase"
  58. keysuccess=1
  59. # FIXME: we're currently allowing any other options to get passed
  60. # through to ssh-add. should we limit it to known ones? For
  61. # example: -d or -c and/or -t <lifetime>
  62. for subkey in $authsubkeys; do
  63. # choose a label by which this key will be known in the agent:
  64. # we are labelling the key by User ID instead of by
  65. # fingerprint, but filtering out all / characters to make sure
  66. # the filename is legit.
  67. primaryuid=$(gpg --with-colons --list-key "0x${subkey}!" | grep '^pub:' | cut -f10 -d: | tr -d /)
  68. #kname="[monkeysphere] $primaryuid"
  69. kname="$primaryuid"
  70. if [ "$1" = '-d' ]; then
  71. # we're removing the subkey:
  72. gpg --export "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname"
  73. (cd "$workingdir" && ssh-add -d "$kname")
  74. else
  75. # we're adding the subkey:
  76. mkfifo "$workingdir/$kname"
  77. gpg --quiet --passphrase-fd 3 3<"$workingdir/passphrase" \
  78. --export-options export-reset-subkey-passwd,export-minimal,no-export-attributes \
  79. --export-secret-subkeys "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname" &
  80. (cd "$workingdir" && DISPLAY=nosuchdisplay SSH_ASKPASS=/bin/false ssh-add "$@" "$kname" </dev/null )&
  81. passphrase_prompt "Enter passphrase for key $kname: " "$workingdir/passphrase"
  82. wait %2
  83. fi
  84. keysuccess="$?"
  85. rm -f "$workingdir/$kname"
  86. done
  87. rm -rf "$workingdir"
  88. # FIXME: sort out the return values: we're just returning the
  89. # success or failure of the final authentication subkey in this
  90. # case. What if earlier ones failed?
  91. exit "$keysuccess"
  92. }