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