fix return in subkey_to_ssh_agent, so that it returns, instead of exits
[monkeysphere.git] / src / share / m / subkey_to_ssh_agent
1 # -*-shell-script-*-
2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
3
4 # Monkeysphere subkey-to-ssh-agent subcommand
5 #
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>
10 #
11 # They are Copyright 2008-2009, and are all released under the GPL,
12 # version 3 or later.
13
14 # try to add all authentication subkeys to the agent
15
16 subkey_to_ssh_agent() {
17     local sshaddresponse=0
18     local secretkeys
19     local authsubkeys
20     local workingdir
21     local keysuccess=0
22     local subkey
23     local publine
24     local kname
25
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.
29
30 For more details, see:
31  http://lists.gnu.org/archive/html/gnutls-devel/2008-08/msg00005.html"
32     fi
33
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."
37     fi
38
39     # and if it looks like it's running, but we can't actually talk to
40     # it, bail out:
41     ssh-add -l >/dev/null || sshaddresponse="$?"
42     if [ "$sshaddresponse" = "2" ]; then
43         failure "Could not connect to ssh-agent"
44     fi
45     
46     # get list of secret keys (to work around bug
47     # https://bugs.g10code.com/gnupg/issue945):
48     secretkeys=$(gpg_user --list-secret-keys --with-colons --fixed-list-mode \
49         --fingerprint | \
50         grep '^fpr:' | cut -f10 -d: | awk '{ print "0x" $1 "!" }')
51
52     if [ -z "$secretkeys" ]; then
53         failure "You have no secret keys in your keyring!
54 You might want to run 'gpg --gen-key'."
55     fi
56     
57     authsubkeys=$(gpg_user --list-secret-keys --with-colons --fixed-list-mode \
58         --fingerprint --fingerprint $secretkeys | \
59         cut -f1,5,10,12 -d: | grep -A1 '^ssb:[^:]*::[^:]*a[^:]*$' | \
60         grep '^fpr::' | cut -f3 -d: | sort -u)
61
62     if [ -z "$authsubkeys" ]; then
63         failure "no authentication-capable subkeys available.
64 You might want to 'monkeysphere gen-subkey'"
65     fi
66
67     workingdir=$(msmktempdir)
68     trap "rm -rf $workingdir" EXIT
69     umask 077
70     mkfifo "$workingdir/passphrase"
71     keysuccess=1
72
73     # FIXME: we're currently allowing any other options to get passed
74     # through to ssh-add.  should we limit it to known ones?  For
75     # example: -d or -c and/or -t <lifetime> 
76
77     for subkey in $authsubkeys; do 
78         # choose a label by which this key will be known in the agent:
79         # we are labelling the key by User ID instead of by
80         # fingerprint, but filtering out all / characters to make sure
81         # the filename is legit.
82
83         primaryuid=$(gpg_user --with-colons --list-key "0x${subkey}!" | grep '^pub:' | cut -f10 -d: | tr -d /)
84
85         #kname="[monkeysphere] $primaryuid"
86         kname="$primaryuid"
87
88         if [ "$1" = '-d' ]; then
89             # we're removing the subkey:
90             gpg_user --export "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname"
91             (cd "$workingdir" && ssh-add -d "$kname")
92         else
93             # we're adding the subkey:
94             mkfifo "$workingdir/$kname"
95             gpg_user --passphrase-fd 3 3<"$workingdir/passphrase" \
96                 --export-options export-reset-subkey-passwd,export-minimal,no-export-attributes \
97                 --export-secret-subkeys "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname" &
98             (cd "$workingdir" && DISPLAY=nosuchdisplay SSH_ASKPASS=/bin/false ssh-add "$@" "$kname" </dev/null )&
99
100             passphrase_prompt "Enter passphrase for key $kname: " "$workingdir/passphrase"
101             wait %2
102         fi || keysuccess="$?"
103
104         rm -f "$workingdir/$kname"
105     done
106
107     trap - EXIT
108     rm -rf "$workingdir"
109
110     # FIXME: sort out the return values: we're just returning the
111     # success or failure of the final authentication subkey in this
112     # case.  What if earlier ones failed?
113     return "$keysuccess"
114 }