]>
Commit | Line | Data |
---|---|---|
2e8454b4 | 1 | #!/usr/bin/env bash |
a1a9c8a4 | 2 | |
ec49a443 | 3 | # dehydrated by lukas2511 |
64e35463 | 4 | # Source: https://github.com/lukas2511/dehydrated |
0fa381ff LS |
5 | # |
6 | # This script is licensed under The MIT License (see LICENSE for more information). | |
a1a9c8a4 | 7 | |
69f3e78b LS |
8 | set -e |
9 | set -u | |
10 | set -o pipefail | |
da2eeda9 | 11 | [[ -n "${ZSH_VERSION:-}" ]] && set -o SH_WORD_SPLIT && set +o FUNCTION_ARGZERO |
81882a64 | 12 | umask 077 # paranoid umask, we're creating private keys |
61f0b7ed | 13 | |
85a25b56 LS |
14 | # Find directory in which this script is stored by traversing all symbolic links |
15 | SOURCE="${0}" | |
16 | while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink | |
17 | DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" | |
18 | SOURCE="$(readlink "$SOURCE")" | |
19 | [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located | |
20 | done | |
21 | SCRIPTDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" | |
22 | ||
0e92aba2 MG |
23 | BASEDIR="${SCRIPTDIR}" |
24 | ||
1f6a80a0 LS |
25 | # Create (identifiable) temporary files |
26 | _mktemp() { | |
75985c6a | 27 | # shellcheck disable=SC2068 |
ec49a443 | 28 | mktemp ${@:-} "${TMPDIR:-/tmp}/dehydrated-XXXXXX" |
1f6a80a0 LS |
29 | } |
30 | ||
bc580335 | 31 | # Check for script dependencies |
9f66bfdb | 32 | check_dependencies() { |
0af7f388 | 33 | # just execute some dummy and/or version commands to see if required tools exist and are actually usable |
115041cd | 34 | openssl version > /dev/null 2>&1 || _exiterr "This script requires an openssl binary." |
f7c7d8c5 | 35 | _sed "" < /dev/null > /dev/null 2>&1 || _exiterr "This script requires sed with support for extended (modern) regular expressions." |
4b8883b4 | 36 | command -v grep > /dev/null 2>&1 || _exiterr "This script requires grep." |
e2e2c362 | 37 | command -v mktemp > /dev/null 2>&1 || _exiterr "This script requires mktemp." |
cfc00c42 | 38 | command -v diff > /dev/null 2>&1 || _exiterr "This script requires diff." |
0af7f388 LS |
39 | |
40 | # curl returns with an error code in some ancient versions so we have to catch that | |
41 | set +e | |
42 | curl -V > /dev/null 2>&1 | |
0af7f388 | 43 | retcode="$?" |
36a03146 | 44 | set -e |
0af7f388 LS |
45 | if [[ ! "${retcode}" = "0" ]] && [[ ! "${retcode}" = "2" ]]; then |
46 | _exiterr "This script requires curl." | |
47 | fi | |
9f66bfdb LS |
48 | } |
49 | ||
ec489069 LS |
50 | store_configvars() { |
51 | __KEY_ALGO="${KEY_ALGO}" | |
52 | __OCSP_MUST_STAPLE="${OCSP_MUST_STAPLE}" | |
53 | __PRIVATE_KEY_RENEW="${PRIVATE_KEY_RENEW}" | |
54 | __KEYSIZE="${KEYSIZE}" | |
55 | __CHALLENGETYPE="${CHALLENGETYPE}" | |
56 | __HOOK="${HOOK}" | |
57 | __WELLKNOWN="${WELLKNOWN}" | |
58 | __HOOK_CHAIN="${HOOK_CHAIN}" | |
59 | __OPENSSL_CNF="${OPENSSL_CNF}" | |
60 | __RENEW_DAYS="${RENEW_DAYS}" | |
364bcccf | 61 | __IP_VERSION="${IP_VERSION}" |
ec489069 LS |
62 | } |
63 | ||
64 | reset_configvars() { | |
65 | KEY_ALGO="${__KEY_ALGO}" | |
66 | OCSP_MUST_STAPLE="${__OCSP_MUST_STAPLE}" | |
67 | PRIVATE_KEY_RENEW="${__PRIVATE_KEY_RENEW}" | |
68 | KEYSIZE="${__KEYSIZE}" | |
69 | CHALLENGETYPE="${__CHALLENGETYPE}" | |
70 | HOOK="${__HOOK}" | |
71 | WELLKNOWN="${__WELLKNOWN}" | |
72 | HOOK_CHAIN="${__HOOK_CHAIN}" | |
73 | OPENSSL_CNF="${__OPENSSL_CNF}" | |
74 | RENEW_DAYS="${__RENEW_DAYS}" | |
364bcccf | 75 | IP_VERSION="${__IP_VERSION}" |
4d3f3fff | 76 | ALT_NAMES= |
ec489069 LS |
77 | } |
78 | ||
79 | # verify configuration values | |
80 | verify_config() { | |
81 | [[ "${CHALLENGETYPE}" =~ (http-01|dns-01) ]] || _exiterr "Unknown challenge type ${CHALLENGETYPE}... can not continue." | |
82 | if [[ "${CHALLENGETYPE}" = "dns-01" ]] && [[ -z "${HOOK}" ]]; then | |
83 | _exiterr "Challenge type dns-01 needs a hook script for deployment... can not continue." | |
84 | fi | |
aed4272e | 85 | if [[ "${CHALLENGETYPE}" = "http-01" && ! -d "${WELLKNOWN}" && ! "${COMMAND:-}" = "register" ]]; then |
ec489069 LS |
86 | _exiterr "WELLKNOWN directory doesn't exist, please create ${WELLKNOWN} and set appropriate permissions." |
87 | fi | |
88 | [[ "${KEY_ALGO}" =~ ^(rsa|prime256v1|secp384r1)$ ]] || _exiterr "Unknown public key algorithm ${KEY_ALGO}... can not continue." | |
364bcccf | 89 | if [[ -n "${IP_VERSION}" ]]; then |
90 | [[ "${IP_VERSION}" = "4" || "${IP_VERSION}" = "6" ]] || _exiterr "Unknown IP version ${IP_VERSION}... can not continue." | |
91 | fi | |
ec489069 LS |
92 | } |
93 | ||
ff116396 LS |
94 | # Setup default config values, search for and load configuration files |
95 | load_config() { | |
00810795 LS |
96 | # Check for config in various locations |
97 | if [[ -z "${CONFIG:-}" ]]; then | |
ec49a443 | 98 | for check_config in "/etc/dehydrated" "/usr/local/etc/dehydrated" "${PWD}" "${SCRIPTDIR}"; do |
0d8b9289 | 99 | if [[ -f "${check_config}/config" ]]; then |
00810795 | 100 | BASEDIR="${check_config}" |
d5b28586 | 101 | CONFIG="${check_config}/config" |
00810795 LS |
102 | break |
103 | fi | |
104 | done | |
105 | fi | |
106 | ||
ff116396 LS |
107 | # Default values |
108 | CA="https://acme-v01.api.letsencrypt.org/directory" | |
6a32f20e LS |
109 | CA_TERMS="https://acme-v01.api.letsencrypt.org/terms" |
110 | LICENSE= | |
785ffa55 | 111 | CERTDIR= |
034ec30c | 112 | ACCOUNTDIR= |
de173892 | 113 | CHALLENGETYPE="http-01" |
a1cb7ccc | 114 | CONFIG_D= |
44aca90c | 115 | DOMAINS_D= |
a3e5ed36 | 116 | DOMAINS_TXT= |
ff116396 | 117 | HOOK= |
6e048f7f | 118 | HOOK_CHAIN="no" |
30ad9584 | 119 | RENEW_DAYS="30" |
ff116396 | 120 | KEYSIZE="4096" |
9baf3532 | 121 | WELLKNOWN= |
e608dc2b | 122 | PRIVATE_KEY_RENEW="yes" |
a13e4103 | 123 | PRIVATE_KEY_ROLLOVER="no" |
c71ca3a8 | 124 | KEY_ALGO=rsa |
f0323faf | 125 | OPENSSL_CNF="$(openssl version -d | cut -d\" -f2)/openssl.cnf" |
ff116396 | 126 | CONTACT_EMAIL= |
9baf3532 | 127 | LOCKFILE= |
8e77ba5e | 128 | OCSP_MUST_STAPLE="no" |
364bcccf | 129 | IP_VERSION= |
1e33cfe5 | 130 | |
81882a64 | 131 | if [[ -z "${CONFIG:-}" ]]; then |
ff116396 | 132 | echo "#" >&2 |
a1cb7ccc | 133 | echo "# !! WARNING !! No main config file found, using default config!" >&2 |
ff116396 | 134 | echo "#" >&2 |
0d8b9289 | 135 | elif [[ -f "${CONFIG}" ]]; then |
a1cb7ccc | 136 | echo "# INFO: Using main config file ${CONFIG}" |
81882a64 LS |
137 | BASEDIR="$(dirname "${CONFIG}")" |
138 | # shellcheck disable=SC1090 | |
139 | . "${CONFIG}" | |
140 | else | |
f06f764f | 141 | _exiterr "Specified config file doesn't exist." |
81882a64 | 142 | fi |
61f0b7ed | 143 | |
a1cb7ccc DB |
144 | if [[ -n "${CONFIG_D}" ]]; then |
145 | if [[ ! -d "${CONFIG_D}" ]]; then | |
146 | _exiterr "The path ${CONFIG_D} specified for CONFIG_D does not point to a directory." >&2 | |
147 | fi | |
148 | ||
e2d8bfa4 | 149 | for check_config_d in "${CONFIG_D}"/*.sh; do |
a1cb7ccc DB |
150 | if [[ ! -e "${check_config_d}" ]]; then |
151 | echo "# !! WARNING !! Extra configuration directory ${CONFIG_D} exists, but no configuration found in it." >&2 | |
152 | break | |
153 | elif [[ -f "${check_config_d}" ]] && [[ -r "${check_config_d}" ]]; then | |
154 | echo "# INFO: Using additional config file ${check_config_d}" | |
5c68c221 | 155 | # shellcheck disable=SC1090 |
e2d8bfa4 | 156 | . "${check_config_d}" |
a1cb7ccc DB |
157 | else |
158 | _exiterr "Specified additional config ${check_config_d} is not readable or not a file at all." >&2 | |
159 | fi | |
160 | done | |
161 | fi | |
162 | ||
81882a64 LS |
163 | # Remove slash from end of BASEDIR. Mostly for cleaner outputs, doesn't change functionality. |
164 | BASEDIR="${BASEDIR%%/}" | |
401f5f75 | 165 | |
1e33cfe5 | 166 | # Check BASEDIR and set default variables |
f06f764f | 167 | [[ -d "${BASEDIR}" ]] || _exiterr "BASEDIR does not exist: ${BASEDIR}" |
ed27e013 | 168 | |
034ec30c LS |
169 | CAHASH="$(echo "${CA}" | urlbase64)" |
170 | [[ -z "${ACCOUNTDIR}" ]] && ACCOUNTDIR="${BASEDIR}/accounts" | |
171 | mkdir -p "${ACCOUNTDIR}/${CAHASH}" | |
172 | [[ -f "${ACCOUNTDIR}/${CAHASH}/config" ]] && . "${ACCOUNTDIR}/${CAHASH}/config" | |
173 | ACCOUNT_KEY="${ACCOUNTDIR}/${CAHASH}/account_key.pem" | |
174 | ACCOUNT_KEY_JSON="${ACCOUNTDIR}/${CAHASH}/registration_info.json" | |
175 | ||
176 | if [[ -f "${BASEDIR}/private_key.pem" ]] && [[ ! -f "${ACCOUNT_KEY}" ]]; then | |
177 | echo "! Moving private_key.pem to ${ACCOUNT_KEY}" | |
178 | mv "${BASEDIR}/private_key.pem" "${ACCOUNT_KEY}" | |
179 | fi | |
180 | if [[ -f "${BASEDIR}/private_key.json" ]] && [[ ! -f "${ACCOUNT_KEY_JSON}" ]]; then | |
181 | echo "! Moving private_key.json to ${ACCOUNT_KEY_JSON}" | |
182 | mv "${BASEDIR}/private_key.json" "${ACCOUNT_KEY_JSON}" | |
183 | fi | |
184 | ||
785ffa55 | 185 | [[ -z "${CERTDIR}" ]] && CERTDIR="${BASEDIR}/certs" |
a3e5ed36 | 186 | [[ -z "${DOMAINS_TXT}" ]] && DOMAINS_TXT="${BASEDIR}/domains.txt" |
64e35463 | 187 | [[ -z "${WELLKNOWN}" ]] && WELLKNOWN="/var/www/dehydrated" |
9baf3532 | 188 | [[ -z "${LOCKFILE}" ]] && LOCKFILE="${BASEDIR}/lock" |
8456855e | 189 | [[ -n "${PARAM_LOCKFILE_SUFFIX:-}" ]] && LOCKFILE="${LOCKFILE}-${PARAM_LOCKFILE_SUFFIX}" |
bd9cc5b0 | 190 | [[ -n "${PARAM_NO_LOCK:-}" ]] && LOCKFILE="" |
9baf3532 | 191 | |
de173892 | 192 | [[ -n "${PARAM_HOOK:-}" ]] && HOOK="${PARAM_HOOK}" |
785ffa55 | 193 | [[ -n "${PARAM_CERTDIR:-}" ]] && CERTDIR="${PARAM_CERTDIR}" |
de173892 | 194 | [[ -n "${PARAM_CHALLENGETYPE:-}" ]] && CHALLENGETYPE="${PARAM_CHALLENGETYPE}" |
c71ca3a8 | 195 | [[ -n "${PARAM_KEY_ALGO:-}" ]] && KEY_ALGO="${PARAM_KEY_ALGO}" |
8e77ba5e | 196 | [[ -n "${PARAM_OCSP_MUST_STAPLE:-}" ]] && OCSP_MUST_STAPLE="${PARAM_OCSP_MUST_STAPLE}" |
364bcccf | 197 | [[ -n "${PARAM_IP_VERSION:-}" ]] && IP_VERSION="${PARAM_IP_VERSION}" |
e925b293 | 198 | |
ec489069 LS |
199 | verify_config |
200 | store_configvars | |
ff116396 LS |
201 | } |
202 | ||
93cd114f | 203 | # Initialize system |
ff116396 LS |
204 | init_system() { |
205 | load_config | |
81882a64 | 206 | |
1e33cfe5 | 207 | # Lockfile handling (prevents concurrent access) |
bd9cc5b0 LS |
208 | if [[ -n "${LOCKFILE}" ]]; then |
209 | LOCKDIR="$(dirname "${LOCKFILE}")" | |
210 | [[ -w "${LOCKDIR}" ]] || _exiterr "Directory ${LOCKDIR} for LOCKFILE ${LOCKFILE} is not writable, aborting." | |
211 | ( set -C; date > "${LOCKFILE}" ) 2>/dev/null || _exiterr "Lock file '${LOCKFILE}' present, aborting." | |
212 | remove_lock() { rm -f "${LOCKFILE}"; } | |
213 | trap 'remove_lock' EXIT | |
214 | fi | |
81882a64 | 215 | |
81882a64 | 216 | # Get CA URLs |
3a9e97f9 | 217 | CA_DIRECTORY="$(http_request get "${CA}")" |
81882a64 LS |
218 | CA_NEW_CERT="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value new-cert)" && |
219 | CA_NEW_AUTHZ="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value new-authz)" && | |
220 | CA_NEW_REG="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value new-reg)" && | |
10d9f342 | 221 | # shellcheck disable=SC2015 |
81882a64 | 222 | CA_REVOKE_CERT="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value revoke-cert)" || |
93cd114f | 223 | _exiterr "Problem retrieving ACME/CA-URLs, check if your configured CA points to the directory entrypoint." |
81882a64 | 224 | |
93cd114f | 225 | # Export some environment variables to be used in hook script |
298a7e9a | 226 | export WELLKNOWN BASEDIR CERTDIR CONFIG COMMAND |
0e92aba2 | 227 | |
93cd114f LS |
228 | # Checking for private key ... |
229 | register_new_key="no" | |
8aa1a05b | 230 | if [[ -n "${PARAM_ACCOUNT_KEY:-}" ]]; then |
0e92aba2 | 231 | # a private key was specified from the command line so use it for this run |
8aa1a05b LS |
232 | echo "Using private key ${PARAM_ACCOUNT_KEY} instead of account key" |
233 | ACCOUNT_KEY="${PARAM_ACCOUNT_KEY}" | |
234 | ACCOUNT_KEY_JSON="${PARAM_ACCOUNT_KEY}.json" | |
0e92aba2 MG |
235 | else |
236 | # Check if private account key exists, if it doesn't exist yet generate a new one (rsa key) | |
8aa1a05b | 237 | if [[ ! -e "${ACCOUNT_KEY}" ]]; then |
6a32f20e LS |
238 | REAL_LICENSE="$(http_request head "${CA_TERMS}" | (grep Location: || true) | awk -F ': ' '{print $2}' | tr -d '\n\r')" |
239 | if [[ -z "${REAL_LICENSE}" ]]; then | |
240 | printf '\n' | |
241 | printf 'Error retrieving terms of service from certificate authority.\n' | |
242 | printf 'Please set LICENSE in config manually.\n' | |
243 | exit 1 | |
244 | fi | |
245 | if [[ ! "${LICENSE}" = "${REAL_LICENSE}" ]]; then | |
246 | if [[ "${PARAM_ACCEPT_TERMS:-}" = "yes" ]]; then | |
247 | LICENSE="${REAL_LICENSE}" | |
248 | else | |
249 | printf '\n' | |
250 | printf 'To use dehydrated with this certificate authority you have to agree to their terms of service which you can find here: %s\n\n' "${REAL_LICENSE}" | |
251 | printf 'To accept these terms of service run `%s --register --accept-terms`.\n' "${0}" | |
252 | exit 1 | |
253 | fi | |
254 | fi | |
255 | ||
81882a64 | 256 | echo "+ Generating account key..." |
8aa1a05b | 257 | _openssl genrsa -out "${ACCOUNT_KEY}" "${KEYSIZE}" |
93cd114f | 258 | register_new_key="yes" |
81882a64 | 259 | fi |
81882a64 | 260 | fi |
8aa1a05b | 261 | openssl rsa -in "${ACCOUNT_KEY}" -check 2>/dev/null > /dev/null || _exiterr "Account key is not valid, can not continue." |
1ab6a436 | 262 | |
81882a64 | 263 | # Get public components from private key and calculate thumbprint |
8aa1a05b LS |
264 | pubExponent64="$(printf '%x' "$(openssl rsa -in "${ACCOUNT_KEY}" -noout -text | awk '/publicExponent/ {print $2}')" | hex2bin | urlbase64)" |
265 | pubMod64="$(openssl rsa -in "${ACCOUNT_KEY}" -noout -modulus | cut -d'=' -f2 | hex2bin | urlbase64)" | |
81882a64 | 266 | |
21c18dd3 | 267 | thumbprint="$(printf '{"e":"%s","kty":"RSA","n":"%s"}' "${pubExponent64}" "${pubMod64}" | openssl dgst -sha256 -binary | urlbase64)" |
81882a64 LS |
268 | |
269 | # If we generated a new private key in the step above we have to register it with the acme-server | |
93cd114f | 270 | if [[ "${register_new_key}" = "yes" ]]; then |
64e35463 | 271 | echo "+ Registering account key with ACME server..." |
034ec30c | 272 | FAILED=false |
aed4272e LS |
273 | |
274 | if [[ -z "${CA_NEW_REG}" ]]; then | |
275 | echo "Certificate authority doesn't allow registrations." | |
276 | FAILED=true | |
034ec30c | 277 | fi |
aed4272e LS |
278 | |
279 | # If an email for the contact has been provided then adding it to the registration request | |
280 | if [[ "${FAILED}" = "false" ]]; then | |
281 | if [[ -n "${CONTACT_EMAIL}" ]]; then | |
282 | (signed_request "${CA_NEW_REG}" '{"resource": "new-reg", "contact":["mailto:'"${CONTACT_EMAIL}"'"], "agreement": "'"$LICENSE"'"}' > "${ACCOUNT_KEY_JSON}") || FAILED=true | |
283 | else | |
284 | (signed_request "${CA_NEW_REG}" '{"resource": "new-reg", "agreement": "'"$LICENSE"'"}' > "${ACCOUNT_KEY_JSON}") || FAILED=true | |
285 | fi | |
034ec30c | 286 | fi |
aed4272e | 287 | |
034ec30c LS |
288 | if [[ "${FAILED}" = "true" ]]; then |
289 | echo | |
290 | echo | |
291 | echo "Error registering account key. See message above for more information." | |
292 | rm "${ACCOUNT_KEY}" "${ACCOUNT_KEY_JSON}" | |
293 | exit 1 | |
81882a64 | 294 | fi |
aed4272e LS |
295 | elif [[ "${COMMAND:-}" = "register" ]]; then |
296 | echo "+ Account already registered!" | |
297 | exit 0 | |
81882a64 | 298 | fi |
81882a64 | 299 | } |
c24843c6 | 300 | |
f7c7d8c5 LS |
301 | # Different sed version for different os types... |
302 | _sed() { | |
c3c9ff4c | 303 | if [[ "${OSTYPE}" = "Linux" ]]; then |
f7c7d8c5 LS |
304 | sed -r "${@}" |
305 | else | |
306 | sed -E "${@}" | |
307 | fi | |
308 | } | |
309 | ||
9f66bfdb LS |
310 | # Print error message and exit with error |
311 | _exiterr() { | |
312 | echo "ERROR: ${1}" >&2 | |
313 | exit 1 | |
314 | } | |
315 | ||
561f0626 LS |
316 | # Remove newlines and whitespace from json |
317 | clean_json() { | |
318 | tr -d '\r\n' | _sed -e 's/ +/ /g' -e 's/\{ /{/g' -e 's/ \}/}/g' -e 's/\[ /[/g' -e 's/ \]/]/g' | |
319 | } | |
320 | ||
994803bf | 321 | # Encode data as url-safe formatted base64 |
61f0b7ed | 322 | urlbase64() { |
c6e60302 | 323 | # urlbase64: base64 encoded string with '+' replaced with '-' and '/' replaced with '_' |
a316a094 | 324 | openssl base64 -e | tr -d '\n\r' | _sed -e 's:=*$::g' -e 'y:+/:-_:' |
61f0b7ed | 325 | } |
91ce50af | 326 | |
16bef17e | 327 | # Convert hex string to binary data |
9fe313d8 | 328 | hex2bin() { |
16bef17e | 329 | # Remove spaces, add leading zero, escape as hex string and parse with printf |
f7c7d8c5 | 330 | printf -- "$(cat | _sed -e 's/[[:space:]]//g' -e 's/^(.(.{2})*)$/0\1/' -e 's/(.{2})/\\x\1/g')" |
9fe313d8 | 331 | } |
61f0b7ed | 332 | |
bc580335 | 333 | # Get string value from json dictionary |
09729186 | 334 | get_json_string_value() { |
5c68c221 LS |
335 | local filter |
336 | filter=$(printf 's/.*"%s": *"\([^"]*\)".*/\\1/p' "$1") | |
337 | sed -n "${filter}" | |
09729186 LS |
338 | } |
339 | ||
9729751d | 340 | rm_json_arrays() { |
341 | local filter | |
342 | filter='s/\[[^][]*\]/null/g' | |
343 | # remove three levels of nested arrays | |
344 | sed -e "${filter}" -e "${filter}" -e "${filter}" | |
345 | } | |
346 | ||
cc605a22 LS |
347 | # OpenSSL writes to stderr/stdout even when there are no errors. So just |
348 | # display the output if the exit code was != 0 to simplify debugging. | |
349 | _openssl() { | |
350 | set +e | |
351 | out="$(openssl "${@}" 2>&1)" | |
352 | res=$? | |
353 | set -e | |
39c01fd7 LS |
354 | if [[ ${res} -ne 0 ]]; then |
355 | echo " + ERROR: failed to run $* (Exitcode: ${res})" >&2 | |
cc605a22 LS |
356 | echo >&2 |
357 | echo "Details:" >&2 | |
39c01fd7 | 358 | echo "${out}" >&2 |
676d15c5 | 359 | echo >&2 |
39c01fd7 | 360 | exit ${res} |
cc605a22 LS |
361 | fi |
362 | } | |
363 | ||
59f16407 | 364 | # Send http(s) request with specified method |
3a9e97f9 | 365 | http_request() { |
1f6a80a0 | 366 | tempcont="$(_mktemp)" |
3cb292cb | 367 | |
364bcccf | 368 | if [[ -n "${IP_VERSION:-}" ]]; then |
369 | ip_version="-${IP_VERSION}" | |
370 | fi | |
371 | ||
1233dc95 | 372 | set +e |
dd5f36e5 | 373 | if [[ "${1}" = "head" ]]; then |
364bcccf | 374 | statuscode="$(curl ${ip_version:-} -s -w "%{http_code}" -o "${tempcont}" "${2}" -I)" |
1233dc95 | 375 | curlret="${?}" |
dd5f36e5 | 376 | elif [[ "${1}" = "get" ]]; then |
364bcccf | 377 | statuscode="$(curl ${ip_version:-} -s -w "%{http_code}" -o "${tempcont}" "${2}")" |
1233dc95 | 378 | curlret="${?}" |
dd5f36e5 | 379 | elif [[ "${1}" = "post" ]]; then |
364bcccf | 380 | statuscode="$(curl ${ip_version:-} -s -w "%{http_code}" -o "${tempcont}" "${2}" -d "${3}")" |
1233dc95 | 381 | curlret="${?}" |
59f16407 | 382 | else |
1233dc95 | 383 | set -e |
59f16407 | 384 | _exiterr "Unknown request method: ${1}" |
91ce50af | 385 | fi |
1233dc95 LS |
386 | set -e |
387 | ||
388 | if [[ ! "${curlret}" = "0" ]]; then | |
df292dec | 389 | _exiterr "Problem connecting to server (${1} for ${2}; curl returned with ${curlret})" |
1233dc95 | 390 | fi |
dd5f36e5 | 391 | |
3cb292cb | 392 | if [[ ! "${statuscode:0:1}" = "2" ]]; then |
6a32f20e LS |
393 | if [[ ! "${2}" = "${CA_TERMS}" ]] || [[ ! "${statuscode:0:1}" = "3" ]]; then |
394 | echo " + ERROR: An error occurred while sending ${1}-request to ${2} (Status ${statuscode})" >&2 | |
395 | echo >&2 | |
396 | echo "Details:" >&2 | |
397 | cat "${tempcont}" >&2 | |
398 | echo >&2 | |
399 | echo >&2 | |
c24843c6 | 400 | |
6a32f20e LS |
401 | # An exclusive hook for the {1}-request error might be useful (e.g., for sending an e-mail to admins) |
402 | if [[ -n "${HOOK}" ]] && [[ "${HOOK_CHAIN}" != "yes" ]]; then | |
403 | errtxt=`cat ${tempcont}` | |
404 | "${HOOK}" "request_failure" "${statuscode}" "${errtxt}" "${1}" | |
405 | fi | |
c24843c6 | 406 | |
6a32f20e | 407 | rm -f "${tempcont}" |
c24843c6 | 408 | |
6a32f20e LS |
409 | # Wait for hook script to clean the challenge if used |
410 | if [[ -n "${HOOK}" ]] && [[ "${HOOK_CHAIN}" != "yes" ]] && [[ -n "${challenge_token:+set}" ]]; then | |
411 | "${HOOK}" "clean_challenge" '' "${challenge_token}" "${keyauth}" | |
412 | fi | |
c24843c6 | 413 | |
6a32f20e LS |
414 | # remove temporary domains.txt file if used |
415 | [[ -n "${PARAM_DOMAIN:-}" && -n "${DOMAINS_TXT:-}" ]] && rm "${DOMAINS_TXT}" | |
416 | exit 1 | |
417 | fi | |
130ea6ab | 418 | fi |
dd5f36e5 | 419 | |
31111265 | 420 | cat "${tempcont}" |
3cb292cb | 421 | rm -f "${tempcont}" |
91ce50af | 422 | } |
81882a64 | 423 | |
1446fd88 | 424 | # Send signed request |
61f0b7ed | 425 | signed_request() { |
c6e60302 | 426 | # Encode payload as urlbase64 |
4aa48d33 | 427 | payload64="$(printf '%s' "${2}" | urlbase64)" |
61f0b7ed | 428 | |
c6e60302 | 429 | # Retrieve nonce from acme-server |
994803bf | 430 | nonce="$(http_request head "${CA}" | grep Replay-Nonce: | awk -F ': ' '{print $2}' | tr -d '\n\r')" |
61f0b7ed | 431 | |
c6e60302 | 432 | # Build header with just our public key and algorithm information |
61f0b7ed LS |
433 | header='{"alg": "RS256", "jwk": {"e": "'"${pubExponent64}"'", "kty": "RSA", "n": "'"${pubMod64}"'"}}' |
434 | ||
c6e60302 | 435 | # Build another header which also contains the previously received nonce and encode it as urlbase64 |
61f0b7ed | 436 | protected='{"alg": "RS256", "jwk": {"e": "'"${pubExponent64}"'", "kty": "RSA", "n": "'"${pubMod64}"'"}, "nonce": "'"${nonce}"'"}' |
4aa48d33 | 437 | protected64="$(printf '%s' "${protected}" | urlbase64)" |
61f0b7ed | 438 | |
c6e60302 | 439 | # Sign header with nonce and our payload with our private key and encode signature as urlbase64 |
8aa1a05b | 440 | signed64="$(printf '%s' "${protected64}.${payload64}" | openssl dgst -sha256 -sign "${ACCOUNT_KEY}" | urlbase64)" |
61f0b7ed | 441 | |
c6e60302 | 442 | # Send header + extended header + payload + signature to the acme-server |
61f0b7ed LS |
443 | data='{"header": '"${header}"', "protected": "'"${protected64}"'", "payload": "'"${payload64}"'", "signature": "'"${signed64}"'"}' |
444 | ||
3a9e97f9 | 445 | http_request post "${1}" "${data}" |
61f0b7ed LS |
446 | } |
447 | ||
a62968c9 NL |
448 | # Extracts all subject names from a CSR |
449 | # Outputs either the CN, or the SANs, one per line | |
450 | extract_altnames() { | |
451 | csr="${1}" # the CSR itself (not a file) | |
81882a64 | 452 | |
a62968c9 NL |
453 | if ! <<<"${csr}" openssl req -verify -noout 2>/dev/null; then |
454 | _exiterr "Certificate signing request isn't valid" | |
09729186 | 455 | fi |
3cc587c2 | 456 | |
a62968c9 | 457 | reqtext="$( <<<"${csr}" openssl req -noout -text )" |
39c01fd7 | 458 | if <<<"${reqtext}" grep -q '^[[:space:]]*X509v3 Subject Alternative Name:[[:space:]]*$'; then |
a62968c9 | 459 | # SANs used, extract these |
3d8d320c | 460 | altnames="$( <<<"${reqtext}" awk '/X509v3 Subject Alternative Name:/{print;getline;print;}' | tail -n1 )" |
a62968c9 | 461 | # split to one per line: |
5c68c221 | 462 | # shellcheck disable=SC1003 |
34f94322 | 463 | altnames="$( <<<"${altnames}" _sed -e 's/^[[:space:]]*//; s/, /\'$'\n''/g' )" |
a62968c9 | 464 | # we can only get DNS: ones signed |
5c68c221 | 465 | if grep -qv '^DNS:' <<<"${altnames}"; then |
a62968c9 NL |
466 | _exiterr "Certificate signing request contains non-DNS Subject Alternative Names" |
467 | fi | |
468 | # strip away the DNS: prefix | |
469 | altnames="$( <<<"${altnames}" _sed -e 's/^DNS://' )" | |
39c01fd7 | 470 | echo "${altnames}" |
a62968c9 NL |
471 | |
472 | else | |
473 | # No SANs, extract CN | |
474 | altnames="$( <<<"${reqtext}" grep '^[[:space:]]*Subject:' | _sed -e 's/.* CN=([^ /,]*).*/\1/' )" | |
39c01fd7 | 475 | echo "${altnames}" |
3dbbb461 | 476 | fi |
a62968c9 | 477 | } |
3dbbb461 | 478 | |
50e7a072 NL |
479 | # Create certificate for domain(s) and outputs it FD 3 |
480 | sign_csr() { | |
481 | csr="${1}" # the CSR itself (not a file) | |
81882a64 | 482 | |
50e7a072 NL |
483 | if { true >&3; } 2>/dev/null; then |
484 | : # fd 3 looks OK | |
485 | else | |
486 | _exiterr "sign_csr: FD 3 not open" | |
61f0b7ed LS |
487 | fi |
488 | ||
50e7a072 NL |
489 | shift 1 || true |
490 | altnames="${*:-}" | |
39c01fd7 LS |
491 | if [ -z "${altnames}" ]; then |
492 | altnames="$( extract_altnames "${csr}" )" | |
a62968c9 | 493 | fi |
3dbbb461 | 494 | |
50e7a072 NL |
495 | if [[ -z "${CA_NEW_AUTHZ}" ]] || [[ -z "${CA_NEW_CERT}" ]]; then |
496 | _exiterr "Certificate authority doesn't allow certificate signing" | |
61f0b7ed | 497 | fi |
c6e60302 | 498 | |
6e048f7f | 499 | local idx=0 |
da2eeda9 | 500 | if [[ -n "${ZSH_VERSION:-}" ]]; then |
9729751d | 501 | local -A challenge_altnames challenge_uris challenge_tokens keyauths deploy_args |
da2eeda9 | 502 | else |
9729751d | 503 | local -a challenge_altnames challenge_uris challenge_tokens keyauths deploy_args |
da2eeda9 | 504 | fi |
39c01fd7 | 505 | |
6e048f7f | 506 | # Request challenges |
1446fd88 | 507 | for altname in ${altnames}; do |
c6e60302 | 508 | # Ask the acme-server for new challenge token and extract them from the resulting json block |
579e2316 | 509 | echo " + Requesting challenge for ${altname}..." |
561f0626 | 510 | response="$(signed_request "${CA_NEW_AUTHZ}" '{"resource": "new-authz", "identifier": {"type": "dns", "value": "'"${altname}"'"}}' | clean_json)" |
61f0b7ed | 511 | |
9729751d | 512 | challenge_status="$(printf '%s' "${response}" | rm_json_arrays | get_json_string_value status)" |
513 | if [ "${challenge_status}" = "valid" ]; then | |
3c1d2673 | 514 | echo " + Already validated!" |
9729751d | 515 | continue |
516 | fi | |
517 | ||
4b8883b4 | 518 | challenges="$(printf '%s\n' "${response}" | sed -n 's/.*\("challenges":[^\[]*\[[^]]*]\).*/\1/p')" |
526843d6 | 519 | repl=$'\n''{' # fix syntax highlighting in Vim |
e925b293 | 520 | challenge="$(printf "%s" "${challenges//\{/${repl}}" | grep \""${CHALLENGETYPE}"\")" |
f7c7d8c5 | 521 | challenge_token="$(printf '%s' "${challenge}" | get_json_string_value token | _sed 's/[^A-Za-z0-9_\-]/_/g')" |
09729186 | 522 | challenge_uri="$(printf '%s' "${challenge}" | get_json_string_value uri)" |
61f0b7ed | 523 | |
dd5f36e5 | 524 | if [[ -z "${challenge_token}" ]] || [[ -z "${challenge_uri}" ]]; then |
1446fd88 | 525 | _exiterr "Can't retrieve challenges (${response})" |
abb95693 LS |
526 | fi |
527 | ||
c6e60302 | 528 | # Challenge response consists of the challenge token and the thumbprint of our public certificate |
61f0b7ed LS |
529 | keyauth="${challenge_token}.${thumbprint}" |
530 | ||
de173892 LS |
531 | case "${CHALLENGETYPE}" in |
532 | "http-01") | |
533 | # Store challenge response in well-known location and make world-readable (so that a webserver can access it) | |
534 | printf '%s' "${keyauth}" > "${WELLKNOWN}/${challenge_token}" | |
535 | chmod a+r "${WELLKNOWN}/${challenge_token}" | |
536 | keyauth_hook="${keyauth}" | |
537 | ;; | |
538 | "dns-01") | |
539 | # Generate DNS entry content for dns-01 validation | |
21c18dd3 | 540 | keyauth_hook="$(printf '%s' "${keyauth}" | openssl dgst -sha256 -binary | urlbase64)" |
de173892 LS |
541 | ;; |
542 | esac | |
61f0b7ed | 543 | |
9729751d | 544 | challenge_altnames[${idx}]="${altname}" |
39c01fd7 LS |
545 | challenge_uris[${idx}]="${challenge_uri}" |
546 | keyauths[${idx}]="${keyauth}" | |
547 | challenge_tokens[${idx}]="${challenge_token}" | |
6e048f7f | 548 | # Note: assumes args will never have spaces! |
39c01fd7 | 549 | deploy_args[${idx}]="${altname} ${challenge_token} ${keyauth_hook}" |
6e048f7f GD |
550 | idx=$((idx+1)) |
551 | done | |
3c1d2673 | 552 | challenge_count="${idx}" |
6e048f7f GD |
553 | |
554 | # Wait for hook script to deploy the challenges if used | |
3c1d2673 | 555 | if [[ ${challenge_count} -ne 0 ]]; then |
9729751d | 556 | # shellcheck disable=SC2068 |
557 | [[ -n "${HOOK}" ]] && [[ "${HOOK_CHAIN}" = "yes" ]] && "${HOOK}" "deploy_challenge" ${deploy_args[@]} | |
558 | fi | |
6e048f7f GD |
559 | |
560 | # Respond to challenges | |
9729751d | 561 | reqstatus="valid" |
6e048f7f | 562 | idx=0 |
3c1d2673 | 563 | if [ ${challenge_count} -ne 0 ]; then |
636fa1a5 AR |
564 | for altname in "${challenge_altnames[@]:0}"; do |
565 | challenge_token="${challenge_tokens[${idx}]}" | |
566 | keyauth="${keyauths[${idx}]}" | |
6e048f7f | 567 | |
636fa1a5 AR |
568 | # Wait for hook script to deploy the challenge if used |
569 | # shellcheck disable=SC2086 | |
570 | [[ -n "${HOOK}" ]] && [[ "${HOOK_CHAIN}" != "yes" ]] && "${HOOK}" "deploy_challenge" ${deploy_args[${idx}]} | |
61f0b7ed | 571 | |
636fa1a5 AR |
572 | # Ask the acme-server to verify our challenge and wait until it is no longer pending |
573 | echo " + Responding to challenge for ${altname}..." | |
574 | result="$(signed_request "${challenge_uris[${idx}]}" '{"resource": "challenge", "keyAuthorization": "'"${keyauth}"'"}' | clean_json)" | |
61f0b7ed | 575 | |
da2eeda9 | 576 | reqstatus="$(printf '%s\n' "${result}" | get_json_string_value status)" |
61f0b7ed | 577 | |
636fa1a5 AR |
578 | while [[ "${reqstatus}" = "pending" ]]; do |
579 | sleep 1 | |
580 | result="$(http_request get "${challenge_uris[${idx}]}")" | |
581 | reqstatus="$(printf '%s\n' "${result}" | get_json_string_value status)" | |
582 | done | |
81882a64 | 583 | |
636fa1a5 | 584 | [[ "${CHALLENGETYPE}" = "http-01" ]] && rm -f "${WELLKNOWN}/${challenge_token}" |
81882a64 | 585 | |
636fa1a5 AR |
586 | # Wait for hook script to clean the challenge if used |
587 | if [[ -n "${HOOK}" ]] && [[ "${HOOK_CHAIN}" != "yes" ]] && [[ -n "${challenge_token}" ]]; then | |
588 | # shellcheck disable=SC2086 | |
589 | "${HOOK}" "clean_challenge" ${deploy_args[${idx}]} | |
590 | fi | |
591 | idx=$((idx+1)) | |
69eea952 | 592 | |
636fa1a5 AR |
593 | if [[ "${reqstatus}" = "valid" ]]; then |
594 | echo " + Challenge is valid!" | |
595 | else | |
596 | [[ -n "${HOOK}" ]] && [[ "${HOOK_CHAIN}" != "yes" ]] && "${HOOK}" "invalid_challenge" "${altname}" "${result}" | |
597 | fi | |
61f0b7ed | 598 | done |
636fa1a5 | 599 | fi |
61f0b7ed | 600 | |
6e048f7f | 601 | # Wait for hook script to clean the challenges if used |
5c68c221 | 602 | # shellcheck disable=SC2068 |
3c1d2673 LS |
603 | if [[ ${challenge_count} -ne 0 ]]; then |
604 | [[ -n "${HOOK}" ]] && [[ "${HOOK_CHAIN}" = "yes" ]] && "${HOOK}" "clean_challenge" ${deploy_args[@]} | |
605 | fi | |
6e048f7f | 606 | |
da2eeda9 | 607 | if [[ "${reqstatus}" != "valid" ]]; then |
6e048f7f | 608 | # Clean up any remaining challenge_tokens if we stopped early |
3c1d2673 | 609 | if [[ "${CHALLENGETYPE}" = "http-01" ]] && [[ ${challenge_count} -ne 0 ]]; then |
39c01fd7 LS |
610 | while [ ${idx} -lt ${#challenge_tokens[@]} ]; do |
611 | rm -f "${WELLKNOWN}/${challenge_tokens[${idx}]}" | |
6e048f7f GD |
612 | idx=$((idx+1)) |
613 | done | |
614 | fi | |
615 | ||
da2eeda9 | 616 | _exiterr "Challenge is invalid! (returned: ${reqstatus}) (result: ${result})" |
6e048f7f GD |
617 | fi |
618 | ||
b7439a83 | 619 | # Finally request certificate from the acme-server and store it in cert-${timestamp}.pem and link from cert.pem |
579e2316 | 620 | echo " + Requesting certificate..." |
50e7a072 | 621 | csr64="$( <<<"${csr}" openssl req -outform DER | urlbase64)" |
09729186 | 622 | crt64="$(signed_request "${CA_NEW_CERT}" '{"resource": "new-cert", "csr": "'"${csr64}"'"}' | openssl base64 -e)" |
50e7a072 | 623 | crt="$( printf -- '-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----\n' "${crt64}" )" |
1446fd88 LS |
624 | |
625 | # Try to load the certificate to detect corruption | |
a4e7c43a | 626 | echo " + Checking certificate..." |
50e7a072 NL |
627 | _openssl x509 -text <<<"${crt}" |
628 | ||
629 | echo "${crt}" >&3 | |
630 | ||
631 | unset challenge_token | |
632 | echo " + Done!" | |
633 | } | |
634 | ||
ee65261e LS |
635 | # grep issuer cert uri from certificate |
636 | get_issuer_cert_uri() { | |
637 | certificate="${1}" | |
638 | openssl x509 -in "${certificate}" -noout -text | (grep 'CA Issuers - URI:' | cut -d':' -f2-) || true | |
639 | } | |
640 | ||
641 | # walk certificate chain, retrieving all intermediate certificates | |
6a32f20e | 642 | walk_chain() { |
ee65261e | 643 | local certificate |
6a32f20e LS |
644 | certificate="${1}" |
645 | ||
6a32f20e | 646 | local issuer_cert_uri |
ee65261e LS |
647 | issuer_cert_uri="${2:-}" |
648 | if [[ -z "${issuer_cert_uri}" ]]; then issuer_cert_uri="$(get_issuer_cert_uri "${certificate}")"; fi | |
6a32f20e LS |
649 | if [[ -n "${issuer_cert_uri}" ]]; then |
650 | # create temporary files | |
651 | local tmpcert | |
652 | local tmpcert_raw | |
653 | tmpcert_raw="$(_mktemp)" | |
654 | tmpcert="$(_mktemp)" | |
655 | ||
656 | # download certificate | |
657 | http_request get "${issuer_cert_uri}" > "${tmpcert_raw}" | |
658 | ||
659 | # PEM | |
660 | if grep -q "BEGIN CERTIFICATE" "${tmpcert_raw}"; then mv "${tmpcert_raw}" "${tmpcert}" | |
661 | # DER | |
662 | elif openssl x509 -in "${tmpcert_raw}" -inform DER -out "${tmpcert}" -outform PEM 2> /dev/null > /dev/null; then : | |
663 | # PKCS7 | |
664 | elif openssl pkcs7 -in "${tmpcert_raw}" -inform DER -out "${tmpcert}" -outform PEM -print_certs 2> /dev/null > /dev/null; then : | |
665 | # Unknown certificate type | |
666 | else _exiterr "Unknown certificate type in chain" | |
667 | fi | |
668 | ||
ee65261e LS |
669 | local next_issuer_cert_uri |
670 | next_issuer_cert_uri="$(get_issuer_cert_uri "${tmpcert}")" | |
671 | if [[ -n "${next_issuer_cert_uri}" ]]; then | |
672 | printf "\n%s\n" "${issuer_cert_uri}" | |
673 | cat "${tmpcert}" | |
674 | walk_chain "${tmpcert}" "${next_issuer_cert_uri}" | |
675 | fi | |
6a32f20e LS |
676 | rm -f "${tmpcert}" "${tmpcert_raw}" |
677 | fi | |
678 | } | |
679 | ||
50e7a072 NL |
680 | # Create certificate for domain(s) |
681 | sign_domain() { | |
682 | domain="${1}" | |
683 | altnames="${*}" | |
684 | timestamp="$(date +%s)" | |
685 | ||
686 | echo " + Signing domains..." | |
687 | if [[ -z "${CA_NEW_AUTHZ}" ]] || [[ -z "${CA_NEW_CERT}" ]]; then | |
688 | _exiterr "Certificate authority doesn't allow certificate signing" | |
689 | fi | |
690 | ||
691 | # If there is no existing certificate directory => make it | |
785ffa55 AM |
692 | if [[ ! -e "${CERTDIR}/${domain}" ]]; then |
693 | echo " + Creating new directory ${CERTDIR}/${domain} ..." | |
694 | mkdir -p "${CERTDIR}/${domain}" || _exiterr "Unable to create directory ${CERTDIR}/${domain}" | |
50e7a072 NL |
695 | fi |
696 | ||
af2bc7a9 LS |
697 | privkey="privkey.pem" |
698 | # generate a new private key if we need or want one | |
785ffa55 | 699 | if [[ ! -r "${CERTDIR}/${domain}/privkey.pem" ]] || [[ "${PRIVATE_KEY_RENEW}" = "yes" ]]; then |
af2bc7a9 LS |
700 | echo " + Generating private key..." |
701 | privkey="privkey-${timestamp}.pem" | |
702 | case "${KEY_ALGO}" in | |
785ffa55 AM |
703 | rsa) _openssl genrsa -out "${CERTDIR}/${domain}/privkey-${timestamp}.pem" "${KEYSIZE}";; |
704 | prime256v1|secp384r1) _openssl ecparam -genkey -name "${KEY_ALGO}" -out "${CERTDIR}/${domain}/privkey-${timestamp}.pem";; | |
af2bc7a9 LS |
705 | esac |
706 | fi | |
a13e4103 | 707 | # move rolloverkey into position (if any) |
708 | if [[ -r "${CERTDIR}/${domain}/privkey.pem" && -r "${CERTDIR}/${domain}/privkey.roll.pem" && "${PRIVATE_KEY_RENEW}" = "yes" && "${PRIVATE_KEY_ROLLOVER}" = "yes" ]]; then | |
709 | echo " + Moving Rolloverkey into position.... " | |
710 | mv "${CERTDIR}/${domain}/privkey.roll.pem" "${CERTDIR}/${domain}/privkey-tmp.pem" | |
711 | mv "${CERTDIR}/${domain}/privkey-${timestamp}.pem" "${CERTDIR}/${domain}/privkey.roll.pem" | |
712 | mv "${CERTDIR}/${domain}/privkey-tmp.pem" "${CERTDIR}/${domain}/privkey-${timestamp}.pem" | |
713 | fi | |
714 | # generate a new private rollover key if we need or want one | |
715 | if [[ ! -r "${CERTDIR}/${domain}/privkey.roll.pem" && "${PRIVATE_KEY_ROLLOVER}" = "yes" && "${PRIVATE_KEY_RENEW}" = "yes" ]]; then | |
716 | echo " + Generating private rollover key..." | |
717 | case "${KEY_ALGO}" in | |
718 | rsa) _openssl genrsa -out "${CERTDIR}/${domain}/privkey.roll.pem" "${KEYSIZE}";; | |
719 | prime256v1|secp384r1) _openssl ecparam -genkey -name "${KEY_ALGO}" -out "${CERTDIR}/${domain}/privkey.roll.pem";; | |
720 | esac | |
721 | fi | |
722 | # delete rolloverkeys if disabled | |
723 | if [[ -r "${CERTDIR}/${domain}/privkey.roll.pem" && ! "${PRIVATE_KEY_ROLLOVER}" = "yes" ]]; then | |
724 | echo " + Removing Rolloverkey (feature disabled)..." | |
725 | rm -f "${CERTDIR}/${domain}/privkey.roll.pem" | |
726 | fi | |
50e7a072 NL |
727 | |
728 | # Generate signing request config and the actual signing request | |
729 | echo " + Generating signing request..." | |
730 | SAN="" | |
731 | for altname in ${altnames}; do | |
732 | SAN+="DNS:${altname}, " | |
733 | done | |
734 | SAN="${SAN%%, }" | |
735 | local tmp_openssl_cnf | |
1f6a80a0 | 736 | tmp_openssl_cnf="$(_mktemp)" |
50e7a072 NL |
737 | cat "${OPENSSL_CNF}" > "${tmp_openssl_cnf}" |
738 | printf "[SAN]\nsubjectAltName=%s" "${SAN}" >> "${tmp_openssl_cnf}" | |
8e77ba5e LS |
739 | if [ "${OCSP_MUST_STAPLE}" = "yes" ]; then |
740 | printf "\n1.3.6.1.5.5.7.1.24=DER:30:03:02:01:05" >> "${tmp_openssl_cnf}" | |
741 | fi | |
785ffa55 | 742 | openssl req -new -sha256 -key "${CERTDIR}/${domain}/${privkey}" -out "${CERTDIR}/${domain}/cert-${timestamp}.csr" -subj "/CN=${domain}/" -reqexts SAN -config "${tmp_openssl_cnf}" |
50e7a072 NL |
743 | rm -f "${tmp_openssl_cnf}" |
744 | ||
785ffa55 | 745 | crt_path="${CERTDIR}/${domain}/cert-${timestamp}.pem" |
5c68c221 | 746 | # shellcheck disable=SC2086 |
785ffa55 | 747 | sign_csr "$(< "${CERTDIR}/${domain}/cert-${timestamp}.csr" )" ${altnames} 3>"${crt_path}" |
329acb58 LS |
748 | |
749 | # Create fullchain.pem | |
1eb6f6d2 | 750 | echo " + Creating fullchain.pem..." |
785ffa55 | 751 | cat "${crt_path}" > "${CERTDIR}/${domain}/fullchain-${timestamp}.pem" |
6a32f20e | 752 | walk_chain "${crt_path}" > "${CERTDIR}/${domain}/chain-${timestamp}.pem" |
785ffa55 | 753 | cat "${CERTDIR}/${domain}/chain-${timestamp}.pem" >> "${CERTDIR}/${domain}/fullchain-${timestamp}.pem" |
329acb58 | 754 | |
1446fd88 | 755 | # Update symlinks |
785ffa55 | 756 | [[ "${privkey}" = "privkey.pem" ]] || ln -sf "privkey-${timestamp}.pem" "${CERTDIR}/${domain}/privkey.pem" |
f343dc11 | 757 | |
785ffa55 AM |
758 | ln -sf "chain-${timestamp}.pem" "${CERTDIR}/${domain}/chain.pem" |
759 | ln -sf "fullchain-${timestamp}.pem" "${CERTDIR}/${domain}/fullchain.pem" | |
760 | ln -sf "cert-${timestamp}.csr" "${CERTDIR}/${domain}/cert.csr" | |
761 | ln -sf "cert-${timestamp}.pem" "${CERTDIR}/${domain}/cert.pem" | |
f343dc11 | 762 | |
c24843c6 | 763 | # Wait for hook script to clean the challenge and to deploy cert if used |
785ffa55 | 764 | [[ -n "${HOOK}" ]] && "${HOOK}" "deploy_cert" "${domain}" "${CERTDIR}/${domain}/privkey.pem" "${CERTDIR}/${domain}/cert.pem" "${CERTDIR}/${domain}/fullchain.pem" "${CERTDIR}/${domain}/chain.pem" "${timestamp}" |
c24843c6 | 765 | |
766 | unset challenge_token | |
579e2316 | 767 | echo " + Done!" |
61f0b7ed LS |
768 | } |
769 | ||
6a32f20e LS |
770 | # Usage: --register |
771 | # Description: Register account key | |
772 | command_register() { | |
773 | init_system | |
aed4272e | 774 | echo "+ Done!" |
6a32f20e LS |
775 | exit 0 |
776 | } | |
777 | ||
0a859a19 | 778 | # Usage: --cron (-c) |
083c6736 | 779 | # Description: Sign/renew non-existant/changed/expiring certificates. |
8f6c2328 | 780 | command_sign_domains() { |
08678743 | 781 | echo "# INFO: Domain signing started: `date`" |
9f66bfdb LS |
782 | init_system |
783 | ||
8f6c2328 | 784 | if [[ -n "${PARAM_DOMAIN:-}" ]]; then |
1f6a80a0 | 785 | DOMAINS_TXT="$(_mktemp)" |
4d3f3fff | 786 | tmp_domains="yes" |
93cd114f | 787 | printf -- "${PARAM_DOMAIN}" > "${DOMAINS_TXT}" |
a3e5ed36 DB |
788 | elif [[ -e "${DOMAINS_TXT}" ]]; then |
789 | if [[ ! -r "${DOMAINS_TXT}" ]]; then | |
790 | _exiterr "domains.txt found but not readable" | |
791 | fi | |
4d3f3fff AS |
792 | elif [[ -n "${DOMAINS_D}" ]]; then |
793 | DOMAINS_TXT="$(_mktemp)" | |
794 | tmp_domains="yes" | |
795 | find "${DOMAINS_D}" -maxdepth 1 -type f | grep -o '[^/]*$' > "${DOMAINS_TXT}" | |
93cd114f LS |
796 | else |
797 | _exiterr "domains.txt not found and --domain not given" | |
8f6c2328 | 798 | fi |
93cd114f | 799 | |
81882a64 | 800 | # Generate certificates for all domains found in domains.txt. Check if existing certificate are about to expire |
2099c77f LS |
801 | ORIGIFS="${IFS}" |
802 | IFS=$'\n' | |
5d92c3b3 | 803 | 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 |
ec489069 | 804 | reset_configvars |
2099c77f | 805 | IFS="${ORIGIFS}" |
81882a64 | 806 | domain="$(printf '%s\n' "${line}" | cut -d' ' -f1)" |
8f6c2328 | 807 | morenames="$(printf '%s\n' "${line}" | cut -s -d' ' -f2-)" |
785ffa55 | 808 | cert="${CERTDIR}/${domain}/cert.pem" |
f9126627 | 809 | |
2d097c92 MG |
810 | force_renew="${PARAM_FORCE:-no}" |
811 | ||
8f6c2328 MG |
812 | if [[ -z "${morenames}" ]];then |
813 | echo "Processing ${domain}" | |
814 | else | |
93cd114f | 815 | echo "Processing ${domain} with alternative names: ${morenames}" |
8f6c2328 MG |
816 | fi |
817 | ||
ec489069 LS |
818 | # read cert config |
819 | # for now this loads the certificate specific config in a subshell and parses a diff of set variables. | |
820 | # we could just source the config file but i decided to go this way to protect people from accidentally overriding | |
821 | # variables used internally by this script itself. | |
44aca90c MS |
822 | if [[ -n "${DOMAINS_D}" ]]; then |
823 | certconfig="${DOMAINS_D}/${domain}" | |
824 | else | |
825 | certconfig="${CERTDIR}/${domain}/config" | |
826 | fi | |
827 | ||
828 | if [ -f "${certconfig}" ]; then | |
ec489069 LS |
829 | echo " + Using certificate specific config file!" |
830 | ORIGIFS="${IFS}" | |
831 | IFS=$'\n' | |
832 | for cfgline in $( | |
833 | beforevars="$(_mktemp)" | |
834 | aftervars="$(_mktemp)" | |
835 | set > "${beforevars}" | |
836 | # shellcheck disable=SC1090 | |
44aca90c | 837 | . "${certconfig}" |
ec489069 LS |
838 | set > "${aftervars}" |
839 | diff -u "${beforevars}" "${aftervars}" | grep -E '^\+[^+]' | |
840 | rm "${beforevars}" | |
841 | rm "${aftervars}" | |
842 | ); do | |
843 | config_var="$(echo "${cfgline:1}" | cut -d'=' -f1)" | |
844 | config_value="$(echo "${cfgline:1}" | cut -d'=' -f2-)" | |
845 | case "${config_var}" in | |
4d3f3fff AS |
846 | ALT_NAMES) |
847 | config_value="$(echo "${config_value}" | tr '[:upper:]' '[:lower:]' | _sed -e "s/^'[[:space:]]*//g" -e "s/[[:space:]]*'$//g" -e 's/[[:space:]]+/ /g')" | |
848 | ;& | |
a13e4103 | 849 | KEY_ALGO|OCSP_MUST_STAPLE|PRIVATE_KEY_RENEW|PRIVATE_KEY_ROLLOVER|KEYSIZE|CHALLENGETYPE|HOOK|WELLKNOWN|HOOK_CHAIN|OPENSSL_CNF|RENEW_DAYS) |
ec489069 LS |
850 | echo " + ${config_var} = ${config_value}" |
851 | declare -- "${config_var}=${config_value}" | |
852 | ;; | |
853 | _) ;; | |
854 | *) echo " ! Setting ${config_var} on a per-certificate base is not (yet) supported" | |
855 | esac | |
856 | done | |
857 | IFS="${ORIGIFS}" | |
858 | fi | |
4d3f3fff AS |
859 | |
860 | if [[ -n "${ALT_NAMES}" ]]; then | |
861 | if [[ -n "${morenames}" ]]; then | |
862 | morenames="${morenames} ${ALT_NAMES}" | |
863 | else | |
864 | morenames="${ALT_NAMES}" | |
865 | fi | |
866 | line="${domain} ${morenames}"; | |
867 | fi | |
868 | ||
ec489069 | 869 | verify_config |
57197306 | 870 | export WELLKNOWN CHALLENGETYPE KEY_ALGO PRIVATE_KEY_ROLLOVER |
ec489069 | 871 | |
81882a64 | 872 | if [[ -e "${cert}" ]]; then |
93cd114f | 873 | printf " + Checking domain name(s) of existing cert..." |
2d097c92 | 874 | |
f7c7d8c5 LS |
875 | certnames="$(openssl x509 -in "${cert}" -text -noout | grep DNS: | _sed 's/DNS://g' | tr -d ' ' | tr ',' '\n' | sort -u | tr '\n' ' ' | _sed 's/ $//')" |
876 | givennames="$(echo "${domain}" "${morenames}"| tr ' ' '\n' | sort -u | tr '\n' ' ' | _sed 's/ $//' | _sed 's/^ //')" | |
2d097c92 MG |
877 | |
878 | if [[ "${certnames}" = "${givennames}" ]]; then | |
879 | echo " unchanged." | |
880 | else | |
881 | echo " changed!" | |
882 | echo " + Domain name(s) are not matching!" | |
883 | echo " + Names in old certificate: ${certnames}" | |
884 | echo " + Configured names: ${givennames}" | |
885 | echo " + Forcing renew." | |
886 | force_renew="yes" | |
887 | fi | |
888 | fi | |
889 | ||
890 | if [[ -e "${cert}" ]]; then | |
891 | echo " + Checking expire date of existing cert..." | |
81882a64 | 892 | valid="$(openssl x509 -enddate -noout -in "${cert}" | cut -d= -f2- )" |
8221727a | 893 | |
93cd114f | 894 | printf " + Valid till %s " "${valid}" |
81882a64 | 895 | if openssl x509 -checkend $((RENEW_DAYS * 86400)) -noout -in "${cert}"; then |
93cd114f | 896 | printf "(Longer than %d days). " "${RENEW_DAYS}" |
2d097c92 MG |
897 | if [[ "${force_renew}" = "yes" ]]; then |
898 | echo "Ignoring because renew was forced!" | |
8f6c2328 | 899 | else |
705fb54e | 900 | # Certificate-Names unchanged and cert is still valid |
dd33de59 | 901 | echo "Skipping renew!" |
785ffa55 | 902 | [[ -n "${HOOK}" ]] && "${HOOK}" "unchanged_cert" "${domain}" "${CERTDIR}/${domain}/privkey.pem" "${CERTDIR}/${domain}/cert.pem" "${CERTDIR}/${domain}/fullchain.pem" "${CERTDIR}/${domain}/chain.pem" |
8f6c2328 MG |
903 | continue |
904 | fi | |
905 | else | |
906 | echo "(Less than ${RENEW_DAYS} days). Renewing!" | |
81882a64 | 907 | fi |
81882a64 | 908 | fi |
8221727a | 909 | |
81882a64 | 910 | # shellcheck disable=SC2086 |
34565c19 B |
911 | if [[ "${PARAM_KEEP_GOING:-}" = "yes" ]]; then |
912 | sign_domain ${line} & | |
913 | wait $! || true | |
914 | else | |
915 | sign_domain ${line} | |
916 | fi | |
a7934fe7 | 917 | done |
f13eaa7f | 918 | |
8f6c2328 | 919 | # remove temporary domains.txt file if used |
4d3f3fff | 920 | [[ "${tmp_domains:-}" = "yes" ]] && rm -f "${DOMAINS_TXT}" |
93cd114f | 921 | |
298a7e9a | 922 | [[ -n "${HOOK}" ]] && "${HOOK}" "exit_hook" |
93cd114f | 923 | exit 0 |
81882a64 | 924 | } |
3390080c | 925 | |
429ec400 NL |
926 | # Usage: --signcsr (-s) path/to/csr.pem |
927 | # Description: Sign a given CSR, output CRT on stdout (advanced usage) | |
928 | command_sign_csr() { | |
929 | # redirect stdout to stderr | |
930 | # leave stdout over at fd 3 to output the cert | |
931 | exec 3>&1 1>&2 | |
932 | ||
933 | init_system | |
934 | ||
935 | csrfile="${1}" | |
936 | if [ ! -r "${csrfile}" ]; then | |
937 | _exiterr "Could not read certificate signing request ${csrfile}" | |
938 | fi | |
939 | ||
620c7eb2 LS |
940 | # gen cert |
941 | certfile="$(_mktemp)" | |
942 | sign_csr "$(< "${csrfile}" )" 3> "${certfile}" | |
943 | ||
d81eb585 | 944 | # print cert |
620c7eb2 LS |
945 | echo "# CERT #" >&3 |
946 | cat "${certfile}" >&3 | |
947 | echo >&3 | |
d81eb585 LS |
948 | |
949 | # print chain | |
950 | if [ -n "${PARAM_FULL_CHAIN:-}" ]; then | |
951 | # get and convert ca cert | |
952 | chainfile="$(_mktemp)" | |
7eca8aec LS |
953 | tmpchain="$(_mktemp)" |
954 | http_request get "$(openssl x509 -in "${certfile}" -noout -text | grep 'CA Issuers - URI:' | cut -d':' -f2-)" > "${tmpchain}" | |
955 | if grep -q "BEGIN CERTIFICATE" "${tmpchain}"; then | |
956 | mv "${tmpchain}" "${chainfile}" | |
957 | else | |
958 | openssl x509 -in "${tmpchain}" -inform DER -out "${chainfile}" -outform PEM | |
959 | rm "${tmpchain}" | |
d81eb585 LS |
960 | fi |
961 | ||
962 | echo "# CHAIN #" >&3 | |
963 | cat "${chainfile}" >&3 | |
964 | ||
965 | rm "${chainfile}" | |
966 | fi | |
620c7eb2 LS |
967 | |
968 | # cleanup | |
969 | rm "${certfile}" | |
429ec400 NL |
970 | |
971 | exit 0 | |
972 | } | |
973 | ||
0a859a19 LS |
974 | # Usage: --revoke (-r) path/to/cert.pem |
975 | # Description: Revoke specified certificate | |
81882a64 | 976 | command_revoke() { |
9f66bfdb LS |
977 | init_system |
978 | ||
3dcfa8b4 LS |
979 | [[ -n "${CA_REVOKE_CERT}" ]] || _exiterr "Certificate authority doesn't allow certificate revocation." |
980 | ||
81882a64 | 981 | cert="${1}" |
c7018036 MG |
982 | if [[ -L "${cert}" ]]; then |
983 | # follow symlink and use real certificate name (so we move the real file and not the symlink at the end) | |
3bc1cf91 LS |
984 | local link_target |
985 | link_target="$(readlink -n "${cert}")" | |
986 | if [[ "${link_target}" =~ ^/ ]]; then | |
c7018036 MG |
987 | cert="${link_target}" |
988 | else | |
989 | cert="$(dirname "${cert}")/${link_target}" | |
990 | fi | |
991 | fi | |
3dcfa8b4 LS |
992 | [[ -f "${cert}" ]] || _exiterr "Could not find certificate ${cert}" |
993 | ||
81882a64 | 994 | echo "Revoking ${cert}" |
3dcfa8b4 | 995 | |
81882a64 | 996 | cert64="$(openssl x509 -in "${cert}" -inform PEM -outform DER | urlbase64)" |
561f0626 | 997 | response="$(signed_request "${CA_REVOKE_CERT}" '{"resource": "revoke-cert", "certificate": "'"${cert64}"'"}' | clean_json)" |
3dcfa8b4 | 998 | # if there is a problem with our revoke request _request (via signed_request) will report this and "exit 1" out |
81882a64 | 999 | # so if we are here, it is safe to assume the request was successful |
3dcfa8b4 LS |
1000 | echo " + Done." |
1001 | echo " + Renaming certificate to ${cert}-revoked" | |
81882a64 LS |
1002 | mv -f "${cert}" "${cert}-revoked" |
1003 | } | |
c24843c6 | 1004 | |
e60682c0 LS |
1005 | # Usage: --cleanup (-gc) |
1006 | # Description: Move unused certificate files to archive directory | |
1007 | command_cleanup() { | |
dec95fff LS |
1008 | load_config |
1009 | ||
e60682c0 LS |
1010 | # Create global archive directory if not existant |
1011 | if [[ ! -e "${BASEDIR}/archive" ]]; then | |
1012 | mkdir "${BASEDIR}/archive" | |
1013 | fi | |
1014 | ||
1015 | # Loop over all certificate directories | |
785ffa55 | 1016 | for certdir in "${CERTDIR}/"*; do |
f9430025 JB |
1017 | # Skip if entry is not a folder |
1018 | [[ -d "${certdir}" ]] || continue | |
1019 | ||
e60682c0 LS |
1020 | # Get certificate name |
1021 | certname="$(basename "${certdir}")" | |
1022 | ||
1023 | # Create certitifaces archive directory if not existant | |
1024 | archivedir="${BASEDIR}/archive/${certname}" | |
1025 | if [[ ! -e "${archivedir}" ]]; then | |
1026 | mkdir "${archivedir}" | |
1027 | fi | |
1028 | ||
1029 | # Loop over file-types (certificates, keys, signing-requests, ...) | |
1030 | for filetype in cert.csr cert.pem chain.pem fullchain.pem privkey.pem; do | |
1031 | # Skip if symlink is broken | |
1032 | [[ -r "${certdir}/${filetype}" ]] || continue | |
1033 | ||
1034 | # Look up current file in use | |
5c68c221 | 1035 | current="$(basename "$(readlink "${certdir}/${filetype}")")" |
e60682c0 LS |
1036 | |
1037 | # Split filetype into name and extension | |
1038 | filebase="$(echo "${filetype}" | cut -d. -f1)" | |
1039 | fileext="$(echo "${filetype}" | cut -d. -f2)" | |
1040 | ||
1041 | # Loop over all files of this type | |
1042 | for file in "${certdir}/${filebase}-"*".${fileext}"; do | |
ac2d8303 JB |
1043 | # Handle case where no files match the wildcard |
1044 | [[ -f "${file}" ]] || break | |
1045 | ||
e60682c0 LS |
1046 | # Check if current file is in use, if unused move to archive directory |
1047 | filename="$(basename "${file}")" | |
1048 | if [[ ! "${filename}" = "${current}" ]]; then | |
5c68c221 | 1049 | echo "Moving unused file to archive directory: ${certname}/${filename}" |
e60682c0 LS |
1050 | mv "${certdir}/${filename}" "${archivedir}/${filename}" |
1051 | fi | |
1052 | done | |
1053 | done | |
1054 | done | |
1055 | ||
1056 | exit 0 | |
1057 | } | |
1058 | ||
0a859a19 LS |
1059 | # Usage: --help (-h) |
1060 | # Description: Show help text | |
81882a64 | 1061 | command_help() { |
7727f5ea LS |
1062 | printf "Usage: %s [-h] [command [argument]] [parameter [argument]] [parameter [argument]] ...\n\n" "${0}" |
1063 | printf "Default command: help\n\n" | |
0a859a19 | 1064 | echo "Commands:" |
760b6894 | 1065 | grep -e '^[[:space:]]*# Usage:' -e '^[[:space:]]*# Description:' -e '^command_.*()[[:space:]]*{' "${0}" | while read -r usage; read -r description; read -r command; do |
31111265 | 1066 | if [[ ! "${usage}" =~ Usage ]] || [[ ! "${description}" =~ Description ]] || [[ ! "${command}" =~ ^command_ ]]; then |
7727f5ea | 1067 | _exiterr "Error generating help text." |
0a859a19 | 1068 | fi |
7727f5ea | 1069 | printf " %-32s %s\n" "${usage##"# Usage: "}" "${description##"# Description: "}" |
0a859a19 | 1070 | done |
7727f5ea | 1071 | printf -- "\nParameters:\n" |
760b6894 | 1072 | grep -E -e '^[[:space:]]*# PARAM_Usage:' -e '^[[:space:]]*# PARAM_Description:' "${0}" | while read -r usage; read -r description; do |
31111265 | 1073 | if [[ ! "${usage}" =~ Usage ]] || [[ ! "${description}" =~ Description ]]; then |
7727f5ea | 1074 | _exiterr "Error generating help text." |
0a859a19 | 1075 | fi |
7727f5ea | 1076 | printf " %-32s %s\n" "${usage##"# PARAM_Usage: "}" "${description##"# PARAM_Description: "}" |
0a859a19 | 1077 | done |
81882a64 | 1078 | } |
063d28a6 | 1079 | |
1ab6a436 LS |
1080 | # Usage: --env (-e) |
1081 | # Description: Output configuration variables for use in other scripts | |
1082 | command_env() { | |
ec49a443 | 1083 | echo "# dehydrated configuration" |
9f66bfdb | 1084 | load_config |
44aca90c | 1085 | typeset -p CA LICENSE CERTDIR CHALLENGETYPE DOMAINS_D DOMAINS_TXT HOOK HOOK_CHAIN RENEW_DAYS ACCOUNT_KEY ACCOUNT_KEY_JSON KEYSIZE WELLKNOWN PRIVATE_KEY_RENEW OPENSSL_CNF CONTACT_EMAIL LOCKFILE |
1ab6a436 LS |
1086 | } |
1087 | ||
bc580335 | 1088 | # Main method (parses script arguments and calls command_* methods) |
9f66bfdb LS |
1089 | main() { |
1090 | COMMAND="" | |
1091 | set_command() { | |
1092 | [[ -z "${COMMAND}" ]] || _exiterr "Only one command can be executed at a time. See help (-h) for more information." | |
1093 | COMMAND="${1}" | |
1094 | } | |
1095 | ||
1096 | check_parameters() { | |
1097 | if [[ -z "${1:-}" ]]; then | |
1098 | echo "The specified command requires additional parameters. See help:" >&2 | |
31111265 LS |
1099 | echo >&2 |
1100 | command_help >&2 | |
81882a64 | 1101 | exit 1 |
9f66bfdb LS |
1102 | elif [[ "${1:0:1}" = "-" ]]; then |
1103 | _exiterr "Invalid argument: ${1}" | |
1104 | fi | |
1105 | } | |
579e2316 | 1106 | |
2a7b4882 | 1107 | [[ -z "${@}" ]] && eval set -- "--help" |
fb0242a4 | 1108 | |
da2eeda9 | 1109 | while (( ${#} )); do |
9f66bfdb LS |
1110 | case "${1}" in |
1111 | --help|-h) | |
1112 | command_help | |
1113 | exit 0 | |
1114 | ;; | |
579e2316 | 1115 | |
9f66bfdb LS |
1116 | --env|-e) |
1117 | set_command env | |
1118 | ;; | |
579e2316 | 1119 | |
9f66bfdb LS |
1120 | --cron|-c) |
1121 | set_command sign_domains | |
1122 | ;; | |
1123 | ||
6a32f20e LS |
1124 | --register) |
1125 | set_command register | |
1126 | ;; | |
1127 | ||
1128 | # PARAM_Usage: --accept-terms | |
1129 | # PARAM_Description: Accept CAs terms of service | |
1130 | --accept-terms) | |
1131 | PARAM_ACCEPT_TERMS="yes" | |
1132 | ;; | |
1133 | ||
429ec400 NL |
1134 | --signcsr|-s) |
1135 | shift 1 | |
1136 | set_command sign_csr | |
1137 | check_parameters "${1:-}" | |
1138 | PARAM_CSR="${1}" | |
1139 | ;; | |
1140 | ||
9f66bfdb LS |
1141 | --revoke|-r) |
1142 | shift 1 | |
1143 | set_command revoke | |
1144 | check_parameters "${1:-}" | |
1145 | PARAM_REVOKECERT="${1}" | |
1146 | ;; | |
5060dea0 | 1147 | |
e60682c0 LS |
1148 | --cleanup|-gc) |
1149 | set_command cleanup | |
1150 | ;; | |
1151 | ||
d81eb585 LS |
1152 | # PARAM_Usage: --full-chain (-fc) |
1153 | # PARAM_Description: Print full chain when using --signcsr | |
1154 | --full-chain|-fc) | |
1155 | PARAM_FULL_CHAIN="1" | |
1156 | ;; | |
1157 | ||
364bcccf | 1158 | # PARAM_Usage: --ipv4 (-4) |
1159 | # PARAM_Description: Resolve names to IPv4 addresses only | |
1160 | --ipv4|-4) | |
1161 | PARAM_IP_VERSION="4" | |
1162 | ;; | |
1163 | ||
1164 | # PARAM_Usage: --ipv6 (-6) | |
1165 | # PARAM_Description: Resolve names to IPv6 addresses only | |
1166 | --ipv6|-6) | |
1167 | PARAM_IP_VERSION="6" | |
1168 | ;; | |
1169 | ||
8f6c2328 | 1170 | # PARAM_Usage: --domain (-d) domain.tld |
9f66bfdb LS |
1171 | # PARAM_Description: Use specified domain name(s) instead of domains.txt entry (one certificate!) |
1172 | --domain|-d) | |
1173 | shift 1 | |
1174 | check_parameters "${1:-}" | |
1175 | if [[ -z "${PARAM_DOMAIN:-}" ]]; then | |
1176 | PARAM_DOMAIN="${1}" | |
1177 | else | |
1178 | PARAM_DOMAIN="${PARAM_DOMAIN} ${1}" | |
1179 | fi | |
1180 | ;; | |
1181 | ||
34565c19 B |
1182 | # PARAM_Usage: --keep-going (-g) |
1183 | # PARAM_Description: Keep going after encountering an error while creating/renewing multiple certificates in cron mode | |
1184 | --keep-going|-g) | |
1185 | PARAM_KEEP_GOING="yes" | |
1186 | ;; | |
1187 | ||
8f6c2328 | 1188 | # PARAM_Usage: --force (-x) |
9f66bfdb LS |
1189 | # PARAM_Description: Force renew of certificate even if it is longer valid than value in RENEW_DAYS |
1190 | --force|-x) | |
1191 | PARAM_FORCE="yes" | |
1192 | ;; | |
1193 | ||
bd9cc5b0 LS |
1194 | # PARAM_Usage: --no-lock (-n) |
1195 | # PARAM_Description: Don't use lockfile (potentially dangerous!) | |
1196 | --no-lock|-n) | |
1197 | PARAM_NO_LOCK="yes" | |
1198 | ;; | |
1199 | ||
8456855e E |
1200 | # PARAM_Usage: --lock-suffix example.com |
1201 | # PARAM_Description: Suffix lockfile name with a string (useful for with -d) | |
1202 | --lock-suffix) | |
1203 | shift 1 | |
1204 | check_parameters "${1:-}" | |
1205 | PARAM_LOCKFILE_SUFFIX="${1}" | |
1206 | ;; | |
1207 | ||
8e77ba5e LS |
1208 | # PARAM_Usage: --ocsp |
1209 | # PARAM_Description: Sets option in CSR indicating OCSP stapling to be mandatory | |
1210 | --ocsp) | |
1211 | PARAM_OCSP_MUST_STAPLE="yes" | |
1212 | ;; | |
1213 | ||
0a859a19 LS |
1214 | # PARAM_Usage: --privkey (-p) path/to/key.pem |
1215 | # PARAM_Description: Use specified private key instead of account key (useful for revocation) | |
9f66bfdb LS |
1216 | --privkey|-p) |
1217 | shift 1 | |
1218 | check_parameters "${1:-}" | |
8aa1a05b | 1219 | PARAM_ACCOUNT_KEY="${1}" |
9f66bfdb LS |
1220 | ;; |
1221 | ||
d5b28586 | 1222 | # PARAM_Usage: --config (-f) path/to/config |
9f66bfdb LS |
1223 | # PARAM_Description: Use specified config file |
1224 | --config|-f) | |
1225 | shift 1 | |
1226 | check_parameters "${1:-}" | |
1227 | CONFIG="${1}" | |
1228 | ;; | |
1229 | ||
ed27e013 MG |
1230 | # PARAM_Usage: --hook (-k) path/to/hook.sh |
1231 | # PARAM_Description: Use specified script for hooks | |
1232 | --hook|-k) | |
1233 | shift 1 | |
1234 | check_parameters "${1:-}" | |
1235 | PARAM_HOOK="${1}" | |
1236 | ;; | |
1237 | ||
785ffa55 AM |
1238 | # PARAM_Usage: --out (-o) certs/directory |
1239 | # PARAM_Description: Output certificates into the specified directory | |
1240 | --out|-o) | |
1241 | shift 1 | |
1242 | check_parameters "${1:-}" | |
1243 | PARAM_CERTDIR="${1}" | |
1244 | ;; | |
1245 | ||
e925b293 MG |
1246 | # PARAM_Usage: --challenge (-t) http-01|dns-01 |
1247 | # PARAM_Description: Which challenge should be used? Currently http-01 and dns-01 are supported | |
1248 | --challenge|-t) | |
1249 | shift 1 | |
1250 | check_parameters "${1:-}" | |
1251 | PARAM_CHALLENGETYPE="${1}" | |
1252 | ;; | |
1253 | ||
c71ca3a8 MG |
1254 | # PARAM_Usage: --algo (-a) rsa|prime256v1|secp384r1 |
1255 | # PARAM_Description: Which public key algorithm should be used? Supported: rsa, prime256v1 and secp384r1 | |
1256 | --algo|-a) | |
1257 | shift 1 | |
1258 | check_parameters "${1:-}" | |
1259 | PARAM_KEY_ALGO="${1}" | |
1260 | ;; | |
1261 | ||
9f66bfdb LS |
1262 | *) |
1263 | echo "Unknown parameter detected: ${1}" >&2 | |
1264 | echo >&2 | |
1265 | command_help >&2 | |
1266 | exit 1 | |
1267 | ;; | |
1268 | esac | |
1269 | ||
1270 | shift 1 | |
1271 | done | |
1272 | ||
1273 | case "${COMMAND}" in | |
1274 | env) command_env;; | |
1275 | sign_domains) command_sign_domains;; | |
6a32f20e | 1276 | register) command_register;; |
429ec400 | 1277 | sign_csr) command_sign_csr "${PARAM_CSR}";; |
9f66bfdb | 1278 | revoke) command_revoke "${PARAM_REVOKECERT}";; |
e60682c0 | 1279 | cleanup) command_cleanup;; |
7191ed25 | 1280 | *) command_help; exit 1;; |
81882a64 | 1281 | esac |
9f66bfdb | 1282 | } |
81882a64 | 1283 | |
c3c9ff4c LS |
1284 | # Determine OS type |
1285 | OSTYPE="$(uname)" | |
1286 | ||
9f66bfdb LS |
1287 | # Check for missing dependencies |
1288 | check_dependencies | |
1289 | ||
1290 | # Run script | |
1291 | main "${@:-}" |