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 STATE_VERIFIED
="verified_ok"
46 BEGIN_CSR
="-----BEGIN CERTIFICATE REQUEST-----"
47 END_CSR
="-----END CERTIFICATE REQUEST-----"
49 BEGIN_CERT
="-----BEGIN CERTIFICATE-----"
50 END_CERT
="-----END CERTIFICATE-----"
55 ECC_SUFFIX
="${ECC_SEP}ecc"
60 DEFAULT_LOG_LEVEL
="$LOG_LEVEL_1"
62 _DEBUG_WIKI
="https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh"
70 if [ "$__INTERACTIVE" ]; then
71 printf '\033[1;31;32m'
74 if [ "$__INTERACTIVE" ]; then
80 if [ "$__INTERACTIVE" ]; then
81 printf '\033[1;31;40m'
84 if [ "$__INTERACTIVE" ]; then
90 if [ -z "$NO_TIMESTAMP" ] ||
[ "$NO_TIMESTAMP" = "0" ]; then
91 printf -- "%s" "[$(date)] "
96 printf -- "%s" "$1='$2'"
102 echo "Diagnosis versions: "
103 echo "openssl:$OPENSSL_BIN"
104 if _exists
"$OPENSSL_BIN"; then
105 $OPENSSL_BIN version
2>&1
107 echo "$OPENSSL_BIN doesn't exists."
111 if [ "$_APACHECTL" ] && _exists
"$_APACHECTL"; then
114 echo "apache doesn't exists."
118 if _exists
"nc"; then
121 _debug
"nc doesn't exists."
126 [ -z "$LOG_FILE" ] && return
127 _printargs
"$@" >>"$LOG_FILE"
137 if [ -z "$NO_TIMESTAMP" ] ||
[ "$NO_TIMESTAMP" = "0" ]; then
138 printf -- "%s" "[$(date)] " >&2
155 if [ -z "$LOG_LEVEL" ] ||
[ "$LOG_LEVEL" -ge "$LOG_LEVEL_1" ]; then
158 if [ -z "$DEBUG" ]; then
165 if [ "$LOG_LEVEL" ] && [ "$LOG_LEVEL" -ge "$LOG_LEVEL_2" ]; then
168 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
174 if [ "$LOG_LEVEL" ] && [ "$LOG_LEVEL" -ge "$LOG_LEVEL_3" ]; then
177 if [ "$DEBUG" ] && [ "$DEBUG" -ge "3" ]; then
185 echo "$_str" |
grep "^$_sub" >/dev
/null
2>&1
191 echo "$_str" |
grep -- "$_sub\$" >/dev
/null
2>&1
197 echo "$_str" |
grep -- "$_sub" >/dev
/null
2>&1
204 if [ -z "$_field" ]; then
205 _usage
"Usage: str field [sep]"
209 if [ -z "$_sep" ]; then
213 for f
in $
(echo "$_str" |
tr ',' ' '); do
214 if [ "$f" = "$_field" ]; then
215 _debug2
"'$_str' contains '$_field'"
216 return 0 #contains ok
219 _debug2
"'$_str' does not contain '$_field'"
220 return 1 #not contains
228 if [ -z "$_findex" ]; then
229 _usage
"Usage: str field [sep]"
233 if [ -z "$_sep" ]; then
238 while [ "$_ffi" -gt "0" ]; do
239 _fv
="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")"
241 printf -- "%s" "$_fv"
244 _ffi
="$(_math "$_ffi" - 1)"
247 printf -- "%s" "$_str"
253 if [ -z "$cmd" ]; then
254 _usage
"Usage: _exists cmd"
258 if eval type type >/dev
/null
2>&1; then
259 eval type "$cmd" >/dev
/null
2>&1
260 elif command >/dev
/null
2>&1; then
261 command -v "$cmd" >/dev
/null
2>&1
263 which "$cmd" >/dev
/null
2>&1
266 _debug3
"$cmd exists=$ret"
273 printf "%s" "$(($_m_opts))"
305 if [ "$(printf '\x41')" != 'A' ]; then
314 _debug3 _URGLY_PRINTF
"$_URGLY_PRINTF"
316 if [ -z "$_URGLY_PRINTF" ]; then
317 h
="$(printf "%s
" "$hex" | cut -c $i-$j)"
323 ic
="$(printf "%s
" "$hex" | cut -c $i)"
324 jc
="$(printf "%s
" "$hex" | cut -c $j)"
325 if [ -z "$ic$jc" ]; then
328 ic
="$(_h_char_2_dec "$ic")"
329 jc
="$(_h_char_2_dec "$jc")"
330 printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")""%s"
333 i="$(_math "$i" + 2)"
334 j="$(_math "$j" + 2)"
344 while [ "$_h_i" -le "$_str_len" ]; do
345 _str_c="$(printf "%s" "$_str" | cut -c "$_h_i")"
346 printf "%02x" "'$_str_c"
347 _h_i="$
(_math
"$_h_i" + 1)"
355 if [ -z "$filename" ]; then
356 _usage "Usage
:_sed_i options filename
"
359 _debug2 options "$options"
360 if sed -h 2>&1 | grep "\
-i\
[SUFFIX
]" >/dev/null 2>&1; then
361 _debug "Using
sed -i"
362 sed -i "$options" "$filename"
364 _debug "No
-i support
in sed"
365 text="$
(cat "$filename")"
366 echo "$text" | sed "$options" >"$filename"
371 if ! egrep -o "$1" 2>/dev/null; then
372 sed -n 's/.*\('"$1"'\).*/\1/p'
376 #Usage: file startline endline
381 if [ -z "$endline" ]; then
382 _usage "Usage
: file startline endline
"
386 i="$
(grep -n -- "$startline" "$filename" | cut
-d : -f 1)"
388 _err "Can not
find start line
: $startline"
391 i="$
(_math
"$i" + 1)"
394 j="$
(grep -n -- "$endline" "$filename" | cut
-d : -f 1)"
396 _err "Can not
find end line
: $endline"
399 j="$
(_math
"$j" - 1)"
402 sed -n "$i,${j}p
" "$filename"
409 $OPENSSL_BIN base64 -e
411 $OPENSSL_BIN base64 -e | tr -d '\r\n'
418 $OPENSSL_BIN base64 -d -A
420 $OPENSSL_BIN base64 -d
424 #Usage: hashalg [outputhex]
425 #Output Base64-encoded digest
428 if [ -z "$alg" ]; then
429 _usage "Usage
: _digest hashalg
"
435 if [ "$alg" = "sha256
" ] || [ "$alg" = "sha1
" ] || [ "$alg" = "md5
" ]; then
436 if [ "$outputhex" ]; then
437 $OPENSSL_BIN dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' '
439 $OPENSSL_BIN dgst -"$alg" -binary | _base64
442 _err "$alg is not supported yet
"
448 #Usage: hashalg secret_hex [outputhex]
455 if [ -z "$secret_hex" ]; then
456 _usage "Usage
: _hmac hashalg secret
[outputhex
]"
460 if [ "$alg" = "sha256
" ] || [ "$alg" = "sha1
" ]; then
461 if [ "$outputhex" ]; then
462 $OPENSSL_BIN dgst -"$alg" -mac HMAC -macopt "hexkey
:$secret_hex" | cut -d = -f 2 | tr -d ' '
464 $OPENSSL_BIN dgst -"$alg" -mac HMAC -macopt "hexkey
:$secret_hex" -binary
467 _err "$alg is not supported yet
"
473 #Usage: keyfile hashalg
474 #Output: Base64-encoded signature value
478 if [ -z "$alg" ]; then
479 _usage "Usage
: _sign keyfile hashalg
"
483 _sign_openssl="$OPENSSL_BIN dgst
-sign $keyfile "
484 if [ "$alg" = "sha256
" ]; then
485 _sign_openssl="$_sign_openssl -$alg"
487 _err "$alg is not supported yet
"
491 if grep "BEGIN RSA PRIVATE KEY
" "$keyfile" >/dev/null 2>&1; then
492 $_sign_openssl | _base64
493 elif grep "BEGIN EC PRIVATE KEY
" "$keyfile" >/dev/null 2>&1; then
494 if ! _signedECText="$
($_sign_openssl |
$OPENSSL_BIN asn1parse
-inform DER
)"; then
495 _err "Sign failed
: $_sign_openssl"
496 _err "Key
file: $keyfile"
497 _err "Key content
:$
(wc -l <"$keyfile") lises
"
500 _debug3 "_signedECText
" "$_signedECText"
501 _ec_r="$
(echo "$_signedECText" | _head_n
2 | _tail_n
1 | cut
-d : -f 4 |
tr -d "\r\n")"
502 _debug3 "_ec_r
" "$_ec_r"
503 _ec_s="$
(echo "$_signedECText" | _head_n
3 | _tail_n
1 | cut
-d : -f 4 |
tr -d "\r\n")"
504 _debug3 "_ec_s
" "$_ec_s"
505 printf "%s
" "$_ec_r$_ec_s" | _h2b | _base64
507 _err "Unknown key
file format.
"
517 if [ -z "$_length" ]; then
521 [ "$_length" != "1024" ] \
522 && [ "$_length" != "2048" ] \
523 && [ "$_length" != "3072" ] \
524 && [ "$_length" != "4096" ] \
525 && [ "$_length" != "8192" ]
528 # _createkey 2048|ec-256 file
533 if _startswith "$length" "ec-
"; then
534 length=$(printf "%s
" "$length" | cut -d '-' -f 2-100)
536 if [ "$length" = "256" ]; then
539 if [ "$length" = "384" ]; then
542 if [ "$length" = "521" ]; then
548 if [ -z "$length" ]; then
552 _debug "Use length
$length"
554 if _isEccKey "$length"; then
555 _debug "Using ec name
: $eccname"
556 $OPENSSL_BIN ecparam -name "$eccname" -genkey 2>/dev/null >"$f"
558 _debug "Using RSA
: $length"
559 $OPENSSL_BIN genrsa "$length" 2>/dev/null >"$f"
562 if [ "$?
" != "0" ]; then
563 _err "Create key error.
"
571 _debug2 _is_idn_d "$_is_idn_d"
572 _idn_temp=$(printf "%s
" "$_is_idn_d" | tr -d '[0-9]' | tr -d '[a-z]' | tr -d '[A-Z]' | tr -d '.,-')
573 _debug2 _idn_temp "$_idn_temp"
578 #aa.com,bb.com,cc.com
581 if ! _is_idn "$__idn_d"; then
582 printf "%s
" "$__idn_d"
587 if _contains "$__idn_d" ','; then
589 for f in $(echo "$__idn_d" | tr ',' ' '); do
590 [ -z "$f" ] && continue
591 if [ -z "$_i_first" ]; then
596 idn --quiet "$f" | tr -d "\r\n"
599 idn "$__idn_d" | tr -d "\r\n"
602 _err "Please
install idn to process IDN names.
"
606 #_createcsr cn san_list keyfile csrfile conf
614 _debug2 domain "$domain"
615 _debug2 domainlist "$domainlist"
616 _debug2 csrkey "$csrkey"
618 _debug2 csrconf "$csrconf"
620 printf "[ req_distinguished_name
]\n[ req
]\ndistinguished_name
= req_distinguished_name
\nreq_extensions
= v3_req
\n[ v3_req
]\n\nkeyUsage
= nonRepudiation
, digitalSignature
, keyEncipherment
" >"$csrconf"
622 if [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then
624 _info "Single domain
" "$domain"
626 domainlist="$
(_idn
"$domainlist")"
627 _debug2 domainlist "$domainlist"
628 if _contains "$domainlist" ","; then
629 alt="DNS
:$
(echo "$domainlist" |
sed "s/,/,DNS:/g")"
631 alt="DNS
:$domainlist"
634 _info "Multi domain
" "$alt"
635 printf -- "\nsubjectAltName
=$alt" >>"$csrconf"
637 if [ "$Le_OCSP_Stable" ]; then
638 _savedomainconf Le_OCSP_Stable "$Le_OCSP_Stable"
639 printf -- "\nbasicConstraints
= CA
:FALSE
\n1.3.6.1.5.5.7.1.24=DER
:30:03:02:01:05" >>"$csrconf"
642 _csr_cn="$
(_idn
"$domain")"
643 _debug2 _csr_cn "$_csr_cn"
644 $OPENSSL_BIN req -new -sha256 -key "$csrkey" -subj "/CN
=$_csr_cn" -config "$csrconf" -out "$csr"
647 #_signcsr key csr conf cert
655 _msg="$
($OPENSSL_BIN x509
-req -days 365 -in "$csr" -signkey "$key" -extensions v3_req
-extfile "$conf" -out "$cert" 2>&1)"
662 _readSubjectFromCSR() {
664 if [ -z "$_csrfile" ]; then
665 _usage "_readSubjectFromCSR mycsr.csr
"
668 $OPENSSL_BIN req -noout -in "$_csrfile" -subject | _egrep_o "CN
=.
*" | cut -d = -f 2 | cut -d / -f 1 | tr -d '\n'
672 #echo comma separated domain list
673 _readSubjectAltNamesFromCSR() {
675 if [ -z "$_csrfile" ]; then
676 _usage "_readSubjectAltNamesFromCSR mycsr.csr
"
680 _csrsubj="$
(_readSubjectFromCSR
"$_csrfile")"
681 _debug _csrsubj "$_csrsubj"
683 _dnsAltnames="$
($OPENSSL_BIN req
-noout -text -in "$_csrfile" |
grep "^ *DNS:.*" |
tr -d ' \n')"
684 _debug _dnsAltnames "$_dnsAltnames"
686 if _contains "$_dnsAltnames," "DNS
:$_csrsubj,"; then
687 _debug "AltNames contains subject
"
688 _dnsAltnames="$
(printf "%s" "$_dnsAltnames," |
sed "s/DNS:$_csrsubj,//g")"
690 _debug "AltNames doesn
't contain subject"
693 printf "%s" "$_dnsAltnames" | sed "s/DNS://g"
697 _readKeyLengthFromCSR() {
699 if [ -z "$_csrfile" ]; then
700 _usage "_readKeyLengthFromCSR mycsr.csr"
704 _outcsr="$($OPENSSL_BIN req -noout -text -in "$_csrfile")"
705 if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then
707 echo "$_outcsr" | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' '
710 echo "$_outcsr" | _egrep_o "^ *Public-Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1
717 if _exists "ss"; then
719 ss -ntpl | grep ":$_port "
723 if _exists "netstat"; then
724 _debug "Using: netstat"
725 if netstat -h 2>&1 | grep "\-p proto" >/dev/null; then
726 #for windows version netstat tool
727 netstat -an -p tcp | grep "LISTENING" | grep ":$_port "
729 if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then
730 netstat -an -p tcp | grep LISTEN | grep ":$_port "
731 elif netstat -help 2>&1 | grep -- '-P protocol
' >/dev/null; then
733 netstat -an -P tcp | grep "\.$_port " | grep "LISTEN"
735 netstat -ntpl | grep ":$_port "
744 #domain [password] [isEcc]
748 if [ -z "$domain" ]; then
749 _usage "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]"
755 _initpath "$domain" "$_isEcc"
757 if [ "$pfxPassword" ]; then
758 $OPENSSL_BIN pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH" -password "pass:$pfxPassword"
760 $OPENSSL_BIN pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH"
763 if [ "$?" = "0" ]; then
764 _info "Success, Pfx is exported to: $CERT_PFX_PATH"
771 _info "Creating account key"
773 _usage "Usage: $PROJECT_ENTRY --createAccountKey --accountkeylength 2048"
778 _create_account_key "$length"
782 _create_account_key() {
786 if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then
787 _debug "Use default length $DEFAULT_ACCOUNT_KEY_LENGTH"
788 length="$DEFAULT_ACCOUNT_KEY_LENGTH"
791 _debug length "$length"
795 if [ -f "$ACCOUNT_KEY_PATH" ]; then
796 _info "Account key exists, skip"
799 #generate account key
800 _createkey "$length" "$ACCOUNT_KEY_PATH"
807 _info "Creating domain key"
809 _usage "Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]"
816 if [ -z "$length" ]; then
817 _debug "Use DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH"
818 length="$DEFAULT_DOMAIN_KEY_LENGTH"
821 _initpath "$domain" "$length"
823 if [ ! -f "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$IS_RENEW" ]); then
824 _createkey "$length" "$CERT_KEY_PATH"
826 if [ "$IS_RENEW" ]; then
827 _info "Domain key exists, skip"
830 _err "Domain key exists, do you want to overwrite the key?"
831 _err "Add '--force', and try again."
838 # domain domainlist isEcc
842 _usage "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]"
850 _initpath "$domain" "$_isEcc"
852 if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then
853 _info "CSR exists, skip"
857 if [ ! -f "$CERT_KEY_PATH" ]; then
858 _err "The key file is not found: $CERT_KEY_PATH"
859 _err "Please create the key file first."
862 _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"
867 tr '/+' '_-
' | tr -d '= '
872 if date -u -d@"$1" 2>/dev/null; then
877 if date -u -r "$1" 2>/dev/null; then
883 _t_s_a=$(echo "0t${1}=Y" | adb)
890 sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n"
895 if stat -c '%U
:%G
' "$1" 2>/dev/null; then
900 if stat -f '%Su
:%Sg
' "$1" 2>/dev/null; then
904 return 1 #error, 'stat
' not found
910 if [ -z "$keyfile" ]; then
911 _usage "Usage: _calcjwk keyfile"
915 if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then
916 _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE"
920 if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
922 pub_exp=$($OPENSSL_BIN rsa -in "$keyfile" -noout -text | grep "^publicExponent:" | cut -d '(' -f 2 | cut -d 'x
' -f 2 | cut -d ')' -f 1)
923 if [ "${#pub_exp}" = "5" ]; then
926 _debug3 pub_exp "$pub_exp"
928 e=$(echo "$pub_exp" | _h2b | _base64)
931 modulus=$($OPENSSL_BIN rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2)
932 _debug3 modulus "$modulus"
933 n="$(printf "%s" "$modulus" | _h2b | _base64 | _urlencode)"
934 jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
937 JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}'
938 JWK_HEADERPLACE_PART1='{"nonce": "'
939 JWK_HEADERPLACE_PART2='", "alg": "RS256", "jwk": '$jwk'}'
940 elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
942 crv="$($OPENSSL_BIN ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
945 if [ -z "$crv" ]; then
946 _debug "Let's try ASN1 OID
"
947 crv_oid="$
($OPENSSL_BIN ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep "^ASN1 OID:" | cut
-d ":" -f 2 |
tr -d " \r\n")"
948 _debug3 crv_oid "$crv_oid"
960 _err "ECC oid
: $crv_oid"
967 pubi="$
($OPENSSL_BIN ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep -n pub
: | cut
-d : -f 1)"
968 pubi=$(_math "$pubi" + 1)
971 pubj="$
($OPENSSL_BIN ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep -n "ASN1 OID:" | cut
-d : -f 1)"
972 pubj=$(_math "$pubj" - 1)
975 pubtext="$
($OPENSSL_BIN ec
-in "$keyfile" -noout -text 2>/dev
/null |
sed -n "$pubi,${pubj}p" |
tr -d " \n\r")"
976 _debug3 pubtext "$pubtext"
978 xlen="$
(printf "%s" "$pubtext" |
tr -d ':' |
wc -c)"
979 xlen=$(_math "$xlen" / 4)
982 xend=$(_math "$xlen" + 1)
983 x="$
(printf "%s" "$pubtext" | cut
-d : -f 2-"$xend")"
986 x64="$
(printf "%s" "$x" |
tr -d : | _h2b | _base64 | _urlencode
)"
989 xend=$(_math "$xend" + 1)
990 y="$
(printf "%s" "$pubtext" | cut
-d : -f "$xend"-10000)"
993 y64="$
(printf "%s" "$y" |
tr -d : | _h2b | _base64 | _urlencode
)"
996 jwk='{"crv
": "'$crv'", "kty
": "EC
", "x
": "'$x64'", "y
": "'$y64'"}'
999 JWK_HEADER='{"alg
": "ES256
", "jwk
": '$jwk'}'
1000 JWK_HEADERPLACE_PART1='{"nonce
": "'
1001 JWK_HEADERPLACE_PART2='", "alg
": "ES256
", "jwk
": '$jwk'}'
1003 _err "Only RSA or EC key is supported.
"
1007 _debug3 JWK_HEADER "$JWK_HEADER"
1008 __CACHED_JWK_KEY_FILE="$keyfile"
1016 if _exists mktemp; then
1017 if mktemp 2>/dev/null; then
1019 elif _contains "$
(mktemp
2>&1)" "-t prefix
" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then
1024 if [ -d "/tmp
" ]; then
1025 echo "/tmp
/${PROJECT_NAME}wefADf24sf.$
(_time
).tmp
"
1027 elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then
1028 echo "/$LE_TEMP_DIR/wefADf24sf.$
(_time
).tmp
"
1031 _err "Can not create temp
file.
"
1036 if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then
1037 HTTP_HEADER="$
(_mktemp
)"
1038 _debug2 HTTP_HEADER "$HTTP_HEADER"
1041 if [ "$__HTTP_INITIALIZED" ]; then
1042 if [ "$_ACME_CURL$_ACME_WGET" ]; then
1043 _debug2 "Http already initialized.
"
1048 if [ -z "$_ACME_CURL" ] && _exists "curl
"; then
1049 _ACME_CURL="curl
-L --silent --dump-header $HTTP_HEADER "
1050 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1051 _CURL_DUMP="$
(_mktemp
)"
1052 _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP "
1055 if [ "$CA_BUNDLE" ]; then
1056 _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE "
1061 if [ -z "$_ACME_WGET" ] && _exists "wget
"; then
1062 _ACME_WGET="wget
-q"
1063 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1064 _ACME_WGET="$_ACME_WGET -d "
1066 if [ "$CA_BUNDLE" ]; then
1067 _ACME_WGET="$_ACME_WGET --ca-certificate $CA_BUNDLE "
1071 __HTTP_INITIALIZED=1
1075 # body url [needbase64] [POST|PUT]
1082 if [ -z "$httpmethod" ]; then
1087 _debug2 "body
" "$body"
1091 if [ "$_ACME_CURL" ]; then
1093 if [ "$HTTPS_INSECURE" ]; then
1094 _CURL="$_CURL --insecure "
1096 _debug "_CURL
" "$_CURL"
1097 if [ "$needbase64" ]; then
1098 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url" | _base64
)"
1100 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url")"
1103 if [ "$_ret" != "0" ]; then
1104 _err "Please refer to https
://curl.haxx.se
/libcurl
/c
/libcurl-errors.html
for error code
: $_ret"
1105 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1106 _err "Here is the curl dump log
:"
1107 _err "$
(cat "$_CURL_DUMP")"
1110 elif [ "$_ACME_WGET" ]; then
1112 if [ "$HTTPS_INSECURE" ]; then
1113 _WGET="$_WGET --no-check-certificate "
1115 _debug "_WGET
" "$_WGET"
1116 if [ "$needbase64" ]; then
1117 if [ "$httpmethod" = "POST
" ]; then
1118 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
)"
1120 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
)"
1123 if [ "$httpmethod" = "POST
" ]; then
1124 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")"
1126 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")"
1130 if [ "$_ret" = "8" ]; then
1132 _debug "wget returns
8, the server returns a
'Bad request' respons
, lets process the response later.
"
1134 if [ "$_ret" != "0" ]; then
1135 _err "Please refer to https
://www.gnu.org
/software
/wget
/manual
/html_node
/Exit-Status.html
for error code
: $_ret"
1137 _sed_i "s
/^
*//g
" "$HTTP_HEADER"
1140 _err "Neither curl nor wget is found
, can not
do $httpmethod.
"
1142 _debug "_ret
" "$_ret"
1143 printf "%s
" "$response"
1147 # url getheader timeout
1154 _debug "timeout
" "$t"
1158 if [ "$_ACME_CURL" ]; then
1160 if [ "$HTTPS_INSECURE" ]; then
1161 _CURL="$_CURL --insecure "
1164 _CURL="$_CURL --connect-timeout $t"
1166 _debug "_CURL
" "$_CURL"
1167 if [ "$onlyheader" ]; then
1168 $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
1170 $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
1173 if [ "$ret" != "0" ]; then
1174 _err "Please refer to https
://curl.haxx.se
/libcurl
/c
/libcurl-errors.html
for error code
: $ret"
1175 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1176 _err "Here is the curl dump log
:"
1177 _err "$
(cat "$_CURL_DUMP")"
1180 elif [ "$_ACME_WGET" ]; then
1182 if [ "$HTTPS_INSECURE" ]; then
1183 _WGET="$_WGET --no-check-certificate "
1186 _WGET="$_WGET --timeout=$t"
1188 _debug "_WGET
" "$_WGET"
1189 if [ "$onlyheader" ]; then
1190 $_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'
1192 $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url"
1195 if [ "$_ret" = "8" ]; then
1197 _debug "wget returns
8, the server returns a
'Bad request' respons
, lets process the response later.
"
1199 if [ "$ret" != "0" ]; then
1200 _err "Please refer to https
://www.gnu.org
/software
/wget
/manual
/html_node
/Exit-Status.html
for error code
: $ret"
1204 _err "Neither curl nor wget is found
, can not
do GET.
"
1215 if ! tail -n "$1" 2>/dev/null; then
1221 # url payload needbase64 keyfile
1222 _send_signed_request() {
1227 if [ -z "$keyfile" ]; then
1228 keyfile="$ACCOUNT_KEY_PATH"
1231 _debug payload "$payload"
1233 if ! _calcjwk "$keyfile"; then
1237 payload64=$(printf "%s
" "$payload" | _base64 | _urlencode)
1238 _debug3 payload64 "$payload64"
1240 if [ -z "$_CACHED_NONCE" ]; then
1241 _debug2 "Get nonce.
"
1242 nonceurl="$API/directory
"
1243 _headers="$
(_get
"$nonceurl" "onlyheader")"
1245 if [ "$?
" != "0" ]; then
1246 _err "Can not connect to
$nonceurl to get nonce.
"
1250 _debug2 _headers "$_headers"
1252 _CACHED_NONCE="$
(echo "$_headers" |
grep "Replay-Nonce:" | _head_n
1 |
tr -d "\r\n " | cut
-d ':' -f 2)"
1253 _debug2 _CACHED_NONCE "$_CACHED_NONCE"
1255 _debug2 "Use _CACHED_NONCE
" "$_CACHED_NONCE"
1257 nonce="$_CACHED_NONCE"
1258 _debug2 nonce "$nonce"
1260 protected="$JWK_HEADERPLACE_PART1$nonce$JWK_HEADERPLACE_PART2"
1261 _debug3 protected "$protected"
1263 protected64="$
(printf "%s" "$protected" | _base64 | _urlencode
)"
1264 _debug3 protected64 "$protected64"
1266 if ! _sig_t="$
(printf "%s" "$protected64.$payload64" | _sign
"$keyfile" "sha256")"; then
1267 _err "Sign request failed.
"
1270 _debug3 _sig_t "$_sig_t"
1272 sig="$
(printf "%s" "$_sig_t" | _urlencode
)"
1275 body="{\"header
\": $JWK_HEADER, \"protected
\": \"$protected64\", \"payload
\": \"$payload64\", \"signature
\": \"$sig\"}"
1276 _debug3 body "$body"
1278 response="$
(_post
"$body" "$url" "$needbase64")"
1280 if [ "$?
" != "0" ]; then
1281 _err "Can not post to
$url"
1284 _debug2 original "$response"
1286 response="$
(echo "$response" | _normalizeJson
)"
1288 responseHeaders="$
(cat "$HTTP_HEADER")"
1290 _debug2 responseHeaders "$responseHeaders"
1291 _debug2 response "$response"
1292 code="$
(grep "^HTTP" "$HTTP_HEADER" | _tail_n
1 | cut
-d " " -f 2 |
tr -d "\r\n")"
1295 _CACHED_NONCE="$
(echo "$responseHeaders" |
grep "Replay-Nonce:" | _head_n
1 |
tr -d "\r\n " | cut
-d ':' -f 2)"
1299 #setopt "file" "opt
" "=" "value
" [";"]
1306 if [ -z "$__opt" ]; then
1307 _usage usage: _setopt '"file" "opt
" "=" "value
" [";"]'
1310 if [ ! -f "$__conf" ]; then
1314 if grep -n "^
$__opt$__sep" "$__conf" >/dev/null; then
1316 if _contains "$__val" "&"; then
1317 __val="$
(echo "$__val" |
sed 's/&/\\&/g')"
1319 text="$
(cat "$__conf")"
1320 echo "$text" | sed "s|^
$__opt$__sep.
*$|
$__opt$__sep$__val$__end|
" >"$__conf"
1322 elif grep -n "^
#$__opt$__sep" "$__conf" >/dev/null; then
1323 if _contains
"$__val" "&"; then
1324 __val
="$(echo "$__val" | sed 's/&/\\&/g')"
1326 text
="$(cat "$__conf")"
1327 echo "$text" |
sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
1331 echo "$__opt$__sep$__val$__end" >>"$__conf"
1333 _debug2
"$(grep -n "^
$__opt$__sep" "$__conf")"
1336 #_save_conf file key value
1342 if [ "$_s_c_f" ]; then
1343 _setopt
"$_s_c_f" "$_sdkey" "=" "'$_sdvalue'"
1345 _err
"config file is empty, can not save $_sdkey=$_sdvalue"
1349 #_clear_conf file key
1353 if [ "$_c_c_f" ]; then
1354 _conf_data
="$(cat "$_c_c_f")"
1355 echo "$_conf_data" |
sed "s/^$_sdkey *=.*$//" >"$_c_c_f"
1357 _err
"config file is empty, can not clear"
1361 #_read_conf file key
1365 if [ -f "$_r_c_f" ]; then
1367 eval "$(grep "^
$_sdkey *=" "$_r_c_f")"
1368 eval "printf \"%s\" \"\$$_sdkey\""
1371 _debug
"config file is empty, can not read $_sdkey"
1375 #_savedomainconf key value
1376 #save to domain.conf
1378 _save_conf
"$DOMAIN_CONF" "$1" "$2"
1381 #_cleardomainconf key
1382 _cleardomainconf
() {
1383 _clear_conf
"$DOMAIN_CONF" "$1"
1386 #_readdomainconf key
1388 _read_conf
"$DOMAIN_CONF" "$1"
1391 #_saveaccountconf key value
1392 _saveaccountconf
() {
1393 _save_conf
"$ACCOUNT_CONF_PATH" "$1" "$2"
1396 #_clearaccountconf key
1397 _clearaccountconf
() {
1398 _clear_conf
"$ACCOUNT_CONF_PATH" "$1"
1401 #_savecaconf key value
1403 _save_conf
"$CA_CONF" "$1" "$2"
1408 _read_conf
"$CA_CONF" "$1"
1411 #_clearaccountconf key
1413 _clear_conf
"$CA_CONF" "$1"
1416 # content localaddress
1420 _debug
"ncaddr" "$ncaddr"
1422 _debug
"startserver: $$"
1423 nchelp
="$(nc -h 2>&1)"
1425 _debug Le_HTTPPort
"$Le_HTTPPort"
1426 _debug Le_Listen_V4
"$Le_Listen_V4"
1427 _debug Le_Listen_V6
"$Le_Listen_V6"
1430 if [ "$Le_Listen_V4" ]; then
1432 elif [ "$Le_Listen_V6" ]; then
1436 if echo "$nchelp" |
grep "\-q[ ,]" >/dev
/null
; then
1437 _NC
="$_NC -q 1 -l $ncaddr"
1439 if echo "$nchelp" |
grep "GNU netcat" >/dev
/null
&& echo "$nchelp" |
grep "\-c, \-\-close" >/dev
/null
; then
1440 _NC
="$_NC -c -l $ncaddr"
1441 elif echo "$nchelp" |
grep "\-N" |
grep "Shutdown the network socket after EOF on stdin" >/dev
/null
; then
1442 _NC
="$_NC -N -l $ncaddr"
1444 _NC
="$_NC -l $ncaddr"
1451 if _contains
"$nchelp" "nmap.org"; then
1452 _debug
"Using ncat: nmap.org"
1453 if ! _exec
"printf \"%s\r\n\r\n%s\" \"HTTP/1.1 200 OK\" \"$content\" | $_NC \"$Le_HTTPPort\" >&2"; then
1457 if [ "$DEBUG" ]; then
1464 if ! _exec
"printf \"%s\r\n\r\n%s\" \"HTTP/1.1 200 OK\" \"$content\" | $_NC -p \"$Le_HTTPPort\" >&2"; then
1465 _exec
"printf \"%s\r\n\r\n%s\" \"HTTP/1.1 200 OK\" \"$content\" | $_NC \"$Le_HTTPPort\" >&2"
1468 if [ "$?" != "0" ]; then
1469 _err
"nc listen error."
1473 if [ "$DEBUG" ]; then
1482 if [ -z "$pid" ]; then
1486 _debug2
"Le_HTTPPort" "$Le_HTTPPort"
1487 if [ "$Le_HTTPPort" ]; then
1488 if [ "$DEBUG" ] && [ "$DEBUG" -gt "3" ]; then
1489 _get
"http://localhost:$Le_HTTPPort" "" 1
1491 _get
"http://localhost:$Le_HTTPPort" "" 1 >/dev
/null
2>&1
1495 _debug2
"Le_TLSPort" "$Le_TLSPort"
1496 if [ "$Le_TLSPort" ]; then
1497 if [ "$DEBUG" ] && [ "$DEBUG" -gt "3" ]; then
1498 _get
"https://localhost:$Le_TLSPort" "" 1
1499 _get
"https://localhost:$Le_TLSPort" "" 1
1501 _get
"https://localhost:$Le_TLSPort" "" 1 >/dev
/null
2>&1
1502 _get
"https://localhost:$Le_TLSPort" "" 1 >/dev
/null
2>&1
1510 if [ "$__INTERACTIVE" ]; then
1511 _sleep_c
="$_sleep_sec"
1512 while [ "$_sleep_c" -ge "0" ]; do
1515 _sleep_c
="$(_math "$_sleep_c" - 1)"
1524 # _starttlsserver san_a san_b port content _ncaddr
1526 _info
"Starting tls server."
1533 _debug san_a
"$san_a"
1534 _debug san_b
"$san_b"
1538 if ! _createkey
"2048" "$TLS_KEY"; then
1539 _err
"Create tls validation key error."
1545 if [ "$san_b" ]; then
1548 if ! _createcsr
"tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF"; then
1549 _err
"Create tls validation csr error."
1554 if ! _signcsr
"$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then
1555 _err
"Create tls validation cert error."
1559 __S_OPENSSL
="$OPENSSL_BIN s_server -cert $TLS_CERT -key $TLS_KEY "
1560 if [ "$opaddr" ]; then
1561 __S_OPENSSL
="$__S_OPENSSL -accept $opaddr:$port"
1563 __S_OPENSSL
="$__S_OPENSSL -accept $port"
1566 _debug Le_Listen_V4
"$Le_Listen_V4"
1567 _debug Le_Listen_V6
"$Le_Listen_V6"
1568 if [ "$Le_Listen_V4" ]; then
1569 __S_OPENSSL
="$__S_OPENSSL -4"
1570 elif [ "$Le_Listen_V6" ]; then
1571 __S_OPENSSL
="$__S_OPENSSL -6"
1574 _debug
"$__S_OPENSSL"
1575 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1576 (printf "%s\r\n\r\n%s" "HTTP/1.1 200 OK" "$content" |
$__S_OPENSSL -tlsextdebug) &
1578 (printf "%s\r\n\r\n%s" "HTTP/1.1 200 OK" "$content" |
$__S_OPENSSL >/dev
/null
2>&1) &
1583 _debug serverproc
"$serverproc"
1589 if ! readlink
-f "$_rf" 2>/dev
/null
; then
1590 if _startswith
"$_rf" "\./$PROJECT_ENTRY"; then
1591 printf -- "%s" "$(pwd)/$PROJECT_ENTRY"
1599 if [ -z "$_SCRIPT_HOME" ]; then
1600 if _exists readlink
&& _exists
dirname; then
1601 _debug
"Lets find script dir."
1602 _debug
"_SCRIPT_" "$_SCRIPT_"
1603 _script
="$(_readlink "$_SCRIPT_")"
1604 _debug
"_script" "$_script"
1605 _script_home
="$(dirname "$_script")"
1606 _debug
"_script_home" "$_script_home"
1607 if [ -d "$_script_home" ]; then
1608 _SCRIPT_HOME
="$_script_home"
1610 _err
"It seems the script home is not correct:$_script_home"
1615 # if [ -z "$LE_WORKING_DIR" ]; then
1616 # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then
1617 # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME"
1618 # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
1620 # LE_WORKING_DIR="$_SCRIPT_HOME"
1624 if [ -z "$LE_WORKING_DIR" ]; then
1625 _debug
"Using default home:$DEFAULT_INSTALL_HOME"
1626 LE_WORKING_DIR
="$DEFAULT_INSTALL_HOME"
1628 export LE_WORKING_DIR
1630 _DEFAULT_ACCOUNT_CONF_PATH
="$LE_WORKING_DIR/account.conf"
1632 if [ -z "$ACCOUNT_CONF_PATH" ]; then
1633 if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
1634 .
"$_DEFAULT_ACCOUNT_CONF_PATH"
1638 if [ -z "$ACCOUNT_CONF_PATH" ]; then
1639 ACCOUNT_CONF_PATH
="$_DEFAULT_ACCOUNT_CONF_PATH"
1642 DEFAULT_LOG_FILE
="$LE_WORKING_DIR/$PROJECT_NAME.log"
1644 DEFAULT_CA_HOME
="$LE_WORKING_DIR/ca"
1646 if [ -z "$LE_TEMP_DIR" ]; then
1647 LE_TEMP_DIR
="$LE_WORKING_DIR/tmp"
1651 #[domain] [keylength]
1656 if [ -f "$ACCOUNT_CONF_PATH" ]; then
1657 .
"$ACCOUNT_CONF_PATH"
1660 if [ "$IN_CRON" ]; then
1661 if [ ! "$_USER_PATH_EXPORTED" ]; then
1662 _USER_PATH_EXPORTED
=1
1663 export PATH
="$USER_PATH:$PATH"
1667 if [ -z "$CA_HOME" ]; then
1668 CA_HOME
="$DEFAULT_CA_HOME"
1671 if [ -z "$API" ]; then
1672 if [ -z "$STAGE" ]; then
1676 _info
"Using stage api:$API"
1680 _API_HOST
="$(echo "$API" | cut -d : -f 2 | tr -d '/')"
1681 CA_DIR
="$CA_HOME/$_API_HOST"
1683 _DEFAULT_CA_CONF
="$CA_DIR/ca.conf"
1685 if [ -z "$CA_CONF" ]; then
1686 CA_CONF
="$_DEFAULT_CA_CONF"
1689 if [ -f "$CA_CONF" ]; then
1693 if [ -z "$ACME_DIR" ]; then
1694 ACME_DIR
="/home/.acme"
1697 if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
1698 APACHE_CONF_BACKUP_DIR
="$LE_WORKING_DIR"
1701 if [ -z "$USER_AGENT" ]; then
1702 USER_AGENT
="$DEFAULT_USER_AGENT"
1705 if [ -z "$HTTP_HEADER" ]; then
1706 HTTP_HEADER
="$LE_WORKING_DIR/http.header"
1709 _OLD_ACCOUNT_KEY
="$LE_WORKING_DIR/account.key"
1710 _OLD_ACCOUNT_JSON
="$LE_WORKING_DIR/account.json"
1712 _DEFAULT_ACCOUNT_KEY_PATH
="$CA_DIR/account.key"
1713 _DEFAULT_ACCOUNT_JSON_PATH
="$CA_DIR/account.json"
1714 if [ -z "$ACCOUNT_KEY_PATH" ]; then
1715 ACCOUNT_KEY_PATH
="$_DEFAULT_ACCOUNT_KEY_PATH"
1718 if [ -z "$ACCOUNT_JSON_PATH" ]; then
1719 ACCOUNT_JSON_PATH
="$_DEFAULT_ACCOUNT_JSON_PATH"
1722 _DEFAULT_CERT_HOME
="$LE_WORKING_DIR"
1723 if [ -z "$CERT_HOME" ]; then
1724 CERT_HOME
="$_DEFAULT_CERT_HOME"
1727 if [ -z "$OPENSSL_BIN" ]; then
1728 OPENSSL_BIN
="$DEFAULT_OPENSSL_BIN"
1731 if [ -z "$1" ]; then
1738 if [ -z "$DOMAIN_PATH" ]; then
1739 domainhome
="$CERT_HOME/$domain"
1740 domainhomeecc
="$CERT_HOME/$domain$ECC_SUFFIX"
1742 DOMAIN_PATH
="$domainhome"
1744 if _isEccKey
"$_ilength"; then
1745 DOMAIN_PATH
="$domainhomeecc"
1747 if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
1748 _info
"The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert."
1751 _debug DOMAIN_PATH
"$DOMAIN_PATH"
1754 if [ -z "$DOMAIN_CONF" ]; then
1755 DOMAIN_CONF
="$DOMAIN_PATH/$domain.conf"
1758 if [ -z "$DOMAIN_SSL_CONF" ]; then
1759 DOMAIN_SSL_CONF
="$DOMAIN_PATH/$domain.csr.conf"
1762 if [ -z "$CSR_PATH" ]; then
1763 CSR_PATH
="$DOMAIN_PATH/$domain.csr"
1765 if [ -z "$CERT_KEY_PATH" ]; then
1766 CERT_KEY_PATH
="$DOMAIN_PATH/$domain.key"
1768 if [ -z "$CERT_PATH" ]; then
1769 CERT_PATH
="$DOMAIN_PATH/$domain.cer"
1771 if [ -z "$CA_CERT_PATH" ]; then
1772 CA_CERT_PATH
="$DOMAIN_PATH/ca.cer"
1774 if [ -z "$CERT_FULLCHAIN_PATH" ]; then
1775 CERT_FULLCHAIN_PATH
="$DOMAIN_PATH/fullchain.cer"
1777 if [ -z "$CERT_PFX_PATH" ]; then
1778 CERT_PFX_PATH
="$DOMAIN_PATH/$domain.pfx"
1781 if [ -z "$TLS_CONF" ]; then
1782 TLS_CONF
="$DOMAIN_PATH/tls.valdation.conf"
1784 if [ -z "$TLS_CERT" ]; then
1785 TLS_CERT
="$DOMAIN_PATH/tls.valdation.cert"
1787 if [ -z "$TLS_KEY" ]; then
1788 TLS_KEY
="$DOMAIN_PATH/tls.valdation.key"
1790 if [ -z "$TLS_CSR" ]; then
1791 TLS_CSR
="$DOMAIN_PATH/tls.valdation.csr"
1797 if [ -z "$_EXEC_TEMP_ERR" ]; then
1798 _EXEC_TEMP_ERR
="$(_mktemp)"
1801 if [ "$_EXEC_TEMP_ERR" ]; then
1802 eval "$@ 2>>$_EXEC_TEMP_ERR"
1809 [ "$_EXEC_TEMP_ERR" ] && _err
"$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR"
1813 _APACHECTL
="apachectl"
1814 if ! _exists apachectl
; then
1815 if _exists apache2ctl
; then
1816 _APACHECTL
="apache2ctl"
1818 _err
"'apachectl not found. It seems that apache is not installed, or you are not root user.'"
1819 _err
"Please use webroot mode to try again."
1824 if ! _exec
$_APACHECTL -V >/dev
/null
; then
1829 if [ "$APACHE_HTTPD_CONF" ]; then
1830 _saveaccountconf APACHE_HTTPD_CONF
"$APACHE_HTTPD_CONF"
1831 httpdconf
="$APACHE_HTTPD_CONF"
1832 httpdconfname
="$(basename "$httpdconfname")"
1834 httpdconfname
="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
1835 _debug httpdconfname "$httpdconfname"
1837 if [ -z "$httpdconfname" ]; then
1838 _err "Can not read apache config file."
1842 if _startswith "$httpdconfname" '/'; then
1843 httpdconf="$httpdconfname"
1844 httpdconfname="$(basename "$httpdconfname")"
1846 httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
1847 _debug httpdroot
"$httpdroot"
1848 httpdconf
="$httpdroot/$httpdconfname"
1849 httpdconfname
="$(basename "$httpdconfname")"
1852 _debug httpdconf
"$httpdconf"
1853 _debug httpdconfname
"$httpdconfname"
1854 if [ ! -f "$httpdconf" ]; then
1855 _err
"Apache Config file not found" "$httpdconf"
1862 if [ -z "$usingApache" ]; then
1866 if ! _apachePath
; then
1870 if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
1871 _debug
"No config file to restore."
1875 cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
1876 _debug
"Restored: $httpdconf."
1877 if ! _exec
$_APACHECTL -t; then
1879 _err
"Sorry, restore apache config error, please contact me."
1882 _debug
"Restored successfully."
1883 rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
1889 if ! _apachePath
; then
1893 #test the conf first
1894 _info
"Checking if there is an error in the apache config file before starting."
1896 if ! _exec
"$_APACHECTL" -t >/dev
/null
; then
1898 _err
"The apache config file has error, please fix it first, then try again."
1899 _err
"Don't worry, there is nothing changed to your system."
1906 _debug
"Backup apache config file" "$httpdconf"
1907 if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
1908 _err
"Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
1909 _err
"This might be a bug of $PROJECT_NAME , pleae report issue: $PROJECT"
1912 _info
"JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
1913 _info
"In case there is an error that can not be restored automatically, you may try restore it yourself."
1914 _info
"The backup file will be deleted on sucess, just forget it."
1918 apacheVer
="$($_APACHECTL -V | grep "Server version
:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
1919 _debug
"apacheVer" "$apacheVer"
1920 apacheMajer
="$(echo "$apacheVer" | cut -d . -f 1)"
1921 apacheMinor
="$(echo "$apacheVer" | cut -d . -f 2)"
1923 if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ]; then
1925 Alias /.well-known/acme-challenge $ACME_DIR
1927 <Directory $ACME_DIR >
1933 Alias /.well-known/acme-challenge $ACME_DIR
1935 <Directory $ACME_DIR >
1942 _msg
="$($_APACHECTL -t 2>&1)"
1943 if [ "$?" != "0" ]; then
1944 _err
"Sorry, apache config error"
1945 if _restoreApache
; then
1946 _err
"The apache config file is restored."
1948 _err
"Sorry, The apache config file can not be restored, please report bug."
1953 if [ ! -d "$ACME_DIR" ]; then
1954 mkdir
-p "$ACME_DIR"
1955 chmod 755 "$ACME_DIR"
1958 if ! _exec
"$_APACHECTL" graceful
; then
1960 _err
"$_APACHECTL graceful error, please contact me."
1969 _stopserver
"$serverproc"
1973 if [ -z "$DEBUG" ]; then
1982 _debug
"_clearupdns"
1983 if [ "$dnsadded" != 1 ] ||
[ -z "$vlist" ]; then
1984 _debug
"Dns not added, skip."
1988 ventries
=$
(echo "$vlist" |
tr ',' ' ')
1989 for ventry
in $ventries; do
1990 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
1991 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
1992 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
1993 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
1995 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
1996 _info
"$d is already verified, skip $vtype."
2000 if [ "$vtype" != "$VTYPE_DNS" ]; then
2001 _info
"Skip $d for $vtype"
2005 d_api
="$(_findHook "$d" dnsapi "$_currentRoot")"
2006 _debug d_api
"$d_api"
2008 if [ -z "$d_api" ]; then
2009 _info
"Not Found domain api file: $d_api"
2014 if ! .
"$d_api"; then
2015 _err
"Load file $d_api error. Please check your api file and try again."
2019 rmcommand
="${_currentRoot}_rm"
2020 if ! _exists
"$rmcommand"; then
2021 _err
"It seems that your api file doesn't define $rmcommand"
2025 txtdomain
="_acme-challenge.$d"
2027 if ! $rmcommand "$txtdomain"; then
2028 _err
"Error removing txt for domain:$txtdomain"
2036 # webroot removelevel tokenfile
2037 _clearupwebbroot
() {
2039 if [ -z "$__webroot" ]; then
2040 _debug
"no webroot specified, skip"
2045 if [ "$2" = '1' ]; then
2046 _rmpath
="$__webroot/.well-known"
2047 elif [ "$2" = '2' ]; then
2048 _rmpath
="$__webroot/.well-known/acme-challenge"
2049 elif [ "$2" = '3' ]; then
2050 _rmpath
="$__webroot/.well-known/acme-challenge/$3"
2052 _debug
"Skip for removelevel:$2"
2055 if [ "$_rmpath" ]; then
2056 if [ "$DEBUG" ]; then
2057 _debug
"Debugging, skip removing: $_rmpath"
2067 _on_before_issue
() {
2068 _debug _on_before_issue
2069 if _hasfield
"$Le_Webroot" "$NO_VALUE"; then
2070 if ! _exists
"nc"; then
2071 _err
"Please install netcat(nc) tools first."
2076 _debug Le_LocalAddress
"$Le_LocalAddress"
2078 alldomains
=$
(echo "$Le_Domain,$Le_Alt" |
tr ',' ' ')
2082 for d
in $alldomains; do
2083 _debug
"Check for domain" "$d"
2084 _currentRoot
="$(_getfield "$Le_Webroot" $_index)"
2085 _debug
"_currentRoot" "$_currentRoot"
2086 _index
=$
(_math
$_index + 1)
2088 if [ "$_currentRoot" = "$NO_VALUE" ]; then
2089 _info
"Standalone mode."
2090 if [ -z "$Le_HTTPPort" ]; then
2093 _savedomainconf
"Le_HTTPPort" "$Le_HTTPPort"
2095 _checkport
="$Le_HTTPPort"
2096 elif [ "$_currentRoot" = "$W_TLS" ]; then
2097 _info
"Standalone tls mode."
2098 if [ -z "$Le_TLSPort" ]; then
2101 _savedomainconf
"Le_TLSPort" "$Le_TLSPort"
2103 _checkport
="$Le_TLSPort"
2106 if [ "$_checkport" ]; then
2107 _debug _checkport
"$_checkport"
2108 _checkaddr
="$(_getfield "$Le_LocalAddress" $_addrIndex)"
2109 _debug _checkaddr
"$_checkaddr"
2111 _addrIndex
="$(_math $_addrIndex + 1)"
2113 _netprc
="$(_ss "$_checkport" | grep "$_checkport")"
2114 netprc
="$(echo "$_netprc" | grep "$_checkaddr")"
2115 if [ -z "$netprc" ]; then
2116 netprc
="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
2118 if [ "$netprc" ]; then
2120 _err
"tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
2121 _err
"Please stop it first"
2127 if _hasfield
"$Le_Webroot" "apache"; then
2128 if ! _setApache
; then
2129 _err
"set up apache error. Report error to me."
2137 if [ "$Le_PreHook" ]; then
2138 _info
"Run pre hook:'$Le_PreHook'"
2140 cd "$DOMAIN_PATH" && eval "$Le_PreHook"
2142 _err
"Error when run pre hook."
2149 _debug _on_issue_err
2150 if [ "$LOG_FILE" ]; then
2151 _err
"Please check log file for more details: $LOG_FILE"
2153 _err
"Please add '--debug' or '--log' to check more details."
2154 _err
"See: $_DEBUG_WIKI"
2157 if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
2158 _debug
"$(_dlg_versions)"
2162 if [ "$Le_PostHook" ]; then
2163 _info
"Run post hook:'$Le_PostHook'"
2165 cd "$DOMAIN_PATH" && eval "$Le_PostHook"
2167 _err
"Error when run post hook."
2173 _on_issue_success
() {
2174 _debug _on_issue_success
2176 if [ "$Le_PostHook" ]; then
2177 _info
"Run post hook:'$Le_PostHook'"
2179 cd "$DOMAIN_PATH" && eval "$Le_PostHook"
2181 _err
"Error when run post hook."
2187 if [ "$IS_RENEW" ] && [ "$Le_RenewHook" ]; then
2188 _info
"Run renew hook:'$Le_RenewHook'"
2190 cd "$DOMAIN_PATH" && eval "$Le_RenewHook"
2192 _err
"Error when run renew hook."
2207 _regAccount
"$_reg_length"
2210 __calcAccountKeyHash
() {
2211 [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256
<"$ACCOUNT_KEY_PATH"
2219 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
2220 _info
"mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
2221 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
2224 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
2225 _info
"mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
2226 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
2229 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
2230 if ! _create_account_key
"$_reg_length"; then
2231 _err
"Create account key error."
2236 if ! _calcjwk
"$ACCOUNT_KEY_PATH"; then
2243 _debug AGREEMENT
"$AGREEMENT"
2245 regjson
='{"resource": "'$_reg_res'", "agreement": "'$AGREEMENT'"}'
2247 if [ "$ACCOUNT_EMAIL" ]; then
2248 regjson
='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
2251 if [ -z "$_updateTos" ]; then
2252 _info
"Registering account"
2254 if ! _send_signed_request
"$API/acme/new-reg" "$regjson"; then
2255 _err
"Register account Error: $response"
2259 if [ "$code" = "" ] ||
[ "$code" = '201' ]; then
2260 echo "$response" >"$ACCOUNT_JSON_PATH"
2262 elif [ "$code" = '409' ]; then
2263 _info
"Already registered"
2265 _err
"Register account Error: $response"
2269 _accUri
="$(echo "$responseHeaders" | grep "^Location
:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
2270 _debug
"_accUri" "$_accUri"
2272 _tos
="$(echo "$responseHeaders" | grep "^Link
:.
*rel
=\"terms-of-service
\"" | _head_n 1 | _egrep_o "<.
*>" | tr -d '<>')"
2273 _debug
"_tos" "$_tos"
2274 if [ -z "$_tos" ]; then
2275 _debug
"Use default tos: $DEFAULT_AGREEMENT"
2276 _tos
="$DEFAULT_AGREEMENT"
2278 if [ "$_tos" != "$AGREEMENT" ]; then
2286 _debug
"Update tos: $_tos"
2287 if ! _send_signed_request
"$_accUri" "$regjson"; then
2288 _err
"Update tos error."
2291 if [ "$code" = '202' ]; then
2292 _info
"Update success."
2294 CA_KEY_HASH
="$(__calcAccountKeyHash)"
2295 _debug
"Calc CA_KEY_HASH" "$CA_KEY_HASH"
2296 _savecaconf CA_KEY_HASH
"$CA_KEY_HASH"
2298 _err
"Update account error."
2307 # domain folder file
2313 if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
2314 d_api
="$_SCRIPT_HOME/$_hookcat/$_hookname"
2315 elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
2316 d_api
="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
2317 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
2318 d_api
="$LE_WORKING_DIR/$_hookdomain/$_hookname"
2319 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
2320 d_api
="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
2321 elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
2322 d_api
="$LE_WORKING_DIR/$_hookname"
2323 elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
2324 d_api
="$LE_WORKING_DIR/$_hookname.sh"
2325 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
2326 d_api
="$LE_WORKING_DIR/$_hookcat/$_hookname"
2327 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
2328 d_api
="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
2331 printf "%s" "$d_api"
2335 __get_domain_new_authz
() {
2337 _info
"Getting new-authz for domain" "$_gdnd"
2339 _Max_new_authz_retry_times
=5
2341 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
2342 _debug
"Try new-authz for the $_authz_i time."
2343 if ! _send_signed_request
"$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
2344 _err
"Can not get domain new authz."
2347 if ! _contains
"$response" "An error occurred while processing your request"; then
2348 _info
"The new-authz request is ok."
2351 _authz_i
="$(_math "$_authz_i" + 1)"
2352 _info
"The server is busy, Sleep $_authz_i to retry."
2356 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
2357 _err
"new-authz retry reach the max $_Max_new_authz_retry_times times."
2360 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
2361 _err
"new-authz error: $response"
2367 #webroot, domain domainlist keylength
2369 if [ -z "$2" ]; then
2370 _usage
"Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
2377 Le_RealCertPath
="$5"
2379 Le_RealCACertPath
="$7"
2381 Le_RealFullChainPath
="$9"
2384 Le_RenewHook
="${12}"
2385 Le_LocalAddress
="${13}"
2387 #remove these later.
2388 if [ "$Le_Webroot" = "dns-cf" ]; then
2391 if [ "$Le_Webroot" = "dns-dp" ]; then
2394 if [ "$Le_Webroot" = "dns-cx" ]; then
2397 _debug
"Using api: $API"
2399 if [ ! "$IS_RENEW" ]; then
2400 _initpath
"$Le_Domain" "$Le_Keylength"
2401 mkdir
-p "$DOMAIN_PATH"
2404 if [ -f "$DOMAIN_CONF" ]; then
2405 Le_NextRenewTime
=$
(_readdomainconf Le_NextRenewTime
)
2406 _debug Le_NextRenewTime
"$Le_NextRenewTime"
2407 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
2408 _saved_domain
=$
(_readdomainconf Le_Domain
)
2409 _debug _saved_domain
"$_saved_domain"
2410 _saved_alt
=$
(_readdomainconf Le_Alt
)
2411 _debug _saved_alt
"$_saved_alt"
2412 if [ "$_saved_domain,$_saved_alt" = "$Le_Domain,$Le_Alt" ]; then
2413 _info
"Domains not changed."
2414 _info
"Skip, Next renewal time is: $(__green "$
(_readdomainconf Le_NextRenewTimeStr
)")"
2415 _info
"Add '$(__red '--force')' to force to renew."
2418 _info
"Domains have changed."
2423 _savedomainconf
"Le_Domain" "$Le_Domain"
2424 _savedomainconf
"Le_Alt" "$Le_Alt"
2425 _savedomainconf
"Le_Webroot" "$Le_Webroot"
2427 _savedomainconf
"Le_PreHook" "$Le_PreHook"
2428 _savedomainconf
"Le_PostHook" "$Le_PostHook"
2429 _savedomainconf
"Le_RenewHook" "$Le_RenewHook"
2431 if [ "$Le_LocalAddress" ]; then
2432 _savedomainconf
"Le_LocalAddress" "$Le_LocalAddress"
2434 _cleardomainconf
"Le_LocalAddress"
2438 _savedomainconf
"Le_API" "$Le_API"
2440 if [ "$Le_Alt" = "$NO_VALUE" ]; then
2444 if [ "$Le_Keylength" = "$NO_VALUE" ]; then
2448 if ! _on_before_issue
; then
2449 _err
"_on_before_issue."
2453 _saved_account_key_hash
="$(_readcaconf "CA_KEY_HASH
")"
2454 _debug2 _saved_account_key_hash
"$_saved_account_key_hash"
2456 if [ -z "$_saved_account_key_hash" ] ||
[ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
2457 if ! _regAccount
"$_accountkeylength"; then
2462 _debug
"_saved_account_key_hash is not changed, skip register account."
2465 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
2466 _info
"Signing from existing CSR."
2468 _key
=$
(_readdomainconf Le_Keylength
)
2469 _debug
"Read key length:$_key"
2470 if [ ! -f "$CERT_KEY_PATH" ] ||
[ "$Le_Keylength" != "$_key" ]; then
2471 if ! createDomainKey
"$Le_Domain" "$Le_Keylength"; then
2472 _err
"Create domain key error."
2479 if ! _createcsr
"$Le_Domain" "$Le_Alt" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
2480 _err
"Create CSR error."
2487 _savedomainconf
"Le_Keylength" "$Le_Keylength"
2491 _info
"Getting domain auth token for each domain"
2493 if [ -z "$vlist" ]; then
2494 alldomains
=$
(echo "$Le_Domain,$Le_Alt" |
tr ',' ' ')
2497 for d
in $alldomains; do
2498 _info
"Getting webroot for domain" "$d"
2499 _w
="$(echo $Le_Webroot | cut -d , -f $_index)"
2504 _debug
"_currentRoot" "$_currentRoot"
2505 _index
=$
(_math
$_index + 1)
2508 if _startswith
"$_currentRoot" "dns"; then
2512 if [ "$_currentRoot" = "$W_TLS" ]; then
2516 if ! __get_domain_new_authz
"$d"; then
2522 if [ -z "$thumbprint" ]; then
2523 accountkey_json
=$
(printf "%s" "$jwk" |
tr -d ' ')
2524 thumbprint
=$
(printf "%s" "$accountkey_json" | _digest
"sha256" | _urlencode
)
2527 entry
="$(printf "%s
\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
2528 _debug entry
"$entry"
2529 if [ -z "$entry" ]; then
2530 _err
"Error, can not get domain token $d"
2535 token
="$(printf "%s
\n" "$entry" | _egrep_o '"token
":"[^
"]*' | cut -d : -f 2 | tr -d '"')"
2536 _debug token "$token"
2538 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
2541 keyauthorization
="$token.$thumbprint"
2542 _debug keyauthorization
"$keyauthorization"
2544 if printf "%s" "$response" |
grep '"status":"valid"' >/dev
/null
2>&1; then
2545 _info
"$d is already verified, skip."
2546 keyauthorization
="$STATE_VERIFIED"
2547 _debug keyauthorization
"$keyauthorization"
2550 dvlist
="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
2551 _debug dvlist
"$dvlist"
2553 vlist
="$vlist$dvlist,"
2559 ventries
=$
(echo "$vlist" |
tr ',' ' ')
2560 for ventry
in $ventries; do
2561 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
2562 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
2563 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
2564 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
2566 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
2567 _info
"$d is already verified, skip $vtype."
2571 if [ "$vtype" = "$VTYPE_DNS" ]; then
2573 txtdomain
="_acme-challenge.$d"
2574 _debug txtdomain
"$txtdomain"
2575 txt
="$(printf "%s
" "$keyauthorization" | _digest "sha256
" | _urlencode)"
2578 d_api
="$(_findHook "$d" dnsapi "$_currentRoot")"
2580 _debug d_api
"$d_api"
2582 if [ "$d_api" ]; then
2583 _info
"Found domain api file: $d_api"
2585 _err
"Add the following TXT record:"
2586 _err
"Domain: '$(__green "$txtdomain")'"
2587 _err
"TXT value: '$(__green "$txt")'"
2588 _err
"Please be aware that you prepend _acme-challenge. before your domain"
2589 _err
"so the resulting subdomain will be: $txtdomain"
2594 if ! .
"$d_api"; then
2595 _err
"Load file $d_api error. Please check your api file and try again."
2599 addcommand
="${_currentRoot}_add"
2600 if ! _exists
"$addcommand"; then
2601 _err
"It seems that your api file is not correct, it must have a function named: $addcommand"
2605 if ! $addcommand "$txtdomain" "$txt"; then
2606 _err
"Error add txt for domain:$txtdomain"
2611 if [ "$?" != "0" ]; then
2620 if [ "$dnsadded" = '0' ]; then
2621 _savedomainconf
"Le_Vlist" "$vlist"
2622 _debug
"Dns record not added yet, so, save to $DOMAIN_CONF and exit."
2623 _err
"Please add the TXT records to the domains, and retry again."
2631 if [ "$dnsadded" = '1' ]; then
2632 if [ -z "$Le_DNSSleep" ]; then
2633 Le_DNSSleep
="$DEFAULT_DNS_SLEEP"
2635 _savedomainconf
"Le_DNSSleep" "$Le_DNSSleep"
2638 _info
"Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
2639 _sleep
"$Le_DNSSleep"
2642 _debug
"ok, let's start to verify"
2645 ventries
=$
(echo "$vlist" |
tr ',' ' ')
2646 for ventry
in $ventries; do
2647 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
2648 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
2649 uri
=$
(echo "$ventry" | cut
-d "$sep" -f 3)
2650 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
2651 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
2653 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
2654 _info
"$d is already verified, skip $vtype."
2658 _info
"Verifying:$d"
2660 _debug
"keyauthorization" "$keyauthorization"
2663 token
="$(printf "%s
" "$keyauthorization" | cut -d '.' -f 1)"
2665 _debug
"_currentRoot" "$_currentRoot"
2667 if [ "$vtype" = "$VTYPE_HTTP" ]; then
2668 if [ "$_currentRoot" = "$NO_VALUE" ]; then
2669 _info
"Standalone mode server"
2670 _ncaddr
="$(_getfield "$Le_LocalAddress" "$_ncIndex")"
2671 _ncIndex
="$(_math $_ncIndex + 1)"
2672 _startserver
"$keyauthorization" "$_ncaddr" &
2673 if [ "$?" != "0" ]; then
2680 _debug serverproc
"$serverproc"
2683 if [ "$_currentRoot" = "apache" ]; then
2684 wellknown_path
="$ACME_DIR"
2686 wellknown_path
="$_currentRoot/.well-known/acme-challenge"
2687 if [ ! -d "$_currentRoot/.well-known" ]; then
2689 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
2696 _debug wellknown_path
"$wellknown_path"
2698 _debug
"writing token:$token to $wellknown_path/$token"
2700 mkdir
-p "$wellknown_path"
2702 if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
2703 _err
"$d:Can not write token to file : $wellknown_path/$token"
2704 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
2710 if [ ! "$usingApache" ]; then
2711 if webroot_owner
=$
(_stat
"$_currentRoot"); then
2712 _debug
"Changing owner/group of .well-known to $webroot_owner"
2713 chown
-R "$webroot_owner" "$_currentRoot/.well-known"
2715 _debug
"not chaning owner/group of webroot"
2721 elif [ "$vtype" = "$VTYPE_TLS" ]; then
2723 #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
2724 #_debug2 _hash_A "$_hash_A"
2725 #_x="$(echo $_hash_A | cut -c 1-32)"
2727 #_y="$(echo $_hash_A | cut -c 33-64)"
2729 #_SAN_A="$_x.$_y.token.acme.invalid"
2730 #_debug2 _SAN_A "$_SAN_A"
2733 _hash_B
="$(printf "%s
" "$keyauthorization" | _digest "sha256
" "hex
")"
2734 _debug2 _hash_B
"$_hash_B"
2735 _x
="$(echo "$_hash_B" | cut -c 1-32)"
2737 _y
="$(echo "$_hash_B" | cut -c 33-64)"
2740 #_SAN_B="$_x.$_y.ka.acme.invalid"
2742 _SAN_B
="$_x.$_y.acme.invalid"
2743 _debug2 _SAN_B
"$_SAN_B"
2745 _ncaddr
="$(_getfield "$Le_LocalAddress" "$_ncIndex")"
2746 _ncIndex
="$(_math "$_ncIndex" + 1)"
2747 if ! _starttlsserver
"$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
2748 _err
"Start tls server error."
2749 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
2756 if ! _send_signed_request
"$uri" "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"; then
2757 _err
"$d:Can not get challenge: $response"
2758 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
2764 if [ ! -z "$code" ] && [ ! "$code" = '202' ]; then
2765 _err
"$d:Challenge error: $response"
2766 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
2773 if [ -z "$MAX_RETRY_TIMES" ]; then
2778 waittimes
=$
(_math
"$waittimes" + 1)
2779 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
2781 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
2787 _debug
"sleep 2 secs to verify"
2790 response
="$(_get "$uri")"
2791 if [ "$?" != "0" ]; then
2792 _err
"$d:Verify error:$response"
2793 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
2798 _debug2 original
"$response"
2800 response
="$(echo "$response" | _normalizeJson)"
2801 _debug2 response
"$response"
2803 status
=$
(echo "$response" | _egrep_o
'"status":"[^"]*' | cut
-d : -f 2 |
tr -d '"')
2804 if [ "$status" = "valid" ]; then
2805 _info
"$(__green Success)"
2806 _stopserver
"$serverproc"
2808 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
2812 if [ "$status" = "invalid" ]; then
2813 error
="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error
":\{[^\}]*')"
2814 _debug2 error
"$error"
2815 errordetail
="$(echo "$error" | _egrep_o '"detail
": *"[^
"]*' | cut -d '"' -f 4)"
2816 _debug2 errordetail "$errordetail"
2817 if [ "$errordetail" ]; then
2818 _err "$d:Verify error:$errordetail"
2820 _err "$d:Verify error:$error"
2822 if [ "$DEBUG" ]; then
2823 if [ "$vtype" = "$VTYPE_HTTP" ]; then
2824 _debug "Debug: get token url."
2825 _get "http://$d/.well-known/acme-challenge/$token" "" 1
2828 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2834 if [ "$status" = "pending" ]; then
2837 _err "$d:Verify error:$response"
2838 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2849 _info "Verify finished, start to sign."
2850 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)"
2852 if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"; then
2859 Le_LinkCert="$(grep -i '^Location.
*$
' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
2860 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
2862 if [ "$Le_LinkCert" ]; then
2863 echo "$BEGIN_CERT" >"$CERT_PATH"
2865 #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
2866 # _debug "Get cert failed. Let's try last response.
"
2867 # printf -- "%s
" "$_rcert" | _dbase64 "multiline
" | _base64 "multiline
" >> "$CERT_PATH"
2870 if ! printf -- "%s
" "$_rcert" | _dbase64 "multiline
" | _base64 "multiline
" >>"$CERT_PATH"; then
2871 _debug "Try cert link.
"
2872 _get "$Le_LinkCert" | _base64 "multiline
" >>"$CERT_PATH"
2875 echo "$END_CERT" >>"$CERT_PATH"
2876 _info "$
(__green
"Cert success.")"
2879 _info "Your cert is
in $
(__green
" $CERT_PATH ")"
2881 if [ -f "$CERT_KEY_PATH" ]; then
2882 _info "Your cert key is
in $
(__green
" $CERT_KEY_PATH ")"
2885 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
2887 if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
2889 _saveaccountconf "USER_PATH
" "$USER_PATH"
2893 if [ -z "$Le_LinkCert" ]; then
2894 response="$
(echo "$response" | _dbase64
"multiline" | _normalizeJson
)"
2895 _err "Sign failed
: $
(echo "$response" | _egrep_o
'"detail":"[^"]*"')"
2900 _cleardomainconf "Le_Vlist
"
2902 Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
2903 if ! _contains "$Le_LinkIssuer" ":"; then
2904 Le_LinkIssuer="$API$Le_LinkIssuer"
2907 _savedomainconf "Le_LinkIssuer
" "$Le_LinkIssuer"
2909 if [ "$Le_LinkIssuer" ]; then
2910 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
2911 _get "$Le_LinkIssuer" | _base64 "multiline
" >>"$CA_CERT_PATH"
2912 echo "$END_CERT" >>"$CA_CERT_PATH"
2913 _info "The intermediate CA cert is
in $
(__green
" $CA_CERT_PATH ")"
2914 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
2915 _info "And the full chain certs is there
: $
(__green
" $CERT_FULLCHAIN_PATH ")"
2918 Le_CertCreateTime=$(_time)
2919 _savedomainconf "Le_CertCreateTime
" "$Le_CertCreateTime"
2921 Le_CertCreateTimeStr=$(date -u)
2922 _savedomainconf "Le_CertCreateTimeStr
" "$Le_CertCreateTimeStr"
2924 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ]; then
2925 Le_RenewalDays="$MAX_RENEW"
2927 _savedomainconf "Le_RenewalDays
" "$Le_RenewalDays"
2930 if [ "$CA_BUNDLE" ]; then
2931 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
2933 _clearaccountconf "CA_BUNDLE
"
2936 if [ "$HTTPS_INSECURE" ]; then
2937 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
2939 _clearaccountconf "HTTPS_INSECURE
"
2942 if [ "$Le_Listen_V4" ]; then
2943 _savedomainconf "Le_Listen_V4
" "$Le_Listen_V4"
2944 _cleardomainconf Le_Listen_V6
2945 elif [ "$Le_Listen_V6" ]; then
2946 _savedomainconf "Le_Listen_V6
" "$Le_Listen_V6"
2947 _cleardomainconf Le_Listen_V4
2950 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
2952 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
2953 _savedomainconf "Le_NextRenewTimeStr
" "$Le_NextRenewTimeStr"
2955 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
2956 _savedomainconf "Le_NextRenewTime
" "$Le_NextRenewTime"
2960 if [ "$Le_RealCertPath$Le_RealKeyPath$Le_RealCACertPath$Le_ReloadCmd$Le_RealFullChainPath" ]; then
2969 if [ -z "$Le_Domain" ]; then
2970 _usage "Usage
: $PROJECT_ENTRY --renew -d domain.com
[--ecc]"
2976 _initpath "$Le_Domain" "$_isEcc"
2978 _info "$
(__green
"Renew: '$Le_Domain'")"
2979 if [ ! -f "$DOMAIN_CONF" ]; then
2980 _info "'$Le_Domain' is not a issued domain
, skip.
"
2984 if [ "$Le_RenewalDays" ]; then
2985 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
2990 if [ "$Le_API" ]; then
2994 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$
(_time
)" -lt "$Le_NextRenewTime" ]; then
2995 _info "Skip
, Next renewal
time is
: $
(__green
"$Le_NextRenewTimeStr")"
2996 _info "Add
'$(__red '--force')' to force to renew.
"
2997 return "$RENEW_SKIP"
3001 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"
3003 if [ "$res" != "0" ]; then
3007 if [ "$Le_DeployHook" ]; then
3008 deploy "$Le_Domain" "$Le_DeployHook" "$Le_Keylength"
3017 #renewAll [stopRenewOnError]
3020 _stopRenewOnError="$1"
3021 _debug "_stopRenewOnError
" "$_stopRenewOnError"
3024 for di in "${CERT_HOME}"/*.*/; do
3026 if ! [ -d "$di" ]; then
3027 _debug "Not directory
, skip
: $di"
3033 if _endswith "$d" "$ECC_SUFFIX"; then
3034 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
3035 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
3037 renew "$d" "$_isEcc"
3040 _debug "Return code
: $rc"
3041 if [ "$rc" != "0" ]; then
3042 if [ "$rc" = "$RENEW_SKIP" ]; then
3044 elif [ "$_stopRenewOnError" ]; then
3045 _err "Error renew
$d, stop now.
"
3049 _err "Error renew
$d, Go ahead to next one.
"
3060 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
3061 _usage "Usage
: $PROJECT_ENTRY --signcsr --csr mycsr.csr
-w /path
/to
/webroot
/a.com
/ "
3067 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
3068 if [ "$?
" != "0" ]; then
3069 _err "Can not
read subject from csr
: $_csrfile"
3072 _debug _csrsubj "$_csrsubj"
3074 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
3075 if [ "$?
" != "0" ]; then
3076 _err "Can not
read domain list from csr
: $_csrfile"
3079 _debug "_csrdomainlist
" "$_csrdomainlist"
3081 if [ -z "$_csrsubj" ]; then
3082 _csrsubj="$
(_getfield
"$_csrdomainlist" 1)"
3083 _debug _csrsubj "$_csrsubj"
3084 _csrdomainlist="$
(echo "$_csrdomainlist" | cut
-d , -f 2-)"
3085 _debug "_csrdomainlist
" "$_csrdomainlist"
3088 if [ -z "$_csrsubj" ]; then
3089 _err "Can not
read subject from csr
: $_csrfile"
3093 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
3094 if [ "$?
" != "0" ] || [ -z "$_csrkeylength" ]; then
3095 _err "Can not
read key length from csr
: $_csrfile"
3099 _initpath "$_csrsubj" "$_csrkeylength"
3100 mkdir -p "$DOMAIN_PATH"
3102 _info "Copy csr to
: $CSR_PATH"
3103 cp "$_csrfile" "$CSR_PATH"
3105 issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength"
3112 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
3113 _usage "Usage
: $PROJECT_ENTRY --showcsr --csr mycsr.csr
"
3119 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
3120 if [ "$?
" != "0" ] || [ -z "$_csrsubj" ]; then
3121 _err "Can not
read subject from csr
: $_csrfile"
3125 _info "Subject
=$_csrsubj"
3127 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
3128 if [ "$?
" != "0" ]; then
3129 _err "Can not
read domain list from csr
: $_csrfile"
3132 _debug "_csrdomainlist
" "$_csrdomainlist"
3134 _info "SubjectAltNames
=$_csrdomainlist"
3136 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
3137 if [ "$?
" != "0" ] || [ -z "$_csrkeylength" ]; then
3138 _err "Can not
read key length from csr
: $_csrfile"
3141 _info "KeyLength
=$_csrkeylength"
3149 if [ "$_raw" ]; then
3150 printf "%s
\n" "Main_Domain
${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew
"
3151 for di in "${CERT_HOME}"/*.*/; do
3152 if ! [ -d "$di" ]; then
3153 _debug "Not directory
, skip
: $di"
3159 if _endswith "$d" "$ECC_SUFFIX"; then
3160 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
3161 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
3163 _initpath "$d" "$_isEcc"
3164 if [ -f "$DOMAIN_CONF" ]; then
3166 printf "%s
\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
3171 if _exists column; then
3172 list "raw
" | column -t -s "$_sep"
3174 list "raw
" | tr "$_sep" '\t'
3184 if [ -z "$Le_DeployHook" ]; then
3185 _usage "Usage
: $PROJECT_ENTRY --deploy -d domain.com
--deploy-hook cpanel
[--ecc] "
3189 _initpath "$Le_Domain" "$_isEcc"
3190 if [ ! -d "$DOMAIN_PATH" ]; then
3191 _err "Domain is not valid
:'$Le_Domain'"
3195 _deployApi="$
(_findHook
"$Le_Domain" deploy
"$Le_DeployHook")"
3196 if [ -z "$_deployApi" ]; then
3197 _err "The deploy hook
$Le_DeployHook is not found.
"
3200 _debug _deployApi "$_deployApi"
3202 _savedomainconf Le_DeployHook "$Le_DeployHook"
3205 if ! . "$_deployApi"; then
3206 _err "Load
file $_deployApi error. Please check your api
file and try again.
"
3210 d_command="${Le_DeployHook}_deploy
"
3211 if ! _exists "$d_command"; then
3212 _err "It seems that your api
file is not correct
, it must have a
function named
: $d_command"
3216 if ! $d_command "$Le_Domain" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
3217 _err "Error deploy
for domain
:$Le_Domain"
3222 _err "Deploy error.
"
3225 _info "$
(__green Success
)"
3232 if [ -z "$Le_Domain" ]; then
3233 _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
]"
3237 Le_RealCertPath="$2"
3239 Le_RealCACertPath="$4"
3241 Le_RealFullChainPath="$6"
3244 _initpath "$Le_Domain" "$_isEcc"
3245 if [ ! -d "$DOMAIN_PATH" ]; then
3246 _err "Domain is not valid
:'$Le_Domain'"
3254 _savedomainconf "Le_RealCertPath
" "$Le_RealCertPath"
3255 _savedomainconf "Le_RealCACertPath
" "$Le_RealCACertPath"
3256 _savedomainconf "Le_RealKeyPath
" "$Le_RealKeyPath"
3257 _savedomainconf "Le_ReloadCmd
" "$Le_ReloadCmd"
3258 _savedomainconf "Le_RealFullChainPath
" "$Le_RealFullChainPath"
3260 if [ "$Le_RealCertPath" = "$NO_VALUE" ]; then
3263 if [ "$Le_RealKeyPath" = "$NO_VALUE" ]; then
3266 if [ "$Le_RealCACertPath" = "$NO_VALUE" ]; then
3267 Le_RealCACertPath=""
3269 if [ "$Le_ReloadCmd" = "$NO_VALUE" ]; then
3272 if [ "$Le_RealFullChainPath" = "$NO_VALUE" ]; then
3273 Le_RealFullChainPath=""
3276 if [ "$Le_RealCertPath" ]; then
3278 _info "Installing cert to
:$Le_RealCertPath"
3279 if [ -f "$Le_RealCertPath" ] && [ ! "$IS_RENEW" ]; then
3280 cp "$Le_RealCertPath" "$Le_RealCertPath".bak
3282 cat "$CERT_PATH" >"$Le_RealCertPath"
3285 if [ "$Le_RealCACertPath" ]; then
3287 _info "Installing CA to
:$Le_RealCACertPath"
3288 if [ "$Le_RealCACertPath" = "$Le_RealCertPath" ]; then
3289 echo "" >>"$Le_RealCACertPath"
3290 cat "$CA_CERT_PATH" >>"$Le_RealCACertPath"
3292 if [ -f "$Le_RealCACertPath" ] && [ ! "$IS_RENEW" ]; then
3293 cp "$Le_RealCACertPath" "$Le_RealCACertPath".bak
3295 cat "$CA_CERT_PATH" >"$Le_RealCACertPath"
3299 if [ "$Le_RealKeyPath" ]; then
3301 _info "Installing key to
:$Le_RealKeyPath"
3302 if [ -f "$Le_RealKeyPath" ] && [ ! "$IS_RENEW" ]; then
3303 cp "$Le_RealKeyPath" "$Le_RealKeyPath".bak
3305 cat "$CERT_KEY_PATH" >"$Le_RealKeyPath"
3308 if [ "$Le_RealFullChainPath" ]; then
3310 _info "Installing full chain to
:$Le_RealFullChainPath"
3311 if [ -f "$Le_RealFullChainPath" ] && [ ! "$IS_RENEW" ]; then
3312 cp "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak
3314 cat "$CERT_FULLCHAIN_PATH" >"$Le_RealFullChainPath"
3317 if [ "$Le_ReloadCmd" ]; then
3319 _info "Run Le_ReloadCmd
: $Le_ReloadCmd"
3320 if (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd"); then
3321 _info "$
(__green
"Reload success")"
3323 _err "Reload error
for :$Le_Domain"
3331 if ! _exists "crontab
"; then
3332 _err "crontab doesn
't exist, so, we can not install cron jobs."
3333 _err "All your certs will not be renewed automatically."
3334 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
3338 _info "Installing cron job"
3339 if ! crontab -l | grep "$PROJECT_ENTRY --cron"; then
3340 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
3341 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
3343 _err "Can not install cronjob, $PROJECT_ENTRY not found."
3346 if _exists uname && uname -a | grep solaris >/dev/null; then
3349 echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
3354 echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
3358 if [ "$?" != "0" ]; then
3359 _err "Install cron job failed. You need to manually renew your certs."
3360 _err "Or you can add cronjob by yourself:"
3361 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
3366 uninstallcronjob() {
3367 if ! _exists "crontab"; then
3370 _info "Removing cron job"
3371 cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")"
3373 if _exists uname && uname -a | grep solaris >/dev/null; then
3374 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab --
3376 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -
3378 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
3379 _info LE_WORKING_DIR
"$LE_WORKING_DIR"
3387 if [ -z "$Le_Domain" ]; then
3388 _usage
"Usage: $PROJECT_ENTRY --revoke -d domain.com"
3394 _initpath
"$Le_Domain" "$_isEcc"
3395 if [ ! -f "$DOMAIN_CONF" ]; then
3396 _err
"$Le_Domain is not a issued domain, skip."
3400 if [ ! -f "$CERT_PATH" ]; then
3401 _err
"Cert for $Le_Domain $CERT_PATH is not found, skip."
3405 cert
="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _urlencode)"
3407 if [ -z "$cert" ]; then
3408 _err
"Cert for $Le_Domain is empty found, skip."
3412 data
="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
3413 uri
="$API/acme/revoke-cert"
3415 if [ -f "$CERT_KEY_PATH" ]; then
3416 _info
"Try domain key first."
3417 if _send_signed_request
"$uri" "$data" "" "$CERT_KEY_PATH"; then
3418 if [ -z "$response" ]; then
3419 _info
"Revoke success."
3423 _err
"Revoke error by domain key."
3428 _info
"Domain key file doesn't exists."
3431 _info
"Try account key."
3433 if _send_signed_request
"$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
3434 if [ -z "$response" ]; then
3435 _info
"Revoke success."
3439 _err
"Revoke error."
3454 while [ "$_d_i" -lt "$_d_max_retry" ]; do
3455 _info
"Deactivate: $_d_domain"
3456 _d_i
="$(_math $_d_i + 1)"
3458 if ! __get_domain_new_authz
"$_d_domain"; then
3459 _err
"Can not get domain new authz token."
3463 authzUri
="$(echo "$responseHeaders" | grep "^Location
:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
3464 _debug
"authzUri" "$authzUri"
3466 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
3467 _err
"new-authz error: $response"
3471 entry
="$(printf "%s
\n" "$response" | _egrep_o '[^\{]*"status
":"valid
","uri
"[^\}]*')"
3472 _debug entry
"$entry"
3474 if [ -z "$entry" ]; then
3475 _info
"No more valid entry found."
3479 _vtype
="$(printf "%s
\n" "$entry" | _egrep_o '"type": *"[^
"]*"' | cut -d : -f 2 | tr -d '"')"
3480 _debug _vtype
"$_vtype"
3481 _info
"Found $_vtype"
3483 uri
="$(printf "%s
\n" "$entry" | _egrep_o '"uri
":"[^
"]*' | cut -d : -f 2,3 | tr -d '"')"
3486 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
3487 _info "Skip $_vtype"
3491 _info "Deactivate: $_vtype"
3493 if ! _send_signed_request "$authzUri" "{\"resource\": \"authz\", \"status\":\"deactivated\"}"; then
3494 _err "Can not deactivate $_vtype."
3498 _info "Deactivate: $_vtype success."
3502 if [ "$_d_i" -lt "$_d_max_retry" ]; then
3503 _info "Deactivated success!"
3505 _err "Deactivate failed."
3514 _debug _d_domain_list "$_d_domain_list"
3515 if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
3516 _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
3519 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
3520 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
3523 if ! _deactivate "$_d_dm" "$_d_type"; then
3529 # Detect profile file if not specified as environment variable
3531 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
3537 SHELLTYPE="$(basename "/$SHELL")"
3539 if [ "$SHELLTYPE" = "bash" ]; then
3540 if [ -f "$HOME/.bashrc" ]; then
3541 DETECTED_PROFILE="$HOME/.bashrc"
3542 elif [ -f "$HOME/.bash_profile" ]; then
3543 DETECTED_PROFILE="$HOME/.bash_profile"
3545 elif [ "$SHELLTYPE" = "zsh" ]; then
3546 DETECTED_PROFILE="$HOME/.zshrc"
3549 if [ -z "$DETECTED_PROFILE" ]; then
3550 if [ -f "$HOME/.profile" ]; then
3551 DETECTED_PROFILE="$HOME/.profile"
3552 elif [ -f "$HOME/.bashrc" ]; then
3553 DETECTED_PROFILE="$HOME/.bashrc"
3554 elif [ -f "$HOME/.bash_profile" ]; then
3555 DETECTED_PROFILE="$HOME/.bash_profile"
3556 elif [ -f "$HOME/.zshrc" ]; then
3557 DETECTED_PROFILE="$HOME/.zshrc"
3561 if [ ! -z "$DETECTED_PROFILE" ]; then
3562 echo "$DETECTED_PROFILE"
3568 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
3569 echo "#ACCOUNT_CONF_PATH=xxxx
3571 #Account configurations:
3572 #Here are the supported macros, uncomment them to make them take effect.
3574 #ACCOUNT_EMAIL=aaa@example.com # the account email used to register account.
3575 #ACCOUNT_KEY_PATH=\"/path/to/account.key\"
3576 #CERT_HOME=\"/path/to/cert/home\"
3580 #LOG_FILE=\"$DEFAULT_LOG_FILE\"
3585 #STAGE=1 # Use the staging api
3586 #FORCE=1 # Force to issue cert
3587 #DEBUG=1 # Debug mode
3589 #OPENSSL_BIN=openssl
3591 #USER_AGENT=\"$USER_AGENT\"
3596 #######################
3599 #CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\"
3601 #CF_Email=\"xxxx@sss.com\"
3603 #######################
3608 #DP_Key=\"sADDsdasdgdsf\"
3610 #######################
3614 #CX_Secret=\"sADDsdasdgdsf\"
3616 #######################
3618 #GD_Key=\"sdfdsgdgdfdasfds\"
3620 #GD_Secret=\"sADDsdasdfsdfdssdgdsf\"
3622 #######################
3624 #NSUPDATE_KEY=\"/path/to/update.key\"
3625 #NSUPDATE_SERVER=\"192.168.0.1\"
3627 #######################
3629 #PDNS_Url=\"http://ns.example.com:8081\"
3630 #PDNS_ServerId=\"localhost\"
3631 #PDNS_Token=\"0123456789ABCDEF\"
3634 #######################
3636 #AWS_ACCESS_KEY_ID=XXXXXXXXXX
3637 #AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXX
3639 " >"$ACCOUNT_CONF_PATH"
3647 if ! _exists "curl" && ! _exists "wget"; then
3648 _err "Please install curl or wget first, we need to access http resources."
3652 if [ -z "$_nocron" ]; then
3653 if ! _exists "crontab"; then
3654 _err "It is recommended to install crontab first. try to install 'cron
, crontab
, crontabs or vixie-cron
'."
3655 _err "We need to set cron job to renew the certs automatically."
3656 _err "Otherwise, your certs will not be able to be renewed automatically."
3657 if [ -z "$FORCE" ]; then
3658 _err "Please add '--force' and try install again to go without crontab."
3659 _err "./$PROJECT_ENTRY --install --force"
3665 if ! _exists "$OPENSSL_BIN"; then
3666 _err "Please install openssl first."
3667 _err "We need openssl to generate keys."
3671 if ! _exists "nc"; then
3672 _err "It is recommended to install nc first, try to install 'nc
' or 'netcat
'."
3673 _err "We use nc for standalone server if you use standalone mode."
3674 _err "If you don't use standalone mode
, just ignore this warning.
"
3683 if [ -z "$_shebang" ]; then
3684 _usage "Usage
: file shebang
"
3687 cp "$_file" "$_file.tmp
"
3688 echo "$_shebang" >"$_file"
3689 sed -n 2,99999p "$_file.tmp
" >>"$_file"
3696 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env
"
3697 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
3698 echo "$
(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.
*$||
" >"$_envfile"
3699 echo "$
(cat "$_envfile")" | sed "s|^
alias le.
*$||
" >"$_envfile"
3700 echo "$
(cat "$_envfile")" | sed "s|^
alias le.sh.
*$||
" >"$_envfile"
3703 _setopt "$_envfile" "export LE_WORKING_DIR
" "=" "\"$LE_WORKING_DIR\""
3704 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
3706 _profile="$
(_detect_profile
)"
3707 if [ "$_profile" ]; then
3708 _debug "Found profile
: $_profile"
3709 _info "Installing
alias to
'$_profile'"
3710 _setopt "$_profile" ".
\"$_envfile\""
3711 _info "OK
, Close and reopen your terminal to start using
$PROJECT_NAME"
3713 _info "No profile is found
, you will need to go into
$LE_WORKING_DIR to use
$PROJECT_NAME"
3717 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh
"
3718 _csh_profile="$HOME/.cshrc
"
3719 if [ -f "$_csh_profile" ]; then
3720 _info "Installing
alias to
'$_csh_profile'"
3721 _setopt "$_cshfile" "setenv LE_WORKING_DIR
" " " "\"$LE_WORKING_DIR\""
3722 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
3723 _setopt "$_csh_profile" "source \"$_cshfile\""
3727 _tcsh_profile="$HOME/.tcshrc
"
3728 if [ -f "$_tcsh_profile" ]; then
3729 _info "Installing
alias to
'$_tcsh_profile'"
3730 _setopt "$_cshfile" "setenv LE_WORKING_DIR
" " " "\"$LE_WORKING_DIR\""
3731 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
3732 _setopt "$_tcsh_profile" "source \"$_cshfile\""
3740 if [ -z "$LE_WORKING_DIR" ]; then
3741 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
3745 if ! _initpath; then
3746 _err "Install failed.
"
3749 if [ "$_nocron" ]; then
3750 _debug "Skip
install cron job
"
3753 if ! _precheck "$_nocron"; then
3754 _err "Pre-check failed
, can not
install.
"
3759 if [ -d "$HOME/.le
" ]; then
3760 for envfile in "le.env
" "le.sh.env
"; do
3761 if [ -f "$HOME/.le
/$envfile" ]; then
3762 if grep "le.sh
" "$HOME/.le
/$envfile" >/dev/null; then
3764 _info "You are upgrading from le.sh
"
3765 _info "Renaming
\"$HOME/.le
\" to
$LE_WORKING_DIR"
3766 mv "$HOME/.le
" "$LE_WORKING_DIR"
3767 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env
"
3774 _info "Installing to
$LE_WORKING_DIR"
3776 if ! mkdir -p "$LE_WORKING_DIR"; then
3777 _err "Can not create working dir
: $LE_WORKING_DIR"
3781 chmod 700 "$LE_WORKING_DIR"
3783 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
3785 if [ "$?
" != "0" ]; then
3786 _err "Install failed
, can not copy
$PROJECT_ENTRY"
3790 _info "Installed to
$LE_WORKING_DIR/$PROJECT_ENTRY"
3794 for subf in $_SUB_FOLDERS; do
3795 if [ -d "$subf" ]; then
3796 mkdir -p "$LE_WORKING_DIR/$subf"
3797 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
3801 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
3805 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
3806 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH
" "=" "\"$ACCOUNT_CONF_PATH\""
3809 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
3810 _saveaccountconf "CERT_HOME
" "$CERT_HOME"
3813 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
3814 _saveaccountconf "ACCOUNT_KEY_PATH
" "$ACCOUNT_KEY_PATH"
3817 if [ -z "$_nocron" ]; then
3821 if [ -z "$NO_DETECT_SH" ]; then
3823 if _exists bash; then
3824 _info "Good
, bash is found
, so change the shebang to use bash as prefered.
"
3825 _shebang='#!/usr/bin/env bash'
3826 _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
3827 for subf in $_SUB_FOLDERS; do
3828 if [ -d "$LE_WORKING_DIR/$subf" ]; then
3829 for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
3830 _setShebang "$_apifile" "$_shebang"
3843 if [ -z "$_nocron" ]; then
3850 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
3851 _info "The keys and certs are
in $LE_WORKING_DIR, you can remove them by yourself.
"
3858 _profile="$
(_detect_profile
)"
3859 if [ "$_profile" ]; then
3860 _info "Uninstalling
alias from
: '$_profile'"
3861 text="$
(cat "$_profile")"
3862 echo "$text" | sed "s|^.
*\"$LE_WORKING_DIR/$PROJECT_NAME.env
\"$||
" >"$_profile"
3865 _csh_profile="$HOME/.cshrc
"
3866 if [ -f "$_csh_profile" ]; then
3867 _info "Uninstalling
alias from
: '$_csh_profile'"
3868 text="$
(cat "$_csh_profile")"
3869 echo "$text" | sed "s|^.
*\"$LE_WORKING_DIR/$PROJECT_NAME.csh
\"$||
" >"$_csh_profile"
3872 _tcsh_profile="$HOME/.tcshrc
"
3873 if [ -f "$_tcsh_profile" ]; then
3874 _info "Uninstalling
alias from
: '$_csh_profile'"
3875 text="$
(cat "$_tcsh_profile")"
3876 echo "$text" | sed "s|^.
*\"$LE_WORKING_DIR/$PROJECT_NAME.csh
\"$||
" >"$_tcsh_profile"
3884 if [ "$AUTO_UPGRADE" = "1" ]; then
3885 export LE_WORKING_DIR
3888 _err "Cron
:Upgrade failed
!"
3892 . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
3898 _info "Auto upgraded to
: $VER"
3914 echo "Usage
: $PROJECT_ENTRY command ...
[parameters
]....
3916 --help, -h Show this
help message.
3917 --version, -v Show version info.
3918 --install Install
$PROJECT_NAME to your system.
3919 --uninstall Uninstall
$PROJECT_NAME, and uninstall the cron job.
3920 --upgrade Upgrade
$PROJECT_NAME to the latest code from
$PROJECT .
3921 --issue Issue a cert.
3922 --signcsr Issue a cert from an existing csr.
3923 --deploy Deploy the cert to your server.
3924 --installcert Install the issued cert to apache
/nginx or any other server.
3925 --renew, -r Renew a cert.
3926 --renewAll Renew all the certs.
3927 --revoke Revoke a cert.
3928 --list List all the certs.
3929 --showcsr Show the content of a csr.
3930 --installcronjob Install the cron job to renew certs
, you don
't need to call this. The 'install' command can automatically install the cron job.
3931 --uninstallcronjob Uninstall the cron job. The 'uninstall
' command can do this automatically.
3932 --cron Run cron job to renew all the certs.
3933 --toPkcs Export the certificate and key to a pfx file.
3934 --updateaccount Update account info.
3935 --registeraccount Register account key.
3936 --createAccountKey, -cak Create an account private key, professional use.
3937 --createDomainKey, -cdk Create an domain private key, professional use.
3938 --createCSR, -ccsr Create CSR , professional use.
3939 --deactivate Deactivate the domain authz, professional use.
3942 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
3943 --force, -f Used to force to install or force to renew a cert immediately.
3944 --staging, --test Use staging server, just for test.
3945 --debug Output debug info.
3947 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
3948 --standalone Use standalone mode.
3949 --tls Use standalone tls mode.
3950 --apache Use apache mode.
3951 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
3952 --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.
3954 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
3955 --accountkeylength, -ak [2048] Specifies the account key length.
3956 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a
file path here.
3957 --log-level 1|
2 Specifies the log level
, default is
1.
3959 These parameters are to
install the cert to nginx
/apache or anyother server after issue
/renew a cert
:
3961 --certpath /path
/to
/real
/cert
/file After issue
/renew
, the cert will be copied to this path.
3962 --keypath /path
/to
/real
/key
/file After issue
/renew
, the key will be copied to this path.
3963 --capath /path
/to
/real
/ca
/file After issue
/renew
, the intermediate cert will be copied to this path.
3964 --fullchainpath /path
/to
/fullchain
/file After issue
/renew
, the fullchain cert will be copied to this path.
3966 --reloadcmd \"service nginx reload
\" After issue
/renew
, it
's used to reload the server.
3968 --accountconf Specifies a customized account config file.
3969 --home Specifies the home dir for $PROJECT_NAME .
3970 --certhome Specifies the home dir to save all the certs, only valid for '--install' command.
3971 --useragent Specifies the user agent string. it will be saved for future use too.
3972 --accountemail Specifies the account email for registering, Only valid for the '--install' command.
3973 --accountkey Specifies the account key path, Only valid for the '--install' command.
3974 --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
3975 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
3976 --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
3977 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
3978 --listraw Only used for '--list' command, list the certs in raw format.
3979 --stopRenewOnError, -se Only valid for '--renewall' command. Stop if one cert has error in renewal.
3980 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
3981 --ca-bundle Specifices the path to the CA certificate bundle to verify api server
's certificate.
3982 --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.
3983 --ecc Specifies to use the ECC cert. Valid for '--installcert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
3984 --csr Specifies the input csr.
3985 --pre-hook Command to be run before obtaining any certificates.
3986 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obain/renew is success or failed.
3987 --renew-hook Command to be run once for each successfully renewed certificate.
3988 --deploy-hook The hook file to deploy cert
3989 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
3990 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
3991 --listen-v4 Force standalone/tls server to listen at ipv4.
3992 --listen-v6 Force standalone/tls server to listen at ipv6.
3993 --openssl-bin Specifies a custom openssl bin location.
3999 _info "Installing from online archive."
4001 if [ ! "$BRANCH" ]; then
4005 target="$PROJECT/archive/$BRANCH.tar.gz"
4006 _info "Downloading $target"
4007 localname="$BRANCH.tar.gz"
4008 if ! _get "$target" >$localname; then
4009 _err "Download error."
4013 _info "Extracting $localname"
4016 cd "$PROJECT_NAME-$BRANCH"
4017 chmod +x $PROJECT_ENTRY
4018 if ./$PROJECT_ENTRY install "$_nocron"; then
4019 _info "Install success!"
4024 rm -rf "$PROJECT_NAME-$BRANCH"
4032 export LE_WORKING_DIR
4033 cd "$LE_WORKING_DIR"
4034 _installOnline "nocron"
4036 _info "Upgrade success!"
4039 _err "Upgrade failed!"
4044 _processAccountConf() {
4045 if [ "$_useragent" ]; then
4046 _saveaccountconf "USER_AGENT" "$_useragent"
4047 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
4048 _saveaccountconf "USER_AGENT" "$USER_AGENT"
4051 if [ "$_accountemail" ]; then
4052 _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
4053 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
4054 _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
4057 if [ "$_openssl_bin" ]; then
4058 _saveaccountconf "OPENSSL_BIN" "$_openssl_bin"
4059 elif [ "$OPENSSL_BIN" ] && [ "$OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
4060 _saveaccountconf "OPENSSL_BIN" "$OPENSSL_BIN"
4063 if [ "$_auto_upgrade" ]; then
4064 _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
4065 elif [ "$AUTO_UPGRADE" ]; then
4066 _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
4074 _altdomains="$NO_VALUE"
4077 _accountkeylength=""
4093 _stopRenewOnError=""
4111 while [ ${#} -gt 0 ]; do
4149 --renewAll | --renewall)
4159 _CMD="installcronjob"
4162 _CMD="uninstallcronjob"
4170 --createAccountKey | --createaccountkey | -cak)
4171 _CMD="createAccountKey"
4173 --createDomainKey | --createdomainkey | -cdk)
4174 _CMD="createDomainKey"
4176 --createCSR | --createcsr | -ccr)
4183 _CMD="updateaccount"
4186 _CMD="registeraccount"
4191 if [ "$_dvalue" ]; then
4192 if _startswith "$_dvalue" "-"; then
4193 _err "'$_dvalue' is not a valid domain for parameter '$1'"
4196 if _is_idn "$_dvalue" && ! _exists idn; then
4197 _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn
' command first."
4201 if [ -z "$_domain" ]; then
4204 if [ "$_altdomains" = "$NO_VALUE" ]; then
4205 _altdomains="$_dvalue"
4207 _altdomains="$_altdomains,$_dvalue"
4222 if [ -z "$2" ] || _startswith "$2" "-"; then
4231 if [ -z "$_webroot" ]; then
4234 _webroot="$_webroot,$wvalue"
4240 if [ -z "$_webroot" ]; then
4243 _webroot="$_webroot,$wvalue"
4248 _local_address="$_local_address$lvalue,"
4253 if [ -z "$_webroot" ]; then
4256 _webroot="$_webroot,$wvalue"
4261 if [ -z "$_webroot" ]; then
4264 _webroot="$_webroot,$wvalue"
4269 if ! _startswith "$2" "-"; then
4273 if [ -z "$_webroot" ]; then
4276 _webroot="$_webroot,$wvalue"
4281 Le_DNSSleep="$_dnssleep"
4289 --accountkeylength | -ak)
4290 _accountkeylength="$2"
4310 --reloadcmd | --reloadCmd)
4320 ACCOUNT_CONF_PATH="$_accountconf"
4329 CERT_HOME="$_certhome"
4334 USER_AGENT="$_useragent"
4339 ACCOUNT_EMAIL="$_accountemail"
4344 ACCOUNT_KEY_PATH="$_accountkey"
4349 Le_RenewalDays="$_days"
4354 Le_HTTPPort="$_httpport"
4359 Le_TLSPort="$_tlsport"
4366 --stopRenewOnError | --stoprenewonerror | -se)
4367 _stopRenewOnError="1"
4374 _ca_bundle="$(readlink -f "$2")"
4375 CA_BUNDLE="$_ca_bundle"
4404 --ocsp-must-staple | --ocsp)
4410 if _startswith "$_logfile" '-'; then
4415 LOG_FILE="$_logfile"
4416 if [ -z "$LOG_LEVEL" ]; then
4417 LOG_LEVEL="$DEFAULT_LOG_LEVEL"
4422 LOG_LEVEL="$_log_level"
4427 if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
4432 AUTO_UPGRADE="$_auto_upgrade"
4436 Le_Listen_V4="$_listen_v4"
4440 Le_Listen_V6="$_listen_v6"
4444 OPENSSL_BIN="$_openssl_bin"
4447 _err "Unknown parameter : $1"
4455 if [ "${_CMD}" != "install" ]; then
4457 if [ "$_log" ]; then
4458 if [ -z "$_logfile" ]; then
4459 _logfile="$DEFAULT_LOG_FILE"
4462 if [ "$_logfile" ]; then
4463 _saveaccountconf "LOG_FILE" "$_logfile"
4464 LOG_FILE="$_logfile"
4467 if [ "$_log_level" ]; then
4468 _saveaccountconf "LOG_LEVEL" "$_log_level"
4469 LOG_LEVEL="$_log_level"
4475 _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
4477 if [ "$DEBUG" ]; then
4482 install) install "$_nocron" ;;
4483 uninstall) uninstall "$_nocron" ;;
4486 issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address"
4489 deploy "$_domain" "$_deploy_hook" "$_ecc"
4492 signcsr "$_csr" "$_webroot"
4495 showcsr "$_csr" "$_domain"
4498 installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_ecc"
4501 renew "$_domain" "$_ecc"
4504 renewAll "$_stopRenewOnError"
4507 revoke "$_domain" "$_ecc"
4510 deactivate "$_domain,$_altdomains"
4513 registeraccount "$_accountkeylength"
4521 installcronjob) installcronjob ;;
4522 uninstallcronjob) uninstallcronjob ;;
4525 toPkcs "$_domain" "$_password" "$_ecc"
4528 createAccountKey "$_accountkeylength"
4531 createDomainKey "$_domain" "$_keylength"
4534 createCSR "$_domain" "$_altdomains" "$_ecc"
4538 _err "Invalid command: $_CMD"
4544 if [ "$_ret" != "0" ]; then
4548 if [ "${_CMD}" = "install" ]; then
4549 if [ "$_log" ]; then
4550 if [ -z "$LOG_FILE" ]; then
4551 LOG_FILE="$DEFAULT_LOG_FILE"
4553 _saveaccountconf "LOG_FILE" "$LOG_FILE"
4556 if [ "$_log_level" ]; then
4557 _saveaccountconf "LOG_LEVEL" "$_log_level"
4564 if [ "$INSTALLONLINE" ]; then
4566 _installOnline $BRANCH
4571 [ -z "$1" ] && showhelp && return
4572 if _startswith "$1" '-'; then _process "$@"; else "$@"; fi