7 PROJECT_ENTRY
="acme.sh"
9 PROJECT
="https://github.com/Neilpang/$PROJECT_NAME"
11 DEFAULT_INSTALL_HOME
="$HOME/.$PROJECT_NAME"
14 _SUB_FOLDERS
="dnsapi deploy"
16 LETSENCRYPT_CA_V1
="https://acme-v01.api.letsencrypt.org/directory"
17 LETSENCRYPT_STAGING_CA_V1
="https://acme-staging.api.letsencrypt.org/directory"
19 LETSENCRYPT_CA_V2
="https://acme-v02.api.letsencrypt.org/directory"
20 LETSENCRYPT_STAGING_CA_V2
="https://acme-staging-v02.api.letsencrypt.org/directory"
22 DEFAULT_CA
=$LETSENCRYPT_CA_V1
23 DEFAULT_STAGING_CA
=$LETSENCRYPT_STAGING_CA_V1
26 DEFAULT_USER_AGENT
="$PROJECT_NAME/$VER ($PROJECT)"
27 DEFAULT_ACCOUNT_EMAIL
=""
29 DEFAULT_ACCOUNT_KEY_LENGTH
=2048
30 DEFAULT_DOMAIN_KEY_LENGTH
=2048
32 DEFAULT_OPENSSL_BIN
="openssl"
34 _OLD_CA_HOST
="https://acme-v01.api.letsencrypt.org"
35 _OLD_STAGE_CA_HOST
="https://acme-staging.api.letsencrypt.org"
39 VTYPE_TLS
="tls-sni-01"
40 VTYPE_TLS2
="tls-sni-02"
42 LOCAL_ANY_ADDRESS
="0.0.0.0"
52 MODE_STATELESS
="stateless"
54 STATE_VERIFIED
="verified_ok"
57 NGINX_START
="#ACME_NGINX_START"
58 NGINX_END
="#ACME_NGINX_END"
60 BEGIN_CSR
="-----BEGIN CERTIFICATE REQUEST-----"
61 END_CSR
="-----END CERTIFICATE REQUEST-----"
63 BEGIN_CERT
="-----BEGIN CERTIFICATE-----"
64 END_CERT
="-----END CERTIFICATE-----"
69 ECC_SUFFIX
="${ECC_SEP}ecc"
74 DEFAULT_LOG_LEVEL
="$LOG_LEVEL_1"
79 DEBUG_LEVEL_DEFAULT
=$DEBUG_LEVEL_1
82 HIDDEN_VALUE
="[hidden](please add '--output-insecure' to see this value)"
84 SYSLOG_ERROR
="user.error"
85 SYSLOG_INFO
="user.info"
86 SYSLOG_DEBUG
="user.debug"
95 SYSLOG_LEVEL_DEBUG_2
=8
97 SYSLOG_LEVEL_DEBUG_3
=9
99 SYSLOG_LEVEL_DEFAULT
=$SYSLOG_LEVEL_ERROR
103 _DEBUG_WIKI
="https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh"
105 _PREPARE_LINK
="https://github.com/Neilpang/acme.sh/wiki/Install-preparations"
107 _STATELESS_WIKI
="https://github.com/Neilpang/acme.sh/wiki/Stateless-Mode"
109 _DNS_MANUAL_ERR
="The dns manual mode can not renew automatically, you must issue it again manually. You'd better use the other modes instead."
111 _DNS_MANUAL_WARN
="It seems that you are using dns manual mode. please take care: $_DNS_MANUAL_ERR"
119 if [ "$__INTERACTIVE${ACME_NO_COLOR}" = "1" ]; then
120 printf '\033[1;31;32m'
123 if [ "$__INTERACTIVE${ACME_NO_COLOR}" = "1" ]; then
129 if [ "$__INTERACTIVE${ACME_NO_COLOR}" = "1" ]; then
130 printf '\033[1;31;40m'
133 if [ "$__INTERACTIVE${ACME_NO_COLOR}" = "1" ]; then
139 if [ -z "$NO_TIMESTAMP" ] ||
[ "$NO_TIMESTAMP" = "0" ]; then
140 printf -- "%s" "[$(date)] "
145 printf -- "%s" "$1='$2'"
151 echo "Diagnosis versions: "
152 echo "openssl:$ACME_OPENSSL_BIN"
153 if _exists
"${ACME_OPENSSL_BIN:-openssl}"; then
154 ${ACME_OPENSSL_BIN:-openssl} version
2>&1
156 echo "$ACME_OPENSSL_BIN doesn't exists."
160 if [ "$_APACHECTL" ] && _exists
"$_APACHECTL"; then
163 echo "apache doesn't exists."
167 if _exists
"nginx"; then
170 echo "nginx doesn't exists."
174 if _exists
"socat"; then
177 _debug
"socat doesn't exists."
183 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" = "$SYSLOG_LEVEL_NONE" ]; then
188 if [ -z "$__logger_i" ]; then
189 if _contains
"$(logger --help 2>&1)" "-i"; then
190 __logger_i
="logger -i"
195 $__logger_i -t "$PROJECT_NAME" -p "$_logclass" "$(_printargs "$@
")" >/dev
/null
2>&1
199 [ -z "$LOG_FILE" ] && return
200 _printargs
"$@" >>"$LOG_FILE"
205 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_INFO" ]; then
206 _syslog
"$SYSLOG_INFO" "$@"
212 _syslog
"$SYSLOG_ERROR" "$@"
214 if [ -z "$NO_TIMESTAMP" ] ||
[ "$NO_TIMESTAMP" = "0" ]; then
215 printf -- "%s" "[$(date)] " >&2
232 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
235 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
236 _syslog
"$SYSLOG_DEBUG" "$@"
238 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
243 #output the sensitive messages
245 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
246 if [ "$OUTPUT_INSECURE" = "1" ]; then
249 _log
"$1" "$HIDDEN_VALUE"
252 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
253 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
255 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
256 if [ "$OUTPUT_INSECURE" = "1" ]; then
259 _printargs
"$1" "$HIDDEN_VALUE" >&2
265 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
268 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
269 _syslog
"$SYSLOG_DEBUG" "$@"
271 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
277 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
278 if [ "$OUTPUT_INSECURE" = "1" ]; then
281 _log
"$1" "$HIDDEN_VALUE"
284 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
285 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
287 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
288 if [ "$OUTPUT_INSECURE" = "1" ]; then
291 _printargs
"$1" "$HIDDEN_VALUE" >&2
297 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
300 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
301 _syslog
"$SYSLOG_DEBUG" "$@"
303 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
309 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
310 if [ "$OUTPUT_INSECURE" = "1" ]; then
313 _log
"$1" "$HIDDEN_VALUE"
316 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
317 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
319 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
320 if [ "$OUTPUT_INSECURE" = "1" ]; then
323 _printargs
"$1" "$HIDDEN_VALUE" >&2
329 # shellcheck disable=SC2018,SC2019
334 # shellcheck disable=SC2018,SC2019
341 echo "$_str" |
grep "^$_sub" >/dev
/null
2>&1
347 echo "$_str" |
grep -- "$_sub\$" >/dev
/null
2>&1
353 echo "$_str" |
grep -- "$_sub" >/dev
/null
2>&1
360 if [ -z "$_field" ]; then
361 _usage
"Usage: str field [sep]"
365 if [ -z "$_sep" ]; then
369 for f
in $
(echo "$_str" |
tr "$_sep" ' '); do
370 if [ "$f" = "$_field" ]; then
371 _debug2
"'$_str' contains '$_field'"
372 return 0 #contains ok
375 _debug2
"'$_str' does not contain '$_field'"
376 return 1 #not contains
385 if [ -z "$_findex" ]; then
386 _usage
"Usage: str field [sep]"
390 if [ -z "$_sep" ]; then
395 while [ "$_ffi" -gt "0" ]; do
396 _fv
="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")"
398 printf -- "%s" "$_fv"
401 _ffi
="$(_math "$_ffi" - 1)"
404 printf -- "%s" "$_str"
410 if [ -z "$cmd" ]; then
411 _usage
"Usage: _exists cmd"
415 if eval type type >/dev
/null
2>&1; then
416 eval type "$cmd" >/dev
/null
2>&1
417 elif command >/dev
/null
2>&1; then
418 command -v "$cmd" >/dev
/null
2>&1
420 which "$cmd" >/dev
/null
2>&1
423 _debug3
"$cmd exists=$ret"
430 printf "%s" "$(($_m_opts))"
462 if [ "$(printf '\x41')" != 'A' ]; then
467 if _exists
xargs && [ "$(printf %s '\\x41' | xargs printf)" = 'A' ]; then
472 if _exists xxd
&& xxd
-r -p 2>/dev
/null
; then
479 _debug2 _URGLY_PRINTF
"$_URGLY_PRINTF"
480 if [ -z "$_URGLY_PRINTF" ]; then
481 if [ "$_ESCAPE_XARGS" ] && _exists
xargs; then
483 echo "$hex" | _upper_case |
sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/g' |
xargs printf
485 for h
in $
(echo "$hex" | _upper_case |
sed 's/\([0-9A-F]\{2\}\)/ \1/g'); do
493 for c
in $
(echo "$hex" | _upper_case |
sed 's/\([0-9A-F]\)/ \1/g'); do
494 if [ -z "$ic" ]; then
499 ic
="$(_h_char_2_dec "$ic")"
500 jc
="$(_h_char_2_dec "$jc")"
501 printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")""%s"
510 _contains "${__OS__:=$(uname -a)}" "solaris" || _contains "${__OS__:=$(uname -a)}" "SunOS"
514 #this can only process ascii chars, should only be used when od command is missing as a backup way.
516 _debug2 "Using _ascii_hex"
520 while [ "$_h_i" -le "$_str_len" ]; do
521 _str_c="$(printf "%s" "$_str" | cut -c "$_h_i")"
522 printf " %02x" "'$_str_c"
523 _h_i="$
(_math
"$_h_i" + 1)"
527 #stdin output hexstr splited by one space
532 od -A n -v -t x1 | tr -s " " | sed 's/ $//' | tr -d "\r\t\n"
533 elif _exists hexdump; then
534 _debug3 "using
hexdump"
535 hexdump -v -e '/1 ""' -e '/1 " %02x
" ""'
536 elif _exists xxd; then
538 xxd -ps -c 20 -i | sed "s
/ 0x
/ /g
" | tr -d ",\n" | tr -s " "
540 _debug3 "using _ascii_hex
"
546 #url encode, no-preserved chars
547 #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
548 #41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a
550 #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
551 #61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a
553 #0 1 2 3 4 5 6 7 8 9 - _ . ~
554 #30 31 32 33 34 35 36 37 38 39 2d 5f 2e 7e
558 _hex_str=$(_hex_dump)
559 _debug3 "_url_encode
"
560 _debug3 "_hex_str
" "$_hex_str"
561 for _hex_code in $_hex_str; do
563 case "${_hex_code}" in
767 printf '%%%s' "$_hex_code"
777 if [ -z "$filename" ]; then
778 _usage "Usage
:_sed_i options filename
"
781 _debug2 options "$options"
782 if sed -h 2>&1 | grep "\
-i\
[SUFFIX
]" >/dev/null 2>&1; then
783 _debug "Using
sed -i"
784 sed -i "$options" "$filename"
786 _debug "No
-i support
in sed"
787 text="$
(cat "$filename")"
788 echo "$text" | sed "$options" >"$filename"
793 if ! egrep -o "$1" 2>/dev/null; then
794 sed -n 's/.*\('"$1"'\).*/\1/p'
798 #Usage: file startline endline
803 if [ -z "$endline" ]; then
804 _usage "Usage
: file startline endline
"
808 i="$
(grep -n -- "$startline" "$filename" | cut
-d : -f 1)"
810 _err "Can not
find start line
: $startline"
813 i="$
(_math
"$i" + 1)"
816 j="$
(grep -n -- "$endline" "$filename" | cut
-d : -f 1)"
818 _err "Can not
find end line
: $endline"
821 j="$
(_math
"$j" - 1)"
824 sed -n "$i,${j}p
" "$filename"
832 _debug3 "base64 multiline
:'$1'"
833 ${ACME_OPENSSL_BIN:-openssl} base64 -e
835 _debug3 "base64 single line.
"
836 ${ACME_OPENSSL_BIN:-openssl} base64 -e | tr -d '\r\n'
843 ${ACME_OPENSSL_BIN:-openssl} base64 -d -A
845 ${ACME_OPENSSL_BIN:-openssl} base64 -d
849 #Usage: hashalg [outputhex]
850 #Output Base64-encoded digest
853 if [ -z "$alg" ]; then
854 _usage "Usage
: _digest hashalg
"
860 if [ "$alg" = "sha256
" ] || [ "$alg" = "sha1
" ] || [ "$alg" = "md5
" ]; then
861 if [ "$outputhex" ]; then
862 ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' '
864 ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -binary | _base64
867 _err "$alg is not supported yet
"
873 #Usage: hashalg secret_hex [outputhex]
880 if [ -z "$secret_hex" ]; then
881 _usage "Usage
: _hmac hashalg secret
[outputhex
]"
885 if [ "$alg" = "sha256
" ] || [ "$alg" = "sha1
" ]; then
886 if [ "$outputhex" ]; then
887 (${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 ' '
889 ${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
892 _err "$alg is not supported yet
"
898 #Usage: keyfile hashalg
899 #Output: Base64-encoded signature value
903 if [ -z "$alg" ]; then
904 _usage "Usage
: _sign keyfile hashalg
"
908 _sign_openssl="${ACME_OPENSSL_BIN:-openssl} dgst
-sign $keyfile "
910 if grep "BEGIN RSA PRIVATE KEY
" "$keyfile" >/dev/null 2>&1; then
911 $_sign_openssl -$alg | _base64
912 elif grep "BEGIN EC PRIVATE KEY
" "$keyfile" >/dev/null 2>&1; then
913 if ! _signedECText="$
($_sign_openssl -sha$__ECC_KEY_LEN |
${ACME_OPENSSL_BIN:-openssl} asn1parse
-inform DER
)"; then
914 _err "Sign failed
: $_sign_openssl"
915 _err "Key
file: $keyfile"
916 _err "Key content
:$
(wc -l <"$keyfile") lines
"
919 _debug3 "_signedECText
" "$_signedECText"
920 _ec_r="$
(echo "$_signedECText" | _head_n
2 | _tail_n
1 | cut
-d : -f 4 |
tr -d "\r\n")"
921 _debug3 "_ec_r
" "$_ec_r"
922 _ec_s="$
(echo "$_signedECText" | _head_n
3 | _tail_n
1 | cut
-d : -f 4 |
tr -d "\r\n")"
923 _debug3 "_ec_s
" "$_ec_s"
924 printf "%s
" "$_ec_r$_ec_s" | _h2b | _base64
926 _err "Unknown key
file format.
"
932 #keylength or isEcc flag (empty str => not ecc)
936 if [ -z "$_length" ]; then
940 [ "$_length" != "1024" ] \
941 && [ "$_length" != "2048" ] \
942 && [ "$_length" != "3072" ] \
943 && [ "$_length" != "4096" ] \
944 && [ "$_length" != "8192" ]
947 # _createkey 2048|ec-256 file
951 _debug2 "_createkey
for file:$f"
953 if _startswith "$length" "ec-
"; then
954 length=$(printf "%s
" "$length" | cut -d '-' -f 2-100)
956 if [ "$length" = "256" ]; then
959 if [ "$length" = "384" ]; then
962 if [ "$length" = "521" ]; then
968 if [ -z "$length" ]; then
972 _debug "Use length
$length"
974 if ! touch "$f" >/dev/null 2>&1; then
975 _f_path="$
(dirname "$f")"
976 _debug _f_path "$_f_path"
977 if ! mkdir -p "$_f_path"; then
978 _err "Can not create path
: $_f_path"
983 if _isEccKey "$length"; then
984 _debug "Using ec name
: $eccname"
985 ${ACME_OPENSSL_BIN:-openssl} ecparam -name "$eccname" -genkey 2>/dev/null >"$f"
987 _debug "Using RSA
: $length"
988 ${ACME_OPENSSL_BIN:-openssl} genrsa "$length" 2>/dev/null >"$f"
991 if [ "$?
" != "0" ]; then
992 _err "Create key error.
"
1000 _debug2 _is_idn_d "$_is_idn_d"
1001 _idn_temp=$(printf "%s
" "$_is_idn_d" | tr -d '0-9' | tr -d 'a-z' | tr -d 'A-Z' | tr -d '.,-')
1002 _debug2 _idn_temp "$_idn_temp"
1007 #aa.com,bb.com,cc.com
1010 if ! _is_idn "$__idn_d"; then
1011 printf "%s
" "$__idn_d"
1015 if _exists idn; then
1016 if _contains "$__idn_d" ','; then
1018 for f in $(echo "$__idn_d" | tr ',' ' '); do
1019 [ -z "$f" ] && continue
1020 if [ -z "$_i_first" ]; then
1025 idn --quiet "$f" | tr -d "\r\n"
1028 idn "$__idn_d" | tr -d "\r\n"
1031 _err "Please
install idn to process IDN names.
"
1035 #_createcsr cn san_list keyfile csrfile conf
1043 _debug2 domain "$domain"
1044 _debug2 domainlist "$domainlist"
1045 _debug2 csrkey "$csrkey"
1047 _debug2 csrconf "$csrconf"
1049 printf "[ req_distinguished_name
]\n[ req
]\ndistinguished_name
= req_distinguished_name
\nreq_extensions
= v3_req
\n[ v3_req
]\n\nkeyUsage
= nonRepudiation
, digitalSignature
, keyEncipherment
" >"$csrconf"
1051 if [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then
1053 _info "Single domain
" "$domain"
1054 printf -- "\nsubjectAltName
=DNS
:$domain" >>"$csrconf"
1056 domainlist="$
(_idn
"$domainlist")"
1057 _debug2 domainlist "$domainlist"
1058 if _contains "$domainlist" ","; then
1059 alt="DNS
:$domain,DNS
:$
(echo "$domainlist" |
sed "s/,/,DNS:/g")"
1061 alt="DNS
:$domain,DNS
:$domainlist"
1064 _info "Multi domain
" "$alt"
1065 printf -- "\nsubjectAltName
=$alt" >>"$csrconf"
1067 if [ "$Le_OCSP_Staple" ] || [ "$Le_OCSP_Stable" ]; then
1068 _savedomainconf Le_OCSP_Staple "$Le_OCSP_Staple"
1069 _cleardomainconf Le_OCSP_Stable
1070 printf -- "\nbasicConstraints
= CA
:FALSE
\n1.3.6.1.5.5.7.1.24=DER
:30:03:02:01:05" >>"$csrconf"
1073 _csr_cn="$
(_idn
"$domain")"
1074 _debug2 _csr_cn "$_csr_cn"
1075 if _contains "$
(uname
-a)" "MINGW
"; then
1076 ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "//CN
=$_csr_cn" -config "$csrconf" -out "$csr"
1078 ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "/CN
=$_csr_cn" -config "$csrconf" -out "$csr"
1082 #_signcsr key csr conf cert
1090 _msg="$
(${ACME_OPENSSL_BIN:-openssl} x509
-req -days 365 -in "$csr" -signkey "$key" -extensions v3_req
-extfile "$conf" -out "$cert" 2>&1)"
1097 _readSubjectFromCSR() {
1099 if [ -z "$_csrfile" ]; then
1100 _usage "_readSubjectFromCSR mycsr.csr
"
1103 ${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'
1107 #echo comma separated domain list
1108 _readSubjectAltNamesFromCSR() {
1110 if [ -z "$_csrfile" ]; then
1111 _usage "_readSubjectAltNamesFromCSR mycsr.csr
"
1115 _csrsubj="$
(_readSubjectFromCSR
"$_csrfile")"
1116 _debug _csrsubj "$_csrsubj"
1118 _dnsAltnames="$
(${ACME_OPENSSL_BIN:-openssl} req
-noout -text -in "$_csrfile" |
grep "^ *DNS:.*" |
tr -d ' \n')"
1119 _debug _dnsAltnames "$_dnsAltnames"
1121 if _contains "$_dnsAltnames," "DNS
:$_csrsubj,"; then
1122 _debug "AltNames contains subject
"
1123 _dnsAltnames="$
(printf "%s" "$_dnsAltnames," |
sed "s/DNS:$_csrsubj,//g")"
1125 _debug "AltNames doesn
't contain subject"
1128 printf "%s" "$_dnsAltnames" | sed "s/DNS://g"
1132 _readKeyLengthFromCSR() {
1134 if [ -z "$_csrfile" ]; then
1135 _usage "_readKeyLengthFromCSR mycsr.csr"
1139 _outcsr="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile")"
1140 _debug2 _outcsr "$_outcsr"
1141 if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then
1143 echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' '
1146 _rkl="$(echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1)"
1147 if [ "$_rkl" ]; then
1150 echo "$_outcsr" | tr "\t" " " | _egrep_o "RSA Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1
1158 if _exists "ss"; then
1160 ss -ntpl 2>/dev/null | grep ":$_port "
1164 if _exists "netstat"; then
1165 _debug "Using: netstat"
1166 if netstat -h 2>&1 | grep "\-p proto" >/dev/null; then
1167 #for windows version netstat tool
1168 netstat -an -p tcp | grep "LISTENING" | grep ":$_port "
1170 if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then
1171 netstat -an -p tcp | grep LISTEN | grep ":$_port "
1172 elif netstat -help 2>&1 | grep -- '-P protocol
' >/dev/null; then
1174 netstat -an -P tcp | grep "\.$_port " | grep "LISTEN"
1175 elif netstat -help 2>&1 | grep "\-p" >/dev/null; then
1177 netstat -ntpl | grep ":$_port "
1179 #for busybox (embedded linux; no pid support)
1180 netstat -ntl 2>/dev/null | grep ":$_port "
1189 #outfile key cert cacert [password [name [caname]]]
1199 if [ "$pfxCaname" ]; then
1200 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName" -caname "$pfxCaname"
1201 elif [ "$pfxName" ]; then
1202 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName"
1203 elif [ "$pfxPassword" ]; then
1204 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword"
1206 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca"
1211 #domain [password] [isEcc]
1215 if [ -z "$domain" ]; then
1216 _usage "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]"
1222 _initpath "$domain" "$_isEcc"
1224 _toPkcs "$CERT_PFX_PATH" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$pfxPassword"
1226 if [ "$?" = "0" ]; then
1227 _info "Success, Pfx is exported to: $CERT_PFX_PATH"
1236 if [ -z "$domain" ]; then
1237 _usage "Usage: $PROJECT_ENTRY --toPkcs8 -d domain [--ecc]"
1243 _initpath "$domain" "$_isEcc"
1245 ${ACME_OPENSSL_BIN:-openssl} pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in "$CERT_KEY_PATH" -out "$CERT_PKCS8_PATH"
1247 if [ "$?" = "0" ]; then
1248 _info "Success, $CERT_PKCS8_PATH"
1254 createAccountKey() {
1255 _info "Creating account key"
1256 if [ -z "$1" ]; then
1257 _usage "Usage: $PROJECT_ENTRY --createAccountKey --accountkeylength 2048"
1262 _create_account_key "$length"
1266 _create_account_key() {
1270 if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then
1271 _debug "Use default length $DEFAULT_ACCOUNT_KEY_LENGTH"
1272 length="$DEFAULT_ACCOUNT_KEY_LENGTH"
1275 _debug length "$length"
1279 if [ -f "$ACCOUNT_KEY_PATH" ]; then
1280 _info "Account key exists, skip"
1283 #generate account key
1284 _createkey "$length" "$ACCOUNT_KEY_PATH"
1291 _info "Creating domain key"
1292 if [ -z "$1" ]; then
1293 _usage "Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]"
1300 if [ -z "$_cdl" ]; then
1301 _debug "Use DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH"
1302 _cdl="$DEFAULT_DOMAIN_KEY_LENGTH"
1305 _initpath "$domain" "$_cdl"
1307 if [ ! -f "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$IS_RENEW" ]) || [ "$Le_ForceNewDomainKey" = "1" ]; then
1308 if _createkey "$_cdl" "$CERT_KEY_PATH"; then
1309 _savedomainconf Le_Keylength "$_cdl"
1310 _info "The domain key is here: $(__green $CERT_KEY_PATH)"
1313 if [ "$IS_RENEW" ]; then
1314 _info "Domain key exists, skip"
1317 _err "Domain key exists, do you want to overwrite the key?"
1318 _err "Add '--force', and try again."
1325 # domain domainlist isEcc
1327 _info "Creating csr"
1328 if [ -z "$1" ]; then
1329 _usage "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]"
1337 _initpath "$domain" "$_isEcc"
1339 if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then
1340 _info "CSR exists, skip"
1344 if [ ! -f "$CERT_KEY_PATH" ]; then
1345 _err "The key file is not found: $CERT_KEY_PATH"
1346 _err "Please create the key file first."
1349 _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"
1354 tr '/+' '_-
' | tr -d '= '
1359 if date -u -d@"$1" 2>/dev/null; then
1364 if date -u -r "$1" 2>/dev/null; then
1369 if _exists adb; then
1370 _t_s_a=$(echo "0t${1}=Y" | adb)
1375 if echo "$1" | awk '{ print strftime
("%c", $0); }' 2>/dev/null; then
1381 sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n"
1386 if stat -c '%U
:%G
' "$1" 2>/dev/null; then
1391 if stat -f '%Su
:%Sg
' "$1" 2>/dev/null; then
1395 return 1 #error, 'stat
' not found
1401 if [ -z "$keyfile" ]; then
1402 _usage "Usage: _calcjwk keyfile"
1406 if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then
1407 _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE"
1411 if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
1413 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)
1414 if [ "${#pub_exp}" = "5" ]; then
1417 _debug3 pub_exp "$pub_exp"
1419 e=$(echo "$pub_exp" | _h2b | _base64)
1422 modulus=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2)
1423 _debug3 modulus "$modulus"
1424 n="$(printf "%s" "$modulus" | _h2b | _base64 | _url_replace)"
1427 jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
1430 JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}'
1431 JWK_HEADERPLACE_PART1='{"nonce": "'
1432 JWK_HEADERPLACE_PART2='", "alg": "RS256"'
1433 elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
1435 crv="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
1437 __ECC_KEY_LEN=$(echo "$crv" | cut -d "-" -f 2)
1438 if [ "$__ECC_KEY_LEN" = "521" ]; then
1441 _debug3 __ECC_KEY_LEN "$__ECC_KEY_LEN"
1442 if [ -z "$crv" ]; then
1443 _debug "Let's try ASN1 OID
"
1444 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")"
1445 _debug3 crv_oid "$crv_oid"
1446 case "${crv_oid}" in
1460 _err "ECC oid
: $crv_oid"
1467 pubi="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep -n pub
: | cut
-d : -f 1)"
1468 pubi=$(_math "$pubi" + 1)
1469 _debug3 pubi "$pubi"
1471 pubj="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep -n "ASN1 OID:" | cut
-d : -f 1)"
1472 pubj=$(_math "$pubj" - 1)
1473 _debug3 pubj "$pubj"
1475 pubtext="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
sed -n "$pubi,${pubj}p" |
tr -d " \n\r")"
1476 _debug3 pubtext "$pubtext"
1478 xlen="$
(printf "%s" "$pubtext" |
tr -d ':' |
wc -c)"
1479 xlen=$(_math "$xlen" / 4)
1480 _debug3 xlen "$xlen"
1482 xend=$(_math "$xlen" + 1)
1483 x="$
(printf "%s" "$pubtext" | cut
-d : -f 2-"$xend")"
1486 x64="$
(printf "%s" "$x" |
tr -d : | _h2b | _base64 | _url_replace
)"
1489 xend=$(_math "$xend" + 1)
1490 y="$
(printf "%s" "$pubtext" | cut
-d : -f "$xend"-10000)"
1493 y64="$
(printf "%s" "$y" |
tr -d : | _h2b | _base64 | _url_replace
)"
1496 jwk='{"crv
": "'$crv'", "kty
": "EC
", "x
": "'$x64'", "y
": "'$y64'"}'
1499 JWK_HEADER='{"alg
": "ES
'$__ECC_KEY_LEN'", "jwk
": '$jwk'}'
1500 JWK_HEADERPLACE_PART1='{"nonce
": "'
1501 JWK_HEADERPLACE_PART2='", "alg
": "ES
'$__ECC_KEY_LEN'"'
1503 _err "Only RSA or EC key is supported.
"
1507 _debug3 JWK_HEADER "$JWK_HEADER"
1508 __CACHED_JWK_KEY_FILE="$keyfile"
1516 date -u "+%Y-
%m-
%d
%H
:%M
:%S
"
1520 if _exists mktemp; then
1521 if mktemp 2>/dev/null; then
1523 elif _contains "$
(mktemp
2>&1)" "-t prefix
" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then
1528 if [ -d "/tmp
" ]; then
1529 echo "/tmp
/${PROJECT_NAME}wefADf24sf.$
(_time
).tmp
"
1531 elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then
1532 echo "/$LE_TEMP_DIR/wefADf24sf.$
(_time
).tmp
"
1535 _err "Can not create temp
file.
"
1540 if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then
1541 HTTP_HEADER="$
(_mktemp
)"
1542 _debug2 HTTP_HEADER "$HTTP_HEADER"
1545 if [ "$__HTTP_INITIALIZED" ]; then
1546 if [ "$_ACME_CURL$_ACME_WGET" ]; then
1547 _debug2 "Http already initialized.
"
1552 if [ -z "$_ACME_CURL" ] && _exists "curl
"; then
1553 _ACME_CURL="curl
-L --silent --dump-header $HTTP_HEADER "
1554 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1555 _CURL_DUMP="$
(_mktemp
)"
1556 _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP "
1559 if [ "$CA_PATH" ]; then
1560 _ACME_CURL="$_ACME_CURL --capath $CA_PATH "
1561 elif [ "$CA_BUNDLE" ]; then
1562 _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE "
1567 if [ -z "$_ACME_WGET" ] && _exists "wget
"; then
1568 _ACME_WGET="wget
-q"
1569 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1570 _ACME_WGET="$_ACME_WGET -d "
1572 if [ "$CA_PATH" ]; then
1573 _ACME_WGET="$_ACME_WGET --ca-directory=$CA_PATH "
1574 elif [ "$CA_BUNDLE" ]; then
1575 _ACME_WGET="$_ACME_WGET --ca-certificate=$CA_BUNDLE "
1579 #from wget 1.14: do not skip body on 404 error
1580 if [ "$_ACME_WGET" ] && _contains "$
($_ACME_WGET --help 2>&1)" "--content-on-error"; then
1581 _ACME_WGET="$_ACME_WGET --content-on-error "
1584 __HTTP_INITIALIZED=1
1588 # body url [needbase64] [POST|PUT]
1595 if [ -z "$httpmethod" ]; then
1599 _debug "_post_url
" "$_post_url"
1600 _debug2 "body
" "$body"
1604 if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
1606 if [ "$HTTPS_INSECURE" ]; then
1607 _CURL="$_CURL --insecure "
1609 _debug "_CURL
" "$_CURL"
1610 if [ "$needbase64" ]; then
1611 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64
)"
1613 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")"
1616 if [ "$_ret" != "0" ]; then
1617 _err "Please refer to https
://curl.haxx.se
/libcurl
/c
/libcurl-errors.html
for error code
: $_ret"
1618 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1619 _err "Here is the curl dump log
:"
1620 _err "$
(cat "$_CURL_DUMP")"
1623 elif [ "$_ACME_WGET" ]; then
1625 if [ "$HTTPS_INSECURE" ]; then
1626 _WGET="$_WGET --no-check-certificate "
1628 _debug "_WGET
" "$_WGET"
1629 if [ "$needbase64" ]; then
1630 if [ "$httpmethod" = "POST
" ]; then
1631 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
)"
1633 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
)"
1636 if [ "$httpmethod" = "POST
" ]; then
1637 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")"
1639 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")"
1643 if [ "$_ret" = "8" ]; then
1645 _debug "wget returns
8, the server returns a
'Bad request' response
, lets process the response later.
"
1647 if [ "$_ret" != "0" ]; then
1648 _err "Please refer to https
://www.gnu.org
/software
/wget
/manual
/html_node
/Exit-Status.html
for error code
: $_ret"
1650 _sed_i "s
/^
*//g
" "$HTTP_HEADER"
1653 _err "Neither curl nor wget is found
, can not
do $httpmethod.
"
1655 _debug "_ret
" "$_ret"
1656 printf "%s
" "$response"
1660 # url getheader timeout
1667 _debug "timeout
" "$t"
1671 if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
1673 if [ "$HTTPS_INSECURE" ]; then
1674 _CURL="$_CURL --insecure "
1677 _CURL="$_CURL --connect-timeout $t"
1679 _debug "_CURL
" "$_CURL"
1680 if [ "$onlyheader" ]; then
1681 $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
1683 $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
1686 if [ "$ret" != "0" ]; then
1687 _err "Please refer to https
://curl.haxx.se
/libcurl
/c
/libcurl-errors.html
for error code
: $ret"
1688 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1689 _err "Here is the curl dump log
:"
1690 _err "$
(cat "$_CURL_DUMP")"
1693 elif [ "$_ACME_WGET" ]; then
1695 if [ "$HTTPS_INSECURE" ]; then
1696 _WGET="$_WGET --no-check-certificate "
1699 _WGET="$_WGET --timeout=$t"
1701 _debug "_WGET
" "$_WGET"
1702 if [ "$onlyheader" ]; then
1703 $_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'
1705 $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url"
1708 if [ "$ret" = "8" ]; then
1710 _debug "wget returns
8, the server returns a
'Bad request' response
, lets process the response later.
"
1712 if [ "$ret" != "0" ]; then
1713 _err "Please refer to https
://www.gnu.org
/software
/wget
/manual
/html_node
/Exit-Status.html
for error code
: $ret"
1717 _err "Neither curl nor wget is found
, can not
do GET.
"
1728 if ! tail -n "$1" 2>/dev/null; then
1734 # url payload needbase64 keyfile
1735 _send_signed_request() {
1740 if [ -z "$keyfile" ]; then
1741 keyfile="$ACCOUNT_KEY_PATH"
1744 _debug payload "$payload"
1746 if ! _calcjwk "$keyfile"; then
1750 payload64=$(printf "%s
" "$payload" | _base64 | _url_replace)
1751 _debug3 payload64 "$payload64"
1753 MAX_REQUEST_RETRY_TIMES=5
1754 _request_retry_times=0
1755 while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
1756 _debug3 _request_retry_times "$_request_retry_times"
1757 if [ -z "$_CACHED_NONCE" ]; then
1759 if [ "$ACME_NEW_NONCE" ]; then
1760 _debug2 "Get nonce. ACME_NEW_NONCE
" "$ACME_NEW_NONCE"
1761 nonceurl="$ACME_NEW_NONCE"
1762 if _post "" "$nonceurl" "" "HEAD
"; then
1763 _headers="$
(cat "$HTTP_HEADER")"
1766 if [ -z "$_headers" ]; then
1767 _debug2 "Get nonce. ACME_DIRECTORY
" "$ACME_DIRECTORY"
1768 nonceurl="$ACME_DIRECTORY"
1769 _headers="$
(_get
"$nonceurl" "onlyheader")"
1772 if [ "$?
" != "0" ]; then
1773 _err "Can not connect to
$nonceurl to get nonce.
"
1777 _debug2 _headers "$_headers"
1779 _CACHED_NONCE="$
(echo "$_headers" |
grep "Replay-Nonce:" | _head_n
1 |
tr -d "\r\n " | cut
-d ':' -f 2)"
1780 _debug2 _CACHED_NONCE "$_CACHED_NONCE"
1782 _debug2 "Use _CACHED_NONCE
" "$_CACHED_NONCE"
1784 nonce="$_CACHED_NONCE"
1785 _debug2 nonce "$nonce"
1787 if [ "$ACME_VERSION" = "2" ]; then
1788 if [ "$url" = "$ACME_NEW_ACCOUNT" ]; then
1789 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url
\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk
\": $jwk"'}'
1791 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url
\": \"${url}$JWK_HEADERPLACE_PART2, \"kid
\": \"$ACCOUNT_URL\""'}'
1794 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url
\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk
\": $jwk"'}'
1796 _debug3 protected "$protected"
1798 protected64="$
(printf "%s" "$protected" | _base64 | _url_replace
)"
1799 _debug3 protected64 "$protected64"
1801 if ! _sig_t="$
(printf "%s" "$protected64.$payload64" | _sign
"$keyfile" "sha256")"; then
1802 _err "Sign request failed.
"
1805 _debug3 _sig_t "$_sig_t"
1807 sig="$
(printf "%s" "$_sig_t" | _url_replace
)"
1810 if [ "$ACME_VERSION" = "2" ]; then
1811 body="{\"protected
\": \"$protected64\", \"payload
\": \"$payload64\", \"signature
\": \"$sig\"}"
1813 body="{\"header
\": $JWK_HEADER, \"protected
\": \"$protected64\", \"payload
\": \"$payload64\", \"signature
\": \"$sig\"}"
1815 _debug3 body "$body"
1817 response="$
(_post
"$body" "$url" "$needbase64")"
1820 if [ "$?
" != "0" ]; then
1821 _err "Can not post to
$url"
1824 _debug2 original "$response"
1825 response="$
(echo "$response" | _normalizeJson
)"
1827 responseHeaders="$
(cat "$HTTP_HEADER")"
1829 _debug2 responseHeaders "$responseHeaders"
1830 _debug2 response "$response"
1831 code="$
(grep "^HTTP" "$HTTP_HEADER" | _tail_n
1 | cut
-d " " -f 2 |
tr -d "\r\n")"
1834 _CACHED_NONCE="$
(echo "$responseHeaders" |
grep "Replay-Nonce:" | _head_n
1 |
tr -d "\r\n " | cut
-d ':' -f 2)"
1837 if [ "$needbase64" ]; then
1838 _body="$
(echo "$_body" | _dbase64
)"
1839 _debug2 _body "$_body"
1842 if _contains "$_body" "JWS has invalid anti-replay nonce
"; then
1843 _info "It seems the CA server is busy now
, let's wait and retry."
1844 _request_retry_times=$(_math "$_request_retry_times" + 1)
1853 #setopt "file" "opt" "=" "value" [";"]
1860 if [ -z "$__opt" ]; then
1861 _usage usage: _setopt '"file" "opt" "=" "value" [";"]'
1864 if [ ! -f "$__conf" ]; then
1868 if grep -n "^$__opt$__sep" "$__conf" >/dev/null; then
1870 if _contains "$__val" "&"; then
1871 __val="$(echo "$__val" | sed 's
/&/\\&/g
')"
1873 text="$(cat "$__conf")"
1874 printf -- "%s\n" "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
1876 elif grep -n "^#$__opt$__sep" "$__conf" >/dev/null; then
1877 if _contains "$__val" "&"; then
1878 __val="$(echo "$__val" | sed 's
/&/\\&/g
')"
1880 text="$(cat "$__conf")"
1881 printf -- "%s\n" "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
1885 echo "$__opt$__sep$__val$__end" >>"$__conf"
1887 _debug3 "$(grep -n "^$__opt$__sep" "$__conf")"
1890 #_save_conf file key value
1896 if [ "$_s_c_f" ]; then
1897 _setopt "$_s_c_f" "$_sdkey" "=" "'$_sdvalue'"
1899 _err "config file is empty, can not save $_sdkey=$_sdvalue"
1903 #_clear_conf file key
1907 if [ "$_c_c_f" ]; then
1908 _conf_data="$(cat "$_c_c_f")"
1909 echo "$_conf_data" | sed "s/^$_sdkey *=.*$//" >"$_c_c_f"
1911 _err "config file is empty, can not clear"
1915 #_read_conf file key
1919 if [ -f "$_r_c_f" ]; then
1921 eval "$(grep "^$_sdkey *=" "$_r_c_f")"
1922 eval "printf \"%s\" \"\$$_sdkey\""
1925 _debug "config file is empty, can not read $_sdkey"
1929 #_savedomainconf key value
1930 #save to domain.conf
1932 _save_conf "$DOMAIN_CONF" "$1" "$2"
1935 #_cleardomainconf key
1936 _cleardomainconf() {
1937 _clear_conf "$DOMAIN_CONF" "$1"
1940 #_readdomainconf key
1942 _read_conf "$DOMAIN_CONF" "$1"
1945 #_saveaccountconf key value
1946 _saveaccountconf() {
1947 _save_conf "$ACCOUNT_CONF_PATH" "$1" "$2"
1951 _saveaccountconf_mutable() {
1952 _save_conf "$ACCOUNT_CONF_PATH" "SAVED_$1" "$2"
1954 _clearaccountconf "$1"
1958 _readaccountconf() {
1959 _read_conf "$ACCOUNT_CONF_PATH" "$1"
1963 _readaccountconf_mutable() {
1965 _readaccountconf "SAVED_$_rac_key"
1968 #_clearaccountconf key
1969 _clearaccountconf() {
1970 _clear_conf "$ACCOUNT_CONF_PATH" "$1"
1973 #_savecaconf key value
1975 _save_conf "$CA_CONF" "$1" "$2"
1980 _read_conf "$CA_CONF" "$1"
1983 #_clearaccountconf key
1985 _clear_conf "$CA_CONF" "$1"
1988 # content localaddress
1992 _debug "ncaddr" "$ncaddr"
1994 _debug "startserver: $$"
1996 _debug Le_HTTPPort "$Le_HTTPPort"
1997 _debug Le_Listen_V4 "$Le_Listen_V4"
1998 _debug Le_Listen_V6 "$Le_Listen_V6"
2001 if [ "$Le_Listen_V4" ]; then
2003 elif [ "$Le_Listen_V6" ]; then
2008 #todo listen address
2009 $_NC TCP-LISTEN:$Le_HTTPPort,crlf,reuseaddr,fork SYSTEM:"sleep 0.5; echo HTTP/1.1 200 OK; echo ; echo $content; echo;" &
2016 if [ -z "$pid" ]; then
2027 if [ "$__INTERACTIVE" ]; then
2028 _sleep_c="$_sleep_sec"
2029 while [ "$_sleep_c" -ge "0" ]; do
2032 _sleep_c="$(_math "$_sleep_c" - 1)"
2041 # _starttlsserver san_a san_b port content _ncaddr
2043 _info "Starting tls server."
2050 _debug san_a "$san_a"
2051 _debug san_b "$san_b"
2055 if ! _createkey "2048" "$TLS_KEY"; then
2056 _err "Create tls validation key error."
2062 if [ "$san_b" ]; then
2065 if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF"; then
2066 _err "Create tls validation csr error."
2071 if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then
2072 _err "Create tls validation cert error."
2076 __S_OPENSSL="${ACME_OPENSSL_BIN:-openssl} s_server -www -cert $TLS_CERT -key $TLS_KEY "
2077 if [ "$opaddr" ]; then
2078 __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port"
2080 __S_OPENSSL="$__S_OPENSSL -accept $port"
2083 _debug Le_Listen_V4 "$Le_Listen_V4"
2084 _debug Le_Listen_V6 "$Le_Listen_V6"
2085 if [ "$Le_Listen_V4" ]; then
2086 __S_OPENSSL="$__S_OPENSSL -4"
2087 elif [ "$Le_Listen_V6" ]; then
2088 __S_OPENSSL="$__S_OPENSSL -6"
2091 _debug "$__S_OPENSSL"
2092 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
2093 $__S_OPENSSL -tlsextdebug &
2095 $__S_OPENSSL >/dev/null 2>&1 &
2100 _debug serverproc "$serverproc"
2106 if ! readlink -f "$_rf" 2>/dev/null; then
2107 if _startswith "$_rf" "/"; then
2111 echo "$(pwd)/$_rf" | _conapath
2120 if [ -z "$_SCRIPT_HOME" ]; then
2121 if _exists readlink && _exists dirname; then
2122 _debug "Lets find script dir."
2123 _debug "_SCRIPT_" "$_SCRIPT_"
2124 _script="$(_readlink "$_SCRIPT_")"
2125 _debug "_script" "$_script"
2126 _script_home="$(dirname "$_script")"
2127 _debug "_script_home" "$_script_home"
2128 if [ -d "$_script_home" ]; then
2129 _SCRIPT_HOME="$_script_home"
2131 _err "It seems the script home is not correct:$_script_home"
2136 # if [ -z "$LE_WORKING_DIR" ]; then
2137 # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then
2138 # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME"
2139 # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
2141 # LE_WORKING_DIR="$_SCRIPT_HOME"
2145 if [ -z "$LE_WORKING_DIR" ]; then
2146 _debug "Using default home:$DEFAULT_INSTALL_HOME"
2147 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
2149 export LE_WORKING_DIR
2151 if [ -z "$LE_CONFIG_HOME" ]; then
2152 LE_CONFIG_HOME="$LE_WORKING_DIR"
2154 _debug "Using config home:$LE_CONFIG_HOME"
2155 export LE_CONFIG_HOME
2157 _DEFAULT_ACCOUNT_CONF_PATH="$LE_CONFIG_HOME/account.conf"
2159 if [ -z "$ACCOUNT_CONF_PATH" ]; then
2160 if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
2161 . "$_DEFAULT_ACCOUNT_CONF_PATH"
2165 if [ -z "$ACCOUNT_CONF_PATH" ]; then
2166 ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
2169 DEFAULT_LOG_FILE="$LE_CONFIG_HOME/$PROJECT_NAME.log"
2171 DEFAULT_CA_HOME="$LE_CONFIG_HOME/ca"
2173 if [ -z "$LE_TEMP_DIR" ]; then
2174 LE_TEMP_DIR="$LE_CONFIG_HOME/tmp"
2180 _api_server="${1:-$ACME_DIRECTORY}"
2181 _debug "_init api for server: $_api_server"
2183 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2184 response=$(_get "$_api_server")
2185 if [ "$?" != "0" ]; then
2186 _debug2 "response" "$response"
2187 _err "Can not init api."
2190 _debug2 "response" "$response"
2192 ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'key-change
" *: *"[^
"]*"' | cut -d '"' -f 3)
2193 if [ -z "$ACME_KEY_CHANGE" ]; then
2194 ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'keyChange" *: *"[^"]*"' | cut -d '"' -f 3)
2196 export ACME_KEY_CHANGE
2198 ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'new-authz
" *: *"[^
"]*"' | cut -d '"' -f 3)
2199 if [ -z "$ACME_NEW_AUTHZ" ]; then
2200 ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'newAuthz" *: *"[^"]*"' | cut -d '"' -f 3)
2202 export ACME_NEW_AUTHZ
2204 ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'new-cert
" *: *"[^
"]*"' | cut -d '"' -f 3)
2205 ACME_NEW_ORDER_RES="new-cert
"
2206 if [ -z "$ACME_NEW_ORDER" ]; then
2207 ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'new-order" *: *"[^"]*"' | cut -d '"' -f 3)
2208 ACME_NEW_ORDER_RES="new-order"
2209 if [ -z "$ACME_NEW_ORDER" ]; then
2210 ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'newOrder
" *: *"[^
"]*"' | cut -d '"' -f 3)
2213 export ACME_NEW_ORDER
2214 export ACME_NEW_ORDER_RES
2216 ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'new-reg" *: *"[^"]*"' | cut -d '"' -f 3)
2217 ACME_NEW_ACCOUNT_RES="new-reg"
2218 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2219 ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'new-account
" *: *"[^
"]*"' | cut -d '"' -f 3)
2220 ACME_NEW_ACCOUNT_RES="new-account
"
2221 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2222 ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'newAccount" *: *"[^"]*"' | cut -d '"' -f 3)
2223 if [ "$ACME_NEW_ACCOUNT" ]; then
2224 export ACME_VERSION=2
2228 export ACME_NEW_ACCOUNT
2229 export ACME_NEW_ACCOUNT_RES
2231 ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revoke-cert
" *: *"[^
"]*"' | cut -d '"' -f 3)
2232 if [ -z "$ACME_REVOKE_CERT" ]; then
2233 ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revokeCert" *: *"[^"]*"' | cut -d '"' -f 3)
2235 export ACME_REVOKE_CERT
2237 ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'new-nonce
" *: *"[^
"]*"' | cut -d '"' -f 3)
2238 if [ -z "$ACME_NEW_NONCE" ]; then
2239 ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'newNonce" *: *"[^"]*"' | cut -d '"' -f 3)
2241 export ACME_NEW_NONCE
2243 ACME_AGREEMENT=$(echo "$response" | _egrep_o 'terms-of-service
" *: *"[^
"]*"' | cut -d '"' -f 3)
2244 if [ -z "$ACME_AGREEMENT" ]; then
2245 ACME_AGREEMENT=$(echo "$response" | _egrep_o 'termsOfService" *: *"[^"]*"' | cut -d '"' -f 3)
2247 export ACME_AGREEMENT
2249 _debug "ACME_KEY_CHANGE" "$ACME_KEY_CHANGE"
2250 _debug "ACME_NEW_AUTHZ" "$ACME_NEW_AUTHZ"
2251 _debug "ACME_NEW_ORDER" "$ACME_NEW_ORDER"
2252 _debug "ACME_NEW_ACCOUNT" "$ACME_NEW_ACCOUNT"
2253 _debug "ACME_REVOKE_CERT" "$ACME_REVOKE_CERT"
2254 _debug "ACME_AGREEMENT" "$ACME_AGREEMENT"
2255 _debug "ACME_NEW_NONCE" "$ACME_NEW_NONCE"
2256 _debug "ACME_VERSION" "$ACME_VERSION"
2261 #[domain] [keylength or isEcc flag]
2266 if [ -f "$ACCOUNT_CONF_PATH" ]; then
2267 . "$ACCOUNT_CONF_PATH"
2270 if [ "$IN_CRON" ]; then
2271 if [ ! "$_USER_PATH_EXPORTED" ]; then
2272 _USER_PATH_EXPORTED=1
2273 export PATH="$USER_PATH:$PATH"
2277 if [ -z "$CA_HOME" ]; then
2278 CA_HOME="$DEFAULT_CA_HOME"
2281 if [ -z "$ACME_DIRECTORY" ]; then
2282 if [ -z "$STAGE" ]; then
2283 ACME_DIRECTORY="$DEFAULT_CA"
2285 ACME_DIRECTORY="$DEFAULT_STAGING_CA"
2286 _info "Using stage ACME_DIRECTORY: $ACME_DIRECTORY"
2290 _debug2 ACME_DIRECTORY "$ACME_DIRECTORY"
2291 _ACME_SERVER_HOST="$(echo "$ACME_DIRECTORY" | cut -d : -f 2 | tr -s / | cut -d / -f 2)"
2292 _debug2 "_ACME_SERVER_HOST" "$_ACME_SERVER_HOST"
2294 CA_DIR="$CA_HOME/$_ACME_SERVER_HOST"
2296 _DEFAULT_CA_CONF="$CA_DIR/ca.conf"
2298 if [ -z "$CA_CONF" ]; then
2299 CA_CONF="$_DEFAULT_CA_CONF"
2301 _debug3 CA_CONF "$CA_CONF"
2303 if [ -f "$CA_CONF" ]; then
2307 if [ -z "$ACME_DIR" ]; then
2308 ACME_DIR="/home/.acme"
2311 if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
2312 APACHE_CONF_BACKUP_DIR="$LE_CONFIG_HOME"
2315 if [ -z "$USER_AGENT" ]; then
2316 USER_AGENT="$DEFAULT_USER_AGENT"
2319 if [ -z "$HTTP_HEADER" ]; then
2320 HTTP_HEADER="$LE_CONFIG_HOME/http.header"
2323 _OLD_ACCOUNT_KEY="$LE_WORKING_DIR/account.key"
2324 _OLD_ACCOUNT_JSON="$LE_WORKING_DIR/account.json"
2326 _DEFAULT_ACCOUNT_KEY_PATH="$CA_DIR/account.key"
2327 _DEFAULT_ACCOUNT_JSON_PATH="$CA_DIR/account.json"
2328 if [ -z "$ACCOUNT_KEY_PATH" ]; then
2329 ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH"
2332 if [ -z "$ACCOUNT_JSON_PATH" ]; then
2333 ACCOUNT_JSON_PATH="$_DEFAULT_ACCOUNT_JSON_PATH"
2336 _DEFAULT_CERT_HOME="$LE_CONFIG_HOME"
2337 if [ -z "$CERT_HOME" ]; then
2338 CERT_HOME="$_DEFAULT_CERT_HOME"
2341 if [ -z "$ACME_OPENSSL_BIN" ] || [ ! -f "$ACME_OPENSSL_BIN" ] || [ ! -x "$ACME_OPENSSL_BIN" ]; then
2342 ACME_OPENSSL_BIN="$DEFAULT_OPENSSL_BIN"
2345 if [ -z "$1" ]; then
2352 if [ -z "$DOMAIN_PATH" ]; then
2353 domainhome="$CERT_HOME/$domain"
2354 domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX"
2356 DOMAIN_PATH="$domainhome"
2358 if _isEccKey "$_ilength"; then
2359 DOMAIN_PATH="$domainhomeecc"
2361 if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
2362 _info "The domain '$domain' seems to have a ECC cert already, please add '$
(__red
"--ecc")' parameter if you want to use that cert."
2365 _debug DOMAIN_PATH "$DOMAIN_PATH"
2368 if [ -z "$DOMAIN_BACKUP_PATH" ]; then
2369 DOMAIN_BACKUP_PATH="$DOMAIN_PATH/backup"
2372 if [ -z "$DOMAIN_CONF" ]; then
2373 DOMAIN_CONF="$DOMAIN_PATH/$domain.conf"
2376 if [ -z "$DOMAIN_SSL_CONF" ]; then
2377 DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf"
2380 if [ -z "$CSR_PATH" ]; then
2381 CSR_PATH="$DOMAIN_PATH/$domain.csr"
2383 if [ -z "$CERT_KEY_PATH" ]; then
2384 CERT_KEY_PATH="$DOMAIN_PATH/$domain.key"
2386 if [ -z "$CERT_PATH" ]; then
2387 CERT_PATH="$DOMAIN_PATH/$domain.cer"
2389 if [ -z "$CA_CERT_PATH" ]; then
2390 CA_CERT_PATH="$DOMAIN_PATH/ca.cer"
2392 if [ -z "$CERT_FULLCHAIN_PATH" ]; then
2393 CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer"
2395 if [ -z "$CERT_PFX_PATH" ]; then
2396 CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx"
2398 if [ -z "$CERT_PKCS8_PATH" ]; then
2399 CERT_PKCS8_PATH="$DOMAIN_PATH/$domain.pkcs8"
2402 if [ -z "$TLS_CONF" ]; then
2403 TLS_CONF="$DOMAIN_PATH/tls.validation.conf"
2405 if [ -z "$TLS_CERT" ]; then
2406 TLS_CERT="$DOMAIN_PATH/tls.validation.cert"
2408 if [ -z "$TLS_KEY" ]; then
2409 TLS_KEY="$DOMAIN_PATH/tls.validation.key"
2411 if [ -z "$TLS_CSR" ]; then
2412 TLS_CSR="$DOMAIN_PATH/tls.validation.csr"
2418 if [ -z "$_EXEC_TEMP_ERR" ]; then
2419 _EXEC_TEMP_ERR="$(_mktemp)"
2422 if [ "$_EXEC_TEMP_ERR" ]; then
2423 eval "$@ 2>>$_EXEC_TEMP_ERR"
2430 [ "$_EXEC_TEMP_ERR" ] && _err "$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR"
2434 _APACHECTL="apachectl"
2435 if ! _exists apachectl; then
2436 if _exists apache2ctl; then
2437 _APACHECTL="apache2ctl"
2439 _err "'apachectl not found. It seems that apache is not installed
, or you are not root user.
'"
2440 _err "Please use webroot mode to try again."
2445 if ! _exec $_APACHECTL -V >/dev/null; then
2450 if [ "$APACHE_HTTPD_CONF" ]; then
2451 _saveaccountconf APACHE_HTTPD_CONF "$APACHE_HTTPD_CONF"
2452 httpdconf="$APACHE_HTTPD_CONF"
2453 httpdconfname="$(basename "$httpdconfname")"
2455 httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
2456 _debug httpdconfname
"$httpdconfname"
2458 if [ -z "$httpdconfname" ]; then
2459 _err
"Can not read apache config file."
2463 if _startswith
"$httpdconfname" '/'; then
2464 httpdconf
="$httpdconfname"
2465 httpdconfname
="$(basename "$httpdconfname")"
2467 httpdroot
="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
2468 _debug httpdroot "$httpdroot"
2469 httpdconf="$httpdroot/$httpdconfname"
2470 httpdconfname="$(basename "$httpdconfname")"
2473 _debug httpdconf "$httpdconf"
2474 _debug httpdconfname "$httpdconfname"
2475 if [ ! -f "$httpdconf" ]; then
2476 _err "Apache Config file not found" "$httpdconf"
2483 if [ -z "$usingApache" ]; then
2487 if ! _apachePath; then
2491 if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
2492 _debug "No config file to restore."
2496 cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
2497 _debug "Restored: $httpdconf."
2498 if ! _exec $_APACHECTL -t; then
2500 _err "Sorry, restore apache config error, please contact me."
2503 _debug "Restored successfully."
2504 rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
2510 if ! _apachePath; then
2514 #test the conf first
2515 _info "Checking if there is an error in the apache config file before starting."
2517 if ! _exec "$_APACHECTL" -t >/dev/null; then
2519 _err "The apache config file has error, please fix it first, then try again."
2520 _err "Don't worry
, there is nothing changed to your system.
"
2527 _debug "Backup apache config
file" "$httpdconf"
2528 if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
2529 _err "Can not backup apache config
file, so abort. Don
't worry, the apache config is not changed."
2530 _err "This might be a bug of $PROJECT_NAME , please report issue: $PROJECT"
2533 _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
2534 _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
2535 _info "The backup file will be deleted on success, just forget it."
2539 apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
2540 _debug "apacheVer" "$apacheVer"
2541 apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)"
2542 apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
2544 if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ]; then
2546 Alias /.well-known/acme-challenge $ACME_DIR
2548 <Directory $ACME_DIR >
2554 Alias /.well-known/acme-challenge $ACME_DIR
2556 <Directory $ACME_DIR >
2563 _msg="$($_APACHECTL -t 2>&1)"
2564 if [ "$?" != "0" ]; then
2565 _err "Sorry, apache config error"
2566 if _restoreApache; then
2567 _err "The apache config file is restored."
2569 _err "Sorry, The apache config file can not be restored, please report bug."
2574 if [ ! -d "$ACME_DIR" ]; then
2575 mkdir -p "$ACME_DIR"
2576 chmod 755 "$ACME_DIR"
2579 if ! _exec "$_APACHECTL" graceful; then
2581 _err "$_APACHECTL graceful error, please contact me."
2589 #find the real nginx conf file
2592 #returns the real nginx conf file
2597 if ! _exists "nginx"; then
2598 _err "nginx command is not found."
2601 FOUND_REAL_NGINX_CONF=""
2602 FOUND_REAL_NGINX_CONF_LN=""
2603 BACKUP_NGINX_CONF=""
2604 _debug _croot "$_croot"
2605 _start_f="$(echo "$_croot" | cut -d : -f 2)"
2606 _debug _start_f "$_start_f"
2607 if [ -z "$_start_f" ]; then
2608 _debug "find start conf from nginx command"
2609 if [ -z "$NGINX_CONF" ]; then
2610 NGINX_CONF="$(nginx -V 2>&1 | _egrep_o "--conf-path=[^ ]* " | tr -d " ")"
2611 _debug NGINX_CONF "$NGINX_CONF"
2612 NGINX_CONF="$(echo "$NGINX_CONF" | cut -d = -f 2)"
2613 _debug NGINX_CONF "$NGINX_CONF"
2614 if [ ! -f "$NGINX_CONF" ]; then
2615 _err "'$NGINX_CONF' doesn't exist.
"
2619 _debug "Found nginx conf
file:$NGINX_CONF"
2621 _start_f="$NGINX_CONF"
2623 _debug "Start detect nginx conf
for $_d from
:$_start_f"
2624 if ! _checkConf "$_d" "$_start_f"; then
2625 _err "Can not
find conf
file for domain
$d"
2628 _info "Found conf
file: $FOUND_REAL_NGINX_CONF"
2630 _ln=$FOUND_REAL_NGINX_CONF_LN
2633 _lnn=$(_math $_ln + 1)
2635 _start_tag="$
(sed -n "$_lnn,${_lnn}p" "$FOUND_REAL_NGINX_CONF")"
2636 _debug "_start_tag
" "$_start_tag"
2637 if [ "$_start_tag" = "$NGINX_START" ]; then
2638 _info "The domain
$_d is already configured
, skip
"
2639 FOUND_REAL_NGINX_CONF=""
2643 mkdir -p "$DOMAIN_BACKUP_PATH"
2644 _backup_conf="$DOMAIN_BACKUP_PATH/$_d.nginx.conf
"
2645 _debug _backup_conf "$_backup_conf"
2646 BACKUP_NGINX_CONF="$_backup_conf"
2647 _info "Backup
$FOUND_REAL_NGINX_CONF to
$_backup_conf"
2648 if ! cp "$FOUND_REAL_NGINX_CONF" "$_backup_conf"; then
2649 _err "backup error.
"
2650 FOUND_REAL_NGINX_CONF=""
2654 _info "Check the nginx conf before setting up.
"
2655 if ! _exec "nginx
-t" >/dev/null; then
2660 _info "OK
, Set up nginx config
file"
2662 if ! sed -n "1,${_ln}p
" "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"; then
2663 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2664 _err "write nginx conf error
, but don
't worry, the file is restored to the original version."
2669 location ~ \"^/\.well-known/acme-challenge/([-_a-zA-Z0-9]+)\$\" {
2670 default_type text/plain;
2671 return 200 \"\$1.$_thumbpt\";
2674 " >>"$FOUND_REAL_NGINX_CONF"
2676 if ! sed -n "${_lnn},99999p" "$_backup_conf" >>"$FOUND_REAL_NGINX_CONF"; then
2677 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2678 _err "write nginx conf error, but don't worry
, the
file is restored.
"
2681 _debug3 "Modified config
:$
(cat $FOUND_REAL_NGINX_CONF)"
2682 _info "nginx conf is
done, let's check it again."
2683 if ! _exec "nginx -t" >/dev/null; then
2685 _err "It seems that nginx conf was broken, let's restore.
"
2686 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2690 _info "Reload nginx
"
2691 if ! _exec "nginx
-s reload
" >/dev/null; then
2693 _err "It seems that nginx reload error
, let's restore."
2694 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2705 _debug "Start _checkConf from:$_c_file"
2706 if [ ! -f "$2" ] && ! echo "$2" | grep '*$
' >/dev/null && echo "$2" | grep '*' >/dev/null; then
2709 if [ -f "$_w_f" ] && _checkConf "$1" "$_w_f"; then
2715 elif [ -f "$2" ]; then
2717 if _isRealNginxConf "$1" "$2"; then
2718 _debug "$2 is found."
2719 FOUND_REAL_NGINX_CONF="$2"
2722 if cat "$2" | tr "\t" " " | grep "^ *include *.*;" >/dev/null; then
2723 _debug "Try include files"
2724 for included in $(cat "$2" | tr "\t" " " | grep "^ *include *.*;" | sed "s/include //" | tr -d " ;"); do
2725 _debug "check included $included"
2726 if _checkConf "$1" "$included"; then
2733 _debug "$2 not found."
2740 _isRealNginxConf() {
2741 _debug "_isRealNginxConf $1 $2"
2742 if [ -f "$2" ]; then
2743 for _fln in $(tr "\t" ' ' <"$2" | grep -n "^ *server_name.* $1" | cut -d : -f 1); do
2745 if [ "$_fln" ]; then
2746 _start=$(tr "\t" ' ' <"$2" | _head_n "$_fln" | grep -n "^ *server *{" | _tail_n 1)
2747 _debug "_start" "$_start"
2748 _start_n=$(echo "$_start" | cut -d : -f 1)
2749 _start_nn=$(_math $_start_n + 1)
2750 _debug "_start_n" "$_start_n"
2751 _debug "_start_nn" "$_start_nn"
2753 _left="$(sed -n "${_start_nn},99999p" "$2")"
2754 _debug2 _left "$_left"
2755 if echo "$_left" | tr "\t" ' ' | grep -n "^ *server *{" >/dev/null; then
2756 _end=$(echo "$_left" | tr "\t" ' ' | grep -n "^ *server *{" | _head_n 1)
2757 _debug "_end" "$_end"
2758 _end_n=$(echo "$_end" | cut -d : -f 1)
2759 _debug "_end_n" "$_end_n"
2760 _seg_n=$(echo "$_left" | sed -n "1,${_end_n}p")
2765 _debug "_seg_n" "$_seg_n"
2767 if [ "$(echo "$_seg_n" | _egrep_o "^ *ssl *on *;")" ] \
2768 || [ "$(echo "$_seg_n" | _egrep_o "listen .* ssl[ |;]")" ]; then
2769 _debug "ssl on, skip"
2771 FOUND_REAL_NGINX_CONF_LN=$_fln
2772 _debug3 "found FOUND_REAL_NGINX_CONF_LN" "$FOUND_REAL_NGINX_CONF_LN"
2781 #restore all the nginx conf
2783 if [ -z "$NGINX_RESTORE_VLIST" ]; then
2784 _debug "No need to restore nginx, skip."
2787 _debug "_restoreNginx"
2788 _debug "NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST"
2790 for ng_entry in $(echo "$NGINX_RESTORE_VLIST" | tr "$dvsep" ' '); do
2791 _debug "ng_entry" "$ng_entry"
2792 _nd=$(echo "$ng_entry" | cut -d "$sep" -f 1)
2793 _ngconf=$(echo "$ng_entry" | cut -d "$sep" -f 2)
2794 _ngbackupconf=$(echo "$ng_entry" | cut -d "$sep" -f 3)
2795 _info "Restoring from $_ngbackupconf to $_ngconf"
2796 cat "$_ngbackupconf" >"$_ngconf"
2799 _info "Reload nginx"
2800 if ! _exec "nginx -s reload" >/dev/null; then
2802 _err "It seems that nginx reload error, please report bug."
2809 _stopserver "$serverproc"
2814 if [ -z "$DEBUG" ]; then
2823 _debug "_clearupdns"
2824 if [ "$dnsadded" != 1 ] || [ -z "$vlist" ]; then
2829 ventries=$(echo "$vlist" | tr ',' ' ')
2830 for ventry in $ventries; do
2831 d=$(echo "$ventry" | cut -d "$sep" -f 1)
2832 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
2833 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
2834 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
2835 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
2837 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
2838 _debug "$d is already verified, skip $vtype."
2842 if [ "$vtype" != "$VTYPE_DNS" ]; then
2843 _info "Skip $d for $vtype"
2847 d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
2848 _debug d_api "$d_api"
2850 if [ -z "$d_api" ]; then
2851 _info "Not Found domain api file: $d_api"
2856 if ! . "$d_api"; then
2857 _err "Load file $d_api error. Please check your api file and try again."
2861 rmcommand="${_currentRoot}_rm"
2862 if ! _exists "$rmcommand"; then
2863 _err "It seems that your api file doesn't define
$rmcommand"
2867 txtdomain="_acme-challenge.
$d"
2869 if ! $rmcommand "$txtdomain" "$txt"; then
2870 _err "Error removing txt
for domain
:$txtdomain"
2878 # webroot removelevel tokenfile
2879 _clearupwebbroot() {
2881 if [ -z "$__webroot" ]; then
2882 _debug "no webroot specified
, skip
"
2887 if [ "$2" = '1' ]; then
2888 _rmpath="$__webroot/.well-known
"
2889 elif [ "$2" = '2' ]; then
2890 _rmpath="$__webroot/.well-known
/acme-challenge
"
2891 elif [ "$2" = '3' ]; then
2892 _rmpath="$__webroot/.well-known
/acme-challenge
/$3"
2894 _debug "Skip
for removelevel
:$2"
2897 if [ "$_rmpath" ]; then
2898 if [ "$DEBUG" ]; then
2899 _debug "Debugging
, skip removing
: $_rmpath"
2909 _on_before_issue() {
2911 _chk_main_domain="$2"
2912 _chk_alt_domains="$3"
2914 _chk_local_addr="$5"
2915 _debug _on_before_issue
2917 if [ "$_chk_pre_hook" ]; then
2918 _info "Run pre hook
:'$_chk_pre_hook'"
2920 cd "$DOMAIN_PATH" && eval "$_chk_pre_hook"
2922 _err "Error when run pre hook.
"
2927 if _hasfield "$_chk_web_roots" "$NO_VALUE"; then
2928 if ! _exists "socat
"; then
2929 _err "Please
install socat tools first.
"
2934 _debug Le_LocalAddress "$_chk_local_addr"
2936 alldomains=$(echo "$_chk_main_domain,$_chk_alt_domains" | tr ',' ' ')
2940 for d in $alldomains; do
2941 _debug "Check
for domain
" "$d"
2942 _currentRoot="$
(_getfield
"$_chk_web_roots" $_index)"
2943 _debug "_currentRoot
" "$_currentRoot"
2944 _index=$(_math $_index + 1)
2946 if [ "$_currentRoot" = "$NO_VALUE" ]; then
2947 _info "Standalone mode.
"
2948 if [ -z "$Le_HTTPPort" ]; then
2951 _savedomainconf "Le_HTTPPort
" "$Le_HTTPPort"
2953 _checkport="$Le_HTTPPort"
2954 elif [ "$_currentRoot" = "$W_TLS" ]; then
2955 _info "Standalone tls mode.
"
2956 if [ -z "$Le_TLSPort" ]; then
2959 _savedomainconf "Le_TLSPort
" "$Le_TLSPort"
2961 _checkport="$Le_TLSPort"
2964 if [ "$_checkport" ]; then
2965 _debug _checkport "$_checkport"
2966 _checkaddr="$
(_getfield
"$_chk_local_addr" $_addrIndex)"
2967 _debug _checkaddr "$_checkaddr"
2969 _addrIndex="$
(_math
$_addrIndex + 1)"
2971 _netprc="$
(_ss
"$_checkport" |
grep "$_checkport")"
2972 netprc="$
(echo "$_netprc" |
grep "$_checkaddr")"
2973 if [ -z "$netprc" ]; then
2974 netprc="$
(echo "$_netprc" |
grep "$LOCAL_ANY_ADDRESS")"
2976 if [ "$netprc" ]; then
2978 _err "tcp port
$_checkport is already used by $
(echo "$netprc" | cut
-d : -f 4)"
2979 _err "Please stop it first
"
2985 if _hasfield "$_chk_web_roots" "apache
"; then
2986 if ! _setApache; then
2987 _err "set up apache error. Report error to me.
"
2999 _debug _on_issue_err
3000 _cleardomainconf "ORDER_FINALIZE
"
3001 if [ "$LOG_FILE" ]; then
3002 _err "Please check log
file for more details
: $LOG_FILE"
3004 _err "Please add
'--debug' or
'--log' to check
more details.
"
3005 _err "See
: $_DEBUG_WIKI"
3009 if [ "$_chk_post_hook" ]; then
3010 _info "Run post hook
:'$_chk_post_hook'"
3012 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
3014 _err "Error when run post hook.
"
3019 #trigger the validation to flush the pending authz
3020 _debug2 "_chk_vlist
" "$_chk_vlist"
3021 if [ "$_chk_vlist" ]; then
3023 _debug2 "start to deactivate authz
"
3024 ventries=$(echo "$_chk_vlist" | tr "$dvsep" ' ')
3025 for ventry in $ventries; do
3026 d=$(echo "$ventry" | cut -d "$sep" -f 1)
3027 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
3028 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
3029 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
3030 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
3031 __trigger_validation "$uri" "$keyauthorization"
3036 if [ "$IS_RENEW" = "1" ] && _hasfield "$Le_Webroot" "dns
"; then
3037 _err "$_DNS_MANUAL_ERR"
3040 if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
3041 _debug "$
(_dlg_versions
)"
3046 _on_issue_success() {
3048 _chk_renew_hook="$2"
3049 _debug _on_issue_success
3051 if [ "$_chk_post_hook" ]; then
3052 _info "Run post hook
:'$_chk_post_hook'"
3054 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
3056 _err "Error when run post hook.
"
3062 if [ "$IS_RENEW" ] && [ "$_chk_renew_hook" ]; then
3063 _info "Run renew hook
:'$_chk_renew_hook'"
3065 cd "$DOMAIN_PATH" && eval "$_chk_renew_hook"
3067 _err "Error when run renew hook.
"
3072 if _hasfield "$Le_Webroot" "dns
"; then
3073 _err "$_DNS_MANUAL_WARN"
3086 _regAccount "$_reg_length"
3089 __calcAccountKeyHash() {
3090 [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH"
3093 __calc_account_thumbprint() {
3094 printf "%s
" "$jwk" | tr -d ' ' | _digest "sha256
" | _url_replace
3101 _debug3 _regAccount "$_regAccount"
3105 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
3106 _info "mv $_OLD_ACCOUNT_KEY to
$ACCOUNT_KEY_PATH"
3107 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3110 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
3111 _info "mv $_OLD_ACCOUNT_JSON to
$ACCOUNT_JSON_PATH"
3112 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3115 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3116 if ! _create_account_key "$_reg_length"; then
3117 _err "Create account key error.
"
3122 if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
3126 if [ "$ACME_VERSION" = "2" ]; then
3127 regjson='{"termsOfServiceAgreed
": true}'
3128 if [ "$ACCOUNT_EMAIL" ]; then
3129 regjson='{"contact
": ["mailto
: '$ACCOUNT_EMAIL'"], "termsOfServiceAgreed
": true}'
3132 _reg_res="$ACME_NEW_ACCOUNT_RES"
3133 regjson='{"resource
": "'$_reg_res'", "terms-of-service-agreed
": true, "agreement
": "'$ACME_AGREEMENT'"}'
3134 if [ "$ACCOUNT_EMAIL" ]; then
3135 regjson='{"resource
": "'$_reg_res'", "contact
": ["mailto
: '$ACCOUNT_EMAIL'"], "terms-of-service-agreed
": true, "agreement
": "'$ACME_AGREEMENT'"}'
3139 _info "Registering account
"
3141 if ! _send_signed_request "${ACME_NEW_ACCOUNT}" "$regjson"; then
3142 _err "Register account Error
: $response"
3146 if [ "$code" = "" ] || [ "$code" = '201' ]; then
3147 echo "$response" >"$ACCOUNT_JSON_PATH"
3149 elif [ "$code" = '409' ]; then
3150 _info "Already registered
"
3152 _err "Register account Error
: $response"
3156 _accUri="$
(echo "$responseHeaders" |
grep "^Location:" | _head_n
1 | cut
-d ' ' -f 2 |
tr -d "\r\n")"
3157 _debug "_accUri
" "$_accUri"
3158 _savecaconf "ACCOUNT_URL
" "$_accUri"
3159 export ACCOUNT_URL="$ACCOUNT_URL"
3161 CA_KEY_HASH="$
(__calcAccountKeyHash
)"
3162 _debug "Calc CA_KEY_HASH
" "$CA_KEY_HASH"
3163 _savecaconf CA_KEY_HASH "$CA_KEY_HASH"
3165 if [ "$code" = '403' ]; then
3166 _err "It seems that the account key is already deactivated
, please use a new account key.
"
3170 ACCOUNT_THUMBPRINT="$
(__calc_account_thumbprint
)"
3171 _info "ACCOUNT_THUMBPRINT
" "$ACCOUNT_THUMBPRINT"
3174 #Implement deactivate account
3175 deactivateaccount() {
3178 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
3179 _info "mv $_OLD_ACCOUNT_KEY to
$ACCOUNT_KEY_PATH"
3180 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3183 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
3184 _info "mv $_OLD_ACCOUNT_JSON to
$ACCOUNT_JSON_PATH"
3185 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3188 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3189 _err "Account key is not found
at: $ACCOUNT_KEY_PATH"
3193 _accUri=$(_readcaconf "ACCOUNT_URL
")
3194 _debug _accUri "$_accUri"
3196 if [ -z "$_accUri" ]; then
3197 _err "The account url is empty
, please run
'--update-account' first to update the account info first
,"
3198 _err "Then try again.
"
3202 if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
3207 if _send_signed_request "$_accUri" "{\"resource
\": \"reg
\", \"status
\":\"deactivated
\"}" && _contains "$response" '"deactivated
"'; then
3208 _info "Deactivate account success
for $_accUri.
"
3209 _accid=$(echo "$response" | _egrep_o "\"id
\" *: *[^
,]*," | cut -d : -f 2 | tr -d ' ,')
3210 elif [ "$code" = "403" ]; then
3211 _info "The account is already deactivated.
"
3212 _accid=$(_getfield "$_accUri" "999" "/")
3214 _err "Deactivate
: account failed
for $_accUri.
"
3218 _debug "Account id
: $_accid"
3219 if [ "$_accid" ]; then
3220 _deactivated_account_path="$CA_DIR/deactivated
/$_accid"
3221 _debug _deactivated_account_path "$_deactivated_account_path"
3222 if mkdir -p "$_deactivated_account_path"; then
3223 _info "Moving deactivated account info to
$_deactivated_account_path/"
3224 mv "$CA_CONF" "$_deactivated_account_path/"
3225 mv "$ACCOUNT_JSON_PATH" "$_deactivated_account_path/"
3226 mv "$ACCOUNT_KEY_PATH" "$_deactivated_account_path/"
3228 _err "Can not create dir
: $_deactivated_account_path, try to remove the deactivated account key.
"
3230 rm -f "$ACCOUNT_JSON_PATH"
3231 rm -f "$ACCOUNT_KEY_PATH"
3236 # domain folder file
3242 if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
3243 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname"
3244 elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh
" ]; then
3245 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh
"
3246 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
3247 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname"
3248 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh
" ]; then
3249 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh
"
3250 elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
3251 d_api="$LE_WORKING_DIR/$_hookname"
3252 elif [ -f "$LE_WORKING_DIR/$_hookname.sh
" ]; then
3253 d_api="$LE_WORKING_DIR/$_hookname.sh
"
3254 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
3255 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname"
3256 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh
" ]; then
3257 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh
"
3260 printf "%s
" "$d_api"
3264 __get_domain_new_authz() {
3266 _info "Getting new-authz
for domain
" "$_gdnd"
3268 _Max_new_authz_retry_times=5
3270 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
3271 _debug "Try new-authz
for the
$_authz_i time.
"
3272 if ! _send_signed_request "${ACME_NEW_AUTHZ}" "{\"resource
\": \"new-authz
\", \"identifier
\": {\"type\": \"dns
\", \"value
\": \"$
(_idn
"$_gdnd")\"}}"; then
3273 _err "Can not get domain new authz.
"
3276 if _contains "$response" "No registration exists matching provided key
"; then
3277 _err "It seems there is an error
, but it
's recovered now, please try again."
3278 _err "If you see this message for a second time, please report bug: $(__green "$PROJECT")"
3279 _clearcaconf "CA_KEY_HASH"
3282 if ! _contains "$response" "An error occurred while processing your request"; then
3283 _info "The new-authz request is ok."
3286 _authz_i="$(_math "$_authz_i" + 1)"
3287 _info "The server is busy, Sleep $_authz_i to retry."
3291 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
3292 _err "new-authz retry reach the max $_Max_new_authz_retry_times times."
3295 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
3296 _err "new-authz error: $response"
3302 #uri keyAuthorization
3303 __trigger_validation() {
3304 _debug2 "tigger domain validation."
3306 _debug2 _t_url "$_t_url"
3308 _debug2 _t_key_authz "$_t_key_authz"
3309 if [ "$ACME_VERSION" = "2" ]; then
3310 _send_signed_request "$_t_url" "{\"keyAuthorization\": \"$_t_key_authz\"}"
3312 _send_signed_request "$_t_url" "{\"resource\": \"challenge\", \"keyAuthorization\": \"$_t_key_authz\"}"
3316 #webroot, domain domainlist keylength
3318 if [ -z "$2" ]; then
3319 _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
3322 if [ -z "$1" ]; then
3323 _usage "Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc."
3329 if _contains "$_main_domain" ","; then
3330 _main_domain=$(echo "$2,$3" | cut -d , -f 1)
3331 _alt_domains=$(echo "$2,$3" | cut -d , -f 2- | sed "s/,${NO_VALUE}$//")
3338 _real_fullchain="$9"
3344 #remove these later.
3345 if [ "$_web_roots" = "dns-cf" ]; then
3348 if [ "$_web_roots" = "dns-dp" ]; then
3351 if [ "$_web_roots" = "dns-cx" ]; then
3355 if [ ! "$IS_RENEW" ]; then
3356 _initpath "$_main_domain" "$_key_length"
3357 mkdir -p "$DOMAIN_PATH"
3360 _debug "Using ACME_DIRECTORY: $ACME_DIRECTORY"
3364 if [ -f "$DOMAIN_CONF" ]; then
3365 Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
3366 _debug Le_NextRenewTime "$Le_NextRenewTime"
3367 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
3368 _saved_domain=$(_readdomainconf Le_Domain)
3369 _debug _saved_domain "$_saved_domain"
3370 _saved_alt=$(_readdomainconf Le_Alt)
3371 _debug _saved_alt "$_saved_alt"
3372 if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then
3373 _info "Domains not changed."
3374 _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")"
3375 _info "Add '$
(__red
'--force')' to force to renew."
3378 _info "Domains have changed."
3383 _savedomainconf "Le_Domain" "$_main_domain"
3384 _savedomainconf "Le_Alt" "$_alt_domains"
3385 _savedomainconf "Le_Webroot" "$_web_roots"
3387 _savedomainconf "Le_PreHook" "$_pre_hook"
3388 _savedomainconf "Le_PostHook" "$_post_hook"
3389 _savedomainconf "Le_RenewHook" "$_renew_hook"
3391 if [ "$_local_addr" ]; then
3392 _savedomainconf "Le_LocalAddress" "$_local_addr"
3394 _cleardomainconf "Le_LocalAddress"
3397 Le_API="$ACME_DIRECTORY"
3398 _savedomainconf "Le_API" "$Le_API"
3400 if [ "$_alt_domains" = "$NO_VALUE" ]; then
3404 if [ "$_key_length" = "$NO_VALUE" ]; then
3408 if ! _on_before_issue "$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
3409 _err "_on_before_issue."
3413 _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")"
3414 _debug2 _saved_account_key_hash "$_saved_account_key_hash"
3416 if [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
3417 if ! _regAccount "$_accountkeylength"; then
3418 _on_issue_err "$_post_hook"
3422 _debug "_saved_account_key_hash is not changed, skip register account."
3425 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
3426 _info "Signing from existing CSR."
3428 _key=$(_readdomainconf Le_Keylength)
3429 _debug "Read key length:$_key"
3430 if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ] || [ "$Le_ForceNewDomainKey" = "1" ]; then
3431 if ! createDomainKey "$_main_domain" "$_key_length"; then
3432 _err "Create domain key error."
3434 _on_issue_err "$_post_hook"
3439 if ! _createcsr "$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
3440 _err "Create CSR error."
3442 _on_issue_err "$_post_hook"
3447 _savedomainconf "Le_Keylength" "$_key_length"
3451 _info "Getting domain auth token for each domain"
3454 if [ -z "$vlist" ]; then
3455 if [ "$ACME_VERSION" = "2" ] && [ -z "$ORDER_FINALIZE" ]; then
3456 #make new order request
3457 _identifiers
="{\"type\":\"dns\",\"value\":\"$_main_domain\"}"
3458 for d
in $
(echo "$_alt_domains" |
tr ',' ' '); do
3459 #todo: check wildcard ?
3461 _identifiers
="$_identifiers,{\"type\":\"dns\",\"value\":\"$d\"}"
3464 _debug2 _identifiers
"$_identifiers"
3465 if ! _send_signed_request
"$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
3466 _err
"Create new order error."
3468 _on_issue_err
"$_post_hook"
3472 ORDER_FINALIZE
="$(echo "$response"| tr -d '\r\n' | _egrep_o '"finalize
" *: *"[^
"]*"' | cut -d '"' -f 4)"
3473 _debug ORDER_FINALIZE
"$ORDER_FINALIZE"
3474 if [ -z "$ORDER_FINALIZE" ]; then
3475 _err
"ORDER_FINALIZE not found."
3477 _on_issue_err
"$_post_hook"
3481 #for dns manual mode
3482 _savedomainconf
"ORDER_FINALIZE" "$ORDER_FINALIZE"
3484 _authorizations_seg
="$(echo "$response"| tr -d '\r\n' | _egrep_o '"authorizations
" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
3485 _debug2 _authorizations_seg "$_authorizations_seg"
3486 if [ -z "$_authorizations_seg" ]; then
3487 _err "_authorizations_seg not found."
3489 _on_issue_err "$_post_hook"
3493 #domain and authz map
3494 _authorizations_map=""
3495 for _authz_url in $(echo "$_authorizations_seg" | tr ',' ' ' ); do
3496 _debug2 "_authz_url" "$_authz_url"
3497 if ! response="$(_get "$_authz_url")"; then
3498 _err "get to authz error."
3500 _on_issue_err "$_post_hook"
3504 response="$(echo "$response" | _normalizeJson)"
3505 _debug2 response "$response"
3506 _d="$(echo "$response" | _egrep_o '"value" *: *"[^"]*"' | cut -d : -f 2 | tr -d ' "')"
3508 _authorizations_map="$_d,$response
3509 $_authorizations_map"
3511 _debug2 _authorizations_map "$_authorizations_map"
3514 alldomains=$(echo "$_main_domain,$_alt_domains" | tr ',' ' ')
3517 for d in $alldomains; do
3518 _info "Getting webroot for domain" "$d"
3519 _index=$(_math $_index + 1)
3520 _w="$(echo $_web_roots | cut -d , -f $_index)"
3525 _debug "_currentRoot" "$_currentRoot"
3528 #todo, v2 wildcard force to use dns
3529 if _startswith "$_currentRoot" "dns"; then
3533 if [ "$_currentRoot" = "$W_TLS" ]; then
3534 if [ "$ACME_VERSION" = "2" ]; then
3541 if [ "$ACME_VERSION" = "2" ]; then
3542 response="$(echo "$_authorizations_map" | grep "^$d," | sed "s/$d,//")"
3543 _debug2 "response" "$response"
3544 if [ -z "$response" ]; then
3545 _err "get to authz error."
3547 _on_issue_err "$_post_hook"
3551 if ! __get_domain_new_authz "$d"; then
3553 _on_issue_err "$_post_hook"
3558 if [ -z "$thumbprint" ]; then
3559 thumbprint="$(__calc_account_thumbprint)"
3562 entry="$(printf "%s\n" "$response" | _egrep_o '[^\
{]*"type":"'$vtype'"[^\
}]*')"
3563 _debug entry "$entry"
3564 if [ -z "$entry" ]; then
3565 _err "Error, can not get domain token $d"
3567 _on_issue_err "$_post_hook"
3570 token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
3571 _debug token
"$token"
3573 if [ "$ACME_VERSION" = "2" ]; then
3574 uri
="$(printf "%s
\n" "$entry" | _egrep_o '"url
":"[^
"]*' | cut -d '"' -f 4 | _head_n 1)"
3576 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d '"' -f 4)"
3580 keyauthorization
="$token.$thumbprint"
3581 _debug keyauthorization
"$keyauthorization"
3583 if printf "%s" "$response" |
grep '"status":"valid"' >/dev
/null
2>&1; then
3584 _debug
"$d is already verified."
3585 keyauthorization
="$STATE_VERIFIED"
3586 _debug keyauthorization
"$keyauthorization"
3589 dvlist
="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
3590 _debug dvlist
"$dvlist"
3592 vlist
="$vlist$dvlist$dvsep"
3595 _debug vlist
"$vlist"
3598 ventries
=$
(echo "$vlist" |
tr "$dvsep" ' ')
3599 for ventry
in $ventries; do
3600 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
3601 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
3602 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
3603 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
3605 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
3606 _debug
"$d is already verified, skip $vtype."
3610 if [ "$vtype" = "$VTYPE_DNS" ]; then
3612 txtdomain
="_acme-challenge.$d"
3613 _debug txtdomain
"$txtdomain"
3614 txt
="$(printf "%s
" "$keyauthorization" | _digest "sha256
" | _url_replace)"
3617 d_api
="$(_findHook "$d" dnsapi "$_currentRoot")"
3619 _debug d_api
"$d_api"
3621 if [ "$d_api" ]; then
3622 _info
"Found domain api file: $d_api"
3624 _info
"$(__red "Add the following TXT record
:")"
3625 _info
"$(__red "Domain
: '$(__green "$txtdomain")'")"
3626 _info
"$(__red "TXT value
: '$(__green "$txt")'")"
3627 _info
"$(__red "Please be aware that you prepend _acme-challenge. before your domain
")"
3628 _info
"$(__red "so the resulting subdomain will be
: $txtdomain")"
3633 if ! .
"$d_api"; then
3634 _err
"Load file $d_api error. Please check your api file and try again."
3638 addcommand
="${_currentRoot}_add"
3639 if ! _exists
"$addcommand"; then
3640 _err
"It seems that your api file is not correct, it must have a function named: $addcommand"
3644 if ! $addcommand "$txtdomain" "$txt"; then
3645 _err
"Error add txt for domain:$txtdomain"
3650 if [ "$?" != "0" ]; then
3652 _on_issue_err
"$_post_hook" "$vlist"
3659 if [ "$dnsadded" = '0' ]; then
3660 _savedomainconf
"Le_Vlist" "$vlist"
3661 _debug
"Dns record not added yet, so, save to $DOMAIN_CONF and exit."
3662 _err
"Please add the TXT records to the domains, and retry again."
3664 _on_issue_err
"$_post_hook"
3670 if [ "$dnsadded" = '1' ]; then
3671 if [ -z "$Le_DNSSleep" ]; then
3672 Le_DNSSleep
="$DEFAULT_DNS_SLEEP"
3674 _savedomainconf
"Le_DNSSleep" "$Le_DNSSleep"
3677 _info
"Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
3678 _sleep
"$Le_DNSSleep"
3681 NGINX_RESTORE_VLIST
=""
3682 _debug
"ok, let's start to verify"
3685 ventries
=$
(echo "$vlist" |
tr "$dvsep" ' ')
3686 for ventry
in $ventries; do
3687 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
3688 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
3689 uri
=$
(echo "$ventry" | cut
-d "$sep" -f 3)
3690 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
3691 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
3693 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
3694 _info
"$d is already verified, skip $vtype."
3698 _info
"Verifying:$d"
3700 _debug
"keyauthorization" "$keyauthorization"
3703 token
="$(printf "%s
" "$keyauthorization" | cut -d '.' -f 1)"
3705 _debug
"_currentRoot" "$_currentRoot"
3707 if [ "$vtype" = "$VTYPE_HTTP" ]; then
3708 if [ "$_currentRoot" = "$NO_VALUE" ]; then
3709 _info
"Standalone mode server"
3710 _ncaddr
="$(_getfield "$_local_addr" "$_ncIndex")"
3711 _ncIndex
="$(_math $_ncIndex + 1)"
3712 _startserver
"$keyauthorization" "$_ncaddr"
3713 if [ "$?" != "0" ]; then
3715 _on_issue_err
"$_post_hook" "$vlist"
3719 _debug serverproc
"$serverproc"
3720 elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
3721 _info
"Stateless mode for domain:$d"
3723 elif _startswith
"$_currentRoot" "$NGINX"; then
3724 _info
"Nginx mode for domain:$d"
3725 #set up nginx server
3726 FOUND_REAL_NGINX_CONF
=""
3727 BACKUP_NGINX_CONF
=""
3728 if ! _setNginx
"$d" "$_currentRoot" "$thumbprint"; then
3730 _on_issue_err
"$_post_hook" "$vlist"
3734 if [ "$FOUND_REAL_NGINX_CONF" ]; then
3735 _realConf
="$FOUND_REAL_NGINX_CONF"
3736 _backup
="$BACKUP_NGINX_CONF"
3737 _debug _realConf
"$_realConf"
3738 NGINX_RESTORE_VLIST
="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
3742 if [ "$_currentRoot" = "apache" ]; then
3743 wellknown_path
="$ACME_DIR"
3745 wellknown_path
="$_currentRoot/.well-known/acme-challenge"
3746 if [ ! -d "$_currentRoot/.well-known" ]; then
3748 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
3755 _debug wellknown_path
"$wellknown_path"
3757 _debug
"writing token:$token to $wellknown_path/$token"
3759 mkdir
-p "$wellknown_path"
3761 if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
3762 _err
"$d:Can not write token to file : $wellknown_path/$token"
3763 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3765 _on_issue_err
"$_post_hook" "$vlist"
3769 if [ ! "$usingApache" ]; then
3770 if webroot_owner
=$
(_stat
"$_currentRoot"); then
3771 _debug
"Changing owner/group of .well-known to $webroot_owner"
3772 if ! _exec
"chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then
3773 _debug
"$(cat "$_EXEC_TEMP_ERR")"
3774 _exec_err
>/dev
/null
2>&1
3777 _debug
"not changing owner/group of webroot"
3783 elif [ "$vtype" = "$VTYPE_TLS" ]; then
3785 #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
3786 #_debug2 _hash_A "$_hash_A"
3787 #_x="$(echo $_hash_A | cut -c 1-32)"
3789 #_y="$(echo $_hash_A | cut -c 33-64)"
3791 #_SAN_A="$_x.$_y.token.acme.invalid"
3792 #_debug2 _SAN_A "$_SAN_A"
3795 _hash_B
="$(printf "%s
" "$keyauthorization" | _digest "sha256
" "hex
")"
3796 _debug2 _hash_B
"$_hash_B"
3797 _x
="$(echo "$_hash_B" | cut -c 1-32)"
3799 _y
="$(echo "$_hash_B" | cut -c 33-64)"
3802 #_SAN_B="$_x.$_y.ka.acme.invalid"
3804 _SAN_B
="$_x.$_y.acme.invalid"
3805 _debug2 _SAN_B
"$_SAN_B"
3807 _ncaddr
="$(_getfield "$_local_addr" "$_ncIndex")"
3808 _ncIndex
="$(_math "$_ncIndex" + 1)"
3809 if ! _starttlsserver
"$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
3810 _err
"Start tls server error."
3811 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3813 _on_issue_err
"$_post_hook" "$vlist"
3818 if ! __trigger_validation
"$uri" "$keyauthorization"; then
3819 _err
"$d:Can not get challenge: $response"
3820 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3822 _on_issue_err
"$_post_hook" "$vlist"
3826 if [ "$code" ] && [ "$code" != '202' ]; then
3827 if [ "$ACME_VERSION" = "2" ] && [ "$code" = '200' ]; then
3828 _debug
"trigger validation code: $code"
3830 _err
"$d:Challenge error: $response"
3831 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3833 _on_issue_err
"$_post_hook" "$vlist"
3839 if [ -z "$MAX_RETRY_TIMES" ]; then
3844 waittimes
=$
(_math
"$waittimes" + 1)
3845 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
3847 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3849 _on_issue_err
"$_post_hook" "$vlist"
3853 _debug
"sleep 2 secs to verify"
3856 response
="$(_get "$uri")"
3857 if [ "$?" != "0" ]; then
3858 _err
"$d:Verify error:$response"
3859 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3861 _on_issue_err
"$_post_hook" "$vlist"
3864 _debug2 original
"$response"
3866 response
="$(echo "$response" | _normalizeJson)"
3867 _debug2 response
"$response"
3869 status
=$
(echo "$response" | _egrep_o
'"status":"[^"]*' | cut
-d : -f 2 |
tr -d '"')
3870 if [ "$status" = "valid" ]; then
3871 _info
"$(__green Success)"
3872 _stopserver
"$serverproc"
3874 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3878 if [ "$status" = "invalid" ]; then
3879 error
="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error
":\{[^\}]*')"
3880 _debug2 error
"$error"
3881 errordetail
="$(echo "$error" | _egrep_o '"detail
": *"[^
"]*' | cut -d '"' -f 4)"
3882 _debug2 errordetail "$errordetail"
3883 if [ "$errordetail" ]; then
3884 _err "$d:Verify error:$errordetail"
3886 _err "$d:Verify error:$error"
3888 if [ "$DEBUG" ]; then
3889 if [ "$vtype" = "$VTYPE_HTTP" ]; then
3890 _debug "Debug: get token url."
3891 _get "http://$d/.well-known/acme-challenge/$token" "" 1
3894 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3896 _on_issue_err "$_post_hook" "$vlist"
3900 if [ "$status" = "pending" ]; then
3903 _err "$d:Verify error:$response"
3904 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3906 _on_issue_err "$_post_hook" "$vlist"
3915 _info "Verify finished, start to sign."
3916 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)"
3918 if [ "$ACME_VERSION" = "2" ]; then
3919 if ! _send_signed_request "${ORDER_FINALIZE}" "{\"csr\": \"$der\"}"; then
3921 _on_issue_err "$_post_hook"
3924 if [ "$code" != "200" ]; then
3925 _err "Sign failed, code is not 200."
3926 _on_issue_err "$_post_hook"
3929 Le_LinkCert="$(echo "$response"| tr -d '\r\n' | _egrep_o '"certificate" *: *"[^"]*"' | cut -d '"' -f 4)"
3931 if ! _get "$Le_LinkCert" > "$CERT_PATH"; then
3932 _err "Sign failed, code is not 200."
3933 _on_issue_err "$_post_hook"
3937 if ! _send_signed_request "${ACME_NEW_ORDER}" "{\"resource\": \"$ACME_NEW_ORDER_RES\", \"csr\": \"$der\"}" "needbase64"; then
3939 _on_issue_err "$_post_hook"
3943 Le_LinkCert="$(grep -i '^Location.
*$
' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
3944 echo "$BEGIN_CERT" >"$CERT_PATH"
3946 #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
3947 # _debug "Get cert failed. Let's try last response.
"
3948 # printf -- "%s
" "$_rcert" | _dbase64 "multiline
" | _base64 "multiline
" >> "$CERT_PATH"
3951 if ! printf -- "%s
" "$_rcert" | _dbase64 "multiline
" | _base64 "multiline
" >>"$CERT_PATH"; then
3952 _debug "Try cert link.
"
3953 _get "$Le_LinkCert" | _base64 "multiline
" >>"$CERT_PATH"
3956 echo "$END_CERT" >>"$CERT_PATH"
3959 _debug "Le_LinkCert
" "$Le_LinkCert"
3960 _savedomainconf "Le_LinkCert
" "$Le_LinkCert"
3962 if [ "$Le_LinkCert" ]; then
3963 _info "$
(__green
"Cert success.")"
3966 _info "Your cert is
in $
(__green
" $CERT_PATH ")"
3968 if [ -f "$CERT_KEY_PATH" ]; then
3969 _info "Your cert key is
in $
(__green
" $CERT_KEY_PATH ")"
3972 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
3974 if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
3976 _saveaccountconf "USER_PATH
" "$USER_PATH"
3980 if [ -z "$Le_LinkCert" ]; then
3981 response="$
(echo "$response" | _dbase64
"multiline" | _normalizeJson
)"
3982 _err "Sign failed
: $
(echo "$response" | _egrep_o
'"detail":"[^"]*"')"
3983 _on_issue_err "$_post_hook"
3987 _cleardomainconf "Le_Vlist
"
3989 Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
3991 if [ "$Le_LinkIssuer" ]; then
3992 if ! _contains "$Le_LinkIssuer" ":"; then
3993 _info "$
(__red
"Relative issuer link found.")"
3994 Le_LinkIssuer="$_ACME_SERVER_HOST$Le_LinkIssuer"
3996 _debug Le_LinkIssuer "$Le_LinkIssuer"
3997 _savedomainconf "Le_LinkIssuer
" "$Le_LinkIssuer"
3999 _link_issuer_retry=0
4001 while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do
4002 _debug _link_issuer_retry "$_link_issuer_retry"
4004 if [ "$ACME_VERSION" = "2" ]; then
4005 if _get "$Le_LinkIssuer" >"$CA_CERT_PATH"; then
4009 if _get "$Le_LinkIssuer" >"$CA_CERT_PATH.der
"; then
4010 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
4011 _base64 "multiline
" <"$CA_CERT_PATH.der
" >>"$CA_CERT_PATH"
4012 echo "$END_CERT" >>"$CA_CERT_PATH"
4014 _info "The intermediate CA cert is
in $
(__green
" $CA_CERT_PATH ")"
4015 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
4016 _info "And the full chain certs is there
: $
(__green
" $CERT_FULLCHAIN_PATH ")"
4018 rm -f "$CA_CERT_PATH.der
"
4022 _link_issuer_retry=$(_math $_link_issuer_retry + 1)
4023 _sleep "$_link_issuer_retry"
4025 if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then
4026 _err "Max retry
for issuer ca cert is reached.
"
4029 _debug "No Le_LinkIssuer header found.
"
4032 Le_CertCreateTime=$(_time)
4033 _savedomainconf "Le_CertCreateTime
" "$Le_CertCreateTime"
4035 Le_CertCreateTimeStr=$(date -u)
4036 _savedomainconf "Le_CertCreateTimeStr
" "$Le_CertCreateTimeStr"
4038 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ]; then
4039 Le_RenewalDays="$MAX_RENEW"
4041 _savedomainconf "Le_RenewalDays
" "$Le_RenewalDays"
4044 if [ "$CA_BUNDLE" ]; then
4045 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
4047 _clearaccountconf "CA_BUNDLE
"
4050 if [ "$CA_PATH" ]; then
4051 _saveaccountconf CA_PATH "$CA_PATH"
4053 _clearaccountconf "CA_PATH
"
4056 if [ "$HTTPS_INSECURE" ]; then
4057 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
4059 _clearaccountconf "HTTPS_INSECURE
"
4062 if [ "$Le_Listen_V4" ]; then
4063 _savedomainconf "Le_Listen_V4
" "$Le_Listen_V4"
4064 _cleardomainconf Le_Listen_V6
4065 elif [ "$Le_Listen_V6" ]; then
4066 _savedomainconf "Le_Listen_V6
" "$Le_Listen_V6"
4067 _cleardomainconf Le_Listen_V4
4070 if [ "$Le_ForceNewDomainKey" = "1" ]; then
4071 _savedomainconf "Le_ForceNewDomainKey
" "$Le_ForceNewDomainKey"
4073 _cleardomainconf Le_ForceNewDomainKey
4076 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
4078 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
4079 _savedomainconf "Le_NextRenewTimeStr
" "$Le_NextRenewTimeStr"
4081 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
4082 _savedomainconf "Le_NextRenewTime
" "$Le_NextRenewTime"
4084 if ! _on_issue_success "$_post_hook" "$_renew_hook"; then
4085 _err "Call hook error.
"
4089 if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
4090 _savedomainconf "Le_RealCertPath
" "$_real_cert"
4091 _savedomainconf "Le_RealCACertPath
" "$_real_ca"
4092 _savedomainconf "Le_RealKeyPath
" "$_real_key"
4093 _savedomainconf "Le_ReloadCmd
" "$_reload_cmd"
4094 _savedomainconf "Le_RealFullChainPath
" "$_real_fullchain"
4095 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
4103 if [ -z "$Le_Domain" ]; then
4104 _usage "Usage
: $PROJECT_ENTRY --renew -d domain.com
[--ecc]"
4110 _initpath "$Le_Domain" "$_isEcc"
4112 _info "$
(__green
"Renew: '$Le_Domain'")"
4113 if [ ! -f "$DOMAIN_CONF" ]; then
4114 _info "'$Le_Domain' is not a issued domain
, skip.
"
4118 if [ "$Le_RenewalDays" ]; then
4119 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
4124 if [ "$Le_API" ]; then
4125 if [ "$_OLD_CA_HOST" = "$Le_API" ]; then
4126 export Le_API="$DEFAULT_CA"
4127 _savedomainconf Le_API "$Le_API"
4129 if [ "$_OLD_STAGE_CA_HOST" = "$Le_API" ]; then
4130 export Le_API="$DEFAULT_STAGING_CA"
4131 _savedomainconf Le_API "$Le_API"
4133 export ACME_DIRECTORY="$Le_API"
4136 ACCOUNT_JSON_PATH=""
4138 _debug3 "initpath again.
"
4139 _initpath "$Le_Domain" "$_isEcc"
4142 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$
(_time
)" -lt "$Le_NextRenewTime" ]; then
4143 _info "Skip
, Next renewal
time is
: $
(__green
"$Le_NextRenewTimeStr")"
4144 _info "Add
'$(__red '--force')' to force to renew.
"
4145 return "$RENEW_SKIP"
4148 if [ "$IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then
4149 _info "Skip invalid cert
for: $Le_Domain"
4154 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"
4156 if [ "$res" != "0" ]; then
4160 if [ "$Le_DeployHook" ]; then
4161 _deploy "$Le_Domain" "$Le_DeployHook"
4170 #renewAll [stopRenewOnError]
4173 _stopRenewOnError="$1"
4174 _debug "_stopRenewOnError
" "$_stopRenewOnError"
4177 for di in "${CERT_HOME}"/*.*/; do
4179 if ! [ -d "$di" ]; then
4180 _debug "Not directory
, skip
: $di"
4186 if _endswith "$d" "$ECC_SUFFIX"; then
4187 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
4188 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
4190 renew "$d" "$_isEcc"
4193 _debug "Return code
: $rc"
4194 if [ "$rc" != "0" ]; then
4195 if [ "$rc" = "$RENEW_SKIP" ]; then
4197 elif [ "$_stopRenewOnError" ]; then
4198 _err "Error renew
$d, stop now.
"
4202 _err "Error renew
$d.
"
4213 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
4214 _usage "Usage
: $PROJECT_ENTRY --signcsr --csr mycsr.csr
-w /path
/to
/webroot
/a.com
/ "
4220 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4221 if [ "$?
" != "0" ]; then
4222 _err "Can not
read subject from csr
: $_csrfile"
4225 _debug _csrsubj "$_csrsubj"
4226 if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.'; then
4227 _info "It seems that the subject
: $_csrsubj is not a valid domain name. Drop it.
"
4231 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4232 if [ "$?
" != "0" ]; then
4233 _err "Can not
read domain list from csr
: $_csrfile"
4236 _debug "_csrdomainlist
" "$_csrdomainlist"
4238 if [ -z "$_csrsubj" ]; then
4239 _csrsubj="$
(_getfield
"$_csrdomainlist" 1)"
4240 _debug _csrsubj "$_csrsubj"
4241 _csrdomainlist="$
(echo "$_csrdomainlist" | cut
-d , -f 2-)"
4242 _debug "_csrdomainlist
" "$_csrdomainlist"
4245 if [ -z "$_csrsubj" ]; then
4246 _err "Can not
read subject from csr
: $_csrfile"
4250 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4251 if [ "$?
" != "0" ] || [ -z "$_csrkeylength" ]; then
4252 _err "Can not
read key length from csr
: $_csrfile"
4256 _initpath "$_csrsubj" "$_csrkeylength"
4257 mkdir -p "$DOMAIN_PATH"
4259 _info "Copy csr to
: $CSR_PATH"
4260 cp "$_csrfile" "$CSR_PATH"
4262 issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength"
4269 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
4270 _usage "Usage
: $PROJECT_ENTRY --showcsr --csr mycsr.csr
"
4276 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4277 if [ "$?
" != "0" ] || [ -z "$_csrsubj" ]; then
4278 _err "Can not
read subject from csr
: $_csrfile"
4282 _info "Subject
=$_csrsubj"
4284 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4285 if [ "$?
" != "0" ]; then
4286 _err "Can not
read domain list from csr
: $_csrfile"
4289 _debug "_csrdomainlist
" "$_csrdomainlist"
4291 _info "SubjectAltNames
=$_csrdomainlist"
4293 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4294 if [ "$?
" != "0" ] || [ -z "$_csrkeylength" ]; then
4295 _err "Can not
read key length from csr
: $_csrfile"
4298 _info "KeyLength
=$_csrkeylength"
4306 if [ "$_raw" ]; then
4307 printf "%s
\n" "Main_Domain
${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew
"
4308 for di in "${CERT_HOME}"/*.*/; do
4309 if ! [ -d "$di" ]; then
4310 _debug "Not directory
, skip
: $di"
4316 if _endswith "$d" "$ECC_SUFFIX"; then
4317 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
4318 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
4320 _initpath "$d" "$_isEcc"
4321 if [ -f "$DOMAIN_CONF" ]; then
4323 printf "%s
\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
4328 if _exists column; then
4329 list "raw
" | column -t -s "$_sep"
4331 list "raw
" | tr "$_sep" '\t'
4341 for _d_api in $(echo "$_hooks" | tr ',' " "); do
4342 _deployApi="$
(_findHook
"$_d" deploy
"$_d_api")"
4343 if [ -z "$_deployApi" ]; then
4344 _err "The deploy hook
$_d_api is not found.
"
4347 _debug _deployApi "$_deployApi"
4350 if ! . "$_deployApi"; then
4351 _err "Load
file $_deployApi error. Please check your api
file and try again.
"
4355 d_command="${_d_api}_deploy
"
4356 if ! _exists "$d_command"; then
4357 _err "It seems that your api
file is not correct
, it must have a
function named
: $d_command"
4361 if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
4362 _err "Error deploy
for domain
:$_d"
4366 _err "Deploy error.
"
4369 _info "$
(__green Success
)"
4379 if [ -z "$_hooks" ]; then
4380 _usage "Usage
: $PROJECT_ENTRY --deploy -d domain.com
--deploy-hook cpanel
[--ecc] "
4384 _initpath "$_d" "$_isEcc"
4385 if [ ! -d "$DOMAIN_PATH" ]; then
4386 _err "Domain is not valid
:'$_d'"
4392 _savedomainconf Le_DeployHook "$_hooks"
4394 _deploy "$_d" "$_hooks"
4399 if [ -z "$_main_domain" ]; then
4400 _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
]"
4408 _real_fullchain="$6"
4411 _initpath "$_main_domain" "$_isEcc"
4412 if [ ! -d "$DOMAIN_PATH" ]; then
4413 _err "Domain is not valid
:'$_main_domain'"
4417 _savedomainconf "Le_RealCertPath
" "$_real_cert"
4418 _savedomainconf "Le_RealCACertPath
" "$_real_ca"
4419 _savedomainconf "Le_RealKeyPath
" "$_real_key"
4420 _savedomainconf "Le_ReloadCmd
" "$_reload_cmd"
4421 _savedomainconf "Le_RealFullChainPath
" "$_real_fullchain"
4423 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
4426 #domain cert key ca fullchain reloadcmd backup-prefix
4432 _real_fullchain="$5"
4436 if [ "$_real_cert" = "$NO_VALUE" ]; then
4439 if [ "$_real_key" = "$NO_VALUE" ]; then
4442 if [ "$_real_ca" = "$NO_VALUE" ]; then
4445 if [ "$_reload_cmd" = "$NO_VALUE" ]; then
4448 if [ "$_real_fullchain" = "$NO_VALUE" ]; then
4452 _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
4453 mkdir -p "$_backup_path"
4455 if [ "$_real_cert" ]; then
4456 _info "Installing cert to
:$_real_cert"
4457 if [ -f "$_real_cert" ] && [ ! "$IS_RENEW" ]; then
4458 cp "$_real_cert" "$_backup_path/cert.bak
"
4460 cat "$CERT_PATH" >"$_real_cert"
4463 if [ "$_real_ca" ]; then
4464 _info "Installing CA to
:$_real_ca"
4465 if [ "$_real_ca" = "$_real_cert" ]; then
4466 echo "" >>"$_real_ca"
4467 cat "$CA_CERT_PATH" >>"$_real_ca"
4469 if [ -f "$_real_ca" ] && [ ! "$IS_RENEW" ]; then
4470 cp "$_real_ca" "$_backup_path/ca.bak
"
4472 cat "$CA_CERT_PATH" >"$_real_ca"
4476 if [ "$_real_key" ]; then
4477 _info "Installing key to
:$_real_key"
4478 if [ -f "$_real_key" ] && [ ! "$IS_RENEW" ]; then
4479 cp "$_real_key" "$_backup_path/key.bak
"
4481 if [ -f "$_real_key" ]; then
4482 cat "$CERT_KEY_PATH" >"$_real_key"
4484 cat "$CERT_KEY_PATH" >"$_real_key"
4485 chmod 700 "$_real_key"
4489 if [ "$_real_fullchain" ]; then
4490 _info "Installing full chain to
:$_real_fullchain"
4491 if [ -f "$_real_fullchain" ] && [ ! "$IS_RENEW" ]; then
4492 cp "$_real_fullchain" "$_backup_path/fullchain.bak
"
4494 cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain"
4497 if [ "$_reload_cmd" ]; then
4498 _info "Run reload cmd
: $_reload_cmd"
4501 export CERT_KEY_PATH
4503 export CERT_FULLCHAIN_PATH
4505 cd "$DOMAIN_PATH" && eval "$_reload_cmd"
4507 _info "$
(__green
"Reload success")"
4509 _err "Reload error
for :$Le_Domain"
4520 if ! _exists "$_CRONTAB" && _exists "fcrontab
"; then
4523 if ! _exists "$_CRONTAB"; then
4524 _err "crontab
/fcrontab doesn
't exist, so, we can not install cron jobs."
4525 _err "All your certs will not be renewed automatically."
4526 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
4530 _info "Installing cron job"
4531 if ! $_CRONTAB -l | grep "$PROJECT_ENTRY --cron"; then
4532 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
4533 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
4535 _err "Can not install cronjob, $PROJECT_ENTRY not found."
4539 if [ "$_c_home" ]; then
4540 _c_entry="--config-home \"$_c_home\" "
4543 random_minute=$(_math $_t % 60)
4544 if _exists uname && uname -a | grep SunOS >/dev/null; then
4547 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
4552 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
4556 if [ "$?" != "0" ]; then
4557 _err "Install cron job failed. You need to manually renew your certs."
4558 _err "Or you can add cronjob by yourself:"
4559 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4564 uninstallcronjob() {
4566 if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
4570 if ! _exists "$_CRONTAB"; then
4573 _info "Removing cron job"
4574 cr="$($_CRONTAB -l | grep "$PROJECT_ENTRY --cron")"
4576 if _exists uname && uname -a | grep solaris >/dev/null; then
4577 $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB --
4579 $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB -
4581 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4582 _info LE_WORKING_DIR
"$LE_WORKING_DIR"
4583 if _contains
"$cr" "--config-home"; then
4584 LE_CONFIG_HOME
="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
4585 _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
4594 if [ -z "$Le_Domain" ]; then
4595 _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com [--ecc]"
4601 _initpath "$Le_Domain" "$_isEcc"
4602 if [ ! -f "$DOMAIN_CONF" ]; then
4603 _err "$Le_Domain is not a issued domain, skip."
4607 if [ ! -f "$CERT_PATH" ]; then
4608 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
4612 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
4614 if [ -z "$cert" ]; then
4615 _err "Cert for $Le_Domain is empty found, skip."
4621 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
4622 uri="${ACME_REVOKE_CERT}"
4624 if [ -f "$CERT_KEY_PATH" ]; then
4625 _info "Try domain key first."
4626 if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
4627 if [ -z "$response" ]; then
4628 _info "Revoke success."
4632 _err "Revoke error by domain key."
4637 _info "Domain key file doesn't exists.
"
4640 _info "Try account key.
"
4642 if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
4643 if [ -z "$response" ]; then
4644 _info "Revoke success.
"
4648 _err "Revoke error.
"
4658 if [ -z "$Le_Domain" ]; then
4659 _usage "Usage
: $PROJECT_ENTRY --remove -d domain.com
[--ecc]"
4665 _initpath "$Le_Domain" "$_isEcc"
4666 _removed_conf="$DOMAIN_CONF.removed
"
4667 if [ ! -f "$DOMAIN_CONF" ]; then
4668 if [ -f "$_removed_conf" ]; then
4669 _err "$Le_Domain is already removed
, You can remove the folder by yourself
: $DOMAIN_PATH"
4671 _err "$Le_Domain is not a issued domain
, skip.
"
4676 if mv "$DOMAIN_CONF" "$_removed_conf"; then
4677 _info "$Le_Domain is removed
, the key and cert files are
in $
(__green
$DOMAIN_PATH)"
4678 _info "You can remove them by yourself.
"
4681 _err "Remove
$Le_Domain failed.
"
4692 if ! __get_domain_new_authz "$_d_domain"; then
4693 _err "Can not get domain new authz token.
"
4697 authzUri="$
(echo "$responseHeaders" |
grep "^Location:" | _head_n
1 | cut
-d ' ' -f 2 |
tr -d "\r\n")"
4698 _debug "authzUri
" "$authzUri"
4700 if [ "$code" ] && [ ! "$code" = '201' ]; then
4701 _err "new-authz error
: $response"
4705 entries="$
(echo "$response" | _egrep_o
'{ *"type":"[^"]*", *"status": *"valid", *"uri"[^}]*')"
4706 if [ -z "$entries" ]; then
4707 _info "No valid entries found.
"
4708 if [ -z "$thumbprint" ]; then
4709 thumbprint="$
(__calc_account_thumbprint
)"
4711 _debug "Trigger validation.
"
4713 entry="$
(printf "%s\n" "$response" | _egrep_o
'[^\{]*"type":"'$vtype'"[^\}]*')"
4714 _debug entry "$entry"
4715 if [ -z "$entry" ]; then
4716 _err "Error
, can not get domain token
$d"
4719 token="$
(printf "%s\n" "$entry" | _egrep_o
'"token":"[^"]*' | cut
-d : -f 2 |
tr -d '"')"
4720 _debug token "$token"
4722 uri="$
(printf "%s\n" "$entry" | _egrep_o
'"uri":"[^"]*' | cut
-d : -f 2,3 |
tr -d '"')"
4725 keyauthorization="$token.
$thumbprint"
4726 _debug keyauthorization "$keyauthorization"
4727 __trigger_validation "$uri" "$keyauthorization"
4732 _d_max_retry=$(echo "$entries" | wc -l)
4733 while [ "$_d_i" -lt "$_d_max_retry" ]; do
4734 _info "Deactivate
: $_d_domain"
4735 _d_i="$
(_math
$_d_i + 1)"
4736 entry="$
(echo "$entries" |
sed -n "${_d_i}p")"
4737 _debug entry "$entry"
4739 if [ -z "$entry" ]; then
4740 _info "No
more valid entry found.
"
4744 _vtype="$
(printf "%s\n" "$entry" | _egrep_o
'"type": *"[^"]*"' | cut
-d : -f 2 |
tr -d '"')"
4745 _debug _vtype "$_vtype"
4746 _info "Found
$_vtype"
4748 uri="$
(printf "%s\n" "$entry" | _egrep_o
'"uri":"[^"]*' | cut
-d : -f 2,3 |
tr -d '"')"
4751 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
4752 _info "Skip
$_vtype"
4756 _info "Deactivate
: $_vtype"
4758 if _send_signed_request "$authzUri" "{\"resource
\": \"authz
\", \"status
\":\"deactivated
\"}" && _contains "$response" '"deactivated
"'; then
4759 _info "Deactivate
: $_vtype success.
"
4761 _err "Can not deactivate
$_vtype.
"
4767 if [ "$_d_i" -eq "$_d_max_retry" ]; then
4768 _info "Deactivated success
!"
4770 _err "Deactivate failed.
"
4780 _debug _d_domain_list "$_d_domain_list"
4781 if [ -z "$
(echo $_d_domain_list | cut
-d , -f 1)" ]; then
4782 _usage "Usage
: $PROJECT_ENTRY --deactivate -d domain.com
[-d domain.com
]"
4785 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
4786 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
4789 if ! _deactivate "$_d_dm" "$_d_type"; then
4795 # Detect profile file if not specified as environment variable
4797 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
4803 SHELLTYPE="$
(basename "/$SHELL")"
4805 if [ "$SHELLTYPE" = "bash
" ]; then
4806 if [ -f "$HOME/.bashrc
" ]; then
4807 DETECTED_PROFILE="$HOME/.bashrc
"
4808 elif [ -f "$HOME/.bash_profile
" ]; then
4809 DETECTED_PROFILE="$HOME/.bash_profile
"
4811 elif [ "$SHELLTYPE" = "zsh
" ]; then
4812 DETECTED_PROFILE="$HOME/.zshrc
"
4815 if [ -z "$DETECTED_PROFILE" ]; then
4816 if [ -f "$HOME/.profile
" ]; then
4817 DETECTED_PROFILE="$HOME/.profile
"
4818 elif [ -f "$HOME/.bashrc
" ]; then
4819 DETECTED_PROFILE="$HOME/.bashrc
"
4820 elif [ -f "$HOME/.bash_profile
" ]; then
4821 DETECTED_PROFILE="$HOME/.bash_profile
"
4822 elif [ -f "$HOME/.zshrc
" ]; then
4823 DETECTED_PROFILE="$HOME/.zshrc
"
4827 echo "$DETECTED_PROFILE"
4832 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4835 #LOG_FILE=\"$DEFAULT_LOG_FILE\"
4842 " >"$ACCOUNT_CONF_PATH"
4850 if ! _exists "curl
" && ! _exists "wget
"; then
4851 _err "Please
install curl or wget first
, we need to access http resources.
"
4855 if [ -z "$_nocron" ]; then
4856 if ! _exists "crontab
" && ! _exists "fcrontab
"; then
4857 _err "It is recommended to
install crontab first. try to
install 'cron, crontab, crontabs or vixie-cron'.
"
4858 _err "We need to
set cron job to renew the certs automatically.
"
4859 _err "Otherwise
, your certs will not be able to be renewed automatically.
"
4860 if [ -z "$FORCE" ]; then
4861 _err "Please add
'--force' and try
install again to go without crontab.
"
4862 _err ".
/$PROJECT_ENTRY --install --force"
4868 if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then
4869 _err "Please
install openssl first. ACME_OPENSSL_BIN
=$ACME_OPENSSL_BIN"
4870 _err "We need openssl to generate keys.
"
4874 if ! _exists "socat
"; then
4875 _err "It is recommended to
install socat first.
"
4876 _err "We use socat
for standalone server
if you use standalone mode.
"
4877 _err "If you don
't use standalone mode, just ignore this warning."
4886 if [ -z "$_shebang" ]; then
4887 _usage "Usage: file shebang"
4890 cp "$_file" "$_file.tmp"
4891 echo "$_shebang" >"$_file"
4892 sed -n 2,99999p "$_file.tmp" >>"$_file"
4901 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4902 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
4903 echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
4904 echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
4905 echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
4908 if [ "$_c_home" ]; then
4909 _c_entry=" --config-home '$_c_home'"
4912 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
4913 if [ "$_c_home" ]; then
4914 _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
4916 _sed_i "/^export LE_CONFIG_HOME/d" "$_envfile"
4918 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4920 _profile="$(_detect_profile)"
4921 if [ "$_profile" ]; then
4922 _debug "Found profile: $_profile"
4923 _info "Installing alias to '$_profile'"
4924 _setopt "$_profile" ". \"$_envfile\""
4925 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
4927 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
4931 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
4932 _csh_profile="$HOME/.cshrc"
4933 if [ -f "$_csh_profile" ]; then
4934 _info "Installing alias to '$_csh_profile'"
4935 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
4936 if [ "$_c_home" ]; then
4937 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
4939 _sed_i "/^setenv LE_CONFIG_HOME/d" "$_cshfile"
4941 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4942 _setopt "$_csh_profile" "source \"$_cshfile\""
4946 _tcsh_profile="$HOME/.tcshrc"
4947 if [ -f "$_tcsh_profile" ]; then
4948 _info "Installing alias to '$_tcsh_profile'"
4949 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
4950 if [ "$_c_home" ]; then
4951 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
4953 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4954 _setopt "$_tcsh_profile" "source \"$_cshfile\""
4962 if [ -z "$LE_WORKING_DIR" ]; then
4963 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
4968 if ! _initpath; then
4969 _err "Install failed."
4972 if [ "$_nocron" ]; then
4973 _debug "Skip install cron job"
4976 if [ "$IN_CRON" != "1" ]; then
4977 if ! _precheck "$_nocron"; then
4978 _err "Pre-check failed, can not install."
4983 if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then
4984 _info "Using config home: $LE_CONFIG_HOME"
4985 _c_home="$LE_CONFIG_HOME"
4989 if [ -d "$HOME/.le" ]; then
4990 for envfile in "le.env" "le.sh.env"; do
4991 if [ -f "$HOME/.le/$envfile" ]; then
4992 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
4994 _info "You are upgrading from le.sh"
4995 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
4996 mv "$HOME/.le" "$LE_WORKING_DIR"
4997 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
5004 _info "Installing to $LE_WORKING_DIR"
5006 if [ ! -d "$LE_WORKING_DIR" ]; then
5007 if ! mkdir -p "$LE_WORKING_DIR"; then
5008 _err "Can not create working dir: $LE_WORKING_DIR"
5012 chmod 700 "$LE_WORKING_DIR"
5015 if [ ! -d "$LE_CONFIG_HOME" ]; then
5016 if ! mkdir -p "$LE_CONFIG_HOME"; then
5017 _err "Can not create config dir: $LE_CONFIG_HOME"
5021 chmod 700 "$LE_CONFIG_HOME"
5024 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
5026 if [ "$?" != "0" ]; then
5027 _err "Install failed, can not copy $PROJECT_ENTRY"
5031 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
5033 if [ "$IN_CRON" != "1" ]; then
5034 _installalias "$_c_home"
5037 for subf in $_SUB_FOLDERS; do
5038 if [ -d "$subf" ]; then
5039 mkdir -p "$LE_WORKING_DIR/$subf"
5040 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
5044 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
5048 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
5049 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
5052 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
5053 _saveaccountconf "CERT_HOME" "$CERT_HOME"
5056 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
5057 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
5060 if [ -z "$_nocron" ]; then
5061 installcronjob "$_c_home"
5064 if [ -z "$NO_DETECT_SH" ]; then
5066 if _exists bash; then
5067 _info "Good, bash is found, so change the shebang to use bash as preferred."
5068 _shebang='#!'"$(env bash -c "command -v bash")"
5069 _setShebang
"$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
5070 for subf
in $_SUB_FOLDERS; do
5071 if [ -d "$LE_WORKING_DIR/$subf" ]; then
5072 for _apifile
in "$LE_WORKING_DIR/$subf/"*.sh
; do
5073 _setShebang
"$_apifile" "$_shebang"
5086 if [ -z "$_nocron" ]; then
5093 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
5094 _info
"The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself."
5101 _profile
="$(_detect_profile)"
5102 if [ "$_profile" ]; then
5103 _info
"Uninstalling alias from: '$_profile'"
5104 text
="$(cat "$_profile")"
5105 echo "$text" |
sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
5108 _csh_profile
="$HOME/.cshrc"
5109 if [ -f "$_csh_profile" ]; then
5110 _info
"Uninstalling alias from: '$_csh_profile'"
5111 text
="$(cat "$_csh_profile")"
5112 echo "$text" |
sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
5115 _tcsh_profile
="$HOME/.tcshrc"
5116 if [ -f "$_tcsh_profile" ]; then
5117 _info
"Uninstalling alias from: '$_csh_profile'"
5118 text
="$(cat "$_tcsh_profile")"
5119 echo "$text" |
sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
5127 _info
"$(__green "===Starting cron
===")"
5128 if [ "$AUTO_UPGRADE" = "1" ]; then
5129 export LE_WORKING_DIR
5132 _err
"Cron:Upgrade failed!"
5136 .
"$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev
/null
5142 _info
"Auto upgraded to: $VER"
5147 _info
"$(__green "===End cron
===")"
5159 echo "Usage: $PROJECT_ENTRY command ...[parameters]....
5161 --help, -h Show this help message.
5162 --version, -v Show version info.
5163 --install Install $PROJECT_NAME to your system.
5164 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
5165 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
5166 --issue Issue a cert.
5167 --signcsr Issue a cert from an existing csr.
5168 --deploy Deploy the cert to your server.
5169 --install-cert Install the issued cert to apache/nginx or any other server.
5170 --renew, -r Renew a cert.
5171 --renew-all Renew all the certs.
5172 --revoke Revoke a cert.
5173 --remove Remove the cert from $PROJECT
5174 --list List all the certs.
5175 --showcsr Show the content of a csr.
5176 --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.
5177 --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
5178 --cron Run cron job to renew all the certs.
5179 --toPkcs Export the certificate and key to a pfx file.
5180 --toPkcs8 Convert to pkcs8 format.
5181 --update-account Update account info.
5182 --register-account Register account key.
5183 --deactivate-account Deactivate the account.
5184 --create-account-key Create an account private key, professional use.
5185 --create-domain-key Create an domain private key, professional use.
5186 --createCSR, -ccsr Create CSR , professional use.
5187 --deactivate Deactivate the domain authz, professional use.
5190 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
5191 --force, -f Used to force to install or force to renew a cert immediately.
5192 --staging, --test Use staging server, just for test.
5193 --debug Output debug info.
5194 --output-insecure Output all the sensitive messages. By default all the credentials/sensitive messages are hidden from the output/debug/log for secure.
5195 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
5196 --standalone Use standalone mode.
5197 --stateless Use stateless mode, see: $_STATELESS_WIKI
5198 --tls Use standalone tls mode.
5199 --apache Use apache mode.
5200 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
5201 --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.
5203 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
5204 --accountkeylength, -ak [2048] Specifies the account key length.
5205 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
5206 --log-level 1|2 Specifies the log level, default is 1.
5207 --syslog [0|3|6|7] Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
5209 These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
5211 --cert-file After issue/renew, the cert will be copied to this path.
5212 --key-file After issue/renew, the key will be copied to this path.
5213 --ca-file After issue/renew, the intermediate cert will be copied to this path.
5214 --fullchain-file After issue/renew, the fullchain cert will be copied to this path.
5216 --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
5218 --server SERVER ACME Directory Resource URI. (default: https://acme-v01.api.letsencrypt.org/directory)
5219 --accountconf Specifies a customized account config file.
5220 --home Specifies the home dir for $PROJECT_NAME .
5221 --cert-home Specifies the home dir to save all the certs, only valid for '--install' command.
5222 --config-home Specifies the home dir to save all the configurations.
5223 --useragent Specifies the user agent string. it will be saved for future use too.
5224 --accountemail Specifies the account email for registering, Only valid for the '--install' command.
5225 --accountkey Specifies the account key path, Only valid for the '--install' command.
5226 --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
5227 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
5228 --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
5229 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
5230 --listraw Only used for '--list' command, list the certs in raw format.
5231 --stopRenewOnError, -se Only valid for '--renew-all' command. Stop if one cert has error in renewal.
5232 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
5233 --ca-bundle Specifies the path to the CA certificate bundle to verify api server's certificate.
5234 --ca-path Specifies directory containing CA certificates in PEM format, used by wget or curl.
5235 --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.
5236 --no-color Do not output color text.
5237 --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
5238 --csr Specifies the input csr.
5239 --pre-hook Command to be run before obtaining any certificates.
5240 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obtain/renew is success or failed.
5241 --renew-hook Command to be run once for each successfully renewed certificate.
5242 --deploy-hook The hook file to deploy cert
5243 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
5244 --always-force-new-domain-key Generate new domain key when renewal. Otherwise, the domain key is not changed by default.
5245 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
5246 --listen-v4 Force standalone/tls server to listen at ipv4.
5247 --listen-v6 Force standalone/tls server to listen at ipv6.
5248 --openssl-bin Specifies a custom openssl bin location.
5249 --use-wget Force to use wget, if you have both curl and wget installed.
5255 _info
"Installing from online archive."
5257 if [ ! "$BRANCH" ]; then
5261 target
="$PROJECT/archive/$BRANCH.tar.gz"
5262 _info
"Downloading $target"
5263 localname
="$BRANCH.tar.gz"
5264 if ! _get
"$target" >$localname; then
5265 _err
"Download error."
5269 _info
"Extracting $localname"
5270 if ! (tar xzf
$localname || gtar xzf
$localname); then
5271 _err
"Extraction error."
5275 cd "$PROJECT_NAME-$BRANCH"
5276 chmod +x
$PROJECT_ENTRY
5277 if .
/$PROJECT_ENTRY install "$_nocron"; then
5278 _info
"Install success!"
5283 rm -rf "$PROJECT_NAME-$BRANCH"
5291 export LE_WORKING_DIR
5292 cd "$LE_WORKING_DIR"
5293 _installOnline
"nocron"
5295 _info
"Upgrade success!"
5298 _err
"Upgrade failed!"
5303 _processAccountConf
() {
5304 if [ "$_useragent" ]; then
5305 _saveaccountconf
"USER_AGENT" "$_useragent"
5306 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
5307 _saveaccountconf
"USER_AGENT" "$USER_AGENT"
5310 if [ "$_accountemail" ]; then
5311 _saveaccountconf
"ACCOUNT_EMAIL" "$_accountemail"
5312 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
5313 _saveaccountconf
"ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
5316 if [ "$_openssl_bin" ]; then
5317 _saveaccountconf
"ACME_OPENSSL_BIN" "$_openssl_bin"
5318 elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
5319 _saveaccountconf
"ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
5322 if [ "$_auto_upgrade" ]; then
5323 _saveaccountconf
"AUTO_UPGRADE" "$_auto_upgrade"
5324 elif [ "$AUTO_UPGRADE" ]; then
5325 _saveaccountconf
"AUTO_UPGRADE" "$AUTO_UPGRADE"
5328 if [ "$_use_wget" ]; then
5329 _saveaccountconf
"ACME_USE_WGET" "$_use_wget"
5330 elif [ "$ACME_USE_WGET" ]; then
5331 _saveaccountconf
"ACME_USE_WGET" "$ACME_USE_WGET"
5339 _altdomains
="$NO_VALUE"
5342 _accountkeylength
=""
5359 _stopRenewOnError
=""
5381 while [ ${#} -gt 0 ]; do
5413 --installcert |
-i |
--install-cert)
5419 --renewAll |
--renewall |
--renew-all)
5431 --installcronjob |
--install-cronjob)
5432 _CMD
="installcronjob"
5434 --uninstallcronjob |
--uninstall-cronjob)
5435 _CMD
="uninstallcronjob"
5446 --createAccountKey |
--createaccountkey |
-cak |
--create-account-key)
5447 _CMD
="createAccountKey"
5449 --createDomainKey |
--createdomainkey |
-cdk |
--create-domain-key)
5450 _CMD
="createDomainKey"
5452 --createCSR |
--createcsr |
-ccr)
5458 --updateaccount |
--update-account)
5459 _CMD
="updateaccount"
5461 --registeraccount |
--register-account)
5462 _CMD
="registeraccount"
5464 --deactivate-account)
5465 _CMD
="deactivateaccount"
5470 if [ "$_dvalue" ]; then
5471 if _startswith
"$_dvalue" "-"; then
5472 _err
"'$_dvalue' is not a valid domain for parameter '$1'"
5475 if _is_idn
"$_dvalue" && ! _exists idn
; then
5476 _err
"It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
5480 if [ -z "$_domain" ]; then
5483 if [ "$_altdomains" = "$NO_VALUE" ]; then
5484 _altdomains
="$_dvalue"
5486 _altdomains
="$_altdomains,$_dvalue"
5502 _server
="$ACME_DIRECTORY"
5503 export ACME_DIRECTORY
5507 if [ -z "$2" ] || _startswith
"$2" "-"; then
5508 DEBUG
="$DEBUG_LEVEL_DEFAULT"
5515 export OUTPUT_INSECURE
=1
5519 if [ -z "$_webroot" ]; then
5522 _webroot
="$_webroot,$wvalue"
5528 if [ -z "$_webroot" ]; then
5531 _webroot
="$_webroot,$wvalue"
5535 wvalue
="$MODE_STATELESS"
5536 if [ -z "$_webroot" ]; then
5539 _webroot
="$_webroot,$wvalue"
5544 _local_address
="$_local_address$lvalue,"
5549 if [ -z "$_webroot" ]; then
5552 _webroot
="$_webroot,$wvalue"
5557 if [ -z "$_webroot" ]; then
5560 _webroot
="$_webroot,$wvalue"
5565 if [ -z "$_webroot" ]; then
5568 _webroot
="$_webroot,$wvalue"
5573 if [ "$2" ] && ! _startswith
"$2" "-"; then
5577 if [ -z "$_webroot" ]; then
5580 _webroot
="$_webroot,$wvalue"
5585 Le_DNSSleep
="$_dnssleep"
5593 --accountkeylength |
-ak)
5594 _accountkeylength
="$2"
5598 --cert-file |
--certpath)
5602 --key-file |
--keypath)
5606 --ca-file |
--capath)
5610 --fullchain-file |
--fullchainpath)
5611 _fullchain_file
="$2"
5614 --reloadcmd |
--reloadCmd)
5624 ACCOUNT_CONF_PATH
="$_accountconf"
5631 --certhome |
--cert-home)
5633 CERT_HOME
="$_certhome"
5638 LE_CONFIG_HOME
="$_confighome"
5643 USER_AGENT
="$_useragent"
5648 ACCOUNT_EMAIL
="$_accountemail"
5653 ACCOUNT_KEY_PATH
="$_accountkey"
5658 Le_RenewalDays
="$_days"
5663 Le_HTTPPort
="$_httpport"
5668 Le_TLSPort
="$_tlsport"
5675 --stopRenewOnError |
--stoprenewonerror |
-se)
5676 _stopRenewOnError
="1"
5683 _ca_bundle
="$(_readlink -f "$2")"
5684 CA_BUNDLE
="$_ca_bundle"
5696 export ACME_NO_COLOR
=1
5718 if [ -z "$2" ] || _startswith
"$2" "-"; then
5719 _usage
"Please specify a value for '--deploy-hook'"
5722 _deploy_hook
="$_deploy_hook$2,"
5725 --ocsp-must-staple |
--ocsp)
5728 --always-force-new-domain-key)
5729 if [ -z "$2" ] || _startswith
"$2" "-"; then
5730 Le_ForceNewDomainKey
=1
5732 Le_ForceNewDomainKey
="$2"
5739 if _startswith
"$_logfile" '-'; then
5744 LOG_FILE
="$_logfile"
5745 if [ -z "$LOG_LEVEL" ]; then
5746 LOG_LEVEL
="$DEFAULT_LOG_LEVEL"
5751 LOG_LEVEL
="$_log_level"
5755 if ! _startswith
"$2" '-'; then
5759 if [ -z "$_syslog" ]; then
5760 _syslog
="$SYSLOG_LEVEL_DEFAULT"
5765 if [ -z "$_auto_upgrade" ] || _startswith
"$_auto_upgrade" '-'; then
5770 AUTO_UPGRADE
="$_auto_upgrade"
5774 Le_Listen_V4
="$_listen_v4"
5778 Le_Listen_V6
="$_listen_v6"
5782 ACME_OPENSSL_BIN
="$_openssl_bin"
5790 _err
"Unknown parameter : $1"
5798 if [ "${_CMD}" != "install" ]; then
5800 if [ "$_log" ]; then
5801 if [ -z "$_logfile" ]; then
5802 _logfile
="$DEFAULT_LOG_FILE"
5805 if [ "$_logfile" ]; then
5806 _saveaccountconf
"LOG_FILE" "$_logfile"
5807 LOG_FILE
="$_logfile"
5810 if [ "$_log_level" ]; then
5811 _saveaccountconf
"LOG_LEVEL" "$_log_level"
5812 LOG_LEVEL
="$_log_level"
5815 if [ "$_syslog" ]; then
5816 if _exists logger
; then
5817 if [ "$_syslog" = "0" ]; then
5818 _clearaccountconf
"SYS_LOG"
5820 _saveaccountconf
"SYS_LOG" "$_syslog"
5824 _err
"The 'logger' command is not found, can not enable syslog."
5825 _clearaccountconf
"SYS_LOG"
5833 _debug2 LE_WORKING_DIR
"$LE_WORKING_DIR"
5835 if [ "$DEBUG" ]; then
5837 if [ "$_server" ]; then
5838 _debug
"Using server: $_server"
5843 install) install "$_nocron" "$_confighome" ;;
5844 uninstall
) uninstall
"$_nocron" ;;
5847 issue
"$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address"
5850 deploy
"$_domain" "$_deploy_hook" "$_ecc"
5853 signcsr
"$_csr" "$_webroot"
5856 showcsr
"$_csr" "$_domain"
5859 installcert
"$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
5862 renew
"$_domain" "$_ecc"
5865 renewAll
"$_stopRenewOnError"
5868 revoke
"$_domain" "$_ecc"
5871 remove
"$_domain" "$_ecc"
5874 deactivate
"$_domain,$_altdomains"
5877 registeraccount
"$_accountkeylength"
5888 installcronjob
) installcronjob
"$_confighome" ;;
5889 uninstallcronjob
) uninstallcronjob
;;
5892 toPkcs
"$_domain" "$_password" "$_ecc"
5895 toPkcs8
"$_domain" "$_ecc"
5898 createAccountKey
"$_accountkeylength"
5901 createDomainKey
"$_domain" "$_keylength"
5904 createCSR
"$_domain" "$_altdomains" "$_ecc"
5908 if [ "$_CMD" ]; then
5909 _err
"Invalid command: $_CMD"
5916 if [ "$_ret" != "0" ]; then
5920 if [ "${_CMD}" = "install" ]; then
5921 if [ "$_log" ]; then
5922 if [ -z "$LOG_FILE" ]; then
5923 LOG_FILE
="$DEFAULT_LOG_FILE"
5925 _saveaccountconf
"LOG_FILE" "$LOG_FILE"
5928 if [ "$_log_level" ]; then
5929 _saveaccountconf
"LOG_LEVEL" "$_log_level"
5932 if [ "$_syslog" ]; then
5933 if _exists logger
; then
5934 if [ "$_syslog" = "0" ]; then
5935 _clearaccountconf
"SYS_LOG"
5937 _saveaccountconf
"SYS_LOG" "$_syslog"
5940 _err
"The 'logger' command is not found, can not enable syslog."
5941 _clearaccountconf
"SYS_LOG"
5951 if [ "$INSTALLONLINE" ]; then
5958 [ -z "$1" ] && showhelp
&& return
5959 if _startswith
"$1" '-'; then _process
"$@"; else "$@"; fi