#!/bin/bash
# genrsa -> generates an RSA private key.
#        -out the output file name
#        -passout the output file password source; password is the seed for the key
#        -aes256 cipher
#        numbits
# rsa -> rsa key processing tool
#     -passin the input file password source; as the key was generated with a seed
#     -in filename to read the key from; if encrypted the pass phrase will be prompted
#     -out filename  to write the new key to
#     no cipher this means that using the rsa utility to read in an encrypted key with no encryption option can be used to
#         remove the pass phrase from a key
# req -> PKCS#10 certificate request and certificate generating utility.
#     -new    creates a certificate request
#     -sha256 digest
#     -key    using our server key
#     -out    new certificate
# x509 -> certificate display and signing utility
#      -req by default a certificate is expected on input. With this option a certificate request is expected instead.
#      -sha256 digest
#      -days specifies the number of days to certificate for
#      -in the certificate request to sign
#      -signkey the key to sign the certificate with
#      -out the new certificate

MYSELF=$(basename "$0")

script_help() {
    cat <<EOF
${MYSELF} help:

This script creates a self-signed certificate. It needs root privileges!

Usage: ${MYSELF} [ <DOMAIN> ]

where:
    DOMAIN ... FQDN without the hostname itself
               Default: recordum.net

EOF
}

if [[ $UID -gt 0 ]]; then
    echo "$MYSELF needs to be run as superuser. You are $USER."
    exit 1
fi

# get serial number / product / hostname of station
SERIAL=$(cat /usr/local/recordum/SERIAL_CODE) || exit 3
PRODUCT=$(cat /usr/local/recordum/PRODUCT_CODE) || exit 4
SERIAL_LC=$(echo "${SERIAL}" | tr "[:upper:]" "[:lower:]")
PRODUCT_LC=$(echo "${PRODUCT}" | tr "[:upper:]" "[:lower:]")
DEFAULT_DOMAIN="recordum.net"
DOMAIN=$(hostname --domain)
if [[ $# -gt 0 ]]; then
    DOMAIN=$1
fi
if [[ -z "$DOMAIN" ]]; then
    DOMAIN=${DEFAULT_DOMAIN}
fi
TARGET_NAME="${PRODUCT_LC}-${SERIAL_LC}"
COMMON_NAME="${TARGET_NAME}.${DOMAIN}"
SUBJECT="/C=AT/ST=Lower Austria/L=Wiener Neustadt/O=mlu-recordum/OU=IT/CN=${COMMON_NAME}"
KEY_STORE=/etc/ssl/private/

# create a product-serial private key, once
if [ ! -f "${KEY_STORE}${TARGET_NAME}.key" ]; then
    /usr/bin/openssl genrsa -aes256 -passout pass:mluRecXL123net -out "${TARGET_NAME}".signed.key 2048
    /usr/bin/openssl rsa -passin pass:mluRecXL123net -in "${TARGET_NAME}".signed.key -out "${TARGET_NAME}".key
    rm "${TARGET_NAME}".signed.key
    # set the system wanted system access rights, to consider a key valid and secure
    chmod og-r "${TARGET_NAME}".key
    chmod u-w "${TARGET_NAME}".key
    mv "${TARGET_NAME}".key ${KEY_STORE}
    # ensure that we also have a public key for signature usage
    /usr/bin/openssl rsa -in "${KEY_STORE}${TARGET_NAME}".key -pubout -out "${KEY_STORE}${TARGET_NAME}-public".key
fi

# generate our new private key
/usr/bin/openssl genrsa -aes256 -passout pass:recXL123 -out signed.key 2048
/usr/bin/openssl rsa -passin pass:recXL123 -in signed.key -out server.key
rm signed.key
# now create the self-signed certificate
/usr/bin/openssl req -new -sha256 -key server.key -out server.csr -subj "${SUBJECT}"
/usr/bin/openssl x509 -req -sha256 -days 3650 -in server.csr -signkey server.key -out server.crt
rm server.csr

# make them secure
chmod og-r server.*
chmod u-w server.*
# and move them to the final resting place
mv server.crt /etc/ssl/certs/
mv server.key ${KEY_STORE}

# ensure that we have a public key for our signed backups
/usr/bin/openssl rsa -in ${KEY_STORE}server.key -pubout -out ${KEY_STORE}pubserver.key

# hostname and certificate common_name need to be the same
/usr/local/bin/recordum_hostname "${TARGET_NAME}" ${DOMAIN}

echo "Self Signed SSL certificate renewal done."

exit 0
