]> git.proxmox.com Git - mirror_acme.sh.git/blame - acme.sh
Merge pull request #3520 from tresni/synology_dsm
[mirror_acme.sh.git] / acme.sh
CommitLineData
0a7c9364 1#!/usr/bin/env sh
bfdf1f48 2
0881cf13 3VER=2.9.0
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
aa59c46c 3581 _debug2 "$_eabresp"
389518e1 3582 _eab_id="$(echo "$_eabresp" | tr ',}' '\n' | grep '"eab_kid"' | cut -d : -f 2 | tr -d '"')"
3583 if [ -z "$_eab_id" ]; then
365aa69a 3584 _err "Can not resolve _eab_id"
389518e1 3585 return 1
3586 fi
3587 _eab_hmac_key="$(echo "$_eabresp" | tr ',}' '\n' | grep '"eab_hmac_key"' | cut -d : -f 2 | tr -d '"')"
3588 if [ -z "$_eab_hmac_key" ]; then
365aa69a 3589 _err "Can not resolve _eab_hmac_key"
389518e1 3590 return 1
3591 fi
3592 _savecaconf CA_EAB_KEY_ID "$_eab_id"
3593 _savecaconf CA_EAB_HMAC_KEY "$_eab_hmac_key"
3594 fi
3595 fi
f96d91cb 3596 if [ "$_eab_id" ] && [ "$_eab_hmac_key" ]; then
3597 eab_protected="{\"alg\":\"HS256\",\"kid\":\"$_eab_id\",\"url\":\"${ACME_NEW_ACCOUNT}\"}"
3598 _debug3 eab_protected "$eab_protected"
3599
3600 eab_protected64=$(printf "%s" "$eab_protected" | _base64 | _url_replace)
3601 _debug3 eab_protected64 "$eab_protected64"
3602
3603 eab_payload64=$(printf "%s" "$jwk" | _base64 | _url_replace)
3604 _debug3 eab_payload64 "$eab_payload64"
3605
3606 eab_sign_t="$eab_protected64.$eab_payload64"
3607 _debug3 eab_sign_t "$eab_sign_t"
3608
d42ff227 3609 key_hex="$(_durl_replace_base64 "$_eab_hmac_key" | _dbase64 | _hex_dump | tr -d ' ')"
f96d91cb 3610 _debug3 key_hex "$key_hex"
3611
d42ff227 3612 eab_signature=$(printf "%s" "$eab_sign_t" | _hmac sha256 $key_hex | _base64 | _url_replace)
f96d91cb 3613 _debug3 eab_signature "$eab_signature"
3614
3615 externalBinding=",\"externalAccountBinding\":{\"protected\":\"$eab_protected64\", \"payload\":\"$eab_payload64\", \"signature\":\"$eab_signature\"}"
3616 _debug3 externalBinding "$externalBinding"
3617 fi
389518e1 3618 if [ "$_email" ]; then
3619 email_sg="\"contact\": [\"mailto:$_email\"], "
c1151b0d 3620 fi
f96d91cb 3621 regjson="{$email_sg\"termsOfServiceAgreed\": true$externalBinding}"
c1151b0d 3622 else
3623 _reg_res="$ACME_NEW_ACCOUNT_RES"
3624 regjson='{"resource": "'$_reg_res'", "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
389518e1 3625 if [ "$_email" ]; then
3626 regjson='{"resource": "'$_reg_res'", "contact": ["mailto:'$_email'"], "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
c1151b0d 3627 fi
f87890cb 3628 fi
4c2a3841 3629
389518e1 3630 _info "Registering account: $ACME_DIRECTORY"
d404e92d 3631
f87890cb 3632 if ! _send_signed_request "${ACME_NEW_ACCOUNT}" "$regjson"; then
3633 _err "Register account Error: $response"
3634 return 1
3635 fi
d404e92d 3636
389518e1 3637 _eabAlreadyBound=""
f87890cb 3638 if [ "$code" = "" ] || [ "$code" = '201' ]; then
3639 echo "$response" >"$ACCOUNT_JSON_PATH"
3640 _info "Registered"
7df20e50 3641 elif [ "$code" = '409' ] || [ "$code" = '200' ]; then
f87890cb 3642 _info "Already registered"
389518e1 3643 elif [ "$code" = '400' ] && _contains "$response" 'The account is not awaiting external account binding'; then
3644 _info "Already register EAB."
3645 _eabAlreadyBound=1
f87890cb 3646 else
3647 _err "Register account Error: $response"
3648 return 1
3649 fi
d404e92d 3650
389518e1 3651 if [ -z "$_eabAlreadyBound" ]; then
3652 _debug2 responseHeaders "$responseHeaders"
3653 _accUri="$(echo "$responseHeaders" | grep -i "^Location:" | _head_n 1 | cut -d ':' -f 2- | tr -d "\r\n ")"
3654 _debug "_accUri" "$_accUri"
3655 if [ -z "$_accUri" ]; then
3656 _err "Can not find account id url."
3657 _err "$responseHeaders"
3658 return 1
3659 fi
3660 _savecaconf "ACCOUNT_URL" "$_accUri"
3661 else
3662 ACCOUNT_URL="$(_readcaconf ACCOUNT_URL)"
7e0b334b 3663 fi
7e0b334b 3664 export ACCOUNT_URL="$_accUri"
d404e92d 3665
f87890cb 3666 CA_KEY_HASH="$(__calcAccountKeyHash)"
3667 _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH"
3668 _savecaconf CA_KEY_HASH "$CA_KEY_HASH"
d404e92d 3669
f87890cb 3670 if [ "$code" = '403' ]; then
3671 _err "It seems that the account key is already deactivated, please use a new account key."
3672 return 1
3673 fi
4c2a3841 3674
f87890cb 3675 ACCOUNT_THUMBPRINT="$(__calc_account_thumbprint)"
3676 _info "ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT"
d404e92d 3677}
3678
79e2f8a2 3679#implement updateaccount
3680updateaccount() {
3681 _initpath
3682
3683 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
3684 _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
3685 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3686 fi
3687
3688 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
3689 _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
3690 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3691 fi
3692
3693 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3694 _err "Account key is not found at: $ACCOUNT_KEY_PATH"
3695 return 1
3696 fi
3697
3698 _accUri=$(_readcaconf "ACCOUNT_URL")
3699 _debug _accUri "$_accUri"
3700
3701 if [ -z "$_accUri" ]; then
3702 _err "The account url is empty, please run '--update-account' first to update the account info first,"
3703 _err "Then try again."
3704 return 1
3705 fi
3706
3707 if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
3708 return 1
3709 fi
3710 _initAPI
3711
389518e1 3712 _email="$(_getAccountEmail)"
79e2f8a2 3713 if [ "$ACME_VERSION" = "2" ]; then
3714 if [ "$ACCOUNT_EMAIL" ]; then
389518e1 3715 updjson='{"contact": ["mailto:'$_email'"]}'
2febdfc3
SW
3716 else
3717 updjson='{"contact": []}'
79e2f8a2 3718 fi
3719 else
3720 # ACMEv1: Updates happen the same way a registration is done.
3721 # https://tools.ietf.org/html/draft-ietf-acme-acme-01#section-6.3
3722 _regAccount
3723 return
3724 fi
3725
3726 # this part handles ACMEv2 account updates.
3727 _send_signed_request "$_accUri" "$updjson"
3728
3729 if [ "$code" = '200' ]; then
72e1a1b2 3730 echo "$response" >"$ACCOUNT_JSON_PATH"
79e2f8a2 3731 _info "account update success for $_accUri."
3732 else
3733 _info "Error. The account was not updated."
3734 return 1
3735 fi
3736}
3737
422dd1fa 3738#Implement deactivate account
3739deactivateaccount() {
3740 _initpath
3741
3742 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
422dd1fa 3743 _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
3744 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3745 fi
3746
3747 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
422dd1fa 3748 _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
3749 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3750 fi
3751
3752 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3753 _err "Account key is not found at: $ACCOUNT_KEY_PATH"
3754 return 1
3755 fi
3756
3757 _accUri=$(_readcaconf "ACCOUNT_URL")
3758 _debug _accUri "$_accUri"
3759
3760 if [ -z "$_accUri" ]; then
3761 _err "The account url is empty, please run '--update-account' first to update the account info first,"
3762 _err "Then try again."
3763 return 1
3764 fi
3765
3766 if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
3767 return 1
3768 fi
3769 _initAPI
3770
d2cde379 3771 if [ "$ACME_VERSION" = "2" ]; then
3772 _djson="{\"status\":\"deactivated\"}"
3773 else
3774 _djson="{\"resource\": \"reg\", \"status\":\"deactivated\"}"
3775 fi
3776 if _send_signed_request "$_accUri" "$_djson" && _contains "$response" '"deactivated"'; then
422dd1fa 3777 _info "Deactivate account success for $_accUri."
3778 _accid=$(echo "$response" | _egrep_o "\"id\" *: *[^,]*," | cut -d : -f 2 | tr -d ' ,')
3779 elif [ "$code" = "403" ]; then
3780 _info "The account is already deactivated."
3781 _accid=$(_getfield "$_accUri" "999" "/")
3782 else
3783 _err "Deactivate: account failed for $_accUri."
3784 return 1
3785 fi
3786
3787 _debug "Account id: $_accid"
3788 if [ "$_accid" ]; then
3789 _deactivated_account_path="$CA_DIR/deactivated/$_accid"
3790 _debug _deactivated_account_path "$_deactivated_account_path"
3791 if mkdir -p "$_deactivated_account_path"; then
3792 _info "Moving deactivated account info to $_deactivated_account_path/"
3793 mv "$CA_CONF" "$_deactivated_account_path/"
3794 mv "$ACCOUNT_JSON_PATH" "$_deactivated_account_path/"
3795 mv "$ACCOUNT_KEY_PATH" "$_deactivated_account_path/"
3796 else
3797 _err "Can not create dir: $_deactivated_account_path, try to remove the deactivated account key."
3798 rm -f "$CA_CONF"
3799 rm -f "$ACCOUNT_JSON_PATH"
3800 rm -f "$ACCOUNT_KEY_PATH"
3801 fi
3802 fi
3803}
3804
a61fe418 3805# domain folder file
3806_findHook() {
3807 _hookdomain="$1"
3808 _hookcat="$2"
3809 _hookname="$3"
3810
c7b16249 3811 if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
3812 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname"
3813 elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
3814 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
b50e701c 3815 elif [ "$_hookdomain" ] && [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
a61fe418 3816 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname"
b50e701c 3817 elif [ "$_hookdomain" ] && [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
a61fe418 3818 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
4c2a3841 3819 elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
a61fe418 3820 d_api="$LE_WORKING_DIR/$_hookname"
4c2a3841 3821 elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
a61fe418 3822 d_api="$LE_WORKING_DIR/$_hookname.sh"
4c2a3841 3823 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
a61fe418 3824 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname"
4c2a3841 3825 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
a61fe418 3826 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
3827 fi
3828
3829 printf "%s" "$d_api"
3830}
3831
f940b2a5 3832#domain
3833__get_domain_new_authz() {
3834 _gdnd="$1"
3835 _info "Getting new-authz for domain" "$_gdnd"
40ef86f4 3836 _initAPI
f940b2a5 3837 _Max_new_authz_retry_times=5
3838 _authz_i=0
4c2a3841 3839 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
efd96153 3840 _debug "Try new-authz for the $_authz_i time."
48d9a8c1 3841 if ! _send_signed_request "${ACME_NEW_AUTHZ}" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
f940b2a5 3842 _err "Can not get domain new authz."
3843 return 1
3844 fi
5413bf87 3845 if _contains "$response" "No registration exists matching provided key"; then
3846 _err "It seems there is an error, but it's recovered now, please try again."
3847 _err "If you see this message for a second time, please report bug: $(__green "$PROJECT")"
3848 _clearcaconf "CA_KEY_HASH"
3849 break
3850 fi
4c2a3841 3851 if ! _contains "$response" "An error occurred while processing your request"; then
f940b2a5 3852 _info "The new-authz request is ok."
3853 break
3854 fi
3855 _authz_i="$(_math "$_authz_i" + 1)"
9e45ac93 3856 _info "The server is busy, Sleep $_authz_i to retry."
f940b2a5 3857 _sleep "$_authz_i"
4c2a3841 3858 done
f940b2a5 3859
4c2a3841 3860 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
efd96153 3861 _err "new-authz retry reach the max $_Max_new_authz_retry_times times."
f940b2a5 3862 fi
4c2a3841 3863
78915896 3864 if [ "$code" ] && [ "$code" != '201' ]; then
f940b2a5 3865 _err "new-authz error: $response"
3866 return 1
3867 fi
3868
3869}
3870
58e4d337 3871#uri keyAuthorization
f94433e5 3872__trigger_validation() {
8bd12ed0 3873 _debug2 "Trigger domain validation."
58e4d337 3874 _t_url="$1"
3875 _debug2 _t_url "$_t_url"
3876 _t_key_authz="$2"
3877 _debug2 _t_key_authz "$_t_key_authz"
920cab6f
K
3878 _t_vtype="$3"
3879 _debug2 _t_vtype "$_t_vtype"
c1151b0d 3880 if [ "$ACME_VERSION" = "2" ]; then
e7f7e96d 3881 _send_signed_request "$_t_url" "{}"
c1151b0d 3882 else
8bd12ed0 3883 _send_signed_request "$_t_url" "{\"resource\": \"challenge\", \"type\": \"$_t_vtype\", \"keyAuthorization\": \"$_t_key_authz\"}"
c1151b0d 3884 fi
58e4d337 3885}
3886
b5ca9bba 3887#endpoint domain type
10eec7d4 3888_ns_lookup_impl() {
b5ca9bba 3889 _ns_ep="$1"
3890 _ns_domain="$2"
3891 _ns_type="$3"
3892 _debug2 "_ns_ep" "$_ns_ep"
3893 _debug2 "_ns_domain" "$_ns_domain"
3894 _debug2 "_ns_type" "$_ns_type"
3895
3896 response="$(_H1="accept: application/dns-json" _get "$_ns_ep?name=$_ns_domain&type=$_ns_type")"
3897 _ret=$?
3898 _debug2 "response" "$response"
3899 if [ "$_ret" != "0" ]; then
3900 return $_ret
3901 fi
3902 _answers="$(echo "$response" | tr '{}' '<>' | _egrep_o '"Answer":\[[^]]*]' | tr '<>' '\n\n')"
3903 _debug2 "_answers" "$_answers"
3904 echo "$_answers"
3905}
3906
3907#domain, type
3908_ns_lookup_cf() {
3909 _cf_ld="$1"
3910 _cf_ld_type="$2"
3911 _cf_ep="https://cloudflare-dns.com/dns-query"
10eec7d4 3912 _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type"
b5ca9bba 3913}
3914
3915#domain, type
3916_ns_purge_cf() {
3917 _cf_d="$1"
3918 _cf_d_type="$2"
3919 _debug "Cloudflare purge $_cf_d_type record for domain $_cf_d"
b9b2cd27 3920 _cf_purl="https://cloudflare-dns.com/api/v1/purge?domain=$_cf_d&type=$_cf_d_type"
b5ca9bba 3921 response="$(_post "" "$_cf_purl")"
3922 _debug2 response "$response"
3923}
3924
10eec7d4 3925#checks if cf server is available
3926_ns_is_available_cf() {
e65144a1 3927 if _get "https://cloudflare-dns.com" "" 1 >/dev/null 2>&1; then
3928 return 0
3929 else
3930 return 1
3931 fi
3932}
3933
3934_ns_is_available_google() {
3935 if _get "https://dns.google" "" 1 >/dev/null 2>&1; then
10eec7d4 3936 return 0
3937 else
3938 return 1
3939 fi
3940}
3941
3942#domain, type
3943_ns_lookup_google() {
3944 _cf_ld="$1"
3945 _cf_ld_type="$2"
3946 _cf_ep="https://dns.google/resolve"
3947 _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type"
3948}
3949
e65144a1 3950_ns_is_available_ali() {
3951 if _get "https://dns.alidns.com" "" 1 >/dev/null 2>&1; then
3952 return 0
3953 else
3954 return 1
3955 fi
3956}
3957
3958#domain, type
3959_ns_lookup_ali() {
3960 _cf_ld="$1"
3961 _cf_ld_type="$2"
3962 _cf_ep="https://dns.alidns.com/resolve"
3963 _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type"
3964}
3965
3966_ns_is_available_dp() {
3967 if _get "https://dns.alidns.com" "" 1 >/dev/null 2>&1; then
3968 return 0
3969 else
3970 return 1
3971 fi
3972}
3973
3974#dnspod
3975_ns_lookup_dp() {
3976 _cf_ld="$1"
3977 _cf_ld_type="$2"
3978 _cf_ep="https://doh.pub/dns-query"
3979 _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type"
3980}
3981
10eec7d4 3982#domain, type
3983_ns_lookup() {
3984 if [ -z "$DOH_USE" ]; then
3985 _debug "Detect dns server first."
3986 if _ns_is_available_cf; then
3987 _debug "Use cloudflare doh server"
3988 export DOH_USE=$DOH_CLOUDFLARE
e65144a1 3989 elif _ns_is_available_google; then
10eec7d4 3990 _debug "Use google doh server"
3991 export DOH_USE=$DOH_GOOGLE
e65144a1 3992 elif _ns_is_available_ali; then
3993 _debug "Use aliyun doh server"
3994 export DOH_USE=$DOH_ALI
290beb90 3995 elif _ns_is_available_dp; then
e65144a1 3996 _debug "Use dns pod doh server"
3997 export DOH_USE=$DOH_DP
e0c32ce7 3998 else
3999 _err "No doh"
10eec7d4 4000 fi
4001 fi
4002
4003 if [ "$DOH_USE" = "$DOH_CLOUDFLARE" ] || [ -z "$DOH_USE" ]; then
4004 _ns_lookup_cf "$@"
e65144a1 4005 elif [ "$DOH_USE" = "$DOH_GOOGLE" ]; then
10eec7d4 4006 _ns_lookup_google "$@"
e65144a1 4007 elif [ "$DOH_USE" = "$DOH_ALI" ]; then
4008 _ns_lookup_ali "$@"
4009 elif [ "$DOH_USE" = "$DOH_DP" ]; then
4010 _ns_lookup_dp "$@"
4011 else
4012 _err "Unknown doh provider: DOH_USE=$DOH_USE"
10eec7d4 4013 fi
4014
4015}
4016
b5ca9bba 4017#txtdomain, alias, txt
4018__check_txt() {
4019 _c_txtdomain="$1"
4020 _c_aliasdomain="$2"
4021 _c_txt="$3"
4022 _debug "_c_txtdomain" "$_c_txtdomain"
4023 _debug "_c_aliasdomain" "$_c_aliasdomain"
4024 _debug "_c_txt" "$_c_txt"
10eec7d4 4025 _answers="$(_ns_lookup "$_c_aliasdomain" TXT)"
b5ca9bba 4026 _contains "$_answers" "$_c_txt"
4027
4028}
4029
4030#txtdomain
4031__purge_txt() {
4032 _p_txtdomain="$1"
4033 _debug _p_txtdomain "$_p_txtdomain"
10eec7d4 4034 if [ "$DOH_USE" = "$DOH_CLOUDFLARE" ] || [ -z "$DOH_USE" ]; then
4035 _ns_purge_cf "$_p_txtdomain" "TXT"
4036 else
e65144a1 4037 _debug "no purge api for this doh api, just sleep 5 secs"
10eec7d4 4038 _sleep 5
4039 fi
4040
b5ca9bba 4041}
4042
4043#wait and check each dns entries
4044_check_dns_entries() {
4045 _success_txt=","
4046 _end_time="$(_time)"
4047 _end_time="$(_math "$_end_time" + 1200)" #let's check no more than 20 minutes.
4048
4049 while [ "$(_time)" -le "$_end_time" ]; do
8a24275b 4050 _info "You can use '--dnssleep' to disable public dns checks."
4051 _info "See: $_DNSCHECK_WIKI"
b5ca9bba 4052 _left=""
4053 for entry in $dns_entries; do
4054 d=$(_getfield "$entry" 1)
4055 txtdomain=$(_getfield "$entry" 2)
0093dc3d 4056 txtdomain=$(_idn "$txtdomain")
b5ca9bba 4057 aliasDomain=$(_getfield "$entry" 3)
0093dc3d 4058 aliasDomain=$(_idn "$aliasDomain")
b5ca9bba 4059 txt=$(_getfield "$entry" 5)
4060 d_api=$(_getfield "$entry" 6)
4061 _debug "d" "$d"
4062 _debug "txtdomain" "$txtdomain"
4063 _debug "aliasDomain" "$aliasDomain"
4064 _debug "txt" "$txt"
4065 _debug "d_api" "$d_api"
4066 _info "Checking $d for $aliasDomain"
4067 if _contains "$_success_txt" ",$txt,"; then
4068 _info "Already success, continue next one."
4069 continue
4070 fi
4071
4072 if __check_txt "$txtdomain" "$aliasDomain" "$txt"; then
4073 _info "Domain $d '$aliasDomain' success."
4074 _success_txt="$_success_txt,$txt,"
4075 continue
4076 fi
4077 _left=1
4078 _info "Not valid yet, let's wait 10 seconds and check next one."
b5ca9bba 4079 __purge_txt "$txtdomain"
4080 if [ "$txtdomain" != "$aliasDomain" ]; then
4081 __purge_txt "$aliasDomain"
4082 fi
b9b2cd27 4083 _sleep 10
b5ca9bba 4084 done
4085 if [ "$_left" ]; then
4086 _info "Let's wait 10 seconds and check again".
4087 _sleep 10
4088 else
4089 _info "All success, let's return"
a44ea0dd 4090 return 0
b5ca9bba 4091 fi
4092 done
a44ea0dd 4093 _info "Timed out waiting for DNS."
4094 return 1
b5ca9bba 4095
4096}
4097
e3ebd582 4098#file
12b19165 4099_get_chain_issuers() {
e3ebd582 4100 _cfile="$1"
987571ce 4101 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 4102 ${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 4103 else
12b19165 4104 _cindex=1
4105 for _startn in $(grep -n -- "$BEGIN_CERT" "$_cfile" | cut -d : -f 1); do
4106 _endn="$(grep -n -- "$END_CERT" "$_cfile" | cut -d : -f 1 | _head_n $_cindex | _tail_n 1)"
4107 _debug2 "_startn" "$_startn"
4108 _debug2 "_endn" "$_endn"
4109 if [ "$DEBUG" ]; then
4110 _debug2 "cert$_cindex" "$(sed -n "$_startn,${_endn}p" "$_cfile")"
4111 fi
4112 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/"
4113 _cindex=$(_math $_cindex + 1)
4114 done
4115 fi
4116}
4117
4118#
4119_get_chain_subjects() {
4120 _cfile="$1"
4121 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
4122 ${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
4123 else
4124 _cindex=1
4125 for _startn in $(grep -n -- "$BEGIN_CERT" "$_cfile" | cut -d : -f 1); do
4126 _endn="$(grep -n -- "$END_CERT" "$_cfile" | cut -d : -f 1 | _head_n $_cindex | _tail_n 1)"
4127 _debug2 "_startn" "$_startn"
4128 _debug2 "_endn" "$_endn"
4129 if [ "$DEBUG" ]; then
4130 _debug2 "cert$_cindex" "$(sed -n "$_startn,${_endn}p" "$_cfile")"
4131 fi
4132 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/"
4133 _cindex=$(_math $_cindex + 1)
4134 done
d5d38b33 4135 fi
e3ebd582 4136}
4137
4138#cert issuer
4139_match_issuer() {
4140 _cfile="$1"
4141 _missuer="$2"
12b19165 4142 _fissuers="$(_get_chain_issuers $_cfile)"
d5d38b33 4143 _debug2 _fissuers "$_fissuers"
12b19165 4144 _rootissuer="$(echo "$_fissuers" | _lower_case | _tail_n 1)"
4145 _debug2 _rootissuer "$_rootissuer"
fdb96e91 4146 _missuer="$(echo "$_missuer" | _lower_case)"
12b19165 4147 _contains "$_rootissuer" "$_missuer"
e3ebd582 4148}
4149
3c07f57a 4150#webroot, domain domainlist keylength
4c3b3608 4151issue() {
4c2a3841 4152 if [ -z "$2" ]; then
2e87e64b 4153 _usage "Usage: $PROJECT_ENTRY --issue --domain <domain.tld> --webroot <directory>"
4c3b3608 4154 return 1
4155 fi
49d75a0c 4156 if [ -z "$1" ]; then
4157 _usage "Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc."
4158 return 1
4159 fi
af1cc3b3 4160 _web_roots="$1"
4161 _main_domain="$2"
02140ce7 4162 _alt_domains="$3"
d2cde379 4163
af1cc3b3 4164 if _contains "$_main_domain" ","; then
4165 _main_domain=$(echo "$2,$3" | cut -d , -f 1)
02140ce7 4166 _alt_domains=$(echo "$2,$3" | cut -d , -f 2- | sed "s/,${NO_VALUE}$//")
2aff36e7 4167 fi
674b5088 4168 _debug _main_domain "$_main_domain"
4169 _debug _alt_domains "$_alt_domains"
4170
d9c9114b 4171 _key_length="$4"
85e1f4ea 4172 _real_cert="$5"
4173 _real_key="$6"
4174 _real_ca="$7"
4175 _reload_cmd="$8"
4176 _real_fullchain="$9"
4177 _pre_hook="${10}"
4178 _post_hook="${11}"
4179 _renew_hook="${12}"
4180 _local_addr="${13}"
875625b1 4181 _challenge_alias="${14}"
e3ebd582 4182 _preferred_chain="${15}"
4c2a3841 4183
bd04638d 4184 if [ -z "$_ACME_IS_RENEW" ]; then
d9c9114b 4185 _initpath "$_main_domain" "$_key_length"
43822d37 4186 mkdir -p "$DOMAIN_PATH"
4187 fi
eccec5f6 4188
a0923622 4189 if _hasfield "$_web_roots" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then
4190 _err "$_DNS_MANUAL_ERROR"
4191 return 1
4192 fi
4193
48d9a8c1 4194 _debug "Using ACME_DIRECTORY: $ACME_DIRECTORY"
4195
4196 _initAPI
4197
4c2a3841 4198 if [ -f "$DOMAIN_CONF" ]; then
61623d22 4199 Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
a4270efa 4200 _debug Le_NextRenewTime "$Le_NextRenewTime"
95e06de5 4201 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
bb25febd 4202 _saved_domain=$(_readdomainconf Le_Domain)
4203 _debug _saved_domain "$_saved_domain"
4204 _saved_alt=$(_readdomainconf Le_Alt)
4205 _debug _saved_alt "$_saved_alt"
02140ce7 4206 if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then
bb25febd 4207 _info "Domains not changed."
4208 _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")"
4c2a3841 4209 _info "Add '$(__red '--force')' to force to renew."
bb25febd 4210 return $RENEW_SKIP
4211 else
4212 _info "Domains have changed."
4213 fi
4c3b3608 4214 fi
4215 fi
96a46cfc 4216
af1cc3b3 4217 _savedomainconf "Le_Domain" "$_main_domain"
02140ce7 4218 _savedomainconf "Le_Alt" "$_alt_domains"
af1cc3b3 4219 _savedomainconf "Le_Webroot" "$_web_roots"
4c2a3841 4220
c7257bcf 4221 _savedomainconf "Le_PreHook" "$_pre_hook" "base64"
4222 _savedomainconf "Le_PostHook" "$_post_hook" "base64"
4223 _savedomainconf "Le_RenewHook" "$_renew_hook" "base64"
4c2a3841 4224
85e1f4ea 4225 if [ "$_local_addr" ]; then
4226 _savedomainconf "Le_LocalAddress" "$_local_addr"
72518d48 4227 else
4228 _cleardomainconf "Le_LocalAddress"
4229 fi
875625b1 4230 if [ "$_challenge_alias" ]; then
4231 _savedomainconf "Le_ChallengeAlias" "$_challenge_alias"
4232 else
4233 _cleardomainconf "Le_ChallengeAlias"
4234 fi
e3ebd582 4235 if [ "$_preferred_chain" ]; then
4236 _savedomainconf "Le_Preferred_Chain" "$_preferred_chain" "base64"
4237 else
4238 _cleardomainconf "Le_Preferred_Chain"
4239 fi
6ae0f7f5 4240
a6d22e3b 4241 Le_API="$ACME_DIRECTORY"
4242 _savedomainconf "Le_API" "$Le_API"
4243
389518e1 4244 _info "Using CA: $ACME_DIRECTORY"
02140ce7 4245 if [ "$_alt_domains" = "$NO_VALUE" ]; then
4246 _alt_domains=""
4c3b3608 4247 fi
4c2a3841 4248
d9c9114b 4249 if [ "$_key_length" = "$NO_VALUE" ]; then
4250 _key_length=""
d404e92d 4251 fi
4c2a3841 4252
85e1f4ea 4253 if ! _on_before_issue "$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
0463b5d6 4254 _err "_on_before_issue."
4255 return 1
4c3b3608 4256 fi
0463b5d6 4257
8a29fbc8 4258 _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")"
4259 _debug2 _saved_account_key_hash "$_saved_account_key_hash"
4c2a3841 4260
e8b54a50 4261 if [ -z "$ACCOUNT_URL" ] || [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
57e58ce7 4262 if ! _regAccount "$_accountkeylength"; then
85e1f4ea 4263 _on_issue_err "$_post_hook"
8a29fbc8 4264 return 1
4265 fi
57e58ce7 4266 else
4267 _debug "_saved_account_key_hash is not changed, skip register account."
166096dc 4268 fi
166096dc 4269
4c2a3841 4270 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
10afcaca 4271 _info "Signing from existing CSR."
4272 else
4273 _key=$(_readdomainconf Le_Keylength)
4274 _debug "Read key length:$_key"
c4b2e582 4275 if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ] || [ "$Le_ForceNewDomainKey" = "1" ]; then
d9c9114b 4276 if ! createDomainKey "$_main_domain" "$_key_length"; then
10afcaca 4277 _err "Create domain key error."
4278 _clearup
85e1f4ea 4279 _on_issue_err "$_post_hook"
10afcaca 4280 return 1
4281 fi
4282 fi
4283
02140ce7 4284 if ! _createcsr "$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
10afcaca 4285 _err "Create CSR error."
5ef501c5 4286 _clearup
85e1f4ea 4287 _on_issue_err "$_post_hook"
41e3eafa 4288 return 1
4289 fi
4c3b3608 4290 fi
10afcaca 4291
d9c9114b 4292 _savedomainconf "Le_Keylength" "$_key_length"
4c2a3841 4293
4c3b3608 4294 vlist="$Le_Vlist"
882ac74a 4295 _cleardomainconf "Le_Vlist"
cae203be 4296 _info "Getting domain auth token for each domain"
4c3b3608 4297 sep='#'
9d725af6 4298 dvsep=','
4c2a3841 4299 if [ -z "$vlist" ]; then
d2cde379 4300 if [ "$ACME_VERSION" = "2" ]; then
c1151b0d 4301 #make new order request
0093dc3d 4302 _identifiers="{\"type\":\"dns\",\"value\":\"$(_idn "$_main_domain")\"}"
38f1b4d2 4303 _w_index=1
674b5088 4304 while true; do
dd17124e 4305 d="$(echo "$_alt_domains," | cut -d , -f "$_w_index")"
674b5088 4306 _w_index="$(_math "$_w_index" + 1)"
4307 _debug d "$d"
4308 if [ -z "$d" ]; then
4309 break
c1151b0d 4310 fi
0093dc3d 4311 _identifiers="$_identifiers,{\"type\":\"dns\",\"value\":\"$(_idn "$d")\"}"
c1151b0d 4312 done
4313 _debug2 _identifiers "$_identifiers"
4314 if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
4315 _err "Create new order error."
4316 _clearup
4317 _on_issue_err "$_post_hook"
4318 return 1
4319 fi
05aa26e6 4320 Le_LinkOrder="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n " | cut -d ":" -f 2-)"
fbdc5a0e 4321 _debug Le_LinkOrder "$Le_LinkOrder"
dbc44c08 4322 Le_OrderFinalize="$(echo "$response" | _egrep_o '"finalize" *: *"[^"]*"' | cut -d '"' -f 4)"
d2cde379 4323 _debug Le_OrderFinalize "$Le_OrderFinalize"
4324 if [ -z "$Le_OrderFinalize" ]; then
78915896 4325 _err "Create new order error. Le_OrderFinalize not found. $response"
c1151b0d 4326 _clearup
4327 _on_issue_err "$_post_hook"
4328 return 1
4329 fi
4330
4331 #for dns manual mode
d2cde379 4332 _savedomainconf "Le_OrderFinalize" "$Le_OrderFinalize"
c1151b0d 4333
93de1e49 4334 _authorizations_seg="$(echo "$response" | _json_decode | _egrep_o '"authorizations" *: *\[[^\[]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
c1151b0d 4335 _debug2 _authorizations_seg "$_authorizations_seg"
4336 if [ -z "$_authorizations_seg" ]; then
4337 _err "_authorizations_seg not found."
4338 _clearup
4339 _on_issue_err "$_post_hook"
4340 return 1
4341 fi
4342
4343 #domain and authz map
4344 _authorizations_map=""
f8d22c48 4345 for _authz_url in $(echo "$_authorizations_seg" | tr ',' ' '); do
c1151b0d 4346 _debug2 "_authz_url" "$_authz_url"
0483d841 4347 if ! _send_signed_request "$_authz_url"; then
c1151b0d 4348 _err "get to authz error."
263c38ca 4349 _err "_authorizations_seg" "$_authorizations_seg"
4350 _err "_authz_url" "$_authz_url"
c1151b0d 4351 _clearup
4352 _on_issue_err "$_post_hook"
4353 return 1
4354 fi
4355
4356 response="$(echo "$response" | _normalizeJson)"
4357 _debug2 response "$response"
4358 _d="$(echo "$response" | _egrep_o '"value" *: *"[^"]*"' | cut -d : -f 2 | tr -d ' "')"
72f54ca6 4359 if _contains "$response" "\"wildcard\" *: *true"; then
4360 _d="*.$_d"
4361 fi
c1151b0d 4362 _debug2 _d "$_d"
4363 _authorizations_map="$_d,$response
4364$_authorizations_map"
4365 done
4366 _debug2 _authorizations_map "$_authorizations_map"
4367 fi
4368
c1151b0d 4369 _index=0
a63b05a9 4370 _currentRoot=""
38f1b4d2 4371 _w_index=1
88bbe55b 4372 while true; do
88bbe55b 4373 d="$(echo "$_main_domain,$_alt_domains," | cut -d , -f "$_w_index")"
4374 _w_index="$(_math "$_w_index" + 1)"
4375 _debug d "$d"
4376 if [ -z "$d" ]; then
4377 break
4378 fi
ca7202eb 4379 _info "Getting webroot for domain" "$d"
c1151b0d 4380 _index=$(_math $_index + 1)
af1cc3b3 4381 _w="$(echo $_web_roots | cut -d , -f $_index)"
9d725af6 4382 _debug _w "$_w"
4c2a3841 4383 if [ "$_w" ]; then
a63b05a9 4384 _currentRoot="$_w"
4385 fi
4386 _debug "_currentRoot" "$_currentRoot"
4c2a3841 4387
a63b05a9 4388 vtype="$VTYPE_HTTP"
c1151b0d 4389 #todo, v2 wildcard force to use dns
3881f221 4390 if _startswith "$_currentRoot" "$W_DNS"; then
a63b05a9 4391 vtype="$VTYPE_DNS"
4392 fi
4c2a3841 4393
08681f4a 4394 if [ "$_currentRoot" = "$W_ALPN" ]; then
4395 vtype="$VTYPE_ALPN"
4396 fi
4397
c1151b0d 4398 if [ "$ACME_VERSION" = "2" ]; then
d04c6dd3 4399 _idn_d="$(_idn "$d")"
dbc43550
L
4400 _candidates="$(echo "$_authorizations_map" | grep -i "^$_idn_d,")"
4401 _debug2 _candidates "$_candidates"
4402 if [ "$(echo "$_candidates" | wc -l)" -gt 1 ]; then
4403 for _can in $_candidates; do
d04c6dd3 4404 if _startswith "$(echo "$_can" | tr '.' '|')" "$(echo "$_idn_d" | tr '.' '|'),"; then
dbc43550 4405 _candidates="$_can"
d04c6dd3 4406 break
4407 fi
4408 done
4409 fi
dbc43550 4410 response="$(echo "$_candidates" | sed "s/$_idn_d,//")"
c1151b0d 4411 _debug2 "response" "$response"
4412 if [ -z "$response" ]; then
4413 _err "get to authz error."
263c38ca 4414 _err "_authorizations_map" "$_authorizations_map"
c1151b0d 4415 _clearup
4416 _on_issue_err "$_post_hook"
4417 return 1
4418 fi
4419 else
4420 if ! __get_domain_new_authz "$d"; then
4421 _clearup
4422 _on_issue_err "$_post_hook"
4423 return 1
4424 fi
c4d8fd83 4425 fi
4426
4c2a3841 4427 if [ -z "$thumbprint" ]; then
339a8ad6 4428 thumbprint="$(__calc_account_thumbprint)"
4c3b3608 4429 fi
4430
dbc44c08 4431 entry="$(echo "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
4c3b3608 4432 _debug entry "$entry"
9541ea6a 4433 keyauthorization=""
4c2a3841 4434 if [ -z "$entry" ]; then
9541ea6a 4435 if ! _startswith "$d" '*.'; then
4436 _debug "Not a wildcard domain, lets check whether the validation is already valid."
4437 if echo "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
4438 _debug "$d is already valid."
4439 keyauthorization="$STATE_VERIFIED"
4440 _debug keyauthorization "$keyauthorization"
4441 fi
4442 fi
4443 if [ -z "$keyauthorization" ]; then
fc3a1817 4444 _err "Error, can not get domain token entry $d for $vtype"
9541ea6a 4445 _supported_vtypes="$(echo "$response" | _egrep_o "\"challenges\":\[[^]]*]" | tr '{' "\n" | grep type | cut -d '"' -f 4 | tr "\n" ' ')"
4446 if [ "$_supported_vtypes" ]; then
4447 _err "The supported validation types are: $_supported_vtypes, but you specified: $vtype"
4448 fi
4449 _clearup
4450 _on_issue_err "$_post_hook"
4451 return 1
b51ed9bb 4452 fi
c1151b0d 4453 fi
f8b225e7 4454
9541ea6a 4455 if [ -z "$keyauthorization" ]; then
4456 token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
4457 _debug token "$token"
cae203be 4458
9541ea6a 4459 if [ -z "$token" ]; then
4460 _err "Error, can not get domain token $entry"
4461 _clearup
4462 _on_issue_err "$_post_hook"
4463 return 1
4464 fi
4465 if [ "$ACME_VERSION" = "2" ]; then
4466 uri="$(echo "$entry" | _egrep_o '"url":"[^"]*' | cut -d '"' -f 4 | _head_n 1)"
4467 else
4468 uri="$(echo "$entry" | _egrep_o '"uri":"[^"]*' | cut -d '"' -f 4)"
4469 fi
4470 _debug uri "$uri"
4c3b3608 4471
9541ea6a 4472 if [ -z "$uri" ]; then
4473 _err "Error, can not get domain uri. $entry"
4474 _clearup
4475 _on_issue_err "$_post_hook"
4476 return 1
4477 fi
4478 keyauthorization="$token.$thumbprint"
d35bf517 4479 _debug keyauthorization "$keyauthorization"
9541ea6a 4480
4481 if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
4482 _debug "$d is already verified."
4483 keyauthorization="$STATE_VERIFIED"
4484 _debug keyauthorization "$keyauthorization"
4485 fi
ec603bee 4486 fi
4487
a63b05a9 4488 dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
4c3b3608 4489 _debug dvlist "$dvlist"
4c2a3841 4490
9d725af6 4491 vlist="$vlist$dvlist$dvsep"
4c3b3608 4492
4493 done
9d725af6 4494 _debug vlist "$vlist"
4c3b3608 4495 #add entry
b5ca9bba 4496 dns_entries=""
4c3b3608 4497 dnsadded=""
9d725af6 4498 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
1f7df33e 4499 _alias_index=1
4c2a3841 4500 for ventry in $ventries; do
ca7202eb 4501 d=$(echo "$ventry" | cut -d "$sep" -f 1)
4502 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
4503 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
4504 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
72f54ca6 4505 _debug d "$d"
4c2a3841 4506 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
69212114 4507 _debug "$d is already verified, skip $vtype."
fd536d37 4508 _alias_index="$(_math "$_alias_index" + 1)"
ec603bee 4509 continue
4510 fi
4511
4c2a3841 4512 if [ "$vtype" = "$VTYPE_DNS" ]; then
4c3b3608 4513 dnsadded='0'
72f54ca6 4514 _dns_root_d="$d"
4515 if _startswith "$_dns_root_d" "*."; then
4516 _dns_root_d="$(echo "$_dns_root_d" | sed 's/*.//')"
4517 fi
875625b1 4518 _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")"
4519 _alias_index="$(_math "$_alias_index" + 1)"
4520 _debug "_d_alias" "$_d_alias"
4521 if [ "$_d_alias" ]; then
64821ad4 4522 if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then
4523 txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")"
4524 else
4525 txtdomain="_acme-challenge.$_d_alias"
4526 fi
82b0ebb7 4527 dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$txtdomain$dvsep$_currentRoot"
875625b1 4528 else
4529 txtdomain="_acme-challenge.$_dns_root_d"
82b0ebb7 4530 dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$dvsep$_currentRoot"
875625b1 4531 fi
82b0ebb7 4532
4c3b3608 4533 _debug txtdomain "$txtdomain"
11927a76 4534 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
4c3b3608 4535 _debug txt "$txt"
a61fe418 4536
b50e701c 4537 d_api="$(_findHook "$_dns_root_d" $_SUB_FOLDER_DNSAPI "$_currentRoot")"
4c3b3608 4538 _debug d_api "$d_api"
82b0ebb7 4539
4540 dns_entry="$dns_entry$dvsep$txt${dvsep}$d_api"
4541 _debug2 dns_entry "$dns_entry"
4c2a3841 4542 if [ "$d_api" ]; then
a180b95c 4543 _debug "Found domain api file: $d_api"
4c3b3608 4544 else
3881f221 4545 if [ "$_currentRoot" != "$W_DNS" ]; then
4546 _err "Can not find dns api hook for: $_currentRoot"
4547 _info "You need to add the txt record manually."
4548 fi
5f8b60a0 4549 _info "$(__red "Add the following TXT record:")"
81772fb7 4550 _info "$(__red "Domain: '$(__green "$txtdomain")'")"
4551 _info "$(__red "TXT value: '$(__green "$txt")'")"
4552 _info "$(__red "Please be aware that you prepend _acme-challenge. before your domain")"
4553 _info "$(__red "so the resulting subdomain will be: $txtdomain")"
4c3b3608 4554 continue
4555 fi
4c2a3841 4556
73b8b120 4557 (
ca7202eb 4558 if ! . "$d_api"; then
73b8b120 4559 _err "Load file $d_api error. Please check your api file and try again."
4560 return 1
4561 fi
4c2a3841 4562
158f22f7 4563 addcommand="${_currentRoot}_add"
ca7202eb 4564 if ! _exists "$addcommand"; then
73b8b120 4565 _err "It seems that your api file is not correct, it must have a function named: $addcommand"
4566 return 1
4567 fi
a180b95c 4568 _info "Adding txt value: $txt for domain: $txtdomain"
ca7202eb 4569 if ! $addcommand "$txtdomain" "$txt"; then
73b8b120 4570 _err "Error add txt for domain:$txtdomain"
4571 return 1
4572 fi
a180b95c 4573 _info "The txt record is added: Success."
73b8b120 4574 )
4c2a3841 4575
4576 if [ "$?" != "0" ]; then
ea722da3 4577 _on_issue_err "$_post_hook" "$vlist"
545f2355 4578 _clearup
4c3b3608 4579 return 1
4580 fi
82b0ebb7 4581 dns_entries="$dns_entries$dns_entry
4582"
4583 _debug2 "$dns_entries"
4c3b3608 4584 dnsadded='1'
4585 fi
4586 done
4587
4c2a3841 4588 if [ "$dnsadded" = '0' ]; then
4589 _savedomainconf "Le_Vlist" "$vlist"
4c3b3608 4590 _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
a8b62261 4591 _err "Please add the TXT records to the domains, and re-run with --renew."
5f8b60a0 4592 _on_issue_err "$_post_hook"
545f2355 4593 _clearup
4c3b3608 4594 return 1
4595 fi
4c2a3841 4596
4c3b3608 4597 fi
4c2a3841 4598
b5ca9bba 4599 if [ "$dns_entries" ]; then
4c2a3841 4600 if [ -z "$Le_DNSSleep" ]; then
427c2780 4601 _info "Let's check each DNS record now. Sleep 20 seconds first."
b5ca9bba 4602 _sleep 20
4603 if ! _check_dns_entries; then
4604 _err "check dns error."
4605 _on_issue_err "$_post_hook"
4606 _clearup
4607 return 1
4608 fi
0e38c60d 4609 else
4c2a3841 4610 _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
b5ca9bba 4611 _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
4612 _sleep "$Le_DNSSleep"
0e38c60d 4613 fi
4c3b3608 4614 fi
4c2a3841 4615
5d943a35 4616 NGINX_RESTORE_VLIST=""
4c3b3608 4617 _debug "ok, let's start to verify"
a63b05a9 4618
0463b5d6 4619 _ncIndex=1
9d725af6 4620 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
4c2a3841 4621 for ventry in $ventries; do
ca7202eb 4622 d=$(echo "$ventry" | cut -d "$sep" -f 1)
4623 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
4624 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
4625 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
4626 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
ec603bee 4627
4c2a3841 4628 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
ec603bee 4629 _info "$d is already verified, skip $vtype."
4630 continue
4631 fi
4632
dd068467 4633 _info "Verifying: $d"
4c3b3608 4634 _debug "d" "$d"
4635 _debug "keyauthorization" "$keyauthorization"
4636 _debug "uri" "$uri"
4637 removelevel=""
e22bcf7c 4638 token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
a63b05a9 4639
4640 _debug "_currentRoot" "$_currentRoot"
4641
4c2a3841 4642 if [ "$vtype" = "$VTYPE_HTTP" ]; then
4643 if [ "$_currentRoot" = "$NO_VALUE" ]; then
4c3b3608 4644 _info "Standalone mode server"
85e1f4ea 4645 _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
0463b5d6 4646 _ncIndex="$(_math $_ncIndex + 1)"
3794b5cb 4647 _startserver "$keyauthorization" "$_ncaddr"
4c2a3841 4648 if [ "$?" != "0" ]; then
5ef501c5 4649 _clearup
58e4d337 4650 _on_issue_err "$_post_hook" "$vlist"
6fc1447f 4651 return 1
4652 fi
5dbf664a 4653 sleep 1
ca7202eb 4654 _debug serverproc "$serverproc"
0e44f587 4655 elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
4656 _info "Stateless mode for domain:$d"
4657 _sleep 1
9d725af6 4658 elif _startswith "$_currentRoot" "$NGINX"; then
4659 _info "Nginx mode for domain:$d"
4660 #set up nginx server
4661 FOUND_REAL_NGINX_CONF=""
4662 BACKUP_NGINX_CONF=""
4663 if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then
4664 _clearup
58e4d337 4665 _on_issue_err "$_post_hook" "$vlist"
9d725af6 4666 return 1
03f8d6e9 4667 fi
302c41ed 4668
03f8d6e9 4669 if [ "$FOUND_REAL_NGINX_CONF" ]; then
9d725af6 4670 _realConf="$FOUND_REAL_NGINX_CONF"
4671 _backup="$BACKUP_NGINX_CONF"
4672 _debug _realConf "$_realConf"
5d943a35 4673 NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
9d725af6 4674 fi
4675 _sleep 1
4c3b3608 4676 else
4c2a3841 4677 if [ "$_currentRoot" = "apache" ]; then
6f930641 4678 wellknown_path="$ACME_DIR"
4679 else
a63b05a9 4680 wellknown_path="$_currentRoot/.well-known/acme-challenge"
4c2a3841 4681 if [ ! -d "$_currentRoot/.well-known" ]; then
6f930641 4682 removelevel='1'
4c2a3841 4683 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
6f930641 4684 removelevel='2'
4685 else
4686 removelevel='3'
4687 fi
4c3b3608 4688 fi
6f930641 4689
4c3b3608 4690 _debug wellknown_path "$wellknown_path"
6f930641 4691
4c3b3608 4692 _debug "writing token:$token to $wellknown_path/$token"
4693
4694 mkdir -p "$wellknown_path"
93fc48a2 4695
4c2a3841 4696 if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
93fc48a2 4697 _err "$d:Can not write token to file : $wellknown_path/$token"
4698 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4699 _clearup
58e4d337 4700 _on_issue_err "$_post_hook" "$vlist"
93fc48a2 4701 return 1
4702 fi
4703
4c2a3841 4704 if [ ! "$usingApache" ]; then
44edb2bd 4705 if webroot_owner=$(_stat "$_currentRoot"); then
32fdc196 4706 _debug "Changing owner/group of .well-known to $webroot_owner"
c87cd0de 4707 if ! _exec "chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then
4708 _debug "$(cat "$_EXEC_TEMP_ERR")"
4709 _exec_err >/dev/null 2>&1
4710 fi
32fdc196 4711 else
b54ce310 4712 _debug "not changing owner/group of webroot"
32fdc196 4713 fi
df886ffa 4714 fi
4c2a3841 4715
4c3b3608 4716 fi
08681f4a 4717 elif [ "$vtype" = "$VTYPE_ALPN" ]; then
4718 acmevalidationv1="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")"
4719 _debug acmevalidationv1 "$acmevalidationv1"
4720 if ! _starttlsserver "$d" "" "$Le_TLSPort" "$keyauthorization" "$_ncaddr" "$acmevalidationv1"; then
4721 _err "Start tls server error."
4722 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4723 _clearup
4724 _on_issue_err "$_post_hook" "$vlist"
4725 return 1
4726 fi
4c3b3608 4727 fi
4c2a3841 4728
920cab6f 4729 if ! __trigger_validation "$uri" "$keyauthorization" "$vtype"; then
c4d8fd83 4730 _err "$d:Can not get challenge: $response"
4731 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4732 _clearup
58e4d337 4733 _on_issue_err "$_post_hook" "$vlist"
c4d8fd83 4734 return 1
4735 fi
4c2a3841 4736
c1151b0d 4737 if [ "$code" ] && [ "$code" != '202' ]; then
8bd12ed0 4738 if [ "$code" = '200' ]; then
c1151b0d 4739 _debug "trigger validation code: $code"
4740 else
8bd12ed0
K
4741 _err "$d:Challenge error: $response"
4742 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4743 _clearup
4744 _on_issue_err "$_post_hook" "$vlist"
4745 return 1
c1151b0d 4746 fi
4c3b3608 4747 fi
4c2a3841 4748
6fc1447f 4749 waittimes=0
4c2a3841 4750 if [ -z "$MAX_RETRY_TIMES" ]; then
6fc1447f 4751 MAX_RETRY_TIMES=30
4752 fi
4c2a3841 4753
4754 while true; do
0c538f75 4755 waittimes=$(_math "$waittimes" + 1)
4c2a3841 4756 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
6fc1447f 4757 _err "$d:Timeout"
4758 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4759 _clearup
58e4d337 4760 _on_issue_err "$_post_hook" "$vlist"
6fc1447f 4761 return 1
4762 fi
4c2a3841 4763
5dbf664a 4764 _debug "sleep 2 secs to verify"
4765 sleep 2
4c3b3608 4766 _debug "checking"
0483d841 4767 if [ "$ACME_VERSION" = "2" ]; then
4768 _send_signed_request "$uri"
4769 else
4770 response="$(_get "$uri")"
4771 fi
4c2a3841 4772 if [ "$?" != "0" ]; then
c60883ef 4773 _err "$d:Verify error:$response"
a63b05a9 4774 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 4775 _clearup
58e4d337 4776 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 4777 return 1
4778 fi
9aaf36cd 4779 _debug2 original "$response"
4c2a3841 4780
4781 response="$(echo "$response" | _normalizeJson)"
7012b91f 4782 _debug2 response "$response"
4c2a3841 4783
4784 status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
4c2a3841 4785
aede5c48 4786 if _contains "$status" "invalid"; then
d0d74907 4787 error="$(echo "$response" | _egrep_o '"error":\{[^\}]*')"
4c2a3841 4788 _debug2 error "$error"
4789 errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
4790 _debug2 errordetail "$errordetail"
4791 if [ "$errordetail" ]; then
4792 _err "$d:Verify error:$errordetail"
4793 else
4794 _err "$d:Verify error:$error"
4795 fi
4796 if [ "$DEBUG" ]; then
4797 if [ "$vtype" = "$VTYPE_HTTP" ]; then
4798 _debug "Debug: get token url."
4799 _get "http://$d/.well-known/acme-challenge/$token" "" 1
4800 fi
4801 fi
a63b05a9 4802 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 4803 _clearup
58e4d337 4804 _on_issue_err "$_post_hook" "$vlist"
4c2a3841 4805 return 1
4c3b3608 4806 fi
4c2a3841 4807
aede5c48 4808 if _contains "$status" "valid"; then
4809 _info "$(__green Success)"
4810 _stopserver "$serverproc"
4811 serverproc=""
4812 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4813 break
4814 fi
4815
4c2a3841 4816 if [ "$status" = "pending" ]; then
4c3b3608 4817 _info "Pending"
93740c99 4818 elif [ "$status" = "processing" ]; then
4819 _info "Processing"
4c3b3608 4820 else
4c2a3841 4821 _err "$d:Verify error:$response"
a63b05a9 4822 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 4823 _clearup
58e4d337 4824 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 4825 return 1
4826 fi
4c2a3841 4827
4c3b3608 4828 done
4c2a3841 4829
4c3b3608 4830 done
4831
4832 _clearup
4833 _info "Verify finished, start to sign."
11927a76 4834 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)"
4c2a3841 4835
c1151b0d 4836 if [ "$ACME_VERSION" = "2" ]; then
bd04638d 4837 _info "Lets finalize the order."
4838 _info "Le_OrderFinalize" "$Le_OrderFinalize"
d2cde379 4839 if ! _send_signed_request "${Le_OrderFinalize}" "{\"csr\": \"$der\"}"; then
c1151b0d 4840 _err "Sign failed."
4841 _on_issue_err "$_post_hook"
4842 return 1
4843 fi
4844 if [ "$code" != "200" ]; then
e7f7e96d 4845 _err "Sign failed, finalize code is not 200."
668c43ab 4846 _err "$response"
c1151b0d 4847 _on_issue_err "$_post_hook"
4848 return 1
4849 fi
e7f7e96d 4850 if [ -z "$Le_LinkOrder" ]; then
0c9c1ae6 4851 Le_LinkOrder="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n \t" | cut -d ":" -f 2-)"
e7f7e96d 4852 fi
fbdc5a0e 4853
e7f7e96d 4854 _savedomainconf "Le_LinkOrder" "$Le_LinkOrder"
4855
4856 _link_cert_retry=0
ff9be30f 4857 _MAX_CERT_RETRY=30
a3d8b993 4858 while [ "$_link_cert_retry" -lt "$_MAX_CERT_RETRY" ]; do
e7f7e96d 4859 if _contains "$response" "\"status\":\"valid\""; then
4860 _debug "Order status is valid."
d0d74907 4861 Le_LinkCert="$(echo "$response" | _egrep_o '"certificate" *: *"[^"]*"' | cut -d '"' -f 4)"
e7f7e96d 4862 _debug Le_LinkCert "$Le_LinkCert"
4863 if [ -z "$Le_LinkCert" ]; then
4864 _err "Sign error, can not find Le_LinkCert"
4865 _err "$response"
4866 _on_issue_err "$_post_hook"
4867 return 1
4868 fi
4869 break
4870 elif _contains "$response" "\"processing\""; then
4871 _info "Order status is processing, lets sleep and retry."
1fe8235a 4872 _retryafter=$(echo "$responseHeaders" | grep -i "^Retry-After *:" | cut -d : -f 2 | tr -d ' ' | tr -d '\r')
4873 _debug "_retryafter" "$_retryafter"
4874 if [ "$_retryafter" ]; then
4875 _info "Retry after: $_retryafter"
4876 _sleep $_retryafter
4877 else
4878 _sleep 2
4879 fi
e7f7e96d 4880 else
4881 _err "Sign error, wrong status"
4882 _err "$response"
4883 _on_issue_err "$_post_hook"
4884 return 1
4885 fi
fbdc5a0e 4886 #the order is processing, so we are going to poll order status
4887 if [ -z "$Le_LinkOrder" ]; then
4888 _err "Sign error, can not get order link location header"
4889 _err "responseHeaders" "$responseHeaders"
4890 _on_issue_err "$_post_hook"
4891 return 1
4892 fi
4893 _info "Polling order status: $Le_LinkOrder"
e7f7e96d 4894 if ! _send_signed_request "$Le_LinkOrder"; then
4895 _err "Sign failed, can not post to Le_LinkOrder cert:$Le_LinkOrder."
4896 _err "$response"
4897 _on_issue_err "$_post_hook"
4898 return 1
4899 fi
4900 _link_cert_retry="$(_math $_link_cert_retry + 1)"
4901 done
4c2a3841 4902
e7f7e96d 4903 if [ -z "$Le_LinkCert" ]; then
4904 _err "Sign failed, can not get Le_LinkCert, retry time limit."
4905 _err "$response"
4906 _on_issue_err "$_post_hook"
4907 return 1
4908 fi
bd04638d 4909 _info "Downloading cert."
4910 _info "Le_LinkCert" "$Le_LinkCert"
f2acdd27 4911 if ! _send_signed_request "$Le_LinkCert"; then
668c43ab 4912 _err "Sign failed, can not download cert:$Le_LinkCert."
4913 _err "$response"
c1151b0d 4914 _on_issue_err "$_post_hook"
4915 return 1
4916 fi
4c3b3608 4917
f2acdd27 4918 echo "$response" >"$CERT_PATH"
e3ebd582 4919 _split_cert_chain "$CERT_PATH" "$CERT_FULLCHAIN_PATH" "$CA_CERT_PATH"
4920
d5d38b33 4921 if [ "$_preferred_chain" ] && [ -f "$CERT_FULLCHAIN_PATH" ]; then
12b19165 4922 if [ "$DEBUG" ]; then
4923 _debug "default chain issuers: " "$(_get_chain_issuers "$CERT_FULLCHAIN_PATH")"
4924 fi
d5d38b33 4925 if ! _match_issuer "$CERT_FULLCHAIN_PATH" "$_preferred_chain"; then
e3ebd582 4926 rels="$(echo "$responseHeaders" | tr -d ' <>' | grep -i "^link:" | grep -i 'rel="alternate"' | cut -d : -f 2- | cut -d ';' -f 1)"
4927 _debug2 "rels" "$rels"
4928 for rel in $rels; do
4929 _info "Try rel: $rel"
4930 if ! _send_signed_request "$rel"; then
4931 _err "Sign failed, can not download cert:$rel"
4932 _err "$response"
4933 continue
4934 fi
4935 _relcert="$CERT_PATH.alt"
4936 _relfullchain="$CERT_FULLCHAIN_PATH.alt"
4937 _relca="$CA_CERT_PATH.alt"
4938 echo "$response" >"$_relcert"
4939 _split_cert_chain "$_relcert" "$_relfullchain" "$_relca"
12b19165 4940 if [ "$DEBUG" ]; then
4941 _debug "rel chain issuers: " "$(_get_chain_issuers "$_relfullchain")"
4942 fi
d5d38b33 4943 if _match_issuer "$_relfullchain" "$_preferred_chain"; then
e3ebd582 4944 _info "Matched issuer in: $rel"
4945 cat $_relcert >"$CERT_PATH"
4946 cat $_relfullchain >"$CERT_FULLCHAIN_PATH"
4947 cat $_relca >"$CA_CERT_PATH"
12b19165 4948 rm -f "$_relcert"
4949 rm -f "$_relfullchain"
4950 rm -f "$_relca"
e3ebd582 4951 break
4952 fi
12b19165 4953 rm -f "$_relcert"
4954 rm -f "$_relfullchain"
4955 rm -f "$_relca"
e3ebd582 4956 done
4957 fi
1c35f46b 4958 fi
c1151b0d 4959 else
4960 if ! _send_signed_request "${ACME_NEW_ORDER}" "{\"resource\": \"$ACME_NEW_ORDER_RES\", \"csr\": \"$der\"}" "needbase64"; then
323febe8 4961 _err "Sign failed. $response"
c1151b0d 4962 _on_issue_err "$_post_hook"
4963 return 1
4964 fi
4965 _rcert="$response"
36a7a840 4966 Le_LinkCert="$(grep -i '^Location.*$' "$HTTP_HEADER" | _tail_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
4c2a3841 4967 echo "$BEGIN_CERT" >"$CERT_PATH"
4c3b3608 4968
72518d48 4969 #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
4970 # _debug "Get cert failed. Let's try last response."
3c07f57a 4971 # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
72518d48 4972 #fi
4c2a3841 4973
4974 if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then
72518d48 4975 _debug "Try cert link."
4c2a3841 4976 _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH"
d404e92d 4977 fi
4978
4c2a3841 4979 echo "$END_CERT" >>"$CERT_PATH"
c1151b0d 4980 fi
4981
4982 _debug "Le_LinkCert" "$Le_LinkCert"
4983 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
4984
183063a2 4985 if [ -z "$Le_LinkCert" ] || ! _checkcert "$CERT_PATH"; then
716f7277 4986 response="$(echo "$response" | _dbase64 "multiline" | tr -d '\0' | _normalizeJson)"
183063a2 4987 _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
4988 _on_issue_err "$_post_hook"
4989 return 1
4990 fi
4991
c1151b0d 4992 if [ "$Le_LinkCert" ]; then
43822d37 4993 _info "$(__green "Cert success.")"
4c3b3608 4994 cat "$CERT_PATH"
5980ebc7 4995
4c2a3841 4996 _info "Your cert is in $(__green " $CERT_PATH ")"
4997
4998 if [ -f "$CERT_KEY_PATH" ]; then
4999 _info "Your cert key is in $(__green " $CERT_KEY_PATH ")"
5980ebc7 5000 fi
5001
bd04638d 5002 if [ ! "$USER_PATH" ] || [ ! "$_ACME_IN_CRON" ]; then
281aa349 5003 USER_PATH="$PATH"
5004 _saveaccountconf "USER_PATH" "$USER_PATH"
5005 fi
4c3b3608 5006 fi
4c3b3608 5007
1c35f46b 5008 if [ "$ACME_VERSION" = "2" ]; then
5009 _debug "v2 chain."
5010 else
183063a2 5011 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
1c35f46b 5012 Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
d8ba26e6 5013
1c35f46b 5014 if [ "$Le_LinkIssuer" ]; then
5015 if ! _contains "$Le_LinkIssuer" ":"; then
5016 _info "$(__red "Relative issuer link found.")"
5017 Le_LinkIssuer="$_ACME_SERVER_HOST$Le_LinkIssuer"
d8ba26e6 5018 fi
1c35f46b 5019 _debug Le_LinkIssuer "$Le_LinkIssuer"
5020 _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
d8ba26e6 5021
1c35f46b 5022 _link_issuer_retry=0
5023 _MAX_ISSUER_RETRY=5
5024 while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do
5025 _debug _link_issuer_retry "$_link_issuer_retry"
5026 if [ "$ACME_VERSION" = "2" ]; then
0483d841 5027 if _send_signed_request "$Le_LinkIssuer"; then
5028 echo "$response" >"$CA_CERT_PATH"
1c35f46b 5029 break
5030 fi
5031 else
5032 if _get "$Le_LinkIssuer" >"$CA_CERT_PATH.der"; then
5033 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
5034 _base64 "multiline" <"$CA_CERT_PATH.der" >>"$CA_CERT_PATH"
5035 echo "$END_CERT" >>"$CA_CERT_PATH"
0f120c41 5036 if ! _checkcert "$CA_CERT_PATH"; then
183063a2 5037 _err "Can not get the ca cert."
5038 break
5039 fi
1c35f46b 5040 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
5041 rm -f "$CA_CERT_PATH.der"
5042 break
5043 fi
c1151b0d 5044 fi
1c35f46b 5045 _link_issuer_retry=$(_math $_link_issuer_retry + 1)
5046 _sleep "$_link_issuer_retry"
5047 done
5048 if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then
5049 _err "Max retry for issuer ca cert is reached."
d8ba26e6 5050 fi
1c35f46b 5051 else
5052 _debug "No Le_LinkIssuer header found."
d8ba26e6 5053 fi
4c3b3608 5054 fi
1c35f46b 5055 [ -f "$CA_CERT_PATH" ] && _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")"
5056 [ -f "$CERT_FULLCHAIN_PATH" ] && _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")"
4c2a3841 5057
3aae1ae3 5058 Le_CertCreateTime=$(_time)
4c2a3841 5059 _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
5060
5061 Le_CertCreateTimeStr=$(date -u)
5062 _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
5063
ec67a1b2 5064 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ]; then
5065 Le_RenewalDays="$DEFAULT_RENEW"
054cb72e 5066 else
4c2a3841 5067 _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
13d7cae9 5068 fi
4c2a3841 5069
5070 if [ "$CA_BUNDLE" ]; then
78009539
PS
5071 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
5072 else
5073 _clearaccountconf "CA_BUNDLE"
5074 fi
5075
2aa75f03 5076 if [ "$CA_PATH" ]; then
5077 _saveaccountconf CA_PATH "$CA_PATH"
5078 else
5079 _clearaccountconf "CA_PATH"
5080 fi
78009539 5081
4c2a3841 5082 if [ "$HTTPS_INSECURE" ]; then
fac1e367 5083 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
5084 else
4c2a3841 5085 _clearaccountconf "HTTPS_INSECURE"
13d7cae9 5086 fi
00a50605 5087
4c2a3841 5088 if [ "$Le_Listen_V4" ]; then
5089 _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
50827188 5090 _cleardomainconf Le_Listen_V6
4c2a3841 5091 elif [ "$Le_Listen_V6" ]; then
5092 _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
50827188 5093 _cleardomainconf Le_Listen_V4
5094 fi
f6dcd989 5095
c4b2e582 5096 if [ "$Le_ForceNewDomainKey" = "1" ]; then
5097 _savedomainconf "Le_ForceNewDomainKey" "$Le_ForceNewDomainKey"
5098 else
5099 _cleardomainconf Le_ForceNewDomainKey
5100 fi
5101
ca7202eb 5102 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
4c2a3841 5103
ca7202eb 5104 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
4c2a3841 5105 _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
5106
ca7202eb 5107 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
4c2a3841 5108 _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
f6dcd989 5109
85e1f4ea 5110 if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
5111 _savedomainconf "Le_RealCertPath" "$_real_cert"
5112 _savedomainconf "Le_RealCACertPath" "$_real_ca"
5113 _savedomainconf "Le_RealKeyPath" "$_real_key"
7690f73e 5114 _savedomainconf "Le_ReloadCmd" "$_reload_cmd" "base64"
85e1f4ea 5115 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
ce8dca7a 5116 if ! _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"; then
5117 return 1
5118 fi
01f54558 5119 fi
4c0d3f1b 5120
ce8dca7a 5121 if ! _on_issue_success "$_post_hook" "$_renew_hook"; then
5122 _err "Call hook error."
5123 return 1
5124 fi
4c3b3608 5125}
5126
d73438a3 5127#in_out_cert out_fullchain out_ca
e3ebd582 5128_split_cert_chain() {
5129 _certf="$1"
5130 _fullchainf="$2"
5131 _caf="$3"
5132 if [ "$(grep -- "$BEGIN_CERT" "$_certf" | wc -l)" -gt "1" ]; then
5133 _debug "Found cert chain"
5134 cat "$_certf" >"$_fullchainf"
5135 _end_n="$(grep -n -- "$END_CERT" "$_fullchainf" | _head_n 1 | cut -d : -f 1)"
5136 _debug _end_n "$_end_n"
5137 sed -n "1,${_end_n}p" "$_fullchainf" >"$_certf"
5138 _end_n="$(_math $_end_n + 1)"
5139 sed -n "${_end_n},9999p" "$_fullchainf" >"$_caf"
5140 fi
5141}
5142
43822d37 5143#domain [isEcc]
4c3b3608 5144renew() {
5145 Le_Domain="$1"
4c2a3841 5146 if [ -z "$Le_Domain" ]; then
2e87e64b 5147 _usage "Usage: $PROJECT_ENTRY --renew --domain <domain.tld> [--ecc]"
4c3b3608 5148 return 1
5149 fi
5150
43822d37 5151 _isEcc="$2"
5152
e799ef29 5153 _initpath "$Le_Domain" "$_isEcc"
43822d37 5154
e2053b22 5155 _info "$(__green "Renew: '$Le_Domain'")"
4c2a3841 5156 if [ ! -f "$DOMAIN_CONF" ]; then
2e87e64b 5157 _info "'$Le_Domain' is not an issued domain, skip."
acae0ac2 5158 return $RENEW_SKIP
4c3b3608 5159 fi
4c2a3841 5160
5161 if [ "$Le_RenewalDays" ]; then
1e6b68f5 5162 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
5163 fi
5164
8663fb7e 5165 . "$DOMAIN_CONF"
c5f1cca3 5166 _debug Le_API "$Le_API"
f2add8de 5167
5168 if [ "$Le_API" = "$LETSENCRYPT_CA_V1" ]; then
5169 _cleardomainconf Le_API
5170 Le_API="$DEFAULT_CA"
5171 fi
5172 if [ "$Le_API" = "$LETSENCRYPT_STAGING_CA_V1" ]; then
5173 _cleardomainconf Le_API
5174 Le_API="$DEFAULT_STAGING_CA"
5175 fi
5176
4c2a3841 5177 if [ "$Le_API" ]; then
48d9a8c1 5178 export ACME_DIRECTORY="$Le_API"
c4236e58 5179 #reload ca configs
5180 ACCOUNT_KEY_PATH=""
5181 ACCOUNT_JSON_PATH=""
5182 CA_CONF=""
5183 _debug3 "initpath again."
5184 _initpath "$Le_Domain" "$_isEcc"
5c48e139 5185 fi
4c2a3841 5186
5187 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
e2053b22 5188 _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
5189 _info "Add '$(__red '--force')' to force to renew."
e799ef29 5190 return "$RENEW_SKIP"
4c3b3608 5191 fi
4c2a3841 5192
bd04638d 5193 if [ "$_ACME_IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then
c4d0aec5 5194 _info "Skip invalid cert for: $Le_Domain"
acae0ac2 5195 return $RENEW_SKIP
c4d0aec5 5196 fi
5197
bd04638d 5198 _ACME_IS_RENEW="1"
7690f73e 5199 Le_ReloadCmd="$(_readdomainconf Le_ReloadCmd)"
c7257bcf 5200 Le_PreHook="$(_readdomainconf Le_PreHook)"
5201 Le_PostHook="$(_readdomainconf Le_PostHook)"
5202 Le_RenewHook="$(_readdomainconf Le_RenewHook)"
b7b01999 5203 Le_Preferred_Chain="$(_readdomainconf Le_Preferred_Chain)"
e3ebd582 5204 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 5205 res="$?"
4c2a3841 5206 if [ "$res" != "0" ]; then
e799ef29 5207 return "$res"
a61fe418 5208 fi
4c2a3841 5209
5210 if [ "$Le_DeployHook" ]; then
93bce1b2 5211 _deploy "$Le_Domain" "$Le_DeployHook"
e799ef29 5212 res="$?"
a61fe418 5213 fi
4c2a3841 5214
bd04638d 5215 _ACME_IS_RENEW=""
4c3b3608 5216
e799ef29 5217 return "$res"
4c3b3608 5218}
5219
cc179731 5220#renewAll [stopRenewOnError]
4c3b3608 5221renewAll() {
5222 _initpath
cc179731 5223 _stopRenewOnError="$1"
5224 _debug "_stopRenewOnError" "$_stopRenewOnError"
5225 _ret="0"
b50e701c 5226 _success_msg=""
5227 _error_msg=""
5228 _skipped_msg=""
c6b68551 5229 _error_level=$NOTIFY_LEVEL_SKIP
5230 _notify_code=$RENEW_SKIP
f803c6c0 5231 _set_level=${NOTIFY_LEVEL:-$NOTIFY_LEVEL_DEFAULT}
5232 _debug "_set_level" "$_set_level"
e591d5cf 5233 for di in "${CERT_HOME}"/*.*/; do
5234 _debug di "$di"
44483dba 5235 if ! [ -d "$di" ]; then
2e87e64b 5236 _debug "Not a directory, skip: $di"
3498a585 5237 continue
5238 fi
e591d5cf 5239 d=$(basename "$di")
201aa244 5240 _debug d "$d"
43822d37 5241 (
201aa244 5242 if _endswith "$d" "$ECC_SUFFIX"; then
5243 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
5244 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 5245 fi
5246 renew "$d" "$_isEcc"
4d2f38b0 5247 )
cc179731 5248 rc="$?"
5249 _debug "Return code: $rc"
c6b68551 5250 if [ "$rc" = "0" ]; then
5251 if [ $_error_level -gt $NOTIFY_LEVEL_RENEW ]; then
5252 _error_level="$NOTIFY_LEVEL_RENEW"
5253 _notify_code=0
5254 fi
bd04638d 5255 if [ "$_ACME_IN_CRON" ]; then
f803c6c0 5256 if [ $_set_level -ge $NOTIFY_LEVEL_RENEW ]; then
c6b68551 5257 if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then
5258 _send_notify "Renew $d success" "Good, the cert is renewed." "$NOTIFY_HOOK" 0
5259 fi
5260 fi
5261 fi
5262 _success_msg="${_success_msg} $d
b50e701c 5263"
c6b68551 5264 elif [ "$rc" = "$RENEW_SKIP" ]; then
5265 if [ $_error_level -gt $NOTIFY_LEVEL_SKIP ]; then
5266 _error_level="$NOTIFY_LEVEL_SKIP"
5267 _notify_code=$RENEW_SKIP
5268 fi
bd04638d 5269 if [ "$_ACME_IN_CRON" ]; then
f803c6c0 5270 if [ $_set_level -ge $NOTIFY_LEVEL_SKIP ]; then
c6b68551 5271 if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then
5272 _send_notify "Renew $d skipped" "Good, the cert is skipped." "$NOTIFY_HOOK" "$RENEW_SKIP"
5273 fi
b50e701c 5274 fi
cc179731 5275 fi
c6b68551 5276 _info "Skipped $d"
5277 _skipped_msg="${_skipped_msg} $d
5278"
b50e701c 5279 else
c6b68551 5280 if [ $_error_level -gt $NOTIFY_LEVEL_ERROR ]; then
5281 _error_level="$NOTIFY_LEVEL_ERROR"
5282 _notify_code=1
5283 fi
bd04638d 5284 if [ "$_ACME_IN_CRON" ]; then
f803c6c0 5285 if [ $_set_level -ge $NOTIFY_LEVEL_ERROR ]; then
c6b68551 5286 if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then
5287 _send_notify "Renew $d error" "There is an error." "$NOTIFY_HOOK" 1
5288 fi
5289 fi
5290 fi
5291 _error_msg="${_error_msg} $d
b50e701c 5292"
c6b68551 5293 if [ "$_stopRenewOnError" ]; then
5294 _err "Error renew $d, stop now."
5295 _ret="$rc"
5296 break
5297 else
5298 _ret="$rc"
5299 _err "Error renew $d."
5300 fi
cc179731 5301 fi
4c3b3608 5302 done
c6b68551 5303 _debug _error_level "$_error_level"
a2738e85 5304 _debug _set_level "$_set_level"
bd04638d 5305 if [ "$_ACME_IN_CRON" ] && [ $_error_level -le $_set_level ]; then
b50e701c 5306 if [ -z "$NOTIFY_MODE" ] || [ "$NOTIFY_MODE" = "$NOTIFY_MODE_BULK" ]; then
5307 _msg_subject="Renew"
5308 if [ "$_error_msg" ]; then
5309 _msg_subject="${_msg_subject} Error"
c6b68551 5310 _msg_data="Error certs:
5311${_error_msg}
5312"
b50e701c 5313 fi
5314 if [ "$_success_msg" ]; then
5315 _msg_subject="${_msg_subject} Success"
c6b68551 5316 _msg_data="${_msg_data}Success certs:
5317${_success_msg}
5318"
b50e701c 5319 fi
5320 if [ "$_skipped_msg" ]; then
5321 _msg_subject="${_msg_subject} Skipped"
c6b68551 5322 _msg_data="${_msg_data}Skipped certs:
5323${_skipped_msg}
b50e701c 5324"
c6b68551 5325 fi
5326
5327 _send_notify "$_msg_subject" "$_msg_data" "$NOTIFY_HOOK" "$_notify_code"
b50e701c 5328 fi
5329 fi
5330
201aa244 5331 return "$_ret"
4c3b3608 5332}
5333
10afcaca 5334#csr webroot
4c2a3841 5335signcsr() {
10afcaca 5336 _csrfile="$1"
5337 _csrW="$2"
5338 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
2e87e64b 5339 _usage "Usage: $PROJECT_ENTRY --sign-csr --csr <csr-file> --webroot <directory>"
10afcaca 5340 return 1
5341 fi
5342
875625b1 5343 _real_cert="$3"
5344 _real_key="$4"
5345 _real_ca="$5"
5346 _reload_cmd="$6"
5347 _real_fullchain="$7"
5348 _pre_hook="${8}"
5349 _post_hook="${9}"
5350 _renew_hook="${10}"
5351 _local_addr="${11}"
5352 _challenge_alias="${12}"
96a95ba9 5353 _preferred_chain="${13}"
875625b1 5354
10afcaca 5355 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 5356 if [ "$?" != "0" ]; then
10afcaca 5357 _err "Can not read subject from csr: $_csrfile"
5358 return 1
5359 fi
ad752b31 5360 _debug _csrsubj "$_csrsubj"
2c9ed4c5 5361 if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.'; then
5362 _info "It seems that the subject: $_csrsubj is not a valid domain name. Drop it."
5363 _csrsubj=""
5364 fi
10afcaca 5365
5366 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 5367 if [ "$?" != "0" ]; then
10afcaca 5368 _err "Can not read domain list from csr: $_csrfile"
5369 return 1
5370 fi
5371 _debug "_csrdomainlist" "$_csrdomainlist"
4c2a3841 5372
5373 if [ -z "$_csrsubj" ]; then
ad752b31 5374 _csrsubj="$(_getfield "$_csrdomainlist" 1)"
5375 _debug _csrsubj "$_csrsubj"
5376 _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
5377 _debug "_csrdomainlist" "$_csrdomainlist"
5378 fi
4c2a3841 5379
5380 if [ -z "$_csrsubj" ]; then
ad752b31 5381 _err "Can not read subject from csr: $_csrfile"
5382 return 1
5383 fi
4c2a3841 5384
10afcaca 5385 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 5386 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 5387 _err "Can not read key length from csr: $_csrfile"
5388 return 1
5389 fi
4c2a3841 5390
cd9fb3b6 5391 if [ -z "$ACME_VERSION" ] && _contains "$_csrsubj,$_csrdomainlist" "*."; then
5392 export ACME_VERSION=2
5393 fi
10afcaca 5394 _initpath "$_csrsubj" "$_csrkeylength"
5395 mkdir -p "$DOMAIN_PATH"
4c2a3841 5396
10afcaca 5397 _info "Copy csr to: $CSR_PATH"
5398 cp "$_csrfile" "$CSR_PATH"
4c2a3841 5399
96a95ba9 5400 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 5401
10afcaca 5402}
5403
5404showcsr() {
4c2a3841 5405 _csrfile="$1"
10afcaca 5406 _csrd="$2"
5407 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
2e87e64b 5408 _usage "Usage: $PROJECT_ENTRY --show-csr --csr <csr-file>"
10afcaca 5409 return 1
5410 fi
5411
5412 _initpath
4c2a3841 5413
10afcaca 5414 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 5415 if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then
10afcaca 5416 _err "Can not read subject from csr: $_csrfile"
5417 return 1
5418 fi
4c2a3841 5419
10afcaca 5420 _info "Subject=$_csrsubj"
5421
5422 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 5423 if [ "$?" != "0" ]; then
10afcaca 5424 _err "Can not read domain list from csr: $_csrfile"
5425 return 1
5426 fi
5427 _debug "_csrdomainlist" "$_csrdomainlist"
5428
5429 _info "SubjectAltNames=$_csrdomainlist"
5430
10afcaca 5431 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 5432 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 5433 _err "Can not read key length from csr: $_csrfile"
5434 return 1
5435 fi
5436 _info "KeyLength=$_csrkeylength"
5437}
5438
95ef046d 5439#listraw domain
6d7eda3e 5440list() {
22ea4004 5441 _raw="$1"
95ef046d 5442 _domain="$2"
6d7eda3e 5443 _initpath
4c2a3841 5444
dcf4f8f6 5445 _sep="|"
4c2a3841 5446 if [ "$_raw" ]; then
95ef046d 5447 if [ -z "$_domain" ]; then
5448 printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}CA${_sep}Created${_sep}Renew"
5449 fi
e591d5cf 5450 for di in "${CERT_HOME}"/*.*/; do
5451 d=$(basename "$di")
201aa244 5452 _debug d "$d"
dcf4f8f6 5453 (
201aa244 5454 if _endswith "$d" "$ECC_SUFFIX"; then
be0df07d 5455 _isEcc="ecc"
201aa244 5456 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 5457 fi
be0df07d 5458 DOMAIN_CONF="$di/$d.conf"
4c2a3841 5459 if [ -f "$DOMAIN_CONF" ]; then
dcf4f8f6 5460 . "$DOMAIN_CONF"
269847d1 5461 _ca="$(_getCAShortName "$Le_API")"
95ef046d 5462 if [ -z "$_domain" ]; then
5463 printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$_ca${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
5464 else
5465 if [ "$_domain" = "$d" ]; then
5466 cat "$DOMAIN_CONF"
5467 fi
5468 fi
dcf4f8f6 5469 fi
5470 )
5471 done
5472 else
4c2a3841 5473 if _exists column; then
95ef046d 5474 list "raw" "$_domain" | column -t -s "$_sep"
22ea4004 5475 else
95ef046d 5476 list "raw" "$_domain" | tr "$_sep" '\t'
22ea4004 5477 fi
dcf4f8f6 5478 fi
6d7eda3e 5479
6d7eda3e 5480}
5481
93bce1b2 5482_deploy() {
5483 _d="$1"
5484 _hooks="$2"
5485
5486 for _d_api in $(echo "$_hooks" | tr ',' " "); do
b50e701c 5487 _deployApi="$(_findHook "$_d" $_SUB_FOLDER_DEPLOY "$_d_api")"
93bce1b2 5488 if [ -z "$_deployApi" ]; then
5489 _err "The deploy hook $_d_api is not found."
5490 return 1
5491 fi
5492 _debug _deployApi "$_deployApi"
5493
5494 if ! (
5495 if ! . "$_deployApi"; then
5496 _err "Load file $_deployApi error. Please check your api file and try again."
5497 return 1
5498 fi
5499
5500 d_command="${_d_api}_deploy"
5501 if ! _exists "$d_command"; then
5502 _err "It seems that your api file is not correct, it must have a function named: $d_command"
5503 return 1
5504 fi
5505
5506 if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
5507 _err "Error deploy for domain:$_d"
5508 return 1
5509 fi
5510 ); then
5511 _err "Deploy error."
5512 return 1
5513 else
5514 _info "$(__green Success)"
5515 fi
5516 done
5517}
5518
5519#domain hooks
a61fe418 5520deploy() {
93bce1b2 5521 _d="$1"
5522 _hooks="$2"
a61fe418 5523 _isEcc="$3"
93bce1b2 5524 if [ -z "$_hooks" ]; then
2e87e64b 5525 _usage "Usage: $PROJECT_ENTRY --deploy --domain <domain.tld> --deploy-hook <hookname> [--ecc] "
a61fe418 5526 return 1
5527 fi
5528
93bce1b2 5529 _initpath "$_d" "$_isEcc"
4c2a3841 5530 if [ ! -d "$DOMAIN_PATH" ]; then
9672c6b8 5531 _err "The domain '$_d' is not a cert name. You must use the cert name to specify the cert to install."
5532 _err "Can not find path:'$DOMAIN_PATH'"
a61fe418 5533 return 1
5534 fi
4c2a3841 5535
93bce1b2 5536 . "$DOMAIN_CONF"
4c2a3841 5537
93bce1b2 5538 _savedomainconf Le_DeployHook "$_hooks"
4c2a3841 5539
93bce1b2 5540 _deploy "$_d" "$_hooks"
a61fe418 5541}
5542
4c3b3608 5543installcert() {
85e1f4ea 5544 _main_domain="$1"
5545 if [ -z "$_main_domain" ]; then
2e87e64b 5546 _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 5547 return 1
5548 fi
5549
85e1f4ea 5550 _real_cert="$2"
5551 _real_key="$3"
5552 _real_ca="$4"
5553 _reload_cmd="$5"
5554 _real_fullchain="$6"
43822d37 5555 _isEcc="$7"
5556
85e1f4ea 5557 _initpath "$_main_domain" "$_isEcc"
4c2a3841 5558 if [ ! -d "$DOMAIN_PATH" ]; then
9672c6b8 5559 _err "The domain '$_main_domain' is not a cert name. You must use the cert name to specify the cert to install."
5560 _err "Can not find path:'$DOMAIN_PATH'"
43822d37 5561 return 1
5562 fi
5563
85e1f4ea 5564 _savedomainconf "Le_RealCertPath" "$_real_cert"
5565 _savedomainconf "Le_RealCACertPath" "$_real_ca"
5566 _savedomainconf "Le_RealKeyPath" "$_real_key"
7690f73e 5567 _savedomainconf "Le_ReloadCmd" "$_reload_cmd" "base64"
85e1f4ea 5568 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
5569
044da37c 5570 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
43822d37 5571}
4c3b3608 5572
044da37c 5573#domain cert key ca fullchain reloadcmd backup-prefix
43822d37 5574_installcert() {
85e1f4ea 5575 _main_domain="$1"
5576 _real_cert="$2"
5577 _real_key="$3"
5578 _real_ca="$4"
044da37c 5579 _real_fullchain="$5"
5580 _reload_cmd="$6"
5581 _backup_prefix="$7"
4c3b3608 5582
85e1f4ea 5583 if [ "$_real_cert" = "$NO_VALUE" ]; then
5584 _real_cert=""
4d2f38b0 5585 fi
85e1f4ea 5586 if [ "$_real_key" = "$NO_VALUE" ]; then
5587 _real_key=""
4d2f38b0 5588 fi
85e1f4ea 5589 if [ "$_real_ca" = "$NO_VALUE" ]; then
5590 _real_ca=""
4d2f38b0 5591 fi
85e1f4ea 5592 if [ "$_reload_cmd" = "$NO_VALUE" ]; then
5593 _reload_cmd=""
4d2f38b0 5594 fi
85e1f4ea 5595 if [ "$_real_fullchain" = "$NO_VALUE" ]; then
5596 _real_fullchain=""
4d2f38b0 5597 fi
4c2a3841 5598
044da37c 5599 _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
5600 mkdir -p "$_backup_path"
5601
85e1f4ea 5602 if [ "$_real_cert" ]; then
5603 _info "Installing cert to:$_real_cert"
bd04638d 5604 if [ -f "$_real_cert" ] && [ ! "$_ACME_IS_RENEW" ]; then
044da37c 5605 cp "$_real_cert" "$_backup_path/cert.bak"
4c3b3608 5606 fi
206be3c1 5607 cat "$CERT_PATH" >"$_real_cert" || return 1
4c3b3608 5608 fi
4c2a3841 5609
85e1f4ea 5610 if [ "$_real_ca" ]; then
5611 _info "Installing CA to:$_real_ca"
5612 if [ "$_real_ca" = "$_real_cert" ]; then
5613 echo "" >>"$_real_ca"
206be3c1 5614 cat "$CA_CERT_PATH" >>"$_real_ca" || return 1
4c3b3608 5615 else
bd04638d 5616 if [ -f "$_real_ca" ] && [ ! "$_ACME_IS_RENEW" ]; then
044da37c 5617 cp "$_real_ca" "$_backup_path/ca.bak"
78552b18 5618 fi
206be3c1 5619 cat "$CA_CERT_PATH" >"$_real_ca" || return 1
4c3b3608 5620 fi
5621 fi
5622
85e1f4ea 5623 if [ "$_real_key" ]; then
5624 _info "Installing key to:$_real_key"
bd04638d 5625 if [ -f "$_real_key" ] && [ ! "$_ACME_IS_RENEW" ]; then
044da37c 5626 cp "$_real_key" "$_backup_path/key.bak"
4c3b3608 5627 fi
82014583 5628 if [ -f "$_real_key" ]; then
206be3c1 5629 cat "$CERT_KEY_PATH" >"$_real_key" || return 1
82014583 5630 else
206be3c1 5631 cat "$CERT_KEY_PATH" >"$_real_key" || return 1
7b92371a 5632 chmod 600 "$_real_key"
82014583 5633 fi
4c3b3608 5634 fi
4c2a3841 5635
85e1f4ea 5636 if [ "$_real_fullchain" ]; then
5637 _info "Installing full chain to:$_real_fullchain"
bd04638d 5638 if [ -f "$_real_fullchain" ] && [ ! "$_ACME_IS_RENEW" ]; then
044da37c 5639 cp "$_real_fullchain" "$_backup_path/fullchain.bak"
a63b05a9 5640 fi
206be3c1 5641 cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain" || return 1
4c2a3841 5642 fi
4c3b3608 5643
85e1f4ea 5644 if [ "$_reload_cmd" ]; then
5645 _info "Run reload cmd: $_reload_cmd"
25555b8c 5646 if (
839bf0e2 5647 export CERT_PATH
5648 export CERT_KEY_PATH
5649 export CA_CERT_PATH
5650 export CERT_FULLCHAIN_PATH
b3f61297 5651 export Le_Domain="$_main_domain"
85e1f4ea 5652 cd "$DOMAIN_PATH" && eval "$_reload_cmd"
839bf0e2 5653 ); then
43822d37 5654 _info "$(__green "Reload success")"
4d2f38b0 5655 else
5656 _err "Reload error for :$Le_Domain"
5657 fi
5658 fi
5659
4c3b3608 5660}
5661
77f96b38 5662__read_password() {
5663 unset _pp
5664 prompt="Enter Password:"
0b04a7f1 5665 while IFS= read -p "$prompt" -r -s -n 1 char; do
5666 if [ "$char" = $'\0' ]; then
4ebad105 5667 break
0b04a7f1 5668 fi
5669 prompt='*'
5670 _pp="$_pp$char"
77f96b38 5671 done
5672 echo "$_pp"
5673}
5674
5675_install_win_taskscheduler() {
5676 _lesh="$1"
5677 _centry="$2"
5678 _randomminute="$3"
5679 if ! _exists cygpath; then
5680 _err "cygpath not found"
5681 return 1
5682 fi
5683 if ! _exists schtasks; then
5684 _err "schtasks.exe is not found, are you on Windows?"
5685 return 1
5686 fi
5687 _winbash="$(cygpath -w $(which bash))"
5688 _debug _winbash "$_winbash"
5689 if [ -z "$_winbash" ]; then
5690 _err "can not find bash path"
5691 return 1
5692 fi
5693 _myname="$(whoami)"
5694 _debug "_myname" "$_myname"
5695 if [ -z "$_myname" ]; then
5696 _err "can not find my user name"
5697 return 1
5698 fi
5699 _debug "_lesh" "$_lesh"
5700
5701 _info "To install scheduler task in your Windows account, you must input your windows password."
5702 _info "$PROJECT_NAME doesn't save your password."
5703 _info "Please input your Windows password for: $(__green "$_myname")"
5704 _password="$(__read_password)"
5705 #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
5706 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
5707 echo
5708
5709}
5710
5711_uninstall_win_taskscheduler() {
5712 if ! _exists schtasks; then
5713 _err "schtasks.exe is not found, are you on Windows?"
5714 return 1
5715 fi
5716 if ! echo SCHTASKS /query /tn "$_WINDOWS_SCHEDULER_NAME" | cmd.exe >/dev/null; then
5717 _debug "scheduler $_WINDOWS_SCHEDULER_NAME is not found."
5718 else
5719 _info "Removing $_WINDOWS_SCHEDULER_NAME"
0b04a7f1 5720 echo SCHTASKS /delete /f /tn "$_WINDOWS_SCHEDULER_NAME" | cmd.exe >/dev/null
77f96b38 5721 fi
5722}
5723
27dbe77f 5724#confighome
4c3b3608 5725installcronjob() {
27dbe77f 5726 _c_home="$1"
4c3b3608 5727 _initpath
415f375c 5728 _CRONTAB="crontab"
77f96b38 5729 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
5730 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
5731 else
5732 _err "Can not install cronjob, $PROJECT_ENTRY not found."
5733 return 1
5734 fi
5735 if [ "$_c_home" ]; then
5736 _c_entry="--config-home \"$_c_home\" "
5737 fi
5738 _t=$(_time)
5739 random_minute=$(_math $_t % 60)
5740
415f375c 5741 if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
5742 _CRONTAB="fcrontab"
5743 fi
77f96b38 5744
415f375c 5745 if ! _exists "$_CRONTAB"; then
77f96b38 5746 if _exists cygpath && _exists schtasks.exe; then
5747 _info "It seems you are on Windows, let's install Windows scheduler task."
5748 if _install_win_taskscheduler "$lesh" "$_c_entry" "$random_minute"; then
5749 _info "Install Windows scheduler task success."
5750 return 0
5751 else
5752 _err "Install Windows scheduler task failed."
5753 return 1
5754 fi
5755 fi
415f375c 5756 _err "crontab/fcrontab doesn't exist, so, we can not install cron jobs."
77546ea5 5757 _err "All your certs will not be renewed automatically."
a7b7355d 5758 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
77546ea5 5759 return 1
5760 fi
4c3b3608 5761 _info "Installing cron job"
415f375c 5762 if ! $_CRONTAB -l | grep "$PROJECT_ENTRY --cron"; then
e2c939fb 5763 if _exists uname && uname -a | grep SunOS >/dev/null; then
415f375c 5764 $_CRONTAB -l | {
4c2a3841 5765 cat
0533bde9 5766 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
415f375c 5767 } | $_CRONTAB --
22ea4004 5768 else
415f375c 5769 $_CRONTAB -l | {
4c2a3841 5770 cat
0533bde9 5771 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
415f375c 5772 } | $_CRONTAB -
22ea4004 5773 fi
4c3b3608 5774 fi
4c2a3841 5775 if [ "$?" != "0" ]; then
4c3b3608 5776 _err "Install cron job failed. You need to manually renew your certs."
5777 _err "Or you can add cronjob by yourself:"
a7b7355d 5778 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4c3b3608 5779 return 1
5780 fi
5781}
5782
5783uninstallcronjob() {
415f375c 5784 _CRONTAB="crontab"
5785 if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
5786 _CRONTAB="fcrontab"
5787 fi
5788
5789 if ! _exists "$_CRONTAB"; then
77f96b38 5790 if _exists cygpath && _exists schtasks.exe; then
5791 _info "It seems you are on Windows, let's uninstall Windows scheduler task."
5792 if _uninstall_win_taskscheduler; then
5793 _info "Uninstall Windows scheduler task success."
5794 return 0
5795 else
5796 _err "Uninstall Windows scheduler task failed."
5797 return 1
5798 fi
5799 fi
37db5b81 5800 return
5801 fi
4c3b3608 5802 _info "Removing cron job"
415f375c 5803 cr="$($_CRONTAB -l | grep "$PROJECT_ENTRY --cron")"
4c2a3841 5804 if [ "$cr" ]; then
5805 if _exists uname && uname -a | grep solaris >/dev/null; then
415f375c 5806 $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB --
22ea4004 5807 else
415f375c 5808 $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB -
22ea4004 5809 fi
a7b7355d 5810 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4c3b3608 5811 _info LE_WORKING_DIR "$LE_WORKING_DIR"
27dbe77f 5812 if _contains "$cr" "--config-home"; then
f5b546b3 5813 LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
5814 _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
27dbe77f 5815 fi
4c2a3841 5816 fi
4c3b3608 5817 _initpath
a7b7355d 5818
4c3b3608 5819}
5820
1041c9f9 5821#domain isECC revokeReason
6cb415f5 5822revoke() {
5823 Le_Domain="$1"
4c2a3841 5824 if [ -z "$Le_Domain" ]; then
2e87e64b 5825 _usage "Usage: $PROJECT_ENTRY --revoke --domain <domain.tld> [--ecc]"
6cb415f5 5826 return 1
5827 fi
4c2a3841 5828
43822d37 5829 _isEcc="$2"
1041c9f9 5830 _reason="$3"
5831 if [ -z "$_reason" ]; then
5832 _reason="0"
5833 fi
c4a375b3 5834 _initpath "$Le_Domain" "$_isEcc"
4c2a3841 5835 if [ ! -f "$DOMAIN_CONF" ]; then
6cb415f5 5836 _err "$Le_Domain is not a issued domain, skip."
4c2a3841 5837 return 1
6cb415f5 5838 fi
4c2a3841 5839
5840 if [ ! -f "$CERT_PATH" ]; then
6cb415f5 5841 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
5842 return 1
5843 fi
6cb415f5 5844
11927a76 5845 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
4c2a3841 5846
5847 if [ -z "$cert" ]; then
6cb415f5 5848 _err "Cert for $Le_Domain is empty found, skip."
5849 return 1
5850 fi
4c2a3841 5851
48d9a8c1 5852 _initAPI
5853
d2cde379 5854 if [ "$ACME_VERSION" = "2" ]; then
1041c9f9 5855 data="{\"certificate\": \"$cert\",\"reason\":$_reason}"
d2cde379 5856 else
5857 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
5858 fi
48d9a8c1 5859 uri="${ACME_REVOKE_CERT}"
6cb415f5 5860
4c2a3841 5861 if [ -f "$CERT_KEY_PATH" ]; then
1befee5a 5862 _info "Try domain key first."
c4a375b3 5863 if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
4c2a3841 5864 if [ -z "$response" ]; then
1befee5a 5865 _info "Revoke success."
c4a375b3 5866 rm -f "$CERT_PATH"
1befee5a 5867 return 0
4c2a3841 5868 else
1befee5a 5869 _err "Revoke error by domain key."
5870 _err "$response"
5871 fi
6cb415f5 5872 fi
4c2a3841 5873 else
eca57bee 5874 _info "Domain key file doesn't exist."
6cb415f5 5875 fi
6cb415f5 5876
1befee5a 5877 _info "Try account key."
6cb415f5 5878
c4a375b3 5879 if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
4c2a3841 5880 if [ -z "$response" ]; then
6cb415f5 5881 _info "Revoke success."
c4a375b3 5882 rm -f "$CERT_PATH"
6cb415f5 5883 return 0
4c2a3841 5884 else
6cb415f5 5885 _err "Revoke error."
c9c31c04 5886 _debug "$response"
6cb415f5 5887 fi
5888 fi
5889 return 1
5890}
4c3b3608 5891
78f0201d 5892#domain ecc
5893remove() {
5894 Le_Domain="$1"
5895 if [ -z "$Le_Domain" ]; then
2e87e64b 5896 _usage "Usage: $PROJECT_ENTRY --remove --domain <domain.tld> [--ecc]"
78f0201d 5897 return 1
5898 fi
5899
5900 _isEcc="$2"
5901
5902 _initpath "$Le_Domain" "$_isEcc"
5903 _removed_conf="$DOMAIN_CONF.removed"
5904 if [ ! -f "$DOMAIN_CONF" ]; then
5905 if [ -f "$_removed_conf" ]; then
5906 _err "$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH"
5907 else
5908 _err "$Le_Domain is not a issued domain, skip."
5909 fi
5910 return 1
5911 fi
5912
5913 if mv "$DOMAIN_CONF" "$_removed_conf"; then
68aea3af 5914 _info "$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)"
78f0201d 5915 _info "You can remove them by yourself."
5916 return 0
5917 else
5918 _err "Remove $Le_Domain failed."
5919 return 1
5920 fi
5921}
5922
0c00e870 5923#domain vtype
5924_deactivate() {
5925 _d_domain="$1"
5926 _d_type="$2"
5927 _initpath
4c2a3841 5928
d2cde379 5929 if [ "$ACME_VERSION" = "2" ]; then
5930 _identifiers="{\"type\":\"dns\",\"value\":\"$_d_domain\"}"
5931 if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
5932 _err "Can not get domain new order."
5933 return 1
5934 fi
d0d74907 5935 _authorizations_seg="$(echo "$response" | _egrep_o '"authorizations" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
d2cde379 5936 _debug2 _authorizations_seg "$_authorizations_seg"
5937 if [ -z "$_authorizations_seg" ]; then
5938 _err "_authorizations_seg not found."
5939 _clearup
5940 _on_issue_err "$_post_hook"
5941 return 1
5942 fi
4c2a3841 5943
d2cde379 5944 authzUri="$_authorizations_seg"
5945 _debug2 "authzUri" "$authzUri"
0483d841 5946 if ! _send_signed_request "$authzUri"; then
d2cde379 5947 _err "get to authz error."
263c38ca 5948 _err "_authorizations_seg" "$_authorizations_seg"
5949 _err "authzUri" "$authzUri"
d2cde379 5950 _clearup
5951 _on_issue_err "$_post_hook"
5952 return 1
5953 fi
4c2a3841 5954
d2cde379 5955 response="$(echo "$response" | _normalizeJson)"
5956 _debug2 response "$response"
5957 _URL_NAME="url"
5958 else
5959 if ! __get_domain_new_authz "$_d_domain"; then
5960 _err "Can not get domain new authz token."
5961 return 1
5962 fi
5963
0712e989 5964 authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ':' -f 2- | tr -d "\r\n")"
d2cde379 5965 _debug "authzUri" "$authzUri"
5966 if [ "$code" ] && [ ! "$code" = '201' ]; then
5967 _err "new-authz error: $response"
5968 return 1
5969 fi
5970 _URL_NAME="uri"
14d7bfda 5971 fi
0c00e870 5972
cc8f2afc 5973 entries="$(echo "$response" | tr '][' '==' | _egrep_o "challenges\": *=[^=]*=" | tr '}{' '\n' | grep "\"status\": *\"valid\"")"
14d7bfda 5974 if [ -z "$entries" ]; then
5975 _info "No valid entries found."
5976 if [ -z "$thumbprint" ]; then
5977 thumbprint="$(__calc_account_thumbprint)"
5978 fi
5979 _debug "Trigger validation."
d2cde379 5980 vtype="$VTYPE_DNS"
d0d74907 5981 entry="$(echo "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
14d7bfda 5982 _debug entry "$entry"
5983 if [ -z "$entry" ]; then
5984 _err "Error, can not get domain token $d"
0c00e870 5985 return 1
5986 fi
d0d74907 5987 token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
14d7bfda 5988 _debug token "$token"
4c2a3841 5989
d0d74907 5990 uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')"
14d7bfda 5991 _debug uri "$uri"
5992
5993 keyauthorization="$token.$thumbprint"
5994 _debug keyauthorization "$keyauthorization"
5995 __trigger_validation "$uri" "$keyauthorization"
5996
5997 fi
5998
5999 _d_i=0
6000 _d_max_retry=$(echo "$entries" | wc -l)
6001 while [ "$_d_i" -lt "$_d_max_retry" ]; do
6002 _info "Deactivate: $_d_domain"
6003 _d_i="$(_math $_d_i + 1)"
6004 entry="$(echo "$entries" | sed -n "${_d_i}p")"
0c00e870 6005 _debug entry "$entry"
4c2a3841 6006
6007 if [ -z "$entry" ]; then
fb2029e7 6008 _info "No more valid entry found."
0c00e870 6009 break
6010 fi
4c2a3841 6011
d0d74907 6012 _vtype="$(echo "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
c4a375b3 6013 _debug _vtype "$_vtype"
0c00e870 6014 _info "Found $_vtype"
6015
199ca77c 6016 uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*\"" | tr -d '" ' | cut -d : -f 2-)"
c4a375b3 6017 _debug uri "$uri"
4c2a3841 6018
6019 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
0c00e870 6020 _info "Skip $_vtype"
6021 continue
6022 fi
4c2a3841 6023
0c00e870 6024 _info "Deactivate: $_vtype"
4c2a3841 6025
d2cde379 6026 if [ "$ACME_VERSION" = "2" ]; then
6027 _djson="{\"status\":\"deactivated\"}"
6028 else
6029 _djson="{\"resource\": \"authz\", \"status\":\"deactivated\"}"
6030 fi
6031
6032 if _send_signed_request "$authzUri" "$_djson" && _contains "$response" '"deactivated"'; then
14d7bfda 6033 _info "Deactivate: $_vtype success."
6034 else
0c00e870 6035 _err "Can not deactivate $_vtype."
14d7bfda 6036 break
0c00e870 6037 fi
4c2a3841 6038
0c00e870 6039 done
6040 _debug "$_d_i"
14d7bfda 6041 if [ "$_d_i" -eq "$_d_max_retry" ]; then
0c00e870 6042 _info "Deactivated success!"
6043 else
6044 _err "Deactivate failed."
6045 fi
6046
6047}
6048
6049deactivate() {
3f4513b3 6050 _d_domain_list="$1"
0c00e870 6051 _d_type="$2"
6052 _initpath
a3bdaa85 6053 _initAPI
3f4513b3 6054 _debug _d_domain_list "$_d_domain_list"
4c2a3841 6055 if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
2e87e64b 6056 _usage "Usage: $PROJECT_ENTRY --deactivate --domain <domain.tld> [--domain <domain2.tld> ...]"
0c00e870 6057 return 1
6058 fi
4c2a3841 6059 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
6060 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
3f4513b3 6061 continue
6062 fi
c4a375b3 6063 if ! _deactivate "$_d_dm" "$_d_type"; then
86c017ec 6064 return 1
6065 fi
3f4513b3 6066 done
0c00e870 6067}
6068
4c3b3608 6069# Detect profile file if not specified as environment variable
6070_detect_profile() {
4c2a3841 6071 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
4c3b3608 6072 echo "$PROFILE"
6073 return
6074 fi
6075
4c3b3608 6076 DETECTED_PROFILE=''
4c3b3608 6077 SHELLTYPE="$(basename "/$SHELL")"
6078
4c2a3841 6079 if [ "$SHELLTYPE" = "bash" ]; then
6080 if [ -f "$HOME/.bashrc" ]; then
4c3b3608 6081 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 6082 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 6083 DETECTED_PROFILE="$HOME/.bash_profile"
6084 fi
4c2a3841 6085 elif [ "$SHELLTYPE" = "zsh" ]; then
4c3b3608 6086 DETECTED_PROFILE="$HOME/.zshrc"
6087 fi
6088
4c2a3841 6089 if [ -z "$DETECTED_PROFILE" ]; then
6090 if [ -f "$HOME/.profile" ]; then
4c3b3608 6091 DETECTED_PROFILE="$HOME/.profile"
4c2a3841 6092 elif [ -f "$HOME/.bashrc" ]; then
4c3b3608 6093 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 6094 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 6095 DETECTED_PROFILE="$HOME/.bash_profile"
4c2a3841 6096 elif [ -f "$HOME/.zshrc" ]; then
4c3b3608 6097 DETECTED_PROFILE="$HOME/.zshrc"
6098 fi
6099 fi
6100
1be222f6 6101 echo "$DETECTED_PROFILE"
4c3b3608 6102}
6103
6104_initconf() {
6105 _initpath
4c2a3841 6106 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
0ca5b799 6107 echo "
d404e92d 6108
d0871bda 6109#LOG_FILE=\"$DEFAULT_LOG_FILE\"
6b500036 6110#LOG_LEVEL=1
5ea6e9c9 6111
251d1c5c 6112#AUTO_UPGRADE=\"1\"
89002ed2 6113
569d6c55 6114#NO_TIMESTAMP=1
5b771039 6115
d5ec5f80 6116 " >"$ACCOUNT_CONF_PATH"
4c3b3608 6117 fi
6118}
6119
c8e9a31e 6120# nocron
c60883ef 6121_precheck() {
c8e9a31e 6122 _nocron="$1"
4c2a3841 6123
6124 if ! _exists "curl" && ! _exists "wget"; then
c60883ef 6125 _err "Please install curl or wget first, we need to access http resources."
4c3b3608 6126 return 1
6127 fi
4c2a3841 6128
6129 if [ -z "$_nocron" ]; then
415f375c 6130 if ! _exists "crontab" && ! _exists "fcrontab"; then
77f96b38 6131 if _exists cygpath && _exists schtasks.exe; then
6132 _info "It seems you are on Windows, we will install Windows scheduler task."
6133 else
6134 _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
6135 _err "We need to set cron job to renew the certs automatically."
6136 _err "Otherwise, your certs will not be able to be renewed automatically."
6137 if [ -z "$FORCE" ]; then
6138 _err "Please add '--force' and try install again to go without crontab."
6139 _err "./$PROJECT_ENTRY --install --force"
6140 return 1
6141 fi
c8e9a31e 6142 fi
77546ea5 6143 fi
4c3b3608 6144 fi
4c2a3841 6145
d8ba26e6 6146 if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then
851fedf7 6147 _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN"
c60883ef 6148 _err "We need openssl to generate keys."
4c3b3608 6149 return 1
6150 fi
4c2a3841 6151
3794b5cb 6152 if ! _exists "socat"; then
6153 _err "It is recommended to install socat first."
6154 _err "We use socat for standalone server if you use standalone mode."
c60883ef 6155 _err "If you don't use standalone mode, just ignore this warning."
6156 fi
4c2a3841 6157
c60883ef 6158 return 0
6159}
6160
0a7c9364 6161_setShebang() {
6162 _file="$1"
6163 _shebang="$2"
4c2a3841 6164 if [ -z "$_shebang" ]; then
43822d37 6165 _usage "Usage: file shebang"
0a7c9364 6166 return 1
6167 fi
6168 cp "$_file" "$_file.tmp"
4c2a3841 6169 echo "$_shebang" >"$_file"
6170 sed -n 2,99999p "$_file.tmp" >>"$_file"
6171 rm -f "$_file.tmp"
0a7c9364 6172}
6173
27dbe77f 6174#confighome
94dc5f33 6175_installalias() {
27dbe77f 6176 _c_home="$1"
94dc5f33 6177 _initpath
6178
6179 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4c2a3841 6180 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
44edb2bd 6181 echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
6182 echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
6183 echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
94dc5f33 6184 fi
6185
27dbe77f 6186 if [ "$_c_home" ]; then
be83a6a3 6187 _c_entry=" --config-home '$_c_home'"
27dbe77f 6188 fi
6189
1786a5e5 6190 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
f5b546b3 6191 if [ "$_c_home" ]; then
6192 _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
d04434e3 6193 else
6194 _sed_i "/^export LE_CONFIG_HOME/d" "$_envfile"
f5b546b3 6195 fi
be83a6a3 6196 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
94dc5f33 6197
6198 _profile="$(_detect_profile)"
4c2a3841 6199 if [ "$_profile" ]; then
94dc5f33 6200 _debug "Found profile: $_profile"
aba5c634 6201 _info "Installing alias to '$_profile'"
94dc5f33 6202 _setopt "$_profile" ". \"$_envfile\""
6203 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
6204 else
6205 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
6206 fi
94dc5f33 6207
6208 #for csh
6209 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
94dc5f33 6210 _csh_profile="$HOME/.cshrc"
4c2a3841 6211 if [ -f "$_csh_profile" ]; then
aba5c634 6212 _info "Installing alias to '$_csh_profile'"
6626371d 6213 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
f5b546b3 6214 if [ "$_c_home" ]; then
6215 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
d04434e3 6216 else
6217 _sed_i "/^setenv LE_CONFIG_HOME/d" "$_cshfile"
f5b546b3 6218 fi
be83a6a3 6219 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4c2a3841 6220 _setopt "$_csh_profile" "source \"$_cshfile\""
94dc5f33 6221 fi
4c2a3841 6222
acafa585 6223 #for tcsh
6224 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 6225 if [ -f "$_tcsh_profile" ]; then
aba5c634 6226 _info "Installing alias to '$_tcsh_profile'"
acafa585 6227 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
f5b546b3 6228 if [ "$_c_home" ]; then
6229 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
6230 fi
be83a6a3 6231 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4c2a3841 6232 _setopt "$_tcsh_profile" "source \"$_cshfile\""
acafa585 6233 fi
94dc5f33 6234
6235}
6236
58c4eaaf 6237# nocron confighome noprofile accountemail
c60883ef 6238install() {
f3e4cea3 6239
4c2a3841 6240 if [ -z "$LE_WORKING_DIR" ]; then
f3e4cea3 6241 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
6242 fi
4c2a3841 6243
c8e9a31e 6244 _nocron="$1"
27dbe77f 6245 _c_home="$2"
86ef0a26 6246 _noprofile="$3"
58c4eaaf 6247 _accountemail="$4"
6248
4c2a3841 6249 if ! _initpath; then
c60883ef 6250 _err "Install failed."
4c3b3608 6251 return 1
6252 fi
4c2a3841 6253 if [ "$_nocron" ]; then
52677b0a 6254 _debug "Skip install cron job"
6255 fi
4c2a3841 6256
bd04638d 6257 if [ "$_ACME_IN_CRON" != "1" ]; then
4356eefb 6258 if ! _precheck "$_nocron"; then
6259 _err "Pre-check failed, can not install."
6260 return 1
6261 fi
4c3b3608 6262 fi
4c2a3841 6263
8e845d9f 6264 if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then
6265 _info "Using config home: $LE_CONFIG_HOME"
6266 _c_home="$LE_CONFIG_HOME"
6267 fi
6268
6cc11ffb 6269 #convert from le
4c2a3841 6270 if [ -d "$HOME/.le" ]; then
6271 for envfile in "le.env" "le.sh.env"; do
6272 if [ -f "$HOME/.le/$envfile" ]; then
6273 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
6274 _upgrading="1"
6275 _info "You are upgrading from le.sh"
6276 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
6277 mv "$HOME/.le" "$LE_WORKING_DIR"
6278 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
6279 break
6cc11ffb 6280 fi
6281 fi
6282 done
6283 fi
6284
4c3b3608 6285 _info "Installing to $LE_WORKING_DIR"
635695ec 6286
d04434e3 6287 if [ ! -d "$LE_WORKING_DIR" ]; then
6288 if ! mkdir -p "$LE_WORKING_DIR"; then
6289 _err "Can not create working dir: $LE_WORKING_DIR"
6290 return 1
6291 fi
6292
6293 chmod 700 "$LE_WORKING_DIR"
4a0f23e2 6294 fi
4c2a3841 6295
d04434e3 6296 if [ ! -d "$LE_CONFIG_HOME" ]; then
6297 if ! mkdir -p "$LE_CONFIG_HOME"; then
6298 _err "Can not create config dir: $LE_CONFIG_HOME"
6299 return 1
6300 fi
762978f8 6301
d04434e3 6302 chmod 700 "$LE_CONFIG_HOME"
27dbe77f 6303 fi
6304
d5ec5f80 6305 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 6306
4c2a3841 6307 if [ "$?" != "0" ]; then
a7b7355d 6308 _err "Install failed, can not copy $PROJECT_ENTRY"
4c3b3608 6309 return 1
6310 fi
6311
a7b7355d 6312 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 6313
bd04638d 6314 if [ "$_ACME_IN_CRON" != "1" ] && [ -z "$_noprofile" ]; then
4356eefb 6315 _installalias "$_c_home"
6316 fi
4c3b3608 6317
4c2a3841 6318 for subf in $_SUB_FOLDERS; do
6319 if [ -d "$subf" ]; then
d5ec5f80 6320 mkdir -p "$LE_WORKING_DIR/$subf"
6321 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
a61fe418 6322 fi
6323 done
6324
4c2a3841 6325 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4c3b3608 6326 _initconf
6327 fi
6cc11ffb 6328
4c2a3841 6329 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
635695ec 6330 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
6cc11ffb 6331 fi
6332
4c2a3841 6333 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
b2817897 6334 _saveaccountconf "CERT_HOME" "$CERT_HOME"
6335 fi
6336
4c2a3841 6337 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
b2817897 6338 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
6339 fi
4c2a3841 6340
6341 if [ -z "$_nocron" ]; then
27dbe77f 6342 installcronjob "$_c_home"
c8e9a31e 6343 fi
0a7c9364 6344
4c2a3841 6345 if [ -z "$NO_DETECT_SH" ]; then
641989fd 6346 #Modify shebang
4c2a3841 6347 if _exists bash; then
694af4ae 6348 _bash_path="$(bash -c "command -v bash 2>/dev/null")"
6349 if [ -z "$_bash_path" ]; then
6350 _bash_path="$(bash -c 'echo $SHELL')"
6351 fi
6352 fi
6353 if [ "$_bash_path" ]; then
329174b6 6354 _info "Good, bash is found, so change the shebang to use bash as preferred."
694af4ae 6355 _shebang='#!'"$_bash_path"
641989fd 6356 _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
4c2a3841 6357 for subf in $_SUB_FOLDERS; do
6358 if [ -d "$LE_WORKING_DIR/$subf" ]; then
6359 for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
a61fe418 6360 _setShebang "$_apifile" "$_shebang"
6361 done
6362 fi
6363 done
0a7c9364 6364 fi
6365 fi
6366
58c4eaaf 6367 if [ "$_accountemail" ]; then
6368 _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
6369 fi
6370
4c3b3608 6371 _info OK
6372}
6373
52677b0a 6374# nocron
4c3b3608 6375uninstall() {
52677b0a 6376 _nocron="$1"
4c2a3841 6377 if [ -z "$_nocron" ]; then
52677b0a 6378 uninstallcronjob
6379 fi
4c3b3608 6380 _initpath
6381
9aa3be7f 6382 _uninstallalias
4c2a3841 6383
d5ec5f80 6384 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
f5b546b3 6385 _info "The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself."
9aa3be7f 6386
6387}
6388
6389_uninstallalias() {
6390 _initpath
6391
4c3b3608 6392 _profile="$(_detect_profile)"
4c2a3841 6393 if [ "$_profile" ]; then
9aa3be7f 6394 _info "Uninstalling alias from: '$_profile'"
d5ec5f80 6395 text="$(cat "$_profile")"
4c2a3841 6396 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
4c3b3608 6397 fi
6398
94dc5f33 6399 _csh_profile="$HOME/.cshrc"
4c2a3841 6400 if [ -f "$_csh_profile" ]; then
9aa3be7f 6401 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 6402 text="$(cat "$_csh_profile")"
4c2a3841 6403 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
94dc5f33 6404 fi
4c2a3841 6405
acafa585 6406 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 6407 if [ -f "$_tcsh_profile" ]; then
9aa3be7f 6408 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 6409 text="$(cat "$_tcsh_profile")"
4c2a3841 6410 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
acafa585 6411 fi
4c3b3608 6412
6413}
6414
6415cron() {
bd04638d 6416 export _ACME_IN_CRON=1
89002ed2 6417 _initpath
d8ba26e6 6418 _info "$(__green "===Starting cron===")"
4c2a3841 6419 if [ "$AUTO_UPGRADE" = "1" ]; then
89002ed2 6420 export LE_WORKING_DIR
6421 (
4c2a3841 6422 if ! upgrade; then
6423 _err "Cron:Upgrade failed!"
6424 return 1
6425 fi
89002ed2 6426 )
d5ec5f80 6427 . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
1ab63043 6428
4c2a3841 6429 if [ -t 1 ]; then
1ab63043 6430 __INTERACTIVE="1"
6431 fi
4c2a3841 6432
89002ed2 6433 _info "Auto upgraded to: $VER"
6434 fi
4c3b3608 6435 renewAll
cc179731 6436 _ret="$?"
bd04638d 6437 _ACME_IN_CRON=""
d8ba26e6 6438 _info "$(__green "===End cron===")"
0ba95a3d 6439 exit $_ret
4c3b3608 6440}
6441
6442version() {
a63b05a9 6443 echo "$PROJECT"
6444 echo "v$VER"
4c3b3608 6445}
6446
b50e701c 6447# subject content hooks code
6448_send_notify() {
6449 _nsubject="$1"
6450 _ncontent="$2"
6451 _nhooks="$3"
6452 _nerror="$4"
6453
6454 if [ "$NOTIFY_LEVEL" = "$NOTIFY_LEVEL_DISABLE" ]; then
6455 _debug "The NOTIFY_LEVEL is $NOTIFY_LEVEL, disabled, just return."
6456 return 0
6457 fi
6458
6459 if [ -z "$_nhooks" ]; then
6460 _debug "The NOTIFY_HOOK is empty, just return."
6461 return 0
6462 fi
6463
6464 _send_err=0
6465 for _n_hook in $(echo "$_nhooks" | tr ',' " "); do
6466 _n_hook_file="$(_findHook "" $_SUB_FOLDER_NOTIFY "$_n_hook")"
143eac09 6467 _info "Sending via: $_n_hook"
6468 _debug "Found $_n_hook_file for $_n_hook"
75191e71 6469 if [ -z "$_n_hook_file" ]; then
6470 _err "Can not find the hook file for $_n_hook"
6471 continue
6472 fi
b50e701c 6473 if ! (
6474 if ! . "$_n_hook_file"; then
6475 _err "Load file $_n_hook_file error. Please check your api file and try again."
6476 return 1
6477 fi
6478
6479 d_command="${_n_hook}_send"
6480 if ! _exists "$d_command"; then
6481 _err "It seems that your api file is not correct, it must have a function named: $d_command"
6482 return 1
6483 fi
6484
6485 if ! $d_command "$_nsubject" "$_ncontent" "$_nerror"; then
6486 _err "Error send message by $d_command"
6487 return 1
6488 fi
6489
6490 return 0
6491 ); then
6492 _err "Set $_n_hook_file error."
6493 _send_err=1
6494 else
6495 _info "$_n_hook $(__green Success)"
6496 fi
6497 done
6498 return $_send_err
6499
6500}
6501
6502# hook
6503_set_notify_hook() {
6504 _nhooks="$1"
6505
5698bec6 6506 _test_subject="Hello, this is a notification from $PROJECT_NAME"
143eac09 6507 _test_content="If you receive this message, your notification works."
b50e701c 6508
6509 _send_notify "$_test_subject" "$_test_content" "$_nhooks" 0
6510
6511}
6512
6513#[hook] [level] [mode]
6514setnotify() {
6515 _nhook="$1"
6516 _nlevel="$2"
6517 _nmode="$3"
6518
6519 _initpath
6520
6521 if [ -z "$_nhook$_nlevel$_nmode" ]; then
2e87e64b 6522 _usage "Usage: $PROJECT_ENTRY --set-notify [--notify-hook <hookname>] [--notify-level <0|1|2|3>] [--notify-mode <0|1>]"
b50e701c 6523 _usage "$_NOTIFY_WIKI"
6524 return 1
6525 fi
6526
6527 if [ "$_nlevel" ]; then
6528 _info "Set notify level to: $_nlevel"
6529 export "NOTIFY_LEVEL=$_nlevel"
6530 _saveaccountconf "NOTIFY_LEVEL" "$NOTIFY_LEVEL"
6531 fi
6532
6533 if [ "$_nmode" ]; then
6534 _info "Set notify mode to: $_nmode"
6535 export "NOTIFY_MODE=$_nmode"
6536 _saveaccountconf "NOTIFY_MODE" "$NOTIFY_MODE"
6537 fi
6538
6539 if [ "$_nhook" ]; then
6540 _info "Set notify hook to: $_nhook"
6541 if [ "$_nhook" = "$NO_VALUE" ]; then
6542 _info "Clear notify hook"
6543 _clearaccountconf "NOTIFY_HOOK"
6544 else
6545 if _set_notify_hook "$_nhook"; then
6546 export NOTIFY_HOOK="$_nhook"
6547 _saveaccountconf "NOTIFY_HOOK" "$NOTIFY_HOOK"
6548 return 0
6549 else
6550 _err "Can not set notify hook to: $_nhook"
6551 return 1
6552 fi
6553 fi
6554 fi
6555
6556}
6557
4c3b3608 6558showhelp() {
d0871bda 6559 _initpath
4c3b3608 6560 version
2e87e64b 6561 echo "Usage: $PROJECT_ENTRY <command> ... [parameters ...]
a63b05a9 6562Commands:
c0fbe823
CE
6563 -h, --help Show this help message.
6564 -v, --version Show version info.
a7b7355d 6565 --install Install $PROJECT_NAME to your system.
6566 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
d8beaf72 6567 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
a63b05a9 6568 --issue Issue a cert.
a61fe418 6569 --deploy Deploy the cert to your server.
d81369d6 6570 -i, --install-cert Install the issued cert to apache/nginx or any other server.
c0fbe823 6571 -r, --renew Renew a cert.
27dbe77f 6572 --renew-all Renew all the certs.
a63b05a9 6573 --revoke Revoke a cert.
47b49f1b 6574 --remove Remove the cert from list of certs known to $PROJECT_NAME.
10afcaca 6575 --list List all the certs.
58150f5d 6576 --to-pkcs12 Export the certificate and key to a pfx file.
d81369d6 6577 --to-pkcs8 Convert to pkcs8 format.
7decf768
CE
6578 --sign-csr Issue a cert from an existing csr.
6579 --show-csr Show the content of a csr.
6580 -ccr, --create-csr Create CSR, professional use.
6581 --create-domain-key Create an domain private key, professional use.
27dbe77f 6582 --update-account Update account info.
6583 --register-account Register account key.
422dd1fa 6584 --deactivate-account Deactivate the account.
0984585d 6585 --create-account-key Create an account private key, professional use.
7decf768
CE
6586 --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.
6587 --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
6588 --cron Run cron job to renew all the certs.
b50e701c 6589 --set-notify Set the cron notification hook, level or mode.
7decf768 6590 --deactivate Deactivate the domain authz, professional use.
8d0e4851 6591 --set-default-ca Used with '--server', Set the default CA to use.
6592 See: $_SERVER_WIKI
b50e701c 6593
3c07f57a 6594
a63b05a9 6595Parameters:
c0fbe823
CE
6596 -d, --domain <domain.tld> Specifies a domain, used to issue, renew or revoke etc.
6597 --challenge-alias <domain.tld> The challenge domain alias for DNS alias mode.
e3ebd582 6598 See: $_DNS_ALIAS_WIKI
a48c22d1 6599
c0fbe823 6600 --domain-alias <domain.tld> The domain alias for DNS alias mode.
e3ebd582 6601 See: $_DNS_ALIAS_WIKI
a48c22d1 6602
c0fbe823 6603 --preferred-chain <chain> If the CA offers multiple certificate chains, prefer the chain with an issuer matching this Subject Common Name.
e3ebd582 6604 If no match, the default offered chain will be used. (default: empty)
6605 See: $_PREFERRED_CHAIN_WIKI
a48c22d1 6606
c0fbe823
CE
6607 -f, --force Force install, force cert renewal or override sudo restrictions.
6608 --staging, --test Use staging server, for testing.
6609 --debug [0|1|2|3] Output debug info. Defaults to 1 if argument is omitted.
e3ebd582 6610 --output-insecure Output all the sensitive messages.
6611 By default all the credentials/sensitive messages are hidden from the output/debug/log for security.
b086afb2 6612 -w, --webroot <directory> Specifies the web root folder for web root mode.
a63b05a9 6613 --standalone Use standalone mode.
08681f4a 6614 --alpn Use standalone alpn mode.
e3ebd582 6615 --stateless Use stateless mode.
6616 See: $_STATELESS_WIKI
a48c22d1 6617
a63b05a9 6618 --apache Use apache mode.
c0fbe823 6619 --dns [dns_hook] Use dns manual mode or dns api. Defaults to manual mode when argument is omitted.
e3ebd582 6620 See: $_DNS_API_WIKI
a48c22d1 6621
c0fbe823 6622 --dnssleep <seconds> The time in seconds to wait for all the txt records to propagate in dns api mode.
e3ebd582 6623 It's not necessary to use this by default, $PROJECT_NAME polls dns status by DOH automatically.
c0fbe823
CE
6624 -k, --keylength <bits> Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384, ec-521.
6625 -ak, --accountkeylength <bits> Specifies the account key length: 2048, 3072, 4096
b086afb2 6626 --log [file] Specifies the log file. Defaults to \"$DEFAULT_LOG_FILE\" if argument is omitted.
c0fbe823
CE
6627 --log-level <1|2> Specifies the log level, default is 1.
6628 --syslog <0|3|6|7> Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
6629 --eab-kid <eab_key_id> Key Identifier for External Account Binding.
6630 --eab-hmac-key <eab_hmac_key> HMAC key for External Account Binding.
2d5f1438
OB
6631
6632
7903fcb4 6633 These parameters are to install the cert to nginx/apache or any other server after issue/renew a cert:
3c07f57a 6634
b086afb2
CE
6635 --cert-file <file> Path to copy the cert file to after issue/renew..
6636 --key-file <file> Path to copy the key file to after issue/renew.
6637 --ca-file <file> Path to copy the intermediate cert file to after issue/renew.
6638 --fullchain-file <file> Path to copy the fullchain cert file to after issue/renew.
c0fbe823 6639 --reloadcmd <command> Command to execute after issue/renew to reload the server.
a63b05a9 6640
c0fbe823 6641 --server <server_uri> ACME Directory Resource URI. (default: $DEFAULT_CA)
e3ebd582 6642 See: $_SERVER_WIKI
6643
c0fbe823
CE
6644 --accountconf <file> Specifies a customized account config file.
6645 --home <directory> Specifies the home dir for $PROJECT_NAME.
6646 --cert-home <directory> Specifies the home dir to save all the certs, only valid for '--install' command.
6647 --config-home <directory> Specifies the home dir to save all the configurations.
6648 --useragent <string> Specifies the user agent string. it will be saved for future use too.
58c4eaaf 6649 -m, --email <email> Specifies the account email, only valid for the '--install' and '--update-account' command.
c0fbe823
CE
6650 --accountkey <file> Specifies the account key path, only valid for the '--install' command.
6651 --days <ndays> Specifies the days to renew the cert when using '--issue' command. The default value is $DEFAULT_RENEW days.
6652 --httpport <port> Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
6653 --tlsport <port> Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
6654 --local-address <ip> Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
dcf4f8f6 6655 --listraw Only used for '--list' command, list the certs in raw format.
d81369d6 6656 -se, --stop-renew-on-error Only valid for '--renew-all' command. Stop if one cert has error in renewal.
13d7cae9 6657 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
c0fbe823
CE
6658 --ca-bundle <file> Specifies the path to the CA certificate bundle to verify api server's certificate.
6659 --ca-path <directory> Specifies directory containing CA certificates in PEM format, used by wget or curl.
58c4eaaf 6660 --no-cron Only valid for '--install' command, which means: do not install the default cron job.
e3ebd582 6661 In this case, the certs will not be renewed automatically.
58c4eaaf 6662 --no-profile Only valid for '--install' command, which means: do not install aliases to user profile.
2910be82 6663 --no-color Do not output color text.
e32b3aac 6664 --force-color Force output of color text. Useful for non-interactive use with the aha tool for HTML E-Mails.
b67d663a 6665 --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--to-pkcs12' and '--create-csr'
c0fbe823
CE
6666 --csr <file> Specifies the input csr.
6667 --pre-hook <command> Command to be run before obtaining any certificates.
6668 --post-hook <command> Command to be run after attempting to obtain/renew certificates. Runs regardless of whether obtain/renew succeeded or failed.
6669 --renew-hook <command> Command to be run after each successfully renewed certificate.
6670 --deploy-hook <hookname> The hook file to deploy cert
6671 --ocsp, --ocsp-must-staple Generate OCSP-Must-Staple extension.
6672 --always-force-new-domain-key Generate new domain key on renewal. Otherwise, the domain key is not changed by default.
6673 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future. Defaults to 1 if argument is omitted.
6ae0f7f5 6674 --listen-v4 Force standalone/tls server to listen at ipv4.
6675 --listen-v6 Force standalone/tls server to listen at ipv6.
c0fbe823 6676 --openssl-bin <file> Specifies a custom openssl bin location.
9b124070 6677 --use-wget Force to use wget, if you have both curl and wget installed.
07fdb087 6678 --yes-I-know-dns-manual-mode-enough-go-ahead-please Force use of dns manual mode.
e3ebd582 6679 See: $_DNS_MANUAL_WIKI
a48c22d1 6680
c0fbe823
CE
6681 -b, --branch <branch> Only valid for '--upgrade' command, specifies the branch name to upgrade to.
6682 --notify-level <0|1|2|3> Set the notification level: Default value is $NOTIFY_LEVEL_DEFAULT.
6683 0: disabled, no notification will be sent.
6684 1: send notifications only when there is an error.
6685 2: send notifications when a cert is successfully renewed, or there is an error.
6686 3: send notifications when a cert is skipped, renewed, or error.
6687 --notify-mode <0|1> Set notification mode. Default value is $NOTIFY_MODE_DEFAULT.
6688 0: Bulk mode. Send all the domain's notifications in one message(mail).
6689 1: Cert mode. Send a message for every single cert.
6690 --notify-hook <hookname> Set the notify hook
6691 --revoke-reason <0-10> The reason for revocation, can be used in conjunction with the '--revoke' command.
e3ebd582 6692 See: $_REVOKE_WIKI
6693
dd6c5c9e
CE
6694 --password <password> Add a password to exported pfx file. Use with --to-pkcs12.
6695
b50e701c 6696
6697"
4c3b3608 6698}
6699
58c4eaaf 6700installOnline() {
4a0f23e2 6701 _info "Installing from online archive."
58c4eaaf 6702
6703 _branch="$BRANCH"
6704 if [ -z "$_branch" ]; then
6705 _branch="master"
4a0f23e2 6706 fi
a8df88ab 6707
58c4eaaf 6708 target="$PROJECT/archive/$_branch.tar.gz"
4a0f23e2 6709 _info "Downloading $target"
58c4eaaf 6710 localname="$_branch.tar.gz"
4c2a3841 6711 if ! _get "$target" >$localname; then
df9547ae 6712 _err "Download error."
4a0f23e2 6713 return 1
6714 fi
0bbe6eef 6715 (
4c2a3841 6716 _info "Extracting $localname"
3a3b0dd5 6717 if ! (tar xzf $localname || gtar xzf $localname); then
6718 _err "Extraction error."
6719 exit 1
6720 fi
4c2a3841 6721
58c4eaaf 6722 cd "$PROJECT_NAME-$_branch"
4c2a3841 6723 chmod +x $PROJECT_ENTRY
58c4eaaf 6724 if ./$PROJECT_ENTRY --install "$@"; then
4c2a3841 6725 _info "Install success!"
ac3667c7 6726 _initpath
cb7e3857 6727 _saveaccountconf "UPGRADE_HASH" "$(_getUpgradeHash)"
4c2a3841 6728 fi
6729
6730 cd ..
6731
58c4eaaf 6732 rm -rf "$PROJECT_NAME-$_branch"
4c2a3841 6733 rm -f "$localname"
0bbe6eef 6734 )
4a0f23e2 6735}
6736
cb7e3857
OB
6737_getRepoHash() {
6738 _hash_path=$1
6739 shift
6740 _hash_url="https://api.github.com/repos/acmesh-official/$PROJECT_NAME/git/refs/$_hash_path"
6741 _get $_hash_url | tr -d "\r\n" | tr '{},' '\n' | grep '"sha":' | cut -d '"' -f 4
6742}
6743
6744_getUpgradeHash() {
f716f606 6745 _b="$BRANCH"
6746 if [ -z "$_b" ]; then
6747 _b="master"
6748 fi
cb7e3857
OB
6749 _hash=$(_getRepoHash "heads/$_b")
6750 if [ -z "$_hash" ]; then _hash=$(_getRepoHash "tags/$_b"); fi
6751 echo $_hash
7a3c61b7 6752}
6753
52677b0a 6754upgrade() {
6755 if (
267f283a 6756 _initpath
cb7e3857 6757 [ -z "$FORCE" ] && [ "$(_getUpgradeHash)" = "$(_readaccountconf "UPGRADE_HASH")" ] && _info "Already uptodate!" && exit 0
267f283a 6758 export LE_WORKING_DIR
d0b748a4 6759 cd "$LE_WORKING_DIR"
58c4eaaf 6760 installOnline "--nocron" "--noprofile"
4c2a3841 6761 ); then
52677b0a 6762 _info "Upgrade success!"
096d8992 6763 exit 0
52677b0a 6764 else
6765 _err "Upgrade failed!"
096d8992 6766 exit 1
52677b0a 6767 fi
6768}
a63b05a9 6769
5ea6e9c9 6770_processAccountConf() {
4c2a3841 6771 if [ "$_useragent" ]; then
5ea6e9c9 6772 _saveaccountconf "USER_AGENT" "$_useragent"
4c2a3841 6773 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
d0871bda 6774 _saveaccountconf "USER_AGENT" "$USER_AGENT"
5ea6e9c9 6775 fi
4c2a3841 6776
a746139c 6777 if [ "$_openssl_bin" ]; then
851fedf7 6778 _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin"
6779 elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
6780 _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
a746139c 6781 fi
6782
4c2a3841 6783 if [ "$_auto_upgrade" ]; then
6bf281f9 6784 _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
4c2a3841 6785 elif [ "$AUTO_UPGRADE" ]; then
6bf281f9 6786 _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
6787 fi
4c2a3841 6788
9b124070 6789 if [ "$_use_wget" ]; then
6790 _saveaccountconf "ACME_USE_WGET" "$_use_wget"
6791 elif [ "$ACME_USE_WGET" ]; then
6792 _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET"
6793 fi
6794
5ea6e9c9 6795}
6796
5bdfdfef 6797_checkSudo() {
6798 if [ "$SUDO_GID" ] && [ "$SUDO_COMMAND" ] && [ "$SUDO_USER" ] && [ "$SUDO_UID" ]; then
6799 if [ "$SUDO_USER" = "root" ] && [ "$SUDO_UID" = "0" ]; then
6800 #it's root using sudo, no matter it's using sudo or not, just fine
6801 return 0
6802 fi
79ad0ff5 6803 if [ -n "$SUDO_COMMAND" ]; then
996f5337 6804 #it's a normal user doing "sudo su", or `sudo -i` or `sudo -s`, or `sudo su acmeuser1`
6805 _endswith "$SUDO_COMMAND" /bin/su || _contains "$SUDO_COMMAND" "/bin/su " || grep "^$SUDO_COMMAND\$" /etc/shells >/dev/null 2>&1
79ad0ff5 6806 return $?
5bdfdfef 6807 fi
6808 #otherwise
6809 return 1
6810 fi
6811 return 0
6812}
6813
737e9e48 6814#server
6815_selectServer() {
6816 _server="$1"
6817 _server_lower="$(echo "$_server" | _lower_case)"
6818 _sindex=0
6819 for snames in $CA_NAMES; do
6820 snames="$(echo "$snames" | _lower_case)"
6821 _sindex="$(_math $_sindex + 1)"
6822 _debug2 "_selectServer try snames" "$snames"
6823 for sname in $(echo "$snames" | tr ',' ' '); do
6824 if [ "$_server_lower" = "$sname" ]; then
6825 _debug2 "_selectServer match $sname"
6826 _serverdir="$(_getfield "$CA_SERVERS" $_sindex)"
6827 _debug "Selected server: $_serverdir"
6828 ACME_DIRECTORY="$_serverdir"
6829 export ACME_DIRECTORY
6830 return
6831 fi
6832 done
6833 done
6834 ACME_DIRECTORY="$_server"
6835 export ACME_DIRECTORY
6836}
6837
269847d1 6838#url
6839_getCAShortName() {
6840 caurl="$1"
95ef046d 6841 if [ -z "$caurl" ]; then
6842 caurl="$DEFAULT_CA"
6843 fi
269847d1 6844 caurl_lower="$(echo $caurl | _lower_case)"
6845 _sindex=0
6846 for surl in $(echo "$CA_SERVERS" | _lower_case | tr , ' '); do
6847 _sindex="$(_math $_sindex + 1)"
6848 if [ "$caurl_lower" = "$surl" ]; then
6849 _nindex=0
6850 for snames in $CA_NAMES; do
6851 _nindex="$(_math $_nindex + 1)"
6852 if [ $_nindex -ge $_sindex ]; then
6853 _getfield "$snames" 1
6854 return
6855 fi
6856 done
6857 fi
6858 done
6859 echo "$caurl"
6860}
6861
737e9e48 6862#set default ca to $ACME_DIRECTORY
6863setdefaultca() {
6864 if [ -z "$ACME_DIRECTORY" ]; then
6865 _err "Please give a --server parameter."
6866 return 1
6867 fi
6868 _saveaccountconf "DEFAULT_ACME_SERVER" "$ACME_DIRECTORY"
df22f680 6869 _info "Changed default CA to: $(__green "$ACME_DIRECTORY")"
737e9e48 6870}
6871
a63b05a9 6872_process() {
6873 _CMD=""
6874 _domain=""
3f4513b3 6875 _altdomains="$NO_VALUE"
a63b05a9 6876 _webroot=""
875625b1 6877 _challenge_alias=""
bdbf323f 6878 _keylength=""
6879 _accountkeylength=""
5c539af7 6880 _cert_file=""
6881 _key_file=""
6882 _ca_file=""
6883 _fullchain_file=""
4d2f38b0 6884 _reloadcmd=""
a63b05a9 6885 _password=""
635695ec 6886 _accountconf=""
6887 _useragent=""
b5eb4b90 6888 _accountemail=""
6889 _accountkey=""
b2817897 6890 _certhome=""
27dbe77f 6891 _confighome=""
39c8f79f 6892 _httpport=""
e22bcf7c 6893 _tlsport=""
0e38c60d 6894 _dnssleep=""
dcf4f8f6 6895 _listraw=""
cc179731 6896 _stopRenewOnError=""
e3698edd 6897 #_insecure=""
78009539 6898 _ca_bundle=""
2aa75f03 6899 _ca_path=""
c8e9a31e 6900 _nocron=""
61556a54 6901 _noprofile=""
43822d37 6902 _ecc=""
10afcaca 6903 _csr=""
b0070f03 6904 _pre_hook=""
6905 _post_hook=""
6906 _renew_hook=""
a61fe418 6907 _deploy_hook=""
5ea6e9c9 6908 _logfile=""
d0871bda 6909 _log=""
0463b5d6 6910 _local_address=""
a73c5b33 6911 _log_level=""
6bf281f9 6912 _auto_upgrade=""
6ae0f7f5 6913 _listen_v4=""
6914 _listen_v6=""
a746139c 6915 _openssl_bin=""
e2edf208 6916 _syslog=""
9b124070 6917 _use_wget=""
98394f99 6918 _server=""
b50e701c 6919 _notify_hook=""
6920 _notify_level=""
6921 _notify_mode=""
1041c9f9 6922 _revoke_reason=""
f96d91cb 6923 _eab_kid=""
6924 _eab_hmac_key=""
e3ebd582 6925 _preferred_chain=""
4c2a3841 6926 while [ ${#} -gt 0 ]; do
a63b05a9 6927 case "${1}" in
4c2a3841 6928
19c43451 6929 --help | -h)
6930 showhelp
6931 return
6932 ;;
6933 --version | -v)
6934 version
6935 return
6936 ;;
6937 --install)
6938 _CMD="install"
6939 ;;
58c4eaaf 6940 --install-online)
6941 shift
6942 installOnline "$@"
6943 return
6944 ;;
19c43451 6945 --uninstall)
6946 _CMD="uninstall"
6947 ;;
6948 --upgrade)
6949 _CMD="upgrade"
6950 ;;
6951 --issue)
6952 _CMD="issue"
6953 ;;
6954 --deploy)
6955 _CMD="deploy"
6956 ;;
7decf768 6957 --sign-csr | --signcsr)
19c43451 6958 _CMD="signcsr"
6959 ;;
7decf768 6960 --show-csr | --showcsr)
19c43451 6961 _CMD="showcsr"
6962 ;;
7decf768 6963 -i | --install-cert | --installcert)
19c43451 6964 _CMD="installcert"
6965 ;;
6966 --renew | -r)
6967 _CMD="renew"
6968 ;;
7decf768 6969 --renew-all | --renewAll | --renewall)
19c43451 6970 _CMD="renewAll"
6971 ;;
6972 --revoke)
6973 _CMD="revoke"
6974 ;;
6975 --remove)
6976 _CMD="remove"
6977 ;;
6978 --list)
6979 _CMD="list"
6980 ;;
7decf768 6981 --install-cronjob | --installcronjob)
19c43451 6982 _CMD="installcronjob"
6983 ;;
7decf768 6984 --uninstall-cronjob | --uninstallcronjob)
19c43451 6985 _CMD="uninstallcronjob"
6986 ;;
6987 --cron)
6988 _CMD="cron"
6989 ;;
1521199e 6990 --to-pkcs12 | --to-pkcs | --toPkcs)
19c43451 6991 _CMD="toPkcs"
6992 ;;
7decf768 6993 --to-pkcs8 | --toPkcs8)
19c43451 6994 _CMD="toPkcs8"
6995 ;;
7decf768 6996 --create-account-key | --createAccountKey | --createaccountkey | -cak)
19c43451 6997 _CMD="createAccountKey"
6998 ;;
7decf768 6999 --create-domain-key | --createDomainKey | --createdomainkey | -cdk)
19c43451 7000 _CMD="createDomainKey"
7001 ;;
7decf768 7002 -ccr | --create-csr | --createCSR | --createcsr)
19c43451 7003 _CMD="createCSR"
7004 ;;
7005 --deactivate)
7006 _CMD="deactivate"
7007 ;;
7decf768 7008 --update-account | --updateaccount)
19c43451 7009 _CMD="updateaccount"
7010 ;;
7decf768 7011 --register-account | --registeraccount)
19c43451 7012 _CMD="registeraccount"
7013 ;;
7014 --deactivate-account)
7015 _CMD="deactivateaccount"
7016 ;;
7017 --set-notify)
7018 _CMD="setnotify"
7019 ;;
7020 --set-default-ca)
7021 _CMD="setdefaultca"
7022 ;;
7decf768 7023 -d | --domain)
19c43451 7024 _dvalue="$2"
4c2a3841 7025
19c43451 7026 if [ "$_dvalue" ]; then
7027 if _startswith "$_dvalue" "-"; then
7028 _err "'$_dvalue' is not a valid domain for parameter '$1'"
7029 return 1
7030 fi
7031 if _is_idn "$_dvalue" && ! _exists idn; then
7032 _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
7033 return 1
7034 fi
4c2a3841 7035
19c43451 7036 if _startswith "$_dvalue" "*."; then
7037 _debug "Wildcard domain"
7038 export ACME_VERSION=2
7039 fi
7040 if [ -z "$_domain" ]; then
7041 _domain="$_dvalue"
7042 else
7043 if [ "$_altdomains" = "$NO_VALUE" ]; then
7044 _altdomains="$_dvalue"
a63b05a9 7045 else
19c43451 7046 _altdomains="$_altdomains,$_dvalue"
a63b05a9 7047 fi
7048 fi
19c43451 7049 fi
4c2a3841 7050
19c43451 7051 shift
7052 ;;
a63b05a9 7053
7decf768 7054 -f | --force)
19c43451 7055 FORCE="1"
7056 ;;
7057 --staging | --test)
7058 STAGE="1"
7059 ;;
7060 --server)
7061 _server="$2"
7062 _selectServer "$_server"
7063 shift
7064 ;;
7065 --debug)
7066 if [ -z "$2" ] || _startswith "$2" "-"; then
7067 DEBUG="$DEBUG_LEVEL_DEFAULT"
7068 else
7069 DEBUG="$2"
64821ad4 7070 shift
19c43451 7071 fi
7072 ;;
7073 --output-insecure)
7074 export OUTPUT_INSECURE=1
7075 ;;
7decf768 7076 -w | --webroot)
19c43451 7077 wvalue="$2"
7078 if [ -z "$_webroot" ]; then
7079 _webroot="$wvalue"
7080 else
7081 _webroot="$_webroot,$wvalue"
7082 fi
7083 shift
7084 ;;
7085 --challenge-alias)
7086 cvalue="$2"
7087 _challenge_alias="$_challenge_alias$cvalue,"
7088 shift
7089 ;;
7090 --domain-alias)
7091 cvalue="$DNS_ALIAS_PREFIX$2"
7092 _challenge_alias="$_challenge_alias$cvalue,"
7093 shift
7094 ;;
7095 --standalone)
7096 wvalue="$NO_VALUE"
7097 if [ -z "$_webroot" ]; then
7098 _webroot="$wvalue"
7099 else
7100 _webroot="$_webroot,$wvalue"
7101 fi
7102 ;;
7103 --alpn)
7104 wvalue="$W_ALPN"
7105 if [ -z "$_webroot" ]; then
7106 _webroot="$wvalue"
7107 else
7108 _webroot="$_webroot,$wvalue"
7109 fi
7110 ;;
7111 --stateless)
7112 wvalue="$MODE_STATELESS"
7113 if [ -z "$_webroot" ]; then
7114 _webroot="$wvalue"
7115 else
7116 _webroot="$_webroot,$wvalue"
7117 fi
7118 ;;
7119 --local-address)
7120 lvalue="$2"
7121 _local_address="$_local_address$lvalue,"
7122 shift
7123 ;;
7124 --apache)
7125 wvalue="apache"
7126 if [ -z "$_webroot" ]; then
7127 _webroot="$wvalue"
7128 else
7129 _webroot="$_webroot,$wvalue"
7130 fi
7131 ;;
7132 --nginx)
7133 wvalue="$NGINX"
7134 if [ "$2" ] && ! _startswith "$2" "-"; then
7135 wvalue="$NGINX$2"
0463b5d6 7136 shift
19c43451 7137 fi
7138 if [ -z "$_webroot" ]; then
7139 _webroot="$wvalue"
7140 else
7141 _webroot="$_webroot,$wvalue"
7142 fi
7143 ;;
7144 --dns)
7145 wvalue="$W_DNS"
7146 if [ "$2" ] && ! _startswith "$2" "-"; then
7147 wvalue="$2"
0e38c60d 7148 shift
19c43451 7149 fi
7150 if [ -z "$_webroot" ]; then
7151 _webroot="$wvalue"
7152 else
7153 _webroot="$_webroot,$wvalue"
7154 fi
7155 ;;
7156 --dnssleep)
7157 _dnssleep="$2"
7158 Le_DNSSleep="$_dnssleep"
7159 shift
7160 ;;
4c2a3841 7161
19c43451 7162 --keylength | -k)
7163 _keylength="$2"
7164 shift
7165 ;;
7decf768 7166 -ak | --accountkeylength)
19c43451 7167 _accountkeylength="$2"
7168 shift
7169 ;;
a63b05a9 7170
19c43451 7171 --cert-file | --certpath)
7172 _cert_file="$2"
7173 shift
7174 ;;
7175 --key-file | --keypath)
7176 _key_file="$2"
7177 shift
7178 ;;
7179 --ca-file | --capath)
7180 _ca_file="$2"
7181 shift
7182 ;;
7183 --fullchain-file | --fullchainpath)
7184 _fullchain_file="$2"
7185 shift
7186 ;;
7187 --reloadcmd | --reloadCmd)
7188 _reloadcmd="$2"
7189 shift
7190 ;;
7191 --password)
7192 _password="$2"
7193 shift
7194 ;;
7195 --accountconf)
7196 _accountconf="$2"
7197 ACCOUNT_CONF_PATH="$_accountconf"
7198 shift
7199 ;;
7200 --home)
7201 LE_WORKING_DIR="$2"
7202 shift
7203 ;;
7decf768 7204 --cert-home | --certhome)
19c43451 7205 _certhome="$2"
7206 CERT_HOME="$_certhome"
7207 shift
7208 ;;
7209 --config-home)
7210 _confighome="$2"
7211 LE_CONFIG_HOME="$_confighome"
7212 shift
7213 ;;
7214 --useragent)
7215 _useragent="$2"
7216 USER_AGENT="$_useragent"
7217 shift
7218 ;;
58c4eaaf 7219 -m | --email | --accountemail)
19c43451 7220 _accountemail="$2"
58c4eaaf 7221 export ACCOUNT_EMAIL="$_accountemail"
19c43451 7222 shift
7223 ;;
7224 --accountkey)
7225 _accountkey="$2"
7226 ACCOUNT_KEY_PATH="$_accountkey"
7227 shift
7228 ;;
7229 --days)
7230 _days="$2"
7231 Le_RenewalDays="$_days"
7232 shift
7233 ;;
7234 --httpport)
7235 _httpport="$2"
7236 Le_HTTPPort="$_httpport"
7237 shift
7238 ;;
7239 --tlsport)
7240 _tlsport="$2"
7241 Le_TLSPort="$_tlsport"
7242 shift
7243 ;;
7244 --listraw)
7245 _listraw="raw"
7246 ;;
7decf768 7247 -se | --stop-renew-on-error | --stopRenewOnError | --stoprenewonerror)
19c43451 7248 _stopRenewOnError="1"
7249 ;;
7250 --insecure)
7251 #_insecure="1"
7252 HTTPS_INSECURE="1"
7253 ;;
7254 --ca-bundle)
7255 _ca_bundle="$(_readlink "$2")"
7256 CA_BUNDLE="$_ca_bundle"
7257 shift
7258 ;;
7259 --ca-path)
7260 _ca_path="$2"
7261 CA_PATH="$_ca_path"
7262 shift
7263 ;;
58c4eaaf 7264 --no-cron | --nocron)
19c43451 7265 _nocron="1"
7266 ;;
58c4eaaf 7267 --no-profile | --noprofile)
19c43451 7268 _noprofile="1"
7269 ;;
2910be82 7270 --no-color)
19c43451 7271 export ACME_NO_COLOR=1
7272 ;;
7273 --force-color)
7274 export ACME_FORCE_COLOR=1
7275 ;;
7276 --ecc)
7277 _ecc="isEcc"
7278 ;;
7279 --csr)
7280 _csr="$2"
7281 shift
7282 ;;
7283 --pre-hook)
7284 _pre_hook="$2"
7285 shift
7286 ;;
7287 --post-hook)
7288 _post_hook="$2"
7289 shift
7290 ;;
7291 --renew-hook)
7292 _renew_hook="$2"
7293 shift
7294 ;;
7295 --deploy-hook)
7296 if [ -z "$2" ] || _startswith "$2" "-"; then
7297 _usage "Please specify a value for '--deploy-hook'"
7298 return 1
7299 fi
7300 _deploy_hook="$_deploy_hook$2,"
7301 shift
7302 ;;
7303 --ocsp-must-staple | --ocsp)
7304 Le_OCSP_Staple="1"
7305 ;;
7306 --always-force-new-domain-key)
7307 if [ -z "$2" ] || _startswith "$2" "-"; then
7308 Le_ForceNewDomainKey=1
7309 else
7310 Le_ForceNewDomainKey="$2"
1041c9f9 7311 shift
19c43451 7312 fi
7313 ;;
7314 --yes-I-know-dns-manual-mode-enough-go-ahead-please)
7315 export FORCE_DNS_MANUAL=1
7316 ;;
7317 --log | --logfile)
7318 _log="1"
7319 _logfile="$2"
7320 if _startswith "$_logfile" '-'; then
7321 _logfile=""
7322 else
f96d91cb 7323 shift
19c43451 7324 fi
7325 LOG_FILE="$_logfile"
7326 if [ -z "$LOG_LEVEL" ]; then
7327 LOG_LEVEL="$DEFAULT_LOG_LEVEL"
7328 fi
7329 ;;
7330 --log-level)
7331 _log_level="$2"
7332 LOG_LEVEL="$_log_level"
7333 shift
7334 ;;
7335 --syslog)
7336 if ! _startswith "$2" '-'; then
7337 _syslog="$2"
f96d91cb 7338 shift
19c43451 7339 fi
7340 if [ -z "$_syslog" ]; then
7341 _syslog="$SYSLOG_LEVEL_DEFAULT"
7342 fi
7343 ;;
7344 --auto-upgrade)
7345 _auto_upgrade="$2"
7346 if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
7347 _auto_upgrade="1"
7348 else
e3ebd582 7349 shift
19c43451 7350 fi
7351 AUTO_UPGRADE="$_auto_upgrade"
7352 ;;
7353 --listen-v4)
7354 _listen_v4="1"
7355 Le_Listen_V4="$_listen_v4"
7356 ;;
7357 --listen-v6)
7358 _listen_v6="1"
7359 Le_Listen_V6="$_listen_v6"
7360 ;;
7361 --openssl-bin)
7362 _openssl_bin="$2"
7363 ACME_OPENSSL_BIN="$_openssl_bin"
7364 shift
7365 ;;
7366 --use-wget)
7367 _use_wget="1"
7368 ACME_USE_WGET="1"
7369 ;;
7370 --branch | -b)
7371 export BRANCH="$2"
7372 shift
7373 ;;
7374 --notify-hook)
7375 _nhook="$2"
7376 if _startswith "$_nhook" "-"; then
7377 _err "'$_nhook' is not a hook name for '$1'"
a63b05a9 7378 return 1
19c43451 7379 fi
7380 if [ "$_notify_hook" ]; then
7381 _notify_hook="$_notify_hook,$_nhook"
7382 else
7383 _notify_hook="$_nhook"
7384 fi
7385 shift
7386 ;;
7387 --notify-level)
7388 _nlevel="$2"
7389 if _startswith "$_nlevel" "-"; then
7390 _err "'$_nlevel' is not a integer for '$1'"
7391 return 1
7392 fi
7393 _notify_level="$_nlevel"
7394 shift
7395 ;;
7396 --notify-mode)
7397 _nmode="$2"
7398 if _startswith "$_nmode" "-"; then
7399 _err "'$_nmode' is not a integer for '$1'"
7400 return 1
7401 fi
7402 _notify_mode="$_nmode"
7403 shift
7404 ;;
7405 --revoke-reason)
7406 _revoke_reason="$2"
7407 if _startswith "$_revoke_reason" "-"; then
7408 _err "'$_revoke_reason' is not a integer for '$1'"
7409 return 1
7410 fi
7411 shift
7412 ;;
7413 --eab-kid)
7414 _eab_kid="$2"
7415 shift
7416 ;;
7417 --eab-hmac-key)
7418 _eab_hmac_key="$2"
7419 shift
7420 ;;
7421 --preferred-chain)
7422 _preferred_chain="$2"
7423 shift
7424 ;;
7425 *)
7426 _err "Unknown parameter : $1"
7427 return 1
7428 ;;
a63b05a9 7429 esac
7430
7431 shift 1
7432 done
7433
4c2a3841 7434 if [ "${_CMD}" != "install" ]; then
5bdfdfef 7435 if [ "$__INTERACTIVE" ] && ! _checkSudo; then
7436 if [ -z "$FORCE" ]; then
7437 #Use "echo" here, instead of _info. it's too early
7438 echo "It seems that you are using sudo, please read this link first:"
7439 echo "$_SUDO_WIKI"
7440 return 1
7441 fi
7442 fi
5ea6e9c9 7443 __initHome
661f0583 7444 if [ "$_log" ]; then
4c2a3841 7445 if [ -z "$_logfile" ]; then
661f0583 7446 _logfile="$DEFAULT_LOG_FILE"
7447 fi
d0871bda 7448 fi
4c2a3841 7449 if [ "$_logfile" ]; then
5ea6e9c9 7450 _saveaccountconf "LOG_FILE" "$_logfile"
661f0583 7451 LOG_FILE="$_logfile"
5ea6e9c9 7452 fi
a73c5b33 7453
4c2a3841 7454 if [ "$_log_level" ]; then
a73c5b33 7455 _saveaccountconf "LOG_LEVEL" "$_log_level"
7456 LOG_LEVEL="$_log_level"
7457 fi
4c2a3841 7458
e2edf208 7459 if [ "$_syslog" ]; then
7460 if _exists logger; then
7461 if [ "$_syslog" = "0" ]; then
7462 _clearaccountconf "SYS_LOG"
7463 else
7464 _saveaccountconf "SYS_LOG" "$_syslog"
7465 fi
7466 SYS_LOG="$_syslog"
7467 else
7468 _err "The 'logger' command is not found, can not enable syslog."
7469 _clearaccountconf "SYS_LOG"
7470 SYS_LOG=""
7471 fi
7472 fi
7473
5ea6e9c9 7474 _processAccountConf
7475 fi
4c2a3841 7476
9d548d81 7477 _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
4c2a3841 7478
7479 if [ "$DEBUG" ]; then
dcf9cb58 7480 version
98394f99 7481 if [ "$_server" ]; then
7482 _debug "Using server: $_server"
7483 fi
dcf9cb58 7484 fi
9a733a57 7485 _debug "Running cmd: ${_CMD}"
a63b05a9 7486 case "${_CMD}" in
58c4eaaf 7487 install) install "$_nocron" "$_confighome" "$_noprofile" "$_accountemail" ;;
19c43451 7488 uninstall) uninstall "$_nocron" ;;
7489 upgrade) upgrade ;;
7490 issue)
7491 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"
7492 ;;
7493 deploy)
7494 deploy "$_domain" "$_deploy_hook" "$_ecc"
7495 ;;
7496 signcsr)
96a95ba9 7497 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 7498 ;;
7499 showcsr)
7500 showcsr "$_csr" "$_domain"
7501 ;;
7502 installcert)
7503 installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
7504 ;;
7505 renew)
7506 renew "$_domain" "$_ecc"
7507 ;;
7508 renewAll)
7509 renewAll "$_stopRenewOnError"
7510 ;;
7511 revoke)
7512 revoke "$_domain" "$_ecc" "$_revoke_reason"
7513 ;;
7514 remove)
7515 remove "$_domain" "$_ecc"
7516 ;;
7517 deactivate)
7518 deactivate "$_domain,$_altdomains"
7519 ;;
7520 registeraccount)
7521 registeraccount "$_accountkeylength" "$_eab_kid" "$_eab_hmac_key"
7522 ;;
7523 updateaccount)
7524 updateaccount
7525 ;;
7526 deactivateaccount)
7527 deactivateaccount
7528 ;;
7529 list)
7530 list "$_listraw" "$_domain"
7531 ;;
7532 installcronjob) installcronjob "$_confighome" ;;
7533 uninstallcronjob) uninstallcronjob ;;
7534 cron) cron ;;
7535 toPkcs)
7536 toPkcs "$_domain" "$_password" "$_ecc"
7537 ;;
7538 toPkcs8)
7539 toPkcs8 "$_domain" "$_ecc"
7540 ;;
7541 createAccountKey)
7542 createAccountKey "$_accountkeylength"
7543 ;;
7544 createDomainKey)
7545 createDomainKey "$_domain" "$_keylength"
7546 ;;
7547 createCSR)
7548 createCSR "$_domain" "$_altdomains" "$_ecc"
7549 ;;
7550 setnotify)
7551 setnotify "$_notify_hook" "$_notify_level" "$_notify_mode"
7552 ;;
7553 setdefaultca)
7554 setdefaultca
7555 ;;
7556 *)
7557 if [ "$_CMD" ]; then
7558 _err "Invalid command: $_CMD"
7559 fi
7560 showhelp
7561 return 1
7562 ;;
a63b05a9 7563 esac
d3595686 7564 _ret="$?"
4c2a3841 7565 if [ "$_ret" != "0" ]; then
d3595686 7566 return $_ret
7567 fi
4c2a3841 7568
7569 if [ "${_CMD}" = "install" ]; then
7570 if [ "$_log" ]; then
7571 if [ -z "$LOG_FILE" ]; then
d0871bda 7572 LOG_FILE="$DEFAULT_LOG_FILE"
7573 fi
7574 _saveaccountconf "LOG_FILE" "$LOG_FILE"
5ea6e9c9 7575 fi
4c2a3841 7576
7577 if [ "$_log_level" ]; then
a73c5b33 7578 _saveaccountconf "LOG_LEVEL" "$_log_level"
7579 fi
e2edf208 7580
7581 if [ "$_syslog" ]; then
7582 if _exists logger; then
7583 if [ "$_syslog" = "0" ]; then
7584 _clearaccountconf "SYS_LOG"
7585 else
7586 _saveaccountconf "SYS_LOG" "$_syslog"
7587 fi
7588 else
7589 _err "The 'logger' command is not found, can not enable syslog."
7590 _clearaccountconf "SYS_LOG"
7591 SYS_LOG=""
7592 fi
7593 fi
7594
5ea6e9c9 7595 _processAccountConf
b5eb4b90 7596 fi
635695ec 7597
a63b05a9 7598}
7599
319e0ae3 7600main() {
7601 [ -z "$1" ] && showhelp && return
4c2a3841 7602 if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
319e0ae3 7603}
e69a7c38 7604
aa7b82de 7605main "$@"