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