]> git.proxmox.com Git - mirror_acme.sh.git/blame - acme.sh
Merge pull request #869 from Neilpang/dev
[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
df711b0e 2582 _debug3 "Modified config:$(cat $FOUND_REAL_NGINX_CONF)"
9d725af6 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
7c67e3d7 2668 if [ "$(echo "$_seg_n" | _egrep_o "^ *ssl *on *;")" ] \
674790a5 2669 || [ "$(echo "$_seg_n" | _egrep_o "listen .* ssl[ |;]")" ]; then
9f90618a 2670 _debug "ssl on, skip"
241cfc43 2671 else
2672 FOUND_REAL_NGINX_CONF_LN=$_fln
2673 _debug3 "found FOUND_REAL_NGINX_CONF_LN" "$FOUND_REAL_NGINX_CONF_LN"
2674 return 0
450efea1 2675 fi
302c41ed 2676 fi
2677 done
9d725af6 2678 fi
302c41ed 2679 return 1
9d725af6 2680}
2681
2682#restore all the nginx conf
2683_restoreNginx() {
5d943a35 2684 if [ -z "$NGINX_RESTORE_VLIST" ]; then
9d725af6 2685 _debug "No need to restore nginx, skip."
2686 return
2687 fi
2688 _debug "_restoreNginx"
5d943a35 2689 _debug "NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST"
9d725af6 2690
5d943a35 2691 for ng_entry in $(echo "$NGINX_RESTORE_VLIST" | tr "$dvsep" ' '); do
9d725af6 2692 _debug "ng_entry" "$ng_entry"
2693 _nd=$(echo "$ng_entry" | cut -d "$sep" -f 1)
2694 _ngconf=$(echo "$ng_entry" | cut -d "$sep" -f 2)
2695 _ngbackupconf=$(echo "$ng_entry" | cut -d "$sep" -f 3)
2696 _info "Restoring from $_ngbackupconf to $_ngconf"
302c41ed 2697 cat "$_ngbackupconf" >"$_ngconf"
9d725af6 2698 done
2699
2700 _info "Reload nginx"
2701 if ! _exec "nginx -s reload" >/dev/null; then
2702 _exec_err
2703 _err "It seems that nginx reload error, please report bug."
2704 return 1
2705 fi
2706 return 0
2707}
2708
5ef501c5 2709_clearup() {
44edb2bd 2710 _stopserver "$serverproc"
4c3b3608 2711 serverproc=""
2712 _restoreApache
9d725af6 2713 _restoreNginx
800e3f45 2714 _clearupdns
4c2a3841 2715 if [ -z "$DEBUG" ]; then
e22bcf7c 2716 rm -f "$TLS_CONF"
2717 rm -f "$TLS_CERT"
2718 rm -f "$TLS_KEY"
2719 rm -f "$TLS_CSR"
2720 fi
4c3b3608 2721}
2722
800e3f45 2723_clearupdns() {
2724 _debug "_clearupdns"
4c2a3841 2725 if [ "$dnsadded" != 1 ] || [ -z "$vlist" ]; then
93fc48a2 2726 _debug "Dns not added, skip."
800e3f45 2727 return
2728 fi
2729
4c2a3841 2730 ventries=$(echo "$vlist" | tr ',' ' ')
2731 for ventry in $ventries; do
0c538f75 2732 d=$(echo "$ventry" | cut -d "$sep" -f 1)
2733 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
2734 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
2735 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
11927a76 2736 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
21f201e3 2737 _debug txt "$txt"
4c2a3841 2738 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
69212114 2739 _debug "$d is already verified, skip $vtype."
800e3f45 2740 continue
2741 fi
2742
4c2a3841 2743 if [ "$vtype" != "$VTYPE_DNS" ]; then
800e3f45 2744 _info "Skip $d for $vtype"
2745 continue
2746 fi
4c2a3841 2747
0c538f75 2748 d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
800e3f45 2749 _debug d_api "$d_api"
4c2a3841 2750
2751 if [ -z "$d_api" ]; then
800e3f45 2752 _info "Not Found domain api file: $d_api"
2753 continue
2754 fi
4c2a3841 2755
800e3f45 2756 (
d5ec5f80 2757 if ! . "$d_api"; then
800e3f45 2758 _err "Load file $d_api error. Please check your api file and try again."
2759 return 1
2760 fi
4c2a3841 2761
800e3f45 2762 rmcommand="${_currentRoot}_rm"
d5ec5f80 2763 if ! _exists "$rmcommand"; then
800e3f45 2764 _err "It seems that your api file doesn't define $rmcommand"
2765 return 1
2766 fi
4c2a3841 2767
800e3f45 2768 txtdomain="_acme-challenge.$d"
4c2a3841 2769
21f201e3 2770 if ! $rmcommand "$txtdomain" "$txt"; then
800e3f45 2771 _err "Error removing txt for domain:$txtdomain"
2772 return 1
2773 fi
2774 )
4c2a3841 2775
800e3f45 2776 done
2777}
2778
4c3b3608 2779# webroot removelevel tokenfile
2780_clearupwebbroot() {
2781 __webroot="$1"
4c2a3841 2782 if [ -z "$__webroot" ]; then
4c3b3608 2783 _debug "no webroot specified, skip"
2784 return 0
2785 fi
4c2a3841 2786
dcf9cb58 2787 _rmpath=""
4c2a3841 2788 if [ "$2" = '1' ]; then
dcf9cb58 2789 _rmpath="$__webroot/.well-known"
4c2a3841 2790 elif [ "$2" = '2' ]; then
dcf9cb58 2791 _rmpath="$__webroot/.well-known/acme-challenge"
4c2a3841 2792 elif [ "$2" = '3' ]; then
dcf9cb58 2793 _rmpath="$__webroot/.well-known/acme-challenge/$3"
4c3b3608 2794 else
cc179731 2795 _debug "Skip for removelevel:$2"
4c3b3608 2796 fi
4c2a3841 2797
2798 if [ "$_rmpath" ]; then
2799 if [ "$DEBUG" ]; then
dcf9cb58 2800 _debug "Debugging, skip removing: $_rmpath"
2801 else
2802 rm -rf "$_rmpath"
2803 fi
2804 fi
4c2a3841 2805
4c3b3608 2806 return 0
2807
2808}
2809
b0070f03 2810_on_before_issue() {
af1cc3b3 2811 _chk_web_roots="$1"
02140ce7 2812 _chk_main_domain="$2"
2813 _chk_alt_domains="$3"
85e1f4ea 2814 _chk_pre_hook="$4"
2815 _chk_local_addr="$5"
30c2d84c 2816 _debug _on_before_issue
d0f7c309 2817 #run pre hook
85e1f4ea 2818 if [ "$_chk_pre_hook" ]; then
2819 _info "Run pre hook:'$_chk_pre_hook'"
d0f7c309 2820 if ! (
85e1f4ea 2821 cd "$DOMAIN_PATH" && eval "$_chk_pre_hook"
d0f7c309 2822 ); then
2823 _err "Error when run pre hook."
2824 return 1
2825 fi
2826 fi
2827
af1cc3b3 2828 if _hasfield "$_chk_web_roots" "$NO_VALUE"; then
4c2a3841 2829 if ! _exists "nc"; then
0463b5d6 2830 _err "Please install netcat(nc) tools first."
2831 return 1
2832 fi
0463b5d6 2833 fi
2834
85e1f4ea 2835 _debug Le_LocalAddress "$_chk_local_addr"
4c2a3841 2836
02140ce7 2837 alldomains=$(echo "$_chk_main_domain,$_chk_alt_domains" | tr ',' ' ')
0463b5d6 2838 _index=1
2839 _currentRoot=""
2840 _addrIndex=1
4c2a3841 2841 for d in $alldomains; do
d5ec5f80 2842 _debug "Check for domain" "$d"
af1cc3b3 2843 _currentRoot="$(_getfield "$_chk_web_roots" $_index)"
0463b5d6 2844 _debug "_currentRoot" "$_currentRoot"
2845 _index=$(_math $_index + 1)
2846 _checkport=""
4c2a3841 2847 if [ "$_currentRoot" = "$NO_VALUE" ]; then
0463b5d6 2848 _info "Standalone mode."
4c2a3841 2849 if [ -z "$Le_HTTPPort" ]; then
0463b5d6 2850 Le_HTTPPort=80
2851 else
4c2a3841 2852 _savedomainconf "Le_HTTPPort" "$Le_HTTPPort"
0463b5d6 2853 fi
2854 _checkport="$Le_HTTPPort"
4c2a3841 2855 elif [ "$_currentRoot" = "$W_TLS" ]; then
0463b5d6 2856 _info "Standalone tls mode."
4c2a3841 2857 if [ -z "$Le_TLSPort" ]; then
0463b5d6 2858 Le_TLSPort=443
2859 else
4c2a3841 2860 _savedomainconf "Le_TLSPort" "$Le_TLSPort"
0463b5d6 2861 fi
2862 _checkport="$Le_TLSPort"
2863 fi
4c2a3841 2864
2865 if [ "$_checkport" ]; then
0463b5d6 2866 _debug _checkport "$_checkport"
85e1f4ea 2867 _checkaddr="$(_getfield "$_chk_local_addr" $_addrIndex)"
0463b5d6 2868 _debug _checkaddr "$_checkaddr"
4c2a3841 2869
0463b5d6 2870 _addrIndex="$(_math $_addrIndex + 1)"
4c2a3841 2871
0463b5d6 2872 _netprc="$(_ss "$_checkport" | grep "$_checkport")"
2873 netprc="$(echo "$_netprc" | grep "$_checkaddr")"
4c2a3841 2874 if [ -z "$netprc" ]; then
0463b5d6 2875 netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
2876 fi
4c2a3841 2877 if [ "$netprc" ]; then
0463b5d6 2878 _err "$netprc"
4c2a3841 2879 _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
0463b5d6 2880 _err "Please stop it first"
2881 return 1
2882 fi
2883 fi
2884 done
2885
af1cc3b3 2886 if _hasfield "$_chk_web_roots" "apache"; then
4c2a3841 2887 if ! _setApache; then
0463b5d6 2888 _err "set up apache error. Report error to me."
2889 return 1
2890 fi
2891 else
2892 usingApache=""
2893 fi
2894
b0070f03 2895}
2896
2897_on_issue_err() {
85e1f4ea 2898 _chk_post_hook="$1"
58e4d337 2899 _chk_vlist="$2"
30c2d84c 2900 _debug _on_issue_err
4c2a3841 2901 if [ "$LOG_FILE" ]; then
a73c5b33 2902 _err "Please check log file for more details: $LOG_FILE"
2903 else
54ae008d 2904 _err "Please add '--debug' or '--log' to check more details."
a73c5b33 2905 _err "See: $_DEBUG_WIKI"
2906 fi
4c2a3841 2907
b0070f03 2908 #run the post hook
85e1f4ea 2909 if [ "$_chk_post_hook" ]; then
2910 _info "Run post hook:'$_chk_post_hook'"
b0070f03 2911 if ! (
85e1f4ea 2912 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
4c2a3841 2913 ); then
b0070f03 2914 _err "Error when run post hook."
2915 return 1
2916 fi
2917 fi
58e4d337 2918
2919 #trigger the validation to flush the pending authz
2920 if [ "$_chk_vlist" ]; then
2921 (
c719a61e 2922 _debug2 "_chk_vlist" "$_chk_vlist"
2923 _debug2 "start to deactivate authz"
2924 ventries=$(echo "$_chk_vlist" | tr "$dvsep" ' ')
2925 for ventry in $ventries; do
2926 d=$(echo "$ventry" | cut -d "$sep" -f 1)
2927 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
2928 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
2929 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
2930 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
f94433e5 2931 __trigger_validation "$uri" "$keyauthorization"
c719a61e 2932 done
58e4d337 2933 )
2934 fi
2935
2936 if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
2937 _debug "$(_dlg_versions)"
2938 fi
2939
b0070f03 2940}
2941
2942_on_issue_success() {
85e1f4ea 2943 _chk_post_hook="$1"
2944 _chk_renew_hook="$2"
30c2d84c 2945 _debug _on_issue_success
b0070f03 2946 #run the post hook
85e1f4ea 2947 if [ "$_chk_post_hook" ]; then
2948 _info "Run post hook:'$_chk_post_hook'"
b0070f03 2949 if ! (
85e1f4ea 2950 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
4c2a3841 2951 ); then
b0070f03 2952 _err "Error when run post hook."
2953 return 1
2954 fi
2955 fi
4c2a3841 2956
b0070f03 2957 #run renew hook
85e1f4ea 2958 if [ "$IS_RENEW" ] && [ "$_chk_renew_hook" ]; then
2959 _info "Run renew hook:'$_chk_renew_hook'"
b0070f03 2960 if ! (
85e1f4ea 2961 cd "$DOMAIN_PATH" && eval "$_chk_renew_hook"
4c2a3841 2962 ); then
b0070f03 2963 _err "Error when run renew hook."
2964 return 1
2965 fi
4c2a3841 2966 fi
2967
b0070f03 2968}
2969
eb59817e 2970updateaccount() {
2971 _initpath
2972 _regAccount
2973}
b0070f03 2974
eb59817e 2975registeraccount() {
57e58ce7 2976 _reg_length="$1"
eb59817e 2977 _initpath
57e58ce7 2978 _regAccount "$_reg_length"
eb59817e 2979}
d404e92d 2980
8a29fbc8 2981__calcAccountKeyHash() {
ca7202eb 2982 [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH"
8a29fbc8 2983}
2984
339a8ad6 2985__calc_account_thumbprint() {
2986 printf "%s" "$jwk" | tr -d ' ' | _digest "sha256" | _url_replace
2987}
2988
57e58ce7 2989#keylength
d404e92d 2990_regAccount() {
2991 _initpath
57e58ce7 2992 _reg_length="$1"
4c2a3841 2993
5c48e139 2994 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
08b6cf02 2995 mkdir -p "$CA_DIR"
5c48e139 2996 _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
2997 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
2998 fi
4c2a3841 2999
5c48e139 3000 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
08b6cf02 3001 mkdir -p "$CA_DIR"
5c48e139 3002 _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
3003 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3004 fi
4c2a3841 3005
3006 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3007 if ! _create_account_key "$_reg_length"; then
d404e92d 3008 _err "Create account key error."
3009 return 1
3010 fi
3011 fi
4c2a3841 3012
3013 if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
d404e92d 3014 return 1
3015 fi
3016
3017 _updateTos=""
3018 _reg_res="new-reg"
4c2a3841 3019 while true; do
d404e92d 3020 _debug AGREEMENT "$AGREEMENT"
4c2a3841 3021
d404e92d 3022 regjson='{"resource": "'$_reg_res'", "agreement": "'$AGREEMENT'"}'
3023
4c2a3841 3024 if [ "$ACCOUNT_EMAIL" ]; then
d404e92d 3025 regjson='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
3026 fi
3027
4c2a3841 3028 if [ -z "$_updateTos" ]; then
d404e92d 3029 _info "Registering account"
3030
4c2a3841 3031 if ! _send_signed_request "$API/acme/new-reg" "$regjson"; then
d404e92d 3032 _err "Register account Error: $response"
3033 return 1
3034 fi
3035
4c2a3841 3036 if [ "$code" = "" ] || [ "$code" = '201' ]; then
ca7202eb 3037 echo "$response" >"$ACCOUNT_JSON_PATH"
d404e92d 3038 _info "Registered"
4c2a3841 3039 elif [ "$code" = '409' ]; then
d404e92d 3040 _info "Already registered"
3041 else
3042 _err "Register account Error: $response"
3043 return 1
3044 fi
3045
4c2a3841 3046 _accUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
d404e92d 3047 _debug "_accUri" "$_accUri"
d404e92d 3048
c2c8f320 3049 _tos="$(echo "$responseHeaders" | grep "^Link:.*rel=\"terms-of-service\"" | _head_n 1 | _egrep_o "<.*>" | tr -d '<>')"
d404e92d 3050 _debug "_tos" "$_tos"
4c2a3841 3051 if [ -z "$_tos" ]; then
d404e92d 3052 _debug "Use default tos: $DEFAULT_AGREEMENT"
3053 _tos="$DEFAULT_AGREEMENT"
3054 fi
3055 if [ "$_tos" != "$AGREEMENT" ]; then
3056 _updateTos=1
3057 AGREEMENT="$_tos"
3058 _reg_res="reg"
3059 continue
3060 fi
4c2a3841 3061
d404e92d 3062 else
3063 _debug "Update tos: $_tos"
4c2a3841 3064 if ! _send_signed_request "$_accUri" "$regjson"; then
d404e92d 3065 _err "Update tos error."
3066 return 1
3067 fi
4c2a3841 3068 if [ "$code" = '202' ]; then
eb59817e 3069 _info "Update success."
4c2a3841 3070
8a29fbc8 3071 CA_KEY_HASH="$(__calcAccountKeyHash)"
3072 _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH"
3073 _savecaconf CA_KEY_HASH "$CA_KEY_HASH"
d404e92d 3074 else
800e3f45 3075 _err "Update account error."
d404e92d 3076 return 1
3077 fi
3078 fi
339a8ad6 3079 ACCOUNT_THUMBPRINT="$(__calc_account_thumbprint)"
3080 _info "ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT"
d404e92d 3081 return 0
3082 done
3083
3084}
3085
a61fe418 3086# domain folder file
3087_findHook() {
3088 _hookdomain="$1"
3089 _hookcat="$2"
3090 _hookname="$3"
3091
c7b16249 3092 if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
3093 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname"
3094 elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
3095 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
4c2a3841 3096 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
a61fe418 3097 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname"
4c2a3841 3098 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
a61fe418 3099 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
4c2a3841 3100 elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
a61fe418 3101 d_api="$LE_WORKING_DIR/$_hookname"
4c2a3841 3102 elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
a61fe418 3103 d_api="$LE_WORKING_DIR/$_hookname.sh"
4c2a3841 3104 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
a61fe418 3105 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname"
4c2a3841 3106 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
a61fe418 3107 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
3108 fi
3109
3110 printf "%s" "$d_api"
3111}
3112
f940b2a5 3113#domain
3114__get_domain_new_authz() {
3115 _gdnd="$1"
3116 _info "Getting new-authz for domain" "$_gdnd"
4c2a3841 3117
f940b2a5 3118 _Max_new_authz_retry_times=5
3119 _authz_i=0
4c2a3841 3120 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
efd96153 3121 _debug "Try new-authz for the $_authz_i time."
4c2a3841 3122 if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
f940b2a5 3123 _err "Can not get domain new authz."
3124 return 1
3125 fi
5413bf87 3126 if _contains "$response" "No registration exists matching provided key"; then
3127 _err "It seems there is an error, but it's recovered now, please try again."
3128 _err "If you see this message for a second time, please report bug: $(__green "$PROJECT")"
3129 _clearcaconf "CA_KEY_HASH"
3130 break
3131 fi
4c2a3841 3132 if ! _contains "$response" "An error occurred while processing your request"; then
f940b2a5 3133 _info "The new-authz request is ok."
3134 break
3135 fi
3136 _authz_i="$(_math "$_authz_i" + 1)"
9e45ac93 3137 _info "The server is busy, Sleep $_authz_i to retry."
f940b2a5 3138 _sleep "$_authz_i"
4c2a3841 3139 done
f940b2a5 3140
4c2a3841 3141 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
efd96153 3142 _err "new-authz retry reach the max $_Max_new_authz_retry_times times."
f940b2a5 3143 fi
4c2a3841 3144
3145 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
f940b2a5 3146 _err "new-authz error: $response"
3147 return 1
3148 fi
3149
3150}
3151
58e4d337 3152#uri keyAuthorization
f94433e5 3153__trigger_validation() {
58e4d337 3154 _debug2 "tigger domain validation."
3155 _t_url="$1"
3156 _debug2 _t_url "$_t_url"
3157 _t_key_authz="$2"
3158 _debug2 _t_key_authz "$_t_key_authz"
3159 _send_signed_request "$_t_url" "{\"resource\": \"challenge\", \"keyAuthorization\": \"$_t_key_authz\"}"
3160}
3161
3c07f57a 3162#webroot, domain domainlist keylength
4c3b3608 3163issue() {
4c2a3841 3164 if [ -z "$2" ]; then
43822d37 3165 _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
4c3b3608 3166 return 1
3167 fi
49d75a0c 3168 if [ -z "$1" ]; then
3169 _usage "Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc."
3170 return 1
3171 fi
af1cc3b3 3172 _web_roots="$1"
3173 _main_domain="$2"
02140ce7 3174 _alt_domains="$3"
af1cc3b3 3175 if _contains "$_main_domain" ","; then
3176 _main_domain=$(echo "$2,$3" | cut -d , -f 1)
02140ce7 3177 _alt_domains=$(echo "$2,$3" | cut -d , -f 2- | sed "s/,${NO_VALUE}$//")
2aff36e7 3178 fi
d9c9114b 3179 _key_length="$4"
85e1f4ea 3180 _real_cert="$5"
3181 _real_key="$6"
3182 _real_ca="$7"
3183 _reload_cmd="$8"
3184 _real_fullchain="$9"
3185 _pre_hook="${10}"
3186 _post_hook="${11}"
3187 _renew_hook="${12}"
3188 _local_addr="${13}"
4c2a3841 3189
eccec5f6 3190 #remove these later.
af1cc3b3 3191 if [ "$_web_roots" = "dns-cf" ]; then
3192 _web_roots="dns_cf"
eccec5f6 3193 fi
af1cc3b3 3194 if [ "$_web_roots" = "dns-dp" ]; then
3195 _web_roots="dns_dp"
eccec5f6 3196 fi
af1cc3b3 3197 if [ "$_web_roots" = "dns-cx" ]; then
3198 _web_roots="dns_cx"
eccec5f6 3199 fi
950172dc 3200 _debug "Using api: $API"
4c2a3841 3201
3202 if [ ! "$IS_RENEW" ]; then
d9c9114b 3203 _initpath "$_main_domain" "$_key_length"
43822d37 3204 mkdir -p "$DOMAIN_PATH"
3205 fi
eccec5f6 3206
4c2a3841 3207 if [ -f "$DOMAIN_CONF" ]; then
61623d22 3208 Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
a4270efa 3209 _debug Le_NextRenewTime "$Le_NextRenewTime"
95e06de5 3210 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
bb25febd 3211 _saved_domain=$(_readdomainconf Le_Domain)
3212 _debug _saved_domain "$_saved_domain"
3213 _saved_alt=$(_readdomainconf Le_Alt)
3214 _debug _saved_alt "$_saved_alt"
02140ce7 3215 if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then
bb25febd 3216 _info "Domains not changed."
3217 _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")"
4c2a3841 3218 _info "Add '$(__red '--force')' to force to renew."
bb25febd 3219 return $RENEW_SKIP
3220 else
3221 _info "Domains have changed."
3222 fi
4c3b3608 3223 fi
3224 fi
96a46cfc 3225
af1cc3b3 3226 _savedomainconf "Le_Domain" "$_main_domain"
02140ce7 3227 _savedomainconf "Le_Alt" "$_alt_domains"
af1cc3b3 3228 _savedomainconf "Le_Webroot" "$_web_roots"
4c2a3841 3229
85e1f4ea 3230 _savedomainconf "Le_PreHook" "$_pre_hook"
3231 _savedomainconf "Le_PostHook" "$_post_hook"
3232 _savedomainconf "Le_RenewHook" "$_renew_hook"
4c2a3841 3233
85e1f4ea 3234 if [ "$_local_addr" ]; then
3235 _savedomainconf "Le_LocalAddress" "$_local_addr"
72518d48 3236 else
3237 _cleardomainconf "Le_LocalAddress"
3238 fi
6ae0f7f5 3239
f6dcd989 3240 Le_API="$API"
3241 _savedomainconf "Le_API" "$Le_API"
4c2a3841 3242
02140ce7 3243 if [ "$_alt_domains" = "$NO_VALUE" ]; then
3244 _alt_domains=""
4c3b3608 3245 fi
4c2a3841 3246
d9c9114b 3247 if [ "$_key_length" = "$NO_VALUE" ]; then
3248 _key_length=""
d404e92d 3249 fi
4c2a3841 3250
85e1f4ea 3251 if ! _on_before_issue "$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
0463b5d6 3252 _err "_on_before_issue."
3253 return 1
4c3b3608 3254 fi
0463b5d6 3255
8a29fbc8 3256 _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")"
3257 _debug2 _saved_account_key_hash "$_saved_account_key_hash"
4c2a3841 3258
3259 if [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
57e58ce7 3260 if ! _regAccount "$_accountkeylength"; then
85e1f4ea 3261 _on_issue_err "$_post_hook"
8a29fbc8 3262 return 1
3263 fi
57e58ce7 3264 else
3265 _debug "_saved_account_key_hash is not changed, skip register account."
166096dc 3266 fi
166096dc 3267
4c2a3841 3268 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
10afcaca 3269 _info "Signing from existing CSR."
3270 else
3271 _key=$(_readdomainconf Le_Keylength)
3272 _debug "Read key length:$_key"
d9c9114b 3273 if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ]; then
3274 if ! createDomainKey "$_main_domain" "$_key_length"; then
10afcaca 3275 _err "Create domain key error."
3276 _clearup
85e1f4ea 3277 _on_issue_err "$_post_hook"
10afcaca 3278 return 1
3279 fi
3280 fi
3281
02140ce7 3282 if ! _createcsr "$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
10afcaca 3283 _err "Create CSR error."
5ef501c5 3284 _clearup
85e1f4ea 3285 _on_issue_err "$_post_hook"
41e3eafa 3286 return 1
3287 fi
4c3b3608 3288 fi
10afcaca 3289
d9c9114b 3290 _savedomainconf "Le_Keylength" "$_key_length"
4c2a3841 3291
4c3b3608 3292 vlist="$Le_Vlist"
cae203be 3293
3294 _info "Getting domain auth token for each domain"
4c3b3608 3295 sep='#'
9d725af6 3296 dvsep=','
4c2a3841 3297 if [ -z "$vlist" ]; then
02140ce7 3298 alldomains=$(echo "$_main_domain,$_alt_domains" | tr ',' ' ')
a63b05a9 3299 _index=1
3300 _currentRoot=""
4c2a3841 3301 for d in $alldomains; do
ca7202eb 3302 _info "Getting webroot for domain" "$d"
af1cc3b3 3303 _w="$(echo $_web_roots | cut -d , -f $_index)"
9d725af6 3304 _debug _w "$_w"
4c2a3841 3305 if [ "$_w" ]; then
a63b05a9 3306 _currentRoot="$_w"
3307 fi
3308 _debug "_currentRoot" "$_currentRoot"
00a50605 3309 _index=$(_math $_index + 1)
4c2a3841 3310
a63b05a9 3311 vtype="$VTYPE_HTTP"
4c2a3841 3312 if _startswith "$_currentRoot" "dns"; then
a63b05a9 3313 vtype="$VTYPE_DNS"
3314 fi
4c2a3841 3315
3316 if [ "$_currentRoot" = "$W_TLS" ]; then
e22bcf7c 3317 vtype="$VTYPE_TLS"
3318 fi
c4d8fd83 3319
4c2a3841 3320 if ! __get_domain_new_authz "$d"; then
c4d8fd83 3321 _clearup
85e1f4ea 3322 _on_issue_err "$_post_hook"
c4d8fd83 3323 return 1
3324 fi
3325
4c2a3841 3326 if [ -z "$thumbprint" ]; then
339a8ad6 3327 thumbprint="$(__calc_account_thumbprint)"
4c3b3608 3328 fi
3329
4c2a3841 3330 entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
4c3b3608 3331 _debug entry "$entry"
4c2a3841 3332 if [ -z "$entry" ]; then
19539575 3333 _err "Error, can not get domain token $d"
3334 _clearup
85e1f4ea 3335 _on_issue_err "$_post_hook"
19539575 3336 return 1
3337 fi
22ea4004 3338 token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
ca7202eb 3339 _debug token "$token"
4c2a3841 3340
3341 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
ca7202eb 3342 _debug uri "$uri"
cae203be 3343
4c3b3608 3344 keyauthorization="$token.$thumbprint"
3345 _debug keyauthorization "$keyauthorization"
3346
95e06de5 3347 if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
69212114 3348 _debug "$d is already verified, skip."
ca7202eb 3349 keyauthorization="$STATE_VERIFIED"
d35bf517 3350 _debug keyauthorization "$keyauthorization"
ec603bee 3351 fi
3352
a63b05a9 3353 dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
4c3b3608 3354 _debug dvlist "$dvlist"
4c2a3841 3355
9d725af6 3356 vlist="$vlist$dvlist$dvsep"
4c3b3608 3357
3358 done
9d725af6 3359 _debug vlist "$vlist"
4c3b3608 3360 #add entry
3361 dnsadded=""
9d725af6 3362 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
4c2a3841 3363 for ventry in $ventries; do
ca7202eb 3364 d=$(echo "$ventry" | cut -d "$sep" -f 1)
3365 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
3366 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
3367 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
ec603bee 3368
4c2a3841 3369 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
69212114 3370 _debug "$d is already verified, skip $vtype."
ec603bee 3371 continue
3372 fi
3373
4c2a3841 3374 if [ "$vtype" = "$VTYPE_DNS" ]; then
4c3b3608 3375 dnsadded='0'
3376 txtdomain="_acme-challenge.$d"
3377 _debug txtdomain "$txtdomain"
11927a76 3378 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
4c3b3608 3379 _debug txt "$txt"
a61fe418 3380
0c538f75 3381 d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
a61fe418 3382
4c3b3608 3383 _debug d_api "$d_api"
4c2a3841 3384
3385 if [ "$d_api" ]; then
4c3b3608 3386 _info "Found domain api file: $d_api"
3387 else
3388 _err "Add the following TXT record:"
0c538f75 3389 _err "Domain: '$(__green "$txtdomain")'"
3390 _err "TXT value: '$(__green "$txt")'"
4c3b3608 3391 _err "Please be aware that you prepend _acme-challenge. before your domain"
3392 _err "so the resulting subdomain will be: $txtdomain"
3393 continue
3394 fi
4c2a3841 3395
73b8b120 3396 (
ca7202eb 3397 if ! . "$d_api"; then
73b8b120 3398 _err "Load file $d_api error. Please check your api file and try again."
3399 return 1
3400 fi
4c2a3841 3401
158f22f7 3402 addcommand="${_currentRoot}_add"
ca7202eb 3403 if ! _exists "$addcommand"; then
73b8b120 3404 _err "It seems that your api file is not correct, it must have a function named: $addcommand"
3405 return 1
3406 fi
4c2a3841 3407
ca7202eb 3408 if ! $addcommand "$txtdomain" "$txt"; then
73b8b120 3409 _err "Error add txt for domain:$txtdomain"
3410 return 1
3411 fi
3412 )
4c2a3841 3413
3414 if [ "$?" != "0" ]; then
5ef501c5 3415 _clearup
85e1f4ea 3416 _on_issue_err "$_post_hook"
4c3b3608 3417 return 1
3418 fi
3419 dnsadded='1'
3420 fi
3421 done
3422
4c2a3841 3423 if [ "$dnsadded" = '0' ]; then
3424 _savedomainconf "Le_Vlist" "$vlist"
4c3b3608 3425 _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
3426 _err "Please add the TXT records to the domains, and retry again."
5ef501c5 3427 _clearup
85e1f4ea 3428 _on_issue_err "$_post_hook"
4c3b3608 3429 return 1
3430 fi
4c2a3841 3431
4c3b3608 3432 fi
4c2a3841 3433
3434 if [ "$dnsadded" = '1' ]; then
3435 if [ -z "$Le_DNSSleep" ]; then
ca7202eb 3436 Le_DNSSleep="$DEFAULT_DNS_SLEEP"
0e38c60d 3437 else
4c2a3841 3438 _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
0e38c60d 3439 fi
3440
5fbc47eb 3441 _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
ca7202eb 3442 _sleep "$Le_DNSSleep"
4c3b3608 3443 fi
4c2a3841 3444
5d943a35 3445 NGINX_RESTORE_VLIST=""
4c3b3608 3446 _debug "ok, let's start to verify"
a63b05a9 3447
0463b5d6 3448 _ncIndex=1
9d725af6 3449 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
4c2a3841 3450 for ventry in $ventries; do
ca7202eb 3451 d=$(echo "$ventry" | cut -d "$sep" -f 1)
3452 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
3453 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
3454 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
3455 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
ec603bee 3456
4c2a3841 3457 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
ec603bee 3458 _info "$d is already verified, skip $vtype."
3459 continue
3460 fi
3461
4c3b3608 3462 _info "Verifying:$d"
3463 _debug "d" "$d"
3464 _debug "keyauthorization" "$keyauthorization"
3465 _debug "uri" "$uri"
3466 removelevel=""
e22bcf7c 3467 token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
a63b05a9 3468
3469 _debug "_currentRoot" "$_currentRoot"
3470
4c2a3841 3471 if [ "$vtype" = "$VTYPE_HTTP" ]; then
3472 if [ "$_currentRoot" = "$NO_VALUE" ]; then
4c3b3608 3473 _info "Standalone mode server"
85e1f4ea 3474 _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
0463b5d6 3475 _ncIndex="$(_math $_ncIndex + 1)"
3476 _startserver "$keyauthorization" "$_ncaddr" &
4c2a3841 3477 if [ "$?" != "0" ]; then
5ef501c5 3478 _clearup
58e4d337 3479 _on_issue_err "$_post_hook" "$vlist"
6fc1447f 3480 return 1
3481 fi
4c3b3608 3482 serverproc="$!"
5dbf664a 3483 sleep 1
ca7202eb 3484 _debug serverproc "$serverproc"
0e44f587 3485 elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
3486 _info "Stateless mode for domain:$d"
3487 _sleep 1
9d725af6 3488 elif _startswith "$_currentRoot" "$NGINX"; then
3489 _info "Nginx mode for domain:$d"
3490 #set up nginx server
3491 FOUND_REAL_NGINX_CONF=""
3492 BACKUP_NGINX_CONF=""
3493 if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then
3494 _clearup
58e4d337 3495 _on_issue_err "$_post_hook" "$vlist"
9d725af6 3496 return 1
03f8d6e9 3497 fi
302c41ed 3498
03f8d6e9 3499 if [ "$FOUND_REAL_NGINX_CONF" ]; then
9d725af6 3500 _realConf="$FOUND_REAL_NGINX_CONF"
3501 _backup="$BACKUP_NGINX_CONF"
3502 _debug _realConf "$_realConf"
5d943a35 3503 NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
9d725af6 3504 fi
3505 _sleep 1
4c3b3608 3506 else
4c2a3841 3507 if [ "$_currentRoot" = "apache" ]; then
6f930641 3508 wellknown_path="$ACME_DIR"
3509 else
a63b05a9 3510 wellknown_path="$_currentRoot/.well-known/acme-challenge"
4c2a3841 3511 if [ ! -d "$_currentRoot/.well-known" ]; then
6f930641 3512 removelevel='1'
4c2a3841 3513 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
6f930641 3514 removelevel='2'
3515 else
3516 removelevel='3'
3517 fi
4c3b3608 3518 fi
6f930641 3519
4c3b3608 3520 _debug wellknown_path "$wellknown_path"
6f930641 3521
4c3b3608 3522 _debug "writing token:$token to $wellknown_path/$token"
3523
3524 mkdir -p "$wellknown_path"
93fc48a2 3525
4c2a3841 3526 if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
93fc48a2 3527 _err "$d:Can not write token to file : $wellknown_path/$token"
3528 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3529 _clearup
58e4d337 3530 _on_issue_err "$_post_hook" "$vlist"
93fc48a2 3531 return 1
3532 fi
3533
4c2a3841 3534 if [ ! "$usingApache" ]; then
44edb2bd 3535 if webroot_owner=$(_stat "$_currentRoot"); then
32fdc196 3536 _debug "Changing owner/group of .well-known to $webroot_owner"
c87cd0de 3537 if ! _exec "chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then
3538 _debug "$(cat "$_EXEC_TEMP_ERR")"
3539 _exec_err >/dev/null 2>&1
3540 fi
32fdc196 3541 else
b54ce310 3542 _debug "not changing owner/group of webroot"
32fdc196 3543 fi
df886ffa 3544 fi
4c2a3841 3545
4c3b3608 3546 fi
4c2a3841 3547
3548 elif [ "$vtype" = "$VTYPE_TLS" ]; then
e22bcf7c 3549 #create A
3550 #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
3551 #_debug2 _hash_A "$_hash_A"
3552 #_x="$(echo $_hash_A | cut -c 1-32)"
3553 #_debug2 _x "$_x"
3554 #_y="$(echo $_hash_A | cut -c 33-64)"
3555 #_debug2 _y "$_y"
3556 #_SAN_A="$_x.$_y.token.acme.invalid"
3557 #_debug2 _SAN_A "$_SAN_A"
4c2a3841 3558
e22bcf7c 3559 #create B
0c538f75 3560 _hash_B="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")"
e22bcf7c 3561 _debug2 _hash_B "$_hash_B"
0c538f75 3562 _x="$(echo "$_hash_B" | cut -c 1-32)"
e22bcf7c 3563 _debug2 _x "$_x"
0c538f75 3564 _y="$(echo "$_hash_B" | cut -c 33-64)"
e22bcf7c 3565 _debug2 _y "$_y"
4c2a3841 3566
e22bcf7c 3567 #_SAN_B="$_x.$_y.ka.acme.invalid"
4c2a3841 3568
e22bcf7c 3569 _SAN_B="$_x.$_y.acme.invalid"
3570 _debug2 _SAN_B "$_SAN_B"
4c2a3841 3571
85e1f4ea 3572 _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
0c538f75 3573 _ncIndex="$(_math "$_ncIndex" + 1)"
0463b5d6 3574 if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
e22bcf7c 3575 _err "Start tls server error."
3576 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3577 _clearup
58e4d337 3578 _on_issue_err "$_post_hook" "$vlist"
e22bcf7c 3579 return 1
3580 fi
4c3b3608 3581 fi
4c2a3841 3582
f94433e5 3583 if ! __trigger_validation "$uri" "$keyauthorization"; then
c4d8fd83 3584 _err "$d:Can not get challenge: $response"
3585 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3586 _clearup
58e4d337 3587 _on_issue_err "$_post_hook" "$vlist"
c4d8fd83 3588 return 1
3589 fi
4c2a3841 3590
3591 if [ ! -z "$code" ] && [ ! "$code" = '202' ]; then
c60883ef 3592 _err "$d:Challenge error: $response"
a63b05a9 3593 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 3594 _clearup
58e4d337 3595 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 3596 return 1
3597 fi
4c2a3841 3598
6fc1447f 3599 waittimes=0
4c2a3841 3600 if [ -z "$MAX_RETRY_TIMES" ]; then
6fc1447f 3601 MAX_RETRY_TIMES=30
3602 fi
4c2a3841 3603
3604 while true; do
0c538f75 3605 waittimes=$(_math "$waittimes" + 1)
4c2a3841 3606 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
6fc1447f 3607 _err "$d:Timeout"
3608 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3609 _clearup
58e4d337 3610 _on_issue_err "$_post_hook" "$vlist"
6fc1447f 3611 return 1
3612 fi
4c2a3841 3613
5dbf664a 3614 _debug "sleep 2 secs to verify"
3615 sleep 2
4c3b3608 3616 _debug "checking"
44edb2bd 3617 response="$(_get "$uri")"
4c2a3841 3618 if [ "$?" != "0" ]; then
c60883ef 3619 _err "$d:Verify error:$response"
a63b05a9 3620 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 3621 _clearup
58e4d337 3622 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 3623 return 1
3624 fi
9aaf36cd 3625 _debug2 original "$response"
4c2a3841 3626
3627 response="$(echo "$response" | _normalizeJson)"
7012b91f 3628 _debug2 response "$response"
4c2a3841 3629
3630 status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
3631 if [ "$status" = "valid" ]; then
93f3098a 3632 _info "$(__green Success)"
ca7202eb 3633 _stopserver "$serverproc"
4c3b3608 3634 serverproc=""
a63b05a9 3635 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c2a3841 3636 break
4c3b3608 3637 fi
4c2a3841 3638
3639 if [ "$status" = "invalid" ]; then
3640 error="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error":\{[^\}]*')"
3641 _debug2 error "$error"
3642 errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
3643 _debug2 errordetail "$errordetail"
3644 if [ "$errordetail" ]; then
3645 _err "$d:Verify error:$errordetail"
3646 else
3647 _err "$d:Verify error:$error"
3648 fi
3649 if [ "$DEBUG" ]; then
3650 if [ "$vtype" = "$VTYPE_HTTP" ]; then
3651 _debug "Debug: get token url."
3652 _get "http://$d/.well-known/acme-challenge/$token" "" 1
3653 fi
3654 fi
a63b05a9 3655 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 3656 _clearup
58e4d337 3657 _on_issue_err "$_post_hook" "$vlist"
4c2a3841 3658 return 1
4c3b3608 3659 fi
4c2a3841 3660
3661 if [ "$status" = "pending" ]; then
4c3b3608 3662 _info "Pending"
3663 else
4c2a3841 3664 _err "$d:Verify error:$response"
a63b05a9 3665 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 3666 _clearup
58e4d337 3667 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 3668 return 1
3669 fi
4c2a3841 3670
4c3b3608 3671 done
4c2a3841 3672
4c3b3608 3673 done
3674
3675 _clearup
3676 _info "Verify finished, start to sign."
11927a76 3677 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)"
4c2a3841 3678
3679 if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"; then
c4d8fd83 3680 _err "Sign failed."
85e1f4ea 3681 _on_issue_err "$_post_hook"
c4d8fd83 3682 return 1
3683 fi
4c2a3841 3684
d404e92d 3685 _rcert="$response"
0c538f75 3686 Le_LinkCert="$(grep -i '^Location.*$' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
d8ba26e6 3687 _debug "Le_LinkCert" "$Le_LinkCert"
4c2a3841 3688 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
4c3b3608 3689
4c2a3841 3690 if [ "$Le_LinkCert" ]; then
3691 echo "$BEGIN_CERT" >"$CERT_PATH"
4c3b3608 3692
72518d48 3693 #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
3694 # _debug "Get cert failed. Let's try last response."
3c07f57a 3695 # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
72518d48 3696 #fi
4c2a3841 3697
3698 if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then
72518d48 3699 _debug "Try cert link."
4c2a3841 3700 _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH"
d404e92d 3701 fi
3702
4c2a3841 3703 echo "$END_CERT" >>"$CERT_PATH"
43822d37 3704 _info "$(__green "Cert success.")"
4c3b3608 3705 cat "$CERT_PATH"
5980ebc7 3706
4c2a3841 3707 _info "Your cert is in $(__green " $CERT_PATH ")"
3708
3709 if [ -f "$CERT_KEY_PATH" ]; then
3710 _info "Your cert key is in $(__green " $CERT_KEY_PATH ")"
5980ebc7 3711 fi
3712
caf1fc10 3713 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
281aa349 3714
4c2a3841 3715 if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
281aa349 3716 USER_PATH="$PATH"
3717 _saveaccountconf "USER_PATH" "$USER_PATH"
3718 fi
4c3b3608 3719 fi
4c3b3608 3720
4c2a3841 3721 if [ -z "$Le_LinkCert" ]; then
0c538f75 3722 response="$(echo "$response" | _dbase64 "multiline" | _normalizeJson)"
4c2a3841 3723 _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
85e1f4ea 3724 _on_issue_err "$_post_hook"
4c3b3608 3725 return 1
3726 fi
4c2a3841 3727
3728 _cleardomainconf "Le_Vlist"
3729
0c538f75 3730 Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
4c2a3841 3731 if ! _contains "$Le_LinkIssuer" ":"; then
fac1e367 3732 Le_LinkIssuer="$API$Le_LinkIssuer"
3733 fi
d8ba26e6 3734 _debug Le_LinkIssuer "$Le_LinkIssuer"
4c2a3841 3735 _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
3736
3737 if [ "$Le_LinkIssuer" ]; then
232c7361 3738 _link_issuer_retry=0
3739 _MAX_ISSUER_RETRY=5
d8ba26e6 3740 while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do
3741 _debug _link_issuer_retry "$_link_issuer_retry"
3742 if _get "$Le_LinkIssuer" >"$CA_CERT_PATH.der"; then
3743 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
3744 _base64 "multiline" <"$CA_CERT_PATH.der" >>"$CA_CERT_PATH"
3745 echo "$END_CERT" >>"$CA_CERT_PATH"
3746
3747 _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")"
3748 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
3749 _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")"
3750
3751 rm -f "$CA_CERT_PATH.der"
3752 break
3753 fi
3754 _link_issuer_retry=$(_math $_link_issuer_retry + 1)
3755 _sleep "$_link_issuer_retry"
3756 done
3757 if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then
3758 _err "Max retry for issuer ca cert is reached."
3759 fi
3760 else
3761 _debug "No Le_LinkIssuer header found."
4c3b3608 3762 fi
4c2a3841 3763
3aae1ae3 3764 Le_CertCreateTime=$(_time)
4c2a3841 3765 _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
3766
3767 Le_CertCreateTimeStr=$(date -u)
3768 _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
3769
3770 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ]; then
ca7202eb 3771 Le_RenewalDays="$MAX_RENEW"
054cb72e 3772 else
4c2a3841 3773 _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
13d7cae9 3774 fi
4c2a3841 3775
3776 if [ "$CA_BUNDLE" ]; then
78009539
PS
3777 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
3778 else
3779 _clearaccountconf "CA_BUNDLE"
3780 fi
3781
2aa75f03 3782 if [ "$CA_PATH" ]; then
3783 _saveaccountconf CA_PATH "$CA_PATH"
3784 else
3785 _clearaccountconf "CA_PATH"
3786 fi
78009539 3787
4c2a3841 3788 if [ "$HTTPS_INSECURE" ]; then
fac1e367 3789 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
3790 else
4c2a3841 3791 _clearaccountconf "HTTPS_INSECURE"
13d7cae9 3792 fi
00a50605 3793
4c2a3841 3794 if [ "$Le_Listen_V4" ]; then
3795 _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
50827188 3796 _cleardomainconf Le_Listen_V6
4c2a3841 3797 elif [ "$Le_Listen_V6" ]; then
3798 _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
50827188 3799 _cleardomainconf Le_Listen_V4
3800 fi
f6dcd989 3801
ca7202eb 3802 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
4c2a3841 3803
ca7202eb 3804 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
4c2a3841 3805 _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
3806
ca7202eb 3807 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
4c2a3841 3808 _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
f6dcd989 3809
85e1f4ea 3810 _on_issue_success "$_post_hook" "$_renew_hook"
4c3b3608 3811
85e1f4ea 3812 if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
3813 _savedomainconf "Le_RealCertPath" "$_real_cert"
3814 _savedomainconf "Le_RealCACertPath" "$_real_ca"
3815 _savedomainconf "Le_RealKeyPath" "$_real_key"
3816 _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
3817 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
044da37c 3818 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
01f54558 3819 fi
4c0d3f1b 3820
4c3b3608 3821}
3822
43822d37 3823#domain [isEcc]
4c3b3608 3824renew() {
3825 Le_Domain="$1"
4c2a3841 3826 if [ -z "$Le_Domain" ]; then
43822d37 3827 _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
4c3b3608 3828 return 1
3829 fi
3830
43822d37 3831 _isEcc="$2"
3832
e799ef29 3833 _initpath "$Le_Domain" "$_isEcc"
43822d37 3834
e2053b22 3835 _info "$(__green "Renew: '$Le_Domain'")"
4c2a3841 3836 if [ ! -f "$DOMAIN_CONF" ]; then
43822d37 3837 _info "'$Le_Domain' is not a issued domain, skip."
4c2a3841 3838 return 0
4c3b3608 3839 fi
4c2a3841 3840
3841 if [ "$Le_RenewalDays" ]; then
1e6b68f5 3842 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
3843 fi
3844
8663fb7e 3845 . "$DOMAIN_CONF"
4c2a3841 3846
3847 if [ "$Le_API" ]; then
5c48e139 3848 API="$Le_API"
c4236e58 3849 #reload ca configs
3850 ACCOUNT_KEY_PATH=""
3851 ACCOUNT_JSON_PATH=""
3852 CA_CONF=""
3853 _debug3 "initpath again."
3854 _initpath "$Le_Domain" "$_isEcc"
5c48e139 3855 fi
4c2a3841 3856
3857 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
e2053b22 3858 _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
3859 _info "Add '$(__red '--force')' to force to renew."
e799ef29 3860 return "$RENEW_SKIP"
4c3b3608 3861 fi
4c2a3841 3862
4c3b3608 3863 IS_RENEW="1"
0463b5d6 3864 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 3865 res="$?"
4c2a3841 3866 if [ "$res" != "0" ]; then
e799ef29 3867 return "$res"
a61fe418 3868 fi
4c2a3841 3869
3870 if [ "$Le_DeployHook" ]; then
93bce1b2 3871 _deploy "$Le_Domain" "$Le_DeployHook"
e799ef29 3872 res="$?"
a61fe418 3873 fi
4c2a3841 3874
4c3b3608 3875 IS_RENEW=""
3876
e799ef29 3877 return "$res"
4c3b3608 3878}
3879
cc179731 3880#renewAll [stopRenewOnError]
4c3b3608 3881renewAll() {
3882 _initpath
cc179731 3883 _stopRenewOnError="$1"
3884 _debug "_stopRenewOnError" "$_stopRenewOnError"
3885 _ret="0"
43822d37 3886
e591d5cf 3887 for di in "${CERT_HOME}"/*.*/; do
3888 _debug di "$di"
44483dba 3889 if ! [ -d "$di" ]; then
3498a585 3890 _debug "Not directory, skip: $di"
3891 continue
3892 fi
e591d5cf 3893 d=$(basename "$di")
201aa244 3894 _debug d "$d"
43822d37 3895 (
201aa244 3896 if _endswith "$d" "$ECC_SUFFIX"; then
3897 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
3898 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 3899 fi
3900 renew "$d" "$_isEcc"
4d2f38b0 3901 )
cc179731 3902 rc="$?"
3903 _debug "Return code: $rc"
4c2a3841 3904 if [ "$rc" != "0" ]; then
3905 if [ "$rc" = "$RENEW_SKIP" ]; then
cc179731 3906 _info "Skipped $d"
4c2a3841 3907 elif [ "$_stopRenewOnError" ]; then
cc179731 3908 _err "Error renew $d, stop now."
201aa244 3909 return "$rc"
cc179731 3910 else
3911 _ret="$rc"
482cb737 3912 _err "Error renew $d."
cc179731 3913 fi
3914 fi
4c3b3608 3915 done
201aa244 3916 return "$_ret"
4c3b3608 3917}
3918
10afcaca 3919#csr webroot
4c2a3841 3920signcsr() {
10afcaca 3921 _csrfile="$1"
3922 _csrW="$2"
3923 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
3924 _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ "
3925 return 1
3926 fi
3927
3928 _initpath
3929
3930 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 3931 if [ "$?" != "0" ]; then
10afcaca 3932 _err "Can not read subject from csr: $_csrfile"
3933 return 1
3934 fi
ad752b31 3935 _debug _csrsubj "$_csrsubj"
10afcaca 3936
3937 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 3938 if [ "$?" != "0" ]; then
10afcaca 3939 _err "Can not read domain list from csr: $_csrfile"
3940 return 1
3941 fi
3942 _debug "_csrdomainlist" "$_csrdomainlist"
4c2a3841 3943
3944 if [ -z "$_csrsubj" ]; then
ad752b31 3945 _csrsubj="$(_getfield "$_csrdomainlist" 1)"
3946 _debug _csrsubj "$_csrsubj"
3947 _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
3948 _debug "_csrdomainlist" "$_csrdomainlist"
3949 fi
4c2a3841 3950
3951 if [ -z "$_csrsubj" ]; then
ad752b31 3952 _err "Can not read subject from csr: $_csrfile"
3953 return 1
3954 fi
4c2a3841 3955
10afcaca 3956 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 3957 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 3958 _err "Can not read key length from csr: $_csrfile"
3959 return 1
3960 fi
4c2a3841 3961
10afcaca 3962 _initpath "$_csrsubj" "$_csrkeylength"
3963 mkdir -p "$DOMAIN_PATH"
4c2a3841 3964
10afcaca 3965 _info "Copy csr to: $CSR_PATH"
3966 cp "$_csrfile" "$CSR_PATH"
4c2a3841 3967
10afcaca 3968 issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength"
4c2a3841 3969
10afcaca 3970}
3971
3972showcsr() {
4c2a3841 3973 _csrfile="$1"
10afcaca 3974 _csrd="$2"
3975 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
3976 _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr"
3977 return 1
3978 fi
3979
3980 _initpath
4c2a3841 3981
10afcaca 3982 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 3983 if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then
10afcaca 3984 _err "Can not read subject from csr: $_csrfile"
3985 return 1
3986 fi
4c2a3841 3987
10afcaca 3988 _info "Subject=$_csrsubj"
3989
3990 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 3991 if [ "$?" != "0" ]; then
10afcaca 3992 _err "Can not read domain list from csr: $_csrfile"
3993 return 1
3994 fi
3995 _debug "_csrdomainlist" "$_csrdomainlist"
3996
3997 _info "SubjectAltNames=$_csrdomainlist"
3998
10afcaca 3999 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 4000 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 4001 _err "Can not read key length from csr: $_csrfile"
4002 return 1
4003 fi
4004 _info "KeyLength=$_csrkeylength"
4005}
4006
6d7eda3e 4007list() {
22ea4004 4008 _raw="$1"
6d7eda3e 4009 _initpath
4c2a3841 4010
dcf4f8f6 4011 _sep="|"
4c2a3841 4012 if [ "$_raw" ]; then
d5ec5f80 4013 printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew"
e591d5cf 4014 for di in "${CERT_HOME}"/*.*/; do
44483dba 4015 if ! [ -d "$di" ]; then
3498a585 4016 _debug "Not directory, skip: $di"
4017 continue
4018 fi
e591d5cf 4019 d=$(basename "$di")
201aa244 4020 _debug d "$d"
dcf4f8f6 4021 (
201aa244 4022 if _endswith "$d" "$ECC_SUFFIX"; then
4023 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
4024 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 4025 fi
e591d5cf 4026 _initpath "$d" "$_isEcc"
4c2a3841 4027 if [ -f "$DOMAIN_CONF" ]; then
dcf4f8f6 4028 . "$DOMAIN_CONF"
d5ec5f80 4029 printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
dcf4f8f6 4030 fi
4031 )
4032 done
4033 else
4c2a3841 4034 if _exists column; then
22ea4004 4035 list "raw" | column -t -s "$_sep"
4036 else
43822d37 4037 list "raw" | tr "$_sep" '\t'
22ea4004 4038 fi
dcf4f8f6 4039 fi
6d7eda3e 4040
6d7eda3e 4041}
4042
93bce1b2 4043_deploy() {
4044 _d="$1"
4045 _hooks="$2"
4046
4047 for _d_api in $(echo "$_hooks" | tr ',' " "); do
4048 _deployApi="$(_findHook "$_d" deploy "$_d_api")"
4049 if [ -z "$_deployApi" ]; then
4050 _err "The deploy hook $_d_api is not found."
4051 return 1
4052 fi
4053 _debug _deployApi "$_deployApi"
4054
4055 if ! (
4056 if ! . "$_deployApi"; then
4057 _err "Load file $_deployApi error. Please check your api file and try again."
4058 return 1
4059 fi
4060
4061 d_command="${_d_api}_deploy"
4062 if ! _exists "$d_command"; then
4063 _err "It seems that your api file is not correct, it must have a function named: $d_command"
4064 return 1
4065 fi
4066
4067 if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
4068 _err "Error deploy for domain:$_d"
4069 return 1
4070 fi
4071 ); then
4072 _err "Deploy error."
4073 return 1
4074 else
4075 _info "$(__green Success)"
4076 fi
4077 done
4078}
4079
4080#domain hooks
a61fe418 4081deploy() {
93bce1b2 4082 _d="$1"
4083 _hooks="$2"
a61fe418 4084 _isEcc="$3"
93bce1b2 4085 if [ -z "$_hooks" ]; then
a61fe418 4086 _usage "Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] "
4087 return 1
4088 fi
4089
93bce1b2 4090 _initpath "$_d" "$_isEcc"
4c2a3841 4091 if [ ! -d "$DOMAIN_PATH" ]; then
93bce1b2 4092 _err "Domain is not valid:'$_d'"
a61fe418 4093 return 1
4094 fi
4c2a3841 4095
93bce1b2 4096 . "$DOMAIN_CONF"
4c2a3841 4097
93bce1b2 4098 _savedomainconf Le_DeployHook "$_hooks"
4c2a3841 4099
93bce1b2 4100 _deploy "$_d" "$_hooks"
a61fe418 4101}
4102
4c3b3608 4103installcert() {
85e1f4ea 4104 _main_domain="$1"
4105 if [ -z "$_main_domain" ]; then
5c539af7 4106 _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 4107 return 1
4108 fi
4109
85e1f4ea 4110 _real_cert="$2"
4111 _real_key="$3"
4112 _real_ca="$4"
4113 _reload_cmd="$5"
4114 _real_fullchain="$6"
43822d37 4115 _isEcc="$7"
4116
85e1f4ea 4117 _initpath "$_main_domain" "$_isEcc"
4c2a3841 4118 if [ ! -d "$DOMAIN_PATH" ]; then
85e1f4ea 4119 _err "Domain is not valid:'$_main_domain'"
43822d37 4120 return 1
4121 fi
4122
85e1f4ea 4123 _savedomainconf "Le_RealCertPath" "$_real_cert"
4124 _savedomainconf "Le_RealCACertPath" "$_real_ca"
4125 _savedomainconf "Le_RealKeyPath" "$_real_key"
4126 _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
4127 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
4128
044da37c 4129 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
43822d37 4130}
4c3b3608 4131
044da37c 4132#domain cert key ca fullchain reloadcmd backup-prefix
43822d37 4133_installcert() {
85e1f4ea 4134 _main_domain="$1"
4135 _real_cert="$2"
4136 _real_key="$3"
4137 _real_ca="$4"
044da37c 4138 _real_fullchain="$5"
4139 _reload_cmd="$6"
4140 _backup_prefix="$7"
4c3b3608 4141
85e1f4ea 4142 if [ "$_real_cert" = "$NO_VALUE" ]; then
4143 _real_cert=""
4d2f38b0 4144 fi
85e1f4ea 4145 if [ "$_real_key" = "$NO_VALUE" ]; then
4146 _real_key=""
4d2f38b0 4147 fi
85e1f4ea 4148 if [ "$_real_ca" = "$NO_VALUE" ]; then
4149 _real_ca=""
4d2f38b0 4150 fi
85e1f4ea 4151 if [ "$_reload_cmd" = "$NO_VALUE" ]; then
4152 _reload_cmd=""
4d2f38b0 4153 fi
85e1f4ea 4154 if [ "$_real_fullchain" = "$NO_VALUE" ]; then
4155 _real_fullchain=""
4d2f38b0 4156 fi
4c2a3841 4157
044da37c 4158 _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
4159 mkdir -p "$_backup_path"
4160
85e1f4ea 4161 if [ "$_real_cert" ]; then
4162 _info "Installing cert to:$_real_cert"
4163 if [ -f "$_real_cert" ] && [ ! "$IS_RENEW" ]; then
044da37c 4164 cp "$_real_cert" "$_backup_path/cert.bak"
4c3b3608 4165 fi
85e1f4ea 4166 cat "$CERT_PATH" >"$_real_cert"
4c3b3608 4167 fi
4c2a3841 4168
85e1f4ea 4169 if [ "$_real_ca" ]; then
4170 _info "Installing CA to:$_real_ca"
4171 if [ "$_real_ca" = "$_real_cert" ]; then
4172 echo "" >>"$_real_ca"
4173 cat "$CA_CERT_PATH" >>"$_real_ca"
4c3b3608 4174 else
85e1f4ea 4175 if [ -f "$_real_ca" ] && [ ! "$IS_RENEW" ]; then
044da37c 4176 cp "$_real_ca" "$_backup_path/ca.bak"
78552b18 4177 fi
85e1f4ea 4178 cat "$CA_CERT_PATH" >"$_real_ca"
4c3b3608 4179 fi
4180 fi
4181
85e1f4ea 4182 if [ "$_real_key" ]; then
4183 _info "Installing key to:$_real_key"
4184 if [ -f "$_real_key" ] && [ ! "$IS_RENEW" ]; then
044da37c 4185 cp "$_real_key" "$_backup_path/key.bak"
4c3b3608 4186 fi
85e1f4ea 4187 cat "$CERT_KEY_PATH" >"$_real_key"
4c3b3608 4188 fi
4c2a3841 4189
85e1f4ea 4190 if [ "$_real_fullchain" ]; then
4191 _info "Installing full chain to:$_real_fullchain"
4192 if [ -f "$_real_fullchain" ] && [ ! "$IS_RENEW" ]; then
044da37c 4193 cp "$_real_fullchain" "$_backup_path/fullchain.bak"
a63b05a9 4194 fi
85e1f4ea 4195 cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain"
4c2a3841 4196 fi
4c3b3608 4197
85e1f4ea 4198 if [ "$_reload_cmd" ]; then
4199 _info "Run reload cmd: $_reload_cmd"
25555b8c 4200 if (
839bf0e2 4201 export CERT_PATH
4202 export CERT_KEY_PATH
4203 export CA_CERT_PATH
4204 export CERT_FULLCHAIN_PATH
58d4c74b 4205 export Le_Domain
85e1f4ea 4206 cd "$DOMAIN_PATH" && eval "$_reload_cmd"
839bf0e2 4207 ); then
43822d37 4208 _info "$(__green "Reload success")"
4d2f38b0 4209 else
4210 _err "Reload error for :$Le_Domain"
4211 fi
4212 fi
4213
4c3b3608 4214}
4215
27dbe77f 4216#confighome
4c3b3608 4217installcronjob() {
27dbe77f 4218 _c_home="$1"
4c3b3608 4219 _initpath
4c2a3841 4220 if ! _exists "crontab"; then
77546ea5 4221 _err "crontab doesn't exist, so, we can not install cron jobs."
4222 _err "All your certs will not be renewed automatically."
a7b7355d 4223 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
77546ea5 4224 return 1
4225 fi
4226
4c3b3608 4227 _info "Installing cron job"
4c2a3841 4228 if ! crontab -l | grep "$PROJECT_ENTRY --cron"; then
4229 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
a7b7355d 4230 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
4c3b3608 4231 else
a7b7355d 4232 _err "Can not install cronjob, $PROJECT_ENTRY not found."
4c3b3608 4233 return 1
4234 fi
27dbe77f 4235
4236 if [ "$_c_home" ]; then
80941f84 4237 _c_entry="--config-home \"$_c_home\" "
27dbe77f 4238 fi
32b3717c 4239 _t=$(_time)
4240 random_minute=$(_math $_t % 60)
e2c939fb 4241 if _exists uname && uname -a | grep SunOS >/dev/null; then
4c2a3841 4242 crontab -l | {
4243 cat
0533bde9 4244 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
4c2a3841 4245 } | crontab --
22ea4004 4246 else
4c2a3841 4247 crontab -l | {
4248 cat
0533bde9 4249 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
4c2a3841 4250 } | crontab -
22ea4004 4251 fi
4c3b3608 4252 fi
4c2a3841 4253 if [ "$?" != "0" ]; then
4c3b3608 4254 _err "Install cron job failed. You need to manually renew your certs."
4255 _err "Or you can add cronjob by yourself:"
a7b7355d 4256 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4c3b3608 4257 return 1
4258 fi
4259}
4260
4261uninstallcronjob() {
4c2a3841 4262 if ! _exists "crontab"; then
37db5b81 4263 return
4264 fi
4c3b3608 4265 _info "Removing cron job"
a7b7355d 4266 cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")"
4c2a3841 4267 if [ "$cr" ]; then
4268 if _exists uname && uname -a | grep solaris >/dev/null; then
22ea4004 4269 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab --
4270 else
4271 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -
4272 fi
a7b7355d 4273 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4c3b3608 4274 _info LE_WORKING_DIR "$LE_WORKING_DIR"
27dbe77f 4275 if _contains "$cr" "--config-home"; then
f5b546b3 4276 LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
4277 _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
27dbe77f 4278 fi
4c2a3841 4279 fi
4c3b3608 4280 _initpath
a7b7355d 4281
4c3b3608 4282}
4283
6cb415f5 4284revoke() {
4285 Le_Domain="$1"
4c2a3841 4286 if [ -z "$Le_Domain" ]; then
78f0201d 4287 _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com [--ecc]"
6cb415f5 4288 return 1
4289 fi
4c2a3841 4290
43822d37 4291 _isEcc="$2"
4292
c4a375b3 4293 _initpath "$Le_Domain" "$_isEcc"
4c2a3841 4294 if [ ! -f "$DOMAIN_CONF" ]; then
6cb415f5 4295 _err "$Le_Domain is not a issued domain, skip."
4c2a3841 4296 return 1
6cb415f5 4297 fi
4c2a3841 4298
4299 if [ ! -f "$CERT_PATH" ]; then
6cb415f5 4300 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
4301 return 1
4302 fi
6cb415f5 4303
11927a76 4304 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
4c2a3841 4305
4306 if [ -z "$cert" ]; then
6cb415f5 4307 _err "Cert for $Le_Domain is empty found, skip."
4308 return 1
4309 fi
4c2a3841 4310
6cb415f5 4311 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
4312 uri="$API/acme/revoke-cert"
4313
4c2a3841 4314 if [ -f "$CERT_KEY_PATH" ]; then
1befee5a 4315 _info "Try domain key first."
c4a375b3 4316 if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
4c2a3841 4317 if [ -z "$response" ]; then
1befee5a 4318 _info "Revoke success."
c4a375b3 4319 rm -f "$CERT_PATH"
1befee5a 4320 return 0
4c2a3841 4321 else
1befee5a 4322 _err "Revoke error by domain key."
4323 _err "$response"
4324 fi
6cb415f5 4325 fi
4c2a3841 4326 else
1befee5a 4327 _info "Domain key file doesn't exists."
6cb415f5 4328 fi
6cb415f5 4329
1befee5a 4330 _info "Try account key."
6cb415f5 4331
c4a375b3 4332 if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
4c2a3841 4333 if [ -z "$response" ]; then
6cb415f5 4334 _info "Revoke success."
c4a375b3 4335 rm -f "$CERT_PATH"
6cb415f5 4336 return 0
4c2a3841 4337 else
6cb415f5 4338 _err "Revoke error."
c9c31c04 4339 _debug "$response"
6cb415f5 4340 fi
4341 fi
4342 return 1
4343}
4c3b3608 4344
78f0201d 4345#domain ecc
4346remove() {
4347 Le_Domain="$1"
4348 if [ -z "$Le_Domain" ]; then
4349 _usage "Usage: $PROJECT_ENTRY --remove -d domain.com [--ecc]"
4350 return 1
4351 fi
4352
4353 _isEcc="$2"
4354
4355 _initpath "$Le_Domain" "$_isEcc"
4356 _removed_conf="$DOMAIN_CONF.removed"
4357 if [ ! -f "$DOMAIN_CONF" ]; then
4358 if [ -f "$_removed_conf" ]; then
4359 _err "$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH"
4360 else
4361 _err "$Le_Domain is not a issued domain, skip."
4362 fi
4363 return 1
4364 fi
4365
4366 if mv "$DOMAIN_CONF" "$_removed_conf"; then
68aea3af 4367 _info "$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)"
78f0201d 4368 _info "You can remove them by yourself."
4369 return 0
4370 else
4371 _err "Remove $Le_Domain failed."
4372 return 1
4373 fi
4374}
4375
0c00e870 4376#domain vtype
4377_deactivate() {
4378 _d_domain="$1"
4379 _d_type="$2"
4380 _initpath
4c2a3841 4381
0c00e870 4382 _d_i=0
4383 _d_max_retry=9
4c2a3841 4384 while [ "$_d_i" -lt "$_d_max_retry" ]; do
0407c4e0 4385 _info "Deactivate: $_d_domain"
0c00e870 4386 _d_i="$(_math $_d_i + 1)"
4c2a3841 4387
4388 if ! __get_domain_new_authz "$_d_domain"; then
f940b2a5 4389 _err "Can not get domain new authz token."
0c00e870 4390 return 1
4391 fi
4c2a3841 4392
c2c8f320 4393 authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
3f4513b3 4394 _debug "authzUri" "$authzUri"
0c00e870 4395
4c2a3841 4396 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
0c00e870 4397 _err "new-authz error: $response"
4398 return 1
4399 fi
4c2a3841 4400
947d14ff 4401 entry="$(printf "%s\n" "$response" | _egrep_o '{"type":"[^"]*","status":"valid","uri"[^}]*')"
0c00e870 4402 _debug entry "$entry"
4c2a3841 4403
4404 if [ -z "$entry" ]; then
fb2029e7 4405 _info "No more valid entry found."
0c00e870 4406 break
4407 fi
4c2a3841 4408
0c00e870 4409 _vtype="$(printf "%s\n" "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
c4a375b3 4410 _debug _vtype "$_vtype"
0c00e870 4411 _info "Found $_vtype"
4412
4c2a3841 4413 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
c4a375b3 4414 _debug uri "$uri"
4c2a3841 4415
4416 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
0c00e870 4417 _info "Skip $_vtype"
4418 continue
4419 fi
4c2a3841 4420
0c00e870 4421 _info "Deactivate: $_vtype"
4c2a3841 4422
4423 if ! _send_signed_request "$authzUri" "{\"resource\": \"authz\", \"status\":\"deactivated\"}"; then
0c00e870 4424 _err "Can not deactivate $_vtype."
4425 return 1
4426 fi
4c2a3841 4427
fb2029e7 4428 _info "Deactivate: $_vtype success."
4c2a3841 4429
0c00e870 4430 done
4431 _debug "$_d_i"
4c2a3841 4432 if [ "$_d_i" -lt "$_d_max_retry" ]; then
0c00e870 4433 _info "Deactivated success!"
4434 else
4435 _err "Deactivate failed."
4436 fi
4437
4438}
4439
4440deactivate() {
3f4513b3 4441 _d_domain_list="$1"
0c00e870 4442 _d_type="$2"
4443 _initpath
3f4513b3 4444 _debug _d_domain_list "$_d_domain_list"
4c2a3841 4445 if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
3f4513b3 4446 _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
0c00e870 4447 return 1
4448 fi
4c2a3841 4449 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
4450 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
3f4513b3 4451 continue
4452 fi
c4a375b3 4453 if ! _deactivate "$_d_dm" "$_d_type"; then
86c017ec 4454 return 1
4455 fi
3f4513b3 4456 done
0c00e870 4457}
4458
4c3b3608 4459# Detect profile file if not specified as environment variable
4460_detect_profile() {
4c2a3841 4461 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
4c3b3608 4462 echo "$PROFILE"
4463 return
4464 fi
4465
4c3b3608 4466 DETECTED_PROFILE=''
4c3b3608 4467 SHELLTYPE="$(basename "/$SHELL")"
4468
4c2a3841 4469 if [ "$SHELLTYPE" = "bash" ]; then
4470 if [ -f "$HOME/.bashrc" ]; then
4c3b3608 4471 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 4472 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 4473 DETECTED_PROFILE="$HOME/.bash_profile"
4474 fi
4c2a3841 4475 elif [ "$SHELLTYPE" = "zsh" ]; then
4c3b3608 4476 DETECTED_PROFILE="$HOME/.zshrc"
4477 fi
4478
4c2a3841 4479 if [ -z "$DETECTED_PROFILE" ]; then
4480 if [ -f "$HOME/.profile" ]; then
4c3b3608 4481 DETECTED_PROFILE="$HOME/.profile"
4c2a3841 4482 elif [ -f "$HOME/.bashrc" ]; then
4c3b3608 4483 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 4484 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 4485 DETECTED_PROFILE="$HOME/.bash_profile"
4c2a3841 4486 elif [ -f "$HOME/.zshrc" ]; then
4c3b3608 4487 DETECTED_PROFILE="$HOME/.zshrc"
4488 fi
4489 fi
4490
4c2a3841 4491 if [ ! -z "$DETECTED_PROFILE" ]; then
4c3b3608 4492 echo "$DETECTED_PROFILE"
4493 fi
4494}
4495
4496_initconf() {
4497 _initpath
4c2a3841 4498 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
0ca5b799 4499 echo "
d404e92d 4500
d0871bda 4501#LOG_FILE=\"$DEFAULT_LOG_FILE\"
6b500036 4502#LOG_LEVEL=1
5ea6e9c9 4503
251d1c5c 4504#AUTO_UPGRADE=\"1\"
89002ed2 4505
569d6c55 4506#NO_TIMESTAMP=1
5b771039 4507
d5ec5f80 4508 " >"$ACCOUNT_CONF_PATH"
4c3b3608 4509 fi
4510}
4511
c8e9a31e 4512# nocron
c60883ef 4513_precheck() {
c8e9a31e 4514 _nocron="$1"
4c2a3841 4515
4516 if ! _exists "curl" && ! _exists "wget"; then
c60883ef 4517 _err "Please install curl or wget first, we need to access http resources."
4c3b3608 4518 return 1
4519 fi
4c2a3841 4520
4521 if [ -z "$_nocron" ]; then
4522 if ! _exists "crontab"; then
c8e9a31e 4523 _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
4524 _err "We need to set cron job to renew the certs automatically."
4525 _err "Otherwise, your certs will not be able to be renewed automatically."
4c2a3841 4526 if [ -z "$FORCE" ]; then
c8e9a31e 4527 _err "Please add '--force' and try install again to go without crontab."
4528 _err "./$PROJECT_ENTRY --install --force"
4529 return 1
4530 fi
77546ea5 4531 fi
4c3b3608 4532 fi
4c2a3841 4533
d8ba26e6 4534 if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then
851fedf7 4535 _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN"
c60883ef 4536 _err "We need openssl to generate keys."
4c3b3608 4537 return 1
4538 fi
4c2a3841 4539
4540 if ! _exists "nc"; then
c60883ef 4541 _err "It is recommended to install nc first, try to install 'nc' or 'netcat'."
4542 _err "We use nc for standalone server if you use standalone mode."
4543 _err "If you don't use standalone mode, just ignore this warning."
4544 fi
4c2a3841 4545
c60883ef 4546 return 0
4547}
4548
0a7c9364 4549_setShebang() {
4550 _file="$1"
4551 _shebang="$2"
4c2a3841 4552 if [ -z "$_shebang" ]; then
43822d37 4553 _usage "Usage: file shebang"
0a7c9364 4554 return 1
4555 fi
4556 cp "$_file" "$_file.tmp"
4c2a3841 4557 echo "$_shebang" >"$_file"
4558 sed -n 2,99999p "$_file.tmp" >>"$_file"
4559 rm -f "$_file.tmp"
0a7c9364 4560}
4561
27dbe77f 4562#confighome
94dc5f33 4563_installalias() {
27dbe77f 4564 _c_home="$1"
94dc5f33 4565 _initpath
4566
4567 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4c2a3841 4568 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
44edb2bd 4569 echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
4570 echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
4571 echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
94dc5f33 4572 fi
4573
27dbe77f 4574 if [ "$_c_home" ]; then
be83a6a3 4575 _c_entry=" --config-home '$_c_home'"
27dbe77f 4576 fi
4577
1786a5e5 4578 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
f5b546b3 4579 if [ "$_c_home" ]; then
4580 _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
4581 fi
be83a6a3 4582 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
94dc5f33 4583
4584 _profile="$(_detect_profile)"
4c2a3841 4585 if [ "$_profile" ]; then
94dc5f33 4586 _debug "Found profile: $_profile"
aba5c634 4587 _info "Installing alias to '$_profile'"
94dc5f33 4588 _setopt "$_profile" ". \"$_envfile\""
4589 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
4590 else
4591 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
4592 fi
94dc5f33 4593
4594 #for csh
4595 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
94dc5f33 4596 _csh_profile="$HOME/.cshrc"
4c2a3841 4597 if [ -f "$_csh_profile" ]; then
aba5c634 4598 _info "Installing alias to '$_csh_profile'"
6626371d 4599 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
f5b546b3 4600 if [ "$_c_home" ]; then
4601 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
4602 fi
be83a6a3 4603 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4c2a3841 4604 _setopt "$_csh_profile" "source \"$_cshfile\""
94dc5f33 4605 fi
4c2a3841 4606
acafa585 4607 #for tcsh
4608 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 4609 if [ -f "$_tcsh_profile" ]; then
aba5c634 4610 _info "Installing alias to '$_tcsh_profile'"
acafa585 4611 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
f5b546b3 4612 if [ "$_c_home" ]; then
4613 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
4614 fi
be83a6a3 4615 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4c2a3841 4616 _setopt "$_tcsh_profile" "source \"$_cshfile\""
acafa585 4617 fi
94dc5f33 4618
4619}
4620
27dbe77f 4621# nocron confighome
c60883ef 4622install() {
f3e4cea3 4623
4c2a3841 4624 if [ -z "$LE_WORKING_DIR" ]; then
f3e4cea3 4625 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
4626 fi
4c2a3841 4627
c8e9a31e 4628 _nocron="$1"
27dbe77f 4629 _c_home="$2"
4c2a3841 4630 if ! _initpath; then
c60883ef 4631 _err "Install failed."
4c3b3608 4632 return 1
4633 fi
4c2a3841 4634 if [ "$_nocron" ]; then
52677b0a 4635 _debug "Skip install cron job"
4636 fi
4c2a3841 4637
4638 if ! _precheck "$_nocron"; then
c60883ef 4639 _err "Pre-check failed, can not install."
4c3b3608 4640 return 1
4641 fi
4c2a3841 4642
6cc11ffb 4643 #convert from le
4c2a3841 4644 if [ -d "$HOME/.le" ]; then
4645 for envfile in "le.env" "le.sh.env"; do
4646 if [ -f "$HOME/.le/$envfile" ]; then
4647 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
4648 _upgrading="1"
4649 _info "You are upgrading from le.sh"
4650 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
4651 mv "$HOME/.le" "$LE_WORKING_DIR"
4652 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4653 break
6cc11ffb 4654 fi
4655 fi
4656 done
4657 fi
4658
4c3b3608 4659 _info "Installing to $LE_WORKING_DIR"
635695ec 4660
4c2a3841 4661 if ! mkdir -p "$LE_WORKING_DIR"; then
90035252 4662 _err "Can not create working dir: $LE_WORKING_DIR"
4a0f23e2 4663 return 1
4664 fi
4c2a3841 4665
762978f8 4666 chmod 700 "$LE_WORKING_DIR"
4667
f5b546b3 4668 if ! mkdir -p "$LE_CONFIG_HOME"; then
4669 _err "Can not create config dir: $LE_CONFIG_HOME"
27dbe77f 4670 return 1
4671 fi
4672
f5b546b3 4673 chmod 700 "$LE_CONFIG_HOME"
27dbe77f 4674
d5ec5f80 4675 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 4676
4c2a3841 4677 if [ "$?" != "0" ]; then
a7b7355d 4678 _err "Install failed, can not copy $PROJECT_ENTRY"
4c3b3608 4679 return 1
4680 fi
4681
a7b7355d 4682 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 4683
27dbe77f 4684 _installalias "$_c_home"
4c3b3608 4685
4c2a3841 4686 for subf in $_SUB_FOLDERS; do
4687 if [ -d "$subf" ]; then
d5ec5f80 4688 mkdir -p "$LE_WORKING_DIR/$subf"
4689 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
a61fe418 4690 fi
4691 done
4692
4c2a3841 4693 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4c3b3608 4694 _initconf
4695 fi
6cc11ffb 4696
4c2a3841 4697 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
635695ec 4698 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
6cc11ffb 4699 fi
4700
4c2a3841 4701 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
b2817897 4702 _saveaccountconf "CERT_HOME" "$CERT_HOME"
4703 fi
4704
4c2a3841 4705 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
b2817897 4706 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
4707 fi
4c2a3841 4708
4709 if [ -z "$_nocron" ]; then
27dbe77f 4710 installcronjob "$_c_home"
c8e9a31e 4711 fi
0a7c9364 4712
4c2a3841 4713 if [ -z "$NO_DETECT_SH" ]; then
641989fd 4714 #Modify shebang
4c2a3841 4715 if _exists bash; then
329174b6 4716 _info "Good, bash is found, so change the shebang to use bash as preferred."
4dd646a4 4717 _shebang='#!'"$(env bash -c "command -v bash")"
641989fd 4718 _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
4c2a3841 4719 for subf in $_SUB_FOLDERS; do
4720 if [ -d "$LE_WORKING_DIR/$subf" ]; then
4721 for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
a61fe418 4722 _setShebang "$_apifile" "$_shebang"
4723 done
4724 fi
4725 done
0a7c9364 4726 fi
4727 fi
4728
4c3b3608 4729 _info OK
4730}
4731
52677b0a 4732# nocron
4c3b3608 4733uninstall() {
52677b0a 4734 _nocron="$1"
4c2a3841 4735 if [ -z "$_nocron" ]; then
52677b0a 4736 uninstallcronjob
4737 fi
4c3b3608 4738 _initpath
4739
9aa3be7f 4740 _uninstallalias
4c2a3841 4741
d5ec5f80 4742 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
f5b546b3 4743 _info "The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself."
9aa3be7f 4744
4745}
4746
4747_uninstallalias() {
4748 _initpath
4749
4c3b3608 4750 _profile="$(_detect_profile)"
4c2a3841 4751 if [ "$_profile" ]; then
9aa3be7f 4752 _info "Uninstalling alias from: '$_profile'"
d5ec5f80 4753 text="$(cat "$_profile")"
4c2a3841 4754 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
4c3b3608 4755 fi
4756
94dc5f33 4757 _csh_profile="$HOME/.cshrc"
4c2a3841 4758 if [ -f "$_csh_profile" ]; then
9aa3be7f 4759 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 4760 text="$(cat "$_csh_profile")"
4c2a3841 4761 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
94dc5f33 4762 fi
4c2a3841 4763
acafa585 4764 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 4765 if [ -f "$_tcsh_profile" ]; then
9aa3be7f 4766 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 4767 text="$(cat "$_tcsh_profile")"
4c2a3841 4768 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
acafa585 4769 fi
4c3b3608 4770
4771}
4772
4773cron() {
281aa349 4774 IN_CRON=1
89002ed2 4775 _initpath
d8ba26e6 4776 _info "$(__green "===Starting cron===")"
4c2a3841 4777 if [ "$AUTO_UPGRADE" = "1" ]; then
89002ed2 4778 export LE_WORKING_DIR
4779 (
4c2a3841 4780 if ! upgrade; then
4781 _err "Cron:Upgrade failed!"
4782 return 1
4783 fi
89002ed2 4784 )
d5ec5f80 4785 . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
1ab63043 4786
4c2a3841 4787 if [ -t 1 ]; then
1ab63043 4788 __INTERACTIVE="1"
4789 fi
4c2a3841 4790
89002ed2 4791 _info "Auto upgraded to: $VER"
4792 fi
4c3b3608 4793 renewAll
cc179731 4794 _ret="$?"
281aa349 4795 IN_CRON=""
d8ba26e6 4796 _info "$(__green "===End cron===")"
0ba95a3d 4797 exit $_ret
4c3b3608 4798}
4799
4800version() {
a63b05a9 4801 echo "$PROJECT"
4802 echo "v$VER"
4c3b3608 4803}
4804
4805showhelp() {
d0871bda 4806 _initpath
4c3b3608 4807 version
a7b7355d 4808 echo "Usage: $PROJECT_ENTRY command ...[parameters]....
a63b05a9 4809Commands:
4810 --help, -h Show this help message.
4811 --version, -v Show version info.
a7b7355d 4812 --install Install $PROJECT_NAME to your system.
4813 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
d8beaf72 4814 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
a63b05a9 4815 --issue Issue a cert.
10afcaca 4816 --signcsr Issue a cert from an existing csr.
a61fe418 4817 --deploy Deploy the cert to your server.
27dbe77f 4818 --install-cert Install the issued cert to apache/nginx or any other server.
a63b05a9 4819 --renew, -r Renew a cert.
27dbe77f 4820 --renew-all Renew all the certs.
a63b05a9 4821 --revoke Revoke a cert.
78f0201d 4822 --remove Remove the cert from $PROJECT
10afcaca 4823 --list List all the certs.
4824 --showcsr Show the content of a csr.
27dbe77f 4825 --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.
4826 --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
a63b05a9 4827 --cron Run cron job to renew all the certs.
4828 --toPkcs Export the certificate and key to a pfx file.
4410226d 4829 --toPkcs8 Convert to pkcs8 format.
27dbe77f 4830 --update-account Update account info.
4831 --register-account Register account key.
0984585d 4832 --create-account-key Create an account private key, professional use.
4833 --create-domain-key Create an domain private key, professional use.
a63b05a9 4834 --createCSR, -ccsr Create CSR , professional use.
0c00e870 4835 --deactivate Deactivate the domain authz, professional use.
3c07f57a 4836
a63b05a9 4837Parameters:
4838 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
4839 --force, -f Used to force to install or force to renew a cert immediately.
4840 --staging, --test Use staging server, just for test.
4841 --debug Output debug info.
e6e85b0c 4842 --output-insecure Output all the sensitive messages. By default all the credentials/sensitive messages are hidden from the output/debug/log for secure.
a63b05a9 4843 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
4844 --standalone Use standalone mode.
0e44f587 4845 --stateless Use stateless mode, see: $_STATELESS_WIKI
e22bcf7c 4846 --tls Use standalone tls mode.
a63b05a9 4847 --apache Use apache mode.
eccec5f6 4848 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
4a4dacb5 4849 --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 4850
a63b05a9 4851 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
4852 --accountkeylength, -ak [2048] Specifies the account key length.
d0871bda 4853 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
a73c5b33 4854 --log-level 1|2 Specifies the log level, default is 1.
52765466 4855 --syslog [0|3|6|7] Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
3c07f57a 4856
a63b05a9 4857 These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
3c07f57a 4858
13fe54c9 4859 --cert-file After issue/renew, the cert will be copied to this path.
4860 --key-file After issue/renew, the key will be copied to this path.
4861 --ca-file After issue/renew, the intermediate cert will be copied to this path.
4862 --fullchain-file After issue/renew, the fullchain cert will be copied to this path.
3c07f57a 4863
a63b05a9 4864 --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
4865
4866 --accountconf Specifies a customized account config file.
635695ec 4867 --home Specifies the home dir for $PROJECT_NAME .
27dbe77f 4868 --cert-home Specifies the home dir to save all the certs, only valid for '--install' command.
4869 --config-home Specifies the home dir to save all the configurations.
635695ec 4870 --useragent Specifies the user agent string. it will be saved for future use too.
b5eb4b90 4871 --accountemail Specifies the account email for registering, Only valid for the '--install' command.
06625071 4872 --accountkey Specifies the account key path, Only valid for the '--install' command.
523c7682 4873 --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
39c8f79f 4874 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
e22bcf7c 4875 --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
6ae0f7f5 4876 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
dcf4f8f6 4877 --listraw Only used for '--list' command, list the certs in raw format.
27dbe77f 4878 --stopRenewOnError, -se Only valid for '--renew-all' command. Stop if one cert has error in renewal.
13d7cae9 4879 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
8f73e241 4880 --ca-bundle Specifies the path to the CA certificate bundle to verify api server's certificate.
13fe54c9 4881 --ca-path Specifies directory containing CA certificates in PEM format, used by wget or curl.
bc96082f 4882 --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 4883 --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
10afcaca 4884 --csr Specifies the input csr.
b0070f03 4885 --pre-hook Command to be run before obtaining any certificates.
84a6730b 4886 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obtain/renew is success or failed.
b0070f03 4887 --renew-hook Command to be run once for each successfully renewed certificate.
a61fe418 4888 --deploy-hook The hook file to deploy cert
0c9546cc 4889 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
6bf281f9 4890 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
6ae0f7f5 4891 --listen-v4 Force standalone/tls server to listen at ipv4.
4892 --listen-v6 Force standalone/tls server to listen at ipv6.
a746139c 4893 --openssl-bin Specifies a custom openssl bin location.
9b124070 4894 --use-wget Force to use wget, if you have both curl and wget installed.
4c3b3608 4895 "
4896}
4897
52677b0a 4898# nocron
4a0f23e2 4899_installOnline() {
4900 _info "Installing from online archive."
52677b0a 4901 _nocron="$1"
4c2a3841 4902 if [ ! "$BRANCH" ]; then
4a0f23e2 4903 BRANCH="master"
4904 fi
a8df88ab 4905
4a0f23e2 4906 target="$PROJECT/archive/$BRANCH.tar.gz"
4907 _info "Downloading $target"
4908 localname="$BRANCH.tar.gz"
4c2a3841 4909 if ! _get "$target" >$localname; then
df9547ae 4910 _err "Download error."
4a0f23e2 4911 return 1
4912 fi
0bbe6eef 4913 (
4c2a3841 4914 _info "Extracting $localname"
3a3b0dd5 4915 if ! (tar xzf $localname || gtar xzf $localname); then
4916 _err "Extraction error."
4917 exit 1
4918 fi
4c2a3841 4919
4920 cd "$PROJECT_NAME-$BRANCH"
4921 chmod +x $PROJECT_ENTRY
4922 if ./$PROJECT_ENTRY install "$_nocron"; then
4923 _info "Install success!"
4924 fi
4925
4926 cd ..
4927
4928 rm -rf "$PROJECT_NAME-$BRANCH"
4929 rm -f "$localname"
0bbe6eef 4930 )
4a0f23e2 4931}
4932
52677b0a 4933upgrade() {
4934 if (
267f283a 4935 _initpath
4936 export LE_WORKING_DIR
d0b748a4 4937 cd "$LE_WORKING_DIR"
52677b0a 4938 _installOnline "nocron"
4c2a3841 4939 ); then
52677b0a 4940 _info "Upgrade success!"
096d8992 4941 exit 0
52677b0a 4942 else
4943 _err "Upgrade failed!"
096d8992 4944 exit 1
52677b0a 4945 fi
4946}
a63b05a9 4947
5ea6e9c9 4948_processAccountConf() {
4c2a3841 4949 if [ "$_useragent" ]; then
5ea6e9c9 4950 _saveaccountconf "USER_AGENT" "$_useragent"
4c2a3841 4951 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
d0871bda 4952 _saveaccountconf "USER_AGENT" "$USER_AGENT"
5ea6e9c9 4953 fi
4c2a3841 4954
4955 if [ "$_accountemail" ]; then
5ea6e9c9 4956 _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
4c2a3841 4957 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
d0871bda 4958 _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
5ea6e9c9 4959 fi
4c2a3841 4960
a746139c 4961 if [ "$_openssl_bin" ]; then
851fedf7 4962 _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin"
4963 elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
4964 _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
a746139c 4965 fi
4966
4c2a3841 4967 if [ "$_auto_upgrade" ]; then
6bf281f9 4968 _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
4c2a3841 4969 elif [ "$AUTO_UPGRADE" ]; then
6bf281f9 4970 _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
4971 fi
4c2a3841 4972
9b124070 4973 if [ "$_use_wget" ]; then
4974 _saveaccountconf "ACME_USE_WGET" "$_use_wget"
4975 elif [ "$ACME_USE_WGET" ]; then
4976 _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET"
4977 fi
4978
5ea6e9c9 4979}
4980
a63b05a9 4981_process() {
4982 _CMD=""
4983 _domain=""
3f4513b3 4984 _altdomains="$NO_VALUE"
a63b05a9 4985 _webroot=""
bdbf323f 4986 _keylength=""
4987 _accountkeylength=""
5c539af7 4988 _cert_file=""
4989 _key_file=""
4990 _ca_file=""
4991 _fullchain_file=""
4d2f38b0 4992 _reloadcmd=""
a63b05a9 4993 _password=""
635695ec 4994 _accountconf=""
4995 _useragent=""
b5eb4b90 4996 _accountemail=""
4997 _accountkey=""
b2817897 4998 _certhome=""
27dbe77f 4999 _confighome=""
39c8f79f 5000 _httpport=""
e22bcf7c 5001 _tlsport=""
0e38c60d 5002 _dnssleep=""
dcf4f8f6 5003 _listraw=""
cc179731 5004 _stopRenewOnError=""
e3698edd 5005 #_insecure=""
78009539 5006 _ca_bundle=""
2aa75f03 5007 _ca_path=""
c8e9a31e 5008 _nocron=""
43822d37 5009 _ecc=""
10afcaca 5010 _csr=""
b0070f03 5011 _pre_hook=""
5012 _post_hook=""
5013 _renew_hook=""
a61fe418 5014 _deploy_hook=""
5ea6e9c9 5015 _logfile=""
d0871bda 5016 _log=""
0463b5d6 5017 _local_address=""
a73c5b33 5018 _log_level=""
6bf281f9 5019 _auto_upgrade=""
6ae0f7f5 5020 _listen_v4=""
5021 _listen_v6=""
a746139c 5022 _openssl_bin=""
e2edf208 5023 _syslog=""
9b124070 5024 _use_wget=""
4c2a3841 5025 while [ ${#} -gt 0 ]; do
a63b05a9 5026 case "${1}" in
4c2a3841 5027
5028 --help | -h)
a63b05a9 5029 showhelp
5030 return
5031 ;;
4c2a3841 5032 --version | -v)
a63b05a9 5033 version
5034 return
5035 ;;
4c2a3841 5036 --install)
a63b05a9 5037 _CMD="install"
5038 ;;
4c2a3841 5039 --uninstall)
a63b05a9 5040 _CMD="uninstall"
5041 ;;
4c2a3841 5042 --upgrade)
52677b0a 5043 _CMD="upgrade"
5044 ;;
4c2a3841 5045 --issue)
a63b05a9 5046 _CMD="issue"
5047 ;;
4c2a3841 5048 --deploy)
a61fe418 5049 _CMD="deploy"
5050 ;;
4c2a3841 5051 --signcsr)
10afcaca 5052 _CMD="signcsr"
5053 ;;
4c2a3841 5054 --showcsr)
10afcaca 5055 _CMD="showcsr"
5056 ;;
db7e4bf9 5057 --installcert | -i | --install-cert)
a63b05a9 5058 _CMD="installcert"
5059 ;;
4c2a3841 5060 --renew | -r)
a63b05a9 5061 _CMD="renew"
5062 ;;
db7e4bf9 5063 --renewAll | --renewall | --renew-all)
a63b05a9 5064 _CMD="renewAll"
5065 ;;
4c2a3841 5066 --revoke)
a63b05a9 5067 _CMD="revoke"
5068 ;;
78f0201d 5069 --remove)
5070 _CMD="remove"
5071 ;;
4c2a3841 5072 --list)
6d7eda3e 5073 _CMD="list"
5074 ;;
ee20015d 5075 --installcronjob | --install-cronjob)
a63b05a9 5076 _CMD="installcronjob"
5077 ;;
db7e4bf9 5078 --uninstallcronjob | --uninstall-cronjob)
a63b05a9 5079 _CMD="uninstallcronjob"
5080 ;;
4c2a3841 5081 --cron)
a63b05a9 5082 _CMD="cron"
5083 ;;
4c2a3841 5084 --toPkcs)
a63b05a9 5085 _CMD="toPkcs"
4c2a3841 5086 ;;
4410226d 5087 --toPkcs8)
5088 _CMD="toPkcs8"
342128a4 5089 ;;
0984585d 5090 --createAccountKey | --createaccountkey | -cak | --create-account-key)
a63b05a9 5091 _CMD="createAccountKey"
5092 ;;
0984585d 5093 --createDomainKey | --createdomainkey | -cdk | --create-domain-key)
a63b05a9 5094 _CMD="createDomainKey"
5095 ;;
4c2a3841 5096 --createCSR | --createcsr | -ccr)
a63b05a9 5097 _CMD="createCSR"
5098 ;;
4c2a3841 5099 --deactivate)
0c00e870 5100 _CMD="deactivate"
5101 ;;
ee20015d 5102 --updateaccount | --update-account)
eb59817e 5103 _CMD="updateaccount"
5104 ;;
ee20015d 5105 --registeraccount | --register-account)
eb59817e 5106 _CMD="registeraccount"
5107 ;;
4c2a3841 5108 --domain | -d)
a63b05a9 5109 _dvalue="$2"
4c2a3841 5110
5111 if [ "$_dvalue" ]; then
5112 if _startswith "$_dvalue" "-"; then
ee1737a5 5113 _err "'$_dvalue' is not a valid domain for parameter '$1'"
5114 return 1
5115 fi
4c2a3841 5116 if _is_idn "$_dvalue" && ! _exists idn; then
9774b01b 5117 _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
5118 return 1
5119 fi
4c2a3841 5120
5121 if [ -z "$_domain" ]; then
ee1737a5 5122 _domain="$_dvalue"
a63b05a9 5123 else
4c2a3841 5124 if [ "$_altdomains" = "$NO_VALUE" ]; then
ee1737a5 5125 _altdomains="$_dvalue"
5126 else
5127 _altdomains="$_altdomains,$_dvalue"
5128 fi
a63b05a9 5129 fi
5130 fi
4c2a3841 5131
a63b05a9 5132 shift
5133 ;;
5134
4c2a3841 5135 --force | -f)
a63b05a9 5136 FORCE="1"
5137 ;;
4c2a3841 5138 --staging | --test)
a63b05a9 5139 STAGE="1"
5140 ;;
4c2a3841 5141 --debug)
5142 if [ -z "$2" ] || _startswith "$2" "-"; then
fc6cf4d9 5143 DEBUG="$DEBUG_LEVEL_DEFAULT"
a63b05a9 5144 else
5145 DEBUG="$2"
5146 shift
4c2a3841 5147 fi
a63b05a9 5148 ;;
e6e85b0c 5149 --output-insecure)
5150 export OUTPUT_INSECURE=1
5151 ;;
4c2a3841 5152 --webroot | -w)
a63b05a9 5153 wvalue="$2"
4c2a3841 5154 if [ -z "$_webroot" ]; then
a63b05a9 5155 _webroot="$wvalue"
5156 else
5157 _webroot="$_webroot,$wvalue"
5158 fi
5159 shift
4c2a3841 5160 ;;
5161 --standalone)
3f4513b3 5162 wvalue="$NO_VALUE"
4c2a3841 5163 if [ -z "$_webroot" ]; then
a63b05a9 5164 _webroot="$wvalue"
5165 else
5166 _webroot="$_webroot,$wvalue"
5167 fi
5168 ;;
0e44f587 5169 --stateless)
5170 wvalue="$MODE_STATELESS"
5171 if [ -z "$_webroot" ]; then
5172 _webroot="$wvalue"
5173 else
5174 _webroot="$_webroot,$wvalue"
5175 fi
5176 ;;
4c2a3841 5177 --local-address)
0463b5d6 5178 lvalue="$2"
5179 _local_address="$_local_address$lvalue,"
5180 shift
5181 ;;
4c2a3841 5182 --apache)
a63b05a9 5183 wvalue="apache"
4c2a3841 5184 if [ -z "$_webroot" ]; then
a63b05a9 5185 _webroot="$wvalue"
5186 else
5187 _webroot="$_webroot,$wvalue"
5188 fi
5189 ;;
9d725af6 5190 --nginx)
5191 wvalue="$NGINX"
5192 if [ -z "$_webroot" ]; then
5193 _webroot="$wvalue"
5194 else
5195 _webroot="$_webroot,$wvalue"
5196 fi
5197 ;;
4c2a3841 5198 --tls)
e22bcf7c 5199 wvalue="$W_TLS"
4c2a3841 5200 if [ -z "$_webroot" ]; then
e22bcf7c 5201 _webroot="$wvalue"
5202 else
5203 _webroot="$_webroot,$wvalue"
5204 fi
5205 ;;
4c2a3841 5206 --dns)
a63b05a9 5207 wvalue="dns"
4c2a3841 5208 if ! _startswith "$2" "-"; then
a63b05a9 5209 wvalue="$2"
5210 shift
5211 fi
4c2a3841 5212 if [ -z "$_webroot" ]; then
a63b05a9 5213 _webroot="$wvalue"
5214 else
5215 _webroot="$_webroot,$wvalue"
5216 fi
5217 ;;
4c2a3841 5218 --dnssleep)
0e38c60d 5219 _dnssleep="$2"
5220 Le_DNSSleep="$_dnssleep"
5221 shift
5222 ;;
4c2a3841 5223
5224 --keylength | -k)
a63b05a9 5225 _keylength="$2"
a63b05a9 5226 shift
5227 ;;
4c2a3841 5228 --accountkeylength | -ak)
2ce87fe2 5229 _accountkeylength="$2"
a63b05a9 5230 shift
5231 ;;
5232
5c539af7 5233 --cert-file | --certpath)
5234 _cert_file="$2"
a63b05a9 5235 shift
5236 ;;
5c539af7 5237 --key-file | --keypath)
5238 _key_file="$2"
a63b05a9 5239 shift
5240 ;;
5c539af7 5241 --ca-file | --capath)
5242 _ca_file="$2"
a63b05a9 5243 shift
5244 ;;
5c539af7 5245 --fullchain-file | --fullchainpath)
5246 _fullchain_file="$2"
a63b05a9 5247 shift
5248 ;;
4c2a3841 5249 --reloadcmd | --reloadCmd)
a63b05a9 5250 _reloadcmd="$2"
5251 shift
5252 ;;
4c2a3841 5253 --password)
a63b05a9 5254 _password="$2"
5255 shift
5256 ;;
4c2a3841 5257 --accountconf)
635695ec 5258 _accountconf="$2"
5259 ACCOUNT_CONF_PATH="$_accountconf"
a7b7355d 5260 shift
a63b05a9 5261 ;;
4c2a3841 5262 --home)
a63b05a9 5263 LE_WORKING_DIR="$2"
a7b7355d 5264 shift
a63b05a9 5265 ;;
ee20015d 5266 --certhome | --cert-home)
b2817897 5267 _certhome="$2"
5268 CERT_HOME="$_certhome"
5269 shift
4c2a3841 5270 ;;
27dbe77f 5271 --config-home)
5272 _confighome="$2"
f5b546b3 5273 LE_CONFIG_HOME="$_confighome"
27dbe77f 5274 shift
5275 ;;
4c2a3841 5276 --useragent)
635695ec 5277 _useragent="$2"
5278 USER_AGENT="$_useragent"
5279 shift
5280 ;;
4c2a3841 5281 --accountemail)
b5eb4b90 5282 _accountemail="$2"
5283 ACCOUNT_EMAIL="$_accountemail"
5284 shift
5285 ;;
4c2a3841 5286 --accountkey)
b5eb4b90 5287 _accountkey="$2"
5288 ACCOUNT_KEY_PATH="$_accountkey"
5289 shift
5290 ;;
4c2a3841 5291 --days)
06625071 5292 _days="$2"
5293 Le_RenewalDays="$_days"
5294 shift
5295 ;;
4c2a3841 5296 --httpport)
39c8f79f 5297 _httpport="$2"
5298 Le_HTTPPort="$_httpport"
5299 shift
5300 ;;
4c2a3841 5301 --tlsport)
e22bcf7c 5302 _tlsport="$2"
5303 Le_TLSPort="$_tlsport"
5304 shift
5305 ;;
4c2a3841 5306
5307 --listraw)
dcf4f8f6 5308 _listraw="raw"
4c2a3841 5309 ;;
5310 --stopRenewOnError | --stoprenewonerror | -se)
cc179731 5311 _stopRenewOnError="1"
5312 ;;
4c2a3841 5313 --insecure)
e3698edd 5314 #_insecure="1"
fac1e367 5315 HTTPS_INSECURE="1"
13d7cae9 5316 ;;
4c2a3841 5317 --ca-bundle)
6c4cc357 5318 _ca_bundle="$(_readlink -f "$2")"
78009539
PS
5319 CA_BUNDLE="$_ca_bundle"
5320 shift
5321 ;;
2aa75f03 5322 --ca-path)
5323 _ca_path="$2"
5324 CA_PATH="$_ca_path"
5325 shift
5326 ;;
4c2a3841 5327 --nocron)
c8e9a31e 5328 _nocron="1"
5329 ;;
4c2a3841 5330 --ecc)
43822d37 5331 _ecc="isEcc"
5332 ;;
4c2a3841 5333 --csr)
10afcaca 5334 _csr="$2"
5335 shift
5336 ;;
4c2a3841 5337 --pre-hook)
b0070f03 5338 _pre_hook="$2"
5339 shift
5340 ;;
4c2a3841 5341 --post-hook)
b0070f03 5342 _post_hook="$2"
5343 shift
5344 ;;
4c2a3841 5345 --renew-hook)
b0070f03 5346 _renew_hook="$2"
5347 shift
5348 ;;
4c2a3841 5349 --deploy-hook)
93bce1b2 5350 if [ -z "$2" ] || _startswith "$2" "-"; then
5351 _usage "Please specify a value for '--deploy-hook'"
5352 return 1
5353 fi
5354 _deploy_hook="$_deploy_hook$2,"
a61fe418 5355 shift
5356 ;;
4c2a3841 5357 --ocsp-must-staple | --ocsp)
96db9362 5358 Le_OCSP_Staple="1"
0c9546cc 5359 ;;
4c2a3841 5360 --log | --logfile)
d0871bda 5361 _log="1"
5ea6e9c9 5362 _logfile="$2"
4c2a3841 5363 if _startswith "$_logfile" '-'; then
d0871bda 5364 _logfile=""
5365 else
5366 shift
5367 fi
5ea6e9c9 5368 LOG_FILE="$_logfile"
4c2a3841 5369 if [ -z "$LOG_LEVEL" ]; then
a73c5b33 5370 LOG_LEVEL="$DEFAULT_LOG_LEVEL"
5371 fi
5372 ;;
4c2a3841 5373 --log-level)
30bfc2ce 5374 _log_level="$2"
a73c5b33 5375 LOG_LEVEL="$_log_level"
5376 shift
5ea6e9c9 5377 ;;
e2edf208 5378 --syslog)
5379 if ! _startswith "$2" '-'; then
5380 _syslog="$2"
5381 shift
5382 fi
5383 if [ -z "$_syslog" ]; then
fc6cf4d9 5384 _syslog="$SYSLOG_LEVEL_DEFAULT"
e2edf208 5385 fi
5386 ;;
4c2a3841 5387 --auto-upgrade)
6bf281f9 5388 _auto_upgrade="$2"
4c2a3841 5389 if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
6bf281f9 5390 _auto_upgrade="1"
5391 else
5392 shift
5393 fi
5394 AUTO_UPGRADE="$_auto_upgrade"
5395 ;;
4c2a3841 5396 --listen-v4)
6ae0f7f5 5397 _listen_v4="1"
5398 Le_Listen_V4="$_listen_v4"
5399 ;;
4c2a3841 5400 --listen-v6)
6ae0f7f5 5401 _listen_v6="1"
5402 Le_Listen_V6="$_listen_v6"
5403 ;;
a746139c 5404 --openssl-bin)
5405 _openssl_bin="$2"
851fedf7 5406 ACME_OPENSSL_BIN="$_openssl_bin"
7c2e8754 5407 shift
a746139c 5408 ;;
9b124070 5409 --use-wget)
5410 _use_wget="1"
5411 ACME_USE_WGET="1"
5412 ;;
4c2a3841 5413 *)
a63b05a9 5414 _err "Unknown parameter : $1"
5415 return 1
5416 ;;
5417 esac
5418
5419 shift 1
5420 done
5421
4c2a3841 5422 if [ "${_CMD}" != "install" ]; then
5ea6e9c9 5423 __initHome
661f0583 5424 if [ "$_log" ]; then
4c2a3841 5425 if [ -z "$_logfile" ]; then
661f0583 5426 _logfile="$DEFAULT_LOG_FILE"
5427 fi
d0871bda 5428 fi
4c2a3841 5429 if [ "$_logfile" ]; then
5ea6e9c9 5430 _saveaccountconf "LOG_FILE" "$_logfile"
661f0583 5431 LOG_FILE="$_logfile"
5ea6e9c9 5432 fi
a73c5b33 5433
4c2a3841 5434 if [ "$_log_level" ]; then
a73c5b33 5435 _saveaccountconf "LOG_LEVEL" "$_log_level"
5436 LOG_LEVEL="$_log_level"
5437 fi
4c2a3841 5438
e2edf208 5439 if [ "$_syslog" ]; then
5440 if _exists logger; then
5441 if [ "$_syslog" = "0" ]; then
5442 _clearaccountconf "SYS_LOG"
5443 else
5444 _saveaccountconf "SYS_LOG" "$_syslog"
5445 fi
5446 SYS_LOG="$_syslog"
5447 else
5448 _err "The 'logger' command is not found, can not enable syslog."
5449 _clearaccountconf "SYS_LOG"
5450 SYS_LOG=""
5451 fi
5452 fi
5453
5ea6e9c9 5454 _processAccountConf
5455 fi
4c2a3841 5456
9d548d81 5457 _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
4c2a3841 5458
5459 if [ "$DEBUG" ]; then
dcf9cb58 5460 version
5461 fi
a63b05a9 5462
5463 case "${_CMD}" in
27dbe77f 5464 install) install "$_nocron" "$_confighome" ;;
bc96082f 5465 uninstall) uninstall "$_nocron" ;;
52677b0a 5466 upgrade) upgrade ;;
a63b05a9 5467 issue)
5c539af7 5468 issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address"
a63b05a9 5469 ;;
a61fe418 5470 deploy)
5471 deploy "$_domain" "$_deploy_hook" "$_ecc"
5472 ;;
10afcaca 5473 signcsr)
5474 signcsr "$_csr" "$_webroot"
5475 ;;
5476 showcsr)
5477 showcsr "$_csr" "$_domain"
5478 ;;
a63b05a9 5479 installcert)
5c539af7 5480 installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
a63b05a9 5481 ;;
4c2a3841 5482 renew)
43822d37 5483 renew "$_domain" "$_ecc"
a63b05a9 5484 ;;
4c2a3841 5485 renewAll)
cc179731 5486 renewAll "$_stopRenewOnError"
a63b05a9 5487 ;;
4c2a3841 5488 revoke)
43822d37 5489 revoke "$_domain" "$_ecc"
a63b05a9 5490 ;;
78f0201d 5491 remove)
5492 remove "$_domain" "$_ecc"
5493 ;;
4c2a3841 5494 deactivate)
3f4513b3 5495 deactivate "$_domain,$_altdomains"
eb59817e 5496 ;;
4c2a3841 5497 registeraccount)
57e58ce7 5498 registeraccount "$_accountkeylength"
eb59817e 5499 ;;
4c2a3841 5500 updateaccount)
eb59817e 5501 updateaccount
5502 ;;
4c2a3841 5503 list)
dcf4f8f6 5504 list "$_listraw"
6d7eda3e 5505 ;;
27dbe77f 5506 installcronjob) installcronjob "$_confighome" ;;
a63b05a9 5507 uninstallcronjob) uninstallcronjob ;;
5508 cron) cron ;;
4c2a3841 5509 toPkcs)
43822d37 5510 toPkcs "$_domain" "$_password" "$_ecc"
a63b05a9 5511 ;;
4410226d 5512 toPkcs8)
5513 toPkcs8 "$_domain" "$_ecc"
5514 ;;
4c2a3841 5515 createAccountKey)
5fbc47eb 5516 createAccountKey "$_accountkeylength"
a63b05a9 5517 ;;
4c2a3841 5518 createDomainKey)
a63b05a9 5519 createDomainKey "$_domain" "$_keylength"
5520 ;;
4c2a3841 5521 createCSR)
43822d37 5522 createCSR "$_domain" "$_altdomains" "$_ecc"
a63b05a9 5523 ;;
5524
5525 *)
27dbe77f 5526 if [ "$_CMD" ]; then
5527 _err "Invalid command: $_CMD"
5528 fi
4c2a3841 5529 showhelp
a63b05a9 5530 return 1
4c2a3841 5531 ;;
a63b05a9 5532 esac
d3595686 5533 _ret="$?"
4c2a3841 5534 if [ "$_ret" != "0" ]; then
d3595686 5535 return $_ret
5536 fi
4c2a3841 5537
5538 if [ "${_CMD}" = "install" ]; then
5539 if [ "$_log" ]; then
5540 if [ -z "$LOG_FILE" ]; then
d0871bda 5541 LOG_FILE="$DEFAULT_LOG_FILE"
5542 fi
5543 _saveaccountconf "LOG_FILE" "$LOG_FILE"
5ea6e9c9 5544 fi
4c2a3841 5545
5546 if [ "$_log_level" ]; then
a73c5b33 5547 _saveaccountconf "LOG_LEVEL" "$_log_level"
5548 fi
e2edf208 5549
5550 if [ "$_syslog" ]; then
5551 if _exists logger; then
5552 if [ "$_syslog" = "0" ]; then
5553 _clearaccountconf "SYS_LOG"
5554 else
5555 _saveaccountconf "SYS_LOG" "$_syslog"
5556 fi
5557 else
5558 _err "The 'logger' command is not found, can not enable syslog."
5559 _clearaccountconf "SYS_LOG"
5560 SYS_LOG=""
5561 fi
5562 fi
5563
5ea6e9c9 5564 _processAccountConf
b5eb4b90 5565 fi
635695ec 5566
a63b05a9 5567}
5568
4c2a3841 5569if [ "$INSTALLONLINE" ]; then
d1f97fc8 5570 INSTALLONLINE=""
2fbf3991 5571 _installOnline
4a0f23e2 5572 exit
5573fi
4c3b3608 5574
319e0ae3 5575main() {
5576 [ -z "$1" ] && showhelp && return
4c2a3841 5577 if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
319e0ae3 5578}
e69a7c38 5579
aa7b82de 5580main "$@"