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