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