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