]>
Commit | Line | Data |
---|---|---|
0a7c9364 | 1 | #!/usr/bin/env sh |
bfdf1f48 | 2 | |
619bae74 | 3 | VER=3.0.5 |
a7b7355d | 4 | |
6cc11ffb | 5 | PROJECT_NAME="acme.sh" |
a7b7355d | 6 | |
6cc11ffb | 7 | PROJECT_ENTRY="acme.sh" |
8 | ||
09f74a9a | 9 | PROJECT="https://github.com/acmesh-official/$PROJECT_NAME" |
4c3b3608 | 10 | |
f3e4cea3 | 11 | DEFAULT_INSTALL_HOME="$HOME/.$PROJECT_NAME" |
77f96b38 | 12 | |
13 | _WINDOWS_SCHEDULER_NAME="$PROJECT_NAME.cron" | |
14 | ||
f3e4cea3 | 15 | _SCRIPT_="$0" |
16 | ||
b50e701c | 17 | _SUB_FOLDER_NOTIFY="notify" |
18 | _SUB_FOLDER_DNSAPI="dnsapi" | |
19 | _SUB_FOLDER_DEPLOY="deploy" | |
20 | ||
21 | _SUB_FOLDERS="$_SUB_FOLDER_DNSAPI $_SUB_FOLDER_DEPLOY $_SUB_FOLDER_NOTIFY" | |
f3e4cea3 | 22 | |
737e9e48 | 23 | CA_LETSENCRYPT_V2="https://acme-v02.api.letsencrypt.org/directory" |
24 | CA_LETSENCRYPT_V2_TEST="https://acme-staging-v02.api.letsencrypt.org/directory" | |
c1151b0d | 25 | |
737e9e48 | 26 | CA_BUYPASS="https://api.buypass.com/acme/directory" |
27 | CA_BUYPASS_TEST="https://api.test4.buypass.no/acme/directory" | |
28 | ||
29 | CA_ZEROSSL="https://acme.zerossl.com/v2/DV90" | |
36752cb6 | 30 | _ZERO_EAB_ENDPOINT="https://api.zerossl.com/acme/eab-credentials-email" |
389518e1 | 31 | |
53d6ab6c | 32 | CA_SSLCOM_RSA="https://acme.ssl.com/sslcom-dv-rsa" |
33 | CA_SSLCOM_ECC="https://acme.ssl.com/sslcom-dv-ecc" | |
34 | ||
fb5091a3 | 35 | CA_GOOGLE="https://dv.acme-v02.api.pki.goog/directory" |
36 | CA_GOOGLE_TEST="https://dv.acme-v02.test-api.pki.goog/directory" | |
37 | ||
d0b51489 | 38 | DEFAULT_CA=$CA_ZEROSSL |
737e9e48 | 39 | DEFAULT_STAGING_CA=$CA_LETSENCRYPT_V2_TEST |
40 | ||
41 | CA_NAMES=" | |
53d6ab6c | 42 | ZeroSSL.com,zerossl |
50fefc3b | 43 | LetsEncrypt.org,letsencrypt |
44 | LetsEncrypt.org_test,letsencrypt_test,letsencrypttest | |
269847d1 | 45 | BuyPass.com,buypass |
46 | BuyPass.com_test,buypass_test,buypasstest | |
53d6ab6c | 47 | SSL.com,sslcom |
fb5091a3 | 48 | Google.com,google |
49 | Google.com_test,googletest,google_test | |
737e9e48 | 50 | " |
51 | ||
fb5091a3 | 52 | CA_SERVERS="$CA_ZEROSSL,$CA_LETSENCRYPT_V2,$CA_LETSENCRYPT_V2_TEST,$CA_BUYPASS,$CA_BUYPASS_TEST,$CA_SSLCOM_RSA,$CA_GOOGLE,$CA_GOOGLE_TEST" |
f87890cb | 53 | |
07af4247 | 54 | DEFAULT_USER_AGENT="$PROJECT_NAME/$VER ($PROJECT)" |
bbbdcb09 | 55 | |
57e58ce7 | 56 | DEFAULT_ACCOUNT_KEY_LENGTH=2048 |
57 | DEFAULT_DOMAIN_KEY_LENGTH=2048 | |
58 | ||
a746139c | 59 | DEFAULT_OPENSSL_BIN="openssl" |
60 | ||
4c3b3608 | 61 | VTYPE_HTTP="http-01" |
62 | VTYPE_DNS="dns-01" | |
08681f4a | 63 | VTYPE_ALPN="tls-alpn-01" |
e22bcf7c | 64 | |
6ae8d101 | 65 | ID_TYPE_DNS="dns" |
66 | ID_TYPE_IP="ip" | |
67 | ||
0463b5d6 | 68 | LOCAL_ANY_ADDRESS="0.0.0.0" |
69 | ||
ec67a1b2 | 70 | DEFAULT_RENEW=60 |
523c7682 | 71 | |
3f4513b3 | 72 | NO_VALUE="no" |
73 | ||
3881f221 | 74 | W_DNS="dns" |
08681f4a | 75 | W_ALPN="alpn" |
64821ad4 | 76 | DNS_ALIAS_PREFIX="=" |
4c3b3608 | 77 | |
0e44f587 | 78 | MODE_STATELESS="stateless" |
79 | ||
ec603bee | 80 | STATE_VERIFIED="verified_ok" |
81 | ||
9d725af6 | 82 | NGINX="nginx:" |
03f8d6e9 | 83 | NGINX_START="#ACME_NGINX_START" |
84 | NGINX_END="#ACME_NGINX_END" | |
9d725af6 | 85 | |
0727f705 JP |
86 | BEGIN_CSR="-----BEGIN [NEW ]\{0,4\}CERTIFICATE REQUEST-----" |
87 | END_CSR="-----END [NEW ]\{0,4\}CERTIFICATE REQUEST-----" | |
88fab7d6 | 88 | |
89 | BEGIN_CERT="-----BEGIN CERTIFICATE-----" | |
90 | END_CERT="-----END CERTIFICATE-----" | |
91 | ||
45e386b2 | 92 | CONTENT_TYPE_JSON="application/jose+json" |
cc179731 | 93 | RENEW_SKIP=2 |
94 | ||
7690f73e | 95 | B64CONF_START="__ACME_BASE64__START_" |
96 | B64CONF_END="__ACME_BASE64__END_" | |
97 | ||
43822d37 | 98 | ECC_SEP="_" |
99 | ECC_SUFFIX="${ECC_SEP}ecc" | |
100 | ||
a73c5b33 | 101 | LOG_LEVEL_1=1 |
102 | LOG_LEVEL_2=2 | |
103 | LOG_LEVEL_3=3 | |
104 | DEFAULT_LOG_LEVEL="$LOG_LEVEL_1" | |
105 | ||
fc6cf4d9 | 106 | DEBUG_LEVEL_1=1 |
107 | DEBUG_LEVEL_2=2 | |
108 | DEBUG_LEVEL_3=3 | |
109 | DEBUG_LEVEL_DEFAULT=$DEBUG_LEVEL_1 | |
110 | DEBUG_LEVEL_NONE=0 | |
111 | ||
10eec7d4 | 112 | DOH_CLOUDFLARE=1 |
113 | DOH_GOOGLE=2 | |
e65144a1 | 114 | DOH_ALI=3 |
115 | DOH_DP=4 | |
10eec7d4 | 116 | |
e6e85b0c | 117 | HIDDEN_VALUE="[hidden](please add '--output-insecure' to see this value)" |
118 | ||
e2edf208 | 119 | SYSLOG_ERROR="user.error" |
fc6cf4d9 | 120 | SYSLOG_INFO="user.info" |
e2edf208 | 121 | SYSLOG_DEBUG="user.debug" |
122 | ||
fc6cf4d9 | 123 | #error |
113089be | 124 | SYSLOG_LEVEL_ERROR=3 |
fc6cf4d9 | 125 | #info |
113089be | 126 | SYSLOG_LEVEL_INFO=6 |
fc6cf4d9 | 127 | #debug |
113089be | 128 | SYSLOG_LEVEL_DEBUG=7 |
fc6cf4d9 | 129 | #debug2 |
113089be | 130 | SYSLOG_LEVEL_DEBUG_2=8 |
fc6cf4d9 | 131 | #debug3 |
113089be | 132 | SYSLOG_LEVEL_DEBUG_3=9 |
fc6cf4d9 | 133 | |
113089be | 134 | SYSLOG_LEVEL_DEFAULT=$SYSLOG_LEVEL_ERROR |
fc6cf4d9 | 135 | #none |
136 | SYSLOG_LEVEL_NONE=0 | |
137 | ||
b50e701c | 138 | NOTIFY_LEVEL_DISABLE=0 |
139 | NOTIFY_LEVEL_ERROR=1 | |
140 | NOTIFY_LEVEL_RENEW=2 | |
141 | NOTIFY_LEVEL_SKIP=3 | |
142 | ||
143 | NOTIFY_LEVEL_DEFAULT=$NOTIFY_LEVEL_RENEW | |
144 | ||
145 | NOTIFY_MODE_BULK=0 | |
146 | NOTIFY_MODE_CERT=1 | |
147 | ||
148 | NOTIFY_MODE_DEFAULT=$NOTIFY_MODE_BULK | |
149 | ||
c39e6c44 | 150 | _BASE64_ENCODED_CFGS="Le_PreHook Le_PostHook Le_RenewHook Le_Preferred_Chain Le_ReloadCmd" |
151 | ||
09f74a9a | 152 | _DEBUG_WIKI="https://github.com/acmesh-official/acme.sh/wiki/How-to-debug-acme.sh" |
4c3b3608 | 153 | |
09f74a9a | 154 | _PREPARE_LINK="https://github.com/acmesh-official/acme.sh/wiki/Install-preparations" |
562a4c05 | 155 | |
09f74a9a | 156 | _STATELESS_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Stateless-Mode" |
0e44f587 | 157 | |
09f74a9a | 158 | _DNS_ALIAS_WIKI="https://github.com/acmesh-official/acme.sh/wiki/DNS-alias-mode" |
875625b1 | 159 | |
09f74a9a | 160 | _DNS_MANUAL_WIKI="https://github.com/acmesh-official/acme.sh/wiki/dns-manual-mode" |
a0923622 | 161 | |
e3ebd582 | 162 | _DNS_API_WIKI="https://github.com/acmesh-official/acme.sh/wiki/dnsapi" |
163 | ||
09f74a9a | 164 | _NOTIFY_WIKI="https://github.com/acmesh-official/acme.sh/wiki/notify" |
b50e701c | 165 | |
09f74a9a | 166 | _SUDO_WIKI="https://github.com/acmesh-official/acme.sh/wiki/sudo" |
5bdfdfef | 167 | |
1041c9f9 | 168 | _REVOKE_WIKI="https://github.com/acmesh-official/acme.sh/wiki/revokecert" |
169 | ||
737e9e48 | 170 | _ZEROSSL_WIKI="https://github.com/acmesh-official/acme.sh/wiki/ZeroSSL.com-CA" |
171 | ||
53d6ab6c | 172 | _SSLCOM_WIKI="https://github.com/acmesh-official/acme.sh/wiki/SSL.com-CA" |
173 | ||
d83d8552 | 174 | _SERVER_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Server" |
175 | ||
e3ebd582 | 176 | _PREFERRED_CHAIN_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Preferred-Chain" |
177 | ||
de4c4eed | 178 | _VALIDITY_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Validity" |
179 | ||
8a24275b | 180 | _DNSCHECK_WIKI="https://github.com/acmesh-official/acme.sh/wiki/dnscheck" |
181 | ||
309bec47 | 182 | _DNS_MANUAL_ERR="The dns manual mode can not renew automatically, you must issue it again manually. You'd better use the other modes instead." |
183 | ||
184 | _DNS_MANUAL_WARN="It seems that you are using dns manual mode. please take care: $_DNS_MANUAL_ERR" | |
185 | ||
a0923622 | 186 | _DNS_MANUAL_ERROR="It seems that you are using dns manual mode. Read this link first: $_DNS_MANUAL_WIKI" |
187 | ||
08ee072f | 188 | __INTERACTIVE="" |
4c2a3841 | 189 | if [ -t 1 ]; then |
08ee072f | 190 | __INTERACTIVE="1" |
191 | fi | |
00a50605 | 192 | |
43822d37 | 193 | __green() { |
137dc1ea | 194 | if [ "${__INTERACTIVE}${ACME_NO_COLOR:-0}" = "10" -o "${ACME_FORCE_COLOR}" = "1" ]; then |
671edc33 | 195 | printf '\33[1;32m%b\33[0m' "$1" |
137dc1ea | 196 | return |
2d12b689 | 197 | fi |
3576754c | 198 | printf -- "%b" "$1" |
43822d37 | 199 | } |
200 | ||
201 | __red() { | |
137dc1ea | 202 | if [ "${__INTERACTIVE}${ACME_NO_COLOR:-0}" = "10" -o "${ACME_FORCE_COLOR}" = "1" ]; then |
671edc33 | 203 | printf '\33[1;31m%b\33[0m' "$1" |
137dc1ea | 204 | return |
2d12b689 | 205 | fi |
3576754c | 206 | printf -- "%b" "$1" |
43822d37 | 207 | } |
00a50605 | 208 | |
a73c5b33 | 209 | _printargs() { |
65a7d569 | 210 | _exitstatus="$?" |
569d6c55 | 211 | if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then |
212 | printf -- "%s" "[$(date)] " | |
213 | fi | |
4c2a3841 | 214 | if [ -z "$2" ]; then |
569d6c55 | 215 | printf -- "%s" "$1" |
43822d37 | 216 | else |
569d6c55 | 217 | printf -- "%s" "$1='$2'" |
43822d37 | 218 | fi |
a73c5b33 | 219 | printf "\n" |
ac9f6e3a | 220 | # return the saved exit status |
52351d7d | 221 | return "$_exitstatus" |
43822d37 | 222 | } |
223 | ||
9d548d81 | 224 | _dlg_versions() { |
225 | echo "Diagnosis versions: " | |
851fedf7 | 226 | echo "openssl:$ACME_OPENSSL_BIN" |
d8ba26e6 | 227 | if _exists "${ACME_OPENSSL_BIN:-openssl}"; then |
228 | ${ACME_OPENSSL_BIN:-openssl} version 2>&1 | |
9d548d81 | 229 | else |
eca57bee | 230 | echo "$ACME_OPENSSL_BIN doesn't exist." |
9d548d81 | 231 | fi |
4c2a3841 | 232 | |
9d548d81 | 233 | echo "apache:" |
4c2a3841 | 234 | if [ "$_APACHECTL" ] && _exists "$_APACHECTL"; then |
e735d8d4 | 235 | $_APACHECTL -V 2>&1 |
9d548d81 | 236 | else |
eca57bee | 237 | echo "apache doesn't exist." |
9d548d81 | 238 | fi |
4c2a3841 | 239 | |
326c386b | 240 | echo "nginx:" |
241 | if _exists "nginx"; then | |
242 | nginx -V 2>&1 | |
243 | else | |
eca57bee | 244 | echo "nginx doesn't exist." |
326c386b | 245 | fi |
246 | ||
3794b5cb | 247 | echo "socat:" |
248 | if _exists "socat"; then | |
463df9e4 | 249 | socat -V 2>&1 |
9d548d81 | 250 | else |
eca57bee | 251 | _debug "socat doesn't exist." |
9d548d81 | 252 | fi |
253 | } | |
43822d37 | 254 | |
e2edf208 | 255 | #class |
256 | _syslog() { | |
65a7d569 | 257 | _exitstatus="$?" |
fc6cf4d9 | 258 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" = "$SYSLOG_LEVEL_NONE" ]; then |
e2edf208 | 259 | return |
260 | fi | |
261 | _logclass="$1" | |
262 | shift | |
5b3e3d9c | 263 | if [ -z "$__logger_i" ]; then |
264 | if _contains "$(logger --help 2>&1)" "-i"; then | |
265 | __logger_i="logger -i" | |
266 | else | |
267 | __logger_i="logger" | |
268 | fi | |
269 | fi | |
270 | $__logger_i -t "$PROJECT_NAME" -p "$_logclass" "$(_printargs "$@")" >/dev/null 2>&1 | |
65a7d569 | 271 | return "$_exitstatus" |
e2edf208 | 272 | } |
273 | ||
a73c5b33 | 274 | _log() { |
275 | [ -z "$LOG_FILE" ] && return | |
95e06de5 | 276 | _printargs "$@" >>"$LOG_FILE" |
a73c5b33 | 277 | } |
278 | ||
279 | _info() { | |
fc6cf4d9 | 280 | _log "$@" |
113089be | 281 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_INFO" ]; then |
fc6cf4d9 | 282 | _syslog "$SYSLOG_INFO" "$@" |
283 | fi | |
a73c5b33 | 284 | _printargs "$@" |
4c3b3608 | 285 | } |
286 | ||
287 | _err() { | |
fc6cf4d9 | 288 | _syslog "$SYSLOG_ERROR" "$@" |
289 | _log "$@" | |
569d6c55 | 290 | if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then |
291 | printf -- "%s" "[$(date)] " >&2 | |
292 | fi | |
4c2a3841 | 293 | if [ -z "$2" ]; then |
65de3110 | 294 | __red "$1" >&2 |
295 | else | |
296 | __red "$1='$2'" >&2 | |
297 | fi | |
b19ba13a | 298 | printf "\n" >&2 |
4c3b3608 | 299 | return 1 |
300 | } | |
301 | ||
43822d37 | 302 | _usage() { |
4c2a3841 | 303 | __red "$@" >&2 |
65de3110 | 304 | printf "\n" >&2 |
43822d37 | 305 | } |
306 | ||
bba5376a JV |
307 | __debug_bash_helper() { |
308 | # At this point only do for --debug 3 | |
309 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -lt "$DEBUG_LEVEL_3" ]; then | |
bba5376a JV |
310 | return |
311 | fi | |
312 | # Return extra debug info when running with bash, otherwise return empty | |
313 | # string. | |
314 | if [ -z "${BASH_VERSION}" ]; then | |
bba5376a JV |
315 | return |
316 | fi | |
317 | # We are a bash shell at this point, return the filename, function name, and | |
318 | # line number as a string | |
319 | _dbh_saveIFS=$IFS | |
320 | IFS=" " | |
adce8f52 JV |
321 | # Must use eval or syntax error happens under dash. The eval should use |
322 | # single quotes as older versions of busybox had a bug with double quotes and | |
323 | # eval. | |
bba5376a JV |
324 | # Use 'caller 1' as we want one level up the stack as we should be called |
325 | # by one of the _debug* functions | |
adce8f52 | 326 | eval '_dbh_called=($(caller 1))' |
bba5376a | 327 | IFS=$_dbh_saveIFS |
adce8f52 | 328 | eval '_dbh_file=${_dbh_called[2]}' |
bba5376a JV |
329 | if [ -n "${_script_home}" ]; then |
330 | # Trim off the _script_home directory name | |
adce8f52 | 331 | eval '_dbh_file=${_dbh_file#$_script_home/}' |
bba5376a | 332 | fi |
adce8f52 JV |
333 | eval '_dbh_function=${_dbh_called[1]}' |
334 | eval '_dbh_lineno=${_dbh_called[0]}' | |
bba5376a JV |
335 | printf "%-40s " "$_dbh_file:${_dbh_function}:${_dbh_lineno}" |
336 | } | |
337 | ||
c60883ef | 338 | _debug() { |
fc6cf4d9 | 339 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then |
340 | _log "$@" | |
a73c5b33 | 341 | fi |
113089be | 342 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then |
fc6cf4d9 | 343 | _syslog "$SYSLOG_DEBUG" "$@" |
344 | fi | |
345 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then | |
bba5376a JV |
346 | _bash_debug=$(__debug_bash_helper) |
347 | _printargs "${_bash_debug}$@" >&2 | |
c60883ef | 348 | fi |
c60883ef | 349 | } |
350 | ||
e6e85b0c | 351 | #output the sensitive messages |
352 | _secure_debug() { | |
353 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then | |
354 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
355 | _log "$@" | |
356 | else | |
357 | _log "$1" "$HIDDEN_VALUE" | |
358 | fi | |
359 | fi | |
360 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then | |
361 | _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE" | |
362 | fi | |
363 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then | |
364 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
365 | _printargs "$@" >&2 | |
366 | else | |
367 | _printargs "$1" "$HIDDEN_VALUE" >&2 | |
368 | fi | |
369 | fi | |
370 | } | |
371 | ||
a63b05a9 | 372 | _debug2() { |
fc6cf4d9 | 373 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then |
374 | _log "$@" | |
a73c5b33 | 375 | fi |
113089be | 376 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then |
fc6cf4d9 | 377 | _syslog "$SYSLOG_DEBUG" "$@" |
378 | fi | |
379 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then | |
bba5376a JV |
380 | _bash_debug=$(__debug_bash_helper) |
381 | _printargs "${_bash_debug}$@" >&2 | |
a63b05a9 | 382 | fi |
a63b05a9 | 383 | } |
384 | ||
e6e85b0c | 385 | _secure_debug2() { |
386 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then | |
387 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
388 | _log "$@" | |
389 | else | |
390 | _log "$1" "$HIDDEN_VALUE" | |
391 | fi | |
392 | fi | |
393 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then | |
394 | _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE" | |
395 | fi | |
396 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then | |
397 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
398 | _printargs "$@" >&2 | |
399 | else | |
400 | _printargs "$1" "$HIDDEN_VALUE" >&2 | |
401 | fi | |
402 | fi | |
403 | } | |
404 | ||
22ea4004 | 405 | _debug3() { |
fc6cf4d9 | 406 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then |
407 | _log "$@" | |
408 | fi | |
113089be | 409 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then |
fc6cf4d9 | 410 | _syslog "$SYSLOG_DEBUG" "$@" |
a73c5b33 | 411 | fi |
fc6cf4d9 | 412 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then |
bba5376a JV |
413 | _bash_debug=$(__debug_bash_helper) |
414 | _printargs "${_bash_debug}$@" >&2 | |
22ea4004 | 415 | fi |
22ea4004 | 416 | } |
417 | ||
e6e85b0c | 418 | _secure_debug3() { |
419 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then | |
420 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
421 | _log "$@" | |
422 | else | |
423 | _log "$1" "$HIDDEN_VALUE" | |
424 | fi | |
425 | fi | |
426 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then | |
427 | _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE" | |
428 | fi | |
429 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then | |
430 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
431 | _printargs "$@" >&2 | |
432 | else | |
433 | _printargs "$1" "$HIDDEN_VALUE" >&2 | |
434 | fi | |
435 | fi | |
436 | } | |
437 | ||
c4bf5eef | 438 | _upper_case() { |
18e4d270 | 439 | if _is_solaris; then |
440 | tr '[:lower:]' '[:upper:]' | |
441 | else | |
442 | # shellcheck disable=SC2018,SC2019 | |
443 | tr 'a-z' 'A-Z' | |
444 | fi | |
c4bf5eef | 445 | } |
446 | ||
447 | _lower_case() { | |
18e4d270 | 448 | if _is_solaris; then |
449 | tr '[:upper:]' '[:lower:]' | |
450 | else | |
451 | # shellcheck disable=SC2018,SC2019 | |
452 | tr 'A-Z' 'a-z' | |
453 | fi | |
c4bf5eef | 454 | } |
455 | ||
4c2a3841 | 456 | _startswith() { |
dceb3aca | 457 | _str="$1" |
458 | _sub="$2" | |
18e4d270 | 459 | echo "$_str" | grep -- "^$_sub" >/dev/null 2>&1 |
dceb3aca | 460 | } |
461 | ||
4c2a3841 | 462 | _endswith() { |
43822d37 | 463 | _str="$1" |
464 | _sub="$2" | |
465 | echo "$_str" | grep -- "$_sub\$" >/dev/null 2>&1 | |
466 | } | |
467 | ||
4c2a3841 | 468 | _contains() { |
dceb3aca | 469 | _str="$1" |
470 | _sub="$2" | |
43822d37 | 471 | echo "$_str" | grep -- "$_sub" >/dev/null 2>&1 |
dceb3aca | 472 | } |
473 | ||
c53da1ef | 474 | _hasfield() { |
475 | _str="$1" | |
476 | _field="$2" | |
477 | _sep="$3" | |
4c2a3841 | 478 | if [ -z "$_field" ]; then |
43822d37 | 479 | _usage "Usage: str field [sep]" |
c53da1ef | 480 | return 1 |
481 | fi | |
4c2a3841 | 482 | |
483 | if [ -z "$_sep" ]; then | |
c53da1ef | 484 | _sep="," |
485 | fi | |
4c2a3841 | 486 | |
6cf7be4b | 487 | for f in $(echo "$_str" | tr "$_sep" ' '); do |
4c2a3841 | 488 | if [ "$f" = "$_field" ]; then |
0c9546cc | 489 | _debug2 "'$_str' contains '$_field'" |
c53da1ef | 490 | return 0 #contains ok |
491 | fi | |
492 | done | |
0c9546cc | 493 | _debug2 "'$_str' does not contain '$_field'" |
3c07f57a | 494 | return 1 #not contains |
c53da1ef | 495 | } |
496 | ||
422dd1fa | 497 | # str index [sep] |
4c2a3841 | 498 | _getfield() { |
0463b5d6 | 499 | _str="$1" |
500 | _findex="$2" | |
501 | _sep="$3" | |
4c2a3841 | 502 | |
503 | if [ -z "$_findex" ]; then | |
0463b5d6 | 504 | _usage "Usage: str field [sep]" |
505 | return 1 | |
506 | fi | |
4c2a3841 | 507 | |
508 | if [ -z "$_sep" ]; then | |
0463b5d6 | 509 | _sep="," |
510 | fi | |
511 | ||
201aa244 | 512 | _ffi="$_findex" |
4c2a3841 | 513 | while [ "$_ffi" -gt "0" ]; do |
201aa244 | 514 | _fv="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")" |
4c2a3841 | 515 | if [ "$_fv" ]; then |
0463b5d6 | 516 | printf -- "%s" "$_fv" |
517 | return 0 | |
518 | fi | |
95e06de5 | 519 | _ffi="$(_math "$_ffi" - 1)" |
0463b5d6 | 520 | done |
4c2a3841 | 521 | |
0463b5d6 | 522 | printf -- "%s" "$_str" |
523 | ||
524 | } | |
525 | ||
4c2a3841 | 526 | _exists() { |
c60883ef | 527 | cmd="$1" |
4c2a3841 | 528 | if [ -z "$cmd" ]; then |
43822d37 | 529 | _usage "Usage: _exists cmd" |
c60883ef | 530 | return 1 |
531 | fi | |
82dc2244 | 532 | |
533 | if eval type type >/dev/null 2>&1; then | |
534 | eval type "$cmd" >/dev/null 2>&1 | |
535 | elif command >/dev/null 2>&1; then | |
19539575 | 536 | command -v "$cmd" >/dev/null 2>&1 |
ce4be4e9 | 537 | else |
e591d5cf | 538 | which "$cmd" >/dev/null 2>&1 |
eac18b1c | 539 | fi |
c60883ef | 540 | ret="$?" |
690a5e20 | 541 | _debug3 "$cmd exists=$ret" |
c60883ef | 542 | return $ret |
543 | } | |
544 | ||
00a50605 | 545 | #a + b |
4c2a3841 | 546 | _math() { |
be68fbd4 | 547 | _m_opts="$@" |
548 | printf "%s" "$(($_m_opts))" | |
00a50605 | 549 | } |
550 | ||
551 | _h_char_2_dec() { | |
552 | _ch=$1 | |
553 | case "${_ch}" in | |
19c43451 | 554 | a | A) |
555 | printf "10" | |
556 | ;; | |
557 | b | B) | |
558 | printf "11" | |
559 | ;; | |
560 | c | C) | |
561 | printf "12" | |
562 | ;; | |
563 | d | D) | |
564 | printf "13" | |
565 | ;; | |
566 | e | E) | |
567 | printf "14" | |
568 | ;; | |
569 | f | F) | |
570 | printf "15" | |
571 | ;; | |
572 | *) | |
573 | printf "%s" "$_ch" | |
574 | ;; | |
19539575 | 575 | esac |
00a50605 | 576 | |
577 | } | |
578 | ||
fac1e367 | 579 | _URGLY_PRINTF="" |
4c2a3841 | 580 | if [ "$(printf '\x41')" != 'A' ]; then |
fac1e367 | 581 | _URGLY_PRINTF=1 |
582 | fi | |
583 | ||
f8bcfeb2 | 584 | _ESCAPE_XARGS="" |
841b7627 | 585 | if _exists xargs && [ "$(printf %s '\\x41' | xargs printf)" = 'A' ]; then |
f8bcfeb2 | 586 | _ESCAPE_XARGS=1 |
587 | fi | |
588 | ||
4c3b3608 | 589 | _h2b() { |
9a90fe37 | 590 | if _exists xxd; then |
591 | if _contains "$(xxd --help 2>&1)" "assumes -c30"; then | |
592 | if xxd -r -p -c 9999 2>/dev/null; then | |
593 | return | |
594 | fi | |
595 | else | |
596 | if xxd -r -p 2>/dev/null; then | |
597 | return | |
598 | fi | |
599 | fi | |
b420ec6c | 600 | fi |
601 | ||
4c3b3608 | 602 | hex=$(cat) |
fa93d68b | 603 | ic="" |
604 | jc="" | |
b420ec6c | 605 | _debug2 _URGLY_PRINTF "$_URGLY_PRINTF" |
606 | if [ -z "$_URGLY_PRINTF" ]; then | |
f8bcfeb2 | 607 | if [ "$_ESCAPE_XARGS" ] && _exists xargs; then |
fa93d68b | 608 | _debug2 "xargs" |
ded4469e | 609 | echo "$hex" | _upper_case | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/g' | xargs printf |
fa93d68b | 610 | else |
ded4469e | 611 | for h in $(echo "$hex" | _upper_case | sed 's/\([0-9A-F]\{2\}\)/ \1/g'); do |
fa93d68b | 612 | if [ -z "$h" ]; then |
613 | break | |
614 | fi | |
615 | printf "\x$h%s" | |
616 | done | |
617 | fi | |
b420ec6c | 618 | else |
ded4469e | 619 | for c in $(echo "$hex" | _upper_case | sed 's/\([0-9A-F]\)/ \1/g'); do |
fa93d68b | 620 | if [ -z "$ic" ]; then |
621 | ic=$c | |
622 | continue | |
00a50605 | 623 | fi |
fa93d68b | 624 | jc=$c |
19539575 | 625 | ic="$(_h_char_2_dec "$ic")" |
626 | jc="$(_h_char_2_dec "$jc")" | |
e51bef6d | 627 | printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")""%s" |
fa93d68b | 628 | ic="" |
629 | jc="" | |
b420ec6c | 630 | done |
631 | fi | |
e591d5cf | 632 | |
4c3b3608 | 633 | } |
634 | ||
542d7977 | 635 | _is_solaris() { |
636 | _contains "${__OS__:=$(uname -a)}" "solaris" || _contains "${__OS__:=$(uname -a)}" "SunOS" | |
637 | } | |
638 | ||
9bdb799b | 639 | #_ascii_hex str |
640 | #this can only process ascii chars, should only be used when od command is missing as a backup way. | |
641 | _ascii_hex() { | |
642 | _debug2 "Using _ascii_hex" | |
643 | _str="$1" | |
644 | _str_len=${#_str} | |
645 | _h_i=1 | |
646 | while [ "$_h_i" -le "$_str_len" ]; do | |
647 | _str_c="$(printf "%s" "$_str" | cut -c "$_h_i")" | |
648 | printf " %02x" "'$_str_c" | |
649 | _h_i="$(_math "$_h_i" + 1)" | |
650 | done | |
651 | } | |
652 | ||
542d7977 | 653 | #stdin output hexstr splited by one space |
654 | #input:"abc" | |
655 | #output: " 61 62 63" | |
656 | _hex_dump() { | |
4e4a6d83 | 657 | if _exists od; then |
658 | od -A n -v -t x1 | tr -s " " | sed 's/ $//' | tr -d "\r\t\n" | |
659 | elif _exists hexdump; then | |
660 | _debug3 "using hexdump" | |
661 | hexdump -v -e '/1 ""' -e '/1 " %02x" ""' | |
662 | elif _exists xxd; then | |
663 | _debug3 "using xxd" | |
664 | xxd -ps -c 20 -i | sed "s/ 0x/ /g" | tr -d ",\n" | tr -s " " | |
665 | else | |
666 | _debug3 "using _ascii_hex" | |
9bdb799b | 667 | str=$(cat) |
668 | _ascii_hex "$str" | |
669 | fi | |
542d7977 | 670 | } |
671 | ||
672 | #url encode, no-preserved chars | |
673 | #A B C D E F G H I J K L M N O P Q R S T U V W X Y Z | |
674 | #41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a | |
675 | ||
676 | #a b c d e f g h i j k l m n o p q r s t u v w x y z | |
677 | #61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a | |
678 | ||
679 | #0 1 2 3 4 5 6 7 8 9 - _ . ~ | |
680 | #30 31 32 33 34 35 36 37 38 39 2d 5f 2e 7e | |
681 | ||
682 | #stdin stdout | |
683 | _url_encode() { | |
684 | _hex_str=$(_hex_dump) | |
685 | _debug3 "_url_encode" | |
686 | _debug3 "_hex_str" "$_hex_str" | |
687 | for _hex_code in $_hex_str; do | |
688 | #upper case | |
689 | case "${_hex_code}" in | |
19c43451 | 690 | "41") |
691 | printf "%s" "A" | |
692 | ;; | |
693 | "42") | |
694 | printf "%s" "B" | |
695 | ;; | |
696 | "43") | |
697 | printf "%s" "C" | |
698 | ;; | |
699 | "44") | |
700 | printf "%s" "D" | |
701 | ;; | |
702 | "45") | |
703 | printf "%s" "E" | |
704 | ;; | |
705 | "46") | |
706 | printf "%s" "F" | |
707 | ;; | |
708 | "47") | |
709 | printf "%s" "G" | |
710 | ;; | |
711 | "48") | |
712 | printf "%s" "H" | |
713 | ;; | |
714 | "49") | |
715 | printf "%s" "I" | |
716 | ;; | |
717 | "4a") | |
718 | printf "%s" "J" | |
719 | ;; | |
720 | "4b") | |
721 | printf "%s" "K" | |
722 | ;; | |
723 | "4c") | |
724 | printf "%s" "L" | |
725 | ;; | |
726 | "4d") | |
727 | printf "%s" "M" | |
728 | ;; | |
729 | "4e") | |
730 | printf "%s" "N" | |
731 | ;; | |
732 | "4f") | |
733 | printf "%s" "O" | |
734 | ;; | |
735 | "50") | |
736 | printf "%s" "P" | |
737 | ;; | |
738 | "51") | |
739 | printf "%s" "Q" | |
740 | ;; | |
741 | "52") | |
742 | printf "%s" "R" | |
743 | ;; | |
744 | "53") | |
745 | printf "%s" "S" | |
746 | ;; | |
747 | "54") | |
748 | printf "%s" "T" | |
749 | ;; | |
750 | "55") | |
751 | printf "%s" "U" | |
752 | ;; | |
753 | "56") | |
754 | printf "%s" "V" | |
755 | ;; | |
756 | "57") | |
757 | printf "%s" "W" | |
758 | ;; | |
759 | "58") | |
760 | printf "%s" "X" | |
761 | ;; | |
762 | "59") | |
763 | printf "%s" "Y" | |
764 | ;; | |
765 | "5a") | |
766 | printf "%s" "Z" | |
767 | ;; | |
c3b1eb08 | 768 | |
769 | #lower case | |
19c43451 | 770 | "61") |
771 | printf "%s" "a" | |
772 | ;; | |
773 | "62") | |
774 | printf "%s" "b" | |
775 | ;; | |
776 | "63") | |
777 | printf "%s" "c" | |
778 | ;; | |
779 | "64") | |
780 | printf "%s" "d" | |
781 | ;; | |
782 | "65") | |
783 | printf "%s" "e" | |
784 | ;; | |
785 | "66") | |
786 | printf "%s" "f" | |
787 | ;; | |
788 | "67") | |
789 | printf "%s" "g" | |
790 | ;; | |
791 | "68") | |
792 | printf "%s" "h" | |
793 | ;; | |
794 | "69") | |
795 | printf "%s" "i" | |
796 | ;; | |
797 | "6a") | |
798 | printf "%s" "j" | |
799 | ;; | |
800 | "6b") | |
801 | printf "%s" "k" | |
802 | ;; | |
803 | "6c") | |
804 | printf "%s" "l" | |
805 | ;; | |
806 | "6d") | |
807 | printf "%s" "m" | |
808 | ;; | |
809 | "6e") | |
810 | printf "%s" "n" | |
811 | ;; | |
812 | "6f") | |
813 | printf "%s" "o" | |
814 | ;; | |
815 | "70") | |
816 | printf "%s" "p" | |
817 | ;; | |
818 | "71") | |
819 | printf "%s" "q" | |
820 | ;; | |
821 | "72") | |
822 | printf "%s" "r" | |
823 | ;; | |
824 | "73") | |
825 | printf "%s" "s" | |
826 | ;; | |
827 | "74") | |
828 | printf "%s" "t" | |
829 | ;; | |
830 | "75") | |
831 | printf "%s" "u" | |
832 | ;; | |
833 | "76") | |
834 | printf "%s" "v" | |
835 | ;; | |
836 | "77") | |
837 | printf "%s" "w" | |
838 | ;; | |
839 | "78") | |
840 | printf "%s" "x" | |
841 | ;; | |
842 | "79") | |
843 | printf "%s" "y" | |
844 | ;; | |
845 | "7a") | |
846 | printf "%s" "z" | |
847 | ;; | |
c3b1eb08 | 848 | #numbers |
19c43451 | 849 | "30") |
850 | printf "%s" "0" | |
851 | ;; | |
852 | "31") | |
853 | printf "%s" "1" | |
854 | ;; | |
855 | "32") | |
856 | printf "%s" "2" | |
857 | ;; | |
858 | "33") | |
859 | printf "%s" "3" | |
860 | ;; | |
861 | "34") | |
862 | printf "%s" "4" | |
863 | ;; | |
864 | "35") | |
865 | printf "%s" "5" | |
866 | ;; | |
867 | "36") | |
868 | printf "%s" "6" | |
869 | ;; | |
870 | "37") | |
871 | printf "%s" "7" | |
872 | ;; | |
873 | "38") | |
874 | printf "%s" "8" | |
875 | ;; | |
876 | "39") | |
877 | printf "%s" "9" | |
878 | ;; | |
879 | "2d") | |
880 | printf "%s" "-" | |
881 | ;; | |
882 | "5f") | |
883 | printf "%s" "_" | |
884 | ;; | |
885 | "2e") | |
886 | printf "%s" "." | |
887 | ;; | |
888 | "7e") | |
889 | printf "%s" "~" | |
890 | ;; | |
891 | #other hex | |
892 | *) | |
893 | printf '%%%s' "$_hex_code" | |
894 | ;; | |
542d7977 | 895 | esac |
e009ec8b | 896 | done |
897 | } | |
898 | ||
b50e701c | 899 | _json_encode() { |
900 | _j_str="$(sed 's/"/\\"/g' | sed "s/\r/\\r/g")" | |
901 | _debug3 "_json_encode" | |
902 | _debug3 "_j_str" "$_j_str" | |
903 | echo "$_j_str" | _hex_dump | _lower_case | sed 's/0a/5c 6e/g' | tr -d ' ' | _h2b | tr -d "\r\n" | |
904 | } | |
905 | ||
93de1e49 | 906 | #from: http:\/\/ to http:// |
907 | _json_decode() { | |
908 | _j_str="$(sed 's#\\/#/#g')" | |
909 | _debug3 "_json_decode" | |
910 | _debug3 "_j_str" "$_j_str" | |
911 | echo "$_j_str" | |
912 | } | |
913 | ||
c60883ef | 914 | #options file |
915 | _sed_i() { | |
916 | options="$1" | |
917 | filename="$2" | |
4c2a3841 | 918 | if [ -z "$filename" ]; then |
43822d37 | 919 | _usage "Usage:_sed_i options filename" |
c60883ef | 920 | return 1 |
921 | fi | |
14f3dbb7 | 922 | _debug2 options "$options" |
923 | if sed -h 2>&1 | grep "\-i\[SUFFIX]" >/dev/null 2>&1; then | |
c60883ef | 924 | _debug "Using sed -i" |
14f3dbb7 | 925 | sed -i "$options" "$filename" |
c60883ef | 926 | else |
927 | _debug "No -i support in sed" | |
19539575 | 928 | text="$(cat "$filename")" |
4c2a3841 | 929 | echo "$text" | sed "$options" >"$filename" |
c60883ef | 930 | fi |
931 | } | |
932 | ||
22ea4004 | 933 | _egrep_o() { |
a3c0c754 | 934 | if ! egrep -o "$1" 2>/dev/null; then |
22ea4004 | 935 | sed -n 's/.*\('"$1"'\).*/\1/p' |
22ea4004 | 936 | fi |
937 | } | |
938 | ||
88fab7d6 | 939 | #Usage: file startline endline |
940 | _getfile() { | |
941 | filename="$1" | |
942 | startline="$2" | |
943 | endline="$3" | |
4c2a3841 | 944 | if [ -z "$endline" ]; then |
43822d37 | 945 | _usage "Usage: file startline endline" |
88fab7d6 | 946 | return 1 |
947 | fi | |
4c2a3841 | 948 | |
949 | i="$(grep -n -- "$startline" "$filename" | cut -d : -f 1)" | |
950 | if [ -z "$i" ]; then | |
88fab7d6 | 951 | _err "Can not find start line: $startline" |
952 | return 1 | |
953 | fi | |
19539575 | 954 | i="$(_math "$i" + 1)" |
955 | _debug i "$i" | |
4c2a3841 | 956 | |
957 | j="$(grep -n -- "$endline" "$filename" | cut -d : -f 1)" | |
958 | if [ -z "$j" ]; then | |
88fab7d6 | 959 | _err "Can not find end line: $endline" |
960 | return 1 | |
961 | fi | |
19539575 | 962 | j="$(_math "$j" - 1)" |
963 | _debug j "$j" | |
4c2a3841 | 964 | |
965 | sed -n "$i,${j}p" "$filename" | |
88fab7d6 | 966 | |
967 | } | |
968 | ||
969 | #Usage: multiline | |
4c3b3608 | 970 | _base64() { |
ec9975c3 | 971 | [ "" ] #urgly |
4c2a3841 | 972 | if [ "$1" ]; then |
24d2a8b9 | 973 | _debug3 "base64 multiline:'$1'" |
d8ba26e6 | 974 | ${ACME_OPENSSL_BIN:-openssl} base64 -e |
88fab7d6 | 975 | else |
4d8b99a3 | 976 | _debug3 "base64 single line." |
d8ba26e6 | 977 | ${ACME_OPENSSL_BIN:-openssl} base64 -e | tr -d '\r\n' |
88fab7d6 | 978 | fi |
979 | } | |
980 | ||
981 | #Usage: multiline | |
982 | _dbase64() { | |
4c2a3841 | 983 | if [ "$1" ]; then |
d8ba26e6 | 984 | ${ACME_OPENSSL_BIN:-openssl} base64 -d |
9b6f7752 | 985 | else |
986 | ${ACME_OPENSSL_BIN:-openssl} base64 -d -A | |
88fab7d6 | 987 | fi |
988 | } | |
989 | ||
183063a2 | 990 | #file |
991 | _checkcert() { | |
992 | _cf="$1" | |
993 | if [ "$DEBUG" ]; then | |
2c7d2230 | 994 | ${ACME_OPENSSL_BIN:-openssl} x509 -noout -text -in "$_cf" |
183063a2 | 995 | else |
2c7d2230 | 996 | ${ACME_OPENSSL_BIN:-openssl} x509 -noout -text -in "$_cf" >/dev/null 2>&1 |
183063a2 | 997 | fi |
998 | } | |
999 | ||
e22bcf7c | 1000 | #Usage: hashalg [outputhex] |
88fab7d6 | 1001 | #Output Base64-encoded digest |
1002 | _digest() { | |
1003 | alg="$1" | |
4c2a3841 | 1004 | if [ -z "$alg" ]; then |
43822d37 | 1005 | _usage "Usage: _digest hashalg" |
88fab7d6 | 1006 | return 1 |
1007 | fi | |
4c2a3841 | 1008 | |
e22bcf7c | 1009 | outputhex="$2" |
4c2a3841 | 1010 | |
c7b16249 | 1011 | if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ] || [ "$alg" = "md5" ]; then |
4c2a3841 | 1012 | if [ "$outputhex" ]; then |
d8ba26e6 | 1013 | ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' ' |
e22bcf7c | 1014 | else |
d8ba26e6 | 1015 | ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -binary | _base64 |
b001840d | 1016 | fi |
1017 | else | |
1018 | _err "$alg is not supported yet" | |
1019 | return 1 | |
1020 | fi | |
1021 | ||
1022 | } | |
1023 | ||
e009ec8b | 1024 | #Usage: hashalg secret_hex [outputhex] |
1025 | #Output binary hmac | |
b001840d | 1026 | _hmac() { |
1027 | alg="$1" | |
e009ec8b | 1028 | secret_hex="$2" |
b001840d | 1029 | outputhex="$3" |
4c2a3841 | 1030 | |
e009ec8b | 1031 | if [ -z "$secret_hex" ]; then |
4c2a3841 | 1032 | _usage "Usage: _hmac hashalg secret [outputhex]" |
b001840d | 1033 | return 1 |
1034 | fi | |
1035 | ||
a6014bf0 | 1036 | if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ]; then |
4c2a3841 | 1037 | if [ "$outputhex" ]; then |
d8ba26e6 | 1038 | (${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -mac HMAC -macopt "hexkey:$secret_hex" 2>/dev/null || ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hmac "$(printf "%s" "$secret_hex" | _h2b)") | cut -d = -f 2 | tr -d ' ' |
e22bcf7c | 1039 | else |
d8ba26e6 | 1040 | ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -mac HMAC -macopt "hexkey:$secret_hex" -binary 2>/dev/null || ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hmac "$(printf "%s" "$secret_hex" | _h2b)" -binary |
e22bcf7c | 1041 | fi |
88fab7d6 | 1042 | else |
1043 | _err "$alg is not supported yet" | |
1044 | return 1 | |
1045 | fi | |
1046 | ||
1047 | } | |
1048 | ||
1049 | #Usage: keyfile hashalg | |
1050 | #Output: Base64-encoded signature value | |
1051 | _sign() { | |
1052 | keyfile="$1" | |
1053 | alg="$2" | |
4c2a3841 | 1054 | if [ -z "$alg" ]; then |
43822d37 | 1055 | _usage "Usage: _sign keyfile hashalg" |
88fab7d6 | 1056 | return 1 |
1057 | fi | |
4c2a3841 | 1058 | |
d8ba26e6 | 1059 | _sign_openssl="${ACME_OPENSSL_BIN:-openssl} dgst -sign $keyfile " |
4c2a3841 | 1060 | |
18827427 | 1061 | if _isRSA "$keyfile" >/dev/null 2>&1; then |
f87890cb | 1062 | $_sign_openssl -$alg | _base64 |
18827427 | 1063 | elif _isEcc "$keyfile" >/dev/null 2>&1; then |
f87890cb | 1064 | if ! _signedECText="$($_sign_openssl -sha$__ECC_KEY_LEN | ${ACME_OPENSSL_BIN:-openssl} asn1parse -inform DER)"; then |
67184d7b | 1065 | _err "Sign failed: $_sign_openssl" |
1066 | _err "Key file: $keyfile" | |
357b514b | 1067 | _err "Key content:$(wc -l <"$keyfile") lines" |
67184d7b | 1068 | return 1 |
1069 | fi | |
998783eb | 1070 | _debug3 "_signedECText" "$_signedECText" |
1071 | _ec_r="$(echo "$_signedECText" | _head_n 2 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")" | |
998783eb | 1072 | _ec_s="$(echo "$_signedECText" | _head_n 3 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")" |
a3295476 | 1073 | if [ "$__ECC_KEY_LEN" -eq "256" ]; then |
1074 | while [ "${#_ec_r}" -lt "64" ]; do | |
f31debc0 | 1075 | _ec_r="0${_ec_r}" |
6654d7a9 | 1076 | done |
a3295476 | 1077 | while [ "${#_ec_s}" -lt "64" ]; do |
f31debc0 | 1078 | _ec_s="0${_ec_s}" |
a3295476 | 1079 | done |
1080 | fi | |
1081 | if [ "$__ECC_KEY_LEN" -eq "384" ]; then | |
1082 | while [ "${#_ec_r}" -lt "96" ]; do | |
f31debc0 | 1083 | _ec_r="0${_ec_r}" |
6654d7a9 | 1084 | done |
a3295476 | 1085 | while [ "${#_ec_s}" -lt "96" ]; do |
f31debc0 | 1086 | _ec_s="0${_ec_s}" |
a3295476 | 1087 | done |
1088 | fi | |
1089 | if [ "$__ECC_KEY_LEN" -eq "512" ]; then | |
1090 | while [ "${#_ec_r}" -lt "132" ]; do | |
f31debc0 | 1091 | _ec_r="0${_ec_r}" |
6654d7a9 | 1092 | done |
a3295476 | 1093 | while [ "${#_ec_s}" -lt "132" ]; do |
f31debc0 | 1094 | _ec_s="0${_ec_s}" |
a3295476 | 1095 | done |
1096 | fi | |
e2a5af1c | 1097 | _debug3 "_ec_r" "$_ec_r" |
998783eb | 1098 | _debug3 "_ec_s" "$_ec_s" |
1099 | printf "%s" "$_ec_r$_ec_s" | _h2b | _base64 | |
1100 | else | |
1101 | _err "Unknown key file format." | |
1102 | return 1 | |
1103 | fi | |
4c2a3841 | 1104 | |
4c3b3608 | 1105 | } |
1106 | ||
3281043e | 1107 | #keylength or isEcc flag (empty str => not ecc) |
43822d37 | 1108 | _isEccKey() { |
1109 | _length="$1" | |
1110 | ||
4c2a3841 | 1111 | if [ -z "$_length" ]; then |
43822d37 | 1112 | return 1 |
1113 | fi | |
1114 | ||
19c43451 | 1115 | [ "$_length" != "1024" ] && |
1116 | [ "$_length" != "2048" ] && | |
1117 | [ "$_length" != "3072" ] && | |
1118 | [ "$_length" != "4096" ] && | |
1119 | [ "$_length" != "8192" ] | |
43822d37 | 1120 | } |
1121 | ||
e22bcf7c | 1122 | # _createkey 2048|ec-256 file |
1123 | _createkey() { | |
1124 | length="$1" | |
1125 | f="$2" | |
c4236e58 | 1126 | _debug2 "_createkey for file:$f" |
43822d37 | 1127 | eccname="$length" |
4c2a3841 | 1128 | if _startswith "$length" "ec-"; then |
f9a6988e | 1129 | length=$(printf "%s" "$length" | cut -d '-' -f 2-100) |
e22bcf7c | 1130 | |
4c2a3841 | 1131 | if [ "$length" = "256" ]; then |
e22bcf7c | 1132 | eccname="prime256v1" |
1133 | fi | |
4c2a3841 | 1134 | if [ "$length" = "384" ]; then |
e22bcf7c | 1135 | eccname="secp384r1" |
1136 | fi | |
4c2a3841 | 1137 | if [ "$length" = "521" ]; then |
e22bcf7c | 1138 | eccname="secp521r1" |
1139 | fi | |
43822d37 | 1140 | |
e22bcf7c | 1141 | fi |
1142 | ||
4c2a3841 | 1143 | if [ -z "$length" ]; then |
1144 | length=2048 | |
43822d37 | 1145 | fi |
4c2a3841 | 1146 | |
cbcd7e0f | 1147 | _debug "Use length $length" |
43822d37 | 1148 | |
af193291 | 1149 | if ! [ -e "$f" ]; then |
1150 | if ! touch "$f" >/dev/null 2>&1; then | |
1151 | _f_path="$(dirname "$f")" | |
1152 | _debug _f_path "$_f_path" | |
1153 | if ! mkdir -p "$_f_path"; then | |
1154 | _err "Can not create path: $_f_path" | |
1155 | return 1 | |
1156 | fi | |
1157 | fi | |
1158 | if ! touch "$f" >/dev/null 2>&1; then | |
81532f37 | 1159 | return 1 |
1160 | fi | |
af193291 | 1161 | chmod 600 "$f" |
81532f37 | 1162 | fi |
1163 | ||
4c2a3841 | 1164 | if _isEccKey "$length"; then |
cbcd7e0f | 1165 | _debug "Using ec name: $eccname" |
a730a081 | 1166 | if _opkey="$(${ACME_OPENSSL_BIN:-openssl} ecparam -name "$eccname" -noout -genkey 2>/dev/null)"; then |
e6df1828 | 1167 | echo "$_opkey" >"$f" |
1168 | else | |
1169 | _err "error ecc key name: $eccname" | |
1170 | return 1 | |
1171 | fi | |
e22bcf7c | 1172 | else |
cbcd7e0f | 1173 | _debug "Using RSA: $length" |
906ef43c | 1174 | __traditional="" |
1175 | if _contains "$(${ACME_OPENSSL_BIN:-openssl} help genrsa 2>&1)" "-traditional"; then | |
1176 | __traditional="-traditional" | |
1177 | fi | |
45289572 | 1178 | if _opkey="$(${ACME_OPENSSL_BIN:-openssl} genrsa $__traditional "$length" 2>/dev/null)"; then |
e6df1828 | 1179 | echo "$_opkey" >"$f" |
1180 | else | |
1181 | _err "error rsa key: $length" | |
1182 | return 1 | |
1183 | fi | |
e22bcf7c | 1184 | fi |
43822d37 | 1185 | |
4c2a3841 | 1186 | if [ "$?" != "0" ]; then |
43822d37 | 1187 | _err "Create key error." |
1188 | return 1 | |
1189 | fi | |
e22bcf7c | 1190 | } |
1191 | ||
9774b01b | 1192 | #domain |
1193 | _is_idn() { | |
1194 | _is_idn_d="$1" | |
049be104 | 1195 | _debug2 _is_idn_d "$_is_idn_d" |
d10f40f1 | 1196 | _idn_temp=$(printf "%s" "$_is_idn_d" | tr -d '0-9' | tr -d 'a-z' | tr -d 'A-Z' | tr -d '*.,-_') |
049be104 | 1197 | _debug2 _idn_temp "$_idn_temp" |
1198 | [ "$_idn_temp" ] | |
9774b01b | 1199 | } |
1200 | ||
1201 | #aa.com | |
1202 | #aa.com,bb.com,cc.com | |
1203 | _idn() { | |
1204 | __idn_d="$1" | |
4c2a3841 | 1205 | if ! _is_idn "$__idn_d"; then |
9774b01b | 1206 | printf "%s" "$__idn_d" |
1207 | return 0 | |
1208 | fi | |
4c2a3841 | 1209 | |
1210 | if _exists idn; then | |
1211 | if _contains "$__idn_d" ','; then | |
9774b01b | 1212 | _i_first="1" |
4c2a3841 | 1213 | for f in $(echo "$__idn_d" | tr ',' ' '); do |
9774b01b | 1214 | [ -z "$f" ] && continue |
4c2a3841 | 1215 | if [ -z "$_i_first" ]; then |
9774b01b | 1216 | printf "%s" "," |
1217 | else | |
1218 | _i_first="" | |
1219 | fi | |
2a1e06f8 | 1220 | idn --quiet "$f" | tr -d "\r\n" |
9774b01b | 1221 | done |
1222 | else | |
1223 | idn "$__idn_d" | tr -d "\r\n" | |
1224 | fi | |
1225 | else | |
1226 | _err "Please install idn to process IDN names." | |
1227 | fi | |
1228 | } | |
1229 | ||
08681f4a | 1230 | #_createcsr cn san_list keyfile csrfile conf acmeValidationv1 |
e22bcf7c | 1231 | _createcsr() { |
1232 | _debug _createcsr | |
1233 | domain="$1" | |
1234 | domainlist="$2" | |
0c9546cc | 1235 | csrkey="$3" |
e22bcf7c | 1236 | csr="$4" |
1237 | csrconf="$5" | |
08681f4a | 1238 | acmeValidationv1="$6" |
e22bcf7c | 1239 | _debug2 domain "$domain" |
1240 | _debug2 domainlist "$domainlist" | |
0c9546cc | 1241 | _debug2 csrkey "$csrkey" |
1242 | _debug2 csr "$csr" | |
1243 | _debug2 csrconf "$csrconf" | |
4c2a3841 | 1244 | |
280e4430 | 1245 | printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\nreq_extensions = v3_req\n[ v3_req ]\n\n" >"$csrconf" |
4c2a3841 | 1246 | |
08681f4a | 1247 | if [ "$acmeValidationv1" ]; then |
4962cc3d | 1248 | domainlist="$(_idn "$domainlist")" |
6ae8d101 | 1249 | _debug2 domainlist "$domainlist" |
e6e07714 | 1250 | alt="" |
6ae8d101 | 1251 | for dl in $(echo "$domainlist" | tr "," ' '); do |
1252 | if [ "$alt" ]; then | |
1253 | alt="$alt,$(_getIdType "$dl" | _upper_case):$dl" | |
1254 | else | |
1255 | alt="$(_getIdType "$dl" | _upper_case):$dl" | |
1256 | fi | |
1257 | done | |
1258 | printf -- "\nsubjectAltName=$alt" >>"$csrconf" | |
08681f4a | 1259 | elif [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then |
e22bcf7c | 1260 | #single domain |
1261 | _info "Single domain" "$domain" | |
6ae8d101 | 1262 | printf -- "\nsubjectAltName=$(_getIdType "$domain" | _upper_case):$(_idn "$domain")" >>"$csrconf" |
e22bcf7c | 1263 | else |
f9a6988e | 1264 | domainlist="$(_idn "$domainlist")" |
9774b01b | 1265 | _debug2 domainlist "$domainlist" |
e67d26ca | 1266 | alt="$(_getIdType "$domain" | _upper_case):$(_idn "$domain")" |
90b65c66 | 1267 | for dl in $(echo "'$domainlist'" | sed "s/,/' '/g"); do |
1268 | dl=$(echo "$dl" | tr -d "'") | |
6ae8d101 | 1269 | alt="$alt,$(_getIdType "$dl" | _upper_case):$dl" |
1270 | done | |
3c07f57a | 1271 | #multi |
e22bcf7c | 1272 | _info "Multi domain" "$alt" |
4c2a3841 | 1273 | printf -- "\nsubjectAltName=$alt" >>"$csrconf" |
0c9546cc | 1274 | fi |
6ba1eda9 | 1275 | if [ "$Le_OCSP_Staple" = "1" ]; then |
96db9362 | 1276 | _savedomainconf Le_OCSP_Staple "$Le_OCSP_Staple" |
4c2a3841 | 1277 | printf -- "\nbasicConstraints = CA:FALSE\n1.3.6.1.5.5.7.1.24=DER:30:03:02:01:05" >>"$csrconf" |
e22bcf7c | 1278 | fi |
4c2a3841 | 1279 | |
08681f4a | 1280 | if [ "$acmeValidationv1" ]; then |
ad613e24 | 1281 | printf "\n1.3.6.1.5.5.7.1.31=critical,DER:04:20:${acmeValidationv1}" >>"${csrconf}" |
08681f4a | 1282 | fi |
1283 | ||
9774b01b | 1284 | _csr_cn="$(_idn "$domain")" |
1285 | _debug2 _csr_cn "$_csr_cn" | |
34f25fa5 | 1286 | if _contains "$(uname -a)" "MINGW"; then |
d43b587d | 1287 | if _isIP "$_csr_cn"; then |
1288 | ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "//O=$PROJECT_NAME" -config "$csrconf" -out "$csr" | |
1289 | else | |
1290 | ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "//CN=$_csr_cn" -config "$csrconf" -out "$csr" | |
1291 | fi | |
34f25fa5 | 1292 | else |
d43b587d | 1293 | if _isIP "$_csr_cn"; then |
1294 | ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "/O=$PROJECT_NAME" -config "$csrconf" -out "$csr" | |
1295 | else | |
1296 | ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "/CN=$_csr_cn" -config "$csrconf" -out "$csr" | |
1297 | fi | |
34f25fa5 | 1298 | fi |
e22bcf7c | 1299 | } |
1300 | ||
1301 | #_signcsr key csr conf cert | |
1302 | _signcsr() { | |
1303 | key="$1" | |
1304 | csr="$2" | |
1305 | conf="$3" | |
1306 | cert="$4" | |
5aa146a5 | 1307 | _debug "_signcsr" |
4c2a3841 | 1308 | |
d8ba26e6 | 1309 | _msg="$(${ACME_OPENSSL_BIN:-openssl} x509 -req -days 365 -in "$csr" -signkey "$key" -extensions v3_req -extfile "$conf" -out "$cert" 2>&1)" |
5aa146a5 | 1310 | _ret="$?" |
1311 | _debug "$_msg" | |
1312 | return $_ret | |
e22bcf7c | 1313 | } |
1314 | ||
10afcaca | 1315 | #_csrfile |
1316 | _readSubjectFromCSR() { | |
1317 | _csrfile="$1" | |
4c2a3841 | 1318 | if [ -z "$_csrfile" ]; then |
10afcaca | 1319 | _usage "_readSubjectFromCSR mycsr.csr" |
1320 | return 1 | |
1321 | fi | |
b963dadc | 1322 | ${ACME_OPENSSL_BIN:-openssl} req -noout -in "$_csrfile" -subject | tr ',' "\n" | _egrep_o "CN *=.*" | cut -d = -f 2 | cut -d / -f 1 | tr -d ' \n' |
10afcaca | 1323 | } |
1324 | ||
1325 | #_csrfile | |
1326 | #echo comma separated domain list | |
1327 | _readSubjectAltNamesFromCSR() { | |
1328 | _csrfile="$1" | |
4c2a3841 | 1329 | if [ -z "$_csrfile" ]; then |
10afcaca | 1330 | _usage "_readSubjectAltNamesFromCSR mycsr.csr" |
1331 | return 1 | |
1332 | fi | |
4c2a3841 | 1333 | |
10afcaca | 1334 | _csrsubj="$(_readSubjectFromCSR "$_csrfile")" |
1335 | _debug _csrsubj "$_csrsubj" | |
4c2a3841 | 1336 | |
d8ba26e6 | 1337 | _dnsAltnames="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile" | grep "^ *DNS:.*" | tr -d ' \n')" |
10afcaca | 1338 | _debug _dnsAltnames "$_dnsAltnames" |
4c2a3841 | 1339 | |
1340 | if _contains "$_dnsAltnames," "DNS:$_csrsubj,"; then | |
10afcaca | 1341 | _debug "AltNames contains subject" |
2b9ebd66 | 1342 | _excapedAlgnames="$(echo "$_dnsAltnames" | tr '*' '#')" |
1343 | _debug _excapedAlgnames "$_excapedAlgnames" | |
1344 | _escapedSubject="$(echo "$_csrsubj" | tr '*' '#')" | |
1345 | _debug _escapedSubject "$_escapedSubject" | |
1346 | _dnsAltnames="$(echo "$_excapedAlgnames," | sed "s/DNS:$_escapedSubject,//g" | tr '#' '*' | sed "s/,\$//g")" | |
1347 | _debug _dnsAltnames "$_dnsAltnames" | |
10afcaca | 1348 | else |
1349 | _debug "AltNames doesn't contain subject" | |
1350 | fi | |
4c2a3841 | 1351 | |
2b9ebd66 | 1352 | echo "$_dnsAltnames" | sed "s/DNS://g" |
10afcaca | 1353 | } |
1354 | ||
3c07f57a | 1355 | #_csrfile |
10afcaca | 1356 | _readKeyLengthFromCSR() { |
1357 | _csrfile="$1" | |
4c2a3841 | 1358 | if [ -z "$_csrfile" ]; then |
1643b476 | 1359 | _usage "_readKeyLengthFromCSR mycsr.csr" |
10afcaca | 1360 | return 1 |
1361 | fi | |
4c2a3841 | 1362 | |
d8ba26e6 | 1363 | _outcsr="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile")" |
7df062b7 | 1364 | _debug2 _outcsr "$_outcsr" |
4c2a3841 | 1365 | if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then |
10afcaca | 1366 | _debug "ECC CSR" |
482cb737 | 1367 | echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' ' |
10afcaca | 1368 | else |
1369 | _debug "RSA CSR" | |
eb0ef6bd | 1370 | _rkl="$(echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1)" |
1371 | if [ "$_rkl" ]; then | |
1372 | echo "$_rkl" | |
1373 | else | |
1374 | echo "$_outcsr" | tr "\t" " " | _egrep_o "RSA Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1 | |
1375 | fi | |
10afcaca | 1376 | fi |
1377 | } | |
1378 | ||
34c27e09 | 1379 | _ss() { |
1380 | _port="$1" | |
4c2a3841 | 1381 | |
1382 | if _exists "ss"; then | |
edf08da6 | 1383 | _debug "Using: ss" |
14d7bfda | 1384 | ss -ntpl 2>/dev/null | grep ":$_port " |
edf08da6 | 1385 | return 0 |
1386 | fi | |
1387 | ||
4c2a3841 | 1388 | if _exists "netstat"; then |
251fc37c | 1389 | _debug "Using: netstat" |
97147b59 | 1390 | if netstat -help 2>&1 | grep "\-p proto" >/dev/null; then |
ccb96535 | 1391 | #for windows version netstat tool |
0463b5d6 | 1392 | netstat -an -p tcp | grep "LISTENING" | grep ":$_port " |
ccb96535 | 1393 | else |
4c2a3841 | 1394 | if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then |
19539575 | 1395 | netstat -an -p tcp | grep LISTEN | grep ":$_port " |
4c2a3841 | 1396 | elif netstat -help 2>&1 | grep -- '-P protocol' >/dev/null; then |
22ea4004 | 1397 | #for solaris |
e3c66532 | 1398 | netstat -an -P tcp | grep "\.$_port " | grep "LISTEN" |
f19f2100 | 1399 | elif netstat -help 2>&1 | grep "\-p" >/dev/null; then |
f21dd911 | 1400 | #for full linux |
19539575 | 1401 | netstat -ntpl | grep ":$_port " |
f21dd911 | 1402 | else |
1403 | #for busybox (embedded linux; no pid support) | |
1404 | netstat -ntl 2>/dev/null | grep ":$_port " | |
edf08da6 | 1405 | fi |
ccb96535 | 1406 | fi |
34c27e09 | 1407 | return 0 |
1408 | fi | |
edf08da6 | 1409 | |
34c27e09 | 1410 | return 1 |
1411 | } | |
1412 | ||
8eab77f3 BC |
1413 | #outfile key cert cacert [password [name [caname]]] |
1414 | _toPkcs() { | |
1415 | _cpfx="$1" | |
1416 | _ckey="$2" | |
1417 | _ccert="$3" | |
1418 | _cca="$4" | |
1419 | pfxPassword="$5" | |
1420 | pfxName="$6" | |
1421 | pfxCaname="$7" | |
1422 | ||
1423 | if [ "$pfxCaname" ]; then | |
1424 | ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName" -caname "$pfxCaname" | |
1425 | elif [ "$pfxName" ]; then | |
1426 | ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName" | |
1427 | elif [ "$pfxPassword" ]; then | |
1428 | ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" | |
1429 | else | |
1430 | ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" | |
1431 | fi | |
1432 | ||
1433 | } | |
1434 | ||
43822d37 | 1435 | #domain [password] [isEcc] |
ac2d5123 | 1436 | toPkcs() { |
1437 | domain="$1" | |
1438 | pfxPassword="$2" | |
4c2a3841 | 1439 | if [ -z "$domain" ]; then |
2e87e64b | 1440 | _usage "Usage: $PROJECT_ENTRY --to-pkcs12 --domain <domain.tld> [--password <password>] [--ecc]" |
ac2d5123 | 1441 | return 1 |
1442 | fi | |
1443 | ||
43822d37 | 1444 | _isEcc="$3" |
4c2a3841 | 1445 | |
43822d37 | 1446 | _initpath "$domain" "$_isEcc" |
1447 | ||
8eab77f3 | 1448 | _toPkcs "$CERT_PFX_PATH" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$pfxPassword" |
4c2a3841 | 1449 | |
1450 | if [ "$?" = "0" ]; then | |
ac2d5123 | 1451 | _info "Success, Pfx is exported to: $CERT_PFX_PATH" |
1452 | fi | |
1453 | ||
1454 | } | |
1455 | ||
4410226d | 1456 | #domain [isEcc] |
1457 | toPkcs8() { | |
1458 | domain="$1" | |
1459 | ||
1460 | if [ -z "$domain" ]; then | |
2e87e64b | 1461 | _usage "Usage: $PROJECT_ENTRY --to-pkcs8 --domain <domain.tld> [--ecc]" |
4410226d | 1462 | return 1 |
1463 | fi | |
1464 | ||
1465 | _isEcc="$2" | |
1466 | ||
1467 | _initpath "$domain" "$_isEcc" | |
1468 | ||
d8ba26e6 | 1469 | ${ACME_OPENSSL_BIN:-openssl} pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in "$CERT_KEY_PATH" -out "$CERT_PKCS8_PATH" |
4410226d | 1470 | |
1471 | if [ "$?" = "0" ]; then | |
1472 | _info "Success, $CERT_PKCS8_PATH" | |
1473 | fi | |
1474 | ||
1475 | } | |
1476 | ||
3c07f57a | 1477 | #[2048] |
4c3b3608 | 1478 | createAccountKey() { |
1479 | _info "Creating account key" | |
4c2a3841 | 1480 | if [ -z "$1" ]; then |
2e87e64b | 1481 | _usage "Usage: $PROJECT_ENTRY --create-account-key [--accountkeylength <bits>]" |
4c3b3608 | 1482 | return |
1483 | fi | |
4c2a3841 | 1484 | |
5fbc47eb | 1485 | length=$1 |
57e58ce7 | 1486 | _create_account_key "$length" |
1487 | ||
1488 | } | |
1489 | ||
1490 | _create_account_key() { | |
1491 | ||
5fbc47eb | 1492 | length=$1 |
4c2a3841 | 1493 | |
1494 | if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then | |
57e58ce7 | 1495 | _debug "Use default length $DEFAULT_ACCOUNT_KEY_LENGTH" |
1496 | length="$DEFAULT_ACCOUNT_KEY_LENGTH" | |
4c3b3608 | 1497 | fi |
4c2a3841 | 1498 | |
5fbc47eb | 1499 | _debug length "$length" |
4c3b3608 | 1500 | _initpath |
5fbc47eb | 1501 | |
57e58ce7 | 1502 | mkdir -p "$CA_DIR" |
4f1888d2 | 1503 | if [ -s "$ACCOUNT_KEY_PATH" ]; then |
4c3b3608 | 1504 | _info "Account key exists, skip" |
4f1888d2 | 1505 | return 0 |
4c3b3608 | 1506 | else |
1507 | #generate account key | |
4f1888d2 | 1508 | if _createkey "$length" "$ACCOUNT_KEY_PATH"; then |
4f1888d2 | 1509 | _info "Create account key ok." |
1510 | return 0 | |
1511 | else | |
1512 | _err "Create account key error." | |
1513 | return 1 | |
1514 | fi | |
4c3b3608 | 1515 | fi |
1516 | ||
1517 | } | |
1518 | ||
43822d37 | 1519 | #domain [length] |
4c3b3608 | 1520 | createDomainKey() { |
1521 | _info "Creating domain key" | |
4c2a3841 | 1522 | if [ -z "$1" ]; then |
2e87e64b | 1523 | _usage "Usage: $PROJECT_ENTRY --create-domain-key --domain <domain.tld> [--keylength <bits>]" |
4c3b3608 | 1524 | return |
1525 | fi | |
4c2a3841 | 1526 | |
4c3b3608 | 1527 | domain=$1 |
2844d73d | 1528 | _cdl=$2 |
e22bcf7c | 1529 | |
2844d73d | 1530 | if [ -z "$_cdl" ]; then |
57e58ce7 | 1531 | _debug "Use DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH" |
2844d73d | 1532 | _cdl="$DEFAULT_DOMAIN_KEY_LENGTH" |
57e58ce7 | 1533 | fi |
e22bcf7c | 1534 | |
2844d73d | 1535 | _initpath "$domain" "$_cdl" |
4c2a3841 | 1536 | |
bd04638d | 1537 | if [ ! -f "$CERT_KEY_PATH" ] || [ ! -s "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$_ACME_IS_RENEW" ]) || [ "$Le_ForceNewDomainKey" = "1" ]; then |
2844d73d | 1538 | if _createkey "$_cdl" "$CERT_KEY_PATH"; then |
1539 | _savedomainconf Le_Keylength "$_cdl" | |
1540 | _info "The domain key is here: $(__green $CERT_KEY_PATH)" | |
7aeb113c | 1541 | return 0 |
4f1888d2 | 1542 | else |
a7420ca3 | 1543 | _err "Can not create domain key" |
4f1888d2 | 1544 | return 1 |
2844d73d | 1545 | fi |
4c3b3608 | 1546 | else |
bd04638d | 1547 | if [ "$_ACME_IS_RENEW" ]; then |
4c3b3608 | 1548 | _info "Domain key exists, skip" |
1549 | return 0 | |
1550 | else | |
1551 | _err "Domain key exists, do you want to overwrite the key?" | |
41e3eafa | 1552 | _err "Add '--force', and try again." |
4c3b3608 | 1553 | return 1 |
1554 | fi | |
1555 | fi | |
1556 | ||
1557 | } | |
1558 | ||
43822d37 | 1559 | # domain domainlist isEcc |
4c3b3608 | 1560 | createCSR() { |
1561 | _info "Creating csr" | |
4c2a3841 | 1562 | if [ -z "$1" ]; then |
2e87e64b | 1563 | _usage "Usage: $PROJECT_ENTRY --create-csr --domain <domain.tld> [--domain <domain2.tld> ...]" |
4c3b3608 | 1564 | return |
1565 | fi | |
4c2a3841 | 1566 | |
43822d37 | 1567 | domain="$1" |
1568 | domainlist="$2" | |
1569 | _isEcc="$3" | |
4c2a3841 | 1570 | |
43822d37 | 1571 | _initpath "$domain" "$_isEcc" |
4c2a3841 | 1572 | |
bd04638d | 1573 | if [ -f "$CSR_PATH" ] && [ "$_ACME_IS_RENEW" ] && [ -z "$FORCE" ]; then |
4c3b3608 | 1574 | _info "CSR exists, skip" |
1575 | return | |
1576 | fi | |
4c2a3841 | 1577 | |
1578 | if [ ! -f "$CERT_KEY_PATH" ]; then | |
43822d37 | 1579 | _err "The key file is not found: $CERT_KEY_PATH" |
1580 | _err "Please create the key file first." | |
1581 | return 1 | |
1582 | fi | |
e22bcf7c | 1583 | _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF" |
4c2a3841 | 1584 | |
4c3b3608 | 1585 | } |
1586 | ||
11927a76 | 1587 | _url_replace() { |
f9a6988e | 1588 | tr '/+' '_-' | tr -d '= ' |
4c3b3608 | 1589 | } |
1590 | ||
f96d91cb | 1591 | #base64 string |
1592 | _durl_replace_base64() { | |
1593 | _l=$((${#1} % 4)) | |
1594 | if [ $_l -eq 2 ]; then | |
1595 | _s="$1"'==' | |
1596 | elif [ $_l -eq 3 ]; then | |
1597 | _s="$1"'=' | |
1598 | else | |
1599 | _s="$1" | |
1600 | fi | |
1601 | echo "$_s" | tr '_-' '/+' | |
1602 | } | |
1603 | ||
4c3b3608 | 1604 | _time2str() { |
cb115809 | 1605 | #BSD |
de4c4eed | 1606 | if date -u -r "$1" -j "+%Y-%m-%dT%H:%M:%SZ" 2>/dev/null; then |
4c3b3608 | 1607 | return |
1608 | fi | |
4c2a3841 | 1609 | |
cb115809 | 1610 | #Linux |
de4c4eed | 1611 | if date -u --date=@"$1" "+%Y-%m-%dT%H:%M:%SZ" 2>/dev/null; then |
4c3b3608 | 1612 | return |
1613 | fi | |
4c2a3841 | 1614 | |
cb115809 | 1615 | #Solaris |
0f607413 | 1616 | if printf "%(%Y-%m-%dT%H:%M:%SZ)T\n" $1 2>/dev/null; then |
1617 | return | |
22ea4004 | 1618 | fi |
4c2a3841 | 1619 | |
a07395fb | 1620 | #Busybox |
de4c4eed | 1621 | if echo "$1" | awk '{ print strftime("%Y-%m-%dT%H:%M:%SZ", $0); }' 2>/dev/null; then |
a07395fb M |
1622 | return |
1623 | fi | |
4c3b3608 | 1624 | } |
1625 | ||
eae29099 | 1626 | _normalizeJson() { |
1627 | sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n" | |
1628 | } | |
1629 | ||
44df2967 | 1630 | _stat() { |
1631 | #Linux | |
4c2a3841 | 1632 | if stat -c '%U:%G' "$1" 2>/dev/null; then |
44df2967 | 1633 | return |
1634 | fi | |
4c2a3841 | 1635 | |
44df2967 | 1636 | #BSD |
4c2a3841 | 1637 | if stat -f '%Su:%Sg' "$1" 2>/dev/null; then |
44df2967 | 1638 | return |
1639 | fi | |
4c2a3841 | 1640 | |
1641 | return 1 #error, 'stat' not found | |
44df2967 | 1642 | } |
1643 | ||
e07795e8 | 1644 | #keyfile |
1645 | _isRSA() { | |
1646 | keyfile=$1 | |
1647 | if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1 || ${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -noout -text | grep "^publicExponent:" >/dev/null 2>&1; then | |
1648 | return 0 | |
1649 | fi | |
1650 | return 1 | |
1651 | } | |
1652 | ||
1653 | #keyfile | |
1654 | _isEcc() { | |
1655 | keyfile=$1 | |
1656 | if grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1 || ${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" >/dev/null 2>&1; then | |
1657 | return 0 | |
1658 | fi | |
1659 | return 1 | |
1660 | } | |
1661 | ||
166096dc | 1662 | #keyfile |
1663 | _calcjwk() { | |
1664 | keyfile="$1" | |
4c2a3841 | 1665 | if [ -z "$keyfile" ]; then |
43822d37 | 1666 | _usage "Usage: _calcjwk keyfile" |
166096dc | 1667 | return 1 |
1668 | fi | |
4c2a3841 | 1669 | |
1670 | if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then | |
ae2db62f | 1671 | _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE" |
1672 | return 0 | |
1673 | fi | |
4c2a3841 | 1674 | |
e07795e8 | 1675 | if _isRSA "$keyfile"; then |
166096dc | 1676 | _debug "RSA key" |
d8ba26e6 | 1677 | pub_exp=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -noout -text | grep "^publicExponent:" | cut -d '(' -f 2 | cut -d 'x' -f 2 | cut -d ')' -f 1) |
4c2a3841 | 1678 | if [ "${#pub_exp}" = "5" ]; then |
166096dc | 1679 | pub_exp=0$pub_exp |
1680 | fi | |
22ea4004 | 1681 | _debug3 pub_exp "$pub_exp" |
4c2a3841 | 1682 | |
f9a6988e | 1683 | e=$(echo "$pub_exp" | _h2b | _base64) |
22ea4004 | 1684 | _debug3 e "$e" |
4c2a3841 | 1685 | |
d8ba26e6 | 1686 | modulus=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2) |
22ea4004 | 1687 | _debug3 modulus "$modulus" |
11927a76 | 1688 | n="$(printf "%s" "$modulus" | _h2b | _base64 | _url_replace)" |
4d8b99a3 | 1689 | _debug3 n "$n" |
1690 | ||
166096dc | 1691 | jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}' |
22ea4004 | 1692 | _debug3 jwk "$jwk" |
4c2a3841 | 1693 | |
5982f4bc | 1694 | JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}' |
1695 | JWK_HEADERPLACE_PART1='{"nonce": "' | |
c1151b0d | 1696 | JWK_HEADERPLACE_PART2='", "alg": "RS256"' |
e07795e8 | 1697 | elif _isEcc "$keyfile"; then |
166096dc | 1698 | _debug "EC key" |
d8ba26e6 | 1699 | crv="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")" |
22ea4004 | 1700 | _debug3 crv "$crv" |
f87890cb | 1701 | __ECC_KEY_LEN=$(echo "$crv" | cut -d "-" -f 2) |
1702 | if [ "$__ECC_KEY_LEN" = "521" ]; then | |
1703 | __ECC_KEY_LEN=512 | |
1704 | fi | |
1705 | _debug3 __ECC_KEY_LEN "$__ECC_KEY_LEN" | |
4c2a3841 | 1706 | if [ -z "$crv" ]; then |
d22b7938 | 1707 | _debug "Let's try ASN1 OID" |
d8ba26e6 | 1708 | crv_oid="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^ASN1 OID:" | cut -d ":" -f 2 | tr -d " \r\n")" |
cae9cee2 | 1709 | _debug3 crv_oid "$crv_oid" |
d22b7938 | 1710 | case "${crv_oid}" in |
19c43451 | 1711 | "prime256v1") |
1712 | crv="P-256" | |
1713 | __ECC_KEY_LEN=256 | |
1714 | ;; | |
1715 | "secp384r1") | |
1716 | crv="P-384" | |
1717 | __ECC_KEY_LEN=384 | |
1718 | ;; | |
1719 | "secp521r1") | |
1720 | crv="P-521" | |
1721 | __ECC_KEY_LEN=512 | |
1722 | ;; | |
1723 | *) | |
1724 | _err "ECC oid : $crv_oid" | |
1725 | return 1 | |
1726 | ;; | |
067d586c | 1727 | esac |
d22b7938 | 1728 | _debug3 crv "$crv" |
1729 | fi | |
4c2a3841 | 1730 | |
d8ba26e6 | 1731 | pubi="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)" |
79a267ab | 1732 | pubi=$(_math "$pubi" + 1) |
22ea4004 | 1733 | _debug3 pubi "$pubi" |
4c2a3841 | 1734 | |
d8ba26e6 | 1735 | pubj="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)" |
79a267ab | 1736 | pubj=$(_math "$pubj" - 1) |
22ea4004 | 1737 | _debug3 pubj "$pubj" |
4c2a3841 | 1738 | |
d8ba26e6 | 1739 | pubtext="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")" |
22ea4004 | 1740 | _debug3 pubtext "$pubtext" |
4c2a3841 | 1741 | |
95e06de5 | 1742 | xlen="$(printf "%s" "$pubtext" | tr -d ':' | wc -c)" |
79a267ab | 1743 | xlen=$(_math "$xlen" / 4) |
22ea4004 | 1744 | _debug3 xlen "$xlen" |
00a50605 | 1745 | |
998783eb | 1746 | xend=$(_math "$xlen" + 1) |
f9a6988e | 1747 | x="$(printf "%s" "$pubtext" | cut -d : -f 2-"$xend")" |
22ea4004 | 1748 | _debug3 x "$x" |
4c2a3841 | 1749 | |
11927a76 | 1750 | x64="$(printf "%s" "$x" | tr -d : | _h2b | _base64 | _url_replace)" |
22ea4004 | 1751 | _debug3 x64 "$x64" |
00a50605 | 1752 | |
19539575 | 1753 | xend=$(_math "$xend" + 1) |
f9a6988e | 1754 | y="$(printf "%s" "$pubtext" | cut -d : -f "$xend"-10000)" |
22ea4004 | 1755 | _debug3 y "$y" |
4c2a3841 | 1756 | |
11927a76 | 1757 | y64="$(printf "%s" "$y" | tr -d : | _h2b | _base64 | _url_replace)" |
22ea4004 | 1758 | _debug3 y64 "$y64" |
4c2a3841 | 1759 | |
ae2db62f | 1760 | jwk='{"crv": "'$crv'", "kty": "EC", "x": "'$x64'", "y": "'$y64'"}' |
22ea4004 | 1761 | _debug3 jwk "$jwk" |
4c2a3841 | 1762 | |
f87890cb | 1763 | JWK_HEADER='{"alg": "ES'$__ECC_KEY_LEN'", "jwk": '$jwk'}' |
5982f4bc | 1764 | JWK_HEADERPLACE_PART1='{"nonce": "' |
c1151b0d | 1765 | JWK_HEADERPLACE_PART2='", "alg": "ES'$__ECC_KEY_LEN'"' |
166096dc | 1766 | else |
238990a2 | 1767 | _err "Only RSA or EC key is supported. keyfile=$keyfile" |
1768 | _debug2 "$(cat "$keyfile")" | |
166096dc | 1769 | return 1 |
1770 | fi | |
1771 | ||
5982f4bc | 1772 | _debug3 JWK_HEADER "$JWK_HEADER" |
ae2db62f | 1773 | __CACHED_JWK_KEY_FILE="$keyfile" |
166096dc | 1774 | } |
fac1e367 | 1775 | |
3aae1ae3 | 1776 | _time() { |
1777 | date -u "+%s" | |
1778 | } | |
fac1e367 | 1779 | |
de4c4eed | 1780 | #support 2 formats: |
1781 | # 2022-04-01 08:10:33 to 1648800633 | |
1782 | #or 2022-04-01T08:10:33Z to 1648800633 | |
1783 | _date2time() { | |
1784 | #Linux | |
1785 | if date -u -d "$(echo "$1" | tr -d "Z" | tr "T" ' ')" +"%s" 2>/dev/null; then | |
1786 | return | |
1787 | fi | |
0f607413 | 1788 | |
1789 | #Solaris | |
1790 | if gdate -u -d "$(echo "$1" | tr -d "Z" | tr "T" ' ')" +"%s" 2>/dev/null; then | |
1791 | return | |
1792 | fi | |
de4c4eed | 1793 | #Mac/BSD |
b4999972 | 1794 | if date -u -j -f "%Y-%m-%d %H:%M:%S" "$(echo "$1" | tr -d "Z" | tr "T" ' ')" +"%s" 2>/dev/null; then |
de4c4eed | 1795 | return |
1796 | fi | |
1797 | _err "Can not parse _date2time $1" | |
1798 | return 1 | |
1799 | } | |
1800 | ||
5d2c5b01 | 1801 | _utc_date() { |
1802 | date -u "+%Y-%m-%d %H:%M:%S" | |
1803 | } | |
1804 | ||
fac1e367 | 1805 | _mktemp() { |
4c2a3841 | 1806 | if _exists mktemp; then |
1807 | if mktemp 2>/dev/null; then | |
610e0f21 | 1808 | return 0 |
4c2a3841 | 1809 | elif _contains "$(mktemp 2>&1)" "-t prefix" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then |
5c48e139 | 1810 | #for Mac osx |
610e0f21 | 1811 | return 0 |
b19ba13a | 1812 | fi |
fac1e367 | 1813 | fi |
4c2a3841 | 1814 | if [ -d "/tmp" ]; then |
3aae1ae3 | 1815 | echo "/tmp/${PROJECT_NAME}wefADf24sf.$(_time).tmp" |
1816 | return 0 | |
4c2a3841 | 1817 | elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then |
610e0f21 | 1818 | echo "/$LE_TEMP_DIR/wefADf24sf.$(_time).tmp" |
1819 | return 0 | |
3aae1ae3 | 1820 | fi |
1821 | _err "Can not create temp file." | |
fac1e367 | 1822 | } |
1823 | ||
effa7fd5 | 1824 | #clear all the https envs to cause _inithttp() to run next time. |
1825 | _resethttp() { | |
1826 | __HTTP_INITIALIZED="" | |
1827 | _ACME_CURL="" | |
1828 | _ACME_WGET="" | |
1829 | ACME_HTTP_NO_REDIRECTS="" | |
1830 | } | |
1831 | ||
fac1e367 | 1832 | _inithttp() { |
1833 | ||
4c2a3841 | 1834 | if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then |
fac1e367 | 1835 | HTTP_HEADER="$(_mktemp)" |
1836 | _debug2 HTTP_HEADER "$HTTP_HEADER" | |
1837 | fi | |
1838 | ||
4c2a3841 | 1839 | if [ "$__HTTP_INITIALIZED" ]; then |
1840 | if [ "$_ACME_CURL$_ACME_WGET" ]; then | |
1befee5a | 1841 | _debug2 "Http already initialized." |
1842 | return 0 | |
1843 | fi | |
1844 | fi | |
4c2a3841 | 1845 | |
1846 | if [ -z "$_ACME_CURL" ] && _exists "curl"; then | |
effa7fd5 | 1847 | _ACME_CURL="curl --silent --dump-header $HTTP_HEADER " |
1848 | if [ -z "$ACME_HTTP_NO_REDIRECTS" ]; then | |
1849 | _ACME_CURL="$_ACME_CURL -L " | |
1850 | fi | |
ae3dda0f | 1851 | if [ "$DEBUG" ] && [ "$DEBUG" -ge 2 ]; then |
fac1e367 | 1852 | _CURL_DUMP="$(_mktemp)" |
1befee5a | 1853 | _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP " |
fac1e367 | 1854 | fi |
1855 | ||
2aa75f03 | 1856 | if [ "$CA_PATH" ]; then |
1857 | _ACME_CURL="$_ACME_CURL --capath $CA_PATH " | |
1858 | elif [ "$CA_BUNDLE" ]; then | |
1befee5a | 1859 | _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE " |
78009539 PS |
1860 | fi |
1861 | ||
6ca5f3d8 | 1862 | if _contains "$(curl --help 2>&1)" "--globoff"; then |
1863 | _ACME_CURL="$_ACME_CURL -g " | |
1864 | fi | |
fac1e367 | 1865 | fi |
4c2a3841 | 1866 | |
1befee5a | 1867 | if [ -z "$_ACME_WGET" ] && _exists "wget"; then |
1868 | _ACME_WGET="wget -q" | |
effa7fd5 | 1869 | if [ "$ACME_HTTP_NO_REDIRECTS" ]; then |
1870 | _ACME_WGET="$_ACME_WGET --max-redirect 0 " | |
1871 | fi | |
4c2a3841 | 1872 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
0d05f9ba | 1873 | if [ "$_ACME_WGET" ] && _contains "$($_ACME_WGET --help 2>&1)" "--debug"; then |
1874 | _ACME_WGET="$_ACME_WGET -d " | |
1875 | fi | |
fac1e367 | 1876 | fi |
2aa75f03 | 1877 | if [ "$CA_PATH" ]; then |
1878 | _ACME_WGET="$_ACME_WGET --ca-directory=$CA_PATH " | |
1879 | elif [ "$CA_BUNDLE" ]; then | |
1880 | _ACME_WGET="$_ACME_WGET --ca-certificate=$CA_BUNDLE " | |
78009539 | 1881 | fi |
fac1e367 | 1882 | fi |
1883 | ||
177b57e1 | 1884 | #from wget 1.14: do not skip body on 404 error |
58ef6d83 | 1885 | if [ "$_ACME_WGET" ] && _contains "$($_ACME_WGET --help 2>&1)" "--content-on-error"; then |
177b57e1 | 1886 | _ACME_WGET="$_ACME_WGET --content-on-error " |
1887 | fi | |
1888 | ||
1befee5a | 1889 | __HTTP_INITIALIZED=1 |
fac1e367 | 1890 | |
fac1e367 | 1891 | } |
fac1e367 | 1892 | |
9cecd525 | 1893 | # body url [needbase64] [POST|PUT|DELETE] [ContentType] |
c60883ef | 1894 | _post() { |
1895 | body="$1" | |
c1151b0d | 1896 | _post_url="$2" |
c60883ef | 1897 | needbase64="$3" |
a4270efa | 1898 | httpmethod="$4" |
45e386b2 | 1899 | _postContentType="$5" |
c60883ef | 1900 | |
4c2a3841 | 1901 | if [ -z "$httpmethod" ]; then |
a4270efa | 1902 | httpmethod="POST" |
1903 | fi | |
1904 | _debug $httpmethod | |
c1151b0d | 1905 | _debug "_post_url" "$_post_url" |
30de13b4 | 1906 | _debug2 "body" "$body" |
6a66ba8a | 1907 | _debug2 "_postContentType" "$_postContentType" |
4c2a3841 | 1908 | |
fac1e367 | 1909 | _inithttp |
4c2a3841 | 1910 | |
9b124070 | 1911 | if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then |
1befee5a | 1912 | _CURL="$_ACME_CURL" |
7834c252 | 1913 | if [ "$HTTPS_INSECURE" ]; then |
1914 | _CURL="$_CURL --insecure " | |
1915 | fi | |
5723fd11 | 1916 | if [ "$httpmethod" = "HEAD" ]; then |
1917 | _CURL="$_CURL -I " | |
1918 | fi | |
ec9fc8cb | 1919 | _debug "_CURL" "$_CURL" |
4c2a3841 | 1920 | if [ "$needbase64" ]; then |
1ba4ab2b | 1921 | if [ "$body" ]; then |
1922 | if [ "$_postContentType" ]; then | |
1923 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64)" | |
1924 | else | |
1925 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64)" | |
1926 | fi | |
39852662 | 1927 | else |
1ba4ab2b | 1928 | if [ "$_postContentType" ]; then |
1929 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url" | _base64)" | |
1930 | else | |
1931 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url" | _base64)" | |
1932 | fi | |
39852662 | 1933 | fi |
c60883ef | 1934 | else |
1ba4ab2b | 1935 | if [ "$body" ]; then |
1936 | if [ "$_postContentType" ]; then | |
1937 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")" | |
1938 | else | |
1939 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")" | |
1940 | fi | |
39852662 | 1941 | else |
1ba4ab2b | 1942 | if [ "$_postContentType" ]; then |
1943 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url")" | |
1944 | else | |
1945 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url")" | |
1946 | fi | |
39852662 | 1947 | fi |
c60883ef | 1948 | fi |
16679b57 | 1949 | _ret="$?" |
4c2a3841 | 1950 | if [ "$_ret" != "0" ]; then |
86c3fa0d | 1951 | _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret" |
4c2a3841 | 1952 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
87ab2d90 | 1953 | _err "Here is the curl dump log:" |
1954 | _err "$(cat "$_CURL_DUMP")" | |
1955 | fi | |
687cfcc2 | 1956 | fi |
4c2a3841 | 1957 | elif [ "$_ACME_WGET" ]; then |
7834c252 | 1958 | _WGET="$_ACME_WGET" |
1959 | if [ "$HTTPS_INSECURE" ]; then | |
1960 | _WGET="$_WGET --no-check-certificate " | |
1961 | fi | |
51b4a9e3 | 1962 | if [ "$httpmethod" = "HEAD" ]; then |
1963 | _WGET="$_WGET --read-timeout=3.0 --tries=2 " | |
1964 | fi | |
7834c252 | 1965 | _debug "_WGET" "$_WGET" |
4c2a3841 | 1966 | if [ "$needbase64" ]; then |
1967 | if [ "$httpmethod" = "POST" ]; then | |
ef871775 | 1968 | if [ "$_postContentType" ]; then |
1969 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)" | |
1970 | else | |
1971 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)" | |
1972 | fi | |
8fb9a709 | 1973 | else |
ef871775 | 1974 | if [ "$_postContentType" ]; then |
1975 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)" | |
1976 | else | |
1977 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)" | |
1978 | fi | |
8fb9a709 | 1979 | fi |
c60883ef | 1980 | else |
4c2a3841 | 1981 | if [ "$httpmethod" = "POST" ]; then |
ef871775 | 1982 | if [ "$_postContentType" ]; then |
1983 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1984 | else | |
1985 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1986 | fi | |
51b4a9e3 | 1987 | elif [ "$httpmethod" = "HEAD" ]; then |
1988 | if [ "$_postContentType" ]; then | |
1989 | response="$($_WGET --spider -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1990 | else | |
1991 | response="$($_WGET --spider -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1992 | fi | |
8fb9a709 | 1993 | else |
ef871775 | 1994 | if [ "$_postContentType" ]; then |
1995 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1996 | else | |
1997 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1998 | fi | |
8fb9a709 | 1999 | fi |
c60883ef | 2000 | fi |
16679b57 | 2001 | _ret="$?" |
4c2a3841 | 2002 | if [ "$_ret" = "8" ]; then |
9f43c270 | 2003 | _ret=0 |
810c129c | 2004 | _debug "wget returns 8, the server returns a 'Bad request' response, lets process the response later." |
9f43c270 | 2005 | fi |
4c2a3841 | 2006 | if [ "$_ret" != "0" ]; then |
86c3fa0d | 2007 | _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret" |
687cfcc2 | 2008 | fi |
c60883ef | 2009 | _sed_i "s/^ *//g" "$HTTP_HEADER" |
d0b748a4 | 2010 | else |
2011 | _ret="$?" | |
2012 | _err "Neither curl nor wget is found, can not do $httpmethod." | |
c60883ef | 2013 | fi |
16679b57 | 2014 | _debug "_ret" "$_ret" |
19539575 | 2015 | printf "%s" "$response" |
16679b57 | 2016 | return $_ret |
c60883ef | 2017 | } |
2018 | ||
75da0713 | 2019 | # url getheader timeout |
c60883ef | 2020 | _get() { |
a4270efa | 2021 | _debug GET |
c60883ef | 2022 | url="$1" |
2023 | onlyheader="$2" | |
75da0713 | 2024 | t="$3" |
79a267ab | 2025 | _debug url "$url" |
72f54ca6 | 2026 | _debug "timeout=$t" |
86c3fa0d | 2027 | |
fac1e367 | 2028 | _inithttp |
2029 | ||
9b124070 | 2030 | if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then |
1befee5a | 2031 | _CURL="$_ACME_CURL" |
7834c252 | 2032 | if [ "$HTTPS_INSECURE" ]; then |
2033 | _CURL="$_CURL --insecure " | |
2034 | fi | |
4c2a3841 | 2035 | if [ "$t" ]; then |
75da0713 | 2036 | _CURL="$_CURL --connect-timeout $t" |
2037 | fi | |
2038 | _debug "_CURL" "$_CURL" | |
4c2a3841 | 2039 | if [ "$onlyheader" ]; then |
f9a6988e | 2040 | $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url" |
c60883ef | 2041 | else |
f9a6988e | 2042 | $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url" |
c60883ef | 2043 | fi |
9aaf36cd | 2044 | ret=$? |
4c2a3841 | 2045 | if [ "$ret" != "0" ]; then |
86c3fa0d | 2046 | _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $ret" |
4c2a3841 | 2047 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
fac1e367 | 2048 | _err "Here is the curl dump log:" |
2049 | _err "$(cat "$_CURL_DUMP")" | |
2050 | fi | |
2051 | fi | |
4c2a3841 | 2052 | elif [ "$_ACME_WGET" ]; then |
1befee5a | 2053 | _WGET="$_ACME_WGET" |
7834c252 | 2054 | if [ "$HTTPS_INSECURE" ]; then |
2055 | _WGET="$_WGET --no-check-certificate " | |
2056 | fi | |
4c2a3841 | 2057 | if [ "$t" ]; then |
75da0713 | 2058 | _WGET="$_WGET --timeout=$t" |
2059 | fi | |
2060 | _debug "_WGET" "$_WGET" | |
4c2a3841 | 2061 | if [ "$onlyheader" ]; then |
f9a6988e | 2062 | $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -S -O /dev/null "$url" 2>&1 | sed 's/^[ ]*//g' |
c60883ef | 2063 | else |
f9a6988e | 2064 | $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url" |
c60883ef | 2065 | fi |
9aaf36cd | 2066 | ret=$? |
f731a4c7 | 2067 | if [ "$ret" = "8" ]; then |
39a1f1ef | 2068 | ret=0 |
810c129c | 2069 | _debug "wget returns 8, the server returns a 'Bad request' response, lets process the response later." |
9f43c270 | 2070 | fi |
4c2a3841 | 2071 | if [ "$ret" != "0" ]; then |
86c3fa0d | 2072 | _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $ret" |
fac1e367 | 2073 | fi |
d0b748a4 | 2074 | else |
2075 | ret=$? | |
2076 | _err "Neither curl nor wget is found, can not do GET." | |
9aaf36cd | 2077 | fi |
ec9fc8cb | 2078 | _debug "ret" "$ret" |
c60883ef | 2079 | return $ret |
2080 | } | |
166096dc | 2081 | |
c2c8f320 | 2082 | _head_n() { |
79a267ab | 2083 | head -n "$1" |
c2c8f320 | 2084 | } |
2085 | ||
2086 | _tail_n() { | |
f9a6988e | 2087 | if ! tail -n "$1" 2>/dev/null; then |
19ab2a29 | 2088 | #fix for solaris |
f9a6988e | 2089 | tail -"$1" |
19ab2a29 | 2090 | fi |
c2c8f320 | 2091 | } |
fac1e367 | 2092 | |
166096dc | 2093 | # url payload needbase64 keyfile |
4c3b3608 | 2094 | _send_signed_request() { |
2095 | url=$1 | |
2096 | payload=$2 | |
2097 | needbase64=$3 | |
166096dc | 2098 | keyfile=$4 |
4c2a3841 | 2099 | if [ -z "$keyfile" ]; then |
166096dc | 2100 | keyfile="$ACCOUNT_KEY_PATH" |
2101 | fi | |
f9a6988e | 2102 | _debug url "$url" |
4c3b3608 | 2103 | _debug payload "$payload" |
4c2a3841 | 2104 | |
2105 | if ! _calcjwk "$keyfile"; then | |
166096dc | 2106 | return 1 |
2107 | fi | |
c60883ef | 2108 | |
cc2d5946 | 2109 | __request_conent_type="$CONTENT_TYPE_JSON" |
2110 | ||
11927a76 | 2111 | payload64=$(printf "%s" "$payload" | _base64 | _url_replace) |
f9a6988e | 2112 | _debug3 payload64 "$payload64" |
4c2a3841 | 2113 | |
709a3fb0 | 2114 | MAX_REQUEST_RETRY_TIMES=20 |
2115 | _sleep_retry_sec=1 | |
0bc745f6 | 2116 | _request_retry_times=0 |
2117 | while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do | |
aad309ee | 2118 | _request_retry_times=$(_math "$_request_retry_times" + 1) |
b7924ce5 | 2119 | _debug3 _request_retry_times "$_request_retry_times" |
0bc745f6 | 2120 | if [ -z "$_CACHED_NONCE" ]; then |
8f01919f | 2121 | _headers="" |
8bd12ed0 K |
2122 | if [ "$ACME_NEW_NONCE" ]; then |
2123 | _debug2 "Get nonce with HEAD. ACME_NEW_NONCE" "$ACME_NEW_NONCE" | |
cae50e16 | 2124 | nonceurl="$ACME_NEW_NONCE" |
1ba4ab2b | 2125 | if _post "" "$nonceurl" "" "HEAD" "$__request_conent_type" >/dev/null; then |
cae50e16 | 2126 | _headers="$(cat "$HTTP_HEADER")" |
e7f7e96d | 2127 | _debug2 _headers "$_headers" |
91d37c78 | 2128 | _CACHED_NONCE="$(echo "$_headers" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2 | cut -d , -f 1)" |
cae50e16 | 2129 | fi |
2130 | fi | |
e7f7e96d | 2131 | if [ -z "$_CACHED_NONCE" ]; then |
33226307 | 2132 | _debug2 "Get nonce with GET. ACME_DIRECTORY" "$ACME_DIRECTORY" |
cae50e16 | 2133 | nonceurl="$ACME_DIRECTORY" |
2134 | _headers="$(_get "$nonceurl" "onlyheader")" | |
e7f7e96d | 2135 | _debug2 _headers "$_headers" |
2136 | _CACHED_NONCE="$(echo "$_headers" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)" | |
cae50e16 | 2137 | fi |
e7f7e96d | 2138 | if [ -z "$_CACHED_NONCE" ] && [ "$ACME_NEW_NONCE" ]; then |
2139 | _debug2 "Get nonce with GET. ACME_NEW_NONCE" "$ACME_NEW_NONCE" | |
2140 | nonceurl="$ACME_NEW_NONCE" | |
2141 | _headers="$(_get "$nonceurl" "onlyheader")" | |
2142 | _debug2 _headers "$_headers" | |
2143 | _CACHED_NONCE="$(echo "$_headers" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)" | |
2144 | fi | |
2145 | _debug2 _CACHED_NONCE "$_CACHED_NONCE" | |
0bc745f6 | 2146 | if [ "$?" != "0" ]; then |
2147 | _err "Can not connect to $nonceurl to get nonce." | |
2148 | return 1 | |
2149 | fi | |
0bc745f6 | 2150 | else |
2151 | _debug2 "Use _CACHED_NONCE" "$_CACHED_NONCE" | |
2152 | fi | |
2153 | nonce="$_CACHED_NONCE" | |
2154 | _debug2 nonce "$nonce" | |
aad309ee | 2155 | if [ -z "$nonce" ]; then |
2156 | _info "Could not get nonce, let's try again." | |
2157 | _sleep 2 | |
2158 | continue | |
2159 | fi | |
af3ea2d4 | 2160 | |
2161 | if [ "$url" = "$ACME_NEW_ACCOUNT" ]; then | |
c1151b0d | 2162 | protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}' |
af3ea2d4 | 2163 | elif [ "$url" = "$ACME_REVOKE_CERT" ] && [ "$keyfile" != "$ACCOUNT_KEY_PATH" ]; then |
2164 | protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}' | |
2165 | else | |
2166 | protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"kid\": \"${ACCOUNT_URL}\""'}' | |
c1151b0d | 2167 | fi |
af3ea2d4 | 2168 | |
0bc745f6 | 2169 | _debug3 protected "$protected" |
a272ee4f | 2170 | |
0bc745f6 | 2171 | protected64="$(printf "%s" "$protected" | _base64 | _url_replace)" |
2172 | _debug3 protected64 "$protected64" | |
4c2a3841 | 2173 | |
0bc745f6 | 2174 | if ! _sig_t="$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256")"; then |
2175 | _err "Sign request failed." | |
2176 | return 1 | |
2177 | fi | |
2178 | _debug3 _sig_t "$_sig_t" | |
166096dc | 2179 | |
0bc745f6 | 2180 | sig="$(printf "%s" "$_sig_t" | _url_replace)" |
2181 | _debug3 sig "$sig" | |
4c2a3841 | 2182 | |
8bd12ed0 | 2183 | body="{\"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}" |
0bc745f6 | 2184 | _debug3 body "$body" |
4c2a3841 | 2185 | |
6a66ba8a | 2186 | response="$(_post "$body" "$url" "$needbase64" "POST" "$__request_conent_type")" |
0bc745f6 | 2187 | _CACHED_NONCE="" |
bbbdcb09 | 2188 | |
0bc745f6 | 2189 | if [ "$?" != "0" ]; then |
2190 | _err "Can not post to $url" | |
2191 | return 1 | |
2192 | fi | |
4c3b3608 | 2193 | |
64802502 | 2194 | responseHeaders="$(cat "$HTTP_HEADER")" |
0bc745f6 | 2195 | _debug2 responseHeaders "$responseHeaders" |
f2acdd27 | 2196 | |
0bc745f6 | 2197 | code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")" |
2198 | _debug code "$code" | |
4c2a3841 | 2199 | |
f2acdd27 | 2200 | _debug2 original "$response" |
0712e989 | 2201 | if echo "$responseHeaders" | grep -i "Content-Type: *application/json" >/dev/null 2>&1; then |
cc8f2afc | 2202 | response="$(echo "$response" | _json_decode | _normalizeJson)" |
8148bfea | 2203 | fi |
f2acdd27 | 2204 | _debug2 response "$response" |
36309e6d | 2205 | |
91d37c78 | 2206 | _CACHED_NONCE="$(echo "$responseHeaders" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2 | cut -d , -f 1)" |
f2acdd27 | 2207 | |
a0ec5b18 | 2208 | if ! _startswith "$code" "2"; then |
f2acdd27 | 2209 | _body="$response" |
2210 | if [ "$needbase64" ]; then | |
2211 | _body="$(echo "$_body" | _dbase64 multiline)" | |
2212 | _debug3 _body "$_body" | |
2213 | fi | |
2214 | ||
2215 | if _contains "$_body" "JWS has invalid anti-replay nonce" || _contains "$_body" "JWS has an invalid anti-replay nonce"; then | |
2216 | _info "It seems the CA server is busy now, let's wait and retry. Sleeping $_sleep_retry_sec seconds." | |
2217 | _CACHED_NONCE="" | |
2218 | _sleep $_sleep_retry_sec | |
2219 | continue | |
2220 | fi | |
0f494c9d | 2221 | if _contains "$_body" "The Replay Nonce is not recognized"; then |
2222 | _info "The replay Nonce is not valid, let's get a new one, Sleeping $_sleep_retry_sec seconds." | |
2223 | _CACHED_NONCE="" | |
2224 | _sleep $_sleep_retry_sec | |
2225 | continue | |
2226 | fi | |
0bc745f6 | 2227 | fi |
a44ea0dd | 2228 | return 0 |
0bc745f6 | 2229 | done |
a44ea0dd | 2230 | _info "Giving up sending to CA server after $MAX_REQUEST_RETRY_TIMES retries." |
2231 | return 1 | |
4c3b3608 | 2232 | |
4c3b3608 | 2233 | } |
4c3b3608 | 2234 | |
2235 | #setopt "file" "opt" "=" "value" [";"] | |
2236 | _setopt() { | |
2237 | __conf="$1" | |
2238 | __opt="$2" | |
2239 | __sep="$3" | |
2240 | __val="$4" | |
2241 | __end="$5" | |
4c2a3841 | 2242 | if [ -z "$__opt" ]; then |
2243 | _usage usage: _setopt '"file" "opt" "=" "value" [";"]' | |
4c3b3608 | 2244 | return |
2245 | fi | |
4c2a3841 | 2246 | if [ ! -f "$__conf" ]; then |
4c3b3608 | 2247 | touch "$__conf" |
2248 | fi | |
2249 | ||
4c2a3841 | 2250 | if grep -n "^$__opt$__sep" "$__conf" >/dev/null; then |
22ea4004 | 2251 | _debug3 OK |
4c2a3841 | 2252 | if _contains "$__val" "&"; then |
79a267ab | 2253 | __val="$(echo "$__val" | sed 's/&/\\&/g')" |
4c3b3608 | 2254 | fi |
79a267ab | 2255 | text="$(cat "$__conf")" |
52f8b787 | 2256 | printf -- "%s\n" "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf" |
4c3b3608 | 2257 | |
4c2a3841 | 2258 | elif grep -n "^#$__opt$__sep" "$__conf" >/dev/null; then |
2259 | if _contains "$__val" "&"; then | |
79a267ab | 2260 | __val="$(echo "$__val" | sed 's/&/\\&/g')" |
4c3b3608 | 2261 | fi |
79a267ab | 2262 | text="$(cat "$__conf")" |
52f8b787 | 2263 | printf -- "%s\n" "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf" |
4c3b3608 | 2264 | |
2265 | else | |
22ea4004 | 2266 | _debug3 APP |
4c2a3841 | 2267 | echo "$__opt$__sep$__val$__end" >>"$__conf" |
4c3b3608 | 2268 | fi |
1efb2085 | 2269 | _debug3 "$(grep -n "^$__opt$__sep" "$__conf")" |
4c3b3608 | 2270 | } |
2271 | ||
7690f73e | 2272 | #_save_conf file key value base64encode |
8a29fbc8 | 2273 | #save to conf |
2274 | _save_conf() { | |
2275 | _s_c_f="$1" | |
2276 | _sdkey="$2" | |
2277 | _sdvalue="$3" | |
7690f73e | 2278 | _b64encode="$4" |
53c01882 | 2279 | if [ "$_sdvalue" ] && [ "$_b64encode" ]; then |
7690f73e | 2280 | _sdvalue="${B64CONF_START}$(printf "%s" "${_sdvalue}" | _base64)${B64CONF_END}" |
2281 | fi | |
4c2a3841 | 2282 | if [ "$_s_c_f" ]; then |
8a29fbc8 | 2283 | _setopt "$_s_c_f" "$_sdkey" "=" "'$_sdvalue'" |
4d2f38b0 | 2284 | else |
8a29fbc8 | 2285 | _err "config file is empty, can not save $_sdkey=$_sdvalue" |
4d2f38b0 | 2286 | fi |
2287 | } | |
2288 | ||
8a29fbc8 | 2289 | #_clear_conf file key |
2290 | _clear_conf() { | |
2291 | _c_c_f="$1" | |
2292 | _sdkey="$2" | |
4c2a3841 | 2293 | if [ "$_c_c_f" ]; then |
20ea8591 | 2294 | _conf_data="$(cat "$_c_c_f")" |
fa574fe8 | 2295 | echo "$_conf_data" | sed "s/^$_sdkey *=.*$//" >"$_c_c_f" |
4c3b3608 | 2296 | else |
8a29fbc8 | 2297 | _err "config file is empty, can not clear" |
4c3b3608 | 2298 | fi |
2299 | } | |
2300 | ||
8a29fbc8 | 2301 | #_read_conf file key |
2302 | _read_conf() { | |
2303 | _r_c_f="$1" | |
2304 | _sdkey="$2" | |
4c2a3841 | 2305 | if [ -f "$_r_c_f" ]; then |
0cfeee4d | 2306 | _sdv="$( |
2307 | eval "$(grep "^$_sdkey *=" "$_r_c_f")" | |
2308 | eval "printf \"%s\" \"\$$_sdkey\"" | |
c97e43dc | 2309 | )" |
7690f73e | 2310 | if _startswith "$_sdv" "${B64CONF_START}" && _endswith "$_sdv" "${B64CONF_END}"; then |
2311 | _sdv="$(echo "$_sdv" | sed "s/${B64CONF_START}//" | sed "s/${B64CONF_END}//" | _dbase64)" | |
2312 | fi | |
2313 | printf "%s" "$_sdv" | |
61623d22 | 2314 | else |
57e58ce7 | 2315 | _debug "config file is empty, can not read $_sdkey" |
4c3b3608 | 2316 | fi |
4c3b3608 | 2317 | } |
2318 | ||
7690f73e | 2319 | #_savedomainconf key value base64encode |
4c3b3608 | 2320 | #save to domain.conf |
2321 | _savedomainconf() { | |
7690f73e | 2322 | _save_conf "$DOMAIN_CONF" "$@" |
4d2f38b0 | 2323 | } |
2324 | ||
2325 | #_cleardomainconf key | |
2326 | _cleardomainconf() { | |
8a29fbc8 | 2327 | _clear_conf "$DOMAIN_CONF" "$1" |
4c3b3608 | 2328 | } |
2329 | ||
61623d22 | 2330 | #_readdomainconf key |
2331 | _readdomainconf() { | |
8a29fbc8 | 2332 | _read_conf "$DOMAIN_CONF" "$1" |
61623d22 | 2333 | } |
2334 | ||
9fb5bb62 PE |
2335 | #_migratedomainconf oldkey newkey base64encode |
2336 | _migratedomainconf() { | |
2337 | _old_key="$1" | |
2338 | _new_key="$2" | |
2339 | _b64encode="$3" | |
2340 | _value=$(_readdomainconf "$_old_key") | |
2341 | if [ -z "$_value" ]; then | |
2342 | return 1 # oldkey is not found | |
2343 | fi | |
2344 | _savedomainconf "$_new_key" "$_value" "$_b64encode" | |
2345 | _cleardomainconf "$_old_key" | |
2346 | _debug "Domain config $_old_key has been migrated to $_new_key" | |
2347 | } | |
2348 | ||
2349 | #_migratedeployconf oldkey newkey base64encode | |
2350 | _migratedeployconf() { | |
2351 | _migratedomainconf "$1" "SAVED_$2" "$3" || | |
2352 | _migratedomainconf "SAVED_$1" "SAVED_$2" "$3" # try only when oldkey itself is not found | |
2353 | } | |
2354 | ||
aec66362 | 2355 | #key value base64encode |
2356 | _savedeployconf() { | |
2357 | _savedomainconf "SAVED_$1" "$2" "$3" | |
2358 | #remove later | |
dc5eda7e | 2359 | _cleardomainconf "$1" |
aec66362 | 2360 | } |
2361 | ||
2362 | #key | |
2363 | _getdeployconf() { | |
2364 | _rac_key="$1" | |
2e3ddd3a | 2365 | _rac_value="$(eval echo \$"$_rac_key")" |
2366 | if [ "$_rac_value" ]; then | |
2367 | if _startswith "$_rac_value" '"' && _endswith "$_rac_value" '"'; then | |
2368 | _debug2 "trim quotation marks" | |
465ece5d | 2369 | eval "export $_rac_key=$_rac_value" |
2e3ddd3a | 2370 | fi |
aec66362 | 2371 | return 0 # do nothing |
2372 | fi | |
2373 | _saved=$(_readdomainconf "SAVED_$_rac_key") | |
25a8240d | 2374 | eval "export $_rac_key=\"\$_saved\"" |
aec66362 | 2375 | } |
2376 | ||
7690f73e | 2377 | #_saveaccountconf key value base64encode |
4c3b3608 | 2378 | _saveaccountconf() { |
7690f73e | 2379 | _save_conf "$ACCOUNT_CONF_PATH" "$@" |
4c3b3608 | 2380 | } |
2381 | ||
7690f73e | 2382 | #key value base64encode |
fcdf41ba | 2383 | _saveaccountconf_mutable() { |
7690f73e | 2384 | _save_conf "$ACCOUNT_CONF_PATH" "SAVED_$1" "$2" "$3" |
fcdf41ba | 2385 | #remove later |
2386 | _clearaccountconf "$1" | |
2387 | } | |
2388 | ||
2389 | #key | |
2390 | _readaccountconf() { | |
2391 | _read_conf "$ACCOUNT_CONF_PATH" "$1" | |
2392 | } | |
2393 | ||
2394 | #key | |
2395 | _readaccountconf_mutable() { | |
2396 | _rac_key="$1" | |
2397 | _readaccountconf "SAVED_$_rac_key" | |
2398 | } | |
2399 | ||
fac1e367 | 2400 | #_clearaccountconf key |
2401 | _clearaccountconf() { | |
8a29fbc8 | 2402 | _clear_conf "$ACCOUNT_CONF_PATH" "$1" |
2403 | } | |
2404 | ||
585c0c38 | 2405 | #key |
2406 | _clearaccountconf_mutable() { | |
2407 | _clearaccountconf "SAVED_$1" | |
2408 | #remove later | |
2409 | _clearaccountconf "$1" | |
2410 | } | |
2411 | ||
8a29fbc8 | 2412 | #_savecaconf key value |
2413 | _savecaconf() { | |
2414 | _save_conf "$CA_CONF" "$1" "$2" | |
2415 | } | |
2416 | ||
2417 | #_readcaconf key | |
2418 | _readcaconf() { | |
2419 | _read_conf "$CA_CONF" "$1" | |
2420 | } | |
2421 | ||
2422 | #_clearaccountconf key | |
2423 | _clearcaconf() { | |
2424 | _clear_conf "$CA_CONF" "$1" | |
fac1e367 | 2425 | } |
2426 | ||
0463b5d6 | 2427 | # content localaddress |
4c3b3608 | 2428 | _startserver() { |
2429 | content="$1" | |
0463b5d6 | 2430 | ncaddr="$2" |
e7f7e96d | 2431 | _debug "content" "$content" |
0463b5d6 | 2432 | _debug "ncaddr" "$ncaddr" |
2433 | ||
6fc1447f | 2434 | _debug "startserver: $$" |
4c2a3841 | 2435 | |
39c8f79f | 2436 | _debug Le_HTTPPort "$Le_HTTPPort" |
6ae0f7f5 | 2437 | _debug Le_Listen_V4 "$Le_Listen_V4" |
2438 | _debug Le_Listen_V6 "$Le_Listen_V6" | |
4c2a3841 | 2439 | |
3794b5cb | 2440 | _NC="socat" |
4c2a3841 | 2441 | if [ "$Le_Listen_V4" ]; then |
6ae0f7f5 | 2442 | _NC="$_NC -4" |
4c2a3841 | 2443 | elif [ "$Le_Listen_V6" ]; then |
6ae0f7f5 | 2444 | _NC="$_NC -6" |
2445 | fi | |
4c2a3841 | 2446 | |
9ad7ac63 | 2447 | if [ "$DEBUG" ] && [ "$DEBUG" -gt "1" ]; then |
5c568d69 | 2448 | _NC="$_NC -d -d -v" |
2449 | fi | |
2450 | ||
9134b6ea HC |
2451 | SOCAT_OPTIONS=TCP-LISTEN:$Le_HTTPPort,crlf,reuseaddr,fork |
2452 | ||
2453 | #Adding bind to local-address | |
d84665cb | 2454 | if [ "$ncaddr" ]; then |
9ad7ac63 | 2455 | SOCAT_OPTIONS="$SOCAT_OPTIONS,bind=${ncaddr}" |
9134b6ea HC |
2456 | fi |
2457 | ||
e7f7e96d | 2458 | _content_len="$(printf "%s" "$content" | wc -c)" |
2459 | _debug _content_len "$_content_len" | |
5c568d69 | 2460 | _debug "_NC" "$_NC $SOCAT_OPTIONS" |
e7f7e96d | 2461 | $_NC $SOCAT_OPTIONS SYSTEM:"sleep 1; \ |
2462 | echo 'HTTP/1.0 200 OK'; \ | |
2463 | echo 'Content-Length\: $_content_len'; \ | |
2464 | echo ''; \ | |
81b2d073 | 2465 | printf '%s' '$content';" & |
3794b5cb | 2466 | serverproc="$!" |
4c3b3608 | 2467 | } |
2468 | ||
4c2a3841 | 2469 | _stopserver() { |
4c3b3608 | 2470 | pid="$1" |
6fc1447f | 2471 | _debug "pid" "$pid" |
4c2a3841 | 2472 | if [ -z "$pid" ]; then |
6fc1447f | 2473 | return |
2474 | fi | |
e22bcf7c | 2475 | |
3794b5cb | 2476 | kill $pid |
2477 | ||
4c3b3608 | 2478 | } |
2479 | ||
fdcb6b72 | 2480 | # sleep sec |
2481 | _sleep() { | |
2482 | _sleep_sec="$1" | |
4c2a3841 | 2483 | if [ "$__INTERACTIVE" ]; then |
fdcb6b72 | 2484 | _sleep_c="$_sleep_sec" |
4c2a3841 | 2485 | while [ "$_sleep_c" -ge "0" ]; do |
c583d6bb | 2486 | printf "\r \r" |
fdcb6b72 | 2487 | __green "$_sleep_c" |
79a267ab | 2488 | _sleep_c="$(_math "$_sleep_c" - 1)" |
fdcb6b72 | 2489 | sleep 1 |
2490 | done | |
c583d6bb | 2491 | printf "\r" |
fdcb6b72 | 2492 | else |
2493 | sleep "$_sleep_sec" | |
2494 | fi | |
2495 | } | |
e22bcf7c | 2496 | |
08681f4a | 2497 | # _starttlsserver san_a san_b port content _ncaddr acmeValidationv1 |
e22bcf7c | 2498 | _starttlsserver() { |
2499 | _info "Starting tls server." | |
2500 | san_a="$1" | |
2501 | san_b="$2" | |
2502 | port="$3" | |
2503 | content="$4" | |
6ae0f7f5 | 2504 | opaddr="$5" |
08681f4a | 2505 | acmeValidationv1="$6" |
4c2a3841 | 2506 | |
e22bcf7c | 2507 | _debug san_a "$san_a" |
2508 | _debug san_b "$san_b" | |
2509 | _debug port "$port" | |
08681f4a | 2510 | _debug acmeValidationv1 "$acmeValidationv1" |
4c2a3841 | 2511 | |
e22bcf7c | 2512 | #create key TLS_KEY |
4c2a3841 | 2513 | if ! _createkey "2048" "$TLS_KEY"; then |
e22bcf7c | 2514 | _err "Create tls validation key error." |
2515 | return 1 | |
2516 | fi | |
4c2a3841 | 2517 | |
e22bcf7c | 2518 | #create csr |
2519 | alt="$san_a" | |
4c2a3841 | 2520 | if [ "$san_b" ]; then |
e22bcf7c | 2521 | alt="$alt,$san_b" |
2522 | fi | |
08681f4a | 2523 | if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$acmeValidationv1"; then |
e22bcf7c | 2524 | _err "Create tls validation csr error." |
2525 | return 1 | |
2526 | fi | |
4c2a3841 | 2527 | |
e22bcf7c | 2528 | #self signed |
4c2a3841 | 2529 | if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then |
e22bcf7c | 2530 | _err "Create tls validation cert error." |
2531 | return 1 | |
2532 | fi | |
4c2a3841 | 2533 | |
5f6e3da7 | 2534 | __S_OPENSSL="${ACME_OPENSSL_BIN:-openssl} s_server -www -cert $TLS_CERT -key $TLS_KEY " |
2535 | if [ "$opaddr" ]; then | |
2536 | __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port" | |
2537 | else | |
2538 | __S_OPENSSL="$__S_OPENSSL -accept $port" | |
2539 | fi | |
6ae0f7f5 | 2540 | |
2541 | _debug Le_Listen_V4 "$Le_Listen_V4" | |
2542 | _debug Le_Listen_V6 "$Le_Listen_V6" | |
4c2a3841 | 2543 | if [ "$Le_Listen_V4" ]; then |
6ae0f7f5 | 2544 | __S_OPENSSL="$__S_OPENSSL -4" |
4c2a3841 | 2545 | elif [ "$Le_Listen_V6" ]; then |
6ae0f7f5 | 2546 | __S_OPENSSL="$__S_OPENSSL -6" |
2547 | fi | |
4c2a3841 | 2548 | |
08681f4a | 2549 | if [ "$acmeValidationv1" ]; then |
2550 | __S_OPENSSL="$__S_OPENSSL -alpn acme-tls/1" | |
2551 | fi | |
2552 | ||
6ae0f7f5 | 2553 | _debug "$__S_OPENSSL" |
5f6e3da7 | 2554 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
2555 | $__S_OPENSSL -tlsextdebug & | |
2556 | else | |
2557 | $__S_OPENSSL >/dev/null 2>&1 & | |
2558 | fi | |
331c4bb6 | 2559 | |
e22bcf7c | 2560 | serverproc="$!" |
5dbf664a | 2561 | sleep 1 |
d5ec5f80 | 2562 | _debug serverproc "$serverproc" |
e22bcf7c | 2563 | } |
2564 | ||
18e46962 | 2565 | #file |
2566 | _readlink() { | |
2567 | _rf="$1" | |
2568 | if ! readlink -f "$_rf" 2>/dev/null; then | |
6c4cc357 | 2569 | if _startswith "$_rf" "/"; then |
2570 | echo "$_rf" | |
7da50703 | 2571 | return 0 |
2572 | fi | |
6c4cc357 | 2573 | echo "$(pwd)/$_rf" | _conapath |
18e46962 | 2574 | fi |
2575 | } | |
2576 | ||
6c4cc357 | 2577 | _conapath() { |
2578 | sed "s#/\./#/#g" | |
2579 | } | |
2580 | ||
5ea6e9c9 | 2581 | __initHome() { |
4c2a3841 | 2582 | if [ -z "$_SCRIPT_HOME" ]; then |
2583 | if _exists readlink && _exists dirname; then | |
66990cf8 | 2584 | _debug "Lets find script dir." |
f3e4cea3 | 2585 | _debug "_SCRIPT_" "$_SCRIPT_" |
18e46962 | 2586 | _script="$(_readlink "$_SCRIPT_")" |
f3e4cea3 | 2587 | _debug "_script" "$_script" |
2588 | _script_home="$(dirname "$_script")" | |
2589 | _debug "_script_home" "$_script_home" | |
4c2a3841 | 2590 | if [ -d "$_script_home" ]; then |
f3e4cea3 | 2591 | _SCRIPT_HOME="$_script_home" |
2592 | else | |
2593 | _err "It seems the script home is not correct:$_script_home" | |
2594 | fi | |
2595 | fi | |
2596 | fi | |
2597 | ||
219e9115 | 2598 | # if [ -z "$LE_WORKING_DIR" ]; then |
2599 | # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then | |
2600 | # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME" | |
2601 | # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" | |
2602 | # else | |
2603 | # LE_WORKING_DIR="$_SCRIPT_HOME" | |
2604 | # fi | |
2605 | # fi | |
4c2a3841 | 2606 | |
2607 | if [ -z "$LE_WORKING_DIR" ]; then | |
f3e4cea3 | 2608 | _debug "Using default home:$DEFAULT_INSTALL_HOME" |
2609 | LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" | |
2610 | fi | |
7da50703 | 2611 | export LE_WORKING_DIR |
f3e4cea3 | 2612 | |
f5b546b3 | 2613 | if [ -z "$LE_CONFIG_HOME" ]; then |
2614 | LE_CONFIG_HOME="$LE_WORKING_DIR" | |
27dbe77f | 2615 | fi |
f5b546b3 | 2616 | _debug "Using config home:$LE_CONFIG_HOME" |
2617 | export LE_CONFIG_HOME | |
27dbe77f | 2618 | |
f5b546b3 | 2619 | _DEFAULT_ACCOUNT_CONF_PATH="$LE_CONFIG_HOME/account.conf" |
d53289d7 | 2620 | |
4c2a3841 | 2621 | if [ -z "$ACCOUNT_CONF_PATH" ]; then |
2622 | if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then | |
8663fb7e | 2623 | . "$_DEFAULT_ACCOUNT_CONF_PATH" |
635695ec | 2624 | fi |
d53289d7 | 2625 | fi |
4c2a3841 | 2626 | |
2627 | if [ -z "$ACCOUNT_CONF_PATH" ]; then | |
d53289d7 | 2628 | ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH" |
4c3b3608 | 2629 | fi |
f8f53a6b | 2630 | _debug3 ACCOUNT_CONF_PATH "$ACCOUNT_CONF_PATH" |
f5b546b3 | 2631 | DEFAULT_LOG_FILE="$LE_CONFIG_HOME/$PROJECT_NAME.log" |
4c2a3841 | 2632 | |
f5b546b3 | 2633 | DEFAULT_CA_HOME="$LE_CONFIG_HOME/ca" |
4c2a3841 | 2634 | |
2635 | if [ -z "$LE_TEMP_DIR" ]; then | |
f5b546b3 | 2636 | LE_TEMP_DIR="$LE_CONFIG_HOME/tmp" |
610e0f21 | 2637 | fi |
5ea6e9c9 | 2638 | } |
2639 | ||
17520043 | 2640 | _clearAPI() { |
2641 | ACME_NEW_ACCOUNT="" | |
2642 | ACME_KEY_CHANGE="" | |
2643 | ACME_NEW_AUTHZ="" | |
2644 | ACME_NEW_ORDER="" | |
2645 | ACME_REVOKE_CERT="" | |
2646 | ACME_NEW_NONCE="" | |
2647 | ACME_AGREEMENT="" | |
2648 | } | |
2649 | ||
48d9a8c1 | 2650 | #server |
2651 | _initAPI() { | |
2652 | _api_server="${1:-$ACME_DIRECTORY}" | |
2653 | _debug "_init api for server: $_api_server" | |
4cee14f3 | 2654 | |
014e0160 | 2655 | MAX_API_RETRY_TIMES=10 |
2656 | _sleep_retry_sec=10 | |
2657 | _request_retry_times=0 | |
2658 | while [ -z "$ACME_NEW_ACCOUNT" ] && [ "${_request_retry_times}" -lt "$MAX_API_RETRY_TIMES" ]; do | |
2659 | _request_retry_times=$(_math "$_request_retry_times" + 1) | |
48d9a8c1 | 2660 | response=$(_get "$_api_server") |
2661 | if [ "$?" != "0" ]; then | |
2662 | _debug2 "response" "$response" | |
014e0160 | 2663 | _info "Can not init api for: $_api_server." |
2664 | _info "Sleep $_sleep_retry_sec and retry." | |
2665 | _sleep "$_sleep_retry_sec" | |
2666 | continue | |
48d9a8c1 | 2667 | fi |
d8bd45c2 | 2668 | response=$(echo "$response" | _json_decode) |
48d9a8c1 | 2669 | _debug2 "response" "$response" |
2670 | ||
af3ea2d4 | 2671 | ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'keyChange" *: *"[^"]*"' | cut -d '"' -f 3) |
48d9a8c1 | 2672 | export ACME_KEY_CHANGE |
2673 | ||
af3ea2d4 | 2674 | ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'newAuthz" *: *"[^"]*"' | cut -d '"' -f 3) |
48d9a8c1 | 2675 | export ACME_NEW_AUTHZ |
2676 | ||
af3ea2d4 | 2677 | ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'newOrder" *: *"[^"]*"' | cut -d '"' -f 3) |
cae50e16 | 2678 | export ACME_NEW_ORDER |
af3ea2d4 | 2679 | |
2680 | ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'newAccount" *: *"[^"]*"' | cut -d '"' -f 3) | |
cae50e16 | 2681 | export ACME_NEW_ACCOUNT |
48d9a8c1 | 2682 | |
af3ea2d4 | 2683 | ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revokeCert" *: *"[^"]*"' | cut -d '"' -f 3) |
48d9a8c1 | 2684 | export ACME_REVOKE_CERT |
2685 | ||
af3ea2d4 | 2686 | ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'newNonce" *: *"[^"]*"' | cut -d '"' -f 3) |
cae50e16 | 2687 | export ACME_NEW_NONCE |
4249e13e | 2688 | |
af3ea2d4 | 2689 | ACME_AGREEMENT=$(echo "$response" | _egrep_o 'termsOfService" *: *"[^"]*"' | cut -d '"' -f 3) |
f87890cb | 2690 | export ACME_AGREEMENT |
cae50e16 | 2691 | |
f87890cb | 2692 | _debug "ACME_KEY_CHANGE" "$ACME_KEY_CHANGE" |
2693 | _debug "ACME_NEW_AUTHZ" "$ACME_NEW_AUTHZ" | |
2694 | _debug "ACME_NEW_ORDER" "$ACME_NEW_ORDER" | |
2695 | _debug "ACME_NEW_ACCOUNT" "$ACME_NEW_ACCOUNT" | |
2696 | _debug "ACME_REVOKE_CERT" "$ACME_REVOKE_CERT" | |
2697 | _debug "ACME_AGREEMENT" "$ACME_AGREEMENT" | |
c1151b0d | 2698 | _debug "ACME_NEW_NONCE" "$ACME_NEW_NONCE" |
9daeae16 | 2699 | if [ "$ACME_NEW_ACCOUNT" ] && [ "$ACME_NEW_ORDER" ]; then |
014e0160 | 2700 | return 0 |
2701 | fi | |
29fe1c86 | 2702 | _info "Sleep $_sleep_retry_sec and retry." |
2703 | _sleep "$_sleep_retry_sec" | |
014e0160 | 2704 | done |
13ab9844 | 2705 | if [ "$ACME_NEW_ACCOUNT" ] && [ "$ACME_NEW_ORDER" ]; then |
2706 | return 0 | |
2707 | fi | |
014e0160 | 2708 | _err "Can not init api, for $_api_server" |
2709 | return 1 | |
48d9a8c1 | 2710 | } |
2711 | ||
8d783e8e | 2712 | _clearCA() { |
8d783e8e | 2713 | export CA_CONF= |
2714 | export ACCOUNT_KEY_PATH= | |
2715 | export ACCOUNT_JSON_PATH= | |
2716 | } | |
2717 | ||
3281043e | 2718 | #[domain] [keylength or isEcc flag] |
5ea6e9c9 | 2719 | _initpath() { |
cd9fb3b6 | 2720 | domain="$1" |
2721 | _ilength="$2" | |
5ea6e9c9 | 2722 | |
2723 | __initHome | |
2724 | ||
4c2a3841 | 2725 | if [ -f "$ACCOUNT_CONF_PATH" ]; then |
8663fb7e | 2726 | . "$ACCOUNT_CONF_PATH" |
4c3b3608 | 2727 | fi |
2728 | ||
bd04638d | 2729 | if [ "$_ACME_IN_CRON" ]; then |
4c2a3841 | 2730 | if [ ! "$_USER_PATH_EXPORTED" ]; then |
281aa349 | 2731 | _USER_PATH_EXPORTED=1 |
2732 | export PATH="$USER_PATH:$PATH" | |
2733 | fi | |
2734 | fi | |
4c2a3841 | 2735 | |
2736 | if [ -z "$CA_HOME" ]; then | |
5c48e139 | 2737 | CA_HOME="$DEFAULT_CA_HOME" |
2738 | fi | |
281aa349 | 2739 | |
48d9a8c1 | 2740 | if [ -z "$ACME_DIRECTORY" ]; then |
b3a801df | 2741 | if [ "$STAGE" ]; then |
2742 | ACME_DIRECTORY="$DEFAULT_STAGING_CA" | |
bd04638d | 2743 | _info "Using ACME_DIRECTORY: $ACME_DIRECTORY" |
4c3b3608 | 2744 | else |
b3a801df | 2745 | default_acme_server=$(_readaccountconf "DEFAULT_ACME_SERVER") |
2746 | _debug default_acme_server "$default_acme_server" | |
2747 | if [ "$default_acme_server" ]; then | |
2748 | ACME_DIRECTORY="$default_acme_server" | |
737e9e48 | 2749 | else |
b3a801df | 2750 | ACME_DIRECTORY="$DEFAULT_CA" |
737e9e48 | 2751 | fi |
4c2a3841 | 2752 | fi |
4c3b3608 | 2753 | fi |
4c2a3841 | 2754 | |
66444663 | 2755 | _debug ACME_DIRECTORY "$ACME_DIRECTORY" |
98394f99 | 2756 | _ACME_SERVER_HOST="$(echo "$ACME_DIRECTORY" | cut -d : -f 2 | tr -s / | cut -d / -f 2)" |
48d9a8c1 | 2757 | _debug2 "_ACME_SERVER_HOST" "$_ACME_SERVER_HOST" |
2758 | ||
53d6ab6c | 2759 | _ACME_SERVER_PATH="$(echo "$ACME_DIRECTORY" | cut -d : -f 2- | tr -s / | cut -d / -f 3-)" |
2760 | _debug2 "_ACME_SERVER_PATH" "$_ACME_SERVER_PATH" | |
4c2a3841 | 2761 | |
593e8e1f | 2762 | CA_DIR="$CA_HOME/$_ACME_SERVER_HOST/$_ACME_SERVER_PATH" |
5c48e139 | 2763 | _DEFAULT_CA_CONF="$CA_DIR/ca.conf" |
4c2a3841 | 2764 | if [ -z "$CA_CONF" ]; then |
5c48e139 | 2765 | CA_CONF="$_DEFAULT_CA_CONF" |
2766 | fi | |
c4236e58 | 2767 | _debug3 CA_CONF "$CA_CONF" |
4c2a3841 | 2768 | |
593e8e1f | 2769 | _OLD_CADIR="$CA_HOME/$_ACME_SERVER_HOST" |
2770 | _OLD_ACCOUNT_KEY="$_OLD_CADIR/account.key" | |
2771 | _OLD_ACCOUNT_JSON="$_OLD_CADIR/account.json" | |
2772 | _OLD_CA_CONF="$_OLD_CADIR/ca.conf" | |
2773 | ||
593e8e1f | 2774 | _DEFAULT_ACCOUNT_KEY_PATH="$CA_DIR/account.key" |
2775 | _DEFAULT_ACCOUNT_JSON_PATH="$CA_DIR/account.json" | |
2776 | if [ -z "$ACCOUNT_KEY_PATH" ]; then | |
2777 | ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH" | |
2778 | if [ -f "$_OLD_ACCOUNT_KEY" ] && ! [ -f "$ACCOUNT_KEY_PATH" ]; then | |
2779 | mkdir -p "$CA_DIR" | |
2780 | mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH" | |
2781 | fi | |
2782 | fi | |
2783 | ||
2784 | if [ -z "$ACCOUNT_JSON_PATH" ]; then | |
2785 | ACCOUNT_JSON_PATH="$_DEFAULT_ACCOUNT_JSON_PATH" | |
2786 | if [ -f "$_OLD_ACCOUNT_JSON" ] && ! [ -f "$ACCOUNT_JSON_PATH" ]; then | |
2787 | mkdir -p "$CA_DIR" | |
2788 | mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH" | |
2789 | fi | |
2790 | fi | |
2791 | ||
2792 | if [ -f "$_OLD_CA_CONF" ] && ! [ -f "$CA_CONF" ]; then | |
2793 | mkdir -p "$CA_DIR" | |
2794 | mv "$_OLD_CA_CONF" "$CA_CONF" | |
2795 | fi | |
2796 | ||
4c2a3841 | 2797 | if [ -f "$CA_CONF" ]; then |
5c48e139 | 2798 | . "$CA_CONF" |
2799 | fi | |
2800 | ||
4c2a3841 | 2801 | if [ -z "$ACME_DIR" ]; then |
4c3b3608 | 2802 | ACME_DIR="/home/.acme" |
2803 | fi | |
4c2a3841 | 2804 | |
2805 | if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then | |
f5b546b3 | 2806 | APACHE_CONF_BACKUP_DIR="$LE_CONFIG_HOME" |
4c3b3608 | 2807 | fi |
4c2a3841 | 2808 | |
2809 | if [ -z "$USER_AGENT" ]; then | |
bbbdcb09 | 2810 | USER_AGENT="$DEFAULT_USER_AGENT" |
2811 | fi | |
4c2a3841 | 2812 | |
2813 | if [ -z "$HTTP_HEADER" ]; then | |
f5b546b3 | 2814 | HTTP_HEADER="$LE_CONFIG_HOME/http.header" |
933c169d | 2815 | fi |
b2817897 | 2816 | |
f5b546b3 | 2817 | _DEFAULT_CERT_HOME="$LE_CONFIG_HOME" |
4c2a3841 | 2818 | if [ -z "$CERT_HOME" ]; then |
a79b26af RD |
2819 | CERT_HOME="$_DEFAULT_CERT_HOME" |
2820 | fi | |
2821 | ||
77f1ea40 | 2822 | if [ -z "$ACME_OPENSSL_BIN" ] || [ ! -f "$ACME_OPENSSL_BIN" ] || [ ! -x "$ACME_OPENSSL_BIN" ]; then |
851fedf7 | 2823 | ACME_OPENSSL_BIN="$DEFAULT_OPENSSL_BIN" |
a746139c | 2824 | fi |
2825 | ||
cd9fb3b6 | 2826 | if [ -z "$domain" ]; then |
4c3b3608 | 2827 | return 0 |
2828 | fi | |
4c2a3841 | 2829 | |
4c2a3841 | 2830 | if [ -z "$DOMAIN_PATH" ]; then |
43822d37 | 2831 | domainhome="$CERT_HOME/$domain" |
2832 | domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX" | |
4c2a3841 | 2833 | |
4c3b3608 | 2834 | DOMAIN_PATH="$domainhome" |
4c2a3841 | 2835 | |
2836 | if _isEccKey "$_ilength"; then | |
43822d37 | 2837 | DOMAIN_PATH="$domainhomeecc" |
2838 | else | |
4c2a3841 | 2839 | if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then |
6d4e903b | 2840 | _info "The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert." |
43822d37 | 2841 | fi |
2842 | fi | |
2843 | _debug DOMAIN_PATH "$DOMAIN_PATH" | |
4c3b3608 | 2844 | fi |
4c2a3841 | 2845 | |
fd72cced | 2846 | if [ -z "$DOMAIN_BACKUP_PATH" ]; then |
d88f8e86 | 2847 | DOMAIN_BACKUP_PATH="$DOMAIN_PATH/backup" |
fd72cced | 2848 | fi |
2849 | ||
4c2a3841 | 2850 | if [ -z "$DOMAIN_CONF" ]; then |
43822d37 | 2851 | DOMAIN_CONF="$DOMAIN_PATH/$domain.conf" |
4c3b3608 | 2852 | fi |
4c2a3841 | 2853 | |
2854 | if [ -z "$DOMAIN_SSL_CONF" ]; then | |
0c9546cc | 2855 | DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf" |
4c3b3608 | 2856 | fi |
4c2a3841 | 2857 | |
2858 | if [ -z "$CSR_PATH" ]; then | |
43822d37 | 2859 | CSR_PATH="$DOMAIN_PATH/$domain.csr" |
4c3b3608 | 2860 | fi |
4c2a3841 | 2861 | if [ -z "$CERT_KEY_PATH" ]; then |
43822d37 | 2862 | CERT_KEY_PATH="$DOMAIN_PATH/$domain.key" |
4c3b3608 | 2863 | fi |
4c2a3841 | 2864 | if [ -z "$CERT_PATH" ]; then |
43822d37 | 2865 | CERT_PATH="$DOMAIN_PATH/$domain.cer" |
4c3b3608 | 2866 | fi |
4c2a3841 | 2867 | if [ -z "$CA_CERT_PATH" ]; then |
43822d37 | 2868 | CA_CERT_PATH="$DOMAIN_PATH/ca.cer" |
4c3b3608 | 2869 | fi |
4c2a3841 | 2870 | if [ -z "$CERT_FULLCHAIN_PATH" ]; then |
43822d37 | 2871 | CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer" |
caf1fc10 | 2872 | fi |
4c2a3841 | 2873 | if [ -z "$CERT_PFX_PATH" ]; then |
43822d37 | 2874 | CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx" |
ac2d5123 | 2875 | fi |
4410226d | 2876 | if [ -z "$CERT_PKCS8_PATH" ]; then |
2877 | CERT_PKCS8_PATH="$DOMAIN_PATH/$domain.pkcs8" | |
2878 | fi | |
4c2a3841 | 2879 | |
2880 | if [ -z "$TLS_CONF" ]; then | |
f94433e5 | 2881 | TLS_CONF="$DOMAIN_PATH/tls.validation.conf" |
e22bcf7c | 2882 | fi |
4c2a3841 | 2883 | if [ -z "$TLS_CERT" ]; then |
f94433e5 | 2884 | TLS_CERT="$DOMAIN_PATH/tls.validation.cert" |
e22bcf7c | 2885 | fi |
4c2a3841 | 2886 | if [ -z "$TLS_KEY" ]; then |
f94433e5 | 2887 | TLS_KEY="$DOMAIN_PATH/tls.validation.key" |
e22bcf7c | 2888 | fi |
4c2a3841 | 2889 | if [ -z "$TLS_CSR" ]; then |
f94433e5 | 2890 | TLS_CSR="$DOMAIN_PATH/tls.validation.csr" |
e22bcf7c | 2891 | fi |
4c2a3841 | 2892 | |
4c3b3608 | 2893 | } |
2894 | ||
610e0f21 | 2895 | _exec() { |
4c2a3841 | 2896 | if [ -z "$_EXEC_TEMP_ERR" ]; then |
610e0f21 | 2897 | _EXEC_TEMP_ERR="$(_mktemp)" |
2898 | fi | |
2899 | ||
4c2a3841 | 2900 | if [ "$_EXEC_TEMP_ERR" ]; then |
3e5b1024 | 2901 | eval "$@ 2>>$_EXEC_TEMP_ERR" |
610e0f21 | 2902 | else |
3e5b1024 | 2903 | eval "$@" |
610e0f21 | 2904 | fi |
2905 | } | |
2906 | ||
2907 | _exec_err() { | |
3e5b1024 | 2908 | [ "$_EXEC_TEMP_ERR" ] && _err "$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR" |
610e0f21 | 2909 | } |
4c3b3608 | 2910 | |
2911 | _apachePath() { | |
c3dd3ef0 | 2912 | _APACHECTL="apachectl" |
4c2a3841 | 2913 | if ! _exists apachectl; then |
2914 | if _exists apache2ctl; then | |
2915 | _APACHECTL="apache2ctl" | |
e4a19585 | 2916 | else |
bc96082f | 2917 | _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'" |
e4a19585 | 2918 | _err "Please use webroot mode to try again." |
2919 | return 1 | |
2920 | fi | |
80a0a7b5 | 2921 | fi |
4c2a3841 | 2922 | |
2923 | if ! _exec $_APACHECTL -V >/dev/null; then | |
610e0f21 | 2924 | _exec_err |
2925 | return 1 | |
2926 | fi | |
4c2a3841 | 2927 | |
2928 | if [ "$APACHE_HTTPD_CONF" ]; then | |
5be1449d | 2929 | _saveaccountconf APACHE_HTTPD_CONF "$APACHE_HTTPD_CONF" |
2930 | httpdconf="$APACHE_HTTPD_CONF" | |
79a267ab | 2931 | httpdconfname="$(basename "$httpdconfname")" |
d62ee940 | 2932 | else |
4c2a3841 | 2933 | httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')" |
5be1449d | 2934 | _debug httpdconfname "$httpdconfname" |
4c2a3841 | 2935 | |
2936 | if [ -z "$httpdconfname" ]; then | |
5be1449d | 2937 | _err "Can not read apache config file." |
2938 | return 1 | |
2939 | fi | |
4c2a3841 | 2940 | |
2941 | if _startswith "$httpdconfname" '/'; then | |
5be1449d | 2942 | httpdconf="$httpdconfname" |
79a267ab | 2943 | httpdconfname="$(basename "$httpdconfname")" |
5be1449d | 2944 | else |
4c2a3841 | 2945 | httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')" |
5be1449d | 2946 | _debug httpdroot "$httpdroot" |
2947 | httpdconf="$httpdroot/$httpdconfname" | |
79a267ab | 2948 | httpdconfname="$(basename "$httpdconfname")" |
5be1449d | 2949 | fi |
d62ee940 | 2950 | fi |
78768e98 | 2951 | _debug httpdconf "$httpdconf" |
8f63baf7 | 2952 | _debug httpdconfname "$httpdconfname" |
4c2a3841 | 2953 | if [ ! -f "$httpdconf" ]; then |
78768e98 | 2954 | _err "Apache Config file not found" "$httpdconf" |
4c3b3608 | 2955 | return 1 |
2956 | fi | |
2957 | return 0 | |
2958 | } | |
2959 | ||
2960 | _restoreApache() { | |
4c2a3841 | 2961 | if [ -z "$usingApache" ]; then |
4c3b3608 | 2962 | return 0 |
2963 | fi | |
2964 | _initpath | |
4c2a3841 | 2965 | if ! _apachePath; then |
4c3b3608 | 2966 | return 1 |
2967 | fi | |
4c2a3841 | 2968 | |
2969 | if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then | |
4c3b3608 | 2970 | _debug "No config file to restore." |
2971 | return 0 | |
2972 | fi | |
4c2a3841 | 2973 | |
2974 | cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf" | |
5ef501c5 | 2975 | _debug "Restored: $httpdconf." |
4c2a3841 | 2976 | if ! _exec $_APACHECTL -t; then |
610e0f21 | 2977 | _exec_err |
4c3b3608 | 2978 | _err "Sorry, restore apache config error, please contact me." |
4c2a3841 | 2979 | return 1 |
4c3b3608 | 2980 | fi |
5ef501c5 | 2981 | _debug "Restored successfully." |
4c3b3608 | 2982 | rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" |
4c2a3841 | 2983 | return 0 |
4c3b3608 | 2984 | } |
2985 | ||
2986 | _setApache() { | |
2987 | _initpath | |
4c2a3841 | 2988 | if ! _apachePath; then |
4c3b3608 | 2989 | return 1 |
2990 | fi | |
2991 | ||
5fc5016d | 2992 | #test the conf first |
869578ce | 2993 | _info "Checking if there is an error in the apache config file before starting." |
4c2a3841 | 2994 | |
44edb2bd | 2995 | if ! _exec "$_APACHECTL" -t >/dev/null; then |
610e0f21 | 2996 | _exec_err |
2997 | _err "The apache config file has error, please fix it first, then try again." | |
869578ce | 2998 | _err "Don't worry, there is nothing changed to your system." |
4c2a3841 | 2999 | return 1 |
5fc5016d | 3000 | else |
3001 | _info "OK" | |
3002 | fi | |
4c2a3841 | 3003 | |
4c3b3608 | 3004 | #backup the conf |
5778811a | 3005 | _debug "Backup apache config file" "$httpdconf" |
4c2a3841 | 3006 | if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then |
869578ce | 3007 | _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed." |
61a48a5b | 3008 | _err "This might be a bug of $PROJECT_NAME , please report issue: $PROJECT" |
8f63baf7 | 3009 | return 1 |
3010 | fi | |
4c3b3608 | 3011 | _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname" |
3012 | _info "In case there is an error that can not be restored automatically, you may try restore it yourself." | |
329174b6 | 3013 | _info "The backup file will be deleted on success, just forget it." |
4c2a3841 | 3014 | |
4c3b3608 | 3015 | #add alias |
4c2a3841 | 3016 | |
3017 | apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)" | |
b09d597c | 3018 | _debug "apacheVer" "$apacheVer" |
94787d53 | 3019 | apacheMajor="$(echo "$apacheVer" | cut -d . -f 1)" |
b09d597c | 3020 | apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)" |
3021 | ||
94787d53 | 3022 | if [ "$apacheVer" ] && [ "$apacheMajor$apacheMinor" -ge "24" ]; then |
b09d597c | 3023 | echo " |
4c3b3608 | 3024 | Alias /.well-known/acme-challenge $ACME_DIR |
3025 | ||
3026 | <Directory $ACME_DIR > | |
3027 | Require all granted | |
b09d597c | 3028 | </Directory> |
4c2a3841 | 3029 | " >>"$httpdconf" |
b09d597c | 3030 | else |
3031 | echo " | |
3032 | Alias /.well-known/acme-challenge $ACME_DIR | |
3033 | ||
3034 | <Directory $ACME_DIR > | |
3035 | Order allow,deny | |
3036 | Allow from all | |
4c3b3608 | 3037 | </Directory> |
4c2a3841 | 3038 | " >>"$httpdconf" |
b09d597c | 3039 | fi |
3040 | ||
4c2a3841 | 3041 | _msg="$($_APACHECTL -t 2>&1)" |
3042 | if [ "$?" != "0" ]; then | |
5fc5016d | 3043 | _err "Sorry, apache config error" |
4c2a3841 | 3044 | if _restoreApache; then |
869578ce | 3045 | _err "The apache config file is restored." |
5fc5016d | 3046 | else |
2e87e64b | 3047 | _err "Sorry, the apache config file can not be restored, please report bug." |
5fc5016d | 3048 | fi |
4c2a3841 | 3049 | return 1 |
4c3b3608 | 3050 | fi |
4c2a3841 | 3051 | |
3052 | if [ ! -d "$ACME_DIR" ]; then | |
4c3b3608 | 3053 | mkdir -p "$ACME_DIR" |
3054 | chmod 755 "$ACME_DIR" | |
3055 | fi | |
4c2a3841 | 3056 | |
44edb2bd | 3057 | if ! _exec "$_APACHECTL" graceful; then |
4c2a3841 | 3058 | _exec_err |
610e0f21 | 3059 | _err "$_APACHECTL graceful error, please contact me." |
4c3b3608 | 3060 | _restoreApache |
4c2a3841 | 3061 | return 1 |
4c3b3608 | 3062 | fi |
3063 | usingApache="1" | |
3064 | return 0 | |
3065 | } | |
3066 | ||
9d725af6 | 3067 | #find the real nginx conf file |
3068 | #backup | |
3069 | #set the nginx conf | |
3070 | #returns the real nginx conf file | |
3071 | _setNginx() { | |
3072 | _d="$1" | |
3073 | _croot="$2" | |
3074 | _thumbpt="$3" | |
37f39c08 | 3075 | |
9d725af6 | 3076 | FOUND_REAL_NGINX_CONF="" |
9f90618a | 3077 | FOUND_REAL_NGINX_CONF_LN="" |
9d725af6 | 3078 | BACKUP_NGINX_CONF="" |
3079 | _debug _croot "$_croot" | |
3080 | _start_f="$(echo "$_croot" | cut -d : -f 2)" | |
3081 | _debug _start_f "$_start_f" | |
3082 | if [ -z "$_start_f" ]; then | |
3083 | _debug "find start conf from nginx command" | |
3084 | if [ -z "$NGINX_CONF" ]; then | |
37f39c08 | 3085 | if ! _exists "nginx"; then |
3086 | _err "nginx command is not found." | |
3087 | return 1 | |
3088 | fi | |
9d725af6 | 3089 | NGINX_CONF="$(nginx -V 2>&1 | _egrep_o "--conf-path=[^ ]* " | tr -d " ")" |
3090 | _debug NGINX_CONF "$NGINX_CONF" | |
3091 | NGINX_CONF="$(echo "$NGINX_CONF" | cut -d = -f 2)" | |
3092 | _debug NGINX_CONF "$NGINX_CONF" | |
1081d98b | 3093 | if [ -z "$NGINX_CONF" ]; then |
3094 | _err "Can not find nginx conf." | |
3095 | NGINX_CONF="" | |
3096 | return 1 | |
3097 | fi | |
9d725af6 | 3098 | if [ ! -f "$NGINX_CONF" ]; then |
3099 | _err "'$NGINX_CONF' doesn't exist." | |
3100 | NGINX_CONF="" | |
3101 | return 1 | |
3102 | fi | |
3103 | _debug "Found nginx conf file:$NGINX_CONF" | |
3104 | fi | |
3105 | _start_f="$NGINX_CONF" | |
3106 | fi | |
03f8d6e9 | 3107 | _debug "Start detect nginx conf for $_d from:$_start_f" |
9d725af6 | 3108 | if ! _checkConf "$_d" "$_start_f"; then |
5378d9ca | 3109 | _err "Can not find conf file for domain $d" |
9d725af6 | 3110 | return 1 |
3111 | fi | |
3112 | _info "Found conf file: $FOUND_REAL_NGINX_CONF" | |
3113 | ||
9f90618a | 3114 | _ln=$FOUND_REAL_NGINX_CONF_LN |
03f8d6e9 | 3115 | _debug "_ln" "$_ln" |
3116 | ||
3117 | _lnn=$(_math $_ln + 1) | |
3118 | _debug _lnn "$_lnn" | |
3119 | _start_tag="$(sed -n "$_lnn,${_lnn}p" "$FOUND_REAL_NGINX_CONF")" | |
3120 | _debug "_start_tag" "$_start_tag" | |
3121 | if [ "$_start_tag" = "$NGINX_START" ]; then | |
3122 | _info "The domain $_d is already configured, skip" | |
3123 | FOUND_REAL_NGINX_CONF="" | |
3124 | return 0 | |
3125 | fi | |
3126 | ||
9d725af6 | 3127 | mkdir -p "$DOMAIN_BACKUP_PATH" |
3128 | _backup_conf="$DOMAIN_BACKUP_PATH/$_d.nginx.conf" | |
3129 | _debug _backup_conf "$_backup_conf" | |
3130 | BACKUP_NGINX_CONF="$_backup_conf" | |
3131 | _info "Backup $FOUND_REAL_NGINX_CONF to $_backup_conf" | |
3132 | if ! cp "$FOUND_REAL_NGINX_CONF" "$_backup_conf"; then | |
3133 | _err "backup error." | |
3134 | FOUND_REAL_NGINX_CONF="" | |
3135 | return 1 | |
3136 | fi | |
3137 | ||
37f39c08 | 3138 | if ! _exists "nginx"; then |
3139 | _err "nginx command is not found." | |
3140 | return 1 | |
3141 | fi | |
9d725af6 | 3142 | _info "Check the nginx conf before setting up." |
3143 | if ! _exec "nginx -t" >/dev/null; then | |
3144 | _exec_err | |
3145 | return 1 | |
3146 | fi | |
3147 | ||
3148 | _info "OK, Set up nginx config file" | |
9d725af6 | 3149 | |
302c41ed | 3150 | if ! sed -n "1,${_ln}p" "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"; then |
03f8d6e9 | 3151 | cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF" |
9d725af6 | 3152 | _err "write nginx conf error, but don't worry, the file is restored to the original version." |
3153 | return 1 | |
3154 | fi | |
3155 | ||
03f8d6e9 | 3156 | echo "$NGINX_START |
9d725af6 | 3157 | location ~ \"^/\.well-known/acme-challenge/([-_a-zA-Z0-9]+)\$\" { |
3158 | default_type text/plain; | |
3159 | return 200 \"\$1.$_thumbpt\"; | |
3c07f57a | 3160 | } |
03f8d6e9 | 3161 | #NGINX_START |
3162 | " >>"$FOUND_REAL_NGINX_CONF" | |
9d725af6 | 3163 | |
03f8d6e9 | 3164 | if ! sed -n "${_lnn},99999p" "$_backup_conf" >>"$FOUND_REAL_NGINX_CONF"; then |
3165 | cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF" | |
9d725af6 | 3166 | _err "write nginx conf error, but don't worry, the file is restored." |
3167 | return 1 | |
3168 | fi | |
df711b0e | 3169 | _debug3 "Modified config:$(cat $FOUND_REAL_NGINX_CONF)" |
9d725af6 | 3170 | _info "nginx conf is done, let's check it again." |
3171 | if ! _exec "nginx -t" >/dev/null; then | |
3172 | _exec_err | |
3173 | _err "It seems that nginx conf was broken, let's restore." | |
302c41ed | 3174 | cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF" |
9d725af6 | 3175 | return 1 |
3176 | fi | |
3177 | ||
3178 | _info "Reload nginx" | |
3179 | if ! _exec "nginx -s reload" >/dev/null; then | |
3180 | _exec_err | |
3181 | _err "It seems that nginx reload error, let's restore." | |
302c41ed | 3182 | cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF" |
9d725af6 | 3183 | return 1 |
3184 | fi | |
3185 | ||
3186 | return 0 | |
3187 | } | |
3188 | ||
3189 | #d , conf | |
3190 | _checkConf() { | |
3191 | _d="$1" | |
3192 | _c_file="$2" | |
3193 | _debug "Start _checkConf from:$_c_file" | |
3194 | if [ ! -f "$2" ] && ! echo "$2" | grep '*$' >/dev/null && echo "$2" | grep '*' >/dev/null; then | |
3195 | _debug "wildcard" | |
3196 | for _w_f in $2; do | |
7f618e7e | 3197 | if [ -f "$_w_f" ] && _checkConf "$1" "$_w_f"; then |
9d725af6 | 3198 | return 0 |
3199 | fi | |
3200 | done | |
3201 | #not found | |
3202 | return 1 | |
3203 | elif [ -f "$2" ]; then | |
3204 | _debug "single" | |
3205 | if _isRealNginxConf "$1" "$2"; then | |
3206 | _debug "$2 is found." | |
3207 | FOUND_REAL_NGINX_CONF="$2" | |
3208 | return 0 | |
3209 | fi | |
5a44e63c | 3210 | if cat "$2" | tr "\t" " " | grep "^ *include *.*;" >/dev/null; then |
9d725af6 | 3211 | _debug "Try include files" |
5a44e63c | 3212 | for included in $(cat "$2" | tr "\t" " " | grep "^ *include *.*;" | sed "s/include //" | tr -d " ;"); do |
9d725af6 | 3213 | _debug "check included $included" |
e353f66e | 3214 | if ! _startswith "$included" "/" && _exists dirname; then |
40e8c5e2 | 3215 | _relpath="$(dirname "$2")" |
0a4ef171 | 3216 | _debug "_relpath" "$_relpath" |
2b5e2d47 | 3217 | included="$_relpath/$included" |
0a4ef171 | 3218 | fi |
9d725af6 | 3219 | if _checkConf "$1" "$included"; then |
3220 | return 0 | |
3221 | fi | |
3222 | done | |
3223 | fi | |
3224 | return 1 | |
3225 | else | |
3226 | _debug "$2 not found." | |
3227 | return 1 | |
3228 | fi | |
3229 | return 1 | |
3230 | } | |
3231 | ||
3232 | #d , conf | |
3233 | _isRealNginxConf() { | |
3234 | _debug "_isRealNginxConf $1 $2" | |
302c41ed | 3235 | if [ -f "$2" ]; then |
3f1a76d9 | 3236 | for _fln in $(tr "\t" ' ' <"$2" | grep -n "^ *server_name.* $1" | cut -d : -f 1); do |
302c41ed | 3237 | _debug _fln "$_fln" |
3238 | if [ "$_fln" ]; then | |
04a609b5 | 3239 | _start=$(tr "\t" ' ' <"$2" | _head_n "$_fln" | grep -n "^ *server *" | grep -v server_name | _tail_n 1) |
9f90618a | 3240 | _debug "_start" "$_start" |
3241 | _start_n=$(echo "$_start" | cut -d : -f 1) | |
3242 | _start_nn=$(_math $_start_n + 1) | |
3243 | _debug "_start_n" "$_start_n" | |
3244 | _debug "_start_nn" "$_start_nn" | |
3245 | ||
3246 | _left="$(sed -n "${_start_nn},99999p" "$2")" | |
3247 | _debug2 _left "$_left" | |
012dd698 | 3248 | _end="$(echo "$_left" | tr "\t" ' ' | grep -n "^ *server *" | grep -v server_name | _head_n 1)" |
3249 | _debug "_end" "$_end" | |
3250 | if [ "$_end" ]; then | |
9f90618a | 3251 | _end_n=$(echo "$_end" | cut -d : -f 1) |
3252 | _debug "_end_n" "$_end_n" | |
3253 | _seg_n=$(echo "$_left" | sed -n "1,${_end_n}p") | |
3254 | else | |
3255 | _seg_n="$_left" | |
3256 | fi | |
3257 | ||
3258 | _debug "_seg_n" "$_seg_n" | |
3259 | ||
04a609b5 | 3260 | _skip_ssl=1 |
d1067c60 | 3261 | for _listen_i in $(echo "$_seg_n" | tr "\t" ' ' | grep "^ *listen" | tr -d " "); do |
04a609b5 | 3262 | if [ "$_listen_i" ]; then |
9841063d | 3263 | if [ "$(echo "$_listen_i" | _egrep_o "listen.*ssl")" ]; then |
04a609b5 | 3264 | _debug2 "$_listen_i is ssl" |
3265 | else | |
3266 | _debug2 "$_listen_i is plain text" | |
3267 | _skip_ssl="" | |
c05eb0b1 | 3268 | break |
3269 | fi | |
04a609b5 | 3270 | fi |
3271 | done | |
3272 | ||
3273 | if [ "$_skip_ssl" = "1" ]; then | |
9f90618a | 3274 | _debug "ssl on, skip" |
241cfc43 | 3275 | else |
3276 | FOUND_REAL_NGINX_CONF_LN=$_fln | |
3277 | _debug3 "found FOUND_REAL_NGINX_CONF_LN" "$FOUND_REAL_NGINX_CONF_LN" | |
3278 | return 0 | |
450efea1 | 3279 | fi |
302c41ed | 3280 | fi |
3281 | done | |
9d725af6 | 3282 | fi |
302c41ed | 3283 | return 1 |
9d725af6 | 3284 | } |
3285 | ||
3286 | #restore all the nginx conf | |
3287 | _restoreNginx() { | |
5d943a35 | 3288 | if [ -z "$NGINX_RESTORE_VLIST" ]; then |
9d725af6 | 3289 | _debug "No need to restore nginx, skip." |
3290 | return | |
3291 | fi | |
3292 | _debug "_restoreNginx" | |
5d943a35 | 3293 | _debug "NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST" |
9d725af6 | 3294 | |
5d943a35 | 3295 | for ng_entry in $(echo "$NGINX_RESTORE_VLIST" | tr "$dvsep" ' '); do |
9d725af6 | 3296 | _debug "ng_entry" "$ng_entry" |
3297 | _nd=$(echo "$ng_entry" | cut -d "$sep" -f 1) | |
3298 | _ngconf=$(echo "$ng_entry" | cut -d "$sep" -f 2) | |
3299 | _ngbackupconf=$(echo "$ng_entry" | cut -d "$sep" -f 3) | |
3300 | _info "Restoring from $_ngbackupconf to $_ngconf" | |
302c41ed | 3301 | cat "$_ngbackupconf" >"$_ngconf" |
9d725af6 | 3302 | done |
3303 | ||
3304 | _info "Reload nginx" | |
3305 | if ! _exec "nginx -s reload" >/dev/null; then | |
3306 | _exec_err | |
3307 | _err "It seems that nginx reload error, please report bug." | |
3308 | return 1 | |
3309 | fi | |
3310 | return 0 | |
3311 | } | |
3312 | ||
5ef501c5 | 3313 | _clearup() { |
44edb2bd | 3314 | _stopserver "$serverproc" |
4c3b3608 | 3315 | serverproc="" |
3316 | _restoreApache | |
9d725af6 | 3317 | _restoreNginx |
800e3f45 | 3318 | _clearupdns |
4c2a3841 | 3319 | if [ -z "$DEBUG" ]; then |
e22bcf7c | 3320 | rm -f "$TLS_CONF" |
3321 | rm -f "$TLS_CERT" | |
3322 | rm -f "$TLS_KEY" | |
3323 | rm -f "$TLS_CSR" | |
3324 | fi | |
4c3b3608 | 3325 | } |
3326 | ||
800e3f45 | 3327 | _clearupdns() { |
3328 | _debug "_clearupdns" | |
b5ca9bba | 3329 | _debug "dns_entries" "$dns_entries" |
3330 | ||
3331 | if [ -z "$dns_entries" ]; then | |
65b22b49 | 3332 | _debug "skip dns." |
800e3f45 | 3333 | return |
3334 | fi | |
875625b1 | 3335 | _info "Removing DNS records." |
800e3f45 | 3336 | |
b5ca9bba | 3337 | for entry in $dns_entries; do |
3338 | d=$(_getfield "$entry" 1) | |
3339 | txtdomain=$(_getfield "$entry" 2) | |
3340 | aliasDomain=$(_getfield "$entry" 3) | |
9a733a57 | 3341 | _currentRoot=$(_getfield "$entry" 4) |
b5ca9bba | 3342 | txt=$(_getfield "$entry" 5) |
3343 | d_api=$(_getfield "$entry" 6) | |
3344 | _debug "d" "$d" | |
3345 | _debug "txtdomain" "$txtdomain" | |
3346 | _debug "aliasDomain" "$aliasDomain" | |
9a733a57 | 3347 | _debug "_currentRoot" "$_currentRoot" |
b5ca9bba | 3348 | _debug "txt" "$txt" |
3349 | _debug "d_api" "$d_api" | |
3350 | if [ "$d_api" = "$txt" ]; then | |
3351 | d_api="" | |
800e3f45 | 3352 | fi |
4c2a3841 | 3353 | |
4c2a3841 | 3354 | if [ -z "$d_api" ]; then |
800e3f45 | 3355 | _info "Not Found domain api file: $d_api" |
3356 | continue | |
3357 | fi | |
4c2a3841 | 3358 | |
b5ca9bba | 3359 | if [ "$aliasDomain" ]; then |
3360 | txtdomain="$aliasDomain" | |
3361 | fi | |
3362 | ||
800e3f45 | 3363 | ( |
d5ec5f80 | 3364 | if ! . "$d_api"; then |
800e3f45 | 3365 | _err "Load file $d_api error. Please check your api file and try again." |
3366 | return 1 | |
3367 | fi | |
4c2a3841 | 3368 | |
800e3f45 | 3369 | rmcommand="${_currentRoot}_rm" |
d5ec5f80 | 3370 | if ! _exists "$rmcommand"; then |
800e3f45 | 3371 | _err "It seems that your api file doesn't define $rmcommand" |
3372 | return 1 | |
3373 | fi | |
a180b95c | 3374 | _info "Removing txt: $txt for domain: $txtdomain" |
21f201e3 | 3375 | if ! $rmcommand "$txtdomain" "$txt"; then |
800e3f45 | 3376 | _err "Error removing txt for domain:$txtdomain" |
3377 | return 1 | |
3378 | fi | |
a180b95c | 3379 | _info "Removed: Success" |
800e3f45 | 3380 | ) |
4c2a3841 | 3381 | |
800e3f45 | 3382 | done |
3383 | } | |
3384 | ||
4c3b3608 | 3385 | # webroot removelevel tokenfile |
3386 | _clearupwebbroot() { | |
3387 | __webroot="$1" | |
4c2a3841 | 3388 | if [ -z "$__webroot" ]; then |
4c3b3608 | 3389 | _debug "no webroot specified, skip" |
3390 | return 0 | |
3391 | fi | |
4c2a3841 | 3392 | |
dcf9cb58 | 3393 | _rmpath="" |
4c2a3841 | 3394 | if [ "$2" = '1' ]; then |
dcf9cb58 | 3395 | _rmpath="$__webroot/.well-known" |
4c2a3841 | 3396 | elif [ "$2" = '2' ]; then |
dcf9cb58 | 3397 | _rmpath="$__webroot/.well-known/acme-challenge" |
4c2a3841 | 3398 | elif [ "$2" = '3' ]; then |
dcf9cb58 | 3399 | _rmpath="$__webroot/.well-known/acme-challenge/$3" |
4c3b3608 | 3400 | else |
cc179731 | 3401 | _debug "Skip for removelevel:$2" |
4c3b3608 | 3402 | fi |
4c2a3841 | 3403 | |
3404 | if [ "$_rmpath" ]; then | |
3405 | if [ "$DEBUG" ]; then | |
dcf9cb58 | 3406 | _debug "Debugging, skip removing: $_rmpath" |
3407 | else | |
3408 | rm -rf "$_rmpath" | |
3409 | fi | |
3410 | fi | |
4c2a3841 | 3411 | |
4c3b3608 | 3412 | return 0 |
3413 | ||
3414 | } | |
3415 | ||
b0070f03 | 3416 | _on_before_issue() { |
af1cc3b3 | 3417 | _chk_web_roots="$1" |
02140ce7 | 3418 | _chk_main_domain="$2" |
3419 | _chk_alt_domains="$3" | |
85e1f4ea | 3420 | _chk_pre_hook="$4" |
3421 | _chk_local_addr="$5" | |
30c2d84c | 3422 | _debug _on_before_issue |
38f1b4d2 | 3423 | _debug _chk_main_domain "$_chk_main_domain" |
3424 | _debug _chk_alt_domains "$_chk_alt_domains" | |
d0f7c309 | 3425 | #run pre hook |
85e1f4ea | 3426 | if [ "$_chk_pre_hook" ]; then |
3427 | _info "Run pre hook:'$_chk_pre_hook'" | |
d0f7c309 | 3428 | if ! ( |
e1643620 BS |
3429 | export Le_Domain="$_chk_main_domain" |
3430 | export Le_Alt="$_chk_alt_domains" | |
85e1f4ea | 3431 | cd "$DOMAIN_PATH" && eval "$_chk_pre_hook" |
d0f7c309 | 3432 | ); then |
3433 | _err "Error when run pre hook." | |
3434 | return 1 | |
3435 | fi | |
3436 | fi | |
3437 | ||
af1cc3b3 | 3438 | if _hasfield "$_chk_web_roots" "$NO_VALUE"; then |
3794b5cb | 3439 | if ! _exists "socat"; then |
3440 | _err "Please install socat tools first." | |
0463b5d6 | 3441 | return 1 |
3442 | fi | |
0463b5d6 | 3443 | fi |
3444 | ||
85e1f4ea | 3445 | _debug Le_LocalAddress "$_chk_local_addr" |
4c2a3841 | 3446 | |
0463b5d6 | 3447 | _index=1 |
3448 | _currentRoot="" | |
3449 | _addrIndex=1 | |
38f1b4d2 | 3450 | _w_index=1 |
931d19ee | 3451 | while true; do |
931d19ee | 3452 | d="$(echo "$_chk_main_domain,$_chk_alt_domains," | cut -d , -f "$_w_index")" |
3453 | _w_index="$(_math "$_w_index" + 1)" | |
3454 | _debug d "$d" | |
3455 | if [ -z "$d" ]; then | |
3456 | break | |
3457 | fi | |
d5ec5f80 | 3458 | _debug "Check for domain" "$d" |
af1cc3b3 | 3459 | _currentRoot="$(_getfield "$_chk_web_roots" $_index)" |
0463b5d6 | 3460 | _debug "_currentRoot" "$_currentRoot" |
3461 | _index=$(_math $_index + 1) | |
3462 | _checkport="" | |
4c2a3841 | 3463 | if [ "$_currentRoot" = "$NO_VALUE" ]; then |
0463b5d6 | 3464 | _info "Standalone mode." |
4c2a3841 | 3465 | if [ -z "$Le_HTTPPort" ]; then |
0463b5d6 | 3466 | Le_HTTPPort=80 |
e7f7e96d | 3467 | _cleardomainconf "Le_HTTPPort" |
0463b5d6 | 3468 | else |
4c2a3841 | 3469 | _savedomainconf "Le_HTTPPort" "$Le_HTTPPort" |
0463b5d6 | 3470 | fi |
3471 | _checkport="$Le_HTTPPort" | |
43ff787b | 3472 | elif [ "$_currentRoot" = "$W_ALPN" ]; then |
3473 | _info "Standalone alpn mode." | |
4c2a3841 | 3474 | if [ -z "$Le_TLSPort" ]; then |
0463b5d6 | 3475 | Le_TLSPort=443 |
3476 | else | |
4c2a3841 | 3477 | _savedomainconf "Le_TLSPort" "$Le_TLSPort" |
0463b5d6 | 3478 | fi |
3479 | _checkport="$Le_TLSPort" | |
3480 | fi | |
4c2a3841 | 3481 | |
3482 | if [ "$_checkport" ]; then | |
0463b5d6 | 3483 | _debug _checkport "$_checkport" |
85e1f4ea | 3484 | _checkaddr="$(_getfield "$_chk_local_addr" $_addrIndex)" |
0463b5d6 | 3485 | _debug _checkaddr "$_checkaddr" |
4c2a3841 | 3486 | |
0463b5d6 | 3487 | _addrIndex="$(_math $_addrIndex + 1)" |
4c2a3841 | 3488 | |
0463b5d6 | 3489 | _netprc="$(_ss "$_checkport" | grep "$_checkport")" |
3490 | netprc="$(echo "$_netprc" | grep "$_checkaddr")" | |
4c2a3841 | 3491 | if [ -z "$netprc" ]; then |
5cc1d952 | 3492 | netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS:$_checkport")" |
0463b5d6 | 3493 | fi |
4c2a3841 | 3494 | if [ "$netprc" ]; then |
0463b5d6 | 3495 | _err "$netprc" |
4c2a3841 | 3496 | _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)" |
0463b5d6 | 3497 | _err "Please stop it first" |
3498 | return 1 | |
3499 | fi | |
3500 | fi | |
3501 | done | |
3502 | ||
af1cc3b3 | 3503 | if _hasfield "$_chk_web_roots" "apache"; then |
4c2a3841 | 3504 | if ! _setApache; then |
0463b5d6 | 3505 | _err "set up apache error. Report error to me." |
3506 | return 1 | |
3507 | fi | |
3508 | else | |
3509 | usingApache="" | |
3510 | fi | |
3511 | ||
b0070f03 | 3512 | } |
3513 | ||
3514 | _on_issue_err() { | |
85e1f4ea | 3515 | _chk_post_hook="$1" |
58e4d337 | 3516 | _chk_vlist="$2" |
30c2d84c | 3517 | _debug _on_issue_err |
cd8fc359 | 3518 | |
4c2a3841 | 3519 | if [ "$LOG_FILE" ]; then |
a73c5b33 | 3520 | _err "Please check log file for more details: $LOG_FILE" |
3521 | else | |
54ae008d | 3522 | _err "Please add '--debug' or '--log' to check more details." |
a73c5b33 | 3523 | _err "See: $_DEBUG_WIKI" |
3524 | fi | |
4c2a3841 | 3525 | |
b0070f03 | 3526 | #run the post hook |
85e1f4ea | 3527 | if [ "$_chk_post_hook" ]; then |
3528 | _info "Run post hook:'$_chk_post_hook'" | |
b0070f03 | 3529 | if ! ( |
85e1f4ea | 3530 | cd "$DOMAIN_PATH" && eval "$_chk_post_hook" |
4c2a3841 | 3531 | ); then |
b0070f03 | 3532 | _err "Error when run post hook." |
3533 | return 1 | |
3534 | fi | |
3535 | fi | |
58e4d337 | 3536 | |
3537 | #trigger the validation to flush the pending authz | |
ea722da3 | 3538 | _debug2 "_chk_vlist" "$_chk_vlist" |
58e4d337 | 3539 | if [ "$_chk_vlist" ]; then |
3540 | ( | |
c719a61e | 3541 | _debug2 "start to deactivate authz" |
3542 | ventries=$(echo "$_chk_vlist" | tr "$dvsep" ' ') | |
3543 | for ventry in $ventries; do | |
3544 | d=$(echo "$ventry" | cut -d "$sep" -f 1) | |
3545 | keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2) | |
3546 | uri=$(echo "$ventry" | cut -d "$sep" -f 3) | |
3547 | vtype=$(echo "$ventry" | cut -d "$sep" -f 4) | |
3548 | _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5) | |
f94433e5 | 3549 | __trigger_validation "$uri" "$keyauthorization" |
c719a61e | 3550 | done |
58e4d337 | 3551 | ) |
3552 | fi | |
3553 | ||
bd04638d | 3554 | if [ "$_ACME_IS_RENEW" = "1" ] && _hasfield "$Le_Webroot" "$W_DNS"; then |
309bec47 | 3555 | _err "$_DNS_MANUAL_ERR" |
3556 | fi | |
3557 | ||
58e4d337 | 3558 | if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then |
3559 | _debug "$(_dlg_versions)" | |
3560 | fi | |
3561 | ||
b0070f03 | 3562 | } |
3563 | ||
3564 | _on_issue_success() { | |
85e1f4ea | 3565 | _chk_post_hook="$1" |
3566 | _chk_renew_hook="$2" | |
30c2d84c | 3567 | _debug _on_issue_success |
c6b68551 | 3568 | |
b0070f03 | 3569 | #run the post hook |
85e1f4ea | 3570 | if [ "$_chk_post_hook" ]; then |
3571 | _info "Run post hook:'$_chk_post_hook'" | |
b0070f03 | 3572 | if ! ( |
c7849a43 | 3573 | export CERT_PATH |
3574 | export CERT_KEY_PATH | |
3575 | export CA_CERT_PATH | |
3576 | export CERT_FULLCHAIN_PATH | |
3577 | export Le_Domain="$_main_domain" | |
85e1f4ea | 3578 | cd "$DOMAIN_PATH" && eval "$_chk_post_hook" |
4c2a3841 | 3579 | ); then |
b0070f03 | 3580 | _err "Error when run post hook." |
3581 | return 1 | |
3582 | fi | |
3583 | fi | |
4c2a3841 | 3584 | |
b0070f03 | 3585 | #run renew hook |
bd04638d | 3586 | if [ "$_ACME_IS_RENEW" ] && [ "$_chk_renew_hook" ]; then |
85e1f4ea | 3587 | _info "Run renew hook:'$_chk_renew_hook'" |
b0070f03 | 3588 | if ! ( |
c7849a43 | 3589 | export CERT_PATH |
3590 | export CERT_KEY_PATH | |
3591 | export CA_CERT_PATH | |
3592 | export CERT_FULLCHAIN_PATH | |
3593 | export Le_Domain="$_main_domain" | |
85e1f4ea | 3594 | cd "$DOMAIN_PATH" && eval "$_chk_renew_hook" |
4c2a3841 | 3595 | ); then |
b0070f03 | 3596 | _err "Error when run renew hook." |
3597 | return 1 | |
3598 | fi | |
4c2a3841 | 3599 | fi |
3600 | ||
867ec010 | 3601 | if _hasfield "$Le_Webroot" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then |
309bec47 | 3602 | _err "$_DNS_MANUAL_WARN" |
3603 | fi | |
3604 | ||
b0070f03 | 3605 | } |
3606 | ||
f96d91cb | 3607 | #account_key_length eab-kid eab-hmac-key |
eb59817e | 3608 | registeraccount() { |
f96d91cb | 3609 | _account_key_length="$1" |
3610 | _eab_id="$2" | |
3611 | _eab_hmac_key="$3" | |
eb59817e | 3612 | _initpath |
f96d91cb | 3613 | _regAccount "$_account_key_length" "$_eab_id" "$_eab_hmac_key" |
eb59817e | 3614 | } |
d404e92d | 3615 | |
8a29fbc8 | 3616 | __calcAccountKeyHash() { |
ca7202eb | 3617 | [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH" |
8a29fbc8 | 3618 | } |
3619 | ||
339a8ad6 | 3620 | __calc_account_thumbprint() { |
3621 | printf "%s" "$jwk" | tr -d ' ' | _digest "sha256" | _url_replace | |
3622 | } | |
3623 | ||
389518e1 | 3624 | _getAccountEmail() { |
3625 | if [ "$ACCOUNT_EMAIL" ]; then | |
3626 | echo "$ACCOUNT_EMAIL" | |
3627 | return 0 | |
3628 | fi | |
3629 | if [ -z "$CA_EMAIL" ]; then | |
3630 | CA_EMAIL="$(_readcaconf CA_EMAIL)" | |
3631 | fi | |
3632 | if [ "$CA_EMAIL" ]; then | |
3633 | echo "$CA_EMAIL" | |
3634 | return 0 | |
3635 | fi | |
3636 | _readaccountconf "ACCOUNT_EMAIL" | |
3637 | } | |
3638 | ||
57e58ce7 | 3639 | #keylength |
d404e92d | 3640 | _regAccount() { |
3641 | _initpath | |
57e58ce7 | 3642 | _reg_length="$1" |
f96d91cb | 3643 | _eab_id="$2" |
3644 | _eab_hmac_key="$3" | |
f87890cb | 3645 | _debug3 _regAccount "$_regAccount" |
c1151b0d | 3646 | _initAPI |
3647 | ||
1bbc33a0 | 3648 | mkdir -p "$CA_DIR" |
4c2a3841 | 3649 | |
3650 | if [ ! -f "$ACCOUNT_KEY_PATH" ]; then | |
3651 | if ! _create_account_key "$_reg_length"; then | |
d404e92d | 3652 | _err "Create account key error." |
3653 | return 1 | |
3654 | fi | |
3655 | fi | |
4c2a3841 | 3656 | |
3657 | if ! _calcjwk "$ACCOUNT_KEY_PATH"; then | |
d404e92d | 3658 | return 1 |
3659 | fi | |
f96d91cb | 3660 | if [ "$_eab_id" ] && [ "$_eab_hmac_key" ]; then |
3661 | _savecaconf CA_EAB_KEY_ID "$_eab_id" | |
3662 | _savecaconf CA_EAB_HMAC_KEY "$_eab_hmac_key" | |
3663 | fi | |
3664 | _eab_id=$(_readcaconf "CA_EAB_KEY_ID") | |
3665 | _eab_hmac_key=$(_readcaconf "CA_EAB_HMAC_KEY") | |
3666 | _secure_debug3 _eab_id "$_eab_id" | |
3667 | _secure_debug3 _eab_hmac_key "$_eab_hmac_key" | |
389518e1 | 3668 | _email="$(_getAccountEmail)" |
3669 | if [ "$_email" ]; then | |
3670 | _savecaconf "CA_EMAIL" "$_email" | |
3671 | fi | |
af3ea2d4 | 3672 | |
3673 | if [ "$ACME_DIRECTORY" = "$CA_ZEROSSL" ]; then | |
3674 | if [ -z "$_eab_id" ] || [ -z "$_eab_hmac_key" ]; then | |
3675 | _info "No EAB credentials found for ZeroSSL, let's get one" | |
3676 | if [ -z "$_email" ]; then | |
d0b51489 | 3677 | _info "$(__green "$PROJECT_NAME is using ZeroSSL as default CA now.")" |
3678 | _info "$(__green "Please update your account with an email address first.")" | |
3679 | _info "$(__green "$PROJECT_ENTRY --register-account -m my@example.com")" | |
3680 | _info "See: $(__green "$_ZEROSSL_WIKI")" | |
af3ea2d4 | 3681 | return 1 |
3682 | fi | |
3683 | _eabresp=$(_post "email=$_email" $_ZERO_EAB_ENDPOINT) | |
3684 | if [ "$?" != "0" ]; then | |
aa59c46c | 3685 | _debug2 "$_eabresp" |
af3ea2d4 | 3686 | _err "Can not get EAB credentials from ZeroSSL." |
3687 | return 1 | |
389518e1 | 3688 | fi |
8ae08b29 | 3689 | _secure_debug2 _eabresp "$_eabresp" |
e0def669 | 3690 | _eab_id="$(echo "$_eabresp" | tr ',}' '\n\n' | grep '"eab_kid"' | cut -d : -f 2 | tr -d '"')" |
d0b51489 | 3691 | _secure_debug2 _eab_id "$_eab_id" |
af3ea2d4 | 3692 | if [ -z "$_eab_id" ]; then |
3693 | _err "Can not resolve _eab_id" | |
3694 | return 1 | |
3695 | fi | |
8ae08b29 | 3696 | _eab_hmac_key="$(echo "$_eabresp" | tr ',}' '\n\n' | grep '"eab_hmac_key"' | cut -d : -f 2 | tr -d '"')" |
d0b51489 | 3697 | _secure_debug2 _eab_hmac_key "$_eab_hmac_key" |
af3ea2d4 | 3698 | if [ -z "$_eab_hmac_key" ]; then |
3699 | _err "Can not resolve _eab_hmac_key" | |
3700 | return 1 | |
3701 | fi | |
3702 | _savecaconf CA_EAB_KEY_ID "$_eab_id" | |
3703 | _savecaconf CA_EAB_HMAC_KEY "$_eab_hmac_key" | |
389518e1 | 3704 | fi |
af3ea2d4 | 3705 | fi |
3706 | if [ "$_eab_id" ] && [ "$_eab_hmac_key" ]; then | |
3707 | eab_protected="{\"alg\":\"HS256\",\"kid\":\"$_eab_id\",\"url\":\"${ACME_NEW_ACCOUNT}\"}" | |
3708 | _debug3 eab_protected "$eab_protected" | |
f96d91cb | 3709 | |
af3ea2d4 | 3710 | eab_protected64=$(printf "%s" "$eab_protected" | _base64 | _url_replace) |
3711 | _debug3 eab_protected64 "$eab_protected64" | |
f96d91cb | 3712 | |
af3ea2d4 | 3713 | eab_payload64=$(printf "%s" "$jwk" | _base64 | _url_replace) |
3714 | _debug3 eab_payload64 "$eab_payload64" | |
f96d91cb | 3715 | |
af3ea2d4 | 3716 | eab_sign_t="$eab_protected64.$eab_payload64" |
3717 | _debug3 eab_sign_t "$eab_sign_t" | |
f96d91cb | 3718 | |
d0b51489 | 3719 | key_hex="$(_durl_replace_base64 "$_eab_hmac_key" | _dbase64 multi | _hex_dump | tr -d ' ')" |
af3ea2d4 | 3720 | _debug3 key_hex "$key_hex" |
f96d91cb | 3721 | |
af3ea2d4 | 3722 | eab_signature=$(printf "%s" "$eab_sign_t" | _hmac sha256 $key_hex | _base64 | _url_replace) |
3723 | _debug3 eab_signature "$eab_signature" | |
f96d91cb | 3724 | |
af3ea2d4 | 3725 | externalBinding=",\"externalAccountBinding\":{\"protected\":\"$eab_protected64\", \"payload\":\"$eab_payload64\", \"signature\":\"$eab_signature\"}" |
3726 | _debug3 externalBinding "$externalBinding" | |
f87890cb | 3727 | fi |
af3ea2d4 | 3728 | if [ "$_email" ]; then |
3729 | email_sg="\"contact\": [\"mailto:$_email\"], " | |
3730 | fi | |
3731 | regjson="{$email_sg\"termsOfServiceAgreed\": true$externalBinding}" | |
4c2a3841 | 3732 | |
389518e1 | 3733 | _info "Registering account: $ACME_DIRECTORY" |
d404e92d | 3734 | |
f87890cb | 3735 | if ! _send_signed_request "${ACME_NEW_ACCOUNT}" "$regjson"; then |
3736 | _err "Register account Error: $response" | |
3737 | return 1 | |
3738 | fi | |
d404e92d | 3739 | |
389518e1 | 3740 | _eabAlreadyBound="" |
f87890cb | 3741 | if [ "$code" = "" ] || [ "$code" = '201' ]; then |
3742 | echo "$response" >"$ACCOUNT_JSON_PATH" | |
3743 | _info "Registered" | |
7df20e50 | 3744 | elif [ "$code" = '409' ] || [ "$code" = '200' ]; then |
f87890cb | 3745 | _info "Already registered" |
389518e1 | 3746 | elif [ "$code" = '400' ] && _contains "$response" 'The account is not awaiting external account binding'; then |
3747 | _info "Already register EAB." | |
3748 | _eabAlreadyBound=1 | |
f87890cb | 3749 | else |
3750 | _err "Register account Error: $response" | |
3751 | return 1 | |
3752 | fi | |
d404e92d | 3753 | |
389518e1 | 3754 | if [ -z "$_eabAlreadyBound" ]; then |
3755 | _debug2 responseHeaders "$responseHeaders" | |
3756 | _accUri="$(echo "$responseHeaders" | grep -i "^Location:" | _head_n 1 | cut -d ':' -f 2- | tr -d "\r\n ")" | |
3757 | _debug "_accUri" "$_accUri" | |
3758 | if [ -z "$_accUri" ]; then | |
3759 | _err "Can not find account id url." | |
3760 | _err "$responseHeaders" | |
3761 | return 1 | |
3762 | fi | |
3763 | _savecaconf "ACCOUNT_URL" "$_accUri" | |
3764 | else | |
3765 | ACCOUNT_URL="$(_readcaconf ACCOUNT_URL)" | |
7e0b334b | 3766 | fi |
7e0b334b | 3767 | export ACCOUNT_URL="$_accUri" |
d404e92d | 3768 | |
f87890cb | 3769 | CA_KEY_HASH="$(__calcAccountKeyHash)" |
3770 | _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH" | |
3771 | _savecaconf CA_KEY_HASH "$CA_KEY_HASH" | |
d404e92d | 3772 | |
f87890cb | 3773 | if [ "$code" = '403' ]; then |
3774 | _err "It seems that the account key is already deactivated, please use a new account key." | |
3775 | return 1 | |
3776 | fi | |
4c2a3841 | 3777 | |
f87890cb | 3778 | ACCOUNT_THUMBPRINT="$(__calc_account_thumbprint)" |
3779 | _info "ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT" | |
d404e92d | 3780 | } |
3781 | ||
79e2f8a2 | 3782 | #implement updateaccount |
3783 | updateaccount() { | |
3784 | _initpath | |
3785 | ||
79e2f8a2 | 3786 | if [ ! -f "$ACCOUNT_KEY_PATH" ]; then |
3787 | _err "Account key is not found at: $ACCOUNT_KEY_PATH" | |
3788 | return 1 | |
3789 | fi | |
3790 | ||
3791 | _accUri=$(_readcaconf "ACCOUNT_URL") | |
3792 | _debug _accUri "$_accUri" | |
3793 | ||
3794 | if [ -z "$_accUri" ]; then | |
3795 | _err "The account url is empty, please run '--update-account' first to update the account info first," | |
3796 | _err "Then try again." | |
3797 | return 1 | |
3798 | fi | |
3799 | ||
3800 | if ! _calcjwk "$ACCOUNT_KEY_PATH"; then | |
3801 | return 1 | |
3802 | fi | |
3803 | _initAPI | |
3804 | ||
389518e1 | 3805 | _email="$(_getAccountEmail)" |
af3ea2d4 | 3806 | |
d53262fa | 3807 | if [ "$_email" ]; then |
af3ea2d4 | 3808 | updjson='{"contact": ["mailto:'$_email'"]}' |
79e2f8a2 | 3809 | else |
af3ea2d4 | 3810 | updjson='{"contact": []}' |
79e2f8a2 | 3811 | fi |
3812 | ||
79e2f8a2 | 3813 | _send_signed_request "$_accUri" "$updjson" |
3814 | ||
3815 | if [ "$code" = '200' ]; then | |
72e1a1b2 | 3816 | echo "$response" >"$ACCOUNT_JSON_PATH" |
de4c4eed | 3817 | _info "Account update success for $_accUri." |
79e2f8a2 | 3818 | else |
3819 | _info "Error. The account was not updated." | |
3820 | return 1 | |
3821 | fi | |
3822 | } | |
3823 | ||
422dd1fa | 3824 | #Implement deactivate account |
3825 | deactivateaccount() { | |
3826 | _initpath | |
3827 | ||
422dd1fa | 3828 | if [ ! -f "$ACCOUNT_KEY_PATH" ]; then |
3829 | _err "Account key is not found at: $ACCOUNT_KEY_PATH" | |
3830 | return 1 | |
3831 | fi | |
3832 | ||
3833 | _accUri=$(_readcaconf "ACCOUNT_URL") | |
3834 | _debug _accUri "$_accUri" | |
3835 | ||
3836 | if [ -z "$_accUri" ]; then | |
3837 | _err "The account url is empty, please run '--update-account' first to update the account info first," | |
3838 | _err "Then try again." | |
3839 | return 1 | |
3840 | fi | |
3841 | ||
3842 | if ! _calcjwk "$ACCOUNT_KEY_PATH"; then | |
3843 | return 1 | |
3844 | fi | |
3845 | _initAPI | |
3846 | ||
af3ea2d4 | 3847 | _djson="{\"status\":\"deactivated\"}" |
3848 | ||
d2cde379 | 3849 | if _send_signed_request "$_accUri" "$_djson" && _contains "$response" '"deactivated"'; then |
422dd1fa | 3850 | _info "Deactivate account success for $_accUri." |
3851 | _accid=$(echo "$response" | _egrep_o "\"id\" *: *[^,]*," | cut -d : -f 2 | tr -d ' ,') | |
3852 | elif [ "$code" = "403" ]; then | |
3853 | _info "The account is already deactivated." | |
3854 | _accid=$(_getfield "$_accUri" "999" "/") | |
3855 | else | |
3856 | _err "Deactivate: account failed for $_accUri." | |
3857 | return 1 | |
3858 | fi | |
3859 | ||
3860 | _debug "Account id: $_accid" | |
3861 | if [ "$_accid" ]; then | |
3862 | _deactivated_account_path="$CA_DIR/deactivated/$_accid" | |
3863 | _debug _deactivated_account_path "$_deactivated_account_path" | |
3864 | if mkdir -p "$_deactivated_account_path"; then | |
3865 | _info "Moving deactivated account info to $_deactivated_account_path/" | |
3866 | mv "$CA_CONF" "$_deactivated_account_path/" | |
3867 | mv "$ACCOUNT_JSON_PATH" "$_deactivated_account_path/" | |
3868 | mv "$ACCOUNT_KEY_PATH" "$_deactivated_account_path/" | |
3869 | else | |
3870 | _err "Can not create dir: $_deactivated_account_path, try to remove the deactivated account key." | |
3871 | rm -f "$CA_CONF" | |
3872 | rm -f "$ACCOUNT_JSON_PATH" | |
3873 | rm -f "$ACCOUNT_KEY_PATH" | |
3874 | fi | |
3875 | fi | |
3876 | } | |
3877 | ||
a61fe418 | 3878 | # domain folder file |
3879 | _findHook() { | |
3880 | _hookdomain="$1" | |
3881 | _hookcat="$2" | |
3882 | _hookname="$3" | |
3883 | ||
c7b16249 | 3884 | if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then |
3885 | d_api="$_SCRIPT_HOME/$_hookcat/$_hookname" | |
3886 | elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then | |
3887 | d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh" | |
b50e701c | 3888 | elif [ "$_hookdomain" ] && [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then |
a61fe418 | 3889 | d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname" |
b50e701c | 3890 | elif [ "$_hookdomain" ] && [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then |
a61fe418 | 3891 | d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" |
4c2a3841 | 3892 | elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then |
a61fe418 | 3893 | d_api="$LE_WORKING_DIR/$_hookname" |
4c2a3841 | 3894 | elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then |
a61fe418 | 3895 | d_api="$LE_WORKING_DIR/$_hookname.sh" |
4c2a3841 | 3896 | elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then |
a61fe418 | 3897 | d_api="$LE_WORKING_DIR/$_hookcat/$_hookname" |
4c2a3841 | 3898 | elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then |
a61fe418 | 3899 | d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh" |
3900 | fi | |
3901 | ||
3902 | printf "%s" "$d_api" | |
3903 | } | |
3904 | ||
f940b2a5 | 3905 | #domain |
3906 | __get_domain_new_authz() { | |
3907 | _gdnd="$1" | |
3908 | _info "Getting new-authz for domain" "$_gdnd" | |
40ef86f4 | 3909 | _initAPI |
f940b2a5 | 3910 | _Max_new_authz_retry_times=5 |
3911 | _authz_i=0 | |
4c2a3841 | 3912 | while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do |
efd96153 | 3913 | _debug "Try new-authz for the $_authz_i time." |
48d9a8c1 | 3914 | if ! _send_signed_request "${ACME_NEW_AUTHZ}" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then |
f940b2a5 | 3915 | _err "Can not get domain new authz." |
3916 | return 1 | |
3917 | fi | |
5413bf87 | 3918 | if _contains "$response" "No registration exists matching provided key"; then |
3919 | _err "It seems there is an error, but it's recovered now, please try again." | |
3920 | _err "If you see this message for a second time, please report bug: $(__green "$PROJECT")" | |
3921 | _clearcaconf "CA_KEY_HASH" | |
3922 | break | |
3923 | fi | |
4c2a3841 | 3924 | if ! _contains "$response" "An error occurred while processing your request"; then |
f940b2a5 | 3925 | _info "The new-authz request is ok." |
3926 | break | |
3927 | fi | |
3928 | _authz_i="$(_math "$_authz_i" + 1)" | |
9e45ac93 | 3929 | _info "The server is busy, Sleep $_authz_i to retry." |
f940b2a5 | 3930 | _sleep "$_authz_i" |
4c2a3841 | 3931 | done |
f940b2a5 | 3932 | |
4c2a3841 | 3933 | if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then |
efd96153 | 3934 | _err "new-authz retry reach the max $_Max_new_authz_retry_times times." |
f940b2a5 | 3935 | fi |
4c2a3841 | 3936 | |
78915896 | 3937 | if [ "$code" ] && [ "$code" != '201' ]; then |
f940b2a5 | 3938 | _err "new-authz error: $response" |
3939 | return 1 | |
3940 | fi | |
3941 | ||
3942 | } | |
3943 | ||
58e4d337 | 3944 | #uri keyAuthorization |
f94433e5 | 3945 | __trigger_validation() { |
8bd12ed0 | 3946 | _debug2 "Trigger domain validation." |
58e4d337 | 3947 | _t_url="$1" |
3948 | _debug2 _t_url "$_t_url" | |
3949 | _t_key_authz="$2" | |
3950 | _debug2 _t_key_authz "$_t_key_authz" | |
920cab6f K |
3951 | _t_vtype="$3" |
3952 | _debug2 _t_vtype "$_t_vtype" | |
af3ea2d4 | 3953 | |
3954 | _send_signed_request "$_t_url" "{}" | |
3955 | ||
58e4d337 | 3956 | } |
3957 | ||
b5ca9bba | 3958 | #endpoint domain type |
10eec7d4 | 3959 | _ns_lookup_impl() { |
b5ca9bba | 3960 | _ns_ep="$1" |
3961 | _ns_domain="$2" | |
3962 | _ns_type="$3" | |
3963 | _debug2 "_ns_ep" "$_ns_ep" | |
3964 | _debug2 "_ns_domain" "$_ns_domain" | |
3965 | _debug2 "_ns_type" "$_ns_type" | |
3966 | ||
3967 | response="$(_H1="accept: application/dns-json" _get "$_ns_ep?name=$_ns_domain&type=$_ns_type")" | |
3968 | _ret=$? | |
3969 | _debug2 "response" "$response" | |
3970 | if [ "$_ret" != "0" ]; then | |
3971 | return $_ret | |
3972 | fi | |
3973 | _answers="$(echo "$response" | tr '{}' '<>' | _egrep_o '"Answer":\[[^]]*]' | tr '<>' '\n\n')" | |
3974 | _debug2 "_answers" "$_answers" | |
3975 | echo "$_answers" | |
3976 | } | |
3977 | ||
3978 | #domain, type | |
3979 | _ns_lookup_cf() { | |
3980 | _cf_ld="$1" | |
3981 | _cf_ld_type="$2" | |
3982 | _cf_ep="https://cloudflare-dns.com/dns-query" | |
10eec7d4 | 3983 | _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type" |
b5ca9bba | 3984 | } |
3985 | ||
3986 | #domain, type | |
3987 | _ns_purge_cf() { | |
3988 | _cf_d="$1" | |
3989 | _cf_d_type="$2" | |
3990 | _debug "Cloudflare purge $_cf_d_type record for domain $_cf_d" | |
b9b2cd27 | 3991 | _cf_purl="https://cloudflare-dns.com/api/v1/purge?domain=$_cf_d&type=$_cf_d_type" |
b5ca9bba | 3992 | response="$(_post "" "$_cf_purl")" |
3993 | _debug2 response "$response" | |
3994 | } | |
3995 | ||
10eec7d4 | 3996 | #checks if cf server is available |
3997 | _ns_is_available_cf() { | |
e65144a1 | 3998 | if _get "https://cloudflare-dns.com" "" 1 >/dev/null 2>&1; then |
3999 | return 0 | |
4000 | else | |
4001 | return 1 | |
4002 | fi | |
4003 | } | |
4004 | ||
4005 | _ns_is_available_google() { | |
4006 | if _get "https://dns.google" "" 1 >/dev/null 2>&1; then | |
10eec7d4 | 4007 | return 0 |
4008 | else | |
4009 | return 1 | |
4010 | fi | |
4011 | } | |
4012 | ||
4013 | #domain, type | |
4014 | _ns_lookup_google() { | |
4015 | _cf_ld="$1" | |
4016 | _cf_ld_type="$2" | |
4017 | _cf_ep="https://dns.google/resolve" | |
4018 | _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type" | |
4019 | } | |
4020 | ||
e65144a1 | 4021 | _ns_is_available_ali() { |
4022 | if _get "https://dns.alidns.com" "" 1 >/dev/null 2>&1; then | |
4023 | return 0 | |
4024 | else | |
4025 | return 1 | |
4026 | fi | |
4027 | } | |
4028 | ||
4029 | #domain, type | |
4030 | _ns_lookup_ali() { | |
4031 | _cf_ld="$1" | |
4032 | _cf_ld_type="$2" | |
4033 | _cf_ep="https://dns.alidns.com/resolve" | |
4034 | _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type" | |
4035 | } | |
4036 | ||
4037 | _ns_is_available_dp() { | |
a0c5d175 | 4038 | if _get "https://doh.pub" "" 1 >/dev/null 2>&1; then |
e65144a1 | 4039 | return 0 |
4040 | else | |
4041 | return 1 | |
4042 | fi | |
4043 | } | |
4044 | ||
4045 | #dnspod | |
4046 | _ns_lookup_dp() { | |
4047 | _cf_ld="$1" | |
4048 | _cf_ld_type="$2" | |
4049 | _cf_ep="https://doh.pub/dns-query" | |
4050 | _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type" | |
4051 | } | |
4052 | ||
10eec7d4 | 4053 | #domain, type |
4054 | _ns_lookup() { | |
4055 | if [ -z "$DOH_USE" ]; then | |
4056 | _debug "Detect dns server first." | |
4057 | if _ns_is_available_cf; then | |
4058 | _debug "Use cloudflare doh server" | |
4059 | export DOH_USE=$DOH_CLOUDFLARE | |
e65144a1 | 4060 | elif _ns_is_available_google; then |
10eec7d4 | 4061 | _debug "Use google doh server" |
4062 | export DOH_USE=$DOH_GOOGLE | |
e65144a1 | 4063 | elif _ns_is_available_ali; then |
4064 | _debug "Use aliyun doh server" | |
4065 | export DOH_USE=$DOH_ALI | |
290beb90 | 4066 | elif _ns_is_available_dp; then |
e65144a1 | 4067 | _debug "Use dns pod doh server" |
4068 | export DOH_USE=$DOH_DP | |
e0c32ce7 | 4069 | else |
4070 | _err "No doh" | |
10eec7d4 | 4071 | fi |
4072 | fi | |
4073 | ||
4074 | if [ "$DOH_USE" = "$DOH_CLOUDFLARE" ] || [ -z "$DOH_USE" ]; then | |
4075 | _ns_lookup_cf "$@" | |
e65144a1 | 4076 | elif [ "$DOH_USE" = "$DOH_GOOGLE" ]; then |
10eec7d4 | 4077 | _ns_lookup_google "$@" |
e65144a1 | 4078 | elif [ "$DOH_USE" = "$DOH_ALI" ]; then |
4079 | _ns_lookup_ali "$@" | |
4080 | elif [ "$DOH_USE" = "$DOH_DP" ]; then | |
4081 | _ns_lookup_dp "$@" | |
4082 | else | |
4083 | _err "Unknown doh provider: DOH_USE=$DOH_USE" | |
10eec7d4 | 4084 | fi |
4085 | ||
4086 | } | |
4087 | ||
b5ca9bba | 4088 | #txtdomain, alias, txt |
4089 | __check_txt() { | |
4090 | _c_txtdomain="$1" | |
4091 | _c_aliasdomain="$2" | |
4092 | _c_txt="$3" | |
4093 | _debug "_c_txtdomain" "$_c_txtdomain" | |
4094 | _debug "_c_aliasdomain" "$_c_aliasdomain" | |
4095 | _debug "_c_txt" "$_c_txt" | |
10eec7d4 | 4096 | _answers="$(_ns_lookup "$_c_aliasdomain" TXT)" |
b5ca9bba | 4097 | _contains "$_answers" "$_c_txt" |
4098 | ||
4099 | } | |
4100 | ||
4101 | #txtdomain | |
4102 | __purge_txt() { | |
4103 | _p_txtdomain="$1" | |
4104 | _debug _p_txtdomain "$_p_txtdomain" | |
10eec7d4 | 4105 | if [ "$DOH_USE" = "$DOH_CLOUDFLARE" ] || [ -z "$DOH_USE" ]; then |
4106 | _ns_purge_cf "$_p_txtdomain" "TXT" | |
4107 | else | |
e65144a1 | 4108 | _debug "no purge api for this doh api, just sleep 5 secs" |
10eec7d4 | 4109 | _sleep 5 |
4110 | fi | |
4111 | ||
b5ca9bba | 4112 | } |
4113 | ||
4114 | #wait and check each dns entries | |
4115 | _check_dns_entries() { | |
4116 | _success_txt="," | |
4117 | _end_time="$(_time)" | |
4118 | _end_time="$(_math "$_end_time" + 1200)" #let's check no more than 20 minutes. | |
4119 | ||
4120 | while [ "$(_time)" -le "$_end_time" ]; do | |
8a24275b | 4121 | _info "You can use '--dnssleep' to disable public dns checks." |
4122 | _info "See: $_DNSCHECK_WIKI" | |
b5ca9bba | 4123 | _left="" |
4124 | for entry in $dns_entries; do | |
4125 | d=$(_getfield "$entry" 1) | |
4126 | txtdomain=$(_getfield "$entry" 2) | |
0093dc3d | 4127 | txtdomain=$(_idn "$txtdomain") |
b5ca9bba | 4128 | aliasDomain=$(_getfield "$entry" 3) |
0093dc3d | 4129 | aliasDomain=$(_idn "$aliasDomain") |
b5ca9bba | 4130 | txt=$(_getfield "$entry" 5) |
4131 | d_api=$(_getfield "$entry" 6) | |
4132 | _debug "d" "$d" | |
4133 | _debug "txtdomain" "$txtdomain" | |
4134 | _debug "aliasDomain" "$aliasDomain" | |
4135 | _debug "txt" "$txt" | |
4136 | _debug "d_api" "$d_api" | |
4137 | _info "Checking $d for $aliasDomain" | |
4138 | if _contains "$_success_txt" ",$txt,"; then | |
4139 | _info "Already success, continue next one." | |
4140 | continue | |
4141 | fi | |
4142 | ||
4143 | if __check_txt "$txtdomain" "$aliasDomain" "$txt"; then | |
4144 | _info "Domain $d '$aliasDomain' success." | |
4145 | _success_txt="$_success_txt,$txt," | |
4146 | continue | |
4147 | fi | |
4148 | _left=1 | |
4149 | _info "Not valid yet, let's wait 10 seconds and check next one." | |
b5ca9bba | 4150 | __purge_txt "$txtdomain" |
4151 | if [ "$txtdomain" != "$aliasDomain" ]; then | |
4152 | __purge_txt "$aliasDomain" | |
4153 | fi | |
b9b2cd27 | 4154 | _sleep 10 |
b5ca9bba | 4155 | done |
4156 | if [ "$_left" ]; then | |
4157 | _info "Let's wait 10 seconds and check again". | |
4158 | _sleep 10 | |
4159 | else | |
4160 | _info "All success, let's return" | |
a44ea0dd | 4161 | return 0 |
b5ca9bba | 4162 | fi |
4163 | done | |
a44ea0dd | 4164 | _info "Timed out waiting for DNS." |
4165 | return 1 | |
b5ca9bba | 4166 | |
4167 | } | |
4168 | ||
e3ebd582 | 4169 | #file |
12b19165 | 4170 | _get_chain_issuers() { |
e3ebd582 | 4171 | _cfile="$1" |
987571ce | 4172 | if _contains "$(${ACME_OPENSSL_BIN:-openssl} help crl2pkcs7 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -help 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 help 2>&1)" "unknown option help"; then |
12b19165 | 4173 | ${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -nocrl -certfile $_cfile | ${ACME_OPENSSL_BIN:-openssl} pkcs7 -print_certs -text -noout | grep -i 'Issuer:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2 |
d5d38b33 | 4174 | else |
12b19165 | 4175 | _cindex=1 |
4176 | for _startn in $(grep -n -- "$BEGIN_CERT" "$_cfile" | cut -d : -f 1); do | |
4177 | _endn="$(grep -n -- "$END_CERT" "$_cfile" | cut -d : -f 1 | _head_n $_cindex | _tail_n 1)" | |
4178 | _debug2 "_startn" "$_startn" | |
4179 | _debug2 "_endn" "$_endn" | |
4180 | if [ "$DEBUG" ]; then | |
4181 | _debug2 "cert$_cindex" "$(sed -n "$_startn,${_endn}p" "$_cfile")" | |
4182 | fi | |
4183 | sed -n "$_startn,${_endn}p" "$_cfile" | ${ACME_OPENSSL_BIN:-openssl} x509 -text -noout | grep 'Issuer:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2 | sed "s/ *\(.*\)/\1/" | |
4184 | _cindex=$(_math $_cindex + 1) | |
4185 | done | |
4186 | fi | |
4187 | } | |
4188 | ||
4189 | # | |
4190 | _get_chain_subjects() { | |
4191 | _cfile="$1" | |
4192 | if _contains "$(${ACME_OPENSSL_BIN:-openssl} help crl2pkcs7 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -help 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 help 2>&1)" "unknown option help"; then | |
4193 | ${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -nocrl -certfile $_cfile | ${ACME_OPENSSL_BIN:-openssl} pkcs7 -print_certs -text -noout | grep -i 'Subject:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2 | |
4194 | else | |
4195 | _cindex=1 | |
4196 | for _startn in $(grep -n -- "$BEGIN_CERT" "$_cfile" | cut -d : -f 1); do | |
4197 | _endn="$(grep -n -- "$END_CERT" "$_cfile" | cut -d : -f 1 | _head_n $_cindex | _tail_n 1)" | |
4198 | _debug2 "_startn" "$_startn" | |
4199 | _debug2 "_endn" "$_endn" | |
4200 | if [ "$DEBUG" ]; then | |
4201 | _debug2 "cert$_cindex" "$(sed -n "$_startn,${_endn}p" "$_cfile")" | |
4202 | fi | |
4203 | sed -n "$_startn,${_endn}p" "$_cfile" | ${ACME_OPENSSL_BIN:-openssl} x509 -text -noout | grep -i 'Subject:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2 | sed "s/ *\(.*\)/\1/" | |
4204 | _cindex=$(_math $_cindex + 1) | |
4205 | done | |
d5d38b33 | 4206 | fi |
e3ebd582 | 4207 | } |
4208 | ||
4209 | #cert issuer | |
4210 | _match_issuer() { | |
4211 | _cfile="$1" | |
4212 | _missuer="$2" | |
12b19165 | 4213 | _fissuers="$(_get_chain_issuers $_cfile)" |
d5d38b33 | 4214 | _debug2 _fissuers "$_fissuers" |
12b19165 | 4215 | _rootissuer="$(echo "$_fissuers" | _lower_case | _tail_n 1)" |
4216 | _debug2 _rootissuer "$_rootissuer" | |
fdb96e91 | 4217 | _missuer="$(echo "$_missuer" | _lower_case)" |
12b19165 | 4218 | _contains "$_rootissuer" "$_missuer" |
e3ebd582 | 4219 | } |
4220 | ||
6ae8d101 | 4221 | #ip |
4222 | _isIPv4() { | |
4223 | for seg in $(echo "$1" | tr '.' ' '); do | |
4f386663 | 4224 | _debug2 seg "$seg" |
4225 | if [ "$(echo "$seg" | tr -d [0-9])" ]; then | |
4226 | #not all number | |
4227 | return 1 | |
4228 | fi | |
4229 | if [ $seg -ge 0 ] && [ $seg -lt 256 ]; then | |
6ae8d101 | 4230 | continue |
4231 | fi | |
4232 | return 1 | |
4233 | done | |
4234 | return 0 | |
4235 | } | |
4236 | ||
4237 | #ip6 | |
4238 | _isIPv6() { | |
4239 | _contains "$1" ":" | |
4240 | } | |
4241 | ||
4242 | #ip | |
4243 | _isIP() { | |
4244 | _isIPv4 "$1" || _isIPv6 "$1" | |
4245 | } | |
4246 | ||
4247 | #identifier | |
4248 | _getIdType() { | |
4249 | if _isIP "$1"; then | |
b8bfb5a5 | 4250 | echo "$ID_TYPE_IP" |
6ae8d101 | 4251 | else |
b8bfb5a5 | 4252 | echo "$ID_TYPE_DNS" |
6ae8d101 | 4253 | fi |
4254 | } | |
4255 | ||
de4c4eed | 4256 | # beginTime dateTo |
4257 | # beginTime is full string format("2022-04-01T08:10:33Z"), beginTime can be empty, to use current time | |
4258 | # dateTo can be ether in full string format("2022-04-01T08:10:33Z") or in delta format(+5d or +20h) | |
4259 | _convertValidaty() { | |
4260 | _beginTime="$1" | |
4261 | _dateTo="$2" | |
4262 | _debug2 "_beginTime" "$_beginTime" | |
4263 | _debug2 "_dateTo" "$_dateTo" | |
4264 | ||
4265 | if _startswith "$_dateTo" "+"; then | |
4266 | _v_begin=$(_time) | |
4267 | if [ "$_beginTime" ]; then | |
4268 | _v_begin="$(_date2time "$_beginTime")" | |
4269 | fi | |
4270 | _debug2 "_v_begin" "$_v_begin" | |
4271 | if _endswith "$_dateTo" "h"; then | |
4272 | _v_end=$(_math "$_v_begin + 60 * 60 * $(echo "$_dateTo" | tr -d '+h')") | |
4273 | elif _endswith "$_dateTo" "d"; then | |
4274 | _v_end=$(_math "$_v_begin + 60 * 60 * 24 * $(echo "$_dateTo" | tr -d '+d')") | |
4275 | else | |
4276 | _err "Not recognized format for _dateTo: $_dateTo" | |
4277 | return 1 | |
4278 | fi | |
4279 | _debug2 "_v_end" "$_v_end" | |
4280 | _time2str "$_v_end" | |
4281 | else | |
4282 | if [ "$(_time)" -gt "$(_date2time "$_dateTo")" ]; then | |
4283 | _err "The validaty to is in the past: _dateTo = $_dateTo" | |
4284 | return 1 | |
4285 | fi | |
4286 | echo "$_dateTo" | |
4287 | fi | |
4288 | } | |
4289 | ||
3c07f57a | 4290 | #webroot, domain domainlist keylength |
4c3b3608 | 4291 | issue() { |
4c2a3841 | 4292 | if [ -z "$2" ]; then |
2e87e64b | 4293 | _usage "Usage: $PROJECT_ENTRY --issue --domain <domain.tld> --webroot <directory>" |
4c3b3608 | 4294 | return 1 |
4295 | fi | |
49d75a0c | 4296 | if [ -z "$1" ]; then |
4297 | _usage "Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc." | |
4298 | return 1 | |
4299 | fi | |
af1cc3b3 | 4300 | _web_roots="$1" |
4301 | _main_domain="$2" | |
02140ce7 | 4302 | _alt_domains="$3" |
d2cde379 | 4303 | |
af1cc3b3 | 4304 | if _contains "$_main_domain" ","; then |
4305 | _main_domain=$(echo "$2,$3" | cut -d , -f 1) | |
02140ce7 | 4306 | _alt_domains=$(echo "$2,$3" | cut -d , -f 2- | sed "s/,${NO_VALUE}$//") |
2aff36e7 | 4307 | fi |
674b5088 | 4308 | _debug _main_domain "$_main_domain" |
4309 | _debug _alt_domains "$_alt_domains" | |
4310 | ||
d9c9114b | 4311 | _key_length="$4" |
85e1f4ea | 4312 | _real_cert="$5" |
4313 | _real_key="$6" | |
4314 | _real_ca="$7" | |
4315 | _reload_cmd="$8" | |
4316 | _real_fullchain="$9" | |
4317 | _pre_hook="${10}" | |
4318 | _post_hook="${11}" | |
4319 | _renew_hook="${12}" | |
4320 | _local_addr="${13}" | |
875625b1 | 4321 | _challenge_alias="${14}" |
e3ebd582 | 4322 | _preferred_chain="${15}" |
de4c4eed | 4323 | _valid_from="${16}" |
4324 | _valid_to="${17}" | |
4c2a3841 | 4325 | |
bd04638d | 4326 | if [ -z "$_ACME_IS_RENEW" ]; then |
d9c9114b | 4327 | _initpath "$_main_domain" "$_key_length" |
43822d37 | 4328 | mkdir -p "$DOMAIN_PATH" |
89abad79 | 4329 | elif ! _hasfield "$_web_roots" "$W_DNS"; then |
655e34b1 | 4330 | Le_OrderFinalize="" |
4331 | Le_LinkOrder="" | |
4332 | Le_LinkCert="" | |
43822d37 | 4333 | fi |
eccec5f6 | 4334 | |
a0923622 | 4335 | if _hasfield "$_web_roots" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then |
4336 | _err "$_DNS_MANUAL_ERROR" | |
4337 | return 1 | |
4338 | fi | |
4339 | ||
4c2a3841 | 4340 | if [ -f "$DOMAIN_CONF" ]; then |
61623d22 | 4341 | Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime) |
a4270efa | 4342 | _debug Le_NextRenewTime "$Le_NextRenewTime" |
95e06de5 | 4343 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then |
6a90856f | 4344 | _valid_to_saved=$(_readdomainconf Le_Valid_to) |
4345 | if [ "$_valid_to_saved" ] && ! _startswith "$_valid_to_saved" "+"; then | |
4346 | _info "The domain is set to be valid to: $_valid_to_saved" | |
4347 | _info "It can not be renewed automatically" | |
4348 | _info "See: $_VALIDITY_WIKI" | |
4349 | return $RENEW_SKIP | |
4350 | fi | |
bb25febd | 4351 | _saved_domain=$(_readdomainconf Le_Domain) |
4352 | _debug _saved_domain "$_saved_domain" | |
4353 | _saved_alt=$(_readdomainconf Le_Alt) | |
4354 | _debug _saved_alt "$_saved_alt" | |
532e44bc | 4355 | _normized_saved_domains="$(echo "$_saved_domain,$_saved_alt" | tr "," "\n" | sort | tr '\n' ',')" |
4356 | _debug _normized_saved_domains "$_normized_saved_domains" | |
4357 | ||
4358 | _normized_domains="$(echo "$_main_domain,$_alt_domains" | tr "," "\n" | sort | tr '\n' ',')" | |
4359 | _debug _normized_domains "$_normized_domains" | |
4360 | ||
4361 | if [ "$_normized_saved_domains" = "$_normized_domains" ]; then | |
bb25febd | 4362 | _info "Domains not changed." |
4363 | _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")" | |
4c2a3841 | 4364 | _info "Add '$(__red '--force')' to force to renew." |
bb25febd | 4365 | return $RENEW_SKIP |
4366 | else | |
4367 | _info "Domains have changed." | |
4368 | fi | |
4c3b3608 | 4369 | fi |
4370 | fi | |
96a46cfc | 4371 | |
38a067e2 | 4372 | _debug "Using ACME_DIRECTORY: $ACME_DIRECTORY" |
4373 | if ! _initAPI; then | |
4374 | return 1 | |
4375 | fi | |
4376 | ||
af1cc3b3 | 4377 | _savedomainconf "Le_Domain" "$_main_domain" |
02140ce7 | 4378 | _savedomainconf "Le_Alt" "$_alt_domains" |
af1cc3b3 | 4379 | _savedomainconf "Le_Webroot" "$_web_roots" |
4c2a3841 | 4380 | |
c7257bcf | 4381 | _savedomainconf "Le_PreHook" "$_pre_hook" "base64" |
4382 | _savedomainconf "Le_PostHook" "$_post_hook" "base64" | |
4383 | _savedomainconf "Le_RenewHook" "$_renew_hook" "base64" | |
4c2a3841 | 4384 | |
85e1f4ea | 4385 | if [ "$_local_addr" ]; then |
4386 | _savedomainconf "Le_LocalAddress" "$_local_addr" | |
72518d48 | 4387 | else |
4388 | _cleardomainconf "Le_LocalAddress" | |
4389 | fi | |
875625b1 | 4390 | if [ "$_challenge_alias" ]; then |
4391 | _savedomainconf "Le_ChallengeAlias" "$_challenge_alias" | |
4392 | else | |
4393 | _cleardomainconf "Le_ChallengeAlias" | |
4394 | fi | |
e3ebd582 | 4395 | if [ "$_preferred_chain" ]; then |
4396 | _savedomainconf "Le_Preferred_Chain" "$_preferred_chain" "base64" | |
4397 | else | |
4398 | _cleardomainconf "Le_Preferred_Chain" | |
4399 | fi | |
6ae0f7f5 | 4400 | |
a6d22e3b | 4401 | Le_API="$ACME_DIRECTORY" |
4402 | _savedomainconf "Le_API" "$Le_API" | |
4403 | ||
389518e1 | 4404 | _info "Using CA: $ACME_DIRECTORY" |
02140ce7 | 4405 | if [ "$_alt_domains" = "$NO_VALUE" ]; then |
4406 | _alt_domains="" | |
4c3b3608 | 4407 | fi |
4c2a3841 | 4408 | |
85e1f4ea | 4409 | if ! _on_before_issue "$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then |
0463b5d6 | 4410 | _err "_on_before_issue." |
4411 | return 1 | |
4c3b3608 | 4412 | fi |
0463b5d6 | 4413 | |
8a29fbc8 | 4414 | _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")" |
4415 | _debug2 _saved_account_key_hash "$_saved_account_key_hash" | |
4c2a3841 | 4416 | |
e8b54a50 | 4417 | if [ -z "$ACCOUNT_URL" ] || [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then |
57e58ce7 | 4418 | if ! _regAccount "$_accountkeylength"; then |
85e1f4ea | 4419 | _on_issue_err "$_post_hook" |
8a29fbc8 | 4420 | return 1 |
4421 | fi | |
57e58ce7 | 4422 | else |
4423 | _debug "_saved_account_key_hash is not changed, skip register account." | |
166096dc | 4424 | fi |
166096dc | 4425 | |
4c2a3841 | 4426 | if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then |
10afcaca | 4427 | _info "Signing from existing CSR." |
4428 | else | |
b376dfa1 CB |
4429 | # When renewing from an old version, the empty Le_Keylength means 2048. |
4430 | # Note, do not use DEFAULT_DOMAIN_KEY_LENGTH as that value may change over | |
4431 | # time but an empty value implies 2048 specifically. | |
10afcaca | 4432 | _key=$(_readdomainconf Le_Keylength) |
b376dfa1 CB |
4433 | if [ -z "$_key" ]; then |
4434 | _key=2048 | |
4435 | fi | |
10afcaca | 4436 | _debug "Read key length:$_key" |
c4b2e582 | 4437 | if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ] || [ "$Le_ForceNewDomainKey" = "1" ]; then |
d9c9114b | 4438 | if ! createDomainKey "$_main_domain" "$_key_length"; then |
10afcaca | 4439 | _err "Create domain key error." |
4440 | _clearup | |
85e1f4ea | 4441 | _on_issue_err "$_post_hook" |
10afcaca | 4442 | return 1 |
4443 | fi | |
4444 | fi | |
4445 | ||
02140ce7 | 4446 | if ! _createcsr "$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then |
10afcaca | 4447 | _err "Create CSR error." |
5ef501c5 | 4448 | _clearup |
85e1f4ea | 4449 | _on_issue_err "$_post_hook" |
41e3eafa | 4450 | return 1 |
4451 | fi | |
4c3b3608 | 4452 | fi |
10afcaca | 4453 | |
d9c9114b | 4454 | _savedomainconf "Le_Keylength" "$_key_length" |
4c2a3841 | 4455 | |
4c3b3608 | 4456 | vlist="$Le_Vlist" |
882ac74a | 4457 | _cleardomainconf "Le_Vlist" |
cae203be | 4458 | _info "Getting domain auth token for each domain" |
4c3b3608 | 4459 | sep='#' |
9d725af6 | 4460 | dvsep=',' |
4c2a3841 | 4461 | if [ -z "$vlist" ]; then |
af3ea2d4 | 4462 | #make new order request |
6ae8d101 | 4463 | _identifiers="{\"type\":\"$(_getIdType "$_main_domain")\",\"value\":\"$(_idn "$_main_domain")\"}" |
af3ea2d4 | 4464 | _w_index=1 |
4465 | while true; do | |
4466 | d="$(echo "$_alt_domains," | cut -d , -f "$_w_index")" | |
4467 | _w_index="$(_math "$_w_index" + 1)" | |
4468 | _debug d "$d" | |
4469 | if [ -z "$d" ]; then | |
4470 | break | |
c1151b0d | 4471 | fi |
6ae8d101 | 4472 | _identifiers="$_identifiers,{\"type\":\"$(_getIdType "$d")\",\"value\":\"$(_idn "$d")\"}" |
af3ea2d4 | 4473 | done |
4474 | _debug2 _identifiers "$_identifiers" | |
de4c4eed | 4475 | _notBefore="" |
4476 | _notAfter="" | |
4477 | ||
4478 | if [ "$_valid_from" ]; then | |
4479 | _savedomainconf "Le_Valid_From" "$_valid_from" | |
4480 | _debug2 "_valid_from" "$_valid_from" | |
4481 | _notBefore="$(_convertValidaty "" "$_valid_from")" | |
4482 | if [ "$?" != "0" ]; then | |
4483 | _err "Can not parse _valid_from: $_valid_from" | |
4484 | return 1 | |
4485 | fi | |
4486 | if [ "$(_time)" -gt "$(_date2time "$_notBefore")" ]; then | |
4487 | _notBefore="" | |
4488 | fi | |
4489 | else | |
4490 | _cleardomainconf "Le_Valid_From" | |
4491 | fi | |
4492 | _debug2 _notBefore "$_notBefore" | |
4493 | ||
4494 | if [ "$_valid_to" ]; then | |
4495 | _debug2 "_valid_to" "$_valid_to" | |
4496 | _savedomainconf "Le_Valid_To" "$_valid_to" | |
4497 | _notAfter="$(_convertValidaty "$_notBefore" "$_valid_to")" | |
4498 | if [ "$?" != "0" ]; then | |
4499 | _err "Can not parse _valid_to: $_valid_to" | |
4500 | return 1 | |
4501 | fi | |
4502 | else | |
4503 | _cleardomainconf "Le_Valid_To" | |
4504 | fi | |
4505 | _debug2 "_notAfter" "$_notAfter" | |
4506 | ||
4507 | _newOrderObj="{\"identifiers\": [$_identifiers]" | |
4508 | if [ "$_notBefore" ]; then | |
4509 | _newOrderObj="$_newOrderObj,\"notBefore\": \"$_notBefore\"" | |
4510 | fi | |
4511 | if [ "$_notAfter" ]; then | |
4512 | _newOrderObj="$_newOrderObj,\"notAfter\": \"$_notAfter\"" | |
4513 | fi | |
4514 | if ! _send_signed_request "$ACME_NEW_ORDER" "$_newOrderObj}"; then | |
af3ea2d4 | 4515 | _err "Create new order error." |
4516 | _clearup | |
4517 | _on_issue_err "$_post_hook" | |
4518 | return 1 | |
4519 | fi | |
de4c4eed | 4520 | |
af3ea2d4 | 4521 | Le_LinkOrder="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n " | cut -d ":" -f 2-)" |
4522 | _debug Le_LinkOrder "$Le_LinkOrder" | |
4523 | Le_OrderFinalize="$(echo "$response" | _egrep_o '"finalize" *: *"[^"]*"' | cut -d '"' -f 4)" | |
4524 | _debug Le_OrderFinalize "$Le_OrderFinalize" | |
4525 | if [ -z "$Le_OrderFinalize" ]; then | |
4526 | _err "Create new order error. Le_OrderFinalize not found. $response" | |
4527 | _clearup | |
4528 | _on_issue_err "$_post_hook" | |
4529 | return 1 | |
4530 | fi | |
c1151b0d | 4531 | |
af3ea2d4 | 4532 | #for dns manual mode |
4533 | _savedomainconf "Le_OrderFinalize" "$Le_OrderFinalize" | |
c1151b0d | 4534 | |
af3ea2d4 | 4535 | _authorizations_seg="$(echo "$response" | _json_decode | _egrep_o '"authorizations" *: *\[[^\[]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')" |
4536 | _debug2 _authorizations_seg "$_authorizations_seg" | |
4537 | if [ -z "$_authorizations_seg" ]; then | |
4538 | _err "_authorizations_seg not found." | |
4539 | _clearup | |
4540 | _on_issue_err "$_post_hook" | |
4541 | return 1 | |
4542 | fi | |
4543 | ||
4544 | #domain and authz map | |
4545 | _authorizations_map="" | |
4546 | for _authz_url in $(echo "$_authorizations_seg" | tr ',' ' '); do | |
4547 | _debug2 "_authz_url" "$_authz_url" | |
4548 | if ! _send_signed_request "$_authz_url"; then | |
4549 | _err "get to authz error." | |
4550 | _err "_authorizations_seg" "$_authorizations_seg" | |
4551 | _err "_authz_url" "$_authz_url" | |
c1151b0d | 4552 | _clearup |
4553 | _on_issue_err "$_post_hook" | |
4554 | return 1 | |
4555 | fi | |
4556 | ||
af3ea2d4 | 4557 | response="$(echo "$response" | _normalizeJson)" |
4558 | _debug2 response "$response" | |
3e8d9a19 | 4559 | _d="$(echo "$response" | _egrep_o '"value" *: *"[^"]*"' | cut -d : -f 2- | tr -d ' "')" |
af3ea2d4 | 4560 | if _contains "$response" "\"wildcard\" *: *true"; then |
4561 | _d="*.$_d" | |
4562 | fi | |
4563 | _debug2 _d "$_d" | |
4564 | _authorizations_map="$_d,$response | |
c1151b0d | 4565 | $_authorizations_map" |
af3ea2d4 | 4566 | done |
4567 | _debug2 _authorizations_map "$_authorizations_map" | |
c1151b0d | 4568 | |
c1151b0d | 4569 | _index=0 |
a63b05a9 | 4570 | _currentRoot="" |
38f1b4d2 | 4571 | _w_index=1 |
88bbe55b | 4572 | while true; do |
88bbe55b | 4573 | d="$(echo "$_main_domain,$_alt_domains," | cut -d , -f "$_w_index")" |
4574 | _w_index="$(_math "$_w_index" + 1)" | |
4575 | _debug d "$d" | |
4576 | if [ -z "$d" ]; then | |
4577 | break | |
4578 | fi | |
ca7202eb | 4579 | _info "Getting webroot for domain" "$d" |
c1151b0d | 4580 | _index=$(_math $_index + 1) |
af1cc3b3 | 4581 | _w="$(echo $_web_roots | cut -d , -f $_index)" |
9d725af6 | 4582 | _debug _w "$_w" |
4c2a3841 | 4583 | if [ "$_w" ]; then |
a63b05a9 | 4584 | _currentRoot="$_w" |
4585 | fi | |
4586 | _debug "_currentRoot" "$_currentRoot" | |
4c2a3841 | 4587 | |
a63b05a9 | 4588 | vtype="$VTYPE_HTTP" |
c1151b0d | 4589 | #todo, v2 wildcard force to use dns |
3881f221 | 4590 | if _startswith "$_currentRoot" "$W_DNS"; then |
a63b05a9 | 4591 | vtype="$VTYPE_DNS" |
4592 | fi | |
4c2a3841 | 4593 | |
08681f4a | 4594 | if [ "$_currentRoot" = "$W_ALPN" ]; then |
4595 | vtype="$VTYPE_ALPN" | |
4596 | fi | |
4597 | ||
af3ea2d4 | 4598 | _idn_d="$(_idn "$d")" |
4599 | _candidates="$(echo "$_authorizations_map" | grep -i "^$_idn_d,")" | |
4600 | _debug2 _candidates "$_candidates" | |
4601 | if [ "$(echo "$_candidates" | wc -l)" -gt 1 ]; then | |
4602 | for _can in $_candidates; do | |
4603 | if _startswith "$(echo "$_can" | tr '.' '|')" "$(echo "$_idn_d" | tr '.' '|'),"; then | |
4604 | _candidates="$_can" | |
4605 | break | |
4606 | fi | |
4607 | done | |
4608 | fi | |
4609 | response="$(echo "$_candidates" | sed "s/$_idn_d,//")" | |
4610 | _debug2 "response" "$response" | |
4611 | if [ -z "$response" ]; then | |
4612 | _err "get to authz error." | |
4613 | _err "_authorizations_map" "$_authorizations_map" | |
4614 | _clearup | |
4615 | _on_issue_err "$_post_hook" | |
4616 | return 1 | |
c4d8fd83 | 4617 | fi |
4618 | ||
4c2a3841 | 4619 | if [ -z "$thumbprint" ]; then |
339a8ad6 | 4620 | thumbprint="$(__calc_account_thumbprint)" |
4c3b3608 | 4621 | fi |
4622 | ||
dbc44c08 | 4623 | entry="$(echo "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')" |
4c3b3608 | 4624 | _debug entry "$entry" |
9541ea6a | 4625 | keyauthorization="" |
4c2a3841 | 4626 | if [ -z "$entry" ]; then |
9541ea6a | 4627 | if ! _startswith "$d" '*.'; then |
4628 | _debug "Not a wildcard domain, lets check whether the validation is already valid." | |
4629 | if echo "$response" | grep '"status":"valid"' >/dev/null 2>&1; then | |
4630 | _debug "$d is already valid." | |
4631 | keyauthorization="$STATE_VERIFIED" | |
4632 | _debug keyauthorization "$keyauthorization" | |
4633 | fi | |
4634 | fi | |
4635 | if [ -z "$keyauthorization" ]; then | |
fc3a1817 | 4636 | _err "Error, can not get domain token entry $d for $vtype" |
9541ea6a | 4637 | _supported_vtypes="$(echo "$response" | _egrep_o "\"challenges\":\[[^]]*]" | tr '{' "\n" | grep type | cut -d '"' -f 4 | tr "\n" ' ')" |
4638 | if [ "$_supported_vtypes" ]; then | |
4639 | _err "The supported validation types are: $_supported_vtypes, but you specified: $vtype" | |
4640 | fi | |
4641 | _clearup | |
4642 | _on_issue_err "$_post_hook" | |
4643 | return 1 | |
b51ed9bb | 4644 | fi |
c1151b0d | 4645 | fi |
f8b225e7 | 4646 | |
9541ea6a | 4647 | if [ -z "$keyauthorization" ]; then |
4648 | token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')" | |
4649 | _debug token "$token" | |
cae203be | 4650 | |
9541ea6a | 4651 | if [ -z "$token" ]; then |
4652 | _err "Error, can not get domain token $entry" | |
4653 | _clearup | |
4654 | _on_issue_err "$_post_hook" | |
4655 | return 1 | |
4656 | fi | |
af3ea2d4 | 4657 | |
4658 | uri="$(echo "$entry" | _egrep_o '"url":"[^"]*' | cut -d '"' -f 4 | _head_n 1)" | |
4659 | ||
9541ea6a | 4660 | _debug uri "$uri" |
4c3b3608 | 4661 | |
9541ea6a | 4662 | if [ -z "$uri" ]; then |
4663 | _err "Error, can not get domain uri. $entry" | |
4664 | _clearup | |
4665 | _on_issue_err "$_post_hook" | |
4666 | return 1 | |
4667 | fi | |
4668 | keyauthorization="$token.$thumbprint" | |
d35bf517 | 4669 | _debug keyauthorization "$keyauthorization" |
9541ea6a | 4670 | |
4671 | if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then | |
4672 | _debug "$d is already verified." | |
4673 | keyauthorization="$STATE_VERIFIED" | |
4674 | _debug keyauthorization "$keyauthorization" | |
4675 | fi | |
ec603bee | 4676 | fi |
4677 | ||
a63b05a9 | 4678 | dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot" |
4c3b3608 | 4679 | _debug dvlist "$dvlist" |
4c2a3841 | 4680 | |
9d725af6 | 4681 | vlist="$vlist$dvlist$dvsep" |
4c3b3608 | 4682 | |
4683 | done | |
9d725af6 | 4684 | _debug vlist "$vlist" |
4c3b3608 | 4685 | #add entry |
b5ca9bba | 4686 | dns_entries="" |
4c3b3608 | 4687 | dnsadded="" |
9d725af6 | 4688 | ventries=$(echo "$vlist" | tr "$dvsep" ' ') |
1f7df33e | 4689 | _alias_index=1 |
4c2a3841 | 4690 | for ventry in $ventries; do |
ca7202eb | 4691 | d=$(echo "$ventry" | cut -d "$sep" -f 1) |
4692 | keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2) | |
4693 | vtype=$(echo "$ventry" | cut -d "$sep" -f 4) | |
4694 | _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5) | |
72f54ca6 | 4695 | _debug d "$d" |
4c2a3841 | 4696 | if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then |
69212114 | 4697 | _debug "$d is already verified, skip $vtype." |
fd536d37 | 4698 | _alias_index="$(_math "$_alias_index" + 1)" |
ec603bee | 4699 | continue |
4700 | fi | |
4701 | ||
4c2a3841 | 4702 | if [ "$vtype" = "$VTYPE_DNS" ]; then |
4c3b3608 | 4703 | dnsadded='0' |
72f54ca6 | 4704 | _dns_root_d="$d" |
4705 | if _startswith "$_dns_root_d" "*."; then | |
4706 | _dns_root_d="$(echo "$_dns_root_d" | sed 's/*.//')" | |
4707 | fi | |
875625b1 | 4708 | _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")" |
39bc4177 | 4709 | test "$_d_alias" = "$NO_VALUE" && _d_alias="" |
875625b1 | 4710 | _alias_index="$(_math "$_alias_index" + 1)" |
4711 | _debug "_d_alias" "$_d_alias" | |
4712 | if [ "$_d_alias" ]; then | |
64821ad4 | 4713 | if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then |
4714 | txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")" | |
4715 | else | |
4716 | txtdomain="_acme-challenge.$_d_alias" | |
4717 | fi | |
82b0ebb7 | 4718 | dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$txtdomain$dvsep$_currentRoot" |
875625b1 | 4719 | else |
4720 | txtdomain="_acme-challenge.$_dns_root_d" | |
82b0ebb7 | 4721 | dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$dvsep$_currentRoot" |
875625b1 | 4722 | fi |
82b0ebb7 | 4723 | |
4c3b3608 | 4724 | _debug txtdomain "$txtdomain" |
11927a76 | 4725 | txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)" |
4c3b3608 | 4726 | _debug txt "$txt" |
a61fe418 | 4727 | |
b50e701c | 4728 | d_api="$(_findHook "$_dns_root_d" $_SUB_FOLDER_DNSAPI "$_currentRoot")" |
4c3b3608 | 4729 | _debug d_api "$d_api" |
82b0ebb7 | 4730 | |
4731 | dns_entry="$dns_entry$dvsep$txt${dvsep}$d_api" | |
4732 | _debug2 dns_entry "$dns_entry" | |
4c2a3841 | 4733 | if [ "$d_api" ]; then |
a180b95c | 4734 | _debug "Found domain api file: $d_api" |
4c3b3608 | 4735 | else |
3881f221 | 4736 | if [ "$_currentRoot" != "$W_DNS" ]; then |
4737 | _err "Can not find dns api hook for: $_currentRoot" | |
4738 | _info "You need to add the txt record manually." | |
4739 | fi | |
5f8b60a0 | 4740 | _info "$(__red "Add the following TXT record:")" |
81772fb7 | 4741 | _info "$(__red "Domain: '$(__green "$txtdomain")'")" |
4742 | _info "$(__red "TXT value: '$(__green "$txt")'")" | |
4743 | _info "$(__red "Please be aware that you prepend _acme-challenge. before your domain")" | |
4744 | _info "$(__red "so the resulting subdomain will be: $txtdomain")" | |
4c3b3608 | 4745 | continue |
4746 | fi | |
4c2a3841 | 4747 | |
73b8b120 | 4748 | ( |
ca7202eb | 4749 | if ! . "$d_api"; then |
73b8b120 | 4750 | _err "Load file $d_api error. Please check your api file and try again." |
4751 | return 1 | |
4752 | fi | |
4c2a3841 | 4753 | |
158f22f7 | 4754 | addcommand="${_currentRoot}_add" |
ca7202eb | 4755 | if ! _exists "$addcommand"; then |
73b8b120 | 4756 | _err "It seems that your api file is not correct, it must have a function named: $addcommand" |
4757 | return 1 | |
4758 | fi | |
a180b95c | 4759 | _info "Adding txt value: $txt for domain: $txtdomain" |
ca7202eb | 4760 | if ! $addcommand "$txtdomain" "$txt"; then |
73b8b120 | 4761 | _err "Error add txt for domain:$txtdomain" |
4762 | return 1 | |
4763 | fi | |
a180b95c | 4764 | _info "The txt record is added: Success." |
73b8b120 | 4765 | ) |
4c2a3841 | 4766 | |
4767 | if [ "$?" != "0" ]; then | |
ea722da3 | 4768 | _on_issue_err "$_post_hook" "$vlist" |
545f2355 | 4769 | _clearup |
4c3b3608 | 4770 | return 1 |
4771 | fi | |
82b0ebb7 | 4772 | dns_entries="$dns_entries$dns_entry |
4773 | " | |
4774 | _debug2 "$dns_entries" | |
4c3b3608 | 4775 | dnsadded='1' |
4776 | fi | |
4777 | done | |
4778 | ||
4c2a3841 | 4779 | if [ "$dnsadded" = '0' ]; then |
4780 | _savedomainconf "Le_Vlist" "$vlist" | |
4c3b3608 | 4781 | _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit." |
a8b62261 | 4782 | _err "Please add the TXT records to the domains, and re-run with --renew." |
5f8b60a0 | 4783 | _on_issue_err "$_post_hook" |
545f2355 | 4784 | _clearup |
4c3b3608 | 4785 | return 1 |
4786 | fi | |
4c2a3841 | 4787 | |
4c3b3608 | 4788 | fi |
4c2a3841 | 4789 | |
b5ca9bba | 4790 | if [ "$dns_entries" ]; then |
4c2a3841 | 4791 | if [ -z "$Le_DNSSleep" ]; then |
427c2780 | 4792 | _info "Let's check each DNS record now. Sleep 20 seconds first." |
b5ca9bba | 4793 | _sleep 20 |
4794 | if ! _check_dns_entries; then | |
4795 | _err "check dns error." | |
4796 | _on_issue_err "$_post_hook" | |
4797 | _clearup | |
4798 | return 1 | |
4799 | fi | |
0e38c60d | 4800 | else |
4c2a3841 | 4801 | _savedomainconf "Le_DNSSleep" "$Le_DNSSleep" |
b5ca9bba | 4802 | _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect" |
4803 | _sleep "$Le_DNSSleep" | |
0e38c60d | 4804 | fi |
4c3b3608 | 4805 | fi |
4c2a3841 | 4806 | |
5d943a35 | 4807 | NGINX_RESTORE_VLIST="" |
4c3b3608 | 4808 | _debug "ok, let's start to verify" |
a63b05a9 | 4809 | |
0463b5d6 | 4810 | _ncIndex=1 |
9d725af6 | 4811 | ventries=$(echo "$vlist" | tr "$dvsep" ' ') |
4c2a3841 | 4812 | for ventry in $ventries; do |
ca7202eb | 4813 | d=$(echo "$ventry" | cut -d "$sep" -f 1) |
4814 | keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2) | |
4815 | uri=$(echo "$ventry" | cut -d "$sep" -f 3) | |
4816 | vtype=$(echo "$ventry" | cut -d "$sep" -f 4) | |
4817 | _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5) | |
ec603bee | 4818 | |
4c2a3841 | 4819 | if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then |
ec603bee | 4820 | _info "$d is already verified, skip $vtype." |
4821 | continue | |
4822 | fi | |
4823 | ||
dd068467 | 4824 | _info "Verifying: $d" |
4c3b3608 | 4825 | _debug "d" "$d" |
4826 | _debug "keyauthorization" "$keyauthorization" | |
4827 | _debug "uri" "$uri" | |
4828 | removelevel="" | |
e22bcf7c | 4829 | token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)" |
a63b05a9 | 4830 | |
4831 | _debug "_currentRoot" "$_currentRoot" | |
4832 | ||
4c2a3841 | 4833 | if [ "$vtype" = "$VTYPE_HTTP" ]; then |
4834 | if [ "$_currentRoot" = "$NO_VALUE" ]; then | |
4c3b3608 | 4835 | _info "Standalone mode server" |
85e1f4ea | 4836 | _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")" |
0463b5d6 | 4837 | _ncIndex="$(_math $_ncIndex + 1)" |
3794b5cb | 4838 | _startserver "$keyauthorization" "$_ncaddr" |
4c2a3841 | 4839 | if [ "$?" != "0" ]; then |
5ef501c5 | 4840 | _clearup |
58e4d337 | 4841 | _on_issue_err "$_post_hook" "$vlist" |
6fc1447f | 4842 | return 1 |
4843 | fi | |
5dbf664a | 4844 | sleep 1 |
ca7202eb | 4845 | _debug serverproc "$serverproc" |
0e44f587 | 4846 | elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then |
4847 | _info "Stateless mode for domain:$d" | |
4848 | _sleep 1 | |
9d725af6 | 4849 | elif _startswith "$_currentRoot" "$NGINX"; then |
4850 | _info "Nginx mode for domain:$d" | |
4851 | #set up nginx server | |
4852 | FOUND_REAL_NGINX_CONF="" | |
4853 | BACKUP_NGINX_CONF="" | |
4854 | if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then | |
4855 | _clearup | |
58e4d337 | 4856 | _on_issue_err "$_post_hook" "$vlist" |
9d725af6 | 4857 | return 1 |
03f8d6e9 | 4858 | fi |
302c41ed | 4859 | |
03f8d6e9 | 4860 | if [ "$FOUND_REAL_NGINX_CONF" ]; then |
9d725af6 | 4861 | _realConf="$FOUND_REAL_NGINX_CONF" |
4862 | _backup="$BACKUP_NGINX_CONF" | |
4863 | _debug _realConf "$_realConf" | |
5d943a35 | 4864 | NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST" |
9d725af6 | 4865 | fi |
4866 | _sleep 1 | |
4c3b3608 | 4867 | else |
4c2a3841 | 4868 | if [ "$_currentRoot" = "apache" ]; then |
6f930641 | 4869 | wellknown_path="$ACME_DIR" |
4870 | else | |
a63b05a9 | 4871 | wellknown_path="$_currentRoot/.well-known/acme-challenge" |
4c2a3841 | 4872 | if [ ! -d "$_currentRoot/.well-known" ]; then |
6f930641 | 4873 | removelevel='1' |
4c2a3841 | 4874 | elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then |
6f930641 | 4875 | removelevel='2' |
4876 | else | |
4877 | removelevel='3' | |
4878 | fi | |
4c3b3608 | 4879 | fi |
6f930641 | 4880 | |
4c3b3608 | 4881 | _debug wellknown_path "$wellknown_path" |
6f930641 | 4882 | |
4c3b3608 | 4883 | _debug "writing token:$token to $wellknown_path/$token" |
4884 | ||
4885 | mkdir -p "$wellknown_path" | |
93fc48a2 | 4886 | |
4c2a3841 | 4887 | if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then |
93fc48a2 | 4888 | _err "$d:Can not write token to file : $wellknown_path/$token" |
4889 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4890 | _clearup | |
58e4d337 | 4891 | _on_issue_err "$_post_hook" "$vlist" |
93fc48a2 | 4892 | return 1 |
4893 | fi | |
4894 | ||
4c2a3841 | 4895 | if [ ! "$usingApache" ]; then |
44edb2bd | 4896 | if webroot_owner=$(_stat "$_currentRoot"); then |
32fdc196 | 4897 | _debug "Changing owner/group of .well-known to $webroot_owner" |
c87cd0de | 4898 | if ! _exec "chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then |
4899 | _debug "$(cat "$_EXEC_TEMP_ERR")" | |
4900 | _exec_err >/dev/null 2>&1 | |
4901 | fi | |
32fdc196 | 4902 | else |
b54ce310 | 4903 | _debug "not changing owner/group of webroot" |
32fdc196 | 4904 | fi |
df886ffa | 4905 | fi |
4c2a3841 | 4906 | |
4c3b3608 | 4907 | fi |
08681f4a | 4908 | elif [ "$vtype" = "$VTYPE_ALPN" ]; then |
4909 | acmevalidationv1="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")" | |
4910 | _debug acmevalidationv1 "$acmevalidationv1" | |
4911 | if ! _starttlsserver "$d" "" "$Le_TLSPort" "$keyauthorization" "$_ncaddr" "$acmevalidationv1"; then | |
4912 | _err "Start tls server error." | |
4913 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4914 | _clearup | |
4915 | _on_issue_err "$_post_hook" "$vlist" | |
4916 | return 1 | |
4917 | fi | |
4c3b3608 | 4918 | fi |
4c2a3841 | 4919 | |
920cab6f | 4920 | if ! __trigger_validation "$uri" "$keyauthorization" "$vtype"; then |
c4d8fd83 | 4921 | _err "$d:Can not get challenge: $response" |
4922 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4923 | _clearup | |
58e4d337 | 4924 | _on_issue_err "$_post_hook" "$vlist" |
c4d8fd83 | 4925 | return 1 |
4926 | fi | |
4c2a3841 | 4927 | |
c1151b0d | 4928 | if [ "$code" ] && [ "$code" != '202' ]; then |
8bd12ed0 | 4929 | if [ "$code" = '200' ]; then |
c1151b0d | 4930 | _debug "trigger validation code: $code" |
4931 | else | |
8bd12ed0 K |
4932 | _err "$d:Challenge error: $response" |
4933 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4934 | _clearup | |
4935 | _on_issue_err "$_post_hook" "$vlist" | |
4936 | return 1 | |
c1151b0d | 4937 | fi |
4c3b3608 | 4938 | fi |
4c2a3841 | 4939 | |
6fc1447f | 4940 | waittimes=0 |
4c2a3841 | 4941 | if [ -z "$MAX_RETRY_TIMES" ]; then |
6fc1447f | 4942 | MAX_RETRY_TIMES=30 |
4943 | fi | |
4c2a3841 | 4944 | |
4945 | while true; do | |
0c538f75 | 4946 | waittimes=$(_math "$waittimes" + 1) |
4c2a3841 | 4947 | if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then |
6fc1447f | 4948 | _err "$d:Timeout" |
4949 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4950 | _clearup | |
58e4d337 | 4951 | _on_issue_err "$_post_hook" "$vlist" |
6fc1447f | 4952 | return 1 |
4953 | fi | |
4c2a3841 | 4954 | |
9aaf36cd | 4955 | _debug2 original "$response" |
4c2a3841 | 4956 | |
4957 | response="$(echo "$response" | _normalizeJson)" | |
7012b91f | 4958 | _debug2 response "$response" |
4c2a3841 | 4959 | |
4960 | status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"') | |
dcc50093 | 4961 | _debug2 status "$status" |
aede5c48 | 4962 | if _contains "$status" "invalid"; then |
d0d74907 | 4963 | error="$(echo "$response" | _egrep_o '"error":\{[^\}]*')" |
4c2a3841 | 4964 | _debug2 error "$error" |
4965 | errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)" | |
4966 | _debug2 errordetail "$errordetail" | |
4967 | if [ "$errordetail" ]; then | |
4968 | _err "$d:Verify error:$errordetail" | |
4969 | else | |
4970 | _err "$d:Verify error:$error" | |
4971 | fi | |
4972 | if [ "$DEBUG" ]; then | |
4973 | if [ "$vtype" = "$VTYPE_HTTP" ]; then | |
4974 | _debug "Debug: get token url." | |
4975 | _get "http://$d/.well-known/acme-challenge/$token" "" 1 | |
4976 | fi | |
4977 | fi | |
a63b05a9 | 4978 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 4979 | _clearup |
58e4d337 | 4980 | _on_issue_err "$_post_hook" "$vlist" |
4c2a3841 | 4981 | return 1 |
4c3b3608 | 4982 | fi |
4c2a3841 | 4983 | |
aede5c48 | 4984 | if _contains "$status" "valid"; then |
4985 | _info "$(__green Success)" | |
4986 | _stopserver "$serverproc" | |
4987 | serverproc="" | |
4988 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4989 | break | |
4990 | fi | |
4991 | ||
4c2a3841 | 4992 | if [ "$status" = "pending" ]; then |
103810ce | 4993 | _info "Pending, The CA is processing your order, please just wait. ($waittimes/$MAX_RETRY_TIMES)" |
93740c99 | 4994 | elif [ "$status" = "processing" ]; then |
103810ce | 4995 | _info "Processing, The CA is processing your order, please just wait. ($waittimes/$MAX_RETRY_TIMES)" |
4c3b3608 | 4996 | else |
4c2a3841 | 4997 | _err "$d:Verify error:$response" |
a63b05a9 | 4998 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 4999 | _clearup |
58e4d337 | 5000 | _on_issue_err "$_post_hook" "$vlist" |
4c3b3608 | 5001 | return 1 |
5002 | fi | |
dcc50093 | 5003 | _debug "sleep 2 secs to verify again" |
14b59142 | 5004 | _sleep 2 |
dcc50093 | 5005 | _debug "checking" |
4c2a3841 | 5006 | |
dcc50093 | 5007 | _send_signed_request "$uri" |
5008 | ||
5009 | if [ "$?" != "0" ]; then | |
5010 | _err "$d:Verify error:$response" | |
5011 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
5012 | _clearup | |
5013 | _on_issue_err "$_post_hook" "$vlist" | |
5014 | return 1 | |
5015 | fi | |
4c3b3608 | 5016 | done |
4c2a3841 | 5017 | |
4c3b3608 | 5018 | done |
5019 | ||
5020 | _clearup | |
5021 | _info "Verify finished, start to sign." | |
11927a76 | 5022 | der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)" |
4c2a3841 | 5023 | |
af3ea2d4 | 5024 | _info "Lets finalize the order." |
5025 | _info "Le_OrderFinalize" "$Le_OrderFinalize" | |
5026 | if ! _send_signed_request "${Le_OrderFinalize}" "{\"csr\": \"$der\"}"; then | |
5027 | _err "Sign failed." | |
5028 | _on_issue_err "$_post_hook" | |
5029 | return 1 | |
5030 | fi | |
5031 | if [ "$code" != "200" ]; then | |
5032 | _err "Sign failed, finalize code is not 200." | |
5033 | _err "$response" | |
5034 | _on_issue_err "$_post_hook" | |
5035 | return 1 | |
5036 | fi | |
5037 | if [ -z "$Le_LinkOrder" ]; then | |
5038 | Le_LinkOrder="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n \t" | cut -d ":" -f 2-)" | |
5039 | fi | |
e7f7e96d | 5040 | |
af3ea2d4 | 5041 | _savedomainconf "Le_LinkOrder" "$Le_LinkOrder" |
5042 | ||
5043 | _link_cert_retry=0 | |
5044 | _MAX_CERT_RETRY=30 | |
5045 | while [ "$_link_cert_retry" -lt "$_MAX_CERT_RETRY" ]; do | |
5046 | if _contains "$response" "\"status\":\"valid\""; then | |
5047 | _debug "Order status is valid." | |
5048 | Le_LinkCert="$(echo "$response" | _egrep_o '"certificate" *: *"[^"]*"' | cut -d '"' -f 4)" | |
5049 | _debug Le_LinkCert "$Le_LinkCert" | |
5050 | if [ -z "$Le_LinkCert" ]; then | |
5051 | _err "Sign error, can not find Le_LinkCert" | |
e7f7e96d | 5052 | _err "$response" |
5053 | _on_issue_err "$_post_hook" | |
5054 | return 1 | |
5055 | fi | |
af3ea2d4 | 5056 | break |
5057 | elif _contains "$response" "\"processing\""; then | |
5058 | _info "Order status is processing, lets sleep and retry." | |
5059 | _retryafter=$(echo "$responseHeaders" | grep -i "^Retry-After *:" | cut -d : -f 2 | tr -d ' ' | tr -d '\r') | |
5060 | _debug "_retryafter" "$_retryafter" | |
5061 | if [ "$_retryafter" ]; then | |
5062 | _info "Retry after: $_retryafter" | |
5063 | _sleep $_retryafter | |
5064 | else | |
5065 | _sleep 2 | |
e7f7e96d | 5066 | fi |
af3ea2d4 | 5067 | else |
5068 | _err "Sign error, wrong status" | |
e7f7e96d | 5069 | _err "$response" |
5070 | _on_issue_err "$_post_hook" | |
5071 | return 1 | |
5072 | fi | |
af3ea2d4 | 5073 | #the order is processing, so we are going to poll order status |
5074 | if [ -z "$Le_LinkOrder" ]; then | |
5075 | _err "Sign error, can not get order link location header" | |
5076 | _err "responseHeaders" "$responseHeaders" | |
5077 | _on_issue_err "$_post_hook" | |
5078 | return 1 | |
5079 | fi | |
5080 | _info "Polling order status: $Le_LinkOrder" | |
5081 | if ! _send_signed_request "$Le_LinkOrder"; then | |
5082 | _err "Sign failed, can not post to Le_LinkOrder cert:$Le_LinkOrder." | |
668c43ab | 5083 | _err "$response" |
c1151b0d | 5084 | _on_issue_err "$_post_hook" |
5085 | return 1 | |
5086 | fi | |
af3ea2d4 | 5087 | _link_cert_retry="$(_math $_link_cert_retry + 1)" |
5088 | done | |
4c3b3608 | 5089 | |
af3ea2d4 | 5090 | if [ -z "$Le_LinkCert" ]; then |
5091 | _err "Sign failed, can not get Le_LinkCert, retry time limit." | |
5092 | _err "$response" | |
5093 | _on_issue_err "$_post_hook" | |
5094 | return 1 | |
5095 | fi | |
5096 | _info "Downloading cert." | |
5097 | _info "Le_LinkCert" "$Le_LinkCert" | |
5098 | if ! _send_signed_request "$Le_LinkCert"; then | |
5099 | _err "Sign failed, can not download cert:$Le_LinkCert." | |
5100 | _err "$response" | |
5101 | _on_issue_err "$_post_hook" | |
5102 | return 1 | |
5103 | fi | |
e3ebd582 | 5104 | |
af3ea2d4 | 5105 | echo "$response" >"$CERT_PATH" |
5106 | _split_cert_chain "$CERT_PATH" "$CERT_FULLCHAIN_PATH" "$CA_CERT_PATH" | |
84fe6654 | 5107 | if [ -z "$_preferred_chain" ]; then |
5108 | _preferred_chain=$(_readcaconf DEFAULT_PREFERRED_CHAIN) | |
5109 | fi | |
af3ea2d4 | 5110 | if [ "$_preferred_chain" ] && [ -f "$CERT_FULLCHAIN_PATH" ]; then |
5111 | if [ "$DEBUG" ]; then | |
5112 | _debug "default chain issuers: " "$(_get_chain_issuers "$CERT_FULLCHAIN_PATH")" | |
5113 | fi | |
5114 | if ! _match_issuer "$CERT_FULLCHAIN_PATH" "$_preferred_chain"; then | |
5115 | rels="$(echo "$responseHeaders" | tr -d ' <>' | grep -i "^link:" | grep -i 'rel="alternate"' | cut -d : -f 2- | cut -d ';' -f 1)" | |
5116 | _debug2 "rels" "$rels" | |
5117 | for rel in $rels; do | |
5118 | _info "Try rel: $rel" | |
5119 | if ! _send_signed_request "$rel"; then | |
5120 | _err "Sign failed, can not download cert:$rel" | |
5121 | _err "$response" | |
5122 | continue | |
5123 | fi | |
5124 | _relcert="$CERT_PATH.alt" | |
5125 | _relfullchain="$CERT_FULLCHAIN_PATH.alt" | |
5126 | _relca="$CA_CERT_PATH.alt" | |
5127 | echo "$response" >"$_relcert" | |
5128 | _split_cert_chain "$_relcert" "$_relfullchain" "$_relca" | |
5129 | if [ "$DEBUG" ]; then | |
5130 | _debug "rel chain issuers: " "$(_get_chain_issuers "$_relfullchain")" | |
5131 | fi | |
5132 | if _match_issuer "$_relfullchain" "$_preferred_chain"; then | |
5133 | _info "Matched issuer in: $rel" | |
5134 | cat $_relcert >"$CERT_PATH" | |
5135 | cat $_relfullchain >"$CERT_FULLCHAIN_PATH" | |
5136 | cat $_relca >"$CA_CERT_PATH" | |
12b19165 | 5137 | rm -f "$_relcert" |
5138 | rm -f "$_relfullchain" | |
5139 | rm -f "$_relca" | |
af3ea2d4 | 5140 | break |
5141 | fi | |
5142 | rm -f "$_relcert" | |
5143 | rm -f "$_relfullchain" | |
5144 | rm -f "$_relca" | |
5145 | done | |
d404e92d | 5146 | fi |
c1151b0d | 5147 | fi |
5148 | ||
5149 | _debug "Le_LinkCert" "$Le_LinkCert" | |
5150 | _savedomainconf "Le_LinkCert" "$Le_LinkCert" | |
5151 | ||
183063a2 | 5152 | if [ -z "$Le_LinkCert" ] || ! _checkcert "$CERT_PATH"; then |
716f7277 | 5153 | response="$(echo "$response" | _dbase64 "multiline" | tr -d '\0' | _normalizeJson)" |
183063a2 | 5154 | _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')" |
5155 | _on_issue_err "$_post_hook" | |
5156 | return 1 | |
5157 | fi | |
5158 | ||
c1151b0d | 5159 | if [ "$Le_LinkCert" ]; then |
43822d37 | 5160 | _info "$(__green "Cert success.")" |
4c3b3608 | 5161 | cat "$CERT_PATH" |
5980ebc7 | 5162 | |
6b97dc67 | 5163 | _info "Your cert is in: $(__green "$CERT_PATH")" |
4c2a3841 | 5164 | |
5165 | if [ -f "$CERT_KEY_PATH" ]; then | |
6b97dc67 | 5166 | _info "Your cert key is in: $(__green "$CERT_KEY_PATH")" |
5980ebc7 | 5167 | fi |
5168 | ||
bd04638d | 5169 | if [ ! "$USER_PATH" ] || [ ! "$_ACME_IN_CRON" ]; then |
281aa349 | 5170 | USER_PATH="$PATH" |
5171 | _saveaccountconf "USER_PATH" "$USER_PATH" | |
5172 | fi | |
4c3b3608 | 5173 | fi |
4c3b3608 | 5174 | |
6b97dc67 | 5175 | [ -f "$CA_CERT_PATH" ] && _info "The intermediate CA cert is in: $(__green "$CA_CERT_PATH")" |
5176 | [ -f "$CERT_FULLCHAIN_PATH" ] && _info "And the full chain certs is there: $(__green "$CERT_FULLCHAIN_PATH")" | |
4c2a3841 | 5177 | |
3aae1ae3 | 5178 | Le_CertCreateTime=$(_time) |
4c2a3841 | 5179 | _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime" |
5180 | ||
2c28d6b1 | 5181 | Le_CertCreateTimeStr=$(_time2str "$Le_CertCreateTime") |
4c2a3841 | 5182 | _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr" |
5183 | ||
ec67a1b2 | 5184 | if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ]; then |
5185 | Le_RenewalDays="$DEFAULT_RENEW" | |
054cb72e | 5186 | else |
4c2a3841 | 5187 | _savedomainconf "Le_RenewalDays" "$Le_RenewalDays" |
13d7cae9 | 5188 | fi |
4c2a3841 | 5189 | |
5190 | if [ "$CA_BUNDLE" ]; then | |
78009539 PS |
5191 | _saveaccountconf CA_BUNDLE "$CA_BUNDLE" |
5192 | else | |
5193 | _clearaccountconf "CA_BUNDLE" | |
5194 | fi | |
5195 | ||
2aa75f03 | 5196 | if [ "$CA_PATH" ]; then |
5197 | _saveaccountconf CA_PATH "$CA_PATH" | |
5198 | else | |
5199 | _clearaccountconf "CA_PATH" | |
5200 | fi | |
78009539 | 5201 | |
4c2a3841 | 5202 | if [ "$HTTPS_INSECURE" ]; then |
fac1e367 | 5203 | _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE" |
5204 | else | |
4c2a3841 | 5205 | _clearaccountconf "HTTPS_INSECURE" |
13d7cae9 | 5206 | fi |
00a50605 | 5207 | |
4c2a3841 | 5208 | if [ "$Le_Listen_V4" ]; then |
5209 | _savedomainconf "Le_Listen_V4" "$Le_Listen_V4" | |
50827188 | 5210 | _cleardomainconf Le_Listen_V6 |
4c2a3841 | 5211 | elif [ "$Le_Listen_V6" ]; then |
5212 | _savedomainconf "Le_Listen_V6" "$Le_Listen_V6" | |
50827188 | 5213 | _cleardomainconf Le_Listen_V4 |
5214 | fi | |
f6dcd989 | 5215 | |
c4b2e582 | 5216 | if [ "$Le_ForceNewDomainKey" = "1" ]; then |
5217 | _savedomainconf "Le_ForceNewDomainKey" "$Le_ForceNewDomainKey" | |
5218 | else | |
5219 | _cleardomainconf Le_ForceNewDomainKey | |
5220 | fi | |
de4c4eed | 5221 | if [ "$_notAfter" ]; then |
5222 | Le_NextRenewTime=$(_date2time "$_notAfter") | |
5223 | Le_NextRenewTimeStr="$_notAfter" | |
6a90856f | 5224 | if [ "$_valid_to" ] && ! _startswith "$_valid_to" "+"; then |
5225 | _info "The domain is set to be valid to: $_valid_to" | |
5226 | _info "It can not be renewed automatically" | |
5227 | _info "See: $_VALIDITY_WIKI" | |
5228 | fi | |
de4c4eed | 5229 | else |
5230 | Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60) | |
5231 | Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime") | |
5232 | Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400) | |
5233 | fi | |
4c2a3841 | 5234 | _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr" |
4c2a3841 | 5235 | _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime" |
f6dcd989 | 5236 | |
85e1f4ea | 5237 | if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then |
5238 | _savedomainconf "Le_RealCertPath" "$_real_cert" | |
5239 | _savedomainconf "Le_RealCACertPath" "$_real_ca" | |
5240 | _savedomainconf "Le_RealKeyPath" "$_real_key" | |
7690f73e | 5241 | _savedomainconf "Le_ReloadCmd" "$_reload_cmd" "base64" |
85e1f4ea | 5242 | _savedomainconf "Le_RealFullChainPath" "$_real_fullchain" |
ce8dca7a | 5243 | if ! _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"; then |
5244 | return 1 | |
5245 | fi | |
01f54558 | 5246 | fi |
4c0d3f1b | 5247 | |
ce8dca7a | 5248 | if ! _on_issue_success "$_post_hook" "$_renew_hook"; then |
5249 | _err "Call hook error." | |
5250 | return 1 | |
5251 | fi | |
4c3b3608 | 5252 | } |
5253 | ||
d73438a3 | 5254 | #in_out_cert out_fullchain out_ca |
e3ebd582 | 5255 | _split_cert_chain() { |
5256 | _certf="$1" | |
5257 | _fullchainf="$2" | |
5258 | _caf="$3" | |
5259 | if [ "$(grep -- "$BEGIN_CERT" "$_certf" | wc -l)" -gt "1" ]; then | |
5260 | _debug "Found cert chain" | |
5261 | cat "$_certf" >"$_fullchainf" | |
5262 | _end_n="$(grep -n -- "$END_CERT" "$_fullchainf" | _head_n 1 | cut -d : -f 1)" | |
5263 | _debug _end_n "$_end_n" | |
5264 | sed -n "1,${_end_n}p" "$_fullchainf" >"$_certf" | |
5265 | _end_n="$(_math $_end_n + 1)" | |
5266 | sed -n "${_end_n},9999p" "$_fullchainf" >"$_caf" | |
5267 | fi | |
5268 | } | |
5269 | ||
8b7a86bd | 5270 | #domain [isEcc] [server] |
4c3b3608 | 5271 | renew() { |
5272 | Le_Domain="$1" | |
4c2a3841 | 5273 | if [ -z "$Le_Domain" ]; then |
8b7a86bd | 5274 | _usage "Usage: $PROJECT_ENTRY --renew --domain <domain.tld> [--ecc] [--server server]" |
4c3b3608 | 5275 | return 1 |
5276 | fi | |
5277 | ||
43822d37 | 5278 | _isEcc="$2" |
8b7a86bd | 5279 | _renewServer="$3" |
5280 | _debug "_renewServer" "$_renewServer" | |
5281 | ||
e799ef29 | 5282 | _initpath "$Le_Domain" "$_isEcc" |
38778f8a | 5283 | |
1566656a | 5284 | _set_level=${NOTIFY_LEVEL:-$NOTIFY_LEVEL_DEFAULT} |
e2053b22 | 5285 | _info "$(__green "Renew: '$Le_Domain'")" |
4c2a3841 | 5286 | if [ ! -f "$DOMAIN_CONF" ]; then |
2e87e64b | 5287 | _info "'$Le_Domain' is not an issued domain, skip." |
acae0ac2 | 5288 | return $RENEW_SKIP |
4c3b3608 | 5289 | fi |
4c2a3841 | 5290 | |
5291 | if [ "$Le_RenewalDays" ]; then | |
1e6b68f5 | 5292 | _savedomainconf Le_RenewalDays "$Le_RenewalDays" |
5293 | fi | |
5294 | ||
8663fb7e | 5295 | . "$DOMAIN_CONF" |
c5f1cca3 | 5296 | _debug Le_API "$Le_API" |
f2add8de | 5297 | |
14b59142 | 5298 | case "$Le_API" in |
5299 | "$CA_LETSENCRYPT_V2_TEST") | |
5300 | _info "Switching back to $CA_LETSENCRYPT_V2" | |
5301 | Le_API="$CA_LETSENCRYPT_V2" | |
5302 | ;; | |
5303 | "$CA_BUYPASS_TEST") | |
5304 | _info "Switching back to $CA_BUYPASS" | |
5305 | Le_API="$CA_BUYPASS" | |
5306 | ;; | |
5307 | "$CA_GOOGLE_TEST") | |
5308 | _info "Switching back to $CA_GOOGLE" | |
5309 | Le_API="$CA_GOOGLE" | |
5310 | ;; | |
5311 | esac | |
5312 | ||
38778f8a | 5313 | if [ "$_server" ]; then |
5314 | Le_API="$_server" | |
5c48e139 | 5315 | fi |
38778f8a | 5316 | _info "Renew to Le_API=$Le_API" |
5317 | ||
38778f8a | 5318 | _clearAPI |
5319 | _clearCA | |
e1d7a6b9 | 5320 | export ACME_DIRECTORY="$Le_API" |
38778f8a | 5321 | |
5322 | #reload ca configs | |
38778f8a | 5323 | _debug2 "initpath again." |
5324 | _initpath "$Le_Domain" "$_isEcc" | |
4c2a3841 | 5325 | |
5326 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then | |
e2053b22 | 5327 | _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")" |
5328 | _info "Add '$(__red '--force')' to force to renew." | |
1566656a | 5329 | if [ -z "$_ACME_IN_RENEWALL" ]; then |
5330 | if [ $_set_level -ge $NOTIFY_LEVEL_SKIP ]; then | |
5331 | _send_notify "Renew $Le_Domain skipped" "Good, the cert is skipped." "$NOTIFY_HOOK" "$RENEW_SKIP" | |
5332 | fi | |
5333 | fi | |
e799ef29 | 5334 | return "$RENEW_SKIP" |
4c3b3608 | 5335 | fi |
4c2a3841 | 5336 | |
bd04638d | 5337 | if [ "$_ACME_IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then |
c4d0aec5 | 5338 | _info "Skip invalid cert for: $Le_Domain" |
acae0ac2 | 5339 | return $RENEW_SKIP |
c4d0aec5 | 5340 | fi |
5341 | ||
bd04638d | 5342 | _ACME_IS_RENEW="1" |
7690f73e | 5343 | Le_ReloadCmd="$(_readdomainconf Le_ReloadCmd)" |
c7257bcf | 5344 | Le_PreHook="$(_readdomainconf Le_PreHook)" |
5345 | Le_PostHook="$(_readdomainconf Le_PostHook)" | |
5346 | Le_RenewHook="$(_readdomainconf Le_RenewHook)" | |
b7b01999 | 5347 | Le_Preferred_Chain="$(_readdomainconf Le_Preferred_Chain)" |
b376dfa1 CB |
5348 | # When renewing from an old version, the empty Le_Keylength means 2048. |
5349 | # Note, do not use DEFAULT_DOMAIN_KEY_LENGTH as that value may change over | |
5350 | # time but an empty value implies 2048 specifically. | |
5351 | Le_Keylength="$(_readdomainconf Le_Keylength)" | |
64847afc | 5352 | if [ -z "$Le_Keylength" ]; then |
5353 | Le_Keylength=2048 | |
5354 | fi | |
225adcc8 | 5355 | issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath" "$Le_PreHook" "$Le_PostHook" "$Le_RenewHook" "$Le_LocalAddress" "$Le_ChallengeAlias" "$Le_Preferred_Chain" "$Le_Valid_From" "$Le_Valid_To" |
e799ef29 | 5356 | res="$?" |
4c2a3841 | 5357 | if [ "$res" != "0" ]; then |
e799ef29 | 5358 | return "$res" |
a61fe418 | 5359 | fi |
4c2a3841 | 5360 | |
5361 | if [ "$Le_DeployHook" ]; then | |
93bce1b2 | 5362 | _deploy "$Le_Domain" "$Le_DeployHook" |
e799ef29 | 5363 | res="$?" |
a61fe418 | 5364 | fi |
4c2a3841 | 5365 | |
bd04638d | 5366 | _ACME_IS_RENEW="" |
1566656a | 5367 | if [ -z "$_ACME_IN_RENEWALL" ]; then |
5368 | if [ "$res" = "0" ]; then | |
5369 | if [ $_set_level -ge $NOTIFY_LEVEL_RENEW ]; then | |
5370 | _send_notify "Renew $d success" "Good, the cert is renewed." "$NOTIFY_HOOK" 0 | |
5371 | fi | |
5372 | else | |
5373 | if [ $_set_level -ge $NOTIFY_LEVEL_ERROR ]; then | |
5374 | _send_notify "Renew $d error" "There is an error." "$NOTIFY_HOOK" 1 | |
5375 | fi | |
5376 | fi | |
5377 | fi | |
4c3b3608 | 5378 | |
e799ef29 | 5379 | return "$res" |
4c3b3608 | 5380 | } |
5381 | ||
8b7a86bd | 5382 | #renewAll [stopRenewOnError] [server] |
4c3b3608 | 5383 | renewAll() { |
5384 | _initpath | |
8d783e8e | 5385 | _clearCA |
cc179731 | 5386 | _stopRenewOnError="$1" |
5387 | _debug "_stopRenewOnError" "$_stopRenewOnError" | |
8b7a86bd | 5388 | |
5389 | _server="$2" | |
5390 | _debug "_server" "$_server" | |
5391 | ||
cc179731 | 5392 | _ret="0" |
b50e701c | 5393 | _success_msg="" |
5394 | _error_msg="" | |
5395 | _skipped_msg="" | |
c6b68551 | 5396 | _error_level=$NOTIFY_LEVEL_SKIP |
5397 | _notify_code=$RENEW_SKIP | |
f803c6c0 | 5398 | _set_level=${NOTIFY_LEVEL:-$NOTIFY_LEVEL_DEFAULT} |
5399 | _debug "_set_level" "$_set_level" | |
1566656a | 5400 | export _ACME_IN_RENEWALL=1 |
e591d5cf | 5401 | for di in "${CERT_HOME}"/*.*/; do |
5402 | _debug di "$di" | |
44483dba | 5403 | if ! [ -d "$di" ]; then |
2e87e64b | 5404 | _debug "Not a directory, skip: $di" |
3498a585 | 5405 | continue |
5406 | fi | |
e591d5cf | 5407 | d=$(basename "$di") |
201aa244 | 5408 | _debug d "$d" |
43822d37 | 5409 | ( |
201aa244 | 5410 | if _endswith "$d" "$ECC_SUFFIX"; then |
5411 | _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2) | |
5412 | d=$(echo "$d" | cut -d "$ECC_SEP" -f 1) | |
43822d37 | 5413 | fi |
8b7a86bd | 5414 | renew "$d" "$_isEcc" "$_server" |
4d2f38b0 | 5415 | ) |
cc179731 | 5416 | rc="$?" |
5417 | _debug "Return code: $rc" | |
c6b68551 | 5418 | if [ "$rc" = "0" ]; then |
5419 | if [ $_error_level -gt $NOTIFY_LEVEL_RENEW ]; then | |
5420 | _error_level="$NOTIFY_LEVEL_RENEW" | |
5421 | _notify_code=0 | |
5422 | fi | |
737eba57 | 5423 | |
5424 | if [ $_set_level -ge $NOTIFY_LEVEL_RENEW ]; then | |
5425 | if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then | |
5426 | _send_notify "Renew $d success" "Good, the cert is renewed." "$NOTIFY_HOOK" 0 | |
c6b68551 | 5427 | fi |
5428 | fi | |
737eba57 | 5429 | |
c6b68551 | 5430 | _success_msg="${_success_msg} $d |
b50e701c | 5431 | " |
c6b68551 | 5432 | elif [ "$rc" = "$RENEW_SKIP" ]; then |
5433 | if [ $_error_level -gt $NOTIFY_LEVEL_SKIP ]; then | |
5434 | _error_level="$NOTIFY_LEVEL_SKIP" | |
5435 | _notify_code=$RENEW_SKIP | |
5436 | fi | |
737eba57 | 5437 | |
5438 | if [ $_set_level -ge $NOTIFY_LEVEL_SKIP ]; then | |
5439 | if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then | |
5440 | _send_notify "Renew $d skipped" "Good, the cert is skipped." "$NOTIFY_HOOK" "$RENEW_SKIP" | |
b50e701c | 5441 | fi |
cc179731 | 5442 | fi |
737eba57 | 5443 | |
c6b68551 | 5444 | _info "Skipped $d" |
5445 | _skipped_msg="${_skipped_msg} $d | |
5446 | " | |
b50e701c | 5447 | else |
c6b68551 | 5448 | if [ $_error_level -gt $NOTIFY_LEVEL_ERROR ]; then |
5449 | _error_level="$NOTIFY_LEVEL_ERROR" | |
5450 | _notify_code=1 | |
5451 | fi | |
737eba57 | 5452 | |
5453 | if [ $_set_level -ge $NOTIFY_LEVEL_ERROR ]; then | |
5454 | if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then | |
5455 | _send_notify "Renew $d error" "There is an error." "$NOTIFY_HOOK" 1 | |
c6b68551 | 5456 | fi |
5457 | fi | |
737eba57 | 5458 | |
c6b68551 | 5459 | _error_msg="${_error_msg} $d |
b50e701c | 5460 | " |
c6b68551 | 5461 | if [ "$_stopRenewOnError" ]; then |
5462 | _err "Error renew $d, stop now." | |
5463 | _ret="$rc" | |
5464 | break | |
5465 | else | |
5466 | _ret="$rc" | |
5467 | _err "Error renew $d." | |
5468 | fi | |
cc179731 | 5469 | fi |
4c3b3608 | 5470 | done |
c6b68551 | 5471 | _debug _error_level "$_error_level" |
a2738e85 | 5472 | _debug _set_level "$_set_level" |
737eba57 | 5473 | if [ $_error_level -le $_set_level ]; then |
b50e701c | 5474 | if [ -z "$NOTIFY_MODE" ] || [ "$NOTIFY_MODE" = "$NOTIFY_MODE_BULK" ]; then |
5475 | _msg_subject="Renew" | |
5476 | if [ "$_error_msg" ]; then | |
5477 | _msg_subject="${_msg_subject} Error" | |
c6b68551 | 5478 | _msg_data="Error certs: |
5479 | ${_error_msg} | |
5480 | " | |
b50e701c | 5481 | fi |
5482 | if [ "$_success_msg" ]; then | |
5483 | _msg_subject="${_msg_subject} Success" | |
c6b68551 | 5484 | _msg_data="${_msg_data}Success certs: |
5485 | ${_success_msg} | |
5486 | " | |
b50e701c | 5487 | fi |
5488 | if [ "$_skipped_msg" ]; then | |
5489 | _msg_subject="${_msg_subject} Skipped" | |
c6b68551 | 5490 | _msg_data="${_msg_data}Skipped certs: |
5491 | ${_skipped_msg} | |
b50e701c | 5492 | " |
c6b68551 | 5493 | fi |
5494 | ||
5495 | _send_notify "$_msg_subject" "$_msg_data" "$NOTIFY_HOOK" "$_notify_code" | |
b50e701c | 5496 | fi |
5497 | fi | |
5498 | ||
201aa244 | 5499 | return "$_ret" |
4c3b3608 | 5500 | } |
5501 | ||
10afcaca | 5502 | #csr webroot |
4c2a3841 | 5503 | signcsr() { |
10afcaca | 5504 | _csrfile="$1" |
5505 | _csrW="$2" | |
5506 | if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then | |
2e87e64b | 5507 | _usage "Usage: $PROJECT_ENTRY --sign-csr --csr <csr-file> --webroot <directory>" |
10afcaca | 5508 | return 1 |
5509 | fi | |
5510 | ||
875625b1 | 5511 | _real_cert="$3" |
5512 | _real_key="$4" | |
5513 | _real_ca="$5" | |
5514 | _reload_cmd="$6" | |
5515 | _real_fullchain="$7" | |
5516 | _pre_hook="${8}" | |
5517 | _post_hook="${9}" | |
5518 | _renew_hook="${10}" | |
5519 | _local_addr="${11}" | |
5520 | _challenge_alias="${12}" | |
96a95ba9 | 5521 | _preferred_chain="${13}" |
875625b1 | 5522 | |
10afcaca | 5523 | _csrsubj=$(_readSubjectFromCSR "$_csrfile") |
4c2a3841 | 5524 | if [ "$?" != "0" ]; then |
10afcaca | 5525 | _err "Can not read subject from csr: $_csrfile" |
5526 | return 1 | |
5527 | fi | |
ad752b31 | 5528 | _debug _csrsubj "$_csrsubj" |
2c9ed4c5 | 5529 | if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.'; then |
5530 | _info "It seems that the subject: $_csrsubj is not a valid domain name. Drop it." | |
5531 | _csrsubj="" | |
5532 | fi | |
10afcaca | 5533 | |
5534 | _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile") | |
4c2a3841 | 5535 | if [ "$?" != "0" ]; then |
10afcaca | 5536 | _err "Can not read domain list from csr: $_csrfile" |
5537 | return 1 | |
5538 | fi | |
5539 | _debug "_csrdomainlist" "$_csrdomainlist" | |
4c2a3841 | 5540 | |
5541 | if [ -z "$_csrsubj" ]; then | |
ad752b31 | 5542 | _csrsubj="$(_getfield "$_csrdomainlist" 1)" |
5543 | _debug _csrsubj "$_csrsubj" | |
5544 | _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)" | |
5545 | _debug "_csrdomainlist" "$_csrdomainlist" | |
5546 | fi | |
4c2a3841 | 5547 | |
5548 | if [ -z "$_csrsubj" ]; then | |
ad752b31 | 5549 | _err "Can not read subject from csr: $_csrfile" |
5550 | return 1 | |
5551 | fi | |
4c2a3841 | 5552 | |
10afcaca | 5553 | _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile") |
4c2a3841 | 5554 | if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then |
10afcaca | 5555 | _err "Can not read key length from csr: $_csrfile" |
5556 | return 1 | |
5557 | fi | |
4c2a3841 | 5558 | |
10afcaca | 5559 | _initpath "$_csrsubj" "$_csrkeylength" |
5560 | mkdir -p "$DOMAIN_PATH" | |
4c2a3841 | 5561 | |
10afcaca | 5562 | _info "Copy csr to: $CSR_PATH" |
5563 | cp "$_csrfile" "$CSR_PATH" | |
4c2a3841 | 5564 | |
96a95ba9 | 5565 | issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength" "$_real_cert" "$_real_key" "$_real_ca" "$_reload_cmd" "$_real_fullchain" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_addr" "$_challenge_alias" "$_preferred_chain" |
4c2a3841 | 5566 | |
10afcaca | 5567 | } |
5568 | ||
5569 | showcsr() { | |
4c2a3841 | 5570 | _csrfile="$1" |
10afcaca | 5571 | _csrd="$2" |
5572 | if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then | |
2e87e64b | 5573 | _usage "Usage: $PROJECT_ENTRY --show-csr --csr <csr-file>" |
10afcaca | 5574 | return 1 |
5575 | fi | |
5576 | ||
5577 | _initpath | |
4c2a3841 | 5578 | |
10afcaca | 5579 | _csrsubj=$(_readSubjectFromCSR "$_csrfile") |
4d89ce5d | 5580 | if [ "$?" != "0" ]; then |
10afcaca | 5581 | _err "Can not read subject from csr: $_csrfile" |
5582 | return 1 | |
5583 | fi | |
4d89ce5d | 5584 | if [ -z "$_csrsubj" ]; then |
5585 | _info "The Subject is empty" | |
5586 | fi | |
4c2a3841 | 5587 | |
10afcaca | 5588 | _info "Subject=$_csrsubj" |
5589 | ||
5590 | _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile") | |
4c2a3841 | 5591 | if [ "$?" != "0" ]; then |
10afcaca | 5592 | _err "Can not read domain list from csr: $_csrfile" |
5593 | return 1 | |
5594 | fi | |
5595 | _debug "_csrdomainlist" "$_csrdomainlist" | |
5596 | ||
5597 | _info "SubjectAltNames=$_csrdomainlist" | |
5598 | ||
10afcaca | 5599 | _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile") |
4c2a3841 | 5600 | if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then |
10afcaca | 5601 | _err "Can not read key length from csr: $_csrfile" |
5602 | return 1 | |
5603 | fi | |
5604 | _info "KeyLength=$_csrkeylength" | |
5605 | } | |
5606 | ||
95ef046d | 5607 | #listraw domain |
6d7eda3e | 5608 | list() { |
22ea4004 | 5609 | _raw="$1" |
95ef046d | 5610 | _domain="$2" |
6d7eda3e | 5611 | _initpath |
4c2a3841 | 5612 | |
dcf4f8f6 | 5613 | _sep="|" |
4c2a3841 | 5614 | if [ "$_raw" ]; then |
95ef046d | 5615 | if [ -z "$_domain" ]; then |
5616 | printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}CA${_sep}Created${_sep}Renew" | |
5617 | fi | |
e591d5cf | 5618 | for di in "${CERT_HOME}"/*.*/; do |
5619 | d=$(basename "$di") | |
201aa244 | 5620 | _debug d "$d" |
dcf4f8f6 | 5621 | ( |
201aa244 | 5622 | if _endswith "$d" "$ECC_SUFFIX"; then |
be0df07d | 5623 | _isEcc="ecc" |
201aa244 | 5624 | d=$(echo "$d" | cut -d "$ECC_SEP" -f 1) |
43822d37 | 5625 | fi |
be0df07d | 5626 | DOMAIN_CONF="$di/$d.conf" |
4c2a3841 | 5627 | if [ -f "$DOMAIN_CONF" ]; then |
dcf4f8f6 | 5628 | . "$DOMAIN_CONF" |
269847d1 | 5629 | _ca="$(_getCAShortName "$Le_API")" |
95ef046d | 5630 | if [ -z "$_domain" ]; then |
5631 | printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$_ca${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr" | |
5632 | else | |
5633 | if [ "$_domain" = "$d" ]; then | |
5634 | cat "$DOMAIN_CONF" | |
5635 | fi | |
5636 | fi | |
dcf4f8f6 | 5637 | fi |
5638 | ) | |
5639 | done | |
5640 | else | |
4c2a3841 | 5641 | if _exists column; then |
95ef046d | 5642 | list "raw" "$_domain" | column -t -s "$_sep" |
22ea4004 | 5643 | else |
95ef046d | 5644 | list "raw" "$_domain" | tr "$_sep" '\t' |
22ea4004 | 5645 | fi |
dcf4f8f6 | 5646 | fi |
6d7eda3e | 5647 | |
6d7eda3e | 5648 | } |
5649 | ||
93bce1b2 | 5650 | _deploy() { |
5651 | _d="$1" | |
5652 | _hooks="$2" | |
5653 | ||
5654 | for _d_api in $(echo "$_hooks" | tr ',' " "); do | |
b50e701c | 5655 | _deployApi="$(_findHook "$_d" $_SUB_FOLDER_DEPLOY "$_d_api")" |
93bce1b2 | 5656 | if [ -z "$_deployApi" ]; then |
5657 | _err "The deploy hook $_d_api is not found." | |
5658 | return 1 | |
5659 | fi | |
5660 | _debug _deployApi "$_deployApi" | |
5661 | ||
5662 | if ! ( | |
5663 | if ! . "$_deployApi"; then | |
5664 | _err "Load file $_deployApi error. Please check your api file and try again." | |
5665 | return 1 | |
5666 | fi | |
5667 | ||
5668 | d_command="${_d_api}_deploy" | |
5669 | if ! _exists "$d_command"; then | |
5670 | _err "It seems that your api file is not correct, it must have a function named: $d_command" | |
5671 | return 1 | |
5672 | fi | |
5673 | ||
5674 | if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then | |
5675 | _err "Error deploy for domain:$_d" | |
5676 | return 1 | |
5677 | fi | |
5678 | ); then | |
5679 | _err "Deploy error." | |
5680 | return 1 | |
5681 | else | |
5682 | _info "$(__green Success)" | |
5683 | fi | |
5684 | done | |
5685 | } | |
5686 | ||
5687 | #domain hooks | |
a61fe418 | 5688 | deploy() { |
93bce1b2 | 5689 | _d="$1" |
5690 | _hooks="$2" | |
a61fe418 | 5691 | _isEcc="$3" |
93bce1b2 | 5692 | if [ -z "$_hooks" ]; then |
2e87e64b | 5693 | _usage "Usage: $PROJECT_ENTRY --deploy --domain <domain.tld> --deploy-hook <hookname> [--ecc] " |
a61fe418 | 5694 | return 1 |
5695 | fi | |
5696 | ||
93bce1b2 | 5697 | _initpath "$_d" "$_isEcc" |
4c2a3841 | 5698 | if [ ! -d "$DOMAIN_PATH" ]; then |
9672c6b8 | 5699 | _err "The domain '$_d' is not a cert name. You must use the cert name to specify the cert to install." |
5700 | _err "Can not find path:'$DOMAIN_PATH'" | |
a61fe418 | 5701 | return 1 |
5702 | fi | |
4c2a3841 | 5703 | |
93bce1b2 | 5704 | . "$DOMAIN_CONF" |
4c2a3841 | 5705 | |
93bce1b2 | 5706 | _savedomainconf Le_DeployHook "$_hooks" |
4c2a3841 | 5707 | |
93bce1b2 | 5708 | _deploy "$_d" "$_hooks" |
a61fe418 | 5709 | } |
5710 | ||
4c3b3608 | 5711 | installcert() { |
85e1f4ea | 5712 | _main_domain="$1" |
5713 | if [ -z "$_main_domain" ]; then | |
2e87e64b | 5714 | _usage "Usage: $PROJECT_ENTRY --install-cert --domain <domain.tld> [--ecc] [--cert-file <file>] [--key-file <file>] [--ca-file <file>] [ --reloadcmd <command>] [--fullchain-file <file>]" |
4c3b3608 | 5715 | return 1 |
5716 | fi | |
5717 | ||
85e1f4ea | 5718 | _real_cert="$2" |
5719 | _real_key="$3" | |
5720 | _real_ca="$4" | |
5721 | _reload_cmd="$5" | |
5722 | _real_fullchain="$6" | |
43822d37 | 5723 | _isEcc="$7" |
5724 | ||
85e1f4ea | 5725 | _initpath "$_main_domain" "$_isEcc" |
4c2a3841 | 5726 | if [ ! -d "$DOMAIN_PATH" ]; then |
9672c6b8 | 5727 | _err "The domain '$_main_domain' is not a cert name. You must use the cert name to specify the cert to install." |
5728 | _err "Can not find path:'$DOMAIN_PATH'" | |
43822d37 | 5729 | return 1 |
5730 | fi | |
5731 | ||
85e1f4ea | 5732 | _savedomainconf "Le_RealCertPath" "$_real_cert" |
5733 | _savedomainconf "Le_RealCACertPath" "$_real_ca" | |
5734 | _savedomainconf "Le_RealKeyPath" "$_real_key" | |
7690f73e | 5735 | _savedomainconf "Le_ReloadCmd" "$_reload_cmd" "base64" |
85e1f4ea | 5736 | _savedomainconf "Le_RealFullChainPath" "$_real_fullchain" |
5737 | ||
044da37c | 5738 | _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd" |
43822d37 | 5739 | } |
4c3b3608 | 5740 | |
044da37c | 5741 | #domain cert key ca fullchain reloadcmd backup-prefix |
43822d37 | 5742 | _installcert() { |
85e1f4ea | 5743 | _main_domain="$1" |
5744 | _real_cert="$2" | |
5745 | _real_key="$3" | |
5746 | _real_ca="$4" | |
044da37c | 5747 | _real_fullchain="$5" |
5748 | _reload_cmd="$6" | |
5749 | _backup_prefix="$7" | |
4c3b3608 | 5750 | |
85e1f4ea | 5751 | if [ "$_real_cert" = "$NO_VALUE" ]; then |
5752 | _real_cert="" | |
4d2f38b0 | 5753 | fi |
85e1f4ea | 5754 | if [ "$_real_key" = "$NO_VALUE" ]; then |
5755 | _real_key="" | |
4d2f38b0 | 5756 | fi |
85e1f4ea | 5757 | if [ "$_real_ca" = "$NO_VALUE" ]; then |
5758 | _real_ca="" | |
4d2f38b0 | 5759 | fi |
85e1f4ea | 5760 | if [ "$_reload_cmd" = "$NO_VALUE" ]; then |
5761 | _reload_cmd="" | |
4d2f38b0 | 5762 | fi |
85e1f4ea | 5763 | if [ "$_real_fullchain" = "$NO_VALUE" ]; then |
5764 | _real_fullchain="" | |
4d2f38b0 | 5765 | fi |
4c2a3841 | 5766 | |
044da37c | 5767 | _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix" |
5768 | mkdir -p "$_backup_path" | |
5769 | ||
85e1f4ea | 5770 | if [ "$_real_cert" ]; then |
6b97dc67 | 5771 | _info "Installing cert to: $_real_cert" |
bd04638d | 5772 | if [ -f "$_real_cert" ] && [ ! "$_ACME_IS_RENEW" ]; then |
044da37c | 5773 | cp "$_real_cert" "$_backup_path/cert.bak" |
4c3b3608 | 5774 | fi |
206be3c1 | 5775 | cat "$CERT_PATH" >"$_real_cert" || return 1 |
4c3b3608 | 5776 | fi |
4c2a3841 | 5777 | |
85e1f4ea | 5778 | if [ "$_real_ca" ]; then |
6b97dc67 | 5779 | _info "Installing CA to: $_real_ca" |
85e1f4ea | 5780 | if [ "$_real_ca" = "$_real_cert" ]; then |
5781 | echo "" >>"$_real_ca" | |
206be3c1 | 5782 | cat "$CA_CERT_PATH" >>"$_real_ca" || return 1 |
4c3b3608 | 5783 | else |
bd04638d | 5784 | if [ -f "$_real_ca" ] && [ ! "$_ACME_IS_RENEW" ]; then |
044da37c | 5785 | cp "$_real_ca" "$_backup_path/ca.bak" |
78552b18 | 5786 | fi |
206be3c1 | 5787 | cat "$CA_CERT_PATH" >"$_real_ca" || return 1 |
4c3b3608 | 5788 | fi |
5789 | fi | |
5790 | ||
85e1f4ea | 5791 | if [ "$_real_key" ]; then |
6b97dc67 | 5792 | _info "Installing key to: $_real_key" |
bd04638d | 5793 | if [ -f "$_real_key" ] && [ ! "$_ACME_IS_RENEW" ]; then |
044da37c | 5794 | cp "$_real_key" "$_backup_path/key.bak" |
4c3b3608 | 5795 | fi |
82014583 | 5796 | if [ -f "$_real_key" ]; then |
206be3c1 | 5797 | cat "$CERT_KEY_PATH" >"$_real_key" || return 1 |
82014583 | 5798 | else |
af193291 | 5799 | touch "$_real_key" || return 1 |
7b92371a | 5800 | chmod 600 "$_real_key" |
af193291 | 5801 | cat "$CERT_KEY_PATH" >"$_real_key" || return 1 |
82014583 | 5802 | fi |
4c3b3608 | 5803 | fi |
4c2a3841 | 5804 | |
85e1f4ea | 5805 | if [ "$_real_fullchain" ]; then |
6b97dc67 | 5806 | _info "Installing full chain to: $_real_fullchain" |
bd04638d | 5807 | if [ -f "$_real_fullchain" ] && [ ! "$_ACME_IS_RENEW" ]; then |
044da37c | 5808 | cp "$_real_fullchain" "$_backup_path/fullchain.bak" |
a63b05a9 | 5809 | fi |
206be3c1 | 5810 | cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain" || return 1 |
4c2a3841 | 5811 | fi |
4c3b3608 | 5812 | |
85e1f4ea | 5813 | if [ "$_reload_cmd" ]; then |
5814 | _info "Run reload cmd: $_reload_cmd" | |
25555b8c | 5815 | if ( |
839bf0e2 | 5816 | export CERT_PATH |
5817 | export CERT_KEY_PATH | |
5818 | export CA_CERT_PATH | |
5819 | export CERT_FULLCHAIN_PATH | |
b3f61297 | 5820 | export Le_Domain="$_main_domain" |
85e1f4ea | 5821 | cd "$DOMAIN_PATH" && eval "$_reload_cmd" |
839bf0e2 | 5822 | ); then |
43822d37 | 5823 | _info "$(__green "Reload success")" |
4d2f38b0 | 5824 | else |
5825 | _err "Reload error for :$Le_Domain" | |
5826 | fi | |
5827 | fi | |
5828 | ||
4c3b3608 | 5829 | } |
5830 | ||
77f96b38 | 5831 | __read_password() { |
5832 | unset _pp | |
5833 | prompt="Enter Password:" | |
0b04a7f1 | 5834 | while IFS= read -p "$prompt" -r -s -n 1 char; do |
5835 | if [ "$char" = $'\0' ]; then | |
4ebad105 | 5836 | break |
0b04a7f1 | 5837 | fi |
5838 | prompt='*' | |
5839 | _pp="$_pp$char" | |
77f96b38 | 5840 | done |
5841 | echo "$_pp" | |
5842 | } | |
5843 | ||
5844 | _install_win_taskscheduler() { | |
5845 | _lesh="$1" | |
5846 | _centry="$2" | |
5847 | _randomminute="$3" | |
5848 | if ! _exists cygpath; then | |
5849 | _err "cygpath not found" | |
5850 | return 1 | |
5851 | fi | |
5852 | if ! _exists schtasks; then | |
5853 | _err "schtasks.exe is not found, are you on Windows?" | |
5854 | return 1 | |
5855 | fi | |
5856 | _winbash="$(cygpath -w $(which bash))" | |
5857 | _debug _winbash "$_winbash" | |
5858 | if [ -z "$_winbash" ]; then | |
5859 | _err "can not find bash path" | |
5860 | return 1 | |
5861 | fi | |
5862 | _myname="$(whoami)" | |
5863 | _debug "_myname" "$_myname" | |
5864 | if [ -z "$_myname" ]; then | |
5865 | _err "can not find my user name" | |
5866 | return 1 | |
5867 | fi | |
5868 | _debug "_lesh" "$_lesh" | |
5869 | ||
5870 | _info "To install scheduler task in your Windows account, you must input your windows password." | |
5871 | _info "$PROJECT_NAME doesn't save your password." | |
5872 | _info "Please input your Windows password for: $(__green "$_myname")" | |
5873 | _password="$(__read_password)" | |
5874 | #SCHTASKS.exe '/create' '/SC' 'DAILY' '/TN' "$_WINDOWS_SCHEDULER_NAME" '/F' '/ST' "00:$_randomminute" '/RU' "$_myname" '/RP' "$_password" '/TR' "$_winbash -l -c '$_lesh --cron --home \"$LE_WORKING_DIR\" $_centry'" >/dev/null | |
5875 | echo SCHTASKS.exe '/create' '/SC' 'DAILY' '/TN' "$_WINDOWS_SCHEDULER_NAME" '/F' '/ST' "00:$_randomminute" '/RU' "$_myname" '/RP' "$_password" '/TR' "\"$_winbash -l -c '$_lesh --cron --home \"$LE_WORKING_DIR\" $_centry'\"" | cmd.exe >/dev/null | |
5876 | echo | |
5877 | ||
5878 | } | |
5879 | ||
5880 | _uninstall_win_taskscheduler() { | |
5881 | if ! _exists schtasks; then | |
5882 | _err "schtasks.exe is not found, are you on Windows?" | |
5883 | return 1 | |
5884 | fi | |
5885 | if ! echo SCHTASKS /query /tn "$_WINDOWS_SCHEDULER_NAME" | cmd.exe >/dev/null; then | |
5886 | _debug "scheduler $_WINDOWS_SCHEDULER_NAME is not found." | |
5887 | else | |
5888 | _info "Removing $_WINDOWS_SCHEDULER_NAME" | |
0b04a7f1 | 5889 | echo SCHTASKS /delete /f /tn "$_WINDOWS_SCHEDULER_NAME" | cmd.exe >/dev/null |
77f96b38 | 5890 | fi |
5891 | } | |
5892 | ||
27dbe77f | 5893 | #confighome |
4c3b3608 | 5894 | installcronjob() { |
27dbe77f | 5895 | _c_home="$1" |
4c3b3608 | 5896 | _initpath |
415f375c | 5897 | _CRONTAB="crontab" |
77f96b38 | 5898 | if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then |
5899 | lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY" | |
5900 | else | |
f63409ee | 5901 | _debug "_SCRIPT_" "$_SCRIPT_" |
5902 | _script="$(_readlink "$_SCRIPT_")" | |
5903 | _debug _script "$_script" | |
5904 | if [ -f "$_script" ]; then | |
5905 | _info "Using the current script from: $_script" | |
5906 | lesh="$_script" | |
5907 | else | |
5908 | _err "Can not install cronjob, $PROJECT_ENTRY not found." | |
5909 | return 1 | |
5910 | fi | |
77f96b38 | 5911 | fi |
5912 | if [ "$_c_home" ]; then | |
5913 | _c_entry="--config-home \"$_c_home\" " | |
5914 | fi | |
5915 | _t=$(_time) | |
5916 | random_minute=$(_math $_t % 60) | |
5917 | ||
415f375c | 5918 | if ! _exists "$_CRONTAB" && _exists "fcrontab"; then |
5919 | _CRONTAB="fcrontab" | |
5920 | fi | |
77f96b38 | 5921 | |
415f375c | 5922 | if ! _exists "$_CRONTAB"; then |
77f96b38 | 5923 | if _exists cygpath && _exists schtasks.exe; then |
5924 | _info "It seems you are on Windows, let's install Windows scheduler task." | |
5925 | if _install_win_taskscheduler "$lesh" "$_c_entry" "$random_minute"; then | |
5926 | _info "Install Windows scheduler task success." | |
5927 | return 0 | |
5928 | else | |
5929 | _err "Install Windows scheduler task failed." | |
5930 | return 1 | |
5931 | fi | |
5932 | fi | |
415f375c | 5933 | _err "crontab/fcrontab doesn't exist, so, we can not install cron jobs." |
77546ea5 | 5934 | _err "All your certs will not be renewed automatically." |
a7b7355d | 5935 | _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday." |
77546ea5 | 5936 | return 1 |
5937 | fi | |
4c3b3608 | 5938 | _info "Installing cron job" |
415f375c | 5939 | if ! $_CRONTAB -l | grep "$PROJECT_ENTRY --cron"; then |
e2c939fb | 5940 | if _exists uname && uname -a | grep SunOS >/dev/null; then |
415f375c | 5941 | $_CRONTAB -l | { |
4c2a3841 | 5942 | cat |
0533bde9 | 5943 | echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null" |
415f375c | 5944 | } | $_CRONTAB -- |
22ea4004 | 5945 | else |
415f375c | 5946 | $_CRONTAB -l | { |
4c2a3841 | 5947 | cat |
0533bde9 | 5948 | echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null" |
415f375c | 5949 | } | $_CRONTAB - |
22ea4004 | 5950 | fi |
4c3b3608 | 5951 | fi |
4c2a3841 | 5952 | if [ "$?" != "0" ]; then |
4c3b3608 | 5953 | _err "Install cron job failed. You need to manually renew your certs." |
5954 | _err "Or you can add cronjob by yourself:" | |
a7b7355d | 5955 | _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null" |
4c3b3608 | 5956 | return 1 |
5957 | fi | |
5958 | } | |
5959 | ||
5960 | uninstallcronjob() { | |
415f375c | 5961 | _CRONTAB="crontab" |
5962 | if ! _exists "$_CRONTAB" && _exists "fcrontab"; then | |
5963 | _CRONTAB="fcrontab" | |
5964 | fi | |
5965 | ||
5966 | if ! _exists "$_CRONTAB"; then | |
77f96b38 | 5967 | if _exists cygpath && _exists schtasks.exe; then |
5968 | _info "It seems you are on Windows, let's uninstall Windows scheduler task." | |
5969 | if _uninstall_win_taskscheduler; then | |
5970 | _info "Uninstall Windows scheduler task success." | |
5971 | return 0 | |
5972 | else | |
5973 | _err "Uninstall Windows scheduler task failed." | |
5974 | return 1 | |
5975 | fi | |
5976 | fi | |
37db5b81 | 5977 | return |
5978 | fi | |
4c3b3608 | 5979 | _info "Removing cron job" |
415f375c | 5980 | cr="$($_CRONTAB -l | grep "$PROJECT_ENTRY --cron")" |
4c2a3841 | 5981 | if [ "$cr" ]; then |
6bdf689d | 5982 | if _exists uname && uname -a | grep SunOS >/dev/null; then |
415f375c | 5983 | $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB -- |
22ea4004 | 5984 | else |
415f375c | 5985 | $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB - |
22ea4004 | 5986 | fi |
a7b7355d | 5987 | LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')" |
4c3b3608 | 5988 | _info LE_WORKING_DIR "$LE_WORKING_DIR" |
27dbe77f | 5989 | if _contains "$cr" "--config-home"; then |
f5b546b3 | 5990 | LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')" |
5991 | _debug LE_CONFIG_HOME "$LE_CONFIG_HOME" | |
27dbe77f | 5992 | fi |
4c2a3841 | 5993 | fi |
4c3b3608 | 5994 | _initpath |
a7b7355d | 5995 | |
4c3b3608 | 5996 | } |
5997 | ||
1041c9f9 | 5998 | #domain isECC revokeReason |
6cb415f5 | 5999 | revoke() { |
6000 | Le_Domain="$1" | |
4c2a3841 | 6001 | if [ -z "$Le_Domain" ]; then |
2e87e64b | 6002 | _usage "Usage: $PROJECT_ENTRY --revoke --domain <domain.tld> [--ecc]" |
6cb415f5 | 6003 | return 1 |
6004 | fi | |
4c2a3841 | 6005 | |
43822d37 | 6006 | _isEcc="$2" |
1041c9f9 | 6007 | _reason="$3" |
6008 | if [ -z "$_reason" ]; then | |
6009 | _reason="0" | |
6010 | fi | |
c4a375b3 | 6011 | _initpath "$Le_Domain" "$_isEcc" |
4c2a3841 | 6012 | if [ ! -f "$DOMAIN_CONF" ]; then |
6cb415f5 | 6013 | _err "$Le_Domain is not a issued domain, skip." |
4c2a3841 | 6014 | return 1 |
6cb415f5 | 6015 | fi |
4c2a3841 | 6016 | |
6017 | if [ ! -f "$CERT_PATH" ]; then | |
6cb415f5 | 6018 | _err "Cert for $Le_Domain $CERT_PATH is not found, skip." |
6019 | return 1 | |
6020 | fi | |
6cb415f5 | 6021 | |
fb73dcea | 6022 | . "$DOMAIN_CONF" |
13fd83e0 | 6023 | _debug Le_API "$Le_API" |
6024 | ||
6025 | if [ "$Le_API" ]; then | |
6026 | if [ "$Le_API" != "$ACME_DIRECTORY" ]; then | |
6027 | _clearAPI | |
6028 | fi | |
6029 | export ACME_DIRECTORY="$Le_API" | |
6030 | #reload ca configs | |
6031 | ACCOUNT_KEY_PATH="" | |
6032 | ACCOUNT_JSON_PATH="" | |
6033 | CA_CONF="" | |
6034 | _debug3 "initpath again." | |
6035 | _initpath "$Le_Domain" "$_isEcc" | |
6036 | _initAPI | |
6037 | fi | |
6038 | ||
11927a76 | 6039 | cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)" |
4c2a3841 | 6040 | |
6041 | if [ -z "$cert" ]; then | |
6cb415f5 | 6042 | _err "Cert for $Le_Domain is empty found, skip." |
6043 | return 1 | |
6044 | fi | |
4c2a3841 | 6045 | |
48d9a8c1 | 6046 | _initAPI |
6047 | ||
af3ea2d4 | 6048 | data="{\"certificate\": \"$cert\",\"reason\":$_reason}" |
6049 | ||
48d9a8c1 | 6050 | uri="${ACME_REVOKE_CERT}" |
6cb415f5 | 6051 | |
4c2a3841 | 6052 | if [ -f "$CERT_KEY_PATH" ]; then |
1befee5a | 6053 | _info "Try domain key first." |
c4a375b3 | 6054 | if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then |
4c2a3841 | 6055 | if [ -z "$response" ]; then |
1befee5a | 6056 | _info "Revoke success." |
c4a375b3 | 6057 | rm -f "$CERT_PATH" |
1befee5a | 6058 | return 0 |
4c2a3841 | 6059 | else |
1befee5a | 6060 | _err "Revoke error by domain key." |
6061 | _err "$response" | |
6062 | fi | |
6cb415f5 | 6063 | fi |
4c2a3841 | 6064 | else |
eca57bee | 6065 | _info "Domain key file doesn't exist." |
6cb415f5 | 6066 | fi |
6cb415f5 | 6067 | |
1befee5a | 6068 | _info "Try account key." |
6cb415f5 | 6069 | |
c4a375b3 | 6070 | if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then |
4c2a3841 | 6071 | if [ -z "$response" ]; then |
6cb415f5 | 6072 | _info "Revoke success." |
c4a375b3 | 6073 | rm -f "$CERT_PATH" |
6cb415f5 | 6074 | return 0 |
4c2a3841 | 6075 | else |
6cb415f5 | 6076 | _err "Revoke error." |
c9c31c04 | 6077 | _debug "$response" |
6cb415f5 | 6078 | fi |
6079 | fi | |
6080 | return 1 | |
6081 | } | |
4c3b3608 | 6082 | |
78f0201d | 6083 | #domain ecc |
6084 | remove() { | |
6085 | Le_Domain="$1" | |
6086 | if [ -z "$Le_Domain" ]; then | |
2e87e64b | 6087 | _usage "Usage: $PROJECT_ENTRY --remove --domain <domain.tld> [--ecc]" |
78f0201d | 6088 | return 1 |
6089 | fi | |
6090 | ||
6091 | _isEcc="$2" | |
6092 | ||
6093 | _initpath "$Le_Domain" "$_isEcc" | |
6094 | _removed_conf="$DOMAIN_CONF.removed" | |
6095 | if [ ! -f "$DOMAIN_CONF" ]; then | |
6096 | if [ -f "$_removed_conf" ]; then | |
6097 | _err "$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH" | |
6098 | else | |
6099 | _err "$Le_Domain is not a issued domain, skip." | |
6100 | fi | |
6101 | return 1 | |
6102 | fi | |
6103 | ||
6104 | if mv "$DOMAIN_CONF" "$_removed_conf"; then | |
68aea3af | 6105 | _info "$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)" |
78f0201d | 6106 | _info "You can remove them by yourself." |
6107 | return 0 | |
6108 | else | |
6109 | _err "Remove $Le_Domain failed." | |
6110 | return 1 | |
6111 | fi | |
6112 | } | |
6113 | ||
0c00e870 | 6114 | #domain vtype |
6115 | _deactivate() { | |
6116 | _d_domain="$1" | |
6117 | _d_type="$2" | |
536a5f7c | 6118 | _initpath "$_d_domain" "$_d_type" |
6119 | ||
6120 | . "$DOMAIN_CONF" | |
6121 | _debug Le_API "$Le_API" | |
6122 | ||
6123 | if [ "$Le_API" ]; then | |
17520043 | 6124 | if [ "$Le_API" != "$ACME_DIRECTORY" ]; then |
6125 | _clearAPI | |
6126 | fi | |
536a5f7c | 6127 | export ACME_DIRECTORY="$Le_API" |
6128 | #reload ca configs | |
6129 | ACCOUNT_KEY_PATH="" | |
6130 | ACCOUNT_JSON_PATH="" | |
6131 | CA_CONF="" | |
6132 | _debug3 "initpath again." | |
6133 | _initpath "$Le_Domain" "$_d_type" | |
17520043 | 6134 | _initAPI |
536a5f7c | 6135 | fi |
4c2a3841 | 6136 | |
3f588234 | 6137 | _identifiers="{\"type\":\"$(_getIdType "$_d_domain")\",\"value\":\"$_d_domain\"}" |
af3ea2d4 | 6138 | if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then |
6139 | _err "Can not get domain new order." | |
6140 | return 1 | |
6141 | fi | |
6142 | _authorizations_seg="$(echo "$response" | _egrep_o '"authorizations" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')" | |
6143 | _debug2 _authorizations_seg "$_authorizations_seg" | |
6144 | if [ -z "$_authorizations_seg" ]; then | |
6145 | _err "_authorizations_seg not found." | |
6146 | _clearup | |
6147 | _on_issue_err "$_post_hook" | |
6148 | return 1 | |
6149 | fi | |
d2cde379 | 6150 | |
af3ea2d4 | 6151 | authzUri="$_authorizations_seg" |
6152 | _debug2 "authzUri" "$authzUri" | |
6153 | if ! _send_signed_request "$authzUri"; then | |
6154 | _err "get to authz error." | |
6155 | _err "_authorizations_seg" "$_authorizations_seg" | |
6156 | _err "authzUri" "$authzUri" | |
6157 | _clearup | |
6158 | _on_issue_err "$_post_hook" | |
6159 | return 1 | |
14d7bfda | 6160 | fi |
0c00e870 | 6161 | |
af3ea2d4 | 6162 | response="$(echo "$response" | _normalizeJson)" |
6163 | _debug2 response "$response" | |
6164 | _URL_NAME="url" | |
6165 | ||
e0def669 | 6166 | entries="$(echo "$response" | tr '][' '==' | _egrep_o "challenges\": *=[^=]*=" | tr '}{' '\n\n' | grep "\"status\": *\"valid\"")" |
14d7bfda | 6167 | if [ -z "$entries" ]; then |
6168 | _info "No valid entries found." | |
6169 | if [ -z "$thumbprint" ]; then | |
6170 | thumbprint="$(__calc_account_thumbprint)" | |
6171 | fi | |
6172 | _debug "Trigger validation." | |
fe77d43f | 6173 | vtype="$(_getIdType "$_d_domain")" |
d0d74907 | 6174 | entry="$(echo "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')" |
14d7bfda | 6175 | _debug entry "$entry" |
6176 | if [ -z "$entry" ]; then | |
6177 | _err "Error, can not get domain token $d" | |
0c00e870 | 6178 | return 1 |
6179 | fi | |
d0d74907 | 6180 | token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')" |
14d7bfda | 6181 | _debug token "$token" |
4c2a3841 | 6182 | |
d0d74907 | 6183 | uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')" |
14d7bfda | 6184 | _debug uri "$uri" |
6185 | ||
6186 | keyauthorization="$token.$thumbprint" | |
6187 | _debug keyauthorization "$keyauthorization" | |
6188 | __trigger_validation "$uri" "$keyauthorization" | |
6189 | ||
6190 | fi | |
6191 | ||
6192 | _d_i=0 | |
6193 | _d_max_retry=$(echo "$entries" | wc -l) | |
6194 | while [ "$_d_i" -lt "$_d_max_retry" ]; do | |
6195 | _info "Deactivate: $_d_domain" | |
6196 | _d_i="$(_math $_d_i + 1)" | |
6197 | entry="$(echo "$entries" | sed -n "${_d_i}p")" | |
0c00e870 | 6198 | _debug entry "$entry" |
4c2a3841 | 6199 | |
6200 | if [ -z "$entry" ]; then | |
fb2029e7 | 6201 | _info "No more valid entry found." |
0c00e870 | 6202 | break |
6203 | fi | |
4c2a3841 | 6204 | |
d0d74907 | 6205 | _vtype="$(echo "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')" |
c4a375b3 | 6206 | _debug _vtype "$_vtype" |
0c00e870 | 6207 | _info "Found $_vtype" |
6208 | ||
199ca77c | 6209 | uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*\"" | tr -d '" ' | cut -d : -f 2-)" |
c4a375b3 | 6210 | _debug uri "$uri" |
4c2a3841 | 6211 | |
6212 | if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then | |
0c00e870 | 6213 | _info "Skip $_vtype" |
6214 | continue | |
6215 | fi | |
4c2a3841 | 6216 | |
0c00e870 | 6217 | _info "Deactivate: $_vtype" |
4c2a3841 | 6218 | |
af3ea2d4 | 6219 | _djson="{\"status\":\"deactivated\"}" |
d2cde379 | 6220 | |
6221 | if _send_signed_request "$authzUri" "$_djson" && _contains "$response" '"deactivated"'; then | |
14d7bfda | 6222 | _info "Deactivate: $_vtype success." |
6223 | else | |
0c00e870 | 6224 | _err "Can not deactivate $_vtype." |
14d7bfda | 6225 | break |
0c00e870 | 6226 | fi |
4c2a3841 | 6227 | |
0c00e870 | 6228 | done |
6229 | _debug "$_d_i" | |
14d7bfda | 6230 | if [ "$_d_i" -eq "$_d_max_retry" ]; then |
0c00e870 | 6231 | _info "Deactivated success!" |
6232 | else | |
6233 | _err "Deactivate failed." | |
6234 | fi | |
6235 | ||
6236 | } | |
6237 | ||
6238 | deactivate() { | |
3f4513b3 | 6239 | _d_domain_list="$1" |
0c00e870 | 6240 | _d_type="$2" |
6241 | _initpath | |
a3bdaa85 | 6242 | _initAPI |
3f4513b3 | 6243 | _debug _d_domain_list "$_d_domain_list" |
4c2a3841 | 6244 | if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then |
2e87e64b | 6245 | _usage "Usage: $PROJECT_ENTRY --deactivate --domain <domain.tld> [--domain <domain2.tld> ...]" |
0c00e870 | 6246 | return 1 |
6247 | fi | |
4c2a3841 | 6248 | for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do |
6249 | if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then | |
3f4513b3 | 6250 | continue |
6251 | fi | |
c4a375b3 | 6252 | if ! _deactivate "$_d_dm" "$_d_type"; then |
86c017ec | 6253 | return 1 |
6254 | fi | |
3f4513b3 | 6255 | done |
0c00e870 | 6256 | } |
6257 | ||
4c3b3608 | 6258 | # Detect profile file if not specified as environment variable |
6259 | _detect_profile() { | |
4c2a3841 | 6260 | if [ -n "$PROFILE" -a -f "$PROFILE" ]; then |
4c3b3608 | 6261 | echo "$PROFILE" |
6262 | return | |
6263 | fi | |
6264 | ||
4c3b3608 | 6265 | DETECTED_PROFILE='' |
4c3b3608 | 6266 | SHELLTYPE="$(basename "/$SHELL")" |
6267 | ||
4c2a3841 | 6268 | if [ "$SHELLTYPE" = "bash" ]; then |
6269 | if [ -f "$HOME/.bashrc" ]; then | |
4c3b3608 | 6270 | DETECTED_PROFILE="$HOME/.bashrc" |
4c2a3841 | 6271 | elif [ -f "$HOME/.bash_profile" ]; then |
4c3b3608 | 6272 | DETECTED_PROFILE="$HOME/.bash_profile" |
6273 | fi | |
4c2a3841 | 6274 | elif [ "$SHELLTYPE" = "zsh" ]; then |
4c3b3608 | 6275 | DETECTED_PROFILE="$HOME/.zshrc" |
6276 | fi | |
6277 | ||
4c2a3841 | 6278 | if [ -z "$DETECTED_PROFILE" ]; then |
6279 | if [ -f "$HOME/.profile" ]; then | |
4c3b3608 | 6280 | DETECTED_PROFILE="$HOME/.profile" |
4c2a3841 | 6281 | elif [ -f "$HOME/.bashrc" ]; then |
4c3b3608 | 6282 | DETECTED_PROFILE="$HOME/.bashrc" |
4c2a3841 | 6283 | elif [ -f "$HOME/.bash_profile" ]; then |
4c3b3608 | 6284 | DETECTED_PROFILE="$HOME/.bash_profile" |
4c2a3841 | 6285 | elif [ -f "$HOME/.zshrc" ]; then |
4c3b3608 | 6286 | DETECTED_PROFILE="$HOME/.zshrc" |
6287 | fi | |
6288 | fi | |
6289 | ||
1be222f6 | 6290 | echo "$DETECTED_PROFILE" |
4c3b3608 | 6291 | } |
6292 | ||
6293 | _initconf() { | |
6294 | _initpath | |
4c2a3841 | 6295 | if [ ! -f "$ACCOUNT_CONF_PATH" ]; then |
0ca5b799 | 6296 | echo " |
d404e92d | 6297 | |
d0871bda | 6298 | #LOG_FILE=\"$DEFAULT_LOG_FILE\" |
6b500036 | 6299 | #LOG_LEVEL=1 |
5ea6e9c9 | 6300 | |
251d1c5c | 6301 | #AUTO_UPGRADE=\"1\" |
89002ed2 | 6302 | |
569d6c55 | 6303 | #NO_TIMESTAMP=1 |
5b771039 | 6304 | |
d5ec5f80 | 6305 | " >"$ACCOUNT_CONF_PATH" |
4c3b3608 | 6306 | fi |
6307 | } | |
6308 | ||
c8e9a31e | 6309 | # nocron |
c60883ef | 6310 | _precheck() { |
c8e9a31e | 6311 | _nocron="$1" |
4c2a3841 | 6312 | |
6313 | if ! _exists "curl" && ! _exists "wget"; then | |
c60883ef | 6314 | _err "Please install curl or wget first, we need to access http resources." |
4c3b3608 | 6315 | return 1 |
6316 | fi | |
4c2a3841 | 6317 | |
6318 | if [ -z "$_nocron" ]; then | |
415f375c | 6319 | if ! _exists "crontab" && ! _exists "fcrontab"; then |
77f96b38 | 6320 | if _exists cygpath && _exists schtasks.exe; then |
6321 | _info "It seems you are on Windows, we will install Windows scheduler task." | |
6322 | else | |
6323 | _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'." | |
6324 | _err "We need to set cron job to renew the certs automatically." | |
6325 | _err "Otherwise, your certs will not be able to be renewed automatically." | |
6326 | if [ -z "$FORCE" ]; then | |
6327 | _err "Please add '--force' and try install again to go without crontab." | |
6328 | _err "./$PROJECT_ENTRY --install --force" | |
6329 | return 1 | |
6330 | fi | |
c8e9a31e | 6331 | fi |
77546ea5 | 6332 | fi |
4c3b3608 | 6333 | fi |
4c2a3841 | 6334 | |
d8ba26e6 | 6335 | if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then |
851fedf7 | 6336 | _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN" |
c60883ef | 6337 | _err "We need openssl to generate keys." |
4c3b3608 | 6338 | return 1 |
6339 | fi | |
4c2a3841 | 6340 | |
3794b5cb | 6341 | if ! _exists "socat"; then |
6342 | _err "It is recommended to install socat first." | |
6343 | _err "We use socat for standalone server if you use standalone mode." | |
c60883ef | 6344 | _err "If you don't use standalone mode, just ignore this warning." |
6345 | fi | |
4c2a3841 | 6346 | |
c60883ef | 6347 | return 0 |
6348 | } | |
6349 | ||
0a7c9364 | 6350 | _setShebang() { |
6351 | _file="$1" | |
6352 | _shebang="$2" | |
4c2a3841 | 6353 | if [ -z "$_shebang" ]; then |
43822d37 | 6354 | _usage "Usage: file shebang" |
0a7c9364 | 6355 | return 1 |
6356 | fi | |
6357 | cp "$_file" "$_file.tmp" | |
4c2a3841 | 6358 | echo "$_shebang" >"$_file" |
6359 | sed -n 2,99999p "$_file.tmp" >>"$_file" | |
6360 | rm -f "$_file.tmp" | |
0a7c9364 | 6361 | } |
6362 | ||
27dbe77f | 6363 | #confighome |
94dc5f33 | 6364 | _installalias() { |
27dbe77f | 6365 | _c_home="$1" |
94dc5f33 | 6366 | _initpath |
6367 | ||
6368 | _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
4c2a3841 | 6369 | if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then |
44edb2bd | 6370 | echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile" |
6371 | echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile" | |
6372 | echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile" | |
94dc5f33 | 6373 | fi |
6374 | ||
27dbe77f | 6375 | if [ "$_c_home" ]; then |
be83a6a3 | 6376 | _c_entry=" --config-home '$_c_home'" |
27dbe77f | 6377 | fi |
6378 | ||
1786a5e5 | 6379 | _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\"" |
f5b546b3 | 6380 | if [ "$_c_home" ]; then |
6381 | _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\"" | |
d04434e3 | 6382 | else |
6383 | _sed_i "/^export LE_CONFIG_HOME/d" "$_envfile" | |
f5b546b3 | 6384 | fi |
be83a6a3 | 6385 | _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\"" |
94dc5f33 | 6386 | |
6387 | _profile="$(_detect_profile)" | |
4c2a3841 | 6388 | if [ "$_profile" ]; then |
94dc5f33 | 6389 | _debug "Found profile: $_profile" |
aba5c634 | 6390 | _info "Installing alias to '$_profile'" |
94dc5f33 | 6391 | _setopt "$_profile" ". \"$_envfile\"" |
6392 | _info "OK, Close and reopen your terminal to start using $PROJECT_NAME" | |
6393 | else | |
6394 | _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME" | |
6395 | fi | |
94dc5f33 | 6396 | |
6397 | #for csh | |
6398 | _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh" | |
94dc5f33 | 6399 | _csh_profile="$HOME/.cshrc" |
4c2a3841 | 6400 | if [ -f "$_csh_profile" ]; then |
aba5c634 | 6401 | _info "Installing alias to '$_csh_profile'" |
6626371d | 6402 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" |
f5b546b3 | 6403 | if [ "$_c_home" ]; then |
6404 | _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\"" | |
d04434e3 | 6405 | else |
6406 | _sed_i "/^setenv LE_CONFIG_HOME/d" "$_cshfile" | |
f5b546b3 | 6407 | fi |
be83a6a3 | 6408 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\"" |
4c2a3841 | 6409 | _setopt "$_csh_profile" "source \"$_cshfile\"" |
94dc5f33 | 6410 | fi |
4c2a3841 | 6411 | |
acafa585 | 6412 | #for tcsh |
6413 | _tcsh_profile="$HOME/.tcshrc" | |
4c2a3841 | 6414 | if [ -f "$_tcsh_profile" ]; then |
aba5c634 | 6415 | _info "Installing alias to '$_tcsh_profile'" |
acafa585 | 6416 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" |
f5b546b3 | 6417 | if [ "$_c_home" ]; then |
6418 | _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\"" | |
6419 | fi | |
be83a6a3 | 6420 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\"" |
4c2a3841 | 6421 | _setopt "$_tcsh_profile" "source \"$_cshfile\"" |
acafa585 | 6422 | fi |
94dc5f33 | 6423 | |
6424 | } | |
6425 | ||
58c4eaaf | 6426 | # nocron confighome noprofile accountemail |
c60883ef | 6427 | install() { |
f3e4cea3 | 6428 | |
4c2a3841 | 6429 | if [ -z "$LE_WORKING_DIR" ]; then |
f3e4cea3 | 6430 | LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" |
6431 | fi | |
4c2a3841 | 6432 | |
c8e9a31e | 6433 | _nocron="$1" |
27dbe77f | 6434 | _c_home="$2" |
86ef0a26 | 6435 | _noprofile="$3" |
58c4eaaf | 6436 | _accountemail="$4" |
6437 | ||
4c2a3841 | 6438 | if ! _initpath; then |
c60883ef | 6439 | _err "Install failed." |
4c3b3608 | 6440 | return 1 |
6441 | fi | |
4c2a3841 | 6442 | if [ "$_nocron" ]; then |
52677b0a | 6443 | _debug "Skip install cron job" |
6444 | fi | |
4c2a3841 | 6445 | |
bd04638d | 6446 | if [ "$_ACME_IN_CRON" != "1" ]; then |
4356eefb | 6447 | if ! _precheck "$_nocron"; then |
6448 | _err "Pre-check failed, can not install." | |
6449 | return 1 | |
6450 | fi | |
4c3b3608 | 6451 | fi |
4c2a3841 | 6452 | |
8e845d9f | 6453 | if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then |
6454 | _info "Using config home: $LE_CONFIG_HOME" | |
6455 | _c_home="$LE_CONFIG_HOME" | |
6456 | fi | |
6457 | ||
6cc11ffb | 6458 | #convert from le |
4c2a3841 | 6459 | if [ -d "$HOME/.le" ]; then |
6460 | for envfile in "le.env" "le.sh.env"; do | |
6461 | if [ -f "$HOME/.le/$envfile" ]; then | |
6462 | if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then | |
6463 | _upgrading="1" | |
6464 | _info "You are upgrading from le.sh" | |
6465 | _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR" | |
6466 | mv "$HOME/.le" "$LE_WORKING_DIR" | |
6467 | mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
6468 | break | |
6cc11ffb | 6469 | fi |
6470 | fi | |
6471 | done | |
6472 | fi | |
6473 | ||
4c3b3608 | 6474 | _info "Installing to $LE_WORKING_DIR" |
635695ec | 6475 | |
d04434e3 | 6476 | if [ ! -d "$LE_WORKING_DIR" ]; then |
6477 | if ! mkdir -p "$LE_WORKING_DIR"; then | |
6478 | _err "Can not create working dir: $LE_WORKING_DIR" | |
6479 | return 1 | |
6480 | fi | |
6481 | ||
6482 | chmod 700 "$LE_WORKING_DIR" | |
4a0f23e2 | 6483 | fi |
4c2a3841 | 6484 | |
d04434e3 | 6485 | if [ ! -d "$LE_CONFIG_HOME" ]; then |
6486 | if ! mkdir -p "$LE_CONFIG_HOME"; then | |
6487 | _err "Can not create config dir: $LE_CONFIG_HOME" | |
6488 | return 1 | |
6489 | fi | |
762978f8 | 6490 | |
d04434e3 | 6491 | chmod 700 "$LE_CONFIG_HOME" |
27dbe77f | 6492 | fi |
6493 | ||
d5ec5f80 | 6494 | cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 6495 | |
4c2a3841 | 6496 | if [ "$?" != "0" ]; then |
a7b7355d | 6497 | _err "Install failed, can not copy $PROJECT_ENTRY" |
4c3b3608 | 6498 | return 1 |
6499 | fi | |
6500 | ||
a7b7355d | 6501 | _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 6502 | |
bd04638d | 6503 | if [ "$_ACME_IN_CRON" != "1" ] && [ -z "$_noprofile" ]; then |
4356eefb | 6504 | _installalias "$_c_home" |
6505 | fi | |
4c3b3608 | 6506 | |
4c2a3841 | 6507 | for subf in $_SUB_FOLDERS; do |
6508 | if [ -d "$subf" ]; then | |
d5ec5f80 | 6509 | mkdir -p "$LE_WORKING_DIR/$subf" |
6510 | cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/ | |
a61fe418 | 6511 | fi |
6512 | done | |
6513 | ||
4c2a3841 | 6514 | if [ ! -f "$ACCOUNT_CONF_PATH" ]; then |
4c3b3608 | 6515 | _initconf |
6516 | fi | |
6cc11ffb | 6517 | |
4c2a3841 | 6518 | if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then |
635695ec | 6519 | _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\"" |
6cc11ffb | 6520 | fi |
6521 | ||
4c2a3841 | 6522 | if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then |
b2817897 | 6523 | _saveaccountconf "CERT_HOME" "$CERT_HOME" |
6524 | fi | |
6525 | ||
4c2a3841 | 6526 | if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then |
b2817897 | 6527 | _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH" |
6528 | fi | |
4c2a3841 | 6529 | |
6530 | if [ -z "$_nocron" ]; then | |
27dbe77f | 6531 | installcronjob "$_c_home" |
c8e9a31e | 6532 | fi |
0a7c9364 | 6533 | |
4c2a3841 | 6534 | if [ -z "$NO_DETECT_SH" ]; then |
641989fd | 6535 | #Modify shebang |
4c2a3841 | 6536 | if _exists bash; then |
694af4ae | 6537 | _bash_path="$(bash -c "command -v bash 2>/dev/null")" |
6538 | if [ -z "$_bash_path" ]; then | |
6539 | _bash_path="$(bash -c 'echo $SHELL')" | |
6540 | fi | |
6541 | fi | |
6542 | if [ "$_bash_path" ]; then | |
329174b6 | 6543 | _info "Good, bash is found, so change the shebang to use bash as preferred." |
694af4ae | 6544 | _shebang='#!'"$_bash_path" |
641989fd | 6545 | _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang" |
4c2a3841 | 6546 | for subf in $_SUB_FOLDERS; do |
6547 | if [ -d "$LE_WORKING_DIR/$subf" ]; then | |
6548 | for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do | |
a61fe418 | 6549 | _setShebang "$_apifile" "$_shebang" |
6550 | done | |
6551 | fi | |
6552 | done | |
0a7c9364 | 6553 | fi |
6554 | fi | |
6555 | ||
58c4eaaf | 6556 | if [ "$_accountemail" ]; then |
6557 | _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail" | |
6558 | fi | |
6559 | ||
4c3b3608 | 6560 | _info OK |
6561 | } | |
6562 | ||
52677b0a | 6563 | # nocron |
4c3b3608 | 6564 | uninstall() { |
52677b0a | 6565 | _nocron="$1" |
4c2a3841 | 6566 | if [ -z "$_nocron" ]; then |
52677b0a | 6567 | uninstallcronjob |
6568 | fi | |
4c3b3608 | 6569 | _initpath |
6570 | ||
9aa3be7f | 6571 | _uninstallalias |
4c2a3841 | 6572 | |
d5ec5f80 | 6573 | rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY" |
f5b546b3 | 6574 | _info "The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself." |
9aa3be7f | 6575 | |
6576 | } | |
6577 | ||
6578 | _uninstallalias() { | |
6579 | _initpath | |
6580 | ||
4c3b3608 | 6581 | _profile="$(_detect_profile)" |
4c2a3841 | 6582 | if [ "$_profile" ]; then |
9aa3be7f | 6583 | _info "Uninstalling alias from: '$_profile'" |
d5ec5f80 | 6584 | text="$(cat "$_profile")" |
4c2a3841 | 6585 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile" |
4c3b3608 | 6586 | fi |
6587 | ||
94dc5f33 | 6588 | _csh_profile="$HOME/.cshrc" |
4c2a3841 | 6589 | if [ -f "$_csh_profile" ]; then |
9aa3be7f | 6590 | _info "Uninstalling alias from: '$_csh_profile'" |
d5ec5f80 | 6591 | text="$(cat "$_csh_profile")" |
4c2a3841 | 6592 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile" |
94dc5f33 | 6593 | fi |
4c2a3841 | 6594 | |
acafa585 | 6595 | _tcsh_profile="$HOME/.tcshrc" |
4c2a3841 | 6596 | if [ -f "$_tcsh_profile" ]; then |
9aa3be7f | 6597 | _info "Uninstalling alias from: '$_csh_profile'" |
d5ec5f80 | 6598 | text="$(cat "$_tcsh_profile")" |
4c2a3841 | 6599 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile" |
acafa585 | 6600 | fi |
4c3b3608 | 6601 | |
6602 | } | |
6603 | ||
6604 | cron() { | |
bd04638d | 6605 | export _ACME_IN_CRON=1 |
89002ed2 | 6606 | _initpath |
d8ba26e6 | 6607 | _info "$(__green "===Starting cron===")" |
4c2a3841 | 6608 | if [ "$AUTO_UPGRADE" = "1" ]; then |
89002ed2 | 6609 | export LE_WORKING_DIR |
6610 | ( | |
4c2a3841 | 6611 | if ! upgrade; then |
6612 | _err "Cron:Upgrade failed!" | |
6613 | return 1 | |
6614 | fi | |
89002ed2 | 6615 | ) |
d5ec5f80 | 6616 | . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null |
1ab63043 | 6617 | |
4c2a3841 | 6618 | if [ -t 1 ]; then |
1ab63043 | 6619 | __INTERACTIVE="1" |
6620 | fi | |
4c2a3841 | 6621 | |
89002ed2 | 6622 | _info "Auto upgraded to: $VER" |
6623 | fi | |
4c3b3608 | 6624 | renewAll |
cc179731 | 6625 | _ret="$?" |
bd04638d | 6626 | _ACME_IN_CRON="" |
d8ba26e6 | 6627 | _info "$(__green "===End cron===")" |
0ba95a3d | 6628 | exit $_ret |
4c3b3608 | 6629 | } |
6630 | ||
6631 | version() { | |
a63b05a9 | 6632 | echo "$PROJECT" |
6633 | echo "v$VER" | |
4c3b3608 | 6634 | } |
6635 | ||
b50e701c | 6636 | # subject content hooks code |
6637 | _send_notify() { | |
6638 | _nsubject="$1" | |
6639 | _ncontent="$2" | |
6640 | _nhooks="$3" | |
6641 | _nerror="$4" | |
6642 | ||
6643 | if [ "$NOTIFY_LEVEL" = "$NOTIFY_LEVEL_DISABLE" ]; then | |
6644 | _debug "The NOTIFY_LEVEL is $NOTIFY_LEVEL, disabled, just return." | |
6645 | return 0 | |
6646 | fi | |
6647 | ||
6648 | if [ -z "$_nhooks" ]; then | |
6649 | _debug "The NOTIFY_HOOK is empty, just return." | |
6650 | return 0 | |
6651 | fi | |
6652 | ||
6653 | _send_err=0 | |
6654 | for _n_hook in $(echo "$_nhooks" | tr ',' " "); do | |
6655 | _n_hook_file="$(_findHook "" $_SUB_FOLDER_NOTIFY "$_n_hook")" | |
143eac09 | 6656 | _info "Sending via: $_n_hook" |
6657 | _debug "Found $_n_hook_file for $_n_hook" | |
75191e71 | 6658 | if [ -z "$_n_hook_file" ]; then |
6659 | _err "Can not find the hook file for $_n_hook" | |
6660 | continue | |
6661 | fi | |
b50e701c | 6662 | if ! ( |
6663 | if ! . "$_n_hook_file"; then | |
6664 | _err "Load file $_n_hook_file error. Please check your api file and try again." | |
6665 | return 1 | |
6666 | fi | |
6667 | ||
6668 | d_command="${_n_hook}_send" | |
6669 | if ! _exists "$d_command"; then | |
6670 | _err "It seems that your api file is not correct, it must have a function named: $d_command" | |
6671 | return 1 | |
6672 | fi | |
6673 | ||
6674 | if ! $d_command "$_nsubject" "$_ncontent" "$_nerror"; then | |
6675 | _err "Error send message by $d_command" | |
6676 | return 1 | |
6677 | fi | |
6678 | ||
6679 | return 0 | |
6680 | ); then | |
6681 | _err "Set $_n_hook_file error." | |
6682 | _send_err=1 | |
6683 | else | |
6684 | _info "$_n_hook $(__green Success)" | |
6685 | fi | |
6686 | done | |
6687 | return $_send_err | |
6688 | ||
6689 | } | |
6690 | ||
6691 | # hook | |
6692 | _set_notify_hook() { | |
6693 | _nhooks="$1" | |
6694 | ||
5698bec6 | 6695 | _test_subject="Hello, this is a notification from $PROJECT_NAME" |
143eac09 | 6696 | _test_content="If you receive this message, your notification works." |
b50e701c | 6697 | |
6698 | _send_notify "$_test_subject" "$_test_content" "$_nhooks" 0 | |
6699 | ||
6700 | } | |
6701 | ||
6702 | #[hook] [level] [mode] | |
6703 | setnotify() { | |
6704 | _nhook="$1" | |
6705 | _nlevel="$2" | |
6706 | _nmode="$3" | |
6707 | ||
6708 | _initpath | |
6709 | ||
6710 | if [ -z "$_nhook$_nlevel$_nmode" ]; then | |
2e87e64b | 6711 | _usage "Usage: $PROJECT_ENTRY --set-notify [--notify-hook <hookname>] [--notify-level <0|1|2|3>] [--notify-mode <0|1>]" |
b50e701c | 6712 | _usage "$_NOTIFY_WIKI" |
6713 | return 1 | |
6714 | fi | |
6715 | ||
6716 | if [ "$_nlevel" ]; then | |
6717 | _info "Set notify level to: $_nlevel" | |
6718 | export "NOTIFY_LEVEL=$_nlevel" | |
6719 | _saveaccountconf "NOTIFY_LEVEL" "$NOTIFY_LEVEL" | |
6720 | fi | |
6721 | ||
6722 | if [ "$_nmode" ]; then | |
6723 | _info "Set notify mode to: $_nmode" | |
6724 | export "NOTIFY_MODE=$_nmode" | |
6725 | _saveaccountconf "NOTIFY_MODE" "$NOTIFY_MODE" | |
6726 | fi | |
6727 | ||
6728 | if [ "$_nhook" ]; then | |
6729 | _info "Set notify hook to: $_nhook" | |
6730 | if [ "$_nhook" = "$NO_VALUE" ]; then | |
6731 | _info "Clear notify hook" | |
6732 | _clearaccountconf "NOTIFY_HOOK" | |
6733 | else | |
6734 | if _set_notify_hook "$_nhook"; then | |
6735 | export NOTIFY_HOOK="$_nhook" | |
6736 | _saveaccountconf "NOTIFY_HOOK" "$NOTIFY_HOOK" | |
6737 | return 0 | |
6738 | else | |
6739 | _err "Can not set notify hook to: $_nhook" | |
6740 | return 1 | |
6741 | fi | |
6742 | fi | |
6743 | fi | |
6744 | ||
6745 | } | |
6746 | ||
4c3b3608 | 6747 | showhelp() { |
d0871bda | 6748 | _initpath |
4c3b3608 | 6749 | version |
2e87e64b | 6750 | echo "Usage: $PROJECT_ENTRY <command> ... [parameters ...] |
a63b05a9 | 6751 | Commands: |
c0fbe823 CE |
6752 | -h, --help Show this help message. |
6753 | -v, --version Show version info. | |
a7b7355d | 6754 | --install Install $PROJECT_NAME to your system. |
6755 | --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job. | |
d8beaf72 | 6756 | --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT. |
a63b05a9 | 6757 | --issue Issue a cert. |
a61fe418 | 6758 | --deploy Deploy the cert to your server. |
d81369d6 | 6759 | -i, --install-cert Install the issued cert to apache/nginx or any other server. |
c0fbe823 | 6760 | -r, --renew Renew a cert. |
27dbe77f | 6761 | --renew-all Renew all the certs. |
a63b05a9 | 6762 | --revoke Revoke a cert. |
47b49f1b | 6763 | --remove Remove the cert from list of certs known to $PROJECT_NAME. |
10afcaca | 6764 | --list List all the certs. |
c39e6c44 | 6765 | --info Show the $PROJECT_NAME configs, or the configs for a domain with [-d domain] parameter. |
58150f5d | 6766 | --to-pkcs12 Export the certificate and key to a pfx file. |
d81369d6 | 6767 | --to-pkcs8 Convert to pkcs8 format. |
7decf768 CE |
6768 | --sign-csr Issue a cert from an existing csr. |
6769 | --show-csr Show the content of a csr. | |
6770 | -ccr, --create-csr Create CSR, professional use. | |
6771 | --create-domain-key Create an domain private key, professional use. | |
27dbe77f | 6772 | --update-account Update account info. |
6773 | --register-account Register account key. | |
422dd1fa | 6774 | --deactivate-account Deactivate the account. |
0984585d | 6775 | --create-account-key Create an account private key, professional use. |
7decf768 CE |
6776 | --install-cronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job. |
6777 | --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically. | |
6778 | --cron Run cron job to renew all the certs. | |
b50e701c | 6779 | --set-notify Set the cron notification hook, level or mode. |
7decf768 | 6780 | --deactivate Deactivate the domain authz, professional use. |
8d0e4851 | 6781 | --set-default-ca Used with '--server', Set the default CA to use. |
6782 | See: $_SERVER_WIKI | |
d4e18997 | 6783 | --set-default-chain Set the default preferred chain for a CA. |
6784 | See: $_PREFERRED_CHAIN_WIKI | |
b50e701c | 6785 | |
3c07f57a | 6786 | |
a63b05a9 | 6787 | Parameters: |
c0fbe823 CE |
6788 | -d, --domain <domain.tld> Specifies a domain, used to issue, renew or revoke etc. |
6789 | --challenge-alias <domain.tld> The challenge domain alias for DNS alias mode. | |
e3ebd582 | 6790 | See: $_DNS_ALIAS_WIKI |
a48c22d1 | 6791 | |
c0fbe823 | 6792 | --domain-alias <domain.tld> The domain alias for DNS alias mode. |
e3ebd582 | 6793 | See: $_DNS_ALIAS_WIKI |
a48c22d1 | 6794 | |
c0fbe823 | 6795 | --preferred-chain <chain> If the CA offers multiple certificate chains, prefer the chain with an issuer matching this Subject Common Name. |
e3ebd582 | 6796 | If no match, the default offered chain will be used. (default: empty) |
6797 | See: $_PREFERRED_CHAIN_WIKI | |
a48c22d1 | 6798 | |
de4c4eed | 6799 | --valid-to <date-time> Request the NotAfter field of the cert. |
6800 | See: $_VALIDITY_WIKI | |
6801 | --valid-from <date-time> Request the NotBefore field of the cert. | |
6802 | See: $_VALIDITY_WIKI | |
6803 | ||
c0fbe823 CE |
6804 | -f, --force Force install, force cert renewal or override sudo restrictions. |
6805 | --staging, --test Use staging server, for testing. | |
6806 | --debug [0|1|2|3] Output debug info. Defaults to 1 if argument is omitted. | |
e3ebd582 | 6807 | --output-insecure Output all the sensitive messages. |
6808 | By default all the credentials/sensitive messages are hidden from the output/debug/log for security. | |
b086afb2 | 6809 | -w, --webroot <directory> Specifies the web root folder for web root mode. |
a63b05a9 | 6810 | --standalone Use standalone mode. |
08681f4a | 6811 | --alpn Use standalone alpn mode. |
e3ebd582 | 6812 | --stateless Use stateless mode. |
6813 | See: $_STATELESS_WIKI | |
a48c22d1 | 6814 | |
a63b05a9 | 6815 | --apache Use apache mode. |
c0fbe823 | 6816 | --dns [dns_hook] Use dns manual mode or dns api. Defaults to manual mode when argument is omitted. |
e3ebd582 | 6817 | See: $_DNS_API_WIKI |
a48c22d1 | 6818 | |
c0fbe823 | 6819 | --dnssleep <seconds> The time in seconds to wait for all the txt records to propagate in dns api mode. |
e3ebd582 | 6820 | It's not necessary to use this by default, $PROJECT_NAME polls dns status by DOH automatically. |
c0fbe823 CE |
6821 | -k, --keylength <bits> Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384, ec-521. |
6822 | -ak, --accountkeylength <bits> Specifies the account key length: 2048, 3072, 4096 | |
b086afb2 | 6823 | --log [file] Specifies the log file. Defaults to \"$DEFAULT_LOG_FILE\" if argument is omitted. |
c0fbe823 CE |
6824 | --log-level <1|2> Specifies the log level, default is 1. |
6825 | --syslog <0|3|6|7> Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug. | |
6826 | --eab-kid <eab_key_id> Key Identifier for External Account Binding. | |
6827 | --eab-hmac-key <eab_hmac_key> HMAC key for External Account Binding. | |
2d5f1438 OB |
6828 | |
6829 | ||
7903fcb4 | 6830 | These parameters are to install the cert to nginx/apache or any other server after issue/renew a cert: |
3c07f57a | 6831 | |
b086afb2 CE |
6832 | --cert-file <file> Path to copy the cert file to after issue/renew.. |
6833 | --key-file <file> Path to copy the key file to after issue/renew. | |
6834 | --ca-file <file> Path to copy the intermediate cert file to after issue/renew. | |
6835 | --fullchain-file <file> Path to copy the fullchain cert file to after issue/renew. | |
c0fbe823 | 6836 | --reloadcmd <command> Command to execute after issue/renew to reload the server. |
a63b05a9 | 6837 | |
c0fbe823 | 6838 | --server <server_uri> ACME Directory Resource URI. (default: $DEFAULT_CA) |
e3ebd582 | 6839 | See: $_SERVER_WIKI |
6840 | ||
c0fbe823 CE |
6841 | --accountconf <file> Specifies a customized account config file. |
6842 | --home <directory> Specifies the home dir for $PROJECT_NAME. | |
6843 | --cert-home <directory> Specifies the home dir to save all the certs, only valid for '--install' command. | |
6844 | --config-home <directory> Specifies the home dir to save all the configurations. | |
6845 | --useragent <string> Specifies the user agent string. it will be saved for future use too. | |
58c4eaaf | 6846 | -m, --email <email> Specifies the account email, only valid for the '--install' and '--update-account' command. |
c0fbe823 CE |
6847 | --accountkey <file> Specifies the account key path, only valid for the '--install' command. |
6848 | --days <ndays> Specifies the days to renew the cert when using '--issue' command. The default value is $DEFAULT_RENEW days. | |
6849 | --httpport <port> Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer. | |
6850 | --tlsport <port> Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer. | |
6851 | --local-address <ip> Specifies the standalone/tls server listening address, in case you have multiple ip addresses. | |
dcf4f8f6 | 6852 | --listraw Only used for '--list' command, list the certs in raw format. |
d81369d6 | 6853 | -se, --stop-renew-on-error Only valid for '--renew-all' command. Stop if one cert has error in renewal. |
13d7cae9 | 6854 | --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted. |
c0fbe823 CE |
6855 | --ca-bundle <file> Specifies the path to the CA certificate bundle to verify api server's certificate. |
6856 | --ca-path <directory> Specifies directory containing CA certificates in PEM format, used by wget or curl. | |
58c4eaaf | 6857 | --no-cron Only valid for '--install' command, which means: do not install the default cron job. |
e3ebd582 | 6858 | In this case, the certs will not be renewed automatically. |
58c4eaaf | 6859 | --no-profile Only valid for '--install' command, which means: do not install aliases to user profile. |
2910be82 | 6860 | --no-color Do not output color text. |
e32b3aac | 6861 | --force-color Force output of color text. Useful for non-interactive use with the aha tool for HTML E-Mails. |
b67d663a | 6862 | --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--to-pkcs12' and '--create-csr' |
c0fbe823 CE |
6863 | --csr <file> Specifies the input csr. |
6864 | --pre-hook <command> Command to be run before obtaining any certificates. | |
6865 | --post-hook <command> Command to be run after attempting to obtain/renew certificates. Runs regardless of whether obtain/renew succeeded or failed. | |
6866 | --renew-hook <command> Command to be run after each successfully renewed certificate. | |
6867 | --deploy-hook <hookname> The hook file to deploy cert | |
6868 | --ocsp, --ocsp-must-staple Generate OCSP-Must-Staple extension. | |
6869 | --always-force-new-domain-key Generate new domain key on renewal. Otherwise, the domain key is not changed by default. | |
6870 | --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future. Defaults to 1 if argument is omitted. | |
6ae0f7f5 | 6871 | --listen-v4 Force standalone/tls server to listen at ipv4. |
6872 | --listen-v6 Force standalone/tls server to listen at ipv6. | |
c0fbe823 | 6873 | --openssl-bin <file> Specifies a custom openssl bin location. |
9b124070 | 6874 | --use-wget Force to use wget, if you have both curl and wget installed. |
07fdb087 | 6875 | --yes-I-know-dns-manual-mode-enough-go-ahead-please Force use of dns manual mode. |
e3ebd582 | 6876 | See: $_DNS_MANUAL_WIKI |
a48c22d1 | 6877 | |
c0fbe823 CE |
6878 | -b, --branch <branch> Only valid for '--upgrade' command, specifies the branch name to upgrade to. |
6879 | --notify-level <0|1|2|3> Set the notification level: Default value is $NOTIFY_LEVEL_DEFAULT. | |
6880 | 0: disabled, no notification will be sent. | |
6881 | 1: send notifications only when there is an error. | |
6882 | 2: send notifications when a cert is successfully renewed, or there is an error. | |
6883 | 3: send notifications when a cert is skipped, renewed, or error. | |
6884 | --notify-mode <0|1> Set notification mode. Default value is $NOTIFY_MODE_DEFAULT. | |
6885 | 0: Bulk mode. Send all the domain's notifications in one message(mail). | |
6886 | 1: Cert mode. Send a message for every single cert. | |
6887 | --notify-hook <hookname> Set the notify hook | |
6888 | --revoke-reason <0-10> The reason for revocation, can be used in conjunction with the '--revoke' command. | |
e3ebd582 | 6889 | See: $_REVOKE_WIKI |
6890 | ||
dd6c5c9e CE |
6891 | --password <password> Add a password to exported pfx file. Use with --to-pkcs12. |
6892 | ||
b50e701c | 6893 | |
6894 | " | |
4c3b3608 | 6895 | } |
6896 | ||
58c4eaaf | 6897 | installOnline() { |
4a0f23e2 | 6898 | _info "Installing from online archive." |
58c4eaaf | 6899 | |
6900 | _branch="$BRANCH" | |
6901 | if [ -z "$_branch" ]; then | |
6902 | _branch="master" | |
4a0f23e2 | 6903 | fi |
a8df88ab | 6904 | |
58c4eaaf | 6905 | target="$PROJECT/archive/$_branch.tar.gz" |
4a0f23e2 | 6906 | _info "Downloading $target" |
58c4eaaf | 6907 | localname="$_branch.tar.gz" |
4c2a3841 | 6908 | if ! _get "$target" >$localname; then |
df9547ae | 6909 | _err "Download error." |
4a0f23e2 | 6910 | return 1 |
6911 | fi | |
0bbe6eef | 6912 | ( |
4c2a3841 | 6913 | _info "Extracting $localname" |
3a3b0dd5 | 6914 | if ! (tar xzf $localname || gtar xzf $localname); then |
6915 | _err "Extraction error." | |
6916 | exit 1 | |
6917 | fi | |
4c2a3841 | 6918 | |
58c4eaaf | 6919 | cd "$PROJECT_NAME-$_branch" |
4c2a3841 | 6920 | chmod +x $PROJECT_ENTRY |
58c4eaaf | 6921 | if ./$PROJECT_ENTRY --install "$@"; then |
4c2a3841 | 6922 | _info "Install success!" |
ac3667c7 | 6923 | _initpath |
cb7e3857 | 6924 | _saveaccountconf "UPGRADE_HASH" "$(_getUpgradeHash)" |
4c2a3841 | 6925 | fi |
6926 | ||
6927 | cd .. | |
6928 | ||
58c4eaaf | 6929 | rm -rf "$PROJECT_NAME-$_branch" |
4c2a3841 | 6930 | rm -f "$localname" |
0bbe6eef | 6931 | ) |
4a0f23e2 | 6932 | } |
6933 | ||
cb7e3857 OB |
6934 | _getRepoHash() { |
6935 | _hash_path=$1 | |
6936 | shift | |
6937 | _hash_url="https://api.github.com/repos/acmesh-official/$PROJECT_NAME/git/refs/$_hash_path" | |
e0def669 | 6938 | _get $_hash_url | tr -d "\r\n" | tr '{},' '\n\n\n' | grep '"sha":' | cut -d '"' -f 4 |
cb7e3857 OB |
6939 | } |
6940 | ||
6941 | _getUpgradeHash() { | |
f716f606 | 6942 | _b="$BRANCH" |
6943 | if [ -z "$_b" ]; then | |
6944 | _b="master" | |
6945 | fi | |
cb7e3857 OB |
6946 | _hash=$(_getRepoHash "heads/$_b") |
6947 | if [ -z "$_hash" ]; then _hash=$(_getRepoHash "tags/$_b"); fi | |
6948 | echo $_hash | |
7a3c61b7 | 6949 | } |
6950 | ||
52677b0a | 6951 | upgrade() { |
6952 | if ( | |
267f283a | 6953 | _initpath |
cb7e3857 | 6954 | [ -z "$FORCE" ] && [ "$(_getUpgradeHash)" = "$(_readaccountconf "UPGRADE_HASH")" ] && _info "Already uptodate!" && exit 0 |
267f283a | 6955 | export LE_WORKING_DIR |
d0b748a4 | 6956 | cd "$LE_WORKING_DIR" |
58c4eaaf | 6957 | installOnline "--nocron" "--noprofile" |
4c2a3841 | 6958 | ); then |
52677b0a | 6959 | _info "Upgrade success!" |
096d8992 | 6960 | exit 0 |
52677b0a | 6961 | else |
6962 | _err "Upgrade failed!" | |
096d8992 | 6963 | exit 1 |
52677b0a | 6964 | fi |
6965 | } | |
a63b05a9 | 6966 | |
5ea6e9c9 | 6967 | _processAccountConf() { |
4c2a3841 | 6968 | if [ "$_useragent" ]; then |
5ea6e9c9 | 6969 | _saveaccountconf "USER_AGENT" "$_useragent" |
4c2a3841 | 6970 | elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then |
d0871bda | 6971 | _saveaccountconf "USER_AGENT" "$USER_AGENT" |
5ea6e9c9 | 6972 | fi |
4c2a3841 | 6973 | |
a746139c | 6974 | if [ "$_openssl_bin" ]; then |
851fedf7 | 6975 | _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin" |
6976 | elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then | |
6977 | _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN" | |
a746139c | 6978 | fi |
6979 | ||
4c2a3841 | 6980 | if [ "$_auto_upgrade" ]; then |
6bf281f9 | 6981 | _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade" |
4c2a3841 | 6982 | elif [ "$AUTO_UPGRADE" ]; then |
6bf281f9 | 6983 | _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE" |
6984 | fi | |
4c2a3841 | 6985 | |
9b124070 | 6986 | if [ "$_use_wget" ]; then |
6987 | _saveaccountconf "ACME_USE_WGET" "$_use_wget" | |
6988 | elif [ "$ACME_USE_WGET" ]; then | |
6989 | _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET" | |
6990 | fi | |
6991 | ||
5ea6e9c9 | 6992 | } |
6993 | ||
5bdfdfef | 6994 | _checkSudo() { |
14b59142 | 6995 | if [ -z "__INTERACTIVE" ]; then |
6996 | #don't check if it's not in an interactive shell | |
6997 | return 0 | |
6998 | fi | |
5bdfdfef | 6999 | if [ "$SUDO_GID" ] && [ "$SUDO_COMMAND" ] && [ "$SUDO_USER" ] && [ "$SUDO_UID" ]; then |
7000 | if [ "$SUDO_USER" = "root" ] && [ "$SUDO_UID" = "0" ]; then | |
7001 | #it's root using sudo, no matter it's using sudo or not, just fine | |
7002 | return 0 | |
7003 | fi | |
79ad0ff5 | 7004 | if [ -n "$SUDO_COMMAND" ]; then |
996f5337 | 7005 | #it's a normal user doing "sudo su", or `sudo -i` or `sudo -s`, or `sudo su acmeuser1` |
7006 | _endswith "$SUDO_COMMAND" /bin/su || _contains "$SUDO_COMMAND" "/bin/su " || grep "^$SUDO_COMMAND\$" /etc/shells >/dev/null 2>&1 | |
79ad0ff5 | 7007 | return $? |
5bdfdfef | 7008 | fi |
7009 | #otherwise | |
7010 | return 1 | |
7011 | fi | |
7012 | return 0 | |
7013 | } | |
7014 | ||
53d6ab6c | 7015 | #server #keylength |
737e9e48 | 7016 | _selectServer() { |
7017 | _server="$1" | |
53d6ab6c | 7018 | _skeylength="$2" |
737e9e48 | 7019 | _server_lower="$(echo "$_server" | _lower_case)" |
7020 | _sindex=0 | |
7021 | for snames in $CA_NAMES; do | |
7022 | snames="$(echo "$snames" | _lower_case)" | |
7023 | _sindex="$(_math $_sindex + 1)" | |
7024 | _debug2 "_selectServer try snames" "$snames" | |
7025 | for sname in $(echo "$snames" | tr ',' ' '); do | |
7026 | if [ "$_server_lower" = "$sname" ]; then | |
7027 | _debug2 "_selectServer match $sname" | |
7028 | _serverdir="$(_getfield "$CA_SERVERS" $_sindex)" | |
53d6ab6c | 7029 | if [ "$_serverdir" = "$CA_SSLCOM_RSA" ] && _isEccKey "$_skeylength"; then |
7030 | _serverdir="$CA_SSLCOM_ECC" | |
7031 | fi | |
737e9e48 | 7032 | _debug "Selected server: $_serverdir" |
7033 | ACME_DIRECTORY="$_serverdir" | |
7034 | export ACME_DIRECTORY | |
7035 | return | |
7036 | fi | |
7037 | done | |
7038 | done | |
7039 | ACME_DIRECTORY="$_server" | |
7040 | export ACME_DIRECTORY | |
7041 | } | |
7042 | ||
269847d1 | 7043 | #url |
7044 | _getCAShortName() { | |
7045 | caurl="$1" | |
95ef046d | 7046 | if [ -z "$caurl" ]; then |
7047 | caurl="$DEFAULT_CA" | |
7048 | fi | |
c7285967 | 7049 | if [ "$CA_SSLCOM_ECC" = "$caurl" ]; then |
7050 | caurl="$CA_SSLCOM_RSA" #just hack to get the short name | |
7051 | fi | |
269847d1 | 7052 | caurl_lower="$(echo $caurl | _lower_case)" |
7053 | _sindex=0 | |
7054 | for surl in $(echo "$CA_SERVERS" | _lower_case | tr , ' '); do | |
7055 | _sindex="$(_math $_sindex + 1)" | |
7056 | if [ "$caurl_lower" = "$surl" ]; then | |
7057 | _nindex=0 | |
7058 | for snames in $CA_NAMES; do | |
7059 | _nindex="$(_math $_nindex + 1)" | |
7060 | if [ $_nindex -ge $_sindex ]; then | |
7061 | _getfield "$snames" 1 | |
7062 | return | |
7063 | fi | |
7064 | done | |
7065 | fi | |
7066 | done | |
7067 | echo "$caurl" | |
7068 | } | |
7069 | ||
737e9e48 | 7070 | #set default ca to $ACME_DIRECTORY |
7071 | setdefaultca() { | |
7072 | if [ -z "$ACME_DIRECTORY" ]; then | |
7073 | _err "Please give a --server parameter." | |
7074 | return 1 | |
7075 | fi | |
7076 | _saveaccountconf "DEFAULT_ACME_SERVER" "$ACME_DIRECTORY" | |
df22f680 | 7077 | _info "Changed default CA to: $(__green "$ACME_DIRECTORY")" |
737e9e48 | 7078 | } |
7079 | ||
d4e18997 | 7080 | #preferred-chain |
7081 | setdefaultchain() { | |
7082 | _initpath | |
7083 | _preferred_chain="$1" | |
7084 | if [ -z "$_preferred_chain" ]; then | |
7085 | _err "Please give a '--preferred-chain value' value." | |
7086 | return 1 | |
7087 | fi | |
7088 | mkdir -p "$CA_DIR" | |
7089 | _savecaconf "DEFAULT_PREFERRED_CHAIN" "$_preferred_chain" | |
7090 | } | |
7091 | ||
c39e6c44 | 7092 | #domain ecc |
7093 | info() { | |
7094 | _domain="$1" | |
7095 | _ecc="$2" | |
7096 | _initpath | |
7097 | if [ -z "$_domain" ]; then | |
7098 | _debug "Show global configs" | |
7099 | echo "LE_WORKING_DIR=$LE_WORKING_DIR" | |
7100 | echo "LE_CONFIG_HOME=$LE_CONFIG_HOME" | |
7101 | cat "$ACCOUNT_CONF_PATH" | |
7102 | else | |
7103 | _debug "Show domain configs" | |
7104 | ( | |
7105 | _initpath "$_domain" "$_ecc" | |
7106 | echo "DOMAIN_CONF=$DOMAIN_CONF" | |
7107 | for seg in $(cat $DOMAIN_CONF | cut -d = -f 1); do | |
7108 | echo "$seg=$(_readdomainconf "$seg")" | |
7109 | done | |
7110 | ) | |
7111 | fi | |
7112 | } | |
7113 | ||
a63b05a9 | 7114 | _process() { |
7115 | _CMD="" | |
7116 | _domain="" | |
3f4513b3 | 7117 | _altdomains="$NO_VALUE" |
a63b05a9 | 7118 | _webroot="" |
875625b1 | 7119 | _challenge_alias="" |
64847afc | 7120 | _keylength="$DEFAULT_DOMAIN_KEY_LENGTH" |
7121 | _accountkeylength="$DEFAULT_ACCOUNT_KEY_LENGTH" | |
5c539af7 | 7122 | _cert_file="" |
7123 | _key_file="" | |
7124 | _ca_file="" | |
7125 | _fullchain_file="" | |
4d2f38b0 | 7126 | _reloadcmd="" |
a63b05a9 | 7127 | _password="" |
635695ec | 7128 | _accountconf="" |
7129 | _useragent="" | |
b5eb4b90 | 7130 | _accountemail="" |
7131 | _accountkey="" | |
b2817897 | 7132 | _certhome="" |
27dbe77f | 7133 | _confighome="" |
39c8f79f | 7134 | _httpport="" |
e22bcf7c | 7135 | _tlsport="" |
0e38c60d | 7136 | _dnssleep="" |
dcf4f8f6 | 7137 | _listraw="" |
cc179731 | 7138 | _stopRenewOnError="" |
e3698edd | 7139 | #_insecure="" |
78009539 | 7140 | _ca_bundle="" |
2aa75f03 | 7141 | _ca_path="" |
c8e9a31e | 7142 | _nocron="" |
61556a54 | 7143 | _noprofile="" |
43822d37 | 7144 | _ecc="" |
10afcaca | 7145 | _csr="" |
b0070f03 | 7146 | _pre_hook="" |
7147 | _post_hook="" | |
7148 | _renew_hook="" | |
a61fe418 | 7149 | _deploy_hook="" |
5ea6e9c9 | 7150 | _logfile="" |
d0871bda | 7151 | _log="" |
0463b5d6 | 7152 | _local_address="" |
a73c5b33 | 7153 | _log_level="" |
6bf281f9 | 7154 | _auto_upgrade="" |
6ae0f7f5 | 7155 | _listen_v4="" |
7156 | _listen_v6="" | |
a746139c | 7157 | _openssl_bin="" |
e2edf208 | 7158 | _syslog="" |
9b124070 | 7159 | _use_wget="" |
98394f99 | 7160 | _server="" |
b50e701c | 7161 | _notify_hook="" |
7162 | _notify_level="" | |
7163 | _notify_mode="" | |
1041c9f9 | 7164 | _revoke_reason="" |
f96d91cb | 7165 | _eab_kid="" |
7166 | _eab_hmac_key="" | |
e3ebd582 | 7167 | _preferred_chain="" |
de4c4eed | 7168 | _valid_from="" |
7169 | _valid_to="" | |
4c2a3841 | 7170 | while [ ${#} -gt 0 ]; do |
a63b05a9 | 7171 | case "${1}" in |
4c2a3841 | 7172 | |
19c43451 | 7173 | --help | -h) |
7174 | showhelp | |
7175 | return | |
7176 | ;; | |
7177 | --version | -v) | |
7178 | version | |
7179 | return | |
7180 | ;; | |
7181 | --install) | |
7182 | _CMD="install" | |
7183 | ;; | |
58c4eaaf | 7184 | --install-online) |
7185 | shift | |
7186 | installOnline "$@" | |
7187 | return | |
7188 | ;; | |
19c43451 | 7189 | --uninstall) |
7190 | _CMD="uninstall" | |
7191 | ;; | |
7192 | --upgrade) | |
7193 | _CMD="upgrade" | |
7194 | ;; | |
7195 | --issue) | |
7196 | _CMD="issue" | |
7197 | ;; | |
7198 | --deploy) | |
7199 | _CMD="deploy" | |
7200 | ;; | |
7decf768 | 7201 | --sign-csr | --signcsr) |
19c43451 | 7202 | _CMD="signcsr" |
7203 | ;; | |
7decf768 | 7204 | --show-csr | --showcsr) |
19c43451 | 7205 | _CMD="showcsr" |
7206 | ;; | |
7decf768 | 7207 | -i | --install-cert | --installcert) |
19c43451 | 7208 | _CMD="installcert" |
7209 | ;; | |
7210 | --renew | -r) | |
7211 | _CMD="renew" | |
7212 | ;; | |
7decf768 | 7213 | --renew-all | --renewAll | --renewall) |
19c43451 | 7214 | _CMD="renewAll" |
7215 | ;; | |
7216 | --revoke) | |
7217 | _CMD="revoke" | |
7218 | ;; | |
7219 | --remove) | |
7220 | _CMD="remove" | |
7221 | ;; | |
7222 | --list) | |
7223 | _CMD="list" | |
7224 | ;; | |
c39e6c44 | 7225 | --info) |
7226 | _CMD="info" | |
7227 | ;; | |
7decf768 | 7228 | --install-cronjob | --installcronjob) |
19c43451 | 7229 | _CMD="installcronjob" |
7230 | ;; | |
7decf768 | 7231 | --uninstall-cronjob | --uninstallcronjob) |
19c43451 | 7232 | _CMD="uninstallcronjob" |
7233 | ;; | |
7234 | --cron) | |
7235 | _CMD="cron" | |
7236 | ;; | |
1521199e | 7237 | --to-pkcs12 | --to-pkcs | --toPkcs) |
19c43451 | 7238 | _CMD="toPkcs" |
7239 | ;; | |
7decf768 | 7240 | --to-pkcs8 | --toPkcs8) |
19c43451 | 7241 | _CMD="toPkcs8" |
7242 | ;; | |
7decf768 | 7243 | --create-account-key | --createAccountKey | --createaccountkey | -cak) |
19c43451 | 7244 | _CMD="createAccountKey" |
7245 | ;; | |
7decf768 | 7246 | --create-domain-key | --createDomainKey | --createdomainkey | -cdk) |
19c43451 | 7247 | _CMD="createDomainKey" |
7248 | ;; | |
7decf768 | 7249 | -ccr | --create-csr | --createCSR | --createcsr) |
19c43451 | 7250 | _CMD="createCSR" |
7251 | ;; | |
7252 | --deactivate) | |
7253 | _CMD="deactivate" | |
7254 | ;; | |
7decf768 | 7255 | --update-account | --updateaccount) |
19c43451 | 7256 | _CMD="updateaccount" |
7257 | ;; | |
7decf768 | 7258 | --register-account | --registeraccount) |
19c43451 | 7259 | _CMD="registeraccount" |
7260 | ;; | |
7261 | --deactivate-account) | |
7262 | _CMD="deactivateaccount" | |
7263 | ;; | |
7264 | --set-notify) | |
7265 | _CMD="setnotify" | |
7266 | ;; | |
7267 | --set-default-ca) | |
7268 | _CMD="setdefaultca" | |
7269 | ;; | |
d4e18997 | 7270 | --set-default-chain) |
7271 | _CMD="setdefaultchain" | |
7272 | ;; | |
7decf768 | 7273 | -d | --domain) |
19c43451 | 7274 | _dvalue="$2" |
4c2a3841 | 7275 | |
19c43451 | 7276 | if [ "$_dvalue" ]; then |
7277 | if _startswith "$_dvalue" "-"; then | |
7278 | _err "'$_dvalue' is not a valid domain for parameter '$1'" | |
7279 | return 1 | |
7280 | fi | |
7281 | if _is_idn "$_dvalue" && ! _exists idn; then | |
7282 | _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first." | |
7283 | return 1 | |
7284 | fi | |
4c2a3841 | 7285 | |
19c43451 | 7286 | if [ -z "$_domain" ]; then |
7287 | _domain="$_dvalue" | |
7288 | else | |
7289 | if [ "$_altdomains" = "$NO_VALUE" ]; then | |
7290 | _altdomains="$_dvalue" | |
a63b05a9 | 7291 | else |
19c43451 | 7292 | _altdomains="$_altdomains,$_dvalue" |
a63b05a9 | 7293 | fi |
7294 | fi | |
19c43451 | 7295 | fi |
4c2a3841 | 7296 | |
19c43451 | 7297 | shift |
7298 | ;; | |
a63b05a9 | 7299 | |
7decf768 | 7300 | -f | --force) |
19c43451 | 7301 | FORCE="1" |
7302 | ;; | |
7303 | --staging | --test) | |
7304 | STAGE="1" | |
7305 | ;; | |
7306 | --server) | |
7307 | _server="$2" | |
19c43451 | 7308 | shift |
7309 | ;; | |
7310 | --debug) | |
7311 | if [ -z "$2" ] || _startswith "$2" "-"; then | |
7312 | DEBUG="$DEBUG_LEVEL_DEFAULT" | |
7313 | else | |
7314 | DEBUG="$2" | |
64821ad4 | 7315 | shift |
19c43451 | 7316 | fi |
7317 | ;; | |
7318 | --output-insecure) | |
7319 | export OUTPUT_INSECURE=1 | |
7320 | ;; | |
7decf768 | 7321 | -w | --webroot) |
19c43451 | 7322 | wvalue="$2" |
7323 | if [ -z "$_webroot" ]; then | |
7324 | _webroot="$wvalue" | |
7325 | else | |
7326 | _webroot="$_webroot,$wvalue" | |
7327 | fi | |
7328 | shift | |
7329 | ;; | |
7330 | --challenge-alias) | |
7331 | cvalue="$2" | |
7332 | _challenge_alias="$_challenge_alias$cvalue," | |
7333 | shift | |
7334 | ;; | |
7335 | --domain-alias) | |
7336 | cvalue="$DNS_ALIAS_PREFIX$2" | |
7337 | _challenge_alias="$_challenge_alias$cvalue," | |
7338 | shift | |
7339 | ;; | |
7340 | --standalone) | |
7341 | wvalue="$NO_VALUE" | |
7342 | if [ -z "$_webroot" ]; then | |
7343 | _webroot="$wvalue" | |
7344 | else | |
7345 | _webroot="$_webroot,$wvalue" | |
7346 | fi | |
7347 | ;; | |
7348 | --alpn) | |
7349 | wvalue="$W_ALPN" | |
7350 | if [ -z "$_webroot" ]; then | |
7351 | _webroot="$wvalue" | |
7352 | else | |
7353 | _webroot="$_webroot,$wvalue" | |
7354 | fi | |
7355 | ;; | |
7356 | --stateless) | |
7357 | wvalue="$MODE_STATELESS" | |
7358 | if [ -z "$_webroot" ]; then | |
7359 | _webroot="$wvalue" | |
7360 | else | |
7361 | _webroot="$_webroot,$wvalue" | |
7362 | fi | |
7363 | ;; | |
7364 | --local-address) | |
7365 | lvalue="$2" | |
7366 | _local_address="$_local_address$lvalue," | |
7367 | shift | |
7368 | ;; | |
7369 | --apache) | |
7370 | wvalue="apache" | |
7371 | if [ -z "$_webroot" ]; then | |
7372 | _webroot="$wvalue" | |
7373 | else | |
7374 | _webroot="$_webroot,$wvalue" | |
7375 | fi | |
7376 | ;; | |
7377 | --nginx) | |
7378 | wvalue="$NGINX" | |
7379 | if [ "$2" ] && ! _startswith "$2" "-"; then | |
7380 | wvalue="$NGINX$2" | |
0463b5d6 | 7381 | shift |
19c43451 | 7382 | fi |
7383 | if [ -z "$_webroot" ]; then | |
7384 | _webroot="$wvalue" | |
7385 | else | |
7386 | _webroot="$_webroot,$wvalue" | |
7387 | fi | |
7388 | ;; | |
7389 | --dns) | |
7390 | wvalue="$W_DNS" | |
7391 | if [ "$2" ] && ! _startswith "$2" "-"; then | |
7392 | wvalue="$2" | |
0e38c60d | 7393 | shift |
19c43451 | 7394 | fi |
7395 | if [ -z "$_webroot" ]; then | |
7396 | _webroot="$wvalue" | |
7397 | else | |
7398 | _webroot="$_webroot,$wvalue" | |
7399 | fi | |
7400 | ;; | |
7401 | --dnssleep) | |
7402 | _dnssleep="$2" | |
7403 | Le_DNSSleep="$_dnssleep" | |
7404 | shift | |
7405 | ;; | |
19c43451 | 7406 | --keylength | -k) |
7407 | _keylength="$2" | |
7408 | shift | |
7409 | ;; | |
7decf768 | 7410 | -ak | --accountkeylength) |
19c43451 | 7411 | _accountkeylength="$2" |
7412 | shift | |
7413 | ;; | |
19c43451 | 7414 | --cert-file | --certpath) |
7415 | _cert_file="$2" | |
7416 | shift | |
7417 | ;; | |
7418 | --key-file | --keypath) | |
7419 | _key_file="$2" | |
7420 | shift | |
7421 | ;; | |
7422 | --ca-file | --capath) | |
7423 | _ca_file="$2" | |
7424 | shift | |
7425 | ;; | |
7426 | --fullchain-file | --fullchainpath) | |
7427 | _fullchain_file="$2" | |
7428 | shift | |
7429 | ;; | |
7430 | --reloadcmd | --reloadCmd) | |
7431 | _reloadcmd="$2" | |
7432 | shift | |
7433 | ;; | |
7434 | --password) | |
7435 | _password="$2" | |
7436 | shift | |
7437 | ;; | |
7438 | --accountconf) | |
7439 | _accountconf="$2" | |
7440 | ACCOUNT_CONF_PATH="$_accountconf" | |
7441 | shift | |
7442 | ;; | |
7443 | --home) | |
7444 | LE_WORKING_DIR="$2" | |
7445 | shift | |
7446 | ;; | |
7decf768 | 7447 | --cert-home | --certhome) |
19c43451 | 7448 | _certhome="$2" |
7449 | CERT_HOME="$_certhome" | |
7450 | shift | |
7451 | ;; | |
7452 | --config-home) | |
7453 | _confighome="$2" | |
7454 | LE_CONFIG_HOME="$_confighome" | |
7455 | shift | |
7456 | ;; | |
7457 | --useragent) | |
7458 | _useragent="$2" | |
7459 | USER_AGENT="$_useragent" | |
7460 | shift | |
7461 | ;; | |
58c4eaaf | 7462 | -m | --email | --accountemail) |
19c43451 | 7463 | _accountemail="$2" |
58c4eaaf | 7464 | export ACCOUNT_EMAIL="$_accountemail" |
19c43451 | 7465 | shift |
7466 | ;; | |
7467 | --accountkey) | |
7468 | _accountkey="$2" | |
7469 | ACCOUNT_KEY_PATH="$_accountkey" | |
7470 | shift | |
7471 | ;; | |
7472 | --days) | |
7473 | _days="$2" | |
7474 | Le_RenewalDays="$_days" | |
7475 | shift | |
7476 | ;; | |
de4c4eed | 7477 | --valid-from) |
7478 | _valid_from="$2" | |
7479 | shift | |
7480 | ;; | |
7481 | --valid-to) | |
7482 | _valid_to="$2" | |
7483 | shift | |
7484 | ;; | |
19c43451 | 7485 | --httpport) |
7486 | _httpport="$2" | |
7487 | Le_HTTPPort="$_httpport" | |
7488 | shift | |
7489 | ;; | |
7490 | --tlsport) | |
7491 | _tlsport="$2" | |
7492 | Le_TLSPort="$_tlsport" | |
7493 | shift | |
7494 | ;; | |
7495 | --listraw) | |
7496 | _listraw="raw" | |
7497 | ;; | |
7decf768 | 7498 | -se | --stop-renew-on-error | --stopRenewOnError | --stoprenewonerror) |
19c43451 | 7499 | _stopRenewOnError="1" |
7500 | ;; | |
7501 | --insecure) | |
7502 | #_insecure="1" | |
7503 | HTTPS_INSECURE="1" | |
7504 | ;; | |
7505 | --ca-bundle) | |
7506 | _ca_bundle="$(_readlink "$2")" | |
7507 | CA_BUNDLE="$_ca_bundle" | |
7508 | shift | |
7509 | ;; | |
7510 | --ca-path) | |
7511 | _ca_path="$2" | |
7512 | CA_PATH="$_ca_path" | |
7513 | shift | |
7514 | ;; | |
58c4eaaf | 7515 | --no-cron | --nocron) |
19c43451 | 7516 | _nocron="1" |
7517 | ;; | |
58c4eaaf | 7518 | --no-profile | --noprofile) |
19c43451 | 7519 | _noprofile="1" |
7520 | ;; | |
2910be82 | 7521 | --no-color) |
19c43451 | 7522 | export ACME_NO_COLOR=1 |
7523 | ;; | |
7524 | --force-color) | |
7525 | export ACME_FORCE_COLOR=1 | |
7526 | ;; | |
7527 | --ecc) | |
7528 | _ecc="isEcc" | |
7529 | ;; | |
7530 | --csr) | |
7531 | _csr="$2" | |
7532 | shift | |
7533 | ;; | |
7534 | --pre-hook) | |
7535 | _pre_hook="$2" | |
7536 | shift | |
7537 | ;; | |
7538 | --post-hook) | |
7539 | _post_hook="$2" | |
7540 | shift | |
7541 | ;; | |
7542 | --renew-hook) | |
7543 | _renew_hook="$2" | |
7544 | shift | |
7545 | ;; | |
7546 | --deploy-hook) | |
7547 | if [ -z "$2" ] || _startswith "$2" "-"; then | |
7548 | _usage "Please specify a value for '--deploy-hook'" | |
7549 | return 1 | |
7550 | fi | |
7551 | _deploy_hook="$_deploy_hook$2," | |
7552 | shift | |
7553 | ;; | |
7554 | --ocsp-must-staple | --ocsp) | |
7555 | Le_OCSP_Staple="1" | |
7556 | ;; | |
7557 | --always-force-new-domain-key) | |
7558 | if [ -z "$2" ] || _startswith "$2" "-"; then | |
7559 | Le_ForceNewDomainKey=1 | |
7560 | else | |
7561 | Le_ForceNewDomainKey="$2" | |
1041c9f9 | 7562 | shift |
19c43451 | 7563 | fi |
7564 | ;; | |
7565 | --yes-I-know-dns-manual-mode-enough-go-ahead-please) | |
7566 | export FORCE_DNS_MANUAL=1 | |
7567 | ;; | |
7568 | --log | --logfile) | |
7569 | _log="1" | |
7570 | _logfile="$2" | |
7571 | if _startswith "$_logfile" '-'; then | |
7572 | _logfile="" | |
7573 | else | |
f96d91cb | 7574 | shift |
19c43451 | 7575 | fi |
7576 | LOG_FILE="$_logfile" | |
7577 | if [ -z "$LOG_LEVEL" ]; then | |
7578 | LOG_LEVEL="$DEFAULT_LOG_LEVEL" | |
7579 | fi | |
7580 | ;; | |
7581 | --log-level) | |
7582 | _log_level="$2" | |
7583 | LOG_LEVEL="$_log_level" | |
7584 | shift | |
7585 | ;; | |
7586 | --syslog) | |
7587 | if ! _startswith "$2" '-'; then | |
7588 | _syslog="$2" | |
f96d91cb | 7589 | shift |
19c43451 | 7590 | fi |
7591 | if [ -z "$_syslog" ]; then | |
7592 | _syslog="$SYSLOG_LEVEL_DEFAULT" | |
7593 | fi | |
7594 | ;; | |
7595 | --auto-upgrade) | |
7596 | _auto_upgrade="$2" | |
7597 | if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then | |
7598 | _auto_upgrade="1" | |
7599 | else | |
e3ebd582 | 7600 | shift |
19c43451 | 7601 | fi |
7602 | AUTO_UPGRADE="$_auto_upgrade" | |
7603 | ;; | |
7604 | --listen-v4) | |
7605 | _listen_v4="1" | |
7606 | Le_Listen_V4="$_listen_v4" | |
7607 | ;; | |
7608 | --listen-v6) | |
7609 | _listen_v6="1" | |
7610 | Le_Listen_V6="$_listen_v6" | |
7611 | ;; | |
7612 | --openssl-bin) | |
7613 | _openssl_bin="$2" | |
7614 | ACME_OPENSSL_BIN="$_openssl_bin" | |
7615 | shift | |
7616 | ;; | |
7617 | --use-wget) | |
7618 | _use_wget="1" | |
7619 | ACME_USE_WGET="1" | |
7620 | ;; | |
7621 | --branch | -b) | |
7622 | export BRANCH="$2" | |
7623 | shift | |
7624 | ;; | |
7625 | --notify-hook) | |
7626 | _nhook="$2" | |
7627 | if _startswith "$_nhook" "-"; then | |
7628 | _err "'$_nhook' is not a hook name for '$1'" | |
a63b05a9 | 7629 | return 1 |
19c43451 | 7630 | fi |
7631 | if [ "$_notify_hook" ]; then | |
7632 | _notify_hook="$_notify_hook,$_nhook" | |
7633 | else | |
7634 | _notify_hook="$_nhook" | |
7635 | fi | |
7636 | shift | |
7637 | ;; | |
7638 | --notify-level) | |
7639 | _nlevel="$2" | |
7640 | if _startswith "$_nlevel" "-"; then | |
7641 | _err "'$_nlevel' is not a integer for '$1'" | |
7642 | return 1 | |
7643 | fi | |
7644 | _notify_level="$_nlevel" | |
7645 | shift | |
7646 | ;; | |
7647 | --notify-mode) | |
7648 | _nmode="$2" | |
7649 | if _startswith "$_nmode" "-"; then | |
7650 | _err "'$_nmode' is not a integer for '$1'" | |
7651 | return 1 | |
7652 | fi | |
7653 | _notify_mode="$_nmode" | |
7654 | shift | |
7655 | ;; | |
7656 | --revoke-reason) | |
7657 | _revoke_reason="$2" | |
7658 | if _startswith "$_revoke_reason" "-"; then | |
7659 | _err "'$_revoke_reason' is not a integer for '$1'" | |
7660 | return 1 | |
7661 | fi | |
7662 | shift | |
7663 | ;; | |
7664 | --eab-kid) | |
7665 | _eab_kid="$2" | |
7666 | shift | |
7667 | ;; | |
7668 | --eab-hmac-key) | |
7669 | _eab_hmac_key="$2" | |
7670 | shift | |
7671 | ;; | |
7672 | --preferred-chain) | |
7673 | _preferred_chain="$2" | |
7674 | shift | |
7675 | ;; | |
7676 | *) | |
7677 | _err "Unknown parameter : $1" | |
7678 | return 1 | |
7679 | ;; | |
a63b05a9 | 7680 | esac |
7681 | ||
7682 | shift 1 | |
7683 | done | |
7684 | ||
53d6ab6c | 7685 | if [ "$_server" ]; then |
30f11d0e | 7686 | _selectServer "$_server" "${_ecc:-$_keylength}" |
8b7a86bd | 7687 | _server="$ACME_DIRECTORY" |
53d6ab6c | 7688 | fi |
7689 | ||
4c2a3841 | 7690 | if [ "${_CMD}" != "install" ]; then |
5bdfdfef | 7691 | if [ "$__INTERACTIVE" ] && ! _checkSudo; then |
7692 | if [ -z "$FORCE" ]; then | |
7693 | #Use "echo" here, instead of _info. it's too early | |
7694 | echo "It seems that you are using sudo, please read this link first:" | |
7695 | echo "$_SUDO_WIKI" | |
7696 | return 1 | |
7697 | fi | |
7698 | fi | |
5ea6e9c9 | 7699 | __initHome |
661f0583 | 7700 | if [ "$_log" ]; then |
4c2a3841 | 7701 | if [ -z "$_logfile" ]; then |
661f0583 | 7702 | _logfile="$DEFAULT_LOG_FILE" |
7703 | fi | |
d0871bda | 7704 | fi |
4c2a3841 | 7705 | if [ "$_logfile" ]; then |
5ea6e9c9 | 7706 | _saveaccountconf "LOG_FILE" "$_logfile" |
661f0583 | 7707 | LOG_FILE="$_logfile" |
5ea6e9c9 | 7708 | fi |
a73c5b33 | 7709 | |
4c2a3841 | 7710 | if [ "$_log_level" ]; then |
a73c5b33 | 7711 | _saveaccountconf "LOG_LEVEL" "$_log_level" |
7712 | LOG_LEVEL="$_log_level" | |
7713 | fi | |
4c2a3841 | 7714 | |
e2edf208 | 7715 | if [ "$_syslog" ]; then |
7716 | if _exists logger; then | |
7717 | if [ "$_syslog" = "0" ]; then | |
7718 | _clearaccountconf "SYS_LOG" | |
7719 | else | |
7720 | _saveaccountconf "SYS_LOG" "$_syslog" | |
7721 | fi | |
7722 | SYS_LOG="$_syslog" | |
7723 | else | |
7724 | _err "The 'logger' command is not found, can not enable syslog." | |
7725 | _clearaccountconf "SYS_LOG" | |
7726 | SYS_LOG="" | |
7727 | fi | |
7728 | fi | |
7729 | ||
5ea6e9c9 | 7730 | _processAccountConf |
7731 | fi | |
4c2a3841 | 7732 | |
9d548d81 | 7733 | _debug2 LE_WORKING_DIR "$LE_WORKING_DIR" |
4c2a3841 | 7734 | |
7735 | if [ "$DEBUG" ]; then | |
dcf9cb58 | 7736 | version |
98394f99 | 7737 | if [ "$_server" ]; then |
7738 | _debug "Using server: $_server" | |
7739 | fi | |
dcf9cb58 | 7740 | fi |
9a733a57 | 7741 | _debug "Running cmd: ${_CMD}" |
a63b05a9 | 7742 | case "${_CMD}" in |
58c4eaaf | 7743 | install) install "$_nocron" "$_confighome" "$_noprofile" "$_accountemail" ;; |
19c43451 | 7744 | uninstall) uninstall "$_nocron" ;; |
7745 | upgrade) upgrade ;; | |
7746 | issue) | |
de4c4eed | 7747 | issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias" "$_preferred_chain" "$_valid_from" "$_valid_to" |
19c43451 | 7748 | ;; |
7749 | deploy) | |
7750 | deploy "$_domain" "$_deploy_hook" "$_ecc" | |
7751 | ;; | |
7752 | signcsr) | |
96a95ba9 | 7753 | signcsr "$_csr" "$_webroot" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias" "$_preferred_chain" |
19c43451 | 7754 | ;; |
7755 | showcsr) | |
7756 | showcsr "$_csr" "$_domain" | |
7757 | ;; | |
7758 | installcert) | |
7759 | installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc" | |
7760 | ;; | |
7761 | renew) | |
8b7a86bd | 7762 | renew "$_domain" "$_ecc" "$_server" |
19c43451 | 7763 | ;; |
7764 | renewAll) | |
8b7a86bd | 7765 | renewAll "$_stopRenewOnError" "$_server" |
19c43451 | 7766 | ;; |
7767 | revoke) | |
7768 | revoke "$_domain" "$_ecc" "$_revoke_reason" | |
7769 | ;; | |
7770 | remove) | |
7771 | remove "$_domain" "$_ecc" | |
7772 | ;; | |
7773 | deactivate) | |
7774 | deactivate "$_domain,$_altdomains" | |
7775 | ;; | |
7776 | registeraccount) | |
7777 | registeraccount "$_accountkeylength" "$_eab_kid" "$_eab_hmac_key" | |
7778 | ;; | |
7779 | updateaccount) | |
7780 | updateaccount | |
7781 | ;; | |
7782 | deactivateaccount) | |
7783 | deactivateaccount | |
7784 | ;; | |
7785 | list) | |
7786 | list "$_listraw" "$_domain" | |
7787 | ;; | |
c39e6c44 | 7788 | info) |
7789 | info "$_domain" "$_ecc" | |
7790 | ;; | |
19c43451 | 7791 | installcronjob) installcronjob "$_confighome" ;; |
7792 | uninstallcronjob) uninstallcronjob ;; | |
7793 | cron) cron ;; | |
7794 | toPkcs) | |
7795 | toPkcs "$_domain" "$_password" "$_ecc" | |
7796 | ;; | |
7797 | toPkcs8) | |
7798 | toPkcs8 "$_domain" "$_ecc" | |
7799 | ;; | |
7800 | createAccountKey) | |
7801 | createAccountKey "$_accountkeylength" | |
7802 | ;; | |
7803 | createDomainKey) | |
7804 | createDomainKey "$_domain" "$_keylength" | |
7805 | ;; | |
7806 | createCSR) | |
7807 | createCSR "$_domain" "$_altdomains" "$_ecc" | |
7808 | ;; | |
7809 | setnotify) | |
7810 | setnotify "$_notify_hook" "$_notify_level" "$_notify_mode" | |
7811 | ;; | |
7812 | setdefaultca) | |
7813 | setdefaultca | |
7814 | ;; | |
d4e18997 | 7815 | setdefaultchain) |
7816 | setdefaultchain "$_preferred_chain" | |
7817 | ;; | |
19c43451 | 7818 | *) |
7819 | if [ "$_CMD" ]; then | |
7820 | _err "Invalid command: $_CMD" | |
7821 | fi | |
7822 | showhelp | |
7823 | return 1 | |
7824 | ;; | |
a63b05a9 | 7825 | esac |
d3595686 | 7826 | _ret="$?" |
4c2a3841 | 7827 | if [ "$_ret" != "0" ]; then |
d3595686 | 7828 | return $_ret |
7829 | fi | |
4c2a3841 | 7830 | |
7831 | if [ "${_CMD}" = "install" ]; then | |
7832 | if [ "$_log" ]; then | |
7833 | if [ -z "$LOG_FILE" ]; then | |
d0871bda | 7834 | LOG_FILE="$DEFAULT_LOG_FILE" |
7835 | fi | |
7836 | _saveaccountconf "LOG_FILE" "$LOG_FILE" | |
5ea6e9c9 | 7837 | fi |
4c2a3841 | 7838 | |
7839 | if [ "$_log_level" ]; then | |
a73c5b33 | 7840 | _saveaccountconf "LOG_LEVEL" "$_log_level" |
7841 | fi | |
e2edf208 | 7842 | |
7843 | if [ "$_syslog" ]; then | |
7844 | if _exists logger; then | |
7845 | if [ "$_syslog" = "0" ]; then | |
7846 | _clearaccountconf "SYS_LOG" | |
7847 | else | |
7848 | _saveaccountconf "SYS_LOG" "$_syslog" | |
7849 | fi | |
7850 | else | |
7851 | _err "The 'logger' command is not found, can not enable syslog." | |
7852 | _clearaccountconf "SYS_LOG" | |
7853 | SYS_LOG="" | |
7854 | fi | |
7855 | fi | |
7856 | ||
5ea6e9c9 | 7857 | _processAccountConf |
b5eb4b90 | 7858 | fi |
635695ec | 7859 | |
a63b05a9 | 7860 | } |
7861 | ||
319e0ae3 | 7862 | main() { |
7863 | [ -z "$1" ] && showhelp && return | |
4c2a3841 | 7864 | if _startswith "$1" '-'; then _process "$@"; else "$@"; fi |
319e0ae3 | 7865 | } |
e69a7c38 | 7866 | |
aa7b82de | 7867 | main "$@" |