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