]>
Commit | Line | Data |
---|---|---|
2e8454b4 | 1 | #!/usr/bin/env bash |
61f0b7ed | 2 | |
69f3e78b LS |
3 | set -e |
4 | set -u | |
5 | set -o pipefail | |
81882a64 | 6 | umask 077 # paranoid umask, we're creating private keys |
61f0b7ed | 7 | |
16943702 LS |
8 | # Get the directory in which this script is stored |
9 | SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
10 | ||
0e92aba2 MG |
11 | # directory for config, private key and certificates |
12 | BASEDIR="${SCRIPTDIR}" | |
13 | ||
401f5f75 | 14 | # Default config values |
09729186 | 15 | CA="https://acme-v01.api.letsencrypt.org/directory" |
00a0937c | 16 | LICENSE="https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf" |
c24843c6 | 17 | HOOK= |
e300c0a1 | 18 | RENEW_DAYS="14" |
1e33cfe5 | 19 | PRIVATE_KEY= |
5a213f5f | 20 | KEYSIZE="4096" |
1e33cfe5 | 21 | WELLKNOWN= |
16943702 | 22 | PRIVATE_KEY_RENEW="no" |
474f33d2 | 23 | OPENSSL_CNF="$(openssl version -d | cut -d'"' -f2)/openssl.cnf" |
ea5b70a3 | 24 | CONTACT_EMAIL= |
f11bb1db | 25 | |
1e33cfe5 AB |
26 | set_defaults() { |
27 | # Default config variables depending on BASEDIR | |
28 | if [[ -z "${PRIVATE_KEY}" ]]; then | |
29 | PRIVATE_KEY="${BASEDIR}/private_key.pem" | |
30 | fi | |
31 | if [[ -z "${WELLKNOWN}" ]]; then | |
32 | WELLKNOWN="${BASEDIR}/.acme-challenges" | |
33 | fi | |
34 | ||
35 | LOCKFILE="${BASEDIR}/lock" | |
36 | } | |
37 | ||
81882a64 LS |
38 | init_system() { |
39 | # Check for config in various locations | |
40 | if [[ -z "${CONFIG:-}" ]]; then | |
5120dfb5 | 41 | for check_config in "${HOME}/.letsencrypt.sh" "/etc/letsencrypt.sh" "/usr/local/etc/letsencrypt.sh" "${PWD}" "${SCRIPTDIR}"; do |
81882a64 LS |
42 | if [[ -e "${check_config}/config.sh" ]]; then |
43 | BASEDIR="${check_config}" | |
44 | CONFIG="${check_config}/config.sh" | |
45 | break | |
46 | fi | |
47 | done | |
454c164b | 48 | fi |
454c164b | 49 | |
81882a64 LS |
50 | if [[ -z "${CONFIG:-}" ]]; then |
51 | echo "WARNING: No config file found, using default config!" | |
52 | sleep 2 | |
53 | elif [[ -e "${CONFIG}" ]]; then | |
1ab6a436 LS |
54 | if [[ ! "${COMMAND}" = "env" ]]; then |
55 | echo "Using config file ${CONFIG}" | |
56 | fi | |
81882a64 LS |
57 | BASEDIR="$(dirname "${CONFIG}")" |
58 | # shellcheck disable=SC1090 | |
59 | . "${CONFIG}" | |
60 | else | |
61 | echo "ERROR: Specified config file doesn't exist." | |
62 | exit 1 | |
63 | fi | |
61f0b7ed | 64 | |
81882a64 LS |
65 | # Remove slash from end of BASEDIR. Mostly for cleaner outputs, doesn't change functionality. |
66 | BASEDIR="${BASEDIR%%/}" | |
401f5f75 | 67 | |
1e33cfe5 AB |
68 | # Check BASEDIR and set default variables |
69 | if [[ ! -d "${BASEDIR}" ]]; then | |
70 | echo "ERROR: BASEDIR does not exist: ${BASEDIR}" | |
71 | exit 1 | |
72 | fi | |
73 | set_defaults | |
81882a64 | 74 | |
1ab6a436 LS |
75 | if [[ "${COMMAND}" = "env" ]]; then |
76 | return | |
77 | fi | |
78 | ||
1e33cfe5 | 79 | # Lockfile handling (prevents concurrent access) |
81882a64 LS |
80 | set -o noclobber |
81 | if ! { date > "${LOCKFILE}"; } 2>/dev/null; then | |
82 | echo " + ERROR: Lock file '${LOCKFILE}' present, aborting." >&2 | |
10d9f342 | 83 | LOCKFILE="" # so remove_lock doesn't remove it |
81882a64 LS |
84 | exit 1 |
85 | fi | |
86 | set +o noclobber | |
87 | ||
88 | remove_lock() { | |
89 | if [[ -n "${LOCKFILE}" ]]; then | |
90 | rm -f "${LOCKFILE}" | |
91 | fi | |
92 | } | |
93 | trap 'remove_lock' EXIT | |
94 | ||
95 | # Export some environment variables to be used in hook script | |
96 | export WELLKNOWN | |
97 | export BASEDIR | |
98 | export CONFIG | |
99 | ||
100 | # Get CA URLs | |
101 | CA_DIRECTORY="$(_request get "${CA}")" | |
102 | CA_NEW_CERT="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value new-cert)" && | |
103 | CA_NEW_AUTHZ="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value new-authz)" && | |
104 | CA_NEW_REG="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value new-reg)" && | |
10d9f342 | 105 | # shellcheck disable=SC2015 |
81882a64 LS |
106 | CA_REVOKE_CERT="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value revoke-cert)" || |
107 | (echo "Error retrieving ACME/CA-URLs, check if your configured CA points to the directory entrypoint."; exit 1) | |
108 | ||
0e92aba2 MG |
109 | |
110 | # check private key ... | |
81882a64 | 111 | register="0" |
0e92aba2 MG |
112 | if [[ -n "${PARAM_PRIVATE_KEY:-}" ]]; then |
113 | # a private key was specified from the command line so use it for this run | |
10d9f342 | 114 | echo "Using private key ${PARAM_PRIVATE_KEY} instead of account key" |
0e92aba2 MG |
115 | PRIVATE_KEY="${PARAM_PRIVATE_KEY}" |
116 | if ! openssl rsa -in "${PRIVATE_KEY}" -check 2>/dev/null > /dev/null; then | |
117 | echo " + ERROR: private key is not valid, can not continue" | |
118 | exit 1 | |
119 | fi | |
120 | else | |
121 | # Check if private account key exists, if it doesn't exist yet generate a new one (rsa key) | |
0e92aba2 | 122 | if [[ ! -e "${PRIVATE_KEY}" ]]; then |
81882a64 | 123 | echo "+ Generating account key..." |
0e92aba2 | 124 | _openssl genrsa -out "${PRIVATE_KEY}" "${KEYSIZE}" |
81882a64 LS |
125 | register="1" |
126 | fi | |
81882a64 | 127 | fi |
1ab6a436 | 128 | |
81882a64 | 129 | # Get public components from private key and calculate thumbprint |
0e92aba2 MG |
130 | pubExponent64="$(printf "%06x" "$(openssl rsa -in "${PRIVATE_KEY}" -noout -text | grep publicExponent | head -1 | cut -d' ' -f2)" | hex2bin | urlbase64)" |
131 | pubMod64="$(printf '%s' "$(openssl rsa -in "${PRIVATE_KEY}" -noout -modulus | cut -d'=' -f2)" | hex2bin | urlbase64)" | |
81882a64 | 132 | |
6816216f | 133 | thumbprint="$(printf '%s' '{"e":"'"${pubExponent64}"'","kty":"RSA","n":"'"${pubMod64}"'"}' | openssl sha -sha256 -binary | urlbase64)" |
81882a64 LS |
134 | |
135 | # If we generated a new private key in the step above we have to register it with the acme-server | |
136 | if [[ "${register}" = "1" ]]; then | |
137 | echo "+ Registering account key with letsencrypt..." | |
138 | if [ -z "${CA_NEW_REG}" ]; then | |
139 | echo " + ERROR: Certificate authority doesn't allow registrations." | |
140 | exit 1 | |
141 | fi | |
142 | # if an email for the contact has been provided then adding it to the registration request | |
143 | if [[ -n "${CONTACT_EMAIL}" ]]; then | |
144 | signed_request "${CA_NEW_REG}" '{"resource": "new-reg", "contact":["mailto:'"${CONTACT_EMAIL}"'"], "agreement": "'"$LICENSE"'"}' > /dev/null | |
145 | else | |
146 | signed_request "${CA_NEW_REG}" '{"resource": "new-reg", "agreement": "'"$LICENSE"'"}' > /dev/null | |
147 | fi | |
148 | fi | |
181dd0ff | 149 | |
81882a64 LS |
150 | if [[ -e "${BASEDIR}/domains.txt" ]]; then |
151 | DOMAINS_TXT="${BASEDIR}/domains.txt" | |
81882a64 LS |
152 | else |
153 | echo "You have to create a domains.txt file listing the domains you want certificates for. Have a look at domains.txt.example." | |
154 | exit 1 | |
155 | fi | |
156 | ||
157 | if [[ ! -e "${WELLKNOWN}" ]]; then | |
158 | mkdir -p "${WELLKNOWN}" | |
159 | fi | |
160 | } | |
c24843c6 | 161 | |
c6e60302 LS |
162 | anti_newline() { |
163 | tr -d '\n\r' | |
164 | } | |
181dd0ff | 165 | |
61f0b7ed | 166 | urlbase64() { |
c6e60302 | 167 | # urlbase64: base64 encoded string with '+' replaced with '-' and '/' replaced with '_' |
81cb6ac7 | 168 | openssl base64 -e | anti_newline | sed 's/=*$//g' | tr '+/' '-_' |
61f0b7ed | 169 | } |
91ce50af | 170 | |
9fe313d8 | 171 | hex2bin() { |
c6e60302 | 172 | # Store hex string from stdin |
20e7d9d7 LS |
173 | tmphex="$(cat)" |
174 | ||
c6e60302 | 175 | # Remove spaces |
20e7d9d7 LS |
176 | hex='' |
177 | for ((i=0; i<${#tmphex}; i+=1)); do | |
178 | test "${tmphex:$i:1}" == " " || hex="${hex}${tmphex:$i:1}" | |
179 | done | |
180 | ||
c6e60302 | 181 | # Add leading zero |
20e7d9d7 LS |
182 | test $((${#hex} & 1)) == 0 || hex="0${hex}" |
183 | ||
c6e60302 | 184 | # Convert to escaped string |
20e7d9d7 LS |
185 | escapedhex='' |
186 | for ((i=0; i<${#hex}; i+=2)); do | |
187 | escapedhex=$escapedhex\\x${hex:$i:2} | |
188 | done | |
189 | ||
c6e60302 | 190 | # Convert to binary data |
219b3e9d | 191 | printf -- "${escapedhex}" |
9fe313d8 | 192 | } |
61f0b7ed | 193 | |
09729186 LS |
194 | get_json_string_value() { |
195 | grep -Eo '"'"${1}"'":\s*"[^"]*"' | cut -d'"' -f4 | |
196 | } | |
197 | ||
198 | get_json_array() { | |
199 | grep -Eo '"'"${1}"'":[^\[]*\[[^]]*]' | |
200 | } | |
201 | ||
91ce50af | 202 | _request() { |
3cb292cb LS |
203 | tempcont="$(mktemp)" |
204 | ||
dd5f36e5 | 205 | if [[ "${1}" = "head" ]]; then |
3cb292cb | 206 | statuscode="$(curl -s -w "%{http_code}" -o "${tempcont}" "${2}" -I)" |
dd5f36e5 | 207 | elif [[ "${1}" = "get" ]]; then |
3cb292cb | 208 | statuscode="$(curl -s -w "%{http_code}" -o "${tempcont}" "${2}")" |
dd5f36e5 | 209 | elif [[ "${1}" = "post" ]]; then |
3cb292cb | 210 | statuscode="$(curl -s -w "%{http_code}" -o "${tempcont}" "${2}" -d "${3}")" |
91ce50af | 211 | fi |
dd5f36e5 | 212 | |
3cb292cb | 213 | if [[ ! "${statuscode:0:1}" = "2" ]]; then |
84fac541 | 214 | echo " + ERROR: An error occurred while sending ${1}-request to ${2} (Status ${statuscode})" >&2 |
3cb292cb LS |
215 | echo >&2 |
216 | echo "Details:" >&2 | |
217 | echo "$(<"${tempcont}"))" >&2 | |
218 | rm -f "${tempcont}" | |
c24843c6 | 219 | |
220 | # Wait for hook script to clean the challenge if used | |
0b2119c4 | 221 | if [[ -n "${HOOK}" ]] && [[ -n "${challenge_token:+set}" ]]; then |
e32ea24c | 222 | ${HOOK} "clean_challenge" '' "${challenge_token}" "${keyauth}" |
c24843c6 | 223 | fi |
224 | ||
8f6c2328 MG |
225 | # remove temporary domains.txt file if used |
226 | if [[ -n "${PARAM_DOMAIN:-}" ]]; then | |
227 | rm "${DOMAINS_TXT}" | |
228 | fi | |
229 | ||
dd5f36e5 | 230 | exit 1 |
130ea6ab | 231 | fi |
dd5f36e5 | 232 | |
3cb292cb LS |
233 | cat "${tempcont}" |
234 | rm -f "${tempcont}" | |
91ce50af | 235 | } |
81882a64 LS |
236 | |
237 | # OpenSSL writes to stderr/stdout even when there are no errors. So just | |
238 | # display the output if the exit code was != 0 to simplify debugging. | |
239 | _openssl() { | |
7f8ea450 | 240 | set +e |
10d9f342 | 241 | out="$(openssl "${@}" 2>&1)" |
7f8ea450 SR |
242 | res=$? |
243 | set -e | |
244 | if [[ $res -ne 0 ]]; then | |
245 | echo " + ERROR: failed to run $* (Exitcode: $res)" >&2 | |
246 | echo >&2 | |
247 | echo "Details:" >&2 | |
248 | echo "$out" >&2 | |
249 | exit $res | |
250 | fi | |
251 | } | |
91ce50af | 252 | |
61f0b7ed | 253 | signed_request() { |
c6e60302 | 254 | # Encode payload as urlbase64 |
4aa48d33 | 255 | payload64="$(printf '%s' "${2}" | urlbase64)" |
61f0b7ed | 256 | |
c6e60302 | 257 | # Retrieve nonce from acme-server |
09729186 | 258 | nonce="$(_request head "${CA}" | grep Replay-Nonce: | awk -F ': ' '{print $2}' | anti_newline)" |
61f0b7ed | 259 | |
c6e60302 | 260 | # Build header with just our public key and algorithm information |
61f0b7ed LS |
261 | header='{"alg": "RS256", "jwk": {"e": "'"${pubExponent64}"'", "kty": "RSA", "n": "'"${pubMod64}"'"}}' |
262 | ||
c6e60302 | 263 | # Build another header which also contains the previously received nonce and encode it as urlbase64 |
61f0b7ed | 264 | protected='{"alg": "RS256", "jwk": {"e": "'"${pubExponent64}"'", "kty": "RSA", "n": "'"${pubMod64}"'"}, "nonce": "'"${nonce}"'"}' |
4aa48d33 | 265 | protected64="$(printf '%s' "${protected}" | urlbase64)" |
61f0b7ed | 266 | |
c6e60302 | 267 | # Sign header with nonce and our payload with our private key and encode signature as urlbase64 |
0e92aba2 | 268 | signed64="$(printf '%s' "${protected64}.${payload64}" | openssl dgst -sha256 -sign "${PRIVATE_KEY}" | urlbase64)" |
61f0b7ed | 269 | |
c6e60302 | 270 | # Send header + extended header + payload + signature to the acme-server |
61f0b7ed LS |
271 | data='{"header": '"${header}"', "protected": "'"${protected64}"'", "payload": "'"${payload64}"'", "signature": "'"${signed64}"'"}' |
272 | ||
91ce50af | 273 | _request post "${1}" "${data}" |
61f0b7ed LS |
274 | } |
275 | ||
61f0b7ed LS |
276 | sign_domain() { |
277 | domain="${1}" | |
1f65a335 | 278 | altnames="${*}" |
81882a64 | 279 | |
579e2316 | 280 | echo " + Signing domains..." |
09729186 LS |
281 | if [[ -z "${CA_NEW_AUTHZ}" ]] || [[ -z "${CA_NEW_CERT}" ]]; then |
282 | echo " + ERROR: Certificate authority doesn't allow certificate signing" | |
283 | exit 1 | |
284 | fi | |
3cc587c2 LS |
285 | timestamp="$(date +%s)" |
286 | ||
3dbbb461 | 287 | # If there is no existing certificate directory => make it |
5b29db97 AJM |
288 | if [[ ! -e "${BASEDIR}/certs/${domain}" ]]; then |
289 | echo " + make directory ${BASEDIR}/certs/${domain} ..." | |
290 | mkdir -p "${BASEDIR}/certs/${domain}" | |
3dbbb461 MG |
291 | fi |
292 | ||
f343dc11 | 293 | privkey="privkey.pem" |
3dbbb461 | 294 | # generate a new private key if we need or want one |
5b29db97 | 295 | if [[ ! -f "${BASEDIR}/certs/${domain}/privkey.pem" ]] || [[ "${PRIVATE_KEY_RENEW}" = "yes" ]]; then |
579e2316 | 296 | echo " + Generating private key..." |
f343dc11 | 297 | privkey="privkey-${timestamp}.pem" |
7f8ea450 | 298 | _openssl genrsa -out "${BASEDIR}/certs/${domain}/privkey-${timestamp}.pem" "${KEYSIZE}" |
61f0b7ed LS |
299 | fi |
300 | ||
c6e60302 LS |
301 | # Generate signing request config and the actual signing request |
302 | SAN="" | |
61f0b7ed | 303 | for altname in $altnames; do |
c6e60302 LS |
304 | SAN+="DNS:${altname}, " |
305 | done | |
cd13a9c2 | 306 | SAN="${SAN%%, }" |
579e2316 | 307 | echo " + Generating signing request..." |
7f8ea450 | 308 | openssl req -new -sha256 -key "${BASEDIR}/certs/${domain}/${privkey}" -out "${BASEDIR}/certs/${domain}/cert-${timestamp}.csr" -subj "/CN=${domain}/" -reqexts SAN -config <(cat "${OPENSSL_CNF}" <(printf "[SAN]\nsubjectAltName=%s" "${SAN}")) |
c6e60302 LS |
309 | |
310 | # Request and respond to challenges | |
311 | for altname in $altnames; do | |
312 | # Ask the acme-server for new challenge token and extract them from the resulting json block | |
579e2316 | 313 | echo " + Requesting challenge for ${altname}..." |
09729186 | 314 | response="$(signed_request "${CA_NEW_AUTHZ}" '{"resource": "new-authz", "identifier": {"type": "dns", "value": "'"${altname}"'"}}')" |
61f0b7ed | 315 | |
09729186 | 316 | challenges="$(printf '%s\n' "${response}" | get_json_array challenges)" |
526843d6 SR |
317 | repl=$'\n''{' # fix syntax highlighting in Vim |
318 | challenge="$(printf "%s" "${challenges//\{/${repl}}" | grep 'http-01')" | |
09729186 LS |
319 | challenge_token="$(printf '%s' "${challenge}" | get_json_string_value token | sed 's/[^A-Za-z0-9_\-]/_/g')" |
320 | challenge_uri="$(printf '%s' "${challenge}" | get_json_string_value uri)" | |
61f0b7ed | 321 | |
dd5f36e5 | 322 | if [[ -z "${challenge_token}" ]] || [[ -z "${challenge_uri}" ]]; then |
a1621214 | 323 | echo " + Error: Can't retrieve challenges (${response})" |
abb95693 LS |
324 | exit 1 |
325 | fi | |
326 | ||
c6e60302 | 327 | # Challenge response consists of the challenge token and the thumbprint of our public certificate |
61f0b7ed LS |
328 | keyauth="${challenge_token}.${thumbprint}" |
329 | ||
c6e60302 | 330 | # Store challenge response in well-known location and make world-readable (so that a webserver can access it) |
4aa48d33 | 331 | printf '%s' "${keyauth}" > "${WELLKNOWN}/${challenge_token}" |
2b5df371 | 332 | chmod a+r "${WELLKNOWN}/${challenge_token}" |
61f0b7ed | 333 | |
b33f1288 | 334 | # Wait for hook script to deploy the challenge if used |
c24843c6 | 335 | if [[ -n "${HOOK}" ]]; then |
e32ea24c | 336 | ${HOOK} "deploy_challenge" "${altname}" "${challenge_token}" "${keyauth}" |
b33f1288 SR |
337 | fi |
338 | ||
c6e60302 | 339 | # Ask the acme-server to verify our challenge and wait until it becomes valid |
579e2316 | 340 | echo " + Responding to challenge for ${altname}..." |
61f0b7ed LS |
341 | result="$(signed_request "${challenge_uri}" '{"resource": "challenge", "keyAuthorization": "'"${keyauth}"'"}')" |
342 | ||
09729186 | 343 | status="$(printf '%s\n' "${result}" | get_json_string_value status)" |
61f0b7ed | 344 | |
cbe1eb2c | 345 | # get status until a result is reached => not pending anymore |
dd5f36e5 | 346 | while [[ "${status}" = "pending" ]]; do |
c6e60302 | 347 | sleep 1 |
09729186 | 348 | status="$(_request get "${challenge_uri}" | get_json_string_value status)" |
61f0b7ed LS |
349 | done |
350 | ||
00837b86 | 351 | rm -f "${WELLKNOWN}/${challenge_token}" |
81882a64 | 352 | |
ab301951 E |
353 | # Wait for hook script to clean the challenge if used |
354 | if [[ -n "${HOOK}" ]] && [[ -n "${challenge_token}" ]]; then | |
355 | ${HOOK} "clean_challenge" "${altname}" "${challenge_token}" "${keyauth}" | |
356 | fi | |
81882a64 | 357 | |
76a37834 | 358 | if [[ "${status}" = "valid" ]]; then |
579e2316 | 359 | echo " + Challenge is valid!" |
76a37834 | 360 | else |
579e2316 | 361 | echo " + Challenge is invalid! (returned: ${status})" |
76a37834 MG |
362 | exit 1 |
363 | fi | |
364 | ||
61f0b7ed LS |
365 | done |
366 | ||
b7439a83 | 367 | # Finally request certificate from the acme-server and store it in cert-${timestamp}.pem and link from cert.pem |
579e2316 | 368 | echo " + Requesting certificate..." |
f343dc11 | 369 | csr64="$(openssl req -in "${BASEDIR}/certs/${domain}/cert-${timestamp}.csr" -outform DER | urlbase64)" |
09729186 | 370 | crt64="$(signed_request "${CA_NEW_CERT}" '{"resource": "new-cert", "csr": "'"${csr64}"'"}' | openssl base64 -e)" |
85da9090 SR |
371 | crt_path="${BASEDIR}/certs/${domain}/cert-${timestamp}.pem" |
372 | printf -- '-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----\n' "${crt64}" > "${crt_path}" | |
10cf2299 SR |
373 | # try to load the certificate to detect corruption |
374 | echo " + Checking certificate..." >&2 | |
375 | _openssl x509 -text < "${crt_path}" | |
329acb58 LS |
376 | |
377 | # Create fullchain.pem | |
1eb6f6d2 LS |
378 | echo " + Creating fullchain.pem..." |
379 | cat "${crt_path}" > "${BASEDIR}/certs/${domain}/fullchain-${timestamp}.pem" | |
380 | _request get "$(openssl x509 -in "${BASEDIR}/certs/${domain}/cert-${timestamp}.pem" -noout -text | grep 'CA Issuers - URI:' | cut -d':' -f2-)" >> "${BASEDIR}/certs/${domain}/fullchain-${timestamp}.pem" | |
381 | ln -sf "fullchain-${timestamp}.pem" "${BASEDIR}/certs/${domain}/fullchain.pem" | |
329acb58 | 382 | |
f343dc11 LS |
383 | # Update remaining symlinks |
384 | if [ ! "${privkey}" = "privkey.pem" ]; then | |
3f6ff8f7 | 385 | ln -sf "privkey-${timestamp}.pem" "${BASEDIR}/certs/${domain}/privkey.pem" |
f343dc11 LS |
386 | fi |
387 | ||
3f6ff8f7 SR |
388 | ln -sf "cert-${timestamp}.csr" "${BASEDIR}/certs/${domain}/cert.csr" |
389 | ln -sf "cert-${timestamp}.pem" "${BASEDIR}/certs/${domain}/cert.pem" | |
f343dc11 | 390 | |
c24843c6 | 391 | # Wait for hook script to clean the challenge and to deploy cert if used |
392 | if [[ -n "${HOOK}" ]]; then | |
e32ea24c | 393 | ${HOOK} "deploy_cert" "${domain}" "${BASEDIR}/certs/${domain}/privkey.pem" "${BASEDIR}/certs/${domain}/cert.pem" "${BASEDIR}/certs/${domain}/fullchain.pem" |
c24843c6 | 394 | fi |
395 | ||
396 | unset challenge_token | |
579e2316 | 397 | echo " + Done!" |
61f0b7ed LS |
398 | } |
399 | ||
f9126627 | 400 | |
0a859a19 LS |
401 | # Usage: --cron (-c) |
402 | # Description: Sign/renew non-existant/changed(TODO)/expiring certificates. | |
8f6c2328 MG |
403 | command_sign_domains() { |
404 | if [[ -n "${PARAM_DOMAIN:-}" ]]; then | |
405 | # we are using a temporary domains.txt file so we don't need to duplicate any code | |
406 | DOMAINS_TXT="$(mktemp)" | |
407 | echo "${PARAM_DOMAIN}" > "${DOMAINS_TXT}" | |
408 | fi | |
81882a64 LS |
409 | # Generate certificates for all domains found in domains.txt. Check if existing certificate are about to expire |
410 | <"${DOMAINS_TXT}" sed 's/^\s*//g;s/\s*$//g' | grep -v '^#' | grep -v '^$' | while read -r line; do | |
411 | domain="$(printf '%s\n' "${line}" | cut -d' ' -f1)" | |
8f6c2328 | 412 | morenames="$(printf '%s\n' "${line}" | cut -s -d' ' -f2-)" |
81882a64 | 413 | cert="${BASEDIR}/certs/${domain}/cert.pem" |
f9126627 | 414 | |
8f6c2328 MG |
415 | if [[ -z "${morenames}" ]];then |
416 | echo "Processing ${domain}" | |
417 | else | |
418 | echo "Processing ${domain} with SAN: ${morenames}" | |
419 | fi | |
420 | ||
81882a64 LS |
421 | if [[ -e "${cert}" ]]; then |
422 | echo " + Found existing cert..." | |
8221727a | 423 | |
81882a64 | 424 | valid="$(openssl x509 -enddate -noout -in "${cert}" | cut -d= -f2- )" |
8221727a | 425 | |
81882a64 LS |
426 | echo -n " + Valid till ${valid} " |
427 | if openssl x509 -checkend $((RENEW_DAYS * 86400)) -noout -in "${cert}"; then | |
8f6c2328 MG |
428 | echo -n "(Longer than ${RENEW_DAYS} days). " |
429 | if [[ "${PARAM_FORCE:-}" = "yes" ]]; then | |
430 | echo "Ignoring because --force was specified!" | |
431 | else | |
432 | echo "Skipping!" | |
433 | continue | |
434 | fi | |
435 | else | |
436 | echo "(Less than ${RENEW_DAYS} days). Renewing!" | |
81882a64 | 437 | fi |
81882a64 | 438 | fi |
8221727a | 439 | |
81882a64 LS |
440 | # shellcheck disable=SC2086 |
441 | sign_domain $line | |
442 | done | |
f13eaa7f | 443 | |
8f6c2328 MG |
444 | # remove temporary domains.txt file if used |
445 | if [[ -n "${PARAM_DOMAIN:-}" ]]; then | |
446 | rm "${DOMAINS_TXT}" | |
447 | fi | |
81882a64 | 448 | } |
3390080c | 449 | |
0a859a19 LS |
450 | # Usage: --revoke (-r) path/to/cert.pem |
451 | # Description: Revoke specified certificate | |
81882a64 LS |
452 | command_revoke() { |
453 | cert="${1}" | |
454 | echo "Revoking ${cert}" | |
455 | if [ -z "${CA_REVOKE_CERT}" ]; then | |
456 | echo " + ERROR: Certificate authority doesn't allow certificate revocation." | |
063d28a6 MG |
457 | exit 1 |
458 | fi | |
81882a64 LS |
459 | cert64="$(openssl x509 -in "${cert}" -inform PEM -outform DER | urlbase64)" |
460 | response="$(signed_request "${CA_REVOKE_CERT}" '{"resource": "revoke-cert", "certificate": "'"${cert64}"'"}')" | |
461 | # if there is a problem with our revoke request _request (via signed_request) will report this and "exit 1" out | |
462 | # so if we are here, it is safe to assume the request was successful | |
463 | echo " + SUCCESS" | |
464 | echo " + renaming certificate to ${cert}-revoked" | |
465 | mv -f "${cert}" "${cert}-revoked" | |
466 | } | |
c24843c6 | 467 | |
0a859a19 LS |
468 | # Usage: --help (-h) |
469 | # Description: Show help text | |
81882a64 | 470 | command_help() { |
0a859a19 | 471 | echo "Usage: ${0} [-h] [command [argument]] [parameter [argument]] [parameter [argument]] ..." |
81882a64 | 472 | echo |
0a859a19 LS |
473 | echo "Default command: cron" |
474 | echo | |
475 | ( | |
476 | echo "Commands:" | |
1ab6a436 | 477 | grep -e '^\s*# Usage:' -e '^\s*# Description:' -e '^command_.*()\s*{' "${0}" | while read -r usage; read -r description; read -r command; do |
0a859a19 LS |
478 | if [[ ! "${usage}" =~ Usage ]]; then |
479 | echo "Error generating help text." | |
480 | exit 1 | |
481 | elif [[ ! "${description}" =~ Description ]]; then | |
482 | echo "Error generating help text." | |
483 | exit 1 | |
484 | elif [[ ! "${command}" =~ ^command_ ]]; then | |
485 | echo "Error generating help text." | |
486 | exit 1 | |
487 | fi | |
488 | printf " %s\t%s\n" "${usage##"# Usage: "}" "${description##"# Description: "}" | |
489 | done | |
490 | echo "---" | |
491 | echo "Parameters:" | |
8d009505 | 492 | grep -E -e '^\s*# PARAM_Usage:' -e '^\s*# PARAM_Description:' "${0}" | while read -r usage; read -r description; do |
0a859a19 LS |
493 | if [[ ! "${usage}" =~ Usage ]]; then |
494 | echo "Error generating help text." | |
495 | exit 1 | |
496 | elif [[ ! "${description}" =~ Description ]]; then | |
497 | echo "Error generating help text." | |
498 | exit 1 | |
499 | fi | |
500 | printf " %s\t%s\n" "${usage##"# PARAM_Usage: "}" "${description##"# PARAM_Description: "}" | |
501 | done | |
502 | ) | column -t -s $'\t' | sed 's/^---$//g' | |
81882a64 | 503 | } |
063d28a6 | 504 | |
1ab6a436 LS |
505 | # Usage: --env (-e) |
506 | # Description: Output configuration variables for use in other scripts | |
507 | command_env() { | |
508 | echo "# letsencrypt.sh configuration" | |
3ba233de | 509 | typeset -p CONFIG |
1eb6f6d2 | 510 | typeset -p CA LICENSE BASEDIR WELLKNOWN PRIVATE_KEY KEYSIZE OPENSSL_CNF HOOK RENEW_DAYS PRIVATE_KEY_RENEW CONTACT_EMAIL |
1ab6a436 LS |
511 | exit 0 |
512 | } | |
513 | ||
81882a64 LS |
514 | args="" |
515 | # change long args to short args | |
516 | # inspired by http://kirk.webfinish.com/?p=45 | |
517 | for arg; do | |
518 | case "${arg}" in | |
519 | --help) args="${args}-h ";; | |
520 | --cron) args="${args}-c ";; | |
8f6c2328 MG |
521 | --domain) args="${args}-d ";; |
522 | --force ) args="${args}-x ";; | |
81882a64 | 523 | --revoke) args="${args}-r ";; |
0e92aba2 | 524 | --privkey) args="${args}-p ";; |
81882a64 | 525 | --config) args="${args}-f ";; |
1ab6a436 | 526 | --env) args="${args}-e ";; |
81882a64 LS |
527 | --*) |
528 | echo "Unknown parameter detected: ${arg}" | |
529 | echo | |
530 | command_help | |
531 | exit 1 | |
532 | ;; | |
533 | # pass through anything else | |
534 | *) args="${args}\"${arg}\" ";; | |
535 | esac | |
536 | done | |
579e2316 | 537 | |
81882a64 | 538 | # Reset the positional parameters to the short options |
10d9f342 | 539 | eval set -- "${args}" |
579e2316 | 540 | |
81882a64 LS |
541 | COMMAND="" |
542 | set_command() { | |
543 | if [[ ! -z "${COMMAND}" ]]; then | |
544 | echo "Only one command can be executed at a time." | |
545 | echo "See help (-h) for more information." | |
546 | exit 1 | |
547 | fi | |
548 | COMMAND="${1}" | |
549 | } | |
579e2316 | 550 | |
81882a64 LS |
551 | check_parameters() { |
552 | if [[ -z "${@}" ]]; then | |
553 | echo "The specified command requires additional parameters. See help:" | |
554 | echo | |
555 | command_help | |
556 | exit 1 | |
5060dea0 | 557 | fi |
81882a64 | 558 | } |
5060dea0 | 559 | |
8f6c2328 | 560 | while getopts ":hcer:d:xf:p:" option; do |
81882a64 LS |
561 | case "${option}" in |
562 | h) | |
563 | command_help | |
564 | exit 0 | |
565 | ;; | |
566 | c) | |
567 | set_command cron | |
568 | ;; | |
1ab6a436 LS |
569 | e) |
570 | set_command env | |
571 | ;; | |
81882a64 LS |
572 | r) |
573 | set_command revoke | |
574 | check_parameters "${OPTARG:-}" | |
575 | revoke_me="${OPTARG}" | |
576 | ;; | |
8f6c2328 MG |
577 | d) |
578 | # PARAM_Usage: --domain (-d) domain.tld | |
579 | # PARAM_Description: Use specified domain name instead of domains.txt, use multiple times for certificate with SAN names | |
81882a64 | 580 | check_parameters "${OPTARG:-}" |
8f6c2328 MG |
581 | if [[ -z "${PARAM_DOMAIN:-}" ]]; then |
582 | PARAM_DOMAIN="${OPTARG}" | |
583 | else | |
584 | PARAM_DOMAIN="${PARAM_DOMAIN} ${OPTARG}" | |
585 | fi | |
586 | ;; | |
587 | x) | |
588 | # PARAM_Usage: --force (-x) | |
589 | # PARAM_Description: force renew of certificate even if it is longer valid than value in RENEW_DAYS | |
590 | PARAM_FORCE="yes" | |
81882a64 LS |
591 | ;; |
592 | f) | |
0a859a19 LS |
593 | # PARAM_Usage: --config (-f) path/to/config.sh |
594 | # PARAM_Description: Use specified config file | |
81882a64 LS |
595 | check_parameters "${OPTARG:-}" |
596 | CONFIG="${OPTARG}" | |
597 | ;; | |
598 | p) | |
0a859a19 LS |
599 | # PARAM_Usage: --privkey (-p) path/to/key.pem |
600 | # PARAM_Description: Use specified private key instead of account key (useful for revocation) | |
81882a64 | 601 | check_parameters "${OPTARG:-}" |
0e92aba2 | 602 | PARAM_PRIVATE_KEY="${OPTARG}" |
81882a64 LS |
603 | ;; |
604 | *) | |
605 | echo "Unknown parameter detected: -${OPTARG}" | |
606 | echo | |
607 | command_help | |
608 | exit 1 | |
609 | ;; | |
610 | esac | |
61f0b7ed | 611 | done |
81882a64 LS |
612 | |
613 | if [[ -z "${COMMAND}" ]]; then | |
614 | COMMAND="cron" | |
615 | fi | |
616 | ||
617 | init_system | |
618 | ||
619 | case "${COMMAND}" in | |
620 | cron) | |
8f6c2328 | 621 | command_sign_domains |
81882a64 | 622 | ;; |
1ab6a436 LS |
623 | env) |
624 | command_env | |
625 | ;; | |
81882a64 | 626 | revoke) |
10d9f342 | 627 | command_revoke "${revoke_me}" |
81882a64 LS |
628 | ;; |
629 | esac |