]> git.proxmox.com Git - mirror_acme.sh.git/blame - acme.sh
add always-force-new-domain-key. fix https://github.com/Neilpang/acme.sh/issues/914
[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
c4b2e582 1284 if [ ! -f "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$IS_RENEW" ]) || [ "$Le_ForceNewDomainKey" = "1" ] ; 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
c4b2e582 3151 _info "Update account tos info 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"
c4b2e582 3358 if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ] || [ "$Le_ForceNewDomainKey" = "1" ]; then
d9c9114b 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
c4b2e582 3888 if [ "$Le_ForceNewDomainKey" = "1" ]; then
3889 _savedomainconf "Le_ForceNewDomainKey" "$Le_ForceNewDomainKey"
3890 else
3891 _cleardomainconf Le_ForceNewDomainKey
3892 fi
3893
ca7202eb 3894 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
4c2a3841 3895
ca7202eb 3896 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
4c2a3841 3897 _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
3898
ca7202eb 3899 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
4c2a3841 3900 _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
f6dcd989 3901
85e1f4ea 3902 _on_issue_success "$_post_hook" "$_renew_hook"
4c3b3608 3903
85e1f4ea 3904 if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
3905 _savedomainconf "Le_RealCertPath" "$_real_cert"
3906 _savedomainconf "Le_RealCACertPath" "$_real_ca"
3907 _savedomainconf "Le_RealKeyPath" "$_real_key"
3908 _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
3909 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
044da37c 3910 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
01f54558 3911 fi
4c0d3f1b 3912
4c3b3608 3913}
3914
43822d37 3915#domain [isEcc]
4c3b3608 3916renew() {
3917 Le_Domain="$1"
4c2a3841 3918 if [ -z "$Le_Domain" ]; then
43822d37 3919 _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
4c3b3608 3920 return 1
3921 fi
3922
43822d37 3923 _isEcc="$2"
3924
e799ef29 3925 _initpath "$Le_Domain" "$_isEcc"
43822d37 3926
e2053b22 3927 _info "$(__green "Renew: '$Le_Domain'")"
4c2a3841 3928 if [ ! -f "$DOMAIN_CONF" ]; then
43822d37 3929 _info "'$Le_Domain' is not a issued domain, skip."
4c2a3841 3930 return 0
4c3b3608 3931 fi
4c2a3841 3932
3933 if [ "$Le_RenewalDays" ]; then
1e6b68f5 3934 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
3935 fi
3936
8663fb7e 3937 . "$DOMAIN_CONF"
4c2a3841 3938
3939 if [ "$Le_API" ]; then
48d9a8c1 3940 if [ "$_OLD_CA_HOST" = "$Le_API" ]; then
3941 export Le_API="$DEFAULT_CA"
3942 _savedomainconf Le_API "$Le_API"
3943 fi
4a2ac7bd 3944 if [ "$_OLD_STAGE_CA_HOST" = "$Le_API" ]; then
3945 export Le_API="$STAGE_CA"
3946 _savedomainconf Le_API "$Le_API"
3947 fi
48d9a8c1 3948 export ACME_DIRECTORY="$Le_API"
c4236e58 3949 #reload ca configs
3950 ACCOUNT_KEY_PATH=""
3951 ACCOUNT_JSON_PATH=""
3952 CA_CONF=""
3953 _debug3 "initpath again."
3954 _initpath "$Le_Domain" "$_isEcc"
5c48e139 3955 fi
4c2a3841 3956
3957 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
e2053b22 3958 _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
3959 _info "Add '$(__red '--force')' to force to renew."
e799ef29 3960 return "$RENEW_SKIP"
4c3b3608 3961 fi
4c2a3841 3962
4c3b3608 3963 IS_RENEW="1"
0463b5d6 3964 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 3965 res="$?"
4c2a3841 3966 if [ "$res" != "0" ]; then
e799ef29 3967 return "$res"
a61fe418 3968 fi
4c2a3841 3969
3970 if [ "$Le_DeployHook" ]; then
93bce1b2 3971 _deploy "$Le_Domain" "$Le_DeployHook"
e799ef29 3972 res="$?"
a61fe418 3973 fi
4c2a3841 3974
4c3b3608 3975 IS_RENEW=""
3976
e799ef29 3977 return "$res"
4c3b3608 3978}
3979
cc179731 3980#renewAll [stopRenewOnError]
4c3b3608 3981renewAll() {
3982 _initpath
cc179731 3983 _stopRenewOnError="$1"
3984 _debug "_stopRenewOnError" "$_stopRenewOnError"
3985 _ret="0"
43822d37 3986
e591d5cf 3987 for di in "${CERT_HOME}"/*.*/; do
3988 _debug di "$di"
44483dba 3989 if ! [ -d "$di" ]; then
3498a585 3990 _debug "Not directory, skip: $di"
3991 continue
3992 fi
e591d5cf 3993 d=$(basename "$di")
201aa244 3994 _debug d "$d"
43822d37 3995 (
201aa244 3996 if _endswith "$d" "$ECC_SUFFIX"; then
3997 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
3998 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 3999 fi
4000 renew "$d" "$_isEcc"
4d2f38b0 4001 )
cc179731 4002 rc="$?"
4003 _debug "Return code: $rc"
4c2a3841 4004 if [ "$rc" != "0" ]; then
4005 if [ "$rc" = "$RENEW_SKIP" ]; then
cc179731 4006 _info "Skipped $d"
4c2a3841 4007 elif [ "$_stopRenewOnError" ]; then
cc179731 4008 _err "Error renew $d, stop now."
201aa244 4009 return "$rc"
cc179731 4010 else
4011 _ret="$rc"
482cb737 4012 _err "Error renew $d."
cc179731 4013 fi
4014 fi
4c3b3608 4015 done
201aa244 4016 return "$_ret"
4c3b3608 4017}
4018
10afcaca 4019#csr webroot
4c2a3841 4020signcsr() {
10afcaca 4021 _csrfile="$1"
4022 _csrW="$2"
4023 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
4024 _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ "
4025 return 1
4026 fi
4027
4028 _initpath
4029
4030 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 4031 if [ "$?" != "0" ]; then
10afcaca 4032 _err "Can not read subject from csr: $_csrfile"
4033 return 1
4034 fi
ad752b31 4035 _debug _csrsubj "$_csrsubj"
2c9ed4c5 4036 if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.'; then
4037 _info "It seems that the subject: $_csrsubj is not a valid domain name. Drop it."
4038 _csrsubj=""
4039 fi
10afcaca 4040
4041 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 4042 if [ "$?" != "0" ]; then
10afcaca 4043 _err "Can not read domain list from csr: $_csrfile"
4044 return 1
4045 fi
4046 _debug "_csrdomainlist" "$_csrdomainlist"
4c2a3841 4047
4048 if [ -z "$_csrsubj" ]; then
ad752b31 4049 _csrsubj="$(_getfield "$_csrdomainlist" 1)"
4050 _debug _csrsubj "$_csrsubj"
4051 _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
4052 _debug "_csrdomainlist" "$_csrdomainlist"
4053 fi
4c2a3841 4054
4055 if [ -z "$_csrsubj" ]; then
ad752b31 4056 _err "Can not read subject from csr: $_csrfile"
4057 return 1
4058 fi
4c2a3841 4059
10afcaca 4060 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 4061 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 4062 _err "Can not read key length from csr: $_csrfile"
4063 return 1
4064 fi
4c2a3841 4065
10afcaca 4066 _initpath "$_csrsubj" "$_csrkeylength"
4067 mkdir -p "$DOMAIN_PATH"
4c2a3841 4068
10afcaca 4069 _info "Copy csr to: $CSR_PATH"
4070 cp "$_csrfile" "$CSR_PATH"
4c2a3841 4071
10afcaca 4072 issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength"
4c2a3841 4073
10afcaca 4074}
4075
4076showcsr() {
4c2a3841 4077 _csrfile="$1"
10afcaca 4078 _csrd="$2"
4079 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
4080 _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr"
4081 return 1
4082 fi
4083
4084 _initpath
4c2a3841 4085
10afcaca 4086 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 4087 if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then
10afcaca 4088 _err "Can not read subject from csr: $_csrfile"
4089 return 1
4090 fi
4c2a3841 4091
10afcaca 4092 _info "Subject=$_csrsubj"
4093
4094 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 4095 if [ "$?" != "0" ]; then
10afcaca 4096 _err "Can not read domain list from csr: $_csrfile"
4097 return 1
4098 fi
4099 _debug "_csrdomainlist" "$_csrdomainlist"
4100
4101 _info "SubjectAltNames=$_csrdomainlist"
4102
10afcaca 4103 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 4104 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 4105 _err "Can not read key length from csr: $_csrfile"
4106 return 1
4107 fi
4108 _info "KeyLength=$_csrkeylength"
4109}
4110
6d7eda3e 4111list() {
22ea4004 4112 _raw="$1"
6d7eda3e 4113 _initpath
4c2a3841 4114
dcf4f8f6 4115 _sep="|"
4c2a3841 4116 if [ "$_raw" ]; then
d5ec5f80 4117 printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew"
e591d5cf 4118 for di in "${CERT_HOME}"/*.*/; do
44483dba 4119 if ! [ -d "$di" ]; then
3498a585 4120 _debug "Not directory, skip: $di"
4121 continue
4122 fi
e591d5cf 4123 d=$(basename "$di")
201aa244 4124 _debug d "$d"
dcf4f8f6 4125 (
201aa244 4126 if _endswith "$d" "$ECC_SUFFIX"; then
4127 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
4128 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 4129 fi
e591d5cf 4130 _initpath "$d" "$_isEcc"
4c2a3841 4131 if [ -f "$DOMAIN_CONF" ]; then
dcf4f8f6 4132 . "$DOMAIN_CONF"
d5ec5f80 4133 printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
dcf4f8f6 4134 fi
4135 )
4136 done
4137 else
4c2a3841 4138 if _exists column; then
22ea4004 4139 list "raw" | column -t -s "$_sep"
4140 else
43822d37 4141 list "raw" | tr "$_sep" '\t'
22ea4004 4142 fi
dcf4f8f6 4143 fi
6d7eda3e 4144
6d7eda3e 4145}
4146
93bce1b2 4147_deploy() {
4148 _d="$1"
4149 _hooks="$2"
4150
4151 for _d_api in $(echo "$_hooks" | tr ',' " "); do
4152 _deployApi="$(_findHook "$_d" deploy "$_d_api")"
4153 if [ -z "$_deployApi" ]; then
4154 _err "The deploy hook $_d_api is not found."
4155 return 1
4156 fi
4157 _debug _deployApi "$_deployApi"
4158
4159 if ! (
4160 if ! . "$_deployApi"; then
4161 _err "Load file $_deployApi error. Please check your api file and try again."
4162 return 1
4163 fi
4164
4165 d_command="${_d_api}_deploy"
4166 if ! _exists "$d_command"; then
4167 _err "It seems that your api file is not correct, it must have a function named: $d_command"
4168 return 1
4169 fi
4170
4171 if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
4172 _err "Error deploy for domain:$_d"
4173 return 1
4174 fi
4175 ); then
4176 _err "Deploy error."
4177 return 1
4178 else
4179 _info "$(__green Success)"
4180 fi
4181 done
4182}
4183
4184#domain hooks
a61fe418 4185deploy() {
93bce1b2 4186 _d="$1"
4187 _hooks="$2"
a61fe418 4188 _isEcc="$3"
93bce1b2 4189 if [ -z "$_hooks" ]; then
a61fe418 4190 _usage "Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] "
4191 return 1
4192 fi
4193
93bce1b2 4194 _initpath "$_d" "$_isEcc"
4c2a3841 4195 if [ ! -d "$DOMAIN_PATH" ]; then
93bce1b2 4196 _err "Domain is not valid:'$_d'"
a61fe418 4197 return 1
4198 fi
4c2a3841 4199
93bce1b2 4200 . "$DOMAIN_CONF"
4c2a3841 4201
93bce1b2 4202 _savedomainconf Le_DeployHook "$_hooks"
4c2a3841 4203
93bce1b2 4204 _deploy "$_d" "$_hooks"
a61fe418 4205}
4206
4c3b3608 4207installcert() {
85e1f4ea 4208 _main_domain="$1"
4209 if [ -z "$_main_domain" ]; then
5c539af7 4210 _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 4211 return 1
4212 fi
4213
85e1f4ea 4214 _real_cert="$2"
4215 _real_key="$3"
4216 _real_ca="$4"
4217 _reload_cmd="$5"
4218 _real_fullchain="$6"
43822d37 4219 _isEcc="$7"
4220
85e1f4ea 4221 _initpath "$_main_domain" "$_isEcc"
4c2a3841 4222 if [ ! -d "$DOMAIN_PATH" ]; then
85e1f4ea 4223 _err "Domain is not valid:'$_main_domain'"
43822d37 4224 return 1
4225 fi
4226
85e1f4ea 4227 _savedomainconf "Le_RealCertPath" "$_real_cert"
4228 _savedomainconf "Le_RealCACertPath" "$_real_ca"
4229 _savedomainconf "Le_RealKeyPath" "$_real_key"
4230 _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
4231 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
4232
044da37c 4233 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
43822d37 4234}
4c3b3608 4235
044da37c 4236#domain cert key ca fullchain reloadcmd backup-prefix
43822d37 4237_installcert() {
85e1f4ea 4238 _main_domain="$1"
4239 _real_cert="$2"
4240 _real_key="$3"
4241 _real_ca="$4"
044da37c 4242 _real_fullchain="$5"
4243 _reload_cmd="$6"
4244 _backup_prefix="$7"
4c3b3608 4245
85e1f4ea 4246 if [ "$_real_cert" = "$NO_VALUE" ]; then
4247 _real_cert=""
4d2f38b0 4248 fi
85e1f4ea 4249 if [ "$_real_key" = "$NO_VALUE" ]; then
4250 _real_key=""
4d2f38b0 4251 fi
85e1f4ea 4252 if [ "$_real_ca" = "$NO_VALUE" ]; then
4253 _real_ca=""
4d2f38b0 4254 fi
85e1f4ea 4255 if [ "$_reload_cmd" = "$NO_VALUE" ]; then
4256 _reload_cmd=""
4d2f38b0 4257 fi
85e1f4ea 4258 if [ "$_real_fullchain" = "$NO_VALUE" ]; then
4259 _real_fullchain=""
4d2f38b0 4260 fi
4c2a3841 4261
044da37c 4262 _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
4263 mkdir -p "$_backup_path"
4264
85e1f4ea 4265 if [ "$_real_cert" ]; then
4266 _info "Installing cert to:$_real_cert"
4267 if [ -f "$_real_cert" ] && [ ! "$IS_RENEW" ]; then
044da37c 4268 cp "$_real_cert" "$_backup_path/cert.bak"
4c3b3608 4269 fi
85e1f4ea 4270 cat "$CERT_PATH" >"$_real_cert"
4c3b3608 4271 fi
4c2a3841 4272
85e1f4ea 4273 if [ "$_real_ca" ]; then
4274 _info "Installing CA to:$_real_ca"
4275 if [ "$_real_ca" = "$_real_cert" ]; then
4276 echo "" >>"$_real_ca"
4277 cat "$CA_CERT_PATH" >>"$_real_ca"
4c3b3608 4278 else
85e1f4ea 4279 if [ -f "$_real_ca" ] && [ ! "$IS_RENEW" ]; then
044da37c 4280 cp "$_real_ca" "$_backup_path/ca.bak"
78552b18 4281 fi
85e1f4ea 4282 cat "$CA_CERT_PATH" >"$_real_ca"
4c3b3608 4283 fi
4284 fi
4285
85e1f4ea 4286 if [ "$_real_key" ]; then
4287 _info "Installing key to:$_real_key"
4288 if [ -f "$_real_key" ] && [ ! "$IS_RENEW" ]; then
044da37c 4289 cp "$_real_key" "$_backup_path/key.bak"
4c3b3608 4290 fi
85e1f4ea 4291 cat "$CERT_KEY_PATH" >"$_real_key"
4c3b3608 4292 fi
4c2a3841 4293
85e1f4ea 4294 if [ "$_real_fullchain" ]; then
4295 _info "Installing full chain to:$_real_fullchain"
4296 if [ -f "$_real_fullchain" ] && [ ! "$IS_RENEW" ]; then
044da37c 4297 cp "$_real_fullchain" "$_backup_path/fullchain.bak"
a63b05a9 4298 fi
85e1f4ea 4299 cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain"
4c2a3841 4300 fi
4c3b3608 4301
85e1f4ea 4302 if [ "$_reload_cmd" ]; then
4303 _info "Run reload cmd: $_reload_cmd"
25555b8c 4304 if (
839bf0e2 4305 export CERT_PATH
4306 export CERT_KEY_PATH
4307 export CA_CERT_PATH
4308 export CERT_FULLCHAIN_PATH
58d4c74b 4309 export Le_Domain
85e1f4ea 4310 cd "$DOMAIN_PATH" && eval "$_reload_cmd"
839bf0e2 4311 ); then
43822d37 4312 _info "$(__green "Reload success")"
4d2f38b0 4313 else
4314 _err "Reload error for :$Le_Domain"
4315 fi
4316 fi
4317
4c3b3608 4318}
4319
27dbe77f 4320#confighome
4c3b3608 4321installcronjob() {
27dbe77f 4322 _c_home="$1"
4c3b3608 4323 _initpath
4c2a3841 4324 if ! _exists "crontab"; then
77546ea5 4325 _err "crontab doesn't exist, so, we can not install cron jobs."
4326 _err "All your certs will not be renewed automatically."
a7b7355d 4327 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
77546ea5 4328 return 1
4329 fi
4330
4c3b3608 4331 _info "Installing cron job"
4c2a3841 4332 if ! crontab -l | grep "$PROJECT_ENTRY --cron"; then
4333 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
a7b7355d 4334 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
4c3b3608 4335 else
a7b7355d 4336 _err "Can not install cronjob, $PROJECT_ENTRY not found."
4c3b3608 4337 return 1
4338 fi
27dbe77f 4339
4340 if [ "$_c_home" ]; then
80941f84 4341 _c_entry="--config-home \"$_c_home\" "
27dbe77f 4342 fi
32b3717c 4343 _t=$(_time)
4344 random_minute=$(_math $_t % 60)
e2c939fb 4345 if _exists uname && uname -a | grep SunOS >/dev/null; then
4c2a3841 4346 crontab -l | {
4347 cat
0533bde9 4348 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
4c2a3841 4349 } | crontab --
22ea4004 4350 else
4c2a3841 4351 crontab -l | {
4352 cat
0533bde9 4353 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
4c2a3841 4354 } | crontab -
22ea4004 4355 fi
4c3b3608 4356 fi
4c2a3841 4357 if [ "$?" != "0" ]; then
4c3b3608 4358 _err "Install cron job failed. You need to manually renew your certs."
4359 _err "Or you can add cronjob by yourself:"
a7b7355d 4360 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4c3b3608 4361 return 1
4362 fi
4363}
4364
4365uninstallcronjob() {
4c2a3841 4366 if ! _exists "crontab"; then
37db5b81 4367 return
4368 fi
4c3b3608 4369 _info "Removing cron job"
a7b7355d 4370 cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")"
4c2a3841 4371 if [ "$cr" ]; then
4372 if _exists uname && uname -a | grep solaris >/dev/null; then
22ea4004 4373 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab --
4374 else
4375 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -
4376 fi
a7b7355d 4377 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4c3b3608 4378 _info LE_WORKING_DIR "$LE_WORKING_DIR"
27dbe77f 4379 if _contains "$cr" "--config-home"; then
f5b546b3 4380 LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
4381 _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
27dbe77f 4382 fi
4c2a3841 4383 fi
4c3b3608 4384 _initpath
a7b7355d 4385
4c3b3608 4386}
4387
6cb415f5 4388revoke() {
4389 Le_Domain="$1"
4c2a3841 4390 if [ -z "$Le_Domain" ]; then
78f0201d 4391 _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com [--ecc]"
6cb415f5 4392 return 1
4393 fi
4c2a3841 4394
43822d37 4395 _isEcc="$2"
4396
c4a375b3 4397 _initpath "$Le_Domain" "$_isEcc"
4c2a3841 4398 if [ ! -f "$DOMAIN_CONF" ]; then
6cb415f5 4399 _err "$Le_Domain is not a issued domain, skip."
4c2a3841 4400 return 1
6cb415f5 4401 fi
4c2a3841 4402
4403 if [ ! -f "$CERT_PATH" ]; then
6cb415f5 4404 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
4405 return 1
4406 fi
6cb415f5 4407
11927a76 4408 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
4c2a3841 4409
4410 if [ -z "$cert" ]; then
6cb415f5 4411 _err "Cert for $Le_Domain is empty found, skip."
4412 return 1
4413 fi
4c2a3841 4414
48d9a8c1 4415 _initAPI
4416
6cb415f5 4417 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
48d9a8c1 4418 uri="${ACME_REVOKE_CERT}"
6cb415f5 4419
4c2a3841 4420 if [ -f "$CERT_KEY_PATH" ]; then
1befee5a 4421 _info "Try domain key first."
c4a375b3 4422 if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
4c2a3841 4423 if [ -z "$response" ]; then
1befee5a 4424 _info "Revoke success."
c4a375b3 4425 rm -f "$CERT_PATH"
1befee5a 4426 return 0
4c2a3841 4427 else
1befee5a 4428 _err "Revoke error by domain key."
4429 _err "$response"
4430 fi
6cb415f5 4431 fi
4c2a3841 4432 else
1befee5a 4433 _info "Domain key file doesn't exists."
6cb415f5 4434 fi
6cb415f5 4435
1befee5a 4436 _info "Try account key."
6cb415f5 4437
c4a375b3 4438 if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
4c2a3841 4439 if [ -z "$response" ]; then
6cb415f5 4440 _info "Revoke success."
c4a375b3 4441 rm -f "$CERT_PATH"
6cb415f5 4442 return 0
4c2a3841 4443 else
6cb415f5 4444 _err "Revoke error."
c9c31c04 4445 _debug "$response"
6cb415f5 4446 fi
4447 fi
4448 return 1
4449}
4c3b3608 4450
78f0201d 4451#domain ecc
4452remove() {
4453 Le_Domain="$1"
4454 if [ -z "$Le_Domain" ]; then
4455 _usage "Usage: $PROJECT_ENTRY --remove -d domain.com [--ecc]"
4456 return 1
4457 fi
4458
4459 _isEcc="$2"
4460
4461 _initpath "$Le_Domain" "$_isEcc"
4462 _removed_conf="$DOMAIN_CONF.removed"
4463 if [ ! -f "$DOMAIN_CONF" ]; then
4464 if [ -f "$_removed_conf" ]; then
4465 _err "$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH"
4466 else
4467 _err "$Le_Domain is not a issued domain, skip."
4468 fi
4469 return 1
4470 fi
4471
4472 if mv "$DOMAIN_CONF" "$_removed_conf"; then
68aea3af 4473 _info "$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)"
78f0201d 4474 _info "You can remove them by yourself."
4475 return 0
4476 else
4477 _err "Remove $Le_Domain failed."
4478 return 1
4479 fi
4480}
4481
0c00e870 4482#domain vtype
4483_deactivate() {
4484 _d_domain="$1"
4485 _d_type="$2"
4486 _initpath
4c2a3841 4487
14d7bfda 4488 if ! __get_domain_new_authz "$_d_domain"; then
4489 _err "Can not get domain new authz token."
4490 return 1
4491 fi
4c2a3841 4492
14d7bfda 4493 authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
4494 _debug "authzUri" "$authzUri"
4c2a3841 4495
14d7bfda 4496 if [ "$code" ] && [ ! "$code" = '201' ]; then
4497 _err "new-authz error: $response"
4498 return 1
4499 fi
0c00e870 4500
14d7bfda 4501 entries="$(echo "$response" | _egrep_o '{ *"type":"[^"]*", *"status": *"valid", *"uri"[^}]*')"
4502 if [ -z "$entries" ]; then
4503 _info "No valid entries found."
4504 if [ -z "$thumbprint" ]; then
4505 thumbprint="$(__calc_account_thumbprint)"
4506 fi
4507 _debug "Trigger validation."
4508 vtype="$VTYPE_HTTP"
4509 entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
4510 _debug entry "$entry"
4511 if [ -z "$entry" ]; then
4512 _err "Error, can not get domain token $d"
0c00e870 4513 return 1
4514 fi
14d7bfda 4515 token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
4516 _debug token "$token"
4c2a3841 4517
14d7bfda 4518 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
4519 _debug uri "$uri"
4520
4521 keyauthorization="$token.$thumbprint"
4522 _debug keyauthorization "$keyauthorization"
4523 __trigger_validation "$uri" "$keyauthorization"
4524
4525 fi
4526
4527 _d_i=0
4528 _d_max_retry=$(echo "$entries" | wc -l)
4529 while [ "$_d_i" -lt "$_d_max_retry" ]; do
4530 _info "Deactivate: $_d_domain"
4531 _d_i="$(_math $_d_i + 1)"
4532 entry="$(echo "$entries" | sed -n "${_d_i}p")"
0c00e870 4533 _debug entry "$entry"
4c2a3841 4534
4535 if [ -z "$entry" ]; then
fb2029e7 4536 _info "No more valid entry found."
0c00e870 4537 break
4538 fi
4c2a3841 4539
0c00e870 4540 _vtype="$(printf "%s\n" "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
c4a375b3 4541 _debug _vtype "$_vtype"
0c00e870 4542 _info "Found $_vtype"
4543
4c2a3841 4544 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
c4a375b3 4545 _debug uri "$uri"
4c2a3841 4546
4547 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
0c00e870 4548 _info "Skip $_vtype"
4549 continue
4550 fi
4c2a3841 4551
0c00e870 4552 _info "Deactivate: $_vtype"
4c2a3841 4553
14d7bfda 4554 if _send_signed_request "$authzUri" "{\"resource\": \"authz\", \"status\":\"deactivated\"}" && _contains "$response" '"deactivated"'; then
4555 _info "Deactivate: $_vtype success."
4556 else
0c00e870 4557 _err "Can not deactivate $_vtype."
14d7bfda 4558 break
0c00e870 4559 fi
4c2a3841 4560
0c00e870 4561 done
4562 _debug "$_d_i"
14d7bfda 4563 if [ "$_d_i" -eq "$_d_max_retry" ]; then
0c00e870 4564 _info "Deactivated success!"
4565 else
4566 _err "Deactivate failed."
4567 fi
4568
4569}
4570
4571deactivate() {
3f4513b3 4572 _d_domain_list="$1"
0c00e870 4573 _d_type="$2"
4574 _initpath
a3bdaa85 4575 _initAPI
3f4513b3 4576 _debug _d_domain_list "$_d_domain_list"
4c2a3841 4577 if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
3f4513b3 4578 _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
0c00e870 4579 return 1
4580 fi
4c2a3841 4581 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
4582 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
3f4513b3 4583 continue
4584 fi
c4a375b3 4585 if ! _deactivate "$_d_dm" "$_d_type"; then
86c017ec 4586 return 1
4587 fi
3f4513b3 4588 done
0c00e870 4589}
4590
4c3b3608 4591# Detect profile file if not specified as environment variable
4592_detect_profile() {
4c2a3841 4593 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
4c3b3608 4594 echo "$PROFILE"
4595 return
4596 fi
4597
4c3b3608 4598 DETECTED_PROFILE=''
4c3b3608 4599 SHELLTYPE="$(basename "/$SHELL")"
4600
4c2a3841 4601 if [ "$SHELLTYPE" = "bash" ]; then
4602 if [ -f "$HOME/.bashrc" ]; then
4c3b3608 4603 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 4604 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 4605 DETECTED_PROFILE="$HOME/.bash_profile"
4606 fi
4c2a3841 4607 elif [ "$SHELLTYPE" = "zsh" ]; then
4c3b3608 4608 DETECTED_PROFILE="$HOME/.zshrc"
4609 fi
4610
4c2a3841 4611 if [ -z "$DETECTED_PROFILE" ]; then
4612 if [ -f "$HOME/.profile" ]; then
4c3b3608 4613 DETECTED_PROFILE="$HOME/.profile"
4c2a3841 4614 elif [ -f "$HOME/.bashrc" ]; then
4c3b3608 4615 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 4616 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 4617 DETECTED_PROFILE="$HOME/.bash_profile"
4c2a3841 4618 elif [ -f "$HOME/.zshrc" ]; then
4c3b3608 4619 DETECTED_PROFILE="$HOME/.zshrc"
4620 fi
4621 fi
4622
1be222f6 4623 echo "$DETECTED_PROFILE"
4c3b3608 4624}
4625
4626_initconf() {
4627 _initpath
4c2a3841 4628 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
0ca5b799 4629 echo "
d404e92d 4630
d0871bda 4631#LOG_FILE=\"$DEFAULT_LOG_FILE\"
6b500036 4632#LOG_LEVEL=1
5ea6e9c9 4633
251d1c5c 4634#AUTO_UPGRADE=\"1\"
89002ed2 4635
569d6c55 4636#NO_TIMESTAMP=1
5b771039 4637
d5ec5f80 4638 " >"$ACCOUNT_CONF_PATH"
4c3b3608 4639 fi
4640}
4641
c8e9a31e 4642# nocron
c60883ef 4643_precheck() {
c8e9a31e 4644 _nocron="$1"
4c2a3841 4645
4646 if ! _exists "curl" && ! _exists "wget"; then
c60883ef 4647 _err "Please install curl or wget first, we need to access http resources."
4c3b3608 4648 return 1
4649 fi
4c2a3841 4650
4651 if [ -z "$_nocron" ]; then
4652 if ! _exists "crontab"; then
c8e9a31e 4653 _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
4654 _err "We need to set cron job to renew the certs automatically."
4655 _err "Otherwise, your certs will not be able to be renewed automatically."
4c2a3841 4656 if [ -z "$FORCE" ]; then
c8e9a31e 4657 _err "Please add '--force' and try install again to go without crontab."
4658 _err "./$PROJECT_ENTRY --install --force"
4659 return 1
4660 fi
77546ea5 4661 fi
4c3b3608 4662 fi
4c2a3841 4663
d8ba26e6 4664 if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then
851fedf7 4665 _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN"
c60883ef 4666 _err "We need openssl to generate keys."
4c3b3608 4667 return 1
4668 fi
4c2a3841 4669
4670 if ! _exists "nc"; then
c60883ef 4671 _err "It is recommended to install nc first, try to install 'nc' or 'netcat'."
4672 _err "We use nc for standalone server if you use standalone mode."
4673 _err "If you don't use standalone mode, just ignore this warning."
4674 fi
4c2a3841 4675
c60883ef 4676 return 0
4677}
4678
0a7c9364 4679_setShebang() {
4680 _file="$1"
4681 _shebang="$2"
4c2a3841 4682 if [ -z "$_shebang" ]; then
43822d37 4683 _usage "Usage: file shebang"
0a7c9364 4684 return 1
4685 fi
4686 cp "$_file" "$_file.tmp"
4c2a3841 4687 echo "$_shebang" >"$_file"
4688 sed -n 2,99999p "$_file.tmp" >>"$_file"
4689 rm -f "$_file.tmp"
0a7c9364 4690}
4691
27dbe77f 4692#confighome
94dc5f33 4693_installalias() {
27dbe77f 4694 _c_home="$1"
94dc5f33 4695 _initpath
4696
4697 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4c2a3841 4698 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
44edb2bd 4699 echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
4700 echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
4701 echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
94dc5f33 4702 fi
4703
27dbe77f 4704 if [ "$_c_home" ]; then
be83a6a3 4705 _c_entry=" --config-home '$_c_home'"
27dbe77f 4706 fi
4707
1786a5e5 4708 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
f5b546b3 4709 if [ "$_c_home" ]; then
4710 _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
d04434e3 4711 else
4712 _sed_i "/^export LE_CONFIG_HOME/d" "$_envfile"
f5b546b3 4713 fi
be83a6a3 4714 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
94dc5f33 4715
4716 _profile="$(_detect_profile)"
4c2a3841 4717 if [ "$_profile" ]; then
94dc5f33 4718 _debug "Found profile: $_profile"
aba5c634 4719 _info "Installing alias to '$_profile'"
94dc5f33 4720 _setopt "$_profile" ". \"$_envfile\""
4721 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
4722 else
4723 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
4724 fi
94dc5f33 4725
4726 #for csh
4727 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
94dc5f33 4728 _csh_profile="$HOME/.cshrc"
4c2a3841 4729 if [ -f "$_csh_profile" ]; then
aba5c634 4730 _info "Installing alias to '$_csh_profile'"
6626371d 4731 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
f5b546b3 4732 if [ "$_c_home" ]; then
4733 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
d04434e3 4734 else
4735 _sed_i "/^setenv LE_CONFIG_HOME/d" "$_cshfile"
f5b546b3 4736 fi
be83a6a3 4737 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4c2a3841 4738 _setopt "$_csh_profile" "source \"$_cshfile\""
94dc5f33 4739 fi
4c2a3841 4740
acafa585 4741 #for tcsh
4742 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 4743 if [ -f "$_tcsh_profile" ]; then
aba5c634 4744 _info "Installing alias to '$_tcsh_profile'"
acafa585 4745 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
f5b546b3 4746 if [ "$_c_home" ]; then
4747 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
4748 fi
be83a6a3 4749 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4c2a3841 4750 _setopt "$_tcsh_profile" "source \"$_cshfile\""
acafa585 4751 fi
94dc5f33 4752
4753}
4754
27dbe77f 4755# nocron confighome
c60883ef 4756install() {
f3e4cea3 4757
4c2a3841 4758 if [ -z "$LE_WORKING_DIR" ]; then
f3e4cea3 4759 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
4760 fi
4c2a3841 4761
c8e9a31e 4762 _nocron="$1"
27dbe77f 4763 _c_home="$2"
4c2a3841 4764 if ! _initpath; then
c60883ef 4765 _err "Install failed."
4c3b3608 4766 return 1
4767 fi
4c2a3841 4768 if [ "$_nocron" ]; then
52677b0a 4769 _debug "Skip install cron job"
4770 fi
4c2a3841 4771
4772 if ! _precheck "$_nocron"; then
c60883ef 4773 _err "Pre-check failed, can not install."
4c3b3608 4774 return 1
4775 fi
4c2a3841 4776
8e845d9f 4777 if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then
4778 _info "Using config home: $LE_CONFIG_HOME"
4779 _c_home="$LE_CONFIG_HOME"
4780 fi
4781
6cc11ffb 4782 #convert from le
4c2a3841 4783 if [ -d "$HOME/.le" ]; then
4784 for envfile in "le.env" "le.sh.env"; do
4785 if [ -f "$HOME/.le/$envfile" ]; then
4786 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
4787 _upgrading="1"
4788 _info "You are upgrading from le.sh"
4789 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
4790 mv "$HOME/.le" "$LE_WORKING_DIR"
4791 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4792 break
6cc11ffb 4793 fi
4794 fi
4795 done
4796 fi
4797
4c3b3608 4798 _info "Installing to $LE_WORKING_DIR"
635695ec 4799
d04434e3 4800 if [ ! -d "$LE_WORKING_DIR" ]; then
4801 if ! mkdir -p "$LE_WORKING_DIR"; then
4802 _err "Can not create working dir: $LE_WORKING_DIR"
4803 return 1
4804 fi
4805
4806 chmod 700 "$LE_WORKING_DIR"
4a0f23e2 4807 fi
4c2a3841 4808
d04434e3 4809 if [ ! -d "$LE_CONFIG_HOME" ]; then
4810 if ! mkdir -p "$LE_CONFIG_HOME"; then
4811 _err "Can not create config dir: $LE_CONFIG_HOME"
4812 return 1
4813 fi
762978f8 4814
d04434e3 4815 chmod 700 "$LE_CONFIG_HOME"
27dbe77f 4816 fi
4817
d5ec5f80 4818 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 4819
4c2a3841 4820 if [ "$?" != "0" ]; then
a7b7355d 4821 _err "Install failed, can not copy $PROJECT_ENTRY"
4c3b3608 4822 return 1
4823 fi
4824
a7b7355d 4825 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 4826
27dbe77f 4827 _installalias "$_c_home"
4c3b3608 4828
4c2a3841 4829 for subf in $_SUB_FOLDERS; do
4830 if [ -d "$subf" ]; then
d5ec5f80 4831 mkdir -p "$LE_WORKING_DIR/$subf"
4832 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
a61fe418 4833 fi
4834 done
4835
4c2a3841 4836 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4c3b3608 4837 _initconf
4838 fi
6cc11ffb 4839
4c2a3841 4840 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
635695ec 4841 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
6cc11ffb 4842 fi
4843
4c2a3841 4844 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
b2817897 4845 _saveaccountconf "CERT_HOME" "$CERT_HOME"
4846 fi
4847
4c2a3841 4848 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
b2817897 4849 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
4850 fi
4c2a3841 4851
4852 if [ -z "$_nocron" ]; then
27dbe77f 4853 installcronjob "$_c_home"
c8e9a31e 4854 fi
0a7c9364 4855
4c2a3841 4856 if [ -z "$NO_DETECT_SH" ]; then
641989fd 4857 #Modify shebang
4c2a3841 4858 if _exists bash; then
329174b6 4859 _info "Good, bash is found, so change the shebang to use bash as preferred."
4dd646a4 4860 _shebang='#!'"$(env bash -c "command -v bash")"
641989fd 4861 _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
4c2a3841 4862 for subf in $_SUB_FOLDERS; do
4863 if [ -d "$LE_WORKING_DIR/$subf" ]; then
4864 for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
a61fe418 4865 _setShebang "$_apifile" "$_shebang"
4866 done
4867 fi
4868 done
0a7c9364 4869 fi
4870 fi
4871
4c3b3608 4872 _info OK
4873}
4874
52677b0a 4875# nocron
4c3b3608 4876uninstall() {
52677b0a 4877 _nocron="$1"
4c2a3841 4878 if [ -z "$_nocron" ]; then
52677b0a 4879 uninstallcronjob
4880 fi
4c3b3608 4881 _initpath
4882
9aa3be7f 4883 _uninstallalias
4c2a3841 4884
d5ec5f80 4885 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
f5b546b3 4886 _info "The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself."
9aa3be7f 4887
4888}
4889
4890_uninstallalias() {
4891 _initpath
4892
4c3b3608 4893 _profile="$(_detect_profile)"
4c2a3841 4894 if [ "$_profile" ]; then
9aa3be7f 4895 _info "Uninstalling alias from: '$_profile'"
d5ec5f80 4896 text="$(cat "$_profile")"
4c2a3841 4897 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
4c3b3608 4898 fi
4899
94dc5f33 4900 _csh_profile="$HOME/.cshrc"
4c2a3841 4901 if [ -f "$_csh_profile" ]; then
9aa3be7f 4902 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 4903 text="$(cat "$_csh_profile")"
4c2a3841 4904 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
94dc5f33 4905 fi
4c2a3841 4906
acafa585 4907 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 4908 if [ -f "$_tcsh_profile" ]; then
9aa3be7f 4909 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 4910 text="$(cat "$_tcsh_profile")"
4c2a3841 4911 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
acafa585 4912 fi
4c3b3608 4913
4914}
4915
4916cron() {
281aa349 4917 IN_CRON=1
89002ed2 4918 _initpath
d8ba26e6 4919 _info "$(__green "===Starting cron===")"
4c2a3841 4920 if [ "$AUTO_UPGRADE" = "1" ]; then
89002ed2 4921 export LE_WORKING_DIR
4922 (
4c2a3841 4923 if ! upgrade; then
4924 _err "Cron:Upgrade failed!"
4925 return 1
4926 fi
89002ed2 4927 )
d5ec5f80 4928 . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
1ab63043 4929
4c2a3841 4930 if [ -t 1 ]; then
1ab63043 4931 __INTERACTIVE="1"
4932 fi
4c2a3841 4933
89002ed2 4934 _info "Auto upgraded to: $VER"
4935 fi
4c3b3608 4936 renewAll
cc179731 4937 _ret="$?"
281aa349 4938 IN_CRON=""
d8ba26e6 4939 _info "$(__green "===End cron===")"
0ba95a3d 4940 exit $_ret
4c3b3608 4941}
4942
4943version() {
a63b05a9 4944 echo "$PROJECT"
4945 echo "v$VER"
4c3b3608 4946}
4947
4948showhelp() {
d0871bda 4949 _initpath
4c3b3608 4950 version
a7b7355d 4951 echo "Usage: $PROJECT_ENTRY command ...[parameters]....
a63b05a9 4952Commands:
4953 --help, -h Show this help message.
4954 --version, -v Show version info.
a7b7355d 4955 --install Install $PROJECT_NAME to your system.
4956 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
d8beaf72 4957 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
a63b05a9 4958 --issue Issue a cert.
10afcaca 4959 --signcsr Issue a cert from an existing csr.
a61fe418 4960 --deploy Deploy the cert to your server.
27dbe77f 4961 --install-cert Install the issued cert to apache/nginx or any other server.
a63b05a9 4962 --renew, -r Renew a cert.
27dbe77f 4963 --renew-all Renew all the certs.
a63b05a9 4964 --revoke Revoke a cert.
78f0201d 4965 --remove Remove the cert from $PROJECT
10afcaca 4966 --list List all the certs.
4967 --showcsr Show the content of a csr.
27dbe77f 4968 --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.
4969 --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
a63b05a9 4970 --cron Run cron job to renew all the certs.
4971 --toPkcs Export the certificate and key to a pfx file.
4410226d 4972 --toPkcs8 Convert to pkcs8 format.
27dbe77f 4973 --update-account Update account info.
4974 --register-account Register account key.
0984585d 4975 --create-account-key Create an account private key, professional use.
4976 --create-domain-key Create an domain private key, professional use.
a63b05a9 4977 --createCSR, -ccsr Create CSR , professional use.
0c00e870 4978 --deactivate Deactivate the domain authz, professional use.
3c07f57a 4979
a63b05a9 4980Parameters:
4981 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
4982 --force, -f Used to force to install or force to renew a cert immediately.
4983 --staging, --test Use staging server, just for test.
4984 --debug Output debug info.
e6e85b0c 4985 --output-insecure Output all the sensitive messages. By default all the credentials/sensitive messages are hidden from the output/debug/log for secure.
a63b05a9 4986 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
4987 --standalone Use standalone mode.
0e44f587 4988 --stateless Use stateless mode, see: $_STATELESS_WIKI
e22bcf7c 4989 --tls Use standalone tls mode.
a63b05a9 4990 --apache Use apache mode.
eccec5f6 4991 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
4a4dacb5 4992 --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 4993
a63b05a9 4994 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
4995 --accountkeylength, -ak [2048] Specifies the account key length.
d0871bda 4996 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
a73c5b33 4997 --log-level 1|2 Specifies the log level, default is 1.
52765466 4998 --syslog [0|3|6|7] Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
3c07f57a 4999
a63b05a9 5000 These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
3c07f57a 5001
13fe54c9 5002 --cert-file After issue/renew, the cert will be copied to this path.
5003 --key-file After issue/renew, the key will be copied to this path.
5004 --ca-file After issue/renew, the intermediate cert will be copied to this path.
5005 --fullchain-file After issue/renew, the fullchain cert will be copied to this path.
3c07f57a 5006
a63b05a9 5007 --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
5008
48d9a8c1 5009 --server SERVER ACME Directory Resource URI. (default: https://acme-v01.api.letsencrypt.org/directory)
a63b05a9 5010 --accountconf Specifies a customized account config file.
635695ec 5011 --home Specifies the home dir for $PROJECT_NAME .
27dbe77f 5012 --cert-home Specifies the home dir to save all the certs, only valid for '--install' command.
5013 --config-home Specifies the home dir to save all the configurations.
635695ec 5014 --useragent Specifies the user agent string. it will be saved for future use too.
b5eb4b90 5015 --accountemail Specifies the account email for registering, Only valid for the '--install' command.
06625071 5016 --accountkey Specifies the account key path, Only valid for the '--install' command.
523c7682 5017 --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
39c8f79f 5018 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
e22bcf7c 5019 --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
6ae0f7f5 5020 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
dcf4f8f6 5021 --listraw Only used for '--list' command, list the certs in raw format.
27dbe77f 5022 --stopRenewOnError, -se Only valid for '--renew-all' command. Stop if one cert has error in renewal.
13d7cae9 5023 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
8f73e241 5024 --ca-bundle Specifies the path to the CA certificate bundle to verify api server's certificate.
13fe54c9 5025 --ca-path Specifies directory containing CA certificates in PEM format, used by wget or curl.
bc96082f 5026 --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 5027 --no-color Do not output color text.
27dbe77f 5028 --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
10afcaca 5029 --csr Specifies the input csr.
b0070f03 5030 --pre-hook Command to be run before obtaining any certificates.
84a6730b 5031 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obtain/renew is success or failed.
b0070f03 5032 --renew-hook Command to be run once for each successfully renewed certificate.
a61fe418 5033 --deploy-hook The hook file to deploy cert
0c9546cc 5034 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
c4b2e582 5035 --always-force-new-domain-key Generate new domain key when renewal. Otherwise, the domain key is not changed by default.
6bf281f9 5036 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
6ae0f7f5 5037 --listen-v4 Force standalone/tls server to listen at ipv4.
5038 --listen-v6 Force standalone/tls server to listen at ipv6.
a746139c 5039 --openssl-bin Specifies a custom openssl bin location.
9b124070 5040 --use-wget Force to use wget, if you have both curl and wget installed.
4c3b3608 5041 "
5042}
5043
52677b0a 5044# nocron
4a0f23e2 5045_installOnline() {
5046 _info "Installing from online archive."
52677b0a 5047 _nocron="$1"
4c2a3841 5048 if [ ! "$BRANCH" ]; then
4a0f23e2 5049 BRANCH="master"
5050 fi
a8df88ab 5051
4a0f23e2 5052 target="$PROJECT/archive/$BRANCH.tar.gz"
5053 _info "Downloading $target"
5054 localname="$BRANCH.tar.gz"
4c2a3841 5055 if ! _get "$target" >$localname; then
df9547ae 5056 _err "Download error."
4a0f23e2 5057 return 1
5058 fi
0bbe6eef 5059 (
4c2a3841 5060 _info "Extracting $localname"
3a3b0dd5 5061 if ! (tar xzf $localname || gtar xzf $localname); then
5062 _err "Extraction error."
5063 exit 1
5064 fi
4c2a3841 5065
5066 cd "$PROJECT_NAME-$BRANCH"
5067 chmod +x $PROJECT_ENTRY
5068 if ./$PROJECT_ENTRY install "$_nocron"; then
5069 _info "Install success!"
5070 fi
5071
5072 cd ..
5073
5074 rm -rf "$PROJECT_NAME-$BRANCH"
5075 rm -f "$localname"
0bbe6eef 5076 )
4a0f23e2 5077}
5078
52677b0a 5079upgrade() {
5080 if (
267f283a 5081 _initpath
5082 export LE_WORKING_DIR
d0b748a4 5083 cd "$LE_WORKING_DIR"
52677b0a 5084 _installOnline "nocron"
4c2a3841 5085 ); then
52677b0a 5086 _info "Upgrade success!"
096d8992 5087 exit 0
52677b0a 5088 else
5089 _err "Upgrade failed!"
096d8992 5090 exit 1
52677b0a 5091 fi
5092}
a63b05a9 5093
5ea6e9c9 5094_processAccountConf() {
4c2a3841 5095 if [ "$_useragent" ]; then
5ea6e9c9 5096 _saveaccountconf "USER_AGENT" "$_useragent"
4c2a3841 5097 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
d0871bda 5098 _saveaccountconf "USER_AGENT" "$USER_AGENT"
5ea6e9c9 5099 fi
4c2a3841 5100
5101 if [ "$_accountemail" ]; then
5ea6e9c9 5102 _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
4c2a3841 5103 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
d0871bda 5104 _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
5ea6e9c9 5105 fi
4c2a3841 5106
a746139c 5107 if [ "$_openssl_bin" ]; then
851fedf7 5108 _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin"
5109 elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
5110 _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
a746139c 5111 fi
5112
4c2a3841 5113 if [ "$_auto_upgrade" ]; then
6bf281f9 5114 _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
4c2a3841 5115 elif [ "$AUTO_UPGRADE" ]; then
6bf281f9 5116 _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
5117 fi
4c2a3841 5118
9b124070 5119 if [ "$_use_wget" ]; then
5120 _saveaccountconf "ACME_USE_WGET" "$_use_wget"
5121 elif [ "$ACME_USE_WGET" ]; then
5122 _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET"
5123 fi
5124
5ea6e9c9 5125}
5126
a63b05a9 5127_process() {
5128 _CMD=""
5129 _domain=""
3f4513b3 5130 _altdomains="$NO_VALUE"
a63b05a9 5131 _webroot=""
bdbf323f 5132 _keylength=""
5133 _accountkeylength=""
5c539af7 5134 _cert_file=""
5135 _key_file=""
5136 _ca_file=""
5137 _fullchain_file=""
4d2f38b0 5138 _reloadcmd=""
a63b05a9 5139 _password=""
635695ec 5140 _accountconf=""
5141 _useragent=""
b5eb4b90 5142 _accountemail=""
5143 _accountkey=""
b2817897 5144 _certhome=""
27dbe77f 5145 _confighome=""
39c8f79f 5146 _httpport=""
e22bcf7c 5147 _tlsport=""
0e38c60d 5148 _dnssleep=""
dcf4f8f6 5149 _listraw=""
cc179731 5150 _stopRenewOnError=""
e3698edd 5151 #_insecure=""
78009539 5152 _ca_bundle=""
2aa75f03 5153 _ca_path=""
c8e9a31e 5154 _nocron=""
43822d37 5155 _ecc=""
10afcaca 5156 _csr=""
b0070f03 5157 _pre_hook=""
5158 _post_hook=""
5159 _renew_hook=""
a61fe418 5160 _deploy_hook=""
5ea6e9c9 5161 _logfile=""
d0871bda 5162 _log=""
0463b5d6 5163 _local_address=""
a73c5b33 5164 _log_level=""
6bf281f9 5165 _auto_upgrade=""
6ae0f7f5 5166 _listen_v4=""
5167 _listen_v6=""
a746139c 5168 _openssl_bin=""
e2edf208 5169 _syslog=""
9b124070 5170 _use_wget=""
98394f99 5171 _server=""
4c2a3841 5172 while [ ${#} -gt 0 ]; do
a63b05a9 5173 case "${1}" in
4c2a3841 5174
5175 --help | -h)
a63b05a9 5176 showhelp
5177 return
5178 ;;
4c2a3841 5179 --version | -v)
a63b05a9 5180 version
5181 return
5182 ;;
4c2a3841 5183 --install)
a63b05a9 5184 _CMD="install"
5185 ;;
4c2a3841 5186 --uninstall)
a63b05a9 5187 _CMD="uninstall"
5188 ;;
4c2a3841 5189 --upgrade)
52677b0a 5190 _CMD="upgrade"
5191 ;;
4c2a3841 5192 --issue)
a63b05a9 5193 _CMD="issue"
5194 ;;
4c2a3841 5195 --deploy)
a61fe418 5196 _CMD="deploy"
5197 ;;
4c2a3841 5198 --signcsr)
10afcaca 5199 _CMD="signcsr"
5200 ;;
4c2a3841 5201 --showcsr)
10afcaca 5202 _CMD="showcsr"
5203 ;;
db7e4bf9 5204 --installcert | -i | --install-cert)
a63b05a9 5205 _CMD="installcert"
5206 ;;
4c2a3841 5207 --renew | -r)
a63b05a9 5208 _CMD="renew"
5209 ;;
db7e4bf9 5210 --renewAll | --renewall | --renew-all)
a63b05a9 5211 _CMD="renewAll"
5212 ;;
4c2a3841 5213 --revoke)
a63b05a9 5214 _CMD="revoke"
5215 ;;
78f0201d 5216 --remove)
5217 _CMD="remove"
5218 ;;
4c2a3841 5219 --list)
6d7eda3e 5220 _CMD="list"
5221 ;;
ee20015d 5222 --installcronjob | --install-cronjob)
a63b05a9 5223 _CMD="installcronjob"
5224 ;;
db7e4bf9 5225 --uninstallcronjob | --uninstall-cronjob)
a63b05a9 5226 _CMD="uninstallcronjob"
5227 ;;
4c2a3841 5228 --cron)
a63b05a9 5229 _CMD="cron"
5230 ;;
4c2a3841 5231 --toPkcs)
a63b05a9 5232 _CMD="toPkcs"
4c2a3841 5233 ;;
4410226d 5234 --toPkcs8)
5235 _CMD="toPkcs8"
342128a4 5236 ;;
0984585d 5237 --createAccountKey | --createaccountkey | -cak | --create-account-key)
a63b05a9 5238 _CMD="createAccountKey"
5239 ;;
0984585d 5240 --createDomainKey | --createdomainkey | -cdk | --create-domain-key)
a63b05a9 5241 _CMD="createDomainKey"
5242 ;;
4c2a3841 5243 --createCSR | --createcsr | -ccr)
a63b05a9 5244 _CMD="createCSR"
5245 ;;
4c2a3841 5246 --deactivate)
0c00e870 5247 _CMD="deactivate"
5248 ;;
ee20015d 5249 --updateaccount | --update-account)
eb59817e 5250 _CMD="updateaccount"
5251 ;;
ee20015d 5252 --registeraccount | --register-account)
eb59817e 5253 _CMD="registeraccount"
5254 ;;
4c2a3841 5255 --domain | -d)
a63b05a9 5256 _dvalue="$2"
4c2a3841 5257
5258 if [ "$_dvalue" ]; then
5259 if _startswith "$_dvalue" "-"; then
ee1737a5 5260 _err "'$_dvalue' is not a valid domain for parameter '$1'"
5261 return 1
5262 fi
4c2a3841 5263 if _is_idn "$_dvalue" && ! _exists idn; then
9774b01b 5264 _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
5265 return 1
5266 fi
4c2a3841 5267
5268 if [ -z "$_domain" ]; then
ee1737a5 5269 _domain="$_dvalue"
a63b05a9 5270 else
4c2a3841 5271 if [ "$_altdomains" = "$NO_VALUE" ]; then
ee1737a5 5272 _altdomains="$_dvalue"
5273 else
5274 _altdomains="$_altdomains,$_dvalue"
5275 fi
a63b05a9 5276 fi
5277 fi
4c2a3841 5278
a63b05a9 5279 shift
5280 ;;
5281
4c2a3841 5282 --force | -f)
a63b05a9 5283 FORCE="1"
5284 ;;
4c2a3841 5285 --staging | --test)
a63b05a9 5286 STAGE="1"
5287 ;;
48d9a8c1 5288 --server)
5289 ACME_DIRECTORY="$2"
98394f99 5290 _server="$ACME_DIRECTORY"
48d9a8c1 5291 export ACME_DIRECTORY
5292 shift
5293 ;;
4c2a3841 5294 --debug)
5295 if [ -z "$2" ] || _startswith "$2" "-"; then
fc6cf4d9 5296 DEBUG="$DEBUG_LEVEL_DEFAULT"
a63b05a9 5297 else
5298 DEBUG="$2"
5299 shift
4c2a3841 5300 fi
a63b05a9 5301 ;;
e6e85b0c 5302 --output-insecure)
5303 export OUTPUT_INSECURE=1
5304 ;;
4c2a3841 5305 --webroot | -w)
a63b05a9 5306 wvalue="$2"
4c2a3841 5307 if [ -z "$_webroot" ]; then
a63b05a9 5308 _webroot="$wvalue"
5309 else
5310 _webroot="$_webroot,$wvalue"
5311 fi
5312 shift
4c2a3841 5313 ;;
5314 --standalone)
3f4513b3 5315 wvalue="$NO_VALUE"
4c2a3841 5316 if [ -z "$_webroot" ]; then
a63b05a9 5317 _webroot="$wvalue"
5318 else
5319 _webroot="$_webroot,$wvalue"
5320 fi
5321 ;;
0e44f587 5322 --stateless)
5323 wvalue="$MODE_STATELESS"
5324 if [ -z "$_webroot" ]; then
5325 _webroot="$wvalue"
5326 else
5327 _webroot="$_webroot,$wvalue"
5328 fi
5329 ;;
4c2a3841 5330 --local-address)
0463b5d6 5331 lvalue="$2"
5332 _local_address="$_local_address$lvalue,"
5333 shift
5334 ;;
4c2a3841 5335 --apache)
a63b05a9 5336 wvalue="apache"
4c2a3841 5337 if [ -z "$_webroot" ]; then
a63b05a9 5338 _webroot="$wvalue"
5339 else
5340 _webroot="$_webroot,$wvalue"
5341 fi
5342 ;;
9d725af6 5343 --nginx)
5344 wvalue="$NGINX"
5345 if [ -z "$_webroot" ]; then
5346 _webroot="$wvalue"
5347 else
5348 _webroot="$_webroot,$wvalue"
5349 fi
5350 ;;
4c2a3841 5351 --tls)
e22bcf7c 5352 wvalue="$W_TLS"
4c2a3841 5353 if [ -z "$_webroot" ]; then
e22bcf7c 5354 _webroot="$wvalue"
5355 else
5356 _webroot="$_webroot,$wvalue"
5357 fi
5358 ;;
4c2a3841 5359 --dns)
a63b05a9 5360 wvalue="dns"
4c2a3841 5361 if ! _startswith "$2" "-"; then
a63b05a9 5362 wvalue="$2"
5363 shift
5364 fi
4c2a3841 5365 if [ -z "$_webroot" ]; then
a63b05a9 5366 _webroot="$wvalue"
5367 else
5368 _webroot="$_webroot,$wvalue"
5369 fi
5370 ;;
4c2a3841 5371 --dnssleep)
0e38c60d 5372 _dnssleep="$2"
5373 Le_DNSSleep="$_dnssleep"
5374 shift
5375 ;;
4c2a3841 5376
5377 --keylength | -k)
a63b05a9 5378 _keylength="$2"
a63b05a9 5379 shift
5380 ;;
4c2a3841 5381 --accountkeylength | -ak)
2ce87fe2 5382 _accountkeylength="$2"
a63b05a9 5383 shift
5384 ;;
5385
5c539af7 5386 --cert-file | --certpath)
5387 _cert_file="$2"
a63b05a9 5388 shift
5389 ;;
5c539af7 5390 --key-file | --keypath)
5391 _key_file="$2"
a63b05a9 5392 shift
5393 ;;
5c539af7 5394 --ca-file | --capath)
5395 _ca_file="$2"
a63b05a9 5396 shift
5397 ;;
5c539af7 5398 --fullchain-file | --fullchainpath)
5399 _fullchain_file="$2"
a63b05a9 5400 shift
5401 ;;
4c2a3841 5402 --reloadcmd | --reloadCmd)
a63b05a9 5403 _reloadcmd="$2"
5404 shift
5405 ;;
4c2a3841 5406 --password)
a63b05a9 5407 _password="$2"
5408 shift
5409 ;;
4c2a3841 5410 --accountconf)
635695ec 5411 _accountconf="$2"
5412 ACCOUNT_CONF_PATH="$_accountconf"
a7b7355d 5413 shift
a63b05a9 5414 ;;
4c2a3841 5415 --home)
a63b05a9 5416 LE_WORKING_DIR="$2"
a7b7355d 5417 shift
a63b05a9 5418 ;;
ee20015d 5419 --certhome | --cert-home)
b2817897 5420 _certhome="$2"
5421 CERT_HOME="$_certhome"
5422 shift
4c2a3841 5423 ;;
27dbe77f 5424 --config-home)
5425 _confighome="$2"
f5b546b3 5426 LE_CONFIG_HOME="$_confighome"
27dbe77f 5427 shift
5428 ;;
4c2a3841 5429 --useragent)
635695ec 5430 _useragent="$2"
5431 USER_AGENT="$_useragent"
5432 shift
5433 ;;
4c2a3841 5434 --accountemail)
b5eb4b90 5435 _accountemail="$2"
5436 ACCOUNT_EMAIL="$_accountemail"
5437 shift
5438 ;;
4c2a3841 5439 --accountkey)
b5eb4b90 5440 _accountkey="$2"
5441 ACCOUNT_KEY_PATH="$_accountkey"
5442 shift
5443 ;;
4c2a3841 5444 --days)
06625071 5445 _days="$2"
5446 Le_RenewalDays="$_days"
5447 shift
5448 ;;
4c2a3841 5449 --httpport)
39c8f79f 5450 _httpport="$2"
5451 Le_HTTPPort="$_httpport"
5452 shift
5453 ;;
4c2a3841 5454 --tlsport)
e22bcf7c 5455 _tlsport="$2"
5456 Le_TLSPort="$_tlsport"
5457 shift
5458 ;;
4c2a3841 5459
5460 --listraw)
dcf4f8f6 5461 _listraw="raw"
4c2a3841 5462 ;;
5463 --stopRenewOnError | --stoprenewonerror | -se)
cc179731 5464 _stopRenewOnError="1"
5465 ;;
4c2a3841 5466 --insecure)
e3698edd 5467 #_insecure="1"
fac1e367 5468 HTTPS_INSECURE="1"
13d7cae9 5469 ;;
4c2a3841 5470 --ca-bundle)
6c4cc357 5471 _ca_bundle="$(_readlink -f "$2")"
78009539
PS
5472 CA_BUNDLE="$_ca_bundle"
5473 shift
5474 ;;
2aa75f03 5475 --ca-path)
5476 _ca_path="$2"
5477 CA_PATH="$_ca_path"
5478 shift
5479 ;;
4c2a3841 5480 --nocron)
c8e9a31e 5481 _nocron="1"
5482 ;;
08b4e1a7 5483 --no-color)
5484 export ACME_NO_COLOR=1
5485 ;;
4c2a3841 5486 --ecc)
43822d37 5487 _ecc="isEcc"
5488 ;;
4c2a3841 5489 --csr)
10afcaca 5490 _csr="$2"
5491 shift
5492 ;;
4c2a3841 5493 --pre-hook)
b0070f03 5494 _pre_hook="$2"
5495 shift
5496 ;;
4c2a3841 5497 --post-hook)
b0070f03 5498 _post_hook="$2"
5499 shift
5500 ;;
4c2a3841 5501 --renew-hook)
b0070f03 5502 _renew_hook="$2"
5503 shift
5504 ;;
4c2a3841 5505 --deploy-hook)
93bce1b2 5506 if [ -z "$2" ] || _startswith "$2" "-"; then
5507 _usage "Please specify a value for '--deploy-hook'"
5508 return 1
5509 fi
5510 _deploy_hook="$_deploy_hook$2,"
a61fe418 5511 shift
5512 ;;
4c2a3841 5513 --ocsp-must-staple | --ocsp)
96db9362 5514 Le_OCSP_Staple="1"
0c9546cc 5515 ;;
c4b2e582 5516 --always-force-new-domain-key)
5517 if [ -z "$2" ] || _startswith "$2" "-"; then
5518 Le_ForceNewDomainKey=1
5519 else
5520 Le_ForceNewDomainKey="$2"
5521 shift
5522 fi
5523 ;;
4c2a3841 5524 --log | --logfile)
d0871bda 5525 _log="1"
5ea6e9c9 5526 _logfile="$2"
4c2a3841 5527 if _startswith "$_logfile" '-'; then
d0871bda 5528 _logfile=""
5529 else
5530 shift
5531 fi
5ea6e9c9 5532 LOG_FILE="$_logfile"
4c2a3841 5533 if [ -z "$LOG_LEVEL" ]; then
a73c5b33 5534 LOG_LEVEL="$DEFAULT_LOG_LEVEL"
5535 fi
5536 ;;
4c2a3841 5537 --log-level)
30bfc2ce 5538 _log_level="$2"
a73c5b33 5539 LOG_LEVEL="$_log_level"
5540 shift
5ea6e9c9 5541 ;;
e2edf208 5542 --syslog)
5543 if ! _startswith "$2" '-'; then
5544 _syslog="$2"
5545 shift
5546 fi
5547 if [ -z "$_syslog" ]; then
fc6cf4d9 5548 _syslog="$SYSLOG_LEVEL_DEFAULT"
e2edf208 5549 fi
5550 ;;
4c2a3841 5551 --auto-upgrade)
6bf281f9 5552 _auto_upgrade="$2"
4c2a3841 5553 if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
6bf281f9 5554 _auto_upgrade="1"
5555 else
5556 shift
5557 fi
5558 AUTO_UPGRADE="$_auto_upgrade"
5559 ;;
4c2a3841 5560 --listen-v4)
6ae0f7f5 5561 _listen_v4="1"
5562 Le_Listen_V4="$_listen_v4"
5563 ;;
4c2a3841 5564 --listen-v6)
6ae0f7f5 5565 _listen_v6="1"
5566 Le_Listen_V6="$_listen_v6"
5567 ;;
a746139c 5568 --openssl-bin)
5569 _openssl_bin="$2"
851fedf7 5570 ACME_OPENSSL_BIN="$_openssl_bin"
7c2e8754 5571 shift
a746139c 5572 ;;
9b124070 5573 --use-wget)
5574 _use_wget="1"
5575 ACME_USE_WGET="1"
5576 ;;
4c2a3841 5577 *)
a63b05a9 5578 _err "Unknown parameter : $1"
5579 return 1
5580 ;;
5581 esac
5582
5583 shift 1
5584 done
5585
4c2a3841 5586 if [ "${_CMD}" != "install" ]; then
5ea6e9c9 5587 __initHome
661f0583 5588 if [ "$_log" ]; then
4c2a3841 5589 if [ -z "$_logfile" ]; then
661f0583 5590 _logfile="$DEFAULT_LOG_FILE"
5591 fi
d0871bda 5592 fi
4c2a3841 5593 if [ "$_logfile" ]; then
5ea6e9c9 5594 _saveaccountconf "LOG_FILE" "$_logfile"
661f0583 5595 LOG_FILE="$_logfile"
5ea6e9c9 5596 fi
a73c5b33 5597
4c2a3841 5598 if [ "$_log_level" ]; then
a73c5b33 5599 _saveaccountconf "LOG_LEVEL" "$_log_level"
5600 LOG_LEVEL="$_log_level"
5601 fi
4c2a3841 5602
e2edf208 5603 if [ "$_syslog" ]; then
5604 if _exists logger; then
5605 if [ "$_syslog" = "0" ]; then
5606 _clearaccountconf "SYS_LOG"
5607 else
5608 _saveaccountconf "SYS_LOG" "$_syslog"
5609 fi
5610 SYS_LOG="$_syslog"
5611 else
5612 _err "The 'logger' command is not found, can not enable syslog."
5613 _clearaccountconf "SYS_LOG"
5614 SYS_LOG=""
5615 fi
5616 fi
5617
5ea6e9c9 5618 _processAccountConf
5619 fi
4c2a3841 5620
9d548d81 5621 _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
4c2a3841 5622
5623 if [ "$DEBUG" ]; then
dcf9cb58 5624 version
98394f99 5625 if [ "$_server" ]; then
5626 _debug "Using server: $_server"
5627 fi
dcf9cb58 5628 fi
a63b05a9 5629
5630 case "${_CMD}" in
27dbe77f 5631 install) install "$_nocron" "$_confighome" ;;
bc96082f 5632 uninstall) uninstall "$_nocron" ;;
52677b0a 5633 upgrade) upgrade ;;
a63b05a9 5634 issue)
5c539af7 5635 issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address"
a63b05a9 5636 ;;
a61fe418 5637 deploy)
5638 deploy "$_domain" "$_deploy_hook" "$_ecc"
5639 ;;
10afcaca 5640 signcsr)
5641 signcsr "$_csr" "$_webroot"
5642 ;;
5643 showcsr)
5644 showcsr "$_csr" "$_domain"
5645 ;;
a63b05a9 5646 installcert)
5c539af7 5647 installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
a63b05a9 5648 ;;
4c2a3841 5649 renew)
43822d37 5650 renew "$_domain" "$_ecc"
a63b05a9 5651 ;;
4c2a3841 5652 renewAll)
cc179731 5653 renewAll "$_stopRenewOnError"
a63b05a9 5654 ;;
4c2a3841 5655 revoke)
43822d37 5656 revoke "$_domain" "$_ecc"
a63b05a9 5657 ;;
78f0201d 5658 remove)
5659 remove "$_domain" "$_ecc"
5660 ;;
4c2a3841 5661 deactivate)
3f4513b3 5662 deactivate "$_domain,$_altdomains"
eb59817e 5663 ;;
4c2a3841 5664 registeraccount)
57e58ce7 5665 registeraccount "$_accountkeylength"
eb59817e 5666 ;;
4c2a3841 5667 updateaccount)
eb59817e 5668 updateaccount
5669 ;;
4c2a3841 5670 list)
dcf4f8f6 5671 list "$_listraw"
6d7eda3e 5672 ;;
27dbe77f 5673 installcronjob) installcronjob "$_confighome" ;;
a63b05a9 5674 uninstallcronjob) uninstallcronjob ;;
5675 cron) cron ;;
4c2a3841 5676 toPkcs)
43822d37 5677 toPkcs "$_domain" "$_password" "$_ecc"
a63b05a9 5678 ;;
4410226d 5679 toPkcs8)
5680 toPkcs8 "$_domain" "$_ecc"
5681 ;;
4c2a3841 5682 createAccountKey)
5fbc47eb 5683 createAccountKey "$_accountkeylength"
a63b05a9 5684 ;;
4c2a3841 5685 createDomainKey)
a63b05a9 5686 createDomainKey "$_domain" "$_keylength"
5687 ;;
4c2a3841 5688 createCSR)
43822d37 5689 createCSR "$_domain" "$_altdomains" "$_ecc"
a63b05a9 5690 ;;
5691
5692 *)
27dbe77f 5693 if [ "$_CMD" ]; then
5694 _err "Invalid command: $_CMD"
5695 fi
4c2a3841 5696 showhelp
a63b05a9 5697 return 1
4c2a3841 5698 ;;
a63b05a9 5699 esac
d3595686 5700 _ret="$?"
4c2a3841 5701 if [ "$_ret" != "0" ]; then
d3595686 5702 return $_ret
5703 fi
4c2a3841 5704
5705 if [ "${_CMD}" = "install" ]; then
5706 if [ "$_log" ]; then
5707 if [ -z "$LOG_FILE" ]; then
d0871bda 5708 LOG_FILE="$DEFAULT_LOG_FILE"
5709 fi
5710 _saveaccountconf "LOG_FILE" "$LOG_FILE"
5ea6e9c9 5711 fi
4c2a3841 5712
5713 if [ "$_log_level" ]; then
a73c5b33 5714 _saveaccountconf "LOG_LEVEL" "$_log_level"
5715 fi
e2edf208 5716
5717 if [ "$_syslog" ]; then
5718 if _exists logger; then
5719 if [ "$_syslog" = "0" ]; then
5720 _clearaccountconf "SYS_LOG"
5721 else
5722 _saveaccountconf "SYS_LOG" "$_syslog"
5723 fi
5724 else
5725 _err "The 'logger' command is not found, can not enable syslog."
5726 _clearaccountconf "SYS_LOG"
5727 SYS_LOG=""
5728 fi
5729 fi
5730
5ea6e9c9 5731 _processAccountConf
b5eb4b90 5732 fi
635695ec 5733
a63b05a9 5734}
5735
4c2a3841 5736if [ "$INSTALLONLINE" ]; then
d1f97fc8 5737 INSTALLONLINE=""
2fbf3991 5738 _installOnline
4a0f23e2 5739 exit
5740fi
4c3b3608 5741
319e0ae3 5742main() {
5743 [ -z "$1" ] && showhelp && return
4c2a3841 5744 if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
319e0ae3 5745}
e69a7c38 5746
aa7b82de 5747main "$@"