7 PROJECT_ENTRY
="acme.sh"
9 PROJECT
="https://github.com/Neilpang/$PROJECT_NAME"
11 DEFAULT_INSTALL_HOME
="$HOME/.$PROJECT_NAME"
14 _SUB_FOLDERS
="dnsapi deploy"
16 DEFAULT_CA
="https://acme-v01.api.letsencrypt.org"
17 DEFAULT_AGREEMENT
="https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf"
19 DEFAULT_USER_AGENT
="$PROJECT_NAME/$VER ($PROJECT)"
20 DEFAULT_ACCOUNT_EMAIL
=""
22 DEFAULT_ACCOUNT_KEY_LENGTH
=2048
23 DEFAULT_DOMAIN_KEY_LENGTH
=2048
25 DEFAULT_OPENSSL_BIN
="openssl"
27 STAGE_CA
="https://acme-staging.api.letsencrypt.org"
31 VTYPE_TLS
="tls-sni-01"
32 #VTYPE_TLS2="tls-sni-02"
34 LOCAL_ANY_ADDRESS
="0.0.0.0"
44 MODE_STATELESS
="stateless"
46 STATE_VERIFIED
="verified_ok"
49 NGINX_START
="#ACME_NGINX_START"
50 NGINX_END
="#ACME_NGINX_END"
52 BEGIN_CSR
="-----BEGIN CERTIFICATE REQUEST-----"
53 END_CSR
="-----END CERTIFICATE REQUEST-----"
55 BEGIN_CERT
="-----BEGIN CERTIFICATE-----"
56 END_CERT
="-----END CERTIFICATE-----"
61 ECC_SUFFIX
="${ECC_SEP}ecc"
66 DEFAULT_LOG_LEVEL
="$LOG_LEVEL_1"
71 DEBUG_LEVEL_DEFAULT
=$DEBUG_LEVEL_1
74 HIDDEN_VALUE
="[hidden](please add '--output-insecure' to see this value)"
76 SYSLOG_ERROR
="user.error"
77 SYSLOG_INFO
="user.info"
78 SYSLOG_DEBUG
="user.debug"
87 SYSLOG_LEVEL_DEBUG_2
=8
89 SYSLOG_LEVEL_DEBUG_3
=9
91 SYSLOG_LEVEL_DEFAULT
=$SYSLOG_LEVEL_ERROR
95 _DEBUG_WIKI
="https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh"
97 _PREPARE_LINK
="https://github.com/Neilpang/acme.sh/wiki/Install-preparations"
99 _STATELESS_WIKI
="https://github.com/Neilpang/acme.sh/wiki/Stateless-Mode"
107 if [ "$__INTERACTIVE" ]; then
108 printf '\033[1;31;32m'
111 if [ "$__INTERACTIVE" ]; then
117 if [ "$__INTERACTIVE" ]; then
118 printf '\033[1;31;40m'
121 if [ "$__INTERACTIVE" ]; then
127 if [ -z "$NO_TIMESTAMP" ] ||
[ "$NO_TIMESTAMP" = "0" ]; then
128 printf -- "%s" "[$(date)] "
133 printf -- "%s" "$1='$2'"
139 echo "Diagnosis versions: "
140 echo "openssl:$ACME_OPENSSL_BIN"
141 if _exists
"${ACME_OPENSSL_BIN:-openssl}"; then
142 ${ACME_OPENSSL_BIN:-openssl} version
2>&1
144 echo "$ACME_OPENSSL_BIN doesn't exists."
148 if [ "$_APACHECTL" ] && _exists
"$_APACHECTL"; then
151 echo "apache doesn't exists."
155 if _exists
"nc"; then
158 _debug
"nc doesn't exists."
164 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" = "$SYSLOG_LEVEL_NONE" ]; then
169 if [ -z "$__logger_i" ]; then
170 if _contains
"$(logger --help 2>&1)" "-i"; then
171 __logger_i
="logger -i"
176 $__logger_i -t "$PROJECT_NAME" -p "$_logclass" "$(_printargs "$@
")" >/dev
/null
2>&1
180 [ -z "$LOG_FILE" ] && return
181 _printargs
"$@" >>"$LOG_FILE"
186 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_INFO" ]; then
187 _syslog
"$SYSLOG_INFO" "$@"
193 _syslog
"$SYSLOG_ERROR" "$@"
195 if [ -z "$NO_TIMESTAMP" ] ||
[ "$NO_TIMESTAMP" = "0" ]; then
196 printf -- "%s" "[$(date)] " >&2
213 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
216 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
217 _syslog
"$SYSLOG_DEBUG" "$@"
219 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
224 #output the sensitive messages
226 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
227 if [ "$OUTPUT_INSECURE" = "1" ]; then
230 _log
"$1" "$HIDDEN_VALUE"
233 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
234 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
236 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
237 if [ "$OUTPUT_INSECURE" = "1" ]; then
240 _printargs
"$1" "$HIDDEN_VALUE" >&2
246 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
249 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
250 _syslog
"$SYSLOG_DEBUG" "$@"
252 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
258 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
259 if [ "$OUTPUT_INSECURE" = "1" ]; then
262 _log
"$1" "$HIDDEN_VALUE"
265 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
266 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
268 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
269 if [ "$OUTPUT_INSECURE" = "1" ]; then
272 _printargs
"$1" "$HIDDEN_VALUE" >&2
278 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
281 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
282 _syslog
"$SYSLOG_DEBUG" "$@"
284 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
290 if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
291 if [ "$OUTPUT_INSECURE" = "1" ]; then
294 _log
"$1" "$HIDDEN_VALUE"
297 if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
298 _syslog
"$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
300 if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
301 if [ "$OUTPUT_INSECURE" = "1" ]; then
304 _printargs
"$1" "$HIDDEN_VALUE" >&2
310 # shellcheck disable=SC2018,SC2019
315 # shellcheck disable=SC2018,SC2019
322 echo "$_str" |
grep "^$_sub" >/dev
/null
2>&1
328 echo "$_str" |
grep -- "$_sub\$" >/dev
/null
2>&1
334 echo "$_str" |
grep -- "$_sub" >/dev
/null
2>&1
341 if [ -z "$_field" ]; then
342 _usage
"Usage: str field [sep]"
346 if [ -z "$_sep" ]; then
350 for f
in $
(echo "$_str" |
tr "$_sep" ' '); do
351 if [ "$f" = "$_field" ]; then
352 _debug2
"'$_str' contains '$_field'"
353 return 0 #contains ok
356 _debug2
"'$_str' does not contain '$_field'"
357 return 1 #not contains
365 if [ -z "$_findex" ]; then
366 _usage
"Usage: str field [sep]"
370 if [ -z "$_sep" ]; then
375 while [ "$_ffi" -gt "0" ]; do
376 _fv
="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")"
378 printf -- "%s" "$_fv"
381 _ffi
="$(_math "$_ffi" - 1)"
384 printf -- "%s" "$_str"
390 if [ -z "$cmd" ]; then
391 _usage
"Usage: _exists cmd"
395 if eval type type >/dev
/null
2>&1; then
396 eval type "$cmd" >/dev
/null
2>&1
397 elif command >/dev
/null
2>&1; then
398 command -v "$cmd" >/dev
/null
2>&1
400 which "$cmd" >/dev
/null
2>&1
403 _debug3
"$cmd exists=$ret"
410 printf "%s" "$(($_m_opts))"
442 if [ "$(printf '\x41')" != 'A' ]; then
455 _debug2 _URGLY_PRINTF
"$_URGLY_PRINTF"
456 if [ -z "$_URGLY_PRINTF" ]; then
457 if _exists
xargs; then
459 echo "$hex" |
sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' |
xargs printf
461 for h
in $
(echo "$hex" |
sed 's/\([0-9A-F]\{2\}\)/ \1/gI'); do
469 for c
in $
(echo "$hex" |
sed 's/\([0-9A-F]\)/ \1/gI'); do
470 if [ -z "$ic" ]; then
475 ic
="$(_h_char_2_dec "$ic")"
476 jc
="$(_h_char_2_dec "$jc")"
477 printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")""%s"
486 _contains "${__OS__:=$(uname -a)}" "solaris" || _contains "${__OS__:=$(uname -a)}" "SunOS"
490 #this can only process ascii chars, should only be used when od command is missing as a backup way.
492 _debug2 "Using _ascii_hex"
496 while [ "$_h_i" -le "$_str_len" ]; do
497 _str_c="$(printf "%s" "$_str" | cut -c "$_h_i")"
498 printf " %02x" "'$_str_c"
499 _h_i="$
(_math
"$_h_i" + 1)"
503 #stdin output hexstr splited by one space
508 od -A n -v -t x1 | tr -s " " | sed 's/ $//' | tr -d "\r\t\n"
509 elif _exists hexdump; then
510 _debug3 "using
hexdump"
511 hexdump -v -e '/1 ""' -e '/1 " %02x
" ""'
512 elif _exists xxd; then
514 xxd -ps -c 20 -i | sed "s
/ 0x
/ /g
" | tr -d ",\n" | tr -s " "
516 _debug3 "using _ascii_hex
"
522 #url encode, no-preserved chars
523 #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
524 #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
526 #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
527 #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
529 #0 1 2 3 4 5 6 7 8 9 - _ . ~
530 #30 31 32 33 34 35 36 37 38 39 2d 5f 2e 7e
534 _hex_str=$(_hex_dump)
535 _debug3 "_url_encode
"
536 _debug3 "_hex_str
" "$_hex_str"
537 for _hex_code in $_hex_str; do
539 case "${_hex_code}" in
743 printf '%%%s' "$_hex_code"
753 if [ -z "$filename" ]; then
754 _usage "Usage
:_sed_i options filename
"
757 _debug2 options "$options"
758 if sed -h 2>&1 | grep "\
-i\
[SUFFIX
]" >/dev/null 2>&1; then
759 _debug "Using
sed -i"
760 sed -i "$options" "$filename"
762 _debug "No
-i support
in sed"
763 text="$
(cat "$filename")"
764 echo "$text" | sed "$options" >"$filename"
769 if ! egrep -o "$1" 2>/dev/null; then
770 sed -n 's/.*\('"$1"'\).*/\1/p'
774 #Usage: file startline endline
779 if [ -z "$endline" ]; then
780 _usage "Usage
: file startline endline
"
784 i="$
(grep -n -- "$startline" "$filename" | cut
-d : -f 1)"
786 _err "Can not
find start line
: $startline"
789 i="$
(_math
"$i" + 1)"
792 j="$
(grep -n -- "$endline" "$filename" | cut
-d : -f 1)"
794 _err "Can not
find end line
: $endline"
797 j="$
(_math
"$j" - 1)"
800 sed -n "$i,${j}p
" "$filename"
808 _debug3 "base64 multiline
:'$1'"
809 ${ACME_OPENSSL_BIN:-openssl} base64 -e
811 _debug3 "base64 single line.
"
812 ${ACME_OPENSSL_BIN:-openssl} base64 -e | tr -d '\r\n'
819 ${ACME_OPENSSL_BIN:-openssl} base64 -d -A
821 ${ACME_OPENSSL_BIN:-openssl} base64 -d
825 #Usage: hashalg [outputhex]
826 #Output Base64-encoded digest
829 if [ -z "$alg" ]; then
830 _usage "Usage
: _digest hashalg
"
836 if [ "$alg" = "sha256
" ] || [ "$alg" = "sha1
" ] || [ "$alg" = "md5
" ]; then
837 if [ "$outputhex" ]; then
838 ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' '
840 ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -binary | _base64
843 _err "$alg is not supported yet
"
849 #Usage: hashalg secret_hex [outputhex]
856 if [ -z "$secret_hex" ]; then
857 _usage "Usage
: _hmac hashalg secret
[outputhex
]"
861 if [ "$alg" = "sha256
" ] || [ "$alg" = "sha1
" ]; then
862 if [ "$outputhex" ]; then
863 (${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 ' '
865 ${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
868 _err "$alg is not supported yet
"
874 #Usage: keyfile hashalg
875 #Output: Base64-encoded signature value
879 if [ -z "$alg" ]; then
880 _usage "Usage
: _sign keyfile hashalg
"
884 _sign_openssl="${ACME_OPENSSL_BIN:-openssl} dgst
-sign $keyfile "
885 if [ "$alg" = "sha256
" ]; then
886 _sign_openssl="$_sign_openssl -$alg"
888 _err "$alg is not supported yet
"
892 if grep "BEGIN RSA PRIVATE KEY
" "$keyfile" >/dev/null 2>&1; then
893 $_sign_openssl | _base64
894 elif grep "BEGIN EC PRIVATE KEY
" "$keyfile" >/dev/null 2>&1; then
895 if ! _signedECText="$
($_sign_openssl |
${ACME_OPENSSL_BIN:-openssl} asn1parse
-inform DER
)"; then
896 _err "Sign failed
: $_sign_openssl"
897 _err "Key
file: $keyfile"
898 _err "Key content
:$
(wc -l <"$keyfile") lines
"
901 _debug3 "_signedECText
" "$_signedECText"
902 _ec_r="$
(echo "$_signedECText" | _head_n
2 | _tail_n
1 | cut
-d : -f 4 |
tr -d "\r\n")"
903 _debug3 "_ec_r
" "$_ec_r"
904 _ec_s="$
(echo "$_signedECText" | _head_n
3 | _tail_n
1 | cut
-d : -f 4 |
tr -d "\r\n")"
905 _debug3 "_ec_s
" "$_ec_s"
906 printf "%s
" "$_ec_r$_ec_s" | _h2b | _base64
908 _err "Unknown key
file format.
"
918 if [ -z "$_length" ]; then
922 [ "$_length" != "1024" ] \
923 && [ "$_length" != "2048" ] \
924 && [ "$_length" != "3072" ] \
925 && [ "$_length" != "4096" ] \
926 && [ "$_length" != "8192" ]
929 # _createkey 2048|ec-256 file
933 _debug2 "_createkey
for file:$f"
935 if _startswith "$length" "ec-
"; then
936 length=$(printf "%s
" "$length" | cut -d '-' -f 2-100)
938 if [ "$length" = "256" ]; then
941 if [ "$length" = "384" ]; then
944 if [ "$length" = "521" ]; then
950 if [ -z "$length" ]; then
954 _debug "Use length
$length"
956 if ! touch "$f" >/dev/null 2>&1; then
957 _f_path="$
(dirname "$f")"
958 _debug _f_path "$_f_path"
959 if ! mkdir -p "$_f_path"; then
960 _err "Can not create path
: $_f_path"
965 if _isEccKey "$length"; then
966 _debug "Using ec name
: $eccname"
967 ${ACME_OPENSSL_BIN:-openssl} ecparam -name "$eccname" -genkey 2>/dev/null >"$f"
969 _debug "Using RSA
: $length"
970 ${ACME_OPENSSL_BIN:-openssl} genrsa "$length" 2>/dev/null >"$f"
973 if [ "$?
" != "0" ]; then
974 _err "Create key error.
"
982 _debug2 _is_idn_d "$_is_idn_d"
983 _idn_temp=$(printf "%s
" "$_is_idn_d" | tr -d '0-9' | tr -d 'a-z' | tr -d 'A-Z' | tr -d '.,-')
984 _debug2 _idn_temp "$_idn_temp"
989 #aa.com,bb.com,cc.com
992 if ! _is_idn "$__idn_d"; then
993 printf "%s
" "$__idn_d"
998 if _contains "$__idn_d" ','; then
1000 for f in $(echo "$__idn_d" | tr ',' ' '); do
1001 [ -z "$f" ] && continue
1002 if [ -z "$_i_first" ]; then
1007 idn --quiet "$f" | tr -d "\r\n"
1010 idn "$__idn_d" | tr -d "\r\n"
1013 _err "Please
install idn to process IDN names.
"
1017 #_createcsr cn san_list keyfile csrfile conf
1025 _debug2 domain "$domain"
1026 _debug2 domainlist "$domainlist"
1027 _debug2 csrkey "$csrkey"
1029 _debug2 csrconf "$csrconf"
1031 printf "[ req_distinguished_name
]\n[ req
]\ndistinguished_name
= req_distinguished_name
\nreq_extensions
= v3_req
\n[ v3_req
]\n\nkeyUsage
= nonRepudiation
, digitalSignature
, keyEncipherment
" >"$csrconf"
1033 if [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then
1035 _info "Single domain
" "$domain"
1037 domainlist="$
(_idn
"$domainlist")"
1038 _debug2 domainlist "$domainlist"
1039 if _contains "$domainlist" ","; then
1040 alt="DNS
:$
(echo "$domainlist" |
sed "s/,/,DNS:/g")"
1042 alt="DNS
:$domainlist"
1045 _info "Multi domain
" "$alt"
1046 printf -- "\nsubjectAltName
=$alt" >>"$csrconf"
1048 if [ "$Le_OCSP_Staple" ] || [ "$Le_OCSP_Stable" ]; then
1049 _savedomainconf Le_OCSP_Staple "$Le_OCSP_Staple"
1050 _cleardomainconf Le_OCSP_Stable
1051 printf -- "\nbasicConstraints
= CA
:FALSE
\n1.3.6.1.5.5.7.1.24=DER
:30:03:02:01:05" >>"$csrconf"
1054 _csr_cn="$
(_idn
"$domain")"
1055 _debug2 _csr_cn "$_csr_cn"
1056 if _contains "$
(uname
-a)" "MINGW
"; then
1057 ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "//CN
=$_csr_cn" -config "$csrconf" -out "$csr"
1059 ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "/CN
=$_csr_cn" -config "$csrconf" -out "$csr"
1063 #_signcsr key csr conf cert
1071 _msg="$
(${ACME_OPENSSL_BIN:-openssl} x509
-req -days 365 -in "$csr" -signkey "$key" -extensions v3_req
-extfile "$conf" -out "$cert" 2>&1)"
1078 _readSubjectFromCSR() {
1080 if [ -z "$_csrfile" ]; then
1081 _usage "_readSubjectFromCSR mycsr.csr
"
1084 ${ACME_OPENSSL_BIN:-openssl} req -noout -in "$_csrfile" -subject | _egrep_o "CN
*=.
*" | cut -d = -f 2 | cut -d / -f 1 | tr -d '\n'
1088 #echo comma separated domain list
1089 _readSubjectAltNamesFromCSR() {
1091 if [ -z "$_csrfile" ]; then
1092 _usage "_readSubjectAltNamesFromCSR mycsr.csr
"
1096 _csrsubj="$
(_readSubjectFromCSR
"$_csrfile")"
1097 _debug _csrsubj "$_csrsubj"
1099 _dnsAltnames="$
(${ACME_OPENSSL_BIN:-openssl} req
-noout -text -in "$_csrfile" |
grep "^ *DNS:.*" |
tr -d ' \n')"
1100 _debug _dnsAltnames "$_dnsAltnames"
1102 if _contains "$_dnsAltnames," "DNS
:$_csrsubj,"; then
1103 _debug "AltNames contains subject
"
1104 _dnsAltnames="$
(printf "%s" "$_dnsAltnames," |
sed "s/DNS:$_csrsubj,//g")"
1106 _debug "AltNames doesn
't contain subject"
1109 printf "%s" "$_dnsAltnames" | sed "s/DNS://g"
1113 _readKeyLengthFromCSR() {
1115 if [ -z "$_csrfile" ]; then
1116 _usage "_readKeyLengthFromCSR mycsr.csr"
1120 _outcsr="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile")"
1121 _debug2 _outcsr "$_outcsr"
1122 if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then
1124 echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' '
1127 echo "$_outcsr" | tr "\t" " " | _egrep_o "(^ *|RSA )Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1
1134 if _exists "ss"; then
1136 ss -ntpl | grep ":$_port "
1140 if _exists "netstat"; then
1141 _debug "Using: netstat"
1142 if netstat -h 2>&1 | grep "\-p proto" >/dev/null; then
1143 #for windows version netstat tool
1144 netstat -an -p tcp | grep "LISTENING" | grep ":$_port "
1146 if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then
1147 netstat -an -p tcp | grep LISTEN | grep ":$_port "
1148 elif netstat -help 2>&1 | grep -- '-P protocol
' >/dev/null; then
1150 netstat -an -P tcp | grep "\.$_port " | grep "LISTEN"
1151 elif netstat -help 2>&1 | grep "\-p" >/dev/null; then
1153 netstat -ntpl | grep ":$_port "
1155 #for busybox (embedded linux; no pid support)
1156 netstat -ntl 2>/dev/null | grep ":$_port "
1165 #domain [password] [isEcc]
1169 if [ -z "$domain" ]; then
1170 _usage "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]"
1176 _initpath "$domain" "$_isEcc"
1178 if [ "$pfxPassword" ]; then
1179 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH" -password "pass:$pfxPassword"
1181 ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH"
1184 if [ "$?" = "0" ]; then
1185 _info "Success, Pfx is exported to: $CERT_PFX_PATH"
1194 if [ -z "$domain" ]; then
1195 _usage "Usage: $PROJECT_ENTRY --toPkcs8 -d domain [--ecc]"
1201 _initpath "$domain" "$_isEcc"
1203 ${ACME_OPENSSL_BIN:-openssl} pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in "$CERT_KEY_PATH" -out "$CERT_PKCS8_PATH"
1205 if [ "$?" = "0" ]; then
1206 _info "Success, $CERT_PKCS8_PATH"
1212 createAccountKey() {
1213 _info "Creating account key"
1214 if [ -z "$1" ]; then
1215 _usage "Usage: $PROJECT_ENTRY --createAccountKey --accountkeylength 2048"
1220 _create_account_key "$length"
1224 _create_account_key() {
1228 if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then
1229 _debug "Use default length $DEFAULT_ACCOUNT_KEY_LENGTH"
1230 length="$DEFAULT_ACCOUNT_KEY_LENGTH"
1233 _debug length "$length"
1237 if [ -f "$ACCOUNT_KEY_PATH" ]; then
1238 _info "Account key exists, skip"
1241 #generate account key
1242 _createkey "$length" "$ACCOUNT_KEY_PATH"
1249 _info "Creating domain key"
1250 if [ -z "$1" ]; then
1251 _usage "Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]"
1258 if [ -z "$_cdl" ]; then
1259 _debug "Use DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH"
1260 _cdl="$DEFAULT_DOMAIN_KEY_LENGTH"
1263 _initpath "$domain" "$_cdl"
1265 if [ ! -f "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$IS_RENEW" ]); then
1266 if _createkey "$_cdl" "$CERT_KEY_PATH"; then
1267 _savedomainconf Le_Keylength "$_cdl"
1268 _info "The domain key is here: $(__green $CERT_KEY_PATH)"
1271 if [ "$IS_RENEW" ]; then
1272 _info "Domain key exists, skip"
1275 _err "Domain key exists, do you want to overwrite the key?"
1276 _err "Add '--force', and try again."
1283 # domain domainlist isEcc
1285 _info "Creating csr"
1286 if [ -z "$1" ]; then
1287 _usage "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]"
1295 _initpath "$domain" "$_isEcc"
1297 if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then
1298 _info "CSR exists, skip"
1302 if [ ! -f "$CERT_KEY_PATH" ]; then
1303 _err "The key file is not found: $CERT_KEY_PATH"
1304 _err "Please create the key file first."
1307 _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"
1312 tr '/+' '_-
' | tr -d '= '
1317 if date -u -d@"$1" 2>/dev/null; then
1322 if date -u -r "$1" 2>/dev/null; then
1327 if _exists adb; then
1328 _t_s_a=$(echo "0t${1}=Y" | adb)
1335 sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n"
1340 if stat -c '%U
:%G
' "$1" 2>/dev/null; then
1345 if stat -f '%Su
:%Sg
' "$1" 2>/dev/null; then
1349 return 1 #error, 'stat
' not found
1355 if [ -z "$keyfile" ]; then
1356 _usage "Usage: _calcjwk keyfile"
1360 if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then
1361 _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE"
1365 if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
1367 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)
1368 if [ "${#pub_exp}" = "5" ]; then
1371 _debug3 pub_exp "$pub_exp"
1373 e=$(echo "$pub_exp" | _h2b | _base64)
1376 modulus=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2)
1377 _debug3 modulus "$modulus"
1378 n="$(printf "%s" "$modulus" | _h2b | _base64 | _url_replace)"
1381 jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
1384 JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}'
1385 JWK_HEADERPLACE_PART1='{"nonce": "'
1386 JWK_HEADERPLACE_PART2='", "alg": "RS256", "jwk": '$jwk'}'
1387 elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
1389 crv="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
1392 if [ -z "$crv" ]; then
1393 _debug "Let's try ASN1 OID
"
1394 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")"
1395 _debug3 crv_oid "$crv_oid"
1396 case "${crv_oid}" in
1407 _err "ECC oid
: $crv_oid"
1414 pubi="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep -n pub
: | cut
-d : -f 1)"
1415 pubi=$(_math "$pubi" + 1)
1416 _debug3 pubi "$pubi"
1418 pubj="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
grep -n "ASN1 OID:" | cut
-d : -f 1)"
1419 pubj=$(_math "$pubj" - 1)
1420 _debug3 pubj "$pubj"
1422 pubtext="$
(${ACME_OPENSSL_BIN:-openssl} ec
-in "$keyfile" -noout -text 2>/dev
/null |
sed -n "$pubi,${pubj}p" |
tr -d " \n\r")"
1423 _debug3 pubtext "$pubtext"
1425 xlen="$
(printf "%s" "$pubtext" |
tr -d ':' |
wc -c)"
1426 xlen=$(_math "$xlen" / 4)
1427 _debug3 xlen "$xlen"
1429 xend=$(_math "$xlen" + 1)
1430 x="$
(printf "%s" "$pubtext" | cut
-d : -f 2-"$xend")"
1433 x64="$
(printf "%s" "$x" |
tr -d : | _h2b | _base64 | _url_replace
)"
1436 xend=$(_math "$xend" + 1)
1437 y="$
(printf "%s" "$pubtext" | cut
-d : -f "$xend"-10000)"
1440 y64="$
(printf "%s" "$y" |
tr -d : | _h2b | _base64 | _url_replace
)"
1443 jwk='{"crv
": "'$crv'", "kty
": "EC
", "x
": "'$x64'", "y
": "'$y64'"}'
1446 JWK_HEADER='{"alg
": "ES256
", "jwk
": '$jwk'}'
1447 JWK_HEADERPLACE_PART1='{"nonce
": "'
1448 JWK_HEADERPLACE_PART2='", "alg
": "ES256
", "jwk
": '$jwk'}'
1450 _err "Only RSA or EC key is supported.
"
1454 _debug3 JWK_HEADER "$JWK_HEADER"
1455 __CACHED_JWK_KEY_FILE="$keyfile"
1463 date -u "+%Y-
%m-
%d
%H
:%M
:%S
"
1467 if _exists mktemp; then
1468 if mktemp 2>/dev/null; then
1470 elif _contains "$
(mktemp
2>&1)" "-t prefix
" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then
1475 if [ -d "/tmp
" ]; then
1476 echo "/tmp
/${PROJECT_NAME}wefADf24sf.$
(_time
).tmp
"
1478 elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then
1479 echo "/$LE_TEMP_DIR/wefADf24sf.$
(_time
).tmp
"
1482 _err "Can not create temp
file.
"
1487 if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then
1488 HTTP_HEADER="$
(_mktemp
)"
1489 _debug2 HTTP_HEADER "$HTTP_HEADER"
1492 if [ "$__HTTP_INITIALIZED" ]; then
1493 if [ "$_ACME_CURL$_ACME_WGET" ]; then
1494 _debug2 "Http already initialized.
"
1499 if [ -z "$_ACME_CURL" ] && _exists "curl
"; then
1500 _ACME_CURL="curl
-L --silent --dump-header $HTTP_HEADER "
1501 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1502 _CURL_DUMP="$
(_mktemp
)"
1503 _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP "
1506 if [ "$CA_PATH" ]; then
1507 _ACME_CURL="$_ACME_CURL --capath $CA_PATH "
1508 elif [ "$CA_BUNDLE" ]; then
1509 _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE "
1514 if [ -z "$_ACME_WGET" ] && _exists "wget
"; then
1515 _ACME_WGET="wget
-q"
1516 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1517 _ACME_WGET="$_ACME_WGET -d "
1519 if [ "$CA_PATH" ]; then
1520 _ACME_WGET="$_ACME_WGET --ca-directory=$CA_PATH "
1521 elif [ "$CA_BUNDLE" ]; then
1522 _ACME_WGET="$_ACME_WGET --ca-certificate=$CA_BUNDLE "
1526 #from wget 1.14: do not skip body on 404 error
1527 if [ "$_ACME_WGET" ] && _contains "$
($_ACME_WGET --help 2>&1)" "--content-on-error"; then
1528 _ACME_WGET="$_ACME_WGET --content-on-error "
1531 __HTTP_INITIALIZED=1
1535 # body url [needbase64] [POST|PUT]
1542 if [ -z "$httpmethod" ]; then
1547 _debug2 "body
" "$body"
1551 if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
1553 if [ "$HTTPS_INSECURE" ]; then
1554 _CURL="$_CURL --insecure "
1556 _debug "_CURL
" "$_CURL"
1557 if [ "$needbase64" ]; then
1558 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url" | _base64
)"
1560 response="$
($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url")"
1563 if [ "$_ret" != "0" ]; then
1564 _err "Please refer to https
://curl.haxx.se
/libcurl
/c
/libcurl-errors.html
for error code
: $_ret"
1565 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1566 _err "Here is the curl dump log
:"
1567 _err "$
(cat "$_CURL_DUMP")"
1570 elif [ "$_ACME_WGET" ]; then
1572 if [ "$HTTPS_INSECURE" ]; then
1573 _WGET="$_WGET --no-check-certificate "
1575 _debug "_WGET
" "$_WGET"
1576 if [ "$needbase64" ]; then
1577 if [ "$httpmethod" = "POST
" ]; then
1578 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER" | _base64
)"
1580 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER" | _base64
)"
1583 if [ "$httpmethod" = "POST
" ]; then
1584 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER")"
1586 response="$
($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER")"
1590 if [ "$_ret" = "8" ]; then
1592 _debug "wget returns
8, the server returns a
'Bad request' response
, lets process the response later.
"
1594 if [ "$_ret" != "0" ]; then
1595 _err "Please refer to https
://www.gnu.org
/software
/wget
/manual
/html_node
/Exit-Status.html
for error code
: $_ret"
1597 _sed_i "s
/^
*//g
" "$HTTP_HEADER"
1600 _err "Neither curl nor wget is found
, can not
do $httpmethod.
"
1602 _debug "_ret
" "$_ret"
1603 printf "%s
" "$response"
1607 # url getheader timeout
1614 _debug "timeout
" "$t"
1618 if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
1620 if [ "$HTTPS_INSECURE" ]; then
1621 _CURL="$_CURL --insecure "
1624 _CURL="$_CURL --connect-timeout $t"
1626 _debug "_CURL
" "$_CURL"
1627 if [ "$onlyheader" ]; then
1628 $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
1630 $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
1633 if [ "$ret" != "0" ]; then
1634 _err "Please refer to https
://curl.haxx.se
/libcurl
/c
/libcurl-errors.html
for error code
: $ret"
1635 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1636 _err "Here is the curl dump log
:"
1637 _err "$
(cat "$_CURL_DUMP")"
1640 elif [ "$_ACME_WGET" ]; then
1642 if [ "$HTTPS_INSECURE" ]; then
1643 _WGET="$_WGET --no-check-certificate "
1646 _WGET="$_WGET --timeout=$t"
1648 _debug "_WGET
" "$_WGET"
1649 if [ "$onlyheader" ]; then
1650 $_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'
1652 $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url"
1655 if [ "$ret" = "8" ]; then
1657 _debug "wget returns
8, the server returns a
'Bad request' response
, lets process the response later.
"
1659 if [ "$ret" != "0" ]; then
1660 _err "Please refer to https
://www.gnu.org
/software
/wget
/manual
/html_node
/Exit-Status.html
for error code
: $ret"
1664 _err "Neither curl nor wget is found
, can not
do GET.
"
1675 if ! tail -n "$1" 2>/dev/null; then
1681 # url payload needbase64 keyfile
1682 _send_signed_request() {
1687 if [ -z "$keyfile" ]; then
1688 keyfile="$ACCOUNT_KEY_PATH"
1691 _debug payload "$payload"
1693 if ! _calcjwk "$keyfile"; then
1697 payload64=$(printf "%s
" "$payload" | _base64 | _url_replace)
1698 _debug3 payload64 "$payload64"
1700 MAX_REQUEST_RETRY_TIMES=5
1701 _request_retry_times=0
1702 while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
1703 _debug3 _request_retry_times "$_request_retry_times"
1704 if [ -z "$_CACHED_NONCE" ]; then
1705 _debug2 "Get nonce.
"
1706 nonceurl="$API/directory
"
1707 _headers="$
(_get
"$nonceurl" "onlyheader")"
1709 if [ "$?
" != "0" ]; then
1710 _err "Can not connect to
$nonceurl to get nonce.
"
1714 _debug2 _headers "$_headers"
1716 _CACHED_NONCE="$
(echo "$_headers" |
grep "Replay-Nonce:" | _head_n
1 |
tr -d "\r\n " | cut
-d ':' -f 2)"
1717 _debug2 _CACHED_NONCE "$_CACHED_NONCE"
1719 _debug2 "Use _CACHED_NONCE
" "$_CACHED_NONCE"
1721 nonce="$_CACHED_NONCE"
1722 _debug2 nonce "$nonce"
1724 protected="$JWK_HEADERPLACE_PART1$nonce$JWK_HEADERPLACE_PART2"
1725 _debug3 protected "$protected"
1727 protected64="$
(printf "%s" "$protected" | _base64 | _url_replace
)"
1728 _debug3 protected64 "$protected64"
1730 if ! _sig_t="$
(printf "%s" "$protected64.$payload64" | _sign
"$keyfile" "sha256")"; then
1731 _err "Sign request failed.
"
1734 _debug3 _sig_t "$_sig_t"
1736 sig="$
(printf "%s" "$_sig_t" | _url_replace
)"
1739 body="{\"header
\": $JWK_HEADER, \"protected
\": \"$protected64\", \"payload
\": \"$payload64\", \"signature
\": \"$sig\"}"
1740 _debug3 body "$body"
1742 response="$
(_post
"$body" "$url" "$needbase64")"
1745 if [ "$?
" != "0" ]; then
1746 _err "Can not post to
$url"
1749 _debug2 original "$response"
1750 response="$
(echo "$response" | _normalizeJson
)"
1752 responseHeaders="$
(cat "$HTTP_HEADER")"
1754 _debug2 responseHeaders "$responseHeaders"
1755 _debug2 response "$response"
1756 code="$
(grep "^HTTP" "$HTTP_HEADER" | _tail_n
1 | cut
-d " " -f 2 |
tr -d "\r\n")"
1759 _CACHED_NONCE="$
(echo "$responseHeaders" |
grep "Replay-Nonce:" | _head_n
1 |
tr -d "\r\n " | cut
-d ':' -f 2)"
1761 if _contains "$response" "JWS has invalid anti-replay nonce
"; then
1762 _info "It seems the CA server is busy now
, let's wait and retry."
1763 _request_retry_times=$(_math "$_request_retry_times" + 1)
1772 #setopt "file" "opt" "=" "value" [";"]
1779 if [ -z "$__opt" ]; then
1780 _usage usage: _setopt '"file" "opt" "=" "value" [";"]'
1783 if [ ! -f "$__conf" ]; then
1787 if grep -n "^$__opt$__sep" "$__conf" >/dev/null; then
1789 if _contains "$__val" "&"; then
1790 __val="$(echo "$__val" | sed 's
/&/\\&/g
')"
1792 text="$(cat "$__conf")"
1793 printf -- "%s\n" "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
1795 elif grep -n "^#$__opt$__sep" "$__conf" >/dev/null; then
1796 if _contains "$__val" "&"; then
1797 __val="$(echo "$__val" | sed 's
/&/\\&/g
')"
1799 text="$(cat "$__conf")"
1800 printf -- "%s\n" "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
1804 echo "$__opt$__sep$__val$__end" >>"$__conf"
1806 _debug3 "$(grep -n "^$__opt$__sep" "$__conf")"
1809 #_save_conf file key value
1815 if [ "$_s_c_f" ]; then
1816 _setopt "$_s_c_f" "$_sdkey" "=" "'$_sdvalue'"
1818 _err "config file is empty, can not save $_sdkey=$_sdvalue"
1822 #_clear_conf file key
1826 if [ "$_c_c_f" ]; then
1827 _conf_data="$(cat "$_c_c_f")"
1828 echo "$_conf_data" | sed "s/^$_sdkey *=.*$//" >"$_c_c_f"
1830 _err "config file is empty, can not clear"
1834 #_read_conf file key
1838 if [ -f "$_r_c_f" ]; then
1840 eval "$(grep "^$_sdkey *=" "$_r_c_f")"
1841 eval "printf \"%s\" \"\$$_sdkey\""
1844 _debug "config file is empty, can not read $_sdkey"
1848 #_savedomainconf key value
1849 #save to domain.conf
1851 _save_conf "$DOMAIN_CONF" "$1" "$2"
1854 #_cleardomainconf key
1855 _cleardomainconf() {
1856 _clear_conf "$DOMAIN_CONF" "$1"
1859 #_readdomainconf key
1861 _read_conf "$DOMAIN_CONF" "$1"
1864 #_saveaccountconf key value
1865 _saveaccountconf() {
1866 _save_conf "$ACCOUNT_CONF_PATH" "$1" "$2"
1870 _saveaccountconf_mutable() {
1871 _save_conf "$ACCOUNT_CONF_PATH" "SAVED_$1" "$2"
1873 _clearaccountconf "$1"
1877 _readaccountconf() {
1878 _read_conf "$ACCOUNT_CONF_PATH" "$1"
1882 _readaccountconf_mutable() {
1884 _readaccountconf "SAVED_$_rac_key"
1887 #_clearaccountconf key
1888 _clearaccountconf() {
1889 _clear_conf "$ACCOUNT_CONF_PATH" "$1"
1892 #_savecaconf key value
1894 _save_conf "$CA_CONF" "$1" "$2"
1899 _read_conf "$CA_CONF" "$1"
1902 #_clearaccountconf key
1904 _clear_conf "$CA_CONF" "$1"
1907 # content localaddress
1911 _debug "ncaddr" "$ncaddr"
1913 _debug "startserver: $$"
1914 nchelp="$(nc -h 2>&1)"
1916 _debug Le_HTTPPort "$Le_HTTPPort"
1917 _debug Le_Listen_V4 "$Le_Listen_V4"
1918 _debug Le_Listen_V6 "$Le_Listen_V6"
1921 if [ "$Le_Listen_V4" ]; then
1923 elif [ "$Le_Listen_V6" ]; then
1927 if [ "$Le_Listen_V4$Le_Listen_V6$ncaddr" ]; then
1928 if ! _contains "$nchelp" "-4"; then
1929 _err "The nc doesn't support
'-4', '-6' or local-address
, please
install 'netcat-openbsd' and try again.
"
1930 _err "See $
(__green
$_PREPARE_LINK)"
1935 if echo "$nchelp" | grep "\
-q[ ,]" >/dev/null; then
1936 _NC="$_NC -q 1 -l $ncaddr"
1938 if echo "$nchelp" | grep "GNU netcat
" >/dev/null && echo "$nchelp" | grep "\
-c, \
-\
-close" >/dev/null; then
1939 _NC="$_NC -c -l $ncaddr"
1940 elif echo "$nchelp" | grep "\
-N" | grep "Shutdown the network socket after EOF on stdin
" >/dev/null; then
1941 _NC="$_NC -N -l $ncaddr"
1943 _NC="$_NC -l $ncaddr"
1950 if _contains "$nchelp" "nmap.org
"; then
1951 _debug "Using ncat
: nmap.org
"
1952 if ! _exec "printf \"%s
\r\n\r\n%s
\" \"HTTP
/1.1 200 OK
\" \"$content\" |
$_NC \"$Le_HTTPPort\" >&2"; then
1956 if [ "$DEBUG" ]; then
1963 if ! _exec "printf \"%s
\r\n\r\n%s
\" \"HTTP
/1.1 200 OK
\" \"$content\" |
$_NC -p \"$Le_HTTPPort\" >&2"; then
1964 _exec "printf \"%s
\r\n\r\n%s
\" \"HTTP
/1.1 200 OK
\" \"$content\" |
$_NC \"$Le_HTTPPort\" >&2"
1967 if [ "$?
" != "0" ]; then
1968 _err "nc listen error.
"
1972 if [ "$DEBUG" ]; then
1981 if [ -z "$pid" ]; then
1985 _debug2 "Le_HTTPPort
" "$Le_HTTPPort"
1986 if [ "$Le_HTTPPort" ]; then
1987 if [ "$DEBUG" ] && [ "$DEBUG" -gt "3" ]; then
1988 _get "http
://localhost
:$Le_HTTPPort" "" 1
1990 _get "http
://localhost
:$Le_HTTPPort" "" 1 >/dev/null 2>&1
1994 _debug2 "Le_TLSPort
" "$Le_TLSPort"
1995 if [ "$Le_TLSPort" ]; then
1996 if [ "$DEBUG" ] && [ "$DEBUG" -gt "3" ]; then
1997 _get "https
://localhost
:$Le_TLSPort" "" 1
1998 _get "https
://localhost
:$Le_TLSPort" "" 1
2000 _get "https
://localhost
:$Le_TLSPort" "" 1 >/dev/null 2>&1
2001 _get "https
://localhost
:$Le_TLSPort" "" 1 >/dev/null 2>&1
2009 if [ "$__INTERACTIVE" ]; then
2010 _sleep_c="$_sleep_sec"
2011 while [ "$_sleep_c" -ge "0" ]; do
2014 _sleep_c="$
(_math
"$_sleep_c" - 1)"
2023 # _starttlsserver san_a san_b port content _ncaddr
2025 _info "Starting tls server.
"
2032 _debug san_a "$san_a"
2033 _debug san_b "$san_b"
2037 if ! _createkey "2048" "$TLS_KEY"; then
2038 _err "Create tls validation key error.
"
2044 if [ "$san_b" ]; then
2047 if ! _createcsr "tls.acme.sh
" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF"; then
2048 _err "Create tls validation csr error.
"
2053 if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then
2054 _err "Create tls validation cert error.
"
2058 __S_OPENSSL="${ACME_OPENSSL_BIN:-openssl} s_server
-cert $TLS_CERT -key $TLS_KEY "
2059 if [ "$opaddr" ]; then
2060 __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port"
2062 __S_OPENSSL="$__S_OPENSSL -accept $port"
2065 _debug Le_Listen_V4 "$Le_Listen_V4"
2066 _debug Le_Listen_V6 "$Le_Listen_V6"
2067 if [ "$Le_Listen_V4" ]; then
2068 __S_OPENSSL="$__S_OPENSSL -4"
2069 elif [ "$Le_Listen_V6" ]; then
2070 __S_OPENSSL="$__S_OPENSSL -6"
2073 _debug "$__S_OPENSSL"
2074 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
2075 (printf "%s
\r\n\r\n%s
" "HTTP
/1.1 200 OK
" "$content" | $__S_OPENSSL -tlsextdebug) &
2077 (printf "%s
\r\n\r\n%s
" "HTTP
/1.1 200 OK
" "$content" | $__S_OPENSSL >/dev/null 2>&1) &
2082 _debug serverproc "$serverproc"
2088 if ! readlink -f "$_rf" 2>/dev/null; then
2089 if _startswith "$_rf" "/"; then
2093 echo "$
(pwd)/$_rf" | _conapath
2102 if [ -z "$_SCRIPT_HOME" ]; then
2103 if _exists readlink
&& _exists
dirname; then
2104 _debug
"Lets find script dir."
2105 _debug
"_SCRIPT_" "$_SCRIPT_"
2106 _script
="$(_readlink "$_SCRIPT_")"
2107 _debug
"_script" "$_script"
2108 _script_home
="$(dirname "$_script")"
2109 _debug
"_script_home" "$_script_home"
2110 if [ -d "$_script_home" ]; then
2111 _SCRIPT_HOME
="$_script_home"
2113 _err
"It seems the script home is not correct:$_script_home"
2118 # if [ -z "$LE_WORKING_DIR" ]; then
2119 # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then
2120 # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME"
2121 # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
2123 # LE_WORKING_DIR="$_SCRIPT_HOME"
2127 if [ -z "$LE_WORKING_DIR" ]; then
2128 _debug
"Using default home:$DEFAULT_INSTALL_HOME"
2129 LE_WORKING_DIR
="$DEFAULT_INSTALL_HOME"
2131 export LE_WORKING_DIR
2133 if [ -z "$LE_CONFIG_HOME" ]; then
2134 LE_CONFIG_HOME
="$LE_WORKING_DIR"
2136 _debug
"Using config home:$LE_CONFIG_HOME"
2137 export LE_CONFIG_HOME
2139 _DEFAULT_ACCOUNT_CONF_PATH
="$LE_CONFIG_HOME/account.conf"
2141 if [ -z "$ACCOUNT_CONF_PATH" ]; then
2142 if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
2143 .
"$_DEFAULT_ACCOUNT_CONF_PATH"
2147 if [ -z "$ACCOUNT_CONF_PATH" ]; then
2148 ACCOUNT_CONF_PATH
="$_DEFAULT_ACCOUNT_CONF_PATH"
2151 DEFAULT_LOG_FILE
="$LE_CONFIG_HOME/$PROJECT_NAME.log"
2153 DEFAULT_CA_HOME
="$LE_CONFIG_HOME/ca"
2155 if [ -z "$LE_TEMP_DIR" ]; then
2156 LE_TEMP_DIR
="$LE_CONFIG_HOME/tmp"
2160 #[domain] [keylength]
2165 if [ -f "$ACCOUNT_CONF_PATH" ]; then
2166 .
"$ACCOUNT_CONF_PATH"
2169 if [ "$IN_CRON" ]; then
2170 if [ ! "$_USER_PATH_EXPORTED" ]; then
2171 _USER_PATH_EXPORTED
=1
2172 export PATH
="$USER_PATH:$PATH"
2176 if [ -z "$CA_HOME" ]; then
2177 CA_HOME
="$DEFAULT_CA_HOME"
2180 if [ -z "$API" ]; then
2181 if [ -z "$STAGE" ]; then
2185 _info
"Using stage api:$API"
2189 _API_HOST
="$(echo "$API" | cut -d : -f 2 | tr -d '/')"
2190 CA_DIR
="$CA_HOME/$_API_HOST"
2192 _DEFAULT_CA_CONF
="$CA_DIR/ca.conf"
2194 if [ -z "$CA_CONF" ]; then
2195 CA_CONF
="$_DEFAULT_CA_CONF"
2197 _debug3 CA_CONF
"$CA_CONF"
2199 if [ -f "$CA_CONF" ]; then
2203 if [ -z "$ACME_DIR" ]; then
2204 ACME_DIR
="/home/.acme"
2207 if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
2208 APACHE_CONF_BACKUP_DIR
="$LE_CONFIG_HOME"
2211 if [ -z "$USER_AGENT" ]; then
2212 USER_AGENT
="$DEFAULT_USER_AGENT"
2215 if [ -z "$HTTP_HEADER" ]; then
2216 HTTP_HEADER
="$LE_CONFIG_HOME/http.header"
2219 _OLD_ACCOUNT_KEY
="$LE_WORKING_DIR/account.key"
2220 _OLD_ACCOUNT_JSON
="$LE_WORKING_DIR/account.json"
2222 _DEFAULT_ACCOUNT_KEY_PATH
="$CA_DIR/account.key"
2223 _DEFAULT_ACCOUNT_JSON_PATH
="$CA_DIR/account.json"
2224 if [ -z "$ACCOUNT_KEY_PATH" ]; then
2225 ACCOUNT_KEY_PATH
="$_DEFAULT_ACCOUNT_KEY_PATH"
2228 if [ -z "$ACCOUNT_JSON_PATH" ]; then
2229 ACCOUNT_JSON_PATH
="$_DEFAULT_ACCOUNT_JSON_PATH"
2232 _DEFAULT_CERT_HOME
="$LE_CONFIG_HOME"
2233 if [ -z "$CERT_HOME" ]; then
2234 CERT_HOME
="$_DEFAULT_CERT_HOME"
2237 if [ -z "$ACME_OPENSSL_BIN" ] ||
[ ! -f "$ACME_OPENSSL_BIN" ] ||
[ ! -x "$ACME_OPENSSL_BIN" ]; then
2238 ACME_OPENSSL_BIN
="$DEFAULT_OPENSSL_BIN"
2241 if [ -z "$1" ]; then
2248 if [ -z "$DOMAIN_PATH" ]; then
2249 domainhome
="$CERT_HOME/$domain"
2250 domainhomeecc
="$CERT_HOME/$domain$ECC_SUFFIX"
2252 DOMAIN_PATH
="$domainhome"
2254 if _isEccKey
"$_ilength"; then
2255 DOMAIN_PATH
="$domainhomeecc"
2257 if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
2258 _info
"The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert."
2261 _debug DOMAIN_PATH
"$DOMAIN_PATH"
2264 if [ -z "$DOMAIN_BACKUP_PATH" ]; then
2265 DOMAIN_BACKUP_PATH
="$DOMAIN_PATH/backup"
2268 if [ -z "$DOMAIN_CONF" ]; then
2269 DOMAIN_CONF
="$DOMAIN_PATH/$domain.conf"
2272 if [ -z "$DOMAIN_SSL_CONF" ]; then
2273 DOMAIN_SSL_CONF
="$DOMAIN_PATH/$domain.csr.conf"
2276 if [ -z "$CSR_PATH" ]; then
2277 CSR_PATH
="$DOMAIN_PATH/$domain.csr"
2279 if [ -z "$CERT_KEY_PATH" ]; then
2280 CERT_KEY_PATH
="$DOMAIN_PATH/$domain.key"
2282 if [ -z "$CERT_PATH" ]; then
2283 CERT_PATH
="$DOMAIN_PATH/$domain.cer"
2285 if [ -z "$CA_CERT_PATH" ]; then
2286 CA_CERT_PATH
="$DOMAIN_PATH/ca.cer"
2288 if [ -z "$CERT_FULLCHAIN_PATH" ]; then
2289 CERT_FULLCHAIN_PATH
="$DOMAIN_PATH/fullchain.cer"
2291 if [ -z "$CERT_PFX_PATH" ]; then
2292 CERT_PFX_PATH
="$DOMAIN_PATH/$domain.pfx"
2294 if [ -z "$CERT_PKCS8_PATH" ]; then
2295 CERT_PKCS8_PATH
="$DOMAIN_PATH/$domain.pkcs8"
2298 if [ -z "$TLS_CONF" ]; then
2299 TLS_CONF
="$DOMAIN_PATH/tls.validation.conf"
2301 if [ -z "$TLS_CERT" ]; then
2302 TLS_CERT
="$DOMAIN_PATH/tls.validation.cert"
2304 if [ -z "$TLS_KEY" ]; then
2305 TLS_KEY
="$DOMAIN_PATH/tls.validation.key"
2307 if [ -z "$TLS_CSR" ]; then
2308 TLS_CSR
="$DOMAIN_PATH/tls.validation.csr"
2314 if [ -z "$_EXEC_TEMP_ERR" ]; then
2315 _EXEC_TEMP_ERR
="$(_mktemp)"
2318 if [ "$_EXEC_TEMP_ERR" ]; then
2319 eval "$@ 2>>$_EXEC_TEMP_ERR"
2326 [ "$_EXEC_TEMP_ERR" ] && _err
"$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR"
2330 _APACHECTL
="apachectl"
2331 if ! _exists apachectl
; then
2332 if _exists apache2ctl
; then
2333 _APACHECTL
="apache2ctl"
2335 _err
"'apachectl not found. It seems that apache is not installed, or you are not root user.'"
2336 _err
"Please use webroot mode to try again."
2341 if ! _exec
$_APACHECTL -V >/dev
/null
; then
2346 if [ "$APACHE_HTTPD_CONF" ]; then
2347 _saveaccountconf APACHE_HTTPD_CONF
"$APACHE_HTTPD_CONF"
2348 httpdconf
="$APACHE_HTTPD_CONF"
2349 httpdconfname
="$(basename "$httpdconfname")"
2351 httpdconfname
="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
2352 _debug httpdconfname "$httpdconfname"
2354 if [ -z "$httpdconfname" ]; then
2355 _err "Can not read apache config file."
2359 if _startswith "$httpdconfname" '/'; then
2360 httpdconf="$httpdconfname"
2361 httpdconfname="$(basename "$httpdconfname")"
2363 httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
2364 _debug httpdroot
"$httpdroot"
2365 httpdconf
="$httpdroot/$httpdconfname"
2366 httpdconfname
="$(basename "$httpdconfname")"
2369 _debug httpdconf
"$httpdconf"
2370 _debug httpdconfname
"$httpdconfname"
2371 if [ ! -f "$httpdconf" ]; then
2372 _err
"Apache Config file not found" "$httpdconf"
2379 if [ -z "$usingApache" ]; then
2383 if ! _apachePath
; then
2387 if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
2388 _debug
"No config file to restore."
2392 cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
2393 _debug
"Restored: $httpdconf."
2394 if ! _exec
$_APACHECTL -t; then
2396 _err
"Sorry, restore apache config error, please contact me."
2399 _debug
"Restored successfully."
2400 rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
2406 if ! _apachePath
; then
2410 #test the conf first
2411 _info
"Checking if there is an error in the apache config file before starting."
2413 if ! _exec
"$_APACHECTL" -t >/dev
/null
; then
2415 _err
"The apache config file has error, please fix it first, then try again."
2416 _err
"Don't worry, there is nothing changed to your system."
2423 _debug
"Backup apache config file" "$httpdconf"
2424 if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
2425 _err
"Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
2426 _err
"This might be a bug of $PROJECT_NAME , please report issue: $PROJECT"
2429 _info
"JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
2430 _info
"In case there is an error that can not be restored automatically, you may try restore it yourself."
2431 _info
"The backup file will be deleted on success, just forget it."
2435 apacheVer
="$($_APACHECTL -V | grep "Server version
:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
2436 _debug
"apacheVer" "$apacheVer"
2437 apacheMajer
="$(echo "$apacheVer" | cut -d . -f 1)"
2438 apacheMinor
="$(echo "$apacheVer" | cut -d . -f 2)"
2440 if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ]; then
2442 Alias /.well-known/acme-challenge $ACME_DIR
2444 <Directory $ACME_DIR >
2450 Alias /.well-known/acme-challenge $ACME_DIR
2452 <Directory $ACME_DIR >
2459 _msg
="$($_APACHECTL -t 2>&1)"
2460 if [ "$?" != "0" ]; then
2461 _err
"Sorry, apache config error"
2462 if _restoreApache
; then
2463 _err
"The apache config file is restored."
2465 _err
"Sorry, The apache config file can not be restored, please report bug."
2470 if [ ! -d "$ACME_DIR" ]; then
2471 mkdir
-p "$ACME_DIR"
2472 chmod 755 "$ACME_DIR"
2475 if ! _exec
"$_APACHECTL" graceful
; then
2477 _err
"$_APACHECTL graceful error, please contact me."
2485 #find the real nginx conf file
2488 #returns the real nginx conf file
2493 if ! _exists
"nginx"; then
2494 _err
"nginx command is not found."
2497 FOUND_REAL_NGINX_CONF
=""
2498 FOUND_REAL_NGINX_CONF_LN
=""
2499 BACKUP_NGINX_CONF
=""
2500 _debug _croot
"$_croot"
2501 _start_f
="$(echo "$_croot" | cut -d : -f 2)"
2502 _debug _start_f
"$_start_f"
2503 if [ -z "$_start_f" ]; then
2504 _debug
"find start conf from nginx command"
2505 if [ -z "$NGINX_CONF" ]; then
2506 NGINX_CONF
="$(nginx -V 2>&1 | _egrep_o "--conf-path=[^
]* " | tr -d " ")"
2507 _debug NGINX_CONF
"$NGINX_CONF"
2508 NGINX_CONF
="$(echo "$NGINX_CONF" | cut -d = -f 2)"
2509 _debug NGINX_CONF
"$NGINX_CONF"
2510 if [ ! -f "$NGINX_CONF" ]; then
2511 _err
"'$NGINX_CONF' doesn't exist."
2515 _debug
"Found nginx conf file:$NGINX_CONF"
2517 _start_f
="$NGINX_CONF"
2519 _debug
"Start detect nginx conf for $_d from:$_start_f"
2520 if ! _checkConf
"$_d" "$_start_f"; then
2521 _err
"Can not find conf file for domain $d"
2524 _info
"Found conf file: $FOUND_REAL_NGINX_CONF"
2526 _ln
=$FOUND_REAL_NGINX_CONF_LN
2529 _lnn
=$
(_math
$_ln + 1)
2531 _start_tag
="$(sed -n "$_lnn,${_lnn}p
" "$FOUND_REAL_NGINX_CONF")"
2532 _debug
"_start_tag" "$_start_tag"
2533 if [ "$_start_tag" = "$NGINX_START" ]; then
2534 _info
"The domain $_d is already configured, skip"
2535 FOUND_REAL_NGINX_CONF
=""
2539 mkdir
-p "$DOMAIN_BACKUP_PATH"
2540 _backup_conf
="$DOMAIN_BACKUP_PATH/$_d.nginx.conf"
2541 _debug _backup_conf
"$_backup_conf"
2542 BACKUP_NGINX_CONF
="$_backup_conf"
2543 _info
"Backup $FOUND_REAL_NGINX_CONF to $_backup_conf"
2544 if ! cp "$FOUND_REAL_NGINX_CONF" "$_backup_conf"; then
2545 _err
"backup error."
2546 FOUND_REAL_NGINX_CONF
=""
2550 _info
"Check the nginx conf before setting up."
2551 if ! _exec
"nginx -t" >/dev
/null
; then
2556 _info
"OK, Set up nginx config file"
2558 if ! sed -n "1,${_ln}p" "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"; then
2559 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2560 _err
"write nginx conf error, but don't worry, the file is restored to the original version."
2565 location ~ \"^/\.well-known/acme-challenge/([-_a-zA-Z0-9]+)\$\" {
2566 default_type text/plain;
2567 return 200 \"\$1.$_thumbpt\";
2570 " >>"$FOUND_REAL_NGINX_CONF"
2572 if ! sed -n "${_lnn},99999p" "$_backup_conf" >>"$FOUND_REAL_NGINX_CONF"; then
2573 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2574 _err
"write nginx conf error, but don't worry, the file is restored."
2578 _info
"nginx conf is done, let's check it again."
2579 if ! _exec
"nginx -t" >/dev
/null
; then
2581 _err
"It seems that nginx conf was broken, let's restore."
2582 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2586 _info
"Reload nginx"
2587 if ! _exec
"nginx -s reload" >/dev
/null
; then
2589 _err
"It seems that nginx reload error, let's restore."
2590 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
2601 _debug
"Start _checkConf from:$_c_file"
2602 if [ ! -f "$2" ] && ! echo "$2" |
grep '*$' >/dev
/null
&& echo "$2" |
grep '*' >/dev
/null
; then
2605 if [ -f "$_w_f" ] && _checkConf
"$1" "$_w_f"; then
2611 elif [ -f "$2" ]; then
2613 if _isRealNginxConf
"$1" "$2"; then
2614 _debug
"$2 is found."
2615 FOUND_REAL_NGINX_CONF
="$2"
2618 if cat "$2" |
tr "\t" " " |
grep "^ *include *.*;" >/dev
/null
; then
2619 _debug
"Try include files"
2620 for included
in $
(cat "$2" |
tr "\t" " " |
grep "^ *include *.*;" |
sed "s/include //" |
tr -d " ;"); do
2621 _debug
"check included $included"
2622 if _checkConf
"$1" "$included"; then
2629 _debug
"$2 not found."
2636 _isRealNginxConf
() {
2637 _debug
"_isRealNginxConf $1 $2"
2638 if [ -f "$2" ]; then
2639 for _fln
in $
(tr "\t" ' ' <"$2" |
grep -n "^ *server_name.* $1" | cut
-d : -f 1); do
2641 if [ "$_fln" ]; then
2642 _start
=$
(tr "\t" ' ' <"$2" | _head_n
"$_fln" |
grep -n "^ *server *{" | _tail_n
1)
2643 _debug
"_start" "$_start"
2644 _start_n
=$
(echo "$_start" | cut
-d : -f 1)
2645 _start_nn
=$
(_math
$_start_n + 1)
2646 _debug
"_start_n" "$_start_n"
2647 _debug
"_start_nn" "$_start_nn"
2649 _left
="$(sed -n "${_start_nn},99999p
" "$2")"
2650 _debug2 _left
"$_left"
2651 if echo "$_left" |
tr "\t" ' ' |
grep -n "^ *server *{" >/dev
/null
; then
2652 _end
=$
(echo "$_left" |
tr "\t" ' ' |
grep -n "^ *server *{" | _head_n
1)
2653 _debug
"_end" "$_end"
2654 _end_n
=$
(echo "$_end" | cut
-d : -f 1)
2655 _debug
"_end_n" "$_end_n"
2656 _seg_n
=$
(echo "$_left" |
sed -n "1,${_end_n}p")
2661 _debug
"_seg_n" "$_seg_n"
2663 if [ "$(echo "$_seg_n" | _egrep_o "^
*ssl
*on
*;")" ]; then
2664 _debug
"ssl on, skip"
2667 FOUND_REAL_NGINX_CONF_LN
=$_fln
2675 #restore all the nginx conf
2677 if [ -z "$NGINX_RESTORE_VLIST" ]; then
2678 _debug
"No need to restore nginx, skip."
2681 _debug
"_restoreNginx"
2682 _debug
"NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST"
2684 for ng_entry
in $
(echo "$NGINX_RESTORE_VLIST" |
tr "$dvsep" ' '); do
2685 _debug
"ng_entry" "$ng_entry"
2686 _nd
=$
(echo "$ng_entry" | cut
-d "$sep" -f 1)
2687 _ngconf
=$
(echo "$ng_entry" | cut
-d "$sep" -f 2)
2688 _ngbackupconf
=$
(echo "$ng_entry" | cut
-d "$sep" -f 3)
2689 _info
"Restoring from $_ngbackupconf to $_ngconf"
2690 cat "$_ngbackupconf" >"$_ngconf"
2693 _info
"Reload nginx"
2694 if ! _exec
"nginx -s reload" >/dev
/null
; then
2696 _err
"It seems that nginx reload error, please report bug."
2703 _stopserver
"$serverproc"
2708 if [ -z "$DEBUG" ]; then
2717 _debug
"_clearupdns"
2718 if [ "$dnsadded" != 1 ] ||
[ -z "$vlist" ]; then
2719 _debug
"Dns not added, skip."
2723 ventries
=$
(echo "$vlist" |
tr ',' ' ')
2724 for ventry
in $ventries; do
2725 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
2726 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
2727 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
2728 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
2729 txt
="$(printf "%s
" "$keyauthorization" | _digest "sha256
" | _url_replace)"
2731 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
2732 _debug
"$d is already verified, skip $vtype."
2736 if [ "$vtype" != "$VTYPE_DNS" ]; then
2737 _info
"Skip $d for $vtype"
2741 d_api
="$(_findHook "$d" dnsapi "$_currentRoot")"
2742 _debug d_api
"$d_api"
2744 if [ -z "$d_api" ]; then
2745 _info
"Not Found domain api file: $d_api"
2750 if ! .
"$d_api"; then
2751 _err
"Load file $d_api error. Please check your api file and try again."
2755 rmcommand
="${_currentRoot}_rm"
2756 if ! _exists
"$rmcommand"; then
2757 _err
"It seems that your api file doesn't define $rmcommand"
2761 txtdomain
="_acme-challenge.$d"
2763 if ! $rmcommand "$txtdomain" "$txt"; then
2764 _err
"Error removing txt for domain:$txtdomain"
2772 # webroot removelevel tokenfile
2773 _clearupwebbroot
() {
2775 if [ -z "$__webroot" ]; then
2776 _debug
"no webroot specified, skip"
2781 if [ "$2" = '1' ]; then
2782 _rmpath
="$__webroot/.well-known"
2783 elif [ "$2" = '2' ]; then
2784 _rmpath
="$__webroot/.well-known/acme-challenge"
2785 elif [ "$2" = '3' ]; then
2786 _rmpath
="$__webroot/.well-known/acme-challenge/$3"
2788 _debug
"Skip for removelevel:$2"
2791 if [ "$_rmpath" ]; then
2792 if [ "$DEBUG" ]; then
2793 _debug
"Debugging, skip removing: $_rmpath"
2803 _on_before_issue
() {
2805 _chk_main_domain
="$2"
2806 _chk_alt_domains
="$3"
2808 _chk_local_addr
="$5"
2809 _debug _on_before_issue
2811 if [ "$_chk_pre_hook" ]; then
2812 _info
"Run pre hook:'$_chk_pre_hook'"
2814 cd "$DOMAIN_PATH" && eval "$_chk_pre_hook"
2816 _err
"Error when run pre hook."
2821 if _hasfield
"$_chk_web_roots" "$NO_VALUE"; then
2822 if ! _exists
"nc"; then
2823 _err
"Please install netcat(nc) tools first."
2828 _debug Le_LocalAddress
"$_chk_local_addr"
2830 alldomains
=$
(echo "$_chk_main_domain,$_chk_alt_domains" |
tr ',' ' ')
2834 for d
in $alldomains; do
2835 _debug
"Check for domain" "$d"
2836 _currentRoot
="$(_getfield "$_chk_web_roots" $_index)"
2837 _debug
"_currentRoot" "$_currentRoot"
2838 _index
=$
(_math
$_index + 1)
2840 if [ "$_currentRoot" = "$NO_VALUE" ]; then
2841 _info
"Standalone mode."
2842 if [ -z "$Le_HTTPPort" ]; then
2845 _savedomainconf
"Le_HTTPPort" "$Le_HTTPPort"
2847 _checkport
="$Le_HTTPPort"
2848 elif [ "$_currentRoot" = "$W_TLS" ]; then
2849 _info
"Standalone tls mode."
2850 if [ -z "$Le_TLSPort" ]; then
2853 _savedomainconf
"Le_TLSPort" "$Le_TLSPort"
2855 _checkport
="$Le_TLSPort"
2858 if [ "$_checkport" ]; then
2859 _debug _checkport
"$_checkport"
2860 _checkaddr
="$(_getfield "$_chk_local_addr" $_addrIndex)"
2861 _debug _checkaddr
"$_checkaddr"
2863 _addrIndex
="$(_math $_addrIndex + 1)"
2865 _netprc
="$(_ss "$_checkport" | grep "$_checkport")"
2866 netprc
="$(echo "$_netprc" | grep "$_checkaddr")"
2867 if [ -z "$netprc" ]; then
2868 netprc
="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
2870 if [ "$netprc" ]; then
2872 _err
"tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
2873 _err
"Please stop it first"
2879 if _hasfield
"$_chk_web_roots" "apache"; then
2880 if ! _setApache
; then
2881 _err
"set up apache error. Report error to me."
2893 _debug _on_issue_err
2894 if [ "$LOG_FILE" ]; then
2895 _err
"Please check log file for more details: $LOG_FILE"
2897 _err
"Please add '--debug' or '--log' to check more details."
2898 _err
"See: $_DEBUG_WIKI"
2902 if [ "$_chk_post_hook" ]; then
2903 _info
"Run post hook:'$_chk_post_hook'"
2905 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
2907 _err
"Error when run post hook."
2912 #trigger the validation to flush the pending authz
2913 if [ "$_chk_vlist" ]; then
2915 _debug2
"_chk_vlist" "$_chk_vlist"
2916 _debug2
"start to deactivate authz"
2917 ventries
=$
(echo "$_chk_vlist" |
tr "$dvsep" ' ')
2918 for ventry
in $ventries; do
2919 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
2920 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
2921 uri
=$
(echo "$ventry" | cut
-d "$sep" -f 3)
2922 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
2923 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
2924 __trigger_validation
"$uri" "$keyauthorization"
2929 if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
2930 _debug
"$(_dlg_versions)"
2935 _on_issue_success
() {
2937 _chk_renew_hook
="$2"
2938 _debug _on_issue_success
2940 if [ "$_chk_post_hook" ]; then
2941 _info
"Run post hook:'$_chk_post_hook'"
2943 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
2945 _err
"Error when run post hook."
2951 if [ "$IS_RENEW" ] && [ "$_chk_renew_hook" ]; then
2952 _info
"Run renew hook:'$_chk_renew_hook'"
2954 cd "$DOMAIN_PATH" && eval "$_chk_renew_hook"
2956 _err
"Error when run renew hook."
2971 _regAccount
"$_reg_length"
2974 __calcAccountKeyHash
() {
2975 [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256
<"$ACCOUNT_KEY_PATH"
2978 __calc_account_thumbprint
() {
2979 printf "%s" "$jwk" |
tr -d ' ' | _digest
"sha256" | _url_replace
2987 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
2989 _info
"mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
2990 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
2993 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
2995 _info
"mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
2996 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
2999 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3000 if ! _create_account_key
"$_reg_length"; then
3001 _err
"Create account key error."
3006 if ! _calcjwk
"$ACCOUNT_KEY_PATH"; then
3013 _debug AGREEMENT
"$AGREEMENT"
3015 regjson
='{"resource": "'$_reg_res'", "agreement": "'$AGREEMENT'"}'
3017 if [ "$ACCOUNT_EMAIL" ]; then
3018 regjson
='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
3021 if [ -z "$_updateTos" ]; then
3022 _info
"Registering account"
3024 if ! _send_signed_request
"$API/acme/new-reg" "$regjson"; then
3025 _err
"Register account Error: $response"
3029 if [ "$code" = "" ] ||
[ "$code" = '201' ]; then
3030 echo "$response" >"$ACCOUNT_JSON_PATH"
3032 elif [ "$code" = '409' ]; then
3033 _info
"Already registered"
3035 _err
"Register account Error: $response"
3039 _accUri
="$(echo "$responseHeaders" | grep "^Location
:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
3040 _debug
"_accUri" "$_accUri"
3042 _tos
="$(echo "$responseHeaders" | grep "^Link
:.
*rel
=\"terms-of-service
\"" | _head_n 1 | _egrep_o "<.
*>" | tr -d '<>')"
3043 _debug
"_tos" "$_tos"
3044 if [ -z "$_tos" ]; then
3045 _debug
"Use default tos: $DEFAULT_AGREEMENT"
3046 _tos
="$DEFAULT_AGREEMENT"
3048 if [ "$_tos" != "$AGREEMENT" ]; then
3056 _debug
"Update tos: $_tos"
3057 if ! _send_signed_request
"$_accUri" "$regjson"; then
3058 _err
"Update tos error."
3061 if [ "$code" = '202' ]; then
3062 _info
"Update success."
3064 CA_KEY_HASH
="$(__calcAccountKeyHash)"
3065 _debug
"Calc CA_KEY_HASH" "$CA_KEY_HASH"
3066 _savecaconf CA_KEY_HASH
"$CA_KEY_HASH"
3068 _err
"Update account error."
3072 ACCOUNT_THUMBPRINT
="$(__calc_account_thumbprint)"
3073 _info
"ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT"
3079 # domain folder file
3085 if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
3086 d_api
="$_SCRIPT_HOME/$_hookcat/$_hookname"
3087 elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
3088 d_api
="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
3089 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
3090 d_api
="$LE_WORKING_DIR/$_hookdomain/$_hookname"
3091 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
3092 d_api
="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
3093 elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
3094 d_api
="$LE_WORKING_DIR/$_hookname"
3095 elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
3096 d_api
="$LE_WORKING_DIR/$_hookname.sh"
3097 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
3098 d_api
="$LE_WORKING_DIR/$_hookcat/$_hookname"
3099 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
3100 d_api
="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
3103 printf "%s" "$d_api"
3107 __get_domain_new_authz
() {
3109 _info
"Getting new-authz for domain" "$_gdnd"
3111 _Max_new_authz_retry_times
=5
3113 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
3114 _debug
"Try new-authz for the $_authz_i time."
3115 if ! _send_signed_request
"$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
3116 _err
"Can not get domain new authz."
3119 if _contains
"$response" "No registration exists matching provided key"; then
3120 _err
"It seems there is an error, but it's recovered now, please try again."
3121 _err
"If you see this message for a second time, please report bug: $(__green "$PROJECT")"
3122 _clearcaconf
"CA_KEY_HASH"
3125 if ! _contains
"$response" "An error occurred while processing your request"; then
3126 _info
"The new-authz request is ok."
3129 _authz_i
="$(_math "$_authz_i" + 1)"
3130 _info
"The server is busy, Sleep $_authz_i to retry."
3134 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
3135 _err
"new-authz retry reach the max $_Max_new_authz_retry_times times."
3138 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
3139 _err
"new-authz error: $response"
3145 #uri keyAuthorization
3146 __trigger_validation
() {
3147 _debug2
"tigger domain validation."
3149 _debug2 _t_url
"$_t_url"
3151 _debug2 _t_key_authz
"$_t_key_authz"
3152 _send_signed_request
"$_t_url" "{\"resource\": \"challenge\", \"keyAuthorization\": \"$_t_key_authz\"}"
3155 #webroot, domain domainlist keylength
3157 if [ -z "$2" ]; then
3158 _usage
"Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
3161 if [ -z "$1" ]; then
3162 _usage
"Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc."
3168 if _contains
"$_main_domain" ","; then
3169 _main_domain
=$
(echo "$2,$3" | cut
-d , -f 1)
3170 _alt_domains
=$
(echo "$2,$3" | cut
-d , -f 2- |
sed "s/,${NO_VALUE}$//")
3177 _real_fullchain
="$9"
3183 #remove these later.
3184 if [ "$_web_roots" = "dns-cf" ]; then
3187 if [ "$_web_roots" = "dns-dp" ]; then
3190 if [ "$_web_roots" = "dns-cx" ]; then
3193 _debug
"Using api: $API"
3195 if [ ! "$IS_RENEW" ]; then
3196 _initpath
"$_main_domain" "$_key_length"
3197 mkdir
-p "$DOMAIN_PATH"
3200 if [ -f "$DOMAIN_CONF" ]; then
3201 Le_NextRenewTime
=$
(_readdomainconf Le_NextRenewTime
)
3202 _debug Le_NextRenewTime
"$Le_NextRenewTime"
3203 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
3204 _saved_domain
=$
(_readdomainconf Le_Domain
)
3205 _debug _saved_domain
"$_saved_domain"
3206 _saved_alt
=$
(_readdomainconf Le_Alt
)
3207 _debug _saved_alt
"$_saved_alt"
3208 if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then
3209 _info
"Domains not changed."
3210 _info
"Skip, Next renewal time is: $(__green "$
(_readdomainconf Le_NextRenewTimeStr
)")"
3211 _info
"Add '$(__red '--force')' to force to renew."
3214 _info
"Domains have changed."
3219 _savedomainconf
"Le_Domain" "$_main_domain"
3220 _savedomainconf
"Le_Alt" "$_alt_domains"
3221 _savedomainconf
"Le_Webroot" "$_web_roots"
3223 _savedomainconf
"Le_PreHook" "$_pre_hook"
3224 _savedomainconf
"Le_PostHook" "$_post_hook"
3225 _savedomainconf
"Le_RenewHook" "$_renew_hook"
3227 if [ "$_local_addr" ]; then
3228 _savedomainconf
"Le_LocalAddress" "$_local_addr"
3230 _cleardomainconf
"Le_LocalAddress"
3234 _savedomainconf
"Le_API" "$Le_API"
3236 if [ "$_alt_domains" = "$NO_VALUE" ]; then
3240 if [ "$_key_length" = "$NO_VALUE" ]; then
3244 if ! _on_before_issue
"$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
3245 _err
"_on_before_issue."
3249 _saved_account_key_hash
="$(_readcaconf "CA_KEY_HASH
")"
3250 _debug2 _saved_account_key_hash
"$_saved_account_key_hash"
3252 if [ -z "$_saved_account_key_hash" ] ||
[ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
3253 if ! _regAccount
"$_accountkeylength"; then
3254 _on_issue_err
"$_post_hook"
3258 _debug
"_saved_account_key_hash is not changed, skip register account."
3261 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
3262 _info
"Signing from existing CSR."
3264 _key
=$
(_readdomainconf Le_Keylength
)
3265 _debug
"Read key length:$_key"
3266 if [ ! -f "$CERT_KEY_PATH" ] ||
[ "$_key_length" != "$_key" ]; then
3267 if ! createDomainKey
"$_main_domain" "$_key_length"; then
3268 _err
"Create domain key error."
3270 _on_issue_err
"$_post_hook"
3275 if ! _createcsr
"$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
3276 _err
"Create CSR error."
3278 _on_issue_err
"$_post_hook"
3283 _savedomainconf
"Le_Keylength" "$_key_length"
3287 _info
"Getting domain auth token for each domain"
3290 if [ -z "$vlist" ]; then
3291 alldomains
=$
(echo "$_main_domain,$_alt_domains" |
tr ',' ' ')
3294 for d
in $alldomains; do
3295 _info
"Getting webroot for domain" "$d"
3296 _w
="$(echo $_web_roots | cut -d , -f $_index)"
3301 _debug
"_currentRoot" "$_currentRoot"
3302 _index
=$
(_math
$_index + 1)
3305 if _startswith
"$_currentRoot" "dns"; then
3309 if [ "$_currentRoot" = "$W_TLS" ]; then
3313 if ! __get_domain_new_authz
"$d"; then
3315 _on_issue_err
"$_post_hook"
3319 if [ -z "$thumbprint" ]; then
3320 thumbprint
="$(__calc_account_thumbprint)"
3323 entry
="$(printf "%s
\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
3324 _debug entry
"$entry"
3325 if [ -z "$entry" ]; then
3326 _err
"Error, can not get domain token $d"
3328 _on_issue_err
"$_post_hook"
3331 token
="$(printf "%s
\n" "$entry" | _egrep_o '"token
":"[^
"]*' | cut -d : -f 2 | tr -d '"')"
3332 _debug token "$token"
3334 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
3337 keyauthorization
="$token.$thumbprint"
3338 _debug keyauthorization
"$keyauthorization"
3340 if printf "%s" "$response" |
grep '"status":"valid"' >/dev
/null
2>&1; then
3341 _debug
"$d is already verified, skip."
3342 keyauthorization
="$STATE_VERIFIED"
3343 _debug keyauthorization
"$keyauthorization"
3346 dvlist
="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
3347 _debug dvlist
"$dvlist"
3349 vlist
="$vlist$dvlist$dvsep"
3352 _debug vlist
"$vlist"
3355 ventries
=$
(echo "$vlist" |
tr "$dvsep" ' ')
3356 for ventry
in $ventries; do
3357 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
3358 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
3359 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
3360 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
3362 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
3363 _debug
"$d is already verified, skip $vtype."
3367 if [ "$vtype" = "$VTYPE_DNS" ]; then
3369 txtdomain
="_acme-challenge.$d"
3370 _debug txtdomain
"$txtdomain"
3371 txt
="$(printf "%s
" "$keyauthorization" | _digest "sha256
" | _url_replace)"
3374 d_api
="$(_findHook "$d" dnsapi "$_currentRoot")"
3376 _debug d_api
"$d_api"
3378 if [ "$d_api" ]; then
3379 _info
"Found domain api file: $d_api"
3381 _err
"Add the following TXT record:"
3382 _err
"Domain: '$(__green "$txtdomain")'"
3383 _err
"TXT value: '$(__green "$txt")'"
3384 _err
"Please be aware that you prepend _acme-challenge. before your domain"
3385 _err
"so the resulting subdomain will be: $txtdomain"
3390 if ! .
"$d_api"; then
3391 _err
"Load file $d_api error. Please check your api file and try again."
3395 addcommand
="${_currentRoot}_add"
3396 if ! _exists
"$addcommand"; then
3397 _err
"It seems that your api file is not correct, it must have a function named: $addcommand"
3401 if ! $addcommand "$txtdomain" "$txt"; then
3402 _err
"Error add txt for domain:$txtdomain"
3407 if [ "$?" != "0" ]; then
3409 _on_issue_err
"$_post_hook"
3416 if [ "$dnsadded" = '0' ]; then
3417 _savedomainconf
"Le_Vlist" "$vlist"
3418 _debug
"Dns record not added yet, so, save to $DOMAIN_CONF and exit."
3419 _err
"Please add the TXT records to the domains, and retry again."
3421 _on_issue_err
"$_post_hook"
3427 if [ "$dnsadded" = '1' ]; then
3428 if [ -z "$Le_DNSSleep" ]; then
3429 Le_DNSSleep
="$DEFAULT_DNS_SLEEP"
3431 _savedomainconf
"Le_DNSSleep" "$Le_DNSSleep"
3434 _info
"Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
3435 _sleep
"$Le_DNSSleep"
3438 NGINX_RESTORE_VLIST
=""
3439 _debug
"ok, let's start to verify"
3442 ventries
=$
(echo "$vlist" |
tr "$dvsep" ' ')
3443 for ventry
in $ventries; do
3444 d
=$
(echo "$ventry" | cut
-d "$sep" -f 1)
3445 keyauthorization
=$
(echo "$ventry" | cut
-d "$sep" -f 2)
3446 uri
=$
(echo "$ventry" | cut
-d "$sep" -f 3)
3447 vtype
=$
(echo "$ventry" | cut
-d "$sep" -f 4)
3448 _currentRoot
=$
(echo "$ventry" | cut
-d "$sep" -f 5)
3450 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
3451 _info
"$d is already verified, skip $vtype."
3455 _info
"Verifying:$d"
3457 _debug
"keyauthorization" "$keyauthorization"
3460 token
="$(printf "%s
" "$keyauthorization" | cut -d '.' -f 1)"
3462 _debug
"_currentRoot" "$_currentRoot"
3464 if [ "$vtype" = "$VTYPE_HTTP" ]; then
3465 if [ "$_currentRoot" = "$NO_VALUE" ]; then
3466 _info
"Standalone mode server"
3467 _ncaddr
="$(_getfield "$_local_addr" "$_ncIndex")"
3468 _ncIndex
="$(_math $_ncIndex + 1)"
3469 _startserver
"$keyauthorization" "$_ncaddr" &
3470 if [ "$?" != "0" ]; then
3472 _on_issue_err
"$_post_hook" "$vlist"
3477 _debug serverproc
"$serverproc"
3478 elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
3479 _info
"Stateless mode for domain:$d"
3481 elif _startswith
"$_currentRoot" "$NGINX"; then
3482 _info
"Nginx mode for domain:$d"
3483 #set up nginx server
3484 FOUND_REAL_NGINX_CONF
=""
3485 BACKUP_NGINX_CONF
=""
3486 if ! _setNginx
"$d" "$_currentRoot" "$thumbprint"; then
3488 _on_issue_err
"$_post_hook" "$vlist"
3492 if [ "$FOUND_REAL_NGINX_CONF" ]; then
3493 _realConf
="$FOUND_REAL_NGINX_CONF"
3494 _backup
="$BACKUP_NGINX_CONF"
3495 _debug _realConf
"$_realConf"
3496 NGINX_RESTORE_VLIST
="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
3500 if [ "$_currentRoot" = "apache" ]; then
3501 wellknown_path
="$ACME_DIR"
3503 wellknown_path
="$_currentRoot/.well-known/acme-challenge"
3504 if [ ! -d "$_currentRoot/.well-known" ]; then
3506 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
3513 _debug wellknown_path
"$wellknown_path"
3515 _debug
"writing token:$token to $wellknown_path/$token"
3517 mkdir
-p "$wellknown_path"
3519 if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
3520 _err
"$d:Can not write token to file : $wellknown_path/$token"
3521 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3523 _on_issue_err
"$_post_hook" "$vlist"
3527 if [ ! "$usingApache" ]; then
3528 if webroot_owner
=$
(_stat
"$_currentRoot"); then
3529 _debug
"Changing owner/group of .well-known to $webroot_owner"
3530 if ! _exec
"chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then
3531 _debug
"$(cat "$_EXEC_TEMP_ERR")"
3532 _exec_err
>/dev
/null
2>&1
3535 _debug
"not changing owner/group of webroot"
3541 elif [ "$vtype" = "$VTYPE_TLS" ]; then
3543 #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
3544 #_debug2 _hash_A "$_hash_A"
3545 #_x="$(echo $_hash_A | cut -c 1-32)"
3547 #_y="$(echo $_hash_A | cut -c 33-64)"
3549 #_SAN_A="$_x.$_y.token.acme.invalid"
3550 #_debug2 _SAN_A "$_SAN_A"
3553 _hash_B
="$(printf "%s
" "$keyauthorization" | _digest "sha256
" "hex
")"
3554 _debug2 _hash_B
"$_hash_B"
3555 _x
="$(echo "$_hash_B" | cut -c 1-32)"
3557 _y
="$(echo "$_hash_B" | cut -c 33-64)"
3560 #_SAN_B="$_x.$_y.ka.acme.invalid"
3562 _SAN_B
="$_x.$_y.acme.invalid"
3563 _debug2 _SAN_B
"$_SAN_B"
3565 _ncaddr
="$(_getfield "$_local_addr" "$_ncIndex")"
3566 _ncIndex
="$(_math "$_ncIndex" + 1)"
3567 if ! _starttlsserver
"$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
3568 _err
"Start tls server error."
3569 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3571 _on_issue_err
"$_post_hook" "$vlist"
3576 if ! __trigger_validation
"$uri" "$keyauthorization"; then
3577 _err
"$d:Can not get challenge: $response"
3578 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3580 _on_issue_err
"$_post_hook" "$vlist"
3584 if [ ! -z "$code" ] && [ ! "$code" = '202' ]; then
3585 _err
"$d:Challenge error: $response"
3586 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3588 _on_issue_err
"$_post_hook" "$vlist"
3593 if [ -z "$MAX_RETRY_TIMES" ]; then
3598 waittimes
=$
(_math
"$waittimes" + 1)
3599 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
3601 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3603 _on_issue_err
"$_post_hook" "$vlist"
3607 _debug
"sleep 2 secs to verify"
3610 response
="$(_get "$uri")"
3611 if [ "$?" != "0" ]; then
3612 _err
"$d:Verify error:$response"
3613 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3615 _on_issue_err
"$_post_hook" "$vlist"
3618 _debug2 original
"$response"
3620 response
="$(echo "$response" | _normalizeJson)"
3621 _debug2 response
"$response"
3623 status
=$
(echo "$response" | _egrep_o
'"status":"[^"]*' | cut
-d : -f 2 |
tr -d '"')
3624 if [ "$status" = "valid" ]; then
3625 _info
"$(__green Success)"
3626 _stopserver
"$serverproc"
3628 _clearupwebbroot
"$_currentRoot" "$removelevel" "$token"
3632 if [ "$status" = "invalid" ]; then
3633 error
="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error
":\{[^\}]*')"
3634 _debug2 error
"$error"
3635 errordetail
="$(echo "$error" | _egrep_o '"detail
": *"[^
"]*' | cut -d '"' -f 4)"
3636 _debug2 errordetail "$errordetail"
3637 if [ "$errordetail" ]; then
3638 _err "$d:Verify error:$errordetail"
3640 _err "$d:Verify error:$error"
3642 if [ "$DEBUG" ]; then
3643 if [ "$vtype" = "$VTYPE_HTTP" ]; then
3644 _debug "Debug: get token url."
3645 _get "http://$d/.well-known/acme-challenge/$token" "" 1
3648 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3650 _on_issue_err "$_post_hook" "$vlist"
3654 if [ "$status" = "pending" ]; then
3657 _err "$d:Verify error:$response"
3658 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3660 _on_issue_err "$_post_hook" "$vlist"
3669 _info "Verify finished, start to sign."
3670 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)"
3672 if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"; then
3674 _on_issue_err "$_post_hook"
3679 Le_LinkCert="$(grep -i '^Location.
*$
' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
3680 _debug "Le_LinkCert" "$Le_LinkCert"
3681 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
3683 if [ "$Le_LinkCert" ]; then
3684 echo "$BEGIN_CERT" >"$CERT_PATH"
3686 #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
3687 # _debug "Get cert failed. Let's try last response.
"
3688 # printf -- "%s
" "$_rcert" | _dbase64 "multiline
" | _base64 "multiline
" >> "$CERT_PATH"
3691 if ! printf -- "%s
" "$_rcert" | _dbase64 "multiline
" | _base64 "multiline
" >>"$CERT_PATH"; then
3692 _debug "Try cert link.
"
3693 _get "$Le_LinkCert" | _base64 "multiline
" >>"$CERT_PATH"
3696 echo "$END_CERT" >>"$CERT_PATH"
3697 _info "$
(__green
"Cert success.")"
3700 _info "Your cert is
in $
(__green
" $CERT_PATH ")"
3702 if [ -f "$CERT_KEY_PATH" ]; then
3703 _info "Your cert key is
in $
(__green
" $CERT_KEY_PATH ")"
3706 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
3708 if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
3710 _saveaccountconf "USER_PATH
" "$USER_PATH"
3714 if [ -z "$Le_LinkCert" ]; then
3715 response="$
(echo "$response" | _dbase64
"multiline" | _normalizeJson
)"
3716 _err "Sign failed
: $
(echo "$response" | _egrep_o
'"detail":"[^"]*"')"
3717 _on_issue_err "$_post_hook"
3721 _cleardomainconf "Le_Vlist
"
3723 Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
3724 if ! _contains "$Le_LinkIssuer" ":"; then
3725 Le_LinkIssuer="$API$Le_LinkIssuer"
3727 _debug Le_LinkIssuer "$Le_LinkIssuer"
3728 _savedomainconf "Le_LinkIssuer
" "$Le_LinkIssuer"
3730 if [ "$Le_LinkIssuer" ]; then
3731 _link_issuer_retry=0
3733 while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do
3734 _debug _link_issuer_retry "$_link_issuer_retry"
3735 if _get "$Le_LinkIssuer" >"$CA_CERT_PATH.der
"; then
3736 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
3737 _base64 "multiline
" <"$CA_CERT_PATH.der
" >>"$CA_CERT_PATH"
3738 echo "$END_CERT" >>"$CA_CERT_PATH"
3740 _info "The intermediate CA cert is
in $
(__green
" $CA_CERT_PATH ")"
3741 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
3742 _info "And the full chain certs is there
: $
(__green
" $CERT_FULLCHAIN_PATH ")"
3744 rm -f "$CA_CERT_PATH.der
"
3747 _link_issuer_retry=$(_math $_link_issuer_retry + 1)
3748 _sleep "$_link_issuer_retry"
3750 if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then
3751 _err "Max retry
for issuer ca cert is reached.
"
3754 _debug "No Le_LinkIssuer header found.
"
3757 Le_CertCreateTime=$(_time)
3758 _savedomainconf "Le_CertCreateTime
" "$Le_CertCreateTime"
3760 Le_CertCreateTimeStr=$(date -u)
3761 _savedomainconf "Le_CertCreateTimeStr
" "$Le_CertCreateTimeStr"
3763 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ]; then
3764 Le_RenewalDays="$MAX_RENEW"
3766 _savedomainconf "Le_RenewalDays
" "$Le_RenewalDays"
3769 if [ "$CA_BUNDLE" ]; then
3770 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
3772 _clearaccountconf "CA_BUNDLE
"
3775 if [ "$CA_PATH" ]; then
3776 _saveaccountconf CA_PATH "$CA_PATH"
3778 _clearaccountconf "CA_PATH
"
3781 if [ "$HTTPS_INSECURE" ]; then
3782 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
3784 _clearaccountconf "HTTPS_INSECURE
"
3787 if [ "$Le_Listen_V4" ]; then
3788 _savedomainconf "Le_Listen_V4
" "$Le_Listen_V4"
3789 _cleardomainconf Le_Listen_V6
3790 elif [ "$Le_Listen_V6" ]; then
3791 _savedomainconf "Le_Listen_V6
" "$Le_Listen_V6"
3792 _cleardomainconf Le_Listen_V4
3795 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
3797 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
3798 _savedomainconf "Le_NextRenewTimeStr
" "$Le_NextRenewTimeStr"
3800 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
3801 _savedomainconf "Le_NextRenewTime
" "$Le_NextRenewTime"
3803 _on_issue_success "$_post_hook" "$_renew_hook"
3805 if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
3806 _savedomainconf "Le_RealCertPath
" "$_real_cert"
3807 _savedomainconf "Le_RealCACertPath
" "$_real_ca"
3808 _savedomainconf "Le_RealKeyPath
" "$_real_key"
3809 _savedomainconf "Le_ReloadCmd
" "$_reload_cmd"
3810 _savedomainconf "Le_RealFullChainPath
" "$_real_fullchain"
3811 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
3819 if [ -z "$Le_Domain" ]; then
3820 _usage "Usage
: $PROJECT_ENTRY --renew -d domain.com
[--ecc]"
3826 _initpath "$Le_Domain" "$_isEcc"
3828 _info "$
(__green
"Renew: '$Le_Domain'")"
3829 if [ ! -f "$DOMAIN_CONF" ]; then
3830 _info "'$Le_Domain' is not a issued domain
, skip.
"
3834 if [ "$Le_RenewalDays" ]; then
3835 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
3840 if [ "$Le_API" ]; then
3844 ACCOUNT_JSON_PATH=""
3846 _debug3 "initpath again.
"
3847 _initpath "$Le_Domain" "$_isEcc"
3850 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$
(_time
)" -lt "$Le_NextRenewTime" ]; then
3851 _info "Skip
, Next renewal
time is
: $
(__green
"$Le_NextRenewTimeStr")"
3852 _info "Add
'$(__red '--force')' to force to renew.
"
3853 return "$RENEW_SKIP"
3857 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"
3859 if [ "$res" != "0" ]; then
3863 if [ "$Le_DeployHook" ]; then
3864 _deploy "$Le_Domain" "$Le_DeployHook"
3873 #renewAll [stopRenewOnError]
3876 _stopRenewOnError="$1"
3877 _debug "_stopRenewOnError
" "$_stopRenewOnError"
3880 for di in "${CERT_HOME}"/*.*/; do
3882 if ! [ -d "$di" ]; then
3883 _debug "Not directory
, skip
: $di"
3889 if _endswith "$d" "$ECC_SUFFIX"; then
3890 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
3891 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
3893 renew "$d" "$_isEcc"
3896 _debug "Return code
: $rc"
3897 if [ "$rc" != "0" ]; then
3898 if [ "$rc" = "$RENEW_SKIP" ]; then
3900 elif [ "$_stopRenewOnError" ]; then
3901 _err "Error renew
$d, stop now.
"
3905 _err "Error renew
$d.
"
3916 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
3917 _usage "Usage
: $PROJECT_ENTRY --signcsr --csr mycsr.csr
-w /path
/to
/webroot
/a.com
/ "
3923 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
3924 if [ "$?
" != "0" ]; then
3925 _err "Can not
read subject from csr
: $_csrfile"
3928 _debug _csrsubj "$_csrsubj"
3930 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
3931 if [ "$?
" != "0" ]; then
3932 _err "Can not
read domain list from csr
: $_csrfile"
3935 _debug "_csrdomainlist
" "$_csrdomainlist"
3937 if [ -z "$_csrsubj" ]; then
3938 _csrsubj="$
(_getfield
"$_csrdomainlist" 1)"
3939 _debug _csrsubj "$_csrsubj"
3940 _csrdomainlist="$
(echo "$_csrdomainlist" | cut
-d , -f 2-)"
3941 _debug "_csrdomainlist
" "$_csrdomainlist"
3944 if [ -z "$_csrsubj" ]; then
3945 _err "Can not
read subject from csr
: $_csrfile"
3949 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
3950 if [ "$?
" != "0" ] || [ -z "$_csrkeylength" ]; then
3951 _err "Can not
read key length from csr
: $_csrfile"
3955 _initpath "$_csrsubj" "$_csrkeylength"
3956 mkdir -p "$DOMAIN_PATH"
3958 _info "Copy csr to
: $CSR_PATH"
3959 cp "$_csrfile" "$CSR_PATH"
3961 issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength"
3968 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
3969 _usage "Usage
: $PROJECT_ENTRY --showcsr --csr mycsr.csr
"
3975 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
3976 if [ "$?
" != "0" ] || [ -z "$_csrsubj" ]; then
3977 _err "Can not
read subject from csr
: $_csrfile"
3981 _info "Subject
=$_csrsubj"
3983 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
3984 if [ "$?
" != "0" ]; then
3985 _err "Can not
read domain list from csr
: $_csrfile"
3988 _debug "_csrdomainlist
" "$_csrdomainlist"
3990 _info "SubjectAltNames
=$_csrdomainlist"
3992 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
3993 if [ "$?
" != "0" ] || [ -z "$_csrkeylength" ]; then
3994 _err "Can not
read key length from csr
: $_csrfile"
3997 _info "KeyLength
=$_csrkeylength"
4005 if [ "$_raw" ]; then
4006 printf "%s
\n" "Main_Domain
${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew
"
4007 for di in "${CERT_HOME}"/*.*/; do
4008 if ! [ -d "$di" ]; then
4009 _debug "Not directory
, skip
: $di"
4015 if _endswith "$d" "$ECC_SUFFIX"; then
4016 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
4017 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
4019 _initpath "$d" "$_isEcc"
4020 if [ -f "$DOMAIN_CONF" ]; then
4022 printf "%s
\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
4027 if _exists column; then
4028 list "raw
" | column -t -s "$_sep"
4030 list "raw
" | tr "$_sep" '\t'
4040 for _d_api in $(echo "$_hooks" | tr ',' " "); do
4041 _deployApi="$
(_findHook
"$_d" deploy
"$_d_api")"
4042 if [ -z "$_deployApi" ]; then
4043 _err "The deploy hook
$_d_api is not found.
"
4046 _debug _deployApi "$_deployApi"
4049 if ! . "$_deployApi"; then
4050 _err "Load
file $_deployApi error. Please check your api
file and try again.
"
4054 d_command="${_d_api}_deploy
"
4055 if ! _exists "$d_command"; then
4056 _err "It seems that your api
file is not correct
, it must have a
function named
: $d_command"
4060 if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
4061 _err "Error deploy
for domain
:$_d"
4065 _err "Deploy error.
"
4068 _info "$
(__green Success
)"
4078 if [ -z "$_hooks" ]; then
4079 _usage "Usage
: $PROJECT_ENTRY --deploy -d domain.com
--deploy-hook cpanel
[--ecc] "
4083 _initpath "$_d" "$_isEcc"
4084 if [ ! -d "$DOMAIN_PATH" ]; then
4085 _err "Domain is not valid
:'$_d'"
4091 _savedomainconf Le_DeployHook "$_hooks"
4093 _deploy "$_d" "$_hooks"
4098 if [ -z "$_main_domain" ]; then
4099 _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
]"
4107 _real_fullchain="$6"
4110 _initpath "$_main_domain" "$_isEcc"
4111 if [ ! -d "$DOMAIN_PATH" ]; then
4112 _err "Domain is not valid
:'$_main_domain'"
4116 _savedomainconf "Le_RealCertPath
" "$_real_cert"
4117 _savedomainconf "Le_RealCACertPath
" "$_real_ca"
4118 _savedomainconf "Le_RealKeyPath
" "$_real_key"
4119 _savedomainconf "Le_ReloadCmd
" "$_reload_cmd"
4120 _savedomainconf "Le_RealFullChainPath
" "$_real_fullchain"
4122 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
4125 #domain cert key ca fullchain reloadcmd backup-prefix
4131 _real_fullchain="$5"
4135 if [ "$_real_cert" = "$NO_VALUE" ]; then
4138 if [ "$_real_key" = "$NO_VALUE" ]; then
4141 if [ "$_real_ca" = "$NO_VALUE" ]; then
4144 if [ "$_reload_cmd" = "$NO_VALUE" ]; then
4147 if [ "$_real_fullchain" = "$NO_VALUE" ]; then
4151 _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
4152 mkdir -p "$_backup_path"
4154 if [ "$_real_cert" ]; then
4155 _info "Installing cert to
:$_real_cert"
4156 if [ -f "$_real_cert" ] && [ ! "$IS_RENEW" ]; then
4157 cp "$_real_cert" "$_backup_path/cert.bak
"
4159 cat "$CERT_PATH" >"$_real_cert"
4162 if [ "$_real_ca" ]; then
4163 _info "Installing CA to
:$_real_ca"
4164 if [ "$_real_ca" = "$_real_cert" ]; then
4165 echo "" >>"$_real_ca"
4166 cat "$CA_CERT_PATH" >>"$_real_ca"
4168 if [ -f "$_real_ca" ] && [ ! "$IS_RENEW" ]; then
4169 cp "$_real_ca" "$_backup_path/ca.bak
"
4171 cat "$CA_CERT_PATH" >"$_real_ca"
4175 if [ "$_real_key" ]; then
4176 _info "Installing key to
:$_real_key"
4177 if [ -f "$_real_key" ] && [ ! "$IS_RENEW" ]; then
4178 cp "$_real_key" "$_backup_path/key.bak
"
4180 cat "$CERT_KEY_PATH" >"$_real_key"
4183 if [ "$_real_fullchain" ]; then
4184 _info "Installing full chain to
:$_real_fullchain"
4185 if [ -f "$_real_fullchain" ] && [ ! "$IS_RENEW" ]; then
4186 cp "$_real_fullchain" "$_backup_path/fullchain.bak
"
4188 cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain"
4191 if [ "$_reload_cmd" ]; then
4192 _info "Run reload cmd
: $_reload_cmd"
4195 export CERT_KEY_PATH
4197 export CERT_FULLCHAIN_PATH
4199 cd "$DOMAIN_PATH" && eval "$_reload_cmd"
4201 _info "$
(__green
"Reload success")"
4203 _err "Reload error
for :$Le_Domain"
4213 if ! _exists "crontab
"; then
4214 _err "crontab doesn
't exist, so, we can not install cron jobs."
4215 _err "All your certs will not be renewed automatically."
4216 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
4220 _info "Installing cron job"
4221 if ! crontab -l | grep "$PROJECT_ENTRY --cron"; then
4222 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
4223 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
4225 _err "Can not install cronjob, $PROJECT_ENTRY not found."
4229 if [ "$_c_home" ]; then
4230 _c_entry="--config-home \"$_c_home\" "
4233 random_minute=$(_math $_t % 60)
4234 if _exists uname && uname -a | grep SunOS >/dev/null; then
4237 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
4242 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
4246 if [ "$?" != "0" ]; then
4247 _err "Install cron job failed. You need to manually renew your certs."
4248 _err "Or you can add cronjob by yourself:"
4249 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4254 uninstallcronjob() {
4255 if ! _exists "crontab"; then
4258 _info "Removing cron job"
4259 cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")"
4261 if _exists uname && uname -a | grep solaris >/dev/null; then
4262 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab --
4264 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -
4266 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4267 _info LE_WORKING_DIR
"$LE_WORKING_DIR"
4268 if _contains
"$cr" "--config-home"; then
4269 LE_CONFIG_HOME
="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
4270 _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
4279 if [ -z "$Le_Domain" ]; then
4280 _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com [--ecc]"
4286 _initpath "$Le_Domain" "$_isEcc"
4287 if [ ! -f "$DOMAIN_CONF" ]; then
4288 _err "$Le_Domain is not a issued domain, skip."
4292 if [ ! -f "$CERT_PATH" ]; then
4293 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
4297 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
4299 if [ -z "$cert" ]; then
4300 _err "Cert for $Le_Domain is empty found, skip."
4304 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
4305 uri="$API/acme/revoke-cert"
4307 if [ -f "$CERT_KEY_PATH" ]; then
4308 _info "Try domain key first."
4309 if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
4310 if [ -z "$response" ]; then
4311 _info "Revoke success."
4315 _err "Revoke error by domain key."
4320 _info "Domain key file doesn't exists.
"
4323 _info "Try account key.
"
4325 if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
4326 if [ -z "$response" ]; then
4327 _info "Revoke success.
"
4331 _err "Revoke error.
"
4341 if [ -z "$Le_Domain" ]; then
4342 _usage "Usage
: $PROJECT_ENTRY --remove -d domain.com
[--ecc]"
4348 _initpath "$Le_Domain" "$_isEcc"
4349 _removed_conf="$DOMAIN_CONF.removed
"
4350 if [ ! -f "$DOMAIN_CONF" ]; then
4351 if [ -f "$_removed_conf" ]; then
4352 _err "$Le_Domain is already removed
, You can remove the folder by yourself
: $DOMAIN_PATH"
4354 _err "$Le_Domain is not a issued domain
, skip.
"
4359 if mv "$DOMAIN_CONF" "$_removed_conf"; then
4360 _info "$Le_Domain is removed
, the key and cert files are
in $
(__green
$DOMAIN_PATH)"
4361 _info "You can remove them by yourself.
"
4364 _err "Remove
$Le_Domain failed.
"
4377 while [ "$_d_i" -lt "$_d_max_retry" ]; do
4378 _info "Deactivate
: $_d_domain"
4379 _d_i="$
(_math
$_d_i + 1)"
4381 if ! __get_domain_new_authz "$_d_domain"; then
4382 _err "Can not get domain new authz token.
"
4386 authzUri="$
(echo "$responseHeaders" |
grep "^Location:" | _head_n
1 | cut
-d ' ' -f 2 |
tr -d "\r\n")"
4387 _debug "authzUri
" "$authzUri"
4389 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
4390 _err "new-authz error
: $response"
4394 entry="$
(printf "%s\n" "$response" | _egrep_o
'{"type":"[^"]*","status":"valid","uri"[^}]*')"
4395 _debug entry "$entry"
4397 if [ -z "$entry" ]; then
4398 _info "No
more valid entry found.
"
4402 _vtype="$
(printf "%s\n" "$entry" | _egrep_o
'"type": *"[^"]*"' | cut
-d : -f 2 |
tr -d '"')"
4403 _debug _vtype "$_vtype"
4404 _info "Found
$_vtype"
4406 uri="$
(printf "%s\n" "$entry" | _egrep_o
'"uri":"[^"]*' | cut
-d : -f 2,3 |
tr -d '"')"
4409 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
4410 _info "Skip
$_vtype"
4414 _info "Deactivate
: $_vtype"
4416 if ! _send_signed_request "$authzUri" "{\"resource
\": \"authz
\", \"status
\":\"deactivated
\"}"; then
4417 _err "Can not deactivate
$_vtype.
"
4421 _info "Deactivate
: $_vtype success.
"
4425 if [ "$_d_i" -lt "$_d_max_retry" ]; then
4426 _info "Deactivated success
!"
4428 _err "Deactivate failed.
"
4437 _debug _d_domain_list "$_d_domain_list"
4438 if [ -z "$
(echo $_d_domain_list | cut
-d , -f 1)" ]; then
4439 _usage "Usage
: $PROJECT_ENTRY --deactivate -d domain.com
[-d domain.com
]"
4442 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
4443 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
4446 if ! _deactivate "$_d_dm" "$_d_type"; then
4452 # Detect profile file if not specified as environment variable
4454 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
4460 SHELLTYPE="$
(basename "/$SHELL")"
4462 if [ "$SHELLTYPE" = "bash
" ]; then
4463 if [ -f "$HOME/.bashrc
" ]; then
4464 DETECTED_PROFILE="$HOME/.bashrc
"
4465 elif [ -f "$HOME/.bash_profile
" ]; then
4466 DETECTED_PROFILE="$HOME/.bash_profile
"
4468 elif [ "$SHELLTYPE" = "zsh
" ]; then
4469 DETECTED_PROFILE="$HOME/.zshrc
"
4472 if [ -z "$DETECTED_PROFILE" ]; then
4473 if [ -f "$HOME/.profile
" ]; then
4474 DETECTED_PROFILE="$HOME/.profile
"
4475 elif [ -f "$HOME/.bashrc
" ]; then
4476 DETECTED_PROFILE="$HOME/.bashrc
"
4477 elif [ -f "$HOME/.bash_profile
" ]; then
4478 DETECTED_PROFILE="$HOME/.bash_profile
"
4479 elif [ -f "$HOME/.zshrc
" ]; then
4480 DETECTED_PROFILE="$HOME/.zshrc
"
4484 if [ ! -z "$DETECTED_PROFILE" ]; then
4485 echo "$DETECTED_PROFILE"
4491 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4494 #LOG_FILE=\"$DEFAULT_LOG_FILE\"
4501 " >"$ACCOUNT_CONF_PATH"
4509 if ! _exists "curl
" && ! _exists "wget
"; then
4510 _err "Please
install curl or wget first
, we need to access http resources.
"
4514 if [ -z "$_nocron" ]; then
4515 if ! _exists "crontab
"; then
4516 _err "It is recommended to
install crontab first. try to
install 'cron, crontab, crontabs or vixie-cron'.
"
4517 _err "We need to
set cron job to renew the certs automatically.
"
4518 _err "Otherwise
, your certs will not be able to be renewed automatically.
"
4519 if [ -z "$FORCE" ]; then
4520 _err "Please add
'--force' and try
install again to go without crontab.
"
4521 _err ".
/$PROJECT_ENTRY --install --force"
4527 if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then
4528 _err "Please
install openssl first. ACME_OPENSSL_BIN
=$ACME_OPENSSL_BIN"
4529 _err "We need openssl to generate keys.
"
4533 if ! _exists "nc
"; then
4534 _err "It is recommended to
install nc first
, try to
install 'nc' or
'netcat'.
"
4535 _err "We use nc
for standalone server
if you use standalone mode.
"
4536 _err "If you don
't use standalone mode, just ignore this warning."
4545 if [ -z "$_shebang" ]; then
4546 _usage "Usage: file shebang"
4549 cp "$_file" "$_file.tmp"
4550 echo "$_shebang" >"$_file"
4551 sed -n 2,99999p "$_file.tmp" >>"$_file"
4560 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4561 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
4562 echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
4563 echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
4564 echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
4567 if [ "$_c_home" ]; then
4568 _c_entry=" --config-home '$_c_home'"
4571 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
4572 if [ "$_c_home" ]; then
4573 _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
4575 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4577 _profile="$(_detect_profile)"
4578 if [ "$_profile" ]; then
4579 _debug "Found profile: $_profile"
4580 _info "Installing alias to '$_profile'"
4581 _setopt "$_profile" ". \"$_envfile\""
4582 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
4584 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
4588 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
4589 _csh_profile="$HOME/.cshrc"
4590 if [ -f "$_csh_profile" ]; then
4591 _info "Installing alias to '$_csh_profile'"
4592 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
4593 if [ "$_c_home" ]; then
4594 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
4596 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4597 _setopt "$_csh_profile" "source \"$_cshfile\""
4601 _tcsh_profile="$HOME/.tcshrc"
4602 if [ -f "$_tcsh_profile" ]; then
4603 _info "Installing alias to '$_tcsh_profile'"
4604 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
4605 if [ "$_c_home" ]; then
4606 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
4608 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4609 _setopt "$_tcsh_profile" "source \"$_cshfile\""
4617 if [ -z "$LE_WORKING_DIR" ]; then
4618 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
4623 if ! _initpath; then
4624 _err "Install failed."
4627 if [ "$_nocron" ]; then
4628 _debug "Skip install cron job"
4631 if ! _precheck "$_nocron"; then
4632 _err "Pre-check failed, can not install."
4637 if [ -d "$HOME/.le" ]; then
4638 for envfile in "le.env" "le.sh.env"; do
4639 if [ -f "$HOME/.le/$envfile" ]; then
4640 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
4642 _info "You are upgrading from le.sh"
4643 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
4644 mv "$HOME/.le" "$LE_WORKING_DIR"
4645 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4652 _info "Installing to $LE_WORKING_DIR"
4654 if ! mkdir -p "$LE_WORKING_DIR"; then
4655 _err "Can not create working dir: $LE_WORKING_DIR"
4659 chmod 700 "$LE_WORKING_DIR"
4661 if ! mkdir -p "$LE_CONFIG_HOME"; then
4662 _err "Can not create config dir: $LE_CONFIG_HOME"
4666 chmod 700 "$LE_CONFIG_HOME"
4668 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
4670 if [ "$?" != "0" ]; then
4671 _err "Install failed, can not copy $PROJECT_ENTRY"
4675 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
4677 _installalias "$_c_home"
4679 for subf in $_SUB_FOLDERS; do
4680 if [ -d "$subf" ]; then
4681 mkdir -p "$LE_WORKING_DIR/$subf"
4682 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
4686 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4690 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
4691 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
4694 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
4695 _saveaccountconf "CERT_HOME" "$CERT_HOME"
4698 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
4699 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
4702 if [ -z "$_nocron" ]; then
4703 installcronjob "$_c_home"
4706 if [ -z "$NO_DETECT_SH" ]; then
4708 if _exists bash; then
4709 _info "Good, bash is found, so change the shebang to use bash as preferred."
4710 _shebang='#!'"$(env bash -c "command -v bash")"
4711 _setShebang
"$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
4712 for subf
in $_SUB_FOLDERS; do
4713 if [ -d "$LE_WORKING_DIR/$subf" ]; then
4714 for _apifile
in "$LE_WORKING_DIR/$subf/"*.sh
; do
4715 _setShebang
"$_apifile" "$_shebang"
4728 if [ -z "$_nocron" ]; then
4735 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
4736 _info
"The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself."
4743 _profile
="$(_detect_profile)"
4744 if [ "$_profile" ]; then
4745 _info
"Uninstalling alias from: '$_profile'"
4746 text
="$(cat "$_profile")"
4747 echo "$text" |
sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
4750 _csh_profile
="$HOME/.cshrc"
4751 if [ -f "$_csh_profile" ]; then
4752 _info
"Uninstalling alias from: '$_csh_profile'"
4753 text
="$(cat "$_csh_profile")"
4754 echo "$text" |
sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
4757 _tcsh_profile
="$HOME/.tcshrc"
4758 if [ -f "$_tcsh_profile" ]; then
4759 _info
"Uninstalling alias from: '$_csh_profile'"
4760 text
="$(cat "$_tcsh_profile")"
4761 echo "$text" |
sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
4769 _info
"$(__green "===Starting cron
===")"
4770 if [ "$AUTO_UPGRADE" = "1" ]; then
4771 export LE_WORKING_DIR
4774 _err
"Cron:Upgrade failed!"
4778 .
"$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev
/null
4784 _info
"Auto upgraded to: $VER"
4789 _info
"$(__green "===End cron
===")"
4801 echo "Usage: $PROJECT_ENTRY command ...[parameters]....
4803 --help, -h Show this help message.
4804 --version, -v Show version info.
4805 --install Install $PROJECT_NAME to your system.
4806 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
4807 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
4808 --issue Issue a cert.
4809 --signcsr Issue a cert from an existing csr.
4810 --deploy Deploy the cert to your server.
4811 --install-cert Install the issued cert to apache/nginx or any other server.
4812 --renew, -r Renew a cert.
4813 --renew-all Renew all the certs.
4814 --revoke Revoke a cert.
4815 --remove Remove the cert from $PROJECT
4816 --list List all the certs.
4817 --showcsr Show the content of a csr.
4818 --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.
4819 --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
4820 --cron Run cron job to renew all the certs.
4821 --toPkcs Export the certificate and key to a pfx file.
4822 --toPkcs8 Convert to pkcs8 format.
4823 --update-account Update account info.
4824 --register-account Register account key.
4825 --create-account-key Create an account private key, professional use.
4826 --create-domain-key Create an domain private key, professional use.
4827 --createCSR, -ccsr Create CSR , professional use.
4828 --deactivate Deactivate the domain authz, professional use.
4831 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
4832 --force, -f Used to force to install or force to renew a cert immediately.
4833 --staging, --test Use staging server, just for test.
4834 --debug Output debug info.
4835 --output-insecure Output all the sensitive messages. By default all the credentials/sensitive messages are hidden from the output/debug/log for secure.
4836 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
4837 --standalone Use standalone mode.
4838 --stateless Use stateless mode, see: $_STATELESS_WIKI
4839 --tls Use standalone tls mode.
4840 --apache Use apache mode.
4841 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
4842 --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.
4844 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
4845 --accountkeylength, -ak [2048] Specifies the account key length.
4846 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
4847 --log-level 1|2 Specifies the log level, default is 1.
4848 --syslog [0|3|6|7] Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
4850 These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
4852 --cert-file After issue/renew, the cert will be copied to this path.
4853 --key-file After issue/renew, the key will be copied to this path.
4854 --ca-file After issue/renew, the intermediate cert will be copied to this path.
4855 --fullchain-file After issue/renew, the fullchain cert will be copied to this path.
4857 --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
4859 --accountconf Specifies a customized account config file.
4860 --home Specifies the home dir for $PROJECT_NAME .
4861 --cert-home Specifies the home dir to save all the certs, only valid for '--install' command.
4862 --config-home Specifies the home dir to save all the configurations.
4863 --useragent Specifies the user agent string. it will be saved for future use too.
4864 --accountemail Specifies the account email for registering, Only valid for the '--install' command.
4865 --accountkey Specifies the account key path, Only valid for the '--install' command.
4866 --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
4867 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
4868 --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
4869 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
4870 --listraw Only used for '--list' command, list the certs in raw format.
4871 --stopRenewOnError, -se Only valid for '--renew-all' command. Stop if one cert has error in renewal.
4872 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
4873 --ca-bundle Specifies the path to the CA certificate bundle to verify api server's certificate.
4874 --ca-path Specifies directory containing CA certificates in PEM format, used by wget or curl.
4875 --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.
4876 --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
4877 --csr Specifies the input csr.
4878 --pre-hook Command to be run before obtaining any certificates.
4879 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obtain/renew is success or failed.
4880 --renew-hook Command to be run once for each successfully renewed certificate.
4881 --deploy-hook The hook file to deploy cert
4882 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
4883 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
4884 --listen-v4 Force standalone/tls server to listen at ipv4.
4885 --listen-v6 Force standalone/tls server to listen at ipv6.
4886 --openssl-bin Specifies a custom openssl bin location.
4887 --use-wget Force to use wget, if you have both curl and wget installed.
4893 _info
"Installing from online archive."
4895 if [ ! "$BRANCH" ]; then
4899 target
="$PROJECT/archive/$BRANCH.tar.gz"
4900 _info
"Downloading $target"
4901 localname
="$BRANCH.tar.gz"
4902 if ! _get
"$target" >$localname; then
4903 _err
"Download error."
4907 _info
"Extracting $localname"
4908 if ! (tar xzf
$localname || gtar xzf
$localname); then
4909 _err
"Extraction error."
4913 cd "$PROJECT_NAME-$BRANCH"
4914 chmod +x
$PROJECT_ENTRY
4915 if .
/$PROJECT_ENTRY install "$_nocron"; then
4916 _info
"Install success!"
4921 rm -rf "$PROJECT_NAME-$BRANCH"
4929 export LE_WORKING_DIR
4930 cd "$LE_WORKING_DIR"
4931 _installOnline
"nocron"
4933 _info
"Upgrade success!"
4936 _err
"Upgrade failed!"
4941 _processAccountConf
() {
4942 if [ "$_useragent" ]; then
4943 _saveaccountconf
"USER_AGENT" "$_useragent"
4944 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
4945 _saveaccountconf
"USER_AGENT" "$USER_AGENT"
4948 if [ "$_accountemail" ]; then
4949 _saveaccountconf
"ACCOUNT_EMAIL" "$_accountemail"
4950 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
4951 _saveaccountconf
"ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
4954 if [ "$_openssl_bin" ]; then
4955 _saveaccountconf
"ACME_OPENSSL_BIN" "$_openssl_bin"
4956 elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
4957 _saveaccountconf
"ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
4960 if [ "$_auto_upgrade" ]; then
4961 _saveaccountconf
"AUTO_UPGRADE" "$_auto_upgrade"
4962 elif [ "$AUTO_UPGRADE" ]; then
4963 _saveaccountconf
"AUTO_UPGRADE" "$AUTO_UPGRADE"
4966 if [ "$_use_wget" ]; then
4967 _saveaccountconf
"ACME_USE_WGET" "$_use_wget"
4968 elif [ "$ACME_USE_WGET" ]; then
4969 _saveaccountconf
"ACME_USE_WGET" "$ACME_USE_WGET"
4977 _altdomains
="$NO_VALUE"
4980 _accountkeylength
=""
4997 _stopRenewOnError
=""
5018 while [ ${#} -gt 0 ]; do
5050 --installcert |
-i |
--install-cert)
5056 --renewAll |
--renewall |
--renew-all)
5068 --installcronjob |
--install-cronjob)
5069 _CMD
="installcronjob"
5071 --uninstallcronjob |
--uninstall-cronjob)
5072 _CMD
="uninstallcronjob"
5083 --createAccountKey |
--createaccountkey |
-cak |
--create-account-key)
5084 _CMD
="createAccountKey"
5086 --createDomainKey |
--createdomainkey |
-cdk |
--create-domain-key)
5087 _CMD
="createDomainKey"
5089 --createCSR |
--createcsr |
-ccr)
5095 --updateaccount |
--update-account)
5096 _CMD
="updateaccount"
5098 --registeraccount |
--register-account)
5099 _CMD
="registeraccount"
5104 if [ "$_dvalue" ]; then
5105 if _startswith
"$_dvalue" "-"; then
5106 _err
"'$_dvalue' is not a valid domain for parameter '$1'"
5109 if _is_idn
"$_dvalue" && ! _exists idn
; then
5110 _err
"It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
5114 if [ -z "$_domain" ]; then
5117 if [ "$_altdomains" = "$NO_VALUE" ]; then
5118 _altdomains
="$_dvalue"
5120 _altdomains
="$_altdomains,$_dvalue"
5135 if [ -z "$2" ] || _startswith
"$2" "-"; then
5136 DEBUG
="$DEBUG_LEVEL_DEFAULT"
5143 export OUTPUT_INSECURE
=1
5147 if [ -z "$_webroot" ]; then
5150 _webroot
="$_webroot,$wvalue"
5156 if [ -z "$_webroot" ]; then
5159 _webroot
="$_webroot,$wvalue"
5163 wvalue
="$MODE_STATELESS"
5164 if [ -z "$_webroot" ]; then
5167 _webroot
="$_webroot,$wvalue"
5172 _local_address
="$_local_address$lvalue,"
5177 if [ -z "$_webroot" ]; then
5180 _webroot
="$_webroot,$wvalue"
5185 if [ -z "$_webroot" ]; then
5188 _webroot
="$_webroot,$wvalue"
5193 if [ -z "$_webroot" ]; then
5196 _webroot
="$_webroot,$wvalue"
5201 if ! _startswith
"$2" "-"; then
5205 if [ -z "$_webroot" ]; then
5208 _webroot
="$_webroot,$wvalue"
5213 Le_DNSSleep
="$_dnssleep"
5221 --accountkeylength |
-ak)
5222 _accountkeylength
="$2"
5226 --cert-file |
--certpath)
5230 --key-file |
--keypath)
5234 --ca-file |
--capath)
5238 --fullchain-file |
--fullchainpath)
5239 _fullchain_file
="$2"
5242 --reloadcmd |
--reloadCmd)
5252 ACCOUNT_CONF_PATH
="$_accountconf"
5259 --certhome |
--cert-home)
5261 CERT_HOME
="$_certhome"
5266 LE_CONFIG_HOME
="$_confighome"
5271 USER_AGENT
="$_useragent"
5276 ACCOUNT_EMAIL
="$_accountemail"
5281 ACCOUNT_KEY_PATH
="$_accountkey"
5286 Le_RenewalDays
="$_days"
5291 Le_HTTPPort
="$_httpport"
5296 Le_TLSPort
="$_tlsport"
5303 --stopRenewOnError |
--stoprenewonerror |
-se)
5304 _stopRenewOnError
="1"
5311 _ca_bundle
="$(_readlink -f "$2")"
5312 CA_BUNDLE
="$_ca_bundle"
5343 if [ -z "$2" ] || _startswith
"$2" "-"; then
5344 _usage
"Please specify a value for '--deploy-hook'"
5347 _deploy_hook
="$_deploy_hook$2,"
5350 --ocsp-must-staple |
--ocsp)
5356 if _startswith
"$_logfile" '-'; then
5361 LOG_FILE
="$_logfile"
5362 if [ -z "$LOG_LEVEL" ]; then
5363 LOG_LEVEL
="$DEFAULT_LOG_LEVEL"
5368 LOG_LEVEL
="$_log_level"
5372 if ! _startswith
"$2" '-'; then
5376 if [ -z "$_syslog" ]; then
5377 _syslog
="$SYSLOG_LEVEL_DEFAULT"
5382 if [ -z "$_auto_upgrade" ] || _startswith
"$_auto_upgrade" '-'; then
5387 AUTO_UPGRADE
="$_auto_upgrade"
5391 Le_Listen_V4
="$_listen_v4"
5395 Le_Listen_V6
="$_listen_v6"
5399 ACME_OPENSSL_BIN
="$_openssl_bin"
5407 _err
"Unknown parameter : $1"
5415 if [ "${_CMD}" != "install" ]; then
5417 if [ "$_log" ]; then
5418 if [ -z "$_logfile" ]; then
5419 _logfile
="$DEFAULT_LOG_FILE"
5422 if [ "$_logfile" ]; then
5423 _saveaccountconf
"LOG_FILE" "$_logfile"
5424 LOG_FILE
="$_logfile"
5427 if [ "$_log_level" ]; then
5428 _saveaccountconf
"LOG_LEVEL" "$_log_level"
5429 LOG_LEVEL
="$_log_level"
5432 if [ "$_syslog" ]; then
5433 if _exists logger
; then
5434 if [ "$_syslog" = "0" ]; then
5435 _clearaccountconf
"SYS_LOG"
5437 _saveaccountconf
"SYS_LOG" "$_syslog"
5441 _err
"The 'logger' command is not found, can not enable syslog."
5442 _clearaccountconf
"SYS_LOG"
5450 _debug2 LE_WORKING_DIR
"$LE_WORKING_DIR"
5452 if [ "$DEBUG" ]; then
5457 install) install "$_nocron" "$_confighome" ;;
5458 uninstall
) uninstall
"$_nocron" ;;
5461 issue
"$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address"
5464 deploy
"$_domain" "$_deploy_hook" "$_ecc"
5467 signcsr
"$_csr" "$_webroot"
5470 showcsr
"$_csr" "$_domain"
5473 installcert
"$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
5476 renew
"$_domain" "$_ecc"
5479 renewAll
"$_stopRenewOnError"
5482 revoke
"$_domain" "$_ecc"
5485 remove
"$_domain" "$_ecc"
5488 deactivate
"$_domain,$_altdomains"
5491 registeraccount
"$_accountkeylength"
5499 installcronjob
) installcronjob
"$_confighome" ;;
5500 uninstallcronjob
) uninstallcronjob
;;
5503 toPkcs
"$_domain" "$_password" "$_ecc"
5506 toPkcs8
"$_domain" "$_ecc"
5509 createAccountKey
"$_accountkeylength"
5512 createDomainKey
"$_domain" "$_keylength"
5515 createCSR
"$_domain" "$_altdomains" "$_ecc"
5519 if [ "$_CMD" ]; then
5520 _err
"Invalid command: $_CMD"
5527 if [ "$_ret" != "0" ]; then
5531 if [ "${_CMD}" = "install" ]; then
5532 if [ "$_log" ]; then
5533 if [ -z "$LOG_FILE" ]; then
5534 LOG_FILE
="$DEFAULT_LOG_FILE"
5536 _saveaccountconf
"LOG_FILE" "$LOG_FILE"
5539 if [ "$_log_level" ]; then
5540 _saveaccountconf
"LOG_LEVEL" "$_log_level"
5543 if [ "$_syslog" ]; then
5544 if _exists logger
; then
5545 if [ "$_syslog" = "0" ]; then
5546 _clearaccountconf
"SYS_LOG"
5548 _saveaccountconf
"SYS_LOG" "$_syslog"
5551 _err
"The 'logger' command is not found, can not enable syslog."
5552 _clearaccountconf
"SYS_LOG"
5562 if [ "$INSTALLONLINE" ]; then
5569 [ -z "$1" ] && showhelp
&& return
5570 if _startswith
"$1" '-'; then _process
"$@"; else "$@"; fi