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