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