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