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