rework the tests to use the new reorganization
[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 gen_key() {
14
15 local keyType="RSA"
16 local keyLength="2048"
17 local keyUsage="auth"
18 local keyExpire
19 local hostName=$(hostname -f)
20 local userID
21 local keyParameters
22 local fingerprint
23
24 # check for presense of secret key
25 # FIXME: is this the proper test to be doing here?
26 fingerprint_server_key >/dev/null \
27         && failure "An OpenPGP host key already exists."
28
29 # get options
30 while true ; do
31         case "$1" in
32             -l|--length)
33                 keyLength="$2"
34                 shift 2
35                 ;;
36             -e|--expire)
37                 keyExpire="$2"
38                 shift 2
39                 ;;
40             *)
41                 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
42                     failure "Unknown option '$1'.
43 Type '$PGRM help' for usage."
44                 fi
45                 hostName="$1"
46                 shift;
47                 break
48                 ;;
49         esac
50 done
51
52 userID="ssh://${hostName}"
53
54 # prompt about key expiration if not specified
55 keyExpire=$(get_gpg_expiration "$keyExpire")
56
57 # set key parameters
58 keyParameters=\
59 "Key-Type: $keyType
60 Key-Length: $keyLength
61 Key-Usage: $keyUsage
62 Name-Real: $userID
63 Expire-Date: $keyExpire"
64
65 echo "The following key parameters will be used for the host private key:"
66 echo "$keyParameters"
67
68 read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
69 if [ ${OK/y/Y} != 'Y' ] ; then
70         failure "aborting."
71 fi
72
73 # add commit command
74 # must include blank line!
75 keyParameters=\
76 "${keyParameters}
77
78 %commit
79 %echo done"
80
81 log verbose "generating host key..."
82 echo "$keyParameters" | gpg_host --batch --gen-key
83
84 # find the key fingerprint of the newly generated key
85 fingerprint=$(fingerprint_server_key)
86
87 # export host ownertrust to authentication keyring
88 log verbose "setting ultimate owner trust for host key..."
89 echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
90
91 # translate the private key to ssh format, and export to a file
92 # for sshs usage.
93 # NOTE: assumes that the primary key is the proper key to use
94 (umask 077 && \
95         gpg_host --export-secret-key "$fingerprint" | \
96         openpgp2ssh "$fingerprint" > "${SYSDATADIR}/ssh_host_rsa_key")
97 log info "SSH host private key output to file: ${SYSDATADIR}/ssh_host_rsa_key"
98 ssh-keygen -y -f "${SYSDATADIR}/ssh_host_rsa_key" > "${SYSDATADIR}/ssh_host_rsa_key.pub"
99 log info "SSH host public key output to file: ${SYSDATADIR}/ssh_host_rsa_key.pub"
100 gpg_authentication "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
101 log info "SSH host public key in OpenPGP form: ${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
102
103 # show info about new key
104 show_key
105
106 }