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 DEFAULT_CA
="https://acme-v01.api.letsencrypt.org"
17 DEFAULT_AGREEMENT
="https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf"
19 DEFAULT_USER_AGENT
="$PROJECT_NAME/$VER ($PROJECT)"
20 DEFAULT_ACCOUNT_EMAIL
=""
22 DEFAULT_ACCOUNT_KEY_LENGTH
=2048
23 DEFAULT_DOMAIN_KEY_LENGTH
=2048
25 DEFAULT_OPENSSL_BIN
="openssl"
27 STAGE_CA
="https://acme-staging.api.letsencrypt.org"
31 VTYPE_TLS
="tls-sni-01"
32 #VTYPE_TLS2="tls-sni-02"
34 LOCAL_ANY_ADDRESS
="0.0.0.0"
44 MODE_STATELESS
="stateless"
46 STATE_VERIFIED
="verified_ok"
49 NGINX_START
="#ACME_NGINX_START"
50 NGINX_END
="#ACME_NGINX_END"
52 BEGIN_CSR
="-----BEGIN CERTIFICATE REQUEST-----"
53 END_CSR
="-----END CERTIFICATE REQUEST-----"
55 BEGIN_CERT
="-----BEGIN CERTIFICATE-----"
56 END_CERT
="-----END CERTIFICATE-----"
61 ECC_SUFFIX
="${ECC_SEP}ecc"
66 DEFAULT_LOG_LEVEL
="$LOG_LEVEL_1"
71 DEBUG_LEVEL_DEFAULT
=$DEBUG_LEVEL_1
74 HIDDEN_VALUE
="[hidden](please add '--output-insecure' to see this value)"
76 SYSLOG_ERROR
="user.error"
77 SYSLOG_INFO
="user.info"
78 SYSLOG_DEBUG
="user.debug"
87 SYSLOG_LEVEL_DEBUG_2
=8
89 SYSLOG_LEVEL_DEBUG_3
=9
91 SYSLOG_LEVEL_DEFAULT
=$SYSLOG_LEVEL_ERROR
95 _DEBUG_WIKI
="https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh"
97 _PREPARE_LINK
="https://github.com/Neilpang/acme.sh/wiki/Install-preparations"
99 _STATELESS_WIKI
="https://github.com/Neilpang/acme.sh/wiki/Stateless-Mode"
107 if [ "$__INTERACTIVE" ]; then
108 printf '\033[1;31;32m'
111 if [ "$__INTERACTIVE" ]; then
117 if [ "$__INTERACTIVE" ]; then
118 printf '\033[1;31;40m'
121 if [ "$__INTERACTIVE" ]; then
127 if [ -z "$NO_TIMESTAMP" ] ||
[ "$NO_TIMESTAMP" = "0" ]; then
128 printf -- "%s" "[$(date)] "
133 printf -- "%s" "$1='$2'"
139 echo "Diagnosis versions: "
140 echo "openssl:$ACME_OPENSSL_BIN"
141 if _exists
"$ACME_OPENSSL_BIN"; then
142 $ACME_OPENSSL_BIN version
2>&1
144 echo "$ACME_OPENSSL_BIN doesn't exists."
148 if [ "$_APACHECTL" ] && _exists
"$_APACHECTL"; then
151 echo "apache doesn't exists."
155 if _exists
"nc"; then
158 _debug
"nc doesn't exists."
164 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" = "$SYSLOG_LEVEL_NONE" ]; then
169 logger
-i -t "$PROJECT_NAME" -p "$_logclass" "$(_printargs "$@
")" >/dev
/null
2>&1
173 [ -z "$LOG_FILE" ] && return
174 _printargs
"$@" >>"$LOG_FILE"
179 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_INFO" ]; then
180 _syslog
"$SYSLOG_INFO" "$@"
186 _syslog
"$SYSLOG_ERROR" "$@"
188 if [ -z "$NO_TIMESTAMP" ] ||
[ "$NO_TIMESTAMP" = "0" ]; then
189 printf -- "%s" "[$(date)] " >&2
206 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
209 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
210 _syslog
"$SYSLOG_DEBUG" "$@"
212 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
217 #output the sensitive messages
219 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
220 if [ "$OUTPUT_INSECURE" = "1" ]; then
223 _log
"$1" "$HIDDEN_VALUE"
226 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
227 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
229 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
230 if [ "$OUTPUT_INSECURE" = "1" ]; then
233 _printargs
"$1" "$HIDDEN_VALUE" >&2
239 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
242 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
243 _syslog
"$SYSLOG_DEBUG" "$@"
245 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
251 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
252 if [ "$OUTPUT_INSECURE" = "1" ]; then
255 _log
"$1" "$HIDDEN_VALUE"
258 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
259 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
261 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
262 if [ "$OUTPUT_INSECURE" = "1" ]; then
265 _printargs
"$1" "$HIDDEN_VALUE" >&2
271 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
274 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
275 _syslog
"$SYSLOG_DEBUG" "$@"
277 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
283 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
284 if [ "$OUTPUT_INSECURE" = "1" ]; then
287 _log
"$1" "$HIDDEN_VALUE"
290 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
291 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
293 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
294 if [ "$OUTPUT_INSECURE" = "1" ]; then
297 _printargs
"$1" "$HIDDEN_VALUE" >&2
305 echo "$_str" |
grep "^$_sub" >/dev
/null
2>&1
311 echo "$_str" |
grep -- "$_sub\$" >/dev
/null
2>&1
317 echo "$_str" |
grep -- "$_sub" >/dev
/null
2>&1
324 if [ -z "$_field" ]; then
325 _usage
"Usage: str field [sep]"
329 if [ -z "$_sep" ]; then
333 for f
in $
(echo "$_str" |
tr ',' ' '); do
334 if [ "$f" = "$_field" ]; then
335 _debug2
"'$_str' contains '$_field'"
336 return 0 #contains ok
339 _debug2
"'$_str' does not contain '$_field'"
340 return 1 #not contains
348 if [ -z "$_findex" ]; then
349 _usage
"Usage: str field [sep]"
353 if [ -z "$_sep" ]; then
358 while [ "$_ffi" -gt "0" ]; do
359 _fv
="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")"
361 printf -- "%s" "$_fv"
364 _ffi
="$(_math "$_ffi" - 1)"
367 printf -- "%s" "$_str"
373 if [ -z "$cmd" ]; then
374 _usage
"Usage: _exists cmd"
378 if eval type type >/dev
/null
2>&1; then
379 eval type "$cmd" >/dev
/null
2>&1
380 elif command >/dev
/null
2>&1; then
381 command -v "$cmd" >/dev
/null
2>&1
383 which "$cmd" >/dev
/null
2>&1
386 _debug3
"$cmd exists=$ret"
393 printf "%s" "$(($_m_opts))"
425 if [ "$(printf '\x41')" != 'A' ]; then
434 _debug3 _URGLY_PRINTF
"$_URGLY_PRINTF"
436 if [ -z "$_URGLY_PRINTF" ]; then
437 h
="$(printf "%s
" "$hex" | cut -c $i-$j)"
443 ic
="$(printf "%s
" "$hex" | cut -c $i)"
444 jc
="$(printf "%s
" "$hex" | cut -c $j)"
445 if [ -z "$ic$jc" ]; then
448 ic
="$(_h_char_2_dec "$ic")"
449 jc
="$(_h_char_2_dec "$jc")"
450 printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")""%s"
453 i="$(_math "$i" + 2)"
454 j="$(_math "$j" + 2)"
460 _contains "${__OS__:=$(uname -a)}" "solaris" || _contains "${__OS__:=$(uname -a)}" "SunOS"
464 #this can only process ascii chars, should only be used when od command is missing as a backup way.
466 _debug2 "Using _ascii_hex"
470 while [ "$_h_i" -le "$_str_len" ]; do
471 _str_c="$(printf "%s" "$_str" | cut -c "$_h_i")"
472 printf " %02x" "'$_str_c"
473 _h_i="$
(_math
"$_h_i" + 1)"
477 #stdin output hexstr splited by one space
482 od -A n -v -t x1 | tr -s " " | sed 's/ $//' | tr -d "\r\t\n"
483 elif _exists hexdump; then
484 _debug3 "using
hexdump"
485 hexdump -v -e '/1 ""' -e '/1 " %02x
" ""'
486 elif _exists xxd; then
488 xxd -ps -c 20 -i | sed "s
/ 0x
/ /g
" | tr -d ",\n" | tr -s " "
490 _debug3 "using _ascii_hex
"
496 #url encode, no-preserved chars
497 #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
498 #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
500 #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
501 #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
503 #0 1 2 3 4 5 6 7 8 9 - _ . ~
504 #30 31 32 33 34 35 36 37 38 39 2d 5f 2e 7e
508 _hex_str=$(_hex_dump)
509 _debug3 "_url_encode
"
510 _debug3 "_hex_str
" "$_hex_str"
511 for _hex_code in $_hex_str; do
513 case "${_hex_code}" in
717 printf '%%%s' "$_hex_code"
727 if [ -z "$filename" ]; then
728 _usage "Usage
:_sed_i options filename
"
731 _debug2 options "$options"
732 if sed -h 2>&1 | grep "\
-i\
[SUFFIX
]" >/dev/null 2>&1; then
733 _debug "Using
sed -i"
734 sed -i "$options" "$filename"
736 _debug "No
-i support
in sed"
737 text="$
(cat "$filename")"
738 echo "$text" | sed "$options" >"$filename"
743 if ! egrep -o "$1" 2>/dev/null; then
744 sed -n 's/.*\('"$1"'\).*/\1/p'
748 #Usage: file startline endline
753 if [ -z "$endline" ]; then
754 _usage "Usage
: file startline endline
"
758 i="$
(grep -n -- "$startline" "$filename" | cut
-d : -f 1)"
760 _err "Can not
find start line
: $startline"
763 i="$
(_math
"$i" + 1)"
766 j="$
(grep -n -- "$endline" "$filename" | cut
-d : -f 1)"
768 _err "Can not
find end line
: $endline"
771 j="$
(_math
"$j" - 1)"
774 sed -n "$i,${j}p
" "$filename"
782 _debug3 "base64 multiline
:'$1'"
783 $ACME_OPENSSL_BIN base64 -e
785 _debug3 "base64 single line.
"
786 $ACME_OPENSSL_BIN base64 -e | tr -d '\r\n'
793 $ACME_OPENSSL_BIN base64 -d -A
795 $ACME_OPENSSL_BIN base64 -d
799 #Usage: hashalg [outputhex]
800 #Output Base64-encoded digest
803 if [ -z "$alg" ]; then
804 _usage "Usage
: _digest hashalg
"
810 if [ "$alg" = "sha256
" ] || [ "$alg" = "sha1
" ] || [ "$alg" = "md5
" ]; then
811 if [ "$outputhex" ]; then
812 $ACME_OPENSSL_BIN dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' '
814 $ACME_OPENSSL_BIN dgst -"$alg" -binary | _base64
817 _err "$alg is not supported yet
"
823 #Usage: hashalg secret_hex [outputhex]
830 if [ -z "$secret_hex" ]; then
831 _usage "Usage
: _hmac hashalg secret
[outputhex
]"
835 if [ "$alg" = "sha256
" ] || [ "$alg" = "sha1
" ]; then
836 if [ "$outputhex" ]; then
837 ($ACME_OPENSSL_BIN dgst -"$alg" -mac HMAC -macopt "hexkey
:$secret_hex" 2>/dev/null || $ACME_OPENSSL_BIN dgst -"$alg" -hmac "$
(printf "%s" "$secret_hex" | _h2b
)") | cut -d = -f 2 | tr -d ' '
839 $ACME_OPENSSL_BIN dgst -"$alg" -mac HMAC -macopt "hexkey
:$secret_hex" -binary 2>/dev/null || $ACME_OPENSSL_BIN dgst -"$alg" -hmac "$
(printf "%s" "$secret_hex" | _h2b
)" -binary
842 _err "$alg is not supported yet
"
848 #Usage: keyfile hashalg
849 #Output: Base64-encoded signature value
853 if [ -z "$alg" ]; then
854 _usage "Usage
: _sign keyfile hashalg
"
858 _sign_openssl="$ACME_OPENSSL_BIN dgst
-sign $keyfile "
859 if [ "$alg" = "sha256
" ]; then
860 _sign_openssl="$_sign_openssl -$alg"
862 _err "$alg is not supported yet
"
866 if grep "BEGIN RSA PRIVATE KEY
" "$keyfile" >/dev/null 2>&1; then
867 $_sign_openssl | _base64
868 elif grep "BEGIN EC PRIVATE KEY
" "$keyfile" >/dev/null 2>&1; then
869 if ! _signedECText="$
($_sign_openssl |
$ACME_OPENSSL_BIN asn1parse
-inform DER
)"; then
870 _err "Sign failed
: $_sign_openssl"
871 _err "Key
file: $keyfile"
872 _err "Key content
:$
(wc -l <"$keyfile") lises
"
875 _debug3 "_signedECText
" "$_signedECText"
876 _ec_r="$
(echo "$_signedECText" | _head_n
2 | _tail_n
1 | cut
-d : -f 4 |
tr -d "\r\n")"
877 _debug3 "_ec_r
" "$_ec_r"
878 _ec_s="$
(echo "$_signedECText" | _head_n
3 | _tail_n
1 | cut
-d : -f 4 |
tr -d "\r\n")"
879 _debug3 "_ec_s
" "$_ec_s"
880 printf "%s
" "$_ec_r$_ec_s" | _h2b | _base64
882 _err "Unknown key
file format.
"
892 if [ -z "$_length" ]; then
896 [ "$_length" != "1024" ] \
897 && [ "$_length" != "2048" ] \
898 && [ "$_length" != "3072" ] \
899 && [ "$_length" != "4096" ] \
900 && [ "$_length" != "8192" ]
903 # _createkey 2048|ec-256 file
907 _debug2 "_createkey
for file:$f"
909 if _startswith "$length" "ec-
"; then
910 length=$(printf "%s
" "$length" | cut -d '-' -f 2-100)
912 if [ "$length" = "256" ]; then
915 if [ "$length" = "384" ]; then
918 if [ "$length" = "521" ]; then
924 if [ -z "$length" ]; then
928 _debug "Use length
$length"
930 if ! touch "$f" >/dev/null 2>&1; then
931 _f_path="$
(dirname "$f")"
932 _debug _f_path "$_f_path"
933 if ! mkdir -p "$_f_path"; then
934 _err "Can not create path
: $_f_path"
939 if _isEccKey "$length"; then
940 _debug "Using ec name
: $eccname"
941 $ACME_OPENSSL_BIN ecparam -name "$eccname" -genkey 2>/dev/null >"$f"
943 _debug "Using RSA
: $length"
944 $ACME_OPENSSL_BIN genrsa "$length" 2>/dev/null >"$f"
947 if [ "$?
" != "0" ]; then
948 _err "Create key error.
"
956 _debug2 _is_idn_d "$_is_idn_d"
957 _idn_temp=$(printf "%s
" "$_is_idn_d" | tr -d '0-9' | tr -d 'a-z' | tr -d 'A-Z' | tr -d '.,-')
958 _debug2 _idn_temp "$_idn_temp"
963 #aa.com,bb.com,cc.com
966 if ! _is_idn "$__idn_d"; then
967 printf "%s
" "$__idn_d"
972 if _contains "$__idn_d" ','; then
974 for f in $(echo "$__idn_d" | tr ',' ' '); do
975 [ -z "$f" ] && continue
976 if [ -z "$_i_first" ]; then
981 idn --quiet "$f" | tr -d "\r\n"
984 idn "$__idn_d" | tr -d "\r\n"
987 _err "Please
install idn to process IDN names.
"
991 #_createcsr cn san_list keyfile csrfile conf
999 _debug2 domain "$domain"
1000 _debug2 domainlist "$domainlist"
1001 _debug2 csrkey "$csrkey"
1003 _debug2 csrconf "$csrconf"
1005 printf "[ req_distinguished_name
]\n[ req
]\ndistinguished_name
= req_distinguished_name
\nreq_extensions
= v3_req
\n[ v3_req
]\n\nkeyUsage
= nonRepudiation
, digitalSignature
, keyEncipherment
" >"$csrconf"
1007 if [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then
1009 _info "Single domain
" "$domain"
1011 domainlist="$
(_idn
"$domainlist")"
1012 _debug2 domainlist "$domainlist"
1013 if _contains "$domainlist" ","; then
1014 alt="DNS
:$
(echo "$domainlist" |
sed "s/,/,DNS:/g")"
1016 alt="DNS
:$domainlist"
1019 _info "Multi domain
" "$alt"
1020 printf -- "\nsubjectAltName
=$alt" >>"$csrconf"
1022 if [ "$Le_OCSP_Staple" ] || [ "$Le_OCSP_Stable" ]; then
1023 _savedomainconf Le_OCSP_Staple "$Le_OCSP_Staple"
1024 _cleardomainconf Le_OCSP_Stable
1025 printf -- "\nbasicConstraints
= CA
:FALSE
\n1.3.6.1.5.5.7.1.24=DER
:30:03:02:01:05" >>"$csrconf"
1028 _csr_cn="$
(_idn
"$domain")"
1029 _debug2 _csr_cn "$_csr_cn"
1030 if _contains "$
(uname
-a)" "MINGW
"; then
1031 $ACME_OPENSSL_BIN req -new -sha256 -key "$csrkey" -subj "//CN
=$_csr_cn" -config "$csrconf" -out "$csr"
1033 $ACME_OPENSSL_BIN req -new -sha256 -key "$csrkey" -subj "/CN
=$_csr_cn" -config "$csrconf" -out "$csr"
1037 #_signcsr key csr conf cert
1045 _msg="$
($ACME_OPENSSL_BIN x509
-req -days 365 -in "$csr" -signkey "$key" -extensions v3_req
-extfile "$conf" -out "$cert" 2>&1)"
1052 _readSubjectFromCSR() {
1054 if [ -z "$_csrfile" ]; then
1055 _usage "_readSubjectFromCSR mycsr.csr
"
1058 $ACME_OPENSSL_BIN req -noout -in "$_csrfile" -subject | _egrep_o "CN
*=.
*" | cut -d = -f 2 | cut -d / -f 1 | tr -d '\n'
1062 #echo comma separated domain list
1063 _readSubjectAltNamesFromCSR() {
1065 if [ -z "$_csrfile" ]; then
1066 _usage "_readSubjectAltNamesFromCSR mycsr.csr
"
1070 _csrsubj="$
(_readSubjectFromCSR
"$_csrfile")"
1071 _debug _csrsubj "$_csrsubj"
1073 _dnsAltnames="$
($ACME_OPENSSL_BIN req
-noout -text -in "$_csrfile" |
grep "^ *DNS:.*" |
tr -d ' \n')"
1074 _debug _dnsAltnames "$_dnsAltnames"
1076 if _contains "$_dnsAltnames," "DNS
:$_csrsubj,"; then
1077 _debug "AltNames contains subject
"
1078 _dnsAltnames="$
(printf "%s" "$_dnsAltnames," |
sed "s/DNS:$_csrsubj,//g")"
1080 _debug "AltNames doesn
't contain subject"
1083 printf "%s" "$_dnsAltnames" | sed "s/DNS://g"
1087 _readKeyLengthFromCSR() {
1089 if [ -z "$_csrfile" ]; then
1090 _usage "_readKeyLengthFromCSR mycsr.csr"
1094 _outcsr="$($ACME_OPENSSL_BIN req -noout -text -in "$_csrfile")"
1095 if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then
1097 echo "$_outcsr" | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' '
1100 echo "$_outcsr" | _egrep_o "(^ *|^RSA )Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1
1107 if _exists "ss"; then
1109 ss -ntpl | grep ":$_port "
1113 if _exists "netstat"; then
1114 _debug "Using: netstat"
1115 if netstat -h 2>&1 | grep "\-p proto" >/dev/null; then
1116 #for windows version netstat tool
1117 netstat -an -p tcp | grep "LISTENING" | grep ":$_port "
1119 if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then
1120 netstat -an -p tcp | grep LISTEN | grep ":$_port "
1121 elif netstat -help 2>&1 | grep -- '-P protocol
' >/dev/null; then
1123 netstat -an -P tcp | grep "\.$_port " | grep "LISTEN"
1125 netstat -ntpl | grep ":$_port "
1134 #domain [password] [isEcc]
1138 if [ -z "$domain" ]; then
1139 _usage "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]"
1145 _initpath "$domain" "$_isEcc"
1147 if [ "$pfxPassword" ]; then
1148 $ACME_OPENSSL_BIN pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH" -password "pass:$pfxPassword"
1150 $ACME_OPENSSL_BIN pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH"
1153 if [ "$?" = "0" ]; then
1154 _info "Success, Pfx is exported to: $CERT_PFX_PATH"
1163 if [ -z "$domain" ]; then
1164 _usage "Usage: $PROJECT_ENTRY --toPkcs8 -d domain [--ecc]"
1170 _initpath "$domain" "$_isEcc"
1172 $ACME_OPENSSL_BIN pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in "$CERT_KEY_PATH" -out "$CERT_PKCS8_PATH"
1174 if [ "$?" = "0" ]; then
1175 _info "Success, $CERT_PKCS8_PATH"
1181 createAccountKey() {
1182 _info "Creating account key"
1183 if [ -z "$1" ]; then
1184 _usage "Usage: $PROJECT_ENTRY --createAccountKey --accountkeylength 2048"
1189 _create_account_key "$length"
1193 _create_account_key() {
1197 if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then
1198 _debug "Use default length $DEFAULT_ACCOUNT_KEY_LENGTH"
1199 length="$DEFAULT_ACCOUNT_KEY_LENGTH"
1202 _debug length "$length"
1206 if [ -f "$ACCOUNT_KEY_PATH" ]; then
1207 _info "Account key exists, skip"
1210 #generate account key
1211 _createkey "$length" "$ACCOUNT_KEY_PATH"
1218 _info "Creating domain key"
1219 if [ -z "$1" ]; then
1220 _usage "Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]"
1227 if [ -z "$length" ]; then
1228 _debug "Use DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH"
1229 length="$DEFAULT_DOMAIN_KEY_LENGTH"
1232 _initpath "$domain" "$length"
1234 if [ ! -f "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$IS_RENEW" ]); then
1235 _createkey "$length" "$CERT_KEY_PATH"
1237 if [ "$IS_RENEW" ]; then
1238 _info "Domain key exists, skip"
1241 _err "Domain key exists, do you want to overwrite the key?"
1242 _err "Add '--force', and try again."
1249 # domain domainlist isEcc
1251 _info "Creating csr"
1252 if [ -z "$1" ]; then
1253 _usage "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]"
1261 _initpath "$domain" "$_isEcc"
1263 if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then
1264 _info "CSR exists, skip"
1268 if [ ! -f "$CERT_KEY_PATH" ]; then
1269 _err "The key file is not found: $CERT_KEY_PATH"
1270 _err "Please create the key file first."
1273 _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"
1278 tr '/+' '_-
' | tr -d '= '
1283 if date -u -d@"$1" 2>/dev/null; then
1288 if date -u -r "$1" 2>/dev/null; then
1293 if _exists adb; then
1294 _t_s_a=$(echo "0t${1}=Y" | adb)
1301 sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n"
1306 if stat -c '%U
:%G
' "$1" 2>/dev/null; then
1311 if stat -f '%Su
:%Sg
' "$1" 2>/dev/null; then
1315 return 1 #error, 'stat
' not found
1321 if [ -z "$keyfile" ]; then
1322 _usage "Usage: _calcjwk keyfile"
1326 if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then
1327 _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE"
1331 if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
1333 pub_exp=$($ACME_OPENSSL_BIN rsa -in "$keyfile" -noout -text | grep "^publicExponent:" | cut -d '(' -f 2 | cut -d 'x
' -f 2 | cut -d ')' -f 1)
1334 if [ "${#pub_exp}" = "5" ]; then
1337 _debug3 pub_exp "$pub_exp"
1339 e=$(echo "$pub_exp" | _h2b | _base64)
1342 modulus=$($ACME_OPENSSL_BIN rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2)
1343 _debug3 modulus "$modulus"
1344 n="$(printf "%s" "$modulus" | _h2b | _base64 | _url_replace)"
1347 jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
1350 JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}'
1351 JWK_HEADERPLACE_PART1='{"nonce": "'
1352 JWK_HEADERPLACE_PART2='", "alg": "RS256", "jwk": '$jwk'}'
1353 elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
1355 crv="$($ACME_OPENSSL_BIN ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
1358 if [ -z "$crv" ]; then
1359 _debug "Let's try ASN1 OID
"
1360 crv_oid="$
($ACME_OPENSSL_BIN ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep "^ASN1 OID:" | cut
-d ":" -f 2 |
tr -d " \r\n")"
1361 _debug3 crv_oid "$crv_oid"
1362 case "${crv_oid}" in
1373 _err "ECC oid
: $crv_oid"
1380 pubi="$
($ACME_OPENSSL_BIN ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep -n pub
: | cut
-d : -f 1)"
1381 pubi=$(_math "$pubi" + 1)
1382 _debug3 pubi "$pubi"
1384 pubj="$
($ACME_OPENSSL_BIN ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep -n "ASN1 OID:" | cut
-d : -f 1)"
1385 pubj=$(_math "$pubj" - 1)
1386 _debug3 pubj "$pubj"
1388 pubtext="$
($ACME_OPENSSL_BIN ec
-in "$keyfile" -noout -text 2>/dev
/null |
sed -n "$pubi,${pubj}p" |
tr -d " \n\r")"
1389 _debug3 pubtext "$pubtext"
1391 xlen="$
(printf "%s" "$pubtext" |
tr -d ':' |
wc -c)"
1392 xlen=$(_math "$xlen" / 4)
1393 _debug3 xlen "$xlen"
1395 xend=$(_math "$xlen" + 1)
1396 x="$
(printf "%s" "$pubtext" | cut
-d : -f 2-"$xend")"
1399 x64="$
(printf "%s" "$x" |
tr -d : | _h2b | _base64 | _url_replace
)"
1402 xend=$(_math "$xend" + 1)
1403 y="$
(printf "%s" "$pubtext" | cut
-d : -f "$xend"-10000)"
1406 y64="$
(printf "%s" "$y" |
tr -d : | _h2b | _base64 | _url_replace
)"
1409 jwk='{"crv
": "'$crv'", "kty
": "EC
", "x
": "'$x64'", "y
": "'$y64'"}'
1412 JWK_HEADER='{"alg
": "ES256
", "jwk
": '$jwk'}'
1413 JWK_HEADERPLACE_PART1='{"nonce
": "'
1414 JWK_HEADERPLACE_PART2='", "alg
": "ES256
", "jwk
": '$jwk'}'
1416 _err "Only RSA or EC key is supported.
"
1420 _debug3 JWK_HEADER "$JWK_HEADER"
1421 __CACHED_JWK_KEY_FILE="$keyfile"
1429 date -u "+%Y-
%m-
%d
%H
:%M
:%S
"
1433 if _exists mktemp; then
1434 if mktemp 2>/dev/null; then
1436 elif _contains "$
(mktemp
2>&1)" "-t prefix
" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then
1441 if [ -d "/tmp
" ]; then
1442 echo "/tmp
/${PROJECT_NAME}wefADf24sf.$
(_time
).tmp
"
1444 elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then
1445 echo "/$LE_TEMP_DIR/wefADf24sf.$
(_time
).tmp
"
1448 _err "Can not create temp
file.
"
1453 if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then
1454 HTTP_HEADER="$
(_mktemp
)"
1455 _debug2 HTTP_HEADER "$HTTP_HEADER"
1458 if [ "$__HTTP_INITIALIZED" ]; then
1459 if [ "$_ACME_CURL$_ACME_WGET" ]; then
1460 _debug2 "Http already initialized.
"
1465 if [ -z "$_ACME_CURL" ] && _exists "curl
"; then
1466 _ACME_CURL="curl
-L --silent --dump-header $HTTP_HEADER "
1467 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1468 _CURL_DUMP="$
(_mktemp
)"
1469 _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP "
1472 if [ "$CA_BUNDLE" ]; then
1473 _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE "
1478 if [ -z "$_ACME_WGET" ] && _exists "wget
"; then
1479 _ACME_WGET="wget
-q"
1480 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1481 _ACME_WGET="$_ACME_WGET -d "
1483 if [ "$CA_BUNDLE" ]; then
1484 _ACME_WGET="$_ACME_WGET --ca-certificate $CA_BUNDLE "
1488 #from wget 1.14: do not skip body on 404 error
1489 if [ "$_ACME_WGET" ] && _contains "$
($_ACME_WGET --help 2>&1)" "--content-on-error"; then
1490 _ACME_WGET="$_ACME_WGET --content-on-error "
1493 __HTTP_INITIALIZED=1
1497 # body url [needbase64] [POST|PUT]
1504 if [ -z "$httpmethod" ]; then
1509 _debug2 "body
" "$body"
1513 if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
1515 if [ "$HTTPS_INSECURE" ]; then
1516 _CURL="$_CURL --insecure "
1518 _debug "_CURL
" "$_CURL"
1519 if [ "$needbase64" ]; then
1520 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url" | _base64
)"
1522 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url")"
1525 if [ "$_ret" != "0" ]; then
1526 _err "Please refer to https
://curl.haxx.se
/libcurl
/c
/libcurl-errors.html
for error code
: $_ret"
1527 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1528 _err "Here is the curl dump log
:"
1529 _err "$
(cat "$_CURL_DUMP")"
1532 elif [ "$_ACME_WGET" ]; then
1534 if [ "$HTTPS_INSECURE" ]; then
1535 _WGET="$_WGET --no-check-certificate "
1537 _debug "_WGET
" "$_WGET"
1538 if [ "$needbase64" ]; then
1539 if [ "$httpmethod" = "POST
" ]; then
1540 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER" | _base64
)"
1542 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER" | _base64
)"
1545 if [ "$httpmethod" = "POST
" ]; then
1546 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER")"
1548 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER")"
1552 if [ "$_ret" = "8" ]; then
1554 _debug "wget returns
8, the server returns a
'Bad request' response
, lets process the response later.
"
1556 if [ "$_ret" != "0" ]; then
1557 _err "Please refer to https
://www.gnu.org
/software
/wget
/manual
/html_node
/Exit-Status.html
for error code
: $_ret"
1559 _sed_i "s
/^
*//g
" "$HTTP_HEADER"
1562 _err "Neither curl nor wget is found
, can not
do $httpmethod.
"
1564 _debug "_ret
" "$_ret"
1565 printf "%s
" "$response"
1569 # url getheader timeout
1576 _debug "timeout
" "$t"
1580 if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
1582 if [ "$HTTPS_INSECURE" ]; then
1583 _CURL="$_CURL --insecure "
1586 _CURL="$_CURL --connect-timeout $t"
1588 _debug "_CURL
" "$_CURL"
1589 if [ "$onlyheader" ]; then
1590 $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
1592 $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
1595 if [ "$ret" != "0" ]; then
1596 _err "Please refer to https
://curl.haxx.se
/libcurl
/c
/libcurl-errors.html
for error code
: $ret"
1597 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1598 _err "Here is the curl dump log
:"
1599 _err "$
(cat "$_CURL_DUMP")"
1602 elif [ "$_ACME_WGET" ]; then
1604 if [ "$HTTPS_INSECURE" ]; then
1605 _WGET="$_WGET --no-check-certificate "
1608 _WGET="$_WGET --timeout=$t"
1610 _debug "_WGET
" "$_WGET"
1611 if [ "$onlyheader" ]; then
1612 $_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'
1614 $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url"
1617 if [ "$ret" = "8" ]; then
1619 _debug "wget returns
8, the server returns a
'Bad request' response
, lets process the response later.
"
1621 if [ "$ret" != "0" ]; then
1622 _err "Please refer to https
://www.gnu.org
/software
/wget
/manual
/html_node
/Exit-Status.html
for error code
: $ret"
1626 _err "Neither curl nor wget is found
, can not
do GET.
"
1637 if ! tail -n "$1" 2>/dev/null; then
1643 # url payload needbase64 keyfile
1644 _send_signed_request() {
1649 if [ -z "$keyfile" ]; then
1650 keyfile="$ACCOUNT_KEY_PATH"
1653 _debug payload "$payload"
1655 if ! _calcjwk "$keyfile"; then
1659 payload64=$(printf "%s
" "$payload" | _base64 | _url_replace)
1660 _debug3 payload64 "$payload64"
1662 MAX_REQUEST_RETRY_TIMES=5
1663 _request_retry_times=0
1664 while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
1665 _debug3 _request_retry_times "$_request_retry_times"
1666 if [ -z "$_CACHED_NONCE" ]; then
1667 _debug2 "Get nonce.
"
1668 nonceurl="$API/directory
"
1669 _headers="$
(_get
"$nonceurl" "onlyheader")"
1671 if [ "$?
" != "0" ]; then
1672 _err "Can not connect to
$nonceurl to get nonce.
"
1676 _debug2 _headers "$_headers"
1678 _CACHED_NONCE="$
(echo "$_headers" |
grep "Replay-Nonce:" | _head_n
1 |
tr -d "\r\n " | cut
-d ':' -f 2)"
1679 _debug2 _CACHED_NONCE "$_CACHED_NONCE"
1681 _debug2 "Use _CACHED_NONCE
" "$_CACHED_NONCE"
1683 nonce="$_CACHED_NONCE"
1684 _debug2 nonce "$nonce"
1686 protected="$JWK_HEADERPLACE_PART1$nonce$JWK_HEADERPLACE_PART2"
1687 _debug3 protected "$protected"
1689 protected64="$
(printf "%s" "$protected" | _base64 | _url_replace
)"
1690 _debug3 protected64 "$protected64"
1692 if ! _sig_t="$
(printf "%s" "$protected64.$payload64" | _sign
"$keyfile" "sha256")"; then
1693 _err "Sign request failed.
"
1696 _debug3 _sig_t "$_sig_t"
1698 sig="$
(printf "%s" "$_sig_t" | _url_replace
)"
1701 body="{\"header
\": $JWK_HEADER, \"protected
\": \"$protected64\", \"payload
\": \"$payload64\", \"signature
\": \"$sig\"}"
1702 _debug3 body "$body"
1704 response="$
(_post
"$body" "$url" "$needbase64")"
1707 if [ "$?
" != "0" ]; then
1708 _err "Can not post to
$url"
1711 _debug2 original "$response"
1712 response="$
(echo "$response" | _normalizeJson
)"
1714 responseHeaders="$
(cat "$HTTP_HEADER")"
1716 _debug2 responseHeaders "$responseHeaders"
1717 _debug2 response "$response"
1718 code="$
(grep "^HTTP" "$HTTP_HEADER" | _tail_n
1 | cut
-d " " -f 2 |
tr -d "\r\n")"
1721 _CACHED_NONCE="$
(echo "$responseHeaders" |
grep "Replay-Nonce:" | _head_n
1 |
tr -d "\r\n " | cut
-d ':' -f 2)"
1723 if _contains "$response" "JWS has invalid anti-replay nonce
"; then
1724 _info "It seems the CA server is busy now
, let's wait and retry."
1725 _request_retry_times=$(_math "$_request_retry_times" + 1)
1734 #setopt "file" "opt" "=" "value" [";"]
1741 if [ -z "$__opt" ]; then
1742 _usage usage: _setopt '"file" "opt" "=" "value" [";"]'
1745 if [ ! -f "$__conf" ]; then
1749 if grep -n "^$__opt$__sep" "$__conf" >/dev/null; then
1751 if _contains "$__val" "&"; then
1752 __val="$(echo "$__val" | sed 's
/&/\\&/g
')"
1754 text="$(cat "$__conf")"
1755 printf -- "%s\n" "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
1757 elif grep -n "^#$__opt$__sep" "$__conf" >/dev/null; then
1758 if _contains "$__val" "&"; then
1759 __val="$(echo "$__val" | sed 's
/&/\\&/g
')"
1761 text="$(cat "$__conf")"
1762 printf -- "%s\n" "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
1766 echo "$__opt$__sep$__val$__end" >>"$__conf"
1768 _debug3 "$(grep -n "^$__opt$__sep" "$__conf")"
1771 #_save_conf file key value
1777 if [ "$_s_c_f" ]; then
1778 _setopt "$_s_c_f" "$_sdkey" "=" "'$_sdvalue'"
1780 _err "config file is empty, can not save $_sdkey=$_sdvalue"
1784 #_clear_conf file key
1788 if [ "$_c_c_f" ]; then
1789 _conf_data="$(cat "$_c_c_f")"
1790 echo "$_conf_data" | sed "s/^$_sdkey *=.*$//" >"$_c_c_f"
1792 _err "config file is empty, can not clear"
1796 #_read_conf file key
1800 if [ -f "$_r_c_f" ]; then
1802 eval "$(grep "^$_sdkey *=" "$_r_c_f")"
1803 eval "printf \"%s\" \"\$$_sdkey\""
1806 _debug "config file is empty, can not read $_sdkey"
1810 #_savedomainconf key value
1811 #save to domain.conf
1813 _save_conf "$DOMAIN_CONF" "$1" "$2"
1816 #_cleardomainconf key
1817 _cleardomainconf() {
1818 _clear_conf "$DOMAIN_CONF" "$1"
1821 #_readdomainconf key
1823 _read_conf "$DOMAIN_CONF" "$1"
1826 #_saveaccountconf key value
1827 _saveaccountconf() {
1828 _save_conf "$ACCOUNT_CONF_PATH" "$1" "$2"
1831 #_clearaccountconf key
1832 _clearaccountconf() {
1833 _clear_conf "$ACCOUNT_CONF_PATH" "$1"
1836 #_savecaconf key value
1838 _save_conf "$CA_CONF" "$1" "$2"
1843 _read_conf "$CA_CONF" "$1"
1846 #_clearaccountconf key
1848 _clear_conf "$CA_CONF" "$1"
1851 # content localaddress
1855 _debug "ncaddr" "$ncaddr"
1857 _debug "startserver: $$"
1858 nchelp="$(nc -h 2>&1)"
1860 _debug Le_HTTPPort "$Le_HTTPPort"
1861 _debug Le_Listen_V4 "$Le_Listen_V4"
1862 _debug Le_Listen_V6 "$Le_Listen_V6"
1865 if [ "$Le_Listen_V4" ]; then
1867 elif [ "$Le_Listen_V6" ]; then
1871 if [ "$Le_Listen_V4$Le_Listen_V6$ncaddr" ]; then
1872 if ! _contains "$nchelp" "-4"; then
1873 _err "The nc doesn't support
'-4', '-6' or local-address
, please
install 'netcat-openbsd' and try again.
"
1874 _err "See $
(__green
$_PREPARE_LINK)"
1879 if echo "$nchelp" | grep "\
-q[ ,]" >/dev/null; then
1880 _NC="$_NC -q 1 -l $ncaddr"
1882 if echo "$nchelp" | grep "GNU netcat
" >/dev/null && echo "$nchelp" | grep "\
-c, \
-\
-close" >/dev/null; then
1883 _NC="$_NC -c -l $ncaddr"
1884 elif echo "$nchelp" | grep "\
-N" | grep "Shutdown the network socket after EOF on stdin
" >/dev/null; then
1885 _NC="$_NC -N -l $ncaddr"
1887 _NC="$_NC -l $ncaddr"
1894 if _contains "$nchelp" "nmap.org
"; then
1895 _debug "Using ncat
: nmap.org
"
1896 if ! _exec "printf \"%s
\r\n\r\n%s
\" \"HTTP
/1.1 200 OK
\" \"$content\" |
$_NC \"$Le_HTTPPort\" >&2"; then
1900 if [ "$DEBUG" ]; then
1907 if ! _exec "printf \"%s
\r\n\r\n%s
\" \"HTTP
/1.1 200 OK
\" \"$content\" |
$_NC -p \"$Le_HTTPPort\" >&2"; then
1908 _exec "printf \"%s
\r\n\r\n%s
\" \"HTTP
/1.1 200 OK
\" \"$content\" |
$_NC \"$Le_HTTPPort\" >&2"
1911 if [ "$?
" != "0" ]; then
1912 _err "nc listen error.
"
1916 if [ "$DEBUG" ]; then
1925 if [ -z "$pid" ]; then
1929 _debug2 "Le_HTTPPort
" "$Le_HTTPPort"
1930 if [ "$Le_HTTPPort" ]; then
1931 if [ "$DEBUG" ] && [ "$DEBUG" -gt "3" ]; then
1932 _get "http
://localhost
:$Le_HTTPPort" "" 1
1934 _get "http
://localhost
:$Le_HTTPPort" "" 1 >/dev/null 2>&1
1938 _debug2 "Le_TLSPort
" "$Le_TLSPort"
1939 if [ "$Le_TLSPort" ]; then
1940 if [ "$DEBUG" ] && [ "$DEBUG" -gt "3" ]; then
1941 _get "https
://localhost
:$Le_TLSPort" "" 1
1942 _get "https
://localhost
:$Le_TLSPort" "" 1
1944 _get "https
://localhost
:$Le_TLSPort" "" 1 >/dev/null 2>&1
1945 _get "https
://localhost
:$Le_TLSPort" "" 1 >/dev/null 2>&1
1953 if [ "$__INTERACTIVE" ]; then
1954 _sleep_c="$_sleep_sec"
1955 while [ "$_sleep_c" -ge "0" ]; do
1958 _sleep_c="$
(_math
"$_sleep_c" - 1)"
1967 # _starttlsserver san_a san_b port content _ncaddr
1969 _info "Starting tls server.
"
1976 _debug san_a "$san_a"
1977 _debug san_b "$san_b"
1981 if ! _createkey "2048" "$TLS_KEY"; then
1982 _err "Create tls validation key error.
"
1988 if [ "$san_b" ]; then
1991 if ! _createcsr "tls.acme.sh
" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF"; then
1992 _err "Create tls validation csr error.
"
1997 if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then
1998 _err "Create tls validation cert error.
"
2002 __S_OPENSSL="$ACME_OPENSSL_BIN s_server
-cert $TLS_CERT -key $TLS_KEY "
2003 if [ "$opaddr" ]; then
2004 __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port"
2006 __S_OPENSSL="$__S_OPENSSL -accept $port"
2009 _debug Le_Listen_V4 "$Le_Listen_V4"
2010 _debug Le_Listen_V6 "$Le_Listen_V6"
2011 if [ "$Le_Listen_V4" ]; then
2012 __S_OPENSSL="$__S_OPENSSL -4"
2013 elif [ "$Le_Listen_V6" ]; then
2014 __S_OPENSSL="$__S_OPENSSL -6"
2017 _debug "$__S_OPENSSL"
2018 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
2019 (printf "%s
\r\n\r\n%s
" "HTTP
/1.1 200 OK
" "$content" | $__S_OPENSSL -tlsextdebug) &
2021 (printf "%s
\r\n\r\n%s
" "HTTP
/1.1 200 OK
" "$content" | $__S_OPENSSL >/dev/null 2>&1) &
2026 _debug serverproc "$serverproc"
2032 if ! readlink -f "$_rf" 2>/dev/null; then
2033 if _startswith "$_rf" "/"; then
2037 echo "$
(pwd)/$_rf" | _conapath
2046 if [ -z "$_SCRIPT_HOME" ]; then
2047 if _exists readlink
&& _exists
dirname; then
2048 _debug
"Lets find script dir."
2049 _debug
"_SCRIPT_" "$_SCRIPT_"
2050 _script
="$(_readlink "$_SCRIPT_")"
2051 _debug
"_script" "$_script"
2052 _script_home
="$(dirname "$_script")"
2053 _debug
"_script_home" "$_script_home"
2054 if [ -d "$_script_home" ]; then
2055 _SCRIPT_HOME
="$_script_home"
2057 _err
"It seems the script home is not correct:$_script_home"
2062 # if [ -z "$LE_WORKING_DIR" ]; then
2063 # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then
2064 # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME"
2065 # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
2067 # LE_WORKING_DIR="$_SCRIPT_HOME"
2071 if [ -z "$LE_WORKING_DIR" ]; then
2072 _debug
"Using default home:$DEFAULT_INSTALL_HOME"
2073 LE_WORKING_DIR
="$DEFAULT_INSTALL_HOME"
2075 export LE_WORKING_DIR
2077 if [ -z "$LE_CONFIG_HOME" ]; then
2078 LE_CONFIG_HOME
="$LE_WORKING_DIR"
2080 _debug
"Using config home:$LE_CONFIG_HOME"
2081 export LE_CONFIG_HOME
2083 _DEFAULT_ACCOUNT_CONF_PATH
="$LE_CONFIG_HOME/account.conf"
2085 if [ -z "$ACCOUNT_CONF_PATH" ]; then
2086 if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
2087 .
"$_DEFAULT_ACCOUNT_CONF_PATH"
2091 if [ -z "$ACCOUNT_CONF_PATH" ]; then
2092 ACCOUNT_CONF_PATH
="$_DEFAULT_ACCOUNT_CONF_PATH"
2095 DEFAULT_LOG_FILE
="$LE_CONFIG_HOME/$PROJECT_NAME.log"
2097 DEFAULT_CA_HOME
="$LE_CONFIG_HOME/ca"
2099 if [ -z "$LE_TEMP_DIR" ]; then
2100 LE_TEMP_DIR
="$LE_CONFIG_HOME/tmp"
2104 #[domain] [keylength]
2109 if [ -f "$ACCOUNT_CONF_PATH" ]; then
2110 .
"$ACCOUNT_CONF_PATH"
2113 if [ "$IN_CRON" ]; then
2114 if [ ! "$_USER_PATH_EXPORTED" ]; then
2115 _USER_PATH_EXPORTED
=1
2116 export PATH
="$USER_PATH:$PATH"
2120 if [ -z "$CA_HOME" ]; then
2121 CA_HOME
="$DEFAULT_CA_HOME"
2124 if [ -z "$API" ]; then
2125 if [ -z "$STAGE" ]; then
2129 _info
"Using stage api:$API"
2133 _API_HOST
="$(echo "$API" | cut -d : -f 2 | tr -d '/')"
2134 CA_DIR
="$CA_HOME/$_API_HOST"
2136 _DEFAULT_CA_CONF
="$CA_DIR/ca.conf"
2138 if [ -z "$CA_CONF" ]; then
2139 CA_CONF
="$_DEFAULT_CA_CONF"
2141 _debug3 CA_CONF
"$CA_CONF"
2143 if [ -f "$CA_CONF" ]; then
2147 if [ -z "$ACME_DIR" ]; then
2148 ACME_DIR
="/home/.acme"
2151 if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
2152 APACHE_CONF_BACKUP_DIR
="$LE_CONFIG_HOME"
2155 if [ -z "$USER_AGENT" ]; then
2156 USER_AGENT
="$DEFAULT_USER_AGENT"
2159 if [ -z "$HTTP_HEADER" ]; then
2160 HTTP_HEADER
="$LE_CONFIG_HOME/http.header"
2163 _OLD_ACCOUNT_KEY
="$LE_WORKING_DIR/account.key"
2164 _OLD_ACCOUNT_JSON
="$LE_WORKING_DIR/account.json"
2166 _DEFAULT_ACCOUNT_KEY_PATH
="$CA_DIR/account.key"
2167 _DEFAULT_ACCOUNT_JSON_PATH
="$CA_DIR/account.json"
2168 if [ -z "$ACCOUNT_KEY_PATH" ]; then
2169 ACCOUNT_KEY_PATH
="$_DEFAULT_ACCOUNT_KEY_PATH"
2172 if [ -z "$ACCOUNT_JSON_PATH" ]; then
2173 ACCOUNT_JSON_PATH
="$_DEFAULT_ACCOUNT_JSON_PATH"
2176 _DEFAULT_CERT_HOME
="$LE_CONFIG_HOME"
2177 if [ -z "$CERT_HOME" ]; then
2178 CERT_HOME
="$_DEFAULT_CERT_HOME"
2181 if [ -z "$ACME_OPENSSL_BIN" ] ||
[ ! -f "$ACME_OPENSSL_BIN" ] ||
[ ! -x "$ACME_OPENSSL_BIN" ]; then
2182 ACME_OPENSSL_BIN
="$DEFAULT_OPENSSL_BIN"
2185 if [ -z "$1" ]; then
2192 if [ -z "$DOMAIN_PATH" ]; then
2193 domainhome
="$CERT_HOME/$domain"
2194 domainhomeecc
="$CERT_HOME/$domain$ECC_SUFFIX"
2196 DOMAIN_PATH
="$domainhome"
2198 if _isEccKey
"$_ilength"; then
2199 DOMAIN_PATH
="$domainhomeecc"
2201 if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
2202 _info
"The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert."
2205 _debug DOMAIN_PATH
"$DOMAIN_PATH"
2208 if [ -z "$DOMAIN_BACKUP_PATH" ]; then
2209 DOMAIN_BACKUP_PATH
="$DOMAIN_PATH/backup"
2212 if [ -z "$DOMAIN_CONF" ]; then
2213 DOMAIN_CONF
="$DOMAIN_PATH/$domain.conf"
2216 if [ -z "$DOMAIN_SSL_CONF" ]; then
2217 DOMAIN_SSL_CONF
="$DOMAIN_PATH/$domain.csr.conf"
2220 if [ -z "$CSR_PATH" ]; then
2221 CSR_PATH
="$DOMAIN_PATH/$domain.csr"
2223 if [ -z "$CERT_KEY_PATH" ]; then
2224 CERT_KEY_PATH
="$DOMAIN_PATH/$domain.key"
2226 if [ -z "$CERT_PATH" ]; then
2227 CERT_PATH
="$DOMAIN_PATH/$domain.cer"
2229 if [ -z "$CA_CERT_PATH" ]; then
2230 CA_CERT_PATH
="$DOMAIN_PATH/ca.cer"
2232 if [ -z "$CERT_FULLCHAIN_PATH" ]; then
2233 CERT_FULLCHAIN_PATH
="$DOMAIN_PATH/fullchain.cer"
2235 if [ -z "$CERT_PFX_PATH" ]; then
2236 CERT_PFX_PATH
="$DOMAIN_PATH/$domain.pfx"
2238 if [ -z "$CERT_PKCS8_PATH" ]; then
2239 CERT_PKCS8_PATH
="$DOMAIN_PATH/$domain.pkcs8"
2242 if [ -z "$TLS_CONF" ]; then
2243 TLS_CONF
="$DOMAIN_PATH/tls.valdation.conf"
2245 if [ -z "$TLS_CERT" ]; then
2246 TLS_CERT
="$DOMAIN_PATH/tls.valdation.cert"
2248 if [ -z "$TLS_KEY" ]; then
2249 TLS_KEY
="$DOMAIN_PATH/tls.valdation.key"
2251 if [ -z "$TLS_CSR" ]; then
2252 TLS_CSR
="$DOMAIN_PATH/tls.valdation.csr"
2258 if [ -z "$_EXEC_TEMP_ERR" ]; then
2259 _EXEC_TEMP_ERR
="$(_mktemp)"
2262 if [ "$_EXEC_TEMP_ERR" ]; then
2263 eval "$@ 2>>$_EXEC_TEMP_ERR"
2270 [ "$_EXEC_TEMP_ERR" ] && _err
"$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR"
2274 _APACHECTL
="apachectl"
2275 if ! _exists apachectl
; then
2276 if _exists apache2ctl
; then
2277 _APACHECTL
="apache2ctl"
2279 _err
"'apachectl not found. It seems that apache is not installed, or you are not root user.'"
2280 _err
"Please use webroot mode to try again."
2285 if ! _exec
$_APACHECTL -V >/dev
/null
; then
2290 if [ "$APACHE_HTTPD_CONF" ]; then
2291 _saveaccountconf APACHE_HTTPD_CONF
"$APACHE_HTTPD_CONF"
2292 httpdconf
="$APACHE_HTTPD_CONF"
2293 httpdconfname
="$(basename "$httpdconfname")"
2295 httpdconfname
="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
2296 _debug httpdconfname "$httpdconfname"
2298 if [ -z "$httpdconfname" ]; then
2299 _err "Can not read apache config file."
2303 if _startswith "$httpdconfname" '/'; then
2304 httpdconf="$httpdconfname"
2305 httpdconfname="$(basename "$httpdconfname")"
2307 httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
2308 _debug httpdroot
"$httpdroot"
2309 httpdconf
="$httpdroot/$httpdconfname"
2310 httpdconfname
="$(basename "$httpdconfname")"
2313 _debug httpdconf
"$httpdconf"
2314 _debug httpdconfname
"$httpdconfname"
2315 if [ ! -f "$httpdconf" ]; then
2316 _err
"Apache Config file not found" "$httpdconf"
2323 if [ -z "$usingApache" ]; then
2327 if ! _apachePath
; then
2331 if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
2332 _debug
"No config file to restore."
2336 cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
2337 _debug
"Restored: $httpdconf."
2338 if ! _exec
$_APACHECTL -t; then
2340 _err
"Sorry, restore apache config error, please contact me."
2343 _debug
"Restored successfully."
2344 rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
2350 if ! _apachePath
; then
2354 #test the conf first
2355 _info
"Checking if there is an error in the apache config file before starting."
2357 if ! _exec
"$_APACHECTL" -t >/dev
/null
; then
2359 _err
"The apache config file has error, please fix it first, then try again."
2360 _err
"Don't worry, there is nothing changed to your system."
2367 _debug
"Backup apache config file" "$httpdconf"
2368 if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
2369 _err
"Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
2370 _err
"This might be a bug of $PROJECT_NAME , pleae report issue: $PROJECT"
2373 _info
"JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
2374 _info
"In case there is an error that can not be restored automatically, you may try restore it yourself."
2375 _info
"The backup file will be deleted on success, just forget it."
2379 apacheVer
="$($_APACHECTL -V | grep "Server version
:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
2380 _debug
"apacheVer" "$apacheVer"
2381 apacheMajer
="$(echo "$apacheVer" | cut -d . -f 1)"
2382 apacheMinor
="$(echo "$apacheVer" | cut -d . -f 2)"
2384 if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ]; then
2386 Alias /.well-known/acme-challenge $ACME_DIR
2388 <Directory $ACME_DIR >
2394 Alias /.well-known/acme-challenge $ACME_DIR
2396 <Directory $ACME_DIR >
2403 _msg
="$($_APACHECTL -t 2>&1)"
2404 if [ "$?" != "0" ]; then
2405 _err
"Sorry, apache config error"
2406 if _restoreApache
; then
2407 _err
"The apache config file is restored."
2409 _err
"Sorry, The apache config file can not be restored, please report bug."
2414 if [ ! -d "$ACME_DIR" ]; then
2415 mkdir
-p "$ACME_DIR"
2416 chmod 755 "$ACME_DIR"
2419 if ! _exec
"$_APACHECTL" graceful
; then
2421 _err
"$_APACHECTL graceful error, please contact me."
2429 #find the real nginx conf file
2432 #returns the real nginx conf file
2437 if ! _exists
"nginx"; then
2438 _err
"nginx command is not found."
2441 FOUND_REAL_NGINX_CONF
=""
2442 FOUND_REAL_NGINX_CONF_LN
=""
2443 BACKUP_NGINX_CONF
=""
2444 _debug _croot
"$_croot"
2445 _start_f
="$(echo "$_croot" | cut -d : -f 2)"
2446 _debug _start_f
"$_start_f"
2447 if [ -z "$_start_f" ]; then
2448 _debug
"find start conf from nginx command"
2449 if [ -z "$NGINX_CONF" ]; then
2450 NGINX_CONF
="$(nginx -V 2>&1 | _egrep_o "--conf-path=[^
]* " | tr -d " ")"
2451 _debug NGINX_CONF
"$NGINX_CONF"
2452 NGINX_CONF
="$(echo "$NGINX_CONF" | cut -d = -f 2)"
2453 _debug NGINX_CONF
"$NGINX_CONF"
2454 if [ ! -f "$NGINX_CONF" ]; then
2455 _err
"'$NGINX_CONF' doesn't exist."
2459 _debug
"Found nginx conf file:$NGINX_CONF"
2461 _start_f
="$NGINX_CONF"
2463 _debug
"Start detect nginx conf for $_d from:$_start_f"
2464 if ! _checkConf
"$_d" "$_start_f"; then
2465 "Can not find conf file for domain $d"
2468 _info
"Found conf file: $FOUND_REAL_NGINX_CONF"
2470 _ln
=$FOUND_REAL_NGINX_CONF_LN
2473 _lnn
=$
(_math
$_ln + 1)
2475 _start_tag
="$(sed -n "$_lnn,${_lnn}p
" "$FOUND_REAL_NGINX_CONF")"
2476 _debug
"_start_tag" "$_start_tag"
2477 if [ "$_start_tag" = "$NGINX_START" ]; then
2478 _info
"The domain $_d is already configured, skip"
2479 FOUND_REAL_NGINX_CONF
=""
2483 mkdir
-p "$DOMAIN_BACKUP_PATH"
2484 _backup_conf
="$DOMAIN_BACKUP_PATH/$_d.nginx.conf"
2485 _debug _backup_conf
"$_backup_conf"
2486 BACKUP_NGINX_CONF
="$_backup_conf"
2487 _info
"Backup $FOUND_REAL_NGINX_CONF to $_backup_conf"
2488 if ! cp "$FOUND_REAL_NGINX_CONF" "$_backup_conf"; then
2489 _err
"backup error."
2490 FOUND_REAL_NGINX_CONF
=""
2494 _info
"Check the nginx conf before setting up."
2495 if ! _exec
"nginx -t" >/dev
/null
; then
2500 _info
"OK, Set up nginx config file"
2502 if ! sed -n "1,${_ln}p" "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"; then
2503 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2504 _err
"write nginx conf error, but don't worry, the file is restored to the original version."
2509 location ~ \"^/\.well-known/acme-challenge/([-_a-zA-Z0-9]+)\$\" {
2510 default_type text/plain;
2511 return 200 \"\$1.$_thumbpt\";
2514 " >>"$FOUND_REAL_NGINX_CONF"
2516 if ! sed -n "${_lnn},99999p" "$_backup_conf" >>"$FOUND_REAL_NGINX_CONF"; then
2517 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2518 _err
"write nginx conf error, but don't worry, the file is restored."
2522 _info
"nginx conf is done, let's check it again."
2523 if ! _exec
"nginx -t" >/dev
/null
; then
2525 _err
"It seems that nginx conf was broken, let's restore."
2526 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2530 _info
"Reload nginx"
2531 if ! _exec
"nginx -s reload" >/dev
/null
; then
2533 _err
"It seems that nginx reload error, let's restore."
2534 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2545 _debug
"Start _checkConf from:$_c_file"
2546 if [ ! -f "$2" ] && ! echo "$2" |
grep '*$' >/dev
/null
&& echo "$2" |
grep '*' >/dev
/null
; then
2549 if _checkConf
"$1" "$_w_f"; then
2555 elif [ -f "$2" ]; then
2557 if _isRealNginxConf
"$1" "$2"; then
2558 _debug
"$2 is found."
2559 FOUND_REAL_NGINX_CONF
="$2"
2562 if grep "^ *include *.*;" "$2" >/dev
/null
; then
2563 _debug
"Try include files"
2564 for included
in $
(grep "^ *include *.*;" "$2" |
sed "s/include //" |
tr -d " ;"); do
2565 _debug
"check included $included"
2566 if _checkConf
"$1" "$included"; then
2573 _debug
"$2 not found."
2580 _isRealNginxConf
() {
2581 _debug
"_isRealNginxConf $1 $2"
2582 if [ -f "$2" ]; then
2583 for _fln
in $
(grep -n "^ *server_name.* $1" "$2" | cut
-d : -f 1); do
2585 if [ "$_fln" ]; then
2586 _start
=$
(cat "$2" | _head_n
"$_fln" |
grep -n "^ *server *{" | _tail_n
1)
2587 _debug
"_start" "$_start"
2588 _start_n
=$
(echo "$_start" | cut
-d : -f 1)
2589 _start_nn
=$
(_math
$_start_n + 1)
2590 _debug
"_start_n" "$_start_n"
2591 _debug
"_start_nn" "$_start_nn"
2593 _left
="$(sed -n "${_start_nn},99999p
" "$2")"
2594 _debug2 _left
"$_left"
2595 if echo "$_left" |
grep -n "^ *server *{" >/dev
/null
; then
2596 _end
=$
(echo "$_left" |
grep -n "^ *server *{" | _head_n
1)
2597 _debug
"_end" "$_end"
2598 _end_n
=$
(echo "$_end" | cut
-d : -f 1)
2599 _debug
"_end_n" "$_end_n"
2600 _seg_n
=$
(echo "$_left" |
sed -n "1,${_end_n}p")
2605 _debug
"_seg_n" "$_seg_n"
2607 if [ "$(echo "$_seg_n" | _egrep_o "^
*ssl
*on
*;")" ]; then
2608 _debug
"ssl on, skip"
2611 FOUND_REAL_NGINX_CONF_LN
=$_fln
2619 #restore all the nginx conf
2621 if [ -z "$NGINX_RESTORE_VLIST" ]; then
2622 _debug
"No need to restore nginx, skip."
2625 _debug
"_restoreNginx"
2626 _debug
"NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST"
2628 for ng_entry
in $
(echo "$NGINX_RESTORE_VLIST" |
tr "$dvsep" ' '); do
2629 _debug
"ng_entry" "$ng_entry"
2630 _nd
=$
(echo "$ng_entry" | cut
-d "$sep" -f 1)
2631 _ngconf
=$
(echo "$ng_entry" | cut
-d "$sep" -f 2)
2632 _ngbackupconf
=$
(echo "$ng_entry" | cut
-d "$sep" -f 3)
2633 _info
"Restoring from $_ngbackupconf to $_ngconf"
2634 cat "$_ngbackupconf" >"$_ngconf"
2637 _info
"Reload nginx"
2638 if ! _exec
"nginx -s reload" >/dev
/null
; then
2640 _err
"It seems that nginx reload error, please report bug."
2647 _stopserver
"$serverproc"
2652 if [ -z "$DEBUG" ]; then
2661 _debug
"_clearupdns"
2662 if [ "$dnsadded" != 1 ] ||
[ -z "$vlist" ]; then
2663 _debug
"Dns not added, skip."
2667 ventries
=$
(echo "$vlist" |
tr ',' ' ')
2668 for ventry
in $ventries; do
2669 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
2670 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
2671 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
2672 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
2673 txt
="$(printf "%s
" "$keyauthorization" | _digest "sha256
" | _url_replace)"
2675 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
2676 _debug
"$d is already verified, skip $vtype."
2680 if [ "$vtype" != "$VTYPE_DNS" ]; then
2681 _info
"Skip $d for $vtype"
2685 d_api
="$(_findHook "$d" dnsapi "$_currentRoot")"
2686 _debug d_api
"$d_api"
2688 if [ -z "$d_api" ]; then
2689 _info
"Not Found domain api file: $d_api"
2694 if ! .
"$d_api"; then
2695 _err
"Load file $d_api error. Please check your api file and try again."
2699 rmcommand
="${_currentRoot}_rm"
2700 if ! _exists
"$rmcommand"; then
2701 _err
"It seems that your api file doesn't define $rmcommand"
2705 txtdomain
="_acme-challenge.$d"
2707 if ! $rmcommand "$txtdomain" "$txt"; then
2708 _err
"Error removing txt for domain:$txtdomain"
2716 # webroot removelevel tokenfile
2717 _clearupwebbroot
() {
2719 if [ -z "$__webroot" ]; then
2720 _debug
"no webroot specified, skip"
2725 if [ "$2" = '1' ]; then
2726 _rmpath
="$__webroot/.well-known"
2727 elif [ "$2" = '2' ]; then
2728 _rmpath
="$__webroot/.well-known/acme-challenge"
2729 elif [ "$2" = '3' ]; then
2730 _rmpath
="$__webroot/.well-known/acme-challenge/$3"
2732 _debug
"Skip for removelevel:$2"
2735 if [ "$_rmpath" ]; then
2736 if [ "$DEBUG" ]; then
2737 _debug
"Debugging, skip removing: $_rmpath"
2747 _on_before_issue
() {
2749 _chk_main_domain
="$2"
2750 _chk_alt_domains
="$3"
2752 _chk_local_addr
="$5"
2753 _debug _on_before_issue
2755 if [ "$_chk_pre_hook" ]; then
2756 _info
"Run pre hook:'$_chk_pre_hook'"
2758 cd "$DOMAIN_PATH" && eval "$_chk_pre_hook"
2760 _err
"Error when run pre hook."
2765 if _hasfield
"$_chk_web_roots" "$NO_VALUE"; then
2766 if ! _exists
"nc"; then
2767 _err
"Please install netcat(nc) tools first."
2772 _debug Le_LocalAddress
"$_chk_local_addr"
2774 alldomains
=$
(echo "$_chk_main_domain,$_chk_alt_domains" |
tr ',' ' ')
2778 for d
in $alldomains; do
2779 _debug
"Check for domain" "$d"
2780 _currentRoot
="$(_getfield "$_chk_web_roots" $_index)"
2781 _debug
"_currentRoot" "$_currentRoot"
2782 _index
=$
(_math
$_index + 1)
2784 if [ "$_currentRoot" = "$NO_VALUE" ]; then
2785 _info
"Standalone mode."
2786 if [ -z "$Le_HTTPPort" ]; then
2789 _savedomainconf
"Le_HTTPPort" "$Le_HTTPPort"
2791 _checkport
="$Le_HTTPPort"
2792 elif [ "$_currentRoot" = "$W_TLS" ]; then
2793 _info
"Standalone tls mode."
2794 if [ -z "$Le_TLSPort" ]; then
2797 _savedomainconf
"Le_TLSPort" "$Le_TLSPort"
2799 _checkport
="$Le_TLSPort"
2802 if [ "$_checkport" ]; then
2803 _debug _checkport
"$_checkport"
2804 _checkaddr
="$(_getfield "$_chk_local_addr" $_addrIndex)"
2805 _debug _checkaddr
"$_checkaddr"
2807 _addrIndex
="$(_math $_addrIndex + 1)"
2809 _netprc
="$(_ss "$_checkport" | grep "$_checkport")"
2810 netprc
="$(echo "$_netprc" | grep "$_checkaddr")"
2811 if [ -z "$netprc" ]; then
2812 netprc
="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
2814 if [ "$netprc" ]; then
2816 _err
"tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
2817 _err
"Please stop it first"
2823 if _hasfield
"$_chk_web_roots" "apache"; then
2824 if ! _setApache
; then
2825 _err
"set up apache error. Report error to me."
2837 _debug _on_issue_err
2838 if [ "$LOG_FILE" ]; then
2839 _err
"Please check log file for more details: $LOG_FILE"
2841 _err
"Please add '--debug' or '--log' to check more details."
2842 _err
"See: $_DEBUG_WIKI"
2846 if [ "$_chk_post_hook" ]; then
2847 _info
"Run post hook:'$_chk_post_hook'"
2849 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
2851 _err
"Error when run post hook."
2856 #trigger the validation to flush the pending authz
2857 if [ "$_chk_vlist" ]; then
2859 _debug2
"_chk_vlist" "$_chk_vlist"
2860 _debug2
"start to deactivate authz"
2861 ventries
=$
(echo "$_chk_vlist" |
tr "$dvsep" ' ')
2862 for ventry
in $ventries; do
2863 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
2864 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
2865 uri
=$
(echo "$ventry" | cut
-d "$sep" -f 3)
2866 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
2867 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
2868 __trigger_validaton
"$uri" "$keyauthorization"
2873 if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
2874 _debug
"$(_dlg_versions)"
2879 _on_issue_success
() {
2881 _chk_renew_hook
="$2"
2882 _debug _on_issue_success
2884 if [ "$_chk_post_hook" ]; then
2885 _info
"Run post hook:'$_chk_post_hook'"
2887 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
2889 _err
"Error when run post hook."
2895 if [ "$IS_RENEW" ] && [ "$_chk_renew_hook" ]; then
2896 _info
"Run renew hook:'$_chk_renew_hook'"
2898 cd "$DOMAIN_PATH" && eval "$_chk_renew_hook"
2900 _err
"Error when run renew hook."
2915 _regAccount
"$_reg_length"
2918 __calcAccountKeyHash
() {
2919 [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256
<"$ACCOUNT_KEY_PATH"
2922 __calc_account_thumbprint
() {
2923 printf "%s" "$jwk" |
tr -d ' ' | _digest
"sha256" | _url_replace
2931 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
2933 _info
"mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
2934 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
2937 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
2939 _info
"mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
2940 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
2943 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
2944 if ! _create_account_key
"$_reg_length"; then
2945 _err
"Create account key error."
2950 if ! _calcjwk
"$ACCOUNT_KEY_PATH"; then
2957 _debug AGREEMENT
"$AGREEMENT"
2959 regjson
='{"resource": "'$_reg_res'", "agreement": "'$AGREEMENT'"}'
2961 if [ "$ACCOUNT_EMAIL" ]; then
2962 regjson
='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
2965 if [ -z "$_updateTos" ]; then
2966 _info
"Registering account"
2968 if ! _send_signed_request
"$API/acme/new-reg" "$regjson"; then
2969 _err
"Register account Error: $response"
2973 if [ "$code" = "" ] ||
[ "$code" = '201' ]; then
2974 echo "$response" >"$ACCOUNT_JSON_PATH"
2976 elif [ "$code" = '409' ]; then
2977 _info
"Already registered"
2979 _err
"Register account Error: $response"
2983 _accUri
="$(echo "$responseHeaders" | grep "^Location
:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
2984 _debug
"_accUri" "$_accUri"
2986 _tos
="$(echo "$responseHeaders" | grep "^Link
:.
*rel
=\"terms-of-service
\"" | _head_n 1 | _egrep_o "<.
*>" | tr -d '<>')"
2987 _debug
"_tos" "$_tos"
2988 if [ -z "$_tos" ]; then
2989 _debug
"Use default tos: $DEFAULT_AGREEMENT"
2990 _tos
="$DEFAULT_AGREEMENT"
2992 if [ "$_tos" != "$AGREEMENT" ]; then
3000 _debug
"Update tos: $_tos"
3001 if ! _send_signed_request
"$_accUri" "$regjson"; then
3002 _err
"Update tos error."
3005 if [ "$code" = '202' ]; then
3006 _info
"Update success."
3008 CA_KEY_HASH
="$(__calcAccountKeyHash)"
3009 _debug
"Calc CA_KEY_HASH" "$CA_KEY_HASH"
3010 _savecaconf CA_KEY_HASH
"$CA_KEY_HASH"
3012 _err
"Update account error."
3016 ACCOUNT_THUMBPRINT
="$(__calc_account_thumbprint)"
3017 _info
"ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT"
3023 # domain folder file
3029 if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
3030 d_api
="$_SCRIPT_HOME/$_hookcat/$_hookname"
3031 elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
3032 d_api
="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
3033 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
3034 d_api
="$LE_WORKING_DIR/$_hookdomain/$_hookname"
3035 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
3036 d_api
="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
3037 elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
3038 d_api
="$LE_WORKING_DIR/$_hookname"
3039 elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
3040 d_api
="$LE_WORKING_DIR/$_hookname.sh"
3041 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
3042 d_api
="$LE_WORKING_DIR/$_hookcat/$_hookname"
3043 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
3044 d_api
="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
3047 printf "%s" "$d_api"
3051 __get_domain_new_authz
() {
3053 _info
"Getting new-authz for domain" "$_gdnd"
3055 _Max_new_authz_retry_times
=5
3057 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
3058 _debug
"Try new-authz for the $_authz_i time."
3059 if ! _send_signed_request
"$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
3060 _err
"Can not get domain new authz."
3063 if _contains
"$response" "No registration exists matching provided key"; then
3064 _err
"It seems there is an error, but it's recovered now, please try again."
3065 _err
"If you see this message for a second time, please report bug: $(__green "$PROJECT")"
3066 _clearcaconf
"CA_KEY_HASH"
3069 if ! _contains
"$response" "An error occurred while processing your request"; then
3070 _info
"The new-authz request is ok."
3073 _authz_i
="$(_math "$_authz_i" + 1)"
3074 _info
"The server is busy, Sleep $_authz_i to retry."
3078 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
3079 _err
"new-authz retry reach the max $_Max_new_authz_retry_times times."
3082 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
3083 _err
"new-authz error: $response"
3089 #uri keyAuthorization
3090 __trigger_validaton
() {
3091 _debug2
"tigger domain validation."
3093 _debug2 _t_url
"$_t_url"
3095 _debug2 _t_key_authz
"$_t_key_authz"
3096 _send_signed_request
"$_t_url" "{\"resource\": \"challenge\", \"keyAuthorization\": \"$_t_key_authz\"}"
3099 #webroot, domain domainlist keylength
3101 if [ -z "$2" ]; then
3102 _usage
"Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
3108 if _contains
"$_main_domain" ","; then
3109 _main_domain
=$
(echo "$2,$3" | cut
-d , -f 1)
3110 _alt_domains
=$
(echo "$2,$3" | cut
-d , -f 2- |
sed "s/,${NO_VALUE}$//")
3117 _real_fullchain
="$9"
3123 #remove these later.
3124 if [ "$_web_roots" = "dns-cf" ]; then
3127 if [ "$_web_roots" = "dns-dp" ]; then
3130 if [ "$_web_roots" = "dns-cx" ]; then
3133 _debug
"Using api: $API"
3135 if [ ! "$IS_RENEW" ]; then
3136 _initpath
"$_main_domain" "$_key_length"
3137 mkdir
-p "$DOMAIN_PATH"
3140 if [ -f "$DOMAIN_CONF" ]; then
3141 Le_NextRenewTime
=$
(_readdomainconf Le_NextRenewTime
)
3142 _debug Le_NextRenewTime
"$Le_NextRenewTime"
3143 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
3144 _saved_domain
=$
(_readdomainconf Le_Domain
)
3145 _debug _saved_domain
"$_saved_domain"
3146 _saved_alt
=$
(_readdomainconf Le_Alt
)
3147 _debug _saved_alt
"$_saved_alt"
3148 if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then
3149 _info
"Domains not changed."
3150 _info
"Skip, Next renewal time is: $(__green "$
(_readdomainconf Le_NextRenewTimeStr
)")"
3151 _info
"Add '$(__red '--force')' to force to renew."
3154 _info
"Domains have changed."
3159 _savedomainconf
"Le_Domain" "$_main_domain"
3160 _savedomainconf
"Le_Alt" "$_alt_domains"
3161 _savedomainconf
"Le_Webroot" "$_web_roots"
3163 _savedomainconf
"Le_PreHook" "$_pre_hook"
3164 _savedomainconf
"Le_PostHook" "$_post_hook"
3165 _savedomainconf
"Le_RenewHook" "$_renew_hook"
3167 if [ "$_local_addr" ]; then
3168 _savedomainconf
"Le_LocalAddress" "$_local_addr"
3170 _cleardomainconf
"Le_LocalAddress"
3174 _savedomainconf
"Le_API" "$Le_API"
3176 if [ "$_alt_domains" = "$NO_VALUE" ]; then
3180 if [ "$_key_length" = "$NO_VALUE" ]; then
3184 if ! _on_before_issue
"$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
3185 _err
"_on_before_issue."
3189 _saved_account_key_hash
="$(_readcaconf "CA_KEY_HASH
")"
3190 _debug2 _saved_account_key_hash
"$_saved_account_key_hash"
3192 if [ -z "$_saved_account_key_hash" ] ||
[ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
3193 if ! _regAccount
"$_accountkeylength"; then
3194 _on_issue_err
"$_post_hook"
3198 _debug
"_saved_account_key_hash is not changed, skip register account."
3201 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
3202 _info
"Signing from existing CSR."
3204 _key
=$
(_readdomainconf Le_Keylength
)
3205 _debug
"Read key length:$_key"
3206 if [ ! -f "$CERT_KEY_PATH" ] ||
[ "$_key_length" != "$_key" ]; then
3207 if ! createDomainKey
"$_main_domain" "$_key_length"; then
3208 _err
"Create domain key error."
3210 _on_issue_err
"$_post_hook"
3215 if ! _createcsr
"$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
3216 _err
"Create CSR error."
3218 _on_issue_err
"$_post_hook"
3223 _savedomainconf
"Le_Keylength" "$_key_length"
3227 _info
"Getting domain auth token for each domain"
3230 if [ -z "$vlist" ]; then
3231 alldomains
=$
(echo "$_main_domain,$_alt_domains" |
tr ',' ' ')
3234 for d
in $alldomains; do
3235 _info
"Getting webroot for domain" "$d"
3236 _w
="$(echo $_web_roots | cut -d , -f $_index)"
3241 _debug
"_currentRoot" "$_currentRoot"
3242 _index
=$
(_math
$_index + 1)
3245 if _startswith
"$_currentRoot" "dns"; then
3249 if [ "$_currentRoot" = "$W_TLS" ]; then
3253 if ! __get_domain_new_authz
"$d"; then
3255 _on_issue_err
"$_post_hook"
3259 if [ -z "$thumbprint" ]; then
3260 thumbprint
="$(__calc_account_thumbprint)"
3263 entry
="$(printf "%s
\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
3264 _debug entry
"$entry"
3265 if [ -z "$entry" ]; then
3266 _err
"Error, can not get domain token $d"
3268 _on_issue_err
"$_post_hook"
3271 token
="$(printf "%s
\n" "$entry" | _egrep_o '"token
":"[^
"]*' | cut -d : -f 2 | tr -d '"')"
3272 _debug token "$token"
3274 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
3277 keyauthorization
="$token.$thumbprint"
3278 _debug keyauthorization
"$keyauthorization"
3280 if printf "%s" "$response" |
grep '"status":"valid"' >/dev
/null
2>&1; then
3281 _debug
"$d is already verified, skip."
3282 keyauthorization
="$STATE_VERIFIED"
3283 _debug keyauthorization
"$keyauthorization"
3286 dvlist
="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
3287 _debug dvlist
"$dvlist"
3289 vlist
="$vlist$dvlist$dvsep"
3292 _debug vlist
"$vlist"
3295 ventries
=$
(echo "$vlist" |
tr "$dvsep" ' ')
3296 for ventry
in $ventries; do
3297 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
3298 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
3299 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
3300 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
3302 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
3303 _debug
"$d is already verified, skip $vtype."
3307 if [ "$vtype" = "$VTYPE_DNS" ]; then
3309 txtdomain
="_acme-challenge.$d"
3310 _debug txtdomain
"$txtdomain"
3311 txt
="$(printf "%s
" "$keyauthorization" | _digest "sha256
" | _url_replace)"
3314 d_api
="$(_findHook "$d" dnsapi "$_currentRoot")"
3316 _debug d_api
"$d_api"
3318 if [ "$d_api" ]; then
3319 _info
"Found domain api file: $d_api"
3321 _err
"Add the following TXT record:"
3322 _err
"Domain: '$(__green "$txtdomain")'"
3323 _err
"TXT value: '$(__green "$txt")'"
3324 _err
"Please be aware that you prepend _acme-challenge. before your domain"
3325 _err
"so the resulting subdomain will be: $txtdomain"
3330 if ! .
"$d_api"; then
3331 _err
"Load file $d_api error. Please check your api file and try again."
3335 addcommand
="${_currentRoot}_add"
3336 if ! _exists
"$addcommand"; then
3337 _err
"It seems that your api file is not correct, it must have a function named: $addcommand"
3341 if ! $addcommand "$txtdomain" "$txt"; then
3342 _err
"Error add txt for domain:$txtdomain"
3347 if [ "$?" != "0" ]; then
3349 _on_issue_err
"$_post_hook"
3356 if [ "$dnsadded" = '0' ]; then
3357 _savedomainconf
"Le_Vlist" "$vlist"
3358 _debug
"Dns record not added yet, so, save to $DOMAIN_CONF and exit."
3359 _err
"Please add the TXT records to the domains, and retry again."
3361 _on_issue_err
"$_post_hook"
3367 if [ "$dnsadded" = '1' ]; then
3368 if [ -z "$Le_DNSSleep" ]; then
3369 Le_DNSSleep
="$DEFAULT_DNS_SLEEP"
3371 _savedomainconf
"Le_DNSSleep" "$Le_DNSSleep"
3374 _info
"Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
3375 _sleep
"$Le_DNSSleep"
3378 NGINX_RESTORE_VLIST
=""
3379 _debug
"ok, let's start to verify"
3382 ventries
=$
(echo "$vlist" |
tr "$dvsep" ' ')
3383 for ventry
in $ventries; do
3384 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
3385 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
3386 uri
=$
(echo "$ventry" | cut
-d "$sep" -f 3)
3387 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
3388 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
3390 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
3391 _info
"$d is already verified, skip $vtype."
3395 _info
"Verifying:$d"
3397 _debug
"keyauthorization" "$keyauthorization"
3400 token
="$(printf "%s
" "$keyauthorization" | cut -d '.' -f 1)"
3402 _debug
"_currentRoot" "$_currentRoot"
3404 if [ "$vtype" = "$VTYPE_HTTP" ]; then
3405 if [ "$_currentRoot" = "$NO_VALUE" ]; then
3406 _info
"Standalone mode server"
3407 _ncaddr
="$(_getfield "$_local_addr" "$_ncIndex")"
3408 _ncIndex
="$(_math $_ncIndex + 1)"
3409 _startserver
"$keyauthorization" "$_ncaddr" &
3410 if [ "$?" != "0" ]; then
3412 _on_issue_err
"$_post_hook" "$vlist"
3417 _debug serverproc
"$serverproc"
3418 elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
3419 _info
"Stateless mode for domain:$d"
3421 elif _startswith
"$_currentRoot" "$NGINX"; then
3422 _info
"Nginx mode for domain:$d"
3423 #set up nginx server
3424 FOUND_REAL_NGINX_CONF
=""
3425 BACKUP_NGINX_CONF
=""
3426 if ! _setNginx
"$d" "$_currentRoot" "$thumbprint"; then
3428 _on_issue_err
"$_post_hook" "$vlist"
3432 if [ "$FOUND_REAL_NGINX_CONF" ]; then
3433 _realConf
="$FOUND_REAL_NGINX_CONF"
3434 _backup
="$BACKUP_NGINX_CONF"
3435 _debug _realConf
"$_realConf"
3436 NGINX_RESTORE_VLIST
="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
3440 if [ "$_currentRoot" = "apache" ]; then
3441 wellknown_path
="$ACME_DIR"
3443 wellknown_path
="$_currentRoot/.well-known/acme-challenge"
3444 if [ ! -d "$_currentRoot/.well-known" ]; then
3446 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
3453 _debug wellknown_path
"$wellknown_path"
3455 _debug
"writing token:$token to $wellknown_path/$token"
3457 mkdir
-p "$wellknown_path"
3459 if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
3460 _err
"$d:Can not write token to file : $wellknown_path/$token"
3461 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3463 _on_issue_err
"$_post_hook" "$vlist"
3467 if [ ! "$usingApache" ]; then
3468 if webroot_owner
=$
(_stat
"$_currentRoot"); then
3469 _debug
"Changing owner/group of .well-known to $webroot_owner"
3470 chown
-R "$webroot_owner" "$_currentRoot/.well-known"
3472 _debug
"not chaning owner/group of webroot"
3478 elif [ "$vtype" = "$VTYPE_TLS" ]; then
3480 #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
3481 #_debug2 _hash_A "$_hash_A"
3482 #_x="$(echo $_hash_A | cut -c 1-32)"
3484 #_y="$(echo $_hash_A | cut -c 33-64)"
3486 #_SAN_A="$_x.$_y.token.acme.invalid"
3487 #_debug2 _SAN_A "$_SAN_A"
3490 _hash_B
="$(printf "%s
" "$keyauthorization" | _digest "sha256
" "hex
")"
3491 _debug2 _hash_B
"$_hash_B"
3492 _x
="$(echo "$_hash_B" | cut -c 1-32)"
3494 _y
="$(echo "$_hash_B" | cut -c 33-64)"
3497 #_SAN_B="$_x.$_y.ka.acme.invalid"
3499 _SAN_B
="$_x.$_y.acme.invalid"
3500 _debug2 _SAN_B
"$_SAN_B"
3502 _ncaddr
="$(_getfield "$_local_addr" "$_ncIndex")"
3503 _ncIndex
="$(_math "$_ncIndex" + 1)"
3504 if ! _starttlsserver
"$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
3505 _err
"Start tls server error."
3506 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3508 _on_issue_err
"$_post_hook" "$vlist"
3513 if ! __trigger_validaton
"$uri" "$keyauthorization"; then
3514 _err
"$d:Can not get challenge: $response"
3515 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3517 _on_issue_err
"$_post_hook" "$vlist"
3521 if [ ! -z "$code" ] && [ ! "$code" = '202' ]; then
3522 _err
"$d:Challenge error: $response"
3523 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3525 _on_issue_err
"$_post_hook" "$vlist"
3530 if [ -z "$MAX_RETRY_TIMES" ]; then
3535 waittimes
=$
(_math
"$waittimes" + 1)
3536 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
3538 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3540 _on_issue_err
"$_post_hook" "$vlist"
3544 _debug
"sleep 2 secs to verify"
3547 response
="$(_get "$uri")"
3548 if [ "$?" != "0" ]; then
3549 _err
"$d:Verify error:$response"
3550 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3552 _on_issue_err
"$_post_hook" "$vlist"
3555 _debug2 original
"$response"
3557 response
="$(echo "$response" | _normalizeJson)"
3558 _debug2 response
"$response"
3560 status
=$
(echo "$response" | _egrep_o
'"status":"[^"]*' | cut
-d : -f 2 |
tr -d '"')
3561 if [ "$status" = "valid" ]; then
3562 _info
"$(__green Success)"
3563 _stopserver
"$serverproc"
3565 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3569 if [ "$status" = "invalid" ]; then
3570 error
="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error
":\{[^\}]*')"
3571 _debug2 error
"$error"
3572 errordetail
="$(echo "$error" | _egrep_o '"detail
": *"[^
"]*' | cut -d '"' -f 4)"
3573 _debug2 errordetail "$errordetail"
3574 if [ "$errordetail" ]; then
3575 _err "$d:Verify error:$errordetail"
3577 _err "$d:Verify error:$error"
3579 if [ "$DEBUG" ]; then
3580 if [ "$vtype" = "$VTYPE_HTTP" ]; then
3581 _debug "Debug: get token url."
3582 _get "http://$d/.well-known/acme-challenge/$token" "" 1
3585 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3587 _on_issue_err "$_post_hook" "$vlist"
3591 if [ "$status" = "pending" ]; then
3594 _err "$d:Verify error:$response"
3595 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3597 _on_issue_err "$_post_hook" "$vlist"
3606 _info "Verify finished, start to sign."
3607 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)"
3609 if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"; then
3611 _on_issue_err "$_post_hook"
3616 Le_LinkCert="$(grep -i '^Location.
*$
' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
3617 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
3619 if [ "$Le_LinkCert" ]; then
3620 echo "$BEGIN_CERT" >"$CERT_PATH"
3622 #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
3623 # _debug "Get cert failed. Let's try last response.
"
3624 # printf -- "%s
" "$_rcert" | _dbase64 "multiline
" | _base64 "multiline
" >> "$CERT_PATH"
3627 if ! printf -- "%s
" "$_rcert" | _dbase64 "multiline
" | _base64 "multiline
" >>"$CERT_PATH"; then
3628 _debug "Try cert link.
"
3629 _get "$Le_LinkCert" | _base64 "multiline
" >>"$CERT_PATH"
3632 echo "$END_CERT" >>"$CERT_PATH"
3633 _info "$
(__green
"Cert success.")"
3636 _info "Your cert is
in $
(__green
" $CERT_PATH ")"
3638 if [ -f "$CERT_KEY_PATH" ]; then
3639 _info "Your cert key is
in $
(__green
" $CERT_KEY_PATH ")"
3642 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
3644 if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
3646 _saveaccountconf "USER_PATH
" "$USER_PATH"
3650 if [ -z "$Le_LinkCert" ]; then
3651 response="$
(echo "$response" | _dbase64
"multiline" | _normalizeJson
)"
3652 _err "Sign failed
: $
(echo "$response" | _egrep_o
'"detail":"[^"]*"')"
3653 _on_issue_err "$_post_hook"
3657 _cleardomainconf "Le_Vlist
"
3659 Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
3660 if ! _contains "$Le_LinkIssuer" ":"; then
3661 Le_LinkIssuer="$API$Le_LinkIssuer"
3664 _savedomainconf "Le_LinkIssuer
" "$Le_LinkIssuer"
3666 if [ "$Le_LinkIssuer" ]; then
3667 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
3668 _get "$Le_LinkIssuer" | _base64 "multiline
" >>"$CA_CERT_PATH"
3669 echo "$END_CERT" >>"$CA_CERT_PATH"
3670 _info "The intermediate CA cert is
in $
(__green
" $CA_CERT_PATH ")"
3671 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
3672 _info "And the full chain certs is there
: $
(__green
" $CERT_FULLCHAIN_PATH ")"
3675 Le_CertCreateTime=$(_time)
3676 _savedomainconf "Le_CertCreateTime
" "$Le_CertCreateTime"
3678 Le_CertCreateTimeStr=$(date -u)
3679 _savedomainconf "Le_CertCreateTimeStr
" "$Le_CertCreateTimeStr"
3681 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ]; then
3682 Le_RenewalDays="$MAX_RENEW"
3684 _savedomainconf "Le_RenewalDays
" "$Le_RenewalDays"
3687 if [ "$CA_BUNDLE" ]; then
3688 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
3690 _clearaccountconf "CA_BUNDLE
"
3693 if [ "$HTTPS_INSECURE" ]; then
3694 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
3696 _clearaccountconf "HTTPS_INSECURE
"
3699 if [ "$Le_Listen_V4" ]; then
3700 _savedomainconf "Le_Listen_V4
" "$Le_Listen_V4"
3701 _cleardomainconf Le_Listen_V6
3702 elif [ "$Le_Listen_V6" ]; then
3703 _savedomainconf "Le_Listen_V6
" "$Le_Listen_V6"
3704 _cleardomainconf Le_Listen_V4
3707 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
3709 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
3710 _savedomainconf "Le_NextRenewTimeStr
" "$Le_NextRenewTimeStr"
3712 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
3713 _savedomainconf "Le_NextRenewTime
" "$Le_NextRenewTime"
3715 _on_issue_success "$_post_hook" "$_renew_hook"
3717 if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
3718 _savedomainconf "Le_RealCertPath
" "$_real_cert"
3719 _savedomainconf "Le_RealCACertPath
" "$_real_ca"
3720 _savedomainconf "Le_RealKeyPath
" "$_real_key"
3721 _savedomainconf "Le_ReloadCmd
" "$_reload_cmd"
3722 _savedomainconf "Le_RealFullChainPath
" "$_real_fullchain"
3723 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
3731 if [ -z "$Le_Domain" ]; then
3732 _usage "Usage
: $PROJECT_ENTRY --renew -d domain.com
[--ecc]"
3738 _initpath "$Le_Domain" "$_isEcc"
3740 _info "$
(__green
"Renew: '$Le_Domain'")"
3741 if [ ! -f "$DOMAIN_CONF" ]; then
3742 _info "'$Le_Domain' is not a issued domain
, skip.
"
3746 if [ "$Le_RenewalDays" ]; then
3747 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
3752 if [ "$Le_API" ]; then
3756 ACCOUNT_JSON_PATH=""
3758 _debug3 "initpath again.
"
3759 _initpath "$Le_Domain" "$_isEcc"
3762 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$
(_time
)" -lt "$Le_NextRenewTime" ]; then
3763 _info "Skip
, Next renewal
time is
: $
(__green
"$Le_NextRenewTimeStr")"
3764 _info "Add
'$(__red '--force')' to force to renew.
"
3765 return "$RENEW_SKIP"
3769 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"
3771 if [ "$res" != "0" ]; then
3775 if [ "$Le_DeployHook" ]; then
3776 _deploy "$Le_Domain" "$Le_DeployHook"
3785 #renewAll [stopRenewOnError]
3788 _stopRenewOnError="$1"
3789 _debug "_stopRenewOnError
" "$_stopRenewOnError"
3792 for di in "${CERT_HOME}"/*.*/; do
3794 if ! [ -d "$di" ]; then
3795 _debug "Not directory
, skip
: $di"
3801 if _endswith "$d" "$ECC_SUFFIX"; then
3802 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
3803 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
3805 renew "$d" "$_isEcc"
3808 _debug "Return code
: $rc"
3809 if [ "$rc" != "0" ]; then
3810 if [ "$rc" = "$RENEW_SKIP" ]; then
3812 elif [ "$_stopRenewOnError" ]; then
3813 _err "Error renew
$d, stop now.
"
3817 _err "Error renew
$d, Go ahead to next one.
"
3828 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
3829 _usage "Usage
: $PROJECT_ENTRY --signcsr --csr mycsr.csr
-w /path
/to
/webroot
/a.com
/ "
3835 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
3836 if [ "$?
" != "0" ]; then
3837 _err "Can not
read subject from csr
: $_csrfile"
3840 _debug _csrsubj "$_csrsubj"
3842 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
3843 if [ "$?
" != "0" ]; then
3844 _err "Can not
read domain list from csr
: $_csrfile"
3847 _debug "_csrdomainlist
" "$_csrdomainlist"
3849 if [ -z "$_csrsubj" ]; then
3850 _csrsubj="$
(_getfield
"$_csrdomainlist" 1)"
3851 _debug _csrsubj "$_csrsubj"
3852 _csrdomainlist="$
(echo "$_csrdomainlist" | cut
-d , -f 2-)"
3853 _debug "_csrdomainlist
" "$_csrdomainlist"
3856 if [ -z "$_csrsubj" ]; then
3857 _err "Can not
read subject from csr
: $_csrfile"
3861 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
3862 if [ "$?
" != "0" ] || [ -z "$_csrkeylength" ]; then
3863 _err "Can not
read key length from csr
: $_csrfile"
3867 _initpath "$_csrsubj" "$_csrkeylength"
3868 mkdir -p "$DOMAIN_PATH"
3870 _info "Copy csr to
: $CSR_PATH"
3871 cp "$_csrfile" "$CSR_PATH"
3873 issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength"
3880 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
3881 _usage "Usage
: $PROJECT_ENTRY --showcsr --csr mycsr.csr
"
3887 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
3888 if [ "$?
" != "0" ] || [ -z "$_csrsubj" ]; then
3889 _err "Can not
read subject from csr
: $_csrfile"
3893 _info "Subject
=$_csrsubj"
3895 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
3896 if [ "$?
" != "0" ]; then
3897 _err "Can not
read domain list from csr
: $_csrfile"
3900 _debug "_csrdomainlist
" "$_csrdomainlist"
3902 _info "SubjectAltNames
=$_csrdomainlist"
3904 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
3905 if [ "$?
" != "0" ] || [ -z "$_csrkeylength" ]; then
3906 _err "Can not
read key length from csr
: $_csrfile"
3909 _info "KeyLength
=$_csrkeylength"
3917 if [ "$_raw" ]; then
3918 printf "%s
\n" "Main_Domain
${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew
"
3919 for di in "${CERT_HOME}"/*.*/; do
3920 if ! [ -d "$di" ]; then
3921 _debug "Not directory
, skip
: $di"
3927 if _endswith "$d" "$ECC_SUFFIX"; then
3928 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
3929 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
3931 _initpath "$d" "$_isEcc"
3932 if [ -f "$DOMAIN_CONF" ]; then
3934 printf "%s
\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
3939 if _exists column; then
3940 list "raw
" | column -t -s "$_sep"
3942 list "raw
" | tr "$_sep" '\t'
3952 for _d_api in $(echo "$_hooks" | tr ',' " "); do
3953 _deployApi="$
(_findHook
"$_d" deploy
"$_d_api")"
3954 if [ -z "$_deployApi" ]; then
3955 _err "The deploy hook
$_d_api is not found.
"
3958 _debug _deployApi "$_deployApi"
3961 if ! . "$_deployApi"; then
3962 _err "Load
file $_deployApi error. Please check your api
file and try again.
"
3966 d_command="${_d_api}_deploy
"
3967 if ! _exists "$d_command"; then
3968 _err "It seems that your api
file is not correct
, it must have a
function named
: $d_command"
3972 if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
3973 _err "Error deploy
for domain
:$_d"
3977 _err "Deploy error.
"
3980 _info "$
(__green Success
)"
3990 if [ -z "$_hooks" ]; then
3991 _usage "Usage
: $PROJECT_ENTRY --deploy -d domain.com
--deploy-hook cpanel
[--ecc] "
3995 _initpath "$_d" "$_isEcc"
3996 if [ ! -d "$DOMAIN_PATH" ]; then
3997 _err "Domain is not valid
:'$_d'"
4003 _savedomainconf Le_DeployHook "$_hooks"
4005 _deploy "$_d" "$_hooks"
4010 if [ -z "$_main_domain" ]; then
4011 _usage "Usage
: $PROJECT_ENTRY --installcert -d domain.com
[--ecc] [--certpath cert-file-path
] [--keypath key-file-path
] [--capath ca-cert-file-path
] [ --reloadCmd reloadCmd
] [--fullchainpath fullchain-path
]"
4019 _real_fullchain="$6"
4022 _initpath "$_main_domain" "$_isEcc"
4023 if [ ! -d "$DOMAIN_PATH" ]; then
4024 _err "Domain is not valid
:'$_main_domain'"
4028 _savedomainconf "Le_RealCertPath
" "$_real_cert"
4029 _savedomainconf "Le_RealCACertPath
" "$_real_ca"
4030 _savedomainconf "Le_RealKeyPath
" "$_real_key"
4031 _savedomainconf "Le_ReloadCmd
" "$_reload_cmd"
4032 _savedomainconf "Le_RealFullChainPath
" "$_real_fullchain"
4034 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
4037 #domain cert key ca fullchain reloadcmd backup-prefix
4043 _real_fullchain="$5"
4047 if [ "$_real_cert" = "$NO_VALUE" ]; then
4050 if [ "$_real_key" = "$NO_VALUE" ]; then
4053 if [ "$_real_ca" = "$NO_VALUE" ]; then
4056 if [ "$_reload_cmd" = "$NO_VALUE" ]; then
4059 if [ "$_real_fullchain" = "$NO_VALUE" ]; then
4063 _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
4064 mkdir -p "$_backup_path"
4066 if [ "$_real_cert" ]; then
4067 _info "Installing cert to
:$_real_cert"
4068 if [ -f "$_real_cert" ] && [ ! "$IS_RENEW" ]; then
4069 cp "$_real_cert" "$_backup_path/cert.bak
"
4071 cat "$CERT_PATH" >"$_real_cert"
4074 if [ "$_real_ca" ]; then
4075 _info "Installing CA to
:$_real_ca"
4076 if [ "$_real_ca" = "$_real_cert" ]; then
4077 echo "" >>"$_real_ca"
4078 cat "$CA_CERT_PATH" >>"$_real_ca"
4080 if [ -f "$_real_ca" ] && [ ! "$IS_RENEW" ]; then
4081 cp "$_real_ca" "$_backup_path/ca.bak
"
4083 cat "$CA_CERT_PATH" >"$_real_ca"
4087 if [ "$_real_key" ]; then
4088 _info "Installing key to
:$_real_key"
4089 if [ -f "$_real_key" ] && [ ! "$IS_RENEW" ]; then
4090 cp "$_real_key" "$_backup_path/key.bak
"
4092 cat "$CERT_KEY_PATH" >"$_real_key"
4095 if [ "$_real_fullchain" ]; then
4096 _info "Installing full chain to
:$_real_fullchain"
4097 if [ -f "$_real_fullchain" ] && [ ! "$IS_RENEW" ]; then
4098 cp "$_real_fullchain" "$_backup_path/fullchain.bak
"
4100 cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain"
4103 if [ "$_reload_cmd" ]; then
4104 _info "Run reload cmd
: $_reload_cmd"
4107 export CERT_KEY_PATH
4109 export CERT_FULLCHAIN_PATH
4110 cd "$DOMAIN_PATH" && eval "$_reload_cmd"
4112 _info "$
(__green
"Reload success")"
4114 _err "Reload error
for :$Le_Domain"
4124 if ! _exists "crontab
"; then
4125 _err "crontab doesn
't exist, so, we can not install cron jobs."
4126 _err "All your certs will not be renewed automatically."
4127 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
4131 _info "Installing cron job"
4132 if ! crontab -l | grep "$PROJECT_ENTRY --cron"; then
4133 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
4134 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
4136 _err "Can not install cronjob, $PROJECT_ENTRY not found."
4140 if [ "$_c_home" ]; then
4141 _c_entry="--config-home \"$_c_home\" "
4144 random_minute=$(_math $_t % 60)
4145 if _exists uname && uname -a | grep SunOS >/dev/null; then
4148 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
4153 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
4157 if [ "$?" != "0" ]; then
4158 _err "Install cron job failed. You need to manually renew your certs."
4159 _err "Or you can add cronjob by yourself:"
4160 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4165 uninstallcronjob() {
4166 if ! _exists "crontab"; then
4169 _info "Removing cron job"
4170 cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")"
4172 if _exists uname && uname -a | grep solaris >/dev/null; then
4173 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab --
4175 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -
4177 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4178 _info LE_WORKING_DIR
"$LE_WORKING_DIR"
4179 if _contains
"$cr" "--config-home"; then
4180 LE_CONFIG_HOME
="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
4181 _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
4190 if [ -z "$Le_Domain" ]; then
4191 _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com [--ecc]"
4197 _initpath "$Le_Domain" "$_isEcc"
4198 if [ ! -f "$DOMAIN_CONF" ]; then
4199 _err "$Le_Domain is not a issued domain, skip."
4203 if [ ! -f "$CERT_PATH" ]; then
4204 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
4208 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
4210 if [ -z "$cert" ]; then
4211 _err "Cert for $Le_Domain is empty found, skip."
4215 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
4216 uri="$API/acme/revoke-cert"
4218 if [ -f "$CERT_KEY_PATH" ]; then
4219 _info "Try domain key first."
4220 if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
4221 if [ -z "$response" ]; then
4222 _info "Revoke success."
4226 _err "Revoke error by domain key."
4231 _info "Domain key file doesn't exists.
"
4234 _info "Try account key.
"
4236 if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
4237 if [ -z "$response" ]; then
4238 _info "Revoke success.
"
4242 _err "Revoke error.
"
4252 if [ -z "$Le_Domain" ]; then
4253 _usage "Usage
: $PROJECT_ENTRY --remove -d domain.com
[--ecc]"
4259 _initpath "$Le_Domain" "$_isEcc"
4260 _removed_conf="$DOMAIN_CONF.removed
"
4261 if [ ! -f "$DOMAIN_CONF" ]; then
4262 if [ -f "$_removed_conf" ]; then
4263 _err "$Le_Domain is already removed
, You can remove the folder by yourself
: $DOMAIN_PATH"
4265 _err "$Le_Domain is not a issued domain
, skip.
"
4270 if mv "$DOMAIN_CONF" "$_removed_conf"; then
4271 _info "$Le_Domain is removed
, the key and cert files are
in $
(__green
$DOMAIN_PATH)"
4272 _info "You can remove them by yourself.
"
4275 _err "Remove
$Le_Domain failed.
"
4288 while [ "$_d_i" -lt "$_d_max_retry" ]; do
4289 _info "Deactivate
: $_d_domain"
4290 _d_i="$
(_math
$_d_i + 1)"
4292 if ! __get_domain_new_authz "$_d_domain"; then
4293 _err "Can not get domain new authz token.
"
4297 authzUri="$
(echo "$responseHeaders" |
grep "^Location:" | _head_n
1 | cut
-d ' ' -f 2 |
tr -d "\r\n")"
4298 _debug "authzUri
" "$authzUri"
4300 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
4301 _err "new-authz error
: $response"
4305 entry="$
(printf "%s\n" "$response" | _egrep_o
'{"type":"[^"]*","status":"valid","uri"[^}]*')"
4306 _debug entry "$entry"
4308 if [ -z "$entry" ]; then
4309 _info "No
more valid entry found.
"
4313 _vtype="$
(printf "%s\n" "$entry" | _egrep_o
'"type": *"[^"]*"' | cut
-d : -f 2 |
tr -d '"')"
4314 _debug _vtype "$_vtype"
4315 _info "Found
$_vtype"
4317 uri="$
(printf "%s\n" "$entry" | _egrep_o
'"uri":"[^"]*' | cut
-d : -f 2,3 |
tr -d '"')"
4320 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
4321 _info "Skip
$_vtype"
4325 _info "Deactivate
: $_vtype"
4327 if ! _send_signed_request "$authzUri" "{\"resource
\": \"authz
\", \"status
\":\"deactivated
\"}"; then
4328 _err "Can not deactivate
$_vtype.
"
4332 _info "Deactivate
: $_vtype success.
"
4336 if [ "$_d_i" -lt "$_d_max_retry" ]; then
4337 _info "Deactivated success
!"
4339 _err "Deactivate failed.
"
4348 _debug _d_domain_list "$_d_domain_list"
4349 if [ -z "$
(echo $_d_domain_list | cut
-d , -f 1)" ]; then
4350 _usage "Usage
: $PROJECT_ENTRY --deactivate -d domain.com
[-d domain.com
]"
4353 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
4354 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
4357 if ! _deactivate "$_d_dm" "$_d_type"; then
4363 # Detect profile file if not specified as environment variable
4365 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
4371 SHELLTYPE="$
(basename "/$SHELL")"
4373 if [ "$SHELLTYPE" = "bash
" ]; then
4374 if [ -f "$HOME/.bashrc
" ]; then
4375 DETECTED_PROFILE="$HOME/.bashrc
"
4376 elif [ -f "$HOME/.bash_profile
" ]; then
4377 DETECTED_PROFILE="$HOME/.bash_profile
"
4379 elif [ "$SHELLTYPE" = "zsh
" ]; then
4380 DETECTED_PROFILE="$HOME/.zshrc
"
4383 if [ -z "$DETECTED_PROFILE" ]; then
4384 if [ -f "$HOME/.profile
" ]; then
4385 DETECTED_PROFILE="$HOME/.profile
"
4386 elif [ -f "$HOME/.bashrc
" ]; then
4387 DETECTED_PROFILE="$HOME/.bashrc
"
4388 elif [ -f "$HOME/.bash_profile
" ]; then
4389 DETECTED_PROFILE="$HOME/.bash_profile
"
4390 elif [ -f "$HOME/.zshrc
" ]; then
4391 DETECTED_PROFILE="$HOME/.zshrc
"
4395 if [ ! -z "$DETECTED_PROFILE" ]; then
4396 echo "$DETECTED_PROFILE"
4402 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4405 #LOG_FILE=\"$DEFAULT_LOG_FILE\"
4412 " >"$ACCOUNT_CONF_PATH"
4420 if ! _exists "curl
" && ! _exists "wget
"; then
4421 _err "Please
install curl or wget first
, we need to access http resources.
"
4425 if [ -z "$_nocron" ]; then
4426 if ! _exists "crontab
"; then
4427 _err "It is recommended to
install crontab first. try to
install 'cron, crontab, crontabs or vixie-cron'.
"
4428 _err "We need to
set cron job to renew the certs automatically.
"
4429 _err "Otherwise
, your certs will not be able to be renewed automatically.
"
4430 if [ -z "$FORCE" ]; then
4431 _err "Please add
'--force' and try
install again to go without crontab.
"
4432 _err ".
/$PROJECT_ENTRY --install --force"
4438 if ! _exists "$ACME_OPENSSL_BIN"; then
4439 _err "Please
install openssl first. ACME_OPENSSL_BIN
=$ACME_OPENSSL_BIN"
4440 _err "We need openssl to generate keys.
"
4444 if ! _exists "nc
"; then
4445 _err "It is recommended to
install nc first
, try to
install 'nc' or
'netcat'.
"
4446 _err "We use nc
for standalone server
if you use standalone mode.
"
4447 _err "If you don
't use standalone mode, just ignore this warning."
4456 if [ -z "$_shebang" ]; then
4457 _usage "Usage: file shebang"
4460 cp "$_file" "$_file.tmp"
4461 echo "$_shebang" >"$_file"
4462 sed -n 2,99999p "$_file.tmp" >>"$_file"
4471 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4472 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
4473 echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
4474 echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
4475 echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
4478 if [ "$_c_home" ]; then
4479 _c_entry=" --config-home '$_c_home'"
4482 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
4483 if [ "$_c_home" ]; then
4484 _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
4486 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4488 _profile="$(_detect_profile)"
4489 if [ "$_profile" ]; then
4490 _debug "Found profile: $_profile"
4491 _info "Installing alias to '$_profile'"
4492 _setopt "$_profile" ". \"$_envfile\""
4493 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
4495 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
4499 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
4500 _csh_profile="$HOME/.cshrc"
4501 if [ -f "$_csh_profile" ]; then
4502 _info "Installing alias to '$_csh_profile'"
4503 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
4504 if [ "$_c_home" ]; then
4505 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
4507 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4508 _setopt "$_csh_profile" "source \"$_cshfile\""
4512 _tcsh_profile="$HOME/.tcshrc"
4513 if [ -f "$_tcsh_profile" ]; then
4514 _info "Installing alias to '$_tcsh_profile'"
4515 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
4516 if [ "$_c_home" ]; then
4517 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
4519 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4520 _setopt "$_tcsh_profile" "source \"$_cshfile\""
4528 if [ -z "$LE_WORKING_DIR" ]; then
4529 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
4534 if ! _initpath; then
4535 _err "Install failed."
4538 if [ "$_nocron" ]; then
4539 _debug "Skip install cron job"
4542 if ! _precheck "$_nocron"; then
4543 _err "Pre-check failed, can not install."
4548 if [ -d "$HOME/.le" ]; then
4549 for envfile in "le.env" "le.sh.env"; do
4550 if [ -f "$HOME/.le/$envfile" ]; then
4551 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
4553 _info "You are upgrading from le.sh"
4554 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
4555 mv "$HOME/.le" "$LE_WORKING_DIR"
4556 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4563 _info "Installing to $LE_WORKING_DIR"
4565 if ! mkdir -p "$LE_WORKING_DIR"; then
4566 _err "Can not create working dir: $LE_WORKING_DIR"
4570 chmod 700 "$LE_WORKING_DIR"
4572 if ! mkdir -p "$LE_CONFIG_HOME"; then
4573 _err "Can not create config dir: $LE_CONFIG_HOME"
4577 chmod 700 "$LE_CONFIG_HOME"
4579 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
4581 if [ "$?" != "0" ]; then
4582 _err "Install failed, can not copy $PROJECT_ENTRY"
4586 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
4588 _installalias "$_c_home"
4590 for subf in $_SUB_FOLDERS; do
4591 if [ -d "$subf" ]; then
4592 mkdir -p "$LE_WORKING_DIR/$subf"
4593 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
4597 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4601 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
4602 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
4605 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
4606 _saveaccountconf "CERT_HOME" "$CERT_HOME"
4609 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
4610 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
4613 if [ -z "$_nocron" ]; then
4614 installcronjob "$_c_home"
4617 if [ -z "$NO_DETECT_SH" ]; then
4619 if _exists bash; then
4620 _info "Good, bash is found, so change the shebang to use bash as preferred."
4621 _shebang='#!/usr/bin/env bash'
4622 _setShebang
"$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
4623 for subf
in $_SUB_FOLDERS; do
4624 if [ -d "$LE_WORKING_DIR/$subf" ]; then
4625 for _apifile
in "$LE_WORKING_DIR/$subf/"*.sh
; do
4626 _setShebang
"$_apifile" "$_shebang"
4639 if [ -z "$_nocron" ]; then
4646 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
4647 _info
"The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself."
4654 _profile
="$(_detect_profile)"
4655 if [ "$_profile" ]; then
4656 _info
"Uninstalling alias from: '$_profile'"
4657 text
="$(cat "$_profile")"
4658 echo "$text" |
sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
4661 _csh_profile
="$HOME/.cshrc"
4662 if [ -f "$_csh_profile" ]; then
4663 _info
"Uninstalling alias from: '$_csh_profile'"
4664 text
="$(cat "$_csh_profile")"
4665 echo "$text" |
sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
4668 _tcsh_profile
="$HOME/.tcshrc"
4669 if [ -f "$_tcsh_profile" ]; then
4670 _info
"Uninstalling alias from: '$_csh_profile'"
4671 text
="$(cat "$_tcsh_profile")"
4672 echo "$text" |
sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
4680 if [ "$AUTO_UPGRADE" = "1" ]; then
4681 export LE_WORKING_DIR
4684 _err
"Cron:Upgrade failed!"
4688 .
"$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev
/null
4694 _info
"Auto upgraded to: $VER"
4710 echo "Usage: $PROJECT_ENTRY command ...[parameters]....
4712 --help, -h Show this help message.
4713 --version, -v Show version info.
4714 --install Install $PROJECT_NAME to your system.
4715 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
4716 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
4717 --issue Issue a cert.
4718 --signcsr Issue a cert from an existing csr.
4719 --deploy Deploy the cert to your server.
4720 --install-cert Install the issued cert to apache/nginx or any other server.
4721 --renew, -r Renew a cert.
4722 --renew-all Renew all the certs.
4723 --revoke Revoke a cert.
4724 --remove Remove the cert from $PROJECT
4725 --list List all the certs.
4726 --showcsr Show the content of a csr.
4727 --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.
4728 --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
4729 --cron Run cron job to renew all the certs.
4730 --toPkcs Export the certificate and key to a pfx file.
4731 --toPkcs8 Convert to pkcs8 format.
4732 --update-account Update account info.
4733 --register-account Register account key.
4734 --create-account-key Create an account private key, professional use.
4735 --create-domain-key Create an domain private key, professional use.
4736 --createCSR, -ccsr Create CSR , professional use.
4737 --deactivate Deactivate the domain authz, professional use.
4740 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
4741 --force, -f Used to force to install or force to renew a cert immediately.
4742 --staging, --test Use staging server, just for test.
4743 --debug Output debug info.
4744 --output-insecure Output all the sensitive messages. By default all the credentials/sensitive messages are hidden from the output/debug/log for secure.
4745 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
4746 --standalone Use standalone mode.
4747 --stateless Use stateless mode, see: $_STATELESS_WIKI
4748 --tls Use standalone tls mode.
4749 --apache Use apache mode.
4750 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
4751 --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.
4753 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
4754 --accountkeylength, -ak [2048] Specifies the account key length.
4755 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
4756 --log-level 1|2 Specifies the log level, default is 1.
4757 --syslog [0|3|6|7] Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
4759 These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
4761 --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path.
4762 --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path.
4763 --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path.
4764 --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path.
4766 --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
4768 --accountconf Specifies a customized account config file.
4769 --home Specifies the home dir for $PROJECT_NAME .
4770 --cert-home Specifies the home dir to save all the certs, only valid for '--install' command.
4771 --config-home Specifies the home dir to save all the configurations.
4772 --useragent Specifies the user agent string. it will be saved for future use too.
4773 --accountemail Specifies the account email for registering, Only valid for the '--install' command.
4774 --accountkey Specifies the account key path, Only valid for the '--install' command.
4775 --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
4776 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
4777 --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
4778 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
4779 --listraw Only used for '--list' command, list the certs in raw format.
4780 --stopRenewOnError, -se Only valid for '--renew-all' command. Stop if one cert has error in renewal.
4781 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
4782 --ca-bundle Specifices the path to the CA certificate bundle to verify api server's certificate.
4783 --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.
4784 --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
4785 --csr Specifies the input csr.
4786 --pre-hook Command to be run before obtaining any certificates.
4787 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obain/renew is success or failed.
4788 --renew-hook Command to be run once for each successfully renewed certificate.
4789 --deploy-hook The hook file to deploy cert
4790 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
4791 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
4792 --listen-v4 Force standalone/tls server to listen at ipv4.
4793 --listen-v6 Force standalone/tls server to listen at ipv6.
4794 --openssl-bin Specifies a custom openssl bin location.
4795 --use-wget Force to use wget, if you have both curl and wget installed.
4801 _info
"Installing from online archive."
4803 if [ ! "$BRANCH" ]; then
4807 target
="$PROJECT/archive/$BRANCH.tar.gz"
4808 _info
"Downloading $target"
4809 localname
="$BRANCH.tar.gz"
4810 if ! _get
"$target" >$localname; then
4811 _err
"Download error."
4815 _info
"Extracting $localname"
4816 if ! (tar xzf
$localname || gtar xzf
$localname); then
4817 _err
"Extraction error."
4821 cd "$PROJECT_NAME-$BRANCH"
4822 chmod +x
$PROJECT_ENTRY
4823 if .
/$PROJECT_ENTRY install "$_nocron"; then
4824 _info
"Install success!"
4829 rm -rf "$PROJECT_NAME-$BRANCH"
4837 export LE_WORKING_DIR
4838 cd "$LE_WORKING_DIR"
4839 _installOnline
"nocron"
4841 _info
"Upgrade success!"
4844 _err
"Upgrade failed!"
4849 _processAccountConf
() {
4850 if [ "$_useragent" ]; then
4851 _saveaccountconf
"USER_AGENT" "$_useragent"
4852 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
4853 _saveaccountconf
"USER_AGENT" "$USER_AGENT"
4856 if [ "$_accountemail" ]; then
4857 _saveaccountconf
"ACCOUNT_EMAIL" "$_accountemail"
4858 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
4859 _saveaccountconf
"ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
4862 if [ "$_openssl_bin" ]; then
4863 _saveaccountconf
"ACME_OPENSSL_BIN" "$_openssl_bin"
4864 elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
4865 _saveaccountconf
"ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
4868 if [ "$_auto_upgrade" ]; then
4869 _saveaccountconf
"AUTO_UPGRADE" "$_auto_upgrade"
4870 elif [ "$AUTO_UPGRADE" ]; then
4871 _saveaccountconf
"AUTO_UPGRADE" "$AUTO_UPGRADE"
4874 if [ "$_use_wget" ]; then
4875 _saveaccountconf
"ACME_USE_WGET" "$_use_wget"
4876 elif [ "$ACME_USE_WGET" ]; then
4877 _saveaccountconf
"ACME_USE_WGET" "$ACME_USE_WGET"
4885 _altdomains
="$NO_VALUE"
4888 _accountkeylength
=""
4905 _stopRenewOnError
=""
4925 while [ ${#} -gt 0 ]; do
4957 --installcert |
-i |
--install-cert)
4963 --renewAll |
--renewall |
--renew-all)
4975 --installcronjob |
--install-cronjob)
4976 _CMD
="installcronjob"
4978 --uninstallcronjob |
--uninstall-cronjob)
4979 _CMD
="uninstallcronjob"
4990 --createAccountKey |
--createaccountkey |
-cak |
--create-account-key)
4991 _CMD
="createAccountKey"
4993 --createDomainKey |
--createdomainkey |
-cdk |
--create-domain-key)
4994 _CMD
="createDomainKey"
4996 --createCSR |
--createcsr |
-ccr)
5002 --updateaccount |
--update-account)
5003 _CMD
="updateaccount"
5005 --registeraccount |
--register-account)
5006 _CMD
="registeraccount"
5011 if [ "$_dvalue" ]; then
5012 if _startswith
"$_dvalue" "-"; then
5013 _err
"'$_dvalue' is not a valid domain for parameter '$1'"
5016 if _is_idn
"$_dvalue" && ! _exists idn
; then
5017 _err
"It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
5021 if [ -z "$_domain" ]; then
5024 if [ "$_altdomains" = "$NO_VALUE" ]; then
5025 _altdomains
="$_dvalue"
5027 _altdomains
="$_altdomains,$_dvalue"
5042 if [ -z "$2" ] || _startswith
"$2" "-"; then
5043 DEBUG
="$DEBUG_LEVEL_DEFAULT"
5050 export OUTPUT_INSECURE
=1
5054 if [ -z "$_webroot" ]; then
5057 _webroot
="$_webroot,$wvalue"
5063 if [ -z "$_webroot" ]; then
5066 _webroot
="$_webroot,$wvalue"
5070 wvalue
="$MODE_STATELESS"
5071 if [ -z "$_webroot" ]; then
5074 _webroot
="$_webroot,$wvalue"
5079 _local_address
="$_local_address$lvalue,"
5084 if [ -z "$_webroot" ]; then
5087 _webroot
="$_webroot,$wvalue"
5092 if [ -z "$_webroot" ]; then
5095 _webroot
="$_webroot,$wvalue"
5100 if [ -z "$_webroot" ]; then
5103 _webroot
="$_webroot,$wvalue"
5108 if ! _startswith
"$2" "-"; then
5112 if [ -z "$_webroot" ]; then
5115 _webroot
="$_webroot,$wvalue"
5120 Le_DNSSleep
="$_dnssleep"
5128 --accountkeylength |
-ak)
5129 _accountkeylength
="$2"
5149 --reloadcmd |
--reloadCmd)
5159 ACCOUNT_CONF_PATH
="$_accountconf"
5166 --certhome |
--cert-home)
5168 CERT_HOME
="$_certhome"
5173 LE_CONFIG_HOME
="$_confighome"
5178 USER_AGENT
="$_useragent"
5183 ACCOUNT_EMAIL
="$_accountemail"
5188 ACCOUNT_KEY_PATH
="$_accountkey"
5193 Le_RenewalDays
="$_days"
5198 Le_HTTPPort
="$_httpport"
5203 Le_TLSPort
="$_tlsport"
5210 --stopRenewOnError |
--stoprenewonerror |
-se)
5211 _stopRenewOnError
="1"
5218 _ca_bundle
="$(_readlink -f "$2")"
5219 CA_BUNDLE
="$_ca_bundle"
5245 if [ -z "$2" ] || _startswith
"$2" "-"; then
5246 _usage
"Please specify a value for '--deploy-hook'"
5249 _deploy_hook
="$_deploy_hook$2,"
5252 --ocsp-must-staple |
--ocsp)
5258 if _startswith
"$_logfile" '-'; then
5263 LOG_FILE
="$_logfile"
5264 if [ -z "$LOG_LEVEL" ]; then
5265 LOG_LEVEL
="$DEFAULT_LOG_LEVEL"
5270 LOG_LEVEL
="$_log_level"
5274 if ! _startswith
"$2" '-'; then
5278 if [ -z "$_syslog" ]; then
5279 _syslog
="$SYSLOG_LEVEL_DEFAULT"
5284 if [ -z "$_auto_upgrade" ] || _startswith
"$_auto_upgrade" '-'; then
5289 AUTO_UPGRADE
="$_auto_upgrade"
5293 Le_Listen_V4
="$_listen_v4"
5297 Le_Listen_V6
="$_listen_v6"
5301 ACME_OPENSSL_BIN
="$_openssl_bin"
5309 _err
"Unknown parameter : $1"
5317 if [ "${_CMD}" != "install" ]; then
5319 if [ "$_log" ]; then
5320 if [ -z "$_logfile" ]; then
5321 _logfile
="$DEFAULT_LOG_FILE"
5324 if [ "$_logfile" ]; then
5325 _saveaccountconf
"LOG_FILE" "$_logfile"
5326 LOG_FILE
="$_logfile"
5329 if [ "$_log_level" ]; then
5330 _saveaccountconf
"LOG_LEVEL" "$_log_level"
5331 LOG_LEVEL
="$_log_level"
5334 if [ "$_syslog" ]; then
5335 if _exists logger
; then
5336 if [ "$_syslog" = "0" ]; then
5337 _clearaccountconf
"SYS_LOG"
5339 _saveaccountconf
"SYS_LOG" "$_syslog"
5343 _err
"The 'logger' command is not found, can not enable syslog."
5344 _clearaccountconf
"SYS_LOG"
5352 _debug2 LE_WORKING_DIR
"$LE_WORKING_DIR"
5354 if [ "$DEBUG" ]; then
5359 install) install "$_nocron" "$_confighome" ;;
5360 uninstall
) uninstall
"$_nocron" ;;
5363 issue
"$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address"
5366 deploy
"$_domain" "$_deploy_hook" "$_ecc"
5369 signcsr
"$_csr" "$_webroot"
5372 showcsr
"$_csr" "$_domain"
5375 installcert
"$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_ecc"
5378 renew
"$_domain" "$_ecc"
5381 renewAll
"$_stopRenewOnError"
5384 revoke
"$_domain" "$_ecc"
5387 remove
"$_domain" "$_ecc"
5390 deactivate
"$_domain,$_altdomains"
5393 registeraccount
"$_accountkeylength"
5401 installcronjob
) installcronjob
"$_confighome" ;;
5402 uninstallcronjob
) uninstallcronjob
;;
5405 toPkcs
"$_domain" "$_password" "$_ecc"
5408 toPkcs8
"$_domain" "$_ecc"
5411 createAccountKey
"$_accountkeylength"
5414 createDomainKey
"$_domain" "$_keylength"
5417 createCSR
"$_domain" "$_altdomains" "$_ecc"
5421 if [ "$_CMD" ]; then
5422 _err
"Invalid command: $_CMD"
5429 if [ "$_ret" != "0" ]; then
5433 if [ "${_CMD}" = "install" ]; then
5434 if [ "$_log" ]; then
5435 if [ -z "$LOG_FILE" ]; then
5436 LOG_FILE
="$DEFAULT_LOG_FILE"
5438 _saveaccountconf
"LOG_FILE" "$LOG_FILE"
5441 if [ "$_log_level" ]; then
5442 _saveaccountconf
"LOG_LEVEL" "$_log_level"
5445 if [ "$_syslog" ]; then
5446 if _exists logger
; then
5447 if [ "$_syslog" = "0" ]; then
5448 _clearaccountconf
"SYS_LOG"
5450 _saveaccountconf
"SYS_LOG" "$_syslog"
5453 _err
"The 'logger' command is not found, can not enable syslog."
5454 _clearaccountconf
"SYS_LOG"
5464 if [ "$INSTALLONLINE" ]; then
5471 [ -z "$1" ] && showhelp
&& return
5472 if _startswith
"$1" '-'; then _process
"$@"; else "$@"; fi