#!/bin/bash
###############################################################################
## makekey                                                                    #
###############################################################################
## Copyright 2002. Spinnaker Networks Inc.                                    #
## All rights reserved.                                                       #
##                                                                            #
###############################################################################
## This script is used by generate a new key & certifcate to be used by SSL   #
##                                                                            #
###############################################################################

#########
## Function: Interrupted
## Called when the script is interrupted
#########
trap 'Interrupted' INT QUIT TERM

Interrupted ()
{
    Cleanup
}

#########
## Function: Cleanup
## Perform Cleanup
#########
Cleanup ()
{
    rm -f ${RAND_FILE}
    rm -f ${CONF_FILE}
    rm -f ${LOG_FILE}
    rm -f ${PEM_FILE}
}

#########
## Function: Usage
## Displays the Usage for this script
#########
Usage ()
{
    echo "usage:  makekey [-s] [-h]" 
    echo "            s:  (optional) save the key/certificate in /var/spinmgmt/keys/spinmgmt.pem" 
    echo "                to be used as the default key for SSL"
    echo "                if not specified, the key/certificate is sent to standard output"
    echo "            h:  displays this usage message" 
}

#########
## Function: GetOptions
## Get the options
#########
GetOptions ()
{
    #########
    ## Retrieve the Command Line Options
    #########

    SAVE_AS_DEFAULT_KEY=0
    GETOPTS="getopts ':sh' OPTION $*"

    ${GETOPTS}
    RET=$?
    while [ ${RET}  -le 0 ]
    do
      case ${OPTION} in
	  s)  SAVE_AS_DEFAULT_KEY=1
	      ;;
	  h)  Usage
	      exit 0
	      ;;
	  *)  Usage
	      exit 1
	      ;;
      esac

      ${GETOPTS}
      RET=$?
    done
}

RAND_FILE=/tmp/ssl.rnd
CONF_FILE=/tmp/ssl.conf
LOG_FILE=/tmp/ssl.log
PEM_FILE=/tmp/spinmgmt.pem
SAVE_PEM_FILE=/var/spinmgmt/keys/spinmgmt.pem

#########
## retrieve the commandline options
#########
GetOptions $*

#########
## Verify opennssl exists
#########

if [ ! -f /usr/bin/openssl ];
then
    echo "openssl not found; cannot continue" >&2
    exit 1
fi


#########
## generate the pseudo random seed file
#########
rm -f ${RAND_FILE}
/usr/bin/openssl rand -out ${RAND_FILE} 1024

#########
## generate the openssl config file
#########
rm -f ${CONF_FILE}
echo "
        RANDFILE = /tmp/ssl.rnd
        
        [ req ]
        default_bits = 1024
        encrypt_key = yes
        distinguished_name = req_dn
        x509_extensions = cert_type
        prompt = no
        
        [ req_dn ]
        countryName=US
        stateOrProvinceName=Unknown
        localityName=Unknown
        0.organizationName=${HOSTNAME}
        organizationalUnitName=root@${HOSTNAME}
        0.commonName=${HOSTNAME}
        
        [ cert_type ]
        nsCertType = server
        
        " >> ${CONF_FILE}

#########
## generate the key/certifcate
#########
rm -f ${PEM_FILE} ${LOG_FILE}
/usr/bin/openssl req -new -x509 -days 365 -nodes -config ${CONF_FILE} -out ${PEM_FILE} -keyout ${PEM_FILE} > ${LOG_FILE} 2>&1
RETVAL=$?
if [ ${RETVAL} -ne 0 ];
then
    echo "Failed to generate key/certificate; openssl failed: " >&2
    cat ${LOG_FILE} >&2
    exit 1
fi

#########
## save the key/certificate file
#########
if [ ${SAVE_AS_DEFAULT_KEY} -eq 1 ];
then
    rm -rf ${SAVE_PEM_FILE}
    install -D -m 600 ${PEM_FILE} ${SAVE_PEM_FILE}
else
    cat ${PEM_FILE}
fi

#########
## perform cleanup
#########
Cleanup

exit 0
