aa647a628ae9f0e96e04948056f9279cbf5789d2
[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     # 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"
45
46     # otherwise find all authentication-capable subkeys and use those
47     else
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 \
51             --fingerprint | \
52             grep '^fpr:' | cut -f10 -d: | awk '{ print "0x" $1 "!" }')
53
54         if [ -z "$secretkeys" ]; then
55             failure "You have no secret keys in your keyring!
56 You might want to run 'gpg --gen-key'."
57         fi
58     
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)
63
64         if [ -z "$authsubkeys" ]; then
65             failure "no authentication-capable subkeys available.
66 You might want to run 'monkeysphere gen-subkey'."
67         fi
68     fi
69
70     workingdir=$(msmktempdir)
71     trap "rm -rf $workingdir" EXIT
72     umask 077
73     mkfifo "$workingdir/passphrase"
74
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> 
78
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'."
86             continue
87         fi
88
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.
93
94         primaryuid=$(gpg_user --with-colons --list-key "0x${subkey}!" | grep '^pub:' | cut -f10 -d: | tr -d /)
95
96         #kname="[monkeysphere] $primaryuid"
97         kname="$primaryuid"
98
99         if [ "$1" = '-d' ]; then
100             # we're removing the subkey:
101             gpg_user --export "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname"
102             (cd "$workingdir" && ssh-add -d "$kname") || keysuccess="$?"
103         else
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 "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname" &
109             (cd "$workingdir" && DISPLAY=nosuchdisplay SSH_ASKPASS=/bin/false ssh-add "$@" "$kname" </dev/null )&
110
111             passphrase_prompt "Enter passphrase for key $kname: " "$workingdir/passphrase"
112             wait %2 || keysuccess="$?"
113         fi
114
115         rm -f "$workingdir/$kname"
116     done
117
118     trap - EXIT
119     rm -rf "$workingdir"
120
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?
124     return "$keysuccess"
125 }