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