]> git.street.me.uk Git - andy/dehydrated.git/blobdiff - letsencrypt.sh
renamed _request method to http_request
[andy/dehydrated.git] / letsencrypt.sh
index cc000d15f87b4033c007547dc67b2b22155a720f..b7ddbb26d25c0a9b7b12e5df54311f6c4b3a2171 100755 (executable)
 set -e
 set -u
 set -o pipefail
+umask 077 # paranoid umask, we're creating private keys
+
+# Get the directory in which this script is stored
+SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 
-# default config values
-CA="https://acme-v01.api.letsencrypt.org"
+# directory for config, private key and certificates
+BASEDIR="${SCRIPTDIR}"
+
+# Default config values
+CA="https://acme-v01.api.letsencrypt.org/directory"
 LICENSE="https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf"
-HOOK_CHALLENGE=
-RENEW_DAYS="14"
+HOOK=
+RENEW_DAYS="30"
+PRIVATE_KEY=
 KEYSIZE="4096"
-WELLKNOWN=".acme-challenges"
-PRIVATE_KEY_RENEW=no
+WELLKNOWN=
+PRIVATE_KEY_RENEW="no"
+OPENSSL_CNF="$(openssl version -d | cut -d'"' -f2)/openssl.cnf"
+CONTACT_EMAIL=
+
+set_defaults() {
+  # Default config variables depending on BASEDIR
+  if [[ -z "${PRIVATE_KEY}" ]]; then
+    PRIVATE_KEY="${BASEDIR}/private_key.pem"
+  fi
+  if [[ -z "${WELLKNOWN}" ]]; then
+    WELLKNOWN="${BASEDIR}/.acme-challenges"
+  fi
 
-if [[ -e "config.sh" ]]; then
-  . ./config.sh
-fi
+  LOCKFILE="${BASEDIR}/lock"
+}
 
