adding directions to pass your key id as the first argument. Also added
[monkeysphere.git] / src / seckey2sshagent
1 #!/bin/bash
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 "$TMPPRIVATE"
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 The first argument to the command should be your gpg key id (the 8 
30 character hex string; try gpg --list-key your@emailaddress.org to 
31 lookup your key id).
32
33 This script is a gross hack at the moment.  It is done by creating a
34 new, temporary private keyring, letting the user remove the
35 passphrases from the keys, and then exporting them.  The temporary
36 private keyring is purged from the system.
37
38 When you use this command, you'll find yourself dropped into a GPG
39 'edit-key' dialog relevant *only* to the temporary private keyring.
40
41 At that point, you should clear the password from your key, with:
42
43  passwd
44  <enter your current password>
45
46 followed by the empty string for the new password.  GPG will ask you
47 if you're really sure.  Answer yes, because this is only relevant to
48 the temporary keyring.  Then, do:
49
50  save
51
52 At this point, your key will be added to your running ssh-agent with
53 the alias 'monkeysphere-key' and seckey2sshagent should terminate.
54 You can check on it with:
55
56  ssh-add -l
57
58
59     
60 }
61
62 # if no hex string is supplied, just print an explanation.
63 # this covers seckey2sshagent --help, --usage, -h, etc...
64 if [ -z "$1" ] || [  "$(echo "$1" | tr -d '0-9a-fA-F')" ]; then
65     explanation
66     exit
67 fi
68
69 trap cleanup EXIT
70
71 GPGIDS="$1"
72
73 if [ -z "$GPGIDS" ]; then
74     # default to using all fingerprints of authentication-enabled keys 
75     GPGIDS=$(gpg  --with-colons --fingerprint --fingerprint --list-secret-keys "$GPGID" | egrep -A1 '^(ssb|sec):.*:[^:]*a[^:]*:$' | grep ^fpr: | cut -d: -f10)
76 fi
77
78 for GPGID in $GPGIDS; do
79
80     TMPPRIVATE=$(mktemp -d)
81     
82     gpg --export-secret-key "$GPGID" | GNUPGHOME="$TMPPRIVATE" gpg --import
83     
84 # idea to script the password stuff.  not working.
85 # read -s -p "enter gpg password: " PASSWD; echo
86 # cmd=$(cat <<EOF
87 # passwd
88 # $PASSWD
89 # \n
90 # \n
91 # \n
92 # yes
93 # save
94 # EOF
95 # )
96 # echo -e "$cmd" | GNUPGHOME="$TMPPRIVATE" gpg --command-fd 0 --edit-key $GPGID
97     
98     GNUPGHOME="$TMPPRIVATE" gpg --edit-key "$GPGID"
99
100     KEYNAME='MonkeySphere Key '$(echo "$GPGID" | tr -c -d '0-9a-fA-F')''
101 # creating this alias so the key is named "monkeysphere-key" in the
102 # comment stored by the agent, while never being written to disk in
103 # SSH form:
104     ln -s /dev/stdin "$TMPPRIVATE/$KEYNAME"
105     
106     GNUPGHOME="$TMPPRIVATE" gpg --export-secret-keys "$GPGID" | \
107         openpgp2ssh $GPGID | (cd "$TMPPRIVATE" && ssh-add -c "$KEYNAME")
108
109     cleanup
110 done
111
112