2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
4 # Monkeysphere subkey-to-ssh-agent subcommand
6 # The monkeysphere scripts are written by:
7 # Jameson Rollins <jrollins@finestructure.net>
8 # Jamie McClelland <jm@mayfirst.org>
9 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
11 # They are Copyright 2008-2009, and are all released under the GPL,
14 # try to add all authentication subkeys to the agent
16 subkey_to_ssh_agent() {
26 if ! test_gnu_dummy_s2k_extension ; then
27 failure "Your version of GnuTLS does not seem capable of using with gpg's exported subkeys.
28 You may want to consider patching or upgrading to GnuTLS 2.6 or later.
30 For more details, see:
31 http://lists.gnu.org/archive/html/gnutls-devel/2008-08/msg00005.html"
34 # if there's no agent running, don't bother:
35 if [ -z "$SSH_AUTH_SOCK" ] || ! which ssh-add >/dev/null ; then
36 failure "No ssh-agent available."
39 # and if it looks like it's running, but we can't actually talk to
43 if [ "$sshaddresponse" = "2" ]; then
44 failure "Could not connect to ssh-agent"
47 # get list of secret keys (to work around https://bugs.g10code.com/gnupg/issue945):
48 secretkeys=$(gpg --list-secret-keys --with-colons --fixed-list-mode --fingerprint | \
49 grep '^fpr:' | cut -f10 -d: | awk '{ print "0x" $1 "!" }')
51 if [ -z "$secretkeys" ]; then
52 failure "You have no secret keys in your keyring!
53 You might want to run 'gpg --gen-key'."
56 authsubkeys=$(gpg --list-secret-keys --with-colons --fixed-list-mode \
57 --fingerprint --fingerprint $secretkeys | \
58 cut -f1,5,10,12 -d: | grep -A1 '^ssb:[^:]*::[^:]*a[^:]*$' | \
59 grep '^fpr::' | cut -f3 -d: | sort -u)
61 if [ -z "$authsubkeys" ]; then
62 failure "no authentication-capable subkeys available.
63 You might want to 'monkeysphere gen-subkey'"
66 workingdir=$(mktemp -d ${TMPDIR:-/tmp}/tmp.XXXXXXXXXX)
68 mkfifo "$workingdir/passphrase"
71 # FIXME: we're currently allowing any other options to get passed
72 # through to ssh-add. should we limit it to known ones? For
73 # example: -d or -c and/or -t <lifetime>
75 for subkey in $authsubkeys; do
76 # choose a label by which this key will be known in the agent:
77 # we are labelling the key by User ID instead of by
78 # fingerprint, but filtering out all / characters to make sure
79 # the filename is legit.
81 primaryuid=$(gpg --with-colons --list-key "0x${subkey}!" | grep '^pub:' | cut -f10 -d: | tr -d /)
83 #kname="[monkeysphere] $primaryuid"
86 if [ "$1" = '-d' ]; then
87 # we're removing the subkey:
88 gpg --export "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname"
89 (cd "$workingdir" && ssh-add -d "$kname")
91 # we're adding the subkey:
92 mkfifo "$workingdir/$kname"
93 gpg --quiet --passphrase-fd 3 3<"$workingdir/passphrase" \
94 --export-options export-reset-subkey-passwd,export-minimal,no-export-attributes \
95 --export-secret-subkeys "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname" &
96 (cd "$workingdir" && DISPLAY=nosuchdisplay SSH_ASKPASS=/bin/false ssh-add "$@" "$kname" </dev/null )&
98 passphrase_prompt "Enter passphrase for key $kname: " "$workingdir/passphrase"
103 rm -f "$workingdir/$kname"
108 # FIXME: sort out the return values: we're just returning the
109 # success or failure of the final authentication subkey in this
110 # case. What if earlier ones failed?