removed test_gnu_dummy_s2k_extension(); no longer necessary
[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 # FIXME: what if you only want to add one authentication subkey to the
17 # agent?
18
19 subkey_to_ssh_agent() {
20     local sshaddresponse=0
21     local secretkeys
22     local authsubkeys
23     local workingdir
24     local keysuccess=0
25     local subkey
26     local publine
27     local kname
28
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."
32     fi
33
34     # and if it looks like it's running, but we can't actually talk to
35     # it, bail out:
36     ssh-add -l >/dev/null || sshaddresponse="$?"
37     if [ "$sshaddresponse" = "2" ]; then
38         failure "Could not connect to ssh-agent"
39     fi
40     
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 \
44         --fingerprint | \
45         grep '^fpr:' | cut -f10 -d: | awk '{ print "0x" $1 "!" }')
46
47     if [ -z "$secretkeys" ]; then
48         failure "You have no secret keys in your keyring!
49 You might want to run 'gpg --gen-key'."
50     fi
51     
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)
56
57     if [ -z "$authsubkeys" ]; then
58         failure "no authentication-capable subkeys available.
59 You might want to 'monkeysphere gen-subkey'"
60     fi
61
62     workingdir=$(msmktempdir)
63     trap "rm -rf $workingdir" EXIT
64     umask 077
65     mkfifo "$workingdir/passphrase"
66
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> 
70
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.
76
77         primaryuid=$(gpg_user --with-colons --list-key "0x${subkey}!" | grep '^pub:' | cut -f10 -d: | tr -d /)
78
79         #kname="[monkeysphere] $primaryuid"
80         kname="$primaryuid"
81
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="$?"
86         else
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 )&
93
94             passphrase_prompt "Enter passphrase for key $kname: " "$workingdir/passphrase"
95             wait %2 || keysuccess="$?"
96         fi
97
98         rm -f "$workingdir/$kname"
99     done
100
101     trap - EXIT
102     rm -rf "$workingdir"
103
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?
107     return "$keysuccess"
108 }