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