7 PROJECT_ENTRY
="acme.sh"
9 PROJECT
="https://github.com/Neilpang/$PROJECT_NAME"
11 DEFAULT_INSTALL_HOME
="$HOME/.$PROJECT_NAME"
13 _WINDOWS_SCHEDULER_NAME
="$PROJECT_NAME.cron"
17 _SUB_FOLDER_NOTIFY
="notify"
18 _SUB_FOLDER_DNSAPI
="dnsapi"
19 _SUB_FOLDER_DEPLOY
="deploy"
21 _SUB_FOLDERS
="$_SUB_FOLDER_DNSAPI $_SUB_FOLDER_DEPLOY $_SUB_FOLDER_NOTIFY"
23 LETSENCRYPT_CA_V1
="https://acme-v01.api.letsencrypt.org/directory"
24 LETSENCRYPT_STAGING_CA_V1
="https://acme-staging.api.letsencrypt.org/directory"
26 LETSENCRYPT_CA_V2
="https://acme-v02.api.letsencrypt.org/directory"
27 LETSENCRYPT_STAGING_CA_V2
="https://acme-staging-v02.api.letsencrypt.org/directory"
29 DEFAULT_CA
=$LETSENCRYPT_CA_V2
30 DEFAULT_STAGING_CA
=$LETSENCRYPT_STAGING_CA_V2
32 DEFAULT_USER_AGENT
="$PROJECT_NAME/$VER ($PROJECT)"
33 DEFAULT_ACCOUNT_EMAIL
=""
35 DEFAULT_ACCOUNT_KEY_LENGTH
=2048
36 DEFAULT_DOMAIN_KEY_LENGTH
=2048
38 DEFAULT_OPENSSL_BIN
="openssl"
40 _OLD_CA_HOST
="https://acme-v01.api.letsencrypt.org"
41 _OLD_STAGE_CA_HOST
="https://acme-staging.api.letsencrypt.org"
45 VTYPE_ALPN
="tls-alpn-01"
47 LOCAL_ANY_ADDRESS
="0.0.0.0"
59 MODE_STATELESS
="stateless"
61 STATE_VERIFIED
="verified_ok"
64 NGINX_START
="#ACME_NGINX_START"
65 NGINX_END
="#ACME_NGINX_END"
67 BEGIN_CSR
="-----BEGIN CERTIFICATE REQUEST-----"
68 END_CSR
="-----END CERTIFICATE REQUEST-----"
70 BEGIN_CERT
="-----BEGIN CERTIFICATE-----"
71 END_CERT
="-----END CERTIFICATE-----"
73 CONTENT_TYPE_JSON
="application/jose+json"
76 B64CONF_START
="__ACME_BASE64__START_"
77 B64CONF_END
="__ACME_BASE64__END_"
80 ECC_SUFFIX
="${ECC_SEP}ecc"
85 DEFAULT_LOG_LEVEL
="$LOG_LEVEL_1"
90 DEBUG_LEVEL_DEFAULT
=$DEBUG_LEVEL_1
93 HIDDEN_VALUE
="[hidden](please add '--output-insecure' to see this value)"
95 SYSLOG_ERROR
="user.error"
96 SYSLOG_INFO
="user.info"
97 SYSLOG_DEBUG
="user.debug"
106 SYSLOG_LEVEL_DEBUG_2
=8
108 SYSLOG_LEVEL_DEBUG_3
=9
110 SYSLOG_LEVEL_DEFAULT
=$SYSLOG_LEVEL_ERROR
114 NOTIFY_LEVEL_DISABLE
=0
119 NOTIFY_LEVEL_DEFAULT
=$NOTIFY_LEVEL_RENEW
124 NOTIFY_MODE_DEFAULT
=$NOTIFY_MODE_BULK
126 _DEBUG_WIKI
="https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh"
128 _PREPARE_LINK
="https://github.com/Neilpang/acme.sh/wiki/Install-preparations"
130 _STATELESS_WIKI
="https://github.com/Neilpang/acme.sh/wiki/Stateless-Mode"
132 _DNS_ALIAS_WIKI
="https://github.com/Neilpang/acme.sh/wiki/DNS-alias-mode"
134 _DNS_MANUAL_WIKI
="https://github.com/Neilpang/acme.sh/wiki/dns-manual-mode"
136 _NOTIFY_WIKI
="https://github.com/Neilpang/acme.sh/wiki/notify"
138 _SUDO_WIKI
="https://github.com/Neilpang/acme.sh/wiki/sudo"
140 _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."
142 _DNS_MANUAL_WARN
="It seems that you are using dns manual mode. please take care: $_DNS_MANUAL_ERR"
144 _DNS_MANUAL_ERROR
="It seems that you are using dns manual mode. Read this link first: $_DNS_MANUAL_WIKI"
152 if [ "${__INTERACTIVE}${ACME_NO_COLOR:-0}" = "10" -o "${ACME_FORCE_COLOR}" = "1" ]; then
153 printf '\033[1;31;32m%b\033[0m' "$1"
160 if [ "${__INTERACTIVE}${ACME_NO_COLOR:-0}" = "10" -o "${ACME_FORCE_COLOR}" = "1" ]; then
161 printf '\033[1;31;40m%b\033[0m' "$1"
169 if [ -z "$NO_TIMESTAMP" ] ||
[ "$NO_TIMESTAMP" = "0" ]; then
170 printf -- "%s" "[$(date)] "
175 printf -- "%s" "$1='$2'"
178 # return the saved exit status
179 return "$_exitstatus"
183 echo "Diagnosis versions: "
184 echo "openssl:$ACME_OPENSSL_BIN"
185 if _exists
"${ACME_OPENSSL_BIN:-openssl}"; then
186 ${ACME_OPENSSL_BIN:-openssl} version
2>&1
188 echo "$ACME_OPENSSL_BIN doesn't exists."
192 if [ "$_APACHECTL" ] && _exists
"$_APACHECTL"; then
195 echo "apache doesn't exists."
199 if _exists
"nginx"; then
202 echo "nginx doesn't exists."
206 if _exists
"socat"; then
209 _debug
"socat doesn't exists."
216 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" = "$SYSLOG_LEVEL_NONE" ]; then
221 if [ -z "$__logger_i" ]; then
222 if _contains
"$(logger --help 2>&1)" "-i"; then
223 __logger_i
="logger -i"
228 $__logger_i -t "$PROJECT_NAME" -p "$_logclass" "$(_printargs "$@
")" >/dev
/null
2>&1
229 return "$_exitstatus"
233 [ -z "$LOG_FILE" ] && return
234 _printargs
"$@" >>"$LOG_FILE"
239 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_INFO" ]; then
240 _syslog
"$SYSLOG_INFO" "$@"
246 _syslog
"$SYSLOG_ERROR" "$@"
248 if [ -z "$NO_TIMESTAMP" ] ||
[ "$NO_TIMESTAMP" = "0" ]; then
249 printf -- "%s" "[$(date)] " >&2
266 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
269 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
270 _syslog
"$SYSLOG_DEBUG" "$@"
272 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
277 #output the sensitive messages
279 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
280 if [ "$OUTPUT_INSECURE" = "1" ]; then
283 _log
"$1" "$HIDDEN_VALUE"
286 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
287 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
289 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
290 if [ "$OUTPUT_INSECURE" = "1" ]; then
293 _printargs
"$1" "$HIDDEN_VALUE" >&2
299 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
302 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
303 _syslog
"$SYSLOG_DEBUG" "$@"
305 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
311 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
312 if [ "$OUTPUT_INSECURE" = "1" ]; then
315 _log
"$1" "$HIDDEN_VALUE"
318 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
319 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
321 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
322 if [ "$OUTPUT_INSECURE" = "1" ]; then
325 _printargs
"$1" "$HIDDEN_VALUE" >&2
331 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
334 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
335 _syslog
"$SYSLOG_DEBUG" "$@"
337 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
343 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
344 if [ "$OUTPUT_INSECURE" = "1" ]; then
347 _log
"$1" "$HIDDEN_VALUE"
350 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
351 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
353 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
354 if [ "$OUTPUT_INSECURE" = "1" ]; then
357 _printargs
"$1" "$HIDDEN_VALUE" >&2
363 # shellcheck disable=SC2018,SC2019
368 # shellcheck disable=SC2018,SC2019
375 echo "$_str" |
grep "^$_sub" >/dev
/null
2>&1
381 echo "$_str" |
grep -- "$_sub\$" >/dev
/null
2>&1
387 echo "$_str" |
grep -- "$_sub" >/dev
/null
2>&1
394 if [ -z "$_field" ]; then
395 _usage
"Usage: str field [sep]"
399 if [ -z "$_sep" ]; then
403 for f
in $
(echo "$_str" |
tr "$_sep" ' '); do
404 if [ "$f" = "$_field" ]; then
405 _debug2
"'$_str' contains '$_field'"
406 return 0 #contains ok
409 _debug2
"'$_str' does not contain '$_field'"
410 return 1 #not contains
419 if [ -z "$_findex" ]; then
420 _usage
"Usage: str field [sep]"
424 if [ -z "$_sep" ]; then
429 while [ "$_ffi" -gt "0" ]; do
430 _fv
="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")"
432 printf -- "%s" "$_fv"
435 _ffi
="$(_math "$_ffi" - 1)"
438 printf -- "%s" "$_str"
444 if [ -z "$cmd" ]; then
445 _usage
"Usage: _exists cmd"
449 if eval type type >/dev
/null
2>&1; then
450 eval type "$cmd" >/dev
/null
2>&1
451 elif command >/dev
/null
2>&1; then
452 command -v "$cmd" >/dev
/null
2>&1
454 which "$cmd" >/dev
/null
2>&1
457 _debug3
"$cmd exists=$ret"
464 printf "%s" "$(($_m_opts))"
496 if [ "$(printf '\x41')" != 'A' ]; then
501 if _exists
xargs && [ "$(printf %s '\\x41' | xargs printf)" = 'A' ]; then
506 if _exists xxd
&& xxd
-r -p 2>/dev
/null
; then
513 _debug2 _URGLY_PRINTF
"$_URGLY_PRINTF"
514 if [ -z "$_URGLY_PRINTF" ]; then
515 if [ "$_ESCAPE_XARGS" ] && _exists
xargs; then
517 echo "$hex" | _upper_case |
sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/g' |
xargs printf
519 for h
in $
(echo "$hex" | _upper_case |
sed 's/\([0-9A-F]\{2\}\)/ \1/g'); do
527 for c
in $
(echo "$hex" | _upper_case |
sed 's/\([0-9A-F]\)/ \1/g'); do
528 if [ -z "$ic" ]; then
533 ic
="$(_h_char_2_dec "$ic")"
534 jc
="$(_h_char_2_dec "$jc")"
535 printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")""%s"
544 _contains "${__OS__:=$(uname -a)}" "solaris" || _contains "${__OS__:=$(uname -a)}" "SunOS"
548 #this can only process ascii chars, should only be used when od command is missing as a backup way.
550 _debug2 "Using _ascii_hex"
554 while [ "$_h_i" -le "$_str_len" ]; do
555 _str_c="$(printf "%s" "$_str" | cut -c "$_h_i")"
556 printf " %02x" "'$_str_c"
557 _h_i="$
(_math
"$_h_i" + 1)"
561 #stdin output hexstr splited by one space
566 od -A n -v -t x1 | tr -s " " | sed 's/ $//' | tr -d "\r\t\n"
567 elif _exists hexdump; then
568 _debug3 "using
hexdump"
569 hexdump -v -e '/1 ""' -e '/1 " %02x
" ""'
570 elif _exists xxd; then
572 xxd -ps -c 20 -i | sed "s
/ 0x
/ /g
" | tr -d ",\n" | tr -s " "
574 _debug3 "using _ascii_hex
"
580 #url encode, no-preserved chars
581 #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
582 #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
584 #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
585 #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
587 #0 1 2 3 4 5 6 7 8 9 - _ . ~
588 #30 31 32 33 34 35 36 37 38 39 2d 5f 2e 7e
592 _hex_str=$(_hex_dump)
593 _debug3 "_url_encode
"
594 _debug3 "_hex_str
" "$_hex_str"
595 for _hex_code in $_hex_str; do
597 case "${_hex_code}" in
801 printf '%%%s' "$_hex_code"
808 _j_str="$
(sed 's/"/\\"/g' |
sed "s/\r/\\r/g")"
809 _debug3 "_json_encode
"
810 _debug3 "_j_str
" "$_j_str"
811 echo "$_j_str" | _hex_dump | _lower_case | sed 's/0a/5c 6e/g' | tr -d ' ' | _h2b | tr -d "\r\n"
818 if [ -z "$filename" ]; then
819 _usage "Usage
:_sed_i options filename
"
822 _debug2 options "$options"
823 if sed -h 2>&1 | grep "\
-i\
[SUFFIX
]" >/dev/null 2>&1; then
824 _debug "Using
sed -i"
825 sed -i "$options" "$filename"
827 _debug "No
-i support
in sed"
828 text="$
(cat "$filename")"
829 echo "$text" | sed "$options" >"$filename"
834 if ! egrep -o "$1" 2>/dev/null; then
835 sed -n 's/.*\('"$1"'\).*/\1/p'
839 #Usage: file startline endline
844 if [ -z "$endline" ]; then
845 _usage "Usage
: file startline endline
"
849 i="$
(grep -n -- "$startline" "$filename" | cut
-d : -f 1)"
851 _err "Can not
find start line
: $startline"
854 i="$
(_math
"$i" + 1)"
857 j="$
(grep -n -- "$endline" "$filename" | cut
-d : -f 1)"
859 _err "Can not
find end line
: $endline"
862 j="$
(_math
"$j" - 1)"
865 sed -n "$i,${j}p
" "$filename"
873 _debug3 "base64 multiline
:'$1'"
874 ${ACME_OPENSSL_BIN:-openssl} base64 -e
876 _debug3 "base64 single line.
"
877 ${ACME_OPENSSL_BIN:-openssl} base64 -e | tr -d '\r\n'
884 ${ACME_OPENSSL_BIN:-openssl} base64 -d -A
886 ${ACME_OPENSSL_BIN:-openssl} base64 -d
893 if [ "$DEBUG" ]; then
894 openssl x509 -noout -text -in "$_cf"
896 openssl x509 -noout -text -in "$_cf" >/dev/null 2>&1
900 #Usage: hashalg [outputhex]
901 #Output Base64-encoded digest
904 if [ -z "$alg" ]; then
905 _usage "Usage
: _digest hashalg
"
911 if [ "$alg" = "sha256
" ] || [ "$alg" = "sha1
" ] || [ "$alg" = "md5
" ]; then
912 if [ "$outputhex" ]; then
913 ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' '
915 ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -binary | _base64
918 _err "$alg is not supported yet
"
924 #Usage: hashalg secret_hex [outputhex]
931 if [ -z "$secret_hex" ]; then
932 _usage "Usage
: _hmac hashalg secret
[outputhex
]"
936 if [ "$alg" = "sha256
" ] || [ "$alg" = "sha1
" ]; then
937 if [ "$outputhex" ]; then
938 (${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 ' '
940 ${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
943 _err "$alg is not supported yet
"
949 #Usage: keyfile hashalg
950 #Output: Base64-encoded signature value
954 if [ -z "$alg" ]; then
955 _usage "Usage
: _sign keyfile hashalg
"
959 _sign_openssl="${ACME_OPENSSL_BIN:-openssl} dgst
-sign $keyfile "
961 if grep "BEGIN RSA PRIVATE KEY
" "$keyfile" >/dev/null 2>&1; then
962 $_sign_openssl -$alg | _base64
963 elif grep "BEGIN EC PRIVATE KEY
" "$keyfile" >/dev/null 2>&1; then
964 if ! _signedECText="$
($_sign_openssl -sha$__ECC_KEY_LEN |
${ACME_OPENSSL_BIN:-openssl} asn1parse
-inform DER
)"; then
965 _err "Sign failed
: $_sign_openssl"
966 _err "Key
file: $keyfile"
967 _err "Key content
:$
(wc -l <"$keyfile") lines
"
970 _debug3 "_signedECText
" "$_signedECText"
971 _ec_r="$
(echo "$_signedECText" | _head_n
2 | _tail_n
1 | cut
-d : -f 4 |
tr -d "\r\n")"
972 _debug3 "_ec_r
" "$_ec_r"
973 _ec_s="$
(echo "$_signedECText" | _head_n
3 | _tail_n
1 | cut
-d : -f 4 |
tr -d "\r\n")"
974 _debug3 "_ec_s
" "$_ec_s"
975 printf "%s
" "$_ec_r$_ec_s" | _h2b | _base64
977 _err "Unknown key
file format.
"
983 #keylength or isEcc flag (empty str => not ecc)
987 if [ -z "$_length" ]; then
991 [ "$_length" != "1024" ] \
992 && [ "$_length" != "2048" ] \
993 && [ "$_length" != "3072" ] \
994 && [ "$_length" != "4096" ] \
995 && [ "$_length" != "8192" ]
998 # _createkey 2048|ec-256 file
1002 _debug2 "_createkey
for file:$f"
1004 if _startswith "$length" "ec-
"; then
1005 length=$(printf "%s
" "$length" | cut -d '-' -f 2-100)
1007 if [ "$length" = "256" ]; then
1008 eccname="prime256v1
"
1010 if [ "$length" = "384" ]; then
1013 if [ "$length" = "521" ]; then
1019 if [ -z "$length" ]; then
1023 _debug "Use length
$length"
1025 if ! touch "$f" >/dev/null 2>&1; then
1026 _f_path="$
(dirname "$f")"
1027 _debug _f_path "$_f_path"
1028 if ! mkdir -p "$_f_path"; then
1029 _err "Can not create path
: $_f_path"
1034 if _isEccKey "$length"; then
1035 _debug "Using ec name
: $eccname"
1036 if _opkey="$
(${ACME_OPENSSL_BIN:-openssl} ecparam
-name "$eccname" -genkey 2>/dev
/null
)"; then
1037 echo "$_opkey" >"$f"
1039 _err "error ecc key name
: $eccname"
1043 _debug "Using RSA
: $length"
1044 if _opkey="$
(${ACME_OPENSSL_BIN:-openssl} genrsa
"$length" 2>/dev
/null
)"; then
1045 echo "$_opkey" >"$f"
1047 _err "error rsa key
: $length"
1052 if [ "$?
" != "0" ]; then
1053 _err "Create key error.
"
1061 _debug2 _is_idn_d "$_is_idn_d"
1062 _idn_temp=$(printf "%s
" "$_is_idn_d" | tr -d '0-9' | tr -d 'a-z' | tr -d 'A-Z' | tr -d '*.,-_')
1063 _debug2 _idn_temp "$_idn_temp"
1068 #aa.com,bb.com,cc.com
1071 if ! _is_idn "$__idn_d"; then
1072 printf "%s
" "$__idn_d"
1076 if _exists idn; then
1077 if _contains "$__idn_d" ','; then
1079 for f in $(echo "$__idn_d" | tr ',' ' '); do
1080 [ -z "$f" ] && continue
1081 if [ -z "$_i_first" ]; then
1086 idn --quiet "$f" | tr -d "\r\n"
1089 idn "$__idn_d" | tr -d "\r\n"
1092 _err "Please
install idn to process IDN names.
"
1096 #_createcsr cn san_list keyfile csrfile conf acmeValidationv1
1104 acmeValidationv1="$6"
1105 _debug2 domain "$domain"
1106 _debug2 domainlist "$domainlist"
1107 _debug2 csrkey "$csrkey"
1109 _debug2 csrconf "$csrconf"
1111 printf "[ req_distinguished_name
]\n[ req
]\ndistinguished_name
= req_distinguished_name
\nreq_extensions
= v3_req
\n[ v3_req
]\n\nkeyUsage
= nonRepudiation
, digitalSignature
, keyEncipherment
" >"$csrconf"
1113 if [ "$acmeValidationv1" ]; then
1114 domainlist="$
(_idn
"$domainlist")"
1115 printf -- "\nsubjectAltName
=DNS
:$domainlist" >>"$csrconf"
1116 elif [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then
1118 _info "Single domain
" "$domain"
1119 printf -- "\nsubjectAltName
=DNS
:$
(_idn
"$domain")" >>"$csrconf"
1121 domainlist="$
(_idn
"$domainlist")"
1122 _debug2 domainlist "$domainlist"
1123 if _contains "$domainlist" ","; then
1124 alt="DNS
:$
(_idn
"$domain"),DNS
:$
(echo "$domainlist" |
sed "s/,,/,/g" |
sed "s/,/,DNS:/g")"
1126 alt="DNS
:$
(_idn
"$domain"),DNS
:$domainlist"
1129 _info "Multi domain
" "$alt"
1130 printf -- "\nsubjectAltName
=$alt" >>"$csrconf"
1132 if [ "$Le_OCSP_Staple" ] || [ "$Le_OCSP_Stable" ]; then
1133 _savedomainconf Le_OCSP_Staple "$Le_OCSP_Staple"
1134 _cleardomainconf Le_OCSP_Stable
1135 printf -- "\nbasicConstraints
= CA
:FALSE
\n1.3.6.1.5.5.7.1.24=DER
:30:03:02:01:05" >>"$csrconf"
1138 if [ "$acmeValidationv1" ]; then
1139 printf "\n1.3.6.1.5.5.7.1.31=critical
,DER
:04:20:${acmeValidationv1}" >>"${csrconf}"
1142 _csr_cn="$
(_idn
"$domain")"
1143 _debug2 _csr_cn "$_csr_cn"
1144 if _contains "$
(uname
-a)" "MINGW
"; then
1145 ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "//CN
=$_csr_cn" -config "$csrconf" -out "$csr"
1147 ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "/CN
=$_csr_cn" -config "$csrconf" -out "$csr"
1151 #_signcsr key csr conf cert
1159 _msg="$
(${ACME_OPENSSL_BIN:-openssl} x509
-req -days 365 -in "$csr" -signkey "$key" -extensions v3_req
-extfile "$conf" -out "$cert" 2>&1)"
1166 _readSubjectFromCSR() {
1168 if [ -z "$_csrfile" ]; then
1169 _usage "_readSubjectFromCSR mycsr.csr
"
1172 ${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'
1176 #echo comma separated domain list
1177 _readSubjectAltNamesFromCSR() {
1179 if [ -z "$_csrfile" ]; then
1180 _usage "_readSubjectAltNamesFromCSR mycsr.csr
"
1184 _csrsubj="$
(_readSubjectFromCSR
"$_csrfile")"
1185 _debug _csrsubj "$_csrsubj"
1187 _dnsAltnames="$
(${ACME_OPENSSL_BIN:-openssl} req
-noout -text -in "$_csrfile" |
grep "^ *DNS:.*" |
tr -d ' \n')"
1188 _debug _dnsAltnames "$_dnsAltnames"
1190 if _contains "$_dnsAltnames," "DNS
:$_csrsubj,"; then
1191 _debug "AltNames contains subject
"
1192 _excapedAlgnames="$
(echo "$_dnsAltnames" |
tr '*' '#')"
1193 _debug _excapedAlgnames "$_excapedAlgnames"
1194 _escapedSubject="$
(echo "$_csrsubj" |
tr '*' '#')"
1195 _debug _escapedSubject "$_escapedSubject"
1196 _dnsAltnames="$
(echo "$_excapedAlgnames," |
sed "s/DNS:$_escapedSubject,//g" |
tr '#' '*' |
sed "s/,\$//g")"
1197 _debug _dnsAltnames "$_dnsAltnames"
1199 _debug "AltNames doesn
't contain subject"
1202 echo "$_dnsAltnames" | sed "s/DNS://g"
1206 _readKeyLengthFromCSR() {
1208 if [ -z "$_csrfile" ]; then
1209 _usage "_readKeyLengthFromCSR mycsr.csr"
1213 _outcsr="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile")"
1214 _debug2 _outcsr "$_outcsr"
1215 if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then
1217 echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' '
1220 _rkl="$(echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1)"
1221 if [ "$_rkl" ]; then
1224 echo "$_outcsr" | tr "\t" " " | _egrep_o "RSA Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1
1232 if _exists "ss"; then
1234 ss -ntpl 2>/dev/null | grep ":$_port "
1238 if _exists "netstat"; then
1239 _debug "Using: netstat"
1240 if netstat -help 2>&1 | grep "\-p proto" >/dev/null; then
1241 #for windows version netstat tool
1242 netstat -an -p tcp | grep "LISTENING" | grep ":$_port "
1244 if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then
1245 netstat -an -p tcp | grep LISTEN | grep ":$_port "
1246 elif netstat -help 2>&1 | grep -- '-P protocol
' >/dev/null; then
1248 netstat -an -P tcp | grep "\.$_port " | grep "LISTEN"
1249 elif netstat -help 2>&1 | grep "\-p" >/dev/null; then
1251 netstat -ntpl | grep ":$_port "
1253 #for busybox (embedded linux; no pid support)
1254 netstat -ntl 2>/dev/null | grep ":$_port "
1263 #outfile key cert cacert [password [name [caname]]]
1273 if [ "$pfxCaname" ]; then
1274 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName" -caname "$pfxCaname"
1275 elif [ "$pfxName" ]; then
1276 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName"
1277 elif [ "$pfxPassword" ]; then
1278 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword"
1280 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca"
1285 #domain [password] [isEcc]
1289 if [ -z "$domain" ]; then
1290 _usage "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]"
1296 _initpath "$domain" "$_isEcc"
1298 _toPkcs "$CERT_PFX_PATH" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$pfxPassword"
1300 if [ "$?" = "0" ]; then
1301 _info "Success, Pfx is exported to: $CERT_PFX_PATH"
1310 if [ -z "$domain" ]; then
1311 _usage "Usage: $PROJECT_ENTRY --toPkcs8 -d domain [--ecc]"
1317 _initpath "$domain" "$_isEcc"
1319 ${ACME_OPENSSL_BIN:-openssl} pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in "$CERT_KEY_PATH" -out "$CERT_PKCS8_PATH"
1321 if [ "$?" = "0" ]; then
1322 _info "Success, $CERT_PKCS8_PATH"
1328 createAccountKey() {
1329 _info "Creating account key"
1330 if [ -z "$1" ]; then
1331 _usage "Usage: $PROJECT_ENTRY --createAccountKey --accountkeylength 2048"
1336 _create_account_key "$length"
1340 _create_account_key() {
1344 if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then
1345 _debug "Use default length $DEFAULT_ACCOUNT_KEY_LENGTH"
1346 length="$DEFAULT_ACCOUNT_KEY_LENGTH"
1349 _debug length "$length"
1353 if [ -s "$ACCOUNT_KEY_PATH" ]; then
1354 _info "Account key exists, skip"
1357 #generate account key
1358 if _createkey "$length" "$ACCOUNT_KEY_PATH"; then
1359 chmod 600 "$ACCOUNT_KEY_PATH"
1360 _info "Create account key ok."
1363 _err "Create account key error."
1372 _info "Creating domain key"
1373 if [ -z "$1" ]; then
1374 _usage "Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]"
1381 if [ -z "$_cdl" ]; then
1382 _debug "Use DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH"
1383 _cdl="$DEFAULT_DOMAIN_KEY_LENGTH"
1386 _initpath "$domain" "$_cdl"
1388 if [ ! -f "$CERT_KEY_PATH" ] || [ ! -s "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$IS_RENEW" ]) || [ "$Le_ForceNewDomainKey" = "1" ]; then
1389 if _createkey "$_cdl" "$CERT_KEY_PATH"; then
1390 _savedomainconf Le_Keylength "$_cdl"
1391 _info "The domain key is here: $(__green $CERT_KEY_PATH)"
1394 _err "Can not create domain key"
1398 if [ "$IS_RENEW" ]; then
1399 _info "Domain key exists, skip"
1402 _err "Domain key exists, do you want to overwrite the key?"
1403 _err "Add '--force', and try again."
1410 # domain domainlist isEcc
1412 _info "Creating csr"
1413 if [ -z "$1" ]; then
1414 _usage "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]"
1422 _initpath "$domain" "$_isEcc"
1424 if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then
1425 _info "CSR exists, skip"
1429 if [ ! -f "$CERT_KEY_PATH" ]; then
1430 _err "The key file is not found: $CERT_KEY_PATH"
1431 _err "Please create the key file first."
1434 _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"
1439 tr '/+' '_-
' | tr -d '= '
1444 if date -u -r "$1" 2>/dev/null; then
1449 if date -u -d@"$1" 2>/dev/null; then
1454 if _exists adb; then
1455 _t_s_a=$(echo "0t${1}=Y" | adb)
1460 if echo "$1" | awk '{ print strftime
("%c", $0); }' 2>/dev/null; then
1466 sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n"
1471 if stat -c '%U
:%G
' "$1" 2>/dev/null; then
1476 if stat -f '%Su
:%Sg
' "$1" 2>/dev/null; then
1480 return 1 #error, 'stat
' not found
1486 if [ -z "$keyfile" ]; then
1487 _usage "Usage: _calcjwk keyfile"
1491 if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then
1492 _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE"
1496 if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
1498 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)
1499 if [ "${#pub_exp}" = "5" ]; then
1502 _debug3 pub_exp "$pub_exp"
1504 e=$(echo "$pub_exp" | _h2b | _base64)
1507 modulus=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2)
1508 _debug3 modulus "$modulus"
1509 n="$(printf "%s" "$modulus" | _h2b | _base64 | _url_replace)"
1512 jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
1515 JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}'
1516 JWK_HEADERPLACE_PART1='{"nonce": "'
1517 JWK_HEADERPLACE_PART2='", "alg": "RS256"'
1518 elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
1520 crv="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
1522 __ECC_KEY_LEN=$(echo "$crv" | cut -d "-" -f 2)
1523 if [ "$__ECC_KEY_LEN" = "521" ]; then
1526 _debug3 __ECC_KEY_LEN "$__ECC_KEY_LEN"
1527 if [ -z "$crv" ]; then
1528 _debug "Let's try ASN1 OID
"
1529 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")"
1530 _debug3 crv_oid "$crv_oid"
1531 case "${crv_oid}" in
1545 _err "ECC oid
: $crv_oid"
1552 pubi="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep -n pub
: | cut
-d : -f 1)"
1553 pubi=$(_math "$pubi" + 1)
1554 _debug3 pubi "$pubi"
1556 pubj="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep -n "ASN1 OID:" | cut
-d : -f 1)"
1557 pubj=$(_math "$pubj" - 1)
1558 _debug3 pubj "$pubj"
1560 pubtext="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
sed -n "$pubi,${pubj}p" |
tr -d " \n\r")"
1561 _debug3 pubtext "$pubtext"
1563 xlen="$
(printf "%s" "$pubtext" |
tr -d ':' |
wc -c)"
1564 xlen=$(_math "$xlen" / 4)
1565 _debug3 xlen "$xlen"
1567 xend=$(_math "$xlen" + 1)
1568 x="$
(printf "%s" "$pubtext" | cut
-d : -f 2-"$xend")"
1571 x64="$
(printf "%s" "$x" |
tr -d : | _h2b | _base64 | _url_replace
)"
1574 xend=$(_math "$xend" + 1)
1575 y="$
(printf "%s" "$pubtext" | cut
-d : -f "$xend"-10000)"
1578 y64="$
(printf "%s" "$y" |
tr -d : | _h2b | _base64 | _url_replace
)"
1581 jwk='{"crv
": "'$crv'", "kty
": "EC
", "x
": "'$x64'", "y
": "'$y64'"}'
1584 JWK_HEADER='{"alg
": "ES
'$__ECC_KEY_LEN'", "jwk
": '$jwk'}'
1585 JWK_HEADERPLACE_PART1='{"nonce
": "'
1586 JWK_HEADERPLACE_PART2='", "alg
": "ES
'$__ECC_KEY_LEN'"'
1588 _err "Only RSA or EC key is supported. keyfile
=$keyfile"
1589 _debug2 "$
(cat "$keyfile")"
1593 _debug3 JWK_HEADER "$JWK_HEADER"
1594 __CACHED_JWK_KEY_FILE="$keyfile"
1602 date -u "+%Y-
%m-
%d
%H
:%M
:%S
"
1606 if _exists mktemp; then
1607 if mktemp 2>/dev/null; then
1609 elif _contains "$
(mktemp
2>&1)" "-t prefix
" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then
1614 if [ -d "/tmp
" ]; then
1615 echo "/tmp
/${PROJECT_NAME}wefADf24sf.$
(_time
).tmp
"
1617 elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then
1618 echo "/$LE_TEMP_DIR/wefADf24sf.$
(_time
).tmp
"
1621 _err "Can not create temp
file.
"
1626 if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then
1627 HTTP_HEADER="$
(_mktemp
)"
1628 _debug2 HTTP_HEADER "$HTTP_HEADER"
1631 if [ "$__HTTP_INITIALIZED" ]; then
1632 if [ "$_ACME_CURL$_ACME_WGET" ]; then
1633 _debug2 "Http already initialized.
"
1638 if [ -z "$_ACME_CURL" ] && _exists "curl
"; then
1639 _ACME_CURL="curl
-L --silent --dump-header $HTTP_HEADER "
1640 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1641 _CURL_DUMP="$
(_mktemp
)"
1642 _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP "
1645 if [ "$CA_PATH" ]; then
1646 _ACME_CURL="$_ACME_CURL --capath $CA_PATH "
1647 elif [ "$CA_BUNDLE" ]; then
1648 _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE "
1651 if _contains "$
(curl
--help 2>&1)" "--globoff"; then
1652 _ACME_CURL="$_ACME_CURL -g "
1656 if [ -z "$_ACME_WGET" ] && _exists "wget
"; then
1657 _ACME_WGET="wget
-q"
1658 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1659 _ACME_WGET="$_ACME_WGET -d "
1661 if [ "$CA_PATH" ]; then
1662 _ACME_WGET="$_ACME_WGET --ca-directory=$CA_PATH "
1663 elif [ "$CA_BUNDLE" ]; then
1664 _ACME_WGET="$_ACME_WGET --ca-certificate=$CA_BUNDLE "
1668 #from wget 1.14: do not skip body on 404 error
1669 if [ "$_ACME_WGET" ] && _contains "$
($_ACME_WGET --help 2>&1)" "--content-on-error"; then
1670 _ACME_WGET="$_ACME_WGET --content-on-error "
1673 __HTTP_INITIALIZED=1
1677 # body url [needbase64] [POST|PUT|DELETE] [ContentType]
1683 _postContentType="$5"
1685 if [ -z "$httpmethod" ]; then
1689 _debug "_post_url
" "$_post_url"
1690 _debug2 "body
" "$body"
1691 _debug2 "_postContentType
" "$_postContentType"
1695 if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
1697 if [ "$HTTPS_INSECURE" ]; then
1698 _CURL="$_CURL --insecure "
1700 if [ "$httpmethod" = "HEAD
" ]; then
1703 _debug "_CURL
" "$_CURL"
1704 if [ "$needbase64" ]; then
1705 if [ "$_postContentType" ]; then
1706 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64
)"
1708 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64
)"
1711 if [ "$_postContentType" ]; then
1712 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")"
1714 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")"
1718 if [ "$_ret" != "0" ]; then
1719 _err "Please refer to https
://curl.haxx.se
/libcurl
/c
/libcurl-errors.html
for error code
: $_ret"
1720 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1721 _err "Here is the curl dump log
:"
1722 _err "$
(cat "$_CURL_DUMP")"
1725 elif [ "$_ACME_WGET" ]; then
1727 if [ "$HTTPS_INSECURE" ]; then
1728 _WGET="$_WGET --no-check-certificate "
1730 _debug "_WGET
" "$_WGET"
1731 if [ "$needbase64" ]; then
1732 if [ "$httpmethod" = "POST
" ]; then
1733 if [ "$_postContentType" ]; then
1734 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64
)"
1736 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
)"
1739 if [ "$_postContentType" ]; then
1740 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64
)"
1742 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
)"
1746 if [ "$httpmethod" = "POST
" ]; then
1747 if [ "$_postContentType" ]; then
1748 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
1750 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")"
1753 if [ "$_postContentType" ]; then
1754 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
1756 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")"
1761 if [ "$_ret" = "8" ]; then
1763 _debug "wget returns
8, the server returns a
'Bad request' response
, lets process the response later.
"
1765 if [ "$_ret" != "0" ]; then
1766 _err "Please refer to https
://www.gnu.org
/software
/wget
/manual
/html_node
/Exit-Status.html
for error code
: $_ret"
1768 _sed_i "s
/^
*//g
" "$HTTP_HEADER"
1771 _err "Neither curl nor wget is found
, can not
do $httpmethod.
"
1773 _debug "_ret
" "$_ret"
1774 printf "%s
" "$response"
1778 # url getheader timeout
1789 if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
1791 if [ "$HTTPS_INSECURE" ]; then
1792 _CURL="$_CURL --insecure "
1795 _CURL="$_CURL --connect-timeout $t"
1797 _debug "_CURL
" "$_CURL"
1798 if [ "$onlyheader" ]; then
1799 $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
1801 $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
1804 if [ "$ret" != "0" ]; then
1805 _err "Please refer to https
://curl.haxx.se
/libcurl
/c
/libcurl-errors.html
for error code
: $ret"
1806 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1807 _err "Here is the curl dump log
:"
1808 _err "$
(cat "$_CURL_DUMP")"
1811 elif [ "$_ACME_WGET" ]; then
1813 if [ "$HTTPS_INSECURE" ]; then
1814 _WGET="$_WGET --no-check-certificate "
1817 _WGET="$_WGET --timeout=$t"
1819 _debug "_WGET
" "$_WGET"
1820 if [ "$onlyheader" ]; then
1821 $_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'
1823 $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url"
1826 if [ "$ret" = "8" ]; then
1828 _debug "wget returns
8, the server returns a
'Bad request' response
, lets process the response later.
"
1830 if [ "$ret" != "0" ]; then
1831 _err "Please refer to https
://www.gnu.org
/software
/wget
/manual
/html_node
/Exit-Status.html
for error code
: $ret"
1835 _err "Neither curl nor wget is found
, can not
do GET.
"
1846 if ! tail -n "$1" 2>/dev/null; then
1852 # url payload needbase64 keyfile
1853 _send_signed_request() {
1858 if [ -z "$keyfile" ]; then
1859 keyfile="$ACCOUNT_KEY_PATH"
1862 _debug payload "$payload"
1864 if ! _calcjwk "$keyfile"; then
1868 __request_conent_type="$CONTENT_TYPE_JSON"
1870 payload64=$(printf "%s
" "$payload" | _base64 | _url_replace)
1871 _debug3 payload64 "$payload64"
1873 MAX_REQUEST_RETRY_TIMES=20
1875 _request_retry_times=0
1876 while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
1877 _request_retry_times=$(_math "$_request_retry_times" + 1)
1878 _debug3 _request_retry_times "$_request_retry_times"
1879 if [ -z "$_CACHED_NONCE" ]; then
1881 if [ "$ACME_NEW_NONCE" ]; then
1882 _debug2 "Get nonce with HEAD. ACME_NEW_NONCE
" "$ACME_NEW_NONCE"
1883 nonceurl="$ACME_NEW_NONCE"
1884 if _post "" "$nonceurl" "" "HEAD
" "$__request_conent_type"; then
1885 _headers="$
(cat "$HTTP_HEADER")"
1886 _debug2 _headers "$_headers"
1887 _CACHED_NONCE="$
(echo "$_headers" |
grep -i "Replay-Nonce:" | _head_n
1 |
tr -d "\r\n " | cut
-d ':' -f 2)"
1890 if [ -z "$_CACHED_NONCE" ]; then
1891 _debug2 "Get nonce with GET. ACME_DIRECTORY
" "$ACME_DIRECTORY"
1892 nonceurl="$ACME_DIRECTORY"
1893 _headers="$
(_get
"$nonceurl" "onlyheader")"
1894 _debug2 _headers "$_headers"
1895 _CACHED_NONCE="$
(echo "$_headers" |
grep -i "Replay-Nonce:" | _head_n
1 |
tr -d "\r\n " | cut
-d ':' -f 2)"
1897 if [ -z "$_CACHED_NONCE" ] && [ "$ACME_NEW_NONCE" ]; then
1898 _debug2 "Get nonce with GET. ACME_NEW_NONCE
" "$ACME_NEW_NONCE"
1899 nonceurl="$ACME_NEW_NONCE"
1900 _headers="$
(_get
"$nonceurl" "onlyheader")"
1901 _debug2 _headers "$_headers"
1902 _CACHED_NONCE="$
(echo "$_headers" |
grep -i "Replay-Nonce:" | _head_n
1 |
tr -d "\r\n " | cut
-d ':' -f 2)"
1904 _debug2 _CACHED_NONCE "$_CACHED_NONCE"
1905 if [ "$?
" != "0" ]; then
1906 _err "Can not connect to
$nonceurl to get nonce.
"
1910 _debug2 "Use _CACHED_NONCE
" "$_CACHED_NONCE"
1912 nonce="$_CACHED_NONCE"
1913 _debug2 nonce "$nonce"
1914 if [ -z "$nonce" ]; then
1915 _info "Could not get nonce
, let's try again."
1919 if [ "$ACME_VERSION" = "2" ]; then
1920 if [ "$url" = "$ACME_NEW_ACCOUNT" ] || [ "$url" = "$ACME_REVOKE_CERT" ]; then
1921 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}'
1923 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"kid\": \"${ACCOUNT_URL}\""'}'
1926 protected
="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}'
1928 _debug3 protected
"$protected"
1930 protected64
="$(printf "%s
" "$protected" | _base64 | _url_replace)"
1931 _debug3 protected64
"$protected64"
1933 if ! _sig_t
="$(printf "%s
" "$protected64.
$payload64" | _sign "$keyfile" "sha256
")"; then
1934 _err
"Sign request failed."
1937 _debug3 _sig_t
"$_sig_t"
1939 sig
="$(printf "%s
" "$_sig_t" | _url_replace)"
1942 body
="{\"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
1943 _debug3 body
"$body"
1945 response
="$(_post "$body" "$url" "$needbase64" "POST
" "$__request_conent_type")"
1948 if [ "$?" != "0" ]; then
1949 _err
"Can not post to $url"
1953 responseHeaders
="$(cat "$HTTP_HEADER")"
1954 _debug2 responseHeaders
"$responseHeaders"
1956 code
="$(grep "^HTTP
" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")"
1959 _debug2 original
"$response"
1960 if echo "$responseHeaders" |
grep -i "Content-Type: application/json" >/dev
/null
2>&1; then
1961 response
="$(echo "$response" | _normalizeJson)"
1963 _debug2 response
"$response"
1965 _CACHED_NONCE
="$(echo "$responseHeaders" | grep -i "Replay-Nonce
:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
1967 if ! _startswith
"$code" "2"; then
1969 if [ "$needbase64" ]; then
1970 _body
="$(echo "$_body" | _dbase64 multiline)"
1971 _debug3 _body
"$_body"
1974 if _contains
"$_body" "JWS has invalid anti-replay nonce" || _contains
"$_body" "JWS has an invalid anti-replay nonce"; then
1975 _info
"It seems the CA server is busy now, let's wait and retry. Sleeping $_sleep_retry_sec seconds."
1977 _sleep
$_sleep_retry_sec
1986 #setopt "file" "opt" "=" "value" [";"]
1993 if [ -z "$__opt" ]; then
1994 _usage usage
: _setopt
'"file" "opt" "=" "value" [";"]'
1997 if [ ! -f "$__conf" ]; then
2001 if grep -n "^$__opt$__sep" "$__conf" >/dev
/null
; then
2003 if _contains
"$__val" "&"; then
2004 __val
="$(echo "$__val" | sed 's/&/\\&/g')"
2006 text
="$(cat "$__conf")"
2007 printf -- "%s\n" "$text" |
sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
2009 elif grep -n "^#$__opt$__sep" "$__conf" >/dev
/null
; then
2010 if _contains
"$__val" "&"; then
2011 __val
="$(echo "$__val" | sed 's/&/\\&/g')"
2013 text
="$(cat "$__conf")"
2014 printf -- "%s\n" "$text" |
sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
2018 echo "$__opt$__sep$__val$__end" >>"$__conf"
2020 _debug3
"$(grep -n "^
$__opt$__sep" "$__conf")"
2023 #_save_conf file key value base64encode
2030 if [ "$_sdvalue" ] && [ "$_b64encode" ]; then
2031 _sdvalue
="${B64CONF_START}$(printf "%s" "${_sdvalue}" | _base64)${B64CONF_END}"
2033 if [ "$_s_c_f" ]; then
2034 _setopt
"$_s_c_f" "$_sdkey" "=" "'$_sdvalue'"
2036 _err
"config file is empty, can not save $_sdkey=$_sdvalue"
2040 #_clear_conf file key
2044 if [ "$_c_c_f" ]; then
2045 _conf_data
="$(cat "$_c_c_f")"
2046 echo "$_conf_data" |
sed "s/^$_sdkey *=.*$//" >"$_c_c_f"
2048 _err
"config file is empty, can not clear"
2052 #_read_conf file key
2056 if [ -f "$_r_c_f" ]; then
2058 eval "$
(grep "^$_sdkey *=" "$_r_c_f")"
2059 eval "printf \"%s
\" \"\$
$_sdkey\""
2061 if _startswith
"$_sdv" "${B64CONF_START}" && _endswith
"$_sdv" "${B64CONF_END}"; then
2062 _sdv
="$(echo "$_sdv" | sed "s
/${B64CONF_START}//" | sed "s
/${B64CONF_END}//" | _dbase64)"
2066 _debug
"config file is empty, can not read $_sdkey"
2070 #_savedomainconf key value base64encode
2071 #save to domain.conf
2073 _save_conf
"$DOMAIN_CONF" "$@"
2076 #_cleardomainconf key
2077 _cleardomainconf
() {
2078 _clear_conf
"$DOMAIN_CONF" "$1"
2081 #_readdomainconf key
2083 _read_conf
"$DOMAIN_CONF" "$1"
2086 #key value base64encode
2088 _savedomainconf
"SAVED_$1" "$2" "$3"
2090 _cleardomainconf
"$1"
2096 _rac_value
="$(eval echo \$"$_rac_key")"
2097 if [ "$_rac_value" ]; then
2098 if _startswith
"$_rac_value" '"' && _endswith
"$_rac_value" '"'; then
2099 _debug2
"trim quotation marks"
2100 eval "export $_rac_key=$_rac_value"
2102 return 0 # do nothing
2104 _saved
=$
(_readdomainconf
"SAVED_$_rac_key")
2105 eval "export $_rac_key=$_saved"
2108 #_saveaccountconf key value base64encode
2109 _saveaccountconf
() {
2110 _save_conf
"$ACCOUNT_CONF_PATH" "$@"
2113 #key value base64encode
2114 _saveaccountconf_mutable
() {
2115 _save_conf
"$ACCOUNT_CONF_PATH" "SAVED_$1" "$2" "$3"
2117 _clearaccountconf
"$1"
2121 _readaccountconf
() {
2122 _read_conf
"$ACCOUNT_CONF_PATH" "$1"
2126 _readaccountconf_mutable
() {
2128 _readaccountconf
"SAVED_$_rac_key"
2131 #_clearaccountconf key
2132 _clearaccountconf
() {
2133 _clear_conf
"$ACCOUNT_CONF_PATH" "$1"
2136 #_savecaconf key value
2138 _save_conf
"$CA_CONF" "$1" "$2"
2143 _read_conf
"$CA_CONF" "$1"
2146 #_clearaccountconf key
2148 _clear_conf
"$CA_CONF" "$1"
2151 # content localaddress
2155 _debug
"content" "$content"
2156 _debug
"ncaddr" "$ncaddr"
2158 _debug
"startserver: $$"
2160 _debug Le_HTTPPort
"$Le_HTTPPort"
2161 _debug Le_Listen_V4
"$Le_Listen_V4"
2162 _debug Le_Listen_V6
"$Le_Listen_V6"
2165 if [ "$Le_Listen_V4" ]; then
2167 elif [ "$Le_Listen_V6" ]; then
2171 if [ "$DEBUG" ] && [ "$DEBUG" -gt "1" ]; then
2175 SOCAT_OPTIONS
=TCP-LISTEN
:$Le_HTTPPort,crlf
,reuseaddr
,fork
2177 #Adding bind to local-address
2178 if [ "$ncaddr" ]; then
2179 SOCAT_OPTIONS
="$SOCAT_OPTIONS,bind=${ncaddr}"
2182 _content_len
="$(printf "%s
" "$content" | wc -c)"
2183 _debug _content_len
"$_content_len"
2184 _debug
"_NC" "$_NC $SOCAT_OPTIONS"
2185 $_NC $SOCAT_OPTIONS SYSTEM
:"sleep 1; \
2186 echo 'HTTP/1.0 200 OK'; \
2187 echo 'Content-Length\: $_content_len'; \
2189 printf -- '$content';" &
2196 if [ -z "$pid" ]; then
2207 if [ "$__INTERACTIVE" ]; then
2208 _sleep_c
="$_sleep_sec"
2209 while [ "$_sleep_c" -ge "0" ]; do
2212 _sleep_c
="$(_math "$_sleep_c" - 1)"
2221 # _starttlsserver san_a san_b port content _ncaddr acmeValidationv1
2223 _info
"Starting tls server."
2229 acmeValidationv1
="$6"
2231 _debug san_a
"$san_a"
2232 _debug san_b
"$san_b"
2234 _debug acmeValidationv1
"$acmeValidationv1"
2237 if ! _createkey
"2048" "$TLS_KEY"; then
2238 _err
"Create tls validation key error."
2244 if [ "$san_b" ]; then
2247 if ! _createcsr
"tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$acmeValidationv1"; then
2248 _err
"Create tls validation csr error."
2253 if ! _signcsr
"$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then
2254 _err
"Create tls validation cert error."
2258 __S_OPENSSL
="${ACME_OPENSSL_BIN:-openssl} s_server -www -cert $TLS_CERT -key $TLS_KEY "
2259 if [ "$opaddr" ]; then
2260 __S_OPENSSL
="$__S_OPENSSL -accept $opaddr:$port"
2262 __S_OPENSSL
="$__S_OPENSSL -accept $port"
2265 _debug Le_Listen_V4
"$Le_Listen_V4"
2266 _debug Le_Listen_V6
"$Le_Listen_V6"
2267 if [ "$Le_Listen_V4" ]; then
2268 __S_OPENSSL
="$__S_OPENSSL -4"
2269 elif [ "$Le_Listen_V6" ]; then
2270 __S_OPENSSL
="$__S_OPENSSL -6"
2273 if [ "$acmeValidationv1" ]; then
2274 __S_OPENSSL
="$__S_OPENSSL -alpn acme-tls/1"
2277 _debug
"$__S_OPENSSL"
2278 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
2279 $__S_OPENSSL -tlsextdebug &
2281 $__S_OPENSSL >/dev
/null
2>&1 &
2286 _debug serverproc
"$serverproc"
2292 if ! readlink
-f "$_rf" 2>/dev
/null
; then
2293 if _startswith
"$_rf" "/"; then
2297 echo "$(pwd)/$_rf" | _conapath
2306 if [ -z "$_SCRIPT_HOME" ]; then
2307 if _exists readlink
&& _exists
dirname; then
2308 _debug
"Lets find script dir."
2309 _debug
"_SCRIPT_" "$_SCRIPT_"
2310 _script
="$(_readlink "$_SCRIPT_")"
2311 _debug
"_script" "$_script"
2312 _script_home
="$(dirname "$_script")"
2313 _debug
"_script_home" "$_script_home"
2314 if [ -d "$_script_home" ]; then
2315 _SCRIPT_HOME
="$_script_home"
2317 _err
"It seems the script home is not correct:$_script_home"
2322 # if [ -z "$LE_WORKING_DIR" ]; then
2323 # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then
2324 # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME"
2325 # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
2327 # LE_WORKING_DIR="$_SCRIPT_HOME"
2331 if [ -z "$LE_WORKING_DIR" ]; then
2332 _debug
"Using default home:$DEFAULT_INSTALL_HOME"
2333 LE_WORKING_DIR
="$DEFAULT_INSTALL_HOME"
2335 export LE_WORKING_DIR
2337 if [ -z "$LE_CONFIG_HOME" ]; then
2338 LE_CONFIG_HOME
="$LE_WORKING_DIR"
2340 _debug
"Using config home:$LE_CONFIG_HOME"
2341 export LE_CONFIG_HOME
2343 _DEFAULT_ACCOUNT_CONF_PATH
="$LE_CONFIG_HOME/account.conf"
2345 if [ -z "$ACCOUNT_CONF_PATH" ]; then
2346 if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
2347 .
"$_DEFAULT_ACCOUNT_CONF_PATH"
2351 if [ -z "$ACCOUNT_CONF_PATH" ]; then
2352 ACCOUNT_CONF_PATH
="$_DEFAULT_ACCOUNT_CONF_PATH"
2355 DEFAULT_LOG_FILE
="$LE_CONFIG_HOME/$PROJECT_NAME.log"
2357 DEFAULT_CA_HOME
="$LE_CONFIG_HOME/ca"
2359 if [ -z "$LE_TEMP_DIR" ]; then
2360 LE_TEMP_DIR
="$LE_CONFIG_HOME/tmp"
2366 _api_server
="${1:-$ACME_DIRECTORY}"
2367 _debug
"_init api for server: $_api_server"
2369 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2370 response
=$
(_get
"$_api_server")
2371 if [ "$?" != "0" ]; then
2372 _debug2
"response" "$response"
2373 _err
"Can not init api."
2376 _debug2
"response" "$response"
2378 ACME_KEY_CHANGE
=$
(echo "$response" | _egrep_o
'key-change" *: *"[^"]*"' | cut
-d '"' -f 3)
2379 if [ -z "$ACME_KEY_CHANGE" ]; then
2380 ACME_KEY_CHANGE
=$
(echo "$response" | _egrep_o
'keyChange" *: *"[^"]*"' | cut
-d '"' -f 3)
2382 export ACME_KEY_CHANGE
2384 ACME_NEW_AUTHZ
=$
(echo "$response" | _egrep_o
'new-authz" *: *"[^"]*"' | cut
-d '"' -f 3)
2385 if [ -z "$ACME_NEW_AUTHZ" ]; then
2386 ACME_NEW_AUTHZ
=$
(echo "$response" | _egrep_o
'newAuthz" *: *"[^"]*"' | cut
-d '"' -f 3)
2388 export ACME_NEW_AUTHZ
2390 ACME_NEW_ORDER
=$
(echo "$response" | _egrep_o
'new-cert" *: *"[^"]*"' | cut
-d '"' -f 3)
2391 ACME_NEW_ORDER_RES
="new-cert"
2392 if [ -z "$ACME_NEW_ORDER" ]; then
2393 ACME_NEW_ORDER
=$
(echo "$response" | _egrep_o
'new-order" *: *"[^"]*"' | cut
-d '"' -f 3)
2394 ACME_NEW_ORDER_RES
="new-order"
2395 if [ -z "$ACME_NEW_ORDER" ]; then
2396 ACME_NEW_ORDER
=$
(echo "$response" | _egrep_o
'newOrder" *: *"[^"]*"' | cut
-d '"' -f 3)
2399 export ACME_NEW_ORDER
2400 export ACME_NEW_ORDER_RES
2402 ACME_NEW_ACCOUNT
=$
(echo "$response" | _egrep_o
'new-reg" *: *"[^"]*"' | cut
-d '"' -f 3)
2403 ACME_NEW_ACCOUNT_RES
="new-reg"
2404 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2405 ACME_NEW_ACCOUNT
=$
(echo "$response" | _egrep_o
'new-account" *: *"[^"]*"' | cut
-d '"' -f 3)
2406 ACME_NEW_ACCOUNT_RES
="new-account"
2407 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2408 ACME_NEW_ACCOUNT
=$
(echo "$response" | _egrep_o
'newAccount" *: *"[^"]*"' | cut
-d '"' -f 3)
2409 if [ "$ACME_NEW_ACCOUNT" ]; then
2410 export ACME_VERSION
=2
2414 export ACME_NEW_ACCOUNT
2415 export ACME_NEW_ACCOUNT_RES
2417 ACME_REVOKE_CERT
=$
(echo "$response" | _egrep_o
'revoke-cert" *: *"[^"]*"' | cut
-d '"' -f 3)
2418 if [ -z "$ACME_REVOKE_CERT" ]; then
2419 ACME_REVOKE_CERT
=$
(echo "$response" | _egrep_o
'revokeCert" *: *"[^"]*"' | cut
-d '"' -f 3)
2421 export ACME_REVOKE_CERT
2423 ACME_NEW_NONCE
=$
(echo "$response" | _egrep_o
'new-nonce" *: *"[^"]*"' | cut
-d '"' -f 3)
2424 if [ -z "$ACME_NEW_NONCE" ]; then
2425 ACME_NEW_NONCE
=$
(echo "$response" | _egrep_o
'newNonce" *: *"[^"]*"' | cut
-d '"' -f 3)
2427 export ACME_NEW_NONCE
2429 ACME_AGREEMENT
=$
(echo "$response" | _egrep_o
'terms-of-service" *: *"[^"]*"' | cut
-d '"' -f 3)
2430 if [ -z "$ACME_AGREEMENT" ]; then
2431 ACME_AGREEMENT
=$
(echo "$response" | _egrep_o
'termsOfService" *: *"[^"]*"' | cut
-d '"' -f 3)
2433 export ACME_AGREEMENT
2435 _debug
"ACME_KEY_CHANGE" "$ACME_KEY_CHANGE"
2436 _debug
"ACME_NEW_AUTHZ" "$ACME_NEW_AUTHZ"
2437 _debug
"ACME_NEW_ORDER" "$ACME_NEW_ORDER"
2438 _debug
"ACME_NEW_ACCOUNT" "$ACME_NEW_ACCOUNT"
2439 _debug
"ACME_REVOKE_CERT" "$ACME_REVOKE_CERT"
2440 _debug
"ACME_AGREEMENT" "$ACME_AGREEMENT"
2441 _debug
"ACME_NEW_NONCE" "$ACME_NEW_NONCE"
2442 _debug
"ACME_VERSION" "$ACME_VERSION"
2447 #[domain] [keylength or isEcc flag]
2454 if [ -f "$ACCOUNT_CONF_PATH" ]; then
2455 .
"$ACCOUNT_CONF_PATH"
2458 if [ "$ACME_IN_CRON" ]; then
2459 if [ ! "$_USER_PATH_EXPORTED" ]; then
2460 _USER_PATH_EXPORTED
=1
2461 export PATH
="$USER_PATH:$PATH"
2465 if [ -z "$CA_HOME" ]; then
2466 CA_HOME
="$DEFAULT_CA_HOME"
2469 if [ "$ACME_VERSION" = "2" ]; then
2470 DEFAULT_CA
="$LETSENCRYPT_CA_V2"
2471 DEFAULT_STAGING_CA
="$LETSENCRYPT_STAGING_CA_V2"
2474 if [ -z "$ACME_DIRECTORY" ]; then
2475 if [ -z "$STAGE" ]; then
2476 ACME_DIRECTORY
="$DEFAULT_CA"
2478 ACME_DIRECTORY
="$DEFAULT_STAGING_CA"
2479 _info
"Using stage ACME_DIRECTORY: $ACME_DIRECTORY"
2483 _debug ACME_DIRECTORY
"$ACME_DIRECTORY"
2484 _ACME_SERVER_HOST
="$(echo "$ACME_DIRECTORY" | cut -d : -f 2 | tr -s / | cut -d / -f 2)"
2485 _debug2
"_ACME_SERVER_HOST" "$_ACME_SERVER_HOST"
2487 CA_DIR
="$CA_HOME/$_ACME_SERVER_HOST"
2489 _DEFAULT_CA_CONF
="$CA_DIR/ca.conf"
2491 if [ -z "$CA_CONF" ]; then
2492 CA_CONF
="$_DEFAULT_CA_CONF"
2494 _debug3 CA_CONF
"$CA_CONF"
2496 if [ -f "$CA_CONF" ]; then
2500 if [ -z "$ACME_DIR" ]; then
2501 ACME_DIR
="/home/.acme"
2504 if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
2505 APACHE_CONF_BACKUP_DIR
="$LE_CONFIG_HOME"
2508 if [ -z "$USER_AGENT" ]; then
2509 USER_AGENT
="$DEFAULT_USER_AGENT"
2512 if [ -z "$HTTP_HEADER" ]; then
2513 HTTP_HEADER
="$LE_CONFIG_HOME/http.header"
2516 _OLD_ACCOUNT_KEY
="$LE_WORKING_DIR/account.key"
2517 _OLD_ACCOUNT_JSON
="$LE_WORKING_DIR/account.json"
2519 _DEFAULT_ACCOUNT_KEY_PATH
="$CA_DIR/account.key"
2520 _DEFAULT_ACCOUNT_JSON_PATH
="$CA_DIR/account.json"
2521 if [ -z "$ACCOUNT_KEY_PATH" ]; then
2522 ACCOUNT_KEY_PATH
="$_DEFAULT_ACCOUNT_KEY_PATH"
2525 if [ -z "$ACCOUNT_JSON_PATH" ]; then
2526 ACCOUNT_JSON_PATH
="$_DEFAULT_ACCOUNT_JSON_PATH"
2529 _DEFAULT_CERT_HOME
="$LE_CONFIG_HOME"
2530 if [ -z "$CERT_HOME" ]; then
2531 CERT_HOME
="$_DEFAULT_CERT_HOME"
2534 if [ -z "$ACME_OPENSSL_BIN" ] ||
[ ! -f "$ACME_OPENSSL_BIN" ] ||
[ ! -x "$ACME_OPENSSL_BIN" ]; then
2535 ACME_OPENSSL_BIN
="$DEFAULT_OPENSSL_BIN"
2538 if [ -z "$domain" ]; then
2542 if [ -z "$DOMAIN_PATH" ]; then
2543 domainhome
="$CERT_HOME/$domain"
2544 domainhomeecc
="$CERT_HOME/$domain$ECC_SUFFIX"
2546 DOMAIN_PATH
="$domainhome"
2548 if _isEccKey
"$_ilength"; then
2549 DOMAIN_PATH
="$domainhomeecc"
2551 if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
2552 _info
"The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert."
2555 _debug DOMAIN_PATH
"$DOMAIN_PATH"
2558 if [ -z "$DOMAIN_BACKUP_PATH" ]; then
2559 DOMAIN_BACKUP_PATH
="$DOMAIN_PATH/backup"
2562 if [ -z "$DOMAIN_CONF" ]; then
2563 DOMAIN_CONF
="$DOMAIN_PATH/$domain.conf"
2566 if [ -z "$DOMAIN_SSL_CONF" ]; then
2567 DOMAIN_SSL_CONF
="$DOMAIN_PATH/$domain.csr.conf"
2570 if [ -z "$CSR_PATH" ]; then
2571 CSR_PATH
="$DOMAIN_PATH/$domain.csr"
2573 if [ -z "$CERT_KEY_PATH" ]; then
2574 CERT_KEY_PATH
="$DOMAIN_PATH/$domain.key"
2576 if [ -z "$CERT_PATH" ]; then
2577 CERT_PATH
="$DOMAIN_PATH/$domain.cer"
2579 if [ -z "$CA_CERT_PATH" ]; then
2580 CA_CERT_PATH
="$DOMAIN_PATH/ca.cer"
2582 if [ -z "$CERT_FULLCHAIN_PATH" ]; then
2583 CERT_FULLCHAIN_PATH
="$DOMAIN_PATH/fullchain.cer"
2585 if [ -z "$CERT_PFX_PATH" ]; then
2586 CERT_PFX_PATH
="$DOMAIN_PATH/$domain.pfx"
2588 if [ -z "$CERT_PKCS8_PATH" ]; then
2589 CERT_PKCS8_PATH
="$DOMAIN_PATH/$domain.pkcs8"
2592 if [ -z "$TLS_CONF" ]; then
2593 TLS_CONF
="$DOMAIN_PATH/tls.validation.conf"
2595 if [ -z "$TLS_CERT" ]; then
2596 TLS_CERT
="$DOMAIN_PATH/tls.validation.cert"
2598 if [ -z "$TLS_KEY" ]; then
2599 TLS_KEY
="$DOMAIN_PATH/tls.validation.key"
2601 if [ -z "$TLS_CSR" ]; then
2602 TLS_CSR
="$DOMAIN_PATH/tls.validation.csr"
2608 if [ -z "$_EXEC_TEMP_ERR" ]; then
2609 _EXEC_TEMP_ERR
="$(_mktemp)"
2612 if [ "$_EXEC_TEMP_ERR" ]; then
2613 eval "$@ 2>>$_EXEC_TEMP_ERR"
2620 [ "$_EXEC_TEMP_ERR" ] && _err
"$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR"
2624 _APACHECTL
="apachectl"
2625 if ! _exists apachectl
; then
2626 if _exists apache2ctl
; then
2627 _APACHECTL
="apache2ctl"
2629 _err
"'apachectl not found. It seems that apache is not installed, or you are not root user.'"
2630 _err
"Please use webroot mode to try again."
2635 if ! _exec
$_APACHECTL -V >/dev
/null
; then
2640 if [ "$APACHE_HTTPD_CONF" ]; then
2641 _saveaccountconf APACHE_HTTPD_CONF
"$APACHE_HTTPD_CONF"
2642 httpdconf
="$APACHE_HTTPD_CONF"
2643 httpdconfname
="$(basename "$httpdconfname")"
2645 httpdconfname
="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
2646 _debug httpdconfname "$httpdconfname"
2648 if [ -z "$httpdconfname" ]; then
2649 _err "Can not read apache config file."
2653 if _startswith "$httpdconfname" '/'; then
2654 httpdconf="$httpdconfname"
2655 httpdconfname="$(basename "$httpdconfname")"
2657 httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
2658 _debug httpdroot
"$httpdroot"
2659 httpdconf
="$httpdroot/$httpdconfname"
2660 httpdconfname
="$(basename "$httpdconfname")"
2663 _debug httpdconf
"$httpdconf"
2664 _debug httpdconfname
"$httpdconfname"
2665 if [ ! -f "$httpdconf" ]; then
2666 _err
"Apache Config file not found" "$httpdconf"
2673 if [ -z "$usingApache" ]; then
2677 if ! _apachePath
; then
2681 if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
2682 _debug
"No config file to restore."
2686 cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
2687 _debug
"Restored: $httpdconf."
2688 if ! _exec
$_APACHECTL -t; then
2690 _err
"Sorry, restore apache config error, please contact me."
2693 _debug
"Restored successfully."
2694 rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
2700 if ! _apachePath
; then
2704 #test the conf first
2705 _info
"Checking if there is an error in the apache config file before starting."
2707 if ! _exec
"$_APACHECTL" -t >/dev
/null
; then
2709 _err
"The apache config file has error, please fix it first, then try again."
2710 _err
"Don't worry, there is nothing changed to your system."
2717 _debug
"Backup apache config file" "$httpdconf"
2718 if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
2719 _err
"Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
2720 _err
"This might be a bug of $PROJECT_NAME , please report issue: $PROJECT"
2723 _info
"JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
2724 _info
"In case there is an error that can not be restored automatically, you may try restore it yourself."
2725 _info
"The backup file will be deleted on success, just forget it."
2729 apacheVer
="$($_APACHECTL -V | grep "Server version
:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
2730 _debug
"apacheVer" "$apacheVer"
2731 apacheMajer
="$(echo "$apacheVer" | cut -d . -f 1)"
2732 apacheMinor
="$(echo "$apacheVer" | cut -d . -f 2)"
2734 if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ]; then
2736 Alias /.well-known/acme-challenge $ACME_DIR
2738 <Directory $ACME_DIR >
2744 Alias /.well-known/acme-challenge $ACME_DIR
2746 <Directory $ACME_DIR >
2753 _msg
="$($_APACHECTL -t 2>&1)"
2754 if [ "$?" != "0" ]; then
2755 _err
"Sorry, apache config error"
2756 if _restoreApache
; then
2757 _err
"The apache config file is restored."
2759 _err
"Sorry, The apache config file can not be restored, please report bug."
2764 if [ ! -d "$ACME_DIR" ]; then
2765 mkdir
-p "$ACME_DIR"
2766 chmod 755 "$ACME_DIR"
2769 if ! _exec
"$_APACHECTL" graceful
; then
2771 _err
"$_APACHECTL graceful error, please contact me."
2779 #find the real nginx conf file
2782 #returns the real nginx conf file
2788 FOUND_REAL_NGINX_CONF
=""
2789 FOUND_REAL_NGINX_CONF_LN
=""
2790 BACKUP_NGINX_CONF
=""
2791 _debug _croot
"$_croot"
2792 _start_f
="$(echo "$_croot" | cut -d : -f 2)"
2793 _debug _start_f
"$_start_f"
2794 if [ -z "$_start_f" ]; then
2795 _debug
"find start conf from nginx command"
2796 if [ -z "$NGINX_CONF" ]; then
2797 if ! _exists
"nginx"; then
2798 _err
"nginx command is not found."
2801 NGINX_CONF
="$(nginx -V 2>&1 | _egrep_o "--conf-path=[^
]* " | tr -d " ")"
2802 _debug NGINX_CONF
"$NGINX_CONF"
2803 NGINX_CONF
="$(echo "$NGINX_CONF" | cut -d = -f 2)"
2804 _debug NGINX_CONF
"$NGINX_CONF"
2805 if [ -z "$NGINX_CONF" ]; then
2806 _err
"Can not find nginx conf."
2810 if [ ! -f "$NGINX_CONF" ]; then
2811 _err
"'$NGINX_CONF' doesn't exist."
2815 _debug
"Found nginx conf file:$NGINX_CONF"
2817 _start_f
="$NGINX_CONF"
2819 _debug
"Start detect nginx conf for $_d from:$_start_f"
2820 if ! _checkConf
"$_d" "$_start_f"; then
2821 _err
"Can not find conf file for domain $d"
2824 _info
"Found conf file: $FOUND_REAL_NGINX_CONF"
2826 _ln
=$FOUND_REAL_NGINX_CONF_LN
2829 _lnn
=$
(_math
$_ln + 1)
2831 _start_tag
="$(sed -n "$_lnn,${_lnn}p
" "$FOUND_REAL_NGINX_CONF")"
2832 _debug
"_start_tag" "$_start_tag"
2833 if [ "$_start_tag" = "$NGINX_START" ]; then
2834 _info
"The domain $_d is already configured, skip"
2835 FOUND_REAL_NGINX_CONF
=""
2839 mkdir
-p "$DOMAIN_BACKUP_PATH"
2840 _backup_conf
="$DOMAIN_BACKUP_PATH/$_d.nginx.conf"
2841 _debug _backup_conf
"$_backup_conf"
2842 BACKUP_NGINX_CONF
="$_backup_conf"
2843 _info
"Backup $FOUND_REAL_NGINX_CONF to $_backup_conf"
2844 if ! cp "$FOUND_REAL_NGINX_CONF" "$_backup_conf"; then
2845 _err
"backup error."
2846 FOUND_REAL_NGINX_CONF
=""
2850 if ! _exists
"nginx"; then
2851 _err
"nginx command is not found."
2854 _info
"Check the nginx conf before setting up."
2855 if ! _exec
"nginx -t" >/dev
/null
; then
2860 _info
"OK, Set up nginx config file"
2862 if ! sed -n "1,${_ln}p" "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"; then
2863 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2864 _err
"write nginx conf error, but don't worry, the file is restored to the original version."
2869 location ~ \"^/\.well-known/acme-challenge/([-_a-zA-Z0-9]+)\$\" {
2870 default_type text/plain;
2871 return 200 \"\$1.$_thumbpt\";
2874 " >>"$FOUND_REAL_NGINX_CONF"
2876 if ! sed -n "${_lnn},99999p" "$_backup_conf" >>"$FOUND_REAL_NGINX_CONF"; then
2877 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2878 _err
"write nginx conf error, but don't worry, the file is restored."
2881 _debug3
"Modified config:$(cat $FOUND_REAL_NGINX_CONF)"
2882 _info
"nginx conf is done, let's check it again."
2883 if ! _exec
"nginx -t" >/dev
/null
; then
2885 _err
"It seems that nginx conf was broken, let's restore."
2886 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2890 _info
"Reload nginx"
2891 if ! _exec
"nginx -s reload" >/dev
/null
; then
2893 _err
"It seems that nginx reload error, let's restore."
2894 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2905 _debug
"Start _checkConf from:$_c_file"
2906 if [ ! -f "$2" ] && ! echo "$2" |
grep '*$' >/dev
/null
&& echo "$2" |
grep '*' >/dev
/null
; then
2909 if [ -f "$_w_f" ] && _checkConf
"$1" "$_w_f"; then
2915 elif [ -f "$2" ]; then
2917 if _isRealNginxConf
"$1" "$2"; then
2918 _debug
"$2 is found."
2919 FOUND_REAL_NGINX_CONF
="$2"
2922 if cat "$2" |
tr "\t" " " |
grep "^ *include *.*;" >/dev
/null
; then
2923 _debug
"Try include files"
2924 for included
in $
(cat "$2" |
tr "\t" " " |
grep "^ *include *.*;" |
sed "s/include //" |
tr -d " ;"); do
2925 _debug
"check included $included"
2926 if _checkConf
"$1" "$included"; then
2933 _debug
"$2 not found."
2940 _isRealNginxConf
() {
2941 _debug
"_isRealNginxConf $1 $2"
2942 if [ -f "$2" ]; then
2943 for _fln
in $
(tr "\t" ' ' <"$2" |
grep -n "^ *server_name.* $1" | cut
-d : -f 1); do
2945 if [ "$_fln" ]; then
2946 _start
=$
(tr "\t" ' ' <"$2" | _head_n
"$_fln" |
grep -n "^ *server *" |
grep -v server_name | _tail_n
1)
2947 _debug
"_start" "$_start"
2948 _start_n
=$
(echo "$_start" | cut
-d : -f 1)
2949 _start_nn
=$
(_math
$_start_n + 1)
2950 _debug
"_start_n" "$_start_n"
2951 _debug
"_start_nn" "$_start_nn"
2953 _left
="$(sed -n "${_start_nn},99999p
" "$2")"
2954 _debug2 _left
"$_left"
2955 _end
="$(echo "$_left" | tr "\t" ' ' | grep -n "^
*server
*" | grep -v server_name | _head_n 1)"
2956 _debug
"_end" "$_end"
2957 if [ "$_end" ]; then
2958 _end_n
=$
(echo "$_end" | cut
-d : -f 1)
2959 _debug
"_end_n" "$_end_n"
2960 _seg_n
=$
(echo "$_left" |
sed -n "1,${_end_n}p")
2965 _debug
"_seg_n" "$_seg_n"
2968 for _listen_i
in $
(echo "$_seg_n" |
tr "\t" ' ' |
grep "^ *listen" |
tr -d " "); do
2969 if [ "$_listen_i" ]; then
2970 if [ "$(echo "$_listen_i" | _egrep_o "listen.
*ssl
")" ]; then
2971 _debug2
"$_listen_i is ssl"
2973 _debug2
"$_listen_i is plain text"
2980 if [ "$_skip_ssl" = "1" ]; then
2981 _debug
"ssl on, skip"
2983 FOUND_REAL_NGINX_CONF_LN
=$_fln
2984 _debug3
"found FOUND_REAL_NGINX_CONF_LN" "$FOUND_REAL_NGINX_CONF_LN"
2993 #restore all the nginx conf
2995 if [ -z "$NGINX_RESTORE_VLIST" ]; then
2996 _debug
"No need to restore nginx, skip."
2999 _debug
"_restoreNginx"
3000 _debug
"NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST"
3002 for ng_entry
in $
(echo "$NGINX_RESTORE_VLIST" |
tr "$dvsep" ' '); do
3003 _debug
"ng_entry" "$ng_entry"
3004 _nd
=$
(echo "$ng_entry" | cut
-d "$sep" -f 1)
3005 _ngconf
=$
(echo "$ng_entry" | cut
-d "$sep" -f 2)
3006 _ngbackupconf
=$
(echo "$ng_entry" | cut
-d "$sep" -f 3)
3007 _info
"Restoring from $_ngbackupconf to $_ngconf"
3008 cat "$_ngbackupconf" >"$_ngconf"
3011 _info
"Reload nginx"
3012 if ! _exec
"nginx -s reload" >/dev
/null
; then
3014 _err
"It seems that nginx reload error, please report bug."
3021 _stopserver
"$serverproc"
3026 if [ -z "$DEBUG" ]; then
3035 _debug
"_clearupdns"
3036 _debug
"dns_entries" "$dns_entries"
3038 if [ -z "$dns_entries" ]; then
3042 _info
"Removing DNS records."
3044 for entry
in $dns_entries; do
3045 d
=$
(_getfield
"$entry" 1)
3046 txtdomain
=$
(_getfield
"$entry" 2)
3047 aliasDomain
=$
(_getfield
"$entry" 3)
3048 _currentRoot
=$
(_getfield
"$entry" 4)
3049 txt
=$
(_getfield
"$entry" 5)
3050 d_api
=$
(_getfield
"$entry" 6)
3052 _debug
"txtdomain" "$txtdomain"
3053 _debug
"aliasDomain" "$aliasDomain"
3054 _debug
"_currentRoot" "$_currentRoot"
3056 _debug
"d_api" "$d_api"
3057 if [ "$d_api" = "$txt" ]; then
3061 if [ -z "$d_api" ]; then
3062 _info
"Not Found domain api file: $d_api"
3066 if [ "$aliasDomain" ]; then
3067 txtdomain
="$aliasDomain"
3071 if ! .
"$d_api"; then
3072 _err
"Load file $d_api error. Please check your api file and try again."
3076 rmcommand
="${_currentRoot}_rm"
3077 if ! _exists
"$rmcommand"; then
3078 _err
"It seems that your api file doesn't define $rmcommand"
3081 _info
"Removing txt: $txt for domain: $txtdomain"
3082 if ! $rmcommand "$txtdomain" "$txt"; then
3083 _err
"Error removing txt for domain:$txtdomain"
3086 _info
"Removed: Success"
3092 # webroot removelevel tokenfile
3093 _clearupwebbroot
() {
3095 if [ -z "$__webroot" ]; then
3096 _debug
"no webroot specified, skip"
3101 if [ "$2" = '1' ]; then
3102 _rmpath
="$__webroot/.well-known"
3103 elif [ "$2" = '2' ]; then
3104 _rmpath
="$__webroot/.well-known/acme-challenge"
3105 elif [ "$2" = '3' ]; then
3106 _rmpath
="$__webroot/.well-known/acme-challenge/$3"
3108 _debug
"Skip for removelevel:$2"
3111 if [ "$_rmpath" ]; then
3112 if [ "$DEBUG" ]; then
3113 _debug
"Debugging, skip removing: $_rmpath"
3123 _on_before_issue
() {
3125 _chk_main_domain
="$2"
3126 _chk_alt_domains
="$3"
3128 _chk_local_addr
="$5"
3129 _debug _on_before_issue
3130 _debug _chk_main_domain
"$_chk_main_domain"
3131 _debug _chk_alt_domains
"$_chk_alt_domains"
3133 if [ "$_chk_pre_hook" ]; then
3134 _info
"Run pre hook:'$_chk_pre_hook'"
3136 cd "$DOMAIN_PATH" && eval "$_chk_pre_hook"
3138 _err
"Error when run pre hook."
3143 if _hasfield
"$_chk_web_roots" "$NO_VALUE"; then
3144 if ! _exists
"socat"; then
3145 _err
"Please install socat tools first."
3150 _debug Le_LocalAddress
"$_chk_local_addr"
3157 d
="$(echo "$_chk_main_domain,$_chk_alt_domains," | cut -d , -f "$_w_index")"
3158 _w_index
="$(_math "$_w_index" + 1)"
3160 if [ -z "$d" ]; then
3163 _debug
"Check for domain" "$d"
3164 _currentRoot
="$(_getfield "$_chk_web_roots" $_index)"
3165 _debug
"_currentRoot" "$_currentRoot"
3166 _index
=$
(_math
$_index + 1)
3168 if [ "$_currentRoot" = "$NO_VALUE" ]; then
3169 _info
"Standalone mode."
3170 if [ -z "$Le_HTTPPort" ]; then
3172 _cleardomainconf
"Le_HTTPPort"
3174 _savedomainconf
"Le_HTTPPort" "$Le_HTTPPort"
3176 _checkport
="$Le_HTTPPort"
3177 elif [ "$_currentRoot" = "$W_ALPN" ]; then
3178 _info
"Standalone alpn mode."
3179 if [ -z "$Le_TLSPort" ]; then
3182 _savedomainconf
"Le_TLSPort" "$Le_TLSPort"
3184 _checkport
="$Le_TLSPort"
3187 if [ "$_checkport" ]; then
3188 _debug _checkport
"$_checkport"
3189 _checkaddr
="$(_getfield "$_chk_local_addr" $_addrIndex)"
3190 _debug _checkaddr
"$_checkaddr"
3192 _addrIndex
="$(_math $_addrIndex + 1)"
3194 _netprc
="$(_ss "$_checkport" | grep "$_checkport")"
3195 netprc
="$(echo "$_netprc" | grep "$_checkaddr")"
3196 if [ -z "$netprc" ]; then
3197 netprc
="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
3199 if [ "$netprc" ]; then
3201 _err
"tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
3202 _err
"Please stop it first"
3208 if _hasfield
"$_chk_web_roots" "apache"; then
3209 if ! _setApache
; then
3210 _err
"set up apache error. Report error to me."
3222 _debug _on_issue_err
3224 if [ "$LOG_FILE" ]; then
3225 _err
"Please check log file for more details: $LOG_FILE"
3227 _err
"Please add '--debug' or '--log' to check more details."
3228 _err
"See: $_DEBUG_WIKI"
3232 if [ "$_chk_post_hook" ]; then
3233 _info
"Run post hook:'$_chk_post_hook'"
3235 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
3237 _err
"Error when run post hook."
3242 #trigger the validation to flush the pending authz
3243 _debug2
"_chk_vlist" "$_chk_vlist"
3244 if [ "$_chk_vlist" ]; then
3246 _debug2
"start to deactivate authz"
3247 ventries
=$
(echo "$_chk_vlist" |
tr "$dvsep" ' ')
3248 for ventry
in $ventries; do
3249 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
3250 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
3251 uri
=$
(echo "$ventry" | cut
-d "$sep" -f 3)
3252 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
3253 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
3254 __trigger_validation
"$uri" "$keyauthorization"
3259 if [ "$IS_RENEW" = "1" ] && _hasfield
"$Le_Webroot" "$W_DNS"; then
3260 _err
"$_DNS_MANUAL_ERR"
3263 if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
3264 _debug
"$(_dlg_versions)"
3269 _on_issue_success
() {
3271 _chk_renew_hook
="$2"
3272 _debug _on_issue_success
3275 if [ "$_chk_post_hook" ]; then
3276 _info
"Run post hook:'$_chk_post_hook'"
3279 export CERT_KEY_PATH
3281 export CERT_FULLCHAIN_PATH
3282 export Le_Domain
="$_main_domain"
3283 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
3285 _err
"Error when run post hook."
3291 if [ "$IS_RENEW" ] && [ "$_chk_renew_hook" ]; then
3292 _info
"Run renew hook:'$_chk_renew_hook'"
3295 export CERT_KEY_PATH
3297 export CERT_FULLCHAIN_PATH
3298 export Le_Domain
="$_main_domain"
3299 cd "$DOMAIN_PATH" && eval "$_chk_renew_hook"
3301 _err
"Error when run renew hook."
3306 if _hasfield
"$Le_Webroot" "$W_DNS"; then
3307 _err
"$_DNS_MANUAL_WARN"
3315 _regAccount
"$_reg_length"
3318 __calcAccountKeyHash
() {
3319 [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256
<"$ACCOUNT_KEY_PATH"
3322 __calc_account_thumbprint
() {
3323 printf "%s" "$jwk" |
tr -d ' ' | _digest
"sha256" | _url_replace
3330 _debug3 _regAccount
"$_regAccount"
3334 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
3335 _info
"mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
3336 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3339 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
3340 _info
"mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
3341 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3344 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3345 if ! _create_account_key
"$_reg_length"; then
3346 _err
"Create account key error."
3351 if ! _calcjwk
"$ACCOUNT_KEY_PATH"; then
3355 if [ "$ACME_VERSION" = "2" ]; then
3356 regjson
='{"termsOfServiceAgreed": true}'
3357 if [ "$ACCOUNT_EMAIL" ]; then
3358 regjson
='{"contact": ["mailto: '$ACCOUNT_EMAIL'"], "termsOfServiceAgreed": true}'
3361 _reg_res
="$ACME_NEW_ACCOUNT_RES"
3362 regjson
='{"resource": "'$_reg_res'", "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
3363 if [ "$ACCOUNT_EMAIL" ]; then
3364 regjson
='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
3368 _info
"Registering account"
3370 if ! _send_signed_request
"${ACME_NEW_ACCOUNT}" "$regjson"; then
3371 _err
"Register account Error: $response"
3375 if [ "$code" = "" ] ||
[ "$code" = '201' ]; then
3376 echo "$response" >"$ACCOUNT_JSON_PATH"
3378 elif [ "$code" = '409' ] ||
[ "$code" = '200' ]; then
3379 _info
"Already registered"
3381 _err
"Register account Error: $response"
3385 _debug2 responseHeaders
"$responseHeaders"
3386 _accUri
="$(echo "$responseHeaders" | grep -i "^Location
:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
3387 _debug
"_accUri" "$_accUri"
3388 if [ -z "$_accUri" ]; then
3389 _err
"Can not find account id url."
3390 _err
"$responseHeaders"
3393 _savecaconf
"ACCOUNT_URL" "$_accUri"
3394 export ACCOUNT_URL
="$_accUri"
3396 CA_KEY_HASH
="$(__calcAccountKeyHash)"
3397 _debug
"Calc CA_KEY_HASH" "$CA_KEY_HASH"
3398 _savecaconf CA_KEY_HASH
"$CA_KEY_HASH"
3400 if [ "$code" = '403' ]; then
3401 _err
"It seems that the account key is already deactivated, please use a new account key."
3405 ACCOUNT_THUMBPRINT
="$(__calc_account_thumbprint)"
3406 _info
"ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT"
3409 #implement updateaccount
3413 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
3414 _info
"mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
3415 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3418 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
3419 _info
"mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
3420 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3423 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3424 _err
"Account key is not found at: $ACCOUNT_KEY_PATH"
3428 _accUri
=$
(_readcaconf
"ACCOUNT_URL")
3429 _debug _accUri
"$_accUri"
3431 if [ -z "$_accUri" ]; then
3432 _err
"The account url is empty, please run '--update-account' first to update the account info first,"
3433 _err
"Then try again."
3437 if ! _calcjwk
"$ACCOUNT_KEY_PATH"; then
3442 if [ "$ACME_VERSION" = "2" ]; then
3443 if [ "$ACCOUNT_EMAIL" ]; then
3444 updjson
='{"contact": ["mailto: '$ACCOUNT_EMAIL'"]}'
3447 # ACMEv1: Updates happen the same way a registration is done.
3448 # https://tools.ietf.org/html/draft-ietf-acme-acme-01#section-6.3
3453 # this part handles ACMEv2 account updates.
3454 _send_signed_request
"$_accUri" "$updjson"
3456 if [ "$code" = '200' ]; then
3457 _info
"account update success for $_accUri."
3459 _info
"Error. The account was not updated."
3464 #Implement deactivate account
3465 deactivateaccount
() {
3468 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
3469 _info
"mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
3470 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3473 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
3474 _info
"mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
3475 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3478 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3479 _err
"Account key is not found at: $ACCOUNT_KEY_PATH"
3483 _accUri
=$
(_readcaconf
"ACCOUNT_URL")
3484 _debug _accUri
"$_accUri"
3486 if [ -z "$_accUri" ]; then
3487 _err
"The account url is empty, please run '--update-account' first to update the account info first,"
3488 _err
"Then try again."
3492 if ! _calcjwk
"$ACCOUNT_KEY_PATH"; then
3497 if [ "$ACME_VERSION" = "2" ]; then
3498 _djson
="{\"status\":\"deactivated\"}"
3500 _djson
="{\"resource\": \"reg\", \"status\":\"deactivated\"}"
3502 if _send_signed_request
"$_accUri" "$_djson" && _contains
"$response" '"deactivated"'; then
3503 _info
"Deactivate account success for $_accUri."
3504 _accid
=$
(echo "$response" | _egrep_o
"\"id\" *: *[^,]*," | cut
-d : -f 2 |
tr -d ' ,')
3505 elif [ "$code" = "403" ]; then
3506 _info
"The account is already deactivated."
3507 _accid
=$
(_getfield
"$_accUri" "999" "/")
3509 _err
"Deactivate: account failed for $_accUri."
3513 _debug
"Account id: $_accid"
3514 if [ "$_accid" ]; then
3515 _deactivated_account_path
="$CA_DIR/deactivated/$_accid"
3516 _debug _deactivated_account_path
"$_deactivated_account_path"
3517 if mkdir
-p "$_deactivated_account_path"; then
3518 _info
"Moving deactivated account info to $_deactivated_account_path/"
3519 mv "$CA_CONF" "$_deactivated_account_path/"
3520 mv "$ACCOUNT_JSON_PATH" "$_deactivated_account_path/"
3521 mv "$ACCOUNT_KEY_PATH" "$_deactivated_account_path/"
3523 _err
"Can not create dir: $_deactivated_account_path, try to remove the deactivated account key."
3525 rm -f "$ACCOUNT_JSON_PATH"
3526 rm -f "$ACCOUNT_KEY_PATH"
3531 # domain folder file
3537 if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
3538 d_api
="$_SCRIPT_HOME/$_hookcat/$_hookname"
3539 elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
3540 d_api
="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
3541 elif [ "$_hookdomain" ] && [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
3542 d_api
="$LE_WORKING_DIR/$_hookdomain/$_hookname"
3543 elif [ "$_hookdomain" ] && [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
3544 d_api
="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
3545 elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
3546 d_api
="$LE_WORKING_DIR/$_hookname"
3547 elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
3548 d_api
="$LE_WORKING_DIR/$_hookname.sh"
3549 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
3550 d_api
="$LE_WORKING_DIR/$_hookcat/$_hookname"
3551 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
3552 d_api
="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
3555 printf "%s" "$d_api"
3559 __get_domain_new_authz
() {
3561 _info
"Getting new-authz for domain" "$_gdnd"
3563 _Max_new_authz_retry_times
=5
3565 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
3566 _debug
"Try new-authz for the $_authz_i time."
3567 if ! _send_signed_request
"${ACME_NEW_AUTHZ}" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
3568 _err
"Can not get domain new authz."
3571 if _contains
"$response" "No registration exists matching provided key"; then
3572 _err
"It seems there is an error, but it's recovered now, please try again."
3573 _err
"If you see this message for a second time, please report bug: $(__green "$PROJECT")"
3574 _clearcaconf
"CA_KEY_HASH"
3577 if ! _contains
"$response" "An error occurred while processing your request"; then
3578 _info
"The new-authz request is ok."
3581 _authz_i
="$(_math "$_authz_i" + 1)"
3582 _info
"The server is busy, Sleep $_authz_i to retry."
3586 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
3587 _err
"new-authz retry reach the max $_Max_new_authz_retry_times times."
3590 if [ "$code" ] && [ "$code" != '201' ]; then
3591 _err
"new-authz error: $response"
3597 #uri keyAuthorization
3598 __trigger_validation
() {
3599 _debug2
"Trigger domain validation."
3601 _debug2 _t_url
"$_t_url"
3603 _debug2 _t_key_authz
"$_t_key_authz"
3605 _debug2 _t_vtype
"$_t_vtype"
3606 if [ "$ACME_VERSION" = "2" ]; then
3607 _send_signed_request
"$_t_url" "{}"
3609 _send_signed_request
"$_t_url" "{\"resource\": \"challenge\", \"type\": \"$_t_vtype\", \"keyAuthorization\": \"$_t_key_authz\"}"
3613 #endpoint domain type
3618 _debug2
"_ns_ep" "$_ns_ep"
3619 _debug2
"_ns_domain" "$_ns_domain"
3620 _debug2
"_ns_type" "$_ns_type"
3622 response
="$(_H1="accept
: application
/dns-json
" _get "$_ns_ep?name
=$_ns_domain&type=$_ns_type")"
3624 _debug2
"response" "$response"
3625 if [ "$_ret" != "0" ]; then
3628 _answers
="$(echo "$response" | tr '{}' '<>' | _egrep_o '"Answer
":\[[^]]*]' | tr '<>' '\n\n')"
3629 _debug2
"_answers" "$_answers"
3637 _cf_ep
="https://cloudflare-dns.com/dns-query"
3638 _ns_lookup
"$_cf_ep" "$_cf_ld" "$_cf_ld_type"
3645 _debug
"Cloudflare purge $_cf_d_type record for domain $_cf_d"
3646 _cf_purl
="https://cloudflare-dns.com/api/v1/purge?domain=$_cf_d&type=$_cf_d_type"
3647 response
="$(_post "" "$_cf_purl")"
3648 _debug2 response
"$response"
3651 #txtdomain, alias, txt
3656 _debug
"_c_txtdomain" "$_c_txtdomain"
3657 _debug
"_c_aliasdomain" "$_c_aliasdomain"
3658 _debug
"_c_txt" "$_c_txt"
3659 _answers
="$(_ns_lookup_cf "$_c_aliasdomain" TXT)"
3660 _contains
"$_answers" "$_c_txt"
3667 _debug _p_txtdomain
"$_p_txtdomain"
3668 _ns_purge_cf
"$_p_txtdomain" "TXT"
3671 #wait and check each dns entries
3672 _check_dns_entries
() {
3674 _end_time
="$(_time)"
3675 _end_time
="$(_math "$_end_time" + 1200)" #let's check no more than 20 minutes.
3677 while [ "$(_time)" -le "$_end_time" ]; do
3679 for entry
in $dns_entries; do
3680 d
=$
(_getfield
"$entry" 1)
3681 txtdomain
=$
(_getfield
"$entry" 2)
3682 txtdomain
=$
(_idn
"$txtdomain")
3683 aliasDomain
=$
(_getfield
"$entry" 3)
3684 aliasDomain
=$
(_idn
"$aliasDomain")
3685 txt
=$
(_getfield
"$entry" 5)
3686 d_api
=$
(_getfield
"$entry" 6)
3688 _debug
"txtdomain" "$txtdomain"
3689 _debug
"aliasDomain" "$aliasDomain"
3691 _debug
"d_api" "$d_api"
3692 _info
"Checking $d for $aliasDomain"
3693 if _contains
"$_success_txt" ",$txt,"; then
3694 _info
"Already success, continue next one."
3698 if __check_txt
"$txtdomain" "$aliasDomain" "$txt"; then
3699 _info
"Domain $d '$aliasDomain' success."
3700 _success_txt
="$_success_txt,$txt,"
3704 _info
"Not valid yet, let's wait 10 seconds and check next one."
3705 __purge_txt
"$txtdomain"
3706 if [ "$txtdomain" != "$aliasDomain" ]; then
3707 __purge_txt
"$aliasDomain"
3711 if [ "$_left" ]; then
3712 _info
"Let's wait 10 seconds and check again".
3715 _info
"All success, let's return"
3722 #webroot, domain domainlist keylength
3724 if [ -z "$2" ]; then
3725 _usage
"Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
3728 if [ -z "$1" ]; then
3729 _usage
"Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc."
3736 if _contains
"$_main_domain" ","; then
3737 _main_domain
=$
(echo "$2,$3" | cut
-d , -f 1)
3738 _alt_domains
=$
(echo "$2,$3" | cut
-d , -f 2- |
sed "s/,${NO_VALUE}$//")
3740 _debug _main_domain
"$_main_domain"
3741 _debug _alt_domains
"$_alt_domains"
3748 _real_fullchain
="$9"
3753 _challenge_alias
="${14}"
3754 #remove these later.
3755 if [ "$_web_roots" = "dns-cf" ]; then
3758 if [ "$_web_roots" = "dns-dp" ]; then
3761 if [ "$_web_roots" = "dns-cx" ]; then
3765 if [ ! "$IS_RENEW" ]; then
3766 _initpath
"$_main_domain" "$_key_length"
3767 mkdir
-p "$DOMAIN_PATH"
3770 if _hasfield
"$_web_roots" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then
3771 _err
"$_DNS_MANUAL_ERROR"
3775 _debug
"Using ACME_DIRECTORY: $ACME_DIRECTORY"
3779 if [ -f "$DOMAIN_CONF" ]; then
3780 Le_NextRenewTime
=$
(_readdomainconf Le_NextRenewTime
)
3781 _debug Le_NextRenewTime
"$Le_NextRenewTime"
3782 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
3783 _saved_domain
=$
(_readdomainconf Le_Domain
)
3784 _debug _saved_domain
"$_saved_domain"
3785 _saved_alt
=$
(_readdomainconf Le_Alt
)
3786 _debug _saved_alt
"$_saved_alt"
3787 if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then
3788 _info
"Domains not changed."
3789 _info
"Skip, Next renewal time is: $(__green "$
(_readdomainconf Le_NextRenewTimeStr
)")"
3790 _info
"Add '$(__red '--force')' to force to renew."
3793 _info
"Domains have changed."
3798 _savedomainconf
"Le_Domain" "$_main_domain"
3799 _savedomainconf
"Le_Alt" "$_alt_domains"
3800 _savedomainconf
"Le_Webroot" "$_web_roots"
3802 _savedomainconf
"Le_PreHook" "$_pre_hook" "base64"
3803 _savedomainconf
"Le_PostHook" "$_post_hook" "base64"
3804 _savedomainconf
"Le_RenewHook" "$_renew_hook" "base64"
3806 if [ "$_local_addr" ]; then
3807 _savedomainconf
"Le_LocalAddress" "$_local_addr"
3809 _cleardomainconf
"Le_LocalAddress"
3811 if [ "$_challenge_alias" ]; then
3812 _savedomainconf
"Le_ChallengeAlias" "$_challenge_alias"
3814 _cleardomainconf
"Le_ChallengeAlias"
3817 if [ "$ACME_DIRECTORY" != "$DEFAULT_CA" ]; then
3818 Le_API
="$ACME_DIRECTORY"
3819 _savedomainconf
"Le_API" "$Le_API"
3821 _cleardomainconf Le_API
3824 if [ "$_alt_domains" = "$NO_VALUE" ]; then
3828 if [ "$_key_length" = "$NO_VALUE" ]; then
3832 if ! _on_before_issue
"$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
3833 _err
"_on_before_issue."
3837 _saved_account_key_hash
="$(_readcaconf "CA_KEY_HASH
")"
3838 _debug2 _saved_account_key_hash
"$_saved_account_key_hash"
3840 if [ -z "$ACCOUNT_URL" ] ||
[ -z "$_saved_account_key_hash" ] ||
[ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
3841 if ! _regAccount
"$_accountkeylength"; then
3842 _on_issue_err
"$_post_hook"
3846 _debug
"_saved_account_key_hash is not changed, skip register account."
3849 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
3850 _info
"Signing from existing CSR."
3852 _key
=$
(_readdomainconf Le_Keylength
)
3853 _debug
"Read key length:$_key"
3854 if [ ! -f "$CERT_KEY_PATH" ] ||
[ "$_key_length" != "$_key" ] ||
[ "$Le_ForceNewDomainKey" = "1" ]; then
3855 if ! createDomainKey
"$_main_domain" "$_key_length"; then
3856 _err
"Create domain key error."
3858 _on_issue_err
"$_post_hook"
3863 if ! _createcsr
"$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
3864 _err
"Create CSR error."
3866 _on_issue_err
"$_post_hook"
3871 _savedomainconf
"Le_Keylength" "$_key_length"
3874 _cleardomainconf
"Le_Vlist"
3875 _info
"Getting domain auth token for each domain"
3878 if [ -z "$vlist" ]; then
3879 if [ "$ACME_VERSION" = "2" ]; then
3880 #make new order request
3881 _identifiers
="{\"type\":\"dns\",\"value\":\"$(_idn "$_main_domain")\"}"
3884 d
="$(echo "$_alt_domains," | cut -d , -f "$_w_index")"
3885 _w_index
="$(_math "$_w_index" + 1)"
3887 if [ -z "$d" ]; then
3890 _identifiers
="$_identifiers,{\"type\":\"dns\",\"value\":\"$(_idn "$d")\"}"
3892 _debug2 _identifiers
"$_identifiers"
3893 if ! _send_signed_request
"$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
3894 _err
"Create new order error."
3896 _on_issue_err
"$_post_hook"
3899 Le_LinkOrder
="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
3900 _debug Le_LinkOrder
"$Le_LinkOrder"
3901 Le_OrderFinalize
="$(echo "$response" | _egrep_o '"finalize
" *: *"[^
"]*"' | cut -d '"' -f 4)"
3902 _debug Le_OrderFinalize
"$Le_OrderFinalize"
3903 if [ -z "$Le_OrderFinalize" ]; then
3904 _err
"Create new order error. Le_OrderFinalize not found. $response"
3906 _on_issue_err
"$_post_hook"
3910 #for dns manual mode
3911 _savedomainconf
"Le_OrderFinalize" "$Le_OrderFinalize"
3913 _authorizations_seg
="$(echo "$response" | _egrep_o '"authorizations
" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
3914 _debug2 _authorizations_seg "$_authorizations_seg"
3915 if [ -z "$_authorizations_seg" ]; then
3916 _err "_authorizations_seg not found."
3918 _on_issue_err "$_post_hook"
3922 #domain and authz map
3923 _authorizations_map=""
3924 for _authz_url in $(echo "$_authorizations_seg" | tr ',' ' '); do
3925 _debug2 "_authz_url" "$_authz_url"
3926 if ! _send_signed_request "$_authz_url"; then
3927 _err "get to authz error."
3928 _err "_authorizations_seg" "$_authorizations_seg"
3929 _err "_authz_url" "$_authz_url"
3931 _on_issue_err "$_post_hook"
3935 response="$(echo "$response" | _normalizeJson)"
3936 _debug2 response "$response"
3937 _d="$(echo "$response" | _egrep_o '"value" *: *"[^"]*"' | cut -d : -f 2 | tr -d ' "')"
3938 if _contains "$response" "\"wildcard\" *: *true"; then
3942 _authorizations_map="$_d,$response
3943 $_authorizations_map"
3945 _debug2 _authorizations_map "$_authorizations_map"
3952 d="$(echo "$_main_domain,$_alt_domains," | cut -d , -f "$_w_index")"
3953 _w_index="$(_math "$_w_index" + 1)"
3955 if [ -z "$d" ]; then
3958 _info "Getting webroot for domain" "$d"
3959 _index=$(_math $_index + 1)
3960 _w="$(echo $_web_roots | cut -d , -f $_index)"
3965 _debug "_currentRoot" "$_currentRoot"
3968 #todo, v2 wildcard force to use dns
3969 if _startswith "$_currentRoot" "$W_DNS"; then
3973 if [ "$_currentRoot" = "$W_ALPN" ]; then
3977 if [ "$ACME_VERSION" = "2" ]; then
3978 response="$(echo "$_authorizations_map" | grep "^$(_idn "$d")," | sed "s/$d,//")"
3979 _debug2 "response" "$response"
3980 if [ -z "$response" ]; then
3981 _err "get to authz error."
3982 _err "_authorizations_map" "$_authorizations_map"
3984 _on_issue_err "$_post_hook"
3988 if ! __get_domain_new_authz "$d"; then
3990 _on_issue_err "$_post_hook"
3995 if [ -z "$thumbprint" ]; then
3996 thumbprint="$(__calc_account_thumbprint)"
3999 entry="$(echo "$response" | _egrep_o '[^\
{]*"type":"'$vtype'"[^\
}]*')"
4000 _debug entry "$entry"
4001 if [ -z "$entry" ]; then
4002 _err "Error, can not get domain token entry $d"
4003 _supported_vtypes="$(echo "$response" | _egrep_o "\"challenges\":\[[^]]*]" | tr '{' "\n" | grep type | cut -d '"' -f 4 | tr "\n" ' ')"
4004 if [ "$_supported_vtypes" ]; then
4005 _err
"The supported validation types are: $_supported_vtypes, but you specified: $vtype"
4008 _on_issue_err
"$_post_hook"
4011 token
="$(echo "$entry" | _egrep_o '"token
":"[^
"]*' | cut -d : -f 2 | tr -d '"')"
4012 _debug token "$token"
4014 if [ -z "$token" ]; then
4015 _err "Error, can not get domain token $entry"
4017 _on_issue_err "$_post_hook"
4020 if [ "$ACME_VERSION" = "2" ]; then
4021 uri="$(echo "$entry" | _egrep_o '"url":"[^"]*' | cut -d '"' -f 4 | _head_n 1)"
4023 uri
="$(echo "$entry" | _egrep_o '"uri
":"[^
"]*' | cut -d '"' -f 4)"
4027 if [ -z "$uri" ]; then
4028 _err "Error, can not get domain uri. $entry"
4030 _on_issue_err "$_post_hook"
4033 keyauthorization="$token.$thumbprint"
4034 _debug keyauthorization "$keyauthorization"
4036 if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
4037 _debug "$d is already verified."
4038 keyauthorization="$STATE_VERIFIED"
4039 _debug keyauthorization "$keyauthorization"
4042 dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
4043 _debug dvlist "$dvlist"
4045 vlist="$vlist$dvlist$dvsep"
4048 _debug vlist "$vlist"
4052 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
4054 for ventry in $ventries; do
4055 d=$(echo "$ventry" | cut -d "$sep" -f 1)
4056 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
4057 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
4058 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
4060 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
4061 _debug "$d is already verified, skip $vtype."
4062 _alias_index="$(_math "$_alias_index" + 1)"
4066 if [ "$vtype" = "$VTYPE_DNS" ]; then
4069 if _startswith "$_dns_root_d" "*."; then
4070 _dns_root_d="$(echo "$_dns_root_d" | sed 's
/*.
//')"
4072 _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")"
4073 _alias_index="$(_math "$_alias_index" + 1)"
4074 _debug "_d_alias" "$_d_alias"
4075 if [ "$_d_alias" ]; then
4076 if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then
4077 txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")"
4079 txtdomain="_acme-challenge.$_d_alias"
4081 dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$txtdomain$dvsep$_currentRoot"
4083 txtdomain="_acme-challenge.$_dns_root_d"
4084 dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$dvsep$_currentRoot"
4087 _debug txtdomain "$txtdomain"
4088 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
4091 d_api="$(_findHook "$_dns_root_d" $_SUB_FOLDER_DNSAPI "$_currentRoot")"
4092 _debug d_api "$d_api"
4094 dns_entry="$dns_entry$dvsep$txt${dvsep}$d_api"
4095 _debug2 dns_entry "$dns_entry"
4096 if [ "$d_api" ]; then
4097 _debug "Found domain api file: $d_api"
4099 if [ "$_currentRoot" != "$W_DNS" ]; then
4100 _err "Can not find dns api hook for: $_currentRoot"
4101 _info "You need to add the txt record manually."
4103 _info "$(__red "Add the following TXT record:")"
4104 _info "$(__red "Domain: '$
(__green
"$txtdomain")'")"
4105 _info "$(__red "TXT value: '$
(__green
"$txt")'")"
4106 _info "$(__red "Please be aware that you prepend _acme-challenge. before your domain")"
4107 _info "$(__red "so the resulting subdomain will be: $txtdomain")"
4112 if ! . "$d_api"; then
4113 _err "Load file $d_api error. Please check your api file and try again."
4117 addcommand="${_currentRoot}_add"
4118 if ! _exists "$addcommand"; then
4119 _err "It seems that your api file is not correct, it must have a function named: $addcommand"
4122 _info "Adding txt value: $txt for domain: $txtdomain"
4123 if ! $addcommand "$txtdomain" "$txt"; then
4124 _err "Error add txt for domain:$txtdomain"
4127 _info "The txt record is added: Success."
4130 if [ "$?" != "0" ]; then
4131 _on_issue_err "$_post_hook" "$vlist"
4135 dns_entries="$dns_entries$dns_entry
4137 _debug2 "$dns_entries"
4142 if [ "$dnsadded" = '0' ]; then
4143 _savedomainconf "Le_Vlist" "$vlist"
4144 _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
4145 _err "Please add the TXT records to the domains, and re-run with --renew."
4146 _on_issue_err "$_post_hook"
4153 if [ "$dns_entries" ]; then
4154 if [ -z "$Le_DNSSleep" ]; then
4155 _info "Let's check each dns records now. Sleep
20 seconds first.
"
4157 if ! _check_dns_entries; then
4158 _err "check dns error.
"
4159 _on_issue_err "$_post_hook"
4164 _savedomainconf "Le_DNSSleep
" "$Le_DNSSleep"
4165 _info "Sleep $
(__green
$Le_DNSSleep) seconds
for the txt records to take effect
"
4166 _sleep "$Le_DNSSleep"
4170 NGINX_RESTORE_VLIST=""
4171 _debug "ok
, let's start to verify"
4174 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
4175 for ventry in $ventries; do
4176 d=$(echo "$ventry" | cut -d "$sep" -f 1)
4177 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
4178 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
4179 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
4180 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
4182 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
4183 _info "$d is already verified, skip $vtype."
4187 _info "Verifying: $d"
4189 _debug "keyauthorization" "$keyauthorization"
4192 token="$(printf "%s" "$keyauthorization" | cut -d '.
' -f 1)"
4194 _debug "_currentRoot" "$_currentRoot"
4196 if [ "$vtype" = "$VTYPE_HTTP" ]; then
4197 if [ "$_currentRoot" = "$NO_VALUE" ]; then
4198 _info "Standalone mode server"
4199 _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
4200 _ncIndex="$(_math $_ncIndex + 1)"
4201 _startserver "$keyauthorization" "$_ncaddr"
4202 if [ "$?" != "0" ]; then
4204 _on_issue_err "$_post_hook" "$vlist"
4208 _debug serverproc "$serverproc"
4209 elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
4210 _info "Stateless mode for domain:$d"
4212 elif _startswith "$_currentRoot" "$NGINX"; then
4213 _info "Nginx mode for domain:$d"
4214 #set up nginx server
4215 FOUND_REAL_NGINX_CONF=""
4216 BACKUP_NGINX_CONF=""
4217 if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then
4219 _on_issue_err "$_post_hook" "$vlist"
4223 if [ "$FOUND_REAL_NGINX_CONF" ]; then
4224 _realConf="$FOUND_REAL_NGINX_CONF"
4225 _backup="$BACKUP_NGINX_CONF"
4226 _debug _realConf "$_realConf"
4227 NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
4231 if [ "$_currentRoot" = "apache" ]; then
4232 wellknown_path="$ACME_DIR"
4234 wellknown_path="$_currentRoot/.well-known/acme-challenge"
4235 if [ ! -d "$_currentRoot/.well-known" ]; then
4237 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
4244 _debug wellknown_path "$wellknown_path"
4246 _debug "writing token:$token to $wellknown_path/$token"
4248 mkdir -p "$wellknown_path"
4250 if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
4251 _err "$d:Can not write token to file : $wellknown_path/$token"
4252 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4254 _on_issue_err "$_post_hook" "$vlist"
4258 if [ ! "$usingApache" ]; then
4259 if webroot_owner=$(_stat "$_currentRoot"); then
4260 _debug "Changing owner/group of .well-known to $webroot_owner"
4261 if ! _exec "chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then
4262 _debug "$(cat "$_EXEC_TEMP_ERR")"
4263 _exec_err >/dev/null 2>&1
4266 _debug "not changing owner/group of webroot"
4271 elif [ "$vtype" = "$VTYPE_ALPN" ]; then
4272 acmevalidationv1="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")"
4273 _debug acmevalidationv1 "$acmevalidationv1"
4274 if ! _starttlsserver "$d" "" "$Le_TLSPort" "$keyauthorization" "$_ncaddr" "$acmevalidationv1"; then
4275 _err "Start tls server error."
4276 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4278 _on_issue_err "$_post_hook" "$vlist"
4283 if ! __trigger_validation "$uri" "$keyauthorization" "$vtype"; then
4284 _err "$d:Can not get challenge: $response"
4285 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4287 _on_issue_err "$_post_hook" "$vlist"
4291 if [ "$code" ] && [ "$code" != '202' ]; then
4292 if [ "$code" = '200' ]; then
4293 _debug "trigger validation code: $code"
4295 _err "$d:Challenge error: $response"
4296 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4298 _on_issue_err "$_post_hook" "$vlist"
4304 if [ -z "$MAX_RETRY_TIMES" ]; then
4309 waittimes=$(_math "$waittimes" + 1)
4310 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
4312 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4314 _on_issue_err "$_post_hook" "$vlist"
4318 _debug "sleep 2 secs to verify"
4321 if [ "$ACME_VERSION" = "2" ]; then
4322 _send_signed_request "$uri"
4324 response="$(_get "$uri")"
4326 if [ "$?" != "0" ]; then
4327 _err "$d:Verify error:$response"
4328 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4330 _on_issue_err "$_post_hook" "$vlist"
4333 _debug2 original "$response"
4335 response="$(echo "$response" | _normalizeJson)"
4336 _debug2 response "$response"
4338 status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
4339 if [ "$status" = "valid
" ]; then
4340 _info "$
(__green Success
)"
4341 _stopserver "$serverproc"
4343 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4347 if [ "$status" = "invalid
" ]; then
4348 error="$
(echo "$response" | _egrep_o
'"error":\{[^\}]*')"
4349 _debug2 error "$error"
4350 errordetail="$
(echo "$error" | _egrep_o
'"detail": *"[^"]*' | cut
-d '"' -f 4)"
4351 _debug2 errordetail "$errordetail"
4352 if [ "$errordetail" ]; then
4353 _err "$d:Verify error
:$errordetail"
4355 _err "$d:Verify error
:$error"
4357 if [ "$DEBUG" ]; then
4358 if [ "$vtype" = "$VTYPE_HTTP" ]; then
4359 _debug "Debug
: get token url.
"
4360 _get "http
://$d/.well-known
/acme-challenge
/$token" "" 1
4363 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4365 _on_issue_err "$_post_hook" "$vlist"
4369 if [ "$status" = "pending
" ]; then
4371 elif [ "$status" = "processing
" ]; then
4374 _err "$d:Verify error
:$response"
4375 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4377 _on_issue_err "$_post_hook" "$vlist"
4386 _info "Verify finished
, start to sign.
"
4387 der="$
(_getfile
"${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" |
tr -d "\r\n" | _url_replace
)"
4389 if [ "$ACME_VERSION" = "2" ]; then
4390 _info "Lets finalize the order
, Le_OrderFinalize
: $Le_OrderFinalize"
4391 if ! _send_signed_request "${Le_OrderFinalize}" "{\"csr
\": \"$der\"}"; then
4393 _on_issue_err "$_post_hook"
4396 if [ "$code" != "200" ]; then
4397 _err "Sign failed
, finalize code is not
200.
"
4399 _on_issue_err "$_post_hook"
4402 if [ -z "$Le_LinkOrder" ]; then
4403 Le_LinkOrder="$
(echo "$responseHeaders" |
grep -i '^Location.*$' | _tail_n
1 |
tr -d "\r\n" | cut
-d " " -f 2)"
4406 _savedomainconf "Le_LinkOrder
" "$Le_LinkOrder"
4410 while [ "$_link_cert_retry" -lt "$_MAX_CERT_RETRY" ]; do
4411 if _contains "$response" "\"status
\":\"valid
\""; then
4412 _debug "Order status is valid.
"
4413 Le_LinkCert="$
(echo "$response" | _egrep_o
'"certificate" *: *"[^"]*"' | cut
-d '"' -f 4)"
4414 _debug Le_LinkCert "$Le_LinkCert"
4415 if [ -z "$Le_LinkCert" ]; then
4416 _err "Sign error
, can not
find Le_LinkCert
"
4418 _on_issue_err "$_post_hook"
4422 elif _contains "$response" "\"processing
\""; then
4423 _info "Order status is processing
, lets
sleep and retry.
"
4426 _err "Sign error
, wrong status
"
4428 _on_issue_err "$_post_hook"
4431 #the order is processing, so we are going to poll order status
4432 if [ -z "$Le_LinkOrder" ]; then
4433 _err "Sign error
, can not get order link location header
"
4434 _err "responseHeaders
" "$responseHeaders"
4435 _on_issue_err "$_post_hook"
4438 _info "Polling order status
: $Le_LinkOrder"
4439 if ! _send_signed_request "$Le_LinkOrder"; then
4440 _err "Sign failed
, can not post to Le_LinkOrder cert
:$Le_LinkOrder.
"
4442 _on_issue_err "$_post_hook"
4445 _link_cert_retry="$
(_math
$_link_cert_retry + 1)"
4448 if [ -z "$Le_LinkCert" ]; then
4449 _err "Sign failed
, can not get Le_LinkCert
, retry
time limit.
"
4451 _on_issue_err "$_post_hook"
4454 _info "Download cert
, Le_LinkCert
: $Le_LinkCert"
4455 if ! _send_signed_request "$Le_LinkCert"; then
4456 _err "Sign failed
, can not download cert
:$Le_LinkCert.
"
4458 _on_issue_err "$_post_hook"
4462 echo "$response" >"$CERT_PATH"
4464 if [ "$
(grep -- "$BEGIN_CERT" "$CERT_PATH" |
wc -l)" -gt "1" ]; then
4465 _debug "Found cert chain
"
4466 cat "$CERT_PATH" >"$CERT_FULLCHAIN_PATH"
4467 _end_n="$
(grep -n -- "$END_CERT" "$CERT_FULLCHAIN_PATH" | _head_n
1 | cut
-d : -f 1)"
4468 _debug _end_n "$_end_n"
4469 sed -n "1,${_end_n}p
" "$CERT_FULLCHAIN_PATH" >"$CERT_PATH"
4470 _end_n="$
(_math
$_end_n + 1)"
4471 sed -n "${_end_n},9999p
" "$CERT_FULLCHAIN_PATH" >"$CA_CERT_PATH"
4475 if ! _send_signed_request "${ACME_NEW_ORDER}" "{\"resource
\": \"$ACME_NEW_ORDER_RES\", \"csr
\": \"$der\"}" "needbase64
"; then
4476 _err "Sign failed.
$response"
4477 _on_issue_err "$_post_hook"
4481 Le_LinkCert="$
(grep -i '^Location.*$' "$HTTP_HEADER" | _tail_n
1 |
tr -d "\r\n" | cut
-d " " -f 2)"
4482 echo "$BEGIN_CERT" >"$CERT_PATH"
4484 #if ! _get "$Le_LinkCert" | _base64 "multiline
" >> "$CERT_PATH" ; then
4485 # _debug "Get cert failed. Let
's try last response."
4486 # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
4489 if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then
4490 _debug "Try cert link."
4491 _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH"
4494 echo "$END_CERT" >>"$CERT_PATH"
4497 _debug "Le_LinkCert" "$Le_LinkCert"
4498 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
4500 if [ -z "$Le_LinkCert" ] || ! _checkcert "$CERT_PATH"; then
4501 response="$(echo "$response" | _dbase64 "multiline" | tr -d '\
0' | _normalizeJson)"
4502 _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
4503 _on_issue_err
"$_post_hook"
4507 if [ "$Le_LinkCert" ]; then
4508 _info
"$(__green "Cert success.
")"
4511 _info
"Your cert is in $(__green " $CERT_PATH ")"
4513 if [ -f "$CERT_KEY_PATH" ]; then
4514 _info
"Your cert key is in $(__green " $CERT_KEY_PATH ")"
4517 if [ ! "$USER_PATH" ] ||
[ ! "$ACME_IN_CRON" ]; then
4519 _saveaccountconf
"USER_PATH" "$USER_PATH"
4523 if [ "$ACME_VERSION" = "2" ]; then
4526 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
4527 Le_LinkIssuer
=$
(grep -i '^Link' "$HTTP_HEADER" | _head_n
1 | cut
-d " " -f 2 | cut
-d ';' -f 1 |
tr -d '<>')
4529 if [ "$Le_LinkIssuer" ]; then
4530 if ! _contains
"$Le_LinkIssuer" ":"; then
4531 _info
"$(__red "Relative issuer link found.
")"
4532 Le_LinkIssuer
="$_ACME_SERVER_HOST$Le_LinkIssuer"
4534 _debug Le_LinkIssuer
"$Le_LinkIssuer"
4535 _savedomainconf
"Le_LinkIssuer" "$Le_LinkIssuer"
4537 _link_issuer_retry
=0
4539 while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do
4540 _debug _link_issuer_retry
"$_link_issuer_retry"
4541 if [ "$ACME_VERSION" = "2" ]; then
4542 if _send_signed_request
"$Le_LinkIssuer"; then
4543 echo "$response" >"$CA_CERT_PATH"
4547 if _get
"$Le_LinkIssuer" >"$CA_CERT_PATH.der"; then
4548 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
4549 _base64
"multiline" <"$CA_CERT_PATH.der" >>"$CA_CERT_PATH"
4550 echo "$END_CERT" >>"$CA_CERT_PATH"
4551 if ! _checkcert
"$CA_CERT_PATH"; then
4552 _err
"Can not get the ca cert."
4555 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
4556 rm -f "$CA_CERT_PATH.der"
4560 _link_issuer_retry
=$
(_math
$_link_issuer_retry + 1)
4561 _sleep
"$_link_issuer_retry"
4563 if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then
4564 _err
"Max retry for issuer ca cert is reached."
4567 _debug
"No Le_LinkIssuer header found."
4570 [ -f "$CA_CERT_PATH" ] && _info
"The intermediate CA cert is in $(__green " $CA_CERT_PATH ")"
4571 [ -f "$CERT_FULLCHAIN_PATH" ] && _info
"And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")"
4573 Le_CertCreateTime
=$
(_time
)
4574 _savedomainconf
"Le_CertCreateTime" "$Le_CertCreateTime"
4576 Le_CertCreateTimeStr
=$
(date -u)
4577 _savedomainconf
"Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
4579 if [ -z "$Le_RenewalDays" ] ||
[ "$Le_RenewalDays" -lt "0" ]; then
4580 Le_RenewalDays
="$DEFAULT_RENEW"
4582 _savedomainconf
"Le_RenewalDays" "$Le_RenewalDays"
4585 if [ "$CA_BUNDLE" ]; then
4586 _saveaccountconf CA_BUNDLE
"$CA_BUNDLE"
4588 _clearaccountconf
"CA_BUNDLE"
4591 if [ "$CA_PATH" ]; then
4592 _saveaccountconf CA_PATH
"$CA_PATH"
4594 _clearaccountconf
"CA_PATH"
4597 if [ "$HTTPS_INSECURE" ]; then
4598 _saveaccountconf HTTPS_INSECURE
"$HTTPS_INSECURE"
4600 _clearaccountconf
"HTTPS_INSECURE"
4603 if [ "$Le_Listen_V4" ]; then
4604 _savedomainconf
"Le_Listen_V4" "$Le_Listen_V4"
4605 _cleardomainconf Le_Listen_V6
4606 elif [ "$Le_Listen_V6" ]; then
4607 _savedomainconf
"Le_Listen_V6" "$Le_Listen_V6"
4608 _cleardomainconf Le_Listen_V4
4611 if [ "$Le_ForceNewDomainKey" = "1" ]; then
4612 _savedomainconf
"Le_ForceNewDomainKey" "$Le_ForceNewDomainKey"
4614 _cleardomainconf Le_ForceNewDomainKey
4617 Le_NextRenewTime
=$
(_math
"$Le_CertCreateTime" + "$Le_RenewalDays" \
* 24 \
* 60 \
* 60)
4619 Le_NextRenewTimeStr
=$
(_time2str
"$Le_NextRenewTime")
4620 _savedomainconf
"Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
4622 Le_NextRenewTime
=$
(_math
"$Le_NextRenewTime" - 86400)
4623 _savedomainconf
"Le_NextRenewTime" "$Le_NextRenewTime"
4625 if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
4626 _savedomainconf
"Le_RealCertPath" "$_real_cert"
4627 _savedomainconf
"Le_RealCACertPath" "$_real_ca"
4628 _savedomainconf
"Le_RealKeyPath" "$_real_key"
4629 _savedomainconf
"Le_ReloadCmd" "$_reload_cmd" "base64"
4630 _savedomainconf
"Le_RealFullChainPath" "$_real_fullchain"
4631 if ! _installcert
"$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"; then
4636 if ! _on_issue_success
"$_post_hook" "$_renew_hook"; then
4637 _err
"Call hook error."
4645 if [ -z "$Le_Domain" ]; then
4646 _usage
"Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
4652 _initpath
"$Le_Domain" "$_isEcc"
4654 _info
"$(__green "Renew
: '$Le_Domain'")"
4655 if [ ! -f "$DOMAIN_CONF" ]; then
4656 _info
"'$Le_Domain' is not a issued domain, skip."
4660 if [ "$Le_RenewalDays" ]; then
4661 _savedomainconf Le_RenewalDays
"$Le_RenewalDays"
4665 _debug Le_API
"$Le_API"
4667 if [ "$Le_API" = "$LETSENCRYPT_CA_V1" ]; then
4668 _cleardomainconf Le_API
4669 Le_API
="$DEFAULT_CA"
4671 if [ "$Le_API" = "$LETSENCRYPT_STAGING_CA_V1" ]; then
4672 _cleardomainconf Le_API
4673 Le_API
="$DEFAULT_STAGING_CA"
4676 if [ "$Le_API" ]; then
4677 if [ "$_OLD_CA_HOST" = "$Le_API" ]; then
4678 export Le_API
="$DEFAULT_CA"
4679 _savedomainconf Le_API
"$Le_API"
4681 if [ "$_OLD_STAGE_CA_HOST" = "$Le_API" ]; then
4682 export Le_API
="$DEFAULT_STAGING_CA"
4683 _savedomainconf Le_API
"$Le_API"
4685 export ACME_DIRECTORY
="$Le_API"
4688 ACCOUNT_JSON_PATH
=""
4690 _debug3
"initpath again."
4691 _initpath
"$Le_Domain" "$_isEcc"
4694 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
4695 _info
"Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
4696 _info
"Add '$(__red '--force')' to force to renew."
4697 return "$RENEW_SKIP"
4700 if [ "$ACME_IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then
4701 _info
"Skip invalid cert for: $Le_Domain"
4706 Le_ReloadCmd
="$(_readdomainconf Le_ReloadCmd)"
4707 Le_PreHook
="$(_readdomainconf Le_PreHook)"
4708 Le_PostHook
="$(_readdomainconf Le_PostHook)"
4709 Le_RenewHook
="$(_readdomainconf Le_RenewHook)"
4710 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"
4712 if [ "$res" != "0" ]; then
4716 if [ "$Le_DeployHook" ]; then
4717 _deploy
"$Le_Domain" "$Le_DeployHook"
4726 #renewAll [stopRenewOnError]
4729 _stopRenewOnError
="$1"
4730 _debug
"_stopRenewOnError" "$_stopRenewOnError"
4735 _error_level
=$NOTIFY_LEVEL_SKIP
4736 _notify_code
=$RENEW_SKIP
4737 _set_level
=${NOTIFY_LEVEL:-$NOTIFY_LEVEL_DEFAULT}
4738 _debug
"_set_level" "$_set_level"
4739 for di
in "${CERT_HOME}"/*.
*/; do
4741 if ! [ -d "$di" ]; then
4742 _debug
"Not directory, skip: $di"
4748 if _endswith
"$d" "$ECC_SUFFIX"; then
4749 _isEcc
=$
(echo "$d" | cut
-d "$ECC_SEP" -f 2)
4750 d
=$
(echo "$d" | cut
-d "$ECC_SEP" -f 1)
4752 renew
"$d" "$_isEcc"
4755 _debug
"Return code: $rc"
4756 if [ "$rc" = "0" ]; then
4757 if [ $_error_level -gt $NOTIFY_LEVEL_RENEW ]; then
4758 _error_level
="$NOTIFY_LEVEL_RENEW"
4761 if [ "$ACME_IN_CRON" ]; then
4762 if [ $_set_level -ge $NOTIFY_LEVEL_RENEW ]; then
4763 if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then
4764 _send_notify
"Renew $d success" "Good, the cert is renewed." "$NOTIFY_HOOK" 0
4768 _success_msg
="${_success_msg} $d
4770 elif [ "$rc" = "$RENEW_SKIP" ]; then
4771 if [ $_error_level -gt $NOTIFY_LEVEL_SKIP ]; then
4772 _error_level
="$NOTIFY_LEVEL_SKIP"
4773 _notify_code
=$RENEW_SKIP
4775 if [ "$ACME_IN_CRON" ]; then
4776 if [ $_set_level -ge $NOTIFY_LEVEL_SKIP ]; then
4777 if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then
4778 _send_notify
"Renew $d skipped" "Good, the cert is skipped." "$NOTIFY_HOOK" "$RENEW_SKIP"
4783 _skipped_msg
="${_skipped_msg} $d
4786 if [ $_error_level -gt $NOTIFY_LEVEL_ERROR ]; then
4787 _error_level
="$NOTIFY_LEVEL_ERROR"
4790 if [ "$ACME_IN_CRON" ]; then
4791 if [ $_set_level -ge $NOTIFY_LEVEL_ERROR ]; then
4792 if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then
4793 _send_notify
"Renew $d error" "There is an error." "$NOTIFY_HOOK" 1
4797 _error_msg
="${_error_msg} $d
4799 if [ "$_stopRenewOnError" ]; then
4800 _err
"Error renew $d, stop now."
4805 _err
"Error renew $d."
4809 _debug _error_level
"$_error_level"
4810 _debug _set_level
"$_set_level"
4811 if [ "$ACME_IN_CRON" ] && [ $_error_level -le $_set_level ]; then
4812 if [ -z "$NOTIFY_MODE" ] ||
[ "$NOTIFY_MODE" = "$NOTIFY_MODE_BULK" ]; then
4813 _msg_subject
="Renew"
4814 if [ "$_error_msg" ]; then
4815 _msg_subject
="${_msg_subject} Error"
4816 _msg_data
="Error certs:
4820 if [ "$_success_msg" ]; then
4821 _msg_subject
="${_msg_subject} Success"
4822 _msg_data
="${_msg_data}Success certs:
4826 if [ "$_skipped_msg" ]; then
4827 _msg_subject
="${_msg_subject} Skipped"
4828 _msg_data
="${_msg_data}Skipped certs:
4833 _send_notify
"$_msg_subject" "$_msg_data" "$NOTIFY_HOOK" "$_notify_code"
4844 if [ -z "$_csrfile" ] ||
[ -z "$_csrW" ]; then
4845 _usage
"Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ "
4853 _real_fullchain
="$7"
4858 _challenge_alias
="${12}"
4860 _csrsubj
=$
(_readSubjectFromCSR
"$_csrfile")
4861 if [ "$?" != "0" ]; then
4862 _err
"Can not read subject from csr: $_csrfile"
4865 _debug _csrsubj
"$_csrsubj"
4866 if _contains
"$_csrsubj" ' ' ||
! _contains
"$_csrsubj" '.'; then
4867 _info
"It seems that the subject: $_csrsubj is not a valid domain name. Drop it."
4871 _csrdomainlist
=$
(_readSubjectAltNamesFromCSR
"$_csrfile")
4872 if [ "$?" != "0" ]; then
4873 _err
"Can not read domain list from csr: $_csrfile"
4876 _debug
"_csrdomainlist" "$_csrdomainlist"
4878 if [ -z "$_csrsubj" ]; then
4879 _csrsubj
="$(_getfield "$_csrdomainlist" 1)"
4880 _debug _csrsubj
"$_csrsubj"
4881 _csrdomainlist
="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
4882 _debug
"_csrdomainlist" "$_csrdomainlist"
4885 if [ -z "$_csrsubj" ]; then
4886 _err
"Can not read subject from csr: $_csrfile"
4890 _csrkeylength
=$
(_readKeyLengthFromCSR
"$_csrfile")
4891 if [ "$?" != "0" ] ||
[ -z "$_csrkeylength" ]; then
4892 _err
"Can not read key length from csr: $_csrfile"
4896 if [ -z "$ACME_VERSION" ] && _contains
"$_csrsubj,$_csrdomainlist" "*."; then
4897 export ACME_VERSION
=2
4899 _initpath
"$_csrsubj" "$_csrkeylength"
4900 mkdir
-p "$DOMAIN_PATH"
4902 _info
"Copy csr to: $CSR_PATH"
4903 cp "$_csrfile" "$CSR_PATH"
4905 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"
4912 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
4913 _usage
"Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr"
4919 _csrsubj
=$
(_readSubjectFromCSR
"$_csrfile")
4920 if [ "$?" != "0" ] ||
[ -z "$_csrsubj" ]; then
4921 _err
"Can not read subject from csr: $_csrfile"
4925 _info
"Subject=$_csrsubj"
4927 _csrdomainlist
=$
(_readSubjectAltNamesFromCSR
"$_csrfile")
4928 if [ "$?" != "0" ]; then
4929 _err
"Can not read domain list from csr: $_csrfile"
4932 _debug
"_csrdomainlist" "$_csrdomainlist"
4934 _info
"SubjectAltNames=$_csrdomainlist"
4936 _csrkeylength
=$
(_readKeyLengthFromCSR
"$_csrfile")
4937 if [ "$?" != "0" ] ||
[ -z "$_csrkeylength" ]; then
4938 _err
"Can not read key length from csr: $_csrfile"
4941 _info
"KeyLength=$_csrkeylength"
4949 if [ "$_raw" ]; then
4950 printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew"
4951 for di
in "${CERT_HOME}"/*.
*/; do
4952 if ! [ -d "$di" ]; then
4953 _debug
"Not directory, skip: $di"
4959 if _endswith
"$d" "$ECC_SUFFIX"; then
4960 _isEcc
=$
(echo "$d" | cut
-d "$ECC_SEP" -f 2)
4961 d
=$
(echo "$d" | cut
-d "$ECC_SEP" -f 1)
4963 _initpath
"$d" "$_isEcc"
4964 if [ -f "$DOMAIN_CONF" ]; then
4966 printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
4971 if _exists
column; then
4972 list
"raw" |
column -t -s "$_sep"
4974 list
"raw" |
tr "$_sep" '\t'
4984 for _d_api
in $
(echo "$_hooks" |
tr ',' " "); do
4985 _deployApi
="$(_findHook "$_d" $_SUB_FOLDER_DEPLOY "$_d_api")"
4986 if [ -z "$_deployApi" ]; then
4987 _err
"The deploy hook $_d_api is not found."
4990 _debug _deployApi
"$_deployApi"
4993 if ! .
"$_deployApi"; then
4994 _err
"Load file $_deployApi error. Please check your api file and try again."
4998 d_command
="${_d_api}_deploy"
4999 if ! _exists
"$d_command"; then
5000 _err
"It seems that your api file is not correct, it must have a function named: $d_command"
5004 if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
5005 _err
"Error deploy for domain:$_d"
5009 _err
"Deploy error."
5012 _info
"$(__green Success)"
5022 if [ -z "$_hooks" ]; then
5023 _usage
"Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] "
5027 _initpath
"$_d" "$_isEcc"
5028 if [ ! -d "$DOMAIN_PATH" ]; then
5029 _err
"The domain '$_d' is not a cert name. You must use the cert name to specify the cert to install."
5030 _err
"Can not find path:'$DOMAIN_PATH'"
5036 _savedomainconf Le_DeployHook
"$_hooks"
5038 _deploy
"$_d" "$_hooks"
5043 if [ -z "$_main_domain" ]; then
5044 _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]"
5052 _real_fullchain
="$6"
5055 _initpath
"$_main_domain" "$_isEcc"
5056 if [ ! -d "$DOMAIN_PATH" ]; then
5057 _err
"The domain '$_main_domain' is not a cert name. You must use the cert name to specify the cert to install."
5058 _err
"Can not find path:'$DOMAIN_PATH'"
5062 _savedomainconf
"Le_RealCertPath" "$_real_cert"
5063 _savedomainconf
"Le_RealCACertPath" "$_real_ca"
5064 _savedomainconf
"Le_RealKeyPath" "$_real_key"
5065 _savedomainconf
"Le_ReloadCmd" "$_reload_cmd" "base64"
5066 _savedomainconf
"Le_RealFullChainPath" "$_real_fullchain"
5068 _installcert
"$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
5071 #domain cert key ca fullchain reloadcmd backup-prefix
5077 _real_fullchain
="$5"
5081 if [ "$_real_cert" = "$NO_VALUE" ]; then
5084 if [ "$_real_key" = "$NO_VALUE" ]; then
5087 if [ "$_real_ca" = "$NO_VALUE" ]; then
5090 if [ "$_reload_cmd" = "$NO_VALUE" ]; then
5093 if [ "$_real_fullchain" = "$NO_VALUE" ]; then
5097 _backup_path
="$DOMAIN_BACKUP_PATH/$_backup_prefix"
5098 mkdir
-p "$_backup_path"
5100 if [ "$_real_cert" ]; then
5101 _info
"Installing cert to:$_real_cert"
5102 if [ -f "$_real_cert" ] && [ ! "$IS_RENEW" ]; then
5103 cp "$_real_cert" "$_backup_path/cert.bak"
5105 cat "$CERT_PATH" >"$_real_cert" ||
return 1
5108 if [ "$_real_ca" ]; then
5109 _info
"Installing CA to:$_real_ca"
5110 if [ "$_real_ca" = "$_real_cert" ]; then
5111 echo "" >>"$_real_ca"
5112 cat "$CA_CERT_PATH" >>"$_real_ca" ||
return 1
5114 if [ -f "$_real_ca" ] && [ ! "$IS_RENEW" ]; then
5115 cp "$_real_ca" "$_backup_path/ca.bak"
5117 cat "$CA_CERT_PATH" >"$_real_ca" ||
return 1
5121 if [ "$_real_key" ]; then
5122 _info
"Installing key to:$_real_key"
5123 if [ -f "$_real_key" ] && [ ! "$IS_RENEW" ]; then
5124 cp "$_real_key" "$_backup_path/key.bak"
5126 if [ -f "$_real_key" ]; then
5127 cat "$CERT_KEY_PATH" >"$_real_key" ||
return 1
5129 cat "$CERT_KEY_PATH" >"$_real_key" ||
return 1
5130 chmod 600 "$_real_key"
5134 if [ "$_real_fullchain" ]; then
5135 _info
"Installing full chain to:$_real_fullchain"
5136 if [ -f "$_real_fullchain" ] && [ ! "$IS_RENEW" ]; then
5137 cp "$_real_fullchain" "$_backup_path/fullchain.bak"
5139 cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain" ||
return 1
5142 if [ "$_reload_cmd" ]; then
5143 _info
"Run reload cmd: $_reload_cmd"
5146 export CERT_KEY_PATH
5148 export CERT_FULLCHAIN_PATH
5149 export Le_Domain
="$_main_domain"
5150 cd "$DOMAIN_PATH" && eval "$_reload_cmd"
5152 _info
"$(__green "Reload success
")"
5154 _err
"Reload error for :$Le_Domain"
5162 prompt
="Enter Password:"
5163 while IFS
= read -p "$prompt" -r -s -n 1 char
; do
5164 if [ "$char" = $
'\0' ]; then
5173 _install_win_taskscheduler
() {
5177 if ! _exists cygpath
; then
5178 _err
"cygpath not found"
5181 if ! _exists schtasks
; then
5182 _err
"schtasks.exe is not found, are you on Windows?"
5185 _winbash
="$(cygpath -w $(which bash))"
5186 _debug _winbash
"$_winbash"
5187 if [ -z "$_winbash" ]; then
5188 _err
"can not find bash path"
5192 _debug
"_myname" "$_myname"
5193 if [ -z "$_myname" ]; then
5194 _err
"can not find my user name"
5197 _debug
"_lesh" "$_lesh"
5199 _info
"To install scheduler task in your Windows account, you must input your windows password."
5200 _info
"$PROJECT_NAME doesn't save your password."
5201 _info
"Please input your Windows password for: $(__green "$_myname")"
5202 _password
="$(__read_password)"
5203 #SCHTASKS.exe '/create' '/SC' 'DAILY' '/TN' "$_WINDOWS_SCHEDULER_NAME" '/F' '/ST' "00:$_randomminute" '/RU' "$_myname" '/RP' "$_password" '/TR' "$_winbash -l -c '$_lesh --cron --home \"$LE_WORKING_DIR\" $_centry'" >/dev/null
5204 echo SCHTASKS.exe
'/create' '/SC' 'DAILY' '/TN' "$_WINDOWS_SCHEDULER_NAME" '/F' '/ST' "00:$_randomminute" '/RU' "$_myname" '/RP' "$_password" '/TR' "\"$_winbash -l -c '$_lesh --cron --home \"$LE_WORKING_DIR\" $_centry'\"" | cmd.exe
>/dev
/null
5209 _uninstall_win_taskscheduler
() {
5210 if ! _exists schtasks
; then
5211 _err
"schtasks.exe is not found, are you on Windows?"
5214 if ! echo SCHTASKS
/query
/tn
"$_WINDOWS_SCHEDULER_NAME" | cmd.exe
>/dev
/null
; then
5215 _debug
"scheduler $_WINDOWS_SCHEDULER_NAME is not found."
5217 _info
"Removing $_WINDOWS_SCHEDULER_NAME"
5218 echo SCHTASKS
/delete
/f
/tn
"$_WINDOWS_SCHEDULER_NAME" | cmd.exe
>/dev
/null
5227 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
5228 lesh
="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
5230 _err
"Can not install cronjob, $PROJECT_ENTRY not found."
5233 if [ "$_c_home" ]; then
5234 _c_entry
="--config-home \"$_c_home\" "
5237 random_minute
=$
(_math
$_t % 60)
5239 if ! _exists
"$_CRONTAB" && _exists
"fcrontab"; then
5243 if ! _exists
"$_CRONTAB"; then
5244 if _exists cygpath
&& _exists schtasks.exe
; then
5245 _info
"It seems you are on Windows, let's install Windows scheduler task."
5246 if _install_win_taskscheduler
"$lesh" "$_c_entry" "$random_minute"; then
5247 _info
"Install Windows scheduler task success."
5250 _err
"Install Windows scheduler task failed."
5254 _err
"crontab/fcrontab doesn't exist, so, we can not install cron jobs."
5255 _err
"All your certs will not be renewed automatically."
5256 _err
"You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
5259 _info
"Installing cron job"
5260 if ! $_CRONTAB -l |
grep "$PROJECT_ENTRY --cron"; then
5261 if _exists uname
&& uname
-a |
grep SunOS
>/dev
/null
; then
5264 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
5269 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
5273 if [ "$?" != "0" ]; then
5274 _err
"Install cron job failed. You need to manually renew your certs."
5275 _err
"Or you can add cronjob by yourself:"
5276 _err
"$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
5281 uninstallcronjob
() {
5283 if ! _exists
"$_CRONTAB" && _exists
"fcrontab"; then
5287 if ! _exists
"$_CRONTAB"; then
5288 if _exists cygpath
&& _exists schtasks.exe
; then
5289 _info
"It seems you are on Windows, let's uninstall Windows scheduler task."
5290 if _uninstall_win_taskscheduler
; then
5291 _info
"Uninstall Windows scheduler task success."
5294 _err
"Uninstall Windows scheduler task failed."
5300 _info
"Removing cron job"
5301 cr
="$($_CRONTAB -l | grep "$PROJECT_ENTRY --cron")"
5303 if _exists uname
&& uname
-a |
grep solaris
>/dev
/null
; then
5304 $_CRONTAB -l |
sed "/$PROJECT_ENTRY --cron/d" |
$_CRONTAB --
5306 $_CRONTAB -l |
sed "/$PROJECT_ENTRY --cron/d" |
$_CRONTAB -
5308 LE_WORKING_DIR
="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
5309 _info LE_WORKING_DIR "$LE_WORKING_DIR"
5310 if _contains "$cr" "--config-home"; then
5311 LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
5312 _debug LE_CONFIG_HOME
"$LE_CONFIG_HOME"
5321 if [ -z "$Le_Domain" ]; then
5322 _usage
"Usage: $PROJECT_ENTRY --revoke -d domain.com [--ecc]"
5328 _initpath
"$Le_Domain" "$_isEcc"
5329 if [ ! -f "$DOMAIN_CONF" ]; then
5330 _err
"$Le_Domain is not a issued domain, skip."
5334 if [ ! -f "$CERT_PATH" ]; then
5335 _err
"Cert for $Le_Domain $CERT_PATH is not found, skip."
5339 cert
="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
5341 if [ -z "$cert" ]; then
5342 _err
"Cert for $Le_Domain is empty found, skip."
5348 if [ "$ACME_VERSION" = "2" ]; then
5349 data
="{\"certificate\": \"$cert\"}"
5351 data
="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
5353 uri
="${ACME_REVOKE_CERT}"
5355 if [ -f "$CERT_KEY_PATH" ]; then
5356 _info
"Try domain key first."
5357 if _send_signed_request
"$uri" "$data" "" "$CERT_KEY_PATH"; then
5358 if [ -z "$response" ]; then
5359 _info
"Revoke success."
5363 _err
"Revoke error by domain key."
5368 _info
"Domain key file doesn't exists."
5371 _info
"Try account key."
5373 if _send_signed_request
"$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
5374 if [ -z "$response" ]; then
5375 _info
"Revoke success."
5379 _err
"Revoke error."
5389 if [ -z "$Le_Domain" ]; then
5390 _usage
"Usage: $PROJECT_ENTRY --remove -d domain.com [--ecc]"
5396 _initpath
"$Le_Domain" "$_isEcc"
5397 _removed_conf
="$DOMAIN_CONF.removed"
5398 if [ ! -f "$DOMAIN_CONF" ]; then
5399 if [ -f "$_removed_conf" ]; then
5400 _err
"$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH"
5402 _err
"$Le_Domain is not a issued domain, skip."
5407 if mv "$DOMAIN_CONF" "$_removed_conf"; then
5408 _info
"$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)"
5409 _info
"You can remove them by yourself."
5412 _err
"Remove $Le_Domain failed."
5423 if [ "$ACME_VERSION" = "2" ]; then
5424 _identifiers
="{\"type\":\"dns\",\"value\":\"$_d_domain\"}"
5425 if ! _send_signed_request
"$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
5426 _err
"Can not get domain new order."
5429 _authorizations_seg
="$(echo "$response" | _egrep_o '"authorizations
" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
5430 _debug2 _authorizations_seg "$_authorizations_seg"
5431 if [ -z "$_authorizations_seg" ]; then
5432 _err "_authorizations_seg not found."
5434 _on_issue_err "$_post_hook"
5438 authzUri="$_authorizations_seg"
5439 _debug2 "authzUri" "$authzUri"
5440 if ! _send_signed_request "$authzUri"; then
5441 _err "get to authz error."
5442 _err "_authorizations_seg" "$_authorizations_seg"
5443 _err "authzUri" "$authzUri"
5445 _on_issue_err "$_post_hook"
5449 response="$(echo "$response" | _normalizeJson)"
5450 _debug2 response "$response"
5453 if ! __get_domain_new_authz "$_d_domain"; then
5454 _err "Can not get domain new authz token."
5458 authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
5459 _debug "authzUri" "$authzUri"
5460 if [ "$code" ] && [ ! "$code" = '201' ]; then
5461 _err "new-authz error: $response"
5467 entries="$(echo "$response" | _egrep_o "{ *\"type\":\"[^\"]*\", *\"status\": *\"valid\", *\"$_URL_NAME\"[^}]*")"
5468 if [ -z "$entries" ]; then
5469 _info "No valid entries found."
5470 if [ -z "$thumbprint" ]; then
5471 thumbprint="$(__calc_account_thumbprint)"
5473 _debug "Trigger validation."
5475 entry="$(echo "$response" | _egrep_o '[^\
{]*"type":"'$vtype'"[^\
}]*')"
5476 _debug entry "$entry"
5477 if [ -z "$entry" ]; then
5478 _err "Error, can not get domain token $d"
5481 token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
5482 _debug token
"$token"
5484 uri
="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^
\"]*" | cut -d : -f 2,3 | tr -d '"')"
5487 keyauthorization="$token.$thumbprint"
5488 _debug keyauthorization "$keyauthorization"
5489 __trigger_validation "$uri" "$keyauthorization"
5494 _d_max_retry=$(echo "$entries" | wc -l)
5495 while [ "$_d_i" -lt "$_d_max_retry" ]; do
5496 _info "Deactivate: $_d_domain"
5497 _d_i="$(_math $_d_i + 1)"
5498 entry="$(echo "$entries" | sed -n "${_d_i}p")"
5499 _debug entry "$entry"
5501 if [ -z "$entry" ]; then
5502 _info "No more valid entry found."
5506 _vtype="$(echo "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
5507 _debug _vtype "$_vtype"
5508 _info "Found $_vtype"
5510 uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')"
5513 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
5514 _info
"Skip $_vtype"
5518 _info
"Deactivate: $_vtype"
5520 if [ "$ACME_VERSION" = "2" ]; then
5521 _djson
="{\"status\":\"deactivated\"}"
5523 _djson
="{\"resource\": \"authz\", \"status\":\"deactivated\"}"
5526 if _send_signed_request
"$authzUri" "$_djson" && _contains
"$response" '"deactivated"'; then
5527 _info
"Deactivate: $_vtype success."
5529 _err
"Can not deactivate $_vtype."
5535 if [ "$_d_i" -eq "$_d_max_retry" ]; then
5536 _info
"Deactivated success!"
5538 _err
"Deactivate failed."
5548 _debug _d_domain_list
"$_d_domain_list"
5549 if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
5550 _usage
"Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
5553 for _d_dm
in $
(echo "$_d_domain_list" |
tr ',' ' '); do
5554 if [ -z "$_d_dm" ] ||
[ "$_d_dm" = "$NO_VALUE" ]; then
5557 if ! _deactivate
"$_d_dm" "$_d_type"; then
5563 # Detect profile file if not specified as environment variable
5565 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
5571 SHELLTYPE
="$(basename "/$SHELL")"
5573 if [ "$SHELLTYPE" = "bash" ]; then
5574 if [ -f "$HOME/.bashrc" ]; then
5575 DETECTED_PROFILE
="$HOME/.bashrc"
5576 elif [ -f "$HOME/.bash_profile" ]; then
5577 DETECTED_PROFILE
="$HOME/.bash_profile"
5579 elif [ "$SHELLTYPE" = "zsh" ]; then
5580 DETECTED_PROFILE
="$HOME/.zshrc"
5583 if [ -z "$DETECTED_PROFILE" ]; then
5584 if [ -f "$HOME/.profile" ]; then
5585 DETECTED_PROFILE
="$HOME/.profile"
5586 elif [ -f "$HOME/.bashrc" ]; then
5587 DETECTED_PROFILE
="$HOME/.bashrc"
5588 elif [ -f "$HOME/.bash_profile" ]; then
5589 DETECTED_PROFILE
="$HOME/.bash_profile"
5590 elif [ -f "$HOME/.zshrc" ]; then
5591 DETECTED_PROFILE
="$HOME/.zshrc"
5595 echo "$DETECTED_PROFILE"
5600 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
5603 #LOG_FILE=\"$DEFAULT_LOG_FILE\"
5610 " >"$ACCOUNT_CONF_PATH"
5618 if ! _exists
"curl" && ! _exists
"wget"; then
5619 _err
"Please install curl or wget first, we need to access http resources."
5623 if [ -z "$_nocron" ]; then
5624 if ! _exists
"crontab" && ! _exists
"fcrontab"; then
5625 if _exists cygpath
&& _exists schtasks.exe
; then
5626 _info
"It seems you are on Windows, we will install Windows scheduler task."
5628 _err
"It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
5629 _err
"We need to set cron job to renew the certs automatically."
5630 _err
"Otherwise, your certs will not be able to be renewed automatically."
5631 if [ -z "$FORCE" ]; then
5632 _err
"Please add '--force' and try install again to go without crontab."
5633 _err
"./$PROJECT_ENTRY --install --force"
5640 if ! _exists
"${ACME_OPENSSL_BIN:-openssl}"; then
5641 _err
"Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN"
5642 _err
"We need openssl to generate keys."
5646 if ! _exists
"socat"; then
5647 _err
"It is recommended to install socat first."
5648 _err
"We use socat for standalone server if you use standalone mode."
5649 _err
"If you don't use standalone mode, just ignore this warning."
5658 if [ -z "$_shebang" ]; then
5659 _usage
"Usage: file shebang"
5662 cp "$_file" "$_file.tmp"
5663 echo "$_shebang" >"$_file"
5664 sed -n 2,99999p
"$_file.tmp" >>"$_file"
5673 _envfile
="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
5674 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
5675 echo "$(cat "$_envfile")" |
sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
5676 echo "$(cat "$_envfile")" |
sed "s|^alias le.*$||" >"$_envfile"
5677 echo "$(cat "$_envfile")" |
sed "s|^alias le.sh.*$||" >"$_envfile"
5680 if [ "$_c_home" ]; then
5681 _c_entry
=" --config-home '$_c_home'"
5684 _setopt
"$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
5685 if [ "$_c_home" ]; then
5686 _setopt
"$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
5688 _sed_i
"/^export LE_CONFIG_HOME/d" "$_envfile"
5690 _setopt
"$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
5692 _profile
="$(_detect_profile)"
5693 if [ "$_profile" ]; then
5694 _debug
"Found profile: $_profile"
5695 _info
"Installing alias to '$_profile'"
5696 _setopt
"$_profile" ". \"$_envfile\""
5697 _info
"OK, Close and reopen your terminal to start using $PROJECT_NAME"
5699 _info
"No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
5703 _cshfile
="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
5704 _csh_profile
="$HOME/.cshrc"
5705 if [ -f "$_csh_profile" ]; then
5706 _info
"Installing alias to '$_csh_profile'"
5707 _setopt
"$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
5708 if [ "$_c_home" ]; then
5709 _setopt
"$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
5711 _sed_i
"/^setenv LE_CONFIG_HOME/d" "$_cshfile"
5713 _setopt
"$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
5714 _setopt
"$_csh_profile" "source \"$_cshfile\""
5718 _tcsh_profile
="$HOME/.tcshrc"
5719 if [ -f "$_tcsh_profile" ]; then
5720 _info
"Installing alias to '$_tcsh_profile'"
5721 _setopt
"$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
5722 if [ "$_c_home" ]; then
5723 _setopt
"$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
5725 _setopt
"$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
5726 _setopt
"$_tcsh_profile" "source \"$_cshfile\""
5731 # nocron confighome noprofile
5734 if [ -z "$LE_WORKING_DIR" ]; then
5735 LE_WORKING_DIR
="$DEFAULT_INSTALL_HOME"
5741 if ! _initpath
; then
5742 _err
"Install failed."
5745 if [ "$_nocron" ]; then
5746 _debug
"Skip install cron job"
5749 if [ "$ACME_IN_CRON" != "1" ]; then
5750 if ! _precheck
"$_nocron"; then
5751 _err
"Pre-check failed, can not install."
5756 if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then
5757 _info
"Using config home: $LE_CONFIG_HOME"
5758 _c_home
="$LE_CONFIG_HOME"
5762 if [ -d "$HOME/.le" ]; then
5763 for envfile
in "le.env" "le.sh.env"; do
5764 if [ -f "$HOME/.le/$envfile" ]; then
5765 if grep "le.sh" "$HOME/.le/$envfile" >/dev
/null
; then
5767 _info
"You are upgrading from le.sh"
5768 _info
"Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
5769 mv "$HOME/.le" "$LE_WORKING_DIR"
5770 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
5777 _info
"Installing to $LE_WORKING_DIR"
5779 if [ ! -d "$LE_WORKING_DIR" ]; then
5780 if ! mkdir
-p "$LE_WORKING_DIR"; then
5781 _err
"Can not create working dir: $LE_WORKING_DIR"
5785 chmod 700 "$LE_WORKING_DIR"
5788 if [ ! -d "$LE_CONFIG_HOME" ]; then
5789 if ! mkdir
-p "$LE_CONFIG_HOME"; then
5790 _err
"Can not create config dir: $LE_CONFIG_HOME"
5794 chmod 700 "$LE_CONFIG_HOME"
5797 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x
"$LE_WORKING_DIR/$PROJECT_ENTRY"
5799 if [ "$?" != "0" ]; then
5800 _err
"Install failed, can not copy $PROJECT_ENTRY"
5804 _info
"Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
5806 if [ "$ACME_IN_CRON" != "1" ] && [ -z "$_noprofile" ]; then
5807 _installalias
"$_c_home"
5810 for subf
in $_SUB_FOLDERS; do
5811 if [ -d "$subf" ]; then
5812 mkdir
-p "$LE_WORKING_DIR/$subf"
5813 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
5817 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
5821 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
5822 _setopt
"$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
5825 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
5826 _saveaccountconf
"CERT_HOME" "$CERT_HOME"
5829 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
5830 _saveaccountconf
"ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
5833 if [ -z "$_nocron" ]; then
5834 installcronjob
"$_c_home"
5837 if [ -z "$NO_DETECT_SH" ]; then
5839 if _exists bash
; then
5840 _bash_path
="$(bash -c "command -v bash
2>/dev
/null
")"
5841 if [ -z "$_bash_path" ]; then
5842 _bash_path
="$(bash -c 'echo $SHELL')"
5845 if [ "$_bash_path" ]; then
5846 _info
"Good, bash is found, so change the shebang to use bash as preferred."
5847 _shebang
='#!'"$_bash_path"
5848 _setShebang
"$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
5849 for subf
in $_SUB_FOLDERS; do
5850 if [ -d "$LE_WORKING_DIR/$subf" ]; then
5851 for _apifile
in "$LE_WORKING_DIR/$subf/"*.sh
; do
5852 _setShebang
"$_apifile" "$_shebang"
5865 if [ -z "$_nocron" ]; then
5872 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
5873 _info
"The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself."
5880 _profile
="$(_detect_profile)"
5881 if [ "$_profile" ]; then
5882 _info
"Uninstalling alias from: '$_profile'"
5883 text
="$(cat "$_profile")"
5884 echo "$text" |
sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
5887 _csh_profile
="$HOME/.cshrc"
5888 if [ -f "$_csh_profile" ]; then
5889 _info
"Uninstalling alias from: '$_csh_profile'"
5890 text
="$(cat "$_csh_profile")"
5891 echo "$text" |
sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
5894 _tcsh_profile
="$HOME/.tcshrc"
5895 if [ -f "$_tcsh_profile" ]; then
5896 _info
"Uninstalling alias from: '$_csh_profile'"
5897 text
="$(cat "$_tcsh_profile")"
5898 echo "$text" |
sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
5904 export ACME_IN_CRON
=1
5906 _info
"$(__green "===Starting cron
===")"
5907 if [ "$AUTO_UPGRADE" = "1" ]; then
5908 export LE_WORKING_DIR
5911 _err
"Cron:Upgrade failed!"
5915 .
"$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev
/null
5921 _info
"Auto upgraded to: $VER"
5926 _info
"$(__green "===End cron
===")"
5935 # subject content hooks code
5942 if [ "$NOTIFY_LEVEL" = "$NOTIFY_LEVEL_DISABLE" ]; then
5943 _debug
"The NOTIFY_LEVEL is $NOTIFY_LEVEL, disabled, just return."
5947 if [ -z "$_nhooks" ]; then
5948 _debug
"The NOTIFY_HOOK is empty, just return."
5953 for _n_hook
in $
(echo "$_nhooks" |
tr ',' " "); do
5954 _n_hook_file
="$(_findHook "" $_SUB_FOLDER_NOTIFY "$_n_hook")"
5955 _info
"Sending via: $_n_hook"
5956 _debug
"Found $_n_hook_file for $_n_hook"
5957 if [ -z "$_n_hook_file" ]; then
5958 _err
"Can not find the hook file for $_n_hook"
5962 if ! .
"$_n_hook_file"; then
5963 _err
"Load file $_n_hook_file error. Please check your api file and try again."
5967 d_command
="${_n_hook}_send"
5968 if ! _exists
"$d_command"; then
5969 _err
"It seems that your api file is not correct, it must have a function named: $d_command"
5973 if ! $d_command "$_nsubject" "$_ncontent" "$_nerror"; then
5974 _err
"Error send message by $d_command"
5980 _err
"Set $_n_hook_file error."
5983 _info
"$_n_hook $(__green Success)"
5991 _set_notify_hook
() {
5994 _test_subject
="Hello, this is notification from $PROJECT_NAME"
5995 _test_content
="If you receive this message, your notification works."
5997 _send_notify
"$_test_subject" "$_test_content" "$_nhooks" 0
6001 #[hook] [level] [mode]
6009 if [ -z "$_nhook$_nlevel$_nmode" ]; then
6010 _usage
"Usage: $PROJECT_ENTRY --set-notify [--notify-hook mailgun] [--notify-level $NOTIFY_LEVEL_DEFAULT] [--notify-mode $NOTIFY_MODE_DEFAULT]"
6011 _usage
"$_NOTIFY_WIKI"
6015 if [ "$_nlevel" ]; then
6016 _info
"Set notify level to: $_nlevel"
6017 export "NOTIFY_LEVEL=$_nlevel"
6018 _saveaccountconf
"NOTIFY_LEVEL" "$NOTIFY_LEVEL"
6021 if [ "$_nmode" ]; then
6022 _info
"Set notify mode to: $_nmode"
6023 export "NOTIFY_MODE=$_nmode"
6024 _saveaccountconf
"NOTIFY_MODE" "$NOTIFY_MODE"
6027 if [ "$_nhook" ]; then
6028 _info
"Set notify hook to: $_nhook"
6029 if [ "$_nhook" = "$NO_VALUE" ]; then
6030 _info
"Clear notify hook"
6031 _clearaccountconf
"NOTIFY_HOOK"
6033 if _set_notify_hook
"$_nhook"; then
6034 export NOTIFY_HOOK
="$_nhook"
6035 _saveaccountconf
"NOTIFY_HOOK" "$NOTIFY_HOOK"
6038 _err
"Can not set notify hook to: $_nhook"
6049 echo "Usage: $PROJECT_ENTRY command ...[parameters]....
6051 --help, -h Show this help message.
6052 --version, -v Show version info.
6053 --install Install $PROJECT_NAME to your system.
6054 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
6055 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
6056 --issue Issue a cert.
6057 --signcsr Issue a cert from an existing csr.
6058 --deploy Deploy the cert to your server.
6059 --install-cert Install the issued cert to apache/nginx or any other server.
6060 --renew, -r Renew a cert.
6061 --renew-all Renew all the certs.
6062 --revoke Revoke a cert.
6063 --remove Remove the cert from list of certs known to $PROJECT_NAME.
6064 --list List all the certs.
6065 --showcsr Show the content of a csr.
6066 --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.
6067 --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
6068 --cron Run cron job to renew all the certs.
6069 --toPkcs Export the certificate and key to a pfx file.
6070 --toPkcs8 Convert to pkcs8 format.
6071 --update-account Update account info.
6072 --register-account Register account key.
6073 --deactivate-account Deactivate the account.
6074 --create-account-key Create an account private key, professional use.
6075 --create-domain-key Create an domain private key, professional use.
6076 --createCSR, -ccsr Create CSR , professional use.
6077 --deactivate Deactivate the domain authz, professional use.
6078 --set-notify Set the cron notification hook, level or mode.
6082 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
6083 --challenge-alias domain.tld The challenge domain alias for DNS alias mode: $_DNS_ALIAS_WIKI
6084 --domain-alias domain.tld The domain alias for DNS alias mode: $_DNS_ALIAS_WIKI
6085 --force, -f Used to force to install or force to renew a cert immediately.
6086 --staging, --test Use staging server, just for test.
6087 --debug Output debug info.
6088 --output-insecure Output all the sensitive messages. By default all the credentials/sensitive messages are hidden from the output/debug/log for secure.
6089 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
6090 --standalone Use standalone mode.
6091 --alpn Use standalone alpn mode.
6092 --stateless Use stateless mode, see: $_STATELESS_WIKI
6093 --apache Use apache mode.
6094 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
6095 --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.
6097 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
6098 --accountkeylength, -ak [2048] Specifies the account key length.
6099 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
6100 --log-level 1|2 Specifies the log level, default is 1.
6101 --syslog [0|3|6|7] Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
6103 These parameters are to install the cert to nginx/apache or any other server after issue/renew a cert:
6105 --cert-file After issue/renew, the cert will be copied to this path.
6106 --key-file After issue/renew, the key will be copied to this path.
6107 --ca-file After issue/renew, the intermediate cert will be copied to this path.
6108 --fullchain-file After issue/renew, the fullchain cert will be copied to this path.
6110 --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
6112 --server SERVER ACME Directory Resource URI. (default: https://acme-v01.api.letsencrypt.org/directory)
6113 --accountconf Specifies a customized account config file.
6114 --home Specifies the home dir for $PROJECT_NAME.
6115 --cert-home Specifies the home dir to save all the certs, only valid for '--install' command.
6116 --config-home Specifies the home dir to save all the configurations.
6117 --useragent Specifies the user agent string. it will be saved for future use too.
6118 --accountemail Specifies the account email, only valid for the '--install' and '--update-account' command.
6119 --accountkey Specifies the account key path, only valid for the '--install' command.
6120 --days Specifies the days to renew the cert when using '--issue' command. The default value is $DEFAULT_RENEW days.
6121 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
6122 --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
6123 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
6124 --listraw Only used for '--list' command, list the certs in raw format.
6125 --stopRenewOnError, -se Only valid for '--renew-all' command. Stop if one cert has error in renewal.
6126 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
6127 --ca-bundle Specifies the path to the CA certificate bundle to verify api server's certificate.
6128 --ca-path Specifies directory containing CA certificates in PEM format, used by wget or curl.
6129 --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.
6130 --noprofile Only valid for '--install' command, which means: do not install aliases to user profile.
6131 --no-color Do not output color text.
6132 --force-color Force output of color text. Useful for non-interactive use with the aha tool for HTML E-Mails.
6133 --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
6134 --csr Specifies the input csr.
6135 --pre-hook Command to be run before obtaining any certificates.
6136 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obtain/renew is success or failed.
6137 --renew-hook Command to be run once for each successfully renewed certificate.
6138 --deploy-hook The hook file to deploy cert
6139 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
6140 --always-force-new-domain-key Generate new domain key when renewal. Otherwise, the domain key is not changed by default.
6141 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
6142 --listen-v4 Force standalone/tls server to listen at ipv4.
6143 --listen-v6 Force standalone/tls server to listen at ipv6.
6144 --openssl-bin Specifies a custom openssl bin location.
6145 --use-wget Force to use wget, if you have both curl and wget installed.
6146 --yes-I-know-dns-manual-mode-enough-go-ahead-please Force to use dns manual mode: $_DNS_MANUAL_WIKI
6147 --branch, -b Only valid for '--upgrade' command, specifies the branch name to upgrade to.
6149 --notify-level 0|1|2|3 Set the notification level: Default value is $NOTIFY_LEVEL_DEFAULT.
6150 0: disabled, no notification will be sent.
6151 1: send notifications only when there is an error.
6152 2: send notifications when a cert is successfully renewed, or there is an error.
6153 3: send notifications when a cert is skipped, renewed, or error.
6154 --notify-mode 0|1 Set notification mode. Default value is $NOTIFY_MODE_DEFAULT.
6155 0: Bulk mode. Send all the domain's notifications in one message(mail).
6156 1: Cert mode. Send a message for every single cert.
6157 --notify-hook [hookname] Set the notify hook
6164 _info
"Installing from online archive."
6167 if [ ! "$BRANCH" ]; then
6171 target
="$PROJECT/archive/$BRANCH.tar.gz"
6172 _info
"Downloading $target"
6173 localname
="$BRANCH.tar.gz"
6174 if ! _get
"$target" >$localname; then
6175 _err
"Download error."
6179 _info
"Extracting $localname"
6180 if ! (tar xzf
$localname || gtar xzf
$localname); then
6181 _err
"Extraction error."
6185 cd "$PROJECT_NAME-$BRANCH"
6186 chmod +x
$PROJECT_ENTRY
6187 if .
/$PROJECT_ENTRY install "$_nocron" "" "$_noprofile"; then
6188 _info
"Install success!"
6193 rm -rf "$PROJECT_NAME-$BRANCH"
6201 export LE_WORKING_DIR
6202 cd "$LE_WORKING_DIR"
6203 _installOnline
"nocron" "noprofile"
6205 _info
"Upgrade success!"
6208 _err
"Upgrade failed!"
6213 _processAccountConf
() {
6214 if [ "$_useragent" ]; then
6215 _saveaccountconf
"USER_AGENT" "$_useragent"
6216 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
6217 _saveaccountconf
"USER_AGENT" "$USER_AGENT"
6220 if [ "$_accountemail" ]; then
6221 _saveaccountconf
"ACCOUNT_EMAIL" "$_accountemail"
6222 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
6223 _saveaccountconf
"ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
6226 if [ "$_openssl_bin" ]; then
6227 _saveaccountconf
"ACME_OPENSSL_BIN" "$_openssl_bin"
6228 elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
6229 _saveaccountconf
"ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
6232 if [ "$_auto_upgrade" ]; then
6233 _saveaccountconf
"AUTO_UPGRADE" "$_auto_upgrade"
6234 elif [ "$AUTO_UPGRADE" ]; then
6235 _saveaccountconf
"AUTO_UPGRADE" "$AUTO_UPGRADE"
6238 if [ "$_use_wget" ]; then
6239 _saveaccountconf
"ACME_USE_WGET" "$_use_wget"
6240 elif [ "$ACME_USE_WGET" ]; then
6241 _saveaccountconf
"ACME_USE_WGET" "$ACME_USE_WGET"
6247 if [ "$SUDO_GID" ] && [ "$SUDO_COMMAND" ] && [ "$SUDO_USER" ] && [ "$SUDO_UID" ]; then
6248 if [ "$SUDO_USER" = "root" ] && [ "$SUDO_UID" = "0" ]; then
6249 #it's root using sudo, no matter it's using sudo or not, just fine
6252 if [ "$SUDO_COMMAND" = "/bin/su" ] ||
[ "$SUDO_COMMAND" = "/bin/bash" ]; then
6253 #it's a normal user doing "sudo su", or `sudo -i` or `sudo -s`
6266 _altdomains
="$NO_VALUE"
6270 _accountkeylength
=""
6287 _stopRenewOnError
=""
6313 while [ ${#} -gt 0 ]; do
6345 --installcert |
-i |
--install-cert)
6351 --renewAll |
--renewall |
--renew-all)
6363 --installcronjob |
--install-cronjob)
6364 _CMD
="installcronjob"
6366 --uninstallcronjob |
--uninstall-cronjob)
6367 _CMD
="uninstallcronjob"
6378 --createAccountKey |
--createaccountkey |
-cak |
--create-account-key)
6379 _CMD
="createAccountKey"
6381 --createDomainKey |
--createdomainkey |
-cdk |
--create-domain-key)
6382 _CMD
="createDomainKey"
6384 --createCSR |
--createcsr |
-ccr)
6390 --updateaccount |
--update-account)
6391 _CMD
="updateaccount"
6393 --registeraccount |
--register-account)
6394 _CMD
="registeraccount"
6396 --deactivate-account)
6397 _CMD
="deactivateaccount"
6405 if [ "$_dvalue" ]; then
6406 if _startswith
"$_dvalue" "-"; then
6407 _err
"'$_dvalue' is not a valid domain for parameter '$1'"
6410 if _is_idn
"$_dvalue" && ! _exists idn
; then
6411 _err
"It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
6415 if _startswith
"$_dvalue" "*."; then
6416 _debug
"Wildcard domain"
6417 export ACME_VERSION
=2
6419 if [ -z "$_domain" ]; then
6422 if [ "$_altdomains" = "$NO_VALUE" ]; then
6423 _altdomains
="$_dvalue"
6425 _altdomains
="$_altdomains,$_dvalue"
6441 _server
="$ACME_DIRECTORY"
6442 export ACME_DIRECTORY
6446 if [ -z "$2" ] || _startswith
"$2" "-"; then
6447 DEBUG
="$DEBUG_LEVEL_DEFAULT"
6454 export OUTPUT_INSECURE
=1
6458 if [ -z "$_webroot" ]; then
6461 _webroot
="$_webroot,$wvalue"
6467 _challenge_alias
="$_challenge_alias$cvalue,"
6471 cvalue
="$DNS_ALIAS_PREFIX$2"
6472 _challenge_alias
="$_challenge_alias$cvalue,"
6477 if [ -z "$_webroot" ]; then
6480 _webroot
="$_webroot,$wvalue"
6485 if [ -z "$_webroot" ]; then
6488 _webroot
="$_webroot,$wvalue"
6492 wvalue
="$MODE_STATELESS"
6493 if [ -z "$_webroot" ]; then
6496 _webroot
="$_webroot,$wvalue"
6501 _local_address
="$_local_address$lvalue,"
6506 if [ -z "$_webroot" ]; then
6509 _webroot
="$_webroot,$wvalue"
6514 if [ "$2" ] && ! _startswith
"$2" "-"; then
6518 if [ -z "$_webroot" ]; then
6521 _webroot
="$_webroot,$wvalue"
6526 if [ "$2" ] && ! _startswith
"$2" "-"; then
6530 if [ -z "$_webroot" ]; then
6533 _webroot
="$_webroot,$wvalue"
6538 Le_DNSSleep
="$_dnssleep"
6546 --accountkeylength |
-ak)
6547 _accountkeylength
="$2"
6551 --cert-file |
--certpath)
6555 --key-file |
--keypath)
6559 --ca-file |
--capath)
6563 --fullchain-file |
--fullchainpath)
6564 _fullchain_file
="$2"
6567 --reloadcmd |
--reloadCmd)
6577 ACCOUNT_CONF_PATH
="$_accountconf"
6584 --certhome |
--cert-home)
6586 CERT_HOME
="$_certhome"
6591 LE_CONFIG_HOME
="$_confighome"
6596 USER_AGENT
="$_useragent"
6601 ACCOUNT_EMAIL
="$_accountemail"
6606 ACCOUNT_KEY_PATH
="$_accountkey"
6611 Le_RenewalDays
="$_days"
6616 Le_HTTPPort
="$_httpport"
6621 Le_TLSPort
="$_tlsport"
6627 --stopRenewOnError |
--stoprenewonerror |
-se)
6628 _stopRenewOnError
="1"
6635 _ca_bundle
="$(_readlink "$2")"
6636 CA_BUNDLE
="$_ca_bundle"
6651 export ACME_NO_COLOR
=1
6654 export ACME_FORCE_COLOR
=1
6676 if [ -z "$2" ] || _startswith
"$2" "-"; then
6677 _usage
"Please specify a value for '--deploy-hook'"
6680 _deploy_hook
="$_deploy_hook$2,"
6683 --ocsp-must-staple |
--ocsp)
6686 --always-force-new-domain-key)
6687 if [ -z "$2" ] || _startswith
"$2" "-"; then
6688 Le_ForceNewDomainKey
=1
6690 Le_ForceNewDomainKey
="$2"
6694 --yes-I-know-dns-manual-mode-enough-go-ahead-please)
6695 export FORCE_DNS_MANUAL
=1
6700 if _startswith
"$_logfile" '-'; then
6705 LOG_FILE
="$_logfile"
6706 if [ -z "$LOG_LEVEL" ]; then
6707 LOG_LEVEL
="$DEFAULT_LOG_LEVEL"
6712 LOG_LEVEL
="$_log_level"
6716 if ! _startswith
"$2" '-'; then
6720 if [ -z "$_syslog" ]; then
6721 _syslog
="$SYSLOG_LEVEL_DEFAULT"
6726 if [ -z "$_auto_upgrade" ] || _startswith
"$_auto_upgrade" '-'; then
6731 AUTO_UPGRADE
="$_auto_upgrade"
6735 Le_Listen_V4
="$_listen_v4"
6739 Le_Listen_V6
="$_listen_v6"
6743 ACME_OPENSSL_BIN
="$_openssl_bin"
6756 if _startswith
"$_nhook" "-"; then
6757 _err
"'$_nhook' is not a hook name for '$1'"
6760 if [ "$_notify_hook" ]; then
6761 _notify_hook
="$_notify_hook,$_nhook"
6763 _notify_hook
="$_nhook"
6769 if _startswith
"$_nlevel" "-"; then
6770 _err
"'$_nlevel' is not a integer for '$1'"
6773 _notify_level
="$_nlevel"
6778 if _startswith
"$_nmode" "-"; then
6779 _err
"'$_nmode' is not a integer for '$1'"
6782 _notify_mode
="$_nmode"
6786 _err
"Unknown parameter : $1"
6794 if [ "${_CMD}" != "install" ]; then
6795 if [ "$__INTERACTIVE" ] && ! _checkSudo
; then
6796 if [ -z "$FORCE" ]; then
6797 #Use "echo" here, instead of _info. it's too early
6798 echo "It seems that you are using sudo, please read this link first:"
6804 if [ "$_log" ]; then
6805 if [ -z "$_logfile" ]; then
6806 _logfile
="$DEFAULT_LOG_FILE"
6809 if [ "$_logfile" ]; then
6810 _saveaccountconf
"LOG_FILE" "$_logfile"
6811 LOG_FILE
="$_logfile"
6814 if [ "$_log_level" ]; then
6815 _saveaccountconf
"LOG_LEVEL" "$_log_level"
6816 LOG_LEVEL
="$_log_level"
6819 if [ "$_syslog" ]; then
6820 if _exists logger
; then
6821 if [ "$_syslog" = "0" ]; then
6822 _clearaccountconf
"SYS_LOG"
6824 _saveaccountconf
"SYS_LOG" "$_syslog"
6828 _err
"The 'logger' command is not found, can not enable syslog."
6829 _clearaccountconf
"SYS_LOG"
6837 _debug2 LE_WORKING_DIR
"$LE_WORKING_DIR"
6839 if [ "$DEBUG" ]; then
6841 if [ "$_server" ]; then
6842 _debug
"Using server: $_server"
6845 _debug
"Running cmd: ${_CMD}"
6847 install) install "$_nocron" "$_confighome" "$_noprofile" ;;
6848 uninstall
) uninstall
"$_nocron" ;;
6851 issue
"$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias"
6854 deploy
"$_domain" "$_deploy_hook" "$_ecc"
6857 signcsr
"$_csr" "$_webroot" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias"
6860 showcsr
"$_csr" "$_domain"
6863 installcert
"$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
6866 renew
"$_domain" "$_ecc"
6869 renewAll
"$_stopRenewOnError"
6872 revoke
"$_domain" "$_ecc"
6875 remove
"$_domain" "$_ecc"
6878 deactivate
"$_domain,$_altdomains"
6881 registeraccount
"$_accountkeylength"
6892 installcronjob
) installcronjob
"$_confighome" ;;
6893 uninstallcronjob
) uninstallcronjob
;;
6896 toPkcs
"$_domain" "$_password" "$_ecc"
6899 toPkcs8
"$_domain" "$_ecc"
6902 createAccountKey
"$_accountkeylength"
6905 createDomainKey
"$_domain" "$_keylength"
6908 createCSR
"$_domain" "$_altdomains" "$_ecc"
6911 setnotify
"$_notify_hook" "$_notify_level" "$_notify_mode"
6914 if [ "$_CMD" ]; then
6915 _err
"Invalid command: $_CMD"
6922 if [ "$_ret" != "0" ]; then
6926 if [ "${_CMD}" = "install" ]; then
6927 if [ "$_log" ]; then
6928 if [ -z "$LOG_FILE" ]; then
6929 LOG_FILE
="$DEFAULT_LOG_FILE"
6931 _saveaccountconf
"LOG_FILE" "$LOG_FILE"
6934 if [ "$_log_level" ]; then
6935 _saveaccountconf
"LOG_LEVEL" "$_log_level"
6938 if [ "$_syslog" ]; then
6939 if _exists logger
; then
6940 if [ "$_syslog" = "0" ]; then
6941 _clearaccountconf
"SYS_LOG"
6943 _saveaccountconf
"SYS_LOG" "$_syslog"
6946 _err
"The 'logger' command is not found, can not enable syslog."
6947 _clearaccountconf
"SYS_LOG"
6957 if [ "$INSTALLONLINE" ]; then
6964 [ -z "$1" ] && showhelp
&& return
6965 if _startswith
"$1" '-'; then _process
"$@"; else "$@"; fi