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