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