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