-umask 077 # paranoid umask, we're creating private keys
+init_system() {
+  # Check for config in various locations
+  if [[ -z "${CONFIG:-}" ]]; then
+    for check_config in "${HOME}/.letsencrypt.sh" "/etc/letsencrypt.sh" "/usr/local/etc/letsencrypt.sh" "${PWD}" "${SCRIPTDIR}"; do
+      if [[ -e "${check_config}/config.sh" ]]; then
+        BASEDIR="${check_config}"
+        CONFIG="${check_config}/config.sh"
+        break
+      fi
+    done
+  fi
+
+  if [[ -z "${CONFIG:-}" ]]; then
+    echo "WARNING: No config file found, using default config!" >&2
+    sleep 2
+  elif [[ -e "${CONFIG}" ]]; then
+    if [[ ! "${COMMAND}" = "env" ]]; then
+      echo "Using config file ${CONFIG}"
+    fi
+    BASEDIR="$(dirname "${CONFIG}")"
+    # shellcheck disable=SC1090
+    . "${CONFIG}"
+  else
+    echo "ERROR: Specified config file doesn't exist." >&2
+    exit 1
+  fi
+
+  # Remove slash from end of BASEDIR. Mostly for cleaner outputs, doesn't change functionality.
+  BASEDIR="${BASEDIR%%/}"
+
+  # Check BASEDIR and set default variables
+  if [[ ! -d "${BASEDIR}" ]]; then
+    echo "ERROR: BASEDIR does not exist: ${BASEDIR}" >&2
+    exit 1
+  fi
+  set_defaults
+
+  if [[ "${COMMAND}" = "env" ]]; then
+    return
+  fi
+
+  # Lockfile handling (prevents concurrent access)
+  set -o noclobber
+  if ! { date > "${LOCKFILE}"; } 2>/dev/null; then
+    echo "  + ERROR: Lock file '${LOCKFILE}' present, aborting." >&2
+    LOCKFILE="" # so remove_lock doesn't remove it
+    exit 1
+  fi
+  set +o noclobber
+
+  remove_lock() {
+    if [[ -n "${LOCKFILE}" ]]; then
+        rm -f "${LOCKFILE}"
+    fi
+  }
+  trap 'remove_lock' EXIT
+
+  # Export some environment variables to be used in hook script
+  export WELLKNOWN
+  export BASEDIR
+  export CONFIG
+
+  # Get CA URLs
+  CA_DIRECTORY="$(http_request get "${CA}")"
+  CA_NEW_CERT="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value new-cert)" &&
+  CA_NEW_AUTHZ="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value new-authz)" &&
+  CA_NEW_REG="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value new-reg)" &&
+  # shellcheck disable=SC2015
+  CA_REVOKE_CERT="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value revoke-cert)" ||
+  (echo "Error retrieving ACME/CA-URLs, check if your configured CA points to the directory entrypoint." >&2; exit 1)
+
+
+  # check private key ...
+  register="0"
+  if [[ -n "${PARAM_PRIVATE_KEY:-}" ]]; then
+    # a private key was specified from the command line so use it for this run
+    echo "Using private key ${PARAM_PRIVATE_KEY} instead of account key"
+    PRIVATE_KEY="${PARAM_PRIVATE_KEY}"
+    if ! openssl rsa -in "${PRIVATE_KEY}" -check 2>/dev/null > /dev/null; then
+      echo " + ERROR: private key is not valid, can not continue" >&2
+      exit 1
+    fi
+  else
+    # Check if private account key exists, if it doesn't exist yet generate a new one (rsa key)
+    if [[ ! -e "${PRIVATE_KEY}" ]]; then
+      echo "+ Generating account key..."
+      _openssl genrsa -out "${PRIVATE_KEY}" "${KEYSIZE}"
+      register="1"
+    fi
+  fi
+
+  # Get public components from private key and calculate thumbprint
+  pubExponent64="$(openssl rsa -in "${PRIVATE_KEY}" -noout -text | grep publicExponent | grep -oE "0x[a-f0-9]+" | cut -d'x' -f2 | hex2bin | urlbase64)"
+  pubMod64="$(openssl rsa -in "${PRIVATE_KEY}" -noout -modulus | cut -d'=' -f2 | hex2bin | urlbase64)"
+
+  thumbprint="$(printf '{"e":"%s","kty":"RSA","n":"%s"}' "${pubExponent64}" "${pubMod64}" | openssl sha -sha256 -binary | urlbase64)"
+
+  # If we generated a new private key in the step above we have to register it with the acme-server
+  if [[ "${register}" = "1" ]]; then
+    echo "+ Registering account key with letsencrypt..."
+    if [ -z "${CA_NEW_REG}" ]; then
+      echo " + ERROR: Certificate authority doesn't allow registrations." >&2
+      exit 1
+    fi
+    # if an email for the contact has been provided then adding it to the registration request
+    if [[ -n "${CONTACT_EMAIL}" ]]; then
+      signed_request "${CA_NEW_REG}" '{"resource": "new-reg", "contact":["mailto:'"${CONTACT_EMAIL}"'"], "agreement": "'"$LICENSE"'"}' > /dev/null
+    else
+      signed_request "${CA_NEW_REG}" '{"resource": "new-reg", "agreement": "'"$LICENSE"'"}' > /dev/null
+    fi
+  fi
+
+  if [[ -e "${BASEDIR}/domains.txt" ]]; then
+    DOMAINS_TXT="${BASEDIR}/domains.txt"
+  else
+    echo " + ERROR: domains.txt not found" >&2
+    exit 1
+  fi
+
+  if [[ ! -e "${WELLKNOWN}" ]]; then
+    echo " + ERROR: WELLKNOWN directory doesn't exist, please create ${WELLKNOWN} and set appropriate permissions." >&2
+    exit 1
+  fi
+}
 
 anti_newline() {
   tr -d '\n\r'
@@ -25,7 +166,7 @@ anti_newline() {
 
 urlbase64() {
   # urlbase64: base64 encoded string with '+' replaced with '-' and '/' replaced with '_'
-  openssl base64 -e | anti_newline | sed -r 's/=*$//g' | tr '+/' '-_'
+  openssl base64 -e | anti_newline | sed 's/=*$//g' | tr '+/' '-_'
 }
 
 hex2bin() {
@@ -48,26 +189,66 @@ hex2bin() {
   done
 
   # Convert to binary data
-  printf "${escapedhex}"
+  printf -- "${escapedhex}"
+}
+
+get_json_string_value() {
+  grep -Eo '"'"${1}"'":[[:space:]]*"[^"]*"' | cut -d'"' -f4
 }
 
-_request() {
-  temperr="$(mktemp)"
+get_json_array() {
+  grep -Eo '"'"${1}"'":[^\[]*\[[^]]*]'
+}
+
+http_request() {
+  tempcont="$(mktemp)"
+
   if [[ "${1}" = "head" ]]; then
-    curl -sSf -I "${2}" 2> "${temperr}"
+    statuscode="$(curl -s -w "%{http_code}" -o "${tempcont}" "${2}" -I)"
   elif [[ "${1}" = "get" ]]; then
-    curl -sSf "${2}" 2> "${temperr}"
+    statuscode="$(curl -s -w "%{http_code}" -o "${tempcont}" "${2}")"
   elif [[ "${1}" = "post" ]]; then
-    curl -sSf "${2}" -d "${3}" 2> "${temperr}"
+    statuscode="$(curl -s -w "%{http_code}" -o "${tempcont}" "${2}" -d "${3}")"
   fi
 
-  if [[ -s "${temperr}" ]]; then
-    echo "  + ERROR: An error occured while sending ${1}-request to ${2} ($(<"${temperr}"))" >&2
-    rm -f "${temperr}"
+  if [[ ! "${statuscode:0:1}" = "2" ]]; then
+    echo "  + ERROR: An error occurred while sending ${1}-request to ${2} (Status ${statuscode})" >&2
+    echo >&2
+    echo "Details:" >&2
+    cat "${tempcont}" >&2
+    rm -f "${tempcont}"
+
+    # Wait for hook script to clean the challenge if used
+    if [[ -n "${HOOK}" ]] && [[ -n "${challenge_token:+set}"  ]]; then
+      ${HOOK} "clean_challenge" '' "${challenge_token}" "${keyauth}"
+    fi
+
+    # remove temporary domains.txt file if used
+    if [[ -n "${PARAM_DOMAIN:-}" ]]; then
+      rm "${DOMAINS_TXT}"
+    fi
+
     exit 1
   fi
 
-  rm -f "${temperr}"
+  cat "${tempcont}"
+  rm -f "${tempcont}"
+}
+
+# OpenSSL writes to stderr/stdout even when there are no errors. So just
+# display the output if the exit code was != 0 to simplify debugging.
+_openssl() {
+  set +e
+  out="$(openssl "${@}" 2>&1)"
+  res=$?
+  set -e
+  if [[ $res -ne 0 ]]; then
+    echo "  + ERROR: failed to run $* (Exitcode: $res)" >&2
+    echo >&2
+    echo "Details:" >&2
+    echo "$out" >&2
+    exit $res
+  fi
 }
 
 signed_request() {
@@ -75,7 +256,7 @@ signed_request() {
   payload64="$(printf '%s' "${2}" | urlbase64)"
 
   # Retrieve nonce from acme-server
-  nonce="$(_request head "${CA}/directory" | grep Replay-Nonce: | awk -F ': ' '{print $2}' | anti_newline)"
+  nonce="$(http_request head "${CA}" | grep Replay-Nonce: | awk -F ': ' '{print $2}' | anti_newline)"
 
   # Build header with just our public key and algorithm information
   header='{"alg": "RS256", "jwk": {"e": "'"${pubExponent64}"'", "kty": "RSA", "n": "'"${pubMod64}"'"}}'
@@ -85,32 +266,37 @@ signed_request() {
   protected64="$(printf '%s' "${protected}" | urlbase64)"
 
   # Sign header with nonce and our payload with our private key and encode signature as urlbase64
-  signed64="$(printf '%s' "${protected64}.${payload64}" | openssl dgst -sha256 -sign private_key.pem | urlbase64)"
+  signed64="$(printf '%s' "${protected64}.${payload64}" | openssl dgst -sha256 -sign "${PRIVATE_KEY}" | urlbase64)"
 
   # Send header + extended header + payload + signature to the acme-server
   data='{"header": '"${header}"', "protected": "'"${protected64}"'", "payload": "'"${payload64}"'", "signature": "'"${signed64}"'"}'
 
-  _request post "${1}" "${data}"
+  http_request post "${1}" "${data}"
 }
 
 sign_domain() {
   domain="${1}"
   altnames="${*}"
-  echo "Signing domain ${1} (${*})..."
+
+  echo " + Signing domains..."
+  if [[ -z "${CA_NEW_AUTHZ}" ]] || [[ -z "${CA_NEW_CERT}" ]]; then
+    echo " + ERROR: Certificate authority doesn't allow certificate signing" >&2
+    exit 1
+  fi
+  timestamp="$(date +%s)"
 
   # If there is no existing certificate directory => make it
-  if [[ ! -e "certs/${domain}" ]]; then
-    echo "  + make directory certs/${domain} ..."
-    mkdir -p "certs/${domain}"
+  if [[ ! -e "${BASEDIR}/certs/${domain}" ]]; then
+    echo " + make directory ${BASEDIR}/certs/${domain} ..."
+    mkdir -p "${BASEDIR}/certs/${domain}"
   fi
 
+  privkey="privkey.pem"
   # generate a new private key if we need or want one
-  if [[ ! -f "certs/${domain}/privkey.pem" ]] || [[ "${PRIVATE_KEY_RENEW}" = "yes" ]]; then
-    echo "  + Generating private key..."
-    timestamp="$(date +%s)"
-    openssl genrsa -out "certs/${domain}/privkey-${timestamp}.pem" "${KEYSIZE}" 2> /dev/null > /dev/null
-    rm -f "certs/${domain}/privkey.pem"
-    ln -s "privkey-${timestamp}.pem" "certs/${domain}/privkey.pem"
+  if [[ ! -f "${BASEDIR}/certs/${domain}/privkey.pem" ]] || [[ "${PRIVATE_KEY_RENEW}" = "yes" ]]; then
+    echo " + Generating private key..."
+    privkey="privkey-${timestamp}.pem"
+    _openssl genrsa -out "${BASEDIR}/certs/${domain}/privkey-${timestamp}.pem" "${KEYSIZE}"
   fi
 
   # Generate signing request config and the actual signing request
@@ -118,21 +304,29 @@ sign_domain() {
   for altname in $altnames; do
     SAN+="DNS:${altname}, "
   done
-  SAN="$(printf '%s' "${SAN}" | sed 's/,\s*$//g')"
-  echo "  + Generating signing request..."
-  openssl req -new -sha256 -key "certs/${domain}/privkey.pem" -out "certs/${domain}/cert.csr" -subj "/CN=${domain}/" -reqexts SAN -config <(cat /etc/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName=%s" "${SAN}")) > /dev/null
+  SAN="${SAN%%, }"
+  echo " + Generating signing request..."
+  local tmp_openssl_cnf
+  tmp_openssl_cnf="$(mktemp)"
+  cat "${OPENSSL_CNF}" > "${tmp_openssl_cnf}"
+  printf "[SAN]\nsubjectAltName=%s" "${SAN}" >> "${tmp_openssl_cnf}"
+  openssl req -new -sha256 -key "${BASEDIR}/certs/${domain}/${privkey}" -out "${BASEDIR}/certs/${domain}/cert-${timestamp}.csr" -subj "/CN=${domain}/" -reqexts SAN -config "${tmp_openssl_cnf}"
+  rm -f "${tmp_openssl_cnf}"
 
   # Request and respond to challenges
   for altname in $altnames; do
     # Ask the acme-server for new challenge token and extract them from the resulting json block
-    echo "  + Requesting challenge for ${altname}..."
-    response="$(signed_request "${CA}/acme/new-authz" '{"resource": "new-authz", "identifier": {"type": "dns", "value": "'"${altname}"'"}}')"
+    echo " + Requesting challenge for ${altname}..."
+    response="$(signed_request "${CA_NEW_AUTHZ}" '{"resource": "new-authz", "identifier": {"type": "dns", "value": "'"${altname}"'"}}')"
 
-    challenge_token="$(printf '%s\n' "${response}" | grep -Eo '"challenges":[^\[]*\[[^]]*]' | sed 's/{/\n{/g' | grep 'http-01' | grep -Eo '"token":\s*"[^"]*"' | cut -d'"' -f4 | sed 's/[^A-Za-z0-9_\-]/_/g')"
-    challenge_uri="$(printf '%s\n' "${response}" | grep -Eo '"challenges":[^\[]*\[[^]]*]' | sed 's/{/\n{/g' | grep 'http-01' | grep -Eo '"uri":\s*"[^"]*"' | cut -d'"' -f4)"
+    challenges="$(printf '%s\n' "${response}" | get_json_array challenges)"
+    repl=$'\n''{' # fix syntax highlighting in Vim
+    challenge="$(printf "%s" "${challenges//\{/${repl}}" | grep 'http-01')"
+    challenge_token="$(printf '%s' "${challenge}" | get_json_string_value token | sed 's/[^A-Za-z0-9_\-]/_/g')"
+    challenge_uri="$(printf '%s' "${challenge}" | get_json_string_value uri)"
 
     if [[ -z "${challenge_token}" ]] || [[ -z "${challenge_uri}" ]]; then
-      echo "  + Error: Can't retrieve challenges (${response})"
+      echo "  + Error: Can't retrieve challenges (${response})" >&2
       exit 1
     fi
 
@@ -144,83 +338,329 @@ sign_domain() {
     chmod a+r "${WELLKNOWN}/${challenge_token}"
 
     # Wait for hook script to deploy the challenge if used
-    if [ -n "${HOOK_CHALLENGE}" ]; then
-        ${HOOK_CHALLENGE} "${WELLKNOWN}/${challenge_token}" "${keyauth}"
+    if [[ -n "${HOOK}" ]]; then
+        ${HOOK} "deploy_challenge" "${altname}" "${challenge_token}" "${keyauth}"
     fi
 
     # Ask the acme-server to verify our challenge and wait until it becomes valid
-    echo "  + Responding to challenge for ${altname}..."
+    echo " + Responding to challenge for ${altname}..."
     result="$(signed_request "${challenge_uri}" '{"resource": "challenge", "keyAuthorization": "'"${keyauth}"'"}')"
 
-    status="$(printf '%s\n' "${result}" | grep -Eo '"status":\s*"[^"]*"' | cut -d'"' -f4)"
+    status="$(printf '%s\n' "${result}" | get_json_string_value status)"
 
-    # get status until it a result is reached => not pending anymore    
+    # get status until a result is reached => not pending anymore
     while [[ "${status}" = "pending" ]]; do
       sleep 1
-      status="$(_request get "${challenge_uri}" | grep -Eo '"status":\s*"[^"]*"' | cut -d'"' -f4)"
+      status="$(http_request get "${challenge_uri}" | get_json_string_value status)"
     done
 
+    rm -f "${WELLKNOWN}/${challenge_token}"
+
+    # Wait for hook script to clean the challenge if used
+    if [[ -n "${HOOK}" ]] && [[ -n "${challenge_token}" ]]; then
+      ${HOOK} "clean_challenge" "${altname}" "${challenge_token}" "${keyauth}"
+    fi
+
     if [[ "${status}" = "valid" ]]; then
-      echo "  + Challenge is valid!"
+      echo " + Challenge is valid!"
     else
-      echo "  + Challenge is invalid! (returned: ${status})"
+      echo " + Challenge is invalid! (returned: ${status})" >&2
       exit 1
     fi
 
   done
 
   # Finally request certificate from the acme-server and store it in cert-${timestamp}.pem and link from cert.pem
-  echo "  + Requesting certificate..."
-  timestamp="$(date +%s)"
-  csr64="$(openssl req -in "certs/${domain}/cert.csr" -outform DER | urlbase64)"
-  crt64="$(signed_request "${CA}/acme/new-cert" '{"resource": "new-cert", "csr": "'"${csr64}"'"}' | openssl base64 -e)"
-  printf -- '-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----\n' "${crt64}" > "certs/${domain}/cert-${timestamp}.pem"
-  rm -f "certs/${domain}/cert.pem"
-  ln -s "cert-${timestamp}.pem" "certs/${domain}/cert.pem"
-  echo "  + Done!"
-}
-
-# Check if private key exists, if it doesn't exist yet generate a new one (rsa key)
-register="0"
-if [[ ! -e "private_key.pem" ]]; then
-  echo "+ Generating account key..."
-  openssl genrsa -out "private_key.pem" "${KEYSIZE}" 2> /dev/null > /dev/null
-  register="1"
-fi
+  echo " + Requesting certificate..."
+  csr64="$(openssl req -in "${BASEDIR}/certs/${domain}/cert-${timestamp}.csr" -outform DER | urlbase64)"
+  crt64="$(signed_request "${CA_NEW_CERT}" '{"resource": "new-cert", "csr": "'"${csr64}"'"}' | openssl base64 -e)"
+  crt_path="${BASEDIR}/certs/${domain}/cert-${timestamp}.pem"
+  printf -- '-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----\n' "${crt64}" > "${crt_path}"
+  # try to load the certificate to detect corruption
+  echo " + Checking certificate..."
+  _openssl x509 -text < "${crt_path}"
+
+  # Create fullchain.pem
+  echo " + Creating fullchain.pem..."
+  cat "${crt_path}" > "${BASEDIR}/certs/${domain}/fullchain-${timestamp}.pem"
+  http_request get "$(openssl x509 -in "${BASEDIR}/certs/${domain}/cert-${timestamp}.pem" -noout -text | grep 'CA Issuers - URI:' | cut -d':' -f2-)" > "${BASEDIR}/certs/${domain}/chain-${timestamp}.pem"
+  if ! grep "BEGIN CERTIFICATE" "${BASEDIR}/certs/${domain}/chain-${timestamp}.pem" > /dev/null 2>&1; then
+    openssl x509 -in "${BASEDIR}/certs/${domain}/chain-${timestamp}.pem" -inform DER -out "${BASEDIR}/certs/${domain}/chain-${timestamp}.pem" -outform PEM
+  fi
+  ln -sf "chain-${timestamp}.pem" "${BASEDIR}/certs/${domain}/chain.pem"
+  cat "${BASEDIR}/certs/${domain}/chain-${timestamp}.pem" >> "${BASEDIR}/certs/${domain}/fullchain-${timestamp}.pem"
+  ln -sf "fullchain-${timestamp}.pem" "${BASEDIR}/certs/${domain}/fullchain.pem"
 
-# Get public components from private key and calculate thumbprint
-pubExponent64="$(printf "%06x" "$(openssl rsa -in private_key.pem -noout -text | grep publicExponent | head -1 | cut -d' ' -f2)" | hex2bin | urlbase64)"
-pubMod64="$(printf '%s' "$(openssl rsa -in private_key.pem -noout -modulus | cut -d'=' -f2)" | hex2bin | urlbase64)"
+  # Update remaining symlinks
+  if [ ! "${privkey}" = "privkey.pem" ]; then
+    ln -sf "privkey-${timestamp}.pem" "${BASEDIR}/certs/${domain}/privkey.pem"
+  fi
 
-thumbprint="$(printf '%s' "$(printf '%s' '{"e":"'"${pubExponent64}"'","kty":"RSA","n":"'"${pubMod64}"'"}' | shasum -a 256 | awk '{print $1}')" | hex2bin | urlbase64)"
+  ln -sf "cert-${timestamp}.csr" "${BASEDIR}/certs/${domain}/cert.csr"
+  ln -sf "cert-${timestamp}.pem" "${BASEDIR}/certs/${domain}/cert.pem"
 
-# If we generated a new private key in the step above we have to register it with the acme-server
-if [[ "${register}" = "1" ]]; then
-  echo "+ Registering account key with letsencrypt..."
-  signed_request "${CA}/acme/new-reg" '{"resource": "new-reg", "agreement": "'"$LICENSE"'"}' > /dev/null
-fi
+  # Wait for hook script to clean the challenge and to deploy cert if used
+  if [[ -n "${HOOK}" ]]; then
+      ${HOOK} "deploy_cert" "${domain}" "${BASEDIR}/certs/${domain}/privkey.pem" "${BASEDIR}/certs/${domain}/cert.pem" "${BASEDIR}/certs/${domain}/fullchain.pem"
+  fi
 
-if [[ ! -e "domains.txt" ]]; then
-  echo "You have to create a domains.txt file listing the domains you want certificates for. Have a look at domains.txt.example."
-  exit 1
-fi
+  unset challenge_token
+  echo " + Done!"
+}
 
-if [[ ! -e "${WELLKNOWN}" ]]; then
-  mkdir -p "${WELLKNOWN}"
-fi
 
-# Generate certificates for all domains found in domain.txt. Check if existing certificate are about to expire
-<domains.txt sed 's/^\s*//g;s/\s*$//g' | grep -v '^#' | grep -v '^$' | while read -r line; do
-  domain="$(echo $line | cut -d' ' -f1)"
-  if [[ -e "certs/${domain}/cert.pem" ]]; then
-    echo -n "Found existing cert for ${domain}. Expire date ..."
-    set +e; openssl x509 -checkend $((${RENEW_DAYS} * 86400)) -noout -in "certs/${domain}/cert.pem"; expiring=$?; set -e
-    if [[ ${expiring} -eq 0 ]]; then
-        echo " is not within ${RENEW_DAYS} days. Skipping"
-        continue
+# Usage: --cron (-c)
+# Description: Sign/renew non-existant/changed/expiring certificates.
+command_sign_domains() {
+  if [[ -n "${PARAM_DOMAIN:-}" ]]; then
+    # we are using a temporary domains.txt file so we don't need to duplicate any code
+    DOMAINS_TXT="$(mktemp)"
+    echo "${PARAM_DOMAIN}" > "${DOMAINS_TXT}"
+  fi
+  # Generate certificates for all domains found in domains.txt. Check if existing certificate are about to expire
+  <"${DOMAINS_TXT}" sed 's/^[[:space:]]*//g;s/[[:space:]]*$//g' | grep -vE '^(#|$)' | while read -r line; do
+    domain="$(printf '%s\n' "${line}" | cut -d' ' -f1)"
+    morenames="$(printf '%s\n' "${line}" | cut -s -d' ' -f2-)"
+    cert="${BASEDIR}/certs/${domain}/cert.pem"
+
+    force_renew="${PARAM_FORCE:-no}"
+
+    if [[ -z "${morenames}" ]];then
+      echo "Processing ${domain}"
+    else
+      echo "Processing ${domain} with SAN: ${morenames}"
+    fi
+
+    if [[ -e "${cert}" ]]; then
+      echo -n " + 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/ $//')"
+      givennames="$(echo "${domain}" "${morenames}"| tr ' ' '\n' | sort -u | tr '\n' ' ' | sed 's/ $//' | sed 's/^ //')"
+
+      if [[ "${certnames}" = "${givennames}" ]]; then
+        echo " unchanged."
+      else
+        echo " changed!"
+        echo " + Domain name(s) are not matching!"
+        echo " + Names in old certificate: ${certnames}"
+        echo " + Configured names: ${givennames}"
+        echo " + Forcing renew."
+        force_renew="yes"
+      fi
+    fi
+
+    if [[ -e "${cert}" ]]; then
+      echo " + Checking expire date of existing cert..."
+
+      valid="$(openssl x509 -enddate -noout -in "${cert}" | cut -d= -f2- )"
+
+      echo -n " + Valid till ${valid} "
+      if openssl x509 -checkend $((RENEW_DAYS * 86400)) -noout -in "${cert}"; then
+        echo -n "(Longer than ${RENEW_DAYS} days). "
+        if [[ "${force_renew}" = "yes" ]]; then
+          echo "Ignoring because renew was forced!"
+        else
+          echo "Skipping!"
+          continue
+        fi
+      else
+        echo "(Less than ${RENEW_DAYS} days). Renewing!"
+      fi
+    fi
+
+    # shellcheck disable=SC2086
+    sign_domain $line
+  done || true
+
+  # remove temporary domains.txt file if used
+  if [[ -n "${PARAM_DOMAIN:-}" ]]; then
+    rm -f "${DOMAINS_TXT}"
+  fi
+}
+
+# Usage: --revoke (-r) path/to/cert.pem
+# Description: Revoke specified certificate
+command_revoke() {
+  cert="${1}"
+  if [[ -L "${cert}" ]]; then
+    # follow symlink and use real certificate name (so we move the real file and not the symlink at the end)
+    local link_target
+    link_target="$(readlink -n "${cert}")"
+    if [[ "${link_target}" =~ ^/ ]]; then
+      cert="${link_target}"
+    else
+      cert="$(dirname "${cert}")/${link_target}"
+    fi
+  fi
+  if [[ ! -f "${cert}" ]]; then
+    echo "ERROR: Could not find certificate ${cert}"
+    exit 1
+  fi
+  echo "Revoking ${cert}"
+  if [[ -z "${CA_REVOKE_CERT}" ]]; then
+    echo " + ERROR: Certificate authority doesn't allow certificate revocation." >&2
+    exit 1
+  fi
+  cert64="$(openssl x509 -in "${cert}" -inform PEM -outform DER | urlbase64)"
+  response="$(signed_request "${CA_REVOKE_CERT}" '{"resource": "revoke-cert", "certificate": "'"${cert64}"'"}')"
+  # if there is a problem with our revoke request http_request (via signed_request) will report this and "exit 1" out
+  # so if we are here, it is safe to assume the request was successful
+  echo " + SUCCESS"
+  echo " + renaming certificate to ${cert}-revoked"
+  mv -f "${cert}" "${cert}-revoked"
+}
+
+# Usage: --help (-h)
+# Description: Show help text
+command_help() {
+  echo "Usage: ${0} [-h] [command [argument]] [parameter [argument]] [parameter [argument]] ..."
+  echo
+  echo "Default command: help"
+  echo
+  (
+  echo "Commands:"
+  grep -e '^[[:space:]]*# Usage:' -e '^[[:space:]]*# Description:' -e '^command_.*()[[:space:]]*{' "${0}" | while read -r usage; read -r description; read -r command; do
+    if [[ ! "${usage}" =~ Usage ]] || [[ ! "${description}" =~ Description ]] || [[ ! "${command}" =~ ^command_ ]]; then
+      echo "Error generating help text." >&2
+      exit 1
     fi
-    echo " is within ${RENEW_DAYS} days. Renewing..."
+    printf " %s\t%s\n" "${usage##"# Usage: "}" "${description##"# Description: "}"
+  done
+  echo "---"
+  echo "Parameters:"
+  grep -E -e '^[[:space:]]*# PARAM_Usage:' -e '^[[:space:]]*# PARAM_Description:' "${0}" | while read -r usage; read -r description; do
+    if [[ ! "${usage}" =~ Usage ]] || [[ ! "${description}" =~ Description ]]; then
+      echo "Error generating help text." >&2
+      exit 1
+    fi
+    printf " %s\t%s\n" "${usage##"# PARAM_Usage: "}" "${description##"# PARAM_Description: "}"
+  done
+  ) | column -t -s $'\t' | sed 's/^---$//g'
+}
+
+# Usage: --env (-e)
+# Description: Output configuration variables for use in other scripts
+command_env() {
+  echo "# letsencrypt.sh configuration"
+  typeset -p CONFIG
+  typeset -p CA LICENSE BASEDIR WELLKNOWN PRIVATE_KEY KEYSIZE OPENSSL_CNF HOOK RENEW_DAYS PRIVATE_KEY_RENEW CONTACT_EMAIL
+  exit 0
+}
+
+args=""
+# change long args to short args
+# inspired by http://kirk.webfinish.com/?p=45
+for arg; do
+  case "${arg}" in
+    --help)    args="${args}-h ";;
+    --cron)    args="${args}-c ";;
+    --domain)  args="${args}-d ";;
+    --force )  args="${args}-x ";;
+    --revoke)  args="${args}-r ";;
+    --privkey) args="${args}-p ";;
+    --config)  args="${args}-f ";;
+    --env)     args="${args}-e ";;
+    --*)
+      echo "Unknown parameter detected: ${arg}" >&2
+      echo >&2
+      command_help >&2
+      exit 1
+      ;;
+    # pass through anything else
+    *) args="${args}\"${arg}\" ";;
+    esac
+done
+
+# Reset the positional parameters to the short options
+eval set -- "${args}"
+
+COMMAND=""
+set_command() {
+  if [[ ! -z "${COMMAND}" ]]; then
+    echo "Only one command can be executed at a time." >&2
+    echo "See help (-h) for more information." >&2
+    exit 1
   fi
+  COMMAND="${1}"
+}
 
-  sign_domain $line
+check_parameters() {
+  if [[ -z "${@}" ]]; then
+    echo "The specified command requires additional parameters. See help:" >&2
+    echo >&2
+    command_help >&2
+    exit 1
+  fi
+}
+
+while getopts ":hcer:d:xf:p:" option; do
+  case "${option}" in
+    h)
+      command_help
+      exit 0
+      ;;
+    c)
+      set_command sign_domains
+      ;;
+    e)
+      set_command env
+      ;;
+    r)
+      set_command revoke
+      check_parameters "${OPTARG:-}"
+      revoke_me="${OPTARG}"
+      ;;
+    d)
+      # PARAM_Usage: --domain (-d) domain.tld
+      # PARAM_Description: Use specified domain name instead of domains.txt, use multiple times for certificate with SAN names
+      check_parameters "${OPTARG:-}"
+      if [[ -z "${PARAM_DOMAIN:-}" ]]; then
+        PARAM_DOMAIN="${OPTARG}"
+      else
+        PARAM_DOMAIN="${PARAM_DOMAIN} ${OPTARG}"
+       fi
+      ;;
+    x)
+      # PARAM_Usage: --force (-x)
+      # PARAM_Description: force renew of certificate even if it is longer valid than value in RENEW_DAYS
+      PARAM_FORCE="yes"
+      ;;
+    f)
+      # PARAM_Usage: --config (-f) path/to/config.sh
+      # PARAM_Description: Use specified config file
+      check_parameters "${OPTARG:-}"
+      CONFIG="${OPTARG}"
+      ;;
+    p)
+      # PARAM_Usage: --privkey (-p) path/to/key.pem
+      # PARAM_Description: Use specified private key instead of account key (useful for revocation)
+      check_parameters "${OPTARG:-}"
+      PARAM_PRIVATE_KEY="${OPTARG}"
+      ;;
+    *)
+      echo "Unknown parameter detected: -${OPTARG}" >&2
+      echo >&2
+      command_help >&2
+      exit 1
+      ;;
+  esac
 done
+
+if [[ -z "${COMMAND}" ]]; then
+  command_help
+  exit 1
+fi
+
+init_system
+
+case "${COMMAND}" in
+  sign_domains)
+    command_sign_domains
+    ;;
+  env)
+    command_env
+    ;;
+  revoke)
+    command_revoke "${revoke_me}"
+    ;;
+esac