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