7 PROJECT_ENTRY
="acme.sh"
9 PROJECT
="https://github.com/Neilpang/$PROJECT_NAME"
11 DEFAULT_INSTALL_HOME
="$HOME/.$PROJECT_NAME"
14 _SUB_FOLDERS
="dnsapi deploy"
16 LETSENCRYPT_CA_V1
="https://acme-v01.api.letsencrypt.org/directory"
17 LETSENCRYPT_STAGING_CA_V1
="https://acme-staging.api.letsencrypt.org/directory"
19 LETSENCRYPT_CA_V2
="https://acme-v02.api.letsencrypt.org/directory"
20 LETSENCRYPT_STAGING_CA_V2
="https://acme-staging-v02.api.letsencrypt.org/directory"
22 DEFAULT_CA
=$LETSENCRYPT_CA_V1
23 DEFAULT_STAGING_CA
=$LETSENCRYPT_STAGING_CA_V1
25 DEFAULT_USER_AGENT
="$PROJECT_NAME/$VER ($PROJECT)"
26 DEFAULT_ACCOUNT_EMAIL
=""
28 DEFAULT_ACCOUNT_KEY_LENGTH
=2048
29 DEFAULT_DOMAIN_KEY_LENGTH
=2048
31 DEFAULT_OPENSSL_BIN
="openssl"
33 _OLD_CA_HOST
="https://acme-v01.api.letsencrypt.org"
34 _OLD_STAGE_CA_HOST
="https://acme-staging.api.letsencrypt.org"
38 VTYPE_ALPN
="tls-alpn-01"
40 LOCAL_ANY_ADDRESS
="0.0.0.0"
52 MODE_STATELESS
="stateless"
54 STATE_VERIFIED
="verified_ok"
57 NGINX_START
="#ACME_NGINX_START"
58 NGINX_END
="#ACME_NGINX_END"
60 BEGIN_CSR
="-----BEGIN CERTIFICATE REQUEST-----"
61 END_CSR
="-----END CERTIFICATE REQUEST-----"
63 BEGIN_CERT
="-----BEGIN CERTIFICATE-----"
64 END_CERT
="-----END CERTIFICATE-----"
66 CONTENT_TYPE_JSON
="application/jose+json"
70 ECC_SUFFIX
="${ECC_SEP}ecc"
75 DEFAULT_LOG_LEVEL
="$LOG_LEVEL_1"
80 DEBUG_LEVEL_DEFAULT
=$DEBUG_LEVEL_1
83 HIDDEN_VALUE
="[hidden](please add '--output-insecure' to see this value)"
85 SYSLOG_ERROR
="user.error"
86 SYSLOG_INFO
="user.info"
87 SYSLOG_DEBUG
="user.debug"
96 SYSLOG_LEVEL_DEBUG_2
=8
98 SYSLOG_LEVEL_DEBUG_3
=9
100 SYSLOG_LEVEL_DEFAULT
=$SYSLOG_LEVEL_ERROR
104 _DEBUG_WIKI
="https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh"
106 _PREPARE_LINK
="https://github.com/Neilpang/acme.sh/wiki/Install-preparations"
108 _STATELESS_WIKI
="https://github.com/Neilpang/acme.sh/wiki/Stateless-Mode"
110 _DNS_ALIAS_WIKI
="https://github.com/Neilpang/acme.sh/wiki/DNS-alias-mode"
112 _DNS_MANUAL_WIKI
="https://github.com/Neilpang/acme.sh/wiki/dns-manual-mode"
114 _DNS_MANUAL_ERR
="The dns manual mode can not renew automatically, you must issue it again manually. You'd better use the other modes instead."
116 _DNS_MANUAL_WARN
="It seems that you are using dns manual mode. please take care: $_DNS_MANUAL_ERR"
118 _DNS_MANUAL_ERROR
="It seems that you are using dns manual mode. Read this link first: $_DNS_MANUAL_WIKI"
126 if [ "${__INTERACTIVE}${ACME_NO_COLOR:-0}" = "10" -o "${ACME_FORCE_COLOR}" = "1" ]; then
127 printf '\033[1;31;32m%b\033[0m' "$1"
134 if [ "${__INTERACTIVE}${ACME_NO_COLOR:-0}" = "10" -o "${ACME_FORCE_COLOR}" = "1" ]; then
135 printf '\033[1;31;40m%b\033[0m' "$1"
142 if [ -z "$NO_TIMESTAMP" ] ||
[ "$NO_TIMESTAMP" = "0" ]; then
143 printf -- "%s" "[$(date)] "
148 printf -- "%s" "$1='$2'"
154 echo "Diagnosis versions: "
155 echo "openssl:$ACME_OPENSSL_BIN"
156 if _exists
"${ACME_OPENSSL_BIN:-openssl}"; then
157 ${ACME_OPENSSL_BIN:-openssl} version
2>&1
159 echo "$ACME_OPENSSL_BIN doesn't exists."
163 if [ "$_APACHECTL" ] && _exists
"$_APACHECTL"; then
166 echo "apache doesn't exists."
170 if _exists
"nginx"; then
173 echo "nginx doesn't exists."
177 if _exists
"socat"; then
180 _debug
"socat doesn't exists."
186 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" = "$SYSLOG_LEVEL_NONE" ]; then
191 if [ -z "$__logger_i" ]; then
192 if _contains
"$(logger --help 2>&1)" "-i"; then
193 __logger_i
="logger -i"
198 $__logger_i -t "$PROJECT_NAME" -p "$_logclass" "$(_printargs "$@
")" >/dev
/null
2>&1
202 [ -z "$LOG_FILE" ] && return
203 _printargs
"$@" >>"$LOG_FILE"
208 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_INFO" ]; then
209 _syslog
"$SYSLOG_INFO" "$@"
215 _syslog
"$SYSLOG_ERROR" "$@"
217 if [ -z "$NO_TIMESTAMP" ] ||
[ "$NO_TIMESTAMP" = "0" ]; then
218 printf -- "%s" "[$(date)] " >&2
235 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
238 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
239 _syslog
"$SYSLOG_DEBUG" "$@"
241 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
246 #output the sensitive messages
248 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
249 if [ "$OUTPUT_INSECURE" = "1" ]; then
252 _log
"$1" "$HIDDEN_VALUE"
255 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
256 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
258 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
259 if [ "$OUTPUT_INSECURE" = "1" ]; then
262 _printargs
"$1" "$HIDDEN_VALUE" >&2
268 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
271 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
272 _syslog
"$SYSLOG_DEBUG" "$@"
274 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
280 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
281 if [ "$OUTPUT_INSECURE" = "1" ]; then
284 _log
"$1" "$HIDDEN_VALUE"
287 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
288 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
290 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
291 if [ "$OUTPUT_INSECURE" = "1" ]; then
294 _printargs
"$1" "$HIDDEN_VALUE" >&2
300 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
303 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
304 _syslog
"$SYSLOG_DEBUG" "$@"
306 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
312 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
313 if [ "$OUTPUT_INSECURE" = "1" ]; then
316 _log
"$1" "$HIDDEN_VALUE"
319 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
320 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
322 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
323 if [ "$OUTPUT_INSECURE" = "1" ]; then
326 _printargs
"$1" "$HIDDEN_VALUE" >&2
332 # shellcheck disable=SC2018,SC2019
337 # shellcheck disable=SC2018,SC2019
344 echo "$_str" |
grep "^$_sub" >/dev
/null
2>&1
350 echo "$_str" |
grep -- "$_sub\$" >/dev
/null
2>&1
356 echo "$_str" |
grep -- "$_sub" >/dev
/null
2>&1
363 if [ -z "$_field" ]; then
364 _usage
"Usage: str field [sep]"
368 if [ -z "$_sep" ]; then
372 for f
in $
(echo "$_str" |
tr "$_sep" ' '); do
373 if [ "$f" = "$_field" ]; then
374 _debug2
"'$_str' contains '$_field'"
375 return 0 #contains ok
378 _debug2
"'$_str' does not contain '$_field'"
379 return 1 #not contains
388 if [ -z "$_findex" ]; then
389 _usage
"Usage: str field [sep]"
393 if [ -z "$_sep" ]; then
398 while [ "$_ffi" -gt "0" ]; do
399 _fv
="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")"
401 printf -- "%s" "$_fv"
404 _ffi
="$(_math "$_ffi" - 1)"
407 printf -- "%s" "$_str"
413 if [ -z "$cmd" ]; then
414 _usage
"Usage: _exists cmd"
418 if eval type type >/dev
/null
2>&1; then
419 eval type "$cmd" >/dev
/null
2>&1
420 elif command >/dev
/null
2>&1; then
421 command -v "$cmd" >/dev
/null
2>&1
423 which "$cmd" >/dev
/null
2>&1
426 _debug3
"$cmd exists=$ret"
433 printf "%s" "$(($_m_opts))"
465 if [ "$(printf '\x41')" != 'A' ]; then
470 if _exists
xargs && [ "$(printf %s '\\x41' | xargs printf)" = 'A' ]; then
475 if _exists xxd
&& xxd
-r -p 2>/dev
/null
; then
482 _debug2 _URGLY_PRINTF
"$_URGLY_PRINTF"
483 if [ -z "$_URGLY_PRINTF" ]; then
484 if [ "$_ESCAPE_XARGS" ] && _exists
xargs; then
486 echo "$hex" | _upper_case |
sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/g' |
xargs printf
488 for h
in $
(echo "$hex" | _upper_case |
sed 's/\([0-9A-F]\{2\}\)/ \1/g'); do
496 for c
in $
(echo "$hex" | _upper_case |
sed 's/\([0-9A-F]\)/ \1/g'); do
497 if [ -z "$ic" ]; then
502 ic
="$(_h_char_2_dec "$ic")"
503 jc
="$(_h_char_2_dec "$jc")"
504 printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")""%s"
513 _contains "${__OS__:=$(uname -a)}" "solaris" || _contains "${__OS__:=$(uname -a)}" "SunOS"
517 #this can only process ascii chars, should only be used when od command is missing as a backup way.
519 _debug2 "Using _ascii_hex"
523 while [ "$_h_i" -le "$_str_len" ]; do
524 _str_c="$(printf "%s" "$_str" | cut -c "$_h_i")"
525 printf " %02x" "'$_str_c"
526 _h_i="$
(_math
"$_h_i" + 1)"
530 #stdin output hexstr splited by one space
535 od -A n -v -t x1 | tr -s " " | sed 's/ $//' | tr -d "\r\t\n"
536 elif _exists hexdump; then
537 _debug3 "using
hexdump"
538 hexdump -v -e '/1 ""' -e '/1 " %02x
" ""'
539 elif _exists xxd; then
541 xxd -ps -c 20 -i | sed "s
/ 0x
/ /g
" | tr -d ",\n" | tr -s " "
543 _debug3 "using _ascii_hex
"
549 #url encode, no-preserved chars
550 #A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
551 #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
553 #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
554 #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
556 #0 1 2 3 4 5 6 7 8 9 - _ . ~
557 #30 31 32 33 34 35 36 37 38 39 2d 5f 2e 7e
561 _hex_str=$(_hex_dump)
562 _debug3 "_url_encode
"
563 _debug3 "_hex_str
" "$_hex_str"
564 for _hex_code in $_hex_str; do
566 case "${_hex_code}" in
770 printf '%%%s' "$_hex_code"
780 if [ -z "$filename" ]; then
781 _usage "Usage
:_sed_i options filename
"
784 _debug2 options "$options"
785 if sed -h 2>&1 | grep "\
-i\
[SUFFIX
]" >/dev/null 2>&1; then
786 _debug "Using
sed -i"
787 sed -i "$options" "$filename"
789 _debug "No
-i support
in sed"
790 text="$
(cat "$filename")"
791 echo "$text" | sed "$options" >"$filename"
796 if ! egrep -o "$1" 2>/dev/null; then
797 sed -n 's/.*\('"$1"'\).*/\1/p'
801 #Usage: file startline endline
806 if [ -z "$endline" ]; then
807 _usage "Usage
: file startline endline
"
811 i="$
(grep -n -- "$startline" "$filename" | cut
-d : -f 1)"
813 _err "Can not
find start line
: $startline"
816 i="$
(_math
"$i" + 1)"
819 j="$
(grep -n -- "$endline" "$filename" | cut
-d : -f 1)"
821 _err "Can not
find end line
: $endline"
824 j="$
(_math
"$j" - 1)"
827 sed -n "$i,${j}p
" "$filename"
835 _debug3 "base64 multiline
:'$1'"
836 ${ACME_OPENSSL_BIN:-openssl} base64 -e
838 _debug3 "base64 single line.
"
839 ${ACME_OPENSSL_BIN:-openssl} base64 -e | tr -d '\r\n'
846 ${ACME_OPENSSL_BIN:-openssl} base64 -d -A
848 ${ACME_OPENSSL_BIN:-openssl} base64 -d
855 if [ "$DEBUG" ]; then
856 openssl x509 -noout -text -in "$_cf"
858 openssl x509 -noout -text -in "$_cf" >/dev/null 2>&1
862 #Usage: hashalg [outputhex]
863 #Output Base64-encoded digest
866 if [ -z "$alg" ]; then
867 _usage "Usage
: _digest hashalg
"
873 if [ "$alg" = "sha256
" ] || [ "$alg" = "sha1
" ] || [ "$alg" = "md5
" ]; then
874 if [ "$outputhex" ]; then
875 ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' '
877 ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -binary | _base64
880 _err "$alg is not supported yet
"
886 #Usage: hashalg secret_hex [outputhex]
893 if [ -z "$secret_hex" ]; then
894 _usage "Usage
: _hmac hashalg secret
[outputhex
]"
898 if [ "$alg" = "sha256
" ] || [ "$alg" = "sha1
" ]; then
899 if [ "$outputhex" ]; then
900 (${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -mac HMAC -macopt "hexkey
:$secret_hex" 2>/dev/null || ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hmac "$
(printf "%s" "$secret_hex" | _h2b
)") | cut -d = -f 2 | tr -d ' '
902 ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -mac HMAC -macopt "hexkey
:$secret_hex" -binary 2>/dev/null || ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hmac "$
(printf "%s" "$secret_hex" | _h2b
)" -binary
905 _err "$alg is not supported yet
"
911 #Usage: keyfile hashalg
912 #Output: Base64-encoded signature value
916 if [ -z "$alg" ]; then
917 _usage "Usage
: _sign keyfile hashalg
"
921 _sign_openssl="${ACME_OPENSSL_BIN:-openssl} dgst
-sign $keyfile "
923 if grep "BEGIN RSA PRIVATE KEY
" "$keyfile" >/dev/null 2>&1; then
924 $_sign_openssl -$alg | _base64
925 elif grep "BEGIN EC PRIVATE KEY
" "$keyfile" >/dev/null 2>&1; then
926 if ! _signedECText="$
($_sign_openssl -sha$__ECC_KEY_LEN |
${ACME_OPENSSL_BIN:-openssl} asn1parse
-inform DER
)"; then
927 _err "Sign failed
: $_sign_openssl"
928 _err "Key
file: $keyfile"
929 _err "Key content
:$
(wc -l <"$keyfile") lines
"
932 _debug3 "_signedECText
" "$_signedECText"
933 _ec_r="$
(echo "$_signedECText" | _head_n
2 | _tail_n
1 | cut
-d : -f 4 |
tr -d "\r\n")"
934 _debug3 "_ec_r
" "$_ec_r"
935 _ec_s="$
(echo "$_signedECText" | _head_n
3 | _tail_n
1 | cut
-d : -f 4 |
tr -d "\r\n")"
936 _debug3 "_ec_s
" "$_ec_s"
937 printf "%s
" "$_ec_r$_ec_s" | _h2b | _base64
939 _err "Unknown key
file format.
"
945 #keylength or isEcc flag (empty str => not ecc)
949 if [ -z "$_length" ]; then
953 [ "$_length" != "1024" ] \
954 && [ "$_length" != "2048" ] \
955 && [ "$_length" != "3072" ] \
956 && [ "$_length" != "4096" ] \
957 && [ "$_length" != "8192" ]
960 # _createkey 2048|ec-256 file
964 _debug2 "_createkey
for file:$f"
966 if _startswith "$length" "ec-
"; then
967 length=$(printf "%s
" "$length" | cut -d '-' -f 2-100)
969 if [ "$length" = "256" ]; then
972 if [ "$length" = "384" ]; then
975 if [ "$length" = "521" ]; then
981 if [ -z "$length" ]; then
985 _debug "Use length
$length"
987 if ! touch "$f" >/dev/null 2>&1; then
988 _f_path="$
(dirname "$f")"
989 _debug _f_path "$_f_path"
990 if ! mkdir -p "$_f_path"; then
991 _err "Can not create path
: $_f_path"
996 if _isEccKey "$length"; then
997 _debug "Using ec name
: $eccname"
998 ${ACME_OPENSSL_BIN:-openssl} ecparam -name "$eccname" -genkey 2>/dev/null >"$f"
1000 _debug "Using RSA
: $length"
1001 ${ACME_OPENSSL_BIN:-openssl} genrsa "$length" 2>/dev/null >"$f"
1004 if [ "$?
" != "0" ]; then
1005 _err "Create key error.
"
1013 _debug2 _is_idn_d "$_is_idn_d"
1014 _idn_temp=$(printf "%s
" "$_is_idn_d" | tr -d '0-9' | tr -d 'a-z' | tr -d 'A-Z' | tr -d '*.,-')
1015 _debug2 _idn_temp "$_idn_temp"
1020 #aa.com,bb.com,cc.com
1023 if ! _is_idn "$__idn_d"; then
1024 printf "%s
" "$__idn_d"
1028 if _exists idn; then
1029 if _contains "$__idn_d" ','; then
1031 for f in $(echo "$__idn_d" | tr ',' ' '); do
1032 [ -z "$f" ] && continue
1033 if [ -z "$_i_first" ]; then
1038 idn --quiet "$f" | tr -d "\r\n"
1041 idn "$__idn_d" | tr -d "\r\n"
1044 _err "Please
install idn to process IDN names.
"
1048 #_createcsr cn san_list keyfile csrfile conf acmeValidationv1
1056 acmeValidationv1="$6"
1057 _debug2 domain "$domain"
1058 _debug2 domainlist "$domainlist"
1059 _debug2 csrkey "$csrkey"
1061 _debug2 csrconf "$csrconf"
1063 printf "[ req_distinguished_name
]\n[ req
]\ndistinguished_name
= req_distinguished_name
\nreq_extensions
= v3_req
\n[ v3_req
]\n\nkeyUsage
= nonRepudiation
, digitalSignature
, keyEncipherment
" >"$csrconf"
1065 if [ "$acmeValidationv1" ]; then
1066 printf -- "\nsubjectAltName
=DNS
:$domainlist" >>"$csrconf"
1067 elif [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then
1069 _info "Single domain
" "$domain"
1070 printf -- "\nsubjectAltName
=DNS
:$domain" >>"$csrconf"
1072 domainlist="$
(_idn
"$domainlist")"
1073 _debug2 domainlist "$domainlist"
1074 if _contains "$domainlist" ","; then
1075 alt="DNS
:$domain,DNS
:$
(echo "$domainlist" |
sed "s/,,/,/g" |
sed "s/,/,DNS:/g")"
1077 alt="DNS
:$domain,DNS
:$domainlist"
1080 _info "Multi domain
" "$alt"
1081 printf -- "\nsubjectAltName
=$alt" >>"$csrconf"
1083 if [ "$Le_OCSP_Staple" ] || [ "$Le_OCSP_Stable" ]; then
1084 _savedomainconf Le_OCSP_Staple "$Le_OCSP_Staple"
1085 _cleardomainconf Le_OCSP_Stable
1086 printf -- "\nbasicConstraints
= CA
:FALSE
\n1.3.6.1.5.5.7.1.24=DER
:30:03:02:01:05" >>"$csrconf"
1089 if [ "$acmeValidationv1" ]; then
1090 printf "\n1.3.6.1.5.5.7.1.31=critical
,DER
:04:20:${acmeValidationv1}" >>"${csrconf}"
1093 _csr_cn="$
(_idn
"$domain")"
1094 _debug2 _csr_cn "$_csr_cn"
1095 if _contains "$
(uname
-a)" "MINGW
"; then
1096 ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "//CN
=$_csr_cn" -config "$csrconf" -out "$csr"
1098 ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "/CN
=$_csr_cn" -config "$csrconf" -out "$csr"
1102 #_signcsr key csr conf cert
1110 _msg="$
(${ACME_OPENSSL_BIN:-openssl} x509
-req -days 365 -in "$csr" -signkey "$key" -extensions v3_req
-extfile "$conf" -out "$cert" 2>&1)"
1117 _readSubjectFromCSR() {
1119 if [ -z "$_csrfile" ]; then
1120 _usage "_readSubjectFromCSR mycsr.csr
"
1123 ${ACME_OPENSSL_BIN:-openssl} req -noout -in "$_csrfile" -subject | tr ',' "\n" | _egrep_o "CN
*=.
*" | cut -d = -f 2 | cut -d / -f 1 | tr -d ' \n'
1127 #echo comma separated domain list
1128 _readSubjectAltNamesFromCSR() {
1130 if [ -z "$_csrfile" ]; then
1131 _usage "_readSubjectAltNamesFromCSR mycsr.csr
"
1135 _csrsubj="$
(_readSubjectFromCSR
"$_csrfile")"
1136 _debug _csrsubj "$_csrsubj"
1138 _dnsAltnames="$
(${ACME_OPENSSL_BIN:-openssl} req
-noout -text -in "$_csrfile" |
grep "^ *DNS:.*" |
tr -d ' \n')"
1139 _debug _dnsAltnames "$_dnsAltnames"
1141 if _contains "$_dnsAltnames," "DNS
:$_csrsubj,"; then
1142 _debug "AltNames contains subject
"
1143 _excapedAlgnames="$
(echo "$_dnsAltnames" |
tr '*' '#')"
1144 _debug _excapedAlgnames "$_excapedAlgnames"
1145 _escapedSubject="$
(echo "$_csrsubj" |
tr '*' '#')"
1146 _debug _escapedSubject "$_escapedSubject"
1147 _dnsAltnames="$
(echo "$_excapedAlgnames," |
sed "s/DNS:$_escapedSubject,//g" |
tr '#' '*' |
sed "s/,\$//g")"
1148 _debug _dnsAltnames "$_dnsAltnames"
1150 _debug "AltNames doesn
't contain subject"
1153 echo "$_dnsAltnames" | sed "s/DNS://g"
1157 _readKeyLengthFromCSR() {
1159 if [ -z "$_csrfile" ]; then
1160 _usage "_readKeyLengthFromCSR mycsr.csr"
1164 _outcsr="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile")"
1165 _debug2 _outcsr "$_outcsr"
1166 if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then
1168 echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' '
1171 _rkl="$(echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1)"
1172 if [ "$_rkl" ]; then
1175 echo "$_outcsr" | tr "\t" " " | _egrep_o "RSA Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1
1183 if _exists "ss"; then
1185 ss -ntpl 2>/dev/null | grep ":$_port "
1189 if _exists "netstat"; then
1190 _debug "Using: netstat"
1191 if netstat -h 2>&1 | grep "\-p proto" >/dev/null; then
1192 #for windows version netstat tool
1193 netstat -an -p tcp | grep "LISTENING" | grep ":$_port "
1195 if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then
1196 netstat -an -p tcp | grep LISTEN | grep ":$_port "
1197 elif netstat -help 2>&1 | grep -- '-P protocol
' >/dev/null; then
1199 netstat -an -P tcp | grep "\.$_port " | grep "LISTEN"
1200 elif netstat -help 2>&1 | grep "\-p" >/dev/null; then
1202 netstat -ntpl | grep ":$_port "
1204 #for busybox (embedded linux; no pid support)
1205 netstat -ntl 2>/dev/null | grep ":$_port "
1214 #outfile key cert cacert [password [name [caname]]]
1224 if [ "$pfxCaname" ]; then
1225 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName" -caname "$pfxCaname"
1226 elif [ "$pfxName" ]; then
1227 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName"
1228 elif [ "$pfxPassword" ]; then
1229 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword"
1231 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca"
1236 #domain [password] [isEcc]
1240 if [ -z "$domain" ]; then
1241 _usage "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]"
1247 _initpath "$domain" "$_isEcc"
1249 _toPkcs "$CERT_PFX_PATH" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$pfxPassword"
1251 if [ "$?" = "0" ]; then
1252 _info "Success, Pfx is exported to: $CERT_PFX_PATH"
1261 if [ -z "$domain" ]; then
1262 _usage "Usage: $PROJECT_ENTRY --toPkcs8 -d domain [--ecc]"
1268 _initpath "$domain" "$_isEcc"
1270 ${ACME_OPENSSL_BIN:-openssl} pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in "$CERT_KEY_PATH" -out "$CERT_PKCS8_PATH"
1272 if [ "$?" = "0" ]; then
1273 _info "Success, $CERT_PKCS8_PATH"
1279 createAccountKey() {
1280 _info "Creating account key"
1281 if [ -z "$1" ]; then
1282 _usage "Usage: $PROJECT_ENTRY --createAccountKey --accountkeylength 2048"
1287 _create_account_key "$length"
1291 _create_account_key() {
1295 if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then
1296 _debug "Use default length $DEFAULT_ACCOUNT_KEY_LENGTH"
1297 length="$DEFAULT_ACCOUNT_KEY_LENGTH"
1300 _debug length "$length"
1304 if [ -f "$ACCOUNT_KEY_PATH" ]; then
1305 _info "Account key exists, skip"
1308 #generate account key
1309 _createkey "$length" "$ACCOUNT_KEY_PATH"
1310 chmod 600 "$ACCOUNT_KEY_PATH"
1317 _info "Creating domain key"
1318 if [ -z "$1" ]; then
1319 _usage "Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]"
1326 if [ -z "$_cdl" ]; then
1327 _debug "Use DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH"
1328 _cdl="$DEFAULT_DOMAIN_KEY_LENGTH"
1331 _initpath "$domain" "$_cdl"
1333 if [ ! -f "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$IS_RENEW" ]) || [ "$Le_ForceNewDomainKey" = "1" ]; then
1334 if _createkey "$_cdl" "$CERT_KEY_PATH"; then
1335 _savedomainconf Le_Keylength "$_cdl"
1336 _info "The domain key is here: $(__green $CERT_KEY_PATH)"
1340 if [ "$IS_RENEW" ]; then
1341 _info "Domain key exists, skip"
1344 _err "Domain key exists, do you want to overwrite the key?"
1345 _err "Add '--force', and try again."
1352 # domain domainlist isEcc
1354 _info "Creating csr"
1355 if [ -z "$1" ]; then
1356 _usage "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]"
1364 _initpath "$domain" "$_isEcc"
1366 if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then
1367 _info "CSR exists, skip"
1371 if [ ! -f "$CERT_KEY_PATH" ]; then
1372 _err "The key file is not found: $CERT_KEY_PATH"
1373 _err "Please create the key file first."
1376 _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"
1381 tr '/+' '_-
' | tr -d '= '
1386 if date -u -r "$1" 2>/dev/null; then
1391 if date -u -d@"$1" 2>/dev/null; then
1396 if _exists adb; then
1397 _t_s_a=$(echo "0t${1}=Y" | adb)
1402 if echo "$1" | awk '{ print strftime
("%c", $0); }' 2>/dev/null; then
1408 sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n"
1413 if stat -c '%U
:%G
' "$1" 2>/dev/null; then
1418 if stat -f '%Su
:%Sg
' "$1" 2>/dev/null; then
1422 return 1 #error, 'stat
' not found
1428 if [ -z "$keyfile" ]; then
1429 _usage "Usage: _calcjwk keyfile"
1433 if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then
1434 _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE"
1438 if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
1440 pub_exp=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -noout -text | grep "^publicExponent:" | cut -d '(' -f 2 | cut -d 'x
' -f 2 | cut -d ')' -f 1)
1441 if [ "${#pub_exp}" = "5" ]; then
1444 _debug3 pub_exp "$pub_exp"
1446 e=$(echo "$pub_exp" | _h2b | _base64)
1449 modulus=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2)
1450 _debug3 modulus "$modulus"
1451 n="$(printf "%s" "$modulus" | _h2b | _base64 | _url_replace)"
1454 jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
1457 JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}'
1458 JWK_HEADERPLACE_PART1='{"nonce": "'
1459 JWK_HEADERPLACE_PART2='", "alg": "RS256"'
1460 elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
1462 crv="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
1464 __ECC_KEY_LEN=$(echo "$crv" | cut -d "-" -f 2)
1465 if [ "$__ECC_KEY_LEN" = "521" ]; then
1468 _debug3 __ECC_KEY_LEN "$__ECC_KEY_LEN"
1469 if [ -z "$crv" ]; then
1470 _debug "Let's try ASN1 OID
"
1471 crv_oid="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep "^ASN1 OID:" | cut
-d ":" -f 2 |
tr -d " \r\n")"
1472 _debug3 crv_oid "$crv_oid"
1473 case "${crv_oid}" in
1487 _err "ECC oid
: $crv_oid"
1494 pubi="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep -n pub
: | cut
-d : -f 1)"
1495 pubi=$(_math "$pubi" + 1)
1496 _debug3 pubi "$pubi"
1498 pubj="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep -n "ASN1 OID:" | cut
-d : -f 1)"
1499 pubj=$(_math "$pubj" - 1)
1500 _debug3 pubj "$pubj"
1502 pubtext="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
sed -n "$pubi,${pubj}p" |
tr -d " \n\r")"
1503 _debug3 pubtext "$pubtext"
1505 xlen="$
(printf "%s" "$pubtext" |
tr -d ':' |
wc -c)"
1506 xlen=$(_math "$xlen" / 4)
1507 _debug3 xlen "$xlen"
1509 xend=$(_math "$xlen" + 1)
1510 x="$
(printf "%s" "$pubtext" | cut
-d : -f 2-"$xend")"
1513 x64="$
(printf "%s" "$x" |
tr -d : | _h2b | _base64 | _url_replace
)"
1516 xend=$(_math "$xend" + 1)
1517 y="$
(printf "%s" "$pubtext" | cut
-d : -f "$xend"-10000)"
1520 y64="$
(printf "%s" "$y" |
tr -d : | _h2b | _base64 | _url_replace
)"
1523 jwk='{"crv
": "'$crv'", "kty
": "EC
", "x
": "'$x64'", "y
": "'$y64'"}'
1526 JWK_HEADER='{"alg
": "ES
'$__ECC_KEY_LEN'", "jwk
": '$jwk'}'
1527 JWK_HEADERPLACE_PART1='{"nonce
": "'
1528 JWK_HEADERPLACE_PART2='", "alg
": "ES
'$__ECC_KEY_LEN'"'
1530 _err "Only RSA or EC key is supported. keyfile
=$keyfile"
1531 _debug2 "$
(cat "$keyfile")"
1535 _debug3 JWK_HEADER "$JWK_HEADER"
1536 __CACHED_JWK_KEY_FILE="$keyfile"
1544 date -u "+%Y-
%m-
%d
%H
:%M
:%S
"
1548 if _exists mktemp; then
1549 if mktemp 2>/dev/null; then
1551 elif _contains "$
(mktemp
2>&1)" "-t prefix
" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then
1556 if [ -d "/tmp
" ]; then
1557 echo "/tmp
/${PROJECT_NAME}wefADf24sf.$
(_time
).tmp
"
1559 elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then
1560 echo "/$LE_TEMP_DIR/wefADf24sf.$
(_time
).tmp
"
1563 _err "Can not create temp
file.
"
1568 if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then
1569 HTTP_HEADER="$
(_mktemp
)"
1570 _debug2 HTTP_HEADER "$HTTP_HEADER"
1573 if [ "$__HTTP_INITIALIZED" ]; then
1574 if [ "$_ACME_CURL$_ACME_WGET" ]; then
1575 _debug2 "Http already initialized.
"
1580 if [ -z "$_ACME_CURL" ] && _exists "curl
"; then
1581 _ACME_CURL="curl
-L --silent --dump-header $HTTP_HEADER "
1582 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1583 _CURL_DUMP="$
(_mktemp
)"
1584 _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP "
1587 if [ "$CA_PATH" ]; then
1588 _ACME_CURL="$_ACME_CURL --capath $CA_PATH "
1589 elif [ "$CA_BUNDLE" ]; then
1590 _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE "
1593 if _contains "$
(curl
--help 2>&1)" "--globoff"; then
1594 _ACME_CURL="$_ACME_CURL -g "
1598 if [ -z "$_ACME_WGET" ] && _exists "wget
"; then
1599 _ACME_WGET="wget
-q"
1600 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1601 _ACME_WGET="$_ACME_WGET -d "
1603 if [ "$CA_PATH" ]; then
1604 _ACME_WGET="$_ACME_WGET --ca-directory=$CA_PATH "
1605 elif [ "$CA_BUNDLE" ]; then
1606 _ACME_WGET="$_ACME_WGET --ca-certificate=$CA_BUNDLE "
1610 #from wget 1.14: do not skip body on 404 error
1611 if [ "$_ACME_WGET" ] && _contains "$
($_ACME_WGET --help 2>&1)" "--content-on-error"; then
1612 _ACME_WGET="$_ACME_WGET --content-on-error "
1615 __HTTP_INITIALIZED=1
1619 # body url [needbase64] [POST|PUT|DELETE] [ContentType]
1625 _postContentType="$5"
1627 if [ -z "$httpmethod" ]; then
1631 _debug "_post_url
" "$_post_url"
1632 _debug2 "body
" "$body"
1633 _debug2 "_postContentType
" "$_postContentType"
1637 if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
1639 if [ "$HTTPS_INSECURE" ]; then
1640 _CURL="$_CURL --insecure "
1642 _debug "_CURL
" "$_CURL"
1643 if [ "$needbase64" ]; then
1644 if [ "$_postContentType" ]; then
1645 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64
)"
1647 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64
)"
1650 if [ "$_postContentType" ]; then
1651 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")"
1653 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")"
1657 if [ "$_ret" != "0" ]; then
1658 _err "Please refer to https
://curl.haxx.se
/libcurl
/c
/libcurl-errors.html
for error code
: $_ret"
1659 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1660 _err "Here is the curl dump log
:"
1661 _err "$
(cat "$_CURL_DUMP")"
1664 elif [ "$_ACME_WGET" ]; then
1666 if [ "$HTTPS_INSECURE" ]; then
1667 _WGET="$_WGET --no-check-certificate "
1669 _debug "_WGET
" "$_WGET"
1670 if [ "$needbase64" ]; then
1671 if [ "$httpmethod" = "POST
" ]; then
1672 if [ "$_postContentType" ]; then
1673 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64
)"
1675 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64
)"
1678 if [ "$_postContentType" ]; then
1679 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64
)"
1681 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64
)"
1685 if [ "$httpmethod" = "POST
" ]; then
1686 if [ "$_postContentType" ]; then
1687 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
1689 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
1692 if [ "$_postContentType" ]; then
1693 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
1695 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
1700 if [ "$_ret" = "8" ]; then
1702 _debug "wget returns
8, the server returns a
'Bad request' response
, lets process the response later.
"
1704 if [ "$_ret" != "0" ]; then
1705 _err "Please refer to https
://www.gnu.org
/software
/wget
/manual
/html_node
/Exit-Status.html
for error code
: $_ret"
1707 _sed_i "s
/^
*//g
" "$HTTP_HEADER"
1710 _err "Neither curl nor wget is found
, can not
do $httpmethod.
"
1712 _debug "_ret
" "$_ret"
1713 printf "%s
" "$response"
1717 # url getheader timeout
1728 if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
1730 if [ "$HTTPS_INSECURE" ]; then
1731 _CURL="$_CURL --insecure "
1734 _CURL="$_CURL --connect-timeout $t"
1736 _debug "_CURL
" "$_CURL"
1737 if [ "$onlyheader" ]; then
1738 $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
1740 $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
1743 if [ "$ret" != "0" ]; then
1744 _err "Please refer to https
://curl.haxx.se
/libcurl
/c
/libcurl-errors.html
for error code
: $ret"
1745 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1746 _err "Here is the curl dump log
:"
1747 _err "$
(cat "$_CURL_DUMP")"
1750 elif [ "$_ACME_WGET" ]; then
1752 if [ "$HTTPS_INSECURE" ]; then
1753 _WGET="$_WGET --no-check-certificate "
1756 _WGET="$_WGET --timeout=$t"
1758 _debug "_WGET
" "$_WGET"
1759 if [ "$onlyheader" ]; then
1760 $_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'
1762 $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url"
1765 if [ "$ret" = "8" ]; then
1767 _debug "wget returns
8, the server returns a
'Bad request' response
, lets process the response later.
"
1769 if [ "$ret" != "0" ]; then
1770 _err "Please refer to https
://www.gnu.org
/software
/wget
/manual
/html_node
/Exit-Status.html
for error code
: $ret"
1774 _err "Neither curl nor wget is found
, can not
do GET.
"
1785 if ! tail -n "$1" 2>/dev/null; then
1791 # url payload needbase64 keyfile
1792 _send_signed_request() {
1797 if [ -z "$keyfile" ]; then
1798 keyfile="$ACCOUNT_KEY_PATH"
1801 _debug payload "$payload"
1803 if ! _calcjwk "$keyfile"; then
1807 __request_conent_type="$CONTENT_TYPE_JSON"
1809 payload64=$(printf "%s
" "$payload" | _base64 | _url_replace)
1810 _debug3 payload64 "$payload64"
1812 MAX_REQUEST_RETRY_TIMES=20
1814 _request_retry_times=0
1815 while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
1816 _request_retry_times=$(_math "$_request_retry_times" + 1)
1817 _debug3 _request_retry_times "$_request_retry_times"
1818 if [ -z "$_CACHED_NONCE" ]; then
1820 if [ "$ACME_NEW_NONCE" ]; then
1821 _debug2 "Get nonce with HEAD. ACME_NEW_NONCE
" "$ACME_NEW_NONCE"
1822 nonceurl="$ACME_NEW_NONCE"
1823 if _post "" "$nonceurl" "" "HEAD
" "$__request_conent_type"; then
1824 _headers="$
(cat "$HTTP_HEADER")"
1827 if [ -z "$_headers" ]; then
1828 _debug2 "Get nonce with GET. ACME_DIRECTORY
" "$ACME_DIRECTORY"
1829 nonceurl="$ACME_DIRECTORY"
1830 _headers="$
(_get
"$nonceurl" "onlyheader")"
1833 if [ "$?
" != "0" ]; then
1834 _err "Can not connect to
$nonceurl to get nonce.
"
1838 _debug2 _headers "$_headers"
1840 _CACHED_NONCE="$
(echo "$_headers" |
grep "Replay-Nonce:" | _head_n
1 |
tr -d "\r\n " | cut
-d ':' -f 2)"
1841 _debug2 _CACHED_NONCE "$_CACHED_NONCE"
1843 _debug2 "Use _CACHED_NONCE
" "$_CACHED_NONCE"
1845 nonce="$_CACHED_NONCE"
1846 _debug2 nonce "$nonce"
1847 if [ -z "$nonce" ]; then
1848 _info "Could not get nonce
, let's try again."
1852 if [ "$ACME_VERSION" = "2" ]; then
1853 if [ "$url" = "$ACME_NEW_ACCOUNT" ] || [ "$url" = "$ACME_REVOKE_CERT" ]; then
1854 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}'
1856 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"kid\": \"${ACCOUNT_URL}\""'}'
1859 protected
="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}'
1861 _debug3 protected
"$protected"
1863 protected64
="$(printf "%s
" "$protected" | _base64 | _url_replace)"
1864 _debug3 protected64
"$protected64"
1866 if ! _sig_t
="$(printf "%s
" "$protected64.
$payload64" | _sign "$keyfile" "sha256
")"; then
1867 _err
"Sign request failed."
1870 _debug3 _sig_t
"$_sig_t"
1872 sig
="$(printf "%s
" "$_sig_t" | _url_replace)"
1875 body
="{\"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
1876 _debug3 body
"$body"
1878 response
="$(_post "$body" "$url" "$needbase64" "POST
" "$__request_conent_type")"
1881 if [ "$?" != "0" ]; then
1882 _err
"Can not post to $url"
1885 _debug2 original
"$response"
1886 response
="$(echo "$response" | _normalizeJson)"
1888 responseHeaders
="$(cat "$HTTP_HEADER")"
1890 _debug2 responseHeaders
"$responseHeaders"
1891 _debug2 response
"$response"
1892 code
="$(grep "^HTTP
" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")"
1895 _CACHED_NONCE
="$(echo "$responseHeaders" | grep "Replay-Nonce
:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
1898 if [ "$needbase64" ]; then
1899 _body
="$(echo "$_body" | _dbase64 | tr -d '\0')"
1900 _debug3 _body
"$_body"
1903 if _contains
"$_body" "JWS has invalid anti-replay nonce" || _contains
"$_body" "JWS has an invalid anti-replay nonce"; then
1904 _info
"It seems the CA server is busy now, let's wait and retry. Sleeping $_sleep_retry_sec seconds."
1906 _sleep
$_sleep_retry_sec
1914 #setopt "file" "opt" "=" "value" [";"]
1921 if [ -z "$__opt" ]; then
1922 _usage usage
: _setopt
'"file" "opt" "=" "value" [";"]'
1925 if [ ! -f "$__conf" ]; then
1929 if grep -n "^$__opt$__sep" "$__conf" >/dev
/null
; then
1931 if _contains
"$__val" "&"; then
1932 __val
="$(echo "$__val" | sed 's/&/\\&/g')"
1934 text
="$(cat "$__conf")"
1935 printf -- "%s\n" "$text" |
sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
1937 elif grep -n "^#$__opt$__sep" "$__conf" >/dev
/null
; then
1938 if _contains
"$__val" "&"; then
1939 __val
="$(echo "$__val" | sed 's/&/\\&/g')"
1941 text
="$(cat "$__conf")"
1942 printf -- "%s\n" "$text" |
sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
1946 echo "$__opt$__sep$__val$__end" >>"$__conf"
1948 _debug3
"$(grep -n "^
$__opt$__sep" "$__conf")"
1951 #_save_conf file key value
1957 if [ "$_s_c_f" ]; then
1958 _setopt
"$_s_c_f" "$_sdkey" "=" "'$_sdvalue'"
1960 _err
"config file is empty, can not save $_sdkey=$_sdvalue"
1964 #_clear_conf file key
1968 if [ "$_c_c_f" ]; then
1969 _conf_data
="$(cat "$_c_c_f")"
1970 echo "$_conf_data" |
sed "s/^$_sdkey *=.*$//" >"$_c_c_f"
1972 _err
"config file is empty, can not clear"
1976 #_read_conf file key
1980 if [ -f "$_r_c_f" ]; then
1982 eval "$(grep "^
$_sdkey *=" "$_r_c_f")"
1983 eval "printf \"%s\" \"\$$_sdkey\""
1986 _debug
"config file is empty, can not read $_sdkey"
1990 #_savedomainconf key value
1991 #save to domain.conf
1993 _save_conf
"$DOMAIN_CONF" "$1" "$2"
1996 #_cleardomainconf key
1997 _cleardomainconf
() {
1998 _clear_conf
"$DOMAIN_CONF" "$1"
2001 #_readdomainconf key
2003 _read_conf
"$DOMAIN_CONF" "$1"
2006 #_saveaccountconf key value
2007 _saveaccountconf
() {
2008 _save_conf
"$ACCOUNT_CONF_PATH" "$1" "$2"
2012 _saveaccountconf_mutable
() {
2013 _save_conf
"$ACCOUNT_CONF_PATH" "SAVED_$1" "$2"
2015 _clearaccountconf
"$1"
2019 _readaccountconf
() {
2020 _read_conf
"$ACCOUNT_CONF_PATH" "$1"
2024 _readaccountconf_mutable
() {
2026 _readaccountconf
"SAVED_$_rac_key"
2029 #_clearaccountconf key
2030 _clearaccountconf
() {
2031 _clear_conf
"$ACCOUNT_CONF_PATH" "$1"
2034 #_savecaconf key value
2036 _save_conf
"$CA_CONF" "$1" "$2"
2041 _read_conf
"$CA_CONF" "$1"
2044 #_clearaccountconf key
2046 _clear_conf
"$CA_CONF" "$1"
2049 # content localaddress
2053 _debug
"ncaddr" "$ncaddr"
2055 _debug
"startserver: $$"
2057 _debug Le_HTTPPort
"$Le_HTTPPort"
2058 _debug Le_Listen_V4
"$Le_Listen_V4"
2059 _debug Le_Listen_V6
"$Le_Listen_V6"
2062 if [ "$Le_Listen_V4" ]; then
2064 elif [ "$Le_Listen_V6" ]; then
2068 if [ "$DEBUG" ] && [ "$DEBUG" -gt "1" ]; then
2072 SOCAT_OPTIONS
=TCP-LISTEN
:$Le_HTTPPort,crlf
,reuseaddr
,fork
2074 #Adding bind to local-address
2075 if [ "$ncaddr" ]; then
2076 SOCAT_OPTIONS
="$SOCAT_OPTIONS,bind=${ncaddr}"
2079 _debug
"_NC" "$_NC $SOCAT_OPTIONS"
2080 $_NC $SOCAT_OPTIONS SYSTEM
:"sleep 1; echo HTTP/1.0 200 OK; echo ; echo $content; echo;" &
2087 if [ -z "$pid" ]; then
2098 if [ "$__INTERACTIVE" ]; then
2099 _sleep_c
="$_sleep_sec"
2100 while [ "$_sleep_c" -ge "0" ]; do
2103 _sleep_c
="$(_math "$_sleep_c" - 1)"
2112 # _starttlsserver san_a san_b port content _ncaddr acmeValidationv1
2114 _info
"Starting tls server."
2120 acmeValidationv1
="$6"
2122 _debug san_a
"$san_a"
2123 _debug san_b
"$san_b"
2125 _debug acmeValidationv1
"$acmeValidationv1"
2128 if ! _createkey
"2048" "$TLS_KEY"; then
2129 _err
"Create tls validation key error."
2135 if [ "$san_b" ]; then
2138 if ! _createcsr
"tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$acmeValidationv1"; then
2139 _err
"Create tls validation csr error."
2144 if ! _signcsr
"$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then
2145 _err
"Create tls validation cert error."
2149 __S_OPENSSL
="${ACME_OPENSSL_BIN:-openssl} s_server -www -cert $TLS_CERT -key $TLS_KEY "
2150 if [ "$opaddr" ]; then
2151 __S_OPENSSL
="$__S_OPENSSL -accept $opaddr:$port"
2153 __S_OPENSSL
="$__S_OPENSSL -accept $port"
2156 _debug Le_Listen_V4
"$Le_Listen_V4"
2157 _debug Le_Listen_V6
"$Le_Listen_V6"
2158 if [ "$Le_Listen_V4" ]; then
2159 __S_OPENSSL
="$__S_OPENSSL -4"
2160 elif [ "$Le_Listen_V6" ]; then
2161 __S_OPENSSL
="$__S_OPENSSL -6"
2164 if [ "$acmeValidationv1" ]; then
2165 __S_OPENSSL
="$__S_OPENSSL -alpn acme-tls/1"
2168 _debug
"$__S_OPENSSL"
2169 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
2170 $__S_OPENSSL -tlsextdebug &
2172 $__S_OPENSSL >/dev
/null
2>&1 &
2177 _debug serverproc
"$serverproc"
2183 if ! readlink
-f "$_rf" 2>/dev
/null
; then
2184 if _startswith
"$_rf" "/"; then
2188 echo "$(pwd)/$_rf" | _conapath
2197 if [ -z "$_SCRIPT_HOME" ]; then
2198 if _exists readlink
&& _exists
dirname; then
2199 _debug
"Lets find script dir."
2200 _debug
"_SCRIPT_" "$_SCRIPT_"
2201 _script
="$(_readlink "$_SCRIPT_")"
2202 _debug
"_script" "$_script"
2203 _script_home
="$(dirname "$_script")"
2204 _debug
"_script_home" "$_script_home"
2205 if [ -d "$_script_home" ]; then
2206 _SCRIPT_HOME
="$_script_home"
2208 _err
"It seems the script home is not correct:$_script_home"
2213 # if [ -z "$LE_WORKING_DIR" ]; then
2214 # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then
2215 # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME"
2216 # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
2218 # LE_WORKING_DIR="$_SCRIPT_HOME"
2222 if [ -z "$LE_WORKING_DIR" ]; then
2223 _debug
"Using default home:$DEFAULT_INSTALL_HOME"
2224 LE_WORKING_DIR
="$DEFAULT_INSTALL_HOME"
2226 export LE_WORKING_DIR
2228 if [ -z "$LE_CONFIG_HOME" ]; then
2229 LE_CONFIG_HOME
="$LE_WORKING_DIR"
2231 _debug
"Using config home:$LE_CONFIG_HOME"
2232 export LE_CONFIG_HOME
2234 _DEFAULT_ACCOUNT_CONF_PATH
="$LE_CONFIG_HOME/account.conf"
2236 if [ -z "$ACCOUNT_CONF_PATH" ]; then
2237 if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
2238 .
"$_DEFAULT_ACCOUNT_CONF_PATH"
2242 if [ -z "$ACCOUNT_CONF_PATH" ]; then
2243 ACCOUNT_CONF_PATH
="$_DEFAULT_ACCOUNT_CONF_PATH"
2246 DEFAULT_LOG_FILE
="$LE_CONFIG_HOME/$PROJECT_NAME.log"
2248 DEFAULT_CA_HOME
="$LE_CONFIG_HOME/ca"
2250 if [ -z "$LE_TEMP_DIR" ]; then
2251 LE_TEMP_DIR
="$LE_CONFIG_HOME/tmp"
2257 _api_server
="${1:-$ACME_DIRECTORY}"
2258 _debug
"_init api for server: $_api_server"
2260 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2261 response
=$
(_get
"$_api_server")
2262 if [ "$?" != "0" ]; then
2263 _debug2
"response" "$response"
2264 _err
"Can not init api."
2267 _debug2
"response" "$response"
2269 ACME_KEY_CHANGE
=$
(echo "$response" | _egrep_o
'key-change" *: *"[^"]*"' | cut
-d '"' -f 3)
2270 if [ -z "$ACME_KEY_CHANGE" ]; then
2271 ACME_KEY_CHANGE
=$
(echo "$response" | _egrep_o
'keyChange" *: *"[^"]*"' | cut
-d '"' -f 3)
2273 export ACME_KEY_CHANGE
2275 ACME_NEW_AUTHZ
=$
(echo "$response" | _egrep_o
'new-authz" *: *"[^"]*"' | cut
-d '"' -f 3)
2276 if [ -z "$ACME_NEW_AUTHZ" ]; then
2277 ACME_NEW_AUTHZ
=$
(echo "$response" | _egrep_o
'newAuthz" *: *"[^"]*"' | cut
-d '"' -f 3)
2279 export ACME_NEW_AUTHZ
2281 ACME_NEW_ORDER
=$
(echo "$response" | _egrep_o
'new-cert" *: *"[^"]*"' | cut
-d '"' -f 3)
2282 ACME_NEW_ORDER_RES
="new-cert"
2283 if [ -z "$ACME_NEW_ORDER" ]; then
2284 ACME_NEW_ORDER
=$
(echo "$response" | _egrep_o
'new-order" *: *"[^"]*"' | cut
-d '"' -f 3)
2285 ACME_NEW_ORDER_RES
="new-order"
2286 if [ -z "$ACME_NEW_ORDER" ]; then
2287 ACME_NEW_ORDER
=$
(echo "$response" | _egrep_o
'newOrder" *: *"[^"]*"' | cut
-d '"' -f 3)
2290 export ACME_NEW_ORDER
2291 export ACME_NEW_ORDER_RES
2293 ACME_NEW_ACCOUNT
=$
(echo "$response" | _egrep_o
'new-reg" *: *"[^"]*"' | cut
-d '"' -f 3)
2294 ACME_NEW_ACCOUNT_RES
="new-reg"
2295 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2296 ACME_NEW_ACCOUNT
=$
(echo "$response" | _egrep_o
'new-account" *: *"[^"]*"' | cut
-d '"' -f 3)
2297 ACME_NEW_ACCOUNT_RES
="new-account"
2298 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2299 ACME_NEW_ACCOUNT
=$
(echo "$response" | _egrep_o
'newAccount" *: *"[^"]*"' | cut
-d '"' -f 3)
2300 if [ "$ACME_NEW_ACCOUNT" ]; then
2301 export ACME_VERSION
=2
2305 export ACME_NEW_ACCOUNT
2306 export ACME_NEW_ACCOUNT_RES
2308 ACME_REVOKE_CERT
=$
(echo "$response" | _egrep_o
'revoke-cert" *: *"[^"]*"' | cut
-d '"' -f 3)
2309 if [ -z "$ACME_REVOKE_CERT" ]; then
2310 ACME_REVOKE_CERT
=$
(echo "$response" | _egrep_o
'revokeCert" *: *"[^"]*"' | cut
-d '"' -f 3)
2312 export ACME_REVOKE_CERT
2314 ACME_NEW_NONCE
=$
(echo "$response" | _egrep_o
'new-nonce" *: *"[^"]*"' | cut
-d '"' -f 3)
2315 if [ -z "$ACME_NEW_NONCE" ]; then
2316 ACME_NEW_NONCE
=$
(echo "$response" | _egrep_o
'newNonce" *: *"[^"]*"' | cut
-d '"' -f 3)
2318 export ACME_NEW_NONCE
2320 ACME_AGREEMENT
=$
(echo "$response" | _egrep_o
'terms-of-service" *: *"[^"]*"' | cut
-d '"' -f 3)
2321 if [ -z "$ACME_AGREEMENT" ]; then
2322 ACME_AGREEMENT
=$
(echo "$response" | _egrep_o
'termsOfService" *: *"[^"]*"' | cut
-d '"' -f 3)
2324 export ACME_AGREEMENT
2326 _debug
"ACME_KEY_CHANGE" "$ACME_KEY_CHANGE"
2327 _debug
"ACME_NEW_AUTHZ" "$ACME_NEW_AUTHZ"
2328 _debug
"ACME_NEW_ORDER" "$ACME_NEW_ORDER"
2329 _debug
"ACME_NEW_ACCOUNT" "$ACME_NEW_ACCOUNT"
2330 _debug
"ACME_REVOKE_CERT" "$ACME_REVOKE_CERT"
2331 _debug
"ACME_AGREEMENT" "$ACME_AGREEMENT"
2332 _debug
"ACME_NEW_NONCE" "$ACME_NEW_NONCE"
2333 _debug
"ACME_VERSION" "$ACME_VERSION"
2338 #[domain] [keylength or isEcc flag]
2345 if [ -f "$ACCOUNT_CONF_PATH" ]; then
2346 .
"$ACCOUNT_CONF_PATH"
2349 if [ "$IN_CRON" ]; then
2350 if [ ! "$_USER_PATH_EXPORTED" ]; then
2351 _USER_PATH_EXPORTED
=1
2352 export PATH
="$USER_PATH:$PATH"
2356 if [ -z "$CA_HOME" ]; then
2357 CA_HOME
="$DEFAULT_CA_HOME"
2360 if [ "$ACME_VERSION" = "2" ]; then
2361 DEFAULT_CA
="$LETSENCRYPT_CA_V2"
2362 DEFAULT_STAGING_CA
="$LETSENCRYPT_STAGING_CA_V2"
2365 if [ -z "$ACME_DIRECTORY" ]; then
2366 if [ -z "$STAGE" ]; then
2367 ACME_DIRECTORY
="$DEFAULT_CA"
2369 ACME_DIRECTORY
="$DEFAULT_STAGING_CA"
2370 _info
"Using stage ACME_DIRECTORY: $ACME_DIRECTORY"
2374 _debug ACME_DIRECTORY
"$ACME_DIRECTORY"
2375 _ACME_SERVER_HOST
="$(echo "$ACME_DIRECTORY" | cut -d : -f 2 | tr -s / | cut -d / -f 2)"
2376 _debug2
"_ACME_SERVER_HOST" "$_ACME_SERVER_HOST"
2378 CA_DIR
="$CA_HOME/$_ACME_SERVER_HOST"
2380 _DEFAULT_CA_CONF
="$CA_DIR/ca.conf"
2382 if [ -z "$CA_CONF" ]; then
2383 CA_CONF
="$_DEFAULT_CA_CONF"
2385 _debug3 CA_CONF
"$CA_CONF"
2387 if [ -f "$CA_CONF" ]; then
2391 if [ -z "$ACME_DIR" ]; then
2392 ACME_DIR
="/home/.acme"
2395 if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
2396 APACHE_CONF_BACKUP_DIR
="$LE_CONFIG_HOME"
2399 if [ -z "$USER_AGENT" ]; then
2400 USER_AGENT
="$DEFAULT_USER_AGENT"
2403 if [ -z "$HTTP_HEADER" ]; then
2404 HTTP_HEADER
="$LE_CONFIG_HOME/http.header"
2407 _OLD_ACCOUNT_KEY
="$LE_WORKING_DIR/account.key"
2408 _OLD_ACCOUNT_JSON
="$LE_WORKING_DIR/account.json"
2410 _DEFAULT_ACCOUNT_KEY_PATH
="$CA_DIR/account.key"
2411 _DEFAULT_ACCOUNT_JSON_PATH
="$CA_DIR/account.json"
2412 if [ -z "$ACCOUNT_KEY_PATH" ]; then
2413 ACCOUNT_KEY_PATH
="$_DEFAULT_ACCOUNT_KEY_PATH"
2416 if [ -z "$ACCOUNT_JSON_PATH" ]; then
2417 ACCOUNT_JSON_PATH
="$_DEFAULT_ACCOUNT_JSON_PATH"
2420 _DEFAULT_CERT_HOME
="$LE_CONFIG_HOME"
2421 if [ -z "$CERT_HOME" ]; then
2422 CERT_HOME
="$_DEFAULT_CERT_HOME"
2425 if [ -z "$ACME_OPENSSL_BIN" ] ||
[ ! -f "$ACME_OPENSSL_BIN" ] ||
[ ! -x "$ACME_OPENSSL_BIN" ]; then
2426 ACME_OPENSSL_BIN
="$DEFAULT_OPENSSL_BIN"
2429 if [ -z "$domain" ]; then
2433 if [ -z "$DOMAIN_PATH" ]; then
2434 domainhome
="$CERT_HOME/$domain"
2435 domainhomeecc
="$CERT_HOME/$domain$ECC_SUFFIX"
2437 DOMAIN_PATH
="$domainhome"
2439 if _isEccKey
"$_ilength"; then
2440 DOMAIN_PATH
="$domainhomeecc"
2442 if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
2443 _info
"The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert."
2446 _debug DOMAIN_PATH
"$DOMAIN_PATH"
2449 if [ -z "$DOMAIN_BACKUP_PATH" ]; then
2450 DOMAIN_BACKUP_PATH
="$DOMAIN_PATH/backup"
2453 if [ -z "$DOMAIN_CONF" ]; then
2454 DOMAIN_CONF
="$DOMAIN_PATH/$domain.conf"
2457 if [ -z "$DOMAIN_SSL_CONF" ]; then
2458 DOMAIN_SSL_CONF
="$DOMAIN_PATH/$domain.csr.conf"
2461 if [ -z "$CSR_PATH" ]; then
2462 CSR_PATH
="$DOMAIN_PATH/$domain.csr"
2464 if [ -z "$CERT_KEY_PATH" ]; then
2465 CERT_KEY_PATH
="$DOMAIN_PATH/$domain.key"
2467 if [ -z "$CERT_PATH" ]; then
2468 CERT_PATH
="$DOMAIN_PATH/$domain.cer"
2470 if [ -z "$CA_CERT_PATH" ]; then
2471 CA_CERT_PATH
="$DOMAIN_PATH/ca.cer"
2473 if [ -z "$CERT_FULLCHAIN_PATH" ]; then
2474 CERT_FULLCHAIN_PATH
="$DOMAIN_PATH/fullchain.cer"
2476 if [ -z "$CERT_PFX_PATH" ]; then
2477 CERT_PFX_PATH
="$DOMAIN_PATH/$domain.pfx"
2479 if [ -z "$CERT_PKCS8_PATH" ]; then
2480 CERT_PKCS8_PATH
="$DOMAIN_PATH/$domain.pkcs8"
2483 if [ -z "$TLS_CONF" ]; then
2484 TLS_CONF
="$DOMAIN_PATH/tls.validation.conf"
2486 if [ -z "$TLS_CERT" ]; then
2487 TLS_CERT
="$DOMAIN_PATH/tls.validation.cert"
2489 if [ -z "$TLS_KEY" ]; then
2490 TLS_KEY
="$DOMAIN_PATH/tls.validation.key"
2492 if [ -z "$TLS_CSR" ]; then
2493 TLS_CSR
="$DOMAIN_PATH/tls.validation.csr"
2499 if [ -z "$_EXEC_TEMP_ERR" ]; then
2500 _EXEC_TEMP_ERR
="$(_mktemp)"
2503 if [ "$_EXEC_TEMP_ERR" ]; then
2504 eval "$@ 2>>$_EXEC_TEMP_ERR"
2511 [ "$_EXEC_TEMP_ERR" ] && _err
"$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR"
2515 _APACHECTL
="apachectl"
2516 if ! _exists apachectl
; then
2517 if _exists apache2ctl
; then
2518 _APACHECTL
="apache2ctl"
2520 _err
"'apachectl not found. It seems that apache is not installed, or you are not root user.'"
2521 _err
"Please use webroot mode to try again."
2526 if ! _exec
$_APACHECTL -V >/dev
/null
; then
2531 if [ "$APACHE_HTTPD_CONF" ]; then
2532 _saveaccountconf APACHE_HTTPD_CONF
"$APACHE_HTTPD_CONF"
2533 httpdconf
="$APACHE_HTTPD_CONF"
2534 httpdconfname
="$(basename "$httpdconfname")"
2536 httpdconfname
="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
2537 _debug httpdconfname "$httpdconfname"
2539 if [ -z "$httpdconfname" ]; then
2540 _err "Can not read apache config file."
2544 if _startswith "$httpdconfname" '/'; then
2545 httpdconf="$httpdconfname"
2546 httpdconfname="$(basename "$httpdconfname")"
2548 httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
2549 _debug httpdroot
"$httpdroot"
2550 httpdconf
="$httpdroot/$httpdconfname"
2551 httpdconfname
="$(basename "$httpdconfname")"
2554 _debug httpdconf
"$httpdconf"
2555 _debug httpdconfname
"$httpdconfname"
2556 if [ ! -f "$httpdconf" ]; then
2557 _err
"Apache Config file not found" "$httpdconf"
2564 if [ -z "$usingApache" ]; then
2568 if ! _apachePath
; then
2572 if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
2573 _debug
"No config file to restore."
2577 cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
2578 _debug
"Restored: $httpdconf."
2579 if ! _exec
$_APACHECTL -t; then
2581 _err
"Sorry, restore apache config error, please contact me."
2584 _debug
"Restored successfully."
2585 rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
2591 if ! _apachePath
; then
2595 #test the conf first
2596 _info
"Checking if there is an error in the apache config file before starting."
2598 if ! _exec
"$_APACHECTL" -t >/dev
/null
; then
2600 _err
"The apache config file has error, please fix it first, then try again."
2601 _err
"Don't worry, there is nothing changed to your system."
2608 _debug
"Backup apache config file" "$httpdconf"
2609 if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
2610 _err
"Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
2611 _err
"This might be a bug of $PROJECT_NAME , please report issue: $PROJECT"
2614 _info
"JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
2615 _info
"In case there is an error that can not be restored automatically, you may try restore it yourself."
2616 _info
"The backup file will be deleted on success, just forget it."
2620 apacheVer
="$($_APACHECTL -V | grep "Server version
:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
2621 _debug
"apacheVer" "$apacheVer"
2622 apacheMajer
="$(echo "$apacheVer" | cut -d . -f 1)"
2623 apacheMinor
="$(echo "$apacheVer" | cut -d . -f 2)"
2625 if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ]; then
2627 Alias /.well-known/acme-challenge $ACME_DIR
2629 <Directory $ACME_DIR >
2635 Alias /.well-known/acme-challenge $ACME_DIR
2637 <Directory $ACME_DIR >
2644 _msg
="$($_APACHECTL -t 2>&1)"
2645 if [ "$?" != "0" ]; then
2646 _err
"Sorry, apache config error"
2647 if _restoreApache
; then
2648 _err
"The apache config file is restored."
2650 _err
"Sorry, The apache config file can not be restored, please report bug."
2655 if [ ! -d "$ACME_DIR" ]; then
2656 mkdir
-p "$ACME_DIR"
2657 chmod 755 "$ACME_DIR"
2660 if ! _exec
"$_APACHECTL" graceful
; then
2662 _err
"$_APACHECTL graceful error, please contact me."
2670 #find the real nginx conf file
2673 #returns the real nginx conf file
2679 FOUND_REAL_NGINX_CONF
=""
2680 FOUND_REAL_NGINX_CONF_LN
=""
2681 BACKUP_NGINX_CONF
=""
2682 _debug _croot
"$_croot"
2683 _start_f
="$(echo "$_croot" | cut -d : -f 2)"
2684 _debug _start_f
"$_start_f"
2685 if [ -z "$_start_f" ]; then
2686 _debug
"find start conf from nginx command"
2687 if [ -z "$NGINX_CONF" ]; then
2688 if ! _exists
"nginx"; then
2689 _err
"nginx command is not found."
2692 NGINX_CONF
="$(nginx -V 2>&1 | _egrep_o "--conf-path=[^
]* " | tr -d " ")"
2693 _debug NGINX_CONF
"$NGINX_CONF"
2694 NGINX_CONF
="$(echo "$NGINX_CONF" | cut -d = -f 2)"
2695 _debug NGINX_CONF
"$NGINX_CONF"
2696 if [ ! -f "$NGINX_CONF" ]; then
2697 _err
"'$NGINX_CONF' doesn't exist."
2701 _debug
"Found nginx conf file:$NGINX_CONF"
2703 _start_f
="$NGINX_CONF"
2705 _debug
"Start detect nginx conf for $_d from:$_start_f"
2706 if ! _checkConf
"$_d" "$_start_f"; then
2707 _err
"Can not find conf file for domain $d"
2710 _info
"Found conf file: $FOUND_REAL_NGINX_CONF"
2712 _ln
=$FOUND_REAL_NGINX_CONF_LN
2715 _lnn
=$
(_math
$_ln + 1)
2717 _start_tag
="$(sed -n "$_lnn,${_lnn}p
" "$FOUND_REAL_NGINX_CONF")"
2718 _debug
"_start_tag" "$_start_tag"
2719 if [ "$_start_tag" = "$NGINX_START" ]; then
2720 _info
"The domain $_d is already configured, skip"
2721 FOUND_REAL_NGINX_CONF
=""
2725 mkdir
-p "$DOMAIN_BACKUP_PATH"
2726 _backup_conf
="$DOMAIN_BACKUP_PATH/$_d.nginx.conf"
2727 _debug _backup_conf
"$_backup_conf"
2728 BACKUP_NGINX_CONF
="$_backup_conf"
2729 _info
"Backup $FOUND_REAL_NGINX_CONF to $_backup_conf"
2730 if ! cp "$FOUND_REAL_NGINX_CONF" "$_backup_conf"; then
2731 _err
"backup error."
2732 FOUND_REAL_NGINX_CONF
=""
2736 if ! _exists
"nginx"; then
2737 _err
"nginx command is not found."
2740 _info
"Check the nginx conf before setting up."
2741 if ! _exec
"nginx -t" >/dev
/null
; then
2746 _info
"OK, Set up nginx config file"
2748 if ! sed -n "1,${_ln}p" "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"; then
2749 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2750 _err
"write nginx conf error, but don't worry, the file is restored to the original version."
2755 location ~ \"^/\.well-known/acme-challenge/([-_a-zA-Z0-9]+)\$\" {
2756 default_type text/plain;
2757 return 200 \"\$1.$_thumbpt\";
2760 " >>"$FOUND_REAL_NGINX_CONF"
2762 if ! sed -n "${_lnn},99999p" "$_backup_conf" >>"$FOUND_REAL_NGINX_CONF"; then
2763 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2764 _err
"write nginx conf error, but don't worry, the file is restored."
2767 _debug3
"Modified config:$(cat $FOUND_REAL_NGINX_CONF)"
2768 _info
"nginx conf is done, let's check it again."
2769 if ! _exec
"nginx -t" >/dev
/null
; then
2771 _err
"It seems that nginx conf was broken, let's restore."
2772 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2776 _info
"Reload nginx"
2777 if ! _exec
"nginx -s reload" >/dev
/null
; then
2779 _err
"It seems that nginx reload error, let's restore."
2780 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2791 _debug
"Start _checkConf from:$_c_file"
2792 if [ ! -f "$2" ] && ! echo "$2" |
grep '*$' >/dev
/null
&& echo "$2" |
grep '*' >/dev
/null
; then
2795 if [ -f "$_w_f" ] && _checkConf
"$1" "$_w_f"; then
2801 elif [ -f "$2" ]; then
2803 if _isRealNginxConf
"$1" "$2"; then
2804 _debug
"$2 is found."
2805 FOUND_REAL_NGINX_CONF
="$2"
2808 if cat "$2" |
tr "\t" " " |
grep "^ *include *.*;" >/dev
/null
; then
2809 _debug
"Try include files"
2810 for included
in $
(cat "$2" |
tr "\t" " " |
grep "^ *include *.*;" |
sed "s/include //" |
tr -d " ;"); do
2811 _debug
"check included $included"
2812 if _checkConf
"$1" "$included"; then
2819 _debug
"$2 not found."
2826 _isRealNginxConf
() {
2827 _debug
"_isRealNginxConf $1 $2"
2828 if [ -f "$2" ]; then
2829 for _fln
in $
(tr "\t" ' ' <"$2" |
grep -n "^ *server_name.* $1" | cut
-d : -f 1); do
2831 if [ "$_fln" ]; then
2832 _start
=$
(tr "\t" ' ' <"$2" | _head_n
"$_fln" |
grep -n "^ *server *" |
grep -v server_name | _tail_n
1)
2833 _debug
"_start" "$_start"
2834 _start_n
=$
(echo "$_start" | cut
-d : -f 1)
2835 _start_nn
=$
(_math
$_start_n + 1)
2836 _debug
"_start_n" "$_start_n"
2837 _debug
"_start_nn" "$_start_nn"
2839 _left
="$(sed -n "${_start_nn},99999p
" "$2")"
2840 _debug2 _left
"$_left"
2841 _end
="$(echo "$_left" | tr "\t" ' ' | grep -n "^
*server
*" | grep -v server_name | _head_n 1)"
2842 _debug
"_end" "$_end"
2843 if [ "$_end" ]; then
2844 _end_n
=$
(echo "$_end" | cut
-d : -f 1)
2845 _debug
"_end_n" "$_end_n"
2846 _seg_n
=$
(echo "$_left" |
sed -n "1,${_end_n}p")
2851 _debug
"_seg_n" "$_seg_n"
2854 for _listen_i
in $
(echo "$_seg_n" |
tr "\t" ' ' |
grep "^ *listen" |
tr -d " "); do
2855 if [ "$_listen_i" ]; then
2856 if [ "$(echo "$_listen_i" | _egrep_o "listen.
*ssl
")" ]; then
2857 _debug2
"$_listen_i is ssl"
2859 _debug2
"$_listen_i is plain text"
2866 if [ "$_skip_ssl" = "1" ]; then
2867 _debug
"ssl on, skip"
2869 FOUND_REAL_NGINX_CONF_LN
=$_fln
2870 _debug3
"found FOUND_REAL_NGINX_CONF_LN" "$FOUND_REAL_NGINX_CONF_LN"
2879 #restore all the nginx conf
2881 if [ -z "$NGINX_RESTORE_VLIST" ]; then
2882 _debug
"No need to restore nginx, skip."
2885 _debug
"_restoreNginx"
2886 _debug
"NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST"
2888 for ng_entry
in $
(echo "$NGINX_RESTORE_VLIST" |
tr "$dvsep" ' '); do
2889 _debug
"ng_entry" "$ng_entry"
2890 _nd
=$
(echo "$ng_entry" | cut
-d "$sep" -f 1)
2891 _ngconf
=$
(echo "$ng_entry" | cut
-d "$sep" -f 2)
2892 _ngbackupconf
=$
(echo "$ng_entry" | cut
-d "$sep" -f 3)
2893 _info
"Restoring from $_ngbackupconf to $_ngconf"
2894 cat "$_ngbackupconf" >"$_ngconf"
2897 _info
"Reload nginx"
2898 if ! _exec
"nginx -s reload" >/dev
/null
; then
2900 _err
"It seems that nginx reload error, please report bug."
2907 _stopserver
"$serverproc"
2912 if [ -z "$DEBUG" ]; then
2921 _debug
"_clearupdns"
2922 _debug
"dnsadded" "$dnsadded"
2923 _debug
"vlist" "$vlist"
2924 #dnsadded is "0" or "1" means dns-01 method was used for at least one domain
2925 if [ -z "$dnsadded" ] ||
[ -z "$vlist" ]; then
2929 _info
"Removing DNS records."
2930 ventries
=$
(echo "$vlist" |
tr ',' ' ')
2932 for ventry
in $ventries; do
2933 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
2934 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
2935 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
2936 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
2937 txt
="$(printf "%s
" "$keyauthorization" | _digest "sha256
" | _url_replace)"
2939 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
2940 _debug
"$d is already verified, skip $vtype."
2941 _alias_index
="$(_math "$_alias_index" + 1)"
2945 if [ "$vtype" != "$VTYPE_DNS" ]; then
2946 _debug
"Skip $d for $vtype"
2950 d_api
="$(_findHook "$d" dnsapi "$_currentRoot")"
2951 _debug d_api
"$d_api"
2953 if [ -z "$d_api" ]; then
2954 _info
"Not Found domain api file: $d_api"
2959 if ! .
"$d_api"; then
2960 _err
"Load file $d_api error. Please check your api file and try again."
2964 rmcommand
="${_currentRoot}_rm"
2965 if ! _exists
"$rmcommand"; then
2966 _err
"It seems that your api file doesn't define $rmcommand"
2971 if _startswith
"$_dns_root_d" "*."; then
2972 _dns_root_d
="$(echo "$_dns_root_d" | sed 's/*.//')"
2975 _d_alias
="$(_getfield "$_challenge_alias" "$_alias_index")"
2976 _alias_index
="$(_math "$_alias_index" + 1)"
2977 _debug
"_d_alias" "$_d_alias"
2978 if [ "$_d_alias" ]; then
2979 if _startswith
"$_d_alias" "$DNS_ALIAS_PREFIX"; then
2980 txtdomain
="$(echo "$_d_alias" | sed "s
/$DNS_ALIAS_PREFIX//")"
2982 txtdomain
="_acme-challenge.$_d_alias"
2985 txtdomain
="_acme-challenge.$_dns_root_d"
2988 if ! $rmcommand "$txtdomain" "$txt"; then
2989 _err
"Error removing txt for domain:$txtdomain"
2997 # webroot removelevel tokenfile
2998 _clearupwebbroot
() {
3000 if [ -z "$__webroot" ]; then
3001 _debug
"no webroot specified, skip"
3006 if [ "$2" = '1' ]; then
3007 _rmpath
="$__webroot/.well-known"
3008 elif [ "$2" = '2' ]; then
3009 _rmpath
="$__webroot/.well-known/acme-challenge"
3010 elif [ "$2" = '3' ]; then
3011 _rmpath
="$__webroot/.well-known/acme-challenge/$3"
3013 _debug
"Skip for removelevel:$2"
3016 if [ "$_rmpath" ]; then
3017 if [ "$DEBUG" ]; then
3018 _debug
"Debugging, skip removing: $_rmpath"
3028 _on_before_issue
() {
3030 _chk_main_domain
="$2"
3031 _chk_alt_domains
="$3"
3033 _chk_local_addr
="$5"
3034 _debug _on_before_issue
3035 _debug _chk_main_domain
"$_chk_main_domain"
3036 _debug _chk_alt_domains
"$_chk_alt_domains"
3038 if [ "$_chk_pre_hook" ]; then
3039 _info
"Run pre hook:'$_chk_pre_hook'"
3041 cd "$DOMAIN_PATH" && eval "$_chk_pre_hook"
3043 _err
"Error when run pre hook."
3048 if _hasfield
"$_chk_web_roots" "$NO_VALUE"; then
3049 if ! _exists
"socat"; then
3050 _err
"Please install socat tools first."
3055 _debug Le_LocalAddress
"$_chk_local_addr"
3062 d
="$(echo "$_chk_main_domain,$_chk_alt_domains," | cut -d , -f "$_w_index")"
3063 _w_index
="$(_math "$_w_index" + 1)"
3065 if [ -z "$d" ]; then
3068 _debug
"Check for domain" "$d"
3069 _currentRoot
="$(_getfield "$_chk_web_roots" $_index)"
3070 _debug
"_currentRoot" "$_currentRoot"
3071 _index
=$
(_math
$_index + 1)
3073 if [ "$_currentRoot" = "$NO_VALUE" ]; then
3074 _info
"Standalone mode."
3075 if [ -z "$Le_HTTPPort" ]; then
3078 _savedomainconf
"Le_HTTPPort" "$Le_HTTPPort"
3080 _checkport
="$Le_HTTPPort"
3081 elif [ "$_currentRoot" = "$W_ALPN" ]; then
3082 _info
"Standalone alpn mode."
3083 if [ -z "$Le_TLSPort" ]; then
3086 _savedomainconf
"Le_TLSPort" "$Le_TLSPort"
3088 _checkport
="$Le_TLSPort"
3091 if [ "$_checkport" ]; then
3092 _debug _checkport
"$_checkport"
3093 _checkaddr
="$(_getfield "$_chk_local_addr" $_addrIndex)"
3094 _debug _checkaddr
"$_checkaddr"
3096 _addrIndex
="$(_math $_addrIndex + 1)"
3098 _netprc
="$(_ss "$_checkport" | grep "$_checkport")"
3099 netprc
="$(echo "$_netprc" | grep "$_checkaddr")"
3100 if [ -z "$netprc" ]; then
3101 netprc
="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
3103 if [ "$netprc" ]; then
3105 _err
"tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
3106 _err
"Please stop it first"
3112 if _hasfield
"$_chk_web_roots" "apache"; then
3113 if ! _setApache
; then
3114 _err
"set up apache error. Report error to me."
3126 _debug _on_issue_err
3128 if [ "$LOG_FILE" ]; then
3129 _err
"Please check log file for more details: $LOG_FILE"
3131 _err
"Please add '--debug' or '--log' to check more details."
3132 _err
"See: $_DEBUG_WIKI"
3136 if [ "$_chk_post_hook" ]; then
3137 _info
"Run post hook:'$_chk_post_hook'"
3139 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
3141 _err
"Error when run post hook."
3146 #trigger the validation to flush the pending authz
3147 _debug2
"_chk_vlist" "$_chk_vlist"
3148 if [ "$_chk_vlist" ]; then
3150 _debug2
"start to deactivate authz"
3151 ventries
=$
(echo "$_chk_vlist" |
tr "$dvsep" ' ')
3152 for ventry
in $ventries; do
3153 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
3154 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
3155 uri
=$
(echo "$ventry" | cut
-d "$sep" -f 3)
3156 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
3157 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
3158 __trigger_validation
"$uri" "$keyauthorization"
3163 if [ "$IS_RENEW" = "1" ] && _hasfield
"$Le_Webroot" "$W_DNS"; then
3164 _err
"$_DNS_MANUAL_ERR"
3167 if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
3168 _debug
"$(_dlg_versions)"
3173 _on_issue_success
() {
3175 _chk_renew_hook
="$2"
3176 _debug _on_issue_success
3178 if [ "$_chk_post_hook" ]; then
3179 _info
"Run post hook:'$_chk_post_hook'"
3181 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
3183 _err
"Error when run post hook."
3189 if [ "$IS_RENEW" ] && [ "$_chk_renew_hook" ]; then
3190 _info
"Run renew hook:'$_chk_renew_hook'"
3192 cd "$DOMAIN_PATH" && eval "$_chk_renew_hook"
3194 _err
"Error when run renew hook."
3199 if _hasfield
"$Le_Webroot" "$W_DNS"; then
3200 _err
"$_DNS_MANUAL_WARN"
3213 _regAccount
"$_reg_length"
3216 __calcAccountKeyHash
() {
3217 [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256
<"$ACCOUNT_KEY_PATH"
3220 __calc_account_thumbprint
() {
3221 printf "%s" "$jwk" |
tr -d ' ' | _digest
"sha256" | _url_replace
3228 _debug3 _regAccount
"$_regAccount"
3232 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
3233 _info
"mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
3234 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3237 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
3238 _info
"mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
3239 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3242 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3243 if ! _create_account_key
"$_reg_length"; then
3244 _err
"Create account key error."
3249 if ! _calcjwk
"$ACCOUNT_KEY_PATH"; then
3253 if [ "$ACME_VERSION" = "2" ]; then
3254 regjson
='{"termsOfServiceAgreed": true}'
3255 if [ "$ACCOUNT_EMAIL" ]; then
3256 regjson
='{"contact": ["mailto: '$ACCOUNT_EMAIL'"], "termsOfServiceAgreed": true}'
3259 _reg_res
="$ACME_NEW_ACCOUNT_RES"
3260 regjson
='{"resource": "'$_reg_res'", "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
3261 if [ "$ACCOUNT_EMAIL" ]; then
3262 regjson
='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
3266 _info
"Registering account"
3268 if ! _send_signed_request
"${ACME_NEW_ACCOUNT}" "$regjson"; then
3269 _err
"Register account Error: $response"
3273 if [ "$code" = "" ] ||
[ "$code" = '201' ]; then
3274 echo "$response" >"$ACCOUNT_JSON_PATH"
3276 elif [ "$code" = '409' ] ||
[ "$code" = '200' ]; then
3277 _info
"Already registered"
3279 _err
"Register account Error: $response"
3283 _debug2 responseHeaders
"$responseHeaders"
3284 _accUri
="$(echo "$responseHeaders" | grep "^Location
:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
3285 _debug
"_accUri" "$_accUri"
3286 if [ -z "$_accUri" ]; then
3287 _err
"Can not find account id url."
3288 _err
"$responseHeaders"
3291 _savecaconf
"ACCOUNT_URL" "$_accUri"
3292 export ACCOUNT_URL
="$_accUri"
3294 CA_KEY_HASH
="$(__calcAccountKeyHash)"
3295 _debug
"Calc CA_KEY_HASH" "$CA_KEY_HASH"
3296 _savecaconf CA_KEY_HASH
"$CA_KEY_HASH"
3298 if [ "$code" = '403' ]; then
3299 _err
"It seems that the account key is already deactivated, please use a new account key."
3303 ACCOUNT_THUMBPRINT
="$(__calc_account_thumbprint)"
3304 _info
"ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT"
3307 #Implement deactivate account
3308 deactivateaccount
() {
3311 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
3312 _info
"mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
3313 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3316 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
3317 _info
"mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
3318 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3321 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3322 _err
"Account key is not found at: $ACCOUNT_KEY_PATH"
3326 _accUri
=$
(_readcaconf
"ACCOUNT_URL")
3327 _debug _accUri
"$_accUri"
3329 if [ -z "$_accUri" ]; then
3330 _err
"The account url is empty, please run '--update-account' first to update the account info first,"
3331 _err
"Then try again."
3335 if ! _calcjwk
"$ACCOUNT_KEY_PATH"; then
3340 if [ "$ACME_VERSION" = "2" ]; then
3341 _djson
="{\"status\":\"deactivated\"}"
3343 _djson
="{\"resource\": \"reg\", \"status\":\"deactivated\"}"
3345 if _send_signed_request
"$_accUri" "$_djson" && _contains
"$response" '"deactivated"'; then
3346 _info
"Deactivate account success for $_accUri."
3347 _accid
=$
(echo "$response" | _egrep_o
"\"id\" *: *[^,]*," | cut
-d : -f 2 |
tr -d ' ,')
3348 elif [ "$code" = "403" ]; then
3349 _info
"The account is already deactivated."
3350 _accid
=$
(_getfield
"$_accUri" "999" "/")
3352 _err
"Deactivate: account failed for $_accUri."
3356 _debug
"Account id: $_accid"
3357 if [ "$_accid" ]; then
3358 _deactivated_account_path
="$CA_DIR/deactivated/$_accid"
3359 _debug _deactivated_account_path
"$_deactivated_account_path"
3360 if mkdir
-p "$_deactivated_account_path"; then
3361 _info
"Moving deactivated account info to $_deactivated_account_path/"
3362 mv "$CA_CONF" "$_deactivated_account_path/"
3363 mv "$ACCOUNT_JSON_PATH" "$_deactivated_account_path/"
3364 mv "$ACCOUNT_KEY_PATH" "$_deactivated_account_path/"
3366 _err
"Can not create dir: $_deactivated_account_path, try to remove the deactivated account key."
3368 rm -f "$ACCOUNT_JSON_PATH"
3369 rm -f "$ACCOUNT_KEY_PATH"
3374 # domain folder file
3380 if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
3381 d_api
="$_SCRIPT_HOME/$_hookcat/$_hookname"
3382 elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
3383 d_api
="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
3384 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
3385 d_api
="$LE_WORKING_DIR/$_hookdomain/$_hookname"
3386 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
3387 d_api
="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
3388 elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
3389 d_api
="$LE_WORKING_DIR/$_hookname"
3390 elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
3391 d_api
="$LE_WORKING_DIR/$_hookname.sh"
3392 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
3393 d_api
="$LE_WORKING_DIR/$_hookcat/$_hookname"
3394 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
3395 d_api
="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
3398 printf "%s" "$d_api"
3402 __get_domain_new_authz
() {
3404 _info
"Getting new-authz for domain" "$_gdnd"
3406 _Max_new_authz_retry_times
=5
3408 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
3409 _debug
"Try new-authz for the $_authz_i time."
3410 if ! _send_signed_request
"${ACME_NEW_AUTHZ}" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
3411 _err
"Can not get domain new authz."
3414 if _contains
"$response" "No registration exists matching provided key"; then
3415 _err
"It seems there is an error, but it's recovered now, please try again."
3416 _err
"If you see this message for a second time, please report bug: $(__green "$PROJECT")"
3417 _clearcaconf
"CA_KEY_HASH"
3420 if ! _contains
"$response" "An error occurred while processing your request"; then
3421 _info
"The new-authz request is ok."
3424 _authz_i
="$(_math "$_authz_i" + 1)"
3425 _info
"The server is busy, Sleep $_authz_i to retry."
3429 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
3430 _err
"new-authz retry reach the max $_Max_new_authz_retry_times times."
3433 if [ "$code" ] && [ "$code" != '201' ]; then
3434 _err
"new-authz error: $response"
3440 #uri keyAuthorization
3441 __trigger_validation
() {
3442 _debug2
"Trigger domain validation."
3444 _debug2 _t_url
"$_t_url"
3446 _debug2 _t_key_authz
"$_t_key_authz"
3448 _debug2 _t_vtype
"$_t_vtype"
3449 if [ "$ACME_VERSION" = "2" ]; then
3450 _send_signed_request
"$_t_url" "{\"keyAuthorization\": \"$_t_key_authz\"}"
3452 _send_signed_request
"$_t_url" "{\"resource\": \"challenge\", \"type\": \"$_t_vtype\", \"keyAuthorization\": \"$_t_key_authz\"}"
3456 #webroot, domain domainlist keylength
3458 if [ -z "$2" ]; then
3459 _usage
"Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
3462 if [ -z "$1" ]; then
3463 _usage
"Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc."
3470 if _contains
"$_main_domain" ","; then
3471 _main_domain
=$
(echo "$2,$3" | cut
-d , -f 1)
3472 _alt_domains
=$
(echo "$2,$3" | cut
-d , -f 2- |
sed "s/,${NO_VALUE}$//")
3474 _debug _main_domain
"$_main_domain"
3475 _debug _alt_domains
"$_alt_domains"
3482 _real_fullchain
="$9"
3487 _challenge_alias
="${14}"
3488 #remove these later.
3489 if [ "$_web_roots" = "dns-cf" ]; then
3492 if [ "$_web_roots" = "dns-dp" ]; then
3495 if [ "$_web_roots" = "dns-cx" ]; then
3499 if [ ! "$IS_RENEW" ]; then
3500 _initpath
"$_main_domain" "$_key_length"
3501 mkdir
-p "$DOMAIN_PATH"
3504 if _hasfield
"$_web_roots" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then
3505 _err
"$_DNS_MANUAL_ERROR"
3509 _debug
"Using ACME_DIRECTORY: $ACME_DIRECTORY"
3513 if [ -f "$DOMAIN_CONF" ]; then
3514 Le_NextRenewTime
=$
(_readdomainconf Le_NextRenewTime
)
3515 _debug Le_NextRenewTime
"$Le_NextRenewTime"
3516 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
3517 _saved_domain
=$
(_readdomainconf Le_Domain
)
3518 _debug _saved_domain
"$_saved_domain"
3519 _saved_alt
=$
(_readdomainconf Le_Alt
)
3520 _debug _saved_alt
"$_saved_alt"
3521 if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then
3522 _info
"Domains not changed."
3523 _info
"Skip, Next renewal time is: $(__green "$
(_readdomainconf Le_NextRenewTimeStr
)")"
3524 _info
"Add '$(__red '--force')' to force to renew."
3527 _info
"Domains have changed."
3532 _savedomainconf
"Le_Domain" "$_main_domain"
3533 _savedomainconf
"Le_Alt" "$_alt_domains"
3534 _savedomainconf
"Le_Webroot" "$_web_roots"
3536 _savedomainconf
"Le_PreHook" "$_pre_hook"
3537 _savedomainconf
"Le_PostHook" "$_post_hook"
3538 _savedomainconf
"Le_RenewHook" "$_renew_hook"
3540 if [ "$_local_addr" ]; then
3541 _savedomainconf
"Le_LocalAddress" "$_local_addr"
3543 _cleardomainconf
"Le_LocalAddress"
3545 if [ "$_challenge_alias" ]; then
3546 _savedomainconf
"Le_ChallengeAlias" "$_challenge_alias"
3548 _cleardomainconf
"Le_ChallengeAlias"
3551 Le_API
="$ACME_DIRECTORY"
3552 _savedomainconf
"Le_API" "$Le_API"
3554 if [ "$_alt_domains" = "$NO_VALUE" ]; then
3558 if [ "$_key_length" = "$NO_VALUE" ]; then
3562 if ! _on_before_issue
"$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
3563 _err
"_on_before_issue."
3567 _saved_account_key_hash
="$(_readcaconf "CA_KEY_HASH
")"
3568 _debug2 _saved_account_key_hash
"$_saved_account_key_hash"
3570 if [ -z "$ACCOUNT_URL" ] ||
[ -z "$_saved_account_key_hash" ] ||
[ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
3571 if ! _regAccount
"$_accountkeylength"; then
3572 _on_issue_err
"$_post_hook"
3576 _debug
"_saved_account_key_hash is not changed, skip register account."
3579 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
3580 _info
"Signing from existing CSR."
3582 _key
=$
(_readdomainconf Le_Keylength
)
3583 _debug
"Read key length:$_key"
3584 if [ ! -f "$CERT_KEY_PATH" ] ||
[ "$_key_length" != "$_key" ] ||
[ "$Le_ForceNewDomainKey" = "1" ]; then
3585 if ! createDomainKey
"$_main_domain" "$_key_length"; then
3586 _err
"Create domain key error."
3588 _on_issue_err
"$_post_hook"
3593 if ! _createcsr
"$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
3594 _err
"Create CSR error."
3596 _on_issue_err
"$_post_hook"
3601 _savedomainconf
"Le_Keylength" "$_key_length"
3605 _info
"Getting domain auth token for each domain"
3608 if [ -z "$vlist" ]; then
3609 if [ "$ACME_VERSION" = "2" ]; then
3610 #make new order request
3611 _identifiers
="{\"type\":\"dns\",\"value\":\"$_main_domain\"}"
3614 d
="$(echo "$_alt_domains," | cut -d , -f "$_w_index")"
3615 _w_index
="$(_math "$_w_index" + 1)"
3617 if [ -z "$d" ]; then
3620 _identifiers
="$_identifiers,{\"type\":\"dns\",\"value\":\"$d\"}"
3622 _debug2 _identifiers
"$_identifiers"
3623 if ! _send_signed_request
"$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
3624 _err
"Create new order error."
3626 _on_issue_err
"$_post_hook"
3630 Le_OrderFinalize
="$(echo "$response" | tr -d '\r\n' | _egrep_o '"finalize
" *: *"[^
"]*"' | cut -d '"' -f 4)"
3631 _debug Le_OrderFinalize
"$Le_OrderFinalize"
3632 if [ -z "$Le_OrderFinalize" ]; then
3633 _err
"Create new order error. Le_OrderFinalize not found. $response"
3635 _on_issue_err
"$_post_hook"
3639 #for dns manual mode
3640 _savedomainconf
"Le_OrderFinalize" "$Le_OrderFinalize"
3642 _authorizations_seg
="$(echo "$response" | tr -d '\r\n' | _egrep_o '"authorizations
" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
3643 _debug2 _authorizations_seg "$_authorizations_seg"
3644 if [ -z "$_authorizations_seg" ]; then
3645 _err "_authorizations_seg not found."
3647 _on_issue_err "$_post_hook"
3651 #domain and authz map
3652 _authorizations_map=""
3653 for _authz_url in $(echo "$_authorizations_seg" | tr ',' ' '); do
3654 _debug2 "_authz_url" "$_authz_url"
3655 if ! _send_signed_request "$_authz_url"; then
3656 _err "get to authz error."
3657 _err "_authorizations_seg" "$_authorizations_seg"
3658 _err "_authz_url" "$_authz_url"
3660 _on_issue_err "$_post_hook"
3664 response="$(echo "$response" | _normalizeJson)"
3665 _debug2 response "$response"
3666 _d="$(echo "$response" | _egrep_o '"value" *: *"[^"]*"' | cut -d : -f 2 | tr -d ' "')"
3667 if _contains "$response" "\"wildcard\" *: *true"; then
3671 _authorizations_map="$_d,$response
3672 $_authorizations_map"
3674 _debug2 _authorizations_map "$_authorizations_map"
3681 d="$(echo "$_main_domain,$_alt_domains," | cut -d , -f "$_w_index")"
3682 _w_index="$(_math "$_w_index" + 1)"
3684 if [ -z "$d" ]; then
3687 _info "Getting webroot for domain" "$d"
3688 _index=$(_math $_index + 1)
3689 _w="$(echo $_web_roots | cut -d , -f $_index)"
3694 _debug "_currentRoot" "$_currentRoot"
3697 #todo, v2 wildcard force to use dns
3698 if _startswith "$_currentRoot" "$W_DNS"; then
3702 if [ "$_currentRoot" = "$W_ALPN" ]; then
3706 if [ "$ACME_VERSION" = "2" ]; then
3707 response="$(echo "$_authorizations_map" | grep "^$d," | sed "s/$d,//")"
3708 _debug2 "response" "$response"
3709 if [ -z "$response" ]; then
3710 _err "get to authz error."
3711 _err "_authorizations_map" "$_authorizations_map"
3713 _on_issue_err "$_post_hook"
3717 if ! __get_domain_new_authz "$d"; then
3719 _on_issue_err "$_post_hook"
3724 if [ -z "$thumbprint" ]; then
3725 thumbprint="$(__calc_account_thumbprint)"
3728 entry="$(printf "%s\n" "$response" | _egrep_o '[^\
{]*"type":"'$vtype'"[^\
}]*')"
3729 _debug entry "$entry"
3730 if [ -z "$entry" ]; then
3731 _err "Error, can not get domain token entry $d"
3732 _supported_vtypes="$(echo "$response" | _egrep_o "\"challenges\":\[[^]]*]" | tr '{' "\n" | grep type | cut -d '"' -f 4 | tr "\n" ' ')"
3733 if [ "$_supported_vtypes" ]; then
3734 _err
"The supported validation types are: $_supported_vtypes, but you specified: $vtype"
3737 _on_issue_err
"$_post_hook"
3740 token
="$(printf "%s
\n" "$entry" | _egrep_o '"token
":"[^
"]*' | cut -d : -f 2 | tr -d '"')"
3741 _debug token "$token"
3743 if [ -z "$token" ]; then
3744 _err "Error, can not get domain token $entry"
3746 _on_issue_err "$_post_hook"
3749 if [ "$ACME_VERSION" = "2" ]; then
3750 uri="$(printf "%s\n" "$entry" | _egrep_o '"url":"[^"]*' | cut -d '"' -f 4 | _head_n 1)"
3752 uri
="$(printf "%s
\n" "$entry" | _egrep_o '"uri
":"[^
"]*' | cut -d '"' -f 4)"
3756 if [ -z "$uri" ]; then
3757 _err "Error, can not get domain uri. $entry"
3759 _on_issue_err "$_post_hook"
3762 keyauthorization="$token.$thumbprint"
3763 _debug keyauthorization "$keyauthorization"
3765 if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
3766 _debug "$d is already verified."
3767 keyauthorization="$STATE_VERIFIED"
3768 _debug keyauthorization "$keyauthorization"
3771 dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
3772 _debug dvlist "$dvlist"
3774 vlist="$vlist$dvlist$dvsep"
3777 _debug vlist "$vlist"
3780 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
3782 for ventry in $ventries; do
3783 d=$(echo "$ventry" | cut -d "$sep" -f 1)
3784 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
3785 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
3786 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
3788 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
3789 _debug "$d is already verified, skip $vtype."
3790 _alias_index="$(_math "$_alias_index" + 1)"
3794 if [ "$vtype" = "$VTYPE_DNS" ]; then
3797 if _startswith "$_dns_root_d" "*."; then
3798 _dns_root_d="$(echo "$_dns_root_d" | sed 's
/*.
//')"
3800 _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")"
3801 _alias_index="$(_math "$_alias_index" + 1)"
3802 _debug "_d_alias" "$_d_alias"
3803 if [ "$_d_alias" ]; then
3804 if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then
3805 txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")"
3807 txtdomain="_acme-challenge.$_d_alias"
3810 txtdomain="_acme-challenge.$_dns_root_d"
3812 _debug txtdomain "$txtdomain"
3813 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
3816 d_api="$(_findHook "$_dns_root_d" dnsapi "$_currentRoot")"
3818 _debug d_api "$d_api"
3820 if [ "$d_api" ]; then
3821 _info "Found domain api file: $d_api"
3823 if [ "$_currentRoot" != "$W_DNS" ]; then
3824 _err "Can not find dns api hook for: $_currentRoot"
3825 _info "You need to add the txt record manually."
3827 _info "$(__red "Add the following TXT record:")"
3828 _info "$(__red "Domain: '$
(__green
"$txtdomain")'")"
3829 _info "$(__red "TXT value: '$
(__green
"$txt")'")"
3830 _info "$(__red "Please be aware that you prepend _acme-challenge. before your domain")"
3831 _info "$(__red "so the resulting subdomain will be: $txtdomain")"
3836 if ! . "$d_api"; then
3837 _err "Load file $d_api error. Please check your api file and try again."
3841 addcommand="${_currentRoot}_add"
3842 if ! _exists "$addcommand"; then
3843 _err "It seems that your api file is not correct, it must have a function named: $addcommand"
3847 if ! $addcommand "$txtdomain" "$txt"; then
3848 _err "Error add txt for domain:$txtdomain"
3853 if [ "$?" != "0" ]; then
3854 _on_issue_err "$_post_hook" "$vlist"
3862 if [ "$dnsadded" = '0' ]; then
3863 _savedomainconf "Le_Vlist" "$vlist"
3864 _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
3865 _err "Please add the TXT records to the domains, and re-run with --renew."
3866 _on_issue_err "$_post_hook"
3873 if [ "$dnsadded" = '1' ]; then
3874 if [ -z "$Le_DNSSleep" ]; then
3875 Le_DNSSleep="$DEFAULT_DNS_SLEEP"
3877 _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
3880 _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
3881 _sleep "$Le_DNSSleep"
3884 NGINX_RESTORE_VLIST=""
3885 _debug "ok, let's start to verify
"
3888 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
3889 for ventry in $ventries; do
3890 d=$(echo "$ventry" | cut -d "$sep" -f 1)
3891 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
3892 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
3893 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
3894 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
3896 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
3897 _info "$d is already verified
, skip
$vtype.
"
3901 _info "Verifying
: $d"
3903 _debug "keyauthorization
" "$keyauthorization"
3906 token="$
(printf "%s" "$keyauthorization" | cut
-d '.' -f 1)"
3908 _debug "_currentRoot
" "$_currentRoot"
3910 if [ "$vtype" = "$VTYPE_HTTP" ]; then
3911 if [ "$_currentRoot" = "$NO_VALUE" ]; then
3912 _info "Standalone mode server
"
3913 _ncaddr="$
(_getfield
"$_local_addr" "$_ncIndex")"
3914 _ncIndex="$
(_math
$_ncIndex + 1)"
3915 _startserver "$keyauthorization" "$_ncaddr"
3916 if [ "$?
" != "0" ]; then
3918 _on_issue_err "$_post_hook" "$vlist"
3922 _debug serverproc "$serverproc"
3923 elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
3924 _info "Stateless mode
for domain
:$d"
3926 elif _startswith "$_currentRoot" "$NGINX"; then
3927 _info "Nginx mode
for domain
:$d"
3928 #set up nginx server
3929 FOUND_REAL_NGINX_CONF=""
3930 BACKUP_NGINX_CONF=""
3931 if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then
3933 _on_issue_err "$_post_hook" "$vlist"
3937 if [ "$FOUND_REAL_NGINX_CONF" ]; then
3938 _realConf="$FOUND_REAL_NGINX_CONF"
3939 _backup="$BACKUP_NGINX_CONF"
3940 _debug _realConf "$_realConf"
3941 NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
3945 if [ "$_currentRoot" = "apache
" ]; then
3946 wellknown_path="$ACME_DIR"
3948 wellknown_path="$_currentRoot/.well-known
/acme-challenge
"
3949 if [ ! -d "$_currentRoot/.well-known
" ]; then
3951 elif [ ! -d "$_currentRoot/.well-known
/acme-challenge
" ]; then
3958 _debug wellknown_path "$wellknown_path"
3960 _debug "writing token
:$token to
$wellknown_path/$token"
3962 mkdir -p "$wellknown_path"
3964 if ! printf "%s
" "$keyauthorization" >"$wellknown_path/$token"; then
3965 _err "$d:Can not
write token to
file : $wellknown_path/$token"
3966 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3968 _on_issue_err "$_post_hook" "$vlist"
3972 if [ ! "$usingApache" ]; then
3973 if webroot_owner=$(_stat "$_currentRoot"); then
3974 _debug "Changing owner
/group of .well-known to
$webroot_owner"
3975 if ! _exec "chown
-R \"$webroot_owner\" \"$_currentRoot/.well-known
\""; then
3976 _debug "$
(cat "$_EXEC_TEMP_ERR")"
3977 _exec_err >/dev/null 2>&1
3980 _debug "not changing owner
/group of webroot
"
3985 elif [ "$vtype" = "$VTYPE_ALPN" ]; then
3986 acmevalidationv1="$
(printf "%s" "$keyauthorization" | _digest
"sha256" "hex")"
3987 _debug acmevalidationv1 "$acmevalidationv1"
3988 if ! _starttlsserver "$d" "" "$Le_TLSPort" "$keyauthorization" "$_ncaddr" "$acmevalidationv1"; then
3989 _err "Start tls server error.
"
3990 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3992 _on_issue_err "$_post_hook" "$vlist"
3997 if ! __trigger_validation "$uri" "$keyauthorization" "$vtype"; then
3998 _err "$d:Can not get challenge
: $response"
3999 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4001 _on_issue_err "$_post_hook" "$vlist"
4005 if [ "$code" ] && [ "$code" != '202' ]; then
4006 if [ "$code" = '200' ]; then
4007 _debug "trigger validation code
: $code"
4009 _err "$d:Challenge error
: $response"
4010 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4012 _on_issue_err "$_post_hook" "$vlist"
4018 if [ -z "$MAX_RETRY_TIMES" ]; then
4023 waittimes=$(_math "$waittimes" + 1)
4024 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
4026 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4028 _on_issue_err "$_post_hook" "$vlist"
4032 _debug "sleep 2 secs to verify
"
4035 if [ "$ACME_VERSION" = "2" ]; then
4036 _send_signed_request "$uri"
4038 response="$
(_get
"$uri")"
4040 if [ "$?
" != "0" ]; then
4041 _err "$d:Verify error
:$response"
4042 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4044 _on_issue_err "$_post_hook" "$vlist"
4047 _debug2 original "$response"
4049 response="$
(echo "$response" | _normalizeJson
)"
4050 _debug2 response "$response"
4052 status=$(echo "$response" | _egrep_o '"status
":"[^
"]*' | cut -d : -f 2 | tr -d '"')
4053 if [ "$status" = "valid" ]; then
4054 _info "$(__green Success)"
4055 _stopserver "$serverproc"
4057 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4061 if [ "$status" = "invalid" ]; then
4062 error="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error":\
{[^\
}]*')"
4063 _debug2 error "$error"
4064 errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
4065 _debug2 errordetail
"$errordetail"
4066 if [ "$errordetail" ]; then
4067 _err
"$d:Verify error:$errordetail"
4069 _err
"$d:Verify error:$error"
4071 if [ "$DEBUG" ]; then
4072 if [ "$vtype" = "$VTYPE_HTTP" ]; then
4073 _debug
"Debug: get token url."
4074 _get
"http://$d/.well-known/acme-challenge/$token" "" 1
4077 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
4079 _on_issue_err
"$_post_hook" "$vlist"
4083 if [ "$status" = "pending" ]; then
4086 _err
"$d:Verify error:$response"
4087 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
4089 _on_issue_err
"$_post_hook" "$vlist"
4098 _info
"Verify finished, start to sign."
4099 der
="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)"
4101 if [ "$ACME_VERSION" = "2" ]; then
4102 if ! _send_signed_request
"${Le_OrderFinalize}" "{\"csr\": \"$der\"}"; then
4104 _on_issue_err
"$_post_hook"
4107 if [ "$code" != "200" ]; then
4108 _err
"Sign failed, code is not 200."
4110 _on_issue_err
"$_post_hook"
4113 Le_LinkCert
="$(echo "$response" | tr -d '\r\n' | _egrep_o '"certificate
" *: *"[^
"]*"' | cut -d '"' -f 4)"
4115 _tempSignedResponse
="$response"
4116 if ! _send_signed_request
"$Le_LinkCert" "" "needbase64"; then
4117 _err
"Sign failed, can not download cert:$Le_LinkCert."
4119 _on_issue_err
"$_post_hook"
4123 echo "$response" | _dbase64
"multiline" >"$CERT_PATH"
4125 if [ "$(grep -- "$BEGIN_CERT" "$CERT_PATH" | wc -l)" -gt "1" ]; then
4126 _debug
"Found cert chain"
4127 cat "$CERT_PATH" >"$CERT_FULLCHAIN_PATH"
4128 _end_n
="$(grep -n -- "$END_CERT" "$CERT_FULLCHAIN_PATH" | _head_n 1 | cut -d : -f 1)"
4129 _debug _end_n
"$_end_n"
4130 sed -n "1,${_end_n}p" "$CERT_FULLCHAIN_PATH" >"$CERT_PATH"
4131 _end_n
="$(_math $_end_n + 1)"
4132 sed -n "${_end_n},9999p" "$CERT_FULLCHAIN_PATH" >"$CA_CERT_PATH"
4134 response
="$_tempSignedResponse"
4136 if ! _send_signed_request
"${ACME_NEW_ORDER}" "{\"resource\": \"$ACME_NEW_ORDER_RES\", \"csr\": \"$der\"}" "needbase64"; then
4137 _err
"Sign failed. $response"
4138 _on_issue_err
"$_post_hook"
4142 Le_LinkCert
="$(grep -i '^Location.*$' "$HTTP_HEADER" | _tail_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
4143 echo "$BEGIN_CERT" >"$CERT_PATH"
4145 #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
4146 # _debug "Get cert failed. Let's try last response."
4147 # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
4150 if ! printf -- "%s" "$_rcert" | _dbase64
"multiline" | _base64
"multiline" >>"$CERT_PATH"; then
4151 _debug
"Try cert link."
4152 _get
"$Le_LinkCert" | _base64
"multiline" >>"$CERT_PATH"
4155 echo "$END_CERT" >>"$CERT_PATH"
4158 _debug
"Le_LinkCert" "$Le_LinkCert"
4159 _savedomainconf
"Le_LinkCert" "$Le_LinkCert"
4161 if [ -z "$Le_LinkCert" ] ||
! _checkcert
"$CERT_PATH"; then
4162 response
="$(echo "$response" | _dbase64 "multiline
" | tr -d '\0' | _normalizeJson)"
4163 _err
"Sign failed: $(echo "$response" | _egrep_o '"detail
":"[^
"]*"')"
4164 _on_issue_err "$_post_hook"
4168 if [ "$Le_LinkCert" ]; then
4169 _info "$(__green "Cert success.")"
4172 _info "Your cert is in $(__green " $CERT_PATH ")"
4174 if [ -f "$CERT_KEY_PATH" ]; then
4175 _info "Your cert key is in $(__green " $CERT_KEY_PATH ")"
4178 if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
4180 _saveaccountconf "USER_PATH" "$USER_PATH"
4184 _cleardomainconf "Le_Vlist"
4186 if [ "$ACME_VERSION" = "2" ]; then
4189 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
4190 Le_LinkIssuer=$(grep -i '^Link
' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
4192 if [ "$Le_LinkIssuer" ]; then
4193 if ! _contains "$Le_LinkIssuer" ":"; then
4194 _info "$(__red "Relative issuer link found.")"
4195 Le_LinkIssuer="$_ACME_SERVER_HOST$Le_LinkIssuer"
4197 _debug Le_LinkIssuer "$Le_LinkIssuer"
4198 _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
4200 _link_issuer_retry=0
4202 while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do
4203 _debug _link_issuer_retry "$_link_issuer_retry"
4204 if [ "$ACME_VERSION" = "2" ]; then
4205 if _send_signed_request "$Le_LinkIssuer"; then
4206 echo "$response" >"$CA_CERT_PATH"
4210 if _get "$Le_LinkIssuer" >"$CA_CERT_PATH.der"; then
4211 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
4212 _base64 "multiline" <"$CA_CERT_PATH.der" >>"$CA_CERT_PATH"
4213 echo "$END_CERT" >>"$CA_CERT_PATH"
4214 if ! _checkcert "$CA_CERT_PATH"; then
4215 _err "Can not get the ca cert."
4218 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
4219 rm -f "$CA_CERT_PATH.der"
4223 _link_issuer_retry=$(_math $_link_issuer_retry + 1)
4224 _sleep "$_link_issuer_retry"
4226 if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then
4227 _err "Max retry for issuer ca cert is reached."
4230 _debug "No Le_LinkIssuer header found."
4233 [ -f "$CA_CERT_PATH" ] && _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")"
4234 [ -f "$CERT_FULLCHAIN_PATH" ] && _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")"
4236 Le_CertCreateTime=$(_time)
4237 _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
4239 Le_CertCreateTimeStr=$(date -u)
4240 _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
4242 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ]; then
4243 Le_RenewalDays="$DEFAULT_RENEW"
4245 _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
4248 if [ "$CA_BUNDLE" ]; then
4249 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
4251 _clearaccountconf "CA_BUNDLE"
4254 if [ "$CA_PATH" ]; then
4255 _saveaccountconf CA_PATH "$CA_PATH"
4257 _clearaccountconf "CA_PATH"
4260 if [ "$HTTPS_INSECURE" ]; then
4261 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
4263 _clearaccountconf "HTTPS_INSECURE"
4266 if [ "$Le_Listen_V4" ]; then
4267 _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
4268 _cleardomainconf Le_Listen_V6
4269 elif [ "$Le_Listen_V6" ]; then
4270 _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
4271 _cleardomainconf Le_Listen_V4
4274 if [ "$Le_ForceNewDomainKey" = "1" ]; then
4275 _savedomainconf "Le_ForceNewDomainKey" "$Le_ForceNewDomainKey"
4277 _cleardomainconf Le_ForceNewDomainKey
4280 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
4282 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
4283 _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
4285 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
4286 _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
4288 if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
4289 _savedomainconf "Le_RealCertPath" "$_real_cert"
4290 _savedomainconf "Le_RealCACertPath" "$_real_ca"
4291 _savedomainconf "Le_RealKeyPath" "$_real_key"
4292 _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
4293 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
4294 if ! _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"; then
4299 if ! _on_issue_success "$_post_hook" "$_renew_hook"; then
4300 _err "Call hook error."
4308 if [ -z "$Le_Domain" ]; then
4309 _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
4315 _initpath "$Le_Domain" "$_isEcc"
4317 _info "$(__green "Renew: '$Le_Domain'")"
4318 if [ ! -f "$DOMAIN_CONF" ]; then
4319 _info "'$Le_Domain' is not a issued domain, skip."
4323 if [ "$Le_RenewalDays" ]; then
4324 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
4328 _debug Le_API "$Le_API"
4329 if [ "$Le_API" ]; then
4330 if [ "$_OLD_CA_HOST" = "$Le_API" ]; then
4331 export Le_API="$DEFAULT_CA"
4332 _savedomainconf Le_API "$Le_API"
4334 if [ "$_OLD_STAGE_CA_HOST" = "$Le_API" ]; then
4335 export Le_API="$DEFAULT_STAGING_CA"
4336 _savedomainconf Le_API "$Le_API"
4338 export ACME_DIRECTORY="$Le_API"
4341 ACCOUNT_JSON_PATH=""
4343 _debug3 "initpath again."
4344 _initpath "$Le_Domain" "$_isEcc"
4347 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
4348 _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
4349 _info "Add '$
(__red
'--force')' to force to renew."
4350 return "$RENEW_SKIP"
4353 if [ "$IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then
4354 _info "Skip invalid cert for: $Le_Domain"
4359 issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath" "$Le_PreHook" "$Le_PostHook" "$Le_RenewHook" "$Le_LocalAddress" "$Le_ChallengeAlias"
4361 if [ "$res" != "0" ]; then
4365 if [ "$Le_DeployHook" ]; then
4366 _deploy "$Le_Domain" "$Le_DeployHook"
4375 #renewAll [stopRenewOnError]
4378 _stopRenewOnError="$1"
4379 _debug "_stopRenewOnError" "$_stopRenewOnError"
4382 for di in "${CERT_HOME}"/*.*/; do
4384 if ! [ -d "$di" ]; then
4385 _debug "Not directory, skip: $di"
4391 if _endswith "$d" "$ECC_SUFFIX"; then
4392 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
4393 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
4395 renew "$d" "$_isEcc"
4398 _debug "Return code: $rc"
4399 if [ "$rc" != "0" ]; then
4400 if [ "$rc" = "$RENEW_SKIP" ]; then
4402 elif [ "$_stopRenewOnError" ]; then
4403 _err "Error renew $d, stop now."
4407 _err "Error renew $d."
4418 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
4419 _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ "
4427 _real_fullchain="$7"
4432 _challenge_alias="${12}"
4434 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4435 if [ "$?" != "0" ]; then
4436 _err "Can not read subject from csr: $_csrfile"
4439 _debug _csrsubj "$_csrsubj"
4440 if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.
'; then
4441 _info "It seems that the subject: $_csrsubj is not a valid domain name. Drop it."
4445 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4446 if [ "$?" != "0" ]; then
4447 _err "Can not read domain list from csr: $_csrfile"
4450 _debug "_csrdomainlist" "$_csrdomainlist"
4452 if [ -z "$_csrsubj" ]; then
4453 _csrsubj="$(_getfield "$_csrdomainlist" 1)"
4454 _debug _csrsubj "$_csrsubj"
4455 _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
4456 _debug "_csrdomainlist" "$_csrdomainlist"
4459 if [ -z "$_csrsubj" ]; then
4460 _err "Can not read subject from csr: $_csrfile"
4464 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4465 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
4466 _err "Can not read key length from csr: $_csrfile"
4470 if [ -z "$ACME_VERSION" ] && _contains "$_csrsubj,$_csrdomainlist" "*."; then
4471 export ACME_VERSION=2
4473 _initpath "$_csrsubj" "$_csrkeylength"
4474 mkdir -p "$DOMAIN_PATH"
4476 _info "Copy csr to: $CSR_PATH"
4477 cp "$_csrfile" "$CSR_PATH"
4479 issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength" "$_real_cert" "$_real_key" "$_real_ca" "$_reload_cmd" "$_real_fullchain" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_addr" "$_challenge_alias"
4486 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
4487 _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr"
4493 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4494 if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then
4495 _err "Can not read subject from csr: $_csrfile"
4499 _info "Subject=$_csrsubj"
4501 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4502 if [ "$?" != "0" ]; then
4503 _err "Can not read domain list from csr: $_csrfile"
4506 _debug "_csrdomainlist" "$_csrdomainlist"
4508 _info "SubjectAltNames=$_csrdomainlist"
4510 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4511 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
4512 _err "Can not read key length from csr: $_csrfile"
4515 _info "KeyLength=$_csrkeylength"
4523 if [ "$_raw" ]; then
4524 printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew"
4525 for di in "${CERT_HOME}"/*.*/; do
4526 if ! [ -d "$di" ]; then
4527 _debug "Not directory, skip: $di"
4533 if _endswith "$d" "$ECC_SUFFIX"; then
4534 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
4535 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
4537 _initpath "$d" "$_isEcc"
4538 if [ -f "$DOMAIN_CONF" ]; then
4540 printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
4545 if _exists column; then
4546 list "raw" | column -t -s "$_sep"
4548 list "raw" | tr "$_sep" '\t'
4558 for _d_api in $(echo "$_hooks" | tr ',' " "); do
4559 _deployApi="$(_findHook "$_d" deploy "$_d_api")"
4560 if [ -z "$_deployApi" ]; then
4561 _err "The deploy hook $_d_api is not found."
4564 _debug _deployApi "$_deployApi"
4567 if ! . "$_deployApi"; then
4568 _err "Load file $_deployApi error. Please check your api file and try again."
4572 d_command="${_d_api}_deploy"
4573 if ! _exists "$d_command"; then
4574 _err "It seems that your api file is not correct, it must have a function named: $d_command"
4578 if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
4579 _err "Error deploy for domain:$_d"
4583 _err "Deploy error."
4586 _info "$(__green Success)"
4596 if [ -z "$_hooks" ]; then
4597 _usage "Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] "
4601 _initpath "$_d" "$_isEcc"
4602 if [ ! -d "$DOMAIN_PATH" ]; then
4603 _err "The domain '$_d' is not a cert name. You must use the cert name to specify the cert to install."
4604 _err "Can not find path:'$DOMAIN_PATH'"
4610 _savedomainconf Le_DeployHook "$_hooks"
4612 _deploy "$_d" "$_hooks"
4617 if [ -z "$_main_domain" ]; then
4618 _usage "Usage: $PROJECT_ENTRY --installcert -d domain.com [--ecc] [--cert-file cert-file-path] [--key-file key-file-path] [--ca-file ca-cert-file-path] [ --reloadCmd reloadCmd] [--fullchain-file fullchain-path]"
4626 _real_fullchain="$6"
4629 _initpath "$_main_domain" "$_isEcc"
4630 if [ ! -d "$DOMAIN_PATH" ]; then
4631 _err "The domain '$_main_domain' is not a cert name. You must use the cert name to specify the cert to install."
4632 _err "Can not find path:'$DOMAIN_PATH'"
4636 _savedomainconf "Le_RealCertPath" "$_real_cert"
4637 _savedomainconf "Le_RealCACertPath" "$_real_ca"
4638 _savedomainconf "Le_RealKeyPath" "$_real_key"
4639 _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
4640 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
4642 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
4645 #domain cert key ca fullchain reloadcmd backup-prefix
4651 _real_fullchain="$5"
4655 if [ "$_real_cert" = "$NO_VALUE" ]; then
4658 if [ "$_real_key" = "$NO_VALUE" ]; then
4661 if [ "$_real_ca" = "$NO_VALUE" ]; then
4664 if [ "$_reload_cmd" = "$NO_VALUE" ]; then
4667 if [ "$_real_fullchain" = "$NO_VALUE" ]; then
4671 _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
4672 mkdir -p "$_backup_path"
4674 if [ "$_real_cert" ]; then
4675 _info "Installing cert to:$_real_cert"
4676 if [ -f "$_real_cert" ] && [ ! "$IS_RENEW" ]; then
4677 cp "$_real_cert" "$_backup_path/cert.bak"
4679 cat "$CERT_PATH" >"$_real_cert" || return 1
4682 if [ "$_real_ca" ]; then
4683 _info "Installing CA to:$_real_ca"
4684 if [ "$_real_ca" = "$_real_cert" ]; then
4685 echo "" >>"$_real_ca"
4686 cat "$CA_CERT_PATH" >>"$_real_ca" || return 1
4688 if [ -f "$_real_ca" ] && [ ! "$IS_RENEW" ]; then
4689 cp "$_real_ca" "$_backup_path/ca.bak"
4691 cat "$CA_CERT_PATH" >"$_real_ca" || return 1
4695 if [ "$_real_key" ]; then
4696 _info "Installing key to:$_real_key"
4697 if [ -f "$_real_key" ] && [ ! "$IS_RENEW" ]; then
4698 cp "$_real_key" "$_backup_path/key.bak"
4700 if [ -f "$_real_key" ]; then
4701 cat "$CERT_KEY_PATH" >"$_real_key" || return 1
4703 cat "$CERT_KEY_PATH" >"$_real_key" || return 1
4704 chmod 600 "$_real_key"
4708 if [ "$_real_fullchain" ]; then
4709 _info "Installing full chain to:$_real_fullchain"
4710 if [ -f "$_real_fullchain" ] && [ ! "$IS_RENEW" ]; then
4711 cp "$_real_fullchain" "$_backup_path/fullchain.bak"
4713 cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain" || return 1
4716 if [ "$_reload_cmd" ]; then
4717 _info "Run reload cmd: $_reload_cmd"
4720 export CERT_KEY_PATH
4722 export CERT_FULLCHAIN_PATH
4724 cd "$DOMAIN_PATH" && eval "$_reload_cmd"
4726 _info "$(__green "Reload success")"
4728 _err "Reload error for :$Le_Domain"
4739 if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
4742 if ! _exists "$_CRONTAB"; then
4743 _err "crontab/fcrontab doesn't exist
, so
, we can not
install cron
jobs.
"
4744 _err "All your certs will not be renewed automatically.
"
4745 _err "You must add your own cron job to call
'$PROJECT_ENTRY --cron' everyday.
"
4749 _info "Installing cron job
"
4750 if ! $_CRONTAB -l | grep "$PROJECT_ENTRY --cron"; then
4751 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
4752 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
4754 _err "Can not
install cronjob
, $PROJECT_ENTRY not found.
"
4758 if [ "$_c_home" ]; then
4759 _c_entry="--config-home \"$_c_home\" "
4762 random_minute=$(_math $_t % 60)
4763 if _exists uname && uname -a | grep SunOS >/dev/null; then
4766 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev
/null
"
4771 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev
/null
"
4775 if [ "$?
" != "0" ]; then
4776 _err "Install cron job failed. You need to manually renew your certs.
"
4777 _err "Or you can add cronjob by yourself
:"
4778 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev
/null
"
4783 uninstallcronjob() {
4785 if ! _exists "$_CRONTAB" && _exists "fcrontab
"; then
4789 if ! _exists "$_CRONTAB"; then
4792 _info "Removing cron job
"
4793 cr="$
($_CRONTAB -l |
grep "$PROJECT_ENTRY --cron")"
4795 if _exists uname && uname -a | grep solaris >/dev/null; then
4796 $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d
" | $_CRONTAB --
4798 $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d
" | $_CRONTAB -
4800 LE_WORKING_DIR="$
(echo "$cr" | cut
-d ' ' -f 9 |
tr -d '"')"
4801 _info LE_WORKING_DIR "$LE_WORKING_DIR"
4802 if _contains "$cr" "--config-home"; then
4803 LE_CONFIG_HOME="$
(echo "$cr" | cut
-d ' ' -f 11 |
tr -d '"')"
4804 _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
4813 if [ -z "$Le_Domain" ]; then
4814 _usage "Usage
: $PROJECT_ENTRY --revoke -d domain.com
[--ecc]"
4820 _initpath "$Le_Domain" "$_isEcc"
4821 if [ ! -f "$DOMAIN_CONF" ]; then
4822 _err "$Le_Domain is not a issued domain
, skip.
"
4826 if [ ! -f "$CERT_PATH" ]; then
4827 _err "Cert
for $Le_Domain $CERT_PATH is not found
, skip.
"
4831 cert="$
(_getfile
"${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" |
tr -d "\r\n" | _url_replace
)"
4833 if [ -z "$cert" ]; then
4834 _err "Cert
for $Le_Domain is empty found
, skip.
"
4840 if [ "$ACME_VERSION" = "2" ]; then
4841 data="{\"certificate
\": \"$cert\"}"
4843 data="{\"resource
\": \"revoke-cert
\", \"certificate
\": \"$cert\"}"
4845 uri="${ACME_REVOKE_CERT}"
4847 if [ -f "$CERT_KEY_PATH" ]; then
4848 _info "Try domain key first.
"
4849 if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
4850 if [ -z "$response" ]; then
4851 _info "Revoke success.
"
4855 _err "Revoke error by domain key.
"
4860 _info "Domain key
file doesn
't exists."
4863 _info "Try account key."
4865 if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
4866 if [ -z "$response" ]; then
4867 _info "Revoke success."
4871 _err "Revoke error."
4881 if [ -z "$Le_Domain" ]; then
4882 _usage "Usage: $PROJECT_ENTRY --remove -d domain.com [--ecc]"
4888 _initpath "$Le_Domain" "$_isEcc"
4889 _removed_conf="$DOMAIN_CONF.removed"
4890 if [ ! -f "$DOMAIN_CONF" ]; then
4891 if [ -f "$_removed_conf" ]; then
4892 _err "$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH"
4894 _err "$Le_Domain is not a issued domain, skip."
4899 if mv "$DOMAIN_CONF" "$_removed_conf"; then
4900 _info "$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)"
4901 _info "You can remove them by yourself."
4904 _err "Remove $Le_Domain failed."
4915 if [ "$ACME_VERSION" = "2" ]; then
4916 _identifiers="{\"type\":\"dns\",\"value\":\"$_d_domain\"}"
4917 if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
4918 _err "Can not get domain new order."
4921 _authorizations_seg="$(echo "$response" | tr -d '\r\n' | _egrep_o '"authorizations" *: *\
[[^\
]*\
]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
4922 _debug2 _authorizations_seg
"$_authorizations_seg"
4923 if [ -z "$_authorizations_seg" ]; then
4924 _err
"_authorizations_seg not found."
4926 _on_issue_err
"$_post_hook"
4930 authzUri
="$_authorizations_seg"
4931 _debug2
"authzUri" "$authzUri"
4932 if ! _send_signed_request
"$authzUri"; then
4933 _err
"get to authz error."
4934 _err
"_authorizations_seg" "$_authorizations_seg"
4935 _err
"authzUri" "$authzUri"
4937 _on_issue_err
"$_post_hook"
4941 response
="$(echo "$response" | _normalizeJson)"
4942 _debug2 response
"$response"
4945 if ! __get_domain_new_authz
"$_d_domain"; then
4946 _err
"Can not get domain new authz token."
4950 authzUri
="$(echo "$responseHeaders" | grep "^Location
:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
4951 _debug
"authzUri" "$authzUri"
4952 if [ "$code" ] && [ ! "$code" = '201' ]; then
4953 _err
"new-authz error: $response"
4959 entries
="$(echo "$response" | _egrep_o "{ *\"type\":\"[^
\"]*\", *\"status
\": *\"valid
\", *\"$_URL_NAME\"[^
}]*")"
4960 if [ -z "$entries" ]; then
4961 _info
"No valid entries found."
4962 if [ -z "$thumbprint" ]; then
4963 thumbprint
="$(__calc_account_thumbprint)"
4965 _debug
"Trigger validation."
4967 entry
="$(printf "%s
\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
4968 _debug entry
"$entry"
4969 if [ -z "$entry" ]; then
4970 _err
"Error, can not get domain token $d"
4973 token
="$(printf "%s
\n" "$entry" | _egrep_o '"token
":"[^
"]*' | cut -d : -f 2 | tr -d '"')"
4974 _debug token "$token"
4976 uri="$(printf "%s\n" "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')"
4979 keyauthorization
="$token.$thumbprint"
4980 _debug keyauthorization
"$keyauthorization"
4981 __trigger_validation
"$uri" "$keyauthorization"
4986 _d_max_retry
=$
(echo "$entries" |
wc -l)
4987 while [ "$_d_i" -lt "$_d_max_retry" ]; do
4988 _info
"Deactivate: $_d_domain"
4989 _d_i
="$(_math $_d_i + 1)"
4990 entry
="$(echo "$entries" | sed -n "${_d_i}p
")"
4991 _debug entry
"$entry"
4993 if [ -z "$entry" ]; then
4994 _info
"No more valid entry found."
4998 _vtype
="$(printf "%s
\n" "$entry" | _egrep_o '"type": *"[^
"]*"' | cut -d : -f 2 | tr -d '"')"
4999 _debug _vtype
"$_vtype"
5000 _info
"Found $_vtype"
5002 uri
="$(printf "%s
\n" "$entry" | _egrep_o "\"$_URL_NAME\":\"[^
\"]*" | cut -d : -f 2,3 | tr -d '"')"
5005 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
5006 _info "Skip $_vtype"
5010 _info "Deactivate: $_vtype"
5012 if [ "$ACME_VERSION" = "2" ]; then
5013 _djson="{\"status\":\"deactivated\"}"
5015 _djson="{\"resource\": \"authz\", \"status\":\"deactivated\"}"
5018 if _send_signed_request "$authzUri" "$_djson" && _contains "$response" '"deactivated"'; then
5019 _info "Deactivate: $_vtype success."
5021 _err "Can not deactivate $_vtype."
5027 if [ "$_d_i" -eq "$_d_max_retry" ]; then
5028 _info "Deactivated success!"
5030 _err "Deactivate failed."
5040 _debug _d_domain_list "$_d_domain_list"
5041 if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
5042 _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
5045 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
5046 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
5049 if ! _deactivate "$_d_dm" "$_d_type"; then
5055 # Detect profile file if not specified as environment variable
5057 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
5063 SHELLTYPE="$(basename "/$SHELL")"
5065 if [ "$SHELLTYPE" = "bash" ]; then
5066 if [ -f "$HOME/.bashrc" ]; then
5067 DETECTED_PROFILE="$HOME/.bashrc"
5068 elif [ -f "$HOME/.bash_profile" ]; then
5069 DETECTED_PROFILE="$HOME/.bash_profile"
5071 elif [ "$SHELLTYPE" = "zsh" ]; then
5072 DETECTED_PROFILE="$HOME/.zshrc"
5075 if [ -z "$DETECTED_PROFILE" ]; then
5076 if [ -f "$HOME/.profile" ]; then
5077 DETECTED_PROFILE="$HOME/.profile"
5078 elif [ -f "$HOME/.bashrc" ]; then
5079 DETECTED_PROFILE="$HOME/.bashrc"
5080 elif [ -f "$HOME/.bash_profile" ]; then
5081 DETECTED_PROFILE="$HOME/.bash_profile"
5082 elif [ -f "$HOME/.zshrc" ]; then
5083 DETECTED_PROFILE="$HOME/.zshrc"
5087 echo "$DETECTED_PROFILE"
5092 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
5095 #LOG_FILE=\"$DEFAULT_LOG_FILE\"
5102 " >"$ACCOUNT_CONF_PATH"
5110 if ! _exists "curl" && ! _exists "wget"; then
5111 _err "Please install curl or wget first, we need to access http resources."
5115 if [ -z "$_nocron" ]; then
5116 if ! _exists "crontab" && ! _exists "fcrontab"; then
5117 _err "It is recommended to install crontab first. try to install 'cron
, crontab
, crontabs or vixie-cron
'."
5118 _err "We need to set cron job to renew the certs automatically."
5119 _err "Otherwise, your certs will not be able to be renewed automatically."
5120 if [ -z "$FORCE" ]; then
5121 _err "Please add '--force' and try install again to go without crontab."
5122 _err "./$PROJECT_ENTRY --install --force"
5128 if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then
5129 _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN"
5130 _err "We need openssl to generate keys."
5134 if ! _exists "socat"; then
5135 _err "It is recommended to install socat first."
5136 _err "We use socat for standalone server if you use standalone mode."
5137 _err "If you don't use standalone mode
, just ignore this warning.
"
5146 if [ -z "$_shebang" ]; then
5147 _usage "Usage
: file shebang
"
5150 cp "$_file" "$_file.tmp
"
5151 echo "$_shebang" >"$_file"
5152 sed -n 2,99999p "$_file.tmp
" >>"$_file"
5161 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env
"
5162 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
5163 echo "$
(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.
*$||
" >"$_envfile"
5164 echo "$
(cat "$_envfile")" | sed "s|^
alias le.
*$||
" >"$_envfile"
5165 echo "$
(cat "$_envfile")" | sed "s|^
alias le.sh.
*$||
" >"$_envfile"
5168 if [ "$_c_home" ]; then
5169 _c_entry=" --config-home '$_c_home'"
5172 _setopt "$_envfile" "export LE_WORKING_DIR
" "=" "\"$LE_WORKING_DIR\""
5173 if [ "$_c_home" ]; then
5174 _setopt "$_envfile" "export LE_CONFIG_HOME
" "=" "\"$LE_CONFIG_HOME\""
5176 _sed_i "/^
export LE_CONFIG_HOME
/d
" "$_envfile"
5178 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
5180 _profile="$
(_detect_profile
)"
5181 if [ "$_profile" ]; then
5182 _debug "Found profile
: $_profile"
5183 _info "Installing
alias to
'$_profile'"
5184 _setopt "$_profile" ".
\"$_envfile\""
5185 _info "OK
, Close and reopen your terminal to start using
$PROJECT_NAME"
5187 _info "No profile is found
, you will need to go into
$LE_WORKING_DIR to use
$PROJECT_NAME"
5191 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh
"
5192 _csh_profile="$HOME/.cshrc
"
5193 if [ -f "$_csh_profile" ]; then
5194 _info "Installing
alias to
'$_csh_profile'"
5195 _setopt "$_cshfile" "setenv LE_WORKING_DIR
" " " "\"$LE_WORKING_DIR\""
5196 if [ "$_c_home" ]; then
5197 _setopt "$_cshfile" "setenv LE_CONFIG_HOME
" " " "\"$LE_CONFIG_HOME\""
5199 _sed_i "/^setenv LE_CONFIG_HOME
/d
" "$_cshfile"
5201 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
5202 _setopt "$_csh_profile" "source \"$_cshfile\""
5206 _tcsh_profile="$HOME/.tcshrc
"
5207 if [ -f "$_tcsh_profile" ]; then
5208 _info "Installing
alias to
'$_tcsh_profile'"
5209 _setopt "$_cshfile" "setenv LE_WORKING_DIR
" " " "\"$LE_WORKING_DIR\""
5210 if [ "$_c_home" ]; then
5211 _setopt "$_cshfile" "setenv LE_CONFIG_HOME
" " " "\"$LE_CONFIG_HOME\""
5213 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
5214 _setopt "$_tcsh_profile" "source \"$_cshfile\""
5219 # nocron confighome noprofile
5222 if [ -z "$LE_WORKING_DIR" ]; then
5223 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
5229 if ! _initpath; then
5230 _err "Install failed.
"
5233 if [ "$_nocron" ]; then
5234 _debug "Skip
install cron job
"
5237 if [ "$IN_CRON" != "1" ]; then
5238 if ! _precheck "$_nocron"; then
5239 _err "Pre-check failed
, can not
install.
"
5244 if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then
5245 _info "Using config home
: $LE_CONFIG_HOME"
5246 _c_home="$LE_CONFIG_HOME"
5250 if [ -d "$HOME/.le
" ]; then
5251 for envfile in "le.env
" "le.sh.env
"; do
5252 if [ -f "$HOME/.le
/$envfile" ]; then
5253 if grep "le.sh
" "$HOME/.le
/$envfile" >/dev/null; then
5255 _info "You are upgrading from le.sh
"
5256 _info "Renaming
\"$HOME/.le
\" to
$LE_WORKING_DIR"
5257 mv "$HOME/.le
" "$LE_WORKING_DIR"
5258 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env
"
5265 _info "Installing to
$LE_WORKING_DIR"
5267 if [ ! -d "$LE_WORKING_DIR" ]; then
5268 if ! mkdir -p "$LE_WORKING_DIR"; then
5269 _err "Can not create working dir
: $LE_WORKING_DIR"
5273 chmod 700 "$LE_WORKING_DIR"
5276 if [ ! -d "$LE_CONFIG_HOME" ]; then
5277 if ! mkdir -p "$LE_CONFIG_HOME"; then
5278 _err "Can not create config dir
: $LE_CONFIG_HOME"
5282 chmod 700 "$LE_CONFIG_HOME"
5285 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
5287 if [ "$?
" != "0" ]; then
5288 _err "Install failed
, can not copy
$PROJECT_ENTRY"
5292 _info "Installed to
$LE_WORKING_DIR/$PROJECT_ENTRY"
5294 if [ "$IN_CRON" != "1" ] && [ -z "$_noprofile" ]; then
5295 _installalias "$_c_home"
5298 for subf in $_SUB_FOLDERS; do
5299 if [ -d "$subf" ]; then
5300 mkdir -p "$LE_WORKING_DIR/$subf"
5301 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
5305 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
5309 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
5310 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH
" "=" "\"$ACCOUNT_CONF_PATH\""
5313 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
5314 _saveaccountconf "CERT_HOME
" "$CERT_HOME"
5317 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
5318 _saveaccountconf "ACCOUNT_KEY_PATH
" "$ACCOUNT_KEY_PATH"
5321 if [ -z "$_nocron" ]; then
5322 installcronjob "$_c_home"
5325 if [ -z "$NO_DETECT_SH" ]; then
5327 if _exists bash; then
5328 _bash_path="$
(bash
-c "command -v bash 2>/dev/null")"
5329 if [ -z "$_bash_path" ]; then
5330 _bash_path="$
(bash
-c 'echo $SHELL')"
5333 if [ "$_bash_path" ]; then
5334 _info "Good
, bash is found
, so change the shebang to use bash as preferred.
"
5335 _shebang='#!'"$_bash_path"
5336 _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
5337 for subf in $_SUB_FOLDERS; do
5338 if [ -d "$LE_WORKING_DIR/$subf" ]; then
5339 for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
5340 _setShebang "$_apifile" "$_shebang"
5353 if [ -z "$_nocron" ]; then
5360 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
5361 _info "The keys and certs are
in \"$
(__green
"$LE_CONFIG_HOME")\", you can remove them by yourself.
"
5368 _profile="$
(_detect_profile
)"
5369 if [ "$_profile" ]; then
5370 _info "Uninstalling
alias from
: '$_profile'"
5371 text="$
(cat "$_profile")"
5372 echo "$text" | sed "s|^.
*\"$LE_WORKING_DIR/$PROJECT_NAME.env
\"$||
" >"$_profile"
5375 _csh_profile="$HOME/.cshrc
"
5376 if [ -f "$_csh_profile" ]; then
5377 _info "Uninstalling
alias from
: '$_csh_profile'"
5378 text="$
(cat "$_csh_profile")"
5379 echo "$text" | sed "s|^.
*\"$LE_WORKING_DIR/$PROJECT_NAME.csh
\"$||
" >"$_csh_profile"
5382 _tcsh_profile="$HOME/.tcshrc
"
5383 if [ -f "$_tcsh_profile" ]; then
5384 _info "Uninstalling
alias from
: '$_csh_profile'"
5385 text="$
(cat "$_tcsh_profile")"
5386 echo "$text" | sed "s|^.
*\"$LE_WORKING_DIR/$PROJECT_NAME.csh
\"$||
" >"$_tcsh_profile"
5394 _info "$
(__green
"===Starting cron===")"
5395 if [ "$AUTO_UPGRADE" = "1" ]; then
5396 export LE_WORKING_DIR
5399 _err "Cron
:Upgrade failed
!"
5403 . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
5409 _info "Auto upgraded to
: $VER"
5414 _info "$
(__green
"===End cron===")"
5426 echo "Usage
: $PROJECT_ENTRY command ...
[parameters
]....
5428 --help, -h Show this
help message.
5429 --version, -v Show version info.
5430 --install Install
$PROJECT_NAME to your system.
5431 --uninstall Uninstall
$PROJECT_NAME, and uninstall the cron job.
5432 --upgrade Upgrade
$PROJECT_NAME to the latest code from
$PROJECT.
5433 --issue Issue a cert.
5434 --signcsr Issue a cert from an existing csr.
5435 --deploy Deploy the cert to your server.
5436 --install-cert Install the issued cert to apache
/nginx or any other server.
5437 --renew, -r Renew a cert.
5438 --renew-all Renew all the certs.
5439 --revoke Revoke a cert.
5440 --remove Remove the cert from list of certs known to
$PROJECT_NAME.
5441 --list List all the certs.
5442 --showcsr Show the content of a csr.
5443 --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.
5444 --uninstall-cronjob Uninstall the cron job. The 'uninstall
' command can do this automatically.
5445 --cron Run cron job to renew all the certs.
5446 --toPkcs Export the certificate and key to a pfx file.
5447 --toPkcs8 Convert to pkcs8 format.
5448 --update-account Update account info.
5449 --register-account Register account key.
5450 --deactivate-account Deactivate the account.
5451 --create-account-key Create an account private key, professional use.
5452 --create-domain-key Create an domain private key, professional use.
5453 --createCSR, -ccsr Create CSR , professional use.
5454 --deactivate Deactivate the domain authz, professional use.
5457 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
5458 --challenge-alias domain.tld The challenge domain alias for DNS alias mode: $_DNS_ALIAS_WIKI
5459 --domain-alias domain.tld The domain alias for DNS alias mode: $_DNS_ALIAS_WIKI
5460 --force, -f Used to force to install or force to renew a cert immediately.
5461 --staging, --test Use staging server, just for test.
5462 --debug Output debug info.
5463 --output-insecure Output all the sensitive messages. By default all the credentials/sensitive messages are hidden from the output/debug/log for secure.
5464 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
5465 --standalone Use standalone mode.
5466 --alpn Use standalone alpn mode.
5467 --stateless Use stateless mode, see: $_STATELESS_WIKI
5468 --apache Use apache mode.
5469 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
5470 --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.
5472 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
5473 --accountkeylength, -ak [2048] Specifies the account key length.
5474 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a
file path here.
5475 --log-level 1|
2 Specifies the log level
, default is
1.
5476 --syslog [0|
3|
6|
7] Syslog level
, 0: disable syslog
, 3: error
, 6: info
, 7: debug.
5478 These parameters are to
install the cert to nginx
/apache or any other server after issue
/renew a cert
:
5480 --cert-file After issue
/renew
, the cert will be copied to this path.
5481 --key-file After issue
/renew
, the key will be copied to this path.
5482 --ca-file After issue
/renew
, the intermediate cert will be copied to this path.
5483 --fullchain-file After issue
/renew
, the fullchain cert will be copied to this path.
5485 --reloadcmd \"service nginx reload
\" After issue
/renew
, it
's used to reload the server.
5487 --server SERVER ACME Directory Resource URI. (default: https://acme-v01.api.letsencrypt.org/directory)
5488 --accountconf Specifies a customized account config file.
5489 --home Specifies the home dir for $PROJECT_NAME.
5490 --cert-home Specifies the home dir to save all the certs, only valid for '--install' command.
5491 --config-home Specifies the home dir to save all the configurations.
5492 --useragent Specifies the user agent string. it will be saved for future use too.
5493 --accountemail Specifies the account email, only valid for the '--install' and '--update-account' command.
5494 --accountkey Specifies the account key path, only valid for the '--install' command.
5495 --days Specifies the days to renew the cert when using '--issue' command. The default value is $DEFAULT_RENEW days.
5496 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
5497 --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
5498 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
5499 --listraw Only used for '--list' command, list the certs in raw format.
5500 --stopRenewOnError, -se Only valid for '--renew-all' command. Stop if one cert has error in renewal.
5501 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
5502 --ca-bundle Specifies the path to the CA certificate bundle to verify api server
's certificate.
5503 --ca-path Specifies directory containing CA certificates in PEM format, used by wget or curl.
5504 --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.
5505 --no-color Do not output color text.
5506 --force-color Force output of color text. Useful for non-interactive use with the aha tool for HTML E-Mails.
5507 --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
5508 --csr Specifies the input csr.
5509 --pre-hook Command to be run before obtaining any certificates.
5510 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obtain/renew is success or failed.
5511 --renew-hook Command to be run once for each successfully renewed certificate.
5512 --deploy-hook The hook file to deploy cert
5513 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
5514 --always-force-new-domain-key Generate new domain key when renewal. Otherwise, the domain key is not changed by default.
5515 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
5516 --listen-v4 Force standalone/tls server to listen at ipv4.
5517 --listen-v6 Force standalone/tls server to listen at ipv6.
5518 --openssl-bin Specifies a custom openssl bin location.
5519 --use-wget Force to use wget, if you have both curl and wget installed.
5520 --yes-I-know-dns-manual-mode-enough-go-ahead-please Force to use dns manual mode: $_DNS_MANUAL_WIKI
5521 --branch, -b Only valid for '--upgrade' command, specifies the branch name to upgrade to.
5527 _info "Installing from online archive."
5530 if [ ! "$BRANCH" ]; then
5534 target="$PROJECT/archive/$BRANCH.tar.gz"
5535 _info "Downloading $target"
5536 localname="$BRANCH.tar.gz"
5537 if ! _get "$target" >$localname; then
5538 _err "Download error."
5542 _info "Extracting $localname"
5543 if ! (tar xzf $localname || gtar xzf $localname); then
5544 _err "Extraction error."
5548 cd "$PROJECT_NAME-$BRANCH"
5549 chmod +x $PROJECT_ENTRY
5550 if ./$PROJECT_ENTRY install "$_nocron" "" "$_noprofile"; then
5551 _info "Install success!"
5556 rm -rf "$PROJECT_NAME-$BRANCH"
5564 export LE_WORKING_DIR
5565 cd "$LE_WORKING_DIR"
5566 _installOnline "nocron" "noprofile"
5568 _info "Upgrade success!"
5571 _err "Upgrade failed!"
5576 _processAccountConf() {
5577 if [ "$_useragent" ]; then
5578 _saveaccountconf "USER_AGENT" "$_useragent"
5579 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
5580 _saveaccountconf "USER_AGENT" "$USER_AGENT"
5583 if [ "$_accountemail" ]; then
5584 _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
5585 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
5586 _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
5589 if [ "$_openssl_bin" ]; then
5590 _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin"
5591 elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
5592 _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
5595 if [ "$_auto_upgrade" ]; then
5596 _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
5597 elif [ "$AUTO_UPGRADE" ]; then
5598 _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
5601 if [ "$_use_wget" ]; then
5602 _saveaccountconf "ACME_USE_WGET" "$_use_wget"
5603 elif [ "$ACME_USE_WGET" ]; then
5604 _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET"
5612 _altdomains="$NO_VALUE"
5616 _accountkeylength=""
5633 _stopRenewOnError=""
5655 while [ ${#} -gt 0 ]; do
5687 --installcert | -i | --install-cert)
5693 --renewAll | --renewall | --renew-all)
5705 --installcronjob | --install-cronjob)
5706 _CMD="installcronjob"
5708 --uninstallcronjob | --uninstall-cronjob)
5709 _CMD="uninstallcronjob"
5720 --createAccountKey | --createaccountkey | -cak | --create-account-key)
5721 _CMD="createAccountKey"
5723 --createDomainKey | --createdomainkey | -cdk | --create-domain-key)
5724 _CMD="createDomainKey"
5726 --createCSR | --createcsr | -ccr)
5732 --updateaccount | --update-account)
5733 _CMD="updateaccount"
5735 --registeraccount | --register-account)
5736 _CMD="registeraccount"
5738 --deactivate-account)
5739 _CMD="deactivateaccount"
5744 if [ "$_dvalue" ]; then
5745 if _startswith "$_dvalue" "-"; then
5746 _err "'$_dvalue' is not a valid domain for parameter '$1'"
5749 if _is_idn "$_dvalue" && ! _exists idn; then
5750 _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn
' command first."
5754 if _startswith "$_dvalue" "*."; then
5755 _debug "Wildcard domain"
5756 export ACME_VERSION=2
5758 if [ -z "$_domain" ]; then
5761 if [ "$_altdomains" = "$NO_VALUE" ]; then
5762 _altdomains="$_dvalue"
5764 _altdomains="$_altdomains,$_dvalue"
5780 _server="$ACME_DIRECTORY"
5781 export ACME_DIRECTORY
5785 if [ -z "$2" ] || _startswith "$2" "-"; then
5786 DEBUG="$DEBUG_LEVEL_DEFAULT"
5793 export OUTPUT_INSECURE=1
5797 if [ -z "$_webroot" ]; then
5800 _webroot="$_webroot,$wvalue"
5806 _challenge_alias="$_challenge_alias$cvalue,"
5810 cvalue="$DNS_ALIAS_PREFIX$2"
5811 _challenge_alias="$_challenge_alias$cvalue,"
5816 if [ -z "$_webroot" ]; then
5819 _webroot="$_webroot,$wvalue"
5824 if [ -z "$_webroot" ]; then
5827 _webroot="$_webroot,$wvalue"
5831 wvalue="$MODE_STATELESS"
5832 if [ -z "$_webroot" ]; then
5835 _webroot="$_webroot,$wvalue"
5840 _local_address="$_local_address$lvalue,"
5845 if [ -z "$_webroot" ]; then
5848 _webroot="$_webroot,$wvalue"
5853 if [ -z "$_webroot" ]; then
5856 _webroot="$_webroot,$wvalue"
5861 if [ "$2" ] && ! _startswith "$2" "-"; then
5865 if [ -z "$_webroot" ]; then
5868 _webroot="$_webroot,$wvalue"
5873 Le_DNSSleep="$_dnssleep"
5881 --accountkeylength | -ak)
5882 _accountkeylength="$2"
5886 --cert-file | --certpath)
5890 --key-file | --keypath)
5894 --ca-file | --capath)
5898 --fullchain-file | --fullchainpath)
5899 _fullchain_file="$2"
5902 --reloadcmd | --reloadCmd)
5912 ACCOUNT_CONF_PATH="$_accountconf"
5919 --certhome | --cert-home)
5921 CERT_HOME="$_certhome"
5926 LE_CONFIG_HOME="$_confighome"
5931 USER_AGENT="$_useragent"
5936 ACCOUNT_EMAIL="$_accountemail"
5941 ACCOUNT_KEY_PATH="$_accountkey"
5946 Le_RenewalDays="$_days"
5951 Le_HTTPPort="$_httpport"
5956 Le_TLSPort="$_tlsport"
5962 --stopRenewOnError | --stoprenewonerror | -se)
5963 _stopRenewOnError="1"
5970 _ca_bundle="$(_readlink "$2")"
5971 CA_BUNDLE="$_ca_bundle"
5983 export ACME_NO_COLOR=1
5986 export ACME_FORCE_COLOR=1
6008 if [ -z "$2" ] || _startswith "$2" "-"; then
6009 _usage "Please specify a value for '--deploy-hook'"
6012 _deploy_hook="$_deploy_hook$2,"
6015 --ocsp-must-staple | --ocsp)
6018 --always-force-new-domain-key)
6019 if [ -z "$2" ] || _startswith "$2" "-"; then
6020 Le_ForceNewDomainKey=1
6022 Le_ForceNewDomainKey="$2"
6026 --yes-I-know-dns-manual-mode-enough-go-ahead-please)
6027 export FORCE_DNS_MANUAL=1
6032 if _startswith "$_logfile" '-'; then
6037 LOG_FILE="$_logfile"
6038 if [ -z "$LOG_LEVEL" ]; then
6039 LOG_LEVEL="$DEFAULT_LOG_LEVEL"
6044 LOG_LEVEL="$_log_level"
6048 if ! _startswith "$2" '-'; then
6052 if [ -z "$_syslog" ]; then
6053 _syslog="$SYSLOG_LEVEL_DEFAULT"
6058 if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
6063 AUTO_UPGRADE="$_auto_upgrade"
6067 Le_Listen_V4="$_listen_v4"
6071 Le_Listen_V6="$_listen_v6"
6075 ACME_OPENSSL_BIN="$_openssl_bin"
6087 _err "Unknown parameter : $1"
6095 if [ "${_CMD}" != "install" ]; then
6097 if [ "$_log" ]; then
6098 if [ -z "$_logfile" ]; then
6099 _logfile="$DEFAULT_LOG_FILE"
6102 if [ "$_logfile" ]; then
6103 _saveaccountconf "LOG_FILE" "$_logfile"
6104 LOG_FILE="$_logfile"
6107 if [ "$_log_level" ]; then
6108 _saveaccountconf "LOG_LEVEL" "$_log_level"
6109 LOG_LEVEL="$_log_level"
6112 if [ "$_syslog" ]; then
6113 if _exists logger; then
6114 if [ "$_syslog" = "0" ]; then
6115 _clearaccountconf "SYS_LOG"
6117 _saveaccountconf "SYS_LOG" "$_syslog"
6121 _err "The 'logger
' command is not found, can not enable syslog."
6122 _clearaccountconf "SYS_LOG"
6130 _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
6132 if [ "$DEBUG" ]; then
6134 if [ "$_server" ]; then
6135 _debug "Using server: $_server"
6140 install) install "$_nocron" "$_confighome" ;;
6141 uninstall) uninstall "$_nocron" ;;
6144 issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias"
6147 deploy "$_domain" "$_deploy_hook" "$_ecc"
6150 signcsr "$_csr" "$_webroot" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias"
6153 showcsr "$_csr" "$_domain"
6156 installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
6159 renew "$_domain" "$_ecc"
6162 renewAll "$_stopRenewOnError"
6165 revoke "$_domain" "$_ecc"
6168 remove "$_domain" "$_ecc"
6171 deactivate "$_domain,$_altdomains"
6174 registeraccount "$_accountkeylength"
6185 installcronjob) installcronjob "$_confighome" ;;
6186 uninstallcronjob) uninstallcronjob ;;
6189 toPkcs "$_domain" "$_password" "$_ecc"
6192 toPkcs8 "$_domain" "$_ecc"
6195 createAccountKey "$_accountkeylength"
6198 createDomainKey "$_domain" "$_keylength"
6201 createCSR "$_domain" "$_altdomains" "$_ecc"
6205 if [ "$_CMD" ]; then
6206 _err "Invalid command: $_CMD"
6213 if [ "$_ret" != "0" ]; then
6217 if [ "${_CMD}" = "install" ]; then
6218 if [ "$_log" ]; then
6219 if [ -z "$LOG_FILE" ]; then
6220 LOG_FILE="$DEFAULT_LOG_FILE"
6222 _saveaccountconf "LOG_FILE" "$LOG_FILE"
6225 if [ "$_log_level" ]; then
6226 _saveaccountconf "LOG_LEVEL" "$_log_level"
6229 if [ "$_syslog" ]; then
6230 if _exists logger; then
6231 if [ "$_syslog" = "0" ]; then
6232 _clearaccountconf "SYS_LOG"
6234 _saveaccountconf "SYS_LOG" "$_syslog"
6237 _err "The 'logger
' command is not found, can not enable syslog."
6238 _clearaccountconf "SYS_LOG"
6248 if [ "$INSTALLONLINE" ]; then
6255 [ -z "$1" ] && showhelp && return
6256 if _startswith "$1" '-'; then _process "$@"; else "$@"; fi