break out a bunch of common functions in monkeysphere-host:
[monkeysphere.git] / src / share / mh / gen_key
index 162a64e17795ff4a4bfcf6a5c2ddba9cf667200b..873ed0298d24ae1e7a02e34359878eae73ee5967 100644 (file)
 
 gen_key() {
 
+local hostName=$(hostname -f)
 local keyType="RSA"
 local keyLength="2048"
 local keyUsage="auth"
-local keyExpire
-local hostName=$(hostname -f)
+local keyExpire="0"
 local userID
-local keyParameters
-local fingerprint
-
-# check for presense of secret key
-# FIXME: is this the proper test to be doing here?
-fingerprint_server_key >/dev/null \
-       && failure "An OpenPGP host key already exists."
 
 # get options
 while true ; do
@@ -34,68 +27,47 @@ while true ; do
                keyLength="$2"
                shift 2
                ;;
-           -e|--expire)
-               keyExpire="$2"
-               shift 2
-               ;;
            *)
                if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
                    failure "Unknown option '$1'.
 Type '$PGRM help' for usage."
                fi
-               hostName="$1"
-               shift;
                break
                ;;
        esac
 done
 
+hostName="$1"
 userID="ssh://${hostName}"
 
-# prompt about key expiration if not specified
-keyExpire=$(get_gpg_expiration "$keyExpire")
+# create host home
+mkdir -p "$GNUPGHOME_HOST"
+chmod 700 "$GNUPGHOME_HOST"
 
-# set key parameters
-keyParameters=\
-"Key-Type: $keyType
+log debug "generating host key..."
+gpg_host --batch --gen-key <<EOF
+Key-Type: $keyType
 Key-Length: $keyLength
 Key-Usage: $keyUsage
 Name-Real: $userID
-Expire-Date: $keyExpire"
-
-echo "The following key parameters will be used for the host private key:"
-echo "$keyParameters"
+Expire-Date: $keyExpire
 
-read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
-if [ ${OK/y/Y} != 'Y' ] ; then
-       failure "aborting."
-fi
+%commit
+%echo done
 
-# add commit command
-# must include blank line!
-keyParameters=\
-"${keyParameters}
+EOF
 
-%commit
-%echo done"
+# load the new host fpr into the fpr variable
+load_fingerprint_secret
 
-log verbose "generating host key..."
-echo "$keyParameters" | gpg_host --batch --gen-key
+# export to ssh secret key file
+create_ssh_sec_file
 
-# find the key fingerprint of the newly generated key
-fingerprint=$(fingerprint_server_key)
+# export to ssh public key file
+create_ssh_pub_file
 
-# translate the private key to ssh format, and export to a file
-# for sshs usage.
-# NOTE: assumes that the primary key is the proper key to use
-(umask 077 && \
-       gpg_host --export-secret-key "$fingerprint" | \
-       openpgp2ssh "$fingerprint" > "${SYSDATADIR}/ssh_host_rsa_key")
-log info "SSH host private key output to file: ${SYSDATADIR}/ssh_host_rsa_key"
-ssh-keygen -y -f "${SYSDATADIR}/ssh_host_rsa_key" > "${SYSDATADIR}/ssh_host_rsa_key.pub"
-log info "SSH host public key output to file: ${SYSDATADIR}/ssh_host_rsa_key.pub"
-gpg_host "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
-log info "SSH host public key in OpenPGP form: ${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
+# export to gpg public key to file
+create_gpg_pub_file
 
 # show info about new key
 show_key