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" ] || ! which 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 # get list of secret keys (to work around bug
42 # https://bugs.g10code.com/gnupg/issue945):
43 secretkeys=$(gpg_user --list-secret-keys --with-colons --fixed-list-mode \
45 grep '^fpr:' | cut -f10 -d: | awk '{ print "0x" $1 "!" }')
47 if [ -z "$secretkeys" ]; then
48 failure "You have no secret keys in your keyring!
49 You might want to run 'gpg --gen-key'."
52 authsubkeys=$(gpg_user --list-secret-keys --with-colons --fixed-list-mode \
53 --fingerprint --fingerprint $secretkeys | \
54 cut -f1,5,10,12 -d: | grep -A1 '^ssb:[^:]*::[^:]*a[^:]*$' | \
55 grep '^fpr::' | cut -f3 -d: | sort -u)
57 if [ -z "$authsubkeys" ]; then
58 failure "no authentication-capable subkeys available.
59 You might want to 'monkeysphere gen-subkey'"
62 workingdir=$(msmktempdir)
63 trap "rm -rf $workingdir" EXIT
65 mkfifo "$workingdir/passphrase"
67 # FIXME: we're currently allowing any other options to get passed
68 # through to ssh-add. should we limit it to known ones? For
69 # example: -d or -c and/or -t <lifetime>
71 for subkey in $authsubkeys; do
72 # choose a label by which this key will be known in the agent:
73 # we are labelling the key by User ID instead of by
74 # fingerprint, but filtering out all / characters to make sure
75 # the filename is legit.
77 primaryuid=$(gpg_user --with-colons --list-key "0x${subkey}!" | grep '^pub:' | cut -f10 -d: | tr -d /)
79 #kname="[monkeysphere] $primaryuid"
82 if [ "$1" = '-d' ]; then
83 # we're removing the subkey:
84 gpg_user --export "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname"
85 (cd "$workingdir" && ssh-add -d "$kname") || keysuccess="$?"
87 # we're adding the subkey:
88 mkfifo "$workingdir/$kname"
89 gpg_user --passphrase-fd 3 3<"$workingdir/passphrase" \
90 --export-options export-reset-subkey-passwd,export-minimal,no-export-attributes \
91 --export-secret-subkeys "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname" &
92 (cd "$workingdir" && DISPLAY=nosuchdisplay SSH_ASKPASS=/bin/false ssh-add "$@" "$kname" </dev/null )&
94 passphrase_prompt "Enter passphrase for key $kname: " "$workingdir/passphrase"
95 wait %2 || keysuccess="$?"
98 rm -f "$workingdir/$kname"
104 # FIXME: sort out the return values: we're just returning the
105 # failure code of the last authentication subkey which fails.
106 # what if more than one authentication subkey fails?