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