Dictate certdir to sign_domain by command_sign_domains

Now the first parameter is the certificate output directory. This is
dictated by command_sign_domains as a single point of authority.
Also in sign_domain and command_sign_domains, the variables have been
rephrased to idicate that the first domain is only the primary, which
is put into the CN field, being part of alldomains. The primary still
indicates the configuration file in DOMAINS_D and the output directory.
This commit is contained in:
typingArtist
2017-06-08 08:32:34 +02:00
parent cd03e2a8d7
commit 3116fe0c8c

View File

@@ -678,8 +678,10 @@ walk_chain() {
# Create certificate for domain(s) # Create certificate for domain(s)
sign_domain() { sign_domain() {
domain="${1}" local certdir="${1}"
altnames="${*}" shift
local primary="${1}"
local alldomains="${*}"
timestamp="$(date +%s)" timestamp="$(date +%s)"
echo " + Signing domains..." echo " + Signing domains..."
@@ -687,8 +689,6 @@ sign_domain() {
_exiterr "Certificate authority doesn't allow certificate signing" _exiterr "Certificate authority doesn't allow certificate signing"
fi fi
local certdir="${CERTDIR}/${domain}"
# If there is no existing certificate directory => make it # If there is no existing certificate directory => make it
if [[ ! -e "${certdir}" ]]; then if [[ ! -e "${certdir}" ]]; then
echo " + Creating new directory ${certdir} ..." echo " + Creating new directory ${certdir} ..."
@@ -729,7 +729,7 @@ sign_domain() {
# Generate signing request config and the actual signing request # Generate signing request config and the actual signing request
echo " + Generating signing request..." echo " + Generating signing request..."
SAN="" SAN=""
for altname in ${altnames}; do for altname in ${alldomains}; do
SAN+="DNS:${altname}, " SAN+="DNS:${altname}, "
done done
SAN="${SAN%%, }" SAN="${SAN%%, }"
@@ -740,12 +740,12 @@ sign_domain() {
if [ "${OCSP_MUST_STAPLE}" = "yes" ]; then if [ "${OCSP_MUST_STAPLE}" = "yes" ]; then
printf "\n1.3.6.1.5.5.7.1.24=DER:30:03:02:01:05" >> "${tmp_openssl_cnf}" printf "\n1.3.6.1.5.5.7.1.24=DER:30:03:02:01:05" >> "${tmp_openssl_cnf}"
fi fi
openssl req -new -sha256 -key "${certdir}/${privkey}" -out "${certdir}/cert-${timestamp}.csr" -subj "/CN=${domain}/" -reqexts SAN -config "${tmp_openssl_cnf}" openssl req -new -sha256 -key "${certdir}/${privkey}" -out "${certdir}/cert-${timestamp}.csr" -subj "/CN=${primary}/" -reqexts SAN -config "${tmp_openssl_cnf}"
rm -f "${tmp_openssl_cnf}" rm -f "${tmp_openssl_cnf}"
crt_path="${certdir}/cert-${timestamp}.pem" crt_path="${certdir}/cert-${timestamp}.pem"
# shellcheck disable=SC2086 # shellcheck disable=SC2086
sign_csr "$(< "${certdir}/cert-${timestamp}.csr" )" ${altnames} 3>"${crt_path}" sign_csr "$(< "${certdir}/cert-${timestamp}.csr" )" ${alldomains} 3>"${crt_path}"
# Create fullchain.pem # Create fullchain.pem
echo " + Creating fullchain.pem..." echo " + Creating fullchain.pem..."
@@ -798,19 +798,20 @@ command_sign_domains() {
for line in $(<"${DOMAINS_TXT}" tr -d '\r' | awk '{print tolower($0)}' | _sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*$//g' -e 's/[[:space:]]+/ /g' | (grep -vE '^(#|$)' || true)); do for line in $(<"${DOMAINS_TXT}" tr -d '\r' | awk '{print tolower($0)}' | _sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*$//g' -e 's/[[:space:]]+/ /g' | (grep -vE '^(#|$)' || true)); do
reset_configvars reset_configvars
IFS="${ORIGIFS}" IFS="${ORIGIFS}"
domain="$(printf '%s\n' "${line}" | cut -d' ' -f1)" primary="$(printf '%s\n' "${line}" | cut -d' ' -f1)"
morenames="$(printf '%s\n' "${line}" | cut -s -d' ' -f2-)" morenames="$(printf '%s\n' "${line}" | cut -s -d' ' -f2-)"
alldomains="${primary} ${morenames}"
local certdir="${CERTDIR}/${domain}" local certdir="${CERTDIR}/${primary}"
cert="${certdir}/cert.pem" cert="${certdir}/cert.pem"
force_renew="${PARAM_FORCE:-no}" force_renew="${PARAM_FORCE:-no}"
if [[ -z "${morenames}" ]];then if [[ -z "${morenames}" ]];then
echo "Processing ${domain}" echo "Processing ${primary}"
else else
echo "Processing ${domain} with alternative names: ${morenames}" echo "Processing ${primary} with alternative names: ${morenames}"
fi fi
# read cert config # read cert config
@@ -818,7 +819,7 @@ command_sign_domains() {
# we could just source the config file but i decided to go this way to protect people from accidentally overriding # we could just source the config file but i decided to go this way to protect people from accidentally overriding
# variables used internally by this script itself. # variables used internally by this script itself.
if [[ -n "${DOMAINS_D}" ]]; then if [[ -n "${DOMAINS_D}" ]]; then
certconfig="${DOMAINS_D}/${domain}" certconfig="${DOMAINS_D}/${primary}"
else else
certconfig="${certdir}/config" certconfig="${certdir}/config"
fi fi
@@ -858,7 +859,7 @@ command_sign_domains() {
printf " + Checking domain name(s) of existing cert..." printf " + Checking domain name(s) of existing cert..."
certnames="$(openssl x509 -in "${cert}" -text -noout | grep DNS: | _sed 's/DNS://g' | tr -d ' ' | tr ',' '\n' | sort -u | tr '\n' ' ' | _sed 's/ $//')" certnames="$(openssl x509 -in "${cert}" -text -noout | grep DNS: | _sed 's/DNS://g' | tr -d ' ' | tr ',' '\n' | sort -u | tr '\n' ' ' | _sed 's/ $//')"
givennames="$(echo "${domain}" "${morenames}"| tr ' ' '\n' | sort -u | tr '\n' ' ' | _sed 's/ $//' | _sed 's/^ //')" givennames="$(echo "${alldomains}"| tr ' ' '\n' | sort -u | tr '\n' ' ' | _sed 's/ $//' | _sed 's/^ //')"
if [[ "${certnames}" = "${givennames}" ]]; then if [[ "${certnames}" = "${givennames}" ]]; then
echo " unchanged." echo " unchanged."
@@ -884,7 +885,7 @@ command_sign_domains() {
else else
# Certificate-Names unchanged and cert is still valid # Certificate-Names unchanged and cert is still valid
echo "Skipping renew!" echo "Skipping renew!"
[[ -n "${HOOK}" ]] && "${HOOK}" "unchanged_cert" "${domain}" "${certdir}/privkey.pem" "${certdir}/cert.pem" "${certdir}/fullchain.pem" "${certdir}/chain.pem" [[ -n "${HOOK}" ]] && "${HOOK}" "unchanged_cert" "${primary}" "${certdir}/privkey.pem" "${certdir}/cert.pem" "${certdir}/fullchain.pem" "${certdir}/chain.pem"
continue continue
fi fi
else else
@@ -894,10 +895,10 @@ command_sign_domains() {
# shellcheck disable=SC2086 # shellcheck disable=SC2086
if [[ "${PARAM_KEEP_GOING:-}" = "yes" ]]; then if [[ "${PARAM_KEEP_GOING:-}" = "yes" ]]; then
sign_domain ${line} & sign_domain "${certdir}" ${alldomains} &
wait $! || true wait $! || true
else else
sign_domain ${line} sign_domain "${certdir}" ${alldomains}
fi fi
done done