cleanup and explanation for seckey2sshagent hack.
[monkeysphere.git] / src / seckey2sshagent
1 #!/bin/sh
2
3 # seckey2sshagent: this is a hack of a script to cope with the fact
4 # that openpgp2ssh currently cannot support encrypted secret keys.
5
6 # the basic operating principal is: 
7
8 # export the secret key in encrypted format to a new keyring
9
10 # remove the passphrase in that keyring
11
12 # use that keyring with openpgp2ssh
13
14 # Authors: Daniel Kahn Gillmor <dkg@fifthhorseman.net>,
15 #          Jameson Rollins <jrollins@fifthhorseman.net>
16
17
18 cleanup() {
19     echo -n "removing temp gpg home... " 1>&2
20     rm -rf $FOO
21     echo "done." 1>&2
22 }
23
24 explanation() {
25
26     echo -n "The basic strategy of seckey2sshagent is to dump your
27 OpenPGP authentication key(s) into your agent.
28
29 This script is a gross hack at the moment.  It is done by creating a
30 new, temporary private keyring, letting the user remove the
31 passphrases from the keys, and then exporting them.  The temporary
32 private keyring is purged from the system.
33
34 When you use this command, you'll find yourself dropped into a GPG
35 'edit-key' dialog relevant *only* to the temporary private keyring.
36
37 At that point, you should clear the password from your key, with:
38
39  passwd
40  <enter your current password>
41
42 followed by the empty string for the new password.  GPG will ask you
43 if you're really sure.  Answer yes, because this is only relevant to
44 the temporary keyring.  Then, do:
45
46  save
47  exit
48
49 At this point, your key will be added to your running ssh-agent with
50 the alias 'monkeysphere-key' and seckey2sshagent should terminate.
51 You can check on it with:
52
53  ssh-add -l
54
55
56     
57 }
58
59 # if no hex string is supplied, just print an explanation.
60 # this covers seckey2sshagent --help, --usage, -h, etc...
61 if [ "$(echo "$1" | tr -d '0-9a-fA-F')" ]; then
62     explanation
63     exit
64 fi
65
66 trap cleanup EXIT
67
68 GPGIDS="$1"
69
70 if [ -z "$GPGIDS" ]; then
71     # default to using all fingerprints of authentication-enabled keys 
72     GPGIDS=$(gpg  --with-colons --fingerprint --fingerprint --list-secret-keys "$GPGID" | egrep -A1 '^(ssb|sec):.*:[^:]*a[^:]*:$' | grep ^fpr: | cut -d: -f10)
73 fi
74
75 for GPGID in $GPGIDS; do
76
77     TMPPRIVATE=$(mktemp -d)
78     
79     gpg --export-secret-key $GPGID | GNUPGHOME="$TMPPRIVATE" gpg --import
80     
81 # idea to script the password stuff.  not working.
82 # read -s -p "enter gpg password: " PASSWD; echo
83 # cmd=$(cat <<EOF
84 # passwd
85 # $PASSWD
86 # \n
87 # \n
88 # \n
89 # yes
90 # save
91 # EOF
92 # )
93 # echo -e "$cmd" | GNUPGHOME="$TMPPRIVATE" gpg --command-fd 0 --edit-key $GPGID
94     
95     GNUPGHOME="$TMPPRIVATE" gpg --edit-key $GPGID
96     
97 # creating this alias so the key is named "monkeysphere-key" in the
98 # comment stored by the agent, while never being written to disk in
99 # SSH form:
100     ln -s /dev/stdin "$TMPPRIVATE"/monkeysphere-key
101     
102     GNUPGHOME="$TMPPRIVATE" gpg --export-secret-keys $GPGID | \
103         openpgp2ssh $GPGID | (cd "$TMPPRIVATE" && ssh-add -c monkeysphere-key)
104
105     cleanup
106 done
107
108