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