merging jrollins and micah work, reverting ui changes for m-h gen-key and import-key
[monkeysphere.git] / src / subcommands / mh / gen-key
1 #!/usr/bin/env bash
2
3 # Monkeysphere host gen-key subcommand
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 #
10 # They are Copyright 2008, and are all released under the GPL, version 3
11 # or later.
12
13 local keyType="RSA"
14 local keyLength="2048"
15 local keyUsage="auth"
16 local keyExpire
17 local revoker
18 local hostName=$(hostname -f)
19 local userID
20 local keyParameters
21 local fingerprint
22
23 # check for presense of secret key
24 # FIXME: is this the proper test to be doing here?
25 fingerprint_server_key >/dev/null \
26         && failure "An OpenPGP host key already exists."
27
28 # get options
29 while true ; do
30         case "$1" in
31             -l|--length)
32                 keyLength="$2"
33                 shift 2
34                 ;;
35             -e|--expire)
36                 keyExpire="$2"
37                 shift 2
38                 ;;
39             -r|--revoker)
40                 revoker="$2"
41                 shift 2
42                 ;;
43             *)
44                 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
45                     failure "Unknown option '$1'.
46 Type '$PGRM help' for usage."
47                 fi
48                 hostName="$1"
49                 shift;
50                 break
51                 ;;
52         esac
53 done
54
55 userID="ssh://${hostName}"
56
57 # prompt about key expiration if not specified
58 keyExpire=$(get_gpg_expiration "$keyExpire")
59
60 # set key parameters
61 keyParameters=\
62 "Key-Type: $keyType
63 Key-Length: $keyLength
64 Key-Usage: $keyUsage
65 Name-Real: $userID
66 Expire-Date: $keyExpire"
67
68 # add the revoker field if specified
69 # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key.
70 # FIXME: key is marked "sensitive"?  is this appropriate?
71 if [ "$revoker" ] ; then
72         keyParameters=\
73 "${keyParameters}
74 Revoker: 1:${revoker} sensitive"
75 fi
76
77 echo "The following key parameters will be used for the host private key:"
78 echo "$keyParameters"
79
80 read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
81 if [ ${OK/y/Y} != 'Y' ] ; then
82         failure "aborting."
83 fi
84
85 # add commit command
86 # must include blank line!
87 keyParameters=\
88 "${keyParameters}
89
90 %commit
91 %echo done"
92
93 log verbose "generating host key..."
94 echo "$keyParameters" | gpg_host --batch --gen-key
95
96 # find the key fingerprint of the newly generated key
97 fingerprint=$(fingerprint_server_key)
98
99 # export host ownertrust to authentication keyring
100 log verbose "setting ultimate owner trust for host key..."
101 echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
102
103 # translate the private key to ssh format, and export to a file
104 # for sshs usage.
105 # NOTE: assumes that the primary key is the proper key to use
106 (umask 077 && \
107         gpg_host --export-secret-key "$fingerprint" | \
108         openpgp2ssh "$fingerprint" > "${SYSDATADIR}/ssh_host_rsa_key")
109 log info "SSH host private key output to file: ${SYSDATADIR}/ssh_host_rsa_key"
110 ssh-keygen -y -f "${SYSDATADIR}/ssh_host_rsa_key" > "${SYSDATADIR}/ssh_host_rsa_key.pub"
111 log info "SSH host public key output to file: ${SYSDATADIR}/ssh_host_rsa_key.pub"
112 gpg_authentication "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
113 log info "SSH host public key in OpenPGP form: ${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
114
115 # show info about new key
116 show_server_key