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 # FIXME: what if you only want to add one authentication subkey to the
19 subkey_to_ssh_agent() {
20 local sshaddresponse=0
29 # if there's no agent running, don't bother:
30 if [ -z "$SSH_AUTH_SOCK" ] || ! type ssh-add >/dev/null ; then
31 failure "No ssh-agent available."
34 # and if it looks like it's running, but we can't actually talk to
36 ssh-add -l >/dev/null || sshaddresponse="$?"
37 if [ "$sshaddresponse" = "2" ]; then
38 failure "Could not connect to ssh-agent"
41 # if the MONKEYSPHERE_SUBKEYS_FOR_AGENT variable is set, use the
42 # keys specified there
43 if [ "$MONKEYSPHERE_SUBKEYS_FOR_AGENT" ] ; then
44 authsubkeys="$MONKEYSPHERE_SUBKEYS_FOR_AGENT"
46 # otherwise find all authentication-capable subkeys and use those
48 # get list of secret keys
49 # (to work around bug https://bugs.g10code.com/gnupg/issue945):
50 secretkeys=$(gpg_user --list-secret-keys --with-colons --fixed-list-mode \
52 grep '^fpr:' | cut -f10 -d: | awk '{ print "0x" $1 "!" }')
54 if [ -z "$secretkeys" ]; then
55 failure "You have no secret keys in your keyring!
56 You might want to run 'gpg --gen-key'."
59 authsubkeys=$(gpg_user --list-secret-keys --with-colons --fixed-list-mode \
60 --fingerprint --fingerprint $secretkeys | \
61 cut -f1,5,10,12 -d: | grep -A1 '^ssb:[^:]*::[^:]*a[^:]*$' | \
62 grep '^fpr::' | cut -f3 -d: | sort -u)
64 if [ -z "$authsubkeys" ]; then
65 failure "no authentication-capable subkeys available.
66 You might want to run 'monkeysphere gen-subkey'."
70 workingdir=$(msmktempdir)
71 trap "rm -rf $workingdir" EXIT
73 mkfifo "$workingdir/passphrase"
75 # FIXME: we're currently allowing any other options to get passed
76 # through to ssh-add. should we limit it to known ones? For
77 # example: -d or -c and/or -t <lifetime>
79 for subkey in $authsubkeys; do
80 # test that the subkey has proper capability
81 capability=$(gpg_user --list-secret-keys --with-colons --fixed-list-mode \
82 --fingerprint --fingerprint "0x${subkey}!" \
83 | egrep -B 1 "^fpr:::::::::${subkey}:$" | grep "^ssb:" | cut -d: -f12)
84 if ! check_capability "$capability" 'a' ; then
85 log error "Did not find authentication-capable subkey with key ID '$subkey'."
89 # choose a label by which this key will be known in the agent:
90 # we are labelling the key by User ID instead of by
91 # fingerprint, but filtering out all / characters to make sure
92 # the filename is legit.
94 primaryuid=$(gpg_user --with-colons --list-key "0x${subkey}!" | grep '^pub:' | cut -f10 -d: | tr -d /)
96 #kname="[monkeysphere] $primaryuid"
99 if [ "$1" = '-d' ]; then
100 # we're removing the subkey:
101 gpg_user --export --no-armor "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname"
102 (cd "$workingdir" && ssh-add -d "$kname") || keysuccess="$?"
104 # we're adding the subkey:
105 mkfifo "$workingdir/$kname"
106 gpg_user --passphrase-fd 3 3<"$workingdir/passphrase" \
107 --export-options export-reset-subkey-passwd,export-minimal,no-export-attributes \
108 --export-secret-subkeys --no-armor "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname" &
109 (cd "$workingdir" && DISPLAY=nosuchdisplay SSH_ASKPASS=/bin/false ssh-add "$@" "$kname" </dev/null )&
111 passphrase_prompt "Enter passphrase for key $kname: " "$workingdir/passphrase"
112 wait %2 || keysuccess="$?"
115 rm -f "$workingdir/$kname"
121 # FIXME: sort out the return values: we're just returning the
122 # failure code of the last authentication subkey which fails.
123 # what if more than one authentication subkey fails?