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